Re: [PATCH bpf 2/2] selftests/bpf: Add test for usdt probe with page-crossing nop10

[email protected]
Newsgroups org.kernel.vger.bpf,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest
Message-ID <796b7f30046de601f05035d9705806b2cc098cb37b81c957797bee18022586be@mail.kernel.org>
> diff --git a/tools/testing/selftests/bpf/prog_tests/usdt.c b/tools/testing/selftests/bpf/prog_tests/usdt.c
> index 8004c9568ffa..eff1e57ab13c 100644
> --- a/tools/testing/selftests/bpf/prog_tests/usdt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/usdt.c

[ ... ]

> @@ -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++) {

This isn't a bug, but should the comment say up to two pages to match the
2 * page_sz scan bound? With the .balign 4096 fill plus the 4086-byte
.skip in usdt_2_cross_page(), the combo can land roughly 8150 bytes past
the function start when the compiler emits a prologue.

> +		if (!memcmp((void *)usdt_2_cross_page + i, nop1_nop10_combo, 11)) {

This isn't a bug, but could find_instr() take a search range so this
subtest reuses it rather than open-coding the same memcmp scan?
find_instr() already implements this loop a hundred lines above, only
with a hardcoded 10-byte window instead of 2 * page_sz.

> +			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);
> +}

[ ... ]


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/32712703327
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.