Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net/mlx5: Serialize command interface flush
@ 2026-12-02 19:43 Tariq Toukan
  2026-12-02 19:49 ` netdev-bot+sinfo
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tariq Toukan @ 2026-12-02 19:43 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn
  Cc: Gal Pressman, Leon Romanovsky, linux-rdma, Mark Bloch,
	Moshe Shemesh, Saeed Mahameed, Shay Drory, Tariq Toukan, Uri Dar

From: Uri Dar <udar@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 <udar@nvidia.com>
Reviewed-by: Moshe Shemesh <moshe@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net] net/mlx5: Serialize command interface flush
  2026-12-02 19:43 [PATCH net] net/mlx5: Serialize command interface flush Tariq Toukan
@ 2026-12-02 19:49 ` netdev-bot+sinfo
  2026-12-02 20:29 ` Keller, Jacob E
  2026-12-03 19:45 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-12-02 19:49 UTC (permalink / raw)
  To: Tariq Toukan
  Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Paolo Abeni, Willem de Bruijn, Gal Pressman,
	Leon Romanovsky, linux-rdma, Mark Bloch, Moshe Shemesh,
	Saeed Mahameed, Shay Drory, Uri Dar

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

 - What hardware the change was tested on. For driver fixes please
   mention the device (and if relevant firmware version) used for
   testing, or say that the change was not tested on real hardware.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH net] net/mlx5: Serialize command interface flush
  2026-12-02 19:43 [PATCH net] net/mlx5: Serialize command interface flush Tariq Toukan
  2026-12-02 19:49 ` netdev-bot+sinfo
@ 2026-12-02 20:29 ` Keller, Jacob E
  2026-12-03 19:45 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: Keller, Jacob E @ 2026-12-02 20:29 UTC (permalink / raw)
  To: Tariq Toukan, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, netdev@vger.kernel.org, Paolo Abeni,
	Willem de Bruijn
  Cc: Gal Pressman, Leon Romanovsky, linux-rdma@vger.kernel.org,
	Mark Bloch, Moshe Shemesh, Saeed Mahameed, Shay Drory, Uri Dar



> -----Original Message-----
> From: Tariq Toukan <tariqt@nvidia.com>
> Sent: Wednesday, December 2, 2026 11:44 AM
> To: Andrew Lunn <andrew+netdev@lunn.ch>; David S. Miller
> <davem@davemloft.net>; Eric Dumazet <edumazet@kernel.org>; Jakub
> Kicinski <kuba@kernel.org>; netdev@vger.kernel.org; Paolo Abeni
> <pabeni@redhat.com>; Willem de Bruijn <willemb@google.com>
> Cc: Gal Pressman <gal@nvidia.com>; Leon Romanovsky <leon@kernel.org>;
> linux-rdma@vger.kernel.org; Mark Bloch <mbloch@nvidia.com>; Moshe
> Shemesh <moshe@nvidia.com>; Saeed Mahameed <saeedm@nvidia.com>;
> Shay Drory <shayd@nvidia.com>; Tariq Toukan <tariqt@nvidia.com>; Uri Dar
> <udar@nvidia.com>
> Subject: [PATCH net] net/mlx5: Serialize command interface flush
> 
> From: Uri Dar <udar@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 <udar@nvidia.com>
> Reviewed-by: Moshe Shemesh <moshe@nvidia.com>
> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
> ---

Reviewed-by: Jacob Keller <jacob.e.keller@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
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net] net/mlx5: Serialize command interface flush
  2026-12-02 19:43 [PATCH net] net/mlx5: Serialize command interface flush Tariq Toukan
  2026-12-02 19:49 ` netdev-bot+sinfo
  2026-12-02 20:29 ` Keller, Jacob E
@ 2026-12-03 19:45 ` netdev-bot+sashiko
  2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 19:45 UTC (permalink / raw)
  To: tariqt
  Cc: andrew+netdev, davem, edumazet, kuba, netdev, pabeni, willemb,
	gal, leon, linux-rdma, mbloch, moshe, saeedm, shayd, udar

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 patch only partly closes the permit-splitting deadlock it
  targets.

--

net/mlx5: Serialize command interface flush

This commit adds a mutex, cmd->flush_lock, around mlx5_cmd_flush(). Two
concurrent flushes can split the cmd->vars.sem permits and spin forever,
and the mutex prevents that. The scenario given is the fatal health work
calling enter_error_state() without intf_state_mutex while the PCI error
handler enters error state.

> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
> index 84583dc5eb1c0..4874c81b642a8 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);

[Severity: High]
Does this cover every function that collects all the permits?

Two other functions in cmd.c also take every cmd->vars.sem permit one at a
time, and they do not take flush_lock:

drivers/net/ethernet/mellanox/mlx5/core/cmd.c:mlx5_cmd_allowed_opcode() {
    ...
	for (i = 0; i < cmd->vars.max_reg_cmds; i++)
		down(&cmd->vars.sem);
	down(&cmd->vars.pages_sem);
    ...
}

mlx5_cmd_change_mod() has the same loop. It is reached through
mlx5_cmd_use_events() and mlx5_cmd_use_polling().

If one of these runs at the same time as mlx5_cmd_flush(), the two can
still split the max_reg_cmds permits. The collector would then sleep in
down() forever.

mlx5_cmd_flush() would spin in its down_trylock() loop. Calling
mlx5_cmd_trigger_completions() there would not help. No regular command
slot can be allocated while the two collectors hold every permit, so
~bitmask & MLX5_CMD_MASK is 0 and nothing is completed.

This looks reachable in the scenario from the commit message. The fatal
health work drops intf_state_mutex before it flushes:

mlx5_fw_fatal_reporter_err_work()
    mutex_unlock(&dev->intf_state_mutex);
    enter_error_state(dev, false);
        mlx5_cmd_flush()

mlx5_pci_err_detected() calls mlx5_unload_one() before
mlx5_drain_health_wq():

mlx5_pci_err_detected()
    mlx5_enter_error_state(dev, false);
    mlx5_error_sw_reset(dev);
    mlx5_unload_one(dev, false);
    mlx5_drain_health_wq(dev);

The unload path reaches both collectors:

mlx5_unload_one()->mlx5_unload()->mlx5_eq_table_destroy()->destroy_async_eqs()
    mlx5_cmd_allowed_opcode(dev, MLX5_CMD_OP_DESTROY_EQ);
    mlx5_cmd_use_polling(dev);
        mlx5_cmd_change_mod()

create_async_eqs() on the load path calls mlx5_cmd_allowed_opcode() and
mlx5_cmd_use_events() in the same way.

If this happens, the PCI error handler or the load/unload thread sleeps
forever in an uninterruptible down() while holding devl_lock and
intf_state_mutex. The health kworker keeps spinning in mlx5_cmd_flush().

Should mlx5_cmd_allowed_opcode() and mlx5_cmd_change_mod() take the same
mutex? If so, a more general name than flush_lock would fit better. The
comment in struct mlx5_cmd in include/linux/mlx5/driver.h ("serialize
command interface flush") would need updating as well.

>  			cond_resched();

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202194338.3593396-1-tariqt%40nvidia.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 19:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 19:43 [PATCH net] net/mlx5: Serialize command interface flush Tariq Toukan
2026-12-02 19:49 ` netdev-bot+sinfo
2026-12-02 20:29 ` Keller, Jacob E
2026-12-03 19:45 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox