bpf/test_run: fix unkillable BPF_PROG_TEST_RUN for flow dissector
Subsystems (3)
PATCH 1/1 bpf/test_run: fix unkillable BPF_PROG_TEST_RUN for flow dissector
Syzbot found out that running BPF_PROG_TEST_RUN with repeat=0xffffffff makes process unkillable. The problem is that when CONFIG_PREEMPT is enabled, we never see need_resched() return true. This is due to the fact that preempt_enable() (which we do in bpf_test_run_one on each iteration) now handles resched if it's needed.
Let's disable preemption for the whole run, not per test. In this case we can properly see whether resched is needed.
Let's also properly return -EINTR to the userspace in case of a signal interrupt.
This is a follow up for a recently fixed issue in bpf_test_run, see commit df1a2cb7c74b ("bpf/test_run: fix unkillable BPF_PROG_TEST_RUN").
--- net/bpf/test_run.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
net/bpf/test_run.c +20 -6
@@ -293,31 +293,45 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,if (!repeat) repeat =1 ; +rcu_read_lock (); +preempt_disable (); time_start =ktime_get_ns ();for (i =0 ; i < repeat; i++) { -preempt_disable (); -rcu_read_lock (); retval =__skb_flow_bpf_dissect (prog, skb, &flow_keys_dissector, &flow_keys); -rcu_read_unlock (); -preempt_enable (); + +if (signal_pending (current)) { +preempt_enable (); +rcu_read_unlock (); + + ret = -EINTR ; + goto out; + }if (need_resched ()) { -if (signal_pending (current)) -break ; time_spent +=ktime_get_ns () - time_start; +preempt_enable (); +rcu_read_unlock (); +cond_resched (); + +rcu_read_lock (); +preempt_disable (); time_start =ktime_get_ns (); } } time_spent +=ktime_get_ns () - time_start; +preempt_enable (); +rcu_read_unlock (); +do_div (time_spent, repeat); duration = time_spent >U32_MAX ?U32_MAX : (u32 )time_spent; ret =bpf_test_finish (kattr, uattr, &flow_keys,sizeof (flow_keys), retval, duration); +out:kfree_skb (skb);kfree (sk);return ret;
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Quoted 11 lines · Stanislav Fomichev
Syzbot found out that running
BPF_PROG_TEST_RUNwith repeat=0xffffffff makes process unkillable. The problem is that whenCONFIG_PREEMPTis enabled, we never seeneed_resched()return true. This is due to the fact thatpreempt_enable()(which we do inbpf_test_run_oneon each iteration) now handles resched if it's needed.Let's disable preemption for the whole run, not per test. In this case we can properly see whether resched is needed.
Let's also properly return-EINTRto the userspace in case of a signal interrupt.
Quoted 6 lines
This is a follow up for a recently fixed issue in
bpf_test_run, see commitdf1a2cb7c74b("bpf/test_run: fix unkillableBPF_PROG_TEST_RUN").Reported-by syzbot <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
Applied, thanks! Would be nice to consolidate at least some of these bits so it's not duplicated, perhaps __always_inline might help for function args in avoiding indirect calls (iirc, perf's rb handling uses this heavily).
Quoted 61 lines
--- net/bpf/test_run.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)net/bpf/test_run.c +20
-6@@ -293,31 +293,45 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,if (!repeat) repeat =1 ; +rcu_read_lock (); +preempt_disable (); time_start =ktime_get_ns ();for (i =0 ; i < repeat; i++) { -preempt_disable (); -rcu_read_lock (); retval =__skb_flow_bpf_dissect (prog, skb, &flow_keys_dissector, &flow_keys); -rcu_read_unlock (); -preempt_enable (); + +if (signal_pending (current)) { +preempt_enable (); +rcu_read_unlock (); + + ret = -EINTR ; + goto out; + }if (need_resched ()) { -if (signal_pending (current)) -break ; time_spent +=ktime_get_ns () - time_start; +preempt_enable (); +rcu_read_unlock (); +cond_resched (); + +rcu_read_lock (); +preempt_disable (); time_start =ktime_get_ns (); } } time_spent +=ktime_get_ns () - time_start; +preempt_enable (); +rcu_read_unlock (); +do_div (time_spent, repeat); duration = time_spent >U32_MAX ?U32_MAX : (u32 )time_spent; ret =bpf_test_finish (kattr, uattr, &flow_keys,sizeof (flow_keys), retval, duration); +out:kfree_skb (skb);kfree (sk);return ret;