blob: 7a92e418a1878c10dfd6a24e5ca41fd4c5b7092c [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 Nazarewiczff9543f2011-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 Nazarewiczff9543f2011-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 Nazarewicz85aa1252012-01-30 13:24:03 +010048/*
49 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
50 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
51 * pages inside of the pageblock (even though it may still end up isolating
52 * some pages).
53 */
54static unsigned long isolate_freepages_block(unsigned long blockpfn,
55 unsigned long end_pfn,
56 struct list_head *freelist,
57 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -070058{
Mel Gormanb7aba692011-01-13 15:45:54 -080059 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070060 struct page *cursor;
61
Mel Gorman748446b2010-05-24 14:32:27 -070062 cursor = pfn_to_page(blockpfn);
63
64 /* Isolate free pages. This assumes the block is valid */
65 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
66 int isolated, i;
67 struct page *page = cursor;
68
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010069 if (!pfn_valid_within(blockpfn)) {
70 if (strict)
71 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070072 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010073 }
Mel Gormanb7aba692011-01-13 15:45:54 -080074 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -070075
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010076 if (!PageBuddy(page)) {
77 if (strict)
78 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070079 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010080 }
Mel Gorman748446b2010-05-24 14:32:27 -070081
82 /* Found a free page, break it into order-0 pages */
83 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010084 if (!isolated && strict)
85 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070086 total_isolated += isolated;
87 for (i = 0; i < isolated; i++) {
88 list_add(&page->lru, freelist);
89 page++;
90 }
91
92 /* If a page was split, advance to the end of it */
93 if (isolated) {
94 blockpfn += isolated - 1;
95 cursor += isolated - 1;
96 }
97 }
98
Mel Gormanb7aba692011-01-13 15:45:54 -080099 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700100 return total_isolated;
101}
102
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100103/**
104 * isolate_freepages_range() - isolate free pages.
105 * @start_pfn: The first PFN to start isolating.
106 * @end_pfn: The one-past-last PFN.
107 *
108 * Non-free pages, invalid PFNs, or zone boundaries within the
109 * [start_pfn, end_pfn) range are considered errors, cause function to
110 * undo its actions and return zero.
111 *
112 * Otherwise, function returns one-past-the-last PFN of isolated page
113 * (which may be greater then end_pfn if end fell in a middle of
114 * a free page).
115 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100116unsigned long
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100117isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
118{
119 unsigned long isolated, pfn, block_end_pfn, flags;
120 struct zone *zone = NULL;
121 LIST_HEAD(freelist);
122
123 if (pfn_valid(start_pfn))
124 zone = page_zone(pfn_to_page(start_pfn));
125
126 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
127 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
128 break;
129
130 /*
131 * On subsequent iterations ALIGN() is actually not needed,
132 * but we keep it that we not to complicate the code.
133 */
134 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
135 block_end_pfn = min(block_end_pfn, end_pfn);
136
137 spin_lock_irqsave(&zone->lock, flags);
138 isolated = isolate_freepages_block(pfn, block_end_pfn,
139 &freelist, true);
140 spin_unlock_irqrestore(&zone->lock, flags);
141
142 /*
143 * In strict mode, isolate_freepages_block() returns 0 if
144 * there are any holes in the block (ie. invalid PFNs or
145 * non-free pages).
146 */
147 if (!isolated)
148 break;
149
150 /*
151 * If we managed to isolate pages, it is always (1 << n) *
152 * pageblock_nr_pages for some non-negative n. (Max order
153 * page may span two pageblocks).
154 */
155 }
156
157 /* split_free_page does not map the pages */
158 map_pages(&freelist);
159
160 if (pfn < end_pfn) {
161 /* Loop terminated early, cleanup. */
162 release_freepages(&freelist);
163 return 0;
164 }
165
166 /* We don't use freelists for anything. */
167 return pfn;
168}
169
Mel Gorman748446b2010-05-24 14:32:27 -0700170/* Update the number of anon and file isolated pages in the zone */
171static void acct_isolated(struct zone *zone, struct compact_control *cc)
172{
173 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700174 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700175
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700176 list_for_each_entry(page, &cc->migratepages, lru)
177 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700178
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700179 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
180 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700181}
182
183/* Similar to reclaim, but different enough that they don't share logic */
184static bool too_many_isolated(struct zone *zone)
185{
Minchan Kimbc693042010-09-09 16:38:00 -0700186 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700187
188 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
189 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700190 active = zone_page_state(zone, NR_ACTIVE_FILE) +
191 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700192 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
193 zone_page_state(zone, NR_ISOLATED_ANON);
194
Minchan Kimbc693042010-09-09 16:38:00 -0700195 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700196}
197
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100198/**
199 * isolate_migratepages_range() - isolate all migrate-able pages in range.
200 * @zone: Zone pages are in.
201 * @cc: Compaction control structure.
202 * @low_pfn: The first PFN of the range.
203 * @end_pfn: The one-past-the-last PFN of the range.
204 *
205 * Isolate all pages that can be migrated from the range specified by
206 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
207 * pending), otherwise PFN of the first page that was not scanned
208 * (which may be both less, equal to or more then end_pfn).
209 *
210 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
211 * zero.
212 *
213 * Apart from cc->migratepages and cc->nr_migratetypes this function
214 * does not modify any cc's fields, in particular it does not modify
215 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700216 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100217unsigned long
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100218isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
219 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700220{
Mel Gorman9927af742011-01-13 15:45:59 -0800221 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800222 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700223 struct list_head *migratelist = &cc->migratepages;
Minchan Kim39deaf82011-10-31 17:06:51 -0700224 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
Mel Gorman748446b2010-05-24 14:32:27 -0700225
Mel Gorman748446b2010-05-24 14:32:27 -0700226 /*
227 * Ensure that there are not too many pages isolated from the LRU
228 * list by either parallel reclaimers or compaction. If there are,
229 * delay for some time until fewer pages are isolated
230 */
231 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700232 /* async migration should just abort */
233 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100234 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700235
Mel Gorman748446b2010-05-24 14:32:27 -0700236 congestion_wait(BLK_RW_ASYNC, HZ/10);
237
238 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100239 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700240 }
241
242 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700243 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700244 spin_lock_irq(&zone->lru_lock);
245 for (; low_pfn < end_pfn; low_pfn++) {
246 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700247 bool locked = true;
248
249 /* give a chance to irqs before checking need_resched() */
250 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
251 spin_unlock_irq(&zone->lru_lock);
252 locked = false;
253 }
254 if (need_resched() || spin_is_contended(&zone->lru_lock)) {
255 if (locked)
256 spin_unlock_irq(&zone->lru_lock);
257 cond_resched();
258 spin_lock_irq(&zone->lru_lock);
259 if (fatal_signal_pending(current))
260 break;
261 } else if (!locked)
262 spin_lock_irq(&zone->lru_lock);
263
Mel Gorman0bf380b2012-02-03 15:37:18 -0800264 /*
265 * migrate_pfn does not necessarily start aligned to a
266 * pageblock. Ensure that pfn_valid is called when moving
267 * into a new MAX_ORDER_NR_PAGES range in case of large
268 * memory holes within the zone
269 */
270 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
271 if (!pfn_valid(low_pfn)) {
272 low_pfn += MAX_ORDER_NR_PAGES - 1;
273 continue;
274 }
275 }
276
Mel Gorman748446b2010-05-24 14:32:27 -0700277 if (!pfn_valid_within(low_pfn))
278 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800279 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700280
Mel Gormandc908602012-02-08 17:13:38 -0800281 /*
282 * Get the page and ensure the page is within the same zone.
283 * See the comment in isolate_freepages about overlapping
284 * nodes. It is deliberate that the new zone lock is not taken
285 * as memory compaction should not move pages between nodes.
286 */
Mel Gorman748446b2010-05-24 14:32:27 -0700287 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800288 if (page_zone(page) != zone)
289 continue;
290
291 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700292 if (PageBuddy(page))
293 continue;
294
Mel Gorman9927af742011-01-13 15:45:59 -0800295 /*
296 * For async migration, also only scan in MOVABLE blocks. Async
297 * migration is optimistic to see if the minimum amount of work
298 * satisfies the allocation
299 */
300 pageblock_nr = low_pfn >> pageblock_order;
301 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
302 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
303 low_pfn += pageblock_nr_pages;
304 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
305 last_pageblock_nr = pageblock_nr;
306 continue;
307 }
308
Andrea Arcangelibc835012011-01-13 15:47:08 -0800309 if (!PageLRU(page))
310 continue;
311
312 /*
313 * PageLRU is set, and lru_lock excludes isolation,
314 * splitting and collapsing (collapsing has already
315 * happened if PageLRU is set).
316 */
317 if (PageTransHuge(page)) {
318 low_pfn += (1 << compound_order(page)) - 1;
319 continue;
320 }
321
Mel Gormanc8244932012-01-12 17:19:38 -0800322 if (!cc->sync)
323 mode |= ISOLATE_ASYNC_MIGRATE;
324
Mel Gorman748446b2010-05-24 14:32:27 -0700325 /* Try isolate the page */
Minchan Kim39deaf82011-10-31 17:06:51 -0700326 if (__isolate_lru_page(page, mode, 0) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700327 continue;
328
Andrea Arcangelibc835012011-01-13 15:47:08 -0800329 VM_BUG_ON(PageTransCompound(page));
330
Mel Gorman748446b2010-05-24 14:32:27 -0700331 /* Successfully isolated */
332 del_page_from_lru_list(zone, page, page_lru(page));
333 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700334 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800335 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700336
337 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800338 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
339 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700340 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800341 }
Mel Gorman748446b2010-05-24 14:32:27 -0700342 }
343
344 acct_isolated(zone, cc);
345
346 spin_unlock_irq(&zone->lru_lock);
Mel Gorman748446b2010-05-24 14:32:27 -0700347
Mel Gormanb7aba692011-01-13 15:45:54 -0800348 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
349
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100350 return low_pfn;
351}
352
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100353#endif /* CONFIG_COMPACTION || CONFIG_CMA */
354#ifdef CONFIG_COMPACTION
355
356/* Returns true if the page is within a block suitable for migration to */
357static bool suitable_migration_target(struct page *page)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100358{
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100359
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100360 int migratetype = get_pageblock_migratetype(page);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100361
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100362 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
363 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
364 return false;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100365
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100366 /* If the page is a large free page, then allow migration */
367 if (PageBuddy(page) && page_order(page) >= pageblock_order)
368 return true;
369
370 /* If the block is MIGRATE_MOVABLE, allow migration */
371 if (migratetype == MIGRATE_MOVABLE)
372 return true;
373
374 /* Otherwise skip the block */
375 return false;
376}
377
378/*
379 * Based on information in the current compact_control, find blocks
380 * suitable for isolating free pages from and then isolate them.
381 */
382static void isolate_freepages(struct zone *zone,
383 struct compact_control *cc)
384{
385 struct page *page;
386 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
387 unsigned long flags;
388 int nr_freepages = cc->nr_freepages;
389 struct list_head *freelist = &cc->freepages;
390
391 /*
392 * Initialise the free scanner. The starting point is where we last
393 * scanned from (or the end of the zone if starting). The low point
394 * is the end of the pageblock the migration scanner is using.
395 */
396 pfn = cc->free_pfn;
397 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
398
399 /*
400 * Take care that if the migration scanner is at the end of the zone
401 * that the free scanner does not accidentally move to the next zone
402 * in the next isolation cycle.
403 */
404 high_pfn = min(low_pfn, pfn);
405
406 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
407
408 /*
409 * Isolate free pages until enough are available to migrate the
410 * pages on cc->migratepages. We stop searching if the migrate
411 * and free page scanners meet or enough free pages are isolated.
412 */
413 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
414 pfn -= pageblock_nr_pages) {
415 unsigned long isolated;
416
417 if (!pfn_valid(pfn))
418 continue;
419
420 /*
421 * Check for overlapping nodes/zones. It's possible on some
422 * configurations to have a setup like
423 * node0 node1 node0
424 * i.e. it's possible that all pages within a zones range of
425 * pages do not belong to a single zone.
426 */
427 page = pfn_to_page(pfn);
428 if (page_zone(page) != zone)
429 continue;
430
431 /* Check the block is suitable for migration */
432 if (!suitable_migration_target(page))
433 continue;
434
435 /*
436 * Found a block suitable for isolating free pages from. Now
437 * we disabled interrupts, double check things are ok and
438 * isolate the pages. This is to minimise the time IRQs
439 * are disabled
440 */
441 isolated = 0;
442 spin_lock_irqsave(&zone->lock, flags);
443 if (suitable_migration_target(page)) {
444 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
445 isolated = isolate_freepages_block(pfn, end_pfn,
446 freelist, false);
447 nr_freepages += isolated;
448 }
449 spin_unlock_irqrestore(&zone->lock, flags);
450
451 /*
452 * Record the highest PFN we isolated pages from. When next
453 * looking for free pages, the search will restart here as
454 * page migration may have returned some pages to the allocator
455 */
456 if (isolated)
457 high_pfn = max(high_pfn, pfn);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100458 }
459
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100460 /* split_free_page does not map the pages */
461 map_pages(freelist);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100462
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100463 cc->free_pfn = high_pfn;
464 cc->nr_freepages = nr_freepages;
Mel Gorman748446b2010-05-24 14:32:27 -0700465}
466
467/*
468 * This is a migrate-callback that "allocates" freepages by taking pages
469 * from the isolated freelists in the block we are migrating to.
470 */
471static struct page *compaction_alloc(struct page *migratepage,
472 unsigned long data,
473 int **result)
474{
475 struct compact_control *cc = (struct compact_control *)data;
476 struct page *freepage;
477
478 /* Isolate free pages if necessary */
479 if (list_empty(&cc->freepages)) {
480 isolate_freepages(cc->zone, cc);
481
482 if (list_empty(&cc->freepages))
483 return NULL;
484 }
485
486 freepage = list_entry(cc->freepages.next, struct page, lru);
487 list_del(&freepage->lru);
488 cc->nr_freepages--;
489
490 return freepage;
491}
492
493/*
494 * We cannot control nr_migratepages and nr_freepages fully when migration is
495 * running as migrate_pages() has no knowledge of compact_control. When
496 * migration is complete, we count the number of pages on the lists by hand.
497 */
498static void update_nr_listpages(struct compact_control *cc)
499{
500 int nr_migratepages = 0;
501 int nr_freepages = 0;
502 struct page *page;
503
504 list_for_each_entry(page, &cc->migratepages, lru)
505 nr_migratepages++;
506 list_for_each_entry(page, &cc->freepages, lru)
507 nr_freepages++;
508
509 cc->nr_migratepages = nr_migratepages;
510 cc->nr_freepages = nr_freepages;
511}
512
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100513/* possible outcome of isolate_migratepages */
514typedef enum {
515 ISOLATE_ABORT, /* Abort compaction now */
516 ISOLATE_NONE, /* No pages isolated, continue scanning */
517 ISOLATE_SUCCESS, /* Pages isolated, migrate */
518} isolate_migrate_t;
519
520/*
521 * Isolate all pages that can be migrated from the block pointed to by
522 * the migrate scanner within compact_control.
523 */
524static isolate_migrate_t isolate_migratepages(struct zone *zone,
525 struct compact_control *cc)
526{
527 unsigned long low_pfn, end_pfn;
528
529 /* Do not scan outside zone boundaries */
530 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
531
532 /* Only scan within a pageblock boundary */
533 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
534
535 /* Do not cross the free scanner or scan within a memory hole */
536 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
537 cc->migrate_pfn = end_pfn;
538 return ISOLATE_NONE;
539 }
540
541 /* Perform the isolation */
542 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
543 if (!low_pfn)
544 return ISOLATE_ABORT;
545
546 cc->migrate_pfn = low_pfn;
547
548 return ISOLATE_SUCCESS;
549}
550
Mel Gorman748446b2010-05-24 14:32:27 -0700551static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800552 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700553{
Mel Gorman56de7262010-05-24 14:32:30 -0700554 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800555 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700556
Mel Gorman748446b2010-05-24 14:32:27 -0700557 if (fatal_signal_pending(current))
558 return COMPACT_PARTIAL;
559
560 /* Compaction run completes if the migrate and free scanner meet */
561 if (cc->free_pfn <= cc->migrate_pfn)
562 return COMPACT_COMPLETE;
563
Johannes Weiner82478fb2011-01-20 14:44:21 -0800564 /*
565 * order == -1 is expected when compacting via
566 * /proc/sys/vm/compact_memory
567 */
Mel Gorman56de7262010-05-24 14:32:30 -0700568 if (cc->order == -1)
569 return COMPACT_CONTINUE;
570
Michal Hocko3957c772011-06-15 15:08:25 -0700571 /* Compaction run is not finished if the watermark is not met */
572 watermark = low_wmark_pages(zone);
573 watermark += (1 << cc->order);
574
575 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
576 return COMPACT_CONTINUE;
577
Mel Gorman56de7262010-05-24 14:32:30 -0700578 /* Direct compactor: Is a suitable page free? */
579 for (order = cc->order; order < MAX_ORDER; order++) {
580 /* Job done if page is free of the right migratetype */
581 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
582 return COMPACT_PARTIAL;
583
584 /* Job done if allocation would set block type */
585 if (order >= pageblock_order && zone->free_area[order].nr_free)
586 return COMPACT_PARTIAL;
587 }
588
Mel Gorman748446b2010-05-24 14:32:27 -0700589 return COMPACT_CONTINUE;
590}
591
Mel Gorman3e7d3442011-01-13 15:45:56 -0800592/*
593 * compaction_suitable: Is this suitable to run compaction on this zone now?
594 * Returns
595 * COMPACT_SKIPPED - If there are too few free pages for compaction
596 * COMPACT_PARTIAL - If the allocation would succeed without compaction
597 * COMPACT_CONTINUE - If compaction should run now
598 */
599unsigned long compaction_suitable(struct zone *zone, int order)
600{
601 int fragindex;
602 unsigned long watermark;
603
604 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700605 * order == -1 is expected when compacting via
606 * /proc/sys/vm/compact_memory
607 */
608 if (order == -1)
609 return COMPACT_CONTINUE;
610
611 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800612 * Watermarks for order-0 must be met for compaction. Note the 2UL.
613 * This is because during migration, copies of pages need to be
614 * allocated and for a short time, the footprint is higher
615 */
616 watermark = low_wmark_pages(zone) + (2UL << order);
617 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
618 return COMPACT_SKIPPED;
619
620 /*
621 * fragmentation index determines if allocation failures are due to
622 * low memory or external fragmentation
623 *
Shaohua Lia582a732011-06-15 15:08:49 -0700624 * index of -1000 implies allocations might succeed depending on
625 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800626 * index towards 0 implies failure is due to lack of memory
627 * index towards 1000 implies failure is due to fragmentation
628 *
629 * Only compact if a failure would be due to fragmentation.
630 */
631 fragindex = fragmentation_index(zone, order);
632 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
633 return COMPACT_SKIPPED;
634
Shaohua Lia582a732011-06-15 15:08:49 -0700635 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
636 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800637 return COMPACT_PARTIAL;
638
639 return COMPACT_CONTINUE;
640}
641
Mel Gorman748446b2010-05-24 14:32:27 -0700642static int compact_zone(struct zone *zone, struct compact_control *cc)
643{
644 int ret;
645
Mel Gorman3e7d3442011-01-13 15:45:56 -0800646 ret = compaction_suitable(zone, cc->order);
647 switch (ret) {
648 case COMPACT_PARTIAL:
649 case COMPACT_SKIPPED:
650 /* Compaction is likely to fail */
651 return ret;
652 case COMPACT_CONTINUE:
653 /* Fall through to compaction */
654 ;
655 }
656
Mel Gorman748446b2010-05-24 14:32:27 -0700657 /* Setup to move all movable pages to the end of the zone */
658 cc->migrate_pfn = zone->zone_start_pfn;
659 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
660 cc->free_pfn &= ~(pageblock_nr_pages-1);
661
662 migrate_prep_local();
663
664 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
665 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700666 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700667
Mel Gormanf9e35b32011-06-15 15:08:52 -0700668 switch (isolate_migratepages(zone, cc)) {
669 case ISOLATE_ABORT:
670 ret = COMPACT_PARTIAL;
671 goto out;
672 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700673 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700674 case ISOLATE_SUCCESS:
675 ;
676 }
Mel Gorman748446b2010-05-24 14:32:27 -0700677
678 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700679 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800680 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800681 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700682 update_nr_listpages(cc);
683 nr_remaining = cc->nr_migratepages;
684
685 count_vm_event(COMPACTBLOCKS);
686 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
687 if (nr_remaining)
688 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800689 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
690 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700691
692 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700693 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700694 putback_lru_pages(&cc->migratepages);
695 cc->nr_migratepages = 0;
696 }
697
698 }
699
Mel Gormanf9e35b32011-06-15 15:08:52 -0700700out:
Mel Gorman748446b2010-05-24 14:32:27 -0700701 /* Release free pages and check accounting */
702 cc->nr_freepages -= release_freepages(&cc->freepages);
703 VM_BUG_ON(cc->nr_freepages != 0);
704
705 return ret;
706}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700707
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700708static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800709 int order, gfp_t gfp_mask,
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700710 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700711{
712 struct compact_control cc = {
713 .nr_freepages = 0,
714 .nr_migratepages = 0,
715 .order = order,
716 .migratetype = allocflags_to_migratetype(gfp_mask),
717 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800718 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -0700719 };
720 INIT_LIST_HEAD(&cc.freepages);
721 INIT_LIST_HEAD(&cc.migratepages);
722
723 return compact_zone(zone, &cc);
724}
725
Mel Gorman5e771902010-05-24 14:32:31 -0700726int sysctl_extfrag_threshold = 500;
727
Mel Gorman56de7262010-05-24 14:32:30 -0700728/**
729 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
730 * @zonelist: The zonelist used for the current allocation
731 * @order: The order of the current allocation
732 * @gfp_mask: The GFP mask of the current allocation
733 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800734 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700735 *
736 * This is the main entry point for direct page compaction.
737 */
738unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800739 int order, gfp_t gfp_mask, nodemask_t *nodemask,
740 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700741{
742 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
743 int may_enter_fs = gfp_mask & __GFP_FS;
744 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700745 struct zoneref *z;
746 struct zone *zone;
747 int rc = COMPACT_SKIPPED;
748
749 /*
750 * Check whether it is worth even starting compaction. The order check is
751 * made because an assumption is made that the page allocator can satisfy
752 * the "cheaper" orders without taking special steps
753 */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800754 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700755 return rc;
756
757 count_vm_event(COMPACTSTALL);
758
759 /* Compact each zone in the list */
760 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
761 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700762 int status;
763
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700764 status = compact_zone_order(zone, order, gfp_mask, sync);
Mel Gorman56de7262010-05-24 14:32:30 -0700765 rc = max(status, rc);
766
Mel Gorman3e7d3442011-01-13 15:45:56 -0800767 /* If a normal allocation would succeed, stop compacting */
768 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
Mel Gorman56de7262010-05-24 14:32:30 -0700769 break;
770 }
771
772 return rc;
773}
774
775
Mel Gorman76ab0f52010-05-24 14:32:28 -0700776/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700777static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700778{
779 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700780 struct zone *zone;
781
Mel Gorman76ab0f52010-05-24 14:32:28 -0700782 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700783
784 zone = &pgdat->node_zones[zoneid];
785 if (!populated_zone(zone))
786 continue;
787
Rik van Riel7be62de2012-03-21 16:33:52 -0700788 cc->nr_freepages = 0;
789 cc->nr_migratepages = 0;
790 cc->zone = zone;
791 INIT_LIST_HEAD(&cc->freepages);
792 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700793
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700794 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700795 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700796
Rik van Rielaff62242012-03-21 16:33:52 -0700797 if (cc->order > 0) {
798 int ok = zone_watermark_ok(zone, cc->order,
799 low_wmark_pages(zone), 0, 0);
800 if (ok && cc->order > zone->compact_order_failed)
801 zone->compact_order_failed = cc->order + 1;
802 /* Currently async compaction is never deferred. */
803 else if (!ok && cc->sync)
804 defer_compaction(zone, cc->order);
805 }
806
Rik van Riel7be62de2012-03-21 16:33:52 -0700807 VM_BUG_ON(!list_empty(&cc->freepages));
808 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700809 }
810
811 return 0;
812}
813
Rik van Riel7be62de2012-03-21 16:33:52 -0700814int compact_pgdat(pg_data_t *pgdat, int order)
815{
816 struct compact_control cc = {
817 .order = order,
818 .sync = false,
819 };
820
821 return __compact_pgdat(pgdat, &cc);
822}
823
824static int compact_node(int nid)
825{
Rik van Riel7be62de2012-03-21 16:33:52 -0700826 struct compact_control cc = {
827 .order = -1,
828 .sync = true,
829 };
830
Hugh Dickins8575ec22012-03-21 16:33:53 -0700831 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700832}
833
Mel Gorman76ab0f52010-05-24 14:32:28 -0700834/* Compact all nodes in the system */
835static int compact_nodes(void)
836{
837 int nid;
838
Hugh Dickins8575ec22012-03-21 16:33:53 -0700839 /* Flush pending updates to the LRU lists */
840 lru_add_drain_all();
841
Mel Gorman76ab0f52010-05-24 14:32:28 -0700842 for_each_online_node(nid)
843 compact_node(nid);
844
845 return COMPACT_COMPLETE;
846}
847
848/* The written value is actually unused, all memory is compacted */
849int sysctl_compact_memory;
850
851/* This is the entry point for compacting all nodes via /proc/sys/vm */
852int sysctl_compaction_handler(struct ctl_table *table, int write,
853 void __user *buffer, size_t *length, loff_t *ppos)
854{
855 if (write)
856 return compact_nodes();
857
858 return 0;
859}
Mel Gormaned4a6d72010-05-24 14:32:29 -0700860
Mel Gorman5e771902010-05-24 14:32:31 -0700861int sysctl_extfrag_handler(struct ctl_table *table, int write,
862 void __user *buffer, size_t *length, loff_t *ppos)
863{
864 proc_dointvec_minmax(table, write, buffer, length, ppos);
865
866 return 0;
867}
868
Mel Gormaned4a6d72010-05-24 14:32:29 -0700869#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800870ssize_t sysfs_compact_node(struct device *dev,
871 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -0700872 const char *buf, size_t count)
873{
Hugh Dickins8575ec22012-03-21 16:33:53 -0700874 int nid = dev->id;
875
876 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
877 /* Flush pending updates to the LRU lists */
878 lru_add_drain_all();
879
880 compact_node(nid);
881 }
Mel Gormaned4a6d72010-05-24 14:32:29 -0700882
883 return count;
884}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800885static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700886
887int compaction_register_node(struct node *node)
888{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800889 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700890}
891
892void compaction_unregister_node(struct node *node)
893{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800894 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700895}
896#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100897
898#endif /* CONFIG_COMPACTION */