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

Hui Zhu posted 3 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH bpf-next 2/3] mm/memcontrol: Return error when accessing kmem with nokmem
Posted by Hui Zhu 1 month, 2 weeks ago
From: Hui Zhu <zhuhui@kylinos.cn>

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 <zhuhui@kylinos.cn>
---
 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
Re: [PATCH bpf-next 2/3] mm/memcontrol: Return error when accessing kmem with nokmem
Posted by JP Kobryn (Meta) 1 month, 2 weeks ago
On 2/12/26 12:23 AM, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@kylinos.cn>
> 
> 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 <zhuhui@kylinos.cn>
> ---
>   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;

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.