Skip to content

Commit 1b68112

Browse files
ioworker0akpm00
authored andcommitted
mm/madvise: introduce clear_young_dirty_ptes() batch helper
Patch series "mm/madvise: enhance lazyfreeing with mTHP in madvise_free", v10. This patchset adds support for lazyfreeing multi-size THP (mTHP) without needing to first split the large folio via split_folio(). However, we still need to split a large folio that is not fully mapped within the target range. If a large folio is locked or shared, or if we fail to split it, we just leave it in place and advance to the next PTE in the range. But note that the behavior is changed; previously, any failure of this sort would cause the entire operation to give up. As large folios become more common, sticking to the old way could result in wasted opportunities. Performance Testing =================== On an Intel I5 CPU, lazyfreeing a 1GiB VMA backed by PTE-mapped folios of the same size results in the following runtimes for madvise(MADV_FREE) in seconds (shorter is better): Folio Size | Old | New | Change ------------------------------------------ 4KiB | 0.590251 | 0.590259 | 0% 16KiB | 2.990447 | 0.185655 | -94% 32KiB | 2.547831 | 0.104870 | -95% 64KiB | 2.457796 | 0.052812 | -97% 128KiB | 2.281034 | 0.032777 | -99% 256KiB | 2.230387 | 0.017496 | -99% 512KiB | 2.189106 | 0.010781 | -99% 1024KiB | 2.183949 | 0.007753 | -99% 2048KiB | 0.002799 | 0.002804 | 0% This patch (of 4): This commit introduces clear_young_dirty_ptes() to replace mkold_ptes(). By doing so, we can use the same function for both use cases (madvise_pageout and madvise_free), and it also provides the flexibility to only clear the dirty flag in the future if needed. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Lance Yang <[email protected]> Suggested-by: Ryan Roberts <[email protected]> Acked-by: David Hildenbrand <[email protected]> Reviewed-by: Ryan Roberts <[email protected]> Cc: Barry Song <[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 80e7502 commit 1b68112

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

include/linux/mm_types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,15 @@ enum fault_flag {
13681368

13691369
typedef unsigned int __bitwise zap_flags_t;
13701370

1371+
/* Flags for clear_young_dirty_ptes(). */
1372+
typedef int __bitwise cydp_t;
1373+
1374+
/* Clear the access bit */
1375+
#define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0))
1376+
1377+
/* Clear the dirty bit */
1378+
#define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1))
1379+
13711380
/*
13721381
* FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
13731382
* other. Here is what they mean, and how to use them:

include/linux/pgtable.h

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,36 +361,6 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
361361
}
362362
#endif
363363

364-
#ifndef mkold_ptes
365-
/**
366-
* mkold_ptes - Mark PTEs that map consecutive pages of the same folio as old.
367-
* @vma: VMA the pages are mapped into.
368-
* @addr: Address the first page is mapped at.
369-
* @ptep: Page table pointer for the first entry.
370-
* @nr: Number of entries to mark old.
371-
*
372-
* May be overridden by the architecture; otherwise, implemented as a simple
373-
* loop over ptep_test_and_clear_young().
374-
*
375-
* Note that PTE bits in the PTE range besides the PFN can differ. For example,
376-
* some PTEs might be write-protected.
377-
*
378-
* Context: The caller holds the page table lock. The PTEs map consecutive
379-
* pages that belong to the same folio. The PTEs are all in the same PMD.
380-
*/
381-
static inline void mkold_ptes(struct vm_area_struct *vma, unsigned long addr,
382-
pte_t *ptep, unsigned int nr)
383-
{
384-
for (;;) {
385-
ptep_test_and_clear_young(vma, addr, ptep);
386-
if (--nr == 0)
387-
break;
388-
ptep++;
389-
addr += PAGE_SIZE;
390-
}
391-
}
392-
#endif
393-
394364
#ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
395365
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
396366
static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
@@ -489,6 +459,50 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
489459
}
490460
#endif
491461

462+
#ifndef clear_young_dirty_ptes
463+
/**
464+
* clear_young_dirty_ptes - Mark PTEs that map consecutive pages of the
465+
* same folio as old/clean.
466+
* @mm: Address space the pages are mapped into.
467+
* @addr: Address the first page is mapped at.
468+
* @ptep: Page table pointer for the first entry.
469+
* @nr: Number of entries to mark old/clean.
470+
* @flags: Flags to modify the PTE batch semantics.
471+
*
472+
* May be overridden by the architecture; otherwise, implemented by
473+
* get_and_clear/modify/set for each pte in the range.
474+
*
475+
* Note that PTE bits in the PTE range besides the PFN can differ. For example,
476+
* some PTEs might be write-protected.
477+
*
478+
* Context: The caller holds the page table lock. The PTEs map consecutive
479+
* pages that belong to the same folio. The PTEs are all in the same PMD.
480+
*/
481+
static inline void clear_young_dirty_ptes(struct vm_area_struct *vma,
482+
unsigned long addr, pte_t *ptep,
483+
unsigned int nr, cydp_t flags)
484+
{
485+
pte_t pte;
486+
487+
for (;;) {
488+
if (flags == CYDP_CLEAR_YOUNG)
489+
ptep_test_and_clear_young(vma, addr, ptep);
490+
else {
491+
pte = ptep_get_and_clear(vma->vm_mm, addr, ptep);
492+
if (flags & CYDP_CLEAR_YOUNG)
493+
pte = pte_mkold(pte);
494+
if (flags & CYDP_CLEAR_DIRTY)
495+
pte = pte_mkclean(pte);
496+
set_pte_at(vma->vm_mm, addr, ptep, pte);
497+
}
498+
if (--nr == 0)
499+
break;
500+
ptep++;
501+
addr += PAGE_SIZE;
502+
}
503+
}
504+
#endif
505+
492506
static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
493507
pte_t *ptep)
494508
{

mm/madvise.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
507507
continue;
508508

509509
if (!pageout && pte_young(ptent)) {
510-
mkold_ptes(vma, addr, pte, nr);
510+
clear_young_dirty_ptes(vma, addr, pte, nr,
511+
CYDP_CLEAR_YOUNG);
511512
tlb_remove_tlb_entries(tlb, pte, nr, addr);
512513
}
513514

0 commit comments

Comments
 (0)