From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796453757; b=GVIpj7J37hp7yGFX67puldtIC3pYkJtOS/U+0widcVs6W1lpyvrtIE4L0zns0pgXh71H /1HjbnnzfmcsdzR2ou6VZAxvVgAUXRsN11KBAZbpmUeXfyrxNoDX+IRUpLkMdz8BUJZSU Kx2YmwjnIX4b4Zq4C8bfqeFvBYSlj+MfLXtmXIEPw39EXSnDJ+MNpTqup13ZheN6Ak3gh GGBrz2rPOfS0NVNl9q/8Xt9QIXx+4dYZWt1trcUxl9QK4FN+LPkiaQYgBUx3t8ZkOObgY iyt2z930sUswhIUx8gzFbaoo1bsqBY7z77+bYlx2i+JMLXga9hyqVjmgXNOzuyvdFqA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796453757; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=ZCZXRjSiTfTgxXcY28RCzj3UHj15UQrSmNQyPmfFFAY=; b=cgHMkkPQMEEZVpnRqvBdx2KQ9iWNi9J4QBwCTjMyJSx5SoDplUXlyW2axB33Q1hweoyh hHWp6+z8KG0qdIFtWKmdMHDgU7uMXSuPGsSrpR8jELQfR30nQbOhWtvfBvhmgojzul3ko Anhc3scTYQ/4nZgGmErPZx8hlQAdqvBJ8nlHL/NPbrcsxXeanPMF9C9TacmhoLxvfBiRh hegZXunWm1n7AqqBqXr16+aPuc0IYQKGUiHbax654aVn/m3c/sjs76p5hXUd1PslgzrDS raQakD3h0KHDMIUDOqw4q72PHRvRekf6x7EDqC9PPRMuetlz4ZfDDC7du6/YPVILYdw== 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=c4h8nxFU; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (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 3F36D606A322 for ; Sat, 5 Dec 2026 06:55:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3F36D606A322 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=c4h8nxFU Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1b56c696405so5127062c88.1 for ; Fri, 04 Dec 2026 22:55:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796453756; x=1797058556; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ZCZXRjSiTfTgxXcY28RCzj3UHj15UQrSmNQyPmfFFAY=; b=c4h8nxFUR3u6iLT3oyh9p1ydtYQg8Zh2PC2UmNXoPZidkgfOEeH77LkPZNRHxbKaUw 5j6r6uwuoHVbTz1j/sw2DkyImL6fzMkDDUlRQpRg7vh33psmjDMDdofaT80C4FFujOAN ypl1NEt58HLay/a5t5sI9gwCLvYX/zcY/nIKaBzlolQKbpO4bgDOQu6KEd8lTtc82qjK wRUgSn6Ht1LCdsjTONx4IKelJFDGKDEbHN8lJp8Wtnwd9NQPmLrDAlkuk6Am3uGeJ7y8 hyV0+2+o83VbQ/KxJDCenQ2ZBQj8fBhi1OB6sk4pJYyGwsN+R7Z8ZAP/oF4PuuSyV8Q4 LpPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796453756; x=1797058556; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZCZXRjSiTfTgxXcY28RCzj3UHj15UQrSmNQyPmfFFAY=; b=m5yqvg4zKWj41+PQMzbAx5Hp6tNLAn1bcXH/yTuVrrMPldUv/EWtLGdaNVRSsTdXbt gwaOyDrAdAqih0/JJnc6gvdB9ytL8Ny+uDzGRvaR//URehipX63JzizJYA5S8cgsaiCY nP+egN1wNySZHXLU6s5vpSh5npWifn8exBSMaq3+aWv1yXL8ukM7/jsbQqJ0YlIe7LDJ Gce+Pw0J7EkLsaIIfvo7Ky3YE2CULYW/qG2CkvwHCSZzSVavfU4ndbKQG33WRcXuImOX JxmH394PRysY0FaDhTRLsUqUev5TjrcQ460+0MZl+nibB1HnnTRfHUj4GN6AsgSIQxO7 phmw== X-Forwarded-Encrypted: i=1; AL+hErGfyDfy3Lx0r9fEZK7y3GU8nkpJzaV5e/uG42pKZlpMC6xG4smAU8ipCzUVjFudjs6AePLj0dOuFPOkT1U=@vger.kernel.org X-Gm-Message-State: APuU4Z9A3f4y0GMu0wWvTT58uFxsh8TW9XxXXO7EoyqywPRpH/g4gYxq s7WANvKLVxjeIru70if1RjhQFhHuFSbaMMHtOD53hek8sMQpwG1gWmbQRuFN7GbQGxHOg6HboHx LeNzEuF/fMU2B2X66aXiGUwJ6DHB/4w== X-Received: from dlgp21-n1.prod.google.com ([2002:a05:7022:b155:10b0:1b2:6c20:7e86]) (user=samagazaryan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:1585:b0:1b5:3aca:8116 with SMTP id a92af1059eb24-1b5d7107d15mr4071964c88.33.1796453756052; Fri, 04 Dec 2026 22:55:56 -0800 (PST) Date: Sat, 5 Dec 2026 06:55:48 +0000 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 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261205065552.2594113-1-samagazaryan@google.com> Subject: [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device 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 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