xref: /openbmc/linux/mm/compaction.c (revision 7b2a2d4a18fffac3c4872021529b0657896db788)
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>
17748446bbSMel Gorman #include "internal.h"
18748446bbSMel Gorman 
19ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA
20ff9543fdSMichal Nazarewicz 
21b7aba698SMel Gorman #define CREATE_TRACE_POINTS
22b7aba698SMel Gorman #include <trace/events/compaction.h>
23b7aba698SMel Gorman 
24748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
25748446bbSMel Gorman {
26748446bbSMel Gorman 	struct page *page, *next;
27748446bbSMel Gorman 	unsigned long count = 0;
28748446bbSMel Gorman 
29748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
30748446bbSMel Gorman 		list_del(&page->lru);
31748446bbSMel Gorman 		__free_page(page);
32748446bbSMel Gorman 		count++;
33748446bbSMel Gorman 	}
34748446bbSMel Gorman 
35748446bbSMel Gorman 	return count;
36748446bbSMel Gorman }
37748446bbSMel Gorman 
38ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list)
39ff9543fdSMichal Nazarewicz {
40ff9543fdSMichal Nazarewicz 	struct page *page;
41ff9543fdSMichal Nazarewicz 
42ff9543fdSMichal Nazarewicz 	list_for_each_entry(page, list, lru) {
43ff9543fdSMichal Nazarewicz 		arch_alloc_page(page, 0);
44ff9543fdSMichal Nazarewicz 		kernel_map_pages(page, 1, 1);
45ff9543fdSMichal Nazarewicz 	}
46ff9543fdSMichal Nazarewicz }
47ff9543fdSMichal Nazarewicz 
4847118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype)
4947118af0SMichal Nazarewicz {
5047118af0SMichal Nazarewicz 	return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
5147118af0SMichal Nazarewicz }
5247118af0SMichal Nazarewicz 
53bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
54bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
55bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
56bb13ffebSMel Gorman 					struct page *page)
57bb13ffebSMel Gorman {
58bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
59bb13ffebSMel Gorman 		return true;
60bb13ffebSMel Gorman 
61bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
62bb13ffebSMel Gorman }
63bb13ffebSMel Gorman 
64bb13ffebSMel Gorman /*
65bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
66bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
67bb13ffebSMel Gorman  * meet.
68bb13ffebSMel Gorman  */
6962997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
70bb13ffebSMel Gorman {
71bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
72bb13ffebSMel Gorman 	unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
73bb13ffebSMel Gorman 	unsigned long pfn;
74bb13ffebSMel Gorman 
75c89511abSMel Gorman 	zone->compact_cached_migrate_pfn = start_pfn;
76c89511abSMel Gorman 	zone->compact_cached_free_pfn = end_pfn;
7762997027SMel Gorman 	zone->compact_blockskip_flush = false;
78bb13ffebSMel Gorman 
79bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
80bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
81bb13ffebSMel Gorman 		struct page *page;
82bb13ffebSMel Gorman 
83bb13ffebSMel Gorman 		cond_resched();
84bb13ffebSMel Gorman 
85bb13ffebSMel Gorman 		if (!pfn_valid(pfn))
86bb13ffebSMel Gorman 			continue;
87bb13ffebSMel Gorman 
88bb13ffebSMel Gorman 		page = pfn_to_page(pfn);
89bb13ffebSMel Gorman 		if (zone != page_zone(page))
90bb13ffebSMel Gorman 			continue;
91bb13ffebSMel Gorman 
92bb13ffebSMel Gorman 		clear_pageblock_skip(page);
93bb13ffebSMel Gorman 	}
94bb13ffebSMel Gorman }
95bb13ffebSMel Gorman 
9662997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
9762997027SMel Gorman {
9862997027SMel Gorman 	int zoneid;
9962997027SMel Gorman 
10062997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
10162997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
10262997027SMel Gorman 		if (!populated_zone(zone))
10362997027SMel Gorman 			continue;
10462997027SMel Gorman 
10562997027SMel Gorman 		/* Only flush if a full compaction finished recently */
10662997027SMel Gorman 		if (zone->compact_blockskip_flush)
10762997027SMel Gorman 			__reset_isolation_suitable(zone);
10862997027SMel Gorman 	}
10962997027SMel Gorman }
11062997027SMel Gorman 
111bb13ffebSMel Gorman /*
112bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
11362997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
114bb13ffebSMel Gorman  */
115c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
116c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
117c89511abSMel Gorman 			bool migrate_scanner)
118bb13ffebSMel Gorman {
119c89511abSMel Gorman 	struct zone *zone = cc->zone;
120bb13ffebSMel Gorman 	if (!page)
121bb13ffebSMel Gorman 		return;
122bb13ffebSMel Gorman 
123c89511abSMel Gorman 	if (!nr_isolated) {
124c89511abSMel Gorman 		unsigned long pfn = page_to_pfn(page);
125bb13ffebSMel Gorman 		set_pageblock_skip(page);
126c89511abSMel Gorman 
127c89511abSMel Gorman 		/* Update where compaction should restart */
128c89511abSMel Gorman 		if (migrate_scanner) {
129c89511abSMel Gorman 			if (!cc->finished_update_migrate &&
130c89511abSMel Gorman 			    pfn > zone->compact_cached_migrate_pfn)
131c89511abSMel Gorman 				zone->compact_cached_migrate_pfn = pfn;
132c89511abSMel Gorman 		} else {
133c89511abSMel Gorman 			if (!cc->finished_update_free &&
134c89511abSMel Gorman 			    pfn < zone->compact_cached_free_pfn)
135c89511abSMel Gorman 				zone->compact_cached_free_pfn = pfn;
136c89511abSMel Gorman 		}
137c89511abSMel Gorman 	}
138bb13ffebSMel Gorman }
139bb13ffebSMel Gorman #else
140bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
141bb13ffebSMel Gorman 					struct page *page)
142bb13ffebSMel Gorman {
143bb13ffebSMel Gorman 	return true;
144bb13ffebSMel Gorman }
145bb13ffebSMel Gorman 
146c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
147c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
148c89511abSMel Gorman 			bool migrate_scanner)
149bb13ffebSMel Gorman {
150bb13ffebSMel Gorman }
151bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
152bb13ffebSMel Gorman 
1532a1402aaSMel Gorman static inline bool should_release_lock(spinlock_t *lock)
1542a1402aaSMel Gorman {
1552a1402aaSMel Gorman 	return need_resched() || spin_is_contended(lock);
1562a1402aaSMel Gorman }
1572a1402aaSMel Gorman 
15885aa125fSMichal Nazarewicz /*
159c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
160c67fe375SMel Gorman  * very heavily contended. Check if the process needs to be scheduled or
161c67fe375SMel Gorman  * if the lock is contended. For async compaction, back out in the event
162c67fe375SMel Gorman  * if contention is severe. For sync compaction, schedule.
163c67fe375SMel Gorman  *
164c67fe375SMel Gorman  * Returns true if the lock is held.
165c67fe375SMel Gorman  * Returns false if the lock is released and compaction should abort
166c67fe375SMel Gorman  */
167c67fe375SMel Gorman static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
168c67fe375SMel Gorman 				      bool locked, struct compact_control *cc)
169c67fe375SMel Gorman {
1702a1402aaSMel Gorman 	if (should_release_lock(lock)) {
171c67fe375SMel Gorman 		if (locked) {
172c67fe375SMel Gorman 			spin_unlock_irqrestore(lock, *flags);
173c67fe375SMel Gorman 			locked = false;
174c67fe375SMel Gorman 		}
175c67fe375SMel Gorman 
176c67fe375SMel Gorman 		/* async aborts if taking too long or contended */
177c67fe375SMel Gorman 		if (!cc->sync) {
178e64c5237SShaohua Li 			cc->contended = true;
179c67fe375SMel Gorman 			return false;
180c67fe375SMel Gorman 		}
181c67fe375SMel Gorman 
182c67fe375SMel Gorman 		cond_resched();
183c67fe375SMel Gorman 	}
184c67fe375SMel Gorman 
185c67fe375SMel Gorman 	if (!locked)
186c67fe375SMel Gorman 		spin_lock_irqsave(lock, *flags);
187c67fe375SMel Gorman 	return true;
188c67fe375SMel Gorman }
189c67fe375SMel Gorman 
190c67fe375SMel Gorman static inline bool compact_trylock_irqsave(spinlock_t *lock,
191c67fe375SMel Gorman 			unsigned long *flags, struct compact_control *cc)
192c67fe375SMel Gorman {
193c67fe375SMel Gorman 	return compact_checklock_irqsave(lock, flags, false, cc);
194c67fe375SMel Gorman }
195c67fe375SMel Gorman 
196f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */
197f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page)
198f40d1e42SMel Gorman {
199f40d1e42SMel Gorman 	int migratetype = get_pageblock_migratetype(page);
200f40d1e42SMel Gorman 
201f40d1e42SMel Gorman 	/* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
202f40d1e42SMel Gorman 	if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
203f40d1e42SMel Gorman 		return false;
204f40d1e42SMel Gorman 
205f40d1e42SMel Gorman 	/* If the page is a large free page, then allow migration */
206f40d1e42SMel Gorman 	if (PageBuddy(page) && page_order(page) >= pageblock_order)
207f40d1e42SMel Gorman 		return true;
208f40d1e42SMel Gorman 
209f40d1e42SMel Gorman 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
210f40d1e42SMel Gorman 	if (migrate_async_suitable(migratetype))
211f40d1e42SMel Gorman 		return true;
212f40d1e42SMel Gorman 
213f40d1e42SMel Gorman 	/* Otherwise skip the block */
214f40d1e42SMel Gorman 	return false;
215f40d1e42SMel Gorman }
216f40d1e42SMel Gorman 
2171fb3f8caSMel Gorman static void compact_capture_page(struct compact_control *cc)
2181fb3f8caSMel Gorman {
2191fb3f8caSMel Gorman 	unsigned long flags;
2201fb3f8caSMel Gorman 	int mtype, mtype_low, mtype_high;
2211fb3f8caSMel Gorman 
2221fb3f8caSMel Gorman 	if (!cc->page || *cc->page)
2231fb3f8caSMel Gorman 		return;
2241fb3f8caSMel Gorman 
2251fb3f8caSMel Gorman 	/*
2261fb3f8caSMel Gorman 	 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
2271fb3f8caSMel Gorman 	 * regardless of the migratetype of the freelist is is captured from.
2281fb3f8caSMel Gorman 	 * This is fine because the order for a high-order MIGRATE_MOVABLE
2291fb3f8caSMel Gorman 	 * allocation is typically at least a pageblock size and overall
2301fb3f8caSMel Gorman 	 * fragmentation is not impaired. Other allocation types must
2311fb3f8caSMel Gorman 	 * capture pages from their own migratelist because otherwise they
2321fb3f8caSMel Gorman 	 * could pollute other pageblocks like MIGRATE_MOVABLE with
2331fb3f8caSMel Gorman 	 * difficult to move pages and making fragmentation worse overall.
2341fb3f8caSMel Gorman 	 */
2351fb3f8caSMel Gorman 	if (cc->migratetype == MIGRATE_MOVABLE) {
2361fb3f8caSMel Gorman 		mtype_low = 0;
2371fb3f8caSMel Gorman 		mtype_high = MIGRATE_PCPTYPES;
2381fb3f8caSMel Gorman 	} else {
2391fb3f8caSMel Gorman 		mtype_low = cc->migratetype;
2401fb3f8caSMel Gorman 		mtype_high = cc->migratetype + 1;
2411fb3f8caSMel Gorman 	}
2421fb3f8caSMel Gorman 
2431fb3f8caSMel Gorman 	/* Speculatively examine the free lists without zone lock */
2441fb3f8caSMel Gorman 	for (mtype = mtype_low; mtype < mtype_high; mtype++) {
2451fb3f8caSMel Gorman 		int order;
2461fb3f8caSMel Gorman 		for (order = cc->order; order < MAX_ORDER; order++) {
2471fb3f8caSMel Gorman 			struct page *page;
2481fb3f8caSMel Gorman 			struct free_area *area;
2491fb3f8caSMel Gorman 			area = &(cc->zone->free_area[order]);
2501fb3f8caSMel Gorman 			if (list_empty(&area->free_list[mtype]))
2511fb3f8caSMel Gorman 				continue;
2521fb3f8caSMel Gorman 
2531fb3f8caSMel Gorman 			/* Take the lock and attempt capture of the page */
2541fb3f8caSMel Gorman 			if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
2551fb3f8caSMel Gorman 				return;
2561fb3f8caSMel Gorman 			if (!list_empty(&area->free_list[mtype])) {
2571fb3f8caSMel Gorman 				page = list_entry(area->free_list[mtype].next,
2581fb3f8caSMel Gorman 							struct page, lru);
2591fb3f8caSMel Gorman 				if (capture_free_page(page, cc->order, mtype)) {
2601fb3f8caSMel Gorman 					spin_unlock_irqrestore(&cc->zone->lock,
2611fb3f8caSMel Gorman 									flags);
2621fb3f8caSMel Gorman 					*cc->page = page;
2631fb3f8caSMel Gorman 					return;
2641fb3f8caSMel Gorman 				}
2651fb3f8caSMel Gorman 			}
2661fb3f8caSMel Gorman 			spin_unlock_irqrestore(&cc->zone->lock, flags);
2671fb3f8caSMel Gorman 		}
2681fb3f8caSMel Gorman 	}
2691fb3f8caSMel Gorman }
2701fb3f8caSMel Gorman 
271c67fe375SMel Gorman /*
27285aa125fSMichal Nazarewicz  * Isolate free pages onto a private freelist. Caller must hold zone->lock.
27385aa125fSMichal Nazarewicz  * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
27485aa125fSMichal Nazarewicz  * pages inside of the pageblock (even though it may still end up isolating
27585aa125fSMichal Nazarewicz  * some pages).
27685aa125fSMichal Nazarewicz  */
277f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
278f40d1e42SMel Gorman 				unsigned long blockpfn,
27985aa125fSMichal Nazarewicz 				unsigned long end_pfn,
28085aa125fSMichal Nazarewicz 				struct list_head *freelist,
28185aa125fSMichal Nazarewicz 				bool strict)
282748446bbSMel Gorman {
283b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
284bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
285f40d1e42SMel Gorman 	unsigned long nr_strict_required = end_pfn - blockpfn;
286f40d1e42SMel Gorman 	unsigned long flags;
287f40d1e42SMel Gorman 	bool locked = false;
288748446bbSMel Gorman 
289748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
290748446bbSMel Gorman 
291f40d1e42SMel Gorman 	/* Isolate free pages. */
292748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
293748446bbSMel Gorman 		int isolated, i;
294748446bbSMel Gorman 		struct page *page = cursor;
295748446bbSMel Gorman 
296b7aba698SMel Gorman 		nr_scanned++;
297f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
298748446bbSMel Gorman 			continue;
299bb13ffebSMel Gorman 		if (!valid_page)
300bb13ffebSMel Gorman 			valid_page = page;
301f40d1e42SMel Gorman 		if (!PageBuddy(page))
302f40d1e42SMel Gorman 			continue;
303f40d1e42SMel Gorman 
304f40d1e42SMel Gorman 		/*
305f40d1e42SMel Gorman 		 * The zone lock must be held to isolate freepages.
306f40d1e42SMel Gorman 		 * Unfortunately this is a very coarse lock and can be
307f40d1e42SMel Gorman 		 * heavily contended if there are parallel allocations
308f40d1e42SMel Gorman 		 * or parallel compactions. For async compaction do not
309f40d1e42SMel Gorman 		 * spin on the lock and we acquire the lock as late as
310f40d1e42SMel Gorman 		 * possible.
311f40d1e42SMel Gorman 		 */
312f40d1e42SMel Gorman 		locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
313f40d1e42SMel Gorman 								locked, cc);
314f40d1e42SMel Gorman 		if (!locked)
315f40d1e42SMel Gorman 			break;
316f40d1e42SMel Gorman 
317f40d1e42SMel Gorman 		/* Recheck this is a suitable migration target under lock */
318f40d1e42SMel Gorman 		if (!strict && !suitable_migration_target(page))
319f40d1e42SMel Gorman 			break;
320f40d1e42SMel Gorman 
321f40d1e42SMel Gorman 		/* Recheck this is a buddy page under lock */
322f40d1e42SMel Gorman 		if (!PageBuddy(page))
323f40d1e42SMel Gorman 			continue;
324748446bbSMel Gorman 
325748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
326748446bbSMel Gorman 		isolated = split_free_page(page);
32785aa125fSMichal Nazarewicz 		if (!isolated && strict)
328f40d1e42SMel Gorman 			break;
329748446bbSMel Gorman 		total_isolated += isolated;
330748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
331748446bbSMel Gorman 			list_add(&page->lru, freelist);
332748446bbSMel Gorman 			page++;
333748446bbSMel Gorman 		}
334748446bbSMel Gorman 
335748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
336748446bbSMel Gorman 		if (isolated) {
337748446bbSMel Gorman 			blockpfn += isolated - 1;
338748446bbSMel Gorman 			cursor += isolated - 1;
339748446bbSMel Gorman 		}
340748446bbSMel Gorman 	}
341748446bbSMel Gorman 
342b7aba698SMel Gorman 	trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
343f40d1e42SMel Gorman 
344f40d1e42SMel Gorman 	/*
345f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
346f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
347f40d1e42SMel Gorman 	 * returned and CMA will fail.
348f40d1e42SMel Gorman 	 */
3490db63d7eSMel Gorman 	if (strict && nr_strict_required > total_isolated)
350f40d1e42SMel Gorman 		total_isolated = 0;
351f40d1e42SMel Gorman 
352f40d1e42SMel Gorman 	if (locked)
353f40d1e42SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
354f40d1e42SMel Gorman 
355bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
356bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
357c89511abSMel Gorman 		update_pageblock_skip(cc, valid_page, total_isolated, false);
358bb13ffebSMel Gorman 
359748446bbSMel Gorman 	return total_isolated;
360748446bbSMel Gorman }
361748446bbSMel Gorman 
36285aa125fSMichal Nazarewicz /**
36385aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
36485aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
36585aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
36685aa125fSMichal Nazarewicz  *
36785aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
36885aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
36985aa125fSMichal Nazarewicz  * undo its actions and return zero.
37085aa125fSMichal Nazarewicz  *
37185aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
37285aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
37385aa125fSMichal Nazarewicz  * a free page).
37485aa125fSMichal Nazarewicz  */
375ff9543fdSMichal Nazarewicz unsigned long
376bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
377bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
37885aa125fSMichal Nazarewicz {
379f40d1e42SMel Gorman 	unsigned long isolated, pfn, block_end_pfn;
38085aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
38185aa125fSMichal Nazarewicz 
38285aa125fSMichal Nazarewicz 	for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
383bb13ffebSMel Gorman 		if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
38485aa125fSMichal Nazarewicz 			break;
38585aa125fSMichal Nazarewicz 
38685aa125fSMichal Nazarewicz 		/*
38785aa125fSMichal Nazarewicz 		 * On subsequent iterations ALIGN() is actually not needed,
38885aa125fSMichal Nazarewicz 		 * but we keep it that we not to complicate the code.
38985aa125fSMichal Nazarewicz 		 */
39085aa125fSMichal Nazarewicz 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
39185aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
39285aa125fSMichal Nazarewicz 
393bb13ffebSMel Gorman 		isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
39485aa125fSMichal Nazarewicz 						   &freelist, true);
39585aa125fSMichal Nazarewicz 
39685aa125fSMichal Nazarewicz 		/*
39785aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
39885aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
39985aa125fSMichal Nazarewicz 		 * non-free pages).
40085aa125fSMichal Nazarewicz 		 */
40185aa125fSMichal Nazarewicz 		if (!isolated)
40285aa125fSMichal Nazarewicz 			break;
40385aa125fSMichal Nazarewicz 
40485aa125fSMichal Nazarewicz 		/*
40585aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
40685aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
40785aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
40885aa125fSMichal Nazarewicz 		 */
40985aa125fSMichal Nazarewicz 	}
41085aa125fSMichal Nazarewicz 
41185aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
41285aa125fSMichal Nazarewicz 	map_pages(&freelist);
41385aa125fSMichal Nazarewicz 
41485aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
41585aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
41685aa125fSMichal Nazarewicz 		release_freepages(&freelist);
41785aa125fSMichal Nazarewicz 		return 0;
41885aa125fSMichal Nazarewicz 	}
41985aa125fSMichal Nazarewicz 
42085aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
42185aa125fSMichal Nazarewicz 	return pfn;
42285aa125fSMichal Nazarewicz }
42385aa125fSMichal Nazarewicz 
424748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
425c67fe375SMel Gorman static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
426748446bbSMel Gorman {
427748446bbSMel Gorman 	struct page *page;
428b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
429748446bbSMel Gorman 
430b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
431b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
432748446bbSMel Gorman 
433c67fe375SMel Gorman 	/* If locked we can use the interrupt unsafe versions */
434c67fe375SMel Gorman 	if (locked) {
435b9e84ac1SMinchan Kim 		__mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
436b9e84ac1SMinchan Kim 		__mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
437c67fe375SMel Gorman 	} else {
438c67fe375SMel Gorman 		mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
439c67fe375SMel Gorman 		mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
440c67fe375SMel Gorman 	}
441748446bbSMel Gorman }
442748446bbSMel Gorman 
443748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
444748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
445748446bbSMel Gorman {
446bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
447748446bbSMel Gorman 
448748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
449748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
450bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
451bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
452748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
453748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
454748446bbSMel Gorman 
455bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
456748446bbSMel Gorman }
457748446bbSMel Gorman 
4582fe86e00SMichal Nazarewicz /**
4592fe86e00SMichal Nazarewicz  * isolate_migratepages_range() - isolate all migrate-able pages in range.
4602fe86e00SMichal Nazarewicz  * @zone:	Zone pages are in.
4612fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
4622fe86e00SMichal Nazarewicz  * @low_pfn:	The first PFN of the range.
4632fe86e00SMichal Nazarewicz  * @end_pfn:	The one-past-the-last PFN of the range.
464e46a2879SMinchan Kim  * @unevictable: true if it allows to isolate unevictable pages
4652fe86e00SMichal Nazarewicz  *
4662fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
4672fe86e00SMichal Nazarewicz  * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
4682fe86e00SMichal Nazarewicz  * pending), otherwise PFN of the first page that was not scanned
4692fe86e00SMichal Nazarewicz  * (which may be both less, equal to or more then end_pfn).
4702fe86e00SMichal Nazarewicz  *
4712fe86e00SMichal Nazarewicz  * Assumes that cc->migratepages is empty and cc->nr_migratepages is
4722fe86e00SMichal Nazarewicz  * zero.
4732fe86e00SMichal Nazarewicz  *
4742fe86e00SMichal Nazarewicz  * Apart from cc->migratepages and cc->nr_migratetypes this function
4752fe86e00SMichal Nazarewicz  * does not modify any cc's fields, in particular it does not modify
4762fe86e00SMichal Nazarewicz  * (or read for that matter) cc->migrate_pfn.
477748446bbSMel Gorman  */
478ff9543fdSMichal Nazarewicz unsigned long
4792fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
480e46a2879SMinchan Kim 		unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
481748446bbSMel Gorman {
4829927af74SMel Gorman 	unsigned long last_pageblock_nr = 0, pageblock_nr;
483b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
484748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
485f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t mode = 0;
486fa9add64SHugh Dickins 	struct lruvec *lruvec;
487c67fe375SMel Gorman 	unsigned long flags;
4882a1402aaSMel Gorman 	bool locked = false;
489bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
490748446bbSMel Gorman 
491748446bbSMel Gorman 	/*
492748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
493748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
494748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
495748446bbSMel Gorman 	 */
496748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
497f9e35b3bSMel Gorman 		/* async migration should just abort */
49868e3e926SLinus Torvalds 		if (!cc->sync)
4992fe86e00SMichal Nazarewicz 			return 0;
500f9e35b3bSMel Gorman 
501748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
502748446bbSMel Gorman 
503748446bbSMel Gorman 		if (fatal_signal_pending(current))
5042fe86e00SMichal Nazarewicz 			return 0;
505748446bbSMel Gorman 	}
506748446bbSMel Gorman 
507748446bbSMel Gorman 	/* Time to isolate some pages for migration */
508b2eef8c0SAndrea Arcangeli 	cond_resched();
509748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
510b2eef8c0SAndrea Arcangeli 		/* give a chance to irqs before checking need_resched() */
5112a1402aaSMel Gorman 		if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
5122a1402aaSMel Gorman 			if (should_release_lock(&zone->lru_lock)) {
513c67fe375SMel Gorman 				spin_unlock_irqrestore(&zone->lru_lock, flags);
514b2eef8c0SAndrea Arcangeli 				locked = false;
515b2eef8c0SAndrea Arcangeli 			}
5162a1402aaSMel Gorman 		}
517b2eef8c0SAndrea Arcangeli 
5180bf380bcSMel Gorman 		/*
5190bf380bcSMel Gorman 		 * migrate_pfn does not necessarily start aligned to a
5200bf380bcSMel Gorman 		 * pageblock. Ensure that pfn_valid is called when moving
5210bf380bcSMel Gorman 		 * into a new MAX_ORDER_NR_PAGES range in case of large
5220bf380bcSMel Gorman 		 * memory holes within the zone
5230bf380bcSMel Gorman 		 */
5240bf380bcSMel Gorman 		if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
5250bf380bcSMel Gorman 			if (!pfn_valid(low_pfn)) {
5260bf380bcSMel Gorman 				low_pfn += MAX_ORDER_NR_PAGES - 1;
5270bf380bcSMel Gorman 				continue;
5280bf380bcSMel Gorman 			}
5290bf380bcSMel Gorman 		}
5300bf380bcSMel Gorman 
531748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
532748446bbSMel Gorman 			continue;
533b7aba698SMel Gorman 		nr_scanned++;
534748446bbSMel Gorman 
535dc908600SMel Gorman 		/*
536dc908600SMel Gorman 		 * Get the page and ensure the page is within the same zone.
537dc908600SMel Gorman 		 * See the comment in isolate_freepages about overlapping
538dc908600SMel Gorman 		 * nodes. It is deliberate that the new zone lock is not taken
539dc908600SMel Gorman 		 * as memory compaction should not move pages between nodes.
540dc908600SMel Gorman 		 */
541748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
542dc908600SMel Gorman 		if (page_zone(page) != zone)
543dc908600SMel Gorman 			continue;
544dc908600SMel Gorman 
545bb13ffebSMel Gorman 		if (!valid_page)
546bb13ffebSMel Gorman 			valid_page = page;
547bb13ffebSMel Gorman 
548bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
549bb13ffebSMel Gorman 		pageblock_nr = low_pfn >> pageblock_order;
550bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
551bb13ffebSMel Gorman 			goto next_pageblock;
552bb13ffebSMel Gorman 
553dc908600SMel Gorman 		/* Skip if free */
554748446bbSMel Gorman 		if (PageBuddy(page))
555748446bbSMel Gorman 			continue;
556748446bbSMel Gorman 
5579927af74SMel Gorman 		/*
5589927af74SMel Gorman 		 * For async migration, also only scan in MOVABLE blocks. Async
5599927af74SMel Gorman 		 * migration is optimistic to see if the minimum amount of work
5609927af74SMel Gorman 		 * satisfies the allocation
5619927af74SMel Gorman 		 */
56268e3e926SLinus Torvalds 		if (!cc->sync && last_pageblock_nr != pageblock_nr &&
56347118af0SMichal Nazarewicz 		    !migrate_async_suitable(get_pageblock_migratetype(page))) {
564c89511abSMel Gorman 			cc->finished_update_migrate = true;
5652a1402aaSMel Gorman 			goto next_pageblock;
5669927af74SMel Gorman 		}
5679927af74SMel Gorman 
5682a1402aaSMel Gorman 		/* Check may be lockless but that's ok as we recheck later */
569bc835011SAndrea Arcangeli 		if (!PageLRU(page))
570bc835011SAndrea Arcangeli 			continue;
571bc835011SAndrea Arcangeli 
572bc835011SAndrea Arcangeli 		/*
5732a1402aaSMel Gorman 		 * PageLRU is set. lru_lock normally excludes isolation
5742a1402aaSMel Gorman 		 * splitting and collapsing (collapsing has already happened
5752a1402aaSMel Gorman 		 * if PageLRU is set) but the lock is not necessarily taken
5762a1402aaSMel Gorman 		 * here and it is wasteful to take it just to check transhuge.
5772a1402aaSMel Gorman 		 * Check TransHuge without lock and skip the whole pageblock if
5782a1402aaSMel Gorman 		 * it's either a transhuge or hugetlbfs page, as calling
5792a1402aaSMel Gorman 		 * compound_order() without preventing THP from splitting the
5802a1402aaSMel Gorman 		 * page underneath us may return surprising results.
581bc835011SAndrea Arcangeli 		 */
582bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
5832a1402aaSMel Gorman 			if (!locked)
5842a1402aaSMel Gorman 				goto next_pageblock;
5852a1402aaSMel Gorman 			low_pfn += (1 << compound_order(page)) - 1;
5862a1402aaSMel Gorman 			continue;
5872a1402aaSMel Gorman 		}
5882a1402aaSMel Gorman 
5892a1402aaSMel Gorman 		/* Check if it is ok to still hold the lock */
5902a1402aaSMel Gorman 		locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
5912a1402aaSMel Gorman 								locked, cc);
5922a1402aaSMel Gorman 		if (!locked || fatal_signal_pending(current))
5932a1402aaSMel Gorman 			break;
5942a1402aaSMel Gorman 
5952a1402aaSMel Gorman 		/* Recheck PageLRU and PageTransHuge under lock */
5962a1402aaSMel Gorman 		if (!PageLRU(page))
5972a1402aaSMel Gorman 			continue;
5982a1402aaSMel Gorman 		if (PageTransHuge(page)) {
599bc835011SAndrea Arcangeli 			low_pfn += (1 << compound_order(page)) - 1;
600bc835011SAndrea Arcangeli 			continue;
601bc835011SAndrea Arcangeli 		}
602bc835011SAndrea Arcangeli 
60368e3e926SLinus Torvalds 		if (!cc->sync)
604c8244935SMel Gorman 			mode |= ISOLATE_ASYNC_MIGRATE;
605c8244935SMel Gorman 
606e46a2879SMinchan Kim 		if (unevictable)
607e46a2879SMinchan Kim 			mode |= ISOLATE_UNEVICTABLE;
608e46a2879SMinchan Kim 
609fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
610fa9add64SHugh Dickins 
611748446bbSMel Gorman 		/* Try isolate the page */
612f3fd4a61SKonstantin Khlebnikov 		if (__isolate_lru_page(page, mode) != 0)
613748446bbSMel Gorman 			continue;
614748446bbSMel Gorman 
615bc835011SAndrea Arcangeli 		VM_BUG_ON(PageTransCompound(page));
616bc835011SAndrea Arcangeli 
617748446bbSMel Gorman 		/* Successfully isolated */
618c89511abSMel Gorman 		cc->finished_update_migrate = true;
619fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
620748446bbSMel Gorman 		list_add(&page->lru, migratelist);
621748446bbSMel Gorman 		cc->nr_migratepages++;
622b7aba698SMel Gorman 		nr_isolated++;
623748446bbSMel Gorman 
624748446bbSMel Gorman 		/* Avoid isolating too much */
62531b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
62631b8384aSHillf Danton 			++low_pfn;
627748446bbSMel Gorman 			break;
628748446bbSMel Gorman 		}
6292a1402aaSMel Gorman 
6302a1402aaSMel Gorman 		continue;
6312a1402aaSMel Gorman 
6322a1402aaSMel Gorman next_pageblock:
6332a1402aaSMel Gorman 		low_pfn += pageblock_nr_pages;
6342a1402aaSMel Gorman 		low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
6352a1402aaSMel Gorman 		last_pageblock_nr = pageblock_nr;
63631b8384aSHillf Danton 	}
637748446bbSMel Gorman 
638c67fe375SMel Gorman 	acct_isolated(zone, locked, cc);
639748446bbSMel Gorman 
640c67fe375SMel Gorman 	if (locked)
641c67fe375SMel Gorman 		spin_unlock_irqrestore(&zone->lru_lock, flags);
642748446bbSMel Gorman 
643bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
644bb13ffebSMel Gorman 	if (low_pfn == end_pfn)
645c89511abSMel Gorman 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
646bb13ffebSMel Gorman 
647b7aba698SMel Gorman 	trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
648b7aba698SMel Gorman 
6492fe86e00SMichal Nazarewicz 	return low_pfn;
6502fe86e00SMichal Nazarewicz }
6512fe86e00SMichal Nazarewicz 
652ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
653ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
654ff9543fdSMichal Nazarewicz /*
655ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
656ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
657ff9543fdSMichal Nazarewicz  */
658ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone,
659ff9543fdSMichal Nazarewicz 				struct compact_control *cc)
660ff9543fdSMichal Nazarewicz {
661ff9543fdSMichal Nazarewicz 	struct page *page;
662ff9543fdSMichal Nazarewicz 	unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
663ff9543fdSMichal Nazarewicz 	int nr_freepages = cc->nr_freepages;
664ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
6652fe86e00SMichal Nazarewicz 
666ff9543fdSMichal Nazarewicz 	/*
667ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
668ff9543fdSMichal Nazarewicz 	 * scanned from (or the end of the zone if starting). The low point
669ff9543fdSMichal Nazarewicz 	 * is the end of the pageblock the migration scanner is using.
670ff9543fdSMichal Nazarewicz 	 */
671ff9543fdSMichal Nazarewicz 	pfn = cc->free_pfn;
672ff9543fdSMichal Nazarewicz 	low_pfn = cc->migrate_pfn + pageblock_nr_pages;
6732fe86e00SMichal Nazarewicz 
674ff9543fdSMichal Nazarewicz 	/*
675ff9543fdSMichal Nazarewicz 	 * Take care that if the migration scanner is at the end of the zone
676ff9543fdSMichal Nazarewicz 	 * that the free scanner does not accidentally move to the next zone
677ff9543fdSMichal Nazarewicz 	 * in the next isolation cycle.
678ff9543fdSMichal Nazarewicz 	 */
679ff9543fdSMichal Nazarewicz 	high_pfn = min(low_pfn, pfn);
680ff9543fdSMichal Nazarewicz 
681ff9543fdSMichal Nazarewicz 	zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
682ff9543fdSMichal Nazarewicz 
683ff9543fdSMichal Nazarewicz 	/*
684ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
685ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
686ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
687ff9543fdSMichal Nazarewicz 	 */
688ff9543fdSMichal Nazarewicz 	for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
689ff9543fdSMichal Nazarewicz 					pfn -= pageblock_nr_pages) {
690ff9543fdSMichal Nazarewicz 		unsigned long isolated;
691ff9543fdSMichal Nazarewicz 
692ff9543fdSMichal Nazarewicz 		if (!pfn_valid(pfn))
693ff9543fdSMichal Nazarewicz 			continue;
694ff9543fdSMichal Nazarewicz 
695ff9543fdSMichal Nazarewicz 		/*
696ff9543fdSMichal Nazarewicz 		 * Check for overlapping nodes/zones. It's possible on some
697ff9543fdSMichal Nazarewicz 		 * configurations to have a setup like
698ff9543fdSMichal Nazarewicz 		 * node0 node1 node0
699ff9543fdSMichal Nazarewicz 		 * i.e. it's possible that all pages within a zones range of
700ff9543fdSMichal Nazarewicz 		 * pages do not belong to a single zone.
701ff9543fdSMichal Nazarewicz 		 */
702ff9543fdSMichal Nazarewicz 		page = pfn_to_page(pfn);
703ff9543fdSMichal Nazarewicz 		if (page_zone(page) != zone)
704ff9543fdSMichal Nazarewicz 			continue;
705ff9543fdSMichal Nazarewicz 
706ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
70768e3e926SLinus Torvalds 		if (!suitable_migration_target(page))
708ff9543fdSMichal Nazarewicz 			continue;
70968e3e926SLinus Torvalds 
710bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
711bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
712bb13ffebSMel Gorman 			continue;
713bb13ffebSMel Gorman 
714f40d1e42SMel Gorman 		/* Found a block suitable for isolating free pages from */
715ff9543fdSMichal Nazarewicz 		isolated = 0;
716ff9543fdSMichal Nazarewicz 		end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
717f40d1e42SMel Gorman 		isolated = isolate_freepages_block(cc, pfn, end_pfn,
718ff9543fdSMichal Nazarewicz 						   freelist, false);
719ff9543fdSMichal Nazarewicz 		nr_freepages += isolated;
720ff9543fdSMichal Nazarewicz 
721ff9543fdSMichal Nazarewicz 		/*
722ff9543fdSMichal Nazarewicz 		 * Record the highest PFN we isolated pages from. When next
723ff9543fdSMichal Nazarewicz 		 * looking for free pages, the search will restart here as
724ff9543fdSMichal Nazarewicz 		 * page migration may have returned some pages to the allocator
725ff9543fdSMichal Nazarewicz 		 */
726c89511abSMel Gorman 		if (isolated) {
727c89511abSMel Gorman 			cc->finished_update_free = true;
728ff9543fdSMichal Nazarewicz 			high_pfn = max(high_pfn, pfn);
729ff9543fdSMichal Nazarewicz 		}
730c89511abSMel Gorman 	}
731ff9543fdSMichal Nazarewicz 
732ff9543fdSMichal Nazarewicz 	/* split_free_page does not map the pages */
733ff9543fdSMichal Nazarewicz 	map_pages(freelist);
734ff9543fdSMichal Nazarewicz 
735ff9543fdSMichal Nazarewicz 	cc->free_pfn = high_pfn;
736ff9543fdSMichal Nazarewicz 	cc->nr_freepages = nr_freepages;
737748446bbSMel Gorman }
738748446bbSMel Gorman 
739748446bbSMel Gorman /*
740748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
741748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
742748446bbSMel Gorman  */
743748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
744748446bbSMel Gorman 					unsigned long data,
745748446bbSMel Gorman 					int **result)
746748446bbSMel Gorman {
747748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
748748446bbSMel Gorman 	struct page *freepage;
749748446bbSMel Gorman 
750748446bbSMel Gorman 	/* Isolate free pages if necessary */
751748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
752748446bbSMel Gorman 		isolate_freepages(cc->zone, cc);
753748446bbSMel Gorman 
754748446bbSMel Gorman 		if (list_empty(&cc->freepages))
755748446bbSMel Gorman 			return NULL;
756748446bbSMel Gorman 	}
757748446bbSMel Gorman 
758748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
759748446bbSMel Gorman 	list_del(&freepage->lru);
760748446bbSMel Gorman 	cc->nr_freepages--;
761748446bbSMel Gorman 
762748446bbSMel Gorman 	return freepage;
763748446bbSMel Gorman }
764748446bbSMel Gorman 
765748446bbSMel Gorman /*
766748446bbSMel Gorman  * We cannot control nr_migratepages and nr_freepages fully when migration is
767748446bbSMel Gorman  * running as migrate_pages() has no knowledge of compact_control. When
768748446bbSMel Gorman  * migration is complete, we count the number of pages on the lists by hand.
769748446bbSMel Gorman  */
770748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc)
771748446bbSMel Gorman {
772748446bbSMel Gorman 	int nr_migratepages = 0;
773748446bbSMel Gorman 	int nr_freepages = 0;
774748446bbSMel Gorman 	struct page *page;
775748446bbSMel Gorman 
776748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru)
777748446bbSMel Gorman 		nr_migratepages++;
778748446bbSMel Gorman 	list_for_each_entry(page, &cc->freepages, lru)
779748446bbSMel Gorman 		nr_freepages++;
780748446bbSMel Gorman 
781748446bbSMel Gorman 	cc->nr_migratepages = nr_migratepages;
782748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
783748446bbSMel Gorman }
784748446bbSMel Gorman 
785ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
786ff9543fdSMichal Nazarewicz typedef enum {
787ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
788ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
789ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
790ff9543fdSMichal Nazarewicz } isolate_migrate_t;
791ff9543fdSMichal Nazarewicz 
792ff9543fdSMichal Nazarewicz /*
793ff9543fdSMichal Nazarewicz  * Isolate all pages that can be migrated from the block pointed to by
794ff9543fdSMichal Nazarewicz  * the migrate scanner within compact_control.
795ff9543fdSMichal Nazarewicz  */
796ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
797ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
798ff9543fdSMichal Nazarewicz {
799ff9543fdSMichal Nazarewicz 	unsigned long low_pfn, end_pfn;
800ff9543fdSMichal Nazarewicz 
801ff9543fdSMichal Nazarewicz 	/* Do not scan outside zone boundaries */
802ff9543fdSMichal Nazarewicz 	low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
803ff9543fdSMichal Nazarewicz 
804ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
805ff9543fdSMichal Nazarewicz 	end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
806ff9543fdSMichal Nazarewicz 
807ff9543fdSMichal Nazarewicz 	/* Do not cross the free scanner or scan within a memory hole */
808ff9543fdSMichal Nazarewicz 	if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
809ff9543fdSMichal Nazarewicz 		cc->migrate_pfn = end_pfn;
810ff9543fdSMichal Nazarewicz 		return ISOLATE_NONE;
811ff9543fdSMichal Nazarewicz 	}
812ff9543fdSMichal Nazarewicz 
813ff9543fdSMichal Nazarewicz 	/* Perform the isolation */
814e46a2879SMinchan Kim 	low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
815e64c5237SShaohua Li 	if (!low_pfn || cc->contended)
816ff9543fdSMichal Nazarewicz 		return ISOLATE_ABORT;
817ff9543fdSMichal Nazarewicz 
818ff9543fdSMichal Nazarewicz 	cc->migrate_pfn = low_pfn;
819ff9543fdSMichal Nazarewicz 
820ff9543fdSMichal Nazarewicz 	return ISOLATE_SUCCESS;
821ff9543fdSMichal Nazarewicz }
822ff9543fdSMichal Nazarewicz 
823748446bbSMel Gorman static int compact_finished(struct zone *zone,
824748446bbSMel Gorman 			    struct compact_control *cc)
825748446bbSMel Gorman {
8265a03b051SAndrea Arcangeli 	unsigned long watermark;
82756de7263SMel Gorman 
828748446bbSMel Gorman 	if (fatal_signal_pending(current))
829748446bbSMel Gorman 		return COMPACT_PARTIAL;
830748446bbSMel Gorman 
831753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
832bb13ffebSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn) {
83362997027SMel Gorman 		/*
83462997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
83562997027SMel Gorman 		 * by kswapd when it goes to sleep. kswapd does not set the
83662997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
83762997027SMel Gorman 		 * based on an allocation request.
83862997027SMel Gorman 		 */
83962997027SMel Gorman 		if (!current_is_kswapd())
84062997027SMel Gorman 			zone->compact_blockskip_flush = true;
84162997027SMel Gorman 
842748446bbSMel Gorman 		return COMPACT_COMPLETE;
843bb13ffebSMel Gorman 	}
844748446bbSMel Gorman 
84582478fb7SJohannes Weiner 	/*
84682478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
84782478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
84882478fb7SJohannes Weiner 	 */
84956de7263SMel Gorman 	if (cc->order == -1)
85056de7263SMel Gorman 		return COMPACT_CONTINUE;
85156de7263SMel Gorman 
8523957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
8533957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
8543957c776SMichal Hocko 	watermark += (1 << cc->order);
8553957c776SMichal Hocko 
8563957c776SMichal Hocko 	if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
8573957c776SMichal Hocko 		return COMPACT_CONTINUE;
8583957c776SMichal Hocko 
85956de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
8601fb3f8caSMel Gorman 	if (cc->page) {
8611fb3f8caSMel Gorman 		/* Was a suitable page captured? */
8621fb3f8caSMel Gorman 		if (*cc->page)
8631fb3f8caSMel Gorman 			return COMPACT_PARTIAL;
8641fb3f8caSMel Gorman 	} else {
8651fb3f8caSMel Gorman 		unsigned int order;
86656de7263SMel Gorman 		for (order = cc->order; order < MAX_ORDER; order++) {
8671fb3f8caSMel Gorman 			struct free_area *area = &zone->free_area[cc->order];
86856de7263SMel Gorman 			/* Job done if page is free of the right migratetype */
8691fb3f8caSMel Gorman 			if (!list_empty(&area->free_list[cc->migratetype]))
87056de7263SMel Gorman 				return COMPACT_PARTIAL;
87156de7263SMel Gorman 
87256de7263SMel Gorman 			/* Job done if allocation would set block type */
8731fb3f8caSMel Gorman 			if (cc->order >= pageblock_order && area->nr_free)
87456de7263SMel Gorman 				return COMPACT_PARTIAL;
87556de7263SMel Gorman 		}
8761fb3f8caSMel Gorman 	}
87756de7263SMel Gorman 
878748446bbSMel Gorman 	return COMPACT_CONTINUE;
879748446bbSMel Gorman }
880748446bbSMel Gorman 
8813e7d3449SMel Gorman /*
8823e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
8833e7d3449SMel Gorman  * Returns
8843e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
8853e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
8863e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
8873e7d3449SMel Gorman  */
8883e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order)
8893e7d3449SMel Gorman {
8903e7d3449SMel Gorman 	int fragindex;
8913e7d3449SMel Gorman 	unsigned long watermark;
8923e7d3449SMel Gorman 
8933e7d3449SMel Gorman 	/*
8943957c776SMichal Hocko 	 * order == -1 is expected when compacting via
8953957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
8963957c776SMichal Hocko 	 */
8973957c776SMichal Hocko 	if (order == -1)
8983957c776SMichal Hocko 		return COMPACT_CONTINUE;
8993957c776SMichal Hocko 
9003957c776SMichal Hocko 	/*
9013e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
9023e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
9033e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
9043e7d3449SMel Gorman 	 */
9053e7d3449SMel Gorman 	watermark = low_wmark_pages(zone) + (2UL << order);
9063e7d3449SMel Gorman 	if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
9073e7d3449SMel Gorman 		return COMPACT_SKIPPED;
9083e7d3449SMel Gorman 
9093e7d3449SMel Gorman 	/*
9103e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
9113e7d3449SMel Gorman 	 * low memory or external fragmentation
9123e7d3449SMel Gorman 	 *
913a582a738SShaohua Li 	 * index of -1000 implies allocations might succeed depending on
914a582a738SShaohua Li 	 * watermarks
9153e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
9163e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
9173e7d3449SMel Gorman 	 *
9183e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
9193e7d3449SMel Gorman 	 */
9203e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
9213e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
9223e7d3449SMel Gorman 		return COMPACT_SKIPPED;
9233e7d3449SMel Gorman 
924a582a738SShaohua Li 	if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
925a582a738SShaohua Li 	    0, 0))
9263e7d3449SMel Gorman 		return COMPACT_PARTIAL;
9273e7d3449SMel Gorman 
9283e7d3449SMel Gorman 	return COMPACT_CONTINUE;
9293e7d3449SMel Gorman }
9303e7d3449SMel Gorman 
931748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
932748446bbSMel Gorman {
933748446bbSMel Gorman 	int ret;
934c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
935c89511abSMel Gorman 	unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
936748446bbSMel Gorman 
9373e7d3449SMel Gorman 	ret = compaction_suitable(zone, cc->order);
9383e7d3449SMel Gorman 	switch (ret) {
9393e7d3449SMel Gorman 	case COMPACT_PARTIAL:
9403e7d3449SMel Gorman 	case COMPACT_SKIPPED:
9413e7d3449SMel Gorman 		/* Compaction is likely to fail */
9423e7d3449SMel Gorman 		return ret;
9433e7d3449SMel Gorman 	case COMPACT_CONTINUE:
9443e7d3449SMel Gorman 		/* Fall through to compaction */
9453e7d3449SMel Gorman 		;
9463e7d3449SMel Gorman 	}
9473e7d3449SMel Gorman 
948c89511abSMel Gorman 	/*
949c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
950c89511abSMel Gorman 	 * information on where the scanners should start but check that it
951c89511abSMel Gorman 	 * is initialised by ensuring the values are within zone boundaries.
952c89511abSMel Gorman 	 */
953c89511abSMel Gorman 	cc->migrate_pfn = zone->compact_cached_migrate_pfn;
954c89511abSMel Gorman 	cc->free_pfn = zone->compact_cached_free_pfn;
955c89511abSMel Gorman 	if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
956c89511abSMel Gorman 		cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
957c89511abSMel Gorman 		zone->compact_cached_free_pfn = cc->free_pfn;
958c89511abSMel Gorman 	}
959c89511abSMel Gorman 	if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
960c89511abSMel Gorman 		cc->migrate_pfn = start_pfn;
961c89511abSMel Gorman 		zone->compact_cached_migrate_pfn = cc->migrate_pfn;
962c89511abSMel Gorman 	}
963748446bbSMel Gorman 
96462997027SMel Gorman 	/*
96562997027SMel Gorman 	 * Clear pageblock skip if there were failures recently and compaction
96662997027SMel Gorman 	 * is about to be retried after being deferred. kswapd does not do
96762997027SMel Gorman 	 * this reset as it'll reset the cached information when going to sleep.
96862997027SMel Gorman 	 */
96962997027SMel Gorman 	if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
97062997027SMel Gorman 		__reset_isolation_suitable(zone);
971bb13ffebSMel Gorman 
972748446bbSMel Gorman 	migrate_prep_local();
973748446bbSMel Gorman 
974748446bbSMel Gorman 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
975748446bbSMel Gorman 		unsigned long nr_migrate, nr_remaining;
9769d502c1cSMinchan Kim 		int err;
977748446bbSMel Gorman 
978f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
979f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
980f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
981e64c5237SShaohua Li 			putback_lru_pages(&cc->migratepages);
982e64c5237SShaohua Li 			cc->nr_migratepages = 0;
983f9e35b3bSMel Gorman 			goto out;
984f9e35b3bSMel Gorman 		case ISOLATE_NONE:
985748446bbSMel Gorman 			continue;
986f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
987f9e35b3bSMel Gorman 			;
988f9e35b3bSMel Gorman 		}
989748446bbSMel Gorman 
990748446bbSMel Gorman 		nr_migrate = cc->nr_migratepages;
9919d502c1cSMinchan Kim 		err = migrate_pages(&cc->migratepages, compaction_alloc,
99268e3e926SLinus Torvalds 				(unsigned long)cc, false,
993*7b2a2d4aSMel Gorman 				cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
994*7b2a2d4aSMel Gorman 				MR_COMPACTION);
995748446bbSMel Gorman 		update_nr_listpages(cc);
996748446bbSMel Gorman 		nr_remaining = cc->nr_migratepages;
997748446bbSMel Gorman 
998b7aba698SMel Gorman 		trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
999b7aba698SMel Gorman 						nr_remaining);
1000748446bbSMel Gorman 
1001748446bbSMel Gorman 		/* Release LRU pages not migrated */
10029d502c1cSMinchan Kim 		if (err) {
1003748446bbSMel Gorman 			putback_lru_pages(&cc->migratepages);
1004748446bbSMel Gorman 			cc->nr_migratepages = 0;
10054bf2bba3SDavid Rientjes 			if (err == -ENOMEM) {
10064bf2bba3SDavid Rientjes 				ret = COMPACT_PARTIAL;
10074bf2bba3SDavid Rientjes 				goto out;
1008748446bbSMel Gorman 			}
10094bf2bba3SDavid Rientjes 		}
10101fb3f8caSMel Gorman 
10111fb3f8caSMel Gorman 		/* Capture a page now if it is a suitable size */
10121fb3f8caSMel Gorman 		compact_capture_page(cc);
1013748446bbSMel Gorman 	}
1014748446bbSMel Gorman 
1015f9e35b3bSMel Gorman out:
1016748446bbSMel Gorman 	/* Release free pages and check accounting */
1017748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
1018748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
1019748446bbSMel Gorman 
1020748446bbSMel Gorman 	return ret;
1021748446bbSMel Gorman }
102276ab0f53SMel Gorman 
1023d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone,
102477f1fe6bSMel Gorman 				 int order, gfp_t gfp_mask,
10251fb3f8caSMel Gorman 				 bool sync, bool *contended,
10261fb3f8caSMel Gorman 				 struct page **page)
102756de7263SMel Gorman {
1028e64c5237SShaohua Li 	unsigned long ret;
102956de7263SMel Gorman 	struct compact_control cc = {
103056de7263SMel Gorman 		.nr_freepages = 0,
103156de7263SMel Gorman 		.nr_migratepages = 0,
103256de7263SMel Gorman 		.order = order,
103356de7263SMel Gorman 		.migratetype = allocflags_to_migratetype(gfp_mask),
103456de7263SMel Gorman 		.zone = zone,
103568e3e926SLinus Torvalds 		.sync = sync,
10361fb3f8caSMel Gorman 		.page = page,
103756de7263SMel Gorman 	};
103856de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
103956de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
104056de7263SMel Gorman 
1041e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1042e64c5237SShaohua Li 
1043e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1044e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1045e64c5237SShaohua Li 
1046e64c5237SShaohua Li 	*contended = cc.contended;
1047e64c5237SShaohua Li 	return ret;
104856de7263SMel Gorman }
104956de7263SMel Gorman 
10505e771905SMel Gorman int sysctl_extfrag_threshold = 500;
10515e771905SMel Gorman 
105256de7263SMel Gorman /**
105356de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
105456de7263SMel Gorman  * @zonelist: The zonelist used for the current allocation
105556de7263SMel Gorman  * @order: The order of the current allocation
105656de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
105756de7263SMel Gorman  * @nodemask: The allowed nodes to allocate from
105877f1fe6bSMel Gorman  * @sync: Whether migration is synchronous or not
1059661c4cb9SMel Gorman  * @contended: Return value that is true if compaction was aborted due to lock contention
1060661c4cb9SMel Gorman  * @page: Optionally capture a free page of the requested order during compaction
106156de7263SMel Gorman  *
106256de7263SMel Gorman  * This is the main entry point for direct page compaction.
106356de7263SMel Gorman  */
106456de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist,
106577f1fe6bSMel Gorman 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
10661fb3f8caSMel Gorman 			bool sync, bool *contended, struct page **page)
106756de7263SMel Gorman {
106856de7263SMel Gorman 	enum zone_type high_zoneidx = gfp_zone(gfp_mask);
106956de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
107056de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
107156de7263SMel Gorman 	struct zoneref *z;
107256de7263SMel Gorman 	struct zone *zone;
107356de7263SMel Gorman 	int rc = COMPACT_SKIPPED;
1074d95ea5d1SBartlomiej Zolnierkiewicz 	int alloc_flags = 0;
107556de7263SMel Gorman 
10764ffb6335SMel Gorman 	/* Check if the GFP flags allow compaction */
1077c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
107856de7263SMel Gorman 		return rc;
107956de7263SMel Gorman 
108056de7263SMel Gorman 	count_vm_event(COMPACTSTALL);
108156de7263SMel Gorman 
1082d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA
1083d95ea5d1SBartlomiej Zolnierkiewicz 	if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1084d95ea5d1SBartlomiej Zolnierkiewicz 		alloc_flags |= ALLOC_CMA;
1085d95ea5d1SBartlomiej Zolnierkiewicz #endif
108656de7263SMel Gorman 	/* Compact each zone in the list */
108756de7263SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
108856de7263SMel Gorman 								nodemask) {
108956de7263SMel Gorman 		int status;
109056de7263SMel Gorman 
1091c67fe375SMel Gorman 		status = compact_zone_order(zone, order, gfp_mask, sync,
10921fb3f8caSMel Gorman 						contended, page);
109356de7263SMel Gorman 		rc = max(status, rc);
109456de7263SMel Gorman 
10953e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
1096d95ea5d1SBartlomiej Zolnierkiewicz 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1097d95ea5d1SBartlomiej Zolnierkiewicz 				      alloc_flags))
109856de7263SMel Gorman 			break;
109956de7263SMel Gorman 	}
110056de7263SMel Gorman 
110156de7263SMel Gorman 	return rc;
110256de7263SMel Gorman }
110356de7263SMel Gorman 
110456de7263SMel Gorman 
110576ab0f53SMel Gorman /* Compact all zones within a node */
11067be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
110776ab0f53SMel Gorman {
110876ab0f53SMel Gorman 	int zoneid;
110976ab0f53SMel Gorman 	struct zone *zone;
111076ab0f53SMel Gorman 
111176ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
111276ab0f53SMel Gorman 
111376ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
111476ab0f53SMel Gorman 		if (!populated_zone(zone))
111576ab0f53SMel Gorman 			continue;
111676ab0f53SMel Gorman 
11177be62de9SRik van Riel 		cc->nr_freepages = 0;
11187be62de9SRik van Riel 		cc->nr_migratepages = 0;
11197be62de9SRik van Riel 		cc->zone = zone;
11207be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
11217be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
112276ab0f53SMel Gorman 
1123aad6ec37SDan Carpenter 		if (cc->order == -1 || !compaction_deferred(zone, cc->order))
11247be62de9SRik van Riel 			compact_zone(zone, cc);
112576ab0f53SMel Gorman 
1126aff62249SRik van Riel 		if (cc->order > 0) {
1127aff62249SRik van Riel 			int ok = zone_watermark_ok(zone, cc->order,
1128aff62249SRik van Riel 						low_wmark_pages(zone), 0, 0);
1129c81758fbSMinchan Kim 			if (ok && cc->order >= zone->compact_order_failed)
1130aff62249SRik van Riel 				zone->compact_order_failed = cc->order + 1;
1131aff62249SRik van Riel 			/* Currently async compaction is never deferred. */
113268e3e926SLinus Torvalds 			else if (!ok && cc->sync)
1133aff62249SRik van Riel 				defer_compaction(zone, cc->order);
1134aff62249SRik van Riel 		}
1135aff62249SRik van Riel 
11367be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->freepages));
11377be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->migratepages));
113876ab0f53SMel Gorman 	}
113976ab0f53SMel Gorman 
114076ab0f53SMel Gorman 	return 0;
114176ab0f53SMel Gorman }
114276ab0f53SMel Gorman 
11437be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order)
11447be62de9SRik van Riel {
11457be62de9SRik van Riel 	struct compact_control cc = {
11467be62de9SRik van Riel 		.order = order,
114768e3e926SLinus Torvalds 		.sync = false,
11481fb3f8caSMel Gorman 		.page = NULL,
11497be62de9SRik van Riel 	};
11507be62de9SRik van Riel 
11517be62de9SRik van Riel 	return __compact_pgdat(pgdat, &cc);
11527be62de9SRik van Riel }
11537be62de9SRik van Riel 
11547be62de9SRik van Riel static int compact_node(int nid)
11557be62de9SRik van Riel {
11567be62de9SRik van Riel 	struct compact_control cc = {
11577be62de9SRik van Riel 		.order = -1,
115868e3e926SLinus Torvalds 		.sync = true,
11591fb3f8caSMel Gorman 		.page = NULL,
11607be62de9SRik van Riel 	};
11617be62de9SRik van Riel 
11628575ec29SHugh Dickins 	return __compact_pgdat(NODE_DATA(nid), &cc);
11637be62de9SRik van Riel }
11647be62de9SRik van Riel 
116576ab0f53SMel Gorman /* Compact all nodes in the system */
116676ab0f53SMel Gorman static int compact_nodes(void)
116776ab0f53SMel Gorman {
116876ab0f53SMel Gorman 	int nid;
116976ab0f53SMel Gorman 
11708575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
11718575ec29SHugh Dickins 	lru_add_drain_all();
11728575ec29SHugh Dickins 
117376ab0f53SMel Gorman 	for_each_online_node(nid)
117476ab0f53SMel Gorman 		compact_node(nid);
117576ab0f53SMel Gorman 
117676ab0f53SMel Gorman 	return COMPACT_COMPLETE;
117776ab0f53SMel Gorman }
117876ab0f53SMel Gorman 
117976ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
118076ab0f53SMel Gorman int sysctl_compact_memory;
118176ab0f53SMel Gorman 
118276ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
118376ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
118476ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
118576ab0f53SMel Gorman {
118676ab0f53SMel Gorman 	if (write)
118776ab0f53SMel Gorman 		return compact_nodes();
118876ab0f53SMel Gorman 
118976ab0f53SMel Gorman 	return 0;
119076ab0f53SMel Gorman }
1191ed4a6d7fSMel Gorman 
11925e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
11935e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
11945e771905SMel Gorman {
11955e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
11965e771905SMel Gorman 
11975e771905SMel Gorman 	return 0;
11985e771905SMel Gorman }
11995e771905SMel Gorman 
1200ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
120110fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev,
120210fbcf4cSKay Sievers 			struct device_attribute *attr,
1203ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1204ed4a6d7fSMel Gorman {
12058575ec29SHugh Dickins 	int nid = dev->id;
12068575ec29SHugh Dickins 
12078575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
12088575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
12098575ec29SHugh Dickins 		lru_add_drain_all();
12108575ec29SHugh Dickins 
12118575ec29SHugh Dickins 		compact_node(nid);
12128575ec29SHugh Dickins 	}
1213ed4a6d7fSMel Gorman 
1214ed4a6d7fSMel Gorman 	return count;
1215ed4a6d7fSMel Gorman }
121610fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1217ed4a6d7fSMel Gorman 
1218ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1219ed4a6d7fSMel Gorman {
122010fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1221ed4a6d7fSMel Gorman }
1222ed4a6d7fSMel Gorman 
1223ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1224ed4a6d7fSMel Gorman {
122510fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1226ed4a6d7fSMel Gorman }
1227ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1228ff9543fdSMichal Nazarewicz 
1229ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
1230