All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device
@ 2026-12-05  6:55 ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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 3 makes the pool
safe to free while slots are in use. Patch 2 moves the pool structures
it needs.

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

With patch 3, a pool is freed only once all its slots are back, so a
slot that a controller never returns keeps the pool allocated, where
i3c_generic_ibi_free_pool() used to warn and free it anyway.
dw-i3c-master leaks a slot when an IBI payload is too long, and patch 1
fixes that.

Patch 3 also makes the core return IBI slots to their generic pool
itself, so it no longer calls ->recycle_ibi_slot(). Every in-tree
controller driver that supports IBIs uses the generic pool, and its
->recycle_ibi_slot() just wraps i3c_generic_ibi_recycle_slot(). If you
agree, a follow-up can remove ->recycle_ibi_slot() from those drivers
and the hub, along with i3c_dev_recycle_ibi_slot_controller().

Changes in v2:
- Drop RFC (Frank).
- New patch 1: return the IBI slot dw-i3c-master leaks on an oversized
  payload. With patch 3, that slot would keep the whole pool allocated.
- Move the generic IBI pool structures in a separate patch (Frank).
- Use guard() and scoped_guard() for the pool lock (Frank).
- Patch 3: count slots in use and free the pool when the last one comes
  back, so a slot the controller has taken but not queued yet keeps the
  pool alive (Sashiko).
- Patch 3: update the IBI slot API documentation to match (Sashiko).
- Patch 4: if requesting controller IBI resources for the new descriptor
  fails, keep the IBI info on it so driver teardown still waits for a
  running handler (Sashiko).
- Rebase on i3c/next.
- v1: https://lore.kernel.org/all/20261202022502.3661797-1-samagazaryan@google.com/

Both failures first showed up in host reset loops, in fewer than 1 in 10
resets. I 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 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 with a 5 ms sleep hit
the WARN in i3c_generic_ibi_free_pool() and then the NULL dereference
shown in patch 3. With only v1's pool change, the second rescan
deadlocked as shown in patch 4.

With v2, 250 rescans on two buses with 0, 5 and 20 ms sleeps ran clean,
and MCTP recovered after every one. Unbinding mctp-i3c with the host
powered off, so that DISEC is NACKed, gave no warnings. To cover the
error path in patch 4, a test-only switch made the controller IBI
request after DAA fail: the device stopped receiving IBIs, and the next
unbind, rebind and DAA all completed cleanly.

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.

Sashiko also pointed out two older problems that this series does not
change:
- Since 9fd00df05e81, IBI handlers run on a per-device workqueue that is
  not freezable, so a queued handler can run during system suspend after
  its controller has suspended.
- i3c_dev_free_ibi_locked() frees the IBI even when disabling it failed.
  mipi-i3c-hci copes because its ->free_ibi() removes the device from
  its IBI lookup table, but svc-i3c-master can still pass such a
  target's IBI to its handler, which then uses the freed slot pool and
  the NULL dev->ibi.
I can follow up on both separately.

v2 is build-tested on this base for arm64 with all I3C drivers enabled,
with W=1 and sparse. I have not tested patch 1 on hardware.

Sam Agazaryan (4):
  i3c: dw: Fix IBI slot leak on oversized payload
  i3c: master: Move generic IBI pool definitions
  i3c: master: Drain IBI slot pool before freeing
  i3c: master: Preserve IBI workqueue across DAA

 drivers/i3c/master.c               | 258 ++++++++++++++++++++++-------
 drivers/i3c/master/dw-i3c-master.c |   1 +
 include/linux/i3c/master.h         |  17 +-
 3 files changed, 210 insertions(+), 66 deletions(-)


base-commit: 7e0cef0dcbe0c561f09e8bbb950617ac36fb7dcb
-- 
2.56.1.915.g69d5ec7298-goog


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

* [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device
@ 2026-12-05  6:55 ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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 3 makes the pool
safe to free while slots are in use. Patch 2 moves the pool structures
it needs.

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

With patch 3, a pool is freed only once all its slots are back, so a
slot that a controller never returns keeps the pool allocated, where
i3c_generic_ibi_free_pool() used to warn and free it anyway.
dw-i3c-master leaks a slot when an IBI payload is too long, and patch 1
fixes that.

Patch 3 also makes the core return IBI slots to their generic pool
itself, so it no longer calls ->recycle_ibi_slot(). Every in-tree
controller driver that supports IBIs uses the generic pool, and its
->recycle_ibi_slot() just wraps i3c_generic_ibi_recycle_slot(). If you
agree, a follow-up can remove ->recycle_ibi_slot() from those drivers
and the hub, along with i3c_dev_recycle_ibi_slot_controller().

Changes in v2:
- Drop RFC (Frank).
- New patch 1: return the IBI slot dw-i3c-master leaks on an oversized
  payload. With patch 3, that slot would keep the whole pool allocated.
- Move the generic IBI pool structures in a separate patch (Frank).
- Use guard() and scoped_guard() for the pool lock (Frank).
- Patch 3: count slots in use and free the pool when the last one comes
  back, so a slot the controller has taken but not queued yet keeps the
  pool alive (Sashiko).
- Patch 3: update the IBI slot API documentation to match (Sashiko).
- Patch 4: if requesting controller IBI resources for the new descriptor
  fails, keep the IBI info on it so driver teardown still waits for a
  running handler (Sashiko).
- Rebase on i3c/next.
- v1: https://lore.kernel.org/all/20261202022502.3661797-1-samagazaryan@google.com/

Both failures first showed up in host reset loops, in fewer than 1 in 10
resets. I 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 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 with a 5 ms sleep hit
the WARN in i3c_generic_ibi_free_pool() and then the NULL dereference
shown in patch 3. With only v1's pool change, the second rescan
deadlocked as shown in patch 4.

With v2, 250 rescans on two buses with 0, 5 and 20 ms sleeps ran clean,
and MCTP recovered after every one. Unbinding mctp-i3c with the host
powered off, so that DISEC is NACKed, gave no warnings. To cover the
error path in patch 4, a test-only switch made the controller IBI
request after DAA fail: the device stopped receiving IBIs, and the next
unbind, rebind and DAA all completed cleanly.

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.

Sashiko also pointed out two older problems that this series does not
change:
- Since 9fd00df05e81, IBI handlers run on a per-device workqueue that is
  not freezable, so a queued handler can run during system suspend after
  its controller has suspended.
- i3c_dev_free_ibi_locked() frees the IBI even when disabling it failed.
  mipi-i3c-hci copes because its ->free_ibi() removes the device from
  its IBI lookup table, but svc-i3c-master can still pass such a
  target's IBI to its handler, which then uses the freed slot pool and
  the NULL dev->ibi.
I can follow up on both separately.

v2 is build-tested on this base for arm64 with all I3C drivers enabled,
with W=1 and sparse. I have not tested patch 1 on hardware.

Sam Agazaryan (4):
  i3c: dw: Fix IBI slot leak on oversized payload
  i3c: master: Move generic IBI pool definitions
  i3c: master: Drain IBI slot pool before freeing
  i3c: master: Preserve IBI workqueue across DAA

 drivers/i3c/master.c               | 258 ++++++++++++++++++++++-------
 drivers/i3c/master/dw-i3c-master.c |   1 +
 include/linux/i3c/master.h         |  17 +-
 3 files changed, 210 insertions(+), 66 deletions(-)


base-commit: 7e0cef0dcbe0c561f09e8bbb950617ac36fb7dcb
-- 
2.56.1.915.g69d5ec7298-goog


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

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

* [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload
  2026-12-05  6:55 ` Sam Agazaryan
@ 2026-12-05  6:55   ` Sam Agazaryan
  -1 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	linux-kernel, Sam Agazaryan

dw_i3c_master_handle_ibi_sir() takes a slot from the generic IBI pool
before it checks the payload length. When the payload is longer than
the device driver asked for, it drains the IBI and returns without
giving the slot back. Each such IBI leaks a slot. Once the pool is
empty, every later IBI from that device is dropped, and freeing the
IBI makes i3c_generic_ibi_free_pool() warn about the missing slots.

Return the slot to the pool before draining.

Fixes: e389b1d72a62 ("i3c: dw: Add support for in-band interrupts")
Signed-off-by: Sam Agazaryan <samagazaryan@google.com>
---

Notes:
    New in v2. Patch 3 frees a pool only once all its slots are back, so
    without this fix the leaked slot would keep the pool allocated.

 drivers/i3c/master/dw-i3c-master.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c
index 76875301b5ce..9434d1383033 100644
--- a/drivers/i3c/master/dw-i3c-master.c
+++ b/drivers/i3c/master/dw-i3c-master.c
@@ -1486,6 +1486,7 @@ static void dw_i3c_master_handle_ibi_sir(struct dw_i3c_master *master,
 		dev_dbg_ratelimited(&master->base.dev,
 				    "IBI payload len %d greater than max %d\n",
 				    len, dev->ibi->max_payload_len);
+		i3c_generic_ibi_recycle_slot(data->ibi_pool, slot);
 		goto err_drain;
 	}
 
-- 
2.56.1.915.g69d5ec7298-goog


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

* [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload
@ 2026-12-05  6:55   ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	linux-kernel, Sam Agazaryan

dw_i3c_master_handle_ibi_sir() takes a slot from the generic IBI pool
before it checks the payload length. When the payload is longer than
the device driver asked for, it drains the IBI and returns without
giving the slot back. Each such IBI leaks a slot. Once the pool is
empty, every later IBI from that device is dropped, and freeing the
IBI makes i3c_generic_ibi_free_pool() warn about the missing slots.

Return the slot to the pool before draining.

Fixes: e389b1d72a62 ("i3c: dw: Add support for in-band interrupts")
Signed-off-by: Sam Agazaryan <samagazaryan@google.com>
---

Notes:
    New in v2. Patch 3 frees a pool only once all its slots are back, so
    without this fix the leaked slot would keep the pool allocated.

 drivers/i3c/master/dw-i3c-master.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c
index 76875301b5ce..9434d1383033 100644
--- a/drivers/i3c/master/dw-i3c-master.c
+++ b/drivers/i3c/master/dw-i3c-master.c
@@ -1486,6 +1486,7 @@ static void dw_i3c_master_handle_ibi_sir(struct dw_i3c_master *master,
 		dev_dbg_ratelimited(&master->base.dev,
 				    "IBI payload len %d greater than max %d\n",
 				    len, dev->ibi->max_payload_len);
+		i3c_generic_ibi_recycle_slot(data->ibi_pool, slot);
 		goto err_drain;
 	}
 
-- 
2.56.1.915.g69d5ec7298-goog


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

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

* [PATCH v2 2/4] i3c: master: Move generic IBI pool definitions
  2026-12-05  6:55 ` Sam Agazaryan
@ 2026-12-05  6:55   ` Sam Agazaryan
  -1 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	linux-kernel, Sam Agazaryan

The next patch uses struct i3c_generic_ibi_slot and struct
i3c_generic_ibi_pool in i3c_master_queue_ibi() and
i3c_master_handle_ibi(). Move both definitions above those functions.

No functional change.

Signed-off-by: Sam Agazaryan <samagazaryan@google.com>
---

Notes:
    New in v2, split out of the next patch (Frank).

 drivers/i3c/master.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 2bd1bd25c674..17f9e8025e49 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3660,6 +3660,20 @@ 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 {
+	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_master_queue_ibi() - Queue an IBI
  * @dev: the device this IBI is coming from
@@ -3706,20 +3720,6 @@ 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
-- 
2.56.1.915.g69d5ec7298-goog


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

* [PATCH v2 2/4] i3c: master: Move generic IBI pool definitions
@ 2026-12-05  6:55   ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	linux-kernel, Sam Agazaryan

The next patch uses struct i3c_generic_ibi_slot and struct
i3c_generic_ibi_pool in i3c_master_queue_ibi() and
i3c_master_handle_ibi(). Move both definitions above those functions.

No functional change.

Signed-off-by: Sam Agazaryan <samagazaryan@google.com>
---

Notes:
    New in v2, split out of the next patch (Frank).

 drivers/i3c/master.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 2bd1bd25c674..17f9e8025e49 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3660,6 +3660,20 @@ 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 {
+	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_master_queue_ibi() - Queue an IBI
  * @dev: the device this IBI is coming from
@@ -3706,20 +3720,6 @@ 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
-- 
2.56.1.915.g69d5ec7298-goog


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

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

* [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing
  2026-12-05  6:55 ` Sam Agazaryan
@ 2026-12-05  6:55   ` Sam Agazaryan
  -1 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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

A controller can also free the pool while it still holds a slot it has
not queued yet. mipi-i3c-hci in PIO mode, for example, keeps a slot
across interrupts until the whole IBI payload has arrived.

Make the pool safe to free while slots are in use. 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. The pool counts the slots it has
handed out. i3c_generic_ibi_free_pool() stops handing out slots and
cancels IBI work that has not started, and the pool is freed when its
last slot comes back: in i3c_generic_ibi_free_pool() itself, when the
controller returns a slot it still held, or when a running handler
finishes.

This assumes every slot passed to i3c_master_queue_ibi() comes from a
generic IBI pool, which is true for all in-tree drivers. A slot that a
controller never returns now keeps its pool allocated, where
i3c_generic_ibi_free_pool() used to warn and free the pool anyway.

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

Notes:
    Changes in v2:
    - Count slots in use and free the pool when the last one comes back,
      instead of from a work item on the IBI workqueue. This also covers a
      slot the controller has taken but not queued yet (Sashiko).
    - Use guard() and scoped_guard() for the pool lock (Frank).
    - Track queued slots on the existing pool->pending list.
    - Document the pool and slot structures, including what the lock
      protects.
    - Update the IBI slot API documentation now that the core returns slots
      to their generic pool itself (Sashiko).

 drivers/i3c/master.c       | 178 +++++++++++++++++++++++++++++--------
 include/linux/i3c/master.h |  12 +--
 2 files changed, 149 insertions(+), 41 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 17f9e8025e49..e52254f65145 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3660,14 +3660,41 @@ 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 - slot of a generic IBI pool
+ * @node: entry in the free_slots or pending list of @pool
+ * @base: the slot handed to the controller driver
+ * @pool: the pool this slot belongs to
+ * @ibi: IBI info of the device the queued IBI came from
+ * @i3cdev: device passed to the IBI handler
+ */
 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 - generic IBI slot pool
+ * @lock: protects @num_in_use, @stopping, both slot lists, and the @ibi and
+ *	  @i3cdev members of each slot
+ * @num_slots: number of entries in @slots
+ * @num_in_use: slots handed out and not returned yet
+ * @stopping: set by i3c_generic_ibi_free_pool(). The pool is freed once
+ *	      @num_in_use drops to 0
+ * @payload_buf: payload storage shared by all slots
+ * @free_slots: slots available to the controller driver
+ * @pending: slots queued to the device IBI workqueue whose handler has not
+ *	     started yet
+ * @slots: the slots
+ */
 struct i3c_generic_ibi_pool {
 	spinlock_t lock;
 	unsigned int num_slots;
+	unsigned int num_in_use;
+	bool stopping;
 	void *payload_buf;
 	struct list_head free_slots;
 	struct list_head pending;
@@ -3684,14 +3711,32 @@ struct i3c_generic_ibi_pool {
  */
 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;
+
+	if (!slot)
 		return;
 
-	if (device_may_wakeup(&dev->dev->dev))
-		pm_wakeup_event(&dev->dev->dev, I3C_WAKEUP_PROCESSING_TIME_MS);
+	gslot = container_of(slot, struct i3c_generic_ibi_slot, base);
+	pool = gslot->pool;
+
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		if (!pool->stopping && dev->ibi && dev->ibi->wq) {
+			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);
+			list_add_tail(&gslot->node, &pool->pending);
+			atomic_inc(&dev->ibi->pending_ibis);
+			queue_work(dev->ibi->wq, &slot->work);
+			return;
+		}
+	}
 
-	atomic_inc(&dev->ibi->pending_ibis);
-	queue_work(dev->ibi->wq, &slot->work);
+	/* The pool or the device IBI is being torn down, drop this IBI. */
+	i3c_generic_ibi_recycle_slot(pool, slot);
 }
 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
 
@@ -3699,18 +3744,35 @@ 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_generic_ibi_pool *pool = gslot->pool;
+	struct i3c_device_ibi_info *ibi;
+	struct i3c_device *i3cdev;
 	struct i3c_ibi_payload payload;
 
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		list_del(&gslot->node);
+		ibi = gslot->ibi;
+		i3cdev = gslot->i3cdev;
+	}
+
 	payload.data = slot->data;
 	payload.len = slot->len;
 
-	if (dev->dev)
-		dev->ibi->handler(dev->dev, &payload);
+	if (i3cdev)
+		ibi->handler(i3cdev, &payload);
+
+	/*
+	 * 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.
+	 * This can free the pool, so the slot must not be used afterwards.
+	 */
+	i3c_generic_ibi_recycle_slot(pool, slot);
 
-	i3c_dev_recycle_ibi_slot_controller(dev, slot);
-	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
-		complete(&dev->ibi->all_ibis_handled);
+	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,
@@ -3720,13 +3782,7 @@ static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
 }
 
-/**
- * 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;
@@ -3747,6 +3803,42 @@ void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
 	kfree(pool->payload_buf);
 	kfree(pool);
 }
+
+/**
+ * i3c_generic_ibi_free_pool() - Free a generic IBI pool
+ * @pool: the IBI pool to free
+ *
+ * Stop handing out slots from a generic IBI pool and cancel IBI work that
+ * has not started yet. The pool is freed once all its slots are back: right
+ * away if none is in use, otherwise when the controller or a running IBI
+ * handler returns the last one.
+ */
+void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+{
+	struct i3c_generic_ibi_slot *slot, *tmp;
+	struct i3c_device_ibi_info *ibi;
+	bool destroy;
+
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		pool->stopping = true;
+		list_for_each_entry_safe(slot, tmp, &pool->pending, node) {
+			/* A handler that has started returns its own slot. */
+			if (!cancel_work(&slot->base.work))
+				continue;
+
+			ibi = slot->ibi;
+			slot->ibi = NULL;
+			list_move_tail(&slot->node, &pool->free_slots);
+			pool->num_in_use--;
+			if (atomic_dec_and_test(&ibi->pending_ibis))
+				complete(&ibi->all_ibis_handled);
+		}
+		destroy = !pool->num_in_use;
+	}
+
+	if (destroy)
+		i3c_generic_ibi_destroy_pool(pool);
+}
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
 
 /**
@@ -3788,6 +3880,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)
@@ -3813,10 +3906,10 @@ EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
  *
  * Invoke the current controller's IBI slot recycling callback.
  *
- * Context: Called from the generic IBI work handler in workqueue context.
- * No bus lock is taken here: the controller owns its IBI pool and is
- * responsible for synchronizing access to it. The generic pool
- * implementation uses its own spinlock.
+ * Context: The generic IBI work handler no longer calls this, it returns
+ * slots to their generic pool directly. No bus lock is taken here: the
+ * controller owns its IBI pool and is responsible for synchronizing access to
+ * it. The generic pool implementation uses its own spinlock.
  */
 void i3c_dev_recycle_ibi_slot_controller(struct i3c_dev_desc *dev,
 					 struct i3c_ibi_slot *slot)
@@ -3836,22 +3929,27 @@ EXPORT_SYMBOL_GPL(i3c_dev_recycle_ibi_slot_controller);
  * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
  * when it's no longer needed.
  *
- * Return: a pointer to a free slot, or NULL if there's no free slot available.
+ * Return: a pointer to a free slot, or NULL if there's no free slot available
+ * or the pool is being freed.
  */
 struct i3c_ibi_slot *
 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
 {
 	struct i3c_generic_ibi_slot *slot;
-	unsigned long flags;
 
-	spin_lock_irqsave(&pool->lock, flags);
+	guard(spinlock_irqsave)(&pool->lock);
+	if (pool->stopping)
+		return NULL;
+
 	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);
+	if (!slot)
+		return NULL;
+
+	list_del(&slot->node);
+	pool->num_in_use++;
 
-	return slot ? &slot->base : NULL;
+	return &slot->base;
 }
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
 
@@ -3860,22 +3958,32 @@ EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
  * @pool: the pool to return the IBI slot to
  * @s: IBI slot to recycle
  *
- * Add an IBI slot back to its generic IBI pool. Should be called from the
- * master driver struct_master_controller_ops->recycle_ibi() method.
+ * Add an IBI slot back to its generic IBI pool. The core calls this once the
+ * IBI handler has run. A controller driver calls it to return a slot it took
+ * with i3c_generic_ibi_get_free_slot() but did not queue. If
+ * i3c_generic_ibi_free_pool() has been called and this was the last slot in
+ * use, the pool is freed.
  */
 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
 				  struct i3c_ibi_slot *s)
 {
 	struct i3c_generic_ibi_slot *slot;
-	unsigned long flags;
+	bool destroy;
 
 	if (!s)
 		return;
 
 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
-	spin_lock_irqsave(&pool->lock, flags);
-	list_add_tail(&slot->node, &pool->free_slots);
-	spin_unlock_irqrestore(&pool->lock, flags);
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		slot->ibi = NULL;
+		list_add_tail(&slot->node, &pool->free_slots);
+		pool->num_in_use--;
+		destroy = pool->stopping && !pool->num_in_use;
+	}
+
+	/* The last slot is back after i3c_generic_ibi_free_pool(). */
+	if (destroy)
+		i3c_generic_ibi_destroy_pool(pool);
 }
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
 
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
index b04fdadbbe69..f0b8fe89a3cc 100644
--- a/include/linux/i3c/master.h
+++ b/include/linux/i3c/master.h
@@ -111,8 +111,9 @@ struct i2c_dev_desc {
  * slot in its IBI slot pool, retrieve the IBI payload and queue the IBI using
  * i3c_master_queue_ibi().
  *
- * How IBI slots are allocated is left to the I3C master driver, though, for
- * simple kmalloc-based allocation, the generic IBI slot pool can be used.
+ * IBI slots must come from a generic IBI slot pool, see
+ * i3c_generic_ibi_alloc_pool(). Once the IBI handler has run, the core returns
+ * the slot to its pool.
  */
 struct i3c_ibi_slot {
 	struct work_struct work;
@@ -467,10 +468,9 @@ struct i3c_bus {
  *		 flag set and then deactivate the hardware IRQ on the
  *		 controller end.
  *		 This method is mandatory only if ->request_ibi is not NULL.
- * @recycle_ibi_slot: recycle an IBI slot. Called every time an IBI has been
- *		      processed by its handler. The IBI slot should be put back
- *		      in the IBI slot pool so that the controller can re-use it
- *		      for a future IBI
+ * @recycle_ibi_slot: recycle an IBI slot. The core no longer calls this: it
+ *		      returns IBI slots to their generic pool itself once the
+ *		      IBI handler has run.
  *		      This method is mandatory only if ->request_ibi is not
  *		      NULL.
  * @enable_hotjoin: enable hot join event detect.
-- 
2.56.1.915.g69d5ec7298-goog


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

* [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing
@ 2026-12-05  6:55   ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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

A controller can also free the pool while it still holds a slot it has
not queued yet. mipi-i3c-hci in PIO mode, for example, keeps a slot
across interrupts until the whole IBI payload has arrived.

Make the pool safe to free while slots are in use. 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. The pool counts the slots it has
handed out. i3c_generic_ibi_free_pool() stops handing out slots and
cancels IBI work that has not started, and the pool is freed when its
last slot comes back: in i3c_generic_ibi_free_pool() itself, when the
controller returns a slot it still held, or when a running handler
finishes.

This assumes every slot passed to i3c_master_queue_ibi() comes from a
generic IBI pool, which is true for all in-tree drivers. A slot that a
controller never returns now keeps its pool allocated, where
i3c_generic_ibi_free_pool() used to warn and free the pool anyway.

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

Notes:
    Changes in v2:
    - Count slots in use and free the pool when the last one comes back,
      instead of from a work item on the IBI workqueue. This also covers a
      slot the controller has taken but not queued yet (Sashiko).
    - Use guard() and scoped_guard() for the pool lock (Frank).
    - Track queued slots on the existing pool->pending list.
    - Document the pool and slot structures, including what the lock
      protects.
    - Update the IBI slot API documentation now that the core returns slots
      to their generic pool itself (Sashiko).

 drivers/i3c/master.c       | 178 +++++++++++++++++++++++++++++--------
 include/linux/i3c/master.h |  12 +--
 2 files changed, 149 insertions(+), 41 deletions(-)

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 17f9e8025e49..e52254f65145 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -3660,14 +3660,41 @@ 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 - slot of a generic IBI pool
+ * @node: entry in the free_slots or pending list of @pool
+ * @base: the slot handed to the controller driver
+ * @pool: the pool this slot belongs to
+ * @ibi: IBI info of the device the queued IBI came from
+ * @i3cdev: device passed to the IBI handler
+ */
 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 - generic IBI slot pool
+ * @lock: protects @num_in_use, @stopping, both slot lists, and the @ibi and
+ *	  @i3cdev members of each slot
+ * @num_slots: number of entries in @slots
+ * @num_in_use: slots handed out and not returned yet
+ * @stopping: set by i3c_generic_ibi_free_pool(). The pool is freed once
+ *	      @num_in_use drops to 0
+ * @payload_buf: payload storage shared by all slots
+ * @free_slots: slots available to the controller driver
+ * @pending: slots queued to the device IBI workqueue whose handler has not
+ *	     started yet
+ * @slots: the slots
+ */
 struct i3c_generic_ibi_pool {
 	spinlock_t lock;
 	unsigned int num_slots;
+	unsigned int num_in_use;
+	bool stopping;
 	void *payload_buf;
 	struct list_head free_slots;
 	struct list_head pending;
@@ -3684,14 +3711,32 @@ struct i3c_generic_ibi_pool {
  */
 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;
+
+	if (!slot)
 		return;
 
-	if (device_may_wakeup(&dev->dev->dev))
-		pm_wakeup_event(&dev->dev->dev, I3C_WAKEUP_PROCESSING_TIME_MS);
+	gslot = container_of(slot, struct i3c_generic_ibi_slot, base);
+	pool = gslot->pool;
+
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		if (!pool->stopping && dev->ibi && dev->ibi->wq) {
+			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);
+			list_add_tail(&gslot->node, &pool->pending);
+			atomic_inc(&dev->ibi->pending_ibis);
+			queue_work(dev->ibi->wq, &slot->work);
+			return;
+		}
+	}
 
-	atomic_inc(&dev->ibi->pending_ibis);
-	queue_work(dev->ibi->wq, &slot->work);
+	/* The pool or the device IBI is being torn down, drop this IBI. */
+	i3c_generic_ibi_recycle_slot(pool, slot);
 }
 EXPORT_SYMBOL_GPL(i3c_master_queue_ibi);
 
@@ -3699,18 +3744,35 @@ 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_generic_ibi_pool *pool = gslot->pool;
+	struct i3c_device_ibi_info *ibi;
+	struct i3c_device *i3cdev;
 	struct i3c_ibi_payload payload;
 
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		list_del(&gslot->node);
+		ibi = gslot->ibi;
+		i3cdev = gslot->i3cdev;
+	}
+
 	payload.data = slot->data;
 	payload.len = slot->len;
 
-	if (dev->dev)
-		dev->ibi->handler(dev->dev, &payload);
+	if (i3cdev)
+		ibi->handler(i3cdev, &payload);
+
+	/*
+	 * 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.
+	 * This can free the pool, so the slot must not be used afterwards.
+	 */
+	i3c_generic_ibi_recycle_slot(pool, slot);
 
-	i3c_dev_recycle_ibi_slot_controller(dev, slot);
-	if (atomic_dec_and_test(&dev->ibi->pending_ibis))
-		complete(&dev->ibi->all_ibis_handled);
+	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,
@@ -3720,13 +3782,7 @@ static void i3c_master_init_ibi_slot(struct i3c_dev_desc *dev,
 	INIT_WORK(&slot->work, i3c_master_handle_ibi);
 }
 
-/**
- * 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;
@@ -3747,6 +3803,42 @@ void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
 	kfree(pool->payload_buf);
 	kfree(pool);
 }
+
+/**
+ * i3c_generic_ibi_free_pool() - Free a generic IBI pool
+ * @pool: the IBI pool to free
+ *
+ * Stop handing out slots from a generic IBI pool and cancel IBI work that
+ * has not started yet. The pool is freed once all its slots are back: right
+ * away if none is in use, otherwise when the controller or a running IBI
+ * handler returns the last one.
+ */
+void i3c_generic_ibi_free_pool(struct i3c_generic_ibi_pool *pool)
+{
+	struct i3c_generic_ibi_slot *slot, *tmp;
+	struct i3c_device_ibi_info *ibi;
+	bool destroy;
+
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		pool->stopping = true;
+		list_for_each_entry_safe(slot, tmp, &pool->pending, node) {
+			/* A handler that has started returns its own slot. */
+			if (!cancel_work(&slot->base.work))
+				continue;
+
+			ibi = slot->ibi;
+			slot->ibi = NULL;
+			list_move_tail(&slot->node, &pool->free_slots);
+			pool->num_in_use--;
+			if (atomic_dec_and_test(&ibi->pending_ibis))
+				complete(&ibi->all_ibis_handled);
+		}
+		destroy = !pool->num_in_use;
+	}
+
+	if (destroy)
+		i3c_generic_ibi_destroy_pool(pool);
+}
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_free_pool);
 
 /**
@@ -3788,6 +3880,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)
@@ -3813,10 +3906,10 @@ EXPORT_SYMBOL_GPL(i3c_generic_ibi_alloc_pool);
  *
  * Invoke the current controller's IBI slot recycling callback.
  *
- * Context: Called from the generic IBI work handler in workqueue context.
- * No bus lock is taken here: the controller owns its IBI pool and is
- * responsible for synchronizing access to it. The generic pool
- * implementation uses its own spinlock.
+ * Context: The generic IBI work handler no longer calls this, it returns
+ * slots to their generic pool directly. No bus lock is taken here: the
+ * controller owns its IBI pool and is responsible for synchronizing access to
+ * it. The generic pool implementation uses its own spinlock.
  */
 void i3c_dev_recycle_ibi_slot_controller(struct i3c_dev_desc *dev,
 					 struct i3c_ibi_slot *slot)
@@ -3836,22 +3929,27 @@ EXPORT_SYMBOL_GPL(i3c_dev_recycle_ibi_slot_controller);
  * The slot should be returned to the pool using i3c_generic_ibi_recycle_slot()
  * when it's no longer needed.
  *
- * Return: a pointer to a free slot, or NULL if there's no free slot available.
+ * Return: a pointer to a free slot, or NULL if there's no free slot available
+ * or the pool is being freed.
  */
 struct i3c_ibi_slot *
 i3c_generic_ibi_get_free_slot(struct i3c_generic_ibi_pool *pool)
 {
 	struct i3c_generic_ibi_slot *slot;
-	unsigned long flags;
 
-	spin_lock_irqsave(&pool->lock, flags);
+	guard(spinlock_irqsave)(&pool->lock);
+	if (pool->stopping)
+		return NULL;
+
 	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);
+	if (!slot)
+		return NULL;
+
+	list_del(&slot->node);
+	pool->num_in_use++;
 
-	return slot ? &slot->base : NULL;
+	return &slot->base;
 }
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
 
@@ -3860,22 +3958,32 @@ EXPORT_SYMBOL_GPL(i3c_generic_ibi_get_free_slot);
  * @pool: the pool to return the IBI slot to
  * @s: IBI slot to recycle
  *
- * Add an IBI slot back to its generic IBI pool. Should be called from the
- * master driver struct_master_controller_ops->recycle_ibi() method.
+ * Add an IBI slot back to its generic IBI pool. The core calls this once the
+ * IBI handler has run. A controller driver calls it to return a slot it took
+ * with i3c_generic_ibi_get_free_slot() but did not queue. If
+ * i3c_generic_ibi_free_pool() has been called and this was the last slot in
+ * use, the pool is freed.
  */
 void i3c_generic_ibi_recycle_slot(struct i3c_generic_ibi_pool *pool,
 				  struct i3c_ibi_slot *s)
 {
 	struct i3c_generic_ibi_slot *slot;
-	unsigned long flags;
+	bool destroy;
 
 	if (!s)
 		return;
 
 	slot = container_of(s, struct i3c_generic_ibi_slot, base);
-	spin_lock_irqsave(&pool->lock, flags);
-	list_add_tail(&slot->node, &pool->free_slots);
-	spin_unlock_irqrestore(&pool->lock, flags);
+	scoped_guard(spinlock_irqsave, &pool->lock) {
+		slot->ibi = NULL;
+		list_add_tail(&slot->node, &pool->free_slots);
+		pool->num_in_use--;
+		destroy = pool->stopping && !pool->num_in_use;
+	}
+
+	/* The last slot is back after i3c_generic_ibi_free_pool(). */
+	if (destroy)
+		i3c_generic_ibi_destroy_pool(pool);
 }
 EXPORT_SYMBOL_GPL(i3c_generic_ibi_recycle_slot);
 
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
index b04fdadbbe69..f0b8fe89a3cc 100644
--- a/include/linux/i3c/master.h
+++ b/include/linux/i3c/master.h
@@ -111,8 +111,9 @@ struct i2c_dev_desc {
  * slot in its IBI slot pool, retrieve the IBI payload and queue the IBI using
  * i3c_master_queue_ibi().
  *
- * How IBI slots are allocated is left to the I3C master driver, though, for
- * simple kmalloc-based allocation, the generic IBI slot pool can be used.
+ * IBI slots must come from a generic IBI slot pool, see
+ * i3c_generic_ibi_alloc_pool(). Once the IBI handler has run, the core returns
+ * the slot to its pool.
  */
 struct i3c_ibi_slot {
 	struct work_struct work;
@@ -467,10 +468,9 @@ struct i3c_bus {
  *		 flag set and then deactivate the hardware IRQ on the
  *		 controller end.
  *		 This method is mandatory only if ->request_ibi is not NULL.
- * @recycle_ibi_slot: recycle an IBI slot. Called every time an IBI has been
- *		      processed by its handler. The IBI slot should be put back
- *		      in the IBI slot pool so that the controller can re-use it
- *		      for a future IBI
+ * @recycle_ibi_slot: recycle an IBI slot. The core no longer calls this: it
+ *		      returns IBI slots to their generic pool itself once the
+ *		      IBI handler has run.
  *		      This method is mandatory only if ->request_ibi is not
  *		      NULL.
  * @enable_hotjoin: enable hot join event detect.
-- 
2.56.1.915.g69d5ec7298-goog


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

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

* [PATCH v2 4/4] i3c: master: Preserve IBI workqueue across DAA
  2026-12-05  6:55 ` Sam Agazaryan
@ 2026-12-05  6:55   ` Sam Agazaryan
  -1 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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,
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@google.com>
---

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


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

* [PATCH v2 4/4] i3c: master: Preserve IBI workqueue across DAA
@ 2026-12-05  6:55   ` Sam Agazaryan
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	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,
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@google.com>
---

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


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

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

* Re: [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload
  2026-12-05  6:55   ` Sam Agazaryan
  (?)
@ 2026-12-05  7:06   ` sashiko-bot
  -1 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  7:06 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:
- [Low] i3c: dw: check payload length before taking IBI slot
--

commit 2966934f7f5a248b3700a8c4400e2a6033800193
Author: Sam Agazaryan <samagazaryan@google.com>

i3c: dw: Fix IBI slot leak on oversized payload

This commit recycles the allocated IBI slot in
dw_i3c_master_handle_ibi_sir() before draining the queue when the payload
length exceeds dev->ibi->max_payload_len.

> diff --git a/drivers/i3c/master/dw-i3c-master.c b/drivers/i3c/master/dw-i3c-master.c
> index 76875301b5ce2..9434d13830334 100644
> --- a/drivers/i3c/master/dw-i3c-master.c
> +++ b/drivers/i3c/master/dw-i3c-master.c
> @@ -1486,6 +1486,7 @@ static void dw_i3c_master_handle_ibi_sir(struct dw_i3c_master *master,
>  		dev_dbg_ratelimited(&master->base.dev,
>  				    "IBI payload len %d greater than max %d\n",
>  				    len, dev->ibi->max_payload_len);
> +		i3c_generic_ibi_recycle_slot(data->ibi_pool, slot);
>  		goto err_drain;
>  	}

[Severity: Low]
Would it be simpler to check dev->ibi->max_payload_len < len before calling
i3c_generic_ibi_get_free_slot()?

Neither len nor dev->ibi->max_payload_len depends on the slot.  Checking it
first avoids taking and immediately recycling a slot under pool->lock in IRQ
context, and logs the length error rather than "No IBI slots available" when
the pool is empty (as hci_dma_process_ibi() does).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205065552.2594113-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] 12+ messages in thread

* Re: [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing
  2026-12-05  6:55   ` Sam Agazaryan
  (?)
@ 2026-12-05  7:17   ` sashiko-bot
  -1 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  7:17 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:
- [Low] i3c: master: recycle_ibi_slot remains mandatory in
  i3c_master_check_ops()
--

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

i3c: master: Drain IBI slot pool before freeing

This commit tracks in-use generic IBI slots so i3c_generic_ibi_free_pool()
cancels unstarted IBI work and defers freeing the pool until the last slot
is returned.  i3c_master_handle_ibi() now recycles slots directly to their
pool instead of calling the controller's recycle_ibi_slot callback.

> diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
> index b04fdadbbe693..f0b8fe89a3cc8 100644
> --- a/include/linux/i3c/master.h
> +++ b/include/linux/i3c/master.h

[ ... ]

> @@ -467,10 +468,9 @@ struct i3c_bus {
>   *		 flag set and then deactivate the hardware IRQ on the
>   *		 controller end.
>   *		 This method is mandatory only if ->request_ibi is not NULL.
> - * @recycle_ibi_slot: recycle an IBI slot. Called every time an IBI has been
> - *		      processed by its handler. The IBI slot should be put back
> - *		      in the IBI slot pool so that the controller can re-use it
> - *		      for a future IBI
> + * @recycle_ibi_slot: recycle an IBI slot. The core no longer calls this: it
> + *		      returns IBI slots to their generic pool itself once the
> + *		      IBI handler has run.
>   *		      This method is mandatory only if ->request_ibi is not
>   *		      NULL.
>   * @enable_hotjoin: enable hot join event detect.

[Severity: Low]
Should recycle_ibi_slot still be mandatory when request_ibi is set?

i3c_master_check_ops() still returns -EINVAL if ops->request_ibi is set
without ops->recycle_ibi_slot, forcing new controller drivers to define a
dead callback.  i3c_dev_recycle_ibi_slot_controller() already checks for
a non-NULL callback, so this check and doc requirement can be dropped.

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

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

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

end of thread, other threads:[~2026-12-05  7:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:55 [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device Sam Agazaryan
2026-12-05  6:55 ` Sam Agazaryan
2026-12-05  6:55 ` [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload Sam Agazaryan
2026-12-05  6:55   ` Sam Agazaryan
2026-12-05  7:06   ` sashiko-bot
2026-12-05  6:55 ` [PATCH v2 2/4] i3c: master: Move generic IBI pool definitions Sam Agazaryan
2026-12-05  6:55   ` Sam Agazaryan
2026-12-05  6:55 ` [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing Sam Agazaryan
2026-12-05  6:55   ` Sam Agazaryan
2026-12-05  7:17   ` sashiko-bot
2026-12-05  6:55 ` [PATCH v2 4/4] i3c: master: Preserve IBI workqueue across DAA Sam Agazaryan
2026-12-05  6:55   ` Sam Agazaryan

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.