]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Do not disable preemption in bpf_test_run().
authorSahil Chandna <chandna.sahil@gmail.com>
Tue, 14 Oct 2025 18:56:35 +0000 (00:26 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 17 Oct 2025 18:29:35 +0000 (11:29 -0700)
The timer mode is initialized to NO_PREEMPT mode by default,
this disables preemption and force execution in atomic context
causing issue on PREEMPT_RT configurations when invoking
spin_lock_bh(), leading to the following warning:

BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 6107, name: syz.0.17
preempt_count: 1, expected: 0
RCU nest depth: 1, expected: 1
Preemption disabled at:
[<ffffffff891fce58>] bpf_test_timer_enter+0xf8/0x140 net/bpf/test_run.c:42

Fix this, by removing NO_PREEMPT/NO_MIGRATE mode check.
Also, the test timer context no longer needs explicit calls to
migrate_disable()/migrate_enable() with rcu_read_lock()/rcu_read_unlock().
Use helpers rcu_read_lock_dont_migrate() and rcu_read_unlock_migrate()
instead.

Reported-by: syzbot+1f1fbecb9413cdbfbef8@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=1f1fbecb9413cdbfbef8
Suggested-by: Yonghong Song <yonghong.song@linux.dev>
Suggested-by: Menglong Dong <menglong.dong@linux.dev>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Tested-by: syzbot+1f1fbecb9413cdbfbef8@syzkaller.appspotmail.com
Co-developed-by: Brahmajit Das <listout@listout.xyz>
Signed-off-by: Brahmajit Das <listout@listout.xyz>
Signed-off-by: Sahil Chandna <chandna.sahil@gmail.com>
Link: https://lore.kernel.org/r/20251014185635.10300-1-chandna.sahil@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
net/bpf/test_run.c

index 1782e83de2cb83171e7b39cdc4131391869b757a..8b7d0b90fea76b646131e7b07ec3817cd87bda59 100644 (file)
@@ -29,7 +29,6 @@
 #include <trace/events/bpf_test_run.h>
 
 struct bpf_test_timer {
-       enum { NO_PREEMPT, NO_MIGRATE } mode;
        u32 i;
        u64 time_start, time_spent;
 };
@@ -37,12 +36,7 @@ struct bpf_test_timer {
 static void bpf_test_timer_enter(struct bpf_test_timer *t)
        __acquires(rcu)
 {
-       rcu_read_lock();
-       if (t->mode == NO_PREEMPT)
-               preempt_disable();
-       else
-               migrate_disable();
-
+       rcu_read_lock_dont_migrate();
        t->time_start = ktime_get_ns();
 }
 
@@ -50,12 +44,7 @@ static void bpf_test_timer_leave(struct bpf_test_timer *t)
        __releases(rcu)
 {
        t->time_start = 0;
-
-       if (t->mode == NO_PREEMPT)
-               preempt_enable();
-       else
-               migrate_enable();
-       rcu_read_unlock();
+       rcu_read_unlock_migrate();
 }
 
 static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
@@ -374,7 +363,7 @@ static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
 
 {
        struct xdp_test_data xdp = { .batch_size = batch_size };
-       struct bpf_test_timer t = { .mode = NO_MIGRATE };
+       struct bpf_test_timer t = {};
        int ret;
 
        if (!repeat)
@@ -404,7 +393,7 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
        struct bpf_prog_array_item item = {.prog = prog};
        struct bpf_run_ctx *old_ctx;
        struct bpf_cg_run_ctx run_ctx;
-       struct bpf_test_timer t = { NO_MIGRATE };
+       struct bpf_test_timer t = {};
        enum bpf_cgroup_storage_type stype;
        int ret;
 
@@ -1377,7 +1366,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
                                     const union bpf_attr *kattr,
                                     union bpf_attr __user *uattr)
 {
-       struct bpf_test_timer t = { NO_PREEMPT };
+       struct bpf_test_timer t = {};
        u32 size = kattr->test.data_size_in;
        struct bpf_flow_dissector ctx = {};
        u32 repeat = kattr->test.repeat;
@@ -1445,7 +1434,7 @@ out:
 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
                                union bpf_attr __user *uattr)
 {
-       struct bpf_test_timer t = { NO_PREEMPT };
+       struct bpf_test_timer t = {};
        struct bpf_prog_array *progs = NULL;
        struct bpf_sk_lookup_kern ctx = {};
        u32 repeat = kattr->test.repeat;