From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796217154; b=WuXbA/c7+gKb8SzwVBGbLD8PrqzN5WDz8OD+Qk7/rW4w6i0/gEqusC6NqO2nYTSNNZ4K BRygEH+oXhGiA8UjhQldAmXA3AvI31kBPBUzK1ourj7l6/uqvjbBQXrLTpzdt87MgbPHB Jkb2mxpsjNKEqsL2xslcs5YFVe8M/0xmd5mUW+wXy7bmxYps1qO5nDJSnLB+zH91yS0AL w2S54og+SDfvE/ppb5+RHK//0mm5iOwypPQCUD6HH9/M5va5dr/TmMtuJ5AvV50KEFYnl v+yqs2x7+ncvZz6oUxb+AAhyFIWKQNDo/k0y+elWUPw+y12FZfr+HTIzYTzon3fTyPg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796217154; 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=GRBZFttLVRjyKHq5Oh3Cj8rJmBmAivYGrKE8btLZblE=; b=DdBvF1QC3UtN9zadLlh7nSdDiLztQPv766pzNl4fJ8McJrjWcCf/sKoWl+p0wQVRVa+d V6nIi7mGluWPpH2Hp93VVNI2uMd2H49xdCQ44K2meHxWJr67SlNyGjM7659rvQ1GPPra+ M6ESu2GpX91HXeBbfl16NRVM2ABQyQxvaiW8VVzs0fDNJtikG1ayMoxsl+ip881b6N8qE 1cFJM0/9rGiTjYHpjeUN/nQKLUnYd0h+nwM7FqeaDIazzOTTiqCmV0NGl37jVrwFZCiyb ynxFz4+GXgXEHvCEM+Y1i3FPSjeaVO1vSXB3lHXPXbLReysMjyijFj01Gae1aE+W/5w== 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=acQIcsYc; arc=none smtp.remote-ip=100.103.45.18 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 232C1606A323 for ; Wed, 2 Dec 2026 13:12:34 +0000 (UTC) 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=acQIcsYc Received: by smtp.kernel.org (Postfix) with ESMTPSA id B10751F00896; Wed, 2 Dec 2026 13:12:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796217154; bh=GRBZFttLVRjyKHq5Oh3Cj8rJmBmAivYGrKE8btLZblE=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=acQIcsYcsFXfL+uXtWIVNP8rzklYZ1cj5xlIpGqD8pl5drEljweVr7SW8ZxNy9qDT AbqiEMHEDZbWzEB93NLtP2l6YzDECLVuV1avxb0hcTPQgWyR9jXIbOvdii8ZLzH+a+ bkya708Vdb4dU2GjAt/X3/sP6FaLDa2G6nBWW/HjYLV2MEaJHxAEE1jVbckrgjMfAH JiTw4Ijyo7Fz1MDIRQEgUt6AFOoUfU3pNEufdWnXehht4O73xdnLVQp74qoZ6pIIP0 Fljq2TtenXanbDLXKWUlNy//CTyxEAulGZg+OXGNnNickIZTMLj8QvfOOOxBo9ThNw ePbi0A5PF7KPA== Subject: Re: [PATCH 20/22] xfrm: Wait for an RCU grace period before freeing device state From: netdev-bot+sashiko@kernel.org To: steffen.klassert@secunet.com Cc: davem@davemloft.net, kuba@kernel.org, herbert@gondor.apana.org.au, netdev@vger.kernel.org Date: Wed, 02 Dec 2026 13:12:33 +0000 Message-ID: <179621715327.3146.499492792826823879@kernel.org> In-Reply-To: <20261201124252.1163254-21-steffen.klassert@secunet.com> References: <20261201124252.1163254-21-steffen.klassert@secunet.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 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? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201124252.1163254-2-steffen.klassert%40secunet.com