[PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state

From: Jianbo Liu

Date: Tue Nov 24 2026 - 20:35:34 EST


xfrm_dev_state_flush() can call xdo_dev_state_free() immediately after
xdo_dev_state_delete(), without waiting for pre-existing RCU readers
of the driver's SA state to complete.

The normal state GC path waits for an RCU grace period before
destroying the state, but the device flush path does not.

Remove the immediate xfrm_dev_state_free() following
xfrm_state_delete(). The delete path queues the state on
xfrm_state_dev_gc_list. The subsequent collection pass picks it up if
a reference is left. If the flush dropped the last one, the state GC
frees it after its own grace period and xfrm_flush_gc() waits for it.

Wait for one RCU grace period after collecting the states and before
freeing them. This establishes the required
xdo_dev_state_delete() -> synchronize_rcu() -> xdo_dev_state_free()
ordering while using a single grace period for the whole flush.

Fixes: 07b87f9eea0c ("xfrm: Fix unregister netdevice hang on hardware offload.")
Signed-off-by: Jianbo Liu <jianbol@xxxxxxxxxx>
Reviewed-by: Carolina Jubran <cjubran@xxxxxxxxxx>
---
net/xfrm/xfrm_state.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index 8465ae064742..9883fb3545f8 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -1000,7 +1000,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
spin_unlock_bh(&net->xfrm.xfrm_state_lock);

err = xfrm_state_delete(x);
- xfrm_dev_state_free(x);

xfrm_audit_state_delete(x, err ? 0 : 1,
task_valid);
@@ -1038,6 +1037,9 @@ 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();
+
hlist_for_each_entry_safe(x, tmp, &flush_list, gclist) {
hlist_del_init(&x->gclist);
xfrm_dev_state_free(x);
--
2.39.3