]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Refactor timer selftests
authorMykyta Yatsenko <yatsenko@meta.com>
Sun, 1 Feb 2026 02:53:58 +0000 (18:53 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 4 Feb 2026 00:58:47 +0000 (16:58 -0800)
Refactor timer selftests, extracting stress test into a separate test.
This makes it easier to debug test failures and allows to extend.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20260201025403.66625-5-alexei.starovoitov@gmail.com
tools/testing/selftests/bpf/prog_tests/timer.c

index 34f9ccce260293755980bcd6fcece491964f7929..4d853d1bd2a71b3d0f1ba0daa7a699945b4457fe 100644 (file)
@@ -22,13 +22,35 @@ static void *spin_lock_thread(void *arg)
        pthread_exit(arg);
 }
 
-static int timer(struct timer *timer_skel)
+
+static int timer_stress(struct timer *timer_skel)
 {
-       int i, err, prog_fd;
+       int i, err = 1, prog_fd;
        LIBBPF_OPTS(bpf_test_run_opts, topts);
        pthread_t thread_id[NUM_THR];
        void *ret;
 
+       prog_fd = bpf_program__fd(timer_skel->progs.race);
+       for (i = 0; i < NUM_THR; i++) {
+               err = pthread_create(&thread_id[i], NULL,
+                                    &spin_lock_thread, &prog_fd);
+               if (!ASSERT_OK(err, "pthread_create"))
+                       break;
+       }
+
+       while (i) {
+               err = pthread_join(thread_id[--i], &ret);
+               if (ASSERT_OK(err, "pthread_join"))
+                       ASSERT_EQ(ret, (void *)&prog_fd, "pthread_join");
+       }
+       return err;
+}
+
+static int timer(struct timer *timer_skel)
+{
+       int err, prog_fd;
+       LIBBPF_OPTS(bpf_test_run_opts, topts);
+
        err = timer__attach(timer_skel);
        if (!ASSERT_OK(err, "timer_attach"))
                return err;
@@ -63,25 +85,10 @@ static int timer(struct timer *timer_skel)
        /* check that code paths completed */
        ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
 
-       prog_fd = bpf_program__fd(timer_skel->progs.race);
-       for (i = 0; i < NUM_THR; i++) {
-               err = pthread_create(&thread_id[i], NULL,
-                                    &spin_lock_thread, &prog_fd);
-               if (!ASSERT_OK(err, "pthread_create"))
-                       break;
-       }
-
-       while (i) {
-               err = pthread_join(thread_id[--i], &ret);
-               if (ASSERT_OK(err, "pthread_join"))
-                       ASSERT_EQ(ret, (void *)&prog_fd, "pthread_join");
-       }
-
        return 0;
 }
 
-/* TODO: use pid filtering */
-void serial_test_timer(void)
+static void test_timer(int (*timer_test_fn)(struct timer *timer_skel))
 {
        struct timer *timer_skel = NULL;
        int err;
@@ -94,13 +101,23 @@ void serial_test_timer(void)
        if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load"))
                return;
 
-       err = timer(timer_skel);
+       err = timer_test_fn(timer_skel);
        ASSERT_OK(err, "timer");
        timer__destroy(timer_skel);
+}
+
+void serial_test_timer(void)
+{
+       test_timer(timer);
 
        RUN_TESTS(timer_failure);
 }
 
+void serial_test_timer_stress(void)
+{
+       test_timer(timer_stress);
+}
+
 void test_timer_interrupt(void)
 {
        struct timer_interrupt *skel = NULL;