From nobody Thu Apr 2 14:10:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 65C183164B7; Tue, 10 Feb 2026 13:37:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730628; cv=none; b=MwDnbkWMHG63w5GZqHxPEXopz6CpRHbGpVVFyWSGs1LIkH+OOC883PDuj90o+8tFdi/B+zOWQB9H4AR3F94giGFb4xNDMCQpPO5Nv/kEjByHqfaFkvAabgKFw5rypWZNLzl+JOYSuOVFyMctHj7pebD6k6v2J+mLvwmSjAKVmhI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730628; c=relaxed/simple; bh=S29y+uuqZrMgOo1hWvanWYe+TzfNJHT+V3QfZUY1MX0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PLVVuFBySd6V2+tZ5krcOJJX4Itt4bDCLuG5Xigm3UvrnEOyioskWfZnYAFADmOn0qgPlpetvJHHNbVTN1iWCricamrc6kRlKdz34wxR7tDo6ZDTq8Q5ibDvs+gBBXQ9yQfC3oWfKU2clDjlqt3yV+wmqT/eoqV5g1ah4pV2cPw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j04/8tW3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j04/8tW3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21916C116C6; Tue, 10 Feb 2026 13:37:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770730627; bh=S29y+uuqZrMgOo1hWvanWYe+TzfNJHT+V3QfZUY1MX0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j04/8tW3y/fVB+KQI5g/8sX9U5Xk4P510ScKTYR3aQfj8NM2O5EuP7AwIEdNdOgK1 AuKv7e9mAfp/wuFTo8uBk/mia4TJCtM8oMNWvcIOkY/f0hc12kSPf9/URuLsXFbe8U 5zj2msq9FzfArus6MFEqjVjU2icWelkU0ZuZdubQf22bubveho7dMnvBK1XKK9xf6V Biy9Mvi4Fj+s4bPrwmPRMPWLWrowyqJ2nN4z2hb1QAUIsF9DQC/DIcYxNGMm1zs7RA VrTjIubUH4aVEsGiv/Si4MN80TG+fvZHmpkGwFBLUnOz3rgRVOODkTIetGumdB33Hr o2JFFdVNKAOkg== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv2 bpf-next 1/5] libbpf: Add uprobe syscall feature detection Date: Tue, 10 Feb 2026 14:36:45 +0100 Message-ID: <20260210133649.524292-2-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260210133649.524292-1-jolsa@kernel.org> References: <20260210133649.524292-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding uprobe syscall feature detection that will be used in following changes. Signed-off-by: Jiri Olsa --- tools/lib/bpf/features.c | 24 ++++++++++++++++++++++++ tools/lib/bpf/libbpf_internal.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index b842b83e2480..04a225c7f2c0 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -506,6 +506,27 @@ static int probe_kern_arg_ctx_tag(int token_fd) return probe_fd(prog_fd); } =20 +#ifdef __x86_64__ + +#ifndef __NR_uprobe +#define __NR_uprobe 336 +#endif + +static int probe_uprobe_syscall(int token_fd) +{ + /* + * If kernel supports uprobe() syscall, it will return -ENXIO when called + * from the outside of a kernel-generated uprobe trampoline. + */ + return syscall(__NR_uprobe) < 0 && errno =3D=3D ENXIO; +} +#else +static int probe_uprobe_syscall(int token_fd) +{ + return 0; +} +#endif + typedef int (*feature_probe_fn)(int /* token_fd */); =20 static struct kern_feature_cache feature_cache; @@ -581,6 +602,9 @@ static struct kern_feature_desc { [FEAT_BTF_QMARK_DATASEC] =3D { "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, }, + [FEAT_UPROBE_SYSCALL] =3D { + "Kernel supports uprobe syscall", probe_uprobe_syscall, + }, }; =20 bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id= feat_id) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index fc59b21b51b5..69aa61c038a9 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -392,6 +392,8 @@ enum kern_feature_id { FEAT_ARG_CTX_TAG, /* Kernel supports '?' at the front of datasec names */ FEAT_BTF_QMARK_DATASEC, + /* Kernel supports uprobe syscall */ + FEAT_UPROBE_SYSCALL, __FEAT_CNT, }; =20 --=20 2.53.0 From nobody Thu Apr 2 14:10:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A923619EEC2; Tue, 10 Feb 2026 13:37:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730637; cv=none; b=pGQFSW5uEk2XyyRdOW+ycby2Y7vR1ek743H9eRLZ0zES9QiFyFFn7wLRm+JzP47g9mINCVvH6l2SbLQXKr6fqHh1GdURmve+W5Movp3Uy4u2ZnmmeUa+EKiahrJTqJ4EjflNHjzT3ZYt6+ecgUPW0j+8OMNRlusNwM9vFXfDDAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730637; c=relaxed/simple; bh=/e5FLaITB6aTJI/Ft1Jn5QjDQwpwtZrhk8oW4a8Zxes=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qhk+/iLVsDAalHHUZ00rBGUjkeq767umCOIFuQFiOMhBCAXqOxpLzGyiD+8EI8rWeKmtEFYfrgVIWEJHgP/mZ4RmdKyYq1EH4ZcfYQDBotLrV3wRFeoeJ4Ns34S9YOuNCL7jte5FJsemIdUim/NQixqPQtIK5BFDerwlYCdet38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cCAuXSYe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cCAuXSYe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73FEAC19423; Tue, 10 Feb 2026 13:37:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770730637; bh=/e5FLaITB6aTJI/Ft1Jn5QjDQwpwtZrhk8oW4a8Zxes=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cCAuXSYelOTYd3HF6We1IHZdPb5TxjroPvpNdZ0VFtu92yYDVF9Vvpt4I4KxPUIC8 ZIVrmVXIjNXqvM3nuqAfSHHcNN3x43Ct4cWB5nb9u+3ZsLw4vKPXhPYH5MftV3vZnl PMClYGnhBXlnw4LjNATKWXuiu8FAGJYSV+jhwRmuUrnRQyWrkfJ5+iy6bdVNk0OclH JJiwIUMFBFK4BL3hg3reqpdASO3Cc33ZxFRWEQAkbEk0Kpnh35B8J8nZ5wdJGH5Ifz 8eGgXyPfbv10xIj6T1JcSFNQb7Qz5g3x1blwq3l/i0iKuYOc0gIP/moWqHhyA0Ku8T CzW/kQ6z2aJTQ== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv2 bpf-next 2/5] libbpf: Add support to detect nop,nop5 instructions combo for usdt probe Date: Tue, 10 Feb 2026 14:36:46 +0100 Message-ID: <20260210133649.524292-3-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260210133649.524292-1-jolsa@kernel.org> References: <20260210133649.524292-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding support to detect nop,nop5 instructions combo for usdt probe by checking on probe's following nop5 instruction. When the nop,nop5 combo is detected together with uprobe syscall, we can place the probe on top of nop5 and get it optimized. [1] https://github.com/libbpf/usdt Signed-off-by: Jiri Olsa --- tools/lib/bpf/usdt.c | 55 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c index d1524f6f54ae..4e5f70bb4c31 100644 --- a/tools/lib/bpf/usdt.c +++ b/tools/lib/bpf/usdt.c @@ -262,6 +262,7 @@ struct usdt_manager { bool has_bpf_cookie; bool has_sema_refcnt; bool has_uprobe_multi; + bool has_uprobe_syscall; }; =20 struct usdt_manager *usdt_manager_new(struct bpf_object *obj) @@ -301,6 +302,13 @@ struct usdt_manager *usdt_manager_new(struct bpf_objec= t *obj) * usdt probes. */ man->has_uprobe_multi =3D kernel_supports(obj, FEAT_UPROBE_MULTI_LINK); + + /* + * Detect kernel support for uprobe() syscall, it's presence means we can + * take advantage of faster nop5 uprobe handling. + * Added in: 56101b69c919 ("uprobes/x86: Add uprobe syscall to speed up u= probe") + */ + man->has_uprobe_syscall =3D kernel_supports(obj, FEAT_UPROBE_SYSCALL); return man; } =20 @@ -585,13 +593,42 @@ static int parse_usdt_note(GElf_Nhdr *nhdr, const cha= r *data, size_t name_off, =20 static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note = *note, __u64 usdt_cookie); =20 -static int collect_usdt_targets(struct usdt_manager *man, Elf *elf, const = char *path, pid_t pid, - const char *usdt_provider, const char *usdt_name, __u64 usdt_cookie, - struct usdt_target **out_targets, size_t *out_target_cnt) +#if defined(__x86_64__) +static bool has_nop_combo(int fd, long off) +{ + static unsigned char nop_combo[6] =3D { + 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nop,nop5 */ + }; + unsigned char buf[6] =3D {}; + + /* + * We are using file descriptor that backs Elf object, + * let's dup it to be on the safe side. + */ + fd =3D dup(fd); + if (fd < 0) + return false; + if (lseek(fd, off, SEEK_SET) =3D=3D off) + read(fd, buf, 6); + close(fd); + return memcmp(buf, nop_combo, 6) =3D=3D 0; +} +#else +static bool has_nop_combo(int fd, long off) +{ + return false; +} +#endif + +static int collect_usdt_targets(struct usdt_manager *man, struct elf_fd *e= lf_fd, const char *path, + pid_t pid, const char *usdt_provider, const char *usdt_name, + __u64 usdt_cookie, struct usdt_target **out_targets, + size_t *out_target_cnt) { size_t off, name_off, desc_off, seg_cnt =3D 0, vma_seg_cnt =3D 0, target_= cnt =3D 0; struct elf_seg *segs =3D NULL, *vma_segs =3D NULL; struct usdt_target *targets =3D NULL, *target; + Elf *elf =3D elf_fd->elf; long base_addr =3D 0; Elf_Scn *notes_scn, *base_scn; GElf_Shdr base_shdr, notes_shdr; @@ -784,6 +821,16 @@ static int collect_usdt_targets(struct usdt_manager *m= an, Elf *elf, const char * target =3D &targets[target_cnt]; memset(target, 0, sizeof(*target)); =20 + /* + * We have uprobe syscall and usdt with nop,nop5 instructions combo, + * so we can place the uprobe directly on nop5 (+1) and get this probe + * optimized. + */ + if (man->has_uprobe_syscall && has_nop_combo(elf_fd->fd, usdt_rel_ip)) { + usdt_abs_ip++; + usdt_rel_ip++; + } + target->abs_ip =3D usdt_abs_ip; target->rel_ip =3D usdt_rel_ip; target->sema_off =3D usdt_sema_off; @@ -998,7 +1045,7 @@ struct bpf_link *usdt_manager_attach_usdt(struct usdt_= manager *man, const struct /* discover USDT in given binary, optionally limiting * activations to a given PID, if pid > 0 */ - err =3D collect_usdt_targets(man, elf_fd.elf, path, pid, usdt_provider, u= sdt_name, + err =3D collect_usdt_targets(man, &elf_fd, path, pid, usdt_provider, usdt= _name, usdt_cookie, &targets, &target_cnt); if (err <=3D 0) { err =3D (err =3D=3D 0) ? -ENOENT : err; --=20 2.53.0 From nobody Thu Apr 2 14:10:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 297DE1DC198; Tue, 10 Feb 2026 13:37:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730647; cv=none; b=YJ4WcQ9ULLtMmE2VzLUGkRI1y880eAR2OPCNClDpoZu84+8zY3HSxjbIIIInz0JGSu19ujW6MnpO28DVynLPv+y7WNiks8hbY9ttKoy62lPqAnrCvVo1e57XwDE+0NBvKpk/Ki0FeebRlk7/PaHyDULXlZiuPhqjquEW1upc1XQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730647; c=relaxed/simple; bh=3n+AbcvciHPQzsJ1Ve5hHZjrNk7frJapa91/WlvFxa4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DmdDBHtJDq19KPS5CCYx9RBvRIihk2apA8y4V8QymXcJWB/zg7Oc/h6Xw5oLkaBl5zUF2p5P9wBxiI2adQBHYfuolRrLek5yin17woL+9bvFo/5U/cqm9y8ceAS5+yu9b5xoQNGPzll0yqE7y9ezy/DnIZsra4kCwzwE/fYn5B0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H3FVRcqI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H3FVRcqI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6DF7C116C6; Tue, 10 Feb 2026 13:37:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770730646; bh=3n+AbcvciHPQzsJ1Ve5hHZjrNk7frJapa91/WlvFxa4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H3FVRcqIj97228QakmaZNDZiaCsXs7j3fjfHudfOMRU0BfuyFGhovp0gKnKpd0Jpy TPMquBR8nOuPzEYkogyQuHrYWCnIBKdVchws8zRgc5q8rnDwSKctCNZAIviNKj7l6k uesO7z3gCqp9pAGjikfGolFDKrK9N4ug65VvQCKZYw+gDLOJ6TGdUFfPoo6bt7w/Rl OkWLy4XJNfObss/MmDalxGUyl3iK2GVAc1X1wfSE3x/5AP6PwQucsPf0jGlXrjLFXq comwfq5I4/wGB3OTaYhVKy3O2cdwFhghQ+Yf1vyU4nbaL2y099ljXTgFLQqydZPV7R DuFmOfoO5Pqog== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv2 bpf-next 3/5] selftests/bpf: Emit nop,no5 instructions combo for x86_64 arch Date: Tue, 10 Feb 2026 14:36:47 +0100 Message-ID: <20260210133649.524292-4-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260210133649.524292-1-jolsa@kernel.org> References: <20260210133649.524292-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Syncing latest usdt.h change [1]. Now that we have no5 optimization support in kernel, let's emit nop,nop5 for usdt probe. We leave it up to the library to use desirable nop instruction. [1] https://github.com/libbpf/usdt/commit/c9865d158984fb2b73e3cbbdcdfb4f583= ad36a73 Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/usdt.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/testing/selftests/bpf/usdt.h b/tools/testing/selftests/b= pf/usdt.h index 549d1f774810..c71e21df38b3 100644 --- a/tools/testing/selftests/bpf/usdt.h +++ b/tools/testing/selftests/bpf/usdt.h @@ -312,6 +312,8 @@ struct usdt_sema { volatile unsigned short active; }; #ifndef USDT_NOP #if defined(__ia64__) || defined(__s390__) || defined(__s390x__) #define USDT_NOP nop 0 +#elif defined(__x86_64__) +#define USDT_NOP .byte 0x90, 0x0f, 0x1f, 0x44, 0x00,= 0x0 /* nop, nop5 */ #else #define USDT_NOP nop #endif --=20 2.53.0 From nobody Thu Apr 2 14:10:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 215411DC198; Tue, 10 Feb 2026 13:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730656; cv=none; b=TVzpsHfWLNoV/pPqKU/4QPSp3L6/+aOezfLf0JqjKsLH5JJGpIF69+BVQqOqw2CVcxgjFmG/NxJS5uQcZPMVxjzYxk5HVtjwW/U9nvuaC7hJvbA+Ec4yydo0QkrkoVGPfJmcN5zmOnlGfYcvBINbPYdYAq4/Mg/MS6LE6wnPbsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730656; c=relaxed/simple; bh=OJL/jCjsZxIrowaR7pNnZ1ZmpK5JnffV4H7ABU7UqV4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pleVq/We+FqJ0xYdvvQpyAFwOVKnImEtVT3P8ytfYtA5ivlKKSDRYHfs2JAW4GEYiTaNhhFwuLX0GRqr/712vgZuJWYYBqf91jy1I7jNFIDtt1UZhMxMYo5HIIO0ckyh16EPIbYrUnac7WjMMab6ASzPxlcmAGvE/pHOrs25d88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E/ztk7Gc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E/ztk7Gc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FF41C116C6; Tue, 10 Feb 2026 13:37:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770730656; bh=OJL/jCjsZxIrowaR7pNnZ1ZmpK5JnffV4H7ABU7UqV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E/ztk7Gc3q2G+HzVPEEd9+pKTOFva4PzJxLRuOrG6F6ml1YtWECDXkAgDT5wblC1C APB8eygni/f85tb4mAxqr6hc41FkurFrfdPbl1u//GnBeTpDFjGkaMHz/XjOfn+RrN /vMcSUujzE/uNn+G4INuoHp7dppzH+zWIYu7prN1RwLtgrXlyCGIETN1CNwCUyCNpk aEiIHsVNNJ6N2390bpKUvUeWkEQqBGtEVNf55Ax2cfSIRljSXSAQ/By3oRHESbjkV9 A8CA/1acNmkUiWJbRQBlSiwFfhnNM8lJsK4SVqXCQ2X1/ZDQkp5XtTfmB45eSPsJIM TMVX7pKeP2T8Q== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv2 bpf-next 4/5] selftests/bpf: Add test for checking correct nop of optimized usdt Date: Tue, 10 Feb 2026 14:36:48 +0100 Message-ID: <20260210133649.524292-5-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260210133649.524292-1-jolsa@kernel.org> References: <20260210133649.524292-1-jolsa@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Adding test that attaches bpf program on usdt probe in 2 scenarios; - attach program on top of usdt_1, which is single nop instruction, so the probe stays on nop instruction and is not optimized. - attach program on top of usdt_2 which is probe defined on top of nop,nop5 combo, so the probe is placed on top of nop5 and is optimized. Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/.gitignore | 2 + tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/prog_tests/usdt.c | 85 +++++++++++++++++++ tools/testing/selftests/bpf/progs/test_usdt.c | 9 ++ tools/testing/selftests/bpf/usdt_1.c | 18 ++++ tools/testing/selftests/bpf/usdt_2.c | 16 ++++ 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/usdt_1.c create mode 100644 tools/testing/selftests/bpf/usdt_2.c diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftes= ts/bpf/.gitignore index a3ea98211ea6..bfdc5518ecc8 100644 --- a/tools/testing/selftests/bpf/.gitignore +++ b/tools/testing/selftests/bpf/.gitignore @@ -47,3 +47,5 @@ verification_cert.h *.BTF *.BTF_ids *.BTF.base +usdt_1 +usdt_2 diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index c6bf4dfb1495..306949162a5b 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -749,7 +749,8 @@ TRUNNER_EXTRA_SOURCES :=3D test_progs.c \ $(VERIFY_SIG_HDR) \ flow_dissector_load.h \ ip_check_defrag_frags.h \ - bpftool_helpers.c + bpftool_helpers.c \ + usdt_1.c usdt_2.c TRUNNER_LIB_SOURCES :=3D find_bit.c TRUNNER_EXTRA_FILES :=3D $(OUTPUT)/urandom_read \ $(OUTPUT)/liburandom_read.so \ diff --git a/tools/testing/selftests/bpf/prog_tests/usdt.c b/tools/testing/= selftests/bpf/prog_tests/usdt.c index f4be5269fa90..0ef74894c1b8 100644 --- a/tools/testing/selftests/bpf/prog_tests/usdt.c +++ b/tools/testing/selftests/bpf/prog_tests/usdt.c @@ -247,6 +247,89 @@ static void subtest_basic_usdt(bool optimized) #undef TRIGGER } =20 +#ifdef __x86_64 +extern void usdt_1(void); +extern void usdt_2(void); + +/* nop, nop5 */ +static unsigned char nop1_nop5_combo[6] =3D { 0x90, 0x0f, 0x1f, 0x44, 0x00= , 0x00 }; +static unsigned char nop1[6] =3D { 0x90 }; + +static void *find_instr(void *fn, unsigned char *instr, size_t cnt) +{ + int i; + + for (i =3D 0; i < 10; i++) { + if (!memcmp(instr, fn + i, cnt)) + return fn + i; + } + return NULL; +} + +static void subtest_optimized_attach(void) +{ + struct test_usdt *skel; + __u8 *addr_1, *addr_2; + + /* usdt_1 probe has single nop instruction */ + addr_1 =3D find_instr(usdt_1, nop1_nop5_combo, 6); + if (!ASSERT_NULL(addr_1, "usdt_1_find_nop1_nop5_combo")) + return; + + addr_1 =3D find_instr(usdt_1, nop1, 1); + if (!ASSERT_OK_PTR(addr_1, "usdt_1_find_nop1")) + return; + + /* usdt_2 probe has nop,nop5 instructions combo */ + addr_2 =3D find_instr(usdt_2, nop1_nop5_combo, 6); + if (!ASSERT_OK_PTR(addr_2, "usdt_2_find_nop1_nop5_combo")) + return; + + skel =3D test_usdt__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_usdt__open_and_load")) + return; + + /* + * Attach program on top of usdt_1 which is single nop probe, + * so the probe won't get optimized. + */ + skel->links.usdt_executed =3D bpf_program__attach_usdt(skel->progs.usdt_e= xecuted, + 0 /*self*/, "/proc/self/exe", + "optimized_attach", "usdt_1", NULL); + if (!ASSERT_OK_PTR(skel->links.usdt_executed, "bpf_program__attach_usdt")) + goto cleanup; + + usdt_1(); + usdt_1(); + + /* nop is on addr_1 address */ + ASSERT_EQ(*addr_1, 0xcc, "int3"); + ASSERT_EQ(skel->bss->executed, 2, "executed"); + + bpf_link__destroy(skel->links.usdt_executed); + + /* + * Attach program on top of usdt_2 which is probe defined on top + * of nop1,nop5 combo, so the probe gets optimized on top of nop5. + */ + skel->links.usdt_executed =3D bpf_program__attach_usdt(skel->progs.usdt_e= xecuted, + 0 /*self*/, "/proc/self/exe", + "optimized_attach", "usdt_2", NULL); + if (!ASSERT_OK_PTR(skel->links.usdt_executed, "bpf_program__attach_usdt")) + goto cleanup; + + usdt_2(); + usdt_2(); + + /* nop5 is on addr_2 + 1 address */ + ASSERT_EQ(*(addr_2 + 1), 0xe8, "call"); + ASSERT_EQ(skel->bss->executed, 4, "executed"); + +cleanup: + test_usdt__destroy(skel); +} +#endif + unsigned short test_usdt_100_semaphore SEC(".probes"); unsigned short test_usdt_300_semaphore SEC(".probes"); unsigned short test_usdt_400_semaphore SEC(".probes"); @@ -516,6 +599,8 @@ void test_usdt(void) #ifdef __x86_64__ if (test__start_subtest("basic_optimized")) subtest_basic_usdt(true); + if (test__start_subtest("optimized_attach")) + subtest_optimized_attach(); #endif if (test__start_subtest("multispec")) subtest_multispec_usdt(); diff --git a/tools/testing/selftests/bpf/progs/test_usdt.c b/tools/testing/= selftests/bpf/progs/test_usdt.c index a78c87537b07..6911868cdf67 100644 --- a/tools/testing/selftests/bpf/progs/test_usdt.c +++ b/tools/testing/selftests/bpf/progs/test_usdt.c @@ -138,4 +138,13 @@ int usdt_sib(struct pt_regs *ctx) return 0; } =20 +int executed; + +SEC("usdt") +int usdt_executed(struct pt_regs *ctx) +{ + executed++; + return 0; +} + char _license[] SEC("license") =3D "GPL"; diff --git a/tools/testing/selftests/bpf/usdt_1.c b/tools/testing/selftests= /bpf/usdt_1.c new file mode 100644 index 000000000000..4f06e8bcf58b --- /dev/null +++ b/tools/testing/selftests/bpf/usdt_1.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#if defined(__x86_64__) + +/* + * Include usdt.h with defined USDT_NOP macro to use single + * nop instruction. + */ +#define USDT_NOP .byte 0x90 +#include "usdt.h" + +__attribute__((aligned(16))) +void usdt_1(void) +{ + USDT(optimized_attach, usdt_1); +} + +#endif diff --git a/tools/testing/selftests/bpf/usdt_2.c b/tools/testing/selftests= /bpf/usdt_2.c new file mode 100644 index 000000000000..789883aaca4c --- /dev/null +++ b/tools/testing/selftests/bpf/usdt_2.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#if defined(__x86_64__) + +/* + * Include usdt.h with default nop,nop5 instructions combo. + */ +#include "usdt.h" + +__attribute__((aligned(16))) +void usdt_2(void) +{ + USDT(optimized_attach, usdt_2); +} + +#endif --=20 2.53.0 From nobody Thu Apr 2 14:10:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 25C471DC198; Tue, 10 Feb 2026 13:37:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730665; cv=none; b=n304bdumv9RS9+n89jSMrxce0hj8imLh9iDTt373GwyB+1Q8xmuwAYq5orMjst1ekVVgh3BflVFizoSEon36rrWLQYK219Yw7sIIRyBOaAc1f6h3mVUQOq+0U3qCqHIUmfngz80GmQ/EZfuNuPAsLgiOiFu3PZjWrFkWxhNmOfw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770730665; c=relaxed/simple; bh=j/ImcZTftZ2nzFNq2YJAfz4LlmzAH/8itUNdqym9u14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RrNiUIq18bgF33Wvz6IPR0NFId4vzXCD3+SN16/7tcVLzyObLC1AL1y4whrlr5VYexWXMA4ic2r3/v3DGkVPDpA9tJ8crVzlugHyMH6aLrNkp/lAy/W+wbc6ICh/A8jPcmczCFpLTJEDOckD+lLWuQMkVaiUi79Qq5FuW0zyGdg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nWZrq5L/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nWZrq5L/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37691C16AAE; Tue, 10 Feb 2026 13:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770730665; bh=j/ImcZTftZ2nzFNq2YJAfz4LlmzAH/8itUNdqym9u14=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nWZrq5L/Yr1+dHrIBUnQ+7+cNDq26K+9cxBp000JitT2QD9Xi0SwkHg4Q35eVgXxH wg6DrmZwrtGcR4vli3lKZr/U/CCTSLt7P76terJjY8QpJROYLSm2w7VIHZFLfvFA9J +cXgrwMqFwnfwycCTYQ9lDMl1j/fodFZAmEFbRhz4Xj2pJTUU0deoE9WvHa087AZyK V49hXYRe/VO8nq1JgvghZyasvHKGNzKqJKzMLpRzqa9zrru/OHqZAxJvJZtIiInZeO hHiSVbOOyN3cqSh9nAOIbYdIgIjjn9UXxdH8mqMTEDsbpJeEfBCGMgar18xp0arprh XPbO5EkuVTowQ== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv2 bpf-next 5/5] selftests/bpf: Add usdt trigger bench Date: Tue, 10 Feb 2026 14:36:49 +0100 Message-ID: <20260210133649.524292-6-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260210133649.524292-1-jolsa@kernel.org> References: <20260210133649.524292-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Adding usdt trigger bench for usdt: trig-usdt_nop - usdt on top of nop1 instruction trig-usdt_nop_combo - usdt on top of nop1/nop5 combo Adding it to benchs/run_bench_uprobes.sh script. Example run on x86_64 kernel with uprobe syscall: # ./benchs/run_bench_uprobes.sh usermode-count : 152.507 =C2=B1 0.098M/s syscall-count : 14.309 =C2=B1 0.093M/s uprobe-nop : 3.190 =C2=B1 0.012M/s uprobe-push : 3.057 =C2=B1 0.004M/s uprobe-ret : 1.095 =C2=B1 0.009M/s uprobe-nop5 : 7.305 =C2=B1 0.034M/s uretprobe-nop : 2.175 =C2=B1 0.005M/s uretprobe-push : 2.109 =C2=B1 0.003M/s uretprobe-ret : 0.945 =C2=B1 0.002M/s uretprobe-nop5 : 3.530 =C2=B1 0.006M/s usdt_nop : 3.235 =C2=B1 0.008M/s <-- added usdt_nop_combo : 7.511 =C2=B1 0.045M/s <-- added Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/Makefile | 2 + tools/testing/selftests/bpf/bench.c | 4 ++ .../selftests/bpf/benchs/bench_trigger.c | 60 +++++++++++++++++++ .../selftests/bpf/benchs/run_bench_uprobes.sh | 2 +- .../selftests/bpf/progs/trigger_bench.c | 10 +++- 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index 306949162a5b..9b2ca0028322 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -874,6 +874,8 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ $(OUTPUT)/bench_bpf_crypto.o \ $(OUTPUT)/bench_sockmap.o \ $(OUTPUT)/bench_lpm_trie_map.o \ + $(OUTPUT)/usdt_1.o \ + $(OUTPUT)/usdt_2.o \ # $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/= bpf/bench.c index 8368bd3a0665..4dacb87e464e 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -541,6 +541,8 @@ extern const struct bench bench_trig_uprobe_nop5; extern const struct bench bench_trig_uretprobe_nop5; extern const struct bench bench_trig_uprobe_multi_nop5; extern const struct bench bench_trig_uretprobe_multi_nop5; +extern const struct bench bench_trig_usdt_nop; +extern const struct bench bench_trig_usdt_nop_combo; #endif =20 extern const struct bench bench_rb_libbpf; @@ -617,6 +619,8 @@ static const struct bench *benchs[] =3D { &bench_trig_uretprobe_nop5, &bench_trig_uprobe_multi_nop5, &bench_trig_uretprobe_multi_nop5, + &bench_trig_usdt_nop, + &bench_trig_usdt_nop_combo, #endif /* ringbuf/perfbuf benchmarks */ &bench_rb_libbpf, diff --git a/tools/testing/selftests/bpf/benchs/bench_trigger.c b/tools/tes= ting/selftests/bpf/benchs/bench_trigger.c index aeec9edd3851..b4b03fe1f61d 100644 --- a/tools/testing/selftests/bpf/benchs/bench_trigger.c +++ b/tools/testing/selftests/bpf/benchs/bench_trigger.c @@ -405,6 +405,23 @@ static void *uprobe_producer_nop5(void *input) uprobe_target_nop5(); return NULL; } + +void usdt_1(void); +void usdt_2(void); + +static void *uprobe_producer_usdt_nop(void *input) +{ + while (true) + usdt_1(); + return NULL; +} + +static void *uprobe_producer_usdt_nop_combo(void *input) +{ + while (true) + usdt_2(); + return NULL; +} #endif =20 static void usetup(bool use_retprobe, bool use_multi, void *target_addr) @@ -542,6 +559,47 @@ static void uretprobe_multi_nop5_setup(void) { usetup(true, true /* use_multi */, &uprobe_target_nop5); } + +static void usdt_setup(const char *name) +{ + struct bpf_link *link; + int err; + + setup_libbpf(); + + ctx.skel =3D trigger_bench__open(); + if (!ctx.skel) { + fprintf(stderr, "failed to open skeleton\n"); + exit(1); + } + + bpf_program__set_autoload(ctx.skel->progs.bench_trigger_usdt, true); + + err =3D trigger_bench__load(ctx.skel); + if (err) { + fprintf(stderr, "failed to load skeleton\n"); + exit(1); + } + + link =3D bpf_program__attach_usdt(ctx.skel->progs.bench_trigger_usdt, + 0 /*self*/, "/proc/self/exe", + "optimized_attach", name, NULL); + if (libbpf_get_error(link)) { + fprintf(stderr, "failed to attach optimized_attach:%s usdt probe\n", nam= e); + exit(1); + } + ctx.skel->links.bench_trigger_usdt =3D link; +} + +static void usdt_nop_setup(void) +{ + usdt_setup("usdt_1"); +} + +static void usdt_nop_combo_setup(void) +{ + usdt_setup("usdt_2"); +} #endif =20 const struct bench bench_trig_syscall_count =3D { @@ -609,4 +667,6 @@ BENCH_TRIG_USERMODE(uprobe_nop5, nop5, "uprobe-nop5"); BENCH_TRIG_USERMODE(uretprobe_nop5, nop5, "uretprobe-nop5"); BENCH_TRIG_USERMODE(uprobe_multi_nop5, nop5, "uprobe-multi-nop5"); BENCH_TRIG_USERMODE(uretprobe_multi_nop5, nop5, "uretprobe-multi-nop5"); +BENCH_TRIG_USERMODE(usdt_nop, usdt_nop, "usdt_nop"); +BENCH_TRIG_USERMODE(usdt_nop_combo, usdt_nop_combo, "usdt_nop_combo"); #endif diff --git a/tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh b/tool= s/testing/selftests/bpf/benchs/run_bench_uprobes.sh index 03f55405484b..3656676d99d2 100755 --- a/tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh +++ b/tools/testing/selftests/bpf/benchs/run_bench_uprobes.sh @@ -2,7 +2,7 @@ =20 set -eufo pipefail =20 -for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret,nop= 5} +for i in usermode-count syscall-count {uprobe,uretprobe}-{nop,push,ret,nop= 5} usdt_nop usdt_nop_combo do summary=3D$(sudo ./bench -w2 -d5 -a trig-$i | tail -n1 | cut -d'(' -f1 | = cut -d' ' -f3-) printf "%-15s: %s\n" $i "$summary" diff --git a/tools/testing/selftests/bpf/progs/trigger_bench.c b/tools/test= ing/selftests/bpf/progs/trigger_bench.c index 4ea0422d1042..f631706bbdc9 100644 --- a/tools/testing/selftests/bpf/progs/trigger_bench.c +++ b/tools/testing/selftests/bpf/progs/trigger_bench.c @@ -1,10 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2020 Facebook -#include +#include "vmlinux.h" #include #include #include #include "bpf_misc.h" +#include "bpf/usdt.bpf.h" =20 char _license[] SEC("license") =3D "GPL"; =20 @@ -180,3 +181,10 @@ int bench_trigger_rawtp(void *ctx) handle(ctx); return 0; } + +SEC("usdt") +int bench_trigger_usdt(void *ctx) +{ + inc_counter(); + return 0; +} --=20 2.53.0