Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp468906rwr; Thu, 4 May 2023 23:18:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6rfNRxGJRqd2z8Z8t6AFy7Ew5vFU1Tv2KRieVbLmefYCQxCev3p2nOU/OT1djEnBCTID+m X-Received: by 2002:a17:902:e744:b0:1a5:1109:f58e with SMTP id p4-20020a170902e74400b001a51109f58emr670872plf.29.1683267497678; Thu, 04 May 2023 23:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683267497; cv=none; d=google.com; s=arc-20160816; b=txTFci9wa2UK5ZR5VgA34NspnXOFi8CHWZKg3V4tO+HcMvH0mtTMYcFTTdhH/p8K9U 39elhzNS2e002BRQtEIs5+VEFwewTmo1hdWpJKn+zgnmMonDJEDkVSfyu9f1sCpJTUy/ mxSm6LoQSZk6UlM7I4RyvL0oKvWLzkcEZOE7JhpwZH1qYkAOThehh71Qa2AqaHQAndrl Vw5ft1LoZeZxpHVvYM9/l18FWm7QLXVJUeOwhh1DpsVC7d5he1pfCj4Lz/huCzN/7Bh3 0Vag2J7JKgQjRCWpmbEtW7B5fKVe5l3OnC/0moV3uCKdoHdL/7MgZh3zdRFalwgqTwm8 6wfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=to2GelvfQOYiPdEg5VbR1Ww7puasvELAAcMjuLN9bQo=; b=iaNNprHNYBdxoZPQNkTdyHLGKHyGjlY5kTQkNjQtaq4/rHWUH/AoPjoeF610OIhZFn QseL1UyCOztHHHQfhojMbt6Zrn7MptySi8jeCV8RJDMAxHqtiEjFODEdZ5kLTwJJB0eh VAw3kMEaSv/1wz/HAqxJN4VYadlfbsQtcJ6mFNZtDw7dus9vD8v1llNGN3TpHwqUuzwm hAxpLhkdy5qJIglnticp76yI0yXoXeFRG6vYM5jcOkMA4yYoTwRgyC9lSpLw9tNl+0tY K96nwYyld6CReQlwSkNrPMGor/EAu/1dLlk2t4WtUHIY/P3EYPII4aY77K357zK1ghJV elog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=giSZniEl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n17-20020a170902d2d100b001ab19e023a1si1044418plc.375.2023.05.04.23.18.01; Thu, 04 May 2023 23:18:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=giSZniEl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230387AbjEEGIy (ORCPT + 99 others); Fri, 5 May 2023 02:08:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjEEGIx (ORCPT ); Fri, 5 May 2023 02:08:53 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63A6415EE5 for ; Thu, 4 May 2023 23:08:46 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-64115e652eeso17481553b3a.0 for ; Thu, 04 May 2023 23:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1683266926; x=1685858926; 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=giSZniElR4IwMglaX8J4DvYJVEoN9PIDwI+g5ofE55e+BQbq+0IL+eL5pOqhO0ciCb eJNGrAiKfCddux7ahLTbRwM22glGy/6T95p55GKxSVI3oihQN7P5y5y/xb/2vbAgkuZn XYtRaOP5bocO/I0ZqTOK6We5w8c65E27MA9ZqSdH3x1I+fWh2kso0l+aTFFARHQT3KgD oYmXnp91OADgI8ooJ/SJz0MYyIh/ViFJ//WkzwqPD3cTbul+r6TpcWY7r46Rl7fJu5Ac CM1UGkDc+CqmkWafJYZw22L+tggPFMse3cMoobg85Opm4sT182vrPghkkei6nDwAGI0p i/Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683266926; x=1685858926; 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=iXmdWwYxze0QngKbopnh6PIBLUhyGZbvcLu9W5VQoMGZXRYr46D+aqCc7BuYrQxH1x Ne3KojRU8xcho+xtTBvnuA9WnMpNZSndLsgRo5CU5CW13Bp9K8lP90s9FfkGqERG+I2A zC8pHVBr1axyf+16y7EHocAQf5Caesr4Hpzxy+10GCCzvaP7y0hqHWLNSvkFj5ybIYFN mhlDFkieAmdIRdqG35Mqs5tlfYjFMJ9rodRIv0UrneNUUbJwK/FWNxfelVsNOOWzdACK sTHupU/aUcPkunqja/KBDZX+H17fXU/NBB3Hg4c43NMbwSnmTTShGBlSB3ORjmlJnFKL aC/w== X-Gm-Message-State: AC+VfDx1YQ0hYzD2xVkGe2BIscSSBOM4G9KYtBW/37ykS+myjsqUbBLs Yh29nKecUTF6OnFiQJpd0C/yfg== X-Received: by 2002:a05:6a00:13a8:b0:63d:6744:8cae with SMTP id t40-20020a056a0013a800b0063d67448caemr833671pfg.2.1683266925850; Thu, 04 May 2023 23:08:45 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id a15-20020aa780cf000000b0063799398eb9sm762160pfn.58.2023.05.04.23.08.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 23:08:45 -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 v6 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Date: Fri, 5 May 2023 14:08:18 +0800 Message-Id: <20230505060818.60037-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230505060818.60037-1-zhoufeng.zf@bytedance.com> References: <20230505060818.60037-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgroup.c diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/selftests/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 unexpected error: -9 (?) xdp_bonding # failed to auto-attach program 'trace_on_entry': -524 (trampoline) xdp_metadata # JIT does not support calling kernel function (kfunc) +test_task_under_cgroup # JIT does not support calling kernel function (kfunc) diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/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 = test__join_cgroup(FOO); + if (!ASSERT_OK(foo < 0, "cgroup_join_foo")) + return; + + skel = test_task_under_cgroup__open(); + if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open")) + goto cleanup; + + skel->rodata->local_pid = getpid(); + skel->bss->remote_pid = getpid(); + skel->rodata->cgid = get_cgroup_id(FOO); + + ret = test_task_under_cgroup__load(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__load")) + goto cleanup; + + ret = test_task_under_cgroup__attach(skel); + if (!ASSERT_OK(ret, "test_task_under_cgroup__attach")) + goto cleanup; + + pid = fork(); + if (pid == 0) + exit(0); + + ret = (pid == -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/tools/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 *ancestor) __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_flags) +{ + struct cgroup *cgrp = NULL; + struct task_struct *acquired; + + if (local_pid != (bpf_get_current_pid_tgid() >> 32)) + return 0; + + acquired = bpf_task_acquire(task); + if (!acquired) + return 0; + + if (local_pid == acquired->tgid) + goto out; + + cgrp = bpf_cgroup_from_id(cgid); + if (!cgrp) + goto out; + + if (bpf_task_under_cgroup(acquired, cgrp)) + remote_pid = acquired->tgid; + +out: + if (cgrp) + bpf_cgroup_release(cgrp); + bpf_task_release(acquired); + + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1