blob: d9d7b35d39330ab3644e0f40ae17cbd4a79b5793 [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
57/* Isolate free pages onto a private freelist. Must hold zone->lock */
58static unsigned long isolate_freepages_block(struct zone *zone,
59 unsigned long blockpfn,
60 struct list_head *freelist)
61{
62 unsigned long zone_end_pfn, end_pfn;
Mel Gormanb7aba692011-01-13 15:45:54 -080063 int nr_scanned = 0, total_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070064 struct page *cursor;
65
66 /* Get the last PFN we should scan for free pages at */
67 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
68 end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn);
69
70 /* Find the first usable PFN in the block to initialse page cursor */
71 for (; blockpfn < end_pfn; blockpfn++) {
72 if (pfn_valid_within(blockpfn))
73 break;
74 }
75 cursor = pfn_to_page(blockpfn);
76
77 /* Isolate free pages. This assumes the block is valid */
78 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
79 int isolated, i;
80 struct page *page = cursor;
81
82 if (!pfn_valid_within(blockpfn))
83 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -080084 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -070085
86 if (!PageBuddy(page))
87 continue;
88
89 /* Found a free page, break it into order-0 pages */
90 isolated = split_free_page(page);
91 total_isolated += isolated;
92 for (i = 0; i < isolated; i++) {
93 list_add(&page->lru, freelist);
94 page++;
95 }
96
97 /* If a page was split, advance to the end of it */
98 if (isolated) {
99 blockpfn += isolated - 1;
100 cursor += isolated - 1;
101 }
102 }
103
Mel Gormanb7aba692011-01-13 15:45:54 -0800104 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700105 return total_isolated;
106}
107
108/* Returns true if the page is within a block suitable for migration to */
109static bool suitable_migration_target(struct page *page)
110{
111
112 int migratetype = get_pageblock_migratetype(page);
113
114 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
115 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
116 return false;
117
118 /* If the page is a large free page, then allow migration */
119 if (PageBuddy(page) && page_order(page) >= pageblock_order)
120 return true;
121
122 /* If the block is MIGRATE_MOVABLE, allow migration */
123 if (migratetype == MIGRATE_MOVABLE)
124 return true;
125
126 /* Otherwise skip the block */
127 return false;
128}
129
Michal Nazarewicz03d44192012-01-30 13:23:47 +0100130static void map_pages(struct list_head *list)
131{
132 struct page *page;
133
134 list_for_each_entry(page, list, lru) {
135 arch_alloc_page(page, 0);
136 kernel_map_pages(page, 1, 1);
137 }
138}
139
Mel Gorman748446b2010-05-24 14:32:27 -0700140/*
141 * Based on information in the current compact_control, find blocks
142 * suitable for isolating free pages from and then isolate them.
143 */
144static void isolate_freepages(struct zone *zone,
145 struct compact_control *cc)
146{
147 struct page *page;
148 unsigned long high_pfn, low_pfn, pfn;
149 unsigned long flags;
150 int nr_freepages = cc->nr_freepages;
151 struct list_head *freelist = &cc->freepages;
152
Mel Gorman7454f4b2011-06-15 15:08:50 -0700153 /*
154 * Initialise the free scanner. The starting point is where we last
155 * scanned from (or the end of the zone if starting). The low point
156 * is the end of the pageblock the migration scanner is using.
157 */
Mel Gorman748446b2010-05-24 14:32:27 -0700158 pfn = cc->free_pfn;
159 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
Mel Gorman7454f4b2011-06-15 15:08:50 -0700160
161 /*
162 * Take care that if the migration scanner is at the end of the zone
163 * that the free scanner does not accidentally move to the next zone
164 * in the next isolation cycle.
165 */
166 high_pfn = min(low_pfn, pfn);
Mel Gorman748446b2010-05-24 14:32:27 -0700167
168 /*
169 * Isolate free pages until enough are available to migrate the
170 * pages on cc->migratepages. We stop searching if the migrate
171 * and free page scanners meet or enough free pages are isolated.
172 */
Mel Gorman748446b2010-05-24 14:32:27 -0700173 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
174 pfn -= pageblock_nr_pages) {
175 unsigned long isolated;
176
177 if (!pfn_valid(pfn))
178 continue;
179
180 /*
181 * Check for overlapping nodes/zones. It's possible on some
182 * configurations to have a setup like
183 * node0 node1 node0
184 * i.e. it's possible that all pages within a zones range of
185 * pages do not belong to a single zone.
186 */
187 page = pfn_to_page(pfn);
188 if (page_zone(page) != zone)
189 continue;
190
191 /* Check the block is suitable for migration */
192 if (!suitable_migration_target(page))
193 continue;
194
Mel Gorman602605a2011-03-22 16:33:08 -0700195 /*
196 * Found a block suitable for isolating free pages from. Now
197 * we disabled interrupts, double check things are ok and
198 * isolate the pages. This is to minimise the time IRQs
199 * are disabled
200 */
201 isolated = 0;
202 spin_lock_irqsave(&zone->lock, flags);
203 if (suitable_migration_target(page)) {
204 isolated = isolate_freepages_block(zone, pfn, freelist);
205 nr_freepages += isolated;
206 }
207 spin_unlock_irqrestore(&zone->lock, flags);
Mel Gorman748446b2010-05-24 14:32:27 -0700208
209 /*
210 * Record the highest PFN we isolated pages from. When next
211 * looking for free pages, the search will restart here as
212 * page migration may have returned some pages to the allocator
213 */
214 if (isolated)
215 high_pfn = max(high_pfn, pfn);
216 }
Mel Gorman748446b2010-05-24 14:32:27 -0700217
218 /* split_free_page does not map the pages */
Michal Nazarewicz03d44192012-01-30 13:23:47 +0100219 map_pages(freelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700220
221 cc->free_pfn = high_pfn;
222 cc->nr_freepages = nr_freepages;
223}
224
225/* Update the number of anon and file isolated pages in the zone */
226static void acct_isolated(struct zone *zone, struct compact_control *cc)
227{
228 struct page *page;
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700229 unsigned int count[2] = { 0, };
Mel Gorman748446b2010-05-24 14:32:27 -0700230
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700231 list_for_each_entry(page, &cc->migratepages, lru)
232 count[!!page_is_file_cache(page)]++;
Mel Gorman748446b2010-05-24 14:32:27 -0700233
Minchan Kimb9e84ac2011-10-31 17:06:44 -0700234 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
235 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
Mel Gorman748446b2010-05-24 14:32:27 -0700236}
237
238/* Similar to reclaim, but different enough that they don't share logic */
239static bool too_many_isolated(struct zone *zone)
240{
Minchan Kimbc693042010-09-09 16:38:00 -0700241 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700242
243 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
244 zone_page_state(zone, NR_INACTIVE_ANON);
Minchan Kimbc693042010-09-09 16:38:00 -0700245 active = zone_page_state(zone, NR_ACTIVE_FILE) +
246 zone_page_state(zone, NR_ACTIVE_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700247 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
248 zone_page_state(zone, NR_ISOLATED_ANON);
249
Minchan Kimbc693042010-09-09 16:38:00 -0700250 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700251}
252
Mel Gormanf9e35b32011-06-15 15:08:52 -0700253/* possible outcome of isolate_migratepages */
254typedef enum {
255 ISOLATE_ABORT, /* Abort compaction now */
256 ISOLATE_NONE, /* No pages isolated, continue scanning */
257 ISOLATE_SUCCESS, /* Pages isolated, migrate */
258} isolate_migrate_t;
259
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100260/**
261 * isolate_migratepages_range() - isolate all migrate-able pages in range.
262 * @zone: Zone pages are in.
263 * @cc: Compaction control structure.
264 * @low_pfn: The first PFN of the range.
265 * @end_pfn: The one-past-the-last PFN of the range.
266 *
267 * Isolate all pages that can be migrated from the range specified by
268 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
269 * pending), otherwise PFN of the first page that was not scanned
270 * (which may be both less, equal to or more then end_pfn).
271 *
272 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
273 * zero.
274 *
275 * Apart from cc->migratepages and cc->nr_migratetypes this function
276 * does not modify any cc's fields, in particular it does not modify
277 * (or read for that matter) cc->migrate_pfn.
Mel Gorman748446b2010-05-24 14:32:27 -0700278 */
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100279static unsigned long
280isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
281 unsigned long low_pfn, unsigned long end_pfn)
Mel Gorman748446b2010-05-24 14:32:27 -0700282{
Mel Gorman9927af742011-01-13 15:45:59 -0800283 unsigned long last_pageblock_nr = 0, pageblock_nr;
Mel Gormanb7aba692011-01-13 15:45:54 -0800284 unsigned long nr_scanned = 0, nr_isolated = 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700285 struct list_head *migratelist = &cc->migratepages;
Minchan Kim39deaf82011-10-31 17:06:51 -0700286 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
Mel Gorman748446b2010-05-24 14:32:27 -0700287
Mel Gorman748446b2010-05-24 14:32:27 -0700288 /*
289 * Ensure that there are not too many pages isolated from the LRU
290 * list by either parallel reclaimers or compaction. If there are,
291 * delay for some time until fewer pages are isolated
292 */
293 while (unlikely(too_many_isolated(zone))) {
Mel Gormanf9e35b32011-06-15 15:08:52 -0700294 /* async migration should just abort */
295 if (!cc->sync)
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100296 return 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700297
Mel Gorman748446b2010-05-24 14:32:27 -0700298 congestion_wait(BLK_RW_ASYNC, HZ/10);
299
300 if (fatal_signal_pending(current))
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100301 return 0;
Mel Gorman748446b2010-05-24 14:32:27 -0700302 }
303
304 /* Time to isolate some pages for migration */
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700305 cond_resched();
Mel Gorman748446b2010-05-24 14:32:27 -0700306 spin_lock_irq(&zone->lru_lock);
307 for (; low_pfn < end_pfn; low_pfn++) {
308 struct page *page;
Andrea Arcangelib2eef8c2011-03-22 16:33:10 -0700309 bool locked = true;
310
311 /* give a chance to irqs before checking need_resched() */
312 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
313 spin_unlock_irq(&zone->lru_lock);
314 locked = false;
315 }
316 if (need_resched() || spin_is_contended(&zone->lru_lock)) {
317 if (locked)
318 spin_unlock_irq(&zone->lru_lock);
319 cond_resched();
320 spin_lock_irq(&zone->lru_lock);
321 if (fatal_signal_pending(current))
322 break;
323 } else if (!locked)
324 spin_lock_irq(&zone->lru_lock);
325
Mel Gorman0bf380b2012-02-03 15:37:18 -0800326 /*
327 * migrate_pfn does not necessarily start aligned to a
328 * pageblock. Ensure that pfn_valid is called when moving
329 * into a new MAX_ORDER_NR_PAGES range in case of large
330 * memory holes within the zone
331 */
332 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
333 if (!pfn_valid(low_pfn)) {
334 low_pfn += MAX_ORDER_NR_PAGES - 1;
335 continue;
336 }
337 }
338
Mel Gorman748446b2010-05-24 14:32:27 -0700339 if (!pfn_valid_within(low_pfn))
340 continue;
Mel Gormanb7aba692011-01-13 15:45:54 -0800341 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700342
Mel Gormandc908602012-02-08 17:13:38 -0800343 /*
344 * Get the page and ensure the page is within the same zone.
345 * See the comment in isolate_freepages about overlapping
346 * nodes. It is deliberate that the new zone lock is not taken
347 * as memory compaction should not move pages between nodes.
348 */
Mel Gorman748446b2010-05-24 14:32:27 -0700349 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800350 if (page_zone(page) != zone)
351 continue;
352
353 /* Skip if free */
Mel Gorman748446b2010-05-24 14:32:27 -0700354 if (PageBuddy(page))
355 continue;
356
Mel Gorman9927af742011-01-13 15:45:59 -0800357 /*
358 * For async migration, also only scan in MOVABLE blocks. Async
359 * migration is optimistic to see if the minimum amount of work
360 * satisfies the allocation
361 */
362 pageblock_nr = low_pfn >> pageblock_order;
363 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
364 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
365 low_pfn += pageblock_nr_pages;
366 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
367 last_pageblock_nr = pageblock_nr;
368 continue;
369 }
370
Andrea Arcangelibc835012011-01-13 15:47:08 -0800371 if (!PageLRU(page))
372 continue;
373
374 /*
375 * PageLRU is set, and lru_lock excludes isolation,
376 * splitting and collapsing (collapsing has already
377 * happened if PageLRU is set).
378 */
379 if (PageTransHuge(page)) {
380 low_pfn += (1 << compound_order(page)) - 1;
381 continue;
382 }
383
Mel Gormanc8244932012-01-12 17:19:38 -0800384 if (!cc->sync)
385 mode |= ISOLATE_ASYNC_MIGRATE;
386
Mel Gorman748446b2010-05-24 14:32:27 -0700387 /* Try isolate the page */
Minchan Kim39deaf82011-10-31 17:06:51 -0700388 if (__isolate_lru_page(page, mode, 0) != 0)
Mel Gorman748446b2010-05-24 14:32:27 -0700389 continue;
390
Andrea Arcangelibc835012011-01-13 15:47:08 -0800391 VM_BUG_ON(PageTransCompound(page));
392
Mel Gorman748446b2010-05-24 14:32:27 -0700393 /* Successfully isolated */
394 del_page_from_lru_list(zone, page, page_lru(page));
395 list_add(&page->lru, migratelist);
Mel Gorman748446b2010-05-24 14:32:27 -0700396 cc->nr_migratepages++;
Mel Gormanb7aba692011-01-13 15:45:54 -0800397 nr_isolated++;
Mel Gorman748446b2010-05-24 14:32:27 -0700398
399 /* Avoid isolating too much */
Hillf Danton31b83842012-01-10 15:07:59 -0800400 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
401 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700402 break;
Hillf Danton31b83842012-01-10 15:07:59 -0800403 }
Mel Gorman748446b2010-05-24 14:32:27 -0700404 }
405
406 acct_isolated(zone, cc);
407
408 spin_unlock_irq(&zone->lru_lock);
Mel Gorman748446b2010-05-24 14:32:27 -0700409
Mel Gormanb7aba692011-01-13 15:45:54 -0800410 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
411
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100412 return low_pfn;
413}
414
415/*
416 * Isolate all pages that can be migrated from the block pointed to by
417 * the migrate scanner within compact_control.
418 */
419static isolate_migrate_t isolate_migratepages(struct zone *zone,
420 struct compact_control *cc)
421{
422 unsigned long low_pfn, end_pfn;
423
424 /* Do not scan outside zone boundaries */
425 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
426
427 /* Only scan within a pageblock boundary */
428 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
429
430 /* Do not cross the free scanner or scan within a memory hole */
431 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
432 cc->migrate_pfn = end_pfn;
433 return ISOLATE_NONE;
434 }
435
436 /* Perform the isolation */
437 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
438 if (!low_pfn)
439 return ISOLATE_ABORT;
440
441 cc->migrate_pfn = low_pfn;
442
Mel Gormanf9e35b32011-06-15 15:08:52 -0700443 return ISOLATE_SUCCESS;
Mel Gorman748446b2010-05-24 14:32:27 -0700444}
445
446/*
447 * This is a migrate-callback that "allocates" freepages by taking pages
448 * from the isolated freelists in the block we are migrating to.
449 */
450static struct page *compaction_alloc(struct page *migratepage,
451 unsigned long data,
452 int **result)
453{
454 struct compact_control *cc = (struct compact_control *)data;
455 struct page *freepage;
456
457 /* Isolate free pages if necessary */
458 if (list_empty(&cc->freepages)) {
459 isolate_freepages(cc->zone, cc);
460
461 if (list_empty(&cc->freepages))
462 return NULL;
463 }
464
465 freepage = list_entry(cc->freepages.next, struct page, lru);
466 list_del(&freepage->lru);
467 cc->nr_freepages--;
468
469 return freepage;
470}
471
472/*
473 * We cannot control nr_migratepages and nr_freepages fully when migration is
474 * running as migrate_pages() has no knowledge of compact_control. When
475 * migration is complete, we count the number of pages on the lists by hand.
476 */
477static void update_nr_listpages(struct compact_control *cc)
478{
479 int nr_migratepages = 0;
480 int nr_freepages = 0;
481 struct page *page;
482
483 list_for_each_entry(page, &cc->migratepages, lru)
484 nr_migratepages++;
485 list_for_each_entry(page, &cc->freepages, lru)
486 nr_freepages++;
487
488 cc->nr_migratepages = nr_migratepages;
489 cc->nr_freepages = nr_freepages;
490}
491
492static int compact_finished(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800493 struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -0700494{
Mel Gorman56de7262010-05-24 14:32:30 -0700495 unsigned int order;
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800496 unsigned long watermark;
Mel Gorman56de7262010-05-24 14:32:30 -0700497
Mel Gorman748446b2010-05-24 14:32:27 -0700498 if (fatal_signal_pending(current))
499 return COMPACT_PARTIAL;
500
501 /* Compaction run completes if the migrate and free scanner meet */
502 if (cc->free_pfn <= cc->migrate_pfn)
503 return COMPACT_COMPLETE;
504
Johannes Weiner82478fb2011-01-20 14:44:21 -0800505 /*
506 * order == -1 is expected when compacting via
507 * /proc/sys/vm/compact_memory
508 */
Mel Gorman56de7262010-05-24 14:32:30 -0700509 if (cc->order == -1)
510 return COMPACT_CONTINUE;
511
Michal Hocko3957c772011-06-15 15:08:25 -0700512 /* Compaction run is not finished if the watermark is not met */
513 watermark = low_wmark_pages(zone);
514 watermark += (1 << cc->order);
515
516 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
517 return COMPACT_CONTINUE;
518
Mel Gorman56de7262010-05-24 14:32:30 -0700519 /* Direct compactor: Is a suitable page free? */
520 for (order = cc->order; order < MAX_ORDER; order++) {
521 /* Job done if page is free of the right migratetype */
522 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
523 return COMPACT_PARTIAL;
524
525 /* Job done if allocation would set block type */
526 if (order >= pageblock_order && zone->free_area[order].nr_free)
527 return COMPACT_PARTIAL;
528 }
529
Mel Gorman748446b2010-05-24 14:32:27 -0700530 return COMPACT_CONTINUE;
531}
532
Mel Gorman3e7d3442011-01-13 15:45:56 -0800533/*
534 * compaction_suitable: Is this suitable to run compaction on this zone now?
535 * Returns
536 * COMPACT_SKIPPED - If there are too few free pages for compaction
537 * COMPACT_PARTIAL - If the allocation would succeed without compaction
538 * COMPACT_CONTINUE - If compaction should run now
539 */
540unsigned long compaction_suitable(struct zone *zone, int order)
541{
542 int fragindex;
543 unsigned long watermark;
544
545 /*
Michal Hocko3957c772011-06-15 15:08:25 -0700546 * order == -1 is expected when compacting via
547 * /proc/sys/vm/compact_memory
548 */
549 if (order == -1)
550 return COMPACT_CONTINUE;
551
552 /*
Mel Gorman3e7d3442011-01-13 15:45:56 -0800553 * Watermarks for order-0 must be met for compaction. Note the 2UL.
554 * This is because during migration, copies of pages need to be
555 * allocated and for a short time, the footprint is higher
556 */
557 watermark = low_wmark_pages(zone) + (2UL << order);
558 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
559 return COMPACT_SKIPPED;
560
561 /*
562 * fragmentation index determines if allocation failures are due to
563 * low memory or external fragmentation
564 *
Shaohua Lia582a732011-06-15 15:08:49 -0700565 * index of -1000 implies allocations might succeed depending on
566 * watermarks
Mel Gorman3e7d3442011-01-13 15:45:56 -0800567 * index towards 0 implies failure is due to lack of memory
568 * index towards 1000 implies failure is due to fragmentation
569 *
570 * Only compact if a failure would be due to fragmentation.
571 */
572 fragindex = fragmentation_index(zone, order);
573 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
574 return COMPACT_SKIPPED;
575
Shaohua Lia582a732011-06-15 15:08:49 -0700576 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
577 0, 0))
Mel Gorman3e7d3442011-01-13 15:45:56 -0800578 return COMPACT_PARTIAL;
579
580 return COMPACT_CONTINUE;
581}
582
Mel Gorman748446b2010-05-24 14:32:27 -0700583static int compact_zone(struct zone *zone, struct compact_control *cc)
584{
585 int ret;
586
Mel Gorman3e7d3442011-01-13 15:45:56 -0800587 ret = compaction_suitable(zone, cc->order);
588 switch (ret) {
589 case COMPACT_PARTIAL:
590 case COMPACT_SKIPPED:
591 /* Compaction is likely to fail */
592 return ret;
593 case COMPACT_CONTINUE:
594 /* Fall through to compaction */
595 ;
596 }
597
Mel Gorman748446b2010-05-24 14:32:27 -0700598 /* Setup to move all movable pages to the end of the zone */
599 cc->migrate_pfn = zone->zone_start_pfn;
600 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
601 cc->free_pfn &= ~(pageblock_nr_pages-1);
602
603 migrate_prep_local();
604
605 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
606 unsigned long nr_migrate, nr_remaining;
Minchan Kim9d502c12011-03-22 16:30:39 -0700607 int err;
Mel Gorman748446b2010-05-24 14:32:27 -0700608
Mel Gormanf9e35b32011-06-15 15:08:52 -0700609 switch (isolate_migratepages(zone, cc)) {
610 case ISOLATE_ABORT:
611 ret = COMPACT_PARTIAL;
612 goto out;
613 case ISOLATE_NONE:
Mel Gorman748446b2010-05-24 14:32:27 -0700614 continue;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700615 case ISOLATE_SUCCESS:
616 ;
617 }
Mel Gorman748446b2010-05-24 14:32:27 -0700618
619 nr_migrate = cc->nr_migratepages;
Minchan Kim9d502c12011-03-22 16:30:39 -0700620 err = migrate_pages(&cc->migratepages, compaction_alloc,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800621 (unsigned long)cc, false,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800622 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
Mel Gorman748446b2010-05-24 14:32:27 -0700623 update_nr_listpages(cc);
624 nr_remaining = cc->nr_migratepages;
625
626 count_vm_event(COMPACTBLOCKS);
627 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
628 if (nr_remaining)
629 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
Mel Gormanb7aba692011-01-13 15:45:54 -0800630 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
631 nr_remaining);
Mel Gorman748446b2010-05-24 14:32:27 -0700632
633 /* Release LRU pages not migrated */
Minchan Kim9d502c12011-03-22 16:30:39 -0700634 if (err) {
Mel Gorman748446b2010-05-24 14:32:27 -0700635 putback_lru_pages(&cc->migratepages);
636 cc->nr_migratepages = 0;
637 }
638
639 }
640
Mel Gormanf9e35b32011-06-15 15:08:52 -0700641out:
Mel Gorman748446b2010-05-24 14:32:27 -0700642 /* Release free pages and check accounting */
643 cc->nr_freepages -= release_freepages(&cc->freepages);
644 VM_BUG_ON(cc->nr_freepages != 0);
645
646 return ret;
647}
Mel Gorman76ab0f52010-05-24 14:32:28 -0700648
Kyungmin Parkd43a87e2011-10-31 17:09:08 -0700649static unsigned long compact_zone_order(struct zone *zone,
Andrea Arcangeli5a03b052011-01-13 15:47:11 -0800650 int order, gfp_t gfp_mask,
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700651 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700652{
653 struct compact_control cc = {
654 .nr_freepages = 0,
655 .nr_migratepages = 0,
656 .order = order,
657 .migratetype = allocflags_to_migratetype(gfp_mask),
658 .zone = zone,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800659 .sync = sync,
Mel Gorman56de7262010-05-24 14:32:30 -0700660 };
661 INIT_LIST_HEAD(&cc.freepages);
662 INIT_LIST_HEAD(&cc.migratepages);
663
664 return compact_zone(zone, &cc);
665}
666
Mel Gorman5e771902010-05-24 14:32:31 -0700667int sysctl_extfrag_threshold = 500;
668
Mel Gorman56de7262010-05-24 14:32:30 -0700669/**
670 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
671 * @zonelist: The zonelist used for the current allocation
672 * @order: The order of the current allocation
673 * @gfp_mask: The GFP mask of the current allocation
674 * @nodemask: The allowed nodes to allocate from
Mel Gorman77f1fe62011-01-13 15:45:57 -0800675 * @sync: Whether migration is synchronous or not
Mel Gorman56de7262010-05-24 14:32:30 -0700676 *
677 * This is the main entry point for direct page compaction.
678 */
679unsigned long try_to_compact_pages(struct zonelist *zonelist,
Mel Gorman77f1fe62011-01-13 15:45:57 -0800680 int order, gfp_t gfp_mask, nodemask_t *nodemask,
681 bool sync)
Mel Gorman56de7262010-05-24 14:32:30 -0700682{
683 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
684 int may_enter_fs = gfp_mask & __GFP_FS;
685 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -0700686 struct zoneref *z;
687 struct zone *zone;
688 int rc = COMPACT_SKIPPED;
689
690 /*
691 * Check whether it is worth even starting compaction. The order check is
692 * made because an assumption is made that the page allocator can satisfy
693 * the "cheaper" orders without taking special steps
694 */
Andrea Arcangelic5a73c32011-01-13 15:47:11 -0800695 if (!order || !may_enter_fs || !may_perform_io)
Mel Gorman56de7262010-05-24 14:32:30 -0700696 return rc;
697
698 count_vm_event(COMPACTSTALL);
699
700 /* Compact each zone in the list */
701 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
702 nodemask) {
Mel Gorman56de7262010-05-24 14:32:30 -0700703 int status;
704
Andrea Arcangelid527caf2011-03-22 16:30:38 -0700705 status = compact_zone_order(zone, order, gfp_mask, sync);
Mel Gorman56de7262010-05-24 14:32:30 -0700706 rc = max(status, rc);
707
Mel Gorman3e7d3442011-01-13 15:45:56 -0800708 /* If a normal allocation would succeed, stop compacting */
709 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
Mel Gorman56de7262010-05-24 14:32:30 -0700710 break;
711 }
712
713 return rc;
714}
715
716
Mel Gorman76ab0f52010-05-24 14:32:28 -0700717/* Compact all zones within a node */
Rik van Riel7be62de2012-03-21 16:33:52 -0700718static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
Mel Gorman76ab0f52010-05-24 14:32:28 -0700719{
720 int zoneid;
Mel Gorman76ab0f52010-05-24 14:32:28 -0700721 struct zone *zone;
722
Mel Gorman76ab0f52010-05-24 14:32:28 -0700723 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
Mel Gorman76ab0f52010-05-24 14:32:28 -0700724
725 zone = &pgdat->node_zones[zoneid];
726 if (!populated_zone(zone))
727 continue;
728
Rik van Riel7be62de2012-03-21 16:33:52 -0700729 cc->nr_freepages = 0;
730 cc->nr_migratepages = 0;
731 cc->zone = zone;
732 INIT_LIST_HEAD(&cc->freepages);
733 INIT_LIST_HEAD(&cc->migratepages);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700734
Dan Carpenteraad6ec32012-03-21 16:33:54 -0700735 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
Rik van Riel7be62de2012-03-21 16:33:52 -0700736 compact_zone(zone, cc);
Mel Gorman76ab0f52010-05-24 14:32:28 -0700737
Rik van Rielaff62242012-03-21 16:33:52 -0700738 if (cc->order > 0) {
739 int ok = zone_watermark_ok(zone, cc->order,
740 low_wmark_pages(zone), 0, 0);
741 if (ok && cc->order > zone->compact_order_failed)
742 zone->compact_order_failed = cc->order + 1;
743 /* Currently async compaction is never deferred. */
744 else if (!ok && cc->sync)
745 defer_compaction(zone, cc->order);
746 }
747
Rik van Riel7be62de2012-03-21 16:33:52 -0700748 VM_BUG_ON(!list_empty(&cc->freepages));
749 VM_BUG_ON(!list_empty(&cc->migratepages));
Mel Gorman76ab0f52010-05-24 14:32:28 -0700750 }
751
752 return 0;
753}
754
Rik van Riel7be62de2012-03-21 16:33:52 -0700755int compact_pgdat(pg_data_t *pgdat, int order)
756{
757 struct compact_control cc = {
758 .order = order,
759 .sync = false,
760 };
761
762 return __compact_pgdat(pgdat, &cc);
763}
764
765static int compact_node(int nid)
766{
Rik van Riel7be62de2012-03-21 16:33:52 -0700767 struct compact_control cc = {
768 .order = -1,
769 .sync = true,
770 };
771
Hugh Dickins8575ec22012-03-21 16:33:53 -0700772 return __compact_pgdat(NODE_DATA(nid), &cc);
Rik van Riel7be62de2012-03-21 16:33:52 -0700773}
774
Mel Gorman76ab0f52010-05-24 14:32:28 -0700775/* Compact all nodes in the system */
776static int compact_nodes(void)
777{
778 int nid;
779
Hugh Dickins8575ec22012-03-21 16:33:53 -0700780 /* Flush pending updates to the LRU lists */
781 lru_add_drain_all();
782
Mel Gorman76ab0f52010-05-24 14:32:28 -0700783 for_each_online_node(nid)
784 compact_node(nid);
785
786 return COMPACT_COMPLETE;
787}
788
789/* The written value is actually unused, all memory is compacted */
790int sysctl_compact_memory;
791
792/* This is the entry point for compacting all nodes via /proc/sys/vm */
793int sysctl_compaction_handler(struct ctl_table *table, int write,
794 void __user *buffer, size_t *length, loff_t *ppos)
795{
796 if (write)
797 return compact_nodes();
798
799 return 0;
800}
Mel Gormaned4a6d72010-05-24 14:32:29 -0700801
Mel Gorman5e771902010-05-24 14:32:31 -0700802int sysctl_extfrag_handler(struct ctl_table *table, int write,
803 void __user *buffer, size_t *length, loff_t *ppos)
804{
805 proc_dointvec_minmax(table, write, buffer, length, ppos);
806
807 return 0;
808}
809
Mel Gormaned4a6d72010-05-24 14:32:29 -0700810#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800811ssize_t sysfs_compact_node(struct device *dev,
812 struct device_attribute *attr,
Mel Gormaned4a6d72010-05-24 14:32:29 -0700813 const char *buf, size_t count)
814{
Hugh Dickins8575ec22012-03-21 16:33:53 -0700815 int nid = dev->id;
816
817 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
818 /* Flush pending updates to the LRU lists */
819 lru_add_drain_all();
820
821 compact_node(nid);
822 }
Mel Gormaned4a6d72010-05-24 14:32:29 -0700823
824 return count;
825}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800826static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700827
828int compaction_register_node(struct node *node)
829{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800830 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700831}
832
833void compaction_unregister_node(struct node *node)
834{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800835 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -0700836}
837#endif /* CONFIG_SYSFS && CONFIG_NUMA */