Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jani Nikula <jani.nikula@linux.intel.com>
To: "Bin Luo (University of Electronic Science and Technology of
	China)" <luob87709@gmail.com>,
	rodrigo.vivi@intel.com
Cc: intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] drm/i915/display: hold frontbuffer ref across dirtyfb fence callback
Date: Fri, 27 Nov 2026 16:17:21 +0200	[thread overview]
Message-ID: <8247d106ca535db291d2f85a3b8bea7956c6ccd8@intel.com> (raw)
In-Reply-To: <20261127133935.1461-1-luob87709@gmail.com>

On Fri, 27 Nov 2026, "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com> wrote:
> 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.

Please describe a real world scenario where this "if" can happen.

BR,
Jani.

>
> 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.
>
> Fixes: 1bb2af547a4b ("drm/i915: Handle dma fences in dirtyfb callback")
> Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
> ---
> 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(-)
>
> diff --git a/drivers/gpu/drm/i915/display/intel_fb.c b/drivers/gpu/drm/i915/display/intel_fb.c
> index 9d71f59..ee0665c 100644
> --- a/drivers/gpu/drm/i915/display/intel_fb.c
> +++ b/drivers/gpu/drm/i915/display/intel_fb.c
> @@ -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);

-- 
Jani Nikula, Intel

      parent reply	other threads:[~2026-11-27 14:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261127133935.1461-1-luob87709@gmail.com>
2026-11-27 13:55 ` [PATCH v2] drm/i915/display: hold frontbuffer ref across dirtyfb fence callback sashiko-bot
2026-11-27 14:17 ` Jani Nikula [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8247d106ca535db291d2f85a3b8bea7956c6ccd8@intel.com \
    --to=jani.nikula@linux.intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luob87709@gmail.com \
    --cc=rodrigo.vivi@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox