From: paulinsider <paulinsider@gmail.com>
To: steffen.klassert@secunet.com, herbert@gondor.apana.org.au,
davem@davemloft.net
Cc: netdev@vger.kernel.org, slamhu@tencent.com,
paulinsider <paulinsider@gmail.com>,
TencentOS Corvus AI <corvus@tencent.com>
Subject: [PATCH] xfrm: hold state while flushing device GC list
Date: Sat, 1 Aug 2026 12:13:40 +0800 [thread overview]
Message-ID: <20260801041340.92117-1-paulinsider@gmail.com> (raw)
The device GC list does not hold a reference to the states it
contains. xfrm_dev_state_flush() drops xfrm_state_dev_gc_lock before
calling xfrm_dev_state_free(), so the regular GC worker can
concurrently destroy and free the entire xfrm_state. Subsequent access
through the stale pointer can corrupt memory and cause a kernel panic.
Try to pin live states with xfrm_state_hold_rcu() while holding the
device GC lock, and skip states whose reference count has already
reached zero. Drop the acquired reference after the device state
cleanup completes. Also use hlist_del_init() when removing dev_gclist
so the node is marked as unhashed after removal.
Fixes: 07b87f9eea0c ("xfrm: Fix unregister netdevice hang on hardware offload.")
Reported-by: TencentOS Corvus AI <corvus@tencent.com>
Signed-off-by: paulinsider <paulinsider@gmail.com>
---
net/xfrm/xfrm_state.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index 36a4f6793ede..01138d3d1067 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -785,7 +785,7 @@ void xfrm_dev_state_free(struct xfrm_state *x)
if (dev && dev->xfrmdev_ops) {
spin_lock_bh(&xfrm_state_dev_gc_lock);
if (!hlist_unhashed(&x->dev_gclist))
- hlist_del(&x->dev_gclist);
+ hlist_del_init(&x->dev_gclist);
spin_unlock_bh(&xfrm_state_dev_gc_lock);
if (dev->xfrmdev_ops->xdo_dev_state_free)
@@ -1006,8 +1006,13 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
xso = &x->xso;
if (xso->dev == dev) {
+ /* The device GC list does not hold a reference to x. */
+ if (!xfrm_state_hold_rcu(x))
+ continue;
+
spin_unlock_bh(&xfrm_state_dev_gc_lock);
xfrm_dev_state_free(x);
+ xfrm_state_put(x);
spin_lock_bh(&xfrm_state_dev_gc_lock);
goto restart_gc;
}
--
2.44.0
reply other threads:[~2026-08-01 4:13 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260801041340.92117-1-paulinsider@gmail.com \
--to=paulinsider@gmail.com \
--cc=corvus@tencent.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=netdev@vger.kernel.org \
--cc=slamhu@tencent.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