Skip to contentSkip to conversation

drm/gem: Mark pinned pages as unevictable

PATCH Superseded Kuo-Hsin Yang 2 participants 10 messages Dec 14, 2018 – Dec 18, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/gem: Mark pinned pages as unevictable

The gem drivers use shmemfs to allocate backing storage for gem objects. These shmemfs pages can be pinned by shmem_read_mapping_page_gfp(). Mark these pinned pages as unevictable to speed up vmscan. See also similar patch on i915 driver [1].

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
---
 drivers/gpu/drm/drm_gem.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +33 -3
index 8b55ece97967f..2896ff60552f5 100644
@@ -37,6 +37,7 @@
 #include <linux/shmem_fs.h>
 #include <linux/dma-buf.h>
 #include <linux/mem_encrypt.h>
+#include <linux/pagevec.h>
 #include <drm/drmP.h>
 #include <drm/drm_vma_manager.h>
 #include <drm/drm_gem.h>
@@ -526,6 +527,17 @@ int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
 }
 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
 
+/*
+ * Move pages to appropriate lru and release the pagevec, decrementing the
+ * ref count of those pages.
+ */
+static void drm_gem_check_release_pagevec(struct pagevec *pvec)
+{
+	check_move_unevictable_pages(pvec);
+	__pagevec_release(pvec);
+	cond_resched();
+}
+
 /**
  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  * from shmem
@@ -551,6 +563,7 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 {
 	struct address_space *mapping;
 	struct page *p, **pages;
+	struct pagevec pvec;
 	int i, npages;
 
 	/* This is the shared memory object that backs the GEM resource */
@@ -568,6 +581,8 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 	if (pages == NULL)
 		return ERR_PTR(-ENOMEM);
 
+	mapping_set_unevictable(mapping);
+
 	for (i = 0; i < npages; i++) {
 		p = shmem_read_mapping_page(mapping, i);
 		if (IS_ERR(p))
@@ -586,8 +601,14 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 	return pages;
 
 fail:
-	while (i--)
-		put_page(pages[i]);
+	mapping_clear_unevictable(mapping);
+	pagevec_init(&pvec);
+	while (i--) {
+		if (!pagevec_add(&pvec, pages[i]))
+			drm_gem_check_release_pagevec(&pvec);
+	}
+	if (pagevec_count(&pvec))
+		drm_gem_check_release_pagevec(&pvec);
 
 	kvfree(pages);
 	return ERR_CAST(p);
@@ -605,6 +626,11 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 		bool dirty, bool accessed)
 {
 	int i, npages;
+	struct address_space *mapping;
+	struct pagevec pvec;
+
+	mapping = file_inode(obj->filp)->i_mapping;
+	mapping_clear_unevictable(mapping);
 
 	/* We already BUG_ON() for non-page-aligned sizes in
 	 * drm_gem_object_init(), so we should never hit this unless
@@ -614,6 +640,7 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 
 	npages = obj->size >> PAGE_SHIFT;
 
+	pagevec_init(&pvec);
 	for (i = 0; i < npages; i++) {
 		if (dirty)
 			set_page_dirty(pages[i]);
@@ -622,8 +649,11 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 			mark_page_accessed(pages[i]);
 
 		/* Undo the reference we took when populating the table */
-		put_page(pages[i]);
+		if (!pagevec_add(&pvec, pages[i]))
+			drm_gem_check_release_pagevec(&pvec);
 	}
+	if (pagevec_count(&pvec))
+		drm_gem_check_release_pagevec(&pvec);
 
 	kvfree(pages);
 }
Signature · 6 lines
2.20.0.405.gbc1bbc6f85-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Quoting Kuo-Hsin Yang (2018-12-14 08:41:35)

Quoted 4 lines

The gem drivers use shmemfs to allocate backing storage for gem objects. These shmemfs pages can be pinned by shmem_read_mapping_page_gfp(). Mark these pinned pages as unevictable to speed up vmscan. See also similar patch on i915 driver [1].

One difference to contemplate is that i915 does at least attempt to invalidate the pinning under mempressure (via the shrinker) and release the pages back to the system. Yes, the same argument about the ineffectiveness of use the evictable LRU while the pages are pinned by GEM is there, but in general we lack the alternative LRU and a means of tracking how much memory is held.

Do you have a driver in mind (msm?) to demonstrate the use case?

Quoted 1 line

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Note that this first requires i915 merged into drm-misc-next. -Chris
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 3 lines · Chris Wilson

Do you have a driver in mind (msm?) to demonstrate the use case?

On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages()/drm_gem_get_pages() to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

See also the chromebook bug: https://crbug.com/896805#c15 .

Quoted 4 lines
Quoted 2 lines

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Note that this first requires i915 merged into drm-misc-next.

The i915 patch has been merged to drm-misc-next.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Quoting Kuo-Hsin Yang (2018-12-14 09:33:19)

Quoted 8 lines · Chris Wilson
Quoted 3 lines

Do you have a driver in mind (msm?) to demonstrate the use case?

On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages()/drm_gem_get_pages() to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

Hmm, but this doesn't change the unavailability of the pages, just how much work the shrinker must do scanning the evictable lists containing GEM pages to no avail. So it won't prevent an oom by itself because rockchip will still be holding the pages.

Or?
-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 11 lines · Chris Wilson

Quoting Kuo-Hsin Yang (2018-12-14 09:33:19)

Quoted 8 lines · Chris Wilson
Quoted 3 lines

Do you have a driver in mind (msm?) to demonstrate the use case?

On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages()/drm_gem_get_pages() to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

Quoted 6 lines

Hmm, but this doesn't change the unavailability of the pages, just how much work the shrinker must do scanning the evictable lists containing GEM pages to no avail. So it won't prevent an oom by itself because rockchip will still be holding the pages.

Or?

When the size of a zone is 4GB, the inactive_ratio is 5. If all pages in the inactive_anon are pinned and active_anon / inactive_anon < 5, page reclaim would only scan inactive_anon due to inactive_ratio. It breaks page reclaim when the rockchip driver only pins about 1/6 of the anon lru pages. If the pinned pages are marked unevictable, the oom-killer would be invoked when anon lru is close to 0.

An example dmesg with oom-killer invocation shows there are still a lot of anon lru pages:

[  516.170949] TaskSchedulerFo invoked oom-killer: gfp_mask=0x24200ca,
order=0, oom_score_adj=300
...
[  516.211085] Mem-Info:
[  516.211116] DMA free:48816kB min:45056kB low:56320kB high:67584kB

active_anon:1453484kB inactive_anon:309284kB active_file:208808kB inactive_file:177308kB unevictable:0kB isolated(anon):3200kB isolated(file):0kB present:4059136kB managed:3904852kB mlocked:0kB dirty:0kB writeback:0kB mapped:245000kB shmem:323300kB slab_reclaimable:43540kB slab_unreclaimable:87896kB kernel_stack:31520kB pagetables:59440kB unstable:0kB bounce:0kB free_pcp:588kB local_pcp:0kB free_cma:0kB writeback_tmp:0kB pages_scanned:10977908 all_unreclaimable? yes
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Quoting Kuo-Hsin Yang (2018-12-14 10:09:11)

Quoted 17 lines · Chris Wilson
Quoted 16 lines

Quoting Kuo-Hsin Yang (2018-12-14 09:33:19)

Quoted 8 lines · Chris Wilson
Quoted 3 lines

Do you have a driver in mind (msm?) to demonstrate the use case?

On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages()/drm_gem_get_pages() to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

Hmm, but this doesn't change the unavailability of the pages, just how much work the shrinker must do scanning the evictable lists containing GEM pages to no avail. So it won't prevent an oom by itself because rockchip will still be holding the pages.

Quoted 8 lines
Quoted 1 line

Or?

When the size of a zone is 4GB, the inactive_ratio is 5. If all pages in the inactive_anon are pinned and active_anon / inactive_anon < 5, page reclaim would only scan inactive_anon due to inactive_ratio. It breaks page reclaim when the rockchip driver only pins about 1/6 of the anon lru pages. If the pinned pages are marked unevictable, the oom-killer would be invoked when anon lru is close to 0.

Ta, I did not know of that relationship. Perfect details for the changelog to explain how this does improve page reclaim even in the absence of a GEM shrinker. :)
-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

PATCH v2 1/1 drm/gem: Mark pinned pages as unevictable

In reply to Kuo-Hsin Yang

The gem drivers use shmemfs to allocate backing storage for gem objects. On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages -> drm_gem_get_pages -> shmem_read_mapping_page to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

E.g. when the size of a zone is 3.9 GiB, the inactive_ratio is 5. If active_anon / inactive_anon < 5 and all pages in the inactive_anon lru are pinned, page reclaim would keep scanning inactive_anon lru without reclaiming memory. It breaks page reclaim when the rockchip driver only pins about 1/6 of the anon lru pages.

Mark these pinned pages as unevictable to avoid the premature oom-killer invocation. See also similar patch on i915 driver [1].

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>
---
 drivers/gpu/drm/drm_gem.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +33 -3
index 8b55ece97967f..2896ff60552f5 100644
@@ -37,6 +37,7 @@
 #include <linux/shmem_fs.h>
 #include <linux/dma-buf.h>
 #include <linux/mem_encrypt.h>
+#include <linux/pagevec.h>
 #include <drm/drmP.h>
 #include <drm/drm_vma_manager.h>
 #include <drm/drm_gem.h>
@@ -526,6 +527,17 @@ int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
 }
 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
 
+/*
+ * Move pages to appropriate lru and release the pagevec, decrementing the
+ * ref count of those pages.
+ */
+static void drm_gem_check_release_pagevec(struct pagevec *pvec)
+{
+	check_move_unevictable_pages(pvec);
+	__pagevec_release(pvec);
+	cond_resched();
+}
+
 /**
  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  * from shmem
@@ -551,6 +563,7 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 {
 	struct address_space *mapping;
 	struct page *p, **pages;
+	struct pagevec pvec;
 	int i, npages;
 
 	/* This is the shared memory object that backs the GEM resource */
@@ -568,6 +581,8 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 	if (pages == NULL)
 		return ERR_PTR(-ENOMEM);
 
+	mapping_set_unevictable(mapping);
+
 	for (i = 0; i < npages; i++) {
 		p = shmem_read_mapping_page(mapping, i);
 		if (IS_ERR(p))
@@ -586,8 +601,14 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
 	return pages;
 
 fail:
-	while (i--)
-		put_page(pages[i]);
+	mapping_clear_unevictable(mapping);
+	pagevec_init(&pvec);
+	while (i--) {
+		if (!pagevec_add(&pvec, pages[i]))
+			drm_gem_check_release_pagevec(&pvec);
+	}
+	if (pagevec_count(&pvec))
+		drm_gem_check_release_pagevec(&pvec);
 
 	kvfree(pages);
 	return ERR_CAST(p);
@@ -605,6 +626,11 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 		bool dirty, bool accessed)
 {
 	int i, npages;
+	struct address_space *mapping;
+	struct pagevec pvec;
+
+	mapping = file_inode(obj->filp)->i_mapping;
+	mapping_clear_unevictable(mapping);
 
 	/* We already BUG_ON() for non-page-aligned sizes in
 	 * drm_gem_object_init(), so we should never hit this unless
@@ -614,6 +640,7 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 
 	npages = obj->size >> PAGE_SHIFT;
 
+	pagevec_init(&pvec);
 	for (i = 0; i < npages; i++) {
 		if (dirty)
 			set_page_dirty(pages[i]);
@@ -622,8 +649,11 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 			mark_page_accessed(pages[i]);
 
 		/* Undo the reference we took when populating the table */
-		put_page(pages[i]);
+		if (!pagevec_add(&pvec, pages[i]))
+			drm_gem_check_release_pagevec(&pvec);
 	}
+	if (pagevec_count(&pvec))
+		drm_gem_check_release_pagevec(&pvec);
 
 	kvfree(pages);
 }
Signature · 6 lines
2.20.0.405.gbc1bbc6f85-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v2 1/1
#8 lore raw

Quoting Kuo-Hsin Yang (2018-12-17 09:04:01)

Quoted 11 lines

The gem drivers use shmemfs to allocate backing storage for gem objects. On Samsung Chromebook Plus, the drm/rockchip driver may call rockchip_gem_get_pages -> drm_gem_get_pages -> shmem_read_mapping_page to pin a lot of pages, breaking the page reclaim mechanism and causing oom-killer invocation.

E.g. when the size of a zone is 3.9 GiB, the inactive_ratio is 5. If active_anon / inactive_anon < 5 and all pages in the inactive_anon lru are pinned, page reclaim would keep scanning inactive_anon lru without reclaiming memory. It breaks page reclaim when the rockchip driver only pins about 1/6 of the anon lru pages.

Right, we invalidate the "inactive anon list should be small enough that the VM never has to do too much work" assumption.

Quoted 8 lines

Mark these pinned pages as unevictable to avoid the premature oom-killer invocation. See also similar patch on i915 driver [1].

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>

-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw
Quoted 15 lines · Chris Wilson

Quoting Kuo-Hsin Yang (2018-12-17 09:04:01)

Quoted 6 lines

E.g. when the size of a zone is 3.9 GiB, the inactive_ratio is 5. If active_anon / inactive_anon < 5 and all pages in the inactive_anon lru are pinned, page reclaim would keep scanning inactive_anon lru without reclaiming memory. It breaks page reclaim when the rockchip driver only pins about 1/6 of the anon lru pages.

Right, we invalidate the "inactive anon list should be small enough that the VM never has to do too much work" assumption.

Quoted 3 lines

Mark these pinned pages as unevictable to avoid the premature oom-killer invocation. See also similar patch on i915 driver [1].

Quoted 6 lines
Quoted 5 lines

[1]: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Cc Daniel Vetter <[email protected]>
Cc Chris Wilson <[email protected]>

Thanks for your review.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel