Skip to content

Commit 53853e2

Browse files
tehcastertorvalds
authored andcommitted
mm, compaction: defer each zone individually instead of preferred zone
When direct sync compaction is often unsuccessful, it may become deferred for some time to avoid further useless attempts, both sync and async. Successful high-order allocations un-defer compaction, while further unsuccessful compaction attempts prolong the compaction deferred period. Currently the checking and setting deferred status is performed only on the preferred zone of the allocation that invoked direct compaction. But compaction itself is attempted on all eligible zones in the zonelist, so the behavior is suboptimal and may lead both to scenarios where 1) compaction is attempted uselessly, or 2) where it's not attempted despite good chances of succeeding, as shown on the examples below: 1) A direct compaction with Normal preferred zone failed and set deferred compaction for the Normal zone. Another unrelated direct compaction with DMA32 as preferred zone will attempt to compact DMA32 zone even though the first compaction attempt also included DMA32 zone. In another scenario, compaction with Normal preferred zone failed to compact Normal zone, but succeeded in the DMA32 zone, so it will not defer compaction. In the next attempt, it will try Normal zone which will fail again, instead of skipping Normal zone and trying DMA32 directly. 2) Kswapd will balance DMA32 zone and reset defer status based on watermarks looking good. A direct compaction with preferred Normal zone will skip compaction of all zones including DMA32 because Normal was still deferred. The allocation might have succeeded in DMA32, but won't. This patch makes compaction deferring work on individual zone basis instead of preferred zone. For each zone, it checks compaction_deferred() to decide if the zone should be skipped. If watermarks fail after compacting the zone, defer_compaction() is called. The zone where watermarks passed can still be deferred when the allocation attempt is unsuccessful. When allocation is successful, compaction_defer_reset() is called for the zone containing the allocated page. This approach should approximate calling defer_compaction() only on zones where compaction was attempted and did not yield allocated page. There might be corner cases but that is inevitable as long as the decision to stop compacting dues not guarantee that a page will be allocated. Due to a new COMPACT_DEFERRED return value, some functions relying implicitly on COMPACT_SKIPPED = 0 had to be updated, with comments made more accurate. The did_some_progress output parameter of __alloc_pages_direct_compact() is removed completely, as the caller actually does not use it after compaction sets it - it is only considered when direct reclaim sets it. During testing on a two-node machine with a single very small Normal zone on node 1, this patch has improved success rates in stress-highalloc mmtests benchmark. The success here were previously made worse by commit 3a02576 ("mm: page_alloc: spill to remote nodes before waking kswapd") as kswapd was no longer resetting often enough the deferred compaction for the Normal zone, and DMA32 zones on both nodes were thus not considered for compaction. On different machine, success rates were improved with __GFP_NO_KSWAPD allocations. [akpm@linux-foundation.org: fix CONFIG_COMPACTION=n build] Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Acked-by: Minchan Kim <minchan@kernel.org> Reviewed-by: Zhang Yanfei <zhangyanfei@cn.fujitsu.com> Acked-by: Mel Gorman <mgorman@suse.de> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Michal Nazarewicz <mina86@mina86.com> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Christoph Lameter <cl@linux.com> Cc: Rik van Riel <riel@redhat.com> Cc: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8b16456 commit 53853e2

4 files changed

Lines changed: 76 additions & 43 deletions

File tree

‎include/linux/compaction.h‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
#define _LINUX_COMPACTION_H
33

44
/* Return values for compact_zone() and try_to_compact_pages() */
5+
/* compaction didn't start as it was deferred due to past failures */
6+
#define COMPACT_DEFERRED 0
57
/* compaction didn't start as it was not possible or direct reclaim was more suitable */
6-
#define COMPACT_SKIPPED 0
8+
#define COMPACT_SKIPPED 1
79
/* compaction should continue to another pageblock */
8-
#define COMPACT_CONTINUE 1
10+
#define COMPACT_CONTINUE 2
911
/* direct compaction partially compacted a zone and there are suitable pages */
10-
#define COMPACT_PARTIAL 2
12+
#define COMPACT_PARTIAL 3
1113
/* The full zone was compacted */
12-
#define COMPACT_COMPLETE 3
14+
#define COMPACT_COMPLETE 4
1315

1416
#ifdef CONFIG_COMPACTION
1517
extern int sysctl_compact_memory;
@@ -22,7 +24,8 @@ extern int sysctl_extfrag_handler(struct ctl_table *table, int write,
2224
extern int fragmentation_index(struct zone *zone, unsigned int order);
2325
extern unsigned long try_to_compact_pages(struct zonelist *zonelist,
2426
int order, gfp_t gfp_mask, nodemask_t *mask,
25-
enum migrate_mode mode, bool *contended);
27+
enum migrate_mode mode, bool *contended,
28+
struct zone **candidate_zone);
2629
extern void compact_pgdat(pg_data_t *pgdat, int order);
2730
extern void reset_isolation_suitable(pg_data_t *pgdat);
2831
extern unsigned long compaction_suitable(struct zone *zone, int order);
@@ -91,7 +94,8 @@ static inline bool compaction_restarting(struct zone *zone, int order)
9194
#else
9295
static inline unsigned long try_to_compact_pages(struct zonelist *zonelist,
9396
int order, gfp_t gfp_mask, nodemask_t *nodemask,
94-
enum migrate_mode mode, bool *contended)
97+
enum migrate_mode mode, bool *contended,
98+
struct zone **candidate_zone)
9599
{
96100
return COMPACT_CONTINUE;
97101
}

‎mm/compaction.c‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,27 +1125,26 @@ int sysctl_extfrag_threshold = 500;
11251125
* @nodemask: The allowed nodes to allocate from
11261126
* @mode: The migration mode for async, sync light, or sync migration
11271127
* @contended: Return value that is true if compaction was aborted due to lock contention
1128-
* @page: Optionally capture a free page of the requested order during compaction
1128+
* @candidate_zone: Return the zone where we think allocation should succeed
11291129
*
11301130
* This is the main entry point for direct page compaction.
11311131
*/
11321132
unsigned long try_to_compact_pages(struct zonelist *zonelist,
11331133
int order, gfp_t gfp_mask, nodemask_t *nodemask,
1134-
enum migrate_mode mode, bool *contended)
1134+
enum migrate_mode mode, bool *contended,
1135+
struct zone **candidate_zone)
11351136
{
11361137
enum zone_type high_zoneidx = gfp_zone(gfp_mask);
11371138
int may_enter_fs = gfp_mask & __GFP_FS;
11381139
int may_perform_io = gfp_mask & __GFP_IO;
11391140
struct zoneref *z;
11401141
struct zone *zone;
1141-
int rc = COMPACT_SKIPPED;
1142+
int rc = COMPACT_DEFERRED;
11421143
int alloc_flags = 0;
11431144

11441145
/* Check if the GFP flags allow compaction */
11451146
if (!order || !may_enter_fs || !may_perform_io)
1146-
return rc;
1147-
1148-
count_compact_event(COMPACTSTALL);
1147+
return COMPACT_SKIPPED;
11491148

11501149
#ifdef CONFIG_CMA
11511150
if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
@@ -1156,14 +1155,33 @@ unsigned long try_to_compact_pages(struct zonelist *zonelist,
11561155
nodemask) {
11571156
int status;
11581157

1158+
if (compaction_deferred(zone, order))
1159+
continue;
1160+
11591161
status = compact_zone_order(zone, order, gfp_mask, mode,
11601162
contended);
11611163
rc = max(status, rc);
11621164

11631165
/* If a normal allocation would succeed, stop compacting */
11641166
if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1165-
alloc_flags))
1167+
alloc_flags)) {
1168+
*candidate_zone = zone;
1169+
/*
1170+
* We think the allocation will succeed in this zone,
1171+
* but it is not certain, hence the false. The caller
1172+
* will repeat this with true if allocation indeed
1173+
* succeeds in this zone.
1174+
*/
1175+
compaction_defer_reset(zone, order, false);
11661176
break;
1177+
} else if (mode != MIGRATE_ASYNC) {
1178+
/*
1179+
* We think that allocation won't succeed in this zone
1180+
* so we defer compaction there. If it ends up
1181+
* succeeding after all, it will be reset.
1182+
*/
1183+
defer_compaction(zone, order);
1184+
}
11671185
}
11681186

11691187
return rc;

‎mm/page_alloc.c‎

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,24 +2297,28 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
22972297
struct zonelist *zonelist, enum zone_type high_zoneidx,
22982298
nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
22992299
int classzone_idx, int migratetype, enum migrate_mode mode,
2300-
bool *contended_compaction, bool *deferred_compaction,
2301-
unsigned long *did_some_progress)
2300+
bool *contended_compaction, bool *deferred_compaction)
23022301
{
2303-
if (!order)
2304-
return NULL;
2302+
struct zone *last_compact_zone = NULL;
2303+
unsigned long compact_result;
23052304

2306-
if (compaction_deferred(preferred_zone, order)) {
2307-
*deferred_compaction = true;
2305+
2306+
if (!order)
23082307
return NULL;
2309-
}
23102308

23112309
current->flags |= PF_MEMALLOC;
2312-
*did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
2310+
compact_result = try_to_compact_pages(zonelist, order, gfp_mask,
23132311
nodemask, mode,
2314-
contended_compaction);
2312+
contended_compaction,
2313+
&last_compact_zone);
23152314
current->flags &= ~PF_MEMALLOC;
23162315

2317-
if (*did_some_progress != COMPACT_SKIPPED) {
2316+
if (compact_result > COMPACT_DEFERRED)
2317+
count_vm_event(COMPACTSTALL);
2318+
else
2319+
*deferred_compaction = true;
2320+
2321+
if (compact_result > COMPACT_SKIPPED) {
23182322
struct page *page;
23192323

23202324
/* Page migration frees to the PCP lists but we want merging */
@@ -2325,27 +2329,31 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
23252329
order, zonelist, high_zoneidx,
23262330
alloc_flags & ~ALLOC_NO_WATERMARKS,
23272331
preferred_zone, classzone_idx, migratetype);
2332+
23282333
if (page) {
2329-
preferred_zone->compact_blockskip_flush = false;
2330-
compaction_defer_reset(preferred_zone, order, true);
2334+
struct zone *zone = page_zone(page);
2335+
2336+
zone->compact_blockskip_flush = false;
2337+
compaction_defer_reset(zone, order, true);
23312338
count_vm_event(COMPACTSUCCESS);
23322339
return page;
23332340
}
23342341

2342+
/*
2343+
* last_compact_zone is where try_to_compact_pages thought
2344+
* allocation should succeed, so it did not defer compaction.
2345+
* But now we know that it didn't succeed, so we do the defer.
2346+
*/
2347+
if (last_compact_zone && mode != MIGRATE_ASYNC)
2348+
defer_compaction(last_compact_zone, order);
2349+
23352350
/*
23362351
* It's bad if compaction run occurs and fails.
23372352
* The most likely reason is that pages exist,
23382353
* but not enough to satisfy watermarks.
23392354
*/
23402355
count_vm_event(COMPACTFAIL);
23412356

2342-
/*
2343-
* As async compaction considers a subset of pageblocks, only
2344-
* defer if the failure was a sync compaction failure.
2345-
*/
2346-
if (mode != MIGRATE_ASYNC)
2347-
defer_compaction(preferred_zone, order);
2348-
23492357
cond_resched();
23502358
}
23512359

@@ -2356,9 +2364,8 @@ static inline struct page *
23562364
__alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
23572365
struct zonelist *zonelist, enum zone_type high_zoneidx,
23582366
nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2359-
int classzone_idx, int migratetype,
2360-
enum migrate_mode mode, bool *contended_compaction,
2361-
bool *deferred_compaction, unsigned long *did_some_progress)
2367+
int classzone_idx, int migratetype, enum migrate_mode mode,
2368+
bool *contended_compaction, bool *deferred_compaction)
23622369
{
23632370
return NULL;
23642371
}
@@ -2634,8 +2641,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
26342641
preferred_zone,
26352642
classzone_idx, migratetype,
26362643
migration_mode, &contended_compaction,
2637-
&deferred_compaction,
2638-
&did_some_progress);
2644+
&deferred_compaction);
26392645
if (page)
26402646
goto got_pg;
26412647

@@ -2727,8 +2733,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
27272733
preferred_zone,
27282734
classzone_idx, migratetype,
27292735
migration_mode, &contended_compaction,
2730-
&deferred_compaction,
2731-
&did_some_progress);
2736+
&deferred_compaction);
27322737
if (page)
27332738
goto got_pg;
27342739
}

‎mm/vmscan.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,7 +2315,10 @@ static bool shrink_zone(struct zone *zone, struct scan_control *sc)
23152315
return reclaimable;
23162316
}
23172317

2318-
/* Returns true if compaction should go ahead for a high-order request */
2318+
/*
2319+
* Returns true if compaction should go ahead for a high-order request, or
2320+
* the high-order allocation would succeed without compaction.
2321+
*/
23192322
static inline bool compaction_ready(struct zone *zone, int order)
23202323
{
23212324
unsigned long balance_gap, watermark;
@@ -2339,8 +2342,11 @@ static inline bool compaction_ready(struct zone *zone, int order)
23392342
if (compaction_deferred(zone, order))
23402343
return watermark_ok;
23412344

2342-
/* If compaction is not ready to start, keep reclaiming */
2343-
if (!compaction_suitable(zone, order))
2345+
/*
2346+
* If compaction is not ready to start and allocation is not likely
2347+
* to succeed without it, then keep reclaiming.
2348+
*/
2349+
if (compaction_suitable(zone, order) == COMPACT_SKIPPED)
23442350
return false;
23452351

23462352
return watermark_ok;
@@ -2818,7 +2824,7 @@ static bool zone_balanced(struct zone *zone, int order,
28182824
return false;
28192825

28202826
if (IS_ENABLED(CONFIG_COMPACTION) && order &&
2821-
!compaction_suitable(zone, order))
2827+
compaction_suitable(zone, order) == COMPACT_SKIPPED)
28222828
return false;
28232829

28242830
return true;

0 commit comments

Comments
 (0)