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 4/5] drm/i915/gem: Read and shrink memory in a separate function
Date: Wed, 15 Jul 2026 17:31:56 +0200	[thread overview]
Message-ID: <8979872eac4f9eabb499f074b66583013f6f524c.camel@linux.intel.com> (raw)
In-Reply-To: <20260713095812.1014365-5-krzysztof.karas@intel.com>

On Mon, 2026-07-13 at 09:58 +0000, Krzysztof Karas wrote:
> Continue unloading shmem_sg_alloc_table by placing reading
> folios and shrink call into a new helper.
> Make the loop a bit more reader-friendly by removing iteration
> over a structure and replacing it with a do-while loop.
> 
> 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.
>  * Use do-while loop suggested by Robin. 
> 
>  drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 102 ++++++++++++----------
>  1 file changed, 55 insertions(+), 47 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> index 4a61b012fb6f..7c8de8fe0a22 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> @@ -78,6 +78,55 @@ static int validate_size(size_t size, unsigned int page_count,
>  	return 0;
>  }
>  
> +static struct folio *shmem_shrink_get_folio(struct address_space *mapping,
> +					    unsigned long folio_index,
> +					    gfp_t gfp, unsigned int page_count,
> +					    struct drm_i915_private *i915)
> +{
> +	struct folio *folio = NULL;
> +	unsigned int retries = 2;
> +
> +	do {
> +		cond_resched();
> +		folio = shmem_read_folio_gfp(mapping, folio_index, gfp);
> +		if (IS_ERR(folio)) {

Going again through then unused shrinking and modification of gfp doesn't 
make sense, I believe.  Could be avoided based on retries value as an 
additional condition.

Thanks,
Janusz


> +			i915_gem_shrink(NULL, i915, 2 * page_count, NULL,
> +					I915_SHRINK_BOUND | I915_SHRINK_UNBOUND);
> +
> +			/*
> +			 * We've tried hard to allocate the memory by reaping
> +			 * our own buffer, now let the real VM do its job and
> +			 * go down in flames if truly OOM.
> +			 *
> +			 * However, since graphics tend to be disposable,
> +			 * defer the oom here by reporting the ENOMEM back
> +			 * to userspace.
> +			 *
> +			 * Reclaim and warn, but no oom.
> +			 */
> +			gfp = mapping_gfp_mask(mapping);
> +
> +			/*
> +			 * Our bo are always dirty and so we require
> +			 * kswapd to reclaim our pages (direct reclaim
> +			 * does not effectively begin pageout of our
> +			 * buffers on its own). However, direct reclaim
> +			 * only waits for kswapd when under allocation
> +			 * congestion. So as a result __GFP_RECLAIM is
> +			 * unreliable and fails to actually reclaim our
> +			 * dirty pages -- unless you try over and over
> +			 * again with !__GFP_NORETRY. However, we still
> +			 * want to fail this allocation rather than
> +			 * trigger the out-of-memory killer and for
> +			 * this we want __GFP_RETRY_MAYFAIL.
> +			 */
> +			gfp |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
> +		}
> +	} while (IS_ERR(folio) && --retries);
> +
> +	return folio;
> +}
> +
>  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,
> @@ -117,57 +166,16 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
>  	for (i = 0; i < page_count; i++) {
>  		unsigned long folio_page_index = 0;
>  		unsigned long nr_pages;
> -		const unsigned int shrink[] = {
> -			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
> -			0,
> -		}, *s = shrink;
>  		gfp_t gfp = noreclaim;
>  
>  		/* Grab the next folio if we exhausted the current one. */
>  		if (!i || i > folio_end) {
> -			do {
> -				cond_resched();
> -				folio = shmem_read_folio_gfp(mapping, i, gfp);
> -				if (!IS_ERR(folio))
> -					break;
> -
> -				if (!*s) {
> -					ret = PTR_ERR(folio);
> -					goto err_sg;
> -				}
> -
> -				i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
> -
> -				/*
> -				* We've tried hard to allocate the memory by reaping
> -				* our own buffer, now let the real VM do its job and
> -				* go down in flames if truly OOM.
> -				*
> -				* However, since graphics tend to be disposable,
> -				* defer the oom here by reporting the ENOMEM back
> -				* to userspace.
> -				*/
> -				if (!*s) {
> -					/* reclaim and warn, but no oom */
> -					gfp = mapping_gfp_mask(mapping);
> -
> -					/*
> -					 * Our bo are always dirty and so we require
> -					 * kswapd to reclaim our pages (direct reclaim
> -					 * does not effectively begin pageout of our
> -					 * buffers on its own). However, direct reclaim
> -					 * only waits for kswapd when under allocation
> -					 * congestion. So as a result __GFP_RECLAIM is
> -					 * unreliable and fails to actually reclaim our
> -					 * dirty pages -- unless you try over and over
> -					 * again with !__GFP_NORETRY. However, we still
> -					 * want to fail this allocation rather than
> -					 * trigger the out-of-memory killer and for
> -					 * this we want __GFP_RETRY_MAYFAIL.
> -					 */
> -					gfp |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
> -				}
> -			} while (1);
> +			folio = shmem_shrink_get_folio(mapping, i, gfp,
> +						       page_count, i915);
> +			if (IS_ERR(folio)) {
> +				ret = PTR_ERR(folio);
> +				goto err_sg;
> +			}
>  
>  			folio_start = folio_pgoff(folio);
>  			folio_end = folio_start + folio_nr_pages(folio) - 1;

  reply	other threads:[~2026-07-15 15:32 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
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 [this message]
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=8979872eac4f9eabb499f074b66583013f6f524c.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