Skip to content

Commit 2516035

Browse files
tehcastertorvalds
authored andcommitted
mm, thp: remove __GFP_NORETRY from khugepaged and madvised allocations
After the previous patch, we can distinguish costly allocations that should be really lightweight, such as THP page faults, with __GFP_NORETRY. This means we don't need to recognize khugepaged allocations via PF_KTHREAD anymore. We can also change THP page faults in areas where madvise(MADV_HUGEPAGE) was used to try as hard as khugepaged, as the process has indicated that it benefits from THP's and is willing to pay some initial latency costs. We can also make the flags handling less cryptic by distinguishing GFP_TRANSHUGE_LIGHT (no reclaim at all, default mode in page fault) from GFP_TRANSHUGE (only direct reclaim, khugepaged default). Adding __GFP_NORETRY or __GFP_KSWAPD_RECLAIM is done where needed. The patch effectively changes the current GFP_TRANSHUGE users as follows: * get_huge_zero_page() - the zero page lifetime should be relatively long and it's shared by multiple users, so it's worth spending some effort on it. We use GFP_TRANSHUGE, and __GFP_NORETRY is not added. This also restores direct reclaim to this allocation, which was unintentionally removed by commit e4a49efe4e7e ("mm: thp: set THP defrag by default to madvise and add a stall-free defrag option") * alloc_hugepage_khugepaged_gfpmask() - this is khugepaged, so latency is not an issue. So if khugepaged "defrag" is enabled (the default), do reclaim via GFP_TRANSHUGE without __GFP_NORETRY. We can remove the PF_KTHREAD check from page alloc. As a side-effect, khugepaged will now no longer check if the initial compaction was deferred or contended. This is OK, as khugepaged sleep times between collapsion attempts are long enough to prevent noticeable disruption, so we should allow it to spend some effort. * migrate_misplaced_transhuge_page() - already was masking out __GFP_RECLAIM, so just convert to GFP_TRANSHUGE_LIGHT which is equivalent. * alloc_hugepage_direct_gfpmask() - vma's with VM_HUGEPAGE (via madvise) are now allocating without __GFP_NORETRY. Other vma's keep using __GFP_NORETRY if direct reclaim/compaction is at all allowed (by default it's allowed only for madvised vma's). The rest is conversion to GFP_TRANSHUGE(_LIGHT). [[email protected]: suggested GFP_TRANSHUGE_LIGHT] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Vlastimil Babka <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Mel Gorman <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3eb2771 commit 2516035

File tree

7 files changed

+30
-25
lines changed

7 files changed

+30
-25
lines changed

include/linux/gfp.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ struct vm_area_struct;
237237
* are expected to be movable via page reclaim or page migration. Typically,
238238
* pages on the LRU would also be allocated with GFP_HIGHUSER_MOVABLE.
239239
*
240-
* GFP_TRANSHUGE is used for THP allocations. They are compound allocations
241-
* that will fail quickly if memory is not available and will not wake
242-
* kswapd on failure.
240+
* GFP_TRANSHUGE and GFP_TRANSHUGE_LIGHT are used for THP allocations. They are
241+
* compound allocations that will generally fail quickly if memory is not
242+
* available and will not wake kswapd/kcompactd on failure. The _LIGHT
243+
* version does not attempt reclaim/compaction at all and is by default used
244+
* in page fault path, while the non-light is used by khugepaged.
243245
*/
244246
#define GFP_ATOMIC (__GFP_HIGH|__GFP_ATOMIC|__GFP_KSWAPD_RECLAIM)
245247
#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS)
@@ -254,9 +256,9 @@ struct vm_area_struct;
254256
#define GFP_DMA32 __GFP_DMA32
255257
#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM)
256258
#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)
257-
#define GFP_TRANSHUGE ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
258-
__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN) & \
259-
~__GFP_RECLAIM)
259+
#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
260+
__GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
261+
#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
260262

261263
/* Convert GFP flags to their corresponding migrate type */
262264
#define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)

include/trace/events/mmflags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#define __def_gfpflag_names \
1313
{(unsigned long)GFP_TRANSHUGE, "GFP_TRANSHUGE"}, \
14+
{(unsigned long)GFP_TRANSHUGE_LIGHT, "GFP_TRANSHUGE_LIGHT"}, \
1415
{(unsigned long)GFP_HIGHUSER_MOVABLE, "GFP_HIGHUSER_MOVABLE"},\
1516
{(unsigned long)GFP_HIGHUSER, "GFP_HIGHUSER"}, \
1617
{(unsigned long)GFP_USER, "GFP_USER"}, \

mm/huge_memory.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -539,23 +539,26 @@ static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page,
539539
}
540540

541541
/*
542-
* If THP is set to always then directly reclaim/compact as necessary
543-
* If set to defer then do no reclaim and defer to khugepaged
542+
* If THP defrag is set to always then directly reclaim/compact as necessary
543+
* If set to defer then do only background reclaim/compact and defer to khugepaged
544544
* If set to madvise and the VMA is flagged then directly reclaim/compact
545+
* When direct reclaim/compact is allowed, don't retry except for flagged VMA's
545546
*/
546547
static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
547548
{
548-
gfp_t reclaim_flags = 0;
549-
550-
if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
551-
(vma->vm_flags & VM_HUGEPAGE))
552-
reclaim_flags = __GFP_DIRECT_RECLAIM;
553-
else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
554-
reclaim_flags = __GFP_KSWAPD_RECLAIM;
555-
else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
556-
reclaim_flags = __GFP_DIRECT_RECLAIM;
557-
558-
return GFP_TRANSHUGE | reclaim_flags;
549+
bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
550+
551+
if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
552+
&transparent_hugepage_flags) && vma_madvised)
553+
return GFP_TRANSHUGE;
554+
else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
555+
&transparent_hugepage_flags))
556+
return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
557+
else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
558+
&transparent_hugepage_flags))
559+
return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
560+
561+
return GFP_TRANSHUGE_LIGHT;
559562
}
560563

561564
/* Caller must hold page table lock. */

mm/khugepaged.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static bool khugepaged_scan_abort(int nid)
694694
/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
695695
static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
696696
{
697-
return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
697+
return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
698698
}
699699

700700
#ifdef CONFIG_NUMA

mm/migrate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
19341934
goto out_dropref;
19351935

19361936
new_page = alloc_pages_node(node,
1937-
(GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_RECLAIM,
1937+
(GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
19381938
HPAGE_PMD_ORDER);
19391939
if (!new_page)
19401940
goto out_fail;

mm/page_alloc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,11 +3556,9 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
35563556
/*
35573557
* Looks like reclaim/compaction is worth trying, but
35583558
* sync compaction could be very expensive, so keep
3559-
* using async compaction, unless it's khugepaged
3560-
* trying to collapse.
3559+
* using async compaction.
35613560
*/
3562-
if (!(current->flags & PF_KTHREAD))
3563-
migration_mode = MIGRATE_ASYNC;
3561+
migration_mode = MIGRATE_ASYNC;
35643562
}
35653563
}
35663564

tools/perf/builtin-kmem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ static const struct {
608608
const char *compact;
609609
} gfp_compact_table[] = {
610610
{ "GFP_TRANSHUGE", "THP" },
611+
{ "GFP_TRANSHUGE_LIGHT", "THL" },
611612
{ "GFP_HIGHUSER_MOVABLE", "HUM" },
612613
{ "GFP_HIGHUSER", "HU" },
613614
{ "GFP_USER", "U" },

0 commit comments

Comments
 (0)