TLB flush multiple pages with a single IPI v3
Subsystems (12)
Mel Gorman
reviewercover TLB flush multiple pages with a single IPI v3
Changelog since V2
o Ensure TLBs are flushed before pages are freed (mel)
Changelog since V1
o Structure and variable renaming (hughd)
o Defer flushes even if the unmapping process is sleeping (huged)
o Alternative sizing of structure (peterz)
o Use GFP_KERNEL instead of GFP_ATOMIC, PF_MEMALLOC protects (andi)
o Immediately flush dirty PTEs to avoid corruption (mel)
o Further clarify docs on the required arch guarantees (mel)
When unmapping pages it is necessary to flush the TLB. If that page was accessed by another CPU then an IPI is used to flush the remote CPU. That is a lot of IPIs if kswapd is scanning and unmapping >100K pages per second.
There already is a window between when a page is unmapped and when it is TLB flushed. This series simply increases the window so multiple pages can be flushed using a single IPI.
Patch 1 simply made the rest of the series easier to write as ftrace
could identify all the senders of TLB flush IPIS.
Patch 2 collects a list of PFNs and sends one IPI to flush them all
Patch 3 uses more memory so further defer when the IPI gets sent
Patch 4 uses the same infrastructure as patch 2 to batch IPIs sent during
page migration.
The performance impact is documented in the changelogs but in the optimistic case on a 4-socket machine the full series reduces interrupts from 900K interrupts/second to 60K interrupts/second.
arch/x86/Kconfig | 1 + arch/x86/include/asm/tlbflush.h | 2 + arch/x86/mm/tlb.c | 1 + include/linux/init_task.h | 8 +++ include/linux/mm_types.h | 1 + include/linux/rmap.h | 13 ++-- include/linux/sched.h | 15 ++++ include/trace/events/tlb.h | 3 +- init/Kconfig | 8 +++ kernel/fork.c | 7 ++ kernel/sched/core.c | 3 + mm/internal.h | 16 +++++ mm/migrate.c | 27 +++++-- mm/rmap.c | 151 ++++++++++++++++++++++++++++++++++++---- mm/vmscan.c | 35 +++++++++- 15 files changed, 267 insertions(+), 24 deletions(-)
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 1/6 x86, mm: Trace when an IPI is about to be sent
It is easy to trace when an IPI is received to flush a TLB but harder to detect what event sent it. This patch makes it easy to identify the source of IPIs being transmitted for TLB flushes on x86.
--- arch/x86/mm/tlb.c | 1 + include/linux/mm_types.h | 1 + include/trace/events/tlb.h | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +1 -0
@@ -140,6 +140,7 @@ void native_flush_tlb_others(const struct cpumask *cpumask, info.flush_end = end;count_vm_tlb_event (NR_TLB_REMOTE_FLUSH ); +trace_tlb_flush (TLB_REMOTE_SEND_IPI , end - start);if (is_uv_system ()) {unsigned int cpu;
include/linux/mm_types.h +1 -0
@@ -532,6 +532,7 @@ enum tlb_flush_reason {TLB_REMOTE_SHOOTDOWN ,TLB_LOCAL_SHOOTDOWN ,TLB_LOCAL_MM_SHOOTDOWN , +TLB_REMOTE_SEND_IPI , NR_TLB_FLUSH_REASONS, };
include/trace/events/tlb.h +2 -1
@@ -11,7 +11,8 @@ { TLB_FLUSH_ON_TASK_SWITCH, "flush on task switch" }, \ { TLB_REMOTE_SHOOTDOWN, "remote shootdown" }, \ { TLB_LOCAL_SHOOTDOWN, "local shootdown" }, \ - { TLB_LOCAL_MM_SHOOTDOWN, "local mm shootdown" } + { TLB_LOCAL_MM_SHOOTDOWN, "local mm shootdown" }, \ + { TLB_REMOTE_SEND_IPI, "remote ipi send" } TRACE_EVENT_CONDITION(tlb_flush,
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 2/6 mm: Send a single IPI to TLB flush multiple pages when unmapping
In reply to Mel GormanAn IPI is sent to flush remote TLBs when a page is unmapped that was recently accessed by other CPUs. There are many circumstances where this happens but the obvious one is kswapd reclaiming pages belonging to a running process as kswapd and the task are likely running on separate CPUs.
On small machines, this is not a significant problem but as machine gets larger with more cores and more memory, the cost of these IPIs can be high. This patch uses a structure similar in principle to a pagevec to collect a list of PFNs and CPUs that require flushing. It then sends one IPI to flush the list of PFNs. A new TLB flush helper is required for this and one is added for x86. Other architectures will need to decide if batching like this is both safe and worth the memory overhead. Specifically the requirement is;
If a clean page is unmapped and not immediately flushed, the architecture must guarantee that a write to that page from a CPU with a cached TLB entry will trap a page fault.
This is essentially what the kernel already depends on but the window is much larger with this patch applied and is worth highlighting.
The impact of this patch depends on the workload as measuring any benefit requires both mapped pages co-located on the LRU and memory pressure. The case with the biggest impact is multiple processes reading mapped pages taken from the vm-scalability test suite. The test case uses NR_CPU readers of mapped files that consume 10*RAM.
vmscale on a 4-node machine with 64G RAM and 48 CPUs
4.0.0 4.0.0
vanilla batchunmap-v3lru-file-mmap-read-elapsed 159.76 ( 0.00%) 118.92 ( 25.56%)
4.0.0 4.0.0
vanilla batchunmap-v3User 567.53 609.20
System 5949.65 4117.68
Elapsed 161.08 120.21
This is showing that the readers completed 25% with 30% less CPU time. From vmstats, it is known that the vanilla kernel was interrupted roughly 900K times per second during the steady phase of the test and the patched kernel was interrupts 180K times per second.
The impact is much lower on a small machine
vmscale on a 1-node machine with 8G RAM and 1 CPU
4.0.0 4.0.0
vanilla batchunmap-v2Ops lru-file-mmap-read-elapsed 22.50 ( 0.00%) 19.82 ( 11.91%)
4.0.0 4.0.0
vanilla batchunmap-v3
User 33.64 32.14
System 36.22 33.68
Elapsed 24.11 21.47It's still a noticeable improvement with vmstat showing interrupts went from roughly 500K per second to 45K per second.
The patch will have no impact on workloads with no memory pressure or have relatively few mapped pages.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/tlbflush.h | 2 + include/linux/init_task.h | 8 ++++ include/linux/rmap.h | 3 ++ include/linux/sched.h | 14 ++++++ init/Kconfig | 8 ++++ kernel/fork.c | 5 ++ kernel/sched/core.c | 3 ++ mm/internal.h | 11 +++++ mm/rmap.c | 104 +++++++++++++++++++++++++++++++++++++++- mm/vmscan.c | 31 +++++++++++- 11 files changed, 187 insertions(+), 3 deletions(-)
arch/x86/Kconfig +1 -0
@@ -30,6 +30,7 @@ config X86 select ARCH_MIGHT_HAVE_PC_SERIO select HAVE_AOUT if X86_32 select HAVE_UNSTABLE_SCHED_CLOCK + select ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH select ARCH_SUPPORTS_NUMA_BALANCING if X86_64 select ARCH_SUPPORTS_INT128 if X86_64 select HAVE_IDE
arch/x86/include/asm/tlbflush.h +2 -0
@@ -152,6 +152,8 @@ static inline void __flush_tlb_one(unsigned long addr)* and page-granular flushes are available only on i486 and up. */ +#define flush_local_tlb_addr (addr) __flush_tlb_one(addr) +#ifndef CONFIG_SMP /* "_up" is for UniProcessor.
include/linux/init_task.h +8 -0
@@ -175,6 +175,13 @@ extern struct task_group root_task_group;# define INIT_NUMA_BALANCING (tsk)#endif +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH +# define INIT_TLBFLUSH_UNMAP_BATCH_CONTROL (tsk) \ + .tlb_ubc = NULL, +#else +# define INIT_TLBFLUSH_UNMAP_BATCH_CONTROL (tsk) +#endif +#ifdef CONFIG_KASAN # define INIT_KASAN (tsk) \ .kasan_depth = 1,
@@ -257,6 +264,7 @@ extern struct task_group root_task_group; INIT_RT_MUTEXES(tsk) \ INIT_VTIME(tsk) \ INIT_NUMA_BALANCING(tsk) \ + INIT_TLBFLUSH_UNMAP_BATCH_CONTROL(tsk) \ INIT_KASAN(tsk) \ }
include/linux/rmap.h +3 -0
@@ -89,6 +89,9 @@ enum ttu_flags {TTU_IGNORE_MLOCK = (1 <<8 ),/* ignore mlock */ TTU_IGNORE_ACCESS = (1 <<9 ),/* don't age */ TTU_IGNORE_HWPOISON = (1 <<10 ),/* corrupted page is recoverable */ +TTU_BATCH_FLUSH = (1 <<11 ),/* Batch TLB flushes where possible +* and caller guarantees they will +* do a final flush if necessary */ }; #ifdef CONFIG_MMU
include/linux/sched.h +14 -0
@@ -1275,6 +1275,16 @@ enum perf_event_task_context { perf_nr_task_contexts, }; +/* Matches SWAP_CLUSTER_MAX but refined to limit header dependencies */ +#define BATCH_TLBFLUSH_SIZE 32UL + +/* Track pages that require TLB flushes */ +struct tlbflush_unmap_batch { +struct cpumask cpumask; +unsigned long nr_pages; +unsigned long pfns[BATCH_TLBFLUSH_SIZE ]; +}; +struct task_struct {volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */ void *stack;
@@ -1634,6 +1644,10 @@ struct task_struct {unsigned long numa_pages_migrated;#endif /* CONFIG_NUMA_BALANCING */ +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH +struct tlbflush_unmap_batch *tlb_ubc; +#endif +struct rcu_head rcu;/*
init/Kconfig +8 -0
@@ -889,6 +889,14 @@ config ARCH_SUPPORTS_NUMA_BALANCING bool # +# For architectures that have a local TLB flush for a PFN without knowledge +# of the VMA. The architecture must provide guarantees on what happens if +# a clean TLB cache entry is written after the unmap. Details are in mm/rmap.c +# near the check for should_defer_flush. +config ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH + bool + +# # For architectures that know their GCC __int128 support is sound # config ARCH_SUPPORTS_INT128
kernel/fork.c +5 -0
@@ -246,6 +246,11 @@ void __put_task_struct(struct task_struct *tsk)delayacct_tsk_free (tsk);put_signal_struct (tsk->signal); +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH +kfree (tsk->tlb_ubc); + tsk->tlb_ubc =NULL ; +#endif +if (!profile_handoff_task (tsk))free_task (tsk); }
kernel/sched/core.c +3 -0
@@ -1823,6 +1823,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) p->numa_group =NULL ;#endif /* CONFIG_NUMA_BALANCING */ +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH + p->tlb_ubc =NULL ; +#endif /* CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH */ }#ifdef CONFIG_NUMA_BALANCING
mm/internal.h +11 -0
@@ -431,4 +431,15 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,#define ALLOC_CMA 0x80/* allow allocations from CMA areas */ #define ALLOC_FAIR 0x100/* fair zone allocation */ +enum ttu_flags ; +struct tlbflush_unmap_batch ; + +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH +void try_to_unmap_flush (void ); +#else +static inline void try_to_unmap_flush (void ) +{ +} + +#endif /* CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH */ #endif /* __MM_INTERNAL_H */
mm/rmap.c +102 -2
131 lines not on this page · raw message
mm/vmscan.c +30 -1
@@ -1024,7 +1024,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,* processes. Try to unmap it here. */ if (page_mapped (page) && mapping) { -switch (try_to_unmap (page, ttu_flags)) { +switch (try_to_unmap (page, + ttu_flags|TTU_BATCH_FLUSH )) {case SWAP_FAIL : goto activate_locked;case SWAP_AGAIN :
@@ -1175,6 +1176,7 @@ keep: }mem_cgroup_uncharge_list (&free_pages); +try_to_unmap_flush ();free_hot_cold_page_list (&free_pages, true);list_splice (&ret_pages, page_list);
@@ -2762,6 +2764,30 @@ out:return false; } +#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH +/* +* Allocate the control structure for batch TLB flushing. An allocation +* failure is harmless as the reclaimer will send IPIs where necessary. +*/ +static inline void alloc_tlb_ubc (void ) +{ +if (current->tlb_ubc) +return ; + + current->tlb_ubc =kmalloc (sizeof (struct tlbflush_unmap_batch ), +GFP_ATOMIC | __GFP_NOWARN); +if (!current->tlb_ubc) +return ; + +cpumask_clear (¤t->tlb_ubc->cpumask); + current->tlb_ubc->nr_pages =0 ; +} +#else +static inline void alloc_tlb_ubc (void ) +{ +} +#endif /* CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH */ +unsigned long try_to_free_pages (struct zonelist *zonelist,int order,gfp_t gfp_mask,nodemask_t *nodemask) {
@@ -2789,6 +2815,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, sc.may_writepage, gfp_mask); +alloc_tlb_ubc (); nr_reclaimed =do_try_to_free_pages (zonelist, &sc);trace_mm_vmscan_direct_reclaim_end (nr_reclaimed);
@@ -3364,6 +3391,8 @@ static int kswapd(void *p)lockdep_set_current_reclaim_state (GFP_KERNEL ); +alloc_tlb_ubc (); +if (!cpumask_empty (cpumask))set_cpus_allowed_ptr (tsk, cpumask); current->reclaim_state = &reclaim_state;
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 3/6 mm: Defer TLB flush after unmap as long as possible
In reply to Mel GormanIf a PTE is unmapped and it's dirty then it was writable recently. Due to deferred TLB flushing, it's best to assume a writable TLB cache entry exists. With that assumption, the TLB must be flushed before any IO can start or the page is freed to avoid lost writes or data corruption. Prior to this patch, such PFNs were simply flushed immediately. In this patch, the caller is informed that such entries potentially exist and it's up to the caller to flush before pages are freed or IO can start.
--- include/linux/rmap.h | 10 ++++++---- mm/rmap.c | 55 ++++++++++++++++++++++++++++++++++++++-------------- mm/vmscan.c | 9 ++++++++- 3 files changed, 54 insertions(+), 20 deletions(-)
include/linux/rmap.h +6 -4
@@ -290,9 +290,11 @@ static inline int page_mkclean(struct page *page)/* * Return values of try_to_unmap */ -#define SWAP_SUCCESS 0 -#define SWAP_AGAIN 1 -#define SWAP_FAIL 2 -#define SWAP_MLOCK 3 +#define SWAP_SUCCESS 0 +#define SWAP_SUCCESS_CACHED 1 +#define SWAP_AGAIN 2 +#define SWAP_AGAIN_CACHED 3 +#define SWAP_FAIL 4 +#define SWAP_MLOCK 5#endif /* _LINUX_RMAP_H */
mm/rmap.c +40 -15
111 lines not on this page · raw message
mm/vmscan.c +8 -1
@@ -860,6 +860,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,unsigned long nr_reclaimed =0 ;unsigned long nr_writeback =0 ;unsigned long nr_immediate =0 ; +bool tlb_flush_required = false;cond_resched ();
@@ -1032,6 +1033,9 @@ static unsigned long shrink_page_list(struct list_head *page_list, goto keep_locked;case SWAP_MLOCK : goto cull_mlocked; +case SWAP_SUCCESS_CACHED : +/* Must flush before free, fall through */ + tlb_flush_required = true;case SWAP_SUCCESS : ;/* try to free the page below */ }
@@ -1176,7 +1180,8 @@ keep: }mem_cgroup_uncharge_list (&free_pages); -try_to_unmap_flush (); +if (tlb_flush_required) +try_to_unmap_flush ();free_hot_cold_page_list (&free_pages, true);list_splice (&ret_pages, page_list);
@@ -1213,6 +1218,7 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone, ret =shrink_page_list (&clean_pages, zone, &sc,TTU_UNMAP |TTU_IGNORE_ACCESS , &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); +try_to_unmap_flush ();list_splice (&clean_pages, page_list);mod_zone_page_state (zone,NR_ISOLATED_FILE , -ret);return ret;
@@ -2225,6 +2231,7 @@ static void shrink_lruvec(struct lruvec *lruvec, int swappiness, scan_adjusted = true; }blk_finish_plug (&plug); +try_to_unmap_flush (); sc->nr_reclaimed += nr_reclaimed;/*
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/6] mm: Defer TLB flush after unmap as long as possible
Quoted 27 lines · Mel Gorman
If a PTE is unmapped and it's dirty then it was writable recently. Due to deferred TLB flushing, it's best to assume a writable TLB cache entry exists. With that assumption, the TLB must be flushed before any IO can start or the page is freed to avoid lost writes or data corruption. Prior to this patch, such PFNs were simply flushed immediately. In this patch, the caller is informed that such entries potentially exist and it's up to the caller to flush before pages are freed or IO can start.
Signed-off-by Mel Gorman <[email protected]>mm/rmap.c +5
-4@@ -1450,10 +1455,11 @@ static int page_not_mapped(struct page *page)* page, used in the pageout path. Caller must hold the page lock. * Return values are: * -* SWAP_SUCCESS - we succeeded in removing all mappings -* SWAP_AGAIN - we missed a mapping, try again later -* SWAP_FAIL - the page is unswappable -* SWAP_MLOCK - page is mlocked. +* SWAP_SUCCESS - we succeeded in removing all mappings +* SWAP_SUCCESS_CACHED - Like SWAP_SUCCESS but a writable TLB entry may exist +* SWAP_AGAIN - we missed a mapping, try again later +* SWAP_FAIL - the page is unswappable +* SWAP_MLOCK - page is mlocked. */ int try_to_unmap (struct page *page,enum ttu_flags flags) {
mm/rmap.c int try_to_unmap(struct page *page, enum ttu_flags flags) · line 1493
Quoted 8 lines
mm/rmap.c +2
-1@@ -1481,7 +1487,8 @@ int try_to_unmap(struct page *page, enum ttu_flags flags) ret =rmap_walk (page, &rwc);if (ret !=SWAP_MLOCK && !page_mapped (page)) - ret =SWAP_SUCCESS ; + ret = (ret ==SWAP_AGAIN_CACHED ) ?SWAP_SUCCESS_CACHED :SWAP_SUCCESS ; +return ret; }
This wants a big fat comment explaining why SWAP_AGAIN_CACHED is turned into SWAP_SUCCESS_CACHED.
I think I understand why this is happening, but I am not sure how to explain it...
Quoted 23 lines
mm/vmscan.c +4
-0@@ -860,6 +860,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,unsigned long nr_reclaimed =0 ;unsigned long nr_writeback =0 ;unsigned long nr_immediate =0 ; +bool tlb_flush_required = false;cond_resched ();@@ -1032,6 +1033,9 @@ static unsigned long shrink_page_list(struct list_head *page_list, goto keep_locked;case SWAP_MLOCK : goto cull_mlocked; +case SWAP_SUCCESS_CACHED : +/* Must flush before free, fall through */ + tlb_flush_required = true;case SWAP_SUCCESS : ;/* try to free the page below */ }
mm/vmscan.c keep: · line 1185
Quoted 7 lines
mm/vmscan.c +2
-1@@ -1176,7 +1180,8 @@ keep: }mem_cgroup_uncharge_list (&free_pages); -try_to_unmap_flush (); +if (tlb_flush_required) +try_to_unmap_flush ();free_hot_cold_page_list (&free_pages, true);
Don't we have to flush the TLB before calling pageout() on the page?
In other words, we would also have to batch up calls to pageout(), if we want to do batched TLB flushing.
This could be accomplished by putting the SWAP_SUCCESS_CACHED pages on a special list, instead of calling pageout() on them immediately, and then calling pageout() on all the pages on that list after the batch flush.
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerRe: [PATCH 3/6] mm: Defer TLB flush after unmap as long as possible
Quoted 30 lines · Rik van Riel
Quoted 29 lines · Mel Gorman
If a PTE is unmapped and it's dirty then it was writable recently. Due to deferred TLB flushing, it's best to assume a writable TLB cache entry exists. With that assumption, the TLB must be flushed before any IO can start or the page is freed to avoid lost writes or data corruption. Prior to this patch, such PFNs were simply flushed immediately. In this patch, the caller is informed that such entries potentially exist and it's up to the caller to flush before pages are freed or IO can start.
Signed-off-by Mel Gorman <[email protected]>mm/rmap.c +5
-4@@ -1450,10 +1455,11 @@ static int page_not_mapped(struct page *page)* page, used in the pageout path. Caller must hold the page lock. * Return values are: * -* SWAP_SUCCESS - we succeeded in removing all mappings -* SWAP_AGAIN - we missed a mapping, try again later -* SWAP_FAIL - the page is unswappable -* SWAP_MLOCK - page is mlocked. +* SWAP_SUCCESS - we succeeded in removing all mappings +* SWAP_SUCCESS_CACHED - Like SWAP_SUCCESS but a writable TLB entry may exist +* SWAP_AGAIN - we missed a mapping, try again later +* SWAP_FAIL - the page is unswappable +* SWAP_MLOCK - page is mlocked. */ int try_to_unmap (struct page *page,enum ttu_flags flags) {@@ -1481,7 +1487,8 @@ int try_to_unmap(struct page *page, enum ttu_flags flags) ret =rmap_walk (page, &rwc);
Quoted 10 lines
mm/rmap.c int try_to_unmap(struct page *page, enum ttu_flags flags) · line 1493
Quoted 6 lines
mm/rmap.c +2
-1if (ret !=SWAP_MLOCK && !page_mapped (page)) - ret =SWAP_SUCCESS ; + ret = (ret ==SWAP_AGAIN_CACHED ) ?SWAP_SUCCESS_CACHED :SWAP_SUCCESS ; +return ret; }This wants a big fat comment explaining why
SWAP_AGAIN_CACHEDis turned intoSWAP_SUCCESS_CACHED.
I'll add something in V4. SWAP_AGAIN_CACHED indicates to rmap_walk that it should continue the rmap but that a write cached PTE was encountered. SWAP_SUCCESS is what callers of try_to_unmap() expect so SWAP_SUCCESS_CACHED is the equivalent.
Quoted 28 lines
I think I understand why this is happening, but I am not sure how to explain it...
Quoted 25 lines
mm/vmscan.c +4
-0@@ -860,6 +860,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,unsigned long nr_reclaimed =0 ;unsigned long nr_writeback =0 ;unsigned long nr_immediate =0 ; +bool tlb_flush_required = false;cond_resched ();@@ -1032,6 +1033,9 @@ static unsigned long shrink_page_list(struct list_head *page_list, goto keep_locked;case SWAP_MLOCK : goto cull_mlocked; +case SWAP_SUCCESS_CACHED : +/* Must flush before free, fall through */ + tlb_flush_required = true;case SWAP_SUCCESS : ;/* try to free the page below */ }@@ -1176,7 +1180,8 @@ keep: }
Quoted 8 lines
mm/vmscan.c keep: · line 1185
Quoted 5 lines
mm/vmscan.c +2
-1mem_cgroup_uncharge_list (&free_pages); -try_to_unmap_flush (); +if (tlb_flush_required) +try_to_unmap_flush ();free_hot_cold_page_list (&free_pages, true);Don't we have to flush the TLB before calling
pageout()on the page?
Not any more. It got removed in patch 2 up and I forgot to reintroduce it with a tlb_flush_required check here. Thanks for that.
Quoted 8 lines
In other words, we would also have to batch up calls to
pageout(), if we want to do batched TLB flushing.This could be accomplished by putting the
SWAP_SUCCESS_CACHEDpages on a special list, instead of callingpageout()on them immediately, and then callingpageout()on all the pages on that list after the batch flush.
True. We had discussed something like that on IRC. It's a good idea but a separate patch because it's less clear-cut. We're taking a partial pass through the list in an attempt to reduce IPIs.
Signature · 8 lines
Mel Gorman SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 4/6 mm, migrate: Drop references to successfully migrated pages at the same time
In reply to Mel GormanAfter each migration attempt, putback_lru_page() is used to drop the last reference to the page. This is fine but it prevents the batching of TLB flushes because the flush must happen before a free. This patch drops all the migrated pages at once in preparation for batching the TLB flush.
--- mm/migrate.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
mm/migrate.c +11 -3
@@ -906,7 +906,7 @@ out:*/ static int unmap_and_move (new_page_t get_new_page,free_page_t put_new_page,unsigned long private,struct page *page,int force, -enum migrate_mode mode) +enum migrate_mode mode,struct list_head *putback_list) {int rc =0 ;int *result =NULL ;
@@ -937,7 +937,7 @@ out:list_del (&page->lru);dec_zone_page_state (page,NR_ISOLATED_ANON +page_is_file_cache (page)); -putback_lru_page (page); +list_add (&page->lru, putback_list); }/*
@@ -1086,6 +1086,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, free_page_t put_new_page, unsigned long private, enum migrate_mode mode, int reason) { +LIST_HEAD (putback_list );int retry =1 ;int nr_failed =0 ;int nr_succeeded =0 ;
@@ -1110,7 +1111,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, pass > 2, mode); else rc =unmap_and_move (get_new_page, put_new_page, - private, page, pass >2 , mode); + private, page, pass >2 , mode, + &putback_list);switch (rc) {case -ENOMEM :
@@ -1135,6 +1137,12 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, } rc = nr_failed + retry; out: +while (!list_empty (&putback_list)) { + page = list_entry(putback_list.prev, struct page, lru); +list_del (&page->lru); +putback_lru_page (page); + } +if (nr_succeeded)count_vm_events (PGMIGRATE_SUCCESS , nr_succeeded);if (nr_failed)
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 5/6 mm, migrate: Batch TLB flushing when unmapping pages for migration
In reply to Mel GormanPage reclaim batches multiple TLB flushes into one IPI and this patch teaches page migration to also batch any necessary flushes. MMtests has a THP scale microbenchmark that deliberately fragments memory and then allocates THPs to stress compaction. It's not a page reclaim benchmark and recent kernels avoid excessive compaction but this patch reduced system CPU usage
4.0.0 4.0.0
baseline batchmigrate-v1
User 970.70 1012.24
System 2067.48 1840.00
Elapsed 1520.63 1529.66Note that this particular workload was not TLB flush intensive with peaks in interrupts during the compaction phase. The 4.0 kernel peaked at 345K interrupts/second, the kernel that batches reclaim TLB entries peaked at 13K interrupts/second and this patch peaked at 10K interrupts/second.
--- mm/internal.h | 5 +++++ mm/migrate.c | 13 +++++++++++-- mm/vmscan.c | 6 +----- 3 files changed, 17 insertions(+), 7 deletions(-)
mm/internal.h +5 -0
@@ -436,10 +436,15 @@ struct tlbflush_unmap_batch;#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH void try_to_unmap_flush (void ); +void alloc_tlb_ubc (void );#else static inline void try_to_unmap_flush (void ) { } +static inline void alloc_tlb_ubc (void ) +{ +} +#endif /* CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH */ #endif /* __MM_INTERNAL_H */
mm/migrate.c +11 -2
@@ -878,8 +878,12 @@ static int __unmap_and_move(struct page *page, struct page *newpage,/* Establish migration ptes or remove ptes */ if (page_mapped (page)) { -try_to_unmap (page, -TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); +int ttu_retval =try_to_unmap (page, +TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS |TTU_BATCH_FLUSH ); + +/* Must flush before copy in case of a writable TLB entry */ +if (ttu_retval ==SWAP_SUCCESS_CACHED ) +try_to_unmap_flush (); page_was_mapped =1 ; }
@@ -1099,6 +1103,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,if (!swapwrite) current->flags |=PF_SWAPWRITE ; +alloc_tlb_ubc (); +for (pass =0 ; pass <10 && retry; pass++) { retry =0 ;
@@ -1137,6 +1143,9 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, } rc = nr_failed + retry; out: +/* Must flush before any potential frees */ +try_to_unmap_flush (); +while (!list_empty (&putback_list)) { page = list_entry(putback_list.prev, struct page, lru);list_del (&page->lru);
mm/vmscan.c +1 -5
@@ -2776,7 +2776,7 @@ out:* Allocate the control structure for batch TLB flushing. An allocation * failure is harmless as the reclaimer will send IPIs where necessary. */ -static inline void alloc_tlb_ubc (void ) +void alloc_tlb_ubc (void ) {if (current->tlb_ubc)return ;
@@ -2789,10 +2789,6 @@ static inline void alloc_tlb_ubc(void)cpumask_clear (¤t->tlb_ubc->cpumask); current->tlb_ubc->nr_pages =0 ; } -#else -static inline void alloc_tlb_ubc (void ) -{ -}#endif /* CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH */ unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
Signature · 7 lines
2.1.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerPATCH 6/6 mm: Gather more PFNs before sending a TLB to flush unmapped pages
In reply to Mel GormanThe patch "mm: Send a single IPI to TLB flush multiple pages when unmapping" would batch 32 pages before sending an IPI. This patch increases the size of the data structure to hold a pages worth of PFNs before sending an IPI. This is a trade-off between memory usage and reducing IPIS sent. In the ideal case where multiple processes are reading large mapped files, this patch reduces interrupts/second from roughly 180K per second to 60K per second.
--- include/linux/sched.h | 9 +++++---- kernel/fork.c | 6 ++++-- mm/vmscan.c | 5 +++-- 3 files changed, 12 insertions(+), 8 deletions(-)
include/linux/sched.h +5 -4
@@ -1275,16 +1275,17 @@ enum perf_event_task_context { perf_nr_task_contexts, }; -/* Matches SWAP_CLUSTER_MAX but refined to limit header dependencies */ -#define BATCH_TLBFLUSH_SIZE 32UL -/* Track pages that require TLB flushes */ struct tlbflush_unmap_batch {struct cpumask cpumask;unsigned long nr_pages; -unsigned long pfns[BATCH_TLBFLUSH_SIZE ]; +unsigned long pfns[0 ]; }; +/* alloc_tlb_ubc() always allocates a page */ +#define BATCH_TLBFLUSH_SIZE \ + ((PAGE_SIZE - sizeof(struct tlbflush_unmap_batch)) / sizeof(unsigned long)) +struct task_struct {volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */ void *stack;
kernel/fork.c +4 -2
@@ -247,8 +247,10 @@ void __put_task_struct(struct task_struct *tsk)put_signal_struct (tsk->signal);#ifdef CONFIG_ARCH_SUPPORTS_LOCAL_TLB_PFN_FLUSH -kfree (tsk->tlb_ubc); - tsk->tlb_ubc =NULL ; +if (tsk->tlb_ubc) { +free_page ((unsigned long )tsk->tlb_ubc); + tsk->tlb_ubc =NULL ; + }#endif if (!profile_handoff_task (tsk))
mm/vmscan.c +3 -2
@@ -2775,14 +2775,15 @@ out:/* * Allocate the control structure for batch TLB flushing. An allocation * failure is harmless as the reclaimer will send IPIs where necessary. +* If the allocation size changes then update BATCH_TLBFLUSH_SIZE. */ void alloc_tlb_ubc (void ) {if (current->tlb_ubc)return ; - current->tlb_ubc =kmalloc (sizeof (struct tlbflush_unmap_batch ), -GFP_ATOMIC | __GFP_NOWARN); + current->tlb_ubc = (struct tlbflush_unmap_batch *) +__get_free_page (GFP_KERNEL | __GFP_NOWARN);if (!current->tlb_ubc)return ;
Signature · 1 line
2.1.2
Quoted 2 lines · Mel Gorman
Changelog since V2
o Ensure TLBs are flushed before pages are freed (mel)
I admit not reading all the patches thoroughly, but doesn't this change of ordering mean that you no longer need the architectural guarantee discussed in patch 2? What's the harm if some other CPU (because the CPU didn't receive an IPI yet) manages to write to a page that you have unmapped in the page tables *but not yet freed*?
Vlastimil
Quoted 30 lines
Changelog since V1
o Structure and variable renaming (hughd)
o Defer flushes even if the unmapping process is sleeping (huged)
o Alternative sizing of structure (peterz)
o UseGFP_KERNELinstead ofGFP_ATOMIC,PF_MEMALLOCprotects (andi)
o Immediately flush dirty PTEs to avoid corruption (mel)
o Further clarify docs on the required arch guarantees (mel)When unmapping pages it is necessary to flush the TLB. If that page was accessed by another CPU then an IPI is used to flush the remote CPU. That is a lot of IPIs if kswapd is scanning and unmapping >100K pages per second.
There already is a window between when a page is unmapped and when it is TLB flushed. This series simply increases the window so multiple pages can be flushed using a single IPI.
Patch 1 simply made the rest of the series easier to write as ftrace
could identify all the senders of TLB flush IPIS.Patch 2 collects a list of PFNs and sends one IPI to flush them all
Patch 3 uses more memory so further defer when the IPI gets sent
Patch 4 uses the same infrastructure as patch 2 to batch IPIs sent during
page migration.The performance impact is documented in the changelogs but in the optimistic case on a 4-socket machine the full series reduces interrupts from 900K interrupts/second to 60K interrupts/second.
Quoted 17 lines
arch/x86/Kconfig | 1 + arch/x86/include/asm/tlbflush.h | 2 + arch/x86/mm/tlb.c | 1 + include/linux/init_task.h | 8 +++ include/linux/mm_types.h | 1 + include/linux/rmap.h | 13 ++-- include/linux/sched.h | 15 ++++ include/trace/events/tlb.h | 3 +- init/Kconfig | 8 +++ kernel/fork.c | 7 ++ kernel/sched/core.c | 3 + mm/internal.h | 16 +++++ mm/migrate.c | 27 +++++-- mm/rmap.c | 151 ++++++++++++++++++++++++++++++++++++---- mm/vmscan.c | 35 +++++++++- 15 files changed, 267 insertions(+), 24 deletions(-)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Mel Gorman
reviewerQuoted 7 lines · Vlastimil Babka
Quoted 2 lines · Mel Gorman
Changelog since V2
o Ensure TLBs are flushed before pages are freed (mel)I admit not reading all the patches thoroughly, but doesn't this change of ordering mean that you no longer need the architectural guarantee discussed in patch 2?
No. If we unmap a page to write it to disk then we cannot allow a CPU to write to the physical page being written through a cached entry.
Signature · 8 lines
Mel Gorman SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>