[PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA
From: Sam Agazaryan
Date: Tue Dec 01 2026 - 21:25:25 EST
When DAA re-adds a device that is already known,
__i3c_master_add_i3c_dev_locked() runs with the bus maintenance lock
held and calls i3c_dev_free_ibi_locked() on the old descriptor. That
destroys its IBI workqueue, which waits for any handler still running.
If that handler starts a private transfer, as mctp_i3c_ibi_handler()
does, it blocks on the bus lock and the two deadlock:
INFO: task i3c-hub-detecto:895 blocked for more than 120 seconds.
Call trace:
__flush_workqueue+0x104/0x3a8
drain_workqueue+0xb4/0x158
destroy_workqueue+0x5c/0x2c8
i3c_dev_free_ibi_locked+0x50/0x88
i3c_master_add_i3c_dev_locked+0x248/0x3b8
hci_cmd_v1_daa+0x214/0x448
i3c_hci_daa+0x1c/0x30
i3c_master_do_daa+0x58/0x130
INFO: task kworker/u16:1:141099 blocked for more than 121 seconds.
Workqueue: 10-20a0168000f i3c_master_handle_ibi
Call trace:
rwsem_down_read_slowpath+0x21c/0x528
down_read+0x48/0xd0
i3c_bus_normaluse_lock+0x18/0x28
i3c_device_do_priv_xfers+0x7c/0xb8
mctp_i3c_ibi_handler+0x170/0x308
i3c_master_handle_ibi+0x60/0xe0
Free only the controller IBI resources of the old descriptor and move
its IBI info, workqueue included, to the new one. If requesting
controller IBI resources for the new descriptor fails, destroy the
workqueue from a system workqueue so that never happens under the bus
lock.
This depends on the previous patch, since the controller now frees the
old IBI pool while a handler may still be running. IBIs queued for the
old descriptor that have not started are dropped, and a handler that is
already running completes against the new one.
Fixes: 9fd00df05e81 ("i3c: master: handle IBIs in order they came")
Fixes: 36faa04ce3d9 ("i3c: master: Remove i3c_dev_disable_ibi_locked(olddev) on device hotjoin")
Signed-off-by: Sam Agazaryan <samagazaryan@xxxxxxxxxx>
---
drivers/i3c/master.c | 39 +++++++++++++++++++++++++++++++-------
include/linux/i3c/master.h | 2 ++
2 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 75c44c45bd08..22fe970ce3ac 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -2728,6 +2728,17 @@ i3c_master_search_i3c_dev_duplicate(struct i3c_dev_desc *refdev)
return NULL;
}
+static void i3c_dev_free_ibi_work(struct work_struct *work)
+{
+ struct i3c_device_ibi_info *ibi = container_of(work,
+ struct i3c_device_ibi_info,
+ destroy_work);
+
+ if (ibi->wq)
+ destroy_workqueue(ibi->wq);
+ kfree(ibi);
+}
+
/**
* __i3c_master_add_i3c_dev_locked() - add an I3C slave to the bus
* @master: master used to send frames on the bus
@@ -2743,6 +2754,7 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
u8 addr, bool probe)
{
struct i3c_device_info info = { .dyn_addr = addr };
+ struct i3c_device_ibi_info *old_ibi = NULL;
struct i3c_dev_desc *newdev, *olddev;
u8 old_dyn_addr = addr, expected_dyn_addr;
struct i3c_ibi_setup ibireq = { };
@@ -2785,10 +2797,9 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
newdev->dev->desc = newdev;
/*
- * We need to restore the IBI state too, so let's save the
- * IBI information and try to restore them after olddev has
- * been detached+released and its IBI has been stopped and
- * the associated resources have been freed.
+ * We need to restore the IBI state too, so save the IBI
+ * information and move it to newdev once olddev has been
+ * detached and released.
*/
mutex_lock(&olddev->ibi_lock);
if (olddev->ibi) {
@@ -2804,9 +2815,18 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
* a new address. This will result in NACK or timeout.
* So, update the olddev->ibi->enabled flag to false
* to avoid DISEC with OldAddr.
+ *
+ * Free only the controller IBI resources and hand
+ * olddev->ibi, workqueue included, over to newdev.
+ * Destroying the workqueue here would wait for any
+ * running IBI handler while we hold the bus
+ * maintenance lock, and the handler may be waiting
+ * for that lock.
*/
olddev->ibi->enabled = false;
- i3c_dev_free_ibi_locked(olddev);
+ i3c_dev_free_ibi_controller_locked(olddev);
+ old_ibi = olddev->ibi;
+ olddev->ibi = NULL;
}
mutex_unlock(&olddev->ibi_lock);
@@ -2860,13 +2880,17 @@ static void __i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master
* should request the IBI again instead of trying to hide that from
* him.
*/
- if (ibireq.handler) {
+ if (old_ibi) {
mutex_lock(&newdev->ibi_lock);
- ret = i3c_dev_request_ibi_locked(newdev, &ibireq);
+ newdev->ibi = old_ibi;
+ ret = i3c_dev_request_ibi_controller_locked(newdev, &ibireq);
if (ret) {
dev_err(&master->dev,
"Failed to request IBI on device %d-%llx",
master->bus.id, newdev->info.pid);
+ newdev->ibi = NULL;
+ /* destroy_workqueue() waits, so not under the bus lock */
+ queue_work(system_dfl_wq, &old_ibi->destroy_work);
} else if (enable_ibi) {
ret = i3c_dev_enable_ibi_locked(newdev);
if (ret)
@@ -4354,6 +4378,7 @@ int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
atomic_set(&ibi->pending_ibis, 0);
init_completion(&ibi->all_ibis_handled);
+ INIT_WORK(&ibi->destroy_work, i3c_dev_free_ibi_work);
ibi->handler = req->handler;
ibi->max_payload_len = req->max_payload_len;
ibi->num_slots = req->num_slots;
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
index d58070a72a49..55f3309ca42b 100644
--- a/include/linux/i3c/master.h
+++ b/include/linux/i3c/master.h
@@ -136,6 +136,7 @@ struct i3c_ibi_slot {
* @num_slots: number of IBI slots reserved for this device
* @enabled: reflect the IBI status
* @wq: workqueue used to execute IBI handlers.
+ * @destroy_work: frees @wq and this structure from a system workqueue
* @handler: IBI handler specified at i3c_device_request_ibi() call time. This
* handler will be called from the controller workqueue, and as such
* is allowed to sleep (though it is recommended to process the IBI
@@ -159,6 +160,7 @@ struct i3c_device_ibi_info {
unsigned int num_slots;
unsigned int enabled;
struct workqueue_struct *wq;
+ struct work_struct destroy_work;
void (*handler)(struct i3c_device *dev,
const struct i3c_ibi_payload *payload);
};
--
2.56.1.911.g883d8bcb20-goog