xref: /linux/mm/compaction.c (revision f5f61a320bf6275f37fcabf6645b4ac8e683c007)
1748446bbSMel Gorman /*
2748446bbSMel Gorman  * linux/mm/compaction.c
3748446bbSMel Gorman  *
4748446bbSMel Gorman  * Memory compaction for the reduction of external fragmentation. Note that
5748446bbSMel Gorman  * this heavily depends upon page migration to do all the real heavy
6748446bbSMel Gorman  * lifting
7748446bbSMel Gorman  *
8748446bbSMel Gorman  * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9748446bbSMel Gorman  */
10748446bbSMel Gorman #include <linux/swap.h>
11748446bbSMel Gorman #include <linux/migrate.h>
12748446bbSMel Gorman #include <linux/compaction.h>
13748446bbSMel Gorman #include <linux/mm_inline.h>
14748446bbSMel Gorman #include <linux/backing-dev.h>
1576ab0f53SMel Gorman #include <linux/sysctl.h>
16ed4a6d7fSMel Gorman #include <linux/sysfs.h>
17bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h>
18194159fbSMinchan Kim #include <linux/page-isolation.h>
19b8c73fc2SAndrey Ryabinin #include <linux/kasan.h>
20748446bbSMel Gorman #include "internal.h"
21748446bbSMel Gorman 
22010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
23010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
24010fc29aSMinchan Kim {
25010fc29aSMinchan Kim 	count_vm_event(item);
26010fc29aSMinchan Kim }
27010fc29aSMinchan Kim 
28010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
29010fc29aSMinchan Kim {
30010fc29aSMinchan Kim 	count_vm_events(item, delta);
31010fc29aSMinchan Kim }
32010fc29aSMinchan Kim #else
33010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
34010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
35010fc29aSMinchan Kim #endif
36010fc29aSMinchan Kim 
37ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
3816c4a097SJoonsoo Kim #ifdef CONFIG_TRACEPOINTS
3916c4a097SJoonsoo Kim static const char *const compaction_status_string[] = {
4016c4a097SJoonsoo Kim 	"deferred",
4116c4a097SJoonsoo Kim 	"skipped",
4216c4a097SJoonsoo Kim 	"continue",
4316c4a097SJoonsoo Kim 	"partial",
4416c4a097SJoonsoo Kim 	"complete",
45837d026dSJoonsoo Kim 	"no_suitable_page",
46837d026dSJoonsoo Kim 	"not_suitable_zone",
4716c4a097SJoonsoo Kim };
4816c4a097SJoonsoo Kim #endif
49ff9543fdSMichal Nazarewicz 
50b7aba698SMel Gorman #define CREATE_TRACE_POINTS
51b7aba698SMel Gorman #include <trace/events/compaction.h>
52b7aba698SMel Gorman 
53748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
54748446bbSMel Gorman {
55748446bbSMel Gorman 	struct page *page, *next;
566bace090SVlastimil Babka 	unsigned long high_pfn = 0;
57748446bbSMel Gorman 
58748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
596bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
60748446bbSMel Gorman 		list_del(&page->lru);
61748446bbSMel Gorman 		__free_page(page);
626bace090SVlastimil Babka 		if (pfn > high_pfn)
636bace090SVlastimil Babka 			high_pfn = pfn;
64748446bbSMel Gorman 	}
65748446bbSMel Gorman 
666bace090SVlastimil Babka 	return high_pfn;
67748446bbSMel Gorman }
68748446bbSMel Gorman 
69ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
70ff9543fdSMichal Nazarewicz {
71ff9543fdSMichal Nazarewicz 	struct page *page;
72ff9543fdSMichal Nazarewicz 
73ff9543fdSMichal Nazarewicz 	list_for_each_entry(page, list, lru) {
74ff9543fdSMichal Nazarewicz 		arch_alloc_page(page, 0);
75ff9543fdSMichal Nazarewicz 		kernel_map_pages(page, 1, 1);
76b8c73fc2SAndrey Ryabinin 		kasan_alloc_pages(page, 0);
77ff9543fdSMichal Nazarewicz 	}
78ff9543fdSMichal Nazarewicz }
79ff9543fdSMichal Nazarewicz 
8047118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype)
8147118af0SMichal Nazarewicz {
8247118af0SMichal Nazarewicz 	return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
8347118af0SMichal Nazarewicz }
8447118af0SMichal Nazarewicz 
857d49d886SVlastimil Babka /*
867d49d886SVlastimil Babka  * Check that the whole (or subset of) a pageblock given by the interval of
877d49d886SVlastimil Babka  * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
887d49d886SVlastimil Babka  * with the migration of free compaction scanner. The scanners then need to
897d49d886SVlastimil Babka  * use only pfn_valid_within() check for arches that allow holes within
907d49d886SVlastimil Babka  * pageblocks.
917d49d886SVlastimil Babka  *
927d49d886SVlastimil Babka  * Return struct page pointer of start_pfn, or NULL if checks were not passed.
937d49d886SVlastimil Babka  *
947d49d886SVlastimil Babka  * It's possible on some configurations to have a setup like node0 node1 node0
957d49d886SVlastimil Babka  * i.e. it's possible that all pages within a zones range of pages do not
967d49d886SVlastimil Babka  * belong to a single zone. We assume that a border between node0 and node1
977d49d886SVlastimil Babka  * can occur within a single pageblock, but not a node0 node1 node0
987d49d886SVlastimil Babka  * interleaving within a single pageblock. It is therefore sufficient to check
997d49d886SVlastimil Babka  * the first and last page of a pageblock and avoid checking each individual
1007d49d886SVlastimil Babka  * page in a pageblock.
1017d49d886SVlastimil Babka  */
1027d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
1037d49d886SVlastimil Babka 				unsigned long end_pfn, struct zone *zone)
1047d49d886SVlastimil Babka {
1057d49d886SVlastimil Babka 	struct page *start_page;
1067d49d886SVlastimil Babka 	struct page *end_page;
1077d49d886SVlastimil Babka 
1087d49d886SVlastimil Babka 	/* end_pfn is one past the range we are checking */
1097d49d886SVlastimil Babka 	end_pfn--;
1107d49d886SVlastimil Babka 
1117d49d886SVlastimil Babka 	if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1127d49d886SVlastimil Babka 		return NULL;
1137d49d886SVlastimil Babka 
1147d49d886SVlastimil Babka 	start_page = pfn_to_page(start_pfn);
1157d49d886SVlastimil Babka 
1167d49d886SVlastimil Babka 	if (page_zone(start_page) != zone)
1177d49d886SVlastimil Babka 		return NULL;
1187d49d886SVlastimil Babka 
1197d49d886SVlastimil Babka 	end_page = pfn_to_page(end_pfn);
1207d49d886SVlastimil Babka 
1217d49d886SVlastimil Babka 	/* This gives a shorter code than deriving page_zone(end_page) */
1227d49d886SVlastimil Babka 	if (page_zone_id(start_page) != page_zone_id(end_page))
1237d49d886SVlastimil Babka 		return NULL;
1247d49d886SVlastimil Babka 
1257d49d886SVlastimil Babka 	return start_page;
1267d49d886SVlastimil Babka }
1277d49d886SVlastimil Babka 
128bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
12924e2716fSJoonsoo Kim 
13024e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
13124e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
13224e2716fSJoonsoo Kim 
13324e2716fSJoonsoo Kim /*
13424e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
13524e2716fSJoonsoo Kim  * allocation success. 1 << compact_defer_limit compactions are skipped up
13624e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
13724e2716fSJoonsoo Kim  */
13824e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
13924e2716fSJoonsoo Kim {
14024e2716fSJoonsoo Kim 	zone->compact_considered = 0;
14124e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
14224e2716fSJoonsoo Kim 
14324e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
14424e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
14524e2716fSJoonsoo Kim 
14624e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
14724e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
14824e2716fSJoonsoo Kim 
14924e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
15024e2716fSJoonsoo Kim }
15124e2716fSJoonsoo Kim 
15224e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
15324e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
15424e2716fSJoonsoo Kim {
15524e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
15624e2716fSJoonsoo Kim 
15724e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
15824e2716fSJoonsoo Kim 		return false;
15924e2716fSJoonsoo Kim 
16024e2716fSJoonsoo Kim 	/* Avoid possible overflow */
16124e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
16224e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
16324e2716fSJoonsoo Kim 
16424e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
16524e2716fSJoonsoo Kim 		return false;
16624e2716fSJoonsoo Kim 
16724e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
16824e2716fSJoonsoo Kim 
16924e2716fSJoonsoo Kim 	return true;
17024e2716fSJoonsoo Kim }
17124e2716fSJoonsoo Kim 
17224e2716fSJoonsoo Kim /*
17324e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
17424e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
17524e2716fSJoonsoo Kim  * expected to succeed.
17624e2716fSJoonsoo Kim  */
17724e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
17824e2716fSJoonsoo Kim 		bool alloc_success)
17924e2716fSJoonsoo Kim {
18024e2716fSJoonsoo Kim 	if (alloc_success) {
18124e2716fSJoonsoo Kim 		zone->compact_considered = 0;
18224e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
18324e2716fSJoonsoo Kim 	}
18424e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
18524e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
18624e2716fSJoonsoo Kim 
18724e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
18824e2716fSJoonsoo Kim }
18924e2716fSJoonsoo Kim 
19024e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
19124e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
19224e2716fSJoonsoo Kim {
19324e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
19424e2716fSJoonsoo Kim 		return false;
19524e2716fSJoonsoo Kim 
19624e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
19724e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
19824e2716fSJoonsoo Kim }
19924e2716fSJoonsoo Kim 
200bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
201bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
202bb13ffebSMel Gorman 					struct page *page)
203bb13ffebSMel Gorman {
204bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
205bb13ffebSMel Gorman 		return true;
206bb13ffebSMel Gorman 
207bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
208bb13ffebSMel Gorman }
209bb13ffebSMel Gorman 
210bb13ffebSMel Gorman /*
211bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
212bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
213bb13ffebSMel Gorman  * meet.
214bb13ffebSMel Gorman  */
21562997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
216bb13ffebSMel Gorman {
217bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
218108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
219bb13ffebSMel Gorman 	unsigned long pfn;
220bb13ffebSMel Gorman 
22135979ef3SDavid Rientjes 	zone->compact_cached_migrate_pfn[0] = start_pfn;
22235979ef3SDavid Rientjes 	zone->compact_cached_migrate_pfn[1] = start_pfn;
223c89511abSMel Gorman 	zone->compact_cached_free_pfn = end_pfn;
22462997027SMel Gorman 	zone->compact_blockskip_flush = false;
225bb13ffebSMel Gorman 
226bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
227bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
228bb13ffebSMel Gorman 		struct page *page;
229bb13ffebSMel Gorman 
230bb13ffebSMel Gorman 		cond_resched();
231bb13ffebSMel Gorman 
232bb13ffebSMel Gorman 		if (!pfn_valid(pfn))
233bb13ffebSMel Gorman 			continue;
234bb13ffebSMel Gorman 
235bb13ffebSMel Gorman 		page = pfn_to_page(pfn);
236bb13ffebSMel Gorman 		if (zone != page_zone(page))
237bb13ffebSMel Gorman 			continue;
238bb13ffebSMel Gorman 
239bb13ffebSMel Gorman 		clear_pageblock_skip(page);
240bb13ffebSMel Gorman 	}
241bb13ffebSMel Gorman }
242bb13ffebSMel Gorman 
24362997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
24462997027SMel Gorman {
24562997027SMel Gorman 	int zoneid;
24662997027SMel Gorman 
24762997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
24862997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
24962997027SMel Gorman 		if (!populated_zone(zone))
25062997027SMel Gorman 			continue;
25162997027SMel Gorman 
25262997027SMel Gorman 		/* Only flush if a full compaction finished recently */
25362997027SMel Gorman 		if (zone->compact_blockskip_flush)
25462997027SMel Gorman 			__reset_isolation_suitable(zone);
25562997027SMel Gorman 	}
25662997027SMel Gorman }
25762997027SMel Gorman 
258bb13ffebSMel Gorman /*
259bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
26062997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
261bb13ffebSMel Gorman  */
262c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
263c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
264edc2ca61SVlastimil Babka 			bool migrate_scanner)
265bb13ffebSMel Gorman {
266c89511abSMel Gorman 	struct zone *zone = cc->zone;
26735979ef3SDavid Rientjes 	unsigned long pfn;
2686815bf3fSJoonsoo Kim 
2696815bf3fSJoonsoo Kim 	if (cc->ignore_skip_hint)
2706815bf3fSJoonsoo Kim 		return;
2716815bf3fSJoonsoo Kim 
272bb13ffebSMel Gorman 	if (!page)
273bb13ffebSMel Gorman 		return;
274bb13ffebSMel Gorman 
27535979ef3SDavid Rientjes 	if (nr_isolated)
27635979ef3SDavid Rientjes 		return;
27735979ef3SDavid Rientjes 
278bb13ffebSMel Gorman 	set_pageblock_skip(page);
279c89511abSMel Gorman 
28035979ef3SDavid Rientjes 	pfn = page_to_pfn(page);
28135979ef3SDavid Rientjes 
28235979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
283c89511abSMel Gorman 	if (migrate_scanner) {
28435979ef3SDavid Rientjes 		if (pfn > zone->compact_cached_migrate_pfn[0])
28535979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = pfn;
286e0b9daebSDavid Rientjes 		if (cc->mode != MIGRATE_ASYNC &&
287e0b9daebSDavid Rientjes 		    pfn > zone->compact_cached_migrate_pfn[1])
28835979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = pfn;
289c89511abSMel Gorman 	} else {
29035979ef3SDavid Rientjes 		if (pfn < zone->compact_cached_free_pfn)
291c89511abSMel Gorman 			zone->compact_cached_free_pfn = pfn;
292c89511abSMel Gorman 	}
293c89511abSMel Gorman }
294bb13ffebSMel Gorman #else
295bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
296bb13ffebSMel Gorman 					struct page *page)
297bb13ffebSMel Gorman {
298bb13ffebSMel Gorman 	return true;
299bb13ffebSMel Gorman }
300bb13ffebSMel Gorman 
301c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
302c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
303edc2ca61SVlastimil Babka 			bool migrate_scanner)
304bb13ffebSMel Gorman {
305bb13ffebSMel Gorman }
306bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
307bb13ffebSMel Gorman 
3081f9efdefSVlastimil Babka /*
3098b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
3108b44d279SVlastimil Babka  * very heavily contended. For async compaction, back out if the lock cannot
3118b44d279SVlastimil Babka  * be taken immediately. For sync compaction, spin on the lock if needed.
3128b44d279SVlastimil Babka  *
3138b44d279SVlastimil Babka  * Returns true if the lock is held
3148b44d279SVlastimil Babka  * Returns false if the lock is not held and compaction should abort
3151f9efdefSVlastimil Babka  */
3168b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
3178b44d279SVlastimil Babka 						struct compact_control *cc)
3188b44d279SVlastimil Babka {
3198b44d279SVlastimil Babka 	if (cc->mode == MIGRATE_ASYNC) {
3208b44d279SVlastimil Babka 		if (!spin_trylock_irqsave(lock, *flags)) {
3218b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_LOCK;
3228b44d279SVlastimil Babka 			return false;
3238b44d279SVlastimil Babka 		}
3248b44d279SVlastimil Babka 	} else {
3258b44d279SVlastimil Babka 		spin_lock_irqsave(lock, *flags);
3268b44d279SVlastimil Babka 	}
3271f9efdefSVlastimil Babka 
3288b44d279SVlastimil Babka 	return true;
3292a1402aaSMel Gorman }
3302a1402aaSMel Gorman 
33185aa125fSMichal Nazarewicz /*
332c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
3338b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
3348b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
3358b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
3368b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
3378b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
3388b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
3398b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
340c67fe375SMel Gorman  *
3418b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
3428b44d279SVlastimil Babka  *		async compaction due to need_resched()
3438b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
3448b44d279SVlastimil Babka  *		scheduled)
345c67fe375SMel Gorman  */
3468b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
3478b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
348c67fe375SMel Gorman {
3498b44d279SVlastimil Babka 	if (*locked) {
3508b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
3518b44d279SVlastimil Babka 		*locked = false;
352c67fe375SMel Gorman 	}
353c67fe375SMel Gorman 
3548b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
3558b44d279SVlastimil Babka 		cc->contended = COMPACT_CONTENDED_SCHED;
3568b44d279SVlastimil Babka 		return true;
3578b44d279SVlastimil Babka 	}
3588b44d279SVlastimil Babka 
3598b44d279SVlastimil Babka 	if (need_resched()) {
360e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC) {
3618b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
3628b44d279SVlastimil Babka 			return true;
363c67fe375SMel Gorman 		}
364c67fe375SMel Gorman 		cond_resched();
365c67fe375SMel Gorman 	}
366c67fe375SMel Gorman 
3678b44d279SVlastimil Babka 	return false;
368c67fe375SMel Gorman }
369c67fe375SMel Gorman 
370be976572SVlastimil Babka /*
371be976572SVlastimil Babka  * Aside from avoiding lock contention, compaction also periodically checks
372be976572SVlastimil Babka  * need_resched() and either schedules in sync compaction or aborts async
3738b44d279SVlastimil Babka  * compaction. This is similar to what compact_unlock_should_abort() does, but
374be976572SVlastimil Babka  * is used where no lock is concerned.
375be976572SVlastimil Babka  *
376be976572SVlastimil Babka  * Returns false when no scheduling was needed, or sync compaction scheduled.
377be976572SVlastimil Babka  * Returns true when async compaction should abort.
378be976572SVlastimil Babka  */
379be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc)
380be976572SVlastimil Babka {
381be976572SVlastimil Babka 	/* async compaction aborts if contended */
382be976572SVlastimil Babka 	if (need_resched()) {
383be976572SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC) {
3841f9efdefSVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
385be976572SVlastimil Babka 			return true;
386be976572SVlastimil Babka 		}
387be976572SVlastimil Babka 
388be976572SVlastimil Babka 		cond_resched();
389be976572SVlastimil Babka 	}
390be976572SVlastimil Babka 
391be976572SVlastimil Babka 	return false;
392be976572SVlastimil Babka }
393be976572SVlastimil Babka 
394c67fe375SMel Gorman /*
3959e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
3969e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
3979e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
39885aa125fSMichal Nazarewicz  */
399f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
400e14c720eSVlastimil Babka 				unsigned long *start_pfn,
40185aa125fSMichal Nazarewicz 				unsigned long end_pfn,
40285aa125fSMichal Nazarewicz 				struct list_head *freelist,
40385aa125fSMichal Nazarewicz 				bool strict)
404748446bbSMel Gorman {
405b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
406bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
407b8b2d825SXiubo Li 	unsigned long flags = 0;
408f40d1e42SMel Gorman 	bool locked = false;
409e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
410748446bbSMel Gorman 
411748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
412748446bbSMel Gorman 
413f40d1e42SMel Gorman 	/* Isolate free pages. */
414748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
415748446bbSMel Gorman 		int isolated, i;
416748446bbSMel Gorman 		struct page *page = cursor;
417748446bbSMel Gorman 
4188b44d279SVlastimil Babka 		/*
4198b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
4208b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
4218b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
4228b44d279SVlastimil Babka 		 */
4238b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
4248b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
4258b44d279SVlastimil Babka 								&locked, cc))
4268b44d279SVlastimil Babka 			break;
4278b44d279SVlastimil Babka 
428b7aba698SMel Gorman 		nr_scanned++;
429f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
4302af120bcSLaura Abbott 			goto isolate_fail;
4312af120bcSLaura Abbott 
432bb13ffebSMel Gorman 		if (!valid_page)
433bb13ffebSMel Gorman 			valid_page = page;
434f40d1e42SMel Gorman 		if (!PageBuddy(page))
4352af120bcSLaura Abbott 			goto isolate_fail;
436f40d1e42SMel Gorman 
437f40d1e42SMel Gorman 		/*
43869b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
43969b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
44069b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
44169b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
44269b7189fSVlastimil Babka 		 * recheck as well.
44369b7189fSVlastimil Babka 		 */
44469b7189fSVlastimil Babka 		if (!locked) {
44569b7189fSVlastimil Babka 			/*
446f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
447f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
448f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
449f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
450f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
451f40d1e42SMel Gorman 			 * possible.
452f40d1e42SMel Gorman 			 */
4538b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
4548b44d279SVlastimil Babka 								&flags, cc);
455f40d1e42SMel Gorman 			if (!locked)
456f40d1e42SMel Gorman 				break;
457f40d1e42SMel Gorman 
458f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
459f40d1e42SMel Gorman 			if (!PageBuddy(page))
4602af120bcSLaura Abbott 				goto isolate_fail;
46169b7189fSVlastimil Babka 		}
462748446bbSMel Gorman 
463748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
464748446bbSMel Gorman 		isolated = split_free_page(page);
465748446bbSMel Gorman 		total_isolated += isolated;
466748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
467748446bbSMel Gorman 			list_add(&page->lru, freelist);
468748446bbSMel Gorman 			page++;
469748446bbSMel Gorman 		}
470748446bbSMel Gorman 
471748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
472748446bbSMel Gorman 		if (isolated) {
473932ff6bbSJoonsoo Kim 			cc->nr_freepages += isolated;
474932ff6bbSJoonsoo Kim 			if (!strict &&
475932ff6bbSJoonsoo Kim 				cc->nr_migratepages <= cc->nr_freepages) {
476932ff6bbSJoonsoo Kim 				blockpfn += isolated;
477932ff6bbSJoonsoo Kim 				break;
478932ff6bbSJoonsoo Kim 			}
479932ff6bbSJoonsoo Kim 
480748446bbSMel Gorman 			blockpfn += isolated - 1;
481748446bbSMel Gorman 			cursor += isolated - 1;
4822af120bcSLaura Abbott 			continue;
483748446bbSMel Gorman 		}
4842af120bcSLaura Abbott 
4852af120bcSLaura Abbott isolate_fail:
4862af120bcSLaura Abbott 		if (strict)
4872af120bcSLaura Abbott 			break;
4882af120bcSLaura Abbott 		else
4892af120bcSLaura Abbott 			continue;
4902af120bcSLaura Abbott 
491748446bbSMel Gorman 	}
492748446bbSMel Gorman 
493e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
494e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
495e34d85f0SJoonsoo Kim 
496e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
497e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
498e14c720eSVlastimil Babka 
499f40d1e42SMel Gorman 	/*
500f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
501f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
502f40d1e42SMel Gorman 	 * returned and CMA will fail.
503f40d1e42SMel Gorman 	 */
5042af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
505f40d1e42SMel Gorman 		total_isolated = 0;
506f40d1e42SMel Gorman 
507f40d1e42SMel Gorman 	if (locked)
508f40d1e42SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
509f40d1e42SMel Gorman 
510bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
511bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
512edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
513bb13ffebSMel Gorman 
514010fc29aSMinchan Kim 	count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
515397487dbSMel Gorman 	if (total_isolated)
516010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
517748446bbSMel Gorman 	return total_isolated;
518748446bbSMel Gorman }
519748446bbSMel Gorman 
52085aa125fSMichal Nazarewicz /**
52185aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
52285aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
52385aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
52485aa125fSMichal Nazarewicz  *
52585aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
52685aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
52785aa125fSMichal Nazarewicz  * undo its actions and return zero.
52885aa125fSMichal Nazarewicz  *
52985aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
53085aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
53185aa125fSMichal Nazarewicz  * a free page).
53285aa125fSMichal Nazarewicz  */
533ff9543fdSMichal Nazarewicz unsigned long
534bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
535bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
53685aa125fSMichal Nazarewicz {
537f40d1e42SMel Gorman 	unsigned long isolated, pfn, block_end_pfn;
53885aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
53985aa125fSMichal Nazarewicz 
5407d49d886SVlastimil Babka 	pfn = start_pfn;
54185aa125fSMichal Nazarewicz 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
5427d49d886SVlastimil Babka 
5437d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
5447d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
545e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
546e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
5477d49d886SVlastimil Babka 
54885aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
54985aa125fSMichal Nazarewicz 
55058420016SJoonsoo Kim 		/*
55158420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
55258420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
55358420016SJoonsoo Kim 		 * scanning range to right one.
55458420016SJoonsoo Kim 		 */
55558420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
55658420016SJoonsoo Kim 			block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
55758420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
55858420016SJoonsoo Kim 		}
55958420016SJoonsoo Kim 
5607d49d886SVlastimil Babka 		if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
5617d49d886SVlastimil Babka 			break;
5627d49d886SVlastimil Babka 
563e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
564e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
56585aa125fSMichal Nazarewicz 
56685aa125fSMichal Nazarewicz 		/*
56785aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
56885aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
56985aa125fSMichal Nazarewicz 		 * non-free pages).
57085aa125fSMichal Nazarewicz 		 */
57185aa125fSMichal Nazarewicz 		if (!isolated)
57285aa125fSMichal Nazarewicz 			break;
57385aa125fSMichal Nazarewicz 
57485aa125fSMichal Nazarewicz 		/*
57585aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
57685aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
57785aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
57885aa125fSMichal Nazarewicz 		 */
57985aa125fSMichal Nazarewicz 	}
58085aa125fSMichal Nazarewicz 
58185aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
58285aa125fSMichal Nazarewicz 	map_pages(&freelist);
58385aa125fSMichal Nazarewicz 
58485aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
58585aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
58685aa125fSMichal Nazarewicz 		release_freepages(&freelist);
58785aa125fSMichal Nazarewicz 		return 0;
58885aa125fSMichal Nazarewicz 	}
58985aa125fSMichal Nazarewicz 
59085aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
59185aa125fSMichal Nazarewicz 	return pfn;
59285aa125fSMichal Nazarewicz }
59385aa125fSMichal Nazarewicz 
594748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
595edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc)
596748446bbSMel Gorman {
597748446bbSMel Gorman 	struct page *page;
598b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
599748446bbSMel Gorman 
600edc2ca61SVlastimil Babka 	if (list_empty(&cc->migratepages))
601edc2ca61SVlastimil Babka 		return;
602edc2ca61SVlastimil Babka 
603b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
604b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
605748446bbSMel Gorman 
606c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
607c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
608c67fe375SMel Gorman }
609748446bbSMel Gorman 
610748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
611748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
612748446bbSMel Gorman {
613bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
614748446bbSMel Gorman 
615748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
616748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
617bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
618bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
619748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
620748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
621748446bbSMel Gorman 
622bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
623748446bbSMel Gorman }
624748446bbSMel Gorman 
6252fe86e00SMichal Nazarewicz /**
626edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
627edc2ca61SVlastimil Babka  *				  a single pageblock
6282fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
629edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
630edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
631edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
6322fe86e00SMichal Nazarewicz  *
6332fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
634edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
635edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
636edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
637edc2ca61SVlastimil Babka  * than end_pfn).
6382fe86e00SMichal Nazarewicz  *
639edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
640edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
641edc2ca61SVlastimil Babka  * is neither read nor updated.
642748446bbSMel Gorman  */
643edc2ca61SVlastimil Babka static unsigned long
644edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
645edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
646748446bbSMel Gorman {
647edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
648b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
649748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
650fa9add64SHugh Dickins 	struct lruvec *lruvec;
651b8b2d825SXiubo Li 	unsigned long flags = 0;
6522a1402aaSMel Gorman 	bool locked = false;
653bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
654e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
655748446bbSMel Gorman 
656748446bbSMel Gorman 	/*
657748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
658748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
659748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
660748446bbSMel Gorman 	 */
661748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
662f9e35b3bSMel Gorman 		/* async migration should just abort */
663e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
6642fe86e00SMichal Nazarewicz 			return 0;
665f9e35b3bSMel Gorman 
666748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
667748446bbSMel Gorman 
668748446bbSMel Gorman 		if (fatal_signal_pending(current))
6692fe86e00SMichal Nazarewicz 			return 0;
670748446bbSMel Gorman 	}
671748446bbSMel Gorman 
672be976572SVlastimil Babka 	if (compact_should_abort(cc))
673aeef4b83SDavid Rientjes 		return 0;
674aeef4b83SDavid Rientjes 
675748446bbSMel Gorman 	/* Time to isolate some pages for migration */
676748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
6778b44d279SVlastimil Babka 		/*
6788b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
6798b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
6808b44d279SVlastimil Babka 		 * if contended.
6818b44d279SVlastimil Babka 		 */
6828b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
6838b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&zone->lru_lock, flags,
6848b44d279SVlastimil Babka 								&locked, cc))
6858b44d279SVlastimil Babka 			break;
686b2eef8c0SAndrea Arcangeli 
687748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
688748446bbSMel Gorman 			continue;
689b7aba698SMel Gorman 		nr_scanned++;
690748446bbSMel Gorman 
691748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
692dc908600SMel Gorman 
693bb13ffebSMel Gorman 		if (!valid_page)
694bb13ffebSMel Gorman 			valid_page = page;
695bb13ffebSMel Gorman 
696c122b208SJoonsoo Kim 		/*
69799c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
69899c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
69999c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
70099c0fd5eSVlastimil Babka 		 * potential isolation targets.
7016c14466cSMel Gorman 		 */
70299c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
70399c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
70499c0fd5eSVlastimil Babka 
70599c0fd5eSVlastimil Babka 			/*
70699c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
70799c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
70899c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
70999c0fd5eSVlastimil Babka 			 */
71099c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
71199c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
712748446bbSMel Gorman 			continue;
71399c0fd5eSVlastimil Babka 		}
714748446bbSMel Gorman 
7159927af74SMel Gorman 		/*
716bf6bddf1SRafael Aquini 		 * Check may be lockless but that's ok as we recheck later.
717bf6bddf1SRafael Aquini 		 * It's possible to migrate LRU pages and balloon pages
718bf6bddf1SRafael Aquini 		 * Skip any other type of page
719bf6bddf1SRafael Aquini 		 */
720bf6bddf1SRafael Aquini 		if (!PageLRU(page)) {
721bf6bddf1SRafael Aquini 			if (unlikely(balloon_page_movable(page))) {
722d6d86c0aSKonstantin Khlebnikov 				if (balloon_page_isolate(page)) {
723bf6bddf1SRafael Aquini 					/* Successfully isolated */
724b6c75016SJoonsoo Kim 					goto isolate_success;
725bf6bddf1SRafael Aquini 				}
726bf6bddf1SRafael Aquini 			}
727bc835011SAndrea Arcangeli 			continue;
728bf6bddf1SRafael Aquini 		}
729bc835011SAndrea Arcangeli 
730bc835011SAndrea Arcangeli 		/*
7312a1402aaSMel Gorman 		 * PageLRU is set. lru_lock normally excludes isolation
7322a1402aaSMel Gorman 		 * splitting and collapsing (collapsing has already happened
7332a1402aaSMel Gorman 		 * if PageLRU is set) but the lock is not necessarily taken
7342a1402aaSMel Gorman 		 * here and it is wasteful to take it just to check transhuge.
7352a1402aaSMel Gorman 		 * Check TransHuge without lock and skip the whole pageblock if
7362a1402aaSMel Gorman 		 * it's either a transhuge or hugetlbfs page, as calling
7372a1402aaSMel Gorman 		 * compound_order() without preventing THP from splitting the
7382a1402aaSMel Gorman 		 * page underneath us may return surprising results.
739bc835011SAndrea Arcangeli 		 */
740bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
7412a1402aaSMel Gorman 			if (!locked)
742edc2ca61SVlastimil Babka 				low_pfn = ALIGN(low_pfn + 1,
743edc2ca61SVlastimil Babka 						pageblock_nr_pages) - 1;
744edc2ca61SVlastimil Babka 			else
7452a1402aaSMel Gorman 				low_pfn += (1 << compound_order(page)) - 1;
746edc2ca61SVlastimil Babka 
7472a1402aaSMel Gorman 			continue;
7482a1402aaSMel Gorman 		}
7492a1402aaSMel Gorman 
750119d6d59SDavid Rientjes 		/*
751119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
752119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
753119d6d59SDavid Rientjes 		 * admittedly racy check.
754119d6d59SDavid Rientjes 		 */
755119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
756119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
757119d6d59SDavid Rientjes 			continue;
758119d6d59SDavid Rientjes 
75969b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
76069b7189fSVlastimil Babka 		if (!locked) {
7618b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&zone->lru_lock,
7628b44d279SVlastimil Babka 								&flags, cc);
7638b44d279SVlastimil Babka 			if (!locked)
7642a1402aaSMel Gorman 				break;
7652a1402aaSMel Gorman 
7662a1402aaSMel Gorman 			/* Recheck PageLRU and PageTransHuge under lock */
7672a1402aaSMel Gorman 			if (!PageLRU(page))
7682a1402aaSMel Gorman 				continue;
7692a1402aaSMel Gorman 			if (PageTransHuge(page)) {
770bc835011SAndrea Arcangeli 				low_pfn += (1 << compound_order(page)) - 1;
771bc835011SAndrea Arcangeli 				continue;
772bc835011SAndrea Arcangeli 			}
77369b7189fSVlastimil Babka 		}
774bc835011SAndrea Arcangeli 
775fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
776fa9add64SHugh Dickins 
777748446bbSMel Gorman 		/* Try isolate the page */
778edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
779748446bbSMel Gorman 			continue;
780748446bbSMel Gorman 
781309381feSSasha Levin 		VM_BUG_ON_PAGE(PageTransCompound(page), page);
782bc835011SAndrea Arcangeli 
783748446bbSMel Gorman 		/* Successfully isolated */
784fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
785b6c75016SJoonsoo Kim 
786b6c75016SJoonsoo Kim isolate_success:
787748446bbSMel Gorman 		list_add(&page->lru, migratelist);
788748446bbSMel Gorman 		cc->nr_migratepages++;
789b7aba698SMel Gorman 		nr_isolated++;
790748446bbSMel Gorman 
791748446bbSMel Gorman 		/* Avoid isolating too much */
79231b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
79331b8384aSHillf Danton 			++low_pfn;
794748446bbSMel Gorman 			break;
795748446bbSMel Gorman 		}
79631b8384aSHillf Danton 	}
797748446bbSMel Gorman 
79899c0fd5eSVlastimil Babka 	/*
79999c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
80099c0fd5eSVlastimil Babka 	 * the range to be scanned.
80199c0fd5eSVlastimil Babka 	 */
80299c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
80399c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
80499c0fd5eSVlastimil Babka 
805c67fe375SMel Gorman 	if (locked)
806c67fe375SMel Gorman 		spin_unlock_irqrestore(&zone->lru_lock, flags);
807748446bbSMel Gorman 
80850b5b094SVlastimil Babka 	/*
80950b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
81050b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
81150b5b094SVlastimil Babka 	 */
81235979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
813edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
814bb13ffebSMel Gorman 
815e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
816e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
817b7aba698SMel Gorman 
818010fc29aSMinchan Kim 	count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
819397487dbSMel Gorman 	if (nr_isolated)
820010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
821397487dbSMel Gorman 
8222fe86e00SMichal Nazarewicz 	return low_pfn;
8232fe86e00SMichal Nazarewicz }
8242fe86e00SMichal Nazarewicz 
825edc2ca61SVlastimil Babka /**
826edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
827edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
828edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
829edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
830edc2ca61SVlastimil Babka  *
831edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
832edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
833edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
834edc2ca61SVlastimil Babka  */
835edc2ca61SVlastimil Babka unsigned long
836edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
837edc2ca61SVlastimil Babka 							unsigned long end_pfn)
838edc2ca61SVlastimil Babka {
839edc2ca61SVlastimil Babka 	unsigned long pfn, block_end_pfn;
840edc2ca61SVlastimil Babka 
841edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
842edc2ca61SVlastimil Babka 	pfn = start_pfn;
843edc2ca61SVlastimil Babka 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
844edc2ca61SVlastimil Babka 
845edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
846edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
847edc2ca61SVlastimil Babka 
848edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
849edc2ca61SVlastimil Babka 
8507d49d886SVlastimil Babka 		if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
851edc2ca61SVlastimil Babka 			continue;
852edc2ca61SVlastimil Babka 
853edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
854edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
855edc2ca61SVlastimil Babka 
856edc2ca61SVlastimil Babka 		/*
857edc2ca61SVlastimil Babka 		 * In case of fatal failure, release everything that might
858edc2ca61SVlastimil Babka 		 * have been isolated in the previous iteration, and signal
859edc2ca61SVlastimil Babka 		 * the failure back to caller.
860edc2ca61SVlastimil Babka 		 */
861edc2ca61SVlastimil Babka 		if (!pfn) {
862edc2ca61SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
863edc2ca61SVlastimil Babka 			cc->nr_migratepages = 0;
864edc2ca61SVlastimil Babka 			break;
865edc2ca61SVlastimil Babka 		}
8666ea41c0cSJoonsoo Kim 
8676ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
8686ea41c0cSJoonsoo Kim 			break;
869edc2ca61SVlastimil Babka 	}
870edc2ca61SVlastimil Babka 	acct_isolated(cc->zone, cc);
871edc2ca61SVlastimil Babka 
872edc2ca61SVlastimil Babka 	return pfn;
873edc2ca61SVlastimil Babka }
874edc2ca61SVlastimil Babka 
875ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
876ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
877018e9a49SAndrew Morton 
878018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
879018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page)
880018e9a49SAndrew Morton {
881018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
882018e9a49SAndrew Morton 	if (PageBuddy(page)) {
883018e9a49SAndrew Morton 		/*
884018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
885018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
886018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
887018e9a49SAndrew Morton 		 */
888018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
889018e9a49SAndrew Morton 			return false;
890018e9a49SAndrew Morton 	}
891018e9a49SAndrew Morton 
892018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
893018e9a49SAndrew Morton 	if (migrate_async_suitable(get_pageblock_migratetype(page)))
894018e9a49SAndrew Morton 		return true;
895018e9a49SAndrew Morton 
896018e9a49SAndrew Morton 	/* Otherwise skip the block */
897018e9a49SAndrew Morton 	return false;
898018e9a49SAndrew Morton }
899018e9a49SAndrew Morton 
900ff9543fdSMichal Nazarewicz /*
901f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
902f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
903f2849aa0SVlastimil Babka  */
904f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
905f2849aa0SVlastimil Babka {
906f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
907f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
908f2849aa0SVlastimil Babka }
909f2849aa0SVlastimil Babka 
910f2849aa0SVlastimil Babka /*
911ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
912ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
913ff9543fdSMichal Nazarewicz  */
914edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
915ff9543fdSMichal Nazarewicz {
916edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
917ff9543fdSMichal Nazarewicz 	struct page *page;
918c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
919e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
920c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
921c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
922ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
9232fe86e00SMichal Nazarewicz 
924ff9543fdSMichal Nazarewicz 	/*
925ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
92649e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
927e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
928e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
929c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
930c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
931c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
93249e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
93349e068f0SVlastimil Babka 	 * is using.
934ff9543fdSMichal Nazarewicz 	 */
935e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
936c96b9e50SVlastimil Babka 	block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
937c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
938c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
9397ed695e0SVlastimil Babka 	low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
9402fe86e00SMichal Nazarewicz 
941ff9543fdSMichal Nazarewicz 	/*
942ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
943ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
944ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
945ff9543fdSMichal Nazarewicz 	 */
946*f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
947c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
948e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
949e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
950ff9543fdSMichal Nazarewicz 
951f6ea3adbSDavid Rientjes 		/*
952f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
953f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
954be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
955f6ea3adbSDavid Rientjes 		 */
956be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
957be976572SVlastimil Babka 						&& compact_should_abort(cc))
958be976572SVlastimil Babka 			break;
959f6ea3adbSDavid Rientjes 
9607d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
9617d49d886SVlastimil Babka 									zone);
9627d49d886SVlastimil Babka 		if (!page)
963ff9543fdSMichal Nazarewicz 			continue;
964ff9543fdSMichal Nazarewicz 
965ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
96668e3e926SLinus Torvalds 		if (!suitable_migration_target(page))
967ff9543fdSMichal Nazarewicz 			continue;
96868e3e926SLinus Torvalds 
969bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
970bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
971bb13ffebSMel Gorman 			continue;
972bb13ffebSMel Gorman 
973e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
974932ff6bbSJoonsoo Kim 		isolate_freepages_block(cc, &isolate_start_pfn,
975c96b9e50SVlastimil Babka 					block_end_pfn, freelist, false);
976ff9543fdSMichal Nazarewicz 
977ff9543fdSMichal Nazarewicz 		/*
978*f5f61a32SVlastimil Babka 		 * If we isolated enough freepages, or aborted due to async
979*f5f61a32SVlastimil Babka 		 * compaction being contended, terminate the loop.
980e14c720eSVlastimil Babka 		 * Remember where the free scanner should restart next time,
981e14c720eSVlastimil Babka 		 * which is where isolate_freepages_block() left off.
982e14c720eSVlastimil Babka 		 * But if it scanned the whole pageblock, isolate_start_pfn
983e14c720eSVlastimil Babka 		 * now points at block_end_pfn, which is the start of the next
984e14c720eSVlastimil Babka 		 * pageblock.
985e14c720eSVlastimil Babka 		 * In that case we will however want to restart at the start
986e14c720eSVlastimil Babka 		 * of the previous pageblock.
987e14c720eSVlastimil Babka 		 */
988*f5f61a32SVlastimil Babka 		if ((cc->nr_freepages >= cc->nr_migratepages)
989*f5f61a32SVlastimil Babka 							|| cc->contended) {
990*f5f61a32SVlastimil Babka 			if (isolate_start_pfn >= block_end_pfn)
991*f5f61a32SVlastimil Babka 				isolate_start_pfn =
992e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
993be976572SVlastimil Babka 			break;
994*f5f61a32SVlastimil Babka 		} else {
995*f5f61a32SVlastimil Babka 			/*
996*f5f61a32SVlastimil Babka 			 * isolate_freepages_block() should not terminate
997*f5f61a32SVlastimil Babka 			 * prematurely unless contended, or isolated enough
998*f5f61a32SVlastimil Babka 			 */
999*f5f61a32SVlastimil Babka 			VM_BUG_ON(isolate_start_pfn < block_end_pfn);
1000*f5f61a32SVlastimil Babka 		}
1001c89511abSMel Gorman 	}
1002ff9543fdSMichal Nazarewicz 
1003ff9543fdSMichal Nazarewicz 	/* split_free_page does not map the pages */
1004ff9543fdSMichal Nazarewicz 	map_pages(freelist);
1005ff9543fdSMichal Nazarewicz 
10067ed695e0SVlastimil Babka 	/*
1007*f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1008*f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1009*f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1010*f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
10117ed695e0SVlastimil Babka 	 */
1012*f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
1013748446bbSMel Gorman }
1014748446bbSMel Gorman 
1015748446bbSMel Gorman /*
1016748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1017748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1018748446bbSMel Gorman  */
1019748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1020748446bbSMel Gorman 					unsigned long data,
1021748446bbSMel Gorman 					int **result)
1022748446bbSMel Gorman {
1023748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1024748446bbSMel Gorman 	struct page *freepage;
1025748446bbSMel Gorman 
1026be976572SVlastimil Babka 	/*
1027be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
1028be976572SVlastimil Babka 	 * contention.
1029be976572SVlastimil Babka 	 */
1030748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1031be976572SVlastimil Babka 		if (!cc->contended)
1032edc2ca61SVlastimil Babka 			isolate_freepages(cc);
1033748446bbSMel Gorman 
1034748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1035748446bbSMel Gorman 			return NULL;
1036748446bbSMel Gorman 	}
1037748446bbSMel Gorman 
1038748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1039748446bbSMel Gorman 	list_del(&freepage->lru);
1040748446bbSMel Gorman 	cc->nr_freepages--;
1041748446bbSMel Gorman 
1042748446bbSMel Gorman 	return freepage;
1043748446bbSMel Gorman }
1044748446bbSMel Gorman 
1045748446bbSMel Gorman /*
1046d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1047d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1048d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1049d53aea3dSDavid Rientjes  */
1050d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1051d53aea3dSDavid Rientjes {
1052d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1053d53aea3dSDavid Rientjes 
1054d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1055d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1056d53aea3dSDavid Rientjes }
1057d53aea3dSDavid Rientjes 
1058ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1059ff9543fdSMichal Nazarewicz typedef enum {
1060ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1061ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1062ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1063ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1064ff9543fdSMichal Nazarewicz 
1065ff9543fdSMichal Nazarewicz /*
10665bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
10675bbe3547SEric B Munson  * compactable pages.
10685bbe3547SEric B Munson  */
10695bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
10705bbe3547SEric B Munson 
10715bbe3547SEric B Munson /*
1072edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1073edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1074edc2ca61SVlastimil Babka  * compact_control.
1075ff9543fdSMichal Nazarewicz  */
1076ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1077ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1078ff9543fdSMichal Nazarewicz {
1079ff9543fdSMichal Nazarewicz 	unsigned long low_pfn, end_pfn;
1080edc2ca61SVlastimil Babka 	struct page *page;
1081edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
10825bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
1083edc2ca61SVlastimil Babka 		(cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1084ff9543fdSMichal Nazarewicz 
1085edc2ca61SVlastimil Babka 	/*
1086edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
1087edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
1088edc2ca61SVlastimil Babka 	 */
1089edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
1090ff9543fdSMichal Nazarewicz 
1091ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
1092a9aacbccSMel Gorman 	end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
1093ff9543fdSMichal Nazarewicz 
1094edc2ca61SVlastimil Babka 	/*
1095edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1096edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1097edc2ca61SVlastimil Babka 	 */
1098edc2ca61SVlastimil Babka 	for (; end_pfn <= cc->free_pfn;
1099edc2ca61SVlastimil Babka 			low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
1100edc2ca61SVlastimil Babka 
1101edc2ca61SVlastimil Babka 		/*
1102edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1103edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1104edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
1105edc2ca61SVlastimil Babka 		 */
1106edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1107edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
1108edc2ca61SVlastimil Babka 			break;
1109edc2ca61SVlastimil Babka 
11107d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
11117d49d886SVlastimil Babka 		if (!page)
1112edc2ca61SVlastimil Babka 			continue;
1113edc2ca61SVlastimil Babka 
1114edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1115edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1116edc2ca61SVlastimil Babka 			continue;
1117edc2ca61SVlastimil Babka 
1118edc2ca61SVlastimil Babka 		/*
1119edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1120edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1121edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1122edc2ca61SVlastimil Babka 		 */
1123edc2ca61SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC &&
1124edc2ca61SVlastimil Babka 		    !migrate_async_suitable(get_pageblock_migratetype(page)))
1125edc2ca61SVlastimil Babka 			continue;
1126ff9543fdSMichal Nazarewicz 
1127ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1128edc2ca61SVlastimil Babka 		low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1129edc2ca61SVlastimil Babka 								isolate_mode);
1130edc2ca61SVlastimil Babka 
1131ff59909aSHugh Dickins 		if (!low_pfn || cc->contended) {
1132ff59909aSHugh Dickins 			acct_isolated(zone, cc);
1133ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1134ff59909aSHugh Dickins 		}
1135ff9543fdSMichal Nazarewicz 
1136edc2ca61SVlastimil Babka 		/*
1137edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1138edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1139edc2ca61SVlastimil Babka 		 * continue or not.
1140edc2ca61SVlastimil Babka 		 */
1141edc2ca61SVlastimil Babka 		break;
1142edc2ca61SVlastimil Babka 	}
1143edc2ca61SVlastimil Babka 
1144edc2ca61SVlastimil Babka 	acct_isolated(zone, cc);
1145f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1146f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1147ff9543fdSMichal Nazarewicz 
1148edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1149ff9543fdSMichal Nazarewicz }
1150ff9543fdSMichal Nazarewicz 
1151837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc,
11526d7ce559SDavid Rientjes 			    const int migratetype)
1153748446bbSMel Gorman {
11548fb74b9fSMel Gorman 	unsigned int order;
11555a03b051SAndrea Arcangeli 	unsigned long watermark;
115656de7263SMel Gorman 
1157be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
1158748446bbSMel Gorman 		return COMPACT_PARTIAL;
1159748446bbSMel Gorman 
1160753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1161f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
116255b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
116335979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
116435979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
116555b7c4c9SVlastimil Babka 		zone->compact_cached_free_pfn = zone_end_pfn(zone);
116655b7c4c9SVlastimil Babka 
116762997027SMel Gorman 		/*
116862997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
116962997027SMel Gorman 		 * by kswapd when it goes to sleep. kswapd does not set the
117062997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
117162997027SMel Gorman 		 * based on an allocation request.
117262997027SMel Gorman 		 */
117362997027SMel Gorman 		if (!current_is_kswapd())
117462997027SMel Gorman 			zone->compact_blockskip_flush = true;
117562997027SMel Gorman 
1176748446bbSMel Gorman 		return COMPACT_COMPLETE;
1177bb13ffebSMel Gorman 	}
1178748446bbSMel Gorman 
117982478fb7SJohannes Weiner 	/*
118082478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
118182478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
118282478fb7SJohannes Weiner 	 */
118356de7263SMel Gorman 	if (cc->order == -1)
118456de7263SMel Gorman 		return COMPACT_CONTINUE;
118556de7263SMel Gorman 
11863957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
11873957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
11883957c776SMichal Hocko 
1189ebff3980SVlastimil Babka 	if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1190ebff3980SVlastimil Babka 							cc->alloc_flags))
11913957c776SMichal Hocko 		return COMPACT_CONTINUE;
11923957c776SMichal Hocko 
119356de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
119456de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
11958fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
11962149cdaeSJoonsoo Kim 		bool can_steal;
11978fb74b9fSMel Gorman 
119856de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
11996d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
120056de7263SMel Gorman 			return COMPACT_PARTIAL;
120156de7263SMel Gorman 
12022149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
12032149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
12042149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
12052149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
12062149cdaeSJoonsoo Kim 			return COMPACT_PARTIAL;
12072149cdaeSJoonsoo Kim #endif
12082149cdaeSJoonsoo Kim 		/*
12092149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
12102149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
12112149cdaeSJoonsoo Kim 		 */
12122149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
12132149cdaeSJoonsoo Kim 						true, &can_steal) != -1)
121456de7263SMel Gorman 			return COMPACT_PARTIAL;
121556de7263SMel Gorman 	}
121656de7263SMel Gorman 
1217837d026dSJoonsoo Kim 	return COMPACT_NO_SUITABLE_PAGE;
1218837d026dSJoonsoo Kim }
1219837d026dSJoonsoo Kim 
1220837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc,
1221837d026dSJoonsoo Kim 			    const int migratetype)
1222837d026dSJoonsoo Kim {
1223837d026dSJoonsoo Kim 	int ret;
1224837d026dSJoonsoo Kim 
1225837d026dSJoonsoo Kim 	ret = __compact_finished(zone, cc, migratetype);
1226837d026dSJoonsoo Kim 	trace_mm_compaction_finished(zone, cc->order, ret);
1227837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1228837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1229837d026dSJoonsoo Kim 
1230837d026dSJoonsoo Kim 	return ret;
1231748446bbSMel Gorman }
1232748446bbSMel Gorman 
12333e7d3449SMel Gorman /*
12343e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
12353e7d3449SMel Gorman  * Returns
12363e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
12373e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
12383e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
12393e7d3449SMel Gorman  */
1240837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order,
1241ebff3980SVlastimil Babka 					int alloc_flags, int classzone_idx)
12423e7d3449SMel Gorman {
12433e7d3449SMel Gorman 	int fragindex;
12443e7d3449SMel Gorman 	unsigned long watermark;
12453e7d3449SMel Gorman 
12463e7d3449SMel Gorman 	/*
12473957c776SMichal Hocko 	 * order == -1 is expected when compacting via
12483957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
12493957c776SMichal Hocko 	 */
12503957c776SMichal Hocko 	if (order == -1)
12513957c776SMichal Hocko 		return COMPACT_CONTINUE;
12523957c776SMichal Hocko 
1253ebff3980SVlastimil Babka 	watermark = low_wmark_pages(zone);
1254ebff3980SVlastimil Babka 	/*
1255ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1256ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1257ebff3980SVlastimil Babka 	 */
1258ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1259ebff3980SVlastimil Babka 								alloc_flags))
1260ebff3980SVlastimil Babka 		return COMPACT_PARTIAL;
1261ebff3980SVlastimil Babka 
12623957c776SMichal Hocko 	/*
12633e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
12643e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
12653e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
12663e7d3449SMel Gorman 	 */
1267ebff3980SVlastimil Babka 	watermark += (2UL << order);
1268ebff3980SVlastimil Babka 	if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
12693e7d3449SMel Gorman 		return COMPACT_SKIPPED;
12703e7d3449SMel Gorman 
12713e7d3449SMel Gorman 	/*
12723e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
12733e7d3449SMel Gorman 	 * low memory or external fragmentation
12743e7d3449SMel Gorman 	 *
1275ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
1276ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
12773e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
12783e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
12793e7d3449SMel Gorman 	 *
12803e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
12813e7d3449SMel Gorman 	 */
12823e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
12833e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1284837d026dSJoonsoo Kim 		return COMPACT_NOT_SUITABLE_ZONE;
12853e7d3449SMel Gorman 
12863e7d3449SMel Gorman 	return COMPACT_CONTINUE;
12873e7d3449SMel Gorman }
12883e7d3449SMel Gorman 
1289837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order,
1290837d026dSJoonsoo Kim 					int alloc_flags, int classzone_idx)
1291837d026dSJoonsoo Kim {
1292837d026dSJoonsoo Kim 	unsigned long ret;
1293837d026dSJoonsoo Kim 
1294837d026dSJoonsoo Kim 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx);
1295837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
1296837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
1297837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
1298837d026dSJoonsoo Kim 
1299837d026dSJoonsoo Kim 	return ret;
1300837d026dSJoonsoo Kim }
1301837d026dSJoonsoo Kim 
1302748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
1303748446bbSMel Gorman {
1304748446bbSMel Gorman 	int ret;
1305c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1306108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
13076d7ce559SDavid Rientjes 	const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1308e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1309fdaf7f5cSVlastimil Babka 	unsigned long last_migrated_pfn = 0;
1310748446bbSMel Gorman 
1311ebff3980SVlastimil Babka 	ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1312ebff3980SVlastimil Babka 							cc->classzone_idx);
13133e7d3449SMel Gorman 	switch (ret) {
13143e7d3449SMel Gorman 	case COMPACT_PARTIAL:
13153e7d3449SMel Gorman 	case COMPACT_SKIPPED:
13163e7d3449SMel Gorman 		/* Compaction is likely to fail */
13173e7d3449SMel Gorman 		return ret;
13183e7d3449SMel Gorman 	case COMPACT_CONTINUE:
13193e7d3449SMel Gorman 		/* Fall through to compaction */
13203e7d3449SMel Gorman 		;
13213e7d3449SMel Gorman 	}
13223e7d3449SMel Gorman 
1323c89511abSMel Gorman 	/*
1324d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1325d3132e4bSVlastimil Babka 	 * is about to be retried after being deferred. kswapd does not do
1326d3132e4bSVlastimil Babka 	 * this reset as it'll reset the cached information when going to sleep.
1327d3132e4bSVlastimil Babka 	 */
1328d3132e4bSVlastimil Babka 	if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1329d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1330d3132e4bSVlastimil Babka 
1331d3132e4bSVlastimil Babka 	/*
1332c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
1333c89511abSMel Gorman 	 * information on where the scanners should start but check that it
1334c89511abSMel Gorman 	 * is initialised by ensuring the values are within zone boundaries.
1335c89511abSMel Gorman 	 */
1336e0b9daebSDavid Rientjes 	cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1337c89511abSMel Gorman 	cc->free_pfn = zone->compact_cached_free_pfn;
1338c89511abSMel Gorman 	if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1339c89511abSMel Gorman 		cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1340c89511abSMel Gorman 		zone->compact_cached_free_pfn = cc->free_pfn;
1341c89511abSMel Gorman 	}
1342c89511abSMel Gorman 	if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1343c89511abSMel Gorman 		cc->migrate_pfn = start_pfn;
134435979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
134535979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1346c89511abSMel Gorman 	}
1347748446bbSMel Gorman 
134816c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
134916c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
13500eb927c0SMel Gorman 
1351748446bbSMel Gorman 	migrate_prep_local();
1352748446bbSMel Gorman 
13536d7ce559SDavid Rientjes 	while ((ret = compact_finished(zone, cc, migratetype)) ==
13546d7ce559SDavid Rientjes 						COMPACT_CONTINUE) {
13559d502c1cSMinchan Kim 		int err;
1356fdaf7f5cSVlastimil Babka 		unsigned long isolate_start_pfn = cc->migrate_pfn;
1357748446bbSMel Gorman 
1358f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1359f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
1360f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
13615733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1362e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1363f9e35b3bSMel Gorman 			goto out;
1364f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1365fdaf7f5cSVlastimil Babka 			/*
1366fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
1367fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
1368fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
1369fdaf7f5cSVlastimil Babka 			 */
1370fdaf7f5cSVlastimil Babka 			goto check_drain;
1371f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1372f9e35b3bSMel Gorman 			;
1373f9e35b3bSMel Gorman 		}
1374748446bbSMel Gorman 
1375d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1376e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
13777b2a2d4aSMel Gorman 				MR_COMPACTION);
1378748446bbSMel Gorman 
1379f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1380f8c9301fSVlastimil Babka 							&cc->migratepages);
1381748446bbSMel Gorman 
1382f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1383f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
13849d502c1cSMinchan Kim 		if (err) {
13855733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
13867ed695e0SVlastimil Babka 			/*
13877ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
13887ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
13897ed695e0SVlastimil Babka 			 */
1390f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
13914bf2bba3SDavid Rientjes 				ret = COMPACT_PARTIAL;
13924bf2bba3SDavid Rientjes 				goto out;
1393748446bbSMel Gorman 			}
13944bf2bba3SDavid Rientjes 		}
1395fdaf7f5cSVlastimil Babka 
1396fdaf7f5cSVlastimil Babka 		/*
1397fdaf7f5cSVlastimil Babka 		 * Record where we could have freed pages by migration and not
1398fdaf7f5cSVlastimil Babka 		 * yet flushed them to buddy allocator. We use the pfn that
1399fdaf7f5cSVlastimil Babka 		 * isolate_migratepages() started from in this loop iteration
1400fdaf7f5cSVlastimil Babka 		 * - this is the lowest page that could have been isolated and
1401fdaf7f5cSVlastimil Babka 		 * then freed by migration.
1402fdaf7f5cSVlastimil Babka 		 */
1403fdaf7f5cSVlastimil Babka 		if (!last_migrated_pfn)
1404fdaf7f5cSVlastimil Babka 			last_migrated_pfn = isolate_start_pfn;
1405fdaf7f5cSVlastimil Babka 
1406fdaf7f5cSVlastimil Babka check_drain:
1407fdaf7f5cSVlastimil Babka 		/*
1408fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
1409fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
1410fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
1411fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
1412fdaf7f5cSVlastimil Babka 		 * would succeed.
1413fdaf7f5cSVlastimil Babka 		 */
1414fdaf7f5cSVlastimil Babka 		if (cc->order > 0 && last_migrated_pfn) {
1415fdaf7f5cSVlastimil Babka 			int cpu;
1416fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
1417fdaf7f5cSVlastimil Babka 				cc->migrate_pfn & ~((1UL << cc->order) - 1);
1418fdaf7f5cSVlastimil Babka 
1419fdaf7f5cSVlastimil Babka 			if (last_migrated_pfn < current_block_start) {
1420fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
1421fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
1422fdaf7f5cSVlastimil Babka 				drain_local_pages(zone);
1423fdaf7f5cSVlastimil Babka 				put_cpu();
1424fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
1425fdaf7f5cSVlastimil Babka 				last_migrated_pfn = 0;
1426fdaf7f5cSVlastimil Babka 			}
1427fdaf7f5cSVlastimil Babka 		}
1428fdaf7f5cSVlastimil Babka 
1429748446bbSMel Gorman 	}
1430748446bbSMel Gorman 
1431f9e35b3bSMel Gorman out:
14326bace090SVlastimil Babka 	/*
14336bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
14346bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
14356bace090SVlastimil Babka 	 */
14366bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
14376bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
14386bace090SVlastimil Babka 
14396bace090SVlastimil Babka 		cc->nr_freepages = 0;
14406bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
14416bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
14426bace090SVlastimil Babka 		free_pfn &= ~(pageblock_nr_pages-1);
14436bace090SVlastimil Babka 		/*
14446bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
14456bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
14466bace090SVlastimil Babka 		 */
14476bace090SVlastimil Babka 		if (free_pfn > zone->compact_cached_free_pfn)
14486bace090SVlastimil Babka 			zone->compact_cached_free_pfn = free_pfn;
14496bace090SVlastimil Babka 	}
1450748446bbSMel Gorman 
145116c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
145216c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
14530eb927c0SMel Gorman 
1454748446bbSMel Gorman 	return ret;
1455748446bbSMel Gorman }
145676ab0f53SMel Gorman 
1457e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order,
1458ebff3980SVlastimil Babka 		gfp_t gfp_mask, enum migrate_mode mode, int *contended,
1459ebff3980SVlastimil Babka 		int alloc_flags, int classzone_idx)
146056de7263SMel Gorman {
1461e64c5237SShaohua Li 	unsigned long ret;
146256de7263SMel Gorman 	struct compact_control cc = {
146356de7263SMel Gorman 		.nr_freepages = 0,
146456de7263SMel Gorman 		.nr_migratepages = 0,
146556de7263SMel Gorman 		.order = order,
14666d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
146756de7263SMel Gorman 		.zone = zone,
1468e0b9daebSDavid Rientjes 		.mode = mode,
1469ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
1470ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
147156de7263SMel Gorman 	};
147256de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
147356de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
147456de7263SMel Gorman 
1475e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1476e64c5237SShaohua Li 
1477e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1478e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1479e64c5237SShaohua Li 
1480e64c5237SShaohua Li 	*contended = cc.contended;
1481e64c5237SShaohua Li 	return ret;
148256de7263SMel Gorman }
148356de7263SMel Gorman 
14845e771905SMel Gorman int sysctl_extfrag_threshold = 500;
14855e771905SMel Gorman 
148656de7263SMel Gorman /**
148756de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
148856de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
14891a6d53a1SVlastimil Babka  * @order: The order of the current allocation
14901a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
14911a6d53a1SVlastimil Babka  * @ac: The context of current allocation
1492e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
14931f9efdefSVlastimil Babka  * @contended: Return value that determines if compaction was aborted due to
14941f9efdefSVlastimil Babka  *	       need_resched() or lock contention
149556de7263SMel Gorman  *
149656de7263SMel Gorman  * This is the main entry point for direct page compaction.
149756de7263SMel Gorman  */
14981a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
14991a6d53a1SVlastimil Babka 			int alloc_flags, const struct alloc_context *ac,
15001a6d53a1SVlastimil Babka 			enum migrate_mode mode, int *contended)
150156de7263SMel Gorman {
150256de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
150356de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
150456de7263SMel Gorman 	struct zoneref *z;
150556de7263SMel Gorman 	struct zone *zone;
150653853e2dSVlastimil Babka 	int rc = COMPACT_DEFERRED;
15071f9efdefSVlastimil Babka 	int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
15081f9efdefSVlastimil Babka 
15091f9efdefSVlastimil Babka 	*contended = COMPACT_CONTENDED_NONE;
151056de7263SMel Gorman 
15114ffb6335SMel Gorman 	/* Check if the GFP flags allow compaction */
1512c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
151353853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
151456de7263SMel Gorman 
1515837d026dSJoonsoo Kim 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode);
1516837d026dSJoonsoo Kim 
151756de7263SMel Gorman 	/* Compact each zone in the list */
15181a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
15191a6d53a1SVlastimil Babka 								ac->nodemask) {
152056de7263SMel Gorman 		int status;
15211f9efdefSVlastimil Babka 		int zone_contended;
152256de7263SMel Gorman 
152353853e2dSVlastimil Babka 		if (compaction_deferred(zone, order))
152453853e2dSVlastimil Babka 			continue;
152553853e2dSVlastimil Babka 
1526e0b9daebSDavid Rientjes 		status = compact_zone_order(zone, order, gfp_mask, mode,
15271a6d53a1SVlastimil Babka 				&zone_contended, alloc_flags,
15281a6d53a1SVlastimil Babka 				ac->classzone_idx);
152956de7263SMel Gorman 		rc = max(status, rc);
15301f9efdefSVlastimil Babka 		/*
15311f9efdefSVlastimil Babka 		 * It takes at least one zone that wasn't lock contended
15321f9efdefSVlastimil Babka 		 * to clear all_zones_contended.
15331f9efdefSVlastimil Babka 		 */
15341f9efdefSVlastimil Babka 		all_zones_contended &= zone_contended;
153556de7263SMel Gorman 
15363e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
1537ebff3980SVlastimil Babka 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
15381a6d53a1SVlastimil Babka 					ac->classzone_idx, alloc_flags)) {
153953853e2dSVlastimil Babka 			/*
154053853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
154153853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
154253853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
154353853e2dSVlastimil Babka 			 * succeeds in this zone.
154453853e2dSVlastimil Babka 			 */
154553853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
15461f9efdefSVlastimil Babka 			/*
15471f9efdefSVlastimil Babka 			 * It is possible that async compaction aborted due to
15481f9efdefSVlastimil Babka 			 * need_resched() and the watermarks were ok thanks to
15491f9efdefSVlastimil Babka 			 * somebody else freeing memory. The allocation can
15501f9efdefSVlastimil Babka 			 * however still fail so we better signal the
15511f9efdefSVlastimil Babka 			 * need_resched() contention anyway (this will not
15521f9efdefSVlastimil Babka 			 * prevent the allocation attempt).
15531f9efdefSVlastimil Babka 			 */
15541f9efdefSVlastimil Babka 			if (zone_contended == COMPACT_CONTENDED_SCHED)
15551f9efdefSVlastimil Babka 				*contended = COMPACT_CONTENDED_SCHED;
15561f9efdefSVlastimil Babka 
15571f9efdefSVlastimil Babka 			goto break_loop;
15581f9efdefSVlastimil Babka 		}
15591f9efdefSVlastimil Babka 
1560f8669795SVlastimil Babka 		if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
156153853e2dSVlastimil Babka 			/*
156253853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
156353853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
156453853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
156553853e2dSVlastimil Babka 			 */
156653853e2dSVlastimil Babka 			defer_compaction(zone, order);
156753853e2dSVlastimil Babka 		}
15681f9efdefSVlastimil Babka 
15691f9efdefSVlastimil Babka 		/*
15701f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
15711f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
15721f9efdefSVlastimil Babka 		 * case do not try further zones and signal need_resched()
15731f9efdefSVlastimil Babka 		 * contention.
15741f9efdefSVlastimil Babka 		 */
15751f9efdefSVlastimil Babka 		if ((zone_contended == COMPACT_CONTENDED_SCHED)
15761f9efdefSVlastimil Babka 					|| fatal_signal_pending(current)) {
15771f9efdefSVlastimil Babka 			*contended = COMPACT_CONTENDED_SCHED;
15781f9efdefSVlastimil Babka 			goto break_loop;
157956de7263SMel Gorman 		}
158056de7263SMel Gorman 
15811f9efdefSVlastimil Babka 		continue;
15821f9efdefSVlastimil Babka break_loop:
15831f9efdefSVlastimil Babka 		/*
15841f9efdefSVlastimil Babka 		 * We might not have tried all the zones, so  be conservative
15851f9efdefSVlastimil Babka 		 * and assume they are not all lock contended.
15861f9efdefSVlastimil Babka 		 */
15871f9efdefSVlastimil Babka 		all_zones_contended = 0;
15881f9efdefSVlastimil Babka 		break;
15891f9efdefSVlastimil Babka 	}
15901f9efdefSVlastimil Babka 
15911f9efdefSVlastimil Babka 	/*
15921f9efdefSVlastimil Babka 	 * If at least one zone wasn't deferred or skipped, we report if all
15931f9efdefSVlastimil Babka 	 * zones that were tried were lock contended.
15941f9efdefSVlastimil Babka 	 */
15951f9efdefSVlastimil Babka 	if (rc > COMPACT_SKIPPED && all_zones_contended)
15961f9efdefSVlastimil Babka 		*contended = COMPACT_CONTENDED_LOCK;
15971f9efdefSVlastimil Babka 
159856de7263SMel Gorman 	return rc;
159956de7263SMel Gorman }
160056de7263SMel Gorman 
160156de7263SMel Gorman 
160276ab0f53SMel Gorman /* Compact all zones within a node */
16037103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
160476ab0f53SMel Gorman {
160576ab0f53SMel Gorman 	int zoneid;
160676ab0f53SMel Gorman 	struct zone *zone;
160776ab0f53SMel Gorman 
160876ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
160976ab0f53SMel Gorman 
161076ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
161176ab0f53SMel Gorman 		if (!populated_zone(zone))
161276ab0f53SMel Gorman 			continue;
161376ab0f53SMel Gorman 
16147be62de9SRik van Riel 		cc->nr_freepages = 0;
16157be62de9SRik van Riel 		cc->nr_migratepages = 0;
16167be62de9SRik van Riel 		cc->zone = zone;
16177be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
16187be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
161976ab0f53SMel Gorman 
1620195b0c60SGioh Kim 		/*
1621195b0c60SGioh Kim 		 * When called via /proc/sys/vm/compact_memory
1622195b0c60SGioh Kim 		 * this makes sure we compact the whole zone regardless of
1623195b0c60SGioh Kim 		 * cached scanner positions.
1624195b0c60SGioh Kim 		 */
1625195b0c60SGioh Kim 		if (cc->order == -1)
1626195b0c60SGioh Kim 			__reset_isolation_suitable(zone);
1627195b0c60SGioh Kim 
1628aad6ec37SDan Carpenter 		if (cc->order == -1 || !compaction_deferred(zone, cc->order))
16297be62de9SRik van Riel 			compact_zone(zone, cc);
163076ab0f53SMel Gorman 
1631aff62249SRik van Riel 		if (cc->order > 0) {
1632de6c60a6SVlastimil Babka 			if (zone_watermark_ok(zone, cc->order,
1633de6c60a6SVlastimil Babka 						low_wmark_pages(zone), 0, 0))
1634de6c60a6SVlastimil Babka 				compaction_defer_reset(zone, cc->order, false);
1635aff62249SRik van Riel 		}
1636aff62249SRik van Riel 
16377be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->freepages));
16387be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->migratepages));
163976ab0f53SMel Gorman 	}
164076ab0f53SMel Gorman }
164176ab0f53SMel Gorman 
16427103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order)
16437be62de9SRik van Riel {
16447be62de9SRik van Riel 	struct compact_control cc = {
16457be62de9SRik van Riel 		.order = order,
1646e0b9daebSDavid Rientjes 		.mode = MIGRATE_ASYNC,
16477be62de9SRik van Riel 	};
16487be62de9SRik van Riel 
16493a7200afSMel Gorman 	if (!order)
16503a7200afSMel Gorman 		return;
16513a7200afSMel Gorman 
16527103f16dSAndrew Morton 	__compact_pgdat(pgdat, &cc);
16537be62de9SRik van Riel }
16547be62de9SRik van Riel 
16557103f16dSAndrew Morton static void compact_node(int nid)
16567be62de9SRik van Riel {
16577be62de9SRik van Riel 	struct compact_control cc = {
16587be62de9SRik van Riel 		.order = -1,
1659e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
166091ca9186SDavid Rientjes 		.ignore_skip_hint = true,
16617be62de9SRik van Riel 	};
16627be62de9SRik van Riel 
16637103f16dSAndrew Morton 	__compact_pgdat(NODE_DATA(nid), &cc);
16647be62de9SRik van Riel }
16657be62de9SRik van Riel 
166676ab0f53SMel Gorman /* Compact all nodes in the system */
16677964c06dSJason Liu static void compact_nodes(void)
166876ab0f53SMel Gorman {
166976ab0f53SMel Gorman 	int nid;
167076ab0f53SMel Gorman 
16718575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
16728575ec29SHugh Dickins 	lru_add_drain_all();
16738575ec29SHugh Dickins 
167476ab0f53SMel Gorman 	for_each_online_node(nid)
167576ab0f53SMel Gorman 		compact_node(nid);
167676ab0f53SMel Gorman }
167776ab0f53SMel Gorman 
167876ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
167976ab0f53SMel Gorman int sysctl_compact_memory;
168076ab0f53SMel Gorman 
168176ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
168276ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
168376ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
168476ab0f53SMel Gorman {
168576ab0f53SMel Gorman 	if (write)
16867964c06dSJason Liu 		compact_nodes();
168776ab0f53SMel Gorman 
168876ab0f53SMel Gorman 	return 0;
168976ab0f53SMel Gorman }
1690ed4a6d7fSMel Gorman 
16915e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
16925e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
16935e771905SMel Gorman {
16945e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
16955e771905SMel Gorman 
16965e771905SMel Gorman 	return 0;
16975e771905SMel Gorman }
16985e771905SMel Gorman 
1699ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
170074e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
170110fbcf4cSKay Sievers 			struct device_attribute *attr,
1702ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1703ed4a6d7fSMel Gorman {
17048575ec29SHugh Dickins 	int nid = dev->id;
17058575ec29SHugh Dickins 
17068575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
17078575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
17088575ec29SHugh Dickins 		lru_add_drain_all();
17098575ec29SHugh Dickins 
17108575ec29SHugh Dickins 		compact_node(nid);
17118575ec29SHugh Dickins 	}
1712ed4a6d7fSMel Gorman 
1713ed4a6d7fSMel Gorman 	return count;
1714ed4a6d7fSMel Gorman }
171510fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1716ed4a6d7fSMel Gorman 
1717ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1718ed4a6d7fSMel Gorman {
171910fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1720ed4a6d7fSMel Gorman }
1721ed4a6d7fSMel Gorman 
1722ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1723ed4a6d7fSMel Gorman {
172410fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1725ed4a6d7fSMel Gorman }
1726ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1727ff9543fdSMichal Nazarewicz 
1728ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
1729