Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sam Agazaryan <samagazaryan@google.com>
To: Alexandre Belloni <alexandre.belloni@bootlin.com>,
	linux-i3c@lists.infradead.org
Cc: Frank Li <Frank.Li@nxp.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 Zbigniew Lukwinski <zbigniew.lukwinski@linux.intel.com>,
	 Ravindra Yashvant Shinde <ravindra.yashvant.shinde@nxp.com>,
	linux-kernel@vger.kernel.org,
	 Sam Agazaryan <samagazaryan@google.com>
Subject: [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing
Date: Wed,  2 Dec 2026 02:25:01 +0000	[thread overview]
Message-ID: <20261202022502.3661797-2-samagazaryan@google.com> (raw)
In-Reply-To: <20261202022502.3661797-1-samagazaryan@google.com>

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

  reply	other threads:[~2026-12-02  2:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-02  2:44   ` [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing sashiko-bot
2026-12-02  2:25 ` [PATCH RFC 2/2] i3c: master: Preserve IBI workqueue across DAA Sam Agazaryan
2026-12-02  2:48   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202022502.3661797-2-samagazaryan@google.com \
    --to=samagazaryan@google.com \
    --cc=Frank.Li@nxp.com \
    --cc=adrian.hunter@intel.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-i3c@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ravindra.yashvant.shinde@nxp.com \
    --cc=zbigniew.lukwinski@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox