Linux IOMMU Development
 help / color / mirror / Atom feed
From: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>
To: Krzysztof Karas <krzysztof.karas@intel.com>,
	 intel-gfx@lists.freedesktop.org,
	dri-devel@lists.freedesktop.org,  iommu@lists.linux.dev
Cc: "Andi Shyti" <andi.shyti@linux.intel.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Jason Gunthorpe" <jgg@ziepe.ca>,
	"Michał Grzelak" <michal.grzelak@intel.com>,
	"Sebastian Brzezinka" <sebastian.brzezinka@intel.com>,
	"Krzysztof Niemiec" <krzysztof.niemiec@intel.com>
Subject: Re: [PATCH v3 3/5] drm/i915/gem: Pull out size validation into a separate function
Date: Wed, 15 Jul 2026 17:21:39 +0200	[thread overview]
Message-ID: <ceb6e88a73eb99700b0af3e19e3d5c4941341752.camel@linux.intel.com> (raw)
In-Reply-To: <20260713095812.1014365-4-krzysztof.karas@intel.com>

Hi Krzysztof,

On Mon, 2026-07-13 at 09:58 +0000, Krzysztof Karas wrote:
> shmem_sg_alloc_table is a very large and hard to read function,
> so reduce the number of operations it is responsible for by
> placing "size" validation in a new helper.
> 
> Signed-off-by: Krzysztof Karas <krzysztof.karas@intel.com>
> ---
> v3:
>  * Split refactoring and put it after the fix in shmem folio
>   counting suggested by Andi.
> 
>  drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 29 ++++++++++++++++-------
>  1 file changed, 20 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> index 0011d76f5b8c..4a61b012fb6f 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> @@ -62,6 +62,22 @@ void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
>  	sg_free_table(st);
>  }
>  
> +static int validate_size(size_t size, unsigned int page_count,
> +			 struct intel_memory_region *mr)
> +{
> +	if (overflows_type(size / PAGE_SIZE, page_count))
> +		return -E2BIG;
> +
> +	/*
> +	 * If there's no chance of allocating enough pages for the whole
> +	 * object, bail early.
> +	 */
> +	if (size > resource_size(&mr->region))
> +		return -ENOMEM;
> +
> +	return 0;
> +}
> +
>  int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
>  			 size_t size, struct intel_memory_region *mr,
>  			 struct address_space *mapping,
> @@ -77,16 +93,11 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
>  	unsigned long i;
>  	int ret;
>  
> -	if (overflows_type(size / PAGE_SIZE, page_count))
> -		return -E2BIG;
> -
>  	page_count = size / PAGE_SIZE;

Verifying if page_count can accommodate a result before assigning that
result to it looked more correctly to me.  Since overflows_type() doesn't
look at the variable's value, only its type, I think you could postpone 
page_count initialization and pass its pointer to your helper to preserve 
that more reasonable order of operations.

Thanks,
Janusz

> -	/*
> -	 * If there's no chance of allocating enough pages for the whole
> -	 * object, bail early.
> -	 */
> -	if (size > resource_size(&mr->region))
> -		return -ENOMEM;
> +
> +	ret = validate_size(size, page_count, mr);
> +	if (ret < 0)
> +		return ret;
>  
>  	if (sg_alloc_table(st, page_count, GFP_KERNEL | __GFP_NOWARN))
>  		return -ENOMEM;

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

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-13  9:58 [PATCH v3 0/5] drivers: Improve memory management for large object allocations when i915/shmem is used with iommu Krzysztof Karas
2026-07-13  9:58 ` [PATCH v3 1/5] drm/i915/gem: Count mapped pages in a folio Krzysztof Karas
2026-07-15 11:18   ` Janusz Krzysztofik
2026-07-20  8:08     ` Krzysztof Karas
2026-07-13  9:58 ` [PATCH v3 2/5] iommu/dma: Catch scatterlist length overflows Krzysztof Karas
2026-07-16 12:39   ` Andi Shyti
2026-07-16 13:48   ` Robin Murphy
2026-07-20  7:46     ` Krzysztof Karas
2026-07-13  9:58 ` [PATCH v3 3/5] drm/i915/gem: Pull out size validation into a separate function Krzysztof Karas
2026-07-15 15:21   ` Janusz Krzysztofik [this message]
2026-07-20  8:10     ` Krzysztof Karas
2026-07-13  9:58 ` [PATCH v3 4/5] drm/i915/gem: Read and shrink memory in " Krzysztof Karas
2026-07-15 15:31   ` Janusz Krzysztofik
2026-07-20  8:18     ` Krzysztof Karas
2026-07-20 10:15       ` Janusz Krzysztofik
2026-07-13  9:58 ` [PATCH v3 5/5] drm/i915/gem: Remove iterator and use while loop Krzysztof Karas
2026-07-15 17:04   ` Janusz Krzysztofik
2026-07-20  8:25     ` Krzysztof Karas
2026-07-20 10:19       ` Janusz Krzysztofik

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=ceb6e88a73eb99700b0af3e19e3d5c4941341752.camel@linux.intel.com \
    --to=janusz.krzysztofik@linux.intel.com \
    --cc=andi.shyti@linux.intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=krzysztof.karas@intel.com \
    --cc=krzysztof.niemiec@intel.com \
    --cc=michal.grzelak@intel.com \
    --cc=robin.murphy@arm.com \
    --cc=sebastian.brzezinka@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