Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <andrii@kernel.org>

+35 -22
+1 -1
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
··· 199 199 attr.type = PERF_TYPE_SOFTWARE; 200 200 attr.config = PERF_COUNT_SW_CPU_CLOCK; 201 201 attr.freq = 1; 202 - attr.sample_freq = 4000; 202 + attr.sample_freq = 1000; 203 203 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC); 204 204 if (!ASSERT_GE(pfd, 0, "perf_fd")) 205 205 goto cleanup;
+20 -10
tools/testing/selftests/bpf/prog_tests/find_vma.c
··· 7 7 #include "find_vma_fail1.skel.h" 8 8 #include "find_vma_fail2.skel.h" 9 9 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) 11 11 { 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 + } 16 18 17 19 skel->bss->found_vm_exec = 0; 18 20 skel->data->find_addr_ret = -1; ··· 32 30 attr.type = PERF_TYPE_HARDWARE; 33 31 attr.config = PERF_COUNT_HW_CPU_CYCLES; 34 32 attr.freq = 1; 35 - attr.sample_freq = 4000; 33 + attr.sample_freq = 1000; 36 34 pfd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC); 37 35 38 36 return pfd >= 0 ? pfd : -errno; 37 + } 38 + 39 + static bool find_vma_pe_condition(struct find_vma *skel) 40 + { 41 + return skel->bss->found_vm_exec == 0 || 42 + skel->data->find_addr_ret != 0 || 43 + skel->data->find_zero_ret == -1 || 44 + strcmp(skel->bss->d_iname, "test_progs") != 0; 39 45 } 40 46 41 47 static void test_find_vma_pe(struct find_vma *skel) ··· 51 41 struct bpf_link *link = NULL; 52 42 volatile int j = 0; 53 43 int pfd, i; 44 + const int one_bn = 1000000000; 54 45 55 46 pfd = open_pe(); 56 47 if (pfd < 0) { ··· 68 57 if (!ASSERT_OK_PTR(link, "attach_perf_event")) 69 58 goto cleanup; 70 59 71 - for (i = 0; i < 1000000; ++i) 60 + for (i = 0; i < one_bn && find_vma_pe_condition(skel); ++i) 72 61 ++j; 73 62 74 - test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */); 63 + test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */, i == one_bn); 75 64 cleanup: 76 65 bpf_link__destroy(link); 77 66 close(pfd); ··· 86 75 return; 87 76 88 77 getpgid(skel->bss->target_pid); 89 - test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */); 78 + test_and_reset_skel(skel, -ENOENT /* could not find vma for ptr 0 */, true); 90 79 } 91 80 92 81 static void test_illegal_write_vma(void) ··· 119 108 skel->bss->addr = (__u64)(uintptr_t)test_find_vma_pe; 120 109 121 110 test_find_vma_pe(skel); 122 - usleep(100000); /* allow the irq_work to finish */ 123 111 test_find_vma_kprobe(skel); 124 112 125 113 find_vma__destroy(skel);
+2 -2
tools/testing/selftests/bpf/prog_tests/perf_branches.c
··· 110 110 attr.type = PERF_TYPE_HARDWARE; 111 111 attr.config = PERF_COUNT_HW_CPU_CYCLES; 112 112 attr.freq = 1; 113 - attr.sample_freq = 4000; 113 + attr.sample_freq = 1000; 114 114 attr.sample_type = PERF_SAMPLE_BRANCH_STACK; 115 115 attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_ANY; 116 116 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC); ··· 151 151 attr.type = PERF_TYPE_SOFTWARE; 152 152 attr.config = PERF_COUNT_SW_CPU_CLOCK; 153 153 attr.freq = 1; 154 - attr.sample_freq = 4000; 154 + attr.sample_freq = 1000; 155 155 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC); 156 156 if (CHECK(pfd < 0, "perf_event_open", "err %d\n", pfd)) 157 157 return;
+1 -1
tools/testing/selftests/bpf/prog_tests/perf_link.c
··· 39 39 attr.type = PERF_TYPE_SOFTWARE; 40 40 attr.config = PERF_COUNT_SW_CPU_CLOCK; 41 41 attr.freq = 1; 42 - attr.sample_freq = 4000; 42 + attr.sample_freq = 1000; 43 43 pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC); 44 44 if (!ASSERT_GE(pfd, 0, "perf_fd")) 45 45 goto cleanup;
+10 -7
tools/testing/selftests/bpf/prog_tests/send_signal.c
··· 4 4 #include <sys/resource.h> 5 5 #include "test_send_signal_kern.skel.h" 6 6 7 - int sigusr1_received = 0; 7 + static int sigusr1_received; 8 8 9 9 static void sigusr1_handler(int signum) 10 10 { 11 - sigusr1_received++; 11 + sigusr1_received = 1; 12 12 } 13 13 14 14 static void test_send_signal_common(struct perf_event_attr *attr, ··· 40 40 41 41 if (pid == 0) { 42 42 int old_prio; 43 + volatile int j = 0; 43 44 44 45 /* install signal handler and notify parent */ 45 - signal(SIGUSR1, sigusr1_handler); 46 + ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal"); 46 47 47 48 close(pipe_c2p[0]); /* close read */ 48 49 close(pipe_p2c[1]); /* close write */ ··· 64 63 ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read"); 65 64 66 65 /* wait a little for signal handler */ 67 - sleep(1); 66 + for (int i = 0; i < 100000000 && !sigusr1_received; i++) 67 + j /= i + 1; 68 68 69 69 buf[0] = sigusr1_received ? '2' : '0'; 70 + ASSERT_EQ(sigusr1_received, 1, "sigusr1_received"); 70 71 ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write"); 71 72 72 73 /* wait for parent notification and exit */ ··· 96 93 goto destroy_skel; 97 94 } 98 95 } else { 99 - pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1, 96 + pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */, 100 97 -1 /* group id */, 0 /* flags */); 101 98 if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) { 102 99 err = -1; ··· 113 110 ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read"); 114 111 115 112 /* trigger the bpf send_signal */ 116 - skel->bss->pid = pid; 117 - skel->bss->sig = SIGUSR1; 118 113 skel->bss->signal_thread = signal_thread; 114 + skel->bss->sig = SIGUSR1; 115 + skel->bss->pid = pid; 119 116 120 117 /* notify child that bpf program can send_signal now */ 121 118 ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
+1 -1
tools/testing/selftests/bpf/progs/test_send_signal_kern.c
··· 10 10 { 11 11 int ret; 12 12 13 - if (status != 0 || sig == 0 || pid == 0) 13 + if (status != 0 || pid == 0) 14 14 return 0; 15 15 16 16 if ((bpf_get_current_pid_tgid() >> 32) == pid) {