Article View: linux.kernel.netdev
Article #24041Re: [PATCH ipsec 2/4] xfrm: Serialize device state teardown
From: netdev-bot+sashiko
Date: Sun, 29 Nov 2026 02:14
Date: Sun, 29 Nov 2026 02:14
264 lines
17300 bytes
17300 bytes
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
Technical Details
Message-ID:
<179591848106.3135.4138564500710245659@kernel.org>Path:
mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373339-linux-kernel-netdev=dewy.ruReferences:
<20261125013412.1123866-3-jianbol@nvidia.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373339-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev Subject: Re: [PATCH ipsec 2/4] xfrm: Serialize device state teardown Date: Sun, 29 Nov 2026 02:14:41 +0000 Organization: Usenet Network Message-ID: <179591848106.3135.4138564500710245659@kernel.org> References: <20261125013412.1123866-3-jianbol@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1082422"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:MbYE4Ff7/1SawEG7WT6HaCOlZ78= sha256:KHwtLE06KBxNG1htCkflUjiWUPAigRX8pjtgYO0Vo/I= sha1:H511KAmEv3mdXxqjztaICwdYZG4= sha256:EXmSDTW4kFDJ+jXuyBCjS40SImDIhBXEfz/1dHO1E9c= 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 29A0D125F91; Sun, 29 Nov 2026 05:15:10 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.501 X-Spam-Status: No, score=-3.501 tagged_above=-9999 required=6.31 tests=[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 7LqI60TppTKn; Sun, 29 Nov 2026 05:15:06 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-373339-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 sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74]) by mail.dewy.ru (Postfix) with ESMTPS id 81570125F90; Sun, 29 Nov 2026 05:14:58 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sto.lore.kernel.org (Postfix) with ESMTP id 85E5E601998; Sun, 29 Nov 2026 02:14:57 +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=1795918488; b=oKbeGbVmgpw3vGlH2nd9DMem6naY5KHLmmIJFTZ+XqLMqmoeWlFIF61knmSwu5IwZafR K5hS+fjn1uuXiSp2OaEiyqPh69Goz7oH/FFbTL7DoQPR8XZ4o8gCtDLJW1+mEZ2/fMZfV VyThry/ZkPJvdsvXUpgUg6HG1Wkb1ljlETLIPTKGKoNAYhQlmNrWhq09jUKNfLBG/5uqW KI1PBSmCZeL4Lem4CKm/wHxqJ051E2yBHSqHKV/cDvB1YGwfJ2hFR+BU6TU0FU8SCWDDl 70psyCgoJBO+Xy4cKUgAJepw4lUeDS96raNyb7vvK5xKdodijyCNwtK/5ZbLGsOFEjQ== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795918488; 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=jiHKV8W03e4Lm0snVf3xzBY+9iVZZDNw4J+MrcBxIvw=; b=qbUkno4Hxgr2uW1NjdrtNAO71GvQz1QqEXmPmVhFYDx1TnAjcLLTyNpW7yb78v1hLTou 5xwOGxBib2vAYvCgm4K0PtI1BZFEieH7urNmvbf2WjDtDdCvAN1WaufW3Vs4NTJuwxLpf CfXofG8nj8G9FE/cIhaFcO8AHJD8tM/t90cUXQ52w3ToCeoBAB+8UR16UEqwsDAR6Pj9r BdcN98q1ThFXsbgFBhcAl9mYAt/29s/awZohgcfqQGSLwe06suw745l3CRzavS/7c8D25 6WfI41th+sH3+FF8gnezTEUnRYnV/pCC90oqgsHZL9ZGXC2RFVKYApuIPhSidkjfnmA== 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=jb4E1owy; 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 431D9606A681; Sun, 29 Nov 2026 02:14:48 +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=jb4E1owy 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=1795918482; b=DPLBSX/O2a31b0JYpVz9/Vjii29U9lIlPw9G1upIoemcrmOfeBPZEDNET/jf/GucCuIo obGfCxg5IBvUPfBJn/8drB529GxdErKjVBP5HvV1ZgQ0Y/iEmJoLoFjqgf7CE6G736htN EI9pAtKit/5K2yTNbzTnj3JozF8FwmBEtJlGwzLBVJOjft44gEjsq4oTAN0DQCQx7BFAk PzFbF0Bsi8QkT7TrjRCOuv3prrOKxH7IaQkPWl7jNq0/OxsE9pS+asPArMQnwU5Tpnkki N/F310Po3kgrBCW23AwyB+NI5UFa+mDV1Zj5tVoBfR7kvrqXS+wVovxYalsabPD6XYA== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795918482; 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=jiHKV8W03e4Lm0snVf3xzBY+9iVZZDNw4J+MrcBxIvw=; b=lO4WEEXqq306lctUqF/yRg6VmObSlJRFGKhXO5b/tQ3rwGdwMKx+KpBAdd3166BvFuQz D/XizytEi1Q0e7v8eryzcNXH2ppsZzKlWcBD9Cv9InMHgUC0m0hYi+gZRepkKGc4sbgpI bbljkbTP6VMzlic2aMgOAfc7SqEC9eRw0cdXWCbGG8ZNdVQfElKj2iVRNerhCKhNOAnD/ Qd43brd5lYCbT1HjxYSxyQlOhA0wP79PyWN4IMM6Az1Fv+AYicdJWxtU8GcLeNhkxe0Uq cEMURgU+vA/hvEDYwNr6Xlw0d3YTUauzeQW3o96oiCWlrxr9QQRPeIB4Nw6S5YilqCw== 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=jb4E1owy; 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 5EE8A606A32F; Sun, 29 Nov 2026 02:14:42 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 806391F000C1; Sun, 29 Nov 2026 02:14:41 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795918482; bh=jiHKV8W03e4Lm0snVf3xzBY+9iVZZDNw4J+MrcBxIvw=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=jb4E1owygwFQleLALl63Yr6QIaChIleFE9I0iLDkbqe3UaB0tlOTGCU7UuNY+6R6S uxA7aq20UTuJ9Z2D2tFA23Wsb58tT27B4PfQC5jWF3iMttXSliIPJqUEmsxas+cuqZ rl6U1DHhaBX0/hAv1u2pi43bQz8E5xua38kJkoBVp97EDw3lLZqz0bEnWAVUvlWhGc PS/wJu4kmxWWkusZtqgmqeo4O2UpcNIYwU6JXcaFhp6ToIlHyMeqpIFGd41lj2Nr4w 0C/Mx27GqJNiGVRJsttvcIdtKB2YJTf1vvjggzjBlR8Aox9R+tkbf1GiC4AK6NEkh5 v7ZrgueQYXstg== 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-3-jianbol@nvidia.com> X-sashiko-severity: High 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