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 2/4] xfrm: Serialize device state teardown
Date: Sun, 29 Nov 2026 02:14:41 +0000 [thread overview]
Message-ID: <179591848106.3135.4138564500710245659@kernel.org> (raw)
In-Reply-To: <20261125013412.1123866-3-jianbol@nvidia.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · Low: 0
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).
- [High] The new `!dev` rejection in xfrm_dev_offload_ok() does not stop
xfrm_dev_direct_output() from using a NULL device, although the commit…
- [Medium] xfrm_dev_state_flush() now skips states on
xfrm_state_dev_gc_list whose refcount is already zero (`if…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch's
design depends on it.
--
xfrm: Serialize device state teardown
This commit makes xfrm_dev_state_free() claim the device by clearing
xso->dev under x->lock and xfrm_state_dev_gc_lock. Only one caller then
unlinks the state and calls xdo_dev_state_free(). xfrm_dev_state_flush()
now takes a state reference before moving entries off the device GC list,
and xfrm_dev_offload_ok() rejects offloaded states with no device.
Link: https://lore.kernel.org/netdev/20260908224854.1591115-1-kuba@kernel.org/
> diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c
> index f153bf695b9dd..3bbb970cd337f 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;
[Severity: High]
The commit message says this check stops xfrm_output() from using "the
NULL device in xfrm_dev_direct_output()". Does it cover that case?
The !dev test only looks at the local READ_ONCE() copy, and that copy is
never passed back to the caller. For a tunnel mode packet offload state
with !skb->sk, xfrm_output() does:
net/xfrm/xfrm_output.c:xfrm_output() {
...
if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) {
if (!xfrm_dev_offload_ok(skb, x)) {
...
if (x->props.mode == XFRM_MODE_TUNNEL && !skb->sk)
return xfrm_dev_direct_output(sk, x, skb);
...
}
xfrm_dev_direct_output() then runs the LOCAL_OUT netfilter hooks. After
that it reads x->xso.dev again with a plain load and no NULL check:
net/xfrm/xfrm_output.c:xfrm_dev_direct_output() {
...
err = skb_dst(skb)->ops->local_out(net, sk, skb);
if (unlikely(err != 1))
return err;
...
skb->dev = x->xso.dev;
__skb_push(skb, skb->dev->hard_header_len);
return dev_queue_xmit(skb);
}
Suppose xfrm_dev_state_free() runs WRITE_ONCE(xso->dev, NULL) on another
CPU between the check and that read. Can skb->dev->hard_header_len then
dereference NULL?
In this commit the window looks wide. The first loop of
xfrm_dev_state_flush() calls xfrm_dev_state_free() right after
xfrm_state_delete(), while forwarded packets on other CPUs can still be
sending on the bundle:
err = xfrm_state_delete(x);
xfrm_dev_state_free(x);
The later commit in the series, "xfrm: Wait for an RCU grace period
before freeing device state", narrows the window but does not seem to
close it. synchronize_rcu() only waits for readers that were already
running when the grace period started.
For example, take a forwarded skb held by NFQUEUE in FORWARD or
POST_ROUTING. It still has a refcounted bundle dst that pins x. It can be
reinjected through nf_reinject() in a new RCU section after x has been
deleted. xfrm_output() does not check km.state on the packet offload path.
Would it be more robust to pass the checked dev into
xfrm_dev_direct_output()? Another option is to READ_ONCE() it there,
check for NULL and drop the packet.
> diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> index 3eb012ed86576..8465ae0647427 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)
> {
[ ... ]
> + 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);
> }
[Severity: High]
This isn't a bug introduced by this patch, but the new locking scheme
depends on it. The commit message says "x->lock also keeps xso->dev
stable for the stats updates that hold it". However, not every stats
caller holds x->lock.
copy_to_user_state() in net/xfrm/xfrm_user.c does this with only
xfrm_cfg_mutex held:
if (x->xso.dev)
xfrm_dev_state_update_stats(x);
There are two ways to reach it. One is xfrm_get_sa() ->
xfrm_state_netlink() -> dump_one_state(), with just a state reference.
The other is xfrm_notify_sa(), after xfrm_del_sa() has already put the
state on xfrm_state_dev_gc_list.
The mlx5 callback accepts xfrm_cfg_mutex as enough protection and reads
x->xso.dev again:
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c:mlx5e_xfrm_update_stats() {
struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
...
struct net *net = dev_net(x->xso.dev);
...
lockdep_assert(lockdep_is_held(&x->lock) ||
lockdep_is_held(&net->xfrm.xfrm_cfg_mutex) ||
lockdep_is_held(&net->xfrm.xfrm_state_lock));
...
}
xfrm_dev_state_flush() runs from the netdev notifier and takes neither
xfrm_cfg_mutex nor xfrm_state_lock around xfrm_dev_state_free(). That
function clears xso->dev and then calls mlx5e_xfrm_free_state(), which
does kfree(sa_entry).
Could an XFRM_MSG_GETSA, or the notification sent for XFRM_MSG_DELSA,
race with NETDEV_DOWN and hit dev_net(NULL)? Could it use sa_entry after
it has been freed?
With this patch the NULL write now happens before the driver free, which
seems to make the dev_net(NULL) case more likely. The synchronize_rcu()
added later in the series won't help here, because this reader is not in
an RCU read-side section.
> @@ -974,6 +982,7 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali
[ ... ]
> @@ -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;
[Severity: Medium]
What happens to a state whose last reference is dropped at the same time
as this flush?
xfrm_state_put() does refcount_dec_and_test() first. Only after that does
__xfrm_state_destroy() queue the GC work:
net/xfrm/xfrm_state.c:__xfrm_state_destroy() {
...
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);
}
The putter could be preempted or interrupted after the refcount reaches
zero but before schedule_work(). One example is a preemptible netlink
handler dropping its xfrm_state_lookup() reference. The flush would then
see refcount 0 here and skip the state.
The xfrm_flush_gc() at the end is only flush_work(&xfrm_state_gc_work),
so it does not wait for work that has not been queued yet.
In that case xfrm_dev_down() or xfrm_dev_unregister() could return while:
- xso->dev still points at the device
- the netdev_hold() reference is still held
- xdo_dev_state_free() has not run yet
The driver callback would run later from the GC worker, after the flush
has already returned.
Before this patch the flush called xfrm_dev_state_free() on every matching
dev_gc_list entry regardless of refcount, serialized with GC by
xfrm_state_gc_mutex. Is this gap intended? It also appears to remain at
the end of the series.
> + 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);
> }
[ ... ]
> + 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;
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com
next prev parent 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 [this message]
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
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=179591848106.3135.4138564500710245659@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