mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: jianbol@nvidia.com
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
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	[thread overview]
Message-ID: <179591848241.3135.5652486461526198891@kernel.org> (raw)
In-Reply-To: <20261125013412.1123866-4-jianbol@nvidia.com>

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

  reply	other threads:[~2026-11-29  2:14 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  1:34 [PATCH ipsec 0/4] xfrm: Fix device state teardown races Jianbo Liu
2026-11-25  1:34 ` [PATCH ipsec 1/4] xfrm: Give GC list nodes their own storage Jianbo Liu
2026-11-29  2:14   ` netdev-bot+sashiko
2026-11-25  1:34 ` [PATCH ipsec 2/4] xfrm: Serialize device state teardown Jianbo Liu
2026-11-29  2:14   ` netdev-bot+sashiko
2026-11-25  1:34 ` [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state Jianbo Liu
2026-11-29  2:14   ` netdev-bot+sashiko [this message]
2026-11-25  1:34 ` [PATCH ipsec 4/4] Revert "xfrm: serialize state GC with device state flush" Jianbo Liu
2026-11-29  2:14   ` netdev-bot+sashiko

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=179591848241.3135.5652486461526198891@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=cjubran@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=jianbol@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    /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

all inboxes | Powered by JetHome®