xref: /openbmc/linux/mm/compaction.c (revision ed4a6d7f0676db50b5023cc01f6cda82a2f2a307)
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>
16*ed4a6d7fSMel Gorman #include <linux/sysfs.h>
17748446bbSMel Gorman #include "internal.h"
18748446bbSMel Gorman 
19748446bbSMel Gorman /*
20748446bbSMel Gorman  * compact_control is used to track pages being migrated and the free pages
21748446bbSMel Gorman  * they are being migrated to during memory compaction. The free_pfn starts
22748446bbSMel Gorman  * at the end of a zone and migrate_pfn begins at the start. Movable pages
23748446bbSMel Gorman  * are moved to the end of a zone during a compaction run and the run
24748446bbSMel Gorman  * completes when free_pfn <= migrate_pfn
25748446bbSMel Gorman  */
26748446bbSMel Gorman struct compact_control {
27748446bbSMel Gorman 	struct list_head freepages;	/* List of free pages to migrate to */
28748446bbSMel Gorman 	struct list_head migratepages;	/* List of pages being migrated */
29748446bbSMel Gorman 	unsigned long nr_freepages;	/* Number of isolated free pages */
30748446bbSMel Gorman 	unsigned long nr_migratepages;	/* Number of pages to migrate */
31748446bbSMel Gorman 	unsigned long free_pfn;		/* isolate_freepages search base */
32748446bbSMel Gorman 	unsigned long migrate_pfn;	/* isolate_migratepages search base */
33748446bbSMel Gorman 
34748446bbSMel Gorman 	/* Account for isolated anon and file pages */
35748446bbSMel Gorman 	unsigned long nr_anon;
36748446bbSMel Gorman 	unsigned long nr_file;
37748446bbSMel Gorman 
38748446bbSMel Gorman 	struct zone *zone;
39748446bbSMel Gorman };
40748446bbSMel 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 
55748446bbSMel Gorman /* Isolate free pages onto a private freelist. Must hold zone->lock */
56748446bbSMel Gorman static unsigned long isolate_freepages_block(struct zone *zone,
57748446bbSMel Gorman 				unsigned long blockpfn,
58748446bbSMel Gorman 				struct list_head *freelist)
59748446bbSMel Gorman {
60748446bbSMel Gorman 	unsigned long zone_end_pfn, end_pfn;
61748446bbSMel Gorman 	int total_isolated = 0;
62748446bbSMel Gorman 	struct page *cursor;
63748446bbSMel Gorman 
64748446bbSMel Gorman 	/* Get the last PFN we should scan for free pages at */
65748446bbSMel Gorman 	zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
66748446bbSMel Gorman 	end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
67748446bbSMel Gorman 
68748446bbSMel Gorman 	/* Find the first usable PFN in the block to initialse page cursor */
69748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++) {
70748446bbSMel Gorman 		if (pfn_valid_within(blockpfn))
71748446bbSMel Gorman 			break;
72748446bbSMel Gorman 	}
73748446bbSMel Gorman 	cursor = pfn_to_page(blockpfn);
74748446bbSMel Gorman 
75748446bbSMel Gorman 	/* Isolate free pages. This assumes the block is valid */
76748446bbSMel Gorman 	for (; blockpfn < end_pfn; blockpfn++, cursor++) {
77748446bbSMel Gorman 		int isolated, i;
78748446bbSMel Gorman 		struct page *page = cursor;
79748446bbSMel Gorman 
80748446bbSMel Gorman 		if (!pfn_valid_within(blockpfn))
81748446bbSMel Gorman 			continue;
82748446bbSMel Gorman 
83748446bbSMel Gorman 		if (!PageBuddy(page))
84748446bbSMel Gorman 			continue;
85748446bbSMel Gorman 
86748446bbSMel Gorman 		/* Found a free page, break it into order-0 pages */
87748446bbSMel Gorman 		isolated = split_free_page(page);
88748446bbSMel Gorman 		total_isolated += isolated;
89748446bbSMel Gorman 		for (i = 0; i < isolated; i++) {
90748446bbSMel Gorman 			list_add(&page->lru, freelist);
91748446bbSMel Gorman 			page++;
92748446bbSMel Gorman 		}
93748446bbSMel Gorman 
94748446bbSMel Gorman 		/* If a page was split, advance to the end of it */
95748446bbSMel Gorman 		if (isolated) {
96748446bbSMel Gorman 			blockpfn += isolated - 1;
97748446bbSMel Gorman 			cursor += isolated - 1;
98748446bbSMel Gorman 		}
99748446bbSMel Gorman 	}
100748446bbSMel Gorman 
101748446bbSMel Gorman 	return total_isolated;
102748446bbSMel Gorman }
103748446bbSMel Gorman 
104748446bbSMel Gorman /* Returns true if the page is within a block suitable for migration to */
105748446bbSMel Gorman static bool suitable_migration_target(struct page *page)
106748446bbSMel Gorman {
107748446bbSMel Gorman 
108748446bbSMel Gorman 	int migratetype = get_pageblock_migratetype(page);
109748446bbSMel Gorman 
110748446bbSMel Gorman 	/* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
111748446bbSMel Gorman 	if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
112748446bbSMel Gorman 		return false;
113748446bbSMel Gorman 
114748446bbSMel Gorman 	/* If the page is a large free page, then allow migration */
115748446bbSMel Gorman 	if (PageBuddy(page) && page_order(page) >= pageblock_order)
116748446bbSMel Gorman 		return true;
117748446bbSMel Gorman 
118748446bbSMel Gorman 	/* If the block is MIGRATE_MOVABLE, allow migration */
119748446bbSMel Gorman 	if (migratetype == MIGRATE_MOVABLE)
120748446bbSMel Gorman 		return true;
121748446bbSMel Gorman 
122748446bbSMel Gorman 	/* Otherwise skip the block */
123748446bbSMel Gorman 	return false;
124748446bbSMel Gorman }
125748446bbSMel Gorman 
126748446bbSMel Gorman /*
127748446bbSMel Gorman  * Based on information in the current compact_control, find blocks
128748446bbSMel Gorman  * suitable for isolating free pages from and then isolate them.
129748446bbSMel Gorman  */
130748446bbSMel Gorman static void isolate_freepages(struct zone *zone,
131748446bbSMel Gorman 				struct compact_control *cc)
132748446bbSMel Gorman {
133748446bbSMel Gorman 	struct page *page;
134748446bbSMel Gorman 	unsigned long high_pfn, low_pfn, pfn;
135748446bbSMel Gorman 	unsigned long flags;
136748446bbSMel Gorman 	int nr_freepages = cc->nr_freepages;
137748446bbSMel Gorman 	struct list_head *freelist = &cc->freepages;
138748446bbSMel Gorman 
139748446bbSMel Gorman 	pfn = cc->free_pfn;
140748446bbSMel Gorman 	low_pfn = cc->migrate_pfn + pageblock_nr_pages;
141748446bbSMel Gorman 	high_pfn = low_pfn;
142748446bbSMel Gorman 
143748446bbSMel Gorman 	/*
144748446bbSMel Gorman 	 * Isolate free pages until enough are available to migrate the
145748446bbSMel Gorman 	 * pages on cc->migratepages. We stop searching if the migrate
146748446bbSMel Gorman 	 * and free page scanners meet or enough free pages are isolated.
147748446bbSMel Gorman 	 */
148748446bbSMel Gorman 	spin_lock_irqsave(&zone->lock, flags);
149748446bbSMel Gorman 	for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
150748446bbSMel Gorman 					pfn -= pageblock_nr_pages) {
151748446bbSMel Gorman 		unsigned long isolated;
152748446bbSMel Gorman 
153748446bbSMel Gorman 		if (!pfn_valid(pfn))
154748446bbSMel Gorman 			continue;
155748446bbSMel Gorman 
156748446bbSMel Gorman 		/*
157748446bbSMel Gorman 		 * Check for overlapping nodes/zones. It's possible on some
158748446bbSMel Gorman 		 * configurations to have a setup like
159748446bbSMel Gorman 		 * node0 node1 node0
160748446bbSMel Gorman 		 * i.e. it's possible that all pages within a zones range of
161748446bbSMel Gorman 		 * pages do not belong to a single zone.
162748446bbSMel Gorman 		 */
163748446bbSMel Gorman 		page = pfn_to_page(pfn);
164748446bbSMel Gorman 		if (page_zone(page) != zone)
165748446bbSMel Gorman 			continue;
166748446bbSMel Gorman 
167748446bbSMel Gorman 		/* Check the block is suitable for migration */
168748446bbSMel Gorman 		if (!suitable_migration_target(page))
169748446bbSMel Gorman 			continue;
170748446bbSMel Gorman 
171748446bbSMel Gorman 		/* Found a block suitable for isolating free pages from */
172748446bbSMel Gorman 		isolated = isolate_freepages_block(zone, pfn, freelist);
173748446bbSMel Gorman 		nr_freepages += isolated;
174748446bbSMel Gorman 
175748446bbSMel Gorman 		/*
176748446bbSMel Gorman 		 * Record the highest PFN we isolated pages from. When next
177748446bbSMel Gorman 		 * looking for free pages, the search will restart here as
178748446bbSMel Gorman 		 * page migration may have returned some pages to the allocator
179748446bbSMel Gorman 		 */
180748446bbSMel Gorman 		if (isolated)
181748446bbSMel Gorman 			high_pfn = max(high_pfn, pfn);
182748446bbSMel Gorman 	}
183748446bbSMel Gorman 	spin_unlock_irqrestore(&zone->lock, flags);
184748446bbSMel Gorman 
185748446bbSMel Gorman 	/* split_free_page does not map the pages */
186748446bbSMel Gorman 	list_for_each_entry(page, freelist, lru) {
187748446bbSMel Gorman 		arch_alloc_page(page, 0);
188748446bbSMel Gorman 		kernel_map_pages(page, 1, 1);
189748446bbSMel Gorman 	}
190748446bbSMel Gorman 
191748446bbSMel Gorman 	cc->free_pfn = high_pfn;
192748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
193748446bbSMel Gorman }
194748446bbSMel Gorman 
195748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */
196748446bbSMel Gorman static void acct_isolated(struct zone *zone, struct compact_control *cc)
197748446bbSMel Gorman {
198748446bbSMel Gorman 	struct page *page;
199748446bbSMel Gorman 	unsigned int count[NR_LRU_LISTS] = { 0, };
200748446bbSMel Gorman 
201748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru) {
202748446bbSMel Gorman 		int lru = page_lru_base_type(page);
203748446bbSMel Gorman 		count[lru]++;
204748446bbSMel Gorman 	}
205748446bbSMel Gorman 
206748446bbSMel Gorman 	cc->nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON];
207748446bbSMel Gorman 	cc->nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE];
208748446bbSMel Gorman 	__mod_zone_page_state(zone, NR_ISOLATED_ANON, cc->nr_anon);
209748446bbSMel Gorman 	__mod_zone_page_state(zone, NR_ISOLATED_FILE, cc->nr_file);
210748446bbSMel Gorman }
211748446bbSMel Gorman 
212748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */
213748446bbSMel Gorman static bool too_many_isolated(struct zone *zone)
214748446bbSMel Gorman {
215748446bbSMel Gorman 
216748446bbSMel Gorman 	unsigned long inactive, isolated;
217748446bbSMel Gorman 
218748446bbSMel Gorman 	inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
219748446bbSMel Gorman 					zone_page_state(zone, NR_INACTIVE_ANON);
220748446bbSMel Gorman 	isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
221748446bbSMel Gorman 					zone_page_state(zone, NR_ISOLATED_ANON);
222748446bbSMel Gorman 
223748446bbSMel Gorman 	return isolated > inactive;
224748446bbSMel Gorman }
225748446bbSMel Gorman 
226748446bbSMel Gorman /*
227748446bbSMel Gorman  * Isolate all pages that can be migrated from the block pointed to by
228748446bbSMel Gorman  * the migrate scanner within compact_control.
229748446bbSMel Gorman  */
230748446bbSMel Gorman static unsigned long isolate_migratepages(struct zone *zone,
231748446bbSMel Gorman 					struct compact_control *cc)
232748446bbSMel Gorman {
233748446bbSMel Gorman 	unsigned long low_pfn, end_pfn;
234748446bbSMel Gorman 	struct list_head *migratelist = &cc->migratepages;
235748446bbSMel Gorman 
236748446bbSMel Gorman 	/* Do not scan outside zone boundaries */
237748446bbSMel Gorman 	low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
238748446bbSMel Gorman 
239748446bbSMel Gorman 	/* Only scan within a pageblock boundary */
240748446bbSMel Gorman 	end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
241748446bbSMel Gorman 
242748446bbSMel Gorman 	/* Do not cross the free scanner or scan within a memory hole */
243748446bbSMel Gorman 	if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
244748446bbSMel Gorman 		cc->migrate_pfn = end_pfn;
245748446bbSMel Gorman 		return 0;
246748446bbSMel Gorman 	}
247748446bbSMel Gorman 
248748446bbSMel Gorman 	/*
249748446bbSMel Gorman 	 * Ensure that there are not too many pages isolated from the LRU
250748446bbSMel Gorman 	 * list by either parallel reclaimers or compaction. If there are,
251748446bbSMel Gorman 	 * delay for some time until fewer pages are isolated
252748446bbSMel Gorman 	 */
253748446bbSMel Gorman 	while (unlikely(too_many_isolated(zone))) {
254748446bbSMel Gorman 		congestion_wait(BLK_RW_ASYNC, HZ/10);
255748446bbSMel Gorman 
256748446bbSMel Gorman 		if (fatal_signal_pending(current))
257748446bbSMel Gorman 			return 0;
258748446bbSMel Gorman 	}
259748446bbSMel Gorman 
260748446bbSMel Gorman 	/* Time to isolate some pages for migration */
261748446bbSMel Gorman 	spin_lock_irq(&zone->lru_lock);
262748446bbSMel Gorman 	for (; low_pfn < end_pfn; low_pfn++) {
263748446bbSMel Gorman 		struct page *page;
264748446bbSMel Gorman 		if (!pfn_valid_within(low_pfn))
265748446bbSMel Gorman 			continue;
266748446bbSMel Gorman 
267748446bbSMel Gorman 		/* Get the page and skip if free */
268748446bbSMel Gorman 		page = pfn_to_page(low_pfn);
269748446bbSMel Gorman 		if (PageBuddy(page))
270748446bbSMel Gorman 			continue;
271748446bbSMel Gorman 
272748446bbSMel Gorman 		/* Try isolate the page */
273748446bbSMel Gorman 		if (__isolate_lru_page(page, ISOLATE_BOTH, 0) != 0)
274748446bbSMel Gorman 			continue;
275748446bbSMel Gorman 
276748446bbSMel Gorman 		/* Successfully isolated */
277748446bbSMel Gorman 		del_page_from_lru_list(zone, page, page_lru(page));
278748446bbSMel Gorman 		list_add(&page->lru, migratelist);
279748446bbSMel Gorman 		mem_cgroup_del_lru(page);
280748446bbSMel Gorman 		cc->nr_migratepages++;
281748446bbSMel Gorman 
282748446bbSMel Gorman 		/* Avoid isolating too much */
283748446bbSMel Gorman 		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
284748446bbSMel Gorman 			break;
285748446bbSMel Gorman 	}
286748446bbSMel Gorman 
287748446bbSMel Gorman 	acct_isolated(zone, cc);
288748446bbSMel Gorman 
289748446bbSMel Gorman 	spin_unlock_irq(&zone->lru_lock);
290748446bbSMel Gorman 	cc->migrate_pfn = low_pfn;
291748446bbSMel Gorman 
292748446bbSMel Gorman 	return cc->nr_migratepages;
293748446bbSMel Gorman }
294748446bbSMel Gorman 
295748446bbSMel Gorman /*
296748446bbSMel Gorman  * This is a migrate-callback that "allocates" freepages by taking pages
297748446bbSMel Gorman  * from the isolated freelists in the block we are migrating to.
298748446bbSMel Gorman  */
299748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage,
300748446bbSMel Gorman 					unsigned long data,
301748446bbSMel Gorman 					int **result)
302748446bbSMel Gorman {
303748446bbSMel Gorman 	struct compact_control *cc = (struct compact_control *)data;
304748446bbSMel Gorman 	struct page *freepage;
305748446bbSMel Gorman 
306748446bbSMel Gorman 	/* Isolate free pages if necessary */
307748446bbSMel Gorman 	if (list_empty(&cc->freepages)) {
308748446bbSMel Gorman 		isolate_freepages(cc->zone, cc);
309748446bbSMel Gorman 
310748446bbSMel Gorman 		if (list_empty(&cc->freepages))
311748446bbSMel Gorman 			return NULL;
312748446bbSMel Gorman 	}
313748446bbSMel Gorman 
314748446bbSMel Gorman 	freepage = list_entry(cc->freepages.next, struct page, lru);
315748446bbSMel Gorman 	list_del(&freepage->lru);
316748446bbSMel Gorman 	cc->nr_freepages--;
317748446bbSMel Gorman 
318748446bbSMel Gorman 	return freepage;
319748446bbSMel Gorman }
320748446bbSMel Gorman 
321748446bbSMel Gorman /*
322748446bbSMel Gorman  * We cannot control nr_migratepages and nr_freepages fully when migration is
323748446bbSMel Gorman  * running as migrate_pages() has no knowledge of compact_control. When
324748446bbSMel Gorman  * migration is complete, we count the number of pages on the lists by hand.
325748446bbSMel Gorman  */
326748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc)
327748446bbSMel Gorman {
328748446bbSMel Gorman 	int nr_migratepages = 0;
329748446bbSMel Gorman 	int nr_freepages = 0;
330748446bbSMel Gorman 	struct page *page;
331748446bbSMel Gorman 
332748446bbSMel Gorman 	list_for_each_entry(page, &cc->migratepages, lru)
333748446bbSMel Gorman 		nr_migratepages++;
334748446bbSMel Gorman 	list_for_each_entry(page, &cc->freepages, lru)
335748446bbSMel Gorman 		nr_freepages++;
336748446bbSMel Gorman 
337748446bbSMel Gorman 	cc->nr_migratepages = nr_migratepages;
338748446bbSMel Gorman 	cc->nr_freepages = nr_freepages;
339748446bbSMel Gorman }
340748446bbSMel Gorman 
341748446bbSMel Gorman static int compact_finished(struct zone *zone,
342748446bbSMel Gorman 						struct compact_control *cc)
343748446bbSMel Gorman {
344748446bbSMel Gorman 	if (fatal_signal_pending(current))
345748446bbSMel Gorman 		return COMPACT_PARTIAL;
346748446bbSMel Gorman 
347748446bbSMel Gorman 	/* Compaction run completes if the migrate and free scanner meet */
348748446bbSMel Gorman 	if (cc->free_pfn <= cc->migrate_pfn)
349748446bbSMel Gorman 		return COMPACT_COMPLETE;
350748446bbSMel Gorman 
351748446bbSMel Gorman 	return COMPACT_CONTINUE;
352748446bbSMel Gorman }
353748446bbSMel Gorman 
354748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc)
355748446bbSMel Gorman {
356748446bbSMel Gorman 	int ret;
357748446bbSMel Gorman 
358748446bbSMel Gorman 	/* Setup to move all movable pages to the end of the zone */
359748446bbSMel Gorman 	cc->migrate_pfn = zone->zone_start_pfn;
360748446bbSMel Gorman 	cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
361748446bbSMel Gorman 	cc->free_pfn &= ~(pageblock_nr_pages-1);
362748446bbSMel Gorman 
363748446bbSMel Gorman 	migrate_prep_local();
364748446bbSMel Gorman 
365748446bbSMel Gorman 	while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
366748446bbSMel Gorman 		unsigned long nr_migrate, nr_remaining;
367748446bbSMel Gorman 
368748446bbSMel Gorman 		if (!isolate_migratepages(zone, cc))
369748446bbSMel Gorman 			continue;
370748446bbSMel Gorman 
371748446bbSMel Gorman 		nr_migrate = cc->nr_migratepages;
372748446bbSMel Gorman 		migrate_pages(&cc->migratepages, compaction_alloc,
373748446bbSMel Gorman 						(unsigned long)cc, 0);
374748446bbSMel Gorman 		update_nr_listpages(cc);
375748446bbSMel Gorman 		nr_remaining = cc->nr_migratepages;
376748446bbSMel Gorman 
377748446bbSMel Gorman 		count_vm_event(COMPACTBLOCKS);
378748446bbSMel Gorman 		count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
379748446bbSMel Gorman 		if (nr_remaining)
380748446bbSMel Gorman 			count_vm_events(COMPACTPAGEFAILED, nr_remaining);
381748446bbSMel Gorman 
382748446bbSMel Gorman 		/* Release LRU pages not migrated */
383748446bbSMel Gorman 		if (!list_empty(&cc->migratepages)) {
384748446bbSMel Gorman 			putback_lru_pages(&cc->migratepages);
385748446bbSMel Gorman 			cc->nr_migratepages = 0;
386748446bbSMel Gorman 		}
387748446bbSMel Gorman 
388748446bbSMel Gorman 	}
389748446bbSMel Gorman 
390748446bbSMel Gorman 	/* Release free pages and check accounting */
391748446bbSMel Gorman 	cc->nr_freepages -= release_freepages(&cc->freepages);
392748446bbSMel Gorman 	VM_BUG_ON(cc->nr_freepages != 0);
393748446bbSMel Gorman 
394748446bbSMel Gorman 	return ret;
395748446bbSMel Gorman }
39676ab0f53SMel Gorman 
39776ab0f53SMel Gorman /* Compact all zones within a node */
39876ab0f53SMel Gorman static int compact_node(int nid)
39976ab0f53SMel Gorman {
40076ab0f53SMel Gorman 	int zoneid;
40176ab0f53SMel Gorman 	pg_data_t *pgdat;
40276ab0f53SMel Gorman 	struct zone *zone;
40376ab0f53SMel Gorman 
40476ab0f53SMel Gorman 	if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
40576ab0f53SMel Gorman 		return -EINVAL;
40676ab0f53SMel Gorman 	pgdat = NODE_DATA(nid);
40776ab0f53SMel Gorman 
40876ab0f53SMel Gorman 	/* Flush pending updates to the LRU lists */
40976ab0f53SMel Gorman 	lru_add_drain_all();
41076ab0f53SMel Gorman 
41176ab0f53SMel Gorman 	for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
41276ab0f53SMel Gorman 		struct compact_control cc = {
41376ab0f53SMel Gorman 			.nr_freepages = 0,
41476ab0f53SMel Gorman 			.nr_migratepages = 0,
41576ab0f53SMel Gorman 		};
41676ab0f53SMel Gorman 
41776ab0f53SMel Gorman 		zone = &pgdat->node_zones[zoneid];
41876ab0f53SMel Gorman 		if (!populated_zone(zone))
41976ab0f53SMel Gorman 			continue;
42076ab0f53SMel Gorman 
42176ab0f53SMel Gorman 		cc.zone = zone;
42276ab0f53SMel Gorman 		INIT_LIST_HEAD(&cc.freepages);
42376ab0f53SMel Gorman 		INIT_LIST_HEAD(&cc.migratepages);
42476ab0f53SMel Gorman 
42576ab0f53SMel Gorman 		compact_zone(zone, &cc);
42676ab0f53SMel Gorman 
42776ab0f53SMel Gorman 		VM_BUG_ON(!list_empty(&cc.freepages));
42876ab0f53SMel Gorman 		VM_BUG_ON(!list_empty(&cc.migratepages));
42976ab0f53SMel Gorman 	}
43076ab0f53SMel Gorman 
43176ab0f53SMel Gorman 	return 0;
43276ab0f53SMel Gorman }
43376ab0f53SMel Gorman 
43476ab0f53SMel Gorman /* Compact all nodes in the system */
43576ab0f53SMel Gorman static int compact_nodes(void)
43676ab0f53SMel Gorman {
43776ab0f53SMel Gorman 	int nid;
43876ab0f53SMel Gorman 
43976ab0f53SMel Gorman 	for_each_online_node(nid)
44076ab0f53SMel Gorman 		compact_node(nid);
44176ab0f53SMel Gorman 
44276ab0f53SMel Gorman 	return COMPACT_COMPLETE;
44376ab0f53SMel Gorman }
44476ab0f53SMel Gorman 
44576ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */
44676ab0f53SMel Gorman int sysctl_compact_memory;
44776ab0f53SMel Gorman 
44876ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */
44976ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write,
45076ab0f53SMel Gorman 			void __user *buffer, size_t *length, loff_t *ppos)
45176ab0f53SMel Gorman {
45276ab0f53SMel Gorman 	if (write)
45376ab0f53SMel Gorman 		return compact_nodes();
45476ab0f53SMel Gorman 
45576ab0f53SMel Gorman 	return 0;
45676ab0f53SMel Gorman }
457*ed4a6d7fSMel Gorman 
458*ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
459*ed4a6d7fSMel Gorman ssize_t sysfs_compact_node(struct sys_device *dev,
460*ed4a6d7fSMel Gorman 			struct sysdev_attribute *attr,
461*ed4a6d7fSMel Gorman 			const char *buf, size_t count)
462*ed4a6d7fSMel Gorman {
463*ed4a6d7fSMel Gorman 	compact_node(dev->id);
464*ed4a6d7fSMel Gorman 
465*ed4a6d7fSMel Gorman 	return count;
466*ed4a6d7fSMel Gorman }
467*ed4a6d7fSMel Gorman static SYSDEV_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
468*ed4a6d7fSMel Gorman 
469*ed4a6d7fSMel Gorman int compaction_register_node(struct node *node)
470*ed4a6d7fSMel Gorman {
471*ed4a6d7fSMel Gorman 	return sysdev_create_file(&node->sysdev, &attr_compact);
472*ed4a6d7fSMel Gorman }
473*ed4a6d7fSMel Gorman 
474*ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node)
475*ed4a6d7fSMel Gorman {
476*ed4a6d7fSMel Gorman 	return sysdev_remove_file(&node->sysdev, &attr_compact);
477*ed4a6d7fSMel Gorman }
478*ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */
479