]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Add timer stress test in NMI context
authorMykyta Yatsenko <yatsenko@meta.com>
Sun, 1 Feb 2026 02:54:01 +0000 (18:54 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 4 Feb 2026 00:58:47 +0000 (16:58 -0800)
Add stress tests for BPF timers that run in NMI context using perf_event
programs attached to PERF_COUNT_HW_CPU_CYCLES.

The tests cover three scenarios:
- nmi_race: Tests concurrent timer start and async cancel operations
- nmi_update: Tests updating a map element (effectively deleting and
  inserting new for array map) from within a timer callback
- nmi_cancel: Tests timer self-cancellation attempt.

A common test_common() helper is used to share timer setup logic across
all test modes.

The tests spawn multiple threads in a child process to generate
perf events, which trigger the BPF programs in NMI context. Hit counters
verify that the NMI code paths were actually exercised.

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-8-alexei.starovoitov@gmail.com
tools/testing/selftests/bpf/prog_tests/timer.c
tools/testing/selftests/bpf/progs/timer.c

index 2b932d4dfd436fd322bd07169f492e20e4ec7624..09ff21e1ad2f020146dd0623f3610d6db656812e 100644 (file)
@@ -1,12 +1,27 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2021 Facebook */
+#include <sched.h>
 #include <test_progs.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
 #include "timer.skel.h"
 #include "timer_failure.skel.h"
 #include "timer_interrupt.skel.h"
 
 #define NUM_THR 8
 
+static int perf_event_open(__u32 type, __u64 config, int pid, int cpu)
+{
+       struct perf_event_attr attr = {
+               .type = type,
+               .config = config,
+               .size = sizeof(struct perf_event_attr),
+               .sample_period = 10000,
+       };
+
+       return syscall(__NR_perf_event_open, &attr, pid, cpu, -1, 0);
+}
+
 static void *spin_lock_thread(void *arg)
 {
        int i, err, prog_fd = *(int *)arg;
@@ -57,6 +72,134 @@ static int timer_stress_async_cancel(struct timer *timer_skel)
        return timer_stress_runner(timer_skel, true);
 }
 
+static void *nmi_cpu_worker(void *arg)
+{
+       volatile __u64 num = 1;
+       int i;
+
+       for (i = 0; i < 500000000; ++i)
+               num *= (i % 7) + 1;
+       (void)num;
+
+       return NULL;
+}
+
+static int run_nmi_test(struct timer *timer_skel, struct bpf_program *prog)
+{
+       struct bpf_link *link = NULL;
+       int pe_fd = -1, pipefd[2] = {-1, -1}, pid = 0, status;
+       char buf = 0;
+       int ret = -1;
+
+       if (!ASSERT_OK(pipe(pipefd), "pipe"))
+               goto cleanup;
+
+       pid = fork();
+       if (pid == 0) {
+               /* Child: spawn multiple threads to consume multiple CPUs */
+               pthread_t threads[NUM_THR];
+               int i;
+
+               close(pipefd[1]);
+               read(pipefd[0], &buf, 1);
+               close(pipefd[0]);
+
+               for (i = 0; i < NUM_THR; i++)
+                       pthread_create(&threads[i], NULL, nmi_cpu_worker, NULL);
+               for (i = 0; i < NUM_THR; i++)
+                       pthread_join(threads[i], NULL);
+               exit(0);
+       }
+
+       if (!ASSERT_GE(pid, 0, "fork"))
+               goto cleanup;
+
+       /* Open perf event for child process across all CPUs */
+       pe_fd = perf_event_open(PERF_TYPE_HARDWARE,
+                               PERF_COUNT_HW_CPU_CYCLES,
+                               pid,  /* measure child process */
+                               -1);  /* on any CPU */
+       if (pe_fd < 0) {
+               if (errno == ENOENT || errno == EOPNOTSUPP) {
+                       printf("SKIP:no PERF_COUNT_HW_CPU_CYCLES\n");
+                       test__skip();
+                       ret = EOPNOTSUPP;
+                       goto cleanup;
+               }
+               ASSERT_GE(pe_fd, 0, "perf_event_open");
+               goto cleanup;
+       }
+
+       link = bpf_program__attach_perf_event(prog, pe_fd);
+       if (!ASSERT_OK_PTR(link, "attach_perf_event"))
+               goto cleanup;
+       pe_fd = -1;  /* Ownership transferred to link */
+
+       /* Signal child to start CPU work */
+       close(pipefd[0]);
+       pipefd[0] = -1;
+       write(pipefd[1], &buf, 1);
+       close(pipefd[1]);
+       pipefd[1] = -1;
+
+       waitpid(pid, &status, 0);
+       pid = 0;
+
+       /* Verify NMI context was hit */
+       ASSERT_GT(timer_skel->bss->test_hits, 0, "test_hits");
+       ret = 0;
+
+cleanup:
+       bpf_link__destroy(link);
+       if (pe_fd >= 0)
+               close(pe_fd);
+       if (pid > 0) {
+               write(pipefd[1], &buf, 1);
+               waitpid(pid, &status, 0);
+       }
+       if (pipefd[0] >= 0)
+               close(pipefd[0]);
+       if (pipefd[1] >= 0)
+               close(pipefd[1]);
+       return ret;
+}
+
+static int timer_stress_nmi_race(struct timer *timer_skel)
+{
+       int err;
+
+       err = run_nmi_test(timer_skel, timer_skel->progs.nmi_race);
+       if (err == EOPNOTSUPP)
+               return 0;
+       return err;
+}
+
+static int timer_stress_nmi_update(struct timer *timer_skel)
+{
+       int err;
+
+       err = run_nmi_test(timer_skel, timer_skel->progs.nmi_update);
+       if (err == EOPNOTSUPP)
+               return 0;
+       if (err)
+               return err;
+       ASSERT_GT(timer_skel->bss->update_hits, 0, "update_hits");
+       return 0;
+}
+
+static int timer_stress_nmi_cancel(struct timer *timer_skel)
+{
+       int err;
+
+       err = run_nmi_test(timer_skel, timer_skel->progs.nmi_cancel);
+       if (err == EOPNOTSUPP)
+               return 0;
+       if (err)
+               return err;
+       ASSERT_GT(timer_skel->bss->cancel_hits, 0, "cancel_hits");
+       return 0;
+}
+
 static int timer(struct timer *timer_skel)
 {
        int err, prog_fd;
@@ -159,6 +302,21 @@ void serial_test_timer_async_cancel(void)
        test_timer(timer_cancel_async);
 }
 
+void serial_test_timer_stress_nmi_race(void)
+{
+       test_timer(timer_stress_nmi_race);
+}
+
+void serial_test_timer_stress_nmi_update(void)
+{
+       test_timer(timer_stress_nmi_update);
+}
+
+void serial_test_timer_stress_nmi_cancel(void)
+{
+       test_timer(timer_stress_nmi_cancel);
+}
+
 void test_timer_interrupt(void)
 {
        struct timer_interrupt *skel = NULL;
index 4b4ca781e7cdcf78015359cbd8f8d8ff591d6036..d6d5fefcd9b13598d83070e573607129e8d579d3 100644 (file)
@@ -63,6 +63,9 @@ __u64 bss_data;
 __u64 abs_data;
 __u64 err;
 __u64 ok;
+__u64 test_hits;
+__u64 update_hits;
+__u64 cancel_hits;
 __u64 callback_check = 52;
 __u64 callback2_check = 52;
 __u64 pinned_callback_check;
@@ -427,30 +430,88 @@ static int race_timer_callback(void *race_array, int *race_key, struct bpf_timer
        return 0;
 }
 
-SEC("syscall")
-int race(void *ctx)
+/* Callback that updates its own map element */
+static int update_self_callback(void *map, int *key, struct bpf_timer *timer)
+{
+       struct elem init = {};
+
+       bpf_map_update_elem(map, key, &init, BPF_ANY);
+       __sync_fetch_and_add(&update_hits, 1);
+       return 0;
+}
+
+/* Callback that cancels itself using async cancel */
+static int cancel_self_callback(void *map, int *key, struct bpf_timer *timer)
+{
+       bpf_timer_cancel_async(timer);
+       __sync_fetch_and_add(&cancel_hits, 1);
+       return 0;
+}
+
+enum test_mode {
+       TEST_RACE_SYNC,
+       TEST_RACE_ASYNC,
+       TEST_UPDATE,
+       TEST_CANCEL,
+};
+
+static __always_inline int test_common(enum test_mode mode)
 {
        struct bpf_timer *timer;
-       int err, race_key = 0;
        struct elem init;
+       int ret, key = 0;
 
        __builtin_memset(&init, 0, sizeof(struct elem));
-       bpf_map_update_elem(&race_array, &race_key, &init, BPF_ANY);
 
-       timer = bpf_map_lookup_elem(&race_array, &race_key);
+       bpf_map_update_elem(&race_array, &key, &init, BPF_ANY);
+       timer = bpf_map_lookup_elem(&race_array, &key);
        if (!timer)
-               return 1;
+               return 0;
+
+       ret = bpf_timer_init(timer, &race_array, CLOCK_MONOTONIC);
+       if (ret && ret != -EBUSY)
+               return 0;
 
-       err = bpf_timer_init(timer, &race_array, CLOCK_MONOTONIC);
-       if (err && err != -EBUSY)
-               return 1;
+       if (mode == TEST_RACE_SYNC || mode == TEST_RACE_ASYNC)
+               bpf_timer_set_callback(timer, race_timer_callback);
+       else if (mode == TEST_UPDATE)
+               bpf_timer_set_callback(timer, update_self_callback);
+       else
+               bpf_timer_set_callback(timer, cancel_self_callback);
 
-       bpf_timer_set_callback(timer, race_timer_callback);
        bpf_timer_start(timer, 0, 0);
-       if (async_cancel)
+
+       if (mode == TEST_RACE_ASYNC)
                bpf_timer_cancel_async(timer);
-       else
+       else if (mode == TEST_RACE_SYNC)
                bpf_timer_cancel(timer);
 
        return 0;
 }
+
+SEC("syscall")
+int race(void *ctx)
+{
+       return test_common(async_cancel ? TEST_RACE_ASYNC : TEST_RACE_SYNC);
+}
+
+SEC("perf_event")
+int nmi_race(void *ctx)
+{
+       __sync_fetch_and_add(&test_hits, 1);
+       return test_common(TEST_RACE_ASYNC);
+}
+
+SEC("perf_event")
+int nmi_update(void *ctx)
+{
+       __sync_fetch_and_add(&test_hits, 1);
+       return test_common(TEST_UPDATE);
+}
+
+SEC("perf_event")
+int nmi_cancel(void *ctx)
+{
+       __sync_fetch_and_add(&test_hits, 1);
+       return test_common(TEST_CANCEL);
+}