Re: [PATCH net V2] net/mlx5e: rep, Fix races between devcom PAIR event and uplink close
From: Shay Drory
Date: Mon Nov 16 2026 - 14:53:16 EST
On 16/11/2026 20:24, netdev-bot+sashiko@xxxxxxxxxx wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 · Medium: 0 · 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 locking only works if the unsynchronized read of
> `esw->devcom` returns a valid pointer.
>
> --
>
> net/mlx5e: rep, Fix races between devcom PAIR event and uplink close
>
> This commit adds mlx5_devcom_comp_read_lock/unlock(), which take the
> devcom component semaphore for read whether or not comp->ready is set.
> It holds that lock across mlx5e_sqs2vport_start() and
> mlx5e_sqs2vport_stop() so that both are serialized against the
> ESW_OFFLOADS PAIR/UNPAIR handler.
>
>> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c b/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
>> index a50a42581528a..962c70b9b5661 100644
>> --- a/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
>> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
>
> [ ... ]
>
>> @@ -630,8 +627,12 @@ mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
>> struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
>> struct mlx5e_rep_priv *rpriv = priv->ppriv;
>> struct mlx5_eswitch_rep *rep = rpriv->rep;
>> + struct mlx5_devcom_comp_dev *devcom;
>>
>> + devcom = esw->devcom;
>> + mlx5_devcom_comp_read_lock(devcom);
>> + mlx5e_sqs2vport_stop(esw, rep);
>> + mlx5_devcom_comp_read_unlock(devcom);
>> }
>
> [Severity: High]
>
> Are the races still reachable if esw->devcom is NULL or stale when it is
> read here and in mlx5e_sqs2vport_start()?
>
> The load of esw->devcom is a plain unsynchronized read. The store side
> has no WRITE_ONCE() and the load side has no READ_ONCE().
> mlx5_devcom_comp_read_lock() also does nothing when passed NULL.
>
> In esw_offloads_enable(), the uplink and VF reps are loaded and their
> netdevs registered before mlx5_esw_offloads_devcom_init() runs. That
> function publishes the pointer and sends the PAIR event right away:
>
> drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c:mlx5_esw_offloads_devcom_init() {
> ...
> esw->devcom = mlx5_devcom_register_component(esw->dev->priv.devc,
> MLX5_DEVCOM_ESW_OFFLOADS,
> attr,
> mlx5_esw_offloads_devcom_event,
> esw);
> if (!esw->devcom)
> return;
>
> err = mlx5_devcom_send_event(esw->devcom,
> ESW_OFFLOADS_DEVCOM_PAIR,
> ...
> }
>
> The PAIR handler runs with comp->sem held for write. It goes through
> mlx5_esw_offloads_devcom_event()->mlx5_esw_offloads_pair()->
> mlx5e_vport_rep_event_pair(), which walks rpriv->vport_sqs_list for
> every loaded rep and calls xa_insert() on rep_sq->sq_peer.
>
> A rep ndo_open or ndo_stop holds rtnl and priv->state_lock. The
> devlink-locked enable path holds neither across this window. If the rep
> reads esw->devcom before the store above, two things can happen:
>
> - mlx5e_sqs2vport_start() snapshots devcom == NULL, so it takes no lock
> and sees peers_ready == false. It then does list_add() into
> rpriv->vport_sqs_list while the PAIR handler is walking that list.
> SQs added after the walk also never get their peer send-to-vport
> rules.
>
> - mlx5e_remove_sqs_fwd_rules() calls mlx5e_sqs2vport_stop() without
> the lock. That can list_del() and kfree() a rep_sq and its sq_peer
> entries while mlx5e_vport_rep_event_pair() is still using them.
>
> These are the two races the commit message describes as fixed.
>
> On teardown, esw_offloads_disable() calls the devcom cleanup while the
> VF rep netdevs are still registered:
>
> mlx5_esw_offloads_devcom_cleanup(esw);
> mlx5_eswitch_disable_pf_vf_vports(esw);
> mlx5_esw_offloads_rep_unload(esw, MLX5_VPORT_UPLINK);
>
> mlx5_esw_offloads_devcom_cleanup() frees the comp_dev before it clears
> the pointer:
>
> mlx5_devcom_unregister_component(esw->devcom);
> xa_destroy(&esw->paired);
> xa_destroy(&esw->fdb_table.offloads.peer_miss_rules);
> esw->devcom = NULL;
>
> Inside mlx5_devcom_unregister_component(), devcom_free_comp_dev() does:
>
> down_write(&comp->sem);
> list_del(&devcom->list);
> WRITE_ONCE(comp->nr_devs, comp->nr_devs - 1);
> up_write(&comp->sem);
>
> kref_put(&devcom->devc->ref, mlx5_devcom_dev_release);
> kfree(devcom);
> kref_put(&comp->ref, mlx5_devcom_comp_release);
>
> The write lock only excludes readers that already hold comp->sem.
>
> Could a concurrent VF rep ndo_stop load the old esw->devcom in
> mlx5e_remove_sqs_fwd_rules() and then call down_read(&devcom->comp->sem)
> after the kfree()? The stop path did not touch esw->devcom before this
> patch, so this use-after-free would be new on the close path.
>
> Would it work better to serialize publishing and clearing esw->devcom
> against rep start/stop, instead of locking whatever pointer happens to be
> read at that moment?
The esw->devcom NULL/stale reads are a devcom lifecycle issue, not
specific to this patch, and will be addressed in a separate patch.
This patch fixes the PAIR/UNPAIR vs. rep start/stop race on an
established devcom.
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112182403.3646298-1-tariqt%40nvidia.com