* [PATCH 1/2] kselftests: cgroup: update kmem test tolerance for multi-memcg stock
2026-04-14 11:05 [PATCH 0/2] kselftests: cgroup: fix test_kmem false failures ranxiaokai627
@ 2026-04-14 11:05 ` ranxiaokai627
2026-04-14 11:05 ` [PATCH 2/2] kselftests: cgroup: account for slab memory in test_percpu_basic ranxiaokai627
1 sibling, 0 replies; 3+ messages in thread
From: ranxiaokai627 @ 2026-04-14 11:05 UTC (permalink / raw)
To: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, tj,
mkoutny, shuah, kuba, hughd, akpm
Cc: cgroups, linux-mm, linux-kselftest, linux-kernel, ran.xiaokai,
ranxiaokai627
From: Ran Xiaokai <ran.xiaokai@zte.com.cn>
Commit f735eebe55f8 ("memcg: multi-memcg percpu charge cache") changed
the percpu charge cache to support multiple memory cgroups
(NR_MEMCG_STOCK) instead of a single memcg per CPU.
Prior to the multi-memcg stock change, the tolerance was calculated as:
PAGE_SIZE * MEMCG_CHARGE_BATCH * num_cpus
With NR_MEMCG_STOCK slots per CPU, the worst-case discrepancy is now:
PAGE_SIZE * MEMCG_CHARGE_BATCH * NR_MEMCG_STOCK * num_cpus
Update the test tolerance to include the NR_MEMCG_STOCK factor to
prevent false positive test failures.
Fixes: f735eebe55f8 ("memcg: multi-memcg percpu charge cache")
Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
tools/testing/selftests/cgroup/test_kmem.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c
index eeabd34bf083..15b8bb424cb5 100644
--- a/tools/testing/selftests/cgroup/test_kmem.c
+++ b/tools/testing/selftests/cgroup/test_kmem.c
@@ -19,12 +19,19 @@
/*
- * Memory cgroup charging is performed using percpu batches 64 pages
- * big (look at MEMCG_CHARGE_BATCH), whereas memory.stat is exact. So
- * the maximum discrepancy between charge and vmstat entries is number
- * of cpus multiplied by 64 pages.
+ * Memory cgroup charging is performed using per-CPU batches to reduce
+ * accounting overhead. Each cache slot can hold up to MEMCG_CHARGE_BATCH
+ * pages for a specific memcg. The per-CPU charge cache supports multiple
+ * memcgs simultaneously (NR_MEMCG_STOCK slots).
+ *
+ * While memory.stat reports exact usage, per-CPU charges are pending
+ * until flushed. Therefore, the maximum discrepancy between charge and
+ * vmstat entries is:
+ *
+ * PAGE_SIZE * MEMCG_CHARGE_BATCH * NR_MEMCG_STOCK * num_cpus
*/
-#define MAX_VMSTAT_ERROR (4096 * 64 * get_nprocs())
+#define NR_MEMCG_STOCK 7
+#define MAX_VMSTAT_ERROR (4096 * 64 * NR_MEMCG_STOCK * get_nprocs())
#define KMEM_DEAD_WAIT_RETRIES 80
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH 2/2] kselftests: cgroup: account for slab memory in test_percpu_basic
2026-04-14 11:05 [PATCH 0/2] kselftests: cgroup: fix test_kmem false failures ranxiaokai627
2026-04-14 11:05 ` [PATCH 1/2] kselftests: cgroup: update kmem test tolerance for multi-memcg stock ranxiaokai627
@ 2026-04-14 11:05 ` ranxiaokai627
1 sibling, 0 replies; 3+ messages in thread
From: ranxiaokai627 @ 2026-04-14 11:05 UTC (permalink / raw)
To: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, tj,
mkoutny, shuah, kuba, hughd, akpm
Cc: cgroups, linux-mm, linux-kselftest, linux-kernel, ran.xiaokai,
ranxiaokai627
From: Ran Xiaokai <ran.xiaokai@zte.com.cn>
The test verifies memory.current approximates memory.stat.percpu within
a tolerance. However, memory.current includes slab overhead. On systems
with few CPUs(<= 4), slab consumption exceeds percpu usage. While percpu
usage grows linearly and dominates as CPU count increases, the
significant slab portion on such few CPU systems causes the difference
to exceed MAX_VMSTAT_ERROR, leading to false test failures.
Fix this by including slab memory in the calculation.
Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
tools/testing/selftests/cgroup/test_kmem.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c
index 15b8bb424cb5..263aedb0727a 100644
--- a/tools/testing/selftests/cgroup/test_kmem.c
+++ b/tools/testing/selftests/cgroup/test_kmem.c
@@ -360,7 +360,7 @@ static int test_percpu_basic(const char *root)
{
int ret = KSFT_FAIL;
char *parent, *child;
- long current, percpu;
+ long current, percpu, slab;
int i;
parent = cg_name(root, "percpu_basic_test");
@@ -386,8 +386,9 @@ static int test_percpu_basic(const char *root)
current = cg_read_long(parent, "memory.current");
percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
+ slab = cg_read_key_long(parent, "memory.stat", "slab ");
- if (current > 0 && percpu > 0 && labs(current - percpu) <
+ if (current > 0 && percpu > 0 && labs(current - percpu - slab) <
MAX_VMSTAT_ERROR)
ret = KSFT_PASS;
else
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread