Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH bpf 1/2] libbpf: Fix usdt attach failure when nop10 crosses page boundary
@ 2026-08-24  9:28 Jiayuan Chen
  2026-08-24  9:28 ` [PATCH bpf 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10 Jiayuan Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Jiayuan Chen @ 2026-08-24  9:28 UTC (permalink / raw)
  To: bpf
  Cc: Jiayuan Chen, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Shuah Khan, Jakub Sitnicki,
	Peter Zijlstra (Intel), Jiawei Zhao, linux-kernel,
	linux-kselftest

The kernel refuses to attach to a nop10 that crosses a page boundary,
since it can't be atomically rewritten:

	/* can_optimize(), arch/x86/kernel/uprobes.c */
	/* We can't do cross page atomic writes yet. */
	return PAGE_SIZE - (vaddr & ~PAGE_MASK) >= OPT_INSN_SIZE;

Whether the nop10 crosses a page is purely up to the binary layout, so
this does happen in practice. libbpf doesn't check for it and blindly
shifts the uprobe onto the nop10, and the attach then fails with
-ENOTSUPP. Just keep the uprobe on the preceding 1-byte nop in that
case, it works everywhere as a regular int3 uprobe.

Fixes: 41a5c7df4466 ("libbpf: Add support to detect nop,nop5 instructions combo for usdt probe")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 tools/lib/bpf/usdt.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c
index 2e56e3ab5b6c..c266ac93cbdd 100644
--- a/tools/lib/bpf/usdt.c
+++ b/tools/lib/bpf/usdt.c
@@ -614,11 +614,28 @@ static bool has_nop_combo(int fd, long off)
 		return false;
 	return memcmp(buf, nop_combo, 11) == 0;
 }
+
+/*
+ * The kernel refuses to attach to a nop10 that crosses a page boundary,
+ * as it can't be atomically rewritten. Page offset of the probe is the
+ * same in the file and in any mapping, so this can be checked statically.
+ */
+static bool nop10_within_page(long off)
+{
+	long page_sz = getpagesize();
+
+	return off % page_sz + 10 <= page_sz;
+}
 #else
 static bool has_nop_combo(int fd, long off)
 {
 	return false;
 }
+
+static bool nop10_within_page(long off)
+{
+	return false;
+}
 #endif
 
 static int collect_usdt_targets(struct usdt_manager *man, struct elf_fd *elf_fd, const char *path,
@@ -827,9 +844,11 @@ static int collect_usdt_targets(struct usdt_manager *man, struct elf_fd *elf_fd,
 		/*
 		 * We have uprobe syscall and usdt with nop,nop10 instructions combo,
 		 * so we can place the uprobe directly on nop10 (+1) and get this probe
-		 * optimized.
+		 * optimized. If the nop10 crosses a page boundary, keep the uprobe
+		 * on the preceding 1-byte nop, which the kernel accepts everywhere.
 		 */
-		if (man->has_uprobe_syscall && has_nop_combo(elf_fd->fd, usdt_rel_ip)) {
+		if (man->has_uprobe_syscall && has_nop_combo(elf_fd->fd, usdt_rel_ip) &&
+		    nop10_within_page(usdt_rel_ip + 1)) {
 			usdt_abs_ip++;
 			usdt_rel_ip++;
 		}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-08-25 18:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24  9:28 [PATCH bpf 1/2] libbpf: Fix usdt attach failure when nop10 crosses page boundary Jiayuan Chen
2026-08-24  9:28 ` [PATCH bpf 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10 Jiayuan Chen
2026-08-24 10:03   ` bot+bpf-ci
2026-08-24 10:03 ` [PATCH bpf 1/2] libbpf: Fix usdt attach failure when nop10 crosses page boundary bot+bpf-ci
2026-08-24 10:27   ` Jiayuan Chen
2026-08-25 13:58 ` Jiri Olsa
2026-08-25 14:29   ` Jiayuan Chen
2026-08-25 18:32     ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox