Netdev List
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: <netdev-bot+sashiko@kernel.org>
Cc: davem@davemloft.net, kuba@kernel.org,
	herbert@gondor.apana.org.au, netdev@vger.kernel.org,
	"Jérémy Jean" <Jeremy.Jean@oss.cyber.gouv.fr>
Subject: Re: [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state
Date: Thu, 3 Dec 2026 08:38:31 +0100	[thread overview]
Message-ID: <axEcdwUu3rXcE5Ny@gauss4> (raw)
In-Reply-To: <179621715327.3146.499492792826823879@kernel.org>

Ccing the author of the patch.

On Wed, Dec 02, 2026 at 01:12:33PM +0000, netdev-bot+sashiko@kernel.org wrote:
> 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?

Jianbo Liu already commented on this when the patch was posted
originally to the netdev list:

https://lore.kernel.org/ipsec/4181b9e2-29d6-4bf1-8665-f96d243bd2ec@nvidia.com/

Same as the previous patch, should be addressed with a separate fix.

> 
> -- 
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201124252.1163254-2-steffen.klassert%40secunet.com

  reply	other threads:[~2026-12-03  7:38 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:41 [PATCH 0/22] pull request (net): ipsec 2026-12-01 Steffen Klassert
2026-12-01 12:41 ` [PATCH 01/22] xfrm: ipcomp: account full fragment pages in truesize Steffen Klassert
2026-12-01 13:36   ` bot+bpf-ci
2026-12-01 12:41 ` [PATCH 02/22] xfrm: esp: reset outer TCP GSO metadata on espintcp input Steffen Klassert
2026-12-01 12:41 ` [PATCH 03/22] xfrm: keep the mode module pinned until destroy_state returns Steffen Klassert
2026-12-01 12:41 ` [PATCH 04/22] xfrm: serialize SA activation with state deletion Steffen Klassert
2026-12-01 12:41 ` [PATCH 05/22] xfrm: keep old state references until migration deletion completes Steffen Klassert
2026-12-01 12:41 ` [PATCH 06/22] xfrm: hold migrated states through rollback Steffen Klassert
2026-12-01 12:41 ` [PATCH 07/22] xfrm: check inner family via skb_dst for small_ipv6 DF clearing Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  6:58     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 08/22] xfrm: account XFRMA_MARK in xfrm_sa_len() Steffen Klassert
2026-12-01 12:41 ` [PATCH 09/22] xfrm: replay: Fix late packet handling with ESN crypto offload Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:08     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 10/22] xfrm: fix IPv6 first-fragment protocol decode Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 11/22] xfrm: fix selector matching for IPv6 atomic fragments Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:15     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 12/22] xfrm: espintcp: read the socket error queue for MSG_ERRQUEUE Steffen Klassert
2026-12-01 12:41 ` [PATCH 13/22] xfrm: iptfs: force a dst reference before queueing an skb Steffen Klassert
2026-12-01 13:36   ` bot+bpf-ci
2026-12-01 12:41 ` [PATCH 14/22] xfrm: espintcp: select receive path by skb protocol Steffen Klassert
2026-12-01 12:41 ` [PATCH 15/22] net: xfrm: interface: restore dst_orig on every xfrm interface Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 16/22] xfrm: iptfs: make room before copying a continuation in reassembly Steffen Klassert
2026-12-01 12:41 ` [PATCH 17/22] xfrm/compat: pass on the translated length of a socket policy Steffen Klassert
2026-12-01 12:41 ` [PATCH 18/22] xfrm: Give GC list nodes their own storage Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-01 12:41 ` [PATCH 19/22] xfrm: Serialize device state teardown Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:24     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:38     ` Steffen Klassert [this message]
2026-12-01 12:41 ` [PATCH 21/22] Revert "xfrm: serialize state GC with device state flush" Steffen Klassert
2026-12-02 13:12   ` netdev-bot+sashiko
2026-12-03  7:59     ` Steffen Klassert
2026-12-01 12:41 ` [PATCH 22/22] xfrm: Serialize offload stats updates with x->lock Steffen Klassert
2026-12-02 16:06 ` [PATCH 0/22] pull request (net): ipsec 2026-12-01 Paolo Abeni

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=axEcdwUu3rXcE5Ny@gauss4 \
    --to=steffen.klassert@secunet.com \
    --cc=Jeremy.Jean@oss.cyber.gouv.fr \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=kuba@kernel.org \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    /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