Ccing the author of the patch. On Wed, Dec 02, 2026 at 01:12:33PM +0000, netd…@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/4181…@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/2026…%40secunet.com