bpf: test_run: Fix timer mode initialization to NO_MIGRATE mode
Subsystems (3)
PATCH 1/1 bpf: test_run: Fix timer mode initialization to NO_MIGRATE mode
By default, the timer mode is being initialized to NO_PREEMPT. This disables preemption and forces execution in atomic context. This can cause issue with PREEMPT_RT when calling spin_lock_bh() due to sleeping nature of the lock.
...
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.17preempt_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 Call Trace: <TASK> dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120 __might_resched+0x44b/0x5d0 kernel/sched/core.c:8957 __rt_spin_lock kernel/locking/spinlock_rt.c:48 [inline] rt_spin_lock+0xc7/0x2c0 kernel/locking/spinlock_rt.c:57 spin_lock_bh include/linux/spinlock_rt.h:88 [inline] __sock_map_delete net/core/sock_map.c:421 [inline] sock_map_delete_elem+0xb7/0x170 net/core/sock_map.c:452 bpf_prog_2c29ac5cdc6b1842+0x43/0x4b bpf_dispatcher_nop_func include/linux/bpf.h:1332 [inline]
...
Change initialization to NO_MIGRATE mode to prevent this.
--- net/bpf/test_run.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/bpf/test_run.c +2 -2
@@ -1368,7 +1368,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 = { NO_MIGRATE };u32 size = kattr->test.data_size_in;struct bpf_flow_dissector ctx = {};u32 repeat = kattr->test.repeat;
@@ -1436,7 +1436,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,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 = {NO_MIGRATE };struct bpf_prog_array *progs =NULL ;struct bpf_sk_lookup_kern ctx = {};u32 repeat = kattr->test.repeat;
Signature · 1 line
2.50.1
Quoted 4 lines · Sahil Chandna
By default, the timer mode is being initialized to
NO_PREEMPT. This disables preemption and forces execution in atomic context. This can cause issue withPREEMPT_RTwhen callingspin_lock_bh()due to sleeping nature of the lock.
What kind of testing did you do for this?
Yonghong Song
reviewerQuoted 38 lines · Sahil Chandna
By default, the timer mode is being initialized to
NO_PREEMPT. This disables preemption and forces execution in atomic context. This can cause issue withPREEMPT_RTwhen callingspin_lock_bh()due to sleeping nature of the lock.
...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.17preempt_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 Call Trace: <TASK> dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120 __might_resched+0x44b/0x5d0 kernel/sched/core.c:8957 __rt_spin_lock kernel/locking/spinlock_rt.c:48 [inline] rt_spin_lock+0xc7/0x2c0 kernel/locking/spinlock_rt.c:57 spin_lock_bh include/linux/spinlock_rt.h:88 [inline] __sock_map_delete net/core/sock_map.c:421 [inline] sock_map_delete_elem+0xb7/0x170 net/core/sock_map.c:452 bpf_prog_2c29ac5cdc6b1842+0x43/0x4b bpf_dispatcher_nop_func include/linux/bpf.h:1332 [inline]...
Change initialization toNO_MIGRATEmode to prevent this.Reported-by [email protected]Tested-by [email protected]Signed-off-by Sahil Chandna <[email protected]>--- net/bpf/test_run.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/bpf/test_run.c +0
-0
net/bpf/test_run.c int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, · line 1371
Quoted 5 lines
net/bpf/test_run.c +1
-1@@ -1368,7 +1368,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 = { NO_MIGRATE };
I checked the original reproducer. And changing from NO_PREEMPT to NO_MIGRATE is needed only with CONFIG_PREEMPT_RT enabled.
net/bpf/test_run.c int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, · line 1439
Quoted 9 lines
net/bpf/test_run.c +1
-1u32 size = kattr->test.data_size_in;struct bpf_flow_dissector ctx = {};u32 repeat = kattr->test.repeat;@@ -1436,7 +1436,7 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,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 = {NO_MIGRATE };
This change is not needed for the above particular BUG.
net/bpf/test_run.c int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, · line 1442
Quoted 3 lines
net/bpf/test_run.c +0
-0struct bpf_prog_array *progs =NULL ;struct bpf_sk_lookup_kern ctx = {};u32 repeat = kattr->test.repeat;
Checking the git history. I found the earliest NO_PREEMPT usage can be traced back to this commit:
commit a439184d515fbf4805f57d11fa5dfd4524d2c0eb
Author: Stanislav Fomichev <[email protected]> Date: Tue Feb 19 10:54:17 2019 -0800
bpf/test_run: fix unkillable BPF_PROG_TEST_RUN for flow dissector
At that time, migrate_disable/enable() are not used yet.
So I suspect that we can remove NO_PREEMPT/NO_MIGRATE in test_run.c and use migrate_disable()/migrate_enable() universally.