Skip to content

Commit 6532d24

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
selftests/bpf: Add test for checking correct nop of optimized usdt
Adding test that attaches bpf program on usdt probe in 2 scenarios; - attach program on top of usdt_1 which is standard nop probe incidentally followed by nop5. The usdt probe does not have extra data in elf note record, so we expect the probe to land on the first nop without being optimized. - attach program on top of usdt_2 which is probe defined on top of nop,nop5 combo. The extra data in the elf note record and presence of upeobe syscall ensures that the probe is placed on top of nop5 and optimized. Signed-off-by: Jiri Olsa <[email protected]>
1 parent 5f86aaf commit 6532d24

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-1
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ xdp_synproxy
4545
xdp_hw_metadata
4646
xdp_features
4747
verification_cert.h
48+
usdt_1
49+
usdt_2

tools/testing/selftests/bpf/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,10 @@ $(VERIFICATION_CERT) $(PRIVATE_KEY): $(VERIFY_SIG_SETUP)
733733
$(VERIFY_SIG_HDR): $(VERIFICATION_CERT)
734734
$(Q)xxd -i -n test_progs_verification_cert $< > $@
735735

736+
ifeq ($(SRCARCH),$(filter $(SRCARCH),x86))
737+
USDTX := usdt_1.c usdt_2.c
738+
endif
739+
736740
# Define test_progs test runner.
737741
TRUNNER_TESTS_DIR := prog_tests
738742
TRUNNER_BPF_PROGS_DIR := progs
@@ -754,7 +758,8 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
754758
json_writer.c \
755759
$(VERIFY_SIG_HDR) \
756760
flow_dissector_load.h \
757-
ip_check_defrag_frags.h
761+
ip_check_defrag_frags.h \
762+
$(USDTX)
758763
TRUNNER_LIB_SOURCES := find_bit.c
759764
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
760765
$(OUTPUT)/liburandom_read.so \

tools/testing/selftests/bpf/prog_tests/usdt.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,86 @@ static void subtest_basic_usdt(bool optimized)
247247
#undef TRIGGER
248248
}
249249

250+
#ifdef __x86_64
251+
extern void usdt_1(void);
252+
extern void usdt_2(void);
253+
254+
/* nop, nop5 */
255+
static unsigned char nop15[6] = { 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00 };
256+
257+
static void *find_nop15(void *fn)
258+
{
259+
int i;
260+
261+
for (i = 0; i < 10; i++) {
262+
if (!memcmp(nop15, fn + i, 5))
263+
return fn + i;
264+
}
265+
return NULL;
266+
}
267+
268+
static void subtest_optimized_attach(void)
269+
{
270+
struct test_usdt *skel;
271+
__u8 *addr_1, *addr_2;
272+
273+
addr_1 = find_nop15(usdt_1);
274+
if (!ASSERT_OK_PTR(addr_1, "find_nop15"))
275+
return;
276+
277+
addr_2 = find_nop15(usdt_2);
278+
if (!ASSERT_OK_PTR(addr_2, "find_nop15"))
279+
return;
280+
281+
skel = test_usdt__open_and_load();
282+
if (!ASSERT_OK_PTR(skel, "test_usdt__open_and_load"))
283+
return;
284+
285+
/*
286+
* Attach program on top of usdt_1 which is standard nop probe
287+
* incidentally followed by nop5. The usdt probe does not have
288+
* extra data in elf note record, so we expect the probe to land
289+
* on the first nop without being optimized.
290+
*/
291+
skel->links.usdt_executed = bpf_program__attach_usdt(skel->progs.usdt_executed,
292+
0 /*self*/, "/proc/self/exe",
293+
"optimized_attach", "usdt_1", NULL);
294+
if (!ASSERT_OK_PTR(skel->links.usdt_executed, "bpf_program__attach_usdt"))
295+
goto cleanup;
296+
297+
usdt_1();
298+
usdt_1();
299+
300+
/* nop is on addr_1 address */
301+
ASSERT_EQ(*addr_1, 0xcc, "int3");
302+
ASSERT_EQ(skel->bss->executed, 2, "executed");
303+
304+
bpf_link__destroy(skel->links.usdt_executed);
305+
306+
/*
307+
* Attach program on top of usdt_2 which is probe defined on top
308+
* of nop,nop5 combo. The extra data in the elf note record and
309+
* presence of uprobe syscall ensures that the probe is placed
310+
* on top of nop5 and optimized.
311+
*/
312+
skel->links.usdt_executed = bpf_program__attach_usdt(skel->progs.usdt_executed,
313+
0 /*self*/, "/proc/self/exe",
314+
"optimized_attach", "usdt_2", NULL);
315+
if (!ASSERT_OK_PTR(skel->links.usdt_executed, "bpf_program__attach_usdt"))
316+
goto cleanup;
317+
318+
usdt_2();
319+
usdt_2();
320+
321+
/* nop5 is on addr_2 + 1 address */
322+
ASSERT_EQ(*(addr_2 + 1), 0xe8, "call");
323+
ASSERT_EQ(skel->bss->executed, 4, "executed");
324+
325+
cleanup:
326+
test_usdt__destroy(skel);
327+
}
328+
#endif
329+
250330
unsigned short test_usdt_100_semaphore SEC(".probes");
251331
unsigned short test_usdt_300_semaphore SEC(".probes");
252332
unsigned short test_usdt_400_semaphore SEC(".probes");
@@ -516,6 +596,8 @@ void test_usdt(void)
516596
#ifdef __x86_64__
517597
if (test__start_subtest("basic_optimized"))
518598
subtest_basic_usdt(true);
599+
if (test__start_subtest("optimized_attach"))
600+
subtest_optimized_attach();
519601
#endif
520602
if (test__start_subtest("multispec"))
521603
subtest_multispec_usdt();

tools/testing/selftests/bpf/progs/test_usdt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,13 @@ int usdt_sib(struct pt_regs *ctx)
138138
return 0;
139139
}
140140

141+
int executed;
142+
143+
SEC("usdt")
144+
int usdt_executed(struct pt_regs *ctx)
145+
{
146+
executed++;
147+
return 0;
148+
}
149+
141150
char _license[] SEC("license") = "GPL";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Include usdt.h with defined USDT_NOP macro will switch
5+
* off the extra info in usdt probe.
6+
*/
7+
#define USDT_NOP .byte 0x90, 0x0f, 0x1f, 0x44, 0x00, 0x00
8+
#include "usdt.h"
9+
10+
__attribute__((aligned(16)))
11+
void usdt_1(void)
12+
{
13+
USDT(optimized_attach, usdt_1);
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Include usdt.h with the extra info in usdt probe and
5+
* nop/nop5 combo for usdt attach point.
6+
*/
7+
#include "usdt.h"
8+
9+
__attribute__((aligned(16)))
10+
void usdt_2(void)
11+
{
12+
USDT(optimized_attach, usdt_2);
13+
}

0 commit comments

Comments
 (0)