[PATCH v2 4/4] i3c: master: Preserve IBI workqueue across DAA
From: Sam Agazaryan
Date: Sat Dec 05 2026 - 01:56:09 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,
keep the IBI info on it anyway, marked as having no controller
resources. A handler may still be running on its workqueue, and keeping
it there means i3c_device_free_ibi() still waits for that handler when
the driver unbinds. Enabling IBIs on such a device fails with -ENODEV,
and disabling or freeing them skips the controller.
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>
---
Notes:
Changes in v2:
- If requesting controller IBI resources for the new descriptor fails,
keep the IBI info on it, marked as having no controller resources,
instead of destroying its workqueue from a system workqueue. Driver
teardown then still waits for a running handler (Sashiko).
- Drop the destroy_work member this made unnecessary.
drivers/i3c/master.c | 52 ++++++++++++++++++++++++++++++--------
include/linux/i3c/master.h | 5 ++++
2 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index e52254f65145..58112c0dd5fd 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -2768,6 +2768,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 = { };
@@ -2810,10 +2811,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) {
@@ -2829,9 +2829,21 @@ 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);
+ if (olddev->ibi->has_controller_resources) {
+ i3c_dev_free_ibi_controller_locked(olddev);
+ olddev->ibi->has_controller_resources = false;
+ }
+ old_ibi = olddev->ibi;
+ olddev->ibi = NULL;
}
mutex_unlock(&olddev->ibi_lock);
@@ -2885,9 +2897,16 @@ 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);
+ /*
+ * Keep the IBI info on newdev even if the controller request
+ * fails. A handler may still be running on its workqueue, and
+ * the driver's i3c_device_free_ibi() must wait for it.
+ */
+ newdev->ibi = old_ibi;
+ ret = i3c_dev_request_ibi_controller_locked(newdev, &ibireq);
+ newdev->ibi->has_controller_resources = !ret;
if (ret) {
dev_err(&master->dev,
"Failed to request IBI on device %d-%llx",
@@ -4291,9 +4310,14 @@ int i3c_dev_disable_ibi_locked(struct i3c_dev_desc *dev)
{
int ret;
- ret = i3c_dev_disable_ibi_controller_locked(dev);
- if (ret)
- return ret;
+ if (!dev->ibi)
+ return -EINVAL;
+
+ if (dev->ibi->has_controller_resources) {
+ ret = i3c_dev_disable_ibi_controller_locked(dev);
+ if (ret)
+ return ret;
+ }
reinit_completion(&dev->ibi->all_ibis_handled);
if (atomic_read(&dev->ibi->pending_ibis))
@@ -4349,6 +4373,9 @@ int i3c_dev_enable_ibi_locked(struct i3c_dev_desc *dev)
{
int ret;
+ if (dev->ibi && !dev->ibi->has_controller_resources)
+ return -ENODEV;
+
ret = i3c_dev_enable_ibi_controller_locked(dev);
if (!ret)
dev->ibi->enabled = true;
@@ -4440,6 +4467,8 @@ int i3c_dev_request_ibi_locked(struct i3c_dev_desc *dev,
destroy_workqueue(ibi->wq);
kfree(ibi);
dev->ibi = NULL;
+ } else {
+ ibi->has_controller_resources = true;
}
return ret;
@@ -4497,7 +4526,8 @@ void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
dev_err(&master->dev, "Failed to disable IBI before freeing\n");
}
- i3c_dev_free_ibi_controller_locked(dev);
+ if (dev->ibi->has_controller_resources)
+ i3c_dev_free_ibi_controller_locked(dev);
if (dev->ibi->wq) {
destroy_workqueue(dev->ibi->wq);
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
index f0b8fe89a3cc..fb1384149d63 100644
--- a/include/linux/i3c/master.h
+++ b/include/linux/i3c/master.h
@@ -136,6 +136,10 @@ struct i3c_ibi_slot {
* rejected by the master
* @num_slots: number of IBI slots reserved for this device
* @enabled: reflect the IBI status
+ * @has_controller_resources: the controller holds IBI resources for this
+ * device, set up by its ->request_ibi() hook. Only
+ * false after DAA re-added the device and the
+ * controller failed to set them up again
* @wq: workqueue used to execute IBI handlers.
* @handler: IBI handler specified at i3c_device_request_ibi() call time. This
* handler will be called from the controller workqueue, and as such
@@ -159,6 +163,7 @@ struct i3c_device_ibi_info {
unsigned int max_payload_len;
unsigned int num_slots;
unsigned int enabled;
+ bool has_controller_resources;
struct workqueue_struct *wq;
void (*handler)(struct i3c_device *dev,
const struct i3c_ibi_payload *payload);
--
2.56.1.915.g69d5ec7298-goog