Skip to contentSkip to conversation

Fix test_cgroup_iter_memcg issues found during back-porting

PATCH Superseded Hui Zhu 3 participants 10 messages Feb 12 – Feb 13

Subsystems (6)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

cover Fix test_cgroup_iter_memcg issues found during back-porting

From: Hui Zhu <[email protected]>

While back-porting "mm: bpf kfuncs to access memcg data", I encountered issues with test_cgroup_iter_memcg, specifically in test_kmem.

The test_cgroup_iter_memcg test would falsely pass when bpf_mem_cgroup_page_state() failed due to incompatible enum values across kernel versions. Additionally, test_kmem would fail on systems with cgroup.memory=nokmem enabled.

This series addresses these issues:
1. Add return value checks for bpf_mem_cgroup_page_state()
2. Return error when accessing kmem with nokmem enabled
3. Skip test_kmem when cgroup.memory=nokmem is set

Hui Zhu (3):
selftests/bpf: Check bpf_mem_cgroup_page_state return value mm/memcontrol: Return error when accessing kmem with nokmem selftests/bpf: Skip test_kmem when cgroup.memory=nokmem

 mm/memcontrol.c                               |  3 +-
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 40 +++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/3 selftests/bpf: Check bpf_mem_cgroup_page_state return value

From: Hui Zhu <[email protected]>

When back-porting test_progs to different kernel versions, I encountered an issue where the test_cgroup_iter_memcg test would falsely pass even when bpf_mem_cgroup_page_state() failed.

The problem occurs when test_progs compiled on one kernel version is executed on another kernel with different enum values for memory statistics (e.g., NR_ANON_MAPPED, NR_FILE_PAGES). In such cases, bpf_mem_cgroup_page_state() returns -1 to indicate failure, but the test didn't check for this error condition and incorrectly reported success.

This patch adds explicit checks to ensure bpf_mem_cgroup_page_state() doesn't return -1 before validating the actual statistics values. This prevents false positives when running test_progs in cross-kernel environments.

---
 .../selftests/bpf/prog_tests/cgroup_iter_memcg.c     | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +12 -0
index a5afd16705f0..13b299512429 100644
@@ -53,6 +53,8 @@ static void test_anon(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup;
 
+	ASSERT_NEQ(memcg_query->nr_anon_mapped, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state NR_ANON_MAPPED");
 	ASSERT_GT(memcg_query->nr_anon_mapped, 0, "final anon mapped val");
 
 cleanup:
@@ -88,6 +90,10 @@ static void test_file(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup_map;
 
+	ASSERT_NEQ(memcg_query->nr_file_pages, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state NR_FILE_PAGES");
+	ASSERT_NEQ(memcg_query->nr_file_mapped, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state NR_FILE_MAPPED");
 	ASSERT_GT(memcg_query->nr_file_pages, 0, "final file value");
 	ASSERT_GT(memcg_query->nr_file_mapped, 0, "final file mapped value");
 
@@ -119,6 +125,8 @@ static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup;
 
+	ASSERT_NEQ(memcg_query->nr_shmem, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state NR_SHMEM");
 	ASSERT_GT(memcg_query->nr_shmem, 0, "final shmem value");
 
 cleanup:
@@ -143,6 +151,8 @@ static void test_kmem(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup;
 
+	ASSERT_NEQ(memcg_query->memcg_kmem, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state MEMCG_KMEM");
 	ASSERT_GT(memcg_query->memcg_kmem, 0, "kmem value");
 
 cleanup:
@@ -170,6 +180,8 @@ static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup;
 
+	ASSERT_NEQ(memcg_query->pgfault, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state PGFAULT");
 	ASSERT_GT(memcg_query->pgfault, 0, "final pgfault val");
 
 cleanup:
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH 2/3 mm/memcontrol: Return error when accessing kmem with nokmem

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

When running tests on hosts with cgroup.memory=nokmem enabled for performance reasons, test_kmem always gets a value of 0 for kmem statistics.

Since BPF programs cannot easily determine whether kmem is enabled, add a check in memcg_stat_item_valid() to return an error when attempting to access MEMCG_KMEM statistics while kmem accounting is disabled via cgroup_memory_nokmem.

This prevents BPF programs from silently receiving zero values and allows them to properly handle the case where kmem accounting is unavailable.

---
 mm/memcontrol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +2 -1
index 129eed3ff5bb..4d8419623d1c 100644
@@ -667,7 +667,8 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
 
 bool memcg_stat_item_valid(int idx)
 {
-	if ((u32)idx >= MEMCG_NR_STAT)
+	if ((u32)idx >= MEMCG_NR_STAT ||
+	    (cgroup_memory_nokmem && (u32)idx == MEMCG_KMEM))
 		return false;
 
 	return !BAD_STAT_IDX(memcg_stats_index(idx));
Signature · 1 line
2.43.0
author
#8 lore raw

PATCH 3/3 selftests/bpf: Skip test_kmem when cgroup.memory=nokmem

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

When cgroup.memory=nokmem is set in kernel command line, kmem accounting is disabled and the test_kmem subtest will fail.

Add a check to skip this test when the parameter is present.

---
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +28 -0
index 13b299512429..203e6b091a21 100644
@@ -134,11 +134,39 @@ static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query)
 	shm_unlink("/tmp_shmem");
 }
 
+static bool cmdline_has(const char *arg)
+{
+	char cmdline[4096];
+	int fd;
+	ssize_t len;
+	bool ret = false;
+
+	fd = open("/proc/cmdline", O_RDONLY);
+	if (fd < 0)
+		return false;
+
+	len = read(fd, cmdline, sizeof(cmdline) - 1);
+	close(fd);
+	if (len < 0)
+		return false;
+
+	cmdline[len] = '\0';
+	if (strstr(cmdline, arg))
+		ret = true;
+
+	return ret;
+}
+
 #define NR_PIPES 64
 static void test_kmem(struct bpf_link *link, struct memcg_query *memcg_query)
 {
 	int fds[NR_PIPES][2], i;
 
+	if (cmdline_has("cgroup.memory=nokmem")) {
+		test__skip();
+		return;
+	}
+
 	/*
 	 * Increase kmem value by creating pipes which will allocate some
 	 * kernel buffers.
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH bpf-next 1/3] selftests/bpf: Check bpf_mem_cgroup_page_state return value

In reply to Hui Zhu
The assertion message references bpf_mem_cgroup_page_state(), but the BPF program actually uses bpf_mem_cgroup_vm_events() to retrieve the pgfault value:
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +0 -0

[ ... ]

tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query) · line 184

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +2 -0
@@ -170,6 +180,8 @@ static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query)
 	if (!ASSERT_OK(read_stats(link), "read stats"))
 		goto cleanup;
 
+	ASSERT_NEQ(memcg_query->pgfault, (unsigned long)-1,
+		  "bpf_mem_cgroup_page_state PGFAULT");
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

The assertion message references bpf_mem_cgroup_page_state(), but the BPF program actually uses bpf_mem_cgroup_vm_events() to retrieve the pgfault value:

tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c:

    memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, PGFAULT);

Should the message be "bpf_mem_cgroup_vm_events PGFAULT" instead?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21939182103

AI-authorship-score: low
AI-authorship-explanation: The commit message uses first-person narrative describing a real back-porting scenario, and the code changes are simple repetitive assertions typical of human-written test improvements.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor documentation issue where an assertion message references the wrong BPF function name; does not affect test correctness or system behavior.

#3 lore raw

Re: [PATCH bpf-next 1/3] selftests/bpf: Check bpf_mem_cgroup_page_state return value

2026年2月12日 16:50, [email protected] mailto:[email protected] 写到:

Quoted 18 lines
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +0 -0
 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
 +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c

[ ... ]

tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query) · line 185

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +0 -0
@@ -170,6 +180,8 @@ static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query)
 if (!ASSERT_OK(read_stats(link), "read stats"))
 goto cleanup;
 
 + ASSERT_NEQ(memcg_query->pgfault, (unsigned long)-1,
 + "bpf_mem_cgroup_page_state PGFAULT");
 

^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quoted 8 lines

The assertion message references bpf_mem_cgroup_page_state(), but the BPF program actually uses bpf_mem_cgroup_vm_events() to retrieve the pgfault value:

tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c:
memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, PGFAULT);

Should the message be "bpf_mem_cgroup_vm_events PGFAULT" instead?

Good catch! I will fix it in the next version.

Best,
Hui

Quoted 13 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21939182103

AI-authorship-score: low
AI-authorship-explanation: The commit message uses first-person narrative describing a real back-porting scenario, and the code changes are simple repetitive assertions typical of human-written test improvements.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Minor documentation issue where an assertion message references the wrong BPF function name; does not affect test correctness or system behavior.

#4 lore raw

Re: [PATCH bpf-next 1/3] selftests/bpf: Check bpf_mem_cgroup_page_state return value

In reply to Hui Zhu
Quoted 9 lines · Hui Zhu

From: Hui Zhu <[email protected]>

When back-porting test_progs to different kernel versions, I encountered an issue where the test_cgroup_iter_memcg test would falsely pass even when bpf_mem_cgroup_page_state() failed.

The problem occurs when test_progs compiled on one kernel version is executed on another kernel with different enum values for memory statistics (e.g., NR_ANON_MAPPED, NR_FILE_PAGES). [...]

This patch looks good but I think to fully solve this cross-kernel issue we should use co-re in the bpf program. In your second revision, can you add an additional patch to make use of bpf_core_enum_value()? This way instead of relying on enum values in vmlinux.h at compile-time, we use the btf info at load-time instead to get the proper value for the given kernel.

#7 lore raw

Re: [PATCH bpf-next 2/3] mm/memcontrol: Return error when accessing kmem with nokmem

In reply to Hui Zhu
Quoted 25 lines · Hui Zhu

From: Hui Zhu <[email protected]>

When running tests on hosts with cgroup.memory=nokmem enabled for performance reasons, test_kmem always gets a value of 0 for kmem statistics.

Since BPF programs cannot easily determine whether kmem is enabled, add a check in memcg_stat_item_valid() to return an error when attempting to access MEMCG_KMEM statistics while kmem accounting is disabled via cgroup_memory_nokmem.

This prevents BPF programs from silently receiving zero values and allows them to properly handle the case where kmem accounting is unavailable.

---
  mm/memcontrol.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +0 -0
index 129eed3ff5bb..4d8419623d1c 100644

mm/memcontrol.c unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) · line 672

Quoted 7 lines
mm/memcontrol.c +2 -1
@@ -667,7 +667,8 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
 
  bool memcg_stat_item_valid(int idx)
  {
-	if ((u32)idx >= MEMCG_NR_STAT)
+	if ((u32)idx >= MEMCG_NR_STAT ||
+	    (cgroup_memory_nokmem && (u32)idx == MEMCG_KMEM))
  		return false;

It's still a valid stat though, right? When it's disabled the value will just remain zero. I don't think this is necessary.

#9 lore raw

Re: [PATCH bpf-next 3/3] selftests/bpf: Skip test_kmem when cgroup.memory=nokmem

In reply to Hui Zhu
Quoted 42 lines · Hui Zhu

From: Hui Zhu <[email protected]>

When cgroup.memory=nokmem is set in kernel command line, kmem accounting is disabled and the test_kmem subtest will fail.

Add a check to skip this test when the parameter is present.

---
  .../bpf/prog_tests/cgroup_iter_memcg.c        | 28 +++++++++++++++++++
  1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +22 -0
index 13b299512429..203e6b091a21 100644
@@ -134,11 +134,39 @@ static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query)
  	shm_unlink("/tmp_shmem");
  }
 
+static bool cmdline_has(const char *arg)
+{
+	char cmdline[4096];
+	int fd;
+	ssize_t len;
+	bool ret = false;
+
+	fd = open("/proc/cmdline", O_RDONLY);
+	if (fd < 0)
+		return false;
+
+	len = read(fd, cmdline, sizeof(cmdline) - 1);
+	close(fd);
+	if (len < 0)
+		return false;
+
+	cmdline[len] = '\0';
+	if (strstr(cmdline, arg))
+		ret = true;
+
+	return ret;
+}

tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query) · line 168

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +5 -0
+
  #define NR_PIPES 64
  static void test_kmem(struct bpf_link *link, struct memcg_query *memcg_query)
  {
  	int fds[NR_PIPES][2], i;
 
+	if (cmdline_has("cgroup.memory=nokmem")) {
+		test__skip();
+		return;
+	}

Instead of just skipping what if we proceed and then confirm we get a zero value after the allocations?

#5 lore raw

Re: [PATCH bpf-next 1/3] selftests/bpf: Check bpf_mem_cgroup_page_state return value

In reply to JP Kobryn

2026年2月13日 08:14, "JP Kobryn" <[email protected] mailto:[email protected]?to=%22JP%20Kobryn%22%20%3Cinwardvessel%40gmail.com%3E > 写到:

Quoted 12 lines · Hui Zhu
Quoted 10 lines

From: Hui Zhu <[email protected]>
When back-porting test_progs to different kernel versions, I encountered an issue where the test_cgroup_iter_memcg test would falsely pass even when bpf_mem_cgroup_page_state() failed.
The problem occurs when test_progs compiled on one kernel version is executed on another kernel with different enum values for memory statistics (e.g., NR_ANON_MAPPED, NR_FILE_PAGES). [...]

Quoted 7 lines

This patch looks good but I think to fully solve this cross-kernel issue we should use co-re in the bpf program. In your second revision, can you add an additional patch to make use of bpf_core_enum_value()? This way instead of relying on enum values in vmlinux.h at compile-time, we use the btf info at load-time instead to get the proper value for the given kernel.

I post v2 according to your comments.
Thanks for your review.

Best,
Hui