Skip to contentSkip to conversation

selftests/bpf: Add bpf_proactive_reclaim test

PATCH Superseded Hui Zhu 2 participants 2 messages Oct 8

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH v3 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:
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  | 127 ++++++++++++++++++
 .../bpf/progs/memcg_proactive_reclaim.c       |  34 +++++
 3 files changed, 162 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 +127 -0

127 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
#1 lore raw
Quoted 23 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.

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

[...]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +4 -0
+
+       /*
+        * Both candidate directories can be tmpfs (initramfs-based CI, for
+        * instance). That is an environment limitation, not a test failure.

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 75

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +10 -0
+        */
+       dir = workload_dir();
+       if (!dir) {
+               test__skip();
+               return;
+       }
+
+       snprintf(data_file, sizeof(data_file),
+                "%s/memcg_proactive_reclaim_XXXXXX", dir);
+       data_fd = mkstemp(data_file);

If we can't write to the directory, will mkstemp() fail? Should we check access(dir, W_OK) first?

tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c line 78

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/memcg_proactive_reclaim.c new file +3 -0
+       if (!ASSERT_GE(data_fd, 0, "mkstemp"))
+               return;
+

Best Regards
Barry