Skip to contentSkip to conversation

reduce THP fault thrashing

RFC In review Vlastimil Babka 2 participants 9 messages Dec 11, 2018 – Jan 10, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover reduce THP fault thrashing

Hi,

this is my attempt at reducing the madvised THP fault local node thrashing by reclaim+compaction attempts which Andrea reported, by trying to better utilize recent compaction results. It doesn't introduce any new __GFP_ONLY_COMPACT flag or add order-specific decisions like Andrea's and David's previous patches, but it does add __GFP_NORETRY back to madvised THP faults, like they both did (Patch 1). Patch 2 is based on another Andrea's suggestion, where any compaction failure is a reason to not try further (not just defered compaction). Finally, patch 3 introduces defered compaction tracking for async mode which is what's used for THP faults. Details in respective patch changelogs.

I haven't tested it yet besides running transhuge-stress and verifying via tracepoints that defered async compaction does happen. I hope all interested parties can test the series on their workloads, thanks in advance. I expect that THP fault success rates will be worse, but hopefully it will also fix the local node thrashing issue. The success rates can then likely be improved by making compaction core smarter, but that's a separate topic.

The series is based on v4.20-rc6.

Vlastimil

Vlastimil Babka (3):
mm, thp: restore __GFP_NORETRY for madvised thp fault allocations mm, page_alloc: reclaim for __GFP_NORETRY costly requests only when
compaction was skipped
mm, compaction: introduce deferred async compaction

 include/linux/compaction.h        | 10 ++--
 include/linux/mmzone.h            |  6 +--
 include/trace/events/compaction.h | 29 ++++++-----
 mm/compaction.c                   | 80 ++++++++++++++++++-------------
 mm/huge_memory.c                  | 13 +++--
 mm/page_alloc.c                   | 14 +++---
 6 files changed, 84 insertions(+), 68 deletions(-)
Signature · 1 line
2.19.2
#1 lore raw

RFC 1/3 mm, thp: restore __GFP_NORETRY for madvised thp fault allocations

Commit 2516035499b9 ("mm, thp: remove __GFP_NORETRY from khugepaged and madvised allocations") intended to make THP faults in MADV_HUGEPAGE areas more successful for processes that indicate that they are willing to pay a higher initial setup cost for long-term THP benefits. In the current page allocator implementation this means that the allocations will try to use reclaim and the more costly sync compaction mode, in case the initial direct async compaction fails.

However, THP faults also include __GFP_THISNODE, which, combined with direct reclaim, can result in a node-reclaim-like local node thrashing behavior, as reported by Andrea [1].

While this patch is not a full fix, the first step is to restore __GFP_NORETRY for madvised THP faults. The expected downside are potentially worse THP fault success rates for the madvised areas, which will have to then rely more on khugepaged. For khugepaged, __GFP_NORETRY is not restored, as its activity should be limited enough by sleeping to cause noticeable thrashing.

Note that alloc_new_node_page() and new_page() is probably another candidate as they handle the migrate_pages(2), resp. mbind(2) syscall, which can thus allow unprivileged node-reclaim-like behavior.

The patch also updates the comments in alloc_hugepage_direct_gfpmask() because elsewhere compaction during page fault is called direct compaction, and 'synchronous' refers to the migration mode, which is not used for THP faults.

[1] https://lkml.kernel.org/m/[email protected]

Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/huge_memory.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/huge_memory.c +6 -7
index 5da55b38b1b7..c442b12b060c 100644
@@ -633,24 +633,23 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
 {
 	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
-	/* Always do synchronous compaction */
+	/* Always try direct compaction */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
+		return GFP_TRANSHUGE | __GFP_NORETRY;
 
 	/* Kick kcompactd and fail quickly */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
 
-	/* Synchronous compaction if madvised, otherwise kick kcompactd */
+	/* Direct compaction if madvised, otherwise kick kcompactd */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT |
-			(vma_madvised ? __GFP_DIRECT_RECLAIM :
+			(vma_madvised ? (__GFP_DIRECT_RECLAIM | __GFP_NORETRY):
 					__GFP_KSWAPD_RECLAIM);
 
-	/* Only do synchronous compaction if madvised */
+	/* Only do direct compaction if madvised */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE_LIGHT |
-		       (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
+		return vma_madvised ? (GFP_TRANSHUGE | __GFP_NORETRY) : GFP_TRANSHUGE_LIGHT;
 
 	return GFP_TRANSHUGE_LIGHT;
 }
Signature · 1 line
2.19.2
#5 lore raw

RFC 2/3 mm, page_alloc: reclaim for __GFP_NORETRY costly requests only when compaction was skipped

In reply to Vlastimil Babka

For costly __GFP_NORETRY allocations (including THP's) we first do an initial compaction attempt and if that fails, we proceed with reclaim and another round of compaction, unless compaction was deferred due to earlier multiple failures. Andrea proposed [1] that we count all compaction failures as the defered case in try_to_compact_pages(), but I don't think that's a good idea in general. Instead, change the __GFP_NORETRY specific condition so that it only proceeds with further reclaim/compaction when the initial compaction attempt was skipped due to lack of free base pages.

Note that the original condition probably never worked properly for THP's, because compaction can only become deferred after a sync compaction failure, and THP's only perform async compaction, except khugepaged, which is infrequent, or madvised faults (until the previous patch restored __GFP_NORETRY for those) which are not the default case. Deferring due to async compaction failures should be however also beneficial and thus introduced in the next patch.

Also note that due to how try_to_compact_pages() constructs its return value from compaction attempts across the whole zonelist, returning COMPACT_SKIPPED means that compaction was skipped for *all* attempted zones/nodes, which means all zones/nodes are low on memory at the same moment. This is probably rare, which would mean that the resulting 'goto nopage' would be very common, just because e.g. a single zone had enough memory and compaction failed there, while the rest of nodes could succeed after reclaim. However, since THP faults use __GFP_THISNODE, compaction is also attempted only for a single node, so in practice there should be no significant loss of information when constructing the return value, nor bias towards 'goto nopage' for THP faults.

[1] https://lkml.kernel.org/r/[email protected]

Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/page_alloc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
mm/page_alloc.c +7 -7
index 2ec9cc407216..3d83a6093ada 100644
@@ -4129,14 +4129,14 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
 		 */
 		if (costly_order && (gfp_mask & __GFP_NORETRY)) {
 			/*
-			 * If compaction is deferred for high-order allocations,
-			 * it is because sync compaction recently failed. If
-			 * this is the case and the caller requested a THP
-			 * allocation, we do not want to heavily disrupt the
-			 * system, so we fail the allocation instead of entering
-			 * direct reclaim.
+			 * If compaction was skipped because of insufficient
+			 * free pages, proceed with reclaim and another
+			 * compaction attempt. If it failed for other reasons or
+			 * was deferred, do not reclaim and retry, as we do not
+			 * want to heavily disrupt the system for a costly
+			 * __GFP_NORETRY allocation such as THP.
 			 */
-			if (compact_result == COMPACT_DEFERRED)
+			if (compact_result != COMPACT_SKIPPED)
 				goto nopage;
 
 			/*
Signature · 1 line
2.19.2
#7 lore raw

RFC 3/3 mm, compaction: introduce deferred async compaction

In reply to Vlastimil Babka

Deferring compaction happens when it fails to fulfill the allocation request at given order, and then a number of the following direct compaction attempts for same or higher orders is skipped; with further failures, the number grows exponentially up to 64. This is reset e.g. when compaction succeeds.

Until now, defering compaction is only performed after a sync compaction fails, and then it also blocks async compaction attempts. The rationale is that only a failed sync compaction is expected to fully exhaust all compaction potential of a zone. However, for THP page faults that use __GFP_NORETRY, this means only async compaction is attempted and thus it is never deferred, potentially resulting in pointless reclaim/compaction attempts in a badly fragmented node.

This patch therefore tracks and checks async compaction deferred status in addition, and mostly separately from sync compaction. This allows deferring THP fault compaction without affecting any sync pageblock-order compaction. Deferring for sync compaction however implies deferring for async compaction as well. When deferred status is reset, it is reset for both modes.

The expected outcome is less compaction/reclaim activity for failing THP faults likely with some expense on THP fault success rate.

Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
---
 include/linux/compaction.h        | 10 ++--
 include/linux/mmzone.h            |  6 +--
 include/trace/events/compaction.h | 29 ++++++-----
 mm/compaction.c                   | 80 ++++++++++++++++++-------------
 4 files changed, 71 insertions(+), 54 deletions(-)
include/linux/compaction.h +5 -5
index 68250a57aace..f1d4dc1deec9 100644
@@ -100,11 +100,11 @@ extern void reset_isolation_suitable(pg_data_t *pgdat);
 extern enum compact_result compaction_suitable(struct zone *zone, int order,
 		unsigned int alloc_flags, int classzone_idx);
 
-extern void defer_compaction(struct zone *zone, int order);
-extern bool compaction_deferred(struct zone *zone, int order);
+extern void defer_compaction(struct zone *zone, int order, bool sync);
+extern bool compaction_deferred(struct zone *zone, int order, bool sync);
 extern void compaction_defer_reset(struct zone *zone, int order,
 				bool alloc_success);
-extern bool compaction_restarting(struct zone *zone, int order);
+extern bool compaction_restarting(struct zone *zone, int order, bool sync);
 
 /* Compaction has made some progress and retrying makes sense */
 static inline bool compaction_made_progress(enum compact_result result)
@@ -189,11 +189,11 @@ static inline enum compact_result compaction_suitable(struct zone *zone, int ord
 	return COMPACT_SKIPPED;
 }
 
-static inline void defer_compaction(struct zone *zone, int order)
+static inline void defer_compaction(struct zone *zone, int order, bool sync)
 {
 }
 
-static inline bool compaction_deferred(struct zone *zone, int order)
+static inline bool compaction_deferred(struct zone *zone, int order, bool sync)
 {
 	return true;
 }
include/linux/mmzone.h +3 -3
index 847705a6d0ec..4c59996dd4f9 100644
@@ -492,9 +492,9 @@ struct zone {
 	 * are skipped before trying again. The number attempted since
 	 * last failure is tracked with compact_considered.
 	 */
-	unsigned int		compact_considered;
-	unsigned int		compact_defer_shift;
-	int			compact_order_failed;
+	unsigned int		compact_considered[2];
+	unsigned int		compact_defer_shift[2];
+	int			compact_order_failed[2];
 #endif
 
 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
include/trace/events/compaction.h +16 -13
index 6074eff3d766..7ef40c76bfed 100644
@@ -245,9 +245,9 @@ DEFINE_EVENT(mm_compaction_suitable_template, mm_compaction_suitable,
 
 DECLARE_EVENT_CLASS(mm_compaction_defer_template,
 
-	TP_PROTO(struct zone *zone, int order),
+	TP_PROTO(struct zone *zone, int order, bool sync),
 
-	TP_ARGS(zone, order),
+	TP_ARGS(zone, order, sync),
 
 	TP_STRUCT__entry(
 		__field(int, nid)
@@ -256,45 +256,48 @@ DECLARE_EVENT_CLASS(mm_compaction_defer_template,
 		__field(unsigned int, considered)
 		__field(unsigned int, defer_shift)
 		__field(int, order_failed)
+		__field(bool, sync)
 	),
 
 	TP_fast_assign(
 		__entry->nid = zone_to_nid(zone);
 		__entry->idx = zone_idx(zone);
 		__entry->order = order;
-		__entry->considered = zone->compact_considered;
-		__entry->defer_shift = zone->compact_defer_shift;
-		__entry->order_failed = zone->compact_order_failed;
+		__entry->considered = zone->compact_considered[sync];
+		__entry->defer_shift = zone->compact_defer_shift[sync];
+		__entry->order_failed = zone->compact_order_failed[sync];
+		__entry->sync = sync;
 	),
 
-	TP_printk("node=%d zone=%-8s order=%d order_failed=%d consider=%u limit=%lu",
+	TP_printk("node=%d zone=%-8s order=%d order_failed=%d consider=%u limit=%lu sync=%d",
 		__entry->nid,
 		__print_symbolic(__entry->idx, ZONE_TYPE),
 		__entry->order,
 		__entry->order_failed,
 		__entry->considered,
-		1UL << __entry->defer_shift)
+		1UL << __entry->defer_shift,
+		__entry->sync)
 );
 
 DEFINE_EVENT(mm_compaction_defer_template, mm_compaction_deferred,
 
-	TP_PROTO(struct zone *zone, int order),
+	TP_PROTO(struct zone *zone, int order, bool sync),
 
-	TP_ARGS(zone, order)
+	TP_ARGS(zone, order, sync)
 );
 
 DEFINE_EVENT(mm_compaction_defer_template, mm_compaction_defer_compaction,
 
-	TP_PROTO(struct zone *zone, int order),
+	TP_PROTO(struct zone *zone, int order, bool sync),
 
-	TP_ARGS(zone, order)
+	TP_ARGS(zone, order, sync)
 );
 
 DEFINE_EVENT(mm_compaction_defer_template, mm_compaction_defer_reset,
 
-	TP_PROTO(struct zone *zone, int order),
+	TP_PROTO(struct zone *zone, int order, bool sync),
 
-	TP_ARGS(zone, order)
+	TP_ARGS(zone, order, sync)
 );
 #endif
 
mm/compaction.c +47 -33

149 lines not on this page · raw message

Signature · 1 line
2.19.2
#2 lore raw

Re: [RFC 1/3] mm, thp: restore __GFP_NORETRY for madvised thp fault allocations

In reply to Vlastimil Babka
Quoted 8 lines · Vlastimil Babka

Commit 2516035499b9 ("mm, thp: remove __GFP_NORETRY from khugepaged and madvised allocations") intended to make THP faults in MADV_HUGEPAGE areas more successful for processes that indicate that they are willing to pay a higher initial setup cost for long-term THP benefits. In the current page allocator implementation this means that the allocations will try to use reclaim and the more costly sync compaction mode, in case the initial direct async compaction fails.

First off, I'm going to have trouble reasoning about this because there is also my own series that reduces compaction failure rates. If that series get approved, then it's far more likely that when compaction fails that we really mean it and retries from the page allocator context may be pointless unless the caller indicates it should really retry for a long time.

The corner case I have in mind is a compaction failure on a very small percentage of remaining pageblocks that are currently under writeback.

It also means that the merit of this series needs to account for whether it's before or after the compaction series as the impact will be different. FWIW, I had the same problem with evaluating the compaction series on the context of __GFP_THISNODE vs !__GFP_THISNODE

Quoted 35 lines

However, THP faults also include __GFP_THISNODE, which, combined with direct reclaim, can result in a node-reclaim-like local node thrashing behavior, as reported by Andrea [1].

While this patch is not a full fix, the first step is to restore __GFP_NORETRY for madvised THP faults. The expected downside are potentially worse THP fault success rates for the madvised areas, which will have to then rely more on khugepaged. For khugepaged, __GFP_NORETRY is not restored, as its activity should be limited enough by sleeping to cause noticeable thrashing.

Note that alloc_new_node_page() and new_page() is probably another candidate as they handle the migrate_pages(2), resp. mbind(2) syscall, which can thus allow unprivileged node-reclaim-like behavior.

The patch also updates the comments in alloc_hugepage_direct_gfpmask() because elsewhere compaction during page fault is called direct compaction, and 'synchronous' refers to the migration mode, which is not used for THP faults.

[1] https://lkml.kernel.org/m/[email protected]

Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/huge_memory.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/huge_memory.c +0 -0
index 5da55b38b1b7..c442b12b060c 100644

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 639

Quoted 9 lines
mm/huge_memory.c +2 -2
@@ -633,24 +633,23 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
 {
 	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
-	/* Always do synchronous compaction */
+	/* Always try direct compaction */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
+		return GFP_TRANSHUGE | __GFP_NORETRY;
 

While I get that you want to reduce thrashing, the configuration item really indicates the system (not just the caller, but everyone) is willing to take a hit to get a THP.

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 649

Quoted 12 lines
mm/huge_memory.c +2 -2
 	/* Kick kcompactd and fail quickly */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
 
-	/* Synchronous compaction if madvised, otherwise kick kcompactd */
+	/* Direct compaction if madvised, otherwise kick kcompactd */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT |
-			(vma_madvised ? __GFP_DIRECT_RECLAIM :
+			(vma_madvised ? (__GFP_DIRECT_RECLAIM | __GFP_NORETRY):
 					__GFP_KSWAPD_RECLAIM);
 

Similar note.

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 653

Quoted 7 lines
mm/huge_memory.c +2 -3
-	/* Only do synchronous compaction if madvised */
+	/* Only do direct compaction if madvised */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE_LIGHT |
-		       (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
+		return vma_madvised ? (GFP_TRANSHUGE | __GFP_NORETRY) : GFP_TRANSHUGE_LIGHT;
 

Similar note.

That said, if this series went in before the compaction series then I would think it's ok and I would Ack it. However, *after* the compaction series, I think it would make more sense to rip out or severely reduce the complex should_compact_retry logic and move towards "if compaction returns, the page allocator should take its word for it except in extreme cases".

Compaction previously was a bit too casual about partially scanning backblocks and the setting/clearing of pageblock bits. The retry logic sortof dealt with it by making reset/clear cycles very frequent but after the series, they are relatively rare[1].

[1] Says he bravely before proven otherwise

Signature · 2 lines
Mel Gorman
SUSE Labs
#6 lore raw

Re: [RFC 2/3] mm, page_alloc: reclaim for __GFP_NORETRY costly requests only when compaction was skipped

In reply to Vlastimil Babka
Quoted 6 lines · Vlastimil Babka

For costly __GFP_NORETRY allocations (including THP's) we first do an initial compaction attempt and if that fails, we proceed with reclaim and another round of compaction, unless compaction was deferred due to earlier multiple failures. Andrea proposed [1] that we count all compaction failures as the defered case in try_to_compact_pages(), but I don't think that's a good idea in general.

I'm still stuck on the pre/post compaction series dilemma. I agree with you before the compaction series and disagree with you after. I'm not sitting on the fence here, I really think the series materially alters the facts :(.

Quoted 12 lines

Instead, change the __GFP_NORETRY specific condition so that it only proceeds with further reclaim/compaction when the initial compaction attempt was skipped due to lack of free base pages.

Note that the original condition probably never worked properly for THP's, because compaction can only become deferred after a sync compaction failure, and THP's only perform async compaction, except khugepaged, which is infrequent, or madvised faults (until the previous patch restored __GFP_NORETRY for those) which are not the default case. Deferring due to async compaction failures should be however also beneficial and thus introduced in the next patch.

Quoted 4 lines

Also note that due to how try_to_compact_pages() constructs its return value from compaction attempts across the whole zonelist, returning COMPACT_SKIPPED means that compaction was skipped for *all* attempted zones/nodes, which means all zones/nodes are low on memory at the same moment. This is probably rare,

It's not as rare as I imagined but compaction series changes it so that it really is rare or when it occurs, it probably means compaction will fail if retried.

Quoted 7 lines

which would mean that the resulting 'goto nopage' would be very common,just because e.g. a single zone had enough memory and compaction failed there, while the rest of nodes could succeed after reclaim. However, since THP faults use __GFP_THISNODE, compaction is also attempted only for a single node, so in practice there should be no significant loss of information when constructing the return value, nor bias towards 'goto nopage' for THP faults.

Post the compaction series, we never direct compact a remote node. I think post the series this concept still makes sense but it would turn into

a) try async compaction
b) try sync compaction
c) defer unconditionally

Ideally Andrea would be able to report back on this but if we can decide on the compaction series and the ordering of this, I can shove the results through the SUSE test grid with both the usemem (similar to Andrea's case) and the thpscale/thpfioscale cases and see what the latency and success rates look like.

Signature · 2 lines
Mel Gorman
SUSE Labs
#8 lore raw

Re: [RFC 3/3] mm, compaction: introduce deferred async compaction

In reply to Vlastimil Babka
Quoted 12 lines · Vlastimil Babka

Deferring compaction happens when it fails to fulfill the allocation request at given order, and then a number of the following direct compaction attempts for same or higher orders is skipped; with further failures, the number grows exponentially up to 64. This is reset e.g. when compaction succeeds.

Until now, defering compaction is only performed after a sync compaction fails, and then it also blocks async compaction attempts. The rationale is that only a failed sync compaction is expected to fully exhaust all compaction potential of a zone. However, for THP page faults that use __GFP_NORETRY, this means only async compaction is attempted and thus it is never deferred, potentially resulting in pointless reclaim/compaction attempts in a badly fragmented node.

Quoted 9 lines

This patch therefore tracks and checks async compaction deferred status in addition, and mostly separately from sync compaction. This allows deferring THP fault compaction without affecting any sync pageblock-order compaction. Deferring for sync compaction however implies deferring for async compaction as well. When deferred status is reset, it is reset for both modes.

The expected outcome is less compaction/reclaim activity for failing THP faults likely with some expense on THP fault success rate.

Either pre/post compaction series I think this makes sense although the details may change slightly. If the caller allows then do async compaction, sync compaction if that fails and defer if that also fails. If the caller can only do async compaction then defer upon failure and keep track of those two callers separetly.

Signature · 2 lines
Mel Gorman
SUSE Labs
#3 lore raw

Re: [RFC 1/3] mm, thp: restore __GFP_NORETRY for madvised thp fault allocations

In reply to Mel Gorman
Quoted 10 lines · Mel Gorman
Quoted 8 lines · Vlastimil Babka

Commit 2516035499b9 ("mm, thp: remove __GFP_NORETRY from khugepaged and madvised allocations") intended to make THP faults in MADV_HUGEPAGE areas more successful for processes that indicate that they are willing to pay a higher initial setup cost for long-term THP benefits. In the current page allocator implementation this means that the allocations will try to use reclaim and the more costly sync compaction mode, in case the initial direct async compaction fails.

Quoted 6 lines

First off, I'm going to have trouble reasoning about this because there is also my own series that reduces compaction failure rates. If that series get approved, then it's far more likely that when compaction fails that we really mean it and retries from the page allocator context may be pointless unless the caller indicates it should really retry for a long time.

Understood.

Quoted 7 lines

The corner case I have in mind is a compaction failure on a very small percentage of remaining pageblocks that are currently under writeback.

It also means that the merit of this series needs to account for whether it's before or after the compaction series as the impact will be different. FWIW, I had the same problem with evaluating the compaction series on the context of __GFP_THISNODE vs !__GFP_THISNODE

Right. In that case I think for mainline, making compaction better has priority over trying to compensate for it. The question is if somebody wants to fix stable/older distro kernels. Now that it wasn't possible to remove the __GFP_THISNODE for THP's, I thought this might be an alternative acceptable to anyone, provided that it works. Backporting your compaction series would be much more difficult I guess. Of course distro kernels can also divert from mainline and go with the __GFP_THISNODE removal privately.

Quoted 38 lines
Quoted 38 lines

However, THP faults also include __GFP_THISNODE, which, combined with direct reclaim, can result in a node-reclaim-like local node thrashing behavior, as reported by Andrea [1].

While this patch is not a full fix, the first step is to restore __GFP_NORETRY for madvised THP faults. The expected downside are potentially worse THP fault success rates for the madvised areas, which will have to then rely more on khugepaged. For khugepaged, __GFP_NORETRY is not restored, as its activity should be limited enough by sleeping to cause noticeable thrashing.

Note that alloc_new_node_page() and new_page() is probably another candidate as they handle the migrate_pages(2), resp. mbind(2) syscall, which can thus allow unprivileged node-reclaim-like behavior.

The patch also updates the comments in alloc_hugepage_direct_gfpmask() because elsewhere compaction during page fault is called direct compaction, and 'synchronous' refers to the migration mode, which is not used for THP faults.

[1] https://lkml.kernel.org/m/[email protected]

Cc Andrea Arcangeli <[email protected]>
Cc David Rientjes <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/huge_memory.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/huge_memory.c +0 -0
index 5da55b38b1b7..c442b12b060c 100644
@@ -633,24 +633,23 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
 {
 	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
Quoted 10 lines

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 639

Quoted 6 lines
mm/huge_memory.c +2 -2
-	/* Always do synchronous compaction */
+	/* Always try direct compaction */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
+		return GFP_TRANSHUGE | __GFP_NORETRY;
 

While I get that you want to reduce thrashing, the configuration item really indicates the system (not just the caller, but everyone) is willing to take a hit to get a THP.

Yeah some hit in exchange for THP's, but probably not an overreclaim due to __GFP_THISNODE implications.

Quoted 25 lines

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 649

Quoted 12 lines
mm/huge_memory.c +2 -2
 	/* Kick kcompactd and fail quickly */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
 
-	/* Synchronous compaction if madvised, otherwise kick kcompactd */
+	/* Direct compaction if madvised, otherwise kick kcompactd */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
 		return GFP_TRANSHUGE_LIGHT |
-			(vma_madvised ? __GFP_DIRECT_RECLAIM :
+			(vma_madvised ? (__GFP_DIRECT_RECLAIM | __GFP_NORETRY):
 					__GFP_KSWAPD_RECLAIM);
 

Similar note.

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 653

Quoted 7 lines
mm/huge_memory.c +2 -3
-	/* Only do synchronous compaction if madvised */
+	/* Only do direct compaction if madvised */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE_LIGHT |
-		       (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
+		return vma_madvised ? (GFP_TRANSHUGE | __GFP_NORETRY) : GFP_TRANSHUGE_LIGHT;
 

Similar note.

Quoted 5 lines

That said, if this series went in before the compaction series then I would think it's ok and I would Ack it. However, *after* the compaction series, I think it would make more sense to rip out or severely reduce the complex should_compact_retry logic and move towards "if compaction returns, the page allocator should take its word for it except in extreme cases".

OK.

Quoted 7 lines

Compaction previously was a bit too casual about partially scanning backblocks and the setting/clearing of pageblock bits. The retry logic sortof dealt with it by making reset/clear cycles very frequent but after the series, they are relatively rare[1].

[1] Says he bravely before proven otherwise

#4 lore raw

Re: [RFC 1/3] mm, thp: restore __GFP_NORETRY for madvised thp fault allocations

Quoted 7 lines · Vlastimil Babka
Quoted 4 lines

It also means that the merit of this series needs to account for whether it's before or after the compaction series as the impact will be different. FWIW, I had the same problem with evaluating the compaction series on the context of __GFP_THISNODE vs !__GFP_THISNODE

Right. In that case I think for mainline, making compaction better has priority over trying to compensate for it.

Thanks, I agree.

Quoted 8 lines

The question is if somebody
wants to fix stable/older distro kernels. Now that it wasn't possible to remove the __GFP_THISNODE for THP's, I thought this might be an alternative acceptable to anyone, provided that it works. Backporting your compaction series would be much more difficult I guess. Of course distro kernels can also divert from mainline and go with the __GFP_THISNODE removal privately.

That is a good point and hopefully Andrea can come back with some data from his side. I can queue up something our side and see how it affects the usemem case. As it's a backporting issue that I think would be rejected by the stable rules, we can discuss the specifics offline and keep "did it work or not" for here.

I agree that backporting the compaction series too far back would get "interesting" as some of the pre-requisites are unexpected -- e.g. all the data we have assumes the fragmentation avoidance stuff is in place and that in turn has other dependencies such as when kcompactd gets woken up, your patches on how fallbacks are managed etc.

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
mm/huge_memory.c +0 -0
index 5da55b38b1b7..c442b12b060c 100644
@@ -633,24 +633,23 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
 {
 	const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
 
Quoted 10 lines

mm/huge_memory.c static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) · line 639

Quoted 6 lines
mm/huge_memory.c +2 -2
-	/* Always do synchronous compaction */
+	/* Always try direct compaction */
 	if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
-		return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
+		return GFP_TRANSHUGE | __GFP_NORETRY;
 

While I get that you want to reduce thrashing, the configuration item really indicates the system (not just the caller, but everyone) is willing to take a hit to get a THP.

Quoted 3 lines

Yeah some hit in exchange for THP's, but probably not an overreclaim due to __GFP_THISNODE implications.

Fair point, we can get that data.

Signature · 2 lines
Mel Gorman
SUSE Labs