Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device
@ 2026-12-05  6:55 Sam Agazaryan
  2026-12-05  6:55 ` [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload Sam Agazaryan
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Sam Agazaryan @ 2026-12-05  6:55 UTC (permalink / raw)
  To: Alexandre Belloni, linux-i3c
  Cc: Frank Li, Adrian Hunter, Miquel Raynal, Jeremy Kerr, Joel Stanley,
	Zbigniew Lukwinski, Ravindra Yashvant Shinde, Bart Van Assche,
	linux-kernel, Sam Agazaryan

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


-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-05  7:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:55 [PATCH v2 0/4] i3c: master: Fix IBI teardown when DAA re-adds a device Sam Agazaryan
2026-12-05  6:55 ` [PATCH v2 1/4] i3c: dw: Fix IBI slot leak on oversized payload Sam Agazaryan
2026-12-05  7:06   ` sashiko-bot
2026-12-05  6:55 ` [PATCH v2 2/4] i3c: master: Move generic IBI pool definitions Sam Agazaryan
2026-12-05  6:55 ` [PATCH v2 3/4] i3c: master: Drain IBI slot pool before freeing Sam Agazaryan
2026-12-05  7:17   ` sashiko-bot
2026-12-05  6:55 ` [PATCH v2 4/4] i3c: master: Preserve IBI workqueue across DAA Sam Agazaryan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox