From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA49A280335 for ; Tue, 13 Jan 2026 21:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768340637; cv=none; b=eDMIa3E4SHdmuhSNahPS6vw1PULQiJj7LyS/CZTacN3Id5txBE+Lb/RQ+BT6J2oi7G5FIMrb7tOIc6f5EpiwZl4Qtlrwzp1kvEBN2BQKoI9kC4+1HF3mropQns8/DGcIx5ZE6YXPCIlUOTbOakuxXeXoD4o4+bIfBqEBHZ+XdAg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768340637; c=relaxed/simple; bh=7bsUtiUgKGUde2s0C0zgl5rK6a734yalkgEEbvzprU8=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=dM7LKexbZnFo+kRvlPi4DRQxcqlathMbhXpIYm8ZHyF32VxjbuvtufcWsNVN5EqTRxu/M7C3JYM4NHw45OrT9ZMQ6GtTRqjWKX2+//Xxc3DQSXXKrpFSHFa0XJhfFJTSj85AMQud+e4jPdQPigDNfWQAr5g/nLOzlsFyt8AuB3I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XX5fxWYo; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XX5fxWYo" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-34c213f7690so5339565a91.2 for ; Tue, 13 Jan 2026 13:43:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768340635; x=1768945435; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=PYn6o6R3QSwAggtOWvfLgCyBT+NI/AbWRIt35aBXUGU=; b=XX5fxWYoh2zuqsT9qzsPCHUujkkeQZyAzxHK+I2nP09zSGwNtSLXyx712YM1O+R0GS nq53ErMS8IL9xqWq4SQWyECVwrJvBcJHP4tDYi86PsuSbLwalkjuEcmFZIbD6F93iI+M RPPvZlFNl5lQZjmV1j7EugBLknfHTh3mi1MRzesiXdO6sRKkT/cV4Yypr6wRfenOqIw/ SD0oWFN0rN1L77mHgc+ZUvRSecUN9GD/szZ8ZlKfwBvZuIvbXVHbk77VnllE5k2EiCSa /TrLMO08tV1mdktmTlI0Ak4ZN4MlhVQCMtdVUH2O+QtWhGYc8A2rl/N3ZZzMzxdMgD9P bX1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768340635; x=1768945435; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PYn6o6R3QSwAggtOWvfLgCyBT+NI/AbWRIt35aBXUGU=; b=Gaaxyy99EuzlLhJcuu97BHP6WvK+WtJ5CoSgkbKKL8g5nUTM51zCyGgsX0ud0TeXN6 djnpMhBg/AzmVI97jNraHXME2Q4YB5eRRxK72I5HHnLWVKWfg+crqJ2GdLfchAtGTLTL c7ESAGjksFPMkbKZvymk4AAt/EzEqjGiln59oTHsYDNWUGku46VbSLOPJc4dJ+aOPV30 yuiw2vXYggqdv8GOpRTgvA4aKpAYI1JZjsoBNiZ10MJlaDVh9Srhj6drIg0HhpJBBm6F QkTQlukO6kVAfiNgBB3yqnsShOssPmrPcaNS3Tu8OSnB5FAYt7r2Uu3iNVfSx5fyetdw LrrA== X-Forwarded-Encrypted: i=1; AJvYcCXmeBzFI4jnYaZgMCarAMXfsfJSwCCtYNlSh7KEbYsxK6FisaAgs2d8FIz+2x6QrUwgxdQOtTM4e+iruy8=@vger.kernel.org X-Gm-Message-State: AOJu0YwGRLhezL6eH6EQA7RZU+qOMP/c54CiaB5rXPqZ5uUkMFGBSQYm nL/tkMRgsNAJxE1LrScUixQCL+s5Qb4yvDqskTk9UczYwxB0putIOcEq X-Gm-Gg: AY/fxX7rLKx6oaQy4feHjB1U0exhs3YfFDcr9euiIZZK3MeNkAIaerELzfuIFWtjt1t lCg5YWwq6KbvdBb2XPBj7KEZ8Z1txFOh9hfzmoMRnIUA1w01k+5LhshNMbOnWrHPjhLkbEvbMS8 OBBBHa3k2DQCayrhfiXb8eUX03ghrqHoMfAESTJrGEzTKvZvytb+23mysjHua+x1zYrMqkVpSDd nPhLqMfj/Ef4u6p26XHPmjmceoQ+AljFUZw5Br7hfH09tNyie6v3Nf/us38Tg2MLQ3qHBgUkWBs gPAnmRMQ9557as07/JRgJgaTql/3nHqAFaBdd0/qSVTHhUOq3kur481Rf4utVuiy+c+cuJtKBBy kETDNk21yv0cZl6FO54OZ9xNLzd17uC2uNKGpzFiqUU2F1TYAmyHRwCRidYfDPNnaryg2JGdZkQ RnMZrdjcYgXCxZxc7v5e5+oCU5jVlQuCg4WrVfpL5h X-Received: by 2002:a17:90b:2e8b:b0:343:7714:4caa with SMTP id 98e67ed59e1d1-35109086381mr473677a91.3.1768340635072; Tue, 13 Jan 2026 13:43:55 -0800 (PST) Received: from [192.168.0.226] ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-351068fe22esm364539a91.2.2026.01.13.13.43.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jan 2026 13:43:54 -0800 (PST) Message-ID: <6a9a60f292e3ce862accd782bd43f8dc2491bca4.camel@gmail.com> Subject: Re: [PATCH bpf-next v1 01/10] bpf: Refactor btf_kfunc_id_set_contains From: Eduard Zingerman To: Ihor Solodrai , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau Cc: Mykyta Yatsenko , Tejun Heo , Alan Maguire , Benjamin Tissoires , Jiri Kosina , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, sched-ext@lists.linux.dev Date: Tue, 13 Jan 2026 13:43:51 -0800 In-Reply-To: <20260109184852.1089786-2-ihor.solodrai@linux.dev> References: <20260109184852.1089786-1-ihor.solodrai@linux.dev> <20260109184852.1089786-2-ihor.solodrai@linux.dev> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.2 (3.58.2-1.fc43) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Fri, 2026-01-09 at 10:48 -0800, Ihor Solodrai wrote: > btf_kfunc_id_set_contains() is called by fetch_kfunc_meta() in the BPF > verifier to get the kfunc flags stored in the .BTF_ids ELF section. > If it returns NULL instead of a valid pointer, it's interpreted as an > illegal kfunc usage failing the verification. >=20 > There are two potential reasons for btf_kfunc_id_set_contains() to > return NULL: >=20 > 1. Provided kfunc BTF id is not present in relevant kfunc id sets. > 2. The kfunc is not allowed, as determined by the program type > specific filter [1]. >=20 > The filter functions accept a pointer to `struct bpf_prog`, so they > might implicitly depend on earlier stages of verification, when > bpf_prog members are set. >=20 > For example, bpf_qdisc_kfunc_filter() in linux/net/sched/bpf_qdisc.c > inspects prog->aux->st_ops [2], which is initialized in: >=20 > check_attach_btf_id() -> check_struct_ops_btf_id() >=20 > So far this hasn't been an issue, because fetch_kfunc_meta() is the > only caller of btf_kfunc_id_set_contains(). >=20 > However in subsequent patches of this series it is necessary to > inspect kfunc flags earlier in BPF verifier, in the add_kfunc_call(). >=20 > To resolve this, refactor btf_kfunc_id_set_contains() into two > interface functions: > * btf_kfunc_flags() that simply returns pointer to kfunc_flags > without applying the filters > * btf_kfunc_is_allowed() that both checks for kfunc_flags existence > (which is a requirement for a kfunc to be allowed) and applies the > prog filters >=20 > See [3] for the previous version of this patch. >=20 > [1] https://lore.kernel.org/all/20230519225157.760788-7-aditi.ghag@isoval= ent.com/ > [2] https://lore.kernel.org/all/20250409214606.2000194-4-ameryhung@gmail.= com/ > [3] https://lore.kernel.org/bpf/20251029190113.3323406-3-ihor.solodrai@li= nux.dev/ >=20 > Signed-off-by: Ihor Solodrai > --- Reviewed-by: Eduard Zingerman > @@ -8715,6 +8730,26 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_pr= og_type prog_type) > } > } > =20 > +bool btf_kfunc_is_allowed(const struct btf *btf, > + u32 kfunc_btf_id, > + const struct bpf_prog *prog) > +{ Nit: I'd just add hook parameter to btf_kfunc_flags(): u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const st= ruct bpf_prog *prog, enum btf_kfunc_hook *hook) and allow passing NULL there, thus avoiding duplicating logic for comm= on hook. > + enum bpf_prog_type prog_type =3D resolve_prog_type(prog); > + enum btf_kfunc_hook hook; > + u32 *kfunc_flags; > + > + kfunc_flags =3D btf_kfunc_id_set_contains(btf, BTF_KFUNC_HOOK_COMMON, k= func_btf_id); > + if (kfunc_flags && __btf_kfunc_is_allowed(btf, BTF_KFUNC_HOOK_COMMON, k= func_btf_id, prog)) > + return true; > + > + hook =3D bpf_prog_type_to_kfunc_hook(prog_type); > + kfunc_flags =3D btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id); > + if (kfunc_flags && __btf_kfunc_is_allowed(btf, hook, kfunc_btf_id, prog= )) > + return true; > + > + return false; > +} > + > /* Caution: > * Reference to the module (obtained using btf_try_get_module) correspon= ding to > * the struct btf *MUST* be held when calling this function from verifie= r [...]