mm: compaction: introduce isolate_freepages_range()
[pandora-kernel.git] / mm / compaction.c
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>
15 #include <linux/sysctl.h>
16 #include <linux/sysfs.h>
17 #include "internal.h"
18
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/compaction.h>
21
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  */
29 struct 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 */
36         bool sync;                      /* Synchronous migration */
37
38         unsigned int order;             /* order a direct compactor needs */
39         int migratetype;                /* MOVABLE, RECLAIMABLE etc */
40         struct zone *zone;
41 };
42
43 static 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 /*
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  */
63 static unsigned long isolate_freepages_block(unsigned long blockpfn,
64                                 unsigned long end_pfn,
65                                 struct list_head *freelist,
66                                 bool strict)
67 {
68         int nr_scanned = 0, total_isolated = 0;
69         struct page *cursor;
70
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
78                 if (!pfn_valid_within(blockpfn)) {
79                         if (strict)
80                                 return 0;
81                         continue;
82                 }
83                 nr_scanned++;
84
85                 if (!PageBuddy(page)) {
86                         if (strict)
87                                 return 0;
88                         continue;
89                 }
90
91                 /* Found a free page, break it into order-0 pages */
92                 isolated = split_free_page(page);
93                 if (!isolated && strict)
94                         return 0;
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
108         trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
109         return total_isolated;
110 }
111
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  */
125 static unsigned long
126 isolate_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
179 /* Returns true if the page is within a block suitable for migration to */
180 static 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
201 static 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
211 /*
212  * Based on information in the current compact_control, find blocks
213  * suitable for isolating free pages from and then isolate them.
214  */
215 static void isolate_freepages(struct zone *zone,
216                                 struct compact_control *cc)
217 {
218         struct page *page;
219         unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
220         unsigned long flags;
221         int nr_freepages = cc->nr_freepages;
222         struct list_head *freelist = &cc->freepages;
223
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          */
229         pfn = cc->free_pfn;
230         low_pfn = cc->migrate_pfn + pageblock_nr_pages;
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);
238
239         zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
240
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          */
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
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)) {
277                         end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
278                         isolated = isolate_freepages_block(pfn, end_pfn,
279                                                            freelist, false);
280                         nr_freepages += isolated;
281                 }
282                 spin_unlock_irqrestore(&zone->lock, flags);
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         }
292
293         /* split_free_page does not map the pages */
294         map_pages(freelist);
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 */
301 static void acct_isolated(struct zone *zone, struct compact_control *cc)
302 {
303         struct page *page;
304         unsigned int count[2] = { 0, };
305
306         list_for_each_entry(page, &cc->migratepages, lru)
307                 count[!!page_is_file_cache(page)]++;
308
309         __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
310         __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
311 }
312
313 /* Similar to reclaim, but different enough that they don't share logic */
314 static bool too_many_isolated(struct zone *zone)
315 {
316         unsigned long active, inactive, isolated;
317
318         inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
319                                         zone_page_state(zone, NR_INACTIVE_ANON);
320         active = zone_page_state(zone, NR_ACTIVE_FILE) +
321                                         zone_page_state(zone, NR_ACTIVE_ANON);
322         isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
323                                         zone_page_state(zone, NR_ISOLATED_ANON);
324
325         return isolated > (inactive + active) / 2;
326 }
327
328 /* possible outcome of isolate_migratepages */
329 typedef 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
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.
353  */
354 static unsigned long
355 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
356                            unsigned long low_pfn, unsigned long end_pfn)
357 {
358         unsigned long last_pageblock_nr = 0, pageblock_nr;
359         unsigned long nr_scanned = 0, nr_isolated = 0;
360         struct list_head *migratelist = &cc->migratepages;
361         isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE;
362
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))) {
369                 /* async migration should just abort */
370                 if (!cc->sync)
371                         return 0;
372
373                 congestion_wait(BLK_RW_ASYNC, HZ/10);
374
375                 if (fatal_signal_pending(current))
376                         return 0;
377         }
378
379         /* Time to isolate some pages for migration */
380         cond_resched();
381         spin_lock_irq(&zone->lru_lock);
382         for (; low_pfn < end_pfn; low_pfn++) {
383                 struct page *page;
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
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
414                 if (!pfn_valid_within(low_pfn))
415                         continue;
416                 nr_scanned++;
417
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                  */
424                 page = pfn_to_page(low_pfn);
425                 if (page_zone(page) != zone)
426                         continue;
427
428                 /* Skip if free */
429                 if (PageBuddy(page))
430                         continue;
431
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
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
459                 if (!cc->sync)
460                         mode |= ISOLATE_ASYNC_MIGRATE;
461
462                 /* Try isolate the page */
463                 if (__isolate_lru_page(page, mode, 0) != 0)
464                         continue;
465
466                 VM_BUG_ON(PageTransCompound(page));
467
468                 /* Successfully isolated */
469                 del_page_from_lru_list(zone, page, page_lru(page));
470                 list_add(&page->lru, migratelist);
471                 cc->nr_migratepages++;
472                 nr_isolated++;
473
474                 /* Avoid isolating too much */
475                 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
476                         ++low_pfn;
477                         break;
478                 }
479         }
480
481         acct_isolated(zone, cc);
482
483         spin_unlock_irq(&zone->lru_lock);
484
485         trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
486
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  */
494 static 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
518         return ISOLATE_SUCCESS;
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  */
525 static 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  */
552 static 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
567 static int compact_finished(struct zone *zone,
568                             struct compact_control *cc)
569 {
570         unsigned int order;
571         unsigned long watermark;
572
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
580         /*
581          * order == -1 is expected when compacting via
582          * /proc/sys/vm/compact_memory
583          */
584         if (cc->order == -1)
585                 return COMPACT_CONTINUE;
586
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
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
605         return COMPACT_CONTINUE;
606 }
607
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  */
615 unsigned long compaction_suitable(struct zone *zone, int order)
616 {
617         int fragindex;
618         unsigned long watermark;
619
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
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          *
640          * index of -1000 implies allocations might succeed depending on
641          * watermarks
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
651         if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
652             0, 0))
653                 return COMPACT_PARTIAL;
654
655         return COMPACT_CONTINUE;
656 }
657
658 static int compact_zone(struct zone *zone, struct compact_control *cc)
659 {
660         int ret;
661
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
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;
682                 int err;
683
684                 switch (isolate_migratepages(zone, cc)) {
685                 case ISOLATE_ABORT:
686                         ret = COMPACT_PARTIAL;
687                         goto out;
688                 case ISOLATE_NONE:
689                         continue;
690                 case ISOLATE_SUCCESS:
691                         ;
692                 }
693
694                 nr_migrate = cc->nr_migratepages;
695                 err = migrate_pages(&cc->migratepages, compaction_alloc,
696                                 (unsigned long)cc, false,
697                                 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
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);
705                 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
706                                                 nr_remaining);
707
708                 /* Release LRU pages not migrated */
709                 if (err) {
710                         putback_lru_pages(&cc->migratepages);
711                         cc->nr_migratepages = 0;
712                         if (err == -ENOMEM) {
713                                 ret = COMPACT_PARTIAL;
714                                 goto out;
715                         }
716                 }
717         }
718
719 out:
720         /* Release free pages and check accounting */
721         cc->nr_freepages -= release_freepages(&cc->freepages);
722         VM_BUG_ON(cc->nr_freepages != 0);
723
724         return ret;
725 }
726
727 static unsigned long compact_zone_order(struct zone *zone,
728                                  int order, gfp_t gfp_mask,
729                                  bool sync)
730 {
731         struct compact_control cc = {
732                 .nr_freepages = 0,
733                 .nr_migratepages = 0,
734                 .order = order,
735                 .migratetype = allocflags_to_migratetype(gfp_mask),
736                 .zone = zone,
737                 .sync = sync,
738         };
739         INIT_LIST_HEAD(&cc.freepages);
740         INIT_LIST_HEAD(&cc.migratepages);
741
742         return compact_zone(zone, &cc);
743 }
744
745 int sysctl_extfrag_threshold = 500;
746
747 /**
748  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
749  * @zonelist: The zonelist used for the current allocation
750  * @order: The order of the current allocation
751  * @gfp_mask: The GFP mask of the current allocation
752  * @nodemask: The allowed nodes to allocate from
753  * @sync: Whether migration is synchronous or not
754  *
755  * This is the main entry point for direct page compaction.
756  */
757 unsigned long try_to_compact_pages(struct zonelist *zonelist,
758                         int order, gfp_t gfp_mask, nodemask_t *nodemask,
759                         bool sync)
760 {
761         enum zone_type high_zoneidx = gfp_zone(gfp_mask);
762         int may_enter_fs = gfp_mask & __GFP_FS;
763         int may_perform_io = gfp_mask & __GFP_IO;
764         struct zoneref *z;
765         struct zone *zone;
766         int rc = COMPACT_SKIPPED;
767
768         /*
769          * Check whether it is worth even starting compaction. The order check is
770          * made because an assumption is made that the page allocator can satisfy
771          * the "cheaper" orders without taking special steps
772          */
773         if (!order || !may_enter_fs || !may_perform_io)
774                 return rc;
775
776         count_vm_event(COMPACTSTALL);
777
778         /* Compact each zone in the list */
779         for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
780                                                                 nodemask) {
781                 int status;
782
783                 status = compact_zone_order(zone, order, gfp_mask, sync);
784                 rc = max(status, rc);
785
786                 /* If a normal allocation would succeed, stop compacting */
787                 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0))
788                         break;
789         }
790
791         return rc;
792 }
793
794
795 /* Compact all zones within a node */
796 static int compact_node(int nid)
797 {
798         int zoneid;
799         pg_data_t *pgdat;
800         struct zone *zone;
801
802         if (nid < 0 || nid >= nr_node_ids || !node_online(nid))
803                 return -EINVAL;
804         pgdat = NODE_DATA(nid);
805
806         /* Flush pending updates to the LRU lists */
807         lru_add_drain_all();
808
809         for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
810                 struct compact_control cc = {
811                         .nr_freepages = 0,
812                         .nr_migratepages = 0,
813                         .order = -1,
814                 };
815
816                 zone = &pgdat->node_zones[zoneid];
817                 if (!populated_zone(zone))
818                         continue;
819
820                 cc.zone = zone;
821                 INIT_LIST_HEAD(&cc.freepages);
822                 INIT_LIST_HEAD(&cc.migratepages);
823
824                 compact_zone(zone, &cc);
825
826                 VM_BUG_ON(!list_empty(&cc.freepages));
827                 VM_BUG_ON(!list_empty(&cc.migratepages));
828         }
829
830         return 0;
831 }
832
833 /* Compact all nodes in the system */
834 static void compact_nodes(void)
835 {
836         int nid;
837
838         for_each_online_node(nid)
839                 compact_node(nid);
840 }
841
842 /* The written value is actually unused, all memory is compacted */
843 int sysctl_compact_memory;
844
845 /* This is the entry point for compacting all nodes via /proc/sys/vm */
846 int sysctl_compaction_handler(struct ctl_table *table, int write,
847                         void __user *buffer, size_t *length, loff_t *ppos)
848 {
849         if (write)
850                 compact_nodes();
851
852         return 0;
853 }
854
855 int sysctl_extfrag_handler(struct ctl_table *table, int write,
856                         void __user *buffer, size_t *length, loff_t *ppos)
857 {
858         proc_dointvec_minmax(table, write, buffer, length, ppos);
859
860         return 0;
861 }
862
863 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
864 ssize_t sysfs_compact_node(struct device *dev,
865                         struct device_attribute *attr,
866                         const char *buf, size_t count)
867 {
868         compact_node(dev->id);
869
870         return count;
871 }
872 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
873
874 int compaction_register_node(struct node *node)
875 {
876         return device_create_file(&node->dev, &dev_attr_compact);
877 }
878
879 void compaction_unregister_node(struct node *node)
880 {
881         return device_remove_file(&node->dev, &dev_attr_compact);
882 }
883 #endif /* CONFIG_SYSFS && CONFIG_NUMA */