* [PATCH ipsec 0/4] xfrm: Fix device state teardown races
@ 2026-11-25 1:34 Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Jianbo Liu @ 2026-11-25 1:34 UTC (permalink / raw)
To: netdev, ipsec
Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Florian Westphal,
linux-kernel, Jianbo Liu
Three fixes for the teardown of xfrm device offload state and one
revert, for ipsec.
xfrm_dev_state_free() runs for the same state from the state GC, from
the device flush and from xfrm_state_find(). Commit 89fefad9f971
("xfrm: serialize state GC with device state flush") serializes the
first two with a mutex, but xfrm_state_find() runs under RCU and
still races with the flush.
Let only one caller free the device state of an SA, hold a state
reference in the flush and wait for an RCU grace period before the
free. The mutex is not needed after that, so revert that commit.
1. xfrm: Give GC list nodes their own storage
Stop sharing the GC list nodes with the bydst and bysrc hash nodes.
Patch 2 uses gclist on states that RCU readers may still walk.
2. xfrm: Serialize device state teardown
Let only one caller free the device state of an SA.
3. xfrm: Wait for an RCU grace period before freeing device state
Let RCU readers of the driver state finish before the free.
4. Revert "xfrm: serialize state GC with device state flush"
Remove the mutex that the fixes above make redundant.
Jianbo Liu (4):
xfrm: Give GC list nodes their own storage
xfrm: Serialize device state teardown
xfrm: Wait for an RCU grace period before freeing device state
Revert "xfrm: serialize state GC with device state flush"
include/net/xfrm.h | 10 +++----
net/xfrm/xfrm_device.c | 4 +--
net/xfrm/xfrm_state.c | 61 ++++++++++++++++++++++++++----------------
3 files changed, 44 insertions(+), 31 deletions(-)
base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
--
2.39.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage
2026-11-25 1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
@ 2026-11-25 1:34 ` Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Jianbo Liu @ 2026-11-25 1:34 UTC (permalink / raw)
To: netdev, ipsec
Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Florian Westphal,
linux-kernel, Jianbo Liu, Carolina Jubran
gclist currently shares storage with bydst, and dev_gclist
shares storage with bysrc.
This is unsafe for bydst and bysrc, which can be traversed under
RCU without holding xfrm_state_lock. __xfrm_state_delete() unlinks
these nodes with hlist_del_init_rcu(), so a reader that started before
the unlink may still be following their linkage after the state has
been removed from the hash table.
Later, when the last reference is dropped, __xfrm_state_destroy()
adds x->gclist to xfrm_state_gc_list. Since gclist and bydst
currently share the same storage, adding the state to the GC list can
overwrite the bydst linkage while an RCU reader is still using it.
The same issue applies to dev_gclist and bysrc.
Give gclist, bydst, and bysrc separate storage so their hash-list
linkage remains valid for outstanding RCU readers. Share dev_gclist
with byseq instead. byseq has no RCU readers, only
__xfrm_find_acq_byseq() under xfrm_state_lock, and the state is
unlinked from byseq before dev_gclist is used. Sharing keeps the
growth of struct xfrm_state at 16 bytes rather than 32.
Fixes: d737a5805581 ("xfrm: state: don't use lock anymore unless acquire operation is needed")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
include/net/xfrm.h | 10 ++++------
net/xfrm/xfrm_state.c | 1 +
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 445223a8dc6f..bc59a979a4d3 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -189,16 +189,14 @@ enum xfrm_replay_mode {
/* Full description of state of transformer. */
struct xfrm_state {
possible_net_t xs_net;
- union {
- struct hlist_node gclist;
- struct hlist_node bydst;
- };
+ struct hlist_node bydst;
+ struct hlist_node bysrc;
+ struct hlist_node gclist;
union {
struct hlist_node dev_gclist;
- struct hlist_node bysrc;
+ struct hlist_node byseq;
};
struct hlist_node byspi;
- struct hlist_node byseq;
struct hlist_node state_cache;
struct hlist_node state_cache_input;
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index f4a4ddaeb9b3..3eb012ed8657 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -751,6 +751,7 @@ struct xfrm_state *xfrm_state_alloc(struct net *net)
INIT_HLIST_NODE(&x->state_cache);
INIT_HLIST_NODE(&x->bydst);
INIT_HLIST_NODE(&x->bysrc);
+ INIT_HLIST_NODE(&x->gclist);
INIT_HLIST_NODE(&x->byspi);
INIT_HLIST_NODE(&x->byseq);
hrtimer_setup(&x->mtimer, xfrm_timer_handler, CLOCK_BOOTTIME,
--
2.39.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH ipsec 2/4] xfrm: Serialize device state teardown
2026-11-25 1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
@ 2026-11-25 1:34 ` Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
3 siblings, 0 replies; 5+ messages in thread
From: Jianbo Liu @ 2026-11-25 1:34 UTC (permalink / raw)
To: netdev, ipsec
Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Florian Westphal,
linux-kernel, Jianbo Liu, Carolina Jubran
Commit 89fefad9f971 ("xfrm: serialize state GC with device state
flush") stopped the state GC and xfrm_dev_state_flush() from freeing
the same device state at once, but a third caller is not covered.
xfrm_state_find() frees a packet offload acquire state when km_query()
fails. It runs under RCU and can't take the mutex.
xfrm_dev_state_delete() first puts the state on xfrm_state_dev_gc_list
with xso->dev still set, so a device flush can find it there and call
xfrm_dev_state_free() too. Both callers see xso->dev set and both
unlink the state, so the second hlist_del() hits a poisoned node.
Claim the device in xfrm_dev_state_free() by clearing xso->dev under
x->lock and xfrm_state_dev_gc_lock, so only one caller unlinks the
state, invokes xdo_dev_state_free() and drops the netdevice reference.
x->lock also keeps xso->dev stable for the stats updates that hold it.
In the flush, take a state reference before removing a state from the
device GC list, so it stays alive while the flush owns it. Skip a
state with no reference left, the state GC frees it.
Keep xso->type unchanged after detaching the device, so lockless
readers of xso->type do not see a packet offload state as a software
state. Reject offloaded states with no device in
xfrm_dev_offload_ok(): a packet offload state would otherwise pass
the !dev check there, and xfrm_output() would send the packet with no
ESP or use the NULL device in xfrm_dev_direct_output().
Found by the AI review of the commit above (see Link) and reproduced
on ConnectX-7 with a packet offload policy that has no SA, an acquire
listener that never reads, UDP senders hitting the policy and the
device taken down in a loop:
[ 167.689163] Oops: general protection fault, probably for non-canonical address 0xdead000000000122: 0000 [#1] SMP
[ 167.690938] CPU: 11 UID: 0 PID: 2093 Comm: python3 Not tainted 7.4.0-rc1+ #10 PREEMPTLAZY
[ 167.694337] RIP: 0010:xfrm_dev_state_free+0x41/0xe0
[ 167.699068] RAX: dead000000000122 RBX: ff1100014f543800 RCX: ff11000103bbf800
[ 167.700247] RDX: dead000000000100 RSI: 0000000000000001 RDI: ffffffff82da8c68
[ 167.716897] Call Trace:
[ 167.717405] <TASK>
[ 167.717863] xfrm_state_find+0x19b0/0x1ab0
[ 167.718629] xfrm_resolve_and_create_bundle+0x17d/0x11e0
[ 167.722070] xfrm_lookup_with_ifid+0x1e7/0xb50
[ 167.722872] xfrm_lookup_route+0x1d/0xa0
[ 167.723593] udp_sendmsg+0x8da/0xc90
[ 167.724273] __sock_sendmsg+0x38/0x70
[ 167.724988] __sys_sendto+0x119/0x180
[ 167.727954] __x64_sys_sendto+0x20/0x30
[ 167.728666] do_syscall_64+0x72/0x3b0
[ 167.729357] entry_SYSCALL_64_after_hwframe+0x4b/0x53
[ 167.741064] </TASK>
[ 167.752388] Kernel panic - not syncing: Fatal exception in interrupt
With this series the same test ran 400 cycles with no crash, and 150
cycles on a kernel with KASAN and lockdep.
Fixes: 07b87f9eea0c ("xfrm: Fix unregister netdevice hang on hardware offload.")
Link: https://lore.kernel.org/netdev/20260908224854.1591115-1-kuba@kernel.org/
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
net/xfrm/xfrm_device.c | 4 ++--
net/xfrm/xfrm_state.c | 51 ++++++++++++++++++++++++++++--------------
2 files changed, 36 insertions(+), 19 deletions(-)
diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c
index f153bf695b9d..3bbb970cd337 100644
--- a/net/xfrm/xfrm_device.c
+++ b/net/xfrm/xfrm_device.c
@@ -422,11 +422,11 @@ bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
int mtu;
struct dst_entry *dst = skb_dst(skb);
struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
- struct net_device *dev = x->xso.dev;
+ struct net_device *dev = READ_ONCE(x->xso.dev);
bool check_tunnel_size;
if (!x->type_offload ||
- (x->xso.type == XFRM_DEV_OFFLOAD_UNSPECIFIED && x->encap))
+ (x->xso.type == XFRM_DEV_OFFLOAD_UNSPECIFIED ? !!x->encap : !dev))
return false;
if ((!dev || dev == xfrm_dst_path(dst)->dev) &&
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index 3eb012ed8657..8465ae064742 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -790,20 +790,28 @@ EXPORT_SYMBOL_GPL(xfrm_dev_state_delete);
void xfrm_dev_state_free(struct xfrm_state *x)
{
struct xfrm_dev_offload *xso = &x->xso;
- struct net_device *dev = READ_ONCE(xso->dev);
+ struct net_device *dev;
- if (dev && dev->xfrmdev_ops) {
- spin_lock_bh(&xfrm_state_dev_gc_lock);
- if (!hlist_unhashed(&x->dev_gclist))
- hlist_del(&x->dev_gclist);
- spin_unlock_bh(&xfrm_state_dev_gc_lock);
+ if (!READ_ONCE(xso->dev))
+ return;
- if (dev->xfrmdev_ops->xdo_dev_state_free)
- dev->xfrmdev_ops->xdo_dev_state_free(dev, x);
- WRITE_ONCE(xso->dev, NULL);
- xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
- netdev_put(dev, &xso->dev_tracker);
+ spin_lock_bh(&x->lock);
+ spin_lock(&xfrm_state_dev_gc_lock);
+ dev = xso->dev;
+ if (!dev || !dev->xfrmdev_ops) {
+ spin_unlock(&xfrm_state_dev_gc_lock);
+ spin_unlock_bh(&x->lock);
+ return;
}
+ if (!hlist_unhashed(&x->dev_gclist))
+ hlist_del(&x->dev_gclist);
+ WRITE_ONCE(xso->dev, NULL);
+ spin_unlock(&xfrm_state_dev_gc_lock);
+ spin_unlock_bh(&x->lock);
+
+ if (dev->xfrmdev_ops->xdo_dev_state_free)
+ dev->xfrmdev_ops->xdo_dev_state_free(dev, x);
+ netdev_put(dev, &xso->dev_tracker);
}
#endif
@@ -974,6 +982,7 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
struct hlist_node *tmp;
struct xfrm_dev_offload *xso;
int i, err = 0, cnt = 0;
+ HLIST_HEAD(flush_list);
spin_lock_bh(&net->xfrm.xfrm_state_lock);
err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
@@ -1012,21 +1021,29 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
mutex_lock(&xfrm_state_gc_mutex);
spin_lock_bh(&xfrm_state_dev_gc_lock);
-restart_gc:
hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) {
xso = &x->xso;
if (xso->dev == dev) {
- spin_unlock_bh(&xfrm_state_dev_gc_lock);
- xfrm_dev_state_free(x);
- spin_lock_bh(&xfrm_state_dev_gc_lock);
- goto restart_gc;
+ if (!refcount_inc_not_zero(&x->refcnt))
+ continue;
+ hlist_del_init(&x->dev_gclist);
+ /* xfrm_dev_state_free() may consume dev_gclist. The
+ * reference prevents state GC from using gclist until
+ * this flush removes it and drops the reference.
+ */
+ hlist_add_head(&x->gclist, &flush_list);
}
-
}
spin_unlock_bh(&xfrm_state_dev_gc_lock);
mutex_unlock(&xfrm_state_gc_mutex);
+ hlist_for_each_entry_safe(x, tmp, &flush_list, gclist) {
+ hlist_del_init(&x->gclist);
+ xfrm_dev_state_free(x);
+ xfrm_state_put(x);
+ }
+
xfrm_flush_gc();
return err;
--
2.39.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state
2026-11-25 1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
@ 2026-11-25 1:34 ` Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
3 siblings, 0 replies; 5+ messages in thread
From: Jianbo Liu @ 2026-11-25 1:34 UTC (permalink / raw)
To: netdev, ipsec
Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Florian Westphal,
linux-kernel, Jianbo Liu, Carolina Jubran
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@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush"
2026-11-25 1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
` (2 preceding siblings ...)
2026-11-25 1:34 ` [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Jianbo Liu
@ 2026-11-25 1:34 ` Jianbo Liu
3 siblings, 0 replies; 5+ messages in thread
From: Jianbo Liu @ 2026-11-25 1:34 UTC (permalink / raw)
To: netdev, ipsec
Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Florian Westphal,
linux-kernel, Jianbo Liu, Carolina Jubran
This reverts commit 89fefad9f971bc637fb22373078144f2563c4be9.
The mutex kept the state GC from destroying a state while
xfrm_dev_state_flush() was freeing its device state. It can't cover
xfrm_state_find(), which frees device state under RCU.
The flush now holds a reference on each state it frees, and
xfrm_dev_state_free() lets only one caller free the device state.
Nothing is left for the mutex to protect, so remove it.
This needs "xfrm: Serialize device state teardown". Without that patch
the flush frees the device state of a state it holds no reference on.
Link: https://lore.kernel.org/netdev/aqfZNeR58HLm2x1o@secunet.com/
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
net/xfrm/xfrm_state.c | 5 -----
1 file changed, 5 deletions(-)
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index 9883fb3545f8..717c0bdb9559 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -226,7 +226,6 @@ static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
static DEFINE_SPINLOCK(xfrm_state_gc_lock);
static DEFINE_SPINLOCK(xfrm_state_dev_gc_lock);
-static DEFINE_MUTEX(xfrm_state_gc_mutex);
int __xfrm_state_delete(struct xfrm_state *x);
@@ -639,10 +638,8 @@ static void xfrm_state_gc_task(struct work_struct *work)
synchronize_rcu();
- mutex_lock(&xfrm_state_gc_mutex);
hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
xfrm_state_gc_destroy(x);
- mutex_unlock(&xfrm_state_gc_mutex);
}
static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
@@ -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();
--
2.39.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 1:35 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Jianbo Liu
2026-11-25 1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox