Change the main "for" loop into "while" to get rid of obscure
iterator "i" and use a more descriptive name to indicate how
many pages were already covered. Detect first loop with
st->nents.

Assisted-by: GitHub Copilot:claude-opus-5
Signed-off-by: Krzysztof Karas <[email protected]>
---
v6:
 * restored shrinking to 2 * page_count, as v5 silently narrowed
   it to page_count - pages_done;
 * ran final checks with Claude Opus and added a tag.

 drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c 
b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
index 0fa34a4ae466..88ad23928d9a 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
@@ -135,7 +135,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, 
struct sg_table *st,
                         unsigned int max_segment)
 {
        unsigned int page_count; /* restricted by sg_alloc_table */
-       unsigned long i;
+       unsigned long pages_done = 0;
        struct scatterlist *sg;
        /* suppress gcc warnings */
        unsigned long next_pfn = 0;
@@ -167,15 +167,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;
                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, i915);
                        if (IS_ERR(folio)) {
                                ret = PTR_ERR(folio);
                                goto err_sg;
@@ -185,14 +185,14 @@ 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;
                nr_pages = min3(folio_nr_pages(folio) - folio_page_index,
-                               page_count - i, max_segment / PAGE_SIZE);
+                               page_count - pages_done, max_segment / 
PAGE_SIZE);
 
-               if (!i ||
+               if (!st->nents ||
                    sg->length >= max_segment ||
                    folio_pfn(folio) != next_pfn) {
-                       if (i)
+                       if (st->nents)
                                sg = sg_next(sg);
 
                        st->nents++;
@@ -214,7 +214,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, 
struct sg_table *st,
                 * and predict where the next folio begins.
                 */
                next_pfn = folio_pfn(folio) + folio_page_index + nr_pages;
-               i += nr_pages - 1;
+               pages_done += nr_pages;
 
                /* Check that the i965g/gm workaround works. */
                GEM_BUG_ON(gfp & __GFP_DMA32 && next_pfn >= 0x00100000UL);
-- 
2.34.1

Reply via email to