blob: 4b863211dfdd797c32006e2b0050f0eedc3430c9 [file] [log] [blame]
Mel Gorman748446b2010-05-24 14:32:27 -07001/*
2 * linux/mm/compaction.c
3 *
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
6 * lifting
7 *
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
9 */
10#include <linux/swap.h>
11#include <linux/migrate.h>
12#include <linux/compaction.h>
13#include <linux/mm_inline.h>
14#include <linux/backing-dev.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070015#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070016#include <linux/sysfs.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include "internal.h"
18
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010019#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
Mel Gormanb7aba692011-01-13 15:45:54 -080021#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
Mel Gorman748446b2010-05-24 14:32:27 -070024static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +010038static void map_pages(struct list_head *list)
39{
40 struct page *page;
41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
Michal Nazarewiczd4158d22011-12-29 13:09:50 +010048static inline bool migrate_async_suitable(int migratetype)
49{
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51}
52
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +010053/*
Mel Gorman0ba33882012-08-21 16:16:17 -070054 * Compaction requires the taking of some coarse locks that are potentially
55 * very heavily contended. Check if the process needs to be scheduled or
56 * if the lock is contended. For async compaction, back out in the event
57 * if contention is severe. For sync compaction, schedule.
58 *
59 * Returns true if the lock is held.
60 * Returns false if the lock is released and compaction should abort
61 */
62static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
63 bool locked, struct compact_control *cc)
64{
65 if (need_resched() || spin_is_contended(lock)) {
66 if (locked) {
67 spin_unlock_irqrestore(lock, *flags);
68 locked = false;
69 }
70
71 /* async aborts if taking too long or contended */
72 if (!cc->sync) {
Shaohua Li03dd8fe2012-10-08 16:32:27 -070073 cc->contended = true;
Mel Gorman0ba33882012-08-21 16:16:17 -070074 return false;
75 }
76
77 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -070078 }
79
80 if (!locked)
81 spin_lock_irqsave(lock, *flags);
82 return true;
83}
84
85static inline bool compact_trylock_irqsave(spinlock_t *lock,
86 unsigned long *flags, struct compact_control *cc)
87{
88 return compact_checklock_irqsave(lock, flags, false, cc);
89}
90
Mel Gormanc74068b2012-10-08 16:29:12 -070091static void compact_capture_page(struct compact_control *cc)
92{
93 unsigned long flags;
94 int mtype, mtype_low, mtype_high;
95
96 if (!cc->page || *cc->page)
97 return;
98
99 /*
100 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
101 * regardless of the migratetype of the freelist is is captured from.
102 * This is fine because the order for a high-order MIGRATE_MOVABLE
103 * allocation is typically at least a pageblock size and overall
104 * fragmentation is not impaired. Other allocation types must
105 * capture pages from their own migratelist because otherwise they
106 * could pollute other pageblocks like MIGRATE_MOVABLE with
107 * difficult to move pages and making fragmentation worse overall.
108 */
109 if (cc->migratetype == MIGRATE_MOVABLE) {
110 mtype_low = 0;
111 mtype_high = MIGRATE_PCPTYPES;
112 } else {
113 mtype_low = cc->migratetype;
114 mtype_high = cc->migratetype + 1;
115 }
116
117 /* Speculatively examine the free lists without zone lock */
118 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
119 int order;
120 for (order = cc->order; order < MAX_ORDER; order++) {
121 struct page *page;
122 struct free_area *area;
123 area = &(cc->zone->free_area[order]);
124 if (list_empty(&area->free_list[mtype]))
125 continue;
126
127 /* Take the lock and attempt capture of the page */
128 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
129 return;
130 if (!list_empty(&area->free_list[mtype])) {
131 page = list_entry(area->free_list[mtype].next,
132 struct page, lru);
133 if (capture_free_page(page, cc->order, mtype)) {
134 spin_unlock_irqrestore(&cc->zone->lock,
135 flags);
136 *cc->page = page;
137 return;
138 }
139 }
140 spin_unlock_irqrestore(&cc->zone->lock, flags);
141 }
142 }
143}
144
Mel Gorman0ba33882012-08-21 16:16:17 -0700145/*
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100146 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
147 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
148 * pages inside of the pageblock (even though it may still end up isolating
149 * some pages).
150 */
151static unsigned long isolate_freepages_block(unsigned long blockpfn,
152 unsigned long end_pfn,
153 struct list_head *freelist,
154 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700155{
Mel Gormanb7aba692011-01-13 15:45:54 -0800156 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700157 struct page *cursor;
158
Mel Gorman748446b2010-05-24 14:32:27 -0700159 cursor = pfn_to_page(blockpfn);
160
161 /* Isolate free pages. This assumes the block is valid */
162 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
163 int isolated, i;
164 struct page *page = cursor;
165
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100166 if (!pfn_valid_within(blockpfn)) {
167 if (strict)
168 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700169 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100170 }
Mel Gormanb7aba692011-01-13 15:45:54 -0800171 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700172
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100173 if (!PageBuddy(page)) {
174 if (strict)
175 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700176 continue;
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100177 }
Mel Gorman748446b2010-05-24 14:32:27 -0700178
179 /* Found a free page, break it into order-0 pages */
180 isolated = split_free_page(page);
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100181 if (!isolated && strict)
182 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700183 total_isolated += isolated;
184 for (i = 0; i < isolated; i++) {
185 list_add(&page->lru, freelist);
186 page++;
187 }
188
189 /* If a page was split, advance to the end of it */
190 if (isolated) {
191 blockpfn += isolated - 1;
192 cursor += isolated - 1;
193 }
194 }
195
Mel Gormanb7aba692011-01-13 15:45:54 -0800196 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700197 return total_isolated;
198}
199
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100200/**
201 * isolate_freepages_range() - isolate free pages.
202 * @start_pfn: The first PFN to start isolating.
203 * @end_pfn: The one-past-last PFN.
204 *
205 * Non-free pages, invalid PFNs, or zone boundaries within the
206 * [start_pfn, end_pfn) range are considered errors, cause function to
207 * undo its actions and return zero.
208 *
209 * Otherwise, function returns one-past-the-last PFN of isolated page
210 * (which may be greater then end_pfn if end fell in a middle of
211 * a free page).
212 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100213unsigned long
Michal Nazarewicz61ee2fc2012-01-30 13:24:03 +0100214isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
215{
216 unsigned long isolated, pfn, block_end_pfn, flags;
217 struct zone *zone = NULL;
218 LIST_HEAD(freelist);
219
220 if (pfn_valid(start_pfn))
221 zone = page_zone(pfn_to_page(start_pfn));
222
223 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
224 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
225 break;
226
227 /*
228 * On subsequent iterations ALIGN() is actually not needed,
229 * but we keep it that we not to complicate the code.
230 */
231 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
232 block_end_pfn = min(block_end_pfn, end_pfn);
233
234 spin_lock_irqsave(&zone->lock, flags);
235 isolated = isolate_freepages_block(pfn, block_end_pfn,
236 &freelist, true);
237 spin_unlock_irqrestore(&zone->lock, flags);
238
239 /*
240 * In strict mode, isolate_freepages_block() returns 0 if
241 * there are any holes in the block (ie. invalid PFNs or
242 * non-free pages).
243 */
244 if (!isolated)
245 break;
246
247 /*
248 * If we managed to isolate pages, it is always (1 << n) *
249 * pageblock_nr_pages for some non-negative n. (Max order
250 * page may span two pageblocks).
251 */
252 }
253
254 /* split_free_page does not map the pages */
255 map_pages(&freelist);
256
257 if (pfn < end_pfn) {
258 /* Loop terminated early, cleanup. */
259 release_freepages(&freelist);
260 return 0;
261 }
262
263 /* We don't use freelists for anything. */
264 return pfn;
265}
266
Mel Gorman748446b2010-05-24 14:32:27 -0700267/* Update the number of anon and file isolated pages in the zone */
Mel Gorman0ba33882012-08-21 16:16:17 -0700268static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700269{
270 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700271 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700272
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700273 list_for_each_entry(page, &cc->migratepages, lru)
274 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700275
Mel Gorman0ba33882012-08-21 16:16:17 -0700276 /* If locked we can use the interrupt unsafe versions */
277 if (locked) {
278 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
279 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
280 } else {
281 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
282 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
283 }
Mel Gorman748446b2010-05-24 14:32:27 -0700284}
285
286/* Similar to reclaim, but different enough that they don't share logic */
287static bool too_many_isolated(struct zone *zone)
288{
Minchan Kimbc693042010-09-09 16:38:00 -0700289 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700290
291 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
292 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700293 active = zone_page_state(zone, NR_ACTIVE_FILE) +
294 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700295 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
296 zone_page_state(zone, NR_ISOLATED_ANON);
297
Minchan Kimbc693042010-09-09 16:38:00 -0700298 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700299}
300
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100301/**
302 * isolate_migratepages_range() - isolate all migrate-able pages in range.
303 * @zone: Zone pages are in.
304 * @cc: Compaction control structure.
305 * @low_pfn: The first PFN of the range.
306 * @end_pfn: The one-past-the-last PFN of the range.
307 *
308 * Isolate all pages that can be migrated from the range specified by
309 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
310 * pending), otherwise PFN of the first page that was not scanned
311 * (which may be both less, equal to or more then end_pfn).
312 *
313 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
314 * zero.
315 *
316 * Apart from cc->migratepages and cc->nr_migratetypes this function
317 * does not modify any cc's fields, in particular it does not modify
318 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700319 */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100320unsigned long
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100321isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
322 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700323{
Mel Gorman9927af742011-01-13 15:45:59 -0800324 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800325 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700326 struct list_head *migratelist = &cc->migratepages;
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700327 isolate_mode_t mode = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700328 unsigned long flags;
329 bool locked;
Mel Gorman748446b2010-05-24 14:32:27 -0700330
Mel Gorman748446b2010-05-24 14:32:27 -0700331 /*
332 * Ensure that there are not too many pages isolated from the LRU
333 * list by either parallel reclaimers or compaction. If there are,
334 * delay for some time until fewer pages are isolated
335 */
336 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700337 /* async migration should just abort */
338 if (!cc->sync)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100339 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700340
Mel Gorman748446b2010-05-24 14:32:27 -0700341 congestion_wait(BLK_RW_ASYNC, HZ/10);
342
343 if (fatal_signal_pending(current))
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100344 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700345 }
346
347 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700348 cond_resched();
Mel Gorman0ba33882012-08-21 16:16:17 -0700349 spin_lock_irqsave(&zone->lru_lock, flags);
350 locked = true;
Mel Gorman748446b2010-05-24 14:32:27 -0700351 for (; low_pfn < end_pfn; low_pfn++) {
352 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700353
354 /* give a chance to irqs before checking need_resched() */
355 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
Mel Gorman0ba33882012-08-21 16:16:17 -0700356 spin_unlock_irqrestore(&zone->lru_lock, flags);
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700357 locked = false;
358 }
Mel Gorman0ba33882012-08-21 16:16:17 -0700359
360 /* Check if it is ok to still hold the lock */
361 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
362 locked, cc);
Mel Gorman4f647b32012-10-08 16:32:30 -0700363 if (!locked || fatal_signal_pending(current))
Mel Gorman0ba33882012-08-21 16:16:17 -0700364 break;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700365
Mel Gorman0bf380b2012-02-03 15:37:18 -0800366 /*
367 * migrate_pfn does not necessarily start aligned to a
368 * pageblock. Ensure that pfn_valid is called when moving
369 * into a new MAX_ORDER_NR_PAGES range in case of large
370 * memory holes within the zone
371 */
372 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
373 if (!pfn_valid(low_pfn)) {
374 low_pfn += MAX_ORDER_NR_PAGES - 1;
375 continue;
376 }
377 }
378
Mel Gorman748446b2010-05-24 14:32:27 -0700379 if (!pfn_valid_within(low_pfn))
380 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800381 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700382
Mel Gormandc908602012-02-08 17:13:38 -0800383 /*
384 * Get the page and ensure the page is within the same zone.
385 * See the comment in isolate_freepages about overlapping
386 * nodes. It is deliberate that the new zone lock is not taken
387 * as memory compaction should not move pages between nodes.
388 */
Mel Gorman748446b2010-05-24 14:32:27 -0700389 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800390 if (page_zone(page) != zone)
391 continue;
392
393 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700394 if (PageBuddy(page))
395 continue;
396
Mel Gorman9927af742011-01-13 15:45:59 -0800397 /*
398 * For async migration, also only scan in MOVABLE blocks. Async
399 * migration is optimistic to see if the minimum amount of work
400 * satisfies the allocation
401 */
402 pageblock_nr = low_pfn >> pageblock_order;
403 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100404 !migrate_async_suitable(get_pageblock_migratetype(page))) {
Mel Gorman9927af742011-01-13 15:45:59 -0800405 low_pfn += pageblock_nr_pages;
406 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
407 last_pageblock_nr = pageblock_nr;
408 continue;
409 }
410
Andrea Arcangelibc835012011-01-13 15:47:08 -0800411 if (!PageLRU(page))
412 continue;
413
414 /*
415 * PageLRU is set, and lru_lock excludes isolation,
416 * splitting and collapsing (collapsing has already
417 * happened if PageLRU is set).
418 */
419 if (PageTransHuge(page)) {
420 low_pfn += (1 << compound_order(page)) - 1;
421 continue;
422 }
423
Mel Gormanc8244932012-01-12 17:19:38 -0800424 if (!cc->sync)
425 mode |= ISOLATE_ASYNC_MIGRATE;
426
Mel Gorman748446b2010-05-24 14:32:27 -0700427 /* Try isolate the page */
Konstantin Khlebnikovfa168092012-05-29 15:06:54 -0700428 if (__isolate_lru_page(page, mode) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700429 continue;
430
Andrea Arcangelibc835012011-01-13 15:47:08 -0800431 VM_BUG_ON(PageTransCompound(page));
432
Mel Gorman748446b2010-05-24 14:32:27 -0700433 /* Successfully isolated */
434 del_page_from_lru_list(zone, page, page_lru(page));
435 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700436 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800437 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700438
439 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800440 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
441 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700442 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800443 }
Mel Gorman748446b2010-05-24 14:32:27 -0700444 }
445
Mel Gorman0ba33882012-08-21 16:16:17 -0700446 acct_isolated(zone, locked, cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700447
Mel Gorman0ba33882012-08-21 16:16:17 -0700448 if (locked)
449 spin_unlock_irqrestore(&zone->lru_lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700450
Mel Gormanb7aba692011-01-13 15:45:54 -0800451 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
452
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100453 return low_pfn;
454}
455
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100456#endif /* CONFIG_COMPACTION || CONFIG_CMA */
457#ifdef CONFIG_COMPACTION
458
459/* Returns true if the page is within a block suitable for migration to */
460static bool suitable_migration_target(struct page *page)
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100461{
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100462
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100463 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100464
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100465 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
466 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
467 return false;
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100468
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100469 /* If the page is a large free page, then allow migration */
470 if (PageBuddy(page) && page_order(page) >= pageblock_order)
471 return true;
472
Michal Nazarewiczd4158d22011-12-29 13:09:50 +0100473 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
474 if (migrate_async_suitable(migratetype))
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100475 return true;
476
477 /* Otherwise skip the block */
478 return false;
479}
480
481/*
482 * Based on information in the current compact_control, find blocks
483 * suitable for isolating free pages from and then isolate them.
484 */
485static void isolate_freepages(struct zone *zone,
486 struct compact_control *cc)
487{
488 struct page *page;
489 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
490 unsigned long flags;
491 int nr_freepages = cc->nr_freepages;
492 struct list_head *freelist = &cc->freepages;
493
494 /*
495 * Initialise the free scanner. The starting point is where we last
496 * scanned from (or the end of the zone if starting). The low point
497 * is the end of the pageblock the migration scanner is using.
498 */
499 pfn = cc->free_pfn;
500 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
501
502 /*
503 * Take care that if the migration scanner is at the end of the zone
504 * that the free scanner does not accidentally move to the next zone
505 * in the next isolation cycle.
506 */
507 high_pfn = min(low_pfn, pfn);
508
509 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
510
511 /*
512 * Isolate free pages until enough are available to migrate the
513 * pages on cc->migratepages. We stop searching if the migrate
514 * and free page scanners meet or enough free pages are isolated.
515 */
516 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
517 pfn -= pageblock_nr_pages) {
518 unsigned long isolated;
519
520 if (!pfn_valid(pfn))
521 continue;
522
523 /*
524 * Check for overlapping nodes/zones. It's possible on some
525 * configurations to have a setup like
526 * node0 node1 node0
527 * i.e. it's possible that all pages within a zones range of
528 * pages do not belong to a single zone.
529 */
530 page = pfn_to_page(pfn);
531 if (page_zone(page) != zone)
532 continue;
533
534 /* Check the block is suitable for migration */
535 if (!suitable_migration_target(page))
536 continue;
537
538 /*
539 * Found a block suitable for isolating free pages from. Now
540 * we disabled interrupts, double check things are ok and
541 * isolate the pages. This is to minimise the time IRQs
542 * are disabled
543 */
544 isolated = 0;
Mel Gorman0ba33882012-08-21 16:16:17 -0700545
546 /*
547 * The zone lock must be held to isolate freepages. This
548 * unfortunately this is a very coarse lock and can be
549 * heavily contended if there are parallel allocations
550 * or parallel compactions. For async compaction do not
551 * spin on the lock
552 */
553 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
554 break;
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100555 if (suitable_migration_target(page)) {
556 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
557 isolated = isolate_freepages_block(pfn, end_pfn,
558 freelist, false);
559 nr_freepages += isolated;
560 }
561 spin_unlock_irqrestore(&zone->lock, flags);
562
563 /*
564 * Record the highest PFN we isolated pages from. When next
565 * looking for free pages, the search will restart here as
566 * page migration may have returned some pages to the allocator
567 */
568 if (isolated)
569 high_pfn = max(high_pfn, pfn);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100570 }
571
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100572 /* split_free_page does not map the pages */
573 map_pages(freelist);
Michal Nazarewicza196a6c2012-01-30 13:16:26 +0100574
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100575 cc->free_pfn = high_pfn;
576 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700577}
578
579/*
580 * This is a migrate-callback that "allocates" freepages by taking pages
581 * from the isolated freelists in the block we are migrating to.
582 */
583static struct page *compaction_alloc(struct page *migratepage,
584 unsigned long data,
585 int **result)
586{
587 struct compact_control *cc = (struct compact_control *)data;
588 struct page *freepage;
589
590 /* Isolate free pages if necessary */
591 if (list_empty(&cc->freepages)) {
592 isolate_freepages(cc->zone, cc);
593
594 if (list_empty(&cc->freepages))
595 return NULL;
596 }
597
598 freepage = list_entry(cc->freepages.next, struct page, lru);
599 list_del(&freepage->lru);
600 cc->nr_freepages--;
601
602 return freepage;
603}
604
605/*
606 * We cannot control nr_migratepages and nr_freepages fully when migration is
607 * running as migrate_pages() has no knowledge of compact_control. When
608 * migration is complete, we count the number of pages on the lists by hand.
609 */
610static void update_nr_listpages(struct compact_control *cc)
611{
612 int nr_migratepages = 0;
613 int nr_freepages = 0;
614 struct page *page;
615
616 list_for_each_entry(page, &cc->migratepages, lru)
617 nr_migratepages++;
618 list_for_each_entry(page, &cc->freepages, lru)
619 nr_freepages++;
620
621 cc->nr_migratepages = nr_migratepages;
622 cc->nr_freepages = nr_freepages;
623}
624
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100625/* possible outcome of isolate_migratepages */
626typedef enum {
627 ISOLATE_ABORT, /* Abort compaction now */
628 ISOLATE_NONE, /* No pages isolated, continue scanning */
629 ISOLATE_SUCCESS, /* Pages isolated, migrate */
630} isolate_migrate_t;
631
632/*
633 * Isolate all pages that can be migrated from the block pointed to by
634 * the migrate scanner within compact_control.
635 */
636static isolate_migrate_t isolate_migratepages(struct zone *zone,
637 struct compact_control *cc)
638{
639 unsigned long low_pfn, end_pfn;
640
641 /* Do not scan outside zone boundaries */
642 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
643
644 /* Only scan within a pageblock boundary */
645 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
646
647 /* Do not cross the free scanner or scan within a memory hole */
648 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
649 cc->migrate_pfn = end_pfn;
650 return ISOLATE_NONE;
651 }
652
653 /* Perform the isolation */
654 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700655 if (!low_pfn || cc->contended)
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +0100656 return ISOLATE_ABORT;
657
658 cc->migrate_pfn = low_pfn;
659
660 return ISOLATE_SUCCESS;
661}
662
Mel Gorman748446b2010-05-24 14:32:27 -0700663static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800664 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700665{
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800666 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700667
Mel Gorman748446b2010-05-24 14:32:27 -0700668 if (fatal_signal_pending(current))
669 return COMPACT_PARTIAL;
670
671 /* Compaction run completes if the migrate and free scanner meet */
672 if (cc->free_pfn <= cc->migrate_pfn)
673 return COMPACT_COMPLETE;
674
Johannes Weiner82478fb2011-01-20 14:44:21 -0800675 /*
676 * order == -1 is expected when compacting via
677 * /proc/sys/vm/compact_memory
678 */
Mel Gorman56de7262010-05-24 14:32:30 -0700679 if (cc->order == -1)
680 return COMPACT_CONTINUE;
681
Michal Hocko3957c772011-06-15 15:08:25 -0700682 /* Compaction run is not finished if the watermark is not met */
683 watermark = low_wmark_pages(zone);
684 watermark += (1 << cc->order);
685
686 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
687 return COMPACT_CONTINUE;
688
Mel Gorman56de7262010-05-24 14:32:30 -0700689 /* Direct compactor: Is a suitable page free? */
Mel Gormanc74068b2012-10-08 16:29:12 -0700690 if (cc->page) {
691 /* Was a suitable page captured? */
692 if (*cc->page)
Mel Gorman56de7262010-05-24 14:32:30 -0700693 return COMPACT_PARTIAL;
Mel Gormanc74068b2012-10-08 16:29:12 -0700694 } else {
695 unsigned int order;
696 for (order = cc->order; order < MAX_ORDER; order++) {
697 struct free_area *area = &zone->free_area[cc->order];
698 /* Job done if page is free of the right migratetype */
699 if (!list_empty(&area->free_list[cc->migratetype]))
700 return COMPACT_PARTIAL;
Mel Gorman56de7262010-05-24 14:32:30 -0700701
Mel Gormanc74068b2012-10-08 16:29:12 -0700702 /* Job done if allocation would set block type */
703 if (cc->order >= pageblock_order && area->nr_free)
704 return COMPACT_PARTIAL;
705 }
Mel Gorman56de7262010-05-24 14:32:30 -0700706 }
707
Mel Gorman748446b2010-05-24 14:32:27 -0700708 return COMPACT_CONTINUE;
709}
710
Mel Gorman3e7d3442011-01-13 15:45:56 -0800711/*
712 * compaction_suitable: Is this suitable to run compaction on this zone now?
713 * Returns
714 * COMPACT_SKIPPED - If there are too few free pages for compaction
715 * COMPACT_PARTIAL - If the allocation would succeed without compaction
716 * COMPACT_CONTINUE - If compaction should run now
717 */
718unsigned long compaction_suitable(struct zone *zone, int order)
719{
720 int fragindex;
721 unsigned long watermark;
722
723 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700724 * order == -1 is expected when compacting via
725 * /proc/sys/vm/compact_memory
726 */
727 if (order == -1)
728 return COMPACT_CONTINUE;
729
730 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800731 * Watermarks for order-0 must be met for compaction. Note the 2UL.
732 * This is because during migration, copies of pages need to be
733 * allocated and for a short time, the footprint is higher
734 */
735 watermark = low_wmark_pages(zone) + (2UL << order);
736 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
737 return COMPACT_SKIPPED;
738
739 /*
740 * fragmentation index determines if allocation failures are due to
741 * low memory or external fragmentation
742 *
Shaohua Lia582a732011-06-15 15:08:49 -0700743 * index of -1000 implies allocations might succeed depending on
744 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800745 * index towards 0 implies failure is due to lack of memory
746 * index towards 1000 implies failure is due to fragmentation
747 *
748 * Only compact if a failure would be due to fragmentation.
749 */
750 fragindex = fragmentation_index(zone, order);
751 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
752 return COMPACT_SKIPPED;
753
Shaohua Lia582a732011-06-15 15:08:49 -0700754 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
755 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800756 return COMPACT_PARTIAL;
757
758 return COMPACT_CONTINUE;
759}
760
Mel Gorman748446b2010-05-24 14:32:27 -0700761static int compact_zone(struct zone *zone, struct compact_control *cc)
762{
763 int ret;
764
Mel Gorman3e7d3442011-01-13 15:45:56 -0800765 ret = compaction_suitable(zone, cc->order);
766 switch (ret) {
767 case COMPACT_PARTIAL:
768 case COMPACT_SKIPPED:
769 /* Compaction is likely to fail */
770 return ret;
771 case COMPACT_CONTINUE:
772 /* Fall through to compaction */
773 ;
774 }
775
Mel Gorman748446b2010-05-24 14:32:27 -0700776 /* Setup to move all movable pages to the end of the zone */
777 cc->migrate_pfn = zone->zone_start_pfn;
778 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
779 cc->free_pfn &= ~(pageblock_nr_pages-1);
780
781 migrate_prep_local();
782
783 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
784 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700785 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700786
Mel Gormanf9e35b32011-06-15 15:08:52 -0700787 switch (isolate_migratepages(zone, cc)) {
788 case ISOLATE_ABORT:
789 ret = COMPACT_PARTIAL;
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700790 putback_lru_pages(&cc->migratepages);
791 cc->nr_migratepages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700792 goto out;
793 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700794 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700795 case ISOLATE_SUCCESS:
796 ;
797 }
Mel Gorman748446b2010-05-24 14:32:27 -0700798
799 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700800 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800801 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800802 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700803 update_nr_listpages(cc);
804 nr_remaining = cc->nr_migratepages;
805
806 count_vm_event(COMPACTBLOCKS);
807 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
808 if (nr_remaining)
809 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800810 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
811 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700812
813 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700814 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700815 putback_lru_pages(&cc->migratepages);
816 cc->nr_migratepages = 0;
David Rientjes7a08b442012-07-11 14:02:13 -0700817 if (err == -ENOMEM) {
818 ret = COMPACT_PARTIAL;
819 goto out;
820 }
Mel Gorman748446b2010-05-24 14:32:27 -0700821 }
Mel Gormanc74068b2012-10-08 16:29:12 -0700822
823 /* Capture a page now if it is a suitable size */
824 compact_capture_page(cc);
Mel Gorman748446b2010-05-24 14:32:27 -0700825 }
826
Mel Gormanf9e35b32011-06-15 15:08:52 -0700827out:
Mel Gorman748446b2010-05-24 14:32:27 -0700828 /* Release free pages and check accounting */
829 cc->nr_freepages -= release_freepages(&cc->freepages);
830 VM_BUG_ON(cc->nr_freepages != 0);
831
832 return ret;
833}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700834
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700835static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800836 int order, gfp_t gfp_mask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700837 bool sync, bool *contended,
838 struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700839{
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700840 unsigned long ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700841 struct compact_control cc = {
842 .nr_freepages = 0,
843 .nr_migratepages = 0,
844 .order = order,
845 .migratetype = allocflags_to_migratetype(gfp_mask),
846 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800847 .sync = sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700848 .page = page,
Mel Gorman56de7262010-05-24 14:32:30 -0700849 };
850 INIT_LIST_HEAD(&cc.freepages);
851 INIT_LIST_HEAD(&cc.migratepages);
852
Shaohua Li03dd8fe2012-10-08 16:32:27 -0700853 ret = compact_zone(zone, &cc);
854
855 VM_BUG_ON(!list_empty(&cc.freepages));
856 VM_BUG_ON(!list_empty(&cc.migratepages));
857
858 *contended = cc.contended;
859 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -0700860}
861
Mel Gorman5e771902010-05-24 14:32:31 -0700862int sysctl_extfrag_threshold = 500;
863
Mel Gorman56de7262010-05-24 14:32:30 -0700864/**
865 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
866 * @zonelist: The zonelist used for the current allocation
867 * @order: The order of the current allocation
868 * @gfp_mask: The GFP mask of the current allocation
869 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800870 * @sync: Whether migration is synchronous or not
Mel Gorman8a063192012-10-08 16:32:31 -0700871 * @contended: Return value that is true if compaction was aborted due to lock contention
872 * @page: Optionally capture a free page of the requested order during compaction
Mel Gorman56de7262010-05-24 14:32:30 -0700873 *
874 * This is the main entry point for direct page compaction.
875 */
876unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800877 int order, gfp_t gfp_mask, nodemask_t *nodemask,
Mel Gormanc74068b2012-10-08 16:29:12 -0700878 bool sync, bool *contended, struct page **page)
Mel Gorman56de7262010-05-24 14:32:30 -0700879{
880 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
881 int may_enter_fs = gfp_mask & __GFP_FS;
882 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700883 struct zoneref *z;
884 struct zone *zone;
885 int rc = COMPACT_SKIPPED;
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700886 int alloc_flags = 0;
Mel Gorman56de7262010-05-24 14:32:30 -0700887
Mel Gormanbc337a92012-10-08 16:29:09 -0700888 /* Check if the GFP flags allow compaction */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800889 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700890 return rc;
891
892 count_vm_event(COMPACTSTALL);
893
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700894#ifdef CONFIG_CMA
895 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
896 alloc_flags |= ALLOC_CMA;
897#endif
Mel Gorman56de7262010-05-24 14:32:30 -0700898 /* Compact each zone in the list */
899 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
900 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700901 int status;
902
Mel Gorman0ba33882012-08-21 16:16:17 -0700903 status = compact_zone_order(zone, order, gfp_mask, sync,
Mel Gormanc74068b2012-10-08 16:29:12 -0700904 contended, page);
Mel Gorman56de7262010-05-24 14:32:30 -0700905 rc = max(status, rc);
906
Mel Gorman3e7d3442011-01-13 15:45:56 -0800907 /* If a normal allocation would succeed, stop compacting */
Bartlomiej Zolnierkiewicz850bf192012-10-08 16:32:05 -0700908 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
909 alloc_flags))
Mel Gorman56de7262010-05-24 14:32:30 -0700910 break;
911 }
912
913 return rc;
914}
915
916
Mel Gorman76ab0f52010-05-24 14:32:28 -0700917/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700918static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700919{
920 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700921 struct zone *zone;
922
Mel Gorman76ab0f52010-05-24 14:32:28 -0700923 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700924
925 zone = &pgdat->node_zones[zoneid];
926 if (!populated_zone(zone))
927 continue;
928
Rik van Riel7be62de2012-03-21 16:33:52 -0700929 cc->nr_freepages = 0;
930 cc->nr_migratepages = 0;
931 cc->zone = zone;
932 INIT_LIST_HEAD(&cc->freepages);
933 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700934
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700935 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700936 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700937
Rik van Rielaff62242012-03-21 16:33:52 -0700938 if (cc->order > 0) {
939 int ok = zone_watermark_ok(zone, cc->order,
940 low_wmark_pages(zone), 0, 0);
Minchan Kim57bb21c2012-08-21 16:16:03 -0700941 if (ok && cc->order >= zone->compact_order_failed)
Rik van Rielaff62242012-03-21 16:33:52 -0700942 zone->compact_order_failed = cc->order + 1;
943 /* Currently async compaction is never deferred. */
944 else if (!ok && cc->sync)
945 defer_compaction(zone, cc->order);
946 }
947
Rik van Riel7be62de2012-03-21 16:33:52 -0700948 VM_BUG_ON(!list_empty(&cc->freepages));
949 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700950 }
951
952 return 0;
953}
954
Rik van Riel7be62de2012-03-21 16:33:52 -0700955int compact_pgdat(pg_data_t *pgdat, int order)
956{
957 struct compact_control cc = {
958 .order = order,
959 .sync = false,
Mel Gormanc74068b2012-10-08 16:29:12 -0700960 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -0700961 };
962
963 return __compact_pgdat(pgdat, &cc);
964}
965
966static int compact_node(int nid)
967{
Rik van Riel7be62de2012-03-21 16:33:52 -0700968 struct compact_control cc = {
969 .order = -1,
970 .sync = true,
Mel Gormanc74068b2012-10-08 16:29:12 -0700971 .page = NULL,
Rik van Riel7be62de2012-03-21 16:33:52 -0700972 };
973
Hugh Dickins8575ec22012-03-21 16:33:53 -0700974 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700975}
976
Mel Gorman76ab0f52010-05-24 14:32:28 -0700977/* Compact all nodes in the system */
Jason Liuc0b96522013-01-11 14:31:47 -0800978static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700979{
980 int nid;
981
Hugh Dickins8575ec22012-03-21 16:33:53 -0700982 /* Flush pending updates to the LRU lists */
983 lru_add_drain_all();
984
Mel Gorman76ab0f52010-05-24 14:32:28 -0700985 for_each_online_node(nid)
986 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700987}
988
989/* The written value is actually unused, all memory is compacted */
990int sysctl_compact_memory;
991
992/* This is the entry point for compacting all nodes via /proc/sys/vm */
993int sysctl_compaction_handler(struct ctl_table *table, int write,
994 void __user *buffer, size_t *length, loff_t *ppos)
995{
996 if (write)
Jason Liuc0b96522013-01-11 14:31:47 -0800997 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -0700998
999 return 0;
1000}
Mel Gormaned4a6d72010-05-24 14:32:29 -07001001
Mel Gorman5e771902010-05-24 14:32:31 -07001002int sysctl_extfrag_handler(struct ctl_table *table, int write,
1003 void __user *buffer, size_t *length, loff_t *ppos)
1004{
1005 proc_dointvec_minmax(table, write, buffer, length, ppos);
1006
1007 return 0;
1008}
1009
Mel Gormaned4a6d72010-05-24 14:32:29 -07001010#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -08001011ssize_t sysfs_compact_node(struct device *dev,
1012 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -07001013 const char *buf, size_t count)
1014{
Hugh Dickins8575ec22012-03-21 16:33:53 -07001015 int nid = dev->id;
1016
1017 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1018 /* Flush pending updates to the LRU lists */
1019 lru_add_drain_all();
1020
1021 compact_node(nid);
1022 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07001023
1024 return count;
1025}
Kay Sievers10fbcf42011-12-21 14:48:43 -08001026static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001027
1028int compaction_register_node(struct node *node)
1029{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001030 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001031}
1032
1033void compaction_unregister_node(struct node *node)
1034{
Kay Sievers10fbcf42011-12-21 14:48:43 -08001035 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07001036}
1037#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewicz02ff1de2011-12-29 13:09:50 +01001038
1039#endif /* CONFIG_COMPACTION */