From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796178314; b=Pyr8gJ5KNzEjVkDMEyGQOp46x3tYsqmWhu1wngWmFKB2euO5CIMCiFZvtaJPC811KSFs KiMWF/LivauLpcFWci91OjfX6ck4IVmxDiZV7r3Vr3edyEbi9gMtG9ei77o5n4P5bhtbH LJGYnHgQc0wi83FZwPzkGrh6X+CWiE1KXmdqsiB35CFsPqRDa4DQA/7lAgb5tchRlEidd zbVO/p4HmGEFYitZsV47OQ4a7x9o17O2iU6yguY55IxpDGWxw1OD820NOFgwvt91GnpQV fZu0ATfgOH9Ho6BQl2189094/cyOojvDdjaSXWf4fBIFtveh48srIReb9C/9Yw6uEIg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796178314; 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=8A7Db6MGURopp6uW82+YcvqiTLFl2RJRNLJ2iYP1Elg=; b=ccL/V/TwvpD/XdQadslGtEnMt8DJiwFRFMlbLCIYxtta/IKSmFRm7po7gLWtwh7DphPb ZE74DyLTohOjYYuF7sqk3zkhQV4mSwaYGyzr9FIdNNa4UbLtC/0q0vk5isdAb6HQ6mfm7 XRuwTCLz4WIJNjUqioINGj7vPw7zy0cWlhtLOFwrr/coOs9wWWjtYlzxFmeVbq0+1WXQv YvcJY2hg7NnNAgoPdgIBKKJDvpj+sFJcDeXITPRUKlEIwGvrhyS58phmnE6F1VtVhHpF1 NLmwFSe3eUFx9JPJ3ZhYt3YqdBzp9u8EMD+gZuGBJhLyXdfj3qIL3punA/f+YvDWpnQ== 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=Mwm9KPtM; arc=none smtp.remote-ip=209.85.210.71 Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) (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 9AFFA606A337 for ; Wed, 2 Dec 2026 02:25:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9AFFA606A337 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=Mwm9KPtM Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-862bdb93612so14984576a34.1 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=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=8A7Db6MGURopp6uW82+YcvqiTLFl2RJRNLJ2iYP1Elg=; b=Mwm9KPtMxyTHiCFkGgBIEWnjqtShWk/4njgAF1sYB/fqpc1Neveeor87qwly3AiRXo 9S6/Nj4KhUhC2znlvXS5Mv9pyfGXQK250YnHdpMSgJ3Jz2pPKGa9P8eKlclVRbmAiOz1 Ligrfur4zhz4RdIRYgKtK7Zzjy+prmTVKn7L2sMJC1+kY7qLDLo8H230+mDzaqgWtHG/ 8sbwjgC8v9FHnzYX+aNDcPUwSz6xtGoYSHTDRFiKCvwl0HF3MBpMUE2hfydehF9h7nDF luGPjxLZUPNC4Y+HHE2/SnE3EY7l8sFGaZBngAgzPFo4EhC7eEdT7sOFcHbzMC97lLNf bQZg== 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=0N1cnhnW3WMLaXnkfH6s7osxW/TrRWI5UF2YLGzLLagbGOo1WVjFcnZPFmfAw6Pu+u woFASOK1Hc9XD31i2XxBU0AZ8JMz7fdC4QrZS7k8InyXz8EvNww+lXIFJveKa+MN/8gy 7OC4LvJ1viHEgBUvEC1G9vyoL6mAHAoRBhJRio5upaMvtRU0/zdlobnymxsHo8g+B0zc vdQO+TUemH9ecIx7UGh7WRfqJNjsaZ7t7U6ExGEIUcFJQaa9az3G6VLO9Ar4lwnaCPC8 bM3Oi9vS83nhHFGm8Y1/2RnpyMNN76s01ad31GMXDsGx2k8twcclzLPWTdb/gxvqRCrF V0YQ== X-Forwarded-Encrypted: i=1; AL+hErGN+9tXyVF5c7SBnRkNtHDGE6EveP+jP2zVT75UcAliAMeuRhDOQZhVe3wXGTHZJEVg43wozuj/SYGD/JU=@vger.kernel.org X-Gm-Message-State: APuU4Z/FRGsj28Oe/apKRahZlz+XvzLU6xHijMnMbqRlWLBXbQZiUk2B DgFQI9TcCnNLwyJRqW5YVVL4LqRV2Z2ti36n8g4OpHKQQtdu0cF57fMkLMgGmMfnKqd3QX7fDne l7/V1BjRS5zD0UY6CXO0dyj6aup/AjA== 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-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: <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 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 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