BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Ziyang Men <ziyang.meme@gmail.com>
Cc: Shakeel Butt <shakeel.butt@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Kumar Kartikeya Dwivedi	 <memxor@gmail.com>,
	bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>,
	 Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	Shuah Khan <shuah@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	 kernel-team@meta.com, linux-mm@kvack.org,
	cgroups@vger.kernel.org,  linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/3] selftests/bpf: compare BPF and memory.stat memcg stat readers
Date: Tue, 07 Jul 2026 16:50:11 -0700	[thread overview]
Message-ID: <bc47c75a00acab57e7fea72612e0f6f089ddecc9.camel@gmail.com> (raw)
In-Reply-To: <ak2LXDWoPFSJL2Q9@devvm16600.scu0.facebook.com>

On Tue, 2026-07-07 at 16:27 -0700, Ziyang Men wrote:
> On Tue, Jul 07, 2026 at 02:21:59AM -0700, Eduard Zingerman wrote:
> > On Mon, 2026-07-06 at 18:50 -0700, Shakeel Butt wrote:
> > > On Mon, Jul 06, 2026 at 05:17:50PM -0700, Eduard Zingerman wrote:
> > > > On Fri, 2026-07-03 at 21:56 -0700, Ziyang Men wrote:
> > > > 
> > > > [...]
> > > > 
> > > > Hi Ziyang,
> > > > 
> > > > I'm a bit hesitant adding 2.5K lines of code to the BPF selftests,
> > > > as this code would need to be (a) maintained, (b) run at each CI invocation.
> > > > Hence, the tests added need to be relevant for the BPF sub-system.
> > > > 
> > > > Regarding the benchmarking part, as you state yourself:
> > > > 
> > > >   > In my testing (a 60-CPU VM) the BPF path is roughly an order of magnitude
> > > >   > faster than the per-cgroup memory.stat parse for a whole-tree scan, mainly
> > > >   > because it avoids the per-cgroup open/read and string parsing.
> > > > 
> > > > With this, I think the benchmarking code can be dropped altogether.
> > > > 
> > > > Next, the three memcg_stat_{reader,churn,churn_percpu}.c files share a
> > > > lot of utility code almost verbatim (e.g. tree definition/construction).
> > > > Such duplication should be avoided.
> > > > 
> > > > Finally, from the BPF point of view the test exercises the following functionality:
> > > > - kfuncs:
> > > >   - bpf_mem_cgroup_page_state
> > > >   - bpf_mem_cgroup_vm_events
> > > >   - bpf_put_mem_cgroup
> > > >   - bpf_get_mem_cgroup
> > > > - main iterator logic.
> > > > 
> > > > All kfuncs but bpf_get_mem_cgroup() are thin wrappers around mm/memcontrol.c code,
> > > > all kfuncs including the bpf_get_mem_cgroup() are already exercised in the selftests.
> > > > The iterator logic itself is covered by 8 sub-tests in the prog_tests/cgroup_iter.c.
> > > > Hence two questions:
> > > > - What do these new tests add in terms of tests coverage?
> > > > - Why do BPF selftests need to exercise the churn and churn_percpu scenarios?
> > > > 
> > > > Shakeel, could you please comment as well?
> > > 
> > > Hi Eduard,
> > > 
> > > Thanks a lot for taking a look. The main motivation I had behind requesting
> > > Ziyang to send this series (beside making him learn the tooling and process of
> > > sending patches to lkml) was to have a reference implementation and performance
> > > comparison for BPF based cgroup/memcg stats collection.
> > > 
> > > However you have correctly pointed out that selftests might not be the right
> > > place for such kind of code as selftests are more focused on functional tests
> > > and run by a lot of CIs while this is a performance benchmarking code.
> > > 
> > > I am wondering if there is a place for this benchmarking code in kernel under
> > > tools folder but archiving it on lkml might be good enough and should be easily
> > > searchable. Anyways thanks again for your time.
> > 
> > Hi Shakeel,
> > 
> > We do have bpf benchmarks in the kernel tree, the entry point is
> > tools/testing/selftests/bpf/bench.c. These are supposed to be
> > performance measurements and are executed manually from time to time
> > (quite rarely, as far as I understand), not by CI.
> > However, if I understand Ziyang's assessment correctly, this code is
> > not really a performance test, but kind of a load test.
> > 
> > Thanks,
> > Eduard
> 
> Hi Eduard,
> 
> Thanks a lot for the review. Yes, as Shakeel mentioned, the performance
> comparison for BPF-based cgroup stats collection was the original motivation.

You said yourself that the test is comparing an in-kernel walk to
parsing strings and opening files.

> But the patch also carries functional value: alongside that comparison, it
> checks the correctness of the stats the kfuncs return.
> 
> Let me first answer the main question -- what these tests add over what we
> already have -- and then lay out a plan.
> 
> First, the static test (memcg_stat_reader) vs the existing cgroup_iter_memcg.
> 
> The existing test calls the kfuncs, but for each value it only checks whether it
> is greater than zero. For example, in prog_tests/cgroup_iter_memcg.c:
> 
>      memset(map, 1, len);                    /* dirty some anon */
>      if (!ASSERT_OK(read_stats(link), "read stats"))
>              goto cleanup;
>      ASSERT_GT(memcg_query->nr_anon_mapped, 0, "final anon mapped val");
> 
> It never checks the value is actually correct -- i.e. compares it against the
> value in cgroupfs -- only that it is non-zero.
> 
> Besides, it also walks a single cgroup:
> 
>      .cgroup.order = BPF_CGROUP_ITER_SELF_ONLY,
> 
> and reads only five fields.

Arguably one of the the cgroup_iter_memcg.c tests can be extended to
allocate some mem and check if the value is reflected in the stats.
But there is a line between MM tests and BPF tests.
All BPF kfuncs except iterator logic itself are thin wrappers on
top of the existing MM functionality. Hence, I don't think that
BPF selftests are a place to stress-test these things.

> 
> The memcg_stat_reader in this patch adds three things:
> 
>    1. It compares the numbers. It reads the stats through the kfuncs and checks
>       they match the values in memory.stat, instead of only checking they are
>       non-zero (memcg_stat_reader.c, check_correctness()):
> 
>          /*
>           * anon (NR_ANON_MAPPED) is rstat-flushed and, with the charger
>           * stopped, deterministic: BPF and memory.stat must agree.
>           */
>          if ((b.anon > f.anon ? b.anon - f.anon
>                               : f.anon - b.anon) > anon_tol)
>                  anon_mism++;
>          ...
>          ASSERT_EQ(anon_mism, 0, "bpf vs file anon (rstat-flushed)");
> 
>       This is the main gap: b.anon comes from the kfunc, f.anon from parsing
>       memory.stat, and the test requires them to agree.

> "This is the main gap"
I'd prefer interacting with a human, not LLM.

>    2. It covers a whole subtree, not a single cgroup:
> 
>          linfo.cgroup.order = BPF_CGROUP_ITER_DESCENDANTS_PRE;
>    3. It reads a much broader field set (~40 fields, collect_full_stats()).
>       Minor.

I don't think that it's responsibility of BPF selftests to check all
these fields.

> Second, the churn test adds something the static test cannot.
> 
> These counters are kept separately on each CPU for speed, and are only added
> together when the code "flushes" them. The existing test does call the flush,
> right before reading (progs/cgroup_iter_memcg.c):
> 
>      bpf_mem_cgroup_flush_stats(memcg);
>      memcg_query.nr_anon_mapped = bpf_mem_cgroup_page_state(
>              memcg, bpf_core_enum_value(enum node_stat_item, NR_ANON_MAPPED));
> 
> But if the cgroup is idle there is nothing to add up, so the flush does no real
> work -- and since the result is only checked for non-zero, nothing verifies the
> flush gathered anything. If bpf_mem_cgroup_flush_stats() were replaced by an
> empty stub, this test would very likely still pass (the kernel also flushes on
> its own, periodically and on thresholds).
> 
> With activity going on, especially spread across several CPUs, we can make the
> flush do real work and then check it by comparing the numbers after the activity
> stops. That flush check is missing from all of the current tests, and this is
> where memcg_stat_churn / churn_percpu come in.

This sounds like an MM stress test.

> Finally, my plan:
> 
>    1. Keep the static (value-correctness) test, or fold it into cgroup_iter_memcg
>       if that works better. I also noticed that several kfuncs are not called by
>        any test today (no callers anywhere under tools/testing/selftests/bpf/,
>        only __ksym declarations in vmlinux.h): bpf_get_root_mem_cgroup,
>        bpf_mem_cgroup_usage, and bpf_mem_cgroup_memory_events. The static test
>        would be a good place to cover them too.

From BPF perspective the test is over-complicated.
There is no need for a generic tree construction.
Creating a hierarchy of several cgroups, allocating memory within
these cgroups and walking them would work perfectly fine.
Existing selftests already do that. Aside from querying actual memory
amount what else do you want to cover?

>    2. Keep only one of the two churn tests -- most likely the per-CPU one, since
>       it is the stronger of the two -- and change its correctness check to
>       actually verify the flush by comparing the numbers. The current check is
>       weak; it only checks non-zero values and that every cgroup was visited:
> 
>          ASSERT_EQ(missing, 0, "all cgroups present in map");
>          ASSERT_GT(total_anon, 0, "tree carries anon under churn");

These are MM tests, not BPF tests.

>    3. Move the code these tests share into a common header, to remove the
>       duplication you pointed out. Or we can combine these two tests into a
>       single one if works better.
> 
>    4. Keep the focus on functional testing in the selftests. The file-vs-BPF
>       timing could stay as an extra bonus -- printed only as informational
>       output in verbose mode, never as pass/fail -- but I'm happy to move that
>       comparison to the bench framework if you think it fits better there.
> 
> Please let me know if you have any concerns with this plan. I am happy to take
> any suggestions on how to improve the tests, or to change the focus of the
> selftests
> 
> Thanks again,
> Ziyang

  reply	other threads:[~2026-07-07 23:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-04  4:56 [PATCH 0/3] selftests/bpf: compare BPF and memory.stat memcg stat readers Ziyang Men
2026-07-04  4:56 ` [PATCH 1/3] selftests/bpf: add memcg_stat_reader BPF-vs-memory.stat benchmark Ziyang Men
2026-07-04  5:06   ` sashiko-bot
2026-07-04  4:56 ` [PATCH 2/3] selftests/bpf: add memcg_stat_churn BPF-vs-memory.stat benchmark under churn Ziyang Men
2026-07-04  5:39   ` bot+bpf-ci
2026-07-04  4:56 ` [PATCH 3/3] selftests/bpf: add memcg_stat_churn_percpu BPF-vs-memory.stat benchmark under cross-CPU churn Ziyang Men
2026-07-04  5:04   ` sashiko-bot
2026-07-04  5:58   ` bot+bpf-ci
2026-07-07  0:17 ` [PATCH 0/3] selftests/bpf: compare BPF and memory.stat memcg stat readers Eduard Zingerman
2026-07-07  1:50   ` Shakeel Butt
2026-07-07  9:21     ` Eduard Zingerman
2026-07-07 23:27       ` Ziyang Men
2026-07-07 23:50         ` Eduard Zingerman [this message]
2026-07-08  0:31           ` Shakeel Butt
2026-07-08  0:33             ` Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc47c75a00acab57e7fea72612e0f6f089ddecc9.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=emil@etsalapatis.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=ziyang.meme@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox