From nobody Mon Feb 9 06:25:11 2026 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA8492FFDFC for ; Tue, 27 Jan 2026 02:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769481914; cv=none; b=jcLW/2Q5iq2pnbBoUZpI6tk+5Co5CXUtgTM/tOPy5Bg7SWVezOloeiyNewMcRM4juK38He196uDo9bu+7WaNnYEnxxwGVYGpiJt8pcbySbzhzvltK+ZNtK9C4y2VDJVUK/EEe6LIgEFvmldReGt2R3wEpRhHT1aN2OxUsmwZAh4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769481914; c=relaxed/simple; bh=w3YGr0s24x6yjPL2gVHFZILWuRUgTYedlGFyvngKpYg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O/ot2KvRjCg3mqAfWvtcoGzYmN7VqtJ7hnAafezRplkeeDtxdoSgmEwjQdKSD1tRLk+H/Hyx3KX2ce/KrvbqLAO6ru3+Ir03IU/md/1Yg/kwb0AfoM2jwQ3jreRmkNsLXV2YWexd3ksNM4JEXcCIrPtLTKizsLRnBoLcPlXXAM8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=UksrWR4I; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="UksrWR4I" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769481909; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vt6yCthx83SOS4lBcyiZWD1/2agiAEDOu7IdKxelec0=; b=UksrWR4IpOgc56E0T4z+uk+SHBgfDOTNl6Ok0oOsw85O9Jz4W0IlcetQU7CqFcwTpRnRdV ueXJEffUTPVeHm1Icl9TSrn2H7tbPmy2tiN1kSCStUiA6lrXGJruNrmdnwFJ4TQqCD4Xwc s1HrsCYMJnWVMdPTgS++ryIz2SJSQAc= From: Roman Gushchin To: bpf@vger.kernel.org Cc: Michal Hocko , Alexei Starovoitov , Matt Bobrowski , Shakeel Butt , JP Kobryn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Suren Baghdasaryan , Johannes Weiner , Andrew Morton , Roman Gushchin Subject: [PATCH bpf-next v3 12/17] bpf: selftests: BPF OOM struct ops test Date: Mon, 26 Jan 2026 18:44:15 -0800 Message-ID: <20260127024421.494929-13-roman.gushchin@linux.dev> In-Reply-To: <20260127024421.494929-1-roman.gushchin@linux.dev> References: <20260127024421.494929-1-roman.gushchin@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Implement a kselftest for the OOM handling functionality. The OOM handling policy which is implemented in BPF is to kill all tasks belonging to the biggest leaf cgroup, which doesn't contain unkillable tasks (tasks with oom_score_adj set to -1000). Pagecache size is excluded from the accounting. The test creates a hierarchy of memory cgroups, causes an OOM at the top level, checks that the expected process is killed and verifies the memcg's oom statistics. The same BPF OOM policy is attached to a memory cgroup and system-wide. In the first case the program does nothing and returns false, so it's executed the second time, when it properly handles the OOM. Signed-off-by: Roman Gushchin --- .../selftests/bpf/prog_tests/test_oom.c | 256 ++++++++++++++++++ tools/testing/selftests/bpf/progs/test_oom.c | 111 ++++++++ 2 files changed, 367 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_oom.c create mode 100644 tools/testing/selftests/bpf/progs/test_oom.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_oom.c b/tools/test= ing/selftests/bpf/prog_tests/test_oom.c new file mode 100644 index 000000000000..a1eadbe1ae83 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_oom.c @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +#include "cgroup_helpers.h" +#include "test_oom.skel.h" + +struct cgroup_desc { + const char *path; + int fd; + unsigned long long id; + int pid; + size_t target; + size_t max; + int oom_score_adj; + bool victim; +}; + +#define MB (1024 * 1024) +#define OOM_SCORE_ADJ_MIN (-1000) +#define OOM_SCORE_ADJ_MAX 1000 + +static struct cgroup_desc cgroups[] =3D { + { .path =3D "/oom_test", .max =3D 80 * MB}, + { .path =3D "/oom_test/cg1", .target =3D 10 * MB, + .oom_score_adj =3D OOM_SCORE_ADJ_MAX }, + { .path =3D "/oom_test/cg2", .target =3D 40 * MB, + .oom_score_adj =3D OOM_SCORE_ADJ_MIN }, + { .path =3D "/oom_test/cg3" }, + { .path =3D "/oom_test/cg3/cg4", .target =3D 30 * MB, + .victim =3D true }, + { .path =3D "/oom_test/cg3/cg5", .target =3D 20 * MB }, +}; + +static int spawn_task(struct cgroup_desc *desc) +{ + char *ptr; + int pid; + + pid =3D fork(); + if (pid < 0) + return pid; + + if (pid > 0) { + /* parent */ + desc->pid =3D pid; + return 0; + } + + /* child */ + if (desc->oom_score_adj) { + char buf[64]; + int fd =3D open("/proc/self/oom_score_adj", O_WRONLY); + + if (fd < 0) + return -1; + + snprintf(buf, sizeof(buf), "%d", desc->oom_score_adj); + write(fd, buf, sizeof(buf)); + close(fd); + } + + ptr =3D (char *)malloc(desc->target); + if (!ptr) + return -ENOMEM; + + memset(ptr, 'a', desc->target); + + while (1) + sleep(1000); + + return 0; +} + +static void setup_environment(void) +{ + int i, err; + + err =3D setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + goto cleanup; + + for (i =3D 0; i < ARRAY_SIZE(cgroups); i++) { + cgroups[i].fd =3D create_and_get_cgroup(cgroups[i].path); + if (!ASSERT_GE(cgroups[i].fd, 0, "create_and_get_cgroup")) + goto cleanup; + + cgroups[i].id =3D get_cgroup_id(cgroups[i].path); + if (!ASSERT_GT(cgroups[i].id, 0, "get_cgroup_id")) + goto cleanup; + + /* Freeze the top-level cgroup */ + if (i =3D=3D 0) { + /* Freeze the top-level cgroup */ + err =3D write_cgroup_file(cgroups[i].path, "cgroup.freeze", "1"); + if (!ASSERT_OK(err, "freeze cgroup")) + goto cleanup; + } + + /* Recursively enable the memory controller */ + if (!cgroups[i].target) { + + err =3D write_cgroup_file(cgroups[i].path, "cgroup.subtree_control", + "+memory"); + if (!ASSERT_OK(err, "enable memory controller")) + goto cleanup; + } + + /* Set memory.max */ + if (cgroups[i].max) { + char buf[256]; + + snprintf(buf, sizeof(buf), "%lu", cgroups[i].max); + err =3D write_cgroup_file(cgroups[i].path, "memory.max", buf); + if (!ASSERT_OK(err, "set memory.max")) + goto cleanup; + + snprintf(buf, sizeof(buf), "0"); + write_cgroup_file(cgroups[i].path, "memory.swap.max", buf); + + } + + /* Spawn tasks creating memory pressure */ + if (cgroups[i].target) { + char buf[256]; + + err =3D spawn_task(&cgroups[i]); + if (!ASSERT_OK(err, "spawn task")) + goto cleanup; + + snprintf(buf, sizeof(buf), "%d", cgroups[i].pid); + err =3D write_cgroup_file(cgroups[i].path, "cgroup.procs", buf); + if (!ASSERT_OK(err, "put child into a cgroup")) + goto cleanup; + } + } + + return; + +cleanup: + cleanup_cgroup_environment(); + + // TODO return an error? +} + +static int run_and_wait_for_oom(void) +{ + int ret =3D -1; + bool first =3D true; + char buf[4096] =3D {}; + size_t size; + + /* Unfreeze the top-level cgroup */ + ret =3D write_cgroup_file(cgroups[0].path, "cgroup.freeze", "0"); + if (!ASSERT_OK(ret, "freeze cgroup")) + return -1; + + for (;;) { + int i, status; + pid_t pid =3D wait(&status); + + if (pid =3D=3D -1) { + if (errno =3D=3D EINTR) + continue; + /* ECHILD */ + break; + } + + if (!first) + continue; + + first =3D false; + + /* Check which process was terminated first */ + for (i =3D 0; i < ARRAY_SIZE(cgroups); i++) { + if (!ASSERT_OK(cgroups[i].victim !=3D + (pid =3D=3D cgroups[i].pid), + "correct process was killed")) { + ret =3D -1; + break; + } + + if (!cgroups[i].victim) + continue; + + /* Check the memcg oom counter */ + size =3D read_cgroup_file(cgroups[i].path, + "memory.events", + buf, sizeof(buf)); + if (!ASSERT_OK(size <=3D 0, "read memory.events")) { + ret =3D -1; + break; + } + + if (!ASSERT_OK(strstr(buf, "oom_kill 1") =3D=3D NULL, + "oom_kill count check")) { + ret =3D -1; + break; + } + } + + /* Kill all remaining tasks */ + for (i =3D 0; i < ARRAY_SIZE(cgroups); i++) + if (cgroups[i].pid && cgroups[i].pid !=3D pid) + kill(cgroups[i].pid, SIGKILL); + } + + return ret; +} + +void test_oom(void) +{ + DECLARE_LIBBPF_OPTS(bpf_struct_ops_opts, opts); + struct bpf_link *link1 =3D NULL, *link2 =3D NULL; + struct test_oom *skel; + int err =3D 0; + + setup_environment(); + + skel =3D test_oom__open_and_load(); + if (!skel) { + err =3D -errno; + CHECK_FAIL(err); + goto cleanup; + } + + opts.flags =3D BPF_F_CGROUP_FD; + opts.target_fd =3D cgroups[0].fd; + link1 =3D bpf_map__attach_struct_ops_opts(skel->maps.test_bpf_oom, &opts); + if (!link1) { + err =3D -errno; + CHECK_FAIL(err); + goto cleanup; + } + + opts.target_fd =3D get_root_cgroup(); + link2 =3D bpf_map__attach_struct_ops_opts(skel->maps.test_bpf_oom, &opts); + if (!link2) { + err =3D -errno; + CHECK_FAIL(err); + goto cleanup; + } + + /* Unfreeze all child tasks and create the memory pressure */ + err =3D run_and_wait_for_oom(); + CHECK_FAIL(err); + +cleanup: + bpf_link__destroy(link1); + bpf_link__destroy(link2); + write_cgroup_file(cgroups[0].path, "cgroup.kill", "1"); + write_cgroup_file(cgroups[0].path, "cgroup.freeze", "0"); + cleanup_cgroup_environment(); + test_oom__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_oom.c b/tools/testing/s= elftests/bpf/progs/test_oom.c new file mode 100644 index 000000000000..7ff354e416bc --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_oom.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +#define OOM_SCORE_ADJ_MIN (-1000) + +static bool mem_cgroup_killable(struct mem_cgroup *memcg) +{ + struct task_struct *task; + bool ret =3D true; + + bpf_for_each(css_task, task, &memcg->css, CSS_TASK_ITER_PROCS) + if (task->signal->oom_score_adj =3D=3D OOM_SCORE_ADJ_MIN) + return false; + + return ret; +} + +/* + * Find the largest leaf cgroup (ignoring page cache) without unkillable t= asks + * and kill all belonging tasks. + */ +SEC("struct_ops.s/handle_out_of_memory") +int BPF_PROG(test_out_of_memory, struct oom_control *oc, struct bpf_struct= _ops_link *link) +{ + struct task_struct *task; + struct mem_cgroup *root_memcg =3D oc->memcg; + struct mem_cgroup *memcg, *victim =3D NULL; + struct cgroup_subsys_state *css_pos, *css; + unsigned long usage, max_usage =3D 0; + unsigned long pagecache =3D 0; + int ret =3D 0; + + if (root_memcg) + root_memcg =3D bpf_get_mem_cgroup(&root_memcg->css); + else + root_memcg =3D bpf_get_root_mem_cgroup(); + + if (!root_memcg) + return 0; + + css =3D &root_memcg->css; + if (css && css->cgroup =3D=3D link->cgroup) + goto exit; + + bpf_rcu_read_lock(); + bpf_for_each(css, css_pos, &root_memcg->css, BPF_CGROUP_ITER_DESCENDANTS_= POST) { + if (css_pos->cgroup->nr_descendants + css_pos->cgroup->nr_dying_descenda= nts) + continue; + + memcg =3D bpf_get_mem_cgroup(css_pos); + if (!memcg) + continue; + + usage =3D bpf_mem_cgroup_usage(memcg); + pagecache =3D bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES); + + if (usage > pagecache) + usage -=3D pagecache; + else + usage =3D 0; + + if ((usage > max_usage) && mem_cgroup_killable(memcg)) { + max_usage =3D usage; + if (victim) + bpf_put_mem_cgroup(victim); + victim =3D bpf_get_mem_cgroup(&memcg->css); + } + + bpf_put_mem_cgroup(memcg); + } + bpf_rcu_read_unlock(); + + if (!victim) + goto exit; + + bpf_for_each(css_task, task, &victim->css, CSS_TASK_ITER_PROCS) { + struct task_struct *t =3D bpf_task_acquire(task); + + if (t) { + /* + * If the task is already an OOM victim, it will + * quit soon and release some memory. + */ + if (bpf_task_is_oom_victim(task)) { + bpf_task_release(t); + ret =3D 1; + break; + } + + bpf_oom_kill_process(oc, task, "bpf oom test"); + bpf_task_release(t); + ret =3D 1; + } + } + + bpf_put_mem_cgroup(victim); +exit: + bpf_put_mem_cgroup(root_memcg); + + return ret; +} + +SEC(".struct_ops.link") +struct bpf_oom_ops test_bpf_oom =3D { + .name =3D "bpf_test_policy", + .handle_out_of_memory =3D (void *)test_out_of_memory, +}; --=20 2.52.0