Skip to contentSkip to conversation

drm/i915/display: hold frontbuffer ref across dirtyfb fence callback

PATCH New Bin Luo (University of Electronic Science and Technology of China) 2 participants 2 messages Nov 26

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915/display: hold frontbuffer ref across dirtyfb fence callback

intel_user_framebuffer_dirty() stores the raw intel_frontbuffer pointer in frontbuffer_fence_cb without taking a frontbuffer reference. If the framebuffer is destroyed and its owner frontbuffer reference is dropped before the reservation dma_fence signals, intel_user_framebuffer_fence_wake() passes a freed intel_frontbuffer pointer to intel_frontbuffer_queue_flush(), causing a use-after-free.

Take a frontbuffer reference via intel_parent_frontbuffer_ref() before registering the fence callback, and release it via intel_parent_frontbuffer_put() in intel_user_framebuffer_fence_wake() after queuing the flush.

Fixes 1bb2af547a4b ("drm/i915: Handle dma fences in dirtyfb callback")

---
Found by code inspection. Compile-tested on x86_64 with W=1 and KASAN for both i915 (drivers/gpu/drm/i915/display/intel_fb.o) and xe (drivers/gpu/drm/xe/i915-display/intel_fb.o); not runtime-tested on Intel display hardware.

 drivers/gpu/drm/i915/display/intel_fb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/display/intel_fb.c +5 -1
index 9d71f59..3bbbedc 100644
@@ -2148,8 +2148,10 @@ static void intel_user_framebuffer_fence_wake(struct dma_fence *dma,
 					      struct dma_fence_cb *data)
 {
 	struct frontbuffer_fence_cb *cb = container_of(data, typeof(*cb), base);
+	struct intel_frontbuffer *front = cb->front;
 
-	intel_frontbuffer_queue_flush(cb->front);
+	intel_frontbuffer_queue_flush(front);
+	intel_parent_frontbuffer_put(front->display, front);
 	kfree(cb);
 	dma_fence_put(dma);
 }
@@ -2160,6 +2162,7 @@ static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
 					struct drm_clip_rect *clips,
 					unsigned int num_clips)
 {
+	struct intel_display *display = to_intel_display(fb->dev);
 	struct drm_gem_object *obj = intel_fb_bo(fb);
 	struct intel_frontbuffer *front = to_intel_frontbuffer(fb);
 	struct dma_fence *fence;
@@ -2185,6 +2188,7 @@ static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
 	}
 
 	cb->front = front;
+	intel_parent_frontbuffer_ref(display, front);
 
 	intel_frontbuffer_invalidate(front, ORIGIN_DIRTYFB);
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/i915: atomic frontbuffer put in intel_user_framebuffer_fence_wake() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/i915: atomic frontbuffer put in intel_user_framebuffer_fence_wake()
--

commit 6668699993fc94a934753276eb08e4a83ae0ca89
Author: [email protected] <[email protected]>

drm/i915/display: hold frontbuffer ref across dirtyfb fence callback

This commit takes a frontbuffer reference before registering the dirtyfb dma_fence callback and drops it in intel_user_framebuffer_fence_wake() after queuing the flush.

Quoted 6 lines
drivers/gpu/drm/i915/display/intel_fb.c +0 -0
index 2c63aeefaea88..a0af4166c1d33 100644
@@ -2184,8 +2184,10 @@ static void intel_user_framebuffer_fence_wake(struct dma_fence *dma,
 					      struct dma_fence_cb *data)

drivers/gpu/drm/i915/display/intel_fb.c static void intel_user_framebuffer_fence_wake(struct dma_fence *dma, · line 2193

Quoted 10 lines
drivers/gpu/drm/i915/display/intel_fb.c +3 -1
 {
 	struct frontbuffer_fence_cb *cb = container_of(data, typeof(*cb), base);
+	struct intel_frontbuffer *front = cb->front;
 
-	intel_frontbuffer_queue_flush(cb->front);
+	intel_frontbuffer_queue_flush(front);
+	intel_parent_frontbuffer_put(front->display, front);
 	kfree(cb);
 	dma_fence_put(dma);
 }

[Severity: High]
Can intel_parent_frontbuffer_put() here drop the final reference to front from atomic dma_fence signaling context?

intel_user_framebuffer_fence_wake() runs under fence->lock with interrupts disabled. If the framebuffer was destroyed before the fence signaled and front->flush_work finishes on another CPU first, intel_parent_frontbuffer_put() drops the last reference:

CPU0 (<hardirq>) CPU1 (kworker)
intel_user_framebuffer_fence_wake()
intel_frontbuffer_queue_flush()

    intel_parent_frontbuffer_ref()
    schedule_work(&front->flush_work)
                                  intel_frontbuffer_flush_work()
                                    ...
                                    intel_parent_frontbuffer_put()

intel_parent_frontbuffer_put()
frontbuffer_release() <-- final put in atomic context

On Xe, frontbuffer_release() calls drm_gem_object_put(front->obj) -> xe_gem_object_free() -> ttm_bo_fini() -> ttm_bo_release() -> xe_ttm_bo_destroy(), which acquires sleeping mutexes such as xe->mem_access.vram_userfault.lock in atomic context. On i915, i915_gem_object_frontbuffer_put() and frontbuffer_release() -> i915_ggtt_clear_scanout() acquire i915->frontbuffer_lock and obj->vma.lock with plain spin_lock(), which can deadlock if the fence signals from an interrupt that preempted a holder of either lock on the same CPU.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1