From: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>
To: Krzysztof Karas <krzysztof.karas@intel.com>
Cc: intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
iommu@lists.linux.dev, "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 5/5] drm/i915/gem: Remove iterator and use while loop
Date: Mon, 20 Jul 2026 12:19:51 +0200 [thread overview]
Message-ID: <4aeb95b2eca04bc4972e2e88ac47f34ee6020a07.camel@linux.intel.com> (raw)
In-Reply-To: <gjalgvm2zdtzz67lh7jvzopm4ba44w4eh2rpaaniqc2jyegpn2@edzadixxdexq>
On Mon, 2026-07-20 at 08:25 +0000, Krzysztof Karas wrote:
> Hi Janusz,
>
> On 2026-07-15 at 19:04:42 +0200, Janusz Krzysztofik wrote:
> > Hi Krzysztof,
> >
> > On Mon, 2026-07-13 at 09:58 +0000, Krzysztof Karas wrote:
> > > Change the main "for" loop into "while" to get rid of obscure
> > > iterator "i" and use more descriptive name to indicate how many
> > > pages were already covered. Detect first loop with st->nents and
> > > put instructions for that case in their own block for easier
> > > reading.
> > >
> > > 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, 14 insertions(+), 15 deletions(-)
> > >
> > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > > index 7c8de8fe0a22..66d0f8f6ffcc 100644
> > > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > > @@ -135,11 +135,11 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
> > > unsigned int page_count; /* restricted by sg_alloc_table */
> > > unsigned long next_pfn = 0; /* suppress gcc warning */
> > > unsigned long folio_start = 0;
> > > + unsigned long pages_done = 0;
> > > unsigned long folio_end = 0;
> > > struct folio *folio = NULL;
> > > struct scatterlist *sg;
> > > gfp_t noreclaim;
> > > - unsigned long i;
> > > int ret;
> > >
> > > page_count = size / PAGE_SIZE;
> > > @@ -163,15 +163,15 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
> > >
> > > sg = st->sgl;
> > > st->nents = 0;
> > > - for (i = 0; i < page_count; i++) {
> > > + while (pages_done < page_count) {
> > > unsigned long folio_page_index = 0;
> > > unsigned long nr_pages;
> > > gfp_t gfp = noreclaim;
> > >
> > > /* Grab the next folio if we exhausted the current one. */
> > > - if (!i || i > folio_end) {
> > > - folio = shmem_shrink_get_folio(mapping, i, gfp,
> > > - page_count, i915);
> > > + if (!pages_done || pages_done > folio_end) {
> > > + folio = shmem_shrink_get_folio(mapping, pages_done, gfp,
> > > + page_count - pages_done, i915);
> > > if (IS_ERR(folio)) {
> > > ret = PTR_ERR(folio);
> > > goto err_sg;
> > > @@ -181,7 +181,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
> > > folio_end = folio_start + folio_nr_pages(folio) - 1;
> > > }
> > >
> > > - folio_page_index = i - folio_start;
> > > + folio_page_index = pages_done - folio_start;
> > > if (WARN_ON_ONCE(folio_page_index >= folio_nr_pages(folio))) {
> > > ret = -EINVAL;
> > > folio_put(folio);
> > > @@ -190,16 +190,15 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
> > >
> > > nr_pages = min_array(((unsigned long[]) {
> > > folio_nr_pages(folio) - folio_page_index,
> > > - page_count - i,
> > > + page_count - pages_done,
> > > max_t(unsigned int, 1, max_segment / PAGE_SIZE),
> > > }), 3);
> > > -
> > > - if (!i ||
> > > - sg->length >= max_segment ||
> > > - folio_pfn(folio) + folio_page_index != next_pfn) {
> > > - if (i)
> > > - sg = sg_next(sg);
> > > -
> > > + if (!st->nents) {
> > > + st->nents++;
> > > + sg_set_page(sg, folio_page(folio, 0), nr_pages * PAGE_SIZE, 0);
> > > + } else if (sg->length >= max_segment ||
> > > + folio_pfn(folio) + folio_page_index != next_pfn) {
> > > + sg = sg_next(sg);
> >
> > Repeating two or three lines of code to avoid calling another one
> > conditionally doesn't look optimal to me. Maybe you could invent a simple
> > replacement of that 'if (i)' conditional expression.
> Perhaps it is not optimal. I do not feel comfortable having two
> conditions that contradict each other in the same block, which
> is why I wanted to take out the first iteration setup.
>
> It is more about aesthetics here, so I do not have a strong
> argument here besides readability. If that is not enough, then
> I'll revert to the previous code.
I think that also depends on how you address my comment to your patch 1/5
on that if condition, so we'll see if this comment will be still
applicable.
Thanks,
Janusz
next prev parent reply other threads:[~2026-07-20 10:19 UTC|newest]
Thread overview: 21+ 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
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 [this message]
2026-07-13 11:01 ` ✓ i915.CI.BAT: success for drivers: Improve memory management for large object allocations when i915/shmem is used with iommu Patchwork
2026-07-13 14:14 ` ✗ i915.CI.Full: failure " Patchwork
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=4aeb95b2eca04bc4972e2e88ac47f34ee6020a07.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