Article View: linux.kernel.netdev
Article #26187Re: [PATCH 19/22] xfrm: Serialize device state teardown
From: Steffen Klassert
Date: Thu, 03 Dec 2026 07:24
Date: Thu, 03 Dec 2026 07:24
193 lines
17032 bytes
17032 bytes
Ccing the author of the patch.
On Wed, Dec 02, 2026 at 01:12:32PM +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: 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 fix for a NULL device in xfrm_dev_direct_output() only
> checks a local snapshot, so the NULL dereference is still reachable.
> - [Medium] xfrm_dev_state_flush() can return while a state on
> xfrm_state_dev_gc_list still has xso->dev == dev, its driver SA and…
>
> --
>
> 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. With that, only one
> caller unlinks the state and drops the netdev reference. The device flush
> now takes a state reference before collecting states from the device GC
> list, and xfrm_dev_offload_ok() rejects offloaded states that have 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;
> >
> > if ((!dev || dev == xfrm_dst_path(dst)->dev) &&
>
> [Severity: High]
> The commit message says this check stops xfrm_output() from using "the
> NULL device in xfrm_dev_direct_output()". Only the local snapshot of
> x->xso.dev is checked here, so is that window really closed?
>
> Nothing that runs after this check receives the snapshot. Take a tunnel
> mode packet offload state with !skb->sk. xfrm_output() calls
> xfrm_dev_direct_output(), which runs the LOCAL_OUT hooks and then loads
> the pointer again:
>
> 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);
> }
>
> The driver callback at the end of xfrm_dev_offload_ok() is also passed x
> rather than dev. For bonding, bond_ipsec_offload_ok()->bond_ipsec_dev()
> loads the pointer again and dereferences it without a NULL check. This
> happens for crypto offload as well:
>
> drivers/net/bonding/bond_main.c:bond_ipsec_dev() {
> struct net_device *bond_dev = xs->xso.dev;
> ...
> bond = netdev_priv(bond_dev);
> if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
> return NULL;
> ...
> }
>
> The bond_ipsec_dev() re-read predates this patch. It is the same NULL
> device use that the commit message says is handled.
Jianbo Liu already commented on this when the patch was posted
originally to the netdev list:
https://lore.kernel.org/ipsec/dba4f4d6-d710-4891-a0f6-5d7a3aae6c5e@nvidia.com/
Should be addressed with a separate fix.
>
> Now that xso->type stays set after detach, can this sequence oops?
>
> CPU1 (TX) CPU2 (NETDEV_DOWN/UNREGISTER)
> xfrm_dev_offload_ok()
> dev = READ_ONCE(x->xso.dev)
> /* non-NULL, check passes */
> xfrm_dev_state_flush()
> xfrm_dev_state_free()
> WRITE_ONCE(xso->dev, NULL);
> xfrm_output()
> xfrm_dev_direct_output()
> skb->dev = x->xso.dev; /* NULL */
> skb->dev->hard_header_len
>
> xfrm_dev_state_free() clears the pointer under x->lock and
> xfrm_state_dev_gc_lock. The TX path takes neither lock.
>
> A later patch in this series, "xfrm: Wait for an RCU grace period before
> freeing device state", adds synchronize_rcu() before
> xfrm_dev_state_free() in the flush. That makes the window smaller but
> does not close it. xso->dev is cleared after the grace period rather than
> unpublished before it.
>
> A TX user that holds a counted bundle dst taken before the delete can
> still enter xfrm_output() in a new RCU section after the grace period.
> Two examples:
>
> - a forwarded packet that is NF_QUEUE'd and reinjected later
> - a UDP sender that sleeps while copying user data after its route
> lookup
>
> Could dev be read once and passed down to xfrm_dev_direct_output() and
> the driver callbacks? Alternatively, it could be checked again at each
> use, or TX users could be kept from reaching x before xso->dev is
> cleared.
>
> > diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
> > index 9c101be250ee8..6f98c5c1a823a 100644
> > --- a/net/xfrm/xfrm_state.c
> > +++ b/net/xfrm/xfrm_state.c
>
> [ ... ]
>
> > @@ -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]
> Can xfrm_dev_state_flush() return while a state on
> xfrm_state_dev_gc_list still has xso->dev == dev, along with its driver
> SA and its netdev reference?
>
> This skip assumes the state GC already owns any state whose refcount is
> zero, and that xfrm_flush_gc() at the end will wait for it. However,
> xfrm_state_put() drops the refcount to zero before __xfrm_state_destroy()
> queues the state and schedules the work:
>
> xfrm_state_put()
> refcount_dec_and_test(&x->refcnt) /* refcnt is now 0 */
> __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);
>
> If the flush walks the dev GC list between those two points, it skips
> the state. xfrm_flush_gc() then calls flush_work() on a work item that is
> neither pending nor running, so it returns at once.
>
> Before this patch, the flush freed the device state of every matching
> entry regardless of its refcount. Here the GC later calls
> xdo_dev_state_free() on a device the flush has already reported as
> clean. For unregister, netdev_wait_allrefs_any() has to wait for the
> reference.
>
> The later patch "xfrm: Wait for an RCU grace period before freeing device
> state" only handles the case where the flush itself drops the last
> reference. This skip is still there at the end of the series.
>
> [ ... ]
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201124252.1163254-2-steffen.klassert%40secunet.com
Technical Details
Message-ID:
<axEZJOdNvY7qosxB@gauss4>Path:
mbox-mo1.pugleaf.net!news-lon.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376366-linux-kernel-netdev=dewy.ruReferences:
<20261201124252.1163254-20-steffen.klassert@secunet.com> <179621715233.3146.12822374373454454587@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!3.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376366-linux-kernel-netdev=dewy.ru From: steffen.klassert@secunet.com (Steffen Klassert) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH 19/22] xfrm: Serialize device state teardown Date: Thu, 3 Dec 2026 08:24:20 +0100 Organization: Usenet Network Message-ID: <axEZJOdNvY7qosxB@gauss4> References: <20261201124252.1163254-20-steffen.klassert@secunet.com> <179621715233.3146.12822374373454454587@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2403838"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:3N07G7QKVGd83Qn/5DTxAhLFQ/E= sha256:7Zo+oLM3T9se4M5uhpuVeZ+ey1+G7nzu1mXfds3EkkQ= sha1:NgsSPBy+s2fLFTxHcJ8GvMPvhHM= sha256:d3BR3SYoJP3xnDhSxhreUD9adAZQIRYQ+d00hvPONHk= Content-Disposition: inline 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 2476411785C; Thu, 3 Dec 2026 10:24:43 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.051 X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, 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=secunet.com 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 BTHzt9OP02l3; Thu, 3 Dec 2026 10:24:39 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376366-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=secunet.com X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 06878117817; Thu, 3 Dec 2026 10:24:28 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 595B8278EC; Thu, 3 Dec 2026 07:24:26 +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=1796282666; b=mMFpRzCslMTQUsUtv7bfvTLk6CUkxJpwl8NAV4IJlpoOHGipUJ2PmnUl+ijC4A14yaUm Lq9Z/19BWNXbhYVRRTUBuBFykTjhmjhv9kDy9NcCKKtCeF9SswMN+z+PIEb3psG9KK1+Q rrbcGXn5R/9EzT3UeO3ZiJOVe4WHn9gEu6lnBMte/XLRkpJ3nBdVVeibbUqf/I8BJDLnX VNBiSePU3Hx0M/YQlM/TKaeJIS7H5LzyCuTPBTFfcGIm/ceCBz9EIiClEpdTLjel7fpPC 6ZiIV5TRw1U6Fa57WxxEdovy37ulXFSw1byLxE3RGVBsF8j1DSJ7ospizGqPGUMSpsg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796282666; h=X-Original-To:Received:DMARC-Filter:Received:X-Virus-Scanned: Received:Received:DKIM-Filter:DKIM-Signature:Received:Received:Date: From:To:CC:Subject:Message-ID:References:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To:X-ClientProxiedBy; bh=S8UnObb7vQNt1Djbjgd2hnGPXw76mmGt3Gbil5+3pOE=; b=CdtnArYvbXuvAoqQpYFmpeLPwy5ZG6v03LNZfUIGjG4gVS00Qm4+LLlRp1mQS9rul6YU nd8vgZR8gqqKj9Nvoa3EfLHVYqmJsYqcOg2jdaFujAGlLMqcVXE1hmCEuL0nC1vD1QDMR +oo+AIw0M6aiTOM+frOSHAWOeeHvhedzZGzgqb+o8nEuapMy1MY9VznluLQZfMK6o8iWK BbPpVGyR5vZZGPiMuwbAh1qdNRLIKLGMR6TZk94YuZYO5FrJfUpj+/jf9POxfa4IwDDo1 zDjZ27oSRsU0HUvX1pMRzhBzH/pG31Etp8sSz7Rw8n253wquZNe2b1+jO86GhHXFnQw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=CVz1JmVk; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; 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 318A5606A322; Thu, 3 Dec 2026 07:24:26 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=CVz1JmVk X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796282665; b=RbbwRYqSt8+UeuuTdK9pkftJphyLi58oDyZFyeUROFbzQtWwIFbgI0HU/DUq2Q2KXLVZ pSMOCxJEt3Kad4YkHRqpAvABUUiwotho+l5k2z622HdWf8DiyBqd9ogN3gdhjteGv09Nn zVfPs1wxm0WtxHencVzvx30cGu90lrXezfKRltGRpAtTsLsip0R5g9ZAD8lFBECI4UgaH 0x5wLCfvU55KYFRxZ+dvs9d2I4qyelV/MsxAS4iMBIl+iZUF42YJlduIxTnXCAWRtaKV3 A5Hy+ScPDpH2JryjgvalcQgAlnDj8zy5Yf3cDZwzi4VJ+UBpRRmXfhx45cZL2b/7P1w== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796282665; h=DMARC-Filter:Received:X-Virus-Scanned:Received:Received:DKIM-Filter: DKIM-Signature:Received:Received:Date:From:To:CC:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To:X-ClientProxiedBy; bh=S8UnObb7vQNt1Djbjgd2hnGPXw76mmGt3Gbil5+3pOE=; b=lXzE5S+xgDBvdPc/lU9vfuhQRF1PhtP+vT/T+w1gci9NEu93cRrgTeH7zVNngOxxDcCf bT1hzd665VxdIo1s8ekTAa7PU3xfnKmT9FwfphQwwNcpkQOeg5R+fkfvudsLu2WP7GtTB ZpIQF7lB4wNgz9SBkh7u/99nvo5cmnNr6IxV4q3pPLrr5svqYq493VXWF7m4dJay/ZCbD BLTTg3hlZg6Ru15DGZEP/UUyIJ6oH1T34wDBOJbejpxxP5PdJJBBcXnxNdboqCiXKoumy qDzPRTe8SSBwvXVp6k7+5pgdoFNa07iLBVuqbB7WtTbTW1Vm2sP5Hft8X5Kv/qy2JiQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=CVz1JmVk; arc=none smtp.remote-ip=62.96.220.36 X-Received: from mx1.secunet.com (mx1.secunet.com [62.96.220.36]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D345606A32D for <netdev@vger.kernel.org>; Thu, 3 Dec 2026 07:24:24 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8D345606A32D X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com X-Received: from localhost (localhost [127.0.0.1]) by mx1.secunet.com (Postfix) with ESMTP id B6B0E2074F; Thu, 3 Dec 2026 08:24:22 +0100 (CET) X-Virus-Scanned: by secunet X-Received: from mx1.secunet.com ([127.0.0.1]) by localhost (mx1.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id E3qbUBChyjk6; Thu, 3 Dec 2026 08:24:21 +0100 (CET) X-Received: from EXCH-01.secunet.de (rl1.secunet.de [10.32.0.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.secunet.com (Postfix) with ESMTPS id 9F41420849; Thu, 3 Dec 2026 08:24:21 +0100 (CET) X-DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com 9F41420849 X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com; s=202301; t=1796282661; bh=S8UnObb7vQNt1Djbjgd2hnGPXw76mmGt3Gbil5+3pOE=; h=Date:From:To:CC:Subject:References:In-Reply-To:From; b=CVz1JmVkDQLh5ef5Z0gBQ7YEtKBYhwb8pnw9LZfS259dRMnPVDAJIACMDLNlMARSG h7s5AjuGrs8OTzFIa645DQ8t3GHd8YInesD6A5iNKSMqBFjx0L+mxbxwVI1eJ94hGz FiSK8FoO/D25qpmRHR+iZ21lW9d3vzlehRYa5bUfDTxznp34IRejtDV9kiYSlw4xcZ YIBXCqXXYSFcadaSVETJOR/4DzzlIC3mu6TpdrPUm3FZmvqydtZE1/rrPP2VEVsuWY T6IyFziGcU3kj6GRg5FftdJx5eqwCMJUna4qCFr5T1fPMpUQB3BQLEjGjr1LsQtN4Q QKg0blk3sIdkA== X-Received: from gauss4 (10.182.7.102) by EXCH-01.secunet.de (10.32.0.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 3 Dec 2026 08:24:20 +0100 X-Received: (nullmailer pid 1036189 invoked by uid 1000); Thu, 03 Dec 2026 07:24:20 -0000 X-CC: <davem@davemloft.net>, <kuba@kernel.org>, <herbert@gondor.apana.org.au>, <netdev@vger.kernel.org>, Jianbo Liu <jianbol@nvidia.com> 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 X-In-Reply-To: <179621715233.3146.12822374373454454587@kernel.org> X-ClientProxiedBy: EXCH-04.secunet.de (10.32.0.184) To EXCH-01.secunet.de (10.32.0.171)