mm: compaction: introduce isolate_freepages_range()
[deliverable/linux.git] / mm / compaction.c
CommitLineData
748446bb
MG
1/*
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>
76ab0f53 15#include <linux/sysctl.h>
ed4a6d7f 16#include <linux/sysfs.h>
748446bb
MG
17#include "internal.h"
18
b7aba698
MG
19#define CREATE_TRACE_POINTS
20#include <trace/events/compaction.h>
21
748446bb
MG
22/*
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 */
77f1fe6b 36 bool sync; /* Synchronous migration */
748446bb 37
aad6ec37 38 int order; /* order a direct compactor needs */
56de7263 39 int migratetype; /* MOVABLE, RECLAIMABLE etc */
748446bb
MG
40 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
85aa125f
MN
57/*
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)
748446bb 67{
b7aba698 68 int nr_scanned = 0, total_isolated = 0;
748446bb
MG
69 struct page *cursor;
70
748446bb
MG
71 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
85aa125f
MN
78 if (!pfn_valid_within(blockpfn)) {
79 if (strict)
80 return 0;
748446bb 81 continue;
85aa125f 82 }
b7aba698 83 nr_scanned++;
748446bb 84
85aa125f
MN
85 if (!PageBuddy(page)) {
86 if (strict)
87 return 0;
748446bb 88 continue;
85aa125f 89 }
748446bb
MG
90
91 /* Found a free page, break it into order-0 pages */
92 isolated = split_free_page(page);
85aa125f
MN
93 if (!isolated && strict)
94 return 0;
748446bb
MG
95 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
b7aba698 108 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
748446bb
MG
109 return total_isolated;
110}
111
85aa125f
MN
112/**
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
748446bb
MG
179/* 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
03d44192
MN
201static 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
748446bb
MG
211/*
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;
85aa125f 219 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
748446bb
MG
220 unsigned long flags;
221 int nr_freepages = cc->nr_freepages;
222 struct list_head *freelist = &cc->freepages;
223
7454f4ba
MG
224 /*
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 */
748446bb
MG
229 pfn = cc->free_pfn;
230 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
7454f4ba
MG
231
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);
748446bb 238
85aa125f
MN
239 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
240
748446bb
MG
241 /*
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 */
748446bb
MG
246 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
602605a4
MG
268 /*
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)) {
85aa125f
MN
277 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
278 isolated = isolate_freepages_block(pfn, end_pfn,
279 freelist, false);
602605a4
MG
280 nr_freepages += isolated;
281 }
282 spin_unlock_irqrestore(&zone->lock, flags);
748446bb
MG
283
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 }
748446bb
MG
292
293 /* split_free_page does not map the pages */
03d44192 294 map_pages(freelist);
748446bb
MG
295
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;
b9e84ac1 304 unsigned int count[2] = { 0, };
748446bb 305
b9e84ac1
MK
306 list_for_each_entry(page, &cc->migratepages, lru)
307 count[!!page_is_file_cache(page)]++;
748446bb 308
b9e84ac1
MK
309 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
310 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
748446bb
MG
311}
312
313/* Similar to reclaim, but different enough that they don't share logic */
314static bool too_many_isolated(struct zone *zone)
315{
bc693045 316 unsigned long active, inactive, isolated;
748446bb
MG
317
318 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
319 zone_page_state(zone, NR_INACTIVE_ANON);
bc693045
MK
320 active = zone_page_state(zone, NR_ACTIVE_FILE) +
321 zone_page_state(zone, NR_ACTIVE_ANON);
748446bb
MG
322 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
323 zone_page_state(zone, NR_ISOLATED_ANON);
324
bc693045 325 return isolated > (inactive + active) / 2;
748446bb
MG
326}
327
f9e35b3b
MG
328/* 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
2fe86e00
MN
335/**
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.
748446bb 353 */
2fe86e00
MN
354static unsigned long
355isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
356 unsigned long low_pfn, unsigned long end_pfn)
748446bb 357{
9927af74 358 unsigned long last_pageblock_nr = 0, pageblock_nr;
b7aba698 359 unsigned long nr_scanned = 0, nr_isolated = 0;
748446bb 360 struct list_head *migratelist = &cc->migratepages;
39deaf85 361 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
748446bb 362
748446bb
MG
363 /*
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))) {
f9e35b3b
MG
369 /* async migration should just abort */
370 if (!cc->sync)
2fe86e00 371 return 0;
f9e35b3b 372
748446bb
MG
373 congestion_wait(BLK_RW_ASYNC, HZ/10);
374
375 if (fatal_signal_pending(current))
2fe86e00 376 return 0;
748446bb
MG
377 }
378
379 /* Time to isolate some pages for migration */
b2eef8c0 380 cond_resched();
748446bb
MG
381 spin_lock_irq(&zone->lru_lock);
382 for (; low_pfn < end_pfn; low_pfn++) {
383 struct page *page;
b2eef8c0
AA
384 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
0bf380bc
MG
401 /*
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
748446bb
MG
414 if (!pfn_valid_within(low_pfn))
415 continue;
b7aba698 416 nr_scanned++;
748446bb 417
dc908600
MG
418 /*
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 */
748446bb 424 page = pfn_to_page(low_pfn);
dc908600
MG
425 if (page_zone(page) != zone)
426 continue;
427
428 /* Skip if free */
748446bb
MG
429 if (PageBuddy(page))
430 continue;
431
9927af74
MG
432 /*
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
bc835011
AA
446 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
c8244935
MG
459 if (!cc->sync)
460 mode |= ISOLATE_ASYNC_MIGRATE;
461
748446bb 462 /* Try isolate the page */
39deaf85 463 if (__isolate_lru_page(page, mode, 0) != 0)
748446bb
MG
464 continue;
465
bc835011
AA
466 VM_BUG_ON(PageTransCompound(page));
467
748446bb
MG
468 /* Successfully isolated */
469 del_page_from_lru_list(zone, page, page_lru(page));
470 list_add(&page->lru, migratelist);
748446bb 471 cc->nr_migratepages++;
b7aba698 472 nr_isolated++;
748446bb
MG
473
474 /* Avoid isolating too much */
31b8384a
HD
475 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
476 ++low_pfn;
748446bb 477 break;
31b8384a 478 }
748446bb
MG
479 }
480
481 acct_isolated(zone, cc);
482
483 spin_unlock_irq(&zone->lru_lock);
748446bb 484
b7aba698
MG
485 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
486
2fe86e00
MN
487 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
f9e35b3b 518 return ISOLATE_SUCCESS;
748446bb
MG
519}
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,
5a03b051 568 struct compact_control *cc)
748446bb 569{
56de7263 570 unsigned int order;
5a03b051 571 unsigned long watermark;
56de7263 572
748446bb
MG
573 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
82478fb7
JW
580 /*
581 * order == -1 is expected when compacting via
582 * /proc/sys/vm/compact_memory
583 */
56de7263
MG
584 if (cc->order == -1)
585 return COMPACT_CONTINUE;
586
3957c776
MH
587 /* 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
56de7263
MG
594 /* 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
748446bb
MG
605 return COMPACT_CONTINUE;
606}
607
3e7d3449
MG
608/*
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
3957c776
MH
620 /*
621 * order == -1 is expected when compacting via
622 * /proc/sys/vm/compact_memory
623 */
624 if (order == -1)
625 return COMPACT_CONTINUE;
626
3e7d3449
MG
627 /*
628 * 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 *
a582a738
SL
640 * index of -1000 implies allocations might succeed depending on
641 * watermarks
3e7d3449
MG
642 * 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
a582a738
SL
651 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
652 0, 0))
3e7d3449
MG
653 return COMPACT_PARTIAL;
654
655 return COMPACT_CONTINUE;
656}
657
748446bb
MG
658static int compact_zone(struct zone *zone, struct compact_control *cc)
659{
660 int ret;
661
3e7d3449
MG
662 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
748446bb
MG
673 /* 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;
9d502c1c 682 int err;
748446bb 683
f9e35b3b
MG
684 switch (isolate_migratepages(zone, cc)) {
685 case ISOLATE_ABORT:
686 ret = COMPACT_PARTIAL;
687 goto out;
688 case ISOLATE_NONE:
748446bb 689 continue;
f9e35b3b
MG
690 case ISOLATE_SUCCESS:
691 ;
692 }
748446bb
MG
693
694 nr_migrate = cc->nr_migratepages;
9d502c1c 695 err = migrate_pages(&cc->migratepages, compaction_alloc,
7f0f2496 696 (unsigned long)cc, false,
a6bc32b8 697 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
748446bb
MG
698 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);
b7aba698
MG
705 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
706 nr_remaining);
748446bb
MG
707
708 /* Release LRU pages not migrated */
9d502c1c 709 if (err) {
748446bb
MG
710 putback_lru_pages(&cc->migratepages);
711 cc->nr_migratepages = 0;
712 }
713
714 }
715
f9e35b3b 716out:
748446bb
MG
717 /* 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}
76ab0f53 723
d43a87e6 724static unsigned long compact_zone_order(struct zone *zone,
5a03b051 725 int order, gfp_t gfp_mask,
d527caf2 726 bool sync)
56de7263
MG
727{
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,
77f1fe6b 734 .sync = sync,
56de7263
MG
735 };
736 INIT_LIST_HEAD(&cc.freepages);
737 INIT_LIST_HEAD(&cc.migratepages);
738
739 return compact_zone(zone, &cc);
740}
741
5e771905
MG
742int sysctl_extfrag_threshold = 500;
743
56de7263
MG
744/**
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
77f1fe6b 750 * @sync: Whether migration is synchronous or not
56de7263
MG
751 *
752 * This is the main entry point for direct page compaction.
753 */
754unsigned long try_to_compact_pages(struct zonelist *zonelist,
77f1fe6b
MG
755 int order, gfp_t gfp_mask, nodemask_t *nodemask,
756 bool sync)
56de7263
MG
757{
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;
56de7263
MG
761 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 */
c5a73c3d 770 if (!order || !may_enter_fs || !may_perform_io)
56de7263
MG
771 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) {
56de7263
MG
778 int status;
779
d527caf2 780 status = compact_zone_order(zone, order, gfp_mask, sync);
56de7263
MG
781 rc = max(status, rc);
782
3e7d3449
MG
783 /* If a normal allocation would succeed, stop compacting */
784 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
56de7263
MG
785 break;
786 }
787
788 return rc;
789}
790
791
76ab0f53 792/* Compact all zones within a node */
7be62de9 793static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
76ab0f53
MG
794{
795 int zoneid;
76ab0f53
MG
796 struct zone *zone;
797
76ab0f53 798 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
76ab0f53
MG
799
800 zone = &pgdat->node_zones[zoneid];
801 if (!populated_zone(zone))
802 continue;
803
7be62de9
RR
804 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);
76ab0f53 809
aad6ec37 810 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
7be62de9 811 compact_zone(zone, cc);
76ab0f53 812
aff62249
RR
813 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
7be62de9
RR
823 VM_BUG_ON(!list_empty(&cc->freepages));
824 VM_BUG_ON(!list_empty(&cc->migratepages));
76ab0f53
MG
825 }
826
827 return 0;
828}
829
7be62de9
RR
830int 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{
7be62de9
RR
842 struct compact_control cc = {
843 .order = -1,
844 .sync = true,
845 };
846
8575ec29 847 return __compact_pgdat(NODE_DATA(nid), &cc);
7be62de9
RR
848}
849
76ab0f53
MG
850/* Compact all nodes in the system */
851static int compact_nodes(void)
852{
853 int nid;
854
8575ec29
HD
855 /* Flush pending updates to the LRU lists */
856 lru_add_drain_all();
857
76ab0f53
MG
858 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}
ed4a6d7f 876
5e771905
MG
877int 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
ed4a6d7f 885#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
10fbcf4c
KS
886ssize_t sysfs_compact_node(struct device *dev,
887 struct device_attribute *attr,
ed4a6d7f
MG
888 const char *buf, size_t count)
889{
8575ec29
HD
890 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 }
ed4a6d7f
MG
898
899 return count;
900}
10fbcf4c 901static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
ed4a6d7f
MG
902
903int compaction_register_node(struct node *node)
904{
10fbcf4c 905 return device_create_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
906}
907
908void compaction_unregister_node(struct node *node)
909{
10fbcf4c 910 return device_remove_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
911}
912#endif /* CONFIG_SYSFS && CONFIG_NUMA */
This page took 0.212426 seconds and 5 git commands to generate.