xref: /openbmc/linux/mm/compaction.c (revision f9e35b3b41f47c4e17d8132edbcab305a6aaa4b0)
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 
38748446bbSMel Gorman 	/* Account for isolated anon and file pages */
39748446bbSMel Gorman 	unsigned long nr_anon;
40748446bbSMel Gorman 	unsigned long nr_file;
41748446bbSMel Gorman 
4256de7263SMel Gorman 	unsigned int order;		/* order a direct compactor needs */
4356de7263SMel Gorman 	int migratetype;		/* MOVABLE, RECLAIMABLE etc */
44748446bbSMel Gorman 	struct zone *zone;
45748446bbSMel Gorman };
46748446bbSMel Gorman 
47748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist)
48748446bbSMel Gorman {
49748446bbSMel Gorman 	struct page *page, *next;
50748446bbSMel Gorman 	unsigned long count = 0;
51748446bbSMel Gorman 
52748446bbSMel Gorman 	list_for_each_entry_safe(page, next, freelist, lru) {
53748446bbSMel Gorman 		list_del(&page->lru);
54748446bbSMel Gorman 		__free_page(page);
55748446bbSMel Gorman 		count++;
56748446bbSMel Gorman 	}
57748446bbSMel Gorman 
58748446bbSMel Gorman 	return count;
59748446bbSMel Gorman }
60748446bbSMel Gorman 
61748446bbSMel Gorman /* Isolate free pages onto a private freelist. Must hold zone->lock */
62748446bbSMel Gorman static unsigned long isolate_freepages_block(struct zone *zone,
63748446bbSMel Gorman 				unsigned long blockpfn,
64748446bbSMel Gorman 				struct list_head *freelist)
65748446bbSMel Gorman {
66748446bbSMel Gorman 	unsigned long zone_end_pfn, end_pfn;
67b7aba698SMel Gorman 	int nr_scanned = 0, total_isolated = 0;
68748446bbSMel Gorman 	struct page *cursor;
69748446bbSMel Gorman 
70748446bbSMel Gorman 	/* Get the last PFN we should scan for free pages at */
71748446bbSMel Gorman 	zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
72748446bbSMel Gorman 	end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
73748446bbSMel Gorman 
74748446bbSMel Gorman 	/* Find the first usable PFN in the block to initialse page cursor */
75748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++) {
76748446bbSMel Gorman 		if (pfn_valid_within(blockpfn))
77748446bbSMel Gorman 			break;
78748446bbSMel Gorman 	}
79748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
80748446bbSMel Gorman 
81748446bbSMel Gorman 	/* Isolate free pages. This assumes the block is valid */
82748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
83748446bbSMel Gorman 		int isolated, i;
84748446bbSMel Gorman 		struct page *page = cursor;
85748446bbSMel Gorman 
86748446bbSMel Gorman 		if (!pfn_valid_within(blockpfn))
87748446bbSMel Gorman 			continue;
88b7aba698SMel Gorman 		nr_scanned++;
89748446bbSMel Gorman 
90748446bbSMel Gorman 		if (!PageBuddy(page))
91748446bbSMel Gorman 			continue;
92748446bbSMel Gorman 
93748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
94748446bbSMel Gorman 		isolated = split_free_page(page);
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 
112748446bbSMel Gorman /* Returns true if the page is within a block suitable for migration to */
113748446bbSMel Gorman static bool suitable_migration_target(struct page *page)
114748446bbSMel Gorman {
115748446bbSMel Gorman 
116748446bbSMel Gorman 	int migratetype = get_pageblock_migratetype(page);
117748446bbSMel Gorman 
118748446bbSMel Gorman 	/* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
119748446bbSMel Gorman 	if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
120748446bbSMel Gorman 		return false;
121748446bbSMel Gorman 
122748446bbSMel Gorman 	/* If the page is a large free page, then allow migration */
123748446bbSMel Gorman 	if (PageBuddy(page) && page_order(page) >= pageblock_order)
124748446bbSMel Gorman 		return true;
125748446bbSMel Gorman 
126748446bbSMel Gorman 	/* If the block is MIGRATE_MOVABLE, allow migration */
127748446bbSMel Gorman 	if (migratetype == MIGRATE_MOVABLE)
128748446bbSMel Gorman 		return true;
129748446bbSMel Gorman 
130748446bbSMel Gorman 	/* Otherwise skip the block */
131748446bbSMel Gorman 	return false;
132748446bbSMel Gorman }
133748446bbSMel Gorman 
134748446bbSMel Gorman /*
135748446bbSMel Gorman  * Based on information in the current compact_control, find blocks
136748446bbSMel Gorman  * suitable for isolating free pages from and then isolate them.
137748446bbSMel Gorman  */
138748446bbSMel Gorman static void isolate_freepages(struct zone *zone,
139748446bbSMel Gorman 				struct compact_control *cc)
140748446bbSMel Gorman {
141748446bbSMel Gorman 	struct page *page;
142748446bbSMel Gorman 	unsigned long high_pfn, low_pfn, pfn;
143748446bbSMel Gorman 	unsigned long flags;
144748446bbSMel Gorman 	int nr_freepages = cc->nr_freepages;
145748446bbSMel Gorman 	struct list_head *freelist = &cc->freepages;
146748446bbSMel Gorman 
1477454f4baSMel Gorman 	/*
1487454f4baSMel Gorman 	 * Initialise the free scanner. The starting point is where we last
1497454f4baSMel Gorman 	 * scanned from (or the end of the zone if starting). The low point
1507454f4baSMel Gorman 	 * is the end of the pageblock the migration scanner is using.
1517454f4baSMel Gorman 	 */
152748446bbSMel Gorman 	pfn = cc->free_pfn;
153748446bbSMel Gorman 	low_pfn = cc->migrate_pfn + pageblock_nr_pages;
1547454f4baSMel Gorman 
1557454f4baSMel Gorman 	/*
1567454f4baSMel Gorman 	 * Take care that if the migration scanner is at the end of the zone
1577454f4baSMel Gorman 	 * that the free scanner does not accidentally move to the next zone
1587454f4baSMel Gorman 	 * in the next isolation cycle.
1597454f4baSMel Gorman 	 */
1607454f4baSMel Gorman 	high_pfn = min(low_pfn, pfn);
161748446bbSMel Gorman 
162748446bbSMel Gorman 	/*
163748446bbSMel Gorman 	 * Isolate free pages until enough are available to migrate the
164748446bbSMel Gorman 	 * pages on cc->migratepages. We stop searching if the migrate
165748446bbSMel Gorman 	 * and free page scanners meet or enough free pages are isolated.
166748446bbSMel Gorman 	 */
167748446bbSMel Gorman 	for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
168748446bbSMel Gorman 					pfn -= pageblock_nr_pages) {
169748446bbSMel Gorman 		unsigned long isolated;
170748446bbSMel Gorman 
171748446bbSMel Gorman 		if (!pfn_valid(pfn))
172748446bbSMel Gorman 			continue;
173748446bbSMel Gorman 
174748446bbSMel Gorman 		/*
175748446bbSMel Gorman 		 * Check for overlapping nodes/zones. It's possible on some
176748446bbSMel Gorman 		 * configurations to have a setup like
177748446bbSMel Gorman 		 * node0 node1 node0
178748446bbSMel Gorman 		 * i.e. it's possible that all pages within a zones range of
179748446bbSMel Gorman 		 * pages do not belong to a single zone.
180748446bbSMel Gorman 		 */
181748446bbSMel Gorman 		page = pfn_to_page(pfn);
182748446bbSMel Gorman 		if (page_zone(page) != zone)
183748446bbSMel Gorman 			continue;
184748446bbSMel Gorman 
185748446bbSMel Gorman 		/* Check the block is suitable for migration */
186748446bbSMel Gorman 		if (!suitable_migration_target(page))
187748446bbSMel Gorman 			continue;
188748446bbSMel Gorman 
189602605a4SMel Gorman 		/*
190602605a4SMel Gorman 		 * Found a block suitable for isolating free pages from. Now
191602605a4SMel Gorman 		 * we disabled interrupts, double check things are ok and
192602605a4SMel Gorman 		 * isolate the pages. This is to minimise the time IRQs
193602605a4SMel Gorman 		 * are disabled
194602605a4SMel Gorman 		 */
195602605a4SMel Gorman 		isolated = 0;
196602605a4SMel Gorman 		spin_lock_irqsave(&zone->lock, flags);
197602605a4SMel Gorman 		if (suitable_migration_target(page)) {
198748446bbSMel Gorman 			isolated = isolate_freepages_block(zone, pfn, freelist);
199748446bbSMel Gorman 			nr_freepages += isolated;
200602605a4SMel Gorman 		}
201602605a4SMel Gorman 		spin_unlock_irqrestore(&zone->lock, flags);
202748446bbSMel Gorman 
203748446bbSMel Gorman 		/*
204748446bbSMel Gorman 		 * Record the highest PFN we isolated pages from. When next
205748446bbSMel Gorman 		 * looking for free pages, the search will restart here as
206748446bbSMel Gorman 		 * page migration may have returned some pages to the allocator
207748446bbSMel Gorman 		 */
208748446bbSMel Gorman 		if (isolated)
209748446bbSMel Gorman 			high_pfn = max(high_pfn, pfn);
210748446bbSMel Gorman 	}
211748446bbSMel Gorman 
212748446bbSMel Gorman 	/* split_free_page does not map the pages */
213748446bbSMel Gorman 	list_for_each_entry(page, freelist, lru) {
214748446bbSMel Gorman 		arch_alloc_page(page, 0);
215748446bbSMel Gorman 		kernel_map_pages(page, 1, 1);
216748446bbSMel Gorman 	}
217748446bbSMel Gorman 
218748446bbSMel Gorman 	cc->free_pfn = high_pfn;
219748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
220748446bbSMel Gorman }
221748446bbSMel Gorman 
222748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
223748446bbSMel Gorman static void acct_isolated(struct zone *zone, struct compact_control *cc)
224748446bbSMel Gorman {
225748446bbSMel Gorman 	struct page *page;
226748446bbSMel Gorman 	unsigned int count[NR_LRU_LISTS] = { 0, };
227748446bbSMel Gorman 
228748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru) {
229748446bbSMel Gorman 		int lru = page_lru_base_type(page);
230748446bbSMel Gorman 		count[lru]++;
231748446bbSMel Gorman 	}
232748446bbSMel Gorman 
233748446bbSMel Gorman 	cc->nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
234748446bbSMel Gorman 	cc->nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
235748446bbSMel Gorman 	__mod_zone_page_state(zone, NR_ISOLATED_ANON, cc->nr_anon);
236748446bbSMel Gorman 	__mod_zone_page_state(zone, NR_ISOLATED_FILE, cc->nr_file);
237748446bbSMel Gorman }
238748446bbSMel Gorman 
239748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
240748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
241748446bbSMel Gorman {
242bc693045SMinchan Kim 	unsigned long active, inactive, isolated;
243748446bbSMel Gorman 
244748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
245748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
246bc693045SMinchan Kim 	active = zone_page_state(zone, NR_ACTIVE_FILE) +
247bc693045SMinchan Kim 					zone_page_state(zone, NR_ACTIVE_ANON);
248748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
249748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
250748446bbSMel Gorman 
251bc693045SMinchan Kim 	return isolated > (inactive + active) / 2;
252748446bbSMel Gorman }
253748446bbSMel Gorman 
254*f9e35b3bSMel Gorman /* possible outcome of isolate_migratepages */
255*f9e35b3bSMel Gorman typedef enum {
256*f9e35b3bSMel Gorman 	ISOLATE_ABORT,		/* Abort compaction now */
257*f9e35b3bSMel Gorman 	ISOLATE_NONE,		/* No pages isolated, continue scanning */
258*f9e35b3bSMel Gorman 	ISOLATE_SUCCESS,	/* Pages isolated, migrate */
259*f9e35b3bSMel Gorman } isolate_migrate_t;
260*f9e35b3bSMel Gorman 
261748446bbSMel Gorman /*
262748446bbSMel Gorman  * Isolate all pages that can be migrated from the block pointed to by
263748446bbSMel Gorman  * the migrate scanner within compact_control.
264748446bbSMel Gorman  */
265*f9e35b3bSMel Gorman static isolate_migrate_t isolate_migratepages(struct zone *zone,
266748446bbSMel Gorman 					struct compact_control *cc)
267748446bbSMel Gorman {
268748446bbSMel Gorman 	unsigned long low_pfn, end_pfn;
2699927af74SMel Gorman 	unsigned long last_pageblock_nr = 0, pageblock_nr;
270b7aba698SMel Gorman 	unsigned long nr_scanned = 0, nr_isolated = 0;
271748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
272748446bbSMel Gorman 
273748446bbSMel Gorman 	/* Do not scan outside zone boundaries */
274748446bbSMel Gorman 	low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
275748446bbSMel Gorman 
276748446bbSMel Gorman 	/* Only scan within a pageblock boundary */
277748446bbSMel Gorman 	end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
278748446bbSMel Gorman 
279748446bbSMel Gorman 	/* Do not cross the free scanner or scan within a memory hole */
280748446bbSMel Gorman 	if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
281748446bbSMel Gorman 		cc->migrate_pfn = end_pfn;
282*f9e35b3bSMel Gorman 		return ISOLATE_NONE;
283748446bbSMel Gorman 	}
284748446bbSMel Gorman 
285748446bbSMel Gorman 	/*
286748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
287748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
288748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
289748446bbSMel Gorman 	 */
290748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
291*f9e35b3bSMel Gorman 		/* async migration should just abort */
292*f9e35b3bSMel Gorman 		if (!cc->sync)
293*f9e35b3bSMel Gorman 			return ISOLATE_ABORT;
294*f9e35b3bSMel Gorman 
295748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
296748446bbSMel Gorman 
297748446bbSMel Gorman 		if (fatal_signal_pending(current))
298*f9e35b3bSMel Gorman 			return ISOLATE_ABORT;
299748446bbSMel Gorman 	}
300748446bbSMel Gorman 
301748446bbSMel Gorman 	/* Time to isolate some pages for migration */
302b2eef8c0SAndrea Arcangeli 	cond_resched();
303748446bbSMel Gorman 	spin_lock_irq(&zone->lru_lock);
304748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
305748446bbSMel Gorman 		struct page *page;
306b2eef8c0SAndrea Arcangeli 		bool locked = true;
307b2eef8c0SAndrea Arcangeli 
308b2eef8c0SAndrea Arcangeli 		/* give a chance to irqs before checking need_resched() */
309b2eef8c0SAndrea Arcangeli 		if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
310b2eef8c0SAndrea Arcangeli 			spin_unlock_irq(&zone->lru_lock);
311b2eef8c0SAndrea Arcangeli 			locked = false;
312b2eef8c0SAndrea Arcangeli 		}
313b2eef8c0SAndrea Arcangeli 		if (need_resched() || spin_is_contended(&zone->lru_lock)) {
314b2eef8c0SAndrea Arcangeli 			if (locked)
315b2eef8c0SAndrea Arcangeli 				spin_unlock_irq(&zone->lru_lock);
316b2eef8c0SAndrea Arcangeli 			cond_resched();
317b2eef8c0SAndrea Arcangeli 			spin_lock_irq(&zone->lru_lock);
318b2eef8c0SAndrea Arcangeli 			if (fatal_signal_pending(current))
319b2eef8c0SAndrea Arcangeli 				break;
320b2eef8c0SAndrea Arcangeli 		} else if (!locked)
321b2eef8c0SAndrea Arcangeli 			spin_lock_irq(&zone->lru_lock);
322b2eef8c0SAndrea Arcangeli 
323748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
324748446bbSMel Gorman 			continue;
325b7aba698SMel Gorman 		nr_scanned++;
326748446bbSMel Gorman 
327748446bbSMel Gorman 		/* Get the page and skip if free */
328748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
329748446bbSMel Gorman 		if (PageBuddy(page))
330748446bbSMel Gorman 			continue;
331748446bbSMel Gorman 
3329927af74SMel Gorman 		/*
3339927af74SMel Gorman 		 * For async migration, also only scan in MOVABLE blocks. Async
3349927af74SMel Gorman 		 * migration is optimistic to see if the minimum amount of work
3359927af74SMel Gorman 		 * satisfies the allocation
3369927af74SMel Gorman 		 */
3379927af74SMel Gorman 		pageblock_nr = low_pfn >> pageblock_order;
3389927af74SMel Gorman 		if (!cc->sync && last_pageblock_nr != pageblock_nr &&
3399927af74SMel Gorman 				get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
3409927af74SMel Gorman 			low_pfn += pageblock_nr_pages;
3419927af74SMel Gorman 			low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
3429927af74SMel Gorman 			last_pageblock_nr = pageblock_nr;
3439927af74SMel Gorman 			continue;
3449927af74SMel Gorman 		}
3459927af74SMel Gorman 
346bc835011SAndrea Arcangeli 		if (!PageLRU(page))
347bc835011SAndrea Arcangeli 			continue;
348bc835011SAndrea Arcangeli 
349bc835011SAndrea Arcangeli 		/*
350bc835011SAndrea Arcangeli 		 * PageLRU is set, and lru_lock excludes isolation,
351bc835011SAndrea Arcangeli 		 * splitting and collapsing (collapsing has already
352bc835011SAndrea Arcangeli 		 * happened if PageLRU is set).
353bc835011SAndrea Arcangeli 		 */
354bc835011SAndrea Arcangeli 		if (PageTransHuge(page)) {
355bc835011SAndrea Arcangeli 			low_pfn += (1 << compound_order(page)) - 1;
356bc835011SAndrea Arcangeli 			continue;
357bc835011SAndrea Arcangeli 		}
358bc835011SAndrea Arcangeli 
359748446bbSMel Gorman 		/* Try isolate the page */
360748446bbSMel Gorman 		if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
361748446bbSMel Gorman 			continue;
362748446bbSMel Gorman 
363bc835011SAndrea Arcangeli 		VM_BUG_ON(PageTransCompound(page));
364bc835011SAndrea Arcangeli 
365748446bbSMel Gorman 		/* Successfully isolated */
366748446bbSMel Gorman 		del_page_from_lru_list(zone, page, page_lru(page));
367748446bbSMel Gorman 		list_add(&page->lru, migratelist);
368748446bbSMel Gorman 		cc->nr_migratepages++;
369b7aba698SMel Gorman 		nr_isolated++;
370748446bbSMel Gorman 
371748446bbSMel Gorman 		/* Avoid isolating too much */
372748446bbSMel Gorman 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
373748446bbSMel Gorman 			break;
374748446bbSMel Gorman 	}
375748446bbSMel Gorman 
376748446bbSMel Gorman 	acct_isolated(zone, cc);
377748446bbSMel Gorman 
378748446bbSMel Gorman 	spin_unlock_irq(&zone->lru_lock);
379748446bbSMel Gorman 	cc->migrate_pfn = low_pfn;
380748446bbSMel Gorman 
381b7aba698SMel Gorman 	trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
382b7aba698SMel Gorman 
383*f9e35b3bSMel Gorman 	return ISOLATE_SUCCESS;
384748446bbSMel Gorman }
385748446bbSMel Gorman 
386748446bbSMel Gorman /*
387748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
388748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
389748446bbSMel Gorman  */
390748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
391748446bbSMel Gorman 					unsigned long data,
392748446bbSMel Gorman 					int **result)
393748446bbSMel Gorman {
394748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
395748446bbSMel Gorman 	struct page *freepage;
396748446bbSMel Gorman 
397748446bbSMel Gorman 	/* Isolate free pages if necessary */
398748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
399748446bbSMel Gorman 		isolate_freepages(cc->zone, cc);
400748446bbSMel Gorman 
401748446bbSMel Gorman 		if (list_empty(&cc->freepages))
402748446bbSMel Gorman 			return NULL;
403748446bbSMel Gorman 	}
404748446bbSMel Gorman 
405748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
406748446bbSMel Gorman 	list_del(&freepage->lru);
407748446bbSMel Gorman 	cc->nr_freepages--;
408748446bbSMel Gorman 
409748446bbSMel Gorman 	return freepage;
410748446bbSMel Gorman }
411748446bbSMel Gorman 
412748446bbSMel Gorman /*
413748446bbSMel Gorman  * We cannot control nr_migratepages and nr_freepages fully when migration is
414748446bbSMel Gorman  * running as migrate_pages() has no knowledge of compact_control. When
415748446bbSMel Gorman  * migration is complete, we count the number of pages on the lists by hand.
416748446bbSMel Gorman  */
417748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc)
418748446bbSMel Gorman {
419748446bbSMel Gorman 	int nr_migratepages = 0;
420748446bbSMel Gorman 	int nr_freepages = 0;
421748446bbSMel Gorman 	struct page *page;
422748446bbSMel Gorman 
423748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru)
424748446bbSMel Gorman 		nr_migratepages++;
425748446bbSMel Gorman 	list_for_each_entry(page, &cc->freepages, lru)
426748446bbSMel Gorman 		nr_freepages++;
427748446bbSMel Gorman 
428748446bbSMel Gorman 	cc->nr_migratepages = nr_migratepages;
429748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
430748446bbSMel Gorman }
431748446bbSMel Gorman 
432748446bbSMel Gorman static int compact_finished(struct zone *zone,
433748446bbSMel Gorman 			    struct compact_control *cc)
434748446bbSMel Gorman {
43556de7263SMel Gorman 	unsigned int order;
4365a03b051SAndrea Arcangeli 	unsigned long watermark;
43756de7263SMel Gorman 
438748446bbSMel Gorman 	if (fatal_signal_pending(current))
439748446bbSMel Gorman 		return COMPACT_PARTIAL;
440748446bbSMel Gorman 
441748446bbSMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
442748446bbSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn)
443748446bbSMel Gorman 		return COMPACT_COMPLETE;
444748446bbSMel Gorman 
44582478fb7SJohannes Weiner 	/*
44682478fb7SJohannes Weiner 	 * order == -1 is expected when compacting via
44782478fb7SJohannes Weiner 	 * /proc/sys/vm/compact_memory
44882478fb7SJohannes Weiner 	 */
44956de7263SMel Gorman 	if (cc->order == -1)
45056de7263SMel Gorman 		return COMPACT_CONTINUE;
45156de7263SMel Gorman 
4523957c776SMichal Hocko 	/* Compaction run is not finished if the watermark is not met */
4533957c776SMichal Hocko 	watermark = low_wmark_pages(zone);
4543957c776SMichal Hocko 	watermark += (1 << cc->order);
4553957c776SMichal Hocko 
4563957c776SMichal Hocko 	if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
4573957c776SMichal Hocko 		return COMPACT_CONTINUE;
4583957c776SMichal Hocko 
45956de7263SMel Gorman 	/* Direct compactor: Is a suitable page free? */
46056de7263SMel Gorman 	for (order = cc->order; order < MAX_ORDER; order++) {
46156de7263SMel Gorman 		/* Job done if page is free of the right migratetype */
46256de7263SMel Gorman 		if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
46356de7263SMel Gorman 			return COMPACT_PARTIAL;
46456de7263SMel Gorman 
46556de7263SMel Gorman 		/* Job done if allocation would set block type */
46656de7263SMel Gorman 		if (order >= pageblock_order && zone->free_area[order].nr_free)
46756de7263SMel Gorman 			return COMPACT_PARTIAL;
46856de7263SMel Gorman 	}
46956de7263SMel Gorman 
470748446bbSMel Gorman 	return COMPACT_CONTINUE;
471748446bbSMel Gorman }
472748446bbSMel Gorman 
4733e7d3449SMel Gorman /*
4743e7d3449SMel Gorman  * compaction_suitable: Is this suitable to run compaction on this zone now?
4753e7d3449SMel Gorman  * Returns
4763e7d3449SMel Gorman  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
4773e7d3449SMel Gorman  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
4783e7d3449SMel Gorman  *   COMPACT_CONTINUE - If compaction should run now
4793e7d3449SMel Gorman  */
4803e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order)
4813e7d3449SMel Gorman {
4823e7d3449SMel Gorman 	int fragindex;
4833e7d3449SMel Gorman 	unsigned long watermark;
4843e7d3449SMel Gorman 
4853e7d3449SMel Gorman 	/*
4863957c776SMichal Hocko 	 * order == -1 is expected when compacting via
4873957c776SMichal Hocko 	 * /proc/sys/vm/compact_memory
4883957c776SMichal Hocko 	 */
4893957c776SMichal Hocko 	if (order == -1)
4903957c776SMichal Hocko 		return COMPACT_CONTINUE;
4913957c776SMichal Hocko 
4923957c776SMichal Hocko 	/*
4933e7d3449SMel Gorman 	 * Watermarks for order-0 must be met for compaction. Note the 2UL.
4943e7d3449SMel Gorman 	 * This is because during migration, copies of pages need to be
4953e7d3449SMel Gorman 	 * allocated and for a short time, the footprint is higher
4963e7d3449SMel Gorman 	 */
4973e7d3449SMel Gorman 	watermark = low_wmark_pages(zone) + (2UL << order);
4983e7d3449SMel Gorman 	if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
4993e7d3449SMel Gorman 		return COMPACT_SKIPPED;
5003e7d3449SMel Gorman 
5013e7d3449SMel Gorman 	/*
5023e7d3449SMel Gorman 	 * fragmentation index determines if allocation failures are due to
5033e7d3449SMel Gorman 	 * low memory or external fragmentation
5043e7d3449SMel Gorman 	 *
505a582a738SShaohua Li 	 * index of -1000 implies allocations might succeed depending on
506a582a738SShaohua Li 	 * watermarks
5073e7d3449SMel Gorman 	 * index towards 0 implies failure is due to lack of memory
5083e7d3449SMel Gorman 	 * index towards 1000 implies failure is due to fragmentation
5093e7d3449SMel Gorman 	 *
5103e7d3449SMel Gorman 	 * Only compact if a failure would be due to fragmentation.
5113e7d3449SMel Gorman 	 */
5123e7d3449SMel Gorman 	fragindex = fragmentation_index(zone, order);
5133e7d3449SMel Gorman 	if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
5143e7d3449SMel Gorman 		return COMPACT_SKIPPED;
5153e7d3449SMel Gorman 
516a582a738SShaohua Li 	if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
517a582a738SShaohua Li 	    0, 0))
5183e7d3449SMel Gorman 		return COMPACT_PARTIAL;
5193e7d3449SMel Gorman 
5203e7d3449SMel Gorman 	return COMPACT_CONTINUE;
5213e7d3449SMel Gorman }
5223e7d3449SMel Gorman 
523748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
524748446bbSMel Gorman {
525748446bbSMel Gorman 	int ret;
526748446bbSMel Gorman 
5273e7d3449SMel Gorman 	ret = compaction_suitable(zone, cc->order);
5283e7d3449SMel Gorman 	switch (ret) {
5293e7d3449SMel Gorman 	case COMPACT_PARTIAL:
5303e7d3449SMel Gorman 	case COMPACT_SKIPPED:
5313e7d3449SMel Gorman 		/* Compaction is likely to fail */
5323e7d3449SMel Gorman 		return ret;
5333e7d3449SMel Gorman 	case COMPACT_CONTINUE:
5343e7d3449SMel Gorman 		/* Fall through to compaction */
5353e7d3449SMel Gorman 		;
5363e7d3449SMel Gorman 	}
5373e7d3449SMel Gorman 
538748446bbSMel Gorman 	/* Setup to move all movable pages to the end of the zone */
539748446bbSMel Gorman 	cc->migrate_pfn = zone->zone_start_pfn;
540748446bbSMel Gorman 	cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
541748446bbSMel Gorman 	cc->free_pfn &= ~(pageblock_nr_pages-1);
542748446bbSMel Gorman 
543748446bbSMel Gorman 	migrate_prep_local();
544748446bbSMel Gorman 
545748446bbSMel Gorman 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
546748446bbSMel Gorman 		unsigned long nr_migrate, nr_remaining;
5479d502c1cSMinchan Kim 		int err;
548748446bbSMel Gorman 
549*f9e35b3bSMel Gorman 		switch (isolate_migratepages(zone, cc)) {
550*f9e35b3bSMel Gorman 		case ISOLATE_ABORT:
551*f9e35b3bSMel Gorman 			ret = COMPACT_PARTIAL;
552*f9e35b3bSMel Gorman 			goto out;
553*f9e35b3bSMel Gorman 		case ISOLATE_NONE:
554748446bbSMel Gorman 			continue;
555*f9e35b3bSMel Gorman 		case ISOLATE_SUCCESS:
556*f9e35b3bSMel Gorman 			;
557*f9e35b3bSMel Gorman 		}
558748446bbSMel Gorman 
559748446bbSMel Gorman 		nr_migrate = cc->nr_migratepages;
5609d502c1cSMinchan Kim 		err = migrate_pages(&cc->migratepages, compaction_alloc,
5617f0f2496SMel Gorman 				(unsigned long)cc, false,
56277f1fe6bSMel Gorman 				cc->sync);
563748446bbSMel Gorman 		update_nr_listpages(cc);
564748446bbSMel Gorman 		nr_remaining = cc->nr_migratepages;
565748446bbSMel Gorman 
566748446bbSMel Gorman 		count_vm_event(COMPACTBLOCKS);
567748446bbSMel Gorman 		count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
568748446bbSMel Gorman 		if (nr_remaining)
569748446bbSMel Gorman 			count_vm_events(COMPACTPAGEFAILED, nr_remaining);
570b7aba698SMel Gorman 		trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
571b7aba698SMel Gorman 						nr_remaining);
572748446bbSMel Gorman 
573748446bbSMel Gorman 		/* Release LRU pages not migrated */
5749d502c1cSMinchan Kim 		if (err) {
575748446bbSMel Gorman 			putback_lru_pages(&cc->migratepages);
576748446bbSMel Gorman 			cc->nr_migratepages = 0;
577748446bbSMel Gorman 		}
578748446bbSMel Gorman 
579748446bbSMel Gorman 	}
580748446bbSMel Gorman 
581*f9e35b3bSMel Gorman out:
582748446bbSMel Gorman 	/* Release free pages and check accounting */
583748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
584748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
585748446bbSMel Gorman 
586748446bbSMel Gorman 	return ret;
587748446bbSMel Gorman }
58876ab0f53SMel Gorman 
5893e7d3449SMel Gorman unsigned long compact_zone_order(struct zone *zone,
59077f1fe6bSMel Gorman 				 int order, gfp_t gfp_mask,
591d527caf2SAndrea Arcangeli 				 bool sync)
59256de7263SMel Gorman {
59356de7263SMel Gorman 	struct compact_control cc = {
59456de7263SMel Gorman 		.nr_freepages = 0,
59556de7263SMel Gorman 		.nr_migratepages = 0,
59656de7263SMel Gorman 		.order = order,
59756de7263SMel Gorman 		.migratetype = allocflags_to_migratetype(gfp_mask),
59856de7263SMel Gorman 		.zone = zone,
59977f1fe6bSMel Gorman 		.sync = sync,
60056de7263SMel Gorman 	};
60156de7263SMel Gorman 	INIT_LIST_HEAD(&cc.freepages);
60256de7263SMel Gorman 	INIT_LIST_HEAD(&cc.migratepages);
60356de7263SMel Gorman 
60456de7263SMel Gorman 	return compact_zone(zone, &cc);
60556de7263SMel Gorman }
60656de7263SMel Gorman 
6075e771905SMel Gorman int sysctl_extfrag_threshold = 500;
6085e771905SMel Gorman 
60956de7263SMel Gorman /**
61056de7263SMel Gorman  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
61156de7263SMel Gorman  * @zonelist: The zonelist used for the current allocation
61256de7263SMel Gorman  * @order: The order of the current allocation
61356de7263SMel Gorman  * @gfp_mask: The GFP mask of the current allocation
61456de7263SMel Gorman  * @nodemask: The allowed nodes to allocate from
61577f1fe6bSMel Gorman  * @sync: Whether migration is synchronous or not
61656de7263SMel Gorman  *
61756de7263SMel Gorman  * This is the main entry point for direct page compaction.
61856de7263SMel Gorman  */
61956de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist,
62077f1fe6bSMel Gorman 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
62177f1fe6bSMel Gorman 			bool sync)
62256de7263SMel Gorman {
62356de7263SMel Gorman 	enum zone_type high_zoneidx = gfp_zone(gfp_mask);
62456de7263SMel Gorman 	int may_enter_fs = gfp_mask & __GFP_FS;
62556de7263SMel Gorman 	int may_perform_io = gfp_mask & __GFP_IO;
62656de7263SMel Gorman 	struct zoneref *z;
62756de7263SMel Gorman 	struct zone *zone;
62856de7263SMel Gorman 	int rc = COMPACT_SKIPPED;
62956de7263SMel Gorman 
63056de7263SMel Gorman 	/*
63156de7263SMel Gorman 	 * Check whether it is worth even starting compaction. The order check is
63256de7263SMel Gorman 	 * made because an assumption is made that the page allocator can satisfy
63356de7263SMel Gorman 	 * the "cheaper" orders without taking special steps
63456de7263SMel Gorman 	 */
635c5a73c3dSAndrea Arcangeli 	if (!order || !may_enter_fs || !may_perform_io)
63656de7263SMel Gorman 		return rc;
63756de7263SMel Gorman 
63856de7263SMel Gorman 	count_vm_event(COMPACTSTALL);
63956de7263SMel Gorman 
64056de7263SMel Gorman 	/* Compact each zone in the list */
64156de7263SMel Gorman 	for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
64256de7263SMel Gorman 								nodemask) {
64356de7263SMel Gorman 		int status;
64456de7263SMel Gorman 
645d527caf2SAndrea Arcangeli 		status = compact_zone_order(zone, order, gfp_mask, sync);
64656de7263SMel Gorman 		rc = max(status, rc);
64756de7263SMel Gorman 
6483e7d3449SMel Gorman 		/* If a normal allocation would succeed, stop compacting */
6493e7d3449SMel Gorman 		if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
65056de7263SMel Gorman 			break;
65156de7263SMel Gorman 	}
65256de7263SMel Gorman 
65356de7263SMel Gorman 	return rc;
65456de7263SMel Gorman }
65556de7263SMel Gorman 
65656de7263SMel Gorman 
65776ab0f53SMel Gorman /* Compact all zones within a node */
65876ab0f53SMel Gorman static int compact_node(int nid)
65976ab0f53SMel Gorman {
66076ab0f53SMel Gorman 	int zoneid;
66176ab0f53SMel Gorman 	pg_data_t *pgdat;
66276ab0f53SMel Gorman 	struct zone *zone;
66376ab0f53SMel Gorman 
66476ab0f53SMel Gorman 	if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
66576ab0f53SMel Gorman 		return -EINVAL;
66676ab0f53SMel Gorman 	pgdat = NODE_DATA(nid);
66776ab0f53SMel Gorman 
66876ab0f53SMel Gorman 	/* Flush pending updates to the LRU lists */
66976ab0f53SMel Gorman 	lru_add_drain_all();
67076ab0f53SMel Gorman 
67176ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
67276ab0f53SMel Gorman 		struct compact_control cc = {
67376ab0f53SMel Gorman 			.nr_freepages = 0,
67476ab0f53SMel Gorman 			.nr_migratepages = 0,
67556de7263SMel Gorman 			.order = -1,
67676ab0f53SMel Gorman 		};
67776ab0f53SMel Gorman 
67876ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
67976ab0f53SMel Gorman 		if (!populated_zone(zone))
68076ab0f53SMel Gorman 			continue;
68176ab0f53SMel Gorman 
68276ab0f53SMel Gorman 		cc.zone = zone;
68376ab0f53SMel Gorman 		INIT_LIST_HEAD(&cc.freepages);
68476ab0f53SMel Gorman 		INIT_LIST_HEAD(&cc.migratepages);
68576ab0f53SMel Gorman 
68676ab0f53SMel Gorman 		compact_zone(zone, &cc);
68776ab0f53SMel Gorman 
68876ab0f53SMel Gorman 		VM_BUG_ON(!list_empty(&cc.freepages));
68976ab0f53SMel Gorman 		VM_BUG_ON(!list_empty(&cc.migratepages));
69076ab0f53SMel Gorman 	}
69176ab0f53SMel Gorman 
69276ab0f53SMel Gorman 	return 0;
69376ab0f53SMel Gorman }
69476ab0f53SMel Gorman 
69576ab0f53SMel Gorman /* Compact all nodes in the system */
69676ab0f53SMel Gorman static int compact_nodes(void)
69776ab0f53SMel Gorman {
69876ab0f53SMel Gorman 	int nid;
69976ab0f53SMel Gorman 
70076ab0f53SMel Gorman 	for_each_online_node(nid)
70176ab0f53SMel Gorman 		compact_node(nid);
70276ab0f53SMel Gorman 
70376ab0f53SMel Gorman 	return COMPACT_COMPLETE;
70476ab0f53SMel Gorman }
70576ab0f53SMel Gorman 
70676ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
70776ab0f53SMel Gorman int sysctl_compact_memory;
70876ab0f53SMel Gorman 
70976ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
71076ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
71176ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
71276ab0f53SMel Gorman {
71376ab0f53SMel Gorman 	if (write)
71476ab0f53SMel Gorman 		return compact_nodes();
71576ab0f53SMel Gorman 
71676ab0f53SMel Gorman 	return 0;
71776ab0f53SMel Gorman }
718ed4a6d7fSMel Gorman 
7195e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write,
7205e771905SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
7215e771905SMel Gorman {
7225e771905SMel Gorman 	proc_dointvec_minmax(table, write, buffer, length, ppos);
7235e771905SMel Gorman 
7245e771905SMel Gorman 	return 0;
7255e771905SMel Gorman }
7265e771905SMel Gorman 
727ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
728ed4a6d7fSMel Gorman ssize_t sysfs_compact_node(struct sys_device *dev,
729ed4a6d7fSMel Gorman 			struct sysdev_attribute *attr,
730ed4a6d7fSMel Gorman 			const char *buf, size_t count)
731ed4a6d7fSMel Gorman {
732ed4a6d7fSMel Gorman 	compact_node(dev->id);
733ed4a6d7fSMel Gorman 
734ed4a6d7fSMel Gorman 	return count;
735ed4a6d7fSMel Gorman }
736ed4a6d7fSMel Gorman static SYSDEV_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
737ed4a6d7fSMel Gorman 
738ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
739ed4a6d7fSMel Gorman {
740ed4a6d7fSMel Gorman 	return sysdev_create_file(&node->sysdev, &attr_compact);
741ed4a6d7fSMel Gorman }
742ed4a6d7fSMel Gorman 
743ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
744ed4a6d7fSMel Gorman {
745ed4a6d7fSMel Gorman 	return sysdev_remove_file(&node->sysdev, &attr_compact);
746ed4a6d7fSMel Gorman }
747ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
748