xref: /openbmc/linux/mm/compaction.c (revision 3a7200af3d9227767869f451ed747aff07d8df48)
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 
70bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION
71bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */
72bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
73bb13ffebSMel Gorman 					struct page *page)
74bb13ffebSMel Gorman {
75bb13ffebSMel Gorman 	if (cc->ignore_skip_hint)
76bb13ffebSMel Gorman 		return true;
77bb13ffebSMel Gorman 
78bb13ffebSMel Gorman 	return !get_pageblock_skip(page);
79bb13ffebSMel Gorman }
80bb13ffebSMel Gorman 
81bb13ffebSMel Gorman /*
82bb13ffebSMel Gorman  * This function is called to clear all cached information on pageblocks that
83bb13ffebSMel Gorman  * should be skipped for page isolation when the migrate and free page scanner
84bb13ffebSMel Gorman  * meet.
85bb13ffebSMel Gorman  */
8662997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone)
87bb13ffebSMel Gorman {
88bb13ffebSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
89108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
90bb13ffebSMel Gorman 	unsigned long pfn;
91bb13ffebSMel Gorman 
92c89511abSMel Gorman 	zone->compact_cached_migrate_pfn = start_pfn;
93c89511abSMel Gorman 	zone->compact_cached_free_pfn = end_pfn;
9462997027SMel Gorman 	zone->compact_blockskip_flush = false;
95bb13ffebSMel Gorman 
96bb13ffebSMel Gorman 	/* Walk the zone and mark every pageblock as suitable for isolation */
97bb13ffebSMel Gorman 	for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
98bb13ffebSMel Gorman 		struct page *page;
99bb13ffebSMel Gorman 
100bb13ffebSMel Gorman 		cond_resched();
101bb13ffebSMel Gorman 
102bb13ffebSMel Gorman 		if (!pfn_valid(pfn))
103bb13ffebSMel Gorman 			continue;
104bb13ffebSMel Gorman 
105bb13ffebSMel Gorman 		page = pfn_to_page(pfn);
106bb13ffebSMel Gorman 		if (zone != page_zone(page))
107bb13ffebSMel Gorman 			continue;
108bb13ffebSMel Gorman 
109bb13ffebSMel Gorman 		clear_pageblock_skip(page);
110bb13ffebSMel Gorman 	}
111bb13ffebSMel Gorman }
112bb13ffebSMel Gorman 
11362997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat)
11462997027SMel Gorman {
11562997027SMel Gorman 	int zoneid;
11662997027SMel Gorman 
11762997027SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
11862997027SMel Gorman 		struct zone *zone = &pgdat->node_zones[zoneid];
11962997027SMel Gorman 		if (!populated_zone(zone))
12062997027SMel Gorman 			continue;
12162997027SMel Gorman 
12262997027SMel Gorman 		/* Only flush if a full compaction finished recently */
12362997027SMel Gorman 		if (zone->compact_blockskip_flush)
12462997027SMel Gorman 			__reset_isolation_suitable(zone);
12562997027SMel Gorman 	}
12662997027SMel Gorman }
12762997027SMel Gorman 
128bb13ffebSMel Gorman /*
129bb13ffebSMel Gorman  * If no pages were isolated then mark this pageblock to be skipped in the
13062997027SMel Gorman  * future. The information is later cleared by __reset_isolation_suitable().
131bb13ffebSMel Gorman  */
132c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
133c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
134c89511abSMel Gorman 			bool migrate_scanner)
135bb13ffebSMel Gorman {
136c89511abSMel Gorman 	struct zone *zone = cc->zone;
137bb13ffebSMel Gorman 	if (!page)
138bb13ffebSMel Gorman 		return;
139bb13ffebSMel Gorman 
140c89511abSMel Gorman 	if (!nr_isolated) {
141c89511abSMel Gorman 		unsigned long pfn = page_to_pfn(page);
142bb13ffebSMel Gorman 		set_pageblock_skip(page);
143c89511abSMel Gorman 
144c89511abSMel Gorman 		/* Update where compaction should restart */
145c89511abSMel Gorman 		if (migrate_scanner) {
146c89511abSMel Gorman 			if (!cc->finished_update_migrate &&
147c89511abSMel Gorman 			    pfn > zone->compact_cached_migrate_pfn)
148c89511abSMel Gorman 				zone->compact_cached_migrate_pfn = pfn;
149c89511abSMel Gorman 		} else {
150c89511abSMel Gorman 			if (!cc->finished_update_free &&
151c89511abSMel Gorman 			    pfn < zone->compact_cached_free_pfn)
152c89511abSMel Gorman 				zone->compact_cached_free_pfn = pfn;
153c89511abSMel Gorman 		}
154c89511abSMel Gorman 	}
155bb13ffebSMel Gorman }
156bb13ffebSMel Gorman #else
157bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc,
158bb13ffebSMel Gorman 					struct page *page)
159bb13ffebSMel Gorman {
160bb13ffebSMel Gorman 	return true;
161bb13ffebSMel Gorman }
162bb13ffebSMel Gorman 
163c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc,
164c89511abSMel Gorman 			struct page *page, unsigned long nr_isolated,
165c89511abSMel Gorman 			bool migrate_scanner)
166bb13ffebSMel Gorman {
167bb13ffebSMel Gorman }
168bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */
169bb13ffebSMel Gorman 
1702a1402aaSMel Gorman static inline bool should_release_lock(spinlock_t *lock)
1712a1402aaSMel Gorman {
1722a1402aaSMel Gorman 	return need_resched() || spin_is_contended(lock);
1732a1402aaSMel Gorman }
1742a1402aaSMel Gorman 
17585aa125fSMichal Nazarewicz /*
176c67fe375SMel Gorman  * Compaction requires the taking of some coarse locks that are potentially
177c67fe375SMel Gorman  * very heavily contended. Check if the process needs to be scheduled or
178c67fe375SMel Gorman  * if the lock is contended. For async compaction, back out in the event
179c67fe375SMel Gorman  * if contention is severe. For sync compaction, schedule.
180c67fe375SMel Gorman  *
181c67fe375SMel Gorman  * Returns true if the lock is held.
182c67fe375SMel Gorman  * Returns false if the lock is released and compaction should abort
183c67fe375SMel Gorman  */
184c67fe375SMel Gorman static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
185c67fe375SMel Gorman 				      bool locked, struct compact_control *cc)
186c67fe375SMel Gorman {
1872a1402aaSMel Gorman 	if (should_release_lock(lock)) {
188c67fe375SMel Gorman 		if (locked) {
189c67fe375SMel Gorman 			spin_unlock_irqrestore(lock, *flags);
190c67fe375SMel Gorman 			locked = false;
191c67fe375SMel Gorman 		}
192c67fe375SMel Gorman 
193c67fe375SMel Gorman 		/* async aborts if taking too long or contended */
194c67fe375SMel Gorman 		if (!cc->sync) {
195e64c5237SShaohua Li 			cc->contended = true;
196c67fe375SMel Gorman 			return false;
197c67fe375SMel Gorman 		}
198c67fe375SMel Gorman 
199c67fe375SMel Gorman 		cond_resched();
200c67fe375SMel Gorman 	}
201c67fe375SMel Gorman 
202c67fe375SMel Gorman 	if (!locked)
203c67fe375SMel Gorman 		spin_lock_irqsave(lock, *flags);
204c67fe375SMel Gorman 	return true;
205c67fe375SMel Gorman }
206c67fe375SMel Gorman 
207c67fe375SMel Gorman static inline bool compact_trylock_irqsave(spinlock_t *lock,
208c67fe375SMel Gorman 			unsigned long *flags, struct compact_control *cc)
209c67fe375SMel Gorman {
210c67fe375SMel Gorman 	return compact_checklock_irqsave(lock, flags, false, cc);
211c67fe375SMel Gorman }
212c67fe375SMel Gorman 
213f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */
214f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page)
215f40d1e42SMel Gorman {
216f40d1e42SMel Gorman 	int migratetype = get_pageblock_migratetype(page);
217f40d1e42SMel Gorman 
218f40d1e42SMel Gorman 	/* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
219194159fbSMinchan Kim 	if (migratetype == MIGRATE_RESERVE)
220194159fbSMinchan Kim 		return false;
221194159fbSMinchan Kim 
222194159fbSMinchan Kim 	if (is_migrate_isolate(migratetype))
223f40d1e42SMel Gorman 		return false;
224f40d1e42SMel Gorman 
225f40d1e42SMel Gorman 	/* If the page is a large free page, then allow migration */
226f40d1e42SMel Gorman 	if (PageBuddy(page) && page_order(page) >= pageblock_order)
227f40d1e42SMel Gorman 		return true;
228f40d1e42SMel Gorman 
229f40d1e42SMel Gorman 	/* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
230f40d1e42SMel Gorman 	if (migrate_async_suitable(migratetype))
231f40d1e42SMel Gorman 		return true;
232f40d1e42SMel Gorman 
233f40d1e42SMel Gorman 	/* Otherwise skip the block */
234f40d1e42SMel Gorman 	return false;
235f40d1e42SMel Gorman }
236f40d1e42SMel Gorman 
237c67fe375SMel Gorman /*
23885aa125fSMichal Nazarewicz  * Isolate free pages onto a private freelist. Caller must hold zone->lock.
23985aa125fSMichal Nazarewicz  * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
24085aa125fSMichal Nazarewicz  * pages inside of the pageblock (even though it may still end up isolating
24185aa125fSMichal Nazarewicz  * some pages).
24285aa125fSMichal Nazarewicz  */
243f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc,
244f40d1e42SMel Gorman 				unsigned long blockpfn,
24585aa125fSMichal Nazarewicz 				unsigned long end_pfn,
24685aa125fSMichal Nazarewicz 				struct list_head *freelist,
24785aa125fSMichal Nazarewicz 				bool strict)
248748446bbSMel Gorman {
249b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
250bb13ffebSMel Gorman 	struct page *cursor, *valid_page = NULL;
251f40d1e42SMel Gorman 	unsigned long nr_strict_required = end_pfn - blockpfn;
252f40d1e42SMel Gorman 	unsigned long flags;
253f40d1e42SMel Gorman 	bool locked = false;
254748446bbSMel Gorman 
255748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
256748446bbSMel Gorman 
257f40d1e42SMel Gorman 	/* Isolate free pages. */
258748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
259748446bbSMel Gorman 		int isolated, i;
260748446bbSMel Gorman 		struct page *page = cursor;
261748446bbSMel Gorman 
262b7aba698SMel Gorman 		nr_scanned++;
263f40d1e42SMel Gorman 		if (!pfn_valid_within(blockpfn))
264748446bbSMel Gorman 			continue;
265bb13ffebSMel Gorman 		if (!valid_page)
266bb13ffebSMel Gorman 			valid_page = page;
267f40d1e42SMel Gorman 		if (!PageBuddy(page))
268f40d1e42SMel Gorman 			continue;
269f40d1e42SMel Gorman 
270f40d1e42SMel Gorman 		/*
271f40d1e42SMel Gorman 		 * The zone lock must be held to isolate freepages.
272f40d1e42SMel Gorman 		 * Unfortunately this is a very coarse lock and can be
273f40d1e42SMel Gorman 		 * heavily contended if there are parallel allocations
274f40d1e42SMel Gorman 		 * or parallel compactions. For async compaction do not
275f40d1e42SMel Gorman 		 * spin on the lock and we acquire the lock as late as
276f40d1e42SMel Gorman 		 * possible.
277f40d1e42SMel Gorman 		 */
278f40d1e42SMel Gorman 		locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
279f40d1e42SMel Gorman 								locked, cc);
280f40d1e42SMel Gorman 		if (!locked)
281f40d1e42SMel Gorman 			break;
282f40d1e42SMel Gorman 
283f40d1e42SMel Gorman 		/* Recheck this is a suitable migration target under lock */
284f40d1e42SMel Gorman 		if (!strict && !suitable_migration_target(page))
285f40d1e42SMel Gorman 			break;
286f40d1e42SMel Gorman 
287f40d1e42SMel Gorman 		/* Recheck this is a buddy page under lock */
288f40d1e42SMel Gorman 		if (!PageBuddy(page))
289f40d1e42SMel Gorman 			continue;
290748446bbSMel Gorman 
291748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
292748446bbSMel Gorman 		isolated = split_free_page(page);
29385aa125fSMichal Nazarewicz 		if (!isolated && strict)
294f40d1e42SMel Gorman 			break;
295748446bbSMel Gorman 		total_isolated += isolated;
296748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
297748446bbSMel Gorman 			list_add(&page->lru, freelist);
298748446bbSMel Gorman 			page++;
299748446bbSMel Gorman 		}
300748446bbSMel Gorman 
301748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
302748446bbSMel Gorman 		if (isolated) {
303748446bbSMel Gorman 			blockpfn += isolated - 1;
304748446bbSMel Gorman 			cursor += isolated - 1;
305748446bbSMel Gorman 		}
306748446bbSMel Gorman 	}
307748446bbSMel Gorman 
308b7aba698SMel Gorman 	trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
309f40d1e42SMel Gorman 
310f40d1e42SMel Gorman 	/*
311f40d1e42SMel Gorman 	 * If strict isolation is requested by CMA then check that all the
312f40d1e42SMel Gorman 	 * pages requested were isolated. If there were any failures, 0 is
313f40d1e42SMel Gorman 	 * returned and CMA will fail.
314f40d1e42SMel Gorman 	 */
3150db63d7eSMel Gorman 	if (strict && nr_strict_required > total_isolated)
316f40d1e42SMel Gorman 		total_isolated = 0;
317f40d1e42SMel Gorman 
318f40d1e42SMel Gorman 	if (locked)
319f40d1e42SMel Gorman 		spin_unlock_irqrestore(&cc->zone->lock, flags);
320f40d1e42SMel Gorman 
321bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
322bb13ffebSMel Gorman 	if (blockpfn == end_pfn)
323c89511abSMel Gorman 		update_pageblock_skip(cc, valid_page, total_isolated, false);
324bb13ffebSMel Gorman 
325010fc29aSMinchan Kim 	count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
326397487dbSMel Gorman 	if (total_isolated)
327010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, total_isolated);
328748446bbSMel Gorman 	return total_isolated;
329748446bbSMel Gorman }
330748446bbSMel Gorman 
33185aa125fSMichal Nazarewicz /**
33285aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
33385aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
33485aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
33585aa125fSMichal Nazarewicz  *
33685aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
33785aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
33885aa125fSMichal Nazarewicz  * undo its actions and return zero.
33985aa125fSMichal Nazarewicz  *
34085aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
34185aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
34285aa125fSMichal Nazarewicz  * a free page).
34385aa125fSMichal Nazarewicz  */
344ff9543fdSMichal Nazarewicz unsigned long
345bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc,
346bb13ffebSMel Gorman 			unsigned long start_pfn, unsigned long end_pfn)
34785aa125fSMichal Nazarewicz {
348f40d1e42SMel Gorman 	unsigned long isolated, pfn, block_end_pfn;
34985aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
35085aa125fSMichal Nazarewicz 
35185aa125fSMichal Nazarewicz 	for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
352bb13ffebSMel Gorman 		if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
35385aa125fSMichal Nazarewicz 			break;
35485aa125fSMichal Nazarewicz 
35585aa125fSMichal Nazarewicz 		/*
35685aa125fSMichal Nazarewicz 		 * On subsequent iterations ALIGN() is actually not needed,
35785aa125fSMichal Nazarewicz 		 * but we keep it that we not to complicate the code.
35885aa125fSMichal Nazarewicz 		 */
35985aa125fSMichal Nazarewicz 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
36085aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
36185aa125fSMichal Nazarewicz 
362bb13ffebSMel Gorman 		isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
36385aa125fSMichal Nazarewicz 						   &freelist, true);
36485aa125fSMichal Nazarewicz 
36585aa125fSMichal Nazarewicz 		/*
36685aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
36785aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
36885aa125fSMichal Nazarewicz 		 * non-free pages).
36985aa125fSMichal Nazarewicz 		 */
37085aa125fSMichal Nazarewicz 		if (!isolated)
37185aa125fSMichal Nazarewicz 			break;
37285aa125fSMichal Nazarewicz 
37385aa125fSMichal Nazarewicz 		/*
37485aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
37585aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
37685aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
37785aa125fSMichal Nazarewicz 		 */
37885aa125fSMichal Nazarewicz 	}
37985aa125fSMichal Nazarewicz 
38085aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
38185aa125fSMichal Nazarewicz 	map_pages(&freelist);
38285aa125fSMichal Nazarewicz 
38385aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
38485aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
38585aa125fSMichal Nazarewicz 		release_freepages(&freelist);
38685aa125fSMichal Nazarewicz 		return 0;
38785aa125fSMichal Nazarewicz 	}
38885aa125fSMichal Nazarewicz 
38985aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
39085aa125fSMichal Nazarewicz 	return pfn;
39185aa125fSMichal Nazarewicz }
39285aa125fSMichal Nazarewicz 
393748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
394c67fe375SMel Gorman static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
395748446bbSMel Gorman {
396748446bbSMel Gorman 	struct page *page;
397b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
398748446bbSMel Gorman 
399b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
400b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
401748446bbSMel Gorman 
402c67fe375SMel Gorman 	/* If locked we can use the interrupt unsafe versions */
403c67fe375SMel Gorman 	if (locked) {
404b9e84ac1SMinchan Kim 		__mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
405b9e84ac1SMinchan Kim 		__mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
406c67fe375SMel Gorman 	} else {
407c67fe375SMel Gorman 		mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
408c67fe375SMel Gorman 		mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
409c67fe375SMel Gorman 	}
410748446bbSMel Gorman }
411748446bbSMel Gorman 
412748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
413748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
414748446bbSMel Gorman {
415bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
416748446bbSMel Gorman 
417748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
418748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
419bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
420bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
421748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
422748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
423748446bbSMel Gorman 
424bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
425748446bbSMel Gorman }
426748446bbSMel Gorman 
4272fe86e00SMichal Nazarewicz /**
4282fe86e00SMichal Nazarewicz  * isolate_migratepages_range() - isolate all migrate-able pages in range.
4292fe86e00SMichal Nazarewicz  * @zone:	Zone pages are in.
4302fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
4312fe86e00SMichal Nazarewicz  * @low_pfn:	The first PFN of the range.
4322fe86e00SMichal Nazarewicz  * @end_pfn:	The one-past-the-last PFN of the range.
433e46a2879SMinchan Kim  * @unevictable: true if it allows to isolate unevictable pages
4342fe86e00SMichal Nazarewicz  *
4352fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
4362fe86e00SMichal Nazarewicz  * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
4372fe86e00SMichal Nazarewicz  * pending), otherwise PFN of the first page that was not scanned
4382fe86e00SMichal Nazarewicz  * (which may be both less, equal to or more then end_pfn).
4392fe86e00SMichal Nazarewicz  *
4402fe86e00SMichal Nazarewicz  * Assumes that cc->migratepages is empty and cc->nr_migratepages is
4412fe86e00SMichal Nazarewicz  * zero.
4422fe86e00SMichal Nazarewicz  *
4432fe86e00SMichal Nazarewicz  * Apart from cc->migratepages and cc->nr_migratetypes this function
4442fe86e00SMichal Nazarewicz  * does not modify any cc's fields, in particular it does not modify
4452fe86e00SMichal Nazarewicz  * (or read for that matter) cc->migrate_pfn.
446748446bbSMel Gorman  */
447ff9543fdSMichal Nazarewicz unsigned long
4482fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
449e46a2879SMinchan Kim 		unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
450748446bbSMel Gorman {
4519927af74SMel Gorman 	unsigned long last_pageblock_nr = 0, pageblock_nr;
452b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
453748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
454f3fd4a61SKonstantin Khlebnikov 	isolate_mode_t mode = 0;
455fa9add64SHugh Dickins 	struct lruvec *lruvec;
456c67fe375SMel Gorman 	unsigned long flags;
4572a1402aaSMel Gorman 	bool locked = false;
458bb13ffebSMel Gorman 	struct page *page = NULL, *valid_page = NULL;
459748446bbSMel Gorman 
460748446bbSMel Gorman 	/*
461748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
462748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
463748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
464748446bbSMel Gorman 	 */
465748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
466f9e35b3bSMel Gorman 		/* async migration should just abort */
46768e3e926SLinus Torvalds 		if (!cc->sync)
4682fe86e00SMichal Nazarewicz 			return 0;
469f9e35b3bSMel Gorman 
470748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
471748446bbSMel Gorman 
472748446bbSMel Gorman 		if (fatal_signal_pending(current))
4732fe86e00SMichal Nazarewicz 			return 0;
474748446bbSMel Gorman 	}
475748446bbSMel Gorman 
476748446bbSMel Gorman 	/* Time to isolate some pages for migration */
477b2eef8c0SAndrea Arcangeli 	cond_resched();
478748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
479b2eef8c0SAndrea Arcangeli 		/* give a chance to irqs before checking need_resched() */
4802a1402aaSMel Gorman 		if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
4812a1402aaSMel Gorman 			if (should_release_lock(&zone->lru_lock)) {
482c67fe375SMel Gorman 				spin_unlock_irqrestore(&zone->lru_lock, flags);
483b2eef8c0SAndrea Arcangeli 				locked = false;
484b2eef8c0SAndrea Arcangeli 			}
4852a1402aaSMel Gorman 		}
486b2eef8c0SAndrea Arcangeli 
4870bf380bcSMel Gorman 		/*
4880bf380bcSMel Gorman 		 * migrate_pfn does not necessarily start aligned to a
4890bf380bcSMel Gorman 		 * pageblock. Ensure that pfn_valid is called when moving
4900bf380bcSMel Gorman 		 * into a new MAX_ORDER_NR_PAGES range in case of large
4910bf380bcSMel Gorman 		 * memory holes within the zone
4920bf380bcSMel Gorman 		 */
4930bf380bcSMel Gorman 		if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
4940bf380bcSMel Gorman 			if (!pfn_valid(low_pfn)) {
4950bf380bcSMel Gorman 				low_pfn += MAX_ORDER_NR_PAGES - 1;
4960bf380bcSMel Gorman 				continue;
4970bf380bcSMel Gorman 			}
4980bf380bcSMel Gorman 		}
4990bf380bcSMel Gorman 
500748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
501748446bbSMel Gorman 			continue;
502b7aba698SMel Gorman 		nr_scanned++;
503748446bbSMel Gorman 
504dc908600SMel Gorman 		/*
505dc908600SMel Gorman 		 * Get the page and ensure the page is within the same zone.
506dc908600SMel Gorman 		 * See the comment in isolate_freepages about overlapping
507dc908600SMel Gorman 		 * nodes. It is deliberate that the new zone lock is not taken
508dc908600SMel Gorman 		 * as memory compaction should not move pages between nodes.
509dc908600SMel Gorman 		 */
510748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
511dc908600SMel Gorman 		if (page_zone(page) != zone)
512dc908600SMel Gorman 			continue;
513dc908600SMel Gorman 
514bb13ffebSMel Gorman 		if (!valid_page)
515bb13ffebSMel Gorman 			valid_page = page;
516bb13ffebSMel Gorman 
517bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
518bb13ffebSMel Gorman 		pageblock_nr = low_pfn >> pageblock_order;
519bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
520bb13ffebSMel Gorman 			goto next_pageblock;
521bb13ffebSMel Gorman 
522dc908600SMel Gorman 		/* Skip if free */
523748446bbSMel Gorman 		if (PageBuddy(page))
524748446bbSMel Gorman 			continue;
525748446bbSMel Gorman 
5269927af74SMel Gorman 		/*
5279927af74SMel Gorman 		 * For async migration, also only scan in MOVABLE blocks. Async
5289927af74SMel Gorman 		 * migration is optimistic to see if the minimum amount of work
5299927af74SMel Gorman 		 * satisfies the allocation
5309927af74SMel Gorman 		 */
53168e3e926SLinus Torvalds 		if (!cc->sync && last_pageblock_nr != pageblock_nr &&
53247118af0SMichal Nazarewicz 		    !migrate_async_suitable(get_pageblock_migratetype(page))) {
533c89511abSMel Gorman 			cc->finished_update_migrate = true;
5342a1402aaSMel Gorman 			goto next_pageblock;
5359927af74SMel Gorman 		}
5369927af74SMel Gorman 
537bf6bddf1SRafael Aquini 		/*
538bf6bddf1SRafael Aquini 		 * Check may be lockless but that's ok as we recheck later.
539bf6bddf1SRafael Aquini 		 * It's possible to migrate LRU pages and balloon pages
540bf6bddf1SRafael Aquini 		 * Skip any other type of page
541bf6bddf1SRafael Aquini 		 */
542bf6bddf1SRafael Aquini 		if (!PageLRU(page)) {
543bf6bddf1SRafael Aquini 			if (unlikely(balloon_page_movable(page))) {
544bf6bddf1SRafael Aquini 				if (locked && balloon_page_isolate(page)) {
545bf6bddf1SRafael Aquini 					/* Successfully isolated */
546bf6bddf1SRafael Aquini 					cc->finished_update_migrate = true;
547bf6bddf1SRafael Aquini 					list_add(&page->lru, migratelist);
548bf6bddf1SRafael Aquini 					cc->nr_migratepages++;
549bf6bddf1SRafael Aquini 					nr_isolated++;
550bf6bddf1SRafael Aquini 					goto check_compact_cluster;
551bf6bddf1SRafael Aquini 				}
552bf6bddf1SRafael Aquini 			}
553bc835011SAndrea Arcangeli 			continue;
554bf6bddf1SRafael Aquini 		}
555bc835011SAndrea Arcangeli 
556bc835011SAndrea Arcangeli 		/*
5572a1402aaSMel Gorman 		 * PageLRU is set. lru_lock normally excludes isolation
5582a1402aaSMel Gorman 		 * splitting and collapsing (collapsing has already happened
5592a1402aaSMel Gorman 		 * if PageLRU is set) but the lock is not necessarily taken
5602a1402aaSMel Gorman 		 * here and it is wasteful to take it just to check transhuge.
5612a1402aaSMel Gorman 		 * Check TransHuge without lock and skip the whole pageblock if
5622a1402aaSMel Gorman 		 * it's either a transhuge or hugetlbfs page, as calling
5632a1402aaSMel Gorman 		 * compound_order() without preventing THP from splitting the
5642a1402aaSMel Gorman 		 * page underneath us may return surprising results.
565bc835011SAndrea Arcangeli 		 */
566bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
5672a1402aaSMel Gorman 			if (!locked)
5682a1402aaSMel Gorman 				goto next_pageblock;
5692a1402aaSMel Gorman 			low_pfn += (1 << compound_order(page)) - 1;
5702a1402aaSMel Gorman 			continue;
5712a1402aaSMel Gorman 		}
5722a1402aaSMel Gorman 
5732a1402aaSMel Gorman 		/* Check if it is ok to still hold the lock */
5742a1402aaSMel Gorman 		locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
5752a1402aaSMel Gorman 								locked, cc);
5762a1402aaSMel Gorman 		if (!locked || fatal_signal_pending(current))
5772a1402aaSMel Gorman 			break;
5782a1402aaSMel Gorman 
5792a1402aaSMel Gorman 		/* Recheck PageLRU and PageTransHuge under lock */
5802a1402aaSMel Gorman 		if (!PageLRU(page))
5812a1402aaSMel Gorman 			continue;
5822a1402aaSMel Gorman 		if (PageTransHuge(page)) {
583bc835011SAndrea Arcangeli 			low_pfn += (1 << compound_order(page)) - 1;
584bc835011SAndrea Arcangeli 			continue;
585bc835011SAndrea Arcangeli 		}
586bc835011SAndrea Arcangeli 
58768e3e926SLinus Torvalds 		if (!cc->sync)
588c8244935SMel Gorman 			mode |= ISOLATE_ASYNC_MIGRATE;
589c8244935SMel Gorman 
590e46a2879SMinchan Kim 		if (unevictable)
591e46a2879SMinchan Kim 			mode |= ISOLATE_UNEVICTABLE;
592e46a2879SMinchan Kim 
593fa9add64SHugh Dickins 		lruvec = mem_cgroup_page_lruvec(page, zone);
594fa9add64SHugh Dickins 
595748446bbSMel Gorman 		/* Try isolate the page */
596f3fd4a61SKonstantin Khlebnikov 		if (__isolate_lru_page(page, mode) != 0)
597748446bbSMel Gorman 			continue;
598748446bbSMel Gorman 
599bc835011SAndrea Arcangeli 		VM_BUG_ON(PageTransCompound(page));
600bc835011SAndrea Arcangeli 
601748446bbSMel Gorman 		/* Successfully isolated */
602c89511abSMel Gorman 		cc->finished_update_migrate = true;
603fa9add64SHugh Dickins 		del_page_from_lru_list(page, lruvec, page_lru(page));
604748446bbSMel Gorman 		list_add(&page->lru, migratelist);
605748446bbSMel Gorman 		cc->nr_migratepages++;
606b7aba698SMel Gorman 		nr_isolated++;
607748446bbSMel Gorman 
608bf6bddf1SRafael Aquini check_compact_cluster:
609748446bbSMel Gorman 		/* Avoid isolating too much */
61031b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
61131b8384aSHillf Danton 			++low_pfn;
612748446bbSMel Gorman 			break;
613748446bbSMel Gorman 		}
6142a1402aaSMel Gorman 
6152a1402aaSMel Gorman 		continue;
6162a1402aaSMel Gorman 
6172a1402aaSMel Gorman next_pageblock:
618a9aacbccSMel Gorman 		low_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages) - 1;
6192a1402aaSMel Gorman 		last_pageblock_nr = pageblock_nr;
62031b8384aSHillf Danton 	}
621748446bbSMel Gorman 
622c67fe375SMel Gorman 	acct_isolated(zone, locked, cc);
623748446bbSMel Gorman 
624c67fe375SMel Gorman 	if (locked)
625c67fe375SMel Gorman 		spin_unlock_irqrestore(&zone->lru_lock, flags);
626748446bbSMel Gorman 
627bb13ffebSMel Gorman 	/* Update the pageblock-skip if the whole pageblock was scanned */
628bb13ffebSMel Gorman 	if (low_pfn == end_pfn)
629c89511abSMel Gorman 		update_pageblock_skip(cc, valid_page, nr_isolated, true);
630bb13ffebSMel Gorman 
631b7aba698SMel Gorman 	trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
632b7aba698SMel Gorman 
633010fc29aSMinchan Kim 	count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
634397487dbSMel Gorman 	if (nr_isolated)
635010fc29aSMinchan Kim 		count_compact_events(COMPACTISOLATED, nr_isolated);
636397487dbSMel Gorman 
6372fe86e00SMichal Nazarewicz 	return low_pfn;
6382fe86e00SMichal Nazarewicz }
6392fe86e00SMichal Nazarewicz 
640ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */
641ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION
642ff9543fdSMichal Nazarewicz /*
643ff9543fdSMichal Nazarewicz  * Based on information in the current compact_control, find blocks
644ff9543fdSMichal Nazarewicz  * suitable for isolating free pages from and then isolate them.
645ff9543fdSMichal Nazarewicz  */
646ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone,
647ff9543fdSMichal Nazarewicz 				struct compact_control *cc)
648ff9543fdSMichal Nazarewicz {
649ff9543fdSMichal Nazarewicz 	struct page *page;
650108bcc96SCody P Schafer 	unsigned long high_pfn, low_pfn, pfn, z_end_pfn, end_pfn;
651ff9543fdSMichal Nazarewicz 	int nr_freepages = cc->nr_freepages;
652ff9543fdSMichal Nazarewicz 	struct list_head *freelist = &cc->freepages;
6532fe86e00SMichal Nazarewicz 
654ff9543fdSMichal Nazarewicz 	/*
655ff9543fdSMichal Nazarewicz 	 * Initialise the free scanner. The starting point is where we last
656ff9543fdSMichal Nazarewicz 	 * scanned from (or the end of the zone if starting). The low point
657ff9543fdSMichal Nazarewicz 	 * is the end of the pageblock the migration scanner is using.
658ff9543fdSMichal Nazarewicz 	 */
659ff9543fdSMichal Nazarewicz 	pfn = cc->free_pfn;
660ff9543fdSMichal Nazarewicz 	low_pfn = cc->migrate_pfn + pageblock_nr_pages;
6612fe86e00SMichal Nazarewicz 
662ff9543fdSMichal Nazarewicz 	/*
663ff9543fdSMichal Nazarewicz 	 * Take care that if the migration scanner is at the end of the zone
664ff9543fdSMichal Nazarewicz 	 * that the free scanner does not accidentally move to the next zone
665ff9543fdSMichal Nazarewicz 	 * in the next isolation cycle.
666ff9543fdSMichal Nazarewicz 	 */
667ff9543fdSMichal Nazarewicz 	high_pfn = min(low_pfn, pfn);
668ff9543fdSMichal Nazarewicz 
669108bcc96SCody P Schafer 	z_end_pfn = zone_end_pfn(zone);
670ff9543fdSMichal Nazarewicz 
671ff9543fdSMichal Nazarewicz 	/*
672ff9543fdSMichal Nazarewicz 	 * Isolate free pages until enough are available to migrate the
673ff9543fdSMichal Nazarewicz 	 * pages on cc->migratepages. We stop searching if the migrate
674ff9543fdSMichal Nazarewicz 	 * and free page scanners meet or enough free pages are isolated.
675ff9543fdSMichal Nazarewicz 	 */
676ff9543fdSMichal Nazarewicz 	for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
677ff9543fdSMichal Nazarewicz 					pfn -= pageblock_nr_pages) {
678ff9543fdSMichal Nazarewicz 		unsigned long isolated;
679ff9543fdSMichal Nazarewicz 
680ff9543fdSMichal Nazarewicz 		if (!pfn_valid(pfn))
681ff9543fdSMichal Nazarewicz 			continue;
682ff9543fdSMichal Nazarewicz 
683ff9543fdSMichal Nazarewicz 		/*
684ff9543fdSMichal Nazarewicz 		 * Check for overlapping nodes/zones. It's possible on some
685ff9543fdSMichal Nazarewicz 		 * configurations to have a setup like
686ff9543fdSMichal Nazarewicz 		 * node0 node1 node0
687ff9543fdSMichal Nazarewicz 		 * i.e. it's possible that all pages within a zones range of
688ff9543fdSMichal Nazarewicz 		 * pages do not belong to a single zone.
689ff9543fdSMichal Nazarewicz 		 */
690ff9543fdSMichal Nazarewicz 		page = pfn_to_page(pfn);
691ff9543fdSMichal Nazarewicz 		if (page_zone(page) != zone)
692ff9543fdSMichal Nazarewicz 			continue;
693ff9543fdSMichal Nazarewicz 
694ff9543fdSMichal Nazarewicz 		/* Check the block is suitable for migration */
69568e3e926SLinus Torvalds 		if (!suitable_migration_target(page))
696ff9543fdSMichal Nazarewicz 			continue;
69768e3e926SLinus Torvalds 
698bb13ffebSMel Gorman 		/* If isolation recently failed, do not retry */
699bb13ffebSMel Gorman 		if (!isolation_suitable(cc, page))
700bb13ffebSMel Gorman 			continue;
701bb13ffebSMel Gorman 
702f40d1e42SMel Gorman 		/* Found a block suitable for isolating free pages from */
703ff9543fdSMichal Nazarewicz 		isolated = 0;
70460177d31SMel Gorman 
70560177d31SMel Gorman 		/*
70660177d31SMel Gorman 		 * As pfn may not start aligned, pfn+pageblock_nr_page
70760177d31SMel Gorman 		 * may cross a MAX_ORDER_NR_PAGES boundary and miss
70860177d31SMel Gorman 		 * a pfn_valid check. Ensure isolate_freepages_block()
70960177d31SMel Gorman 		 * only scans within a pageblock
71060177d31SMel Gorman 		 */
71160177d31SMel Gorman 		end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
712108bcc96SCody P Schafer 		end_pfn = min(end_pfn, z_end_pfn);
713f40d1e42SMel Gorman 		isolated = isolate_freepages_block(cc, pfn, end_pfn,
714ff9543fdSMichal Nazarewicz 						   freelist, false);
715ff9543fdSMichal Nazarewicz 		nr_freepages += isolated;
716ff9543fdSMichal Nazarewicz 
717ff9543fdSMichal Nazarewicz 		/*
718ff9543fdSMichal Nazarewicz 		 * Record the highest PFN we isolated pages from. When next
719ff9543fdSMichal Nazarewicz 		 * looking for free pages, the search will restart here as
720ff9543fdSMichal Nazarewicz 		 * page migration may have returned some pages to the allocator
721ff9543fdSMichal Nazarewicz 		 */
722c89511abSMel Gorman 		if (isolated) {
723c89511abSMel Gorman 			cc->finished_update_free = true;
724ff9543fdSMichal Nazarewicz 			high_pfn = max(high_pfn, pfn);
725ff9543fdSMichal Nazarewicz 		}
726c89511abSMel Gorman 	}
727ff9543fdSMichal Nazarewicz 
728ff9543fdSMichal Nazarewicz 	/* split_free_page does not map the pages */
729ff9543fdSMichal Nazarewicz 	map_pages(freelist);
730ff9543fdSMichal Nazarewicz 
731ff9543fdSMichal Nazarewicz 	cc->free_pfn = high_pfn;
732ff9543fdSMichal Nazarewicz 	cc->nr_freepages = nr_freepages;
733748446bbSMel Gorman }
734748446bbSMel Gorman 
735748446bbSMel Gorman /*
736748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
737748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
738748446bbSMel Gorman  */
739748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
740748446bbSMel Gorman 					unsigned long data,
741748446bbSMel Gorman 					int **result)
742748446bbSMel Gorman {
743748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
744748446bbSMel Gorman 	struct page *freepage;
745748446bbSMel Gorman 
746748446bbSMel Gorman 	/* Isolate free pages if necessary */
747748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
748748446bbSMel Gorman 		isolate_freepages(cc->zone, cc);
749748446bbSMel Gorman 
750748446bbSMel Gorman 		if (list_empty(&cc->freepages))
751748446bbSMel Gorman 			return NULL;
752748446bbSMel Gorman 	}
753748446bbSMel Gorman 
754748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
755748446bbSMel Gorman 	list_del(&freepage->lru);
756748446bbSMel Gorman 	cc->nr_freepages--;
757748446bbSMel Gorman 
758748446bbSMel Gorman 	return freepage;
759748446bbSMel Gorman }
760748446bbSMel Gorman 
761748446bbSMel Gorman /*
762748446bbSMel Gorman  * We cannot control nr_migratepages and nr_freepages fully when migration is
763748446bbSMel Gorman  * running as migrate_pages() has no knowledge of compact_control. When
764748446bbSMel Gorman  * migration is complete, we count the number of pages on the lists by hand.
765748446bbSMel Gorman  */
766748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc)
767748446bbSMel Gorman {
768748446bbSMel Gorman 	int nr_migratepages = 0;
769748446bbSMel Gorman 	int nr_freepages = 0;
770748446bbSMel Gorman 	struct page *page;
771748446bbSMel Gorman 
772748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru)
773748446bbSMel Gorman 		nr_migratepages++;
774748446bbSMel Gorman 	list_for_each_entry(page, &cc->freepages, lru)
775748446bbSMel Gorman 		nr_freepages++;
776748446bbSMel Gorman 
777748446bbSMel Gorman 	cc->nr_migratepages = nr_migratepages;
778748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
779748446bbSMel Gorman }
780748446bbSMel Gorman 
781ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */
782ff9543fdSMichal Nazarewicz typedef enum {
783ff9543fdSMichal Nazarewicz 	ISOLATE_ABORT,		/* Abort compaction now */
784ff9543fdSMichal Nazarewicz 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
785ff9543fdSMichal Nazarewicz 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
786ff9543fdSMichal Nazarewicz } isolate_migrate_t;
787ff9543fdSMichal Nazarewicz 
788ff9543fdSMichal Nazarewicz /*
789ff9543fdSMichal Nazarewicz  * Isolate all pages that can be migrated from the block pointed to by
790ff9543fdSMichal Nazarewicz  * the migrate scanner within compact_control.
791ff9543fdSMichal Nazarewicz  */
792ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
793ff9543fdSMichal Nazarewicz 					struct compact_control *cc)
794ff9543fdSMichal Nazarewicz {
795ff9543fdSMichal Nazarewicz 	unsigned long low_pfn, end_pfn;
796ff9543fdSMichal Nazarewicz 
797ff9543fdSMichal Nazarewicz 	/* Do not scan outside zone boundaries */
798ff9543fdSMichal Nazarewicz 	low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
799ff9543fdSMichal Nazarewicz 
800ff9543fdSMichal Nazarewicz 	/* Only scan within a pageblock boundary */
801a9aacbccSMel Gorman 	end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
802ff9543fdSMichal Nazarewicz 
803ff9543fdSMichal Nazarewicz 	/* Do not cross the free scanner or scan within a memory hole */
804ff9543fdSMichal Nazarewicz 	if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
805ff9543fdSMichal Nazarewicz 		cc->migrate_pfn = end_pfn;
806ff9543fdSMichal Nazarewicz 		return ISOLATE_NONE;
807ff9543fdSMichal Nazarewicz 	}
808ff9543fdSMichal Nazarewicz 
809ff9543fdSMichal Nazarewicz 	/* Perform the isolation */
810e46a2879SMinchan Kim 	low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
811e64c5237SShaohua Li 	if (!low_pfn || cc->contended)
812ff9543fdSMichal Nazarewicz 		return ISOLATE_ABORT;
813ff9543fdSMichal Nazarewicz 
814ff9543fdSMichal Nazarewicz 	cc->migrate_pfn = low_pfn;
815ff9543fdSMichal Nazarewicz 
816ff9543fdSMichal Nazarewicz 	return ISOLATE_SUCCESS;
817ff9543fdSMichal Nazarewicz }
818ff9543fdSMichal Nazarewicz 
819748446bbSMel Gorman static int compact_finished(struct zone *zone,
820748446bbSMel Gorman 			    struct compact_control *cc)
821748446bbSMel Gorman {
8228fb74b9fSMel Gorman 	unsigned int order;
8235a03b051SAndrea Arcangeli 	unsigned long watermark;
82456de7263SMel Gorman 
825748446bbSMel Gorman 	if (fatal_signal_pending(current))
826748446bbSMel Gorman 		return COMPACT_PARTIAL;
827748446bbSMel Gorman 
828753341a4SMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
829bb13ffebSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn) {
83062997027SMel Gorman 		/*
83162997027SMel Gorman 		 * Mark that the PG_migrate_skip information should be cleared
83262997027SMel Gorman 		 * by kswapd when it goes to sleep. kswapd does not set the
83362997027SMel Gorman 		 * flag itself as the decision to be clear should be directly
83462997027SMel Gorman 		 * based on an allocation request.
83562997027SMel Gorman 		 */
83662997027SMel Gorman 		if (!current_is_kswapd())
83762997027SMel Gorman 			zone->compact_blockskip_flush = true;
83862997027SMel Gorman 
839748446bbSMel Gorman 		return COMPACT_COMPLETE;
840bb13ffebSMel Gorman 	}
841748446bbSMel Gorman 
84282478fb7SJohannes Weiner 	/*
84382478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
84482478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
84582478fb7SJohannes Weiner 	 */
84656de7263SMel Gorman 	if (cc->order == -1)
84756de7263SMel Gorman 		return COMPACT_CONTINUE;
84856de7263SMel Gorman 
8493957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
8503957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
8513957c776SMichal Hocko 	watermark += (1 << cc->order);
8523957c776SMichal Hocko 
8533957c776SMichal Hocko 	if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
8543957c776SMichal Hocko 		return COMPACT_CONTINUE;
8553957c776SMichal Hocko 
85656de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
85756de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
8588fb74b9fSMel Gorman 		struct free_area *area = &zone->free_area[order];
8598fb74b9fSMel Gorman 
86056de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
8611fb3f8caSMel Gorman 		if (!list_empty(&area->free_list[cc->migratetype]))
86256de7263SMel Gorman 			return COMPACT_PARTIAL;
86356de7263SMel Gorman 
86456de7263SMel Gorman 		/* Job done if allocation would set block type */
8651fb3f8caSMel Gorman 		if (cc->order >= pageblock_order && area->nr_free)
86656de7263SMel Gorman 			return COMPACT_PARTIAL;
86756de7263SMel Gorman 	}
86856de7263SMel Gorman 
869748446bbSMel Gorman 	return COMPACT_CONTINUE;
870748446bbSMel Gorman }
871748446bbSMel Gorman 
8723e7d3449SMel Gorman /*
8733e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
8743e7d3449SMel Gorman  * Returns
8753e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
8763e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
8773e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
8783e7d3449SMel Gorman  */
8793e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order)
8803e7d3449SMel Gorman {
8813e7d3449SMel Gorman 	int fragindex;
8823e7d3449SMel Gorman 	unsigned long watermark;
8833e7d3449SMel Gorman 
8843e7d3449SMel Gorman 	/*
8853957c776SMichal Hocko 	 * order == -1 is expected when compacting via
8863957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
8873957c776SMichal Hocko 	 */
8883957c776SMichal Hocko 	if (order == -1)
8893957c776SMichal Hocko 		return COMPACT_CONTINUE;
8903957c776SMichal Hocko 
8913957c776SMichal Hocko 	/*
8923e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
8933e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
8943e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
8953e7d3449SMel Gorman 	 */
8963e7d3449SMel Gorman 	watermark = low_wmark_pages(zone) + (2UL << order);
8973e7d3449SMel Gorman 	if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
8983e7d3449SMel Gorman 		return COMPACT_SKIPPED;
8993e7d3449SMel Gorman 
9003e7d3449SMel Gorman 	/*
9013e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
9023e7d3449SMel Gorman 	 * low memory or external fragmentation
9033e7d3449SMel Gorman 	 *
904a582a738SShaohua Li 	 * index of -1000 implies allocations might succeed depending on
905a582a738SShaohua Li 	 * watermarks
9063e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
9073e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
9083e7d3449SMel Gorman 	 *
9093e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
9103e7d3449SMel Gorman 	 */
9113e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
9123e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
9133e7d3449SMel Gorman 		return COMPACT_SKIPPED;
9143e7d3449SMel Gorman 
915a582a738SShaohua Li 	if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
916a582a738SShaohua Li 	    0, 0))
9173e7d3449SMel Gorman 		return COMPACT_PARTIAL;
9183e7d3449SMel Gorman 
9193e7d3449SMel Gorman 	return COMPACT_CONTINUE;
9203e7d3449SMel Gorman }
9213e7d3449SMel Gorman 
922748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
923748446bbSMel Gorman {
924748446bbSMel Gorman 	int ret;
925c89511abSMel Gorman 	unsigned long start_pfn = zone->zone_start_pfn;
926108bcc96SCody P Schafer 	unsigned long end_pfn = zone_end_pfn(zone);
927748446bbSMel Gorman 
9283e7d3449SMel Gorman 	ret = compaction_suitable(zone, cc->order);
9293e7d3449SMel Gorman 	switch (ret) {
9303e7d3449SMel Gorman 	case COMPACT_PARTIAL:
9313e7d3449SMel Gorman 	case COMPACT_SKIPPED:
9323e7d3449SMel Gorman 		/* Compaction is likely to fail */
9333e7d3449SMel Gorman 		return ret;
9343e7d3449SMel Gorman 	case COMPACT_CONTINUE:
9353e7d3449SMel Gorman 		/* Fall through to compaction */
9363e7d3449SMel Gorman 		;
9373e7d3449SMel Gorman 	}
9383e7d3449SMel Gorman 
939c89511abSMel Gorman 	/*
940c89511abSMel Gorman 	 * Setup to move all movable pages to the end of the zone. Used cached
941c89511abSMel Gorman 	 * information on where the scanners should start but check that it
942c89511abSMel Gorman 	 * is initialised by ensuring the values are within zone boundaries.
943c89511abSMel Gorman 	 */
944c89511abSMel Gorman 	cc->migrate_pfn = zone->compact_cached_migrate_pfn;
945c89511abSMel Gorman 	cc->free_pfn = zone->compact_cached_free_pfn;
946c89511abSMel Gorman 	if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
947c89511abSMel Gorman 		cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
948c89511abSMel Gorman 		zone->compact_cached_free_pfn = cc->free_pfn;
949c89511abSMel Gorman 	}
950c89511abSMel Gorman 	if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
951c89511abSMel Gorman 		cc->migrate_pfn = start_pfn;
952c89511abSMel Gorman 		zone->compact_cached_migrate_pfn = cc->migrate_pfn;
953c89511abSMel Gorman 	}
954748446bbSMel Gorman 
95562997027SMel Gorman 	/*
95662997027SMel Gorman 	 * Clear pageblock skip if there were failures recently and compaction
95762997027SMel Gorman 	 * is about to be retried after being deferred. kswapd does not do
95862997027SMel Gorman 	 * this reset as it'll reset the cached information when going to sleep.
95962997027SMel Gorman 	 */
96062997027SMel Gorman 	if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
96162997027SMel Gorman 		__reset_isolation_suitable(zone);
962bb13ffebSMel Gorman 
963748446bbSMel Gorman 	migrate_prep_local();
964748446bbSMel Gorman 
965748446bbSMel Gorman 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
966748446bbSMel Gorman 		unsigned long nr_migrate, nr_remaining;
9679d502c1cSMinchan Kim 		int err;
968748446bbSMel Gorman 
969f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
970f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
971f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
9725733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
973e64c5237SShaohua Li 			cc->nr_migratepages = 0;
974f9e35b3bSMel Gorman 			goto out;
975f9e35b3bSMel Gorman 		case ISOLATE_NONE:
976748446bbSMel Gorman 			continue;
977f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
978f9e35b3bSMel Gorman 			;
979f9e35b3bSMel Gorman 		}
980748446bbSMel Gorman 
981748446bbSMel Gorman 		nr_migrate = cc->nr_migratepages;
9829d502c1cSMinchan Kim 		err = migrate_pages(&cc->migratepages, compaction_alloc,
9839c620e2bSHugh Dickins 				(unsigned long)cc,
9847b2a2d4aSMel Gorman 				cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC,
9857b2a2d4aSMel Gorman 				MR_COMPACTION);
986748446bbSMel Gorman 		update_nr_listpages(cc);
987748446bbSMel Gorman 		nr_remaining = cc->nr_migratepages;
988748446bbSMel Gorman 
989b7aba698SMel Gorman 		trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
990b7aba698SMel Gorman 						nr_remaining);
991748446bbSMel Gorman 
9925733c7d1SRafael Aquini 		/* Release isolated pages not migrated */
9939d502c1cSMinchan Kim 		if (err) {
9945733c7d1SRafael Aquini 			putback_movable_pages(&cc->migratepages);
995748446bbSMel Gorman 			cc->nr_migratepages = 0;
9964bf2bba3SDavid Rientjes 			if (err == -ENOMEM) {
9974bf2bba3SDavid Rientjes 				ret = COMPACT_PARTIAL;
9984bf2bba3SDavid Rientjes 				goto out;
999748446bbSMel Gorman 			}
10004bf2bba3SDavid Rientjes 		}
1001748446bbSMel Gorman 	}
1002748446bbSMel Gorman 
1003f9e35b3bSMel Gorman out:
1004748446bbSMel Gorman 	/* Release free pages and check accounting */
1005748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
1006748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
1007748446bbSMel Gorman 
1008748446bbSMel Gorman 	return ret;
1009748446bbSMel Gorman }
101076ab0f53SMel Gorman 
1011d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone,
101277f1fe6bSMel Gorman 				 int order, gfp_t gfp_mask,
10138fb74b9fSMel Gorman 				 bool sync, bool *contended)
101456de7263SMel Gorman {
1015e64c5237SShaohua Li 	unsigned long ret;
101656de7263SMel Gorman 	struct compact_control cc = {
101756de7263SMel Gorman 		.nr_freepages = 0,
101856de7263SMel Gorman 		.nr_migratepages = 0,
101956de7263SMel Gorman 		.order = order,
102056de7263SMel Gorman 		.migratetype = allocflags_to_migratetype(gfp_mask),
102156de7263SMel Gorman 		.zone = zone,
102268e3e926SLinus Torvalds 		.sync = sync,
102356de7263SMel Gorman 	};
102456de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
102556de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
102656de7263SMel Gorman 
1027e64c5237SShaohua Li 	ret = compact_zone(zone, &cc);
1028e64c5237SShaohua Li 
1029e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.freepages));
1030e64c5237SShaohua Li 	VM_BUG_ON(!list_empty(&cc.migratepages));
1031e64c5237SShaohua Li 
1032e64c5237SShaohua Li 	*contended = cc.contended;
1033e64c5237SShaohua Li 	return ret;
103456de7263SMel Gorman }
103556de7263SMel Gorman 
10365e771905SMel Gorman int sysctl_extfrag_threshold = 500;
10375e771905SMel Gorman 
103856de7263SMel Gorman /**
103956de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
104056de7263SMel Gorman  * @zonelist: The zonelist used for the current allocation
104156de7263SMel Gorman  * @order: The order of the current allocation
104256de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
104356de7263SMel Gorman  * @nodemask: The allowed nodes to allocate from
104477f1fe6bSMel Gorman  * @sync: Whether migration is synchronous or not
1045661c4cb9SMel Gorman  * @contended: Return value that is true if compaction was aborted due to lock contention
1046661c4cb9SMel Gorman  * @page: Optionally capture a free page of the requested order during compaction
104756de7263SMel Gorman  *
104856de7263SMel Gorman  * This is the main entry point for direct page compaction.
104956de7263SMel Gorman  */
105056de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist,
105177f1fe6bSMel Gorman 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
10528fb74b9fSMel Gorman 			bool sync, bool *contended)
105356de7263SMel Gorman {
105456de7263SMel Gorman 	enum zone_type high_zoneidx = gfp_zone(gfp_mask);
105556de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
105656de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
105756de7263SMel Gorman 	struct zoneref *z;
105856de7263SMel Gorman 	struct zone *zone;
105956de7263SMel Gorman 	int rc = COMPACT_SKIPPED;
1060d95ea5d1SBartlomiej Zolnierkiewicz 	int alloc_flags = 0;
106156de7263SMel Gorman 
10624ffb6335SMel Gorman 	/* Check if the GFP flags allow compaction */
1063c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
106456de7263SMel Gorman 		return rc;
106556de7263SMel Gorman 
1066010fc29aSMinchan Kim 	count_compact_event(COMPACTSTALL);
106756de7263SMel Gorman 
1068d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA
1069d95ea5d1SBartlomiej Zolnierkiewicz 	if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1070d95ea5d1SBartlomiej Zolnierkiewicz 		alloc_flags |= ALLOC_CMA;
1071d95ea5d1SBartlomiej Zolnierkiewicz #endif
107256de7263SMel Gorman 	/* Compact each zone in the list */
107356de7263SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
107456de7263SMel Gorman 								nodemask) {
107556de7263SMel Gorman 		int status;
107656de7263SMel Gorman 
1077c67fe375SMel Gorman 		status = compact_zone_order(zone, order, gfp_mask, sync,
10788fb74b9fSMel Gorman 						contended);
107956de7263SMel Gorman 		rc = max(status, rc);
108056de7263SMel Gorman 
10813e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
1082d95ea5d1SBartlomiej Zolnierkiewicz 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1083d95ea5d1SBartlomiej Zolnierkiewicz 				      alloc_flags))
108456de7263SMel Gorman 			break;
108556de7263SMel Gorman 	}
108656de7263SMel Gorman 
108756de7263SMel Gorman 	return rc;
108856de7263SMel Gorman }
108956de7263SMel Gorman 
109056de7263SMel Gorman 
109176ab0f53SMel Gorman /* Compact all zones within a node */
10927103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
109376ab0f53SMel Gorman {
109476ab0f53SMel Gorman 	int zoneid;
109576ab0f53SMel Gorman 	struct zone *zone;
109676ab0f53SMel Gorman 
109776ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
109876ab0f53SMel Gorman 
109976ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
110076ab0f53SMel Gorman 		if (!populated_zone(zone))
110176ab0f53SMel Gorman 			continue;
110276ab0f53SMel Gorman 
11037be62de9SRik van Riel 		cc->nr_freepages = 0;
11047be62de9SRik van Riel 		cc->nr_migratepages = 0;
11057be62de9SRik van Riel 		cc->zone = zone;
11067be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
11077be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
110876ab0f53SMel Gorman 
1109aad6ec37SDan Carpenter 		if (cc->order == -1 || !compaction_deferred(zone, cc->order))
11107be62de9SRik van Riel 			compact_zone(zone, cc);
111176ab0f53SMel Gorman 
1112aff62249SRik van Riel 		if (cc->order > 0) {
1113aff62249SRik van Riel 			int ok = zone_watermark_ok(zone, cc->order,
1114aff62249SRik van Riel 						low_wmark_pages(zone), 0, 0);
1115c81758fbSMinchan Kim 			if (ok && cc->order >= zone->compact_order_failed)
1116aff62249SRik van Riel 				zone->compact_order_failed = cc->order + 1;
1117aff62249SRik van Riel 			/* Currently async compaction is never deferred. */
111868e3e926SLinus Torvalds 			else if (!ok && cc->sync)
1119aff62249SRik van Riel 				defer_compaction(zone, cc->order);
1120aff62249SRik van Riel 		}
1121aff62249SRik van Riel 
11227be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->freepages));
11237be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->migratepages));
112476ab0f53SMel Gorman 	}
112576ab0f53SMel Gorman }
112676ab0f53SMel Gorman 
11277103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order)
11287be62de9SRik van Riel {
11297be62de9SRik van Riel 	struct compact_control cc = {
11307be62de9SRik van Riel 		.order = order,
113168e3e926SLinus Torvalds 		.sync = false,
11327be62de9SRik van Riel 	};
11337be62de9SRik van Riel 
1134*3a7200afSMel Gorman 	if (!order)
1135*3a7200afSMel Gorman 		return;
1136*3a7200afSMel Gorman 
11377103f16dSAndrew Morton 	__compact_pgdat(pgdat, &cc);
11387be62de9SRik van Riel }
11397be62de9SRik van Riel 
11407103f16dSAndrew Morton static void compact_node(int nid)
11417be62de9SRik van Riel {
11427be62de9SRik van Riel 	struct compact_control cc = {
11437be62de9SRik van Riel 		.order = -1,
114468e3e926SLinus Torvalds 		.sync = true,
11457be62de9SRik van Riel 	};
11467be62de9SRik van Riel 
11477103f16dSAndrew Morton 	__compact_pgdat(NODE_DATA(nid), &cc);
11487be62de9SRik van Riel }
11497be62de9SRik van Riel 
115076ab0f53SMel Gorman /* Compact all nodes in the system */
11517964c06dSJason Liu static void compact_nodes(void)
115276ab0f53SMel Gorman {
115376ab0f53SMel Gorman 	int nid;
115476ab0f53SMel Gorman 
11558575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
11568575ec29SHugh Dickins 	lru_add_drain_all();
11578575ec29SHugh Dickins 
115876ab0f53SMel Gorman 	for_each_online_node(nid)
115976ab0f53SMel Gorman 		compact_node(nid);
116076ab0f53SMel Gorman }
116176ab0f53SMel Gorman 
116276ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
116376ab0f53SMel Gorman int sysctl_compact_memory;
116476ab0f53SMel Gorman 
116576ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
116676ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
116776ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
116876ab0f53SMel Gorman {
116976ab0f53SMel Gorman 	if (write)
11707964c06dSJason Liu 		compact_nodes();
117176ab0f53SMel Gorman 
117276ab0f53SMel Gorman 	return 0;
117376ab0f53SMel Gorman }
1174ed4a6d7fSMel Gorman 
11755e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
11765e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
11775e771905SMel Gorman {
11785e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
11795e771905SMel Gorman 
11805e771905SMel Gorman 	return 0;
11815e771905SMel Gorman }
11825e771905SMel Gorman 
1183ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
118410fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev,
118510fbcf4cSKay Sievers 			struct device_attribute *attr,
1186ed4a6d7fSMel Gorman 			const char *buf, size_t count)
1187ed4a6d7fSMel Gorman {
11888575ec29SHugh Dickins 	int nid = dev->id;
11898575ec29SHugh Dickins 
11908575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
11918575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
11928575ec29SHugh Dickins 		lru_add_drain_all();
11938575ec29SHugh Dickins 
11948575ec29SHugh Dickins 		compact_node(nid);
11958575ec29SHugh Dickins 	}
1196ed4a6d7fSMel Gorman 
1197ed4a6d7fSMel Gorman 	return count;
1198ed4a6d7fSMel Gorman }
119910fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1200ed4a6d7fSMel Gorman 
1201ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
1202ed4a6d7fSMel Gorman {
120310fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
1204ed4a6d7fSMel Gorman }
1205ed4a6d7fSMel Gorman 
1206ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
1207ed4a6d7fSMel Gorman {
120810fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
1209ed4a6d7fSMel Gorman }
1210ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1211ff9543fdSMichal Nazarewicz 
1212ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */
1213