Skip to contentSkip to conversation

Fix test_cgroup_iter_memcg issues found during back-porting

PATCH Superseded Hui Zhu 3 participants 9 messages Feb 28

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

v3 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.

These patches are my fixes for the problems I encountered.

Changelog:
v3:
According to the comments of JP Kobryn, remove kmem subtest from cgroup_iter_memcg and fix assertion string in test_pgfault. v2:
According to the comments of JP Kobryn, added bpf_core_enum_value() usage in the BPF program to handle cross-kernel enum value differences at load-time instead of compile-time.
Dropped the mm/memcontrol.c patch.
Modified test_kmem handling: instead of skipping when nokmem is set, verify that kmem value is zero as expected.
According to the comments of bot, fixed assertion message: changed "bpf_mem_cgroup_page_state" to "bpf_mem_cgroup_vm_events" for PGFAULT check.

Hui Zhu (3):
selftests/bpf: Remove kmem subtest from cgroup_iter_memcg
bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg selftests/bpf: Check bpf_mem_cgroup_page_state return value

 .../testing/selftests/bpf/cgroup_iter_memcg.h |  2 -
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 38 +++++--------------
 .../selftests/bpf/progs/cgroup_iter_memcg.c   | 18 ++++++---
 3 files changed, 22 insertions(+), 36 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/3 selftests/bpf: Remove kmem subtest from cgroup_iter_memcg

From: Hui Zhu <[email protected]>

When cgroup.memory=nokmem is set in the kernel command line, kmem accounting is disabled. This causes the test_kmem subtest in cgroup_iter_memcg to fail because it expects non-zero kmem values.

Remove the kmem subtest altogether since the remaining subtests (shmem, file, pgfault) already provide sufficient coverage for the cgroup iter memcg functionality.

---
 .../testing/selftests/bpf/cgroup_iter_memcg.h |  2 --
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 28 -------------------
 .../selftests/bpf/progs/cgroup_iter_memcg.c   |  1 -
 3 files changed, 31 deletions(-)
tools/testing/selftests/bpf/cgroup_iter_memcg.h +0 -2
index 3f59b127943b..ff20ec537164 100644
@@ -9,8 +9,6 @@ struct memcg_query {
 	unsigned long nr_shmem;
 	unsigned long nr_file_pages;
 	unsigned long nr_file_mapped;
-	/* some memcg_stat_item */
-	unsigned long memcg_kmem;
 	/* some vm_event_item */
 	unsigned long pgfault;
 };
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +0 -28
index a5afd16705f0..88fc3e83d2b7 100644
@@ -126,32 +126,6 @@ static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query)
 	shm_unlink("/tmp_shmem");
 }
 
-#define NR_PIPES 64
-static void test_kmem(struct bpf_link *link, struct memcg_query *memcg_query)
-{
-	int fds[NR_PIPES][2], i;
-
-	/*
-	 * Increase kmem value by creating pipes which will allocate some
-	 * kernel buffers.
-	 */
-	for (i = 0; i < NR_PIPES; i++) {
-		if (!ASSERT_OK(pipe(fds[i]), "pipe"))
-			goto cleanup;
-	}
-
-	if (!ASSERT_OK(read_stats(link), "read stats"))
-		goto cleanup;
-
-	ASSERT_GT(memcg_query->memcg_kmem, 0, "kmem value");
-
-cleanup:
-	for (i = i - 1; i >= 0; i--) {
-		close(fds[i][0]);
-		close(fds[i][1]);
-	}
-}
-
 static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query)
 {
 	void *map;
@@ -209,8 +183,6 @@ void test_cgroup_iter_memcg(void)
 		test_shmem(link, &skel->data_query->memcg_query);
 	if (test__start_subtest("cgroup_iter_memcg__file"))
 		test_file(link, &skel->data_query->memcg_query);
-	if (test__start_subtest("cgroup_iter_memcg__kmem"))
-		test_kmem(link, &skel->data_query->memcg_query);
 	if (test__start_subtest("cgroup_iter_memcg__pgfault"))
 		test_pgfault(link, &skel->data_query->memcg_query);
 
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c +0 -1
index 59fb70a3cc50..12f79a44133e 100644
@@ -30,7 +30,6 @@ int cgroup_memcg_query(struct bpf_iter__cgroup *ctx)
 	memcg_query.nr_shmem = bpf_mem_cgroup_page_state(memcg, NR_SHMEM);
 	memcg_query.nr_file_pages = bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES);
 	memcg_query.nr_file_mapped = bpf_mem_cgroup_page_state(memcg, NR_FILE_MAPPED);
-	memcg_query.memcg_kmem = bpf_mem_cgroup_page_state(memcg, MEMCG_KMEM);
 	memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, PGFAULT);
 
 	bpf_put_mem_cgroup(memcg);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/3 bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

Replace hardcoded enum values with bpf_core_enum_value() calls in cgroup_iter_memcg test to improve portability across different kernel versions.

The change adds runtime enum value resolution for:
- node_stat_item: NR_ANON_MAPPED, NR_SHMEM, NR_FILE_PAGES, NR_FILE_MAPPED
- memcg_stat_item: MEMCG_KMEM
- vm_event_item: PGFAULT

This ensures the BPF program can adapt to enum value changes between kernel versions.

---
 .../selftests/bpf/progs/cgroup_iter_memcg.c     | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c +12 -5
index 12f79a44133e..06a385c9d85b 100644
@@ -26,11 +26,18 @@ int cgroup_memcg_query(struct bpf_iter__cgroup *ctx)
 
 	bpf_mem_cgroup_flush_stats(memcg);
 
-	memcg_query.nr_anon_mapped = bpf_mem_cgroup_page_state(memcg, NR_ANON_MAPPED);
-	memcg_query.nr_shmem = bpf_mem_cgroup_page_state(memcg, NR_SHMEM);
-	memcg_query.nr_file_pages = bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES);
-	memcg_query.nr_file_mapped = bpf_mem_cgroup_page_state(memcg, NR_FILE_MAPPED);
-	memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, PGFAULT);
+	memcg_query.nr_anon_mapped = bpf_mem_cgroup_page_state(
+		memcg,
+		bpf_core_enum_value(enum node_stat_item, NR_ANON_MAPPED));
+	memcg_query.nr_shmem = bpf_mem_cgroup_page_state(
+		memcg, bpf_core_enum_value(enum node_stat_item, NR_SHMEM));
+	memcg_query.nr_file_pages = bpf_mem_cgroup_page_state(
+		memcg, bpf_core_enum_value(enum node_stat_item, NR_FILE_PAGES));
+	memcg_query.nr_file_mapped = bpf_mem_cgroup_page_state(
+		memcg,
+		bpf_core_enum_value(enum node_stat_item, NR_FILE_MAPPED));
+	memcg_query.pgfault = bpf_mem_cgroup_vm_events(
+		memcg, bpf_core_enum_value(enum vm_event_item, PGFAULT));
 
 	bpf_put_mem_cgroup(memcg);
 
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH bpf-next v3 2/3] bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg

The commit message lists "memcg_stat_item: MEMCG_KMEM" but this patch does not touch MEMCG_KMEM at all. The prior patch in the series (fa0721ccce66 "selftests/bpf: Remove kmem subtest from…
Quoted 5 lines

The change adds runtime enum value resolution for:
- node_stat_item: NR_ANON_MAPPED, NR_SHMEM, NR_FILE_PAGES, NR_FILE_MAPPED
- memcg_stat_item: MEMCG_KMEM
- vm_event_item: PGFAULT

The commit message lists "memcg_stat_item: MEMCG_KMEM" but this patch does not touch MEMCG_KMEM at all. The prior patch in the series (fa0721ccce66 "selftests/bpf: Remove kmem subtest from cgroup_iter_memcg") already removed the MEMCG_KMEM usage entirely.

Should this bullet be dropped from the commit message?

---
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/22516077164

AI-authorship-score: low
AI-authorship-explanation: The stale MEMCG_KMEM reference left in the commit message after restructuring the series is characteristic of a human forgetting to update text.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Commit message lists a MEMCG_KMEM change that does not exist in the patch, causing a mismatch between the commit description and the actual diff.

reviewer Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH bpf-next v3 2/3] bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg

In reply to Hui Zhu
Quoted 12 lines · Hui Zhu

From: Hui Zhu <[email protected]>

Replace hardcoded enum values with bpf_core_enum_value() calls in cgroup_iter_memcg test to improve portability across different kernel versions.

The change adds runtime enum value resolution for:
- node_stat_item: NR_ANON_MAPPED, NR_SHMEM, NR_FILE_PAGES, NR_FILE_MAPPED
- memcg_stat_item: MEMCG_KMEM
- vm_event_item: PGFAULT

Quoted 4 lines

This ensures the BPF program can adapt to enum value changes between kernel versions.

Bot's comments aside:

Quoted 32 lines
---
 .../selftests/bpf/progs/cgroup_iter_memcg.c     | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c +12 -5
index 12f79a44133e..06a385c9d85b 100644
@@ -26,11 +26,18 @@ int cgroup_memcg_query(struct bpf_iter__cgroup *ctx)
 
 	bpf_mem_cgroup_flush_stats(memcg);
 
-	memcg_query.nr_anon_mapped = bpf_mem_cgroup_page_state(memcg, NR_ANON_MAPPED);
-	memcg_query.nr_shmem = bpf_mem_cgroup_page_state(memcg, NR_SHMEM);
-	memcg_query.nr_file_pages = bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES);
-	memcg_query.nr_file_mapped = bpf_mem_cgroup_page_state(memcg, NR_FILE_MAPPED);
-	memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, PGFAULT);
+	memcg_query.nr_anon_mapped = bpf_mem_cgroup_page_state(
+		memcg,
+		bpf_core_enum_value(enum node_stat_item, NR_ANON_MAPPED));
+	memcg_query.nr_shmem = bpf_mem_cgroup_page_state(
+		memcg, bpf_core_enum_value(enum node_stat_item, NR_SHMEM));
+	memcg_query.nr_file_pages = bpf_mem_cgroup_page_state(
+		memcg, bpf_core_enum_value(enum node_stat_item, NR_FILE_PAGES));
+	memcg_query.nr_file_mapped = bpf_mem_cgroup_page_state(
+		memcg,
+		bpf_core_enum_value(enum node_stat_item, NR_FILE_MAPPED));
+	memcg_query.pgfault = bpf_mem_cgroup_vm_events(
+		memcg, bpf_core_enum_value(enum vm_event_item, PGFAULT));
 
 	bpf_put_mem_cgroup(memcg);
 
author
#5 lore raw

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

In reply to 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.

This patch adds explicit checks to ensure bpf_mem_cgroup_page_state() doesn't return -1 before validating the actual statistics values.

---
 .../selftests/bpf/prog_tests/cgroup_iter_memcg.c       | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +10 -0
index 88fc3e83d2b7..9eadfbd3fdb9 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:
@@ -144,6 +152,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_vm_events PGFAULT");
 	ASSERT_GT(memcg_query->pgfault, 0, "final pgfault val");
 
 cleanup:
Signature · 1 line
2.43.0
#6 lore raw

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

Quoted 10 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.

This patch adds explicit checks to ensure bpf_mem_cgroup_page_state() doesn't return -1 before validating the actual statistics values.

I'm a bit confused, what is the exact scenario where we read -1 for these stats? AFAICT bpf_mem_cgroup_page_state has to return -1 for them, and it turn this is possible if the idx we're passing is not valid - but the ones we do always should be.

Quoted 46 lines
---
 .../selftests/bpf/prog_tests/cgroup_iter_memcg.c       | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c +10 -0
index 88fc3e83d2b7..9eadfbd3fdb9 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:
@@ -144,6 +152,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_vm_events PGFAULT");
 	ASSERT_GT(memcg_query->pgfault, 0, "final pgfault val");
 
 cleanup:
#7 lore raw
In reply to Hui Zhu
Quoted 8 lines · Hui Zhu

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.

These patches are my fixes for the problems I encountered.

Nit: Could you be a bit more descriptive about the series' contents? Cover letters normally focus on what what the patch does - for example the issues being found during backporting is not pertinent to the patchset itself, because the issues are still present on bpf-next.

Quoted 24 lines

Changelog:
v3:
According to the comments of JP Kobryn, remove kmem subtest from cgroup_iter_memcg and fix assertion string in test_pgfault. v2:
According to the comments of JP Kobryn, added bpf_core_enum_value() usage in the BPF program to handle cross-kernel enum value differences at load-time instead of compile-time.
Dropped the mm/memcontrol.c patch.
Modified test_kmem handling: instead of skipping when nokmem is set, verify that kmem value is zero as expected.
According to the comments of bot, fixed assertion message: changed "bpf_mem_cgroup_page_state" to "bpf_mem_cgroup_vm_events" for PGFAULT check.

Hui Zhu (3):
selftests/bpf: Remove kmem subtest from cgroup_iter_memcg
bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg selftests/bpf: Check bpf_mem_cgroup_page_state return value

 .../testing/selftests/bpf/cgroup_iter_memcg.h |  2 -
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 38 +++++--------------
 .../selftests/bpf/progs/cgroup_iter_memcg.c   | 18 ++++++---
 3 files changed, 22 insertions(+), 36 deletions(-)
#8 lore raw
Quoted 7 lines · Emil Tsalapatis
Quoted 6 lines · Hui Zhu

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.

Quoted 8 lines
Quoted 2 lines

These patches are my fixes for the problems I encountered.

Nit: Could you be a bit more descriptive about the series' contents? Cover letters normally focus on what what the patch does - for example the issues being found during backporting is not pertinent to the patchset itself, because the issues are still present on bpf-next.

Also, responded on v3 instead of v4 sorry about that. Comments and tags still apply since the only delta is in the commit message.

Quoted 24 lines
Quoted 24 lines

Changelog:
v3:
According to the comments of JP Kobryn, remove kmem subtest from cgroup_iter_memcg and fix assertion string in test_pgfault. v2:
According to the comments of JP Kobryn, added bpf_core_enum_value() usage in the BPF program to handle cross-kernel enum value differences at load-time instead of compile-time.
Dropped the mm/memcontrol.c patch.
Modified test_kmem handling: instead of skipping when nokmem is set, verify that kmem value is zero as expected.
According to the comments of bot, fixed assertion message: changed "bpf_mem_cgroup_page_state" to "bpf_mem_cgroup_vm_events" for PGFAULT check.

Hui Zhu (3):
selftests/bpf: Remove kmem subtest from cgroup_iter_memcg
bpf: Use bpf_core_enum_value for stats in cgroup_iter_memcg selftests/bpf: Check bpf_mem_cgroup_page_state return value

 .../testing/selftests/bpf/cgroup_iter_memcg.h |  2 -
 .../bpf/prog_tests/cgroup_iter_memcg.c        | 38 +++++--------------
 .../selftests/bpf/progs/cgroup_iter_memcg.c   | 18 ++++++---
 3 files changed, 22 insertions(+), 36 deletions(-)