Reduce tlb and interrupt numbers over 90% by improving folio migration
Subsystems (9)
Byungchul Park
reviewerv9 cover Reduce tlb and interrupt numbers over 90% by improving folio migration
Hi everyone,
While I'm working with a tiered memory system e.g. CXL memory, I have been facing migration overhead esp. tlb shootdown on promotion or demotion between different tiers. Yeah.. most tlb shootdowns on migration through hinting fault can be avoided thanks to Huang Ying's work, commit 4d4b6d66db ("mm,unmap: avoid flushing tlb in batch if PTE is inaccessible"). See the following link for more information:
https://lore.kernel.org/lkml/[email protected]/
However, it's only for ones using hinting fault. I thought it'd be much better if we have a general mechanism to reduce all tlb numbers that we can ultimately apply to any type of migration.
I'm suggesting a mechanism called MIGRC that stands for 'Migration Read Copy', to reduce tlb numbers by deferring tlb flush until the source folios at migration actually become used, of course, only if the target PTE don't have write permission.
To achieve that:
1. For the folios that map only to non-writable tlb entries, prevent tlb flush during migration but perform it just before the source folios actually become used out of buddy or pcp.
2. When any non-writable tlb entry changes to writable e.g. through fault handler, give up migrc mechanism and perform tlb flush required right away.
No matter what type of workload is used for performance evaluation, the result would be positive thanks to the unconditional reduction of tlb flushes, tlb misses and interrupts. For the test, I picked up XSBench that is widely used for performance analysis on high performance computing architectures - https://github.com/ANL-CESAR/XSBench.
The result would depend on memory latency and how often reclaim runs, which implies tlb miss overhead and how many times migration happens. The slower the memory is and the more reclaim runs, the better migrc works so as to obtain the better result. In my system, the result shows:
1. itlb flushes are reduced over 90%.
2. itlb misses are reduced over 30%.
3. All the other tlb numbers also get enhanced.
4. tlb shootdown interrupts are reduced over 90%.
5. The test program runtime is reduced over 5%.
The test envitonment:
Architecture - x86_64
QEMU - kvm enabled, host cpu
Numa - 2 nodes (16 CPUs 1GB, no CPUs 99GB)
Linux Kernel - v6.9-rc4, numa balancing tiering on, demotion enabled
< measurement: raw data - tlb and interrupt numbers >
$ perf stat -a \
-e itlb.itlb_flush \
-e tlb_flush.dtlb_thread \
-e tlb_flush.stlb_any \
-e dtlb-load-misses \
-e dtlb-store-misses \
-e itlb-load-misses \
XSBench -t 16 -p 50000000$ grep "TLB shootdowns" /proc/interrupts
BEFORE
------
40417078 itlb.itlb_flush 234852566 tlb_flush.dtlb_thread 153192357 tlb_flush.stlb_any
119001107892 dTLB-load-misses
307921167 dTLB-store-misses
1355272118 iTLB-load-misses
TLB: 1364803 1303670 1333921 1349607
1356934 1354216 1332972 1342842
1350265 1316443 1355928 1360793
1298239 1326358 1343006 1340971TLB shootdowns
AFTER
-----
3316495 itlb.itlb_flush 138912511 tlb_flush.dtlb_thread 115199341 tlb_flush.stlb_any
117610390021 dTLB-load-misses
198042233 dTLB-store-misses
840066984 iTLB-load-misses
TLB: 117257 119219 117178 115737
117967 118948 117508 116079
116962 117266 117320 117215
105808 103934 115672 117610TLB shootdowns
< measurement: user experience - runtime >
$ time XSBench -t 16 -p 50000000
BEFORE
------
Threads: 16 Runtime: 968.783 seconds Lookups: 1,700,000,000 Lookups/s: 1,754,778
15208.91s user 141.44s system 1564% cpu 16:20.98 total
AFTER
-----
Threads: 16 Runtime: 913.210 seconds Lookups: 1,700,000,000 Lookups/s: 1,861,565
14351.69s user 138.23s system 1565% cpu 15:25.47 total
---
Changes from v8:
1. Rebase on the latest, v6.9-rc4.
2. Supplement comments and commit message.
3. Change the candidate to apply migrc mechnism:
BEFORE - The source folios at demotion and promotion. AFTER - The souce folios at any type of migration.
4. Change how migrc mechanism works:
BEFORE - Reduce tlb flushes by deferring folio_free() for
source folios during demotion and promotion.
AFTER - Reduce tlb flushes by deferring tlb flush until they
actually become used, out of pcp or buddy. The current version of migrc does *not* defer calling folio_free() but let it go as it is as the same as vanilla kernel, with the folios marked kind of 'need to tlb flush'. And then handle the flush when the page exits from pcp or buddy so as to prevent changing vm stats e.g. free pages.
Changes from v7:
1. Rewrite cover letter to explain what 'migrc' mechasism is. (feedbacked by Andrew Morton)
2. Supplement the commit message of a patch 'mm: Add APIs to free a folio directly to the buddy bypassing pcp'. (feedbacked by Andrew Morton)
Changes from v6:
1. Fix build errors in case of CONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSH disabled by moving migrc_flush_{start,end}() calls from arch code to try_to_unmap_flush() in mm/rmap.c.
Changes from v5:
1. Fix build errors in case of CONFIG_MIGRATION disabled or CONFIG_HWPOISON_INJECT moduled. (feedbacked by kernel test bot and Raymond Jay Golo)
2. Organize migrc code with two kconfigs, CONFIG_MIGRATION and CONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSH.
Changes from v4:
1. Rebase on v6.7.
2. Fix build errors in arm64 that is doing nothing for tlb flush but has CONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSH. (reported by kernel test robot)
3. Don't use any page flag. So the system would give up migrc mechanism more often but it's okay. The final improvement is good enough.
4. Instead, optimize full tlb flush(arch_tlbbatch_flush()) by avoiding redundant CPUs from tlb flush.
Changes from v3:
1. Don't use the kconfig, CONFIG_MIGRC, and remove sysctl knob, migrc_enable. (feedbacked by Nadav)
2. Remove the optimization skipping CPUs that have already performed tlb flushes needed by any reason when performing tlb flushes by migrc because I can't tell the performance difference between w/ the optimization and w/o that. (feedbacked by Nadav)
3. Minimize arch-specific code. While at it, move all the migrc declarations and inline functions from include/linux/mm.h to mm/internal.h (feedbacked by Dave Hansen, Nadav)
4. Separate a part making migrc paused when the system is in high memory pressure to another patch. (feedbacked by Nadav)
5. Rename:
a. arch_tlbbatch_clean() to arch_tlbbatch_clear(), b. tlb_ubc_nowr to tlb_ubc_ro,
c. migrc_try_flush_free_folios() to migrc_flush_free_folios(), d. migrc_stop to migrc_pause.
(feedbacked by Nadav)
6. Use ->lru list_head instead of introducing a new llist_head. (feedbacked by Nadav)
7. Use non-atomic operations of page-flag when it's safe. (feedbacked by Nadav)
8. Use stack instead of keeping a pointer of 'struct migrc_req' in struct task, which is for manipulating it locally. (feedbacked by Nadav)
9. Replace a lot of simple functions to inline functions placed in a header, mm/internal.h. (feedbacked by Nadav)
10. Add additional sufficient comments. (feedbacked by Nadav)
11. Remove a lot of wrapper functions. (feedbacked by Nadav)
Changes from RFC v2:
1. Remove additional occupation in struct page. To do that, unioned with lru field for migrc's list and added a page flag. I know page flag is a thing that we don't like to add but no choice because migrc should distinguish folios under migrc's control from others. Instead, I force migrc to be used only on 64 bit system to mitigate you guys from getting angry.
2. Remove meaningless internal object allocator that I introduced to minimize impact onto the system. However, a ton of tests showed there was no difference.
3. Stop migrc from working when the system is in high memory pressure like about to perform direct reclaim. At the condition where the swap mechanism is heavily used, I found the system suffered from regression without this control.
4. Exclude folios that pte_dirty() == true from migrc's interest so that migrc can work simpler.
5. Combine several patches that work tightly coupled to one.
6. Add sufficient comments for better review.
7. Manage migrc's request in per-node manner (from globally).
8. Add tlb miss improvement in commit message.
9. Test with more CPUs(4 -> 16) to see bigger improvement.
Changes from RFC:
1. Fix a bug triggered when a destination folio at the previous migration becomes a source folio at the next migration, before the folio gets handled properly so that the folio can play with another migration. There was inconsistency in the folio's state. Fixed it.
2. Split the patch set into more pieces so that the folks can review better. (Feedbacked by Nadav Amit)
3. Fix a wrong usage of barrier e.g. smp_mb__after_atomic(). (Feedbacked by Nadav Amit)
4. Tried to add sufficient comments to explain the patch set better. (Feedbacked by Nadav Amit)
Byungchul Park (8):
x86/tlb: add APIs manipulating tlb batch's arch data
arm64: tlbflush: add APIs manipulating tlb batch's arch data mm/rmap: recognize read-only tlb entries during batched tlb flush x86/tlb, mm/rmap: separate arch_tlbbatch_clear() out of
arch_tlbbatch_flush()
mm: separate move/undo parts from migrate_pages_batch()
mm: buddy: make room for a new variable, mgen, in struct page
mm: add folio_put_mgen() to deliver migrc's generation number to pcp
or buddy
mm: defer tlb flush until the source folios at migration actually get
used
arch/arm64/include/asm/tlbflush.h | 18 ++ arch/x86/include/asm/tlbflush.h | 18 ++ arch/x86/mm/tlb.c | 2 - include/linux/mm.h | 22 ++ include/linux/mm_types.h | 39 ++- include/linux/sched.h | 10 + mm/compaction.c | 10 + mm/internal.h | 85 +++++- mm/memory.c | 8 + mm/migrate.c | 487 ++++++++++++++++++++++++++---- mm/page_alloc.c | 125 ++++++-- mm/page_reporting.c | 10 + mm/rmap.c | 40 ++- mm/swap.c | 19 +- 14 files changed, 795 insertions(+), 98 deletions(-)
base-commit: 0bbac3facb5d6cc0171c45c9873a2dc96bea9680
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 1/8 x86/tlb: add APIs manipulating tlb batch's arch data
This is a preparation for migrc mechanism that needs to recognize read-only tlb entries during migration by separating tlb batch arch data into two, one is for read-only entries and the other is for writable ones, and merging those two when needed.
Migrc also needs to optimize tlb shootdown by skipping CPUs that have already performed tlb flush needed for a while. To support it, added APIs manipulating arch data for x86.
--- arch/x86/include/asm/tlbflush.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/include/asm/tlbflush.h +18 -0
@@ -5,6 +5,7 @@#include <linux/mm_types.h> #include <linux/mmu_notifier.h> #include <linux/sched.h> +#include <linux/cpumask.h> #include <asm/processor.h> #include <asm/cpufeature.h>
@@ -293,6 +294,23 @@ static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm)extern void arch_tlbbatch_flush (struct arch_tlbflush_unmap_batch *batch); +static inline void arch_tlbbatch_clear (struct arch_tlbflush_unmap_batch *batch) +{ +cpumask_clear (&batch->cpumask); +} + +static inline void arch_tlbbatch_fold (struct arch_tlbflush_unmap_batch *bdst, +struct arch_tlbflush_unmap_batch *bsrc) +{ +cpumask_or (&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); +} + +static inline bool arch_tlbbatch_done (struct arch_tlbflush_unmap_batch *bdst, +struct arch_tlbflush_unmap_batch *bsrc) +{ +return !cpumask_andnot (&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); +} +static inline bool pte_flags_need_flush (unsigned long oldflags,unsigned long newflags,bool ignore_access)
Signature · 1 line
2.17.1
Re: [PATCH v9 1/8] x86/tlb: add APIs manipulating tlb batch's arch data
Quoted 4 lines · Byungchul Park
This is a preparation for migrc mechanism that needs to recognize read-only tlb entries during migration by separating tlb batch arch data into two, one is for read-only entries and the other is for writable ones, and merging those two when needed.
I have no idea what migrc mechanism means. Please add enough context into the change log so the reviewer does not have to go through loops and hoops to figure out what this is about.
Thanks,
tglx
Byungchul Park
reviewerRe: [PATCH v9 1/8] x86/tlb: add APIs manipulating tlb batch's arch data
Quoted 9 lines · Thomas Gleixner
Quoted 4 lines · Byungchul Park
This is a preparation for migrc mechanism that needs to recognize read-only tlb entries during migration by separating tlb batch arch data into two, one is for read-only entries and the other is for writable ones, and merging those two when needed.
I have no idea what migrc mechanism means. Please add enough context into the change log so the reviewer does not have to go through loops and hoops to figure out what this is about.
I will add a simple description explaining 'what migrc is' at every patch mentioning migrc, from the next spin. Thank you for the advice.
JFYI, you can see what migrc is in the following link:
https://lore.kernel.org/lkml/[email protected]/
Byungchul
Quoted 3 lines
Thanks,
tglx
Byungchul Park
reviewerPATCH v9 2/8 arm64: tlbflush: add APIs manipulating tlb batch's arch data
In reply to Byungchul ParkThis is a preparation for migrc mechanism that requires to manipulate tlb batch's arch data. Even though arm64 does nothing for it, arch with CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH should provide the APIs.
--- arch/arm64/include/asm/tlbflush.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/arm64/include/asm/tlbflush.h +18 -0
@@ -347,6 +347,24 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)dsb (ish); } +static inline void arch_tlbbatch_clear (struct arch_tlbflush_unmap_batch *batch) +{ +/* nothing to do */ +} + +static inline void arch_tlbbatch_fold (struct arch_tlbflush_unmap_batch *bdst, +struct arch_tlbflush_unmap_batch *bsrc) +{ +/* nothing to do */ +} + +static inline bool arch_tlbbatch_done (struct arch_tlbflush_unmap_batch *bdst, +struct arch_tlbflush_unmap_batch *bsrc) +{ +/* Kernel can consider tlb batch always has been done. */ +return true; +} +/* * This is meant to avoid soft lock-ups on large TLB flushing ranges and not * necessarily a performance improvement.
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 3/8 mm/rmap: recognize read-only tlb entries during batched tlb flush
In reply to Byungchul ParkFunctionally, no change. This is a preparation for migrc mechanism that requires to recognize read-only tlb entries and handle them in a different way. The newly introduced API, fold_ubc(), will be used by migrc mechanism.
--- include/linux/sched.h | 1 + mm/internal.h | 4 ++++ mm/rmap.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-)
include/linux/sched.h +1 -0
@@ -1334,6 +1334,7 @@ struct task_struct {#endif struct tlbflush_unmap_batch tlb_ubc; +struct tlbflush_unmap_batch tlb_ubc_ro;/* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe;
mm/internal.h +4 -0
@@ -1019,6 +1019,7 @@ extern struct workqueue_struct *mm_percpu_wq;void try_to_unmap_flush (void );void try_to_unmap_flush_dirty (void );void flush_tlb_batched_pending (struct mm_struct *mm); +void fold_ubc (struct tlbflush_unmap_batch *dst,struct tlbflush_unmap_batch *src);#else static inline void try_to_unmap_flush (void ) {
@@ -1029,6 +1030,9 @@ static inline void try_to_unmap_flush_dirty(void)static inline void flush_tlb_batched_pending (struct mm_struct *mm) { } +static inline void fold_ubc (struct tlbflush_unmap_batch *dst,struct tlbflush_unmap_batch *src) +{ +}#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ extern const struct trace_print_flags pageflag_names[];
mm/rmap.c +30 -1
@@ -635,6 +635,28 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, }#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH + +void fold_ubc (struct tlbflush_unmap_batch *dst, +struct tlbflush_unmap_batch *src) +{ +if (!src->flush_required) +return ; + +/* +* Fold src to dst. +*/ +arch_tlbbatch_fold (&dst->arch, &src->arch); + dst->writable = dst->writable || src->writable; + dst->flush_required = true; + +/* +* Reset src. +*/ +arch_tlbbatch_clear (&src->arch); + src->flush_required = false; + src->writable = false; +} +/* * Flush TLB entries for recently unmapped pages from remote CPUs. It is * important if a PTE was dirty when it was unmapped that it's flushed
@@ -644,7 +666,9 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio,void try_to_unmap_flush (void ) {struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; +struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; +fold_ubc (tlb_ubc, tlb_ubc_ro);if (!tlb_ubc->flush_required)return ;
@@ -675,13 +699,18 @@ void try_to_unmap_flush_dirty(void)static void set_tlb_ubc_flush_pending (struct mm_struct *mm,pte_t pteval,unsigned long uaddr) { -struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; +struct tlbflush_unmap_batch *tlb_ubc;int batch;bool writable =pte_dirty (pteval);if (!pte_accessible (mm, pteval))return ; +if (pte_write (pteval) || writable) + tlb_ubc = ¤t->tlb_ubc; +else + tlb_ubc = ¤t->tlb_ubc_ro; +arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr); tlb_ubc->flush_required = true;
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 4/8 x86/tlb, mm/rmap: separate arch_tlbbatch_clear() out of arch_tlbbatch_flush()
In reply to Byungchul ParkThis is a preparation for migrc mechanism that requires to avoid redundant tlb flushes by manipulating tlb batch's arch data. To achieve that, it's needed to separate the part clearing the tlb batch's arch data out of arch_tlbbatch_flush().
--- arch/x86/mm/tlb.c | 2 -- mm/rmap.c | 1 + 2 files changed, 1 insertion(+), 2 deletions(-)
arch/x86/mm/tlb.c +0 -2
@@ -1265,8 +1265,6 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)local_irq_enable (); } -cpumask_clear (&batch->cpumask); -put_flush_tlb_info ();put_cpu (); }
mm/rmap.c +1 -0
@@ -673,6 +673,7 @@ void try_to_unmap_flush(void)return ;arch_tlbbatch_flush (&tlb_ubc->arch); +arch_tlbbatch_clear (&tlb_ubc->arch); tlb_ubc->flush_required = false; tlb_ubc->writable = false; }
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 5/8 mm: separate move/undo parts from migrate_pages_batch()
In reply to Byungchul ParkFunctionally, no change. This is a preparation for migrc mechanism that requires to use separated folio lists for its own handling during migration. Refactored migrate_pages_batch() and separated move/undo parts from migrate_pages_batch().
--- mm/migrate.c | 134 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 83 insertions(+), 51 deletions(-)
mm/migrate.c +83 -51
@@ -1609,6 +1609,81 @@ static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,return nr_failed; } +static void migrate_folios_move (struct list_head *src_folios, +struct list_head *dst_folios, +free_folio_t put_new_folio,unsigned long private, +enum migrate_mode mode,int reason, +struct list_head *ret_folios, +struct migrate_pages_stats *stats, +int *retry,int *thp_retry,int *nr_failed, +int *nr_retry_pages) +{ +struct folio *folio, *folio2, *dst, *dst2; +bool is_thp; +int nr_pages; +int rc; + + dst = list_first_entry(dst_folios, struct folio, lru); + dst2 =list_next_entry (dst, lru); +list_for_each_entry_safe (folio, folio2, src_folios, lru) { + is_thp =folio_test_large (folio) &&folio_test_pmd_mappable (folio); + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =migrate_folio_move (put_new_folio, private, + folio, dst, mode, + reason, ret_folios); +/* +* The rules are: +* Success: folio will be freed +* -EAGAIN: stay on the unmap_folios list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -EAGAIN : + *retry +=1 ; + *thp_retry += is_thp; + *nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp; +break ; +default : + *nr_failed +=1 ; + stats->nr_thp_failed += is_thp; + stats->nr_failed_pages += nr_pages; +break ; + } + dst = dst2; + dst2 =list_next_entry (dst, lru); + } +} + +static void migrate_folios_undo (struct list_head *src_folios, +struct list_head *dst_folios, +free_folio_t put_new_folio,unsigned long private, +struct list_head *ret_folios) +{ +struct folio *folio, *folio2, *dst, *dst2; + + dst = list_first_entry(dst_folios, struct folio, lru); + dst2 =list_next_entry (dst, lru); +list_for_each_entry_safe (folio, folio2, src_folios, lru) { +int old_page_state =0 ; +struct anon_vma *anon_vma =NULL ; + +__migrate_folio_extract (dst, &old_page_state, &anon_vma); +migrate_folio_undo_src (folio, old_page_state &PAGE_WAS_MAPPED , + anon_vma, true, ret_folios); +list_del (&dst->lru); +migrate_folio_undo_dst (dst, true, put_new_folio, private); + dst = dst2; + dst2 =list_next_entry (dst, lru); + } +} +/* * migrate_pages_batch() first unmaps folios in the from list as many as * possible, then move the unmapped folios.
@@ -1631,7 +1706,7 @@ static int migrate_pages_batch(struct list_head *from,int pass =0 ;bool is_thp = false;bool is_large = false; -struct folio *folio, *folio2, *dst =NULL , *dst2; +struct folio *folio, *folio2, *dst =NULL ;int rc, rc_saved =0 , nr_pages;LIST_HEAD (unmap_folios);LIST_HEAD (dst_folios);
@@ -1767,42 +1842,11 @@ static int migrate_pages_batch(struct list_head *from, thp_retry =0 ; nr_retry_pages =0 ; - dst = list_first_entry(&dst_folios, struct folio, lru); - dst2 =list_next_entry (dst, lru); -list_for_each_entry_safe (folio, folio2, &unmap_folios, lru) { - is_thp =folio_test_large (folio) &&folio_test_pmd_mappable (folio); - nr_pages =folio_nr_pages (folio); - -cond_resched (); - - rc =migrate_folio_move (put_new_folio, private, - folio, dst, mode, - reason, ret_folios); -/* -* The rules are: -* Success: folio will be freed -* -EAGAIN: stay on the unmap_folios list -* Other errno: put on ret_folios list -*/ -switch (rc) { -case -EAGAIN : - retry++; - thp_retry += is_thp; - nr_retry_pages += nr_pages; -break ; -case MIGRATEPAGE_SUCCESS : - stats->nr_succeeded += nr_pages; - stats->nr_thp_succeeded += is_thp; -break ; -default : - nr_failed++; - stats->nr_thp_failed += is_thp; - stats->nr_failed_pages += nr_pages; -break ; - } - dst = dst2; - dst2 =list_next_entry (dst, lru); - } +/* Move the unmapped folios */ +migrate_folios_move (&unmap_folios, &dst_folios, + put_new_folio, private, mode, reason, + ret_folios, stats, &retry, &thp_retry, + &nr_failed, &nr_retry_pages); } nr_failed += retry; stats->nr_thp_failed += thp_retry;
@@ -1811,20 +1855,8 @@ static int migrate_pages_batch(struct list_head *from, rc = rc_saved ? : nr_failed; out:/* Cleanup remaining folios */ - dst = list_first_entry(&dst_folios, struct folio, lru); - dst2 =list_next_entry (dst, lru); -list_for_each_entry_safe (folio, folio2, &unmap_folios, lru) { -int old_page_state =0 ; -struct anon_vma *anon_vma =NULL ; - -__migrate_folio_extract (dst, &old_page_state, &anon_vma); -migrate_folio_undo_src (folio, old_page_state &PAGE_WAS_MAPPED , - anon_vma, true, ret_folios); -list_del (&dst->lru); -migrate_folio_undo_dst (dst, true, put_new_folio, private); - dst = dst2; - dst2 =list_next_entry (dst, lru); - } +migrate_folios_undo (&unmap_folios, &dst_folios, + put_new_folio, private, ret_folios);return rc; }
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 6/8 mm: buddy: make room for a new variable, mgen, in struct page
In reply to Byungchul ParkFunctionally, no change. This is a preparation for migrc mechanism that tracks need of tlb flush for each page residing in buddy, using a generation number in struct page.
Fortunately, since the private field in struct page is used only to store page order in buddy, ranging from 0 to MAX_PAGE_ORDER, that can be covered with unsigned short int. So splitted it into two smaller ones, order and mgen, so that the both can be used in buddy at the same time.
--- include/linux/mm_types.h | 39 ++++++++++++++++++++++++++++++++------- mm/internal.h | 4 ++-- mm/page_alloc.c | 13 ++++++++----- 3 files changed, 42 insertions(+), 14 deletions(-)
include/linux/mm_types.h +32 -7
@@ -108,13 +108,24 @@ struct page {pgoff_t index;/* Our offset within mapping. */ unsigned long share;/* share count for fsdax */ }; -/** -* @private: Mapping-private opaque data. -* Usually used for buffer_heads if PagePrivate. -* Used for swp_entry_t if PageSwapCache. -* Indicates order in the buddy system if PageBuddy. -*/ -unsigned long private; +union { +/** +* @private: Mapping-private opaque data. +* Usually used for buffer_heads if PagePrivate. +* Used for swp_entry_t if PageSwapCache. +*/ +unsigned long private; +struct { +/* +* Indicates order in the buddy system if PageBuddy. +*/ +unsigned short int order; +/* +* Tracks need of tlb flush used by migrc +*/ +unsigned short int mgen; + }; + }; };struct {/* page_pool used by netstack */ /**
@@ -520,6 +531,20 @@ static inline void set_page_private(struct page *page, unsigned long private) page->private = private; } +#define page_buddy_order (page) ((page)->order) + +static inline void set_page_buddy_order (struct page *page,unsigned int order) +{ + page->order = (unsigned short int )order; +} + +#define page_buddy_mgen (page) ((page)->mgen) + +static inline void set_page_buddy_mgen (struct page *page,unsigned short int mgen) +{ + page->mgen = mgen; +} +static inline void *folio_get_private (struct folio *folio) {return folio->private;
mm/internal.h +2 -2
@@ -364,7 +364,7 @@ struct alloc_context {static inline unsigned int buddy_order (struct page *page) {/* PageBuddy() must be checked by the caller */ -return page_private (page); +return page_buddy_order (page); }/*
@@ -378,7 +378,7 @@ static inline unsigned int buddy_order(struct page *page)* times, potentially observing different values in the tests and the actual * use of the result. */ -#define buddy_order_unsafe (page) READ_ONCE(page_private(page)) +#define buddy_order_unsafe (page) READ_ONCE(page_buddy_order(page))/* * This function checks whether a page is free && is the buddy
mm/page_alloc.c +8 -5
@@ -604,9 +604,12 @@ void destroy_large_folio(struct folio *folio)free_the_page (&folio->page,folio_order (folio)); } -static inline void set_buddy_order (struct page *page,unsigned int order) +static inline void set_buddy_order_mgen (struct page *page, +unsigned int order, +unsigned short int mgen) { -set_page_private (page, order); +set_page_buddy_order (page, order); +set_page_buddy_mgen (page, order);__SetPageBuddy (page); }
@@ -824,7 +827,7 @@ static inline void __free_one_page(struct page *page, } done_merging: -set_buddy_order (page, order); +set_buddy_order_mgen (page, order,0 );if (fpi_flags &FPI_TO_TAIL ) to_tail = true;
@@ -1404,7 +1407,7 @@ static inline void expand(struct zone *zone, struct page *page,continue ;add_to_free_list (&page[size], zone, high, migratetype); -set_buddy_order (&page[size], high); +set_buddy_order_mgen (&page[size], high,0 ); } }
@@ -6710,7 +6713,7 @@ static void break_down_buddy_pages(struct zone *zone, struct page *page,continue ;add_to_free_list (current_buddy, zone, high, migratetype); -set_buddy_order (current_buddy, high); +set_buddy_order_mgen (current_buddy, high,0 ); } }
Signature · 1 line
2.17.1
Byungchul Park
reviewerPATCH v9 7/8 mm: add folio_put_mgen() to deliver migrc's generation number to pcp or buddy
In reply to Byungchul ParkIntroduced a new API, folio_put_mgen(), to deliver migrc's generation number to pcp or buddy that will be used by migrc mechanism to track need of tlb flush for each page residing in pcp or buddy.
migrc makes decision whether tlb flush is needed or not, based on a generation number stored in the interesting page and the global generation number, for that tlb flush required has been completed.
For now, the delivery works only for the following call path but not for e.g. free_the_page(), __free_pages_ok() and free_unref_page_list() that are not for releasing source folios during migration:
folio_put_mgen()
__folio_put_mgen() __folio_put_small() free_unref_page() free_unref_page_commit() free_one_page() __free_one_page()
The generation number should be handed over properly when pages travel between pcp and buddy, and must do necessary things on exit from pcp or buddy.
It's worth noting that this patch doesn't include actual body for tlb flush on the exit, which will be filled by the main patch of migrc mechanism.
--- include/linux/mm.h | 22 ++++++++ include/linux/sched.h | 1 + mm/compaction.c | 10 ++++ mm/internal.h | 41 ++++++++++++++- mm/page_alloc.c | 114 +++++++++++++++++++++++++++++++++++------- mm/page_reporting.c | 10 ++++ mm/swap.c | 19 +++++-- 7 files changed, 195 insertions(+), 22 deletions(-)
include/linux/mm.h +22 -0
@@ -1307,6 +1307,7 @@ static inline struct folio *virt_to_folio(const void *x) }void __folio_put (struct folio *folio); +void __folio_put_mgen (struct folio *folio,unsigned short int mgen);void put_pages_list (struct list_head *pages);
@@ -1506,6 +1507,27 @@ static inline void folio_put(struct folio *folio)__folio_put (folio); } +/** +* folio_put_mgen - Decrement the last reference count on a folio. +* @folio: The folio. +* @mgen: The migrc generation # of TLB flush that the folio requires. +* +* The folio's reference count should be one since the only user, folio +* migration code, calls folio_put_mgen() only when the folio has no +* reference else. The memory will be released back to the page +* allocator and may be used by another allocation immediately. Do not +* access the memory or the struct folio after calling folio_put_mgen(). +* +* Context: May be called in process or interrupt context, but not in NMI +* context. May be called while holding a spinlock. +*/ +static inline void folio_put_mgen (struct folio *folio,unsigned short int mgen) +{ +if (WARN_ON (!folio_put_testzero (folio))) +return ; +__folio_put_mgen (folio, mgen); +} +/** * folio_put_refs - Reduce the reference count on a folio. * @folio: The folio.
include/linux/sched.h +1 -0
@@ -1335,6 +1335,7 @@ struct task_struct {struct tlbflush_unmap_batch tlb_ubc;struct tlbflush_unmap_batch tlb_ubc_ro; +unsigned short int mgen;/* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe;
mm/compaction.c +10 -0
@@ -701,6 +701,11 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,if (locked)spin_unlock_irqrestore (&cc->zone->lock, flags); +/* +* Check and flush before using the isolated pages. +*/ +check_flush_task_mgen (); +/* * Be careful to not go outside of the pageblock. */
@@ -1673,6 +1678,11 @@ static void fast_isolate_freepages(struct compact_control *cc)spin_unlock_irqrestore (&cc->zone->lock, flags); +/* +* Check and flush before using the isolated pages. +*/ +check_flush_task_mgen (); +/* Skip fast search if enough freepages isolated */ if (cc->nr_freepages >= cc->nr_migratepages)break ;
mm/internal.h +40 -1
51 lines not on this page · raw message
mm/page_alloc.c +96 -18
319 lines not on this page · raw message
mm/page_reporting.c +10 -0
@@ -221,6 +221,11 @@ page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,/* release lock before waiting on report processing */ spin_unlock_irq (&zone->lock); +/* +* Check and flush before using the isolated pages. +*/ +check_flush_task_mgen (); +/* begin processing pages in local list */ err = prdev->report (prdev, sgl,PAGE_REPORTING_CAPACITY );
@@ -253,6 +258,11 @@ page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone,spin_unlock_irq (&zone->lock); +/* +* Check and flush before using the isolated pages. +*/ +check_flush_task_mgen (); +return err; }
mm/swap.c +16 -3
@@ -112,11 +112,24 @@ static void page_cache_release(struct folio *folio)unlock_page_lruvec_irqrestore (lruvec, flags); } -static void __folio_put_small (struct folio *folio) +static void __folio_put_small (struct folio *folio,unsigned short int mgen) {page_cache_release (folio);mem_cgroup_uncharge (folio); -free_unref_page (&folio->page,0 ); +free_unref_page (&folio->page,0 , mgen); +} + +void __folio_put_mgen (struct folio *folio,unsigned short int mgen) +{ +if (unlikely (folio_is_zone_device (folio))) +WARN_ON (1 ); +else if (unlikely (folio_test_large (folio))) +WARN_ON (1 ); +/* +* For now, migrc supports this case only. +*/ +else +__folio_put_small (folio, mgen); }static void __folio_put_large (struct folio *folio)
@@ -139,7 +152,7 @@ void __folio_put(struct folio *folio)else if (unlikely (folio_test_large (folio)))__folio_put_large (folio);else -__folio_put_small (folio); +__folio_put_small (folio,0 ); }EXPORT_SYMBOL (__folio_put);
Signature · 1 line
2.17.1
Re: [PATCH v9 7/8] mm: add folio_put_mgen() to deliver migrc's generation number to pcp or buddy
Quoted 12 lines · Byungchul Park
Introduced a new API,
folio_put_mgen(), to deliver migrc's generation number to pcp or buddy that will be used by migrc mechanism to track need of tlb flush for each page residing in pcp or buddy.migrc makes decision whether tlb flush is needed or not, based on a generation number stored in the interesting page and the global generation number, for that tlb flush required has been completed.
For now, the delivery works only for the following call path but not for e.g.
free_the_page(),__free_pages_ok()andfree_unref_page_list()that are not for releasing source folios during migration:
Quoted 7 lines
folio_put_mgen()
__folio_put_mgen()
__folio_put_small()
free_unref_page()
free_unref_page_commit()
free_one_page()
__free_one_page()You're going to want to rebase this on mm-unstable. __folio_put_small() went away in "mm: combine __folio_put_small, __folio_put_large and __folio_put"
Byungchul Park
reviewerRe: [PATCH v9 7/8] mm: add folio_put_mgen() to deliver migrc's generation number to pcp or buddy
Quoted 21 lines · Matthew Wilcox
Quoted 12 lines · Byungchul Park
Introduced a new API,
folio_put_mgen(), to deliver migrc's generation number to pcp or buddy that will be used by migrc mechanism to track need of tlb flush for each page residing in pcp or buddy.migrc makes decision whether tlb flush is needed or not, based on a generation number stored in the interesting page and the global generation number, for that tlb flush required has been completed.
For now, the delivery works only for the following call path but not for e.g.
free_the_page(),__free_pages_ok()andfree_unref_page_list()that are not for releasing source folios during migration:Quoted 7 lines
folio_put_mgen() __folio_put_mgen() __folio_put_small() free_unref_page() free_unref_page_commit() free_one_page() __free_one_page()
Quoted 3 lines
You're going to want to rebase this on mm-unstable.
__folio_put_small()went away in "mm: combine__folio_put_small,__folio_put_largeand__folio_put"
I checked it. Indeed. Thanks for the information.
Byungchul
kernel test robot
build botRe: [PATCH v9 7/8] mm: add folio_put_mgen() to deliver migrc's generation number to pcp or buddy
In reply to Byungchul Parkkernel test robot noticed the following build errors:
Hi Byungchul,
kernel test robot noticed the following build errors:
[auto build test ERROR on 0bbac3facb5d6cc0171c45c9873a2dc96bea9680]
url: https://github.com/intel-lab-lkp/linux/commits/Byungchul-Park/x86-tlb-add-APIs-manipulating-tlb-batch-s-arch-data/20240417-152211 base: 0bbac3facb5d6cc0171c45c9873a2dc96bea9680 patch link: https://lore.kernel.org/r/20240417071847.29584-8-byungchul%40sk.com patch subject: [PATCH v9 7/8] mm: add folio_put_mgen() to deliver migrc's generation number to pcp or buddy config: riscv-defconfig (https://download.01.org/0day-ci/archive/20240418/[email protected]/config) compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project 7089c359a3845323f6f30c44a47dd901f2edfe63) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240418/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from mm/page_alloc.c:19:
In file included from include/linux/mm.h:2230:
include/linux/vmstat.h:522:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
522 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
In file included from mm/page_alloc.c:44:
include/linux/mm_inline.h:47:41: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
47 | __mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
| ~~~~~~~~~~~ ^ ~~~
include/linux/mm_inline.h:49:22: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
49 | NR_ZONE_LRU_BASE + lru, nr_pages);
| ~~~~~~~~~~~~~~~~ ^ ~~~
mm/page_alloc.c:2812:2: warning: arithmetic between different enumeration types ('enum vm_event_item' and 'enum zone_type') [-Wenum-enum-conversion]
2812 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:135:34: note: expanded from macro '__count_zid_vm_events'
135 | __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
| ~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
mm/page_alloc.c:2934:3: warning: arithmetic between different enumeration types ('enum vm_event_item' and 'enum zone_type') [-Wenum-enum-conversion]
2934 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:135:34: note: expanded from macro '__count_zid_vm_events'
135 | __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
| ~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
mm/page_alloc.c:4575:2: warning: arithmetic between different enumeration types ('enum vm_event_item' and 'enum zone_type') [-Wenum-enum-conversion]
4575 | __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:135:34: note: expanded from macro '__count_zid_vm_events'
135 | __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
| ~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
In file included from mm/page_alloc.c:58:
>> mm/internal.h:1404:9: error: call to '__compiletime_assert_368' declared with 'error' attribute: BUILD_BUG failed
1404 | return xchg(¤t->mgen, 0);
| ^
include/linux/atomic/atomic-instrumented.h:4758:2: note: expanded from macro 'xchg'
4758 | raw_xchg(__ai_ptr, __VA_ARGS__); \
| ^
include/linux/atomic/atomic-arch-fallback.h:12:18: note: expanded from macro 'raw_xchg'
12 | #define raw_xchg arch_xchg
| ^
arch/riscv/include/asm/cmpxchg.h:145:23: note: expanded from macro 'arch_xchg'
145 | (__typeof__(*(ptr))) __arch_xchg((ptr), _x_, sizeof(*(ptr))); \
| ^
note: (skipping 4 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:437:2: note: expanded from macro '_compiletime_assert'
437 | __compiletime_assert(condition, msg, prefix, suffix)
| ^
include/linux/compiler_types.h:430:4: note: expanded from macro '__compiletime_assert'
430 | prefix ## suffix(); \
| ^
<scratch space>:124:1: note: expanded from here
124 | __compiletime_assert_368
| ^
In file included from mm/page_alloc.c:58:
>> mm/internal.h:1404:9: error: call to '__compiletime_assert_368' declared with 'error' attribute: BUILD_BUG failed
include/linux/atomic/atomic-instrumented.h:4758:2: note: expanded from macro 'xchg'
4758 | raw_xchg(__ai_ptr, __VA_ARGS__); \
| ^
include/linux/atomic/atomic-arch-fallback.h:12:18: note: expanded from macro 'raw_xchg'
12 | #define raw_xchg arch_xchg
| ^
arch/riscv/include/asm/cmpxchg.h:145:23: note: expanded from macro 'arch_xchg'
145 | (__typeof__(*(ptr))) __arch_xchg((ptr), _x_, sizeof(*(ptr))); \
| ^
note: (skipping 4 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
include/linux/compiler_types.h:437:2: note: expanded from macro '_compiletime_assert'
437 | __compiletime_assert(condition, msg, prefix, suffix)
| ^
include/linux/compiler_types.h:430:4: note: expanded from macro '__compiletime_assert'
430 | prefix ## suffix(); \
| ^
<scratch space>:124:1: note: expanded from here
124 | __compiletime_assert_368
| ^6 warnings and 2 errors generated.
vim +1404 mm/internal.h
1401
1402 static inline unsigned int hand_over_task_mgen(void)
1403 {
> 1404 return xchg(¤t->mgen, 0);
1405 }
1406
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Byungchul Park
reviewerPATCH v9 8/8 mm: defer tlb flush until the source folios at migration actually get used
In reply to Byungchul ParkThis is implementation of MIGRC mechanism that stands for 'Migration Read Copy'. We always face the migration overhead at either promotion or demotion, while working with tiered memory e.g. CXL memory and found out tlb shootdown is one that is needed to get rid of if possible.
Fortunately, tlb flush can be defered as long as it guarantees to be performed before the source folios at migration actually become used, of course, only if the target PTE entries have read-only permission, precisely, don't have write permission. Otherwise, no doubt the sytem might get messed up.
To achieve that:
1. For the folios that map only to non-writable tlb entries, prevent tlb flush during migration but perform it just before the source folios actually become used out of buddy or pcp.
2. When any non-writable tlb entry changes to writable e.g. through fault handler, give up migrc mechanism and perform tlb flush required right away.
No matter what type of workload is used for performance evaluation, the result would be positive thanks to the unconditional reduction of tlb flushes, tlb misses and interrupts. For the test, I picked up XSBench that is widely used for performance analysis on high performance computing architectures - https://github.com/ANL-CESAR/XSBench.
The result would depend on memory latency and how often reclaim runs, which implies tlb miss overhead and how many times migration happens. The slower the memory is and the more reclaim runs, the better migrc works so as to obtain the better result. In my system, the result shows:
1. itlb flushes are reduced over 90%.
2. itlb misses are reduced over 30%.
3. All the other tlb numbers also get enhanced.
4. tlb shootdown interrupts are reduced over 90%.
5. The test program runtime is reduced over 5%.
The test envitonment:
Architecture - x86_64
QEMU - kvm enabled, host cpu
Numa - 2 nodes (16 CPUs 1GB, no CPUs 99GB)
Linux Kernel - v6.9-rc4, numa balancing tiering on, demotion enabled
< measurement: raw data - tlb and interrupt numbers >
$ perf stat -a \
-e itlb.itlb_flush \
-e tlb_flush.dtlb_thread \
-e tlb_flush.stlb_any \
-e dtlb-load-misses \
-e dtlb-store-misses \
-e itlb-load-misses \
XSBench -t 16 -p 50000000$ grep "TLB shootdowns" /proc/interrupts
BEFORE
------
40417078 itlb.itlb_flush
234852566 tlb_flush.dtlb_thread
153192357 tlb_flush.stlb_any
119001107892 dTLB-load-misses
307921167 dTLB-store-misses
1355272118 iTLB-load-misses
TLB: 1364803 1303670 1333921 1349607
1356934 1354216 1332972 1342842
1350265 1316443 1355928 1360793
1298239 1326358 1343006 1340971TLB shootdowns
AFTER
-----
3316495 itlb.itlb_flush
138912511 tlb_flush.dtlb_thread
115199341 tlb_flush.stlb_any
117610390021 dTLB-load-misses
198042233 dTLB-store-misses
840066984 iTLB-load-misses
TLB: 117257 119219 117178 115737
117967 118948 117508 116079
116962 117266 117320 117215
105808 103934 115672 117610TLB shootdowns
< measurement: user experience - runtime >
$ time XSBench -t 16 -p 50000000
BEFORE
------
Threads: 16 Runtime: 968.783 seconds Lookups: 1,700,000,000 Lookups/s: 1,754,778
15208.91s user 141.44s system 1564% cpu 16:20.98 total
AFTER
-----
Threads: 16 Runtime: 913.210 seconds Lookups: 1,700,000,000 Lookups/s: 1,861,565
14351.69s user 138.23s system 1565% cpu 15:25.47 total
--- include/linux/sched.h | 8 + mm/internal.h | 46 +++++- mm/memory.c | 8 + mm/migrate.c | 359 ++++++++++++++++++++++++++++++++++++++++-- mm/rmap.c | 12 +- 5 files changed, 414 insertions(+), 19 deletions(-)
include/linux/sched.h +8 -0
@@ -1337,6 +1337,14 @@ struct task_struct {struct tlbflush_unmap_batch tlb_ubc_ro;unsigned short int mgen; +#if defined(CONFIG_MIGRATION ) && defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH ) +/* +* whether all the mappings of a folio during unmap are read-only +* so that migrc can work on the folio +*/ +bool can_migrc; +#endif +/* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe;
mm/internal.h +41 -5
60 lines not on this page · raw message
mm/memory.c +8 -0
@@ -3611,6 +3611,14 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)if (vmf->page) folio =page_folio (vmf->page); +/* +* The folio may or may not be one that is under migrc's control +* and about to change its permission from read-only to writable. +* Conservatively give up deferring tlb flush just in case. +*/ +if (folio) +migrc_flush (); +/* * Shared mapping: we are guaranteed to have VM_WRITE and * FAULT_FLAG_WRITE set at this point.
mm/migrate.c +347 -12
480 lines not on this page · raw message
mm/rmap.c +10 -2
@@ -672,7 +672,9 @@ void try_to_unmap_flush(void)if (!tlb_ubc->flush_required)return ; +rmap_flush_start ();arch_tlbbatch_flush (&tlb_ubc->arch); +rmap_flush_end (tlb_ubc);arch_tlbbatch_clear (&tlb_ubc->arch); tlb_ubc->flush_required = false; tlb_ubc->writable = false;
@@ -707,9 +709,15 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval,if (!pte_accessible (mm, pteval))return ; -if (pte_write (pteval) || writable) +if (pte_write (pteval) || writable) { tlb_ubc = ¤t->tlb_ubc; -else + +/* +* migrc cannot work with the folio once it found a +* writable or dirty mapping on it. +*/ +can_migrc_fail (); + }else tlb_ubc = ¤t->tlb_ubc_ro;arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr);
Signature · 1 line
2.17.1
Byungchul Park
reviewerQuoted 11 lines · Byungchul Park
Hi everyone,
While I'm working with a tiered memory system e.g. CXL memory, I have been facing migration overhead esp. tlb shootdown on promotion or demotion between different tiers. Yeah.. most tlb shootdowns on migration through hinting fault can be avoided thanks to Huang Ying's work, commit
4d4b6d66db("mm,unmap: avoid flushing tlb in batch if PTE is inaccessible"). See the following link for more information:
Quoted 8 lines
However, it's only for ones using hinting fault. I thought it'd be much better if we have a general mechanism to reduce all tlb numbers that we can ultimately apply to any type of migration.
I'm suggesting a mechanism called MIGRC that stands for 'Migration Read Copy', to reduce tlb numbers by deferring tlb flush until the source folios at migration actually become used, of course, only if the target PTE don't have write permission.
I stably obtain the performance improvement over 5% in end user's point of view, which I think is not negligible. Thoughts?
Byungchul
Quoted 258 lines
To achieve that:
1. For the folios that map only to non-writable tlb entries, prevent tlb flush during migration but perform it just before the source folios actually become used out of buddy or pcp.
2. When any non-writable tlb entry changes to writable e.g. through fault handler, give up migrc mechanism and perform tlb flush required right away.
No matter what type of workload is used for performance evaluation, the result would be positive thanks to the unconditional reduction of tlb flushes, tlb misses and interrupts. For the test, I picked up XSBench that is widely used for performance analysis on high performance computing architectures - https://github.com/ANL-CESAR/XSBench.
The result would depend on memory latency and how often reclaim runs, which implies tlb miss overhead and how many times migration happens. The slower the memory is and the more reclaim runs, the better migrc works so as to obtain the better result. In my system, the result shows:
1. itlb flushes are reduced over 90%.
2. itlb misses are reduced over 30%.
3. All the other tlb numbers also get enhanced.
4. tlb shootdown interrupts are reduced over 90%.
5. The test program runtime is reduced over 5%.The test envitonment:
Architecture -
x86_64
QEMU - kvm enabled, host cpu
Numa - 2 nodes (16 CPUs 1GB, no CPUs 99GB)
Linux Kernel - v6.9-rc4, numa balancing tiering on, demotion enabled< measurement: raw data - tlb and interrupt numbers >
$ perf stat -a \ -e itlb.itlb_flush \ -e tlb_flush.dtlb_thread \ -e tlb_flush.stlb_any \ -e dtlb-load-misses \ -e dtlb-store-misses \ -e itlb-load-misses \ XSBench -t 16 -p 50000000$ grep "TLB shootdowns" /proc/interruptsBEFORE ------ 40417078 itlb.itlb_flush 234852566 tlb_flush.dtlb_thread 153192357 tlb_flush.stlb_any 119001107892 dTLB-load-misses 307921167 dTLB-store-misses 1355272118 iTLB-load-misses TLB: 1364803 1303670 1333921 1349607 1356934 1354216 1332972 1342842 1350265 1316443 1355928 1360793 1298239 1326358 1343006 1340971 TLB shootdowns AFTER ----- 3316495 itlb.itlb_flush 138912511 tlb_flush.dtlb_thread 115199341 tlb_flush.stlb_any 117610390021 dTLB-load-misses 198042233 dTLB-store-misses 840066984 iTLB-load-misses TLB: 117257 119219 117178 115737 117967 118948 117508 116079 116962 117266 117320 117215 105808 103934 115672 117610 TLB shootdowns< measurement: user experience - runtime >
$ time XSBench -t 16 -p 50000000BEFORE ------ Threads: 16 Runtime: 968.783 seconds Lookups: 1,700,000,000 Lookups/s: 1,754,778 15208.91s user 141.44s system 1564% cpu 16:20.98 total AFTER ----- Threads: 16 Runtime: 913.210 seconds Lookups: 1,700,000,000 Lookups/s: 1,861,565 14351.69s user 138.23s system 1565% cpu 15:25.47 total---
Changes from v8:
1. Rebase on the latest, v6.9-rc4.
2. Supplement comments and commit message.
3. Change the candidate to apply migrc mechnism:BEFORE - The source folios at demotion and promotion. AFTER - The souce folios at any type of migration.
4. Change how migrc mechanism works:
BEFORE - Reduce tlb flushes by deferring
folio_free()for
source folios during demotion and promotion.
AFTER - Reduce tlb flushes by deferring tlb flush until they
actually become used, out of pcp or buddy. The current version of migrc does *not* defer callingfolio_free()but let it go as it is as the same as vanilla kernel, with the folios marked kind of 'need to tlb flush'. And then handle the flush when the page exits from pcp or buddy so as to prevent changing vm stats e.g. free pages.Changes from v7:
1. Rewrite cover letter to explain what 'migrc' mechasism is. (feedbacked by Andrew Morton)
2. Supplement the commit message of a patch 'mm: Add APIs to free a folio directly to the buddy bypassing pcp'. (feedbacked by Andrew Morton)Changes from v6:
1. Fix build errors in case ofCONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSHdisabled by moving migrc_flush_{start,end}() calls from arch code totry_to_unmap_flush()inmm/rmap.c.Changes from v5:
1. Fix build errors in case ofCONFIG_MIGRATIONdisabled orCONFIG_HWPOISON_INJECTmoduled. (feedbacked by kernel test bot and Raymond Jay Golo)
2. Organize migrc code with two kconfigs,CONFIG_MIGRATIONandCONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSH.Changes from v4:
1. Rebase on v6.7.
2. Fix build errors in arm64 that is doing nothing for tlb flush but hasCONFIG_ARCH_WANT_BATCHED_UNMAP_tlb_FLUSH. (reported by kernel test robot)
3. Don't use any page flag. So the system would give up migrc mechanism more often but it's okay. The final improvement is good enough.
4. Instead, optimize full tlb flush(arch_tlbbatch_flush()) by avoiding redundant CPUs from tlb flush.Changes from v3:
1. Don't use the kconfig,
CONFIG_MIGRC, and remove sysctl knob,migrc_enable. (feedbacked by Nadav)
2. Remove the optimization skipping CPUs that have already performed tlb flushes needed by any reason when performing tlb flushes by migrc because I can't tell the performance difference between w/ the optimization and w/o that. (feedbacked by Nadav)
3. Minimize arch-specific code. While at it, move all the migrc declarations and inline functions frominclude/linux/mm.htomm/internal.h(feedbacked by Dave Hansen, Nadav)
4. Separate a part making migrc paused when the system is in high memory pressure to another patch. (feedbacked by Nadav)
5. Rename:
a.arch_tlbbatch_clean()toarch_tlbbatch_clear(), b.tlb_ubc_nowrtotlb_ubc_ro,
c.migrc_try_flush_free_folios()tomigrc_flush_free_folios(), d.migrc_stoptomigrc_pause.
(feedbacked by Nadav)
6. Use ->lrulist_headinstead of introducing a newllist_head. (feedbacked by Nadav)
7. Use non-atomic operations of page-flag when it's safe. (feedbacked by Nadav)
8. Use stack instead of keeping a pointer of 'structmigrc_req' in struct task, which is for manipulating it locally. (feedbacked by Nadav)
9. Replace a lot of simple functions to inline functions placed in a header,mm/internal.h. (feedbacked by Nadav)
10. Add additional sufficient comments. (feedbacked by Nadav)
11. Remove a lot of wrapper functions. (feedbacked by Nadav)Changes from RFC v2:
1. Remove additional occupation in struct page. To do that, unioned with lru field for migrc's list and added a page flag. I know page flag is a thing that we don't like to add but no choice because migrc should distinguish folios under migrc's control from others. Instead, I force migrc to be used only on 64 bit system to mitigate you guys from getting angry.
2. Remove meaningless internal object allocator that I introduced to minimize impact onto the system. However, a ton of tests showed there was no difference.
3. Stop migrc from working when the system is in high memory pressure like about to perform direct reclaim. At the condition where the swap mechanism is heavily used, I found the system suffered from regression without this control.
4. Exclude folios thatpte_dirty()== true from migrc's interest so that migrc can work simpler.
5. Combine several patches that work tightly coupled to one.
6. Add sufficient comments for better review.
7. Manage migrc's request in per-node manner (from globally).
8. Add tlb miss improvement in commit message.
9. Test with moreCPUs(4 -> 16)to see bigger improvement.Changes from RFC:
1. Fix a bug triggered when a destination folio at the previous migration becomes a source folio at the next migration, before the folio gets handled properly so that the folio can play with another migration. There was inconsistency in the folio's state. Fixed it.
2. Split the patch set into more pieces so that the folks can review better. (Feedbacked by Nadav Amit)
3. Fix a wrong usage of barrier e.g.smp_mb__after_atomic(). (Feedbacked by Nadav Amit)
4. Tried to add sufficient comments to explain the patch set better. (Feedbacked by Nadav Amit)Byungchul Park (8):
x86/tlb: add APIs manipulating tlb batch's arch data
arm64: tlbflush: add APIs manipulating tlb batch's arch data mm/rmap: recognize read-only tlb entries during batched tlb flush x86/tlb, mm/rmap: separatearch_tlbbatch_clear()out of
arch_tlbbatch_flush()
mm: separate move/undo parts frommigrate_pages_batch()
mm: buddy: make room for a new variable, mgen, in struct page
mm: addfolio_put_mgen()to deliver migrc's generation number to pcp
or buddy
mm: defer tlb flush until the source folios at migration actually get
usedarch/arm64/include/asm/tlbflush.h | 18 ++ arch/x86/include/asm/tlbflush.h | 18 ++ arch/x86/mm/tlb.c | 2 - include/linux/mm.h | 22 ++ include/linux/mm_types.h | 39 ++- include/linux/sched.h | 10 + mm/compaction.c | 10 + mm/internal.h | 85 +++++- mm/memory.c | 8 + mm/migrate.c | 487 ++++++++++++++++++++++++++---- mm/page_alloc.c | 125 ++++++-- mm/page_reporting.c | 10 + mm/rmap.c | 40 ++- mm/swap.c | 19 +- 14 files changed, 795 insertions(+), 98 deletions(-)base-commit:
0bbac3facb5d6cc0171c45c9873a2dc96bea9680Signature · 1 line
2.17.1