Skip to content

Commit 89e8685

Browse files
ioworker0akpm00
authored andcommitted
mm/arm64: override clear_young_dirty_ptes() batch helper
The per-pte get_and_clear/modify/set approach would result in unfolding/refolding for contpte mappings on arm64. So we need to override clear_young_dirty_ptes() for arm64 to avoid it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Lance Yang <[email protected]> Suggested-by: Barry Song <[email protected]> Suggested-by: Ryan Roberts <[email protected]> Reviewed-by: Ryan Roberts <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jeff Xie <[email protected]> Cc: Kefeng Wang <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Muchun Song <[email protected]> Cc: Peter Xu <[email protected]> Cc: Yang Shi <[email protected]> Cc: Yin Fengwei <[email protected]> Cc: Zach O'Keefe <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 1b68112 commit 89e8685

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

arch/arm64/include/asm/pgtable.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,46 @@ static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address,
12231223
__ptep_set_wrprotect(mm, address, ptep);
12241224
}
12251225

1226+
static inline void __clear_young_dirty_pte(struct vm_area_struct *vma,
1227+
unsigned long addr, pte_t *ptep,
1228+
pte_t pte, cydp_t flags)
1229+
{
1230+
pte_t old_pte;
1231+
1232+
do {
1233+
old_pte = pte;
1234+
1235+
if (flags & CYDP_CLEAR_YOUNG)
1236+
pte = pte_mkold(pte);
1237+
if (flags & CYDP_CLEAR_DIRTY)
1238+
pte = pte_mkclean(pte);
1239+
1240+
pte_val(pte) = cmpxchg_relaxed(&pte_val(*ptep),
1241+
pte_val(old_pte), pte_val(pte));
1242+
} while (pte_val(pte) != pte_val(old_pte));
1243+
}
1244+
1245+
static inline void __clear_young_dirty_ptes(struct vm_area_struct *vma,
1246+
unsigned long addr, pte_t *ptep,
1247+
unsigned int nr, cydp_t flags)
1248+
{
1249+
pte_t pte;
1250+
1251+
for (;;) {
1252+
pte = __ptep_get(ptep);
1253+
1254+
if (flags == (CYDP_CLEAR_YOUNG | CYDP_CLEAR_DIRTY))
1255+
__set_pte(ptep, pte_mkclean(pte_mkold(pte)));
1256+
else
1257+
__clear_young_dirty_pte(vma, addr, ptep, pte, flags);
1258+
1259+
if (--nr == 0)
1260+
break;
1261+
ptep++;
1262+
addr += PAGE_SIZE;
1263+
}
1264+
}
1265+
12261266
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
12271267
#define __HAVE_ARCH_PMDP_SET_WRPROTECT
12281268
static inline void pmdp_set_wrprotect(struct mm_struct *mm,
@@ -1379,6 +1419,9 @@ extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
13791419
extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
13801420
unsigned long addr, pte_t *ptep,
13811421
pte_t entry, int dirty);
1422+
extern void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
1423+
unsigned long addr, pte_t *ptep,
1424+
unsigned int nr, cydp_t flags);
13821425

13831426
static __always_inline void contpte_try_fold(struct mm_struct *mm,
13841427
unsigned long addr, pte_t *ptep, pte_t pte)
@@ -1603,6 +1646,17 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma,
16031646
return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty);
16041647
}
16051648

1649+
#define clear_young_dirty_ptes clear_young_dirty_ptes
1650+
static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,
1651+
unsigned long addr, pte_t *ptep,
1652+
unsigned int nr, cydp_t flags)
1653+
{
1654+
if (likely(nr == 1 && !pte_cont(__ptep_get(ptep))))
1655+
__clear_young_dirty_ptes(vma, addr, ptep, nr, flags);
1656+
else
1657+
contpte_clear_young_dirty_ptes(vma, addr, ptep, nr, flags);
1658+
}
1659+
16061660
#else /* CONFIG_ARM64_CONTPTE */
16071661

16081662
#define ptep_get __ptep_get
@@ -1622,6 +1676,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma,
16221676
#define wrprotect_ptes __wrprotect_ptes
16231677
#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
16241678
#define ptep_set_access_flags __ptep_set_access_flags
1679+
#define clear_young_dirty_ptes __clear_young_dirty_ptes
16251680

16261681
#endif /* CONFIG_ARM64_CONTPTE */
16271682

arch/arm64/mm/contpte.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,35 @@ void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
361361
}
362362
EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
363363

364+
void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
365+
unsigned long addr, pte_t *ptep,
366+
unsigned int nr, cydp_t flags)
367+
{
368+
/*
369+
* We can safely clear access/dirty without needing to unfold from
370+
* the architectures perspective, even when contpte is set. If the
371+
* range starts or ends midway through a contpte block, we can just
372+
* expand to include the full contpte block. While this is not
373+
* exactly what the core-mm asked for, it tracks access/dirty per
374+
* folio, not per page. And since we only create a contpte block
375+
* when it is covered by a single folio, we can get away with
376+
* clearing access/dirty for the whole block.
377+
*/
378+
unsigned long start = addr;
379+
unsigned long end = start + nr;
380+
381+
if (pte_cont(__ptep_get(ptep + nr - 1)))
382+
end = ALIGN(end, CONT_PTE_SIZE);
383+
384+
if (pte_cont(__ptep_get(ptep))) {
385+
start = ALIGN_DOWN(start, CONT_PTE_SIZE);
386+
ptep = contpte_align_down(ptep);
387+
}
388+
389+
__clear_young_dirty_ptes(vma, start, ptep, end - start, flags);
390+
}
391+
EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
392+
364393
int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
365394
unsigned long addr, pte_t *ptep,
366395
pte_t entry, int dirty)

0 commit comments

Comments
 (0)