Skip to content

i3c: master: Fix IBI teardown when DAA re-adds a device

Sam Agazaryan linux-i3c lkml 2 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest4 patches 0 repliesNew
  2. v1 RFC2 patches 2 repliesSuperseded

Changes in v2 from the cover letter

- 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/[email protected]/

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4i3c: dw: Fix IBI slot leak on oversized payloaddrivers/i3c/master/dw-i3c-master.c+1 -00 replies
2/4i3c: master: Move generic IBI pool definitionsdrivers/i3c/master.c+14 -140 replies
3/4i3c: master: Drain IBI slot pool before freeingdrivers/i3c/master.c include/linux/i3c/master.h+149 -410 replies
4/4i3c: master: Preserve IBI workqueue across DAAdrivers/i3c/master.c include/linux/i3c/master.h+46 -110 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device

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/[email protected]/

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

Signature · 1 line
2.56.1.915.g69d5ec7298-goog