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. Signed-off-by: Hui Zhu <[email protected]> --- mm/memcontrol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 129eed3ff5bb..4d8419623d1c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -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)); -- 2.43.0

