Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jani Nikula <jani.nikula@linux.intel.com>
To: "Cavitt, Jonathan" <jonathan.cavitt@intel.com>,
	"intel-gfx@lists.freedesktop.org"
	<intel-gfx@lists.freedesktop.org>
Cc: "Gupta, Saurabhg" <saurabhg.gupta@intel.com>,
	"Zuo, Alex" <alex.zuo@intel.com>,
	"Zanoni, Paulo R" <paulo.r.zanoni@intel.com>,
	"ville.syrjala@linux.intel.com" <ville.syrjala@linux.intel.com>,
	"daniel.vetter@ffwll.ch" <daniel.vetter@ffwll.ch>,
	"Cavitt, Jonathan" <jonathan.cavitt@intel.com>
Subject: RE: [PATCH] drm/i915/display: Prevent u64 underflow in intel_fbc_stolen_end
Date: Wed, 07 Jan 2026 17:55:54 +0200	[thread overview]
Message-ID: <4f5fb36bc0dfef8e0bcd584226cf3c2e442f063e@intel.com> (raw)
In-Reply-To: <CH0PR11MB544499F13BF3FF457345EEB2E584A@CH0PR11MB5444.namprd11.prod.outlook.com>

On Wed, 07 Jan 2026, "Cavitt, Jonathan" <jonathan.cavitt@intel.com> wrote:
> -----Original Message-----
> From: Jani Nikula <jani.nikula@linux.intel.com> 
> Sent: Wednesday, January 7, 2026 3:39 AM
> To: Cavitt, Jonathan <jonathan.cavitt@intel.com>; intel-gfx@lists.freedesktop.org
> Cc: Gupta, Saurabhg <saurabhg.gupta@intel.com>; Zuo, Alex <alex.zuo@intel.com>; Cavitt, Jonathan <jonathan.cavitt@intel.com>; Zanoni, Paulo R <paulo.r.zanoni@intel.com>; ville.syrjala@linux.intel.com; daniel.vetter@ffwll.ch
> Subject: Re: [PATCH] drm/i915/display: Prevent u64 underflow in intel_fbc_stolen_end
>> 
>> On Fri, 19 Dec 2025, Jonathan Cavitt <jonathan.cavitt@intel.com> wrote:
>> > Static analysis reveals a potential integer underflow in
>> > intel_fbc_stolen_end.  This can apparently occur if
>> > intel_parent_stolen_area_size returns zero (or, theoretically, any value
>> > less than 2^23), as 2^23 is subtracted from the return value and stored
>> > in a u64.  While this doesn't appear to cause any issues due to the use
>> > of the min() function to clamp the return values from the
>> > intel_fbc_stolen_end function, it would be best practice to avoid
>> > undeflowing values like this on principle.  So, rework the function to
>> > prevent the underflow from occurring.  Note that the underflow at
>> > present would result in the value of intel_fbc_cfb_base_max being
>> > returned at the end of intel_fbc_stolen_end, so just return that if the
>> > value of intel_parent_stolen_area_size is too small.
>> >
>> > While we're here, create a macro for the 2^23 value and modify the
>> > execution path for readability.
>> >
>> > Fixes: a9da512b3ed7 ("drm/i915: avoid the last 8mb of stolen on BDW/SKL")
>> > Signed-off-by: Jonathan Cavitt <jonathan.cavitt@intel.com>
>> > Cc: Paulo Zanoni <paulo.r.zanoni@intel.com>
>> > Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
>> > Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
>> > ---
>> >  drivers/gpu/drm/i915/display/intel_fbc.c | 20 ++++++++++++++------
>> >  1 file changed, 14 insertions(+), 6 deletions(-)
>> >
>> > diff --git a/drivers/gpu/drm/i915/display/intel_fbc.c b/drivers/gpu/drm/i915/display/intel_fbc.c
>> > index fef2f35ff1e9..00c32df50933 100644
>> > --- a/drivers/gpu/drm/i915/display/intel_fbc.c
>> > +++ b/drivers/gpu/drm/i915/display/intel_fbc.c
>> > @@ -807,21 +807,29 @@ static u64 intel_fbc_cfb_base_max(struct intel_display *display)
>> >  		return BIT_ULL(32);
>> >  }
>> >  
>> > +#define STOLEN_RESERVE_MAX	SZ_8M
>> >  static u64 intel_fbc_stolen_end(struct intel_display *display)
>> >  {
>> > -	u64 end;
>> > +	u64 end = intel_fbc_cfb_base_max(display);
>> >  
>> >  	/* The FBC hardware for BDW/SKL doesn't have access to the stolen
>> >  	 * reserved range size, so it always assumes the maximum (8mb) is used.
>> >  	 * If we enable FBC using a CFB on that memory range we'll get FIFO
>> >  	 * underruns, even if that range is not reserved by the BIOS. */
>> >  	if (display->platform.broadwell ||
>> > -	    (DISPLAY_VER(display) == 9 && !display->platform.broxton))
>> > -		end = intel_parent_stolen_area_size(display) - 8 * 1024 * 1024;
>> > -	else
>> > -		end = U64_MAX;
>> > +	    (DISPLAY_VER(display) == 9 && !display->platform.broxton)) {
>> > +		u64 stolen_area_size = intel_parent_stolen_area_size(display);
>> > +
>> > +		/* If stolen_area_size is less than STOLEN_RESERVE_MAX,
>> > +		 * use intel_fbc_cfb_base_max instead. */
>> 
>> Please use the proper multi-line comment style.
>
> Should I also fix the comment about FBC hardware while I'm here?

I wouldn't mind.

>
>> 
>> > +		if (stolen_area_size < STOLEN_RESERVE_MAX)
>> > +			return end;
>> 
>> check_sub_overflow(), perhaps with a drm_WARN_ON(), would be the way to
>> go I think. You can get rid of the extra macro too.
>
> So, instead of STOLEN_RESERVE_MAX, just directly reference SZ_8M here?

Yeah, since using check_sub_overflow() means you don't have to reference
the value twice, so you don't need the macro for it.

> -Jonathan Cavitt
>
>> 
>> > +
>> > +		stolen_area_size -= STOLEN_RESERVE_MAX;
>> 
>> A blank line is preferred before return.
>> 
>> > +		return min(end, stolen_area_size);
>> > +	}
>> >  
>> > -	return min(end, intel_fbc_cfb_base_max(display));
>> > +	return end;
>> >  }
>> >  
>> >  static int intel_fbc_min_limit(const struct intel_plane_state *plane_state)
>> 
>> -- 
>> Jani Nikula, Intel
>> 

-- 
Jani Nikula, Intel

      reply	other threads:[~2026-01-07 15:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-19 21:03 [PATCH] drm/i915/display: Prevent u64 underflow in intel_fbc_stolen_end Jonathan Cavitt
2025-12-19 23:43 ` ✓ i915.CI.BAT: success for " Patchwork
2025-12-23  7:54 ` ✓ i915.CI.Full: " Patchwork
2026-01-07 11:39 ` [PATCH] " Jani Nikula
2026-01-07 15:09   ` Cavitt, Jonathan
2026-01-07 15:55     ` 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=4f5fb36bc0dfef8e0bcd584226cf3c2e442f063e@intel.com \
    --to=jani.nikula@linux.intel.com \
    --cc=alex.zuo@intel.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=jonathan.cavitt@intel.com \
    --cc=paulo.r.zanoni@intel.com \
    --cc=saurabhg.gupta@intel.com \
    --cc=ville.syrjala@linux.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