mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaim
Subsystems (6)
cover mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaim
This series revives Buddy Lumpkin's earlier multi-kswapd proposal:
https://lkml.iu.edu/hypermail/linux/kernel/1804.0/00342.html
The motivation is stronger now than when the patch was first discussed. Many current systems have hundreds of cores per NUMA node, not the single-digit or low-tens core counts that were more common at the time. When reclaim does not keep up, direct reclaim can still push allocation latency into application paths and leave substantial CPU capacity waiting for memory to be freed.
This patchset adds adaptive multi-threaded kswapd. The wakeup policy uses node load to decide how many kswapd workers to run, so reclaim can scale when it helps and stay conservative on already busy nodes.
Series summary:
1. Allow multiple kswapd threads per node and add control plumbing.
2. Wake an appropriate number of kswapd threads from per-node runnable load.
Concerns from the original discussion and how this series addresses some of them:
- Concern: Direct reclaim is intended to slow a memory-hogging thread.
Response: That can be acceptable on lower-core systems. On high-core systems, idling many cores while reclaim catches up can cost more than allowing reclaim parallelism to scale. It can also block higher-priority tasks in direct reclaim while they perform reclaim work on behalf of lower-priority memory-hogging tasks.
- Concern: More kswapd threads may hide deeper reclaim issues.
Response: This series is additive to ongoing reclaim improvements. In our testing, multi-threaded kswapd was able to improve performance on top of what multi-gen LRU already provides.
- Concern: Existing knobs (such as swappiness and watermarks) should be preferred.
Response: In our testing, those knobs alone did not reliably hit performance targets and could increase CPU cost for the same workload objective.
- Concern: Need evidence from real workloads.
Response: This cover letter includes Cassandra results showing higher throughput and lower response latency.
- Concern: More reclaim threads may increase pressure on well-behaved tasks.
Response: Adaptive wakeup addresses this by choosing thread count from node load.
- Concern: Additional configuration can increase operational complexity.
Response: The user-facing interface is intentionally minimal:
max_kswapds_per_node.
- Concern: Lock contention may serialize workers.
Response: The Cassandra runs below still show net gains, indicating contention did not erase the benefit for this workload. The wakeup path now uses wake_up_nr() against the existing kswapd_wait queue, avoiding pgdat->kswapd_lock (a sleeping mutex) entirely on the allocation hot path.
Real-world workload results (Cassandra):
Tests were performed on 7.0.0-rc1.
- max_kswapds_per_node=1
- throughput sample: 171146
- reference latency value: 6.375
- op rates: 43256, 42731, 42341, 42818 ops/s
- p99 latency: 6.3, 6.4, 6.4, 6.4 ms
- max_kswapds_per_node=8
- throughput sample: 183639
- reference latency value: 6.0
- op rates: 45791, 45253, 46534, 46061 ops/s
- p99 latency: 6.0, 6.1, 5.9, 6.0 ms
Observed improvement in these runs was about +7.3% throughput and about -5.9% response latency, which shows practical benefit for production-style database workloads.
In our runs, performance numbers were essentially unchanged with and without the adaptive multi-threaded kswapd wakeup policy. In both cases, they outperformed the single-kswapd-thread baseline. This indicates the adaptive method preserved the multi-threaded performance improvement.
Addendum: alternative approaches evaluated
- PSI per NUMA node.
I prototyped PSI-based node pressure ranges to drive wakeup count. This became cumbersome because robust PSI-to-thread mappings were not straightforward across workload types.
- CPU mask snapshot policy.
I also tested a simple CPU mask snapshot approach.
While functional, it reflects a moment-in-time view and does not capture pressure trends over a broader sampling window.
Buddy Lumpkin (1):
vmscan: Support multiple kswapd threads per node
Ehab Ababneh (2):
mm/vmscan: handle racing max_seq advancement
mm/vmscan: make kswapd wakeups NUMA load-aware
include/linux/mmzone.h | 5 +- include/trace/events/vmscan.h | 28 +++ mm/compaction.c | 8 +- mm/internal.h | 3 + mm/page_alloc.c | 26 +++ mm/vmscan.c | 419 +++++++++++++++++++++++++++++++++++++++--- 6 files changed, 465 insertions(+), 24 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH 1/3 vmscan: Support multiple kswapd threads per node
From: Buddy Lumpkin <[email protected]>
Page replacement is handled in the Linux Kernel in one of two ways:
1) Asynchronously via kswapd
2) Synchronously, via direct reclaim
At page allocation time the allocating task is immediately given a page from the zone free list allowing it to go right back to work doing whatever it was doing; Probably directly or indirectly executing business logic.
Just prior to satisfying the allocation, free pages is checked to see if it has reached the zone low watermark and if so, kswapd is awakened. Kswapd will start scanning pages looking for inactive pages to evict to make room for new page allocations. The work of kswapd allows tasks to continue allocating memory from their respective zone free list without incurring any delay.
When the demand for free pages exceeds the rate that kswapd tasks can supply them, page allocation works differently. Once the allocating task finds that the number of free pages is at or below the zone min watermark, the task will no longer pull pages from the free list. Instead, the task will run the same CPU-bound routines as kswapd to satisfy its own allocation by scanning and evicting pages. This is called a direct reclaim.
The time spent performing a direct reclaim can be substantial, often taking tens to hundreds of milliseconds for small order0 allocations to half a second or more for order9 huge-page allocations. In fact, kswapd is not actually required on a linux system. It exists for the sole purpose of optimizing performance by preventing direct reclaims.
When memory shortfall is sufficient to trigger direct reclaims, they can occur in any task that is running on the system. A single aggressive memory allocating task can set the stage for collateral damage to occur in small tasks that rarely allocate additional memory. Consider the impact of injecting an additional 100ms of latency when nscd allocates memory to facilitate caching of a DNS query.
The presence of direct reclaims 10 years ago was a fairly reliable indicator that too much was being asked of a Linux system. Kswapd was likely wasting time scanning pages that were ineligible for eviction. Adding RAM or reducing the working set size would usually make the problem go away. Since then hardware has evolved to bring a new struggle for kswapd. Storage speeds have increased by orders of magnitude while CPU clock speeds stayed the same or even slowed down in exchange for more cores per package. This presents a throughput problem for a single threaded kswapd that will get worse with each generation of new hardware.
Test Details
NOTE: The tests below were run with shadow entries disabled. See the associated patch and cover letter for details
The tests below were designed with the assumption that a kswapd bottleneck is best demonstrated using filesystem reads. This way, the inactive list will be full of clean pages, simplifying the analysis and allowing kswapd to achieve the highest possible steal rate. Maximum steal rates for kswapd are likely to be the same or lower for any other mix of page types on the system.
Tests were run on a 2U Oracle X7-2L with 52 Intel Xeon Skylake 2GHz cores, 756GB of RAM and 8 x 3.6 TB NVMe Solid State Disk drives. Each drive has an XFS file system mounted separately as /d0 through /d7. SSD drives require multiple concurrent streams to show their potential, so I created eleven 250GB zero-filled files on each drive so that I could test with parallel reads.
The test script runs in multiple stages. At each stage, the number of dd tasks run concurrently is increased by 2. I did not include all of the test output for brevity.
During each stage dd tasks are launched to read from each drive in a round robin fashion until the specified number of tasks for the stage has been reached. Then iostat, vmstat and top are started in the background with 10 second intervals. After five minutes, all of the dd tasks are killed and the iostat, vmstat and top output is parsed in order to report the following:
CPU consumption
- sy - aggregate kernel mode CPU consumption from vmstat output. The value doesn't tend to fluctuate much so I just grab the highest value. Each sample is averaged over 10 seconds
- dd_cpu - for all of the dd tasks averaged across the top samples since there is a lot of variation.
Throughput
- in Kbytes
- Command is iostat -x -d 10 -g total
This first test performs reads using O_DIRECT in order to show the maximum throughput that can be obtained using these drives. It also demonstrates how rapidly throughput scales as the number of dd tasks are increased.
The dd command for this test looks like this:
Command Used: dd iflag=direct if=/d${i}/$n of=/dev/null bs=4M
Test #1: Direct IO
dd sy dd_cpu throughput
6 0 2.33 14726026.40 10 1 2.95 19954974.80 16 1 2.63 24419689.30 22 1 2.63 25430303.20 28 1 2.91 26026513.20 34 1 2.53 26178618.00 40 1 2.18 26239229.20 46 1 1.91 26250550.40 52 1 1.69 26251845.60 58 1 1.54 26253205.60 64 1 1.43 26253780.80 70 1 1.31 26254154.80 76 1 1.21 26253660.80 82 1 1.12 26254214.80 88 1 1.07 26253770.00 90 1 1.04 26252406.40
Throughput was close to peak with only 22 dd tasks. Very little system CPU was consumed as expected as the drives DMA directly into the user address space when using direct IO.
In this next test, the iflag=direct option is removed and we only run the test until the pgscan_kswapd from /proc/vmstat starts to increment. At that point metrics are parsed and reported and the pagecache contents are dropped prior to the next test. Lather, rinse, repeat.
Test #2: standard file system IO, no page replacement dd sy dd_cpu throughput
6 2 28.78 5134316.40 10 3 31.40 8051218.40 16 5 34.73 11438106.80 22 7 33.65 14140596.40 28 8 31.24 16393455.20 34 10 29.88 18219463.60 40 11 28.33 19644159.60 46 11 25.05 20802497.60 52 13 26.92 22092370.00 58 13 23.29 22884881.20 64 14 23.12 23452248.80 70 15 22.40 23916468.00 76 16 22.06 24328737.20 82 17 20.97 24718693.20 88 16 18.57 25149404.40 90 16 18.31 25245565.60
Each read has to pause after the buffer in kernel space is populated while those pages are added to the pagecache and copied into the user address space. For this reason, more parallel streams are required to achieve peak throughput. The copy operation consumes substantially more CPU than direct IO as expected.
The next test measures throughput after kswapd starts running. This is the same test only we wait for kswapd to wake up before we start collecting metrics. The script actually keeps track of a few things that were not mentioned earlier. It tracks direct reclaims and page scans by watching the metrics in /proc/vmstat. CPU consumption for kswapd is tracked the same way it is tracked for dd.
Since the test is 100% reads, you can assume that the page steal rate for kswapd and direct reclaims is almost identical to the scan rate.
Test #3: 1 kswapd thread per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 4 26.07 28.56 27.03 7355924.40 0 459316976 0 16 7 34.94 69.33 69.66 10867895.20 0 872661643 0 22 10 36.03 93.99 99.33 13130613.60 489 1037654473 11268334 28 10 30.34 95.90 98.60 14601509.60 671 1182591373 15429142 34 14 34.77 97.50 99.23 16468012.00 10850 1069005644 249839515 40 17 36.32 91.49 97.11 17335987.60 18903 975417728 434467710 46 19 38.40 90.54 91.61 17705394.40 25369 855737040 582427973 52 22 40.88 83.97 83.70 17607680.40 31250 709532935 724282458 58 25 40.89 82.19 80.14 17976905.60 35060 657796473 804117540 64 28 41.77 73.49 75.20 18001910.00 39073 561813658 895289337 70 33 45.51 63.78 64.39 17061897.20 44523 379465571 1020726436 76 36 46.95 57.96 60.32 16964459.60 47717 291299464 1093172384 82 39 47.16 55.43 56.16 16949956.00 49479 247071062 1134163008 88 42 47.41 53.75 47.62 16930911.20 51521 195449924 1180442208 90 43 47.18 51.40 50.59 16864428.00 51618 190758156 1183203901
In the previous test where kswapd was not involved, the system-wide kernel mode CPU consumption with 90 dd tasks was 16%. In this test CPU consumption with 90 tasks is at 43%. With 52 cores, and two kswapd tasks (one per NUMA node), kswapd can only be responsible for a little over 4% of the increase. The rest is likely caused by 51,618 direct reclaims that scanned 1.2 billion pages over the five minute time period of the test.
Same test, more kswapd tasks:
Test #4: 4 kswapd threads per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 5 27.09 16.65 14.17 7842605.60 0 459105291 0 16 10 37.12 26.02 24.85 11352920.40 15 920527796 358515 22 11 36.94 37.13 35.82 13771869.60 0 1132169011 0 28 13 35.23 48.43 46.86 16089746.00 0 1312902070 0 34 15 33.37 53.02 55.69 18314856.40 0 1476169080 0 40 19 35.90 69.60 64.41 19836126.80 0 1629999149 0 46 22 36.82 88.55 57.20 20740216.40 0 1708478106 0 52 24 34.38 93.76 68.34 21758352.00 0 1794055559 0 58 24 30.51 79.20 82.33 22735594.00 0 1872794397 0 64 26 30.21 97.12 76.73 23302203.60 176 1916593721 4206821 70 33 32.92 92.91 92.87 23776588.00 3575 1817685086 85574159 76 37 31.62 91.20 89.83 24308196.80 4752 1812262569 113981763 82 29 25.53 93.23 92.33 24802791.20 306 2032093122 7350704 88 43 37.12 76.18 77.01 25145694.40 20310 1253204719 487048202 90 42 38.56 73.90 74.57 22516787.60 22774 1193637495 545463615
By increasing the number of kswapd threads, throughput increased by ~50% while kernel mode CPU utilization decreased or stayed the same, likely due to a decrease in the number of parallel tasks at any given time doing page replacement.
--- include/linux/mmzone.h | 5 +- mm/compaction.c | 8 +++- mm/internal.h | 3 ++ mm/page_alloc.c | 26 +++++++++++ mm/vmscan.c | 104 +++++++++++++++++++++++++++++++++++------ 5 files changed, 128 insertions(+), 18 deletions(-)
include/linux/mmzone.h +4 -1
@@ -62,6 +62,8 @@*/ #define PAGE_ALLOC_COSTLY_ORDER 3 +#define MAX_KSWAPD_THREADS 16 +#if !defined(CONFIG_HAVE_GIGANTIC_FOLIOS )/* * We don't expect any folios that exceed buddy sizes (and consequently
@@ -1516,7 +1518,8 @@ typedef struct pglist_data {#ifdef CONFIG_MEMORY_HOTPLUG struct mutex kswapd_lock;#endif -struct task_struct *kswapd;/* Protected by kswapd_lock */ +/* Protected by kswapd_lock */ +struct task_struct *kswapd[MAX_KSWAPD_THREADS ];int kswapd_order;enum zone_type kswapd_highest_zoneidx;
mm/compaction.c +6 -2
@@ -2168,9 +2168,13 @@ static isolate_migrate_t isolate_migratepages(struct compact_control *cc)static bool kswapd_is_running (pg_data_t *pgdat) {bool running; - +int i;pgdat_kswapd_lock (pgdat); - running = pgdat->kswapd &&task_is_running (pgdat->kswapd); +for (i =0 ; i <MAX_KSWAPD_THREADS ; ++i) { + running = pgdat->kswapd[i] &&task_is_running (pgdat->kswapd[i]); +if (running) +break ; + }pgdat_kswapd_unlock (pgdat);return running;
mm/internal.h +3 -0
@@ -645,10 +645,13 @@ extern char * const zone_names[MAX_NR_ZONES];/* perform sanity checks on struct pages being allocated or freed */ DECLARE_STATIC_KEY_MAYBE (CONFIG_DEBUG_VM , check_pages_enabled); +extern int kswapd_threads; +extern int max_kswapd_threads;extern int min_free_kbytes;extern int defrag_mode;void setup_per_zone_wmarks (void ); +void update_kswapd_threads (void );void calculate_min_free_kbytes (void );int __meminit init_per_zone_wmark_min (void );void page_alloc_sysctl_init (void );
mm/page_alloc.c +26 -0
@@ -273,6 +273,7 @@ int user_min_free_kbytes = -1;static int watermark_boost_factor__read_mostly =15000 ;static int watermark_scale_factor =10 ;int defrag_mode; +int max_kswapd_threads =MAX_KSWAPD_THREADS ;/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ int movable_zone;
@@ -6614,6 +6615,22 @@ static int min_free_kbytes_sysctl_handler(const struct ctl_table *table, int wrireturn 0 ; } +static int kswapd_threads_sysctl_handler (const struct ctl_table *table, +int write,void __user *buffer, +size_t *length,loff_t *ppos) +{ +int rc; + + rc =proc_dointvec_minmax (table, write, buffer, length, ppos); +if (rc) +return rc; + +if (write) +update_kswapd_threads (); + +return 0 ; +} +static int watermark_scale_factor_sysctl_handler (const struct ctl_table *table,int write,void *buffer,size_t *length,loff_t *ppos) {
@@ -6776,6 +6793,15 @@ static const struct ctl_table page_alloc_sysctl_table[] = { .proc_handler = proc_dointvec_minmax, .extra1 = SYSCTL_ZERO, }, + { + .procname = "kswapd_threads", + .data = &kswapd_threads, + .maxlen = sizeof(kswapd_threads), + .mode = 0644, + .proc_handler = kswapd_threads_sysctl_handler, + .extra1 = SYSCTL_ONE, + .extra2 = &max_kswapd_threads, + }, { .procname = "watermark_scale_factor", .data = &watermark_scale_factor,
mm/vmscan.c +89 -15
134 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 2/3 mm/vmscan: handle racing max_seq advancement
In reply to Ehab AbabnehWhen kswapd support is expanded to multiple threads per node, concurrent aging becomes more likely and try_to_inc_max_seq() can lose a race to another aging pass that has already advanced max_seq.
Treat that case as success instead of warning, since the desired state has already been reached by a concurrent updater.
--- mm/vmscan.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
mm/vmscan.c +6 -5
@@ -3942,15 +3942,18 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappinessstruct lru_gen_folio *lrugen = &lruvec->lrugen; restart:if (seq <READ_ONCE (lrugen->max_seq)) -return false; +return true;lruvec_lock_irq (lruvec);VM_WARN_ON_ONCE (!seq_is_valid (lruvec)); success = seq == lrugen->max_seq; -if (!success) +if (!success) { +/* Concurrent aging advanced max_seq first; goal achieved. */ + success = true; goto unlock; + }for (type =0 ; type <ANON_AND_FILE ; type++) {if (get_nr_gens (lruvec, type) !=MAX_NR_GENS )
@@ -4045,10 +4048,8 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,walk_mm (mm, walk); }while (mm); done: -if (success) { +if (success) success =inc_max_seq (lruvec, seq, swappiness); -WARN_ON_ONCE (!success); - }return success; }
Signature · 1 line
2.43.0
RFC PATCH 3/3 mm/vmscan: make kswapd wakeups NUMA load-aware
In reply to Ehab AbabnehTrack a per-node 10-second rolling average of idle CPU cores and use it in wakeup_kswapd() to decide how many kswapd threads to wake.
A background worker samples the number of idle cores on each NUMA node once per second (node_cores - nr_running, clamped to zero) and maintains a rolling average over a 10-second window. When memory pressure triggers a wakeup, the number of kswapd threads woken is capped to that average so reclaim scales with available CPU capacity.
On a lightly loaded node most cores are idle, so more threads wake and reclaim can proceed quickly. On a saturated node few cores are idle, so fewer threads wake, avoiding unnecessary scheduling contention that would compete with the workload already running there.
--- include/trace/events/vmscan.h | 28 +++ mm/internal.h | 4 +- mm/page_alloc.c | 16 +- mm/vmscan.c | 340 ++++++++++++++++++++++++++++++++-- 4 files changed, 359 insertions(+), 29 deletions(-)
include/trace/events/vmscan.h +28 -0
@@ -174,6 +174,34 @@ TRACE_EVENT(mm_vmscan_wakeup_kswapd, show_gfp_flags(__entry->gfp_flags)) ); +TRACE_EVENT (mm_vmscan_kswapd_threads_to_wake, + + TP_PROTO(int nid, int threads_to_wake, int node_cpus, + int avg_idle_cores), + +TP_ARGS (nid, threads_to_wake, node_cpus, avg_idle_cores), + +TP_STRUCT__entry ( + __field(int, nid) + __field(int, threads_to_wake) + __field(int, node_cpus) +__field (int, avg_idle_cores) + ), + +TP_fast_assign ( + __entry->nid = nid; + __entry->threads_to_wake = threads_to_wake; + __entry->node_cpus = node_cpus; + __entry->avg_idle_cores = avg_idle_cores; + ), + +TP_printk ("nid=%d threads_to_wake=%d node_cpus=%d avg_idle_cores=%d" , + __entry->nid, + __entry->threads_to_wake, + __entry->node_cpus, + __entry->avg_idle_cores) +); + DECLARE_EVENT_CLASS(mm_vmscan_direct_reclaim_begin_template, TP_PROTO(gfp_t gfp_flags, int order, struct mem_cgroup *memcg),
mm/internal.h +2 -2
@@ -645,13 +645,13 @@ extern char * const zone_names[MAX_NR_ZONES];/* perform sanity checks on struct pages being allocated or freed */ DECLARE_STATIC_KEY_MAYBE (CONFIG_DEBUG_VM , check_pages_enabled); -extern int kswapd_threads; +extern int max_kswapds_per_node;extern int max_kswapd_threads;extern int min_free_kbytes;extern int defrag_mode;void setup_per_zone_wmarks (void ); -void update_kswapd_threads (void ); +void update_max_kswapds_per_node (void );void calculate_min_free_kbytes (void );int __meminit init_per_zone_wmark_min (void );void page_alloc_sysctl_init (void );
mm/page_alloc.c +8 -8
@@ -6615,9 +6615,9 @@ static int min_free_kbytes_sysctl_handler(const struct ctl_table *table, int wrireturn 0 ; } -static int kswapd_threads_sysctl_handler (const struct ctl_table *table, -int write,void __user *buffer, -size_t *length,loff_t *ppos) +static int max_kswapds_per_node_sysctl_handler (const struct ctl_table *table, +int write,void __user *buffer, +size_t *length,loff_t *ppos) {int rc;
@@ -6626,7 +6626,7 @@ static int kswapd_threads_sysctl_handler(const struct ctl_table *table,return rc;if (write) -update_kswapd_threads (); +update_max_kswapds_per_node ();return 0 ; }
@@ -6794,11 +6794,11 @@ static const struct ctl_table page_alloc_sysctl_table[] = { .extra1 = SYSCTL_ZERO, }, { - .procname = "kswapd_threads", - .data = &kswapd_threads, - .maxlen = sizeof(kswapd_threads), + .procname = "max_kswapds_per_node", + .data = &max_kswapds_per_node, + .maxlen = sizeof(max_kswapds_per_node), .mode = 0644, - .proc_handler = kswapd_threads_sysctl_handler, + .proc_handler = max_kswapds_per_node_sysctl_handler, .extra1 = SYSCTL_ONE, .extra2 = &max_kswapd_threads, },
mm/vmscan.c +321 -19
438 lines not on this page · raw message
Signature · 1 line
2.43.0
Zi Yan
reviewerQuoted 2 lines · Ehab Ababneh
This series revives Buddy Lumpkin's earlier multi-kswapd proposal:
https://lkml.iu.edu/hypermail/linux/kernel/1804.0/00342.html
+others
Please Cc everyone in the cover letter, so people can know the motivation of the patchset.
Quoted 75 lines
The motivation is stronger now than when the patch was first discussed. Many current systems have hundreds of cores per NUMA node, not the single-digit or low-tens core counts that were more common at the time. When reclaim does not keep up, direct reclaim can still push allocation latency into application paths and leave substantial CPU capacity waiting for memory to be freed.
This patchset adds adaptive multi-threaded kswapd. The wakeup policy uses node load to decide how many kswapd workers to run, so reclaim can scale when it helps and stay conservative on already busy nodes.
Series summary:
1. Allow multiple kswapd threads per node and add control plumbing.
2. Wake an appropriate number of kswapd threads from per-node runnable load.Concerns from the original discussion and how this series addresses some of them:
- Concern: Direct reclaim is intended to slow a memory-hogging thread.
Response: That can be acceptable on lower-core systems. On high-core systems, idling many cores while reclaim catches up can cost more than allowing reclaim parallelism to scale. It can also block higher-priority tasks in direct reclaim while they perform reclaim work on behalf of lower-priority memory-hogging tasks.- Concern: More kswapd threads may hide deeper reclaim issues.
Response: This series is additive to ongoing reclaim improvements. In our testing, multi-threaded kswapd was able to improve performance on top of what multi-gen LRU already provides.- Concern: Existing knobs (such as swappiness and watermarks) should be preferred.
Response: In our testing, those knobs alone did not reliably hit performance targets and could increase CPU cost for the same workload objective.- Concern: Need evidence from real workloads.
Response: This cover letter includes Cassandra results showing higher throughput and lower response latency.- Concern: More reclaim threads may increase pressure on well-behaved tasks.
Response: Adaptive wakeup addresses this by choosing thread count from node load.- Concern: Additional configuration can increase operational complexity.
Response: The user-facing interface is intentionally minimal:
max_kswapds_per_node.- Concern: Lock contention may serialize workers.
Response: The Cassandra runs below still show net gains, indicating contention did not erase the benefit for this workload. The wakeup path now useswake_up_nr()against the existingkswapd_waitqueue, avoidingpgdat->kswapd_lock(a sleeping mutex) entirely on the allocation hot path.Real-world workload results (Cassandra):
Tests were performed on 7.0.0-rc1.
- max_kswapds_per_node=1
- throughput sample: 171146
- reference latency value: 6.375
- op rates: 43256, 42731, 42341, 42818 ops/s
- p99 latency: 6.3, 6.4, 6.4, 6.4 ms- max_kswapds_per_node=8
- throughput sample: 183639
- reference latency value: 6.0
- op rates: 45791, 45253, 46534, 46061 ops/s
- p99 latency: 6.0, 6.1, 5.9, 6.0 ms
Quoted 3 lines
Observed improvement in these runs was about +7.3% throughput and about -5.9% response latency, which shows practical benefit for production-style database workloads.
But the result suggested we should not have multi-threaded kswapd, because 7 extra threads give only 7.3% throughput. That is not a good use of CPU cores. I suspect these threads are competing for LRU locks. We might want to partition each LRU list into N first, before N kswapds can work truely in parallel.
Quoted 32 lines
In our runs, performance numbers were essentially unchanged with and without the adaptive multi-threaded kswapd wakeup policy. In both cases, they outperformed the single-kswapd-thread baseline. This indicates the adaptive method preserved the multi-threaded performance improvement.
Addendum: alternative approaches evaluated
- PSI per NUMA node.
I prototyped PSI-based node pressure ranges to drive wakeup count. This became cumbersome because robust PSI-to-thread mappings were not straightforward across workload types.- CPU mask snapshot policy.
I also tested a simple CPU mask snapshot approach.
While functional, it reflects a moment-in-time view and does not capture pressure trends over a broader sampling window.Buddy Lumpkin (1):
vmscan: Support multiple kswapd threads per nodeEhab Ababneh (2):
mm/vmscan: handle racingmax_seqadvancement
mm/vmscan: make kswapd wakeups NUMA load-awareinclude/linux/mmzone.h | 5 +- include/trace/events/vmscan.h | 28 +++ mm/compaction.c | 8 +- mm/internal.h | 3 + mm/page_alloc.c | 26 +++ mm/vmscan.c | 419 +++++++++++++++++++++++++++++++++++++++--- 6 files changed, 465 insertions(+), 24 deletions(-)
Signature · 2 lines
Best Regards, Yan, Zi
Quoted 107 lines
-----Original Message-----
From: Zi Yan <[email protected]>
Sent: Tuesday, September 8, 2026 7:16 PM
To: Ababneh, Ehab <[email protected]>; [email protected]; linux- [email protected]
Cc: Buddy Lumpkin <[email protected]>; Andrew Morton <[email protected]>; Kairui Song <[email protected]>; Qi Zheng <[email protected]>; Shakeel Butt <[email protected]>; Barry Song <[email protected]>; Axel Rasmussen <[email protected]>; Yuanchu Xie <[email protected]>; Wei Xu <[email protected]>; David Hildenbrand <[email protected]>; Lorenzo Stoakes <[email protected]>; Liam R. Howlett <[email protected]>; Vlastimil Babka <[email protected]>; Mike Rapoport <[email protected]>; Suren Baghdasaryan <[email protected]>; Michal Hocko <[email protected]>; Brendan Jackman <[email protected]>; Johannes Weiner <[email protected]>
Subject: Re: [RFC PATCH 0/3] mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaimQuoted 2 lines · Ehab Ababneh
This series revives Buddy Lumpkin's earlier multi-kswapd proposal:
https://lkml.iu.edu/hypermail/linux/kernel/1804.0/00342.html+others
Please Cc everyone in the cover letter, so people can know the motivation of the patchset.
Quoted 75 lines
The motivation is stronger now than when the patch was first discussed. Many current systems have hundreds of cores per NUMA node, not the single-digit or low-tens core counts that were more common at the time. When reclaim does not keep up, direct reclaim can still push allocation latency into application paths and leave substantial CPU capacity waiting for memory to be freed.
This patchset adds adaptive multi-threaded kswapd. The wakeup policy uses node load to decide how many kswapd workers to run, so reclaim can scale when it helps and stay conservative on already busy nodes.
Series summary:
1. Allow multiple kswapd threads per node and add control plumbing.
2. Wake an appropriate number of kswapd threads from per-node runnable load.Concerns from the original discussion and how this series addresses some of them:
- Concern: Direct reclaim is intended to slow a memory-hogging thread.
Response: That can be acceptable on lower-core systems. On high-core systems, idling many cores while reclaim catches up can cost more than allowing reclaim parallelism to scale. It can also block higher-priority tasks in direct reclaim while they perform reclaim work on behalf of lower-priority memory-hogging tasks.- Concern: More kswapd threads may hide deeper reclaim issues.
Response: This series is additive to ongoing reclaim improvements. In our testing, multi-threaded kswapd was able to improve performance on top of what multi-gen LRU already provides.- Concern: Existing knobs (such as swappiness and watermarks) should be preferred.
Response: In our testing, those knobs alone did not reliably hit performance targets and could increase CPU cost for the same workload objective.- Concern: Need evidence from real workloads.
Response: This cover letter includes Cassandra results showing higher throughput and lower response latency.- Concern: More reclaim threads may increase pressure on well-behaved tasks.
Response: Adaptive wakeup addresses this by choosing thread count from node load.- Concern: Additional configuration can increase operational complexity.
Response: The user-facing interface is intentionally minimal:
max_kswapds_per_node.- Concern: Lock contention may serialize workers.
Response: The Cassandra runs below still show net gains, indicating contention did not erase the benefit for this workload. The wakeup path now useswake_up_nr()against the existingkswapd_waitqueue, avoidingpgdat->kswapd_lock(a sleeping mutex) entirely on the allocation hot path.Real-world workload results (Cassandra):
Tests were performed on 7.0.0-rc1.
- max_kswapds_per_node=1
- throughput sample: 171146
- reference latency value: 6.375
- op rates: 43256, 42731, 42341, 42818 ops/s
- p99 latency: 6.3, 6.4, 6.4, 6.4 ms- max_kswapds_per_node=8
- throughput sample: 183639
- reference latency value: 6.0
- op rates: 45791, 45253, 46534, 46061 ops/s
- p99 latency: 6.0, 6.1, 5.9, 6.0 msQuoted 3 lines
Observed improvement in these runs was about +7.3% throughput and about -5.9% response latency, which shows practical benefit for production-style database workloads.
Quoted 6 lines
But the result suggested we should not have multi-threaded kswapd, because 7 extra threads give only 7.3% throughput. That is not a good use of CPU cores. I suspect these threads are competing for LRU locks.
We might want to partition each LRU list into N first, before N kswapds can work truely in parallel.
Thanks for the feedback. I agree that scaling beyond a small number of workers needs more investigation.
The purpose of this work is to make the maximum number of kswapd threads configurable rather than hard-coding one thread per node. The useful number is workload- and hardware-dependent, but the hard-coded value of one is the least favorable value in all of our tests. The adaptive wakeup policy only wakes workers when the node has idle CPU capacity. Therefore, it does not force additional reclaim work onto already busy CPUs.
Improving P99 latency under memory pressure is difficult, so even a small improvement can be valuable. These results also show that a value greater than one can be beneficial for a given workload. In the latest test, increasing from one to four threads improved throughput from 195,169 to 229,976 op/s (+17.8%) and reduced mean P99 latency from 6.300 ms to 5.975 ms (-5.2%).
Going from four to eight threads added only 0.8% throughput and regressed P99 latency. This suggests that four threads are beneficial for this workload, while higher counts need further investigation.
Quoted 42 lines
Quoted 5 lines
In our runs, performance numbers were essentially unchanged with and without the adaptive multi-threaded kswapd wakeup policy. In both cases, they outperformed the single-kswapd-thread baseline. This indicates the adaptive method preserved the multi-threaded performance
improvement.
Quoted 6 lines
Addendum: alternative approaches evaluated
- PSI per NUMA node.
I prototyped PSI-based node pressure ranges to drive wakeup count. This became cumbersome because robust PSI-to-thread mappings werenot
Quoted 14 lines
straightforward across workload types.
- CPU mask snapshot policy.
I also tested a simple CPU mask snapshot approach.
While functional, it reflects a moment-in-time view and does not capture pressure trends over a broader sampling window.Buddy Lumpkin (1):
vmscan: Support multiple kswapd threads per nodeEhab Ababneh (2):
mm/vmscan: handle racingmax_seqadvancement
mm/vmscan: make kswapd wakeups NUMA load-awareQuoted 6 lines
include/linux/mmzone.h | 5 +- include/trace/events/vmscan.h | 28 +++ mm/compaction.c | 8 +- mm/internal.h | 3 + mm/page_alloc.c | 26 +++ mm/vmscan.c | 419+++++++++++++++++++++++++++++++++++++++---
Quoted 1 line
6 files changed, 465 insertions(+), 24 deletions(-)--
Best Regards,
Yan, Zi
v2 cover mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaim
This is a follow-up to the RFC for adaptive multi-threaded kswapd on NUMA. It handles concurrent MGLRU aging, adjusts wakeups to node CPU load, and scales the hopeless threshold for multiple workers.
Under memory pressure, an lruvec can end up with only two MGLRU generations. That leaves an older and a younger cohort, a bit like inactive and active LRU lists, though MGLRU tracks age differently.
Regarding the concerns raised in review:
- In the updated Cassandra comparison, four kswapd threads reduced p99 latency from 6.5 ms to 3.35 ms (about 49%) and increased throughput by 10.3%. We believe the latency gain comes from the reclaim design, not from changes to LRU locking.
- This newer result compares four kswapd threads with one. The earlier 7.3% throughput result was measured with eight threads.
We also saw periods with zero file scans. The MIN_NR_GENS guard may
contribute: when only two generations remain, it makes scan_folios() return without scanning the oldest one. This patch tests whether letting kswapd scan that generation helps reclaim progress. The current traces do not tie the zero-scan events to the same lruvec, so this remains a hypothesis; more per-lruvec tracing is needed to compare it with lock contention.
Changes since v1:
- Add a diagnostic kswapd scan path at MIN_NR_GENS.
- Scale the hopeless-node retry threshold by the active worker count.
Testing:
- Cassandra workload with one and four kswapd threads.
Buddy Lumpkin (1):
vmscan: Support multiple kswapd threads per node
Ehab Ababneh (4):
mm/vmscan: silence spurious max_seq race warning mm/vmscan: make kswapd wakeups NUMA load-aware mm/vmscan: let kswapd scan minimum-generation lruvecs mm/vmscan: scale hopeless threshold for kswapd workers
include/linux/mmzone.h | 5 +- include/trace/events/vmscan.h | 32 +++ mm/compaction.c | 8 +- mm/internal.h | 3 + mm/page_alloc.c | 26 +++ mm/vmscan.c | 426 ++++++++++++++++++++++++++++++++-- 6 files changed, 473 insertions(+), 27 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH v2 1/5 vmscan: Support multiple kswapd threads per node
From: Buddy Lumpkin <[email protected]>
Page replacement is handled in the Linux Kernel in one of two ways:
1) Asynchronously via kswapd
2) Synchronously, via direct reclaim
At page allocation time the allocating task is immediately given a page from the zone free list allowing it to go right back to work doing whatever it was doing; Probably directly or indirectly executing business logic.
Just prior to satisfying the allocation, free pages is checked to see if it has reached the zone low watermark and if so, kswapd is awakened. Kswapd will start scanning pages looking for inactive pages to evict to make room for new page allocations. The work of kswapd allows tasks to continue allocating memory from their respective zone free list without incurring any delay.
When the demand for free pages exceeds the rate that kswapd tasks can supply them, page allocation works differently. Once the allocating task finds that the number of free pages is at or below the zone min watermark, the task will no longer pull pages from the free list. Instead, the task will run the same CPU-bound routines as kswapd to satisfy its own allocation by scanning and evicting pages. This is called a direct reclaim.
The time spent performing a direct reclaim can be substantial, often taking tens to hundreds of milliseconds for small order0 allocations to half a second or more for order9 huge-page allocations. In fact, kswapd is not actually required on a linux system. It exists for the sole purpose of optimizing performance by preventing direct reclaims.
When memory shortfall is sufficient to trigger direct reclaims, they can occur in any task that is running on the system. A single aggressive memory allocating task can set the stage for collateral damage to occur in small tasks that rarely allocate additional memory. Consider the impact of injecting an additional 100ms of latency when nscd allocates memory to facilitate caching of a DNS query.
The presence of direct reclaims 10 years ago was a fairly reliable indicator that too much was being asked of a Linux system. Kswapd was likely wasting time scanning pages that were ineligible for eviction. Adding RAM or reducing the working set size would usually make the problem go away. Since then hardware has evolved to bring a new struggle for kswapd. Storage speeds have increased by orders of magnitude while CPU clock speeds stayed the same or even slowed down in exchange for more cores per package. This presents a throughput problem for a single threaded kswapd that will get worse with each generation of new hardware.
Test Details
NOTE: The tests below were run with shadow entries disabled. See the associated patch and cover letter for details
The tests below were designed with the assumption that a kswapd bottleneck is best demonstrated using filesystem reads. This way, the inactive list will be full of clean pages, simplifying the analysis and allowing kswapd to achieve the highest possible steal rate. Maximum steal rates for kswapd are likely to be the same or lower for any other mix of page types on the system.
Tests were run on a 2U Oracle X7-2L with 52 Intel Xeon Skylake 2GHz cores, 756GB of RAM and 8 x 3.6 TB NVMe Solid State Disk drives. Each drive has an XFS file system mounted separately as /d0 through /d7. SSD drives require multiple concurrent streams to show their potential, so I created eleven 250GB zero-filled files on each drive so that I could test with parallel reads.
The test script runs in multiple stages. At each stage, the number of dd tasks run concurrently is increased by 2. I did not include all of the test output for brevity.
During each stage dd tasks are launched to read from each drive in a round robin fashion until the specified number of tasks for the stage has been reached. Then iostat, vmstat and top are started in the background with 10 second intervals. After five minutes, all of the dd tasks are killed and the iostat, vmstat and top output is parsed in order to report the following:
CPU consumption
- sy - aggregate kernel mode CPU consumption from vmstat output. The value doesn't tend to fluctuate much so I just grab the highest value. Each sample is averaged over 10 seconds
- dd_cpu - for all of the dd tasks averaged across the top samples since there is a lot of variation.
Throughput
- in Kbytes
- Command is iostat -x -d 10 -g total
This first test performs reads using O_DIRECT in order to show the maximum throughput that can be obtained using these drives. It also demonstrates how rapidly throughput scales as the number of dd tasks are increased.
The dd command for this test looks like this:
Command Used: dd iflag=direct if=/d${i}/$n of=/dev/null bs=4M
Test #1: Direct IO
dd sy dd_cpu throughput
6 0 2.33 14726026.40 10 1 2.95 19954974.80 16 1 2.63 24419689.30 22 1 2.63 25430303.20 28 1 2.91 26026513.20 34 1 2.53 26178618.00 40 1 2.18 26239229.20 46 1 1.91 26250550.40 52 1 1.69 26251845.60 58 1 1.54 26253205.60 64 1 1.43 26253780.80 70 1 1.31 26254154.80 76 1 1.21 26253660.80 82 1 1.12 26254214.80 88 1 1.07 26253770.00 90 1 1.04 26252406.40
Throughput was close to peak with only 22 dd tasks. Very little system CPU was consumed as expected as the drives DMA directly into the user address space when using direct IO.
In this next test, the iflag=direct option is removed and we only run the test until the pgscan_kswapd from /proc/vmstat starts to increment. At that point metrics are parsed and reported and the pagecache contents are dropped prior to the next test. Lather, rinse, repeat.
Test #2: standard file system IO, no page replacement dd sy dd_cpu throughput
6 2 28.78 5134316.40 10 3 31.40 8051218.40 16 5 34.73 11438106.80 22 7 33.65 14140596.40 28 8 31.24 16393455.20 34 10 29.88 18219463.60 40 11 28.33 19644159.60 46 11 25.05 20802497.60 52 13 26.92 22092370.00 58 13 23.29 22884881.20 64 14 23.12 23452248.80 70 15 22.40 23916468.00 76 16 22.06 24328737.20 82 17 20.97 24718693.20 88 16 18.57 25149404.40 90 16 18.31 25245565.60
Each read has to pause after the buffer in kernel space is populated while those pages are added to the pagecache and copied into the user address space. For this reason, more parallel streams are required to achieve peak throughput. The copy operation consumes substantially more CPU than direct IO as expected.
The next test measures throughput after kswapd starts running. This is the same test only we wait for kswapd to wake up before we start collecting metrics. The script actually keeps track of a few things that were not mentioned earlier. It tracks direct reclaims and page scans by watching the metrics in /proc/vmstat. CPU consumption for kswapd is tracked the same way it is tracked for dd.
Since the test is 100% reads, you can assume that the page steal rate for kswapd and direct reclaims is almost identical to the scan rate.
Test #3: 1 kswapd thread per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 4 26.07 28.56 27.03 7355924.40 0 459316976 0 16 7 34.94 69.33 69.66 10867895.20 0 872661643 0 22 10 36.03 93.99 99.33 13130613.60 489 1037654473 11268334 28 10 30.34 95.90 98.60 14601509.60 671 1182591373 15429142 34 14 34.77 97.50 99.23 16468012.00 10850 1069005644 249839515 40 17 36.32 91.49 97.11 17335987.60 18903 975417728 434467710 46 19 38.40 90.54 91.61 17705394.40 25369 855737040 582427973 52 22 40.88 83.97 83.70 17607680.40 31250 709532935 724282458 58 25 40.89 82.19 80.14 17976905.60 35060 657796473 804117540 64 28 41.77 73.49 75.20 18001910.00 39073 561813658 895289337 70 33 45.51 63.78 64.39 17061897.20 44523 379465571 1020726436 76 36 46.95 57.96 60.32 16964459.60 47717 291299464 1093172384 82 39 47.16 55.43 56.16 16949956.00 49479 247071062 1134163008 88 42 47.41 53.75 47.62 16930911.20 51521 195449924 1180442208 90 43 47.18 51.40 50.59 16864428.00 51618 190758156 1183203901
In the previous test where kswapd was not involved, the system-wide kernel mode CPU consumption with 90 dd tasks was 16%. In this test CPU consumption with 90 tasks is at 43%. With 52 cores, and two kswapd tasks (one per NUMA node), kswapd can only be responsible for a little over 4% of the increase. The rest is likely caused by 51,618 direct reclaims that scanned 1.2 billion pages over the five minute time period of the test.
Same test, more kswapd tasks:
Test #4: 4 kswapd threads per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 5 27.09 16.65 14.17 7842605.60 0 459105291 0 16 10 37.12 26.02 24.85 11352920.40 15 920527796 358515 22 11 36.94 37.13 35.82 13771869.60 0 1132169011 0 28 13 35.23 48.43 46.86 16089746.00 0 1312902070 0 34 15 33.37 53.02 55.69 18314856.40 0 1476169080 0 40 19 35.90 69.60 64.41 19836126.80 0 1629999149 0 46 22 36.82 88.55 57.20 20740216.40 0 1708478106 0 52 24 34.38 93.76 68.34 21758352.00 0 1794055559 0 58 24 30.51 79.20 82.33 22735594.00 0 1872794397 0 64 26 30.21 97.12 76.73 23302203.60 176 1916593721 4206821 70 33 32.92 92.91 92.87 23776588.00 3575 1817685086 85574159 76 37 31.62 91.20 89.83 24308196.80 4752 1812262569 113981763 82 29 25.53 93.23 92.33 24802791.20 306 2032093122 7350704 88 43 37.12 76.18 77.01 25145694.40 20310 1253204719 487048202 90 42 38.56 73.90 74.57 22516787.60 22774 1193637495 545463615
By increasing the number of kswapd threads, throughput increased by ~50% while kernel mode CPU utilization decreased or stayed the same, likely due to a decrease in the number of parallel tasks at any given time doing page replacement.
--- include/linux/mmzone.h | 5 +- mm/compaction.c | 8 +++- mm/internal.h | 3 ++ mm/page_alloc.c | 26 +++++++++++ mm/vmscan.c | 104 +++++++++++++++++++++++++++++++++++------ 5 files changed, 128 insertions(+), 18 deletions(-)
include/linux/mmzone.h +4 -1
@@ -62,6 +62,8 @@*/ #define PAGE_ALLOC_COSTLY_ORDER 3 +#define MAX_KSWAPD_THREADS 16 +#if !defined(CONFIG_HAVE_GIGANTIC_FOLIOS )/* * We don't expect any folios that exceed buddy sizes (and consequently
@@ -1529,7 +1531,8 @@ typedef struct pglist_data {#ifdef CONFIG_MEMORY_HOTPLUG struct mutex kswapd_lock;#endif -struct task_struct *kswapd;/* Protected by kswapd_lock */ +/* Protected by kswapd_lock */ +struct task_struct *kswapd[MAX_KSWAPD_THREADS ];int kswapd_order;enum zone_type kswapd_highest_zoneidx;
mm/compaction.c +6 -2
@@ -2198,9 +2198,13 @@ static isolate_migrate_t isolate_migratepages(struct compact_control *cc)static bool kswapd_is_running (pg_data_t *pgdat) {bool running; - +int i;pgdat_kswapd_lock (pgdat); - running = pgdat->kswapd &&task_is_running (pgdat->kswapd); +for (i =0 ; i <MAX_KSWAPD_THREADS ; ++i) { + running = pgdat->kswapd[i] &&task_is_running (pgdat->kswapd[i]); +if (running) +break ; + }pgdat_kswapd_unlock (pgdat);return running;
mm/internal.h +3 -0
@@ -717,10 +717,13 @@ void set_recommended_min_free_kbytes(void);extern char *const zone_names[MAX_NR_ZONES ]; +extern int kswapd_threads; +extern int max_kswapd_threads;extern int min_free_kbytes;extern int defrag_mode;void setup_per_zone_wmarks (void ); +void update_kswapd_threads (void );void calculate_min_free_kbytes (void );int __meminit init_per_zone_wmark_min (void );
mm/page_alloc.c +26 -0
@@ -277,6 +277,7 @@ int user_min_free_kbytes = -1;static int watermark_boost_factor__read_mostly =15000 ;static int watermark_scale_factor =10 ;int defrag_mode; +int max_kswapd_threads =MAX_KSWAPD_THREADS ;/* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ int movable_zone;
@@ -6777,6 +6778,22 @@ static int min_free_kbytes_sysctl_handler(const struct ctl_table *table, int wrireturn 0 ; } +static int kswapd_threads_sysctl_handler (const struct ctl_table *table, +int write,void __user *buffer, +size_t *length,loff_t *ppos) +{ +int rc; + + rc =proc_dointvec_minmax (table, write, buffer, length, ppos); +if (rc) +return rc; + +if (write) +update_kswapd_threads (); + +return 0 ; +} +static int watermark_scale_factor_sysctl_handler (const struct ctl_table *table,int write,void *buffer,size_t *length,loff_t *ppos) {
@@ -6950,6 +6967,15 @@ static const struct ctl_table page_alloc_sysctl_table[] = { .proc_handler = proc_dointvec_minmax, .extra1 = SYSCTL_ZERO, }, + { + .procname = "kswapd_threads", + .data = &kswapd_threads, + .maxlen = sizeof(kswapd_threads), + .mode = 0644, + .proc_handler = kswapd_threads_sysctl_handler, + .extra1 = SYSCTL_ONE, + .extra2 = &max_kswapd_threads, + }, { .procname = "watermark_scale_factor", .data = &watermark_scale_factor,
mm/vmscan.c +89 -15
134 lines not on this page · raw message
Signature · 1 line
2.43.0
Barry Song
reviewerRe: [RFC PATCH v2 1/5] vmscan: Support multiple kswapd threads per node
Quoted 212 lines · Ehab Ababneh
From: Buddy Lumpkin <[email protected]>
Page replacement is handled in the Linux Kernel in one of two ways:
1) Asynchronously via kswapd
2) Synchronously, via direct reclaimAt page allocation time the allocating task is immediately given a page from the zone free list allowing it to go right back to work doing whatever it was doing; Probably directly or indirectly executing business logic.
Just prior to satisfying the allocation, free pages is checked to see if it has reached the zone low watermark and if so, kswapd is awakened. Kswapd will start scanning pages looking for inactive pages to evict to make room for new page allocations. The work of kswapd allows tasks to continue allocating memory from their respective zone free list without incurring any delay.
When the demand for free pages exceeds the rate that kswapd tasks can supply them, page allocation works differently. Once the allocating task finds that the number of free pages is at or below the zone min watermark, the task will no longer pull pages from the free list. Instead, the task will run the same CPU-bound routines as kswapd to satisfy its own allocation by scanning and evicting pages. This is called a direct reclaim.
The time spent performing a direct reclaim can be substantial, often taking tens to hundreds of milliseconds for small order0 allocations to half a second or more for order9 huge-page allocations. In fact, kswapd is not actually required on a linux system. It exists for the sole purpose of optimizing performance by preventing direct reclaims.
When memory shortfall is sufficient to trigger direct reclaims, they can occur in any task that is running on the system. A single aggressive memory allocating task can set the stage for collateral damage to occur in small tasks that rarely allocate additional memory. Consider the impact of injecting an additional 100ms of latency when nscd allocates memory to facilitate caching of a DNS query.
The presence of direct reclaims 10 years ago was a fairly reliable indicator that too much was being asked of a Linux system. Kswapd was likely wasting time scanning pages that were ineligible for eviction. Adding RAM or reducing the working set size would usually make the problem go away. Since then hardware has evolved to bring a new struggle for kswapd. Storage speeds have increased by orders of magnitude while CPU clock speeds stayed the same or even slowed down in exchange for more cores per package. This presents a throughput problem for a single threaded kswapd that will get worse with each generation of new hardware.
Test Details
NOTE: The tests below were run with shadow entries disabled. See the associated patch and cover letter for details
The tests below were designed with the assumption that a kswapd bottleneck is best demonstrated using filesystem reads. This way, the inactive list will be full of clean pages, simplifying the analysis and allowing kswapd to achieve the highest possible steal rate. Maximum steal rates for kswapd are likely to be the same or lower for any other mix of page types on the system.
Tests were run on a 2U Oracle X7-2L with 52 Intel Xeon Skylake 2GHz cores, 756GB of RAM and 8 x 3.6 TB NVMe Solid State Disk drives. Each drive has an XFS file system mounted separately as /d0 through /d7. SSD drives require multiple concurrent streams to show their potential, so I created eleven 250GB zero-filled files on each drive so that I could test with parallel reads.
The test script runs in multiple stages. At each stage, the number of dd tasks run concurrently is increased by 2. I did not include all of the test output for brevity.
During each stage dd tasks are launched to read from each drive in a round robin fashion until the specified number of tasks for the stage has been reached. Then iostat, vmstat and top are started in the background with 10 second intervals. After five minutes, all of the dd tasks are killed and the iostat, vmstat and top output is parsed in order to report the following:
CPU consumption
- sy - aggregate kernel mode CPU consumption from vmstat output. The value doesn't tend to fluctuate much so I just grab the highest value. Each sample is averaged over 10 seconds
-dd_cpu- for all of the dd tasks averaged across the top samples since there is a lot of variation.Throughput
- in Kbytes
- Command is iostat -x -d 10 -g totalThis first test performs reads using
O_DIRECTin order to show the maximum throughput that can be obtained using these drives. It also demonstrates how rapidly throughput scales as the number of dd tasks are increased.The dd command for this test looks like this:
Command Used: dd iflag=direct if=/d${i}/$n of=/dev/null bs=4M
Test #1: Direct IO
dd sydd_cputhroughput6 0 2.33 14726026.40 10 1 2.95 19954974.80 16 1 2.63 24419689.30 22 1 2.63 25430303.20 28 1 2.91 26026513.20 34 1 2.53 26178618.00 40 1 2.18 26239229.20 46 1 1.91 26250550.40 52 1 1.69 26251845.60 58 1 1.54 26253205.60 64 1 1.43 26253780.80 70 1 1.31 26254154.80 76 1 1.21 26253660.80 82 1 1.12 26254214.80 88 1 1.07 26253770.00 90 1 1.04 26252406.40Throughput was close to peak with only 22 dd tasks. Very little system CPU was consumed as expected as the drives DMA directly into the user address space when using direct IO.
In this next test, the iflag=direct option is removed and we only run the test until the
pgscan_kswapdfrom/proc/vmstatstarts to increment. At that point metrics are parsed and reported and the pagecache contents are dropped prior to the next test. Lather, rinse, repeat.Test #2: standard file system IO, no page replacement dd sy
dd_cputhroughput6 2 28.78 5134316.40 10 3 31.40 8051218.40 16 5 34.73 11438106.80 22 7 33.65 14140596.40 28 8 31.24 16393455.20 34 10 29.88 18219463.60 40 11 28.33 19644159.60 46 11 25.05 20802497.60 52 13 26.92 22092370.00 58 13 23.29 22884881.20 64 14 23.12 23452248.80 70 15 22.40 23916468.00 76 16 22.06 24328737.20 82 17 20.97 24718693.20 88 16 18.57 25149404.40 90 16 18.31 25245565.60Each read has to pause after the buffer in kernel space is populated while those pages are added to the pagecache and copied into the user address space. For this reason, more parallel streams are required to achieve peak throughput. The copy operation consumes substantially more CPU than direct IO as expected.
The next test measures throughput after kswapd starts running. This is the same test only we wait for kswapd to wake up before we start collecting metrics. The script actually keeps track of a few things that were not mentioned earlier. It tracks direct reclaims and page scans by watching the metrics in
/proc/vmstat. CPU consumption for kswapd is tracked the same way it is tracked for dd.Since the test is 100% reads, you can assume that the page steal rate for kswapd and direct reclaims is almost identical to the scan rate.
Test #3: 1 kswapd thread per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 4 26.07 28.56 27.03 7355924.40 0 459316976 0 16 7 34.94 69.33 69.66 10867895.20 0 872661643 0 22 10 36.03 93.99 99.33 13130613.60 489 1037654473 11268334 28 10 30.34 95.90 98.60 14601509.60 671 1182591373 15429142 34 14 34.77 97.50 99.23 16468012.00 10850 1069005644 249839515 40 17 36.32 91.49 97.11 17335987.60 18903 975417728 434467710 46 19 38.40 90.54 91.61 17705394.40 25369 855737040 582427973 52 22 40.88 83.97 83.70 17607680.40 31250 709532935 724282458 58 25 40.89 82.19 80.14 17976905.60 35060 657796473 804117540 64 28 41.77 73.49 75.20 18001910.00 39073 561813658 895289337 70 33 45.51 63.78 64.39 17061897.20 44523 379465571 1020726436 76 36 46.95 57.96 60.32 16964459.60 47717 291299464 1093172384 82 39 47.16 55.43 56.16 16949956.00 49479 247071062 1134163008 88 42 47.41 53.75 47.62 16930911.20 51521 195449924 1180442208 90 43 47.18 51.40 50.59 16864428.00 51618 190758156 1183203901In the previous test where kswapd was not involved, the system-wide kernel mode CPU consumption with 90 dd tasks was 16%. In this test CPU consumption with 90 tasks is at 43%. With 52 cores, and two kswapd tasks (one per NUMA node), kswapd can only be responsible for a little over 4% of the increase. The rest is likely caused by 51,618 direct reclaims that scanned 1.2 billion pages over the five minute time period of the test.
Same test, more kswapd tasks:
Test #4: 4 kswapd threads per node
dd sy dd_cpu kswapd0 kswapd1 throughput dr pgscan_kswapd pgscan_direct 10 5 27.09 16.65 14.17 7842605.60 0 459105291 0 16 10 37.12 26.02 24.85 11352920.40 15 920527796 358515 22 11 36.94 37.13 35.82 13771869.60 0 1132169011 0 28 13 35.23 48.43 46.86 16089746.00 0 1312902070 0 34 15 33.37 53.02 55.69 18314856.40 0 1476169080 0 40 19 35.90 69.60 64.41 19836126.80 0 1629999149 0 46 22 36.82 88.55 57.20 20740216.40 0 1708478106 0 52 24 34.38 93.76 68.34 21758352.00 0 1794055559 0 58 24 30.51 79.20 82.33 22735594.00 0 1872794397 0 64 26 30.21 97.12 76.73 23302203.60 176 1916593721 4206821 70 33 32.92 92.91 92.87 23776588.00 3575 1817685086 85574159 76 37 31.62 91.20 89.83 24308196.80 4752 1812262569 113981763 82 29 25.53 93.23 92.33 24802791.20 306 2032093122 7350704 88 43 37.12 76.18 77.01 25145694.40 20310 1253204719 487048202 90 42 38.56 73.90 74.57 22516787.60 22774 1193637495 545463615By increasing the number of kswapd threads, throughput increased by ~50% while kernel mode CPU utilization decreased or stayed the same, likely due to a decrease in the number of parallel tasks at any given time doing page replacement.
Quoted 4 lines
Signed-off-by Buddy Lumpkin <[email protected]>Signed-off-by Ehab Ababneh <[email protected]>(cherry picked from commit 9ca50cb821f07627d555d31311f1c296bcd415d0)---
Hi Buddy, Ehab,
I'm not quite sure we want to increase the number of kswapd threads. It seems you want to reclaim memory more aggressively in the background to reduce direct reclaim. However, do we really need more kswapd threads when userspace can use other proactive reclamation approaches? For Android, we also have mechanisms like lmkd.
Also, if the bottleneck is aging or compression, we could consider something like kcompressd for asynchronous compression, which has been on hold for a while.
https://lore.kernel.org/linux-mm/[email protected]/
Or perhaps something like kaged for aging? Those could be much lighter than running multiple kswapd threads in parallel.
Best Regards
Barry
RFC PATCH v2 2/5 mm/vmscan: silence spurious max_seq race warning
In reply to Ehab AbabnehWhen kswapd support is expanded to multiple threads per node, concurrent aging becomes more likely and try_to_inc_max_seq() can lose a race to another aging pass that has already advanced max_seq. That is benign and not a bug, so drop the WARN_ON_ONCE that fires on it.
Do not change the returned success value in this case: a lost race still means *this* call performed no eviction-enabling work, and get_nr_to_scan() relies on that to decide whether to keep scanning this lruvec. Treating a lost race as success caused threads to bail out of reclaim early under contention, cutting reclaim throughput and leading to OOM kills under sustained memory pressure (e.g. the Cassandra benchmark).
--- mm/vmscan.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/vmscan.c +1 -3
@@ -4116,10 +4116,8 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,walk_mm (mm, walk); }while (mm); done: -if (success) { +if (success) success =inc_max_seq (lruvec, seq, swappiness); -WARN_ON_ONCE (!success); - }return success; }
Signature · 1 line
2.43.0
RFC PATCH v2 3/5 mm/vmscan: make kswapd wakeups NUMA load-aware
In reply to Ehab AbabnehTrack a per-node 10-second rolling average of idle CPU cores and use it in wakeup_kswapd() to decide how many kswapd threads to wake.
A background worker samples the number of idle cores on each NUMA node once per second (node_cores - nr_running, clamped to zero) and maintains a rolling average over a 10-second window. When memory pressure triggers a wakeup, the number of kswapd threads woken is capped to that average so reclaim scales with available CPU capacity.
On a lightly loaded node most cores are idle, so more threads wake and reclaim can proceed quickly. On a saturated node few cores are idle, so fewer threads wake, avoiding unnecessary scheduling contention that would compete with the workload already running there.
--- include/trace/events/vmscan.h | 32 ++++ mm/internal.h | 4 +- mm/page_alloc.c | 16 +- mm/vmscan.c | 349 +++++++++++++++++++++++++++++++--- 4 files changed, 369 insertions(+), 32 deletions(-)
include/trace/events/vmscan.h +32 -0
@@ -174,6 +174,38 @@ TRACE_EVENT(mm_vmscan_wakeup_kswapd, show_gfp_flags(__entry->gfp_flags)) ); +TRACE_EVENT (mm_vmscan_kswapd_threads_to_wake, + + TP_PROTO(int nid, int threads_to_wake, int threads_woken, + int node_cpus, int avg_idle_cores), + +TP_ARGS (nid, threads_to_wake, threads_woken, node_cpus, + avg_idle_cores), + +TP_STRUCT__entry ( + __field(int, nid) + __field(int, threads_to_wake) + __field(int, threads_woken) + __field(int, node_cpus) +__field (int, avg_idle_cores) + ), + +TP_fast_assign ( + __entry->nid = nid; + __entry->threads_to_wake = threads_to_wake; + __entry->threads_woken = threads_woken; + __entry->node_cpus = node_cpus; + __entry->avg_idle_cores = avg_idle_cores; + ), + +TP_printk ("nid=%d threads_to_wake=%d threads_woken=%d node_cpus=%d avg_idle_cores=%d" , + __entry->nid, + __entry->threads_to_wake, + __entry->threads_woken, + __entry->node_cpus, + __entry->avg_idle_cores) +); + DECLARE_EVENT_CLASS(mm_vmscan_direct_reclaim_begin_template, TP_PROTO(gfp_t gfp_flags, int order, struct mem_cgroup *memcg),
mm/internal.h +2 -2
@@ -717,13 +717,13 @@ void set_recommended_min_free_kbytes(void);extern char *const zone_names[MAX_NR_ZONES ]; -extern int kswapd_threads; +extern int max_kswapds_per_node;extern int max_kswapd_threads;extern int min_free_kbytes;extern int defrag_mode;void setup_per_zone_wmarks (void ); -void update_kswapd_threads (void ); +void update_max_kswapds_per_node (void );void calculate_min_free_kbytes (void );int __meminit init_per_zone_wmark_min (void );
mm/page_alloc.c +8 -8
@@ -6778,9 +6778,9 @@ static int min_free_kbytes_sysctl_handler(const struct ctl_table *table, int wrireturn 0 ; } -static int kswapd_threads_sysctl_handler (const struct ctl_table *table, -int write,void __user *buffer, -size_t *length,loff_t *ppos) +static int max_kswapds_per_node_sysctl_handler (const struct ctl_table *table, +int write,void __user *buffer, +size_t *length,loff_t *ppos) {int rc;
@@ -6789,7 +6789,7 @@ static int kswapd_threads_sysctl_handler(const struct ctl_table *table,return rc;if (write) -update_kswapd_threads (); +update_max_kswapds_per_node ();return 0 ; }
@@ -6968,11 +6968,11 @@ static const struct ctl_table page_alloc_sysctl_table[] = { .extra1 = SYSCTL_ZERO, }, { - .procname = "kswapd_threads", - .data = &kswapd_threads, - .maxlen = sizeof(kswapd_threads), + .procname = "max_kswapds_per_node", + .data = &max_kswapds_per_node, + .maxlen = sizeof(max_kswapds_per_node), .mode = 0644, - .proc_handler = kswapd_threads_sysctl_handler, + .proc_handler = max_kswapds_per_node_sysctl_handler, .extra1 = SYSCTL_ONE, .extra2 = &max_kswapd_threads, },
mm/vmscan.c +327 -22
465 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v2 4/5 mm/vmscan: let kswapd scan minimum-generation lruvecs
In reply to Ehab AbabnehWhen an lruvec has only MIN_NR_GENS generations, scan_folios() returns without scanning that type. Let kswapd scan the oldest generation while keeping the early return for other reclaimers.
Under memory pressure, an lruvec can end up with only two generations. That leaves an older and a younger cohort, a bit like inactive and active LRU lists, though MGLRU tracks age differently.
This is a diagnostic change to see whether kswapd can reclaim from the oldest remaining generation when aging does not advance.
--- mm/vmscan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
@@ -5017,7 +5017,7 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,VM_WARN_ON_ONCE (nr_to_scan >MAX_LRU_BATCH );VM_WARN_ON_ONCE (!list_empty (list)); -if (get_nr_gens (lruvec, type) ==MIN_NR_GENS ) +if (!current_is_kswapd () &&get_nr_gens (lruvec, type) ==MIN_NR_GENS )return 0 ; gen =lru_gen_from_seq (lrugen->min_seq[type]);
Signature · 1 line
2.43.0
RFC PATCH v2 5/5 mm/vmscan: scale hopeless threshold for kswapd workers
In reply to Ehab AbabnehThe per-node failure counter advances once per worker balance pass that makes no reclaim progress. With multiple kswapd workers, it can reach MAX_RECLAIM_RETRIES while others are still reclaiming, marking the node hopeless prematurely.
Scale the threshold by the active kswapd worker count so each worker keeps the existing retry budget.
--- mm/vmscan.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmscan.c +2 -1
@@ -7940,7 +7940,8 @@ void kswapd_try_clear_hopeless(struct pglist_data *pgdat,bool kswapd_test_hopeless (pg_data_t *pgdat) { -return atomic_read (&pgdat->kswapd_failures) >=MAX_RECLAIM_RETRIES ; +return atomic_read (&pgdat->kswapd_failures) >= +MAX_RECLAIM_RETRIES *READ_ONCE (current_kswapds_per_node); }#ifdef CONFIG_HIBERNATION
Signature · 1 line
2.43.0
Lorenzo Stoakes (ARM)
reviewerHi,
Please always send new revisions of series individually, not in-reply-to any other email.
The easiest way of accomplishing this is to use b4 [0] (the recommended way of sending patches to mm), otherwise format patches like this:
For a patch series, e.g. at v2:
$ git format-patch -v2 --cover-letter --thread ...
For an individual patch, e.g. at v2:
$ git format-patch -v2 HEAD~1
[0]: https://b4.docs.kernel.org/en/latest/contributor/send.html
--
Cheers, Lorenzo
Barry Song
reviewerQuoted 17 lines · Ehab Ababneh
This is a follow-up to the RFC for adaptive multi-threaded kswapd on NUMA. It handles concurrent MGLRU aging, adjusts wakeups to node CPU load, and scales the hopeless threshold for multiple workers.
Under memory pressure, an lruvec can end up with only two MGLRU generations. That leaves an older and a younger cohort, a bit like inactive and active LRU lists, though MGLRU tracks age differently.
Regarding the concerns raised in review:
- In the updated Cassandra comparison, four kswapd threads reduced p99 latency from 6.5 ms to 3.35 ms (about 49%) and increased throughput by 10.3%. We believe the latency gain comes from the reclaim design, not from changes to LRU locking.
- This newer result compares four kswapd threads with one. The earlier 7.3% throughput result was measured with eight threads.
Quoted 7 lines
We also saw periods with zero file scans. The
MIN_NR_GENSguard may
contribute: when only two generations remain, it makesscan_folios()return without scanning the oldest one. This patch tests whether letting kswapd scan that generation helps reclaim progress. The current traces do not tie the zero-scan events to the same lruvec, so this remains a hypothesis; more per-lruvec tracing is needed to compare it with lock contention.
Hi Ehab,
There is a patchset under development by Kairui, me and gaofei:
https://git.kernel.org/pub/scm/linux/kernel/git/baohua/linux.git/log/?h=kairui_scanbalance_0.3
which allows reclamation after we arrive MIN_NR_GENS.
This might fix your problem. Hopefully, we can find some time next week to send it out.
Thanks
Barry
Hi Barry, All,
Quoted 46 lines
-----Original Message-----
From: Barry Song <[email protected]>
Sent: Saturday, October 10, 2026 1:19 AM
To: Ababneh, Ehab <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v2 0/5] mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaimQuoted 17 lines · Ehab Ababneh
This is a follow-up to the RFC for adaptive multi-threaded kswapd on NUMA. It handles concurrent MGLRU aging, adjusts wakeups to node CPU load, and scales the hopeless threshold for multiple workers.
Under memory pressure, an lruvec can end up with only two MGLRU generations. That leaves an older and a younger cohort, a bit like inactive and active LRU lists, though MGLRU tracks age differently.
Regarding the concerns raised in review:
- In the updated Cassandra comparison, four kswapd threads reduced p99 latency from 6.5 ms to 3.35 ms (about 49%) and increased throughput by 10.3%. We believe the latency gain comes from the reclaim design, not from changes to LRU locking.
- This newer result compares four kswapd threads with one. The earlier 7.3% throughput result was measured with eight threads.Quoted 7 lines
We also saw periods with zero file scans. The
MIN_NR_GENSguard may
contribute: when only two generations remain, it makesscan_folios()return without scanning the oldest one. This patch tests whether letting kswapd scan that generation helps reclaim progress. The current traces do not tie the zero-scan events to the same lruvec, so this remains a hypothesis; more per-lruvec tracing is needed to compare it with lock contention.Hi Ehab,
There is a patchset under development by Kairui, me and gaofei:
Quoted 8 lines
https://git.kernel.org/pub/scm/linux/kernel/git/baohua/linux.git/log/?h=kairu
i_scanbalance_0.3which allows reclamation after we arrive
MIN_NR_GENS.This might fix your problem. Hopefully, we can find some time next week to send it out.
Thank you for the information.
I compared the two patch sets and here is a summary of the results:
Baseline: Linux v7.3-rc7 plus the revert of 6cbdd9726fb5.
My approach: Adaptive multi-threaded kswapd (4 threads/node), plus a patch that allows scanning pages when only two generations remain.
Compared against: Kairui’s scan-balance patches, based on the same baseline:
https://git.kernel.org/pub/scm/linux/kernel/git/baohua/linux.git/log/?h=kairui_scanbalance_0.3
Both benchmarks used the same 12-minute Cassandra workload with 100 client threads. Compared with the scan-balance run, my approach produced:
Throughput: 71,577 vs. 66,246 op/s, an 8.0% increase. p99 latency: 3.6 vs. 6.3 ms, a 42.9% reduction. PSI some: 41.9 vs. 109.6 s, a 61.8% reduction. PSI full: 39.0 vs. 97.9 s, a 60.2% reduction.
I also submitted an RFC related to regression introduce by 6cbdd9726fb5:
https://lore.kernel.org/linux-mm/[email protected]/
If possible, I would like to get some tips from Shakeel or Johannes.
Shakeel and Johannes, when you have a chance, I’d appreciate your thoughts on the proposed patch set.
Best,
Ehab
Quoted 2 lines
Thanks
Barry
Hi all,
I would appreciate your thoughts on how we might achieve a similar performance improvement without increasing the number of kswapd threads.
In the previous patchset, using four kswapd threads reduced Cassandra p99 latency from 6.5 ms to 3.35 ms (about 48%) and improved throughput by 10.3%. I believe this is a substantial result and worth pursuing further.
To explore alternatives, I tried increasing the reclaim target for kswapd passes (testing multipliers from 2 to 100), shortening kswapd's sleep intervals, and making it more patient before declaring a node hopeless. So far, none of these approaches has produced a comparable improvement.
I would be grateful for suggestions on other approaches worth investigating.
Thanks,
Ehab
Quoted 109 lines
-----Original Message-----
From: Ababneh, Ehab
Sent: Wednesday, October 14, 2026 12:29 AM
To: 'Barry Song' <[email protected]>; Shakeel Butt <[email protected]>; Johannes Weiner <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [RFC PATCH v2 0/5] mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaimHi Barry, All,
Quoted 15 lines
-----Original Message-----
From: Barry Song <[email protected]>
Sent: Saturday, October 10, 2026 1:19 AM
To: Ababneh, Ehab <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH v2 0/5] mm/vmscan: adaptive multi-threaded kswapd for NUMA-aware reclaimQuoted 1 line
On Sat, Oct 10, 2026 at 4:30 AM Ehab Ababneh
Quoted 30 lines
Quoted 17 lines · wrote:
This is a follow-up to the RFC for adaptive multi-threaded kswapd on NUMA. It handles concurrent MGLRU aging, adjusts wakeups to node CPU load, and scales the hopeless threshold for multiple workers.
Under memory pressure, an lruvec can end up with only two MGLRU generations. That leaves an older and a younger cohort, a bit like inactive and active LRU lists, though MGLRU tracks age differently.
Regarding the concerns raised in review:
- In the updated Cassandra comparison, four kswapd threads reduced p99 latency from 6.5 ms to 3.35 ms (about 49%) and increased throughput by 10.3%. We believe the latency gain comes from the reclaim design, not from changes to LRU locking.
- This newer result compares four kswapd threads with one. The earlier 7.3% throughput result was measured with eight threads.Quoted 7 lines
We also saw periods with zero file scans. The
MIN_NR_GENSguard may
contribute: when only two generations remain, it makesscan_folios()return without scanning the oldest one. This patch tests whether letting kswapd scan that generation helps reclaim progress. The current traces do not tie the zero-scan events to the same lruvec, so this remains a hypothesis; more per-lruvec tracing is needed to compare it with lock contention.Hi Ehab,
There is a patchset under development by Kairui, me and gaofei:
Quoted 9 lines
https://git.kernel.org/pub/scm/linux/kernel/git/baohua/linux.git/log/?
h=kairu
i_scanbalance_0.3which allows reclamation after we arrive
MIN_NR_GENS.This might fix your problem. Hopefully, we can find some time next week to send it out.
Thank you for the information.
I compared the two patch sets and here is a summary of the results:
Baseline: Linux v7.3-rc7 plus the revert of
6cbdd9726fb5.My approach: Adaptive multi-threaded kswapd (4 threads/node), plus a patch that allows scanning pages when only two generations remain.
Compared against: Kairui’s scan-balance patches, based on the same baseline:
https://git.kernel.org/pub/scm/linux/kernel/git/baohua/linux.git/log/?h=kair
ui_scanbalance_0.3Both benchmarks used the same 12-minute Cassandra workload with 100 client threads. Compared with the scan-balance run, my approach produced:
Throughput: 71,577 vs. 66,246 op/s, an 8.0% increase. p99 latency: 3.6 vs. 6.3 ms, a 42.9% reduction. PSI some: 41.9 vs. 109.6 s, a 61.8% reduction. PSI full: 39.0 vs. 97.9 s, a 60.2% reduction.
I also submitted an RFC related to regression introduce by
6cbdd9726fb5:
https://lore.kernel.org/linux-mm/20260901180704.168106-1-
[email protected]/If possible, I would like to get some tips from Shakeel or Johannes.
Shakeel and Johannes, when you have a chance, I’d appreciate your thoughts on the proposed patch set.
Best,
EhabQuoted 2 lines
Thanks
Barry
Barry Song
reviewerQuoted 11 lines · Ehab
Hi all,
I would appreciate your thoughts on how we might achieve a similar performance improvement without increasing the number of kswapd threads.
In the previous patchset, using four kswapd threads reduced Cassandra p99 latency from 6.5 ms to 3.35 ms (about 48%) and improved throughput by 10.3%. I believe this is a substantial result and worth pursuing further.
Hi Ehab,
I assume the performance improvement mainly comes from reducing direct reclaim by using four async threads to reclaim memory. Is that correct?
Quoted 6 lines
To explore alternatives, I tried increasing the reclaim target for kswapd passes (testing multipliers from 2 to 100), shortening kswapd's sleep intervals, and making it more patient before declaring a node hopeless. So far, none of these approaches has produced a comparable improvement.
So basically, one thread isn't sufficient to reclaim memory in time, causing your workloads to fall into direct reclaim frequently?
Quoted 3 lines
I would be grateful for suggestions on other approaches worth investigating.
We have various types of proactive reclaim that can run asynchronously in parallel:
1. Memcg proactive memory reclaim
2. Node proactive reclaim
3. recently, Hui Zhu even provides a bpf-based reclamation:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=48ea1ba704aa3a9
We have these approaches that allow userspace to flexibly ramp up reclaim before we have to fall back to direct reclaim.
Also, if you're using zswap or zram, compression itself could be the bottleneck.
There was a proposal for kcompressd to handle compression asynchronously, which has been on hold for a while:
https://lore.kernel.org/linux-mm/[email protected]/
Or, if aging is your bottleneck, perhaps something like async aging could help?
I'm still not sure where the performance improvement comes from. Is it mainly due to reduced direct reclaim?
Best,
Barry
Quoted 3 lines · Ehab
I would appreciate your thoughts on how we might achieve a similar performance improvement without increasing the number of kswapd threads.
It also just plain doesn't seem like folks are very interested in optimizations in this area at the moment. Feedback that's basically, "go optimize something else" would also be very welcome.
Shakeel Butt
reviewerQuoted 8 lines · Dave Hansen
Quoted 3 lines · Ehab
I would appreciate your thoughts on how we might achieve a similar performance improvement without increasing the number of kswapd threads.
It also just plain doesn't seem like folks are very interested in optimizations in this area at the moment. Feedback that's basically, "go optimize something else" would also be very welcome.
Hi Ehab and Dave,
My appologies for late response. I have not gotten the chance to look deeper into the series as I am stuck in something else but I am very much interested in this (line of) work.
I have discussed with Johannes about multithreaded kswapds but the motivation is more about scaling kswapd threads corresponding to the number of cores on the node. We are getting CPU with hundred of cores per numa code and kswapd is already too late or slow to keep up with allocations happening on all of those cores. So, I definitely think we need multiple workers doing the background reclaim.
Please give me sometime, I will organize my thoughts on how we should aim to tackle this challenge. I will also ask Johannes to provide his take on the issue.
Thanks for pushing this work.
Shakeel