linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] kselftests: cgroup: fix test_kmem false failures
@ 2026-04-14 11:05 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 ` [PATCH 2/2] kselftests: cgroup: account for slab memory in test_percpu_basic ranxiaokai627
  0 siblings, 2 replies; 5+ 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>

This patchset fixes two issues in testing/selftests/cgroup/test_kmem.c
that cause false test failures under certain system configurations.

Patch 1/2 updates the MAX_VMSTAT_ERROR tolerance to account for the
multi-memcg percpu charge cache introduced by commit f735eebe55f8
("memcg: multi-memcg percpu charge cache") with NR_MEMCG_STOCK
(currently 7) slots per CPU, the worst-case discrepancy between
memory.current and memory.stat.percpu has increased.

Patch 2/2 fixes the test_percpu_basic test to account for slab memory
overhead. On systems with few CPUs (<= 4), slab consumption exceeds
percpu usage, causing the test to fail even when the percpu accounting
is working correctly.

Ran Xiaokai (2):
  kselftests: cgroup: update kmem test tolerance for multi-memcg stock
  kselftests: cgroup: account for slab memory in test_percpu_basic

 tools/testing/selftests/cgroup/test_kmem.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

-- 
2.25.1




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [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-15  9:53   ` Michal Koutný
  2026-04-14 11:05 ` [PATCH 2/2] kselftests: cgroup: account for slab memory in test_percpu_basic ranxiaokai627
  1 sibling, 1 reply; 5+ 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] 5+ 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; 5+ 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] 5+ messages in thread

* Re: [PATCH 1/2] kselftests: cgroup: update kmem test tolerance for multi-memcg stock
  2026-04-14 11:05 ` [PATCH 1/2] kselftests: cgroup: update kmem test tolerance for multi-memcg stock ranxiaokai627
@ 2026-04-15  9:53   ` Michal Koutný
  2026-04-15 11:03     ` ranxiaokai627
  0 siblings, 1 reply; 5+ messages in thread
From: Michal Koutný @ 2026-04-15  9:53 UTC (permalink / raw)
  To: ranxiaokai627
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, tj,
	shuah, kuba, hughd, akpm, cgroups, linux-mm, linux-kselftest,
	linux-kernel, ran.xiaokai

[-- Attachment #1: Type: text/plain, Size: 947 bytes --]

Hello Xiaokai.

On Tue, Apr 14, 2026 at 11:05:23AM +0000, ranxiaokai627@163.com wrote:
> Fixes: f735eebe55f8 ("memcg: multi-memcg percpu charge cache")

An interesting catch.

> -#define MAX_VMSTAT_ERROR (4096 * 64 * get_nprocs())
> +#define NR_MEMCG_STOCK 7
> +#define MAX_VMSTAT_ERROR (4096 * 64 * NR_MEMCG_STOCK * get_nprocs())

When you touch this, I think this could be factored into it too:

+#define MAX_VMSTAT_ERROR (sysconf(_SC_PAGESIZE) * 64 * NR_MEMCG_STOCK * get_nprocs())

And given how much the selftest depends in this implementation
detail(?), I see that there are other selftests that include directly
from the tree, I'd suggest also
#include "../../../../include/linux/memcontrol.h"

and use the constant from there (i.e. move NR_MEMCG_STOCK to there too).

That should make the selftest more flexible, resilient to future changes
and it'd document ramification of these constants too.

Thanks,
Michal

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kselftests: cgroup: update kmem test tolerance for multi-memcg stock
  2026-04-15  9:53   ` Michal Koutný
@ 2026-04-15 11:03     ` ranxiaokai627
  0 siblings, 0 replies; 5+ messages in thread
From: ranxiaokai627 @ 2026-04-15 11:03 UTC (permalink / raw)
  To: mkoutny
  Cc: akpm, cgroups, hannes, hughd, kuba, linux-kernel,
	linux-kselftest, linux-mm, mhocko, muchun.song, ran.xiaokai,
	ranxiaokai627, roman.gushchin, shakeel.butt, shuah, tj

>Hello Xiaokai.
>
>On Tue, Apr 14, 2026 at 11:05:23AM +0000, ranxiaokai627@163.com wrote:
>> Fixes: f735eebe55f8 ("memcg: multi-memcg percpu charge cache")
>
>An interesting catch.
>
>> -#define MAX_VMSTAT_ERROR (4096 * 64 * get_nprocs())
>> +#define NR_MEMCG_STOCK 7
>> +#define MAX_VMSTAT_ERROR (4096 * 64 * NR_MEMCG_STOCK * get_nprocs())
>
>When you touch this, I think this could be factored into it too:
>
>+#define MAX_VMSTAT_ERROR (sysconf(_SC_PAGESIZE) * 64 * NR_MEMCG_STOCK * get_nprocs())

Thanks for the review, yes, this will improve the test' portability
across architectures with different page sizes.

>And given how much the selftest depends in this implementation
>detail(?), I see that there are other selftests that include directly
>from the tree, I'd suggest also
>#include "../../../../include/linux/memcontrol.h"
>
>and use the constant from there (i.e. move NR_MEMCG_STOCK to there too).
>
>That should make the selftest more flexible, resilient to future changes
>and it'd document ramification of these constants too.

Agreed. Including memcontrol.h ensures the test stays in sync with
kernel changes.

>Thanks,
>Michal



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-04-15 11:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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-15  9:53   ` Michal Koutný
2026-04-15 11:03     ` ranxiaokai627
2026-04-14 11:05 ` [PATCH 2/2] kselftests: cgroup: account for slab memory in test_percpu_basic ranxiaokai627

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox