Skip to contentSkip to conversation

drm/i915: Use the existing pages when retrying to DMA map

PATCH In review Tvrtko Ursulin 4 participants 8 messages Dec 20, 2016 – Jan 9, 2017

Subsystems (2)
lore
Share & tools
lore

Tvrtko Ursulin

author maintainer
#0 lore raw

PATCH 1/1 drm/i915: Use the existing pages when retrying to DMA map

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

Also change back the page counter to unsigned int because that is what the sg API supports.

---
Only compile tested!

---
 drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +30 -10
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
+
 static struct sg_table *
 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 {
 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
-	const unsigned long page_count = obj->base.size / PAGE_SIZE;
-	unsigned long i;
+	const unsigned int page_count = obj->base.size / PAGE_SIZE;
+	unsigned int i;
 	struct address_space *mapping;
 	struct sg_table *st;
 	struct scatterlist *sg;
@@ -2371,7 +2395,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (st == NULL)
 		return ERR_PTR(-ENOMEM);
 
-rebuild_st:
 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
 		kfree(st);
 		return ERR_PTR(-ENOMEM);
@@ -2429,6 +2452,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	/* Trim unused sg entries to avoid wasting memory. */
 	i915_sg_trim(st);
 
+prepare_gtt:
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret) {
 		/* DMA remapping failed? One possible cause is that
@@ -2436,16 +2460,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 		 * for PAGE_SIZE chunks instead may be helpful.
 		 */
 		if (max_segment > PAGE_SIZE) {
-			for_each_sgt_page(page, sgt_iter, st)
-				put_page(page);
-			sg_free_table(st);
-
+			i915_sg_uncoalesce(st, page_count);
 			max_segment = PAGE_SIZE;
-			goto rebuild_st;
+			goto prepare_gtt;
 		} else {
 			dev_warn(&dev_priv->drm.pdev->dev,
-				 "Failed to DMA remap %lu pages\n",
-				 page_count);
+				 "Failed to DMA remap %u pages\n", page_count);
 			goto err_pages;
 		}
 	}
Signature · 6 lines
2.7.4

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

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

You are freeing and reallocating the pages - I thought you meant the sg_table. The cost for recovering the shmemfs after allocation/swapin already performed in the first pass should be a look up in the radix tree. Not worrisome.

Quoted 2 lines

Also change back the page counter to unsigned int because that is what the sg API supports.

Rather go the other way and start reporting an error when we overflow. Being strict all page_counts should be u64. :|

Quoted 15 lines

---
Only compile tested!

---
 drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +0 -0
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }

drivers/gpu/drm/i915/i915_gem.c static void i915_sg_trim(struct sg_table *orig_st) · line 2351

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +9 -0
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;

I was hoping for a way to use the pages already allocated. Feels possible, just reversing the chain and walking backwards will be fun.

Quoted 13 lines
drivers/gpu/drm/i915/i915_gem.c +13 -0
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
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 11 lines · Chris Wilson
Quoted 5 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

You are freeing and reallocating the pages - I thought you meant the sg_table. The cost for recovering the shmemfs after allocation/swapin already performed in the first pass should be a look up in the radix tree. Not worrisome.

Maybe not, but looks a bit unsightly to me. This is simple enough and one re-allocation less. From both pages and sg entries, to just the sg entries.

Quoted 5 lines
Quoted 2 lines

Also change back the page counter to unsigned int because that is what the sg API supports.

Rather go the other way and start reporting an error when we overflow. Being strict all page_counts should be u64. :|

As I wrote in the other thread, that's already in i915_gem_object_create. Added there at the time when I spotted the sg_alloc_table unsigned int business.

Quoted 16 lines
Quoted 16 lines

---
Only compile tested!

---
 drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +0 -0
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
Quoted 12 lines

drivers/gpu/drm/i915/i915_gem.c static void i915_sg_trim(struct sg_table *orig_st) · line 2351

Quoted 9 lines
drivers/gpu/drm/i915/i915_gem.c +9 -0
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;

I was hoping for a way to use the pages already allocated. Feels possible, just reversing the chain and walking backwards will be fun.

For a rainy day as you said. :)

Quoted 15 lines
Quoted 4 lines
drivers/gpu/drm/i915/i915_gem.c +3 -0
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);

drivers/gpu/drm/i915/i915_gem.c static void i915_sg_trim(struct sg_table *orig_st) · line 2365

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +10 -0
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}

Regards,

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

#3 lore raw

✗ Fi.CI.BAT: failure for drm/i915: Use the existing pages when retrying to DMA map

In reply to Tvrtko Ursulin
== Series Details ==

== Series Details ==

Series: drm/i915: Use the existing pages when retrying to DMA map
URL : https://patchwork.freedesktop.org/series/17061/
State : failure

== Summary ==

Series 17061v1 drm/i915: Use the existing pages when retrying to DMA map
https://patchwork.freedesktop.org/api/1.0/series/17061/revisions/1/mbox/

Test drv_module_reload:

        Subgroup basic-reload-inject:
                dmesg-warn -> PASS       (fi-skl-6260u)
Test kms_busy:
        Subgroup basic-flip-default-b:
                pass       -> INCOMPLETE (fi-bxt-j4205)
Test pm_rpm:
        Subgroup basic-pci-d3-state:
                fail       -> PASS       (fi-skl-6260u)
        Subgroup basic-rte:
                fail       -> PASS       (fi-skl-6260u)
fi-bdw-5557u     total:247  pass:233  dwarn:0   dfail:0   fail:0   skip:14 
fi-bsw-n3050     total:247  pass:208  dwarn:0   dfail:0   fail:0   skip:39 
fi-bxt-j4205     total:170  pass:156  dwarn:0   dfail:0   fail:0   skip:13 
fi-bxt-t5700     total:247  pass:220  dwarn:0   dfail:0   fail:0   skip:27 
fi-byt-j1900     total:247  pass:220  dwarn:0   dfail:0   fail:0   skip:27 
fi-byt-n2820     total:247  pass:216  dwarn:0   dfail:0   fail:0   skip:31 
fi-hsw-4770      total:247  pass:228  dwarn:0   dfail:0   fail:0   skip:19 
fi-hsw-4770r     total:247  pass:228  dwarn:0   dfail:0   fail:0   skip:19 
fi-ilk-650       total:247  pass:195  dwarn:0   dfail:0   fail:0   skip:52 
fi-ivb-3520m     total:247  pass:226  dwarn:0   dfail:0   fail:0   skip:21 
fi-ivb-3770      total:247  pass:226  dwarn:0   dfail:0   fail:0   skip:21 
fi-kbl-7500u     total:247  pass:226  dwarn:0   dfail:0   fail:0   skip:21 
fi-skl-6260u     total:247  pass:234  dwarn:0   dfail:0   fail:0   skip:13 
fi-skl-6700hq    total:247  pass:227  dwarn:0   dfail:0   fail:0   skip:20 
fi-skl-6700k     total:247  pass:224  dwarn:3   dfail:0   fail:0   skip:20 
fi-skl-6770hq    total:247  pass:234  dwarn:0   dfail:0   fail:0   skip:13 
fi-snb-2520m     total:247  pass:216  dwarn:0   dfail:0   fail:0   skip:31 
fi-snb-2600      total:247  pass:215  dwarn:0   dfail:0   fail:0   skip:32 

7285204135ffcf095bea64196db6ef900d47e048 drm-tip: 2016y-12m-20d-14h-50m-18s UTC integration manifest d4c3b77 drm/i915: Use the existing pages when retrying to DMA map

== Logs ==

For more details see: https://intel-gfx-ci.01.org/CI/Patchwork_3342/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#4 lore raw
In reply to Tvrtko Ursulin

Hi,

Quoted 87 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

Also change back the page counter to unsigned int because that is what the sg API supports.

---
Only compile tested!

---
 drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +28 -7
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
+
 static struct sg_table *
 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 {
 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
-	const unsigned long page_count = obj->base.size / PAGE_SIZE;
-	unsigned long i;
+	const unsigned int page_count = obj->base.size / PAGE_SIZE;
+	unsigned int i;
 	struct address_space *mapping;
 	struct sg_table *st;
 	struct scatterlist *sg;
@@ -2371,7 +2395,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (st == NULL)
 		return ERR_PTR(-ENOMEM);
 
-rebuild_st:
 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
 		kfree(st);
 		return ERR_PTR(-ENOMEM);
@@ -2429,6 +2452,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	/* Trim unused sg entries to avoid wasting memory. */
 	i915_sg_trim(st);
 
+prepare_gtt:
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret) {
 		/* DMA remapping failed? One possible cause is that
@@ -2436,16 +2460,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 		 * for PAGE_SIZE chunks instead may be helpful.
 		 */
 		if (max_segment > PAGE_SIZE) {
-			for_each_sgt_page(page, sgt_iter, st)
-				put_page(page);
-			sg_free_table(st);
-
+			i915_sg_uncoalesce(st, page_count);
 			max_segment = PAGE_SIZE;

drivers/gpu/drm/i915/i915_gem.c i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) · line 2471

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +2 -3
-			goto rebuild_st;
+			goto prepare_gtt;
 		} else {
 			dev_warn(&dev_priv->drm.pdev->dev,
-				 "Failed to DMA remap %lu pages\n",
-				 page_count);
+				 "Failed to DMA remap %u pages\n", page_count);
 			goto err_pages;
 		}
 	}

Are you still against this? As a reminder it saves a put_page/allocate page-from-shmemfs cycle on dma mapping failures.

Regards,

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

#5 lore raw
Quoted 96 lines · Tvrtko Ursulin

Hi,

Quoted 92 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

Also change back the page counter to unsigned int because that is what the sg API supports.

---
Only compile tested!
---
drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------

1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +24 -0
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
+

static struct sg_table * i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)

{
	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
drivers/gpu/drm/i915/i915_gem.c +2 -2
-	const unsigned long page_count = obj->base.size / PAGE_SIZE;
-	unsigned long i;
+	const unsigned int page_count = obj->base.size / PAGE_SIZE;
+	unsigned int i;
	struct address_space *mapping;
	struct sg_table *st;
	struct scatterlist *sg;
drivers/gpu/drm/i915/i915_gem.c +3 -8
@@ -2371,7 +2395,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (st == NULL)
 		return ERR_PTR(-ENOMEM);
 
-rebuild_st:
 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
 		kfree(st);
 		return ERR_PTR(-ENOMEM);
@@ -2429,6 +2452,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	/* Trim unused sg entries to avoid wasting memory. */
 	i915_sg_trim(st);
 
+prepare_gtt:
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret) {
 		/* DMA remapping failed? One possible cause is that
@@ -2436,16 +2460,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 		 * for PAGE_SIZE chunks instead may be helpful.
 		 */
 		if (max_segment > PAGE_SIZE) {
-			for_each_sgt_page(page, sgt_iter, st)
-				put_page(page);
-			sg_free_table(st);
-
+			i915_sg_uncoalesce(st, page_count);
 			max_segment = PAGE_SIZE;
-			goto rebuild_st;
+			goto prepare_gtt;
 		} else {
 			dev_warn(&dev_priv->drm.pdev->dev,
-				 "Failed to DMA remap %lu pages\n",
-				 page_count);
Quoted 8 lines

drivers/gpu/drm/i915/i915_gem.c i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) · line 2471

Quoted 5 lines
drivers/gpu/drm/i915/i915_gem.c +1 -0
+				 "Failed to DMA remap %u pages\n", page_count);
 			goto err_pages;
 		}
 	}

Are you still against this? As a reminder it saves a put_page/allocate page-from-shmemfs cycle on dma mapping failures.

I still regard it as incomplete. Why bother saving that trip and not the actual page allocation itself.
-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
#6 lore raw
Quoted 106 lines · Chris Wilson
Quoted 96 lines · Tvrtko Ursulin

Hi,

Quoted 92 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

Also change back the page counter to unsigned int because that is what the sg API supports.

---
Only compile tested!
---
drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------

1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +24 -0
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
+

static struct sg_table * i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)

{
	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
drivers/gpu/drm/i915/i915_gem.c +2 -2
-	const unsigned long page_count = obj->base.size / PAGE_SIZE;
-	unsigned long i;
+	const unsigned int page_count = obj->base.size / PAGE_SIZE;
+	unsigned int i;
	struct address_space *mapping;
	struct sg_table *st;
	struct scatterlist *sg;
drivers/gpu/drm/i915/i915_gem.c +3 -8
@@ -2371,7 +2395,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (st == NULL)
 		return ERR_PTR(-ENOMEM);
 
-rebuild_st:
 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
 		kfree(st);
 		return ERR_PTR(-ENOMEM);
@@ -2429,6 +2452,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	/* Trim unused sg entries to avoid wasting memory. */
 	i915_sg_trim(st);
 
+prepare_gtt:
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret) {
 		/* DMA remapping failed? One possible cause is that
@@ -2436,16 +2460,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 		 * for PAGE_SIZE chunks instead may be helpful.
 		 */
 		if (max_segment > PAGE_SIZE) {
-			for_each_sgt_page(page, sgt_iter, st)
-				put_page(page);
-			sg_free_table(st);
-
+			i915_sg_uncoalesce(st, page_count);
 			max_segment = PAGE_SIZE;
-			goto rebuild_st;
+			goto prepare_gtt;
 		} else {
 			dev_warn(&dev_priv->drm.pdev->dev,
-				 "Failed to DMA remap %lu pages\n",
-				 page_count);
Quoted 8 lines

drivers/gpu/drm/i915/i915_gem.c i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) · line 2471

Quoted 5 lines
drivers/gpu/drm/i915/i915_gem.c +1 -0
+				 "Failed to DMA remap %u pages\n", page_count);
 			goto err_pages;
 		}
 	}

Are you still against this? As a reminder it saves a put_page/allocate page-from-shmemfs cycle on dma mapping failures.

Quoted 2 lines

I still regard it as incomplete. Why bother saving that trip and not the actual page allocation itself.

Page allocation is exactly what it avoids re-doing!?

You meant sg_alloc_table maybe? We could move the trim to be last and do the uncoalesce in place but it would be so much more complex that I am not sure it is worth it for this edge case.

The patch above was fast and simple improvement to the dma remapping retry you added.

Regards,

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

#7 lore raw
Quoted 108 lines · Tvrtko Ursulin
Quoted 106 lines · Chris Wilson
Quoted 104 lines · Tvrtko Ursulin

Hi,

Quoted 87 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Rather than freeing and re-allocating the pages when DMA mapping in large chunks fails, we can just rebuild the sg table with no coalescing.

Also change back the page counter to unsigned int because that is what the sg API supports.

---
Only compile tested!
---
drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++----------

1 file changed, 30 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +24 -0
index 5275f6248ce3..e73f9f5a5d23 100644
@@ -2340,12 +2340,36 @@ static void i915_sg_trim(struct sg_table *orig_st)
 	*orig_st = new_st;
 }
 
+static void i915_sg_uncoalesce(struct sg_table *orig_st, unsigned long nents)
+{
+	struct sg_table new_st;
+	struct scatterlist *new_sg;
+	struct sgt_iter sgt_iter;
+	struct page *page;
+
+	if (sg_alloc_table(&new_st, nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sgt_page(page, sgt_iter, orig_st) {
+		sg_set_page(new_sg, page, PAGE_SIZE, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
+
+	sg_free_table(orig_st);
+
+	*orig_st = new_st;
+}
+

static struct sg_table * i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)

{
	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
drivers/gpu/drm/i915/i915_gem.c +2 -2
-	const unsigned long page_count = obj->base.size / PAGE_SIZE;
-	unsigned long i;
+	const unsigned int page_count = obj->base.size / PAGE_SIZE;
+	unsigned int i;
	struct address_space *mapping;
	struct sg_table *st;
	struct scatterlist *sg;
drivers/gpu/drm/i915/i915_gem.c +2 -5
@@ -2371,7 +2395,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (st == NULL)
 		return ERR_PTR(-ENOMEM);
 
-rebuild_st:
 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
 		kfree(st);
 		return ERR_PTR(-ENOMEM);
@@ -2429,6 +2452,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	/* Trim unused sg entries to avoid wasting memory. */
 	i915_sg_trim(st);
 
+prepare_gtt:
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret) {
 		/* DMA remapping failed? One possible cause is that
@@ -2436,16 +2460,12 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 		 * for PAGE_SIZE chunks instead may be helpful.
 		 */
 		if (max_segment > PAGE_SIZE) {
-			for_each_sgt_page(page, sgt_iter, st)
-				put_page(page);
-			sg_free_table(st);
-
+			i915_sg_uncoalesce(st, page_count);
 			max_segment = PAGE_SIZE;

drivers/gpu/drm/i915/i915_gem.c i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) · line 2471

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +2 -3
-			goto rebuild_st;
+			goto prepare_gtt;
 		} else {
 			dev_warn(&dev_priv->drm.pdev->dev,
-				 "Failed to DMA remap %lu pages\n",
-				 page_count);
+				 "Failed to DMA remap %u pages\n", page_count);
 			goto err_pages;
 		}
 	}

Are you still against this? As a reminder it saves a put_page/allocate page-from-shmemfs cycle on dma mapping failures.

Quoted 4 lines
Quoted 2 lines

I still regard it as incomplete. Why bother saving that trip and not the actual page allocation itself.

Page allocation is exactly what it avoids re-doing!?

It is not. The shmemfs pages are not being freed nor reallocated, just their use count being manipulated.

Quoted 3 lines

You meant sg_alloc_table maybe? We could move the trim to be last and do the uncoalesce in place but it would be so much more complex that I am not sure it is worth it for this edge case.

Only the sg_table is being reallocated and doing actual kfree/kmalloc.

Quoted 2 lines

The patch above was fast and simple improvement to the dma remapping retry you added.

I didn't value it as being a significant improvement and thought you have a more complete solution in mind.

For starters we can reduce the duplication here with

drivers/gpu/drm/i915/i915_gem.c +19 -19
index ed3cd1a5f9bb..1bfec811f9b7 100644
@@ -2199,20 +2199,12 @@ void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
        invalidate_mapping_pages(mapping, 0, (loff_t)-1);
 }
 
-static void
-i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
-                             struct sg_table *pages)
+static void free_sg_pages(struct drm_i915_gem_object *obj,
+                         struct sg_table *pages)
 {
        struct sgt_iter sgt_iter;
        struct page *page;
 
-       __i915_gem_object_release_shmem(obj, pages, true);
-
-       i915_gem_gtt_finish_pages(obj, pages);
-
-       if (i915_gem_object_needs_bit17_swizzle(obj))
-               i915_gem_object_save_bit_17_swizzle(obj, pages);
-
        for_each_sgt_page(page, sgt_iter, pages) {
                if (obj->mm.dirty)
                        set_page_dirty(page);
@@ -2222,9 +2214,23 @@ i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
 
                put_page(page);
        }
+       sg_free_table(pages);
+
        obj->mm.dirty = false;
+}
 
-       sg_free_table(pages);
+static void
+i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
+                             struct sg_table *pages)
+{
+       __i915_gem_object_release_shmem(obj, pages, true);
+
+       i915_gem_gtt_finish_pages(obj, pages);
+
+       if (i915_gem_object_needs_bit17_swizzle(obj))
+               i915_gem_object_save_bit_17_swizzle(obj, pages);
+
+       free_sg_pages(obj, pages);
        kfree(pages);
 }
 
@@ -2322,7 +2328,6 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
        struct address_space *mapping;
        struct sg_table *st;
        struct scatterlist *sg;
-       struct sgt_iter sgt_iter;
        struct page *page;
        unsigned long last_pfn = 0;     /* suppress gcc warning */
        unsigned int max_segment;
@@ -2409,10 +2414,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
                 * for PAGE_SIZE chunks instead may be helpful.
                 */
                if (max_segment > PAGE_SIZE) {
-                       for_each_sgt_page(page, sgt_iter, st)
-                               put_page(page);
-                       sg_free_table(st);
-
+                       free_sg_pages(obj, st);
                        max_segment = PAGE_SIZE;
                        goto rebuild_st;
                } else {
@@ -2431,9 +2433,7 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 err_sg:
        sg_mark_end(sg);
 err_pages:
-       for_each_sgt_page(page, sgt_iter, st)
-               put_page(page);
-       sg_free_table(st);
+       free_sg_pages(obj, st);
        kfree(st);
 
        /* shmemfs first checks if there is enough memory to allocate the page
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx