ipv6: Prevent rt6_insert_exception() for dying fib6_info.
Subsystems (3)
PATCH 1/1 ipv6: Prevent rt6_insert_exception() for dying fib6_info.
Before the cited commit, fib6_nh_flush_exceptions() always set from->exception_bucket_flushed = 1 under rt6_exception_lock to prevent rt6_insert_exception() from inserting a new exception for a dying fib6_info.
The flag was replaced with the FIB6_EXCEPTION_BUCKET_FLUSHED bit stored in nh->rt6i_exception_bucket.
The problem is that now the bit is only set when the bucket is not NULL and fib6_nh_flush_exceptions() is called from fib6_nh_release() after fib6_ref has already reached zero.
If rt6_insert_exception() is called while the target fib6_info is being removed via fib6_purge_rt(), a new exception could be created successfully because rt6_flush_exceptions() no longer sets the bit.
This creates a reference cycle between the fib6_info and the exception route, leaking the fib6_info, its nexthop device, and all per-CPU routes in fib6_nh->rt6i_pcpu, which stalls netdev unregistration.
[ 34.680602] unregister_netdevice: waiting for gre6 to become free. Usage count = 68 [ 44.920675] unregister_netdevice: waiting for gre6 to become free. Usage count = 68 [ 55.176582] unregister_netdevice: waiting for gre6 to become free. Usage count = 68
Let's call fib6_drop_pcpu_from() before rt6_flush_exceptions(), to set fib6_destroying before rt6_exception_lock, and check f6i->fib6_destroying in rt6_insert_exception().
Note that FIB6_EXCEPTION_BUCKET_FLUSHED logic is dead and we can clean it up in net-next.
--- net/ipv6/ip6_fib.c | 2 +- net/ipv6/route.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
net/ipv6/ip6_fib.c +1 -1
@@ -1043,8 +1043,8 @@ static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,struct fib6_table *table = rt->fib6_table;/* Flush all cached dst in exception table */ -rt6_flush_exceptions (rt);fib6_drop_pcpu_from (rt); +rt6_flush_exceptions (rt);if (rt->nh) {spin_lock (&rt->nh->lock);
net/ipv6/route.c +5 -0
@@ -1729,6 +1729,11 @@ static int rt6_insert_exception(struct rt6_info *nrt,spin_lock_bh (&rt6_exception_lock); +if (f6i->fib6_destroying) { + err = -ENOENT ; + goto out; + } + bucket =rcu_dereference_protected (nh->rt6i_exception_bucket,lockdep_is_held (&rt6_exception_lock));if (!bucket) {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Quoted 30 lines · Kuniyuki Iwashima
Before the cited commit,
fib6_nh_flush_exceptions()always setfrom->exception_bucket_flushed= 1 underrt6_exception_lockto preventrt6_insert_exception()from inserting a new exception for a dyingfib6_info.The flag was replaced with the
FIB6_EXCEPTION_BUCKET_FLUSHEDbit stored innh->rt6i_exception_bucket.The problem is that now the bit is only set when the bucket is not NULL and
fib6_nh_flush_exceptions()is called fromfib6_nh_release()afterfib6_refhas already reached zero.If
rt6_insert_exception()is called while the targetfib6_infois being removed viafib6_purge_rt(), a new exception could be created successfully becausert6_flush_exceptions()no longer sets the bit.This creates a reference cycle between the
fib6_infoand the exception route, leaking thefib6_info, its nexthop device, and all per-CPU routes infib6_nh->rt6i_pcpu, which stalls netdev unregistration.[ 34.680602] unregister_netdevice: waiting for gre6 to become free. Usage count = 68 [ 44.920675] unregister_netdevice: waiting for gre6 to become free. Usage count = 68 [ 55.176582] unregister_netdevice: waiting for gre6 to become free. Usage count = 68Let's call
fib6_drop_pcpu_from()beforert6_flush_exceptions(), to setfib6_destroyingbeforert6_exception_lock, and checkf6i->fib6_destroyinginrt6_insert_exception().
Quoted 5 lines
Note that
FIB6_EXCEPTION_BUCKET_FLUSHEDlogic is dead and we can clean it up in net-next.Fixes cc5c073a693f ("ipv6: Move exception bucket to fib6_nh")Signed-off-by Kuniyuki Iwashima <[email protected]>
Looks OK, but if you need another version (or in net-next when you remove FIB6_EXCEPTION_BUCKET_FLUSHED), then also update the comment in fib6_drop_pcpu_from() to make it clear that 'fib6_destroying' also prevents the addition of exception routes and not only per-CPU ones.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The new guard in rt6_insert_exception() (net/ipv6/route.c:1732) trusts f6i->fib6_destroying, but that bit is…
--
ipv6: Prevent rt6_insert_exception() for dying fib6_info.
This reorders fib6_purge_rt() so that fib6_drop_pcpu_from() runs before rt6_flush_exceptions(), and adds a f6i->fib6_destroying check in rt6_insert_exception() so a dying fib6_info can no longer gain a new exception route. The goal is to stop the fib6_info<->exception reference cycle that stalls netdev unregistration.
Quoted 19 lines
net/ipv6/ip6_fib.c +1
-1@@ -1043,8 +1043,8 @@ static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,struct fib6_table *table = rt->fib6_table;/* Flush all cached dst in exception table */ -rt6_flush_exceptions (rt);fib6_drop_pcpu_from (rt); +rt6_flush_exceptions (rt);if (rt->nh) {spin_lock (&rt->nh->lock);net/ipv6/route.c +0
-0
net/ipv6/route.c static int rt6_insert_exception(struct rt6_info *nrt, · line 1736
Quoted 8 lines
net/ipv6/route.c +5
-0@@ -1729,6 +1729,11 @@ static int rt6_insert_exception(struct rt6_info *nrt,spin_lock_bh (&rt6_exception_lock); +if (f6i->fib6_destroying) { + err = -ENOENT ; + goto out; + } +
[Severity: Medium]
Can this check observe fib6_destroying == 0 after fib6_purge_rt() has already set it?
fib6_destroying shares one u8 storage unit with three other bits in struct fib6_info:
include/net/ip6_fib.h:
u8 should_flush:1, dst_nocount:1, dst_nopolicy:1, fib6_destroying:1, unused:4;
Adjacent bitfields are a single memory location, so every assignment to any of them is a non-atomic read-modify-write of the whole byte.
The setter in fib6_drop_pcpu_from() runs with only the table's tb6_lock held:
net/ipv6/ip6_fib.c:fib6_drop_pcpu_from() {
f6i->fib6_destroying = 1;
mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */
...
}One peer writer of the same byte is addrconf_disable_policy_idev(), which holds only rtnl_net_lock plus idev->lock and ifa->lock:
net/ipv6/addrconf.c:addrconf_disable_policy_idev() {
...
rcu_read_lock();
ifa->rt->dst_nopolicy = val ? true : false;
...
}Is there any lock common to both paths? inet6_rtm_delroute() is registered with RTNL_FLAG_DOIT_UNLOCKED, and ip6_route_del()->__ip6_del_rt()->fib6_del()->fib6_purge_rt() takes only rcu_read_lock() and tb6_lock, so a write to net.ipv6.conf.<dev>.disable_policy on a host route (ifa->rt) does not appear to be serialized against the deletion path.
If the disable_policy writer loaded the byte before fib6_destroying was set and stores its copy back afterwards, the bit is cleared again. A third thread in ip6_rt_update_pmtu() or rt6_do_redirect() that still holds the pre-removal fib6_info would then read fib6_destroying == 0 here and insert an exception after the final rt6_flush_exceptions(), which recreates the fib6_info<->exception cycle and the
unregister_netdevice: waiting for gre6 to become free
stall described in the commit message. The same lost update also lets rt6_make_pcpu_route() re-add per-CPU routes after __fib6_drop_pcpu_from() has run.
Note the load here is a plain bitfield read with no READ_ONCE(), and the store side is a plain RMW, so this is also visible to KCSAN as a data race. rt6_multipath_flush() writes should_flush under tb6_lock, so dst_nopolicy looks like the one unsynchronized peer.
Would it make sense to give this flag its own storage accessed with WRITE_ONCE()/READ_ONCE() (or to serialize all writers of the containing byte), and to document which lock protects it?
The comment above the store in fib6_drop_pcpu_from() still mentions only rt6_make_pcpu_route() and the cmpxchg() as the counterpart; could it also mention the new rt6_exception_lock reader added here?
Quoted 3 lines
net/ipv6/route.c +0
-0bucket =rcu_dereference_protected (nh->rt6i_exception_bucket,lockdep_is_held (&rt6_exception_lock));if (!bucket) {
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260918082209.2853582-1-kuniyu%40google.com
Ido Schimmel
maintainerQuoted 102 lines · On Mon, Sep 21, 2026 at 08:24:07AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The new guard in
rt6_insert_exception()(net/ipv6/route.c:1732) trustsf6i->fib6_destroying, but that bit is…--
ipv6: Prevent
rt6_insert_exception()for dyingfib6_info.This reorders
fib6_purge_rt()so thatfib6_drop_pcpu_from()runs beforert6_flush_exceptions(), and adds af6i->fib6_destroyingcheck inrt6_insert_exception()so a dyingfib6_infocan no longer gain a new exception route. The goal is to stop the fib6_info<->exception reference cycle that stalls netdev unregistration.Quoted 19 lines
net/ipv6/ip6_fib.c +1
-1@@ -1043,8 +1043,8 @@ static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,struct fib6_table *table = rt->fib6_table;/* Flush all cached dst in exception table */ -rt6_flush_exceptions (rt);fib6_drop_pcpu_from (rt); +rt6_flush_exceptions (rt);if (rt->nh) {spin_lock (&rt->nh->lock);net/ipv6/route.c +0
-0net/ipv6/route.c static int rt6_insert_exception(struct rt6_info *nrt, · line 1736
Quoted 8 lines
net/ipv6/route.c +5
-0@@ -1729,6 +1729,11 @@ static int rt6_insert_exception(struct rt6_info *nrt,spin_lock_bh (&rt6_exception_lock); +if (f6i->fib6_destroying) { + err = -ENOENT ; + goto out; + } +[Severity: Medium]
Can this check observefib6_destroying== 0 afterfib6_purge_rt()has already set it?
fib6_destroyingshares one u8 storage unit with three other bits in structfib6_info:
include/net/ip6_fib.h:u8 should_flush:1, dst_nocount:1, dst_nopolicy:1, fib6_destroying:1, unused:4;Adjacent bitfields are a single memory location, so every assignment to any of them is a non-atomic read-modify-write of the whole byte.
The setter in
fib6_drop_pcpu_from()runs with only the table'stb6_lockheld:net/ipv6/ip6_fib.c:fib6_drop_pcpu_from() { f6i->fib6_destroying = 1; mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */ ... }One peer writer of the same byte is
addrconf_disable_policy_idev(), which holds onlyrtnl_net_lockplusidev->lockandifa->lock:net/ipv6/addrconf.c:addrconf_disable_policy_idev() { ... rcu_read_lock(); ifa->rt->dst_nopolicy = val ? true : false; ... }Is there any lock common to both paths?
inet6_rtm_delroute()is registered withRTNL_FLAG_DOIT_UNLOCKED, and ip6_route_del()->__ip6_del_rt()->fib6_del()->fib6_purge_rt() takes onlyrcu_read_lock()andtb6_lock, so a write to net.ipv6.conf.<dev>.disable_policy on a host route (ifa->rt) does not appear to be serialized against the deletion path.If the
disable_policywriter loaded the byte beforefib6_destroyingwas set and stores its copy back afterwards, the bit is cleared again. A third thread inip6_rt_update_pmtu()orrt6_do_redirect()that still holds the pre-removalfib6_infowould then readfib6_destroying== 0 here and insert an exception after the finalrt6_flush_exceptions(), which recreates the fib6_info<->exception cycle and the
Quoted 5 lines
unregister_netdevice: waiting for gre6 to become freestall described in the commit message. The same lost update also lets
rt6_make_pcpu_route()re-add per-CPU routes after__fib6_drop_pcpu_from()has run.
It's a pre-existing issue and not a problem introduced by this patch. The likelihood of this happening is close to zero.
The current patch needs to be backported up to v5.3 and the issue being flagged here is only visible since v6.16 (bd11ff421d36), so either handle the issue in a follow-up or in patch 2/2 of v2.
Quoted 6 lines
Note the load here is a plain bitfield read with no
READ_ONCE(), and the store side is a plain RMW, so this is also visible to KCSAN as a data race.rt6_multipath_flush()writesshould_flushundertb6_lock, sodst_nopolicylooks like the one unsynchronized peer.
Quoted 7 lines
Would it make sense to give this flag its own storage accessed with WRITE_ONCE()/READ_ONCE() (or to serialize all writers of the containing byte), and to document which lock protects it?
The comment above the store in
fib6_drop_pcpu_from()still mentions onlyrt6_make_pcpu_route()and thecmpxchg()as the counterpart; could it also mention the newrt6_exception_lockreader added here?
I already mentioned this and it's minor.
Quoted 104 lines · Ido Schimmel
Quoted 102 lines · On Mon, Sep 21, 2026 at 08:24:07AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The new guard in
rt6_insert_exception()(net/ipv6/route.c:1732) trustsf6i->fib6_destroying, but that bit is…--
ipv6: Prevent
rt6_insert_exception()for dyingfib6_info.This reorders
fib6_purge_rt()so thatfib6_drop_pcpu_from()runs beforert6_flush_exceptions(), and adds af6i->fib6_destroyingcheck inrt6_insert_exception()so a dyingfib6_infocan no longer gain a new exception route. The goal is to stop the fib6_info<->exception reference cycle that stalls netdev unregistration.Quoted 19 lines
net/ipv6/ip6_fib.c +1
-1@@ -1043,8 +1043,8 @@ static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,struct fib6_table *table = rt->fib6_table;/* Flush all cached dst in exception table */ -rt6_flush_exceptions (rt);fib6_drop_pcpu_from (rt); +rt6_flush_exceptions (rt);if (rt->nh) {spin_lock (&rt->nh->lock);net/ipv6/route.c +0
-0net/ipv6/route.c static int rt6_insert_exception(struct rt6_info *nrt, · line 1736
Quoted 8 lines
net/ipv6/route.c +5
-0@@ -1729,6 +1729,11 @@ static int rt6_insert_exception(struct rt6_info *nrt,spin_lock_bh (&rt6_exception_lock); +if (f6i->fib6_destroying) { + err = -ENOENT ; + goto out; + } +[Severity: Medium]
Can this check observefib6_destroying== 0 afterfib6_purge_rt()has already set it?
fib6_destroyingshares one u8 storage unit with three other bits in structfib6_info:
include/net/ip6_fib.h:u8 should_flush:1, dst_nocount:1, dst_nopolicy:1, fib6_destroying:1, unused:4;Adjacent bitfields are a single memory location, so every assignment to any of them is a non-atomic read-modify-write of the whole byte.
The setter in
fib6_drop_pcpu_from()runs with only the table'stb6_lockheld:net/ipv6/ip6_fib.c:fib6_drop_pcpu_from() { f6i->fib6_destroying = 1; mb(); /* paired with the cmpxchg() in rt6_make_pcpu_route() */ ... }One peer writer of the same byte is
addrconf_disable_policy_idev(), which holds onlyrtnl_net_lockplusidev->lockandifa->lock:net/ipv6/addrconf.c:addrconf_disable_policy_idev() { ... rcu_read_lock(); ifa->rt->dst_nopolicy = val ? true : false; ... }Is there any lock common to both paths?
inet6_rtm_delroute()is registered withRTNL_FLAG_DOIT_UNLOCKED, and ip6_route_del()->__ip6_del_rt()->fib6_del()->fib6_purge_rt() takes onlyrcu_read_lock()andtb6_lock, so a write to net.ipv6.conf.<dev>.disable_policy on a host route (ifa->rt) does not appear to be serialized against the deletion path.If the
disable_policywriter loaded the byte beforefib6_destroyingwas set and stores its copy back afterwards, the bit is cleared again. A third thread inip6_rt_update_pmtu()orrt6_do_redirect()that still holds the pre-removalfib6_infowould then readfib6_destroying== 0 here and insert an exception after the finalrt6_flush_exceptions(), which recreates the fib6_info<->exception cycle and the
Quoted 8 lines
Quoted 5 lines
unregister_netdevice: waiting for gre6 to become freestall described in the commit message. The same lost update also lets
rt6_make_pcpu_route()re-add per-CPU routes after__fib6_drop_pcpu_from()has run.It's a pre-existing issue and not a problem introduced by this patch. The likelihood of this happening is close to zero.
Agreed.
Quoted 4 lines
The current patch needs to be backported up to v5.3 and the issue being flagged here is only visible since v6.16 (
bd11ff421d36), so either handle the issue in a follow-up or in patch 2/2 of v2.
I'd follow up on it. I might not have enough time to check all bit field users and locking this week (due to oncall).
Quoted 11 lines
Quoted 11 lines
Note the load here is a plain bitfield read with no
READ_ONCE(), and the store side is a plain RMW, so this is also visible to KCSAN as a data race.rt6_multipath_flush()writesshould_flushundertb6_lock, sodst_nopolicylooks like the one unsynchronized peer.Would it make sense to give this flag its own storage accessed with WRITE_ONCE()/READ_ONCE() (or to serialize all writers of the containing byte), and to document which lock protects it?
Quoted 5 lines
Quoted 3 lines
The comment above the store in
fib6_drop_pcpu_from()still mentions onlyrt6_make_pcpu_route()and thecmpxchg()as the counterpart; could it also mention the newrt6_exception_lockreader added here?I already mentioned this and it's minor.
Yes, I'll follow up on this in net-next.
Thanks !
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 18 Sep 2026 08:22:05 +0000 you wrote:
Before the cited commit,
fib6_nh_flush_exceptions()always setfrom->exception_bucket_flushed= 1 underrt6_exception_lockto preventrt6_insert_exception()from inserting a new exception for a dyingfib6_info.The flag was replaced with the
FIB6_EXCEPTION_BUCKET_FLUSHEDbit stored innh->rt6i_exception_bucket.[...]
Here is the summary with links:
- [v1,net] ipv6: Prevent rt6_insert_exception() for dying fib6_info.
https://git.kernel.org/netdev/net/c/0346ec2f080b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html