Skip to content

Commit 004fc58

Browse files
Anshuman Khandualctmarinas
authored andcommitted
arm64/mm: Intercept pfn changes in set_pte_at()
Changing pfn on a user page table mapped entry, without first going through break-before-make (BBM) procedure is unsafe. This just updates set_pte_at() to intercept such changes, via an updated pgattr_change_is_safe(). This new check happens via __check_racy_pte_update(), which has now been renamed as __check_safe_pte_update(). Cc: Will Deacon <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Andrew Morton <[email protected]> Cc: [email protected] Cc: [email protected] Acked-by: Mark Rutland <[email protected]> Signed-off-by: Anshuman Khandual <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent a70f00e commit 004fc58

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

arch/arm64/include/asm/pgtable.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte)
275275
}
276276

277277
extern void __sync_icache_dcache(pte_t pteval);
278+
bool pgattr_change_is_safe(u64 old, u64 new);
278279

279280
/*
280281
* PTE bits configuration in the presence of hardware Dirty Bit Management
@@ -292,7 +293,7 @@ extern void __sync_icache_dcache(pte_t pteval);
292293
* PTE_DIRTY || (PTE_WRITE && !PTE_RDONLY)
293294
*/
294295

295-
static inline void __check_racy_pte_update(struct mm_struct *mm, pte_t *ptep,
296+
static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep,
296297
pte_t pte)
297298
{
298299
pte_t old_pte;
@@ -318,6 +319,9 @@ static inline void __check_racy_pte_update(struct mm_struct *mm, pte_t *ptep,
318319
VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte),
319320
"%s: racy dirty state clearing: 0x%016llx -> 0x%016llx",
320321
__func__, pte_val(old_pte), pte_val(pte));
322+
VM_WARN_ONCE(!pgattr_change_is_safe(pte_val(old_pte), pte_val(pte)),
323+
"%s: unsafe attribute change: 0x%016llx -> 0x%016llx",
324+
__func__, pte_val(old_pte), pte_val(pte));
321325
}
322326

323327
static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
@@ -346,7 +350,7 @@ static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
346350
mte_sync_tags(old_pte, pte);
347351
}
348352

349-
__check_racy_pte_update(mm, ptep, pte);
353+
__check_safe_pte_update(mm, ptep, pte);
350354

351355
set_pte(ptep, pte);
352356
}

arch/arm64/mm/mmu.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static phys_addr_t __init early_pgtable_alloc(int shift)
133133
return phys;
134134
}
135135

136-
static bool pgattr_change_is_safe(u64 old, u64 new)
136+
bool pgattr_change_is_safe(u64 old, u64 new)
137137
{
138138
/*
139139
* The following mapping attributes may be updated in live
@@ -142,9 +142,13 @@ static bool pgattr_change_is_safe(u64 old, u64 new)
142142
pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
143143

144144
/* creating or taking down mappings is always safe */
145-
if (old == 0 || new == 0)
145+
if (!pte_valid(__pte(old)) || !pte_valid(__pte(new)))
146146
return true;
147147

148+
/* A live entry's pfn should not change */
149+
if (pte_pfn(__pte(old)) != pte_pfn(__pte(new)))
150+
return false;
151+
148152
/* live contiguous mappings may not be manipulated at all */
149153
if ((old | new) & PTE_CONT)
150154
return false;

0 commit comments

Comments
 (0)