VC4 implements async page flips with a custom code path that waits for
the BO fence before completing the flip. On VC4_GEN_4 (RPi 0-3), the
completion also decrements the usecnt of the old FB's BO. This series
fixes three bugs in that path.

Since commit e8941ac97f28 ("drm/vc4: Remove BOs seqnos"), the completion
runs from a dma_fence callback. VC4 signals that fence from the render
job IRQ handler. As vc4_bo_dec_usecnt() takes a mutex, running an SDL
application with SDL_VIDEODRIVER=kmsdrm on the RPi 3 triggers:

  [ 2511.636319] BUG: sleeping function called from invalid context at 
kernel/locking/mutex.c:623
  [ 2511.636667] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 0, 
name: swapper/0
  [ 2511.636681] preempt_count: 1000002, expected: 0
  [ 2511.636689] RCU nest depth: 0, expected: 0
  [ 2511.636698] locks held by swapper/0/0: 1, on CPU#0:
  [ 2511.636711]  #0: ffff000037183198 (&fence->inline_lock){-...}-{3:3}, at: 
dma_fence_signal+0x88/0xec
  <snip>
  [ 2511.636892] Hardware name: Raspberry Pi 3 Model B Plus Rev 1.3 (DT)
  [ 2511.636899] Call trace:
  <snip>
  [ 2511.636991]  mutex_lock_nested+0x24/0x30
  [ 2511.637002]  vc4_bo_dec_usecnt+0x40/0x124 [vc4]
  [ 2511.637070]  vc4_async_page_flip_complete_with_cleanup+0xa0/0xc4 [vc4]
  [ 2511.637102]  dma_fence_signal_timestamp_locked+0x118/0x290
  [ 2511.637116]  dma_fence_signal+0x9c/0xec
  [ 2511.637126]  vc4_irq_render_job_done+0x24/0x114 [vc4]
  [ 2511.637158]  vc4_irq+0x16c/0x274 [vc4]

While looking into this issue, I also noticed that, since commit
4d12c36fb73b ("drm/vc4: crtc: Move the BO handling out of common page-flip
callback"), the old FB is released before decrementing its BO usecnt. If
userspace removes the old FB and closes its handle while the flip is
pending, vc4_bo_dec_usecnt() runs on a freed or recycled BO.

Patch 1 fixes the use-after-free, patch 2 moves the completion to a worker
and patch 3 returns fence lookup errors to userspace instead of leaving
the flip pending forever.

In v1, Sashiko reported that a flip waiting on its fence can complete
after, or concurrently with, a later flip that has no fence. This has been
possible since commit 82faa3276012 ("drm/vc4: kms: Remove async modeset
semaphore") and isn't changed by this series. The best way to fix it
would be moving to the generic async helpers, which is on my TODO list.
Until then, this series keeps the current behavior and only fixes the bugs
above.

Best regards,
- Maíra

---
v1 -> v2: 
https://lore.kernel.org/r/[email protected]

- [1/3] Add the refcount underflow warning to the commit message.
- [2/3] Allocate the workqueue after initializing the CRTC, so it's
  released before the CRTC is cleaned up and named after the CRTC (Sashiko).
- [3/3] New patch.

---
Maíra Canal (3):
      drm/vc4: Drop BO usecnt before releasing the old FB
      drm/vc4: Complete async page flips from an ordered workqueue
      drm/vc4: Look up the async page flip fence before committing the flip

 drivers/gpu/drm/vc4/vc4_crtc.c | 136 +++++++++++++++++++----------------------
 drivers/gpu/drm/vc4/vc4_drv.h  |   6 ++
 2 files changed, 70 insertions(+), 72 deletions(-)
---
base-commit: d48ea86919434695da12d280dccab72b094397e1
change-id: 20261111-vc4-async-flip-fixes-1b1ab17dc795

Reply via email to