Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH net] net/mlx5: SF, remove probed SF devices when arming vhca events fails
@ 2026-11-17  3:11 Changyul Lee
  2026-11-18  3:11 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Changyul Lee @ 2026-11-17  3:11 UTC (permalink / raw)
  To: Saeed Mahameed, Leon Romanovsky, Tariq Toukan, Mark Bloch
  Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Parav Pandit, Shay Drory, netdev, linux-rdma,
	linux-kernel, Changyul Lee

mlx5_sf_dev_table_create() queues work that probes the SFs which are
already active, and then arms the vhca state events. If arming fails,
the arm_err path stops and flushes that work and frees the table, but
the SF devices that the work already added stay on the auxiliary bus
and in table->devices. dev->priv.sf_dev_table is set to NULL, so
mlx5_sf_dev_table_destroy() returns early later and nothing removes
them, while they keep pointing at the parent mlx5_core_dev.

Remove them with mlx5_sf_dev_destroy_all() after the work is flushed,
as mlx5_sf_dev_table_destroy() does. Move the helper up so that it is
defined before the new caller.

Fixes: 4f57332d6a55 ("net/mlx5: SF: Fix probing active SFs during driver probe phase")
Signed-off-by: Changyul Lee <lcy8047@gmail.com>
---
Found with a static analysis tool we are developing.

Compile-tested only, with x86_64_defconfig plus MLX5_CORE, MLX5_CORE_EN,
MLX5_ESWITCH, MLX5_SF and W=1.

 .../ethernet/mellanox/mlx5/core/sf/dev/dev.c  | 23 ++++++++++---------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c b/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
index e75cbf333898..44f6292ad59e 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
@@ -328,6 +328,17 @@ int mlx5_sf_dev_notifier_init(struct mlx5_core_dev *dev)
 	return mlx5_vhca_event_notifier_register(dev, &dev->priv.sf_dev_nb);
 }
 
+static void mlx5_sf_dev_destroy_all(struct mlx5_sf_dev_table *table)
+{
+	struct mlx5_sf_dev *sf_dev;
+	unsigned long index;
+
+	xa_for_each(&table->devices, index, sf_dev) {
+		xa_erase(&table->devices, index);
+		mlx5_sf_dev_remove_aux(table->dev, sf_dev);
+	}
+}
+
 void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)
 {
 	struct mlx5_sf_dev_table *table;
@@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)
 arm_err:
 	mlx5_sf_dev_destroy_active_works(table);
 	mlx5_vhca_event_work_queues_flush(dev);
+	mlx5_sf_dev_destroy_all(table);
 add_active_err:
 	kfree(table);
 	dev->priv.sf_dev_table = NULL;
@@ -367,17 +379,6 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)
 	mlx5_core_err(dev, "SF DEV table create err = %d\n", err);
 }
 
-static void mlx5_sf_dev_destroy_all(struct mlx5_sf_dev_table *table)
-{
-	struct mlx5_sf_dev *sf_dev;
-	unsigned long index;
-
-	xa_for_each(&table->devices, index, sf_dev) {
-		xa_erase(&table->devices, index);
-		mlx5_sf_dev_remove_aux(table->dev, sf_dev);
-	}
-}
-
 void mlx5_sf_dev_notifier_cleanup(struct mlx5_core_dev *dev)
 {
 	if (mlx5_core_is_sf(dev))

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.43.0


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

* Re: [PATCH net] net/mlx5: SF, remove probed SF devices when arming vhca events fails
  2026-11-17  3:11 [PATCH net] net/mlx5: SF, remove probed SF devices when arming vhca events fails Changyul Lee
@ 2026-11-18  3:11 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18  3:11 UTC (permalink / raw)
  To: Changyul Lee; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/mlx5: race and UAF in mlx5_sf_dev_table_create() arm_err
  cleanup
--

commit a5d70bb82124aa0963c3bb188af6126f523476a3
Author: Changyul Lee <lcy8047@gmail.com>

net/mlx5: SF, remove probed SF devices when arming vhca events fails

This commit calls mlx5_sf_dev_destroy_all() on the arm_err error path in
mlx5_sf_dev_table_create() so that SF auxiliary devices probed by the
active work are removed if arming VHCA events fails.

> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c b/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
> index e75cbf333898d..44f6292ad59ec 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c
[ ... ]
> @@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)
>  arm_err:
>  	mlx5_sf_dev_destroy_active_works(table);
>  	mlx5_vhca_event_work_queues_flush(dev);
> +	mlx5_sf_dev_destroy_all(table);
>  add_active_err:
>  	kfree(table);
>  	dev->priv.sf_dev_table = NULL;

[Severity: Medium]
Can mlx5_sf_dev_state_change_handler() race with mlx5_sf_dev_destroy_all()
and kfree(table) on the arm_err path?

If mlx5_sf_dev_vhca_arm_all() fails after arming one or more SF functions,
those functions remain armed in firmware while dev->priv.sf_dev_nb is still
registered and dev->priv.sf_dev_table still points to table.  Unlike
mlx5_sf_dev_table_destroy(), which runs after mlx5_sf_dev_notifier_cleanup()
has unregistered the notifier, arm_err runs with the notifier active.

When mlx5_sf_dev_destroy_all() removes an already-probed SF, tearing down
its HCA transitions the function from MLX5_VHCA_STATE_IN_USE back to
MLX5_VHCA_STATE_ACTIVE, which can queue a new VHCA state event after
mlx5_vhca_event_work_queues_flush() has already returned:

CPU0                                  CPU1 (vhca_events wq)
mlx5_sf_dev_table_create()
  mlx5_sf_dev_vhca_arm_all()
    /* arms SF 0, fails on SF 1 */
  mlx5_sf_dev_destroy_active_works()
  mlx5_vhca_event_work_queues_flush()
  mlx5_sf_dev_destroy_all(table)
    xa_erase(&table->devices, 0)
    mlx5_sf_dev_remove_aux()
      /* SF 0 becomes ACTIVE,
         fires VHCA event */
                                      mlx5_sf_dev_state_change_handler()
                                        table = dev->priv.sf_dev_table;
  kfree(table)
                                        xa_load(&table->devices, 0)
                                          <-- use-after-free
  dev->priv.sf_dev_table = NULL;

Even if the handler runs before kfree(table), seeing MLX5_VHCA_STATE_ACTIVE
after xa_erase() causes mlx5_sf_dev_add() to re-add the SF auxiliary device
that mlx5_sf_dev_destroy_all() just removed, and a state change to
ALLOCATED or TEARDOWN_REQUEST can call mlx5_sf_dev_del() concurrently with
mlx5_sf_dev_destroy_all().

Clearing dev->priv.sf_dev_table before mlx5_vhca_event_work_queues_flush()
(with a barrier or WRITE_ONCE/READ_ONCE pair) would ensure no handler runs
against table while mlx5_sf_dev_destroy_all() and kfree(table) execute.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117031141.1720619-1-lcy8047@gmail.com?part=1

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

end of thread, other threads:[~2026-11-18  3:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  3:11 [PATCH net] net/mlx5: SF, remove probed SF devices when arming vhca events fails Changyul Lee
2026-11-18  3:11 ` sashiko-bot

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