Skip to contentSkip to conversation

selftests/bpf: Add bpf_proactive_reclaim test

PATCH In review Hui Zhu 2 participants 2 messages Oct 9

Subsystems (6)
lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH v4 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:
v4:
According to the comment of Barry, test skips instead of failing on a read-only mount.
v3:
According to the comment of Barry, use write() + fsync() to charge the page cache instead of ftruncate() + read().
According to the comment of AI, skip instead of fail when no regular filesystem is available for the workload file.
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  | 130 ++++++++++++++++++
 .../bpf/progs/memcg_proactive_reclaim.c       |  34 +++++
 3 files changed, 165 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
index 2b883b388f90..5621ef94ad7e 100644
@@ -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 +130 -0

130 lines not on this page · raw message

tools/testing/selftests/bpf/progs/memcg_proactive_reclaim.c new file +34 -0
new file mode 100644
index 000000000000..b551192526fc
@@ -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
Reviewed-by on v4 1/1
#1 lore raw
Quoted 17 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_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.

Quoted 6 lines

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

Thanks very much!