selftests: memcg: Fix test_memcontrol test failures with large page sizes
Subsystems (4)
cover selftests: memcg: Fix test_memcontrol test failures with large page sizes
There are a number of test failures with the running of the test_memcontrol selftest on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. This patch series makes some minor changes to the kernel and the test_memcontrol selftest to address these failures.
The first kernel patch scales the memcg vmstats flush threshold logarithmetically instead of linearly with the total number of CPUs. The second kernel patch scale down MEMCG_CHARGE_BATCH with increases in page size. These 2 patches help to reduce the discrepancies between the reported usage data with the real ones.
The next 5 test_memcontrol selftest patches adjust the testing code to greatly reduce the chance that it will report failure, though some occasional failures is still possible.
To verify the changes, the test_memcontrol selftest was run 100 times each on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. No failure was observed other than some failures of the test_memcg_reclaim test when running on a 16k page size kernel. The reclaim_until() call failed because of the unexpected over-reclaim of memory. This will need a further look but it happens with the 16k page size kernel only and I don't have a production ready kernel config file to use in buildinig this 16k page size kernel. The new test_memcontrol selftest and kernel were also run on a 96-core x86 system to make sure there was no regression.
Waiman Long (7):
memcg: Scale up vmstats flush threshold with log2(nums_possible_cpus)
memcg: Scale down MEMCG_CHARGE_BATCH with increase in PAGE_SIZE
selftests: memcg: Iterate pages based on the actual page size
selftests: memcg: Increase error tolerance in accordance with page
size
selftests: memcg: Reduce the expected swap.peak with larger page size
selftests: memcg: Don't call reclaim_until() if already in target
selftests: memcg: Treat failure for zeroing sock in test_memcg_sock as
XFAIL
include/linux/memcontrol.h | 8 +- mm/memcontrol.c | 17 ++-- .../cgroup/lib/include/cgroup_util.h | 1 + .../selftests/cgroup/test_memcontrol.c | 83 +++++++++++++++---- 4 files changed, 87 insertions(+), 22 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/7 memcg: Scale up vmstats flush threshold with log2(nums_possible_cpus)
The vmstats flush threshold currently increases linearly with the number of online CPUs. As the number of CPUs increases over time, it will become increasingly difficult to meet the threshold and update the vmstats data in a timely manner. These days, systems with hundreds of CPUs or even thousands of them are becoming more common.
For example, the test_memcg_sock test of test_memcontrol always fails when running on an arm64 system with 128 CPUs. It is because the threshold is now 64*128 = 8192. With 4k page size, it needs changes in 32 MB of memory. It will be even worse with larger page size like 64k.
To make the output of memory.stat more correct, it is better to scale up the threshold logarithmically instead of linearly with the number of CPUs. With the log2 scale, we can use the possibly larger num_possible_cpus() instead of num_online_cpus() which may change at run time.
Although there is supposed to be a periodic and asynchronous flush of vmstats every 2 seconds, the actual time lag between succesive runs can actually vary quite a bit. In fact, I have seen time lags of up to 10s of seconds in some cases. So we couldn't too rely on the hope that there will be an asynchronous vmstats flush every 2 seconds. This may be something we need to look into.
--- mm/memcontrol.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
mm/memcontrol.c +12 -5
@@ -548,20 +548,20 @@ struct memcg_vmstats {* rstat update tree grow unbounded. * * 2) Flush the stats synchronously on reader side only when there are more than -* (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization -* will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but -* only for 2 seconds due to (1). +* (MEMCG_CHARGE_BATCH * (ilog2(nr_cpus) + 1)) update events. Though this +* optimization will let stats be out of sync by up to that amount but only +* for 2 seconds due to (1). */ static void flush_memcg_stats_dwork (struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);static u64 flush_last_time; +static int vmstats_flush_threshold__ro_after_init ;#define FLUSH_TIME (2UL*HZ)static bool memcg_vmstats_needs_flush (struct memcg_vmstats *vmstats) { -return atomic_read (&vmstats->stats_updates) > -MEMCG_CHARGE_BATCH *num_online_cpus (); +return atomic_read (&vmstats->stats_updates) > vmstats_flush_threshold; } static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val,
@@ -5191,6 +5191,13 @@ int __init mem_cgroup_init(void) memcg_pn_cachep =KMEM_CACHE (mem_cgroup_per_node,SLAB_PANIC |SLAB_HWCACHE_ALIGN ); +/* +* Logarithmically scale up vmstats flush threshold with the number +* of CPUs. +* N.B. ilog2(1) = 0. +*/ + vmstats_flush_threshold =MEMCG_CHARGE_BATCH * + (ilog2 (num_possible_cpus ()) +1 );return 0 ; }
Signature · 1 line
2.53.0
PATCH 2/7 memcg: Scale down MEMCG_CHARGE_BATCH with increase in PAGE_SIZE
In reply to Waiman LongFor a system with 4k page size, each percpu memcg_stock can hide up to 256 kbytes of memory with the current MEMCG_CHARGE_BATCH value of
64. For another system with 64k page size, that becomes 4 Mbytes. This MEMCG_CHARGE_BATCH value also controls how often should the memcg vmstat values need flushing. As a result, the values reported in various memory cgroup control files are even less indicative of the actual memory consumption of a particular memory cgroup when the page size increases from 4k.
This problem can be illustrated by running the test_memcontrol selftest. Running a 4k page size kernel on a 128-core arm64 system, the test_memcg_current_peak test which allocates a 50M anonymous memory passed. With a 64k page size kernel on the same system, however, the same test failed because the "anon" attribute of memory.stat file might report a size of 0 depending on the number of CPUs the system has.
To solve this inaccurate memory stats problem, we need to scale down the amount of memory that can be hidden by reducing MEMCG_CHARGE_BATCH when the page size increases. The same user application will likely consume more memory on systems with larger page size and it is also less efficient if we scale down MEMCG_CHARGE_BATCH by too much. So I believe a good compromise is to scale down MEMCG_CHARGE_BATCH by 2 for 16k page size and by 4 with 64k page size.
With that change, the test_memcg_current_peak test passed again with the modified 64k page size kernel.
--- include/linux/memcontrol.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/memcontrol.h +7 -1
@@ -328,8 +328,14 @@ struct mem_cgroup {* size of first charge trial. * TODO: maybe necessary to use big numbers in big irons or dynamic based of the * workload. +* +* There are 3 common base page sizes - 4k, 16k & 64k. In order to limit the +* amount of memory that can be hidden in each percpu memcg_stock for a given +* memcg, we scale down MEMCG_CHARGE_BATCH by 2 for 16k and 4 for 64k. */ -#define MEMCG_CHARGE_BATCH 64U +#define MEMCG_CHARGE_BATCH_BASE 64U +#define MEMCG_CHARGE_BATCH_SHIFT ((PAGE_SHIFT <= 16) ? (PAGE_SHIFT - 12)/2 : 2) +#define MEMCG_CHARGE_BATCH (MEMCG_CHARGE_BATCH_BASE >> MEMCG_CHARGE_BATCH_SHIFT)extern struct mem_cgroup *root_mem_cgroup;
Signature · 1 line
2.53.0
PATCH 3/7 selftests: memcg: Iterate pages based on the actual page size
In reply to Waiman LongThe current test_memcontrol test fault in memory by write a value to the start of a page based on the default value of 4k page size. Micro-optimize it by using the actual system page size to do the iteration.
--- tools/testing/selftests/cgroup/test_memcontrol.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/test_memcontrol.c +6 -4
@@ -25,6 +25,7 @@static bool has_localevents;static bool has_recursiveprot; +static int page_size;int get_temp_fd (void ) {
@@ -60,7 +61,7 @@ int alloc_anon(const char *cgroup, void *arg)char *buf, *ptr; buf =malloc (size); -for (ptr = buf; ptr < buf + size; ptr +=PAGE_SIZE ) +for (ptr = buf; ptr < buf + size; ptr += page_size) *ptr =0 ;free (buf);
@@ -183,7 +184,7 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg)return -1 ; } -for (ptr = buf; ptr < buf + size; ptr +=PAGE_SIZE ) +for (ptr = buf; ptr < buf + size; ptr += page_size) *ptr =0 ; current =cg_read_long (cgroup,"memory.current" );
@@ -413,7 +414,7 @@ static int alloc_anon_noexit(const char *cgroup, void *arg)return -1 ; } -for (ptr = buf; ptr < buf + size; ptr +=PAGE_SIZE ) +for (ptr = buf; ptr < buf + size; ptr += page_size) *ptr =0 ;while (getppid () == ppid)
@@ -999,7 +1000,7 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)return -1 ; } -for (ptr = buf; ptr < buf + size; ptr +=PAGE_SIZE ) +for (ptr = buf; ptr < buf + size; ptr += page_size) *ptr =0 ; mem_current =cg_read_long (cgroup,"memory.current" );
@@ -1679,6 +1680,7 @@ int main(int argc, char **argv)char root[PATH_MAX ];int i, proc_status; + page_size =sysconf (_SC_PAGE_SIZE);ksft_print_header ();ksft_set_plan (ARRAY_SIZE (tests));if (cg_find_unified_root (root,sizeof (root),NULL ))
Signature · 1 line
2.53.0
PATCH 4/7 selftests: memcg: Increase error tolerance in accordance with page size
In reply to Waiman LongIt was found that some of the tests in test_memcontrol can fail more readily if system page size is larger than 4k. It is because the actual memory.current value deviates more from the expected value with larger page size. To avoid this failure, the error tolerance is now increased in accordance to the current system page size value. The page size scale factor is set to 2 for 64k page and 1 for 16k page.
Changes are made in alloc_pagecache_max_30M(), test_memcg_protection() and alloc_anon_50M_check_swap() to increase the error tolerance for memory.current for larger page size. The current set of values are chosen to ensure that the relevant test_memcontrol tests no longer have any test failure in a 100 repeated run of test_memcontrol with a 4k/16k/64k page size kernels on an arm64 system.
--- .../cgroup/lib/include/cgroup_util.h | 1 + .../selftests/cgroup/test_memcontrol.c | 23 ++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-)
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +1 -0
@@ -6,6 +6,7 @@#define PAGE_SIZE 4096#endif +#define KB (x) (x << 10)#define MB (x) (x << 20)#define USEC_PER_SEC 1000000L
tools/testing/selftests/cgroup/test_memcontrol.c +17 -6
@@ -26,6 +26,7 @@static bool has_localevents;static bool has_recursiveprot;static int page_size; +static int pscale_factor;/* Page size scale factor */ int get_temp_fd (void ) {
@@ -571,16 +572,17 @@ static int test_memcg_protection(const char *root, bool min)if (cg_run (parent[2 ], alloc_anon, (void *)MB (148 ))) goto cleanup; -if (!values_close (cg_read_long (parent[1 ],"memory.current" ),MB (50 ),3 )) +if (!values_close (cg_read_long (parent[1 ],"memory.current" ),MB (50 ), +3 + (min ?0 :4 ) * pscale_factor)) goto cleanup;for (i =0 ; i <ARRAY_SIZE (children); i++) c[i] =cg_read_long (children[i],"memory.current" ); -if (!values_close (c[0 ],MB (29 ),15 )) +if (!values_close (c[0 ],MB (29 ),15 +3 * pscale_factor)) goto cleanup; -if (!values_close (c[1 ],MB (21 ),20 )) +if (!values_close (c[1 ],MB (21 ),20 + pscale_factor)) goto cleanup;if (c[3 ] !=0 )
@@ -596,7 +598,8 @@ static int test_memcg_protection(const char *root, bool min) } current = min ?MB (50 ) :MB (30 ); -if (!values_close (cg_read_long (parent[1 ],"memory.current" ), current,3 )) +if (!values_close (cg_read_long (parent[1 ],"memory.current" ), current, +9 + (min ?0 :6 ) * pscale_factor)) goto cleanup;if (!reclaim_until (children[0 ],MB (10 )))
@@ -684,7 +687,7 @@ static int alloc_pagecache_max_30M(const char *cgroup, void *arg) goto cleanup; current =cg_read_long (cgroup,"memory.current" ); -if (!values_close (current,MB (30 ),5 )) +if (!values_close (current,MB (30 ),5 + (pscale_factor ?2 :0 ))) goto cleanup; ret =0 ;
@@ -1004,7 +1007,7 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) *ptr =0 ; mem_current =cg_read_long (cgroup,"memory.current" ); -if (!mem_current || !values_close (mem_current, mem_max,3 )) +if (!mem_current || !values_close (mem_current, mem_max,6 + pscale_factor)) goto cleanup; swap_current =cg_read_long (cgroup,"memory.swap.current" );
@@ -1681,6 +1684,14 @@ int main(int argc, char **argv)int i, proc_status; page_size =sysconf (_SC_PAGE_SIZE); +/* +* It is found that the actual memory.current value can deviate more +* from the expected value with larger page size. So error tolerance +* will have to be increased a bit more for larger page size. +*/ +if (page_size >KB (4 )) + pscale_factor = (page_size >=KB (64 )) ?2 :1 ; +ksft_print_header ();ksft_set_plan (ARRAY_SIZE (tests));if (cg_find_unified_root (root,sizeof (root),NULL ))
Signature · 1 line
2.53.0
PATCH 5/7 selftests: memcg: Reduce the expected swap.peak with larger page size
In reply to Waiman LongWhen running the test_memcg_swap_max_peak test which sets swap.max to 30M on an arm64 system with 64k page size, the test failed as the swap.peak could only reach up only to 27,328,512 bytes (about 25.45 MB which is lower than the expected 29M) before the allocating task got oom-killed.
It is likely due to the fact that it takes longer to write out a larger page to swap and hence a lower swap.peak is being reached. Setting memory.high to 29M to throttle memory allocation when nearing memory.max helps, but it still could only reach up to 29,032,448 bytes (about 27.04M). As a result, we have to reduce the expected swap.peak with larger page size. Now swap.peak is expected to reach only 27M with 64k page, 29M with 4k page and 28M with 16k page.
--- .../selftests/cgroup/test_memcontrol.c | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/test_memcontrol.c +22 -4
@@ -1032,6 +1032,7 @@ static int test_memcg_swap_max_peak(const char *root)char *memcg;long max, peak;struct stat ss; +long swap_peak;int swap_peak_fd =-1 , mem_peak_fd =-1 ;/* any non-empty string resets */
@@ -1119,6 +1120,23 @@ static int test_memcg_swap_max_peak(const char *root)if (cg_write (memcg,"memory.max" ,"30M" )) goto cleanup; +/* +* The swap.peak that can be reached will depend on the system page +* size. With larger page size (e.g. 64k), it takes more time to write +* the anonymous memory page to swap and so the peak reached will be +* lower before the memory allocation process get oom-killed. One way +* to allow the swap.peak to go higher is to throttle memory allocation +* by setting memory.high to, say, 29M to give more time to swap out the +* memory before oom-kill. This is still not enough for it to reach +* 29M reachable with 4k page. So we still need to reduce the expected +* swap.peak accordingly. +*/ + swap_peak = (page_size ==KB (4 )) ?MB (29 ) : + ((page_size <=KB (16 )) ?MB (28 ) :MB (27 )); + +if (cg_write (memcg,"memory.high" ,"29M" )) + goto cleanup; +/* Should be killed by OOM killer */ if (!cg_run (memcg, alloc_anon, (void *)MB (100 ))) goto cleanup;
@@ -1134,7 +1152,7 @@ static int test_memcg_swap_max_peak(const char *root) goto cleanup; peak =cg_read_long (memcg,"memory.swap.peak" ); -if (peak <MB (29 )) +if (peak < swap_peak) goto cleanup; peak =cg_read_long_fd (mem_peak_fd);
@@ -1142,7 +1160,7 @@ static int test_memcg_swap_max_peak(const char *root) goto cleanup; peak =cg_read_long_fd (swap_peak_fd); -if (peak <MB (29 )) +if (peak < swap_peak) goto cleanup;/*
@@ -1181,7 +1199,7 @@ static int test_memcg_swap_max_peak(const char *root)if (cg_read_long (memcg,"memory.peak" ) <MB (29 )) goto cleanup; -if (cg_read_long (memcg,"memory.swap.peak" ) <MB (29 )) +if (cg_read_long (memcg,"memory.swap.peak" ) < swap_peak) goto cleanup;if (cg_run (memcg, alloc_anon_50M_check_swap, (void *)MB (30 )))
@@ -1196,7 +1214,7 @@ static int test_memcg_swap_max_peak(const char *root) goto cleanup; peak =cg_read_long (memcg,"memory.swap.peak" ); -if (peak <MB (29 )) +if (peak < swap_peak) goto cleanup; peak =cg_read_long_fd (mem_peak_fd);
Signature · 1 line
2.53.0
PATCH 6/7 selftests: memcg: Don't call reclaim_until() if already in target
In reply to Waiman LongNear the end of test_memcg_protection(), reclaim_until() is called to reduce memory.current of children[0] to 10M. It was found that with larger page size (e.g. 64k) the various memory cgroups in test_memcg_protection() would deviate further from the expected values especially for the test_memcg_low test. As a result, children[0] might have reached the target already without reclamation. The will cause the reclaim_until() function to report failure as no reclamation is needed.
Avoid this unexpected failure by skipping the reclaim_until() call if memory.current of children[0] has already reached the target size for kernel with non-4k page size.
--- tools/testing/selftests/cgroup/test_memcontrol.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/test_memcontrol.c +10 -1
@@ -490,6 +490,7 @@ static int test_memcg_protection(const char *root, bool min)long current;int i, attempts;int fd; +bool do_reclaim; fd =get_temp_fd ();if (fd <0 )
@@ -602,7 +603,15 @@ static int test_memcg_protection(const char *root, bool min) 9 + (min ? 0 : 6) * pscale_factor)) goto cleanup; -if (!reclaim_until (children[0 ],MB (10 ))) +/* +* With larger page size, it is possible that memory.current of +* children[0] is close to 10M. Skip the reclaim_until() call if +* that is the case. +*/ + current =cg_read_long (children[0 ],"memory.current" ); + do_reclaim = (page_size ==KB (4 )) || + ((current >MB (10 )) && !values_close (current,MB (10 ),3 )); +if (do_reclaim && !reclaim_until (children[0 ],MB (10 ))) goto cleanup;if (min) {
Signature · 1 line
2.53.0
PATCH 7/7 selftests: memcg: Treat failure for zeroing sock in test_memcg_sock as XFAIL
In reply to Waiman LongAlthough there is supposed to be a periodic and asynchronous flush of stats every 2 seconds, the actual time lag between succesive runs can actually vary quite a bit. In fact, I have seen time lag of up to 10s of seconds in some cases.
At the end of test_memcg_sock, it waits up to 3 seconds for the "sock" attribute of memory.stat to go back down to 0. Obviously it may occasionally fail especially when the kernel has large page size (e.g. 64k). Treat this failure as an expected failure (XFAIL) to distinguish it from the other failure cases.
--- tools/testing/selftests/cgroup/test_memcontrol.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/test_memcontrol.c +12 -1
@@ -1486,12 +1486,20 @@ static int test_memcg_sock(const char *root)* Poll memory.stat for up to 3 seconds (~FLUSH_TIME plus some * scheduling slack) and require that the "sock " counter * eventually drops to zero. +* +* The actual run-to-run elapse time between consecutive run +* of asynchronous memcg rstat flush may varies quite a bit. +* So the 3 seconds wait time may not be enough for the "sock" +* counter to go down to 0. Treat it as a XFAIL instead of +* a FAIL. */ sock_post =cg_read_key_long_poll (memcg,"memory.stat" ,"sock " ,0 ,MEMCG_SOCKSTAT_WAIT_RETRIES ,DEFAULT_WAIT_INTERVAL_US ); -if (sock_post) +if (sock_post) { + ret =KSFT_XFAIL ; goto cleanup; + } ret =KSFT_PASS ;
@@ -1753,6 +1761,9 @@ int main(int argc, char **argv)case KSFT_SKIP :ksft_test_result_skip ("%s\n" , tests[i].name);break ; +case KSFT_XFAIL : +ksft_test_result_xfail ("%s\n" , tests[i].name); +break ;default :ksft_test_result_fail ("%s\n" , tests[i].name);break ;
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 15 lines · On Thu, 19 Mar 2026 13:37:45 -0400 Waiman Long <[email protected]> wrote:
There are a number of test failures with the running of the
test_memcontrolselftest on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. This patch series makes some minor changes to the kernel and thetest_memcontrolselftest to address these failures.The first kernel patch scales the memcg vmstats flush threshold logarithmetically instead of linearly with the total number of CPUs. The second kernel patch scale down
MEMCG_CHARGE_BATCHwith increases in page size. These 2 patches help to reduce the discrepancies between the reported usage data with the real ones.The next 5
test_memcontrolselftest patches adjust the testing code to greatly reduce the chance that it will report failure, though some occasional failures is still possible.
Quoted 10 lines
To verify the changes, the
test_memcontrolselftest was run 100 times each on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. No failure was observed other than some failures of thetest_memcg_reclaimtest when running on a 16k page size kernel. Thereclaim_until()call failed because of the unexpected over-reclaim of memory. This will need a further look but it happens with the 16k page size kernel only and I don't have a production ready kernel config file to use in buildinig this 16k page size kernel. The newtest_memcontrolselftest and kernel were also run on a 96-core x86 system to make sure there was no regression.
AI reviewbot asks questions:
https://sashiko.dev/#/patchset/20260319173752.1472864-1-longman%40redhat.com
Re: [PATCH 1/7] memcg: Scale up vmstats flush threshold with log2(nums_possible_cpus)
In reply to Waiman LongQuoted 60 lines · Waiman Long
The vmstats flush threshold currently increases linearly with the number of online CPUs. As the number of CPUs increases over time, it will become increasingly difficult to meet the threshold and update the vmstats data in a timely manner. These days, systems with hundreds of CPUs or even thousands of them are becoming more common.
For example, the
test_memcg_socktest oftest_memcontrolalways fails when running on an arm64 system with 128 CPUs. It is because the threshold is now 64*128 = 8192. With 4k page size, it needs changes in 32 MB of memory. It will be even worse with larger page size like 64k.To make the output of memory.stat more correct, it is better to scale up the threshold logarithmically instead of linearly with the number of CPUs. With the log2 scale, we can use the possibly larger
num_possible_cpus()instead ofnum_online_cpus()which may change at run time.Although there is supposed to be a periodic and asynchronous flush of vmstats every 2 seconds, the actual time lag between succesive runs can actually vary quite a bit. In fact, I have seen time lags of up to 10s of seconds in some cases. So we couldn't too rely on the hope that there will be an asynchronous vmstats flush every 2 seconds. This may be something we need to look into.
Signed-off-by Waiman Long <[email protected]>--- mm/memcontrol.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)mm/memcontrol.c +5
-5@@ -548,20 +548,20 @@ struct memcg_vmstats {* rstat update tree grow unbounded. * * 2) Flush the stats synchronously on reader side only when there are more than -* (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization -* will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but -* only for 2 seconds due to (1). +* (MEMCG_CHARGE_BATCH * (ilog2(nr_cpus) + 1)) update events. Though this +* optimization will let stats be out of sync by up to that amount but only +* for 2 seconds due to (1). */ static void flush_memcg_stats_dwork (struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);static u64 flush_last_time; +static int vmstats_flush_threshold__ro_after_init ;#define FLUSH_TIME (2UL*HZ)static bool memcg_vmstats_needs_flush (struct memcg_vmstats *vmstats) { -return atomic_read (&vmstats->stats_updates) > -MEMCG_CHARGE_BATCH *num_online_cpus (); +return atomic_read (&vmstats->stats_updates) > vmstats_flush_threshold; } static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val,
mm/memcontrol.c int __init mem_cgroup_init(void) · line 5200
Quoted 10 lines
mm/memcontrol.c +7
-0@@ -5191,6 +5191,13 @@ int __init mem_cgroup_init(void) memcg_pn_cachep =KMEM_CACHE (mem_cgroup_per_node,SLAB_PANIC |SLAB_HWCACHE_ALIGN ); +/* +* Logarithmically scale up vmstats flush threshold with the number +* of CPUs. +* N.B. ilog2(1) = 0. +*/ + vmstats_flush_threshold =MEMCG_CHARGE_BATCH * + (ilog2 (num_possible_cpus ()) +1 );
Changing the threashold from linearly to logarithmically looks smarter, but my concern is that, on large systems (hundreds/thousands of CPUs), the threshold drops dramatically.
For example, 1024 CPUs it goes from 65536 (256MB) to only 704 (2.7MB), that's almost 100x. Could this potentially raise a performance issue as frequently read 'memory.stat' on a heavily loaded system?
Maybe go with MEMCG_CHARGE_BATCH * int_sqrt(num_possible_cpus()), which sits between linear and log2?
Signature · 2 lines
Regards, Li Wang
Re: [PATCH 2/7] memcg: Scale down MEMCG_CHARGE_BATCH with increase in PAGE_SIZE
In reply to Waiman LongQuoted 5 lines · Waiman Long
include/linux/memcontrol.h +0
-0@@ -328,8 +328,14 @@ struct mem_cgroup {* size of first charge trial. * TODO: maybe necessary to use big numbers in big irons or dynamic based of the
include/linux/memcontrol.h struct mem_cgroup { · line 338
Quoted 10 lines
include/linux/memcontrol.h +7
-1* workload. +* +* There are 3 common base page sizes - 4k, 16k & 64k. In order to limit the +* amount of memory that can be hidden in each percpu memcg_stock for a given +* memcg, we scale down MEMCG_CHARGE_BATCH by 2 for 16k and 4 for 64k. */ -#define MEMCG_CHARGE_BATCH 64U +#define MEMCG_CHARGE_BATCH_BASE 64U +#define MEMCG_CHARGE_BATCH_SHIFT ((PAGE_SHIFT <= 16) ? (PAGE_SHIFT - 12)/2 : 2) +#define MEMCG_CHARGE_BATCH (MEMCG_CHARGE_BATCH_BASE >> MEMCG_CHARGE_BATCH_SHIFT)
This is a good complement to the first patch. With this change, I got a chart to compare the three methods (linear, log2, sqrt) in the count threshold:
4k page size (BATCH=64):
CPUs linear log2 sqrt
--------------------------------
1 256KB 256KB 256KB 8 2MB 1MB 512KB 128 32MB 2MB 2.75MB 1024 256MB 2.75MB 8MB
64k page size (BATCH=16):
CPUs linear log2 sqrt
-------------------------------
1 1MB 1MB 1MB 8 8MB 4MB 2MB 128 128MB 8MB 11MB 1024 1GB 11MB 32MB
Both are huge improvements.
log2 flushes more aggressively on large systems, which gives more accurate stats but at the cost of more frequent synchronous flushes.
sqrt is more conservative, still a massive reduction from linear but gives more breathing room on large systems, which may be better for performance.
I would leave this choice to you, Waiman, and the data is for reference.
Signature · 2 lines
Regards, Li Wang
Re: [PATCH 3/7] selftests: memcg: Iterate pages based on the actual page size
In reply to Waiman LongQuoted 6 lines · Waiman Long
The current
test_memcontroltest fault in memory by write a value to the start of a page based on the default value of 4k page size. Micro-optimize it by using the actual system page size to do the iteration.Signed-off-by Waiman Long <[email protected]>
Signature · 2 lines
Regards, Li Wang
Re: [PATCH 1/7] memcg: Scale up vmstats flush threshold with log2(nums_possible_cpus)
In reply to Li WangQuoted 75 lines · Li Wang
Quoted 60 lines · Waiman Long
The vmstats flush threshold currently increases linearly with the number of online CPUs. As the number of CPUs increases over time, it will become increasingly difficult to meet the threshold and update the vmstats data in a timely manner. These days, systems with hundreds of CPUs or even thousands of them are becoming more common.
For example, the
test_memcg_socktest oftest_memcontrolalways fails when running on an arm64 system with 128 CPUs. It is because the threshold is now 64*128 = 8192. With 4k page size, it needs changes in 32 MB of memory. It will be even worse with larger page size like 64k.To make the output of memory.stat more correct, it is better to scale up the threshold logarithmically instead of linearly with the number of CPUs. With the log2 scale, we can use the possibly larger
num_possible_cpus()instead ofnum_online_cpus()which may change at run time.Although there is supposed to be a periodic and asynchronous flush of vmstats every 2 seconds, the actual time lag between succesive runs can actually vary quite a bit. In fact, I have seen time lags of up to 10s of seconds in some cases. So we couldn't too rely on the hope that there will be an asynchronous vmstats flush every 2 seconds. This may be something we need to look into.
Signed-off-by Waiman Long <[email protected]>--- mm/memcontrol.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)mm/memcontrol.c +5
-5@@ -548,20 +548,20 @@ struct memcg_vmstats {* rstat update tree grow unbounded. * * 2) Flush the stats synchronously on reader side only when there are more than -* (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization -* will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but -* only for 2 seconds due to (1). +* (MEMCG_CHARGE_BATCH * (ilog2(nr_cpus) + 1)) update events. Though this +* optimization will let stats be out of sync by up to that amount but only +* for 2 seconds due to (1). */ static void flush_memcg_stats_dwork (struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);static u64 flush_last_time; +static int vmstats_flush_threshold__ro_after_init ;#define FLUSH_TIME (2UL*HZ)static bool memcg_vmstats_needs_flush (struct memcg_vmstats *vmstats) { -return atomic_read (&vmstats->stats_updates) > -MEMCG_CHARGE_BATCH *num_online_cpus (); +return atomic_read (&vmstats->stats_updates) > vmstats_flush_threshold; } static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val,mm/memcontrol.c int __init mem_cgroup_init(void) · line 5200
Quoted 10 lines
mm/memcontrol.c +7
-0@@ -5191,6 +5191,13 @@ int __init mem_cgroup_init(void) memcg_pn_cachep =KMEM_CACHE (mem_cgroup_per_node,SLAB_PANIC |SLAB_HWCACHE_ALIGN ); +/* +* Logarithmically scale up vmstats flush threshold with the number +* of CPUs. +* N.B. ilog2(1) = 0. +*/ + vmstats_flush_threshold =MEMCG_CHARGE_BATCH * + (ilog2 (num_possible_cpus ()) +1 );Changing the threashold from linearly to logarithmically looks smarter, but my concern is that, on large systems (hundreds/thousands of CPUs), the threshold drops dramatically.
Quoted 6 lines
For example, 1024 CPUs it goes from 65536 (256MB) to only 704 (2.7MB), that's almost 100x. Could this potentially raise a performance issue as frequently read 'memory.stat' on a heavily loaded system?
Maybe go with
MEMCG_CHARGE_BATCH* int_sqrt(num_possible_cpus()), which sits between linear and log2?
I have also been thinking about scaling faster than log2 but still below linear. I believe int_sqrt() is a good suggestion and I will adopt it in the next version.
Thanks,
Longman
Re: [PATCH 2/7] memcg: Scale down MEMCG_CHARGE_BATCH with increase in PAGE_SIZE
In reply to Li WangQuoted 42 lines · Li Wang
Quoted 5 lines · Waiman Long
include/linux/memcontrol.h +0
-0@@ -328,8 +328,14 @@ struct mem_cgroup {* size of first charge trial. * TODO: maybe necessary to use big numbers in big irons or dynamic based of the include/linux/memcontrol.h struct mem_cgroup { · line 338
Quoted 10 lines
include/linux/memcontrol.h +7
-1* workload. +* +* There are 3 common base page sizes - 4k, 16k & 64k. In order to limit the +* amount of memory that can be hidden in each percpu memcg_stock for a given +* memcg, we scale down MEMCG_CHARGE_BATCH by 2 for 16k and 4 for 64k. */ -#define MEMCG_CHARGE_BATCH 64U +#define MEMCG_CHARGE_BATCH_BASE 64U +#define MEMCG_CHARGE_BATCH_SHIFT ((PAGE_SHIFT <= 16) ? (PAGE_SHIFT - 12)/2 : 2) +#define MEMCG_CHARGE_BATCH (MEMCG_CHARGE_BATCH_BASE >> MEMCG_CHARGE_BATCH_SHIFT)This is a good complement to the first patch. With this change, I got a chart to compare the three methods (linear, log2, sqrt) in the count threshold:
4k page size (BATCH=64):
CPUs linear log2 sqrt -------------------------------- 1 256KB 256KB 256KB 8 2MB 1MB 512KB 128 32MB 2MB 2.75MB 1024 256MB 2.75MB 8MB64k page size (BATCH=16):
CPUs linear log2 sqrt
-------------------------------1 1MB 1MB 1MB 8 8MB 4MB 2MB 128 128MB 8MB 11MB 1024 1GB 11MB 32MBBoth are huge improvements.
Quoted 8 lines
log2 flushes more aggressively on large systems, which gives more accurate stats but at the cost of more frequent synchronous flushes.
sqrt is more conservative, still a massive reduction from linear but gives more breathing room on large systems, which may be better for performance.
I would leave this choice to you, Waiman, and the data is for reference.
I think it is a good idea to use the int_sqrt() function and I will use it in the next version.
Cheers,
Longman
Quoted 45 lines · Andrew Morton
Quoted 15 lines · On Thu, 19 Mar 2026 13:37:45 -0400 Waiman Long <[email protected]> wrote:
There are a number of test failures with the running of the
test_memcontrolselftest on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. This patch series makes some minor changes to the kernel and thetest_memcontrolselftest to address these failures.The first kernel patch scales the memcg vmstats flush threshold logarithmetically instead of linearly with the total number of CPUs. The second kernel patch scale down
MEMCG_CHARGE_BATCHwith increases in page size. These 2 patches help to reduce the discrepancies between the reported usage data with the real ones.The next 5
test_memcontrolselftest patches adjust the testing code to greatly reduce the chance that it will report failure, though some occasional failures is still possible.Quoted 10 lines
To verify the changes, the
test_memcontrolselftest was run 100 times each on a 128-core arm64 system on kernels with 4k/16k/64k page sizes. No failure was observed other than some failures of thetest_memcg_reclaimtest when running on a 16k page size kernel. Thereclaim_until()call failed because of the unexpected over-reclaim of memory. This will need a further look but it happens with the 16k page size kernel only and I don't have a production ready kernel config file to use in buildinig this 16k page size kernel. The newtest_memcontrolselftest and kernel were also run on a 96-core x86 system to make sure there was no regression.AI reviewbot asks questions:
https://sashiko.dev/#/patchset/20260319173752.1472864-1-longman%40redhat.commm/memcontrol.c struct memcg_vmstats { · line 553
Quoted 13 lines
mm/memcontrol.c +3
-3@@ -548,20 +548,20 @@ struct memcg_vmstats { * rstat update tree grow unbounded. * * 2) Flush the stats synchronously on reader side only when there are more than - * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization - * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but - * only for 2 seconds due to (1). + * (MEMCG_CHARGE_BATCH * (ilog2(nr_cpus) + 1)) update events. Though this + * optimization will let stats be out of sync by up to that amount but only + * for 2 seconds due to (1).Is this description accurate regarding the maximum out of sync amount? Looking at
memcg_rstat_updated(), updates are buffered locally on each CPU
Quoted 8 lines
up to
MEMCG_CHARGE_BATCH- 1 before they are added to the globalvmstats->stats_updatescounter.
Becausememcg_vmstats_needs_flush()only checks the global counter, could N CPUs each bufferMEMCG_CHARGE_BATCH- 1 updates without triggering a synchronous flush?
If so, wouldn't the actual worst-case out-of-sync error be
N * (MEMCG_CHARGE_BATCH- 1) +vmstats_flush_threshold, which remains linear with the number of CPUs rather than scaling logarithmically?
Good point, the worst case scenario can indeed be worse than that. I will update the comment accordingly.
Quoted 7 lines
Quoted 7 lines
mm/memcontrol.c +3
-0@@ -328,8 +328,14 @@ struct mem_cgroup { * size of first charge trial. * TODO: maybe necessary to use big numbers in big irons or dynamic based of the * workload. + * + * There are 3 common base page sizes - 4k, 16k & 64k. In order to limit the + * amount of memory that can be hidden in each percpu memcg_stock for a given
Quoted 8 lines
mm/memcontrol.c struct mem_cgroup { · line 338
Quoted 6 lines
mm/memcontrol.c +4
-1+* memcg, we scale down MEMCG_CHARGE_BATCH by 2 for 16k and 4 for 64k. */ -#define MEMCG_CHARGE_BATCH 64U +#define MEMCG_CHARGE_BATCH_BASE 64U +#define MEMCG_CHARGE_BATCH_SHIFT ((PAGE_SHIFT <= 16) ? (PAGE_SHIFT - 12)/2 : 2) +#define MEMCG_CHARGE_BATCH (MEMCG_CHARGE_BATCH_BASE >> MEMCG_CHARGE_BATCH_SHIFT)Will this cause false failures in the cgroup selftests on systems with larger page sizes?
Exactly, Li Wang had posted a patch [1] to address this issue. We will have to update this code again if this patch is adopted to reduce the expected MAX_VMSTAT_ERROR accordingly.
[1] https://lore.kernel.org/lkml/[email protected]/
Quoted 20 lines
In
tools/testing/selftests/cgroup/test_kmem.c,MAX_VMSTAT_ERRORis hardcoded assuming a 4KB page size and a 64-page charge batch (256KB per CPU):
#defineMAX_VMSTAT_ERROR(4096 * 64 *get_nprocs())
...
if (labs(sum - current) <MAX_VMSTAT_ERROR) {
...
With this change, a 64KB page system will have a batch size of 16. The theoretical per-CPU error becomes 16 pages * 64KB = 1024KB per CPU, which is 4 times larger than the hardcodedMAX_VMSTAT_ERROR.
DoesMAX_VMSTAT_ERRORintest_kmem.cneed to be updated to compute the error using the runtime system page size and the adjusted batch limit?Quoted 8 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +0
-0@@ -6,6 +6,7 @@ #define PAGE_SIZE 4096 #endif
Quoted 4 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h line 9
Quoted 1 line
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +1
-0+#define KB (x) (x << 10)This isn't a bug, but could this macro definition risk operator precedence issues if invoked with an expression? Should it be defined with inner parentheses around the argument, like #define KB(x) ((x) << 10)?
So far, only a single integer value is used for x. So the current macro is still OK. Yes, I do agree that adding parenthesis will be more safe for future use cases.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +1
-1@@ -1181,7 +1199,7 @@ static int test_memcg_swap_max_peak(const char *root) if (cg_read_long(memcg, "memory.peak") < MB(29)) goto cleanup; - if (cg_read_long(memcg, "memory.swap.peak") < MB(29)) + if (cg_read_long(memcg, "memory.swap.peak") < swap_peak) goto cleanup;
Quoted 11 lines
Quoted 2 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +0
-0if (cg_run(memcg, alloc_anon_50M_check_swap, (void *)MB(30)))goto cleanup;
Does leaving memory.high set to 29M alter the semantics of the subsequent
alloc_anon_50M_check_swaptest?
Whenalloc_anon_50M_check_swapruns, it allocates 50MB and expects memory.current to be close to the 30MB memory.max limit.
Because memory.high is still set to 29MB from the earlier operation, the 50MB allocation gets throttled at 29MB and enters synchronous reclaim before reaching the 30MB memory.max limit.
Could this inadvertently change the second test from verifying swap behavior under a hard limit to verifying it under a soft limit?
The purpose of setting memory.high to 29M is to slow down the memory allocation process in order to enable the swapping code to swap out more anonymous memory before the OOM killer comes in and kill the process. Otherwise, the actual swap out value will be even lower with larger page size. I can drop setting memory.high and set the threshold even lower in order to avoid expected failures.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +6
-1@@ -1477,12 +1477,20 @@ static int test_memcg_sock(const char *root)* Poll memory.stat for up to 3 seconds (~FLUSH_TIME plus some * scheduling slack) and require that the "sock " counter * eventually drops to zero. +* +* The actual run-to-run elapse time between consecutive run +* of asynchronous memcg rstat flush may varies quite a bit. +* So the 3 seconds wait time may not be enough for the "sock" +* counter to go down to 0. Treat it as a XFAIL instead of +* a FAIL. */ sock_post = cg_read_key_long_poll(memcg, "memory.stat", "sock ", 0, MEMCG_SOCKSTAT_WAIT_RETRIES, DEFAULT_WAIT_INTERVAL_US); - if (sock_post)
Quoted 8 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h static int test_memcg_sock(const char *root) · line 1493
Quoted 4 lines
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +3
-0+ if (sock_post) { + ret = KSFT_XFAIL; goto cleanup; + }Does this code inadvertently mask actual system or I/O errors? If
cg_read_key_long_poll()returns -1 because it failed to read the file or the key is missing, this check will treat it as an expected timeout failure rather than an actual test failure.
I will add a positive value check before setting KSFT_XFAIL.
Quoted 7 lines
Does marking this condition as
KSFT_XFAILprevent the test from catching genuine socket memory leaks?
If a kernel regression causes socket memory to actually leak, the test will time out and report an expected failure, which CI systems might ignore. Would it be more robust to increase the polling timeout to accommodate the maximum latency observed, or manually trigger a synchronous flush, instead of masking the timeout?
We may have to increase the timeout excessively in order to allow for the possible variations of the asynchronous vmstats flush delay. That may make the test take too long to run. In my own test, the current code will fail rather frequently without this change.
I do suggest that we will have to look into this issue and we can remove this expected failure if the issue is fixed.
Cheers,
Longman
Quoted 25 lines · Waiman Long
Quoted 7 lines
mm/memcontrol.c struct memcg_vmstats { · line 550
Quoted 7 lines
mm/memcontrol.c +0
-0@@ -548,20 +548,20 @@ struct memcg_vmstats { * rstat update tree grow unbounded. * * 2) Flush the stats synchronously on reader side only when thereare more than
Quoted 1 line
mm/memcontrol.c struct memcg_vmstats { · line 551
Quoted 1 line
mm/memcontrol.c +0
-1- * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though thisoptimization
Quoted 1 line
mm/memcontrol.c struct memcg_vmstats { · line 552
Quoted 1 line
mm/memcontrol.c +0
-1- * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH*
nr_cpus) butQuoted 2 lines
mm/memcontrol.c struct memcg_vmstats { · line 551
Quoted 2 lines
mm/memcontrol.c +1
-1- * only for 2 seconds due to (1). + * (MEMCG_CHARGE_BATCH * (ilog2(nr_cpus) + 1)) update events.Though this
Quoted 1 line
mm/memcontrol.c struct memcg_vmstats { · line 552
Quoted 1 line
mm/memcontrol.c +1
-0+ * optimization will let stats be out of sync by up to thatamount but only
Quoted 3 lines
mm/memcontrol.c struct memcg_vmstats { · line 553
Quoted 1 line
mm/memcontrol.c +1
-0+ * for 2 seconds due to (1).Is this description accurate regarding the maximum out of sync amount? Looking at
memcg_rstat_updated(), updates are buffered locally oneach CPU
Quoted 3 lines
up to
MEMCG_CHARGE_BATCH- 1 before they are added to the globalvmstats->stats_updatescounter.
Becausememcg_vmstats_needs_flush()only checks the global counter,could
Quoted 8 lines
Quoted 5 lines
N CPUs each buffer
MEMCG_CHARGE_BATCH- 1 updates without triggering a synchronous flush?
If so, wouldn't the actual worst-case out-of-sync error be N * (MEMCG_CHARGE_BATCH- 1) +vmstats_flush_threshold, which remains linear with the number of CPUs rather than scaling logarithmically?Good point, the worst case scenario can indeed be worse than that. I will update the comment accordingly.
Looking at the code again, the hidden charge in memcg_stock should only affect memory.current, not memory.stat. There is nothing to add to the worst case situation.
Cheers,
Longman