i915: Limit the length of an sg list to the requested length
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / i915 / gem / i915_gem_shmem.c
index 8f1633c..73a4a4e 100644 (file)
@@ -100,6 +100,7 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
        st->nents = 0;
        for (i = 0; i < page_count; i++) {
                struct folio *folio;
+               unsigned long nr_pages;
                const unsigned int shrink[] = {
                        I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
                        0,
@@ -150,6 +151,8 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
                        }
                } while (1);
 
+               nr_pages = min_t(unsigned long,
+                               folio_nr_pages(folio), page_count - i);
                if (!i ||
                    sg->length >= max_segment ||
                    folio_pfn(folio) != next_pfn) {
@@ -157,13 +160,13 @@ int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
                                sg = sg_next(sg);
 
                        st->nents++;
-                       sg_set_folio(sg, folio, folio_size(folio), 0);
+                       sg_set_folio(sg, folio, nr_pages * PAGE_SIZE, 0);
                } else {
                        /* XXX: could overflow? */
-                       sg->length += folio_size(folio);
+                       sg->length += nr_pages * PAGE_SIZE;
                }
-               next_pfn = folio_pfn(folio) + folio_nr_pages(folio);
-               i += folio_nr_pages(folio) - 1;
+               next_pfn = folio_pfn(folio) + nr_pages;
+               i += nr_pages - 1;
 
                /* Check that the i965g/gm workaround works. */
                GEM_BUG_ON(gfp & __GFP_DMA32 && next_pfn >= 0x00100000UL);