Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1977211rwr; Fri, 21 Apr 2023 02:20:52 -0700 (PDT) X-Google-Smtp-Source: AKy350ZpiU8g7TwSUinxvA5LBC9lC9jij3IFwFeEnVHT70zVMDJEA9RR/iD5T9O5pxe80qLfdqDd X-Received: by 2002:a05:6a00:22c3:b0:639:9089:798e with SMTP id f3-20020a056a0022c300b006399089798emr5968531pfj.19.1682068851695; Fri, 21 Apr 2023 02:20:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682068851; cv=none; d=google.com; s=arc-20160816; b=QLgbpFZ+9Jwxyo4E2gJRt4vkUzMX2x/soRmXkmVsVI9QhPT2B6aBsy2swcls6FdBQq Iene+6IwWjbIhqhuAUF8un0bnqRmHJhM4F6T4muHC4Po3IBvHaREae5+vTlGYwiUoxw2 JyjzzXmEK8ku3qAF5HFKxsVf+uA1DU68I76qiIQRTyZTxZG5AKdc1JXbHHOH3VhB5cvz t2PT+RMluuE//DZlnhQ6zMYw7XHjLGLqQmt83TygIINRy9jF/rMj1Zq9Q1c8c25Mevob MSZRhSJHN7FPfpAmVeHLBS0sA4atD4+jm8SrWY1mFSL2xc9qSLru6KT59IcXc/P7oK3c PDpg== 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=t0gJQV+jBsuJN+dqapwDGx8wuqSDbN1mALWddpdSsEk=; b=bYZSNPRGyuPHyS0JyNop2sUv+mWcTENx93DGrdBwQhMvZ6vOtXe20S3Yv3Yi8JeEZv 96PK34qAtig2h+hh3LE2btXdnspTcuHUNRCd3hkFiQ9A8+zSyouyzKOXpEMXRs4bvYoe e8ik17N7jVeRSFhvwMVp6Ij2QJNy3put1QnZDX6TMxU4RwL2A4C/j9TVsl7/rcpobT4A uoKEumulso9Z4ycNSQtTM6I5K6RQ4hd08qoQgELLuzauirovv+5+oHoib7d+4VWaTdGW hewJBZ58lzpGqz4Z4cuULqNcvo7dPDczz/Pxbr2gnJy6+Ja65ZDRgzjuGjbgVTcd+kKy EBcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=S5Ivxp96; 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 r25-20020a6560d9000000b004faf341b31asi3879445pgv.196.2023.04.21.02.20.35; Fri, 21 Apr 2023 02:20:51 -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=S5Ivxp96; 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 S231869AbjDUJG0 (ORCPT + 99 others); Fri, 21 Apr 2023 05:06:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231676AbjDUJGP (ORCPT ); Fri, 21 Apr 2023 05:06:15 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7908C9EDA for ; Fri, 21 Apr 2023 02:05:47 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2472dc49239so1721957a91.1 for ; Fri, 21 Apr 2023 02:05:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1682067947; x=1684659947; 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=t0gJQV+jBsuJN+dqapwDGx8wuqSDbN1mALWddpdSsEk=; b=S5Ivxp968YSA3NDLM8BeS1NCb2DfvXA3IuISPyCNzeNTePZMImiqO1QFMplQ3aXvDz /JvtVHFnfcDqRSLbbaCRYizZ6eVphwrfwkjJf7dk2xAGd6ln9qyu2KFaV5fIQGQLEGbh UFTjg240tmdYVaKwG8OZ6gX0sCfvi3mqh0IFgjtv3fmK0Ctvild8XBJs1iZoKvJFwu54 qQqD2rnDRafbasd6xUreABRj/ccs0fL2FTpiONAYRciJzBzv3/2xzI1k/ZZ/gFwxgUY5 HePnsiegALfcpVVlCX9aIdraq+Dy+PNzgb9CdkwgZPAqjAbqksx1axNBgTMMnjnnUNXn zPMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682067947; x=1684659947; 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=t0gJQV+jBsuJN+dqapwDGx8wuqSDbN1mALWddpdSsEk=; b=gTN0oncV2w5FaVWfLc2afc1zmU+Js/zvW368AHribxuHm8y9w4dvjQjxPWqiKTpLxd juhuwEIrY8ZxlyN5xpxoe88CkLgr4pO1p6hTEt2EPeGXIfgsmzXWjkoICQ6LEuKiWAUB iFTYTG/XCjIJkrUH4ZqhJO+QYHlGonsSGyeOtp+Q4UXJsYyq2H19y5MFCuAOSREXfNg0 koSpFJ0BtaOjFf15zJ7ya9cUKzPhABwL1ARQqvKAy0VyBAeTVALYxiSI3W6o8FBg0vMv s76vGMIWGA2X7Y4PsVz1Ir+QO1i6RVd9LkubtqJ5kmZguniSZy7yIqj7der83G1S5+oO ibDw== X-Gm-Message-State: AAQBX9f9i1uPw9BQQOkOgckDN1SPalcOEl3rMojA+ykAlanPS6+DXAAj yNFw+wb/+QU/rCj6piFYznVFeQ== X-Received: by 2002:a17:90a:3004:b0:246:681c:71fd with SMTP id g4-20020a17090a300400b00246681c71fdmr4294867pjb.6.1682067946936; Fri, 21 Apr 2023 02:05:46 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id t3-20020a17090a950300b0024796ddd19bsm4192309pjo.7.2023.04.21.02.05.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Apr 2023 02:05:46 -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 v2 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Date: Fri, 21 Apr 2023 17:04:03 +0800 Message-Id: <20230421090403.15515-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230421090403.15515-1-zhoufeng.zf@bytedance.com> References: <20230421090403.15515-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,URIBL_BLOCKED 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 pid which call the getuid syscall 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 getuid syscall, then check if remote_pid and local_pid are equal. Signed-off-by: Feng Zhou --- .../bpf/prog_tests/task_under_cgroup.c | 46 +++++++++++++++++++ .../selftests/bpf/progs/cgrp_kfunc_common.h | 1 + .../bpf/progs/test_task_under_cgroup.c | 40 ++++++++++++++++ 3 files changed, 87 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/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c new file mode 100644 index 000000000000..bd3deb469938 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#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 = -1; + + 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->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; + + syscall(__NR_getuid); + + test_task_under_cgroup__detach(skel); + + ASSERT_EQ(skel->bss->remote_pid, skel->rodata->local_pid, + "test task_under_cgroup"); + +cleanup: + if (foo) + close(foo); + + test_task_under_cgroup__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h b/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h index 22914a70db54..41b3ea231698 100644 --- a/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h +++ b/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h @@ -26,6 +26,7 @@ struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym; struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; +int bpf_task_under_cgroup(struct cgroup *cgrp, struct task_struct *task) __ksym; static inline struct __cgrps_kfunc_map_value *cgrps_kfunc_map_value_lookup(struct cgroup *cgrp) { 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..e2740f9b029d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Bytedance */ + +#include +#include +#include +#include + +#include "cgrp_kfunc_common.h" + +const volatile int local_pid; +const volatile long cgid; +int remote_pid; + +SEC("tp_btf/sys_enter") +int BPF_PROG(sysenter, struct pt_regs *regs, long id) +{ + struct cgroup *cgrp; + + if (id != __NR_getuid) + return 0; + + if (local_pid != (bpf_get_current_pid_tgid() >> 32)) + return 0; + + cgrp = bpf_cgroup_from_id(cgid); + if (!cgrp) + return 0; + + if (!bpf_task_under_cgroup(cgrp, bpf_get_current_task_btf())) + goto out; + + remote_pid = local_pid; + +out: + bpf_cgroup_release(cgrp); + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1