From: Roman Gushchin Add a BPF selftest attaching to the psi_avgs_work_tp tracepoint to monitor memory pressure in a cgroup throttled by memory.high. When full memory stall growth exceeds 50 ms within an update interval, the tracepoint schedules a sleepable bpf_task_work callback that invokes bpf_out_of_memory() for the pressured cgroup, verifying that the allocating task is terminated with SIGKILL and memory.events records oom_kill 1. Signed-off-by: Roman Gushchin Co-developed-by: Ben Gardon Signed-off-by: Ben Gardon --- v3 -> v4: - Updated subject/body from stale "PSI struct ops test" to reflect the psi_avgs_work_tp tracepoint design. - Indexed the BTF-flattened group->total array explicitly as total[PSI_AVGS * (NR_PSI_STATES - 1) + PSI_MEM_FULL], added missing SPDX header, removed redundant extern __ksym declarations, and dropped the first-interval skip. - Replaced cgroup freezing and racy fork/migration with a sync pipe, removed dead struct_ops remnants and FD/child leaks, added runtime PSI skip check, verified SIGKILL, and renamed to serial_test_psi(). v3: https://lore.kernel.org/all/20260127024421.494929-18-roman.gushchin@linux.dev/ .../selftests/bpf/prog_tests/test_psi.c | 107 ++++++++++++++++++ tools/testing/selftests/bpf/progs/test_psi.c | 88 ++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_psi.c create mode 100644 tools/testing/selftests/bpf/progs/test_psi.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_psi.c b/tools/testing/selftests/bpf/prog_tests/test_psi.c new file mode 100644 index 000000000000..9f3fdf2d6c5b --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_psi.c @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include + +#include "cgroup_helpers.h" +#include "memory_holder.h" + +#define MB (1024UL * 1024UL) +#include "test_psi.skel.h" + +/* + * Serialized because this test attaches to the global psi_avgs_work_tp + * tracepoint and drives real memory pressure / OOM under memory.high, which + * can interfere with other OOM tests running concurrently under test_progs -j. + */ +void serial_test_psi(void) +{ + const char *parent_cg = "/psi_test"; + const char *target_cg = "/psi_test/cg"; + struct test_psi *skel = NULL; + struct memory_holder worker; + int parent_fd = -1, target_fd = -1; + int status = 0, err; + unsigned long long cg_id; + char buf[4096] = {}; + + memory_holder_init(&worker); + + if (access("/proc/pressure/memory", R_OK) != 0) { + test__skip(); + return; + } + + err = setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + return; + + parent_fd = create_and_get_cgroup(parent_cg); + if (!ASSERT_GE(parent_fd, 0, "create parent cgroup")) + goto cleanup; + + err = write_cgroup_file(parent_cg, "cgroup.subtree_control", "+memory"); + if (!ASSERT_OK(err, "enable memory controller")) + goto cleanup; + + target_fd = create_and_get_cgroup(target_cg); + if (!ASSERT_GE(target_fd, 0, "create target cgroup")) + goto cleanup; + + cg_id = get_cgroup_id(target_cg); + if (!ASSERT_GT(cg_id, 0, "get_cgroup_id")) + goto cleanup; + + snprintf(buf, sizeof(buf), "%lu", 40 * MB); + err = write_cgroup_file(target_cg, "memory.high", buf); + if (!ASSERT_OK(err, "set memory.high")) + goto cleanup; + write_cgroup_file(target_cg, "memory.swap.max", "0"); + + err = memory_holder_spawn(&worker, 500 * MB, MB, 0, true); + if (!ASSERT_OK(err, "spawn worker")) + goto cleanup; + + snprintf(buf, sizeof(buf), "%d", worker.pid); + err = write_cgroup_file(target_cg, "cgroup.procs", buf); + if (!ASSERT_OK(err, "put child into cgroup")) + goto cleanup; + + skel = test_psi__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + goto cleanup; + + skel->bss->high_pressure_cgroup_id = cg_id; + skel->bss->my_pid = getpid(); + + err = test_psi__attach(skel); + if (!ASSERT_OK(err, "attach")) + goto cleanup; + + /* Release worker now that it is inside target_cg and PSI BPF is attached */ + if (!ASSERT_OK(memory_holder_start(&worker), "release worker")) + goto cleanup; + + if (!ASSERT_EQ(waitpid(worker.pid, &status, 0), worker.pid, + "waitpid worker")) + goto cleanup; + worker.pid = -1; + + ASSERT_TRUE(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL, + "worker terminated by SIGKILL"); + + if (ASSERT_OK(read_cgroup_file(target_cg, "memory.events", + buf, sizeof(buf)), + "read memory.events")) + ASSERT_OK_PTR(strstr(buf, "oom_kill 1"), "oom_kill count check"); + +cleanup: + memory_holder_cleanup(&worker); + if (target_fd >= 0) + close(target_fd); + if (parent_fd >= 0) + close(parent_fd); + test_psi__destroy(skel); + cleanup_cgroup_environment(); +} diff --git a/tools/testing/selftests/bpf/progs/test_psi.c b/tools/testing/selftests/bpf/progs/test_psi.c new file mode 100644 index 000000000000..927c1b8323d0 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_psi.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "vmlinux.h" +#include "bpf_experimental.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +/* cgroup which will experience the high memory pressure */ +u64 high_pressure_cgroup_id; +u32 my_pid = 0; + +/* last total full memory pressure value */ +u64 last_mem_full_total = 0; + +struct elem { + struct bpf_task_work tw; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, struct elem); +} tw_map SEC(".maps"); + +static int psi_oom_work(struct bpf_map *map, void *key, void *value) +{ + struct cgroup *cgrp; + struct mem_cgroup *memcg; + + cgrp = bpf_cgroup_from_id(high_pressure_cgroup_id); + if (!cgrp) + return 0; + + memcg = bpf_get_mem_cgroup(&cgrp->self); + if (memcg) { + bpf_out_of_memory(memcg, 0, BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK); + bpf_put_mem_cgroup(memcg); + } + + bpf_cgroup_release(cgrp); + return 0; +} + +static void schedule_oom_work(void) +{ + struct task_struct *task; + struct elem *val; + int key = 0; + + task = bpf_task_from_pid(my_pid); + if (task) { + val = bpf_map_lookup_elem(&tw_map, &key); + if (val) + bpf_task_work_schedule_signal(task, &val->tw, + &tw_map, psi_oom_work); + bpf_task_release(task); + } +} + +SEC("tp_btf/psi_avgs_work_tp") +int BPF_PROG(psi_avgs, struct psi_group *group) +{ + u64 current_total; + u64 growth; + + /* Monitor only a single target cgroup */ + if (group->cgroup_id != high_pressure_cgroup_id) + return 0; + + /* + * Check for memory pressure. BTF flattens the 2D array + * total[NR_PSI_AGGREGATORS][NR_PSI_STATES - 1] in vmlinux.h. + */ + current_total = BPF_CORE_READ(group, + total[PSI_AVGS * (NR_PSI_STATES - 1) + + PSI_MEM_FULL]); + growth = current_total - last_mem_full_total; + last_mem_full_total = current_total; + + /* Declare an OOM if growth > 50ms within the update period */ + if (growth > 50000000) + schedule_oom_work(); + + return 0; +} -- 2.56.1.911.g883d8bcb20-goog