Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1729487rwr; Fri, 28 Apr 2023 00:35:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ72Q4eGeeVWFOyy9PM73NY1jaghC+uLqYUcODs6LPt/G30SpwSeWZGMgNJ53+Y9am5SDZlb X-Received: by 2002:a17:90a:1fcb:b0:24b:fda8:4a1f with SMTP id z11-20020a17090a1fcb00b0024bfda84a1fmr5503108pjz.7.1682667304405; Fri, 28 Apr 2023 00:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682667304; cv=none; d=google.com; s=arc-20160816; b=cGnXHcXlGYbT1Hofci1rCyKktq3IHf5v7Wh7GkYsSgMlL6HJTMCgQLh3xPMEEgOGR0 sliru7jM6XEtsm2/INmOg12yumMGf/Zj/+gmpUC6BXB2nlXOaNPna0GgJ2ydQsUcf8Nz UiZhX8j4MxFX9aw7/dCtuYL69P/HEUHHiFSFFj9NqooLMYDLvO6nFPDvhwnek8EjVyA/ kliEaewZModaOsapmEOJ3V15i5PAc7phrYGJ1SDPsuebwNbp8N4K8JjXCT/uFPtQ3vC0 wkjH2LmVphcHPaPF1Gn681h/zU9yMtKOpcXl2X9+H5/l+cS5LyohXGA9seYySObH2QxK /7NQ== 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=0BVJHOpXJP2O2nDTKA8p24jt3D3+BhxA0VLRakwV7yc=; b=FBy2+rRQgxHftwZVL7OlV1pthPlZboStuPsKrvLtVQHiPyGjsNGdCMZWkHQvJLjTAq egxFUbgXj5e3/20EJloSRzCST4cF/QvIlrXWW5OQAnQ8bXMJEe/QXOqquZkIw9b6U2yX x0EJvipT9+MmN8fiMR5nhSwQM8YY1OoIMZhpSTXM0dBbK+iKRCyIFA8TvLUCnjIYNMFR AyuRjWv6Uq2MJtBdeufgrV+VdExrzunIGQZi2RHumzTAhtRk6gAltbWyLK3fjuNbYANV xu3WT6j7J+uSTH8heulG6ciWGen6Ev63WzV4elDT8WFPA5xxKoqiRDPVHPKLNSVZ/lhT 2ZZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=TK4l8iwo; 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 w185-20020a6382c2000000b0051b35caf3fesi16724488pgd.239.2023.04.28.00.34.52; Fri, 28 Apr 2023 00:35:04 -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=TK4l8iwo; 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 S1345489AbjD1HSp (ORCPT + 99 others); Fri, 28 Apr 2023 03:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345490AbjD1HSf (ORCPT ); Fri, 28 Apr 2023 03:18:35 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69857449D for ; Fri, 28 Apr 2023 00:18:06 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-64115e652eeso10497009b3a.0 for ; Fri, 28 Apr 2023 00:18:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1682666286; x=1685258286; 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=0BVJHOpXJP2O2nDTKA8p24jt3D3+BhxA0VLRakwV7yc=; b=TK4l8iwoo83rRIEhzs+Tk9Ibi88EuLo/u5UJ75l3+iPRCigxhrfpstyqVgZDBekxpR 8kfPMPMikgTGQqxu5wyv+Gv7tqy93I4MPfUtveCZU1vtfXaK+WCRXYJV8UG0xbvtGJDP 6qGV3xFWMPCM1B5WFB325Dw2SXNaeLSKVVAg32S7ep+fq2taYlU4PlmtwR4IrgBwNgr2 m6hlyP9PlWz60HX5nNh6tNKHx0dp3v22K+XB8xd5AwFCpXMKj3yB7WWRylRzkYk+rOs1 qhMTw/YHzlzLmo/t67R10fbsDK2pWdSVq4uGKbELdaBQQBW+kq8AvScGTgnEeiCZpuiY nxfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682666286; x=1685258286; 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=0BVJHOpXJP2O2nDTKA8p24jt3D3+BhxA0VLRakwV7yc=; b=SVvD9a6rhyRlButV1+803H2nHdMES5EUndbG4pWifhU45vtyqqkrmh84OCFHwafUKu FTDBg1+XRdlpj3+G4x+VihZtF6LbvaYtgki07Ci+0pVIaDIYyRZUk7gAdluNu/gIzDT7 11dtkQ1/n/T0NE4J/MEnvZwxRIQvdfcloyxZwX5W0HZQEOtlqEL1YwuIR08M7KOpV1A2 0y+P61/vVVFckh9xLyGTbSiLITkULo7FbVs7xWQIs0rQ3vPqJySy7MDxZZkpUQIydL7T pmzw5ICMAGn8FTi1GGaT2gtRdU5GxU2CjaGHQ7yHN3vJy7Ll228DEyXP1SAgnrGcjuP1 PAbw== X-Gm-Message-State: AC+VfDyzSryAr1rg2NzSFNb9hDonUkrahisGCUpynh+k1S3Rrxs/ZmaC THKkb7Oq101bb1VbCZbIMt6LiA== X-Received: by 2002:a17:90a:cb81:b0:249:7ad3:e843 with SMTP id a1-20020a17090acb8100b002497ad3e843mr5541110pju.16.1682666285777; Fri, 28 Apr 2023 00:18:05 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id j2-20020a17090a318200b0024739d29252sm14159939pjb.15.2023.04.28.00.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 00:18:05 -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 v4 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Date: Fri, 28 Apr 2023 15:17:37 +0800 Message-Id: <20230428071737.43849-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20230428071737.43849-1-zhoufeng.zf@bytedance.com> References: <20230428071737.43849-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_NONE,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 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 --- tools/testing/selftests/bpf/DENYLIST.s390x | 1 + .../bpf/prog_tests/task_under_cgroup.c | 55 +++++++++++++++++++ .../bpf/progs/test_task_under_cgroup.c | 51 +++++++++++++++++ 3 files changed, 107 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..5e79dff86dec --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c @@ -0,0 +1,55 @@ +// 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 = -1; + 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); + else if (pid == -1) + printf("Couldn't fork process!\n"); + + wait(NULL); + + test_task_under_cgroup__detach(skel); + + ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid, + "test task_under_cgroup"); + +cleanup: + if (foo >= 0) + close(foo); + + test_task_under_cgroup__destroy(skel); +} 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..5bcb726d6d0a --- /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 long 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 = NULL; + + 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 (acquired) + bpf_task_release(acquired); + if (cgrp) + bpf_cgroup_release(cgrp); + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1