Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 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== 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== 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 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 ; Tue, 1 Dec 2026 12:45:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6C2EE606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com 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 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 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) 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) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com A48BA205E3 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== 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 Received: (nullmailer pid 1165832 invoked by uid 1000); Tue, 01 Dec 2026 12:43:56 -0000 From: Steffen Klassert To: David Miller , Jakub Kicinski CC: Herbert Xu , Steffen Klassert , Subject: [PATCH 19/22] xfrm: Serialize device state teardown Date: Tue, 1 Dec 2026 13:41:45 +0100 Message-ID: <20261201124252.1163254-20-steffen.klassert@secunet.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201124252.1163254-1-steffen.klassert@secunet.com> References: <20261201124252.1163254-1-steffen.klassert@secunet.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EXCH-02.secunet.de (10.32.0.172) To EXCH-01.secunet.de (10.32.0.171) From: Jianbo Liu 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] [ 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] [ 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 Reviewed-by: Carolina Jubran Signed-off-by: Steffen Klassert --- 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