drm/i915/display: hold frontbuffer ref across dirtyfb fence callback
Subsystems (3)
PATCH v2 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 embed a work_struct in frontbuffer_fence_cb so intel_user_framebuffer_fence_wake() only schedules the work item and drops the fence reference in atomic dma_fence signaling context. Perform intel_frontbuffer_flush(), release the frontbuffer reference with intel_parent_frontbuffer_put(), and free cb from process context in intel_user_framebuffer_fence_work(). This avoids calling intel_parent_frontbuffer_put() in atomic context, where dropping the final reference can sleep on xe or deadlock on non-irqsafe locks in i915.
Found by static code audit.
---
Changes in v2:
- Embed struct work_struct in frontbuffer_fence_cb and move intel_frontbuffer_flush(), intel_parent_frontbuffer_put(), and kfree(cb) into intel_user_framebuffer_fence_work() so intel_parent_frontbuffer_put() is never invoked from atomic dma_fence signaling context (fixing the sleeping-in-atomic/deadlock issue flagged on v1).
Testing note:
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 | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/display/intel_fb.c +16 -2
@@ -5,6 +5,7 @@#include <linux/dma-fence.h> #include <linux/dma-resv.h> +#include <linux/workqueue.h> #include <drm/drm_blend.h> #include <drm/drm_gem.h>
@@ -2141,16 +2142,26 @@ static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,struct frontbuffer_fence_cb {struct dma_fence_cb base; +struct work_struct work;struct intel_frontbuffer *front; }; +static void intel_user_framebuffer_fence_work (struct work_struct *work) +{ +struct frontbuffer_fence_cb *cb =container_of (work,typeof (*cb), work); +struct intel_frontbuffer *front = cb->front; + +intel_frontbuffer_flush (front,ORIGIN_DIRTYFB ); +intel_parent_frontbuffer_put (front->display, front); +kfree (cb); +} +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); -intel_frontbuffer_queue_flush (cb->front); -kfree (cb); +schedule_work (&cb->work);dma_fence_put (dma); }
@@ -2160,6 +2171,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 +2197,8 @@ static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb, } cb->front = front; +INIT_WORK (&cb->work, intel_user_framebuffer_fence_work); +intel_parent_frontbuffer_ref (display, front);intel_frontbuffer_invalidate (front,ORIGIN_DIRTYFB );
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/i915/display: dead intel_frontbuffer_queue_flush() and flush_work --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/i915/display: dead intel_frontbuffer_queue_flush() and flush_work
--
commit ef90877ebdeba58596971006e74cdcc49524595d
Author: [email protected] <[email protected]>
drm/i915/display: hold frontbuffer ref across dirtyfb fence callback
This commit holds a frontbuffer reference across the dirtyfb dma_fence callback and defers the frontbuffer flush and reference release to a work item embedded in frontbuffer_fence_cb.
Quoted 4 lines
drivers/gpu/drm/i915/display/intel_fb.c +0
-0
[ ... ]
Quoted 18 lines
drivers/gpu/drm/i915/display/intel_fb.c +11
-0@@ -2177,16 +2178,26 @@ static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,struct frontbuffer_fence_cb {struct dma_fence_cb base; +struct work_struct work;struct intel_frontbuffer *front; }; +static void intel_user_framebuffer_fence_work (struct work_struct *work) +{ +struct frontbuffer_fence_cb *cb =container_of (work,typeof (*cb), work); +struct intel_frontbuffer *front = cb->front; + +intel_frontbuffer_flush (front,ORIGIN_DIRTYFB ); +intel_parent_frontbuffer_put (front->display, front); +kfree (cb); +} +
drivers/gpu/drm/i915/display/intel_fb.c static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb, · line 2202
Quoted 10 lines
drivers/gpu/drm/i915/display/intel_fb.c +1
-2static 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); -intel_frontbuffer_queue_flush (cb->front); -kfree (cb); +schedule_work (&cb->work);dma_fence_put (dma); }
[Severity: Low]
Does replacing intel_frontbuffer_queue_flush() here leave intel_frontbuffer_queue_flush(), intel_frontbuffer_flush_work(), and struct intel_frontbuffer's flush_work member unused?
intel_user_framebuffer_fence_wake() was the only caller of intel_frontbuffer_queue_flush() in the tree, so those helpers in intel_frontbuffer.c and flush_work in struct intel_frontbuffer are now dead code (and intel_frontbuffer_queue_flush() still contains an atomic intel_parent_frontbuffer_put() fallback if schedule_work() returns false).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jani Nikula
maintainerQuoted 6 lines · On Fri, 27 Nov 2026, "Bin Luo (University of Electronic Science and Technology…
intel_user_framebuffer_dirty()stores the rawintel_frontbufferpointer infrontbuffer_fence_cbwithout taking a frontbuffer reference. If the framebuffer is destroyed and its owner frontbuffer reference is dropped before the reservationdma_fencesignals,intel_user_framebuffer_fence_wake()passes a freedintel_frontbufferpointer tointel_frontbuffer_queue_flush(), causing a use-after-free.
Please describe a real world scenario where this "if" can happen.
BR,
Jani.
Quoted 90 lines
Take a frontbuffer reference via
intel_parent_frontbuffer_ref()before registering the fence callback, and embed awork_structinfrontbuffer_fence_cbsointel_user_framebuffer_fence_wake()only schedules the work item and drops the fence reference in atomicdma_fencesignaling context. Performintel_frontbuffer_flush(), release the frontbuffer reference withintel_parent_frontbuffer_put(), and free cb from process context inintel_user_framebuffer_fence_work(). This avoids callingintel_parent_frontbuffer_put()in atomic context, where dropping the final reference can sleep on xe or deadlock on non-irqsafe locks in i915.Found by static code audit.
Fixes 1bb2af547a4b ("drm/i915: Handle dma fences in dirtyfb callback")---
Changes in v2:
- Embed structwork_structinfrontbuffer_fence_cband moveintel_frontbuffer_flush(),intel_parent_frontbuffer_put(), and kfree(cb) intointel_user_framebuffer_fence_work()sointel_parent_frontbuffer_put()is never invoked from atomicdma_fencesignaling context (fixing the sleeping-in-atomic/deadlock issue flagged on v1).Testing note:
Compile-tested onx86_64with 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 | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)drivers/gpu/drm/i915/display/intel_fb.c +16
-2@@ -5,6 +5,7 @@#include <linux/dma-fence.h> #include <linux/dma-resv.h> +#include <linux/workqueue.h> #include <drm/drm_blend.h> #include <drm/drm_gem.h> @@ -2141,16 +2142,26 @@ static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,struct frontbuffer_fence_cb {struct dma_fence_cb base; +struct work_struct work;struct intel_frontbuffer *front; }; +static void intel_user_framebuffer_fence_work (struct work_struct *work) +{ +struct frontbuffer_fence_cb *cb =container_of (work,typeof (*cb), work); +struct intel_frontbuffer *front = cb->front; + +intel_frontbuffer_flush (front,ORIGIN_DIRTYFB ); +intel_parent_frontbuffer_put (front->display, front); +kfree (cb); +} +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); -intel_frontbuffer_queue_flush (cb->front); -kfree (cb); +schedule_work (&cb->work);dma_fence_put (dma); }@@ -2160,6 +2171,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 +2197,8 @@ static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb, } cb->front = front; +INIT_WORK (&cb->work, intel_user_framebuffer_fence_work); +intel_parent_frontbuffer_ref (display, front);intel_frontbuffer_invalidate (front,ORIGIN_DIRTYFB );
Signature · 1 line
Jani Nikula, Intel
Hi Jani,
Quoted 9 lines · Jani Nikula
Quoted 6 lines · On Fri, 27 Nov 2026, "Bin Luo (University of Electronic Science and Technology…
intel_user_framebuffer_dirty()stores the rawintel_frontbufferpointer infrontbuffer_fence_cbwithout taking a frontbuffer reference. If the framebuffer is destroyed and its owner frontbuffer reference is dropped before the reservationdma_fencesignals,intel_user_framebuffer_fence_wake()passes a freedintel_frontbufferpointer tointel_frontbuffer_queue_flush(), causing a use-after-free.Please describe a real world scenario where this "if" can happen.
1. Framebuffer fb1 is currently scanned out on a plane (so atomic_read(&front->bits) != 0), and its BO has an unsignaled write dma_fence in obj->resv:
- On xe, any normal in-flight GPU render job to fb1's BO suffices, because xe_frontbuffer has no i915_active front->write tracker holding a reference to front.
- On i915 (as well as xe), an imported DMA-BUF / PRIME fence from another GPU or a sync_file imported via DMA_BUF_IOCTL_IMPORT_SYNC_FILE likewise does not take a frontbuffer reference via front->write.
2. Userspace issues DRM_IOCTL_MODE_DIRTYFB on fb1:
- intel_user_framebuffer_dirty() fetches the unsignaled singleton
fence from obj->resv, attaches intel_user_framebuffer_fence_wake()
with the raw cb->front pointer, and returns 0. - drm_mode_dirtyfb_ioctl() immediately drops its temporary fb1 reference via drm_framebuffer_put(fb1).
3. Before that fence signals (e.g., a render job taking >1 vblank or a slow/stalled external DMA-BUF fence), userspace flips the plane to another framebuffer fb2 (or disables the CRTC / closes the DRM fd) and removes fb1 via DRM_IOCTL_MODE_RMFB (or drm_fb_release()).
- The atomic commit only waits on fb2's fence (if any) and the
vblank, not on the old fb1's obj->resv fences.
4. Once vblank completes, intel_cleanup_plane_fb() drops the plane's reference to fb1, fb1's refcount hits zero, and intel_user_framebuffer_destroy() calls intel_parent_frontbuffer_put(display, intel_fb->frontbuffer).
- Because nothing else holds a reference to front (on xe,
intel_fb->frontbuffer is the sole reference until
intel_frontbuffer_queue_flush() runs; on i915, foreign DMA-BUF
fences do not hold front->write), frontbuffer_release() frees
front (kfree() on xe, kfree_rcu() on i915) while cb->base is still
attached to the unsignaled fence.5. When the fence later signals (normal completion or GPU reset), intel_user_framebuffer_fence_wake() passes the freed cb->front to intel_frontbuffer_queue_flush().
If you agree this scenario is worth fixing, I can also send a v3 that includes this scenario in the commit message and removes the now-unused intel_frontbuffer_queue_flush() / intel_frontbuffer_flush_work() / flush_work dead code noted by Sashiko.
Best regards,
Bin Luo