xref: /openbmc/linux/mm/compaction.c (revision 21dc7e023611fbcf8e38f255731bcf3cc38e7638)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2748446bbSMel Gorman /*
3748446bbSMel Gorman  * linux/mm/compaction.c
4748446bbSMel Gorman  *
5748446bbSMel Gorman  * Memory compaction for the reduction of external fragmentation. Note that
6748446bbSMel Gorman  * this heavily depends upon page migration to do all the real heavy
7748446bbSMel Gorman  * lifting
8748446bbSMel Gorman  *
9748446bbSMel Gorman  * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
10748446bbSMel Gorman  */
11698b1b30SVlastimil Babka #include <linux/cpu.h>
12748446bbSMel Gorman #include <linux/swap.h>
13748446bbSMel Gorman #include <linux/migrate.h>
14748446bbSMel Gorman #include <linux/compaction.h>
15748446bbSMel Gorman #include <linux/mm_inline.h>
16174cd4b1SIngo Molnar #include <linux/sched/signal.h>
17748446bbSMel Gorman #include <linux/backing-dev.h>
1876ab0f53SMel Gorman #include <linux/sysctl.h>
19ed4a6d7fSMel Gorman #include <linux/sysfs.h>
20194159fbSMinchan Kim #include <linux/page-isolation.h>
21b8c73fc2SAndrey Ryabinin #include <linux/kasan.h>
22698b1b30SVlastimil Babka #include <linux/kthread.h>
23698b1b30SVlastimil Babka #include <linux/freezer.h>
2483358eceSJoonsoo Kim #include <linux/page_owner.h>
25748446bbSMel Gorman #include "internal.h"
26748446bbSMel Gorman 
27010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
28010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
29010fc29aSMinchan Kim {
30010fc29aSMinchan Kim 	count_vm_event(item);
31010fc29aSMinchan Kim }
32010fc29aSMinchan Kim 
33010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
34010fc29aSMinchan Kim {
35010fc29aSMinchan Kim 	count_vm_events(item, delta);
36010fc29aSMinchan Kim }
37010fc29aSMinchan Kim #else
38010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
39010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
40010fc29aSMinchan Kim #endif
41010fc29aSMinchan Kim 
42ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
43ff9543fdSMichal Nazarewicz 
44b7aba698SMel Gorman #define CREATE_TRACE_POINTS
45b7aba698SMel Gorman #include <trace/events/compaction.h>
46b7aba698SMel Gorman 
4706b6640aSVlastimil Babka #define block_start_pfn(pfn, order)	round_down(pfn, 1UL << (order))
4806b6640aSVlastimil Babka #define block_end_pfn(pfn, order)	ALIGN((pfn) + 1, 1UL << (order))
4906b6640aSVlastimil Babka #define pageblock_start_pfn(pfn)	block_start_pfn(pfn, pageblock_order)
5006b6640aSVlastimil Babka #define pageblock_end_pfn(pfn)		block_end_pfn(pfn, pageblock_order)
5106b6640aSVlastimil Babka 
52748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
53748446bbSMel Gorman {
54748446bbSMel Gorman 	struct page *page, *next;
556bace090SVlastimil Babka 	unsigned long high_pfn = 0;
56748446bbSMel Gorman 
57748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
586bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
59748446bbSMel Gorman 		list_del(&page->lru);
60748446bbSMel Gorman 		__free_page(page);
616bace090SVlastimil Babka 		if (pfn > high_pfn)
626bace090SVlastimil Babka 			high_pfn = pfn;
63748446bbSMel Gorman 	}
64748446bbSMel Gorman 
656bace090SVlastimil Babka 	return high_pfn;
66748446bbSMel Gorman }
67748446bbSMel Gorman 
68ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
69ff9543fdSMichal Nazarewicz {
7066c64223SJoonsoo Kim 	unsigned int i, order, nr_pages;
7166c64223SJoonsoo Kim 	struct page *page, *next;
7266c64223SJoonsoo Kim 	LIST_HEAD(tmp_list);
73ff9543fdSMichal Nazarewicz 
7466c64223SJoonsoo Kim 	list_for_each_entry_safe(page, next, list, lru) {
7566c64223SJoonsoo Kim 		list_del(&page->lru);
7666c64223SJoonsoo Kim 
7766c64223SJoonsoo Kim 		order = page_private(page);
7866c64223SJoonsoo Kim 		nr_pages = 1 << order;
7966c64223SJoonsoo Kim 
8046f24fd8SJoonsoo Kim 		post_alloc_hook(page, order, __GFP_MOVABLE);
8166c64223SJoonsoo Kim 		if (order)
8266c64223SJoonsoo Kim 			split_page(page, order);
8366c64223SJoonsoo Kim 
8466c64223SJoonsoo Kim 		for (i = 0; i < nr_pages; i++) {
8566c64223SJoonsoo Kim 			list_add(&page->lru, &tmp_list);
8666c64223SJoonsoo Kim 			page++;
87ff9543fdSMichal Nazarewicz 		}
88ff9543fdSMichal Nazarewicz 	}
89ff9543fdSMichal Nazarewicz 
9066c64223SJoonsoo Kim 	list_splice(&tmp_list, list);
9166c64223SJoonsoo Kim }
9266c64223SJoonsoo Kim 
93bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
9424e2716fSJoonsoo Kim 
95bda807d4SMinchan Kim int PageMovable(struct page *page)
96bda807d4SMinchan Kim {
97bda807d4SMinchan Kim 	struct address_space *mapping;
98bda807d4SMinchan Kim 
99bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
100bda807d4SMinchan Kim 	if (!__PageMovable(page))
101bda807d4SMinchan Kim 		return 0;
102bda807d4SMinchan Kim 
103bda807d4SMinchan Kim 	mapping = page_mapping(page);
104bda807d4SMinchan Kim 	if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
105bda807d4SMinchan Kim 		return 1;
106bda807d4SMinchan Kim 
107bda807d4SMinchan Kim 	return 0;
108bda807d4SMinchan Kim }
109bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable);
110bda807d4SMinchan Kim 
111bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping)
112bda807d4SMinchan Kim {
113bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
114bda807d4SMinchan Kim 	VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
115bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
116bda807d4SMinchan Kim }
117bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable);
118bda807d4SMinchan Kim 
119bda807d4SMinchan Kim void __ClearPageMovable(struct page *page)
120bda807d4SMinchan Kim {
121bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageLocked(page), page);
122bda807d4SMinchan Kim 	VM_BUG_ON_PAGE(!PageMovable(page), page);
123bda807d4SMinchan Kim 	/*
124bda807d4SMinchan Kim 	 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
125bda807d4SMinchan Kim 	 * flag so that VM can catch up released page by driver after isolation.
126bda807d4SMinchan Kim 	 * With it, VM migration doesn't try to put it back.
127bda807d4SMinchan Kim 	 */
128bda807d4SMinchan Kim 	page->mapping = (void *)((unsigned long)page->mapping &
129bda807d4SMinchan Kim 				PAGE_MAPPING_MOVABLE);
130bda807d4SMinchan Kim }
131bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable);
132bda807d4SMinchan Kim 
13324e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
13424e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
13524e2716fSJoonsoo Kim 
13624e2716fSJoonsoo Kim /*
13724e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
13824e2716fSJoonsoo Kim  * allocation success. 1 << compact_defer_limit compactions are skipped up
13924e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
14024e2716fSJoonsoo Kim  */
14124e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
14224e2716fSJoonsoo Kim {
14324e2716fSJoonsoo Kim 	zone->compact_considered = 0;
14424e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
14524e2716fSJoonsoo Kim 
14624e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
14724e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
14824e2716fSJoonsoo Kim 
14924e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
15024e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
15124e2716fSJoonsoo Kim 
15224e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
15324e2716fSJoonsoo Kim }
15424e2716fSJoonsoo Kim 
15524e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
15624e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
15724e2716fSJoonsoo Kim {
15824e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
15924e2716fSJoonsoo Kim 
16024e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
16124e2716fSJoonsoo Kim 		return false;
16224e2716fSJoonsoo Kim 
16324e2716fSJoonsoo Kim 	/* Avoid possible overflow */
16424e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
16524e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
16624e2716fSJoonsoo Kim 
16724e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
16824e2716fSJoonsoo Kim 		return false;
16924e2716fSJoonsoo Kim 
17024e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
17124e2716fSJoonsoo Kim 
17224e2716fSJoonsoo Kim 	return true;
17324e2716fSJoonsoo Kim }
17424e2716fSJoonsoo Kim 
17524e2716fSJoonsoo Kim /*
17624e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
17724e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
17824e2716fSJoonsoo Kim  * expected to succeed.
17924e2716fSJoonsoo Kim  */
18024e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
18124e2716fSJoonsoo Kim 		bool alloc_success)
18224e2716fSJoonsoo Kim {
18324e2716fSJoonsoo Kim 	if (alloc_success) {
18424e2716fSJoonsoo Kim 		zone->compact_considered = 0;
18524e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
18624e2716fSJoonsoo Kim 	}
18724e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
18824e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
18924e2716fSJoonsoo Kim 
19024e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
19124e2716fSJoonsoo Kim }
19224e2716fSJoonsoo Kim 
19324e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
19424e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
19524e2716fSJoonsoo Kim {
19624e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
19724e2716fSJoonsoo Kim 		return false;
19824e2716fSJoonsoo Kim 
19924e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
20024e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
20124e2716fSJoonsoo Kim }
20224e2716fSJoonsoo Kim 
203bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
204bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
205bb13ffebSMel Gorman 					struct page *page)
206bb13ffebSMel Gorman {
207bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
208bb13ffebSMel Gorman 		return true;
209bb13ffebSMel Gorman 
210bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
211bb13ffebSMel Gorman }
212bb13ffebSMel Gorman 
21302333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
21402333641SVlastimil Babka {
21502333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
21602333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
217623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
21806b6640aSVlastimil Babka 				pageblock_start_pfn(zone_end_pfn(zone) - 1);
21902333641SVlastimil Babka }
22002333641SVlastimil Babka 
221bb13ffebSMel Gorman /*
222*21dc7e02SDavid Rientjes  * Hugetlbfs pages should consistenly be skipped until updated by the hugetlb
223*21dc7e02SDavid Rientjes  * subsystem.  It is always pointless to compact pages of pageblock_order and
224*21dc7e02SDavid Rientjes  * the free scanner can reconsider when no longer huge.
225*21dc7e02SDavid Rientjes  */
226*21dc7e02SDavid Rientjes static bool pageblock_skip_persistent(struct page *page, unsigned int order)
227*21dc7e02SDavid Rientjes {
228*21dc7e02SDavid Rientjes 	if (!PageHuge(page))
229*21dc7e02SDavid Rientjes 		return false;
230*21dc7e02SDavid Rientjes 	if (order != pageblock_order)
231*21dc7e02SDavid Rientjes 		return false;
232*21dc7e02SDavid Rientjes 	return true;
233*21dc7e02SDavid Rientjes }
234*21dc7e02SDavid Rientjes 
235*21dc7e02SDavid Rientjes /*
236bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
237bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
238bb13ffebSMel Gorman  * meet.
239bb13ffebSMel Gorman  */
24062997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
241bb13ffebSMel Gorman {
242bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
243108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
244bb13ffebSMel Gorman 	unsigned long pfn;
245bb13ffebSMel Gorman 
24662997027SMel Gorman 	zone->compact_blockskip_flush = false;
247bb13ffebSMel Gorman 
248bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
249bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
250bb13ffebSMel Gorman 		struct page *page;
251bb13ffebSMel Gorman 
252bb13ffebSMel Gorman 		cond_resched();
253bb13ffebSMel Gorman 
254ccbe1e4dSMichal Hocko 		page = pfn_to_online_page(pfn);
255ccbe1e4dSMichal Hocko 		if (!page)
256bb13ffebSMel Gorman 			continue;
257bb13ffebSMel Gorman 		if (zone != page_zone(page))
258bb13ffebSMel Gorman 			continue;
259*21dc7e02SDavid Rientjes 		if (pageblock_skip_persistent(page, compound_order(page)))
260*21dc7e02SDavid Rientjes 			continue;
261bb13ffebSMel Gorman 
262bb13ffebSMel Gorman 		clear_pageblock_skip(page);
263bb13ffebSMel Gorman 	}
26402333641SVlastimil Babka 
26502333641SVlastimil Babka 	reset_cached_positions(zone);
266bb13ffebSMel Gorman }
267bb13ffebSMel Gorman 
26862997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
26962997027SMel Gorman {
27062997027SMel Gorman 	int zoneid;
27162997027SMel Gorman 
27262997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
27362997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
27462997027SMel Gorman 		if (!populated_zone(zone))
27562997027SMel Gorman 			continue;
27662997027SMel Gorman 
27762997027SMel Gorman 		/* Only flush if a full compaction finished recently */
27862997027SMel Gorman 		if (zone->compact_blockskip_flush)
27962997027SMel Gorman 			__reset_isolation_suitable(zone);
28062997027SMel Gorman 	}
28162997027SMel Gorman }
28262997027SMel Gorman 
283bb13ffebSMel Gorman /*
284bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
28562997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
286bb13ffebSMel Gorman  */
287c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
288c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
289edc2ca61SVlastimil Babka 			bool migrate_scanner)
290bb13ffebSMel Gorman {
291c89511abSMel Gorman 	struct zone *zone = cc->zone;
29235979ef3SDavid Rientjes 	unsigned long pfn;
2936815bf3fSJoonsoo Kim 
2946815bf3fSJoonsoo Kim 	if (cc->ignore_skip_hint)
2956815bf3fSJoonsoo Kim 		return;
2966815bf3fSJoonsoo Kim 
297bb13ffebSMel Gorman 	if (!page)
298bb13ffebSMel Gorman 		return;
299bb13ffebSMel Gorman 
30035979ef3SDavid Rientjes 	if (nr_isolated)
30135979ef3SDavid Rientjes 		return;
30235979ef3SDavid Rientjes 
303bb13ffebSMel Gorman 	set_pageblock_skip(page);
304c89511abSMel Gorman 
30535979ef3SDavid Rientjes 	pfn = page_to_pfn(page);
30635979ef3SDavid Rientjes 
30735979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
308c89511abSMel Gorman 	if (migrate_scanner) {
30935979ef3SDavid Rientjes 		if (pfn > zone->compact_cached_migrate_pfn[0])
31035979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = pfn;
311e0b9daebSDavid Rientjes 		if (cc->mode != MIGRATE_ASYNC &&
312e0b9daebSDavid Rientjes 		    pfn > zone->compact_cached_migrate_pfn[1])
31335979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = pfn;
314c89511abSMel Gorman 	} else {
31535979ef3SDavid Rientjes 		if (pfn < zone->compact_cached_free_pfn)
316c89511abSMel Gorman 			zone->compact_cached_free_pfn = pfn;
317c89511abSMel Gorman 	}
318c89511abSMel Gorman }
319bb13ffebSMel Gorman #else
320bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
321bb13ffebSMel Gorman 					struct page *page)
322bb13ffebSMel Gorman {
323bb13ffebSMel Gorman 	return true;
324bb13ffebSMel Gorman }
325bb13ffebSMel Gorman 
326*21dc7e02SDavid Rientjes static inline bool pageblock_skip_persistent(struct page *page,
327*21dc7e02SDavid Rientjes 					     unsigned int order)
328*21dc7e02SDavid Rientjes {
329*21dc7e02SDavid Rientjes 	return false;
330*21dc7e02SDavid Rientjes }
331*21dc7e02SDavid Rientjes 
332*21dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc,
333c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
334edc2ca61SVlastimil Babka 			bool migrate_scanner)
335bb13ffebSMel Gorman {
336bb13ffebSMel Gorman }
337bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
338bb13ffebSMel Gorman 
3391f9efdefSVlastimil Babka /*
3408b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
3418b44d279SVlastimil Babka  * very heavily contended. For async compaction, back out if the lock cannot
3428b44d279SVlastimil Babka  * be taken immediately. For sync compaction, spin on the lock if needed.
3438b44d279SVlastimil Babka  *
3448b44d279SVlastimil Babka  * Returns true if the lock is held
3458b44d279SVlastimil Babka  * Returns false if the lock is not held and compaction should abort
3461f9efdefSVlastimil Babka  */
3478b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
3488b44d279SVlastimil Babka 						struct compact_control *cc)
3498b44d279SVlastimil Babka {
3508b44d279SVlastimil Babka 	if (cc->mode == MIGRATE_ASYNC) {
3518b44d279SVlastimil Babka 		if (!spin_trylock_irqsave(lock, *flags)) {
352c3486f53SVlastimil Babka 			cc->contended = true;
3538b44d279SVlastimil Babka 			return false;
3548b44d279SVlastimil Babka 		}
3558b44d279SVlastimil Babka 	} else {
3568b44d279SVlastimil Babka 		spin_lock_irqsave(lock, *flags);
3578b44d279SVlastimil Babka 	}
3581f9efdefSVlastimil Babka 
3598b44d279SVlastimil Babka 	return true;
3602a1402aaSMel Gorman }
3612a1402aaSMel Gorman 
36285aa125fSMichal Nazarewicz /*
363c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
3648b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
3658b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
3668b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
3678b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
3688b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
3698b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
3708b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
371c67fe375SMel Gorman  *
3728b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
3738b44d279SVlastimil Babka  *		async compaction due to need_resched()
3748b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
3758b44d279SVlastimil Babka  *		scheduled)
376c67fe375SMel Gorman  */
3778b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
3788b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
379c67fe375SMel Gorman {
3808b44d279SVlastimil Babka 	if (*locked) {
3818b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
3828b44d279SVlastimil Babka 		*locked = false;
383c67fe375SMel Gorman 	}
384c67fe375SMel Gorman 
3858b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
386c3486f53SVlastimil Babka 		cc->contended = true;
3878b44d279SVlastimil Babka 		return true;
3888b44d279SVlastimil Babka 	}
3898b44d279SVlastimil Babka 
3908b44d279SVlastimil Babka 	if (need_resched()) {
391e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC) {
392c3486f53SVlastimil Babka 			cc->contended = true;
3938b44d279SVlastimil Babka 			return true;
394c67fe375SMel Gorman 		}
395c67fe375SMel Gorman 		cond_resched();
396c67fe375SMel Gorman 	}
397c67fe375SMel Gorman 
3988b44d279SVlastimil Babka 	return false;
399c67fe375SMel Gorman }
400c67fe375SMel Gorman 
401be976572SVlastimil Babka /*
402be976572SVlastimil Babka  * Aside from avoiding lock contention, compaction also periodically checks
403be976572SVlastimil Babka  * need_resched() and either schedules in sync compaction or aborts async
4048b44d279SVlastimil Babka  * compaction. This is similar to what compact_unlock_should_abort() does, but
405be976572SVlastimil Babka  * is used where no lock is concerned.
406be976572SVlastimil Babka  *
407be976572SVlastimil Babka  * Returns false when no scheduling was needed, or sync compaction scheduled.
408be976572SVlastimil Babka  * Returns true when async compaction should abort.
409be976572SVlastimil Babka  */
410be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc)
411be976572SVlastimil Babka {
412be976572SVlastimil Babka 	/* async compaction aborts if contended */
413be976572SVlastimil Babka 	if (need_resched()) {
414be976572SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC) {
415c3486f53SVlastimil Babka 			cc->contended = true;
416be976572SVlastimil Babka 			return true;
417be976572SVlastimil Babka 		}
418be976572SVlastimil Babka 
419be976572SVlastimil Babka 		cond_resched();
420be976572SVlastimil Babka 	}
421be976572SVlastimil Babka 
422be976572SVlastimil Babka 	return false;
423be976572SVlastimil Babka }
424be976572SVlastimil Babka 
425c67fe375SMel Gorman /*
4269e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
4279e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
4289e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
42985aa125fSMichal Nazarewicz  */
430f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
431e14c720eSVlastimil Babka 				unsigned long *start_pfn,
43285aa125fSMichal Nazarewicz 				unsigned long end_pfn,
43385aa125fSMichal Nazarewicz 				struct list_head *freelist,
43485aa125fSMichal Nazarewicz 				bool strict)
435748446bbSMel Gorman {
436b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
437bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
438b8b2d825SXiubo Li 	unsigned long flags = 0;
439f40d1e42SMel Gorman 	bool locked = false;
440e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
44166c64223SJoonsoo Kim 	unsigned int order;
442748446bbSMel Gorman 
443748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
444748446bbSMel Gorman 
445f40d1e42SMel Gorman 	/* Isolate free pages. */
446748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
44766c64223SJoonsoo Kim 		int isolated;
448748446bbSMel Gorman 		struct page *page = cursor;
449748446bbSMel Gorman 
4508b44d279SVlastimil Babka 		/*
4518b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
4528b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
4538b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
4548b44d279SVlastimil Babka 		 */
4558b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
4568b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
4578b44d279SVlastimil Babka 								&locked, cc))
4588b44d279SVlastimil Babka 			break;
4598b44d279SVlastimil Babka 
460b7aba698SMel Gorman 		nr_scanned++;
461f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
4622af120bcSLaura Abbott 			goto isolate_fail;
4632af120bcSLaura Abbott 
464bb13ffebSMel Gorman 		if (!valid_page)
465bb13ffebSMel Gorman 			valid_page = page;
4669fcd6d2eSVlastimil Babka 
4679fcd6d2eSVlastimil Babka 		/*
4689fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
4699fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
4709fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
4719fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
4729fcd6d2eSVlastimil Babka 		 */
4739fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
474*21dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
4759fcd6d2eSVlastimil Babka 
476*21dc7e02SDavid Rientjes 			if (pageblock_skip_persistent(page, order)) {
477*21dc7e02SDavid Rientjes 				set_pageblock_skip(page);
478*21dc7e02SDavid Rientjes 				blockpfn = end_pfn;
479*21dc7e02SDavid Rientjes 			} else if (likely(order < MAX_ORDER)) {
480*21dc7e02SDavid Rientjes 				blockpfn += (1UL << order) - 1;
481*21dc7e02SDavid Rientjes 				cursor += (1UL << order) - 1;
4829fcd6d2eSVlastimil Babka 			}
4839fcd6d2eSVlastimil Babka 			goto isolate_fail;
4849fcd6d2eSVlastimil Babka 		}
4859fcd6d2eSVlastimil Babka 
486f40d1e42SMel Gorman 		if (!PageBuddy(page))
4872af120bcSLaura Abbott 			goto isolate_fail;
488f40d1e42SMel Gorman 
489f40d1e42SMel Gorman 		/*
49069b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
49169b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
49269b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
49369b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
49469b7189fSVlastimil Babka 		 * recheck as well.
49569b7189fSVlastimil Babka 		 */
49669b7189fSVlastimil Babka 		if (!locked) {
49769b7189fSVlastimil Babka 			/*
498f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
499f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
500f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
501f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
502f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
503f40d1e42SMel Gorman 			 * possible.
504f40d1e42SMel Gorman 			 */
5058b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
5068b44d279SVlastimil Babka 								&flags, cc);
507f40d1e42SMel Gorman 			if (!locked)
508f40d1e42SMel Gorman 				break;
509f40d1e42SMel Gorman 
510f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
511f40d1e42SMel Gorman 			if (!PageBuddy(page))
5122af120bcSLaura Abbott 				goto isolate_fail;
51369b7189fSVlastimil Babka 		}
514748446bbSMel Gorman 
51566c64223SJoonsoo Kim 		/* Found a free page, will break it into order-0 pages */
51666c64223SJoonsoo Kim 		order = page_order(page);
51766c64223SJoonsoo Kim 		isolated = __isolate_free_page(page, order);
518a4f04f2cSDavid Rientjes 		if (!isolated)
519a4f04f2cSDavid Rientjes 			break;
52066c64223SJoonsoo Kim 		set_page_private(page, order);
521a4f04f2cSDavid Rientjes 
522748446bbSMel Gorman 		total_isolated += isolated;
523a4f04f2cSDavid Rientjes 		cc->nr_freepages += isolated;
52466c64223SJoonsoo Kim 		list_add_tail(&page->lru, freelist);
52566c64223SJoonsoo Kim 
526a4f04f2cSDavid Rientjes 		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
527932ff6bbSJoonsoo Kim 			blockpfn += isolated;
528932ff6bbSJoonsoo Kim 			break;
529932ff6bbSJoonsoo Kim 		}
530a4f04f2cSDavid Rientjes 		/* Advance to the end of split page */
531748446bbSMel Gorman 		blockpfn += isolated - 1;
532748446bbSMel Gorman 		cursor += isolated - 1;
5332af120bcSLaura Abbott 		continue;
5342af120bcSLaura Abbott 
5352af120bcSLaura Abbott isolate_fail:
5362af120bcSLaura Abbott 		if (strict)
5372af120bcSLaura Abbott 			break;
5382af120bcSLaura Abbott 		else
5392af120bcSLaura Abbott 			continue;
5402af120bcSLaura Abbott 
541748446bbSMel Gorman 	}
542748446bbSMel Gorman 
543a4f04f2cSDavid Rientjes 	if (locked)
544a4f04f2cSDavid Rientjes 		spin_unlock_irqrestore(&cc->zone->lock, flags);
545a4f04f2cSDavid Rientjes 
5469fcd6d2eSVlastimil Babka 	/*
5479fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
5489fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
5499fcd6d2eSVlastimil Babka 	 */
5509fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
5519fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
5529fcd6d2eSVlastimil Babka 
553e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
554e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
555e34d85f0SJoonsoo Kim 
556e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
557e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
558e14c720eSVlastimil Babka 
559f40d1e42SMel Gorman 	/*
560f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
561f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
562f40d1e42SMel Gorman 	 * returned and CMA will fail.
563f40d1e42SMel Gorman 	 */
5642af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
565f40d1e42SMel Gorman 		total_isolated = 0;
566f40d1e42SMel Gorman 
567bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
568bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
569edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
570bb13ffebSMel Gorman 
5717f354a54SDavid Rientjes 	cc->total_free_scanned += nr_scanned;
572397487dbSMel Gorman 	if (total_isolated)
573010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
574748446bbSMel Gorman 	return total_isolated;
575748446bbSMel Gorman }
576748446bbSMel Gorman 
57785aa125fSMichal Nazarewicz /**
57885aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
57985aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
58085aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
58185aa125fSMichal Nazarewicz  *
58285aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
58385aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
58485aa125fSMichal Nazarewicz  * undo its actions and return zero.
58585aa125fSMichal Nazarewicz  *
58685aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
58785aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
58885aa125fSMichal Nazarewicz  * a free page).
58985aa125fSMichal Nazarewicz  */
590ff9543fdSMichal Nazarewicz unsigned long
591bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
592bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
59385aa125fSMichal Nazarewicz {
594e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
59585aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
59685aa125fSMichal Nazarewicz 
5977d49d886SVlastimil Babka 	pfn = start_pfn;
59806b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
599e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
600e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
60106b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
6027d49d886SVlastimil Babka 
6037d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
604e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
6057d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
606e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
607e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
6087d49d886SVlastimil Babka 
60985aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
61085aa125fSMichal Nazarewicz 
61158420016SJoonsoo Kim 		/*
61258420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
61358420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
61458420016SJoonsoo Kim 		 * scanning range to right one.
61558420016SJoonsoo Kim 		 */
61658420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
61706b6640aSVlastimil Babka 			block_start_pfn = pageblock_start_pfn(pfn);
61806b6640aSVlastimil Babka 			block_end_pfn = pageblock_end_pfn(pfn);
61958420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
62058420016SJoonsoo Kim 		}
62158420016SJoonsoo Kim 
622e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
623e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
6247d49d886SVlastimil Babka 			break;
6257d49d886SVlastimil Babka 
626e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
627e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
62885aa125fSMichal Nazarewicz 
62985aa125fSMichal Nazarewicz 		/*
63085aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
63185aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
63285aa125fSMichal Nazarewicz 		 * non-free pages).
63385aa125fSMichal Nazarewicz 		 */
63485aa125fSMichal Nazarewicz 		if (!isolated)
63585aa125fSMichal Nazarewicz 			break;
63685aa125fSMichal Nazarewicz 
63785aa125fSMichal Nazarewicz 		/*
63885aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
63985aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
64085aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
64185aa125fSMichal Nazarewicz 		 */
64285aa125fSMichal Nazarewicz 	}
64385aa125fSMichal Nazarewicz 
64466c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
64585aa125fSMichal Nazarewicz 	map_pages(&freelist);
64685aa125fSMichal Nazarewicz 
64785aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
64885aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
64985aa125fSMichal Nazarewicz 		release_freepages(&freelist);
65085aa125fSMichal Nazarewicz 		return 0;
65185aa125fSMichal Nazarewicz 	}
65285aa125fSMichal Nazarewicz 
65385aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
65485aa125fSMichal Nazarewicz 	return pfn;
65585aa125fSMichal Nazarewicz }
65685aa125fSMichal Nazarewicz 
657748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
658748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
659748446bbSMel Gorman {
660bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
661748446bbSMel Gorman 
662599d0c95SMel Gorman 	inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
663599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
664599d0c95SMel Gorman 	active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
665599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
666599d0c95SMel Gorman 	isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
667599d0c95SMel Gorman 			node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
668748446bbSMel Gorman 
669bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
670748446bbSMel Gorman }
671748446bbSMel Gorman 
6722fe86e00SMichal Nazarewicz /**
673edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
674edc2ca61SVlastimil Babka  *				  a single pageblock
6752fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
676edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
677edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
678edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
6792fe86e00SMichal Nazarewicz  *
6802fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
681edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
682edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
683edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
684edc2ca61SVlastimil Babka  * than end_pfn).
6852fe86e00SMichal Nazarewicz  *
686edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
687edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
688edc2ca61SVlastimil Babka  * is neither read nor updated.
689748446bbSMel Gorman  */
690edc2ca61SVlastimil Babka static unsigned long
691edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
692edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
693748446bbSMel Gorman {
694edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
695b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
696fa9add64SHugh Dickins 	struct lruvec *lruvec;
697b8b2d825SXiubo Li 	unsigned long flags = 0;
6982a1402aaSMel Gorman 	bool locked = false;
699bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
700e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
701fdd048e1SVlastimil Babka 	bool skip_on_failure = false;
702fdd048e1SVlastimil Babka 	unsigned long next_skip_pfn = 0;
703748446bbSMel Gorman 
704748446bbSMel Gorman 	/*
705748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
706748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
707748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
708748446bbSMel Gorman 	 */
709748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
710f9e35b3bSMel Gorman 		/* async migration should just abort */
711e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
7122fe86e00SMichal Nazarewicz 			return 0;
713f9e35b3bSMel Gorman 
714748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
715748446bbSMel Gorman 
716748446bbSMel Gorman 		if (fatal_signal_pending(current))
7172fe86e00SMichal Nazarewicz 			return 0;
718748446bbSMel Gorman 	}
719748446bbSMel Gorman 
720be976572SVlastimil Babka 	if (compact_should_abort(cc))
721aeef4b83SDavid Rientjes 		return 0;
722aeef4b83SDavid Rientjes 
723fdd048e1SVlastimil Babka 	if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
724fdd048e1SVlastimil Babka 		skip_on_failure = true;
725fdd048e1SVlastimil Babka 		next_skip_pfn = block_end_pfn(low_pfn, cc->order);
726fdd048e1SVlastimil Babka 	}
727fdd048e1SVlastimil Babka 
728748446bbSMel Gorman 	/* Time to isolate some pages for migration */
729748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
73029c0dde8SVlastimil Babka 
731fdd048e1SVlastimil Babka 		if (skip_on_failure && low_pfn >= next_skip_pfn) {
732fdd048e1SVlastimil Babka 			/*
733fdd048e1SVlastimil Babka 			 * We have isolated all migration candidates in the
734fdd048e1SVlastimil Babka 			 * previous order-aligned block, and did not skip it due
735fdd048e1SVlastimil Babka 			 * to failure. We should migrate the pages now and
736fdd048e1SVlastimil Babka 			 * hopefully succeed compaction.
737fdd048e1SVlastimil Babka 			 */
738fdd048e1SVlastimil Babka 			if (nr_isolated)
739fdd048e1SVlastimil Babka 				break;
740fdd048e1SVlastimil Babka 
741fdd048e1SVlastimil Babka 			/*
742fdd048e1SVlastimil Babka 			 * We failed to isolate in the previous order-aligned
743fdd048e1SVlastimil Babka 			 * block. Set the new boundary to the end of the
744fdd048e1SVlastimil Babka 			 * current block. Note we can't simply increase
745fdd048e1SVlastimil Babka 			 * next_skip_pfn by 1 << order, as low_pfn might have
746fdd048e1SVlastimil Babka 			 * been incremented by a higher number due to skipping
747fdd048e1SVlastimil Babka 			 * a compound or a high-order buddy page in the
748fdd048e1SVlastimil Babka 			 * previous loop iteration.
749fdd048e1SVlastimil Babka 			 */
750fdd048e1SVlastimil Babka 			next_skip_pfn = block_end_pfn(low_pfn, cc->order);
751fdd048e1SVlastimil Babka 		}
752fdd048e1SVlastimil Babka 
7538b44d279SVlastimil Babka 		/*
7548b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
7558b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
7568b44d279SVlastimil Babka 		 * if contended.
7578b44d279SVlastimil Babka 		 */
7588b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
759a52633d8SMel Gorman 		    && compact_unlock_should_abort(zone_lru_lock(zone), flags,
7608b44d279SVlastimil Babka 								&locked, cc))
7618b44d279SVlastimil Babka 			break;
762b2eef8c0SAndrea Arcangeli 
763748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
764fdd048e1SVlastimil Babka 			goto isolate_fail;
765b7aba698SMel Gorman 		nr_scanned++;
766748446bbSMel Gorman 
767748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
768dc908600SMel Gorman 
769bb13ffebSMel Gorman 		if (!valid_page)
770bb13ffebSMel Gorman 			valid_page = page;
771bb13ffebSMel Gorman 
772c122b208SJoonsoo Kim 		/*
77399c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
77499c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
77599c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
77699c0fd5eSVlastimil Babka 		 * potential isolation targets.
7776c14466cSMel Gorman 		 */
77899c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
77999c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
78099c0fd5eSVlastimil Babka 
78199c0fd5eSVlastimil Babka 			/*
78299c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
78399c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
78499c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
78599c0fd5eSVlastimil Babka 			 */
78699c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
78799c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
788748446bbSMel Gorman 			continue;
78999c0fd5eSVlastimil Babka 		}
790748446bbSMel Gorman 
7919927af74SMel Gorman 		/*
79229c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
79329c0dde8SVlastimil Babka 		 * hugetlbfs are not to be compacted. We can potentially save
79429c0dde8SVlastimil Babka 		 * a lot of iterations if we skip them at once. The check is
79529c0dde8SVlastimil Babka 		 * racy, but we can consider only valid values and the only
79629c0dde8SVlastimil Babka 		 * danger is skipping too much.
797bc835011SAndrea Arcangeli 		 */
79829c0dde8SVlastimil Babka 		if (PageCompound(page)) {
799*21dc7e02SDavid Rientjes 			const unsigned int order = compound_order(page);
80029c0dde8SVlastimil Babka 
801*21dc7e02SDavid Rientjes 			if (pageblock_skip_persistent(page, order)) {
802*21dc7e02SDavid Rientjes 				set_pageblock_skip(page);
803*21dc7e02SDavid Rientjes 				low_pfn = end_pfn;
804*21dc7e02SDavid Rientjes 			} else if (likely(order < MAX_ORDER))
805*21dc7e02SDavid Rientjes 				low_pfn += (1UL << order) - 1;
806fdd048e1SVlastimil Babka 			goto isolate_fail;
8072a1402aaSMel Gorman 		}
8082a1402aaSMel Gorman 
809bda807d4SMinchan Kim 		/*
810bda807d4SMinchan Kim 		 * Check may be lockless but that's ok as we recheck later.
811bda807d4SMinchan Kim 		 * It's possible to migrate LRU and non-lru movable pages.
812bda807d4SMinchan Kim 		 * Skip any other type of page
813bda807d4SMinchan Kim 		 */
814bda807d4SMinchan Kim 		if (!PageLRU(page)) {
815bda807d4SMinchan Kim 			/*
816bda807d4SMinchan Kim 			 * __PageMovable can return false positive so we need
817bda807d4SMinchan Kim 			 * to verify it under page_lock.
818bda807d4SMinchan Kim 			 */
819bda807d4SMinchan Kim 			if (unlikely(__PageMovable(page)) &&
820bda807d4SMinchan Kim 					!PageIsolated(page)) {
821bda807d4SMinchan Kim 				if (locked) {
822a52633d8SMel Gorman 					spin_unlock_irqrestore(zone_lru_lock(zone),
823bda807d4SMinchan Kim 									flags);
824bda807d4SMinchan Kim 					locked = false;
825bda807d4SMinchan Kim 				}
826bda807d4SMinchan Kim 
8279e5bcd61SYisheng Xie 				if (!isolate_movable_page(page, isolate_mode))
828bda807d4SMinchan Kim 					goto isolate_success;
829bda807d4SMinchan Kim 			}
830bda807d4SMinchan Kim 
831fdd048e1SVlastimil Babka 			goto isolate_fail;
832bda807d4SMinchan Kim 		}
83329c0dde8SVlastimil Babka 
834119d6d59SDavid Rientjes 		/*
835119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
836119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
837119d6d59SDavid Rientjes 		 * admittedly racy check.
838119d6d59SDavid Rientjes 		 */
839119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
840119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
841fdd048e1SVlastimil Babka 			goto isolate_fail;
842119d6d59SDavid Rientjes 
84373e64c51SMichal Hocko 		/*
84473e64c51SMichal Hocko 		 * Only allow to migrate anonymous pages in GFP_NOFS context
84573e64c51SMichal Hocko 		 * because those do not depend on fs locks.
84673e64c51SMichal Hocko 		 */
84773e64c51SMichal Hocko 		if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
84873e64c51SMichal Hocko 			goto isolate_fail;
84973e64c51SMichal Hocko 
85069b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
85169b7189fSVlastimil Babka 		if (!locked) {
852a52633d8SMel Gorman 			locked = compact_trylock_irqsave(zone_lru_lock(zone),
8538b44d279SVlastimil Babka 								&flags, cc);
8548b44d279SVlastimil Babka 			if (!locked)
8552a1402aaSMel Gorman 				break;
8562a1402aaSMel Gorman 
85729c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
8582a1402aaSMel Gorman 			if (!PageLRU(page))
859fdd048e1SVlastimil Babka 				goto isolate_fail;
86029c0dde8SVlastimil Babka 
86129c0dde8SVlastimil Babka 			/*
86229c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
86329c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
86429c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
86529c0dde8SVlastimil Babka 			 */
86629c0dde8SVlastimil Babka 			if (unlikely(PageCompound(page))) {
867*21dc7e02SDavid Rientjes 				const unsigned int order = compound_order(page);
868*21dc7e02SDavid Rientjes 
869*21dc7e02SDavid Rientjes 				if (pageblock_skip_persistent(page, order)) {
870*21dc7e02SDavid Rientjes 					set_pageblock_skip(page);
871*21dc7e02SDavid Rientjes 					low_pfn = end_pfn;
872*21dc7e02SDavid Rientjes 				} else
873*21dc7e02SDavid Rientjes 					low_pfn += (1UL << order) - 1;
874fdd048e1SVlastimil Babka 				goto isolate_fail;
875bc835011SAndrea Arcangeli 			}
87669b7189fSVlastimil Babka 		}
877bc835011SAndrea Arcangeli 
878599d0c95SMel Gorman 		lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
879fa9add64SHugh Dickins 
880748446bbSMel Gorman 		/* Try isolate the page */
881edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
882fdd048e1SVlastimil Babka 			goto isolate_fail;
883748446bbSMel Gorman 
88429c0dde8SVlastimil Babka 		VM_BUG_ON_PAGE(PageCompound(page), page);
885bc835011SAndrea Arcangeli 
886748446bbSMel Gorman 		/* Successfully isolated */
887fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
8886afcf8efSMing Ling 		inc_node_page_state(page,
8896afcf8efSMing Ling 				NR_ISOLATED_ANON + page_is_file_cache(page));
890b6c75016SJoonsoo Kim 
891b6c75016SJoonsoo Kim isolate_success:
892fdd048e1SVlastimil Babka 		list_add(&page->lru, &cc->migratepages);
893748446bbSMel Gorman 		cc->nr_migratepages++;
894b7aba698SMel Gorman 		nr_isolated++;
895748446bbSMel Gorman 
896a34753d2SVlastimil Babka 		/*
897a34753d2SVlastimil Babka 		 * Record where we could have freed pages by migration and not
898a34753d2SVlastimil Babka 		 * yet flushed them to buddy allocator.
899a34753d2SVlastimil Babka 		 * - this is the lowest page that was isolated and likely be
900a34753d2SVlastimil Babka 		 * then freed by migration.
901a34753d2SVlastimil Babka 		 */
902a34753d2SVlastimil Babka 		if (!cc->last_migrated_pfn)
903a34753d2SVlastimil Babka 			cc->last_migrated_pfn = low_pfn;
904a34753d2SVlastimil Babka 
905748446bbSMel Gorman 		/* Avoid isolating too much */
90631b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
90731b8384aSHillf Danton 			++low_pfn;
908748446bbSMel Gorman 			break;
909748446bbSMel Gorman 		}
910fdd048e1SVlastimil Babka 
911fdd048e1SVlastimil Babka 		continue;
912fdd048e1SVlastimil Babka isolate_fail:
913fdd048e1SVlastimil Babka 		if (!skip_on_failure)
914fdd048e1SVlastimil Babka 			continue;
915fdd048e1SVlastimil Babka 
916fdd048e1SVlastimil Babka 		/*
917fdd048e1SVlastimil Babka 		 * We have isolated some pages, but then failed. Release them
918fdd048e1SVlastimil Babka 		 * instead of migrating, as we cannot form the cc->order buddy
919fdd048e1SVlastimil Babka 		 * page anyway.
920fdd048e1SVlastimil Babka 		 */
921fdd048e1SVlastimil Babka 		if (nr_isolated) {
922fdd048e1SVlastimil Babka 			if (locked) {
923a52633d8SMel Gorman 				spin_unlock_irqrestore(zone_lru_lock(zone), flags);
924fdd048e1SVlastimil Babka 				locked = false;
925fdd048e1SVlastimil Babka 			}
926fdd048e1SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
927fdd048e1SVlastimil Babka 			cc->nr_migratepages = 0;
928fdd048e1SVlastimil Babka 			cc->last_migrated_pfn = 0;
929fdd048e1SVlastimil Babka 			nr_isolated = 0;
930fdd048e1SVlastimil Babka 		}
931fdd048e1SVlastimil Babka 
932fdd048e1SVlastimil Babka 		if (low_pfn < next_skip_pfn) {
933fdd048e1SVlastimil Babka 			low_pfn = next_skip_pfn - 1;
934fdd048e1SVlastimil Babka 			/*
935fdd048e1SVlastimil Babka 			 * The check near the loop beginning would have updated
936fdd048e1SVlastimil Babka 			 * next_skip_pfn too, but this is a bit simpler.
937fdd048e1SVlastimil Babka 			 */
938fdd048e1SVlastimil Babka 			next_skip_pfn += 1UL << cc->order;
939fdd048e1SVlastimil Babka 		}
94031b8384aSHillf Danton 	}
941748446bbSMel Gorman 
94299c0fd5eSVlastimil Babka 	/*
94399c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
94499c0fd5eSVlastimil Babka 	 * the range to be scanned.
94599c0fd5eSVlastimil Babka 	 */
94699c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
94799c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
94899c0fd5eSVlastimil Babka 
949c67fe375SMel Gorman 	if (locked)
950a52633d8SMel Gorman 		spin_unlock_irqrestore(zone_lru_lock(zone), flags);
951748446bbSMel Gorman 
95250b5b094SVlastimil Babka 	/*
95350b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
95450b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
95550b5b094SVlastimil Babka 	 */
95635979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
957edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
958bb13ffebSMel Gorman 
959e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
960e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
961b7aba698SMel Gorman 
9627f354a54SDavid Rientjes 	cc->total_migrate_scanned += nr_scanned;
963397487dbSMel Gorman 	if (nr_isolated)
964010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
965397487dbSMel Gorman 
9662fe86e00SMichal Nazarewicz 	return low_pfn;
9672fe86e00SMichal Nazarewicz }
9682fe86e00SMichal Nazarewicz 
969edc2ca61SVlastimil Babka /**
970edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
971edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
972edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
973edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
974edc2ca61SVlastimil Babka  *
975edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
976edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
977edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
978edc2ca61SVlastimil Babka  */
979edc2ca61SVlastimil Babka unsigned long
980edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
981edc2ca61SVlastimil Babka 							unsigned long end_pfn)
982edc2ca61SVlastimil Babka {
983e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
984edc2ca61SVlastimil Babka 
985edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
986edc2ca61SVlastimil Babka 	pfn = start_pfn;
98706b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(pfn);
988e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
989e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
99006b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(pfn);
991edc2ca61SVlastimil Babka 
992edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
993e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
994edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
995edc2ca61SVlastimil Babka 
996edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
997edc2ca61SVlastimil Babka 
998e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
999e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
1000edc2ca61SVlastimil Babka 			continue;
1001edc2ca61SVlastimil Babka 
1002edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1003edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
1004edc2ca61SVlastimil Babka 
100514af4a5eSHugh Dickins 		if (!pfn)
1006edc2ca61SVlastimil Babka 			break;
10076ea41c0cSJoonsoo Kim 
10086ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
10096ea41c0cSJoonsoo Kim 			break;
1010edc2ca61SVlastimil Babka 	}
1011edc2ca61SVlastimil Babka 
1012edc2ca61SVlastimil Babka 	return pfn;
1013edc2ca61SVlastimil Babka }
1014edc2ca61SVlastimil Babka 
1015ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1016ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
1017018e9a49SAndrew Morton 
1018b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc,
1019b682debdSVlastimil Babka 							struct page *page)
1020b682debdSVlastimil Babka {
1021282722b0SVlastimil Babka 	int block_mt;
1022282722b0SVlastimil Babka 
1023282722b0SVlastimil Babka 	if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1024b682debdSVlastimil Babka 		return true;
1025b682debdSVlastimil Babka 
1026282722b0SVlastimil Babka 	block_mt = get_pageblock_migratetype(page);
1027282722b0SVlastimil Babka 
1028282722b0SVlastimil Babka 	if (cc->migratetype == MIGRATE_MOVABLE)
1029282722b0SVlastimil Babka 		return is_migrate_movable(block_mt);
1030282722b0SVlastimil Babka 	else
1031282722b0SVlastimil Babka 		return block_mt == cc->migratetype;
1032b682debdSVlastimil Babka }
1033b682debdSVlastimil Babka 
1034018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
10359f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc,
10369f7e3387SVlastimil Babka 							struct page *page)
1037018e9a49SAndrew Morton {
1038018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
1039018e9a49SAndrew Morton 	if (PageBuddy(page)) {
1040018e9a49SAndrew Morton 		/*
1041018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
1042018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
1043018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
1044018e9a49SAndrew Morton 		 */
1045018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
1046018e9a49SAndrew Morton 			return false;
1047018e9a49SAndrew Morton 	}
1048018e9a49SAndrew Morton 
10491ef36db2SYisheng Xie 	if (cc->ignore_block_suitable)
10501ef36db2SYisheng Xie 		return true;
10511ef36db2SYisheng Xie 
1052018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1053b682debdSVlastimil Babka 	if (is_migrate_movable(get_pageblock_migratetype(page)))
1054018e9a49SAndrew Morton 		return true;
1055018e9a49SAndrew Morton 
1056018e9a49SAndrew Morton 	/* Otherwise skip the block */
1057018e9a49SAndrew Morton 	return false;
1058018e9a49SAndrew Morton }
1059018e9a49SAndrew Morton 
1060ff9543fdSMichal Nazarewicz /*
1061f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
1062f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
1063f2849aa0SVlastimil Babka  */
1064f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
1065f2849aa0SVlastimil Babka {
1066f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
1067f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
1068f2849aa0SVlastimil Babka }
1069f2849aa0SVlastimil Babka 
1070f2849aa0SVlastimil Babka /*
1071ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
1072ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
1073ff9543fdSMichal Nazarewicz  */
1074edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
1075ff9543fdSMichal Nazarewicz {
1076edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
1077ff9543fdSMichal Nazarewicz 	struct page *page;
1078c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
1079e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
1080c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
1081c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
1082ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
10832fe86e00SMichal Nazarewicz 
1084ff9543fdSMichal Nazarewicz 	/*
1085ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
108649e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
1087e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
1088e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
1089c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
1090c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
1091c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
109249e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
109349e068f0SVlastimil Babka 	 * is using.
1094ff9543fdSMichal Nazarewicz 	 */
1095e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
109606b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(cc->free_pfn);
1097c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1098c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
109906b6640aSVlastimil Babka 	low_pfn = pageblock_end_pfn(cc->migrate_pfn);
11002fe86e00SMichal Nazarewicz 
1101ff9543fdSMichal Nazarewicz 	/*
1102ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
1103ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
1104ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
1105ff9543fdSMichal Nazarewicz 	 */
1106f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
1107c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
1108e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
1109e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
1110f6ea3adbSDavid Rientjes 		/*
1111f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
1112f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
1113be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
1114f6ea3adbSDavid Rientjes 		 */
1115be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1116be976572SVlastimil Babka 						&& compact_should_abort(cc))
1117be976572SVlastimil Babka 			break;
1118f6ea3adbSDavid Rientjes 
11197d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
11207d49d886SVlastimil Babka 									zone);
11217d49d886SVlastimil Babka 		if (!page)
1122ff9543fdSMichal Nazarewicz 			continue;
1123ff9543fdSMichal Nazarewicz 
1124ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
11259f7e3387SVlastimil Babka 		if (!suitable_migration_target(cc, page))
1126ff9543fdSMichal Nazarewicz 			continue;
112768e3e926SLinus Torvalds 
1128bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
1129bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
1130bb13ffebSMel Gorman 			continue;
1131bb13ffebSMel Gorman 
1132e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
1133a46cbf3bSDavid Rientjes 		isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1134a46cbf3bSDavid Rientjes 					freelist, false);
1135ff9543fdSMichal Nazarewicz 
1136ff9543fdSMichal Nazarewicz 		/*
1137a46cbf3bSDavid Rientjes 		 * If we isolated enough freepages, or aborted due to lock
1138a46cbf3bSDavid Rientjes 		 * contention, terminate.
1139e14c720eSVlastimil Babka 		 */
1140f5f61a32SVlastimil Babka 		if ((cc->nr_freepages >= cc->nr_migratepages)
1141f5f61a32SVlastimil Babka 							|| cc->contended) {
1142a46cbf3bSDavid Rientjes 			if (isolate_start_pfn >= block_end_pfn) {
1143a46cbf3bSDavid Rientjes 				/*
1144a46cbf3bSDavid Rientjes 				 * Restart at previous pageblock if more
1145a46cbf3bSDavid Rientjes 				 * freepages can be isolated next time.
1146a46cbf3bSDavid Rientjes 				 */
1147f5f61a32SVlastimil Babka 				isolate_start_pfn =
1148e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
1149a46cbf3bSDavid Rientjes 			}
1150be976572SVlastimil Babka 			break;
1151a46cbf3bSDavid Rientjes 		} else if (isolate_start_pfn < block_end_pfn) {
1152f5f61a32SVlastimil Babka 			/*
1153a46cbf3bSDavid Rientjes 			 * If isolation failed early, do not continue
1154a46cbf3bSDavid Rientjes 			 * needlessly.
1155f5f61a32SVlastimil Babka 			 */
1156a46cbf3bSDavid Rientjes 			break;
1157f5f61a32SVlastimil Babka 		}
1158c89511abSMel Gorman 	}
1159ff9543fdSMichal Nazarewicz 
116066c64223SJoonsoo Kim 	/* __isolate_free_page() does not map the pages */
1161ff9543fdSMichal Nazarewicz 	map_pages(freelist);
1162ff9543fdSMichal Nazarewicz 
11637ed695e0SVlastimil Babka 	/*
1164f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
1165f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1166f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1167f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
11687ed695e0SVlastimil Babka 	 */
1169f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
1170748446bbSMel Gorman }
1171748446bbSMel Gorman 
1172748446bbSMel Gorman /*
1173748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1174748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1175748446bbSMel Gorman  */
1176748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1177748446bbSMel Gorman 					unsigned long data,
1178748446bbSMel Gorman 					int **result)
1179748446bbSMel Gorman {
1180748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1181748446bbSMel Gorman 	struct page *freepage;
1182748446bbSMel Gorman 
1183be976572SVlastimil Babka 	/*
1184be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
1185be976572SVlastimil Babka 	 * contention.
1186be976572SVlastimil Babka 	 */
1187748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1188be976572SVlastimil Babka 		if (!cc->contended)
1189edc2ca61SVlastimil Babka 			isolate_freepages(cc);
1190748446bbSMel Gorman 
1191748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1192748446bbSMel Gorman 			return NULL;
1193748446bbSMel Gorman 	}
1194748446bbSMel Gorman 
1195748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1196748446bbSMel Gorman 	list_del(&freepage->lru);
1197748446bbSMel Gorman 	cc->nr_freepages--;
1198748446bbSMel Gorman 
1199748446bbSMel Gorman 	return freepage;
1200748446bbSMel Gorman }
1201748446bbSMel Gorman 
1202748446bbSMel Gorman /*
1203d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1204d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1205d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1206d53aea3dSDavid Rientjes  */
1207d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1208d53aea3dSDavid Rientjes {
1209d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1210d53aea3dSDavid Rientjes 
1211d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1212d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1213d53aea3dSDavid Rientjes }
1214d53aea3dSDavid Rientjes 
1215ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1216ff9543fdSMichal Nazarewicz typedef enum {
1217ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1218ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1219ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1220ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1221ff9543fdSMichal Nazarewicz 
1222ff9543fdSMichal Nazarewicz /*
12235bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
12245bbe3547SEric B Munson  * compactable pages.
12255bbe3547SEric B Munson  */
12265bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
12275bbe3547SEric B Munson 
12285bbe3547SEric B Munson /*
1229edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1230edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1231edc2ca61SVlastimil Babka  * compact_control.
1232ff9543fdSMichal Nazarewicz  */
1233ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1234ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1235ff9543fdSMichal Nazarewicz {
1236e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1237e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1238e1409c32SJoonsoo Kim 	unsigned long low_pfn;
1239edc2ca61SVlastimil Babka 	struct page *page;
1240edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
12415bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
12421d2047feSHugh Dickins 		(cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1243ff9543fdSMichal Nazarewicz 
1244edc2ca61SVlastimil Babka 	/*
1245edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
1246edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
1247edc2ca61SVlastimil Babka 	 */
1248edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
124906b6640aSVlastimil Babka 	block_start_pfn = pageblock_start_pfn(low_pfn);
1250e1409c32SJoonsoo Kim 	if (block_start_pfn < zone->zone_start_pfn)
1251e1409c32SJoonsoo Kim 		block_start_pfn = zone->zone_start_pfn;
1252ff9543fdSMichal Nazarewicz 
1253ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
125406b6640aSVlastimil Babka 	block_end_pfn = pageblock_end_pfn(low_pfn);
1255ff9543fdSMichal Nazarewicz 
1256edc2ca61SVlastimil Babka 	/*
1257edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1258edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1259edc2ca61SVlastimil Babka 	 */
1260e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
1261e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1262e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1263e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1264edc2ca61SVlastimil Babka 
1265edc2ca61SVlastimil Babka 		/*
1266edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1267edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1268edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
1269edc2ca61SVlastimil Babka 		 */
1270edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1271edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
1272edc2ca61SVlastimil Babka 			break;
1273edc2ca61SVlastimil Babka 
1274e1409c32SJoonsoo Kim 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1275e1409c32SJoonsoo Kim 									zone);
12767d49d886SVlastimil Babka 		if (!page)
1277edc2ca61SVlastimil Babka 			continue;
1278edc2ca61SVlastimil Babka 
1279edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1280edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1281edc2ca61SVlastimil Babka 			continue;
1282edc2ca61SVlastimil Babka 
1283edc2ca61SVlastimil Babka 		/*
1284edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1285edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1286edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1287edc2ca61SVlastimil Babka 		 */
1288b682debdSVlastimil Babka 		if (!suitable_migration_source(cc, page))
1289edc2ca61SVlastimil Babka 			continue;
1290ff9543fdSMichal Nazarewicz 
1291ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1292e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1293e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1294edc2ca61SVlastimil Babka 
12956afcf8efSMing Ling 		if (!low_pfn || cc->contended)
1296ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1297ff9543fdSMichal Nazarewicz 
1298edc2ca61SVlastimil Babka 		/*
1299edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1300edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1301edc2ca61SVlastimil Babka 		 * continue or not.
1302edc2ca61SVlastimil Babka 		 */
1303edc2ca61SVlastimil Babka 		break;
1304edc2ca61SVlastimil Babka 	}
1305edc2ca61SVlastimil Babka 
1306f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1307f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1308ff9543fdSMichal Nazarewicz 
1309edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1310ff9543fdSMichal Nazarewicz }
1311ff9543fdSMichal Nazarewicz 
131221c527a3SYaowei Bai /*
131321c527a3SYaowei Bai  * order == -1 is expected when compacting via
131421c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
131521c527a3SYaowei Bai  */
131621c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
131721c527a3SYaowei Bai {
131821c527a3SYaowei Bai 	return order == -1;
131921c527a3SYaowei Bai }
132021c527a3SYaowei Bai 
1321d39773a0SVlastimil Babka static enum compact_result __compact_finished(struct zone *zone,
1322d39773a0SVlastimil Babka 						struct compact_control *cc)
1323748446bbSMel Gorman {
13248fb74b9fSMel Gorman 	unsigned int order;
1325d39773a0SVlastimil Babka 	const int migratetype = cc->migratetype;
132656de7263SMel Gorman 
1327be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
13282d1e1041SVlastimil Babka 		return COMPACT_CONTENDED;
1329748446bbSMel Gorman 
1330753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1331f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
133255b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
133302333641SVlastimil Babka 		reset_cached_positions(zone);
133455b7c4c9SVlastimil Babka 
133562997027SMel Gorman 		/*
133662997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1337accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
133862997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
133962997027SMel Gorman 		 * based on an allocation request.
134062997027SMel Gorman 		 */
1341accf6242SVlastimil Babka 		if (cc->direct_compaction)
134262997027SMel Gorman 			zone->compact_blockskip_flush = true;
134362997027SMel Gorman 
1344c8f7de0bSMichal Hocko 		if (cc->whole_zone)
1345748446bbSMel Gorman 			return COMPACT_COMPLETE;
1346c8f7de0bSMichal Hocko 		else
1347c8f7de0bSMichal Hocko 			return COMPACT_PARTIAL_SKIPPED;
1348bb13ffebSMel Gorman 	}
1349748446bbSMel Gorman 
135021c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
135156de7263SMel Gorman 		return COMPACT_CONTINUE;
135256de7263SMel Gorman 
1353baf6a9a1SVlastimil Babka 	if (cc->finishing_block) {
1354baf6a9a1SVlastimil Babka 		/*
1355baf6a9a1SVlastimil Babka 		 * We have finished the pageblock, but better check again that
1356baf6a9a1SVlastimil Babka 		 * we really succeeded.
1357baf6a9a1SVlastimil Babka 		 */
1358baf6a9a1SVlastimil Babka 		if (IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
1359baf6a9a1SVlastimil Babka 			cc->finishing_block = false;
1360baf6a9a1SVlastimil Babka 		else
1361baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1362baf6a9a1SVlastimil Babka 	}
1363baf6a9a1SVlastimil Babka 
136456de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
136556de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
13668fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
13672149cdaeSJoonsoo Kim 		bool can_steal;
13688fb74b9fSMel Gorman 
136956de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
13706d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
1371cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
137256de7263SMel Gorman 
13732149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
13742149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
13752149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
13762149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
1377cf378319SVlastimil Babka 			return COMPACT_SUCCESS;
13782149cdaeSJoonsoo Kim #endif
13792149cdaeSJoonsoo Kim 		/*
13802149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
13812149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
13822149cdaeSJoonsoo Kim 		 */
13832149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
1384baf6a9a1SVlastimil Babka 						true, &can_steal) != -1) {
1385baf6a9a1SVlastimil Babka 
1386baf6a9a1SVlastimil Babka 			/* movable pages are OK in any pageblock */
1387baf6a9a1SVlastimil Babka 			if (migratetype == MIGRATE_MOVABLE)
1388cf378319SVlastimil Babka 				return COMPACT_SUCCESS;
1389baf6a9a1SVlastimil Babka 
1390baf6a9a1SVlastimil Babka 			/*
1391baf6a9a1SVlastimil Babka 			 * We are stealing for a non-movable allocation. Make
1392baf6a9a1SVlastimil Babka 			 * sure we finish compacting the current pageblock
1393baf6a9a1SVlastimil Babka 			 * first so it is as free as possible and we won't
1394baf6a9a1SVlastimil Babka 			 * have to steal another one soon. This only applies
1395baf6a9a1SVlastimil Babka 			 * to sync compaction, as async compaction operates
1396baf6a9a1SVlastimil Babka 			 * on pageblocks of the same migratetype.
1397baf6a9a1SVlastimil Babka 			 */
1398baf6a9a1SVlastimil Babka 			if (cc->mode == MIGRATE_ASYNC ||
1399baf6a9a1SVlastimil Babka 					IS_ALIGNED(cc->migrate_pfn,
1400baf6a9a1SVlastimil Babka 							pageblock_nr_pages)) {
1401baf6a9a1SVlastimil Babka 				return COMPACT_SUCCESS;
1402baf6a9a1SVlastimil Babka 			}
1403baf6a9a1SVlastimil Babka 
1404baf6a9a1SVlastimil Babka 			cc->finishing_block = true;
1405baf6a9a1SVlastimil Babka 			return COMPACT_CONTINUE;
1406baf6a9a1SVlastimil Babka 		}
140756de7263SMel Gorman 	}
140856de7263SMel Gorman 
1409837d026dSJoonsoo Kim 	return COMPACT_NO_SUITABLE_PAGE;
1410837d026dSJoonsoo Kim }
1411837d026dSJoonsoo Kim 
1412ea7ab982SMichal Hocko static enum compact_result compact_finished(struct zone *zone,
1413d39773a0SVlastimil Babka 			struct compact_control *cc)
1414837d026dSJoonsoo Kim {
1415837d026dSJoonsoo Kim 	int ret;
1416837d026dSJoonsoo Kim 
1417d39773a0SVlastimil Babka 	ret = __compact_finished(zone, cc);
1418837d026dSJoonsoo Kim 	trace_mm_compaction_finished(zone, cc->order, ret);
1419837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1420837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1421837d026dSJoonsoo Kim 
1422837d026dSJoonsoo Kim 	return ret;
1423748446bbSMel Gorman }
1424748446bbSMel Gorman 
14253e7d3449SMel Gorman /*
14263e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
14273e7d3449SMel Gorman  * Returns
14283e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
1429cf378319SVlastimil Babka  *   COMPACT_SUCCESS  - If the allocation would succeed without compaction
14303e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
14313e7d3449SMel Gorman  */
1432ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order,
1433c603844bSMel Gorman 					unsigned int alloc_flags,
143486a294a8SMichal Hocko 					int classzone_idx,
143586a294a8SMichal Hocko 					unsigned long wmark_target)
14363e7d3449SMel Gorman {
14373e7d3449SMel Gorman 	unsigned long watermark;
14383e7d3449SMel Gorman 
143921c527a3SYaowei Bai 	if (is_via_compact_memory(order))
14403957c776SMichal Hocko 		return COMPACT_CONTINUE;
14413957c776SMichal Hocko 
1442f2b8228cSVlastimil Babka 	watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
1443ebff3980SVlastimil Babka 	/*
1444ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1445ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1446ebff3980SVlastimil Babka 	 */
1447ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1448ebff3980SVlastimil Babka 								alloc_flags))
1449cf378319SVlastimil Babka 		return COMPACT_SUCCESS;
1450ebff3980SVlastimil Babka 
14513957c776SMichal Hocko 	/*
14529861a62cSVlastimil Babka 	 * Watermarks for order-0 must be met for compaction to be able to
1453984fdba6SVlastimil Babka 	 * isolate free pages for migration targets. This means that the
1454984fdba6SVlastimil Babka 	 * watermark and alloc_flags have to match, or be more pessimistic than
1455984fdba6SVlastimil Babka 	 * the check in __isolate_free_page(). We don't use the direct
1456984fdba6SVlastimil Babka 	 * compactor's alloc_flags, as they are not relevant for freepage
1457984fdba6SVlastimil Babka 	 * isolation. We however do use the direct compactor's classzone_idx to
1458984fdba6SVlastimil Babka 	 * skip over zones where lowmem reserves would prevent allocation even
1459984fdba6SVlastimil Babka 	 * if compaction succeeds.
14608348faf9SVlastimil Babka 	 * For costly orders, we require low watermark instead of min for
14618348faf9SVlastimil Babka 	 * compaction to proceed to increase its chances.
1462984fdba6SVlastimil Babka 	 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
1463984fdba6SVlastimil Babka 	 * suitable migration targets
14643e7d3449SMel Gorman 	 */
14658348faf9SVlastimil Babka 	watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
14668348faf9SVlastimil Babka 				low_wmark_pages(zone) : min_wmark_pages(zone);
14678348faf9SVlastimil Babka 	watermark += compact_gap(order);
146886a294a8SMichal Hocko 	if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
1469984fdba6SVlastimil Babka 						ALLOC_CMA, wmark_target))
14703e7d3449SMel Gorman 		return COMPACT_SKIPPED;
14713e7d3449SMel Gorman 
1472cc5c9f09SVlastimil Babka 	return COMPACT_CONTINUE;
1473cc5c9f09SVlastimil Babka }
1474cc5c9f09SVlastimil Babka 
1475cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order,
1476cc5c9f09SVlastimil Babka 					unsigned int alloc_flags,
1477cc5c9f09SVlastimil Babka 					int classzone_idx)
1478cc5c9f09SVlastimil Babka {
1479cc5c9f09SVlastimil Babka 	enum compact_result ret;
1480cc5c9f09SVlastimil Babka 	int fragindex;
1481cc5c9f09SVlastimil Babka 
1482cc5c9f09SVlastimil Babka 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
1483cc5c9f09SVlastimil Babka 				    zone_page_state(zone, NR_FREE_PAGES));
14843e7d3449SMel Gorman 	/*
14853e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
14863e7d3449SMel Gorman 	 * low memory or external fragmentation
14873e7d3449SMel Gorman 	 *
1488ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
1489ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
14903e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
14913e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
14923e7d3449SMel Gorman 	 *
149320311420SVlastimil Babka 	 * Only compact if a failure would be due to fragmentation. Also
149420311420SVlastimil Babka 	 * ignore fragindex for non-costly orders where the alternative to
149520311420SVlastimil Babka 	 * a successful reclaim/compaction is OOM. Fragindex and the
149620311420SVlastimil Babka 	 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
149720311420SVlastimil Babka 	 * excessive compaction for costly orders, but it should not be at the
149820311420SVlastimil Babka 	 * expense of system stability.
14993e7d3449SMel Gorman 	 */
150020311420SVlastimil Babka 	if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
15013e7d3449SMel Gorman 		fragindex = fragmentation_index(zone, order);
15023e7d3449SMel Gorman 		if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1503cc5c9f09SVlastimil Babka 			ret = COMPACT_NOT_SUITABLE_ZONE;
15043e7d3449SMel Gorman 	}
15053e7d3449SMel Gorman 
1506837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
1507837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
1508837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
1509837d026dSJoonsoo Kim 
1510837d026dSJoonsoo Kim 	return ret;
1511837d026dSJoonsoo Kim }
1512837d026dSJoonsoo Kim 
151386a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
151486a294a8SMichal Hocko 		int alloc_flags)
151586a294a8SMichal Hocko {
151686a294a8SMichal Hocko 	struct zone *zone;
151786a294a8SMichal Hocko 	struct zoneref *z;
151886a294a8SMichal Hocko 
151986a294a8SMichal Hocko 	/*
152086a294a8SMichal Hocko 	 * Make sure at least one zone would pass __compaction_suitable if we continue
152186a294a8SMichal Hocko 	 * retrying the reclaim.
152286a294a8SMichal Hocko 	 */
152386a294a8SMichal Hocko 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
152486a294a8SMichal Hocko 					ac->nodemask) {
152586a294a8SMichal Hocko 		unsigned long available;
152686a294a8SMichal Hocko 		enum compact_result compact_result;
152786a294a8SMichal Hocko 
152886a294a8SMichal Hocko 		/*
152986a294a8SMichal Hocko 		 * Do not consider all the reclaimable memory because we do not
153086a294a8SMichal Hocko 		 * want to trash just for a single high order allocation which
153186a294a8SMichal Hocko 		 * is even not guaranteed to appear even if __compaction_suitable
153286a294a8SMichal Hocko 		 * is happy about the watermark check.
153386a294a8SMichal Hocko 		 */
15345a1c84b4SMel Gorman 		available = zone_reclaimable_pages(zone) / order;
153586a294a8SMichal Hocko 		available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
153686a294a8SMichal Hocko 		compact_result = __compaction_suitable(zone, order, alloc_flags,
153786a294a8SMichal Hocko 				ac_classzone_idx(ac), available);
1538cc5c9f09SVlastimil Babka 		if (compact_result != COMPACT_SKIPPED)
153986a294a8SMichal Hocko 			return true;
154086a294a8SMichal Hocko 	}
154186a294a8SMichal Hocko 
154286a294a8SMichal Hocko 	return false;
154386a294a8SMichal Hocko }
154486a294a8SMichal Hocko 
1545ea7ab982SMichal Hocko static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
1546748446bbSMel Gorman {
1547ea7ab982SMichal Hocko 	enum compact_result ret;
1548c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1549108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
1550e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1551748446bbSMel Gorman 
1552d39773a0SVlastimil Babka 	cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1553ebff3980SVlastimil Babka 	ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1554ebff3980SVlastimil Babka 							cc->classzone_idx);
15553e7d3449SMel Gorman 	/* Compaction is likely to fail */
1556cf378319SVlastimil Babka 	if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
15573e7d3449SMel Gorman 		return ret;
1558c46649deSMichal Hocko 
1559c46649deSMichal Hocko 	/* huh, compaction_suitable is returning something unexpected */
1560c46649deSMichal Hocko 	VM_BUG_ON(ret != COMPACT_CONTINUE);
15613e7d3449SMel Gorman 
1562c89511abSMel Gorman 	/*
1563d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1564accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
1565d3132e4bSVlastimil Babka 	 */
1566accf6242SVlastimil Babka 	if (compaction_restarting(zone, cc->order))
1567d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1568d3132e4bSVlastimil Babka 
1569d3132e4bSVlastimil Babka 	/*
1570c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
157106ed2998SVlastimil Babka 	 * information on where the scanners should start (unless we explicitly
157206ed2998SVlastimil Babka 	 * want to compact the whole zone), but check that it is initialised
157306ed2998SVlastimil Babka 	 * by ensuring the values are within zone boundaries.
1574c89511abSMel Gorman 	 */
157506ed2998SVlastimil Babka 	if (cc->whole_zone) {
157606ed2998SVlastimil Babka 		cc->migrate_pfn = start_pfn;
157706ed2998SVlastimil Babka 		cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
157806ed2998SVlastimil Babka 	} else {
1579e0b9daebSDavid Rientjes 		cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1580c89511abSMel Gorman 		cc->free_pfn = zone->compact_cached_free_pfn;
1581623446e4SJoonsoo Kim 		if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
158206b6640aSVlastimil Babka 			cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
1583c89511abSMel Gorman 			zone->compact_cached_free_pfn = cc->free_pfn;
1584c89511abSMel Gorman 		}
1585623446e4SJoonsoo Kim 		if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
1586c89511abSMel Gorman 			cc->migrate_pfn = start_pfn;
158735979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
158835979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1589c89511abSMel Gorman 		}
1590c8f7de0bSMichal Hocko 
1591c8f7de0bSMichal Hocko 		if (cc->migrate_pfn == start_pfn)
1592c8f7de0bSMichal Hocko 			cc->whole_zone = true;
159306ed2998SVlastimil Babka 	}
1594c8f7de0bSMichal Hocko 
15951a16718cSJoonsoo Kim 	cc->last_migrated_pfn = 0;
1596748446bbSMel Gorman 
159716c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
159816c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
15990eb927c0SMel Gorman 
1600748446bbSMel Gorman 	migrate_prep_local();
1601748446bbSMel Gorman 
1602d39773a0SVlastimil Babka 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
16039d502c1cSMinchan Kim 		int err;
1604748446bbSMel Gorman 
1605f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1606f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
16072d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
16085733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1609e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1610f9e35b3bSMel Gorman 			goto out;
1611f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1612fdaf7f5cSVlastimil Babka 			/*
1613fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
1614fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
1615fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
1616fdaf7f5cSVlastimil Babka 			 */
1617fdaf7f5cSVlastimil Babka 			goto check_drain;
1618f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1619f9e35b3bSMel Gorman 			;
1620f9e35b3bSMel Gorman 		}
1621748446bbSMel Gorman 
1622d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1623e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
16247b2a2d4aSMel Gorman 				MR_COMPACTION);
1625748446bbSMel Gorman 
1626f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1627f8c9301fSVlastimil Babka 							&cc->migratepages);
1628748446bbSMel Gorman 
1629f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1630f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
16319d502c1cSMinchan Kim 		if (err) {
16325733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
16337ed695e0SVlastimil Babka 			/*
16347ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
16357ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
16367ed695e0SVlastimil Babka 			 */
1637f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
16382d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
16394bf2bba3SDavid Rientjes 				goto out;
1640748446bbSMel Gorman 			}
1641fdd048e1SVlastimil Babka 			/*
1642fdd048e1SVlastimil Babka 			 * We failed to migrate at least one page in the current
1643fdd048e1SVlastimil Babka 			 * order-aligned block, so skip the rest of it.
1644fdd048e1SVlastimil Babka 			 */
1645fdd048e1SVlastimil Babka 			if (cc->direct_compaction &&
1646fdd048e1SVlastimil Babka 						(cc->mode == MIGRATE_ASYNC)) {
1647fdd048e1SVlastimil Babka 				cc->migrate_pfn = block_end_pfn(
1648fdd048e1SVlastimil Babka 						cc->migrate_pfn - 1, cc->order);
1649fdd048e1SVlastimil Babka 				/* Draining pcplists is useless in this case */
1650fdd048e1SVlastimil Babka 				cc->last_migrated_pfn = 0;
1651fdd048e1SVlastimil Babka 
1652fdd048e1SVlastimil Babka 			}
16534bf2bba3SDavid Rientjes 		}
1654fdaf7f5cSVlastimil Babka 
1655fdaf7f5cSVlastimil Babka check_drain:
1656fdaf7f5cSVlastimil Babka 		/*
1657fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
1658fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
1659fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
1660fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
1661fdaf7f5cSVlastimil Babka 		 * would succeed.
1662fdaf7f5cSVlastimil Babka 		 */
16631a16718cSJoonsoo Kim 		if (cc->order > 0 && cc->last_migrated_pfn) {
1664fdaf7f5cSVlastimil Babka 			int cpu;
1665fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
166606b6640aSVlastimil Babka 				block_start_pfn(cc->migrate_pfn, cc->order);
1667fdaf7f5cSVlastimil Babka 
16681a16718cSJoonsoo Kim 			if (cc->last_migrated_pfn < current_block_start) {
1669fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
1670fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
1671fdaf7f5cSVlastimil Babka 				drain_local_pages(zone);
1672fdaf7f5cSVlastimil Babka 				put_cpu();
1673fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
16741a16718cSJoonsoo Kim 				cc->last_migrated_pfn = 0;
1675fdaf7f5cSVlastimil Babka 			}
1676fdaf7f5cSVlastimil Babka 		}
1677fdaf7f5cSVlastimil Babka 
1678748446bbSMel Gorman 	}
1679748446bbSMel Gorman 
1680f9e35b3bSMel Gorman out:
16816bace090SVlastimil Babka 	/*
16826bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
16836bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
16846bace090SVlastimil Babka 	 */
16856bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
16866bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
16876bace090SVlastimil Babka 
16886bace090SVlastimil Babka 		cc->nr_freepages = 0;
16896bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
16906bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
169106b6640aSVlastimil Babka 		free_pfn = pageblock_start_pfn(free_pfn);
16926bace090SVlastimil Babka 		/*
16936bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
16946bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
16956bace090SVlastimil Babka 		 */
16966bace090SVlastimil Babka 		if (free_pfn > zone->compact_cached_free_pfn)
16976bace090SVlastimil Babka 			zone->compact_cached_free_pfn = free_pfn;
16986bace090SVlastimil Babka 	}
1699748446bbSMel Gorman 
17007f354a54SDavid Rientjes 	count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
17017f354a54SDavid Rientjes 	count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
17027f354a54SDavid Rientjes 
170316c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
170416c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
17050eb927c0SMel Gorman 
1706748446bbSMel Gorman 	return ret;
1707748446bbSMel Gorman }
170876ab0f53SMel Gorman 
1709ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order,
1710c3486f53SVlastimil Babka 		gfp_t gfp_mask, enum compact_priority prio,
1711c603844bSMel Gorman 		unsigned int alloc_flags, int classzone_idx)
171256de7263SMel Gorman {
1713ea7ab982SMichal Hocko 	enum compact_result ret;
171456de7263SMel Gorman 	struct compact_control cc = {
171556de7263SMel Gorman 		.nr_freepages = 0,
171656de7263SMel Gorman 		.nr_migratepages = 0,
17177f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
17187f354a54SDavid Rientjes 		.total_free_scanned = 0,
171956de7263SMel Gorman 		.order = order,
17206d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
172156de7263SMel Gorman 		.zone = zone,
1722a5508cd8SVlastimil Babka 		.mode = (prio == COMPACT_PRIO_ASYNC) ?
1723a5508cd8SVlastimil Babka 					MIGRATE_ASYNC :	MIGRATE_SYNC_LIGHT,
1724ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
1725ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
1726accf6242SVlastimil Babka 		.direct_compaction = true,
1727a8e025e5SVlastimil Babka 		.whole_zone = (prio == MIN_COMPACT_PRIORITY),
17289f7e3387SVlastimil Babka 		.ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
17299f7e3387SVlastimil Babka 		.ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
173056de7263SMel Gorman 	};
173156de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
173256de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
173356de7263SMel Gorman 
1734e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1735e64c5237SShaohua Li 
1736e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1737e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1738e64c5237SShaohua Li 
1739e64c5237SShaohua Li 	return ret;
174056de7263SMel Gorman }
174156de7263SMel Gorman 
17425e771905SMel Gorman int sysctl_extfrag_threshold = 500;
17435e771905SMel Gorman 
174456de7263SMel Gorman /**
174556de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
174656de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
17471a6d53a1SVlastimil Babka  * @order: The order of the current allocation
17481a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
17491a6d53a1SVlastimil Babka  * @ac: The context of current allocation
1750e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
175156de7263SMel Gorman  *
175256de7263SMel Gorman  * This is the main entry point for direct page compaction.
175356de7263SMel Gorman  */
1754ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
1755c603844bSMel Gorman 		unsigned int alloc_flags, const struct alloc_context *ac,
1756c3486f53SVlastimil Babka 		enum compact_priority prio)
175756de7263SMel Gorman {
175856de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
175956de7263SMel Gorman 	struct zoneref *z;
176056de7263SMel Gorman 	struct zone *zone;
17611d4746d3SMichal Hocko 	enum compact_result rc = COMPACT_SKIPPED;
176256de7263SMel Gorman 
176373e64c51SMichal Hocko 	/*
176473e64c51SMichal Hocko 	 * Check if the GFP flags allow compaction - GFP_NOIO is really
176573e64c51SMichal Hocko 	 * tricky context because the migration might require IO
176673e64c51SMichal Hocko 	 */
176773e64c51SMichal Hocko 	if (!may_perform_io)
176853853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
176956de7263SMel Gorman 
1770a5508cd8SVlastimil Babka 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
1771837d026dSJoonsoo Kim 
177256de7263SMel Gorman 	/* Compact each zone in the list */
17731a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
17741a6d53a1SVlastimil Babka 								ac->nodemask) {
1775ea7ab982SMichal Hocko 		enum compact_result status;
177656de7263SMel Gorman 
1777a8e025e5SVlastimil Babka 		if (prio > MIN_COMPACT_PRIORITY
1778a8e025e5SVlastimil Babka 					&& compaction_deferred(zone, order)) {
17791d4746d3SMichal Hocko 			rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
178053853e2dSVlastimil Babka 			continue;
17811d4746d3SMichal Hocko 		}
178253853e2dSVlastimil Babka 
1783a5508cd8SVlastimil Babka 		status = compact_zone_order(zone, order, gfp_mask, prio,
1784c3486f53SVlastimil Babka 					alloc_flags, ac_classzone_idx(ac));
178556de7263SMel Gorman 		rc = max(status, rc);
178656de7263SMel Gorman 
17877ceb009aSVlastimil Babka 		/* The allocation should succeed, stop compacting */
17887ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
178953853e2dSVlastimil Babka 			/*
179053853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
179153853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
179253853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
179353853e2dSVlastimil Babka 			 * succeeds in this zone.
179453853e2dSVlastimil Babka 			 */
179553853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
17961f9efdefSVlastimil Babka 
1797c3486f53SVlastimil Babka 			break;
17981f9efdefSVlastimil Babka 		}
17991f9efdefSVlastimil Babka 
1800a5508cd8SVlastimil Babka 		if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
1801c3486f53SVlastimil Babka 					status == COMPACT_PARTIAL_SKIPPED))
180253853e2dSVlastimil Babka 			/*
180353853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
180453853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
180553853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
180653853e2dSVlastimil Babka 			 */
180753853e2dSVlastimil Babka 			defer_compaction(zone, order);
18081f9efdefSVlastimil Babka 
18091f9efdefSVlastimil Babka 		/*
18101f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
18111f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
1812c3486f53SVlastimil Babka 		 * case do not try further zones
18131f9efdefSVlastimil Babka 		 */
1814c3486f53SVlastimil Babka 		if ((prio == COMPACT_PRIO_ASYNC && need_resched())
1815c3486f53SVlastimil Babka 					|| fatal_signal_pending(current))
18161f9efdefSVlastimil Babka 			break;
18171f9efdefSVlastimil Babka 	}
18181f9efdefSVlastimil Babka 
181956de7263SMel Gorman 	return rc;
182056de7263SMel Gorman }
182156de7263SMel Gorman 
182256de7263SMel Gorman 
182376ab0f53SMel Gorman /* Compact all zones within a node */
18247103f16dSAndrew Morton static void compact_node(int nid)
18257be62de9SRik van Riel {
1826791cae96SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
1827791cae96SVlastimil Babka 	int zoneid;
1828791cae96SVlastimil Babka 	struct zone *zone;
18297be62de9SRik van Riel 	struct compact_control cc = {
18307be62de9SRik van Riel 		.order = -1,
18317f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
18327f354a54SDavid Rientjes 		.total_free_scanned = 0,
1833e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
183491ca9186SDavid Rientjes 		.ignore_skip_hint = true,
183506ed2998SVlastimil Babka 		.whole_zone = true,
183673e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
18377be62de9SRik van Riel 	};
18387be62de9SRik van Riel 
1839791cae96SVlastimil Babka 
1840791cae96SVlastimil Babka 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1841791cae96SVlastimil Babka 
1842791cae96SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1843791cae96SVlastimil Babka 		if (!populated_zone(zone))
1844791cae96SVlastimil Babka 			continue;
1845791cae96SVlastimil Babka 
1846791cae96SVlastimil Babka 		cc.nr_freepages = 0;
1847791cae96SVlastimil Babka 		cc.nr_migratepages = 0;
1848791cae96SVlastimil Babka 		cc.zone = zone;
1849791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1850791cae96SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1851791cae96SVlastimil Babka 
1852791cae96SVlastimil Babka 		compact_zone(zone, &cc);
1853791cae96SVlastimil Babka 
1854791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
1855791cae96SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
1856791cae96SVlastimil Babka 	}
18577be62de9SRik van Riel }
18587be62de9SRik van Riel 
185976ab0f53SMel Gorman /* Compact all nodes in the system */
18607964c06dSJason Liu static void compact_nodes(void)
186176ab0f53SMel Gorman {
186276ab0f53SMel Gorman 	int nid;
186376ab0f53SMel Gorman 
18648575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
18658575ec29SHugh Dickins 	lru_add_drain_all();
18668575ec29SHugh Dickins 
186776ab0f53SMel Gorman 	for_each_online_node(nid)
186876ab0f53SMel Gorman 		compact_node(nid);
186976ab0f53SMel Gorman }
187076ab0f53SMel Gorman 
187176ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
187276ab0f53SMel Gorman int sysctl_compact_memory;
187376ab0f53SMel Gorman 
1874fec4eb2cSYaowei Bai /*
1875fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
1876fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
1877fec4eb2cSYaowei Bai  */
187876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
187976ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
188076ab0f53SMel Gorman {
188176ab0f53SMel Gorman 	if (write)
18827964c06dSJason Liu 		compact_nodes();
188376ab0f53SMel Gorman 
188476ab0f53SMel Gorman 	return 0;
188576ab0f53SMel Gorman }
1886ed4a6d7fSMel Gorman 
18875e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
18885e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
18895e771905SMel Gorman {
18905e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
18915e771905SMel Gorman 
18925e771905SMel Gorman 	return 0;
18935e771905SMel Gorman }
18945e771905SMel Gorman 
1895ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
189674e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
189710fbcf4cSKay Sievers 			struct device_attribute *attr,
1898ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1899ed4a6d7fSMel Gorman {
19008575ec29SHugh Dickins 	int nid = dev->id;
19018575ec29SHugh Dickins 
19028575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
19038575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
19048575ec29SHugh Dickins 		lru_add_drain_all();
19058575ec29SHugh Dickins 
19068575ec29SHugh Dickins 		compact_node(nid);
19078575ec29SHugh Dickins 	}
1908ed4a6d7fSMel Gorman 
1909ed4a6d7fSMel Gorman 	return count;
1910ed4a6d7fSMel Gorman }
191110fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1912ed4a6d7fSMel Gorman 
1913ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1914ed4a6d7fSMel Gorman {
191510fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1916ed4a6d7fSMel Gorman }
1917ed4a6d7fSMel Gorman 
1918ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1919ed4a6d7fSMel Gorman {
192010fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1921ed4a6d7fSMel Gorman }
1922ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1923ff9543fdSMichal Nazarewicz 
1924698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1925698b1b30SVlastimil Babka {
1926172400c6SVlastimil Babka 	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
1927698b1b30SVlastimil Babka }
1928698b1b30SVlastimil Babka 
1929698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
1930698b1b30SVlastimil Babka {
1931698b1b30SVlastimil Babka 	int zoneid;
1932698b1b30SVlastimil Babka 	struct zone *zone;
1933698b1b30SVlastimil Babka 	enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1934698b1b30SVlastimil Babka 
19356cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
1936698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1937698b1b30SVlastimil Babka 
1938698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1939698b1b30SVlastimil Babka 			continue;
1940698b1b30SVlastimil Babka 
1941698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1942698b1b30SVlastimil Babka 					classzone_idx) == COMPACT_CONTINUE)
1943698b1b30SVlastimil Babka 			return true;
1944698b1b30SVlastimil Babka 	}
1945698b1b30SVlastimil Babka 
1946698b1b30SVlastimil Babka 	return false;
1947698b1b30SVlastimil Babka }
1948698b1b30SVlastimil Babka 
1949698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
1950698b1b30SVlastimil Babka {
1951698b1b30SVlastimil Babka 	/*
1952698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
1953698b1b30SVlastimil Babka 	 * order is allocatable.
1954698b1b30SVlastimil Babka 	 */
1955698b1b30SVlastimil Babka 	int zoneid;
1956698b1b30SVlastimil Babka 	struct zone *zone;
1957698b1b30SVlastimil Babka 	struct compact_control cc = {
1958698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
19597f354a54SDavid Rientjes 		.total_migrate_scanned = 0,
19607f354a54SDavid Rientjes 		.total_free_scanned = 0,
1961698b1b30SVlastimil Babka 		.classzone_idx = pgdat->kcompactd_classzone_idx,
1962698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
1963a0647dc9SDavid Rientjes 		.ignore_skip_hint = false,
196473e64c51SMichal Hocko 		.gfp_mask = GFP_KERNEL,
1965698b1b30SVlastimil Babka 	};
1966698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1967698b1b30SVlastimil Babka 							cc.classzone_idx);
19687f354a54SDavid Rientjes 	count_compact_event(KCOMPACTD_WAKE);
1969698b1b30SVlastimil Babka 
19706cd9dc3eSChen Feng 	for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
1971698b1b30SVlastimil Babka 		int status;
1972698b1b30SVlastimil Babka 
1973698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1974698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1975698b1b30SVlastimil Babka 			continue;
1976698b1b30SVlastimil Babka 
1977698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
1978698b1b30SVlastimil Babka 			continue;
1979698b1b30SVlastimil Babka 
1980698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1981698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
1982698b1b30SVlastimil Babka 			continue;
1983698b1b30SVlastimil Babka 
1984698b1b30SVlastimil Babka 		cc.nr_freepages = 0;
1985698b1b30SVlastimil Babka 		cc.nr_migratepages = 0;
19867f354a54SDavid Rientjes 		cc.total_migrate_scanned = 0;
19877f354a54SDavid Rientjes 		cc.total_free_scanned = 0;
1988698b1b30SVlastimil Babka 		cc.zone = zone;
1989698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1990698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1991698b1b30SVlastimil Babka 
1992172400c6SVlastimil Babka 		if (kthread_should_stop())
1993172400c6SVlastimil Babka 			return;
1994698b1b30SVlastimil Babka 		status = compact_zone(zone, &cc);
1995698b1b30SVlastimil Babka 
19967ceb009aSVlastimil Babka 		if (status == COMPACT_SUCCESS) {
1997698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
1998c8f7de0bSMichal Hocko 		} else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
1999698b1b30SVlastimil Babka 			/*
2000698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
2001698b1b30SVlastimil Babka 			 * sync direct compaction does.
2002698b1b30SVlastimil Babka 			 */
2003698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
2004698b1b30SVlastimil Babka 		}
2005698b1b30SVlastimil Babka 
20067f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
20077f354a54SDavid Rientjes 				     cc.total_migrate_scanned);
20087f354a54SDavid Rientjes 		count_compact_events(KCOMPACTD_FREE_SCANNED,
20097f354a54SDavid Rientjes 				     cc.total_free_scanned);
20107f354a54SDavid Rientjes 
2011698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
2012698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
2013698b1b30SVlastimil Babka 	}
2014698b1b30SVlastimil Babka 
2015698b1b30SVlastimil Babka 	/*
2016698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
2017698b1b30SVlastimil Babka 	 * the requested order/classzone_idx in case it was higher/tighter than
2018698b1b30SVlastimil Babka 	 * our current ones
2019698b1b30SVlastimil Babka 	 */
2020698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
2021698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
2022698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2023698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2024698b1b30SVlastimil Babka }
2025698b1b30SVlastimil Babka 
2026698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2027698b1b30SVlastimil Babka {
2028698b1b30SVlastimil Babka 	if (!order)
2029698b1b30SVlastimil Babka 		return;
2030698b1b30SVlastimil Babka 
2031698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
2032698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
2033698b1b30SVlastimil Babka 
2034698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx > classzone_idx)
2035698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = classzone_idx;
2036698b1b30SVlastimil Babka 
20376818600fSDavidlohr Bueso 	/*
20386818600fSDavidlohr Bueso 	 * Pairs with implicit barrier in wait_event_freezable()
20396818600fSDavidlohr Bueso 	 * such that wakeups are not missed.
20406818600fSDavidlohr Bueso 	 */
20416818600fSDavidlohr Bueso 	if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2042698b1b30SVlastimil Babka 		return;
2043698b1b30SVlastimil Babka 
2044698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
2045698b1b30SVlastimil Babka 		return;
2046698b1b30SVlastimil Babka 
2047698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2048698b1b30SVlastimil Babka 							classzone_idx);
2049698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
2050698b1b30SVlastimil Babka }
2051698b1b30SVlastimil Babka 
2052698b1b30SVlastimil Babka /*
2053698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
2054698b1b30SVlastimil Babka  * from the init process.
2055698b1b30SVlastimil Babka  */
2056698b1b30SVlastimil Babka static int kcompactd(void *p)
2057698b1b30SVlastimil Babka {
2058698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
2059698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
2060698b1b30SVlastimil Babka 
2061698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2062698b1b30SVlastimil Babka 
2063698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
2064698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
2065698b1b30SVlastimil Babka 
2066698b1b30SVlastimil Babka 	set_freezable();
2067698b1b30SVlastimil Babka 
2068698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
2069698b1b30SVlastimil Babka 	pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2070698b1b30SVlastimil Babka 
2071698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
2072698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2073698b1b30SVlastimil Babka 		wait_event_freezable(pgdat->kcompactd_wait,
2074698b1b30SVlastimil Babka 				kcompactd_work_requested(pgdat));
2075698b1b30SVlastimil Babka 
2076698b1b30SVlastimil Babka 		kcompactd_do_work(pgdat);
2077698b1b30SVlastimil Babka 	}
2078698b1b30SVlastimil Babka 
2079698b1b30SVlastimil Babka 	return 0;
2080698b1b30SVlastimil Babka }
2081698b1b30SVlastimil Babka 
2082698b1b30SVlastimil Babka /*
2083698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
2084698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2085698b1b30SVlastimil Babka  */
2086698b1b30SVlastimil Babka int kcompactd_run(int nid)
2087698b1b30SVlastimil Babka {
2088698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
2089698b1b30SVlastimil Babka 	int ret = 0;
2090698b1b30SVlastimil Babka 
2091698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
2092698b1b30SVlastimil Babka 		return 0;
2093698b1b30SVlastimil Babka 
2094698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2095698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
2096698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
2097698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
2098698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
2099698b1b30SVlastimil Babka 	}
2100698b1b30SVlastimil Babka 	return ret;
2101698b1b30SVlastimil Babka }
2102698b1b30SVlastimil Babka 
2103698b1b30SVlastimil Babka /*
2104698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
2105698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
2106698b1b30SVlastimil Babka  */
2107698b1b30SVlastimil Babka void kcompactd_stop(int nid)
2108698b1b30SVlastimil Babka {
2109698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2110698b1b30SVlastimil Babka 
2111698b1b30SVlastimil Babka 	if (kcompactd) {
2112698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
2113698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
2114698b1b30SVlastimil Babka 	}
2115698b1b30SVlastimil Babka }
2116698b1b30SVlastimil Babka 
2117698b1b30SVlastimil Babka /*
2118698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
2119698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
2120698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
2121698b1b30SVlastimil Babka  * restore their cpu bindings.
2122698b1b30SVlastimil Babka  */
2123e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu)
2124698b1b30SVlastimil Babka {
2125698b1b30SVlastimil Babka 	int nid;
2126698b1b30SVlastimil Babka 
2127698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY) {
2128698b1b30SVlastimil Babka 		pg_data_t *pgdat = NODE_DATA(nid);
2129698b1b30SVlastimil Babka 		const struct cpumask *mask;
2130698b1b30SVlastimil Babka 
2131698b1b30SVlastimil Babka 		mask = cpumask_of_node(pgdat->node_id);
2132698b1b30SVlastimil Babka 
2133698b1b30SVlastimil Babka 		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2134698b1b30SVlastimil Babka 			/* One of our CPUs online: restore mask */
2135698b1b30SVlastimil Babka 			set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2136698b1b30SVlastimil Babka 	}
2137e46b1db2SAnna-Maria Gleixner 	return 0;
2138698b1b30SVlastimil Babka }
2139698b1b30SVlastimil Babka 
2140698b1b30SVlastimil Babka static int __init kcompactd_init(void)
2141698b1b30SVlastimil Babka {
2142698b1b30SVlastimil Babka 	int nid;
2143e46b1db2SAnna-Maria Gleixner 	int ret;
2144e46b1db2SAnna-Maria Gleixner 
2145e46b1db2SAnna-Maria Gleixner 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2146e46b1db2SAnna-Maria Gleixner 					"mm/compaction:online",
2147e46b1db2SAnna-Maria Gleixner 					kcompactd_cpu_online, NULL);
2148e46b1db2SAnna-Maria Gleixner 	if (ret < 0) {
2149e46b1db2SAnna-Maria Gleixner 		pr_err("kcompactd: failed to register hotplug callbacks.\n");
2150e46b1db2SAnna-Maria Gleixner 		return ret;
2151e46b1db2SAnna-Maria Gleixner 	}
2152698b1b30SVlastimil Babka 
2153698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
2154698b1b30SVlastimil Babka 		kcompactd_run(nid);
2155698b1b30SVlastimil Babka 	return 0;
2156698b1b30SVlastimil Babka }
2157698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
2158698b1b30SVlastimil Babka 
2159ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
2160