> -----Original Message-----
> From: Tariq Toukan <tari…@nvidia.com>
> Sent: Wednesday, December 2, 2026 11:44 AM
> To: Andrew Lunn <andr…@lunn.ch>; David S. Miller
> <dav…@davemloft.net>; Eric Dumazet <edum…@kernel.org>; Jakub
> Kicinski <ku…@kernel.org>; netd…@vger.kernel.org; Paolo Abeni
> <pabe…@redhat.com>; Willem de Bruijn <will…@google.com>
> Cc: Gal Pressman <g…@nvidia.com>; Leon Romanovsky <le…@kernel.org>;
> linu…@vger.kernel.org; Mark Bloch <mblo…@nvidia.com>; Moshe
> Shemesh <mos…@nvidia.com>; Saeed Mahameed <saee…@nvidia.com>;
> Shay Drory <sha…@nvidia.com>; Tariq Toukan <tari…@nvidia.com>; Uri Dar
> <ud…@nvidia.com>
> Subject: [PATCH net] net/mlx5: Serialize command interface flush
>
> From: Uri Dar <ud…@nvidia.com>
>
> mlx5_cmd_flush() takes the command semaphore permits one at a time and
> holds them until it has all of them. Two flushes running at the same
> time can split the permits between them, and then both spin forever.
>
> This can happen as the fatal health work calls enter_error_state()
> without intf_state_mutex, for example while the PCI error handler
> enters error state.
>
> Serialize mlx5_cmd_flush() with a mutex. A second flush now waits for
> the first one, then finds all permits free and returns at once.
>
> Fixes: 51d138c2610a ("net/mlx5: Fix health error state handling")
> Signed-off-by: Uri Dar <ud…@nvidia.com>
> Reviewed-by: Moshe Shemesh <mos…@nvidia.com>
> Signed-off-by: Tariq Toukan <tari…@nvidia.com>
> ---
Reviewed-by: Jacob Keller <jaco…@intel.com>
> drivers/net/ethernet/mellanox/mlx5/core/cmd.c | 5 +++++
> include/linux/mlx5/driver.h | 4 ++++
> 2 files changed, 9 insertions(+)
>
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
> b/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
> index 84583dc5eb1c..4874c81b642a 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
> @@ -1876,6 +1876,7 @@ void mlx5_cmd_flush(struct mlx5_core_dev *dev)
> struct mlx5_cmd *cmd = &dev->cmd;
> int i;
>
> + mutex_lock(&cmd->flush_lock);
> for (i = 0; i < cmd->vars.max_reg_cmds; i++) {
> while (down_trylock(&cmd->vars.sem)) {
> mlx5_cmd_trigger_completions(dev);
> @@ -1892,6 +1893,7 @@ void mlx5_cmd_flush(struct mlx5_core_dev *dev)
> up(&cmd->vars.pages_sem);
> for (i = 0; i < cmd->vars.max_reg_cmds; i++)
> up(&cmd->vars.sem);
> + mutex_unlock(&cmd->flush_lock);
> }
>
> static struct mlx5_cmd_msg *alloc_msg(struct mlx5_core_dev *dev, int
> in_size,
> @@ -2438,11 +2440,13 @@ int mlx5_cmd_init(struct mlx5_core_dev *dev)
>
> spin_lock_init(&cmd->alloc_lock);
> spin_lock_init(&cmd->token_lock);
> + mutex_init(&cmd->flush_lock);
>
> set_wqname(dev);
> cmd->wq = create_singlethread_workqueue(cmd->wq_name);
> if (!cmd->wq) {
> mlx5_core_err(dev, "failed to create command
> workqueue\n");
> + mutex_destroy(&cmd->flush_lock);
> return -ENOMEM;
> }
>
> @@ -2457,6 +2461,7 @@ void mlx5_cmd_cleanup(struct mlx5_core_dev
> *dev)
>
> mlx5_cmdif_debugfs_cleanup(dev);
> destroy_workqueue(cmd->wq);
> + mutex_destroy(&cmd->flush_lock);
> }
>
> int mlx5_cmd_enable(struct mlx5_core_dev *dev)
> diff --git a/include/linux/mlx5/driver.h b/include/linux/mlx5/driver.h
> index 84e9f6bdb7c1..e336187c1091 100644
> --- a/include/linux/mlx5/driver.h
> +++ b/include/linux/mlx5/driver.h
> @@ -325,6 +325,10 @@ struct mlx5_cmd {
> */
> spinlock_t token_lock;
> u8 token;
> +
> + /* serialize command interface flush
> + */
> + struct mutex flush_lock;
> char wq_name[MLX5_CMD_WQ_MAX_NAME];
> struct workqueue_struct *wq;
> int mode;
> --
> 2.44.0
>