Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398533; b=lG3+iSHK80NIQcrcWW9lu1hXmTV1TG8OECOXzZ/anY37R8Fw3f2ZXXC3Vxzoi1MFSppo JaVpJSLqJCuYZDGNWHm6y6G2b1dB6+x39F7aE2YgJLmNKfgE3QO75KXJG3rE7k3owedzJ 6nMmcoaZik9Nvdmz7PBuHB36rwu3OohmjTj0zlaeVXKThGf2NvFM9ALXdtYlB3dmKv+Rb z2ZDYDe1km4DkwFEvGivkGQRdsdQwstlZZBwXCOTNpGP4kG36lxXpANS3fWMN/km21UTn D94yVtQ57GqewAkOL+GnhDSh2Mz8+Bwp0epmZV/jTNfaar5z2P22svgZCC7tDX6IJmA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398533; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=rNOiQX0zR5YbGQfuzluYKV2Lw/rS8rTWwZEzPcKFZdU=; b=P6m/rM7f9tVAP31pe+TuDxj5r8MLXnD7ak04WhwIt9EoJVvUUPl2ZT/OX3X8pSbZ080n 6o+1ILCYzs4EwFTXeUB3P5Ed14vpQS5f1Z/gfdedjddIl3vZwtVc1AyA1BkwOZS2CSsQd k7200nBEKFxON3Izf9QjwvQiVYLNAoMUxlgoJ4aDoVa8DTx5iCkNyUBkig3xizPq4XM65 JGgGBnaYGqP26k+eQxCEmR5sec/vvLYz2eB6jJtzR5T1XY0D1OEQEq8btbXMRmxp51at6 YD4Gfnu3Iy/B8k/CvoZ17b1jMbvFj6iEjiUjYQQoqIsCG3Xtt7CMMI7agUVDs4DbTFw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SRlkkANU; arc=none smtp.remote-ip=209.85.221.53 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 8764E606A322 for ; Fri, 4 Dec 2026 15:35:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8764E606A322 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.a=rsa-sha256 header.s=20260707 header.b=SRlkkANU Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-491971ac844so1007908f8f.2 for ; Fri, 04 Dec 2026 07:35:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796398532; x=1797003332; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rNOiQX0zR5YbGQfuzluYKV2Lw/rS8rTWwZEzPcKFZdU=; b=SRlkkANUC8pINYWt5+KNc9rEP8s8RU7cIRCRquRSaGNqS+P0iG9o+WdinXCsI8nmRk ksRMC2Yj2mVpKfMJE40kVDL2VVNM+qOJjRz+UzxtYbFvMOhCXmbnTJ8qbhBZPAXk8DJT kWvhBtWR4QJgfwJPwCAvHQPiEIpiDbIh4SJEwwx2nJoy7gcKjDFr20pWnA2ytZQYkEWi LU4E36MfQ5Hb5h14n1ZlxzpeSKmqOry+6e1FY75r68Bpc5kn1/GCesCtzGS7MKILc3Vy IW9R4TlepAedeTEO9+pdISjg7aEqA2erOT9B+CmGXt7oV69mbuY4IQTehiP34c3Z1DaQ IhRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796398532; x=1797003332; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=rNOiQX0zR5YbGQfuzluYKV2Lw/rS8rTWwZEzPcKFZdU=; b=GgieyOSDGNlV2VH2a4LO9bUgWl09HDsBaY2W6Vw7I4UuQ6iCEkQO8nhsbLN0aDgbte MY4RQn0ISEh6o4Dwy1etQwDrxexyNcMFQhJbHJA7i+WFxReu5gIE5wKzCMWs55RuiAEn FccNNbiSlVm3sU6vmClCw2Sx50JBCp8+4Y3E+G7a7FPMnRmTSATMPt/gt5JzVXQXg0jy 5MT5wcmkYxnuHqbhuYgyOqDjcna40BDBE1E7x3IdkjlsfjNmi+uGlJF8+6iZDfB9lU6A 0pmyulVbTG3Lle8tUBOaJXNlArpdUAld/BliDLlKLNv7jlQp42mBvBL/waCBuSsI9VRX 6b7A== X-Gm-Message-State: APuU4Z+PMpOZrs1AI4tiozenvWWLG1NHT8MuJwRhKkXCmvmwL73xURC7 K1HJKZL/cBJUtKb7LUO6B56K48lqXpxHGzBY1OFkiAMjD6gKNrYfYsYfsZSIP85L X-Gm-Gg: AZFdTZdnygzV9QkyH40LWSeUz7+aqPPQovKzZC+mOYoR5b3ZdkDQm6UKxgHK4IKKAn6 FbaytDn6X4NPY4pFx8nygfHEiimGirAU5CuRgQAoAZhVkBJGhVHaYYKO4Xry1jCk/JTFgQuVDfz 6JPgD/EVhiyTArtAZOZQ52aYzueT5ROKgdm1/LRBi5yujqEzKCr9/+/z/nBQOXmVFFd++tqrxsV 6ls4joPovf/nuQey4GyKSQqMPNN0AKMkvRIfUjkdmfYFFRVIJbxSgvO7/QCLQ0FbnOnNAsnNfAq lBxcTlkg4s07+AhsbG8GDF486T5W/7RXfBqdnzO1JelTB50cvLaqW+VJLNDKzZNbEHmTz5OrsHr akhaUfdeeno8l2ldW+/rRriTgIv8xP27i/Wijx9KyaJ+t0ayEzTJqeRMpSA8nsr1EpCqXGlwbFo At0nYtSWc3ZYU/rzprJEvSfgCGykml42l5LdS18GydUpB61rKicLyKUEuOWC8D6dCYx/deXQ6Gp D3K9klCnRGn5wLinWAwL8QEnOGM3MXNHfOSN7Zvt44AG7CnwmpYGjtRm0HYbt9sMNEEaDiDLNp/ Jl4D1VfRm2RGTuzHaHyhk7tv8eFOEqPfod1InNyYNmOpPHU= X-Received: by 2002:a05:600c:4f8c:b0:4a4:4d57:8487 with SMTP id 5b1f17b1804b1-4bbeb84cc4amr47032005e9.5.1796398531826; Fri, 04 Dec 2026 07:35:31 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d174fe6fbcsm5922841f8f.58.2026.12.04.07.35.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 07:35:31 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , John Fastabend , Viktor Malik Cc: Anton Protopopov Subject: [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first Date: Fri, 4 Dec 2026 15:47:02 +0000 Message-ID: <20261204154705.1411391-4-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204154705.1411391-1-a.s.protopopov@gmail.com> References: <20261204154705.1411391-1-a.s.protopopov@gmail.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit For "mod:func" libbpf looks func up in the BTF of the module, and that lookup searches the types of vmlinux first. If vmlinux has a function of that name too, libbpf takes its ID, and the program attaches to the module function with the vmlinux prototype. The kernel now refuses this when the module has a function of its own of that name. A glob of a tracing multi program for a module walks the types of vmlinux too, and takes both IDs for such a name. Look up the function of the module first, and take the one of vmlinux only if the module has no function of that name. The dedup of BTF merges a module function of the same prototype into the vmlinux one, which is then the type of the module function. Fixes: 8f8a024272f3 ("libbpf: support "module: Function" syntax for tracing programs") Fixes: f2aa370dfe57 ("libbpf: Add support to create tracing multi link") Signed-off-by: Anton Protopopov --- tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8ed4786e0e51..72330598a0f8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11869,7 +11869,14 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, */ if (ret < 0 || ret >= sizeof(btf_type_name)) return -ENAMETOOLONG; - return btf__find_by_name_kind(btf, btf_type_name, kind); + /* + * A function of a module first, then the one of vmlinux, which the + * dedup of BTF merges a module function of the same prototype into + */ + ret = btf__find_by_name_kind_own(btf, btf_type_name, kind); + if (ret > 0 || !btf__base_btf(btf)) + return ret; + return btf__find_by_name_kind(btf__base_btf(btf), btf_type_name, kind); } static inline int find_attach_btf_id(struct btf *btf, const char *name, @@ -13887,40 +13894,60 @@ bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t) return true; } +static int add_func_id_by_glob(const struct btf *btf, __u32 type_id, const char *pattern, + bool skip_own, __u32 **ids, size_t *cap, size_t *cnt) +{ + const struct btf_type *t = btf__type_by_id(btf, type_id); + const char *name; + + if (btf_kind(t) != BTF_KIND_FUNC) + return 0; + name = btf__name_by_offset(btf, t->name_off); + if (!name || !glob_match(name, pattern)) + return 0; + if (!btf_type_is_traceable_func(btf, t)) + return 0; + if (skip_own && btf__find_by_name_kind_own(btf, name, BTF_KIND_FUNC) > 0) + return 0; + + if (libbpf_ensure_mem((void **) ids, cap, sizeof(**ids), *cnt + 1)) + return -ENOMEM; + (*ids)[(*cnt)++] = type_id; + return 0; +} + static int collect_btf_func_ids_by_glob(const struct btf *btf, const char *pattern, __u32 **ids) { - __u32 type_id, nr_types = btf__type_cnt(btf); + const struct btf *base_btf = btf__base_btf(btf); + __u32 type_id, start_id, nr_types = btf__type_cnt(btf); size_t cap = 0, cnt = 0; + int err; if (!pattern) return -EINVAL; - for (type_id = 1; type_id < nr_types; type_id++) { - const struct btf_type *t = btf__type_by_id(btf, type_id); - const char *name; - int err; - - if (btf_kind(t) != BTF_KIND_FUNC) - continue; - name = btf__name_by_offset(btf, t->name_off); - if (!name) - continue; - - if (!glob_match(name, pattern)) - continue; - if (!btf_type_is_traceable_func(btf, t)) - continue; - - err = libbpf_ensure_mem((void **) ids, &cap, sizeof(**ids), cnt + 1); - if (err) { - free(*ids); - return -ENOMEM; - } - (*ids)[cnt++] = type_id; + /* + * The functions of a module first, then the ones of vmlinux, which the + * dedup of BTF merges module functions of the same prototype into + */ + start_id = base_btf ? btf__type_cnt(base_btf) : 1; + for (type_id = start_id; type_id < nr_types; type_id++) { + err = add_func_id_by_glob(btf, type_id, pattern, false, ids, &cap, &cnt); + if (err) + goto err_out; + } + for (type_id = 1; type_id < start_id; type_id++) { + err = add_func_id_by_glob(btf, type_id, pattern, true, ids, &cap, &cnt); + if (err) + goto err_out; } return cnt; + +err_out: + free(*ids); + return err; } static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *pattern, __u32 **ids) -- 2.43.0