From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796283516; b=n+bYzxrzs3wdG+4/1Ho9AHhLfWNnqzCDStMoQqxp7//XLNziA9ugdYpA0UbbrXFN+gST AXTSwAYPN6fjk00o8BqnyyKbg2ox8sT694rPY2e+8hoJoThBG+YZxTuMrqkw9/CV3mT0N 71bau40wwy2+DS7zFyoVBuPA8XxaCGhzzCWkzutS0WpBOAw1uGV/cXirKx8rjco/UNxKa ogHJxo/tNcex6j/xX4tWoleulPwP9pf3jvuOfckk29Fh5S895J2bHJUoevxTFIXMJ0u46 k7mrhPkHrVgPpKQGxxmKAluLYsdM0McMiABaopO29jSlwJlQG/Oqg4RZC2VB7ajP7+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796283516; 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=KsbUuMgGTCocS7KPiglEPg2ssWhwtELdQJBduKQypuE=; b=imkARUsHp/QaUWagL+KNEC9cS9B6uOurwNy64d3qGlK3LEtABMaA46toEdk+XprKVjnZ 59dwl9/Zq2MQSuiWEmbCkxgdKyQobYXSRA2Xs37oV73MhWhzZWZ1pGPc69mPE5zdaV3nL WDkjVxz5fjC1lTQQzuAR+67D5Lwq64KTQVqsm3UxWGY10jhJyYJGE/1TJA0ZurecQorEO sbmIYagCQtnr4p/A9FkzBfGTn/huKZQSNzbseQDs1GCoXBPTWvQ9ucfFTlrPDZeU1C+Pi UmFAZP5zzUr4CsSwj2MGTimfLWcXRH+8c6GS5wHkjLSwN8yzGsaM4B4WjgK3DWgLQ/g== 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=uZZ7lqxK; arc=none smtp.remote-ip=62.96.220.36 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 01CD1606A322 for ; Thu, 3 Dec 2026 07:38:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 01CD1606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com 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=uZZ7lqxK Received: from localhost (localhost [127.0.0.1]) by mx1.secunet.com (Postfix) with ESMTP id 2C6472067F; Thu, 3 Dec 2026 08:38:34 +0100 (CET) X-Virus-Scanned: by secunet 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 nEtUs-baBW_k; Thu, 3 Dec 2026 08:38:33 +0100 (CET) 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 EB7C42085B; Thu, 3 Dec 2026 08:38:32 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com EB7C42085B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com; s=202301; t=1796283513; bh=KsbUuMgGTCocS7KPiglEPg2ssWhwtELdQJBduKQypuE=; h=Date:From:To:CC:Subject:References:In-Reply-To:From; b=uZZ7lqxKDIyBCWKuZpJwaYVecD54WbTA2OBo0gIq5repiZKxBFS2qXJhaHcUikQPq u3VDt1bD4xB8Oby+5fqn01F29ALtux9LKoSYmvGqa+3tIMhPuRoSLb/JbZ88dkoF2A bYTExTYEof1kKDb3XaRw3n4V2EV1M/pMFAxf669+t+1w45H156yfjqxM4rHXoPKts1 XEsEgK96XGFNcyUNusKbmQxCzvex3YNCEi8RKoqPKcMrclaRmJeKX8ybFxHHrrAVvJ u7pw9FYphrJCELeGkISEjvO1gDgQR+/PLB95f8BBLBP6Sb5RwmHVuSYlK6dzFfxjZ+ E7d92hhQMr4aA== 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:38:32 +0100 Received: (nullmailer pid 1069071 invoked by uid 1000); Thu, 03 Dec 2026 07:38:31 -0000 Date: Thu, 3 Dec 2026 08:38:31 +0100 From: Steffen Klassert To: CC: , , , , =?iso-8859-1?B?Suly6W15?= Jean Subject: Re: [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state Message-ID: References: <20261201124252.1163254-21-steffen.klassert@secunet.com> <179621715327.3146.499492792826823879@kernel.org> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <179621715327.3146.499492792826823879@kernel.org> X-ClientProxiedBy: EXCH-03.secunet.de (10.32.0.183) To EXCH-01.secunet.de (10.32.0.171) 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