xref: /openbmc/linux/mm/compaction.c (revision 14af4a5e9b26ad251f81c174e8a43f3e179434a5)
1748446bbSMel Gorman /*
2748446bbSMel Gorman  * linux/mm/compaction.c
3748446bbSMel Gorman  *
4748446bbSMel Gorman  * Memory compaction for the reduction of external fragmentation. Note that
5748446bbSMel Gorman  * this heavily depends upon page migration to do all the real heavy
6748446bbSMel Gorman  * lifting
7748446bbSMel Gorman  *
8748446bbSMel Gorman  * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9748446bbSMel Gorman  */
10698b1b30SVlastimil Babka #include <linux/cpu.h>
11748446bbSMel Gorman #include <linux/swap.h>
12748446bbSMel Gorman #include <linux/migrate.h>
13748446bbSMel Gorman #include <linux/compaction.h>
14748446bbSMel Gorman #include <linux/mm_inline.h>
15748446bbSMel Gorman #include <linux/backing-dev.h>
1676ab0f53SMel Gorman #include <linux/sysctl.h>
17ed4a6d7fSMel Gorman #include <linux/sysfs.h>
18bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h>
19194159fbSMinchan Kim #include <linux/page-isolation.h>
20b8c73fc2SAndrey Ryabinin #include <linux/kasan.h>
21698b1b30SVlastimil Babka #include <linux/kthread.h>
22698b1b30SVlastimil Babka #include <linux/freezer.h>
23748446bbSMel Gorman #include "internal.h"
24748446bbSMel Gorman 
25010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
26010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
27010fc29aSMinchan Kim {
28010fc29aSMinchan Kim 	count_vm_event(item);
29010fc29aSMinchan Kim }
30010fc29aSMinchan Kim 
31010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
32010fc29aSMinchan Kim {
33010fc29aSMinchan Kim 	count_vm_events(item, delta);
34010fc29aSMinchan Kim }
35010fc29aSMinchan Kim #else
36010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
37010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
38010fc29aSMinchan Kim #endif
39010fc29aSMinchan Kim 
40ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
41ff9543fdSMichal Nazarewicz 
42b7aba698SMel Gorman #define CREATE_TRACE_POINTS
43b7aba698SMel Gorman #include <trace/events/compaction.h>
44b7aba698SMel Gorman 
45748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
46748446bbSMel Gorman {
47748446bbSMel Gorman 	struct page *page, *next;
486bace090SVlastimil Babka 	unsigned long high_pfn = 0;
49748446bbSMel Gorman 
50748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
516bace090SVlastimil Babka 		unsigned long pfn = page_to_pfn(page);
52748446bbSMel Gorman 		list_del(&page->lru);
53748446bbSMel Gorman 		__free_page(page);
546bace090SVlastimil Babka 		if (pfn > high_pfn)
556bace090SVlastimil Babka 			high_pfn = pfn;
56748446bbSMel Gorman 	}
57748446bbSMel Gorman 
586bace090SVlastimil Babka 	return high_pfn;
59748446bbSMel Gorman }
60748446bbSMel Gorman 
61ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
62ff9543fdSMichal Nazarewicz {
63ff9543fdSMichal Nazarewicz 	struct page *page;
64ff9543fdSMichal Nazarewicz 
65ff9543fdSMichal Nazarewicz 	list_for_each_entry(page, list, lru) {
66ff9543fdSMichal Nazarewicz 		arch_alloc_page(page, 0);
67ff9543fdSMichal Nazarewicz 		kernel_map_pages(page, 1, 1);
68b8c73fc2SAndrey Ryabinin 		kasan_alloc_pages(page, 0);
69ff9543fdSMichal Nazarewicz 	}
70ff9543fdSMichal Nazarewicz }
71ff9543fdSMichal Nazarewicz 
7247118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype)
7347118af0SMichal Nazarewicz {
7447118af0SMichal Nazarewicz 	return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
7547118af0SMichal Nazarewicz }
7647118af0SMichal Nazarewicz 
77bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
7824e2716fSJoonsoo Kim 
7924e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */
8024e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6
8124e2716fSJoonsoo Kim 
8224e2716fSJoonsoo Kim /*
8324e2716fSJoonsoo Kim  * Compaction is deferred when compaction fails to result in a page
8424e2716fSJoonsoo Kim  * allocation success. 1 << compact_defer_limit compactions are skipped up
8524e2716fSJoonsoo Kim  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
8624e2716fSJoonsoo Kim  */
8724e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order)
8824e2716fSJoonsoo Kim {
8924e2716fSJoonsoo Kim 	zone->compact_considered = 0;
9024e2716fSJoonsoo Kim 	zone->compact_defer_shift++;
9124e2716fSJoonsoo Kim 
9224e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
9324e2716fSJoonsoo Kim 		zone->compact_order_failed = order;
9424e2716fSJoonsoo Kim 
9524e2716fSJoonsoo Kim 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
9624e2716fSJoonsoo Kim 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
9724e2716fSJoonsoo Kim 
9824e2716fSJoonsoo Kim 	trace_mm_compaction_defer_compaction(zone, order);
9924e2716fSJoonsoo Kim }
10024e2716fSJoonsoo Kim 
10124e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */
10224e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order)
10324e2716fSJoonsoo Kim {
10424e2716fSJoonsoo Kim 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
10524e2716fSJoonsoo Kim 
10624e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
10724e2716fSJoonsoo Kim 		return false;
10824e2716fSJoonsoo Kim 
10924e2716fSJoonsoo Kim 	/* Avoid possible overflow */
11024e2716fSJoonsoo Kim 	if (++zone->compact_considered > defer_limit)
11124e2716fSJoonsoo Kim 		zone->compact_considered = defer_limit;
11224e2716fSJoonsoo Kim 
11324e2716fSJoonsoo Kim 	if (zone->compact_considered >= defer_limit)
11424e2716fSJoonsoo Kim 		return false;
11524e2716fSJoonsoo Kim 
11624e2716fSJoonsoo Kim 	trace_mm_compaction_deferred(zone, order);
11724e2716fSJoonsoo Kim 
11824e2716fSJoonsoo Kim 	return true;
11924e2716fSJoonsoo Kim }
12024e2716fSJoonsoo Kim 
12124e2716fSJoonsoo Kim /*
12224e2716fSJoonsoo Kim  * Update defer tracking counters after successful compaction of given order,
12324e2716fSJoonsoo Kim  * which means an allocation either succeeded (alloc_success == true) or is
12424e2716fSJoonsoo Kim  * expected to succeed.
12524e2716fSJoonsoo Kim  */
12624e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order,
12724e2716fSJoonsoo Kim 		bool alloc_success)
12824e2716fSJoonsoo Kim {
12924e2716fSJoonsoo Kim 	if (alloc_success) {
13024e2716fSJoonsoo Kim 		zone->compact_considered = 0;
13124e2716fSJoonsoo Kim 		zone->compact_defer_shift = 0;
13224e2716fSJoonsoo Kim 	}
13324e2716fSJoonsoo Kim 	if (order >= zone->compact_order_failed)
13424e2716fSJoonsoo Kim 		zone->compact_order_failed = order + 1;
13524e2716fSJoonsoo Kim 
13624e2716fSJoonsoo Kim 	trace_mm_compaction_defer_reset(zone, order);
13724e2716fSJoonsoo Kim }
13824e2716fSJoonsoo Kim 
13924e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */
14024e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order)
14124e2716fSJoonsoo Kim {
14224e2716fSJoonsoo Kim 	if (order < zone->compact_order_failed)
14324e2716fSJoonsoo Kim 		return false;
14424e2716fSJoonsoo Kim 
14524e2716fSJoonsoo Kim 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
14624e2716fSJoonsoo Kim 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
14724e2716fSJoonsoo Kim }
14824e2716fSJoonsoo Kim 
149bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
150bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
151bb13ffebSMel Gorman 					struct page *page)
152bb13ffebSMel Gorman {
153bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
154bb13ffebSMel Gorman 		return true;
155bb13ffebSMel Gorman 
156bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
157bb13ffebSMel Gorman }
158bb13ffebSMel Gorman 
15902333641SVlastimil Babka static void reset_cached_positions(struct zone *zone)
16002333641SVlastimil Babka {
16102333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
16202333641SVlastimil Babka 	zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
163623446e4SJoonsoo Kim 	zone->compact_cached_free_pfn =
164623446e4SJoonsoo Kim 			round_down(zone_end_pfn(zone) - 1, pageblock_nr_pages);
16502333641SVlastimil Babka }
16602333641SVlastimil Babka 
167bb13ffebSMel Gorman /*
168bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
169bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
170bb13ffebSMel Gorman  * meet.
171bb13ffebSMel Gorman  */
17262997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
173bb13ffebSMel Gorman {
174bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
175108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
176bb13ffebSMel Gorman 	unsigned long pfn;
177bb13ffebSMel Gorman 
17862997027SMel Gorman 	zone->compact_blockskip_flush = false;
179bb13ffebSMel Gorman 
180bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
181bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
182bb13ffebSMel Gorman 		struct page *page;
183bb13ffebSMel Gorman 
184bb13ffebSMel Gorman 		cond_resched();
185bb13ffebSMel Gorman 
186bb13ffebSMel Gorman 		if (!pfn_valid(pfn))
187bb13ffebSMel Gorman 			continue;
188bb13ffebSMel Gorman 
189bb13ffebSMel Gorman 		page = pfn_to_page(pfn);
190bb13ffebSMel Gorman 		if (zone != page_zone(page))
191bb13ffebSMel Gorman 			continue;
192bb13ffebSMel Gorman 
193bb13ffebSMel Gorman 		clear_pageblock_skip(page);
194bb13ffebSMel Gorman 	}
19502333641SVlastimil Babka 
19602333641SVlastimil Babka 	reset_cached_positions(zone);
197bb13ffebSMel Gorman }
198bb13ffebSMel Gorman 
19962997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
20062997027SMel Gorman {
20162997027SMel Gorman 	int zoneid;
20262997027SMel Gorman 
20362997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
20462997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
20562997027SMel Gorman 		if (!populated_zone(zone))
20662997027SMel Gorman 			continue;
20762997027SMel Gorman 
20862997027SMel Gorman 		/* Only flush if a full compaction finished recently */
20962997027SMel Gorman 		if (zone->compact_blockskip_flush)
21062997027SMel Gorman 			__reset_isolation_suitable(zone);
21162997027SMel Gorman 	}
21262997027SMel Gorman }
21362997027SMel Gorman 
214bb13ffebSMel Gorman /*
215bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
21662997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
217bb13ffebSMel Gorman  */
218c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
219c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
220edc2ca61SVlastimil Babka 			bool migrate_scanner)
221bb13ffebSMel Gorman {
222c89511abSMel Gorman 	struct zone *zone = cc->zone;
22335979ef3SDavid Rientjes 	unsigned long pfn;
2246815bf3fSJoonsoo Kim 
2256815bf3fSJoonsoo Kim 	if (cc->ignore_skip_hint)
2266815bf3fSJoonsoo Kim 		return;
2276815bf3fSJoonsoo Kim 
228bb13ffebSMel Gorman 	if (!page)
229bb13ffebSMel Gorman 		return;
230bb13ffebSMel Gorman 
23135979ef3SDavid Rientjes 	if (nr_isolated)
23235979ef3SDavid Rientjes 		return;
23335979ef3SDavid Rientjes 
234bb13ffebSMel Gorman 	set_pageblock_skip(page);
235c89511abSMel Gorman 
23635979ef3SDavid Rientjes 	pfn = page_to_pfn(page);
23735979ef3SDavid Rientjes 
23835979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
239c89511abSMel Gorman 	if (migrate_scanner) {
24035979ef3SDavid Rientjes 		if (pfn > zone->compact_cached_migrate_pfn[0])
24135979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = pfn;
242e0b9daebSDavid Rientjes 		if (cc->mode != MIGRATE_ASYNC &&
243e0b9daebSDavid Rientjes 		    pfn > zone->compact_cached_migrate_pfn[1])
24435979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = pfn;
245c89511abSMel Gorman 	} else {
24635979ef3SDavid Rientjes 		if (pfn < zone->compact_cached_free_pfn)
247c89511abSMel Gorman 			zone->compact_cached_free_pfn = pfn;
248c89511abSMel Gorman 	}
249c89511abSMel Gorman }
250bb13ffebSMel Gorman #else
251bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
252bb13ffebSMel Gorman 					struct page *page)
253bb13ffebSMel Gorman {
254bb13ffebSMel Gorman 	return true;
255bb13ffebSMel Gorman }
256bb13ffebSMel Gorman 
257c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
258c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
259edc2ca61SVlastimil Babka 			bool migrate_scanner)
260bb13ffebSMel Gorman {
261bb13ffebSMel Gorman }
262bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
263bb13ffebSMel Gorman 
2641f9efdefSVlastimil Babka /*
2658b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
2668b44d279SVlastimil Babka  * very heavily contended. For async compaction, back out if the lock cannot
2678b44d279SVlastimil Babka  * be taken immediately. For sync compaction, spin on the lock if needed.
2688b44d279SVlastimil Babka  *
2698b44d279SVlastimil Babka  * Returns true if the lock is held
2708b44d279SVlastimil Babka  * Returns false if the lock is not held and compaction should abort
2711f9efdefSVlastimil Babka  */
2728b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
2738b44d279SVlastimil Babka 						struct compact_control *cc)
2748b44d279SVlastimil Babka {
2758b44d279SVlastimil Babka 	if (cc->mode == MIGRATE_ASYNC) {
2768b44d279SVlastimil Babka 		if (!spin_trylock_irqsave(lock, *flags)) {
2778b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_LOCK;
2788b44d279SVlastimil Babka 			return false;
2798b44d279SVlastimil Babka 		}
2808b44d279SVlastimil Babka 	} else {
2818b44d279SVlastimil Babka 		spin_lock_irqsave(lock, *flags);
2828b44d279SVlastimil Babka 	}
2831f9efdefSVlastimil Babka 
2848b44d279SVlastimil Babka 	return true;
2852a1402aaSMel Gorman }
2862a1402aaSMel Gorman 
28785aa125fSMichal Nazarewicz /*
288c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
2898b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
2908b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
2918b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
2928b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
2938b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
2948b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
2958b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
296c67fe375SMel Gorman  *
2978b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
2988b44d279SVlastimil Babka  *		async compaction due to need_resched()
2998b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
3008b44d279SVlastimil Babka  *		scheduled)
301c67fe375SMel Gorman  */
3028b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
3038b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
304c67fe375SMel Gorman {
3058b44d279SVlastimil Babka 	if (*locked) {
3068b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
3078b44d279SVlastimil Babka 		*locked = false;
308c67fe375SMel Gorman 	}
309c67fe375SMel Gorman 
3108b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
3118b44d279SVlastimil Babka 		cc->contended = COMPACT_CONTENDED_SCHED;
3128b44d279SVlastimil Babka 		return true;
3138b44d279SVlastimil Babka 	}
3148b44d279SVlastimil Babka 
3158b44d279SVlastimil Babka 	if (need_resched()) {
316e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC) {
3178b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
3188b44d279SVlastimil Babka 			return true;
319c67fe375SMel Gorman 		}
320c67fe375SMel Gorman 		cond_resched();
321c67fe375SMel Gorman 	}
322c67fe375SMel Gorman 
3238b44d279SVlastimil Babka 	return false;
324c67fe375SMel Gorman }
325c67fe375SMel Gorman 
326be976572SVlastimil Babka /*
327be976572SVlastimil Babka  * Aside from avoiding lock contention, compaction also periodically checks
328be976572SVlastimil Babka  * need_resched() and either schedules in sync compaction or aborts async
3298b44d279SVlastimil Babka  * compaction. This is similar to what compact_unlock_should_abort() does, but
330be976572SVlastimil Babka  * is used where no lock is concerned.
331be976572SVlastimil Babka  *
332be976572SVlastimil Babka  * Returns false when no scheduling was needed, or sync compaction scheduled.
333be976572SVlastimil Babka  * Returns true when async compaction should abort.
334be976572SVlastimil Babka  */
335be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc)
336be976572SVlastimil Babka {
337be976572SVlastimil Babka 	/* async compaction aborts if contended */
338be976572SVlastimil Babka 	if (need_resched()) {
339be976572SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC) {
3401f9efdefSVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
341be976572SVlastimil Babka 			return true;
342be976572SVlastimil Babka 		}
343be976572SVlastimil Babka 
344be976572SVlastimil Babka 		cond_resched();
345be976572SVlastimil Babka 	}
346be976572SVlastimil Babka 
347be976572SVlastimil Babka 	return false;
348be976572SVlastimil Babka }
349be976572SVlastimil Babka 
350c67fe375SMel Gorman /*
3519e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
3529e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
3539e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
35485aa125fSMichal Nazarewicz  */
355f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
356e14c720eSVlastimil Babka 				unsigned long *start_pfn,
35785aa125fSMichal Nazarewicz 				unsigned long end_pfn,
35885aa125fSMichal Nazarewicz 				struct list_head *freelist,
35985aa125fSMichal Nazarewicz 				bool strict)
360748446bbSMel Gorman {
361b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
362bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
363b8b2d825SXiubo Li 	unsigned long flags = 0;
364f40d1e42SMel Gorman 	bool locked = false;
365e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
366748446bbSMel Gorman 
367748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
368748446bbSMel Gorman 
369f40d1e42SMel Gorman 	/* Isolate free pages. */
370748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
371748446bbSMel Gorman 		int isolated, i;
372748446bbSMel Gorman 		struct page *page = cursor;
373748446bbSMel Gorman 
3748b44d279SVlastimil Babka 		/*
3758b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
3768b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
3778b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
3788b44d279SVlastimil Babka 		 */
3798b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
3808b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
3818b44d279SVlastimil Babka 								&locked, cc))
3828b44d279SVlastimil Babka 			break;
3838b44d279SVlastimil Babka 
384b7aba698SMel Gorman 		nr_scanned++;
385f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
3862af120bcSLaura Abbott 			goto isolate_fail;
3872af120bcSLaura Abbott 
388bb13ffebSMel Gorman 		if (!valid_page)
389bb13ffebSMel Gorman 			valid_page = page;
3909fcd6d2eSVlastimil Babka 
3919fcd6d2eSVlastimil Babka 		/*
3929fcd6d2eSVlastimil Babka 		 * For compound pages such as THP and hugetlbfs, we can save
3939fcd6d2eSVlastimil Babka 		 * potentially a lot of iterations if we skip them at once.
3949fcd6d2eSVlastimil Babka 		 * The check is racy, but we can consider only valid values
3959fcd6d2eSVlastimil Babka 		 * and the only danger is skipping too much.
3969fcd6d2eSVlastimil Babka 		 */
3979fcd6d2eSVlastimil Babka 		if (PageCompound(page)) {
3989fcd6d2eSVlastimil Babka 			unsigned int comp_order = compound_order(page);
3999fcd6d2eSVlastimil Babka 
4009fcd6d2eSVlastimil Babka 			if (likely(comp_order < MAX_ORDER)) {
4019fcd6d2eSVlastimil Babka 				blockpfn += (1UL << comp_order) - 1;
4029fcd6d2eSVlastimil Babka 				cursor += (1UL << comp_order) - 1;
4039fcd6d2eSVlastimil Babka 			}
4049fcd6d2eSVlastimil Babka 
4059fcd6d2eSVlastimil Babka 			goto isolate_fail;
4069fcd6d2eSVlastimil Babka 		}
4079fcd6d2eSVlastimil Babka 
408f40d1e42SMel Gorman 		if (!PageBuddy(page))
4092af120bcSLaura Abbott 			goto isolate_fail;
410f40d1e42SMel Gorman 
411f40d1e42SMel Gorman 		/*
41269b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
41369b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
41469b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
41569b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
41669b7189fSVlastimil Babka 		 * recheck as well.
41769b7189fSVlastimil Babka 		 */
41869b7189fSVlastimil Babka 		if (!locked) {
41969b7189fSVlastimil Babka 			/*
420f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
421f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
422f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
423f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
424f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
425f40d1e42SMel Gorman 			 * possible.
426f40d1e42SMel Gorman 			 */
4278b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
4288b44d279SVlastimil Babka 								&flags, cc);
429f40d1e42SMel Gorman 			if (!locked)
430f40d1e42SMel Gorman 				break;
431f40d1e42SMel Gorman 
432f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
433f40d1e42SMel Gorman 			if (!PageBuddy(page))
4342af120bcSLaura Abbott 				goto isolate_fail;
43569b7189fSVlastimil Babka 		}
436748446bbSMel Gorman 
437748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
438748446bbSMel Gorman 		isolated = split_free_page(page);
439748446bbSMel Gorman 		total_isolated += isolated;
440748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
441748446bbSMel Gorman 			list_add(&page->lru, freelist);
442748446bbSMel Gorman 			page++;
443748446bbSMel Gorman 		}
444748446bbSMel Gorman 
445748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
446748446bbSMel Gorman 		if (isolated) {
447932ff6bbSJoonsoo Kim 			cc->nr_freepages += isolated;
448932ff6bbSJoonsoo Kim 			if (!strict &&
449932ff6bbSJoonsoo Kim 				cc->nr_migratepages <= cc->nr_freepages) {
450932ff6bbSJoonsoo Kim 				blockpfn += isolated;
451932ff6bbSJoonsoo Kim 				break;
452932ff6bbSJoonsoo Kim 			}
453932ff6bbSJoonsoo Kim 
454748446bbSMel Gorman 			blockpfn += isolated - 1;
455748446bbSMel Gorman 			cursor += isolated - 1;
4562af120bcSLaura Abbott 			continue;
457748446bbSMel Gorman 		}
4582af120bcSLaura Abbott 
4592af120bcSLaura Abbott isolate_fail:
4602af120bcSLaura Abbott 		if (strict)
4612af120bcSLaura Abbott 			break;
4622af120bcSLaura Abbott 		else
4632af120bcSLaura Abbott 			continue;
4642af120bcSLaura Abbott 
465748446bbSMel Gorman 	}
466748446bbSMel Gorman 
4679fcd6d2eSVlastimil Babka 	/*
4689fcd6d2eSVlastimil Babka 	 * There is a tiny chance that we have read bogus compound_order(),
4699fcd6d2eSVlastimil Babka 	 * so be careful to not go outside of the pageblock.
4709fcd6d2eSVlastimil Babka 	 */
4719fcd6d2eSVlastimil Babka 	if (unlikely(blockpfn > end_pfn))
4729fcd6d2eSVlastimil Babka 		blockpfn = end_pfn;
4739fcd6d2eSVlastimil Babka 
474e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
475e34d85f0SJoonsoo Kim 					nr_scanned, total_isolated);
476e34d85f0SJoonsoo Kim 
477e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
478e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
479e14c720eSVlastimil Babka 
480f40d1e42SMel Gorman 	/*
481f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
482f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
483f40d1e42SMel Gorman 	 * returned and CMA will fail.
484f40d1e42SMel Gorman 	 */
4852af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
486f40d1e42SMel Gorman 		total_isolated = 0;
487f40d1e42SMel Gorman 
488f40d1e42SMel Gorman 	if (locked)
489f40d1e42SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
490f40d1e42SMel Gorman 
491bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
492bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
493edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
494bb13ffebSMel Gorman 
495010fc29aSMinchan Kim 	count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
496397487dbSMel Gorman 	if (total_isolated)
497010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
498748446bbSMel Gorman 	return total_isolated;
499748446bbSMel Gorman }
500748446bbSMel Gorman 
50185aa125fSMichal Nazarewicz /**
50285aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
50385aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
50485aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
50585aa125fSMichal Nazarewicz  *
50685aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
50785aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
50885aa125fSMichal Nazarewicz  * undo its actions and return zero.
50985aa125fSMichal Nazarewicz  *
51085aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
51185aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
51285aa125fSMichal Nazarewicz  * a free page).
51385aa125fSMichal Nazarewicz  */
514ff9543fdSMichal Nazarewicz unsigned long
515bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
516bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
51785aa125fSMichal Nazarewicz {
518e1409c32SJoonsoo Kim 	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
51985aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
52085aa125fSMichal Nazarewicz 
5217d49d886SVlastimil Babka 	pfn = start_pfn;
522e1409c32SJoonsoo Kim 	block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
523e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
524e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
52585aa125fSMichal Nazarewicz 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
5267d49d886SVlastimil Babka 
5277d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
528e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
5297d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
530e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
531e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
5327d49d886SVlastimil Babka 
53385aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
53485aa125fSMichal Nazarewicz 
53558420016SJoonsoo Kim 		/*
53658420016SJoonsoo Kim 		 * pfn could pass the block_end_pfn if isolated freepage
53758420016SJoonsoo Kim 		 * is more than pageblock order. In this case, we adjust
53858420016SJoonsoo Kim 		 * scanning range to right one.
53958420016SJoonsoo Kim 		 */
54058420016SJoonsoo Kim 		if (pfn >= block_end_pfn) {
541e1409c32SJoonsoo Kim 			block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
54258420016SJoonsoo Kim 			block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
54358420016SJoonsoo Kim 			block_end_pfn = min(block_end_pfn, end_pfn);
54458420016SJoonsoo Kim 		}
54558420016SJoonsoo Kim 
546e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
547e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
5487d49d886SVlastimil Babka 			break;
5497d49d886SVlastimil Babka 
550e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
551e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
55285aa125fSMichal Nazarewicz 
55385aa125fSMichal Nazarewicz 		/*
55485aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
55585aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
55685aa125fSMichal Nazarewicz 		 * non-free pages).
55785aa125fSMichal Nazarewicz 		 */
55885aa125fSMichal Nazarewicz 		if (!isolated)
55985aa125fSMichal Nazarewicz 			break;
56085aa125fSMichal Nazarewicz 
56185aa125fSMichal Nazarewicz 		/*
56285aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
56385aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
56485aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
56585aa125fSMichal Nazarewicz 		 */
56685aa125fSMichal Nazarewicz 	}
56785aa125fSMichal Nazarewicz 
56885aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
56985aa125fSMichal Nazarewicz 	map_pages(&freelist);
57085aa125fSMichal Nazarewicz 
57185aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
57285aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
57385aa125fSMichal Nazarewicz 		release_freepages(&freelist);
57485aa125fSMichal Nazarewicz 		return 0;
57585aa125fSMichal Nazarewicz 	}
57685aa125fSMichal Nazarewicz 
57785aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
57885aa125fSMichal Nazarewicz 	return pfn;
57985aa125fSMichal Nazarewicz }
58085aa125fSMichal Nazarewicz 
581748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
582edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc)
583748446bbSMel Gorman {
584748446bbSMel Gorman 	struct page *page;
585b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
586748446bbSMel Gorman 
587edc2ca61SVlastimil Babka 	if (list_empty(&cc->migratepages))
588edc2ca61SVlastimil Babka 		return;
589edc2ca61SVlastimil Babka 
590b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
591b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
592748446bbSMel Gorman 
593c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
594c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
595c67fe375SMel Gorman }
596748446bbSMel Gorman 
597748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
598748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
599748446bbSMel Gorman {
600bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
601748446bbSMel Gorman 
602748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
603748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
604bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
605bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
606748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
607748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
608748446bbSMel Gorman 
609bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
610748446bbSMel Gorman }
611748446bbSMel Gorman 
6122fe86e00SMichal Nazarewicz /**
613edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
614edc2ca61SVlastimil Babka  *				  a single pageblock
6152fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
616edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
617edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
618edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
6192fe86e00SMichal Nazarewicz  *
6202fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
621edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
622edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
623edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
624edc2ca61SVlastimil Babka  * than end_pfn).
6252fe86e00SMichal Nazarewicz  *
626edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
627edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
628edc2ca61SVlastimil Babka  * is neither read nor updated.
629748446bbSMel Gorman  */
630edc2ca61SVlastimil Babka static unsigned long
631edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
632edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
633748446bbSMel Gorman {
634edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
635b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
636748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
637fa9add64SHugh Dickins 	struct lruvec *lruvec;
638b8b2d825SXiubo Li 	unsigned long flags = 0;
6392a1402aaSMel Gorman 	bool locked = false;
640bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
641e34d85f0SJoonsoo Kim 	unsigned long start_pfn = low_pfn;
642748446bbSMel Gorman 
643748446bbSMel Gorman 	/*
644748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
645748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
646748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
647748446bbSMel Gorman 	 */
648748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
649f9e35b3bSMel Gorman 		/* async migration should just abort */
650e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
6512fe86e00SMichal Nazarewicz 			return 0;
652f9e35b3bSMel Gorman 
653748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
654748446bbSMel Gorman 
655748446bbSMel Gorman 		if (fatal_signal_pending(current))
6562fe86e00SMichal Nazarewicz 			return 0;
657748446bbSMel Gorman 	}
658748446bbSMel Gorman 
659be976572SVlastimil Babka 	if (compact_should_abort(cc))
660aeef4b83SDavid Rientjes 		return 0;
661aeef4b83SDavid Rientjes 
662748446bbSMel Gorman 	/* Time to isolate some pages for migration */
663748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
66429c0dde8SVlastimil Babka 		bool is_lru;
66529c0dde8SVlastimil Babka 
6668b44d279SVlastimil Babka 		/*
6678b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
6688b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
6698b44d279SVlastimil Babka 		 * if contended.
6708b44d279SVlastimil Babka 		 */
6718b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
6728b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&zone->lru_lock, flags,
6738b44d279SVlastimil Babka 								&locked, cc))
6748b44d279SVlastimil Babka 			break;
675b2eef8c0SAndrea Arcangeli 
676748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
677748446bbSMel Gorman 			continue;
678b7aba698SMel Gorman 		nr_scanned++;
679748446bbSMel Gorman 
680748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
681dc908600SMel Gorman 
682bb13ffebSMel Gorman 		if (!valid_page)
683bb13ffebSMel Gorman 			valid_page = page;
684bb13ffebSMel Gorman 
685c122b208SJoonsoo Kim 		/*
68699c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
68799c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
68899c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
68999c0fd5eSVlastimil Babka 		 * potential isolation targets.
6906c14466cSMel Gorman 		 */
69199c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
69299c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
69399c0fd5eSVlastimil Babka 
69499c0fd5eSVlastimil Babka 			/*
69599c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
69699c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
69799c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
69899c0fd5eSVlastimil Babka 			 */
69999c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
70099c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
701748446bbSMel Gorman 			continue;
70299c0fd5eSVlastimil Babka 		}
703748446bbSMel Gorman 
7049927af74SMel Gorman 		/*
705bf6bddf1SRafael Aquini 		 * Check may be lockless but that's ok as we recheck later.
706bf6bddf1SRafael Aquini 		 * It's possible to migrate LRU pages and balloon pages
707bf6bddf1SRafael Aquini 		 * Skip any other type of page
708bf6bddf1SRafael Aquini 		 */
70929c0dde8SVlastimil Babka 		is_lru = PageLRU(page);
71029c0dde8SVlastimil Babka 		if (!is_lru) {
711bf6bddf1SRafael Aquini 			if (unlikely(balloon_page_movable(page))) {
712d6d86c0aSKonstantin Khlebnikov 				if (balloon_page_isolate(page)) {
713bf6bddf1SRafael Aquini 					/* Successfully isolated */
714b6c75016SJoonsoo Kim 					goto isolate_success;
715bf6bddf1SRafael Aquini 				}
716bf6bddf1SRafael Aquini 			}
717bf6bddf1SRafael Aquini 		}
718bc835011SAndrea Arcangeli 
719bc835011SAndrea Arcangeli 		/*
72029c0dde8SVlastimil Babka 		 * Regardless of being on LRU, compound pages such as THP and
72129c0dde8SVlastimil Babka 		 * hugetlbfs are not to be compacted. We can potentially save
72229c0dde8SVlastimil Babka 		 * a lot of iterations if we skip them at once. The check is
72329c0dde8SVlastimil Babka 		 * racy, but we can consider only valid values and the only
72429c0dde8SVlastimil Babka 		 * danger is skipping too much.
725bc835011SAndrea Arcangeli 		 */
72629c0dde8SVlastimil Babka 		if (PageCompound(page)) {
72729c0dde8SVlastimil Babka 			unsigned int comp_order = compound_order(page);
72829c0dde8SVlastimil Babka 
72929c0dde8SVlastimil Babka 			if (likely(comp_order < MAX_ORDER))
73029c0dde8SVlastimil Babka 				low_pfn += (1UL << comp_order) - 1;
731edc2ca61SVlastimil Babka 
7322a1402aaSMel Gorman 			continue;
7332a1402aaSMel Gorman 		}
7342a1402aaSMel Gorman 
73529c0dde8SVlastimil Babka 		if (!is_lru)
73629c0dde8SVlastimil Babka 			continue;
73729c0dde8SVlastimil Babka 
738119d6d59SDavid Rientjes 		/*
739119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
740119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
741119d6d59SDavid Rientjes 		 * admittedly racy check.
742119d6d59SDavid Rientjes 		 */
743119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
744119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
745119d6d59SDavid Rientjes 			continue;
746119d6d59SDavid Rientjes 
74769b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
74869b7189fSVlastimil Babka 		if (!locked) {
7498b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&zone->lru_lock,
7508b44d279SVlastimil Babka 								&flags, cc);
7518b44d279SVlastimil Babka 			if (!locked)
7522a1402aaSMel Gorman 				break;
7532a1402aaSMel Gorman 
75429c0dde8SVlastimil Babka 			/* Recheck PageLRU and PageCompound under lock */
7552a1402aaSMel Gorman 			if (!PageLRU(page))
7562a1402aaSMel Gorman 				continue;
75729c0dde8SVlastimil Babka 
75829c0dde8SVlastimil Babka 			/*
75929c0dde8SVlastimil Babka 			 * Page become compound since the non-locked check,
76029c0dde8SVlastimil Babka 			 * and it's on LRU. It can only be a THP so the order
76129c0dde8SVlastimil Babka 			 * is safe to read and it's 0 for tail pages.
76229c0dde8SVlastimil Babka 			 */
76329c0dde8SVlastimil Babka 			if (unlikely(PageCompound(page))) {
76429c0dde8SVlastimil Babka 				low_pfn += (1UL << compound_order(page)) - 1;
765bc835011SAndrea Arcangeli 				continue;
766bc835011SAndrea Arcangeli 			}
76769b7189fSVlastimil Babka 		}
768bc835011SAndrea Arcangeli 
769fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
770fa9add64SHugh Dickins 
771748446bbSMel Gorman 		/* Try isolate the page */
772edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
773748446bbSMel Gorman 			continue;
774748446bbSMel Gorman 
77529c0dde8SVlastimil Babka 		VM_BUG_ON_PAGE(PageCompound(page), page);
776bc835011SAndrea Arcangeli 
777748446bbSMel Gorman 		/* Successfully isolated */
778fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
779b6c75016SJoonsoo Kim 
780b6c75016SJoonsoo Kim isolate_success:
781748446bbSMel Gorman 		list_add(&page->lru, migratelist);
782748446bbSMel Gorman 		cc->nr_migratepages++;
783b7aba698SMel Gorman 		nr_isolated++;
784748446bbSMel Gorman 
785748446bbSMel Gorman 		/* Avoid isolating too much */
78631b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
78731b8384aSHillf Danton 			++low_pfn;
788748446bbSMel Gorman 			break;
789748446bbSMel Gorman 		}
79031b8384aSHillf Danton 	}
791748446bbSMel Gorman 
79299c0fd5eSVlastimil Babka 	/*
79399c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
79499c0fd5eSVlastimil Babka 	 * the range to be scanned.
79599c0fd5eSVlastimil Babka 	 */
79699c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
79799c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
79899c0fd5eSVlastimil Babka 
799c67fe375SMel Gorman 	if (locked)
800c67fe375SMel Gorman 		spin_unlock_irqrestore(&zone->lru_lock, flags);
801748446bbSMel Gorman 
80250b5b094SVlastimil Babka 	/*
80350b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
80450b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
80550b5b094SVlastimil Babka 	 */
80635979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
807edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
808bb13ffebSMel Gorman 
809e34d85f0SJoonsoo Kim 	trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
810e34d85f0SJoonsoo Kim 						nr_scanned, nr_isolated);
811b7aba698SMel Gorman 
812010fc29aSMinchan Kim 	count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
813397487dbSMel Gorman 	if (nr_isolated)
814010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
815397487dbSMel Gorman 
8162fe86e00SMichal Nazarewicz 	return low_pfn;
8172fe86e00SMichal Nazarewicz }
8182fe86e00SMichal Nazarewicz 
819edc2ca61SVlastimil Babka /**
820edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
821edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
822edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
823edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
824edc2ca61SVlastimil Babka  *
825edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
826edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
827edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
828edc2ca61SVlastimil Babka  */
829edc2ca61SVlastimil Babka unsigned long
830edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
831edc2ca61SVlastimil Babka 							unsigned long end_pfn)
832edc2ca61SVlastimil Babka {
833e1409c32SJoonsoo Kim 	unsigned long pfn, block_start_pfn, block_end_pfn;
834edc2ca61SVlastimil Babka 
835edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
836edc2ca61SVlastimil Babka 	pfn = start_pfn;
837e1409c32SJoonsoo Kim 	block_start_pfn = pfn & ~(pageblock_nr_pages - 1);
838e1409c32SJoonsoo Kim 	if (block_start_pfn < cc->zone->zone_start_pfn)
839e1409c32SJoonsoo Kim 		block_start_pfn = cc->zone->zone_start_pfn;
840edc2ca61SVlastimil Babka 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
841edc2ca61SVlastimil Babka 
842edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
843e1409c32SJoonsoo Kim 				block_start_pfn = block_end_pfn,
844edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
845edc2ca61SVlastimil Babka 
846edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
847edc2ca61SVlastimil Babka 
848e1409c32SJoonsoo Kim 		if (!pageblock_pfn_to_page(block_start_pfn,
849e1409c32SJoonsoo Kim 					block_end_pfn, cc->zone))
850edc2ca61SVlastimil Babka 			continue;
851edc2ca61SVlastimil Babka 
852edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
853edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
854edc2ca61SVlastimil Babka 
855*14af4a5eSHugh Dickins 		if (!pfn)
856edc2ca61SVlastimil Babka 			break;
8576ea41c0cSJoonsoo Kim 
8586ea41c0cSJoonsoo Kim 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
8596ea41c0cSJoonsoo Kim 			break;
860edc2ca61SVlastimil Babka 	}
861edc2ca61SVlastimil Babka 	acct_isolated(cc->zone, cc);
862edc2ca61SVlastimil Babka 
863edc2ca61SVlastimil Babka 	return pfn;
864edc2ca61SVlastimil Babka }
865edc2ca61SVlastimil Babka 
866ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
867ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
868018e9a49SAndrew Morton 
869018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */
870018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page)
871018e9a49SAndrew Morton {
872018e9a49SAndrew Morton 	/* If the page is a large free page, then disallow migration */
873018e9a49SAndrew Morton 	if (PageBuddy(page)) {
874018e9a49SAndrew Morton 		/*
875018e9a49SAndrew Morton 		 * We are checking page_order without zone->lock taken. But
876018e9a49SAndrew Morton 		 * the only small danger is that we skip a potentially suitable
877018e9a49SAndrew Morton 		 * pageblock, so it's not worth to check order for valid range.
878018e9a49SAndrew Morton 		 */
879018e9a49SAndrew Morton 		if (page_order_unsafe(page) >= pageblock_order)
880018e9a49SAndrew Morton 			return false;
881018e9a49SAndrew Morton 	}
882018e9a49SAndrew Morton 
883018e9a49SAndrew Morton 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
884018e9a49SAndrew Morton 	if (migrate_async_suitable(get_pageblock_migratetype(page)))
885018e9a49SAndrew Morton 		return true;
886018e9a49SAndrew Morton 
887018e9a49SAndrew Morton 	/* Otherwise skip the block */
888018e9a49SAndrew Morton 	return false;
889018e9a49SAndrew Morton }
890018e9a49SAndrew Morton 
891ff9543fdSMichal Nazarewicz /*
892f2849aa0SVlastimil Babka  * Test whether the free scanner has reached the same or lower pageblock than
893f2849aa0SVlastimil Babka  * the migration scanner, and compaction should thus terminate.
894f2849aa0SVlastimil Babka  */
895f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc)
896f2849aa0SVlastimil Babka {
897f2849aa0SVlastimil Babka 	return (cc->free_pfn >> pageblock_order)
898f2849aa0SVlastimil Babka 		<= (cc->migrate_pfn >> pageblock_order);
899f2849aa0SVlastimil Babka }
900f2849aa0SVlastimil Babka 
901f2849aa0SVlastimil Babka /*
902ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
903ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
904ff9543fdSMichal Nazarewicz  */
905edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
906ff9543fdSMichal Nazarewicz {
907edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
908ff9543fdSMichal Nazarewicz 	struct page *page;
909c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
910e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
911c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
912c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
913ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
9142fe86e00SMichal Nazarewicz 
915ff9543fdSMichal Nazarewicz 	/*
916ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
91749e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
918e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
919e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
920c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
921c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
922c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
92349e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
92449e068f0SVlastimil Babka 	 * is using.
925ff9543fdSMichal Nazarewicz 	 */
926e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
927c96b9e50SVlastimil Babka 	block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
928c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
929c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
9307ed695e0SVlastimil Babka 	low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
9312fe86e00SMichal Nazarewicz 
932ff9543fdSMichal Nazarewicz 	/*
933ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
934ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
935ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
936ff9543fdSMichal Nazarewicz 	 */
937f5f61a32SVlastimil Babka 	for (; block_start_pfn >= low_pfn;
938c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
939e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
940e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
941ff9543fdSMichal Nazarewicz 
942f6ea3adbSDavid Rientjes 		/*
943f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
944f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
945be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
946f6ea3adbSDavid Rientjes 		 */
947be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
948be976572SVlastimil Babka 						&& compact_should_abort(cc))
949be976572SVlastimil Babka 			break;
950f6ea3adbSDavid Rientjes 
9517d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
9527d49d886SVlastimil Babka 									zone);
9537d49d886SVlastimil Babka 		if (!page)
954ff9543fdSMichal Nazarewicz 			continue;
955ff9543fdSMichal Nazarewicz 
956ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
95768e3e926SLinus Torvalds 		if (!suitable_migration_target(page))
958ff9543fdSMichal Nazarewicz 			continue;
95968e3e926SLinus Torvalds 
960bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
961bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
962bb13ffebSMel Gorman 			continue;
963bb13ffebSMel Gorman 
964e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
965932ff6bbSJoonsoo Kim 		isolate_freepages_block(cc, &isolate_start_pfn,
966c96b9e50SVlastimil Babka 					block_end_pfn, freelist, false);
967ff9543fdSMichal Nazarewicz 
968ff9543fdSMichal Nazarewicz 		/*
969f5f61a32SVlastimil Babka 		 * If we isolated enough freepages, or aborted due to async
970f5f61a32SVlastimil Babka 		 * compaction being contended, terminate the loop.
971e14c720eSVlastimil Babka 		 * Remember where the free scanner should restart next time,
972e14c720eSVlastimil Babka 		 * which is where isolate_freepages_block() left off.
973e14c720eSVlastimil Babka 		 * But if it scanned the whole pageblock, isolate_start_pfn
974e14c720eSVlastimil Babka 		 * now points at block_end_pfn, which is the start of the next
975e14c720eSVlastimil Babka 		 * pageblock.
976e14c720eSVlastimil Babka 		 * In that case we will however want to restart at the start
977e14c720eSVlastimil Babka 		 * of the previous pageblock.
978e14c720eSVlastimil Babka 		 */
979f5f61a32SVlastimil Babka 		if ((cc->nr_freepages >= cc->nr_migratepages)
980f5f61a32SVlastimil Babka 							|| cc->contended) {
981f5f61a32SVlastimil Babka 			if (isolate_start_pfn >= block_end_pfn)
982f5f61a32SVlastimil Babka 				isolate_start_pfn =
983e14c720eSVlastimil Babka 					block_start_pfn - pageblock_nr_pages;
984be976572SVlastimil Babka 			break;
985f5f61a32SVlastimil Babka 		} else {
986f5f61a32SVlastimil Babka 			/*
987f5f61a32SVlastimil Babka 			 * isolate_freepages_block() should not terminate
988f5f61a32SVlastimil Babka 			 * prematurely unless contended, or isolated enough
989f5f61a32SVlastimil Babka 			 */
990f5f61a32SVlastimil Babka 			VM_BUG_ON(isolate_start_pfn < block_end_pfn);
991f5f61a32SVlastimil Babka 		}
992c89511abSMel Gorman 	}
993ff9543fdSMichal Nazarewicz 
994ff9543fdSMichal Nazarewicz 	/* split_free_page does not map the pages */
995ff9543fdSMichal Nazarewicz 	map_pages(freelist);
996ff9543fdSMichal Nazarewicz 
9977ed695e0SVlastimil Babka 	/*
998f5f61a32SVlastimil Babka 	 * Record where the free scanner will restart next time. Either we
999f5f61a32SVlastimil Babka 	 * broke from the loop and set isolate_start_pfn based on the last
1000f5f61a32SVlastimil Babka 	 * call to isolate_freepages_block(), or we met the migration scanner
1001f5f61a32SVlastimil Babka 	 * and the loop terminated due to isolate_start_pfn < low_pfn
10027ed695e0SVlastimil Babka 	 */
1003f5f61a32SVlastimil Babka 	cc->free_pfn = isolate_start_pfn;
1004748446bbSMel Gorman }
1005748446bbSMel Gorman 
1006748446bbSMel Gorman /*
1007748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
1008748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
1009748446bbSMel Gorman  */
1010748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
1011748446bbSMel Gorman 					unsigned long data,
1012748446bbSMel Gorman 					int **result)
1013748446bbSMel Gorman {
1014748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
1015748446bbSMel Gorman 	struct page *freepage;
1016748446bbSMel Gorman 
1017be976572SVlastimil Babka 	/*
1018be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
1019be976572SVlastimil Babka 	 * contention.
1020be976572SVlastimil Babka 	 */
1021748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
1022be976572SVlastimil Babka 		if (!cc->contended)
1023edc2ca61SVlastimil Babka 			isolate_freepages(cc);
1024748446bbSMel Gorman 
1025748446bbSMel Gorman 		if (list_empty(&cc->freepages))
1026748446bbSMel Gorman 			return NULL;
1027748446bbSMel Gorman 	}
1028748446bbSMel Gorman 
1029748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
1030748446bbSMel Gorman 	list_del(&freepage->lru);
1031748446bbSMel Gorman 	cc->nr_freepages--;
1032748446bbSMel Gorman 
1033748446bbSMel Gorman 	return freepage;
1034748446bbSMel Gorman }
1035748446bbSMel Gorman 
1036748446bbSMel Gorman /*
1037d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
1038d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
1039d53aea3dSDavid Rientjes  * special handling needed for NUMA.
1040d53aea3dSDavid Rientjes  */
1041d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
1042d53aea3dSDavid Rientjes {
1043d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
1044d53aea3dSDavid Rientjes 
1045d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
1046d53aea3dSDavid Rientjes 	cc->nr_freepages++;
1047d53aea3dSDavid Rientjes }
1048d53aea3dSDavid Rientjes 
1049ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
1050ff9543fdSMichal Nazarewicz typedef enum {
1051ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
1052ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
1053ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
1054ff9543fdSMichal Nazarewicz } isolate_migrate_t;
1055ff9543fdSMichal Nazarewicz 
1056ff9543fdSMichal Nazarewicz /*
10575bbe3547SEric B Munson  * Allow userspace to control policy on scanning the unevictable LRU for
10585bbe3547SEric B Munson  * compactable pages.
10595bbe3547SEric B Munson  */
10605bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1;
10615bbe3547SEric B Munson 
10625bbe3547SEric B Munson /*
1063edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
1064edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
1065edc2ca61SVlastimil Babka  * compact_control.
1066ff9543fdSMichal Nazarewicz  */
1067ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
1068ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
1069ff9543fdSMichal Nazarewicz {
1070e1409c32SJoonsoo Kim 	unsigned long block_start_pfn;
1071e1409c32SJoonsoo Kim 	unsigned long block_end_pfn;
1072e1409c32SJoonsoo Kim 	unsigned long low_pfn;
10731a16718cSJoonsoo Kim 	unsigned long isolate_start_pfn;
1074edc2ca61SVlastimil Babka 	struct page *page;
1075edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
10765bbe3547SEric B Munson 		(sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
1077edc2ca61SVlastimil Babka 		(cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1078ff9543fdSMichal Nazarewicz 
1079edc2ca61SVlastimil Babka 	/*
1080edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
1081edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
1082edc2ca61SVlastimil Babka 	 */
1083edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
1084e1409c32SJoonsoo Kim 	block_start_pfn = cc->migrate_pfn & ~(pageblock_nr_pages - 1);
1085e1409c32SJoonsoo Kim 	if (block_start_pfn < zone->zone_start_pfn)
1086e1409c32SJoonsoo Kim 		block_start_pfn = zone->zone_start_pfn;
1087ff9543fdSMichal Nazarewicz 
1088ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
1089e1409c32SJoonsoo Kim 	block_end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
1090ff9543fdSMichal Nazarewicz 
1091edc2ca61SVlastimil Babka 	/*
1092edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
1093edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
1094edc2ca61SVlastimil Babka 	 */
1095e1409c32SJoonsoo Kim 	for (; block_end_pfn <= cc->free_pfn;
1096e1409c32SJoonsoo Kim 			low_pfn = block_end_pfn,
1097e1409c32SJoonsoo Kim 			block_start_pfn = block_end_pfn,
1098e1409c32SJoonsoo Kim 			block_end_pfn += pageblock_nr_pages) {
1099edc2ca61SVlastimil Babka 
1100edc2ca61SVlastimil Babka 		/*
1101edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
1102edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
1103edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
1104edc2ca61SVlastimil Babka 		 */
1105edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1106edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
1107edc2ca61SVlastimil Babka 			break;
1108edc2ca61SVlastimil Babka 
1109e1409c32SJoonsoo Kim 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1110e1409c32SJoonsoo Kim 									zone);
11117d49d886SVlastimil Babka 		if (!page)
1112edc2ca61SVlastimil Babka 			continue;
1113edc2ca61SVlastimil Babka 
1114edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1115edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1116edc2ca61SVlastimil Babka 			continue;
1117edc2ca61SVlastimil Babka 
1118edc2ca61SVlastimil Babka 		/*
1119edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1120edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1121edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1122edc2ca61SVlastimil Babka 		 */
1123edc2ca61SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC &&
1124edc2ca61SVlastimil Babka 		    !migrate_async_suitable(get_pageblock_migratetype(page)))
1125edc2ca61SVlastimil Babka 			continue;
1126ff9543fdSMichal Nazarewicz 
1127ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
11281a16718cSJoonsoo Kim 		isolate_start_pfn = low_pfn;
1129e1409c32SJoonsoo Kim 		low_pfn = isolate_migratepages_block(cc, low_pfn,
1130e1409c32SJoonsoo Kim 						block_end_pfn, isolate_mode);
1131edc2ca61SVlastimil Babka 
1132ff59909aSHugh Dickins 		if (!low_pfn || cc->contended) {
1133ff59909aSHugh Dickins 			acct_isolated(zone, cc);
1134ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1135ff59909aSHugh Dickins 		}
1136ff9543fdSMichal Nazarewicz 
1137edc2ca61SVlastimil Babka 		/*
11381a16718cSJoonsoo Kim 		 * Record where we could have freed pages by migration and not
11391a16718cSJoonsoo Kim 		 * yet flushed them to buddy allocator.
11401a16718cSJoonsoo Kim 		 * - this is the lowest page that could have been isolated and
11411a16718cSJoonsoo Kim 		 * then freed by migration.
11421a16718cSJoonsoo Kim 		 */
11431a16718cSJoonsoo Kim 		if (cc->nr_migratepages && !cc->last_migrated_pfn)
11441a16718cSJoonsoo Kim 			cc->last_migrated_pfn = isolate_start_pfn;
11451a16718cSJoonsoo Kim 
11461a16718cSJoonsoo Kim 		/*
1147edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1148edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1149edc2ca61SVlastimil Babka 		 * continue or not.
1150edc2ca61SVlastimil Babka 		 */
1151edc2ca61SVlastimil Babka 		break;
1152edc2ca61SVlastimil Babka 	}
1153edc2ca61SVlastimil Babka 
1154edc2ca61SVlastimil Babka 	acct_isolated(zone, cc);
1155f2849aa0SVlastimil Babka 	/* Record where migration scanner will be restarted. */
1156f2849aa0SVlastimil Babka 	cc->migrate_pfn = low_pfn;
1157ff9543fdSMichal Nazarewicz 
1158edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1159ff9543fdSMichal Nazarewicz }
1160ff9543fdSMichal Nazarewicz 
116121c527a3SYaowei Bai /*
116221c527a3SYaowei Bai  * order == -1 is expected when compacting via
116321c527a3SYaowei Bai  * /proc/sys/vm/compact_memory
116421c527a3SYaowei Bai  */
116521c527a3SYaowei Bai static inline bool is_via_compact_memory(int order)
116621c527a3SYaowei Bai {
116721c527a3SYaowei Bai 	return order == -1;
116821c527a3SYaowei Bai }
116921c527a3SYaowei Bai 
1170837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc,
11716d7ce559SDavid Rientjes 			    const int migratetype)
1172748446bbSMel Gorman {
11738fb74b9fSMel Gorman 	unsigned int order;
11745a03b051SAndrea Arcangeli 	unsigned long watermark;
117556de7263SMel Gorman 
1176be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
11772d1e1041SVlastimil Babka 		return COMPACT_CONTENDED;
1178748446bbSMel Gorman 
1179753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1180f2849aa0SVlastimil Babka 	if (compact_scanners_met(cc)) {
118155b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
118202333641SVlastimil Babka 		reset_cached_positions(zone);
118355b7c4c9SVlastimil Babka 
118462997027SMel Gorman 		/*
118562997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
1186accf6242SVlastimil Babka 		 * by kswapd when it goes to sleep. kcompactd does not set the
118762997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
118862997027SMel Gorman 		 * based on an allocation request.
118962997027SMel Gorman 		 */
1190accf6242SVlastimil Babka 		if (cc->direct_compaction)
119162997027SMel Gorman 			zone->compact_blockskip_flush = true;
119262997027SMel Gorman 
1193748446bbSMel Gorman 		return COMPACT_COMPLETE;
1194bb13ffebSMel Gorman 	}
1195748446bbSMel Gorman 
119621c527a3SYaowei Bai 	if (is_via_compact_memory(cc->order))
119756de7263SMel Gorman 		return COMPACT_CONTINUE;
119856de7263SMel Gorman 
11993957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
12003957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
12013957c776SMichal Hocko 
1202ebff3980SVlastimil Babka 	if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
1203ebff3980SVlastimil Babka 							cc->alloc_flags))
12043957c776SMichal Hocko 		return COMPACT_CONTINUE;
12053957c776SMichal Hocko 
120656de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
120756de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
12088fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
12092149cdaeSJoonsoo Kim 		bool can_steal;
12108fb74b9fSMel Gorman 
121156de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
12126d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
121356de7263SMel Gorman 			return COMPACT_PARTIAL;
121456de7263SMel Gorman 
12152149cdaeSJoonsoo Kim #ifdef CONFIG_CMA
12162149cdaeSJoonsoo Kim 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
12172149cdaeSJoonsoo Kim 		if (migratetype == MIGRATE_MOVABLE &&
12182149cdaeSJoonsoo Kim 			!list_empty(&area->free_list[MIGRATE_CMA]))
12192149cdaeSJoonsoo Kim 			return COMPACT_PARTIAL;
12202149cdaeSJoonsoo Kim #endif
12212149cdaeSJoonsoo Kim 		/*
12222149cdaeSJoonsoo Kim 		 * Job done if allocation would steal freepages from
12232149cdaeSJoonsoo Kim 		 * other migratetype buddy lists.
12242149cdaeSJoonsoo Kim 		 */
12252149cdaeSJoonsoo Kim 		if (find_suitable_fallback(area, order, migratetype,
12262149cdaeSJoonsoo Kim 						true, &can_steal) != -1)
122756de7263SMel Gorman 			return COMPACT_PARTIAL;
122856de7263SMel Gorman 	}
122956de7263SMel Gorman 
1230837d026dSJoonsoo Kim 	return COMPACT_NO_SUITABLE_PAGE;
1231837d026dSJoonsoo Kim }
1232837d026dSJoonsoo Kim 
1233837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc,
1234837d026dSJoonsoo Kim 			    const int migratetype)
1235837d026dSJoonsoo Kim {
1236837d026dSJoonsoo Kim 	int ret;
1237837d026dSJoonsoo Kim 
1238837d026dSJoonsoo Kim 	ret = __compact_finished(zone, cc, migratetype);
1239837d026dSJoonsoo Kim 	trace_mm_compaction_finished(zone, cc->order, ret);
1240837d026dSJoonsoo Kim 	if (ret == COMPACT_NO_SUITABLE_PAGE)
1241837d026dSJoonsoo Kim 		ret = COMPACT_CONTINUE;
1242837d026dSJoonsoo Kim 
1243837d026dSJoonsoo Kim 	return ret;
1244748446bbSMel Gorman }
1245748446bbSMel Gorman 
12463e7d3449SMel Gorman /*
12473e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
12483e7d3449SMel Gorman  * Returns
12493e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
12503e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
12513e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
12523e7d3449SMel Gorman  */
1253837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order,
1254ebff3980SVlastimil Babka 					int alloc_flags, int classzone_idx)
12553e7d3449SMel Gorman {
12563e7d3449SMel Gorman 	int fragindex;
12573e7d3449SMel Gorman 	unsigned long watermark;
12583e7d3449SMel Gorman 
125921c527a3SYaowei Bai 	if (is_via_compact_memory(order))
12603957c776SMichal Hocko 		return COMPACT_CONTINUE;
12613957c776SMichal Hocko 
1262ebff3980SVlastimil Babka 	watermark = low_wmark_pages(zone);
1263ebff3980SVlastimil Babka 	/*
1264ebff3980SVlastimil Babka 	 * If watermarks for high-order allocation are already met, there
1265ebff3980SVlastimil Babka 	 * should be no need for compaction at all.
1266ebff3980SVlastimil Babka 	 */
1267ebff3980SVlastimil Babka 	if (zone_watermark_ok(zone, order, watermark, classzone_idx,
1268ebff3980SVlastimil Babka 								alloc_flags))
1269ebff3980SVlastimil Babka 		return COMPACT_PARTIAL;
1270ebff3980SVlastimil Babka 
12713957c776SMichal Hocko 	/*
12723e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
12733e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
12743e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
12753e7d3449SMel Gorman 	 */
1276ebff3980SVlastimil Babka 	watermark += (2UL << order);
1277ebff3980SVlastimil Babka 	if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags))
12783e7d3449SMel Gorman 		return COMPACT_SKIPPED;
12793e7d3449SMel Gorman 
12803e7d3449SMel Gorman 	/*
12813e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
12823e7d3449SMel Gorman 	 * low memory or external fragmentation
12833e7d3449SMel Gorman 	 *
1284ebff3980SVlastimil Babka 	 * index of -1000 would imply allocations might succeed depending on
1285ebff3980SVlastimil Babka 	 * watermarks, but we already failed the high-order watermark check
12863e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
12873e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
12883e7d3449SMel Gorman 	 *
12893e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
12903e7d3449SMel Gorman 	 */
12913e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
12923e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1293837d026dSJoonsoo Kim 		return COMPACT_NOT_SUITABLE_ZONE;
12943e7d3449SMel Gorman 
12953e7d3449SMel Gorman 	return COMPACT_CONTINUE;
12963e7d3449SMel Gorman }
12973e7d3449SMel Gorman 
1298837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order,
1299837d026dSJoonsoo Kim 					int alloc_flags, int classzone_idx)
1300837d026dSJoonsoo Kim {
1301837d026dSJoonsoo Kim 	unsigned long ret;
1302837d026dSJoonsoo Kim 
1303837d026dSJoonsoo Kim 	ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx);
1304837d026dSJoonsoo Kim 	trace_mm_compaction_suitable(zone, order, ret);
1305837d026dSJoonsoo Kim 	if (ret == COMPACT_NOT_SUITABLE_ZONE)
1306837d026dSJoonsoo Kim 		ret = COMPACT_SKIPPED;
1307837d026dSJoonsoo Kim 
1308837d026dSJoonsoo Kim 	return ret;
1309837d026dSJoonsoo Kim }
1310837d026dSJoonsoo Kim 
1311748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
1312748446bbSMel Gorman {
1313748446bbSMel Gorman 	int ret;
1314c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1315108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
13166d7ce559SDavid Rientjes 	const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1317e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1318748446bbSMel Gorman 
1319ebff3980SVlastimil Babka 	ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1320ebff3980SVlastimil Babka 							cc->classzone_idx);
13213e7d3449SMel Gorman 	switch (ret) {
13223e7d3449SMel Gorman 	case COMPACT_PARTIAL:
13233e7d3449SMel Gorman 	case COMPACT_SKIPPED:
13243e7d3449SMel Gorman 		/* Compaction is likely to fail */
13253e7d3449SMel Gorman 		return ret;
13263e7d3449SMel Gorman 	case COMPACT_CONTINUE:
13273e7d3449SMel Gorman 		/* Fall through to compaction */
13283e7d3449SMel Gorman 		;
13293e7d3449SMel Gorman 	}
13303e7d3449SMel Gorman 
1331c89511abSMel Gorman 	/*
1332d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1333accf6242SVlastimil Babka 	 * is about to be retried after being deferred.
1334d3132e4bSVlastimil Babka 	 */
1335accf6242SVlastimil Babka 	if (compaction_restarting(zone, cc->order))
1336d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1337d3132e4bSVlastimil Babka 
1338d3132e4bSVlastimil Babka 	/*
1339c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
1340c89511abSMel Gorman 	 * information on where the scanners should start but check that it
1341c89511abSMel Gorman 	 * is initialised by ensuring the values are within zone boundaries.
1342c89511abSMel Gorman 	 */
1343e0b9daebSDavid Rientjes 	cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1344c89511abSMel Gorman 	cc->free_pfn = zone->compact_cached_free_pfn;
1345623446e4SJoonsoo Kim 	if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
1346623446e4SJoonsoo Kim 		cc->free_pfn = round_down(end_pfn - 1, pageblock_nr_pages);
1347c89511abSMel Gorman 		zone->compact_cached_free_pfn = cc->free_pfn;
1348c89511abSMel Gorman 	}
1349623446e4SJoonsoo Kim 	if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
1350c89511abSMel Gorman 		cc->migrate_pfn = start_pfn;
135135979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
135235979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1353c89511abSMel Gorman 	}
13541a16718cSJoonsoo Kim 	cc->last_migrated_pfn = 0;
1355748446bbSMel Gorman 
135616c4a097SJoonsoo Kim 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
135716c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync);
13580eb927c0SMel Gorman 
1359748446bbSMel Gorman 	migrate_prep_local();
1360748446bbSMel Gorman 
13616d7ce559SDavid Rientjes 	while ((ret = compact_finished(zone, cc, migratetype)) ==
13626d7ce559SDavid Rientjes 						COMPACT_CONTINUE) {
13639d502c1cSMinchan Kim 		int err;
1364748446bbSMel Gorman 
1365f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1366f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
13672d1e1041SVlastimil Babka 			ret = COMPACT_CONTENDED;
13685733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1369e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1370f9e35b3bSMel Gorman 			goto out;
1371f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1372fdaf7f5cSVlastimil Babka 			/*
1373fdaf7f5cSVlastimil Babka 			 * We haven't isolated and migrated anything, but
1374fdaf7f5cSVlastimil Babka 			 * there might still be unflushed migrations from
1375fdaf7f5cSVlastimil Babka 			 * previous cc->order aligned block.
1376fdaf7f5cSVlastimil Babka 			 */
1377fdaf7f5cSVlastimil Babka 			goto check_drain;
1378f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1379f9e35b3bSMel Gorman 			;
1380f9e35b3bSMel Gorman 		}
1381748446bbSMel Gorman 
1382d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1383e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
13847b2a2d4aSMel Gorman 				MR_COMPACTION);
1385748446bbSMel Gorman 
1386f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1387f8c9301fSVlastimil Babka 							&cc->migratepages);
1388748446bbSMel Gorman 
1389f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1390f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
13919d502c1cSMinchan Kim 		if (err) {
13925733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
13937ed695e0SVlastimil Babka 			/*
13947ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
13957ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
13967ed695e0SVlastimil Babka 			 */
1397f2849aa0SVlastimil Babka 			if (err == -ENOMEM && !compact_scanners_met(cc)) {
13982d1e1041SVlastimil Babka 				ret = COMPACT_CONTENDED;
13994bf2bba3SDavid Rientjes 				goto out;
1400748446bbSMel Gorman 			}
14014bf2bba3SDavid Rientjes 		}
1402fdaf7f5cSVlastimil Babka 
1403fdaf7f5cSVlastimil Babka check_drain:
1404fdaf7f5cSVlastimil Babka 		/*
1405fdaf7f5cSVlastimil Babka 		 * Has the migration scanner moved away from the previous
1406fdaf7f5cSVlastimil Babka 		 * cc->order aligned block where we migrated from? If yes,
1407fdaf7f5cSVlastimil Babka 		 * flush the pages that were freed, so that they can merge and
1408fdaf7f5cSVlastimil Babka 		 * compact_finished() can detect immediately if allocation
1409fdaf7f5cSVlastimil Babka 		 * would succeed.
1410fdaf7f5cSVlastimil Babka 		 */
14111a16718cSJoonsoo Kim 		if (cc->order > 0 && cc->last_migrated_pfn) {
1412fdaf7f5cSVlastimil Babka 			int cpu;
1413fdaf7f5cSVlastimil Babka 			unsigned long current_block_start =
1414fdaf7f5cSVlastimil Babka 				cc->migrate_pfn & ~((1UL << cc->order) - 1);
1415fdaf7f5cSVlastimil Babka 
14161a16718cSJoonsoo Kim 			if (cc->last_migrated_pfn < current_block_start) {
1417fdaf7f5cSVlastimil Babka 				cpu = get_cpu();
1418fdaf7f5cSVlastimil Babka 				lru_add_drain_cpu(cpu);
1419fdaf7f5cSVlastimil Babka 				drain_local_pages(zone);
1420fdaf7f5cSVlastimil Babka 				put_cpu();
1421fdaf7f5cSVlastimil Babka 				/* No more flushing until we migrate again */
14221a16718cSJoonsoo Kim 				cc->last_migrated_pfn = 0;
1423fdaf7f5cSVlastimil Babka 			}
1424fdaf7f5cSVlastimil Babka 		}
1425fdaf7f5cSVlastimil Babka 
1426748446bbSMel Gorman 	}
1427748446bbSMel Gorman 
1428f9e35b3bSMel Gorman out:
14296bace090SVlastimil Babka 	/*
14306bace090SVlastimil Babka 	 * Release free pages and update where the free scanner should restart,
14316bace090SVlastimil Babka 	 * so we don't leave any returned pages behind in the next attempt.
14326bace090SVlastimil Babka 	 */
14336bace090SVlastimil Babka 	if (cc->nr_freepages > 0) {
14346bace090SVlastimil Babka 		unsigned long free_pfn = release_freepages(&cc->freepages);
14356bace090SVlastimil Babka 
14366bace090SVlastimil Babka 		cc->nr_freepages = 0;
14376bace090SVlastimil Babka 		VM_BUG_ON(free_pfn == 0);
14386bace090SVlastimil Babka 		/* The cached pfn is always the first in a pageblock */
14396bace090SVlastimil Babka 		free_pfn &= ~(pageblock_nr_pages-1);
14406bace090SVlastimil Babka 		/*
14416bace090SVlastimil Babka 		 * Only go back, not forward. The cached pfn might have been
14426bace090SVlastimil Babka 		 * already reset to zone end in compact_finished()
14436bace090SVlastimil Babka 		 */
14446bace090SVlastimil Babka 		if (free_pfn > zone->compact_cached_free_pfn)
14456bace090SVlastimil Babka 			zone->compact_cached_free_pfn = free_pfn;
14466bace090SVlastimil Babka 	}
1447748446bbSMel Gorman 
144816c4a097SJoonsoo Kim 	trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
144916c4a097SJoonsoo Kim 				cc->free_pfn, end_pfn, sync, ret);
14500eb927c0SMel Gorman 
14512d1e1041SVlastimil Babka 	if (ret == COMPACT_CONTENDED)
14522d1e1041SVlastimil Babka 		ret = COMPACT_PARTIAL;
14532d1e1041SVlastimil Babka 
1454748446bbSMel Gorman 	return ret;
1455748446bbSMel Gorman }
145676ab0f53SMel Gorman 
1457e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order,
1458ebff3980SVlastimil Babka 		gfp_t gfp_mask, enum migrate_mode mode, int *contended,
1459ebff3980SVlastimil Babka 		int alloc_flags, int classzone_idx)
146056de7263SMel Gorman {
1461e64c5237SShaohua Li 	unsigned long ret;
146256de7263SMel Gorman 	struct compact_control cc = {
146356de7263SMel Gorman 		.nr_freepages = 0,
146456de7263SMel Gorman 		.nr_migratepages = 0,
146556de7263SMel Gorman 		.order = order,
14666d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
146756de7263SMel Gorman 		.zone = zone,
1468e0b9daebSDavid Rientjes 		.mode = mode,
1469ebff3980SVlastimil Babka 		.alloc_flags = alloc_flags,
1470ebff3980SVlastimil Babka 		.classzone_idx = classzone_idx,
1471accf6242SVlastimil Babka 		.direct_compaction = true,
147256de7263SMel Gorman 	};
147356de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
147456de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
147556de7263SMel Gorman 
1476e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1477e64c5237SShaohua Li 
1478e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1479e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1480e64c5237SShaohua Li 
1481e64c5237SShaohua Li 	*contended = cc.contended;
1482e64c5237SShaohua Li 	return ret;
148356de7263SMel Gorman }
148456de7263SMel Gorman 
14855e771905SMel Gorman int sysctl_extfrag_threshold = 500;
14865e771905SMel Gorman 
148756de7263SMel Gorman /**
148856de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
148956de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
14901a6d53a1SVlastimil Babka  * @order: The order of the current allocation
14911a6d53a1SVlastimil Babka  * @alloc_flags: The allocation flags of the current allocation
14921a6d53a1SVlastimil Babka  * @ac: The context of current allocation
1493e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
14941f9efdefSVlastimil Babka  * @contended: Return value that determines if compaction was aborted due to
14951f9efdefSVlastimil Babka  *	       need_resched() or lock contention
149656de7263SMel Gorman  *
149756de7263SMel Gorman  * This is the main entry point for direct page compaction.
149856de7263SMel Gorman  */
14991a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
15001a6d53a1SVlastimil Babka 			int alloc_flags, const struct alloc_context *ac,
15011a6d53a1SVlastimil Babka 			enum migrate_mode mode, int *contended)
150256de7263SMel Gorman {
150356de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
150456de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
150556de7263SMel Gorman 	struct zoneref *z;
150656de7263SMel Gorman 	struct zone *zone;
150753853e2dSVlastimil Babka 	int rc = COMPACT_DEFERRED;
15081f9efdefSVlastimil Babka 	int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
15091f9efdefSVlastimil Babka 
15101f9efdefSVlastimil Babka 	*contended = COMPACT_CONTENDED_NONE;
151156de7263SMel Gorman 
15124ffb6335SMel Gorman 	/* Check if the GFP flags allow compaction */
1513c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
151453853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
151556de7263SMel Gorman 
1516837d026dSJoonsoo Kim 	trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode);
1517837d026dSJoonsoo Kim 
151856de7263SMel Gorman 	/* Compact each zone in the list */
15191a6d53a1SVlastimil Babka 	for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
15201a6d53a1SVlastimil Babka 								ac->nodemask) {
152156de7263SMel Gorman 		int status;
15221f9efdefSVlastimil Babka 		int zone_contended;
152356de7263SMel Gorman 
152453853e2dSVlastimil Babka 		if (compaction_deferred(zone, order))
152553853e2dSVlastimil Babka 			continue;
152653853e2dSVlastimil Babka 
1527e0b9daebSDavid Rientjes 		status = compact_zone_order(zone, order, gfp_mask, mode,
15281a6d53a1SVlastimil Babka 				&zone_contended, alloc_flags,
15291a6d53a1SVlastimil Babka 				ac->classzone_idx);
153056de7263SMel Gorman 		rc = max(status, rc);
15311f9efdefSVlastimil Babka 		/*
15321f9efdefSVlastimil Babka 		 * It takes at least one zone that wasn't lock contended
15331f9efdefSVlastimil Babka 		 * to clear all_zones_contended.
15341f9efdefSVlastimil Babka 		 */
15351f9efdefSVlastimil Babka 		all_zones_contended &= zone_contended;
153656de7263SMel Gorman 
15373e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
1538ebff3980SVlastimil Babka 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone),
15391a6d53a1SVlastimil Babka 					ac->classzone_idx, alloc_flags)) {
154053853e2dSVlastimil Babka 			/*
154153853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
154253853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
154353853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
154453853e2dSVlastimil Babka 			 * succeeds in this zone.
154553853e2dSVlastimil Babka 			 */
154653853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
15471f9efdefSVlastimil Babka 			/*
15481f9efdefSVlastimil Babka 			 * It is possible that async compaction aborted due to
15491f9efdefSVlastimil Babka 			 * need_resched() and the watermarks were ok thanks to
15501f9efdefSVlastimil Babka 			 * somebody else freeing memory. The allocation can
15511f9efdefSVlastimil Babka 			 * however still fail so we better signal the
15521f9efdefSVlastimil Babka 			 * need_resched() contention anyway (this will not
15531f9efdefSVlastimil Babka 			 * prevent the allocation attempt).
15541f9efdefSVlastimil Babka 			 */
15551f9efdefSVlastimil Babka 			if (zone_contended == COMPACT_CONTENDED_SCHED)
15561f9efdefSVlastimil Babka 				*contended = COMPACT_CONTENDED_SCHED;
15571f9efdefSVlastimil Babka 
15581f9efdefSVlastimil Babka 			goto break_loop;
15591f9efdefSVlastimil Babka 		}
15601f9efdefSVlastimil Babka 
1561f8669795SVlastimil Babka 		if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) {
156253853e2dSVlastimil Babka 			/*
156353853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
156453853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
156553853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
156653853e2dSVlastimil Babka 			 */
156753853e2dSVlastimil Babka 			defer_compaction(zone, order);
156853853e2dSVlastimil Babka 		}
15691f9efdefSVlastimil Babka 
15701f9efdefSVlastimil Babka 		/*
15711f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
15721f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
15731f9efdefSVlastimil Babka 		 * case do not try further zones and signal need_resched()
15741f9efdefSVlastimil Babka 		 * contention.
15751f9efdefSVlastimil Babka 		 */
15761f9efdefSVlastimil Babka 		if ((zone_contended == COMPACT_CONTENDED_SCHED)
15771f9efdefSVlastimil Babka 					|| fatal_signal_pending(current)) {
15781f9efdefSVlastimil Babka 			*contended = COMPACT_CONTENDED_SCHED;
15791f9efdefSVlastimil Babka 			goto break_loop;
158056de7263SMel Gorman 		}
158156de7263SMel Gorman 
15821f9efdefSVlastimil Babka 		continue;
15831f9efdefSVlastimil Babka break_loop:
15841f9efdefSVlastimil Babka 		/*
15851f9efdefSVlastimil Babka 		 * We might not have tried all the zones, so  be conservative
15861f9efdefSVlastimil Babka 		 * and assume they are not all lock contended.
15871f9efdefSVlastimil Babka 		 */
15881f9efdefSVlastimil Babka 		all_zones_contended = 0;
15891f9efdefSVlastimil Babka 		break;
15901f9efdefSVlastimil Babka 	}
15911f9efdefSVlastimil Babka 
15921f9efdefSVlastimil Babka 	/*
15931f9efdefSVlastimil Babka 	 * If at least one zone wasn't deferred or skipped, we report if all
15941f9efdefSVlastimil Babka 	 * zones that were tried were lock contended.
15951f9efdefSVlastimil Babka 	 */
15961f9efdefSVlastimil Babka 	if (rc > COMPACT_SKIPPED && all_zones_contended)
15971f9efdefSVlastimil Babka 		*contended = COMPACT_CONTENDED_LOCK;
15981f9efdefSVlastimil Babka 
159956de7263SMel Gorman 	return rc;
160056de7263SMel Gorman }
160156de7263SMel Gorman 
160256de7263SMel Gorman 
160376ab0f53SMel Gorman /* Compact all zones within a node */
16047103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
160576ab0f53SMel Gorman {
160676ab0f53SMel Gorman 	int zoneid;
160776ab0f53SMel Gorman 	struct zone *zone;
160876ab0f53SMel Gorman 
160976ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
161076ab0f53SMel Gorman 
161176ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
161276ab0f53SMel Gorman 		if (!populated_zone(zone))
161376ab0f53SMel Gorman 			continue;
161476ab0f53SMel Gorman 
16157be62de9SRik van Riel 		cc->nr_freepages = 0;
16167be62de9SRik van Riel 		cc->nr_migratepages = 0;
16177be62de9SRik van Riel 		cc->zone = zone;
16187be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
16197be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
162076ab0f53SMel Gorman 
1621195b0c60SGioh Kim 		/*
1622195b0c60SGioh Kim 		 * When called via /proc/sys/vm/compact_memory
1623195b0c60SGioh Kim 		 * this makes sure we compact the whole zone regardless of
1624195b0c60SGioh Kim 		 * cached scanner positions.
1625195b0c60SGioh Kim 		 */
162621c527a3SYaowei Bai 		if (is_via_compact_memory(cc->order))
1627195b0c60SGioh Kim 			__reset_isolation_suitable(zone);
1628195b0c60SGioh Kim 
162921c527a3SYaowei Bai 		if (is_via_compact_memory(cc->order) ||
163021c527a3SYaowei Bai 				!compaction_deferred(zone, cc->order))
16317be62de9SRik van Riel 			compact_zone(zone, cc);
163276ab0f53SMel Gorman 
163375469345SJoonsoo Kim 		VM_BUG_ON(!list_empty(&cc->freepages));
163475469345SJoonsoo Kim 		VM_BUG_ON(!list_empty(&cc->migratepages));
163575469345SJoonsoo Kim 
163675469345SJoonsoo Kim 		if (is_via_compact_memory(cc->order))
163775469345SJoonsoo Kim 			continue;
163875469345SJoonsoo Kim 
1639de6c60a6SVlastimil Babka 		if (zone_watermark_ok(zone, cc->order,
1640de6c60a6SVlastimil Babka 				low_wmark_pages(zone), 0, 0))
1641de6c60a6SVlastimil Babka 			compaction_defer_reset(zone, cc->order, false);
1642aff62249SRik van Riel 	}
164376ab0f53SMel Gorman }
164476ab0f53SMel Gorman 
16457103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order)
16467be62de9SRik van Riel {
16477be62de9SRik van Riel 	struct compact_control cc = {
16487be62de9SRik van Riel 		.order = order,
1649e0b9daebSDavid Rientjes 		.mode = MIGRATE_ASYNC,
16507be62de9SRik van Riel 	};
16517be62de9SRik van Riel 
16523a7200afSMel Gorman 	if (!order)
16533a7200afSMel Gorman 		return;
16543a7200afSMel Gorman 
16557103f16dSAndrew Morton 	__compact_pgdat(pgdat, &cc);
16567be62de9SRik van Riel }
16577be62de9SRik van Riel 
16587103f16dSAndrew Morton static void compact_node(int nid)
16597be62de9SRik van Riel {
16607be62de9SRik van Riel 	struct compact_control cc = {
16617be62de9SRik van Riel 		.order = -1,
1662e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
166391ca9186SDavid Rientjes 		.ignore_skip_hint = true,
16647be62de9SRik van Riel 	};
16657be62de9SRik van Riel 
16667103f16dSAndrew Morton 	__compact_pgdat(NODE_DATA(nid), &cc);
16677be62de9SRik van Riel }
16687be62de9SRik van Riel 
166976ab0f53SMel Gorman /* Compact all nodes in the system */
16707964c06dSJason Liu static void compact_nodes(void)
167176ab0f53SMel Gorman {
167276ab0f53SMel Gorman 	int nid;
167376ab0f53SMel Gorman 
16748575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
16758575ec29SHugh Dickins 	lru_add_drain_all();
16768575ec29SHugh Dickins 
167776ab0f53SMel Gorman 	for_each_online_node(nid)
167876ab0f53SMel Gorman 		compact_node(nid);
167976ab0f53SMel Gorman }
168076ab0f53SMel Gorman 
168176ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
168276ab0f53SMel Gorman int sysctl_compact_memory;
168376ab0f53SMel Gorman 
1684fec4eb2cSYaowei Bai /*
1685fec4eb2cSYaowei Bai  * This is the entry point for compacting all nodes via
1686fec4eb2cSYaowei Bai  * /proc/sys/vm/compact_memory
1687fec4eb2cSYaowei Bai  */
168876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
168976ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
169076ab0f53SMel Gorman {
169176ab0f53SMel Gorman 	if (write)
16927964c06dSJason Liu 		compact_nodes();
169376ab0f53SMel Gorman 
169476ab0f53SMel Gorman 	return 0;
169576ab0f53SMel Gorman }
1696ed4a6d7fSMel Gorman 
16975e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
16985e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
16995e771905SMel Gorman {
17005e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
17015e771905SMel Gorman 
17025e771905SMel Gorman 	return 0;
17035e771905SMel Gorman }
17045e771905SMel Gorman 
1705ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
170674e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
170710fbcf4cSKay Sievers 			struct device_attribute *attr,
1708ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1709ed4a6d7fSMel Gorman {
17108575ec29SHugh Dickins 	int nid = dev->id;
17118575ec29SHugh Dickins 
17128575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
17138575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
17148575ec29SHugh Dickins 		lru_add_drain_all();
17158575ec29SHugh Dickins 
17168575ec29SHugh Dickins 		compact_node(nid);
17178575ec29SHugh Dickins 	}
1718ed4a6d7fSMel Gorman 
1719ed4a6d7fSMel Gorman 	return count;
1720ed4a6d7fSMel Gorman }
172110fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1722ed4a6d7fSMel Gorman 
1723ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1724ed4a6d7fSMel Gorman {
172510fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1726ed4a6d7fSMel Gorman }
1727ed4a6d7fSMel Gorman 
1728ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1729ed4a6d7fSMel Gorman {
173010fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1731ed4a6d7fSMel Gorman }
1732ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1733ff9543fdSMichal Nazarewicz 
1734698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat)
1735698b1b30SVlastimil Babka {
1736698b1b30SVlastimil Babka 	return pgdat->kcompactd_max_order > 0;
1737698b1b30SVlastimil Babka }
1738698b1b30SVlastimil Babka 
1739698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat)
1740698b1b30SVlastimil Babka {
1741698b1b30SVlastimil Babka 	int zoneid;
1742698b1b30SVlastimil Babka 	struct zone *zone;
1743698b1b30SVlastimil Babka 	enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
1744698b1b30SVlastimil Babka 
1745698b1b30SVlastimil Babka 	for (zoneid = 0; zoneid < classzone_idx; zoneid++) {
1746698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1747698b1b30SVlastimil Babka 
1748698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1749698b1b30SVlastimil Babka 			continue;
1750698b1b30SVlastimil Babka 
1751698b1b30SVlastimil Babka 		if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1752698b1b30SVlastimil Babka 					classzone_idx) == COMPACT_CONTINUE)
1753698b1b30SVlastimil Babka 			return true;
1754698b1b30SVlastimil Babka 	}
1755698b1b30SVlastimil Babka 
1756698b1b30SVlastimil Babka 	return false;
1757698b1b30SVlastimil Babka }
1758698b1b30SVlastimil Babka 
1759698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat)
1760698b1b30SVlastimil Babka {
1761698b1b30SVlastimil Babka 	/*
1762698b1b30SVlastimil Babka 	 * With no special task, compact all zones so that a page of requested
1763698b1b30SVlastimil Babka 	 * order is allocatable.
1764698b1b30SVlastimil Babka 	 */
1765698b1b30SVlastimil Babka 	int zoneid;
1766698b1b30SVlastimil Babka 	struct zone *zone;
1767698b1b30SVlastimil Babka 	struct compact_control cc = {
1768698b1b30SVlastimil Babka 		.order = pgdat->kcompactd_max_order,
1769698b1b30SVlastimil Babka 		.classzone_idx = pgdat->kcompactd_classzone_idx,
1770698b1b30SVlastimil Babka 		.mode = MIGRATE_SYNC_LIGHT,
1771698b1b30SVlastimil Babka 		.ignore_skip_hint = true,
1772698b1b30SVlastimil Babka 
1773698b1b30SVlastimil Babka 	};
1774698b1b30SVlastimil Babka 	bool success = false;
1775698b1b30SVlastimil Babka 
1776698b1b30SVlastimil Babka 	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1777698b1b30SVlastimil Babka 							cc.classzone_idx);
1778698b1b30SVlastimil Babka 	count_vm_event(KCOMPACTD_WAKE);
1779698b1b30SVlastimil Babka 
1780698b1b30SVlastimil Babka 	for (zoneid = 0; zoneid < cc.classzone_idx; zoneid++) {
1781698b1b30SVlastimil Babka 		int status;
1782698b1b30SVlastimil Babka 
1783698b1b30SVlastimil Babka 		zone = &pgdat->node_zones[zoneid];
1784698b1b30SVlastimil Babka 		if (!populated_zone(zone))
1785698b1b30SVlastimil Babka 			continue;
1786698b1b30SVlastimil Babka 
1787698b1b30SVlastimil Babka 		if (compaction_deferred(zone, cc.order))
1788698b1b30SVlastimil Babka 			continue;
1789698b1b30SVlastimil Babka 
1790698b1b30SVlastimil Babka 		if (compaction_suitable(zone, cc.order, 0, zoneid) !=
1791698b1b30SVlastimil Babka 							COMPACT_CONTINUE)
1792698b1b30SVlastimil Babka 			continue;
1793698b1b30SVlastimil Babka 
1794698b1b30SVlastimil Babka 		cc.nr_freepages = 0;
1795698b1b30SVlastimil Babka 		cc.nr_migratepages = 0;
1796698b1b30SVlastimil Babka 		cc.zone = zone;
1797698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.freepages);
1798698b1b30SVlastimil Babka 		INIT_LIST_HEAD(&cc.migratepages);
1799698b1b30SVlastimil Babka 
1800698b1b30SVlastimil Babka 		status = compact_zone(zone, &cc);
1801698b1b30SVlastimil Babka 
1802698b1b30SVlastimil Babka 		if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone),
1803698b1b30SVlastimil Babka 						cc.classzone_idx, 0)) {
1804698b1b30SVlastimil Babka 			success = true;
1805698b1b30SVlastimil Babka 			compaction_defer_reset(zone, cc.order, false);
1806698b1b30SVlastimil Babka 		} else if (status == COMPACT_COMPLETE) {
1807698b1b30SVlastimil Babka 			/*
1808698b1b30SVlastimil Babka 			 * We use sync migration mode here, so we defer like
1809698b1b30SVlastimil Babka 			 * sync direct compaction does.
1810698b1b30SVlastimil Babka 			 */
1811698b1b30SVlastimil Babka 			defer_compaction(zone, cc.order);
1812698b1b30SVlastimil Babka 		}
1813698b1b30SVlastimil Babka 
1814698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.freepages));
1815698b1b30SVlastimil Babka 		VM_BUG_ON(!list_empty(&cc.migratepages));
1816698b1b30SVlastimil Babka 	}
1817698b1b30SVlastimil Babka 
1818698b1b30SVlastimil Babka 	/*
1819698b1b30SVlastimil Babka 	 * Regardless of success, we are done until woken up next. But remember
1820698b1b30SVlastimil Babka 	 * the requested order/classzone_idx in case it was higher/tighter than
1821698b1b30SVlastimil Babka 	 * our current ones
1822698b1b30SVlastimil Babka 	 */
1823698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order <= cc.order)
1824698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = 0;
1825698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
1826698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
1827698b1b30SVlastimil Babka }
1828698b1b30SVlastimil Babka 
1829698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
1830698b1b30SVlastimil Babka {
1831698b1b30SVlastimil Babka 	if (!order)
1832698b1b30SVlastimil Babka 		return;
1833698b1b30SVlastimil Babka 
1834698b1b30SVlastimil Babka 	if (pgdat->kcompactd_max_order < order)
1835698b1b30SVlastimil Babka 		pgdat->kcompactd_max_order = order;
1836698b1b30SVlastimil Babka 
1837698b1b30SVlastimil Babka 	if (pgdat->kcompactd_classzone_idx > classzone_idx)
1838698b1b30SVlastimil Babka 		pgdat->kcompactd_classzone_idx = classzone_idx;
1839698b1b30SVlastimil Babka 
1840698b1b30SVlastimil Babka 	if (!waitqueue_active(&pgdat->kcompactd_wait))
1841698b1b30SVlastimil Babka 		return;
1842698b1b30SVlastimil Babka 
1843698b1b30SVlastimil Babka 	if (!kcompactd_node_suitable(pgdat))
1844698b1b30SVlastimil Babka 		return;
1845698b1b30SVlastimil Babka 
1846698b1b30SVlastimil Babka 	trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
1847698b1b30SVlastimil Babka 							classzone_idx);
1848698b1b30SVlastimil Babka 	wake_up_interruptible(&pgdat->kcompactd_wait);
1849698b1b30SVlastimil Babka }
1850698b1b30SVlastimil Babka 
1851698b1b30SVlastimil Babka /*
1852698b1b30SVlastimil Babka  * The background compaction daemon, started as a kernel thread
1853698b1b30SVlastimil Babka  * from the init process.
1854698b1b30SVlastimil Babka  */
1855698b1b30SVlastimil Babka static int kcompactd(void *p)
1856698b1b30SVlastimil Babka {
1857698b1b30SVlastimil Babka 	pg_data_t *pgdat = (pg_data_t*)p;
1858698b1b30SVlastimil Babka 	struct task_struct *tsk = current;
1859698b1b30SVlastimil Babka 
1860698b1b30SVlastimil Babka 	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
1861698b1b30SVlastimil Babka 
1862698b1b30SVlastimil Babka 	if (!cpumask_empty(cpumask))
1863698b1b30SVlastimil Babka 		set_cpus_allowed_ptr(tsk, cpumask);
1864698b1b30SVlastimil Babka 
1865698b1b30SVlastimil Babka 	set_freezable();
1866698b1b30SVlastimil Babka 
1867698b1b30SVlastimil Babka 	pgdat->kcompactd_max_order = 0;
1868698b1b30SVlastimil Babka 	pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
1869698b1b30SVlastimil Babka 
1870698b1b30SVlastimil Babka 	while (!kthread_should_stop()) {
1871698b1b30SVlastimil Babka 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
1872698b1b30SVlastimil Babka 		wait_event_freezable(pgdat->kcompactd_wait,
1873698b1b30SVlastimil Babka 				kcompactd_work_requested(pgdat));
1874698b1b30SVlastimil Babka 
1875698b1b30SVlastimil Babka 		kcompactd_do_work(pgdat);
1876698b1b30SVlastimil Babka 	}
1877698b1b30SVlastimil Babka 
1878698b1b30SVlastimil Babka 	return 0;
1879698b1b30SVlastimil Babka }
1880698b1b30SVlastimil Babka 
1881698b1b30SVlastimil Babka /*
1882698b1b30SVlastimil Babka  * This kcompactd start function will be called by init and node-hot-add.
1883698b1b30SVlastimil Babka  * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
1884698b1b30SVlastimil Babka  */
1885698b1b30SVlastimil Babka int kcompactd_run(int nid)
1886698b1b30SVlastimil Babka {
1887698b1b30SVlastimil Babka 	pg_data_t *pgdat = NODE_DATA(nid);
1888698b1b30SVlastimil Babka 	int ret = 0;
1889698b1b30SVlastimil Babka 
1890698b1b30SVlastimil Babka 	if (pgdat->kcompactd)
1891698b1b30SVlastimil Babka 		return 0;
1892698b1b30SVlastimil Babka 
1893698b1b30SVlastimil Babka 	pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
1894698b1b30SVlastimil Babka 	if (IS_ERR(pgdat->kcompactd)) {
1895698b1b30SVlastimil Babka 		pr_err("Failed to start kcompactd on node %d\n", nid);
1896698b1b30SVlastimil Babka 		ret = PTR_ERR(pgdat->kcompactd);
1897698b1b30SVlastimil Babka 		pgdat->kcompactd = NULL;
1898698b1b30SVlastimil Babka 	}
1899698b1b30SVlastimil Babka 	return ret;
1900698b1b30SVlastimil Babka }
1901698b1b30SVlastimil Babka 
1902698b1b30SVlastimil Babka /*
1903698b1b30SVlastimil Babka  * Called by memory hotplug when all memory in a node is offlined. Caller must
1904698b1b30SVlastimil Babka  * hold mem_hotplug_begin/end().
1905698b1b30SVlastimil Babka  */
1906698b1b30SVlastimil Babka void kcompactd_stop(int nid)
1907698b1b30SVlastimil Babka {
1908698b1b30SVlastimil Babka 	struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
1909698b1b30SVlastimil Babka 
1910698b1b30SVlastimil Babka 	if (kcompactd) {
1911698b1b30SVlastimil Babka 		kthread_stop(kcompactd);
1912698b1b30SVlastimil Babka 		NODE_DATA(nid)->kcompactd = NULL;
1913698b1b30SVlastimil Babka 	}
1914698b1b30SVlastimil Babka }
1915698b1b30SVlastimil Babka 
1916698b1b30SVlastimil Babka /*
1917698b1b30SVlastimil Babka  * It's optimal to keep kcompactd on the same CPUs as their memory, but
1918698b1b30SVlastimil Babka  * not required for correctness. So if the last cpu in a node goes
1919698b1b30SVlastimil Babka  * away, we get changed to run anywhere: as the first one comes back,
1920698b1b30SVlastimil Babka  * restore their cpu bindings.
1921698b1b30SVlastimil Babka  */
1922698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action,
1923698b1b30SVlastimil Babka 			void *hcpu)
1924698b1b30SVlastimil Babka {
1925698b1b30SVlastimil Babka 	int nid;
1926698b1b30SVlastimil Babka 
1927698b1b30SVlastimil Babka 	if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
1928698b1b30SVlastimil Babka 		for_each_node_state(nid, N_MEMORY) {
1929698b1b30SVlastimil Babka 			pg_data_t *pgdat = NODE_DATA(nid);
1930698b1b30SVlastimil Babka 			const struct cpumask *mask;
1931698b1b30SVlastimil Babka 
1932698b1b30SVlastimil Babka 			mask = cpumask_of_node(pgdat->node_id);
1933698b1b30SVlastimil Babka 
1934698b1b30SVlastimil Babka 			if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
1935698b1b30SVlastimil Babka 				/* One of our CPUs online: restore mask */
1936698b1b30SVlastimil Babka 				set_cpus_allowed_ptr(pgdat->kcompactd, mask);
1937698b1b30SVlastimil Babka 		}
1938698b1b30SVlastimil Babka 	}
1939698b1b30SVlastimil Babka 	return NOTIFY_OK;
1940698b1b30SVlastimil Babka }
1941698b1b30SVlastimil Babka 
1942698b1b30SVlastimil Babka static int __init kcompactd_init(void)
1943698b1b30SVlastimil Babka {
1944698b1b30SVlastimil Babka 	int nid;
1945698b1b30SVlastimil Babka 
1946698b1b30SVlastimil Babka 	for_each_node_state(nid, N_MEMORY)
1947698b1b30SVlastimil Babka 		kcompactd_run(nid);
1948698b1b30SVlastimil Babka 	hotcpu_notifier(cpu_callback, 0);
1949698b1b30SVlastimil Babka 	return 0;
1950698b1b30SVlastimil Babka }
1951698b1b30SVlastimil Babka subsys_initcall(kcompactd_init)
1952698b1b30SVlastimil Babka 
1953ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
1954