Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1311690rwr; Fri, 5 May 2023 12:05:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5SfFdTTLCt86LZaxbV1Fk9VjWSUe3Qr3E4Dsw+wPB6ncAM0+BnNM490ezIIKI1Dvcnp6j/ X-Received: by 2002:a17:903:124f:b0:1aa:d292:3814 with SMTP id u15-20020a170903124f00b001aad2923814mr2701074plh.67.1683313539989; Fri, 05 May 2023 12:05:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683313539; cv=none; d=google.com; s=arc-20160816; b=x083Eokf1QBm9hpeyLEI7Jj9YUsgXSKyjhj5ce4WhcRQRxHXSFvAY2dk8+90oK3HP8 7nagjEZWCxYBttcwHNNR66mp4+Q3r1cBfmbWw2SDnp3Gn4HQYl+rdn3bnNdJCV5s4NP5 P2hwTa3a4BNpHGcx+zxWu58N3AwVLIwtlBNUecOU2iiu6bUOkkZzXt+qoniC1apPTvLM 8vLXKfQofYkppv2hMHnfaUBudxdEuMdnZmPJtBBFQuOTTC6xD9138yUQLm7aLLiVwl5Z lw8T6X88iRrl36h2ZIYVMOB7oB0DwRpzU4gdhZQl+4vhah9qgiZYpJoPVinWi5fpRU7X wEoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=GEImjqo4Dp5/B90VuSOXELs+WXxVvm2HyH+HGBF0hQ4=; b=emoecAKkKNaHkBmKTs+1MyJXgTpUGVqPtAWyvX/RCuOkiHxIwwhOARsbECqnKdZCnf k+BLpGE4VPNCthe0UHIQtinmchWDRCqQCGQadF7Wgs0513SnQDegUQiRI3P3TtEy+hDX K8Nt997NMkcLtzxlYDZ1MVRxiyRwFeTdr1BNFQm/Q1Z5gwi9Z0vGoDRp4Z3GfRwXrZqy 1r6ckr3j0zS1nTtpJkU8jks5zL30vkDGgx/u2B1m2/xvIaE7EiUKmKx0eIQmdnfzK7Oz q/jIaOvUmxCwh8VZwFsq7dc1fdrFvkyJij3J7N+JhuJU0vn95NBX+3rh7d6k98PX75M9 sR3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=jjFYaFDY; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d8-20020a170902cec800b0019e57f5a5aesi2383396plg.567.2023.05.05.12.05.25; Fri, 05 May 2023 12:05:39 -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=@gmail.com header.s=20221208 header.b=jjFYaFDY; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233145AbjEESs4 (ORCPT + 99 others); Fri, 5 May 2023 14:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232168AbjEESsy (ORCPT ); Fri, 5 May 2023 14:48:54 -0400 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E0A018FD3; Fri, 5 May 2023 11:48:52 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2ac8ee9cf7aso3465031fa.2; Fri, 05 May 2023 11:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683312530; x=1685904530; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GEImjqo4Dp5/B90VuSOXELs+WXxVvm2HyH+HGBF0hQ4=; b=jjFYaFDYSeUzx3kAeBOpfqAqx6gQ1SaXr4s9REGSLNefWd6wqSeMnfAEGy2qu0f76u OESnuRcyj/o4EHm6f/OTlLTQ40tmR0nAT7ogeCIlUFA9AfMn+RTOICptlXuELEQRTwCi kopVOWGXYsfuSOqpW37VAZWSoOdyYmU6OQ9hko9x/vxwFQG2w9S5vUdkQHoVcWzmJhPg 20Q4V4e9dAwF136sqXxlO3WJZiyY/VeDoPcFseHKHajSkgiedStXjcxr+JEW4GJwyajT VjpGtn3//fA87LEQPhIoiAQRabr/77dGtEv5Aa9rBipWSfuV9VVoP2I65ezAzsFXth62 K8vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683312530; x=1685904530; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GEImjqo4Dp5/B90VuSOXELs+WXxVvm2HyH+HGBF0hQ4=; b=iuMl7jahLdm4DXOdQTSgNS6oohvq5IuRk1rCYX3M0DU/QyODmMdPMyGduVDiHD1kQd NF9HQJ6ZLlwgRWMkPvN5uNhrCTn7ivBR9RGWrCZA/0TBsiG5onuCBiNJANfFOlgaBx/N riQZIc/5+MOcmwJCCCVPNXdTCdDJBiT1iVDMA9QU2Q6hHTj//qeI3LDO7MkTMbsK7uME Zl1NtWRyzzHEZHjozVH+4VOZ4/iIi+D4KC4q6uuTbv7aMhrFHqGdu8Eaup3xPLTDZd90 J46uiyK4KQmKxjgArXMqOPqjLhX7M+VALcxDhVLsWh9vfvm1TbMeXzlGAVnOjLqXhCGo xNpA== X-Gm-Message-State: AC+VfDyGJCXkHq770/TUTqNPZSAl15j9t3NqlWaqm4URz7KZFtWrpmKQ 8LfayZlEtTbxb+cxJGsO4/85VItnnX9TWbE3Q+E= X-Received: by 2002:a2e:9d17:0:b0:2ac:7958:ed34 with SMTP id t23-20020a2e9d17000000b002ac7958ed34mr639661lji.45.1683312530155; Fri, 05 May 2023 11:48:50 -0700 (PDT) MIME-Version: 1.0 References: <20230505060818.60037-1-zhoufeng.zf@bytedance.com> <20230505060818.60037-2-zhoufeng.zf@bytedance.com> In-Reply-To: From: Alexei Starovoitov Date: Fri, 5 May 2023 11:48:38 -0700 Message-ID: Subject: Re: [PATCH bpf-next v6 1/2] bpf: Add bpf_task_under_cgroup() kfunc To: Yonghong Song Cc: Feng Zhou , Hao Luo , Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 On Fri, May 5, 2023 at 11:44=E2=80=AFAM Yonghong Song wrote: > > > > On 5/5/23 12:18 AM, Feng Zhou wrote: > > =E5=9C=A8 2023/5/5 14:58, Hao Luo =E5=86=99=E9=81=93: > >> On Thu, May 4, 2023 at 11:08=E2=80=AFPM Feng zhou > >> wrote: > >>> > >> <...> > >>> --- > >>> kernel/bpf/helpers.c | 20 ++++++++++++++++++++ > >>> 1 file changed, 20 insertions(+) > >>> > >>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > >>> index bb6b4637ebf2..453cbd312366 100644 > >>> --- a/kernel/bpf/helpers.c > >>> +++ b/kernel/bpf/helpers.c > >>> @@ -2149,6 +2149,25 @@ __bpf_kfunc struct cgroup > >>> *bpf_cgroup_from_id(u64 cgid) > >>> 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 > >>> @ancestor. > >>> + * 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) > >>> +{ > >>> + if (unlikely(!ancestor || !task)) > >>> + return -EINVAL; > >>> + > >>> + return task_under_cgroup_hierarchy(task, ancestor); > >>> +} > >>> #endif /* CONFIG_CGROUPS */ > >>> > >> > >> I wonder in what situation a null 'task' or 'ancestor' can be passed. > >> Please call out in the comment that the returned value can be a > >> negative error, so that writing if(bpf_task_under_cgroup()) may cause > >> surprising results. > >> > >> Hao > > > > Hmm, you are right. As kfunc, the NULL value of the parameter is judged= , > > and bpf verify will prompt the developer to add it. There is really no > > need to add this part of the judgment. See other people's opinions. > > Thanks for pointing out Hou. > > Currently, bpf_task_under_cgroup() is marked as KF_RCU. > > Per documentation: > 2.4.7 KF_RCU flag > ----------------- > > The KF_RCU flag is a weaker version of KF_TRUSTED_ARGS. The kfuncs > marked with > KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier > guarantees > that the objects are valid and there is no use-after-free. The pointers > are not > NULL, but the object's refcount could have reached zero. The kfuncs need = to > consider doing refcnt !=3D 0 check, especially when returning a KF_ACQUIR= E > pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should very > likely > also be KF_RET_NULL. > > > The pointer cannot be NULL, so the following line of code can be removed: > >>> + if (unlikely(!ancestor || !task)) > >>> + return -EINVAL; Right. With KF_RCU the verifier guarantees !=3D NULL. Let's get rid of this check. This will make the return value clean. > I think we do not need to check refcnt !=3D 0 case since ancestor and > task won't go away. correct. > In the example of second patch, both arguments are TRUSTED arguments > which is stronger than RCU, so the test itself is okay. > I am considering whether we should enforce arguments of the kfunc > to be KF_TRUSTED_ARGS, but I think esp. in some cases, cgroup > might be RCU protected e.g., task->cgroup->dfl_cgrp. So leaving argument > requirement as KF_RCU should be better. +1