ipv6: fix a BUG in rt6_get_pcpu_route() under PREEMPT_RT
Subsystems (4)
PATCH v3 1/1 ipv6: fix a BUG in rt6_get_pcpu_route() under PREEMPT_RT
On PREEMPT_RT kernels, after rt6_get_pcpu_route() returns NULL, the current task can be preempted. Another task running on the same CPU may then execute rt6_make_pcpu_route() and successfully install a pcpu_rt entry. When the first task resumes execution, its cmpxchg() in rt6_make_pcpu_route() will fail because rt6i_pcpu is no longer NULL, triggering the BUG_ON(prev). It's easy to reproduce it by adding mdelay() after rt6_get_pcpu_route().
Using preempt_disable/enable is not appropriate here because ip6_rt_pcpu_alloc() may sleep.
Fix this by handling the cmpxchg() failure gracefully on PREEMPT_RT:
free our allocation and return the existing pcpu_rt installed by another task. The BUG_ON is retained for non-PREEMPT_RT kernels where such races should not occur.
---
v2 -> v3: Use correct Fixes tag.
https://lore.kernel.org/netdev/[email protected]/ v1 -> v2: Drop migrate_{disable, enabled} suggested by Steven and Paolo. https://lore.kernel.org/all/[email protected]/T/ --- net/ipv6/route.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
net/ipv6/route.c +12 -1
@@ -1470,7 +1470,18 @@ static struct rt6_info *rt6_make_pcpu_route(struct net *net, p =this_cpu_ptr (res->nh->rt6i_pcpu); prev =cmpxchg (p,NULL , pcpu_rt); -BUG_ON (prev); +if (unlikely (prev)) { +/* +* Another task on this CPU already installed a pcpu_rt. +* This can happen on PREEMPT_RT where preemption is possible. +* Free our allocation and return the existing one. +*/ +BUG_ON (!IS_ENABLED (CONFIG_PREEMPT_RT )); + +dst_dev_put (&pcpu_rt->dst); +dst_release (&pcpu_rt->dst); +return prev; + }if (res->f6i->fib6_destroying) {struct fib6_info *from;
Signature · 1 line
2.43.0