Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6712629rdb; Fri, 15 Dec 2023 06:31:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFp2VzhPdG9CPATmZuxHaTqhdOkbp1zSdf2E6k1UtfxGRSF1GcYvLpu5BHFkD/TLiN+I5Wv X-Received: by 2002:a17:902:a5c7:b0:1d0:7844:5086 with SMTP id t7-20020a170902a5c700b001d078445086mr10079885plq.7.1702650700452; Fri, 15 Dec 2023 06:31:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702650700; cv=none; d=google.com; s=arc-20160816; b=a1s2JesVsJTPlz0Hgapjy1H4hcrpxsBLFhOJGGS5CAEQoD9i5vUxpzT8+nLmilM2V3 p/+gbcv0j5+S1yWReBAnZwksYn4p7bF70yPLqvEJpgnD3Jgd0++b/TSDVInfEhndGgoL I66zR9dbZVv1RKawGdEEiHFllkfbyHhPYW/n3kxgc/YepCwAvh02UDhRrd6ncuTiEHZN UQ7yz2UJwS1GFx5KPjoJIFRouFIx7WsE8YLQGZG6syCxl6q6VyMt9DMi2Xu9aPPMbn2M srSY1SdRkcO7xT4OUKxf8lKORhDPIF4tsuHLbWIsYAVCtoVaeIJTmjwXvraltAAs9sBj /Y+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature:message-id; bh=Qe4Vv2WCEvd4ouMbR/Hft2N+D0eJgtFU87sk0W4t3gI=; fh=4fqN0dyDcWlHZgdI31XUVJcahl6/NIVXWfhWu8DsG0s=; b=USYh+NGNFH0BPsh6J8mWbmbEhwIjzjKESb6H5X/qYQTz9Le51h8ThpaRgGiHiG257u FQVfZiFF4J9/ptudzi69oCvzUA7PuKWIGjCHERuPg8Q/r83AWUIwm4NMZxC4pvIPHhLR RJLdHCmXn0vKWZaKu3XDuJgwcAXM9Qdje52QsqPPEN9kHw9hEp0LFSDn8xljWVultXEZ OzCnB+QbxCqmk3SCoQj2VSUs3W9PCpxp4bQc7W3G4L+OEkmzimZGO9frob9bNwb/3bsZ T+of+7zXsdp/J9JuPgEwoFbVRYgh5QUHBOJFJhNLpy9F1K+25lwn5pXnX4bWZeygxquH WbhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Kq4UbYwL; spf=pass (google.com: domain of linux-kernel+bounces-1107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1107-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id g2-20020a1709026b4200b001d2f0bf3dccsi12063792plt.273.2023.12.15.06.31.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 06:31:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Kq4UbYwL; spf=pass (google.com: domain of linux-kernel+bounces-1107-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1107-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 1C929B2294D for ; Fri, 15 Dec 2023 14:31:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE21653AF; Fri, 15 Dec 2023 14:31:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Kq4UbYwL" X-Original-To: linux-kernel@vger.kernel.org Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A7562583 for ; Fri, 15 Dec 2023 14:31:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1702650676; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qe4Vv2WCEvd4ouMbR/Hft2N+D0eJgtFU87sk0W4t3gI=; b=Kq4UbYwLOU0kjnanSGnEYL9yQST4aXjg2RXUgAgjJd73Pkm5str2CvQlKZPVjFJp2aFNrS xBRgtIGGSoCQK4Og5hngQcM4GQhvJqgqCaH13M3d/T9GENasLYbcCezlZLIhdpRF/wAuEq E6OHe8GWPuieH0LVb29wuPwsIcmbVdk= Date: Fri, 15 Dec 2023 06:31:08 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [RFC PATCH v3 1/3] bpf: cgroup: Introduce helper cgroup_bpf_current_enabled() Content-Language: en-GB To: =?UTF-8?Q?Michael_Wei=C3=9F?= , Christian Brauner , Alexander Mikhalitsyn , Alexei Starovoitov , Paul Moore Cc: Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Quentin Monnet , Alexander Viro , Miklos Szeredi , Amir Goldstein , "Serge E. Hallyn" , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org, gyroidos@aisec.fraunhofer.de, Alexander Mikhalitsyn References: <20231213143813.6818-1-michael.weiss@aisec.fraunhofer.de> <20231213143813.6818-2-michael.weiss@aisec.fraunhofer.de> <6960ef41-fe22-4297-adc7-c85264288b6d@linux.dev> <3e085cef-e74d-417b-ab9b-b8795fa5e5c3@aisec.fraunhofer.de> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yonghong Song In-Reply-To: <3e085cef-e74d-417b-ab9b-b8795fa5e5c3@aisec.fraunhofer.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On 12/14/23 12:17 AM, Michael Weiß wrote: > On 13.12.23 17:59, Yonghong Song wrote: >> On 12/13/23 6:38 AM, Michael Weiß wrote: >>> This helper can be used to check if a cgroup-bpf specific program is >>> active for the current task. >>> >>> Signed-off-by: Michael Weiß >>> Reviewed-by: Alexander Mikhalitsyn >>> --- >>> include/linux/bpf-cgroup.h | 2 ++ >>> kernel/bpf/cgroup.c | 14 ++++++++++++++ >>> 2 files changed, 16 insertions(+) >>> >>> diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h >>> index a789266feac3..7cb49bde09ff 100644 >>> --- a/include/linux/bpf-cgroup.h >>> +++ b/include/linux/bpf-cgroup.h >>> @@ -191,6 +191,8 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk, >>> return array != &bpf_empty_prog_array.hdr; >>> } >>> >>> +bool cgroup_bpf_current_enabled(enum cgroup_bpf_attach_type type); >>> + >>> /* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */ >>> #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb) \ >>> ({ \ >>> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c >>> index 491d20038cbe..9007165abe8c 100644 >>> --- a/kernel/bpf/cgroup.c >>> +++ b/kernel/bpf/cgroup.c >>> @@ -24,6 +24,20 @@ >>> DEFINE_STATIC_KEY_ARRAY_FALSE(cgroup_bpf_enabled_key, MAX_CGROUP_BPF_ATTACH_TYPE); >>> EXPORT_SYMBOL(cgroup_bpf_enabled_key); >>> >>> +bool cgroup_bpf_current_enabled(enum cgroup_bpf_attach_type type) >>> +{ >>> + struct cgroup *cgrp; >>> + struct bpf_prog_array *array; >>> + >>> + rcu_read_lock(); >>> + cgrp = task_dfl_cgroup(current); >>> + rcu_read_unlock(); >>> + >>> + array = rcu_access_pointer(cgrp->bpf.effective[type]); >> This seems wrong here. The cgrp could become invalid once leaving >> rcu critical section. > You are right, maybe we where to opportunistic here. We just wanted > to hold the lock as short as possible. > >>> + return array != &bpf_empty_prog_array.hdr; >> I guess you need include 'array' usage as well in the rcu cs. >> So overall should look like: >> >> rcu_read_lock(); >> cgrp = task_dfl_cgroup(current); >> array = rcu_access_pointer(cgrp->bpf.effective[type]); > Looks reasonable, but that we are in the cs now I would change this to > rcu_dereference() then. copy-paste error. Right, should use rcu_deference() indeed. > >> bpf_prog_exists = array != &bpf_empty_prog_array.hdr; >> rcu_read_unlock(); >> >> return bpf_prog_exists; >> >>> +} >>> +EXPORT_SYMBOL(cgroup_bpf_current_enabled); >>> + >>> /* __always_inline is necessary to prevent indirect call through run_prog >>> * function pointer. >>> */