From nobody Tue Sep 29 09:45:50 2026 Received: from out-186.mta1.migadu.com (mta1.migadu.com [37.59.57.117]) (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 7039339A04A for ; Sun, 9 Aug 2026 15:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.59.57.117 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287722; cv=none; b=CGrmjreoL97xM/PmrDfMTV3cwkZftHE9ss6WNuBmLjF3Q1z+k/VZ+mREOR9U7RnIyq+cnKl4G/xzWW5/BgwL9rVVL3MBkHjbZygRIIVqde6bmA/7AbB3FeoMOWyczej2PLYnPqelf5kXUJ3STWwHFNqhWQQbYVQ+kBSKCBBDJe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287722; c=relaxed/simple; bh=NPPv63MwhFAsbs4rGDL9pQ7OXlzNtqBnsPeNXBDr5hQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PkOSlhB2eFM9Ws7Vkfl6Pa6qL7PZkOUr8DYXho8MFyolqmHxi3SA3/Qvy+ateEkXefagW4Z348R/BGBayKuMvoNBwVA+R1gOfc6b1vx0CqGZa1svcoxndABKHJ2Zwe7H87pINlD9P8gVkGRY9ofE+1Nw6vrjuh5lQfh0cRRCKsg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=n1/W/gnA; arc=none smtp.client-ip=37.59.57.117 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) header.d=linux.dev header.i=@linux.dev header.b="n1/W/gnA" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287718; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0nWaRn08RUguxiYGsJ8AlACfbwZDlTj8g6X00LLcNBk=; b=n1/W/gnAdHJBs3InaDrzXLC+vxGUeQNeitL/NhNMpWZd6XJKjfIGQJSaQoh7qeTu3rChs1 Y3HiUt815mpH7jECtG2iF97dBob3y8V8LPzGHYeNzLSABKGLwEQ0Y9prDbLOXEBc+YAxGj L+JMzFhBGXS1n/kdW/pGiJ1cFiTxWbI= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 01/13] bpf: Initialize ftrace_managed in bpf_trampoline_get Date: Sun, 9 Aug 2026 23:00:59 +0800 Message-ID: <20260809150111.45000-2-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" A trampoline managed by ftrace is determined by its target, that said ftrace_managed is already known when the trampoline is initialized. so, set 'ftrace_managed =3D tr->ip !=3D 0' in bpf_trampoline_get() instead of relying on the first regular or tracing_multi attachment to set it. This makes the backend selection independent of attachment order and keeps the registration callbacks focused on installing the trampoline. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Leon Hwang --- kernel/bpf/trampoline.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index e07af35ed040..8c2c1d1b9094 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -474,18 +474,11 @@ static int register_fentry(struct bpf_trampoline *tr,= struct bpf_tramp_image *im void *data __maybe_unused) { void *new_addr =3D im->image; - void *ip =3D tr->func.addr; - unsigned long faddr; int ret; =20 - faddr =3D ftrace_location((unsigned long)ip); - if (faddr) { + if (tr->func.ftrace_managed) { if (!tr->fops) return -ENOTSUPP; - tr->func.ftrace_managed =3D true; - } - - if (tr->func.ftrace_managed) { ret =3D direct_ops_add(tr, new_addr); } else { ret =3D bpf_trampoline_update_fentry(tr, 0, NULL, new_addr); @@ -1226,6 +1219,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, =20 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); tr->func.addr =3D (void *)tgt_info->tgt_addr; + tr->func.ftrace_managed =3D tr->ip !=3D 0; out: trampoline_unlock(tr); return tr; @@ -1583,7 +1577,6 @@ static int register_fentry_multi(struct bpf_trampolin= e *tr, struct bpf_tramp_ima if (bpf_trampoline_use_jmp(tr->flags)) addr =3D ftrace_jmp_set(addr); =20 - tr->func.ftrace_managed =3D true; ftrace_hash_add(data->reg, data->entry, ip, addr); tr->cur_image =3D im; return 0; --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-186.mta1.migadu.com (mta1.migadu.com [37.59.57.117]) (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 0F11A3A1693 for ; Sun, 9 Aug 2026 15:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.59.57.117 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287729; cv=none; b=AqdPTGgg9rNsQhEncfUnT6S0rVQfrd6VUkCyVfBPDxR98VvUGTi1lrQNX4DWXhv07jYqT+z580aClqRfMhvvKtyo2bBY/pb9YTdGcdOV172+/mCedTGEwxZB9zcQt73/YaW198NYZOkvQ2N62LZ8garmPY4wVpXrsNcgAskDEv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287729; c=relaxed/simple; bh=3MTCI5XeL+tPy6FWJF/tlCqoSyBh5wU07XGAQFqDxbg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZVOWvIaqaGGLWz7pge3WFVyKtba4FUAxH28V/hM84Q2z8sWr+ikLTf2bIZ2AdcEiDruSyGszVniqlb5oOwuut4QrBnBCkeGYAZ1cRDZzyBnGtK9ts4i5/x+byqOMTIZdfiFg5f5QSkhaI6i+2KvAFOwS1VfASt1GD+YVau5noMI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=UjRYenol; arc=none smtp.client-ip=37.59.57.117 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) header.d=linux.dev header.i=@linux.dev header.b="UjRYenol" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WPqBAE7OME6JB+B8bz7yQ1+hF2jZRRJRbW/HV1BzL+Q=; b=UjRYenolm0FPBJHMfJyghG/DV6Gs0ZAgHFe/SIRAsx3bNSXY86fuBoDqgrD/Op3NBewr/Z //+VS5rYncX176hSQ5o1Dmw8dsa9AQC4q3x8iuUqf4ZjzDi957Hn6ca+gI8Oe3eN1dD0dq nfovL+/g547OyHmBE3lR9FTNyO/PSAQ= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 02/13] bpf: Factor out update_fentry_multi helper Date: Sun, 9 Aug 2026 23:01:00 +0800 Message-ID: <20260809150111.45000-3-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The tracing_multi register, unregister, and modify callbacks differ only in the trampoline image and ftrace hash they use. Move their common address setup, hash insertion, and current image update into update_fentry_multi(). No functional changes intended. Signed-off-by: Leon Hwang --- kernel/bpf/trampoline.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 8c2c1d1b9094..6e1898e1b200 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -1568,47 +1568,41 @@ static void ftrace_hash_add(struct ftrace_hash *has= h, struct ftrace_func_entry * add_ftrace_hash_entry(hash, entry); } =20 -static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tra= mp_image *im, void *ptr) +static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, + struct bpf_tramp_image *im, struct ftrace_hash *hash, + struct bpf_tracing_multi_data *data) { - unsigned long addr =3D (unsigned long) im->image; + unsigned long addr =3D (unsigned long)(im ? im->image : tr->cur_image->im= age); unsigned long ip =3D ftrace_location(tr->ip); - struct bpf_tracing_multi_data *data =3D ptr; =20 if (bpf_trampoline_use_jmp(tr->flags)) addr =3D ftrace_jmp_set(addr); =20 - ftrace_hash_add(data->reg, data->entry, ip, addr); + ftrace_hash_add(hash, data->entry, ip, addr); tr->cur_image =3D im; return 0; } =20 -static int unregister_fentry_multi(struct bpf_trampoline *tr, u32 orig_fla= gs, void *ptr) +static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tra= mp_image *im, void *ptr) { - unsigned long addr =3D (unsigned long) tr->cur_image->image; - unsigned long ip =3D ftrace_location(tr->ip); struct bpf_tracing_multi_data *data =3D ptr; =20 - if (bpf_trampoline_use_jmp(tr->flags)) - addr =3D ftrace_jmp_set(addr); + return update_fentry_multi(tr, 0, im, data->reg, data); +} =20 - ftrace_hash_add(data->unreg, data->entry, ip, addr); - tr->cur_image =3D NULL; - return 0; +static int unregister_fentry_multi(struct bpf_trampoline *tr, u32 orig_fla= gs, void *ptr) +{ + struct bpf_tracing_multi_data *data =3D ptr; + + return update_fentry_multi(tr, orig_flags, NULL, data->unreg, data); } =20 static int modify_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, = struct bpf_tramp_image *im, bool lock_direct_mutex, void *ptr) { - unsigned long addr =3D (unsigned long) im->image; - unsigned long ip =3D ftrace_location(tr->ip); struct bpf_tracing_multi_data *data =3D ptr; =20 - if (bpf_trampoline_use_jmp(tr->flags)) - addr =3D ftrace_jmp_set(addr); - - ftrace_hash_add(data->modify, data->entry, ip, addr); - tr->cur_image =3D im; - return 0; + return update_fentry_multi(tr, orig_flags, im, data->modify, data); } =20 static const struct bpf_trampoline_ops trampoline_multi_ops =3D { --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) (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 8309F3C9890 for ; Sun, 9 Aug 2026 15:02:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287735; cv=none; b=RCvOoS3XfA9/x8EkSMnEAFKTj676LoweyLdeHhY9Yvl+r8ladFa6ameA4rCJoO++AZaqVWO0dGyfGruenbCh3PLwOSghDHMYbnWVDskqxIfmwART3kXXyimf4QlDcBRXbr2LoKW6gxeAILY9J94oRvRuqt8AYkHCjK/PbkyEsI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287735; c=relaxed/simple; bh=BEoCcAyF+R/sOKReri78++IeZsP6ZHkO2YpBECkJFOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U9xjL5AgxBQf2AwP3TOi4F9Zmwmq09gdAYFiImpxYuTCxG0KqUx9n7uYxt2ocJ26MyBAqNHE72tuMjNvZNyc2K5B0nSiqOKhcveFmUEhVNRYzIn0zQHFNUhvc/9MZgsn7Qgi4j30+SkyD64zCKsrwAYargbehSKHXYFYonb0a1o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jraZSiFr; arc=none smtp.client-ip=95.215.58.188 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) header.d=linux.dev header.i=@linux.dev header.b="jraZSiFr" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287731; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UvmPAjSTsp9C9P0krIgzdAKGfO6OEzvEZTGyYBjPPWc=; b=jraZSiFrWBbR17WCYVve0oahjZvxb//yOWl+2/23IGcg46nIn7usGNytHs1T8uMcx5MWq8 1j1GlQ+r/NxKs9JJViz8j2XFng86QT4dIMlPAoSd0jL9ADi5ga6t0iIUx0CmMwlXWGZobh /Bp/FSe2ZPmoZCYaL3QXYT5dVvJ0Qsc= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 03/13] bpf: Drop unnecessary ftrace_location() in update_fentry_multi() Date: Sun, 9 Aug 2026 23:01:01 +0800 Message-ID: <20260809150111.45000-4-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Since 'tr->ip' is already a 'ftrace_location()'-ed IP in bpf_trampoline_lookup(), it is unnecessary to call ftrace_location() again in update_fentry_multi(). Signed-off-by: Leon Hwang --- kernel/bpf/trampoline.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 6e1898e1b200..eddd259d3776 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -1573,12 +1573,11 @@ static int update_fentry_multi(struct bpf_trampolin= e *tr, u32 orig_flags, struct bpf_tracing_multi_data *data) { unsigned long addr =3D (unsigned long)(im ? im->image : tr->cur_image->im= age); - unsigned long ip =3D ftrace_location(tr->ip); =20 if (bpf_trampoline_use_jmp(tr->flags)) addr =3D ftrace_jmp_set(addr); =20 - ftrace_hash_add(hash, data->entry, ip, addr); + ftrace_hash_add(hash, data->entry, tr->ip, addr); tr->cur_image =3D im; return 0; } --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) (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 B80B93CE489 for ; Sun, 9 Aug 2026 15:02:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287742; cv=none; b=enWBr0f7Dpb3bFKEKVB6v5YnoEKpnx6r++L7V8c3zcKcYiKI0uRAxANhUvop1y8cGhXZRzioOJ4nP72bHHL5jnw8y/UkQcjRsxgJ2+WIhTH8JZsgYyOL9aLUH8p6/KQeGK4A6ywRqGhGapJA1jSpB2lSKfDTRAqlTTLHb9DN6aI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287742; c=relaxed/simple; bh=7FOSTniZwjPrQT60YlmJvMFPrfJzIvlQebiOf9s+Bzw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GnBSaFY80eyFLgkrNuHHtr8QMqbOEFs0s/I2TyIfzRmr4VnKvrSNRwBX+MIDTiudr1ngnJcjACCkemV/8sH79JFRxpX28DTgwiFnvPgSnl4/GOlJApirbFgl8STeQOmiBFAlJHF+81yPMYnllGksULAc4cd/0cK4S5RqSFzreho= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vsLkRGwY; arc=none smtp.client-ip=95.215.58.188 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) header.d=linux.dev header.i=@linux.dev header.b="vsLkRGwY" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287737; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jhJ08LZoEnuY9C/qeiI0H3GIXG/a0BicPUcIztOIMk0=; b=vsLkRGwYb4zueDrRx4owZyraboZ0fkrOVs1FDxZ6IZSGUmL4d6n0J5JKCfCDyntrUTPfzu 8YoU34vK/qjEtSBSLOsFzKYqGvc36GFWIkC1nAFhXTtv6n0dZzx+Ae8MLh5EJNjnj7Vomq iAVGX1sFor60U90Cwj8hxmj17YaEbeA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 04/13] bpf: Add tracing_multi link support for bpf progs Date: Sun, 9 Aug 2026 23:01:02 +0800 Message-ID: <20260809150111.45000-5-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Similar to the tracing_multi link support for kernel functions, add support for bpf progs by reusing the 'ids' fields as BTF ids, and adding fds array to indicate the target bpf prog. Then, for each pair of fd+id, they will be the attach targets of tracing_multi link. When attaching bpf prog, it must attaches to the target by text poke way. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Leon Hwang --- include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 2 + kernel/bpf/trampoline.c | 45 ++++++++-- kernel/bpf/verifier.c | 22 ++--- kernel/trace/bpf_trace.c | 157 ++++++++++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 2 + 6 files changed, 189 insertions(+), 42 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b4a10c9878cf..9962228bca41 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1517,6 +1517,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr); int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int nu= m_funcs); =20 int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, + u64 *keys, struct bpf_prog **progs, struct bpf_tracing_multi_link *link); int bpf_trampoline_multi_detach(struct bpf_prog *prog, struct bpf_tracing_multi_link *link); @@ -1635,6 +1636,7 @@ static inline bool bpf_prog_has_trampoline(const stru= ct bpf_prog *prog) return false; } static inline int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *= ids, + u64 *keys, struct bpf_prog **progs, struct bpf_tracing_multi_link *link) { return -ENOTSUPP; @@ -2002,6 +2004,7 @@ struct bpf_tracing_multi_link { struct bpf_tracing_multi_data data; u64 *cookies; struct bpf_tramp_node *fexits; + struct bpf_prog **progs; int nodes_cnt; struct bpf_tracing_multi_node nodes[] __counted_by(nodes_cnt); }; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index ffd96e8b920b..6fa93a6ff54f 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1888,6 +1888,8 @@ union bpf_attr { __aligned_u64 ids; __aligned_u64 cookies; __u32 cnt; + __u32 :32; + __aligned_u64 fds; } tracing_multi; }; } link_create; diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index eddd259d3776..fc51ea2428be 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -1572,12 +1572,23 @@ static int update_fentry_multi(struct bpf_trampolin= e *tr, u32 orig_flags, struct bpf_tramp_image *im, struct ftrace_hash *hash, struct bpf_tracing_multi_data *data) { - unsigned long addr =3D (unsigned long)(im ? im->image : tr->cur_image->im= age); + if (tr->func.ftrace_managed) { + unsigned long addr =3D (unsigned long)(im ? im->image : tr->cur_image->i= mage); =20 - if (bpf_trampoline_use_jmp(tr->flags)) - addr =3D ftrace_jmp_set(addr); + if (bpf_trampoline_use_jmp(tr->flags)) + addr =3D ftrace_jmp_set(addr); + + ftrace_hash_add(hash, data->entry, tr->ip, addr); + } else { + void *old_addr =3D tr->cur_image ? tr->cur_image->image : NULL; + void *new_addr =3D im ? im->image : NULL; + int ret; + + ret =3D bpf_trampoline_update_fentry(tr, orig_flags, old_addr, new_addr); + if (ret) + return ret; + } =20 - ftrace_hash_add(hash, data->entry, tr->ip, addr); tr->cur_image =3D im; return 0; } @@ -1627,6 +1638,18 @@ static void bpf_trampoline_multi_attach_free(struct = bpf_trampoline *tr) =20 static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr) { + if (!tr->func.ftrace_managed) { + void *failed_addr =3D tr->cur_image ? tr->cur_image->image : NULL; + void *old_addr =3D tr->multi_attach.old_image ? + tr->multi_attach.old_image->image : NULL; + u32 orig_flags =3D tr->flags; + int ret; + + tr->flags =3D tr->multi_attach.old_flags; + ret =3D bpf_trampoline_update_fentry(tr, orig_flags, failed_addr, old_ad= dr); + WARN_ONCE(ret, "bpf_trampoline_update_fentry failed: %d\n", ret); + } + if (tr->cur_image) bpf_tramp_image_put(tr->cur_image); tr->cur_image =3D tr->multi_attach.old_image; @@ -1643,6 +1666,7 @@ static void bpf_trampoline_multi_attach_rollback(stru= ct bpf_trampoline *tr) for_each_mnode_cnt(mnode, link, link->nodes_cnt) =20 int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, + u64 *keys, struct bpf_prog **progs, struct bpf_tracing_multi_link *link) { struct bpf_tracing_multi_data *data =3D &link->data; @@ -1651,18 +1675,18 @@ int bpf_trampoline_multi_attach(struct bpf_prog *pr= og, u32 *ids, struct bpf_tracing_multi_node *mnode; struct bpf_trampoline *tr; int i, err, rollback_cnt; - u64 key; =20 for_each_mnode(mnode, link) { rollback_cnt =3D i; =20 - err =3D bpf_check_attach_btf_id_multi(btf, prog, ids[i], &tgt_info); + if (progs) + err =3D bpf_check_attach_target(NULL, prog, progs[i], ids[i], &tgt_info= ); + else + err =3D bpf_check_attach_btf_id_multi(btf, prog, ids[i], &tgt_info); if (err) goto rollback_put; =20 - key =3D bpf_trampoline_compute_key(NULL, btf, ids[i]); - - tr =3D bpf_trampoline_get(key, &tgt_info); + tr =3D bpf_trampoline_get(keys[i], &tgt_info); if (!tr) { err =3D -ENOMEM; goto rollback_put; @@ -1691,6 +1715,9 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog= , u32 *ids, for_each_mnode(mnode, link) { bpf_trampoline_multi_attach_init(mnode->trampoline); =20 + if (progs && progs[i]->aux->tail_call_reachable) + mnode->trampoline->flags |=3D BPF_TRAMP_F_TAIL_CALL_CTX; + data->entry =3D &mnode->entry; err =3D __bpf_trampoline_link_prog(&mnode->node, mnode->trampoline, NULL, &trampoline_multi_ops, data); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index add3affc5703..887d5f654a3a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19291,22 +19291,24 @@ int bpf_check_attach_target(struct bpf_verifier_l= og *log, return -EOPNOTSUPP; } =20 - /* - * *.multi programs don't need an address during program - * verification, we just take the module ref if needed. - */ - if (is_tracing_multi_id(prog, btf_id)) { + if (tgt_prog) { + if (subprog =3D=3D 0) + addr =3D (long) tgt_prog->bpf_func; + else + addr =3D (long) tgt_prog->aux->func[subprog]->bpf_func; + } else if (is_tracing_multi_id(prog, btf_id)) { + /* + * *.multi programs don't need an address during program + * verification, we just take the module ref if needed. + * *.multi programs don't have tgt_prog during program + * verification. + */ if (btf_is_module(btf)) { mod =3D btf_try_get_module(btf); if (!mod) return -ENOENT; } addr =3D 0; - } else if (tgt_prog) { - if (subprog =3D=3D 0) - addr =3D (long) tgt_prog->bpf_func; - else - addr =3D (long) tgt_prog->aux->func[subprog]->bpf_func; } else { if (btf_is_module(btf)) { mod =3D btf_try_get_module(btf); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 891897f8a1b3..a805a5b73303 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3682,12 +3682,15 @@ __bpf_kfunc_end_defs(); #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) && \ defined(CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS) =20 +static void bpf_put_progs(struct bpf_prog **progs, int cnt); + static void bpf_tracing_multi_link_release(struct bpf_link *link) { struct bpf_tracing_multi_link *tr_link =3D container_of(link, struct bpf_tracing_multi_link, link); =20 WARN_ON_ONCE(bpf_trampoline_multi_detach(link->prog, tr_link)); + bpf_put_progs(tr_link->progs, tr_link->nodes_cnt); } =20 static void bpf_tracing_multi_link_dealloc(struct bpf_link *link) @@ -3695,6 +3698,7 @@ static void bpf_tracing_multi_link_dealloc(struct bpf= _link *link) struct bpf_tracing_multi_link *tr_link =3D container_of(link, struct bpf_tracing_multi_link, link); =20 + kvfree(tr_link->progs); kvfree(tr_link->fexits); kvfree(tr_link->cookies); kvfree(tr_link); @@ -3790,64 +3794,128 @@ static const struct bpf_link_ops bpf_tracing_multi= _link_lops =3D { #endif }; =20 -static int ids_cmp_r(const void *pa, const void *pb, const void *priv __ma= ybe_unused) +struct tracing_multi_sort_data { + u64 *keys; + struct bpf_prog **progs; + u32 *ids; + u64 *cookies; +}; + +static int keys_cmp_r(const void *pa, const void *pb, const void *priv __m= aybe_unused) { - u32 a =3D *(u32 *) pa; - u32 b =3D *(u32 *) pb; + const u64 *key_a =3D pa, *key_b =3D pb; =20 - return (a > b) - (a < b); + return (*key_a > *key_b) - (*key_a < *key_b); } =20 -static void ids_swap_r(void *a, void *b, int size __maybe_unused, - const void *priv __maybe_unused) +static void keys_swap_r(void *a, void *b, int size __maybe_unused, + const void *priv __maybe_unused) { + const struct tracing_multi_sort_data *data =3D priv; + struct bpf_prog **prog_a, **prog_b; u64 *cookie_a, *cookie_b, *cookies; - u32 *id_a =3D a, *id_b =3D b, *ids; - void **data =3D (void **) priv; + u64 *key_a =3D a, *key_b =3D b, *keys; + u32 *id_a, *id_b, *ids; =20 - ids =3D data[0]; - cookies =3D data[1]; + keys =3D data->keys; + ids =3D data->ids; + cookies =3D data->cookies; + id_a =3D ids + (key_a - keys); + id_b =3D ids + (key_b - keys); + + if (data->progs) { + prog_a =3D data->progs + (key_a - keys); + prog_b =3D data->progs + (key_b - keys); + swap(*prog_a, *prog_b); + } =20 if (cookies) { cookie_a =3D cookies + (id_a - ids); cookie_b =3D cookies + (id_b - ids); swap(*cookie_a, *cookie_b); } + swap(*id_a, *id_b); + swap(*key_a, *key_b); } =20 -static int check_dup_ids(u32 *ids, u64 *cookies, u32 cnt) +static int check_dup_keys(u64 *keys, struct bpf_prog **progs, u32 *ids, + u64 *cookies, u32 cnt) { - void *data[2] =3D { ids, cookies }; - int err =3D 0; + struct tracing_multi_sort_data data =3D { + .keys =3D keys, + .progs =3D progs, + .ids =3D ids, + .cookies =3D cookies, + }; =20 /* - * Sort ids array (together with cookies array if defined) - * and check it for duplicates. The ids and cookies arrays - * are left sorted. + * Sort trampoline keys together with target programs, ids, and cookies, + * then check for duplicates. The arrays are left sorted. */ - sort_r_nonatomic(ids, cnt, sizeof(ids[0]), ids_cmp_r, ids_swap_r, data); + sort_r_nonatomic(keys, cnt, sizeof(keys[0]), keys_cmp_r, keys_swap_r, &da= ta); + + for (int i =3D 1; i < cnt; i++) + if (keys[i] =3D=3D keys[i - 1]) + return -EINVAL; + + return 0; +} + +static void bpf_put_progs(struct bpf_prog **progs, int cnt) +{ + int i; + + if (!progs) + return; + + for (i =3D 0; i < cnt; i++) + bpf_prog_put(progs[i]); +} + +static int bpf_get_progs(struct bpf_prog **progs, int *fds, int cnt) +{ + int err, i, put_cnt =3D 0; + struct bpf_prog *prog; + + for (i =3D 0; i < cnt; i++) { + prog =3D bpf_prog_get(fds[i]); + if (IS_ERR(prog)) { + err =3D PTR_ERR(prog); + goto error; + } + + progs[i] =3D prog; + put_cnt++; =20 - for (int i =3D 1; i < cnt; i++) { - if (ids[i] =3D=3D ids[i - 1]) { + if (is_tracing_multi(prog->expected_attach_type)) { err =3D -EINVAL; - break; + goto error; } } + return 0; + +error: + bpf_put_progs(progs, put_cnt); return err; } =20 int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *= attr) { struct bpf_tracing_multi_link *link =3D NULL; + struct btf *btf =3D prog->aux->attach_btf; struct bpf_tramp_node *fexits =3D NULL; struct bpf_link_primer link_primer; + u64 *cookies =3D NULL, *keys =3D NULL; + struct bpf_prog **progs =3D NULL; u32 cnt, *ids =3D NULL; u64 __user *ucookies; - u64 *cookies =3D NULL; u32 __user *uids; + int __user *ufds; + int *fds =3D NULL; int err; =20 + ufds =3D u64_to_user_ptr(attr->link_create.tracing_multi.fds); uids =3D u64_to_user_ptr(attr->link_create.tracing_multi.ids); cnt =3D attr->link_create.tracing_multi.cnt; =20 @@ -3867,6 +3935,32 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, = const union bpf_attr *attr) goto error; } =20 + if (ufds) { + fds =3D kvmalloc_objs(*fds, cnt); + if (!fds) { + err =3D -ENOMEM; + goto error; + } + + if (copy_from_user(fds, ufds, cnt * sizeof(*fds))) { + err =3D -EFAULT; + goto error; + + } + + progs =3D kvmalloc_objs(*progs, cnt); + if (!progs) { + err =3D -ENOMEM; + goto error; + } + + err =3D bpf_get_progs(progs, fds, cnt); + if (err) { + cnt =3D 0; + goto error; + } + } + ucookies =3D u64_to_user_ptr(attr->link_create.tracing_multi.cookies); if (ucookies) { cookies =3D kvmalloc_objs(*cookies, cnt); @@ -3880,7 +3974,16 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, = const union bpf_attr *attr) } } =20 - err =3D check_dup_ids(ids, cookies, cnt); + keys =3D kvmalloc_objs(*keys, cnt); + if (!keys) { + err =3D -ENOMEM; + goto error; + } + + for (int i =3D 0; i < cnt; i++) + keys[i] =3D bpf_trampoline_compute_key(progs ? progs[i] : NULL, btf, ids= [i]); + + err =3D check_dup_keys(keys, progs, ids, cookies, cnt); if (err) goto error; =20 @@ -3908,19 +4011,27 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog,= const union bpf_attr *attr) link->nodes_cnt =3D cnt; link->cookies =3D cookies; link->fexits =3D fexits; + link->progs =3D progs; =20 - err =3D bpf_trampoline_multi_attach(prog, ids, link); + err =3D bpf_trampoline_multi_attach(prog, ids, keys, progs, link); kvfree(ids); + kvfree(keys); + kvfree(fds); if (err) { + bpf_put_progs(progs, cnt); bpf_link_cleanup(&link_primer); return err; } return bpf_link_settle(&link_primer); =20 error: + bpf_put_progs(progs, cnt); kvfree(fexits); kvfree(cookies); kvfree(ids); + kvfree(keys); + kvfree(fds); + kvfree(progs); kvfree(link); return err; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index ffd96e8b920b..6fa93a6ff54f 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1888,6 +1888,8 @@ union bpf_attr { __aligned_u64 ids; __aligned_u64 cookies; __u32 cnt; + __u32 :32; + __aligned_u64 fds; } tracing_multi; }; } link_create; --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 67F23257844 for ; Sun, 9 Aug 2026 15:02:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287768; cv=none; b=NARRuCmEyJbT/XkPrb7NO+qD/RLzZ9z8spz+fwVbEeEV6aeS+tSUnuLff3ZDaLBfI/YJYFmWQRdR5uemMjkRRsGziUeiI5HBVUBgH7XeSheIR5klyJcQzIuzetb9BoK43GWDiEjzF/5/Grqa7X5iYTIDe2Ho/Qw1CEtZM524x8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287768; c=relaxed/simple; bh=Id5pefIyKE/U91Zn8zI+k2Hjyo9tKnshYz+qiSarsd8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RMl8nuBt5BWbmYDsXg87XELYizbUB7OxktbSpRTj4xh9HIuWa/VplbqMvKu0x3DFo/fEreWsagN/qkdOsaodlNF+fC1aC+c9mhmQ2of7dqKMqRtmlBSig9UjuMpZ2fo/dnBMlywQJYz7aFONYAzSY6YPKZVhD0UnCXqKj5AMZtc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=HQpv1WvT; arc=none smtp.client-ip=91.218.175.179 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) header.d=linux.dev header.i=@linux.dev header.b="HQpv1WvT" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0TMqdN/yDSpEh6YcQdZh+gNoQqcZgWyH9Vy4rBrBQRs=; b=HQpv1WvT1UmCIhKrrK/hJSqT5fZtbdawTE1ZrFyjEmAcOWT1K8Vb7JPZ4kOaEKOPypM9IR JoCXc9oro8JJWaZMh9i+8cUJ1YWBDa/B6f3Z+hW/9kdQAEJyowYhdffyJri1AR6TRIk+YZ Yqs0DQeUxDYlaB2Dp+SGMV8bAIuzOSA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 05/13] libbpf: Add tracing_multi link support for bpf progs Date: Sun, 9 Aug 2026 23:01:03 +0800 Message-ID: <20260809150111.45000-6-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" By design, a tracing_multi link does not support both kernel functions and bpf progs at the same time. In libbpf, the bpf_tracing_multi_opts options provides two new fields for tracing bpf progs: * 'fds': the fd array of the target bpf progs. One fd must be provided multiple times for multiple targets. * 'funcs': the function name array of the target bpf progs. The 'cnt' field indicates the size of both 'fds' and 'funcs' arrays. The 'funcs' will be translated to 'ids' by 'libbpf_find_prog_btf_id()'. At last, 'fds'+'ids' will be passed to kernel. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 ++ tools/lib/bpf/libbpf.c | 37 +++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 4 +++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 96819c082c77..a20585174903 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -852,6 +852,7 @@ int bpf_link_create(int prog_fd, int target_fd, attr.link_create.tracing_multi.ids =3D ptr_to_u64(OPTS_GET(opts, tracing= _multi.ids, 0)); attr.link_create.tracing_multi.cookies =3D ptr_to_u64(OPTS_GET(opts, tra= cing_multi.cookies, 0)); attr.link_create.tracing_multi.cnt =3D OPTS_GET(opts, tracing_multi.cnt,= 0); + attr.link_create.tracing_multi.fds =3D ptr_to_u64(OPTS_GET(opts, tracing= _multi.fds, 0)); if (!OPTS_ZEROED(opts, tracing_multi)) return libbpf_err(-EINVAL); break; diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 7534a593edae..63961db29b74 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -474,6 +474,8 @@ struct bpf_link_create_opts { const __u32 *ids; const __u64 *cookies; __u32 cnt; + __u32 :32; + const int *fds; } tracing_multi; }; size_t :0; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 514e4e9daa82..d698a64ff800 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -12687,10 +12687,12 @@ bpf_program__attach_tracing_multi(const struct bp= f_program *prog, const char *pa { LIBBPF_OPTS(bpf_link_create_opts, lopts); int prog_fd, link_fd, err, cnt; + struct bpf_link *link =3D NULL; __u32 *free_ids =3D NULL; - struct bpf_link *link; const __u64 *cookies; + const char **funcs; const __u32 *ids; + const int *fds; =20 if (!OPTS_VALID(opts, bpf_tracing_multi_opts)) return libbpf_err_ptr(-EINVAL); @@ -12705,12 +12707,22 @@ bpf_program__attach_tracing_multi(const struct bp= f_program *prog, const char *pa cnt =3D OPTS_GET(opts, cnt, 0); ids =3D OPTS_GET(opts, ids, NULL); cookies =3D OPTS_GET(opts, cookies, NULL); + fds =3D OPTS_GET(opts, fds, NULL); + funcs =3D OPTS_GET(opts, funcs, NULL); =20 - if (!!ids !=3D !!cnt) + if (ids && fds) + return libbpf_err_ptr(-EINVAL); + if (!!fds !=3D !!funcs) + return libbpf_err_ptr(-EINVAL); + if (fds && !cnt) return libbpf_err_ptr(-EINVAL); - if (pattern && (ids || cookies)) + if (ids && !cnt) return libbpf_err_ptr(-EINVAL); - if (!pattern && !ids) + if (!fds && !ids && cnt) + return libbpf_err_ptr(-EINVAL); + if (pattern && (fds || ids || cookies)) + return libbpf_err_ptr(-EINVAL); + if (!pattern && !ids && !fds) return libbpf_err_ptr(-EINVAL); =20 if (pattern) { @@ -12720,11 +12732,28 @@ bpf_program__attach_tracing_multi(const struct bp= f_program *prog, const char *pa if (cnt =3D=3D 0) return libbpf_err_ptr(-EINVAL); ids =3D (const __u32 *) free_ids; + } else if (fds) { + size_t cap =3D 0; + int i; + + err =3D libbpf_ensure_mem((void **) &free_ids, &cap, sizeof(*free_ids), = cnt); + if (err) + return libbpf_err_ptr(err); + + for (i =3D 0; i < cnt; i++) { + err =3D libbpf_find_prog_btf_id(funcs[i], fds[i], prog->obj->token_fd); + if (err < 0) + goto error; + + free_ids[i] =3D err; + } + ids =3D (const __u32 *) free_ids; } =20 lopts.tracing_multi.ids =3D ids; lopts.tracing_multi.cookies =3D cookies; lopts.tracing_multi.cnt =3D cnt; + lopts.tracing_multi.fds =3D fds; =20 link =3D calloc(1, sizeof(*link)); if (!link) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index b965ad571540..7b2cfa1ad572 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -732,10 +732,12 @@ struct bpf_tracing_multi_opts { const __u32 *ids; const __u64 *cookies; size_t cnt; + const int *fds; + const char **funcs; size_t :0; }; =20 -#define bpf_tracing_multi_opts__last_field cnt +#define bpf_tracing_multi_opts__last_field funcs =20 LIBBPF_API struct bpf_link * bpf_program__attach_tracing_multi(const struct bpf_program *prog, const ch= ar *pattern, --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 00EAD257844 for ; Sun, 9 Aug 2026 15:02:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287774; cv=none; b=ahdZ9SMeh8NNUHBiZ/yrfJkrT6xtTVh2P/ab26T44kvzDw5SMIEWHrTZFCapSDVWbW+AXNWmFSeOsEq20faar/6GvF+BPJwmhZUoeMSpu7GqR5I+vPT6SgH1OAuRIMPDr8KR+gKW2iCUdKGBtB4bF3y2F6GrhO0uVh7JjFJCTZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287774; c=relaxed/simple; bh=7Sg0M1Pso7u9S1IroiOYbnUf8qOXAj3UA7JZPPX8w84=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V7jxLV4rZRV8Tc17lPrvNnutV+NPGxdKjxU9bPdKcwkr5UsEARbzM9sa1voK+hE7etDjXiUDyQlrBaqtfGXvnrIE4yeOBtbIR5MV056aU7W7yaHDjq2MxSB2xYIqmjHWF4K5q4G1J2fvaxA90hNBqD6TYv4PEfdY4iBGGDGC+WQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=GbVj7eXd; arc=none smtp.client-ip=91.218.175.179 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) header.d=linux.dev header.i=@linux.dev header.b="GbVj7eXd" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UNeZp3MPMEvz8aXrCIJri7M6DLgx9QVZUN9FITbE8Ww=; b=GbVj7eXdJHqpWo8+c70h4avQLIYBuWLpFld8RD8AZzGJWcoY5gxZpI0DecQ6vHxTsFNtQk PkHZiYu1aaxa4HiMjmIYGOT2AthLMoZt/e8x3ZbbFKvqWi/jgZY9yt+t43rapqJGXtnVGL mH3uVyymFcfpWNBwnvQXKE64VXvX4ac= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 06/13] bpf: Add tracing_multi link fdinfo support for bpf progs Date: Sun, 9 Aug 2026 23:01:04 +0800 Message-ID: <20260809150111.45000-7-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Show the target bpf prog info in the fdinfo. $ cat /proc/self/fdinfo/13 pos: 0 flags: 02000000 mnt_id: 18 ino: 1045 link_type: tracing_multi link_id: 9 prog_tag: 9a7f9e3ab6f138ab prog_id: 25 attach_type: 61 cnt: 2 prog-id btf-id cookie func 21 4 16706819 target_1 22 5 16706819 target_2 Signed-off-by: Leon Hwang --- kernel/trace/bpf_trace.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index a805a5b73303..80928401830c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3759,26 +3759,46 @@ static int bpf_tracing_multi_link_fill_link_info(co= nst struct bpf_link *link, } =20 #ifdef CONFIG_PROC_FS +static const char *bpf_prog_func_name(struct bpf_prog *prog, u32 btf_id) +{ + const struct btf *btf =3D prog->aux->btf; + const struct btf_type *t; + + t =3D btf_type_by_id(btf, btf_id); + if (!t || !btf_type_is_func(t)) + return ""; + + return btf_name_by_offset(btf, t->name_off); +} + static void bpf_tracing_multi_show_fdinfo(const struct bpf_link *link, struct seq_file *seq) { struct bpf_tracing_multi_link *tr_link =3D container_of(link, struct bpf_tracing_multi_link, link); bool has_cookies =3D !!tr_link->cookies; + bool has_progs =3D !!tr_link->progs; =20 seq_printf(seq, "attach_type:\t%u\n", tr_link->link.attach_type); seq_printf(seq, "cnt:\t%u\n", tr_link->nodes_cnt); =20 - seq_printf(seq, "%s\t %s\t %s\t %s\n", "obj-id", "btf-id", "cookie", "fun= c"); + seq_printf(seq, "%s\t %s\t %s\t %s\n", has_progs ? "prog-id" : "obj-id", = "btf-id", "cookie", + "func"); for (int i =3D 0; i < tr_link->nodes_cnt; i++) { struct bpf_tracing_multi_node *mnode =3D &tr_link->nodes[i]; + u64 cookie =3D has_cookies ? tr_link->cookies[i] : 0; u32 btf_id, obj_id; =20 bpf_trampoline_unpack_key(mnode->trampoline->key, &obj_id, &btf_id); - seq_printf(seq, "%u\t %u\t %llu\t %pS\n", - obj_id, btf_id, - has_cookies ? tr_link->cookies[i] : 0, - (void *) mnode->trampoline->ip); + btf_id =3D has_progs ? (u32) mnode->trampoline->key : btf_id; + if (has_progs) + seq_printf(seq, "%u\t %u\t %llu\t %s\n", + obj_id, btf_id, cookie, + bpf_prog_func_name(tr_link->progs[i], btf_id)); + else + seq_printf(seq, "%u\t %u\t %llu\t %pS\n", + obj_id, btf_id, cookie, + (void *) mnode->trampoline->ip); =20 cond_resched(); } --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 329C43CF1FA for ; Sun, 9 Aug 2026 15:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287784; cv=none; b=uy2HOOJ8ZHfoGskGTIvmxiZWkViLTpfosaQXHXxlYkx1/phfxvIOmSSVpyENtAxUZn/uei/vklfRWBWFhH9kSoUiKeEHWaavioNGfQdkOcZEU4pixPh4Em9lBXG8+ik9pzuSJZehkf3Zu7p9VwWWgOekk+tOmW6zC3qN6Qi8NRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287784; c=relaxed/simple; bh=3/IN5wnJnEt5cI0QjVc3/5Pf29pwFGxdMq4On/UPNyQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OC3e3P/MJDT33TbjUzTfoYE1x1WUh1Ux4P1aihXGsF9FQE8ip53Hem25kwTFSbKW2uDIZUvifW0v8MM5OWPlDpc9GKY1H3f8G2XmKLfZaka9Ou98L67xYG4kW5Hb5/ruBFA9knWnYbWLCLviqBHt+dlRJmgwujE8dqrxAkrlA4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=mIa9O170; arc=none smtp.client-ip=91.218.175.188 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) header.d=linux.dev header.i=@linux.dev header.b="mIa9O170" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287777; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18gWgexAqBcbw9foGzmRcScDxWxgQFtJ1Mv18hB9TDc=; b=mIa9O1705R0HQKSMIgQylNB89rq1gUSmRH7UUeQxUhugPCndj8yXW9hIE0ORbN1MqKuNh2 YXG575xe/7BuwBOuIhkX18G+6zBprO/CJpfsXFtlqBA1+moswwgIT2NIhiMA7NZdfKqN8k 5F+HyO26/jllts2R4RWYN8IreddAxhg= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 07/13] bpf: Add tracing_multi link info support for bpf progs Date: Sun, 9 Aug 2026 23:01:05 +0800 Message-ID: <20260809150111.45000-8-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Show bpf prog info in the link info. $ bpftool link 9: tracing_multi prog 25 attach_type trace_fsession_multi btf_obj_id 1 count 2 prog_id func_btf_id addr cookie func [= module] 21 4 ffffffffc080064c 16706819 target= _1 [bpf] 22 5 ffffffffc08006d4 16706819 target= _2 [bpf] pids test_progs(98) Signed-off-by: Leon Hwang --- include/uapi/linux/bpf.h | 4 ++- kernel/trace/bpf_trace.c | 25 ++++++++++---- tools/bpf/bpftool/link.c | 59 +++++++++++++++++++++++++++------- tools/include/uapi/linux/bpf.h | 4 ++- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6fa93a6ff54f..e0c8ea5ed2c8 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6890,10 +6890,12 @@ struct bpf_link_info { __u32 attach_type; __u32 count; /* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32; + __u32 tgt_progs:1; + __u32 :31; __aligned_u64 ids; __aligned_u64 addrs; __aligned_u64 cookies; + __aligned_u64 func_btf_ids; } tracing_multi; struct { __u32 type; /* enum bpf_perf_event_type */ diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 80928401830c..d998e7ea563d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3707,15 +3707,16 @@ static void bpf_tracing_multi_link_dealloc(struct b= pf_link *link) static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *li= nk, struct bpf_link_info *info) { + u32 __user *fids =3D u64_to_user_ptr(info->tracing_multi.func_btf_ids); u64 __user *ucookies =3D u64_to_user_ptr(info->tracing_multi.cookies); u64 __user *uaddrs =3D u64_to_user_ptr(info->tracing_multi.addrs); u32 __user *uids =3D u64_to_user_ptr(info->tracing_multi.ids); struct bpf_tracing_multi_link *tr_link; u32 ucount =3D info->tracing_multi.count; - bool has_cookies, show_addrs; + bool has_cookies, show_addrs, has_progs; int err =3D 0; =20 - if ((uids || ucookies || uaddrs) && !ucount) + if ((uids || ucookies || uaddrs || fids) && !ucount) return -EINVAL; =20 tr_link =3D container_of(link, struct bpf_tracing_multi_link, link); @@ -3723,9 +3724,12 @@ static int bpf_tracing_multi_link_fill_link_info(con= st struct bpf_link *link, info->tracing_multi.attach_type =3D tr_link->link.attach_type; info->tracing_multi.count =3D tr_link->nodes_cnt; info->tracing_multi.btf_obj_id =3D btf_obj_id(tr_link->link.prog->aux->at= tach_btf); + info->tracing_multi.tgt_progs =3D has_progs =3D !!tr_link->progs; =20 - if (!uids && !ucookies && !uaddrs) + if (!uids && !ucookies && !uaddrs && !fids) return 0; + if (has_progs !=3D !!fids) + return -EINVAL; =20 if (ucount < tr_link->nodes_cnt) err =3D -ENOSPC; @@ -3737,20 +3741,27 @@ static int bpf_tracing_multi_link_fill_link_info(co= nst struct bpf_link *link, =20 for (int i =3D 0; i < ucount; i++) { struct bpf_tracing_multi_node *mnode =3D &tr_link->nodes[i]; + u32 prog_id, btf_id; u64 addr, cookie; - u32 id; =20 - bpf_trampoline_unpack_key(mnode->trampoline->key, NULL, &id); + bpf_trampoline_unpack_key(mnode->trampoline->key, &prog_id, &btf_id); =20 - addr =3D show_addrs ? mnode->trampoline->ip : 0; + if (!show_addrs) + addr =3D 0; + else if (has_progs) + addr =3D (u64) mnode->trampoline->func.addr; + else + addr =3D mnode->trampoline->ip; cookie =3D has_cookies ? tr_link->cookies[i] : 0; =20 - if (uids && put_user(id, uids + i)) + if (uids && put_user(has_progs ? prog_id : btf_id, uids + i)) return -EFAULT; if (uaddrs && put_user(addr, uaddrs + i)) return -EFAULT; if (ucookies && put_user(cookie, ucookies + i)) return -EFAULT; + if (has_progs && put_user((u32) mnode->trampoline->key, fids + i)) + return -EFAULT; =20 cond_resched(); } diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 088d1d206065..d5c5ec433350 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -422,12 +422,30 @@ show_uprobe_multi_json(struct bpf_link_info *info, js= on_writer_t *wtr) jsonw_end_array(json_wtr); } =20 +#define BPF_PROG_PREFIX "bpf_prog_" +#define BPF_PROG_PREFIX_LEN (sizeof(BPF_PROG_PREFIX) - 1) + +static const char * +sym_name_trim_prefix(struct kernel_sym *sym, bool is_prog) +{ + int prefix_len; + + if (!is_prog) + return sym->name; + + /* Ref kernel/bpf/core.c::bpf_prog_ksym_set_name() */ + prefix_len =3D BPF_PROG_PREFIX_LEN; + prefix_len +=3D BPF_TAG_SIZE * 2; + prefix_len +=3D 1; /* skip the '_' */ + return sym->name + prefix_len; +} + static void show_tracing_multi_json(struct bpf_link_info *info, json_writer_t *wtr) { - bool is_ibt_enabled =3D is_x86_ibt_enabled(), show_symbol; + bool is_ibt_enabled =3D is_x86_ibt_enabled(), show_symbol, tgt_progs; __u64 *addrs, *cookies; - __u32 i, *ids; + __u32 i, *ids, *fids; =20 if (!dd.sym_count) kernel_syms_load(&dd); @@ -443,6 +461,8 @@ show_tracing_multi_json(struct bpf_link_info *info, jso= n_writer_t *wtr) ids =3D u64_to_u32_arr(info->tracing_multi.ids); addrs =3D u64_to_arr(info->tracing_multi.addrs); cookies =3D u64_to_arr(info->tracing_multi.cookies); + fids =3D u64_to_u32_arr(info->tracing_multi.func_btf_ids); + tgt_progs =3D info->tracing_multi.tgt_progs; =20 for (i =3D 0; i < info->tracing_multi.count; i++) { struct kernel_sym *sym; @@ -451,10 +471,12 @@ show_tracing_multi_json(struct bpf_link_info *info, j= son_writer_t *wtr) sym =3D show_symbol ? find_kernel_sym_by_addr(addr, is_ibt_enabled) : NU= LL; =20 jsonw_start_object(wtr); - jsonw_uint_field(wtr, "id", ids[i]); + jsonw_uint_field(wtr, tgt_progs ? "prog_id" : "id", ids[i]); + if (tgt_progs) + jsonw_uint_field(wtr, "func_btf_id", fids[i]); jsonw_uint_field(wtr, "addr", addr); if (sym) { - jsonw_string_field(wtr, "func", sym->name); + jsonw_string_field(wtr, "func", sym_name_trim_prefix(sym, tgt_progs)); if (sym->module[0] =3D=3D '\0') { jsonw_name(wtr, "module"); jsonw_null(wtr); @@ -903,9 +925,9 @@ static void show_uprobe_multi_plain(struct bpf_link_inf= o *info) =20 static void show_tracing_multi_plain(struct bpf_link_info *info) { - bool is_ibt_enabled =3D is_x86_ibt_enabled(), show_symbol; + bool is_ibt_enabled =3D is_x86_ibt_enabled(), show_symbol, tgt_progs; __u64 *addrs, *cookies; - __u32 i, *ids; + __u32 i, *ids, *fids; =20 if (!info->tracing_multi.count) return; @@ -919,12 +941,18 @@ static void show_tracing_multi_plain(struct bpf_link_= info *info) printf("btf_obj_id %u ", info->tracing_multi.btf_obj_id); printf("count %u ", info->tracing_multi.count); =20 - printf("\n\t%-16s %-16s %-16s %s", - "btf_id", "addr", "cookie", "func [module]"); + tgt_progs =3D info->tracing_multi.tgt_progs; + if (tgt_progs) + printf("\n\t%-16s %-16s %-16s %-16s %s", + "prog_id", "func_btf_id", "addr", "cookie", "func [module]"); + else + printf("\n\t%-16s %-16s %-16s %s", + "btf_id", "addr", "cookie", "func [module]"); =20 ids =3D u64_to_u32_arr(info->tracing_multi.ids); addrs =3D u64_to_arr(info->tracing_multi.addrs); cookies =3D u64_to_arr(info->tracing_multi.cookies); + fids =3D u64_to_u32_arr(info->tracing_multi.func_btf_ids); =20 for (i =3D 0; i < info->tracing_multi.count; i++) { __u64 addr =3D addrs[i]; @@ -932,9 +960,12 @@ static void show_tracing_multi_plain(struct bpf_link_i= nfo *info) =20 sym =3D show_symbol ? find_kernel_sym_by_addr(addr, is_ibt_enabled) : NU= LL; =20 - printf("\n\t%-16u %016llx %-16llu", ids[i], addr, cookies[i]); + if (tgt_progs) + printf("\n\t%-16u %-16u %016llx %-16llu", ids[i], fids[i], addr, cookie= s[i]); + else + printf("\n\t%-16u %016llx %-16llu", ids[i], addr, cookies[i]); if (sym) { - printf(" %s", sym->name); + printf(" %s", sym_name_trim_prefix(sym, tgt_progs)); if (sym->module[0] !=3D '\0') printf(" [%s]", sym->module); } @@ -1140,7 +1171,7 @@ static int show_link_close_plain(int fd, struct bpf_l= ink_info *info) static int do_show_link(int fd) { __u64 *ref_ctr_offsets =3D NULL, *offsets =3D NULL, *cookies =3D NULL; - __u32 *ids =3D NULL; + __u32 *ids =3D NULL, *func_btf_ids =3D NULL; struct bpf_link_info info; __u32 len =3D sizeof(info); char path_buf[PATH_MAX]; @@ -1232,9 +1263,11 @@ static int do_show_link(int fd) ids =3D calloc(count, sizeof(__u32)); addrs =3D calloc(count, sizeof(__u64)); cookies =3D calloc(count, sizeof(__u64)); - if (!ids || !addrs || !cookies) { + func_btf_ids =3D info.tracing_multi.tgt_progs ? calloc(count, sizeof(__= u32)) : NULL; + if (!ids || !addrs || !cookies || (info.tracing_multi.tgt_progs && !fun= c_btf_ids)) { p_err("mem alloc failed"); close(fd); + free(func_btf_ids); free(cookies); free(addrs); free(ids); @@ -1243,6 +1276,7 @@ static int do_show_link(int fd) info.tracing_multi.ids =3D ptr_to_u64(ids); info.tracing_multi.addrs =3D ptr_to_u64(addrs); info.tracing_multi.cookies =3D ptr_to_u64(cookies); + info.tracing_multi.func_btf_ids =3D ptr_to_u64(func_btf_ids); goto again; } } @@ -1282,6 +1316,7 @@ static int do_show_link(int fd) show_link_close_plain(fd, &info); =20 free(ref_ctr_offsets); + free(func_btf_ids); free(cookies); free(offsets); free(addrs); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6fa93a6ff54f..e0c8ea5ed2c8 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6890,10 +6890,12 @@ struct bpf_link_info { __u32 attach_type; __u32 count; /* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32; + __u32 tgt_progs:1; + __u32 :31; __aligned_u64 ids; __aligned_u64 addrs; __aligned_u64 cookies; + __aligned_u64 func_btf_ids; } tracing_multi; struct { __u32 type; /* enum bpf_perf_event_type */ --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (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 97B0E3CB561 for ; Sun, 9 Aug 2026 15:03:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287791; cv=none; b=jFKHCKMmfUJtF/OexbQC+jbJoSuK4Elyp0XlBVpUSALoYjU0lqFW+Vu1CBYWYD7Bt8I1fpZSvO8j4/5jTkLaY6VEcfLtKBbhV9X3ZjC+z2UxCGWdyJtNy1oxduW9n0RnYusmdNnTL2QWH7sTCN/zoRxK6xqjogkI0Y1FQHCifaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287791; c=relaxed/simple; bh=P+XpgSYtuURb8u/F/GNzQ+ZudQ36OmDlkZsdItpC/ps=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZZI15Yps6bxv/e6UqSTIXuEM5dw/hPvV1sCac0kfwKvpBykN8FSmKb5WD8FfvdkoxcBGg+a8xnHUX+4405JLtFHlpHHm6HAG7NoM8tn3egTFuf/wjTqAICEPg3RbF8WVX6O6Zt9HVNxF1YuSiIxLnZr30+WDfTuJeiMRUFh3vx4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=YSJFSfDi; arc=none smtp.client-ip=91.218.175.170 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) header.d=linux.dev header.i=@linux.dev header.b="YSJFSfDi" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287786; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pJmQ4Z13HqELfI6OaXH9YpMv3G6mpuQ7rIyCRb6Dlsg=; b=YSJFSfDiSN8pOmHbHlq65t38gl2KBrq2jy/X7gCu9xS92SYpkEb7vblPFmyAjuikFzDTyv a6F3X4tk1LhhZoRu5Ez/upKjn03Opv1xW5klMBQlxa8GujIj1alrO40Hy9Xiw/wAVvb4ns 4MYvVk6zf//BD6CAbzgY7oyqOXm5h9k= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 08/13] selftests/bpf: Add tracing_multi bpf prog attach test Date: Sun, 9 Aug 2026 23:01:06 +0800 Message-ID: <20260809150111.45000-9-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Attach to two bpf progs with fentry.multi, fexit.multi, and fsession.multi progs. Assisted-by: Codex:gpt-5.5 Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/tracing_multi.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 52 ++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c b/tools= /testing/selftests/bpf/prog_tests/tracing_multi.c index 0aa9532a05cf..5ecb751af9be 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c @@ -12,6 +12,7 @@ #include "tracing_multi_verifier.skel.h" #include "tracing_multi_bench.skel.h" #include "tracing_multi_rollback.skel.h" +#include "tracing_multi_bpf.skel.h" #include "trace_helpers.h" =20 static __u64 bpf_fentry_test_cookies[] =3D { @@ -288,6 +289,73 @@ static void test_link_api_ids(bool test_cookies) free(ids); } =20 +static int run_bpf_target(struct bpf_program *prog, __u32 retval, const ch= ar *name) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + int err; + + err =3D bpf_prog_test_run_opts(bpf_program__fd(prog), &topts); + if (!ASSERT_OK(err, name)) + return err; + + if (!ASSERT_EQ(topts.retval, retval, name)) + return -EINVAL; + + return 0; +} + +static void test_link_api_bpf_prog(void) +{ + const char *funcs[] =3D { "target_1", "target_2" }; + LIBBPF_OPTS(bpf_tracing_multi_opts, opts); + struct tracing_multi_bpf *skel =3D NULL; + int fds[ARRAY_SIZE(funcs)]; + struct bpf_link *link; + + skel =3D tracing_multi_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tracing_multi_bpf__open_and_load")) + return; + + fds[0] =3D bpf_program__fd(skel->progs.target_1); + fds[1] =3D bpf_program__fd(skel->progs.target_2); + + opts.fds =3D fds; + opts.funcs =3D funcs; + opts.cnt =3D ARRAY_SIZE(fds); + + link =3D bpf_program__attach_tracing_multi(skel->progs.test_fentry, NULL,= &opts); + if (!ASSERT_OK_PTR(link, "attach_fentry")) + goto cleanup; + skel->links.test_fentry =3D link; + + link =3D bpf_program__attach_tracing_multi(skel->progs.test_fexit, NULL, = &opts); + if (!ASSERT_OK_PTR(link, "attach_fexit")) + goto cleanup; + skel->links.test_fexit =3D link; + + link =3D bpf_program__attach_tracing_multi(skel->progs.test_fsession, NUL= L, &opts); + if (!ASSERT_OK_PTR(link, "attach_fsession")) + goto cleanup; + skel->links.test_fsession =3D link; + + if (run_bpf_target(skel->progs.target_1, 1, "target_1")) + goto cleanup; + if (run_bpf_target(skel->progs.target_2, 2, "target_2")) + goto cleanup; + + ASSERT_EQ(skel->bss->target_1_result, 1, "target_1_result"); + ASSERT_EQ(skel->bss->target_2_result, 1, "target_2_result"); + ASSERT_EQ(skel->bss->test_result_fentry, ARRAY_SIZE(funcs), "test_result_= fentry"); + ASSERT_EQ(skel->bss->test_result_fexit, ARRAY_SIZE(funcs), "test_result_f= exit"); + ASSERT_EQ(skel->bss->test_result_fsession_entry, ARRAY_SIZE(funcs), + "test_result_fsession_entry"); + ASSERT_EQ(skel->bss->test_result_fsession_exit, ARRAY_SIZE(funcs), + "test_result_fsession_exit"); + +cleanup: + tracing_multi_bpf__destroy(skel); +} + static void test_module_skel_api(void) { struct tracing_multi_module *skel =3D NULL; @@ -1026,4 +1094,6 @@ void test_tracing_multi_test(void) RUN_TESTS(tracing_multi_verifier); if (test__start_subtest("fentry_after_multi")) test_fentry_after_multi(); + if (test__start_subtest("link_api_bpf_prog")) + test_link_api_bpf_prog(); } diff --git a/tools/testing/selftests/bpf/progs/tracing_multi_bpf.c b/tools/= testing/selftests/bpf/progs/tracing_multi_bpf.c new file mode 100644 index 000000000000..3e02602558ad --- /dev/null +++ b/tools/testing/selftests/bpf/progs/tracing_multi_bpf.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +__u64 test_result_fentry; +__u64 test_result_fexit; +__u64 test_result_fsession_entry; +__u64 test_result_fsession_exit; +__u64 target_1_result; +__u64 target_2_result; + +SEC("syscall") +int target_1(void *ctx) +{ + target_1_result++; + return 1; +} + +SEC("syscall") +int target_2(void *ctx) +{ + target_2_result++; + return 2; +} + +SEC("fentry.multi") +int BPF_PROG(test_fentry) +{ + test_result_fentry++; + return 0; +} + +SEC("fexit.multi") +int BPF_PROG(test_fexit) +{ + test_result_fexit++; + return 0; +} + +SEC("fsession.multi") +int BPF_PROG(test_fsession) +{ + if (bpf_session_is_return(ctx)) + test_result_fsession_exit++; + else + test_result_fsession_entry++; + + return 0; +} --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 86D7C257844 for ; Sun, 9 Aug 2026 15:03:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287798; cv=none; b=JT/6CR/AVtvEqVwQPKAbh67xhamRyB8WUFNMRXPCdco3+jFRgDd/RNG2jv9+upwxuecYkCZ7yEXvVWLdEJg2ut49dQljSojEhciKjMx2A4o8bimpAmtDXLFMHBLIZY4RaaW7iO2VniIjQPt0LM3gc+P0JRezqywrxGy7iFTUSMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287798; c=relaxed/simple; bh=pbHf0D/yxYYzwp2ghuYK3J09LjXB6a1N7iXNw2hCyM0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ffXYCNwckyJ9snlPEj0IOwI30PuOB512W2a7qr/tDu5eT+iC043nuvF+iwMzxuydpuLB196hVnqF/o/6seH8uxMjQJt7oCC6rnO4zZoYf6AMMskkwKGxtrjtb+sNc6UXE+yzJzpu8Nwb1y77kV7vXroy8annrNoIV6KiZHAKDR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=bsAD89zh; arc=none smtp.client-ip=91.218.175.188 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) header.d=linux.dev header.i=@linux.dev header.b="bsAD89zh" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z5mOx5lt73FP0tlgvSP29wgB7UHvAV6W77wkgAeURVo=; b=bsAD89zhSA45J5+Zn50gFy+SH4eep1b9V9sv5utbiA0a4Rt3gtt/+dItjs7q88LBcYnBvS +yRBv9ulRtucXmcL0/9C2n8uLej/UffNfdVAlQl/TQee+857Ym6ON2zHUt4q948s0WvtMg bOno5/CA/jkNcvDdiITNzYM9vISBECU= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 09/13] selftests/bpf: Add tracing_multi bpf prog attach failure tests Date: Sun, 9 Aug 2026 23:01:07 +0800 Message-ID: <20260809150111.45000-10-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Tests against two libbpf failures and two kernel failures: * libbpf: 'ids' + 'fds'. * libbpf: 'pattern' + 'fds'. * kernel: 'fds' + invalid 'ids'. * kernel: duplicate targets via different fd for the same bpf prog + func. Assisted-by: Codex:gpt-5.5 Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/tracing_multi.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c b/tools= /testing/selftests/bpf/prog_tests/tracing_multi.c index 5ecb751af9be..5cf11707eaf8 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c @@ -730,6 +730,104 @@ static void test_attach_api_fails(void) free(ids2); } =20 +static void test_attach_api_bpf_prog_fails(void) +{ + static const char * const kfuncs[] =3D { + "bpf_fentry_test1", + }; + const char *bpf_funcs[] =3D { "target_1" }; + LIBBPF_OPTS(bpf_tracing_multi_opts, opts); + LIBBPF_OPTS(bpf_link_create_opts, lopts); + struct tracing_multi_bpf *skel =3D NULL; + int fd, target_id, token_fd; + int fds[2], dup_fd =3D -1; + struct bpf_link *link; + __u32 target_ids[2]; + __u32 *ids =3D NULL; + long err; + + skel =3D tracing_multi_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tracing_multi_bpf__open_and_load")) + return; + + ids =3D get_ids(kfuncs, ARRAY_SIZE(kfuncs), NULL); + if (!ASSERT_OK_PTR(ids, "get_ids")) + goto cleanup; + + fds[0] =3D bpf_program__fd(skel->progs.target_1); + + /* fail#1 (libbpf) kernel ids and BPF prog fds */ + LIBBPF_OPTS_RESET(opts, + .ids =3D ids, + .fds =3D fds, + .funcs =3D bpf_funcs, + .cnt =3D ARRAY_SIZE(bpf_funcs), + ); + + link =3D bpf_program__attach_tracing_multi(skel->progs.test_fentry, NULL,= &opts); + err =3D libbpf_get_error(link); + if (!ASSERT_EQ(err, -EINVAL, "libbpf_ids_fds")) { + if (!err) + bpf_link__destroy(link); + goto cleanup; + } + + /* fail#2 (libbpf) kernel pattern and BPF prog fds */ + LIBBPF_OPTS_RESET(opts, + .fds =3D fds, + .funcs =3D bpf_funcs, + .cnt =3D ARRAY_SIZE(bpf_funcs), + ); + + link =3D bpf_program__attach_tracing_multi(skel->progs.test_fentry, + "bpf_fentry_test*", &opts); + err =3D libbpf_get_error(link); + if (!ASSERT_EQ(err, -EINVAL, "libbpf_pattern_fds")) { + if (!err) + bpf_link__destroy(link); + goto cleanup; + } + + /* fail#3 (kernel) kernel id with BPF prog fd */ + lopts.tracing_multi.ids =3D ids; + lopts.tracing_multi.fds =3D fds; + lopts.tracing_multi.cnt =3D ARRAY_SIZE(bpf_funcs); + fd =3D bpf_link_create(bpf_program__fd(skel->progs.test_fentry), 0, + BPF_TRACE_FENTRY_MULTI, &lopts); + ASSERT_EQ(fd, -EINVAL, "kernel_id_bpf_fd"); + if (fd >=3D 0) + close(fd); + + /* fail#4 (kernel) duplicate target through different fds */ + token_fd =3D bpf_object__token_fd(skel->obj); + token_fd =3D token_fd < 0 ? 0 : token_fd; + target_id =3D libbpf_find_prog_btf_id("target_1", fds[0], token_fd); + if (!ASSERT_GT(target_id, 0, "target_1_btf_id")) + goto cleanup; + + dup_fd =3D dup(fds[0]); + if (!ASSERT_GE(dup_fd, 0, "dup_target_fd")) + goto cleanup; + + fds[1] =3D dup_fd; + target_ids[0] =3D target_id; + target_ids[1] =3D target_id; + lopts.tracing_multi.ids =3D target_ids; + lopts.tracing_multi.fds =3D fds; + lopts.tracing_multi.cnt =3D ARRAY_SIZE(fds); + fd =3D bpf_link_create(bpf_program__fd(skel->progs.test_fentry), 0, + BPF_TRACE_FENTRY_MULTI, &lopts); + ASSERT_EQ(fd, -EINVAL, "duplicate_bpf_target"); + if (fd >=3D 0) + close(fd); + +cleanup: + if (dup_fd >=3D 0) + close(dup_fd); + tracing_multi_bpf__destroy(skel); + free(ids); +} + void serial_test_tracing_multi_bench_attach(void) { LIBBPF_OPTS(bpf_tracing_multi_opts, opts); @@ -1096,4 +1194,6 @@ void test_tracing_multi_test(void) test_fentry_after_multi(); if (test__start_subtest("link_api_bpf_prog")) test_link_api_bpf_prog(); + if (test__start_subtest("attach_api_bpf_prog_fails")) + test_attach_api_bpf_prog_fails(); } --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (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 7568234887B for ; Sun, 9 Aug 2026 15:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287820; cv=none; b=dYqaK6RpsY5FjVd/R6E17MABxOAjSQFBHpJlglFN0cYohh/+gux9O6DQWTwVHy9WVGfA+DONPWkLBsnpc781VDndrSOunjgJYE92JBDCXdq+IKdx9a6OFyNfTEYAXTaFRlhStj4wgPwElqrPg8IizV6PuHrgQcARzDmN8QayXXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287820; c=relaxed/simple; bh=boqQ747tfQaFagRvmdFYTI9yhU/TokaN62heSKuPkoI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LT+oOcSjV7ItHgoLOgNWAe0JLJD9FPFJIcC3Ak00UMA4N484md5Rnb9FNpvLiXP1ijkBE9RDILx55ITQaKrSaiEGsy/zyIkLLaBts3ePOZN2uHEEG1Jv+b3IclL1MORto1WkWOW7u0Ud5ZwkZtjtDDTEJkvjc8U8TxhZOEkVtdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rkh+ODk3; arc=none smtp.client-ip=91.218.175.180 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) header.d=linux.dev header.i=@linux.dev header.b="rkh+ODk3" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JZNBCvDZ4wsi/VaiOyMJSfJG8oJ1nr2lIgU1xAz5DUM=; b=rkh+ODk3DWDxpDTOQw6lkm+r0DDv70LKJ1b9xrwzPBth/UHXLRFpm9OBYwbSY/b0nnDLRY Oq76SH0KC9tut8LmQY7VSvvikgbkyUlE72FsHrGw+CsMRjfVWO5oEBOaEzQphhgOFEl8we fYv+nga+R5CfSYkOE+gElYMNvtcyhxs= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 10/13] selftests/bpf: Add tracing_multi bpf prog cookie test Date: Sun, 9 Aug 2026 23:01:08 +0800 Message-ID: <20260809150111.45000-11-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Verify that the cookie also works for bpf prog targets of tracing_multi link. Assisted-by: Codex:gpt-5.5 Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/tracing_multi.c | 25 ++++++++++++++++++- .../selftests/bpf/progs/tracing_multi_bpf.c | 13 ++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c b/tools= /testing/selftests/bpf/prog_tests/tracing_multi.c index 5cf11707eaf8..070266a864d4 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c @@ -304,9 +304,12 @@ static int run_bpf_target(struct bpf_program *prog, __= u32 retval, const char *na return 0; } =20 -static void test_link_api_bpf_prog(void) +static void __test_link_api_bpf_prog(bool test_cookies) { const char *funcs[] =3D { "target_1", "target_2" }; + __u64 fentry_cookies[] =3D { 0xfeed01, 0xfeed01 }; + __u64 fexit_cookies[] =3D { 0xfeed02, 0xfeed02 }; + __u64 fsession_cookies[] =3D { 0xfeed03, 0xfeed03 }; LIBBPF_OPTS(bpf_tracing_multi_opts, opts); struct tracing_multi_bpf *skel =3D NULL; int fds[ARRAY_SIZE(funcs)]; @@ -319,20 +322,28 @@ static void test_link_api_bpf_prog(void) fds[0] =3D bpf_program__fd(skel->progs.target_1); fds[1] =3D bpf_program__fd(skel->progs.target_2); =20 + skel->bss->test_cookies =3D test_cookies; + skel->bss->fentry_cookie =3D fentry_cookies[0]; + skel->bss->fexit_cookie =3D fexit_cookies[0]; + skel->bss->fsession_cookie =3D fsession_cookies[0]; + opts.fds =3D fds; opts.funcs =3D funcs; opts.cnt =3D ARRAY_SIZE(fds); =20 + opts.cookies =3D test_cookies ? fentry_cookies : NULL; link =3D bpf_program__attach_tracing_multi(skel->progs.test_fentry, NULL,= &opts); if (!ASSERT_OK_PTR(link, "attach_fentry")) goto cleanup; skel->links.test_fentry =3D link; =20 + opts.cookies =3D test_cookies ? fexit_cookies : NULL; link =3D bpf_program__attach_tracing_multi(skel->progs.test_fexit, NULL, = &opts); if (!ASSERT_OK_PTR(link, "attach_fexit")) goto cleanup; skel->links.test_fexit =3D link; =20 + opts.cookies =3D test_cookies ? fsession_cookies : NULL; link =3D bpf_program__attach_tracing_multi(skel->progs.test_fsession, NUL= L, &opts); if (!ASSERT_OK_PTR(link, "attach_fsession")) goto cleanup; @@ -356,6 +367,16 @@ static void test_link_api_bpf_prog(void) tracing_multi_bpf__destroy(skel); } =20 +static void test_link_api_bpf_prog(void) +{ + __test_link_api_bpf_prog(false); +} + +static void test_cookies_bpf_prog(void) +{ + __test_link_api_bpf_prog(true); +} + static void test_module_skel_api(void) { struct tracing_multi_module *skel =3D NULL; @@ -1196,4 +1217,6 @@ void test_tracing_multi_test(void) test_link_api_bpf_prog(); if (test__start_subtest("attach_api_bpf_prog_fails")) test_attach_api_bpf_prog_fails(); + if (test__start_subtest("cookies_bpf_prog")) + test_cookies_bpf_prog(); } diff --git a/tools/testing/selftests/bpf/progs/tracing_multi_bpf.c b/tools/= testing/selftests/bpf/progs/tracing_multi_bpf.c index 3e02602558ad..651b5d86203f 100644 --- a/tools/testing/selftests/bpf/progs/tracing_multi_bpf.c +++ b/tools/testing/selftests/bpf/progs/tracing_multi_bpf.c @@ -5,6 +5,10 @@ =20 char _license[] SEC("license") =3D "GPL"; =20 +bool test_cookies; +__u64 fentry_cookie; +__u64 fexit_cookie; +__u64 fsession_cookie; __u64 test_result_fentry; __u64 test_result_fexit; __u64 test_result_fsession_entry; @@ -29,20 +33,25 @@ int target_2(void *ctx) SEC("fentry.multi") int BPF_PROG(test_fentry) { - test_result_fentry++; + if (!test_cookies || bpf_get_attach_cookie(ctx) =3D=3D fentry_cookie) + test_result_fentry++; return 0; } =20 SEC("fexit.multi") int BPF_PROG(test_fexit) { - test_result_fexit++; + if (!test_cookies || bpf_get_attach_cookie(ctx) =3D=3D fexit_cookie) + test_result_fexit++; return 0; } =20 SEC("fsession.multi") int BPF_PROG(test_fsession) { + if (test_cookies && bpf_get_attach_cookie(ctx) !=3D fsession_cookie) + return 0; + if (bpf_session_is_return(ctx)) test_result_fsession_exit++; else --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (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 50F5B3CF041; Sun, 9 Aug 2026 15:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287828; cv=none; b=BRnj3zyALKuYofm54OfG1Ydzu1ok2cZTJPk1vd72CjjRSvsqbikFcMYfNBE0BDTLKxGG+H23nanzkXVgmlbtZhgHdEKsTOf94iVuM533yOnES0d3+qlBv+DBB/9khlX/SQrEVqB8+VGdS/Lrkvzhm2BFwFMon1spdItmfQA9VgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287828; c=relaxed/simple; bh=1vOP+z2LX9aSUYzy+aDrDhpFgod+obE8Z0hNEUWLF3A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ucfmmXDVGPjMBuFWPDSXO1MtnwJgiD+bRsnzqpCJdXkDPxdxcblJQQx+GScLaelCQuTQksLCryqQjIWK8CnoJR5NtrR2SR7HszMfbQERSkhf1UV+qpAHSUVc6qpy8RTW+5oqIbscATrnZqAhgeOoc3uJ9TuPGaP5uUNp4vqEcl8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=CT6KeBkw; arc=none smtp.client-ip=91.218.175.182 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) header.d=linux.dev header.i=@linux.dev header.b="CT6KeBkw" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287824; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QGjoJlDAtsVtLGlFyV0FUEW111v48ECa1+dfAaHklbM=; b=CT6KeBkwBubhCRTMC61yUG9TaQC42aKx+6FSlHk/mhYlK/eeQXamhyhYgRrm7bUxTVyVpO xIjYjpO2Q4Cqm0vRe/E+gTQkdmwGr4/GRD/PC2DojhokvA7Za9e66SDkD3zCTul/xqU00M 86hKkZPE72MHfXWHd0Kqjqj5qx4MZHs= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 11/13] selftests/bpf: Add tracing_multi bpf prog rollback test Date: Sun, 9 Aug 2026 23:01:09 +0800 Message-ID: <20260809150111.45000-12-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When rollbacking, the prepared trampolines should be reverted. In this test, the first bpf prog will be attached with the trampoline, and the second bpf prog won't be attached because the bpf prog has been attached by a fentry.multi prog, that causes the rollback procedure. Assisted-by: Codex:gpt-5.5 Signed-off-by: Leon Hwang --- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/tracing_multi.c | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d698a64ff800..705f50a44410 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10629,7 +10629,7 @@ int libbpf_find_vmlinux_btf_id(const char *name, return libbpf_err(err); } =20 -static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd,= int token_fd) +int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int to= ken_fd) { struct bpf_prog_info info; __u32 info_len =3D sizeof(info); diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index 7a74abb904f8..87a3828e9b8a 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -436,6 +436,7 @@ int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 log_level, int token_fd); struct btf *btf_load_from_kernel(__u32 id, struct btf *base_btf, int token= _fd); +int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd, int to= ken_fd); =20 struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf); void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c b/tools= /testing/selftests/bpf/prog_tests/tracing_multi.c index 070266a864d4..a4d9934ad1dc 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c @@ -377,6 +377,65 @@ static void test_cookies_bpf_prog(void) __test_link_api_bpf_prog(true); } =20 +static void test_rollback_bpf_prog(void) +{ + int token_fd, prog_fd, fd =3D -1, pre_fd =3D -1; + LIBBPF_OPTS(bpf_link_create_opts, lopts); + struct tracing_multi_bpf *skel =3D NULL; + __u32 target_ids[2], ids[2]; + int target_fds[2], fds[2]; + int first, second; + + skel =3D tracing_multi_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tracing_multi_bpf__open_and_load")) + return; + + token_fd =3D bpf_object__token_fd(skel->obj); + token_fd =3D token_fd < 0 ? 0 : token_fd; + target_fds[0] =3D bpf_program__fd(skel->progs.target_1); + target_fds[1] =3D bpf_program__fd(skel->progs.target_2); + target_ids[0] =3D libbpf_find_prog_btf_id("target_1", target_fds[0], toke= n_fd); + if (!ASSERT_GT(target_ids[0], 0, "target_1_btf_id")) + goto cleanup; + target_ids[1] =3D libbpf_find_prog_btf_id("target_2", target_fds[1], toke= n_fd); + if (!ASSERT_GT(target_ids[1], 0, "target_2_btf_id")) + goto cleanup; + + first =3D target_fds[0] < target_fds[1] ? 0 : 1; + second =3D 1 - first; + fds[0] =3D target_fds[first]; + fds[1] =3D target_fds[second]; + ids[0] =3D target_ids[first]; + ids[1] =3D target_ids[second]; + + prog_fd =3D bpf_program__fd(skel->progs.test_fentry); + lopts.tracing_multi.ids =3D &ids[1]; + lopts.tracing_multi.fds =3D &fds[1]; + lopts.tracing_multi.cnt =3D 1; + pre_fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FENTRY_MULTI, &lopts); + if (!ASSERT_GE(pre_fd, 0, "pre_attach")) + goto cleanup; + + lopts.tracing_multi.ids =3D ids; + lopts.tracing_multi.fds =3D fds; + lopts.tracing_multi.cnt =3D ARRAY_SIZE(ids); + + fd =3D bpf_link_create(prog_fd, 0, BPF_TRACE_FENTRY_MULTI, &lopts); + if (!ASSERT_EQ(fd, -EBUSY, "bpf_link_create")) + goto cleanup; + + if (run_bpf_target(skel->progs.target_1, first + 1, "rollback_target")) + goto cleanup; + ASSERT_EQ(skel->bss->test_result_fentry, 0, "test_result_fentry"); + +cleanup: + if (fd >=3D 0) + close(fd); + if (pre_fd >=3D 0) + close(pre_fd); + tracing_multi_bpf__destroy(skel); +} + static void test_module_skel_api(void) { struct tracing_multi_module *skel =3D NULL; @@ -1219,4 +1278,6 @@ void test_tracing_multi_test(void) test_attach_api_bpf_prog_fails(); if (test__start_subtest("cookies_bpf_prog")) test_cookies_bpf_prog(); + if (test__start_subtest("rollback_bpf_prog")) + test_rollback_bpf_prog(); } --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (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 02E3E395ADB for ; Sun, 9 Aug 2026 15:03:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287837; cv=none; b=n7mAp4psy11K60RGV0j65kj9zC6FqGPjQW7hN1rTk4P8e3bOMqGmCl420ZyV2P5gt/X+REvqhxXYdp4ai9hIJ7iG/0ofkRf/d2sNAj/roMCmbLEbpchqn5qNUIiR3yx4xsYTM1Rb3p5b+Lqqn3IXIPNuJFfey9mvI1HXpZqjwGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287837; c=relaxed/simple; bh=zBLqgxg2VWEshKBolS9K4GcVQ9RGdOlhNG5rvbUUCCM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nl296BtPhtugoVLgqedVOpeouMNpR+R1naKCBvMt6s34BqHwBT0HfqVYJ/1t0qNIqNmRjpHAmo8Vz6R6Gmlcn29O+kBFfMMbi/waESrL3dddwYVe1UxY6TvJNueqwSp4KMv2Gl+zjbpx5KY5vRkEt3x1C1C/BGp2V14/+jCXaMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Cp/uXRWo; arc=none smtp.client-ip=91.218.175.171 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) header.d=linux.dev header.i=@linux.dev header.b="Cp/uXRWo" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287832; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=muD8qsIIlK+plHOZFGvhNTizDcPRscrfaO8XrFsVISs=; b=Cp/uXRWoGzENxJNCIXTjZQRQ/SIwi8mA7GfogpQW3KpzRHR8TmaK2BEg/csikVvKXyeo9/ PG/1FV5Tq+pZxzS04aAoAnH5yNQxScYFE/L0L882mVw36EX/xCwcr6MtcWSdtPNd4ay2Cf cl0a98InUbqJtbo7POaroX/ZG+Jqi6Y= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 12/13] selftests/bpf: Add tracing_multi bpf prog link info test Date: Sun, 9 Aug 2026 23:01:10 +0800 Message-ID: <20260809150111.45000-13-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Verify the link info works well for bpf prog targets of tracing_multi link. Assisted-by: Codex:gpt-5.5 Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++++++++-- 1 file changed, 152 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/fill_link_info.c b/tool= s/testing/selftests/bpf/prog_tests/fill_link_info.c index 0918321c8e63..a5f8bfcb1e07 100644 --- a/tools/testing/selftests/bpf/prog_tests/fill_link_info.c +++ b/tools/testing/selftests/bpf/prog_tests/fill_link_info.c @@ -3,11 +3,13 @@ =20 #include #include +#include #include #include #include #include "trace_helpers.h" #include "test_fill_link_info.skel.h" +#include "tracing_multi_bpf.skel.h" #include "bpf/libbpf_internal.h" =20 #define TP_CAT "sched" @@ -39,8 +41,19 @@ struct tmulti_target { __u64 addr; __u64 cookie; __u32 id; + __u32 func_btf_id; }; =20 +static const char *tmulti_bpf_funcs[] =3D { + "target_1", + "target_2", +}; + +static __u64 tmulti_bpf_fentry_cookies[] =3D { 0xfeed01, 0xfeed01 }; +static __u64 tmulti_bpf_fexit_cookies[] =3D { 0xfeed02, 0xfeed02 }; +static __u64 tmulti_bpf_fsession_cookies[] =3D { 0xfeed03, 0xfeed03 }; +#define TRACING_MULTI_BPF_CNT ARRAY_SIZE(tmulti_bpf_funcs) + #define UPROBE_FILE "/proc/self/exe" static ssize_t uprobe_offset; /* uprobe attach point */ @@ -421,14 +434,28 @@ static int tmulti_target_cmp(const void *a, const voi= d *b) return (ta->id > tb->id) - (ta->id < tb->id); } =20 +static int setup_tmulti_btf_obj_id(const struct bpf_program *prog, __u32 *= btf_obj_id) +{ + struct bpf_prog_info prog_info =3D {}; + __u32 len =3D sizeof(prog_info); + int err; + + err =3D bpf_prog_get_info_by_fd(bpf_program__fd(prog), &prog_info, &len); + if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) + return -1; + if (!ASSERT_GT(prog_info.attach_btf_obj_id, 0, "attach_btf_obj_id")) + return -1; + + *btf_obj_id =3D prog_info.attach_btf_obj_id; + return 0; +} + static int setup_tmulti_targets(const struct bpf_program *prog, struct tmulti_target *targets, __u32 *btf_obj_id) { - struct bpf_prog_info prog_info; - __u32 len =3D sizeof(prog_info); struct btf *btf; - int err, i; + int i; __s32 id; =20 btf =3D btf__load_vmlinux_btf(); @@ -446,13 +473,8 @@ static int setup_tmulti_targets(const struct bpf_progr= am *prog, targets[i].id =3D id; } =20 - memset(&prog_info, 0, len); - err =3D bpf_prog_get_info_by_fd(bpf_program__fd(prog), &prog_info, &len); - if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) - goto error; - if (!ASSERT_GT(prog_info.attach_btf_obj_id, 0, "attach_btf_obj_id")) + if (setup_tmulti_btf_obj_id(prog, btf_obj_id)) goto error; - *btf_obj_id =3D prog_info.attach_btf_obj_id; =20 /* * The kernel tracing multi attach sorts ids. We sort as well, @@ -467,13 +489,48 @@ static int setup_tmulti_targets(const struct bpf_prog= ram *prog, return -1; } =20 +static int setup_tmulti_bpf_prog_targets(const struct bpf_program *prog, i= nt token_fd, + const __u64 *cookies, + const struct bpf_program * const *target_progs, + struct tmulti_target *targets, + __u32 *btf_obj_id) +{ + struct bpf_prog_info prog_info =3D {}; + __u32 len =3D sizeof(prog_info), func_btf_id; + int err, i, prog_fd; + + for (i =3D 0; i < TRACING_MULTI_BPF_CNT; i++) { + memset(&prog_info, 0, sizeof(prog_info)); + len =3D sizeof(prog_info); + prog_info.jited_ksyms =3D ptr_to_u64(&targets[i].addr); + prog_info.nr_jited_ksyms =3D 1; + prog_fd =3D bpf_program__fd(target_progs[i]); + err =3D bpf_prog_get_info_by_fd(prog_fd, &prog_info, &len); + if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd")) + return -1; + + func_btf_id =3D libbpf_find_prog_btf_id(tmulti_bpf_funcs[i], prog_fd, to= ken_fd); + if (!ASSERT_GT(func_btf_id, 0, "libbpf_find_prog_btf_id")) + return -1; + + targets[i].cookie =3D cookies[i]; + targets[i].id =3D prog_info.id; + targets[i].func_btf_id =3D func_btf_id; + } + + return setup_tmulti_btf_obj_id(prog, btf_obj_id); +} + static int verify_tracing_multi_link_info(int fd, const struct bpf_program= *prog, const struct tmulti_target *targets, - __u32 btf_obj_id, bool has_cookies) + __u32 btf_obj_id, __u32 count, + bool has_cookies, bool tgt_progs) { +#define TMULTI_CNT (TRACING_MULTI_CNT > TRACING_MULTI_BPF_CNT ? \ + TRACING_MULTI_CNT : TRACING_MULTI_BPF_CNT) enum bpf_attach_type attach_type =3D bpf_program__expected_attach_type(pr= og); - __u64 addrs[TRACING_MULTI_CNT], cookies[TRACING_MULTI_CNT]; - __u32 ids[TRACING_MULTI_CNT]; + __u64 addrs[TMULTI_CNT], cookies[TMULTI_CNT]; + __u32 ids[TMULTI_CNT], fids[TMULTI_CNT]; struct bpf_link_info info; __u32 len =3D sizeof(info); int err, i; @@ -487,7 +544,9 @@ static int verify_tracing_multi_link_info(int fd, const= struct bpf_program *prog return -1; =20 ASSERT_EQ(info.tracing_multi.attach_type, attach_type, "info.tracing_mult= i.attach_type"); - ASSERT_EQ(info.tracing_multi.count, TRACING_MULTI_CNT, "info.tracing_mult= i.count"); + ASSERT_EQ(info.tracing_multi.count, count, "info.tracing_multi.count"); + ASSERT_EQ((int) info.tracing_multi.tgt_progs, (int) tgt_progs, + "info.tracing_multi.tgt_progs"); =20 memset(ids, 0, sizeof(ids)); memset(cookies, 0, sizeof(cookies)); @@ -496,7 +555,8 @@ static int verify_tracing_multi_link_info(int fd, const= struct bpf_program *prog info.tracing_multi.ids =3D ptr_to_u64(ids); info.tracing_multi.addrs =3D ptr_to_u64(addrs); info.tracing_multi.cookies =3D has_cookies ? ptr_to_u64(cookies) : 0; - info.tracing_multi.count =3D TRACING_MULTI_CNT; + info.tracing_multi.func_btf_ids =3D tgt_progs ? ptr_to_u64(fids) : 0; + info.tracing_multi.count =3D count; =20 err =3D bpf_link_get_info_by_fd(fd, &info, &len); if (!ASSERT_OK(err, "bpf_link_get_info_by_fd")) @@ -506,14 +566,19 @@ static int verify_tracing_multi_link_info(int fd, con= st struct bpf_program *prog return -1; =20 ASSERT_EQ(info.tracing_multi.attach_type, attach_type, "info.tracing_mult= i.attach_type"); - ASSERT_EQ(info.tracing_multi.count, TRACING_MULTI_CNT, "info.tracing_mult= i.count"); + ASSERT_EQ(info.tracing_multi.count, count, "info.tracing_multi.count"); ASSERT_EQ(info.tracing_multi.btf_obj_id, btf_obj_id, "tracing_multi.btf_o= bj_id"); + ASSERT_EQ((int) info.tracing_multi.tgt_progs, (int) tgt_progs, + "info.tracing_multi.tgt_progs"); =20 - for (i =3D 0; i < TRACING_MULTI_CNT; i++) { + for (i =3D 0; i < count; i++) { ASSERT_EQ(ids[i], targets[i].id, "tracing_multi.ids"); ASSERT_EQ(cookies[i], has_cookies ? targets[i].cookie : 0, "tracing_mult= i.cookies"); =20 - if (targets[i].addr) { + if (tgt_progs) { + ASSERT_EQ(addrs[i], targets[i].addr, "tracing_multi.addrs"); + ASSERT_EQ(fids[i], targets[i].func_btf_id, "tracing_multi.func_btf_ids"= ); + } else if (targets[i].addr) { struct ksym *ksym; =20 if (!ASSERT_NEQ(addrs[i], 0, "tracing_multi.addrs")) @@ -624,13 +689,78 @@ static void test_tracing_multi_fill_link_info(struct = test_fill_link_info *skel, verify_tracing_multi_invalid_user_buffer(link_fd, targets); } else { err =3D verify_tracing_multi_link_info(link_fd, skel->progs.tmulti_run, - targets, btf_obj_id, has_cookies); + targets, btf_obj_id, TRACING_MULTI_CNT, + has_cookies, false); ASSERT_OK(err, "verify_tracing_multi_link_info"); } =20 bpf_link__destroy(link); } =20 +static void test_tracing_multi_bpf_fill_link_info_one(struct tracing_multi= _bpf *skel, + struct bpf_program *prog, + __u64 *cookies, + bool has_cookies) +{ + LIBBPF_OPTS(bpf_tracing_multi_opts, opts); + const struct bpf_program *target_progs[] =3D { + skel->progs.target_1, + skel->progs.target_2, + }; + struct tmulti_target targets[TRACING_MULTI_BPF_CNT] =3D {}; + int fds[TRACING_MULTI_BPF_CNT], token_fd, err, i; + struct bpf_link *link; + __u32 btf_obj_id; + + token_fd =3D bpf_object__token_fd(skel->obj); + token_fd =3D token_fd < 0 ? 0 : token_fd; + + if (setup_tmulti_bpf_prog_targets(prog, token_fd, cookies, target_progs, = targets, + &btf_obj_id)) + return; + + for (i =3D 0; i < TRACING_MULTI_BPF_CNT; i++) + fds[i] =3D bpf_program__fd(target_progs[i]); + + opts.fds =3D fds; + opts.funcs =3D tmulti_bpf_funcs; + opts.cnt =3D ARRAY_SIZE(fds); + opts.cookies =3D has_cookies ? cookies : NULL; + + link =3D bpf_program__attach_tracing_multi(prog, NULL, &opts); + if (!ASSERT_OK_PTR(link, "bpf_program__attach_tracing_multi")) + return; + + err =3D verify_tracing_multi_link_info(bpf_link__fd(link), prog, targets,= btf_obj_id, + TRACING_MULTI_BPF_CNT, has_cookies, true); + ASSERT_OK(err, "verify_tracing_multi_link_info"); + + bpf_link__destroy(link); +} + +static void test_tracing_multi_bpf_fill_link_info(bool has_cookies) +{ + struct tracing_multi_bpf *skel; + +#ifndef __x86_64__ + test__skip(); + return; +#endif + + skel =3D tracing_multi_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tracing_multi_bpf__open_and_load")) + return; + + test_tracing_multi_bpf_fill_link_info_one(skel, skel->progs.test_fentry, + tmulti_bpf_fentry_cookies, has_cookies); + test_tracing_multi_bpf_fill_link_info_one(skel, skel->progs.test_fexit, + tmulti_bpf_fexit_cookies, has_cookies); + test_tracing_multi_bpf_fill_link_info_one(skel, skel->progs.test_fsession, + tmulti_bpf_fsession_cookies, has_cookies); + + tracing_multi_bpf__destroy(skel); +} + #define SEC(name) __attribute__((section(name), used)) =20 static short uprobe_link_info_sema_1 SEC(".probes"); @@ -879,6 +1009,10 @@ void test_fill_link_info(void) test_tracing_multi_fill_link_info(skel, false, false); test_tracing_multi_fill_link_info(skel, true, false); } + if (test__start_subtest("tracing_multi_bpf_link_info")) { + test_tracing_multi_bpf_fill_link_info(false); + test_tracing_multi_bpf_fill_link_info(true); + } if (test__start_subtest("tracing_multi_invalid_ubuff")) test_tracing_multi_fill_link_info(skel, true, true); =20 --=20 2.55.0 From nobody Tue Sep 29 09:45:50 2026 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) (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 3B1E13D300D for ; Sun, 9 Aug 2026 15:04:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287844; cv=none; b=b3AToFU4r6ApP+Bk1cd5Hek7mu02Z+6b3aEb5YaYrRB8FAB+2fep/jzc3yQsa/cpvG3CHjAolddcjo5y8j5bzqLHup7+z5kAK1UC6UlMg6WF3E+Z7/NHn2IeojISGG4uG1j0edeg/fl2vpIh/DRasfyST6S17DKdLszQGBbN6Vg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786287844; c=relaxed/simple; bh=WKKvuwqoUsuRf/1ptLanZYpkM3g1G8lKnGH2y/JGobM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lK7Ks8URkyLMcMyf0o0dWo3egOZ6UulP9s1WuOGko/sF+fnHDgFWOj837DbIyHjB2P7Wnk+jZZvLkLqJHrWdxHoj9eOPZA2bCEnz3eJUr/lAjToFk3Xvi0u6OqXchB/h+JWxecdpkbvE3NBXH2XZpwcEtELsVQCNw+GDjdtb3CA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=UFhC1ofg; arc=none smtp.client-ip=91.218.175.173 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) header.d=linux.dev header.i=@linux.dev header.b="UFhC1ofg" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1786287840; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TCHhS6KjGvWcXPjdrezwj/YbVHHb8dwo6y09eVtwJRc=; b=UFhC1ofgv6ab4Bqjcjd1QnD6gQndJKsitOYd1byDyN6bIt4ao96G1fBNhA44TqiaHOKJSx 4U39mt7Zb34KKExceAOT8TsclRoN/9ylHwLzabBLBE6s3EC81VGfZrC3ult8dlgUlNzhdQ TfGacCpHbGcAiy1jsSa+kLgjF2xD3vw= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan , Leon Hwang , Mykyta Yatsenko , Avinash Duduskar , Anton Protopopov , Amery Hung , Jordan Rife , Rong Tao , Eyal Birger , Pu Lehui , Jingguo Tan , Lin Ma , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next 13/13] selftests/bpf: Test tailcall with fentry.multi Date: Sun, 9 Aug 2026 23:01:11 +0800 Message-ID: <20260809150111.45000-14-leon.hwang@linux.dev> In-Reply-To: <20260809150111.45000-1-leon.hwang@linux.dev> References: <20260809150111.45000-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Positively verify that a tail call from a BPF subprog works when the subprog is traced by fentry.multi. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Leon Hwang --- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/tailcalls.c b/tools/tes= ting/selftests/bpf/prog_tests/tailcalls.c index c5c9d6c359bb..1bef6b290f3a 100644 --- a/tools/testing/selftests/bpf/prog_tests/tailcalls.c +++ b/tools/testing/selftests/bpf/prog_tests/tailcalls.c @@ -15,6 +15,7 @@ #include "tailcall_callback.skel.h" #include "tailcall_bpf2bpf2.skel.h" #include "tailcall_bpf2bpf_fexit.skel.h" +#include "tracing_multi_bpf.skel.h" =20 /* test_tailcall_1 checks basic functionality by patching multiple locatio= ns * in a single program for a single tail call slot with nop->jmp, jmp->nop @@ -1953,6 +1954,65 @@ static void test_tailcall_bpf2bpf_fexit_links(void) tailcall_bpf2bpf2__destroy(skel_tc); } =20 +/* + * Verify that a tail call from a BPF subprog works when the subprog is + * traced by fentry.multi. + */ +static void test_tailcall_bpf2bpf_fentry_multi(void) +{ + struct tracing_multi_bpf *skel_tracing =3D NULL; + LIBBPF_OPTS(bpf_tracing_multi_opts, opts); + const char *funcs[] =3D { "subprog_tail" }; + struct tailcall_bpf2bpf2 *skel_tc; + struct bpf_link *link =3D NULL; + int fds[ARRAY_SIZE(funcs)]; + char buff[128] =3D {}; + int err, key =3D 0; + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in =3D buff, + .data_size_in =3D sizeof(buff), + .repeat =3D 1, + ); + +#ifndef __x86_64__ + test__skip(); + return; +#endif + + skel_tc =3D tailcall_bpf2bpf2__open_and_load(); + if (!ASSERT_OK_PTR(skel_tc, "tailcall_bpf2bpf2__open_and_load")) + return; + + skel_tracing =3D tracing_multi_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel_tracing, "tracing_multi_bpf__open_and_load")) + goto out; + + fds[0] =3D bpf_program__fd(skel_tc->progs.classifier_0); + err =3D bpf_map_update_elem(bpf_map__fd(skel_tc->maps.jmp_table), &key, &= fds[0], BPF_ANY); + if (!ASSERT_OK(err, "bpf_map_update_elem")) + goto out; + + opts.fds =3D fds; + opts.funcs =3D funcs; + opts.cnt =3D ARRAY_SIZE(fds); + link =3D bpf_program__attach_tracing_multi(skel_tracing->progs.test_fentr= y, NULL, &opts); + if (!ASSERT_OK_PTR(link, "bpf_program__attach_tracing_multi")) + goto out; + + err =3D bpf_prog_test_run_opts(bpf_program__fd(skel_tc->progs.entry), &to= pts); + if (!ASSERT_OK(err, "bpf_prog_test_run_opts")) + goto out; + + ASSERT_EQ(topts.retval, 1, "retval"); + ASSERT_EQ(skel_tc->bss->count, 33, "count"); + ASSERT_EQ(skel_tracing->bss->test_result_fentry, 33, "fentry.multi count"= ); + +out: + bpf_link__destroy(link); + tracing_multi_bpf__destroy(skel_tracing); + tailcall_bpf2bpf2__destroy(skel_tc); +} + void test_tailcalls(void) { if (test__start_subtest("tailcall_1")) @@ -2022,4 +2082,6 @@ void test_tailcalls(void) test_tailcall_callback(); if (test__start_subtest("tailcall_bpf2bpf_fexit_links")) test_tailcall_bpf2bpf_fexit_links(); + if (test__start_subtest("tailcall_bpf2bpf_fentry_multi")) + test_tailcall_bpf2bpf_fentry_multi(); } --=20 2.55.0