[PATCH rdma-next 5/9] RDMA/core: Add ib_dispatch_event_sync()

From: Edward Srouji

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


ib_dispatch_event() hands each event to a work item on ib_wq, which is
not ordered, so events dispatched one after the other can be processed
in either order, and a cache update can publish an older state after a
newer one. A driver has no way to wait for an event it dispatched
short of flushing ib_wq, which waits for the work of every device,
including work that may itself be waiting for that driver.

Add ib_dispatch_event_sync(), which processes the event in the
caller's context and returns the error of the cache update, so that
events a driver passes to it one after the other are processed in that
order. They are not ordered against events passed to
ib_dispatch_event(). It must be called from a workqueue, the context
event handlers are promised, and not from an event handler, where it
would take event_handler_rwsem recursively and could deadlock behind a
waiting writer; lockdep checks the latter.

The processing moves to a helper shared with the work item: the cache
is still updated before the handlers are called, P_Key and GID changes
still revalidate security, and the handlers are still not called when
the update fails, nor for a GID change. The handlers get a copy of the
event, as they do from the work item.

A later patch uses it in mlx5_ib to replay port events that arrived
while the device was not yet active.

Reviewed-by: Michael Gur <michaelgur@xxxxxxxxxx>
Signed-off-by: Edward Srouji <edwards@xxxxxxxxxx>
---
drivers/infiniband/core/cache.c | 63 ++++++++++++++++++++++++++++++++++-------
include/rdma/ib_verbs.h | 1 +
2 files changed, 53 insertions(+), 11 deletions(-)

diff --git a/drivers/infiniband/core/cache.c b/drivers/infiniband/core/cache.c
index c338c2e52d8bd041bdc194ca574b7c35cc9b6d0d..4e376a866715d4e0613087dccde232934171c1e1 100644
--- a/drivers/infiniband/core/cache.c
+++ b/drivers/infiniband/core/cache.c
@@ -1522,29 +1522,36 @@ ib_cache_update(struct ib_device *device, u32 port, bool update_gids,
return ret;
}

-static void ib_cache_event_task(struct work_struct *_work)
+static int ib_cache_event_process(struct ib_event *event)
{
- struct ib_update_work *work =
- container_of(_work, struct ib_update_work, work);
int ret;

/* Before distributing the cache update event, first sync
* the cache.
*/
- ret = ib_cache_update(work->event.device, work->event.element.port_num,
- work->event.event == IB_EVENT_GID_CHANGE ||
- work->event.event == IB_EVENT_CLIENT_REREGISTER,
- work->event.event == IB_EVENT_PKEY_CHANGE,
- work->event.event == IB_EVENT_PKEY_CHANGE ||
- work->event.event == IB_EVENT_GID_CHANGE);
+ ret = ib_cache_update(event->device, event->element.port_num,
+ event->event == IB_EVENT_GID_CHANGE ||
+ event->event == IB_EVENT_CLIENT_REREGISTER,
+ event->event == IB_EVENT_PKEY_CHANGE,
+ event->event == IB_EVENT_PKEY_CHANGE ||
+ event->event == IB_EVENT_GID_CHANGE);

/* GID event is notified already for individual GID entries by
* dispatch_gid_change_event(). Hence, notifiy for rest of the
* events.
*/
- if (!ret && work->event.event != IB_EVENT_GID_CHANGE)
- ib_dispatch_event_clients(&work->event);
+ if (!ret && event->event != IB_EVENT_GID_CHANGE)
+ ib_dispatch_event_clients(event);
+
+ return ret;
+}

+static void ib_cache_event_task(struct work_struct *_work)
+{
+ struct ib_update_work *work =
+ container_of(_work, struct ib_update_work, work);
+
+ ib_cache_event_process(&work->event);
kfree(work);
}

@@ -1593,6 +1600,40 @@ void ib_dispatch_event(const struct ib_event *event)
}
EXPORT_SYMBOL(ib_dispatch_event);

+/**
+ * ib_dispatch_event_sync - Process an asynchronous event before returning
+ * @event: Event to process
+ *
+ * Does in the caller's context what ib_dispatch_event() leaves to a work
+ * item, by the same rules: an event that affects the cache updates it first,
+ * and the registered event handlers are then called, unless that update
+ * failed or the event is a GID change. All of it completes before this
+ * returns, so events a caller passes here one after the other are processed
+ * in that order. They are not ordered against events passed to
+ * ib_dispatch_event().
+ *
+ * Context: Workqueue context, which event handlers are promised, with no
+ * locks held that the cache update or the handlers may take. Not from an
+ * event handler.
+ *
+ * Return: 0, or the error of the cache update.
+ */
+int ib_dispatch_event_sync(const struct ib_event *event)
+{
+ struct ib_event copy = *event;
+
+ might_sleep();
+ lockdep_assert_not_held(&event->device->event_handler_rwsem);
+
+ if (!is_cache_update_event(&copy)) {
+ ib_dispatch_event_clients(&copy);
+ return 0;
+ }
+
+ return ib_cache_event_process(&copy);
+}
+EXPORT_SYMBOL(ib_dispatch_event_sync);
+
int ib_cache_setup_one(struct ib_device *device)
{
u32 p;
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index 29228226a236638aa746b9f5e0172acded69f135..ab6b9af1a6d3018c441e608b0fbaa9afbd1aa3bf 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -3308,6 +3308,7 @@ bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
void ib_register_event_handler(struct ib_event_handler *event_handler);
void ib_unregister_event_handler(struct ib_event_handler *event_handler);
void ib_dispatch_event(const struct ib_event *event);
+int ib_dispatch_event_sync(const struct ib_event *event);

int ib_query_port(struct ib_device *device,
u32 port_num, struct ib_port_attr *port_attr);

--
2.49.0