mm: compaction: acquire the zone->lru_lock as late as possible
[platform/adaptation/renesas_rcar/renesas_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 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
20
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/compaction.h>
23
24 static 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
38 static 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
48 static inline bool migrate_async_suitable(int migratetype)
49 {
50         return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
51 }
52
53 static inline bool should_release_lock(spinlock_t *lock)
54 {
55         return need_resched() || spin_is_contended(lock);
56 }
57
58 /*
59  * Compaction requires the taking of some coarse locks that are potentially
60  * very heavily contended. Check if the process needs to be scheduled or
61  * if the lock is contended. For async compaction, back out in the event
62  * if contention is severe. For sync compaction, schedule.
63  *
64  * Returns true if the lock is held.
65  * Returns false if the lock is released and compaction should abort
66  */
67 static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
68                                       bool locked, struct compact_control *cc)
69 {
70         if (should_release_lock(lock)) {
71                 if (locked) {
72                         spin_unlock_irqrestore(lock, *flags);
73                         locked = false;
74                 }
75
76                 /* async aborts if taking too long or contended */
77                 if (!cc->sync) {
78                         cc->contended = true;
79                         return false;
80                 }
81
82                 cond_resched();
83         }
84
85         if (!locked)
86                 spin_lock_irqsave(lock, *flags);
87         return true;
88 }
89
90 static inline bool compact_trylock_irqsave(spinlock_t *lock,
91                         unsigned long *flags, struct compact_control *cc)
92 {
93         return compact_checklock_irqsave(lock, flags, false, cc);
94 }
95
96 static void compact_capture_page(struct compact_control *cc)
97 {
98         unsigned long flags;
99         int mtype, mtype_low, mtype_high;
100
101         if (!cc->page || *cc->page)
102                 return;
103
104         /*
105          * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
106          * regardless of the migratetype of the freelist is is captured from.
107          * This is fine because the order for a high-order MIGRATE_MOVABLE
108          * allocation is typically at least a pageblock size and overall
109          * fragmentation is not impaired. Other allocation types must
110          * capture pages from their own migratelist because otherwise they
111          * could pollute other pageblocks like MIGRATE_MOVABLE with
112          * difficult to move pages and making fragmentation worse overall.
113          */
114         if (cc->migratetype == MIGRATE_MOVABLE) {
115                 mtype_low = 0;
116                 mtype_high = MIGRATE_PCPTYPES;
117         } else {
118                 mtype_low = cc->migratetype;
119                 mtype_high = cc->migratetype + 1;
120         }
121
122         /* Speculatively examine the free lists without zone lock */
123         for (mtype = mtype_low; mtype < mtype_high; mtype++) {
124                 int order;
125                 for (order = cc->order; order < MAX_ORDER; order++) {
126                         struct page *page;
127                         struct free_area *area;
128                         area = &(cc->zone->free_area[order]);
129                         if (list_empty(&area->free_list[mtype]))
130                                 continue;
131
132                         /* Take the lock and attempt capture of the page */
133                         if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
134                                 return;
135                         if (!list_empty(&area->free_list[mtype])) {
136                                 page = list_entry(area->free_list[mtype].next,
137                                                         struct page, lru);
138                                 if (capture_free_page(page, cc->order, mtype)) {
139                                         spin_unlock_irqrestore(&cc->zone->lock,
140                                                                         flags);
141                                         *cc->page = page;
142                                         return;
143                                 }
144                         }
145                         spin_unlock_irqrestore(&cc->zone->lock, flags);
146                 }
147         }
148 }
149
150 /*
151  * Isolate free pages onto a private freelist. Caller must hold zone->lock.
152  * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
153  * pages inside of the pageblock (even though it may still end up isolating
154  * some pages).
155  */
156 static unsigned long isolate_freepages_block(unsigned long blockpfn,
157                                 unsigned long end_pfn,
158                                 struct list_head *freelist,
159                                 bool strict)
160 {
161         int nr_scanned = 0, total_isolated = 0;
162         struct page *cursor;
163
164         cursor = pfn_to_page(blockpfn);
165
166         /* Isolate free pages. This assumes the block is valid */
167         for (; blockpfn < end_pfn; blockpfn++, cursor++) {
168                 int isolated, i;
169                 struct page *page = cursor;
170
171                 if (!pfn_valid_within(blockpfn)) {
172                         if (strict)
173                                 return 0;
174                         continue;
175                 }
176                 nr_scanned++;
177
178                 if (!PageBuddy(page)) {
179                         if (strict)
180                                 return 0;
181                         continue;
182                 }
183
184                 /* Found a free page, break it into order-0 pages */
185                 isolated = split_free_page(page);
186                 if (!isolated && strict)
187                         return 0;
188                 total_isolated += isolated;
189                 for (i = 0; i < isolated; i++) {
190                         list_add(&page->lru, freelist);
191                         page++;
192                 }
193
194                 /* If a page was split, advance to the end of it */
195                 if (isolated) {
196                         blockpfn += isolated - 1;
197                         cursor += isolated - 1;
198                 }
199         }
200
201         trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
202         return total_isolated;
203 }
204
205 /**
206  * isolate_freepages_range() - isolate free pages.
207  * @start_pfn: The first PFN to start isolating.
208  * @end_pfn:   The one-past-last PFN.
209  *
210  * Non-free pages, invalid PFNs, or zone boundaries within the
211  * [start_pfn, end_pfn) range are considered errors, cause function to
212  * undo its actions and return zero.
213  *
214  * Otherwise, function returns one-past-the-last PFN of isolated page
215  * (which may be greater then end_pfn if end fell in a middle of
216  * a free page).
217  */
218 unsigned long
219 isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
220 {
221         unsigned long isolated, pfn, block_end_pfn, flags;
222         struct zone *zone = NULL;
223         LIST_HEAD(freelist);
224
225         if (pfn_valid(start_pfn))
226                 zone = page_zone(pfn_to_page(start_pfn));
227
228         for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
229                 if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
230                         break;
231
232                 /*
233                  * On subsequent iterations ALIGN() is actually not needed,
234                  * but we keep it that we not to complicate the code.
235                  */
236                 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
237                 block_end_pfn = min(block_end_pfn, end_pfn);
238
239                 spin_lock_irqsave(&zone->lock, flags);
240                 isolated = isolate_freepages_block(pfn, block_end_pfn,
241                                                    &freelist, true);
242                 spin_unlock_irqrestore(&zone->lock, flags);
243
244                 /*
245                  * In strict mode, isolate_freepages_block() returns 0 if
246                  * there are any holes in the block (ie. invalid PFNs or
247                  * non-free pages).
248                  */
249                 if (!isolated)
250                         break;
251
252                 /*
253                  * If we managed to isolate pages, it is always (1 << n) *
254                  * pageblock_nr_pages for some non-negative n.  (Max order
255                  * page may span two pageblocks).
256                  */
257         }
258
259         /* split_free_page does not map the pages */
260         map_pages(&freelist);
261
262         if (pfn < end_pfn) {
263                 /* Loop terminated early, cleanup. */
264                 release_freepages(&freelist);
265                 return 0;
266         }
267
268         /* We don't use freelists for anything. */
269         return pfn;
270 }
271
272 /* Update the number of anon and file isolated pages in the zone */
273 static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
274 {
275         struct page *page;
276         unsigned int count[2] = { 0, };
277
278         list_for_each_entry(page, &cc->migratepages, lru)
279                 count[!!page_is_file_cache(page)]++;
280
281         /* If locked we can use the interrupt unsafe versions */
282         if (locked) {
283                 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
284                 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
285         } else {
286                 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
287                 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
288         }
289 }
290
291 /* Similar to reclaim, but different enough that they don't share logic */
292 static bool too_many_isolated(struct zone *zone)
293 {
294         unsigned long active, inactive, isolated;
295
296         inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
297                                         zone_page_state(zone, NR_INACTIVE_ANON);
298         active = zone_page_state(zone, NR_ACTIVE_FILE) +
299                                         zone_page_state(zone, NR_ACTIVE_ANON);
300         isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
301                                         zone_page_state(zone, NR_ISOLATED_ANON);
302
303         return isolated > (inactive + active) / 2;
304 }
305
306 /**
307  * isolate_migratepages_range() - isolate all migrate-able pages in range.
308  * @zone:       Zone pages are in.
309  * @cc:         Compaction control structure.
310  * @low_pfn:    The first PFN of the range.
311  * @end_pfn:    The one-past-the-last PFN of the range.
312  *
313  * Isolate all pages that can be migrated from the range specified by
314  * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
315  * pending), otherwise PFN of the first page that was not scanned
316  * (which may be both less, equal to or more then end_pfn).
317  *
318  * Assumes that cc->migratepages is empty and cc->nr_migratepages is
319  * zero.
320  *
321  * Apart from cc->migratepages and cc->nr_migratetypes this function
322  * does not modify any cc's fields, in particular it does not modify
323  * (or read for that matter) cc->migrate_pfn.
324  */
325 unsigned long
326 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
327                            unsigned long low_pfn, unsigned long end_pfn)
328 {
329         unsigned long last_pageblock_nr = 0, pageblock_nr;
330         unsigned long nr_scanned = 0, nr_isolated = 0;
331         struct list_head *migratelist = &cc->migratepages;
332         isolate_mode_t mode = 0;
333         struct lruvec *lruvec;
334         unsigned long flags;
335         bool locked = false;
336
337         /*
338          * Ensure that there are not too many pages isolated from the LRU
339          * list by either parallel reclaimers or compaction. If there are,
340          * delay for some time until fewer pages are isolated
341          */
342         while (unlikely(too_many_isolated(zone))) {
343                 /* async migration should just abort */
344                 if (!cc->sync)
345                         return 0;
346
347                 congestion_wait(BLK_RW_ASYNC, HZ/10);
348
349                 if (fatal_signal_pending(current))
350                         return 0;
351         }
352
353         /* Time to isolate some pages for migration */
354         cond_resched();
355         for (; low_pfn < end_pfn; low_pfn++) {
356                 struct page *page;
357
358                 /* give a chance to irqs before checking need_resched() */
359                 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
360                         if (should_release_lock(&zone->lru_lock)) {
361                                 spin_unlock_irqrestore(&zone->lru_lock, flags);
362                                 locked = false;
363                         }
364                 }
365
366                 /*
367                  * migrate_pfn does not necessarily start aligned to a
368                  * pageblock. Ensure that pfn_valid is called when moving
369                  * into a new MAX_ORDER_NR_PAGES range in case of large
370                  * memory holes within the zone
371                  */
372                 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
373                         if (!pfn_valid(low_pfn)) {
374                                 low_pfn += MAX_ORDER_NR_PAGES - 1;
375                                 continue;
376                         }
377                 }
378
379                 if (!pfn_valid_within(low_pfn))
380                         continue;
381                 nr_scanned++;
382
383                 /*
384                  * Get the page and ensure the page is within the same zone.
385                  * See the comment in isolate_freepages about overlapping
386                  * nodes. It is deliberate that the new zone lock is not taken
387                  * as memory compaction should not move pages between nodes.
388                  */
389                 page = pfn_to_page(low_pfn);
390                 if (page_zone(page) != zone)
391                         continue;
392
393                 /* Skip if free */
394                 if (PageBuddy(page))
395                         continue;
396
397                 /*
398                  * For async migration, also only scan in MOVABLE blocks. Async
399                  * migration is optimistic to see if the minimum amount of work
400                  * satisfies the allocation
401                  */
402                 pageblock_nr = low_pfn >> pageblock_order;
403                 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
404                     !migrate_async_suitable(get_pageblock_migratetype(page))) {
405                         goto next_pageblock;
406                 }
407
408                 /* Check may be lockless but that's ok as we recheck later */
409                 if (!PageLRU(page))
410                         continue;
411
412                 /*
413                  * PageLRU is set. lru_lock normally excludes isolation
414                  * splitting and collapsing (collapsing has already happened
415                  * if PageLRU is set) but the lock is not necessarily taken
416                  * here and it is wasteful to take it just to check transhuge.
417                  * Check TransHuge without lock and skip the whole pageblock if
418                  * it's either a transhuge or hugetlbfs page, as calling
419                  * compound_order() without preventing THP from splitting the
420                  * page underneath us may return surprising results.
421                  */
422                 if (PageTransHuge(page)) {
423                         if (!locked)
424                                 goto next_pageblock;
425                         low_pfn += (1 << compound_order(page)) - 1;
426                         continue;
427                 }
428
429                 /* Check if it is ok to still hold the lock */
430                 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
431                                                                 locked, cc);
432                 if (!locked || fatal_signal_pending(current))
433                         break;
434
435                 /* Recheck PageLRU and PageTransHuge under lock */
436                 if (!PageLRU(page))
437                         continue;
438                 if (PageTransHuge(page)) {
439                         low_pfn += (1 << compound_order(page)) - 1;
440                         continue;
441                 }
442
443                 if (!cc->sync)
444                         mode |= ISOLATE_ASYNC_MIGRATE;
445
446                 lruvec = mem_cgroup_page_lruvec(page, zone);
447
448                 /* Try isolate the page */
449                 if (__isolate_lru_page(page, mode) != 0)
450                         continue;
451
452                 VM_BUG_ON(PageTransCompound(page));
453
454                 /* Successfully isolated */
455                 del_page_from_lru_list(page, lruvec, page_lru(page));
456                 list_add(&page->lru, migratelist);
457                 cc->nr_migratepages++;
458                 nr_isolated++;
459
460                 /* Avoid isolating too much */
461                 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
462                         ++low_pfn;
463                         break;
464                 }
465
466                 continue;
467
468 next_pageblock:
469                 low_pfn += pageblock_nr_pages;
470                 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
471                 last_pageblock_nr = pageblock_nr;
472         }
473
474         acct_isolated(zone, locked, cc);
475
476         if (locked)
477                 spin_unlock_irqrestore(&zone->lru_lock, flags);
478
479         trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
480
481         return low_pfn;
482 }
483
484 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
485 #ifdef CONFIG_COMPACTION
486
487 /* Returns true if the page is within a block suitable for migration to */
488 static bool suitable_migration_target(struct page *page)
489 {
490
491         int migratetype = get_pageblock_migratetype(page);
492
493         /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
494         if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
495                 return false;
496
497         /* If the page is a large free page, then allow migration */
498         if (PageBuddy(page) && page_order(page) >= pageblock_order)
499                 return true;
500
501         /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
502         if (migrate_async_suitable(migratetype))
503                 return true;
504
505         /* Otherwise skip the block */
506         return false;
507 }
508
509 /*
510  * Returns the start pfn of the last page block in a zone.  This is the starting
511  * point for full compaction of a zone.  Compaction searches for free pages from
512  * the end of each zone, while isolate_freepages_block scans forward inside each
513  * page block.
514  */
515 static unsigned long start_free_pfn(struct zone *zone)
516 {
517         unsigned long free_pfn;
518         free_pfn = zone->zone_start_pfn + zone->spanned_pages;
519         free_pfn &= ~(pageblock_nr_pages-1);
520         return free_pfn;
521 }
522
523 /*
524  * Based on information in the current compact_control, find blocks
525  * suitable for isolating free pages from and then isolate them.
526  */
527 static void isolate_freepages(struct zone *zone,
528                                 struct compact_control *cc)
529 {
530         struct page *page;
531         unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
532         unsigned long flags;
533         int nr_freepages = cc->nr_freepages;
534         struct list_head *freelist = &cc->freepages;
535
536         /*
537          * Initialise the free scanner. The starting point is where we last
538          * scanned from (or the end of the zone if starting). The low point
539          * is the end of the pageblock the migration scanner is using.
540          */
541         pfn = cc->free_pfn;
542         low_pfn = cc->migrate_pfn + pageblock_nr_pages;
543
544         /*
545          * Take care that if the migration scanner is at the end of the zone
546          * that the free scanner does not accidentally move to the next zone
547          * in the next isolation cycle.
548          */
549         high_pfn = min(low_pfn, pfn);
550
551         zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
552
553         /*
554          * Isolate free pages until enough are available to migrate the
555          * pages on cc->migratepages. We stop searching if the migrate
556          * and free page scanners meet or enough free pages are isolated.
557          */
558         for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
559                                         pfn -= pageblock_nr_pages) {
560                 unsigned long isolated;
561
562                 if (!pfn_valid(pfn))
563                         continue;
564
565                 /*
566                  * Check for overlapping nodes/zones. It's possible on some
567                  * configurations to have a setup like
568                  * node0 node1 node0
569                  * i.e. it's possible that all pages within a zones range of
570                  * pages do not belong to a single zone.
571                  */
572                 page = pfn_to_page(pfn);
573                 if (page_zone(page) != zone)
574                         continue;
575
576                 /* Check the block is suitable for migration */
577                 if (!suitable_migration_target(page))
578                         continue;
579
580                 /*
581                  * Found a block suitable for isolating free pages from. Now
582                  * we disabled interrupts, double check things are ok and
583                  * isolate the pages. This is to minimise the time IRQs
584                  * are disabled
585                  */
586                 isolated = 0;
587
588                 /*
589                  * The zone lock must be held to isolate freepages. This
590                  * unfortunately this is a very coarse lock and can be
591                  * heavily contended if there are parallel allocations
592                  * or parallel compactions. For async compaction do not
593                  * spin on the lock
594                  */
595                 if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
596                         break;
597                 if (suitable_migration_target(page)) {
598                         end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
599                         isolated = isolate_freepages_block(pfn, end_pfn,
600                                                            freelist, false);
601                         nr_freepages += isolated;
602                 }
603                 spin_unlock_irqrestore(&zone->lock, flags);
604
605                 /*
606                  * Record the highest PFN we isolated pages from. When next
607                  * looking for free pages, the search will restart here as
608                  * page migration may have returned some pages to the allocator
609                  */
610                 if (isolated) {
611                         high_pfn = max(high_pfn, pfn);
612
613                         /*
614                          * If the free scanner has wrapped, update
615                          * compact_cached_free_pfn to point to the highest
616                          * pageblock with free pages. This reduces excessive
617                          * scanning of full pageblocks near the end of the
618                          * zone
619                          */
620                         if (cc->order > 0 && cc->wrapped)
621                                 zone->compact_cached_free_pfn = high_pfn;
622                 }
623         }
624
625         /* split_free_page does not map the pages */
626         map_pages(freelist);
627
628         cc->free_pfn = high_pfn;
629         cc->nr_freepages = nr_freepages;
630
631         /* If compact_cached_free_pfn is reset then set it now */
632         if (cc->order > 0 && !cc->wrapped &&
633                         zone->compact_cached_free_pfn == start_free_pfn(zone))
634                 zone->compact_cached_free_pfn = high_pfn;
635 }
636
637 /*
638  * This is a migrate-callback that "allocates" freepages by taking pages
639  * from the isolated freelists in the block we are migrating to.
640  */
641 static struct page *compaction_alloc(struct page *migratepage,
642                                         unsigned long data,
643                                         int **result)
644 {
645         struct compact_control *cc = (struct compact_control *)data;
646         struct page *freepage;
647
648         /* Isolate free pages if necessary */
649         if (list_empty(&cc->freepages)) {
650                 isolate_freepages(cc->zone, cc);
651
652                 if (list_empty(&cc->freepages))
653                         return NULL;
654         }
655
656         freepage = list_entry(cc->freepages.next, struct page, lru);
657         list_del(&freepage->lru);
658         cc->nr_freepages--;
659
660         return freepage;
661 }
662
663 /*
664  * We cannot control nr_migratepages and nr_freepages fully when migration is
665  * running as migrate_pages() has no knowledge of compact_control. When
666  * migration is complete, we count the number of pages on the lists by hand.
667  */
668 static void update_nr_listpages(struct compact_control *cc)
669 {
670         int nr_migratepages = 0;
671         int nr_freepages = 0;
672         struct page *page;
673
674         list_for_each_entry(page, &cc->migratepages, lru)
675                 nr_migratepages++;
676         list_for_each_entry(page, &cc->freepages, lru)
677                 nr_freepages++;
678
679         cc->nr_migratepages = nr_migratepages;
680         cc->nr_freepages = nr_freepages;
681 }
682
683 /* possible outcome of isolate_migratepages */
684 typedef enum {
685         ISOLATE_ABORT,          /* Abort compaction now */
686         ISOLATE_NONE,           /* No pages isolated, continue scanning */
687         ISOLATE_SUCCESS,        /* Pages isolated, migrate */
688 } isolate_migrate_t;
689
690 /*
691  * Isolate all pages that can be migrated from the block pointed to by
692  * the migrate scanner within compact_control.
693  */
694 static isolate_migrate_t isolate_migratepages(struct zone *zone,
695                                         struct compact_control *cc)
696 {
697         unsigned long low_pfn, end_pfn;
698
699         /* Do not scan outside zone boundaries */
700         low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
701
702         /* Only scan within a pageblock boundary */
703         end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
704
705         /* Do not cross the free scanner or scan within a memory hole */
706         if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
707                 cc->migrate_pfn = end_pfn;
708                 return ISOLATE_NONE;
709         }
710
711         /* Perform the isolation */
712         low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
713         if (!low_pfn || cc->contended)
714                 return ISOLATE_ABORT;
715
716         cc->migrate_pfn = low_pfn;
717
718         return ISOLATE_SUCCESS;
719 }
720
721 static int compact_finished(struct zone *zone,
722                             struct compact_control *cc)
723 {
724         unsigned long watermark;
725
726         if (fatal_signal_pending(current))
727                 return COMPACT_PARTIAL;
728
729         /*
730          * A full (order == -1) compaction run starts at the beginning and
731          * end of a zone; it completes when the migrate and free scanner meet.
732          * A partial (order > 0) compaction can start with the free scanner
733          * at a random point in the zone, and may have to restart.
734          */
735         if (cc->free_pfn <= cc->migrate_pfn) {
736                 if (cc->order > 0 && !cc->wrapped) {
737                         /* We started partway through; restart at the end. */
738                         unsigned long free_pfn = start_free_pfn(zone);
739                         zone->compact_cached_free_pfn = free_pfn;
740                         cc->free_pfn = free_pfn;
741                         cc->wrapped = 1;
742                         return COMPACT_CONTINUE;
743                 }
744                 return COMPACT_COMPLETE;
745         }
746
747         /* We wrapped around and ended up where we started. */
748         if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
749                 return COMPACT_COMPLETE;
750
751         /*
752          * order == -1 is expected when compacting via
753          * /proc/sys/vm/compact_memory
754          */
755         if (cc->order == -1)
756                 return COMPACT_CONTINUE;
757
758         /* Compaction run is not finished if the watermark is not met */
759         watermark = low_wmark_pages(zone);
760         watermark += (1 << cc->order);
761
762         if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
763                 return COMPACT_CONTINUE;
764
765         /* Direct compactor: Is a suitable page free? */
766         if (cc->page) {
767                 /* Was a suitable page captured? */
768                 if (*cc->page)
769                         return COMPACT_PARTIAL;
770         } else {
771                 unsigned int order;
772                 for (order = cc->order; order < MAX_ORDER; order++) {
773                         struct free_area *area = &zone->free_area[cc->order];
774                         /* Job done if page is free of the right migratetype */
775                         if (!list_empty(&area->free_list[cc->migratetype]))
776                                 return COMPACT_PARTIAL;
777
778                         /* Job done if allocation would set block type */
779                         if (cc->order >= pageblock_order && area->nr_free)
780                                 return COMPACT_PARTIAL;
781                 }
782         }
783
784         return COMPACT_CONTINUE;
785 }
786
787 /*
788  * compaction_suitable: Is this suitable to run compaction on this zone now?
789  * Returns
790  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
791  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
792  *   COMPACT_CONTINUE - If compaction should run now
793  */
794 unsigned long compaction_suitable(struct zone *zone, int order)
795 {
796         int fragindex;
797         unsigned long watermark;
798
799         /*
800          * order == -1 is expected when compacting via
801          * /proc/sys/vm/compact_memory
802          */
803         if (order == -1)
804                 return COMPACT_CONTINUE;
805
806         /*
807          * Watermarks for order-0 must be met for compaction. Note the 2UL.
808          * This is because during migration, copies of pages need to be
809          * allocated and for a short time, the footprint is higher
810          */
811         watermark = low_wmark_pages(zone) + (2UL << order);
812         if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
813                 return COMPACT_SKIPPED;
814
815         /*
816          * fragmentation index determines if allocation failures are due to
817          * low memory or external fragmentation
818          *
819          * index of -1000 implies allocations might succeed depending on
820          * watermarks
821          * index towards 0 implies failure is due to lack of memory
822          * index towards 1000 implies failure is due to fragmentation
823          *
824          * Only compact if a failure would be due to fragmentation.
825          */
826         fragindex = fragmentation_index(zone, order);
827         if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
828                 return COMPACT_SKIPPED;
829
830         if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
831             0, 0))
832                 return COMPACT_PARTIAL;
833
834         return COMPACT_CONTINUE;
835 }
836
837 static int compact_zone(struct zone *zone, struct compact_control *cc)
838 {
839         int ret;
840
841         ret = compaction_suitable(zone, cc->order);
842         switch (ret) {
843         case COMPACT_PARTIAL:
844         case COMPACT_SKIPPED:
845                 /* Compaction is likely to fail */
846                 return ret;
847         case COMPACT_CONTINUE:
848                 /* Fall through to compaction */
849                 ;
850         }
851
852         /* Setup to move all movable pages to the end of the zone */
853         cc->migrate_pfn = zone->zone_start_pfn;
854
855         if (cc->order > 0) {
856                 /* Incremental compaction. Start where the last one stopped. */
857                 cc->free_pfn = zone->compact_cached_free_pfn;
858                 cc->start_free_pfn = cc->free_pfn;
859         } else {
860                 /* Order == -1 starts at the end of the zone. */
861                 cc->free_pfn = start_free_pfn(zone);
862         }
863
864         migrate_prep_local();
865
866         while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
867                 unsigned long nr_migrate, nr_remaining;
868                 int err;
869
870                 switch (isolate_migratepages(zone, cc)) {
871                 case ISOLATE_ABORT:
872                         ret = COMPACT_PARTIAL;
873                         putback_lru_pages(&cc->migratepages);
874                         cc->nr_migratepages = 0;
875                         goto out;
876                 case ISOLATE_NONE:
877                         continue;
878                 case ISOLATE_SUCCESS:
879                         ;
880                 }
881
882                 nr_migrate = cc->nr_migratepages;
883                 err = migrate_pages(&cc->migratepages, compaction_alloc,
884                                 (unsigned long)cc, false,
885                                 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
886                 update_nr_listpages(cc);
887                 nr_remaining = cc->nr_migratepages;
888
889                 count_vm_event(COMPACTBLOCKS);
890                 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
891                 if (nr_remaining)
892                         count_vm_events(COMPACTPAGEFAILED, nr_remaining);
893                 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
894                                                 nr_remaining);
895
896                 /* Release LRU pages not migrated */
897                 if (err) {
898                         putback_lru_pages(&cc->migratepages);
899                         cc->nr_migratepages = 0;
900                         if (err == -ENOMEM) {
901                                 ret = COMPACT_PARTIAL;
902                                 goto out;
903                         }
904                 }
905
906                 /* Capture a page now if it is a suitable size */
907                 compact_capture_page(cc);
908         }
909
910 out:
911         /* Release free pages and check accounting */
912         cc->nr_freepages -= release_freepages(&cc->freepages);
913         VM_BUG_ON(cc->nr_freepages != 0);
914
915         return ret;
916 }
917
918 static unsigned long compact_zone_order(struct zone *zone,
919                                  int order, gfp_t gfp_mask,
920                                  bool sync, bool *contended,
921                                  struct page **page)
922 {
923         unsigned long ret;
924         struct compact_control cc = {
925                 .nr_freepages = 0,
926                 .nr_migratepages = 0,
927                 .order = order,
928                 .migratetype = allocflags_to_migratetype(gfp_mask),
929                 .zone = zone,
930                 .sync = sync,
931                 .page = page,
932         };
933         INIT_LIST_HEAD(&cc.freepages);
934         INIT_LIST_HEAD(&cc.migratepages);
935
936         ret = compact_zone(zone, &cc);
937
938         VM_BUG_ON(!list_empty(&cc.freepages));
939         VM_BUG_ON(!list_empty(&cc.migratepages));
940
941         *contended = cc.contended;
942         return ret;
943 }
944
945 int sysctl_extfrag_threshold = 500;
946
947 /**
948  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
949  * @zonelist: The zonelist used for the current allocation
950  * @order: The order of the current allocation
951  * @gfp_mask: The GFP mask of the current allocation
952  * @nodemask: The allowed nodes to allocate from
953  * @sync: Whether migration is synchronous or not
954  * @contended: Return value that is true if compaction was aborted due to lock contention
955  * @page: Optionally capture a free page of the requested order during compaction
956  *
957  * This is the main entry point for direct page compaction.
958  */
959 unsigned long try_to_compact_pages(struct zonelist *zonelist,
960                         int order, gfp_t gfp_mask, nodemask_t *nodemask,
961                         bool sync, bool *contended, struct page **page)
962 {
963         enum zone_type high_zoneidx = gfp_zone(gfp_mask);
964         int may_enter_fs = gfp_mask & __GFP_FS;
965         int may_perform_io = gfp_mask & __GFP_IO;
966         struct zoneref *z;
967         struct zone *zone;
968         int rc = COMPACT_SKIPPED;
969         int alloc_flags = 0;
970
971         /* Check if the GFP flags allow compaction */
972         if (!order || !may_enter_fs || !may_perform_io)
973                 return rc;
974
975         count_vm_event(COMPACTSTALL);
976
977 #ifdef CONFIG_CMA
978         if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
979                 alloc_flags |= ALLOC_CMA;
980 #endif
981         /* Compact each zone in the list */
982         for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
983                                                                 nodemask) {
984                 int status;
985
986                 status = compact_zone_order(zone, order, gfp_mask, sync,
987                                                 contended, page);
988                 rc = max(status, rc);
989
990                 /* If a normal allocation would succeed, stop compacting */
991                 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
992                                       alloc_flags))
993                         break;
994         }
995
996         return rc;
997 }
998
999
1000 /* Compact all zones within a node */
1001 static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1002 {
1003         int zoneid;
1004         struct zone *zone;
1005
1006         for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1007
1008                 zone = &pgdat->node_zones[zoneid];
1009                 if (!populated_zone(zone))
1010                         continue;
1011
1012                 cc->nr_freepages = 0;
1013                 cc->nr_migratepages = 0;
1014                 cc->zone = zone;
1015                 INIT_LIST_HEAD(&cc->freepages);
1016                 INIT_LIST_HEAD(&cc->migratepages);
1017
1018                 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1019                         compact_zone(zone, cc);
1020
1021                 if (cc->order > 0) {
1022                         int ok = zone_watermark_ok(zone, cc->order,
1023                                                 low_wmark_pages(zone), 0, 0);
1024                         if (ok && cc->order >= zone->compact_order_failed)
1025                                 zone->compact_order_failed = cc->order + 1;
1026                         /* Currently async compaction is never deferred. */
1027                         else if (!ok && cc->sync)
1028                                 defer_compaction(zone, cc->order);
1029                 }
1030
1031                 VM_BUG_ON(!list_empty(&cc->freepages));
1032                 VM_BUG_ON(!list_empty(&cc->migratepages));
1033         }
1034
1035         return 0;
1036 }
1037
1038 int compact_pgdat(pg_data_t *pgdat, int order)
1039 {
1040         struct compact_control cc = {
1041                 .order = order,
1042                 .sync = false,
1043                 .page = NULL,
1044         };
1045
1046         return __compact_pgdat(pgdat, &cc);
1047 }
1048
1049 static int compact_node(int nid)
1050 {
1051         struct compact_control cc = {
1052                 .order = -1,
1053                 .sync = true,
1054                 .page = NULL,
1055         };
1056
1057         return __compact_pgdat(NODE_DATA(nid), &cc);
1058 }
1059
1060 /* Compact all nodes in the system */
1061 static int compact_nodes(void)
1062 {
1063         int nid;
1064
1065         /* Flush pending updates to the LRU lists */
1066         lru_add_drain_all();
1067
1068         for_each_online_node(nid)
1069                 compact_node(nid);
1070
1071         return COMPACT_COMPLETE;
1072 }
1073
1074 /* The written value is actually unused, all memory is compacted */
1075 int sysctl_compact_memory;
1076
1077 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1078 int sysctl_compaction_handler(struct ctl_table *table, int write,
1079                         void __user *buffer, size_t *length, loff_t *ppos)
1080 {
1081         if (write)
1082                 return compact_nodes();
1083
1084         return 0;
1085 }
1086
1087 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1088                         void __user *buffer, size_t *length, loff_t *ppos)
1089 {
1090         proc_dointvec_minmax(table, write, buffer, length, ppos);
1091
1092         return 0;
1093 }
1094
1095 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1096 ssize_t sysfs_compact_node(struct device *dev,
1097                         struct device_attribute *attr,
1098                         const char *buf, size_t count)
1099 {
1100         int nid = dev->id;
1101
1102         if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1103                 /* Flush pending updates to the LRU lists */
1104                 lru_add_drain_all();
1105
1106                 compact_node(nid);
1107         }
1108
1109         return count;
1110 }
1111 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1112
1113 int compaction_register_node(struct node *node)
1114 {
1115         return device_create_file(&node->dev, &dev_attr_compact);
1116 }
1117
1118 void compaction_unregister_node(struct node *node)
1119 {
1120         return device_remove_file(&node->dev, &dev_attr_compact);
1121 }
1122 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1123
1124 #endif /* CONFIG_COMPACTION */