[PATCH rdma-next 8/9] RDMA/mlx5: Replay port configuration events after probe

From: Edward Srouji

Date: Sun Nov 22 2026 - 12:40:20 EST


ib_register_device() lets ib_core and its clients read the port's LID
and P_Key table, and they refresh what they read only on the matching
event. mlx5_ib dispatches no event until the device becomes active at
the end of probe, so a configuration change in between is lost for
good.

On hypervisor setups the SM can program a VF's partitions while it
probes, putting the tenant P_Key at index 0 in front of the default
one. ib_sa then keeps the stale index of the default P_Key and its
queries are dropped:

log_trap_info: Bad P_Key:0x802a on SL:0 from LID1:724 (GUID
0xc42a1030048d4bc) QP1:0x1 to LID2:546 (GUID 0xc42a10300e720ce)
QP2:0x1 port 20

so IPoIB never gets a carrier.

Commit 97834eba7c19 ("net/mlx5: Delay events till ib registration
ends") made mlx5_core hold such events back until the add completed,
but commit df097a278c75 ("IB/mlx5: Use the new mlx5 core notifier
API") moved mlx5_ib to a notifier that does not. This went unnoticed
for years, as a lost P_Key change matters only when it changes
index 0, used by IPoIB, or moves the default P_Key, whose index ib_sa
uses. OpenSM keeps the default P_Key at index 0 unless a partition is
marked indx0, as the tenant partition above is, and even then only a
small fraction of probes lose the race with the SM's write.

Commit ede132a5cf55 ("RDMA/mlx5: Move events notifier registration to
be after device registration") widened the window in which a change
breaks the SA queries: one during ib_register_device() no longer
rebuilds the GSI transmit QPs either.

Record LID and P_Key changes and client reregistration requests on
InfiniBand ports while the device is inactive, and replay them when it
becomes active, ahead of any later event. The replay processes them
with ib_dispatch_event_sync(), so that a replayed change cannot
overwrite the result of a newer one. GUID changes are not recorded, as
on InfiniBand ports ib_core notifies no client of them; a lost one
leaves only the GID cache stale. Port state changes are not recorded
either, being sensitive to the order in which ib_core delivers events.
While the device is inactive, the notifier now queues no handler at
all, rather than running the handlers without dispatching to ib_core.

A device that hits a system error during probe now fails it with -EIO
rather than being activated, which would also replay the recorded
events into it; its clients are then removed instead of being left with
a device that never reports the error.

ib_active becomes a bool, as the lock now covering it does not cover
is_rep and lag_active, which shared its byte as bit fields.

Events during ib_register_device() are still missed, as the notifier
is registered after it; the next patch fixes that.

Fixes: df097a278c75 ("IB/mlx5: Use the new mlx5 core notifier API")
Reviewed-by: Michael Gur <michaelgur@xxxxxxxxxx>
Signed-off-by: Edward Srouji <edwards@xxxxxxxxxx>
---
drivers/infiniband/hw/mlx5/main.c | 116 +++++++++++++++++++++++++++++++++--
drivers/infiniband/hw/mlx5/mlx5_ib.h | 12 +++-
2 files changed, 123 insertions(+), 5 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
index 80399920078edfca8f672fb77514ba6b90966068..8a9e68782a5c449e5565a098e75636d5054ee6c1 100644
--- a/drivers/infiniband/hw/mlx5/main.c
+++ b/drivers/infiniband/hw/mlx5/main.c
@@ -3118,6 +3118,73 @@ static int handle_port_change(struct mlx5_ib_dev *ibdev, u32 port, u8 sub_type,
return 0;
}

+#define MLX5_IB_PORT_CONFIG_SUBTYPES \
+ (BIT(MLX5_PORT_CHANGE_SUBTYPE_LID) | \
+ BIT(MLX5_PORT_CHANGE_SUBTYPE_PKEY) | \
+ BIT(MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG))
+
+static void record_port_event(struct mlx5_ib_dev *dev, u32 port, u8 sub_type)
+{
+ lockdep_assert_held(&dev->pending_events_lock);
+
+ /*
+ * Only InfiniBand ports are recorded, as all their port events come
+ * through this notifier. Port state changes are not, as unlike
+ * configuration changes they are sensitive to the order in which
+ * ib_core delivers the replayed events.
+ */
+ if (!rdma_is_port_valid(&dev->ib_dev, port) ||
+ mlx5_ib_port_link_layer(&dev->ib_dev, port) !=
+ IB_LINK_LAYER_INFINIBAND ||
+ sub_type >= BITS_PER_LONG ||
+ !(BIT(sub_type) & MLX5_IB_PORT_CONFIG_SUBTYPES))
+ return;
+
+ dev->port[port - 1].pending_port_events |= BIT(sub_type);
+}
+
+static bool mlx5_ib_dev_active(struct mlx5_ib_dev *dev)
+{
+ bool active;
+
+ spin_lock_irq(&dev->pending_events_lock);
+ active = dev->ib_active;
+ spin_unlock_irq(&dev->pending_events_lock);
+
+ return active;
+}
+
+static void mlx5_ib_replay_port_events(struct work_struct *_work)
+{
+ struct mlx5_ib_dev *dev = container_of(_work, struct mlx5_ib_dev,
+ pending_events_work);
+ struct ib_event ibev = { .device = &dev->ib_dev };
+ unsigned long pending;
+ u32 port, sub_type;
+ int err;
+
+ rdma_for_each_port(&dev->ib_dev, port) {
+ spin_lock_irq(&dev->pending_events_lock);
+ pending = dev->port[port - 1].pending_port_events;
+ dev->port[port - 1].pending_port_events = 0;
+ spin_unlock_irq(&dev->pending_events_lock);
+
+ for_each_set_bit(sub_type, &pending, BITS_PER_LONG) {
+ if (!mlx5_ib_dev_active(dev))
+ return;
+
+ if (handle_port_change(dev, port, sub_type, &ibev))
+ continue;
+
+ /* Processed before events queued behind this work */
+ err = ib_dispatch_event_sync(&ibev);
+ if (err)
+ mlx5_ib_warn(dev, "replayed event %d on port %u failed, %d\n",
+ ibev.event, port, err);
+ }
+ }
+}
+
static void mlx5_ib_handle_event(struct work_struct *_work)
{
struct mlx5_ib_event_work *work =
@@ -3161,14 +3228,31 @@ static void mlx5_ib_handle_event(struct work_struct *_work)
static int mlx5_ib_event(struct notifier_block *nb,
unsigned long event, void *param)
{
+ struct mlx5_ib_dev *dev =
+ container_of(nb, struct mlx5_ib_dev, mdev_events);
struct mlx5_ib_event_work *work;
+ unsigned long flags;
+
+ /* Until the device is active, keep only what is to be replayed */
+ spin_lock_irqsave(&dev->pending_events_lock, flags);
+ if (!dev->ib_active) {
+ if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
+ struct mlx5_eqe *eqe = param;
+
+ record_port_event(dev, mlx5_ib_eqe_port_num(eqe),
+ eqe->sub_type);
+ }
+ spin_unlock_irqrestore(&dev->pending_events_lock, flags);
+ return NOTIFY_OK;
+ }
+ spin_unlock_irqrestore(&dev->pending_events_lock, flags);

work = kmalloc_obj(*work, GFP_ATOMIC);
if (!work)
return NOTIFY_DONE;

INIT_WORK(&work->work, mlx5_ib_handle_event);
- work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
+ work->dev = dev;
work->is_slave = false;
work->param = param;
work->event = event;
@@ -3203,6 +3287,7 @@ static void mlx5_ib_handle_sys_error_event(struct work_struct *_work)
container_of(_work, struct mlx5_ib_event_work, work);
struct mlx5_ib_dev *ibdev = work->dev;
struct ib_event ibev;
+ bool active;

ibev.event = IB_EVENT_DEVICE_FATAL;
mlx5_ib_handle_internal_error(ibdev);
@@ -3214,10 +3299,13 @@ static void mlx5_ib_handle_sys_error_event(struct work_struct *_work)
goto out;
}

- if (ibdev->ib_active)
- ib_dispatch_event(&ibev);
-
+ spin_lock_irq(&ibdev->pending_events_lock);
+ active = ibdev->ib_active;
ibdev->ib_active = false;
+ spin_unlock_irq(&ibdev->pending_events_lock);
+
+ if (active)
+ ib_dispatch_event(&ibev);
out:
kfree(work);
}
@@ -4457,6 +4545,8 @@ static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
}

mutex_init(&dev->lb.mutex);
+ spin_lock_init(&dev->pending_events_lock);
+ INIT_WORK(&dev->pending_events_work, mlx5_ib_replay_port_events);

err = mlx5r_cmd_query_special_mkeys(dev);
if (err)
@@ -5024,13 +5114,31 @@ static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)

static int mlx5_ib_stage_active_init(struct mlx5_ib_dev *dev)
{
+ /*
+ * Fail a device that hit a system error during probe, as the error
+ * handler may already have run. The replay is queued in the same
+ * critical section, so that it runs before any event the notifier
+ * queues once it sees the device active.
+ */
+ spin_lock_irq(&dev->pending_events_lock);
+ if (dev->mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
+ spin_unlock_irq(&dev->pending_events_lock);
+ return -EIO;
+ }
dev->ib_active = true;
+ queue_work(mlx5_ib_event_wq, &dev->pending_events_work);
+ spin_unlock_irq(&dev->pending_events_lock);
+
return 0;
}

static void mlx5_ib_stage_active_cleanup(struct mlx5_ib_dev *dev)
{
+ spin_lock_irq(&dev->pending_events_lock);
dev->ib_active = false;
+ spin_unlock_irq(&dev->pending_events_lock);
+
+ cancel_work_sync(&dev->pending_events_work);
}

void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
diff --git a/drivers/infiniband/hw/mlx5/mlx5_ib.h b/drivers/infiniband/hw/mlx5/mlx5_ib.h
index 3884e5524e7dfb2a37515a6f4e8caaa82224c964..7e1e0bcd096c994473dcfe2db5655b47bd48915e 100644
--- a/drivers/infiniband/hw/mlx5/mlx5_ib.h
+++ b/drivers/infiniband/hw/mlx5/mlx5_ib.h
@@ -860,6 +860,8 @@ struct mlx5_ib_port {
struct mlx5_ib_dbg_cc_params *dbg_cc_params;
struct mlx5_roce roce;
struct mlx5_eswitch_rep *rep;
+ /* Port change subtypes to replay, protected by pending_events_lock */
+ unsigned long pending_port_events;
#ifdef CONFIG_MLX5_MACSEC
struct mlx5_reserved_gids *reserved_gids;
#endif
@@ -1100,13 +1102,21 @@ struct mlx5_ib_dev {
*/
struct mutex data_direct_lock;
struct notifier_block mdev_events;
+ /* Protects ib_active writes and the ports' pending_port_events */
+ spinlock_t pending_events_lock;
+ struct work_struct pending_events_work;
struct notifier_block sys_error_events;
struct notifier_block lag_events;
int num_ports;
/* serialize update of capability mask
*/
struct mutex cap_mask_mutex;
- u8 ib_active:1;
+ /*
+ * Written under pending_events_lock, and read under it when the read
+ * must be ordered with activation or with port event recording and
+ * replay.
+ */
+ bool ib_active;
u8 is_rep:1;
u8 lag_active:1;
u8 fill_delay;

--
2.49.0