Article View: linux.kernel.netdev
Article #25264[PATCH 19/22] xfrm: Serialize device state teardown
From: Steffen Klassert
Date: Tue, 01 Dec 2026 12:41
Date: Tue, 01 Dec 2026 12:41
176 lines
16365 bytes
16365 bytes
From: Jianbo Liu <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>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.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 9c101be250ee..6f98c5c1a823 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.53.0
Technical Details
Message-ID:
<20261201124252.1163254-20-steffen.klassert@secunet.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!peer05.ams1!peer.ams1.xlned.com!news.xlned.com!npeer.as286.net!npeer-ng0.as286.net!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375058-linux-kernel-netdev=dewy.ruReferences:
<20261201124252.1163254-1-steffen.klassert@secunet.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!peer05.ams1!peer.ams1.xlned.com!news.xlned.com!npeer.as286.net!npeer-ng0.as286.net!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375058-linux-kernel-netdev=dewy.ru From: steffen.klassert@secunet.com (Steffen Klassert) Newsgroups: linux.kernel.netdev Subject: [PATCH 19/22] xfrm: Serialize device state teardown Date: Tue, 1 Dec 2026 13:41:45 +0100 Organization: Usenet Network Message-ID: <20261201124252.1163254-20-steffen.klassert@secunet.com> References: <20261201124252.1163254-1-steffen.klassert@secunet.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1712650"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:Zuv7cWdlb/r0XZ6goHyx3BZQNpQ= sha256:a/UInstPhYNrre4mV5SBLxCkTt6i0Sa7savv9ZY9tdQ= sha1:w6FLphz52kLPundUKm+tcgdna60= sha256:t9NyfIsFtxjguGaovBMollmuxvqYOKddXxVbk2AjJ8c= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id B5DB411739A; Tue, 1 Dec 2026 15:45:56 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=secunet.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9r8LFaq9RA7y; Tue, 1 Dec 2026 15:45:53 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-375058-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=secunet.com X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id B0AC4117395; Tue, 1 Dec 2026 15:45:44 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 2F90128364; Tue, 1 Dec 2026 12:45:38 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796129135; b=YNmVCxcZpX6o1yt15Snq1VVyexuVld4qopfVzkuAytgi8cAidDZ8yCIwoQKX1keDXtp/ IUS0QNO9UJPuJiFjtso2TKPPXzfRhlojtKMnXv6rcRVIaXhHjRwPfwCdfZYkXRrECXGSj A4H2iBdEOCypBSjkswTbD5YqV6ao7bE7g1UD2bjJPHLcWFrCksS7ff/60BeOrac3nDnsF Yfq9CL1Fg4Xu7JQIytH39jy0WJQWgZW+oRFIZ3EqoqImG/Ht0r3wJmNcU8kZy84UjGmDC 3dHJHdTISo9XM36L6CEJMk1/DhwIyVg08vGfFiQv0pcn3R8P+oBpYy6eOvU4za6o/lg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796129135; h=X-Original-To:Received:DMARC-Filter:Received:X-Virus-Scanned: Received:Received:DKIM-Filter:DKIM-Signature:Received:Received:From: To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding:Content-Type:X-ClientProxiedBy; bh=YH1EdcxZxvsZxUa/xQbDnbNHcJizVfIgl8au0eJnG1M=; b=5MMQvu7rNj7RIe8jk3gUruxfSqPNblpeCX+94oEWcLGUYCmTZqyN/hc9YxgckJCTTbtR 3hq/r9DVsOMNbWepVR7HVhxdLNjLZMFLlZSa86C1Vs7waM7MoI2xHsjrdjNtZfQKwodH/ kmTMxRbA4KAMxkjqNvJYGucd+kgbLbq5rxZejTURjNHxsEH+0zJAml5PPVFxzqpa1GYfP FiYy7hCUjjVXP1fpH/08n4mFq06vEmVF3aekuGFsnTiMZbqvMK+C6l+NSHGkexulF4bQK QAtBCmR8hSxUTLzDwDABsZc20E+vUoyj9uo3wmtZNbdmtEf1g7rtEahU6mAVBaEnOZA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=LguHinx6; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DEE91606A69F; Tue, 1 Dec 2026 12:45:34 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=LguHinx6 X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796129134; b=VHwA3KrmVQPNrB+is6F3txzrNidx5qFLE5Wki+nssmd/fb/a62yTyp9w3nx2vOIBQnmd hUpcm3hR53uaeb+LeACi8/nnbZlIPX6qicmQ/N6ScuLHeXLZraNnWkFtSnsyGXdEwwK1W 8k7YXOGhHEeKLQyDjzVdYorUbl8ZB1hUuy1XLr1AuAOXwLg843sEd7g6ntiJRWxuR8O2r 8K8F21uxIga12+dTMfkPtFS6DYPpkPVEuOSvhG18FhXecUB6rGgYz5QKvqtn0CpVv2Gwe lysbLREZ0Vp6PTXCmw8AjBVwqf6YQCp1XpiTzmyM8AXdJo82qlhacL3UBohq01idwkg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796129134; h=DMARC-Filter:Received:X-Virus-Scanned:Received:Received:DKIM-Filter: DKIM-Signature:Received:Received:From:To:CC:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:Content-Type:X-ClientProxiedBy; bh=YH1EdcxZxvsZxUa/xQbDnbNHcJizVfIgl8au0eJnG1M=; b=rLfWjKMdDwUlDl6mfWNCY8wZ5H1BrpvBXbadwI/UVIB50GldCz7ngge7kobRBq+zO6Ob ELLjvdLIoppLt9zOIwOljkcNzuYH+xRyvgt7QQEKwwgJrklfisJUQ7PTrH3JUHPw07qrS n8nclQNNWgLXQuw3P/znLiZNlFUgUeBrckKTc9QbnRhrkormbxygs2/0ggE0z+sAGYLFS Q4Sev9vPoOcoVSNPhySMdzFX26/823Sl78qAsLsLEfh+jZxCx3EBG5Gg/rs26LX2iActT fyHkvUUZ6meHML6MYMlmLPAlUtC7Q0VMEGMuP4bTOT+7Q2oiduKsEGmbA/wV6ru3pYg== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=LguHinx6; arc=none smtp.remote-ip=62.96.220.36 X-Received: from mx1.secunet.com (mx1.secunet.com [62.96.220.36]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C2EE606A32A for <netdev@vger.kernel.org>; Tue, 1 Dec 2026 12:45:23 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6C2EE606A32A X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com X-Received: from localhost (localhost [127.0.0.1]) by mx1.secunet.com (Postfix) with ESMTP id C3C5220590; Tue, 1 Dec 2026 13:45:21 +0100 (CET) X-Virus-Scanned: by secunet X-Received: from mx1.secunet.com ([127.0.0.1]) by localhost (mx1.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CXBeIlOcPxAV; Tue, 1 Dec 2026 13:45:20 +0100 (CET) X-Received: from EXCH-01.secunet.de (rl1.secunet.de [10.32.0.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.secunet.com (Postfix) with ESMTPS id A48BA205E3; Tue, 1 Dec 2026 13:45:20 +0100 (CET) X-DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com A48BA205E3 X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com; s=202301; t=1796129120; bh=YH1EdcxZxvsZxUa/xQbDnbNHcJizVfIgl8au0eJnG1M=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=LguHinx6wAj1iRdv3UdQA0+cJesNDLVx5ywjxqN1iVJRywEeI9e/me8yxAtWBCNWl ZHnCD7tZwN7FrOWtEe+s75WDy8Fr9maq045L+eMqZz/mgkq208JC71R8GIEs/7PQCD di8WEFOKpiFzpQnOY2akUOycN1wiSDKRn/++iUCgZfGtsXzu/6OL6Lj0dYeF6X6PII D/qEm7RxHj4n9coIBa5d+CAZl/OQ4b431JAwYsgWTG5oVEIAVJavbrKWTz4Lej/I7S fV2MrJCLXSVRfHJplsknCPCou7Nmk2XkbR2bq+/+B32TgJzfsjRYlWO46mFUE02lcB hIiNuvgjNntaw== X-Received: from gauss4 (10.182.7.102) by EXCH-01.secunet.de (10.32.0.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Tue, 1 Dec 2026 13:45:20 +0100 X-Received: (nullmailer pid 1165832 invoked by uid 1000); Tue, 01 Dec 2026 12:43:56 -0000 X-CC: Herbert Xu <herbert@gondor.apana.org.au>, Steffen Klassert <steffen.klassert@secunet.com>, <netdev@vger.kernel.org> X-Mailer: git-send-email 2.53.0 X-In-Reply-To: <20261201124252.1163254-1-steffen.klassert@secunet.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-ClientProxiedBy: EXCH-02.secunet.de (10.32.0.172) To EXCH-01.secunet.de (10.32.0.171) X-Received-Bytes: 16093