mm: page_alloc: change fallbacks array handling
[GitHub/exynos8895/android_kernel_samsung_universal8895.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
ff9543fd
MN
19#if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
b7aba698
MG
21#define CREATE_TRACE_POINTS
22#include <trace/events/compaction.h>
23
748446bb
MG
24static unsigned long release_freepages(struct list_head *freelist)
25{
26 struct page *page, *next;
27 unsigned long count = 0;
28
29 list_for_each_entry_safe(page, next, freelist, lru) {
30 list_del(&page->lru);
31 __free_page(page);
32 count++;
33 }
34
35 return count;
36}
37
ff9543fd
MN
38static void map_pages(struct list_head *list)
39{
40 struct page *page;
41
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
45 }
46}
47
85aa125f
MN
48/*
49 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
50 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
51 * pages inside of the pageblock (even though it may still end up isolating
52 * some pages).
53 */
54static unsigned long isolate_freepages_block(unsigned long blockpfn,
55 unsigned long end_pfn,
56 struct list_head *freelist,
57 bool strict)
748446bb 58{
b7aba698 59 int nr_scanned = 0, total_isolated = 0;
748446bb
MG
60 struct page *cursor;
61
748446bb
MG
62 cursor = pfn_to_page(blockpfn);
63
64 /* Isolate free pages. This assumes the block is valid */
65 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
66 int isolated, i;
67 struct page *page = cursor;
68
85aa125f
MN
69 if (!pfn_valid_within(blockpfn)) {
70 if (strict)
71 return 0;
748446bb 72 continue;
85aa125f 73 }
b7aba698 74 nr_scanned++;
748446bb 75
85aa125f
MN
76 if (!PageBuddy(page)) {
77 if (strict)
78 return 0;
748446bb 79 continue;
85aa125f 80 }
748446bb
MG
81
82 /* Found a free page, break it into order-0 pages */
83 isolated = split_free_page(page);
85aa125f
MN
84 if (!isolated && strict)
85 return 0;
748446bb
MG
86 total_isolated += isolated;
87 for (i = 0; i < isolated; i++) {
88 list_add(&page->lru, freelist);
89 page++;
90 }
91
92 /* If a page was split, advance to the end of it */
93 if (isolated) {
94 blockpfn += isolated - 1;
95 cursor += isolated - 1;
96 }
97 }
98
b7aba698 99 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
748446bb
MG
100 return total_isolated;
101}
102
85aa125f
MN
103/**
104 * isolate_freepages_range() - isolate free pages.
105 * @start_pfn: The first PFN to start isolating.
106 * @end_pfn: The one-past-last PFN.
107 *
108 * Non-free pages, invalid PFNs, or zone boundaries within the
109 * [start_pfn, end_pfn) range are considered errors, cause function to
110 * undo its actions and return zero.
111 *
112 * Otherwise, function returns one-past-the-last PFN of isolated page
113 * (which may be greater then end_pfn if end fell in a middle of
114 * a free page).
115 */
ff9543fd 116unsigned long
85aa125f
MN
117isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
118{
119 unsigned long isolated, pfn, block_end_pfn, flags;
120 struct zone *zone = NULL;
121 LIST_HEAD(freelist);
122
123 if (pfn_valid(start_pfn))
124 zone = page_zone(pfn_to_page(start_pfn));
125
126 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
127 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
128 break;
129
130 /*
131 * On subsequent iterations ALIGN() is actually not needed,
132 * but we keep it that we not to complicate the code.
133 */
134 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
135 block_end_pfn = min(block_end_pfn, end_pfn);
136
137 spin_lock_irqsave(&zone->lock, flags);
138 isolated = isolate_freepages_block(pfn, block_end_pfn,
139 &freelist, true);
140 spin_unlock_irqrestore(&zone->lock, flags);
141
142 /*
143 * In strict mode, isolate_freepages_block() returns 0 if
144 * there are any holes in the block (ie. invalid PFNs or
145 * non-free pages).
146 */
147 if (!isolated)
148 break;
149
150 /*
151 * If we managed to isolate pages, it is always (1 << n) *
152 * pageblock_nr_pages for some non-negative n. (Max order
153 * page may span two pageblocks).
154 */
155 }
156
157 /* split_free_page does not map the pages */
158 map_pages(&freelist);
159
160 if (pfn < end_pfn) {
161 /* Loop terminated early, cleanup. */
162 release_freepages(&freelist);
163 return 0;
164 }
165
166 /* We don't use freelists for anything. */
167 return pfn;
168}
169
748446bb
MG
170/* Update the number of anon and file isolated pages in the zone */
171static void acct_isolated(struct zone *zone, struct compact_control *cc)
172{
173 struct page *page;
b9e84ac1 174 unsigned int count[2] = { 0, };
748446bb 175
b9e84ac1
MK
176 list_for_each_entry(page, &cc->migratepages, lru)
177 count[!!page_is_file_cache(page)]++;
748446bb 178
b9e84ac1
MK
179 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
180 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
748446bb
MG
181}
182
183/* Similar to reclaim, but different enough that they don't share logic */
184static bool too_many_isolated(struct zone *zone)
185{
bc693045 186 unsigned long active, inactive, isolated;
748446bb
MG
187
188 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
189 zone_page_state(zone, NR_INACTIVE_ANON);
bc693045
MK
190 active = zone_page_state(zone, NR_ACTIVE_FILE) +
191 zone_page_state(zone, NR_ACTIVE_ANON);
748446bb
MG
192 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
193 zone_page_state(zone, NR_ISOLATED_ANON);
194
bc693045 195 return isolated > (inactive + active) / 2;
748446bb
MG
196}
197
2fe86e00
MN
198/**
199 * isolate_migratepages_range() - isolate all migrate-able pages in range.
200 * @zone: Zone pages are in.
201 * @cc: Compaction control structure.
202 * @low_pfn: The first PFN of the range.
203 * @end_pfn: The one-past-the-last PFN of the range.
204 *
205 * Isolate all pages that can be migrated from the range specified by
206 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
207 * pending), otherwise PFN of the first page that was not scanned
208 * (which may be both less, equal to or more then end_pfn).
209 *
210 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
211 * zero.
212 *
213 * Apart from cc->migratepages and cc->nr_migratetypes this function
214 * does not modify any cc's fields, in particular it does not modify
215 * (or read for that matter) cc->migrate_pfn.
748446bb 216 */
ff9543fd 217unsigned long
2fe86e00
MN
218isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
219 unsigned long low_pfn, unsigned long end_pfn)
748446bb 220{
9927af74 221 unsigned long last_pageblock_nr = 0, pageblock_nr;
b7aba698 222 unsigned long nr_scanned = 0, nr_isolated = 0;
748446bb 223 struct list_head *migratelist = &cc->migratepages;
39deaf85 224 isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
748446bb 225
748446bb
MG
226 /*
227 * Ensure that there are not too many pages isolated from the LRU
228 * list by either parallel reclaimers or compaction. If there are,
229 * delay for some time until fewer pages are isolated
230 */
231 while (unlikely(too_many_isolated(zone))) {
f9e35b3b
MG
232 /* async migration should just abort */
233 if (!cc->sync)
2fe86e00 234 return 0;
f9e35b3b 235
748446bb
MG
236 congestion_wait(BLK_RW_ASYNC, HZ/10);
237
238 if (fatal_signal_pending(current))
2fe86e00 239 return 0;
748446bb
MG
240 }
241
242 /* Time to isolate some pages for migration */
b2eef8c0 243 cond_resched();
748446bb
MG
244 spin_lock_irq(&zone->lru_lock);
245 for (; low_pfn < end_pfn; low_pfn++) {
246 struct page *page;
b2eef8c0
AA
247 bool locked = true;
248
249 /* give a chance to irqs before checking need_resched() */
250 if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) {
251 spin_unlock_irq(&zone->lru_lock);
252 locked = false;
253 }
254 if (need_resched() || spin_is_contended(&zone->lru_lock)) {
255 if (locked)
256 spin_unlock_irq(&zone->lru_lock);
257 cond_resched();
258 spin_lock_irq(&zone->lru_lock);
259 if (fatal_signal_pending(current))
260 break;
261 } else if (!locked)
262 spin_lock_irq(&zone->lru_lock);
263
0bf380bc
MG
264 /*
265 * migrate_pfn does not necessarily start aligned to a
266 * pageblock. Ensure that pfn_valid is called when moving
267 * into a new MAX_ORDER_NR_PAGES range in case of large
268 * memory holes within the zone
269 */
270 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
271 if (!pfn_valid(low_pfn)) {
272 low_pfn += MAX_ORDER_NR_PAGES - 1;
273 continue;
274 }
275 }
276
748446bb
MG
277 if (!pfn_valid_within(low_pfn))
278 continue;
b7aba698 279 nr_scanned++;
748446bb 280
dc908600
MG
281 /*
282 * Get the page and ensure the page is within the same zone.
283 * See the comment in isolate_freepages about overlapping
284 * nodes. It is deliberate that the new zone lock is not taken
285 * as memory compaction should not move pages between nodes.
286 */
748446bb 287 page = pfn_to_page(low_pfn);
dc908600
MG
288 if (page_zone(page) != zone)
289 continue;
290
291 /* Skip if free */
748446bb
MG
292 if (PageBuddy(page))
293 continue;
294
9927af74
MG
295 /*
296 * For async migration, also only scan in MOVABLE blocks. Async
297 * migration is optimistic to see if the minimum amount of work
298 * satisfies the allocation
299 */
300 pageblock_nr = low_pfn >> pageblock_order;
301 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
302 get_pageblock_migratetype(page) != MIGRATE_MOVABLE) {
303 low_pfn += pageblock_nr_pages;
304 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
305 last_pageblock_nr = pageblock_nr;
306 continue;
307 }
308
bc835011
AA
309 if (!PageLRU(page))
310 continue;
311
312 /*
313 * PageLRU is set, and lru_lock excludes isolation,
314 * splitting and collapsing (collapsing has already
315 * happened if PageLRU is set).
316 */
317 if (PageTransHuge(page)) {
318 low_pfn += (1 << compound_order(page)) - 1;
319 continue;
320 }
321
c8244935
MG
322 if (!cc->sync)
323 mode |= ISOLATE_ASYNC_MIGRATE;
324
748446bb 325 /* Try isolate the page */
39deaf85 326 if (__isolate_lru_page(page, mode, 0) != 0)
748446bb
MG
327 continue;
328
bc835011
AA
329 VM_BUG_ON(PageTransCompound(page));
330
748446bb
MG
331 /* Successfully isolated */
332 del_page_from_lru_list(zone, page, page_lru(page));
333 list_add(&page->lru, migratelist);
748446bb 334 cc->nr_migratepages++;
b7aba698 335 nr_isolated++;
748446bb
MG
336
337 /* Avoid isolating too much */
31b8384a
HD
338 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
339 ++low_pfn;
748446bb 340 break;
31b8384a 341 }
748446bb
MG
342 }
343
344 acct_isolated(zone, cc);
345
346 spin_unlock_irq(&zone->lru_lock);
748446bb 347
b7aba698
MG
348 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
349
2fe86e00
MN
350 return low_pfn;
351}
352
ff9543fd
MN
353#endif /* CONFIG_COMPACTION || CONFIG_CMA */
354#ifdef CONFIG_COMPACTION
355
356/* Returns true if the page is within a block suitable for migration to */
357static bool suitable_migration_target(struct page *page)
358{
359
360 int migratetype = get_pageblock_migratetype(page);
361
362 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
363 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
364 return false;
365
366 /* If the page is a large free page, then allow migration */
367 if (PageBuddy(page) && page_order(page) >= pageblock_order)
368 return true;
369
370 /* If the block is MIGRATE_MOVABLE, allow migration */
371 if (migratetype == MIGRATE_MOVABLE)
372 return true;
373
374 /* Otherwise skip the block */
375 return false;
376}
377
2fe86e00 378/*
ff9543fd
MN
379 * Based on information in the current compact_control, find blocks
380 * suitable for isolating free pages from and then isolate them.
2fe86e00 381 */
ff9543fd
MN
382static void isolate_freepages(struct zone *zone,
383 struct compact_control *cc)
2fe86e00 384{
ff9543fd
MN
385 struct page *page;
386 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
387 unsigned long flags;
388 int nr_freepages = cc->nr_freepages;
389 struct list_head *freelist = &cc->freepages;
2fe86e00 390
ff9543fd
MN
391 /*
392 * Initialise the free scanner. The starting point is where we last
393 * scanned from (or the end of the zone if starting). The low point
394 * is the end of the pageblock the migration scanner is using.
395 */
396 pfn = cc->free_pfn;
397 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
2fe86e00 398
ff9543fd
MN
399 /*
400 * Take care that if the migration scanner is at the end of the zone
401 * that the free scanner does not accidentally move to the next zone
402 * in the next isolation cycle.
403 */
404 high_pfn = min(low_pfn, pfn);
2fe86e00 405
ff9543fd 406 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
2fe86e00 407
ff9543fd
MN
408 /*
409 * Isolate free pages until enough are available to migrate the
410 * pages on cc->migratepages. We stop searching if the migrate
411 * and free page scanners meet or enough free pages are isolated.
412 */
413 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
414 pfn -= pageblock_nr_pages) {
415 unsigned long isolated;
2fe86e00 416
ff9543fd
MN
417 if (!pfn_valid(pfn))
418 continue;
2fe86e00 419
ff9543fd
MN
420 /*
421 * Check for overlapping nodes/zones. It's possible on some
422 * configurations to have a setup like
423 * node0 node1 node0
424 * i.e. it's possible that all pages within a zones range of
425 * pages do not belong to a single zone.
426 */
427 page = pfn_to_page(pfn);
428 if (page_zone(page) != zone)
429 continue;
430
431 /* Check the block is suitable for migration */
432 if (!suitable_migration_target(page))
433 continue;
434
435 /*
436 * Found a block suitable for isolating free pages from. Now
437 * we disabled interrupts, double check things are ok and
438 * isolate the pages. This is to minimise the time IRQs
439 * are disabled
440 */
441 isolated = 0;
442 spin_lock_irqsave(&zone->lock, flags);
443 if (suitable_migration_target(page)) {
444 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
445 isolated = isolate_freepages_block(pfn, end_pfn,
446 freelist, false);
447 nr_freepages += isolated;
448 }
449 spin_unlock_irqrestore(&zone->lock, flags);
450
451 /*
452 * Record the highest PFN we isolated pages from. When next
453 * looking for free pages, the search will restart here as
454 * page migration may have returned some pages to the allocator
455 */
456 if (isolated)
457 high_pfn = max(high_pfn, pfn);
458 }
459
460 /* split_free_page does not map the pages */
461 map_pages(freelist);
462
463 cc->free_pfn = high_pfn;
464 cc->nr_freepages = nr_freepages;
748446bb
MG
465}
466
467/*
468 * This is a migrate-callback that "allocates" freepages by taking pages
469 * from the isolated freelists in the block we are migrating to.
470 */
471static struct page *compaction_alloc(struct page *migratepage,
472 unsigned long data,
473 int **result)
474{
475 struct compact_control *cc = (struct compact_control *)data;
476 struct page *freepage;
477
478 /* Isolate free pages if necessary */
479 if (list_empty(&cc->freepages)) {
480 isolate_freepages(cc->zone, cc);
481
482 if (list_empty(&cc->freepages))
483 return NULL;
484 }
485
486 freepage = list_entry(cc->freepages.next, struct page, lru);
487 list_del(&freepage->lru);
488 cc->nr_freepages--;
489
490 return freepage;
491}
492
493/*
494 * We cannot control nr_migratepages and nr_freepages fully when migration is
495 * running as migrate_pages() has no knowledge of compact_control. When
496 * migration is complete, we count the number of pages on the lists by hand.
497 */
498static void update_nr_listpages(struct compact_control *cc)
499{
500 int nr_migratepages = 0;
501 int nr_freepages = 0;
502 struct page *page;
503
504 list_for_each_entry(page, &cc->migratepages, lru)
505 nr_migratepages++;
506 list_for_each_entry(page, &cc->freepages, lru)
507 nr_freepages++;
508
509 cc->nr_migratepages = nr_migratepages;
510 cc->nr_freepages = nr_freepages;
511}
512
ff9543fd
MN
513/* possible outcome of isolate_migratepages */
514typedef enum {
515 ISOLATE_ABORT, /* Abort compaction now */
516 ISOLATE_NONE, /* No pages isolated, continue scanning */
517 ISOLATE_SUCCESS, /* Pages isolated, migrate */
518} isolate_migrate_t;
519
520/*
521 * Isolate all pages that can be migrated from the block pointed to by
522 * the migrate scanner within compact_control.
523 */
524static isolate_migrate_t isolate_migratepages(struct zone *zone,
525 struct compact_control *cc)
526{
527 unsigned long low_pfn, end_pfn;
528
529 /* Do not scan outside zone boundaries */
530 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
531
532 /* Only scan within a pageblock boundary */
533 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
534
535 /* Do not cross the free scanner or scan within a memory hole */
536 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
537 cc->migrate_pfn = end_pfn;
538 return ISOLATE_NONE;
539 }
540
541 /* Perform the isolation */
542 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
543 if (!low_pfn)
544 return ISOLATE_ABORT;
545
546 cc->migrate_pfn = low_pfn;
547
548 return ISOLATE_SUCCESS;
549}
550
748446bb 551static int compact_finished(struct zone *zone,
5a03b051 552 struct compact_control *cc)
748446bb 553{
56de7263 554 unsigned int order;
5a03b051 555 unsigned long watermark;
56de7263 556
748446bb
MG
557 if (fatal_signal_pending(current))
558 return COMPACT_PARTIAL;
559
560 /* Compaction run completes if the migrate and free scanner meet */
561 if (cc->free_pfn <= cc->migrate_pfn)
562 return COMPACT_COMPLETE;
563
82478fb7
JW
564 /*
565 * order == -1 is expected when compacting via
566 * /proc/sys/vm/compact_memory
567 */
56de7263
MG
568 if (cc->order == -1)
569 return COMPACT_CONTINUE;
570
3957c776
MH
571 /* Compaction run is not finished if the watermark is not met */
572 watermark = low_wmark_pages(zone);
573 watermark += (1 << cc->order);
574
575 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
576 return COMPACT_CONTINUE;
577
56de7263
MG
578 /* Direct compactor: Is a suitable page free? */
579 for (order = cc->order; order < MAX_ORDER; order++) {
580 /* Job done if page is free of the right migratetype */
581 if (!list_empty(&zone->free_area[order].free_list[cc->migratetype]))
582 return COMPACT_PARTIAL;
583
584 /* Job done if allocation would set block type */
585 if (order >= pageblock_order && zone->free_area[order].nr_free)
586 return COMPACT_PARTIAL;
587 }
588
748446bb
MG
589 return COMPACT_CONTINUE;
590}
591
3e7d3449
MG
592/*
593 * compaction_suitable: Is this suitable to run compaction on this zone now?
594 * Returns
595 * COMPACT_SKIPPED - If there are too few free pages for compaction
596 * COMPACT_PARTIAL - If the allocation would succeed without compaction
597 * COMPACT_CONTINUE - If compaction should run now
598 */
599unsigned long compaction_suitable(struct zone *zone, int order)
600{
601 int fragindex;
602 unsigned long watermark;
603
3957c776
MH
604 /*
605 * order == -1 is expected when compacting via
606 * /proc/sys/vm/compact_memory
607 */
608 if (order == -1)
609 return COMPACT_CONTINUE;
610
3e7d3449
MG
611 /*
612 * Watermarks for order-0 must be met for compaction. Note the 2UL.
613 * This is because during migration, copies of pages need to be
614 * allocated and for a short time, the footprint is higher
615 */
616 watermark = low_wmark_pages(zone) + (2UL << order);
617 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
618 return COMPACT_SKIPPED;
619
620 /*
621 * fragmentation index determines if allocation failures are due to
622 * low memory or external fragmentation
623 *
a582a738
SL
624 * index of -1000 implies allocations might succeed depending on
625 * watermarks
3e7d3449
MG
626 * index towards 0 implies failure is due to lack of memory
627 * index towards 1000 implies failure is due to fragmentation
628 *
629 * Only compact if a failure would be due to fragmentation.
630 */
631 fragindex = fragmentation_index(zone, order);
632 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
633 return COMPACT_SKIPPED;
634
a582a738
SL
635 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
636 0, 0))
3e7d3449
MG
637 return COMPACT_PARTIAL;
638
639 return COMPACT_CONTINUE;
640}
641
748446bb
MG
642static int compact_zone(struct zone *zone, struct compact_control *cc)
643{
644 int ret;
645
3e7d3449
MG
646 ret = compaction_suitable(zone, cc->order);
647 switch (ret) {
648 case COMPACT_PARTIAL:
649 case COMPACT_SKIPPED:
650 /* Compaction is likely to fail */
651 return ret;
652 case COMPACT_CONTINUE:
653 /* Fall through to compaction */
654 ;
655 }
656
748446bb
MG
657 /* Setup to move all movable pages to the end of the zone */
658 cc->migrate_pfn = zone->zone_start_pfn;
659 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
660 cc->free_pfn &= ~(pageblock_nr_pages-1);
661
662 migrate_prep_local();
663
664 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
665 unsigned long nr_migrate, nr_remaining;
9d502c1c 666 int err;
748446bb 667
f9e35b3b
MG
668 switch (isolate_migratepages(zone, cc)) {
669 case ISOLATE_ABORT:
670 ret = COMPACT_PARTIAL;
671 goto out;
672 case ISOLATE_NONE:
748446bb 673 continue;
f9e35b3b
MG
674 case ISOLATE_SUCCESS:
675 ;
676 }
748446bb
MG
677
678 nr_migrate = cc->nr_migratepages;
9d502c1c 679 err = migrate_pages(&cc->migratepages, compaction_alloc,
7f0f2496 680 (unsigned long)cc, false,
a6bc32b8 681 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
748446bb
MG
682 update_nr_listpages(cc);
683 nr_remaining = cc->nr_migratepages;
684
685 count_vm_event(COMPACTBLOCKS);
686 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
687 if (nr_remaining)
688 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
b7aba698
MG
689 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
690 nr_remaining);
748446bb
MG
691
692 /* Release LRU pages not migrated */
9d502c1c 693 if (err) {
748446bb
MG
694 putback_lru_pages(&cc->migratepages);
695 cc->nr_migratepages = 0;
696 }
697
698 }
699
f9e35b3b 700out:
748446bb
MG
701 /* Release free pages and check accounting */
702 cc->nr_freepages -= release_freepages(&cc->freepages);
703 VM_BUG_ON(cc->nr_freepages != 0);
704
705 return ret;
706}
76ab0f53 707
d43a87e6 708static unsigned long compact_zone_order(struct zone *zone,
5a03b051 709 int order, gfp_t gfp_mask,
d527caf2 710 bool sync)
56de7263
MG
711{
712 struct compact_control cc = {
713 .nr_freepages = 0,
714 .nr_migratepages = 0,
715 .order = order,
716 .migratetype = allocflags_to_migratetype(gfp_mask),
717 .zone = zone,
77f1fe6b 718 .sync = sync,
56de7263
MG
719 };
720 INIT_LIST_HEAD(&cc.freepages);
721 INIT_LIST_HEAD(&cc.migratepages);
722
723 return compact_zone(zone, &cc);
724}
725
5e771905
MG
726int sysctl_extfrag_threshold = 500;
727
56de7263
MG
728/**
729 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
730 * @zonelist: The zonelist used for the current allocation
731 * @order: The order of the current allocation
732 * @gfp_mask: The GFP mask of the current allocation
733 * @nodemask: The allowed nodes to allocate from
77f1fe6b 734 * @sync: Whether migration is synchronous or not
56de7263
MG
735 *
736 * This is the main entry point for direct page compaction.
737 */
738unsigned long try_to_compact_pages(struct zonelist *zonelist,
77f1fe6b
MG
739 int order, gfp_t gfp_mask, nodemask_t *nodemask,
740 bool sync)
56de7263
MG
741{
742 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
743 int may_enter_fs = gfp_mask & __GFP_FS;
744 int may_perform_io = gfp_mask & __GFP_IO;
56de7263
MG
745 struct zoneref *z;
746 struct zone *zone;
747 int rc = COMPACT_SKIPPED;
748
749 /*
750 * Check whether it is worth even starting compaction. The order check is
751 * made because an assumption is made that the page allocator can satisfy
752 * the "cheaper" orders without taking special steps
753 */
c5a73c3d 754 if (!order || !may_enter_fs || !may_perform_io)
56de7263
MG
755 return rc;
756
757 count_vm_event(COMPACTSTALL);
758
759 /* Compact each zone in the list */
760 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
761 nodemask) {
56de7263
MG
762 int status;
763
d527caf2 764 status = compact_zone_order(zone, order, gfp_mask, sync);
56de7263
MG
765 rc = max(status, rc);
766
3e7d3449
MG
767 /* If a normal allocation would succeed, stop compacting */
768 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
56de7263
MG
769 break;
770 }
771
772 return rc;
773}
774
775
76ab0f53 776/* Compact all zones within a node */
7be62de9 777static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
76ab0f53
MG
778{
779 int zoneid;
76ab0f53
MG
780 struct zone *zone;
781
76ab0f53 782 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
76ab0f53
MG
783
784 zone = &pgdat->node_zones[zoneid];
785 if (!populated_zone(zone))
786 continue;
787
7be62de9
RR
788 cc->nr_freepages = 0;
789 cc->nr_migratepages = 0;
790 cc->zone = zone;
791 INIT_LIST_HEAD(&cc->freepages);
792 INIT_LIST_HEAD(&cc->migratepages);
76ab0f53 793
aad6ec37 794 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
7be62de9 795 compact_zone(zone, cc);
76ab0f53 796
aff62249
RR
797 if (cc->order > 0) {
798 int ok = zone_watermark_ok(zone, cc->order,
799 low_wmark_pages(zone), 0, 0);
800 if (ok && cc->order > zone->compact_order_failed)
801 zone->compact_order_failed = cc->order + 1;
802 /* Currently async compaction is never deferred. */
803 else if (!ok && cc->sync)
804 defer_compaction(zone, cc->order);
805 }
806
7be62de9
RR
807 VM_BUG_ON(!list_empty(&cc->freepages));
808 VM_BUG_ON(!list_empty(&cc->migratepages));
76ab0f53
MG
809 }
810
811 return 0;
812}
813
7be62de9
RR
814int compact_pgdat(pg_data_t *pgdat, int order)
815{
816 struct compact_control cc = {
817 .order = order,
818 .sync = false,
819 };
820
821 return __compact_pgdat(pgdat, &cc);
822}
823
824static int compact_node(int nid)
825{
7be62de9
RR
826 struct compact_control cc = {
827 .order = -1,
828 .sync = true,
829 };
830
8575ec29 831 return __compact_pgdat(NODE_DATA(nid), &cc);
7be62de9
RR
832}
833
76ab0f53
MG
834/* Compact all nodes in the system */
835static int compact_nodes(void)
836{
837 int nid;
838
8575ec29
HD
839 /* Flush pending updates to the LRU lists */
840 lru_add_drain_all();
841
76ab0f53
MG
842 for_each_online_node(nid)
843 compact_node(nid);
844
845 return COMPACT_COMPLETE;
846}
847
848/* The written value is actually unused, all memory is compacted */
849int sysctl_compact_memory;
850
851/* This is the entry point for compacting all nodes via /proc/sys/vm */
852int sysctl_compaction_handler(struct ctl_table *table, int write,
853 void __user *buffer, size_t *length, loff_t *ppos)
854{
855 if (write)
856 return compact_nodes();
857
858 return 0;
859}
ed4a6d7f 860
5e771905
MG
861int sysctl_extfrag_handler(struct ctl_table *table, int write,
862 void __user *buffer, size_t *length, loff_t *ppos)
863{
864 proc_dointvec_minmax(table, write, buffer, length, ppos);
865
866 return 0;
867}
868
ed4a6d7f 869#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
10fbcf4c
KS
870ssize_t sysfs_compact_node(struct device *dev,
871 struct device_attribute *attr,
ed4a6d7f
MG
872 const char *buf, size_t count)
873{
8575ec29
HD
874 int nid = dev->id;
875
876 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
877 /* Flush pending updates to the LRU lists */
878 lru_add_drain_all();
879
880 compact_node(nid);
881 }
ed4a6d7f
MG
882
883 return count;
884}
10fbcf4c 885static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
ed4a6d7f
MG
886
887int compaction_register_node(struct node *node)
888{
10fbcf4c 889 return device_create_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
890}
891
892void compaction_unregister_node(struct node *node)
893{
10fbcf4c 894 return device_remove_file(&node->dev, &dev_attr_compact);
ed4a6d7f
MG
895}
896#endif /* CONFIG_SYSFS && CONFIG_NUMA */
ff9543fd
MN
897
898#endif /* CONFIG_COMPACTION */