Re: [PATCH bpf-next v3 4/6] mm: introduce BPF kfuncs to access memcg statistics and events

From: Alexei Starovoitov

Date: Mon Dec 22 2025 - 21:11:32 EST


On Mon, Dec 22, 2025 at 12:18 PM Roman Gushchin
<roman.gushchin@xxxxxxxxx> wrote:
>
> + if (idx < 0 || idx >= MEMCG_NR_STAT || !memcg_stat_item_valid(idx))
> + return (unsigned long)-1;

memcg_stat_item_valid() and memcg_stat_item_valid()
helpers introduced specifically to be used in these kfuncs,
so I feel it's cleaner to do all idx checking within them
instead of splitting the checks like this.
Then it will be easier to see that
memcg_stat_item_valid(idx) access is in bounds when idx < MEMCG_NR_STAT

Also I'd do one check like (u32)idx >= MEMCG_NR_STAT
and drop idx < 0 part. Compiler is probably smart enough to
optimize this way itself, but I'd still do one check.