* [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-29 2:14 ` netdev-bot+sashiko
2026-11-25 1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ 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 [flat|nested] 9+ messages in thread* Re: [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage
2026-11-25 1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
@ 2026-11-29 2:14 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29 2:14 UTC (permalink / raw)
To: jianbol
Cc: netdev, ipsec, steffen.klassert, herbert, davem, edumazet, kuba,
pabeni, horms, fw, linux-kernel, cjubran
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit fixes two aliasing bugs but has only one Fixes: tag,
d737a5805581 ("xfrm: state: don't use lock anymore unless acquire…
- [Low] The message says the gclist/bydst linkage is overwritten "later,
when the last reference is dropped", and then that "the same issue…
--
xfrm: Give GC list nodes their own storage
This patch stops struct xfrm_state from sharing storage between gclist
and bydst, and between dev_gclist and bysrc. Without the split, lock-free
RCU readers of the bydst and bysrc hash chains could follow linkage that
the GC lists had already overwritten. dev_gclist now shares a union with
byseq, which is only walked under xfrm_state_lock.
> 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.
[Severity: Low]
Is "the same issue" accurate for the dev_gclist/bysrc half? Its mechanism
and its consequence look different from the gclist/bydst case.
dev_gclist is not written when the last reference is dropped. It is
written right after the RCU unlink, and no grace period passes first:
__xfrm_state_delete()
hlist_del_init_rcu(&x->bysrc); /* ->next kept for readers */
...
spin_unlock(&net->xfrm.xfrm_state_lock);
xfrm_dev_state_delete(x)
hlist_add_head(&x->dev_gclist, &xfrm_state_dev_gc_list);
Then xfrm_dev_state_flush() calls xfrm_dev_state_free() right after
xfrm_state_delete(), with no synchronize_rcu(). xfrm_dev_state_free()
does this:
if (!hlist_unhashed(&x->dev_gclist))
hlist_del(&x->dev_gclist);
hlist_del() writes LIST_POISON1 into the storage that used to alias
x->bysrc.next.
Suppose a reader in __xfrm_state_lookup_byaddr() is on x at that point:
hlist_for_each_entry_rcu(x, state_ptrs->bysrc + h, bysrc) {
if (x->props.family != family ||
It can load LIST_POISON1. hlist_entry_safe() only filters NULL, so the
read of x->props.family would fault. The reader could also follow
x->bysrc.next into another state on the dev GC list, and that state may
already have been freed.
These lookups run from xfrm6_input_addr() on IPv6 receive, and from the
XFRM_MSG_DELSA/GETSA netlink path in xfrm_user.c.
For gclist/bydst, xfrm_state_gc_task() calls synchronize_rcu() before
freeing. The result there is mostly a diverted or truncated walk, so
SAs get missed.
Could the commit message say that the offload half fixes a possible oops
or use-after-free, and not only a corrupted walk?
> Fixes: d737a5805581 ("xfrm: state: don't use lock anymore unless acquire operation is needed")
[Severity: Low]
This tag covers the gclist/bydst alias. Does the dev_gclist/bysrc alias
need its own Fixes: tags?
Commit 07b87f9eea0c ("xfrm: Fix unregister netdevice hang on hardware
offload.") added the union of dev_gclist and bysrc in 2024.
Later, commit 2e460eefbd44 ("xfrm: state: make xfrm_state_lookup_byaddr
lockless") replaced spin_lock_bh(&net->xfrm.xfrm_state_lock) with
rcu_read_lock() in xfrm_state_lookup_byaddr().
Before that change, every bysrc walker held xfrm_state_lock. This
includes __xfrm_state_lookup_byaddr() through both
xfrm_state_lookup_byaddr() and __xfrm_state_locate(), and also
xfrm_migrate_state_find(). Reusing the bysrc storage only became unsafe
once both commits were in the tree.
Stable trees older than 6.10 don't have the dev_gclist union, so the
include/net/xfrm.h hunk won't apply there as-is.
Would Fixes: tags for 07b87f9eea0c and 2e460eefbd44 make it clearer
which trees need the bysrc half?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com
^ permalink raw reply [flat|nested] 9+ 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-29 2:14 ` netdev-bot+sashiko
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, 1 reply; 9+ 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 [flat|nested] 9+ messages in thread* Re: [PATCH ipsec 2/4] xfrm: Serialize device state teardown
2026-11-25 1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
@ 2026-11-29 2:14 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29 2:14 UTC (permalink / raw)
To: jianbol
Cc: netdev, ipsec, steffen.klassert, herbert, davem, edumazet, kuba,
pabeni, horms, fw, linux-kernel, cjubran
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · 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).
- [High] The new `!dev` rejection in xfrm_dev_offload_ok() does not stop
xfrm_dev_direct_output() from using a NULL device, although the commit…
- [Medium] xfrm_dev_state_flush() now skips states on
xfrm_state_dev_gc_list whose refcount is already zero (`if…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch's
design depends on it.
--
xfrm: Serialize device state teardown
This commit makes xfrm_dev_state_free() claim the device by clearing
xso->dev under x->lock and xfrm_state_dev_gc_lock. Only one caller then
unlinks the state and calls xdo_dev_state_free(). xfrm_dev_state_flush()
now takes a state reference before moving entries off the device GC list,
and xfrm_dev_offload_ok() rejects offloaded states with no device.
Link: https://lore.kernel.org/netdev/20260908224854.1591115-1-kuba@kernel.org/
> diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c
> index f153bf695b9dd..3bbb970cd337f 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;
[Severity: High]
The commit message says this check stops xfrm_output() from using "the
NULL device in xfrm_dev_direct_output()". Does it cover that case?
The !dev test only looks at the local READ_ONCE() copy, and that copy is
never passed back to the caller. For a tunnel mode packet offload state
with !skb->sk, xfrm_output() does:
net/xfrm/xfrm_output.c:xfrm_output() {
...
if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) {
if (!xfrm_dev_offload_ok(skb, x)) {
...
if (x->props.mode == XFRM_MODE_TUNNEL && !skb->sk)
return xfrm_dev_direct_output(sk, x, skb);
...
}
xfrm_dev_direct_output() then runs the LOCAL_OUT netfilter hooks. After
that it reads x->xso.dev again with a plain load and no NULL check:
net/xfrm/xfrm_output.c:xfrm_dev_direct_output() {
...
err = skb_dst(skb)->ops->local_out(net, sk, skb);
if (unlikely(err != 1))
return err;
...
skb->dev = x->xso.dev;
__skb_push(skb, skb->dev->hard_header_len);
return dev_queue_xmit(skb);
}
Suppose xfrm_dev_state_free() runs WRITE_ONCE(xso->dev, NULL) on another
CPU between the check and that read. Can skb->dev->hard_header_len then
dereference NULL?
In this commit the window looks wide. The first loop of
xfrm_dev_state_flush() calls xfrm_dev_state_free() right after
xfrm_state_delete(), while forwarded packets on other CPUs can still be
sending on the bundle:
err = xfrm_state_delete(x);
xfrm_dev_state_free(x);
The later commit in the series, "xfrm: Wait for an RCU grace period
before freeing device state", narrows the window but does not seem to
close it. synchronize_rcu() only waits for readers that were already
running when the grace period started.
For example, take a forwarded skb held by NFQUEUE in FORWARD or
POST_ROUTING. It still has a refcounted bundle dst that pins x. It can be
reinjected through nf_reinject() in a new RCU section after x has been
deleted. xfrm_output() does not check km.state on the packet offload path.
Would it be more robust to pass the checked dev into
xfrm_dev_direct_output()? Another option is to READ_ONCE() it there,
check for NULL and drop the packet.
> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 3eb012ed86576..8465ae0647427 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)
> {
[ ... ]
> + 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);
> }
[Severity: High]
This isn't a bug introduced by this patch, but the new locking scheme
depends on it. The commit message says "x->lock also keeps xso->dev
stable for the stats updates that hold it". However, not every stats
caller holds x->lock.
copy_to_user_state() in net/xfrm/xfrm_user.c does this with only
xfrm_cfg_mutex held:
if (x->xso.dev)
xfrm_dev_state_update_stats(x);
There are two ways to reach it. One is xfrm_get_sa() ->
xfrm_state_netlink() -> dump_one_state(), with just a state reference.
The other is xfrm_notify_sa(), after xfrm_del_sa() has already put the
state on xfrm_state_dev_gc_list.
The mlx5 callback accepts xfrm_cfg_mutex as enough protection and reads
x->xso.dev again:
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c:mlx5e_xfrm_update_stats() {
struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
...
struct net *net = dev_net(x->xso.dev);
...
lockdep_assert(lockdep_is_held(&x->lock) ||
lockdep_is_held(&net->xfrm.xfrm_cfg_mutex) ||
lockdep_is_held(&net->xfrm.xfrm_state_lock));
...
}
xfrm_dev_state_flush() runs from the netdev notifier and takes neither
xfrm_cfg_mutex nor xfrm_state_lock around xfrm_dev_state_free(). That
function clears xso->dev and then calls mlx5e_xfrm_free_state(), which
does kfree(sa_entry).
Could an XFRM_MSG_GETSA, or the notification sent for XFRM_MSG_DELSA,
race with NETDEV_DOWN and hit dev_net(NULL)? Could it use sa_entry after
it has been freed?
With this patch the NULL write now happens before the driver free, which
seems to make the dev_net(NULL) case more likely. The synchronize_rcu()
added later in the series won't help here, because this reader is not in
an RCU read-side section.
> @@ -974,6 +982,7 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
[ ... ]
> @@ -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;
[Severity: Medium]
What happens to a state whose last reference is dropped at the same time
as this flush?
xfrm_state_put() does refcount_dec_and_test() first. Only after that does
__xfrm_state_destroy() queue the GC work:
net/xfrm/xfrm_state.c:__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);
}
The putter could be preempted or interrupted after the refcount reaches
zero but before schedule_work(). One example is a preemptible netlink
handler dropping its xfrm_state_lookup() reference. The flush would then
see refcount 0 here and skip the state.
The xfrm_flush_gc() at the end is only flush_work(&xfrm_state_gc_work),
so it does not wait for work that has not been queued yet.
In that case xfrm_dev_down() or xfrm_dev_unregister() could return while:
- xso->dev still points at the device
- the netdev_hold() reference is still held
- xdo_dev_state_free() has not run yet
The driver callback would run later from the GC worker, after the flush
has already returned.
Before this patch the flush called xfrm_dev_state_free() on every matching
dev_gc_list entry regardless of refcount, serialized with GC by
xfrm_state_gc_mutex. Is this gap intended? It also appears to remain at
the end of the series.
> + 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);
> }
[ ... ]
> + 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;
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com
^ permalink raw reply [flat|nested] 9+ 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-29 2:14 ` netdev-bot+sashiko
2026-11-25 1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
3 siblings, 1 reply; 9+ 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 [flat|nested] 9+ messages in thread* Re: [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state
2026-11-25 1:34 ` [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Jianbo Liu
@ 2026-11-29 2:14 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29 2:14 UTC (permalink / raw)
To: jianbol
Cc: netdev, ipsec, steffen.klassert, herbert, davem, edumazet, kuba,
pabeni, horms, fw, linux-kernel, cjubran
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 1 · Low: 1
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 patch removes the synchronous xfrm_dev_state_free() after
xfrm_state_delete() in xfrm_dev_state_flush() (net/xfrm/xfrm_state.c,…
- [Low] The new synchronize_rcu() in xfrm_dev_state_flush()
(net/xfrm/xfrm_state.c:1041) runs with RTNL held.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
leaves a second caller of the same use-after-free.
--
xfrm: Wait for an RCU grace period before freeing device state
This patch removes the xfrm_dev_state_free() call that ran right after
xfrm_state_delete() in xfrm_dev_state_flush(). Device state freeing now
depends on the dev GC list collection pass or on the state GC. It also
adds a synchronize_rcu() between collecting the states and calling
xfrm_dev_state_free(), so that a grace period separates
xdo_dev_state_delete() and xdo_dev_state_free().
> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 8465ae0647427..9883fb3545f8f 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);
[Severity: Medium]
With the synchronous free gone, can a state deleted by the flush end up
neither on flush_list nor visible to xfrm_flush_gc()?
The commit message covers two cases. Either the collection pass finds the
state with a reference left, or the flush dropped the last reference.
What happens when some other holder (an skb secpath, a dst bundle, a
netlink handler) drops the last reference after the flush loop's
xfrm_state_put() but before the collection pass?
xfrm_state_put()->__xfrm_state_destroy() does:
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);
spin_unlock_bh() can run pending softirqs or act as a preemption point
before schedule_work() is reached. If the collection pass in
xfrm_dev_state_flush() runs during that gap, it sees a zero refcount and
skips the state:
if (!refcount_inc_not_zero(&x->refcnt))
continue;
flush_list may then be empty, so synchronize_rcu() is skipped.
xfrm_flush_gc()->flush_work() also returns at once, because the work is
not queued yet.
Would xfrm_dev_state_flush() then return for NETDEV_DOWN or
NETDEV_UNREGISTER with xso->dev still set, the netdev reference still
held and xdo_dev_state_free() not yet called?
The state GC would free it later, but only after the device is down. On
unregister, netdev_wait_allref would wait for it. On mlx5, operations
such as mlx5_eswitch_block_ipsec could fail for a while because the SA
is still there.
Before this patch, the flush loop freed the states it deleted while it
still held a reference. The earlier "xfrm: Serialize device state
teardown" patch in this series added the zero-refcount skip, and this
patch now applies it to the states the flush deletes.
[ ... ]
> @@ -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();
> +
[Severity: High]
This is a pre-existing issue, not one introduced by this patch. The
commit message says the required ordering is
xdo_dev_state_delete() -> synchronize_rcu() -> xdo_dev_state_free(), but
the grace period is only added here.
Does bond_ipsec_del_sa_all() in drivers/net/bonding/bond_main.c have the
same use-after-free?
It runs when the active slave changes, via bond_change_active_slave().
For each offloaded SA it calls delete and then free back to back on the
old slave:
drivers/net/bonding/bond_main.c:bond_ipsec_del_sa_all() {
...
if (ipsec->xs->km.state != XFRM_STATE_DEAD)
real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
ipsec->xs);
spin_unlock_bh(&ipsec->xs->lock);
if (real_dev->xfrmdev_ops->xdo_dev_state_free)
real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev,
ipsec->xs);
...
}
With an mlx5 slave, mlx5e_xfrm_del_state() removes sa_entry with
xa_erase_bh(&ipsec->sadb, ...). mlx5e_xfrm_free_state() then calls
kfree(sa_entry). At the same time, the RX path on the old slave can be
in:
mlx5e_ipsec_offload_handle_rx_skb()
rcu_read_lock();
sa_entry = xa_load(&ipsec->sadb, sa_handle);
...
xfrm_state_hold(sa_entry->x);
rcu_read_unlock();
If xa_load() ran before the erase, can xfrm_state_hold(sa_entry->x) read
freed memory after the kfree()?
The hardware RX rule is only removed later, inside free_state, by
mlx5e_accel_ipsec_fs_del_rule(). Tagged packets can keep arriving during
this window. The commit message doesn't mention this path. Should it
also get a grace period between delete and free?
[Severity: Low]
xfrm_dev_state_flush() is only called from xfrm_dev_down() and
xfrm_dev_unregister() in net/xfrm/xfrm_device.c. Both are netdev
notifier handlers that run under RTNL. Would synchronize_net() be a
better fit here?
synchronize_net() switches to synchronize_rcu_expedited() when
rtnl_is_locked(). With synchronize_rcu(), each device with offloaded SAs
adds a full normal grace period to RTNL hold time. During batched
unregister or netns dismantle, these grace periods run back to back.
In the same call, xfrm_flush_gc() may also wait for
xfrm_state_gc_task(), which runs its own synchronize_rcu().
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com
^ permalink raw reply [flat|nested] 9+ 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
2026-11-29 2:14 ` netdev-bot+sashiko
3 siblings, 1 reply; 9+ 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 [flat|nested] 9+ messages in thread* Re: [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush"
2026-11-25 1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
@ 2026-11-29 2:14 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29 2:14 UTC (permalink / raw)
To: jianbol
Cc: netdev, ipsec, steffen.klassert, herbert, davem, edumazet, kuba,
pabeni, horms, fw, linux-kernel, cjubran
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@secunet.com/
> 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
^ permalink raw reply [flat|nested] 9+ messages in thread