From: Hui Zhu <zhuhui@kylinos.cn>
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.
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
---
.../selftests/bpf/prog_tests/cgroup_iter_memcg.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c b/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
index a5afd16705f0..897b17b58df3 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
@@ -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_vm_events 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:
--
2.43.0
> diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c b/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
> --- a/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
> +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
[ ... ]
> @@ -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_vm_events MEMCG_KMEM");
Should this assertion string say "bpf_mem_cgroup_page_state MEMCG_KMEM"
instead? The BPF program in progs/cgroup_iter_memcg.c populates
memcg_kmem using bpf_mem_cgroup_page_state(), not
bpf_mem_cgroup_vm_events():
idx = bpf_core_enum_value(enum memcg_stat_item, MEMCG_KMEM);
...
memcg_query.memcg_kmem = bpf_mem_cgroup_page_state(memcg, idx);
> @@ -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");
Similarly, should this say "bpf_mem_cgroup_vm_events PGFAULT"? The BPF
program retrieves pgfault using bpf_mem_cgroup_vm_events():
idx = bpf_core_enum_value(enum vm_event_item, PGFAULT);
...
memcg_query.pgfault = bpf_mem_cgroup_vm_events(memcg, idx);
It looks like the two assertion description strings for test_kmem and
test_pgfault were swapped.
---
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/21978734425
AI-authorship-score: low
AI-authorship-explanation: The commit describes a specific back-porting scenario, and the copy-paste error in swapped assertion strings is characteristic of human authorship.
issues-found: 2
issue-severity-score: low
issue-severity-explanation: Two assertion description strings are swapped between test_kmem and test_pgfault, producing misleading diagnostics on test failure but not affecting test correctness or system stability.
© 2016 - 2026 Red Hat, Inc.