Article View: linux.kernel.netdev
Article #24040Re: [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state
From: netdev-bot+sashiko
Date: Sun, 29 Nov 2026 02:14
Date: Sun, 29 Nov 2026 02:14
162 lines
14166 bytes
14166 bytes
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
Technical Details
Message-ID:
<179591848241.3135.5652486461526198891@kernel.org>Path:
mbox-mo1.pugleaf.net!news-can.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373340-linux-kernel-netdev=dewy.ruReferences:
<20261125013412.1123866-4-jianbol@nvidia.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-can.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373340-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev Subject: Re: [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Date: Sun, 29 Nov 2026 02:14:42 +0000 Organization: Usenet Network Message-ID: <179591848241.3135.5652486461526198891@kernel.org> References: <20261125013412.1123866-4-jianbol@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1082370"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:LFX484uAYy9V3+8OZugUiN1RDOU= sha256:8d6nE0D3VgfFk0gEhurgtHi/iA+ZDONitc8SUL+bd6U= sha1:mDyq4lfumHB76X/n688Fwoi3Jyw= sha256:qLGBsTNZebK9NDvbhXsxsXoW68lYrUOktTA8pNJ9koA= 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 CE8B5125F91; Sun, 29 Nov 2026 05:15:07 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org 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 SpJMOH1RmrFp; Sun, 29 Nov 2026 05:15:04 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-373340-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id CED99125F92; Sun, 29 Nov 2026 05:14:54 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 004632231E; Sun, 29 Nov 2026 02:14:49 +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=1795918484; b=UeXoMufswFtLR+TiuRDGVdMhEI9EpqsG5l/1qTEmBGvWappWPbBibh+Y8MaGVD1HSHUK t+YdSsbzJvEGK3QJWRD93d7+oifRV1lQC022L2zY8vhtSO2E8Nhl1guycNZOSi9Jy9qk0 P8/1Bd5X0O7IcLohQpLOcmgIQJT39/ayuB7j8VLcIumaKxvhQyQA4dG0RJi1tDyBlcgDK eWDHPAE1vDRk8yqF6Z0ndgB6IK5+mBoC/+XyhhaIAN7TlufT75PtanoViipThG4aRlkuU yZ5J7+MOdbflKuCXrTunUT9yL0jWtong1fs6qGLTiX9fqwwqdW5D95rYlaVNZM52lwg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795918484; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=iJDvne977J9hGYVcc2MQbJ+XSNWIZUtVo5KuGGHm0cM=; b=sYEDR49IlYQatXKaILymIP8nUd0B39atQKARSM8qDudSUUD2kByuIxH70d5pIpe0dYp2 zn7apbWaWQBpYUr/zrN5D1LBOPtnrEpPKY7LvBVXFXejEbGN6YxFV8Pd+Vx6c2W5sJf+h 5bJ/pK7cNEN6onyrM8kzxIcRRclW896G4SyDa3te5JVJFDxszoGksHZwVwMoWg8jKlVsa G8VCOEIE5wODJRB1go/5sO2b3qytA1pQcBjOelEsLVGfaW4J430bUfwD1VzICGBi/ZBi5 2tYq2+JnPu4rg7h4DBbIyCFwLaW2kfpObxQEAoMlirdbTCKKVlJMxQXk06Gtm+jMCgQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aOaqE1AE; 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 900AE606A692; Sun, 29 Nov 2026 02:14:44 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aOaqE1AE X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795918483; b=hCiUSJBvOZM0f7BLPL1jXd5z9CSihcZ0Yr0UEMCjjy16lUV3+GGZHRxR2qYtSLOIdRFS AqkoNoQOLtAwC1IgbfV4e74FBIaW4neoJNuwUKl3ThQoBfgzoptFjMSPQl8GWC2Duvez1 kPLOWv+71+EFSviDK4TmSdF4vPR6h/XN5b50jyY3AmMWT0hhrfELt4qotihSLo932dg9P hqJuRoAhLuuXZIA5NUbo077fGxaUYi8chvSFO5Uby0pZIq638an+U3Cb2+71HWMO2nrA+ Sx8pkFXKBi8jefRZvW8Mj01K0jAJXmtwOqQainvDb6fJysxHJn4XfnINlMh1ZBKFaFg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795918483; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=iJDvne977J9hGYVcc2MQbJ+XSNWIZUtVo5KuGGHm0cM=; b=aJepsuxSJSQRVXHaDqM9MbFvoj995su678Qc7UmLTitQ0Ug6FZr7TB/xv8Hs+JI9Hlv1 lwRjZ7W+7Nw1dA7ev4E8YsO+5t3X1Z2X5z7mWaCBxL/pcwqdXNO5gxhypeN7KNyUr5X3L qNCAljbsMCcY7MoiQ6hrANP9bFIw1Nv9iWiyKMDYP0unxRLpb4H6amRaB41/g7tj9xifW ZYGqhYpN/YdCqb+3SgLWKjlrvAFGdm6SS6gK8XMcuHh6DcZlr9Z1vvjI/1M8eGZmNaG4S uwPfNKr8ouqbPfCZVv3Cte3FpHhBNda1Y9fhcHn4+DCT0uOxn4ubDT6Tc/KBo49NzYA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aOaqE1AE; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFB23606A33F; Sun, 29 Nov 2026 02:14:43 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id D52651F0089A; Sun, 29 Nov 2026 02:14:42 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795918483; bh=iJDvne977J9hGYVcc2MQbJ+XSNWIZUtVo5KuGGHm0cM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=aOaqE1AEkOqzBJBKh16Ug5vM70de357aRrBq2upfcYaXoi0OlqDtTXL7/wCTDop6D eZLJ2kI6UUY5fZwDNAPvwHdD4PMaTJxvamu+kAQ5DB4FZ55oRtlVMYctPmWYjagLTz CFOzTbIjGYVdeeQ+qK7eF9sOs1+Z7R4BWbRVHXqQ6MusBBLKPMb2nquvlGSPQE3Jlu 2chFZ6y0p1YO42LC2Gz5n/z8QlXI1CVB4lDSK6deTZtOk9W73TTT55f5v9dA+x95lH 1dw6/ejkMuEQozA6N7uvtsO7Q+S8Na2+VL5h9NveVrxz2hG5a/LFDkpUKKnOtbSGv/ 7JthrdMi0O2SQ== X-Cc: netdev@vger.kernel.org, ipsec@lists.linux.dev, steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, fw@strlen.de, linux-kernel@vger.kernel.org, cjubran@nvidia.com X-In-Reply-To: <20261125013412.1123866-4-jianbol@nvidia.com> X-sashiko-severity: Medium 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