ipv6: fix a BUG in rt6_get_pcpu_route() under PREEMPT_RT
Subsystems (4)
PATCH v4 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 replaced by WARN_ON_ONCE for non-PREEMPT_RT kernels where such races should not occur.
---
v2 -> v4: Use correct Fixes tag; Replace BUG_ON by WARN_ON_ONCE.
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. +*/ +WARN_ON_ONCE (!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
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 9 lines · On Tue, 23 Dec 2025 13:14:12 +0800 you wrote:
On
PREEMPT_RTkernels, afterrt6_get_pcpu_route()returns NULL, the current task can be preempted. Another task running on the same CPU may then executert6_make_pcpu_route()and successfully install apcpu_rtentry. When the first task resumes execution, itscmpxchg()inrt6_make_pcpu_route()will fail becausert6i_pcpuis no longer NULL, triggering theBUG_ON(prev). It's easy to reproduce it by addingmdelay()afterrt6_get_pcpu_route().[...]
Here is the summary with links:
- [net,v4] ipv6: fix a BUG in rt6_get_pcpu_route() under PREEMPT_RT
https://git.kernel.org/netdev/net/c/1adaea51c61b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Sebastian Andrzej Siewior
maintainerQuoted 16 lines · Jiayuan Chen
On
PREEMPT_RTkernels, afterrt6_get_pcpu_route()returns NULL, the current task can be preempted. Another task running on the same CPU may then executert6_make_pcpu_route()and successfully install apcpu_rtentry. When the first task resumes execution, itscmpxchg()inrt6_make_pcpu_route()will fail becausert6i_pcpuis no longer NULL, triggering theBUG_ON(prev). It's easy to reproduce it by addingmdelay()afterrt6_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 onPREEMPT_RT:
free our allocation and return the existingpcpu_rtinstalled by another task. TheBUG_ONis replaced byWARN_ON_ONCEfor non-PREEMPT_RT kernels where such races should not occur.
Quoted 5 lines
Fixes d2d6422f8bd1 ("x86: Allow to enable PREEMPT_RT.")Reported-by [email protected]Signed-off-by Jiayuan Chen <[email protected]>
This is upstream as commit
1adaea51c61b5 ("ipv6: fix a BUG in rt6_get_pcpu_route() under PREEMPT_RT")
and should be backported down to v6.12 due to the fixes tag. RT wise it should be broken since its introduction so if the stable team could take it down to v5.10-rt, that would be nice.
Sebastian
Hi Sebastian!
Quoted 23 lines · Sebastian Andrzej Siewior
Quoted 16 lines · Jiayuan Chen
On
PREEMPT_RTkernels, afterrt6_get_pcpu_route()returns NULL, the current task can be preempted. Another task running on the same CPU may then executert6_make_pcpu_route()and successfully install apcpu_rtentry. When the first task resumes execution, itscmpxchg()inrt6_make_pcpu_route()will fail becausert6i_pcpuis no longer NULL, triggering theBUG_ON(prev). It's easy to reproduce it by addingmdelay()afterrt6_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 onPREEMPT_RT:
free our allocation and return the existingpcpu_rtinstalled by another task. TheBUG_ONis replaced byWARN_ON_ONCEfor non-PREEMPT_RT kernels where such races should not occur.Quoted 5 lines
Fixes d2d6422f8bd1 ("x86: Allow to enable PREEMPT_RT.")Reported-by [email protected]Signed-off-by Jiayuan Chen <[email protected]>
Quoted 6 lines
This is upstream as commit
1adaea51c61b5("ipv6: fix a BUG inrt6_get_pcpu_route()underPREEMPT_RT")and should be backported down to v6.12 due to the fixes tag. RT wise it should be broken since its introduction so if the stable team could take it down to v5.10-rt, that would be nice.
Just to clarify, do you mean us, the stable RT maintainers, or Greg-KH and his merry band?
Best,
Luis
Sebastian Andrzej Siewior
maintainerQuoted 1 line · Luis Claudio R. Goncalves
Hi Sebastian!
Hi,
Quoted 9 lines
Quoted 6 lines
This is upstream as commit
1adaea51c61b5("ipv6: fix a BUG inrt6_get_pcpu_route()underPREEMPT_RT")and should be backported down to v6.12 due to the fixes tag. RT wise it should be broken since its introduction so if the stable team could take it down to v5.10-rt, that would be nice.
Just to clarify, do you mean us, the stable RT maintainers, or Greg-KH and his merry band?
The RT maintainers. I don't think Greg will apply to kernels prio the fixes commit also RT is not available in upstream so it makes no difference. You could wait a month and see ;)
This was there since its introduction in 4.x and never triggered.
Quoted 2 lines
Best,
Luis
Sebastian