Skip to contentSkip to conversation

mm,thp,shmem: limit shmem THP alloc gfp_mask

PATCH Superseded Rik van Riel 4 participants 10 messages Oct 22, 2020 – Oct 23, 2020

Subsystems (5)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm,thp,shmem: limit shmem THP alloc gfp_mask

The allocation flags of anonymous transparent huge pages can be controlled through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can help the system from getting bogged down in the page reclaim and compaction code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those configuration settings, and some workloads ended up with all CPUs stuck on the LRU lock in the page reclaim code, trying to allocate dozens of THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem hugepage allocations, to prevent that from happening.

This way a THP defrag setting of "never" or "defer+madvise" will result in quick allocation failures without direct reclaim when no 2MB free pages are available.

---

include/linux/gfp.h +2 -0
index c603237e006c..0a5b164a26d9 100644
@@ -614,6 +614,8 @@ bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
 extern void pm_restrict_gfp_mask(void);
 extern void pm_restore_gfp_mask(void);
 
+extern gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma);
+
 #ifdef CONFIG_PM_SLEEP
 extern bool pm_suspended_storage(void);
 #else
mm/huge_memory.c +1 -1
index 9474dbc150ed..9b08ce5cc387 100644
@@ -649,7 +649,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
  *	    available
  * never: never stall for any thp allocation
  */
-static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
+gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
 {
 	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
mm/shmem.c +5 -2
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
+			       true);
 	shmem_pseudo_vma_destroy(&pvma);
 	if (page)
 		prep_transhuge_page(page);
Signature · 1 line
All rights reversed.
reviewer
#1 lore raw
Quoted 10 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can help the system from getting bogged down in the page reclaim and compaction code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those configuration settings, and some workloads ended up with all CPUs stuck on the LRU lock in the page reclaim code, trying to allocate dozens of THPs simultaneously.

Quoted 6 lines

This patch applies the same configurated limitation of THPs to shmem hugepage allocations, to prevent that from happening.

This way a THP defrag setting of "never" or "defer+madvise" will result in quick allocation failures without direct reclaim when no 2MB free pages are available.

I remmeber I wanted to unify this in the past as well. The patch got reverted in the end. It was a long story and I do not have time to read through lengthy discussions again. I do remember though that there were some objections pointing out that shmem has its own behavior which is controlled by the mount option - at least for the explicitly mounted shmems. I might misremember.

[...]

mm/shmem.c static struct page *shmem_alloc_hugepage(gfp_t gfp, · line 1549

Quoted 12 lines
mm/shmem.c +2 -2
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;

What is the reason for these when alloc_hugepage_direct_gfpmask provides the full mask?

Quoted 9 lines
mm/shmem.c +3 -0
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
+			       true);
 	shmem_pseudo_vma_destroy(&pvma);
 	if (page)
 		prep_transhuge_page(page);
Signature · 2 lines
-- 
All rights reversed.
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 27 lines · Michal Hocko
Quoted 20 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled
through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can
help the system from getting bogged down in the page reclaim and compaction
code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those
configuration settings, and some workloads ended up with all CPUs stuck
on the LRU lock in the page reclaim code, trying to allocate dozens of
THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem
hugepage allocations, to prevent that from happening.

Quoted 5 lines

This way a THP defrag setting of "never" or "defer+madvise" will result
in quick allocation failures without direct reclaim when no 2MB free
pages are available.

Quoted 8 lines

I remmeber I wanted to unify this in the past as well. The patch got reverted in the end. It was a long story and I do not have time to read
through lengthy discussions again. I do remember though that there were
some objections pointing out that shmem has its own behavior which is controlled by the mount option - at least for the explicitly mounted shmems. I might misremember.

That is not entirely true, though.

THP has two main sysfs knobs: "enabled" and "defrag"

The mount options
control the shmem equivalent options
for "enabled", but they do not do anything for the "defrag" equivalent options.

This patch applies the "defrag" THP options to
shmem.

Quoted 10 lines

[...]

Quoted 8 lines
mm/shmem.c +0 -0
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page
 *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
Quoted 11 lines

mm/shmem.c static struct page · line 1552

Quoted 7 lines
mm/shmem.c +2 -2
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY |
 __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
 true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;

What is the reason for these when alloc_hugepage_direct_gfpmask provides
the full mask?

The mapping_gfp_mask for the shmem file might have additional restrictions above and beyond the gfp mask returned by alloc_hugepage_direct_gfpmask, and I am not sure we should just ignore the mapping_gfp_mask.

That is why this patch takes the union of both gfp masks.

However, digging into things more, it looks like shmem inodes always have the mapping gfp mask set to GFP_HIGHUSER_MOVABLE, and it is never changed, so simply using the output from alloc_hugepage_direct_gfpmask should be fine.

I can do the patch either way. Just let me know what you prefer.

Quoted 10 lines
Quoted 10 lines
mm/shmem.c +2 -0
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0,

numa_node_id(),

mm/shmem.c +1 -0
+			       true);
 	shmem_pseudo_vma_destroy(&pvma);
 	if (page)
 		prep_transhuge_page(page);
Signature · 2 lines
-- 
All rights reversed.
Signature · 1 line
All Rights Reversed.
reviewer
#3 lore raw
Quoted 37 lines · Rik van Riel
Quoted 27 lines · Michal Hocko
Quoted 20 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled
through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can
help the system from getting bogged down in the page reclaim and compaction
code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those
configuration settings, and some workloads ended up with all CPUs stuck
on the LRU lock in the page reclaim code, trying to allocate dozens of
THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem
hugepage allocations, to prevent that from happening.

Quoted 5 lines

This way a THP defrag setting of "never" or "defer+madvise" will result
in quick allocation failures without direct reclaim when no 2MB free
pages are available.

Quoted 8 lines

I remmeber I wanted to unify this in the past as well. The patch got reverted in the end. It was a long story and I do not have time to read
through lengthy discussions again. I do remember though that there were
some objections pointing out that shmem has its own behavior which is controlled by the mount option - at least for the explicitly mounted shmems. I might misremember.

Quoted 8 lines

That is not entirely true, though.

THP has two main sysfs knobs: "enabled" and "defrag"

The mount options
control the shmem equivalent options
for "enabled", but they do not do anything for the "defrag" equivalent options.

Yeah, the situation is quite messy :/

Quoted 2 lines

This patch applies the "defrag" THP options to
shmem.

I am not really objecting I just do remember some pushback. My previous attempt was to unify everything inside alloc_pages_vma IIRC.

Quoted 22 lines
Quoted 10 lines

[...]

Quoted 8 lines
mm/shmem.c +0 -0
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page
 *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
Quoted 11 lines

mm/shmem.c static struct page · line 1552

Quoted 7 lines
mm/shmem.c +2 -2
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY |
 __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
 true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;

What is the reason for these when alloc_hugepage_direct_gfpmask provides
the full mask?

Quoted 4 lines

The mapping_gfp_mask for the shmem file might have additional restrictions above and beyond the gfp mask returned by alloc_hugepage_direct_gfpmask, and I am not sure we should just ignore the mapping_gfp_mask.

No, we shouldn't. But I do not see why you should be adding the above set of flags on top.

Quoted 8 lines

That is why this patch takes the union of both gfp masks.

However, digging into things more, it looks like shmem inodes always have the mapping gfp mask set to GFP_HIGHUSER_MOVABLE, and it is never changed, so simply using the output from alloc_hugepage_direct_gfpmask should be fine.

I can do the patch either way. Just let me know what you prefer.

I would just and the given gfp with alloc_hugepage_direct_gfpmask

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 27 lines · Michal Hocko
Quoted 26 lines · Rik van Riel
Quoted 24 lines · Michal Hocko
Quoted 18 lines · Rik van Riel
mm/shmem.c +0 -1
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page
 *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY

|
__GFP_NOWARN,

mm/shmem.c +0 -1
-			HPAGE_PMD_ORDER, &pvma, 0,

numa_node_id(),
true);

mm/shmem.c +1 -0
+	/* Limit the gfp mask according to THP configuration.

*/

mm/shmem.c static struct page *shmem_alloc_hugepage(gfp_t gfp, · line 1549

Quoted 1 line
mm/shmem.c +1 -0
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;

What is the reason for these when alloc_hugepage_direct_gfpmask provides
the full mask?

Quoted 7 lines
Quoted 4 lines

The mapping_gfp_mask for the shmem file might have additional restrictions above and beyond the gfp mask returned by alloc_hugepage_direct_gfpmask, and I am not sure we should just ignore the mapping_gfp_mask.

No, we shouldn't. But I do not see why you should be adding the above set of flags on top.

Because THP allocations are higher order and optimistic, and we want them to:
1) be annotated as compound allocations
2) fail (and fall back to 4kB allocations) when they cannot be easily satisfied, and
3) not create a spew of allocation failure backtraces on the (serial) console when these THP allocations fail

Quoted 10 lines
Quoted 8 lines

That is why this patch takes the union of both gfp masks.

However, digging into things more, it looks like shmem inodes always have the mapping gfp mask set to GFP_HIGHUSER_MOVABLE, and it is never changed, so simply using the output from alloc_hugepage_direct_gfpmask should be fine.

I can do the patch either way. Just let me know what you prefer.

I would just and the given gfp with alloc_hugepage_direct_gfpmask

That would miss the three things we definitely want from above.

Signature · 1 line
All Rights Reversed.
reviewer
#5 lore raw
Quoted 36 lines · Rik van Riel
Quoted 27 lines · Michal Hocko
Quoted 26 lines · Rik van Riel
Quoted 24 lines · Michal Hocko
Quoted 18 lines · Rik van Riel
mm/shmem.c +0 -1
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page
 *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY

|
__GFP_NOWARN,

mm/shmem.c +0 -1
-			HPAGE_PMD_ORDER, &pvma, 0,

numa_node_id(),
true);

mm/shmem.c +1 -0
+	/* Limit the gfp mask according to THP configuration.

*/

mm/shmem.c static struct page *shmem_alloc_hugepage(gfp_t gfp, · line 1549

Quoted 1 line
mm/shmem.c +1 -0
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;

What is the reason for these when alloc_hugepage_direct_gfpmask provides
the full mask?

Quoted 7 lines
Quoted 4 lines

The mapping_gfp_mask for the shmem file might have additional restrictions above and beyond the gfp mask returned by alloc_hugepage_direct_gfpmask, and I am not sure we should just ignore the mapping_gfp_mask.

No, we shouldn't. But I do not see why you should be adding the above set of flags on top.

Quoted 7 lines

Because THP allocations are higher order and optimistic, and we want them to:
1) be annotated as compound allocations
2) fail (and fall back to 4kB allocations) when they cannot be easily satisfied, and
3) not create a spew of allocation failure backtraces on the (serial) console when these THP allocations fail

This all is already returned from alloc_hugepage_direct_gfpmask.

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw
In reply to Rik van Riel
Quoted 10 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can help the system from getting bogged down in the page reclaim and compaction code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those configuration settings, and some workloads ended up with all CPUs stuck on the LRU lock in the page reclaim code, trying to allocate dozens of THPs simultaneously.

Quoted 8 lines

This patch applies the same configurated limitation of THPs to shmem hugepage allocations, to prevent that from happening.

This way a THP defrag setting of "never" or "defer+madvise" will result in quick allocation failures without direct reclaim when no 2MB free pages are available.

FTR, a patch to the same effect was sent by Xu Yu:

https://lore.kernel.org/r/11e1ead211eb7d141efa0eb75a46ee2096ee63f8.1603267572.git.xuyu@linux.alibaba.com

Quoted 47 lines

---

include/linux/gfp.h +2 -0
index c603237e006c..0a5b164a26d9 100644
@@ -614,6 +614,8 @@ bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
  extern void pm_restrict_gfp_mask(void);
  extern void pm_restore_gfp_mask(void);
 
+extern gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma);
+
  #ifdef CONFIG_PM_SLEEP
  extern bool pm_suspended_storage(void);
  #else
mm/huge_memory.c +1 -1
index 9474dbc150ed..9b08ce5cc387 100644
@@ -649,7 +649,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
   *	    available
   * never: never stall for any thp allocation
   */
-static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
+gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  {
  	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
mm/shmem.c +5 -2
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
  		return NULL;
 
  	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
+			       true);
  	shmem_pseudo_vma_destroy(&pvma);
  	if (page)
  		prep_transhuge_page(page);
#7 lore raw
Quoted 14 lines · Vlastimil Babka
Quoted 13 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can help the system from getting bogged down in the page reclaim and compaction code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those configuration settings, and some workloads ended up with all CPUs stuck on the LRU lock in the page reclaim code, trying to allocate dozens of THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem hugepage allocations, to prevent that from happening.

Quoted 7 lines
Quoted 5 lines

This way a THP defrag setting of "never" or "defer+madvise" will result in quick allocation failures without direct reclaim when no 2MB free pages are available.

FTR, a patch to the same effect was sent by Xu Yu:

Hm thought I did CC, but TB ate it. sorry for the noise

Quoted 50 lines

https://lore.kernel.org/r/11e1ead211eb7d141efa0eb75a46ee2096ee63f8.1603267572.git.xuyu@linux.alibaba.com

Quoted 47 lines

---

include/linux/gfp.h +2 -0
index c603237e006c..0a5b164a26d9 100644
@@ -614,6 +614,8 @@ bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
  extern void pm_restrict_gfp_mask(void);
  extern void pm_restore_gfp_mask(void);
 
+extern gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma);
+
  #ifdef CONFIG_PM_SLEEP
  extern bool pm_suspended_storage(void);
  #else
mm/huge_memory.c +1 -1
index 9474dbc150ed..9b08ce5cc387 100644
@@ -649,7 +649,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
   *	    available
   * never: never stall for any thp allocation
   */
-static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
+gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  {
  	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
mm/shmem.c +5 -2
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
  		return NULL;
 
  	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
+			       true);
  	shmem_pseudo_vma_destroy(&pvma);
  	if (page)
  		prep_transhuge_page(page);
#8 lore raw
In reply to Rik van Riel
Quoted 51 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can help the system from getting bogged down in the page reclaim and compaction code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those configuration settings, and some workloads ended up with all CPUs stuck on the LRU lock in the page reclaim code, trying to allocate dozens of THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem hugepage allocations, to prevent that from happening.

This way a THP defrag setting of "never" or "defer+madvise" will result in quick allocation failures without direct reclaim when no 2MB free pages are available.

---

include/linux/gfp.h +2 -0
index c603237e006c..0a5b164a26d9 100644
@@ -614,6 +614,8 @@ bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
  extern void pm_restrict_gfp_mask(void);
  extern void pm_restore_gfp_mask(void);
 
+extern gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma);
+
  #ifdef CONFIG_PM_SLEEP
  extern bool pm_suspended_storage(void);
  #else
mm/huge_memory.c +1 -1
index 9474dbc150ed..9b08ce5cc387 100644
@@ -649,7 +649,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
   *	    available
   * never: never stall for any thp allocation
   */
-static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
+gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  {
  	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
mm/shmem.c +0 -0
index 537c137698f8..d1290eb508e5 100644

mm/shmem.c static struct page *shmem_alloc_hugepage(gfp_t gfp, · line 1550

Quoted 8 lines
mm/shmem.c +3 -2
@@ -1545,8 +1545,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
  		return NULL;
 
  	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);

It is fine to reuse alloc_hugepage_direct_gfpmask, but pvma.vm_flags & VM_HUGEPAGE is always false here, thus, vma_madvised in alloc_hugepage_direct_gfpmask will always be false.

That is why I chose to do the gfp_mask fixup in shmem_getpage_gfp, using sgp_huge to indicate vma_madvised, although with some silly mistakes pointed out by you, in another mail thread.

It will be better if vma_madvised is well handled in your solution.

Quoted 6 lines
mm/shmem.c +2 -0
+	page = alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
+			       true);
  	shmem_pseudo_vma_destroy(&pvma);
  	if (page)
  		prep_transhuge_page(page);
Signature · 2 lines
Thanks,
Yu
#9 lore raw
Quoted 81 lines · Yu Xu
Quoted 64 lines · Rik van Riel

The allocation flags of anonymous transparent huge pages can be controlled
through the files in /sys/kernel/mm/transparent_hugepage/defrag, which can
help the system from getting bogged down in the page reclaim and compaction
code when many THPs are getting allocated simultaneously.

However, the gfp_mask for shmem THP allocations were not limited by those
configuration settings, and some workloads ended up with all CPUs stuck
on the LRU lock in the page reclaim code, trying to allocate dozens of
THPs simultaneously.

This patch applies the same configurated limitation of THPs to shmem
hugepage allocations, to prevent that from happening.

This way a THP defrag setting of "never" or "defer+madvise" will result
in quick allocation failures without direct reclaim when no 2MB free
pages are available.

---

include/linux/gfp.h +2 -0
index c603237e006c..0a5b164a26d9 100644
@@ -614,6 +614,8 @@ bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
  extern void pm_restrict_gfp_mask(void);
  extern void pm_restore_gfp_mask(void);
 
+extern gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct
 *vma);
+
  #ifdef CONFIG_PM_SLEEP
  extern bool pm_suspended_storage(void);
  #else
mm/huge_memory.c +1 -1
index 9474dbc150ed..9b08ce5cc387 100644
@@ -649,7 +649,7 @@ static vm_fault_t
 __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
   *	    available
   * never: never stall for any thp allocation
   */
-static inline gfp_t alloc_hugepage_direct_gfpmask(struct
 vm_area_struct *vma)
+gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  {
  	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
mm/shmem.c +0 -0
index 537c137698f8..d1290eb508e5 100644
@@ -1545,8 +1545,11 @@ static struct page
 *shmem_alloc_hugepage(gfp_t gfp,

mm/shmem.c static struct page · line 1553

Quoted 10 lines
mm/shmem.c +3 -2
  		return NULL;
 
  	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY |
 __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(),
 true);
+	/* Limit the gfp mask according to THP configuration. */
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	gfp &= alloc_hugepage_direct_gfpmask(&pvma);

It is fine to reuse alloc_hugepage_direct_gfpmask, but pvma.vm_flags & VM_HUGEPAGE is always false here, thus, vma_madvised in alloc_hugepage_direct_gfpmask will always be false.

Quoted 5 lines

That is why I chose to do the gfp_mask fixup in shmem_getpage_gfp, using sgp_huge to indicate vma_madvised, although with some silly mistakes pointed out by you, in another mail thread.

It will be better if vma_madvised is well handled in your solution.

OK, let me send a v2 that does that!

By just passing a correct gfp_mask to shmem_alloc_and_acct_page we can also avoid the gfp gymnastics in shmem_alloc_hugepage that Michal rightfully objected to.

Signature · 1 line
All Rights Reversed.