xref: /openbmc/linux/mm/compaction.c (revision 5f438eee8f2e972e910b558a1a243def508b6a35)
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 
53748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
54748446bbSMel Gorman {
55748446bbSMel Gorman 	struct page *page, *next;
566bace090SVlastimil Babka 	unsigned long high_pfn = 0;
57748446bbSMel Gorman 
58748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
596bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
60748446bbSMel Gorman 		list_del(&page->lru);
61748446bbSMel Gorman 		__free_page(page);
626bace090SVlastimil Babka 		if (pfn > high_pfn)
636bace090SVlastimil Babka 			high_pfn = pfn;
64748446bbSMel Gorman 	}
65748446bbSMel Gorman 
666bace090SVlastimil Babka 	return high_pfn;
67748446bbSMel Gorman }
68748446bbSMel Gorman 
694469ab98SMel Gorman static void split_map_pages(struct list_head *list)
70ff9543fdSMichal Nazarewicz {
7166c64223SJoonsoo Kim 	unsigned int i, order, nr_pages;
7266c64223SJoonsoo Kim 	struct page *page, *next;
7366c64223SJoonsoo Kim 	LIST_HEAD(tmp_list);
74ff9543fdSMichal Nazarewicz 
7566c64223SJoonsoo Kim 	list_for_each_entry_safe(page, next, list, lru) {
7666c64223SJoonsoo Kim 		list_del(&page->lru);
7766c64223SJoonsoo Kim 
7866c64223SJoonsoo Kim 		order = page_private(page);
7966c64223SJoonsoo Kim 		nr_pages = 1 << order;
8066c64223SJoonsoo Kim 
8146f24fd8SJoonsoo Kim 		post_alloc_hook(page, order, __GFP_MOVABLE);
8266c64223SJoonsoo Kim 		if (order)
8366c64223SJoonsoo Kim 			split_page(page, order);
8466c64223SJoonsoo Kim 
8566c64223SJoonsoo Kim 		for (i = 0; i < nr_pages; i++) {
8666c64223SJoonsoo Kim 			list_add(&page->lru, &tmp_list);
8766c64223SJoonsoo Kim 			page++;
88ff9543fdSMichal Nazarewicz 		}
89ff9543fdSMichal Nazarewicz 	}
90ff9543fdSMichal Nazarewicz 
9166c64223SJoonsoo Kim 	list_splice(&tmp_list, list);
9266c64223SJoonsoo Kim }
9366c64223SJoonsoo Kim 
94bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
9524e2716fSJoonsoo Kim 
96bda807d4SMinchan Kim int PageMovable(struct page *page)
97bda807d4SMinchan Kim {
98bda807d4SMinchan Kim 	struct address_space *mapping;
99bda807d4SMinchan Kim 
100bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
101bda807d4SMinchan Kim 	if (!__PageMovable(page))
102bda807d4SMinchan Kim 		return 0;
103bda807d4SMinchan Kim 
104bda807d4SMinchan Kim 	mapping = page_mapping(page);
105bda807d4SMinchan Kim 	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
106bda807d4SMinchan Kim 		return 1;
107bda807d4SMinchan Kim 
108bda807d4SMinchan Kim 	return 0;
109bda807d4SMinchan Kim }
110bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable);
111bda807d4SMinchan Kim 
112bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping)
113bda807d4SMinchan Kim {
114bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
115bda807d4SMinchan Kim 	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
116bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
117bda807d4SMinchan Kim }
118bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable);
119bda807d4SMinchan Kim 
120bda807d4SMinchan Kim void __ClearPageMovable(struct page *page)
121bda807d4SMinchan Kim {
122bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
123bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageMovable(page), page);
124bda807d4SMinchan Kim 	/*
125bda807d4SMinchan Kim 	 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
126bda807d4SMinchan Kim 	 * flag so that VM can catch up released page by driver after isolation.
127bda807d4SMinchan Kim 	 * With it, VM migration doesn't try to put it back.
128bda807d4SMinchan Kim 	 */
129bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)page->mapping &
130bda807d4SMinchan Kim 				PAGE_MAPPING_MOVABLE);
131bda807d4SMinchan Kim }
132bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable);
133bda807d4SMinchan Kim 
13424e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
13524e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
13624e2716fSJoonsoo Kim 
13724e2716fSJoonsoo Kim /*
13824e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
13924e2716fSJoonsoo Kim  * allocation success. 1 << compact_defer_limit compactions are skipped up
14024e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
14124e2716fSJoonsoo Kim  */
14224e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
14324e2716fSJoonsoo Kim {
14424e2716fSJoonsoo Kim 	zone->compact_considered = 0;
14524e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
14624e2716fSJoonsoo Kim 
14724e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
14824e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
14924e2716fSJoonsoo Kim 
15024e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
15124e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
15224e2716fSJoonsoo Kim 
15324e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
15424e2716fSJoonsoo Kim }
15524e2716fSJoonsoo Kim 
15624e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
15724e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
15824e2716fSJoonsoo Kim {
15924e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
16024e2716fSJoonsoo Kim 
16124e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
16224e2716fSJoonsoo Kim 		return false;
16324e2716fSJoonsoo Kim 
16424e2716fSJoonsoo Kim 	/* Avoid possible overflow */
16524e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
16624e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
16724e2716fSJoonsoo Kim 
16824e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
16924e2716fSJoonsoo Kim 		return false;
17024e2716fSJoonsoo Kim 
17124e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
17224e2716fSJoonsoo Kim 
17324e2716fSJoonsoo Kim 	return true;
17424e2716fSJoonsoo Kim }
17524e2716fSJoonsoo Kim 
17624e2716fSJoonsoo Kim /*
17724e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
17824e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
17924e2716fSJoonsoo Kim  * expected to succeed.
18024e2716fSJoonsoo Kim  */
18124e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
18224e2716fSJoonsoo Kim 		bool alloc_success)
18324e2716fSJoonsoo Kim {
18424e2716fSJoonsoo Kim 	if (alloc_success) {
18524e2716fSJoonsoo Kim 		zone->compact_considered = 0;
18624e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
18724e2716fSJoonsoo Kim 	}
18824e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
18924e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
19024e2716fSJoonsoo Kim 
19124e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
19224e2716fSJoonsoo Kim }
19324e2716fSJoonsoo Kim 
19424e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
19524e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
19624e2716fSJoonsoo Kim {
19724e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
19824e2716fSJoonsoo Kim 		return false;
19924e2716fSJoonsoo Kim 
20024e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
20124e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
20224e2716fSJoonsoo Kim }
20324e2716fSJoonsoo Kim 
204bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
205bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
206bb13ffebSMel Gorman 					struct page *page)
207bb13ffebSMel Gorman {
208bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
209bb13ffebSMel Gorman 		return true;
210bb13ffebSMel Gorman 
211bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
212bb13ffebSMel Gorman }
213bb13ffebSMel Gorman 
21402333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
21502333641SVlastimil Babka {
21602333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
21702333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
218623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
21906b6640aSVlastimil Babka 				pageblock_start_pfn(zone_end_pfn(zone) - 1);
22002333641SVlastimil Babka }
22102333641SVlastimil Babka 
222bb13ffebSMel Gorman /*
223b527cfe5SVlastimil Babka  * Compound pages of >= pageblock_order should consistenly be skipped until
224b527cfe5SVlastimil Babka  * released. It is always pointless to compact pages of such order (if they are
225b527cfe5SVlastimil Babka  * migratable), and the pageblocks they occupy cannot contain any free pages.
22621dc7e02SDavid Rientjes  */
227b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page)
22821dc7e02SDavid Rientjes {
229b527cfe5SVlastimil Babka 	if (!PageCompound(page))
23021dc7e02SDavid Rientjes 		return false;
231b527cfe5SVlastimil Babka 
232b527cfe5SVlastimil Babka 	page = compound_head(page);
233b527cfe5SVlastimil Babka 
234b527cfe5SVlastimil Babka 	if (compound_order(page) >= pageblock_order)
23521dc7e02SDavid Rientjes 		return true;
236b527cfe5SVlastimil Babka 
237b527cfe5SVlastimil Babka 	return false;
23821dc7e02SDavid Rientjes }
23921dc7e02SDavid Rientjes 
240e332f741SMel Gorman static bool
241e332f741SMel Gorman __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
242e332f741SMel Gorman 							bool check_target)
243e332f741SMel Gorman {
244e332f741SMel Gorman 	struct page *page = pfn_to_online_page(pfn);
245e332f741SMel Gorman 	struct page *end_page;
246e332f741SMel Gorman 	unsigned long block_pfn;
247e332f741SMel Gorman 
248e332f741SMel Gorman 	if (!page)
249e332f741SMel Gorman 		return false;
250e332f741SMel Gorman 	if (zone != page_zone(page))
251e332f741SMel Gorman 		return false;
252e332f741SMel Gorman 	if (pageblock_skip_persistent(page))
253e332f741SMel Gorman 		return false;
254e332f741SMel Gorman 
255e332f741SMel Gorman 	/*
256e332f741SMel Gorman 	 * If skip is already cleared do no further checking once the
257e332f741SMel Gorman 	 * restart points have been set.
258e332f741SMel Gorman 	 */
259e332f741SMel Gorman 	if (check_source && check_target && !get_pageblock_skip(page))
260e332f741SMel Gorman 		return true;
261e332f741SMel Gorman 
262e332f741SMel Gorman 	/*
263e332f741SMel Gorman 	 * If clearing skip for the target scanner, do not select a
264e332f741SMel Gorman 	 * non-movable pageblock as the starting point.
265e332f741SMel Gorman 	 */
266e332f741SMel Gorman 	if (!check_source && check_target &&
267e332f741SMel Gorman 	    get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
268e332f741SMel Gorman 		return false;
269e332f741SMel Gorman 
270e332f741SMel Gorman 	/*
271e332f741SMel Gorman 	 * Only clear the hint if a sample indicates there is either a
272e332f741SMel Gorman 	 * free page or an LRU page in the block. One or other condition
273e332f741SMel Gorman 	 * is necessary for the block to be a migration source/target.
274e332f741SMel Gorman 	 */
275e332f741SMel Gorman 	block_pfn = pageblock_start_pfn(pfn);
276e332f741SMel Gorman 	pfn = max(block_pfn, zone->zone_start_pfn);
277e332f741SMel Gorman 	page = pfn_to_page(pfn);
278e332f741SMel Gorman 	if (zone != page_zone(page))
279e332f741SMel Gorman 		return false;
280e332f741SMel Gorman 	pfn = block_pfn + pageblock_nr_pages;
281e332f741SMel Gorman 	pfn = min(pfn, zone_end_pfn(zone));
282e332f741SMel Gorman 	end_page = pfn_to_page(pfn);
283e332f741SMel Gorman 
284e332f741SMel Gorman 	do {
285e332f741SMel Gorman 		if (pfn_valid_within(pfn)) {
286e332f741SMel Gorman 			if (check_source && PageLRU(page)) {
287e332f741SMel Gorman 				clear_pageblock_skip(page);
288e332f741SMel Gorman 				return true;
289e332f741SMel Gorman 			}
290e332f741SMel Gorman 
291e332f741SMel Gorman 			if (check_target && PageBuddy(page)) {
292e332f741SMel Gorman 				clear_pageblock_skip(page);
293e332f741SMel Gorman 				return true;
294e332f741SMel Gorman 			}
295e332f741SMel Gorman 		}
296e332f741SMel Gorman 
297e332f741SMel Gorman 		page += (1 << PAGE_ALLOC_COSTLY_ORDER);
298e332f741SMel Gorman 		pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
299e332f741SMel Gorman 	} while (page < end_page);
300e332f741SMel Gorman 
301e332f741SMel Gorman 	return false;
302e332f741SMel Gorman }
303e332f741SMel Gorman 
30421dc7e02SDavid Rientjes /*
305bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
306bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
307bb13ffebSMel Gorman  * meet.
308bb13ffebSMel Gorman  */
30962997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
310bb13ffebSMel Gorman {
311e332f741SMel Gorman 	unsigned long migrate_pfn = zone->zone_start_pfn;
312e332f741SMel Gorman 	unsigned long free_pfn = zone_end_pfn(zone);
313e332f741SMel Gorman 	unsigned long reset_migrate = free_pfn;
314e332f741SMel Gorman 	unsigned long reset_free = migrate_pfn;
315e332f741SMel Gorman 	bool source_set = false;
316e332f741SMel Gorman 	bool free_set = false;
317e332f741SMel Gorman 
318e332f741SMel Gorman 	if (!zone->compact_blockskip_flush)
319e332f741SMel Gorman 		return;
320bb13ffebSMel Gorman 
32162997027SMel Gorman 	zone->compact_blockskip_flush = false;
322bb13ffebSMel Gorman 
323e332f741SMel Gorman 	/*
324e332f741SMel Gorman 	 * Walk the zone and update pageblock skip information. Source looks
325e332f741SMel Gorman 	 * for PageLRU while target looks for PageBuddy. When the scanner
326e332f741SMel Gorman 	 * is found, both PageBuddy and PageLRU are checked as the pageblock
327e332f741SMel Gorman 	 * is suitable as both source and target.
328e332f741SMel Gorman 	 */
329e332f741SMel Gorman 	for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
330e332f741SMel Gorman 					free_pfn -= pageblock_nr_pages) {
331bb13ffebSMel Gorman 		cond_resched();
332bb13ffebSMel Gorman 
333e332f741SMel Gorman 		/* Update the migrate PFN */
334e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
335e332f741SMel Gorman 		    migrate_pfn < reset_migrate) {
336e332f741SMel Gorman 			source_set = true;
337e332f741SMel Gorman 			reset_migrate = migrate_pfn;
338e332f741SMel Gorman 			zone->compact_init_migrate_pfn = reset_migrate;
339e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[0] = reset_migrate;
340e332f741SMel Gorman 			zone->compact_cached_migrate_pfn[1] = reset_migrate;
341bb13ffebSMel Gorman 		}
34202333641SVlastimil Babka 
343e332f741SMel Gorman 		/* Update the free PFN */
344e332f741SMel Gorman 		if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
345e332f741SMel Gorman 		    free_pfn > reset_free) {
346e332f741SMel Gorman 			free_set = true;
347e332f741SMel Gorman 			reset_free = free_pfn;
348e332f741SMel Gorman 			zone->compact_init_free_pfn = reset_free;
349e332f741SMel Gorman 			zone->compact_cached_free_pfn = reset_free;
350e332f741SMel Gorman 		}
351e332f741SMel Gorman 	}
352e332f741SMel Gorman 
353e332f741SMel Gorman 	/* Leave no distance if no suitable block was reset */
354e332f741SMel Gorman 	if (reset_migrate >= reset_free) {
355e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[0] = migrate_pfn;
356e332f741SMel Gorman 		zone->compact_cached_migrate_pfn[1] = migrate_pfn;
357e332f741SMel Gorman 		zone->compact_cached_free_pfn = free_pfn;
358e332f741SMel Gorman 	}
359bb13ffebSMel Gorman }
360bb13ffebSMel Gorman 
36162997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
36262997027SMel Gorman {
36362997027SMel Gorman 	int zoneid;
36462997027SMel Gorman 
36562997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
36662997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
36762997027SMel Gorman 		if (!populated_zone(zone))
36862997027SMel Gorman 			continue;
36962997027SMel Gorman 
37062997027SMel Gorman 		/* Only flush if a full compaction finished recently */
37162997027SMel Gorman 		if (zone->compact_blockskip_flush)
37262997027SMel Gorman 			__reset_isolation_suitable(zone);
37362997027SMel Gorman 	}
37462997027SMel Gorman }
37562997027SMel Gorman 
376bb13ffebSMel Gorman /*
377e380bebeSMel Gorman  * Sets the pageblock skip bit if it was clear. Note that this is a hint as
378e380bebeSMel Gorman  * locks are not required for read/writers. Returns true if it was already set.
379e380bebeSMel Gorman  */
380e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
381e380bebeSMel Gorman 							unsigned long pfn)
382e380bebeSMel Gorman {
383e380bebeSMel Gorman 	bool skip;
384e380bebeSMel Gorman 
385e380bebeSMel Gorman 	/* Do no update if skip hint is being ignored */
386e380bebeSMel Gorman 	if (cc->ignore_skip_hint)
387e380bebeSMel Gorman 		return false;
388e380bebeSMel Gorman 
389e380bebeSMel Gorman 	if (!IS_ALIGNED(pfn, pageblock_nr_pages))
390e380bebeSMel Gorman 		return false;
391e380bebeSMel Gorman 
392e380bebeSMel Gorman 	skip = get_pageblock_skip(page);
393e380bebeSMel Gorman 	if (!skip && !cc->no_set_skip_hint)
394e380bebeSMel Gorman 		set_pageblock_skip(page);
395e380bebeSMel Gorman 
396e380bebeSMel Gorman 	return skip;
397e380bebeSMel Gorman }
398e380bebeSMel Gorman 
399e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
400e380bebeSMel Gorman {
401e380bebeSMel Gorman 	struct zone *zone = cc->zone;
402e380bebeSMel Gorman 
403e380bebeSMel Gorman 	pfn = pageblock_end_pfn(pfn);
404e380bebeSMel Gorman 
405e380bebeSMel Gorman 	/* Set for isolation rather than compaction */
406e380bebeSMel Gorman 	if (cc->no_set_skip_hint)
407e380bebeSMel Gorman 		return;
408e380bebeSMel Gorman 
409e380bebeSMel Gorman 	if (pfn > zone->compact_cached_migrate_pfn[0])
410e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[0] = pfn;
411e380bebeSMel Gorman 	if (cc->mode != MIGRATE_ASYNC &&
412e380bebeSMel Gorman 	    pfn > zone->compact_cached_migrate_pfn[1])
413e380bebeSMel Gorman 		zone->compact_cached_migrate_pfn[1] = pfn;
414e380bebeSMel Gorman }
415e380bebeSMel Gorman 
416e380bebeSMel Gorman /*
417bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
41862997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
419bb13ffebSMel Gorman  */
420c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
421d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
422bb13ffebSMel Gorman {
423c89511abSMel Gorman 	struct zone *zone = cc->zone;
4246815bf3fSJoonsoo Kim 
4252583d671SVlastimil Babka 	if (cc->no_set_skip_hint)
4266815bf3fSJoonsoo Kim 		return;
4276815bf3fSJoonsoo Kim 
428bb13ffebSMel Gorman 	if (!page)
429bb13ffebSMel Gorman 		return;
430bb13ffebSMel Gorman 
431bb13ffebSMel Gorman 	set_pageblock_skip(page);
432c89511abSMel Gorman 
43335979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
43435979ef3SDavid Rientjes 	if (pfn < zone->compact_cached_free_pfn)
435c89511abSMel Gorman 		zone->compact_cached_free_pfn = pfn;
436c89511abSMel Gorman }
437bb13ffebSMel Gorman #else
438bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
439bb13ffebSMel Gorman 					struct page *page)
440bb13ffebSMel Gorman {
441bb13ffebSMel Gorman 	return true;
442bb13ffebSMel Gorman }
443bb13ffebSMel Gorman 
444b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page)
44521dc7e02SDavid Rientjes {
44621dc7e02SDavid Rientjes 	return false;
44721dc7e02SDavid Rientjes }
44821dc7e02SDavid Rientjes 
44921dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc,
450d097a6f6SMel Gorman 			struct page *page, unsigned long pfn)
451bb13ffebSMel Gorman {
452bb13ffebSMel Gorman }
453e380bebeSMel Gorman 
454e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
455e380bebeSMel Gorman {
456e380bebeSMel Gorman }
457e380bebeSMel Gorman 
458e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page,
459e380bebeSMel Gorman 							unsigned long pfn)
460e380bebeSMel Gorman {
461e380bebeSMel Gorman 	return false;
462e380bebeSMel Gorman }
463bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
464bb13ffebSMel Gorman 
4651f9efdefSVlastimil Babka /*
4668b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
467cb2dcaf0SMel Gorman  * very heavily contended. For async compaction, trylock and record if the
468cb2dcaf0SMel Gorman  * lock is contended. The lock will still be acquired but compaction will
469cb2dcaf0SMel Gorman  * abort when the current block is finished regardless of success rate.
470cb2dcaf0SMel Gorman  * Sync compaction acquires the lock.
4718b44d279SVlastimil Babka  *
472cb2dcaf0SMel Gorman  * Always returns true which makes it easier to track lock state in callers.
4731f9efdefSVlastimil Babka  */
474cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
4758b44d279SVlastimil Babka 						struct compact_control *cc)
4768b44d279SVlastimil Babka {
477cb2dcaf0SMel Gorman 	/* Track if the lock is contended in async mode */
478cb2dcaf0SMel Gorman 	if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
479cb2dcaf0SMel Gorman 		if (spin_trylock_irqsave(lock, *flags))
480cb2dcaf0SMel Gorman 			return true;
481cb2dcaf0SMel Gorman 
482c3486f53SVlastimil Babka 		cc->contended = true;
4838b44d279SVlastimil Babka 	}
4841f9efdefSVlastimil Babka 
485cb2dcaf0SMel Gorman 	spin_lock_irqsave(lock, *flags);
4868b44d279SVlastimil Babka 	return true;
4872a1402aaSMel Gorman }
4882a1402aaSMel Gorman 
48985aa125fSMichal Nazarewicz /*
490c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
4918b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
4928b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
4938b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
4948b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
4958b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
4968b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
4978b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
498c67fe375SMel Gorman  *
4998b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
5008b44d279SVlastimil Babka  *		async compaction due to need_resched()
5018b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
5028b44d279SVlastimil Babka  *		scheduled)
503c67fe375SMel Gorman  */
5048b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
5058b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
506c67fe375SMel Gorman {
5078b44d279SVlastimil Babka 	if (*locked) {
5088b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
5098b44d279SVlastimil Babka 		*locked = false;
510c67fe375SMel Gorman 	}
511c67fe375SMel Gorman 
5128b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
513c3486f53SVlastimil Babka 		cc->contended = true;
5148b44d279SVlastimil Babka 		return true;
5158b44d279SVlastimil Babka 	}
5168b44d279SVlastimil Babka 
517cf66f070SMel Gorman 	cond_resched();
518be976572SVlastimil Babka 
519be976572SVlastimil Babka 	return false;
520be976572SVlastimil Babka }
521be976572SVlastimil Babka 
522c67fe375SMel Gorman /*
5239e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
5249e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
5259e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
52685aa125fSMichal Nazarewicz  */
527f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
528e14c720eSVlastimil Babka 				unsigned long *start_pfn,
52985aa125fSMichal Nazarewicz 				unsigned long end_pfn,
53085aa125fSMichal Nazarewicz 				struct list_head *freelist,
5314fca9730SMel Gorman 				unsigned int stride,
53285aa125fSMichal Nazarewicz 				bool strict)
533748446bbSMel Gorman {
534b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
535d097a6f6SMel Gorman 	struct page *cursor;
536b8b2d825SXiubo Li 	unsigned long flags = 0;
537f40d1e42SMel Gorman 	bool locked = false;
538e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
53966c64223SJoonsoo Kim 	unsigned int order;
540748446bbSMel Gorman 
5414fca9730SMel Gorman 	/* Strict mode is for isolation, speed is secondary */
5424fca9730SMel Gorman 	if (strict)
5434fca9730SMel Gorman 		stride = 1;
5444fca9730SMel Gorman 
545748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
546748446bbSMel Gorman 
547f40d1e42SMel Gorman 	/* Isolate free pages. */
5484fca9730SMel Gorman 	for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
54966c64223SJoonsoo Kim 		int isolated;
550748446bbSMel Gorman 		struct page *page = cursor;
551748446bbSMel Gorman 
5528b44d279SVlastimil Babka 		/*
5538b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
5548b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
5558b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
5568b44d279SVlastimil Babka 		 */
5578b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
5588b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
5598b44d279SVlastimil Babka 								&locked, cc))
5608b44d279SVlastimil Babka 			break;
5618b44d279SVlastimil Babka 
562b7aba698SMel Gorman 		nr_scanned++;
563f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
5642af120bcSLaura Abbott 			goto isolate_fail;
5652af120bcSLaura Abbott 
5669fcd6d2eSVlastimil Babka 		/*
5679fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
5689fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
5699fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
5709fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
5719fcd6d2eSVlastimil Babka 		 */
5729fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
57321dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
5749fcd6d2eSVlastimil Babka 
575d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER)) {
57621dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
57721dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
5789fcd6d2eSVlastimil Babka 			}
5799fcd6d2eSVlastimil Babka 			goto isolate_fail;
5809fcd6d2eSVlastimil Babka 		}
5819fcd6d2eSVlastimil Babka 
582f40d1e42SMel Gorman 		if (!PageBuddy(page))
5832af120bcSLaura Abbott 			goto isolate_fail;
584f40d1e42SMel Gorman 
585f40d1e42SMel Gorman 		/*
58669b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
58769b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
58869b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
58969b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
59069b7189fSVlastimil Babka 		 * recheck as well.
59169b7189fSVlastimil Babka 		 */
59269b7189fSVlastimil Babka 		if (!locked) {
593cb2dcaf0SMel Gorman 			locked = compact_lock_irqsave(&cc->zone->lock,
5948b44d279SVlastimil Babka 								&flags, cc);
595f40d1e42SMel Gorman 
596f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
597f40d1e42SMel Gorman 			if (!PageBuddy(page))
5982af120bcSLaura Abbott 				goto isolate_fail;
59969b7189fSVlastimil Babka 		}
600748446bbSMel Gorman 
60166c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
60266c64223SJoonsoo Kim 		order = page_order(page);
60366c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
604a4f04f2cSDavid Rientjes 		if (!isolated)
605a4f04f2cSDavid Rientjes 			break;
60666c64223SJoonsoo Kim 		set_page_private(page, order);
607a4f04f2cSDavid Rientjes 
608748446bbSMel Gorman 		total_isolated += isolated;
609a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
61066c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
61166c64223SJoonsoo Kim 
612a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
613932ff6bbSJoonsoo Kim 			blockpfn += isolated;
614932ff6bbSJoonsoo Kim 			break;
615932ff6bbSJoonsoo Kim 		}
616a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
617748446bbSMel Gorman 		blockpfn += isolated - 1;
618748446bbSMel Gorman 		cursor += isolated - 1;
6192af120bcSLaura Abbott 		continue;
6202af120bcSLaura Abbott 
6212af120bcSLaura Abbott isolate_fail:
6222af120bcSLaura Abbott 		if (strict)
6232af120bcSLaura Abbott 			break;
6242af120bcSLaura Abbott 		else
6252af120bcSLaura Abbott 			continue;
6262af120bcSLaura Abbott 
627748446bbSMel Gorman 	}
628748446bbSMel Gorman 
629a4f04f2cSDavid Rientjes 	if (locked)
630a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
631a4f04f2cSDavid Rientjes 
6329fcd6d2eSVlastimil Babka 	/*
6339fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
6349fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
6359fcd6d2eSVlastimil Babka 	 */
6369fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
6379fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
6389fcd6d2eSVlastimil Babka 
639e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
640e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
641e34d85f0SJoonsoo Kim 
642e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
643e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
644e14c720eSVlastimil Babka 
645f40d1e42SMel Gorman 	/*
646f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
647f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
648f40d1e42SMel Gorman 	 * returned and CMA will fail.
649f40d1e42SMel Gorman 	 */
6502af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
651f40d1e42SMel Gorman 		total_isolated = 0;
652f40d1e42SMel Gorman 
6537f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
654397487dbSMel Gorman 	if (total_isolated)
655010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
656748446bbSMel Gorman 	return total_isolated;
657748446bbSMel Gorman }
658748446bbSMel Gorman 
65985aa125fSMichal Nazarewicz /**
66085aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
661e8b098fcSMike Rapoport  * @cc:        Compaction control structure.
66285aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
66385aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
66485aa125fSMichal Nazarewicz  *
66585aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
66685aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
66785aa125fSMichal Nazarewicz  * undo its actions and return zero.
66885aa125fSMichal Nazarewicz  *
66985aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
67085aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
67185aa125fSMichal Nazarewicz  * a free page).
67285aa125fSMichal Nazarewicz  */
673ff9543fdSMichal Nazarewicz unsigned long
674bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
675bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
67685aa125fSMichal Nazarewicz {
677e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
67885aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
67985aa125fSMichal Nazarewicz 
6807d49d886SVlastimil Babka 	pfn = start_pfn;
68106b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
682e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
683e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
68406b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
6857d49d886SVlastimil Babka 
6867d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
687e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
6887d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
689e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
690e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
6917d49d886SVlastimil Babka 
69285aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
69385aa125fSMichal Nazarewicz 
69458420016SJoonsoo Kim 		/*
69558420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
69658420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
69758420016SJoonsoo Kim 		 * scanning range to right one.
69858420016SJoonsoo Kim 		 */
69958420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
70006b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
70106b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
70258420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
70358420016SJoonsoo Kim 		}
70458420016SJoonsoo Kim 
705e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
706e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
7077d49d886SVlastimil Babka 			break;
7087d49d886SVlastimil Babka 
709e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
7104fca9730SMel Gorman 					block_end_pfn, &freelist, 0, true);
71185aa125fSMichal Nazarewicz 
71285aa125fSMichal Nazarewicz 		/*
71385aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
71485aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
71585aa125fSMichal Nazarewicz 		 * non-free pages).
71685aa125fSMichal Nazarewicz 		 */
71785aa125fSMichal Nazarewicz 		if (!isolated)
71885aa125fSMichal Nazarewicz 			break;
71985aa125fSMichal Nazarewicz 
72085aa125fSMichal Nazarewicz 		/*
72185aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
72285aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
72385aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
72485aa125fSMichal Nazarewicz 		 */
72585aa125fSMichal Nazarewicz 	}
72685aa125fSMichal Nazarewicz 
72766c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
7284469ab98SMel Gorman 	split_map_pages(&freelist);
72985aa125fSMichal Nazarewicz 
73085aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
73185aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
73285aa125fSMichal Nazarewicz 		release_freepages(&freelist);
73385aa125fSMichal Nazarewicz 		return 0;
73485aa125fSMichal Nazarewicz 	}
73585aa125fSMichal Nazarewicz 
73685aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
73785aa125fSMichal Nazarewicz 	return pfn;
73885aa125fSMichal Nazarewicz }
73985aa125fSMichal Nazarewicz 
740748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
741*5f438eeeSAndrey Ryabinin static bool too_many_isolated(pg_data_t *pgdat)
742748446bbSMel Gorman {
743bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
744748446bbSMel Gorman 
745*5f438eeeSAndrey Ryabinin 	inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
746*5f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_INACTIVE_ANON);
747*5f438eeeSAndrey Ryabinin 	active = node_page_state(pgdat, NR_ACTIVE_FILE) +
748*5f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ACTIVE_ANON);
749*5f438eeeSAndrey Ryabinin 	isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
750*5f438eeeSAndrey Ryabinin 			node_page_state(pgdat, NR_ISOLATED_ANON);
751748446bbSMel Gorman 
752bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
753748446bbSMel Gorman }
754748446bbSMel Gorman 
7552fe86e00SMichal Nazarewicz /**
756edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
757edc2ca61SVlastimil Babka  *				  a single pageblock
7582fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
759edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
760edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
761edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
7622fe86e00SMichal Nazarewicz  *
7632fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
764edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
765edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
766edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
767edc2ca61SVlastimil Babka  * than end_pfn).
7682fe86e00SMichal Nazarewicz  *
769edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
770edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
771edc2ca61SVlastimil Babka  * is neither read nor updated.
772748446bbSMel Gorman  */
773edc2ca61SVlastimil Babka static unsigned long
774edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
775edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
776748446bbSMel Gorman {
777*5f438eeeSAndrey Ryabinin 	pg_data_t *pgdat = cc->zone->zone_pgdat;
778b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
779fa9add64SHugh Dickins 	struct lruvec *lruvec;
780b8b2d825SXiubo Li 	unsigned long flags = 0;
7812a1402aaSMel Gorman 	bool locked = false;
782bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
783e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
784fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
785fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
786e380bebeSMel Gorman 	bool skip_updated = false;
787748446bbSMel Gorman 
788748446bbSMel Gorman 	/*
789748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
790748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
791748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
792748446bbSMel Gorman 	 */
793*5f438eeeSAndrey Ryabinin 	while (unlikely(too_many_isolated(pgdat))) {
794f9e35b3bSMel Gorman 		/* async migration should just abort */
795e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
7962fe86e00SMichal Nazarewicz 			return 0;
797f9e35b3bSMel Gorman 
798748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
799748446bbSMel Gorman 
800748446bbSMel Gorman 		if (fatal_signal_pending(current))
8012fe86e00SMichal Nazarewicz 			return 0;
802748446bbSMel Gorman 	}
803748446bbSMel Gorman 
804cf66f070SMel Gorman 	cond_resched();
805aeef4b83SDavid Rientjes 
806fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
807fdd048e1SVlastimil Babka 		skip_on_failure = true;
808fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
809fdd048e1SVlastimil Babka 	}
810fdd048e1SVlastimil Babka 
811748446bbSMel Gorman 	/* Time to isolate some pages for migration */
812748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
81329c0dde8SVlastimil Babka 
814fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
815fdd048e1SVlastimil Babka 			/*
816fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
817fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
818fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
819fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
820fdd048e1SVlastimil Babka 			 */
821fdd048e1SVlastimil Babka 			if (nr_isolated)
822fdd048e1SVlastimil Babka 				break;
823fdd048e1SVlastimil Babka 
824fdd048e1SVlastimil Babka 			/*
825fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
826fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
827fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
828fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
829fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
830fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
831fdd048e1SVlastimil Babka 			 * previous loop iteration.
832fdd048e1SVlastimil Babka 			 */
833fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
834fdd048e1SVlastimil Babka 		}
835fdd048e1SVlastimil Babka 
8368b44d279SVlastimil Babka 		/*
8378b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
8388b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
8398b44d279SVlastimil Babka 		 * if contended.
8408b44d279SVlastimil Babka 		 */
8418b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
842f4b7e272SAndrey Ryabinin 		    && compact_unlock_should_abort(&pgdat->lru_lock,
843f4b7e272SAndrey Ryabinin 					    flags, &locked, cc))
8448b44d279SVlastimil Babka 			break;
845b2eef8c0SAndrea Arcangeli 
846748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
847fdd048e1SVlastimil Babka 			goto isolate_fail;
848b7aba698SMel Gorman 		nr_scanned++;
849748446bbSMel Gorman 
850748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
851dc908600SMel Gorman 
852e380bebeSMel Gorman 		/*
853e380bebeSMel Gorman 		 * Check if the pageblock has already been marked skipped.
854e380bebeSMel Gorman 		 * Only the aligned PFN is checked as the caller isolates
855e380bebeSMel Gorman 		 * COMPACT_CLUSTER_MAX at a time so the second call must
856e380bebeSMel Gorman 		 * not falsely conclude that the block should be skipped.
857e380bebeSMel Gorman 		 */
858e380bebeSMel Gorman 		if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
859e380bebeSMel Gorman 			if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
860e380bebeSMel Gorman 				low_pfn = end_pfn;
861e380bebeSMel Gorman 				goto isolate_abort;
862e380bebeSMel Gorman 			}
863bb13ffebSMel Gorman 			valid_page = page;
864e380bebeSMel Gorman 		}
865bb13ffebSMel Gorman 
866c122b208SJoonsoo Kim 		/*
86799c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
86899c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
86999c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
87099c0fd5eSVlastimil Babka 		 * potential isolation targets.
8716c14466cSMel Gorman 		 */
87299c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
87399c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
87499c0fd5eSVlastimil Babka 
87599c0fd5eSVlastimil Babka 			/*
87699c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
87799c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
87899c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
87999c0fd5eSVlastimil Babka 			 */
88099c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
88199c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
882748446bbSMel Gorman 			continue;
88399c0fd5eSVlastimil Babka 		}
884748446bbSMel Gorman 
8859927af74SMel Gorman 		/*
88629c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
88729c0dde8SVlastimil Babka 		 * hugetlbfs are not to be compacted. We can potentially save
88829c0dde8SVlastimil Babka 		 * a lot of iterations if we skip them at once. The check is
88929c0dde8SVlastimil Babka 		 * racy, but we can consider only valid values and the only
89029c0dde8SVlastimil Babka 		 * danger is skipping too much.
891bc835011SAndrea Arcangeli 		 */
89229c0dde8SVlastimil Babka 		if (PageCompound(page)) {
89321dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
89429c0dde8SVlastimil Babka 
895d3c85badSVlastimil Babka 			if (likely(order < MAX_ORDER))
89621dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
897fdd048e1SVlastimil Babka 			goto isolate_fail;
8982a1402aaSMel Gorman 		}
8992a1402aaSMel Gorman 
900bda807d4SMinchan Kim 		/*
901bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
902bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
903bda807d4SMinchan Kim 		 * Skip any other type of page
904bda807d4SMinchan Kim 		 */
905bda807d4SMinchan Kim 		if (!PageLRU(page)) {
906bda807d4SMinchan Kim 			/*
907bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
908bda807d4SMinchan Kim 			 * to verify it under page_lock.
909bda807d4SMinchan Kim 			 */
910bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
911bda807d4SMinchan Kim 					!PageIsolated(page)) {
912bda807d4SMinchan Kim 				if (locked) {
913f4b7e272SAndrey Ryabinin 					spin_unlock_irqrestore(&pgdat->lru_lock,
914bda807d4SMinchan Kim 									flags);
915bda807d4SMinchan Kim 					locked = false;
916bda807d4SMinchan Kim 				}
917bda807d4SMinchan Kim 
9189e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
919bda807d4SMinchan Kim 					goto isolate_success;
920bda807d4SMinchan Kim 			}
921bda807d4SMinchan Kim 
922fdd048e1SVlastimil Babka 			goto isolate_fail;
923bda807d4SMinchan Kim 		}
92429c0dde8SVlastimil Babka 
925119d6d59SDavid Rientjes 		/*
926119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
927119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
928119d6d59SDavid Rientjes 		 * admittedly racy check.
929119d6d59SDavid Rientjes 		 */
930119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
931119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
932fdd048e1SVlastimil Babka 			goto isolate_fail;
933119d6d59SDavid Rientjes 
93473e64c51SMichal Hocko 		/*
93573e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
93673e64c51SMichal Hocko 		 * because those do not depend on fs locks.
93773e64c51SMichal Hocko 		 */
93873e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
93973e64c51SMichal Hocko 			goto isolate_fail;
94073e64c51SMichal Hocko 
94169b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
94269b7189fSVlastimil Babka 		if (!locked) {
943f4b7e272SAndrey Ryabinin 			locked = compact_lock_irqsave(&pgdat->lru_lock,
9448b44d279SVlastimil Babka 								&flags, cc);
945e380bebeSMel Gorman 
946e380bebeSMel Gorman 			/* Try get exclusive access under lock */
947e380bebeSMel Gorman 			if (!skip_updated) {
948e380bebeSMel Gorman 				skip_updated = true;
949e380bebeSMel Gorman 				if (test_and_set_skip(cc, page, low_pfn))
950e380bebeSMel Gorman 					goto isolate_abort;
951e380bebeSMel Gorman 			}
9522a1402aaSMel Gorman 
95329c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
9542a1402aaSMel Gorman 			if (!PageLRU(page))
955fdd048e1SVlastimil Babka 				goto isolate_fail;
95629c0dde8SVlastimil Babka 
95729c0dde8SVlastimil Babka 			/*
95829c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
95929c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
96029c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
96129c0dde8SVlastimil Babka 			 */
96229c0dde8SVlastimil Babka 			if (unlikely(PageCompound(page))) {
963d3c85badSVlastimil Babka 				low_pfn += (1UL << compound_order(page)) - 1;
964fdd048e1SVlastimil Babka 				goto isolate_fail;
965bc835011SAndrea Arcangeli 			}
96669b7189fSVlastimil Babka 		}
967bc835011SAndrea Arcangeli 
968f4b7e272SAndrey Ryabinin 		lruvec = mem_cgroup_page_lruvec(page, pgdat);
969fa9add64SHugh Dickins 
970748446bbSMel Gorman 		/* Try isolate the page */
971edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
972fdd048e1SVlastimil Babka 			goto isolate_fail;
973748446bbSMel Gorman 
97429c0dde8SVlastimil Babka 		VM_BUG_ON_PAGE(PageCompound(page), page);
975bc835011SAndrea Arcangeli 
976748446bbSMel Gorman 		/* Successfully isolated */
977fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
9786afcf8efSMing Ling 		inc_node_page_state(page,
9796afcf8efSMing Ling 				NR_ISOLATED_ANON + page_is_file_cache(page));
980b6c75016SJoonsoo Kim 
981b6c75016SJoonsoo Kim isolate_success:
982fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
983748446bbSMel Gorman 		cc->nr_migratepages++;
984b7aba698SMel Gorman 		nr_isolated++;
985748446bbSMel Gorman 
986804d3121SMel Gorman 		/*
987804d3121SMel Gorman 		 * Avoid isolating too much unless this block is being
988cb2dcaf0SMel Gorman 		 * rescanned (e.g. dirty/writeback pages, parallel allocation)
989cb2dcaf0SMel Gorman 		 * or a lock is contended. For contention, isolate quickly to
990cb2dcaf0SMel Gorman 		 * potentially remove one source of contention.
991804d3121SMel Gorman 		 */
992cb2dcaf0SMel Gorman 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
993cb2dcaf0SMel Gorman 		    !cc->rescan && !cc->contended) {
99431b8384aSHillf Danton 			++low_pfn;
995748446bbSMel Gorman 			break;
996748446bbSMel Gorman 		}
997fdd048e1SVlastimil Babka 
998fdd048e1SVlastimil Babka 		continue;
999fdd048e1SVlastimil Babka isolate_fail:
1000fdd048e1SVlastimil Babka 		if (!skip_on_failure)
1001fdd048e1SVlastimil Babka 			continue;
1002fdd048e1SVlastimil Babka 
1003fdd048e1SVlastimil Babka 		/*
1004fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
1005fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
1006fdd048e1SVlastimil Babka 		 * page anyway.
1007fdd048e1SVlastimil Babka 		 */
1008fdd048e1SVlastimil Babka 		if (nr_isolated) {
1009fdd048e1SVlastimil Babka 			if (locked) {
1010f4b7e272SAndrey Ryabinin 				spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1011fdd048e1SVlastimil Babka 				locked = false;
1012fdd048e1SVlastimil Babka 			}
1013fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
1014fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
1015fdd048e1SVlastimil Babka 			nr_isolated = 0;
1016fdd048e1SVlastimil Babka 		}
1017fdd048e1SVlastimil Babka 
1018fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
1019fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
1020fdd048e1SVlastimil Babka 			/*
1021fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
1022fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
1023fdd048e1SVlastimil Babka 			 */
1024fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
1025fdd048e1SVlastimil Babka 		}
102631b8384aSHillf Danton 	}
1027748446bbSMel Gorman 
102899c0fd5eSVlastimil Babka 	/*
102999c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
103099c0fd5eSVlastimil Babka 	 * the range to be scanned.
103199c0fd5eSVlastimil Babka 	 */
103299c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
103399c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
103499c0fd5eSVlastimil Babka 
1035e380bebeSMel Gorman isolate_abort:
1036c67fe375SMel Gorman 	if (locked)
1037f4b7e272SAndrey Ryabinin 		spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1038748446bbSMel Gorman 
103950b5b094SVlastimil Babka 	/*
1040804d3121SMel Gorman 	 * Updated the cached scanner pfn once the pageblock has been scanned
1041804d3121SMel Gorman 	 * Pages will either be migrated in which case there is no point
1042804d3121SMel Gorman 	 * scanning in the near future or migration failed in which case the
1043804d3121SMel Gorman 	 * failure reason may persist. The block is marked for skipping if
1044804d3121SMel Gorman 	 * there were no pages isolated in the block or if the block is
1045804d3121SMel Gorman 	 * rescanned twice in a row.
104650b5b094SVlastimil Babka 	 */
1047804d3121SMel Gorman 	if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
1048e380bebeSMel Gorman 		if (valid_page && !skip_updated)
1049e380bebeSMel Gorman 			set_pageblock_skip(valid_page);
1050e380bebeSMel Gorman 		update_cached_migrate(cc, low_pfn);
1051e380bebeSMel Gorman 	}
1052bb13ffebSMel Gorman 
1053e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1054e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
1055b7aba698SMel Gorman 
10567f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
1057397487dbSMel Gorman 	if (nr_isolated)
1058010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
1059397487dbSMel Gorman 
10602fe86e00SMichal Nazarewicz 	return low_pfn;
10612fe86e00SMichal Nazarewicz }
10622fe86e00SMichal Nazarewicz 
1063edc2ca61SVlastimil Babka /**
1064edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1065edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
1066edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
1067edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
1068edc2ca61SVlastimil Babka  *
1069edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
1070edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
1071edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
1072edc2ca61SVlastimil Babka  */
1073edc2ca61SVlastimil Babka unsigned long
1074edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1075edc2ca61SVlastimil Babka 							unsigned long end_pfn)
1076edc2ca61SVlastimil Babka {
1077e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
1078edc2ca61SVlastimil Babka 
1079edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
1080edc2ca61SVlastimil Babka 	pfn = start_pfn;
108106b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
1082e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
1083e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
108406b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
1085edc2ca61SVlastimil Babka 
1086edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
1087e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
1088edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
1089edc2ca61SVlastimil Babka 
1090edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
1091edc2ca61SVlastimil Babka 
1092e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
1093e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
1094edc2ca61SVlastimil Babka 			continue;
1095edc2ca61SVlastimil Babka 
1096edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1097edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
1098edc2ca61SVlastimil Babka 
109914af4a5eSHugh Dickins 		if (!pfn)
1100edc2ca61SVlastimil Babka 			break;
11016ea41c0cSJoonsoo Kim 
11026ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
11036ea41c0cSJoonsoo Kim 			break;
1104edc2ca61SVlastimil Babka 	}
1105edc2ca61SVlastimil Babka 
1106edc2ca61SVlastimil Babka 	return pfn;
1107edc2ca61SVlastimil Babka }
1108edc2ca61SVlastimil Babka 
1109ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1110ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1111018e9a49SAndrew Morton 
1112b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1113b682debdSVlastimil Babka 							struct page *page)
1114b682debdSVlastimil Babka {
1115282722b0SVlastimil Babka 	int block_mt;
1116282722b0SVlastimil Babka 
11179bebefd5SMel Gorman 	if (pageblock_skip_persistent(page))
11189bebefd5SMel Gorman 		return false;
11199bebefd5SMel Gorman 
1120282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1121b682debdSVlastimil Babka 		return true;
1122b682debdSVlastimil Babka 
1123282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1124282722b0SVlastimil Babka 
1125282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1126282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1127282722b0SVlastimil Babka 	else
1128282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1129b682debdSVlastimil Babka }
1130b682debdSVlastimil Babka 
1131018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
11329f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
11339f7e3387SVlastimil Babka 							struct page *page)
1134018e9a49SAndrew Morton {
1135018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1136018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1137018e9a49SAndrew Morton 		/*
1138018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1139018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1140018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1141018e9a49SAndrew Morton 		 */
1142018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
1143018e9a49SAndrew Morton 			return false;
1144018e9a49SAndrew Morton 	}
1145018e9a49SAndrew Morton 
11461ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
11471ef36db2SYisheng Xie 		return true;
11481ef36db2SYisheng Xie 
1149018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1150b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1151018e9a49SAndrew Morton 		return true;
1152018e9a49SAndrew Morton 
1153018e9a49SAndrew Morton 	/* Otherwise skip the block */
1154018e9a49SAndrew Morton 	return false;
1155018e9a49SAndrew Morton }
1156018e9a49SAndrew Morton 
115770b44595SMel Gorman static inline unsigned int
115870b44595SMel Gorman freelist_scan_limit(struct compact_control *cc)
115970b44595SMel Gorman {
116070b44595SMel Gorman 	return (COMPACT_CLUSTER_MAX >> cc->fast_search_fail) + 1;
116170b44595SMel Gorman }
116270b44595SMel Gorman 
1163ff9543fdSMichal Nazarewicz /*
1164f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1165f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1166f2849aa0SVlastimil Babka  */
1167f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1168f2849aa0SVlastimil Babka {
1169f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1170f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1171f2849aa0SVlastimil Babka }
1172f2849aa0SVlastimil Babka 
11735a811889SMel Gorman /*
11745a811889SMel Gorman  * Used when scanning for a suitable migration target which scans freelists
11755a811889SMel Gorman  * in reverse. Reorders the list such as the unscanned pages are scanned
11765a811889SMel Gorman  * first on the next iteration of the free scanner
11775a811889SMel Gorman  */
11785a811889SMel Gorman static void
11795a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage)
11805a811889SMel Gorman {
11815a811889SMel Gorman 	LIST_HEAD(sublist);
11825a811889SMel Gorman 
11835a811889SMel Gorman 	if (!list_is_last(freelist, &freepage->lru)) {
11845a811889SMel Gorman 		list_cut_before(&sublist, freelist, &freepage->lru);
11855a811889SMel Gorman 		if (!list_empty(&sublist))
11865a811889SMel Gorman 			list_splice_tail(&sublist, freelist);
11875a811889SMel Gorman 	}
11885a811889SMel Gorman }
11895a811889SMel Gorman 
11905a811889SMel Gorman /*
11915a811889SMel Gorman  * Similar to move_freelist_head except used by the migration scanner
11925a811889SMel Gorman  * when scanning forward. It's possible for these list operations to
11935a811889SMel Gorman  * move against each other if they search the free list exactly in
11945a811889SMel Gorman  * lockstep.
11955a811889SMel Gorman  */
119670b44595SMel Gorman static void
119770b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage)
119870b44595SMel Gorman {
119970b44595SMel Gorman 	LIST_HEAD(sublist);
120070b44595SMel Gorman 
120170b44595SMel Gorman 	if (!list_is_first(freelist, &freepage->lru)) {
120270b44595SMel Gorman 		list_cut_position(&sublist, freelist, &freepage->lru);
120370b44595SMel Gorman 		if (!list_empty(&sublist))
120470b44595SMel Gorman 			list_splice_tail(&sublist, freelist);
120570b44595SMel Gorman 	}
120670b44595SMel Gorman }
120770b44595SMel Gorman 
12085a811889SMel Gorman static void
12095a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
12105a811889SMel Gorman {
12115a811889SMel Gorman 	unsigned long start_pfn, end_pfn;
12125a811889SMel Gorman 	struct page *page = pfn_to_page(pfn);
12135a811889SMel Gorman 
12145a811889SMel Gorman 	/* Do not search around if there are enough pages already */
12155a811889SMel Gorman 	if (cc->nr_freepages >= cc->nr_migratepages)
12165a811889SMel Gorman 		return;
12175a811889SMel Gorman 
12185a811889SMel Gorman 	/* Minimise scanning during async compaction */
12195a811889SMel Gorman 	if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
12205a811889SMel Gorman 		return;
12215a811889SMel Gorman 
12225a811889SMel Gorman 	/* Pageblock boundaries */
12235a811889SMel Gorman 	start_pfn = pageblock_start_pfn(pfn);
12245a811889SMel Gorman 	end_pfn = min(start_pfn + pageblock_nr_pages, zone_end_pfn(cc->zone));
12255a811889SMel Gorman 
12265a811889SMel Gorman 	/* Scan before */
12275a811889SMel Gorman 	if (start_pfn != pfn) {
12284fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false);
12295a811889SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages)
12305a811889SMel Gorman 			return;
12315a811889SMel Gorman 	}
12325a811889SMel Gorman 
12335a811889SMel Gorman 	/* Scan after */
12345a811889SMel Gorman 	start_pfn = pfn + nr_isolated;
12355a811889SMel Gorman 	if (start_pfn != end_pfn)
12364fca9730SMel Gorman 		isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
12375a811889SMel Gorman 
12385a811889SMel Gorman 	/* Skip this pageblock in the future as it's full or nearly full */
12395a811889SMel Gorman 	if (cc->nr_freepages < cc->nr_migratepages)
12405a811889SMel Gorman 		set_pageblock_skip(page);
12415a811889SMel Gorman }
12425a811889SMel Gorman 
1243dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */
1244dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order)
1245dbe2d4e4SMel Gorman {
1246dbe2d4e4SMel Gorman 	order--;
1247dbe2d4e4SMel Gorman 	if (order < 0)
1248dbe2d4e4SMel Gorman 		order = cc->order - 1;
1249dbe2d4e4SMel Gorman 
1250dbe2d4e4SMel Gorman 	/* Search wrapped around? */
1251dbe2d4e4SMel Gorman 	if (order == cc->search_order) {
1252dbe2d4e4SMel Gorman 		cc->search_order--;
1253dbe2d4e4SMel Gorman 		if (cc->search_order < 0)
1254dbe2d4e4SMel Gorman 			cc->search_order = cc->order - 1;
1255dbe2d4e4SMel Gorman 		return -1;
1256dbe2d4e4SMel Gorman 	}
1257dbe2d4e4SMel Gorman 
1258dbe2d4e4SMel Gorman 	return order;
1259dbe2d4e4SMel Gorman }
1260dbe2d4e4SMel Gorman 
12615a811889SMel Gorman static unsigned long
12625a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc)
12635a811889SMel Gorman {
12645a811889SMel Gorman 	unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1);
12655a811889SMel Gorman 	unsigned int nr_scanned = 0;
12665a811889SMel Gorman 	unsigned long low_pfn, min_pfn, high_pfn = 0, highest = 0;
12675a811889SMel Gorman 	unsigned long nr_isolated = 0;
12685a811889SMel Gorman 	unsigned long distance;
12695a811889SMel Gorman 	struct page *page = NULL;
12705a811889SMel Gorman 	bool scan_start = false;
12715a811889SMel Gorman 	int order;
12725a811889SMel Gorman 
12735a811889SMel Gorman 	/* Full compaction passes in a negative order */
12745a811889SMel Gorman 	if (cc->order <= 0)
12755a811889SMel Gorman 		return cc->free_pfn;
12765a811889SMel Gorman 
12775a811889SMel Gorman 	/*
12785a811889SMel Gorman 	 * If starting the scan, use a deeper search and use the highest
12795a811889SMel Gorman 	 * PFN found if a suitable one is not found.
12805a811889SMel Gorman 	 */
1281e332f741SMel Gorman 	if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
12825a811889SMel Gorman 		limit = pageblock_nr_pages >> 1;
12835a811889SMel Gorman 		scan_start = true;
12845a811889SMel Gorman 	}
12855a811889SMel Gorman 
12865a811889SMel Gorman 	/*
12875a811889SMel Gorman 	 * Preferred point is in the top quarter of the scan space but take
12885a811889SMel Gorman 	 * a pfn from the top half if the search is problematic.
12895a811889SMel Gorman 	 */
12905a811889SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn);
12915a811889SMel Gorman 	low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
12925a811889SMel Gorman 	min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
12935a811889SMel Gorman 
12945a811889SMel Gorman 	if (WARN_ON_ONCE(min_pfn > low_pfn))
12955a811889SMel Gorman 		low_pfn = min_pfn;
12965a811889SMel Gorman 
1297dbe2d4e4SMel Gorman 	/*
1298dbe2d4e4SMel Gorman 	 * Search starts from the last successful isolation order or the next
1299dbe2d4e4SMel Gorman 	 * order to search after a previous failure
1300dbe2d4e4SMel Gorman 	 */
1301dbe2d4e4SMel Gorman 	cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1302dbe2d4e4SMel Gorman 
1303dbe2d4e4SMel Gorman 	for (order = cc->search_order;
1304dbe2d4e4SMel Gorman 	     !page && order >= 0;
1305dbe2d4e4SMel Gorman 	     order = next_search_order(cc, order)) {
13065a811889SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
13075a811889SMel Gorman 		struct list_head *freelist;
13085a811889SMel Gorman 		struct page *freepage;
13095a811889SMel Gorman 		unsigned long flags;
13105a811889SMel Gorman 		unsigned int order_scanned = 0;
13115a811889SMel Gorman 
13125a811889SMel Gorman 		if (!area->nr_free)
13135a811889SMel Gorman 			continue;
13145a811889SMel Gorman 
13155a811889SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
13165a811889SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
13175a811889SMel Gorman 		list_for_each_entry_reverse(freepage, freelist, lru) {
13185a811889SMel Gorman 			unsigned long pfn;
13195a811889SMel Gorman 
13205a811889SMel Gorman 			order_scanned++;
13215a811889SMel Gorman 			nr_scanned++;
13225a811889SMel Gorman 			pfn = page_to_pfn(freepage);
13235a811889SMel Gorman 
13245a811889SMel Gorman 			if (pfn >= highest)
13255a811889SMel Gorman 				highest = pageblock_start_pfn(pfn);
13265a811889SMel Gorman 
13275a811889SMel Gorman 			if (pfn >= low_pfn) {
13285a811889SMel Gorman 				cc->fast_search_fail = 0;
1329dbe2d4e4SMel Gorman 				cc->search_order = order;
13305a811889SMel Gorman 				page = freepage;
13315a811889SMel Gorman 				break;
13325a811889SMel Gorman 			}
13335a811889SMel Gorman 
13345a811889SMel Gorman 			if (pfn >= min_pfn && pfn > high_pfn) {
13355a811889SMel Gorman 				high_pfn = pfn;
13365a811889SMel Gorman 
13375a811889SMel Gorman 				/* Shorten the scan if a candidate is found */
13385a811889SMel Gorman 				limit >>= 1;
13395a811889SMel Gorman 			}
13405a811889SMel Gorman 
13415a811889SMel Gorman 			if (order_scanned >= limit)
13425a811889SMel Gorman 				break;
13435a811889SMel Gorman 		}
13445a811889SMel Gorman 
13455a811889SMel Gorman 		/* Use a minimum pfn if a preferred one was not found */
13465a811889SMel Gorman 		if (!page && high_pfn) {
13475a811889SMel Gorman 			page = pfn_to_page(high_pfn);
13485a811889SMel Gorman 
13495a811889SMel Gorman 			/* Update freepage for the list reorder below */
13505a811889SMel Gorman 			freepage = page;
13515a811889SMel Gorman 		}
13525a811889SMel Gorman 
13535a811889SMel Gorman 		/* Reorder to so a future search skips recent pages */
13545a811889SMel Gorman 		move_freelist_head(freelist, freepage);
13555a811889SMel Gorman 
13565a811889SMel Gorman 		/* Isolate the page if available */
13575a811889SMel Gorman 		if (page) {
13585a811889SMel Gorman 			if (__isolate_free_page(page, order)) {
13595a811889SMel Gorman 				set_page_private(page, order);
13605a811889SMel Gorman 				nr_isolated = 1 << order;
13615a811889SMel Gorman 				cc->nr_freepages += nr_isolated;
13625a811889SMel Gorman 				list_add_tail(&page->lru, &cc->freepages);
13635a811889SMel Gorman 				count_compact_events(COMPACTISOLATED, nr_isolated);
13645a811889SMel Gorman 			} else {
13655a811889SMel Gorman 				/* If isolation fails, abort the search */
13665a811889SMel Gorman 				order = -1;
13675a811889SMel Gorman 				page = NULL;
13685a811889SMel Gorman 			}
13695a811889SMel Gorman 		}
13705a811889SMel Gorman 
13715a811889SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
13725a811889SMel Gorman 
13735a811889SMel Gorman 		/*
13745a811889SMel Gorman 		 * Smaller scan on next order so the total scan ig related
13755a811889SMel Gorman 		 * to freelist_scan_limit.
13765a811889SMel Gorman 		 */
13775a811889SMel Gorman 		if (order_scanned >= limit)
13785a811889SMel Gorman 			limit = min(1U, limit >> 1);
13795a811889SMel Gorman 	}
13805a811889SMel Gorman 
13815a811889SMel Gorman 	if (!page) {
13825a811889SMel Gorman 		cc->fast_search_fail++;
13835a811889SMel Gorman 		if (scan_start) {
13845a811889SMel Gorman 			/*
13855a811889SMel Gorman 			 * Use the highest PFN found above min. If one was
13865a811889SMel Gorman 			 * not found, be pessemistic for direct compaction
13875a811889SMel Gorman 			 * and use the min mark.
13885a811889SMel Gorman 			 */
13895a811889SMel Gorman 			if (highest) {
13905a811889SMel Gorman 				page = pfn_to_page(highest);
13915a811889SMel Gorman 				cc->free_pfn = highest;
13925a811889SMel Gorman 			} else {
13935a811889SMel Gorman 				if (cc->direct_compaction) {
13945a811889SMel Gorman 					page = pfn_to_page(min_pfn);
13955a811889SMel Gorman 					cc->free_pfn = min_pfn;
13965a811889SMel Gorman 				}
13975a811889SMel Gorman 			}
13985a811889SMel Gorman 		}
13995a811889SMel Gorman 	}
14005a811889SMel Gorman 
1401d097a6f6SMel Gorman 	if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1402d097a6f6SMel Gorman 		highest -= pageblock_nr_pages;
14035a811889SMel Gorman 		cc->zone->compact_cached_free_pfn = highest;
1404d097a6f6SMel Gorman 	}
14055a811889SMel Gorman 
14065a811889SMel Gorman 	cc->total_free_scanned += nr_scanned;
14075a811889SMel Gorman 	if (!page)
14085a811889SMel Gorman 		return cc->free_pfn;
14095a811889SMel Gorman 
14105a811889SMel Gorman 	low_pfn = page_to_pfn(page);
14115a811889SMel Gorman 	fast_isolate_around(cc, low_pfn, nr_isolated);
14125a811889SMel Gorman 	return low_pfn;
14135a811889SMel Gorman }
14145a811889SMel Gorman 
1415f2849aa0SVlastimil Babka /*
1416ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1417ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1418ff9543fdSMichal Nazarewicz  */
1419edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1420ff9543fdSMichal Nazarewicz {
1421edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1422ff9543fdSMichal Nazarewicz 	struct page *page;
1423c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1424e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1425c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1426c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1427ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
14284fca9730SMel Gorman 	unsigned int stride;
14292fe86e00SMichal Nazarewicz 
14305a811889SMel Gorman 	/* Try a small search of the free lists for a candidate */
14315a811889SMel Gorman 	isolate_start_pfn = fast_isolate_freepages(cc);
14325a811889SMel Gorman 	if (cc->nr_freepages)
14335a811889SMel Gorman 		goto splitmap;
14345a811889SMel Gorman 
1435ff9543fdSMichal Nazarewicz 	/*
1436ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
143749e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1438e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1439e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1440c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1441c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1442c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
144349e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
144449e068f0SVlastimil Babka 	 * is using.
1445ff9543fdSMichal Nazarewicz 	 */
1446e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
14475a811889SMel Gorman 	block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
1448c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1449c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
145006b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
14514fca9730SMel Gorman 	stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
14522fe86e00SMichal Nazarewicz 
1453ff9543fdSMichal Nazarewicz 	/*
1454ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1455ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1456ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1457ff9543fdSMichal Nazarewicz 	 */
1458f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1459c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1460e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1461e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
14624fca9730SMel Gorman 		unsigned long nr_isolated;
14634fca9730SMel Gorman 
1464f6ea3adbSDavid Rientjes 		/*
1465f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1466cb810ad2SMel Gorman 		 * suitable migration targets, so periodically check resched.
1467f6ea3adbSDavid Rientjes 		 */
1468cb810ad2SMel Gorman 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1469cf66f070SMel Gorman 			cond_resched();
1470f6ea3adbSDavid Rientjes 
14717d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
14727d49d886SVlastimil Babka 									zone);
14737d49d886SVlastimil Babka 		if (!page)
1474ff9543fdSMichal Nazarewicz 			continue;
1475ff9543fdSMichal Nazarewicz 
1476ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
14779f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1478ff9543fdSMichal Nazarewicz 			continue;
147968e3e926SLinus Torvalds 
1480bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1481bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1482bb13ffebSMel Gorman 			continue;
1483bb13ffebSMel Gorman 
1484e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
14854fca9730SMel Gorman 		nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
14864fca9730SMel Gorman 					block_end_pfn, freelist, stride, false);
1487ff9543fdSMichal Nazarewicz 
1488d097a6f6SMel Gorman 		/* Update the skip hint if the full pageblock was scanned */
1489d097a6f6SMel Gorman 		if (isolate_start_pfn == block_end_pfn)
1490d097a6f6SMel Gorman 			update_pageblock_skip(cc, page, block_start_pfn);
1491d097a6f6SMel Gorman 
1492cb2dcaf0SMel Gorman 		/* Are enough freepages isolated? */
1493cb2dcaf0SMel Gorman 		if (cc->nr_freepages >= cc->nr_migratepages) {
1494a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1495a46cbf3bSDavid Rientjes 				/*
1496a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1497a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1498a46cbf3bSDavid Rientjes 				 */
1499f5f61a32SVlastimil Babka 				isolate_start_pfn =
1500e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1501a46cbf3bSDavid Rientjes 			}
1502be976572SVlastimil Babka 			break;
1503a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1504f5f61a32SVlastimil Babka 			/*
1505a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1506a46cbf3bSDavid Rientjes 			 * needlessly.
1507f5f61a32SVlastimil Babka 			 */
1508a46cbf3bSDavid Rientjes 			break;
1509f5f61a32SVlastimil Babka 		}
15104fca9730SMel Gorman 
15114fca9730SMel Gorman 		/* Adjust stride depending on isolation */
15124fca9730SMel Gorman 		if (nr_isolated) {
15134fca9730SMel Gorman 			stride = 1;
15144fca9730SMel Gorman 			continue;
15154fca9730SMel Gorman 		}
15164fca9730SMel Gorman 		stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1517c89511abSMel Gorman 	}
1518ff9543fdSMichal Nazarewicz 
15197ed695e0SVlastimil Babka 	/*
1520f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1521f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1522f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1523f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
15247ed695e0SVlastimil Babka 	 */
1525f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
15265a811889SMel Gorman 
15275a811889SMel Gorman splitmap:
15285a811889SMel Gorman 	/* __isolate_free_page() does not map the pages */
15295a811889SMel Gorman 	split_map_pages(freelist);
1530748446bbSMel Gorman }
1531748446bbSMel Gorman 
1532748446bbSMel Gorman /*
1533748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1534748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1535748446bbSMel Gorman  */
1536748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1537666feb21SMichal Hocko 					unsigned long data)
1538748446bbSMel Gorman {
1539748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1540748446bbSMel Gorman 	struct page *freepage;
1541748446bbSMel Gorman 
1542748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1543edc2ca61SVlastimil Babka 		isolate_freepages(cc);
1544748446bbSMel Gorman 
1545748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1546748446bbSMel Gorman 			return NULL;
1547748446bbSMel Gorman 	}
1548748446bbSMel Gorman 
1549748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1550748446bbSMel Gorman 	list_del(&freepage->lru);
1551748446bbSMel Gorman 	cc->nr_freepages--;
1552748446bbSMel Gorman 
1553748446bbSMel Gorman 	return freepage;
1554748446bbSMel Gorman }
1555748446bbSMel Gorman 
1556748446bbSMel Gorman /*
1557d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1558d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1559d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1560d53aea3dSDavid Rientjes  */
1561d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1562d53aea3dSDavid Rientjes {
1563d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1564d53aea3dSDavid Rientjes 
1565d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1566d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1567d53aea3dSDavid Rientjes }
1568d53aea3dSDavid Rientjes 
1569ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1570ff9543fdSMichal Nazarewicz typedef enum {
1571ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1572ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1573ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1574ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1575ff9543fdSMichal Nazarewicz 
1576ff9543fdSMichal Nazarewicz /*
15775bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
15785bbe3547SEric B Munson  * compactable pages.
15795bbe3547SEric B Munson  */
15805bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
15815bbe3547SEric B Munson 
158270b44595SMel Gorman static inline void
158370b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
158470b44595SMel Gorman {
158570b44595SMel Gorman 	if (cc->fast_start_pfn == ULONG_MAX)
158670b44595SMel Gorman 		return;
158770b44595SMel Gorman 
158870b44595SMel Gorman 	if (!cc->fast_start_pfn)
158970b44595SMel Gorman 		cc->fast_start_pfn = pfn;
159070b44595SMel Gorman 
159170b44595SMel Gorman 	cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
159270b44595SMel Gorman }
159370b44595SMel Gorman 
159470b44595SMel Gorman static inline unsigned long
159570b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc)
159670b44595SMel Gorman {
159770b44595SMel Gorman 	if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
159870b44595SMel Gorman 		return cc->migrate_pfn;
159970b44595SMel Gorman 
160070b44595SMel Gorman 	cc->migrate_pfn = cc->fast_start_pfn;
160170b44595SMel Gorman 	cc->fast_start_pfn = ULONG_MAX;
160270b44595SMel Gorman 
160370b44595SMel Gorman 	return cc->migrate_pfn;
160470b44595SMel Gorman }
160570b44595SMel Gorman 
160670b44595SMel Gorman /*
160770b44595SMel Gorman  * Briefly search the free lists for a migration source that already has
160870b44595SMel Gorman  * some free pages to reduce the number of pages that need migration
160970b44595SMel Gorman  * before a pageblock is free.
161070b44595SMel Gorman  */
161170b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc)
161270b44595SMel Gorman {
161370b44595SMel Gorman 	unsigned int limit = freelist_scan_limit(cc);
161470b44595SMel Gorman 	unsigned int nr_scanned = 0;
161570b44595SMel Gorman 	unsigned long distance;
161670b44595SMel Gorman 	unsigned long pfn = cc->migrate_pfn;
161770b44595SMel Gorman 	unsigned long high_pfn;
161870b44595SMel Gorman 	int order;
161970b44595SMel Gorman 
162070b44595SMel Gorman 	/* Skip hints are relied on to avoid repeats on the fast search */
162170b44595SMel Gorman 	if (cc->ignore_skip_hint)
162270b44595SMel Gorman 		return pfn;
162370b44595SMel Gorman 
162470b44595SMel Gorman 	/*
162570b44595SMel Gorman 	 * If the migrate_pfn is not at the start of a zone or the start
162670b44595SMel Gorman 	 * of a pageblock then assume this is a continuation of a previous
162770b44595SMel Gorman 	 * scan restarted due to COMPACT_CLUSTER_MAX.
162870b44595SMel Gorman 	 */
162970b44595SMel Gorman 	if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
163070b44595SMel Gorman 		return pfn;
163170b44595SMel Gorman 
163270b44595SMel Gorman 	/*
163370b44595SMel Gorman 	 * For smaller orders, just linearly scan as the number of pages
163470b44595SMel Gorman 	 * to migrate should be relatively small and does not necessarily
163570b44595SMel Gorman 	 * justify freeing up a large block for a small allocation.
163670b44595SMel Gorman 	 */
163770b44595SMel Gorman 	if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
163870b44595SMel Gorman 		return pfn;
163970b44595SMel Gorman 
164070b44595SMel Gorman 	/*
164170b44595SMel Gorman 	 * Only allow kcompactd and direct requests for movable pages to
164270b44595SMel Gorman 	 * quickly clear out a MOVABLE pageblock for allocation. This
164370b44595SMel Gorman 	 * reduces the risk that a large movable pageblock is freed for
164470b44595SMel Gorman 	 * an unmovable/reclaimable small allocation.
164570b44595SMel Gorman 	 */
164670b44595SMel Gorman 	if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
164770b44595SMel Gorman 		return pfn;
164870b44595SMel Gorman 
164970b44595SMel Gorman 	/*
165070b44595SMel Gorman 	 * When starting the migration scanner, pick any pageblock within the
165170b44595SMel Gorman 	 * first half of the search space. Otherwise try and pick a pageblock
165270b44595SMel Gorman 	 * within the first eighth to reduce the chances that a migration
165370b44595SMel Gorman 	 * target later becomes a source.
165470b44595SMel Gorman 	 */
165570b44595SMel Gorman 	distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
165670b44595SMel Gorman 	if (cc->migrate_pfn != cc->zone->zone_start_pfn)
165770b44595SMel Gorman 		distance >>= 2;
165870b44595SMel Gorman 	high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
165970b44595SMel Gorman 
166070b44595SMel Gorman 	for (order = cc->order - 1;
166170b44595SMel Gorman 	     order >= PAGE_ALLOC_COSTLY_ORDER && pfn == cc->migrate_pfn && nr_scanned < limit;
166270b44595SMel Gorman 	     order--) {
166370b44595SMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
166470b44595SMel Gorman 		struct list_head *freelist;
166570b44595SMel Gorman 		unsigned long flags;
166670b44595SMel Gorman 		struct page *freepage;
166770b44595SMel Gorman 
166870b44595SMel Gorman 		if (!area->nr_free)
166970b44595SMel Gorman 			continue;
167070b44595SMel Gorman 
167170b44595SMel Gorman 		spin_lock_irqsave(&cc->zone->lock, flags);
167270b44595SMel Gorman 		freelist = &area->free_list[MIGRATE_MOVABLE];
167370b44595SMel Gorman 		list_for_each_entry(freepage, freelist, lru) {
167470b44595SMel Gorman 			unsigned long free_pfn;
167570b44595SMel Gorman 
167670b44595SMel Gorman 			nr_scanned++;
167770b44595SMel Gorman 			free_pfn = page_to_pfn(freepage);
167870b44595SMel Gorman 			if (free_pfn < high_pfn) {
167970b44595SMel Gorman 				/*
168070b44595SMel Gorman 				 * Avoid if skipped recently. Ideally it would
168170b44595SMel Gorman 				 * move to the tail but even safe iteration of
168270b44595SMel Gorman 				 * the list assumes an entry is deleted, not
168370b44595SMel Gorman 				 * reordered.
168470b44595SMel Gorman 				 */
168570b44595SMel Gorman 				if (get_pageblock_skip(freepage)) {
168670b44595SMel Gorman 					if (list_is_last(freelist, &freepage->lru))
168770b44595SMel Gorman 						break;
168870b44595SMel Gorman 
168970b44595SMel Gorman 					continue;
169070b44595SMel Gorman 				}
169170b44595SMel Gorman 
169270b44595SMel Gorman 				/* Reorder to so a future search skips recent pages */
169370b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
169470b44595SMel Gorman 
1695e380bebeSMel Gorman 				update_fast_start_pfn(cc, free_pfn);
169670b44595SMel Gorman 				pfn = pageblock_start_pfn(free_pfn);
169770b44595SMel Gorman 				cc->fast_search_fail = 0;
169870b44595SMel Gorman 				set_pageblock_skip(freepage);
169970b44595SMel Gorman 				break;
170070b44595SMel Gorman 			}
170170b44595SMel Gorman 
170270b44595SMel Gorman 			if (nr_scanned >= limit) {
170370b44595SMel Gorman 				cc->fast_search_fail++;
170470b44595SMel Gorman 				move_freelist_tail(freelist, freepage);
170570b44595SMel Gorman 				break;
170670b44595SMel Gorman 			}
170770b44595SMel Gorman 		}
170870b44595SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
170970b44595SMel Gorman 	}
171070b44595SMel Gorman 
171170b44595SMel Gorman 	cc->total_migrate_scanned += nr_scanned;
171270b44595SMel Gorman 
171370b44595SMel Gorman 	/*
171470b44595SMel Gorman 	 * If fast scanning failed then use a cached entry for a page block
171570b44595SMel Gorman 	 * that had free pages as the basis for starting a linear scan.
171670b44595SMel Gorman 	 */
171770b44595SMel Gorman 	if (pfn == cc->migrate_pfn)
171870b44595SMel Gorman 		pfn = reinit_migrate_pfn(cc);
171970b44595SMel Gorman 
172070b44595SMel Gorman 	return pfn;
172170b44595SMel Gorman }
172270b44595SMel Gorman 
17235bbe3547SEric B Munson /*
1724edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1725edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1726edc2ca61SVlastimil Babka  * compact_control.
1727ff9543fdSMichal Nazarewicz  */
1728ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1729ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1730ff9543fdSMichal Nazarewicz {
1731e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1732e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1733e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1734edc2ca61SVlastimil Babka 	struct page *page;
1735edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
17365bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
17371d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
173870b44595SMel Gorman 	bool fast_find_block;
1739ff9543fdSMichal Nazarewicz 
1740edc2ca61SVlastimil Babka 	/*
1741edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
174270b44595SMel Gorman 	 * initialized by compact_zone(). The first failure will use
174370b44595SMel Gorman 	 * the lowest PFN as the starting point for linear scanning.
1744edc2ca61SVlastimil Babka 	 */
174570b44595SMel Gorman 	low_pfn = fast_find_migrateblock(cc);
174606b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
1747e1409c32SJoonsoo Kim 	if (block_start_pfn < zone->zone_start_pfn)
1748e1409c32SJoonsoo Kim 		block_start_pfn = zone->zone_start_pfn;
1749ff9543fdSMichal Nazarewicz 
175070b44595SMel Gorman 	/*
175170b44595SMel Gorman 	 * fast_find_migrateblock marks a pageblock skipped so to avoid
175270b44595SMel Gorman 	 * the isolation_suitable check below, check whether the fast
175370b44595SMel Gorman 	 * search was successful.
175470b44595SMel Gorman 	 */
175570b44595SMel Gorman 	fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
175670b44595SMel Gorman 
1757ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
175806b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1759ff9543fdSMichal Nazarewicz 
1760edc2ca61SVlastimil Babka 	/*
1761edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1762edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1763edc2ca61SVlastimil Babka 	 */
1764e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
176570b44595SMel Gorman 			fast_find_block = false,
1766e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1767e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1768e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1769edc2ca61SVlastimil Babka 
1770edc2ca61SVlastimil Babka 		/*
1771edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1772edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1773cb810ad2SMel Gorman 		 * need to schedule.
1774edc2ca61SVlastimil Babka 		 */
1775cb810ad2SMel Gorman 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1776cf66f070SMel Gorman 			cond_resched();
1777edc2ca61SVlastimil Babka 
1778e1409c32SJoonsoo Kim 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1779e1409c32SJoonsoo Kim 									zone);
17807d49d886SVlastimil Babka 		if (!page)
1781edc2ca61SVlastimil Babka 			continue;
1782edc2ca61SVlastimil Babka 
1783e380bebeSMel Gorman 		/*
1784e380bebeSMel Gorman 		 * If isolation recently failed, do not retry. Only check the
1785e380bebeSMel Gorman 		 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1786e380bebeSMel Gorman 		 * to be visited multiple times. Assume skip was checked
1787e380bebeSMel Gorman 		 * before making it "skip" so other compaction instances do
1788e380bebeSMel Gorman 		 * not scan the same block.
1789e380bebeSMel Gorman 		 */
1790e380bebeSMel Gorman 		if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1791e380bebeSMel Gorman 		    !fast_find_block && !isolation_suitable(cc, page))
1792edc2ca61SVlastimil Babka 			continue;
1793edc2ca61SVlastimil Babka 
1794edc2ca61SVlastimil Babka 		/*
17959bebefd5SMel Gorman 		 * For async compaction, also only scan in MOVABLE blocks
17969bebefd5SMel Gorman 		 * without huge pages. Async compaction is optimistic to see
17979bebefd5SMel Gorman 		 * if the minimum amount of work satisfies the allocation.
17989bebefd5SMel Gorman 		 * The cached PFN is updated as it's possible that all
17999bebefd5SMel Gorman 		 * remaining blocks between source and target are unsuitable
18009bebefd5SMel Gorman 		 * and the compaction scanners fail to meet.
1801edc2ca61SVlastimil Babka 		 */
18029bebefd5SMel Gorman 		if (!suitable_migration_source(cc, page)) {
18039bebefd5SMel Gorman 			update_cached_migrate(cc, block_end_pfn);
1804edc2ca61SVlastimil Babka 			continue;
18059bebefd5SMel Gorman 		}
1806ff9543fdSMichal Nazarewicz 
1807ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1808e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1809e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1810edc2ca61SVlastimil Babka 
1811cb2dcaf0SMel Gorman 		if (!low_pfn)
1812ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1813ff9543fdSMichal Nazarewicz 
1814edc2ca61SVlastimil Babka 		/*
1815edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1816edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1817edc2ca61SVlastimil Babka 		 * continue or not.
1818edc2ca61SVlastimil Babka 		 */
1819edc2ca61SVlastimil Babka 		break;
1820edc2ca61SVlastimil Babka 	}
1821edc2ca61SVlastimil Babka 
1822f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1823f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1824ff9543fdSMichal Nazarewicz 
1825edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1826ff9543fdSMichal Nazarewicz }
1827ff9543fdSMichal Nazarewicz 
182821c527a3SYaowei Bai /*
182921c527a3SYaowei Bai  * order == -1 is expected when compacting via
183021c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
183121c527a3SYaowei Bai  */
183221c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
183321c527a3SYaowei Bai {
183421c527a3SYaowei Bai 	return order == -1;
183521c527a3SYaowei Bai }
183621c527a3SYaowei Bai 
183740cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc)
1838748446bbSMel Gorman {
18398fb74b9fSMel Gorman 	unsigned int order;
1840d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
1841cb2dcaf0SMel Gorman 	int ret;
1842748446bbSMel Gorman 
1843753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1844f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
184555b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
184640cacbcbSMel Gorman 		reset_cached_positions(cc->zone);
184755b7c4c9SVlastimil Babka 
184862997027SMel Gorman 		/*
184962997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1850accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
185162997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
185262997027SMel Gorman 		 * based on an allocation request.
185362997027SMel Gorman 		 */
1854accf6242SVlastimil Babka 		if (cc->direct_compaction)
185540cacbcbSMel Gorman 			cc->zone->compact_blockskip_flush = true;
185662997027SMel Gorman 
1857c8f7de0bSMichal Hocko 		if (cc->whole_zone)
1858748446bbSMel Gorman 			return COMPACT_COMPLETE;
1859c8f7de0bSMichal Hocko 		else
1860c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
1861bb13ffebSMel Gorman 	}
1862748446bbSMel Gorman 
186321c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
186456de7263SMel Gorman 		return COMPACT_CONTINUE;
186556de7263SMel Gorman 
1866baf6a9a1SVlastimil Babka 	/*
1867efe771c7SMel Gorman 	 * Always finish scanning a pageblock to reduce the possibility of
1868efe771c7SMel Gorman 	 * fallbacks in the future. This is particularly important when
1869efe771c7SMel Gorman 	 * migration source is unmovable/reclaimable but it's not worth
1870efe771c7SMel Gorman 	 * special casing.
1871baf6a9a1SVlastimil Babka 	 */
1872efe771c7SMel Gorman 	if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
1873baf6a9a1SVlastimil Babka 		return COMPACT_CONTINUE;
1874baf6a9a1SVlastimil Babka 
187556de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
1876cb2dcaf0SMel Gorman 	ret = COMPACT_NO_SUITABLE_PAGE;
187756de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
187840cacbcbSMel Gorman 		struct free_area *area = &cc->zone->free_area[order];
18792149cdaeSJoonsoo Kim 		bool can_steal;
18808fb74b9fSMel Gorman 
188156de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
18826d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
1883cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
188456de7263SMel Gorman 
18852149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
18862149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
18872149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
18882149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
1889cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
18902149cdaeSJoonsoo Kim #endif
18912149cdaeSJoonsoo Kim 		/*
18922149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
18932149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
18942149cdaeSJoonsoo Kim 		 */
18952149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
1896baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
1897baf6a9a1SVlastimil Babka 
1898baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
1899baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
1900cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
1901baf6a9a1SVlastimil Babka 
1902baf6a9a1SVlastimil Babka 			/*
1903baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
1904baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
1905baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
1906baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
1907baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
1908baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
1909baf6a9a1SVlastimil Babka 			 */
1910baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
1911baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
1912baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
1913baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
1914baf6a9a1SVlastimil Babka 			}
1915baf6a9a1SVlastimil Babka 
1916cb2dcaf0SMel Gorman 			ret = COMPACT_CONTINUE;
1917cb2dcaf0SMel Gorman 			break;
1918baf6a9a1SVlastimil Babka 		}
191956de7263SMel Gorman 	}
192056de7263SMel Gorman 
1921cb2dcaf0SMel Gorman 	if (cc->contended || fatal_signal_pending(current))
1922cb2dcaf0SMel Gorman 		ret = COMPACT_CONTENDED;
1923cb2dcaf0SMel Gorman 
1924cb2dcaf0SMel Gorman 	return ret;
1925837d026dSJoonsoo Kim }
1926837d026dSJoonsoo Kim 
192740cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc)
1928837d026dSJoonsoo Kim {
1929837d026dSJoonsoo Kim 	int ret;
1930837d026dSJoonsoo Kim 
193140cacbcbSMel Gorman 	ret = __compact_finished(cc);
193240cacbcbSMel Gorman 	trace_mm_compaction_finished(cc->zone, cc->order, ret);
1933837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1934837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1935837d026dSJoonsoo Kim 
1936837d026dSJoonsoo Kim 	return ret;
1937748446bbSMel Gorman }
1938748446bbSMel Gorman 
19393e7d3449SMel Gorman /*
19403e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
19413e7d3449SMel Gorman  * Returns
19423e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
1943cf378319SVlastimil Babka  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
19443e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
19453e7d3449SMel Gorman  */
1946ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
1947c603844bSMel Gorman 					unsigned int alloc_flags,
194886a294a8SMichal Hocko 					int classzone_idx,
194986a294a8SMichal Hocko 					unsigned long wmark_target)
19503e7d3449SMel Gorman {
19513e7d3449SMel Gorman 	unsigned long watermark;
19523e7d3449SMel Gorman 
195321c527a3SYaowei Bai 	if (is_via_compact_memory(order))
19543957c776SMichal Hocko 		return COMPACT_CONTINUE;
19553957c776SMichal Hocko 
1956a9214443SMel Gorman 	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
1957ebff3980SVlastimil Babka 	/*
1958ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1959ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1960ebff3980SVlastimil Babka 	 */
1961ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1962ebff3980SVlastimil Babka 								alloc_flags))
1963cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
1964ebff3980SVlastimil Babka 
19653957c776SMichal Hocko 	/*
19669861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
1967984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
1968984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
1969984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
1970984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
1971984fdba6SVlastimil Babka 	 * isolation. We however do use the direct compactor's classzone_idx to
1972984fdba6SVlastimil Babka 	 * skip over zones where lowmem reserves would prevent allocation even
1973984fdba6SVlastimil Babka 	 * if compaction succeeds.
19748348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
19758348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
1976d883c6cfSJoonsoo Kim 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
1977d883c6cfSJoonsoo Kim 	 * suitable migration targets
19783e7d3449SMel Gorman 	 */
19798348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
19808348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
19818348faf9SVlastimil Babka 	watermark += compact_gap(order);
198286a294a8SMichal Hocko 	if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1983d883c6cfSJoonsoo Kim 						ALLOC_CMA, wmark_target))
19843e7d3449SMel Gorman 		return COMPACT_SKIPPED;
19853e7d3449SMel Gorman 
1986cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
1987cc5c9f09SVlastimil Babka }
1988cc5c9f09SVlastimil Babka 
1989cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
1990cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
1991cc5c9f09SVlastimil Babka 					int classzone_idx)
1992cc5c9f09SVlastimil Babka {
1993cc5c9f09SVlastimil Babka 	enum compact_result ret;
1994cc5c9f09SVlastimil Babka 	int fragindex;
1995cc5c9f09SVlastimil Babka 
1996cc5c9f09SVlastimil Babka 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1997cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
19983e7d3449SMel Gorman 	/*
19993e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
20003e7d3449SMel Gorman 	 * low memory or external fragmentation
20013e7d3449SMel Gorman 	 *
2002ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
2003ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
20043e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
20053e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
20063e7d3449SMel Gorman 	 *
200720311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
200820311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
200920311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
201020311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
201120311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
201220311420SVlastimil Babka 	 * expense of system stability.
20133e7d3449SMel Gorman 	 */
201420311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
20153e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
20163e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2017cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
20183e7d3449SMel Gorman 	}
20193e7d3449SMel Gorman 
2020837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
2021837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
2022837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
2023837d026dSJoonsoo Kim 
2024837d026dSJoonsoo Kim 	return ret;
2025837d026dSJoonsoo Kim }
2026837d026dSJoonsoo Kim 
202786a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
202886a294a8SMichal Hocko 		int alloc_flags)
202986a294a8SMichal Hocko {
203086a294a8SMichal Hocko 	struct zone *zone;
203186a294a8SMichal Hocko 	struct zoneref *z;
203286a294a8SMichal Hocko 
203386a294a8SMichal Hocko 	/*
203486a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
203586a294a8SMichal Hocko 	 * retrying the reclaim.
203686a294a8SMichal Hocko 	 */
203786a294a8SMichal Hocko 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
203886a294a8SMichal Hocko 					ac->nodemask) {
203986a294a8SMichal Hocko 		unsigned long available;
204086a294a8SMichal Hocko 		enum compact_result compact_result;
204186a294a8SMichal Hocko 
204286a294a8SMichal Hocko 		/*
204386a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
204486a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
204586a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
204686a294a8SMichal Hocko 		 * is happy about the watermark check.
204786a294a8SMichal Hocko 		 */
20485a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
204986a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
205086a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
205186a294a8SMichal Hocko 				ac_classzone_idx(ac), available);
2052cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
205386a294a8SMichal Hocko 			return true;
205486a294a8SMichal Hocko 	}
205586a294a8SMichal Hocko 
205686a294a8SMichal Hocko 	return false;
205786a294a8SMichal Hocko }
205886a294a8SMichal Hocko 
20595e1f0f09SMel Gorman static enum compact_result
20605e1f0f09SMel Gorman compact_zone(struct compact_control *cc, struct capture_control *capc)
2061748446bbSMel Gorman {
2062ea7ab982SMichal Hocko 	enum compact_result ret;
206340cacbcbSMel Gorman 	unsigned long start_pfn = cc->zone->zone_start_pfn;
206440cacbcbSMel Gorman 	unsigned long end_pfn = zone_end_pfn(cc->zone);
2065566e54e1SMel Gorman 	unsigned long last_migrated_pfn;
2066e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
20678854c55fSMel Gorman 	bool update_cached;
2068748446bbSMel Gorman 
2069d39773a0SVlastimil Babka 	cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask);
207040cacbcbSMel Gorman 	ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
2071ebff3980SVlastimil Babka 							cc->classzone_idx);
20723e7d3449SMel Gorman 	/* Compaction is likely to fail */
2073cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
20743e7d3449SMel Gorman 		return ret;
2075c46649deSMichal Hocko 
2076c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
2077c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
20783e7d3449SMel Gorman 
2079c89511abSMel Gorman 	/*
2080d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
2081accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
2082d3132e4bSVlastimil Babka 	 */
208340cacbcbSMel Gorman 	if (compaction_restarting(cc->zone, cc->order))
208440cacbcbSMel Gorman 		__reset_isolation_suitable(cc->zone);
2085d3132e4bSVlastimil Babka 
2086d3132e4bSVlastimil Babka 	/*
2087c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
208806ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
208906ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
209006ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
2091c89511abSMel Gorman 	 */
209270b44595SMel Gorman 	cc->fast_start_pfn = 0;
209306ed2998SVlastimil Babka 	if (cc->whole_zone) {
209406ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
209506ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
209606ed2998SVlastimil Babka 	} else {
209740cacbcbSMel Gorman 		cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
209840cacbcbSMel Gorman 		cc->free_pfn = cc->zone->compact_cached_free_pfn;
2099623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
210006b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
210140cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = cc->free_pfn;
2102c89511abSMel Gorman 		}
2103623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2104c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
210540cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
210640cacbcbSMel Gorman 			cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2107c89511abSMel Gorman 		}
2108c8f7de0bSMichal Hocko 
2109e332f741SMel Gorman 		if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
2110c8f7de0bSMichal Hocko 			cc->whole_zone = true;
211106ed2998SVlastimil Babka 	}
2112c8f7de0bSMichal Hocko 
2113566e54e1SMel Gorman 	last_migrated_pfn = 0;
2114748446bbSMel Gorman 
21158854c55fSMel Gorman 	/*
21168854c55fSMel Gorman 	 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
21178854c55fSMel Gorman 	 * the basis that some migrations will fail in ASYNC mode. However,
21188854c55fSMel Gorman 	 * if the cached PFNs match and pageblocks are skipped due to having
21198854c55fSMel Gorman 	 * no isolation candidates, then the sync state does not matter.
21208854c55fSMel Gorman 	 * Until a pageblock with isolation candidates is found, keep the
21218854c55fSMel Gorman 	 * cached PFNs in sync to avoid revisiting the same blocks.
21228854c55fSMel Gorman 	 */
21238854c55fSMel Gorman 	update_cached = !sync &&
21248854c55fSMel Gorman 		cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
21258854c55fSMel Gorman 
212616c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
212716c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
21280eb927c0SMel Gorman 
2129748446bbSMel Gorman 	migrate_prep_local();
2130748446bbSMel Gorman 
213140cacbcbSMel Gorman 	while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
21329d502c1cSMinchan Kim 		int err;
2133566e54e1SMel Gorman 		unsigned long start_pfn = cc->migrate_pfn;
2134748446bbSMel Gorman 
2135804d3121SMel Gorman 		/*
2136804d3121SMel Gorman 		 * Avoid multiple rescans which can happen if a page cannot be
2137804d3121SMel Gorman 		 * isolated (dirty/writeback in async mode) or if the migrated
2138804d3121SMel Gorman 		 * pages are being allocated before the pageblock is cleared.
2139804d3121SMel Gorman 		 * The first rescan will capture the entire pageblock for
2140804d3121SMel Gorman 		 * migration. If it fails, it'll be marked skip and scanning
2141804d3121SMel Gorman 		 * will proceed as normal.
2142804d3121SMel Gorman 		 */
2143804d3121SMel Gorman 		cc->rescan = false;
2144804d3121SMel Gorman 		if (pageblock_start_pfn(last_migrated_pfn) ==
2145804d3121SMel Gorman 		    pageblock_start_pfn(start_pfn)) {
2146804d3121SMel Gorman 			cc->rescan = true;
2147804d3121SMel Gorman 		}
2148804d3121SMel Gorman 
214940cacbcbSMel Gorman 		switch (isolate_migratepages(cc->zone, cc)) {
2150f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
21512d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
21525733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
2153e64c5237SShaohua Li 			cc->nr_migratepages = 0;
2154566e54e1SMel Gorman 			last_migrated_pfn = 0;
2155f9e35b3bSMel Gorman 			goto out;
2156f9e35b3bSMel Gorman 		case ISOLATE_NONE:
21578854c55fSMel Gorman 			if (update_cached) {
21588854c55fSMel Gorman 				cc->zone->compact_cached_migrate_pfn[1] =
21598854c55fSMel Gorman 					cc->zone->compact_cached_migrate_pfn[0];
21608854c55fSMel Gorman 			}
21618854c55fSMel Gorman 
2162fdaf7f5cSVlastimil Babka 			/*
2163fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
2164fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
2165fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
2166fdaf7f5cSVlastimil Babka 			 */
2167fdaf7f5cSVlastimil Babka 			goto check_drain;
2168f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
21698854c55fSMel Gorman 			update_cached = false;
2170566e54e1SMel Gorman 			last_migrated_pfn = start_pfn;
2171f9e35b3bSMel Gorman 			;
2172f9e35b3bSMel Gorman 		}
2173748446bbSMel Gorman 
2174d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
2175e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
21767b2a2d4aSMel Gorman 				MR_COMPACTION);
2177748446bbSMel Gorman 
2178f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2179f8c9301fSVlastimil Babka 							&cc->migratepages);
2180748446bbSMel Gorman 
2181f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
2182f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
21839d502c1cSMinchan Kim 		if (err) {
21845733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
21857ed695e0SVlastimil Babka 			/*
21867ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
21877ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
21887ed695e0SVlastimil Babka 			 */
2189f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
21902d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
21914bf2bba3SDavid Rientjes 				goto out;
2192748446bbSMel Gorman 			}
2193fdd048e1SVlastimil Babka 			/*
2194fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
2195fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
2196fdd048e1SVlastimil Babka 			 */
2197fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
2198fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
2199fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
2200fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
2201fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
2202566e54e1SMel Gorman 				last_migrated_pfn = 0;
2203fdd048e1SVlastimil Babka 			}
22044bf2bba3SDavid Rientjes 		}
2205fdaf7f5cSVlastimil Babka 
2206fdaf7f5cSVlastimil Babka check_drain:
2207fdaf7f5cSVlastimil Babka 		/*
2208fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
2209fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
2210fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
2211fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
2212fdaf7f5cSVlastimil Babka 		 * would succeed.
2213fdaf7f5cSVlastimil Babka 		 */
2214566e54e1SMel Gorman 		if (cc->order > 0 && last_migrated_pfn) {
2215fdaf7f5cSVlastimil Babka 			int cpu;
2216fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
221706b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
2218fdaf7f5cSVlastimil Babka 
2219566e54e1SMel Gorman 			if (last_migrated_pfn < current_block_start) {
2220fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
2221fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
222240cacbcbSMel Gorman 				drain_local_pages(cc->zone);
2223fdaf7f5cSVlastimil Babka 				put_cpu();
2224fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
2225566e54e1SMel Gorman 				last_migrated_pfn = 0;
2226fdaf7f5cSVlastimil Babka 			}
2227fdaf7f5cSVlastimil Babka 		}
2228fdaf7f5cSVlastimil Babka 
22295e1f0f09SMel Gorman 		/* Stop if a page has been captured */
22305e1f0f09SMel Gorman 		if (capc && capc->page) {
22315e1f0f09SMel Gorman 			ret = COMPACT_SUCCESS;
22325e1f0f09SMel Gorman 			break;
22335e1f0f09SMel Gorman 		}
2234748446bbSMel Gorman 	}
2235748446bbSMel Gorman 
2236f9e35b3bSMel Gorman out:
22376bace090SVlastimil Babka 	/*
22386bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
22396bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
22406bace090SVlastimil Babka 	 */
22416bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
22426bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
22436bace090SVlastimil Babka 
22446bace090SVlastimil Babka 		cc->nr_freepages = 0;
22456bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
22466bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
224706b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
22486bace090SVlastimil Babka 		/*
22496bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
22506bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
22516bace090SVlastimil Babka 		 */
225240cacbcbSMel Gorman 		if (free_pfn > cc->zone->compact_cached_free_pfn)
225340cacbcbSMel Gorman 			cc->zone->compact_cached_free_pfn = free_pfn;
22546bace090SVlastimil Babka 	}
2255748446bbSMel Gorman 
22567f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
22577f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
22587f354a54SDavid Rientjes 
225916c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
226016c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
22610eb927c0SMel Gorman 
2262748446bbSMel Gorman 	return ret;
2263748446bbSMel Gorman }
226476ab0f53SMel Gorman 
2265ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
2266c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
22675e1f0f09SMel Gorman 		unsigned int alloc_flags, int classzone_idx,
22685e1f0f09SMel Gorman 		struct page **capture)
226956de7263SMel Gorman {
2270ea7ab982SMichal Hocko 	enum compact_result ret;
227156de7263SMel Gorman 	struct compact_control cc = {
227256de7263SMel Gorman 		.nr_freepages = 0,
227356de7263SMel Gorman 		.nr_migratepages = 0,
22747f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
22757f354a54SDavid Rientjes 		.total_free_scanned = 0,
227656de7263SMel Gorman 		.order = order,
2277dbe2d4e4SMel Gorman 		.search_order = order,
22786d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
227956de7263SMel Gorman 		.zone = zone,
2280a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
2281a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
2282ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
2283ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
2284accf6242SVlastimil Babka 		.direct_compaction = true,
2285a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
22869f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
22879f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
228856de7263SMel Gorman 	};
22895e1f0f09SMel Gorman 	struct capture_control capc = {
22905e1f0f09SMel Gorman 		.cc = &cc,
22915e1f0f09SMel Gorman 		.page = NULL,
22925e1f0f09SMel Gorman 	};
22935e1f0f09SMel Gorman 
22945e1f0f09SMel Gorman 	if (capture)
22955e1f0f09SMel Gorman 		current->capture_control = &capc;
229656de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
229756de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
229856de7263SMel Gorman 
22995e1f0f09SMel Gorman 	ret = compact_zone(&cc, &capc);
2300e64c5237SShaohua Li 
2301e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
2302e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
2303e64c5237SShaohua Li 
23045e1f0f09SMel Gorman 	*capture = capc.page;
23055e1f0f09SMel Gorman 	current->capture_control = NULL;
23065e1f0f09SMel Gorman 
2307e64c5237SShaohua Li 	return ret;
230856de7263SMel Gorman }
230956de7263SMel Gorman 
23105e771905SMel Gorman int sysctl_extfrag_threshold = 500;
23115e771905SMel Gorman 
231256de7263SMel Gorman /**
231356de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
231456de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
23151a6d53a1SVlastimil Babka  * @order: The order of the current allocation
23161a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
23171a6d53a1SVlastimil Babka  * @ac: The context of current allocation
2318112d2d29SYang Shi  * @prio: Determines how hard direct compaction should try to succeed
231956de7263SMel Gorman  *
232056de7263SMel Gorman  * This is the main entry point for direct page compaction.
232156de7263SMel Gorman  */
2322ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
2323c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
23245e1f0f09SMel Gorman 		enum compact_priority prio, struct page **capture)
232556de7263SMel Gorman {
232656de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
232756de7263SMel Gorman 	struct zoneref *z;
232856de7263SMel Gorman 	struct zone *zone;
23291d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
233056de7263SMel Gorman 
233173e64c51SMichal Hocko 	/*
233273e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
233373e64c51SMichal Hocko 	 * tricky context because the migration might require IO
233473e64c51SMichal Hocko 	 */
233573e64c51SMichal Hocko 	if (!may_perform_io)
233653853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
233756de7263SMel Gorman 
2338a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
2339837d026dSJoonsoo Kim 
234056de7263SMel Gorman 	/* Compact each zone in the list */
23411a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
23421a6d53a1SVlastimil Babka 								ac->nodemask) {
2343ea7ab982SMichal Hocko 		enum compact_result status;
234456de7263SMel Gorman 
2345a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
2346a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
23471d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
234853853e2dSVlastimil Babka 			continue;
23491d4746d3SMichal Hocko 		}
235053853e2dSVlastimil Babka 
2351a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
23525e1f0f09SMel Gorman 				alloc_flags, ac_classzone_idx(ac), capture);
235356de7263SMel Gorman 		rc = max(status, rc);
235456de7263SMel Gorman 
23557ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
23567ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
235753853e2dSVlastimil Babka 			/*
235853853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
235953853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
236053853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
236153853e2dSVlastimil Babka 			 * succeeds in this zone.
236253853e2dSVlastimil Babka 			 */
236353853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
23641f9efdefSVlastimil Babka 
2365c3486f53SVlastimil Babka 			break;
23661f9efdefSVlastimil Babka 		}
23671f9efdefSVlastimil Babka 
2368a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
2369c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
237053853e2dSVlastimil Babka 			/*
237153853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
237253853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
237353853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
237453853e2dSVlastimil Babka 			 */
237553853e2dSVlastimil Babka 			defer_compaction(zone, order);
23761f9efdefSVlastimil Babka 
23771f9efdefSVlastimil Babka 		/*
23781f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
23791f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
2380c3486f53SVlastimil Babka 		 * case do not try further zones
23811f9efdefSVlastimil Babka 		 */
2382c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2383c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
23841f9efdefSVlastimil Babka 			break;
23851f9efdefSVlastimil Babka 	}
23861f9efdefSVlastimil Babka 
238756de7263SMel Gorman 	return rc;
238856de7263SMel Gorman }
238956de7263SMel Gorman 
239056de7263SMel Gorman 
239176ab0f53SMel Gorman /* Compact all zones within a node */
23927103f16dSAndrew Morton static void compact_node(int nid)
23937be62de9SRik van Riel {
2394791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2395791cae96SVlastimil Babka 	int zoneid;
2396791cae96SVlastimil Babka 	struct zone *zone;
23977be62de9SRik van Riel 	struct compact_control cc = {
23987be62de9SRik van Riel 		.order = -1,
23997f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
24007f354a54SDavid Rientjes 		.total_free_scanned = 0,
2401e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
240291ca9186SDavid Rientjes 		.ignore_skip_hint = true,
240306ed2998SVlastimil Babka 		.whole_zone = true,
240473e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
24057be62de9SRik van Riel 	};
24067be62de9SRik van Riel 
2407791cae96SVlastimil Babka 
2408791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2409791cae96SVlastimil Babka 
2410791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2411791cae96SVlastimil Babka 		if (!populated_zone(zone))
2412791cae96SVlastimil Babka 			continue;
2413791cae96SVlastimil Babka 
2414791cae96SVlastimil Babka 		cc.nr_freepages = 0;
2415791cae96SVlastimil Babka 		cc.nr_migratepages = 0;
2416791cae96SVlastimil Babka 		cc.zone = zone;
2417791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
2418791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
2419791cae96SVlastimil Babka 
24205e1f0f09SMel Gorman 		compact_zone(&cc, NULL);
2421791cae96SVlastimil Babka 
2422791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2423791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2424791cae96SVlastimil Babka 	}
24257be62de9SRik van Riel }
24267be62de9SRik van Riel 
242776ab0f53SMel Gorman /* Compact all nodes in the system */
24287964c06dSJason Liu static void compact_nodes(void)
242976ab0f53SMel Gorman {
243076ab0f53SMel Gorman 	int nid;
243176ab0f53SMel Gorman 
24328575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
24338575ec29SHugh Dickins 	lru_add_drain_all();
24348575ec29SHugh Dickins 
243576ab0f53SMel Gorman 	for_each_online_node(nid)
243676ab0f53SMel Gorman 		compact_node(nid);
243776ab0f53SMel Gorman }
243876ab0f53SMel Gorman 
243976ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
244076ab0f53SMel Gorman int sysctl_compact_memory;
244176ab0f53SMel Gorman 
2442fec4eb2cSYaowei Bai /*
2443fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
2444fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
2445fec4eb2cSYaowei Bai  */
244676ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
244776ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
244876ab0f53SMel Gorman {
244976ab0f53SMel Gorman 	if (write)
24507964c06dSJason Liu 		compact_nodes();
245176ab0f53SMel Gorman 
245276ab0f53SMel Gorman 	return 0;
245376ab0f53SMel Gorman }
2454ed4a6d7fSMel Gorman 
2455ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
245674e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
245710fbcf4cSKay Sievers 			struct device_attribute *attr,
2458ed4a6d7fSMel Gorman 			const char *buf, size_t count)
2459ed4a6d7fSMel Gorman {
24608575ec29SHugh Dickins 	int nid = dev->id;
24618575ec29SHugh Dickins 
24628575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
24638575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
24648575ec29SHugh Dickins 		lru_add_drain_all();
24658575ec29SHugh Dickins 
24668575ec29SHugh Dickins 		compact_node(nid);
24678575ec29SHugh Dickins 	}
2468ed4a6d7fSMel Gorman 
2469ed4a6d7fSMel Gorman 	return count;
2470ed4a6d7fSMel Gorman }
24710825a6f9SJoe Perches static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node);
2472ed4a6d7fSMel Gorman 
2473ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
2474ed4a6d7fSMel Gorman {
247510fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
2476ed4a6d7fSMel Gorman }
2477ed4a6d7fSMel Gorman 
2478ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
2479ed4a6d7fSMel Gorman {
248010fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
2481ed4a6d7fSMel Gorman }
2482ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
2483ff9543fdSMichal Nazarewicz 
2484698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2485698b1b30SVlastimil Babka {
2486172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
2487698b1b30SVlastimil Babka }
2488698b1b30SVlastimil Babka 
2489698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
2490698b1b30SVlastimil Babka {
2491698b1b30SVlastimil Babka 	int zoneid;
2492698b1b30SVlastimil Babka 	struct zone *zone;
2493698b1b30SVlastimil Babka 	enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
2494698b1b30SVlastimil Babka 
24956cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
2496698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2497698b1b30SVlastimil Babka 
2498698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2499698b1b30SVlastimil Babka 			continue;
2500698b1b30SVlastimil Babka 
2501698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
2502698b1b30SVlastimil Babka 					classzone_idx) == COMPACT_CONTINUE)
2503698b1b30SVlastimil Babka 			return true;
2504698b1b30SVlastimil Babka 	}
2505698b1b30SVlastimil Babka 
2506698b1b30SVlastimil Babka 	return false;
2507698b1b30SVlastimil Babka }
2508698b1b30SVlastimil Babka 
2509698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
2510698b1b30SVlastimil Babka {
2511698b1b30SVlastimil Babka 	/*
2512698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
2513698b1b30SVlastimil Babka 	 * order is allocatable.
2514698b1b30SVlastimil Babka 	 */
2515698b1b30SVlastimil Babka 	int zoneid;
2516698b1b30SVlastimil Babka 	struct zone *zone;
2517698b1b30SVlastimil Babka 	struct compact_control cc = {
2518698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
2519dbe2d4e4SMel Gorman 		.search_order = pgdat->kcompactd_max_order,
25207f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
25217f354a54SDavid Rientjes 		.total_free_scanned = 0,
2522698b1b30SVlastimil Babka 		.classzone_idx = pgdat->kcompactd_classzone_idx,
2523698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
2524a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
252573e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
2526698b1b30SVlastimil Babka 	};
2527698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
2528698b1b30SVlastimil Babka 							cc.classzone_idx);
25297f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
2530698b1b30SVlastimil Babka 
25316cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
2532698b1b30SVlastimil Babka 		int status;
2533698b1b30SVlastimil Babka 
2534698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
2535698b1b30SVlastimil Babka 		if (!populated_zone(zone))
2536698b1b30SVlastimil Babka 			continue;
2537698b1b30SVlastimil Babka 
2538698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
2539698b1b30SVlastimil Babka 			continue;
2540698b1b30SVlastimil Babka 
2541698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
2542698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
2543698b1b30SVlastimil Babka 			continue;
2544698b1b30SVlastimil Babka 
2545698b1b30SVlastimil Babka 		cc.nr_freepages = 0;
2546698b1b30SVlastimil Babka 		cc.nr_migratepages = 0;
25477f354a54SDavid Rientjes 		cc.total_migrate_scanned = 0;
25487f354a54SDavid Rientjes 		cc.total_free_scanned = 0;
2549698b1b30SVlastimil Babka 		cc.zone = zone;
2550698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
2551698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
2552698b1b30SVlastimil Babka 
2553172400c6SVlastimil Babka 		if (kthread_should_stop())
2554172400c6SVlastimil Babka 			return;
25555e1f0f09SMel Gorman 		status = compact_zone(&cc, NULL);
2556698b1b30SVlastimil Babka 
25577ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
2558698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
2559c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
2560698b1b30SVlastimil Babka 			/*
2561bc3106b2SDavid Rientjes 			 * Buddy pages may become stranded on pcps that could
2562bc3106b2SDavid Rientjes 			 * otherwise coalesce on the zone's free area for
2563bc3106b2SDavid Rientjes 			 * order >= cc.order.  This is ratelimited by the
2564bc3106b2SDavid Rientjes 			 * upcoming deferral.
2565bc3106b2SDavid Rientjes 			 */
2566bc3106b2SDavid Rientjes 			drain_all_pages(zone);
2567bc3106b2SDavid Rientjes 
2568bc3106b2SDavid Rientjes 			/*
2569698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
2570698b1b30SVlastimil Babka 			 * sync direct compaction does.
2571698b1b30SVlastimil Babka 			 */
2572698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
2573698b1b30SVlastimil Babka 		}
2574698b1b30SVlastimil Babka 
25757f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
25767f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
25777f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
25787f354a54SDavid Rientjes 				     cc.total_free_scanned);
25797f354a54SDavid Rientjes 
2580698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2581698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2582698b1b30SVlastimil Babka 	}
2583698b1b30SVlastimil Babka 
2584698b1b30SVlastimil Babka 	/*
2585698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
2586698b1b30SVlastimil Babka 	 * the requested order/classzone_idx in case it was higher/tighter than
2587698b1b30SVlastimil Babka 	 * our current ones
2588698b1b30SVlastimil Babka 	 */
2589698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2590698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
2591698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2592698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2593698b1b30SVlastimil Babka }
2594698b1b30SVlastimil Babka 
2595698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2596698b1b30SVlastimil Babka {
2597698b1b30SVlastimil Babka 	if (!order)
2598698b1b30SVlastimil Babka 		return;
2599698b1b30SVlastimil Babka 
2600698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2601698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2602698b1b30SVlastimil Babka 
2603698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx > classzone_idx)
2604698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = classzone_idx;
2605698b1b30SVlastimil Babka 
26066818600fSDavidlohr Bueso 	/*
26076818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
26086818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
26096818600fSDavidlohr Bueso 	 */
26106818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2611698b1b30SVlastimil Babka 		return;
2612698b1b30SVlastimil Babka 
2613698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2614698b1b30SVlastimil Babka 		return;
2615698b1b30SVlastimil Babka 
2616698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2617698b1b30SVlastimil Babka 							classzone_idx);
2618698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2619698b1b30SVlastimil Babka }
2620698b1b30SVlastimil Babka 
2621698b1b30SVlastimil Babka /*
2622698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2623698b1b30SVlastimil Babka  * from the init process.
2624698b1b30SVlastimil Babka  */
2625698b1b30SVlastimil Babka static int kcompactd(void *p)
2626698b1b30SVlastimil Babka {
2627698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2628698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2629698b1b30SVlastimil Babka 
2630698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2631698b1b30SVlastimil Babka 
2632698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2633698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2634698b1b30SVlastimil Babka 
2635698b1b30SVlastimil Babka 	set_freezable();
2636698b1b30SVlastimil Babka 
2637698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
2638698b1b30SVlastimil Babka 	pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2639698b1b30SVlastimil Babka 
2640698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2641eb414681SJohannes Weiner 		unsigned long pflags;
2642eb414681SJohannes Weiner 
2643698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2644698b1b30SVlastimil Babka 		wait_event_freezable(pgdat->kcompactd_wait,
2645698b1b30SVlastimil Babka 				kcompactd_work_requested(pgdat));
2646698b1b30SVlastimil Babka 
2647eb414681SJohannes Weiner 		psi_memstall_enter(&pflags);
2648698b1b30SVlastimil Babka 		kcompactd_do_work(pgdat);
2649eb414681SJohannes Weiner 		psi_memstall_leave(&pflags);
2650698b1b30SVlastimil Babka 	}
2651698b1b30SVlastimil Babka 
2652698b1b30SVlastimil Babka 	return 0;
2653698b1b30SVlastimil Babka }
2654698b1b30SVlastimil Babka 
2655698b1b30SVlastimil Babka /*
2656698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2657698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2658698b1b30SVlastimil Babka  */
2659698b1b30SVlastimil Babka int kcompactd_run(int nid)
2660698b1b30SVlastimil Babka {
2661698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2662698b1b30SVlastimil Babka 	int ret = 0;
2663698b1b30SVlastimil Babka 
2664698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2665698b1b30SVlastimil Babka 		return 0;
2666698b1b30SVlastimil Babka 
2667698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2668698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2669698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2670698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2671698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2672698b1b30SVlastimil Babka 	}
2673698b1b30SVlastimil Babka 	return ret;
2674698b1b30SVlastimil Babka }
2675698b1b30SVlastimil Babka 
2676698b1b30SVlastimil Babka /*
2677698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2678698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2679698b1b30SVlastimil Babka  */
2680698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2681698b1b30SVlastimil Babka {
2682698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2683698b1b30SVlastimil Babka 
2684698b1b30SVlastimil Babka 	if (kcompactd) {
2685698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2686698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2687698b1b30SVlastimil Babka 	}
2688698b1b30SVlastimil Babka }
2689698b1b30SVlastimil Babka 
2690698b1b30SVlastimil Babka /*
2691698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2692698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2693698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2694698b1b30SVlastimil Babka  * restore their cpu bindings.
2695698b1b30SVlastimil Babka  */
2696e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2697698b1b30SVlastimil Babka {
2698698b1b30SVlastimil Babka 	int nid;
2699698b1b30SVlastimil Babka 
2700698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2701698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2702698b1b30SVlastimil Babka 		const struct cpumask *mask;
2703698b1b30SVlastimil Babka 
2704698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2705698b1b30SVlastimil Babka 
2706698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2707698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2708698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2709698b1b30SVlastimil Babka 	}
2710e46b1db2SAnna-Maria Gleixner 	return 0;
2711698b1b30SVlastimil Babka }
2712698b1b30SVlastimil Babka 
2713698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2714698b1b30SVlastimil Babka {
2715698b1b30SVlastimil Babka 	int nid;
2716e46b1db2SAnna-Maria Gleixner 	int ret;
2717e46b1db2SAnna-Maria Gleixner 
2718e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2719e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2720e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2721e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2722e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2723e46b1db2SAnna-Maria Gleixner 		return ret;
2724e46b1db2SAnna-Maria Gleixner 	}
2725698b1b30SVlastimil Babka 
2726698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2727698b1b30SVlastimil Babka 		kcompactd_run(nid);
2728698b1b30SVlastimil Babka 	return 0;
2729698b1b30SVlastimil Babka }
2730698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2731698b1b30SVlastimil Babka 
2732ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2733