xref: /linux/mm/compaction.c (revision 6d7ce55940b6ecd463ca044ad241f0122d913293)
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  */
10748446bbSMel Gorman #include <linux/swap.h>
11748446bbSMel Gorman #include <linux/migrate.h>
12748446bbSMel Gorman #include <linux/compaction.h>
13748446bbSMel Gorman #include <linux/mm_inline.h>
14748446bbSMel Gorman #include <linux/backing-dev.h>
1576ab0f53SMel Gorman #include <linux/sysctl.h>
16ed4a6d7fSMel Gorman #include <linux/sysfs.h>
17bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h>
18194159fbSMinchan Kim #include <linux/page-isolation.h>
19748446bbSMel Gorman #include "internal.h"
20748446bbSMel Gorman 
21010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION
22010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item)
23010fc29aSMinchan Kim {
24010fc29aSMinchan Kim 	count_vm_event(item);
25010fc29aSMinchan Kim }
26010fc29aSMinchan Kim 
27010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta)
28010fc29aSMinchan Kim {
29010fc29aSMinchan Kim 	count_vm_events(item, delta);
30010fc29aSMinchan Kim }
31010fc29aSMinchan Kim #else
32010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0)
33010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0)
34010fc29aSMinchan Kim #endif
35010fc29aSMinchan Kim 
36ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
37ff9543fdSMichal Nazarewicz 
38b7aba698SMel Gorman #define CREATE_TRACE_POINTS
39b7aba698SMel Gorman #include <trace/events/compaction.h>
40b7aba698SMel Gorman 
41748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
42748446bbSMel Gorman {
43748446bbSMel Gorman 	struct page *page, *next;
44748446bbSMel Gorman 	unsigned long count = 0;
45748446bbSMel Gorman 
46748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
47748446bbSMel Gorman 		list_del(&page->lru);
48748446bbSMel Gorman 		__free_page(page);
49748446bbSMel Gorman 		count++;
50748446bbSMel Gorman 	}
51748446bbSMel Gorman 
52748446bbSMel Gorman 	return count;
53748446bbSMel Gorman }
54748446bbSMel Gorman 
55ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
56ff9543fdSMichal Nazarewicz {
57ff9543fdSMichal Nazarewicz 	struct page *page;
58ff9543fdSMichal Nazarewicz 
59ff9543fdSMichal Nazarewicz 	list_for_each_entry(page, list, lru) {
60ff9543fdSMichal Nazarewicz 		arch_alloc_page(page, 0);
61ff9543fdSMichal Nazarewicz 		kernel_map_pages(page, 1, 1);
62ff9543fdSMichal Nazarewicz 	}
63ff9543fdSMichal Nazarewicz }
64ff9543fdSMichal Nazarewicz 
6547118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype)
6647118af0SMichal Nazarewicz {
6747118af0SMichal Nazarewicz 	return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
6847118af0SMichal Nazarewicz }
6947118af0SMichal Nazarewicz 
707d49d886SVlastimil Babka /*
717d49d886SVlastimil Babka  * Check that the whole (or subset of) a pageblock given by the interval of
727d49d886SVlastimil Babka  * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
737d49d886SVlastimil Babka  * with the migration of free compaction scanner. The scanners then need to
747d49d886SVlastimil Babka  * use only pfn_valid_within() check for arches that allow holes within
757d49d886SVlastimil Babka  * pageblocks.
767d49d886SVlastimil Babka  *
777d49d886SVlastimil Babka  * Return struct page pointer of start_pfn, or NULL if checks were not passed.
787d49d886SVlastimil Babka  *
797d49d886SVlastimil Babka  * It's possible on some configurations to have a setup like node0 node1 node0
807d49d886SVlastimil Babka  * i.e. it's possible that all pages within a zones range of pages do not
817d49d886SVlastimil Babka  * belong to a single zone. We assume that a border between node0 and node1
827d49d886SVlastimil Babka  * can occur within a single pageblock, but not a node0 node1 node0
837d49d886SVlastimil Babka  * interleaving within a single pageblock. It is therefore sufficient to check
847d49d886SVlastimil Babka  * the first and last page of a pageblock and avoid checking each individual
857d49d886SVlastimil Babka  * page in a pageblock.
867d49d886SVlastimil Babka  */
877d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
887d49d886SVlastimil Babka 				unsigned long end_pfn, struct zone *zone)
897d49d886SVlastimil Babka {
907d49d886SVlastimil Babka 	struct page *start_page;
917d49d886SVlastimil Babka 	struct page *end_page;
927d49d886SVlastimil Babka 
937d49d886SVlastimil Babka 	/* end_pfn is one past the range we are checking */
947d49d886SVlastimil Babka 	end_pfn--;
957d49d886SVlastimil Babka 
967d49d886SVlastimil Babka 	if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
977d49d886SVlastimil Babka 		return NULL;
987d49d886SVlastimil Babka 
997d49d886SVlastimil Babka 	start_page = pfn_to_page(start_pfn);
1007d49d886SVlastimil Babka 
1017d49d886SVlastimil Babka 	if (page_zone(start_page) != zone)
1027d49d886SVlastimil Babka 		return NULL;
1037d49d886SVlastimil Babka 
1047d49d886SVlastimil Babka 	end_page = pfn_to_page(end_pfn);
1057d49d886SVlastimil Babka 
1067d49d886SVlastimil Babka 	/* This gives a shorter code than deriving page_zone(end_page) */
1077d49d886SVlastimil Babka 	if (page_zone_id(start_page) != page_zone_id(end_page))
1087d49d886SVlastimil Babka 		return NULL;
1097d49d886SVlastimil Babka 
1107d49d886SVlastimil Babka 	return start_page;
1117d49d886SVlastimil Babka }
1127d49d886SVlastimil Babka 
113bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
114bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
115bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
116bb13ffebSMel Gorman 					struct page *page)
117bb13ffebSMel Gorman {
118bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
119bb13ffebSMel Gorman 		return true;
120bb13ffebSMel Gorman 
121bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
122bb13ffebSMel Gorman }
123bb13ffebSMel Gorman 
124bb13ffebSMel Gorman /*
125bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
126bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
127bb13ffebSMel Gorman  * meet.
128bb13ffebSMel Gorman  */
12962997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
130bb13ffebSMel Gorman {
131bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
132108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
133bb13ffebSMel Gorman 	unsigned long pfn;
134bb13ffebSMel Gorman 
13535979ef3SDavid Rientjes 	zone->compact_cached_migrate_pfn[0] = start_pfn;
13635979ef3SDavid Rientjes 	zone->compact_cached_migrate_pfn[1] = start_pfn;
137c89511abSMel Gorman 	zone->compact_cached_free_pfn = end_pfn;
13862997027SMel Gorman 	zone->compact_blockskip_flush = false;
139bb13ffebSMel Gorman 
140bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
141bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142bb13ffebSMel Gorman 		struct page *page;
143bb13ffebSMel Gorman 
144bb13ffebSMel Gorman 		cond_resched();
145bb13ffebSMel Gorman 
146bb13ffebSMel Gorman 		if (!pfn_valid(pfn))
147bb13ffebSMel Gorman 			continue;
148bb13ffebSMel Gorman 
149bb13ffebSMel Gorman 		page = pfn_to_page(pfn);
150bb13ffebSMel Gorman 		if (zone != page_zone(page))
151bb13ffebSMel Gorman 			continue;
152bb13ffebSMel Gorman 
153bb13ffebSMel Gorman 		clear_pageblock_skip(page);
154bb13ffebSMel Gorman 	}
155bb13ffebSMel Gorman }
156bb13ffebSMel Gorman 
15762997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
15862997027SMel Gorman {
15962997027SMel Gorman 	int zoneid;
16062997027SMel Gorman 
16162997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
16262997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
16362997027SMel Gorman 		if (!populated_zone(zone))
16462997027SMel Gorman 			continue;
16562997027SMel Gorman 
16662997027SMel Gorman 		/* Only flush if a full compaction finished recently */
16762997027SMel Gorman 		if (zone->compact_blockskip_flush)
16862997027SMel Gorman 			__reset_isolation_suitable(zone);
16962997027SMel Gorman 	}
17062997027SMel Gorman }
17162997027SMel Gorman 
172bb13ffebSMel Gorman /*
173bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
17462997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
175bb13ffebSMel Gorman  */
176c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
177c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
178edc2ca61SVlastimil Babka 			bool migrate_scanner)
179bb13ffebSMel Gorman {
180c89511abSMel Gorman 	struct zone *zone = cc->zone;
18135979ef3SDavid Rientjes 	unsigned long pfn;
1826815bf3fSJoonsoo Kim 
1836815bf3fSJoonsoo Kim 	if (cc->ignore_skip_hint)
1846815bf3fSJoonsoo Kim 		return;
1856815bf3fSJoonsoo Kim 
186bb13ffebSMel Gorman 	if (!page)
187bb13ffebSMel Gorman 		return;
188bb13ffebSMel Gorman 
18935979ef3SDavid Rientjes 	if (nr_isolated)
19035979ef3SDavid Rientjes 		return;
19135979ef3SDavid Rientjes 
192bb13ffebSMel Gorman 	set_pageblock_skip(page);
193c89511abSMel Gorman 
19435979ef3SDavid Rientjes 	pfn = page_to_pfn(page);
19535979ef3SDavid Rientjes 
19635979ef3SDavid Rientjes 	/* Update where async and sync compaction should restart */
197c89511abSMel Gorman 	if (migrate_scanner) {
19835979ef3SDavid Rientjes 		if (cc->finished_update_migrate)
19935979ef3SDavid Rientjes 			return;
20035979ef3SDavid Rientjes 		if (pfn > zone->compact_cached_migrate_pfn[0])
20135979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[0] = pfn;
202e0b9daebSDavid Rientjes 		if (cc->mode != MIGRATE_ASYNC &&
203e0b9daebSDavid Rientjes 		    pfn > zone->compact_cached_migrate_pfn[1])
20435979ef3SDavid Rientjes 			zone->compact_cached_migrate_pfn[1] = pfn;
205c89511abSMel Gorman 	} else {
20635979ef3SDavid Rientjes 		if (cc->finished_update_free)
20735979ef3SDavid Rientjes 			return;
20835979ef3SDavid Rientjes 		if (pfn < zone->compact_cached_free_pfn)
209c89511abSMel Gorman 			zone->compact_cached_free_pfn = pfn;
210c89511abSMel Gorman 	}
211c89511abSMel Gorman }
212bb13ffebSMel Gorman #else
213bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
214bb13ffebSMel Gorman 					struct page *page)
215bb13ffebSMel Gorman {
216bb13ffebSMel Gorman 	return true;
217bb13ffebSMel Gorman }
218bb13ffebSMel Gorman 
219c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
220c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
221edc2ca61SVlastimil Babka 			bool migrate_scanner)
222bb13ffebSMel Gorman {
223bb13ffebSMel Gorman }
224bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
225bb13ffebSMel Gorman 
2261f9efdefSVlastimil Babka /*
2278b44d279SVlastimil Babka  * Compaction requires the taking of some coarse locks that are potentially
2288b44d279SVlastimil Babka  * very heavily contended. For async compaction, back out if the lock cannot
2298b44d279SVlastimil Babka  * be taken immediately. For sync compaction, spin on the lock if needed.
2308b44d279SVlastimil Babka  *
2318b44d279SVlastimil Babka  * Returns true if the lock is held
2328b44d279SVlastimil Babka  * Returns false if the lock is not held and compaction should abort
2331f9efdefSVlastimil Babka  */
2348b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
2358b44d279SVlastimil Babka 						struct compact_control *cc)
2368b44d279SVlastimil Babka {
2378b44d279SVlastimil Babka 	if (cc->mode == MIGRATE_ASYNC) {
2388b44d279SVlastimil Babka 		if (!spin_trylock_irqsave(lock, *flags)) {
2398b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_LOCK;
2408b44d279SVlastimil Babka 			return false;
2418b44d279SVlastimil Babka 		}
2428b44d279SVlastimil Babka 	} else {
2438b44d279SVlastimil Babka 		spin_lock_irqsave(lock, *flags);
2448b44d279SVlastimil Babka 	}
2451f9efdefSVlastimil Babka 
2468b44d279SVlastimil Babka 	return true;
2472a1402aaSMel Gorman }
2482a1402aaSMel Gorman 
24985aa125fSMichal Nazarewicz /*
250c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
2518b44d279SVlastimil Babka  * very heavily contended. The lock should be periodically unlocked to avoid
2528b44d279SVlastimil Babka  * having disabled IRQs for a long time, even when there is nobody waiting on
2538b44d279SVlastimil Babka  * the lock. It might also be that allowing the IRQs will result in
2548b44d279SVlastimil Babka  * need_resched() becoming true. If scheduling is needed, async compaction
2558b44d279SVlastimil Babka  * aborts. Sync compaction schedules.
2568b44d279SVlastimil Babka  * Either compaction type will also abort if a fatal signal is pending.
2578b44d279SVlastimil Babka  * In either case if the lock was locked, it is dropped and not regained.
258c67fe375SMel Gorman  *
2598b44d279SVlastimil Babka  * Returns true if compaction should abort due to fatal signal pending, or
2608b44d279SVlastimil Babka  *		async compaction due to need_resched()
2618b44d279SVlastimil Babka  * Returns false when compaction can continue (sync compaction might have
2628b44d279SVlastimil Babka  *		scheduled)
263c67fe375SMel Gorman  */
2648b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock,
2658b44d279SVlastimil Babka 		unsigned long flags, bool *locked, struct compact_control *cc)
266c67fe375SMel Gorman {
2678b44d279SVlastimil Babka 	if (*locked) {
2688b44d279SVlastimil Babka 		spin_unlock_irqrestore(lock, flags);
2698b44d279SVlastimil Babka 		*locked = false;
270c67fe375SMel Gorman 	}
271c67fe375SMel Gorman 
2728b44d279SVlastimil Babka 	if (fatal_signal_pending(current)) {
2738b44d279SVlastimil Babka 		cc->contended = COMPACT_CONTENDED_SCHED;
2748b44d279SVlastimil Babka 		return true;
2758b44d279SVlastimil Babka 	}
2768b44d279SVlastimil Babka 
2778b44d279SVlastimil Babka 	if (need_resched()) {
278e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC) {
2798b44d279SVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
2808b44d279SVlastimil Babka 			return true;
281c67fe375SMel Gorman 		}
282c67fe375SMel Gorman 		cond_resched();
283c67fe375SMel Gorman 	}
284c67fe375SMel Gorman 
2858b44d279SVlastimil Babka 	return false;
286c67fe375SMel Gorman }
287c67fe375SMel Gorman 
288be976572SVlastimil Babka /*
289be976572SVlastimil Babka  * Aside from avoiding lock contention, compaction also periodically checks
290be976572SVlastimil Babka  * need_resched() and either schedules in sync compaction or aborts async
2918b44d279SVlastimil Babka  * compaction. This is similar to what compact_unlock_should_abort() does, but
292be976572SVlastimil Babka  * is used where no lock is concerned.
293be976572SVlastimil Babka  *
294be976572SVlastimil Babka  * Returns false when no scheduling was needed, or sync compaction scheduled.
295be976572SVlastimil Babka  * Returns true when async compaction should abort.
296be976572SVlastimil Babka  */
297be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc)
298be976572SVlastimil Babka {
299be976572SVlastimil Babka 	/* async compaction aborts if contended */
300be976572SVlastimil Babka 	if (need_resched()) {
301be976572SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC) {
3021f9efdefSVlastimil Babka 			cc->contended = COMPACT_CONTENDED_SCHED;
303be976572SVlastimil Babka 			return true;
304be976572SVlastimil Babka 		}
305be976572SVlastimil Babka 
306be976572SVlastimil Babka 		cond_resched();
307be976572SVlastimil Babka 	}
308be976572SVlastimil Babka 
309be976572SVlastimil Babka 	return false;
310be976572SVlastimil Babka }
311be976572SVlastimil Babka 
312f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */
313f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page)
314f40d1e42SMel Gorman {
3157d348b9eSJoonsoo Kim 	/* If the page is a large free page, then disallow migration */
31699c0fd5eSVlastimil Babka 	if (PageBuddy(page)) {
31799c0fd5eSVlastimil Babka 		/*
31899c0fd5eSVlastimil Babka 		 * We are checking page_order without zone->lock taken. But
31999c0fd5eSVlastimil Babka 		 * the only small danger is that we skip a potentially suitable
32099c0fd5eSVlastimil Babka 		 * pageblock, so it's not worth to check order for valid range.
32199c0fd5eSVlastimil Babka 		 */
32299c0fd5eSVlastimil Babka 		if (page_order_unsafe(page) >= pageblock_order)
3237d348b9eSJoonsoo Kim 			return false;
32499c0fd5eSVlastimil Babka 	}
325f40d1e42SMel Gorman 
326f40d1e42SMel Gorman 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
3277d348b9eSJoonsoo Kim 	if (migrate_async_suitable(get_pageblock_migratetype(page)))
328f40d1e42SMel Gorman 		return true;
329f40d1e42SMel Gorman 
330f40d1e42SMel Gorman 	/* Otherwise skip the block */
331f40d1e42SMel Gorman 	return false;
332f40d1e42SMel Gorman }
333f40d1e42SMel Gorman 
334c67fe375SMel Gorman /*
3359e4be470SJerome Marchand  * Isolate free pages onto a private freelist. If @strict is true, will abort
3369e4be470SJerome Marchand  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
3379e4be470SJerome Marchand  * (even though it may still end up isolating some pages).
33885aa125fSMichal Nazarewicz  */
339f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
340e14c720eSVlastimil Babka 				unsigned long *start_pfn,
34185aa125fSMichal Nazarewicz 				unsigned long end_pfn,
34285aa125fSMichal Nazarewicz 				struct list_head *freelist,
34385aa125fSMichal Nazarewicz 				bool strict)
344748446bbSMel Gorman {
345b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
346bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
347f40d1e42SMel Gorman 	unsigned long flags;
348f40d1e42SMel Gorman 	bool locked = false;
349e14c720eSVlastimil Babka 	unsigned long blockpfn = *start_pfn;
350748446bbSMel Gorman 
351748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
352748446bbSMel Gorman 
353f40d1e42SMel Gorman 	/* Isolate free pages. */
354748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
355748446bbSMel Gorman 		int isolated, i;
356748446bbSMel Gorman 		struct page *page = cursor;
357748446bbSMel Gorman 
3588b44d279SVlastimil Babka 		/*
3598b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
3608b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort if fatal signal
3618b44d279SVlastimil Babka 		 * pending or async compaction detects need_resched()
3628b44d279SVlastimil Babka 		 */
3638b44d279SVlastimil Babka 		if (!(blockpfn % SWAP_CLUSTER_MAX)
3648b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&cc->zone->lock, flags,
3658b44d279SVlastimil Babka 								&locked, cc))
3668b44d279SVlastimil Babka 			break;
3678b44d279SVlastimil Babka 
368b7aba698SMel Gorman 		nr_scanned++;
369f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
3702af120bcSLaura Abbott 			goto isolate_fail;
3712af120bcSLaura Abbott 
372bb13ffebSMel Gorman 		if (!valid_page)
373bb13ffebSMel Gorman 			valid_page = page;
374f40d1e42SMel Gorman 		if (!PageBuddy(page))
3752af120bcSLaura Abbott 			goto isolate_fail;
376f40d1e42SMel Gorman 
377f40d1e42SMel Gorman 		/*
37869b7189fSVlastimil Babka 		 * If we already hold the lock, we can skip some rechecking.
37969b7189fSVlastimil Babka 		 * Note that if we hold the lock now, checked_pageblock was
38069b7189fSVlastimil Babka 		 * already set in some previous iteration (or strict is true),
38169b7189fSVlastimil Babka 		 * so it is correct to skip the suitable migration target
38269b7189fSVlastimil Babka 		 * recheck as well.
38369b7189fSVlastimil Babka 		 */
38469b7189fSVlastimil Babka 		if (!locked) {
38569b7189fSVlastimil Babka 			/*
386f40d1e42SMel Gorman 			 * The zone lock must be held to isolate freepages.
387f40d1e42SMel Gorman 			 * Unfortunately this is a very coarse lock and can be
388f40d1e42SMel Gorman 			 * heavily contended if there are parallel allocations
389f40d1e42SMel Gorman 			 * or parallel compactions. For async compaction do not
390f40d1e42SMel Gorman 			 * spin on the lock and we acquire the lock as late as
391f40d1e42SMel Gorman 			 * possible.
392f40d1e42SMel Gorman 			 */
3938b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&cc->zone->lock,
3948b44d279SVlastimil Babka 								&flags, cc);
395f40d1e42SMel Gorman 			if (!locked)
396f40d1e42SMel Gorman 				break;
397f40d1e42SMel Gorman 
398f40d1e42SMel Gorman 			/* Recheck this is a buddy page under lock */
399f40d1e42SMel Gorman 			if (!PageBuddy(page))
4002af120bcSLaura Abbott 				goto isolate_fail;
40169b7189fSVlastimil Babka 		}
402748446bbSMel Gorman 
403748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
404748446bbSMel Gorman 		isolated = split_free_page(page);
405748446bbSMel Gorman 		total_isolated += isolated;
406748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
407748446bbSMel Gorman 			list_add(&page->lru, freelist);
408748446bbSMel Gorman 			page++;
409748446bbSMel Gorman 		}
410748446bbSMel Gorman 
411748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
412748446bbSMel Gorman 		if (isolated) {
413748446bbSMel Gorman 			blockpfn += isolated - 1;
414748446bbSMel Gorman 			cursor += isolated - 1;
4152af120bcSLaura Abbott 			continue;
416748446bbSMel Gorman 		}
4172af120bcSLaura Abbott 
4182af120bcSLaura Abbott isolate_fail:
4192af120bcSLaura Abbott 		if (strict)
4202af120bcSLaura Abbott 			break;
4212af120bcSLaura Abbott 		else
4222af120bcSLaura Abbott 			continue;
4232af120bcSLaura Abbott 
424748446bbSMel Gorman 	}
425748446bbSMel Gorman 
426e14c720eSVlastimil Babka 	/* Record how far we have got within the block */
427e14c720eSVlastimil Babka 	*start_pfn = blockpfn;
428e14c720eSVlastimil Babka 
429b7aba698SMel Gorman 	trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
430f40d1e42SMel Gorman 
431f40d1e42SMel Gorman 	/*
432f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
433f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
434f40d1e42SMel Gorman 	 * returned and CMA will fail.
435f40d1e42SMel Gorman 	 */
4362af120bcSLaura Abbott 	if (strict && blockpfn < end_pfn)
437f40d1e42SMel Gorman 		total_isolated = 0;
438f40d1e42SMel Gorman 
439f40d1e42SMel Gorman 	if (locked)
440f40d1e42SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
441f40d1e42SMel Gorman 
442bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
443bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
444edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, total_isolated, false);
445bb13ffebSMel Gorman 
446010fc29aSMinchan Kim 	count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
447397487dbSMel Gorman 	if (total_isolated)
448010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
449748446bbSMel Gorman 	return total_isolated;
450748446bbSMel Gorman }
451748446bbSMel Gorman 
45285aa125fSMichal Nazarewicz /**
45385aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
45485aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
45585aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
45685aa125fSMichal Nazarewicz  *
45785aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
45885aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
45985aa125fSMichal Nazarewicz  * undo its actions and return zero.
46085aa125fSMichal Nazarewicz  *
46185aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
46285aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
46385aa125fSMichal Nazarewicz  * a free page).
46485aa125fSMichal Nazarewicz  */
465ff9543fdSMichal Nazarewicz unsigned long
466bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
467bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
46885aa125fSMichal Nazarewicz {
469f40d1e42SMel Gorman 	unsigned long isolated, pfn, block_end_pfn;
47085aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
47185aa125fSMichal Nazarewicz 
4727d49d886SVlastimil Babka 	pfn = start_pfn;
47385aa125fSMichal Nazarewicz 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
4747d49d886SVlastimil Babka 
4757d49d886SVlastimil Babka 	for (; pfn < end_pfn; pfn += isolated,
4767d49d886SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
477e14c720eSVlastimil Babka 		/* Protect pfn from changing by isolate_freepages_block */
478e14c720eSVlastimil Babka 		unsigned long isolate_start_pfn = pfn;
4797d49d886SVlastimil Babka 
48085aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
48185aa125fSMichal Nazarewicz 
4827d49d886SVlastimil Babka 		if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
4837d49d886SVlastimil Babka 			break;
4847d49d886SVlastimil Babka 
485e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
486e14c720eSVlastimil Babka 						block_end_pfn, &freelist, true);
48785aa125fSMichal Nazarewicz 
48885aa125fSMichal Nazarewicz 		/*
48985aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
49085aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
49185aa125fSMichal Nazarewicz 		 * non-free pages).
49285aa125fSMichal Nazarewicz 		 */
49385aa125fSMichal Nazarewicz 		if (!isolated)
49485aa125fSMichal Nazarewicz 			break;
49585aa125fSMichal Nazarewicz 
49685aa125fSMichal Nazarewicz 		/*
49785aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
49885aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
49985aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
50085aa125fSMichal Nazarewicz 		 */
50185aa125fSMichal Nazarewicz 	}
50285aa125fSMichal Nazarewicz 
50385aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
50485aa125fSMichal Nazarewicz 	map_pages(&freelist);
50585aa125fSMichal Nazarewicz 
50685aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
50785aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
50885aa125fSMichal Nazarewicz 		release_freepages(&freelist);
50985aa125fSMichal Nazarewicz 		return 0;
51085aa125fSMichal Nazarewicz 	}
51185aa125fSMichal Nazarewicz 
51285aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
51385aa125fSMichal Nazarewicz 	return pfn;
51485aa125fSMichal Nazarewicz }
51585aa125fSMichal Nazarewicz 
516748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
517edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc)
518748446bbSMel Gorman {
519748446bbSMel Gorman 	struct page *page;
520b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
521748446bbSMel Gorman 
522edc2ca61SVlastimil Babka 	if (list_empty(&cc->migratepages))
523edc2ca61SVlastimil Babka 		return;
524edc2ca61SVlastimil Babka 
525b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
526b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
527748446bbSMel Gorman 
528c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
529c67fe375SMel Gorman 	mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
530c67fe375SMel Gorman }
531748446bbSMel Gorman 
532748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
533748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
534748446bbSMel Gorman {
535bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
536748446bbSMel Gorman 
537748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
538748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
539bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
540bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
541748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
542748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
543748446bbSMel Gorman 
544bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
545748446bbSMel Gorman }
546748446bbSMel Gorman 
5472fe86e00SMichal Nazarewicz /**
548edc2ca61SVlastimil Babka  * isolate_migratepages_block() - isolate all migrate-able pages within
549edc2ca61SVlastimil Babka  *				  a single pageblock
5502fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
551edc2ca61SVlastimil Babka  * @low_pfn:	The first PFN to isolate
552edc2ca61SVlastimil Babka  * @end_pfn:	The one-past-the-last PFN to isolate, within same pageblock
553edc2ca61SVlastimil Babka  * @isolate_mode: Isolation mode to be used.
5542fe86e00SMichal Nazarewicz  *
5552fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
556edc2ca61SVlastimil Babka  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
557edc2ca61SVlastimil Babka  * Returns zero if there is a fatal signal pending, otherwise PFN of the
558edc2ca61SVlastimil Babka  * first page that was not scanned (which may be both less, equal to or more
559edc2ca61SVlastimil Babka  * than end_pfn).
5602fe86e00SMichal Nazarewicz  *
561edc2ca61SVlastimil Babka  * The pages are isolated on cc->migratepages list (not required to be empty),
562edc2ca61SVlastimil Babka  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
563edc2ca61SVlastimil Babka  * is neither read nor updated.
564748446bbSMel Gorman  */
565edc2ca61SVlastimil Babka static unsigned long
566edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
567edc2ca61SVlastimil Babka 			unsigned long end_pfn, isolate_mode_t isolate_mode)
568748446bbSMel Gorman {
569edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
570b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
571748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
572fa9add64SHugh Dickins 	struct lruvec *lruvec;
573c67fe375SMel Gorman 	unsigned long flags;
5742a1402aaSMel Gorman 	bool locked = false;
575bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
576748446bbSMel Gorman 
577748446bbSMel Gorman 	/*
578748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
579748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
580748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
581748446bbSMel Gorman 	 */
582748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
583f9e35b3bSMel Gorman 		/* async migration should just abort */
584e0b9daebSDavid Rientjes 		if (cc->mode == MIGRATE_ASYNC)
5852fe86e00SMichal Nazarewicz 			return 0;
586f9e35b3bSMel Gorman 
587748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
588748446bbSMel Gorman 
589748446bbSMel Gorman 		if (fatal_signal_pending(current))
5902fe86e00SMichal Nazarewicz 			return 0;
591748446bbSMel Gorman 	}
592748446bbSMel Gorman 
593be976572SVlastimil Babka 	if (compact_should_abort(cc))
594aeef4b83SDavid Rientjes 		return 0;
595aeef4b83SDavid Rientjes 
596748446bbSMel Gorman 	/* Time to isolate some pages for migration */
597748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
5988b44d279SVlastimil Babka 		/*
5998b44d279SVlastimil Babka 		 * Periodically drop the lock (if held) regardless of its
6008b44d279SVlastimil Babka 		 * contention, to give chance to IRQs. Abort async compaction
6018b44d279SVlastimil Babka 		 * if contended.
6028b44d279SVlastimil Babka 		 */
6038b44d279SVlastimil Babka 		if (!(low_pfn % SWAP_CLUSTER_MAX)
6048b44d279SVlastimil Babka 		    && compact_unlock_should_abort(&zone->lru_lock, flags,
6058b44d279SVlastimil Babka 								&locked, cc))
6068b44d279SVlastimil Babka 			break;
607b2eef8c0SAndrea Arcangeli 
608748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
609748446bbSMel Gorman 			continue;
610b7aba698SMel Gorman 		nr_scanned++;
611748446bbSMel Gorman 
612748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
613dc908600SMel Gorman 
614bb13ffebSMel Gorman 		if (!valid_page)
615bb13ffebSMel Gorman 			valid_page = page;
616bb13ffebSMel Gorman 
617c122b208SJoonsoo Kim 		/*
61899c0fd5eSVlastimil Babka 		 * Skip if free. We read page order here without zone lock
61999c0fd5eSVlastimil Babka 		 * which is generally unsafe, but the race window is small and
62099c0fd5eSVlastimil Babka 		 * the worst thing that can happen is that we skip some
62199c0fd5eSVlastimil Babka 		 * potential isolation targets.
6226c14466cSMel Gorman 		 */
62399c0fd5eSVlastimil Babka 		if (PageBuddy(page)) {
62499c0fd5eSVlastimil Babka 			unsigned long freepage_order = page_order_unsafe(page);
62599c0fd5eSVlastimil Babka 
62699c0fd5eSVlastimil Babka 			/*
62799c0fd5eSVlastimil Babka 			 * Without lock, we cannot be sure that what we got is
62899c0fd5eSVlastimil Babka 			 * a valid page order. Consider only values in the
62999c0fd5eSVlastimil Babka 			 * valid order range to prevent low_pfn overflow.
63099c0fd5eSVlastimil Babka 			 */
63199c0fd5eSVlastimil Babka 			if (freepage_order > 0 && freepage_order < MAX_ORDER)
63299c0fd5eSVlastimil Babka 				low_pfn += (1UL << freepage_order) - 1;
633748446bbSMel Gorman 			continue;
63499c0fd5eSVlastimil Babka 		}
635748446bbSMel Gorman 
6369927af74SMel Gorman 		/*
637bf6bddf1SRafael Aquini 		 * Check may be lockless but that's ok as we recheck later.
638bf6bddf1SRafael Aquini 		 * It's possible to migrate LRU pages and balloon pages
639bf6bddf1SRafael Aquini 		 * Skip any other type of page
640bf6bddf1SRafael Aquini 		 */
641bf6bddf1SRafael Aquini 		if (!PageLRU(page)) {
642bf6bddf1SRafael Aquini 			if (unlikely(balloon_page_movable(page))) {
643bf6bddf1SRafael Aquini 				if (locked && balloon_page_isolate(page)) {
644bf6bddf1SRafael Aquini 					/* Successfully isolated */
645b6c75016SJoonsoo Kim 					goto isolate_success;
646bf6bddf1SRafael Aquini 				}
647bf6bddf1SRafael Aquini 			}
648bc835011SAndrea Arcangeli 			continue;
649bf6bddf1SRafael Aquini 		}
650bc835011SAndrea Arcangeli 
651bc835011SAndrea Arcangeli 		/*
6522a1402aaSMel Gorman 		 * PageLRU is set. lru_lock normally excludes isolation
6532a1402aaSMel Gorman 		 * splitting and collapsing (collapsing has already happened
6542a1402aaSMel Gorman 		 * if PageLRU is set) but the lock is not necessarily taken
6552a1402aaSMel Gorman 		 * here and it is wasteful to take it just to check transhuge.
6562a1402aaSMel Gorman 		 * Check TransHuge without lock and skip the whole pageblock if
6572a1402aaSMel Gorman 		 * it's either a transhuge or hugetlbfs page, as calling
6582a1402aaSMel Gorman 		 * compound_order() without preventing THP from splitting the
6592a1402aaSMel Gorman 		 * page underneath us may return surprising results.
660bc835011SAndrea Arcangeli 		 */
661bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
6622a1402aaSMel Gorman 			if (!locked)
663edc2ca61SVlastimil Babka 				low_pfn = ALIGN(low_pfn + 1,
664edc2ca61SVlastimil Babka 						pageblock_nr_pages) - 1;
665edc2ca61SVlastimil Babka 			else
6662a1402aaSMel Gorman 				low_pfn += (1 << compound_order(page)) - 1;
667edc2ca61SVlastimil Babka 
6682a1402aaSMel Gorman 			continue;
6692a1402aaSMel Gorman 		}
6702a1402aaSMel Gorman 
671119d6d59SDavid Rientjes 		/*
672119d6d59SDavid Rientjes 		 * Migration will fail if an anonymous page is pinned in memory,
673119d6d59SDavid Rientjes 		 * so avoid taking lru_lock and isolating it unnecessarily in an
674119d6d59SDavid Rientjes 		 * admittedly racy check.
675119d6d59SDavid Rientjes 		 */
676119d6d59SDavid Rientjes 		if (!page_mapping(page) &&
677119d6d59SDavid Rientjes 		    page_count(page) > page_mapcount(page))
678119d6d59SDavid Rientjes 			continue;
679119d6d59SDavid Rientjes 
68069b7189fSVlastimil Babka 		/* If we already hold the lock, we can skip some rechecking */
68169b7189fSVlastimil Babka 		if (!locked) {
6828b44d279SVlastimil Babka 			locked = compact_trylock_irqsave(&zone->lru_lock,
6838b44d279SVlastimil Babka 								&flags, cc);
6848b44d279SVlastimil Babka 			if (!locked)
6852a1402aaSMel Gorman 				break;
6862a1402aaSMel Gorman 
6872a1402aaSMel Gorman 			/* Recheck PageLRU and PageTransHuge under lock */
6882a1402aaSMel Gorman 			if (!PageLRU(page))
6892a1402aaSMel Gorman 				continue;
6902a1402aaSMel Gorman 			if (PageTransHuge(page)) {
691bc835011SAndrea Arcangeli 				low_pfn += (1 << compound_order(page)) - 1;
692bc835011SAndrea Arcangeli 				continue;
693bc835011SAndrea Arcangeli 			}
69469b7189fSVlastimil Babka 		}
695bc835011SAndrea Arcangeli 
696fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
697fa9add64SHugh Dickins 
698748446bbSMel Gorman 		/* Try isolate the page */
699edc2ca61SVlastimil Babka 		if (__isolate_lru_page(page, isolate_mode) != 0)
700748446bbSMel Gorman 			continue;
701748446bbSMel Gorman 
702309381feSSasha Levin 		VM_BUG_ON_PAGE(PageTransCompound(page), page);
703bc835011SAndrea Arcangeli 
704748446bbSMel Gorman 		/* Successfully isolated */
705fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
706b6c75016SJoonsoo Kim 
707b6c75016SJoonsoo Kim isolate_success:
708b6c75016SJoonsoo Kim 		cc->finished_update_migrate = true;
709748446bbSMel Gorman 		list_add(&page->lru, migratelist);
710748446bbSMel Gorman 		cc->nr_migratepages++;
711b7aba698SMel Gorman 		nr_isolated++;
712748446bbSMel Gorman 
713748446bbSMel Gorman 		/* Avoid isolating too much */
71431b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
71531b8384aSHillf Danton 			++low_pfn;
716748446bbSMel Gorman 			break;
717748446bbSMel Gorman 		}
71831b8384aSHillf Danton 	}
719748446bbSMel Gorman 
72099c0fd5eSVlastimil Babka 	/*
72199c0fd5eSVlastimil Babka 	 * The PageBuddy() check could have potentially brought us outside
72299c0fd5eSVlastimil Babka 	 * the range to be scanned.
72399c0fd5eSVlastimil Babka 	 */
72499c0fd5eSVlastimil Babka 	if (unlikely(low_pfn > end_pfn))
72599c0fd5eSVlastimil Babka 		low_pfn = end_pfn;
72699c0fd5eSVlastimil Babka 
727c67fe375SMel Gorman 	if (locked)
728c67fe375SMel Gorman 		spin_unlock_irqrestore(&zone->lru_lock, flags);
729748446bbSMel Gorman 
73050b5b094SVlastimil Babka 	/*
73150b5b094SVlastimil Babka 	 * Update the pageblock-skip information and cached scanner pfn,
73250b5b094SVlastimil Babka 	 * if the whole pageblock was scanned without isolating any page.
73350b5b094SVlastimil Babka 	 */
73435979ef3SDavid Rientjes 	if (low_pfn == end_pfn)
735edc2ca61SVlastimil Babka 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
736bb13ffebSMel Gorman 
737b7aba698SMel Gorman 	trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
738b7aba698SMel Gorman 
739010fc29aSMinchan Kim 	count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
740397487dbSMel Gorman 	if (nr_isolated)
741010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
742397487dbSMel Gorman 
7432fe86e00SMichal Nazarewicz 	return low_pfn;
7442fe86e00SMichal Nazarewicz }
7452fe86e00SMichal Nazarewicz 
746edc2ca61SVlastimil Babka /**
747edc2ca61SVlastimil Babka  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
748edc2ca61SVlastimil Babka  * @cc:        Compaction control structure.
749edc2ca61SVlastimil Babka  * @start_pfn: The first PFN to start isolating.
750edc2ca61SVlastimil Babka  * @end_pfn:   The one-past-last PFN.
751edc2ca61SVlastimil Babka  *
752edc2ca61SVlastimil Babka  * Returns zero if isolation fails fatally due to e.g. pending signal.
753edc2ca61SVlastimil Babka  * Otherwise, function returns one-past-the-last PFN of isolated page
754edc2ca61SVlastimil Babka  * (which may be greater than end_pfn if end fell in a middle of a THP page).
755edc2ca61SVlastimil Babka  */
756edc2ca61SVlastimil Babka unsigned long
757edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
758edc2ca61SVlastimil Babka 							unsigned long end_pfn)
759edc2ca61SVlastimil Babka {
760edc2ca61SVlastimil Babka 	unsigned long pfn, block_end_pfn;
761edc2ca61SVlastimil Babka 
762edc2ca61SVlastimil Babka 	/* Scan block by block. First and last block may be incomplete */
763edc2ca61SVlastimil Babka 	pfn = start_pfn;
764edc2ca61SVlastimil Babka 	block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
765edc2ca61SVlastimil Babka 
766edc2ca61SVlastimil Babka 	for (; pfn < end_pfn; pfn = block_end_pfn,
767edc2ca61SVlastimil Babka 				block_end_pfn += pageblock_nr_pages) {
768edc2ca61SVlastimil Babka 
769edc2ca61SVlastimil Babka 		block_end_pfn = min(block_end_pfn, end_pfn);
770edc2ca61SVlastimil Babka 
7717d49d886SVlastimil Babka 		if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
772edc2ca61SVlastimil Babka 			continue;
773edc2ca61SVlastimil Babka 
774edc2ca61SVlastimil Babka 		pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
775edc2ca61SVlastimil Babka 							ISOLATE_UNEVICTABLE);
776edc2ca61SVlastimil Babka 
777edc2ca61SVlastimil Babka 		/*
778edc2ca61SVlastimil Babka 		 * In case of fatal failure, release everything that might
779edc2ca61SVlastimil Babka 		 * have been isolated in the previous iteration, and signal
780edc2ca61SVlastimil Babka 		 * the failure back to caller.
781edc2ca61SVlastimil Babka 		 */
782edc2ca61SVlastimil Babka 		if (!pfn) {
783edc2ca61SVlastimil Babka 			putback_movable_pages(&cc->migratepages);
784edc2ca61SVlastimil Babka 			cc->nr_migratepages = 0;
785edc2ca61SVlastimil Babka 			break;
786edc2ca61SVlastimil Babka 		}
787edc2ca61SVlastimil Babka 	}
788edc2ca61SVlastimil Babka 	acct_isolated(cc->zone, cc);
789edc2ca61SVlastimil Babka 
790edc2ca61SVlastimil Babka 	return pfn;
791edc2ca61SVlastimil Babka }
792edc2ca61SVlastimil Babka 
793ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
794ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
795ff9543fdSMichal Nazarewicz /*
796ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
797ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
798ff9543fdSMichal Nazarewicz  */
799edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc)
800ff9543fdSMichal Nazarewicz {
801edc2ca61SVlastimil Babka 	struct zone *zone = cc->zone;
802ff9543fdSMichal Nazarewicz 	struct page *page;
803c96b9e50SVlastimil Babka 	unsigned long block_start_pfn;	/* start of current pageblock */
804e14c720eSVlastimil Babka 	unsigned long isolate_start_pfn; /* exact pfn we start at */
805c96b9e50SVlastimil Babka 	unsigned long block_end_pfn;	/* end of current pageblock */
806c96b9e50SVlastimil Babka 	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
807ff9543fdSMichal Nazarewicz 	int nr_freepages = cc->nr_freepages;
808ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
8092fe86e00SMichal Nazarewicz 
810ff9543fdSMichal Nazarewicz 	/*
811ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
81249e068f0SVlastimil Babka 	 * successfully isolated from, zone-cached value, or the end of the
813e14c720eSVlastimil Babka 	 * zone when isolating for the first time. For looping we also need
814e14c720eSVlastimil Babka 	 * this pfn aligned down to the pageblock boundary, because we do
815c96b9e50SVlastimil Babka 	 * block_start_pfn -= pageblock_nr_pages in the for loop.
816c96b9e50SVlastimil Babka 	 * For ending point, take care when isolating in last pageblock of a
817c96b9e50SVlastimil Babka 	 * a zone which ends in the middle of a pageblock.
81849e068f0SVlastimil Babka 	 * The low boundary is the end of the pageblock the migration scanner
81949e068f0SVlastimil Babka 	 * is using.
820ff9543fdSMichal Nazarewicz 	 */
821e14c720eSVlastimil Babka 	isolate_start_pfn = cc->free_pfn;
822c96b9e50SVlastimil Babka 	block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
823c96b9e50SVlastimil Babka 	block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
824c96b9e50SVlastimil Babka 						zone_end_pfn(zone));
8257ed695e0SVlastimil Babka 	low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
8262fe86e00SMichal Nazarewicz 
827ff9543fdSMichal Nazarewicz 	/*
828ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
829ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
830ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
831ff9543fdSMichal Nazarewicz 	 */
832c96b9e50SVlastimil Babka 	for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
833c96b9e50SVlastimil Babka 				block_end_pfn = block_start_pfn,
834e14c720eSVlastimil Babka 				block_start_pfn -= pageblock_nr_pages,
835e14c720eSVlastimil Babka 				isolate_start_pfn = block_start_pfn) {
836ff9543fdSMichal Nazarewicz 		unsigned long isolated;
837ff9543fdSMichal Nazarewicz 
838f6ea3adbSDavid Rientjes 		/*
839f6ea3adbSDavid Rientjes 		 * This can iterate a massively long zone without finding any
840f6ea3adbSDavid Rientjes 		 * suitable migration targets, so periodically check if we need
841be976572SVlastimil Babka 		 * to schedule, or even abort async compaction.
842f6ea3adbSDavid Rientjes 		 */
843be976572SVlastimil Babka 		if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
844be976572SVlastimil Babka 						&& compact_should_abort(cc))
845be976572SVlastimil Babka 			break;
846f6ea3adbSDavid Rientjes 
8477d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
8487d49d886SVlastimil Babka 									zone);
8497d49d886SVlastimil Babka 		if (!page)
850ff9543fdSMichal Nazarewicz 			continue;
851ff9543fdSMichal Nazarewicz 
852ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
85368e3e926SLinus Torvalds 		if (!suitable_migration_target(page))
854ff9543fdSMichal Nazarewicz 			continue;
85568e3e926SLinus Torvalds 
856bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
857bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
858bb13ffebSMel Gorman 			continue;
859bb13ffebSMel Gorman 
860e14c720eSVlastimil Babka 		/* Found a block suitable for isolating free pages from. */
861e14c720eSVlastimil Babka 		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
862c96b9e50SVlastimil Babka 					block_end_pfn, freelist, false);
863ff9543fdSMichal Nazarewicz 		nr_freepages += isolated;
864ff9543fdSMichal Nazarewicz 
865ff9543fdSMichal Nazarewicz 		/*
866e14c720eSVlastimil Babka 		 * Remember where the free scanner should restart next time,
867e14c720eSVlastimil Babka 		 * which is where isolate_freepages_block() left off.
868e14c720eSVlastimil Babka 		 * But if it scanned the whole pageblock, isolate_start_pfn
869e14c720eSVlastimil Babka 		 * now points at block_end_pfn, which is the start of the next
870e14c720eSVlastimil Babka 		 * pageblock.
871e14c720eSVlastimil Babka 		 * In that case we will however want to restart at the start
872e14c720eSVlastimil Babka 		 * of the previous pageblock.
873e14c720eSVlastimil Babka 		 */
874e14c720eSVlastimil Babka 		cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
875e14c720eSVlastimil Babka 				isolate_start_pfn :
876e14c720eSVlastimil Babka 				block_start_pfn - pageblock_nr_pages;
877e14c720eSVlastimil Babka 
878e14c720eSVlastimil Babka 		/*
879e9ade569SVlastimil Babka 		 * Set a flag that we successfully isolated in this pageblock.
880e9ade569SVlastimil Babka 		 * In the next loop iteration, zone->compact_cached_free_pfn
881e9ade569SVlastimil Babka 		 * will not be updated and thus it will effectively contain the
882e9ade569SVlastimil Babka 		 * highest pageblock we isolated pages from.
883ff9543fdSMichal Nazarewicz 		 */
884e9ade569SVlastimil Babka 		if (isolated)
885c89511abSMel Gorman 			cc->finished_update_free = true;
886be976572SVlastimil Babka 
887be976572SVlastimil Babka 		/*
888be976572SVlastimil Babka 		 * isolate_freepages_block() might have aborted due to async
889be976572SVlastimil Babka 		 * compaction being contended
890be976572SVlastimil Babka 		 */
891be976572SVlastimil Babka 		if (cc->contended)
892be976572SVlastimil Babka 			break;
893c89511abSMel Gorman 	}
894ff9543fdSMichal Nazarewicz 
895ff9543fdSMichal Nazarewicz 	/* split_free_page does not map the pages */
896ff9543fdSMichal Nazarewicz 	map_pages(freelist);
897ff9543fdSMichal Nazarewicz 
8987ed695e0SVlastimil Babka 	/*
8997ed695e0SVlastimil Babka 	 * If we crossed the migrate scanner, we want to keep it that way
9007ed695e0SVlastimil Babka 	 * so that compact_finished() may detect this
9017ed695e0SVlastimil Babka 	 */
902c96b9e50SVlastimil Babka 	if (block_start_pfn < low_pfn)
903e9ade569SVlastimil Babka 		cc->free_pfn = cc->migrate_pfn;
904c96b9e50SVlastimil Babka 
905ff9543fdSMichal Nazarewicz 	cc->nr_freepages = nr_freepages;
906748446bbSMel Gorman }
907748446bbSMel Gorman 
908748446bbSMel Gorman /*
909748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
910748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
911748446bbSMel Gorman  */
912748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
913748446bbSMel Gorman 					unsigned long data,
914748446bbSMel Gorman 					int **result)
915748446bbSMel Gorman {
916748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
917748446bbSMel Gorman 	struct page *freepage;
918748446bbSMel Gorman 
919be976572SVlastimil Babka 	/*
920be976572SVlastimil Babka 	 * Isolate free pages if necessary, and if we are not aborting due to
921be976572SVlastimil Babka 	 * contention.
922be976572SVlastimil Babka 	 */
923748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
924be976572SVlastimil Babka 		if (!cc->contended)
925edc2ca61SVlastimil Babka 			isolate_freepages(cc);
926748446bbSMel Gorman 
927748446bbSMel Gorman 		if (list_empty(&cc->freepages))
928748446bbSMel Gorman 			return NULL;
929748446bbSMel Gorman 	}
930748446bbSMel Gorman 
931748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
932748446bbSMel Gorman 	list_del(&freepage->lru);
933748446bbSMel Gorman 	cc->nr_freepages--;
934748446bbSMel Gorman 
935748446bbSMel Gorman 	return freepage;
936748446bbSMel Gorman }
937748446bbSMel Gorman 
938748446bbSMel Gorman /*
939d53aea3dSDavid Rientjes  * This is a migrate-callback that "frees" freepages back to the isolated
940d53aea3dSDavid Rientjes  * freelist.  All pages on the freelist are from the same zone, so there is no
941d53aea3dSDavid Rientjes  * special handling needed for NUMA.
942d53aea3dSDavid Rientjes  */
943d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data)
944d53aea3dSDavid Rientjes {
945d53aea3dSDavid Rientjes 	struct compact_control *cc = (struct compact_control *)data;
946d53aea3dSDavid Rientjes 
947d53aea3dSDavid Rientjes 	list_add(&page->lru, &cc->freepages);
948d53aea3dSDavid Rientjes 	cc->nr_freepages++;
949d53aea3dSDavid Rientjes }
950d53aea3dSDavid Rientjes 
951ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
952ff9543fdSMichal Nazarewicz typedef enum {
953ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
954ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
955ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
956ff9543fdSMichal Nazarewicz } isolate_migrate_t;
957ff9543fdSMichal Nazarewicz 
958ff9543fdSMichal Nazarewicz /*
959edc2ca61SVlastimil Babka  * Isolate all pages that can be migrated from the first suitable block,
960edc2ca61SVlastimil Babka  * starting at the block pointed to by the migrate scanner pfn within
961edc2ca61SVlastimil Babka  * compact_control.
962ff9543fdSMichal Nazarewicz  */
963ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
964ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
965ff9543fdSMichal Nazarewicz {
966ff9543fdSMichal Nazarewicz 	unsigned long low_pfn, end_pfn;
967edc2ca61SVlastimil Babka 	struct page *page;
968edc2ca61SVlastimil Babka 	const isolate_mode_t isolate_mode =
969edc2ca61SVlastimil Babka 		(cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
970ff9543fdSMichal Nazarewicz 
971edc2ca61SVlastimil Babka 	/*
972edc2ca61SVlastimil Babka 	 * Start at where we last stopped, or beginning of the zone as
973edc2ca61SVlastimil Babka 	 * initialized by compact_zone()
974edc2ca61SVlastimil Babka 	 */
975edc2ca61SVlastimil Babka 	low_pfn = cc->migrate_pfn;
976ff9543fdSMichal Nazarewicz 
977ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
978a9aacbccSMel Gorman 	end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
979ff9543fdSMichal Nazarewicz 
980edc2ca61SVlastimil Babka 	/*
981edc2ca61SVlastimil Babka 	 * Iterate over whole pageblocks until we find the first suitable.
982edc2ca61SVlastimil Babka 	 * Do not cross the free scanner.
983edc2ca61SVlastimil Babka 	 */
984edc2ca61SVlastimil Babka 	for (; end_pfn <= cc->free_pfn;
985edc2ca61SVlastimil Babka 			low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
986edc2ca61SVlastimil Babka 
987edc2ca61SVlastimil Babka 		/*
988edc2ca61SVlastimil Babka 		 * This can potentially iterate a massively long zone with
989edc2ca61SVlastimil Babka 		 * many pageblocks unsuitable, so periodically check if we
990edc2ca61SVlastimil Babka 		 * need to schedule, or even abort async compaction.
991edc2ca61SVlastimil Babka 		 */
992edc2ca61SVlastimil Babka 		if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
993edc2ca61SVlastimil Babka 						&& compact_should_abort(cc))
994edc2ca61SVlastimil Babka 			break;
995edc2ca61SVlastimil Babka 
9967d49d886SVlastimil Babka 		page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
9977d49d886SVlastimil Babka 		if (!page)
998edc2ca61SVlastimil Babka 			continue;
999edc2ca61SVlastimil Babka 
1000edc2ca61SVlastimil Babka 		/* If isolation recently failed, do not retry */
1001edc2ca61SVlastimil Babka 		if (!isolation_suitable(cc, page))
1002edc2ca61SVlastimil Babka 			continue;
1003edc2ca61SVlastimil Babka 
1004edc2ca61SVlastimil Babka 		/*
1005edc2ca61SVlastimil Babka 		 * For async compaction, also only scan in MOVABLE blocks.
1006edc2ca61SVlastimil Babka 		 * Async compaction is optimistic to see if the minimum amount
1007edc2ca61SVlastimil Babka 		 * of work satisfies the allocation.
1008edc2ca61SVlastimil Babka 		 */
1009edc2ca61SVlastimil Babka 		if (cc->mode == MIGRATE_ASYNC &&
1010edc2ca61SVlastimil Babka 		    !migrate_async_suitable(get_pageblock_migratetype(page)))
1011edc2ca61SVlastimil Babka 			continue;
1012ff9543fdSMichal Nazarewicz 
1013ff9543fdSMichal Nazarewicz 		/* Perform the isolation */
1014edc2ca61SVlastimil Babka 		low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1015edc2ca61SVlastimil Babka 								isolate_mode);
1016edc2ca61SVlastimil Babka 
1017e64c5237SShaohua Li 		if (!low_pfn || cc->contended)
1018ff9543fdSMichal Nazarewicz 			return ISOLATE_ABORT;
1019ff9543fdSMichal Nazarewicz 
1020edc2ca61SVlastimil Babka 		/*
1021edc2ca61SVlastimil Babka 		 * Either we isolated something and proceed with migration. Or
1022edc2ca61SVlastimil Babka 		 * we failed and compact_zone should decide if we should
1023edc2ca61SVlastimil Babka 		 * continue or not.
1024edc2ca61SVlastimil Babka 		 */
1025edc2ca61SVlastimil Babka 		break;
1026edc2ca61SVlastimil Babka 	}
1027edc2ca61SVlastimil Babka 
1028edc2ca61SVlastimil Babka 	acct_isolated(zone, cc);
1029edc2ca61SVlastimil Babka 	/* Record where migration scanner will be restarted */
1030ff9543fdSMichal Nazarewicz 	cc->migrate_pfn = low_pfn;
1031ff9543fdSMichal Nazarewicz 
1032edc2ca61SVlastimil Babka 	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1033ff9543fdSMichal Nazarewicz }
1034ff9543fdSMichal Nazarewicz 
1035*6d7ce559SDavid Rientjes static int compact_finished(struct zone *zone, struct compact_control *cc,
1036*6d7ce559SDavid Rientjes 			    const int migratetype)
1037748446bbSMel Gorman {
10388fb74b9fSMel Gorman 	unsigned int order;
10395a03b051SAndrea Arcangeli 	unsigned long watermark;
104056de7263SMel Gorman 
1041be976572SVlastimil Babka 	if (cc->contended || fatal_signal_pending(current))
1042748446bbSMel Gorman 		return COMPACT_PARTIAL;
1043748446bbSMel Gorman 
1044753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
1045bb13ffebSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn) {
104655b7c4c9SVlastimil Babka 		/* Let the next compaction start anew. */
104735979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
104835979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
104955b7c4c9SVlastimil Babka 		zone->compact_cached_free_pfn = zone_end_pfn(zone);
105055b7c4c9SVlastimil Babka 
105162997027SMel Gorman 		/*
105262997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
105362997027SMel Gorman 		 * by kswapd when it goes to sleep. kswapd does not set the
105462997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
105562997027SMel Gorman 		 * based on an allocation request.
105662997027SMel Gorman 		 */
105762997027SMel Gorman 		if (!current_is_kswapd())
105862997027SMel Gorman 			zone->compact_blockskip_flush = true;
105962997027SMel Gorman 
1060748446bbSMel Gorman 		return COMPACT_COMPLETE;
1061bb13ffebSMel Gorman 	}
1062748446bbSMel Gorman 
106382478fb7SJohannes Weiner 	/*
106482478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
106582478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
106682478fb7SJohannes Weiner 	 */
106756de7263SMel Gorman 	if (cc->order == -1)
106856de7263SMel Gorman 		return COMPACT_CONTINUE;
106956de7263SMel Gorman 
10703957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
10713957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
10723957c776SMichal Hocko 	watermark += (1 << cc->order);
10733957c776SMichal Hocko 
10743957c776SMichal Hocko 	if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
10753957c776SMichal Hocko 		return COMPACT_CONTINUE;
10763957c776SMichal Hocko 
107756de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
107856de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
10798fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
10808fb74b9fSMel Gorman 
108156de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
1082*6d7ce559SDavid Rientjes 		if (!list_empty(&area->free_list[migratetype]))
108356de7263SMel Gorman 			return COMPACT_PARTIAL;
108456de7263SMel Gorman 
108556de7263SMel Gorman 		/* Job done if allocation would set block type */
10861fb3f8caSMel Gorman 		if (cc->order >= pageblock_order && area->nr_free)
108756de7263SMel Gorman 			return COMPACT_PARTIAL;
108856de7263SMel Gorman 	}
108956de7263SMel Gorman 
1090748446bbSMel Gorman 	return COMPACT_CONTINUE;
1091748446bbSMel Gorman }
1092748446bbSMel Gorman 
10933e7d3449SMel Gorman /*
10943e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
10953e7d3449SMel Gorman  * Returns
10963e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
10973e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
10983e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
10993e7d3449SMel Gorman  */
11003e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order)
11013e7d3449SMel Gorman {
11023e7d3449SMel Gorman 	int fragindex;
11033e7d3449SMel Gorman 	unsigned long watermark;
11043e7d3449SMel Gorman 
11053e7d3449SMel Gorman 	/*
11063957c776SMichal Hocko 	 * order == -1 is expected when compacting via
11073957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
11083957c776SMichal Hocko 	 */
11093957c776SMichal Hocko 	if (order == -1)
11103957c776SMichal Hocko 		return COMPACT_CONTINUE;
11113957c776SMichal Hocko 
11123957c776SMichal Hocko 	/*
11133e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
11143e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
11153e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
11163e7d3449SMel Gorman 	 */
11173e7d3449SMel Gorman 	watermark = low_wmark_pages(zone) + (2UL << order);
11183e7d3449SMel Gorman 	if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
11193e7d3449SMel Gorman 		return COMPACT_SKIPPED;
11203e7d3449SMel Gorman 
11213e7d3449SMel Gorman 	/*
11223e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
11233e7d3449SMel Gorman 	 * low memory or external fragmentation
11243e7d3449SMel Gorman 	 *
1125a582a738SShaohua Li 	 * index of -1000 implies allocations might succeed depending on
1126a582a738SShaohua Li 	 * watermarks
11273e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
11283e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
11293e7d3449SMel Gorman 	 *
11303e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
11313e7d3449SMel Gorman 	 */
11323e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
11333e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
11343e7d3449SMel Gorman 		return COMPACT_SKIPPED;
11353e7d3449SMel Gorman 
1136a582a738SShaohua Li 	if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1137a582a738SShaohua Li 	    0, 0))
11383e7d3449SMel Gorman 		return COMPACT_PARTIAL;
11393e7d3449SMel Gorman 
11403e7d3449SMel Gorman 	return COMPACT_CONTINUE;
11413e7d3449SMel Gorman }
11423e7d3449SMel Gorman 
1143748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
1144748446bbSMel Gorman {
1145748446bbSMel Gorman 	int ret;
1146c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
1147108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
1148*6d7ce559SDavid Rientjes 	const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1149e0b9daebSDavid Rientjes 	const bool sync = cc->mode != MIGRATE_ASYNC;
1150748446bbSMel Gorman 
11513e7d3449SMel Gorman 	ret = compaction_suitable(zone, cc->order);
11523e7d3449SMel Gorman 	switch (ret) {
11533e7d3449SMel Gorman 	case COMPACT_PARTIAL:
11543e7d3449SMel Gorman 	case COMPACT_SKIPPED:
11553e7d3449SMel Gorman 		/* Compaction is likely to fail */
11563e7d3449SMel Gorman 		return ret;
11573e7d3449SMel Gorman 	case COMPACT_CONTINUE:
11583e7d3449SMel Gorman 		/* Fall through to compaction */
11593e7d3449SMel Gorman 		;
11603e7d3449SMel Gorman 	}
11613e7d3449SMel Gorman 
1162c89511abSMel Gorman 	/*
1163d3132e4bSVlastimil Babka 	 * Clear pageblock skip if there were failures recently and compaction
1164d3132e4bSVlastimil Babka 	 * is about to be retried after being deferred. kswapd does not do
1165d3132e4bSVlastimil Babka 	 * this reset as it'll reset the cached information when going to sleep.
1166d3132e4bSVlastimil Babka 	 */
1167d3132e4bSVlastimil Babka 	if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1168d3132e4bSVlastimil Babka 		__reset_isolation_suitable(zone);
1169d3132e4bSVlastimil Babka 
1170d3132e4bSVlastimil Babka 	/*
1171c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
1172c89511abSMel Gorman 	 * information on where the scanners should start but check that it
1173c89511abSMel Gorman 	 * is initialised by ensuring the values are within zone boundaries.
1174c89511abSMel Gorman 	 */
1175e0b9daebSDavid Rientjes 	cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1176c89511abSMel Gorman 	cc->free_pfn = zone->compact_cached_free_pfn;
1177c89511abSMel Gorman 	if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1178c89511abSMel Gorman 		cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1179c89511abSMel Gorman 		zone->compact_cached_free_pfn = cc->free_pfn;
1180c89511abSMel Gorman 	}
1181c89511abSMel Gorman 	if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1182c89511abSMel Gorman 		cc->migrate_pfn = start_pfn;
118335979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
118435979ef3SDavid Rientjes 		zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1185c89511abSMel Gorman 	}
1186748446bbSMel Gorman 
11870eb927c0SMel Gorman 	trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
11880eb927c0SMel Gorman 
1189748446bbSMel Gorman 	migrate_prep_local();
1190748446bbSMel Gorman 
1191*6d7ce559SDavid Rientjes 	while ((ret = compact_finished(zone, cc, migratetype)) ==
1192*6d7ce559SDavid Rientjes 						COMPACT_CONTINUE) {
11939d502c1cSMinchan Kim 		int err;
1194748446bbSMel Gorman 
1195f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
1196f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
1197f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
11985733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
1199e64c5237SShaohua Li 			cc->nr_migratepages = 0;
1200f9e35b3bSMel Gorman 			goto out;
1201f9e35b3bSMel Gorman 		case ISOLATE_NONE:
1202748446bbSMel Gorman 			continue;
1203f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
1204f9e35b3bSMel Gorman 			;
1205f9e35b3bSMel Gorman 		}
1206748446bbSMel Gorman 
1207d53aea3dSDavid Rientjes 		err = migrate_pages(&cc->migratepages, compaction_alloc,
1208e0b9daebSDavid Rientjes 				compaction_free, (unsigned long)cc, cc->mode,
12097b2a2d4aSMel Gorman 				MR_COMPACTION);
1210748446bbSMel Gorman 
1211f8c9301fSVlastimil Babka 		trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1212f8c9301fSVlastimil Babka 							&cc->migratepages);
1213748446bbSMel Gorman 
1214f8c9301fSVlastimil Babka 		/* All pages were either migrated or will be released */
1215f8c9301fSVlastimil Babka 		cc->nr_migratepages = 0;
12169d502c1cSMinchan Kim 		if (err) {
12175733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
12187ed695e0SVlastimil Babka 			/*
12197ed695e0SVlastimil Babka 			 * migrate_pages() may return -ENOMEM when scanners meet
12207ed695e0SVlastimil Babka 			 * and we want compact_finished() to detect it
12217ed695e0SVlastimil Babka 			 */
12227ed695e0SVlastimil Babka 			if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
12234bf2bba3SDavid Rientjes 				ret = COMPACT_PARTIAL;
12244bf2bba3SDavid Rientjes 				goto out;
1225748446bbSMel Gorman 			}
12264bf2bba3SDavid Rientjes 		}
1227748446bbSMel Gorman 	}
1228748446bbSMel Gorman 
1229f9e35b3bSMel Gorman out:
1230748446bbSMel Gorman 	/* Release free pages and check accounting */
1231748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
1232748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
1233748446bbSMel Gorman 
12340eb927c0SMel Gorman 	trace_mm_compaction_end(ret);
12350eb927c0SMel Gorman 
1236748446bbSMel Gorman 	return ret;
1237748446bbSMel Gorman }
123876ab0f53SMel Gorman 
1239e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order,
12401f9efdefSVlastimil Babka 		gfp_t gfp_mask, enum migrate_mode mode, int *contended)
124156de7263SMel Gorman {
1242e64c5237SShaohua Li 	unsigned long ret;
124356de7263SMel Gorman 	struct compact_control cc = {
124456de7263SMel Gorman 		.nr_freepages = 0,
124556de7263SMel Gorman 		.nr_migratepages = 0,
124656de7263SMel Gorman 		.order = order,
1247*6d7ce559SDavid Rientjes 		.gfp_mask = gfp_mask,
124856de7263SMel Gorman 		.zone = zone,
1249e0b9daebSDavid Rientjes 		.mode = mode,
125056de7263SMel Gorman 	};
125156de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
125256de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
125356de7263SMel Gorman 
1254e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1255e64c5237SShaohua Li 
1256e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1257e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1258e64c5237SShaohua Li 
1259e64c5237SShaohua Li 	*contended = cc.contended;
1260e64c5237SShaohua Li 	return ret;
126156de7263SMel Gorman }
126256de7263SMel Gorman 
12635e771905SMel Gorman int sysctl_extfrag_threshold = 500;
12645e771905SMel Gorman 
126556de7263SMel Gorman /**
126656de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
126756de7263SMel Gorman  * @zonelist: The zonelist used for the current allocation
126856de7263SMel Gorman  * @order: The order of the current allocation
126956de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
127056de7263SMel Gorman  * @nodemask: The allowed nodes to allocate from
1271e0b9daebSDavid Rientjes  * @mode: The migration mode for async, sync light, or sync migration
12721f9efdefSVlastimil Babka  * @contended: Return value that determines if compaction was aborted due to
12731f9efdefSVlastimil Babka  *	       need_resched() or lock contention
127453853e2dSVlastimil Babka  * @candidate_zone: Return the zone where we think allocation should succeed
127556de7263SMel Gorman  *
127656de7263SMel Gorman  * This is the main entry point for direct page compaction.
127756de7263SMel Gorman  */
127856de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist,
127977f1fe6bSMel Gorman 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
12801f9efdefSVlastimil Babka 			enum migrate_mode mode, int *contended,
128153853e2dSVlastimil Babka 			struct zone **candidate_zone)
128256de7263SMel Gorman {
128356de7263SMel Gorman 	enum zone_type high_zoneidx = gfp_zone(gfp_mask);
128456de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
128556de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
128656de7263SMel Gorman 	struct zoneref *z;
128756de7263SMel Gorman 	struct zone *zone;
128853853e2dSVlastimil Babka 	int rc = COMPACT_DEFERRED;
1289d95ea5d1SBartlomiej Zolnierkiewicz 	int alloc_flags = 0;
12901f9efdefSVlastimil Babka 	int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
12911f9efdefSVlastimil Babka 
12921f9efdefSVlastimil Babka 	*contended = COMPACT_CONTENDED_NONE;
129356de7263SMel Gorman 
12944ffb6335SMel Gorman 	/* Check if the GFP flags allow compaction */
1295c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
129653853e2dSVlastimil Babka 		return COMPACT_SKIPPED;
129756de7263SMel Gorman 
1298d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA
129943e7a34dSDavid Rientjes 	if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1300d95ea5d1SBartlomiej Zolnierkiewicz 		alloc_flags |= ALLOC_CMA;
1301d95ea5d1SBartlomiej Zolnierkiewicz #endif
130256de7263SMel Gorman 	/* Compact each zone in the list */
130356de7263SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
130456de7263SMel Gorman 								nodemask) {
130556de7263SMel Gorman 		int status;
13061f9efdefSVlastimil Babka 		int zone_contended;
130756de7263SMel Gorman 
130853853e2dSVlastimil Babka 		if (compaction_deferred(zone, order))
130953853e2dSVlastimil Babka 			continue;
131053853e2dSVlastimil Babka 
1311e0b9daebSDavid Rientjes 		status = compact_zone_order(zone, order, gfp_mask, mode,
13121f9efdefSVlastimil Babka 							&zone_contended);
131356de7263SMel Gorman 		rc = max(status, rc);
13141f9efdefSVlastimil Babka 		/*
13151f9efdefSVlastimil Babka 		 * It takes at least one zone that wasn't lock contended
13161f9efdefSVlastimil Babka 		 * to clear all_zones_contended.
13171f9efdefSVlastimil Babka 		 */
13181f9efdefSVlastimil Babka 		all_zones_contended &= zone_contended;
131956de7263SMel Gorman 
13203e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
1321d95ea5d1SBartlomiej Zolnierkiewicz 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
132253853e2dSVlastimil Babka 				      alloc_flags)) {
132353853e2dSVlastimil Babka 			*candidate_zone = zone;
132453853e2dSVlastimil Babka 			/*
132553853e2dSVlastimil Babka 			 * We think the allocation will succeed in this zone,
132653853e2dSVlastimil Babka 			 * but it is not certain, hence the false. The caller
132753853e2dSVlastimil Babka 			 * will repeat this with true if allocation indeed
132853853e2dSVlastimil Babka 			 * succeeds in this zone.
132953853e2dSVlastimil Babka 			 */
133053853e2dSVlastimil Babka 			compaction_defer_reset(zone, order, false);
13311f9efdefSVlastimil Babka 			/*
13321f9efdefSVlastimil Babka 			 * It is possible that async compaction aborted due to
13331f9efdefSVlastimil Babka 			 * need_resched() and the watermarks were ok thanks to
13341f9efdefSVlastimil Babka 			 * somebody else freeing memory. The allocation can
13351f9efdefSVlastimil Babka 			 * however still fail so we better signal the
13361f9efdefSVlastimil Babka 			 * need_resched() contention anyway (this will not
13371f9efdefSVlastimil Babka 			 * prevent the allocation attempt).
13381f9efdefSVlastimil Babka 			 */
13391f9efdefSVlastimil Babka 			if (zone_contended == COMPACT_CONTENDED_SCHED)
13401f9efdefSVlastimil Babka 				*contended = COMPACT_CONTENDED_SCHED;
13411f9efdefSVlastimil Babka 
13421f9efdefSVlastimil Babka 			goto break_loop;
13431f9efdefSVlastimil Babka 		}
13441f9efdefSVlastimil Babka 
13451f9efdefSVlastimil Babka 		if (mode != MIGRATE_ASYNC) {
134653853e2dSVlastimil Babka 			/*
134753853e2dSVlastimil Babka 			 * We think that allocation won't succeed in this zone
134853853e2dSVlastimil Babka 			 * so we defer compaction there. If it ends up
134953853e2dSVlastimil Babka 			 * succeeding after all, it will be reset.
135053853e2dSVlastimil Babka 			 */
135153853e2dSVlastimil Babka 			defer_compaction(zone, order);
135253853e2dSVlastimil Babka 		}
13531f9efdefSVlastimil Babka 
13541f9efdefSVlastimil Babka 		/*
13551f9efdefSVlastimil Babka 		 * We might have stopped compacting due to need_resched() in
13561f9efdefSVlastimil Babka 		 * async compaction, or due to a fatal signal detected. In that
13571f9efdefSVlastimil Babka 		 * case do not try further zones and signal need_resched()
13581f9efdefSVlastimil Babka 		 * contention.
13591f9efdefSVlastimil Babka 		 */
13601f9efdefSVlastimil Babka 		if ((zone_contended == COMPACT_CONTENDED_SCHED)
13611f9efdefSVlastimil Babka 					|| fatal_signal_pending(current)) {
13621f9efdefSVlastimil Babka 			*contended = COMPACT_CONTENDED_SCHED;
13631f9efdefSVlastimil Babka 			goto break_loop;
136456de7263SMel Gorman 		}
136556de7263SMel Gorman 
13661f9efdefSVlastimil Babka 		continue;
13671f9efdefSVlastimil Babka break_loop:
13681f9efdefSVlastimil Babka 		/*
13691f9efdefSVlastimil Babka 		 * We might not have tried all the zones, so  be conservative
13701f9efdefSVlastimil Babka 		 * and assume they are not all lock contended.
13711f9efdefSVlastimil Babka 		 */
13721f9efdefSVlastimil Babka 		all_zones_contended = 0;
13731f9efdefSVlastimil Babka 		break;
13741f9efdefSVlastimil Babka 	}
13751f9efdefSVlastimil Babka 
13761f9efdefSVlastimil Babka 	/*
13771f9efdefSVlastimil Babka 	 * If at least one zone wasn't deferred or skipped, we report if all
13781f9efdefSVlastimil Babka 	 * zones that were tried were lock contended.
13791f9efdefSVlastimil Babka 	 */
13801f9efdefSVlastimil Babka 	if (rc > COMPACT_SKIPPED && all_zones_contended)
13811f9efdefSVlastimil Babka 		*contended = COMPACT_CONTENDED_LOCK;
13821f9efdefSVlastimil Babka 
138356de7263SMel Gorman 	return rc;
138456de7263SMel Gorman }
138556de7263SMel Gorman 
138656de7263SMel Gorman 
138776ab0f53SMel Gorman /* Compact all zones within a node */
13887103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
138976ab0f53SMel Gorman {
139076ab0f53SMel Gorman 	int zoneid;
139176ab0f53SMel Gorman 	struct zone *zone;
139276ab0f53SMel Gorman 
139376ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
139476ab0f53SMel Gorman 
139576ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
139676ab0f53SMel Gorman 		if (!populated_zone(zone))
139776ab0f53SMel Gorman 			continue;
139876ab0f53SMel Gorman 
13997be62de9SRik van Riel 		cc->nr_freepages = 0;
14007be62de9SRik van Riel 		cc->nr_migratepages = 0;
14017be62de9SRik van Riel 		cc->zone = zone;
14027be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
14037be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
140476ab0f53SMel Gorman 
1405aad6ec37SDan Carpenter 		if (cc->order == -1 || !compaction_deferred(zone, cc->order))
14067be62de9SRik van Riel 			compact_zone(zone, cc);
140776ab0f53SMel Gorman 
1408aff62249SRik van Riel 		if (cc->order > 0) {
1409de6c60a6SVlastimil Babka 			if (zone_watermark_ok(zone, cc->order,
1410de6c60a6SVlastimil Babka 						low_wmark_pages(zone), 0, 0))
1411de6c60a6SVlastimil Babka 				compaction_defer_reset(zone, cc->order, false);
1412aff62249SRik van Riel 		}
1413aff62249SRik van Riel 
14147be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->freepages));
14157be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->migratepages));
141676ab0f53SMel Gorman 	}
141776ab0f53SMel Gorman }
141876ab0f53SMel Gorman 
14197103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order)
14207be62de9SRik van Riel {
14217be62de9SRik van Riel 	struct compact_control cc = {
14227be62de9SRik van Riel 		.order = order,
1423e0b9daebSDavid Rientjes 		.mode = MIGRATE_ASYNC,
14247be62de9SRik van Riel 	};
14257be62de9SRik van Riel 
14263a7200afSMel Gorman 	if (!order)
14273a7200afSMel Gorman 		return;
14283a7200afSMel Gorman 
14297103f16dSAndrew Morton 	__compact_pgdat(pgdat, &cc);
14307be62de9SRik van Riel }
14317be62de9SRik van Riel 
14327103f16dSAndrew Morton static void compact_node(int nid)
14337be62de9SRik van Riel {
14347be62de9SRik van Riel 	struct compact_control cc = {
14357be62de9SRik van Riel 		.order = -1,
1436e0b9daebSDavid Rientjes 		.mode = MIGRATE_SYNC,
143791ca9186SDavid Rientjes 		.ignore_skip_hint = true,
14387be62de9SRik van Riel 	};
14397be62de9SRik van Riel 
14407103f16dSAndrew Morton 	__compact_pgdat(NODE_DATA(nid), &cc);
14417be62de9SRik van Riel }
14427be62de9SRik van Riel 
144376ab0f53SMel Gorman /* Compact all nodes in the system */
14447964c06dSJason Liu static void compact_nodes(void)
144576ab0f53SMel Gorman {
144676ab0f53SMel Gorman 	int nid;
144776ab0f53SMel Gorman 
14488575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
14498575ec29SHugh Dickins 	lru_add_drain_all();
14508575ec29SHugh Dickins 
145176ab0f53SMel Gorman 	for_each_online_node(nid)
145276ab0f53SMel Gorman 		compact_node(nid);
145376ab0f53SMel Gorman }
145476ab0f53SMel Gorman 
145576ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
145676ab0f53SMel Gorman int sysctl_compact_memory;
145776ab0f53SMel Gorman 
145876ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
145976ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
146076ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
146176ab0f53SMel Gorman {
146276ab0f53SMel Gorman 	if (write)
14637964c06dSJason Liu 		compact_nodes();
146476ab0f53SMel Gorman 
146576ab0f53SMel Gorman 	return 0;
146676ab0f53SMel Gorman }
1467ed4a6d7fSMel Gorman 
14685e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
14695e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
14705e771905SMel Gorman {
14715e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
14725e771905SMel Gorman 
14735e771905SMel Gorman 	return 0;
14745e771905SMel Gorman }
14755e771905SMel Gorman 
1476ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
147774e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev,
147810fbcf4cSKay Sievers 			struct device_attribute *attr,
1479ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1480ed4a6d7fSMel Gorman {
14818575ec29SHugh Dickins 	int nid = dev->id;
14828575ec29SHugh Dickins 
14838575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
14848575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
14858575ec29SHugh Dickins 		lru_add_drain_all();
14868575ec29SHugh Dickins 
14878575ec29SHugh Dickins 		compact_node(nid);
14888575ec29SHugh Dickins 	}
1489ed4a6d7fSMel Gorman 
1490ed4a6d7fSMel Gorman 	return count;
1491ed4a6d7fSMel Gorman }
149210fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1493ed4a6d7fSMel Gorman 
1494ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1495ed4a6d7fSMel Gorman {
149610fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1497ed4a6d7fSMel Gorman }
1498ed4a6d7fSMel Gorman 
1499ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1500ed4a6d7fSMel Gorman {
150110fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1502ed4a6d7fSMel Gorman }
1503ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1504ff9543fdSMichal Nazarewicz 
1505ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
1506