xref: /openbmc/linux/mm/compaction.c (revision 2583d6713267a4c80126e4e50dd45f5cf685ebe8)
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 
298*2583d671SVlastimil 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 
47921dc7e02SDavid Rientjes 			if (pageblock_skip_persistent(page, order)) {
48021dc7e02SDavid Rientjes 				set_pageblock_skip(page);
48121dc7e02SDavid Rientjes 				blockpfn = end_pfn;
48221dc7e02SDavid Rientjes 			} else if (likely(order < MAX_ORDER)) {
48321dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
48421dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
4859fcd6d2eSVlastimil Babka 			}
4869fcd6d2eSVlastimil Babka 			goto isolate_fail;
4879fcd6d2eSVlastimil Babka 		}
4889fcd6d2eSVlastimil Babka 
489f40d1e42SMel Gorman 		if (!PageBuddy(page))
4902af120bcSLaura Abbott 			goto isolate_fail;
491f40d1e42SMel Gorman 
492f40d1e42SMel Gorman 		/*
49369b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
49469b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
49569b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
49669b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
49769b7189fSVlastimil Babka 		 * recheck as well.
49869b7189fSVlastimil Babka 		 */
49969b7189fSVlastimil Babka 		if (!locked) {
50069b7189fSVlastimil Babka 			/*
501f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
502f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
503f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
504f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
505f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
506f40d1e42SMel Gorman 			 * possible.
507f40d1e42SMel Gorman 			 */
5088b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
5098b44d279SVlastimil Babka 								&flags, cc);
510f40d1e42SMel Gorman 			if (!locked)
511f40d1e42SMel Gorman 				break;
512f40d1e42SMel Gorman 
513f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
514f40d1e42SMel Gorman 			if (!PageBuddy(page))
5152af120bcSLaura Abbott 				goto isolate_fail;
51669b7189fSVlastimil Babka 		}
517748446bbSMel Gorman 
51866c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
51966c64223SJoonsoo Kim 		order = page_order(page);
52066c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
521a4f04f2cSDavid Rientjes 		if (!isolated)
522a4f04f2cSDavid Rientjes 			break;
52366c64223SJoonsoo Kim 		set_page_private(page, order);
524a4f04f2cSDavid Rientjes 
525748446bbSMel Gorman 		total_isolated += isolated;
526a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
52766c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
52866c64223SJoonsoo Kim 
529a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
530932ff6bbSJoonsoo Kim 			blockpfn += isolated;
531932ff6bbSJoonsoo Kim 			break;
532932ff6bbSJoonsoo Kim 		}
533a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
534748446bbSMel Gorman 		blockpfn += isolated - 1;
535748446bbSMel Gorman 		cursor += isolated - 1;
5362af120bcSLaura Abbott 		continue;
5372af120bcSLaura Abbott 
5382af120bcSLaura Abbott isolate_fail:
5392af120bcSLaura Abbott 		if (strict)
5402af120bcSLaura Abbott 			break;
5412af120bcSLaura Abbott 		else
5422af120bcSLaura Abbott 			continue;
5432af120bcSLaura Abbott 
544748446bbSMel Gorman 	}
545748446bbSMel Gorman 
546a4f04f2cSDavid Rientjes 	if (locked)
547a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
548a4f04f2cSDavid Rientjes 
5499fcd6d2eSVlastimil Babka 	/*
5509fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
5519fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
5529fcd6d2eSVlastimil Babka 	 */
5539fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
5549fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
5559fcd6d2eSVlastimil Babka 
556e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
557e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
558e34d85f0SJoonsoo Kim 
559e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
560e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
561e14c720eSVlastimil Babka 
562f40d1e42SMel Gorman 	/*
563f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
564f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
565f40d1e42SMel Gorman 	 * returned and CMA will fail.
566f40d1e42SMel Gorman 	 */
5672af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
568f40d1e42SMel Gorman 		total_isolated = 0;
569f40d1e42SMel Gorman 
570bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
571bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
572edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
573bb13ffebSMel Gorman 
5747f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
575397487dbSMel Gorman 	if (total_isolated)
576010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
577748446bbSMel Gorman 	return total_isolated;
578748446bbSMel Gorman }
579748446bbSMel Gorman 
58085aa125fSMichal Nazarewicz /**
58185aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
58285aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
58385aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
58485aa125fSMichal Nazarewicz  *
58585aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
58685aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
58785aa125fSMichal Nazarewicz  * undo its actions and return zero.
58885aa125fSMichal Nazarewicz  *
58985aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
59085aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
59185aa125fSMichal Nazarewicz  * a free page).
59285aa125fSMichal Nazarewicz  */
593ff9543fdSMichal Nazarewicz unsigned long
594bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
595bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
59685aa125fSMichal Nazarewicz {
597e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
59885aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
59985aa125fSMichal Nazarewicz 
6007d49d886SVlastimil Babka 	pfn = start_pfn;
60106b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
602e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
603e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
60406b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
6057d49d886SVlastimil Babka 
6067d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
607e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
6087d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
609e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
610e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
6117d49d886SVlastimil Babka 
61285aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
61385aa125fSMichal Nazarewicz 
61458420016SJoonsoo Kim 		/*
61558420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
61658420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
61758420016SJoonsoo Kim 		 * scanning range to right one.
61858420016SJoonsoo Kim 		 */
61958420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
62006b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
62106b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
62258420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
62358420016SJoonsoo Kim 		}
62458420016SJoonsoo Kim 
625e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
626e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
6277d49d886SVlastimil Babka 			break;
6287d49d886SVlastimil Babka 
629e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
630e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
63185aa125fSMichal Nazarewicz 
63285aa125fSMichal Nazarewicz 		/*
63385aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
63485aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
63585aa125fSMichal Nazarewicz 		 * non-free pages).
63685aa125fSMichal Nazarewicz 		 */
63785aa125fSMichal Nazarewicz 		if (!isolated)
63885aa125fSMichal Nazarewicz 			break;
63985aa125fSMichal Nazarewicz 
64085aa125fSMichal Nazarewicz 		/*
64185aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
64285aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
64385aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
64485aa125fSMichal Nazarewicz 		 */
64585aa125fSMichal Nazarewicz 	}
64685aa125fSMichal Nazarewicz 
64766c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
64885aa125fSMichal Nazarewicz 	map_pages(&freelist);
64985aa125fSMichal Nazarewicz 
65085aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
65185aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
65285aa125fSMichal Nazarewicz 		release_freepages(&freelist);
65385aa125fSMichal Nazarewicz 		return 0;
65485aa125fSMichal Nazarewicz 	}
65585aa125fSMichal Nazarewicz 
65685aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
65785aa125fSMichal Nazarewicz 	return pfn;
65885aa125fSMichal Nazarewicz }
65985aa125fSMichal Nazarewicz 
660748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
661748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
662748446bbSMel Gorman {
663bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
664748446bbSMel Gorman 
665599d0c95SMel Gorman 	inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
666599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
667599d0c95SMel Gorman 	active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
668599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
669599d0c95SMel Gorman 	isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
670599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
671748446bbSMel Gorman 
672bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
673748446bbSMel Gorman }
674748446bbSMel Gorman 
6752fe86e00SMichal Nazarewicz /**
676edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
677edc2ca61SVlastimil Babka  *				  a single pageblock
6782fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
679edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
680edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
681edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
6822fe86e00SMichal Nazarewicz  *
6832fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
684edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
685edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
686edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
687edc2ca61SVlastimil Babka  * than end_pfn).
6882fe86e00SMichal Nazarewicz  *
689edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
690edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
691edc2ca61SVlastimil Babka  * is neither read nor updated.
692748446bbSMel Gorman  */
693edc2ca61SVlastimil Babka static unsigned long
694edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
695edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
696748446bbSMel Gorman {
697edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
698b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
699fa9add64SHugh Dickins 	struct lruvec *lruvec;
700b8b2d825SXiubo Li 	unsigned long flags = 0;
7012a1402aaSMel Gorman 	bool locked = false;
702bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
703e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
704fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
705fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
706748446bbSMel Gorman 
707748446bbSMel Gorman 	/*
708748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
709748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
710748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
711748446bbSMel Gorman 	 */
712748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
713f9e35b3bSMel Gorman 		/* async migration should just abort */
714e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
7152fe86e00SMichal Nazarewicz 			return 0;
716f9e35b3bSMel Gorman 
717748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
718748446bbSMel Gorman 
719748446bbSMel Gorman 		if (fatal_signal_pending(current))
7202fe86e00SMichal Nazarewicz 			return 0;
721748446bbSMel Gorman 	}
722748446bbSMel Gorman 
723be976572SVlastimil Babka 	if (compact_should_abort(cc))
724aeef4b83SDavid Rientjes 		return 0;
725aeef4b83SDavid Rientjes 
726fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
727fdd048e1SVlastimil Babka 		skip_on_failure = true;
728fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
729fdd048e1SVlastimil Babka 	}
730fdd048e1SVlastimil Babka 
731748446bbSMel Gorman 	/* Time to isolate some pages for migration */
732748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
73329c0dde8SVlastimil Babka 
734fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
735fdd048e1SVlastimil Babka 			/*
736fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
737fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
738fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
739fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
740fdd048e1SVlastimil Babka 			 */
741fdd048e1SVlastimil Babka 			if (nr_isolated)
742fdd048e1SVlastimil Babka 				break;
743fdd048e1SVlastimil Babka 
744fdd048e1SVlastimil Babka 			/*
745fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
746fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
747fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
748fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
749fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
750fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
751fdd048e1SVlastimil Babka 			 * previous loop iteration.
752fdd048e1SVlastimil Babka 			 */
753fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
754fdd048e1SVlastimil Babka 		}
755fdd048e1SVlastimil Babka 
7568b44d279SVlastimil Babka 		/*
7578b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
7588b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
7598b44d279SVlastimil Babka 		 * if contended.
7608b44d279SVlastimil Babka 		 */
7618b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
762a52633d8SMel Gorman 		    && compact_unlock_should_abort(zone_lru_lock(zone), flags,
7638b44d279SVlastimil Babka 								&locked, cc))
7648b44d279SVlastimil Babka 			break;
765b2eef8c0SAndrea Arcangeli 
766748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
767fdd048e1SVlastimil Babka 			goto isolate_fail;
768b7aba698SMel Gorman 		nr_scanned++;
769748446bbSMel Gorman 
770748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
771dc908600SMel Gorman 
772bb13ffebSMel Gorman 		if (!valid_page)
773bb13ffebSMel Gorman 			valid_page = page;
774bb13ffebSMel Gorman 
775c122b208SJoonsoo Kim 		/*
77699c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
77799c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
77899c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
77999c0fd5eSVlastimil Babka 		 * potential isolation targets.
7806c14466cSMel Gorman 		 */
78199c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
78299c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
78399c0fd5eSVlastimil Babka 
78499c0fd5eSVlastimil Babka 			/*
78599c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
78699c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
78799c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
78899c0fd5eSVlastimil Babka 			 */
78999c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
79099c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
791748446bbSMel Gorman 			continue;
79299c0fd5eSVlastimil Babka 		}
793748446bbSMel Gorman 
7949927af74SMel Gorman 		/*
79529c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
79629c0dde8SVlastimil Babka 		 * hugetlbfs are not to be compacted. We can potentially save
79729c0dde8SVlastimil Babka 		 * a lot of iterations if we skip them at once. The check is
79829c0dde8SVlastimil Babka 		 * racy, but we can consider only valid values and the only
79929c0dde8SVlastimil Babka 		 * danger is skipping too much.
800bc835011SAndrea Arcangeli 		 */
80129c0dde8SVlastimil Babka 		if (PageCompound(page)) {
80221dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
80329c0dde8SVlastimil Babka 
80421dc7e02SDavid Rientjes 			if (pageblock_skip_persistent(page, order)) {
80521dc7e02SDavid Rientjes 				set_pageblock_skip(page);
80621dc7e02SDavid Rientjes 				low_pfn = end_pfn;
80721dc7e02SDavid Rientjes 			} else if (likely(order < MAX_ORDER))
80821dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
809fdd048e1SVlastimil Babka 			goto isolate_fail;
8102a1402aaSMel Gorman 		}
8112a1402aaSMel Gorman 
812bda807d4SMinchan Kim 		/*
813bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
814bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
815bda807d4SMinchan Kim 		 * Skip any other type of page
816bda807d4SMinchan Kim 		 */
817bda807d4SMinchan Kim 		if (!PageLRU(page)) {
818bda807d4SMinchan Kim 			/*
819bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
820bda807d4SMinchan Kim 			 * to verify it under page_lock.
821bda807d4SMinchan Kim 			 */
822bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
823bda807d4SMinchan Kim 					!PageIsolated(page)) {
824bda807d4SMinchan Kim 				if (locked) {
825a52633d8SMel Gorman 					spin_unlock_irqrestore(zone_lru_lock(zone),
826bda807d4SMinchan Kim 									flags);
827bda807d4SMinchan Kim 					locked = false;
828bda807d4SMinchan Kim 				}
829bda807d4SMinchan Kim 
8309e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
831bda807d4SMinchan Kim 					goto isolate_success;
832bda807d4SMinchan Kim 			}
833bda807d4SMinchan Kim 
834fdd048e1SVlastimil Babka 			goto isolate_fail;
835bda807d4SMinchan Kim 		}
83629c0dde8SVlastimil Babka 
837119d6d59SDavid Rientjes 		/*
838119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
839119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
840119d6d59SDavid Rientjes 		 * admittedly racy check.
841119d6d59SDavid Rientjes 		 */
842119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
843119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
844fdd048e1SVlastimil Babka 			goto isolate_fail;
845119d6d59SDavid Rientjes 
84673e64c51SMichal Hocko 		/*
84773e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
84873e64c51SMichal Hocko 		 * because those do not depend on fs locks.
84973e64c51SMichal Hocko 		 */
85073e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
85173e64c51SMichal Hocko 			goto isolate_fail;
85273e64c51SMichal Hocko 
85369b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
85469b7189fSVlastimil Babka 		if (!locked) {
855a52633d8SMel Gorman 			locked = compact_trylock_irqsave(zone_lru_lock(zone),
8568b44d279SVlastimil Babka 								&flags, cc);
8578b44d279SVlastimil Babka 			if (!locked)
8582a1402aaSMel Gorman 				break;
8592a1402aaSMel Gorman 
86029c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
8612a1402aaSMel Gorman 			if (!PageLRU(page))
862fdd048e1SVlastimil Babka 				goto isolate_fail;
86329c0dde8SVlastimil Babka 
86429c0dde8SVlastimil Babka 			/*
86529c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
86629c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
86729c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
86829c0dde8SVlastimil Babka 			 */
86929c0dde8SVlastimil Babka 			if (unlikely(PageCompound(page))) {
87021dc7e02SDavid Rientjes 				const unsigned int order = compound_order(page);
87121dc7e02SDavid Rientjes 
87221dc7e02SDavid Rientjes 				if (pageblock_skip_persistent(page, order)) {
87321dc7e02SDavid Rientjes 					set_pageblock_skip(page);
87421dc7e02SDavid Rientjes 					low_pfn = end_pfn;
87521dc7e02SDavid Rientjes 				} else
87621dc7e02SDavid Rientjes 					low_pfn += (1UL << order) - 1;
877fdd048e1SVlastimil Babka 				goto isolate_fail;
878bc835011SAndrea Arcangeli 			}
87969b7189fSVlastimil Babka 		}
880bc835011SAndrea Arcangeli 
881599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
882fa9add64SHugh Dickins 
883748446bbSMel Gorman 		/* Try isolate the page */
884edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
885fdd048e1SVlastimil Babka 			goto isolate_fail;
886748446bbSMel Gorman 
88729c0dde8SVlastimil Babka 		VM_BUG_ON_PAGE(PageCompound(page), page);
888bc835011SAndrea Arcangeli 
889748446bbSMel Gorman 		/* Successfully isolated */
890fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
8916afcf8efSMing Ling 		inc_node_page_state(page,
8926afcf8efSMing Ling 				NR_ISOLATED_ANON + page_is_file_cache(page));
893b6c75016SJoonsoo Kim 
894b6c75016SJoonsoo Kim isolate_success:
895fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
896748446bbSMel Gorman 		cc->nr_migratepages++;
897b7aba698SMel Gorman 		nr_isolated++;
898748446bbSMel Gorman 
899a34753d2SVlastimil Babka 		/*
900a34753d2SVlastimil Babka 		 * Record where we could have freed pages by migration and not
901a34753d2SVlastimil Babka 		 * yet flushed them to buddy allocator.
902a34753d2SVlastimil Babka 		 * - this is the lowest page that was isolated and likely be
903a34753d2SVlastimil Babka 		 * then freed by migration.
904a34753d2SVlastimil Babka 		 */
905a34753d2SVlastimil Babka 		if (!cc->last_migrated_pfn)
906a34753d2SVlastimil Babka 			cc->last_migrated_pfn = low_pfn;
907a34753d2SVlastimil Babka 
908748446bbSMel Gorman 		/* Avoid isolating too much */
90931b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
91031b8384aSHillf Danton 			++low_pfn;
911748446bbSMel Gorman 			break;
912748446bbSMel Gorman 		}
913fdd048e1SVlastimil Babka 
914fdd048e1SVlastimil Babka 		continue;
915fdd048e1SVlastimil Babka isolate_fail:
916fdd048e1SVlastimil Babka 		if (!skip_on_failure)
917fdd048e1SVlastimil Babka 			continue;
918fdd048e1SVlastimil Babka 
919fdd048e1SVlastimil Babka 		/*
920fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
921fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
922fdd048e1SVlastimil Babka 		 * page anyway.
923fdd048e1SVlastimil Babka 		 */
924fdd048e1SVlastimil Babka 		if (nr_isolated) {
925fdd048e1SVlastimil Babka 			if (locked) {
926a52633d8SMel Gorman 				spin_unlock_irqrestore(zone_lru_lock(zone), flags);
927fdd048e1SVlastimil Babka 				locked = false;
928fdd048e1SVlastimil Babka 			}
929fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
930fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
931fdd048e1SVlastimil Babka 			cc->last_migrated_pfn = 0;
932fdd048e1SVlastimil Babka 			nr_isolated = 0;
933fdd048e1SVlastimil Babka 		}
934fdd048e1SVlastimil Babka 
935fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
936fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
937fdd048e1SVlastimil Babka 			/*
938fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
939fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
940fdd048e1SVlastimil Babka 			 */
941fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
942fdd048e1SVlastimil Babka 		}
94331b8384aSHillf Danton 	}
944748446bbSMel Gorman 
94599c0fd5eSVlastimil Babka 	/*
94699c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
94799c0fd5eSVlastimil Babka 	 * the range to be scanned.
94899c0fd5eSVlastimil Babka 	 */
94999c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
95099c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
95199c0fd5eSVlastimil Babka 
952c67fe375SMel Gorman 	if (locked)
953a52633d8SMel Gorman 		spin_unlock_irqrestore(zone_lru_lock(zone), flags);
954748446bbSMel Gorman 
95550b5b094SVlastimil Babka 	/*
95650b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
95750b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
95850b5b094SVlastimil Babka 	 */
95935979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
960edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
961bb13ffebSMel Gorman 
962e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
963e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
964b7aba698SMel Gorman 
9657f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
966397487dbSMel Gorman 	if (nr_isolated)
967010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
968397487dbSMel Gorman 
9692fe86e00SMichal Nazarewicz 	return low_pfn;
9702fe86e00SMichal Nazarewicz }
9712fe86e00SMichal Nazarewicz 
972edc2ca61SVlastimil Babka /**
973edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
974edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
975edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
976edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
977edc2ca61SVlastimil Babka  *
978edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
979edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
980edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
981edc2ca61SVlastimil Babka  */
982edc2ca61SVlastimil Babka unsigned long
983edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
984edc2ca61SVlastimil Babka 							unsigned long end_pfn)
985edc2ca61SVlastimil Babka {
986e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
987edc2ca61SVlastimil Babka 
988edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
989edc2ca61SVlastimil Babka 	pfn = start_pfn;
99006b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
991e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
992e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
99306b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
994edc2ca61SVlastimil Babka 
995edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
996e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
997edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
998edc2ca61SVlastimil Babka 
999edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
1000edc2ca61SVlastimil Babka 
1001e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
1002e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
1003edc2ca61SVlastimil Babka 			continue;
1004edc2ca61SVlastimil Babka 
1005edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1006edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
1007edc2ca61SVlastimil Babka 
100814af4a5eSHugh Dickins 		if (!pfn)
1009edc2ca61SVlastimil Babka 			break;
10106ea41c0cSJoonsoo Kim 
10116ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
10126ea41c0cSJoonsoo Kim 			break;
1013edc2ca61SVlastimil Babka 	}
1014edc2ca61SVlastimil Babka 
1015edc2ca61SVlastimil Babka 	return pfn;
1016edc2ca61SVlastimil Babka }
1017edc2ca61SVlastimil Babka 
1018ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1019ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1020018e9a49SAndrew Morton 
1021b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1022b682debdSVlastimil Babka 							struct page *page)
1023b682debdSVlastimil Babka {
1024282722b0SVlastimil Babka 	int block_mt;
1025282722b0SVlastimil Babka 
1026282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1027b682debdSVlastimil Babka 		return true;
1028b682debdSVlastimil Babka 
1029282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1030282722b0SVlastimil Babka 
1031282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1032282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1033282722b0SVlastimil Babka 	else
1034282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1035b682debdSVlastimil Babka }
1036b682debdSVlastimil Babka 
1037018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
10389f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
10399f7e3387SVlastimil Babka 							struct page *page)
1040018e9a49SAndrew Morton {
1041018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1042018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1043018e9a49SAndrew Morton 		/*
1044018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1045018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1046018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1047018e9a49SAndrew Morton 		 */
1048018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
1049018e9a49SAndrew Morton 			return false;
1050018e9a49SAndrew Morton 	}
1051018e9a49SAndrew Morton 
10521ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
10531ef36db2SYisheng Xie 		return true;
10541ef36db2SYisheng Xie 
1055018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1056b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1057018e9a49SAndrew Morton 		return true;
1058018e9a49SAndrew Morton 
1059018e9a49SAndrew Morton 	/* Otherwise skip the block */
1060018e9a49SAndrew Morton 	return false;
1061018e9a49SAndrew Morton }
1062018e9a49SAndrew Morton 
1063ff9543fdSMichal Nazarewicz /*
1064f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1065f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1066f2849aa0SVlastimil Babka  */
1067f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1068f2849aa0SVlastimil Babka {
1069f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1070f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1071f2849aa0SVlastimil Babka }
1072f2849aa0SVlastimil Babka 
1073f2849aa0SVlastimil Babka /*
1074ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1075ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1076ff9543fdSMichal Nazarewicz  */
1077edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1078ff9543fdSMichal Nazarewicz {
1079edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1080ff9543fdSMichal Nazarewicz 	struct page *page;
1081c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1082e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1083c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1084c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1085ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
10862fe86e00SMichal Nazarewicz 
1087ff9543fdSMichal Nazarewicz 	/*
1088ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
108949e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1090e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1091e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1092c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1093c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1094c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
109549e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
109649e068f0SVlastimil Babka 	 * is using.
1097ff9543fdSMichal Nazarewicz 	 */
1098e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
109906b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(cc->free_pfn);
1100c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1101c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
110206b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
11032fe86e00SMichal Nazarewicz 
1104ff9543fdSMichal Nazarewicz 	/*
1105ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1106ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1107ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1108ff9543fdSMichal Nazarewicz 	 */
1109f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1110c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1111e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1112e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
1113f6ea3adbSDavid Rientjes 		/*
1114f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1115f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
1116be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
1117f6ea3adbSDavid Rientjes 		 */
1118be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1119be976572SVlastimil Babka 						&& compact_should_abort(cc))
1120be976572SVlastimil Babka 			break;
1121f6ea3adbSDavid Rientjes 
11227d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
11237d49d886SVlastimil Babka 									zone);
11247d49d886SVlastimil Babka 		if (!page)
1125ff9543fdSMichal Nazarewicz 			continue;
1126ff9543fdSMichal Nazarewicz 
1127ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
11289f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1129ff9543fdSMichal Nazarewicz 			continue;
113068e3e926SLinus Torvalds 
1131bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1132bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1133bb13ffebSMel Gorman 			continue;
1134bb13ffebSMel Gorman 
1135e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
1136a46cbf3bSDavid Rientjes 		isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1137a46cbf3bSDavid Rientjes 					freelist, false);
1138ff9543fdSMichal Nazarewicz 
1139ff9543fdSMichal Nazarewicz 		/*
1140a46cbf3bSDavid Rientjes 		 * If we isolated enough freepages, or aborted due to lock
1141a46cbf3bSDavid Rientjes 		 * contention, terminate.
1142e14c720eSVlastimil Babka 		 */
1143f5f61a32SVlastimil Babka 		if ((cc->nr_freepages >= cc->nr_migratepages)
1144f5f61a32SVlastimil Babka 							|| cc->contended) {
1145a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1146a46cbf3bSDavid Rientjes 				/*
1147a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1148a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1149a46cbf3bSDavid Rientjes 				 */
1150f5f61a32SVlastimil Babka 				isolate_start_pfn =
1151e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1152a46cbf3bSDavid Rientjes 			}
1153be976572SVlastimil Babka 			break;
1154a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1155f5f61a32SVlastimil Babka 			/*
1156a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1157a46cbf3bSDavid Rientjes 			 * needlessly.
1158f5f61a32SVlastimil Babka 			 */
1159a46cbf3bSDavid Rientjes 			break;
1160f5f61a32SVlastimil Babka 		}
1161c89511abSMel Gorman 	}
1162ff9543fdSMichal Nazarewicz 
116366c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
1164ff9543fdSMichal Nazarewicz 	map_pages(freelist);
1165ff9543fdSMichal Nazarewicz 
11667ed695e0SVlastimil Babka 	/*
1167f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1168f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1169f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1170f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
11717ed695e0SVlastimil Babka 	 */
1172f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
1173748446bbSMel Gorman }
1174748446bbSMel Gorman 
1175748446bbSMel Gorman /*
1176748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1177748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1178748446bbSMel Gorman  */
1179748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1180748446bbSMel Gorman 					unsigned long data,
1181748446bbSMel Gorman 					int **result)
1182748446bbSMel Gorman {
1183748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1184748446bbSMel Gorman 	struct page *freepage;
1185748446bbSMel Gorman 
1186be976572SVlastimil Babka 	/*
1187be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
1188be976572SVlastimil Babka 	 * contention.
1189be976572SVlastimil Babka 	 */
1190748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1191be976572SVlastimil Babka 		if (!cc->contended)
1192edc2ca61SVlastimil Babka 			isolate_freepages(cc);
1193748446bbSMel Gorman 
1194748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1195748446bbSMel Gorman 			return NULL;
1196748446bbSMel Gorman 	}
1197748446bbSMel Gorman 
1198748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1199748446bbSMel Gorman 	list_del(&freepage->lru);
1200748446bbSMel Gorman 	cc->nr_freepages--;
1201748446bbSMel Gorman 
1202748446bbSMel Gorman 	return freepage;
1203748446bbSMel Gorman }
1204748446bbSMel Gorman 
1205748446bbSMel Gorman /*
1206d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1207d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1208d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1209d53aea3dSDavid Rientjes  */
1210d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1211d53aea3dSDavid Rientjes {
1212d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1213d53aea3dSDavid Rientjes 
1214d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1215d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1216d53aea3dSDavid Rientjes }
1217d53aea3dSDavid Rientjes 
1218ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1219ff9543fdSMichal Nazarewicz typedef enum {
1220ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1221ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1222ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1223ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1224ff9543fdSMichal Nazarewicz 
1225ff9543fdSMichal Nazarewicz /*
12265bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
12275bbe3547SEric B Munson  * compactable pages.
12285bbe3547SEric B Munson  */
12295bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
12305bbe3547SEric B Munson 
12315bbe3547SEric B Munson /*
1232edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1233edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1234edc2ca61SVlastimil Babka  * compact_control.
1235ff9543fdSMichal Nazarewicz  */
1236ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1237ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1238ff9543fdSMichal Nazarewicz {
1239e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1240e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1241e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1242edc2ca61SVlastimil Babka 	struct page *page;
1243edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
12445bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
12451d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1246ff9543fdSMichal Nazarewicz 
1247edc2ca61SVlastimil Babka 	/*
1248edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
1249edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
1250edc2ca61SVlastimil Babka 	 */
1251edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
125206b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
1253e1409c32SJoonsoo Kim 	if (block_start_pfn < zone->zone_start_pfn)
1254e1409c32SJoonsoo Kim 		block_start_pfn = zone->zone_start_pfn;
1255ff9543fdSMichal Nazarewicz 
1256ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
125706b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1258ff9543fdSMichal Nazarewicz 
1259edc2ca61SVlastimil Babka 	/*
1260edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1261edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1262edc2ca61SVlastimil Babka 	 */
1263e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
1264e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1265e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1266e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1267edc2ca61SVlastimil Babka 
1268edc2ca61SVlastimil Babka 		/*
1269edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1270edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1271edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
1272edc2ca61SVlastimil Babka 		 */
1273edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1274edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
1275edc2ca61SVlastimil Babka 			break;
1276edc2ca61SVlastimil Babka 
1277e1409c32SJoonsoo Kim 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1278e1409c32SJoonsoo Kim 									zone);
12797d49d886SVlastimil Babka 		if (!page)
1280edc2ca61SVlastimil Babka 			continue;
1281edc2ca61SVlastimil Babka 
1282edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1283edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1284edc2ca61SVlastimil Babka 			continue;
1285edc2ca61SVlastimil Babka 
1286edc2ca61SVlastimil Babka 		/*
1287edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1288edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1289edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1290edc2ca61SVlastimil Babka 		 */
1291b682debdSVlastimil Babka 		if (!suitable_migration_source(cc, page))
1292edc2ca61SVlastimil Babka 			continue;
1293ff9543fdSMichal Nazarewicz 
1294ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1295e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1296e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1297edc2ca61SVlastimil Babka 
12986afcf8efSMing Ling 		if (!low_pfn || cc->contended)
1299ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1300ff9543fdSMichal Nazarewicz 
1301edc2ca61SVlastimil Babka 		/*
1302edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1303edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1304edc2ca61SVlastimil Babka 		 * continue or not.
1305edc2ca61SVlastimil Babka 		 */
1306edc2ca61SVlastimil Babka 		break;
1307edc2ca61SVlastimil Babka 	}
1308edc2ca61SVlastimil Babka 
1309f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1310f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1311ff9543fdSMichal Nazarewicz 
1312edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1313ff9543fdSMichal Nazarewicz }
1314ff9543fdSMichal Nazarewicz 
131521c527a3SYaowei Bai /*
131621c527a3SYaowei Bai  * order == -1 is expected when compacting via
131721c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
131821c527a3SYaowei Bai  */
131921c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
132021c527a3SYaowei Bai {
132121c527a3SYaowei Bai 	return order == -1;
132221c527a3SYaowei Bai }
132321c527a3SYaowei Bai 
1324d39773a0SVlastimil Babka static enum compact_result __compact_finished(struct zone *zone,
1325d39773a0SVlastimil Babka 						struct compact_control *cc)
1326748446bbSMel Gorman {
13278fb74b9fSMel Gorman 	unsigned int order;
1328d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
132956de7263SMel Gorman 
1330be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
13312d1e1041SVlastimil Babka 		return COMPACT_CONTENDED;
1332748446bbSMel Gorman 
1333753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1334f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
133555b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
133602333641SVlastimil Babka 		reset_cached_positions(zone);
133755b7c4c9SVlastimil Babka 
133862997027SMel Gorman 		/*
133962997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1340accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
134162997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
134262997027SMel Gorman 		 * based on an allocation request.
134362997027SMel Gorman 		 */
1344accf6242SVlastimil Babka 		if (cc->direct_compaction)
134562997027SMel Gorman 			zone->compact_blockskip_flush = true;
134662997027SMel Gorman 
1347c8f7de0bSMichal Hocko 		if (cc->whole_zone)
1348748446bbSMel Gorman 			return COMPACT_COMPLETE;
1349c8f7de0bSMichal Hocko 		else
1350c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
1351bb13ffebSMel Gorman 	}
1352748446bbSMel Gorman 
135321c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
135456de7263SMel Gorman 		return COMPACT_CONTINUE;
135556de7263SMel Gorman 
1356baf6a9a1SVlastimil Babka 	if (cc->finishing_block) {
1357baf6a9a1SVlastimil Babka 		/*
1358baf6a9a1SVlastimil Babka 		 * We have finished the pageblock, but better check again that
1359baf6a9a1SVlastimil Babka 		 * we really succeeded.
1360baf6a9a1SVlastimil Babka 		 */
1361baf6a9a1SVlastimil Babka 		if (IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
1362baf6a9a1SVlastimil Babka 			cc->finishing_block = false;
1363baf6a9a1SVlastimil Babka 		else
1364baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1365baf6a9a1SVlastimil Babka 	}
1366baf6a9a1SVlastimil Babka 
136756de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
136856de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
13698fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
13702149cdaeSJoonsoo Kim 		bool can_steal;
13718fb74b9fSMel Gorman 
137256de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
13736d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
1374cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
137556de7263SMel Gorman 
13762149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
13772149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
13782149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
13792149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
1380cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
13812149cdaeSJoonsoo Kim #endif
13822149cdaeSJoonsoo Kim 		/*
13832149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
13842149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
13852149cdaeSJoonsoo Kim 		 */
13862149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
1387baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
1388baf6a9a1SVlastimil Babka 
1389baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
1390baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
1391cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
1392baf6a9a1SVlastimil Babka 
1393baf6a9a1SVlastimil Babka 			/*
1394baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
1395baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
1396baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
1397baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
1398baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
1399baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
1400baf6a9a1SVlastimil Babka 			 */
1401baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
1402baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
1403baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
1404baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
1405baf6a9a1SVlastimil Babka 			}
1406baf6a9a1SVlastimil Babka 
1407baf6a9a1SVlastimil Babka 			cc->finishing_block = true;
1408baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1409baf6a9a1SVlastimil Babka 		}
141056de7263SMel Gorman 	}
141156de7263SMel Gorman 
1412837d026dSJoonsoo Kim 	return COMPACT_NO_SUITABLE_PAGE;
1413837d026dSJoonsoo Kim }
1414837d026dSJoonsoo Kim 
1415ea7ab982SMichal Hocko static enum compact_result compact_finished(struct zone *zone,
1416d39773a0SVlastimil Babka 			struct compact_control *cc)
1417837d026dSJoonsoo Kim {
1418837d026dSJoonsoo Kim 	int ret;
1419837d026dSJoonsoo Kim 
1420d39773a0SVlastimil Babka 	ret = __compact_finished(zone, cc);
1421837d026dSJoonsoo Kim 	trace_mm_compaction_finished(zone, cc->order, ret);
1422837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1423837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1424837d026dSJoonsoo Kim 
1425837d026dSJoonsoo Kim 	return ret;
1426748446bbSMel Gorman }
1427748446bbSMel Gorman 
14283e7d3449SMel Gorman /*
14293e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
14303e7d3449SMel Gorman  * Returns
14313e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
1432cf378319SVlastimil Babka  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
14333e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
14343e7d3449SMel Gorman  */
1435ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
1436c603844bSMel Gorman 					unsigned int alloc_flags,
143786a294a8SMichal Hocko 					int classzone_idx,
143886a294a8SMichal Hocko 					unsigned long wmark_target)
14393e7d3449SMel Gorman {
14403e7d3449SMel Gorman 	unsigned long watermark;
14413e7d3449SMel Gorman 
144221c527a3SYaowei Bai 	if (is_via_compact_memory(order))
14433957c776SMichal Hocko 		return COMPACT_CONTINUE;
14443957c776SMichal Hocko 
1445f2b8228cSVlastimil Babka 	watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
1446ebff3980SVlastimil Babka 	/*
1447ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1448ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1449ebff3980SVlastimil Babka 	 */
1450ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1451ebff3980SVlastimil Babka 								alloc_flags))
1452cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
1453ebff3980SVlastimil Babka 
14543957c776SMichal Hocko 	/*
14559861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
1456984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
1457984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
1458984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
1459984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
1460984fdba6SVlastimil Babka 	 * isolation. We however do use the direct compactor's classzone_idx to
1461984fdba6SVlastimil Babka 	 * skip over zones where lowmem reserves would prevent allocation even
1462984fdba6SVlastimil Babka 	 * if compaction succeeds.
14638348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
14648348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
1465984fdba6SVlastimil Babka 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
1466984fdba6SVlastimil Babka 	 * suitable migration targets
14673e7d3449SMel Gorman 	 */
14688348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
14698348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
14708348faf9SVlastimil Babka 	watermark += compact_gap(order);
147186a294a8SMichal Hocko 	if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1472984fdba6SVlastimil Babka 						ALLOC_CMA, wmark_target))
14733e7d3449SMel Gorman 		return COMPACT_SKIPPED;
14743e7d3449SMel Gorman 
1475cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
1476cc5c9f09SVlastimil Babka }
1477cc5c9f09SVlastimil Babka 
1478cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
1479cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
1480cc5c9f09SVlastimil Babka 					int classzone_idx)
1481cc5c9f09SVlastimil Babka {
1482cc5c9f09SVlastimil Babka 	enum compact_result ret;
1483cc5c9f09SVlastimil Babka 	int fragindex;
1484cc5c9f09SVlastimil Babka 
1485cc5c9f09SVlastimil Babka 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1486cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
14873e7d3449SMel Gorman 	/*
14883e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
14893e7d3449SMel Gorman 	 * low memory or external fragmentation
14903e7d3449SMel Gorman 	 *
1491ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
1492ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
14933e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
14943e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
14953e7d3449SMel Gorman 	 *
149620311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
149720311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
149820311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
149920311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
150020311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
150120311420SVlastimil Babka 	 * expense of system stability.
15023e7d3449SMel Gorman 	 */
150320311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
15043e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
15053e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1506cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
15073e7d3449SMel Gorman 	}
15083e7d3449SMel Gorman 
1509837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
1510837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
1511837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
1512837d026dSJoonsoo Kim 
1513837d026dSJoonsoo Kim 	return ret;
1514837d026dSJoonsoo Kim }
1515837d026dSJoonsoo Kim 
151686a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
151786a294a8SMichal Hocko 		int alloc_flags)
151886a294a8SMichal Hocko {
151986a294a8SMichal Hocko 	struct zone *zone;
152086a294a8SMichal Hocko 	struct zoneref *z;
152186a294a8SMichal Hocko 
152286a294a8SMichal Hocko 	/*
152386a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
152486a294a8SMichal Hocko 	 * retrying the reclaim.
152586a294a8SMichal Hocko 	 */
152686a294a8SMichal Hocko 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
152786a294a8SMichal Hocko 					ac->nodemask) {
152886a294a8SMichal Hocko 		unsigned long available;
152986a294a8SMichal Hocko 		enum compact_result compact_result;
153086a294a8SMichal Hocko 
153186a294a8SMichal Hocko 		/*
153286a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
153386a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
153486a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
153586a294a8SMichal Hocko 		 * is happy about the watermark check.
153686a294a8SMichal Hocko 		 */
15375a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
153886a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
153986a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
154086a294a8SMichal Hocko 				ac_classzone_idx(ac), available);
1541cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
154286a294a8SMichal Hocko 			return true;
154386a294a8SMichal Hocko 	}
154486a294a8SMichal Hocko 
154586a294a8SMichal Hocko 	return false;
154686a294a8SMichal Hocko }
154786a294a8SMichal Hocko 
1548ea7ab982SMichal Hocko static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
1549748446bbSMel Gorman {
1550ea7ab982SMichal Hocko 	enum compact_result ret;
1551c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1552108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
1553e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1554748446bbSMel Gorman 
1555d39773a0SVlastimil Babka 	cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1556ebff3980SVlastimil Babka 	ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1557ebff3980SVlastimil Babka 							cc->classzone_idx);
15583e7d3449SMel Gorman 	/* Compaction is likely to fail */
1559cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
15603e7d3449SMel Gorman 		return ret;
1561c46649deSMichal Hocko 
1562c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
1563c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
15643e7d3449SMel Gorman 
1565c89511abSMel Gorman 	/*
1566d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1567accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
1568d3132e4bSVlastimil Babka 	 */
1569accf6242SVlastimil Babka 	if (compaction_restarting(zone, cc->order))
1570d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1571d3132e4bSVlastimil Babka 
1572d3132e4bSVlastimil Babka 	/*
1573c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
157406ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
157506ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
157606ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
1577c89511abSMel Gorman 	 */
157806ed2998SVlastimil Babka 	if (cc->whole_zone) {
157906ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
158006ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
158106ed2998SVlastimil Babka 	} else {
1582e0b9daebSDavid Rientjes 		cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1583c89511abSMel Gorman 		cc->free_pfn = zone->compact_cached_free_pfn;
1584623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
158506b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
1586c89511abSMel Gorman 			zone->compact_cached_free_pfn = cc->free_pfn;
1587c89511abSMel Gorman 		}
1588623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
1589c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
159035979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
159135979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1592c89511abSMel Gorman 		}
1593c8f7de0bSMichal Hocko 
1594c8f7de0bSMichal Hocko 		if (cc->migrate_pfn == start_pfn)
1595c8f7de0bSMichal Hocko 			cc->whole_zone = true;
159606ed2998SVlastimil Babka 	}
1597c8f7de0bSMichal Hocko 
15981a16718cSJoonsoo Kim 	cc->last_migrated_pfn = 0;
1599748446bbSMel Gorman 
160016c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
160116c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
16020eb927c0SMel Gorman 
1603748446bbSMel Gorman 	migrate_prep_local();
1604748446bbSMel Gorman 
1605d39773a0SVlastimil Babka 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
16069d502c1cSMinchan Kim 		int err;
1607748446bbSMel Gorman 
1608f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1609f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
16102d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
16115733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1612e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1613f9e35b3bSMel Gorman 			goto out;
1614f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1615fdaf7f5cSVlastimil Babka 			/*
1616fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
1617fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
1618fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
1619fdaf7f5cSVlastimil Babka 			 */
1620fdaf7f5cSVlastimil Babka 			goto check_drain;
1621f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1622f9e35b3bSMel Gorman 			;
1623f9e35b3bSMel Gorman 		}
1624748446bbSMel Gorman 
1625d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1626e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
16277b2a2d4aSMel Gorman 				MR_COMPACTION);
1628748446bbSMel Gorman 
1629f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1630f8c9301fSVlastimil Babka 							&cc->migratepages);
1631748446bbSMel Gorman 
1632f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1633f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
16349d502c1cSMinchan Kim 		if (err) {
16355733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
16367ed695e0SVlastimil Babka 			/*
16377ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
16387ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
16397ed695e0SVlastimil Babka 			 */
1640f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
16412d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
16424bf2bba3SDavid Rientjes 				goto out;
1643748446bbSMel Gorman 			}
1644fdd048e1SVlastimil Babka 			/*
1645fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
1646fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
1647fdd048e1SVlastimil Babka 			 */
1648fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
1649fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
1650fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
1651fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
1652fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
1653fdd048e1SVlastimil Babka 				cc->last_migrated_pfn = 0;
1654fdd048e1SVlastimil Babka 
1655fdd048e1SVlastimil Babka 			}
16564bf2bba3SDavid Rientjes 		}
1657fdaf7f5cSVlastimil Babka 
1658fdaf7f5cSVlastimil Babka check_drain:
1659fdaf7f5cSVlastimil Babka 		/*
1660fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
1661fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
1662fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
1663fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
1664fdaf7f5cSVlastimil Babka 		 * would succeed.
1665fdaf7f5cSVlastimil Babka 		 */
16661a16718cSJoonsoo Kim 		if (cc->order > 0 && cc->last_migrated_pfn) {
1667fdaf7f5cSVlastimil Babka 			int cpu;
1668fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
166906b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
1670fdaf7f5cSVlastimil Babka 
16711a16718cSJoonsoo Kim 			if (cc->last_migrated_pfn < current_block_start) {
1672fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
1673fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
1674fdaf7f5cSVlastimil Babka 				drain_local_pages(zone);
1675fdaf7f5cSVlastimil Babka 				put_cpu();
1676fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
16771a16718cSJoonsoo Kim 				cc->last_migrated_pfn = 0;
1678fdaf7f5cSVlastimil Babka 			}
1679fdaf7f5cSVlastimil Babka 		}
1680fdaf7f5cSVlastimil Babka 
1681748446bbSMel Gorman 	}
1682748446bbSMel Gorman 
1683f9e35b3bSMel Gorman out:
16846bace090SVlastimil Babka 	/*
16856bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
16866bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
16876bace090SVlastimil Babka 	 */
16886bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
16896bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
16906bace090SVlastimil Babka 
16916bace090SVlastimil Babka 		cc->nr_freepages = 0;
16926bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
16936bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
169406b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
16956bace090SVlastimil Babka 		/*
16966bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
16976bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
16986bace090SVlastimil Babka 		 */
16996bace090SVlastimil Babka 		if (free_pfn > zone->compact_cached_free_pfn)
17006bace090SVlastimil Babka 			zone->compact_cached_free_pfn = free_pfn;
17016bace090SVlastimil Babka 	}
1702748446bbSMel Gorman 
17037f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
17047f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
17057f354a54SDavid Rientjes 
170616c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
170716c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
17080eb927c0SMel Gorman 
1709748446bbSMel Gorman 	return ret;
1710748446bbSMel Gorman }
171176ab0f53SMel Gorman 
1712ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
1713c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
1714c603844bSMel Gorman 		unsigned int alloc_flags, int classzone_idx)
171556de7263SMel Gorman {
1716ea7ab982SMichal Hocko 	enum compact_result ret;
171756de7263SMel Gorman 	struct compact_control cc = {
171856de7263SMel Gorman 		.nr_freepages = 0,
171956de7263SMel Gorman 		.nr_migratepages = 0,
17207f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
17217f354a54SDavid Rientjes 		.total_free_scanned = 0,
172256de7263SMel Gorman 		.order = order,
17236d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
172456de7263SMel Gorman 		.zone = zone,
1725a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
1726a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
1727ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
1728ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
1729accf6242SVlastimil Babka 		.direct_compaction = true,
1730a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
17319f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
17329f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
173356de7263SMel Gorman 	};
173456de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
173556de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
173656de7263SMel Gorman 
1737e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1738e64c5237SShaohua Li 
1739e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1740e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1741e64c5237SShaohua Li 
1742e64c5237SShaohua Li 	return ret;
174356de7263SMel Gorman }
174456de7263SMel Gorman 
17455e771905SMel Gorman int sysctl_extfrag_threshold = 500;
17465e771905SMel Gorman 
174756de7263SMel Gorman /**
174856de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
174956de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
17501a6d53a1SVlastimil Babka  * @order: The order of the current allocation
17511a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
17521a6d53a1SVlastimil Babka  * @ac: The context of current allocation
1753e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
175456de7263SMel Gorman  *
175556de7263SMel Gorman  * This is the main entry point for direct page compaction.
175656de7263SMel Gorman  */
1757ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
1758c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
1759c3486f53SVlastimil Babka 		enum compact_priority prio)
176056de7263SMel Gorman {
176156de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
176256de7263SMel Gorman 	struct zoneref *z;
176356de7263SMel Gorman 	struct zone *zone;
17641d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
176556de7263SMel Gorman 
176673e64c51SMichal Hocko 	/*
176773e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
176873e64c51SMichal Hocko 	 * tricky context because the migration might require IO
176973e64c51SMichal Hocko 	 */
177073e64c51SMichal Hocko 	if (!may_perform_io)
177153853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
177256de7263SMel Gorman 
1773a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
1774837d026dSJoonsoo Kim 
177556de7263SMel Gorman 	/* Compact each zone in the list */
17761a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
17771a6d53a1SVlastimil Babka 								ac->nodemask) {
1778ea7ab982SMichal Hocko 		enum compact_result status;
177956de7263SMel Gorman 
1780a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
1781a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
17821d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
178353853e2dSVlastimil Babka 			continue;
17841d4746d3SMichal Hocko 		}
178553853e2dSVlastimil Babka 
1786a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
1787c3486f53SVlastimil Babka 					alloc_flags, ac_classzone_idx(ac));
178856de7263SMel Gorman 		rc = max(status, rc);
178956de7263SMel Gorman 
17907ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
17917ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
179253853e2dSVlastimil Babka 			/*
179353853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
179453853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
179553853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
179653853e2dSVlastimil Babka 			 * succeeds in this zone.
179753853e2dSVlastimil Babka 			 */
179853853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
17991f9efdefSVlastimil Babka 
1800c3486f53SVlastimil Babka 			break;
18011f9efdefSVlastimil Babka 		}
18021f9efdefSVlastimil Babka 
1803a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
1804c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
180553853e2dSVlastimil Babka 			/*
180653853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
180753853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
180853853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
180953853e2dSVlastimil Babka 			 */
181053853e2dSVlastimil Babka 			defer_compaction(zone, order);
18111f9efdefSVlastimil Babka 
18121f9efdefSVlastimil Babka 		/*
18131f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
18141f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
1815c3486f53SVlastimil Babka 		 * case do not try further zones
18161f9efdefSVlastimil Babka 		 */
1817c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
1818c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
18191f9efdefSVlastimil Babka 			break;
18201f9efdefSVlastimil Babka 	}
18211f9efdefSVlastimil Babka 
182256de7263SMel Gorman 	return rc;
182356de7263SMel Gorman }
182456de7263SMel Gorman 
182556de7263SMel Gorman 
182676ab0f53SMel Gorman /* Compact all zones within a node */
18277103f16dSAndrew Morton static void compact_node(int nid)
18287be62de9SRik van Riel {
1829791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
1830791cae96SVlastimil Babka 	int zoneid;
1831791cae96SVlastimil Babka 	struct zone *zone;
18327be62de9SRik van Riel 	struct compact_control cc = {
18337be62de9SRik van Riel 		.order = -1,
18347f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
18357f354a54SDavid Rientjes 		.total_free_scanned = 0,
1836e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
183791ca9186SDavid Rientjes 		.ignore_skip_hint = true,
183806ed2998SVlastimil Babka 		.whole_zone = true,
183973e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
18407be62de9SRik van Riel 	};
18417be62de9SRik van Riel 
1842791cae96SVlastimil Babka 
1843791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1844791cae96SVlastimil Babka 
1845791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1846791cae96SVlastimil Babka 		if (!populated_zone(zone))
1847791cae96SVlastimil Babka 			continue;
1848791cae96SVlastimil Babka 
1849791cae96SVlastimil Babka 		cc.nr_freepages = 0;
1850791cae96SVlastimil Babka 		cc.nr_migratepages = 0;
1851791cae96SVlastimil Babka 		cc.zone = zone;
1852791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1853791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1854791cae96SVlastimil Babka 
1855791cae96SVlastimil Babka 		compact_zone(zone, &cc);
1856791cae96SVlastimil Babka 
1857791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
1858791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
1859791cae96SVlastimil Babka 	}
18607be62de9SRik van Riel }
18617be62de9SRik van Riel 
186276ab0f53SMel Gorman /* Compact all nodes in the system */
18637964c06dSJason Liu static void compact_nodes(void)
186476ab0f53SMel Gorman {
186576ab0f53SMel Gorman 	int nid;
186676ab0f53SMel Gorman 
18678575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
18688575ec29SHugh Dickins 	lru_add_drain_all();
18698575ec29SHugh Dickins 
187076ab0f53SMel Gorman 	for_each_online_node(nid)
187176ab0f53SMel Gorman 		compact_node(nid);
187276ab0f53SMel Gorman }
187376ab0f53SMel Gorman 
187476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
187576ab0f53SMel Gorman int sysctl_compact_memory;
187676ab0f53SMel Gorman 
1877fec4eb2cSYaowei Bai /*
1878fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
1879fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
1880fec4eb2cSYaowei Bai  */
188176ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
188276ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
188376ab0f53SMel Gorman {
188476ab0f53SMel Gorman 	if (write)
18857964c06dSJason Liu 		compact_nodes();
188676ab0f53SMel Gorman 
188776ab0f53SMel Gorman 	return 0;
188876ab0f53SMel Gorman }
1889ed4a6d7fSMel Gorman 
18905e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
18915e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
18925e771905SMel Gorman {
18935e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
18945e771905SMel Gorman 
18955e771905SMel Gorman 	return 0;
18965e771905SMel Gorman }
18975e771905SMel Gorman 
1898ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
189974e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
190010fbcf4cSKay Sievers 			struct device_attribute *attr,
1901ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1902ed4a6d7fSMel Gorman {
19038575ec29SHugh Dickins 	int nid = dev->id;
19048575ec29SHugh Dickins 
19058575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
19068575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
19078575ec29SHugh Dickins 		lru_add_drain_all();
19088575ec29SHugh Dickins 
19098575ec29SHugh Dickins 		compact_node(nid);
19108575ec29SHugh Dickins 	}
1911ed4a6d7fSMel Gorman 
1912ed4a6d7fSMel Gorman 	return count;
1913ed4a6d7fSMel Gorman }
191410fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1915ed4a6d7fSMel Gorman 
1916ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1917ed4a6d7fSMel Gorman {
191810fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1919ed4a6d7fSMel Gorman }
1920ed4a6d7fSMel Gorman 
1921ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1922ed4a6d7fSMel Gorman {
192310fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1924ed4a6d7fSMel Gorman }
1925ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1926ff9543fdSMichal Nazarewicz 
1927698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1928698b1b30SVlastimil Babka {
1929172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
1930698b1b30SVlastimil Babka }
1931698b1b30SVlastimil Babka 
1932698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
1933698b1b30SVlastimil Babka {
1934698b1b30SVlastimil Babka 	int zoneid;
1935698b1b30SVlastimil Babka 	struct zone *zone;
1936698b1b30SVlastimil Babka 	enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1937698b1b30SVlastimil Babka 
19386cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
1939698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1940698b1b30SVlastimil Babka 
1941698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1942698b1b30SVlastimil Babka 			continue;
1943698b1b30SVlastimil Babka 
1944698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1945698b1b30SVlastimil Babka 					classzone_idx) == COMPACT_CONTINUE)
1946698b1b30SVlastimil Babka 			return true;
1947698b1b30SVlastimil Babka 	}
1948698b1b30SVlastimil Babka 
1949698b1b30SVlastimil Babka 	return false;
1950698b1b30SVlastimil Babka }
1951698b1b30SVlastimil Babka 
1952698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
1953698b1b30SVlastimil Babka {
1954698b1b30SVlastimil Babka 	/*
1955698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
1956698b1b30SVlastimil Babka 	 * order is allocatable.
1957698b1b30SVlastimil Babka 	 */
1958698b1b30SVlastimil Babka 	int zoneid;
1959698b1b30SVlastimil Babka 	struct zone *zone;
1960698b1b30SVlastimil Babka 	struct compact_control cc = {
1961698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
19627f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
19637f354a54SDavid Rientjes 		.total_free_scanned = 0,
1964698b1b30SVlastimil Babka 		.classzone_idx = pgdat->kcompactd_classzone_idx,
1965698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
1966a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
196773e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
1968698b1b30SVlastimil Babka 	};
1969698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1970698b1b30SVlastimil Babka 							cc.classzone_idx);
19717f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
1972698b1b30SVlastimil Babka 
19736cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
1974698b1b30SVlastimil Babka 		int status;
1975698b1b30SVlastimil Babka 
1976698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1977698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1978698b1b30SVlastimil Babka 			continue;
1979698b1b30SVlastimil Babka 
1980698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
1981698b1b30SVlastimil Babka 			continue;
1982698b1b30SVlastimil Babka 
1983698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1984698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
1985698b1b30SVlastimil Babka 			continue;
1986698b1b30SVlastimil Babka 
1987698b1b30SVlastimil Babka 		cc.nr_freepages = 0;
1988698b1b30SVlastimil Babka 		cc.nr_migratepages = 0;
19897f354a54SDavid Rientjes 		cc.total_migrate_scanned = 0;
19907f354a54SDavid Rientjes 		cc.total_free_scanned = 0;
1991698b1b30SVlastimil Babka 		cc.zone = zone;
1992698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1993698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1994698b1b30SVlastimil Babka 
1995172400c6SVlastimil Babka 		if (kthread_should_stop())
1996172400c6SVlastimil Babka 			return;
1997698b1b30SVlastimil Babka 		status = compact_zone(zone, &cc);
1998698b1b30SVlastimil Babka 
19997ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
2000698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
2001c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
2002698b1b30SVlastimil Babka 			/*
2003698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
2004698b1b30SVlastimil Babka 			 * sync direct compaction does.
2005698b1b30SVlastimil Babka 			 */
2006698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
2007698b1b30SVlastimil Babka 		}
2008698b1b30SVlastimil Babka 
20097f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
20107f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
20117f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
20127f354a54SDavid Rientjes 				     cc.total_free_scanned);
20137f354a54SDavid Rientjes 
2014698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2015698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2016698b1b30SVlastimil Babka 	}
2017698b1b30SVlastimil Babka 
2018698b1b30SVlastimil Babka 	/*
2019698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
2020698b1b30SVlastimil Babka 	 * the requested order/classzone_idx in case it was higher/tighter than
2021698b1b30SVlastimil Babka 	 * our current ones
2022698b1b30SVlastimil Babka 	 */
2023698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2024698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
2025698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2026698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2027698b1b30SVlastimil Babka }
2028698b1b30SVlastimil Babka 
2029698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2030698b1b30SVlastimil Babka {
2031698b1b30SVlastimil Babka 	if (!order)
2032698b1b30SVlastimil Babka 		return;
2033698b1b30SVlastimil Babka 
2034698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2035698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2036698b1b30SVlastimil Babka 
2037698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx > classzone_idx)
2038698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = classzone_idx;
2039698b1b30SVlastimil Babka 
20406818600fSDavidlohr Bueso 	/*
20416818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
20426818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
20436818600fSDavidlohr Bueso 	 */
20446818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2045698b1b30SVlastimil Babka 		return;
2046698b1b30SVlastimil Babka 
2047698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2048698b1b30SVlastimil Babka 		return;
2049698b1b30SVlastimil Babka 
2050698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2051698b1b30SVlastimil Babka 							classzone_idx);
2052698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2053698b1b30SVlastimil Babka }
2054698b1b30SVlastimil Babka 
2055698b1b30SVlastimil Babka /*
2056698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2057698b1b30SVlastimil Babka  * from the init process.
2058698b1b30SVlastimil Babka  */
2059698b1b30SVlastimil Babka static int kcompactd(void *p)
2060698b1b30SVlastimil Babka {
2061698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2062698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2063698b1b30SVlastimil Babka 
2064698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2065698b1b30SVlastimil Babka 
2066698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2067698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2068698b1b30SVlastimil Babka 
2069698b1b30SVlastimil Babka 	set_freezable();
2070698b1b30SVlastimil Babka 
2071698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
2072698b1b30SVlastimil Babka 	pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2073698b1b30SVlastimil Babka 
2074698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2075698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2076698b1b30SVlastimil Babka 		wait_event_freezable(pgdat->kcompactd_wait,
2077698b1b30SVlastimil Babka 				kcompactd_work_requested(pgdat));
2078698b1b30SVlastimil Babka 
2079698b1b30SVlastimil Babka 		kcompactd_do_work(pgdat);
2080698b1b30SVlastimil Babka 	}
2081698b1b30SVlastimil Babka 
2082698b1b30SVlastimil Babka 	return 0;
2083698b1b30SVlastimil Babka }
2084698b1b30SVlastimil Babka 
2085698b1b30SVlastimil Babka /*
2086698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2087698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2088698b1b30SVlastimil Babka  */
2089698b1b30SVlastimil Babka int kcompactd_run(int nid)
2090698b1b30SVlastimil Babka {
2091698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2092698b1b30SVlastimil Babka 	int ret = 0;
2093698b1b30SVlastimil Babka 
2094698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2095698b1b30SVlastimil Babka 		return 0;
2096698b1b30SVlastimil Babka 
2097698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2098698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2099698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2100698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2101698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2102698b1b30SVlastimil Babka 	}
2103698b1b30SVlastimil Babka 	return ret;
2104698b1b30SVlastimil Babka }
2105698b1b30SVlastimil Babka 
2106698b1b30SVlastimil Babka /*
2107698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2108698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2109698b1b30SVlastimil Babka  */
2110698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2111698b1b30SVlastimil Babka {
2112698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2113698b1b30SVlastimil Babka 
2114698b1b30SVlastimil Babka 	if (kcompactd) {
2115698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2116698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2117698b1b30SVlastimil Babka 	}
2118698b1b30SVlastimil Babka }
2119698b1b30SVlastimil Babka 
2120698b1b30SVlastimil Babka /*
2121698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2122698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2123698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2124698b1b30SVlastimil Babka  * restore their cpu bindings.
2125698b1b30SVlastimil Babka  */
2126e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2127698b1b30SVlastimil Babka {
2128698b1b30SVlastimil Babka 	int nid;
2129698b1b30SVlastimil Babka 
2130698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2131698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2132698b1b30SVlastimil Babka 		const struct cpumask *mask;
2133698b1b30SVlastimil Babka 
2134698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2135698b1b30SVlastimil Babka 
2136698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2137698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2138698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2139698b1b30SVlastimil Babka 	}
2140e46b1db2SAnna-Maria Gleixner 	return 0;
2141698b1b30SVlastimil Babka }
2142698b1b30SVlastimil Babka 
2143698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2144698b1b30SVlastimil Babka {
2145698b1b30SVlastimil Babka 	int nid;
2146e46b1db2SAnna-Maria Gleixner 	int ret;
2147e46b1db2SAnna-Maria Gleixner 
2148e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2149e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2150e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2151e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2152e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2153e46b1db2SAnna-Maria Gleixner 		return ret;
2154e46b1db2SAnna-Maria Gleixner 	}
2155698b1b30SVlastimil Babka 
2156698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2157698b1b30SVlastimil Babka 		kcompactd_run(nid);
2158698b1b30SVlastimil Babka 	return 0;
2159698b1b30SVlastimil Babka }
2160698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2161698b1b30SVlastimil Babka 
2162ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2163