xref: /openbmc/linux/mm/compaction.c (revision d3c85bad89b9153df741af14ad859ee49677f00d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2748446bbSMel Gorman /*
3748446bbSMel Gorman  * linux/mm/compaction.c
4748446bbSMel Gorman  *
5748446bbSMel Gorman  * Memory compaction for the reduction of external fragmentation. Note that
6748446bbSMel Gorman  * this heavily depends upon page migration to do all the real heavy
7748446bbSMel Gorman  * lifting
8748446bbSMel Gorman  *
9748446bbSMel Gorman  * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
10748446bbSMel Gorman  */
11698b1b30SVlastimil Babka #include <linux/cpu.h>
12748446bbSMel Gorman #include <linux/swap.h>
13748446bbSMel Gorman #include <linux/migrate.h>
14748446bbSMel Gorman #include <linux/compaction.h>
15748446bbSMel Gorman #include <linux/mm_inline.h>
16174cd4b1SIngo Molnar #include <linux/sched/signal.h>
17748446bbSMel Gorman #include <linux/backing-dev.h>
1876ab0f53SMel Gorman #include <linux/sysctl.h>
19ed4a6d7fSMel Gorman #include <linux/sysfs.h>
20194159fbSMinchan Kim #include <linux/page-isolation.h>
21b8c73fc2SAndrey Ryabinin #include <linux/kasan.h>
22698b1b30SVlastimil Babka #include <linux/kthread.h>
23698b1b30SVlastimil Babka #include <linux/freezer.h>
2483358eceSJoonsoo Kim #include <linux/page_owner.h>
25748446bbSMel Gorman #include "internal.h"
26748446bbSMel Gorman 
27010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
28010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
29010fc29aSMinchan Kim {
30010fc29aSMinchan Kim 	count_vm_event(item);
31010fc29aSMinchan Kim }
32010fc29aSMinchan Kim 
33010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
34010fc29aSMinchan Kim {
35010fc29aSMinchan Kim 	count_vm_events(item, delta);
36010fc29aSMinchan Kim }
37010fc29aSMinchan Kim #else
38010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
39010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
40010fc29aSMinchan Kim #endif
41010fc29aSMinchan Kim 
42ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
43ff9543fdSMichal Nazarewicz 
44b7aba698SMel Gorman #define CREATE_TRACE_POINTS
45b7aba698SMel Gorman #include <trace/events/compaction.h>
46b7aba698SMel Gorman 
4706b6640aSVlastimil Babka #define block_start_pfn(pfn, order)	round_down(pfn, 1UL << (order))
4806b6640aSVlastimil Babka #define block_end_pfn(pfn, order)	ALIGN((pfn) + 1, 1UL << (order))
4906b6640aSVlastimil Babka #define pageblock_start_pfn(pfn)	block_start_pfn(pfn, pageblock_order)
5006b6640aSVlastimil Babka #define pageblock_end_pfn(pfn)		block_end_pfn(pfn, pageblock_order)
5106b6640aSVlastimil Babka 
52748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
53748446bbSMel Gorman {
54748446bbSMel Gorman 	struct page *page, *next;
556bace090SVlastimil Babka 	unsigned long high_pfn = 0;
56748446bbSMel Gorman 
57748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
586bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
59748446bbSMel Gorman 		list_del(&page->lru);
60748446bbSMel Gorman 		__free_page(page);
616bace090SVlastimil Babka 		if (pfn > high_pfn)
626bace090SVlastimil Babka 			high_pfn = pfn;
63748446bbSMel Gorman 	}
64748446bbSMel Gorman 
656bace090SVlastimil Babka 	return high_pfn;
66748446bbSMel Gorman }
67748446bbSMel Gorman 
68ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
69ff9543fdSMichal Nazarewicz {
7066c64223SJoonsoo Kim 	unsigned int i, order, nr_pages;
7166c64223SJoonsoo Kim 	struct page *page, *next;
7266c64223SJoonsoo Kim 	LIST_HEAD(tmp_list);
73ff9543fdSMichal Nazarewicz 
7466c64223SJoonsoo Kim 	list_for_each_entry_safe(page, next, list, lru) {
7566c64223SJoonsoo Kim 		list_del(&page->lru);
7666c64223SJoonsoo Kim 
7766c64223SJoonsoo Kim 		order = page_private(page);
7866c64223SJoonsoo Kim 		nr_pages = 1 << order;
7966c64223SJoonsoo Kim 
8046f24fd8SJoonsoo Kim 		post_alloc_hook(page, order, __GFP_MOVABLE);
8166c64223SJoonsoo Kim 		if (order)
8266c64223SJoonsoo Kim 			split_page(page, order);
8366c64223SJoonsoo Kim 
8466c64223SJoonsoo Kim 		for (i = 0; i < nr_pages; i++) {
8566c64223SJoonsoo Kim 			list_add(&page->lru, &tmp_list);
8666c64223SJoonsoo Kim 			page++;
87ff9543fdSMichal Nazarewicz 		}
88ff9543fdSMichal Nazarewicz 	}
89ff9543fdSMichal Nazarewicz 
9066c64223SJoonsoo Kim 	list_splice(&tmp_list, list);
9166c64223SJoonsoo Kim }
9266c64223SJoonsoo Kim 
93bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
9424e2716fSJoonsoo Kim 
95bda807d4SMinchan Kim int PageMovable(struct page *page)
96bda807d4SMinchan Kim {
97bda807d4SMinchan Kim 	struct address_space *mapping;
98bda807d4SMinchan Kim 
99bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
100bda807d4SMinchan Kim 	if (!__PageMovable(page))
101bda807d4SMinchan Kim 		return 0;
102bda807d4SMinchan Kim 
103bda807d4SMinchan Kim 	mapping = page_mapping(page);
104bda807d4SMinchan Kim 	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
105bda807d4SMinchan Kim 		return 1;
106bda807d4SMinchan Kim 
107bda807d4SMinchan Kim 	return 0;
108bda807d4SMinchan Kim }
109bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable);
110bda807d4SMinchan Kim 
111bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping)
112bda807d4SMinchan Kim {
113bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
114bda807d4SMinchan Kim 	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
115bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
116bda807d4SMinchan Kim }
117bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable);
118bda807d4SMinchan Kim 
119bda807d4SMinchan Kim void __ClearPageMovable(struct page *page)
120bda807d4SMinchan Kim {
121bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
122bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageMovable(page), page);
123bda807d4SMinchan Kim 	/*
124bda807d4SMinchan Kim 	 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
125bda807d4SMinchan Kim 	 * flag so that VM can catch up released page by driver after isolation.
126bda807d4SMinchan Kim 	 * With it, VM migration doesn't try to put it back.
127bda807d4SMinchan Kim 	 */
128bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)page->mapping &
129bda807d4SMinchan Kim 				PAGE_MAPPING_MOVABLE);
130bda807d4SMinchan Kim }
131bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable);
132bda807d4SMinchan Kim 
13324e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
13424e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
13524e2716fSJoonsoo Kim 
13624e2716fSJoonsoo Kim /*
13724e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
13824e2716fSJoonsoo Kim  * allocation success. 1 << compact_defer_limit compactions are skipped up
13924e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
14024e2716fSJoonsoo Kim  */
14124e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
14224e2716fSJoonsoo Kim {
14324e2716fSJoonsoo Kim 	zone->compact_considered = 0;
14424e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
14524e2716fSJoonsoo Kim 
14624e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
14724e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
14824e2716fSJoonsoo Kim 
14924e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
15024e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
15124e2716fSJoonsoo Kim 
15224e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
15324e2716fSJoonsoo Kim }
15424e2716fSJoonsoo Kim 
15524e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
15624e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
15724e2716fSJoonsoo Kim {
15824e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
15924e2716fSJoonsoo Kim 
16024e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
16124e2716fSJoonsoo Kim 		return false;
16224e2716fSJoonsoo Kim 
16324e2716fSJoonsoo Kim 	/* Avoid possible overflow */
16424e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
16524e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
16624e2716fSJoonsoo Kim 
16724e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
16824e2716fSJoonsoo Kim 		return false;
16924e2716fSJoonsoo Kim 
17024e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
17124e2716fSJoonsoo Kim 
17224e2716fSJoonsoo Kim 	return true;
17324e2716fSJoonsoo Kim }
17424e2716fSJoonsoo Kim 
17524e2716fSJoonsoo Kim /*
17624e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
17724e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
17824e2716fSJoonsoo Kim  * expected to succeed.
17924e2716fSJoonsoo Kim  */
18024e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
18124e2716fSJoonsoo Kim 		bool alloc_success)
18224e2716fSJoonsoo Kim {
18324e2716fSJoonsoo Kim 	if (alloc_success) {
18424e2716fSJoonsoo Kim 		zone->compact_considered = 0;
18524e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
18624e2716fSJoonsoo Kim 	}
18724e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
18824e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
18924e2716fSJoonsoo Kim 
19024e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
19124e2716fSJoonsoo Kim }
19224e2716fSJoonsoo Kim 
19324e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
19424e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
19524e2716fSJoonsoo Kim {
19624e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
19724e2716fSJoonsoo Kim 		return false;
19824e2716fSJoonsoo Kim 
19924e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
20024e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
20124e2716fSJoonsoo Kim }
20224e2716fSJoonsoo Kim 
203bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
204bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
205bb13ffebSMel Gorman 					struct page *page)
206bb13ffebSMel Gorman {
207bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
208bb13ffebSMel Gorman 		return true;
209bb13ffebSMel Gorman 
210bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
211bb13ffebSMel Gorman }
212bb13ffebSMel Gorman 
21302333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
21402333641SVlastimil Babka {
21502333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
21602333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
217623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
21806b6640aSVlastimil Babka 				pageblock_start_pfn(zone_end_pfn(zone) - 1);
21902333641SVlastimil Babka }
22002333641SVlastimil Babka 
221bb13ffebSMel Gorman /*
222b527cfe5SVlastimil Babka  * Compound pages of >= pageblock_order should consistenly be skipped until
223b527cfe5SVlastimil Babka  * released. It is always pointless to compact pages of such order (if they are
224b527cfe5SVlastimil Babka  * migratable), and the pageblocks they occupy cannot contain any free pages.
22521dc7e02SDavid Rientjes  */
226b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page)
22721dc7e02SDavid Rientjes {
228b527cfe5SVlastimil Babka 	if (!PageCompound(page))
22921dc7e02SDavid Rientjes 		return false;
230b527cfe5SVlastimil Babka 
231b527cfe5SVlastimil Babka 	page = compound_head(page);
232b527cfe5SVlastimil Babka 
233b527cfe5SVlastimil Babka 	if (compound_order(page) >= pageblock_order)
23421dc7e02SDavid Rientjes 		return true;
235b527cfe5SVlastimil Babka 
236b527cfe5SVlastimil Babka 	return false;
23721dc7e02SDavid Rientjes }
23821dc7e02SDavid Rientjes 
23921dc7e02SDavid Rientjes /*
240bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
241bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
242bb13ffebSMel Gorman  * meet.
243bb13ffebSMel Gorman  */
24462997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
245bb13ffebSMel Gorman {
246bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
247108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
248bb13ffebSMel Gorman 	unsigned long pfn;
249bb13ffebSMel Gorman 
25062997027SMel Gorman 	zone->compact_blockskip_flush = false;
251bb13ffebSMel Gorman 
252bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
253bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
254bb13ffebSMel Gorman 		struct page *page;
255bb13ffebSMel Gorman 
256bb13ffebSMel Gorman 		cond_resched();
257bb13ffebSMel Gorman 
258ccbe1e4dSMichal Hocko 		page = pfn_to_online_page(pfn);
259ccbe1e4dSMichal Hocko 		if (!page)
260bb13ffebSMel Gorman 			continue;
261bb13ffebSMel Gorman 		if (zone != page_zone(page))
262bb13ffebSMel Gorman 			continue;
263b527cfe5SVlastimil Babka 		if (pageblock_skip_persistent(page))
26421dc7e02SDavid Rientjes 			continue;
265bb13ffebSMel Gorman 
266bb13ffebSMel Gorman 		clear_pageblock_skip(page);
267bb13ffebSMel Gorman 	}
26802333641SVlastimil Babka 
26902333641SVlastimil Babka 	reset_cached_positions(zone);
270bb13ffebSMel Gorman }
271bb13ffebSMel Gorman 
27262997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
27362997027SMel Gorman {
27462997027SMel Gorman 	int zoneid;
27562997027SMel Gorman 
27662997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
27762997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
27862997027SMel Gorman 		if (!populated_zone(zone))
27962997027SMel Gorman 			continue;
28062997027SMel Gorman 
28162997027SMel Gorman 		/* Only flush if a full compaction finished recently */
28262997027SMel Gorman 		if (zone->compact_blockskip_flush)
28362997027SMel Gorman 			__reset_isolation_suitable(zone);
28462997027SMel Gorman 	}
28562997027SMel Gorman }
28662997027SMel Gorman 
287bb13ffebSMel Gorman /*
288bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
28962997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
290bb13ffebSMel Gorman  */
291c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
292c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
293edc2ca61SVlastimil Babka 			bool migrate_scanner)
294bb13ffebSMel Gorman {
295c89511abSMel Gorman 	struct zone *zone = cc->zone;
29635979ef3SDavid Rientjes 	unsigned long pfn;
2976815bf3fSJoonsoo Kim 
2982583d671SVlastimil Babka 	if (cc->no_set_skip_hint)
2996815bf3fSJoonsoo Kim 		return;
3006815bf3fSJoonsoo Kim 
301bb13ffebSMel Gorman 	if (!page)
302bb13ffebSMel Gorman 		return;
303bb13ffebSMel Gorman 
30435979ef3SDavid Rientjes 	if (nr_isolated)
30535979ef3SDavid Rientjes 		return;
30635979ef3SDavid Rientjes 
307bb13ffebSMel Gorman 	set_pageblock_skip(page);
308c89511abSMel Gorman 
30935979ef3SDavid Rientjes 	pfn = page_to_pfn(page);
31035979ef3SDavid Rientjes 
31135979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
312c89511abSMel Gorman 	if (migrate_scanner) {
31335979ef3SDavid Rientjes 		if (pfn > zone->compact_cached_migrate_pfn[0])
31435979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = pfn;
315e0b9daebSDavid Rientjes 		if (cc->mode != MIGRATE_ASYNC &&
316e0b9daebSDavid Rientjes 		    pfn > zone->compact_cached_migrate_pfn[1])
31735979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = pfn;
318c89511abSMel Gorman 	} else {
31935979ef3SDavid Rientjes 		if (pfn < zone->compact_cached_free_pfn)
320c89511abSMel Gorman 			zone->compact_cached_free_pfn = pfn;
321c89511abSMel Gorman 	}
322c89511abSMel Gorman }
323bb13ffebSMel Gorman #else
324bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
325bb13ffebSMel Gorman 					struct page *page)
326bb13ffebSMel Gorman {
327bb13ffebSMel Gorman 	return true;
328bb13ffebSMel Gorman }
329bb13ffebSMel Gorman 
330b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page)
33121dc7e02SDavid Rientjes {
33221dc7e02SDavid Rientjes 	return false;
33321dc7e02SDavid Rientjes }
33421dc7e02SDavid Rientjes 
33521dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc,
336c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
337edc2ca61SVlastimil Babka 			bool migrate_scanner)
338bb13ffebSMel Gorman {
339bb13ffebSMel Gorman }
340bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
341bb13ffebSMel Gorman 
3421f9efdefSVlastimil Babka /*
3438b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
3448b44d279SVlastimil Babka  * very heavily contended. For async compaction, back out if the lock cannot
3458b44d279SVlastimil Babka  * be taken immediately. For sync compaction, spin on the lock if needed.
3468b44d279SVlastimil Babka  *
3478b44d279SVlastimil Babka  * Returns true if the lock is held
3488b44d279SVlastimil Babka  * Returns false if the lock is not held and compaction should abort
3491f9efdefSVlastimil Babka  */
3508b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
3518b44d279SVlastimil Babka 						struct compact_control *cc)
3528b44d279SVlastimil Babka {
3538b44d279SVlastimil Babka 	if (cc->mode == MIGRATE_ASYNC) {
3548b44d279SVlastimil Babka 		if (!spin_trylock_irqsave(lock, *flags)) {
355c3486f53SVlastimil Babka 			cc->contended = true;
3568b44d279SVlastimil Babka 			return false;
3578b44d279SVlastimil Babka 		}
3588b44d279SVlastimil Babka 	} else {
3598b44d279SVlastimil Babka 		spin_lock_irqsave(lock, *flags);
3608b44d279SVlastimil Babka 	}
3611f9efdefSVlastimil Babka 
3628b44d279SVlastimil Babka 	return true;
3632a1402aaSMel Gorman }
3642a1402aaSMel Gorman 
36585aa125fSMichal Nazarewicz /*
366c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
3678b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
3688b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
3698b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
3708b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
3718b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
3728b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
3738b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
374c67fe375SMel Gorman  *
3758b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
3768b44d279SVlastimil Babka  *		async compaction due to need_resched()
3778b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
3788b44d279SVlastimil Babka  *		scheduled)
379c67fe375SMel Gorman  */
3808b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
3818b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
382c67fe375SMel Gorman {
3838b44d279SVlastimil Babka 	if (*locked) {
3848b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
3858b44d279SVlastimil Babka 		*locked = false;
386c67fe375SMel Gorman 	}
387c67fe375SMel Gorman 
3888b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
389c3486f53SVlastimil Babka 		cc->contended = true;
3908b44d279SVlastimil Babka 		return true;
3918b44d279SVlastimil Babka 	}
3928b44d279SVlastimil Babka 
3938b44d279SVlastimil Babka 	if (need_resched()) {
394e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC) {
395c3486f53SVlastimil Babka 			cc->contended = true;
3968b44d279SVlastimil Babka 			return true;
397c67fe375SMel Gorman 		}
398c67fe375SMel Gorman 		cond_resched();
399c67fe375SMel Gorman 	}
400c67fe375SMel Gorman 
4018b44d279SVlastimil Babka 	return false;
402c67fe375SMel Gorman }
403c67fe375SMel Gorman 
404be976572SVlastimil Babka /*
405be976572SVlastimil Babka  * Aside from avoiding lock contention, compaction also periodically checks
406be976572SVlastimil Babka  * need_resched() and either schedules in sync compaction or aborts async
4078b44d279SVlastimil Babka  * compaction. This is similar to what compact_unlock_should_abort() does, but
408be976572SVlastimil Babka  * is used where no lock is concerned.
409be976572SVlastimil Babka  *
410be976572SVlastimil Babka  * Returns false when no scheduling was needed, or sync compaction scheduled.
411be976572SVlastimil Babka  * Returns true when async compaction should abort.
412be976572SVlastimil Babka  */
413be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc)
414be976572SVlastimil Babka {
415be976572SVlastimil Babka 	/* async compaction aborts if contended */
416be976572SVlastimil Babka 	if (need_resched()) {
417be976572SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC) {
418c3486f53SVlastimil Babka 			cc->contended = true;
419be976572SVlastimil Babka 			return true;
420be976572SVlastimil Babka 		}
421be976572SVlastimil Babka 
422be976572SVlastimil Babka 		cond_resched();
423be976572SVlastimil Babka 	}
424be976572SVlastimil Babka 
425be976572SVlastimil Babka 	return false;
426be976572SVlastimil Babka }
427be976572SVlastimil Babka 
428c67fe375SMel Gorman /*
4299e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
4309e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
4319e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
43285aa125fSMichal Nazarewicz  */
433f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
434e14c720eSVlastimil Babka 				unsigned long *start_pfn,
43585aa125fSMichal Nazarewicz 				unsigned long end_pfn,
43685aa125fSMichal Nazarewicz 				struct list_head *freelist,
43785aa125fSMichal Nazarewicz 				bool strict)
438748446bbSMel Gorman {
439b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
440bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
441b8b2d825SXiubo Li 	unsigned long flags = 0;
442f40d1e42SMel Gorman 	bool locked = false;
443e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
44466c64223SJoonsoo Kim 	unsigned int order;
445748446bbSMel Gorman 
446748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
447748446bbSMel Gorman 
448f40d1e42SMel Gorman 	/* Isolate free pages. */
449748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
45066c64223SJoonsoo Kim 		int isolated;
451748446bbSMel Gorman 		struct page *page = cursor;
452748446bbSMel Gorman 
4538b44d279SVlastimil Babka 		/*
4548b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
4558b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
4568b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
4578b44d279SVlastimil Babka 		 */
4588b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
4598b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
4608b44d279SVlastimil Babka 								&locked, cc))
4618b44d279SVlastimil Babka 			break;
4628b44d279SVlastimil Babka 
463b7aba698SMel Gorman 		nr_scanned++;
464f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
4652af120bcSLaura Abbott 			goto isolate_fail;
4662af120bcSLaura Abbott 
467bb13ffebSMel Gorman 		if (!valid_page)
468bb13ffebSMel Gorman 			valid_page = page;
4699fcd6d2eSVlastimil Babka 
4709fcd6d2eSVlastimil Babka 		/*
4719fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
4729fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
4739fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
4749fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
4759fcd6d2eSVlastimil Babka 		 */
4769fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
47721dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
4789fcd6d2eSVlastimil Babka 
479*d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER)) {
48021dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
48121dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
4829fcd6d2eSVlastimil Babka 			}
4839fcd6d2eSVlastimil Babka 			goto isolate_fail;
4849fcd6d2eSVlastimil Babka 		}
4859fcd6d2eSVlastimil Babka 
486f40d1e42SMel Gorman 		if (!PageBuddy(page))
4872af120bcSLaura Abbott 			goto isolate_fail;
488f40d1e42SMel Gorman 
489f40d1e42SMel Gorman 		/*
49069b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
49169b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
49269b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
49369b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
49469b7189fSVlastimil Babka 		 * recheck as well.
49569b7189fSVlastimil Babka 		 */
49669b7189fSVlastimil Babka 		if (!locked) {
49769b7189fSVlastimil Babka 			/*
498f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
499f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
500f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
501f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
502f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
503f40d1e42SMel Gorman 			 * possible.
504f40d1e42SMel Gorman 			 */
5058b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
5068b44d279SVlastimil Babka 								&flags, cc);
507f40d1e42SMel Gorman 			if (!locked)
508f40d1e42SMel Gorman 				break;
509f40d1e42SMel Gorman 
510f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
511f40d1e42SMel Gorman 			if (!PageBuddy(page))
5122af120bcSLaura Abbott 				goto isolate_fail;
51369b7189fSVlastimil Babka 		}
514748446bbSMel Gorman 
51566c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
51666c64223SJoonsoo Kim 		order = page_order(page);
51766c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
518a4f04f2cSDavid Rientjes 		if (!isolated)
519a4f04f2cSDavid Rientjes 			break;
52066c64223SJoonsoo Kim 		set_page_private(page, order);
521a4f04f2cSDavid Rientjes 
522748446bbSMel Gorman 		total_isolated += isolated;
523a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
52466c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
52566c64223SJoonsoo Kim 
526a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
527932ff6bbSJoonsoo Kim 			blockpfn += isolated;
528932ff6bbSJoonsoo Kim 			break;
529932ff6bbSJoonsoo Kim 		}
530a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
531748446bbSMel Gorman 		blockpfn += isolated - 1;
532748446bbSMel Gorman 		cursor += isolated - 1;
5332af120bcSLaura Abbott 		continue;
5342af120bcSLaura Abbott 
5352af120bcSLaura Abbott isolate_fail:
5362af120bcSLaura Abbott 		if (strict)
5372af120bcSLaura Abbott 			break;
5382af120bcSLaura Abbott 		else
5392af120bcSLaura Abbott 			continue;
5402af120bcSLaura Abbott 
541748446bbSMel Gorman 	}
542748446bbSMel Gorman 
543a4f04f2cSDavid Rientjes 	if (locked)
544a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
545a4f04f2cSDavid Rientjes 
5469fcd6d2eSVlastimil Babka 	/*
5479fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
5489fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
5499fcd6d2eSVlastimil Babka 	 */
5509fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
5519fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
5529fcd6d2eSVlastimil Babka 
553e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
554e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
555e34d85f0SJoonsoo Kim 
556e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
557e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
558e14c720eSVlastimil Babka 
559f40d1e42SMel Gorman 	/*
560f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
561f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
562f40d1e42SMel Gorman 	 * returned and CMA will fail.
563f40d1e42SMel Gorman 	 */
5642af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
565f40d1e42SMel Gorman 		total_isolated = 0;
566f40d1e42SMel Gorman 
567bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
568bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
569edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
570bb13ffebSMel Gorman 
5717f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
572397487dbSMel Gorman 	if (total_isolated)
573010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
574748446bbSMel Gorman 	return total_isolated;
575748446bbSMel Gorman }
576748446bbSMel Gorman 
57785aa125fSMichal Nazarewicz /**
57885aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
57985aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
58085aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
58185aa125fSMichal Nazarewicz  *
58285aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
58385aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
58485aa125fSMichal Nazarewicz  * undo its actions and return zero.
58585aa125fSMichal Nazarewicz  *
58685aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
58785aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
58885aa125fSMichal Nazarewicz  * a free page).
58985aa125fSMichal Nazarewicz  */
590ff9543fdSMichal Nazarewicz unsigned long
591bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
592bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
59385aa125fSMichal Nazarewicz {
594e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
59585aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
59685aa125fSMichal Nazarewicz 
5977d49d886SVlastimil Babka 	pfn = start_pfn;
59806b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
599e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
600e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
60106b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
6027d49d886SVlastimil Babka 
6037d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
604e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
6057d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
606e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
607e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
6087d49d886SVlastimil Babka 
60985aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
61085aa125fSMichal Nazarewicz 
61158420016SJoonsoo Kim 		/*
61258420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
61358420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
61458420016SJoonsoo Kim 		 * scanning range to right one.
61558420016SJoonsoo Kim 		 */
61658420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
61706b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
61806b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
61958420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
62058420016SJoonsoo Kim 		}
62158420016SJoonsoo Kim 
622e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
623e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
6247d49d886SVlastimil Babka 			break;
6257d49d886SVlastimil Babka 
626e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
627e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
62885aa125fSMichal Nazarewicz 
62985aa125fSMichal Nazarewicz 		/*
63085aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
63185aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
63285aa125fSMichal Nazarewicz 		 * non-free pages).
63385aa125fSMichal Nazarewicz 		 */
63485aa125fSMichal Nazarewicz 		if (!isolated)
63585aa125fSMichal Nazarewicz 			break;
63685aa125fSMichal Nazarewicz 
63785aa125fSMichal Nazarewicz 		/*
63885aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
63985aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
64085aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
64185aa125fSMichal Nazarewicz 		 */
64285aa125fSMichal Nazarewicz 	}
64385aa125fSMichal Nazarewicz 
64466c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
64585aa125fSMichal Nazarewicz 	map_pages(&freelist);
64685aa125fSMichal Nazarewicz 
64785aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
64885aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
64985aa125fSMichal Nazarewicz 		release_freepages(&freelist);
65085aa125fSMichal Nazarewicz 		return 0;
65185aa125fSMichal Nazarewicz 	}
65285aa125fSMichal Nazarewicz 
65385aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
65485aa125fSMichal Nazarewicz 	return pfn;
65585aa125fSMichal Nazarewicz }
65685aa125fSMichal Nazarewicz 
657748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
658748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
659748446bbSMel Gorman {
660bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
661748446bbSMel Gorman 
662599d0c95SMel Gorman 	inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
663599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
664599d0c95SMel Gorman 	active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
665599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
666599d0c95SMel Gorman 	isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
667599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
668748446bbSMel Gorman 
669bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
670748446bbSMel Gorman }
671748446bbSMel Gorman 
6722fe86e00SMichal Nazarewicz /**
673edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
674edc2ca61SVlastimil Babka  *				  a single pageblock
6752fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
676edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
677edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
678edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
6792fe86e00SMichal Nazarewicz  *
6802fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
681edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
682edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
683edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
684edc2ca61SVlastimil Babka  * than end_pfn).
6852fe86e00SMichal Nazarewicz  *
686edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
687edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
688edc2ca61SVlastimil Babka  * is neither read nor updated.
689748446bbSMel Gorman  */
690edc2ca61SVlastimil Babka static unsigned long
691edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
692edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
693748446bbSMel Gorman {
694edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
695b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
696fa9add64SHugh Dickins 	struct lruvec *lruvec;
697b8b2d825SXiubo Li 	unsigned long flags = 0;
6982a1402aaSMel Gorman 	bool locked = false;
699bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
700e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
701fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
702fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
703748446bbSMel Gorman 
704748446bbSMel Gorman 	/*
705748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
706748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
707748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
708748446bbSMel Gorman 	 */
709748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
710f9e35b3bSMel Gorman 		/* async migration should just abort */
711e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
7122fe86e00SMichal Nazarewicz 			return 0;
713f9e35b3bSMel Gorman 
714748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
715748446bbSMel Gorman 
716748446bbSMel Gorman 		if (fatal_signal_pending(current))
7172fe86e00SMichal Nazarewicz 			return 0;
718748446bbSMel Gorman 	}
719748446bbSMel Gorman 
720be976572SVlastimil Babka 	if (compact_should_abort(cc))
721aeef4b83SDavid Rientjes 		return 0;
722aeef4b83SDavid Rientjes 
723fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
724fdd048e1SVlastimil Babka 		skip_on_failure = true;
725fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
726fdd048e1SVlastimil Babka 	}
727fdd048e1SVlastimil Babka 
728748446bbSMel Gorman 	/* Time to isolate some pages for migration */
729748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
73029c0dde8SVlastimil Babka 
731fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
732fdd048e1SVlastimil Babka 			/*
733fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
734fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
735fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
736fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
737fdd048e1SVlastimil Babka 			 */
738fdd048e1SVlastimil Babka 			if (nr_isolated)
739fdd048e1SVlastimil Babka 				break;
740fdd048e1SVlastimil Babka 
741fdd048e1SVlastimil Babka 			/*
742fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
743fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
744fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
745fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
746fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
747fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
748fdd048e1SVlastimil Babka 			 * previous loop iteration.
749fdd048e1SVlastimil Babka 			 */
750fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
751fdd048e1SVlastimil Babka 		}
752fdd048e1SVlastimil Babka 
7538b44d279SVlastimil Babka 		/*
7548b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
7558b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
7568b44d279SVlastimil Babka 		 * if contended.
7578b44d279SVlastimil Babka 		 */
7588b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
759a52633d8SMel Gorman 		    && compact_unlock_should_abort(zone_lru_lock(zone), flags,
7608b44d279SVlastimil Babka 								&locked, cc))
7618b44d279SVlastimil Babka 			break;
762b2eef8c0SAndrea Arcangeli 
763748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
764fdd048e1SVlastimil Babka 			goto isolate_fail;
765b7aba698SMel Gorman 		nr_scanned++;
766748446bbSMel Gorman 
767748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
768dc908600SMel Gorman 
769bb13ffebSMel Gorman 		if (!valid_page)
770bb13ffebSMel Gorman 			valid_page = page;
771bb13ffebSMel Gorman 
772c122b208SJoonsoo Kim 		/*
77399c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
77499c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
77599c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
77699c0fd5eSVlastimil Babka 		 * potential isolation targets.
7776c14466cSMel Gorman 		 */
77899c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
77999c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
78099c0fd5eSVlastimil Babka 
78199c0fd5eSVlastimil Babka 			/*
78299c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
78399c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
78499c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
78599c0fd5eSVlastimil Babka 			 */
78699c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
78799c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
788748446bbSMel Gorman 			continue;
78999c0fd5eSVlastimil Babka 		}
790748446bbSMel Gorman 
7919927af74SMel Gorman 		/*
79229c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
79329c0dde8SVlastimil Babka 		 * hugetlbfs are not to be compacted. We can potentially save
79429c0dde8SVlastimil Babka 		 * a lot of iterations if we skip them at once. The check is
79529c0dde8SVlastimil Babka 		 * racy, but we can consider only valid values and the only
79629c0dde8SVlastimil Babka 		 * danger is skipping too much.
797bc835011SAndrea Arcangeli 		 */
79829c0dde8SVlastimil Babka 		if (PageCompound(page)) {
79921dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
80029c0dde8SVlastimil Babka 
801*d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER))
80221dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
803fdd048e1SVlastimil Babka 			goto isolate_fail;
8042a1402aaSMel Gorman 		}
8052a1402aaSMel Gorman 
806bda807d4SMinchan Kim 		/*
807bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
808bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
809bda807d4SMinchan Kim 		 * Skip any other type of page
810bda807d4SMinchan Kim 		 */
811bda807d4SMinchan Kim 		if (!PageLRU(page)) {
812bda807d4SMinchan Kim 			/*
813bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
814bda807d4SMinchan Kim 			 * to verify it under page_lock.
815bda807d4SMinchan Kim 			 */
816bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
817bda807d4SMinchan Kim 					!PageIsolated(page)) {
818bda807d4SMinchan Kim 				if (locked) {
819a52633d8SMel Gorman 					spin_unlock_irqrestore(zone_lru_lock(zone),
820bda807d4SMinchan Kim 									flags);
821bda807d4SMinchan Kim 					locked = false;
822bda807d4SMinchan Kim 				}
823bda807d4SMinchan Kim 
8249e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
825bda807d4SMinchan Kim 					goto isolate_success;
826bda807d4SMinchan Kim 			}
827bda807d4SMinchan Kim 
828fdd048e1SVlastimil Babka 			goto isolate_fail;
829bda807d4SMinchan Kim 		}
83029c0dde8SVlastimil Babka 
831119d6d59SDavid Rientjes 		/*
832119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
833119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
834119d6d59SDavid Rientjes 		 * admittedly racy check.
835119d6d59SDavid Rientjes 		 */
836119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
837119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
838fdd048e1SVlastimil Babka 			goto isolate_fail;
839119d6d59SDavid Rientjes 
84073e64c51SMichal Hocko 		/*
84173e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
84273e64c51SMichal Hocko 		 * because those do not depend on fs locks.
84373e64c51SMichal Hocko 		 */
84473e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
84573e64c51SMichal Hocko 			goto isolate_fail;
84673e64c51SMichal Hocko 
84769b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
84869b7189fSVlastimil Babka 		if (!locked) {
849a52633d8SMel Gorman 			locked = compact_trylock_irqsave(zone_lru_lock(zone),
8508b44d279SVlastimil Babka 								&flags, cc);
8518b44d279SVlastimil Babka 			if (!locked)
8522a1402aaSMel Gorman 				break;
8532a1402aaSMel Gorman 
85429c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
8552a1402aaSMel Gorman 			if (!PageLRU(page))
856fdd048e1SVlastimil Babka 				goto isolate_fail;
85729c0dde8SVlastimil Babka 
85829c0dde8SVlastimil Babka 			/*
85929c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
86029c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
86129c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
86229c0dde8SVlastimil Babka 			 */
86329c0dde8SVlastimil Babka 			if (unlikely(PageCompound(page))) {
864*d3c85badSVlastimil Babka 				low_pfn += (1UL << compound_order(page)) - 1;
865fdd048e1SVlastimil Babka 				goto isolate_fail;
866bc835011SAndrea Arcangeli 			}
86769b7189fSVlastimil Babka 		}
868bc835011SAndrea Arcangeli 
869599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
870fa9add64SHugh Dickins 
871748446bbSMel Gorman 		/* Try isolate the page */
872edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
873fdd048e1SVlastimil Babka 			goto isolate_fail;
874748446bbSMel Gorman 
87529c0dde8SVlastimil Babka 		VM_BUG_ON_PAGE(PageCompound(page), page);
876bc835011SAndrea Arcangeli 
877748446bbSMel Gorman 		/* Successfully isolated */
878fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
8796afcf8efSMing Ling 		inc_node_page_state(page,
8806afcf8efSMing Ling 				NR_ISOLATED_ANON + page_is_file_cache(page));
881b6c75016SJoonsoo Kim 
882b6c75016SJoonsoo Kim isolate_success:
883fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
884748446bbSMel Gorman 		cc->nr_migratepages++;
885b7aba698SMel Gorman 		nr_isolated++;
886748446bbSMel Gorman 
887a34753d2SVlastimil Babka 		/*
888a34753d2SVlastimil Babka 		 * Record where we could have freed pages by migration and not
889a34753d2SVlastimil Babka 		 * yet flushed them to buddy allocator.
890a34753d2SVlastimil Babka 		 * - this is the lowest page that was isolated and likely be
891a34753d2SVlastimil Babka 		 * then freed by migration.
892a34753d2SVlastimil Babka 		 */
893a34753d2SVlastimil Babka 		if (!cc->last_migrated_pfn)
894a34753d2SVlastimil Babka 			cc->last_migrated_pfn = low_pfn;
895a34753d2SVlastimil Babka 
896748446bbSMel Gorman 		/* Avoid isolating too much */
89731b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
89831b8384aSHillf Danton 			++low_pfn;
899748446bbSMel Gorman 			break;
900748446bbSMel Gorman 		}
901fdd048e1SVlastimil Babka 
902fdd048e1SVlastimil Babka 		continue;
903fdd048e1SVlastimil Babka isolate_fail:
904fdd048e1SVlastimil Babka 		if (!skip_on_failure)
905fdd048e1SVlastimil Babka 			continue;
906fdd048e1SVlastimil Babka 
907fdd048e1SVlastimil Babka 		/*
908fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
909fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
910fdd048e1SVlastimil Babka 		 * page anyway.
911fdd048e1SVlastimil Babka 		 */
912fdd048e1SVlastimil Babka 		if (nr_isolated) {
913fdd048e1SVlastimil Babka 			if (locked) {
914a52633d8SMel Gorman 				spin_unlock_irqrestore(zone_lru_lock(zone), flags);
915fdd048e1SVlastimil Babka 				locked = false;
916fdd048e1SVlastimil Babka 			}
917fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
918fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
919fdd048e1SVlastimil Babka 			cc->last_migrated_pfn = 0;
920fdd048e1SVlastimil Babka 			nr_isolated = 0;
921fdd048e1SVlastimil Babka 		}
922fdd048e1SVlastimil Babka 
923fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
924fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
925fdd048e1SVlastimil Babka 			/*
926fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
927fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
928fdd048e1SVlastimil Babka 			 */
929fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
930fdd048e1SVlastimil Babka 		}
93131b8384aSHillf Danton 	}
932748446bbSMel Gorman 
93399c0fd5eSVlastimil Babka 	/*
93499c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
93599c0fd5eSVlastimil Babka 	 * the range to be scanned.
93699c0fd5eSVlastimil Babka 	 */
93799c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
93899c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
93999c0fd5eSVlastimil Babka 
940c67fe375SMel Gorman 	if (locked)
941a52633d8SMel Gorman 		spin_unlock_irqrestore(zone_lru_lock(zone), flags);
942748446bbSMel Gorman 
94350b5b094SVlastimil Babka 	/*
94450b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
94550b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
94650b5b094SVlastimil Babka 	 */
94735979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
948edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
949bb13ffebSMel Gorman 
950e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
951e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
952b7aba698SMel Gorman 
9537f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
954397487dbSMel Gorman 	if (nr_isolated)
955010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
956397487dbSMel Gorman 
9572fe86e00SMichal Nazarewicz 	return low_pfn;
9582fe86e00SMichal Nazarewicz }
9592fe86e00SMichal Nazarewicz 
960edc2ca61SVlastimil Babka /**
961edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
962edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
963edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
964edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
965edc2ca61SVlastimil Babka  *
966edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
967edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
968edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
969edc2ca61SVlastimil Babka  */
970edc2ca61SVlastimil Babka unsigned long
971edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
972edc2ca61SVlastimil Babka 							unsigned long end_pfn)
973edc2ca61SVlastimil Babka {
974e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
975edc2ca61SVlastimil Babka 
976edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
977edc2ca61SVlastimil Babka 	pfn = start_pfn;
97806b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
979e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
980e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
98106b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
982edc2ca61SVlastimil Babka 
983edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
984e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
985edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
986edc2ca61SVlastimil Babka 
987edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
988edc2ca61SVlastimil Babka 
989e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
990e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
991edc2ca61SVlastimil Babka 			continue;
992edc2ca61SVlastimil Babka 
993edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
994edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
995edc2ca61SVlastimil Babka 
99614af4a5eSHugh Dickins 		if (!pfn)
997edc2ca61SVlastimil Babka 			break;
9986ea41c0cSJoonsoo Kim 
9996ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
10006ea41c0cSJoonsoo Kim 			break;
1001edc2ca61SVlastimil Babka 	}
1002edc2ca61SVlastimil Babka 
1003edc2ca61SVlastimil Babka 	return pfn;
1004edc2ca61SVlastimil Babka }
1005edc2ca61SVlastimil Babka 
1006ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1007ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1008018e9a49SAndrew Morton 
1009b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1010b682debdSVlastimil Babka 							struct page *page)
1011b682debdSVlastimil Babka {
1012282722b0SVlastimil Babka 	int block_mt;
1013282722b0SVlastimil Babka 
1014282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1015b682debdSVlastimil Babka 		return true;
1016b682debdSVlastimil Babka 
1017282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1018282722b0SVlastimil Babka 
1019282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1020282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1021282722b0SVlastimil Babka 	else
1022282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1023b682debdSVlastimil Babka }
1024b682debdSVlastimil Babka 
1025018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
10269f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
10279f7e3387SVlastimil Babka 							struct page *page)
1028018e9a49SAndrew Morton {
1029018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1030018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1031018e9a49SAndrew Morton 		/*
1032018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1033018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1034018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1035018e9a49SAndrew Morton 		 */
1036018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
1037018e9a49SAndrew Morton 			return false;
1038018e9a49SAndrew Morton 	}
1039018e9a49SAndrew Morton 
10401ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
10411ef36db2SYisheng Xie 		return true;
10421ef36db2SYisheng Xie 
1043018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1044b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1045018e9a49SAndrew Morton 		return true;
1046018e9a49SAndrew Morton 
1047018e9a49SAndrew Morton 	/* Otherwise skip the block */
1048018e9a49SAndrew Morton 	return false;
1049018e9a49SAndrew Morton }
1050018e9a49SAndrew Morton 
1051ff9543fdSMichal Nazarewicz /*
1052f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1053f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1054f2849aa0SVlastimil Babka  */
1055f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1056f2849aa0SVlastimil Babka {
1057f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1058f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1059f2849aa0SVlastimil Babka }
1060f2849aa0SVlastimil Babka 
1061f2849aa0SVlastimil Babka /*
1062ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1063ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1064ff9543fdSMichal Nazarewicz  */
1065edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1066ff9543fdSMichal Nazarewicz {
1067edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1068ff9543fdSMichal Nazarewicz 	struct page *page;
1069c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1070e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1071c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1072c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1073ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
10742fe86e00SMichal Nazarewicz 
1075ff9543fdSMichal Nazarewicz 	/*
1076ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
107749e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1078e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1079e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1080c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1081c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1082c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
108349e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
108449e068f0SVlastimil Babka 	 * is using.
1085ff9543fdSMichal Nazarewicz 	 */
1086e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
108706b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(cc->free_pfn);
1088c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1089c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
109006b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
10912fe86e00SMichal Nazarewicz 
1092ff9543fdSMichal Nazarewicz 	/*
1093ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1094ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1095ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1096ff9543fdSMichal Nazarewicz 	 */
1097f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1098c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1099e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1100e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
1101f6ea3adbSDavid Rientjes 		/*
1102f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1103f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
1104be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
1105f6ea3adbSDavid Rientjes 		 */
1106be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1107be976572SVlastimil Babka 						&& compact_should_abort(cc))
1108be976572SVlastimil Babka 			break;
1109f6ea3adbSDavid Rientjes 
11107d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
11117d49d886SVlastimil Babka 									zone);
11127d49d886SVlastimil Babka 		if (!page)
1113ff9543fdSMichal Nazarewicz 			continue;
1114ff9543fdSMichal Nazarewicz 
1115ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
11169f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1117ff9543fdSMichal Nazarewicz 			continue;
111868e3e926SLinus Torvalds 
1119bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1120bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1121bb13ffebSMel Gorman 			continue;
1122bb13ffebSMel Gorman 
1123e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
1124a46cbf3bSDavid Rientjes 		isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1125a46cbf3bSDavid Rientjes 					freelist, false);
1126ff9543fdSMichal Nazarewicz 
1127ff9543fdSMichal Nazarewicz 		/*
1128a46cbf3bSDavid Rientjes 		 * If we isolated enough freepages, or aborted due to lock
1129a46cbf3bSDavid Rientjes 		 * contention, terminate.
1130e14c720eSVlastimil Babka 		 */
1131f5f61a32SVlastimil Babka 		if ((cc->nr_freepages >= cc->nr_migratepages)
1132f5f61a32SVlastimil Babka 							|| cc->contended) {
1133a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1134a46cbf3bSDavid Rientjes 				/*
1135a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1136a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1137a46cbf3bSDavid Rientjes 				 */
1138f5f61a32SVlastimil Babka 				isolate_start_pfn =
1139e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1140a46cbf3bSDavid Rientjes 			}
1141be976572SVlastimil Babka 			break;
1142a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1143f5f61a32SVlastimil Babka 			/*
1144a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1145a46cbf3bSDavid Rientjes 			 * needlessly.
1146f5f61a32SVlastimil Babka 			 */
1147a46cbf3bSDavid Rientjes 			break;
1148f5f61a32SVlastimil Babka 		}
1149c89511abSMel Gorman 	}
1150ff9543fdSMichal Nazarewicz 
115166c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
1152ff9543fdSMichal Nazarewicz 	map_pages(freelist);
1153ff9543fdSMichal Nazarewicz 
11547ed695e0SVlastimil Babka 	/*
1155f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1156f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1157f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1158f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
11597ed695e0SVlastimil Babka 	 */
1160f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
1161748446bbSMel Gorman }
1162748446bbSMel Gorman 
1163748446bbSMel Gorman /*
1164748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1165748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1166748446bbSMel Gorman  */
1167748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1168748446bbSMel Gorman 					unsigned long data,
1169748446bbSMel Gorman 					int **result)
1170748446bbSMel Gorman {
1171748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1172748446bbSMel Gorman 	struct page *freepage;
1173748446bbSMel Gorman 
1174be976572SVlastimil Babka 	/*
1175be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
1176be976572SVlastimil Babka 	 * contention.
1177be976572SVlastimil Babka 	 */
1178748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1179be976572SVlastimil Babka 		if (!cc->contended)
1180edc2ca61SVlastimil Babka 			isolate_freepages(cc);
1181748446bbSMel Gorman 
1182748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1183748446bbSMel Gorman 			return NULL;
1184748446bbSMel Gorman 	}
1185748446bbSMel Gorman 
1186748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1187748446bbSMel Gorman 	list_del(&freepage->lru);
1188748446bbSMel Gorman 	cc->nr_freepages--;
1189748446bbSMel Gorman 
1190748446bbSMel Gorman 	return freepage;
1191748446bbSMel Gorman }
1192748446bbSMel Gorman 
1193748446bbSMel Gorman /*
1194d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1195d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1196d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1197d53aea3dSDavid Rientjes  */
1198d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1199d53aea3dSDavid Rientjes {
1200d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1201d53aea3dSDavid Rientjes 
1202d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1203d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1204d53aea3dSDavid Rientjes }
1205d53aea3dSDavid Rientjes 
1206ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1207ff9543fdSMichal Nazarewicz typedef enum {
1208ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1209ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1210ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1211ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1212ff9543fdSMichal Nazarewicz 
1213ff9543fdSMichal Nazarewicz /*
12145bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
12155bbe3547SEric B Munson  * compactable pages.
12165bbe3547SEric B Munson  */
12175bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
12185bbe3547SEric B Munson 
12195bbe3547SEric B Munson /*
1220edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1221edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1222edc2ca61SVlastimil Babka  * compact_control.
1223ff9543fdSMichal Nazarewicz  */
1224ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1225ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1226ff9543fdSMichal Nazarewicz {
1227e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1228e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1229e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1230edc2ca61SVlastimil Babka 	struct page *page;
1231edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
12325bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
12331d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1234ff9543fdSMichal Nazarewicz 
1235edc2ca61SVlastimil Babka 	/*
1236edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
1237edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
1238edc2ca61SVlastimil Babka 	 */
1239edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
124006b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
1241e1409c32SJoonsoo Kim 	if (block_start_pfn < zone->zone_start_pfn)
1242e1409c32SJoonsoo Kim 		block_start_pfn = zone->zone_start_pfn;
1243ff9543fdSMichal Nazarewicz 
1244ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
124506b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1246ff9543fdSMichal Nazarewicz 
1247edc2ca61SVlastimil Babka 	/*
1248edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1249edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1250edc2ca61SVlastimil Babka 	 */
1251e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
1252e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1253e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1254e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1255edc2ca61SVlastimil Babka 
1256edc2ca61SVlastimil Babka 		/*
1257edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1258edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1259edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
1260edc2ca61SVlastimil Babka 		 */
1261edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1262edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
1263edc2ca61SVlastimil Babka 			break;
1264edc2ca61SVlastimil Babka 
1265e1409c32SJoonsoo Kim 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1266e1409c32SJoonsoo Kim 									zone);
12677d49d886SVlastimil Babka 		if (!page)
1268edc2ca61SVlastimil Babka 			continue;
1269edc2ca61SVlastimil Babka 
1270edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1271edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1272edc2ca61SVlastimil Babka 			continue;
1273edc2ca61SVlastimil Babka 
1274edc2ca61SVlastimil Babka 		/*
1275edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1276edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1277edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1278edc2ca61SVlastimil Babka 		 */
1279b682debdSVlastimil Babka 		if (!suitable_migration_source(cc, page))
1280edc2ca61SVlastimil Babka 			continue;
1281ff9543fdSMichal Nazarewicz 
1282ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1283e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1284e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1285edc2ca61SVlastimil Babka 
12866afcf8efSMing Ling 		if (!low_pfn || cc->contended)
1287ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1288ff9543fdSMichal Nazarewicz 
1289edc2ca61SVlastimil Babka 		/*
1290edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1291edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1292edc2ca61SVlastimil Babka 		 * continue or not.
1293edc2ca61SVlastimil Babka 		 */
1294edc2ca61SVlastimil Babka 		break;
1295edc2ca61SVlastimil Babka 	}
1296edc2ca61SVlastimil Babka 
1297f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1298f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1299ff9543fdSMichal Nazarewicz 
1300edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1301ff9543fdSMichal Nazarewicz }
1302ff9543fdSMichal Nazarewicz 
130321c527a3SYaowei Bai /*
130421c527a3SYaowei Bai  * order == -1 is expected when compacting via
130521c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
130621c527a3SYaowei Bai  */
130721c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
130821c527a3SYaowei Bai {
130921c527a3SYaowei Bai 	return order == -1;
131021c527a3SYaowei Bai }
131121c527a3SYaowei Bai 
1312d39773a0SVlastimil Babka static enum compact_result __compact_finished(struct zone *zone,
1313d39773a0SVlastimil Babka 						struct compact_control *cc)
1314748446bbSMel Gorman {
13158fb74b9fSMel Gorman 	unsigned int order;
1316d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
131756de7263SMel Gorman 
1318be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
13192d1e1041SVlastimil Babka 		return COMPACT_CONTENDED;
1320748446bbSMel Gorman 
1321753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1322f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
132355b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
132402333641SVlastimil Babka 		reset_cached_positions(zone);
132555b7c4c9SVlastimil Babka 
132662997027SMel Gorman 		/*
132762997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1328accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
132962997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
133062997027SMel Gorman 		 * based on an allocation request.
133162997027SMel Gorman 		 */
1332accf6242SVlastimil Babka 		if (cc->direct_compaction)
133362997027SMel Gorman 			zone->compact_blockskip_flush = true;
133462997027SMel Gorman 
1335c8f7de0bSMichal Hocko 		if (cc->whole_zone)
1336748446bbSMel Gorman 			return COMPACT_COMPLETE;
1337c8f7de0bSMichal Hocko 		else
1338c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
1339bb13ffebSMel Gorman 	}
1340748446bbSMel Gorman 
134121c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
134256de7263SMel Gorman 		return COMPACT_CONTINUE;
134356de7263SMel Gorman 
1344baf6a9a1SVlastimil Babka 	if (cc->finishing_block) {
1345baf6a9a1SVlastimil Babka 		/*
1346baf6a9a1SVlastimil Babka 		 * We have finished the pageblock, but better check again that
1347baf6a9a1SVlastimil Babka 		 * we really succeeded.
1348baf6a9a1SVlastimil Babka 		 */
1349baf6a9a1SVlastimil Babka 		if (IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
1350baf6a9a1SVlastimil Babka 			cc->finishing_block = false;
1351baf6a9a1SVlastimil Babka 		else
1352baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1353baf6a9a1SVlastimil Babka 	}
1354baf6a9a1SVlastimil Babka 
135556de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
135656de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
13578fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
13582149cdaeSJoonsoo Kim 		bool can_steal;
13598fb74b9fSMel Gorman 
136056de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
13616d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
1362cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
136356de7263SMel Gorman 
13642149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
13652149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
13662149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
13672149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
1368cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
13692149cdaeSJoonsoo Kim #endif
13702149cdaeSJoonsoo Kim 		/*
13712149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
13722149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
13732149cdaeSJoonsoo Kim 		 */
13742149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
1375baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
1376baf6a9a1SVlastimil Babka 
1377baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
1378baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
1379cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
1380baf6a9a1SVlastimil Babka 
1381baf6a9a1SVlastimil Babka 			/*
1382baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
1383baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
1384baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
1385baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
1386baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
1387baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
1388baf6a9a1SVlastimil Babka 			 */
1389baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
1390baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
1391baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
1392baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
1393baf6a9a1SVlastimil Babka 			}
1394baf6a9a1SVlastimil Babka 
1395baf6a9a1SVlastimil Babka 			cc->finishing_block = true;
1396baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1397baf6a9a1SVlastimil Babka 		}
139856de7263SMel Gorman 	}
139956de7263SMel Gorman 
1400837d026dSJoonsoo Kim 	return COMPACT_NO_SUITABLE_PAGE;
1401837d026dSJoonsoo Kim }
1402837d026dSJoonsoo Kim 
1403ea7ab982SMichal Hocko static enum compact_result compact_finished(struct zone *zone,
1404d39773a0SVlastimil Babka 			struct compact_control *cc)
1405837d026dSJoonsoo Kim {
1406837d026dSJoonsoo Kim 	int ret;
1407837d026dSJoonsoo Kim 
1408d39773a0SVlastimil Babka 	ret = __compact_finished(zone, cc);
1409837d026dSJoonsoo Kim 	trace_mm_compaction_finished(zone, cc->order, ret);
1410837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1411837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1412837d026dSJoonsoo Kim 
1413837d026dSJoonsoo Kim 	return ret;
1414748446bbSMel Gorman }
1415748446bbSMel Gorman 
14163e7d3449SMel Gorman /*
14173e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
14183e7d3449SMel Gorman  * Returns
14193e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
1420cf378319SVlastimil Babka  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
14213e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
14223e7d3449SMel Gorman  */
1423ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
1424c603844bSMel Gorman 					unsigned int alloc_flags,
142586a294a8SMichal Hocko 					int classzone_idx,
142686a294a8SMichal Hocko 					unsigned long wmark_target)
14273e7d3449SMel Gorman {
14283e7d3449SMel Gorman 	unsigned long watermark;
14293e7d3449SMel Gorman 
143021c527a3SYaowei Bai 	if (is_via_compact_memory(order))
14313957c776SMichal Hocko 		return COMPACT_CONTINUE;
14323957c776SMichal Hocko 
1433f2b8228cSVlastimil Babka 	watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
1434ebff3980SVlastimil Babka 	/*
1435ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1436ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1437ebff3980SVlastimil Babka 	 */
1438ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1439ebff3980SVlastimil Babka 								alloc_flags))
1440cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
1441ebff3980SVlastimil Babka 
14423957c776SMichal Hocko 	/*
14439861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
1444984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
1445984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
1446984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
1447984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
1448984fdba6SVlastimil Babka 	 * isolation. We however do use the direct compactor's classzone_idx to
1449984fdba6SVlastimil Babka 	 * skip over zones where lowmem reserves would prevent allocation even
1450984fdba6SVlastimil Babka 	 * if compaction succeeds.
14518348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
14528348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
1453984fdba6SVlastimil Babka 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
1454984fdba6SVlastimil Babka 	 * suitable migration targets
14553e7d3449SMel Gorman 	 */
14568348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
14578348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
14588348faf9SVlastimil Babka 	watermark += compact_gap(order);
145986a294a8SMichal Hocko 	if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1460984fdba6SVlastimil Babka 						ALLOC_CMA, wmark_target))
14613e7d3449SMel Gorman 		return COMPACT_SKIPPED;
14623e7d3449SMel Gorman 
1463cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
1464cc5c9f09SVlastimil Babka }
1465cc5c9f09SVlastimil Babka 
1466cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
1467cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
1468cc5c9f09SVlastimil Babka 					int classzone_idx)
1469cc5c9f09SVlastimil Babka {
1470cc5c9f09SVlastimil Babka 	enum compact_result ret;
1471cc5c9f09SVlastimil Babka 	int fragindex;
1472cc5c9f09SVlastimil Babka 
1473cc5c9f09SVlastimil Babka 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1474cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
14753e7d3449SMel Gorman 	/*
14763e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
14773e7d3449SMel Gorman 	 * low memory or external fragmentation
14783e7d3449SMel Gorman 	 *
1479ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
1480ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
14813e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
14823e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
14833e7d3449SMel Gorman 	 *
148420311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
148520311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
148620311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
148720311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
148820311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
148920311420SVlastimil Babka 	 * expense of system stability.
14903e7d3449SMel Gorman 	 */
149120311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
14923e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
14933e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1494cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
14953e7d3449SMel Gorman 	}
14963e7d3449SMel Gorman 
1497837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
1498837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
1499837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
1500837d026dSJoonsoo Kim 
1501837d026dSJoonsoo Kim 	return ret;
1502837d026dSJoonsoo Kim }
1503837d026dSJoonsoo Kim 
150486a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
150586a294a8SMichal Hocko 		int alloc_flags)
150686a294a8SMichal Hocko {
150786a294a8SMichal Hocko 	struct zone *zone;
150886a294a8SMichal Hocko 	struct zoneref *z;
150986a294a8SMichal Hocko 
151086a294a8SMichal Hocko 	/*
151186a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
151286a294a8SMichal Hocko 	 * retrying the reclaim.
151386a294a8SMichal Hocko 	 */
151486a294a8SMichal Hocko 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
151586a294a8SMichal Hocko 					ac->nodemask) {
151686a294a8SMichal Hocko 		unsigned long available;
151786a294a8SMichal Hocko 		enum compact_result compact_result;
151886a294a8SMichal Hocko 
151986a294a8SMichal Hocko 		/*
152086a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
152186a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
152286a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
152386a294a8SMichal Hocko 		 * is happy about the watermark check.
152486a294a8SMichal Hocko 		 */
15255a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
152686a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
152786a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
152886a294a8SMichal Hocko 				ac_classzone_idx(ac), available);
1529cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
153086a294a8SMichal Hocko 			return true;
153186a294a8SMichal Hocko 	}
153286a294a8SMichal Hocko 
153386a294a8SMichal Hocko 	return false;
153486a294a8SMichal Hocko }
153586a294a8SMichal Hocko 
1536ea7ab982SMichal Hocko static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
1537748446bbSMel Gorman {
1538ea7ab982SMichal Hocko 	enum compact_result ret;
1539c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1540108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
1541e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1542748446bbSMel Gorman 
1543d39773a0SVlastimil Babka 	cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1544ebff3980SVlastimil Babka 	ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1545ebff3980SVlastimil Babka 							cc->classzone_idx);
15463e7d3449SMel Gorman 	/* Compaction is likely to fail */
1547cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
15483e7d3449SMel Gorman 		return ret;
1549c46649deSMichal Hocko 
1550c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
1551c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
15523e7d3449SMel Gorman 
1553c89511abSMel Gorman 	/*
1554d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1555accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
1556d3132e4bSVlastimil Babka 	 */
1557accf6242SVlastimil Babka 	if (compaction_restarting(zone, cc->order))
1558d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1559d3132e4bSVlastimil Babka 
1560d3132e4bSVlastimil Babka 	/*
1561c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
156206ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
156306ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
156406ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
1565c89511abSMel Gorman 	 */
156606ed2998SVlastimil Babka 	if (cc->whole_zone) {
156706ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
156806ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
156906ed2998SVlastimil Babka 	} else {
1570e0b9daebSDavid Rientjes 		cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1571c89511abSMel Gorman 		cc->free_pfn = zone->compact_cached_free_pfn;
1572623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
157306b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
1574c89511abSMel Gorman 			zone->compact_cached_free_pfn = cc->free_pfn;
1575c89511abSMel Gorman 		}
1576623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
1577c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
157835979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
157935979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1580c89511abSMel Gorman 		}
1581c8f7de0bSMichal Hocko 
1582c8f7de0bSMichal Hocko 		if (cc->migrate_pfn == start_pfn)
1583c8f7de0bSMichal Hocko 			cc->whole_zone = true;
158406ed2998SVlastimil Babka 	}
1585c8f7de0bSMichal Hocko 
15861a16718cSJoonsoo Kim 	cc->last_migrated_pfn = 0;
1587748446bbSMel Gorman 
158816c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
158916c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
15900eb927c0SMel Gorman 
1591748446bbSMel Gorman 	migrate_prep_local();
1592748446bbSMel Gorman 
1593d39773a0SVlastimil Babka 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
15949d502c1cSMinchan Kim 		int err;
1595748446bbSMel Gorman 
1596f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1597f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
15982d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
15995733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1600e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1601f9e35b3bSMel Gorman 			goto out;
1602f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1603fdaf7f5cSVlastimil Babka 			/*
1604fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
1605fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
1606fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
1607fdaf7f5cSVlastimil Babka 			 */
1608fdaf7f5cSVlastimil Babka 			goto check_drain;
1609f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1610f9e35b3bSMel Gorman 			;
1611f9e35b3bSMel Gorman 		}
1612748446bbSMel Gorman 
1613d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1614e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
16157b2a2d4aSMel Gorman 				MR_COMPACTION);
1616748446bbSMel Gorman 
1617f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1618f8c9301fSVlastimil Babka 							&cc->migratepages);
1619748446bbSMel Gorman 
1620f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1621f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
16229d502c1cSMinchan Kim 		if (err) {
16235733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
16247ed695e0SVlastimil Babka 			/*
16257ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
16267ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
16277ed695e0SVlastimil Babka 			 */
1628f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
16292d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
16304bf2bba3SDavid Rientjes 				goto out;
1631748446bbSMel Gorman 			}
1632fdd048e1SVlastimil Babka 			/*
1633fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
1634fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
1635fdd048e1SVlastimil Babka 			 */
1636fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
1637fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
1638fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
1639fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
1640fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
1641fdd048e1SVlastimil Babka 				cc->last_migrated_pfn = 0;
1642fdd048e1SVlastimil Babka 
1643fdd048e1SVlastimil Babka 			}
16444bf2bba3SDavid Rientjes 		}
1645fdaf7f5cSVlastimil Babka 
1646fdaf7f5cSVlastimil Babka check_drain:
1647fdaf7f5cSVlastimil Babka 		/*
1648fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
1649fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
1650fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
1651fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
1652fdaf7f5cSVlastimil Babka 		 * would succeed.
1653fdaf7f5cSVlastimil Babka 		 */
16541a16718cSJoonsoo Kim 		if (cc->order > 0 && cc->last_migrated_pfn) {
1655fdaf7f5cSVlastimil Babka 			int cpu;
1656fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
165706b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
1658fdaf7f5cSVlastimil Babka 
16591a16718cSJoonsoo Kim 			if (cc->last_migrated_pfn < current_block_start) {
1660fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
1661fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
1662fdaf7f5cSVlastimil Babka 				drain_local_pages(zone);
1663fdaf7f5cSVlastimil Babka 				put_cpu();
1664fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
16651a16718cSJoonsoo Kim 				cc->last_migrated_pfn = 0;
1666fdaf7f5cSVlastimil Babka 			}
1667fdaf7f5cSVlastimil Babka 		}
1668fdaf7f5cSVlastimil Babka 
1669748446bbSMel Gorman 	}
1670748446bbSMel Gorman 
1671f9e35b3bSMel Gorman out:
16726bace090SVlastimil Babka 	/*
16736bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
16746bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
16756bace090SVlastimil Babka 	 */
16766bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
16776bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
16786bace090SVlastimil Babka 
16796bace090SVlastimil Babka 		cc->nr_freepages = 0;
16806bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
16816bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
168206b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
16836bace090SVlastimil Babka 		/*
16846bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
16856bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
16866bace090SVlastimil Babka 		 */
16876bace090SVlastimil Babka 		if (free_pfn > zone->compact_cached_free_pfn)
16886bace090SVlastimil Babka 			zone->compact_cached_free_pfn = free_pfn;
16896bace090SVlastimil Babka 	}
1690748446bbSMel Gorman 
16917f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
16927f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
16937f354a54SDavid Rientjes 
169416c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
169516c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
16960eb927c0SMel Gorman 
1697748446bbSMel Gorman 	return ret;
1698748446bbSMel Gorman }
169976ab0f53SMel Gorman 
1700ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
1701c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
1702c603844bSMel Gorman 		unsigned int alloc_flags, int classzone_idx)
170356de7263SMel Gorman {
1704ea7ab982SMichal Hocko 	enum compact_result ret;
170556de7263SMel Gorman 	struct compact_control cc = {
170656de7263SMel Gorman 		.nr_freepages = 0,
170756de7263SMel Gorman 		.nr_migratepages = 0,
17087f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
17097f354a54SDavid Rientjes 		.total_free_scanned = 0,
171056de7263SMel Gorman 		.order = order,
17116d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
171256de7263SMel Gorman 		.zone = zone,
1713a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
1714a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
1715ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
1716ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
1717accf6242SVlastimil Babka 		.direct_compaction = true,
1718a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
17199f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
17209f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
172156de7263SMel Gorman 	};
172256de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
172356de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
172456de7263SMel Gorman 
1725e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1726e64c5237SShaohua Li 
1727e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1728e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1729e64c5237SShaohua Li 
1730e64c5237SShaohua Li 	return ret;
173156de7263SMel Gorman }
173256de7263SMel Gorman 
17335e771905SMel Gorman int sysctl_extfrag_threshold = 500;
17345e771905SMel Gorman 
173556de7263SMel Gorman /**
173656de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
173756de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
17381a6d53a1SVlastimil Babka  * @order: The order of the current allocation
17391a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
17401a6d53a1SVlastimil Babka  * @ac: The context of current allocation
1741e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
174256de7263SMel Gorman  *
174356de7263SMel Gorman  * This is the main entry point for direct page compaction.
174456de7263SMel Gorman  */
1745ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
1746c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
1747c3486f53SVlastimil Babka 		enum compact_priority prio)
174856de7263SMel Gorman {
174956de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
175056de7263SMel Gorman 	struct zoneref *z;
175156de7263SMel Gorman 	struct zone *zone;
17521d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
175356de7263SMel Gorman 
175473e64c51SMichal Hocko 	/*
175573e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
175673e64c51SMichal Hocko 	 * tricky context because the migration might require IO
175773e64c51SMichal Hocko 	 */
175873e64c51SMichal Hocko 	if (!may_perform_io)
175953853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
176056de7263SMel Gorman 
1761a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
1762837d026dSJoonsoo Kim 
176356de7263SMel Gorman 	/* Compact each zone in the list */
17641a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
17651a6d53a1SVlastimil Babka 								ac->nodemask) {
1766ea7ab982SMichal Hocko 		enum compact_result status;
176756de7263SMel Gorman 
1768a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
1769a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
17701d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
177153853e2dSVlastimil Babka 			continue;
17721d4746d3SMichal Hocko 		}
177353853e2dSVlastimil Babka 
1774a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
1775c3486f53SVlastimil Babka 					alloc_flags, ac_classzone_idx(ac));
177656de7263SMel Gorman 		rc = max(status, rc);
177756de7263SMel Gorman 
17787ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
17797ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
178053853e2dSVlastimil Babka 			/*
178153853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
178253853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
178353853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
178453853e2dSVlastimil Babka 			 * succeeds in this zone.
178553853e2dSVlastimil Babka 			 */
178653853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
17871f9efdefSVlastimil Babka 
1788c3486f53SVlastimil Babka 			break;
17891f9efdefSVlastimil Babka 		}
17901f9efdefSVlastimil Babka 
1791a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
1792c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
179353853e2dSVlastimil Babka 			/*
179453853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
179553853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
179653853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
179753853e2dSVlastimil Babka 			 */
179853853e2dSVlastimil Babka 			defer_compaction(zone, order);
17991f9efdefSVlastimil Babka 
18001f9efdefSVlastimil Babka 		/*
18011f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
18021f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
1803c3486f53SVlastimil Babka 		 * case do not try further zones
18041f9efdefSVlastimil Babka 		 */
1805c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
1806c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
18071f9efdefSVlastimil Babka 			break;
18081f9efdefSVlastimil Babka 	}
18091f9efdefSVlastimil Babka 
181056de7263SMel Gorman 	return rc;
181156de7263SMel Gorman }
181256de7263SMel Gorman 
181356de7263SMel Gorman 
181476ab0f53SMel Gorman /* Compact all zones within a node */
18157103f16dSAndrew Morton static void compact_node(int nid)
18167be62de9SRik van Riel {
1817791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
1818791cae96SVlastimil Babka 	int zoneid;
1819791cae96SVlastimil Babka 	struct zone *zone;
18207be62de9SRik van Riel 	struct compact_control cc = {
18217be62de9SRik van Riel 		.order = -1,
18227f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
18237f354a54SDavid Rientjes 		.total_free_scanned = 0,
1824e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
182591ca9186SDavid Rientjes 		.ignore_skip_hint = true,
182606ed2998SVlastimil Babka 		.whole_zone = true,
182773e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
18287be62de9SRik van Riel 	};
18297be62de9SRik van Riel 
1830791cae96SVlastimil Babka 
1831791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1832791cae96SVlastimil Babka 
1833791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1834791cae96SVlastimil Babka 		if (!populated_zone(zone))
1835791cae96SVlastimil Babka 			continue;
1836791cae96SVlastimil Babka 
1837791cae96SVlastimil Babka 		cc.nr_freepages = 0;
1838791cae96SVlastimil Babka 		cc.nr_migratepages = 0;
1839791cae96SVlastimil Babka 		cc.zone = zone;
1840791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1841791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1842791cae96SVlastimil Babka 
1843791cae96SVlastimil Babka 		compact_zone(zone, &cc);
1844791cae96SVlastimil Babka 
1845791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
1846791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
1847791cae96SVlastimil Babka 	}
18487be62de9SRik van Riel }
18497be62de9SRik van Riel 
185076ab0f53SMel Gorman /* Compact all nodes in the system */
18517964c06dSJason Liu static void compact_nodes(void)
185276ab0f53SMel Gorman {
185376ab0f53SMel Gorman 	int nid;
185476ab0f53SMel Gorman 
18558575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
18568575ec29SHugh Dickins 	lru_add_drain_all();
18578575ec29SHugh Dickins 
185876ab0f53SMel Gorman 	for_each_online_node(nid)
185976ab0f53SMel Gorman 		compact_node(nid);
186076ab0f53SMel Gorman }
186176ab0f53SMel Gorman 
186276ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
186376ab0f53SMel Gorman int sysctl_compact_memory;
186476ab0f53SMel Gorman 
1865fec4eb2cSYaowei Bai /*
1866fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
1867fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
1868fec4eb2cSYaowei Bai  */
186976ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
187076ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
187176ab0f53SMel Gorman {
187276ab0f53SMel Gorman 	if (write)
18737964c06dSJason Liu 		compact_nodes();
187476ab0f53SMel Gorman 
187576ab0f53SMel Gorman 	return 0;
187676ab0f53SMel Gorman }
1877ed4a6d7fSMel Gorman 
18785e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
18795e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
18805e771905SMel Gorman {
18815e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
18825e771905SMel Gorman 
18835e771905SMel Gorman 	return 0;
18845e771905SMel Gorman }
18855e771905SMel Gorman 
1886ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
188774e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
188810fbcf4cSKay Sievers 			struct device_attribute *attr,
1889ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1890ed4a6d7fSMel Gorman {
18918575ec29SHugh Dickins 	int nid = dev->id;
18928575ec29SHugh Dickins 
18938575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
18948575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
18958575ec29SHugh Dickins 		lru_add_drain_all();
18968575ec29SHugh Dickins 
18978575ec29SHugh Dickins 		compact_node(nid);
18988575ec29SHugh Dickins 	}
1899ed4a6d7fSMel Gorman 
1900ed4a6d7fSMel Gorman 	return count;
1901ed4a6d7fSMel Gorman }
190210fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1903ed4a6d7fSMel Gorman 
1904ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1905ed4a6d7fSMel Gorman {
190610fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1907ed4a6d7fSMel Gorman }
1908ed4a6d7fSMel Gorman 
1909ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1910ed4a6d7fSMel Gorman {
191110fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1912ed4a6d7fSMel Gorman }
1913ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1914ff9543fdSMichal Nazarewicz 
1915698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1916698b1b30SVlastimil Babka {
1917172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
1918698b1b30SVlastimil Babka }
1919698b1b30SVlastimil Babka 
1920698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
1921698b1b30SVlastimil Babka {
1922698b1b30SVlastimil Babka 	int zoneid;
1923698b1b30SVlastimil Babka 	struct zone *zone;
1924698b1b30SVlastimil Babka 	enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1925698b1b30SVlastimil Babka 
19266cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
1927698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1928698b1b30SVlastimil Babka 
1929698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1930698b1b30SVlastimil Babka 			continue;
1931698b1b30SVlastimil Babka 
1932698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1933698b1b30SVlastimil Babka 					classzone_idx) == COMPACT_CONTINUE)
1934698b1b30SVlastimil Babka 			return true;
1935698b1b30SVlastimil Babka 	}
1936698b1b30SVlastimil Babka 
1937698b1b30SVlastimil Babka 	return false;
1938698b1b30SVlastimil Babka }
1939698b1b30SVlastimil Babka 
1940698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
1941698b1b30SVlastimil Babka {
1942698b1b30SVlastimil Babka 	/*
1943698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
1944698b1b30SVlastimil Babka 	 * order is allocatable.
1945698b1b30SVlastimil Babka 	 */
1946698b1b30SVlastimil Babka 	int zoneid;
1947698b1b30SVlastimil Babka 	struct zone *zone;
1948698b1b30SVlastimil Babka 	struct compact_control cc = {
1949698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
19507f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
19517f354a54SDavid Rientjes 		.total_free_scanned = 0,
1952698b1b30SVlastimil Babka 		.classzone_idx = pgdat->kcompactd_classzone_idx,
1953698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
1954a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
195573e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
1956698b1b30SVlastimil Babka 	};
1957698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1958698b1b30SVlastimil Babka 							cc.classzone_idx);
19597f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
1960698b1b30SVlastimil Babka 
19616cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
1962698b1b30SVlastimil Babka 		int status;
1963698b1b30SVlastimil Babka 
1964698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1965698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1966698b1b30SVlastimil Babka 			continue;
1967698b1b30SVlastimil Babka 
1968698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
1969698b1b30SVlastimil Babka 			continue;
1970698b1b30SVlastimil Babka 
1971698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1972698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
1973698b1b30SVlastimil Babka 			continue;
1974698b1b30SVlastimil Babka 
1975698b1b30SVlastimil Babka 		cc.nr_freepages = 0;
1976698b1b30SVlastimil Babka 		cc.nr_migratepages = 0;
19777f354a54SDavid Rientjes 		cc.total_migrate_scanned = 0;
19787f354a54SDavid Rientjes 		cc.total_free_scanned = 0;
1979698b1b30SVlastimil Babka 		cc.zone = zone;
1980698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1981698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1982698b1b30SVlastimil Babka 
1983172400c6SVlastimil Babka 		if (kthread_should_stop())
1984172400c6SVlastimil Babka 			return;
1985698b1b30SVlastimil Babka 		status = compact_zone(zone, &cc);
1986698b1b30SVlastimil Babka 
19877ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
1988698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
1989c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
1990698b1b30SVlastimil Babka 			/*
1991698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
1992698b1b30SVlastimil Babka 			 * sync direct compaction does.
1993698b1b30SVlastimil Babka 			 */
1994698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
1995698b1b30SVlastimil Babka 		}
1996698b1b30SVlastimil Babka 
19977f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
19987f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
19997f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
20007f354a54SDavid Rientjes 				     cc.total_free_scanned);
20017f354a54SDavid Rientjes 
2002698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2003698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2004698b1b30SVlastimil Babka 	}
2005698b1b30SVlastimil Babka 
2006698b1b30SVlastimil Babka 	/*
2007698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
2008698b1b30SVlastimil Babka 	 * the requested order/classzone_idx in case it was higher/tighter than
2009698b1b30SVlastimil Babka 	 * our current ones
2010698b1b30SVlastimil Babka 	 */
2011698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2012698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
2013698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2014698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2015698b1b30SVlastimil Babka }
2016698b1b30SVlastimil Babka 
2017698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2018698b1b30SVlastimil Babka {
2019698b1b30SVlastimil Babka 	if (!order)
2020698b1b30SVlastimil Babka 		return;
2021698b1b30SVlastimil Babka 
2022698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2023698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2024698b1b30SVlastimil Babka 
2025698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx > classzone_idx)
2026698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = classzone_idx;
2027698b1b30SVlastimil Babka 
20286818600fSDavidlohr Bueso 	/*
20296818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
20306818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
20316818600fSDavidlohr Bueso 	 */
20326818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2033698b1b30SVlastimil Babka 		return;
2034698b1b30SVlastimil Babka 
2035698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2036698b1b30SVlastimil Babka 		return;
2037698b1b30SVlastimil Babka 
2038698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2039698b1b30SVlastimil Babka 							classzone_idx);
2040698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2041698b1b30SVlastimil Babka }
2042698b1b30SVlastimil Babka 
2043698b1b30SVlastimil Babka /*
2044698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2045698b1b30SVlastimil Babka  * from the init process.
2046698b1b30SVlastimil Babka  */
2047698b1b30SVlastimil Babka static int kcompactd(void *p)
2048698b1b30SVlastimil Babka {
2049698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2050698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2051698b1b30SVlastimil Babka 
2052698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2053698b1b30SVlastimil Babka 
2054698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2055698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2056698b1b30SVlastimil Babka 
2057698b1b30SVlastimil Babka 	set_freezable();
2058698b1b30SVlastimil Babka 
2059698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
2060698b1b30SVlastimil Babka 	pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2061698b1b30SVlastimil Babka 
2062698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2063698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2064698b1b30SVlastimil Babka 		wait_event_freezable(pgdat->kcompactd_wait,
2065698b1b30SVlastimil Babka 				kcompactd_work_requested(pgdat));
2066698b1b30SVlastimil Babka 
2067698b1b30SVlastimil Babka 		kcompactd_do_work(pgdat);
2068698b1b30SVlastimil Babka 	}
2069698b1b30SVlastimil Babka 
2070698b1b30SVlastimil Babka 	return 0;
2071698b1b30SVlastimil Babka }
2072698b1b30SVlastimil Babka 
2073698b1b30SVlastimil Babka /*
2074698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2075698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2076698b1b30SVlastimil Babka  */
2077698b1b30SVlastimil Babka int kcompactd_run(int nid)
2078698b1b30SVlastimil Babka {
2079698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2080698b1b30SVlastimil Babka 	int ret = 0;
2081698b1b30SVlastimil Babka 
2082698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2083698b1b30SVlastimil Babka 		return 0;
2084698b1b30SVlastimil Babka 
2085698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2086698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2087698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2088698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2089698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2090698b1b30SVlastimil Babka 	}
2091698b1b30SVlastimil Babka 	return ret;
2092698b1b30SVlastimil Babka }
2093698b1b30SVlastimil Babka 
2094698b1b30SVlastimil Babka /*
2095698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2096698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2097698b1b30SVlastimil Babka  */
2098698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2099698b1b30SVlastimil Babka {
2100698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2101698b1b30SVlastimil Babka 
2102698b1b30SVlastimil Babka 	if (kcompactd) {
2103698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2104698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2105698b1b30SVlastimil Babka 	}
2106698b1b30SVlastimil Babka }
2107698b1b30SVlastimil Babka 
2108698b1b30SVlastimil Babka /*
2109698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2110698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2111698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2112698b1b30SVlastimil Babka  * restore their cpu bindings.
2113698b1b30SVlastimil Babka  */
2114e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2115698b1b30SVlastimil Babka {
2116698b1b30SVlastimil Babka 	int nid;
2117698b1b30SVlastimil Babka 
2118698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2119698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2120698b1b30SVlastimil Babka 		const struct cpumask *mask;
2121698b1b30SVlastimil Babka 
2122698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2123698b1b30SVlastimil Babka 
2124698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2125698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2126698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2127698b1b30SVlastimil Babka 	}
2128e46b1db2SAnna-Maria Gleixner 	return 0;
2129698b1b30SVlastimil Babka }
2130698b1b30SVlastimil Babka 
2131698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2132698b1b30SVlastimil Babka {
2133698b1b30SVlastimil Babka 	int nid;
2134e46b1db2SAnna-Maria Gleixner 	int ret;
2135e46b1db2SAnna-Maria Gleixner 
2136e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2137e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2138e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2139e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2140e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2141e46b1db2SAnna-Maria Gleixner 		return ret;
2142e46b1db2SAnna-Maria Gleixner 	}
2143698b1b30SVlastimil Babka 
2144698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2145698b1b30SVlastimil Babka 		kcompactd_run(nid);
2146698b1b30SVlastimil Babka 	return 0;
2147698b1b30SVlastimil Babka }
2148698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2149698b1b30SVlastimil Babka 
2150ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2151