xref: /openbmc/linux/mm/compaction.c (revision d99fd5feb0ac1d56c36c760a8d922a46bd6c5521)
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  */
1602271b016SHui Su static 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 */
1752271b016SHui Su static 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 */
18362b35fe0SMateusz Nosek 	if (++zone->compact_considered >= defer_limit) {
18424e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
18524e2716fSJoonsoo Kim 		return false;
18662b35fe0SMateusz Nosek 	}
18724e2716fSJoonsoo Kim 
18824e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
18924e2716fSJoonsoo Kim 
19024e2716fSJoonsoo Kim 	return true;
19124e2716fSJoonsoo Kim }
19224e2716fSJoonsoo Kim 
19324e2716fSJoonsoo Kim /*
19424e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
19524e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
19624e2716fSJoonsoo Kim  * expected to succeed.
19724e2716fSJoonsoo Kim  */
19824e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
19924e2716fSJoonsoo Kim 		bool alloc_success)
20024e2716fSJoonsoo Kim {
20124e2716fSJoonsoo Kim 	if (alloc_success) {
20224e2716fSJoonsoo Kim 		zone->compact_considered = 0;
20324e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
20424e2716fSJoonsoo Kim 	}
20524e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
20624e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
20724e2716fSJoonsoo Kim 
20824e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
20924e2716fSJoonsoo Kim }
21024e2716fSJoonsoo Kim 
21124e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
2122271b016SHui Su static bool compaction_restarting(struct zone *zone, int order)
21324e2716fSJoonsoo Kim {
21424e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
21524e2716fSJoonsoo Kim 		return false;
21624e2716fSJoonsoo Kim 
21724e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
21824e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
21924e2716fSJoonsoo Kim }
22024e2716fSJoonsoo Kim 
221bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
222bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
223bb13ffebSMel Gorman 					struct page *page)
224bb13ffebSMel Gorman {
225bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
226bb13ffebSMel Gorman 		return true;
227bb13ffebSMel Gorman 
228bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
229bb13ffebSMel Gorman }
230bb13ffebSMel Gorman 
23102333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
23202333641SVlastimil Babka {
23302333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
23402333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
235623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
23606b6640aSVlastimil Babka 				pageblock_start_pfn(zone_end_pfn(zone) - 1);
23702333641SVlastimil Babka }
23802333641SVlastimil Babka 
239bb13ffebSMel Gorman /*
2402271b016SHui Su  * Compound pages of >= pageblock_order should consistently be skipped until
241b527cfe5SVlastimil Babka  * released. It is always pointless to compact pages of such order (if they are
242b527cfe5SVlastimil Babka  * migratable), and the pageblocks they occupy cannot contain any free pages.
24321dc7e02SDavid Rientjes  */
244b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page)
24521dc7e02SDavid Rientjes {
246b527cfe5SVlastimil Babka 	if (!PageCompound(page))
24721dc7e02SDavid Rientjes 		return false;
248b527cfe5SVlastimil Babka 
249b527cfe5SVlastimil Babka 	page = compound_head(page);
250b527cfe5SVlastimil Babka 
251b527cfe5SVlastimil Babka 	if (compound_order(page) >= pageblock_order)
25221dc7e02SDavid Rientjes 		return true;
253b527cfe5SVlastimil Babka 
254b527cfe5SVlastimil Babka 	return false;
25521dc7e02SDavid Rientjes }
25621dc7e02SDavid Rientjes 
257e332f741SMel Gorman static bool
258e332f741SMel Gorman __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
259e332f741SMel Gorman 							bool check_target)
260e332f741SMel Gorman {
261e332f741SMel Gorman 	struct page *page = pfn_to_online_page(pfn);
2626b0868c8SMel Gorman 	struct page *block_page;
263e332f741SMel Gorman 	struct page *end_page;
264e332f741SMel Gorman 	unsigned long block_pfn;
265e332f741SMel Gorman 
266e332f741SMel Gorman 	if (!page)
267e332f741SMel Gorman 		return false;
268e332f741SMel Gorman 	if (zone != page_zone(page))
269e332f741SMel Gorman 		return false;
270e332f741SMel Gorman 	if (pageblock_skip_persistent(page))
271e332f741SMel Gorman 		return false;
272e332f741SMel Gorman 
273e332f741SMel Gorman 	/*
274e332f741SMel Gorman 	 * If skip is already cleared do no further checking once the
275e332f741SMel Gorman 	 * restart points have been set.
276e332f741SMel Gorman 	 */
277e332f741SMel Gorman 	if (check_source && check_target && !get_pageblock_skip(page))
278e332f741SMel Gorman 		return true;
279e332f741SMel Gorman 
280e332f741SMel Gorman 	/*
281e332f741SMel Gorman 	 * If clearing skip for the target scanner, do not select a
282e332f741SMel Gorman 	 * non-movable pageblock as the starting point.
283e332f741SMel Gorman 	 */
284e332f741SMel Gorman 	if (!check_source && check_target &&
285e332f741SMel Gorman 	    get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
286e332f741SMel Gorman 		return false;
287e332f741SMel Gorman 
2886b0868c8SMel Gorman 	/* Ensure the start of the pageblock or zone is online and valid */
2896b0868c8SMel Gorman 	block_pfn = pageblock_start_pfn(pfn);
290a2e9a5afSVlastimil Babka 	block_pfn = max(block_pfn, zone->zone_start_pfn);
291a2e9a5afSVlastimil Babka 	block_page = pfn_to_online_page(block_pfn);
2926b0868c8SMel Gorman 	if (block_page) {
2936b0868c8SMel Gorman 		page = block_page;
2946b0868c8SMel Gorman 		pfn = block_pfn;
2956b0868c8SMel Gorman 	}
2966b0868c8SMel Gorman 
2976b0868c8SMel Gorman 	/* Ensure the end of the pageblock or zone is online and valid */
298a2e9a5afSVlastimil Babka 	block_pfn = pageblock_end_pfn(pfn) - 1;
2996b0868c8SMel Gorman 	block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
3006b0868c8SMel Gorman 	end_page = pfn_to_online_page(block_pfn);
3016b0868c8SMel Gorman 	if (!end_page)
3026b0868c8SMel Gorman 		return false;
3036b0868c8SMel Gorman 
304e332f741SMel Gorman 	/*
305e332f741SMel Gorman 	 * Only clear the hint if a sample indicates there is either a
306e332f741SMel Gorman 	 * free page or an LRU page in the block. One or other condition
307e332f741SMel Gorman 	 * is necessary for the block to be a migration source/target.
308e332f741SMel Gorman 	 */
309e332f741SMel Gorman 	do {
310e332f741SMel Gorman 		if (pfn_valid_within(pfn)) {
311e332f741SMel Gorman 			if (check_source && PageLRU(page)) {
312e332f741SMel Gorman 				clear_pageblock_skip(page);
313e332f741SMel Gorman 				return true;
314e332f741SMel Gorman 			}
315e332f741SMel Gorman 
316e332f741SMel Gorman 			if (check_target && PageBuddy(page)) {
317e332f741SMel Gorman 				clear_pageblock_skip(page);
318e332f741SMel Gorman 				return true;
319e332f741SMel Gorman 			}
320e332f741SMel Gorman 		}
321e332f741SMel Gorman 
322e332f741SMel Gorman 		page += (1 << PAGE_ALLOC_COSTLY_ORDER);
323e332f741SMel Gorman 		pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
324a2e9a5afSVlastimil Babka 	} while (page <= end_page);
325e332f741SMel Gorman 
326e332f741SMel Gorman 	return false;
327e332f741SMel Gorman }
328e332f741SMel Gorman 
32921dc7e02SDavid Rientjes /*
330bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
331bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
332bb13ffebSMel Gorman  * meet.
333bb13ffebSMel Gorman  */
33462997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
335bb13ffebSMel Gorman {
336e332f741SMel Gorman 	unsigned long migrate_pfn = zone->zone_start_pfn;
3376b0868c8SMel Gorman 	unsigned long free_pfn = zone_end_pfn(zone) - 1;
338e332f741SMel Gorman 	unsigned long reset_migrate = free_pfn;
339e332f741SMel Gorman 	unsigned long reset_free = migrate_pfn;
340e332f741SMel Gorman 	bool source_set = false;
341e332f741SMel Gorman 	bool free_set = false;
342e332f741SMel Gorman 
343e332f741SMel Gorman 	if (!zone->compact_blockskip_flush)
344e332f741SMel Gorman 		return;
345bb13ffebSMel Gorman 
34662997027SMel Gorman 	zone->compact_blockskip_flush = false;
347bb13ffebSMel Gorman 
348e332f741SMel Gorman 	/*
349e332f741SMel Gorman 	 * Walk the zone and update pageblock skip information. Source looks
350e332f741SMel Gorman 	 * for PageLRU while target looks for PageBuddy. When the scanner
351e332f741SMel Gorman 	 * is found, both PageBuddy and PageLRU are checked as the pageblock
352e332f741SMel Gorman 	 * is suitable as both source and target.
353e332f741SMel Gorman 	 */
354e332f741SMel Gorman 	for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
355e332f741SMel Gorman 					free_pfn -= pageblock_nr_pages) {
356bb13ffebSMel Gorman 		cond_resched();
357bb13ffebSMel Gorman 
358e332f741SMel Gorman 		/* Update the migrate PFN */
359e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
360e332f741SMel Gorman 		    migrate_pfn < reset_migrate) {
361e332f741SMel Gorman 			source_set = true;
362e332f741SMel Gorman 			reset_migrate = migrate_pfn;
363e332f741SMel Gorman 			zone->compact_init_migrate_pfn = reset_migrate;
364e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[0] = reset_migrate;
365e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[1] = reset_migrate;
366bb13ffebSMel Gorman 		}
36702333641SVlastimil Babka 
368e332f741SMel Gorman 		/* Update the free PFN */
369e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
370e332f741SMel Gorman 		    free_pfn > reset_free) {
371e332f741SMel Gorman 			free_set = true;
372e332f741SMel Gorman 			reset_free = free_pfn;
373e332f741SMel Gorman 			zone->compact_init_free_pfn = reset_free;
374e332f741SMel Gorman 			zone->compact_cached_free_pfn = reset_free;
375e332f741SMel Gorman 		}
376e332f741SMel Gorman 	}
377e332f741SMel Gorman 
378e332f741SMel Gorman 	/* Leave no distance if no suitable block was reset */
379e332f741SMel Gorman 	if (reset_migrate >= reset_free) {
380e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[0] = migrate_pfn;
381e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[1] = migrate_pfn;
382e332f741SMel Gorman 		zone->compact_cached_free_pfn = free_pfn;
383e332f741SMel Gorman 	}
384bb13ffebSMel Gorman }
385bb13ffebSMel Gorman 
38662997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
38762997027SMel Gorman {
38862997027SMel Gorman 	int zoneid;
38962997027SMel Gorman 
39062997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
39162997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
39262997027SMel Gorman 		if (!populated_zone(zone))
39362997027SMel Gorman 			continue;
39462997027SMel Gorman 
39562997027SMel Gorman 		/* Only flush if a full compaction finished recently */
39662997027SMel Gorman 		if (zone->compact_blockskip_flush)
39762997027SMel Gorman 			__reset_isolation_suitable(zone);
39862997027SMel Gorman 	}
39962997027SMel Gorman }
40062997027SMel Gorman 
401bb13ffebSMel Gorman /*
402e380bebeSMel Gorman  * Sets the pageblock skip bit if it was clear. Note that this is a hint as
403e380bebeSMel Gorman  * locks are not required for read/writers. Returns true if it was already set.
404e380bebeSMel Gorman  */
405e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
406e380bebeSMel Gorman 							unsigned long pfn)
407e380bebeSMel Gorman {
408e380bebeSMel Gorman 	bool skip;
409e380bebeSMel Gorman 
410e380bebeSMel Gorman 	/* Do no update if skip hint is being ignored */
411e380bebeSMel Gorman 	if (cc->ignore_skip_hint)
412e380bebeSMel Gorman 		return false;
413e380bebeSMel Gorman 
414e380bebeSMel Gorman 	if (!IS_ALIGNED(pfn, pageblock_nr_pages))
415e380bebeSMel Gorman 		return false;
416e380bebeSMel Gorman 
417e380bebeSMel Gorman 	skip = get_pageblock_skip(page);
418e380bebeSMel Gorman 	if (!skip && !cc->no_set_skip_hint)
419e380bebeSMel Gorman 		set_pageblock_skip(page);
420e380bebeSMel Gorman 
421e380bebeSMel Gorman 	return skip;
422e380bebeSMel Gorman }
423e380bebeSMel Gorman 
424e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
425e380bebeSMel Gorman {
426e380bebeSMel Gorman 	struct zone *zone = cc->zone;
427e380bebeSMel Gorman 
428e380bebeSMel Gorman 	pfn = pageblock_end_pfn(pfn);
429e380bebeSMel Gorman 
430e380bebeSMel Gorman 	/* Set for isolation rather than compaction */
431e380bebeSMel Gorman 	if (cc->no_set_skip_hint)
432e380bebeSMel Gorman 		return;
433e380bebeSMel Gorman 
434e380bebeSMel Gorman 	if (pfn > zone->compact_cached_migrate_pfn[0])
435e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[0] = pfn;
436e380bebeSMel Gorman 	if (cc->mode != MIGRATE_ASYNC &&
437e380bebeSMel Gorman 	    pfn > zone->compact_cached_migrate_pfn[1])
438e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[1] = pfn;
439e380bebeSMel Gorman }
440e380bebeSMel Gorman 
441e380bebeSMel Gorman /*
442bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
44362997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
444bb13ffebSMel Gorman  */
445c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
446d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
447bb13ffebSMel Gorman {
448c89511abSMel Gorman 	struct zone *zone = cc->zone;
4496815bf3fSJoonsoo Kim 
4502583d671SVlastimil Babka 	if (cc->no_set_skip_hint)
4516815bf3fSJoonsoo Kim 		return;
4526815bf3fSJoonsoo Kim 
453bb13ffebSMel Gorman 	if (!page)
454bb13ffebSMel Gorman 		return;
455bb13ffebSMel Gorman 
456bb13ffebSMel Gorman 	set_pageblock_skip(page);
457c89511abSMel Gorman 
45835979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
45935979ef3SDavid Rientjes 	if (pfn < zone->compact_cached_free_pfn)
460c89511abSMel Gorman 		zone->compact_cached_free_pfn = pfn;
461c89511abSMel Gorman }
462bb13ffebSMel Gorman #else
463bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
464bb13ffebSMel Gorman 					struct page *page)
465bb13ffebSMel Gorman {
466bb13ffebSMel Gorman 	return true;
467bb13ffebSMel Gorman }
468bb13ffebSMel Gorman 
469b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page)
47021dc7e02SDavid Rientjes {
47121dc7e02SDavid Rientjes 	return false;
47221dc7e02SDavid Rientjes }
47321dc7e02SDavid Rientjes 
47421dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc,
475d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
476bb13ffebSMel Gorman {
477bb13ffebSMel Gorman }
478e380bebeSMel Gorman 
479e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
480e380bebeSMel Gorman {
481e380bebeSMel Gorman }
482e380bebeSMel Gorman 
483e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
484e380bebeSMel Gorman 							unsigned long pfn)
485e380bebeSMel Gorman {
486e380bebeSMel Gorman 	return false;
487e380bebeSMel Gorman }
488bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
489bb13ffebSMel Gorman 
4901f9efdefSVlastimil Babka /*
4918b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
492cb2dcaf0SMel Gorman  * very heavily contended. For async compaction, trylock and record if the
493cb2dcaf0SMel Gorman  * lock is contended. The lock will still be acquired but compaction will
494cb2dcaf0SMel Gorman  * abort when the current block is finished regardless of success rate.
495cb2dcaf0SMel Gorman  * Sync compaction acquires the lock.
4968b44d279SVlastimil Babka  *
497cb2dcaf0SMel Gorman  * Always returns true which makes it easier to track lock state in callers.
4981f9efdefSVlastimil Babka  */
499cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
5008b44d279SVlastimil Babka 						struct compact_control *cc)
50177337edeSJules Irenge 	__acquires(lock)
5028b44d279SVlastimil Babka {
503cb2dcaf0SMel Gorman 	/* Track if the lock is contended in async mode */
504cb2dcaf0SMel Gorman 	if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
505cb2dcaf0SMel Gorman 		if (spin_trylock_irqsave(lock, *flags))
506cb2dcaf0SMel Gorman 			return true;
507cb2dcaf0SMel Gorman 
508c3486f53SVlastimil Babka 		cc->contended = true;
5098b44d279SVlastimil Babka 	}
5101f9efdefSVlastimil Babka 
511cb2dcaf0SMel Gorman 	spin_lock_irqsave(lock, *flags);
5128b44d279SVlastimil Babka 	return true;
5132a1402aaSMel Gorman }
5142a1402aaSMel Gorman 
51585aa125fSMichal Nazarewicz /*
516c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
5178b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
5188b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
5198b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
5208b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
5218b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
5228b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
5238b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
524c67fe375SMel Gorman  *
5258b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
5268b44d279SVlastimil Babka  *		async compaction due to need_resched()
5278b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
5288b44d279SVlastimil Babka  *		scheduled)
529c67fe375SMel Gorman  */
5308b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
5318b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
532c67fe375SMel Gorman {
5338b44d279SVlastimil Babka 	if (*locked) {
5348b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
5358b44d279SVlastimil Babka 		*locked = false;
536c67fe375SMel Gorman 	}
537c67fe375SMel Gorman 
5388b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
539c3486f53SVlastimil Babka 		cc->contended = true;
5408b44d279SVlastimil Babka 		return true;
5418b44d279SVlastimil Babka 	}
5428b44d279SVlastimil Babka 
543cf66f070SMel Gorman 	cond_resched();
544be976572SVlastimil Babka 
545be976572SVlastimil Babka 	return false;
546be976572SVlastimil Babka }
547be976572SVlastimil Babka 
548c67fe375SMel Gorman /*
5499e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
5509e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
5519e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
55285aa125fSMichal Nazarewicz  */
553f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
554e14c720eSVlastimil Babka 				unsigned long *start_pfn,
55585aa125fSMichal Nazarewicz 				unsigned long end_pfn,
55685aa125fSMichal Nazarewicz 				struct list_head *freelist,
5574fca9730SMel Gorman 				unsigned int stride,
55885aa125fSMichal Nazarewicz 				bool strict)
559748446bbSMel Gorman {
560b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
561d097a6f6SMel Gorman 	struct page *cursor;
562b8b2d825SXiubo Li 	unsigned long flags = 0;
563f40d1e42SMel Gorman 	bool locked = false;
564e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
56566c64223SJoonsoo Kim 	unsigned int order;
566748446bbSMel Gorman 
5674fca9730SMel Gorman 	/* Strict mode is for isolation, speed is secondary */
5684fca9730SMel Gorman 	if (strict)
5694fca9730SMel Gorman 		stride = 1;
5704fca9730SMel Gorman 
571748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
572748446bbSMel Gorman 
573f40d1e42SMel Gorman 	/* Isolate free pages. */
5744fca9730SMel Gorman 	for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
57566c64223SJoonsoo Kim 		int isolated;
576748446bbSMel Gorman 		struct page *page = cursor;
577748446bbSMel Gorman 
5788b44d279SVlastimil Babka 		/*
5798b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
5808b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
5818b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
5828b44d279SVlastimil Babka 		 */
5838b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
5848b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
5858b44d279SVlastimil Babka 								&locked, cc))
5868b44d279SVlastimil Babka 			break;
5878b44d279SVlastimil Babka 
588b7aba698SMel Gorman 		nr_scanned++;
589f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
5902af120bcSLaura Abbott 			goto isolate_fail;
5912af120bcSLaura Abbott 
5929fcd6d2eSVlastimil Babka 		/*
5939fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
5949fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
5959fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
5969fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
5979fcd6d2eSVlastimil Babka 		 */
5989fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
59921dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
6009fcd6d2eSVlastimil Babka 
601d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER)) {
60221dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
60321dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
6049fcd6d2eSVlastimil Babka 			}
6059fcd6d2eSVlastimil Babka 			goto isolate_fail;
6069fcd6d2eSVlastimil Babka 		}
6079fcd6d2eSVlastimil Babka 
608f40d1e42SMel Gorman 		if (!PageBuddy(page))
6092af120bcSLaura Abbott 			goto isolate_fail;
610f40d1e42SMel Gorman 
611f40d1e42SMel Gorman 		/*
61269b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
61369b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
61469b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
61569b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
61669b7189fSVlastimil Babka 		 * recheck as well.
61769b7189fSVlastimil Babka 		 */
61869b7189fSVlastimil Babka 		if (!locked) {
619cb2dcaf0SMel Gorman 			locked = compact_lock_irqsave(&cc->zone->lock,
6208b44d279SVlastimil Babka 								&flags, cc);
621f40d1e42SMel Gorman 
622f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
623f40d1e42SMel Gorman 			if (!PageBuddy(page))
6242af120bcSLaura Abbott 				goto isolate_fail;
62569b7189fSVlastimil Babka 		}
626748446bbSMel Gorman 
62766c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
628ab130f91SMatthew Wilcox (Oracle) 		order = buddy_order(page);
62966c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
630a4f04f2cSDavid Rientjes 		if (!isolated)
631a4f04f2cSDavid Rientjes 			break;
63266c64223SJoonsoo Kim 		set_page_private(page, order);
633a4f04f2cSDavid Rientjes 
634748446bbSMel Gorman 		total_isolated += isolated;
635a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
63666c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
63766c64223SJoonsoo Kim 
638a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
639932ff6bbSJoonsoo Kim 			blockpfn += isolated;
640932ff6bbSJoonsoo Kim 			break;
641932ff6bbSJoonsoo Kim 		}
642a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
643748446bbSMel Gorman 		blockpfn += isolated - 1;
644748446bbSMel Gorman 		cursor += isolated - 1;
6452af120bcSLaura Abbott 		continue;
6462af120bcSLaura Abbott 
6472af120bcSLaura Abbott isolate_fail:
6482af120bcSLaura Abbott 		if (strict)
6492af120bcSLaura Abbott 			break;
6502af120bcSLaura Abbott 		else
6512af120bcSLaura Abbott 			continue;
6522af120bcSLaura Abbott 
653748446bbSMel Gorman 	}
654748446bbSMel Gorman 
655a4f04f2cSDavid Rientjes 	if (locked)
656a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
657a4f04f2cSDavid Rientjes 
6589fcd6d2eSVlastimil Babka 	/*
6599fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
6609fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
6619fcd6d2eSVlastimil Babka 	 */
6629fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
6639fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
6649fcd6d2eSVlastimil Babka 
665e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
666e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
667e34d85f0SJoonsoo Kim 
668e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
669e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
670e14c720eSVlastimil Babka 
671f40d1e42SMel Gorman 	/*
672f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
673f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
674f40d1e42SMel Gorman 	 * returned and CMA will fail.
675f40d1e42SMel Gorman 	 */
6762af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
677f40d1e42SMel Gorman 		total_isolated = 0;
678f40d1e42SMel Gorman 
6797f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
680397487dbSMel Gorman 	if (total_isolated)
681010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
682748446bbSMel Gorman 	return total_isolated;
683748446bbSMel Gorman }
684748446bbSMel Gorman 
68585aa125fSMichal Nazarewicz /**
68685aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
687e8b098fcSMike Rapoport  * @cc:        Compaction control structure.
68885aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
68985aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
69085aa125fSMichal Nazarewicz  *
69185aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
69285aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
69385aa125fSMichal Nazarewicz  * undo its actions and return zero.
69485aa125fSMichal Nazarewicz  *
69585aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
69685aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
69785aa125fSMichal Nazarewicz  * a free page).
69885aa125fSMichal Nazarewicz  */
699ff9543fdSMichal Nazarewicz unsigned long
700bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
701bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
70285aa125fSMichal Nazarewicz {
703e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
70485aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
70585aa125fSMichal Nazarewicz 
7067d49d886SVlastimil Babka 	pfn = start_pfn;
70706b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
708e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
709e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
71006b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
7117d49d886SVlastimil Babka 
7127d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
713e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
7147d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
715e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
716e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
7177d49d886SVlastimil Babka 
71885aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
71985aa125fSMichal Nazarewicz 
72058420016SJoonsoo Kim 		/*
72158420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
72258420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
72358420016SJoonsoo Kim 		 * scanning range to right one.
72458420016SJoonsoo Kim 		 */
72558420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
72606b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
72706b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
72858420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
72958420016SJoonsoo Kim 		}
73058420016SJoonsoo Kim 
731e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
732e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
7337d49d886SVlastimil Babka 			break;
7347d49d886SVlastimil Babka 
735e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
7364fca9730SMel Gorman 					block_end_pfn, &freelist, 0, true);
73785aa125fSMichal Nazarewicz 
73885aa125fSMichal Nazarewicz 		/*
73985aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
74085aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
74185aa125fSMichal Nazarewicz 		 * non-free pages).
74285aa125fSMichal Nazarewicz 		 */
74385aa125fSMichal Nazarewicz 		if (!isolated)
74485aa125fSMichal Nazarewicz 			break;
74585aa125fSMichal Nazarewicz 
74685aa125fSMichal Nazarewicz 		/*
74785aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
74885aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
74985aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
75085aa125fSMichal Nazarewicz 		 */
75185aa125fSMichal Nazarewicz 	}
75285aa125fSMichal Nazarewicz 
75366c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
7544469ab98SMel Gorman 	split_map_pages(&freelist);
75585aa125fSMichal Nazarewicz 
75685aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
75785aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
75885aa125fSMichal Nazarewicz 		release_freepages(&freelist);
75985aa125fSMichal Nazarewicz 		return 0;
76085aa125fSMichal Nazarewicz 	}
76185aa125fSMichal Nazarewicz 
76285aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
76385aa125fSMichal Nazarewicz 	return pfn;
76485aa125fSMichal Nazarewicz }
76585aa125fSMichal Nazarewicz 
766748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
7675f438eeeSAndrey Ryabinin static bool too_many_isolated(pg_data_t *pgdat)
768748446bbSMel Gorman {
769bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
770748446bbSMel Gorman 
7715f438eeeSAndrey Ryabinin 	inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
7725f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_INACTIVE_ANON);
7735f438eeeSAndrey Ryabinin 	active = node_page_state(pgdat, NR_ACTIVE_FILE) +
7745f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ACTIVE_ANON);
7755f438eeeSAndrey Ryabinin 	isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
7765f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ISOLATED_ANON);
777748446bbSMel Gorman 
778bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
779748446bbSMel Gorman }
780748446bbSMel Gorman 
7812fe86e00SMichal Nazarewicz /**
782edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
783edc2ca61SVlastimil Babka  *				  a single pageblock
7842fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
785edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
786edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
787edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
7882fe86e00SMichal Nazarewicz  *
7892fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
790edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
791edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
792edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
793edc2ca61SVlastimil Babka  * than end_pfn).
7942fe86e00SMichal Nazarewicz  *
795edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
796edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
797edc2ca61SVlastimil Babka  * is neither read nor updated.
798748446bbSMel Gorman  */
799edc2ca61SVlastimil Babka static unsigned long
800edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
801edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
802748446bbSMel Gorman {
8035f438eeeSAndrey Ryabinin 	pg_data_t *pgdat = cc->zone->zone_pgdat;
804b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
805fa9add64SHugh Dickins 	struct lruvec *lruvec;
806b8b2d825SXiubo Li 	unsigned long flags = 0;
8076168d0daSAlex Shi 	struct lruvec *locked = NULL;
808bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
809e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
810fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
811fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
812e380bebeSMel Gorman 	bool skip_updated = false;
813748446bbSMel Gorman 
814748446bbSMel Gorman 	/*
815748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
816748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
817748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
818748446bbSMel Gorman 	 */
8195f438eeeSAndrey Ryabinin 	while (unlikely(too_many_isolated(pgdat))) {
820d20bdd57SZi Yan 		/* stop isolation if there are still pages not migrated */
821d20bdd57SZi Yan 		if (cc->nr_migratepages)
822d20bdd57SZi Yan 			return 0;
823d20bdd57SZi Yan 
824f9e35b3bSMel Gorman 		/* async migration should just abort */
825e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
8262fe86e00SMichal Nazarewicz 			return 0;
827f9e35b3bSMel Gorman 
828748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
829748446bbSMel Gorman 
830748446bbSMel Gorman 		if (fatal_signal_pending(current))
8312fe86e00SMichal Nazarewicz 			return 0;
832748446bbSMel Gorman 	}
833748446bbSMel Gorman 
834cf66f070SMel Gorman 	cond_resched();
835aeef4b83SDavid Rientjes 
836fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
837fdd048e1SVlastimil Babka 		skip_on_failure = true;
838fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
839fdd048e1SVlastimil Babka 	}
840fdd048e1SVlastimil Babka 
841748446bbSMel Gorman 	/* Time to isolate some pages for migration */
842748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
84329c0dde8SVlastimil Babka 
844fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
845fdd048e1SVlastimil Babka 			/*
846fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
847fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
848fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
849fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
850fdd048e1SVlastimil Babka 			 */
851fdd048e1SVlastimil Babka 			if (nr_isolated)
852fdd048e1SVlastimil Babka 				break;
853fdd048e1SVlastimil Babka 
854fdd048e1SVlastimil Babka 			/*
855fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
856fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
857fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
858fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
859fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
860fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
861fdd048e1SVlastimil Babka 			 * previous loop iteration.
862fdd048e1SVlastimil Babka 			 */
863fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
864fdd048e1SVlastimil Babka 		}
865fdd048e1SVlastimil Babka 
8668b44d279SVlastimil Babka 		/*
8678b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
868670105a2SMel Gorman 		 * contention, to give chance to IRQs. Abort completely if
869670105a2SMel Gorman 		 * a fatal signal is pending.
8708b44d279SVlastimil Babka 		 */
8716168d0daSAlex Shi 		if (!(low_pfn % SWAP_CLUSTER_MAX)) {
8726168d0daSAlex Shi 			if (locked) {
8736168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(locked, flags);
8746168d0daSAlex Shi 				locked = NULL;
8756168d0daSAlex Shi 			}
8766168d0daSAlex Shi 
8776168d0daSAlex Shi 			if (fatal_signal_pending(current)) {
8786168d0daSAlex Shi 				cc->contended = true;
8796168d0daSAlex Shi 
880670105a2SMel Gorman 				low_pfn = 0;
881670105a2SMel Gorman 				goto fatal_pending;
882670105a2SMel Gorman 			}
883b2eef8c0SAndrea Arcangeli 
8846168d0daSAlex Shi 			cond_resched();
8856168d0daSAlex Shi 		}
8866168d0daSAlex Shi 
887748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
888fdd048e1SVlastimil Babka 			goto isolate_fail;
889b7aba698SMel Gorman 		nr_scanned++;
890748446bbSMel Gorman 
891748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
892dc908600SMel Gorman 
893e380bebeSMel Gorman 		/*
894e380bebeSMel Gorman 		 * Check if the pageblock has already been marked skipped.
895e380bebeSMel Gorman 		 * Only the aligned PFN is checked as the caller isolates
896e380bebeSMel Gorman 		 * COMPACT_CLUSTER_MAX at a time so the second call must
897e380bebeSMel Gorman 		 * not falsely conclude that the block should be skipped.
898e380bebeSMel Gorman 		 */
899e380bebeSMel Gorman 		if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
900e380bebeSMel Gorman 			if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
901e380bebeSMel Gorman 				low_pfn = end_pfn;
9029df41314SAlex Shi 				page = NULL;
903e380bebeSMel Gorman 				goto isolate_abort;
904e380bebeSMel Gorman 			}
905bb13ffebSMel Gorman 			valid_page = page;
906e380bebeSMel Gorman 		}
907bb13ffebSMel Gorman 
908c122b208SJoonsoo Kim 		/*
90999c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
91099c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
91199c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
91299c0fd5eSVlastimil Babka 		 * potential isolation targets.
9136c14466cSMel Gorman 		 */
91499c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
915ab130f91SMatthew Wilcox (Oracle) 			unsigned long freepage_order = buddy_order_unsafe(page);
91699c0fd5eSVlastimil Babka 
91799c0fd5eSVlastimil Babka 			/*
91899c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
91999c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
92099c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
92199c0fd5eSVlastimil Babka 			 */
92299c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
92399c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
924748446bbSMel Gorman 			continue;
92599c0fd5eSVlastimil Babka 		}
926748446bbSMel Gorman 
9279927af74SMel Gorman 		/*
92829c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
9291da2f328SRik van Riel 		 * hugetlbfs are not to be compacted unless we are attempting
9301da2f328SRik van Riel 		 * an allocation much larger than the huge page size (eg CMA).
9311da2f328SRik van Riel 		 * We can potentially save a lot of iterations if we skip them
9321da2f328SRik van Riel 		 * at once. The check is racy, but we can consider only valid
9331da2f328SRik van Riel 		 * values and the only danger is skipping too much.
934bc835011SAndrea Arcangeli 		 */
9351da2f328SRik van Riel 		if (PageCompound(page) && !cc->alloc_contig) {
93621dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
93729c0dde8SVlastimil Babka 
938d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER))
93921dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
940fdd048e1SVlastimil Babka 			goto isolate_fail;
9412a1402aaSMel Gorman 		}
9422a1402aaSMel Gorman 
943bda807d4SMinchan Kim 		/*
944bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
945bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
946bda807d4SMinchan Kim 		 * Skip any other type of page
947bda807d4SMinchan Kim 		 */
948bda807d4SMinchan Kim 		if (!PageLRU(page)) {
949bda807d4SMinchan Kim 			/*
950bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
951bda807d4SMinchan Kim 			 * to verify it under page_lock.
952bda807d4SMinchan Kim 			 */
953bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
954bda807d4SMinchan Kim 					!PageIsolated(page)) {
955bda807d4SMinchan Kim 				if (locked) {
9566168d0daSAlex Shi 					unlock_page_lruvec_irqrestore(locked, flags);
9576168d0daSAlex Shi 					locked = NULL;
958bda807d4SMinchan Kim 				}
959bda807d4SMinchan Kim 
9609e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
961bda807d4SMinchan Kim 					goto isolate_success;
962bda807d4SMinchan Kim 			}
963bda807d4SMinchan Kim 
964fdd048e1SVlastimil Babka 			goto isolate_fail;
965bda807d4SMinchan Kim 		}
96629c0dde8SVlastimil Babka 
967119d6d59SDavid Rientjes 		/*
968119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
969119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
970119d6d59SDavid Rientjes 		 * admittedly racy check.
971119d6d59SDavid Rientjes 		 */
972119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
973119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
974fdd048e1SVlastimil Babka 			goto isolate_fail;
975119d6d59SDavid Rientjes 
97673e64c51SMichal Hocko 		/*
97773e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
97873e64c51SMichal Hocko 		 * because those do not depend on fs locks.
97973e64c51SMichal Hocko 		 */
98073e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
98173e64c51SMichal Hocko 			goto isolate_fail;
98273e64c51SMichal Hocko 
9839df41314SAlex Shi 		/*
9849df41314SAlex Shi 		 * Be careful not to clear PageLRU until after we're
9859df41314SAlex Shi 		 * sure the page is not being freed elsewhere -- the
9869df41314SAlex Shi 		 * page release code relies on it.
9879df41314SAlex Shi 		 */
9889df41314SAlex Shi 		if (unlikely(!get_page_unless_zero(page)))
9899df41314SAlex Shi 			goto isolate_fail;
9909df41314SAlex Shi 
991c2135f7cSAlex Shi 		if (!__isolate_lru_page_prepare(page, isolate_mode))
9929df41314SAlex Shi 			goto isolate_fail_put;
9939df41314SAlex Shi 
9949df41314SAlex Shi 		/* Try isolate the page */
9959df41314SAlex Shi 		if (!TestClearPageLRU(page))
9969df41314SAlex Shi 			goto isolate_fail_put;
9979df41314SAlex Shi 
9986168d0daSAlex Shi 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
9996168d0daSAlex Shi 
100069b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
10016168d0daSAlex Shi 		if (lruvec != locked) {
10026168d0daSAlex Shi 			if (locked)
10036168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(locked, flags);
10046168d0daSAlex Shi 
10056168d0daSAlex Shi 			compact_lock_irqsave(&lruvec->lru_lock, &flags, cc);
10066168d0daSAlex Shi 			locked = lruvec;
10076168d0daSAlex Shi 
10086168d0daSAlex Shi 			lruvec_memcg_debug(lruvec, page);
1009e380bebeSMel Gorman 
1010e380bebeSMel Gorman 			/* Try get exclusive access under lock */
1011e380bebeSMel Gorman 			if (!skip_updated) {
1012e380bebeSMel Gorman 				skip_updated = true;
1013e380bebeSMel Gorman 				if (test_and_set_skip(cc, page, low_pfn))
1014e380bebeSMel Gorman 					goto isolate_abort;
1015e380bebeSMel Gorman 			}
10162a1402aaSMel Gorman 
101729c0dde8SVlastimil Babka 			/*
101829c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
101929c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
102029c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
102129c0dde8SVlastimil Babka 			 */
10221da2f328SRik van Riel 			if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
1023d8c6546bSMatthew Wilcox (Oracle) 				low_pfn += compound_nr(page) - 1;
10249df41314SAlex Shi 				SetPageLRU(page);
10259df41314SAlex Shi 				goto isolate_fail_put;
1026bc835011SAndrea Arcangeli 			}
1027*d99fd5feSAlex Shi 		}
1028fa9add64SHugh Dickins 
10291da2f328SRik van Riel 		/* The whole page is taken off the LRU; skip the tail pages. */
10301da2f328SRik van Riel 		if (PageCompound(page))
10311da2f328SRik van Riel 			low_pfn += compound_nr(page) - 1;
1032bc835011SAndrea Arcangeli 
1033748446bbSMel Gorman 		/* Successfully isolated */
103446ae6b2cSYu Zhao 		del_page_from_lru_list(page, lruvec);
10351da2f328SRik van Riel 		mod_node_page_state(page_pgdat(page),
10369de4f22aSHuang Ying 				NR_ISOLATED_ANON + page_is_file_lru(page),
10376c357848SMatthew Wilcox (Oracle) 				thp_nr_pages(page));
1038b6c75016SJoonsoo Kim 
1039b6c75016SJoonsoo Kim isolate_success:
1040fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
104138935861SZi Yan 		cc->nr_migratepages += compound_nr(page);
104238935861SZi Yan 		nr_isolated += compound_nr(page);
1043748446bbSMel Gorman 
1044804d3121SMel Gorman 		/*
1045804d3121SMel Gorman 		 * Avoid isolating too much unless this block is being
1046cb2dcaf0SMel Gorman 		 * rescanned (e.g. dirty/writeback pages, parallel allocation)
1047cb2dcaf0SMel Gorman 		 * or a lock is contended. For contention, isolate quickly to
1048cb2dcaf0SMel Gorman 		 * potentially remove one source of contention.
1049804d3121SMel Gorman 		 */
105038935861SZi Yan 		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
1051cb2dcaf0SMel Gorman 		    !cc->rescan && !cc->contended) {
105231b8384aSHillf Danton 			++low_pfn;
1053748446bbSMel Gorman 			break;
1054748446bbSMel Gorman 		}
1055fdd048e1SVlastimil Babka 
1056fdd048e1SVlastimil Babka 		continue;
10579df41314SAlex Shi 
10589df41314SAlex Shi isolate_fail_put:
10599df41314SAlex Shi 		/* Avoid potential deadlock in freeing page under lru_lock */
10609df41314SAlex Shi 		if (locked) {
10616168d0daSAlex Shi 			unlock_page_lruvec_irqrestore(locked, flags);
10626168d0daSAlex Shi 			locked = NULL;
10639df41314SAlex Shi 		}
10649df41314SAlex Shi 		put_page(page);
10659df41314SAlex Shi 
1066fdd048e1SVlastimil Babka isolate_fail:
1067fdd048e1SVlastimil Babka 		if (!skip_on_failure)
1068fdd048e1SVlastimil Babka 			continue;
1069fdd048e1SVlastimil Babka 
1070fdd048e1SVlastimil Babka 		/*
1071fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
1072fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
1073fdd048e1SVlastimil Babka 		 * page anyway.
1074fdd048e1SVlastimil Babka 		 */
1075fdd048e1SVlastimil Babka 		if (nr_isolated) {
1076fdd048e1SVlastimil Babka 			if (locked) {
10776168d0daSAlex Shi 				unlock_page_lruvec_irqrestore(locked, flags);
10786168d0daSAlex Shi 				locked = NULL;
1079fdd048e1SVlastimil Babka 			}
1080fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
1081fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
1082fdd048e1SVlastimil Babka 			nr_isolated = 0;
1083fdd048e1SVlastimil Babka 		}
1084fdd048e1SVlastimil Babka 
1085fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
1086fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
1087fdd048e1SVlastimil Babka 			/*
1088fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
1089fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
1090fdd048e1SVlastimil Babka 			 */
1091fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
1092fdd048e1SVlastimil Babka 		}
109331b8384aSHillf Danton 	}
1094748446bbSMel Gorman 
109599c0fd5eSVlastimil Babka 	/*
109699c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
109799c0fd5eSVlastimil Babka 	 * the range to be scanned.
109899c0fd5eSVlastimil Babka 	 */
109999c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
110099c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
110199c0fd5eSVlastimil Babka 
11029df41314SAlex Shi 	page = NULL;
11039df41314SAlex Shi 
1104e380bebeSMel Gorman isolate_abort:
1105c67fe375SMel Gorman 	if (locked)
11066168d0daSAlex Shi 		unlock_page_lruvec_irqrestore(locked, flags);
11079df41314SAlex Shi 	if (page) {
11089df41314SAlex Shi 		SetPageLRU(page);
11099df41314SAlex Shi 		put_page(page);
11109df41314SAlex Shi 	}
1111748446bbSMel Gorman 
111250b5b094SVlastimil Babka 	/*
1113804d3121SMel Gorman 	 * Updated the cached scanner pfn once the pageblock has been scanned
1114804d3121SMel Gorman 	 * Pages will either be migrated in which case there is no point
1115804d3121SMel Gorman 	 * scanning in the near future or migration failed in which case the
1116804d3121SMel Gorman 	 * failure reason may persist. The block is marked for skipping if
1117804d3121SMel Gorman 	 * there were no pages isolated in the block or if the block is
1118804d3121SMel Gorman 	 * rescanned twice in a row.
111950b5b094SVlastimil Babka 	 */
1120804d3121SMel Gorman 	if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
1121e380bebeSMel Gorman 		if (valid_page && !skip_updated)
1122e380bebeSMel Gorman 			set_pageblock_skip(valid_page);
1123e380bebeSMel Gorman 		update_cached_migrate(cc, low_pfn);
1124e380bebeSMel Gorman 	}
1125bb13ffebSMel Gorman 
1126e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1127e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
1128b7aba698SMel Gorman 
1129670105a2SMel Gorman fatal_pending:
11307f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
1131397487dbSMel Gorman 	if (nr_isolated)
1132010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
1133397487dbSMel Gorman 
11342fe86e00SMichal Nazarewicz 	return low_pfn;
11352fe86e00SMichal Nazarewicz }
11362fe86e00SMichal Nazarewicz 
1137edc2ca61SVlastimil Babka /**
1138edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1139edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
1140edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
1141edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
1142edc2ca61SVlastimil Babka  *
1143edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
1144edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
1145edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
1146edc2ca61SVlastimil Babka  */
1147edc2ca61SVlastimil Babka unsigned long
1148edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1149edc2ca61SVlastimil Babka 							unsigned long end_pfn)
1150edc2ca61SVlastimil Babka {
1151e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
1152edc2ca61SVlastimil Babka 
1153edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
1154edc2ca61SVlastimil Babka 	pfn = start_pfn;
115506b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
1156e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
1157e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
115806b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
1159edc2ca61SVlastimil Babka 
1160edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
1161e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
1162edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
1163edc2ca61SVlastimil Babka 
1164edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
1165edc2ca61SVlastimil Babka 
1166e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
1167e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
1168edc2ca61SVlastimil Babka 			continue;
1169edc2ca61SVlastimil Babka 
1170edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1171edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
1172edc2ca61SVlastimil Babka 
117314af4a5eSHugh Dickins 		if (!pfn)
1174edc2ca61SVlastimil Babka 			break;
11756ea41c0cSJoonsoo Kim 
117638935861SZi Yan 		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
11776ea41c0cSJoonsoo Kim 			break;
1178edc2ca61SVlastimil Babka 	}
1179edc2ca61SVlastimil Babka 
1180edc2ca61SVlastimil Babka 	return pfn;
1181edc2ca61SVlastimil Babka }
1182edc2ca61SVlastimil Babka 
1183ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1184ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1185018e9a49SAndrew Morton 
1186b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1187b682debdSVlastimil Babka 							struct page *page)
1188b682debdSVlastimil Babka {
1189282722b0SVlastimil Babka 	int block_mt;
1190282722b0SVlastimil Babka 
11919bebefd5SMel Gorman 	if (pageblock_skip_persistent(page))
11929bebefd5SMel Gorman 		return false;
11939bebefd5SMel Gorman 
1194282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1195b682debdSVlastimil Babka 		return true;
1196b682debdSVlastimil Babka 
1197282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1198282722b0SVlastimil Babka 
1199282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1200282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1201282722b0SVlastimil Babka 	else
1202282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1203b682debdSVlastimil Babka }
1204b682debdSVlastimil Babka 
1205018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
12069f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
12079f7e3387SVlastimil Babka 							struct page *page)
1208018e9a49SAndrew Morton {
1209018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1210018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1211018e9a49SAndrew Morton 		/*
1212018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1213018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1214018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1215018e9a49SAndrew Morton 		 */
1216ab130f91SMatthew Wilcox (Oracle) 		if (buddy_order_unsafe(page) >= pageblock_order)
1217018e9a49SAndrew Morton 			return false;
1218018e9a49SAndrew Morton 	}
1219018e9a49SAndrew Morton 
12201ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
12211ef36db2SYisheng Xie 		return true;
12221ef36db2SYisheng Xie 
1223018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1224b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1225018e9a49SAndrew Morton 		return true;
1226018e9a49SAndrew Morton 
1227018e9a49SAndrew Morton 	/* Otherwise skip the block */
1228018e9a49SAndrew Morton 	return false;
1229018e9a49SAndrew Morton }
1230018e9a49SAndrew Morton 
123170b44595SMel Gorman static inline unsigned int
123270b44595SMel Gorman freelist_scan_limit(struct compact_control *cc)
123370b44595SMel Gorman {
1234dd7ef7bdSQian Cai 	unsigned short shift = BITS_PER_LONG - 1;
1235dd7ef7bdSQian Cai 
1236dd7ef7bdSQian Cai 	return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
123770b44595SMel Gorman }
123870b44595SMel Gorman 
1239ff9543fdSMichal Nazarewicz /*
1240f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1241f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1242f2849aa0SVlastimil Babka  */
1243f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1244f2849aa0SVlastimil Babka {
1245f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1246f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1247f2849aa0SVlastimil Babka }
1248f2849aa0SVlastimil Babka 
12495a811889SMel Gorman /*
12505a811889SMel Gorman  * Used when scanning for a suitable migration target which scans freelists
12515a811889SMel Gorman  * in reverse. Reorders the list such as the unscanned pages are scanned
12525a811889SMel Gorman  * first on the next iteration of the free scanner
12535a811889SMel Gorman  */
12545a811889SMel Gorman static void
12555a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage)
12565a811889SMel Gorman {
12575a811889SMel Gorman 	LIST_HEAD(sublist);
12585a811889SMel Gorman 
12595a811889SMel Gorman 	if (!list_is_last(freelist, &freepage->lru)) {
12605a811889SMel Gorman 		list_cut_before(&sublist, freelist, &freepage->lru);
12615a811889SMel Gorman 		if (!list_empty(&sublist))
12625a811889SMel Gorman 			list_splice_tail(&sublist, freelist);
12635a811889SMel Gorman 	}
12645a811889SMel Gorman }
12655a811889SMel Gorman 
12665a811889SMel Gorman /*
12675a811889SMel Gorman  * Similar to move_freelist_head except used by the migration scanner
12685a811889SMel Gorman  * when scanning forward. It's possible for these list operations to
12695a811889SMel Gorman  * move against each other if they search the free list exactly in
12705a811889SMel Gorman  * lockstep.
12715a811889SMel Gorman  */
127270b44595SMel Gorman static void
127370b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage)
127470b44595SMel Gorman {
127570b44595SMel Gorman 	LIST_HEAD(sublist);
127670b44595SMel Gorman 
127770b44595SMel Gorman 	if (!list_is_first(freelist, &freepage->lru)) {
127870b44595SMel Gorman 		list_cut_position(&sublist, freelist, &freepage->lru);
127970b44595SMel Gorman 		if (!list_empty(&sublist))
128070b44595SMel Gorman 			list_splice_tail(&sublist, freelist);
128170b44595SMel Gorman 	}
128270b44595SMel Gorman }
128370b44595SMel Gorman 
12845a811889SMel Gorman static void
12855a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
12865a811889SMel Gorman {
12875a811889SMel Gorman 	unsigned long start_pfn, end_pfn;
12885a811889SMel Gorman 	struct page *page = pfn_to_page(pfn);
12895a811889SMel Gorman 
12905a811889SMel Gorman 	/* Do not search around if there are enough pages already */
12915a811889SMel Gorman 	if (cc->nr_freepages >= cc->nr_migratepages)
12925a811889SMel Gorman 		return;
12935a811889SMel Gorman 
12945a811889SMel Gorman 	/* Minimise scanning during async compaction */
12955a811889SMel Gorman 	if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
12965a811889SMel Gorman 		return;
12975a811889SMel Gorman 
12985a811889SMel Gorman 	/* Pageblock boundaries */
12995a811889SMel Gorman 	start_pfn = pageblock_start_pfn(pfn);
130060fce36aSMel Gorman 	end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)) - 1;
13015a811889SMel Gorman 
13025a811889SMel Gorman 	/* Scan before */
13035a811889SMel Gorman 	if (start_pfn != pfn) {
13044fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false);
13055a811889SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages)
13065a811889SMel Gorman 			return;
13075a811889SMel Gorman 	}
13085a811889SMel Gorman 
13095a811889SMel Gorman 	/* Scan after */
13105a811889SMel Gorman 	start_pfn = pfn + nr_isolated;
131160fce36aSMel Gorman 	if (start_pfn < end_pfn)
13124fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
13135a811889SMel Gorman 
13145a811889SMel Gorman 	/* Skip this pageblock in the future as it's full or nearly full */
13155a811889SMel Gorman 	if (cc->nr_freepages < cc->nr_migratepages)
13165a811889SMel Gorman 		set_pageblock_skip(page);
13175a811889SMel Gorman }
13185a811889SMel Gorman 
1319dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */
1320dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order)
1321dbe2d4e4SMel Gorman {
1322dbe2d4e4SMel Gorman 	order--;
1323dbe2d4e4SMel Gorman 	if (order < 0)
1324dbe2d4e4SMel Gorman 		order = cc->order - 1;
1325dbe2d4e4SMel Gorman 
1326dbe2d4e4SMel Gorman 	/* Search wrapped around? */
1327dbe2d4e4SMel Gorman 	if (order == cc->search_order) {
1328dbe2d4e4SMel Gorman 		cc->search_order--;
1329dbe2d4e4SMel Gorman 		if (cc->search_order < 0)
1330dbe2d4e4SMel Gorman 			cc->search_order = cc->order - 1;
1331dbe2d4e4SMel Gorman 		return -1;
1332dbe2d4e4SMel Gorman 	}
1333dbe2d4e4SMel Gorman 
1334dbe2d4e4SMel Gorman 	return order;
1335dbe2d4e4SMel Gorman }
1336dbe2d4e4SMel Gorman 
13375a811889SMel Gorman static unsigned long
13385a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc)
13395a811889SMel Gorman {
13405a811889SMel Gorman 	unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1);
13415a811889SMel Gorman 	unsigned int nr_scanned = 0;
134274e21484SRokudo Yan 	unsigned long low_pfn, min_pfn, highest = 0;
13435a811889SMel Gorman 	unsigned long nr_isolated = 0;
13445a811889SMel Gorman 	unsigned long distance;
13455a811889SMel Gorman 	struct page *page = NULL;
13465a811889SMel Gorman 	bool scan_start = false;
13475a811889SMel Gorman 	int order;
13485a811889SMel Gorman 
13495a811889SMel Gorman 	/* Full compaction passes in a negative order */
13505a811889SMel Gorman 	if (cc->order <= 0)
13515a811889SMel Gorman 		return cc->free_pfn;
13525a811889SMel Gorman 
13535a811889SMel Gorman 	/*
13545a811889SMel Gorman 	 * If starting the scan, use a deeper search and use the highest
13555a811889SMel Gorman 	 * PFN found if a suitable one is not found.
13565a811889SMel Gorman 	 */
1357e332f741SMel Gorman 	if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
13585a811889SMel Gorman 		limit = pageblock_nr_pages >> 1;
13595a811889SMel Gorman 		scan_start = true;
13605a811889SMel Gorman 	}
13615a811889SMel Gorman 
13625a811889SMel Gorman 	/*
13635a811889SMel Gorman 	 * Preferred point is in the top quarter of the scan space but take
13645a811889SMel Gorman 	 * a pfn from the top half if the search is problematic.
13655a811889SMel Gorman 	 */
13665a811889SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn);
13675a811889SMel Gorman 	low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
13685a811889SMel Gorman 	min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
13695a811889SMel Gorman 
13705a811889SMel Gorman 	if (WARN_ON_ONCE(min_pfn > low_pfn))
13715a811889SMel Gorman 		low_pfn = min_pfn;
13725a811889SMel Gorman 
1373dbe2d4e4SMel Gorman 	/*
1374dbe2d4e4SMel Gorman 	 * Search starts from the last successful isolation order or the next
1375dbe2d4e4SMel Gorman 	 * order to search after a previous failure
1376dbe2d4e4SMel Gorman 	 */
1377dbe2d4e4SMel Gorman 	cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1378dbe2d4e4SMel Gorman 
1379dbe2d4e4SMel Gorman 	for (order = cc->search_order;
1380dbe2d4e4SMel Gorman 	     !page && order >= 0;
1381dbe2d4e4SMel Gorman 	     order = next_search_order(cc, order)) {
13825a811889SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
13835a811889SMel Gorman 		struct list_head *freelist;
13845a811889SMel Gorman 		struct page *freepage;
13855a811889SMel Gorman 		unsigned long flags;
13865a811889SMel Gorman 		unsigned int order_scanned = 0;
138774e21484SRokudo Yan 		unsigned long high_pfn = 0;
13885a811889SMel Gorman 
13895a811889SMel Gorman 		if (!area->nr_free)
13905a811889SMel Gorman 			continue;
13915a811889SMel Gorman 
13925a811889SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
13935a811889SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
13945a811889SMel Gorman 		list_for_each_entry_reverse(freepage, freelist, lru) {
13955a811889SMel Gorman 			unsigned long pfn;
13965a811889SMel Gorman 
13975a811889SMel Gorman 			order_scanned++;
13985a811889SMel Gorman 			nr_scanned++;
13995a811889SMel Gorman 			pfn = page_to_pfn(freepage);
14005a811889SMel Gorman 
14015a811889SMel Gorman 			if (pfn >= highest)
14025a811889SMel Gorman 				highest = pageblock_start_pfn(pfn);
14035a811889SMel Gorman 
14045a811889SMel Gorman 			if (pfn >= low_pfn) {
14055a811889SMel Gorman 				cc->fast_search_fail = 0;
1406dbe2d4e4SMel Gorman 				cc->search_order = order;
14075a811889SMel Gorman 				page = freepage;
14085a811889SMel Gorman 				break;
14095a811889SMel Gorman 			}
14105a811889SMel Gorman 
14115a811889SMel Gorman 			if (pfn >= min_pfn && pfn > high_pfn) {
14125a811889SMel Gorman 				high_pfn = pfn;
14135a811889SMel Gorman 
14145a811889SMel Gorman 				/* Shorten the scan if a candidate is found */
14155a811889SMel Gorman 				limit >>= 1;
14165a811889SMel Gorman 			}
14175a811889SMel Gorman 
14185a811889SMel Gorman 			if (order_scanned >= limit)
14195a811889SMel Gorman 				break;
14205a811889SMel Gorman 		}
14215a811889SMel Gorman 
14225a811889SMel Gorman 		/* Use a minimum pfn if a preferred one was not found */
14235a811889SMel Gorman 		if (!page && high_pfn) {
14245a811889SMel Gorman 			page = pfn_to_page(high_pfn);
14255a811889SMel Gorman 
14265a811889SMel Gorman 			/* Update freepage for the list reorder below */
14275a811889SMel Gorman 			freepage = page;
14285a811889SMel Gorman 		}
14295a811889SMel Gorman 
14305a811889SMel Gorman 		/* Reorder to so a future search skips recent pages */
14315a811889SMel Gorman 		move_freelist_head(freelist, freepage);
14325a811889SMel Gorman 
14335a811889SMel Gorman 		/* Isolate the page if available */
14345a811889SMel Gorman 		if (page) {
14355a811889SMel Gorman 			if (__isolate_free_page(page, order)) {
14365a811889SMel Gorman 				set_page_private(page, order);
14375a811889SMel Gorman 				nr_isolated = 1 << order;
14385a811889SMel Gorman 				cc->nr_freepages += nr_isolated;
14395a811889SMel Gorman 				list_add_tail(&page->lru, &cc->freepages);
14405a811889SMel Gorman 				count_compact_events(COMPACTISOLATED, nr_isolated);
14415a811889SMel Gorman 			} else {
14425a811889SMel Gorman 				/* If isolation fails, abort the search */
14435b56d996SQian Cai 				order = cc->search_order + 1;
14445a811889SMel Gorman 				page = NULL;
14455a811889SMel Gorman 			}
14465a811889SMel Gorman 		}
14475a811889SMel Gorman 
14485a811889SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
14495a811889SMel Gorman 
14505a811889SMel Gorman 		/*
14515a811889SMel Gorman 		 * Smaller scan on next order so the total scan ig related
14525a811889SMel Gorman 		 * to freelist_scan_limit.
14535a811889SMel Gorman 		 */
14545a811889SMel Gorman 		if (order_scanned >= limit)
14555a811889SMel Gorman 			limit = min(1U, limit >> 1);
14565a811889SMel Gorman 	}
14575a811889SMel Gorman 
14585a811889SMel Gorman 	if (!page) {
14595a811889SMel Gorman 		cc->fast_search_fail++;
14605a811889SMel Gorman 		if (scan_start) {
14615a811889SMel Gorman 			/*
14625a811889SMel Gorman 			 * Use the highest PFN found above min. If one was
1463f3867755SEthon Paul 			 * not found, be pessimistic for direct compaction
14645a811889SMel Gorman 			 * and use the min mark.
14655a811889SMel Gorman 			 */
14665a811889SMel Gorman 			if (highest) {
14675a811889SMel Gorman 				page = pfn_to_page(highest);
14685a811889SMel Gorman 				cc->free_pfn = highest;
14695a811889SMel Gorman 			} else {
1470e577c8b6SSuzuki K Poulose 				if (cc->direct_compaction && pfn_valid(min_pfn)) {
147173a6e474SBaoquan He 					page = pageblock_pfn_to_page(min_pfn,
147273a6e474SBaoquan He 						pageblock_end_pfn(min_pfn),
147373a6e474SBaoquan He 						cc->zone);
14745a811889SMel Gorman 					cc->free_pfn = min_pfn;
14755a811889SMel Gorman 				}
14765a811889SMel Gorman 			}
14775a811889SMel Gorman 		}
14785a811889SMel Gorman 	}
14795a811889SMel Gorman 
1480d097a6f6SMel Gorman 	if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1481d097a6f6SMel Gorman 		highest -= pageblock_nr_pages;
14825a811889SMel Gorman 		cc->zone->compact_cached_free_pfn = highest;
1483d097a6f6SMel Gorman 	}
14845a811889SMel Gorman 
14855a811889SMel Gorman 	cc->total_free_scanned += nr_scanned;
14865a811889SMel Gorman 	if (!page)
14875a811889SMel Gorman 		return cc->free_pfn;
14885a811889SMel Gorman 
14895a811889SMel Gorman 	low_pfn = page_to_pfn(page);
14905a811889SMel Gorman 	fast_isolate_around(cc, low_pfn, nr_isolated);
14915a811889SMel Gorman 	return low_pfn;
14925a811889SMel Gorman }
14935a811889SMel Gorman 
1494f2849aa0SVlastimil Babka /*
1495ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1496ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1497ff9543fdSMichal Nazarewicz  */
1498edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1499ff9543fdSMichal Nazarewicz {
1500edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1501ff9543fdSMichal Nazarewicz 	struct page *page;
1502c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1503e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1504c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1505c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1506ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
15074fca9730SMel Gorman 	unsigned int stride;
15082fe86e00SMichal Nazarewicz 
15095a811889SMel Gorman 	/* Try a small search of the free lists for a candidate */
15105a811889SMel Gorman 	isolate_start_pfn = fast_isolate_freepages(cc);
15115a811889SMel Gorman 	if (cc->nr_freepages)
15125a811889SMel Gorman 		goto splitmap;
15135a811889SMel Gorman 
1514ff9543fdSMichal Nazarewicz 	/*
1515ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
151649e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1517e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1518e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1519c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1520c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1521a1c1dbebSRandy Dunlap 	 * zone which ends in the middle of a pageblock.
152249e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
152349e068f0SVlastimil Babka 	 * is using.
1524ff9543fdSMichal Nazarewicz 	 */
1525e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
15265a811889SMel Gorman 	block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
1527c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1528c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
152906b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
15304fca9730SMel Gorman 	stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
15312fe86e00SMichal Nazarewicz 
1532ff9543fdSMichal Nazarewicz 	/*
1533ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1534ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1535ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1536ff9543fdSMichal Nazarewicz 	 */
1537f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1538c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1539e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1540e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
15414fca9730SMel Gorman 		unsigned long nr_isolated;
15424fca9730SMel Gorman 
1543f6ea3adbSDavid Rientjes 		/*
1544f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1545cb810ad2SMel Gorman 		 * suitable migration targets, so periodically check resched.
1546f6ea3adbSDavid Rientjes 		 */
1547cb810ad2SMel Gorman 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1548cf66f070SMel Gorman 			cond_resched();
1549f6ea3adbSDavid Rientjes 
15507d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
15517d49d886SVlastimil Babka 									zone);
15527d49d886SVlastimil Babka 		if (!page)
1553ff9543fdSMichal Nazarewicz 			continue;
1554ff9543fdSMichal Nazarewicz 
1555ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
15569f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1557ff9543fdSMichal Nazarewicz 			continue;
155868e3e926SLinus Torvalds 
1559bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1560bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1561bb13ffebSMel Gorman 			continue;
1562bb13ffebSMel Gorman 
1563e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
15644fca9730SMel Gorman 		nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
15654fca9730SMel Gorman 					block_end_pfn, freelist, stride, false);
1566ff9543fdSMichal Nazarewicz 
1567d097a6f6SMel Gorman 		/* Update the skip hint if the full pageblock was scanned */
1568d097a6f6SMel Gorman 		if (isolate_start_pfn == block_end_pfn)
1569d097a6f6SMel Gorman 			update_pageblock_skip(cc, page, block_start_pfn);
1570d097a6f6SMel Gorman 
1571cb2dcaf0SMel Gorman 		/* Are enough freepages isolated? */
1572cb2dcaf0SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages) {
1573a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1574a46cbf3bSDavid Rientjes 				/*
1575a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1576a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1577a46cbf3bSDavid Rientjes 				 */
1578f5f61a32SVlastimil Babka 				isolate_start_pfn =
1579e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1580a46cbf3bSDavid Rientjes 			}
1581be976572SVlastimil Babka 			break;
1582a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1583f5f61a32SVlastimil Babka 			/*
1584a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1585a46cbf3bSDavid Rientjes 			 * needlessly.
1586f5f61a32SVlastimil Babka 			 */
1587a46cbf3bSDavid Rientjes 			break;
1588f5f61a32SVlastimil Babka 		}
15894fca9730SMel Gorman 
15904fca9730SMel Gorman 		/* Adjust stride depending on isolation */
15914fca9730SMel Gorman 		if (nr_isolated) {
15924fca9730SMel Gorman 			stride = 1;
15934fca9730SMel Gorman 			continue;
15944fca9730SMel Gorman 		}
15954fca9730SMel Gorman 		stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1596c89511abSMel Gorman 	}
1597ff9543fdSMichal Nazarewicz 
15987ed695e0SVlastimil Babka 	/*
1599f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1600f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1601f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1602f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
16037ed695e0SVlastimil Babka 	 */
1604f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
16055a811889SMel Gorman 
16065a811889SMel Gorman splitmap:
16075a811889SMel Gorman 	/* __isolate_free_page() does not map the pages */
16085a811889SMel Gorman 	split_map_pages(freelist);
1609748446bbSMel Gorman }
1610748446bbSMel Gorman 
1611748446bbSMel Gorman /*
1612748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1613748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1614748446bbSMel Gorman  */
1615748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1616666feb21SMichal Hocko 					unsigned long data)
1617748446bbSMel Gorman {
1618748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1619748446bbSMel Gorman 	struct page *freepage;
1620748446bbSMel Gorman 
1621748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1622edc2ca61SVlastimil Babka 		isolate_freepages(cc);
1623748446bbSMel Gorman 
1624748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1625748446bbSMel Gorman 			return NULL;
1626748446bbSMel Gorman 	}
1627748446bbSMel Gorman 
1628748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1629748446bbSMel Gorman 	list_del(&freepage->lru);
1630748446bbSMel Gorman 	cc->nr_freepages--;
1631748446bbSMel Gorman 
1632748446bbSMel Gorman 	return freepage;
1633748446bbSMel Gorman }
1634748446bbSMel Gorman 
1635748446bbSMel Gorman /*
1636d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1637d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1638d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1639d53aea3dSDavid Rientjes  */
1640d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1641d53aea3dSDavid Rientjes {
1642d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1643d53aea3dSDavid Rientjes 
1644d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1645d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1646d53aea3dSDavid Rientjes }
1647d53aea3dSDavid Rientjes 
1648ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1649ff9543fdSMichal Nazarewicz typedef enum {
1650ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1651ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1652ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1653ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1654ff9543fdSMichal Nazarewicz 
1655ff9543fdSMichal Nazarewicz /*
16565bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
16575bbe3547SEric B Munson  * compactable pages.
16585bbe3547SEric B Munson  */
16596923aa0dSSebastian Andrzej Siewior #ifdef CONFIG_PREEMPT_RT
16606923aa0dSSebastian Andrzej Siewior int sysctl_compact_unevictable_allowed __read_mostly = 0;
16616923aa0dSSebastian Andrzej Siewior #else
16625bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
16636923aa0dSSebastian Andrzej Siewior #endif
16645bbe3547SEric B Munson 
166570b44595SMel Gorman static inline void
166670b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
166770b44595SMel Gorman {
166870b44595SMel Gorman 	if (cc->fast_start_pfn == ULONG_MAX)
166970b44595SMel Gorman 		return;
167070b44595SMel Gorman 
167170b44595SMel Gorman 	if (!cc->fast_start_pfn)
167270b44595SMel Gorman 		cc->fast_start_pfn = pfn;
167370b44595SMel Gorman 
167470b44595SMel Gorman 	cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
167570b44595SMel Gorman }
167670b44595SMel Gorman 
167770b44595SMel Gorman static inline unsigned long
167870b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc)
167970b44595SMel Gorman {
168070b44595SMel Gorman 	if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
168170b44595SMel Gorman 		return cc->migrate_pfn;
168270b44595SMel Gorman 
168370b44595SMel Gorman 	cc->migrate_pfn = cc->fast_start_pfn;
168470b44595SMel Gorman 	cc->fast_start_pfn = ULONG_MAX;
168570b44595SMel Gorman 
168670b44595SMel Gorman 	return cc->migrate_pfn;
168770b44595SMel Gorman }
168870b44595SMel Gorman 
168970b44595SMel Gorman /*
169070b44595SMel Gorman  * Briefly search the free lists for a migration source that already has
169170b44595SMel Gorman  * some free pages to reduce the number of pages that need migration
169270b44595SMel Gorman  * before a pageblock is free.
169370b44595SMel Gorman  */
169470b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc)
169570b44595SMel Gorman {
169670b44595SMel Gorman 	unsigned int limit = freelist_scan_limit(cc);
169770b44595SMel Gorman 	unsigned int nr_scanned = 0;
169870b44595SMel Gorman 	unsigned long distance;
169970b44595SMel Gorman 	unsigned long pfn = cc->migrate_pfn;
170070b44595SMel Gorman 	unsigned long high_pfn;
170170b44595SMel Gorman 	int order;
170270b44595SMel Gorman 
170370b44595SMel Gorman 	/* Skip hints are relied on to avoid repeats on the fast search */
170470b44595SMel Gorman 	if (cc->ignore_skip_hint)
170570b44595SMel Gorman 		return pfn;
170670b44595SMel Gorman 
170770b44595SMel Gorman 	/*
170870b44595SMel Gorman 	 * If the migrate_pfn is not at the start of a zone or the start
170970b44595SMel Gorman 	 * of a pageblock then assume this is a continuation of a previous
171070b44595SMel Gorman 	 * scan restarted due to COMPACT_CLUSTER_MAX.
171170b44595SMel Gorman 	 */
171270b44595SMel Gorman 	if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
171370b44595SMel Gorman 		return pfn;
171470b44595SMel Gorman 
171570b44595SMel Gorman 	/*
171670b44595SMel Gorman 	 * For smaller orders, just linearly scan as the number of pages
171770b44595SMel Gorman 	 * to migrate should be relatively small and does not necessarily
171870b44595SMel Gorman 	 * justify freeing up a large block for a small allocation.
171970b44595SMel Gorman 	 */
172070b44595SMel Gorman 	if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
172170b44595SMel Gorman 		return pfn;
172270b44595SMel Gorman 
172370b44595SMel Gorman 	/*
172470b44595SMel Gorman 	 * Only allow kcompactd and direct requests for movable pages to
172570b44595SMel Gorman 	 * quickly clear out a MOVABLE pageblock for allocation. This
172670b44595SMel Gorman 	 * reduces the risk that a large movable pageblock is freed for
172770b44595SMel Gorman 	 * an unmovable/reclaimable small allocation.
172870b44595SMel Gorman 	 */
172970b44595SMel Gorman 	if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
173070b44595SMel Gorman 		return pfn;
173170b44595SMel Gorman 
173270b44595SMel Gorman 	/*
173370b44595SMel Gorman 	 * When starting the migration scanner, pick any pageblock within the
173470b44595SMel Gorman 	 * first half of the search space. Otherwise try and pick a pageblock
173570b44595SMel Gorman 	 * within the first eighth to reduce the chances that a migration
173670b44595SMel Gorman 	 * target later becomes a source.
173770b44595SMel Gorman 	 */
173870b44595SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
173970b44595SMel Gorman 	if (cc->migrate_pfn != cc->zone->zone_start_pfn)
174070b44595SMel Gorman 		distance >>= 2;
174170b44595SMel Gorman 	high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
174270b44595SMel Gorman 
174370b44595SMel Gorman 	for (order = cc->order - 1;
174470b44595SMel Gorman 	     order >= PAGE_ALLOC_COSTLY_ORDER && pfn == cc->migrate_pfn && nr_scanned < limit;
174570b44595SMel Gorman 	     order--) {
174670b44595SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
174770b44595SMel Gorman 		struct list_head *freelist;
174870b44595SMel Gorman 		unsigned long flags;
174970b44595SMel Gorman 		struct page *freepage;
175070b44595SMel Gorman 
175170b44595SMel Gorman 		if (!area->nr_free)
175270b44595SMel Gorman 			continue;
175370b44595SMel Gorman 
175470b44595SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
175570b44595SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
175670b44595SMel Gorman 		list_for_each_entry(freepage, freelist, lru) {
175770b44595SMel Gorman 			unsigned long free_pfn;
175870b44595SMel Gorman 
175970b44595SMel Gorman 			nr_scanned++;
176070b44595SMel Gorman 			free_pfn = page_to_pfn(freepage);
176170b44595SMel Gorman 			if (free_pfn < high_pfn) {
176270b44595SMel Gorman 				/*
176370b44595SMel Gorman 				 * Avoid if skipped recently. Ideally it would
176470b44595SMel Gorman 				 * move to the tail but even safe iteration of
176570b44595SMel Gorman 				 * the list assumes an entry is deleted, not
176670b44595SMel Gorman 				 * reordered.
176770b44595SMel Gorman 				 */
176870b44595SMel Gorman 				if (get_pageblock_skip(freepage)) {
176970b44595SMel Gorman 					if (list_is_last(freelist, &freepage->lru))
177070b44595SMel Gorman 						break;
177170b44595SMel Gorman 
177270b44595SMel Gorman 					continue;
177370b44595SMel Gorman 				}
177470b44595SMel Gorman 
177570b44595SMel Gorman 				/* Reorder to so a future search skips recent pages */
177670b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
177770b44595SMel Gorman 
1778e380bebeSMel Gorman 				update_fast_start_pfn(cc, free_pfn);
177970b44595SMel Gorman 				pfn = pageblock_start_pfn(free_pfn);
178070b44595SMel Gorman 				cc->fast_search_fail = 0;
178170b44595SMel Gorman 				set_pageblock_skip(freepage);
178270b44595SMel Gorman 				break;
178370b44595SMel Gorman 			}
178470b44595SMel Gorman 
178570b44595SMel Gorman 			if (nr_scanned >= limit) {
178670b44595SMel Gorman 				cc->fast_search_fail++;
178770b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
178870b44595SMel Gorman 				break;
178970b44595SMel Gorman 			}
179070b44595SMel Gorman 		}
179170b44595SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
179270b44595SMel Gorman 	}
179370b44595SMel Gorman 
179470b44595SMel Gorman 	cc->total_migrate_scanned += nr_scanned;
179570b44595SMel Gorman 
179670b44595SMel Gorman 	/*
179770b44595SMel Gorman 	 * If fast scanning failed then use a cached entry for a page block
179870b44595SMel Gorman 	 * that had free pages as the basis for starting a linear scan.
179970b44595SMel Gorman 	 */
180070b44595SMel Gorman 	if (pfn == cc->migrate_pfn)
180170b44595SMel Gorman 		pfn = reinit_migrate_pfn(cc);
180270b44595SMel Gorman 
180370b44595SMel Gorman 	return pfn;
180470b44595SMel Gorman }
180570b44595SMel Gorman 
18065bbe3547SEric B Munson /*
1807edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1808edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1809edc2ca61SVlastimil Babka  * compact_control.
1810ff9543fdSMichal Nazarewicz  */
181132aaf055SPengfei Li static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
1812ff9543fdSMichal Nazarewicz {
1813e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1814e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1815e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1816edc2ca61SVlastimil Babka 	struct page *page;
1817edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
18185bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
18191d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
182070b44595SMel Gorman 	bool fast_find_block;
1821ff9543fdSMichal Nazarewicz 
1822edc2ca61SVlastimil Babka 	/*
1823edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
182470b44595SMel Gorman 	 * initialized by compact_zone(). The first failure will use
182570b44595SMel Gorman 	 * the lowest PFN as the starting point for linear scanning.
1826edc2ca61SVlastimil Babka 	 */
182770b44595SMel Gorman 	low_pfn = fast_find_migrateblock(cc);
182806b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
182932aaf055SPengfei Li 	if (block_start_pfn < cc->zone->zone_start_pfn)
183032aaf055SPengfei Li 		block_start_pfn = cc->zone->zone_start_pfn;
1831ff9543fdSMichal Nazarewicz 
183270b44595SMel Gorman 	/*
183370b44595SMel Gorman 	 * fast_find_migrateblock marks a pageblock skipped so to avoid
183470b44595SMel Gorman 	 * the isolation_suitable check below, check whether the fast
183570b44595SMel Gorman 	 * search was successful.
183670b44595SMel Gorman 	 */
183770b44595SMel Gorman 	fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
183870b44595SMel Gorman 
1839ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
184006b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1841ff9543fdSMichal Nazarewicz 
1842edc2ca61SVlastimil Babka 	/*
1843edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1844edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1845edc2ca61SVlastimil Babka 	 */
1846e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
184770b44595SMel Gorman 			fast_find_block = false,
1848e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1849e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1850e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1851edc2ca61SVlastimil Babka 
1852edc2ca61SVlastimil Babka 		/*
1853edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1854edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1855cb810ad2SMel Gorman 		 * need to schedule.
1856edc2ca61SVlastimil Babka 		 */
1857cb810ad2SMel Gorman 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1858cf66f070SMel Gorman 			cond_resched();
1859edc2ca61SVlastimil Babka 
186032aaf055SPengfei Li 		page = pageblock_pfn_to_page(block_start_pfn,
186132aaf055SPengfei Li 						block_end_pfn, cc->zone);
18627d49d886SVlastimil Babka 		if (!page)
1863edc2ca61SVlastimil Babka 			continue;
1864edc2ca61SVlastimil Babka 
1865e380bebeSMel Gorman 		/*
1866e380bebeSMel Gorman 		 * If isolation recently failed, do not retry. Only check the
1867e380bebeSMel Gorman 		 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1868e380bebeSMel Gorman 		 * to be visited multiple times. Assume skip was checked
1869e380bebeSMel Gorman 		 * before making it "skip" so other compaction instances do
1870e380bebeSMel Gorman 		 * not scan the same block.
1871e380bebeSMel Gorman 		 */
1872e380bebeSMel Gorman 		if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1873e380bebeSMel Gorman 		    !fast_find_block && !isolation_suitable(cc, page))
1874edc2ca61SVlastimil Babka 			continue;
1875edc2ca61SVlastimil Babka 
1876edc2ca61SVlastimil Babka 		/*
18779bebefd5SMel Gorman 		 * For async compaction, also only scan in MOVABLE blocks
18789bebefd5SMel Gorman 		 * without huge pages. Async compaction is optimistic to see
18799bebefd5SMel Gorman 		 * if the minimum amount of work satisfies the allocation.
18809bebefd5SMel Gorman 		 * The cached PFN is updated as it's possible that all
18819bebefd5SMel Gorman 		 * remaining blocks between source and target are unsuitable
18829bebefd5SMel Gorman 		 * and the compaction scanners fail to meet.
1883edc2ca61SVlastimil Babka 		 */
18849bebefd5SMel Gorman 		if (!suitable_migration_source(cc, page)) {
18859bebefd5SMel Gorman 			update_cached_migrate(cc, block_end_pfn);
1886edc2ca61SVlastimil Babka 			continue;
18879bebefd5SMel Gorman 		}
1888ff9543fdSMichal Nazarewicz 
1889ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1890e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1891e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1892edc2ca61SVlastimil Babka 
1893cb2dcaf0SMel Gorman 		if (!low_pfn)
1894ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1895ff9543fdSMichal Nazarewicz 
1896edc2ca61SVlastimil Babka 		/*
1897edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1898edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1899edc2ca61SVlastimil Babka 		 * continue or not.
1900edc2ca61SVlastimil Babka 		 */
1901edc2ca61SVlastimil Babka 		break;
1902edc2ca61SVlastimil Babka 	}
1903edc2ca61SVlastimil Babka 
1904f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1905f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1906ff9543fdSMichal Nazarewicz 
1907edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1908ff9543fdSMichal Nazarewicz }
1909ff9543fdSMichal Nazarewicz 
191021c527a3SYaowei Bai /*
191121c527a3SYaowei Bai  * order == -1 is expected when compacting via
191221c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
191321c527a3SYaowei Bai  */
191421c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
191521c527a3SYaowei Bai {
191621c527a3SYaowei Bai 	return order == -1;
191721c527a3SYaowei Bai }
191821c527a3SYaowei Bai 
1919facdaa91SNitin Gupta static bool kswapd_is_running(pg_data_t *pgdat)
1920facdaa91SNitin Gupta {
1921facdaa91SNitin Gupta 	return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING);
1922facdaa91SNitin Gupta }
1923facdaa91SNitin Gupta 
1924facdaa91SNitin Gupta /*
1925facdaa91SNitin Gupta  * A zone's fragmentation score is the external fragmentation wrt to the
1926facdaa91SNitin Gupta  * COMPACTION_HPAGE_ORDER scaled by the zone's size. It returns a value
1927facdaa91SNitin Gupta  * in the range [0, 100].
1928facdaa91SNitin Gupta  *
1929facdaa91SNitin Gupta  * The scaling factor ensures that proactive compaction focuses on larger
1930facdaa91SNitin Gupta  * zones like ZONE_NORMAL, rather than smaller, specialized zones like
1931facdaa91SNitin Gupta  * ZONE_DMA32. For smaller zones, the score value remains close to zero,
1932facdaa91SNitin Gupta  * and thus never exceeds the high threshold for proactive compaction.
1933facdaa91SNitin Gupta  */
1934d34c0a75SNitin Gupta static unsigned int fragmentation_score_zone(struct zone *zone)
1935facdaa91SNitin Gupta {
1936facdaa91SNitin Gupta 	unsigned long score;
1937facdaa91SNitin Gupta 
1938facdaa91SNitin Gupta 	score = zone->present_pages *
1939facdaa91SNitin Gupta 			extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
1940facdaa91SNitin Gupta 	return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
1941facdaa91SNitin Gupta }
1942facdaa91SNitin Gupta 
1943facdaa91SNitin Gupta /*
1944facdaa91SNitin Gupta  * The per-node proactive (background) compaction process is started by its
1945facdaa91SNitin Gupta  * corresponding kcompactd thread when the node's fragmentation score
1946facdaa91SNitin Gupta  * exceeds the high threshold. The compaction process remains active till
1947facdaa91SNitin Gupta  * the node's score falls below the low threshold, or one of the back-off
1948facdaa91SNitin Gupta  * conditions is met.
1949facdaa91SNitin Gupta  */
1950d34c0a75SNitin Gupta static unsigned int fragmentation_score_node(pg_data_t *pgdat)
1951facdaa91SNitin Gupta {
1952d34c0a75SNitin Gupta 	unsigned int score = 0;
1953facdaa91SNitin Gupta 	int zoneid;
1954facdaa91SNitin Gupta 
1955facdaa91SNitin Gupta 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1956facdaa91SNitin Gupta 		struct zone *zone;
1957facdaa91SNitin Gupta 
1958facdaa91SNitin Gupta 		zone = &pgdat->node_zones[zoneid];
1959facdaa91SNitin Gupta 		score += fragmentation_score_zone(zone);
1960facdaa91SNitin Gupta 	}
1961facdaa91SNitin Gupta 
1962facdaa91SNitin Gupta 	return score;
1963facdaa91SNitin Gupta }
1964facdaa91SNitin Gupta 
1965d34c0a75SNitin Gupta static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
1966facdaa91SNitin Gupta {
1967d34c0a75SNitin Gupta 	unsigned int wmark_low;
1968facdaa91SNitin Gupta 
1969facdaa91SNitin Gupta 	/*
1970facdaa91SNitin Gupta 	 * Cap the low watermak to avoid excessive compaction
1971facdaa91SNitin Gupta 	 * activity in case a user sets the proactivess tunable
1972facdaa91SNitin Gupta 	 * close to 100 (maximum).
1973facdaa91SNitin Gupta 	 */
1974d34c0a75SNitin Gupta 	wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
1975d34c0a75SNitin Gupta 	return low ? wmark_low : min(wmark_low + 10, 100U);
1976facdaa91SNitin Gupta }
1977facdaa91SNitin Gupta 
1978facdaa91SNitin Gupta static bool should_proactive_compact_node(pg_data_t *pgdat)
1979facdaa91SNitin Gupta {
1980facdaa91SNitin Gupta 	int wmark_high;
1981facdaa91SNitin Gupta 
1982facdaa91SNitin Gupta 	if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
1983facdaa91SNitin Gupta 		return false;
1984facdaa91SNitin Gupta 
1985facdaa91SNitin Gupta 	wmark_high = fragmentation_score_wmark(pgdat, false);
1986facdaa91SNitin Gupta 	return fragmentation_score_node(pgdat) > wmark_high;
1987facdaa91SNitin Gupta }
1988facdaa91SNitin Gupta 
198940cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc)
1990748446bbSMel Gorman {
19918fb74b9fSMel Gorman 	unsigned int order;
1992d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
1993cb2dcaf0SMel Gorman 	int ret;
1994748446bbSMel Gorman 
1995753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1996f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
199755b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
199840cacbcbSMel Gorman 		reset_cached_positions(cc->zone);
199955b7c4c9SVlastimil Babka 
200062997027SMel Gorman 		/*
200162997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
2002accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
200362997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
200462997027SMel Gorman 		 * based on an allocation request.
200562997027SMel Gorman 		 */
2006accf6242SVlastimil Babka 		if (cc->direct_compaction)
200740cacbcbSMel Gorman 			cc->zone->compact_blockskip_flush = true;
200862997027SMel Gorman 
2009c8f7de0bSMichal Hocko 		if (cc->whole_zone)
2010748446bbSMel Gorman 			return COMPACT_COMPLETE;
2011c8f7de0bSMichal Hocko 		else
2012c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
2013bb13ffebSMel Gorman 	}
2014748446bbSMel Gorman 
2015facdaa91SNitin Gupta 	if (cc->proactive_compaction) {
2016facdaa91SNitin Gupta 		int score, wmark_low;
2017facdaa91SNitin Gupta 		pg_data_t *pgdat;
2018facdaa91SNitin Gupta 
2019facdaa91SNitin Gupta 		pgdat = cc->zone->zone_pgdat;
2020facdaa91SNitin Gupta 		if (kswapd_is_running(pgdat))
2021facdaa91SNitin Gupta 			return COMPACT_PARTIAL_SKIPPED;
2022facdaa91SNitin Gupta 
2023facdaa91SNitin Gupta 		score = fragmentation_score_zone(cc->zone);
2024facdaa91SNitin Gupta 		wmark_low = fragmentation_score_wmark(pgdat, true);
2025facdaa91SNitin Gupta 
2026facdaa91SNitin Gupta 		if (score > wmark_low)
2027facdaa91SNitin Gupta 			ret = COMPACT_CONTINUE;
2028facdaa91SNitin Gupta 		else
2029facdaa91SNitin Gupta 			ret = COMPACT_SUCCESS;
2030facdaa91SNitin Gupta 
2031facdaa91SNitin Gupta 		goto out;
2032facdaa91SNitin Gupta 	}
2033facdaa91SNitin Gupta 
203421c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
203556de7263SMel Gorman 		return COMPACT_CONTINUE;
203656de7263SMel Gorman 
2037baf6a9a1SVlastimil Babka 	/*
2038efe771c7SMel Gorman 	 * Always finish scanning a pageblock to reduce the possibility of
2039efe771c7SMel Gorman 	 * fallbacks in the future. This is particularly important when
2040efe771c7SMel Gorman 	 * migration source is unmovable/reclaimable but it's not worth
2041efe771c7SMel Gorman 	 * special casing.
2042baf6a9a1SVlastimil Babka 	 */
2043efe771c7SMel Gorman 	if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2044baf6a9a1SVlastimil Babka 		return COMPACT_CONTINUE;
2045baf6a9a1SVlastimil Babka 
204656de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
2047cb2dcaf0SMel Gorman 	ret = COMPACT_NO_SUITABLE_PAGE;
204856de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
204940cacbcbSMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
20502149cdaeSJoonsoo Kim 		bool can_steal;
20518fb74b9fSMel Gorman 
205256de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
2053b03641afSDan Williams 		if (!free_area_empty(area, migratetype))
2054cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
205556de7263SMel Gorman 
20562149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
20572149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
20582149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
2059b03641afSDan Williams 			!free_area_empty(area, MIGRATE_CMA))
2060cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
20612149cdaeSJoonsoo Kim #endif
20622149cdaeSJoonsoo Kim 		/*
20632149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
20642149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
20652149cdaeSJoonsoo Kim 		 */
20662149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
2067baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
2068baf6a9a1SVlastimil Babka 
2069baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
2070baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
2071cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
2072baf6a9a1SVlastimil Babka 
2073baf6a9a1SVlastimil Babka 			/*
2074baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
2075baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
2076baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
2077baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
2078baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
2079baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
2080baf6a9a1SVlastimil Babka 			 */
2081baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
2082baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
2083baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
2084baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
2085baf6a9a1SVlastimil Babka 			}
2086baf6a9a1SVlastimil Babka 
2087cb2dcaf0SMel Gorman 			ret = COMPACT_CONTINUE;
2088cb2dcaf0SMel Gorman 			break;
2089baf6a9a1SVlastimil Babka 		}
209056de7263SMel Gorman 	}
209156de7263SMel Gorman 
2092facdaa91SNitin Gupta out:
2093cb2dcaf0SMel Gorman 	if (cc->contended || fatal_signal_pending(current))
2094cb2dcaf0SMel Gorman 		ret = COMPACT_CONTENDED;
2095cb2dcaf0SMel Gorman 
2096cb2dcaf0SMel Gorman 	return ret;
2097837d026dSJoonsoo Kim }
2098837d026dSJoonsoo Kim 
209940cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc)
2100837d026dSJoonsoo Kim {
2101837d026dSJoonsoo Kim 	int ret;
2102837d026dSJoonsoo Kim 
210340cacbcbSMel Gorman 	ret = __compact_finished(cc);
210440cacbcbSMel Gorman 	trace_mm_compaction_finished(cc->zone, cc->order, ret);
2105837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
2106837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
2107837d026dSJoonsoo Kim 
2108837d026dSJoonsoo Kim 	return ret;
2109748446bbSMel Gorman }
2110748446bbSMel Gorman 
2111ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
2112c603844bSMel Gorman 					unsigned int alloc_flags,
211397a225e6SJoonsoo Kim 					int highest_zoneidx,
211486a294a8SMichal Hocko 					unsigned long wmark_target)
21153e7d3449SMel Gorman {
21163e7d3449SMel Gorman 	unsigned long watermark;
21173e7d3449SMel Gorman 
211821c527a3SYaowei Bai 	if (is_via_compact_memory(order))
21193957c776SMichal Hocko 		return COMPACT_CONTINUE;
21203957c776SMichal Hocko 
2121a9214443SMel Gorman 	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
2122ebff3980SVlastimil Babka 	/*
2123ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
2124ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
2125ebff3980SVlastimil Babka 	 */
212697a225e6SJoonsoo Kim 	if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
2127ebff3980SVlastimil Babka 								alloc_flags))
2128cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
2129ebff3980SVlastimil Babka 
21303957c776SMichal Hocko 	/*
21319861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
2132984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
2133984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
2134984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
2135984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
213697a225e6SJoonsoo Kim 	 * isolation. We however do use the direct compactor's highest_zoneidx
213797a225e6SJoonsoo Kim 	 * to skip over zones where lowmem reserves would prevent allocation
213897a225e6SJoonsoo Kim 	 * even if compaction succeeds.
21398348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
21408348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
2141d883c6cfSJoonsoo Kim 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2142d883c6cfSJoonsoo Kim 	 * suitable migration targets
21433e7d3449SMel Gorman 	 */
21448348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
21458348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
21468348faf9SVlastimil Babka 	watermark += compact_gap(order);
214797a225e6SJoonsoo Kim 	if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
2148d883c6cfSJoonsoo Kim 						ALLOC_CMA, wmark_target))
21493e7d3449SMel Gorman 		return COMPACT_SKIPPED;
21503e7d3449SMel Gorman 
2151cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
2152cc5c9f09SVlastimil Babka }
2153cc5c9f09SVlastimil Babka 
21542b1a20c3SHui Su /*
21552b1a20c3SHui Su  * compaction_suitable: Is this suitable to run compaction on this zone now?
21562b1a20c3SHui Su  * Returns
21572b1a20c3SHui Su  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
21582b1a20c3SHui Su  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
21592b1a20c3SHui Su  *   COMPACT_CONTINUE - If compaction should run now
21602b1a20c3SHui Su  */
2161cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
2162cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
216397a225e6SJoonsoo Kim 					int highest_zoneidx)
2164cc5c9f09SVlastimil Babka {
2165cc5c9f09SVlastimil Babka 	enum compact_result ret;
2166cc5c9f09SVlastimil Babka 	int fragindex;
2167cc5c9f09SVlastimil Babka 
216897a225e6SJoonsoo Kim 	ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
2169cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
21703e7d3449SMel Gorman 	/*
21713e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
21723e7d3449SMel Gorman 	 * low memory or external fragmentation
21733e7d3449SMel Gorman 	 *
2174ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
2175ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
21763e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
21773e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
21783e7d3449SMel Gorman 	 *
217920311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
218020311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
218120311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
218220311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
218320311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
218420311420SVlastimil Babka 	 * expense of system stability.
21853e7d3449SMel Gorman 	 */
218620311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
21873e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
21883e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2189cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
21903e7d3449SMel Gorman 	}
21913e7d3449SMel Gorman 
2192837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
2193837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
2194837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
2195837d026dSJoonsoo Kim 
2196837d026dSJoonsoo Kim 	return ret;
2197837d026dSJoonsoo Kim }
2198837d026dSJoonsoo Kim 
219986a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
220086a294a8SMichal Hocko 		int alloc_flags)
220186a294a8SMichal Hocko {
220286a294a8SMichal Hocko 	struct zone *zone;
220386a294a8SMichal Hocko 	struct zoneref *z;
220486a294a8SMichal Hocko 
220586a294a8SMichal Hocko 	/*
220686a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
220786a294a8SMichal Hocko 	 * retrying the reclaim.
220886a294a8SMichal Hocko 	 */
220997a225e6SJoonsoo Kim 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
221097a225e6SJoonsoo Kim 				ac->highest_zoneidx, ac->nodemask) {
221186a294a8SMichal Hocko 		unsigned long available;
221286a294a8SMichal Hocko 		enum compact_result compact_result;
221386a294a8SMichal Hocko 
221486a294a8SMichal Hocko 		/*
221586a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
221686a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
221786a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
221886a294a8SMichal Hocko 		 * is happy about the watermark check.
221986a294a8SMichal Hocko 		 */
22205a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
222186a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
222286a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
222397a225e6SJoonsoo Kim 				ac->highest_zoneidx, available);
2224cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
222586a294a8SMichal Hocko 			return true;
222686a294a8SMichal Hocko 	}
222786a294a8SMichal Hocko 
222886a294a8SMichal Hocko 	return false;
222986a294a8SMichal Hocko }
223086a294a8SMichal Hocko 
22315e1f0f09SMel Gorman static enum compact_result
22325e1f0f09SMel Gorman compact_zone(struct compact_control *cc, struct capture_control *capc)
2233748446bbSMel Gorman {
2234ea7ab982SMichal Hocko 	enum compact_result ret;
223540cacbcbSMel Gorman 	unsigned long start_pfn = cc->zone->zone_start_pfn;
223640cacbcbSMel Gorman 	unsigned long end_pfn = zone_end_pfn(cc->zone);
2237566e54e1SMel Gorman 	unsigned long last_migrated_pfn;
2238e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
22398854c55fSMel Gorman 	bool update_cached;
2240748446bbSMel Gorman 
2241a94b5252SYafang Shao 	/*
2242a94b5252SYafang Shao 	 * These counters track activities during zone compaction.  Initialize
2243a94b5252SYafang Shao 	 * them before compacting a new zone.
2244a94b5252SYafang Shao 	 */
2245a94b5252SYafang Shao 	cc->total_migrate_scanned = 0;
2246a94b5252SYafang Shao 	cc->total_free_scanned = 0;
2247a94b5252SYafang Shao 	cc->nr_migratepages = 0;
2248a94b5252SYafang Shao 	cc->nr_freepages = 0;
2249a94b5252SYafang Shao 	INIT_LIST_HEAD(&cc->freepages);
2250a94b5252SYafang Shao 	INIT_LIST_HEAD(&cc->migratepages);
2251a94b5252SYafang Shao 
225201c0bfe0SWei Yang 	cc->migratetype = gfp_migratetype(cc->gfp_mask);
225340cacbcbSMel Gorman 	ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
225497a225e6SJoonsoo Kim 							cc->highest_zoneidx);
22553e7d3449SMel Gorman 	/* Compaction is likely to fail */
2256cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
22573e7d3449SMel Gorman 		return ret;
2258c46649deSMichal Hocko 
2259c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
2260c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
22613e7d3449SMel Gorman 
2262c89511abSMel Gorman 	/*
2263d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
2264accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
2265d3132e4bSVlastimil Babka 	 */
226640cacbcbSMel Gorman 	if (compaction_restarting(cc->zone, cc->order))
226740cacbcbSMel Gorman 		__reset_isolation_suitable(cc->zone);
2268d3132e4bSVlastimil Babka 
2269d3132e4bSVlastimil Babka 	/*
2270c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
227106ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
227206ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
227306ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
2274c89511abSMel Gorman 	 */
227570b44595SMel Gorman 	cc->fast_start_pfn = 0;
227606ed2998SVlastimil Babka 	if (cc->whole_zone) {
227706ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
227806ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
227906ed2998SVlastimil Babka 	} else {
228040cacbcbSMel Gorman 		cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
228140cacbcbSMel Gorman 		cc->free_pfn = cc->zone->compact_cached_free_pfn;
2282623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
228306b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
228440cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = cc->free_pfn;
2285c89511abSMel Gorman 		}
2286623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2287c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
228840cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
228940cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2290c89511abSMel Gorman 		}
2291c8f7de0bSMichal Hocko 
2292e332f741SMel Gorman 		if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
2293c8f7de0bSMichal Hocko 			cc->whole_zone = true;
229406ed2998SVlastimil Babka 	}
2295c8f7de0bSMichal Hocko 
2296566e54e1SMel Gorman 	last_migrated_pfn = 0;
2297748446bbSMel Gorman 
22988854c55fSMel Gorman 	/*
22998854c55fSMel Gorman 	 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
23008854c55fSMel Gorman 	 * the basis that some migrations will fail in ASYNC mode. However,
23018854c55fSMel Gorman 	 * if the cached PFNs match and pageblocks are skipped due to having
23028854c55fSMel Gorman 	 * no isolation candidates, then the sync state does not matter.
23038854c55fSMel Gorman 	 * Until a pageblock with isolation candidates is found, keep the
23048854c55fSMel Gorman 	 * cached PFNs in sync to avoid revisiting the same blocks.
23058854c55fSMel Gorman 	 */
23068854c55fSMel Gorman 	update_cached = !sync &&
23078854c55fSMel Gorman 		cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
23088854c55fSMel Gorman 
230916c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
231016c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
23110eb927c0SMel Gorman 
2312748446bbSMel Gorman 	migrate_prep_local();
2313748446bbSMel Gorman 
231440cacbcbSMel Gorman 	while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
23159d502c1cSMinchan Kim 		int err;
231619d3cf9dSYanfei Xu 		unsigned long iteration_start_pfn = cc->migrate_pfn;
2317748446bbSMel Gorman 
2318804d3121SMel Gorman 		/*
2319804d3121SMel Gorman 		 * Avoid multiple rescans which can happen if a page cannot be
2320804d3121SMel Gorman 		 * isolated (dirty/writeback in async mode) or if the migrated
2321804d3121SMel Gorman 		 * pages are being allocated before the pageblock is cleared.
2322804d3121SMel Gorman 		 * The first rescan will capture the entire pageblock for
2323804d3121SMel Gorman 		 * migration. If it fails, it'll be marked skip and scanning
2324804d3121SMel Gorman 		 * will proceed as normal.
2325804d3121SMel Gorman 		 */
2326804d3121SMel Gorman 		cc->rescan = false;
2327804d3121SMel Gorman 		if (pageblock_start_pfn(last_migrated_pfn) ==
232819d3cf9dSYanfei Xu 		    pageblock_start_pfn(iteration_start_pfn)) {
2329804d3121SMel Gorman 			cc->rescan = true;
2330804d3121SMel Gorman 		}
2331804d3121SMel Gorman 
233232aaf055SPengfei Li 		switch (isolate_migratepages(cc)) {
2333f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
23342d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
23355733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
2336e64c5237SShaohua Li 			cc->nr_migratepages = 0;
2337f9e35b3bSMel Gorman 			goto out;
2338f9e35b3bSMel Gorman 		case ISOLATE_NONE:
23398854c55fSMel Gorman 			if (update_cached) {
23408854c55fSMel Gorman 				cc->zone->compact_cached_migrate_pfn[1] =
23418854c55fSMel Gorman 					cc->zone->compact_cached_migrate_pfn[0];
23428854c55fSMel Gorman 			}
23438854c55fSMel Gorman 
2344fdaf7f5cSVlastimil Babka 			/*
2345fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
2346fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
2347fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
2348fdaf7f5cSVlastimil Babka 			 */
2349fdaf7f5cSVlastimil Babka 			goto check_drain;
2350f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
23518854c55fSMel Gorman 			update_cached = false;
235219d3cf9dSYanfei Xu 			last_migrated_pfn = iteration_start_pfn;
2353f9e35b3bSMel Gorman 		}
2354748446bbSMel Gorman 
2355d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
2356e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
23577b2a2d4aSMel Gorman 				MR_COMPACTION);
2358748446bbSMel Gorman 
2359f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2360f8c9301fSVlastimil Babka 							&cc->migratepages);
2361748446bbSMel Gorman 
2362f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
2363f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
23649d502c1cSMinchan Kim 		if (err) {
23655733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
23667ed695e0SVlastimil Babka 			/*
23677ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
23687ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
23697ed695e0SVlastimil Babka 			 */
2370f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
23712d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
23724bf2bba3SDavid Rientjes 				goto out;
2373748446bbSMel Gorman 			}
2374fdd048e1SVlastimil Babka 			/*
2375fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
2376fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
2377fdd048e1SVlastimil Babka 			 */
2378fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
2379fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
2380fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
2381fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
2382fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
2383566e54e1SMel Gorman 				last_migrated_pfn = 0;
2384fdd048e1SVlastimil Babka 			}
23854bf2bba3SDavid Rientjes 		}
2386fdaf7f5cSVlastimil Babka 
2387fdaf7f5cSVlastimil Babka check_drain:
2388fdaf7f5cSVlastimil Babka 		/*
2389fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
2390fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
2391fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
2392fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
2393fdaf7f5cSVlastimil Babka 		 * would succeed.
2394fdaf7f5cSVlastimil Babka 		 */
2395566e54e1SMel Gorman 		if (cc->order > 0 && last_migrated_pfn) {
2396fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
239706b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
2398fdaf7f5cSVlastimil Babka 
2399566e54e1SMel Gorman 			if (last_migrated_pfn < current_block_start) {
2400b01b2141SIngo Molnar 				lru_add_drain_cpu_zone(cc->zone);
2401fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
2402566e54e1SMel Gorman 				last_migrated_pfn = 0;
2403fdaf7f5cSVlastimil Babka 			}
2404fdaf7f5cSVlastimil Babka 		}
2405fdaf7f5cSVlastimil Babka 
24065e1f0f09SMel Gorman 		/* Stop if a page has been captured */
24075e1f0f09SMel Gorman 		if (capc && capc->page) {
24085e1f0f09SMel Gorman 			ret = COMPACT_SUCCESS;
24095e1f0f09SMel Gorman 			break;
24105e1f0f09SMel Gorman 		}
2411748446bbSMel Gorman 	}
2412748446bbSMel Gorman 
2413f9e35b3bSMel Gorman out:
24146bace090SVlastimil Babka 	/*
24156bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
24166bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
24176bace090SVlastimil Babka 	 */
24186bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
24196bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
24206bace090SVlastimil Babka 
24216bace090SVlastimil Babka 		cc->nr_freepages = 0;
24226bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
24236bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
242406b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
24256bace090SVlastimil Babka 		/*
24266bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
24276bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
24286bace090SVlastimil Babka 		 */
242940cacbcbSMel Gorman 		if (free_pfn > cc->zone->compact_cached_free_pfn)
243040cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = free_pfn;
24316bace090SVlastimil Babka 	}
2432748446bbSMel Gorman 
24337f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
24347f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
24357f354a54SDavid Rientjes 
243616c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
243716c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
24380eb927c0SMel Gorman 
2439748446bbSMel Gorman 	return ret;
2440748446bbSMel Gorman }
244176ab0f53SMel Gorman 
2442ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
2443c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
244497a225e6SJoonsoo Kim 		unsigned int alloc_flags, int highest_zoneidx,
24455e1f0f09SMel Gorman 		struct page **capture)
244656de7263SMel Gorman {
2447ea7ab982SMichal Hocko 	enum compact_result ret;
244856de7263SMel Gorman 	struct compact_control cc = {
244956de7263SMel Gorman 		.order = order,
2450dbe2d4e4SMel Gorman 		.search_order = order,
24516d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
245256de7263SMel Gorman 		.zone = zone,
2453a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
2454a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
2455ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
245697a225e6SJoonsoo Kim 		.highest_zoneidx = highest_zoneidx,
2457accf6242SVlastimil Babka 		.direct_compaction = true,
2458a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
24599f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
24609f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
246156de7263SMel Gorman 	};
24625e1f0f09SMel Gorman 	struct capture_control capc = {
24635e1f0f09SMel Gorman 		.cc = &cc,
24645e1f0f09SMel Gorman 		.page = NULL,
24655e1f0f09SMel Gorman 	};
24665e1f0f09SMel Gorman 
2467b9e20f0dSVlastimil Babka 	/*
2468b9e20f0dSVlastimil Babka 	 * Make sure the structs are really initialized before we expose the
2469b9e20f0dSVlastimil Babka 	 * capture control, in case we are interrupted and the interrupt handler
2470b9e20f0dSVlastimil Babka 	 * frees a page.
2471b9e20f0dSVlastimil Babka 	 */
2472b9e20f0dSVlastimil Babka 	barrier();
2473b9e20f0dSVlastimil Babka 	WRITE_ONCE(current->capture_control, &capc);
247456de7263SMel Gorman 
24755e1f0f09SMel Gorman 	ret = compact_zone(&cc, &capc);
2476e64c5237SShaohua Li 
2477e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
2478e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
2479e64c5237SShaohua Li 
2480b9e20f0dSVlastimil Babka 	/*
2481b9e20f0dSVlastimil Babka 	 * Make sure we hide capture control first before we read the captured
2482b9e20f0dSVlastimil Babka 	 * page pointer, otherwise an interrupt could free and capture a page
2483b9e20f0dSVlastimil Babka 	 * and we would leak it.
2484b9e20f0dSVlastimil Babka 	 */
2485b9e20f0dSVlastimil Babka 	WRITE_ONCE(current->capture_control, NULL);
2486b9e20f0dSVlastimil Babka 	*capture = READ_ONCE(capc.page);
24875e1f0f09SMel Gorman 
2488e64c5237SShaohua Li 	return ret;
248956de7263SMel Gorman }
249056de7263SMel Gorman 
24915e771905SMel Gorman int sysctl_extfrag_threshold = 500;
24925e771905SMel Gorman 
249356de7263SMel Gorman /**
249456de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
249556de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
24961a6d53a1SVlastimil Babka  * @order: The order of the current allocation
24971a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
24981a6d53a1SVlastimil Babka  * @ac: The context of current allocation
2499112d2d29SYang Shi  * @prio: Determines how hard direct compaction should try to succeed
25006467552cSVlastimil Babka  * @capture: Pointer to free page created by compaction will be stored here
250156de7263SMel Gorman  *
250256de7263SMel Gorman  * This is the main entry point for direct page compaction.
250356de7263SMel Gorman  */
2504ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
2505c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
25065e1f0f09SMel Gorman 		enum compact_priority prio, struct page **capture)
250756de7263SMel Gorman {
250856de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
250956de7263SMel Gorman 	struct zoneref *z;
251056de7263SMel Gorman 	struct zone *zone;
25111d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
251256de7263SMel Gorman 
251373e64c51SMichal Hocko 	/*
251473e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
251573e64c51SMichal Hocko 	 * tricky context because the migration might require IO
251673e64c51SMichal Hocko 	 */
251773e64c51SMichal Hocko 	if (!may_perform_io)
251853853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
251956de7263SMel Gorman 
2520a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
2521837d026dSJoonsoo Kim 
252256de7263SMel Gorman 	/* Compact each zone in the list */
252397a225e6SJoonsoo Kim 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
252497a225e6SJoonsoo Kim 					ac->highest_zoneidx, ac->nodemask) {
2525ea7ab982SMichal Hocko 		enum compact_result status;
252656de7263SMel Gorman 
2527a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
2528a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
25291d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
253053853e2dSVlastimil Babka 			continue;
25311d4746d3SMichal Hocko 		}
253253853e2dSVlastimil Babka 
2533a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
253497a225e6SJoonsoo Kim 				alloc_flags, ac->highest_zoneidx, capture);
253556de7263SMel Gorman 		rc = max(status, rc);
253656de7263SMel Gorman 
25377ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
25387ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
253953853e2dSVlastimil Babka 			/*
254053853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
254153853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
254253853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
254353853e2dSVlastimil Babka 			 * succeeds in this zone.
254453853e2dSVlastimil Babka 			 */
254553853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
25461f9efdefSVlastimil Babka 
2547c3486f53SVlastimil Babka 			break;
25481f9efdefSVlastimil Babka 		}
25491f9efdefSVlastimil Babka 
2550a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
2551c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
255253853e2dSVlastimil Babka 			/*
255353853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
255453853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
255553853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
255653853e2dSVlastimil Babka 			 */
255753853e2dSVlastimil Babka 			defer_compaction(zone, order);
25581f9efdefSVlastimil Babka 
25591f9efdefSVlastimil Babka 		/*
25601f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
25611f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
2562c3486f53SVlastimil Babka 		 * case do not try further zones
25631f9efdefSVlastimil Babka 		 */
2564c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2565c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
25661f9efdefSVlastimil Babka 			break;
25671f9efdefSVlastimil Babka 	}
25681f9efdefSVlastimil Babka 
256956de7263SMel Gorman 	return rc;
257056de7263SMel Gorman }
257156de7263SMel Gorman 
2572facdaa91SNitin Gupta /*
2573facdaa91SNitin Gupta  * Compact all zones within a node till each zone's fragmentation score
2574facdaa91SNitin Gupta  * reaches within proactive compaction thresholds (as determined by the
2575facdaa91SNitin Gupta  * proactiveness tunable).
2576facdaa91SNitin Gupta  *
2577facdaa91SNitin Gupta  * It is possible that the function returns before reaching score targets
2578facdaa91SNitin Gupta  * due to various back-off conditions, such as, contention on per-node or
2579facdaa91SNitin Gupta  * per-zone locks.
2580facdaa91SNitin Gupta  */
2581facdaa91SNitin Gupta static void proactive_compact_node(pg_data_t *pgdat)
2582facdaa91SNitin Gupta {
2583facdaa91SNitin Gupta 	int zoneid;
2584facdaa91SNitin Gupta 	struct zone *zone;
2585facdaa91SNitin Gupta 	struct compact_control cc = {
2586facdaa91SNitin Gupta 		.order = -1,
2587facdaa91SNitin Gupta 		.mode = MIGRATE_SYNC_LIGHT,
2588facdaa91SNitin Gupta 		.ignore_skip_hint = true,
2589facdaa91SNitin Gupta 		.whole_zone = true,
2590facdaa91SNitin Gupta 		.gfp_mask = GFP_KERNEL,
2591facdaa91SNitin Gupta 		.proactive_compaction = true,
2592facdaa91SNitin Gupta 	};
2593facdaa91SNitin Gupta 
2594facdaa91SNitin Gupta 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2595facdaa91SNitin Gupta 		zone = &pgdat->node_zones[zoneid];
2596facdaa91SNitin Gupta 		if (!populated_zone(zone))
2597facdaa91SNitin Gupta 			continue;
2598facdaa91SNitin Gupta 
2599facdaa91SNitin Gupta 		cc.zone = zone;
2600facdaa91SNitin Gupta 
2601facdaa91SNitin Gupta 		compact_zone(&cc, NULL);
2602facdaa91SNitin Gupta 
2603facdaa91SNitin Gupta 		VM_BUG_ON(!list_empty(&cc.freepages));
2604facdaa91SNitin Gupta 		VM_BUG_ON(!list_empty(&cc.migratepages));
2605facdaa91SNitin Gupta 	}
2606facdaa91SNitin Gupta }
260756de7263SMel Gorman 
260876ab0f53SMel Gorman /* Compact all zones within a node */
26097103f16dSAndrew Morton static void compact_node(int nid)
26107be62de9SRik van Riel {
2611791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2612791cae96SVlastimil Babka 	int zoneid;
2613791cae96SVlastimil Babka 	struct zone *zone;
26147be62de9SRik van Riel 	struct compact_control cc = {
26157be62de9SRik van Riel 		.order = -1,
2616e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
261791ca9186SDavid Rientjes 		.ignore_skip_hint = true,
261806ed2998SVlastimil Babka 		.whole_zone = true,
261973e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
26207be62de9SRik van Riel 	};
26217be62de9SRik van Riel 
2622791cae96SVlastimil Babka 
2623791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2624791cae96SVlastimil Babka 
2625791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2626791cae96SVlastimil Babka 		if (!populated_zone(zone))
2627791cae96SVlastimil Babka 			continue;
2628791cae96SVlastimil Babka 
2629791cae96SVlastimil Babka 		cc.zone = zone;
2630791cae96SVlastimil Babka 
26315e1f0f09SMel Gorman 		compact_zone(&cc, NULL);
2632791cae96SVlastimil Babka 
2633791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2634791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2635791cae96SVlastimil Babka 	}
26367be62de9SRik van Riel }
26377be62de9SRik van Riel 
263876ab0f53SMel Gorman /* Compact all nodes in the system */
26397964c06dSJason Liu static void compact_nodes(void)
264076ab0f53SMel Gorman {
264176ab0f53SMel Gorman 	int nid;
264276ab0f53SMel Gorman 
26438575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
26448575ec29SHugh Dickins 	lru_add_drain_all();
26458575ec29SHugh Dickins 
264676ab0f53SMel Gorman 	for_each_online_node(nid)
264776ab0f53SMel Gorman 		compact_node(nid);
264876ab0f53SMel Gorman }
264976ab0f53SMel Gorman 
265076ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
265176ab0f53SMel Gorman int sysctl_compact_memory;
265276ab0f53SMel Gorman 
2653fec4eb2cSYaowei Bai /*
2654facdaa91SNitin Gupta  * Tunable for proactive compaction. It determines how
2655facdaa91SNitin Gupta  * aggressively the kernel should compact memory in the
2656facdaa91SNitin Gupta  * background. It takes values in the range [0, 100].
2657facdaa91SNitin Gupta  */
2658d34c0a75SNitin Gupta unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
2659facdaa91SNitin Gupta 
2660facdaa91SNitin Gupta /*
2661fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
2662fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
2663fec4eb2cSYaowei Bai  */
266476ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
266532927393SChristoph Hellwig 			void *buffer, size_t *length, loff_t *ppos)
266676ab0f53SMel Gorman {
266776ab0f53SMel Gorman 	if (write)
26687964c06dSJason Liu 		compact_nodes();
266976ab0f53SMel Gorman 
267076ab0f53SMel Gorman 	return 0;
267176ab0f53SMel Gorman }
2672ed4a6d7fSMel Gorman 
2673ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
267474e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
267510fbcf4cSKay Sievers 			struct device_attribute *attr,
2676ed4a6d7fSMel Gorman 			const char *buf, size_t count)
2677ed4a6d7fSMel Gorman {
26788575ec29SHugh Dickins 	int nid = dev->id;
26798575ec29SHugh Dickins 
26808575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
26818575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
26828575ec29SHugh Dickins 		lru_add_drain_all();
26838575ec29SHugh Dickins 
26848575ec29SHugh Dickins 		compact_node(nid);
26858575ec29SHugh Dickins 	}
2686ed4a6d7fSMel Gorman 
2687ed4a6d7fSMel Gorman 	return count;
2688ed4a6d7fSMel Gorman }
26890825a6f9SJoe Perches static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node);
2690ed4a6d7fSMel Gorman 
2691ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
2692ed4a6d7fSMel Gorman {
269310fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
2694ed4a6d7fSMel Gorman }
2695ed4a6d7fSMel Gorman 
2696ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
2697ed4a6d7fSMel Gorman {
269810fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
2699ed4a6d7fSMel Gorman }
2700ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
2701ff9543fdSMichal Nazarewicz 
2702698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2703698b1b30SVlastimil Babka {
2704172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
2705698b1b30SVlastimil Babka }
2706698b1b30SVlastimil Babka 
2707698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
2708698b1b30SVlastimil Babka {
2709698b1b30SVlastimil Babka 	int zoneid;
2710698b1b30SVlastimil Babka 	struct zone *zone;
271197a225e6SJoonsoo Kim 	enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
2712698b1b30SVlastimil Babka 
271397a225e6SJoonsoo Kim 	for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
2714698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2715698b1b30SVlastimil Babka 
2716698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2717698b1b30SVlastimil Babka 			continue;
2718698b1b30SVlastimil Babka 
2719698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
272097a225e6SJoonsoo Kim 					highest_zoneidx) == COMPACT_CONTINUE)
2721698b1b30SVlastimil Babka 			return true;
2722698b1b30SVlastimil Babka 	}
2723698b1b30SVlastimil Babka 
2724698b1b30SVlastimil Babka 	return false;
2725698b1b30SVlastimil Babka }
2726698b1b30SVlastimil Babka 
2727698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
2728698b1b30SVlastimil Babka {
2729698b1b30SVlastimil Babka 	/*
2730698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
2731698b1b30SVlastimil Babka 	 * order is allocatable.
2732698b1b30SVlastimil Babka 	 */
2733698b1b30SVlastimil Babka 	int zoneid;
2734698b1b30SVlastimil Babka 	struct zone *zone;
2735698b1b30SVlastimil Babka 	struct compact_control cc = {
2736698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
2737dbe2d4e4SMel Gorman 		.search_order = pgdat->kcompactd_max_order,
273897a225e6SJoonsoo Kim 		.highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
2739698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
2740a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
274173e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
2742698b1b30SVlastimil Babka 	};
2743698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
274497a225e6SJoonsoo Kim 							cc.highest_zoneidx);
27457f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
2746698b1b30SVlastimil Babka 
274797a225e6SJoonsoo Kim 	for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
2748698b1b30SVlastimil Babka 		int status;
2749698b1b30SVlastimil Babka 
2750698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2751698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2752698b1b30SVlastimil Babka 			continue;
2753698b1b30SVlastimil Babka 
2754698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
2755698b1b30SVlastimil Babka 			continue;
2756698b1b30SVlastimil Babka 
2757698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
2758698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
2759698b1b30SVlastimil Babka 			continue;
2760698b1b30SVlastimil Babka 
2761172400c6SVlastimil Babka 		if (kthread_should_stop())
2762172400c6SVlastimil Babka 			return;
2763a94b5252SYafang Shao 
2764a94b5252SYafang Shao 		cc.zone = zone;
27655e1f0f09SMel Gorman 		status = compact_zone(&cc, NULL);
2766698b1b30SVlastimil Babka 
27677ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
2768698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
2769c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
2770698b1b30SVlastimil Babka 			/*
2771bc3106b2SDavid Rientjes 			 * Buddy pages may become stranded on pcps that could
2772bc3106b2SDavid Rientjes 			 * otherwise coalesce on the zone's free area for
2773bc3106b2SDavid Rientjes 			 * order >= cc.order.  This is ratelimited by the
2774bc3106b2SDavid Rientjes 			 * upcoming deferral.
2775bc3106b2SDavid Rientjes 			 */
2776bc3106b2SDavid Rientjes 			drain_all_pages(zone);
2777bc3106b2SDavid Rientjes 
2778bc3106b2SDavid Rientjes 			/*
2779698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
2780698b1b30SVlastimil Babka 			 * sync direct compaction does.
2781698b1b30SVlastimil Babka 			 */
2782698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
2783698b1b30SVlastimil Babka 		}
2784698b1b30SVlastimil Babka 
27857f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
27867f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
27877f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
27887f354a54SDavid Rientjes 				     cc.total_free_scanned);
27897f354a54SDavid Rientjes 
2790698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2791698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2792698b1b30SVlastimil Babka 	}
2793698b1b30SVlastimil Babka 
2794698b1b30SVlastimil Babka 	/*
2795698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
279697a225e6SJoonsoo Kim 	 * the requested order/highest_zoneidx in case it was higher/tighter
279797a225e6SJoonsoo Kim 	 * than our current ones
2798698b1b30SVlastimil Babka 	 */
2799698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2800698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
280197a225e6SJoonsoo Kim 	if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
280297a225e6SJoonsoo Kim 		pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2803698b1b30SVlastimil Babka }
2804698b1b30SVlastimil Babka 
280597a225e6SJoonsoo Kim void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
2806698b1b30SVlastimil Babka {
2807698b1b30SVlastimil Babka 	if (!order)
2808698b1b30SVlastimil Babka 		return;
2809698b1b30SVlastimil Babka 
2810698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2811698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2812698b1b30SVlastimil Babka 
281397a225e6SJoonsoo Kim 	if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
281497a225e6SJoonsoo Kim 		pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
2815698b1b30SVlastimil Babka 
28166818600fSDavidlohr Bueso 	/*
28176818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
28186818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
28196818600fSDavidlohr Bueso 	 */
28206818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2821698b1b30SVlastimil Babka 		return;
2822698b1b30SVlastimil Babka 
2823698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2824698b1b30SVlastimil Babka 		return;
2825698b1b30SVlastimil Babka 
2826698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
282797a225e6SJoonsoo Kim 							highest_zoneidx);
2828698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2829698b1b30SVlastimil Babka }
2830698b1b30SVlastimil Babka 
2831698b1b30SVlastimil Babka /*
2832698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2833698b1b30SVlastimil Babka  * from the init process.
2834698b1b30SVlastimil Babka  */
2835698b1b30SVlastimil Babka static int kcompactd(void *p)
2836698b1b30SVlastimil Babka {
2837698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2838698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2839facdaa91SNitin Gupta 	unsigned int proactive_defer = 0;
2840698b1b30SVlastimil Babka 
2841698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2842698b1b30SVlastimil Babka 
2843698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2844698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2845698b1b30SVlastimil Babka 
2846698b1b30SVlastimil Babka 	set_freezable();
2847698b1b30SVlastimil Babka 
2848698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
284997a225e6SJoonsoo Kim 	pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2850698b1b30SVlastimil Babka 
2851698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2852eb414681SJohannes Weiner 		unsigned long pflags;
2853eb414681SJohannes Weiner 
2854698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2855facdaa91SNitin Gupta 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
2856facdaa91SNitin Gupta 			kcompactd_work_requested(pgdat),
2857facdaa91SNitin Gupta 			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
2858698b1b30SVlastimil Babka 
2859eb414681SJohannes Weiner 			psi_memstall_enter(&pflags);
2860698b1b30SVlastimil Babka 			kcompactd_do_work(pgdat);
2861eb414681SJohannes Weiner 			psi_memstall_leave(&pflags);
2862facdaa91SNitin Gupta 			continue;
2863facdaa91SNitin Gupta 		}
2864facdaa91SNitin Gupta 
2865facdaa91SNitin Gupta 		/* kcompactd wait timeout */
2866facdaa91SNitin Gupta 		if (should_proactive_compact_node(pgdat)) {
2867facdaa91SNitin Gupta 			unsigned int prev_score, score;
2868facdaa91SNitin Gupta 
2869facdaa91SNitin Gupta 			if (proactive_defer) {
2870facdaa91SNitin Gupta 				proactive_defer--;
2871facdaa91SNitin Gupta 				continue;
2872facdaa91SNitin Gupta 			}
2873facdaa91SNitin Gupta 			prev_score = fragmentation_score_node(pgdat);
2874facdaa91SNitin Gupta 			proactive_compact_node(pgdat);
2875facdaa91SNitin Gupta 			score = fragmentation_score_node(pgdat);
2876facdaa91SNitin Gupta 			/*
2877facdaa91SNitin Gupta 			 * Defer proactive compaction if the fragmentation
2878facdaa91SNitin Gupta 			 * score did not go down i.e. no progress made.
2879facdaa91SNitin Gupta 			 */
2880facdaa91SNitin Gupta 			proactive_defer = score < prev_score ?
2881facdaa91SNitin Gupta 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
2882facdaa91SNitin Gupta 		}
2883698b1b30SVlastimil Babka 	}
2884698b1b30SVlastimil Babka 
2885698b1b30SVlastimil Babka 	return 0;
2886698b1b30SVlastimil Babka }
2887698b1b30SVlastimil Babka 
2888698b1b30SVlastimil Babka /*
2889698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2890698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2891698b1b30SVlastimil Babka  */
2892698b1b30SVlastimil Babka int kcompactd_run(int nid)
2893698b1b30SVlastimil Babka {
2894698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2895698b1b30SVlastimil Babka 	int ret = 0;
2896698b1b30SVlastimil Babka 
2897698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2898698b1b30SVlastimil Babka 		return 0;
2899698b1b30SVlastimil Babka 
2900698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2901698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2902698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2903698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2904698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2905698b1b30SVlastimil Babka 	}
2906698b1b30SVlastimil Babka 	return ret;
2907698b1b30SVlastimil Babka }
2908698b1b30SVlastimil Babka 
2909698b1b30SVlastimil Babka /*
2910698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2911698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2912698b1b30SVlastimil Babka  */
2913698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2914698b1b30SVlastimil Babka {
2915698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2916698b1b30SVlastimil Babka 
2917698b1b30SVlastimil Babka 	if (kcompactd) {
2918698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2919698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2920698b1b30SVlastimil Babka 	}
2921698b1b30SVlastimil Babka }
2922698b1b30SVlastimil Babka 
2923698b1b30SVlastimil Babka /*
2924698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2925698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2926698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2927698b1b30SVlastimil Babka  * restore their cpu bindings.
2928698b1b30SVlastimil Babka  */
2929e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2930698b1b30SVlastimil Babka {
2931698b1b30SVlastimil Babka 	int nid;
2932698b1b30SVlastimil Babka 
2933698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2934698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2935698b1b30SVlastimil Babka 		const struct cpumask *mask;
2936698b1b30SVlastimil Babka 
2937698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2938698b1b30SVlastimil Babka 
2939698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2940698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2941698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2942698b1b30SVlastimil Babka 	}
2943e46b1db2SAnna-Maria Gleixner 	return 0;
2944698b1b30SVlastimil Babka }
2945698b1b30SVlastimil Babka 
2946698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2947698b1b30SVlastimil Babka {
2948698b1b30SVlastimil Babka 	int nid;
2949e46b1db2SAnna-Maria Gleixner 	int ret;
2950e46b1db2SAnna-Maria Gleixner 
2951e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2952e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2953e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2954e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2955e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2956e46b1db2SAnna-Maria Gleixner 		return ret;
2957e46b1db2SAnna-Maria Gleixner 	}
2958698b1b30SVlastimil Babka 
2959698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2960698b1b30SVlastimil Babka 		kcompactd_run(nid);
2961698b1b30SVlastimil Babka 	return 0;
2962698b1b30SVlastimil Babka }
2963698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2964698b1b30SVlastimil Babka 
2965ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2966