From nobody Wed Dec 17 08:19:34 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04650C77B7C for ; Sat, 6 May 2023 03:16:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231182AbjEFDQK (ORCPT ); Fri, 5 May 2023 23:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjEFDQI (ORCPT ); Fri, 5 May 2023 23:16:08 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31BF672BC for ; Fri, 5 May 2023 20:16:07 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6439f186366so1019574b3a.2 for ; Fri, 05 May 2023 20:16:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1683342966; x=1685934966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=12hdTKRnWgWYBI/c8XKFkpu0HU3jtNl5td2foqM9iPY=; b=MzphiRViPhHlc280608eePs8ynETZ2b7P+HwIIxB6+Z/axscsWkRnhL10NE6yugEkM caE/rr7mt25gIyCHXJcV6rwlKwuI7CWVHAjuLk4WlRoRFZj1GmmQJztnYFG1uGKKXKFb 3jOPICfSc48ikntdLzz7FHzwlKmeaN6X3stTiGaWGxU0vK2SYh2qIPmaMMaAivD9oiBh 4T9Wi0G4OE/g8G6OIOWIMquxnbgv+K9B+D5y70sDbNcQbRCugXELWHK9E11l+yj6tVWY 5r4UwCf3Z23FAw5nshH11I8Bkgzg4s/LGVhRLbShRHDnDWbjGiW+2N3uTtQ5EgARTdBP GLTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683342966; x=1685934966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=12hdTKRnWgWYBI/c8XKFkpu0HU3jtNl5td2foqM9iPY=; b=OilgKKmvm2Osqb6/SJE3CJliSogX9FKkDuRaNdY8vfXCBQPzXq3dusq1RdmSl/La2K U7z3qdnPN4A3c2fsGF8CfA2XWa2eyrjLY3JUg4p/7o3H4Qqj6MPWI1/cb9AxK266n9d6 mRQ6HREmILSVRB4s6lJkUoWItJLqmDES5En7U2yFy4lX4ol5YMs8P/ANEZcgbRjVboRg O+Yb2DAz3mbIhDYA0o9nvQPxnvirUTRc/Byd1OSrTfgyFmBq3rPFnSpA/94RjPJFJ5Bz NWUVbi66TYfq19uivhG9U35geIX/pMAe8GW6iFVI+uLaNKWSbSa31msvSvFWf/cWN3d9 VF1A== X-Gm-Message-State: AC+VfDzsN1EqUUT3v3IyuwNARl/bm6m9JYQZXNvzc+jdSDav26j6N71l 4r4/++jjFZxsrw+LLr0TOhPm6g== X-Google-Smtp-Source: ACHHUZ4Corn9SYGDaNfFKrHwTdh7kiOsEP/Mr3gVK9kadXF5bNntSJBGGkp5HLEkIY4hCqufVory4g== X-Received: by 2002:a05:6a20:7484:b0:f8:a493:5290 with SMTP id p4-20020a056a20748400b000f8a4935290mr4674612pzd.20.1683342966713; Fri, 05 May 2023 20:16:06 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id j1-20020aa783c1000000b0063a1e7d7439sm2256663pfn.69.2023.05.05.20.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 May 2023 20:16:06 -0700 (PDT) From: Feng zhou To: martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v7 1/2] bpf: Add bpf_task_under_cgroup() kfunc Date: Sat, 6 May 2023 11:15:44 +0800 Message-Id: <20230506031545.35991-2-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230506031545.35991-1-zhoufeng.zf@bytedance.com> References: <20230506031545.35991-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou Add a kfunc that's similar to the bpf_current_task_under_cgroup. The difference is that it is a designated task. When hook sched related functions, sometimes it is necessary to specify a task instead of the current task. Signed-off-by: Feng Zhou Acked-by: Yonghong Song --- kernel/bpf/helpers.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index bb6b4637ebf2..a128fe0ab2d0 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2149,6 +2149,22 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cg= id) return NULL; return cgrp; } + +/** + * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, = test + * task's membership of cgroup ancestry. + * @task: the task to be tested + * @ancestor: possible ancestor of @task's cgroup + * + * Tests whether @task's default cgroup hierarchy is a descendant of @ance= stor. + * It follows all the same rules as cgroup_is_descendant, and only applies + * to the default hierarchy. + */ +__bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, + struct cgroup *ancestor) +{ + return task_under_cgroup_hierarchy(task, ancestor); +} #endif /* CONFIG_CGROUPS */ =20 /** @@ -2400,6 +2416,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | K= F_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_SET8_END(generic_btf_ids) --=20 2.20.1 From nobody Wed Dec 17 08:19:34 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B85C3C77B7C for ; Sat, 6 May 2023 03:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231796AbjEFDQV (ORCPT ); Fri, 5 May 2023 23:16:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231202AbjEFDQS (ORCPT ); Fri, 5 May 2023 23:16:18 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 484CD7D9D for ; Fri, 5 May 2023 20:16:15 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-64388cf3263so1856330b3a.3 for ; Fri, 05 May 2023 20:16:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1683342975; x=1685934975; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=to2GelvfQOYiPdEg5VbR1Ww7puasvELAAcMjuLN9bQo=; b=G4QwcUrf6CVw+EAosRd5hW/CMdORTsw+ty+TQdl+aknip9mItDPNTCXoQfaBkB64o5 5qsNWoUqCTVTVKM91fj30sbrlZuy2+NT6nJK7vyA4bMIIZvkCWX0WsTTpycWay+nHGNI hGHhULaLo4mWcJh/FXcgoD6zz200wj17BEtjTMfuxYYar7aQNPqKuSKEOgrxMEv6mnDA tdCFSyOcgnssJCIYjOesqZq6mjBKCYa/socXtbEwhlsJGLGclRNVAOOFPLz5pvMCzSfl 6jAfuLQQrJP7A93ebgzne/mlxsTcRoPbC1tmxPDliJK3okxfMZPLvW98EqV7GY5YPiKY LLZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683342975; x=1685934975; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=to2GelvfQOYiPdEg5VbR1Ww7puasvELAAcMjuLN9bQo=; b=f486i3qICJRs+B6tMwO3aBoJLN23+rTRKMKpKnG/BUBVQbMyUbXRGVwZJbMHEOJXF5 XPPF26UIPqx4YXnznmsBpDSwtqdSKjA81kE7JeRPe3zS8D0ozUbC8McwsZB33iGCnR0f yZN52/RHZTd+mHm08Spes2hTIEdSQrmpGqiKO5EM1eWZ3wFYDwuPjGwppc7GmrdjzzKT uzmITe/wQfWKdyX5aCQuKhKAfI9l+3EUh9heLgJ5K9gKkN/+P7ve0WUqof4FuUd8dB21 57vnvOhZPY04virDnDBAUFxFwHMFt0cuZg+7MN5HFDtwIubO9sEAWADSSU9AVvRnLWJU lq0w== X-Gm-Message-State: AC+VfDz2loSgqurYnit/BSZVyIjASLcQJ4pVP5vMkOpsvpTFTn/QKiLf PQrUY5kjWTYdkY0vIOFaU4RnHg== X-Google-Smtp-Source: ACHHUZ4XixT8yEXOL0eemh5CgkA4zUbrmyVnaZLiFvrr/bwxmMpceRiwYGf9XVCGNFA/F9Zyg3JKwA== X-Received: by 2002:a05:6a00:a8d:b0:63a:ece0:48d0 with SMTP id b13-20020a056a000a8d00b0063aece048d0mr5556731pfl.28.1683342974764; Fri, 05 May 2023 20:16:14 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id j1-20020aa783c1000000b0063a1e7d7439sm2256663pfn.69.2023.05.05.20.16.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 May 2023 20:16:14 -0700 (PDT) From: Feng zhou To: martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v7 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Date: Sat, 6 May 2023 11:15:45 +0800 Message-Id: <20230506031545.35991-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230506031545.35991-1-zhoufeng.zf@bytedance.com> References: <20230506031545.35991-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou test_progs: Tests new kfunc bpf_task_under_cgroup(). The bpf program saves the new task's pid within a given cgroup to the remote_pid, which is convenient for the user-mode program to verify the test correctness. The user-mode program creates its own mount namespace, and mounts the cgroupsv2 hierarchy in there, call the fork syscall, then check if remote_pid and local_pid are unequal. Signed-off-by: Feng Zhou Acked-by: Yonghong Song --- tools/testing/selftests/bpf/DENYLIST.s390x | 1 + .../bpf/prog_tests/task_under_cgroup.c | 53 +++++++++++++++++++ .../bpf/progs/test_task_under_cgroup.c | 51 ++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_under_cgrou= p.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgrou= p.c diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/sel= ftests/bpf/DENYLIST.s390x index c7463f3ec3c0..5061d9e24c16 100644 --- a/tools/testing/selftests/bpf/DENYLIST.s390x +++ b/tools/testing/selftests/bpf/DENYLIST.s390x @@ -26,3 +26,4 @@ user_ringbuf # failed to find= kernel BTF type ID of verif_stats # trace_vprintk__open_and_load un= expected error: -9 (?) xdp_bonding # failed to auto-attach program '= trace_on_entry': -524 (trampoline) xdp_metadata # JIT does not support calling ke= rnel function (kfunc) +test_task_under_cgroup # JIT does not support calling ke= rnel function (kfunc) diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/t= ools/testing/selftests/bpf/prog_tests/task_under_cgroup.c new file mode 100644 index 000000000000..4224727fb364 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#include +#include +#include +#include "test_task_under_cgroup.skel.h" + +#define FOO "/foo" + +void test_task_under_cgroup(void) +{ + struct test_task_under_cgroup *skel; + int ret, foo; + pid_t pid; + + foo =3D test__join_cgroup(FOO); + if (!ASSERT_OK(foo < 0, "cgroup_join_foo")) + return; + + skel =3D test_task_under_cgroup__open(); + if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open")) + goto cleanup; + + skel->rodata->local_pid =3D getpid(); + skel->bss->remote_pid =3D getpid(); + skel->rodata->cgid =3D get_cgroup_id(FOO); + + ret =3D test_task_under_cgroup__load(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__load")) + goto cleanup; + + ret =3D test_task_under_cgroup__attach(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__attach")) + goto cleanup; + + pid =3D fork(); + if (pid =3D=3D 0) + exit(0); + + ret =3D (pid =3D=3D -1); + if (ASSERT_OK(ret, "fork process")) + wait(NULL); + + test_task_under_cgroup__detach(skel); + + ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid, + "test task_under_cgroup"); + +cleanup: + test_task_under_cgroup__destroy(skel); + close(foo); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/t= ools/testing/selftests/bpf/progs/test_task_under_cgroup.c new file mode 100644 index 000000000000..56cdc0a553f0 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#include +#include +#include + +#include "bpf_misc.h" + +struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym; +long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancest= or) __ksym; +void bpf_cgroup_release(struct cgroup *p) __ksym; +struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +const volatile int local_pid; +const volatile __u64 cgid; +int remote_pid; + +SEC("tp_btf/task_newtask") +int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_fla= gs) +{ + struct cgroup *cgrp =3D NULL; + struct task_struct *acquired; + + if (local_pid !=3D (bpf_get_current_pid_tgid() >> 32)) + return 0; + + acquired =3D bpf_task_acquire(task); + if (!acquired) + return 0; + + if (local_pid =3D=3D acquired->tgid) + goto out; + + cgrp =3D bpf_cgroup_from_id(cgid); + if (!cgrp) + goto out; + + if (bpf_task_under_cgroup(acquired, cgrp)) + remote_pid =3D acquired->tgid; + +out: + if (cgrp) + bpf_cgroup_release(cgrp); + bpf_task_release(acquired); + + return 0; +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.20.1