Skip to contentSkip to conversation

shmem: shmem_writepage() split unlikely i915 THP

PATCH In review Hugh Dickins 3 participants 5 messages Aug 30, 2020 – Sep 1, 2020

Subsystems (2)
lore
Share & tools
lore

Hugh Dickins

author maintainer
#0 lore raw

PATCH 3/5 shmem: shmem_writepage() split unlikely i915 THP

drivers/gpu/drm/i915/gem/i915_gem_shmem.c contains a shmem_writeback() which calls shmem_writepage() from a shrinker: that usually works well enough; but if /sys/kernel/mm/transparent_hugepage/shmem_enabled has been set to "force" (documented as "Force the huge option on for all - very useful for testing"), shmem_writepage() is surprised to be called with a huge page, and crashes on the VM_BUG_ON_PAGE(PageCompound) (I did not find out where the crash happens when CONFIG_DEBUG_VM is off).

LRU page reclaim always splits the shmem huge page first: I'd prefer not to demand that of i915, so check and split compound in shmem_writepage().

Fixes 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")

---
I've marked this for stable just for the info, but the number of users affected is very probably 1, so please feel free to delete that marking.

 mm/shmem.c |    9 +++++++++
 1 file changed, 9 insertions(+)
mm/shmem.c +9 -1
@@ -1362,7 +1362,15 @@ static int shmem_writepage(struct page *
 	swp_entry_t swap;
 	pgoff_t index;
 
-	VM_BUG_ON_PAGE(PageCompound(page), page);
+	/*
+	 * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "force",
+	 * then drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
+	 * and its shmem_writeback() needs them to be split when swapping.
+	 */
+	if (PageTransCompound(page))
+		if (split_huge_page(page) < 0)
+			goto redirty;
+
 	BUG_ON(!PageLocked(page));
 	mapping = page->mapping;
 	index = page->index;
Reviewed-by on v1 3/5
#1 lore raw
Quoted 9 lines · Hugh Dickins

drivers/gpu/drm/i915/gem/i915_gem_shmem.c contains a shmem_writeback() which calls shmem_writepage() from a shrinker: that usually works well enough; but if /sys/kernel/mm/transparent_hugepage/shmem_enabled has been set to "force" (documented as "Force the huge option on for all - very useful for testing"), shmem_writepage() is surprised to be called with a huge page, and crashes on the VM_BUG_ON_PAGE(PageCompound) (I did not find out where the crash happens when CONFIG_DEBUG_VM is off).

Quoted 6 lines

LRU page reclaim always splits the shmem huge page first: I'd prefer not to demand that of i915, so check and split compound in shmem_writepage().

Fixes 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")
#2 lore raw
In reply to Hugh Dickins
Quoted 27 lines · Hugh Dickins

drivers/gpu/drm/i915/gem/i915_gem_shmem.c contains a shmem_writeback() which calls shmem_writepage() from a shrinker: that usually works well enough; but if /sys/kernel/mm/transparent_hugepage/shmem_enabled has been set to "force" (documented as "Force the huge option on for all - very useful for testing"), shmem_writepage() is surprised to be called with a huge page, and crashes on the VM_BUG_ON_PAGE(PageCompound) (I did not find out where the crash happens when CONFIG_DEBUG_VM is off).

LRU page reclaim always splits the shmem huge page first: I'd prefer not to demand that of i915, so check and split compound in shmem_writepage().

Fixes 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")

---
I've marked this for stable just for the info, but the number of users affected is very probably 1, so please feel free to delete that marking.

 mm/shmem.c |    9 +++++++++
 1 file changed, 9 insertions(+)
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +0 -0
@@ -1362,7 +1362,15 @@ static int shmem_writepage(struct page *
        swp_entry_t swap;
        pgoff_t index;

mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 static int shmem_writepage(struct page * · line 1372

Quoted 10 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +8 -1
 
-       VM_BUG_ON_PAGE(PageCompound(page), page);
+       /*
+        * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "force",
+        * then drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
+        * and its shmem_writeback() needs them to be split when swapping.
+        */
+       if (PageTransCompound(page))
+               if (split_huge_page(page) < 0)
+                       goto redirty;

The change looks good to me. Acked-by: Yang Shi <[email protected]>

Just a nit: it may be better to move the spilte after the !PageLocked assertion? Split needs page locked too.

Quoted 5 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +1 -0
+
        BUG_ON(!PageLocked(page));
        mapping = page->mapping;
        index = page->index;
author maintainer
#3 lore raw
Quoted 29 lines · Yang Shi
Quoted 28 lines · Hugh Dickins

drivers/gpu/drm/i915/gem/i915_gem_shmem.c contains a shmem_writeback() which calls shmem_writepage() from a shrinker: that usually works well enough; but if /sys/kernel/mm/transparent_hugepage/shmem_enabled has been set to "force" (documented as "Force the huge option on for all - very useful for testing"), shmem_writepage() is surprised to be called with a huge page, and crashes on the VM_BUG_ON_PAGE(PageCompound) (I did not find out where the crash happens when CONFIG_DEBUG_VM is off).

LRU page reclaim always splits the shmem huge page first: I'd prefer not to demand that of i915, so check and split compound in shmem_writepage().

Fixes 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")

---
I've marked this for stable just for the info, but the number of users affected is very probably 1, so please feel free to delete that marking.

 mm/shmem.c |    9 +++++++++
 1 file changed, 9 insertions(+)
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +0 -0
@@ -1362,7 +1362,15 @@ static int shmem_writepage(struct page *
        swp_entry_t swap;
        pgoff_t index;
 
Quoted 11 lines

mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 static int shmem_writepage(struct page * · line 1372

Quoted 9 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +8 -1
-       VM_BUG_ON_PAGE(PageCompound(page), page);
+       /*
+        * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "force",
+        * then drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
+        * and its shmem_writeback() needs them to be split when swapping.
+        */
+       if (PageTransCompound(page))
+               if (split_huge_page(page) < 0)
+                       goto redirty;

The change looks good to me. Acked-by: Yang Shi <[email protected]>

Thanks.

Quoted 3 lines

Just a nit: it may be better to move the spilte after the !PageLocked assertion? Split needs page locked too.

I hadn't considered that, but I think it's best left as is:
split_huge_page_to_list() has its own

	VM_BUG_ON_PAGE(!PageLocked(head), head);

to enforce its needs: think of the old BUG_ON(!PageLocked(page)) below as enforcing shmem's needs, checking that split_huge_page() did not unlock it :)

Quoted 6 lines
Quoted 6 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +1 -0
+
        BUG_ON(!PageLocked(page));
        mapping = page->mapping;
        index = page->index;
#4 lore raw
Quoted 49 lines · Hugh Dickins
Quoted 29 lines · Yang Shi
Quoted 28 lines · Hugh Dickins

drivers/gpu/drm/i915/gem/i915_gem_shmem.c contains a shmem_writeback() which calls shmem_writepage() from a shrinker: that usually works well enough; but if /sys/kernel/mm/transparent_hugepage/shmem_enabled has been set to "force" (documented as "Force the huge option on for all - very useful for testing"), shmem_writepage() is surprised to be called with a huge page, and crashes on the VM_BUG_ON_PAGE(PageCompound) (I did not find out where the crash happens when CONFIG_DEBUG_VM is off).

LRU page reclaim always splits the shmem huge page first: I'd prefer not to demand that of i915, so check and split compound in shmem_writepage().

Fixes 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")

---
I've marked this for stable just for the info, but the number of users affected is very probably 1, so please feel free to delete that marking.

 mm/shmem.c |    9 +++++++++
 1 file changed, 9 insertions(+)
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +0 -0
@@ -1362,7 +1362,15 @@ static int shmem_writepage(struct page *
        swp_entry_t swap;
        pgoff_t index;
 
Quoted 11 lines

mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 static int shmem_writepage(struct page * · line 1372

Quoted 9 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +8 -1
-       VM_BUG_ON_PAGE(PageCompound(page), page);
+       /*
+        * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "force",
+        * then drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages,
+        * and its shmem_writeback() needs them to be split when swapping.
+        */
+       if (PageTransCompound(page))
+               if (split_huge_page(page) < 0)
+                       goto redirty;

The change looks good to me. Acked-by: Yang Shi <[email protected]>

Thanks.

Quoted 3 lines

Just a nit: it may be better to move the spilte after the !PageLocked assertion? Split needs page locked too.

Quoted 6 lines

I hadn't considered that, but I think it's best left as is:
split_huge_page_to_list() has its own

        VM_BUG_ON_PAGE(!PageLocked(head), head);

to enforce its needs: think of the old BUG_ON(!PageLocked(page)) below as enforcing shmem's needs, checking that split_huge_page() did not unlock it :)

Yes, it is definitely fine to keep it as is. I just thought we could bailout earlier if the page is not locked.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
mm/shmem.c 2020-08-28 17:35:08.326024349 -0700 renamed from mm/shmem.c 2020-08-16 17:32:50.693507198 -0700 +1 -0
+
        BUG_ON(!PageLocked(page));
        mapping = page->mapping;
        index = page->index;