🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25798

Re: [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state

#25798
From: netdev-bot+sashiko
Date: Wed, 02 Dec 2026 13:12
245 lines
17322 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 (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] xfrm: race in xfrm_dev_state_flush() misses in-flight state
  destruction
- [Low] The synchronize_rcu() this patch adds to xfrm_dev_state_flush()
  (net/xfrm/xfrm_state.c line 1041) always runs with RTNL held.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is the same
  bug class on another caller.
- [Low] Documentation/networking/xfrm/xfrm_device.rst (lines 190-200)
  describes the xdo_dev_state_delete()/xdo_dev_state_free() sequence for…

--

xfrm: Wait for an RCU grace period before freeing device state

This commit removes the xfrm_dev_state_free() call that ran immediately
after xfrm_state_delete() in xfrm_dev_state_flush(). It adds a single
synchronize_rcu() before the collected device states are freed. The goal
is to enforce xdo_dev_state_delete() -> synchronize_rcu() ->
xdo_dev_state_free() ordering on the device flush path.

> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 6f98c5c1a823a..92c3631a43bf7 100644
> --- a/net/xfrm/xfrm_state.c
> +++ b/net/xfrm/xfrm_state.c

[ ... ]

> @@ -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: Low]
Would synchronize_net() be a better fit here than synchronize_rcu()?

As far as I can tell, every caller of xfrm_dev_state_flush() runs from a
netdev notifier with RTNL held:

  xfrm_dev_event()->xfrm_dev_down()          (NETDEV_DOWN)
  xfrm_dev_event()->xfrm_dev_unregister()    (NETDEV_UNREGISTER)
  bond_master_netdev_event()                 (NETDEV_UNREGISTER)

When flush_list is non-empty, RTNL stays held for a full normal grace
period. That happens whenever offloaded states still have references, for
example from policy bundles. It happens once per flushed device, including
during batched unregister and netns teardown.

Before this patch, xfrm_flush_gc() could already wait on the GC worker's
grace period, so this adds at most one more grace period per flush.

synchronize_net() gives the same ordering but uses the expedited variant
when called in this context:

net/core/dev.c:synchronize_net() {
	might_sleep();
	if (from_cleanup_net() || rtnl_is_locked())
		synchronize_rcu_expedited();
	else
		synchronize_rcu();
}

The commit message discusses the cost ("using a single grace period for
the whole flush") but not the RTNL context.

[Severity: High]
This isn't a bug introduced by this patch, but does
bond_ipsec_del_sa_all() in drivers/net/bonding/bond_main.c need the same
ordering?

The commit message says the xdo_dev_state_delete() -> synchronize_rcu()
-> xdo_dev_state_free() ordering is required. On an active slave change,
bond_change_active_slave()->bond_ipsec_del_sa_all() calls the two slave
ops one right after the other:

drivers/net/bonding/bond_main.c:bond_ipsec_del_sa_all() {
    ...
		spin_lock_bh(&ipsec->xs->lock);
		ipsec->xs->xso.real_dev = NULL;
		/* Don't double delete states killed by the user. */
		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 mlx5 slaves, mlx5e_xfrm_del_state() only calls xa_erase_bh() on
ipsec->sadb. mlx5e_xfrm_free_state() then calls kfree(sa_entry).

Meanwhile, the RX path reads sa_entry under RCU:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_rxtx.c: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();
    ...
}

Suppose NAPI on the old slave loaded sa_entry before the xa_erase_bh().
Could it then dereference sa_entry->x after the kfree() and call
xfrm_state_hold() on freed memory?

In-flight TX on the old slave seems to have the same exposure.
mlx5e_ipsec_handle_tx_skb() uses x->xso.offload_handle and calls
sa_entry->set_iv_op(). Bonding doesn't clear offload_handle before
bond_ipsec_add_sa_all() reprograms it.

States with km.state == XFRM_STATE_DEAD skip the delete here but are
still freed immediately. Their earlier slave delete went through
xfrm_dev_state_delete()->bond_ipsec_del_sa() without a grace period
before this free.

The window is narrow, because mlx5e_xfrm_free_state() runs firmware
commands first. Still, nothing separates the two ops with a grace period.

>  	hlist_for_each_entry_safe(x, tmp, &flush_list, gclist) {
>  		hlist_del_init(&x->gclist);
>  		xfrm_dev_state_free(x);

[Severity: Low]
This is a pre-existing issue, but Documentation/networking/xfrm/
xfrm_device.rst doesn't say that the core provides an RCU grace period
between xdo_dev_state_delete() and xdo_dev_state_free():

  Later,
  xdo_dev_state_free() and xdo_dev_policy_free() are called from a garbage
  collection routine after all reference counts to the state and policy
  have been removed

  As a netdev is set to DOWN the XFRM stack's netdev listener will call
  xdo_dev_state_delete(), xdo_dev_policy_delete(), xdo_dev_state_free() and
  xdo_dev_policy_free() on any remaining offloaded states.

The "after all reference counts ... have been removed" wording also
doesn't match this loop. Here xfrm_dev_state_free() runs while the flush
still holds the reference it took with refcount_inc_not_zero(), and
xfrm_state_put() is only called afterwards.

Both gaps predate this patch. xfrm_state_gc_task() already called
synchronize_rcu() before xfrm_state_gc_destroy(), and the old flush path
already freed device state while holding a reference.

Since this patch extends the grace-period guarantee to the flush path,
could the documentation describe it for driver authors?

[ ... ]

> @@ -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]
Can removing this call let xfrm_dev_state_flush() return before
xdo_dev_state_free() has run for a state it just deleted?

After this change, the first loop drops its reference with
xfrm_state_put(). It then relies on one of two things to free the
device state before returning:

  - the xfrm_state_dev_gc_list scan picking x up via
    refcount_inc_not_zero(), or
  - xfrm_flush_gc() waiting for the GC worker.

Suppose another CPU holds the last reference to x while the flush runs,
for example from an skb secpath, an xfrm_dst, or a concurrent lookup:

  CPU0 (flush)                      CPU1
  xfrm_state_delete(x)
  xfrm_state_put(x)  /* refcnt 1 */
                                    xfrm_state_put(x)
                                      refcount_dec_and_test() -> 0
  scan xfrm_state_dev_gc_list
    refcount_inc_not_zero() fails
    x is skipped
  xfrm_flush_gc()
    flush_work() returns, nothing
    queued yet
                                      __xfrm_state_destroy(x)
                                        hlist_add_head(&x->gclist)
                                        schedule_work()

__xfrm_state_destroy() queues x on xfrm_state_gc_list and calls
schedule_work() without xfrm_state_dev_gc_lock. Since it can run from
softirq, it can't take xfrm_state_gc_mutex either. Nothing orders it
against the scan.

The same applies if xfrm_state_gc_task() is already running and has done
hlist_move_list() before x is queued. In that case flush_work() only
waits for that instance.

Before this patch, the synchronous xfrm_dev_state_free() after
xfrm_state_delete() covered every state found in the bydst hash, no
matter who held the last reference.

The effect of returning early depends on the device:

  - Bond, NETDEV_UNREGISTER: unregister_netdevice_many_notify() calls
    bond_uninit(), which destroys bond->ipsec_lock.
    xfrm_state_gc_task() can later reach bond_ipsec_free_sa() and take
    that destroyed mutex.

  - mlx5, NETDEV_DOWN: if mlx5e_xfrm_free_state() hasn't run yet,
    mlx5_eswitch_unblock_ipsec() is still pending, so eswitch mode
    changes can fail with -EBUSY.

  - mlx5, later teardown: mlx5e_ipsec_cleanup() could free priv->ipsec
    before mlx5e_xfrm_free_state() dereferences sa_entry->ipsec.

The later revert of "xfrm: serialize state GC with device state flush"
in this series removes xfrm_state_gc_mutex. It doesn't seem to close
this window either.

How is the flush meant to wait for offloaded states whose last reference
is dropped concurrently with the dev_gclist scan?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201124252.1163254-2-steffen.klassert%40secunet.com
Technical Details
Message-ID: <179621715327.3146.499492792826823879@kernel.org>
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!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-375802-linux-kernel-netdev=dewy.ru
References: <20261201124252.1163254-21-steffen.klassert@secunet.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!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-375802-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing
 device state
Date: Wed, 02 Dec 2026 13:12:33 +0000
Organization: Usenet Network
Message-ID: <179621715327.3146.499492792826823879@kernel.org>
References: <20261201124252.1163254-21-steffen.klassert@secunet.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1976984"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:oRCe1Mpi0dO59tMWaDnl2nYmceg= sha256:EbvabsSRE70LDPqMMa9Nbl+17FMWdhI62qWwJ6pVolI=
	sha1:VQZ7u9qAsmCWcZ6Ynv+mrXHXVuc= sha256:c+ySb/9mSyPzOwHG61gdFMq3qLo+j6ezAZ2/yaLvrd4=
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 ADADB117649; Wed,  2 Dec 2026 16:12:52 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.4
X-Spam-Status: No, score=-3.4 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, URIBL_BLOCKED=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 5BeslYPfBOzr;
	Wed,  2 Dec 2026 16:12:49 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-375802-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id DA619117643; Wed,  2 Dec 2026 16:12:40 +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 5A04C2BF18; Wed,  2 Dec 2026 13:12:35 +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=1796217155;
	b=OsYKFOUxkOmR8iuZiKBpJud5IgkVw67DNcBCZ4i2V3FvI0U8W1rUM/s4fwMTiuIHW6ev
	 bbg+TrvmCFzcVK0vmerSjnqkSGskRHeZSClwg0hjhK5ivO1XPqudtIFXQLNGL5DZ9TGHD
	 yWJgtRobtHVGAh7dkJd0WS8VHewiKrTQO8vsC/kXPAUen1j1GKiomFvdkFlADHVAnwIiH
	 IxWoBBs+QIo96J3NkkQWGw/98a8bADX/kiUtKSlOMEa3wwIz8qTOUH1ZSMBhAwnCjHbuD
	 nXXXStqa7phXd866aNezwlX4N2jmqbCNEXXEvBml60XvjsCMFQhEQdrKte76vgaWpfA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796217155;
	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=GRBZFttLVRjyKHq5Oh3Cj8rJmBmAivYGrKE8btLZblE=;
	b=0xJjN78oPll4aMeheE0voi+vaqU7zIBJsGVyITqtiKUYGqykx9RbH40zqUxzPepCHqsS
	 xDGCRDmQfcZvD3mqfZCsGzmGUlLsJkUUcJ7e7c9rmfzq53qwYtWmyDXYfx5Qk3f8fmoG0
	 ORIvca7WabQwEqL75j8HlosuySrxScM7Yj04HVuCwA6a0TveId5pwL9QoR36u2D4yfN2M
	 0eDYpUh93uDYriy265I6jGntUR6PiZw5xOYWiIFyK8K9v8TTkywnqibUvXTal0bx49UK6
	 j/IetB1RcsGKHIAChZZBSjdiWJR2QVlzAB3Ge36fyxByu36W0HFVaqas9Zx3CrHMVQQ==
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=acQIcsYc;
	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 008A9606A332;
	Wed,  2 Dec 2026 13:12:35 +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=acQIcsYc
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=1796217154;
	b=WuXbA/c7+gKb8SzwVBGbLD8PrqzN5WDz8OD+Qk7/rW4w6i0/gEqusC6NqO2nYTSNNZ4K
	 BRygEH+oXhGiA8UjhQldAmXA3AvI31kBPBUzK1ourj7l6/uqvjbBQXrLTpzdt87MgbPHB
	 Jkb2mxpsjNKEqsL2xslcs5YFVe8M/0xmd5mUW+wXy7bmxYps1qO5nDJSnLB+zH91yS0AL
	 w2S54og+SDfvE/ppb5+RHK//0mm5iOwypPQCUD6HH9/M5va5dr/TmMtuJ5AvV50KEFYnl
	 v+yqs2x7+ncvZz6oUxb+AAhyFIWKQNDo/k0y+elWUPw+y12FZfr+HTIzYTzon3fTyPg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796217154;
	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=GRBZFttLVRjyKHq5Oh3Cj8rJmBmAivYGrKE8btLZblE=;
	b=DdBvF1QC3UtN9zadLlh7nSdDiLztQPv766pzNl4fJ8McJrjWcCf/sKoWl+p0wQVRVa+d
	 V6nIi7mGluWPpH2Hp93VVNI2uMd2H49xdCQ44K2meHxWJr67SlNyGjM7659rvQ1GPPra+
	 M6ESu2GpX91HXeBbfl16NRVM2ABQyQxvaiW8VVzs0fDNJtikG1ayMoxsl+ip881b6N8qE
	 1cFJM0/9rGiTjYHpjeUN/nQKLUnYd0h+nwM7FqeaDIazzOTTiqCmV0NGl37jVrwFZCiyb
	 ynxFz4+GXgXEHvCEM+Y1i3FPSjeaVO1vSXB3lHXPXbLReysMjyijFj01Gae1aE+W/5w==
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=acQIcsYc;
	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 232C1606A323
	for <netdev@vger.kernel.org>; Wed,  2 Dec 2026 13:12:34 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id B10751F00896;
	Wed,  2 Dec 2026 13:12:33 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796217154;
	bh=GRBZFttLVRjyKHq5Oh3Cj8rJmBmAivYGrKE8btLZblE=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=acQIcsYcsFXfL+uXtWIVNP8rzklYZ1cj5xlIpGqD8pl5drEljweVr7SW8ZxNy9qDT
	 AbqiEMHEDZbWzEB93NLtP2l6YzDECLVuV1avxb0hcTPQgWyR9jXIbOvdii8ZLzH+a+
	 bkya708Vdb4dU2GjAt/X3/sP6FaLDa2G6nBWW/HjYLV2MEaJHxAEE1jVbckrgjMfAH
	 JiTw4Ijyo7Fz1MDIRQEgUt6AFOoUfU3pNEufdWnXehht4O73xdnLVQp74qoZ6pIIP0
	 Fljq2TtenXanbDLXKWUlNy//CTyxEAulGZg+OXGNnNickIZTMLj8QvfOOOxBo9ThNw
	 ePbi0A5PF7KPA==
X-Cc: davem@davemloft.net, kuba@kernel.org, herbert@gondor.apana.org.au,
 netdev@vger.kernel.org
X-In-Reply-To: <20261201124252.1163254-21-steffen.klassert@secunet.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