Skip to contentSkip to conversation

drm/i915: Introduce i915_alloc_sg_table

RFC In review Tvrtko Ursulin 4 participants 4 messages Oct 10, 2016 – Oct 11, 2016

Subsystems (2)
lore
Share & tools
lore

Tvrtko Ursulin

author maintainer
#0 lore raw

RFC 1/1 drm/i915: Introduce i915_alloc_sg_table

From: Tvrtko Ursulin <[email protected]>

There are currently two places in the code which build the sg table for the object backing store and in the future there will be one more.

Consolidate that into a single helper which takes a caller defined context and callbacks.

!!! Compile tested only. !!!

---
 drivers/gpu/drm/i915/i915_gem.c         | 162 ++++++++++++++++----------------
 drivers/gpu/drm/i915/i915_gem_gtt.c     |  84 +++++++++++++++++
 drivers/gpu/drm/i915/i915_gem_gtt.h     |  14 +++
 drivers/gpu/drm/i915/i915_gem_userptr.c |  54 ++++-------
 4 files changed, 198 insertions(+), 116 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +83 -79
index a89a88922448..79dcd912759f 100644
@@ -2208,19 +2208,69 @@ i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
 	return 0;
 }
 
+struct i915_gem_get_pages_gtt_ctx {
+	struct drm_i915_private *dev_priv;
+	gfp_t gfp;
+	struct address_space *mapping;
+	unsigned int page_count;
+	bool shrunk_all;
+};
+
+static struct page *
+i915_gem_gtt_get_page(void *context, unsigned int page_num)
+{
+	struct i915_gem_get_pages_gtt_ctx *ctx = context;
+	struct page *page;
+
+	if (!ctx->shrunk_all)
+		page = shmem_read_mapping_page_gfp(ctx->mapping, page_num,
+						   ctx->gfp);
+	else
+		page = shmem_read_mapping_page(ctx->mapping, page_num);
+
+	/* Check that the i965g/gm workaround works. */
+	WARN_ON(page && !IS_ERR(page) && (ctx->gfp & __GFP_DMA32) &&
+		(page_to_pfn(page) >= 0x00100000UL));
+
+	return page;
+}
+
+static bool
+i915_gem_gtt_get_page_failed(void *context, unsigned int page_num,
+			     unsigned int err_cnt, int err)
+{
+	struct i915_gem_get_pages_gtt_ctx *ctx = context;
+
+	if (err_cnt > 1)
+		return true;
+
+	if (err_cnt == 0) {
+		i915_gem_shrink(ctx->dev_priv, ctx->page_count,
+				I915_SHRINK_BOUND | I915_SHRINK_UNBOUND |
+				I915_SHRINK_PURGEABLE);
+	} else {
+		i915_gem_shrink_all(ctx->dev_priv);
+		ctx->shrunk_all = true;
+	}
+
+	return false;
+}
+
+static void
+i915_gem_gtt_put_page(void *context, struct page *page)
+{
+	put_page(page);
+}
+
 static int
 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 {
 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
-	int page_count, i;
-	struct address_space *mapping;
+	struct i915_gem_get_pages_gtt_ctx ctx;
 	struct sg_table *st;
-	struct scatterlist *sg;
 	struct sgt_iter sgt_iter;
 	struct page *page;
-	unsigned long last_pfn = 0;	/* suppress gcc warning */
 	int ret;
-	gfp_t gfp;
 
 	/* Assert that the object is not currently in any GPU domain. As it
 	 * wasn't in the GTT, there shouldn't be any way it could have been in
@@ -2229,78 +2279,44 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
 	BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
 
-	st = kmalloc(sizeof(*st), GFP_KERNEL);
-	if (st == NULL)
-		return -ENOMEM;
-
-	page_count = obj->base.size / PAGE_SIZE;
-	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
-		kfree(st);
-		return -ENOMEM;
-	}
+	ctx.dev_priv = dev_priv;
+	ctx.page_count = obj->base.size / PAGE_SIZE;
+	ctx.shrunk_all = false;
 
 	/* Get the list of pages out of our struct file.  They'll be pinned
 	 * at this point until we release them.
 	 *
 	 * Fail silently without starting the shrinker
 	 */
-	mapping = obj->base.filp->f_mapping;
-	gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
-	gfp |= __GFP_NORETRY | __GFP_NOWARN;
-	sg = st->sgl;
-	st->nents = 0;
-	for (i = 0; i < page_count; i++) {
-		page = shmem_read_mapping_page_gfp(mapping, i, gfp);
-		if (IS_ERR(page)) {
-			i915_gem_shrink(dev_priv,
-					page_count,
-					I915_SHRINK_BOUND |
-					I915_SHRINK_UNBOUND |
-					I915_SHRINK_PURGEABLE);
-			page = shmem_read_mapping_page_gfp(mapping, i, gfp);
-		}
-		if (IS_ERR(page)) {
-			/* 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.
-			 */
-			i915_gem_shrink_all(dev_priv);
-			page = shmem_read_mapping_page(mapping, i);
-			if (IS_ERR(page)) {
-				ret = PTR_ERR(page);
-				goto err_pages;
-			}
-		}
-#ifdef CONFIG_SWIOTLB
-		if (swiotlb_nr_tbl()) {
-			st->nents++;
-			sg_set_page(sg, page, PAGE_SIZE, 0);
-			sg = sg_next(sg);
-			continue;
-		}
-#endif
-		if (!i || page_to_pfn(page) != last_pfn + 1) {
-			if (i)
-				sg = sg_next(sg);
-			st->nents++;
-			sg_set_page(sg, page, PAGE_SIZE, 0);
-		} else {
-			sg->length += PAGE_SIZE;
-		}
-		last_pfn = page_to_pfn(page);
-
-		/* Check that the i965g/gm workaround works. */
-		WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
+	ctx.mapping = obj->base.filp->f_mapping;
+	ctx.gfp = mapping_gfp_constraint(ctx.mapping,
+					 ~(__GFP_IO | __GFP_RECLAIM));
+	ctx.gfp |= __GFP_NORETRY | __GFP_NOWARN;
+
+	st = i915_alloc_sg_table(ctx.page_count, &ctx,
+				 i915_gem_gtt_get_page,
+				 i915_gem_gtt_get_page_failed,
+				 i915_gem_gtt_put_page);
+	if (IS_ERR(st)) {
+		ret = PTR_ERR(st);
+		/* shmemfs first checks if there is enough memory to allocate
+		 * the page and reports ENOSPC should there be insufficient,
+		 * along with the usual ENOMEM for a genuine allocation failure.
+		 *
+		 * We use ENOSPC in our driver to mean that we have run out of
+		 * aperture space and so want to translate the error from
+		 * shmemfs back to our usual understanding of ENOMEM.
+		 */
+		if (ret == -ENOSPC)
+			ret = -ENOMEM;
+		return ret;
 	}
-#ifdef CONFIG_SWIOTLB
-	if (!swiotlb_nr_tbl())
-#endif
-		sg_mark_end(sg);
+
 	obj->pages = st;
 
 	ret = i915_gem_gtt_prepare_object(obj);
 	if (ret)
-		goto err_pages;
+		goto err;
 
 	if (i915_gem_object_needs_bit17_swizzle(obj))
 		i915_gem_object_do_bit_17_swizzle(obj);
@@ -2311,24 +2327,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 
 	return 0;
 
-err_pages:
-	sg_mark_end(sg);
+err:
 	for_each_sgt_page(page, sgt_iter, st)
 		put_page(page);
 	sg_free_table(st);
 	kfree(st);
 
-	/* shmemfs first checks if there is enough memory to allocate the page
-	 * and reports ENOSPC should there be insufficient, along with the usual
-	 * ENOMEM for a genuine allocation failure.
-	 *
-	 * We use ENOSPC in our driver to mean that we have run out of aperture
-	 * space and so want to translate the error from shmemfs back to our
-	 * usual understanding of ENOMEM.
-	 */
-	if (ret == -ENOSPC)
-		ret = -ENOMEM;
-
 	return ret;
 }
 
drivers/gpu/drm/i915/i915_gem_gtt.c +84 -0

87 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_gtt.h +14 -0
index ec78be2f8c77..17768c99605e 100644
@@ -730,4 +730,18 @@ static inline struct page *i915_vma_first_page(struct i915_vma *vma)
 	return sg_page(vma->pages->sgl);
 }
 
+typedef struct page *
+(*i915_page_alloc_f)(void *context, unsigned int page_num);
+typedef bool
+(*i915_page_alloc_err_f)(void *context, unsigned int page_num,
+			 unsigned int err_cnt, int err);
+typedef void
+(*i915_page_put_f)(void *context, struct page *page);
+
+struct sg_table *
+i915_alloc_sg_table(unsigned int page_count, void *context,
+		    i915_page_alloc_f page_alloc,
+		    i915_page_alloc_err_f page_alloc_error,
+		    i915_page_put_f page_put);
+
 #endif
drivers/gpu/drm/i915/i915_gem_userptr.c +17 -37

71 lines not on this page · raw message

Signature · 6 lines
2.7.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

There are currently two places in the code which build the sg table for the object backing store and in the future there will be one more.

Consolidate that into a single helper which takes a caller defined context and callbacks.

Not getting warm fuzzy feelings about it. To surmise, what you really want is a common method of applying sg coalescing whilst iteratively building the sg list.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw
Quoted 7 lines · Chris Wilson
Quoted 6 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

There are currently two places in the code which build the sg table for the object backing store and in the future there will be one more.

Quoted 5 lines
Quoted 2 lines

Consolidate that into a single helper which takes a caller defined context and callbacks.

Not getting warm fuzzy feelings about it. To surmise, what you really want is a common method of applying sg coalescing whilst iteratively building the sg list.

Something like wrapping the sg_set_page and sg_next into a single i915 wrapper with some local status then?

Without too much thought:

i915_sg_build_init(&iter ... )
...

for (...) {
    i915_sg_build_next(&iter, i, page)

?

I'll sketch it out in more detail when you merge your swiotlb improvement.

Regards,

Tvrtko

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#3 lore raw

✓ Fi.CI.BAT: success for drm/i915: Introduce i915_alloc_sg_table

In reply to Tvrtko Ursulin
== Series Details ==

== Series Details ==

Series: drm/i915: Introduce i915_alloc_sg_table
URL : https://patchwork.freedesktop.org/series/13524/
State : success

== Summary ==

Series 13524v1 drm/i915: Introduce i915_alloc_sg_table
https://patchwork.freedesktop.org/api/1.0/series/13524/revisions/1/mbox/

Test vgem_basic:

        Subgroup unload:
                skip       -> PASS       (fi-bdw-5557u)
fi-bdw-5557u     total:248  pass:232  dwarn:0   dfail:0   fail:0   skip:16 
fi-bsw-n3050     total:248  pass:204  dwarn:0   dfail:0   fail:0   skip:44 
fi-bxt-t5700     total:248  pass:217  dwarn:0   dfail:0   fail:0   skip:31 
fi-byt-j1900     total:248  pass:213  dwarn:2   dfail:0   fail:1   skip:32 
fi-hsw-4770      total:248  pass:224  dwarn:0   dfail:0   fail:0   skip:24 
fi-hsw-4770r     total:248  pass:224  dwarn:0   dfail:0   fail:0   skip:24 
fi-ivb-3520m     total:248  pass:221  dwarn:0   dfail:0   fail:0   skip:27 
fi-ivb-3770      total:248  pass:207  dwarn:0   dfail:0   fail:0   skip:41 
fi-kbl-7200u     total:248  pass:222  dwarn:0   dfail:0   fail:0   skip:26 
fi-skl-6260u     total:248  pass:232  dwarn:0   dfail:0   fail:0   skip:16 
fi-skl-6700hq    total:248  pass:224  dwarn:0   dfail:0   fail:0   skip:24 
fi-skl-6700k     total:248  pass:221  dwarn:1   dfail:0   fail:0   skip:26 
fi-skl-6770hq    total:248  pass:231  dwarn:1   dfail:0   fail:1   skip:15 
fi-snb-2520m     total:248  pass:211  dwarn:0   dfail:0   fail:0   skip:37 
fi-snb-2600      total:248  pass:209  dwarn:0   dfail:0   fail:0   skip:39 

Results at /archive/results/CI_IGT_test/Patchwork_2659/

e37a15c8d775e79dddc8345a0f6afdcfe1f607d9 drm-intel-nightly: 2016y-10m-10d-14h-33m-29s UTC integration manifest 1db9a28 drm/i915: Introduce i915_alloc_sg_table

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx