From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FC23606A33D for ; Wed, 2 Dec 2026 02:25:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=kAi6+vS6; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QERBQvQq DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Unsubscribe-Post:List-Owner; bh=2yn7AAPQvPnKazExoKQfm83u8u91pGMdaRNzas3BPRE=; b=kAi6+vS6kpWaBjjsdnKIObllS/ eB7nFDVYZmuGY/3VnknD29OUa97MdvF71nMlT7H2BxpnKiNAjy34EwJPKz8HAL0TFWfW7TvLSG2Ir Oj0WUXedsoduoR6tpxz8zH8yN7DkqfW60rtb8XWnY7Wft4u3BErqzZm3ZEdAW+OkgrwiJ0jIh/tSk 7sSaEAjLybZFJeeXEVgrVj8IEOWivnouSoMTGuhG/MxvLZelcd1Kp2ikOxGt6AvzZXAn/ZURBlkYB DpADFFS3cUQOJVUFM4tf8BC5XUcYSSVqlg0Xas7rW4cuJkMl+O4yLCRUGYgpVyZNJqWPNXcNnPyE0 TTqEhijA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYa20-00000009xKH-41Zc; Wed, 02 Dec 2026 02:25:16 +0000 Received: from mail-oo1-xc45.google.com ([2607:f8b0:4864:20::c45]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYa1y-00000009xJ5-0Fsz for linux-i3c@lists.infradead.org; Wed, 02 Dec 2026 02:25:15 +0000 Received: by mail-oo1-xc45.google.com with SMTP id 006d021491bc7-71af90057faso12247214eaf.0 for ; Tue, 01 Dec 2026 18:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796178312; x=1796783112; darn=lists.infradead.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=8A7Db6MGURopp6uW82+YcvqiTLFl2RJRNLJ2iYP1Elg=; b=QERBQvQq+BkhBP8dMtkkQylsiE+UuZMolZuy5Shk7sUGZQLpDTv50LcVlRtr2oXb0k dpzAYdLsCTGA8AJsbCQ71kdxGW1/4GJ4Rxy0lavpoj6lVDiZSeb0EdrNzEcl+YkonmZq G2uLLy15yxmBMpt7VT7NipbkFnm7cX9Td+UFjiQtKNak5RfxzWx4mCJbB07QIb1UxcWf nM8UuDCuUi1j2PZes5tclUeS0ou6unDJ6t+nrRT1rctX737ljH/x7cbJzdNa7Y585ZnG ZTCP79YqQeIB9fmtcy1NtHBZRJNmKvxCtQxhZ8Vpb50KNNIRjFW2X2y/Dni+7LpWSr7v cW7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796178312; x=1796783112; 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=8A7Db6MGURopp6uW82+YcvqiTLFl2RJRNLJ2iYP1Elg=; b=gJZBCdEF37UjPlBMk2Ow6MhwXCEPIP+aDxoP8g44y1bpC+lk0WBV7nl5rNN9ofDDZj VgARlrCwijPGzrf0w9kr3dARfyaoVYDrJsdzubZL0ewchePUMt/2BdWxa+5q0XpDOxKD AlVzPDFPJgIIfOiDz3rZzmwdD+HBu7LpC12sP0dCEfQTz/Gtx4M3Aq8XJPIqpi2S0gAt 8HeM+7ZpMB49PvsKi4dieN4HZcHck7U2O6mdPwyj+wyuMi1ACza3UM9zmPuxOH9gN/Gd uWnFtJzvR+C/QnpX+gePVFnHVHMSzqPCMVeXkmU+thvvcZfAdJodPgBanVjysNkIA53s ao8w== X-Forwarded-Encrypted: i=1; AL+hErH+kO3x27BdhIO/yAycdyjBHUnqfFV118CHy1yLj6G7Mz5BwhgOr6mUpdjdrfsZADQyzQtLEC71wes=@lists.infradead.org X-Gm-Message-State: APuU4Z/DQ1XZ5PGhiXuSWUOej72ddEciUXdpzD+r1P9g6Lyz94MIorpw lpe/hMhBnHmYRleBUfPPBQPi5EwvZ9j0QMnEYxAZdc1CIsC+g6i0YLoNLNkQqbeW91s1ZwqENKM UYV/D2Qds0DKcHepQUdRecDY4g825iA== X-Received: from oak6-n1.prod.google.com ([2002:a05:6871:88c6:10b0:4a9:9f74:256f]) (user=samagazaryan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2088:b0:71a:fcbf:247a with SMTP id 006d021491bc7-71b327f03c8mr24312858eaf.66.1796178312294; Tue, 01 Dec 2026 18:25:12 -0800 (PST) Date: Wed, 2 Dec 2026 02:25:01 +0000 In-Reply-To: <20261202022502.3661797-1-samagazaryan@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261202022502.3661797-1-samagazaryan@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202022502.3661797-2-samagazaryan@google.com> Subject: [PATCH RFC 1/2] i3c: master: Drain IBI slot pool before freeing From: Sam Agazaryan To: Alexandre Belloni , linux-i3c@lists.infradead.org Cc: Frank Li , Adrian Hunter , Zbigniew Lukwinski , Ravindra Yashvant Shinde , linux-kernel@vger.kernel.org, Sam Agazaryan X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_182514_120630_FA843DF3 X-CRM114-Status: GOOD ( 21.60 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-i3c@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-i3c" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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