[PATCH bpf-next 4/4] selftests/bpf: Expect marked programs to drop B on PREEMPT_RT

From: Mike Bazov via B4 Relay

Date: Wed Dec 02 2026 - 08:16:12 EST


From: Mike Bazov <mike.bazov@xxxxxxxxx>

On PREEMPT_RT a program with per-CPU state stays preemptible, and its
per-CPU counter refuses B's run while A is preempted inside it. Expect B
to be dropped in the subtests of such programs there. In
two_dispatchers, B's own run of the first dispatcher is dropped too, and
B reaches the handler through the second one, which has a counter of its
own, and runs it inside A.

Signed-off-by: Mike Bazov <mike.bazov@xxxxxxxxx>
---
.../selftests/bpf/prog_tests/trampoline_preempt.c | 35 ++++++++++++++++++----
1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
index 8708239fed5..0234e5dec51 100644
--- a/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_preempt.c
@@ -5,6 +5,7 @@
#include <signal.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
+#include <sys/utsname.h>
#include <unistd.h>
#include <test_progs.h>
#include "trampoline_preempt.skel.h"
@@ -31,8 +32,21 @@ enum { NO_PERCPU, PERCPU_MAP, PERCPU_VAR, THIS_CPU_PTR, CPU_ID };
enum expect {
B_RUNS, /* B runs the program while A is preempted inside it */
B_AFTER, /* B runs the program after A has left it */
+ B_DROPPED, /* B can't run the program while A is inside it */
};

+/*
+ * On PREEMPT_RT a program with per-CPU state stays preemptible, and a per-CPU
+ * counter refuses another run of it: B, which would run it after A, is dropped.
+ */
+static bool preempt_rt(void)
+{
+ struct utsname u;
+
+ /* The version of a PREEMPT_RT kernel says so, see init/Makefile */
+ return !uname(&u) && strstr(u.version, "PREEMPT_RT");
+}
+
/* What threads A and B share */
struct ctx {
struct trampoline_preempt *skel;
@@ -162,10 +176,21 @@ static void run_subtest(const struct subtest *t)
{
struct bpf_prog_info info = {};
__u32 info_len = sizeof(info), zero = 0;
+ enum expect expect = t->expect;
struct ctx c = {};
sigset_t oldset;
pthread_t ta, tb;
- int err, fd;
+ int err, fd, misses = 0;
+
+ /*
+ * B's own run of A's program is dropped. In two_dispatchers B still reaches
+ * the handler through the other dispatcher, which has a counter of its own,
+ * and runs it inside A.
+ */
+ if (expect == B_AFTER && preempt_rt()) {
+ expect = t->handler ? B_RUNS : B_DROPPED;
+ misses = NR_ROUNDS;
+ }

c.skel = load_progs(t->prog, t->prog_b, t->handler, t->percpu);
if (!c.skel)
@@ -203,21 +228,21 @@ static void run_subtest(const struct subtest *t)
ASSERT_OK(c.err_a, "worker_a");
ASSERT_OK(c.err_b, "worker_b");
ASSERT_EQ(c.skel->bss->sig_err, 0, "sig_err");
- if (t->expect == B_RUNS && !c.overlaps) {
+ if (expect == B_RUNS && !c.overlaps) {
/* B never preempted A inside the program, so nothing was tested */
test__skip();
goto out;
}
- ASSERT_EQ(c.overlaps, t->expect == B_RUNS ? NR_ROUNDS : 0, "overlaps");
+ ASSERT_EQ(c.overlaps, expect == B_AFTER ? 0 : NR_ROUNDS, "overlaps");
ASSERT_EQ(c.skel->bss->runs_a, NR_ROUNDS, "runs_a");
- ASSERT_EQ(c.skel->bss->runs_b, NR_ROUNDS, "runs_b");
+ ASSERT_EQ(c.skel->bss->runs_b, expect == B_DROPPED ? 0 : NR_ROUNDS, "runs_b");
if (t->handler)
ASSERT_EQ(c.skel->bss->stack_corrupt, 0, "stack_corrupt");
fd = bpf_program__fd(bpf_object__find_program_by_name(c.skel->obj, t->prog));
err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
if (!ASSERT_OK(err, "prog_info"))
goto out;
- ASSERT_EQ(info.recursion_misses, 0, "recursion_misses");
+ ASSERT_EQ(info.recursion_misses, misses, "recursion_misses");
out:
trampoline_preempt__destroy(c.skel);
}

--
2.43.0