xref: /openbmc/linux/mm/compaction.c (revision 6c357848b44b4016ca422178aa368a7472245f6f)
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>
25eb414681SJohannes Weiner #include <linux/psi.h>
26748446bbSMel Gorman #include "internal.h"
27748446bbSMel Gorman 
28010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
29010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
30010fc29aSMinchan Kim {
31010fc29aSMinchan Kim 	count_vm_event(item);
32010fc29aSMinchan Kim }
33010fc29aSMinchan Kim 
34010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
35010fc29aSMinchan Kim {
36010fc29aSMinchan Kim 	count_vm_events(item, delta);
37010fc29aSMinchan Kim }
38010fc29aSMinchan Kim #else
39010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
40010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
41010fc29aSMinchan Kim #endif
42010fc29aSMinchan Kim 
43ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
44ff9543fdSMichal Nazarewicz 
45b7aba698SMel Gorman #define CREATE_TRACE_POINTS
46b7aba698SMel Gorman #include <trace/events/compaction.h>
47b7aba698SMel Gorman 
4806b6640aSVlastimil Babka #define block_start_pfn(pfn, order)	round_down(pfn, 1UL << (order))
4906b6640aSVlastimil Babka #define block_end_pfn(pfn, order)	ALIGN((pfn) + 1, 1UL << (order))
5006b6640aSVlastimil Babka #define pageblock_start_pfn(pfn)	block_start_pfn(pfn, pageblock_order)
5106b6640aSVlastimil Babka #define pageblock_end_pfn(pfn)		block_end_pfn(pfn, pageblock_order)
5206b6640aSVlastimil Babka 
53facdaa91SNitin Gupta /*
54facdaa91SNitin Gupta  * Fragmentation score check interval for proactive compaction purposes.
55facdaa91SNitin Gupta  */
56d34c0a75SNitin Gupta static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500;
57facdaa91SNitin Gupta 
58facdaa91SNitin Gupta /*
59facdaa91SNitin Gupta  * Page order with-respect-to which proactive compaction
60facdaa91SNitin Gupta  * calculates external fragmentation, which is used as
61facdaa91SNitin Gupta  * the "fragmentation score" of a node/zone.
62facdaa91SNitin Gupta  */
63facdaa91SNitin Gupta #if defined CONFIG_TRANSPARENT_HUGEPAGE
64facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER	HPAGE_PMD_ORDER
6525788738SNitin Gupta #elif defined CONFIG_HUGETLBFS
66facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER	HUGETLB_PAGE_ORDER
67facdaa91SNitin Gupta #else
68facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER	(PMD_SHIFT - PAGE_SHIFT)
69facdaa91SNitin Gupta #endif
70facdaa91SNitin Gupta 
71748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
72748446bbSMel Gorman {
73748446bbSMel Gorman 	struct page *page, *next;
746bace090SVlastimil Babka 	unsigned long high_pfn = 0;
75748446bbSMel Gorman 
76748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
776bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
78748446bbSMel Gorman 		list_del(&page->lru);
79748446bbSMel Gorman 		__free_page(page);
806bace090SVlastimil Babka 		if (pfn > high_pfn)
816bace090SVlastimil Babka 			high_pfn = pfn;
82748446bbSMel Gorman 	}
83748446bbSMel Gorman 
846bace090SVlastimil Babka 	return high_pfn;
85748446bbSMel Gorman }
86748446bbSMel Gorman 
874469ab98SMel Gorman static void split_map_pages(struct list_head *list)
88ff9543fdSMichal Nazarewicz {
8966c64223SJoonsoo Kim 	unsigned int i, order, nr_pages;
9066c64223SJoonsoo Kim 	struct page *page, *next;
9166c64223SJoonsoo Kim 	LIST_HEAD(tmp_list);
92ff9543fdSMichal Nazarewicz 
9366c64223SJoonsoo Kim 	list_for_each_entry_safe(page, next, list, lru) {
9466c64223SJoonsoo Kim 		list_del(&page->lru);
9566c64223SJoonsoo Kim 
9666c64223SJoonsoo Kim 		order = page_private(page);
9766c64223SJoonsoo Kim 		nr_pages = 1 << order;
9866c64223SJoonsoo Kim 
9946f24fd8SJoonsoo Kim 		post_alloc_hook(page, order, __GFP_MOVABLE);
10066c64223SJoonsoo Kim 		if (order)
10166c64223SJoonsoo Kim 			split_page(page, order);
10266c64223SJoonsoo Kim 
10366c64223SJoonsoo Kim 		for (i = 0; i < nr_pages; i++) {
10466c64223SJoonsoo Kim 			list_add(&page->lru, &tmp_list);
10566c64223SJoonsoo Kim 			page++;
106ff9543fdSMichal Nazarewicz 		}
107ff9543fdSMichal Nazarewicz 	}
108ff9543fdSMichal Nazarewicz 
10966c64223SJoonsoo Kim 	list_splice(&tmp_list, list);
11066c64223SJoonsoo Kim }
11166c64223SJoonsoo Kim 
112bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
11324e2716fSJoonsoo Kim 
114bda807d4SMinchan Kim int PageMovable(struct page *page)
115bda807d4SMinchan Kim {
116bda807d4SMinchan Kim 	struct address_space *mapping;
117bda807d4SMinchan Kim 
118bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
119bda807d4SMinchan Kim 	if (!__PageMovable(page))
120bda807d4SMinchan Kim 		return 0;
121bda807d4SMinchan Kim 
122bda807d4SMinchan Kim 	mapping = page_mapping(page);
123bda807d4SMinchan Kim 	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
124bda807d4SMinchan Kim 		return 1;
125bda807d4SMinchan Kim 
126bda807d4SMinchan Kim 	return 0;
127bda807d4SMinchan Kim }
128bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable);
129bda807d4SMinchan Kim 
130bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping)
131bda807d4SMinchan Kim {
132bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
133bda807d4SMinchan Kim 	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
134bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
135bda807d4SMinchan Kim }
136bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable);
137bda807d4SMinchan Kim 
138bda807d4SMinchan Kim void __ClearPageMovable(struct page *page)
139bda807d4SMinchan Kim {
140bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
141bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageMovable(page), page);
142bda807d4SMinchan Kim 	/*
143bda807d4SMinchan Kim 	 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
144bda807d4SMinchan Kim 	 * flag so that VM can catch up released page by driver after isolation.
145bda807d4SMinchan Kim 	 * With it, VM migration doesn't try to put it back.
146bda807d4SMinchan Kim 	 */
147bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)page->mapping &
148bda807d4SMinchan Kim 				PAGE_MAPPING_MOVABLE);
149bda807d4SMinchan Kim }
150bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable);
151bda807d4SMinchan Kim 
15224e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
15324e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
15424e2716fSJoonsoo Kim 
15524e2716fSJoonsoo Kim /*
15624e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
157860b3272SAlex Shi  * allocation success. 1 << compact_defer_shift, compactions are skipped up
15824e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
15924e2716fSJoonsoo Kim  */
16024e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
16124e2716fSJoonsoo Kim {
16224e2716fSJoonsoo Kim 	zone->compact_considered = 0;
16324e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
16424e2716fSJoonsoo Kim 
16524e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
16624e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
16724e2716fSJoonsoo Kim 
16824e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
16924e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
17024e2716fSJoonsoo Kim 
17124e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
17224e2716fSJoonsoo Kim }
17324e2716fSJoonsoo Kim 
17424e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
17524e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
17624e2716fSJoonsoo Kim {
17724e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
17824e2716fSJoonsoo Kim 
17924e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
18024e2716fSJoonsoo Kim 		return false;
18124e2716fSJoonsoo Kim 
18224e2716fSJoonsoo Kim 	/* Avoid possible overflow */
18324e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
18424e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
18524e2716fSJoonsoo Kim 
18624e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
18724e2716fSJoonsoo Kim 		return false;
18824e2716fSJoonsoo Kim 
18924e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
19024e2716fSJoonsoo Kim 
19124e2716fSJoonsoo Kim 	return true;
19224e2716fSJoonsoo Kim }
19324e2716fSJoonsoo Kim 
19424e2716fSJoonsoo Kim /*
19524e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
19624e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
19724e2716fSJoonsoo Kim  * expected to succeed.
19824e2716fSJoonsoo Kim  */
19924e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
20024e2716fSJoonsoo Kim 		bool alloc_success)
20124e2716fSJoonsoo Kim {
20224e2716fSJoonsoo Kim 	if (alloc_success) {
20324e2716fSJoonsoo Kim 		zone->compact_considered = 0;
20424e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
20524e2716fSJoonsoo Kim 	}
20624e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
20724e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
20824e2716fSJoonsoo Kim 
20924e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
21024e2716fSJoonsoo Kim }
21124e2716fSJoonsoo Kim 
21224e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
21324e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
21424e2716fSJoonsoo Kim {
21524e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
21624e2716fSJoonsoo Kim 		return false;
21724e2716fSJoonsoo Kim 
21824e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
21924e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
22024e2716fSJoonsoo Kim }
22124e2716fSJoonsoo Kim 
222bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
223bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
224bb13ffebSMel Gorman 					struct page *page)
225bb13ffebSMel Gorman {
226bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
227bb13ffebSMel Gorman 		return true;
228bb13ffebSMel Gorman 
229bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
230bb13ffebSMel Gorman }
231bb13ffebSMel Gorman 
23202333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
23302333641SVlastimil Babka {
23402333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
23502333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
236623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
23706b6640aSVlastimil Babka 				pageblock_start_pfn(zone_end_pfn(zone) - 1);
23802333641SVlastimil Babka }
23902333641SVlastimil Babka 
240bb13ffebSMel Gorman /*
241b527cfe5SVlastimil Babka  * Compound pages of >= pageblock_order should consistenly be skipped until
242b527cfe5SVlastimil Babka  * released. It is always pointless to compact pages of such order (if they are
243b527cfe5SVlastimil Babka  * migratable), and the pageblocks they occupy cannot contain any free pages.
24421dc7e02SDavid Rientjes  */
245b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page)
24621dc7e02SDavid Rientjes {
247b527cfe5SVlastimil Babka 	if (!PageCompound(page))
24821dc7e02SDavid Rientjes 		return false;
249b527cfe5SVlastimil Babka 
250b527cfe5SVlastimil Babka 	page = compound_head(page);
251b527cfe5SVlastimil Babka 
252b527cfe5SVlastimil Babka 	if (compound_order(page) >= pageblock_order)
25321dc7e02SDavid Rientjes 		return true;
254b527cfe5SVlastimil Babka 
255b527cfe5SVlastimil Babka 	return false;
25621dc7e02SDavid Rientjes }
25721dc7e02SDavid Rientjes 
258e332f741SMel Gorman static bool
259e332f741SMel Gorman __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
260e332f741SMel Gorman 							bool check_target)
261e332f741SMel Gorman {
262e332f741SMel Gorman 	struct page *page = pfn_to_online_page(pfn);
2636b0868c8SMel Gorman 	struct page *block_page;
264e332f741SMel Gorman 	struct page *end_page;
265e332f741SMel Gorman 	unsigned long block_pfn;
266e332f741SMel Gorman 
267e332f741SMel Gorman 	if (!page)
268e332f741SMel Gorman 		return false;
269e332f741SMel Gorman 	if (zone != page_zone(page))
270e332f741SMel Gorman 		return false;
271e332f741SMel Gorman 	if (pageblock_skip_persistent(page))
272e332f741SMel Gorman 		return false;
273e332f741SMel Gorman 
274e332f741SMel Gorman 	/*
275e332f741SMel Gorman 	 * If skip is already cleared do no further checking once the
276e332f741SMel Gorman 	 * restart points have been set.
277e332f741SMel Gorman 	 */
278e332f741SMel Gorman 	if (check_source && check_target && !get_pageblock_skip(page))
279e332f741SMel Gorman 		return true;
280e332f741SMel Gorman 
281e332f741SMel Gorman 	/*
282e332f741SMel Gorman 	 * If clearing skip for the target scanner, do not select a
283e332f741SMel Gorman 	 * non-movable pageblock as the starting point.
284e332f741SMel Gorman 	 */
285e332f741SMel Gorman 	if (!check_source && check_target &&
286e332f741SMel Gorman 	    get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
287e332f741SMel Gorman 		return false;
288e332f741SMel Gorman 
2896b0868c8SMel Gorman 	/* Ensure the start of the pageblock or zone is online and valid */
2906b0868c8SMel Gorman 	block_pfn = pageblock_start_pfn(pfn);
291a2e9a5afSVlastimil Babka 	block_pfn = max(block_pfn, zone->zone_start_pfn);
292a2e9a5afSVlastimil Babka 	block_page = pfn_to_online_page(block_pfn);
2936b0868c8SMel Gorman 	if (block_page) {
2946b0868c8SMel Gorman 		page = block_page;
2956b0868c8SMel Gorman 		pfn = block_pfn;
2966b0868c8SMel Gorman 	}
2976b0868c8SMel Gorman 
2986b0868c8SMel Gorman 	/* Ensure the end of the pageblock or zone is online and valid */
299a2e9a5afSVlastimil Babka 	block_pfn = pageblock_end_pfn(pfn) - 1;
3006b0868c8SMel Gorman 	block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
3016b0868c8SMel Gorman 	end_page = pfn_to_online_page(block_pfn);
3026b0868c8SMel Gorman 	if (!end_page)
3036b0868c8SMel Gorman 		return false;
3046b0868c8SMel Gorman 
305e332f741SMel Gorman 	/*
306e332f741SMel Gorman 	 * Only clear the hint if a sample indicates there is either a
307e332f741SMel Gorman 	 * free page or an LRU page in the block. One or other condition
308e332f741SMel Gorman 	 * is necessary for the block to be a migration source/target.
309e332f741SMel Gorman 	 */
310e332f741SMel Gorman 	do {
311e332f741SMel Gorman 		if (pfn_valid_within(pfn)) {
312e332f741SMel Gorman 			if (check_source && PageLRU(page)) {
313e332f741SMel Gorman 				clear_pageblock_skip(page);
314e332f741SMel Gorman 				return true;
315e332f741SMel Gorman 			}
316e332f741SMel Gorman 
317e332f741SMel Gorman 			if (check_target && PageBuddy(page)) {
318e332f741SMel Gorman 				clear_pageblock_skip(page);
319e332f741SMel Gorman 				return true;
320e332f741SMel Gorman 			}
321e332f741SMel Gorman 		}
322e332f741SMel Gorman 
323e332f741SMel Gorman 		page += (1 << PAGE_ALLOC_COSTLY_ORDER);
324e332f741SMel Gorman 		pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
325a2e9a5afSVlastimil Babka 	} while (page <= end_page);
326e332f741SMel Gorman 
327e332f741SMel Gorman 	return false;
328e332f741SMel Gorman }
329e332f741SMel Gorman 
33021dc7e02SDavid Rientjes /*
331bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
332bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
333bb13ffebSMel Gorman  * meet.
334bb13ffebSMel Gorman  */
33562997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
336bb13ffebSMel Gorman {
337e332f741SMel Gorman 	unsigned long migrate_pfn = zone->zone_start_pfn;
3386b0868c8SMel Gorman 	unsigned long free_pfn = zone_end_pfn(zone) - 1;
339e332f741SMel Gorman 	unsigned long reset_migrate = free_pfn;
340e332f741SMel Gorman 	unsigned long reset_free = migrate_pfn;
341e332f741SMel Gorman 	bool source_set = false;
342e332f741SMel Gorman 	bool free_set = false;
343e332f741SMel Gorman 
344e332f741SMel Gorman 	if (!zone->compact_blockskip_flush)
345e332f741SMel Gorman 		return;
346bb13ffebSMel Gorman 
34762997027SMel Gorman 	zone->compact_blockskip_flush = false;
348bb13ffebSMel Gorman 
349e332f741SMel Gorman 	/*
350e332f741SMel Gorman 	 * Walk the zone and update pageblock skip information. Source looks
351e332f741SMel Gorman 	 * for PageLRU while target looks for PageBuddy. When the scanner
352e332f741SMel Gorman 	 * is found, both PageBuddy and PageLRU are checked as the pageblock
353e332f741SMel Gorman 	 * is suitable as both source and target.
354e332f741SMel Gorman 	 */
355e332f741SMel Gorman 	for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
356e332f741SMel Gorman 					free_pfn -= pageblock_nr_pages) {
357bb13ffebSMel Gorman 		cond_resched();
358bb13ffebSMel Gorman 
359e332f741SMel Gorman 		/* Update the migrate PFN */
360e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
361e332f741SMel Gorman 		    migrate_pfn < reset_migrate) {
362e332f741SMel Gorman 			source_set = true;
363e332f741SMel Gorman 			reset_migrate = migrate_pfn;
364e332f741SMel Gorman 			zone->compact_init_migrate_pfn = reset_migrate;
365e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[0] = reset_migrate;
366e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[1] = reset_migrate;
367bb13ffebSMel Gorman 		}
36802333641SVlastimil Babka 
369e332f741SMel Gorman 		/* Update the free PFN */
370e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
371e332f741SMel Gorman 		    free_pfn > reset_free) {
372e332f741SMel Gorman 			free_set = true;
373e332f741SMel Gorman 			reset_free = free_pfn;
374e332f741SMel Gorman 			zone->compact_init_free_pfn = reset_free;
375e332f741SMel Gorman 			zone->compact_cached_free_pfn = reset_free;
376e332f741SMel Gorman 		}
377e332f741SMel Gorman 	}
378e332f741SMel Gorman 
379e332f741SMel Gorman 	/* Leave no distance if no suitable block was reset */
380e332f741SMel Gorman 	if (reset_migrate >= reset_free) {
381e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[0] = migrate_pfn;
382e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[1] = migrate_pfn;
383e332f741SMel Gorman 		zone->compact_cached_free_pfn = free_pfn;
384e332f741SMel Gorman 	}
385bb13ffebSMel Gorman }
386bb13ffebSMel Gorman 
38762997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
38862997027SMel Gorman {
38962997027SMel Gorman 	int zoneid;
39062997027SMel Gorman 
39162997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
39262997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
39362997027SMel Gorman 		if (!populated_zone(zone))
39462997027SMel Gorman 			continue;
39562997027SMel Gorman 
39662997027SMel Gorman 		/* Only flush if a full compaction finished recently */
39762997027SMel Gorman 		if (zone->compact_blockskip_flush)
39862997027SMel Gorman 			__reset_isolation_suitable(zone);
39962997027SMel Gorman 	}
40062997027SMel Gorman }
40162997027SMel Gorman 
402bb13ffebSMel Gorman /*
403e380bebeSMel Gorman  * Sets the pageblock skip bit if it was clear. Note that this is a hint as
404e380bebeSMel Gorman  * locks are not required for read/writers. Returns true if it was already set.
405e380bebeSMel Gorman  */
406e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
407e380bebeSMel Gorman 							unsigned long pfn)
408e380bebeSMel Gorman {
409e380bebeSMel Gorman 	bool skip;
410e380bebeSMel Gorman 
411e380bebeSMel Gorman 	/* Do no update if skip hint is being ignored */
412e380bebeSMel Gorman 	if (cc->ignore_skip_hint)
413e380bebeSMel Gorman 		return false;
414e380bebeSMel Gorman 
415e380bebeSMel Gorman 	if (!IS_ALIGNED(pfn, pageblock_nr_pages))
416e380bebeSMel Gorman 		return false;
417e380bebeSMel Gorman 
418e380bebeSMel Gorman 	skip = get_pageblock_skip(page);
419e380bebeSMel Gorman 	if (!skip && !cc->no_set_skip_hint)
420e380bebeSMel Gorman 		set_pageblock_skip(page);
421e380bebeSMel Gorman 
422e380bebeSMel Gorman 	return skip;
423e380bebeSMel Gorman }
424e380bebeSMel Gorman 
425e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
426e380bebeSMel Gorman {
427e380bebeSMel Gorman 	struct zone *zone = cc->zone;
428e380bebeSMel Gorman 
429e380bebeSMel Gorman 	pfn = pageblock_end_pfn(pfn);
430e380bebeSMel Gorman 
431e380bebeSMel Gorman 	/* Set for isolation rather than compaction */
432e380bebeSMel Gorman 	if (cc->no_set_skip_hint)
433e380bebeSMel Gorman 		return;
434e380bebeSMel Gorman 
435e380bebeSMel Gorman 	if (pfn > zone->compact_cached_migrate_pfn[0])
436e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[0] = pfn;
437e380bebeSMel Gorman 	if (cc->mode != MIGRATE_ASYNC &&
438e380bebeSMel Gorman 	    pfn > zone->compact_cached_migrate_pfn[1])
439e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[1] = pfn;
440e380bebeSMel Gorman }
441e380bebeSMel Gorman 
442e380bebeSMel Gorman /*
443bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
44462997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
445bb13ffebSMel Gorman  */
446c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
447d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
448bb13ffebSMel Gorman {
449c89511abSMel Gorman 	struct zone *zone = cc->zone;
4506815bf3fSJoonsoo Kim 
4512583d671SVlastimil Babka 	if (cc->no_set_skip_hint)
4526815bf3fSJoonsoo Kim 		return;
4536815bf3fSJoonsoo Kim 
454bb13ffebSMel Gorman 	if (!page)
455bb13ffebSMel Gorman 		return;
456bb13ffebSMel Gorman 
457bb13ffebSMel Gorman 	set_pageblock_skip(page);
458c89511abSMel Gorman 
45935979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
46035979ef3SDavid Rientjes 	if (pfn < zone->compact_cached_free_pfn)
461c89511abSMel Gorman 		zone->compact_cached_free_pfn = pfn;
462c89511abSMel Gorman }
463bb13ffebSMel Gorman #else
464bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
465bb13ffebSMel Gorman 					struct page *page)
466bb13ffebSMel Gorman {
467bb13ffebSMel Gorman 	return true;
468bb13ffebSMel Gorman }
469bb13ffebSMel Gorman 
470b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page)
47121dc7e02SDavid Rientjes {
47221dc7e02SDavid Rientjes 	return false;
47321dc7e02SDavid Rientjes }
47421dc7e02SDavid Rientjes 
47521dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc,
476d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
477bb13ffebSMel Gorman {
478bb13ffebSMel Gorman }
479e380bebeSMel Gorman 
480e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
481e380bebeSMel Gorman {
482e380bebeSMel Gorman }
483e380bebeSMel Gorman 
484e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
485e380bebeSMel Gorman 							unsigned long pfn)
486e380bebeSMel Gorman {
487e380bebeSMel Gorman 	return false;
488e380bebeSMel Gorman }
489bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
490bb13ffebSMel Gorman 
4911f9efdefSVlastimil Babka /*
4928b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
493cb2dcaf0SMel Gorman  * very heavily contended. For async compaction, trylock and record if the
494cb2dcaf0SMel Gorman  * lock is contended. The lock will still be acquired but compaction will
495cb2dcaf0SMel Gorman  * abort when the current block is finished regardless of success rate.
496cb2dcaf0SMel Gorman  * Sync compaction acquires the lock.
4978b44d279SVlastimil Babka  *
498cb2dcaf0SMel Gorman  * Always returns true which makes it easier to track lock state in callers.
4991f9efdefSVlastimil Babka  */
500cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
5018b44d279SVlastimil Babka 						struct compact_control *cc)
50277337edeSJules Irenge 	__acquires(lock)
5038b44d279SVlastimil Babka {
504cb2dcaf0SMel Gorman 	/* Track if the lock is contended in async mode */
505cb2dcaf0SMel Gorman 	if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
506cb2dcaf0SMel Gorman 		if (spin_trylock_irqsave(lock, *flags))
507cb2dcaf0SMel Gorman 			return true;
508cb2dcaf0SMel Gorman 
509c3486f53SVlastimil Babka 		cc->contended = true;
5108b44d279SVlastimil Babka 	}
5111f9efdefSVlastimil Babka 
512cb2dcaf0SMel Gorman 	spin_lock_irqsave(lock, *flags);
5138b44d279SVlastimil Babka 	return true;
5142a1402aaSMel Gorman }
5152a1402aaSMel Gorman 
51685aa125fSMichal Nazarewicz /*
517c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
5188b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
5198b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
5208b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
5218b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
5228b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
5238b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
5248b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
525c67fe375SMel Gorman  *
5268b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
5278b44d279SVlastimil Babka  *		async compaction due to need_resched()
5288b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
5298b44d279SVlastimil Babka  *		scheduled)
530c67fe375SMel Gorman  */
5318b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
5328b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
533c67fe375SMel Gorman {
5348b44d279SVlastimil Babka 	if (*locked) {
5358b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
5368b44d279SVlastimil Babka 		*locked = false;
537c67fe375SMel Gorman 	}
538c67fe375SMel Gorman 
5398b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
540c3486f53SVlastimil Babka 		cc->contended = true;
5418b44d279SVlastimil Babka 		return true;
5428b44d279SVlastimil Babka 	}
5438b44d279SVlastimil Babka 
544cf66f070SMel Gorman 	cond_resched();
545be976572SVlastimil Babka 
546be976572SVlastimil Babka 	return false;
547be976572SVlastimil Babka }
548be976572SVlastimil Babka 
549c67fe375SMel Gorman /*
5509e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
5519e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
5529e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
55385aa125fSMichal Nazarewicz  */
554f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
555e14c720eSVlastimil Babka 				unsigned long *start_pfn,
55685aa125fSMichal Nazarewicz 				unsigned long end_pfn,
55785aa125fSMichal Nazarewicz 				struct list_head *freelist,
5584fca9730SMel Gorman 				unsigned int stride,
55985aa125fSMichal Nazarewicz 				bool strict)
560748446bbSMel Gorman {
561b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
562d097a6f6SMel Gorman 	struct page *cursor;
563b8b2d825SXiubo Li 	unsigned long flags = 0;
564f40d1e42SMel Gorman 	bool locked = false;
565e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
56666c64223SJoonsoo Kim 	unsigned int order;
567748446bbSMel Gorman 
5684fca9730SMel Gorman 	/* Strict mode is for isolation, speed is secondary */
5694fca9730SMel Gorman 	if (strict)
5704fca9730SMel Gorman 		stride = 1;
5714fca9730SMel Gorman 
572748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
573748446bbSMel Gorman 
574f40d1e42SMel Gorman 	/* Isolate free pages. */
5754fca9730SMel Gorman 	for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
57666c64223SJoonsoo Kim 		int isolated;
577748446bbSMel Gorman 		struct page *page = cursor;
578748446bbSMel Gorman 
5798b44d279SVlastimil Babka 		/*
5808b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
5818b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
5828b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
5838b44d279SVlastimil Babka 		 */
5848b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
5858b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
5868b44d279SVlastimil Babka 								&locked, cc))
5878b44d279SVlastimil Babka 			break;
5888b44d279SVlastimil Babka 
589b7aba698SMel Gorman 		nr_scanned++;
590f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
5912af120bcSLaura Abbott 			goto isolate_fail;
5922af120bcSLaura Abbott 
5939fcd6d2eSVlastimil Babka 		/*
5949fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
5959fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
5969fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
5979fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
5989fcd6d2eSVlastimil Babka 		 */
5999fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
60021dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
6019fcd6d2eSVlastimil Babka 
602d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER)) {
60321dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
60421dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
6059fcd6d2eSVlastimil Babka 			}
6069fcd6d2eSVlastimil Babka 			goto isolate_fail;
6079fcd6d2eSVlastimil Babka 		}
6089fcd6d2eSVlastimil Babka 
609f40d1e42SMel Gorman 		if (!PageBuddy(page))
6102af120bcSLaura Abbott 			goto isolate_fail;
611f40d1e42SMel Gorman 
612f40d1e42SMel Gorman 		/*
61369b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
61469b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
61569b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
61669b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
61769b7189fSVlastimil Babka 		 * recheck as well.
61869b7189fSVlastimil Babka 		 */
61969b7189fSVlastimil Babka 		if (!locked) {
620cb2dcaf0SMel Gorman 			locked = compact_lock_irqsave(&cc->zone->lock,
6218b44d279SVlastimil Babka 								&flags, cc);
622f40d1e42SMel Gorman 
623f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
624f40d1e42SMel Gorman 			if (!PageBuddy(page))
6252af120bcSLaura Abbott 				goto isolate_fail;
62669b7189fSVlastimil Babka 		}
627748446bbSMel Gorman 
62866c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
62966c64223SJoonsoo Kim 		order = page_order(page);
63066c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
631a4f04f2cSDavid Rientjes 		if (!isolated)
632a4f04f2cSDavid Rientjes 			break;
63366c64223SJoonsoo Kim 		set_page_private(page, order);
634a4f04f2cSDavid Rientjes 
635748446bbSMel Gorman 		total_isolated += isolated;
636a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
63766c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
63866c64223SJoonsoo Kim 
639a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
640932ff6bbSJoonsoo Kim 			blockpfn += isolated;
641932ff6bbSJoonsoo Kim 			break;
642932ff6bbSJoonsoo Kim 		}
643a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
644748446bbSMel Gorman 		blockpfn += isolated - 1;
645748446bbSMel Gorman 		cursor += isolated - 1;
6462af120bcSLaura Abbott 		continue;
6472af120bcSLaura Abbott 
6482af120bcSLaura Abbott isolate_fail:
6492af120bcSLaura Abbott 		if (strict)
6502af120bcSLaura Abbott 			break;
6512af120bcSLaura Abbott 		else
6522af120bcSLaura Abbott 			continue;
6532af120bcSLaura Abbott 
654748446bbSMel Gorman 	}
655748446bbSMel Gorman 
656a4f04f2cSDavid Rientjes 	if (locked)
657a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
658a4f04f2cSDavid Rientjes 
6599fcd6d2eSVlastimil Babka 	/*
6609fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
6619fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
6629fcd6d2eSVlastimil Babka 	 */
6639fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
6649fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
6659fcd6d2eSVlastimil Babka 
666e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
667e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
668e34d85f0SJoonsoo Kim 
669e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
670e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
671e14c720eSVlastimil Babka 
672f40d1e42SMel Gorman 	/*
673f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
674f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
675f40d1e42SMel Gorman 	 * returned and CMA will fail.
676f40d1e42SMel Gorman 	 */
6772af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
678f40d1e42SMel Gorman 		total_isolated = 0;
679f40d1e42SMel Gorman 
6807f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
681397487dbSMel Gorman 	if (total_isolated)
682010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
683748446bbSMel Gorman 	return total_isolated;
684748446bbSMel Gorman }
685748446bbSMel Gorman 
68685aa125fSMichal Nazarewicz /**
68785aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
688e8b098fcSMike Rapoport  * @cc:        Compaction control structure.
68985aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
69085aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
69185aa125fSMichal Nazarewicz  *
69285aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
69385aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
69485aa125fSMichal Nazarewicz  * undo its actions and return zero.
69585aa125fSMichal Nazarewicz  *
69685aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
69785aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
69885aa125fSMichal Nazarewicz  * a free page).
69985aa125fSMichal Nazarewicz  */
700ff9543fdSMichal Nazarewicz unsigned long
701bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
702bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
70385aa125fSMichal Nazarewicz {
704e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
70585aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
70685aa125fSMichal Nazarewicz 
7077d49d886SVlastimil Babka 	pfn = start_pfn;
70806b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
709e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
710e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
71106b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
7127d49d886SVlastimil Babka 
7137d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
714e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
7157d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
716e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
717e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
7187d49d886SVlastimil Babka 
71985aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
72085aa125fSMichal Nazarewicz 
72158420016SJoonsoo Kim 		/*
72258420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
72358420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
72458420016SJoonsoo Kim 		 * scanning range to right one.
72558420016SJoonsoo Kim 		 */
72658420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
72706b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
72806b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
72958420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
73058420016SJoonsoo Kim 		}
73158420016SJoonsoo Kim 
732e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
733e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
7347d49d886SVlastimil Babka 			break;
7357d49d886SVlastimil Babka 
736e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
7374fca9730SMel Gorman 					block_end_pfn, &freelist, 0, true);
73885aa125fSMichal Nazarewicz 
73985aa125fSMichal Nazarewicz 		/*
74085aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
74185aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
74285aa125fSMichal Nazarewicz 		 * non-free pages).
74385aa125fSMichal Nazarewicz 		 */
74485aa125fSMichal Nazarewicz 		if (!isolated)
74585aa125fSMichal Nazarewicz 			break;
74685aa125fSMichal Nazarewicz 
74785aa125fSMichal Nazarewicz 		/*
74885aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
74985aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
75085aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
75185aa125fSMichal Nazarewicz 		 */
75285aa125fSMichal Nazarewicz 	}
75385aa125fSMichal Nazarewicz 
75466c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
7554469ab98SMel Gorman 	split_map_pages(&freelist);
75685aa125fSMichal Nazarewicz 
75785aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
75885aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
75985aa125fSMichal Nazarewicz 		release_freepages(&freelist);
76085aa125fSMichal Nazarewicz 		return 0;
76185aa125fSMichal Nazarewicz 	}
76285aa125fSMichal Nazarewicz 
76385aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
76485aa125fSMichal Nazarewicz 	return pfn;
76585aa125fSMichal Nazarewicz }
76685aa125fSMichal Nazarewicz 
767748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
7685f438eeeSAndrey Ryabinin static bool too_many_isolated(pg_data_t *pgdat)
769748446bbSMel Gorman {
770bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
771748446bbSMel Gorman 
7725f438eeeSAndrey Ryabinin 	inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
7735f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_INACTIVE_ANON);
7745f438eeeSAndrey Ryabinin 	active = node_page_state(pgdat, NR_ACTIVE_FILE) +
7755f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ACTIVE_ANON);
7765f438eeeSAndrey Ryabinin 	isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
7775f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ISOLATED_ANON);
778748446bbSMel Gorman 
779bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
780748446bbSMel Gorman }
781748446bbSMel Gorman 
7822fe86e00SMichal Nazarewicz /**
783edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
784edc2ca61SVlastimil Babka  *				  a single pageblock
7852fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
786edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
787edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
788edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
7892fe86e00SMichal Nazarewicz  *
7902fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
791edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
792edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
793edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
794edc2ca61SVlastimil Babka  * than end_pfn).
7952fe86e00SMichal Nazarewicz  *
796edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
797edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
798edc2ca61SVlastimil Babka  * is neither read nor updated.
799748446bbSMel Gorman  */
800edc2ca61SVlastimil Babka static unsigned long
801edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
802edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
803748446bbSMel Gorman {
8045f438eeeSAndrey Ryabinin 	pg_data_t *pgdat = cc->zone->zone_pgdat;
805b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
806fa9add64SHugh Dickins 	struct lruvec *lruvec;
807b8b2d825SXiubo Li 	unsigned long flags = 0;
8082a1402aaSMel Gorman 	bool locked = false;
809bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
810e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
811fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
812fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
813e380bebeSMel Gorman 	bool skip_updated = false;
814748446bbSMel Gorman 
815748446bbSMel Gorman 	/*
816748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
817748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
818748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
819748446bbSMel Gorman 	 */
8205f438eeeSAndrey Ryabinin 	while (unlikely(too_many_isolated(pgdat))) {
821f9e35b3bSMel Gorman 		/* async migration should just abort */
822e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
8232fe86e00SMichal Nazarewicz 			return 0;
824f9e35b3bSMel Gorman 
825748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
826748446bbSMel Gorman 
827748446bbSMel Gorman 		if (fatal_signal_pending(current))
8282fe86e00SMichal Nazarewicz 			return 0;
829748446bbSMel Gorman 	}
830748446bbSMel Gorman 
831cf66f070SMel Gorman 	cond_resched();
832aeef4b83SDavid Rientjes 
833fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
834fdd048e1SVlastimil Babka 		skip_on_failure = true;
835fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
836fdd048e1SVlastimil Babka 	}
837fdd048e1SVlastimil Babka 
838748446bbSMel Gorman 	/* Time to isolate some pages for migration */
839748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
84029c0dde8SVlastimil Babka 
841fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
842fdd048e1SVlastimil Babka 			/*
843fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
844fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
845fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
846fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
847fdd048e1SVlastimil Babka 			 */
848fdd048e1SVlastimil Babka 			if (nr_isolated)
849fdd048e1SVlastimil Babka 				break;
850fdd048e1SVlastimil Babka 
851fdd048e1SVlastimil Babka 			/*
852fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
853fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
854fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
855fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
856fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
857fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
858fdd048e1SVlastimil Babka 			 * previous loop iteration.
859fdd048e1SVlastimil Babka 			 */
860fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
861fdd048e1SVlastimil Babka 		}
862fdd048e1SVlastimil Babka 
8638b44d279SVlastimil Babka 		/*
8648b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
865670105a2SMel Gorman 		 * contention, to give chance to IRQs. Abort completely if
866670105a2SMel Gorman 		 * a fatal signal is pending.
8678b44d279SVlastimil Babka 		 */
8688b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
869f4b7e272SAndrey Ryabinin 		    && compact_unlock_should_abort(&pgdat->lru_lock,
870670105a2SMel Gorman 					    flags, &locked, cc)) {
871670105a2SMel Gorman 			low_pfn = 0;
872670105a2SMel Gorman 			goto fatal_pending;
873670105a2SMel Gorman 		}
874b2eef8c0SAndrea Arcangeli 
875748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
876fdd048e1SVlastimil Babka 			goto isolate_fail;
877b7aba698SMel Gorman 		nr_scanned++;
878748446bbSMel Gorman 
879748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
880dc908600SMel Gorman 
881e380bebeSMel Gorman 		/*
882e380bebeSMel Gorman 		 * Check if the pageblock has already been marked skipped.
883e380bebeSMel Gorman 		 * Only the aligned PFN is checked as the caller isolates
884e380bebeSMel Gorman 		 * COMPACT_CLUSTER_MAX at a time so the second call must
885e380bebeSMel Gorman 		 * not falsely conclude that the block should be skipped.
886e380bebeSMel Gorman 		 */
887e380bebeSMel Gorman 		if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
888e380bebeSMel Gorman 			if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
889e380bebeSMel Gorman 				low_pfn = end_pfn;
890e380bebeSMel Gorman 				goto isolate_abort;
891e380bebeSMel Gorman 			}
892bb13ffebSMel Gorman 			valid_page = page;
893e380bebeSMel Gorman 		}
894bb13ffebSMel Gorman 
895c122b208SJoonsoo Kim 		/*
89699c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
89799c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
89899c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
89999c0fd5eSVlastimil Babka 		 * potential isolation targets.
9006c14466cSMel Gorman 		 */
90199c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
90299c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
90399c0fd5eSVlastimil Babka 
90499c0fd5eSVlastimil Babka 			/*
90599c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
90699c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
90799c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
90899c0fd5eSVlastimil Babka 			 */
90999c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
91099c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
911748446bbSMel Gorman 			continue;
91299c0fd5eSVlastimil Babka 		}
913748446bbSMel Gorman 
9149927af74SMel Gorman 		/*
91529c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
9161da2f328SRik van Riel 		 * hugetlbfs are not to be compacted unless we are attempting
9171da2f328SRik van Riel 		 * an allocation much larger than the huge page size (eg CMA).
9181da2f328SRik van Riel 		 * We can potentially save a lot of iterations if we skip them
9191da2f328SRik van Riel 		 * at once. The check is racy, but we can consider only valid
9201da2f328SRik van Riel 		 * values and the only danger is skipping too much.
921bc835011SAndrea Arcangeli 		 */
9221da2f328SRik van Riel 		if (PageCompound(page) && !cc->alloc_contig) {
92321dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
92429c0dde8SVlastimil Babka 
925d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER))
92621dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
927fdd048e1SVlastimil Babka 			goto isolate_fail;
9282a1402aaSMel Gorman 		}
9292a1402aaSMel Gorman 
930bda807d4SMinchan Kim 		/*
931bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
932bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
933bda807d4SMinchan Kim 		 * Skip any other type of page
934bda807d4SMinchan Kim 		 */
935bda807d4SMinchan Kim 		if (!PageLRU(page)) {
936bda807d4SMinchan Kim 			/*
937bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
938bda807d4SMinchan Kim 			 * to verify it under page_lock.
939bda807d4SMinchan Kim 			 */
940bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
941bda807d4SMinchan Kim 					!PageIsolated(page)) {
942bda807d4SMinchan Kim 				if (locked) {
943f4b7e272SAndrey Ryabinin 					spin_unlock_irqrestore(&pgdat->lru_lock,
944bda807d4SMinchan Kim 									flags);
945bda807d4SMinchan Kim 					locked = false;
946bda807d4SMinchan Kim 				}
947bda807d4SMinchan Kim 
9489e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
949bda807d4SMinchan Kim 					goto isolate_success;
950bda807d4SMinchan Kim 			}
951bda807d4SMinchan Kim 
952fdd048e1SVlastimil Babka 			goto isolate_fail;
953bda807d4SMinchan Kim 		}
95429c0dde8SVlastimil Babka 
955119d6d59SDavid Rientjes 		/*
956119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
957119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
958119d6d59SDavid Rientjes 		 * admittedly racy check.
959119d6d59SDavid Rientjes 		 */
960119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
961119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
962fdd048e1SVlastimil Babka 			goto isolate_fail;
963119d6d59SDavid Rientjes 
96473e64c51SMichal Hocko 		/*
96573e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
96673e64c51SMichal Hocko 		 * because those do not depend on fs locks.
96773e64c51SMichal Hocko 		 */
96873e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
96973e64c51SMichal Hocko 			goto isolate_fail;
97073e64c51SMichal Hocko 
97169b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
97269b7189fSVlastimil Babka 		if (!locked) {
973f4b7e272SAndrey Ryabinin 			locked = compact_lock_irqsave(&pgdat->lru_lock,
9748b44d279SVlastimil Babka 								&flags, cc);
975e380bebeSMel Gorman 
976e380bebeSMel Gorman 			/* Try get exclusive access under lock */
977e380bebeSMel Gorman 			if (!skip_updated) {
978e380bebeSMel Gorman 				skip_updated = true;
979e380bebeSMel Gorman 				if (test_and_set_skip(cc, page, low_pfn))
980e380bebeSMel Gorman 					goto isolate_abort;
981e380bebeSMel Gorman 			}
9822a1402aaSMel Gorman 
98329c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
9842a1402aaSMel Gorman 			if (!PageLRU(page))
985fdd048e1SVlastimil Babka 				goto isolate_fail;
98629c0dde8SVlastimil Babka 
98729c0dde8SVlastimil Babka 			/*
98829c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
98929c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
99029c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
99129c0dde8SVlastimil Babka 			 */
9921da2f328SRik van Riel 			if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
993d8c6546bSMatthew Wilcox (Oracle) 				low_pfn += compound_nr(page) - 1;
994fdd048e1SVlastimil Babka 				goto isolate_fail;
995bc835011SAndrea Arcangeli 			}
99669b7189fSVlastimil Babka 		}
997bc835011SAndrea Arcangeli 
998f4b7e272SAndrey Ryabinin 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
999fa9add64SHugh Dickins 
1000748446bbSMel Gorman 		/* Try isolate the page */
1001edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
1002fdd048e1SVlastimil Babka 			goto isolate_fail;
1003748446bbSMel Gorman 
10041da2f328SRik van Riel 		/* The whole page is taken off the LRU; skip the tail pages. */
10051da2f328SRik van Riel 		if (PageCompound(page))
10061da2f328SRik van Riel 			low_pfn += compound_nr(page) - 1;
1007bc835011SAndrea Arcangeli 
1008748446bbSMel Gorman 		/* Successfully isolated */
1009fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
10101da2f328SRik van Riel 		mod_node_page_state(page_pgdat(page),
10119de4f22aSHuang Ying 				NR_ISOLATED_ANON + page_is_file_lru(page),
1012*6c357848SMatthew Wilcox (Oracle) 				thp_nr_pages(page));
1013b6c75016SJoonsoo Kim 
1014b6c75016SJoonsoo Kim isolate_success:
1015fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
1016748446bbSMel Gorman 		cc->nr_migratepages++;
1017b7aba698SMel Gorman 		nr_isolated++;
1018748446bbSMel Gorman 
1019804d3121SMel Gorman 		/*
1020804d3121SMel Gorman 		 * Avoid isolating too much unless this block is being
1021cb2dcaf0SMel Gorman 		 * rescanned (e.g. dirty/writeback pages, parallel allocation)
1022cb2dcaf0SMel Gorman 		 * or a lock is contended. For contention, isolate quickly to
1023cb2dcaf0SMel Gorman 		 * potentially remove one source of contention.
1024804d3121SMel Gorman 		 */
1025cb2dcaf0SMel Gorman 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
1026cb2dcaf0SMel Gorman 		    !cc->rescan && !cc->contended) {
102731b8384aSHillf Danton 			++low_pfn;
1028748446bbSMel Gorman 			break;
1029748446bbSMel Gorman 		}
1030fdd048e1SVlastimil Babka 
1031fdd048e1SVlastimil Babka 		continue;
1032fdd048e1SVlastimil Babka isolate_fail:
1033fdd048e1SVlastimil Babka 		if (!skip_on_failure)
1034fdd048e1SVlastimil Babka 			continue;
1035fdd048e1SVlastimil Babka 
1036fdd048e1SVlastimil Babka 		/*
1037fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
1038fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
1039fdd048e1SVlastimil Babka 		 * page anyway.
1040fdd048e1SVlastimil Babka 		 */
1041fdd048e1SVlastimil Babka 		if (nr_isolated) {
1042fdd048e1SVlastimil Babka 			if (locked) {
1043f4b7e272SAndrey Ryabinin 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1044fdd048e1SVlastimil Babka 				locked = false;
1045fdd048e1SVlastimil Babka 			}
1046fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
1047fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
1048fdd048e1SVlastimil Babka 			nr_isolated = 0;
1049fdd048e1SVlastimil Babka 		}
1050fdd048e1SVlastimil Babka 
1051fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
1052fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
1053fdd048e1SVlastimil Babka 			/*
1054fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
1055fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
1056fdd048e1SVlastimil Babka 			 */
1057fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
1058fdd048e1SVlastimil Babka 		}
105931b8384aSHillf Danton 	}
1060748446bbSMel Gorman 
106199c0fd5eSVlastimil Babka 	/*
106299c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
106399c0fd5eSVlastimil Babka 	 * the range to be scanned.
106499c0fd5eSVlastimil Babka 	 */
106599c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
106699c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
106799c0fd5eSVlastimil Babka 
1068e380bebeSMel Gorman isolate_abort:
1069c67fe375SMel Gorman 	if (locked)
1070f4b7e272SAndrey Ryabinin 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1071748446bbSMel Gorman 
107250b5b094SVlastimil Babka 	/*
1073804d3121SMel Gorman 	 * Updated the cached scanner pfn once the pageblock has been scanned
1074804d3121SMel Gorman 	 * Pages will either be migrated in which case there is no point
1075804d3121SMel Gorman 	 * scanning in the near future or migration failed in which case the
1076804d3121SMel Gorman 	 * failure reason may persist. The block is marked for skipping if
1077804d3121SMel Gorman 	 * there were no pages isolated in the block or if the block is
1078804d3121SMel Gorman 	 * rescanned twice in a row.
107950b5b094SVlastimil Babka 	 */
1080804d3121SMel Gorman 	if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
1081e380bebeSMel Gorman 		if (valid_page && !skip_updated)
1082e380bebeSMel Gorman 			set_pageblock_skip(valid_page);
1083e380bebeSMel Gorman 		update_cached_migrate(cc, low_pfn);
1084e380bebeSMel Gorman 	}
1085bb13ffebSMel Gorman 
1086e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1087e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
1088b7aba698SMel Gorman 
1089670105a2SMel Gorman fatal_pending:
10907f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
1091397487dbSMel Gorman 	if (nr_isolated)
1092010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
1093397487dbSMel Gorman 
10942fe86e00SMichal Nazarewicz 	return low_pfn;
10952fe86e00SMichal Nazarewicz }
10962fe86e00SMichal Nazarewicz 
1097edc2ca61SVlastimil Babka /**
1098edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1099edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
1100edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
1101edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
1102edc2ca61SVlastimil Babka  *
1103edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
1104edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
1105edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
1106edc2ca61SVlastimil Babka  */
1107edc2ca61SVlastimil Babka unsigned long
1108edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1109edc2ca61SVlastimil Babka 							unsigned long end_pfn)
1110edc2ca61SVlastimil Babka {
1111e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
1112edc2ca61SVlastimil Babka 
1113edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
1114edc2ca61SVlastimil Babka 	pfn = start_pfn;
111506b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
1116e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
1117e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
111806b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
1119edc2ca61SVlastimil Babka 
1120edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
1121e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
1122edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
1123edc2ca61SVlastimil Babka 
1124edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
1125edc2ca61SVlastimil Babka 
1126e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
1127e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
1128edc2ca61SVlastimil Babka 			continue;
1129edc2ca61SVlastimil Babka 
1130edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1131edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
1132edc2ca61SVlastimil Babka 
113314af4a5eSHugh Dickins 		if (!pfn)
1134edc2ca61SVlastimil Babka 			break;
11356ea41c0cSJoonsoo Kim 
11366ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
11376ea41c0cSJoonsoo Kim 			break;
1138edc2ca61SVlastimil Babka 	}
1139edc2ca61SVlastimil Babka 
1140edc2ca61SVlastimil Babka 	return pfn;
1141edc2ca61SVlastimil Babka }
1142edc2ca61SVlastimil Babka 
1143ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1144ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1145018e9a49SAndrew Morton 
1146b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1147b682debdSVlastimil Babka 							struct page *page)
1148b682debdSVlastimil Babka {
1149282722b0SVlastimil Babka 	int block_mt;
1150282722b0SVlastimil Babka 
11519bebefd5SMel Gorman 	if (pageblock_skip_persistent(page))
11529bebefd5SMel Gorman 		return false;
11539bebefd5SMel Gorman 
1154282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1155b682debdSVlastimil Babka 		return true;
1156b682debdSVlastimil Babka 
1157282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1158282722b0SVlastimil Babka 
1159282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1160282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1161282722b0SVlastimil Babka 	else
1162282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1163b682debdSVlastimil Babka }
1164b682debdSVlastimil Babka 
1165018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
11669f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
11679f7e3387SVlastimil Babka 							struct page *page)
1168018e9a49SAndrew Morton {
1169018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1170018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1171018e9a49SAndrew Morton 		/*
1172018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1173018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1174018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1175018e9a49SAndrew Morton 		 */
1176018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
1177018e9a49SAndrew Morton 			return false;
1178018e9a49SAndrew Morton 	}
1179018e9a49SAndrew Morton 
11801ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
11811ef36db2SYisheng Xie 		return true;
11821ef36db2SYisheng Xie 
1183018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1184b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1185018e9a49SAndrew Morton 		return true;
1186018e9a49SAndrew Morton 
1187018e9a49SAndrew Morton 	/* Otherwise skip the block */
1188018e9a49SAndrew Morton 	return false;
1189018e9a49SAndrew Morton }
1190018e9a49SAndrew Morton 
119170b44595SMel Gorman static inline unsigned int
119270b44595SMel Gorman freelist_scan_limit(struct compact_control *cc)
119370b44595SMel Gorman {
1194dd7ef7bdSQian Cai 	unsigned short shift = BITS_PER_LONG - 1;
1195dd7ef7bdSQian Cai 
1196dd7ef7bdSQian Cai 	return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
119770b44595SMel Gorman }
119870b44595SMel Gorman 
1199ff9543fdSMichal Nazarewicz /*
1200f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1201f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1202f2849aa0SVlastimil Babka  */
1203f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1204f2849aa0SVlastimil Babka {
1205f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1206f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1207f2849aa0SVlastimil Babka }
1208f2849aa0SVlastimil Babka 
12095a811889SMel Gorman /*
12105a811889SMel Gorman  * Used when scanning for a suitable migration target which scans freelists
12115a811889SMel Gorman  * in reverse. Reorders the list such as the unscanned pages are scanned
12125a811889SMel Gorman  * first on the next iteration of the free scanner
12135a811889SMel Gorman  */
12145a811889SMel Gorman static void
12155a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage)
12165a811889SMel Gorman {
12175a811889SMel Gorman 	LIST_HEAD(sublist);
12185a811889SMel Gorman 
12195a811889SMel Gorman 	if (!list_is_last(freelist, &freepage->lru)) {
12205a811889SMel Gorman 		list_cut_before(&sublist, freelist, &freepage->lru);
12215a811889SMel Gorman 		if (!list_empty(&sublist))
12225a811889SMel Gorman 			list_splice_tail(&sublist, freelist);
12235a811889SMel Gorman 	}
12245a811889SMel Gorman }
12255a811889SMel Gorman 
12265a811889SMel Gorman /*
12275a811889SMel Gorman  * Similar to move_freelist_head except used by the migration scanner
12285a811889SMel Gorman  * when scanning forward. It's possible for these list operations to
12295a811889SMel Gorman  * move against each other if they search the free list exactly in
12305a811889SMel Gorman  * lockstep.
12315a811889SMel Gorman  */
123270b44595SMel Gorman static void
123370b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage)
123470b44595SMel Gorman {
123570b44595SMel Gorman 	LIST_HEAD(sublist);
123670b44595SMel Gorman 
123770b44595SMel Gorman 	if (!list_is_first(freelist, &freepage->lru)) {
123870b44595SMel Gorman 		list_cut_position(&sublist, freelist, &freepage->lru);
123970b44595SMel Gorman 		if (!list_empty(&sublist))
124070b44595SMel Gorman 			list_splice_tail(&sublist, freelist);
124170b44595SMel Gorman 	}
124270b44595SMel Gorman }
124370b44595SMel Gorman 
12445a811889SMel Gorman static void
12455a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
12465a811889SMel Gorman {
12475a811889SMel Gorman 	unsigned long start_pfn, end_pfn;
12485a811889SMel Gorman 	struct page *page = pfn_to_page(pfn);
12495a811889SMel Gorman 
12505a811889SMel Gorman 	/* Do not search around if there are enough pages already */
12515a811889SMel Gorman 	if (cc->nr_freepages >= cc->nr_migratepages)
12525a811889SMel Gorman 		return;
12535a811889SMel Gorman 
12545a811889SMel Gorman 	/* Minimise scanning during async compaction */
12555a811889SMel Gorman 	if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
12565a811889SMel Gorman 		return;
12575a811889SMel Gorman 
12585a811889SMel Gorman 	/* Pageblock boundaries */
12595a811889SMel Gorman 	start_pfn = pageblock_start_pfn(pfn);
126060fce36aSMel Gorman 	end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)) - 1;
12615a811889SMel Gorman 
12625a811889SMel Gorman 	/* Scan before */
12635a811889SMel Gorman 	if (start_pfn != pfn) {
12644fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false);
12655a811889SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages)
12665a811889SMel Gorman 			return;
12675a811889SMel Gorman 	}
12685a811889SMel Gorman 
12695a811889SMel Gorman 	/* Scan after */
12705a811889SMel Gorman 	start_pfn = pfn + nr_isolated;
127160fce36aSMel Gorman 	if (start_pfn < end_pfn)
12724fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
12735a811889SMel Gorman 
12745a811889SMel Gorman 	/* Skip this pageblock in the future as it's full or nearly full */
12755a811889SMel Gorman 	if (cc->nr_freepages < cc->nr_migratepages)
12765a811889SMel Gorman 		set_pageblock_skip(page);
12775a811889SMel Gorman }
12785a811889SMel Gorman 
1279dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */
1280dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order)
1281dbe2d4e4SMel Gorman {
1282dbe2d4e4SMel Gorman 	order--;
1283dbe2d4e4SMel Gorman 	if (order < 0)
1284dbe2d4e4SMel Gorman 		order = cc->order - 1;
1285dbe2d4e4SMel Gorman 
1286dbe2d4e4SMel Gorman 	/* Search wrapped around? */
1287dbe2d4e4SMel Gorman 	if (order == cc->search_order) {
1288dbe2d4e4SMel Gorman 		cc->search_order--;
1289dbe2d4e4SMel Gorman 		if (cc->search_order < 0)
1290dbe2d4e4SMel Gorman 			cc->search_order = cc->order - 1;
1291dbe2d4e4SMel Gorman 		return -1;
1292dbe2d4e4SMel Gorman 	}
1293dbe2d4e4SMel Gorman 
1294dbe2d4e4SMel Gorman 	return order;
1295dbe2d4e4SMel Gorman }
1296dbe2d4e4SMel Gorman 
12975a811889SMel Gorman static unsigned long
12985a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc)
12995a811889SMel Gorman {
13005a811889SMel Gorman 	unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1);
13015a811889SMel Gorman 	unsigned int nr_scanned = 0;
13025a811889SMel Gorman 	unsigned long low_pfn, min_pfn, high_pfn = 0, highest = 0;
13035a811889SMel Gorman 	unsigned long nr_isolated = 0;
13045a811889SMel Gorman 	unsigned long distance;
13055a811889SMel Gorman 	struct page *page = NULL;
13065a811889SMel Gorman 	bool scan_start = false;
13075a811889SMel Gorman 	int order;
13085a811889SMel Gorman 
13095a811889SMel Gorman 	/* Full compaction passes in a negative order */
13105a811889SMel Gorman 	if (cc->order <= 0)
13115a811889SMel Gorman 		return cc->free_pfn;
13125a811889SMel Gorman 
13135a811889SMel Gorman 	/*
13145a811889SMel Gorman 	 * If starting the scan, use a deeper search and use the highest
13155a811889SMel Gorman 	 * PFN found if a suitable one is not found.
13165a811889SMel Gorman 	 */
1317e332f741SMel Gorman 	if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
13185a811889SMel Gorman 		limit = pageblock_nr_pages >> 1;
13195a811889SMel Gorman 		scan_start = true;
13205a811889SMel Gorman 	}
13215a811889SMel Gorman 
13225a811889SMel Gorman 	/*
13235a811889SMel Gorman 	 * Preferred point is in the top quarter of the scan space but take
13245a811889SMel Gorman 	 * a pfn from the top half if the search is problematic.
13255a811889SMel Gorman 	 */
13265a811889SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn);
13275a811889SMel Gorman 	low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
13285a811889SMel Gorman 	min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
13295a811889SMel Gorman 
13305a811889SMel Gorman 	if (WARN_ON_ONCE(min_pfn > low_pfn))
13315a811889SMel Gorman 		low_pfn = min_pfn;
13325a811889SMel Gorman 
1333dbe2d4e4SMel Gorman 	/*
1334dbe2d4e4SMel Gorman 	 * Search starts from the last successful isolation order or the next
1335dbe2d4e4SMel Gorman 	 * order to search after a previous failure
1336dbe2d4e4SMel Gorman 	 */
1337dbe2d4e4SMel Gorman 	cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1338dbe2d4e4SMel Gorman 
1339dbe2d4e4SMel Gorman 	for (order = cc->search_order;
1340dbe2d4e4SMel Gorman 	     !page && order >= 0;
1341dbe2d4e4SMel Gorman 	     order = next_search_order(cc, order)) {
13425a811889SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
13435a811889SMel Gorman 		struct list_head *freelist;
13445a811889SMel Gorman 		struct page *freepage;
13455a811889SMel Gorman 		unsigned long flags;
13465a811889SMel Gorman 		unsigned int order_scanned = 0;
13475a811889SMel Gorman 
13485a811889SMel Gorman 		if (!area->nr_free)
13495a811889SMel Gorman 			continue;
13505a811889SMel Gorman 
13515a811889SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
13525a811889SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
13535a811889SMel Gorman 		list_for_each_entry_reverse(freepage, freelist, lru) {
13545a811889SMel Gorman 			unsigned long pfn;
13555a811889SMel Gorman 
13565a811889SMel Gorman 			order_scanned++;
13575a811889SMel Gorman 			nr_scanned++;
13585a811889SMel Gorman 			pfn = page_to_pfn(freepage);
13595a811889SMel Gorman 
13605a811889SMel Gorman 			if (pfn >= highest)
13615a811889SMel Gorman 				highest = pageblock_start_pfn(pfn);
13625a811889SMel Gorman 
13635a811889SMel Gorman 			if (pfn >= low_pfn) {
13645a811889SMel Gorman 				cc->fast_search_fail = 0;
1365dbe2d4e4SMel Gorman 				cc->search_order = order;
13665a811889SMel Gorman 				page = freepage;
13675a811889SMel Gorman 				break;
13685a811889SMel Gorman 			}
13695a811889SMel Gorman 
13705a811889SMel Gorman 			if (pfn >= min_pfn && pfn > high_pfn) {
13715a811889SMel Gorman 				high_pfn = pfn;
13725a811889SMel Gorman 
13735a811889SMel Gorman 				/* Shorten the scan if a candidate is found */
13745a811889SMel Gorman 				limit >>= 1;
13755a811889SMel Gorman 			}
13765a811889SMel Gorman 
13775a811889SMel Gorman 			if (order_scanned >= limit)
13785a811889SMel Gorman 				break;
13795a811889SMel Gorman 		}
13805a811889SMel Gorman 
13815a811889SMel Gorman 		/* Use a minimum pfn if a preferred one was not found */
13825a811889SMel Gorman 		if (!page && high_pfn) {
13835a811889SMel Gorman 			page = pfn_to_page(high_pfn);
13845a811889SMel Gorman 
13855a811889SMel Gorman 			/* Update freepage for the list reorder below */
13865a811889SMel Gorman 			freepage = page;
13875a811889SMel Gorman 		}
13885a811889SMel Gorman 
13895a811889SMel Gorman 		/* Reorder to so a future search skips recent pages */
13905a811889SMel Gorman 		move_freelist_head(freelist, freepage);
13915a811889SMel Gorman 
13925a811889SMel Gorman 		/* Isolate the page if available */
13935a811889SMel Gorman 		if (page) {
13945a811889SMel Gorman 			if (__isolate_free_page(page, order)) {
13955a811889SMel Gorman 				set_page_private(page, order);
13965a811889SMel Gorman 				nr_isolated = 1 << order;
13975a811889SMel Gorman 				cc->nr_freepages += nr_isolated;
13985a811889SMel Gorman 				list_add_tail(&page->lru, &cc->freepages);
13995a811889SMel Gorman 				count_compact_events(COMPACTISOLATED, nr_isolated);
14005a811889SMel Gorman 			} else {
14015a811889SMel Gorman 				/* If isolation fails, abort the search */
14025b56d996SQian Cai 				order = cc->search_order + 1;
14035a811889SMel Gorman 				page = NULL;
14045a811889SMel Gorman 			}
14055a811889SMel Gorman 		}
14065a811889SMel Gorman 
14075a811889SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
14085a811889SMel Gorman 
14095a811889SMel Gorman 		/*
14105a811889SMel Gorman 		 * Smaller scan on next order so the total scan ig related
14115a811889SMel Gorman 		 * to freelist_scan_limit.
14125a811889SMel Gorman 		 */
14135a811889SMel Gorman 		if (order_scanned >= limit)
14145a811889SMel Gorman 			limit = min(1U, limit >> 1);
14155a811889SMel Gorman 	}
14165a811889SMel Gorman 
14175a811889SMel Gorman 	if (!page) {
14185a811889SMel Gorman 		cc->fast_search_fail++;
14195a811889SMel Gorman 		if (scan_start) {
14205a811889SMel Gorman 			/*
14215a811889SMel Gorman 			 * Use the highest PFN found above min. If one was
1422f3867755SEthon Paul 			 * not found, be pessimistic for direct compaction
14235a811889SMel Gorman 			 * and use the min mark.
14245a811889SMel Gorman 			 */
14255a811889SMel Gorman 			if (highest) {
14265a811889SMel Gorman 				page = pfn_to_page(highest);
14275a811889SMel Gorman 				cc->free_pfn = highest;
14285a811889SMel Gorman 			} else {
1429e577c8b6SSuzuki K Poulose 				if (cc->direct_compaction && pfn_valid(min_pfn)) {
143073a6e474SBaoquan He 					page = pageblock_pfn_to_page(min_pfn,
143173a6e474SBaoquan He 						pageblock_end_pfn(min_pfn),
143273a6e474SBaoquan He 						cc->zone);
14335a811889SMel Gorman 					cc->free_pfn = min_pfn;
14345a811889SMel Gorman 				}
14355a811889SMel Gorman 			}
14365a811889SMel Gorman 		}
14375a811889SMel Gorman 	}
14385a811889SMel Gorman 
1439d097a6f6SMel Gorman 	if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1440d097a6f6SMel Gorman 		highest -= pageblock_nr_pages;
14415a811889SMel Gorman 		cc->zone->compact_cached_free_pfn = highest;
1442d097a6f6SMel Gorman 	}
14435a811889SMel Gorman 
14445a811889SMel Gorman 	cc->total_free_scanned += nr_scanned;
14455a811889SMel Gorman 	if (!page)
14465a811889SMel Gorman 		return cc->free_pfn;
14475a811889SMel Gorman 
14485a811889SMel Gorman 	low_pfn = page_to_pfn(page);
14495a811889SMel Gorman 	fast_isolate_around(cc, low_pfn, nr_isolated);
14505a811889SMel Gorman 	return low_pfn;
14515a811889SMel Gorman }
14525a811889SMel Gorman 
1453f2849aa0SVlastimil Babka /*
1454ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1455ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1456ff9543fdSMichal Nazarewicz  */
1457edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1458ff9543fdSMichal Nazarewicz {
1459edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1460ff9543fdSMichal Nazarewicz 	struct page *page;
1461c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1462e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1463c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1464c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1465ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
14664fca9730SMel Gorman 	unsigned int stride;
14672fe86e00SMichal Nazarewicz 
14685a811889SMel Gorman 	/* Try a small search of the free lists for a candidate */
14695a811889SMel Gorman 	isolate_start_pfn = fast_isolate_freepages(cc);
14705a811889SMel Gorman 	if (cc->nr_freepages)
14715a811889SMel Gorman 		goto splitmap;
14725a811889SMel Gorman 
1473ff9543fdSMichal Nazarewicz 	/*
1474ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
147549e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1476e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1477e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1478c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1479c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1480a1c1dbebSRandy Dunlap 	 * zone which ends in the middle of a pageblock.
148149e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
148249e068f0SVlastimil Babka 	 * is using.
1483ff9543fdSMichal Nazarewicz 	 */
1484e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
14855a811889SMel Gorman 	block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
1486c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1487c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
148806b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
14894fca9730SMel Gorman 	stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
14902fe86e00SMichal Nazarewicz 
1491ff9543fdSMichal Nazarewicz 	/*
1492ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1493ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1494ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1495ff9543fdSMichal Nazarewicz 	 */
1496f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1497c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1498e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1499e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
15004fca9730SMel Gorman 		unsigned long nr_isolated;
15014fca9730SMel Gorman 
1502f6ea3adbSDavid Rientjes 		/*
1503f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1504cb810ad2SMel Gorman 		 * suitable migration targets, so periodically check resched.
1505f6ea3adbSDavid Rientjes 		 */
1506cb810ad2SMel Gorman 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1507cf66f070SMel Gorman 			cond_resched();
1508f6ea3adbSDavid Rientjes 
15097d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
15107d49d886SVlastimil Babka 									zone);
15117d49d886SVlastimil Babka 		if (!page)
1512ff9543fdSMichal Nazarewicz 			continue;
1513ff9543fdSMichal Nazarewicz 
1514ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
15159f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1516ff9543fdSMichal Nazarewicz 			continue;
151768e3e926SLinus Torvalds 
1518bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1519bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1520bb13ffebSMel Gorman 			continue;
1521bb13ffebSMel Gorman 
1522e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
15234fca9730SMel Gorman 		nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
15244fca9730SMel Gorman 					block_end_pfn, freelist, stride, false);
1525ff9543fdSMichal Nazarewicz 
1526d097a6f6SMel Gorman 		/* Update the skip hint if the full pageblock was scanned */
1527d097a6f6SMel Gorman 		if (isolate_start_pfn == block_end_pfn)
1528d097a6f6SMel Gorman 			update_pageblock_skip(cc, page, block_start_pfn);
1529d097a6f6SMel Gorman 
1530cb2dcaf0SMel Gorman 		/* Are enough freepages isolated? */
1531cb2dcaf0SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages) {
1532a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1533a46cbf3bSDavid Rientjes 				/*
1534a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1535a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1536a46cbf3bSDavid Rientjes 				 */
1537f5f61a32SVlastimil Babka 				isolate_start_pfn =
1538e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1539a46cbf3bSDavid Rientjes 			}
1540be976572SVlastimil Babka 			break;
1541a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1542f5f61a32SVlastimil Babka 			/*
1543a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1544a46cbf3bSDavid Rientjes 			 * needlessly.
1545f5f61a32SVlastimil Babka 			 */
1546a46cbf3bSDavid Rientjes 			break;
1547f5f61a32SVlastimil Babka 		}
15484fca9730SMel Gorman 
15494fca9730SMel Gorman 		/* Adjust stride depending on isolation */
15504fca9730SMel Gorman 		if (nr_isolated) {
15514fca9730SMel Gorman 			stride = 1;
15524fca9730SMel Gorman 			continue;
15534fca9730SMel Gorman 		}
15544fca9730SMel Gorman 		stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1555c89511abSMel Gorman 	}
1556ff9543fdSMichal Nazarewicz 
15577ed695e0SVlastimil Babka 	/*
1558f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1559f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1560f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1561f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
15627ed695e0SVlastimil Babka 	 */
1563f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
15645a811889SMel Gorman 
15655a811889SMel Gorman splitmap:
15665a811889SMel Gorman 	/* __isolate_free_page() does not map the pages */
15675a811889SMel Gorman 	split_map_pages(freelist);
1568748446bbSMel Gorman }
1569748446bbSMel Gorman 
1570748446bbSMel Gorman /*
1571748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1572748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1573748446bbSMel Gorman  */
1574748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1575666feb21SMichal Hocko 					unsigned long data)
1576748446bbSMel Gorman {
1577748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1578748446bbSMel Gorman 	struct page *freepage;
1579748446bbSMel Gorman 
1580748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1581edc2ca61SVlastimil Babka 		isolate_freepages(cc);
1582748446bbSMel Gorman 
1583748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1584748446bbSMel Gorman 			return NULL;
1585748446bbSMel Gorman 	}
1586748446bbSMel Gorman 
1587748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1588748446bbSMel Gorman 	list_del(&freepage->lru);
1589748446bbSMel Gorman 	cc->nr_freepages--;
1590748446bbSMel Gorman 
1591748446bbSMel Gorman 	return freepage;
1592748446bbSMel Gorman }
1593748446bbSMel Gorman 
1594748446bbSMel Gorman /*
1595d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1596d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1597d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1598d53aea3dSDavid Rientjes  */
1599d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1600d53aea3dSDavid Rientjes {
1601d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1602d53aea3dSDavid Rientjes 
1603d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1604d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1605d53aea3dSDavid Rientjes }
1606d53aea3dSDavid Rientjes 
1607ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1608ff9543fdSMichal Nazarewicz typedef enum {
1609ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1610ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1611ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1612ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1613ff9543fdSMichal Nazarewicz 
1614ff9543fdSMichal Nazarewicz /*
16155bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
16165bbe3547SEric B Munson  * compactable pages.
16175bbe3547SEric B Munson  */
16186923aa0dSSebastian Andrzej Siewior #ifdef CONFIG_PREEMPT_RT
16196923aa0dSSebastian Andrzej Siewior int sysctl_compact_unevictable_allowed __read_mostly = 0;
16206923aa0dSSebastian Andrzej Siewior #else
16215bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
16226923aa0dSSebastian Andrzej Siewior #endif
16235bbe3547SEric B Munson 
162470b44595SMel Gorman static inline void
162570b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
162670b44595SMel Gorman {
162770b44595SMel Gorman 	if (cc->fast_start_pfn == ULONG_MAX)
162870b44595SMel Gorman 		return;
162970b44595SMel Gorman 
163070b44595SMel Gorman 	if (!cc->fast_start_pfn)
163170b44595SMel Gorman 		cc->fast_start_pfn = pfn;
163270b44595SMel Gorman 
163370b44595SMel Gorman 	cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
163470b44595SMel Gorman }
163570b44595SMel Gorman 
163670b44595SMel Gorman static inline unsigned long
163770b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc)
163870b44595SMel Gorman {
163970b44595SMel Gorman 	if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
164070b44595SMel Gorman 		return cc->migrate_pfn;
164170b44595SMel Gorman 
164270b44595SMel Gorman 	cc->migrate_pfn = cc->fast_start_pfn;
164370b44595SMel Gorman 	cc->fast_start_pfn = ULONG_MAX;
164470b44595SMel Gorman 
164570b44595SMel Gorman 	return cc->migrate_pfn;
164670b44595SMel Gorman }
164770b44595SMel Gorman 
164870b44595SMel Gorman /*
164970b44595SMel Gorman  * Briefly search the free lists for a migration source that already has
165070b44595SMel Gorman  * some free pages to reduce the number of pages that need migration
165170b44595SMel Gorman  * before a pageblock is free.
165270b44595SMel Gorman  */
165370b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc)
165470b44595SMel Gorman {
165570b44595SMel Gorman 	unsigned int limit = freelist_scan_limit(cc);
165670b44595SMel Gorman 	unsigned int nr_scanned = 0;
165770b44595SMel Gorman 	unsigned long distance;
165870b44595SMel Gorman 	unsigned long pfn = cc->migrate_pfn;
165970b44595SMel Gorman 	unsigned long high_pfn;
166070b44595SMel Gorman 	int order;
166170b44595SMel Gorman 
166270b44595SMel Gorman 	/* Skip hints are relied on to avoid repeats on the fast search */
166370b44595SMel Gorman 	if (cc->ignore_skip_hint)
166470b44595SMel Gorman 		return pfn;
166570b44595SMel Gorman 
166670b44595SMel Gorman 	/*
166770b44595SMel Gorman 	 * If the migrate_pfn is not at the start of a zone or the start
166870b44595SMel Gorman 	 * of a pageblock then assume this is a continuation of a previous
166970b44595SMel Gorman 	 * scan restarted due to COMPACT_CLUSTER_MAX.
167070b44595SMel Gorman 	 */
167170b44595SMel Gorman 	if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
167270b44595SMel Gorman 		return pfn;
167370b44595SMel Gorman 
167470b44595SMel Gorman 	/*
167570b44595SMel Gorman 	 * For smaller orders, just linearly scan as the number of pages
167670b44595SMel Gorman 	 * to migrate should be relatively small and does not necessarily
167770b44595SMel Gorman 	 * justify freeing up a large block for a small allocation.
167870b44595SMel Gorman 	 */
167970b44595SMel Gorman 	if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
168070b44595SMel Gorman 		return pfn;
168170b44595SMel Gorman 
168270b44595SMel Gorman 	/*
168370b44595SMel Gorman 	 * Only allow kcompactd and direct requests for movable pages to
168470b44595SMel Gorman 	 * quickly clear out a MOVABLE pageblock for allocation. This
168570b44595SMel Gorman 	 * reduces the risk that a large movable pageblock is freed for
168670b44595SMel Gorman 	 * an unmovable/reclaimable small allocation.
168770b44595SMel Gorman 	 */
168870b44595SMel Gorman 	if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
168970b44595SMel Gorman 		return pfn;
169070b44595SMel Gorman 
169170b44595SMel Gorman 	/*
169270b44595SMel Gorman 	 * When starting the migration scanner, pick any pageblock within the
169370b44595SMel Gorman 	 * first half of the search space. Otherwise try and pick a pageblock
169470b44595SMel Gorman 	 * within the first eighth to reduce the chances that a migration
169570b44595SMel Gorman 	 * target later becomes a source.
169670b44595SMel Gorman 	 */
169770b44595SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
169870b44595SMel Gorman 	if (cc->migrate_pfn != cc->zone->zone_start_pfn)
169970b44595SMel Gorman 		distance >>= 2;
170070b44595SMel Gorman 	high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
170170b44595SMel Gorman 
170270b44595SMel Gorman 	for (order = cc->order - 1;
170370b44595SMel Gorman 	     order >= PAGE_ALLOC_COSTLY_ORDER && pfn == cc->migrate_pfn && nr_scanned < limit;
170470b44595SMel Gorman 	     order--) {
170570b44595SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
170670b44595SMel Gorman 		struct list_head *freelist;
170770b44595SMel Gorman 		unsigned long flags;
170870b44595SMel Gorman 		struct page *freepage;
170970b44595SMel Gorman 
171070b44595SMel Gorman 		if (!area->nr_free)
171170b44595SMel Gorman 			continue;
171270b44595SMel Gorman 
171370b44595SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
171470b44595SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
171570b44595SMel Gorman 		list_for_each_entry(freepage, freelist, lru) {
171670b44595SMel Gorman 			unsigned long free_pfn;
171770b44595SMel Gorman 
171870b44595SMel Gorman 			nr_scanned++;
171970b44595SMel Gorman 			free_pfn = page_to_pfn(freepage);
172070b44595SMel Gorman 			if (free_pfn < high_pfn) {
172170b44595SMel Gorman 				/*
172270b44595SMel Gorman 				 * Avoid if skipped recently. Ideally it would
172370b44595SMel Gorman 				 * move to the tail but even safe iteration of
172470b44595SMel Gorman 				 * the list assumes an entry is deleted, not
172570b44595SMel Gorman 				 * reordered.
172670b44595SMel Gorman 				 */
172770b44595SMel Gorman 				if (get_pageblock_skip(freepage)) {
172870b44595SMel Gorman 					if (list_is_last(freelist, &freepage->lru))
172970b44595SMel Gorman 						break;
173070b44595SMel Gorman 
173170b44595SMel Gorman 					continue;
173270b44595SMel Gorman 				}
173370b44595SMel Gorman 
173470b44595SMel Gorman 				/* Reorder to so a future search skips recent pages */
173570b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
173670b44595SMel Gorman 
1737e380bebeSMel Gorman 				update_fast_start_pfn(cc, free_pfn);
173870b44595SMel Gorman 				pfn = pageblock_start_pfn(free_pfn);
173970b44595SMel Gorman 				cc->fast_search_fail = 0;
174070b44595SMel Gorman 				set_pageblock_skip(freepage);
174170b44595SMel Gorman 				break;
174270b44595SMel Gorman 			}
174370b44595SMel Gorman 
174470b44595SMel Gorman 			if (nr_scanned >= limit) {
174570b44595SMel Gorman 				cc->fast_search_fail++;
174670b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
174770b44595SMel Gorman 				break;
174870b44595SMel Gorman 			}
174970b44595SMel Gorman 		}
175070b44595SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
175170b44595SMel Gorman 	}
175270b44595SMel Gorman 
175370b44595SMel Gorman 	cc->total_migrate_scanned += nr_scanned;
175470b44595SMel Gorman 
175570b44595SMel Gorman 	/*
175670b44595SMel Gorman 	 * If fast scanning failed then use a cached entry for a page block
175770b44595SMel Gorman 	 * that had free pages as the basis for starting a linear scan.
175870b44595SMel Gorman 	 */
175970b44595SMel Gorman 	if (pfn == cc->migrate_pfn)
176070b44595SMel Gorman 		pfn = reinit_migrate_pfn(cc);
176170b44595SMel Gorman 
176270b44595SMel Gorman 	return pfn;
176370b44595SMel Gorman }
176470b44595SMel Gorman 
17655bbe3547SEric B Munson /*
1766edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1767edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1768edc2ca61SVlastimil Babka  * compact_control.
1769ff9543fdSMichal Nazarewicz  */
177032aaf055SPengfei Li static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
1771ff9543fdSMichal Nazarewicz {
1772e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1773e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1774e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1775edc2ca61SVlastimil Babka 	struct page *page;
1776edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
17775bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
17781d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
177970b44595SMel Gorman 	bool fast_find_block;
1780ff9543fdSMichal Nazarewicz 
1781edc2ca61SVlastimil Babka 	/*
1782edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
178370b44595SMel Gorman 	 * initialized by compact_zone(). The first failure will use
178470b44595SMel Gorman 	 * the lowest PFN as the starting point for linear scanning.
1785edc2ca61SVlastimil Babka 	 */
178670b44595SMel Gorman 	low_pfn = fast_find_migrateblock(cc);
178706b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
178832aaf055SPengfei Li 	if (block_start_pfn < cc->zone->zone_start_pfn)
178932aaf055SPengfei Li 		block_start_pfn = cc->zone->zone_start_pfn;
1790ff9543fdSMichal Nazarewicz 
179170b44595SMel Gorman 	/*
179270b44595SMel Gorman 	 * fast_find_migrateblock marks a pageblock skipped so to avoid
179370b44595SMel Gorman 	 * the isolation_suitable check below, check whether the fast
179470b44595SMel Gorman 	 * search was successful.
179570b44595SMel Gorman 	 */
179670b44595SMel Gorman 	fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
179770b44595SMel Gorman 
1798ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
179906b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1800ff9543fdSMichal Nazarewicz 
1801edc2ca61SVlastimil Babka 	/*
1802edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1803edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1804edc2ca61SVlastimil Babka 	 */
1805e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
180670b44595SMel Gorman 			fast_find_block = false,
1807e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1808e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1809e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1810edc2ca61SVlastimil Babka 
1811edc2ca61SVlastimil Babka 		/*
1812edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1813edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1814cb810ad2SMel Gorman 		 * need to schedule.
1815edc2ca61SVlastimil Babka 		 */
1816cb810ad2SMel Gorman 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1817cf66f070SMel Gorman 			cond_resched();
1818edc2ca61SVlastimil Babka 
181932aaf055SPengfei Li 		page = pageblock_pfn_to_page(block_start_pfn,
182032aaf055SPengfei Li 						block_end_pfn, cc->zone);
18217d49d886SVlastimil Babka 		if (!page)
1822edc2ca61SVlastimil Babka 			continue;
1823edc2ca61SVlastimil Babka 
1824e380bebeSMel Gorman 		/*
1825e380bebeSMel Gorman 		 * If isolation recently failed, do not retry. Only check the
1826e380bebeSMel Gorman 		 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1827e380bebeSMel Gorman 		 * to be visited multiple times. Assume skip was checked
1828e380bebeSMel Gorman 		 * before making it "skip" so other compaction instances do
1829e380bebeSMel Gorman 		 * not scan the same block.
1830e380bebeSMel Gorman 		 */
1831e380bebeSMel Gorman 		if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1832e380bebeSMel Gorman 		    !fast_find_block && !isolation_suitable(cc, page))
1833edc2ca61SVlastimil Babka 			continue;
1834edc2ca61SVlastimil Babka 
1835edc2ca61SVlastimil Babka 		/*
18369bebefd5SMel Gorman 		 * For async compaction, also only scan in MOVABLE blocks
18379bebefd5SMel Gorman 		 * without huge pages. Async compaction is optimistic to see
18389bebefd5SMel Gorman 		 * if the minimum amount of work satisfies the allocation.
18399bebefd5SMel Gorman 		 * The cached PFN is updated as it's possible that all
18409bebefd5SMel Gorman 		 * remaining blocks between source and target are unsuitable
18419bebefd5SMel Gorman 		 * and the compaction scanners fail to meet.
1842edc2ca61SVlastimil Babka 		 */
18439bebefd5SMel Gorman 		if (!suitable_migration_source(cc, page)) {
18449bebefd5SMel Gorman 			update_cached_migrate(cc, block_end_pfn);
1845edc2ca61SVlastimil Babka 			continue;
18469bebefd5SMel Gorman 		}
1847ff9543fdSMichal Nazarewicz 
1848ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1849e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1850e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1851edc2ca61SVlastimil Babka 
1852cb2dcaf0SMel Gorman 		if (!low_pfn)
1853ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1854ff9543fdSMichal Nazarewicz 
1855edc2ca61SVlastimil Babka 		/*
1856edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1857edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1858edc2ca61SVlastimil Babka 		 * continue or not.
1859edc2ca61SVlastimil Babka 		 */
1860edc2ca61SVlastimil Babka 		break;
1861edc2ca61SVlastimil Babka 	}
1862edc2ca61SVlastimil Babka 
1863f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1864f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1865ff9543fdSMichal Nazarewicz 
1866edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1867ff9543fdSMichal Nazarewicz }
1868ff9543fdSMichal Nazarewicz 
186921c527a3SYaowei Bai /*
187021c527a3SYaowei Bai  * order == -1 is expected when compacting via
187121c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
187221c527a3SYaowei Bai  */
187321c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
187421c527a3SYaowei Bai {
187521c527a3SYaowei Bai 	return order == -1;
187621c527a3SYaowei Bai }
187721c527a3SYaowei Bai 
1878facdaa91SNitin Gupta static bool kswapd_is_running(pg_data_t *pgdat)
1879facdaa91SNitin Gupta {
1880facdaa91SNitin Gupta 	return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING);
1881facdaa91SNitin Gupta }
1882facdaa91SNitin Gupta 
1883facdaa91SNitin Gupta /*
1884facdaa91SNitin Gupta  * A zone's fragmentation score is the external fragmentation wrt to the
1885facdaa91SNitin Gupta  * COMPACTION_HPAGE_ORDER scaled by the zone's size. It returns a value
1886facdaa91SNitin Gupta  * in the range [0, 100].
1887facdaa91SNitin Gupta  *
1888facdaa91SNitin Gupta  * The scaling factor ensures that proactive compaction focuses on larger
1889facdaa91SNitin Gupta  * zones like ZONE_NORMAL, rather than smaller, specialized zones like
1890facdaa91SNitin Gupta  * ZONE_DMA32. For smaller zones, the score value remains close to zero,
1891facdaa91SNitin Gupta  * and thus never exceeds the high threshold for proactive compaction.
1892facdaa91SNitin Gupta  */
1893d34c0a75SNitin Gupta static unsigned int fragmentation_score_zone(struct zone *zone)
1894facdaa91SNitin Gupta {
1895facdaa91SNitin Gupta 	unsigned long score;
1896facdaa91SNitin Gupta 
1897facdaa91SNitin Gupta 	score = zone->present_pages *
1898facdaa91SNitin Gupta 			extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
1899facdaa91SNitin Gupta 	return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
1900facdaa91SNitin Gupta }
1901facdaa91SNitin Gupta 
1902facdaa91SNitin Gupta /*
1903facdaa91SNitin Gupta  * The per-node proactive (background) compaction process is started by its
1904facdaa91SNitin Gupta  * corresponding kcompactd thread when the node's fragmentation score
1905facdaa91SNitin Gupta  * exceeds the high threshold. The compaction process remains active till
1906facdaa91SNitin Gupta  * the node's score falls below the low threshold, or one of the back-off
1907facdaa91SNitin Gupta  * conditions is met.
1908facdaa91SNitin Gupta  */
1909d34c0a75SNitin Gupta static unsigned int fragmentation_score_node(pg_data_t *pgdat)
1910facdaa91SNitin Gupta {
1911d34c0a75SNitin Gupta 	unsigned int score = 0;
1912facdaa91SNitin Gupta 	int zoneid;
1913facdaa91SNitin Gupta 
1914facdaa91SNitin Gupta 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1915facdaa91SNitin Gupta 		struct zone *zone;
1916facdaa91SNitin Gupta 
1917facdaa91SNitin Gupta 		zone = &pgdat->node_zones[zoneid];
1918facdaa91SNitin Gupta 		score += fragmentation_score_zone(zone);
1919facdaa91SNitin Gupta 	}
1920facdaa91SNitin Gupta 
1921facdaa91SNitin Gupta 	return score;
1922facdaa91SNitin Gupta }
1923facdaa91SNitin Gupta 
1924d34c0a75SNitin Gupta static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
1925facdaa91SNitin Gupta {
1926d34c0a75SNitin Gupta 	unsigned int wmark_low;
1927facdaa91SNitin Gupta 
1928facdaa91SNitin Gupta 	/*
1929facdaa91SNitin Gupta 	 * Cap the low watermak to avoid excessive compaction
1930facdaa91SNitin Gupta 	 * activity in case a user sets the proactivess tunable
1931facdaa91SNitin Gupta 	 * close to 100 (maximum).
1932facdaa91SNitin Gupta 	 */
1933d34c0a75SNitin Gupta 	wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
1934d34c0a75SNitin Gupta 	return low ? wmark_low : min(wmark_low + 10, 100U);
1935facdaa91SNitin Gupta }
1936facdaa91SNitin Gupta 
1937facdaa91SNitin Gupta static bool should_proactive_compact_node(pg_data_t *pgdat)
1938facdaa91SNitin Gupta {
1939facdaa91SNitin Gupta 	int wmark_high;
1940facdaa91SNitin Gupta 
1941facdaa91SNitin Gupta 	if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
1942facdaa91SNitin Gupta 		return false;
1943facdaa91SNitin Gupta 
1944facdaa91SNitin Gupta 	wmark_high = fragmentation_score_wmark(pgdat, false);
1945facdaa91SNitin Gupta 	return fragmentation_score_node(pgdat) > wmark_high;
1946facdaa91SNitin Gupta }
1947facdaa91SNitin Gupta 
194840cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc)
1949748446bbSMel Gorman {
19508fb74b9fSMel Gorman 	unsigned int order;
1951d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
1952cb2dcaf0SMel Gorman 	int ret;
1953748446bbSMel Gorman 
1954753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1955f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
195655b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
195740cacbcbSMel Gorman 		reset_cached_positions(cc->zone);
195855b7c4c9SVlastimil Babka 
195962997027SMel Gorman 		/*
196062997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1961accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
196262997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
196362997027SMel Gorman 		 * based on an allocation request.
196462997027SMel Gorman 		 */
1965accf6242SVlastimil Babka 		if (cc->direct_compaction)
196640cacbcbSMel Gorman 			cc->zone->compact_blockskip_flush = true;
196762997027SMel Gorman 
1968c8f7de0bSMichal Hocko 		if (cc->whole_zone)
1969748446bbSMel Gorman 			return COMPACT_COMPLETE;
1970c8f7de0bSMichal Hocko 		else
1971c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
1972bb13ffebSMel Gorman 	}
1973748446bbSMel Gorman 
1974facdaa91SNitin Gupta 	if (cc->proactive_compaction) {
1975facdaa91SNitin Gupta 		int score, wmark_low;
1976facdaa91SNitin Gupta 		pg_data_t *pgdat;
1977facdaa91SNitin Gupta 
1978facdaa91SNitin Gupta 		pgdat = cc->zone->zone_pgdat;
1979facdaa91SNitin Gupta 		if (kswapd_is_running(pgdat))
1980facdaa91SNitin Gupta 			return COMPACT_PARTIAL_SKIPPED;
1981facdaa91SNitin Gupta 
1982facdaa91SNitin Gupta 		score = fragmentation_score_zone(cc->zone);
1983facdaa91SNitin Gupta 		wmark_low = fragmentation_score_wmark(pgdat, true);
1984facdaa91SNitin Gupta 
1985facdaa91SNitin Gupta 		if (score > wmark_low)
1986facdaa91SNitin Gupta 			ret = COMPACT_CONTINUE;
1987facdaa91SNitin Gupta 		else
1988facdaa91SNitin Gupta 			ret = COMPACT_SUCCESS;
1989facdaa91SNitin Gupta 
1990facdaa91SNitin Gupta 		goto out;
1991facdaa91SNitin Gupta 	}
1992facdaa91SNitin Gupta 
199321c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
199456de7263SMel Gorman 		return COMPACT_CONTINUE;
199556de7263SMel Gorman 
1996baf6a9a1SVlastimil Babka 	/*
1997efe771c7SMel Gorman 	 * Always finish scanning a pageblock to reduce the possibility of
1998efe771c7SMel Gorman 	 * fallbacks in the future. This is particularly important when
1999efe771c7SMel Gorman 	 * migration source is unmovable/reclaimable but it's not worth
2000efe771c7SMel Gorman 	 * special casing.
2001baf6a9a1SVlastimil Babka 	 */
2002efe771c7SMel Gorman 	if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2003baf6a9a1SVlastimil Babka 		return COMPACT_CONTINUE;
2004baf6a9a1SVlastimil Babka 
200556de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
2006cb2dcaf0SMel Gorman 	ret = COMPACT_NO_SUITABLE_PAGE;
200756de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
200840cacbcbSMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
20092149cdaeSJoonsoo Kim 		bool can_steal;
20108fb74b9fSMel Gorman 
201156de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
2012b03641afSDan Williams 		if (!free_area_empty(area, migratetype))
2013cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
201456de7263SMel Gorman 
20152149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
20162149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
20172149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
2018b03641afSDan Williams 			!free_area_empty(area, MIGRATE_CMA))
2019cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
20202149cdaeSJoonsoo Kim #endif
20212149cdaeSJoonsoo Kim 		/*
20222149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
20232149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
20242149cdaeSJoonsoo Kim 		 */
20252149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
2026baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
2027baf6a9a1SVlastimil Babka 
2028baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
2029baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
2030cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
2031baf6a9a1SVlastimil Babka 
2032baf6a9a1SVlastimil Babka 			/*
2033baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
2034baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
2035baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
2036baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
2037baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
2038baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
2039baf6a9a1SVlastimil Babka 			 */
2040baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
2041baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
2042baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
2043baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
2044baf6a9a1SVlastimil Babka 			}
2045baf6a9a1SVlastimil Babka 
2046cb2dcaf0SMel Gorman 			ret = COMPACT_CONTINUE;
2047cb2dcaf0SMel Gorman 			break;
2048baf6a9a1SVlastimil Babka 		}
204956de7263SMel Gorman 	}
205056de7263SMel Gorman 
2051facdaa91SNitin Gupta out:
2052cb2dcaf0SMel Gorman 	if (cc->contended || fatal_signal_pending(current))
2053cb2dcaf0SMel Gorman 		ret = COMPACT_CONTENDED;
2054cb2dcaf0SMel Gorman 
2055cb2dcaf0SMel Gorman 	return ret;
2056837d026dSJoonsoo Kim }
2057837d026dSJoonsoo Kim 
205840cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc)
2059837d026dSJoonsoo Kim {
2060837d026dSJoonsoo Kim 	int ret;
2061837d026dSJoonsoo Kim 
206240cacbcbSMel Gorman 	ret = __compact_finished(cc);
206340cacbcbSMel Gorman 	trace_mm_compaction_finished(cc->zone, cc->order, ret);
2064837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
2065837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
2066837d026dSJoonsoo Kim 
2067837d026dSJoonsoo Kim 	return ret;
2068748446bbSMel Gorman }
2069748446bbSMel Gorman 
20703e7d3449SMel Gorman /*
20713e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
20723e7d3449SMel Gorman  * Returns
20733e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
2074cf378319SVlastimil Babka  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
20753e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
20763e7d3449SMel Gorman  */
2077ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
2078c603844bSMel Gorman 					unsigned int alloc_flags,
207997a225e6SJoonsoo Kim 					int highest_zoneidx,
208086a294a8SMichal Hocko 					unsigned long wmark_target)
20813e7d3449SMel Gorman {
20823e7d3449SMel Gorman 	unsigned long watermark;
20833e7d3449SMel Gorman 
208421c527a3SYaowei Bai 	if (is_via_compact_memory(order))
20853957c776SMichal Hocko 		return COMPACT_CONTINUE;
20863957c776SMichal Hocko 
2087a9214443SMel Gorman 	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
2088ebff3980SVlastimil Babka 	/*
2089ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
2090ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
2091ebff3980SVlastimil Babka 	 */
209297a225e6SJoonsoo Kim 	if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
2093ebff3980SVlastimil Babka 								alloc_flags))
2094cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
2095ebff3980SVlastimil Babka 
20963957c776SMichal Hocko 	/*
20979861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
2098984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
2099984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
2100984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
2101984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
210297a225e6SJoonsoo Kim 	 * isolation. We however do use the direct compactor's highest_zoneidx
210397a225e6SJoonsoo Kim 	 * to skip over zones where lowmem reserves would prevent allocation
210497a225e6SJoonsoo Kim 	 * even if compaction succeeds.
21058348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
21068348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
2107d883c6cfSJoonsoo Kim 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2108d883c6cfSJoonsoo Kim 	 * suitable migration targets
21093e7d3449SMel Gorman 	 */
21108348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
21118348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
21128348faf9SVlastimil Babka 	watermark += compact_gap(order);
211397a225e6SJoonsoo Kim 	if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
2114d883c6cfSJoonsoo Kim 						ALLOC_CMA, wmark_target))
21153e7d3449SMel Gorman 		return COMPACT_SKIPPED;
21163e7d3449SMel Gorman 
2117cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
2118cc5c9f09SVlastimil Babka }
2119cc5c9f09SVlastimil Babka 
2120cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
2121cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
212297a225e6SJoonsoo Kim 					int highest_zoneidx)
2123cc5c9f09SVlastimil Babka {
2124cc5c9f09SVlastimil Babka 	enum compact_result ret;
2125cc5c9f09SVlastimil Babka 	int fragindex;
2126cc5c9f09SVlastimil Babka 
212797a225e6SJoonsoo Kim 	ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
2128cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
21293e7d3449SMel Gorman 	/*
21303e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
21313e7d3449SMel Gorman 	 * low memory or external fragmentation
21323e7d3449SMel Gorman 	 *
2133ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
2134ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
21353e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
21363e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
21373e7d3449SMel Gorman 	 *
213820311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
213920311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
214020311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
214120311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
214220311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
214320311420SVlastimil Babka 	 * expense of system stability.
21443e7d3449SMel Gorman 	 */
214520311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
21463e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
21473e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2148cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
21493e7d3449SMel Gorman 	}
21503e7d3449SMel Gorman 
2151837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
2152837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
2153837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
2154837d026dSJoonsoo Kim 
2155837d026dSJoonsoo Kim 	return ret;
2156837d026dSJoonsoo Kim }
2157837d026dSJoonsoo Kim 
215886a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
215986a294a8SMichal Hocko 		int alloc_flags)
216086a294a8SMichal Hocko {
216186a294a8SMichal Hocko 	struct zone *zone;
216286a294a8SMichal Hocko 	struct zoneref *z;
216386a294a8SMichal Hocko 
216486a294a8SMichal Hocko 	/*
216586a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
216686a294a8SMichal Hocko 	 * retrying the reclaim.
216786a294a8SMichal Hocko 	 */
216897a225e6SJoonsoo Kim 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
216997a225e6SJoonsoo Kim 				ac->highest_zoneidx, ac->nodemask) {
217086a294a8SMichal Hocko 		unsigned long available;
217186a294a8SMichal Hocko 		enum compact_result compact_result;
217286a294a8SMichal Hocko 
217386a294a8SMichal Hocko 		/*
217486a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
217586a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
217686a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
217786a294a8SMichal Hocko 		 * is happy about the watermark check.
217886a294a8SMichal Hocko 		 */
21795a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
218086a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
218186a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
218297a225e6SJoonsoo Kim 				ac->highest_zoneidx, available);
2183cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
218486a294a8SMichal Hocko 			return true;
218586a294a8SMichal Hocko 	}
218686a294a8SMichal Hocko 
218786a294a8SMichal Hocko 	return false;
218886a294a8SMichal Hocko }
218986a294a8SMichal Hocko 
21905e1f0f09SMel Gorman static enum compact_result
21915e1f0f09SMel Gorman compact_zone(struct compact_control *cc, struct capture_control *capc)
2192748446bbSMel Gorman {
2193ea7ab982SMichal Hocko 	enum compact_result ret;
219440cacbcbSMel Gorman 	unsigned long start_pfn = cc->zone->zone_start_pfn;
219540cacbcbSMel Gorman 	unsigned long end_pfn = zone_end_pfn(cc->zone);
2196566e54e1SMel Gorman 	unsigned long last_migrated_pfn;
2197e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
21988854c55fSMel Gorman 	bool update_cached;
2199748446bbSMel Gorman 
2200a94b5252SYafang Shao 	/*
2201a94b5252SYafang Shao 	 * These counters track activities during zone compaction.  Initialize
2202a94b5252SYafang Shao 	 * them before compacting a new zone.
2203a94b5252SYafang Shao 	 */
2204a94b5252SYafang Shao 	cc->total_migrate_scanned = 0;
2205a94b5252SYafang Shao 	cc->total_free_scanned = 0;
2206a94b5252SYafang Shao 	cc->nr_migratepages = 0;
2207a94b5252SYafang Shao 	cc->nr_freepages = 0;
2208a94b5252SYafang Shao 	INIT_LIST_HEAD(&cc->freepages);
2209a94b5252SYafang Shao 	INIT_LIST_HEAD(&cc->migratepages);
2210a94b5252SYafang Shao 
221101c0bfe0SWei Yang 	cc->migratetype = gfp_migratetype(cc->gfp_mask);
221240cacbcbSMel Gorman 	ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
221397a225e6SJoonsoo Kim 							cc->highest_zoneidx);
22143e7d3449SMel Gorman 	/* Compaction is likely to fail */
2215cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
22163e7d3449SMel Gorman 		return ret;
2217c46649deSMichal Hocko 
2218c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
2219c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
22203e7d3449SMel Gorman 
2221c89511abSMel Gorman 	/*
2222d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
2223accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
2224d3132e4bSVlastimil Babka 	 */
222540cacbcbSMel Gorman 	if (compaction_restarting(cc->zone, cc->order))
222640cacbcbSMel Gorman 		__reset_isolation_suitable(cc->zone);
2227d3132e4bSVlastimil Babka 
2228d3132e4bSVlastimil Babka 	/*
2229c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
223006ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
223106ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
223206ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
2233c89511abSMel Gorman 	 */
223470b44595SMel Gorman 	cc->fast_start_pfn = 0;
223506ed2998SVlastimil Babka 	if (cc->whole_zone) {
223606ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
223706ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
223806ed2998SVlastimil Babka 	} else {
223940cacbcbSMel Gorman 		cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
224040cacbcbSMel Gorman 		cc->free_pfn = cc->zone->compact_cached_free_pfn;
2241623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
224206b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
224340cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = cc->free_pfn;
2244c89511abSMel Gorman 		}
2245623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2246c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
224740cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
224840cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2249c89511abSMel Gorman 		}
2250c8f7de0bSMichal Hocko 
2251e332f741SMel Gorman 		if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
2252c8f7de0bSMichal Hocko 			cc->whole_zone = true;
225306ed2998SVlastimil Babka 	}
2254c8f7de0bSMichal Hocko 
2255566e54e1SMel Gorman 	last_migrated_pfn = 0;
2256748446bbSMel Gorman 
22578854c55fSMel Gorman 	/*
22588854c55fSMel Gorman 	 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
22598854c55fSMel Gorman 	 * the basis that some migrations will fail in ASYNC mode. However,
22608854c55fSMel Gorman 	 * if the cached PFNs match and pageblocks are skipped due to having
22618854c55fSMel Gorman 	 * no isolation candidates, then the sync state does not matter.
22628854c55fSMel Gorman 	 * Until a pageblock with isolation candidates is found, keep the
22638854c55fSMel Gorman 	 * cached PFNs in sync to avoid revisiting the same blocks.
22648854c55fSMel Gorman 	 */
22658854c55fSMel Gorman 	update_cached = !sync &&
22668854c55fSMel Gorman 		cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
22678854c55fSMel Gorman 
226816c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
226916c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
22700eb927c0SMel Gorman 
2271748446bbSMel Gorman 	migrate_prep_local();
2272748446bbSMel Gorman 
227340cacbcbSMel Gorman 	while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
22749d502c1cSMinchan Kim 		int err;
2275566e54e1SMel Gorman 		unsigned long start_pfn = cc->migrate_pfn;
2276748446bbSMel Gorman 
2277804d3121SMel Gorman 		/*
2278804d3121SMel Gorman 		 * Avoid multiple rescans which can happen if a page cannot be
2279804d3121SMel Gorman 		 * isolated (dirty/writeback in async mode) or if the migrated
2280804d3121SMel Gorman 		 * pages are being allocated before the pageblock is cleared.
2281804d3121SMel Gorman 		 * The first rescan will capture the entire pageblock for
2282804d3121SMel Gorman 		 * migration. If it fails, it'll be marked skip and scanning
2283804d3121SMel Gorman 		 * will proceed as normal.
2284804d3121SMel Gorman 		 */
2285804d3121SMel Gorman 		cc->rescan = false;
2286804d3121SMel Gorman 		if (pageblock_start_pfn(last_migrated_pfn) ==
2287804d3121SMel Gorman 		    pageblock_start_pfn(start_pfn)) {
2288804d3121SMel Gorman 			cc->rescan = true;
2289804d3121SMel Gorman 		}
2290804d3121SMel Gorman 
229132aaf055SPengfei Li 		switch (isolate_migratepages(cc)) {
2292f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
22932d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
22945733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
2295e64c5237SShaohua Li 			cc->nr_migratepages = 0;
2296f9e35b3bSMel Gorman 			goto out;
2297f9e35b3bSMel Gorman 		case ISOLATE_NONE:
22988854c55fSMel Gorman 			if (update_cached) {
22998854c55fSMel Gorman 				cc->zone->compact_cached_migrate_pfn[1] =
23008854c55fSMel Gorman 					cc->zone->compact_cached_migrate_pfn[0];
23018854c55fSMel Gorman 			}
23028854c55fSMel Gorman 
2303fdaf7f5cSVlastimil Babka 			/*
2304fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
2305fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
2306fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
2307fdaf7f5cSVlastimil Babka 			 */
2308fdaf7f5cSVlastimil Babka 			goto check_drain;
2309f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
23108854c55fSMel Gorman 			update_cached = false;
2311566e54e1SMel Gorman 			last_migrated_pfn = start_pfn;
2312f9e35b3bSMel Gorman 			;
2313f9e35b3bSMel Gorman 		}
2314748446bbSMel Gorman 
2315d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
2316e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
23177b2a2d4aSMel Gorman 				MR_COMPACTION);
2318748446bbSMel Gorman 
2319f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2320f8c9301fSVlastimil Babka 							&cc->migratepages);
2321748446bbSMel Gorman 
2322f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
2323f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
23249d502c1cSMinchan Kim 		if (err) {
23255733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
23267ed695e0SVlastimil Babka 			/*
23277ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
23287ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
23297ed695e0SVlastimil Babka 			 */
2330f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
23312d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
23324bf2bba3SDavid Rientjes 				goto out;
2333748446bbSMel Gorman 			}
2334fdd048e1SVlastimil Babka 			/*
2335fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
2336fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
2337fdd048e1SVlastimil Babka 			 */
2338fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
2339fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
2340fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
2341fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
2342fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
2343566e54e1SMel Gorman 				last_migrated_pfn = 0;
2344fdd048e1SVlastimil Babka 			}
23454bf2bba3SDavid Rientjes 		}
2346fdaf7f5cSVlastimil Babka 
2347fdaf7f5cSVlastimil Babka check_drain:
2348fdaf7f5cSVlastimil Babka 		/*
2349fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
2350fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
2351fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
2352fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
2353fdaf7f5cSVlastimil Babka 		 * would succeed.
2354fdaf7f5cSVlastimil Babka 		 */
2355566e54e1SMel Gorman 		if (cc->order > 0 && last_migrated_pfn) {
2356fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
235706b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
2358fdaf7f5cSVlastimil Babka 
2359566e54e1SMel Gorman 			if (last_migrated_pfn < current_block_start) {
2360b01b2141SIngo Molnar 				lru_add_drain_cpu_zone(cc->zone);
2361fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
2362566e54e1SMel Gorman 				last_migrated_pfn = 0;
2363fdaf7f5cSVlastimil Babka 			}
2364fdaf7f5cSVlastimil Babka 		}
2365fdaf7f5cSVlastimil Babka 
23665e1f0f09SMel Gorman 		/* Stop if a page has been captured */
23675e1f0f09SMel Gorman 		if (capc && capc->page) {
23685e1f0f09SMel Gorman 			ret = COMPACT_SUCCESS;
23695e1f0f09SMel Gorman 			break;
23705e1f0f09SMel Gorman 		}
2371748446bbSMel Gorman 	}
2372748446bbSMel Gorman 
2373f9e35b3bSMel Gorman out:
23746bace090SVlastimil Babka 	/*
23756bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
23766bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
23776bace090SVlastimil Babka 	 */
23786bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
23796bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
23806bace090SVlastimil Babka 
23816bace090SVlastimil Babka 		cc->nr_freepages = 0;
23826bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
23836bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
238406b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
23856bace090SVlastimil Babka 		/*
23866bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
23876bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
23886bace090SVlastimil Babka 		 */
238940cacbcbSMel Gorman 		if (free_pfn > cc->zone->compact_cached_free_pfn)
239040cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = free_pfn;
23916bace090SVlastimil Babka 	}
2392748446bbSMel Gorman 
23937f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
23947f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
23957f354a54SDavid Rientjes 
239616c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
239716c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
23980eb927c0SMel Gorman 
2399748446bbSMel Gorman 	return ret;
2400748446bbSMel Gorman }
240176ab0f53SMel Gorman 
2402ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
2403c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
240497a225e6SJoonsoo Kim 		unsigned int alloc_flags, int highest_zoneidx,
24055e1f0f09SMel Gorman 		struct page **capture)
240656de7263SMel Gorman {
2407ea7ab982SMichal Hocko 	enum compact_result ret;
240856de7263SMel Gorman 	struct compact_control cc = {
240956de7263SMel Gorman 		.order = order,
2410dbe2d4e4SMel Gorman 		.search_order = order,
24116d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
241256de7263SMel Gorman 		.zone = zone,
2413a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
2414a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
2415ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
241697a225e6SJoonsoo Kim 		.highest_zoneidx = highest_zoneidx,
2417accf6242SVlastimil Babka 		.direct_compaction = true,
2418a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
24199f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
24209f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
242156de7263SMel Gorman 	};
24225e1f0f09SMel Gorman 	struct capture_control capc = {
24235e1f0f09SMel Gorman 		.cc = &cc,
24245e1f0f09SMel Gorman 		.page = NULL,
24255e1f0f09SMel Gorman 	};
24265e1f0f09SMel Gorman 
2427b9e20f0dSVlastimil Babka 	/*
2428b9e20f0dSVlastimil Babka 	 * Make sure the structs are really initialized before we expose the
2429b9e20f0dSVlastimil Babka 	 * capture control, in case we are interrupted and the interrupt handler
2430b9e20f0dSVlastimil Babka 	 * frees a page.
2431b9e20f0dSVlastimil Babka 	 */
2432b9e20f0dSVlastimil Babka 	barrier();
2433b9e20f0dSVlastimil Babka 	WRITE_ONCE(current->capture_control, &capc);
243456de7263SMel Gorman 
24355e1f0f09SMel Gorman 	ret = compact_zone(&cc, &capc);
2436e64c5237SShaohua Li 
2437e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
2438e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
2439e64c5237SShaohua Li 
2440b9e20f0dSVlastimil Babka 	/*
2441b9e20f0dSVlastimil Babka 	 * Make sure we hide capture control first before we read the captured
2442b9e20f0dSVlastimil Babka 	 * page pointer, otherwise an interrupt could free and capture a page
2443b9e20f0dSVlastimil Babka 	 * and we would leak it.
2444b9e20f0dSVlastimil Babka 	 */
2445b9e20f0dSVlastimil Babka 	WRITE_ONCE(current->capture_control, NULL);
2446b9e20f0dSVlastimil Babka 	*capture = READ_ONCE(capc.page);
24475e1f0f09SMel Gorman 
2448e64c5237SShaohua Li 	return ret;
244956de7263SMel Gorman }
245056de7263SMel Gorman 
24515e771905SMel Gorman int sysctl_extfrag_threshold = 500;
24525e771905SMel Gorman 
245356de7263SMel Gorman /**
245456de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
245556de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
24561a6d53a1SVlastimil Babka  * @order: The order of the current allocation
24571a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
24581a6d53a1SVlastimil Babka  * @ac: The context of current allocation
2459112d2d29SYang Shi  * @prio: Determines how hard direct compaction should try to succeed
24606467552cSVlastimil Babka  * @capture: Pointer to free page created by compaction will be stored here
246156de7263SMel Gorman  *
246256de7263SMel Gorman  * This is the main entry point for direct page compaction.
246356de7263SMel Gorman  */
2464ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
2465c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
24665e1f0f09SMel Gorman 		enum compact_priority prio, struct page **capture)
246756de7263SMel Gorman {
246856de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
246956de7263SMel Gorman 	struct zoneref *z;
247056de7263SMel Gorman 	struct zone *zone;
24711d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
247256de7263SMel Gorman 
247373e64c51SMichal Hocko 	/*
247473e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
247573e64c51SMichal Hocko 	 * tricky context because the migration might require IO
247673e64c51SMichal Hocko 	 */
247773e64c51SMichal Hocko 	if (!may_perform_io)
247853853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
247956de7263SMel Gorman 
2480a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
2481837d026dSJoonsoo Kim 
248256de7263SMel Gorman 	/* Compact each zone in the list */
248397a225e6SJoonsoo Kim 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
248497a225e6SJoonsoo Kim 					ac->highest_zoneidx, ac->nodemask) {
2485ea7ab982SMichal Hocko 		enum compact_result status;
248656de7263SMel Gorman 
2487a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
2488a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
24891d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
249053853e2dSVlastimil Babka 			continue;
24911d4746d3SMichal Hocko 		}
249253853e2dSVlastimil Babka 
2493a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
249497a225e6SJoonsoo Kim 				alloc_flags, ac->highest_zoneidx, capture);
249556de7263SMel Gorman 		rc = max(status, rc);
249656de7263SMel Gorman 
24977ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
24987ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
249953853e2dSVlastimil Babka 			/*
250053853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
250153853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
250253853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
250353853e2dSVlastimil Babka 			 * succeeds in this zone.
250453853e2dSVlastimil Babka 			 */
250553853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
25061f9efdefSVlastimil Babka 
2507c3486f53SVlastimil Babka 			break;
25081f9efdefSVlastimil Babka 		}
25091f9efdefSVlastimil Babka 
2510a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
2511c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
251253853e2dSVlastimil Babka 			/*
251353853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
251453853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
251553853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
251653853e2dSVlastimil Babka 			 */
251753853e2dSVlastimil Babka 			defer_compaction(zone, order);
25181f9efdefSVlastimil Babka 
25191f9efdefSVlastimil Babka 		/*
25201f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
25211f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
2522c3486f53SVlastimil Babka 		 * case do not try further zones
25231f9efdefSVlastimil Babka 		 */
2524c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2525c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
25261f9efdefSVlastimil Babka 			break;
25271f9efdefSVlastimil Babka 	}
25281f9efdefSVlastimil Babka 
252956de7263SMel Gorman 	return rc;
253056de7263SMel Gorman }
253156de7263SMel Gorman 
2532facdaa91SNitin Gupta /*
2533facdaa91SNitin Gupta  * Compact all zones within a node till each zone's fragmentation score
2534facdaa91SNitin Gupta  * reaches within proactive compaction thresholds (as determined by the
2535facdaa91SNitin Gupta  * proactiveness tunable).
2536facdaa91SNitin Gupta  *
2537facdaa91SNitin Gupta  * It is possible that the function returns before reaching score targets
2538facdaa91SNitin Gupta  * due to various back-off conditions, such as, contention on per-node or
2539facdaa91SNitin Gupta  * per-zone locks.
2540facdaa91SNitin Gupta  */
2541facdaa91SNitin Gupta static void proactive_compact_node(pg_data_t *pgdat)
2542facdaa91SNitin Gupta {
2543facdaa91SNitin Gupta 	int zoneid;
2544facdaa91SNitin Gupta 	struct zone *zone;
2545facdaa91SNitin Gupta 	struct compact_control cc = {
2546facdaa91SNitin Gupta 		.order = -1,
2547facdaa91SNitin Gupta 		.mode = MIGRATE_SYNC_LIGHT,
2548facdaa91SNitin Gupta 		.ignore_skip_hint = true,
2549facdaa91SNitin Gupta 		.whole_zone = true,
2550facdaa91SNitin Gupta 		.gfp_mask = GFP_KERNEL,
2551facdaa91SNitin Gupta 		.proactive_compaction = true,
2552facdaa91SNitin Gupta 	};
2553facdaa91SNitin Gupta 
2554facdaa91SNitin Gupta 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2555facdaa91SNitin Gupta 		zone = &pgdat->node_zones[zoneid];
2556facdaa91SNitin Gupta 		if (!populated_zone(zone))
2557facdaa91SNitin Gupta 			continue;
2558facdaa91SNitin Gupta 
2559facdaa91SNitin Gupta 		cc.zone = zone;
2560facdaa91SNitin Gupta 
2561facdaa91SNitin Gupta 		compact_zone(&cc, NULL);
2562facdaa91SNitin Gupta 
2563facdaa91SNitin Gupta 		VM_BUG_ON(!list_empty(&cc.freepages));
2564facdaa91SNitin Gupta 		VM_BUG_ON(!list_empty(&cc.migratepages));
2565facdaa91SNitin Gupta 	}
2566facdaa91SNitin Gupta }
256756de7263SMel Gorman 
256876ab0f53SMel Gorman /* Compact all zones within a node */
25697103f16dSAndrew Morton static void compact_node(int nid)
25707be62de9SRik van Riel {
2571791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2572791cae96SVlastimil Babka 	int zoneid;
2573791cae96SVlastimil Babka 	struct zone *zone;
25747be62de9SRik van Riel 	struct compact_control cc = {
25757be62de9SRik van Riel 		.order = -1,
2576e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
257791ca9186SDavid Rientjes 		.ignore_skip_hint = true,
257806ed2998SVlastimil Babka 		.whole_zone = true,
257973e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
25807be62de9SRik van Riel 	};
25817be62de9SRik van Riel 
2582791cae96SVlastimil Babka 
2583791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2584791cae96SVlastimil Babka 
2585791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2586791cae96SVlastimil Babka 		if (!populated_zone(zone))
2587791cae96SVlastimil Babka 			continue;
2588791cae96SVlastimil Babka 
2589791cae96SVlastimil Babka 		cc.zone = zone;
2590791cae96SVlastimil Babka 
25915e1f0f09SMel Gorman 		compact_zone(&cc, NULL);
2592791cae96SVlastimil Babka 
2593791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2594791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2595791cae96SVlastimil Babka 	}
25967be62de9SRik van Riel }
25977be62de9SRik van Riel 
259876ab0f53SMel Gorman /* Compact all nodes in the system */
25997964c06dSJason Liu static void compact_nodes(void)
260076ab0f53SMel Gorman {
260176ab0f53SMel Gorman 	int nid;
260276ab0f53SMel Gorman 
26038575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
26048575ec29SHugh Dickins 	lru_add_drain_all();
26058575ec29SHugh Dickins 
260676ab0f53SMel Gorman 	for_each_online_node(nid)
260776ab0f53SMel Gorman 		compact_node(nid);
260876ab0f53SMel Gorman }
260976ab0f53SMel Gorman 
261076ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
261176ab0f53SMel Gorman int sysctl_compact_memory;
261276ab0f53SMel Gorman 
2613fec4eb2cSYaowei Bai /*
2614facdaa91SNitin Gupta  * Tunable for proactive compaction. It determines how
2615facdaa91SNitin Gupta  * aggressively the kernel should compact memory in the
2616facdaa91SNitin Gupta  * background. It takes values in the range [0, 100].
2617facdaa91SNitin Gupta  */
2618d34c0a75SNitin Gupta unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
2619facdaa91SNitin Gupta 
2620facdaa91SNitin Gupta /*
2621fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
2622fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
2623fec4eb2cSYaowei Bai  */
262476ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
262532927393SChristoph Hellwig 			void *buffer, size_t *length, loff_t *ppos)
262676ab0f53SMel Gorman {
262776ab0f53SMel Gorman 	if (write)
26287964c06dSJason Liu 		compact_nodes();
262976ab0f53SMel Gorman 
263076ab0f53SMel Gorman 	return 0;
263176ab0f53SMel Gorman }
2632ed4a6d7fSMel Gorman 
2633ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
263474e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
263510fbcf4cSKay Sievers 			struct device_attribute *attr,
2636ed4a6d7fSMel Gorman 			const char *buf, size_t count)
2637ed4a6d7fSMel Gorman {
26388575ec29SHugh Dickins 	int nid = dev->id;
26398575ec29SHugh Dickins 
26408575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
26418575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
26428575ec29SHugh Dickins 		lru_add_drain_all();
26438575ec29SHugh Dickins 
26448575ec29SHugh Dickins 		compact_node(nid);
26458575ec29SHugh Dickins 	}
2646ed4a6d7fSMel Gorman 
2647ed4a6d7fSMel Gorman 	return count;
2648ed4a6d7fSMel Gorman }
26490825a6f9SJoe Perches static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node);
2650ed4a6d7fSMel Gorman 
2651ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
2652ed4a6d7fSMel Gorman {
265310fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
2654ed4a6d7fSMel Gorman }
2655ed4a6d7fSMel Gorman 
2656ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
2657ed4a6d7fSMel Gorman {
265810fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
2659ed4a6d7fSMel Gorman }
2660ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
2661ff9543fdSMichal Nazarewicz 
2662698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2663698b1b30SVlastimil Babka {
2664172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
2665698b1b30SVlastimil Babka }
2666698b1b30SVlastimil Babka 
2667698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
2668698b1b30SVlastimil Babka {
2669698b1b30SVlastimil Babka 	int zoneid;
2670698b1b30SVlastimil Babka 	struct zone *zone;
267197a225e6SJoonsoo Kim 	enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
2672698b1b30SVlastimil Babka 
267397a225e6SJoonsoo Kim 	for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
2674698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2675698b1b30SVlastimil Babka 
2676698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2677698b1b30SVlastimil Babka 			continue;
2678698b1b30SVlastimil Babka 
2679698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
268097a225e6SJoonsoo Kim 					highest_zoneidx) == COMPACT_CONTINUE)
2681698b1b30SVlastimil Babka 			return true;
2682698b1b30SVlastimil Babka 	}
2683698b1b30SVlastimil Babka 
2684698b1b30SVlastimil Babka 	return false;
2685698b1b30SVlastimil Babka }
2686698b1b30SVlastimil Babka 
2687698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
2688698b1b30SVlastimil Babka {
2689698b1b30SVlastimil Babka 	/*
2690698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
2691698b1b30SVlastimil Babka 	 * order is allocatable.
2692698b1b30SVlastimil Babka 	 */
2693698b1b30SVlastimil Babka 	int zoneid;
2694698b1b30SVlastimil Babka 	struct zone *zone;
2695698b1b30SVlastimil Babka 	struct compact_control cc = {
2696698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
2697dbe2d4e4SMel Gorman 		.search_order = pgdat->kcompactd_max_order,
269897a225e6SJoonsoo Kim 		.highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
2699698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
2700a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
270173e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
2702698b1b30SVlastimil Babka 	};
2703698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
270497a225e6SJoonsoo Kim 							cc.highest_zoneidx);
27057f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
2706698b1b30SVlastimil Babka 
270797a225e6SJoonsoo Kim 	for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
2708698b1b30SVlastimil Babka 		int status;
2709698b1b30SVlastimil Babka 
2710698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2711698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2712698b1b30SVlastimil Babka 			continue;
2713698b1b30SVlastimil Babka 
2714698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
2715698b1b30SVlastimil Babka 			continue;
2716698b1b30SVlastimil Babka 
2717698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
2718698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
2719698b1b30SVlastimil Babka 			continue;
2720698b1b30SVlastimil Babka 
2721172400c6SVlastimil Babka 		if (kthread_should_stop())
2722172400c6SVlastimil Babka 			return;
2723a94b5252SYafang Shao 
2724a94b5252SYafang Shao 		cc.zone = zone;
27255e1f0f09SMel Gorman 		status = compact_zone(&cc, NULL);
2726698b1b30SVlastimil Babka 
27277ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
2728698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
2729c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
2730698b1b30SVlastimil Babka 			/*
2731bc3106b2SDavid Rientjes 			 * Buddy pages may become stranded on pcps that could
2732bc3106b2SDavid Rientjes 			 * otherwise coalesce on the zone's free area for
2733bc3106b2SDavid Rientjes 			 * order >= cc.order.  This is ratelimited by the
2734bc3106b2SDavid Rientjes 			 * upcoming deferral.
2735bc3106b2SDavid Rientjes 			 */
2736bc3106b2SDavid Rientjes 			drain_all_pages(zone);
2737bc3106b2SDavid Rientjes 
2738bc3106b2SDavid Rientjes 			/*
2739698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
2740698b1b30SVlastimil Babka 			 * sync direct compaction does.
2741698b1b30SVlastimil Babka 			 */
2742698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
2743698b1b30SVlastimil Babka 		}
2744698b1b30SVlastimil Babka 
27457f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
27467f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
27477f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
27487f354a54SDavid Rientjes 				     cc.total_free_scanned);
27497f354a54SDavid Rientjes 
2750698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2751698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2752698b1b30SVlastimil Babka 	}
2753698b1b30SVlastimil Babka 
2754698b1b30SVlastimil Babka 	/*
2755698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
275697a225e6SJoonsoo Kim 	 * the requested order/highest_zoneidx in case it was higher/tighter
275797a225e6SJoonsoo Kim 	 * than our current ones
2758698b1b30SVlastimil Babka 	 */
2759698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2760698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
276197a225e6SJoonsoo Kim 	if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
276297a225e6SJoonsoo Kim 		pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2763698b1b30SVlastimil Babka }
2764698b1b30SVlastimil Babka 
276597a225e6SJoonsoo Kim void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
2766698b1b30SVlastimil Babka {
2767698b1b30SVlastimil Babka 	if (!order)
2768698b1b30SVlastimil Babka 		return;
2769698b1b30SVlastimil Babka 
2770698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2771698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2772698b1b30SVlastimil Babka 
277397a225e6SJoonsoo Kim 	if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
277497a225e6SJoonsoo Kim 		pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
2775698b1b30SVlastimil Babka 
27766818600fSDavidlohr Bueso 	/*
27776818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
27786818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
27796818600fSDavidlohr Bueso 	 */
27806818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2781698b1b30SVlastimil Babka 		return;
2782698b1b30SVlastimil Babka 
2783698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2784698b1b30SVlastimil Babka 		return;
2785698b1b30SVlastimil Babka 
2786698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
278797a225e6SJoonsoo Kim 							highest_zoneidx);
2788698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2789698b1b30SVlastimil Babka }
2790698b1b30SVlastimil Babka 
2791698b1b30SVlastimil Babka /*
2792698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2793698b1b30SVlastimil Babka  * from the init process.
2794698b1b30SVlastimil Babka  */
2795698b1b30SVlastimil Babka static int kcompactd(void *p)
2796698b1b30SVlastimil Babka {
2797698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2798698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2799facdaa91SNitin Gupta 	unsigned int proactive_defer = 0;
2800698b1b30SVlastimil Babka 
2801698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2802698b1b30SVlastimil Babka 
2803698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2804698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2805698b1b30SVlastimil Babka 
2806698b1b30SVlastimil Babka 	set_freezable();
2807698b1b30SVlastimil Babka 
2808698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
280997a225e6SJoonsoo Kim 	pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2810698b1b30SVlastimil Babka 
2811698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2812eb414681SJohannes Weiner 		unsigned long pflags;
2813eb414681SJohannes Weiner 
2814698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2815facdaa91SNitin Gupta 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
2816facdaa91SNitin Gupta 			kcompactd_work_requested(pgdat),
2817facdaa91SNitin Gupta 			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
2818698b1b30SVlastimil Babka 
2819eb414681SJohannes Weiner 			psi_memstall_enter(&pflags);
2820698b1b30SVlastimil Babka 			kcompactd_do_work(pgdat);
2821eb414681SJohannes Weiner 			psi_memstall_leave(&pflags);
2822facdaa91SNitin Gupta 			continue;
2823facdaa91SNitin Gupta 		}
2824facdaa91SNitin Gupta 
2825facdaa91SNitin Gupta 		/* kcompactd wait timeout */
2826facdaa91SNitin Gupta 		if (should_proactive_compact_node(pgdat)) {
2827facdaa91SNitin Gupta 			unsigned int prev_score, score;
2828facdaa91SNitin Gupta 
2829facdaa91SNitin Gupta 			if (proactive_defer) {
2830facdaa91SNitin Gupta 				proactive_defer--;
2831facdaa91SNitin Gupta 				continue;
2832facdaa91SNitin Gupta 			}
2833facdaa91SNitin Gupta 			prev_score = fragmentation_score_node(pgdat);
2834facdaa91SNitin Gupta 			proactive_compact_node(pgdat);
2835facdaa91SNitin Gupta 			score = fragmentation_score_node(pgdat);
2836facdaa91SNitin Gupta 			/*
2837facdaa91SNitin Gupta 			 * Defer proactive compaction if the fragmentation
2838facdaa91SNitin Gupta 			 * score did not go down i.e. no progress made.
2839facdaa91SNitin Gupta 			 */
2840facdaa91SNitin Gupta 			proactive_defer = score < prev_score ?
2841facdaa91SNitin Gupta 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
2842facdaa91SNitin Gupta 		}
2843698b1b30SVlastimil Babka 	}
2844698b1b30SVlastimil Babka 
2845698b1b30SVlastimil Babka 	return 0;
2846698b1b30SVlastimil Babka }
2847698b1b30SVlastimil Babka 
2848698b1b30SVlastimil Babka /*
2849698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2850698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2851698b1b30SVlastimil Babka  */
2852698b1b30SVlastimil Babka int kcompactd_run(int nid)
2853698b1b30SVlastimil Babka {
2854698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2855698b1b30SVlastimil Babka 	int ret = 0;
2856698b1b30SVlastimil Babka 
2857698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2858698b1b30SVlastimil Babka 		return 0;
2859698b1b30SVlastimil Babka 
2860698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2861698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2862698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2863698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2864698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2865698b1b30SVlastimil Babka 	}
2866698b1b30SVlastimil Babka 	return ret;
2867698b1b30SVlastimil Babka }
2868698b1b30SVlastimil Babka 
2869698b1b30SVlastimil Babka /*
2870698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2871698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2872698b1b30SVlastimil Babka  */
2873698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2874698b1b30SVlastimil Babka {
2875698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2876698b1b30SVlastimil Babka 
2877698b1b30SVlastimil Babka 	if (kcompactd) {
2878698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2879698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2880698b1b30SVlastimil Babka 	}
2881698b1b30SVlastimil Babka }
2882698b1b30SVlastimil Babka 
2883698b1b30SVlastimil Babka /*
2884698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2885698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2886698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2887698b1b30SVlastimil Babka  * restore their cpu bindings.
2888698b1b30SVlastimil Babka  */
2889e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2890698b1b30SVlastimil Babka {
2891698b1b30SVlastimil Babka 	int nid;
2892698b1b30SVlastimil Babka 
2893698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2894698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2895698b1b30SVlastimil Babka 		const struct cpumask *mask;
2896698b1b30SVlastimil Babka 
2897698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2898698b1b30SVlastimil Babka 
2899698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2900698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2901698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2902698b1b30SVlastimil Babka 	}
2903e46b1db2SAnna-Maria Gleixner 	return 0;
2904698b1b30SVlastimil Babka }
2905698b1b30SVlastimil Babka 
2906698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2907698b1b30SVlastimil Babka {
2908698b1b30SVlastimil Babka 	int nid;
2909e46b1db2SAnna-Maria Gleixner 	int ret;
2910e46b1db2SAnna-Maria Gleixner 
2911e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2912e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2913e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2914e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2915e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2916e46b1db2SAnna-Maria Gleixner 		return ret;
2917e46b1db2SAnna-Maria Gleixner 	}
2918698b1b30SVlastimil Babka 
2919698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2920698b1b30SVlastimil Babka 		kcompactd_run(nid);
2921698b1b30SVlastimil Babka 	return 0;
2922698b1b30SVlastimil Babka }
2923698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2924698b1b30SVlastimil Babka 
2925ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2926