From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245469; b=gsWmCx0zFSEqTuKiA+WriNwFiFFZdzZdNngD2zNkOlZbKYg8xLSsdVKpltXRIwR+OeEN 6rtn5coh6DP0njMkwdw/dJY+l2U44/qSr1UuAWIOR4rFcVy/pRLfRuFy7dK8w5cLoYuf/ ejBTc4kbk6T9EetAPWFwmIg9AyQ+dgJ0sfiAavZfYjJFWg02GgnDY6UWMJqrcB5udN6WY T6Gb5ZJ16z47Me/qYrNXJQyrpCF27F5ip8Bh3dGtCuoZifDPqGoDl154IGzg+RZ9eTiPq KiW1gWrh5tYcMtx/La0IdB6go/ZWBx2UazKpAQKbUEqWNgPuWRnTJjahaSX/NjIMS5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245469; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=+ABtbUfwyWryMJDCytDAGqSieAyTXNhJBg+FBk/e/YI=; b=OhpW6/Wo+2ei8f8HoTlSt0Ibkq8NEFicO+5Un+t4gn2nuv9xn7o5Dd4rSEE0QLTyhm99 1sGkuIeB6jhjJ85q3YJdntMERvy0xGkVcr0zc3GCVmg2z5IwsvArIVmQIPTu1K+xFLi8n 0nbN73phleuYMd4JNxGIALnpIAuLNZpJJdPdMX0UWtGDIKMml1a0hJTePIA+EbTBU6+uQ ayl1mb1PJdhQnLJUmrl1uS1lvzVSr39SGQmFpQl1Fv4PUILLSgLAIqhbpNEX+pbFVr6am 2tHg4+/AXO03lSDZ6A6LkpFIHU5fnSooTobYwpsJfTNAbPIPDhgSwsBqCB8IufRvw4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dBnhV8rW; arc=none smtp.remote-ip=209.85.167.199 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ADBC606A69B for ; Wed, 2 Dec 2026 21:04:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5ADBC606A69B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dBnhV8rW Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-4fb73e868aaso14867297b6e.0 for ; Wed, 02 Dec 2026 13:04:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796245468; x=1796850268; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+ABtbUfwyWryMJDCytDAGqSieAyTXNhJBg+FBk/e/YI=; b=dBnhV8rWtQOZRh4fXj4SwXORjJxab6uC8idsBMKOc1ZYAk+G+0xoQZegUcsWQtLMCx WPuIBR9dIBhAw/Zjm2SyrSRMG9xsxkrInOKzWPk4iaIy8mq9RkVMZowK2F/SSs8qfsDO /BJitYQjvlstQy2yxxDSeF7T3zMdSc7Htw6sUBM/MLKNHiXoVD01euQ2ZeuhoJx2dSQm jG0E4+EHhscfDNuuIikCdhm6U+1x06lR14qUSdx3XqhpDSVTJkaHyuF3V3xOB9w0BEeH gjUUn2kOtLirRkBf56WS6Is5PNrmmtI0IHQnUmxxVmqoKYgP0NytPxRUeltitoFVG6tj 1E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245468; x=1796850268; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+ABtbUfwyWryMJDCytDAGqSieAyTXNhJBg+FBk/e/YI=; b=NN0NrJjmBi73d98zNbc+BzYi10CH9zXuzGi1WSiEOaoraXvCRpraeUTj8faYjgMfMs B2bAKIPdg8WZl59XmXkM3EL+EFhD5maruPqHdGuKrClghxtGU1PTeFRxAm1wOg91X8L0 FqyLH2rqr9Qr18W4+9JovdUK1e2hp6wPFfws0PffcL+Dkzdwni+AmGh7E8y+jYePTMDU BC6qDFw6HYcqYQNU5FACgVxyhz3nZZPmf2pwn9HfLeJyGMSEBIBJm05ggGzkx0diUFHP wR/GgoU9HHHHolyrNTDGvNzcqc6TYSR/mE2rTkr/c4HrhE6whKAyp+7K9TbjW5WGxrjg 4YQg== X-Forwarded-Encrypted: i=1; AL+hErE9P9q9HAeEslhHX8P6EaRbEc/cLSUzpK3Fwlj+hHlGlVJNsQZGqR7tTNRje04N5FdK+Y0X1rOQ@vger.kernel.org X-Gm-Message-State: APuU4Z+Sop4j27d8f7dYHuu0WWyaUAS2GGr/8oW2DUFZFb1uG/Q0wSW4 irGzQcY76z26OfcfbK47mVeyBGv16ErgXmsqZPCCAXje9wsC8GV3ArBSlQ/dyyj7PFuafOMyDaz rzl8dQXmLZg== X-Received: from iojj12.prod.google.com ([2002:a05:6602:820c:b0:9c7:7732:c3c1]) (user=bgardon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:e90:b0:568:d66f:6327 with SMTP id 5614622812f47-57a0b44ef0bmr40073969b6e.51.1796245467884; Wed, 02 Dec 2026 13:04:27 -0800 (PST) Date: Wed, 2 Dec 2026 13:03:55 -0800 In-Reply-To: <20261202210415.517651-1-bgardon@google.com> X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202210415.517651-1-bgardon@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202210415.517651-6-bgardon@google.com> Subject: [PATCH bpf-next v4 05/17] selftests/bpf: Add BPF OOM struct ops test From: Ben Gardon To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Andrew Morton , Johannes Weiner , Michal Hocko , Shakeel Butt , Roman Gushchin , Tejun Heo Cc: JP Kobryn , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Rientjes , Suren Baghdasaryan , Josh Don , Sweet Tea Dorminy , Chuyi Zhou , Yafang Shao , benbwgardon@gmail.com, bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Shuah Khan , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" From: Roman Gushchin Implement a kselftest for the OOM handling functionality. This BPF OOM policy terminates all tasks belonging to the largest leaf cgroup (excluding page cache) that contains no exempt tasks (oom_score_adj set to -1000). The test: 1. builds a hierarchy of memory cgroups 2. pre-allocates baseline memory, below memory.max 3. triggers an OOM from the largest eligible leaf 4. verifies that the expected process is selected and terminated with SIGKILL 5. checks the chosen memcg's memory.events oom_kill counter to validate Attaching the same policy to both the subtree root (where it returns 0) and the root cgroup verifies hierarchical delegation. Signed-off-by: Roman Gushchin Co-developed-by: Ben Gardon Signed-off-by: Ben Gardon --- v3 -> v4: - Adopted bpf_map__attach_cgroup_opts() and CO-RE task->signal->oom_mm. - Replaced cgroup freezing with pipes to synchronize with workers so multi-CPU runs cannot flake. - Renamed to serial_test_oom() to avoid root-cgroup collisions under test_progs -j. - Scanned the full subtree for in-flight oom_mm, passed acquired task reference to bpf_oom_kill_process(), and checked its return value. v3: https://lore.kernel.org/all/20260127024421.494929-13-roman.gushchin@linux.dev/ .../selftests/bpf/prog_tests/memory_holder.h | 158 +++++++++++++ .../selftests/bpf/prog_tests/test_oom.c | 218 ++++++++++++++++++ tools/testing/selftests/bpf/progs/test_oom.c | 132 +++++++++++ 3 files changed, 508 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/memory_holder.h 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/memory_holder.h b/tools/testing/selftests/bpf/prog_tests/memory_holder.h new file mode 100644 index 000000000000..34b408da3958 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/memory_holder.h @@ -0,0 +1,158 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __MEMORY_HOLDER_H +#define __MEMORY_HOLDER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +struct memory_holder { + pid_t pid; + int start_fd; + int done_fd; +}; + +static inline void memory_holder_init(struct memory_holder *w) +{ + w->pid = -1; + w->start_fd = -1; + w->done_fd = -1; +} + +static inline void memory_holder_cleanup(struct memory_holder *w) +{ + if (w->start_fd >= 0) { + close(w->start_fd); + w->start_fd = -1; + } + if (w->done_fd >= 0) { + close(w->done_fd); + w->done_fd = -1; + } + if (w->pid > 0) { + kill(w->pid, SIGKILL); + waitpid(w->pid, NULL, 0); + w->pid = -1; + } +} + +/* + * Fork a child that sets /proc/self/oom_score_adj, waits for a byte on + * start_fd (after the parent places it into a cgroup), faults in @target + * bytes of anonymous memory (in @chunk-byte chunks if @chunk > 0), writes a + * byte to done_fd, and either exits (if @exit_after_alloc is true) or sleeps + * until killed. + */ +static inline int memory_holder_spawn(struct memory_holder *w, size_t target, + size_t chunk, int oom_score_adj, + bool exit_after_alloc) +{ + int start_pipe[2] = { -1, -1 }; + int done_pipe[2] = { -1, -1 }; + int ret = 0; + pid_t pid; + char c; + + memory_holder_init(w); + + if (pipe(start_pipe) < 0) + return -errno; + if (pipe(done_pipe) < 0) { + ret = -errno; + goto err_pipes; + } + + pid = fork(); + if (pid < 0) { + ret = -errno; + goto err_pipes; + } + + if (pid > 0) { + w->pid = pid; + w->start_fd = start_pipe[1]; + w->done_fd = done_pipe[0]; + close(start_pipe[0]); + close(done_pipe[1]); + return 0; + } + + close(start_pipe[1]); + close(done_pipe[0]); + + { + char buf[8]; + int fd = open("/proc/self/oom_score_adj", O_WRONLY); + + if (fd < 0) + _exit(1); + snprintf(buf, sizeof(buf), "%d", oom_score_adj); + if (write(fd, buf, strlen(buf)) < 0) + _exit(1); + close(fd); + } + + /* Wait for parent to place us in the target cgroup and release us */ + if (read(start_pipe[0], &c, 1) != 1) + _exit(1); + close(start_pipe[0]); + + if (target) { + size_t step = chunk ?: target; + size_t done = 0; + + while (done < target) { + size_t sz = target - done < step ? target - done : step; + char *ptr = malloc(sz); + + if (!ptr) + _exit(ENOMEM); + memset(ptr, 'a', sz); + /* Prevent -O2 dead-store elimination before _exit() */ + asm volatile("" : : "r"(ptr) : "memory"); + done += sz; + } + } + + if (write(done_pipe[1], "d", 1) != 1) + _exit(1); + close(done_pipe[1]); + + if (exit_after_alloc) + _exit(0); + + for (;;) + sleep(1000); + +err_pipes: + if (start_pipe[0] >= 0) + close(start_pipe[0]); + if (start_pipe[1] >= 0) + close(start_pipe[1]); + if (done_pipe[0] >= 0) + close(done_pipe[0]); + if (done_pipe[1] >= 0) + close(done_pipe[1]); + return ret; +} + +static inline int memory_holder_start(struct memory_holder *w) +{ + return write(w->start_fd, "g", 1) == 1 ? 0 : -EIO; +} + +static inline int memory_holder_wait_ready(struct memory_holder *w) +{ + char c; + + return read(w->done_fd, &c, 1) == 1 ? 0 : -EIO; +} + +#endif /* __MEMORY_HOLDER_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/test_oom.c b/tools/testing/selftests/bpf/prog_tests/test_oom.c new file mode 100644 index 000000000000..f17742ace1ce --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_oom.c @@ -0,0 +1,218 @@ +// 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_oom.skel.h" + +#define OOM_SCORE_ADJ_MIN (-1000) +#define OOM_SCORE_ADJ_MAX 1000 + +struct cgroup_desc { + const char *path; + int fd; + struct memory_holder worker; + size_t target; + size_t max; + int oom_score_adj; + bool expect_terminated; +}; + +/* + * Baseline workers (cg1 10 MB + cg2 35 MB + cg5 10 MB = 55 MB) allocate first + * and wait under the 80 MB limit. Then cg4 (35 MB) allocates and crosses 80 MB + * at ~25 MB, triggering a single deterministic OOM where cg2 (35 MB) is exempt + * via OOM_SCORE_ADJ_MIN and cg4 (~25 MB) is the largest eligible leaf. + */ +static struct cgroup_desc cgroups[] = { + { .path = "/oom_test", .max = 80 * MB }, + { .path = "/oom_test/cg1", .target = 10 * MB, + .oom_score_adj = OOM_SCORE_ADJ_MAX }, + { .path = "/oom_test/cg2", .target = 35 * MB, + .oom_score_adj = OOM_SCORE_ADJ_MIN }, + { .path = "/oom_test/cg3" }, + { .path = "/oom_test/cg3/cg4", .target = 35 * MB, + .expect_terminated = true }, + { .path = "/oom_test/cg3/cg5", .target = 10 * MB }, +}; + +static void reset_cgroups(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + cgroups[i].fd = -1; + memory_holder_init(&cgroups[i].worker); + } +} + +static void cleanup_workers(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + memory_holder_cleanup(&cgroups[i].worker); + if (cgroups[i].fd >= 0) { + close(cgroups[i].fd); + cgroups[i].fd = -1; + } + } +} + +static int setup_environment(void) +{ + int i, err; + + reset_cgroups(); + + err = setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + return -1; + + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + char buf[64]; + + cgroups[i].fd = create_and_get_cgroup(cgroups[i].path); + if (!ASSERT_GE(cgroups[i].fd, 0, "create_and_get_cgroup")) + return -1; + + if (cgroups[i].target == 0) { + err = write_cgroup_file(cgroups[i].path, + "cgroup.subtree_control", + "+memory"); + if (!ASSERT_OK(err, "enable memory controller")) + return -1; + } + + if (cgroups[i].max) { + snprintf(buf, sizeof(buf), "%zu", cgroups[i].max); + err = write_cgroup_file(cgroups[i].path, "memory.max", + buf); + if (!ASSERT_OK(err, "set memory.max")) + return -1; + write_cgroup_file(cgroups[i].path, "memory.swap.max", + "0"); + } + + if (cgroups[i].target) { + err = memory_holder_spawn(&cgroups[i].worker, + cgroups[i].target, 0, + cgroups[i].oom_score_adj, + cgroups[i].expect_terminated); + if (!ASSERT_OK(err, "spawn worker")) + return -1; + + snprintf(buf, sizeof(buf), "%d", cgroups[i].worker.pid); + err = write_cgroup_file(cgroups[i].path, "cgroup.procs", + buf); + if (!ASSERT_OK(err, "put child into cgroup")) + return -1; + } + } + + /* Pre-allocate all non-trigger baseline workers under memory.max */ + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + if (!cgroups[i].target || cgroups[i].expect_terminated) + continue; + + if (!ASSERT_OK(memory_holder_start(&cgroups[i].worker), + "start baseline worker")) + return -1; + if (!ASSERT_OK(memory_holder_wait_ready(&cgroups[i].worker), + "wait baseline worker")) + return -1; + } + + return 0; +} + +/* + * Serialized because this test attaches a BPF OOM policy to the root cgroup + * and triggers real memcg OOMs, which would collide with other OOM tests + * running in parallel under test_progs -j. + */ +void serial_test_oom(void) +{ + LIBBPF_OPTS(bpf_cgroup_opts, opts); + struct bpf_link *cgroup_link = NULL, *root_link = NULL; + struct test_oom *skel = NULL; + int root_cg_fd = -1, status = 0, i; + char buf[4096] = {}; + + if (setup_environment()) + goto cleanup; + + skel = test_oom__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + goto cleanup; + + /* + * Attach to both /oom_test (which returns 0) and root (which handles + * the OOM) to exercise hierarchical delegation. + */ + cgroup_link = bpf_map__attach_cgroup_opts(skel->maps.test_bpf_oom, + cgroups[0].fd, &opts); + if (!ASSERT_OK_PTR(cgroup_link, "attach_oom_cgroup")) + goto cleanup; + + root_cg_fd = get_root_cgroup(); + if (!ASSERT_GE(root_cg_fd, 0, "get_root_cgroup")) + goto cleanup; + + root_link = bpf_map__attach_cgroup_opts(skel->maps.test_bpf_oom, + root_cg_fd, &opts); + if (!ASSERT_OK_PTR(root_link, "attach_oom_root")) + goto cleanup; + + /* Release the trigger worker (cg4) to cross memory.max */ + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + if (!cgroups[i].expect_terminated) + continue; + + if (!ASSERT_OK(memory_holder_start(&cgroups[i].worker), + "start trigger worker")) + goto cleanup; + + if (!ASSERT_EQ(waitpid(cgroups[i].worker.pid, &status, 0), + cgroups[i].worker.pid, "waitpid trigger")) + goto cleanup; + cgroups[i].worker.pid = -1; + + if (!ASSERT_TRUE(WIFSIGNALED(status) && + WTERMSIG(status) == SIGKILL, + "expected process was terminated")) + goto cleanup; + + if (!ASSERT_OK(read_cgroup_file(cgroups[i].path, + "memory.events", + buf, sizeof(buf)), + "read memory.events")) + goto cleanup; + + if (!ASSERT_OK_PTR(strstr(buf, "oom_kill 1"), + "oom_kill count check")) + goto cleanup; + } + + /* Verify all other baseline workers survived */ + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { + if (cgroups[i].worker.pid > 0) + ASSERT_EQ(waitpid(cgroups[i].worker.pid, &status, + WNOHANG), 0, + "non-target worker survived"); + } + +cleanup: + bpf_link__destroy(root_link); + bpf_link__destroy(cgroup_link); + if (root_cg_fd >= 0) + close(root_cg_fd); + cleanup_workers(); + cleanup_cgroup_environment(); + test_oom__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_oom.c b/tools/testing/selftests/bpf/progs/test_oom.c new file mode 100644 index 000000000000..a752d332f8b7 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_oom.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Example BPF OOM policy for selftests (not intended for production use). + * + * Exercises bpf_oom_ops attachment, hierarchical delegation (returning 0 on + * the subtree root and handling on root), and bpf_oom_kill_process() by + * terminating all tasks in the largest eligible leaf cgroup (similar to + * memory.oom.group semantics). + */ +#include "vmlinux.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +#define OOM_SCORE_ADJ_MIN (-1000) + +static bool mem_cgroup_has_oom_victim(struct mem_cgroup *memcg) +{ + struct task_struct *task; + + bpf_for_each(css_task, task, &memcg->css, CSS_TASK_ITER_PROCS) + if (BPF_CORE_READ(task, signal, oom_mm)) + return true; + + return false; +} + +static bool mem_cgroup_killable(struct mem_cgroup *memcg) +{ + struct task_struct *task; + + bpf_for_each(css_task, task, &memcg->css, CSS_TASK_ITER_PROCS) + if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) + return false; + + return true; +} + +/* + * Find the largest leaf cgroup (ignoring page cache) without exempt tasks + * and terminate all belonging tasks. + */ +SEC("struct_ops.s/handle_out_of_memory") +int BPF_PROG(test_out_of_memory, struct oom_control *oc, struct cgroup *cgrp) +{ + struct task_struct *task; + struct mem_cgroup *root_memcg = oc->memcg; + struct mem_cgroup *memcg, *chosen = NULL; + struct cgroup_subsys_state *css_pos, *css; + unsigned long usage, max_usage = 0; + unsigned long pagecache = 0; + int ret = 0; + + if (root_memcg) + root_memcg = bpf_get_mem_cgroup(&root_memcg->css); + else + root_memcg = bpf_get_root_mem_cgroup(); + + if (!root_memcg) + return 0; + + css = &root_memcg->css; + if (css && css->cgroup == cgrp) + 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_descendants) + continue; + + memcg = bpf_get_mem_cgroup(css_pos); + if (!memcg) + continue; + + /* + * If any task in the subtree is already exiting due to OOM, + * back off and let it release memory. + */ + if (mem_cgroup_has_oom_victim(memcg)) { + bpf_put_mem_cgroup(memcg); + ret = 1; + break; + } + + usage = bpf_mem_cgroup_usage(memcg); + pagecache = bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES); + + if (usage > pagecache) + usage -= pagecache; + else + usage = 0; + + if (usage > max_usage && mem_cgroup_killable(memcg)) { + max_usage = usage; + if (chosen) + bpf_put_mem_cgroup(chosen); + chosen = bpf_get_mem_cgroup(&memcg->css); + } + + bpf_put_mem_cgroup(memcg); + } + bpf_rcu_read_unlock(); + + if (ret || !chosen) + goto put_chosen; + + bpf_for_each(css_task, task, &chosen->css, CSS_TASK_ITER_PROCS) { + struct task_struct *t = bpf_task_acquire(task); + + if (t) { + if (!bpf_oom_kill_process(oc, t, "bpf oom test")) + ret = 1; + bpf_task_release(t); + } + } + +put_chosen: + if (chosen) + bpf_put_mem_cgroup(chosen); +exit: + bpf_put_mem_cgroup(root_memcg); + + return ret; +} + +SEC(".struct_ops.link") +struct bpf_oom_ops test_bpf_oom = { + .name = "bpf_test_policy", + .handle_out_of_memory = (void *)test_out_of_memory, +}; -- 2.56.1.911.g883d8bcb20-goog