From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.12 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359924; b=r/zr4CgbW/gNd0EE8L07fWEgjzKyOnWBRKdju8EK3EJsWy0KJ6oBa/LowjJ1ZZ/VIAai Ckxtvof7E37W8w24xSLuoH8ijqyVaTG5T7ElpBw9IayFfXzpS0zmvWqFqtIhBnNFxdDRA Ga3e4JBYq6L+8GZdxgD5389My9OhQhTWsCQa0SV7/A+83m9hjV8ey3Jd53N2Z3MPvh3SU gch0jzwfff5WElK1yoQKD9ou7Alda2xTEHoQKd9a1fFE+kO7hmZ80+pZBva1ZRKrt5Fzf uFygeK7xA2E0wbsNMSa56+fEk4Vl/3aNj8BMA1F0bu/4yEc5hLq8OaPUxfy9vG61enQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359924; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=PbGjdIHTglgJdreZymjagOMnH+Mha5JLXEYGzA2x3EI=; b=4BaLe2QGWW6R0wgsFJhIaQnALpGA8V5X7Gef5h3qtTkzejh4fCb01dvRecJz/kXm1rWt j/G9HhV4D3xR/C4F5RrL1T9D67eFEPwc8mQojURowKOfm/VX5HySJOMjqom8L20eqCkjs vt8HEX2tr2bhIKZYa9ju6hYuS2V4taAm2K/sQyykBwSSTLGx0wZn+j2/qg3/26y6LTonV KdFNFQaYCpKF81wqN/S7VU4ieNE6CAtUb/w9QDXmDQmOTzGMpWSREIrKxYqzq6pHRIjSZ f+5Zmm3/WRLSSZ14Q89+mpAEly1BZu2sdDPPGdbYn6FCav0iTNuURUxNvTPXB7ocdcw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=hDGUl2RJ; arc=none smtp.remote-ip=95.215.58.12 Received: from mta1.migadu.com (out-12.mta1.migadu.com [95.215.58.12]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45FFC606A33F for ; Fri, 4 Dec 2026 04:52:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45FFC606A33F 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=hDGUl2RJ X-Envelope-To: sched-ext@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=OWnwNQkIocyCOgx6gayV+KiDJeNfywPBRyzBdLC3BKY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796359920; v=1; x=1796964720; b=hDGUl2RJH/t6vBgzZgHHuuB/698PU9xjph0uC5KMy4SXgY11aSyCE2Yw0guWVXDc02vj/tic +F6ro8sZp4nSlbaZSXYPl3Xj8kGYg3esdETcWXqm2htlktvI4G3qL3CHPylW8VmTLEB4qldnZsM erdM/s1aPTgmHMOcN1YHYHzI= X-Envelope-To: sched-ext@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id 419ca27c1b0f7b74; Fri, 04 Dec 2026 04:52:00 +0000 X-Mizu-Trace-ID: 419ca27c1b0f7b74 X-Migadu-Flow: FLOW_OUT Message-ID: Date: Thu, 3 Dec 2026 21:51:55 -0700 X-Mailing-List: sched-ext@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next v3 3/6] libbpf: Resolve kfunc variants by prototype To: Eduard Zingerman , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev References: <20261124224811.16134-1-ihor.solodrai@linux.dev> <20261124224811.16134-4-ihor.solodrai@linux.dev> Content-Language: en-US From: Ihor Solodrai In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026-11-25 1:28 p.m., Eduard Zingerman wrote: > On Tue, 2026-11-24 at 15:48 -0700, Ihor Solodrai wrote: >> libbpf resolves a kfunc declaration by exact name, after dropping its >> last ___ suffix, and only then checks the prototype. The name alone >> picks the kernel function, so the kernel can't serve both prototypes of >> a changed kfunc. For example, vmlinux.h declares: >> >> bool scx_bpf_dsq_move_to_local(u64 dsq_id); >> bool scx_bpf_dsq_move_to_local___v2(u64 dsq_id, u64 enq_flags); >> >> libbpf resolves the second to scx_bpf_dsq_move_to_local() and rejects >> its prototype. Keeping the old prototype as ___v1 and giving the new >> one the unsuffixed name would break old declarations the same way. >> >> Select a kfunc in two steps instead: >> >> 1. Collect the functions in vmlinux and module BTFs with the same >> essential name as the declaration, as CO-RE does for types. The >> "bpf_kfunc" decl tag is ignored. >> >> 2. Resolve the candidate with a compatible prototype. If there is >> none, a __weak kfunc stays unresolved. More than one is an error, >> even for a __weak kfunc. >> >> The prototype check is stricter than bpf_core_types_are_compat(). It >> also compares the names of structs, unions and forward declarations, >> ignoring ___ suffixes. Members aren't compared. Typedefs and qualifiers >> are skipped. For example: >> >> typedef struct foo foo_t; >> >> void kfunc(struct foo *arg); >> void kfunc(struct foo___local *arg); >> void kfunc(foo_t *arg); >> >> are compatible, but not with: >> >> void kfunc(struct bar *arg); >> >> So a declaration with an outdated struct name no longer resolves. For >> example, commit 8740156ad33b ("bpf: Require a BPF cpumask for >> bpf_cpumask_populate()") changed: >> >> before: int bpf_cpumask_populate(struct cpumask *, void *, size_t); >> after: int bpf_cpumask_populate(struct bpf_cpumask *, void *, size_t); >> >> Each declaration now resolves only on a kernel with the same prototype. >> A stacked suffix, as in foo___v2___compat, no longer resolves foo___v2, >> because its essential name is foo___v2. compat.bpf.h now tries foo___v2 >> first. >> >> Module BTFs are now loaded for every object with kfuncs: 0.5 and 1.4 ms >> for 8 and 32 small modules in a VM. Lookup cost per kfunc: >> >> before (hit / miss) after >> sorted, 23 modules 0.14 / 0.65 us 0.8 us >> unsorted (v6.18) 0.1 / 0.5 ms 0.7 ms >> >> Signed-off-by: Ihor Solodrai >> --- > > I have no nits besides what Andrii points out. Hi Eduard. Thank you for the review. > I'm curious if you tried this one the BPF programs corpus shared by Alexei, > to check how many real world programs are broken by this change. Did that after I saw your comment (and had a chance). IMO TL;DR given our prior discussions is that we can accept the breakages. An updated (or additional, depending on what kernels BPF program tries to cover) kfunc declaration on BPF side fixes them. Sloppy prose with the details follows. I think this is useful to have on record for the bots and future reference. I answered your other questions as well, scroll please :) A bot replayed libbpf's kfunc resolution for every corpus [1] object against the BTF of kernels v6.1, v6.6, v6.8, v6.10, v6.12, v6.18, v7.2 and bpf-next. The corpus builds multiple variants of some progs (different compilers etc), giving 672 total BPF objects that declare kfuncs. With this series (v3), 92 of them (14%) have a declaration that no longer resolves on at least one of the tested kernels, for 19 distinct kfuncs. Outside the kernel tree, that's 22 objects in 4 projects: 18 scx schedulers, bpfjailer (2), libbpf-rs (1) and bpf-tutorial (1). Of these objects, only bpfjailer's verity_enforce declares kfuncs without __weak (bpf_verify_pkcs7_signature() and bpf_get_fsverity_digest()), so it fails to load on v6.1 to v6.10. The other declarations are __weak and stay unresolved. If the call is guarded by bpf_ksym_exists(), the program takes its fallback path; otherwise it fails to load. kfuncs objects kernels kernel types before v6.11 15 43 v6.1-v6.10 BPF selftests 15 39 bpfjailer, libbpf-rs, bpf-tutorial 5 4 stacked ___v2___compat names 3 46 v7.2, bpf-next tools/ and selftests/sched_ext 2 28 scx repo 3 18 bpf_cpumask_populate() 1 3 v6.18, v7.2 BPF selftests 1 3 This is what the CI bot reported [2]. Commit cce4c40b9606 ("bpf: treewide: Align kfunc signatures to prog point-of-view") changed these kfuncs in v6.11 from kernel types to the UAPI types that programs pass. A sample, with the v6.10 prototype and the number of objects: bpf_dynptr_from_skb(struct sk_buff *, ...) 25 bpf_dynptr_slice(const struct bpf_dynptr_kern *, ...) 23 bpf_dynptr_from_xdp(struct xdp_buff *, ...) 9 bpf_get_file_xattr(..., struct bpf_dynptr_kern *) 4 bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *, ...) 4 bpf_sk_assign_tcp_reqsk(struct sk_buff *, ...) 1 The other 9 are dynptr and crypto kfuncs taking struct bpf_dynptr_kern. With this series, such a declaration doesn't resolve on v6.1 to v6.10. No single declaration resolves on both sides of v6.11. A program that has to run on both sides can declare both prototypes and choose in the prog: extern int bpf_dynptr_from_skb(struct __sk_buff *, __u64, struct bpf_dynptr *) __weak __ksym; extern int bpf_dynptr_from_skb___old(struct sk_buff *, __u64, struct bpf_dynptr_kern *) __weak __ksym; if (bpf_ksym_exists(bpf_dynptr_from_skb)) bpf_dynptr_from_skb(skb, 0, &ptr); else bpf_dynptr_from_skb___old((void *)skb, 0, (void *)&ptr); Re: scx_bpf_dsq_insert___v2___compat. With this series its essential name is scx_bpf_dsq_insert___v2, while the kernel's scx_bpf_dsq_insert___v2 is a variant of scx_bpf_dsq_insert. So it stays unresolved, and compat.bpf.h falls back to scx_bpf_dsq_insert(). Patch 2 makes compat.bpf.h try scx_bpf_dsq_insert___v2 first; the scx repo needs the same change. [1] https://github.com/4ast/bpf-corpus [2] https://lore.kernel.org/bpf/bdaa5b918a0e59230fcce839fea102b598efeb3a65de54f668c15f29573370df@mail.kernel.org/ > > ... > >> +static __u16 kfunc_type_kind(const struct btf_type *t) >> +{ >> + if (btf_is_fwd(t)) >> + return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT; >> + if (btf_is_enum64(t)) >> + return BTF_KIND_ENUM; >> + return btf_kind(t); >> +} >> + >> +/* >> + * Return 1 if the types match, 0 if they don't, or -EINVAL if a type >> + * can't be looked up or the types nest deeper than depth. >> + */ >> +static int kfunc_type_match(const struct btf *a, __u32 a_id, >> + const struct btf *b, __u32 b_id, int depth) >> +{ >> + const struct btf_type *at = skip_mods_and_typedefs(a, a_id, NULL); >> + const struct btf_type *bt = skip_mods_and_typedefs(b, b_id, NULL); >> + __u16 kind; >> + int i, ret; >> + >> + if (!at || !bt || !depth) >> + return -EINVAL; >> + >> + kind = kfunc_type_kind(at); >> + if (kind != kfunc_type_kind(bt)) >> + return 0; > > This makes enums incompatible with int types, do we want an exception > for such a case? bpf_core_types_are_compat() rejects int vs enum too, I'd leave it consistent. No declaration in the corpus is affected by this. > >> + >> + switch (kind) { >> + case BTF_KIND_STRUCT: >> + case BTF_KIND_UNION: { >> + const char *an = btf__name_by_offset(a, at->name_off); >> + const char *bn = btf__name_by_offset(b, bt->name_off); >> + size_t name_len = bpf_core_essential_name_len(an); >> + >> + return name_len == bpf_core_essential_name_len(bn) && >> + strncmp(an, bn, name_len) == 0; >> + } >> + case BTF_KIND_UNKN: >> + case BTF_KIND_ENUM: >> + return 1; >> + case BTF_KIND_INT: >> + return btf_int_offset(at) == 0 && btf_int_offset(bt) == 0; >> + case BTF_KIND_PTR: >> + return kfunc_type_match(a, at->type, b, bt->type, depth - 1); > > This makes `int *` compatible with e.g. `char *`, do we care? I don't think we care. bpf_core_types_are_compat() does the same: integers of any size and signedness are compatible, also behind a pointer. The verifier looks at the size based on the kernel prototype, so a byte buffer passed to int * will fail to load. And resolve_btfids check between variants will catch this as well. See get_kfunc_arg_type() (or check_kfunc_args() on <=7.2). > >> [...]