All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC 0/2] i3c: master: Fix IBI teardown when DAA re-adds a device
@ 2026-12-02  2:25 ` Sam Agazaryan
  0 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

When DAA re-adds a device that is already on the bus, as on a Hot-Join
after a target reset, __i3c_master_add_i3c_dev_locked() frees the old
descriptor's IBI resources with the bus maintenance lock held. On an
AST2700 BMC with MCTP over I3C this caused two bugs.

The controller frees its IBI slot pool while IBI work is still queued or
running, and the handler then recycles its slot through the controller,
which has already freed that device's IBI data. Patch 1 makes the pool
safe to free with slots in flight.

destroy_workqueue() on the old IBI workqueue waits for a handler that is
blocked on the bus lock DAA holds. Patch 2 moves the workqueue to the
new descriptor instead of destroying it.

Two points I would like feedback on.

Patch 1 returns slots straight to their pool instead of through
->recycle_ibi_slot(), so the core now assumes every slot passed to
i3c_master_queue_ibi() comes from a generic IBI pool. That holds for
all in-tree drivers. If you would rather the core own the pool, or
->recycle_ibi_slot() go away, I can rework it.

Patch 2 hands the old descriptor's IBI state to the new one. IBIs
queued for the old descriptor that have not started are dropped, and
a handler that is already running finishes against the new one.
Deferring the old teardown until DAA drops the lock looked harder,
since the controller has to release its per-device IBI resources
before the old descriptor is detached inside DAA.

Both failures first showed up in host reset loops, in fewer than 1 in 10
resets. The series was tested on a 6.18-based kernel with equivalent
changes. That kernel has a sysfs attribute that runs RSTDAA and DAA on a
bus, which userspace uses after a host reset. I looped it on the bus
with the host CPU's MCTP endpoint, with 100 to 200 MCTP packets per
second, each announced by an IBI, and a test-only 5 ms sleep at the
start of i3c_master_handle_ibi() so a handler is still running when DAA
re-adds the device. Without the sleep, 30 rescans on the unpatched
kernel did not hit the race.

Without the series, the first or second rescan hit the WARN in
i3c_generic_ibi_free_pool() and then the NULL dereference shown in
patch 1. With patch 1 alone, the second rescan deadlocked as shown in
patch 2. With both patches, 250 rescans on two buses with 0, 5 and
20 ms delays ran clean, and MCTP recovered after every one.

On the unpatched kernel, lockdep also flags the problem on the first
rescan, with no test sleep: a possible circular locking dependency at
destroy_workqueue() in the DAA path, through the MCTP IBI handler and
the bus lock.

On this base the series is build-tested for arm64 with all I3C drivers
enabled.

Sam Agazaryan (2):
  i3c: master: Drain IBI slot pool before freeing
  i3c: master: Preserve IBI workqueue across DAA

 drivers/i3c/master.c       | 188 +++++++++++++++++++++++++++++--------
 include/linux/i3c/master.h |   2 +
 2 files changed, 150 insertions(+), 40 deletions(-)


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH RFC 0/2] i3c: master: Fix IBI teardown when DAA re-adds a device
@ 2026-12-02  2:25 ` Sam Agazaryan
  0 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

When DAA re-adds a device that is already on the bus, as on a Hot-Join
after a target reset, __i3c_master_add_i3c_dev_locked() frees the old
descriptor's IBI resources with the bus maintenance lock held. On an
AST2700 BMC with MCTP over I3C this caused two bugs.

The controller frees its IBI slot pool while IBI work is still queued or
running, and the handler then recycles its slot through the controller,
which has already freed that device's IBI data. Patch 1 makes the pool
safe to free with slots in flight.

destroy_workqueue() on the old IBI workqueue waits for a handler that is
blocked on the bus lock DAA holds. Patch 2 moves the workqueue to the
new descriptor instead of destroying it.

Two points I would like feedback on.

Patch 1 returns slots straight to their pool instead of through
->recycle_ibi_slot(), so the core now assumes every slot passed to
i3c_master_queue_ibi() comes from a generic IBI pool. That holds for
all in-tree drivers. If you would rather the core own the pool, or
->recycle_ibi_slot() go away, I can rework it.

Patch 2 hands the old descriptor's IBI state to the new one. IBIs
queued for the old descriptor that have not started are dropped, and
a handler that is already running finishes against the new one.
Deferring the old teardown until DAA drops the lock looked harder,
since the controller has to release its per-device IBI resources
before the old descriptor is detached inside DAA.

Both failures first showed up in host reset loops, in fewer than 1 in 10
resets. The series was tested on a 6.18-based kernel with equivalent
changes. That kernel has a sysfs attribute that runs RSTDAA and DAA on a
bus, which userspace uses after a host reset. I looped it on the bus
with the host CPU's MCTP endpoint, with 100 to 200 MCTP packets per
second, each announced by an IBI, and a test-only 5 ms sleep at the
start of i3c_master_handle_ibi() so a handler is still running when DAA
re-adds the device. Without the sleep, 30 rescans on the unpatched
kernel did not hit the race.

Without the series, the first or second rescan hit the WARN in
i3c_generic_ibi_free_pool() and then the NULL dereference shown in
patch 1. With patch 1 alone, the second rescan deadlocked as shown in
patch 2. With both patches, 250 rescans on two buses with 0, 5 and
20 ms delays ran clean, and MCTP recovered after every one.

On the unpatched kernel, lockdep also flags the problem on the first
rescan, with no test sleep: a possible circular locking dependency at
destroy_workqueue() in the DAA path, through the MCTP IBI handler and
the bus lock.

On this base the series is build-tested for arm64 with all I3C drivers
enabled.

Sam Agazaryan (2):
  i3c: master: Drain IBI slot pool before freeing
  i3c: master: Preserve IBI workqueue across DAA

 drivers/i3c/master.c       | 188 +++++++++++++++++++++++++++++--------
 include/linux/i3c/master.h |   2 +
 2 files changed, 150 insertions(+), 40 deletions(-)


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.1.911.g883d8bcb20-goog


-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

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

* [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing
  2026-12-02  2:25 ` Sam Agazaryan
@ 2026-12-02  2:25   ` Sam Agazaryan
  -1 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

When DAA re-adds a device that is already known, as on a Hot-Join after
a target reset, __i3c_master_add_i3c_dev_locked() clears
olddev->ibi->enabled before calling i3c_dev_free_ibi_locked(). That
skips the wait for pending IBIs, so the controller frees its IBI pool
while slot work is still queued or running on dev->ibi->wq. The handler
then recycles its slot through the controller, whose per-device IBI
data is already gone:

  Internal error: Oops: 0000000096000004 [#1] SMP
  Workqueue: 10-20a0168000f i3c_master_handle_ibi
  pc : hci_dma_recycle_ibi_slot+0x1c/0x38
  lr : i3c_hci_recycle_ibi_slot+0x2c/0x40
  Call trace:
   hci_dma_recycle_ibi_slot+0x1c/0x38 (P)
   i3c_hci_recycle_ibi_slot+0x2c/0x40
   i3c_master_handle_ibi+0x78/0xe0
   process_one_work+0x154/0x3b0
   worker_thread+0x28c/0x3a0

Make the pool safe to free with slots in flight. Each slot now records
its pool, and i3c_master_queue_ibi() records the IBI info and device the
handler needs, so i3c_master_handle_ibi() no longer uses slot->dev and
returns the slot straight to its pool. i3c_generic_ibi_free_pool() stops
handing out slots and cancels work that has not started. If IBIs were
ever queued, it frees the pool from a work item on the same ordered
workqueue, which runs after any handler still in progress.

This assumes every slot passed to i3c_master_queue_ibi() comes from a
generic IBI pool, which is true for all in-tree drivers.

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@google.com>
---
 drivers/i3c/master.c | 149 +++++++++++++++++++++++++++++++++----------
 1 file changed, 116 insertions(+), 33 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 01e05bad8661..75c44c45bd08 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3619,6 +3619,26 @@ static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
 /* Approximate time for IBI handler to run */
 #define I3C_WAKEUP_PROCESSING_TIME_MS 100
 
+struct i3c_generic_ibi_slot {
+	struct list_head node;
+	struct i3c_ibi_slot base;
+	struct i3c_generic_ibi_pool *pool;
+	struct i3c_device_ibi_info *ibi;
+	struct i3c_device *i3cdev;
+};
+
+struct i3c_generic_ibi_pool {
+	spinlock_t lock;
+	unsigned int num_slots;
+	bool stopping;
+	struct workqueue_struct *wq;
+	struct work_struct free_work;
+	void *payload_buf;
+	struct list_head free_slots;
+	struct list_head pending;
+	struct i3c_generic_ibi_slot slots[] __counted_by(num_slots);
+};
+
 /**
  * i3c_master_queue_ibi() - Queue an IBI
  * @dev: the device this IBI is coming from
@@ -3629,14 +3649,32 @@ static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
  */
 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
 {
-	if (!dev->ibi || !slot)
+	struct i3c_generic_ibi_slot *gslot;
+	struct i3c_generic_ibi_pool *pool;
+	unsigned long flags;
+
+	if (!slot)
 		return;
 
+	gslot = container_of(slot, struct i3c_generic_ibi_slot, base);
+	pool = gslot->pool;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	if (pool->stopping || !dev->ibi || !dev->ibi->wq) {
+		spin_unlock_irqrestore(&pool->lock, flags);
+		i3c_generic_ibi_recycle_slot(pool, slot);
+		return;
+	}
+
 	if (device_may_wakeup(&dev->dev->dev))
 		pm_wakeup_event(&dev->dev->dev, I3C_WAKEUP_PROCESSING_TIME_MS);
 
+	gslot->ibi = dev->ibi;
+	gslot->i3cdev = READ_ONCE(dev->dev);
+	pool->wq = dev->ibi->wq;
 	atomic_inc(&dev->ibi->pending_ibis);
 	queue_work(dev->ibi->wq, &slot->work);
+	spin_unlock_irqrestore(&pool->lock, flags);
 }
 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
 
@@ -3644,18 +3682,27 @@ static void i3c_master_handle_ibi(struct work_struct *work)
 {
 	struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
 						 work);
-	struct i3c_dev_desc *dev = slot->dev;
+	struct i3c_generic_ibi_slot *gslot =
+		container_of(slot, struct i3c_generic_ibi_slot, base);
+	struct i3c_device_ibi_info *ibi = gslot->ibi;
+	struct i3c_device *i3cdev = gslot->i3cdev;
 	struct i3c_ibi_payload payload;
 
 	payload.data = slot->data;
 	payload.len = slot->len;
 
-	if (dev->dev)
-		dev->ibi->handler(dev->dev, &payload);
+	if (i3cdev)
+		ibi->handler(i3cdev, &payload);
 
-	i3c_dev_recycle_ibi_slot_controller(dev, slot);
-	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
-		complete(&dev->ibi->all_ibis_handled);
+	/*
+	 * Recycle the slot directly into its pool rather than through
+	 * i3c_dev_recycle_ibi_slot_controller(slot->dev, slot), as slot->dev
+	 * may have been detached and freed during DAA while the handler slept.
+	 */
+	i3c_generic_ibi_recycle_slot(gslot->pool, slot);
+
+	if (atomic_dec_and_test(&ibi->pending_ibis))
+		complete(&ibi->all_ibis_handled);
 }
 
 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
@@ -3665,27 +3712,7 @@ static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
 }
 
-struct i3c_generic_ibi_slot {
-	struct list_head node;
-	struct i3c_ibi_slot base;
-};
-
-struct i3c_generic_ibi_pool {
-	spinlock_t lock;
-	unsigned int num_slots;
-	void *payload_buf;
-	struct list_head free_slots;
-	struct list_head pending;
-	struct i3c_generic_ibi_slot slots[] __counted_by(num_slots);
-};
-
-/**
- * i3c_generic_ibi_free_pool() - Free a generic IBI pool
- * @pool: the IBI pool to free
- *
- * Free all IBI slots allated by a generic IBI pool.
- */
-void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+static void i3c_generic_ibi_destroy_pool(struct i3c_generic_ibi_pool *pool)
 {
 	struct i3c_generic_ibi_slot *slot;
 	unsigned int nslots = 0;
@@ -3706,6 +3733,56 @@ void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
 	kfree(pool->payload_buf);
 	kfree(pool);
 }
+
+static void i3c_generic_ibi_free_pool_work(struct work_struct *work)
+{
+	struct i3c_generic_ibi_pool *pool =
+		container_of(work, struct i3c_generic_ibi_pool, free_work);
+
+	i3c_generic_ibi_destroy_pool(pool);
+}
+
+/**
+ * i3c_generic_ibi_free_pool() - Free a generic IBI pool
+ * @pool: the IBI pool to free
+ *
+ * Free all IBI slots allocated by a generic IBI pool. If IBIs were ever
+ * queued, IBI work that has not started yet is cancelled and the pool is
+ * freed from the device IBI workqueue, after any handler that is still
+ * running has returned its slot.
+ */
+void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+{
+	struct i3c_generic_ibi_slot *slot;
+	struct i3c_device_ibi_info *ibi;
+	struct workqueue_struct *wq;
+	unsigned long flags;
+	unsigned int i;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	pool->stopping = true;
+	wq = pool->wq;
+	if (!wq) {
+		/* No IBI was ever queued, so none can be in flight. */
+		spin_unlock_irqrestore(&pool->lock, flags);
+		i3c_generic_ibi_destroy_pool(pool);
+		return;
+	}
+
+	for (i = 0; i < pool->num_slots; i++) {
+		slot = &pool->slots[i];
+		if (cancel_work(&slot->base.work)) {
+			ibi = slot->ibi;
+			slot->ibi = NULL;
+			list_add_tail(&slot->node, &pool->free_slots);
+			if (ibi && atomic_dec_and_test(&ibi->pending_ibis))
+				complete(&ibi->all_ibis_handled);
+		}
+	}
+	spin_unlock_irqrestore(&pool->lock, flags);
+
+	queue_work(wq, &pool->free_work);
+}
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
 
 /**
@@ -3735,6 +3812,7 @@ i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
 	spin_lock_init(&pool->lock);
 	INIT_LIST_HEAD(&pool->free_slots);
 	INIT_LIST_HEAD(&pool->pending);
+	INIT_WORK(&pool->free_work, i3c_generic_ibi_free_pool_work);
 
 	if (req->max_payload_len) {
 		pool->payload_buf = kcalloc(req->num_slots,
@@ -3747,6 +3825,7 @@ i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
 
 	for (i = 0; i < req->num_slots; i++) {
 		slot = &pool->slots[i];
+		slot->pool = pool;
 		i3c_master_init_ibi_slot(dev, &slot->base);
 
 		if (req->max_payload_len)
@@ -3800,14 +3879,17 @@ EXPORT_SYMBOL_GPL(i3c_dev_recycle_ibi_slot_controller);
 struct i3c_ibi_slot *
 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
 {
-	struct i3c_generic_ibi_slot *slot;
+	struct i3c_generic_ibi_slot *slot = NULL;
 	unsigned long flags;
 
 	spin_lock_irqsave(&pool->lock, flags);
-	slot = list_first_entry_or_null(&pool->free_slots,
-					struct i3c_generic_ibi_slot, node);
-	if (slot)
-		list_del(&slot->node);
+	if (!pool->stopping) {
+		slot = list_first_entry_or_null(&pool->free_slots,
+						struct i3c_generic_ibi_slot,
+						node);
+		if (slot)
+			list_del(&slot->node);
+	}
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	return slot ? &slot->base : NULL;
@@ -3833,6 +3915,7 @@ void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
 
 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
 	spin_lock_irqsave(&pool->lock, flags);
+	slot->ibi = NULL;
 	list_add_tail(&slot->node, &pool->free_slots);
 	spin_unlock_irqrestore(&pool->lock, flags);
 }
-- 
2.56.1.911.g883d8bcb20-goog


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

* [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing
@ 2026-12-02  2:25   ` Sam Agazaryan
  0 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

When DAA re-adds a device that is already known, as on a Hot-Join after
a target reset, __i3c_master_add_i3c_dev_locked() clears
olddev->ibi->enabled before calling i3c_dev_free_ibi_locked(). That
skips the wait for pending IBIs, so the controller frees its IBI pool
while slot work is still queued or running on dev->ibi->wq. The handler
then recycles its slot through the controller, whose per-device IBI
data is already gone:

  Internal error: Oops: 0000000096000004 [#1] SMP
  Workqueue: 10-20a0168000f i3c_master_handle_ibi
  pc : hci_dma_recycle_ibi_slot+0x1c/0x38
  lr : i3c_hci_recycle_ibi_slot+0x2c/0x40
  Call trace:
   hci_dma_recycle_ibi_slot+0x1c/0x38 (P)
   i3c_hci_recycle_ibi_slot+0x2c/0x40
   i3c_master_handle_ibi+0x78/0xe0
   process_one_work+0x154/0x3b0
   worker_thread+0x28c/0x3a0

Make the pool safe to free with slots in flight. Each slot now records
its pool, and i3c_master_queue_ibi() records the IBI info and device the
handler needs, so i3c_master_handle_ibi() no longer uses slot->dev and
returns the slot straight to its pool. i3c_generic_ibi_free_pool() stops
handing out slots and cancels work that has not started. If IBIs were
ever queued, it frees the pool from a work item on the same ordered
workqueue, which runs after any handler still in progress.

This assumes every slot passed to i3c_master_queue_ibi() comes from a
generic IBI pool, which is true for all in-tree drivers.

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@google.com>
---
 drivers/i3c/master.c | 149 +++++++++++++++++++++++++++++++++----------
 1 file changed, 116 insertions(+), 33 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 01e05bad8661..75c44c45bd08 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3619,6 +3619,26 @@ static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
 /* Approximate time for IBI handler to run */
 #define I3C_WAKEUP_PROCESSING_TIME_MS 100
 
+struct i3c_generic_ibi_slot {
+	struct list_head node;
+	struct i3c_ibi_slot base;
+	struct i3c_generic_ibi_pool *pool;
+	struct i3c_device_ibi_info *ibi;
+	struct i3c_device *i3cdev;
+};
+
+struct i3c_generic_ibi_pool {
+	spinlock_t lock;
+	unsigned int num_slots;
+	bool stopping;
+	struct workqueue_struct *wq;
+	struct work_struct free_work;
+	void *payload_buf;
+	struct list_head free_slots;
+	struct list_head pending;
+	struct i3c_generic_ibi_slot slots[] __counted_by(num_slots);
+};
+
 /**
  * i3c_master_queue_ibi() - Queue an IBI
  * @dev: the device this IBI is coming from
@@ -3629,14 +3649,32 @@ static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
  */
 void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
 {
-	if (!dev->ibi || !slot)
+	struct i3c_generic_ibi_slot *gslot;
+	struct i3c_generic_ibi_pool *pool;
+	unsigned long flags;
+
+	if (!slot)
 		return;
 
+	gslot = container_of(slot, struct i3c_generic_ibi_slot, base);
+	pool = gslot->pool;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	if (pool->stopping || !dev->ibi || !dev->ibi->wq) {
+		spin_unlock_irqrestore(&pool->lock, flags);
+		i3c_generic_ibi_recycle_slot(pool, slot);
+		return;
+	}
+
 	if (device_may_wakeup(&dev->dev->dev))
 		pm_wakeup_event(&dev->dev->dev, I3C_WAKEUP_PROCESSING_TIME_MS);
 
+	gslot->ibi = dev->ibi;
+	gslot->i3cdev = READ_ONCE(dev->dev);
+	pool->wq = dev->ibi->wq;
 	atomic_inc(&dev->ibi->pending_ibis);
 	queue_work(dev->ibi->wq, &slot->work);
+	spin_unlock_irqrestore(&pool->lock, flags);
 }
 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
 
@@ -3644,18 +3682,27 @@ static void i3c_master_handle_ibi(struct work_struct *work)
 {
 	struct i3c_ibi_slot *slot = container_of(work, struct i3c_ibi_slot,
 						 work);
-	struct i3c_dev_desc *dev = slot->dev;
+	struct i3c_generic_ibi_slot *gslot =
+		container_of(slot, struct i3c_generic_ibi_slot, base);
+	struct i3c_device_ibi_info *ibi = gslot->ibi;
+	struct i3c_device *i3cdev = gslot->i3cdev;
 	struct i3c_ibi_payload payload;
 
 	payload.data = slot->data;
 	payload.len = slot->len;
 
-	if (dev->dev)
-		dev->ibi->handler(dev->dev, &payload);
+	if (i3cdev)
+		ibi->handler(i3cdev, &payload);
 
-	i3c_dev_recycle_ibi_slot_controller(dev, slot);
-	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
-		complete(&dev->ibi->all_ibis_handled);
+	/*
+	 * Recycle the slot directly into its pool rather than through
+	 * i3c_dev_recycle_ibi_slot_controller(slot->dev, slot), as slot->dev
+	 * may have been detached and freed during DAA while the handler slept.
+	 */
+	i3c_generic_ibi_recycle_slot(gslot->pool, slot);
+
+	if (atomic_dec_and_test(&ibi->pending_ibis))
+		complete(&ibi->all_ibis_handled);
 }
 
 static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
@@ -3665,27 +3712,7 @@ static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
 }
 
-struct i3c_generic_ibi_slot {
-	struct list_head node;
-	struct i3c_ibi_slot base;
-};
-
-struct i3c_generic_ibi_pool {
-	spinlock_t lock;
-	unsigned int num_slots;
-	void *payload_buf;
-	struct list_head free_slots;
-	struct list_head pending;
-	struct i3c_generic_ibi_slot slots[] __counted_by(num_slots);
-};
-
-/**
- * i3c_generic_ibi_free_pool() - Free a generic IBI pool
- * @pool: the IBI pool to free
- *
- * Free all IBI slots allated by a generic IBI pool.
- */
-void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+static void i3c_generic_ibi_destroy_pool(struct i3c_generic_ibi_pool *pool)
 {
 	struct i3c_generic_ibi_slot *slot;
 	unsigned int nslots = 0;
@@ -3706,6 +3733,56 @@ void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
 	kfree(pool->payload_buf);
 	kfree(pool);
 }
+
+static void i3c_generic_ibi_free_pool_work(struct work_struct *work)
+{
+	struct i3c_generic_ibi_pool *pool =
+		container_of(work, struct i3c_generic_ibi_pool, free_work);
+
+	i3c_generic_ibi_destroy_pool(pool);
+}
+
+/**
+ * i3c_generic_ibi_free_pool() - Free a generic IBI pool
+ * @pool: the IBI pool to free
+ *
+ * Free all IBI slots allocated by a generic IBI pool. If IBIs were ever
+ * queued, IBI work that has not started yet is cancelled and the pool is
+ * freed from the device IBI workqueue, after any handler that is still
+ * running has returned its slot.
+ */
+void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+{
+	struct i3c_generic_ibi_slot *slot;
+	struct i3c_device_ibi_info *ibi;
+	struct workqueue_struct *wq;
+	unsigned long flags;
+	unsigned int i;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	pool->stopping = true;
+	wq = pool->wq;
+	if (!wq) {
+		/* No IBI was ever queued, so none can be in flight. */
+		spin_unlock_irqrestore(&pool->lock, flags);
+		i3c_generic_ibi_destroy_pool(pool);
+		return;
+	}
+
+	for (i = 0; i < pool->num_slots; i++) {
+		slot = &pool->slots[i];
+		if (cancel_work(&slot->base.work)) {
+			ibi = slot->ibi;
+			slot->ibi = NULL;
+			list_add_tail(&slot->node, &pool->free_slots);
+			if (ibi && atomic_dec_and_test(&ibi->pending_ibis))
+				complete(&ibi->all_ibis_handled);
+		}
+	}
+	spin_unlock_irqrestore(&pool->lock, flags);
+
+	queue_work(wq, &pool->free_work);
+}
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
 
 /**
@@ -3735,6 +3812,7 @@ i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
 	spin_lock_init(&pool->lock);
 	INIT_LIST_HEAD(&pool->free_slots);
 	INIT_LIST_HEAD(&pool->pending);
+	INIT_WORK(&pool->free_work, i3c_generic_ibi_free_pool_work);
 
 	if (req->max_payload_len) {
 		pool->payload_buf = kcalloc(req->num_slots,
@@ -3747,6 +3825,7 @@ i3c_generic_ibi_alloc_pool(struct i3c_dev_desc *dev,
 
 	for (i = 0; i < req->num_slots; i++) {
 		slot = &pool->slots[i];
+		slot->pool = pool;
 		i3c_master_init_ibi_slot(dev, &slot->base);
 
 		if (req->max_payload_len)
@@ -3800,14 +3879,17 @@ EXPORT_SYMBOL_GPL(i3c_dev_recycle_ibi_slot_controller);
 struct i3c_ibi_slot *
 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
 {
-	struct i3c_generic_ibi_slot *slot;
+	struct i3c_generic_ibi_slot *slot = NULL;
 	unsigned long flags;
 
 	spin_lock_irqsave(&pool->lock, flags);
-	slot = list_first_entry_or_null(&pool->free_slots,
-					struct i3c_generic_ibi_slot, node);
-	if (slot)
-		list_del(&slot->node);
+	if (!pool->stopping) {
+		slot = list_first_entry_or_null(&pool->free_slots,
+						struct i3c_generic_ibi_slot,
+						node);
+		if (slot)
+			list_del(&slot->node);
+	}
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	return slot ? &slot->base : NULL;
@@ -3833,6 +3915,7 @@ void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
 
 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
 	spin_lock_irqsave(&pool->lock, flags);
+	slot->ibi = NULL;
 	list_add_tail(&slot->node, &pool->free_slots);
 	spin_unlock_irqrestore(&pool->lock, flags);
 }
-- 
2.56.1.911.g883d8bcb20-goog


-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

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

* [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA
  2026-12-02  2:25 ` Sam Agazaryan
@ 2026-12-02  2:25   ` Sam Agazaryan
  -1 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

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@google.com>
---
 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


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

* [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA
@ 2026-12-02  2:25   ` Sam Agazaryan
  0 siblings, 0 replies; 8+ messages in thread
From: Sam Agazaryan @ 2026-12-02  2:25 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Zbigniew Lukwinski,
	Ravindra Yashvant Shinde, linux-kernel, Sam Agazaryan

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@google.com>
---
 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


-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

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

* Re: [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing
  2026-12-02  2:25   ` Sam Agazaryan
  (?)
@ 2026-12-02  2:44   ` sashiko-bot
  -1 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-02  2:44 UTC (permalink / raw)
  To: Sam Agazaryan, linux-i3c; +Cc: Frank.Li, Alexandre Belloni

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] i3c: master: UAF on checked-out IBI slot in
  i3c_generic_ibi_free_pool()
--

commit 992daa004ac4d4fb63c601d96cf2c13eec0369d4
Author: Sam Agazaryan <samagazaryan@google.com>

i3c: master: Drain IBI slot pool before freeing

This commit records the pool and target device metadata on each generic
IBI slot so slots can be recycled directly into their pool, and defers
destroying the pool onto the device IBI workqueue when IBIs have been
queued.

> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index 01e05bad8661e..75c44c45bd08c 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
[ ... ]
> @@ -3629,14 +3649,32 @@ static void i3c_master_unregister_i3c_devs(struct i3c_master_controller *master)
>   */
>  void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot)
>  {
> -	if (!dev->ibi || !slot)
> +	struct i3c_generic_ibi_slot *gslot;
> +	struct i3c_generic_ibi_pool *pool;
> +	unsigned long flags;
> +
> +	if (!slot)
>  		return;
>  
> +	gslot = container_of(slot, struct i3c_generic_ibi_slot, base);
> +	pool = gslot->pool;
> +
> +	spin_lock_irqsave(&pool->lock, flags);
> +	if (pool->stopping || !dev->ibi || !dev->ibi->wq) {
> +		spin_unlock_irqrestore(&pool->lock, flags);
> +		i3c_generic_ibi_recycle_slot(pool, slot);
> +		return;
> +	}
> +
>  	if (device_may_wakeup(&dev->dev->dev))
>  		pm_wakeup_event(&dev->dev->dev, I3C_WAKEUP_PROCESSING_TIME_MS);
>  
> +	gslot->ibi = dev->ibi;
> +	gslot->i3cdev = READ_ONCE(dev->dev);
> +	pool->wq = dev->ibi->wq;
>  	atomic_inc(&dev->ibi->pending_ibis);
>  	queue_work(dev->ibi->wq, &slot->work);
> +	spin_unlock_irqrestore(&pool->lock, flags);
>  }
[ ... ]
> @@ -3706,6 +3733,56 @@ void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
>  	kfree(pool->payload_buf);
>  	kfree(pool);
>  }
> +
> +static void i3c_generic_ibi_free_pool_work(struct work_struct *work)
> +{
> +	struct i3c_generic_ibi_pool *pool =
> +		container_of(work, struct i3c_generic_ibi_pool, free_work);
> +
> +	i3c_generic_ibi_destroy_pool(pool);
> +}
> +
> +/**
> + * i3c_generic_ibi_free_pool() - Free a generic IBI pool
> + * @pool: the IBI pool to free
> + *
> + * Free all IBI slots allocated by a generic IBI pool. If IBIs were ever
> + * queued, IBI work that has not started yet is cancelled and the pool is
> + * freed from the device IBI workqueue, after any handler that is still
> + * running has returned its slot.
> + */
> +void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
> +{
> +	struct i3c_generic_ibi_slot *slot;
> +	struct i3c_device_ibi_info *ibi;
> +	struct workqueue_struct *wq;
> +	unsigned long flags;
> +	unsigned int i;
> +
> +	spin_lock_irqsave(&pool->lock, flags);
> +	pool->stopping = true;
> +	wq = pool->wq;
> +	if (!wq) {
> +		/* No IBI was ever queued, so none can be in flight. */
> +		spin_unlock_irqrestore(&pool->lock, flags);
> +		i3c_generic_ibi_destroy_pool(pool);
> +		return;
> +	}
> +
> +	for (i = 0; i < pool->num_slots; i++) {
> +		slot = &pool->slots[i];
> +		if (cancel_work(&slot->base.work)) {
> +			ibi = slot->ibi;
> +			slot->ibi = NULL;
> +			list_add_tail(&slot->node, &pool->free_slots);
> +			if (ibi && atomic_dec_and_test(&ibi->pending_ibis))
> +				complete(&ibi->all_ibis_handled);
> +		}
> +	}
> +	spin_unlock_irqrestore(&pool->lock, flags);
> +
> +	queue_work(wq, &pool->free_work);
> +}

[Severity: High]
Can i3c_generic_ibi_free_pool() destroy the pool while a slot checked out
via i3c_generic_ibi_get_free_slot() has not yet reached
i3c_master_queue_ibi() or i3c_generic_ibi_recycle_slot()?

Controller drivers check out a slot with i3c_generic_ibi_get_free_slot(),
populate slot->data outside pool->lock, and only then pass the slot to
i3c_master_queue_ibi() (which the pool->stopping check in
i3c_master_queue_ibi() anticipates).  For example, the MIPI I3C HCI PIO
driver holds a checked-out slot in pio->ibi.slot across multiple segment
interrupts before queueing it, and svc_i3c_master_ibi_isr() checks out a
slot under master->xferqueue.lock while svc_i3c_master_free_ibi() only
takes master->ibi.lock before calling i3c_generic_ibi_free_pool().

CPU0                                  CPU1
i3c_generic_ibi_get_free_slot(pool)
  list_del(&slot->node)
                                      i3c_generic_ibi_free_pool(pool)
                                        pool->stopping = true
                                        /* pool->wq is still NULL */
                                        i3c_generic_ibi_destroy_pool(pool)
                                          WARN_ON(nslots != pool->num_slots)
                                          kfree(pool->payload_buf)
                                          kfree(pool)
memcpy(slot->data, ...)
  <-- use-after-free on payload_buf
i3c_master_queue_ibi(dev, slot)
  spin_lock_irqsave(&pool->lock, ...)
    <-- use-after-free on pool

Even when pool->wq is non-NULL from an earlier IBI, cancel_work() returns
false for a checked-out slot that has not been queued yet, and
pool->free_work can run on wq and free pool->payload_buf and pool before
the controller finishes filling or recycling the slot.  In addition, when
i3c_master_queue_ibi() sees pool->stopping, it drops pool->lock before
calling i3c_generic_ibi_recycle_slot(), leaving a window where the slot is
still off pool->free_slots while i3c_generic_ibi_destroy_pool() runs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202022502.3661797-1-samagazaryan@google.com?part=1

-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

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

* Re: [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA
  2026-12-02  2:25   ` Sam Agazaryan
  (?)
@ 2026-12-02  2:48   ` sashiko-bot
  -1 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-02  2:48 UTC (permalink / raw)
  To: Sam Agazaryan, linux-i3c; +Cc: Frank.Li, Alexandre Belloni

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] i3c: master: use-after-free in i3c_generic_ibi_free_pool() on
  checked-out slot
- [Medium] i3c: master: UAF on async IBI wq teardown in
  __i3c_master_add_i3c_dev_locked()
--

commit 4b74a8ce04bb2a901a13cfbe1ec9e6788a44f476
Author: Sam Agazaryan <samagazaryan@google.com>

i3c: master: Preserve IBI workqueue across DAA

This commit avoids destroying the old device descriptor's IBI workqueue
under the bus maintenance lock during DAA by moving the IBI info and its
workqueue to the new descriptor and deferring workqueue destruction on
failure.

> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index 75c44c45bd08c..22fe970ce3ac1 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
[ ... ]
> @@ -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);

[Severity: High]
Can freeing olddev's controller IBI pool here cause a use-after-free on
pool and pool->payload_buf if the controller still holds a slot checked
out via i3c_generic_ibi_get_free_slot()?

Commit "i3c: master: Drain IBI slot pool before freeing" adds a
pool->stopping check in i3c_master_queue_ibi() to recycle a slot that was
checked out before i3c_generic_ibi_free_pool() ran, but
i3c_generic_ibi_free_pool() only drains work items already queued on
pool->wq.  A checked-out slot that has not been passed to
i3c_master_queue_ibi() yet is neither on pool->free_slots nor queued on
pool->wq.  For example, mipi-i3c-hci checks out pio->ibi.slot in
hci_pio_prep_new_ibi() and holds it across interrupts when an IBI spans
multiple segments, while hci_pio_free_ibi() calls
i3c_generic_ibi_free_pool() without clearing pio->ibi.slot:

CPU0                                  CPU1
hci_pio_irq_handler()
  hci_pio_prep_new_ibi()
    slot = i3c_generic_ibi_get_free_slot(pool)
  (awaits next IBI segment)
                                      __i3c_master_add_i3c_dev_locked()
                                        i3c_dev_free_ibi_controller_locked()
                                          ...
                                          i3c_generic_ibi_free_pool(pool)
                                            (pool->wq is NULL, or
                                             free_work runs on pool->wq)
                                            i3c_generic_ibi_destroy_pool(pool)
                                              WARN_ON(nslots != pool->num_slots)
                                              kfree(pool->payload_buf)
                                              kfree(pool)
hci_pio_irq_handler()
  writes into slot->data  <-- use-after-free
  i3c_master_queue_ibi(..., slot)  <-- use-after-free

The same race also exists in svc-i3c-master, where svc_i3c_master_ibi_isr()
checks out and queues a slot under master->xferqueue.lock while
svc_i3c_master_free_ibi() only takes master->ibi.lock before calling
i3c_generic_ibi_free_pool().

> @@ -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)

[Severity: Medium]
If i3c_dev_request_ibi_controller_locked() fails while an IBI handler is
still running on old_ibi->wq, can setting newdev->ibi = NULL and deferring
destroy_workqueue() to system_dfl_wq cause a use-after-free on driver
teardown?

Once newdev->ibi is cleared to NULL, a subsequent driver unbind or
i3c_master_unregister() calls i3c_device_disable_ibi() and
i3c_device_free_ibi(), both of which see dev->ibi == NULL and return
immediately without waiting for old_ibi->wq to drain:

CPU0 (old_ibi->wq)                CPU1
i3c_master_handle_ibi()
  ibi->handler(i3cdev, &payload)
    mctp_i3c_ibi_handler()
      (blocked on bus lock)
                                  __i3c_master_add_i3c_dev_locked()
                                    i3c_dev_request_ibi_controller_locked()
                                      (fails)
                                    newdev->ibi = NULL
                                    queue_work(..., &old_ibi->destroy_work)
                                  (releases bus lock)
                                  mctp_i3c_remove_device()
                                    i3c_device_disable_ibi() -> no-op
                                    i3c_device_free_ibi() -> no-op
                                    kfree(mi)
      ...
      accesses mi  <-- use-after-free

For the same reason, i3c_master_unregister_i3c_devs() can drop the final
reference on i3cdev->dev while ibi->handler(i3cdev, &payload) is still
using i3cdev.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202022502.3661797-1-samagazaryan@google.com?part=2

-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

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

end of thread, other threads:[~2026-12-02  2:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:25 [PATCH RFC 0/2] i3c: master: Fix IBI teardown when DAA re-adds a device Sam Agazaryan
2026-12-02  2:25 ` Sam Agazaryan
2026-12-02  2:25 ` [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing Sam Agazaryan
2026-12-02  2:25   ` Sam Agazaryan
2026-12-02  2:44   ` sashiko-bot
2026-12-02  2:25 ` [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA Sam Agazaryan
2026-12-02  2:25   ` Sam Agazaryan
2026-12-02  2:48   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.