Skip to contentSkip to conversation

drm/i915: Trim the object sg table

PATCH Applied Tvrtko Ursulin 4 participants 7 messages Nov 9, 2016 – Nov 10, 2016

Subsystems (2)
lore
Share & tools
lore

Tvrtko Ursulin

author maintainer
#0 lore raw

PATCH 1/1 drm/i915: Trim the object sg table

From: Tvrtko Ursulin <[email protected]>

At the moment we allocate enough sg table entries assuming we will not be able to do any coallescing. But since in practice we most often can, and more so very effectively, this ends up wasting a lot of memory.

A simple and effective way of trimming the over-allocated entries is to copy the table over to a new one allocated to the exact size.

Experiment on my freshly logged and idle desktop (KDE) showed that by doing this we can save approximately 1 MiB of RAM, or when running a typical benchmark like gl_manhattan I have even seen a 6 MiB saving.

v2:
* Update commit message.
* Use temporary sg_table on stack. (Chris Wilson)

---
 drivers/gpu/drm/i915/i915_gem.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/gpu/drm/i915/i915_gem.c +25 -0
index d2ad73d0b5b9..411aae535abe 100644
@@ -2232,6 +2232,28 @@ static unsigned int swiotlb_max_size(void)
 #endif
 }
 
+static void i915_sg_trim(struct sg_table *orig_st)
+{
+	struct sg_table new_st;
+	struct scatterlist *sg, *new_sg;
+	unsigned int i;
+
+	if (orig_st->nents == orig_st->orig_nents)
+		return;
+
+	if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
+		sg_set_page(new_sg, sg_page(sg), sg->length, 0);
+		new_sg = sg_next(new_sg);
+	}
+
+	sg_free_table(orig_st);
+	memcpy(orig_st, &new_st, sizeof(*orig_st));
+}
+
 static struct sg_table *
 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 {
@@ -2317,6 +2339,9 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (sg) /* loop terminated early; short sg table */
 		sg_mark_end(sg);
 
+	/* Trim unused sg entries to avoid wasting memory. */
+	i915_sg_trim(st);
+
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret)
 		goto err_pages;
Signature · 6 lines
2.7.4

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

From: Tvrtko Ursulin <[email protected]>

At the moment we allocate enough sg table entries assuming we will not be able to do any coallescing. But since in practice we most often can, and more so very effectively, this ends up wasting a lot of memory.

Quoted 5 lines

A simple and effective way of trimming the over-allocated entries is to copy the table over to a new one allocated to the exact size.

Experiment on my freshly logged and idle desktop (KDE) showed

Experiments

Quoted 3 lines

that by doing this we can save approximately 1 MiB of RAM, or when running a typical benchmark like gl_manhattan I have even seen a 6 MiB saving.

More complicated techniques such as only copying the last used page and freeing the rest are left to the reader.

Quoted 24 lines

v2:
* Update commit message.
* Use temporary sg_table on stack. (Chris Wilson)

---
 drivers/gpu/drm/i915/i915_gem.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/gpu/drm/i915/i915_gem.c +6 -0
index d2ad73d0b5b9..411aae535abe 100644
@@ -2232,6 +2232,28 @@ static unsigned int swiotlb_max_size(void)
 #endif
 }
 
+static void i915_sg_trim(struct sg_table *orig_st)
+{
+	struct sg_table new_st;
+	struct scatterlist *sg, *new_sg;
+	unsigned int i;
+

drivers/gpu/drm/i915/i915_gem.c static unsigned int swiotlb_max_size(void) · line 2250

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +10 -0
+	if (orig_st->nents == orig_st->orig_nents)
+		return;
+
+	if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
+		sg_set_page(new_sg, sg_page(sg), sg->length, 0);
+		new_sg = sg_next(new_sg);

Worth a

	/* called before being DMA mapped, no need to copy sg->dma_* */

?

drivers/gpu/drm/i915/i915_gem.c static unsigned int swiotlb_max_size(void) · line 2254

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem.c +4 -0
+	}
+
+	sg_free_table(orig_st);
+	memcpy(orig_st, &new_st, sizeof(*orig_st));

I would have used *orig_st = new;

^ I remembered it this time!
Took a couple of attempts to spell my name right though. -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 12 lines · Chris Wilson
Quoted 11 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

At the moment we allocate enough sg table entries assuming we will not be able to do any coallescing. But since in practice we most often can, and more so very effectively, this ends up wasting a lot of memory.

A simple and effective way of trimming the over-allocated entries is to copy the table over to a new one allocated to the exact size.

Quoted 8 lines
Quoted 1 line

Experiment on my freshly logged and idle desktop (KDE) showed

Experiments

Quoted 3 lines

that by doing this we can save approximately 1 MiB of RAM, or when running a typical benchmark like gl_manhattan I have even seen a 6 MiB saving.

More complicated techniques such as only copying the last used page and freeing the rest are left to the reader.

Yes that would need to go into the core kernel since it needs access to static alloc/free functions and performance benefit might be quite small for that. Typically I see coalescing working really well so the delta in saved allocations and frees would be quite small. Perhaps I need to attempt to fragment my memory a lot to see what happens then.

Quoted 36 lines
Quoted 24 lines

v2:
* Update commit message.
* Use temporary sg_table on stack. (Chris Wilson)

---
 drivers/gpu/drm/i915/i915_gem.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/gpu/drm/i915/i915_gem.c +6 -0
index d2ad73d0b5b9..411aae535abe 100644
@@ -2232,6 +2232,28 @@ static unsigned int swiotlb_max_size(void)
 #endif
 }
 
+static void i915_sg_trim(struct sg_table *orig_st)
+{
+	struct sg_table new_st;
+	struct scatterlist *sg, *new_sg;
+	unsigned int i;
+

drivers/gpu/drm/i915/i915_gem.c static unsigned int swiotlb_max_size(void) · line 2250

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +10 -0
+	if (orig_st->nents == orig_st->orig_nents)
+		return;
+
+	if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
+		sg_set_page(new_sg, sg_page(sg), sg->length, 0);
+		new_sg = sg_next(new_sg);

Worth a

Quoted 2 lines
	/* called before being DMA mapped, no need to copy sg->dma_* */

?

Hm, or something safer than a comment. Unfortunately entries are not zeroed by default to enable a GEM_BUG_ON here. Unless CONFIG_GEM_DEBUG could mean GFP_ZERO added to some our allocations. :)

Yeah I think comment is the best option as long as this function is static only. Will add.

Quoted 7 lines

drivers/gpu/drm/i915/i915_gem.c static unsigned int swiotlb_max_size(void) · line 2254

Quoted 5 lines
drivers/gpu/drm/i915/i915_gem.c +4 -0
+	}
+
+	sg_free_table(orig_st);
+	memcpy(orig_st, &new_st, sizeof(*orig_st));

I would have used *orig_st = new;

It is more readable, agreed.

Quoted 4 lines

^ I remembered it this time!
Took a couple of attempts to spell my name right though.

Thanks! I assume I can keep it for the above little changes.

Regards,

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

#3 lore raw
Quoted 7 lines · Tvrtko Ursulin
Quoted 3 lines · Chris Wilson

^ I remembered it this time!
Took a couple of attempts to spell my name right though.

Thanks! I assume I can keep it for the above little changes.

Yes.
-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
author maintainer
#4 lore raw

PATCH v3 1/1 drm/i915: Trim the object sg table

In reply to Chris Wilson

From: Tvrtko Ursulin <[email protected]>

At the moment we allocate enough sg table entries assuming we will not be able to do any coalescing. But since in practice we most often can, and more so very effectively, this ends up wasting a lot of memory.

A simple and effective way of trimming the over-allocated entries is to copy the table over to a new one allocated to the exact size.

Experiments on my freshly logged and idle desktop (KDE) showed that by doing this we can save approximately 1 MiB of RAM, or when running a typical benchmark like gl_manhattan I have even seen a 6 MiB saving.

More complicated techniques such as only copying the last used page and freeing the rest are left to the reader.

v2:
* Update commit message.
* Use temporary sg_table on stack. (Chris Wilson)

v3:
* Commit message update.
* Comment added.
* Replace memcpy with copy assignment.
(Chris Wilson)

---
 drivers/gpu/drm/i915/i915_gem.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/gpu/drm/i915/i915_gem.c +27 -0
index d2ad73d0b5b9..1c20edba7f2a 100644
@@ -2232,6 +2232,30 @@ static unsigned int swiotlb_max_size(void)
 #endif
 }
 
+static void i915_sg_trim(struct sg_table *orig_st)
+{
+	struct sg_table new_st;
+	struct scatterlist *sg, *new_sg;
+	unsigned int i;
+
+	if (orig_st->nents == orig_st->orig_nents)
+		return;
+
+	if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL))
+		return;
+
+	new_sg = new_st.sgl;
+	for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
+		sg_set_page(new_sg, sg_page(sg), sg->length, 0);
+		/* called before being DMA mapped, no need to copy sg->dma_* */
+		new_sg = sg_next(new_sg);
+	}
+
+	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)
 {
@@ -2317,6 +2341,9 @@ i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
 	if (sg) /* loop terminated early; short sg table */
 		sg_mark_end(sg);
 
+	/* Trim unused sg entries to avoid wasting memory. */
+	i915_sg_trim(st);
+
 	ret = i915_gem_gtt_prepare_pages(obj, st);
 	if (ret)
 		goto err_pages;
Signature · 6 lines
2.7.4

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

✗ Fi.CI.BAT: warning for drm/i915: Trim the object sg table (rev2)

In reply to Tvrtko Ursulin
== Series Details ==

== Series Details ==

Series: drm/i915: Trim the object sg table (rev2)
URL : https://patchwork.freedesktop.org/series/15036/
State : warning

== Summary ==

Series 15036v2 drm/i915: Trim the object sg table
https://patchwork.freedesktop.org/api/1.0/series/15036/revisions/2/mbox/

Test drv_module_reload_basic:

                pass       -> DMESG-WARN (fi-bsw-n3050)
fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15 
fi-bsw-n3050     total:244  pass:203  dwarn:1   dfail:0   fail:0   skip:40 
fi-bxt-t5700     total:244  pass:216  dwarn:0   dfail:0   fail:0   skip:28 
fi-byt-j1900     total:244  pass:216  dwarn:0   dfail:0   fail:0   skip:28 
fi-byt-n2820     total:244  pass:212  dwarn:0   dfail:0   fail:0   skip:32 
fi-hsw-4770      total:244  pass:224  dwarn:0   dfail:0   fail:0   skip:20 
fi-hsw-4770r     total:244  pass:224  dwarn:0   dfail:0   fail:0   skip:20 
fi-ilk-650       total:244  pass:191  dwarn:0   dfail:0   fail:0   skip:53 
fi-ivb-3520m     total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22 
fi-ivb-3770      total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22 
fi-kbl-7200u     total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22 
fi-skl-6260u     total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14 
fi-skl-6700hq    total:244  pass:223  dwarn:0   dfail:0   fail:0   skip:21 
fi-skl-6700k     total:244  pass:222  dwarn:1   dfail:0   fail:0   skip:21 
fi-skl-6770hq    total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14 
fi-snb-2520m     total:244  pass:212  dwarn:0   dfail:0   fail:0   skip:32 
fi-snb-2600      total:244  pass:211  dwarn:0   dfail:0   fail:0   skip:33 

74d13d4fab710f664d5eeb15fd3de821a7f46818 drm-intel-nightly: 2016y-11m-09d-15h-02m-46s UTC integration manifest 2af81e6 drm/i915: Trim the object sg table

== Logs ==

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

#6 lore raw

Re: ✗ Fi.CI.BAT: warning for drm/i915: Trim the object sg table (rev2)

Quoted 6 lines · Patchwork

== Series Details ==

Series: drm/i915: Trim the object sg table (rev2)
URL : https://patchwork.freedesktop.org/series/15036/
State : warning

Quoted 7 lines

== Summary ==

Series 15036v2 drm/i915: Trim the object sg table
https://patchwork.freedesktop.org/api/1.0/series/15036/revisions/2/mbox/

Test drv_module_reload_basic:

                pass       -> DMESG-WARN (fi-bsw-n3050)

Unrelated, raised new BZ at https://bugs.freedesktop.org/show_bug.cgi?id=98670.

Quoted 19 lines
fi-bdw-5557u     total:244  pass:229  dwarn:0   dfail:0   fail:0   skip:15
fi-bsw-n3050     total:244  pass:203  dwarn:1   dfail:0   fail:0   skip:40
fi-bxt-t5700     total:244  pass:216  dwarn:0   dfail:0   fail:0   skip:28
fi-byt-j1900     total:244  pass:216  dwarn:0   dfail:0   fail:0   skip:28
fi-byt-n2820     total:244  pass:212  dwarn:0   dfail:0   fail:0   skip:32
fi-hsw-4770      total:244  pass:224  dwarn:0   dfail:0   fail:0   skip:20
fi-hsw-4770r     total:244  pass:224  dwarn:0   dfail:0   fail:0   skip:20
fi-ilk-650       total:244  pass:191  dwarn:0   dfail:0   fail:0   skip:53
fi-ivb-3520m     total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22
fi-ivb-3770      total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22
fi-kbl-7200u     total:244  pass:222  dwarn:0   dfail:0   fail:0   skip:22
fi-skl-6260u     total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14
fi-skl-6700hq    total:244  pass:223  dwarn:0   dfail:0   fail:0   skip:21
fi-skl-6700k     total:244  pass:222  dwarn:1   dfail:0   fail:0   skip:21
fi-skl-6770hq    total:244  pass:230  dwarn:0   dfail:0   fail:0   skip:14
fi-snb-2520m     total:244  pass:212  dwarn:0   dfail:0   fail:0   skip:32
fi-snb-2600      total:244  pass:211  dwarn:0   dfail:0   fail:0   skip:33
Quoted 2 lines

74d13d4fab710f664d5eeb15fd3de821a7f46818 drm-intel-nightly: 2016y-11m-09d-15h-02m-46s UTC integration manifest 2af81e6 drm/i915: Trim the object sg table

Merged to dinq, thanks for the review!

Regards,

Tvrtko

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