Re: [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush"
From: netdev-bot+sashiko
Date: Sat Nov 28 2026 - 21:15:01 EST
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] xfrm_dev_state_flush() (net/xfrm/xfrm_state.c:1018-1044) now
skips any matching state on xfrm_state_dev_gc_list whose refcount is…
--
Revert "xfrm: serialize state GC with device state flush"
This reverts the mutex that serialized xfrm_state_gc_task() with the
dev_gclist pass in xfrm_dev_state_flush(). It depends on "xfrm: Serialize
device state teardown". In that patch the flush takes a reference on each
state it frees and skips states whose refcount has already reached zero.
Link: https://lore.kernel.org/netdev/aqfZNeR58HLm2x1o@xxxxxxxxxxx/
> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 9883fb3545f8f..717c0bdb95597 100644
> --- a/net/xfrm/xfrm_state.c
> +++ b/net/xfrm/xfrm_state.c
[ ... ]
> @@ -1018,7 +1015,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
> out:
> spin_unlock_bh(&net->xfrm.xfrm_state_lock);
>
> - mutex_lock(&xfrm_state_gc_mutex);
> spin_lock_bh(&xfrm_state_dev_gc_lock);
> hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) {
> xso = &x->xso;
> @@ -1035,7 +1031,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
> }
> }
> spin_unlock_bh(&xfrm_state_dev_gc_lock);
> - mutex_unlock(&xfrm_state_gc_mutex);
>
> if (!hlist_empty(&flush_list))
> synchronize_rcu();
[Severity: Medium]
Without the mutex, the dev_gclist pass in xfrm_dev_state_flush() relies
on the refcount skip added by "xfrm: Serialize device state teardown":
net/xfrm/xfrm_state.c:xfrm_dev_state_flush() {
...
if (xso->dev == dev) {
if (!refcount_inc_not_zero(&x->refcnt))
continue;
...
xfrm_flush_gc();
}
A skipped state is left to the state GC. The only wait is xfrm_flush_gc(),
which is just flush_work(&xfrm_state_gc_work).
Can that wait miss the skipped state? xfrm_state_put() makes the zero
refcount visible before the state is queued for GC:
xfrm_state_put()
refcount_dec_and_test(&x->refcnt)
__xfrm_state_destroy()
spin_lock_bh(&xfrm_state_gc_lock);
hlist_add_head(&x->gclist, &xfrm_state_gc_list);
spin_unlock_bh(&xfrm_state_gc_lock);
schedule_work(&xfrm_state_gc_work);
Another context can drop the last reference, for example a dst bundle
destroy, a secpath free, or a netlink getsa put. Suppose it is preempted
or delayed between refcount_dec_and_test() and schedule_work() while the
flush is running. The flush then skips the state, and flush_work() finds
the work idle and returns at once.
In that case the NETDEV_DOWN or NETDEV_UNREGISTER notifier returns while
xdo_dev_state_free() and netdev_put(dev, &xso->dev_tracker) for that SA
are still waiting to run from the GC work.
For NETDEV_UNREGISTER, unregister_netdevice_many_notify() calls
ndo_uninit right after the notifiers. For bonding that is bond_uninit(),
which does:
mutex_destroy(&bond->ipsec_lock);
A late call chain
xfrm_state_gc_task()->xfrm_state_gc_destroy()->xfrm_dev_state_free()->bond_ipsec_free_sa()
would then lock bond->ipsec_lock after it has been destroyed.
Before the series, the device state of every matching dev_gclist entry
was freed synchronously, whatever its refcount. The series says skipped
states are freed by the state GC and that xfrm_flush_gc() waits for them.
Does that wait only hold when the flush itself dropped the last
reference?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com