Skip to content

Commit 3399dd9

Browse files
committed
Merge branch 'BPF test_progs tests improvement'
Mykola Lysenko says: ==================== First patch reduces the sample_freq to 1000 to ensure test will work even when kernel.perf_event_max_sample_rate was reduced to 1000. Patches for send_signal and find_vma tune the test implementation to make sure needed thread is scheduled. Also, both tests will finish as soon as possible after the test condition is met. ==================== Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 7fd9fd4 + ba83af0 commit 3399dd9

File tree

6 files changed

+35
-22
lines changed

6 files changed

+35
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void pe_subtest(struct test_bpf_cookie *skel)
199199
attr.type = PERF_TYPE_SOFTWARE;
200200
attr.config = PERF_COUNT_SW_CPU_CLOCK;
201201
attr.freq = 1;
202-
attr.sample_freq = 4000;
202+
attr.sample_freq = 1000;
203203
pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
204204
if (!ASSERT_GE(pfd, 0, "perf_fd"))
205205
goto cleanup;

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#include "find_vma_fail1.skel.h"
88
#include "find_vma_fail2.skel.h"
99

10-
static void test_and_reset_skel(struct find_vma *skel, int expected_find_zero_ret)
10+
static void test_and_reset_skel(struct find_vma *skel, int expected_find_zero_ret, bool need_test)
1111
{
12-
ASSERT_EQ(skel->bss->found_vm_exec, 1, "found_vm_exec");
13-
ASSERT_EQ(skel->data->find_addr_ret, 0, "find_addr_ret");
14-
ASSERT_EQ(skel->data->find_zero_ret, expected_find_zero_ret, "find_zero_ret");
15-
ASSERT_OK_PTR(strstr(skel->bss->d_iname, "test_progs"), "find_test_progs");
12+
if (need_test) {
13+
ASSERT_EQ(skel->bss->found_vm_exec, 1, "found_vm_exec");
14+
ASSERT_EQ(skel->data->find_addr_ret, 0, "find_addr_ret");
15+
ASSERT_EQ(skel->data->find_zero_ret, expected_find_zero_ret, "find_zero_ret");
16+
ASSERT_OK_PTR(strstr(skel->bss->d_iname, "test_progs"), "find_test_progs");
17+
}
1618

1719
skel->bss->found_vm_exec = 0;
1820
skel->data->find_addr_ret = -1;
@@ -30,17 +32,26 @@ static int open_pe(void)
3032
attr.type = PERF_TYPE_HARDWARE;
3133
attr.config = PERF_COUNT_HW_CPU_CYCLES;
3234
attr.freq = 1;
33-
attr.sample_freq = 4000;
35+
attr.sample_freq = 1000;
3436
pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC);
3537

3638
return pfd >= 0 ? pfd : -errno;
3739
}
3840

41+
static bool find_vma_pe_condition(struct find_vma *skel)
42+
{
43+
return skel->bss->found_vm_exec == 0 ||
44+
skel->data->find_addr_ret != 0 ||
45+
skel->data->find_zero_ret == -1 ||
46+
strcmp(skel->bss->d_iname, "test_progs") != 0;
47+
}
48+
3949
static void test_find_vma_pe(struct find_vma *skel)
4050
{
4151
struct bpf_link *link = NULL;
4252
volatile int j = 0;
4353
int pfd, i;
54+
const int one_bn = 1000000000;
4455

4556
pfd = open_pe();
4657
if (pfd < 0) {
@@ -57,10 +68,10 @@ static void test_find_vma_pe(struct find_vma *skel)
5768
if (!ASSERT_OK_PTR(link, "attach_perf_event"))
5869
goto cleanup;
5970

60-
for (i = 0; i < 1000000; ++i)
71+
for (i = 0; i < one_bn && find_vma_pe_condition(skel); ++i)
6172
++j;
6273

63-
test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */);
74+
test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */, i == one_bn);
6475
cleanup:
6576
bpf_link__destroy(link);
6677
close(pfd);
@@ -75,7 +86,7 @@ static void test_find_vma_kprobe(struct find_vma *skel)
7586
return;
7687

7788
getpgid(skel->bss->target_pid);
78-
test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */);
89+
test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */, true);
7990
}
8091

8192
static void test_illegal_write_vma(void)
@@ -108,7 +119,6 @@ void serial_test_find_vma(void)
108119
skel->bss->addr = (__u64)(uintptr_t)test_find_vma_pe;
109120

110121
test_find_vma_pe(skel);
111-
usleep(100000); /* allow the irq_work to finish */
112122
test_find_vma_kprobe(skel);
113123

114124
find_vma__destroy(skel);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static void test_perf_branches_hw(void)
110110
attr.type = PERF_TYPE_HARDWARE;
111111
attr.config = PERF_COUNT_HW_CPU_CYCLES;
112112
attr.freq = 1;
113-
attr.sample_freq = 4000;
113+
attr.sample_freq = 1000;
114114
attr.sample_type = PERF_SAMPLE_BRANCH_STACK;
115115
attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_ANY;
116116
pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
@@ -151,7 +151,7 @@ static void test_perf_branches_no_hw(void)
151151
attr.type = PERF_TYPE_SOFTWARE;
152152
attr.config = PERF_COUNT_SW_CPU_CLOCK;
153153
attr.freq = 1;
154-
attr.sample_freq = 4000;
154+
attr.sample_freq = 1000;
155155
pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
156156
if (CHECK(pfd < 0, "perf_event_open", "err %d\n", pfd))
157157
return;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void serial_test_perf_link(void)
3939
attr.type = PERF_TYPE_SOFTWARE;
4040
attr.config = PERF_COUNT_SW_CPU_CLOCK;
4141
attr.freq = 1;
42-
attr.sample_freq = 4000;
42+
attr.sample_freq = 1000;
4343
pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC);
4444
if (!ASSERT_GE(pfd, 0, "perf_fd"))
4545
goto cleanup;

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include <sys/resource.h>
55
#include "test_send_signal_kern.skel.h"
66

7-
int sigusr1_received = 0;
7+
static int sigusr1_received;
88

99
static void sigusr1_handler(int signum)
1010
{
11-
sigusr1_received++;
11+
sigusr1_received = 1;
1212
}
1313

1414
static void test_send_signal_common(struct perf_event_attr *attr,
@@ -40,9 +40,10 @@ static void test_send_signal_common(struct perf_event_attr *attr,
4040

4141
if (pid == 0) {
4242
int old_prio;
43+
volatile int j = 0;
4344

4445
/* install signal handler and notify parent */
45-
signal(SIGUSR1, sigusr1_handler);
46+
ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
4647

4748
close(pipe_c2p[0]); /* close read */
4849
close(pipe_p2c[1]); /* close write */
@@ -63,9 +64,11 @@ static void test_send_signal_common(struct perf_event_attr *attr,
6364
ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
6465

6566
/* wait a little for signal handler */
66-
sleep(1);
67+
for (int i = 0; i < 100000000 && !sigusr1_received; i++)
68+
j /= i + 1;
6769

6870
buf[0] = sigusr1_received ? '2' : '0';
71+
ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
6972
ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
7073

7174
/* wait for parent notification and exit */
@@ -93,7 +96,7 @@ static void test_send_signal_common(struct perf_event_attr *attr,
9396
goto destroy_skel;
9497
}
9598
} else {
96-
pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1,
99+
pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
97100
-1 /* group id */, 0 /* flags */);
98101
if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
99102
err = -1;
@@ -110,9 +113,9 @@ static void test_send_signal_common(struct perf_event_attr *attr,
110113
ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
111114

112115
/* trigger the bpf send_signal */
113-
skel->bss->pid = pid;
114-
skel->bss->sig = SIGUSR1;
115116
skel->bss->signal_thread = signal_thread;
117+
skel->bss->sig = SIGUSR1;
118+
skel->bss->pid = pid;
116119

117120
/* notify child that bpf program can send_signal now */
118121
ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static __always_inline int bpf_send_signal_test(void *ctx)
1010
{
1111
int ret;
1212

13-
if (status != 0 || sig == 0 || pid == 0)
13+
if (status != 0 || pid == 0)
1414
return 0;
1515

1616
if ((bpf_get_current_pid_tgid() >> 32) == pid) {

0 commit comments

Comments
 (0)