From nobody Thu Apr 2 14:06:36 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 C80333644B6; Tue, 24 Feb 2026 10:39:37 +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=1771929577; cv=none; b=gMJS3O0oqZKqJgNPJ8ruYEdKps/NPmSujNz1CJKyoR7jFL3E4esZzUUGzsXpTryA7qLhihECWxKtq17gYJUEATrh/O+T1Dgp3nt6n2wbx5jn57OHy8mzkSeySqeGb8klYyXhapX02UQtpXszq9QzDTqqhjhYLbY33ICnylpTx6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771929577; c=relaxed/simple; bh=Gb3Elkny7jCdBeuIV/xdQ1NCvtvANwyTKBH0kHt03sI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JsFVstfZwj7YeDmOo06bdx+gOCEPGcVTytQ+KOnYsIrqK86LmuMv1ejpZhVZIruQmxvS0c3HccGr2rB1FVxU1AGw0Bf0ukaKDsaJKnI7t2pYwkGscqjlJfYdqNOjYT7lMxfzY1Dg3FBClepB0zdpnqUqni3AWz9VsXd2bVhGps0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kcY8HmDw; 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="kcY8HmDw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8B3EC116D0; Tue, 24 Feb 2026 10:39:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771929577; bh=Gb3Elkny7jCdBeuIV/xdQ1NCvtvANwyTKBH0kHt03sI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kcY8HmDwsFmQJudXCVwmHvs+y6rrDIS0NGHFOd6wRrMq3iV0YoS67OJngECPmtsbE 2pCjoYezTO0JY71uuN8uoUvnAF6iW22Gdz/maWRV6RwuPjy/N/09XhigYjI2h6WyzT Knjis65hAsmrjIrZRmImESP7Y+OYnZOlUjl3V5mLdjZ7OTkDWXD0YSThNu077qQ7m1 c8BwevSGGjz7z7KhgglATI8Hmuwmd2Uj6ricCLBmD9481QLPp24au9iek4TsHwDmVT vCoSg8JOFHB0VmkRTUB06VWzUDSWeqyhFAoA8Qubp97ot8Jh1L+eTeKBmtwsArRzX1 AlD4XeDvx15BQ== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv5 bpf-next 1/5] libbpf: Add uprobe syscall feature detection Date: Tue, 24 Feb 2026 11:39:11 +0100 Message-ID: <20260224103915.1369690-2-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260224103915.1369690-1-jolsa@kernel.org> References: <20260224103915.1369690-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 2fa434f09cce..adcad221c601 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -568,6 +568,27 @@ static int probe_ldimm64_full_range_off(int token_fd) return 1; } =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; @@ -646,6 +667,9 @@ static struct kern_feature_desc { [FEAT_LDIMM64_FULL_RANGE_OFF] =3D { "full range LDIMM64 support", probe_ldimm64_full_range_off, }, + [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 974147e8a8aa..4bcb6ca69bb1 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -394,6 +394,8 @@ enum kern_feature_id { FEAT_BTF_QMARK_DATASEC, /* Kernel supports LDIMM64 imm offsets past 512 MiB. */ FEAT_LDIMM64_FULL_RANGE_OFF, + /* Kernel supports uprobe syscall */ + FEAT_UPROBE_SYSCALL, __FEAT_CNT, }; =20 --=20 2.53.0 From nobody Thu Apr 2 14:06:36 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 24DC536B042; Tue, 24 Feb 2026 10:39:47 +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=1771929587; cv=none; b=LNo6N5cMMUANR7MMfh2l+XJslA9Km4HGnmM4dSYZxKTqoAFEuT5kYA+4wd932jwsd5LEKS4EIo19VXyLrGZl7bmHybqsvqRfSBxiEXkBG9j6GBwxVRW+uRFSF1Cy8lhZRDV9g6OVtDflPIXqCg8BjXAx42b8GDzUdCwCHxUcWfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771929587; c=relaxed/simple; bh=YBAss1AXFulyJXE+1UqkNKMBsJd/Obe6LLBaU3cldBA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kCUGeVlvSeJFxBkdaREvT7jPUtidHuIhnQYFaQF5eAO/94ilTJGp+LURmuh0P7mreGHEUuahBKbTp8oa2otd+Xc7v1ileaJVurMsGGOXy8asa6HCipSZC+qhfAO26tT+6xauChGU69/Qk08t0cmqDiAoaqYS9j1JyBuLhhXKkSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oDsw9/Ag; 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="oDsw9/Ag" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24D3AC116D0; Tue, 24 Feb 2026 10:39:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771929587; bh=YBAss1AXFulyJXE+1UqkNKMBsJd/Obe6LLBaU3cldBA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oDsw9/Ag1gFzapGGfvKzgvByP1BccZzYLMkwXR/VBs6n0b0fJqxe3MmYLKWn/PIOX 9CnV5GOGtMoS4glPc5NQnzJT0OgRRIwmMARLpgAdReAmg+mgJ6eirfIPC0OXJfbaO/ pFnzpNchEbwu5PXo8o5aLiBZPLr+7N5ENfmqnj6/boW7tCbLNVTcW9xx0VulpHIstd 4ZZzMx+MrEA7OEuWeNYMsrci9MDLufCw/bIDQujw9n/qBNL6l1HOSCoEU1EJBVkYCb 8g+jSQN/ktwu7C0WVD108LUKCKeh6rgA25yRdY/S+J0bPERlXZeTeBz9h14MN3Ooqc AYlEPc4Zzbohg== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv5 bpf-next 2/5] libbpf: Add support to detect nop,nop5 instructions combo for usdt probe Date: Tue, 24 Feb 2026 11:39:12 +0100 Message-ID: <20260224103915.1369690-3-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260224103915.1369690-1-jolsa@kernel.org> References: <20260224103915.1369690-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. Signed-off-by: Jiri Olsa --- tools/lib/bpf/usdt.c | 47 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c index d1524f6f54ae..e3710933fd52 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,34 @@ 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) +{ + unsigned char nop_combo[6] =3D { + 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nop,nop5 */ + }; + unsigned char buf[6]; + + if (pread(fd, buf, 6, off) !=3D 6) + return false; + 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 +813,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 +1037,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:06:36 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 8271C37882B; Tue, 24 Feb 2026 10:39:56 +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=1771929596; cv=none; b=Wwekm8NWNiGaohWpe61fogWs7g17TrO4YY1ELbLMXTZ/etma1meAipTyF1AeOZIg9GL/fCC7fQ7pI6VgOQDyc5RFw2G6hQr5OUfl0MDkBZL+Rmln5VPvflYTZwnwSnNvxAvyzi+karTIxglZymPHVHgdQSxSq3pnFP9+uh1X2Rc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771929596; c=relaxed/simple; bh=DQxx4sQpSRlmKFD8OqooGbohxZ8Wbb2yF5Xw1ruqRyM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MMkysObCkxelkN2wG/t03aSmy+26H09HH0uti2B9klC7D2HqNfh+xkNw/iei4EnbiNcBD9rAl9ELWLJUQC6NdN6xQqeYMB8vSokgY3vjtIoFE5jR1N98g6uuVZRY9qngL7Q0+RIKUVfI8bcjPAuAghfiwAM4eAhHFj8igX+fIKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Yr/DDsM0; 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="Yr/DDsM0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70C8AC116D0; Tue, 24 Feb 2026 10:39:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771929596; bh=DQxx4sQpSRlmKFD8OqooGbohxZ8Wbb2yF5Xw1ruqRyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yr/DDsM0i418Upb3pxpIfPelvII4Jxm9HoJwsghNv/ewikzEZoBRsLrGSK6AyYQPG +NovTIJgTVwuOZ7keeANqUWpA/x76K1NozwTw9uh6ZM2T7/8YpxDGJUjae9ALUCc0A oA1kWrl55G9IFX54h3fvZ+3dGg/7f+3mO6RDU9LaSwSWrlfHAFPzCbO9Np+QibyGyH eAqPg+zcTauOfxp6i1gjQSCUizsZt1HvSP4obldMq6FS+wrowPBPaAAsf/MUAUsqon XVeXoqrmJfD683x6Dbd5mpHQC5HzaCVbmYV4Kf0+B4bhZCg4wzOZH5vaV/1WyLyv1m rHqwlwoKV5EOA== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv5 bpf-next 3/5] selftests/bpf: Emit nop,nop5 instructions combo for x86_64 arch Date: Tue, 24 Feb 2026 11:39:13 +0100 Message-ID: <20260224103915.1369690-4-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260224103915.1369690-1-jolsa@kernel.org> References: <20260224103915.1369690-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 nop5 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:06:36 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 704AB37882B; Tue, 24 Feb 2026 10:40:06 +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=1771929606; cv=none; b=Ps9x2/0eYA7anSssyMGGs1ZpX9XqEEMxRbeUcYKsT08YVZhOOPcPcCsBOGoPSSt/fe+3Xbuns2FRU7tbtfQ0a/E5wfYp1xJVDV2w7B1o6KY6cGMM7CYsKSFZHm/h2ID//qDD6+rhSL2pnFmC7hwexBuezreEqzZl92WRCQYSbYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771929606; c=relaxed/simple; bh=aMVzuAIk1dN5DYG5umlxXnQpvAHaohfIb8cpPlQAlvo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WlDsqBUVPSduAFrNGTuyHn7CSBmXWApSN6yg+q5a/UVjkHmKxYusnatVS3UsTqdtHeEZfwEWcl4V1UAeTthpSvbX7fAkD1PeMc84aa2uUSf444x5D1miEEpFB/CNx6mAKSnObWeyOLvr+YVyE9UsC0kIWo0ZJcJU7dpFlhc6YBw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fT8o23Sy; 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="fT8o23Sy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 169A1C2BC86; Tue, 24 Feb 2026 10:40:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771929606; bh=aMVzuAIk1dN5DYG5umlxXnQpvAHaohfIb8cpPlQAlvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fT8o23SyDMX9aBYs69qqPtQXD/A+PeJUGPveiZmXneF8IiMMA5BlLWO4FDwPhomZU 2vfeWEhUKsyQl9q2Y5EdjqLLsOAKV0/bmoM5zxCAyiK7Lj3XWi6dNcgnD7EBfGMoA6 pOpfkK80icMZodeb9NrHLPLNWQdHzP/vlxGEaaMr2BiqeHq+ffd94vByAQn3Uw/zaS zYgLlDxjD4oaKydCQBxieUxk4a2i4L9pBvLZSnJMuNVRaIFAITo9A7yrwfVBJA/8ac eXa2UdwGVjD+2ha2IQH2yaR2a3fYq8Oer99uHBbFPe2U4EMLXzYAjred6/cbqIjUMR PdHmDWxFYhYJQ== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv5 bpf-next 4/5] selftests/bpf: Add test for checking correct nop of optimized usdt Date: Tue, 24 Feb 2026 11:39:14 +0100 Message-ID: <20260224103915.1369690-5-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260224103915.1369690-1-jolsa@kernel.org> References: <20260224103915.1369690-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 | 92 +++++++++++++++++++ tools/testing/selftests/bpf/progs/test_usdt.c | 12 +++ tools/testing/selftests/bpf/usdt_1.c | 18 ++++ tools/testing/selftests/bpf/usdt_2.c | 16 ++++ 6 files changed, 142 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 6776158f1f3e..23857d17fc5b 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..69759b27794d 100644 --- a/tools/testing/selftests/bpf/prog_tests/usdt.c +++ b/tools/testing/selftests/bpf/prog_tests/usdt.c @@ -247,6 +247,96 @@ static void subtest_basic_usdt(bool optimized) #undef TRIGGER } =20 +#ifdef __x86_64__ +extern void usdt_1(void); +extern void usdt_2(void); + +static unsigned char nop1[1] =3D { 0x90 }; +static unsigned char nop1_nop5_combo[6] =3D { 0x90, 0x0f, 0x1f, 0x44, 0x00= , 0x00 }; + +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 USDT 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 USDT 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; + + skel->bss->expected_ip =3D (unsigned long) addr_1; + + /* + * 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(); + + /* int3 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); + + /* we expect the nop5 ip */ + skel->bss->expected_ip =3D (unsigned long) addr_2 + 1; + + /* + * 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(); + + /* nop stays on addr_2 address */ + ASSERT_EQ(*addr_2, 0x90, "nop"); + + /* call 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 +606,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..f00cb52874e0 100644 --- a/tools/testing/selftests/bpf/progs/test_usdt.c +++ b/tools/testing/selftests/bpf/progs/test_usdt.c @@ -138,4 +138,16 @@ int usdt_sib(struct pt_regs *ctx) return 0; } =20 +#ifdef __TARGET_ARCH_x86 +int executed; +unsigned long expected_ip; + +SEC("usdt") +int usdt_executed(struct pt_regs *ctx) +{ + if (expected_ip =3D=3D ctx->ip) + executed++; + return 0; +} +#endif 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:06:36 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 6B8D8377551; Tue, 24 Feb 2026 10:40:15 +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=1771929615; cv=none; b=pUTcEetij8EF//w5p7BH9Df1ARto8Cnjso4lfFioGzXdp9DP2ok+6Ut9qQ1O5NAo3+rh9b38p8MW6YsZjfYzLXw2t8ziy2ELpaVoHtljKAkh4QpUw5tVaD/47m4TcMZn5lOmjyI9DPKSsfuiGisJbnq1etM0R6z0vTthtDECBy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771929615; c=relaxed/simple; bh=F+27PLKRsMR9oBOYaHEH2ngLd8FV0hMP80Zyg/41MHs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PP7//xYjWEVED4EFBhdaCObK44C/rXiK1nVmIVZyn4luLUYsEMsrwAbj3ZDeVNiPHhom5Prd8iy4oBSUhkvea88vHSqPM1CVBBrno0NJn4o8qTJxk7l9TuFxxz9XuHNqBWSxDNGqr7/yRCXMd+cevYG0WdDaQPZ6ow0LcdbI+mU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bOCwEqF5; 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="bOCwEqF5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6322AC116D0; Tue, 24 Feb 2026 10:40:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771929615; bh=F+27PLKRsMR9oBOYaHEH2ngLd8FV0hMP80Zyg/41MHs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bOCwEqF51QTlIU+XhLgvUTQTmM594NK59hVXtqWieqVnu5HEmQpfQLiqFFoxHFied 24ITGEN3iQ/pHZeByxS3Y9TZhNwcOmo860OwcEik/IoS5px/jXFacviWxhBJNc6vn1 uslR9V7FLNAdUPOo2g8g/s8HsEcBRvgUHrBUvxK1Rzs0xHP2svYTb3Ct8CX/RVk6/v zw2Nx4PkWQiGB03Bt/+G57ywGzHKUdpj2zOpxFbV+Ix8BiRYCMlw6KHRIT0Jj43nAI E7MZl0Z0zcrjRI8xsCzqarUnf86yft3GI6czeBf8jnog8Mg6qdUzBeIrINb4XNotlb 7Y0f1aLR2v+9g== From: Jiri Olsa To: Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Yonghong Song , John Fastabend Subject: [PATCHv5 bpf-next 5/5] selftests/bpf: Add usdt trigger bench Date: Tue, 24 Feb 2026 11:39:15 +0100 Message-ID: <20260224103915.1369690-6-jolsa@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260224103915.1369690-1-jolsa@kernel.org> References: <20260224103915.1369690-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-nop5 - 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-nop5 : 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 23857d17fc5b..8196128251d5 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..029b3e21f438 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_nop5; #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_nop5, #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..2ee6a2df2c26 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_nop5(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_nop5_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_nop5, usdt_nop5, "usdt-nop5"); #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..9ec59423b949 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-nop5 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..3225b4aee8ff 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