From: Jianbo Liu <jianbol@nvidia.com>
To: <netdev@vger.kernel.org>, <ipsec@lists.linux.dev>
Cc: Steffen Klassert <steffen.klassert@secunet.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, Florian Westphal <fw@strlen.de>,
<linux-kernel@vger.kernel.org>, Jianbo Liu <jianbol@nvidia.com>,
Carolina Jubran <cjubran@nvidia.com>
Subject: [PATCH ipsec 2/4] xfrm: Serialize device state teardown
Date: Wed, 25 Nov 2026 03:34:10 +0200 [thread overview]
Message-ID: <20261125013412.1123866-3-jianbol@nvidia.com> (raw)
In-Reply-To: <20261125013412.1123866-1-jianbol@nvidia.com>
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
next prev parent reply other threads:[~2026-11-25 1:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125013412.1123866-3-jianbol@nvidia.com \
--to=jianbol@nvidia.com \
--cc=cjubran@nvidia.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fw@strlen.de \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=ipsec@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=steffen.klassert@secunet.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox