From mboxrd@z Thu Jan 1 00:00:00 1970 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 B7725C77B73 for ; Fri, 21 Apr 2023 02:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233541AbjDUCcg (ORCPT ); Thu, 20 Apr 2023 22:32:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233322AbjDUCcf (ORCPT ); Thu, 20 Apr 2023 22:32:35 -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 348314C1B for ; Thu, 20 Apr 2023 19:32:08 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-63b4dfead1bso1576725b3a.3 for ; Thu, 20 Apr 2023 19:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1682044325; x=1684636325; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=+Os9mukvCSPKKdw4nMw1R9O8vMlREwTtiShtLG9IPKE=; b=HCUz5FFzKFUHDPDgsikCdeCMO1zCq/dlZrN9CoWrMcHhOk5eW5mGuvAW0p45w19CZ9 6DhK5F2Gk1GWEBSlELCPFSghDIlDSe1DGcJsa3XDYcDtdAEhjPD95OxhadNqFqLjy31a AEwqXTmEZj/G96t5M0pUJFRsj3q4EZINhn5IxkcSe8fP6t4MfV3qgawZfZCEP/9uKbME XEmL0ckKkFWmjHQeQG7hPRTo7o4ucGkjtRfjE1LcDP8ADmrc3OOqwoNwl3GOCVz2MgqQ qWNVGhGaMk/y7AJaDYlyWUfw2DAWzlQCWRH/UzMHCoGdZelgd211UAMLzH4/cQqVS8G9 RY2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682044325; x=1684636325; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=+Os9mukvCSPKKdw4nMw1R9O8vMlREwTtiShtLG9IPKE=; b=S87H1eABPKPigASAk42g78/RW0YiInohMZG+yz5qN6rHaYSpTuaR6HDqz9hA8q7ztS 4KFuzcnwEXrqmwdcuABNUI739lMR4u5/i1rD8Uu1BMXR8fwUdGEvaH7mSPlwN8t+BEg8 6RoUf/tzb/FjmIdYV1mR1bjqU6Mcn+8f0IcBJ3NvzjEKlwqH1YvXN/EyNRqSJBjGUBxS 44jQsZJ3WprszMfrLRqvkqj/FffC6zuJN0gEECR1esrgbL1i4q751kd4CKR36sDsq9um +Y9SP7PldQDssMRkHXH83AI42cSHtbSAx1kflEb1W0tQStJP4zNkfHdufW60jEPLeboO xqQg== X-Gm-Message-State: AAQBX9fkzGYi9vXkampEqhrzG3Q+F5H3wh/vPbzaqbMbpTcsIxOQVjdm R+9P71jjPH6ZKrYe0h8sOp3caA== X-Google-Smtp-Source: AKy350an2piqFQ6UOXrk8jJ6gIyDjZgCDQB+P/aZgG82pfL/0ZNRMdlbfqgcUvlglP49ovd9ARWPuw== X-Received: by 2002:a05:6a00:1acd:b0:63d:2aac:7b88 with SMTP id f13-20020a056a001acd00b0063d2aac7b88mr4799316pfv.25.1682044325691; Thu, 20 Apr 2023 19:32:05 -0700 (PDT) Received: from [10.71.57.173] ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id o8-20020a62f908000000b006260526cf0csm1847551pfh.116.2023.04.20.19.31.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Apr 2023 19:32:05 -0700 (PDT) Message-ID: <184a2930-99ee-4cbe-9d9e-2f7d7fa8a2e2@bytedance.com> Date: Fri, 21 Apr 2023 10:31:55 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [External] Re: [PATCH bpf-next 1/2] bpf: Add bpf_task_under_cgroup helper To: Alexei Starovoitov Cc: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mykola Lysenko , Shuah Khan , bpf , LKML , Network Development , "open list:KERNEL SELFTEST FRAMEWORK" , yangzhenze@bytedance.com, Dongdong Wang References: <20230420072657.80324-1-zhoufeng.zf@bytedance.com> <20230420072657.80324-2-zhoufeng.zf@bytedance.com> From: Feng Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 在 2023/4/21 02:22, Alexei Starovoitov 写道: > On Thu, Apr 20, 2023 at 12:27 AM Feng zhou wrote: >> From: Feng Zhou >> >> This adds a bpf helper 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 >> --- >> include/uapi/linux/bpf.h | 13 +++++++++++++ >> kernel/bpf/verifier.c | 4 +++- >> kernel/trace/bpf_trace.c | 31 +++++++++++++++++++++++++++++++ >> tools/include/uapi/linux/bpf.h | 13 +++++++++++++ >> 4 files changed, 60 insertions(+), 1 deletion(-) >> >> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h >> index 4b20a7269bee..3d31ddb39e10 100644 >> --- a/include/uapi/linux/bpf.h >> +++ b/include/uapi/linux/bpf.h >> @@ -5550,6 +5550,18 @@ union bpf_attr { >> * 0 on success. >> * >> * **-ENOENT** if the bpf_local_storage cannot be found. >> + * >> + * long bpf_task_under_cgroup(struct bpf_map *map, struct task_struct *task, u32 index) >> + * Description >> + * Check whether the probe is being run is the context of a given >> + * subset of the cgroup2 hierarchy. The cgroup2 to test is held by >> + * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. >> + * Return >> + * The return value depends on the result of the test, and can be: >> + * >> + * * 1, if assigned task belongs to the cgroup2. >> + * * 0, if assigned task does not belong to the cgroup2. >> + * * A negative error code, if an error occurred. >> */ >> #define ___BPF_FUNC_MAPPER(FN, ctx...) \ >> FN(unspec, 0, ##ctx) \ >> @@ -5764,6 +5776,7 @@ union bpf_attr { >> FN(user_ringbuf_drain, 209, ##ctx) \ >> FN(cgrp_storage_get, 210, ##ctx) \ >> FN(cgrp_storage_delete, 211, ##ctx) \ >> + FN(task_under_cgroup, 212, ##ctx) \ >> /* */ >> >> /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c >> index 1e05355facdc..1e2c3c3e8d5f 100644 >> --- a/kernel/bpf/verifier.c >> +++ b/kernel/bpf/verifier.c >> @@ -7771,7 +7771,8 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, >> break; >> case BPF_MAP_TYPE_CGROUP_ARRAY: >> if (func_id != BPF_FUNC_skb_under_cgroup && >> - func_id != BPF_FUNC_current_task_under_cgroup) >> + func_id != BPF_FUNC_current_task_under_cgroup && >> + func_id != BPF_FUNC_task_under_cgroup) >> goto error; >> break; >> case BPF_MAP_TYPE_CGROUP_STORAGE: >> @@ -7902,6 +7903,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, >> goto error; >> break; >> case BPF_FUNC_current_task_under_cgroup: >> + case BPF_FUNC_task_under_cgroup: >> case BPF_FUNC_skb_under_cgroup: >> if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) >> goto error; >> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c >> index bcf91bc7bf71..b02a04768824 100644 >> --- a/kernel/trace/bpf_trace.c >> +++ b/kernel/trace/bpf_trace.c >> @@ -814,6 +814,35 @@ static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { >> .arg2_type = ARG_ANYTHING, >> }; >> >> +BPF_CALL_3(bpf_task_under_cgroup, struct bpf_map *, map, struct task_struct *, >> + task, u32, idx) >> +{ >> + struct bpf_array *array = container_of(map, struct bpf_array, map); >> + struct cgroup *cgrp; >> + >> + if (unlikely(!task)) >> + return -ENOENT; >> + >> + if (unlikely(idx >= array->map.max_entries)) >> + return -E2BIG; >> + >> + cgrp = READ_ONCE(array->ptrs[idx]); >> + if (unlikely(!cgrp)) >> + return -EAGAIN; >> + >> + return task_under_cgroup_hierarchy(task, cgrp); > We don't add helpers anymore. > Please wrap task_under_cgroup_hierarchy() as a kfunc > that takes two TRUSTED pointers task and cgroup. Will do, thanks.