blob: 06b198fa9abe2e0f1452e8c996c549036afdb258 [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
Mel Gormanb7aba692011-01-13 15:45:54 -080019#define CREATE_TRACE_POINTS
20#include <trace/events/compaction.h>
21
Mel Gorman748446b2010-05-24 14:32:27 -070022/*
23 * compact_control is used to track pages being migrated and the free pages
24 * they are being migrated to during memory compaction. The free_pfn starts
25 * at the end of a zone and migrate_pfn begins at the start. Movable pages
26 * are moved to the end of a zone during a compaction run and the run
27 * completes when free_pfn <= migrate_pfn
28 */
29struct compact_control {
30 struct list_head freepages; /* List of free pages to migrate to */
31 struct list_head migratepages; /* List of pages being migrated */
32 unsigned long nr_freepages; /* Number of isolated free pages */
33 unsigned long nr_migratepages; /* Number of pages to migrate */
34 unsigned long free_pfn; /* isolate_freepages search base */
35 unsigned long migrate_pfn; /* isolate_migratepages search base */
Mel Gorman77f1fe62011-01-13 15:45:57 -080036 bool sync; /* Synchronous migration */
Mel Gorman748446b2010-05-24 14:32:27 -070037
Dan Carpenteraad6ec32012-03-21 16:33:54 -070038 int order; /* order a direct compactor needs */
Mel Gorman56de7262010-05-24 14:32:30 -070039 int migratetype; /* MOVABLE, RECLAIMABLE etc */
Mel Gorman748446b2010-05-24 14:32:27 -070040 struct zone *zone;
41};
42
43static unsigned long release_freepages(struct list_head *freelist)
44{
45 struct page *page, *next;
46 unsigned long count = 0;
47
48 list_for_each_entry_safe(page, next, freelist, lru) {
49 list_del(&page->lru);
50 __free_page(page);
51 count++;
52 }
53
54 return count;
55}
56
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010057/*
58 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
59 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
60 * pages inside of the pageblock (even though it may still end up isolating
61 * some pages).
62 */
63static unsigned long isolate_freepages_block(unsigned long blockpfn,
64 unsigned long end_pfn,
65 struct list_head *freelist,
66 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -070067{
Mel Gormanb7aba692011-01-13 15:45:54 -080068 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070069 struct page *cursor;
70
Mel Gorman748446b2010-05-24 14:32:27 -070071 cursor = pfn_to_page(blockpfn);
72
73 /* Isolate free pages. This assumes the block is valid */
74 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
75 int isolated, i;
76 struct page *page = cursor;
77
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010078 if (!pfn_valid_within(blockpfn)) {
79 if (strict)
80 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070081 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010082 }
Mel Gormanb7aba692011-01-13 15:45:54 -080083 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -070084
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010085 if (!PageBuddy(page)) {
86 if (strict)
87 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070088 continue;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010089 }
Mel Gorman748446b2010-05-24 14:32:27 -070090
91 /* Found a free page, break it into order-0 pages */
92 isolated = split_free_page(page);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +010093 if (!isolated && strict)
94 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -070095 total_isolated += isolated;
96 for (i = 0; i < isolated; i++) {
97 list_add(&page->lru, freelist);
98 page++;
99 }
100
101 /* If a page was split, advance to the end of it */
102 if (isolated) {
103 blockpfn += isolated - 1;
104 cursor += isolated - 1;
105 }
106 }
107
Mel Gormanb7aba692011-01-13 15:45:54 -0800108 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700109 return total_isolated;
110}
111
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100112/**
113 * isolate_freepages_range() - isolate free pages.
114 * @start_pfn: The first PFN to start isolating.
115 * @end_pfn: The one-past-last PFN.
116 *
117 * Non-free pages, invalid PFNs, or zone boundaries within the
118 * [start_pfn, end_pfn) range are considered errors, cause function to
119 * undo its actions and return zero.
120 *
121 * Otherwise, function returns one-past-the-last PFN of isolated page
122 * (which may be greater then end_pfn if end fell in a middle of
123 * a free page).
124 */
125static unsigned long
126isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
127{
128 unsigned long isolated, pfn, block_end_pfn, flags;
129 struct zone *zone = NULL;
130 LIST_HEAD(freelist);
131
132 if (pfn_valid(start_pfn))
133 zone = page_zone(pfn_to_page(start_pfn));
134
135 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
136 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
137 break;
138
139 /*
140 * On subsequent iterations ALIGN() is actually not needed,
141 * but we keep it that we not to complicate the code.
142 */
143 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
144 block_end_pfn = min(block_end_pfn, end_pfn);
145
146 spin_lock_irqsave(&zone->lock, flags);
147 isolated = isolate_freepages_block(pfn, block_end_pfn,
148 &freelist, true);
149 spin_unlock_irqrestore(&zone->lock, flags);
150
151 /*
152 * In strict mode, isolate_freepages_block() returns 0 if
153 * there are any holes in the block (ie. invalid PFNs or
154 * non-free pages).
155 */
156 if (!isolated)
157 break;
158
159 /*
160 * If we managed to isolate pages, it is always (1 << n) *
161 * pageblock_nr_pages for some non-negative n. (Max order
162 * page may span two pageblocks).
163 */
164 }
165
166 /* split_free_page does not map the pages */
167 map_pages(&freelist);
168
169 if (pfn < end_pfn) {
170 /* Loop terminated early, cleanup. */
171 release_freepages(&freelist);
172 return 0;
173 }
174
175 /* We don't use freelists for anything. */
176 return pfn;
177}
178
Mel Gorman748446b2010-05-24 14:32:27 -0700179/* Returns true if the page is within a block suitable for migration to */
180static bool suitable_migration_target(struct page *page)
181{
182
183 int migratetype = get_pageblock_migratetype(page);
184
185 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
186 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
187 return false;
188
189 /* If the page is a large free page, then allow migration */
190 if (PageBuddy(page) && page_order(page) >= pageblock_order)
191 return true;
192
193 /* If the block is MIGRATE_MOVABLE, allow migration */
194 if (migratetype == MIGRATE_MOVABLE)
195 return true;
196
197 /* Otherwise skip the block */
198 return false;
199}
200
Michal Nazarewicz03d44192012-01-30 13:23:47 +0100201static void map_pages(struct list_head *list)
202{
203 struct page *page;
204
205 list_for_each_entry(page, list, lru) {
206 arch_alloc_page(page, 0);
207 kernel_map_pages(page, 1, 1);
208 }
209}
210
Mel Gorman748446b2010-05-24 14:32:27 -0700211/*
212 * Based on information in the current compact_control, find blocks
213 * suitable for isolating free pages from and then isolate them.
214 */
215static void isolate_freepages(struct zone *zone,
216 struct compact_control *cc)
217{
218 struct page *page;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100219 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700220 unsigned long flags;
221 int nr_freepages = cc->nr_freepages;
222 struct list_head *freelist = &cc->freepages;
223
Mel Gorman7454f4b2011-06-15 15:08:50 -0700224 /*
225 * Initialise the free scanner. The starting point is where we last
226 * scanned from (or the end of the zone if starting). The low point
227 * is the end of the pageblock the migration scanner is using.
228 */
Mel Gorman748446b2010-05-24 14:32:27 -0700229 pfn = cc->free_pfn;
230 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
Mel Gorman7454f4b2011-06-15 15:08:50 -0700231
232 /*
233 * Take care that if the migration scanner is at the end of the zone
234 * that the free scanner does not accidentally move to the next zone
235 * in the next isolation cycle.
236 */
237 high_pfn = min(low_pfn, pfn);
Mel Gorman748446b2010-05-24 14:32:27 -0700238
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100239 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
240
Mel Gorman748446b2010-05-24 14:32:27 -0700241 /*
242 * Isolate free pages until enough are available to migrate the
243 * pages on cc->migratepages. We stop searching if the migrate
244 * and free page scanners meet or enough free pages are isolated.
245 */
Mel Gorman748446b2010-05-24 14:32:27 -0700246 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
247 pfn -= pageblock_nr_pages) {
248 unsigned long isolated;
249
250 if (!pfn_valid(pfn))
251 continue;
252
253 /*
254 * Check for overlapping nodes/zones. It's possible on some
255 * configurations to have a setup like
256 * node0 node1 node0
257 * i.e. it's possible that all pages within a zones range of
258 * pages do not belong to a single zone.
259 */
260 page = pfn_to_page(pfn);
261 if (page_zone(page) != zone)
262 continue;
263
264 /* Check the block is suitable for migration */
265 if (!suitable_migration_target(page))
266 continue;
267
Mel Gorman602605a2011-03-22 16:33:08 -0700268 /*
269 * Found a block suitable for isolating free pages from. Now
270 * we disabled interrupts, double check things are ok and
271 * isolate the pages. This is to minimise the time IRQs
272 * are disabled
273 */
274 isolated = 0;
275 spin_lock_irqsave(&zone->lock, flags);
276 if (suitable_migration_target(page)) {
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100277 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
278 isolated = isolate_freepages_block(pfn, end_pfn,
279 freelist, false);
Mel Gorman602605a2011-03-22 16:33:08 -0700280 nr_freepages += isolated;
281 }
282 spin_unlock_irqrestore(&zone->lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700283
284 /*
285 * Record the highest PFN we isolated pages from. When next
286 * looking for free pages, the search will restart here as
287 * page migration may have returned some pages to the allocator
288 */
289 if (isolated)
290 high_pfn = max(high_pfn, pfn);
291 }
Mel Gorman748446b2010-05-24 14:32:27 -0700292
293 /* split_free_page does not map the pages */
Michal Nazarewicz03d44192012-01-30 13:23:47 +0100294 map_pages(freelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700295
296 cc->free_pfn = high_pfn;
297 cc->nr_freepages = nr_freepages;
298}
299
300/* Update the number of anon and file isolated pages in the zone */
301static void acct_isolated(struct zone *zone, struct compact_control *cc)
302{
303 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700304 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700305
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700306 list_for_each_entry(page, &cc->migratepages, lru)
307 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700308
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700309 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
310 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700311}
312
313/* Similar to reclaim, but different enough that they don't share logic */
314static bool too_many_isolated(struct zone *zone)
315{
Minchan Kimbc693042010-09-09 16:38:00 -0700316 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700317
318 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
319 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700320 active = zone_page_state(zone, NR_ACTIVE_FILE) +
321 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700322 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
323 zone_page_state(zone, NR_ISOLATED_ANON);
324
Minchan Kimbc693042010-09-09 16:38:00 -0700325 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700326}
327
Mel Gormanf9e35b32011-06-15 15:08:52 -0700328/* possible outcome of isolate_migratepages */
329typedef enum {
330 ISOLATE_ABORT, /* Abort compaction now */
331 ISOLATE_NONE, /* No pages isolated, continue scanning */
332 ISOLATE_SUCCESS, /* Pages isolated, migrate */
333} isolate_migrate_t;
334
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100335/**
336 * isolate_migratepages_range() - isolate all migrate-able pages in range.
337 * @zone: Zone pages are in.
338 * @cc: Compaction control structure.
339 * @low_pfn: The first PFN of the range.
340 * @end_pfn: The one-past-the-last PFN of the range.
341 *
342 * Isolate all pages that can be migrated from the range specified by
343 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
344 * pending), otherwise PFN of the first page that was not scanned
345 * (which may be both less, equal to or more then end_pfn).
346 *
347 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
348 * zero.
349 *
350 * Apart from cc->migratepages and cc->nr_migratetypes this function
351 * does not modify any cc's fields, in particular it does not modify
352 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700353 */
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100354static unsigned long
355isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
356 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700357{
Mel Gorman9927af742011-01-13 15:45:59 -0800358 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800359 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700360 struct list_head *migratelist = &cc->migratepages;
Minchan Kim39deaf82011-10-31 17:06:51 -0700361 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
Mel Gorman748446b2010-05-24 14:32:27 -0700362
Mel Gorman748446b2010-05-24 14:32:27 -0700363 /*
364 * Ensure that there are not too many pages isolated from the LRU
365 * list by either parallel reclaimers or compaction. If there are,
366 * delay for some time until fewer pages are isolated
367 */
368 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700369 /* async migration should just abort */
370 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100371 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700372
Mel Gorman748446b2010-05-24 14:32:27 -0700373 congestion_wait(BLK_RW_ASYNC, HZ/10);
374
375 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100376 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700377 }
378
379 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700380 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700381 spin_lock_irq(&zone->lru_lock);
382 for (; low_pfn < end_pfn; low_pfn++) {
383 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700384 bool locked = true;
385
386 /* give a chance to irqs before checking need_resched() */
387 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
388 spin_unlock_irq(&zone->lru_lock);
389 locked = false;
390 }
391 if (need_resched() || spin_is_contended(&zone->lru_lock)) {
392 if (locked)
393 spin_unlock_irq(&zone->lru_lock);
394 cond_resched();
395 spin_lock_irq(&zone->lru_lock);
396 if (fatal_signal_pending(current))
397 break;
398 } else if (!locked)
399 spin_lock_irq(&zone->lru_lock);
400
Mel Gorman0bf380b2012-02-03 15:37:18 -0800401 /*
402 * migrate_pfn does not necessarily start aligned to a
403 * pageblock. Ensure that pfn_valid is called when moving
404 * into a new MAX_ORDER_NR_PAGES range in case of large
405 * memory holes within the zone
406 */
407 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
408 if (!pfn_valid(low_pfn)) {
409 low_pfn += MAX_ORDER_NR_PAGES - 1;
410 continue;
411 }
412 }
413
Mel Gorman748446b2010-05-24 14:32:27 -0700414 if (!pfn_valid_within(low_pfn))
415 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800416 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700417
Mel Gormandc908602012-02-08 17:13:38 -0800418 /*
419 * Get the page and ensure the page is within the same zone.
420 * See the comment in isolate_freepages about overlapping
421 * nodes. It is deliberate that the new zone lock is not taken
422 * as memory compaction should not move pages between nodes.
423 */
Mel Gorman748446b2010-05-24 14:32:27 -0700424 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800425 if (page_zone(page) != zone)
426 continue;
427
428 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700429 if (PageBuddy(page))
430 continue;
431
Mel Gorman9927af742011-01-13 15:45:59 -0800432 /*
433 * For async migration, also only scan in MOVABLE blocks. Async
434 * migration is optimistic to see if the minimum amount of work
435 * satisfies the allocation
436 */
437 pageblock_nr = low_pfn >> pageblock_order;
438 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
439 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
440 low_pfn += pageblock_nr_pages;
441 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
442 last_pageblock_nr = pageblock_nr;
443 continue;
444 }
445
Andrea Arcangelibc835012011-01-13 15:47:08 -0800446 if (!PageLRU(page))
447 continue;
448
449 /*
450 * PageLRU is set, and lru_lock excludes isolation,
451 * splitting and collapsing (collapsing has already
452 * happened if PageLRU is set).
453 */
454 if (PageTransHuge(page)) {
455 low_pfn += (1 << compound_order(page)) - 1;
456 continue;
457 }
458
Mel Gormanc8244932012-01-12 17:19:38 -0800459 if (!cc->sync)
460 mode |= ISOLATE_ASYNC_MIGRATE;
461
Mel Gorman748446b2010-05-24 14:32:27 -0700462 /* Try isolate the page */
Minchan Kim39deaf82011-10-31 17:06:51 -0700463 if (__isolate_lru_page(page, mode, 0) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700464 continue;
465
Andrea Arcangelibc835012011-01-13 15:47:08 -0800466 VM_BUG_ON(PageTransCompound(page));
467
Mel Gorman748446b2010-05-24 14:32:27 -0700468 /* Successfully isolated */
469 del_page_from_lru_list(zone, page, page_lru(page));
470 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700471 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800472 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700473
474 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800475 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
476 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700477 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800478 }
Mel Gorman748446b2010-05-24 14:32:27 -0700479 }
480
481 acct_isolated(zone, cc);
482
483 spin_unlock_irq(&zone->lru_lock);
Mel Gorman748446b2010-05-24 14:32:27 -0700484
Mel Gormanb7aba692011-01-13 15:45:54 -0800485 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
486
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100487 return low_pfn;
488}
489
490/*
491 * Isolate all pages that can be migrated from the block pointed to by
492 * the migrate scanner within compact_control.
493 */
494static isolate_migrate_t isolate_migratepages(struct zone *zone,
495 struct compact_control *cc)
496{
497 unsigned long low_pfn, end_pfn;
498
499 /* Do not scan outside zone boundaries */
500 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
501
502 /* Only scan within a pageblock boundary */
503 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
504
505 /* Do not cross the free scanner or scan within a memory hole */
506 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
507 cc->migrate_pfn = end_pfn;
508 return ISOLATE_NONE;
509 }
510
511 /* Perform the isolation */
512 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
513 if (!low_pfn)
514 return ISOLATE_ABORT;
515
516 cc->migrate_pfn = low_pfn;
517
Mel Gormanf9e35b32011-06-15 15:08:52 -0700518 return ISOLATE_SUCCESS;
Mel Gorman748446b2010-05-24 14:32:27 -0700519}
520
521/*
522 * This is a migrate-callback that "allocates" freepages by taking pages
523 * from the isolated freelists in the block we are migrating to.
524 */
525static struct page *compaction_alloc(struct page *migratepage,
526 unsigned long data,
527 int **result)
528{
529 struct compact_control *cc = (struct compact_control *)data;
530 struct page *freepage;
531
532 /* Isolate free pages if necessary */
533 if (list_empty(&cc->freepages)) {
534 isolate_freepages(cc->zone, cc);
535
536 if (list_empty(&cc->freepages))
537 return NULL;
538 }
539
540 freepage = list_entry(cc->freepages.next, struct page, lru);
541 list_del(&freepage->lru);
542 cc->nr_freepages--;
543
544 return freepage;
545}
546
547/*
548 * We cannot control nr_migratepages and nr_freepages fully when migration is
549 * running as migrate_pages() has no knowledge of compact_control. When
550 * migration is complete, we count the number of pages on the lists by hand.
551 */
552static void update_nr_listpages(struct compact_control *cc)
553{
554 int nr_migratepages = 0;
555 int nr_freepages = 0;
556 struct page *page;
557
558 list_for_each_entry(page, &cc->migratepages, lru)
559 nr_migratepages++;
560 list_for_each_entry(page, &cc->freepages, lru)
561 nr_freepages++;
562
563 cc->nr_migratepages = nr_migratepages;
564 cc->nr_freepages = nr_freepages;
565}
566
567static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800568 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700569{
Mel Gorman56de7262010-05-24 14:32:30 -0700570 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800571 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700572
Mel Gorman748446b2010-05-24 14:32:27 -0700573 if (fatal_signal_pending(current))
574 return COMPACT_PARTIAL;
575
576 /* Compaction run completes if the migrate and free scanner meet */
577 if (cc->free_pfn <= cc->migrate_pfn)
578 return COMPACT_COMPLETE;
579
Johannes Weiner82478fb2011-01-20 14:44:21 -0800580 /*
581 * order == -1 is expected when compacting via
582 * /proc/sys/vm/compact_memory
583 */
Mel Gorman56de7262010-05-24 14:32:30 -0700584 if (cc->order == -1)
585 return COMPACT_CONTINUE;
586
Michal Hocko3957c772011-06-15 15:08:25 -0700587 /* Compaction run is not finished if the watermark is not met */
588 watermark = low_wmark_pages(zone);
589 watermark += (1 << cc->order);
590
591 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
592 return COMPACT_CONTINUE;
593
Mel Gorman56de7262010-05-24 14:32:30 -0700594 /* Direct compactor: Is a suitable page free? */
595 for (order = cc->order; order < MAX_ORDER; order++) {
596 /* Job done if page is free of the right migratetype */
597 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
598 return COMPACT_PARTIAL;
599
600 /* Job done if allocation would set block type */
601 if (order >= pageblock_order && zone->free_area[order].nr_free)
602 return COMPACT_PARTIAL;
603 }
604
Mel Gorman748446b2010-05-24 14:32:27 -0700605 return COMPACT_CONTINUE;
606}
607
Mel Gorman3e7d3442011-01-13 15:45:56 -0800608/*
609 * compaction_suitable: Is this suitable to run compaction on this zone now?
610 * Returns
611 * COMPACT_SKIPPED - If there are too few free pages for compaction
612 * COMPACT_PARTIAL - If the allocation would succeed without compaction
613 * COMPACT_CONTINUE - If compaction should run now
614 */
615unsigned long compaction_suitable(struct zone *zone, int order)
616{
617 int fragindex;
618 unsigned long watermark;
619
620 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700621 * order == -1 is expected when compacting via
622 * /proc/sys/vm/compact_memory
623 */
624 if (order == -1)
625 return COMPACT_CONTINUE;
626
627 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800628 * Watermarks for order-0 must be met for compaction. Note the 2UL.
629 * This is because during migration, copies of pages need to be
630 * allocated and for a short time, the footprint is higher
631 */
632 watermark = low_wmark_pages(zone) + (2UL << order);
633 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
634 return COMPACT_SKIPPED;
635
636 /*
637 * fragmentation index determines if allocation failures are due to
638 * low memory or external fragmentation
639 *
Shaohua Lia582a732011-06-15 15:08:49 -0700640 * index of -1000 implies allocations might succeed depending on
641 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800642 * index towards 0 implies failure is due to lack of memory
643 * index towards 1000 implies failure is due to fragmentation
644 *
645 * Only compact if a failure would be due to fragmentation.
646 */
647 fragindex = fragmentation_index(zone, order);
648 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
649 return COMPACT_SKIPPED;
650
Shaohua Lia582a732011-06-15 15:08:49 -0700651 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
652 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800653 return COMPACT_PARTIAL;
654
655 return COMPACT_CONTINUE;
656}
657
Mel Gorman748446b2010-05-24 14:32:27 -0700658static int compact_zone(struct zone *zone, struct compact_control *cc)
659{
660 int ret;
661
Mel Gorman3e7d3442011-01-13 15:45:56 -0800662 ret = compaction_suitable(zone, cc->order);
663 switch (ret) {
664 case COMPACT_PARTIAL:
665 case COMPACT_SKIPPED:
666 /* Compaction is likely to fail */
667 return ret;
668 case COMPACT_CONTINUE:
669 /* Fall through to compaction */
670 ;
671 }
672
Mel Gorman748446b2010-05-24 14:32:27 -0700673 /* Setup to move all movable pages to the end of the zone */
674 cc->migrate_pfn = zone->zone_start_pfn;
675 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
676 cc->free_pfn &= ~(pageblock_nr_pages-1);
677
678 migrate_prep_local();
679
680 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
681 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700682 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700683
Mel Gormanf9e35b32011-06-15 15:08:52 -0700684 switch (isolate_migratepages(zone, cc)) {
685 case ISOLATE_ABORT:
686 ret = COMPACT_PARTIAL;
687 goto out;
688 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700689 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700690 case ISOLATE_SUCCESS:
691 ;
692 }
Mel Gorman748446b2010-05-24 14:32:27 -0700693
694 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700695 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800696 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800697 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700698 update_nr_listpages(cc);
699 nr_remaining = cc->nr_migratepages;
700
701 count_vm_event(COMPACTBLOCKS);
702 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
703 if (nr_remaining)
704 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800705 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
706 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700707
708 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700709 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700710 putback_lru_pages(&cc->migratepages);
711 cc->nr_migratepages = 0;
712 }
713
714 }
715
Mel Gormanf9e35b32011-06-15 15:08:52 -0700716out:
Mel Gorman748446b2010-05-24 14:32:27 -0700717 /* Release free pages and check accounting */
718 cc->nr_freepages -= release_freepages(&cc->freepages);
719 VM_BUG_ON(cc->nr_freepages != 0);
720
721 return ret;
722}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700723
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700724static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800725 int order, gfp_t gfp_mask,
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700726 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700727{
728 struct compact_control cc = {
729 .nr_freepages = 0,
730 .nr_migratepages = 0,
731 .order = order,
732 .migratetype = allocflags_to_migratetype(gfp_mask),
733 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800734 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -0700735 };
736 INIT_LIST_HEAD(&cc.freepages);
737 INIT_LIST_HEAD(&cc.migratepages);
738
739 return compact_zone(zone, &cc);
740}
741
Mel Gorman5e771902010-05-24 14:32:31 -0700742int sysctl_extfrag_threshold = 500;
743
Mel Gorman56de7262010-05-24 14:32:30 -0700744/**
745 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
746 * @zonelist: The zonelist used for the current allocation
747 * @order: The order of the current allocation
748 * @gfp_mask: The GFP mask of the current allocation
749 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800750 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700751 *
752 * This is the main entry point for direct page compaction.
753 */
754unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800755 int order, gfp_t gfp_mask, nodemask_t *nodemask,
756 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700757{
758 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
759 int may_enter_fs = gfp_mask & __GFP_FS;
760 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700761 struct zoneref *z;
762 struct zone *zone;
763 int rc = COMPACT_SKIPPED;
764
765 /*
766 * Check whether it is worth even starting compaction. The order check is
767 * made because an assumption is made that the page allocator can satisfy
768 * the "cheaper" orders without taking special steps
769 */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800770 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700771 return rc;
772
773 count_vm_event(COMPACTSTALL);
774
775 /* Compact each zone in the list */
776 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
777 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700778 int status;
779
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700780 status = compact_zone_order(zone, order, gfp_mask, sync);
Mel Gorman56de7262010-05-24 14:32:30 -0700781 rc = max(status, rc);
782
Mel Gorman3e7d3442011-01-13 15:45:56 -0800783 /* If a normal allocation would succeed, stop compacting */
784 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
Mel Gorman56de7262010-05-24 14:32:30 -0700785 break;
786 }
787
788 return rc;
789}
790
791
Mel Gorman76ab0f52010-05-24 14:32:28 -0700792/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700793static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700794{
795 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700796 struct zone *zone;
797
Mel Gorman76ab0f52010-05-24 14:32:28 -0700798 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700799
800 zone = &pgdat->node_zones[zoneid];
801 if (!populated_zone(zone))
802 continue;
803
Rik van Riel7be62de2012-03-21 16:33:52 -0700804 cc->nr_freepages = 0;
805 cc->nr_migratepages = 0;
806 cc->zone = zone;
807 INIT_LIST_HEAD(&cc->freepages);
808 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700809
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700810 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700811 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700812
Rik van Rielaff62242012-03-21 16:33:52 -0700813 if (cc->order > 0) {
814 int ok = zone_watermark_ok(zone, cc->order,
815 low_wmark_pages(zone), 0, 0);
816 if (ok && cc->order > zone->compact_order_failed)
817 zone->compact_order_failed = cc->order + 1;
818 /* Currently async compaction is never deferred. */
819 else if (!ok && cc->sync)
820 defer_compaction(zone, cc->order);
821 }
822
Rik van Riel7be62de2012-03-21 16:33:52 -0700823 VM_BUG_ON(!list_empty(&cc->freepages));
824 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700825 }
826
827 return 0;
828}
829
Rik van Riel7be62de2012-03-21 16:33:52 -0700830int compact_pgdat(pg_data_t *pgdat, int order)
831{
832 struct compact_control cc = {
833 .order = order,
834 .sync = false,
835 };
836
837 return __compact_pgdat(pgdat, &cc);
838}
839
840static int compact_node(int nid)
841{
Rik van Riel7be62de2012-03-21 16:33:52 -0700842 struct compact_control cc = {
843 .order = -1,
844 .sync = true,
845 };
846
Hugh Dickins8575ec22012-03-21 16:33:53 -0700847 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700848}
849
Mel Gorman76ab0f52010-05-24 14:32:28 -0700850/* Compact all nodes in the system */
851static int compact_nodes(void)
852{
853 int nid;
854
Hugh Dickins8575ec22012-03-21 16:33:53 -0700855 /* Flush pending updates to the LRU lists */
856 lru_add_drain_all();
857
Mel Gorman76ab0f52010-05-24 14:32:28 -0700858 for_each_online_node(nid)
859 compact_node(nid);
860
861 return COMPACT_COMPLETE;
862}
863
864/* The written value is actually unused, all memory is compacted */
865int sysctl_compact_memory;
866
867/* This is the entry point for compacting all nodes via /proc/sys/vm */
868int sysctl_compaction_handler(struct ctl_table *table, int write,
869 void __user *buffer, size_t *length, loff_t *ppos)
870{
871 if (write)
872 return compact_nodes();
873
874 return 0;
875}
Mel Gormaned4a6d72010-05-24 14:32:29 -0700876
Mel Gorman5e771902010-05-24 14:32:31 -0700877int sysctl_extfrag_handler(struct ctl_table *table, int write,
878 void __user *buffer, size_t *length, loff_t *ppos)
879{
880 proc_dointvec_minmax(table, write, buffer, length, ppos);
881
882 return 0;
883}
884
Mel Gormaned4a6d72010-05-24 14:32:29 -0700885#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800886ssize_t sysfs_compact_node(struct device *dev,
887 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -0700888 const char *buf, size_t count)
889{
Hugh Dickins8575ec22012-03-21 16:33:53 -0700890 int nid = dev->id;
891
892 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
893 /* Flush pending updates to the LRU lists */
894 lru_add_drain_all();
895
896 compact_node(nid);
897 }
Mel Gormaned4a6d72010-05-24 14:32:29 -0700898
899 return count;
900}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800901static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700902
903int compaction_register_node(struct node *node)
904{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800905 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700906}
907
908void compaction_unregister_node(struct node *node)
909{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800910 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700911}
912#endif /* CONFIG_SYSFS && CONFIG_NUMA */