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.
Signed-off-by: Krzysztof Karas <[email protected]> --- v4: * Removed "else if" and reverted to having all three conditions inside first "if" (Janusz); 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 ac8022997553..f8fe5e3a4eb4 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -137,11 +137,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; ret = set_page_count_from_size(size, &page_count, mr); @@ -166,15 +166,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; @@ -184,7 +184,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); @@ -193,14 +193,14 @@ 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_segment / PAGE_SIZE, }), 3); - if (!i || + if (!st->nents || sg->length >= max_segment || folio_pfn(folio) + folio_page_index != next_pfn) { - if (i) + if (st->nents) sg = sg_next(sg); st->nents++; @@ -223,7 +223,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
