libbpf: Fix usdt attach failure when nop10 crosses page boundary
Subsystems (4)
PATCH v2 1/2 libbpf: Fix usdt attach failure when nop10 crosses page boundary
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.
---
v1 -> v2: move check into has_nop_combo.
v1: https://lore.kernel.org/bpf/[email protected]/
--- tools/lib/bpf/usdt.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/lib/bpf/usdt.c +4 -0
@@ -608,8 +608,12 @@ static bool has_nop_combo(int fd, long off)unsigned char nop_combo[11 ] = {0x90 ,0x66 ,0x2e ,0x0f ,0x1f ,0x84 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 , }; +long page_sz =getpagesize ();unsigned char buf[11 ]; +/* the kernel can't attach to a nop10 that crosses a page boundary */ +if ((off +1 ) % page_sz +10 > page_sz) +return false;if (pread (fd, buf,11 , off) !=11 )return false;return memcmp (buf, nop_combo,11 ) ==0 ;
Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/bpf: Add test for usdt probe with page-crossing nop10
Add a USDT probe laid out so that its nop10 crosses a page boundary and check that attaching to it succeeds and the probe fires. Without the previous fix libbpf shifts the uprobe onto the nop10 and the attach fails with -ENOTSUPP.
--- tools/testing/selftests/bpf/prog_tests/usdt.c | 54 +++++++++++++++++++ tools/testing/selftests/bpf/usdt_2.c | 16 ++++++ 2 files changed, 70 insertions(+)
tools/testing/selftests/bpf/prog_tests/usdt.c +54 -0
@@ -250,6 +250,7 @@ static void subtest_basic_usdt(bool optimized)#ifdef __x86_64__extern void usdt_1 (void );extern void usdt_2 (void ); +extern void usdt_2_cross_page (void );extern void usdt_red_zone_trigger (void );static unsigned char nop1[1 ] = {0x90 };
@@ -342,6 +343,57 @@ static void subtest_optimized_attach(void)test_usdt__destroy (skel); } +/* +* Test attachment to a USDT probe whose nop10 crosses a page boundary. +* The kernel can't optimize such nop10, so libbpf keeps the uprobe on +* the preceding 1-byte nop. Do not assume any particular placement +* here, though: however the probe ends up attached, the attachment +* must succeed and the probe must fire. +*/ +static void subtest_optimized_attach_cross_page (void ) +{ +long page_sz =sysconf (_SC_PAGESIZE); +struct test_usdt *skel; +__u8 *addr =NULL ; +long i; + +/* combo is placed up to a page of padding after the function start */ +for (i =0 ; i <2 * page_sz; i++) { +if (!memcmp ((void *)usdt_2_cross_page + i, nop1_nop10_combo,11 )) { + addr = (void *)usdt_2_cross_page + i; +break ; + } + } +if (!ASSERT_OK_PTR (addr,"find_nop1_nop10_combo" )) +return ; + +/* layout sanity check: the nop10 must cross the page boundary */ +if (!ASSERT_GT ((unsigned long )(addr +1 ) % page_sz +10 , page_sz, +"nop10_crosses_page" )) +return ; + + skel =test_usdt__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_usdt__open_and_load" )) +return ; + + skel->bss->my_pid =getpid (); + + skel->links.usdt0 =bpf_program__attach_usdt (skel->progs.usdt0, +0 /*self*/ ,"/proc/self/exe" , +"optimized_attach" , +"usdt_2_cross_page" ,NULL ); +if (!ASSERT_OK_PTR (skel->links.usdt0,"bpf_program__attach_usdt" )) + goto cleanup; + +usdt_2_cross_page (); +usdt_2_cross_page (); + +ASSERT_EQ (skel->bss->usdt0_called,2 ,"usdt0_called" ); + +cleanup: +test_usdt__destroy (skel); +} +/* * Test that USDT arguments survive nop10 optimization in a function where * the compiler places operands in the red zone.
@@ -660,6 +712,8 @@ void test_usdt(void)subtest_basic_usdt (true);if (test__start_subtest ("optimized_attach" ))subtest_optimized_attach (); +if (test__start_subtest ("optimized_attach_cross_page" )) +subtest_optimized_attach_cross_page ();if (test__start_subtest ("optimized_red_zone" ))subtest_optimized_red_zone ();#endif
tools/testing/selftests/bpf/usdt_2.c +16 -0
@@ -13,6 +13,22 @@ void usdt_2(void)USDT (optimized_attach, usdt_2); } +/* +* Force the nop1,nop10 combo of the USDT probe to a spot where the nop10 +* crosses a page boundary: .balign starts the padding exactly at a page +* start regardless of the compiler-generated prologue size, and the 4086 +* one-byte nops put the nop1 at page offset 4086, so the following nop10 +* occupies the last 9 bytes of that page and 1 byte of the next one. +* The kernel can't optimize such nop10, so libbpf must keep the uprobe +* on the 1-byte nop. +*/ +__attribute__((noinline)) +void usdt_2_cross_page (void ) +{ + asmvolatile (".balign 4096, 0x90\n\t.skip 4086, 0x90" ); +USDT (optimized_attach, usdt_2_cross_page); +} +static volatile unsigned long usdt_red_zone_arg1 =0xDEADBEEF ;static volatile unsigned long usdt_red_zone_arg2 =0xCAFEBABE ;static volatile unsigned long usdt_red_zone_arg3 =0xFEEDFACE ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH bpf v2 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The search loop limit `2 * page_sz` for locating the USDT probe can cause the test to falsely…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The search loop limit 2 * page_sz for locating the USDT probe can cause the test to falsely fail if the compiler generates a prologue of 11 bytes or larger. --
--- Patch [2]: [PATCH bpf v2 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10 --- commit 3ab03ec0febc59ef7c61c7f1449047ef827d5e7b
Author: Jiayuan Chen <[email protected]>
selftests/bpf: Add test for usdt probe with page-crossing nop10
This commit adds a USDT probe layout where the nop10 crosses a page boundary. It verifies that attaching to it succeeds and the probe fires without the uprobe being shifted onto the nop10 incorrectly.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +0
-0
[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +6
-0@@ -342,6 +343,57 @@ static void subtest_optimized_attach(void)test_usdt__destroy (skel); } +/* +* Test attachment to a USDT probe whose nop10 crosses a page boundary. +* The kernel can't optimize such nop10, so libbpf keeps the uprobe on +* the preceding 1-byte nop. Do not assume any particular placement +* here, though: however the probe ends up attached, the attachment +* must succeed and the probe must fire.
tools/testing/selftests/bpf/prog_tests/usdt.c static void subtest_optimized_attach(void) · line 361
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +10
-0+*/ +static void subtest_optimized_attach_cross_page (void ) +{ +long page_sz =sysconf (_SC_PAGESIZE); +struct test_usdt *skel; +__u8 *addr =NULL ; +long i; + +/* combo is placed up to a page of padding after the function start */ +for (i =0 ; i <2 * page_sz; i++) {
[Severity: Medium]
Will this loop bound of 2 * page_sz (8192) cause the test to falsely fail on builds with larger compiler prologues (like ASAN builds or those with frame pointers enabled)?
If the compiler generates a prologue of size P, the offset to the probe is P + align_padding (up to 4095) + skip_padding (4086) = P + 8181.
If P is 11 bytes or larger, the offset reaches 8192, which would exceed this i < 2 * page_sz search bound and cause a spurious ASSERT_OK_PTR failure.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +7
-0+if (!memcmp ((void *)usdt_2_cross_page + i, nop1_nop10_combo,11 )) { + addr = (void *)usdt_2_cross_page + i; +break ; + } + } +if (!ASSERT_OK_PTR (addr,"find_nop1_nop10_combo" )) +return ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf v2 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10
Quoted 44 lines · On 8/25/26 11:13 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The search loop limit2 * page_szfor locating the USDT probe can cause the test to falsely fail if the compiler generates a prologue of 11 bytes or larger. ----- Patch [2]: [PATCH bpf v2 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10 --- commit
3ab03ec0febc59ef7c61c7f1449047ef827d5e7b
Author: Jiayuan Chen <[email protected]>selftests/bpf: Add test for usdt probe with page-crossing nop10
This commit adds a USDT probe layout where the nop10 crosses a page boundary. It verifies that attaching to it succeeds and the probe fires without the uprobe being shifted onto the nop10 incorrectly.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +0
-0[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +6
-0@@ -342,6 +343,57 @@ static void subtest_optimized_attach(void)test_usdt__destroy (skel); } +/* +* Test attachment to a USDT probe whose nop10 crosses a page boundary. +* The kernel can't optimize such nop10, so libbpf keeps the uprobe on +* the preceding 1-byte nop. Do not assume any particular placement +* here, though: however the probe ends up attached, the attachment +* must succeed and the probe must fire. tools/testing/selftests/bpf/prog_tests/usdt.c static void subtest_optimized_attach(void) · line 361
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +10
-0+*/ +static void subtest_optimized_attach_cross_page (void ) +{ +long page_sz =sysconf (_SC_PAGESIZE); +struct test_usdt *skel; +__u8 *addr =NULL ; +long i; + +/* combo is placed up to a page of padding after the function start */ +for (i =0 ; i <2 * page_sz; i++) {[Severity: Medium]
Will this loop bound of 2 *page_sz(8192) cause the test to falsely fail on builds with larger compiler prologues (like ASAN builds or those with frame pointers enabled)?
Quoted 6 lines
If the compiler generates a prologue of size P, the offset to the probe is P +
align_padding(up to 4095) +skip_padding(4086) = P + 8181.If P is 11 bytes or larger, the offset reaches 8192, which would exceed this i < 2 *
page_szsearch bound and cause a spuriousASSERT_OK_PTRfailure.
I got such review locally, but I want to say that if such a compiler existed, the existing tests would break before
this one: find_instr() in subtest_optimized_attach() only scans 10 bytes past the function start.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/usdt.c +7
-0+if (!memcmp ((void *)usdt_2_cross_page + i, nop1_nop10_combo,11 )) { + addr = (void *)usdt_2_cross_page + i; +break ; + } + } +if (!ASSERT_OK_PTR (addr,"find_nop1_nop10_combo" )) +return ;
Quoted 29 lines · Jiayuan Chen
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 ee2862439e5c ("libbpf: Change has_nop_combo to work on top of nop10")Signed-off-by Jiayuan Chen <[email protected]>---
v1 -> v2: move check intohas_nop_combo.
v1: https://lore.kernel.org/bpf/[email protected]/--- tools/lib/bpf/usdt.c | 4 ++++ 1 file changed, 4 insertions(+)tools/lib/bpf/usdt.c +0
-0
tools/lib/bpf/usdt.c static bool has_nop_combo(int fd, long off) · line 616
Quoted 9 lines
tools/lib/bpf/usdt.c +4
-0@@ -608,8 +608,12 @@ static bool has_nop_combo(int fd, long off)unsigned char nop_combo[11 ] = {0x90 ,0x66 ,0x2e ,0x0f ,0x1f ,0x84 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 , }; +long page_sz =getpagesize ();unsigned char buf[11 ]; +/* the kernel can't attach to a nop10 that crosses a page boundary */ +if ((off +1 ) % page_sz +10 > page_sz) +return false;
yeah, this works ok, applied this version, disregard request to unify has_nop_combo() with man->has_uprobe_syscall check
Quoted 6 lines
tools/lib/bpf/usdt.c +0
-1if (pread (fd, buf,11 , off) !=11 )return false;return memcmp (buf, nop_combo,11 ) ==0 ; --2.43.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Tue, 25 Aug 2026 23:04:33 +0800 you wrote:
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;[...]
Here is the summary with links:
- [bpf,v2,1/2] libbpf: Fix usdt attach failure when nop10 crosses page boundary
https://git.kernel.org/bpf/bpf-next/c/8b94878b2572 - [bpf,v2,2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10 https://git.kernel.org/bpf/bpf-next/c/1555de33018f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html