From nobody Sun Feb 8 23:04:18 2026 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (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 E32D020A5EB for ; Mon, 28 Apr 2025 03:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811432; cv=none; b=c0y4u6PtFkY3es1LaG9f2wBzgZNNcVjD2t6d4llaSyAjL0PgiZ5JrgAY6IoxMzQNTyCt/Eo3XYVxfmMBKaYxezDi2GQv+llO1T3KgW+CcYO2On12EQhEdY+xYWM3m/T2bnaWL1jA0BJGEInObwtGneMFFVY4J0NNcVriYav+xmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811432; c=relaxed/simple; bh=9m6dGm3hgSNWG+2WOSHP2KyLWYLAjePv7Lo4oUqmktU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YCfq4MrijBMkJ51Rv55s1k8vSyYKYToSpetBKicwIFuvNEmvug3AyifHYrUnEMC3RcR0mQIz9t7mkVJVqboTJtStaw/lJRfyZMlaJyuVflEPYRi5amtOV802o9NV4S8iTnUCs6h16KYfklwH9MdMzJPU1OFDH1uYQkdzK2E6nSs= 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=WoHk90vS; arc=none smtp.client-ip=95.215.58.177 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="WoHk90vS" 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=1745811429; 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=PMD4alSuF/CQrdJat1BcZoxvyxgjdLtWthYldLJYf1E=; b=WoHk90vSsCT6HqUIwr7zPvav48u2HbF9Fj+LO5seyFrEcCAO/yMDiQn7IpkGImXxA5Cx3W jN5br5GN6WKkrp+RIO/MIO4P0oJHADYQOhd7umOi69q0P/NDHaILQKKI3xxLRvkpkvHRIX Rab1/AQR2q1H72iYJeg+PrWNcLnEGGE= From: Roman Gushchin To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Alexei Starovoitov , Johannes Weiner , Michal Hocko , Shakeel Butt , Suren Baghdasaryan , David Rientjes , Josh Don , Chuyi Zhou , cgroups@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, Roman Gushchin Subject: [PATCH rfc 12/12] bpf: selftests: psi handler test Date: Mon, 28 Apr 2025 03:36:17 +0000 Message-ID: <20250428033617.3797686-13-roman.gushchin@linux.dev> In-Reply-To: <20250428033617.3797686-1-roman.gushchin@linux.dev> References: <20250428033617.3797686-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" Add a psi handler test. The test creates a cgroup with two child sub-cgroups, sets up memory.high for one of those and puts memory hungry processes in each of them. Then it sets up a psi trigger for one of cgroups and waits till the process in this cgroup will be killed by the OOM killer. To make sure there was indeed an OOM event, it checks the corresponding memcg statistics. Signed-off-by: Roman Gushchin --- tools/testing/selftests/bpf/prog_tests/psi.c | 234 +++++++++++++++++++ tools/testing/selftests/bpf/progs/test_psi.c | 43 ++++ 2 files changed, 277 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/psi.c create mode 100644 tools/testing/selftests/bpf/progs/test_psi.c diff --git a/tools/testing/selftests/bpf/prog_tests/psi.c b/tools/testing/s= elftests/bpf/prog_tests/psi.c new file mode 100644 index 000000000000..99d68bc20eee --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/psi.c @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cgroup_helpers.h" +#include "test_psi.skel.h" + +struct cgroup_desc { + const char *path; + int fd; + unsigned long long id; + int pid; + size_t target; + size_t high; + bool victim; + bool psi; +}; + +#define MB (1024 * 1024) + +static struct cgroup_desc cgroups[] =3D { + { .path =3D "/oom_test" }, + { .path =3D "/oom_test/cg1", .target =3D 100 * MB }, + { .path =3D "/oom_test/cg2", .target =3D 500 * MB, + .high =3D 40 * MB, .psi =3D true, .victim =3D true }, +}; + +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 */ + ptr =3D (char *)malloc(desc->target); + if (!ptr) + return -ENOMEM; + + memset(ptr, 'a', desc->target); + + while (1) + sleep(1000); + + return 0; +} + +static int setup_psi_alert(struct cgroup_desc *desc) +{ + const char *trig =3D "some 100000 1000000"; + int fd; + + fd =3D open_cgroup_file(desc->path, "memory.pressure", O_RDWR); + if (fd < 0) { + printf("memory.pressure open error: %s\n", strerror(errno)); + return 1; + } + + if (write(fd, trig, strlen(trig) + 1) < 0) { + printf("memory.pressure write error: %s\n", strerror(errno)); + return 1; + } + + /* keep fd open, otherwise the psi trigger will be deleted */ + 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; + + 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; + } + + if (!cgroups[i].target) { + /* Recursively enable the memory controller */ + err =3D write_cgroup_file(cgroups[i].path, "cgroup.subtree_control", + "+memory"); + if (!ASSERT_OK(err, "enable memory controller")) + goto cleanup; + } + + if (cgroups[i].high) { + char buf[256]; + + snprintf(buf, sizeof(buf), "%lu", cgroups[i].high); + err =3D write_cgroup_file(cgroups[i].path, "memory.high", buf); + if (!ASSERT_OK(err, "set memory.high")) + goto cleanup; + + snprintf(buf, sizeof(buf), "0"); + write_cgroup_file(cgroups[i].path, "memory.swap.max", buf); + } + + 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; + } + + if (cgroups[i].psi) { + err =3D setup_psi_alert(&cgroups[i]); + if (!ASSERT_OK(err, "create psi trigger")) + goto cleanup; + } + } + + return; + +cleanup: + cleanup_cgroup_environment(); +} + +static int run_and_wait_for_oom(void) +{ + int ret =3D -1; + bool first =3D true; + char buf[4096] =3D {}; + size_t size; + + 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; + + 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; + + 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; + } + } + + 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_psi(void) +{ + struct test_psi *skel; + int err; + + skel =3D test_psi__open_and_load(); + err =3D test_psi__attach(skel); + if (!ASSERT_OK(err, "test_psi__attach")) + goto cleanup; + + setup_environment(); + + run_and_wait_for_oom(); + + cleanup_cgroup_environment(); +cleanup: + test_psi__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_psi.c b/tools/testing/s= elftests/bpf/progs/test_psi.c new file mode 100644 index 000000000000..8cbc1e0a5b24 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_psi.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" + +char _license[] SEC("license") =3D "GPL"; + +struct mem_cgroup *bpf_get_mem_cgroup(struct cgroup_subsys_state *css) __k= sym; +void bpf_put_mem_cgroup(struct mem_cgroup *memcg) __ksym; +int bpf_out_of_memory(struct mem_cgroup *memcg, int order) __ksym; + +SEC("fmod_ret.s/bpf_handle_psi_event") +int BPF_PROG(test_psi_event, struct psi_trigger *t) +{ + struct cgroup *cgroup =3D NULL; + struct mem_cgroup *memcg; + u64 cgroup_id; + + if (!t->of || !t->of->kn) { + bpf_out_of_memory(NULL, 0); + return 1; + } + + cgroup_id =3D t->of->kn->__parent->id; + cgroup =3D bpf_cgroup_from_id(cgroup_id); + if (!cgroup) + return 0; + + memcg =3D bpf_get_mem_cgroup(&cgroup->self); + if (!memcg) { + bpf_cgroup_release(cgroup); + return 0; + } + + bpf_out_of_memory(memcg, 0); + + bpf_put_mem_cgroup(memcg); + bpf_cgroup_release(cgroup); + + return 1; +} --=20 2.49.0.901.g37484f566f-goog