Skip to content

Commit 7c33e97

Browse files
Schandna-gitinitAlexei Starovoitov
authored andcommitted
bpf: Do not disable preemption in bpf_test_run().
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>
1 parent f6fddc6 commit 7c33e97

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

net/bpf/test_run.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,22 @@
2929
#include <trace/events/bpf_test_run.h>
3030

3131
struct bpf_test_timer {
32-
enum { NO_PREEMPT, NO_MIGRATE } mode;
3332
u32 i;
3433
u64 time_start, time_spent;
3534
};
3635

3736
static void bpf_test_timer_enter(struct bpf_test_timer *t)
3837
__acquires(rcu)
3938
{
40-
rcu_read_lock();
41-
if (t->mode == NO_PREEMPT)
42-
preempt_disable();
43-
else
44-
migrate_disable();
45-
39+
rcu_read_lock_dont_migrate();
4640
t->time_start = ktime_get_ns();
4741
}
4842

4943
static void bpf_test_timer_leave(struct bpf_test_timer *t)
5044
__releases(rcu)
5145
{
5246
t->time_start = 0;
53-
54-
if (t->mode == NO_PREEMPT)
55-
preempt_enable();
56-
else
57-
migrate_enable();
58-
rcu_read_unlock();
47+
rcu_read_unlock_migrate();
5948
}
6049

6150
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,
374363

375364
{
376365
struct xdp_test_data xdp = { .batch_size = batch_size };
377-
struct bpf_test_timer t = { .mode = NO_MIGRATE };
366+
struct bpf_test_timer t = {};
378367
int ret;
379368

380369
if (!repeat)
@@ -404,7 +393,7 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
404393
struct bpf_prog_array_item item = {.prog = prog};
405394
struct bpf_run_ctx *old_ctx;
406395
struct bpf_cg_run_ctx run_ctx;
407-
struct bpf_test_timer t = { NO_MIGRATE };
396+
struct bpf_test_timer t = {};
408397
enum bpf_cgroup_storage_type stype;
409398
int ret;
410399

@@ -1377,7 +1366,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
13771366
const union bpf_attr *kattr,
13781367
union bpf_attr __user *uattr)
13791368
{
1380-
struct bpf_test_timer t = { NO_PREEMPT };
1369+
struct bpf_test_timer t = {};
13811370
u32 size = kattr->test.data_size_in;
13821371
struct bpf_flow_dissector ctx = {};
13831372
u32 repeat = kattr->test.repeat;
@@ -1445,7 +1434,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
14451434
int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
14461435
union bpf_attr __user *uattr)
14471436
{
1448-
struct bpf_test_timer t = { NO_PREEMPT };
1437+
struct bpf_test_timer t = {};
14491438
struct bpf_prog_array *progs = NULL;
14501439
struct bpf_sk_lookup_kern ctx = {};
14511440
u32 repeat = kattr->test.repeat;

0 commit comments

Comments
 (0)