selftests/bpf: Add bpf_proactive_reclaim test
Subsystems (6)
PATCH v2 1/1 selftests/bpf: Add bpf_proactive_reclaim test
From: Hui Zhu <[email protected]>
bpf_proactive_reclaim() performs one bounded reclaim pass per call and, unlike a write to memory.reclaim, does not retry until the goal is reached.
Charge 32 MiB of page cache to a cgroup, ask for all of it in one call, and check that the result is positive but well below the request: one pass is capped at MEMCG_CHARGE_BATCH pages, far short of 32 MiB on both 4K and 64K page kernels.
The test deliberately covers only the kfunc itself. A full example of the intended asynchronous use, where a BPF program watches one cgroup's workingset refaults and reclaims another one from bpf_wq callbacks, is maintained out of tree at [1], released under GPLv2.
Add CONFIG_MEMCG to the config fragment, without which mm/bpf_memcontrol.c is not built at all.
[1] https://github.com/teawater/memcg-async-reclaim
---
Changelog:
v2:
According to the comment of Alexei, remove the samples/bpf sample patch. Ship it out-of-tree as a standalone project.
tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/memcg_proactive_reclaim.c | 124 ++++++++++++++++++ .../bpf/progs/memcg_proactive_reclaim.c | 34 +++++ 3 files changed, 159 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c create mode 100644 tools/testing/selftests/bpf/progs/memcg_proactive_reclaim.c
tools/testing/selftests/bpf/config +1 -0
@@ -57,6 +57,7 @@ CONFIG_LIRC=y CONFIG_LIVEPATCH=y CONFIG_LWTUNNEL=y CONFIG_LWTUNNEL_BPF=y +CONFIG_MEMCG=y CONFIG_MODULE_SIG=y CONFIG_MODULE_SRCVERSION_ALL=y CONFIG_MODULE_UNLOAD=y
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +124 -0
124 lines not on this page · raw message
tools/testing/selftests/bpf/progs/memcg_proactive_reclaim.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +struct reclaim_args { +__u64 cgroup_id; +__u64 size; +}; + +/* Signed, because bpf_test_run_opts.retval is a __u32. */ +__s64 reclaimed; + +SEC("syscall") +int memcg_proactive_reclaim (struct reclaim_args *ctx) +{ +struct mem_cgroup *memcg; +struct cgroup *cgrp; + + cgrp =bpf_cgroup_from_id (ctx->cgroup_id); +if (!cgrp) +return 0 ; + + memcg =bpf_get_mem_cgroup (&cgrp->self); +if (memcg) { + reclaimed =bpf_proactive_reclaim (memcg, ctx->size,-1 ); +bpf_put_mem_cgroup (memcg); + } +bpf_cgroup_release (cgrp); + +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: memcg_proactive_reclaim test fails instead of skipping when regular fs is missing…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: memcg_proactive_reclaim test fails instead of skipping when regular fs is missing --
commit 8a730887084ae177532f166fce0611c868fe9578
Author: Hui Zhu <[email protected]>
selftests/bpf: Add bpf_proactive_reclaim test
This commit adds a selftest for the bpf_proactive_reclaim() kfunc. It charges page cache to a cgroup and issues a bounded reclaim pass using the BPF helper, verifying that the result is capped correctly.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +0
-0
[ ... ]
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +16
-0+static const char *workload_dir (void ) +{ +static const char *const dirs[] = {"/tmp" ,"." }; +struct statfs st; +int i; + +for (i =0 ; i <ARRAY_SIZE (dirs); i++) +if (!statfs (dirs[i], &st) && st.f_type !=TMPFS_MAGIC && + st.f_type !=RAMFS_MAGIC ) +return dirs[i]; + +return NULL ; +} + +void test_memcg_proactive_reclaim (void ) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 65
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +3
-0+ dir =workload_dir (); +if (!ASSERT_OK_PTR (dir,"workload dir on a regular filesystem" )) +return ;
[Severity: Medium]
Will this cause the test suite to incorrectly report a failure rather than a skip when a regular filesystem is missing?
If workload_dir() returns NULL when both /tmp and . are tmpfs or ramfs (which is common in CI or initramfs environments), using ASSERT_OK_PTR(dir) here will fail the test suite explicitly. Should this skip the test instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 24 lines · Hui Zhu
From: Hui Zhu <[email protected]>
bpf_proactive_reclaim()performs one bounded reclaim pass per call and, unlike a write to memory.reclaim, does not retry until the goal is reached.Charge 32 MiB of page cache to a cgroup, ask for all of it in one call, and check that the result is positive but well below the request: one pass is capped at
MEMCG_CHARGE_BATCHpages, far short of 32 MiB on both 4K and 64K page kernels.The test deliberately covers only the kfunc itself. A full example of the intended asynchronous use, where a BPF program watches one cgroup's workingset refaults and reclaims another one from
bpf_wqcallbacks, is maintained out of tree at [1], released under GPLv2.Add
CONFIG_MEMCGto the config fragment, without whichmm/bpf_memcontrol.cis not built at all.[1] https://github.com/teawater/memcg-async-reclaim
Signed-off-by Hui Zhu <[email protected]>---
[...]
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 95
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +12
-0+ +/* +* Charge FILE_SIZE of page cache to the cgroup. Reading rather than +* writing keeps the pages clean, so reclaim does not have to start +* writeback before it can evict them. +*/ +if (!ASSERT_OK (ftruncate (data_fd,FILE_SIZE ),"ftruncate" )) + goto out; +for (off =0 ; off < (off_t )FILE_SIZE ; off +=sizeof (buf)) +if (!ASSERT_GT (read (data_fd, buf,sizeof (buf)),0 ,"read" )) + goto out; +
In case we might map file holes to the zero PFN someday (not sure when), this wouldn't necessarily give us page cache. So maybe write + fsync is more future-proof? In that case, you could also drop the two lines of comments.
Best Regards
Barry
[email protected]
AI reviewCould this allocation method cause issues if file holes are mapped to the zero PFN in the future? The read() approach assumes that accessing a ftruncate()-created sparse file will allocate page…
Quoted 12 lines
tools/testing/selftests/bpf/config +1
-0@@ -57,6 +57,7 @@ CONFIG_LIRC=y CONFIG_LIVEPATCH=y CONFIG_LWTUNNEL=y CONFIG_LWTUNNEL_BPF=y +CONFIG_MEMCG=y CONFIG_MODULE_SIG=y CONFIG_MODULE_SRCVERSION_ALL=y CONFIG_MODULE_UNLOAD=y
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 94
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +10
-0+/* +* Charge FILE_SIZE of page cache to the cgroup. Reading rather than +* writing keeps the pages clean, so reclaim does not have to start +* writeback before it can evict them. +*/ +if (!ASSERT_OK (ftruncate (data_fd,FILE_SIZE ),"ftruncate" )) + goto out; +for (off =0 ; off < (off_t )FILE_SIZE ; off +=sizeof (buf)) +if (!ASSERT_GT (read (data_fd, buf,sizeof (buf)),0 ,"read" )) + goto out;
Could this allocation method cause issues if file holes are mapped to the zero PFN in the future? The read() approach assumes that accessing a ftruncate()-created sparse file will allocate page cache, but if the kernel someday maps file holes to a shared zero page instead of allocating backing pages, this wouldn't necessarily give us page cache to reclaim.
Barry Song suggested using write() followed by fsync() as a more future-proof way to ensure actual page cache allocation, which would also allow removing the two lines of explanatory comments above.
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +8
-0+ + args.cgroup_id = cgroup_id; + args.size =FILE_SIZE ; + skel->bss->reclaimed =0 ; + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.memcg_proactive_reclaim), + &opts); +if (!ASSERT_OK (err,"test_run" )) + goto out;
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 112
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +10
-0+if (!ASSERT_EQ (opts.retval,0 ,"retval" )) + goto out; + +/* +* A single call is a single bounded pass: it reclaims something, but +* stops well short of the requested size instead of retrying until the +* goal is reached the way a write to memory.reclaim does. +*/ +ASSERT_GT (skel->bss->reclaimed,0 ,"reclaimed" ); +ASSERT_LT (skel->bss->reclaimed, (__s64 )FILE_SIZE ,"single pass" );
[ ... ]
---
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/36389535910