From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796453760; b=Htt7DeDZ0rfQ11HdSUPTdoFUSbmjspA+VvFrweu1EeDRpgIlfkR1Org50SABjmYeYV2l OxPss637/XsPsdXTgnSV+iATL/uLvWZFrs6Eths3GzMu15lu0B+/ZdREqZ4G2vviQs8Ml yMxs733Jc8PU/y97V76eXVJDJqqMWOoheBeDirg1OyQYPu8vKUNUEO5TSXYaKUt+AnsVR gBwYdI329WXD6wOr2p1jMiScsCQAaluwRfdSuTfcxOIqGsAMv78jifY6vpI50CHpIt/ee 4/XLqsaPF5eKhN4I2IiW2bfNSPPRXY63oGf7DokEhw97CdqYyMh8JzqdRa3/FqBqNsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796453760; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=jQN4VEgcQa/2QQXFDAtXMvByorbdfwXQcj8HWn/huvE=; b=5AKXoU1ge44EHe3udkl7b1FXN49mlrWx7CjP+Cu8KKawrQ5L6zU+RuodCdTnG1K9Oqky uJuh+DNS4S6IQDul0W3+wd977JdpdPLQZMq4stqzIzMiXXoYPUfGCVaWvBdOEqgff6OiN 7JCiXvG9GH9XjlyH2WTBC8Xurjyc2RDgwefn1dV4757pmx5iLKLzH8nGkUTHjT/kzcO52 Ej9s+bZRdz4tIR7LP2HFWwJ2LTgE7rKsUi9SunslMa09lw30D4y/ws+JN1iuUBguyeWtc +QXzhbQKiY6PhW2xtPj7caGsxBZiaDQkjRvF4bfG97D2HxscgkoPdvycRreD2j2ykSA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--samagazaryan.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=E22RkXI3; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D4B7606A33F for ; Sat, 5 Dec 2026 06:56:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D4B7606A33F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samagazaryan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=E22RkXI3 Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-383ec96a7deso3116149eec.1 for ; Fri, 04 Dec 2026 22:56:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796453759; x=1797058559; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jQN4VEgcQa/2QQXFDAtXMvByorbdfwXQcj8HWn/huvE=; b=E22RkXI3ZLsR+n5Zo7mFtUtrOHb8cH+og5azi9C11RVFqycCzvcOcGWnggkVbHQn65 OboFuWx7ttyEXGHOFXh5RVhB3BGZCtXkCrO3hyz4LGmLe9vXpOHeial9g2vv02PWDyut ppdHLG0h6IAGsOV86p98xr/0pj7n1n9uf7XYQQEh4n/jFcfAOBHxPbgLtk+Enm9OJuvY 9E5OWeTwbCQ5EXTZndHjyhXrCLuPNrIW4zbxJ7gljow881W7Z5MvaHEotkfyieABSnQh DMMg/d9ePtmdBaZ4nr2sSCYpkew/9eEqmKk6PyO5MV3p6IDHxOrre0ZrimzRCMZ/5end GUKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796453759; x=1797058559; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jQN4VEgcQa/2QQXFDAtXMvByorbdfwXQcj8HWn/huvE=; b=XplYH/0+qGrmYFNGTlnFy4YPq5wGG7sBEGI/oaeDSuc8XSVeH0CC8UbPWZzizIIG8H dm+B0fZ+y7QmeswLymoBDajHih4PIzelI5YO0QCMNIYvR5I8hLHvXz0t8EB1j8bUjqoS jVwWiSLuCu0wO/+PwTKMlDHKN1UIfsIMoI6fPhXk68iK2kJ7J2NryiuIvxV7GgNBqVSL FHvr70AjAFuCLdAlrxdedPgzGpq87oc6Lk6q9QgIXhqu+SuXmzF22K1HRKOltX6X8C1d iTFd+LFZUPya8KBPNK+VNkc6y5XZ3WkbPdi4Hjufvadrpcfosy1Or6HQYjYQtYe6w7p6 9fDQ== X-Forwarded-Encrypted: i=1; AL+hErGLH6YRd5MQlPbNN0/P3LkdDanBBMYahuA7c5hW8njLR3anXsQBX7H2A6NAh1M6plQvZ3O/mMAEeBIotyY=@vger.kernel.org X-Gm-Message-State: AB4q+uHQWltfFyxIZAeUa0q0lcv6PDJcKxzh/DA6IcjPI+otp6HMPxdQ lru6cY71LlOwT/tQHXp4Ot1y637+E4ciSJ9lx6EYNk84o5BCy2/EAONMD4/4Xw/lhBQhDZWgvxO uH34ra7GvDYTe0tAxbkSChzMGj1mPxw== X-Received: from dyah4-n1.prod.google.com ([2002:a05:693c:83c4:10b0:386:9be5:2d95]) (user=samagazaryan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:69d3:b0:382:f0d4:1e77 with SMTP id 5a478bee46e88-38752165875mr3660596eec.36.1796453759185; Fri, 04 Dec 2026 22:55:59 -0800 (PST) Date: Sat, 5 Dec 2026 06:55:51 +0000 In-Reply-To: <20261205065552.2594113-1-samagazaryan@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261205065552.2594113-1-samagazaryan@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261205065552.2594113-4-samagazaryan@google.com> Subject: [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing From: Sam Agazaryan To: Alexandre Belloni , linux-i3c@lists.infradead.org Cc: Frank Li , Adrian Hunter , Miquel Raynal , Jeremy Kerr , Joel Stanley , Zbigniew Lukwinski , Ravindra Yashvant Shinde , Bart Van Assche , linux-kernel@vger.kernel.org, Sam Agazaryan Content-Type: text/plain; charset="UTF-8" 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 --- 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