Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318082; b=CvkG5XxrCvGVFXXA2mlfuRCxf4atwgKMlrKkzrNglLWoQAvNlFnz6cM9vBe+UgidFLKd hOYBRPS+U7hXUaa+JrDYaCMEZPEhJ7Ak5w+4BOPcqUxd/J+HHKNYddK0MLum/xKqXtiHK tlPrvaDVIlskpjhcOGrhKpv5bU9EJflyXWZ28vG3sk/+q/5o0r5zm/K+YjWx+5s4VGWZj lzKkru9RVMZ/V+M6mL606g8vJSzXFtR3Yy6SzEQdsLfrZLQVV4+dx0IK4EBtzIF8oqGiK hXKz5uQN3r5CABwSRVorYYU6LZwHT9QGLodX5qzD+g8/mFCr+AJZnOXva2XncIwa8wg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318082; 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=rrDdXvigd4mcDpX78/JKhIqrhF3WFXbOnM2/8JBGyAY=; b=lYgPGkUv3QdVuzHthtgRXs8/ScP7qq1hwUy0YBQzG94vogFbzTKm6MxV1fp/zDWjkdxw BWmD9PqslgsjVquQvUi75eibFfqeC5P9CyoTQKLRUXBn8vpRnFOc9asg1f8W0Xfa+c7oy 3sXmreY4S0b1TrQHP6+J+r95rtR4HWnKf0T8X7YgL9n7u5PiRjQNOLkmE13Xfoow2Cd7x DxCh4zcym160wt4nIMnAkMs8GCn1ZSmHnbqokmXYM69yslFbSDiImG9q3xzds5I3ZF3Jw 1DeWX1KobX448GcpgAECqV6AJoKAzYzhA+fpWoSe+n/jFLg91D4GauRrDnxj2Z8tkfw== 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=Y4td10N8; arc=none smtp.remote-ip=209.85.128.48 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 D64E8606A333 for ; Thu, 3 Dec 2026 17:14:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D64E8606A333 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=Y4td10N8 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4a3517554f7so130214845e9.2 for ; Thu, 03 Dec 2026 09:14:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796318080; x=1796922880; 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=rrDdXvigd4mcDpX78/JKhIqrhF3WFXbOnM2/8JBGyAY=; b=Y4td10N8OhDecv5Ik/AJ2y6nkwL5fEBmfnVJT9egjZ9d20NugeM8oGC2/7ig782Tyr Ka9yD+n+AwWKmvJ3rqJTb8rbXOYV8UdUY/di8SVry/PwFeTYc996UCThjBSFqkKo2tCF BKqCDryR1zeaSBf2yeiPEu+5wAYUyEMdVV9j/Jil8O8eoEbE+4exQPhoR33T03EZmFlg 4mpJPabDvH2w3SP8qbTwgbJTlKBay8OaZp9NvhUXSofCn373Lk3ca8IA0OpNGVy5OiEv tJgB4XJHzZYMco0jKsOSvwDYpfUBU618uZpcrtkRTdmofi6y4DpYH9Uaz8tpYkPiYQ0q UxUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796318080; x=1796922880; 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=rrDdXvigd4mcDpX78/JKhIqrhF3WFXbOnM2/8JBGyAY=; b=yyQGM2Td+15RT8AYaG8B0x1AgvM3GBAt33Uo7bo8Zxz44TROjUw2Ge3LSEwKFA6oHR aL/MQ094p1l9efV66VdvjJSh/xKY/zxacUbzgtM8l6nWJ2See5goUuajVjPL90cMESZI Vzt3pDM199QlMLTfa+eiqx8lFHwz0W+rxQHofLr0VCa9V9dcCcVcfKZJoAZV+SPvHDj9 k90gyHpp0jHHFve1ZiEXzDwu6VO0C2pg0ukKKIEdY13K/pQgBcF2x4/+ctiCOBIEqGHo xfnkwZqmDdulh6vNUdkSxcwi1XdF9wTMBrPunH/hxs9QQHDpodNk3h8IpZsVm1TxET/z xy8A== X-Gm-Message-State: APuU4Z8AxSOp5jYfIgFnl5CUphGEVZmJR35u94Bz+vN31iCyD02tUp6s UY7A5q0Fqi7l3Mm5uQSECQTLVDLCvduS49Lnxp4NgBiD8m4Jd+48Z/EgPLn8SALI X-Gm-Gg: AZFdTZdbJl2XELlVemKpYZsnBw5PQOptk4RRj4URAI4D32bh9Vl+niPBe5zklTJCOSC CCiSAY+++xm2dYfHHCVjfYRH1bP96ZEQD4Rb87s4r4o7y9ZDN+5rloctX9OEJgi4XF/3yrz28FV nVsEeIaJDi8WzUE8UsSnLHBa2CCeuaoqqqkQc6r0draqXcU2SoIVxtRwPF2GkTNG6XKMCgzoQPM Xfemfhi59CxxRml6p14ge0nZU2WaS8mfpf5lcE6583Ws/C+DCQKI4m+JdQGXN9gOk8SARODHWf+ E+i10Y2Nz0xQ55a03ge44P9Cz/sVHxjs5hWoVQe4qpBoIYtVaFX53GOkeXlvrliYpnptAwii7mD MSsTGNZcNKnq5FQbdJ4i1T1qP0BF4UFhbJSy47/X0Fd+7DI10CPsPQoGvJ7kA7C487DP9NXQuwD y6iiWDeedgF8hW5oFnx7+J3Fw8rAO4PIAg8nQHmmFckFPE6GoNd4sTwYg2mYAC3UCfDc+lGQMtH auhWU4fluvzeIkHIfd/+QFoRDj7xYQVc/G3Uvjq7/SYpZbg2B+QO07gBB+467/ZjOon+M3JQKyT 6qYV5qRz6GqCjVKVRKdvcFeRHSIVbyBNJpoQPzQVsYlEn3GUNo8CTKOrSlha+rm/Mir/ X-Received: by 2002:a05:600c:3509:b0:4bb:76f6:d23a with SMTP id 5b1f17b1804b1-4bb88814d16mr897779695e9.21.1796318080259; Thu, 03 Dec 2026 09:14:40 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb9067846esm906409615e9.4.2026.12.03.09.14.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:14:39 -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 v1 bpf-next 3/4] libbpf: Look up the functions of a module first Date: Thu, 3 Dec 2026 17:26:14 +0000 Message-ID: <20261203172617.1137735-4-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261203172617.1137735-1-a.s.protopopov@gmail.com> References: <20261203172617.1137735-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 | 73 +++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8ed4786e0e51..b75b94add227 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11869,6 +11869,13 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, */ if (ret < 0 || ret >= sizeof(btf_type_name)) return -ENAMETOOLONG; + /* + * 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, btf_type_name, kind); } @@ -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