xref: /openbmc/linux/mm/compaction.c (revision 85aa125f001f87f96a72e9e6ee515490843b1202)
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 
19b7aba698SMel Gorman #define CREATE_TRACE_POINTS
20b7aba698SMel Gorman #include <trace/events/compaction.h>
21b7aba698SMel Gorman 
22748446bbSMel Gorman /*
23748446bbSMel Gorman  * compact_control is used to track pages being migrated and the free pages
24748446bbSMel Gorman  * they are being migrated to during memory compaction. The free_pfn starts
25748446bbSMel Gorman  * at the end of a zone and migrate_pfn begins at the start. Movable pages
26748446bbSMel Gorman  * are moved to the end of a zone during a compaction run and the run
27748446bbSMel Gorman  * completes when free_pfn <= migrate_pfn
28748446bbSMel Gorman  */
29748446bbSMel Gorman struct compact_control {
30748446bbSMel Gorman 	struct list_head freepages;	/* List of free pages to migrate to */
31748446bbSMel Gorman 	struct list_head migratepages;	/* List of pages being migrated */
32748446bbSMel Gorman 	unsigned long nr_freepages;	/* Number of isolated free pages */
33748446bbSMel Gorman 	unsigned long nr_migratepages;	/* Number of pages to migrate */
34748446bbSMel Gorman 	unsigned long free_pfn;		/* isolate_freepages search base */
35748446bbSMel Gorman 	unsigned long migrate_pfn;	/* isolate_migratepages search base */
3677f1fe6bSMel Gorman 	bool sync;			/* Synchronous migration */
37748446bbSMel Gorman 
38aad6ec37SDan Carpenter 	int order;			/* order a direct compactor needs */
3956de7263SMel Gorman 	int migratetype;		/* MOVABLE, RECLAIMABLE etc */
40748446bbSMel Gorman 	struct zone *zone;
41748446bbSMel Gorman };
42748446bbSMel Gorman 
43748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
44748446bbSMel Gorman {
45748446bbSMel Gorman 	struct page *page, *next;
46748446bbSMel Gorman 	unsigned long count = 0;
47748446bbSMel Gorman 
48748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
49748446bbSMel Gorman 		list_del(&page->lru);
50748446bbSMel Gorman 		__free_page(page);
51748446bbSMel Gorman 		count++;
52748446bbSMel Gorman 	}
53748446bbSMel Gorman 
54748446bbSMel Gorman 	return count;
55748446bbSMel Gorman }
56748446bbSMel Gorman 
57*85aa125fSMichal Nazarewicz /*
58*85aa125fSMichal Nazarewicz  * Isolate free pages onto a private freelist. Caller must hold zone->lock.
59*85aa125fSMichal Nazarewicz  * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
60*85aa125fSMichal Nazarewicz  * pages inside of the pageblock (even though it may still end up isolating
61*85aa125fSMichal Nazarewicz  * some pages).
62*85aa125fSMichal Nazarewicz  */
63*85aa125fSMichal Nazarewicz static unsigned long isolate_freepages_block(unsigned long blockpfn,
64*85aa125fSMichal Nazarewicz 				unsigned long end_pfn,
65*85aa125fSMichal Nazarewicz 				struct list_head *freelist,
66*85aa125fSMichal Nazarewicz 				bool strict)
67748446bbSMel Gorman {
68b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
69748446bbSMel Gorman 	struct page *cursor;
70748446bbSMel Gorman 
71748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
72748446bbSMel Gorman 
73748446bbSMel Gorman 	/* Isolate free pages. This assumes the block is valid */
74748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
75748446bbSMel Gorman 		int isolated, i;
76748446bbSMel Gorman 		struct page *page = cursor;
77748446bbSMel Gorman 
78*85aa125fSMichal Nazarewicz 		if (!pfn_valid_within(blockpfn)) {
79*85aa125fSMichal Nazarewicz 			if (strict)
80*85aa125fSMichal Nazarewicz 				return 0;
81748446bbSMel Gorman 			continue;
82*85aa125fSMichal Nazarewicz 		}
83b7aba698SMel Gorman 		nr_scanned++;
84748446bbSMel Gorman 
85*85aa125fSMichal Nazarewicz 		if (!PageBuddy(page)) {
86*85aa125fSMichal Nazarewicz 			if (strict)
87*85aa125fSMichal Nazarewicz 				return 0;
88748446bbSMel Gorman 			continue;
89*85aa125fSMichal Nazarewicz 		}
90748446bbSMel Gorman 
91748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
92748446bbSMel Gorman 		isolated = split_free_page(page);
93*85aa125fSMichal Nazarewicz 		if (!isolated && strict)
94*85aa125fSMichal Nazarewicz 			return 0;
95748446bbSMel Gorman 		total_isolated += isolated;
96748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
97748446bbSMel Gorman 			list_add(&page->lru, freelist);
98748446bbSMel Gorman 			page++;
99748446bbSMel Gorman 		}
100748446bbSMel Gorman 
101748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
102748446bbSMel Gorman 		if (isolated) {
103748446bbSMel Gorman 			blockpfn += isolated - 1;
104748446bbSMel Gorman 			cursor += isolated - 1;
105748446bbSMel Gorman 		}
106748446bbSMel Gorman 	}
107748446bbSMel Gorman 
108b7aba698SMel Gorman 	trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
109748446bbSMel Gorman 	return total_isolated;
110748446bbSMel Gorman }
111748446bbSMel Gorman 
112*85aa125fSMichal Nazarewicz /**
113*85aa125fSMichal Nazarewicz  * isolate_freepages_range() - isolate free pages.
114*85aa125fSMichal Nazarewicz  * @start_pfn: The first PFN to start isolating.
115*85aa125fSMichal Nazarewicz  * @end_pfn:   The one-past-last PFN.
116*85aa125fSMichal Nazarewicz  *
117*85aa125fSMichal Nazarewicz  * Non-free pages, invalid PFNs, or zone boundaries within the
118*85aa125fSMichal Nazarewicz  * [start_pfn, end_pfn) range are considered errors, cause function to
119*85aa125fSMichal Nazarewicz  * undo its actions and return zero.
120*85aa125fSMichal Nazarewicz  *
121*85aa125fSMichal Nazarewicz  * Otherwise, function returns one-past-the-last PFN of isolated page
122*85aa125fSMichal Nazarewicz  * (which may be greater then end_pfn if end fell in a middle of
123*85aa125fSMichal Nazarewicz  * a free page).
124*85aa125fSMichal Nazarewicz  */
125*85aa125fSMichal Nazarewicz static unsigned long
126*85aa125fSMichal Nazarewicz isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
127*85aa125fSMichal Nazarewicz {
128*85aa125fSMichal Nazarewicz 	unsigned long isolated, pfn, block_end_pfn, flags;
129*85aa125fSMichal Nazarewicz 	struct zone *zone = NULL;
130*85aa125fSMichal Nazarewicz 	LIST_HEAD(freelist);
131*85aa125fSMichal Nazarewicz 
132*85aa125fSMichal Nazarewicz 	if (pfn_valid(start_pfn))
133*85aa125fSMichal Nazarewicz 		zone = page_zone(pfn_to_page(start_pfn));
134*85aa125fSMichal Nazarewicz 
135*85aa125fSMichal Nazarewicz 	for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
136*85aa125fSMichal Nazarewicz 		if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
137*85aa125fSMichal Nazarewicz 			break;
138*85aa125fSMichal Nazarewicz 
139*85aa125fSMichal Nazarewicz 		/*
140*85aa125fSMichal Nazarewicz 		 * On subsequent iterations ALIGN() is actually not needed,
141*85aa125fSMichal Nazarewicz 		 * but we keep it that we not to complicate the code.
142*85aa125fSMichal Nazarewicz 		 */
143*85aa125fSMichal Nazarewicz 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
144*85aa125fSMichal Nazarewicz 		block_end_pfn = min(block_end_pfn, end_pfn);
145*85aa125fSMichal Nazarewicz 
146*85aa125fSMichal Nazarewicz 		spin_lock_irqsave(&zone->lock, flags);
147*85aa125fSMichal Nazarewicz 		isolated = isolate_freepages_block(pfn, block_end_pfn,
148*85aa125fSMichal Nazarewicz 						   &freelist, true);
149*85aa125fSMichal Nazarewicz 		spin_unlock_irqrestore(&zone->lock, flags);
150*85aa125fSMichal Nazarewicz 
151*85aa125fSMichal Nazarewicz 		/*
152*85aa125fSMichal Nazarewicz 		 * In strict mode, isolate_freepages_block() returns 0 if
153*85aa125fSMichal Nazarewicz 		 * there are any holes in the block (ie. invalid PFNs or
154*85aa125fSMichal Nazarewicz 		 * non-free pages).
155*85aa125fSMichal Nazarewicz 		 */
156*85aa125fSMichal Nazarewicz 		if (!isolated)
157*85aa125fSMichal Nazarewicz 			break;
158*85aa125fSMichal Nazarewicz 
159*85aa125fSMichal Nazarewicz 		/*
160*85aa125fSMichal Nazarewicz 		 * If we managed to isolate pages, it is always (1 << n) *
161*85aa125fSMichal Nazarewicz 		 * pageblock_nr_pages for some non-negative n.  (Max order
162*85aa125fSMichal Nazarewicz 		 * page may span two pageblocks).
163*85aa125fSMichal Nazarewicz 		 */
164*85aa125fSMichal Nazarewicz 	}
165*85aa125fSMichal Nazarewicz 
166*85aa125fSMichal Nazarewicz 	/* split_free_page does not map the pages */
167*85aa125fSMichal Nazarewicz 	map_pages(&freelist);
168*85aa125fSMichal Nazarewicz 
169*85aa125fSMichal Nazarewicz 	if (pfn < end_pfn) {
170*85aa125fSMichal Nazarewicz 		/* Loop terminated early, cleanup. */
171*85aa125fSMichal Nazarewicz 		release_freepages(&freelist);
172*85aa125fSMichal Nazarewicz 		return 0;
173*85aa125fSMichal Nazarewicz 	}
174*85aa125fSMichal Nazarewicz 
175*85aa125fSMichal Nazarewicz 	/* We don't use freelists for anything. */
176*85aa125fSMichal Nazarewicz 	return pfn;
177*85aa125fSMichal Nazarewicz }
178*85aa125fSMichal Nazarewicz 
179748446bbSMel Gorman /* Returns true if the page is within a block suitable for migration to */
180748446bbSMel Gorman static bool suitable_migration_target(struct page *page)
181748446bbSMel Gorman {
182748446bbSMel Gorman 
183748446bbSMel Gorman 	int migratetype = get_pageblock_migratetype(page);
184748446bbSMel Gorman 
185748446bbSMel Gorman 	/* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
186748446bbSMel Gorman 	if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
187748446bbSMel Gorman 		return false;
188748446bbSMel Gorman 
189748446bbSMel Gorman 	/* If the page is a large free page, then allow migration */
190748446bbSMel Gorman 	if (PageBuddy(page) && page_order(page) >= pageblock_order)
191748446bbSMel Gorman 		return true;
192748446bbSMel Gorman 
193748446bbSMel Gorman 	/* If the block is MIGRATE_MOVABLE, allow migration */
194748446bbSMel Gorman 	if (migratetype == MIGRATE_MOVABLE)
195748446bbSMel Gorman 		return true;
196748446bbSMel Gorman 
197748446bbSMel Gorman 	/* Otherwise skip the block */
198748446bbSMel Gorman 	return false;
199748446bbSMel Gorman }
200748446bbSMel Gorman 
20103d44192SMichal Nazarewicz static void map_pages(struct list_head *list)
20203d44192SMichal Nazarewicz {
20303d44192SMichal Nazarewicz 	struct page *page;
20403d44192SMichal Nazarewicz 
20503d44192SMichal Nazarewicz 	list_for_each_entry(page, list, lru) {
20603d44192SMichal Nazarewicz 		arch_alloc_page(page, 0);
20703d44192SMichal Nazarewicz 		kernel_map_pages(page, 1, 1);
20803d44192SMichal Nazarewicz 	}
20903d44192SMichal Nazarewicz }
21003d44192SMichal Nazarewicz 
211748446bbSMel Gorman /*
212748446bbSMel Gorman  * Based on information in the current compact_control, find blocks
213748446bbSMel Gorman  * suitable for isolating free pages from and then isolate them.
214748446bbSMel Gorman  */
215748446bbSMel Gorman static void isolate_freepages(struct zone *zone,
216748446bbSMel Gorman 				struct compact_control *cc)
217748446bbSMel Gorman {
218748446bbSMel Gorman 	struct page *page;
219*85aa125fSMichal Nazarewicz 	unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
220748446bbSMel Gorman 	unsigned long flags;
221748446bbSMel Gorman 	int nr_freepages = cc->nr_freepages;
222748446bbSMel Gorman 	struct list_head *freelist = &cc->freepages;
223748446bbSMel Gorman 
2247454f4baSMel Gorman 	/*
2257454f4baSMel Gorman 	 * Initialise the free scanner. The starting point is where we last
2267454f4baSMel Gorman 	 * scanned from (or the end of the zone if starting). The low point
2277454f4baSMel Gorman 	 * is the end of the pageblock the migration scanner is using.
2287454f4baSMel Gorman 	 */
229748446bbSMel Gorman 	pfn = cc->free_pfn;
230748446bbSMel Gorman 	low_pfn = cc->migrate_pfn + pageblock_nr_pages;
2317454f4baSMel Gorman 
2327454f4baSMel Gorman 	/*
2337454f4baSMel Gorman 	 * Take care that if the migration scanner is at the end of the zone
2347454f4baSMel Gorman 	 * that the free scanner does not accidentally move to the next zone
2357454f4baSMel Gorman 	 * in the next isolation cycle.
2367454f4baSMel Gorman 	 */
2377454f4baSMel Gorman 	high_pfn = min(low_pfn, pfn);
238748446bbSMel Gorman 
239*85aa125fSMichal Nazarewicz 	zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
240*85aa125fSMichal Nazarewicz 
241748446bbSMel Gorman 	/*
242748446bbSMel Gorman 	 * Isolate free pages until enough are available to migrate the
243748446bbSMel Gorman 	 * pages on cc->migratepages. We stop searching if the migrate
244748446bbSMel Gorman 	 * and free page scanners meet or enough free pages are isolated.
245748446bbSMel Gorman 	 */
246748446bbSMel Gorman 	for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
247748446bbSMel Gorman 					pfn -= pageblock_nr_pages) {
248748446bbSMel Gorman 		unsigned long isolated;
249748446bbSMel Gorman 
250748446bbSMel Gorman 		if (!pfn_valid(pfn))
251748446bbSMel Gorman 			continue;
252748446bbSMel Gorman 
253748446bbSMel Gorman 		/*
254748446bbSMel Gorman 		 * Check for overlapping nodes/zones. It's possible on some
255748446bbSMel Gorman 		 * configurations to have a setup like
256748446bbSMel Gorman 		 * node0 node1 node0
257748446bbSMel Gorman 		 * i.e. it's possible that all pages within a zones range of
258748446bbSMel Gorman 		 * pages do not belong to a single zone.
259748446bbSMel Gorman 		 */
260748446bbSMel Gorman 		page = pfn_to_page(pfn);
261748446bbSMel Gorman 		if (page_zone(page) != zone)
262748446bbSMel Gorman 			continue;
263748446bbSMel Gorman 
264748446bbSMel Gorman 		/* Check the block is suitable for migration */
265748446bbSMel Gorman 		if (!suitable_migration_target(page))
266748446bbSMel Gorman 			continue;
267748446bbSMel Gorman 
268602605a4SMel Gorman 		/*
269602605a4SMel Gorman 		 * Found a block suitable for isolating free pages from. Now
270602605a4SMel Gorman 		 * we disabled interrupts, double check things are ok and
271602605a4SMel Gorman 		 * isolate the pages. This is to minimise the time IRQs
272602605a4SMel Gorman 		 * are disabled
273602605a4SMel Gorman 		 */
274602605a4SMel Gorman 		isolated = 0;
275602605a4SMel Gorman 		spin_lock_irqsave(&zone->lock, flags);
276602605a4SMel Gorman 		if (suitable_migration_target(page)) {
277*85aa125fSMichal Nazarewicz 			end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
278*85aa125fSMichal Nazarewicz 			isolated = isolate_freepages_block(pfn, end_pfn,
279*85aa125fSMichal Nazarewicz 							   freelist, false);
280748446bbSMel Gorman 			nr_freepages += isolated;
281602605a4SMel Gorman 		}
282602605a4SMel Gorman 		spin_unlock_irqrestore(&zone->lock, flags);
283748446bbSMel Gorman 
284748446bbSMel Gorman 		/*
285748446bbSMel Gorman 		 * Record the highest PFN we isolated pages from. When next
286748446bbSMel Gorman 		 * looking for free pages, the search will restart here as
287748446bbSMel Gorman 		 * page migration may have returned some pages to the allocator
288748446bbSMel Gorman 		 */
289748446bbSMel Gorman 		if (isolated)
290748446bbSMel Gorman 			high_pfn = max(high_pfn, pfn);
291748446bbSMel Gorman 	}
292748446bbSMel Gorman 
293748446bbSMel Gorman 	/* split_free_page does not map the pages */
29403d44192SMichal Nazarewicz 	map_pages(freelist);
295748446bbSMel Gorman 
296748446bbSMel Gorman 	cc->free_pfn = high_pfn;
297748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
298748446bbSMel Gorman }
299748446bbSMel Gorman 
300748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
301748446bbSMel Gorman static void acct_isolated(struct zone *zone, struct compact_control *cc)
302748446bbSMel Gorman {
303748446bbSMel Gorman 	struct page *page;
304b9e84ac1SMinchan Kim 	unsigned int count[2] = { 0, };
305748446bbSMel Gorman 
306b9e84ac1SMinchan Kim 	list_for_each_entry(page, &cc->migratepages, lru)
307b9e84ac1SMinchan Kim 		count[!!page_is_file_cache(page)]++;
308748446bbSMel Gorman 
309b9e84ac1SMinchan Kim 	__mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
310b9e84ac1SMinchan Kim 	__mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
311748446bbSMel Gorman }
312748446bbSMel Gorman 
313748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
314748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
315748446bbSMel Gorman {
316bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
317748446bbSMel Gorman 
318748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
319748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
320bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
321bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
322748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
323748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
324748446bbSMel Gorman 
325bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
326748446bbSMel Gorman }
327748446bbSMel Gorman 
328f9e35b3bSMel Gorman /* possible outcome of isolate_migratepages */
329f9e35b3bSMel Gorman typedef enum {
330f9e35b3bSMel Gorman 	ISOLATE_ABORT,		/* Abort compaction now */
331f9e35b3bSMel Gorman 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
332f9e35b3bSMel Gorman 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
333f9e35b3bSMel Gorman } isolate_migrate_t;
334f9e35b3bSMel Gorman 
3352fe86e00SMichal Nazarewicz /**
3362fe86e00SMichal Nazarewicz  * isolate_migratepages_range() - isolate all migrate-able pages in range.
3372fe86e00SMichal Nazarewicz  * @zone:	Zone pages are in.
3382fe86e00SMichal Nazarewicz  * @cc:		Compaction control structure.
3392fe86e00SMichal Nazarewicz  * @low_pfn:	The first PFN of the range.
3402fe86e00SMichal Nazarewicz  * @end_pfn:	The one-past-the-last PFN of the range.
3412fe86e00SMichal Nazarewicz  *
3422fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the range specified by
3432fe86e00SMichal Nazarewicz  * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
3442fe86e00SMichal Nazarewicz  * pending), otherwise PFN of the first page that was not scanned
3452fe86e00SMichal Nazarewicz  * (which may be both less, equal to or more then end_pfn).
3462fe86e00SMichal Nazarewicz  *
3472fe86e00SMichal Nazarewicz  * Assumes that cc->migratepages is empty and cc->nr_migratepages is
3482fe86e00SMichal Nazarewicz  * zero.
3492fe86e00SMichal Nazarewicz  *
3502fe86e00SMichal Nazarewicz  * Apart from cc->migratepages and cc->nr_migratetypes this function
3512fe86e00SMichal Nazarewicz  * does not modify any cc's fields, in particular it does not modify
3522fe86e00SMichal Nazarewicz  * (or read for that matter) cc->migrate_pfn.
353748446bbSMel Gorman  */
3542fe86e00SMichal Nazarewicz static unsigned long
3552fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
3562fe86e00SMichal Nazarewicz 			   unsigned long low_pfn, unsigned long end_pfn)
357748446bbSMel Gorman {
3589927af74SMel Gorman 	unsigned long last_pageblock_nr = 0, pageblock_nr;
359b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
360748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
36139deaf85SMinchan Kim 	isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
362748446bbSMel Gorman 
363748446bbSMel Gorman 	/*
364748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
365748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
366748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
367748446bbSMel Gorman 	 */
368748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
369f9e35b3bSMel Gorman 		/* async migration should just abort */
370f9e35b3bSMel Gorman 		if (!cc->sync)
3712fe86e00SMichal Nazarewicz 			return 0;
372f9e35b3bSMel Gorman 
373748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
374748446bbSMel Gorman 
375748446bbSMel Gorman 		if (fatal_signal_pending(current))
3762fe86e00SMichal Nazarewicz 			return 0;
377748446bbSMel Gorman 	}
378748446bbSMel Gorman 
379748446bbSMel Gorman 	/* Time to isolate some pages for migration */
380b2eef8c0SAndrea Arcangeli 	cond_resched();
381748446bbSMel Gorman 	spin_lock_irq(&zone->lru_lock);
382748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
383748446bbSMel Gorman 		struct page *page;
384b2eef8c0SAndrea Arcangeli 		bool locked = true;
385b2eef8c0SAndrea Arcangeli 
386b2eef8c0SAndrea Arcangeli 		/* give a chance to irqs before checking need_resched() */
387b2eef8c0SAndrea Arcangeli 		if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
388b2eef8c0SAndrea Arcangeli 			spin_unlock_irq(&zone->lru_lock);
389b2eef8c0SAndrea Arcangeli 			locked = false;
390b2eef8c0SAndrea Arcangeli 		}
391b2eef8c0SAndrea Arcangeli 		if (need_resched() || spin_is_contended(&zone->lru_lock)) {
392b2eef8c0SAndrea Arcangeli 			if (locked)
393b2eef8c0SAndrea Arcangeli 				spin_unlock_irq(&zone->lru_lock);
394b2eef8c0SAndrea Arcangeli 			cond_resched();
395b2eef8c0SAndrea Arcangeli 			spin_lock_irq(&zone->lru_lock);
396b2eef8c0SAndrea Arcangeli 			if (fatal_signal_pending(current))
397b2eef8c0SAndrea Arcangeli 				break;
398b2eef8c0SAndrea Arcangeli 		} else if (!locked)
399b2eef8c0SAndrea Arcangeli 			spin_lock_irq(&zone->lru_lock);
400b2eef8c0SAndrea Arcangeli 
4010bf380bcSMel Gorman 		/*
4020bf380bcSMel Gorman 		 * migrate_pfn does not necessarily start aligned to a
4030bf380bcSMel Gorman 		 * pageblock. Ensure that pfn_valid is called when moving
4040bf380bcSMel Gorman 		 * into a new MAX_ORDER_NR_PAGES range in case of large
4050bf380bcSMel Gorman 		 * memory holes within the zone
4060bf380bcSMel Gorman 		 */
4070bf380bcSMel Gorman 		if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
4080bf380bcSMel Gorman 			if (!pfn_valid(low_pfn)) {
4090bf380bcSMel Gorman 				low_pfn += MAX_ORDER_NR_PAGES - 1;
4100bf380bcSMel Gorman 				continue;
4110bf380bcSMel Gorman 			}
4120bf380bcSMel Gorman 		}
4130bf380bcSMel Gorman 
414748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
415748446bbSMel Gorman 			continue;
416b7aba698SMel Gorman 		nr_scanned++;
417748446bbSMel Gorman 
418dc908600SMel Gorman 		/*
419dc908600SMel Gorman 		 * Get the page and ensure the page is within the same zone.
420dc908600SMel Gorman 		 * See the comment in isolate_freepages about overlapping
421dc908600SMel Gorman 		 * nodes. It is deliberate that the new zone lock is not taken
422dc908600SMel Gorman 		 * as memory compaction should not move pages between nodes.
423dc908600SMel Gorman 		 */
424748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
425dc908600SMel Gorman 		if (page_zone(page) != zone)
426dc908600SMel Gorman 			continue;
427dc908600SMel Gorman 
428dc908600SMel Gorman 		/* Skip if free */
429748446bbSMel Gorman 		if (PageBuddy(page))
430748446bbSMel Gorman 			continue;
431748446bbSMel Gorman 
4329927af74SMel Gorman 		/*
4339927af74SMel Gorman 		 * For async migration, also only scan in MOVABLE blocks. Async
4349927af74SMel Gorman 		 * migration is optimistic to see if the minimum amount of work
4359927af74SMel Gorman 		 * satisfies the allocation
4369927af74SMel Gorman 		 */
4379927af74SMel Gorman 		pageblock_nr = low_pfn >> pageblock_order;
4389927af74SMel Gorman 		if (!cc->sync && last_pageblock_nr != pageblock_nr &&
4399927af74SMel Gorman 				get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
4409927af74SMel Gorman 			low_pfn += pageblock_nr_pages;
4419927af74SMel Gorman 			low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
4429927af74SMel Gorman 			last_pageblock_nr = pageblock_nr;
4439927af74SMel Gorman 			continue;
4449927af74SMel Gorman 		}
4459927af74SMel Gorman 
446bc835011SAndrea Arcangeli 		if (!PageLRU(page))
447bc835011SAndrea Arcangeli 			continue;
448bc835011SAndrea Arcangeli 
449bc835011SAndrea Arcangeli 		/*
450bc835011SAndrea Arcangeli 		 * PageLRU is set, and lru_lock excludes isolation,
451bc835011SAndrea Arcangeli 		 * splitting and collapsing (collapsing has already
452bc835011SAndrea Arcangeli 		 * happened if PageLRU is set).
453bc835011SAndrea Arcangeli 		 */
454bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
455bc835011SAndrea Arcangeli 			low_pfn += (1 << compound_order(page)) - 1;
456bc835011SAndrea Arcangeli 			continue;
457bc835011SAndrea Arcangeli 		}
458bc835011SAndrea Arcangeli 
459c8244935SMel Gorman 		if (!cc->sync)
460c8244935SMel Gorman 			mode |= ISOLATE_ASYNC_MIGRATE;
461c8244935SMel Gorman 
462748446bbSMel Gorman 		/* Try isolate the page */
46339deaf85SMinchan Kim 		if (__isolate_lru_page(page, mode, 0) != 0)
464748446bbSMel Gorman 			continue;
465748446bbSMel Gorman 
466bc835011SAndrea Arcangeli 		VM_BUG_ON(PageTransCompound(page));
467bc835011SAndrea Arcangeli 
468748446bbSMel Gorman 		/* Successfully isolated */
469748446bbSMel Gorman 		del_page_from_lru_list(zone, page, page_lru(page));
470748446bbSMel Gorman 		list_add(&page->lru, migratelist);
471748446bbSMel Gorman 		cc->nr_migratepages++;
472b7aba698SMel Gorman 		nr_isolated++;
473748446bbSMel Gorman 
474748446bbSMel Gorman 		/* Avoid isolating too much */
47531b8384aSHillf Danton 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
47631b8384aSHillf Danton 			++low_pfn;
477748446bbSMel Gorman 			break;
478748446bbSMel Gorman 		}
47931b8384aSHillf Danton 	}
480748446bbSMel Gorman 
481748446bbSMel Gorman 	acct_isolated(zone, cc);
482748446bbSMel Gorman 
483748446bbSMel Gorman 	spin_unlock_irq(&zone->lru_lock);
484748446bbSMel Gorman 
485b7aba698SMel Gorman 	trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
486b7aba698SMel Gorman 
4872fe86e00SMichal Nazarewicz 	return low_pfn;
4882fe86e00SMichal Nazarewicz }
4892fe86e00SMichal Nazarewicz 
4902fe86e00SMichal Nazarewicz /*
4912fe86e00SMichal Nazarewicz  * Isolate all pages that can be migrated from the block pointed to by
4922fe86e00SMichal Nazarewicz  * the migrate scanner within compact_control.
4932fe86e00SMichal Nazarewicz  */
4942fe86e00SMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone,
4952fe86e00SMichal Nazarewicz 					struct compact_control *cc)
4962fe86e00SMichal Nazarewicz {
4972fe86e00SMichal Nazarewicz 	unsigned long low_pfn, end_pfn;
4982fe86e00SMichal Nazarewicz 
4992fe86e00SMichal Nazarewicz 	/* Do not scan outside zone boundaries */
5002fe86e00SMichal Nazarewicz 	low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
5012fe86e00SMichal Nazarewicz 
5022fe86e00SMichal Nazarewicz 	/* Only scan within a pageblock boundary */
5032fe86e00SMichal Nazarewicz 	end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
5042fe86e00SMichal Nazarewicz 
5052fe86e00SMichal Nazarewicz 	/* Do not cross the free scanner or scan within a memory hole */
5062fe86e00SMichal Nazarewicz 	if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
5072fe86e00SMichal Nazarewicz 		cc->migrate_pfn = end_pfn;
5082fe86e00SMichal Nazarewicz 		return ISOLATE_NONE;
5092fe86e00SMichal Nazarewicz 	}
5102fe86e00SMichal Nazarewicz 
5112fe86e00SMichal Nazarewicz 	/* Perform the isolation */
5122fe86e00SMichal Nazarewicz 	low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
5132fe86e00SMichal Nazarewicz 	if (!low_pfn)
5142fe86e00SMichal Nazarewicz 		return ISOLATE_ABORT;
5152fe86e00SMichal Nazarewicz 
5162fe86e00SMichal Nazarewicz 	cc->migrate_pfn = low_pfn;
5172fe86e00SMichal Nazarewicz 
518f9e35b3bSMel Gorman 	return ISOLATE_SUCCESS;
519748446bbSMel Gorman }
520748446bbSMel Gorman 
521748446bbSMel Gorman /*
522748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
523748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
524748446bbSMel Gorman  */
525748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
526748446bbSMel Gorman 					unsigned long data,
527748446bbSMel Gorman 					int **result)
528748446bbSMel Gorman {
529748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
530748446bbSMel Gorman 	struct page *freepage;
531748446bbSMel Gorman 
532748446bbSMel Gorman 	/* Isolate free pages if necessary */
533748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
534748446bbSMel Gorman 		isolate_freepages(cc->zone, cc);
535748446bbSMel Gorman 
536748446bbSMel Gorman 		if (list_empty(&cc->freepages))
537748446bbSMel Gorman 			return NULL;
538748446bbSMel Gorman 	}
539748446bbSMel Gorman 
540748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
541748446bbSMel Gorman 	list_del(&freepage->lru);
542748446bbSMel Gorman 	cc->nr_freepages--;
543748446bbSMel Gorman 
544748446bbSMel Gorman 	return freepage;
545748446bbSMel Gorman }
546748446bbSMel Gorman 
547748446bbSMel Gorman /*
548748446bbSMel Gorman  * We cannot control nr_migratepages and nr_freepages fully when migration is
549748446bbSMel Gorman  * running as migrate_pages() has no knowledge of compact_control. When
550748446bbSMel Gorman  * migration is complete, we count the number of pages on the lists by hand.
551748446bbSMel Gorman  */
552748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc)
553748446bbSMel Gorman {
554748446bbSMel Gorman 	int nr_migratepages = 0;
555748446bbSMel Gorman 	int nr_freepages = 0;
556748446bbSMel Gorman 	struct page *page;
557748446bbSMel Gorman 
558748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru)
559748446bbSMel Gorman 		nr_migratepages++;
560748446bbSMel Gorman 	list_for_each_entry(page, &cc->freepages, lru)
561748446bbSMel Gorman 		nr_freepages++;
562748446bbSMel Gorman 
563748446bbSMel Gorman 	cc->nr_migratepages = nr_migratepages;
564748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
565748446bbSMel Gorman }
566748446bbSMel Gorman 
567748446bbSMel Gorman static int compact_finished(struct zone *zone,
568748446bbSMel Gorman 			    struct compact_control *cc)
569748446bbSMel Gorman {
57056de7263SMel Gorman 	unsigned int order;
5715a03b051SAndrea Arcangeli 	unsigned long watermark;
57256de7263SMel Gorman 
573748446bbSMel Gorman 	if (fatal_signal_pending(current))
574748446bbSMel Gorman 		return COMPACT_PARTIAL;
575748446bbSMel Gorman 
576748446bbSMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
577748446bbSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn)
578748446bbSMel Gorman 		return COMPACT_COMPLETE;
579748446bbSMel Gorman 
58082478fb7SJohannes Weiner 	/*
58182478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
58282478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
58382478fb7SJohannes Weiner 	 */
58456de7263SMel Gorman 	if (cc->order == -1)
58556de7263SMel Gorman 		return COMPACT_CONTINUE;
58656de7263SMel Gorman 
5873957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
5883957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
5893957c776SMichal Hocko 	watermark += (1 << cc->order);
5903957c776SMichal Hocko 
5913957c776SMichal Hocko 	if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
5923957c776SMichal Hocko 		return COMPACT_CONTINUE;
5933957c776SMichal Hocko 
59456de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
59556de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
59656de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
59756de7263SMel Gorman 		if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
59856de7263SMel Gorman 			return COMPACT_PARTIAL;
59956de7263SMel Gorman 
60056de7263SMel Gorman 		/* Job done if allocation would set block type */
60156de7263SMel Gorman 		if (order >= pageblock_order && zone->free_area[order].nr_free)
60256de7263SMel Gorman 			return COMPACT_PARTIAL;
60356de7263SMel Gorman 	}
60456de7263SMel Gorman 
605748446bbSMel Gorman 	return COMPACT_CONTINUE;
606748446bbSMel Gorman }
607748446bbSMel Gorman 
6083e7d3449SMel Gorman /*
6093e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
6103e7d3449SMel Gorman  * Returns
6113e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
6123e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
6133e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
6143e7d3449SMel Gorman  */
6153e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order)
6163e7d3449SMel Gorman {
6173e7d3449SMel Gorman 	int fragindex;
6183e7d3449SMel Gorman 	unsigned long watermark;
6193e7d3449SMel Gorman 
6203e7d3449SMel Gorman 	/*
6213957c776SMichal Hocko 	 * order == -1 is expected when compacting via
6223957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
6233957c776SMichal Hocko 	 */
6243957c776SMichal Hocko 	if (order == -1)
6253957c776SMichal Hocko 		return COMPACT_CONTINUE;
6263957c776SMichal Hocko 
6273957c776SMichal Hocko 	/*
6283e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
6293e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
6303e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
6313e7d3449SMel Gorman 	 */
6323e7d3449SMel Gorman 	watermark = low_wmark_pages(zone) + (2UL << order);
6333e7d3449SMel Gorman 	if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
6343e7d3449SMel Gorman 		return COMPACT_SKIPPED;
6353e7d3449SMel Gorman 
6363e7d3449SMel Gorman 	/*
6373e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
6383e7d3449SMel Gorman 	 * low memory or external fragmentation
6393e7d3449SMel Gorman 	 *
640a582a738SShaohua Li 	 * index of -1000 implies allocations might succeed depending on
641a582a738SShaohua Li 	 * watermarks
6423e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
6433e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
6443e7d3449SMel Gorman 	 *
6453e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
6463e7d3449SMel Gorman 	 */
6473e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
6483e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
6493e7d3449SMel Gorman 		return COMPACT_SKIPPED;
6503e7d3449SMel Gorman 
651a582a738SShaohua Li 	if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
652a582a738SShaohua Li 	    0, 0))
6533e7d3449SMel Gorman 		return COMPACT_PARTIAL;
6543e7d3449SMel Gorman 
6553e7d3449SMel Gorman 	return COMPACT_CONTINUE;
6563e7d3449SMel Gorman }
6573e7d3449SMel Gorman 
658748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
659748446bbSMel Gorman {
660748446bbSMel Gorman 	int ret;
661748446bbSMel Gorman 
6623e7d3449SMel Gorman 	ret = compaction_suitable(zone, cc->order);
6633e7d3449SMel Gorman 	switch (ret) {
6643e7d3449SMel Gorman 	case COMPACT_PARTIAL:
6653e7d3449SMel Gorman 	case COMPACT_SKIPPED:
6663e7d3449SMel Gorman 		/* Compaction is likely to fail */
6673e7d3449SMel Gorman 		return ret;
6683e7d3449SMel Gorman 	case COMPACT_CONTINUE:
6693e7d3449SMel Gorman 		/* Fall through to compaction */
6703e7d3449SMel Gorman 		;
6713e7d3449SMel Gorman 	}
6723e7d3449SMel Gorman 
673748446bbSMel Gorman 	/* Setup to move all movable pages to the end of the zone */
674748446bbSMel Gorman 	cc->migrate_pfn = zone->zone_start_pfn;
675748446bbSMel Gorman 	cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
676748446bbSMel Gorman 	cc->free_pfn &= ~(pageblock_nr_pages-1);
677748446bbSMel Gorman 
678748446bbSMel Gorman 	migrate_prep_local();
679748446bbSMel Gorman 
680748446bbSMel Gorman 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
681748446bbSMel Gorman 		unsigned long nr_migrate, nr_remaining;
6829d502c1cSMinchan Kim 		int err;
683748446bbSMel Gorman 
684f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
685f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
686f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
687f9e35b3bSMel Gorman 			goto out;
688f9e35b3bSMel Gorman 		case ISOLATE_NONE:
689748446bbSMel Gorman 			continue;
690f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
691f9e35b3bSMel Gorman 			;
692f9e35b3bSMel Gorman 		}
693748446bbSMel Gorman 
694748446bbSMel Gorman 		nr_migrate = cc->nr_migratepages;
6959d502c1cSMinchan Kim 		err = migrate_pages(&cc->migratepages, compaction_alloc,
6967f0f2496SMel Gorman 				(unsigned long)cc, false,
697a6bc32b8SMel Gorman 				cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
698748446bbSMel Gorman 		update_nr_listpages(cc);
699748446bbSMel Gorman 		nr_remaining = cc->nr_migratepages;
700748446bbSMel Gorman 
701748446bbSMel Gorman 		count_vm_event(COMPACTBLOCKS);
702748446bbSMel Gorman 		count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
703748446bbSMel Gorman 		if (nr_remaining)
704748446bbSMel Gorman 			count_vm_events(COMPACTPAGEFAILED, nr_remaining);
705b7aba698SMel Gorman 		trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
706b7aba698SMel Gorman 						nr_remaining);
707748446bbSMel Gorman 
708748446bbSMel Gorman 		/* Release LRU pages not migrated */
7099d502c1cSMinchan Kim 		if (err) {
710748446bbSMel Gorman 			putback_lru_pages(&cc->migratepages);
711748446bbSMel Gorman 			cc->nr_migratepages = 0;
712748446bbSMel Gorman 		}
713748446bbSMel Gorman 
714748446bbSMel Gorman 	}
715748446bbSMel Gorman 
716f9e35b3bSMel Gorman out:
717748446bbSMel Gorman 	/* Release free pages and check accounting */
718748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
719748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
720748446bbSMel Gorman 
721748446bbSMel Gorman 	return ret;
722748446bbSMel Gorman }
72376ab0f53SMel Gorman 
724d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone,
72577f1fe6bSMel Gorman 				 int order, gfp_t gfp_mask,
726d527caf2SAndrea Arcangeli 				 bool sync)
72756de7263SMel Gorman {
72856de7263SMel Gorman 	struct compact_control cc = {
72956de7263SMel Gorman 		.nr_freepages = 0,
73056de7263SMel Gorman 		.nr_migratepages = 0,
73156de7263SMel Gorman 		.order = order,
73256de7263SMel Gorman 		.migratetype = allocflags_to_migratetype(gfp_mask),
73356de7263SMel Gorman 		.zone = zone,
73477f1fe6bSMel Gorman 		.sync = sync,
73556de7263SMel Gorman 	};
73656de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
73756de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
73856de7263SMel Gorman 
73956de7263SMel Gorman 	return compact_zone(zone, &cc);
74056de7263SMel Gorman }
74156de7263SMel Gorman 
7425e771905SMel Gorman int sysctl_extfrag_threshold = 500;
7435e771905SMel Gorman 
74456de7263SMel Gorman /**
74556de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
74656de7263SMel Gorman  * @zonelist: The zonelist used for the current allocation
74756de7263SMel Gorman  * @order: The order of the current allocation
74856de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
74956de7263SMel Gorman  * @nodemask: The allowed nodes to allocate from
75077f1fe6bSMel Gorman  * @sync: Whether migration is synchronous or not
75156de7263SMel Gorman  *
75256de7263SMel Gorman  * This is the main entry point for direct page compaction.
75356de7263SMel Gorman  */
75456de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist,
75577f1fe6bSMel Gorman 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
75677f1fe6bSMel Gorman 			bool sync)
75756de7263SMel Gorman {
75856de7263SMel Gorman 	enum zone_type high_zoneidx = gfp_zone(gfp_mask);
75956de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
76056de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
76156de7263SMel Gorman 	struct zoneref *z;
76256de7263SMel Gorman 	struct zone *zone;
76356de7263SMel Gorman 	int rc = COMPACT_SKIPPED;
76456de7263SMel Gorman 
76556de7263SMel Gorman 	/*
76656de7263SMel Gorman 	 * Check whether it is worth even starting compaction. The order check is
76756de7263SMel Gorman 	 * made because an assumption is made that the page allocator can satisfy
76856de7263SMel Gorman 	 * the "cheaper" orders without taking special steps
76956de7263SMel Gorman 	 */
770c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
77156de7263SMel Gorman 		return rc;
77256de7263SMel Gorman 
77356de7263SMel Gorman 	count_vm_event(COMPACTSTALL);
77456de7263SMel Gorman 
77556de7263SMel Gorman 	/* Compact each zone in the list */
77656de7263SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
77756de7263SMel Gorman 								nodemask) {
77856de7263SMel Gorman 		int status;
77956de7263SMel Gorman 
780d527caf2SAndrea Arcangeli 		status = compact_zone_order(zone, order, gfp_mask, sync);
78156de7263SMel Gorman 		rc = max(status, rc);
78256de7263SMel Gorman 
7833e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
7843e7d3449SMel Gorman 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
78556de7263SMel Gorman 			break;
78656de7263SMel Gorman 	}
78756de7263SMel Gorman 
78856de7263SMel Gorman 	return rc;
78956de7263SMel Gorman }
79056de7263SMel Gorman 
79156de7263SMel Gorman 
79276ab0f53SMel Gorman /* Compact all zones within a node */
7937be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
79476ab0f53SMel Gorman {
79576ab0f53SMel Gorman 	int zoneid;
79676ab0f53SMel Gorman 	struct zone *zone;
79776ab0f53SMel Gorman 
79876ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
79976ab0f53SMel Gorman 
80076ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
80176ab0f53SMel Gorman 		if (!populated_zone(zone))
80276ab0f53SMel Gorman 			continue;
80376ab0f53SMel Gorman 
8047be62de9SRik van Riel 		cc->nr_freepages = 0;
8057be62de9SRik van Riel 		cc->nr_migratepages = 0;
8067be62de9SRik van Riel 		cc->zone = zone;
8077be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->freepages);
8087be62de9SRik van Riel 		INIT_LIST_HEAD(&cc->migratepages);
80976ab0f53SMel Gorman 
810aad6ec37SDan Carpenter 		if (cc->order == -1 || !compaction_deferred(zone, cc->order))
8117be62de9SRik van Riel 			compact_zone(zone, cc);
81276ab0f53SMel Gorman 
813aff62249SRik van Riel 		if (cc->order > 0) {
814aff62249SRik van Riel 			int ok = zone_watermark_ok(zone, cc->order,
815aff62249SRik van Riel 						low_wmark_pages(zone), 0, 0);
816aff62249SRik van Riel 			if (ok && cc->order > zone->compact_order_failed)
817aff62249SRik van Riel 				zone->compact_order_failed = cc->order + 1;
818aff62249SRik van Riel 			/* Currently async compaction is never deferred. */
819aff62249SRik van Riel 			else if (!ok && cc->sync)
820aff62249SRik van Riel 				defer_compaction(zone, cc->order);
821aff62249SRik van Riel 		}
822aff62249SRik van Riel 
8237be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->freepages));
8247be62de9SRik van Riel 		VM_BUG_ON(!list_empty(&cc->migratepages));
82576ab0f53SMel Gorman 	}
82676ab0f53SMel Gorman 
82776ab0f53SMel Gorman 	return 0;
82876ab0f53SMel Gorman }
82976ab0f53SMel Gorman 
8307be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order)
8317be62de9SRik van Riel {
8327be62de9SRik van Riel 	struct compact_control cc = {
8337be62de9SRik van Riel 		.order = order,
8347be62de9SRik van Riel 		.sync = false,
8357be62de9SRik van Riel 	};
8367be62de9SRik van Riel 
8377be62de9SRik van Riel 	return __compact_pgdat(pgdat, &cc);
8387be62de9SRik van Riel }
8397be62de9SRik van Riel 
8407be62de9SRik van Riel static int compact_node(int nid)
8417be62de9SRik van Riel {
8427be62de9SRik van Riel 	struct compact_control cc = {
8437be62de9SRik van Riel 		.order = -1,
8447be62de9SRik van Riel 		.sync = true,
8457be62de9SRik van Riel 	};
8467be62de9SRik van Riel 
8478575ec29SHugh Dickins 	return __compact_pgdat(NODE_DATA(nid), &cc);
8487be62de9SRik van Riel }
8497be62de9SRik van Riel 
85076ab0f53SMel Gorman /* Compact all nodes in the system */
85176ab0f53SMel Gorman static int compact_nodes(void)
85276ab0f53SMel Gorman {
85376ab0f53SMel Gorman 	int nid;
85476ab0f53SMel Gorman 
8558575ec29SHugh Dickins 	/* Flush pending updates to the LRU lists */
8568575ec29SHugh Dickins 	lru_add_drain_all();
8578575ec29SHugh Dickins 
85876ab0f53SMel Gorman 	for_each_online_node(nid)
85976ab0f53SMel Gorman 		compact_node(nid);
86076ab0f53SMel Gorman 
86176ab0f53SMel Gorman 	return COMPACT_COMPLETE;
86276ab0f53SMel Gorman }
86376ab0f53SMel Gorman 
86476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
86576ab0f53SMel Gorman int sysctl_compact_memory;
86676ab0f53SMel Gorman 
86776ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
86876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
86976ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
87076ab0f53SMel Gorman {
87176ab0f53SMel Gorman 	if (write)
87276ab0f53SMel Gorman 		return compact_nodes();
87376ab0f53SMel Gorman 
87476ab0f53SMel Gorman 	return 0;
87576ab0f53SMel Gorman }
876ed4a6d7fSMel Gorman 
8775e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
8785e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
8795e771905SMel Gorman {
8805e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
8815e771905SMel Gorman 
8825e771905SMel Gorman 	return 0;
8835e771905SMel Gorman }
8845e771905SMel Gorman 
885ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
88610fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev,
88710fbcf4cSKay Sievers 			struct device_attribute *attr,
888ed4a6d7fSMel Gorman 			const char *buf, size_t count)
889ed4a6d7fSMel Gorman {
8908575ec29SHugh Dickins 	int nid = dev->id;
8918575ec29SHugh Dickins 
8928575ec29SHugh Dickins 	if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
8938575ec29SHugh Dickins 		/* Flush pending updates to the LRU lists */
8948575ec29SHugh Dickins 		lru_add_drain_all();
8958575ec29SHugh Dickins 
8968575ec29SHugh Dickins 		compact_node(nid);
8978575ec29SHugh Dickins 	}
898ed4a6d7fSMel Gorman 
899ed4a6d7fSMel Gorman 	return count;
900ed4a6d7fSMel Gorman }
90110fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
902ed4a6d7fSMel Gorman 
903ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
904ed4a6d7fSMel Gorman {
90510fbcf4cSKay Sievers 	return device_create_file(&node->dev, &dev_attr_compact);
906ed4a6d7fSMel Gorman }
907ed4a6d7fSMel Gorman 
908ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
909ed4a6d7fSMel Gorman {
91010fbcf4cSKay Sievers 	return device_remove_file(&node->dev, &dev_attr_compact);
911ed4a6d7fSMel Gorman }
912ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
913