]> Git Repo - linux.git/blob - mm/compaction.c
mm, compaction: remember position within pageblock in free pages scanner
[linux.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 <[email protected]>
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 <linux/balloon_compaction.h>
18 #include <linux/page-isolation.h>
19 #include "internal.h"
20
21 #ifdef CONFIG_COMPACTION
22 static inline void count_compact_event(enum vm_event_item item)
23 {
24         count_vm_event(item);
25 }
26
27 static inline void count_compact_events(enum vm_event_item item, long delta)
28 {
29         count_vm_events(item, delta);
30 }
31 #else
32 #define count_compact_event(item) do { } while (0)
33 #define count_compact_events(item, delta) do { } while (0)
34 #endif
35
36 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/compaction.h>
40
41 static unsigned long release_freepages(struct list_head *freelist)
42 {
43         struct page *page, *next;
44         unsigned long count = 0;
45
46         list_for_each_entry_safe(page, next, freelist, lru) {
47                 list_del(&page->lru);
48                 __free_page(page);
49                 count++;
50         }
51
52         return count;
53 }
54
55 static void map_pages(struct list_head *list)
56 {
57         struct page *page;
58
59         list_for_each_entry(page, list, lru) {
60                 arch_alloc_page(page, 0);
61                 kernel_map_pages(page, 1, 1);
62         }
63 }
64
65 static inline bool migrate_async_suitable(int migratetype)
66 {
67         return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68 }
69
70 /*
71  * Check that the whole (or subset of) a pageblock given by the interval of
72  * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
73  * with the migration of free compaction scanner. The scanners then need to
74  * use only pfn_valid_within() check for arches that allow holes within
75  * pageblocks.
76  *
77  * Return struct page pointer of start_pfn, or NULL if checks were not passed.
78  *
79  * It's possible on some configurations to have a setup like node0 node1 node0
80  * i.e. it's possible that all pages within a zones range of pages do not
81  * belong to a single zone. We assume that a border between node0 and node1
82  * can occur within a single pageblock, but not a node0 node1 node0
83  * interleaving within a single pageblock. It is therefore sufficient to check
84  * the first and last page of a pageblock and avoid checking each individual
85  * page in a pageblock.
86  */
87 static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
88                                 unsigned long end_pfn, struct zone *zone)
89 {
90         struct page *start_page;
91         struct page *end_page;
92
93         /* end_pfn is one past the range we are checking */
94         end_pfn--;
95
96         if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
97                 return NULL;
98
99         start_page = pfn_to_page(start_pfn);
100
101         if (page_zone(start_page) != zone)
102                 return NULL;
103
104         end_page = pfn_to_page(end_pfn);
105
106         /* This gives a shorter code than deriving page_zone(end_page) */
107         if (page_zone_id(start_page) != page_zone_id(end_page))
108                 return NULL;
109
110         return start_page;
111 }
112
113 #ifdef CONFIG_COMPACTION
114 /* Returns true if the pageblock should be scanned for pages to isolate. */
115 static inline bool isolation_suitable(struct compact_control *cc,
116                                         struct page *page)
117 {
118         if (cc->ignore_skip_hint)
119                 return true;
120
121         return !get_pageblock_skip(page);
122 }
123
124 /*
125  * This function is called to clear all cached information on pageblocks that
126  * should be skipped for page isolation when the migrate and free page scanner
127  * meet.
128  */
129 static void __reset_isolation_suitable(struct zone *zone)
130 {
131         unsigned long start_pfn = zone->zone_start_pfn;
132         unsigned long end_pfn = zone_end_pfn(zone);
133         unsigned long pfn;
134
135         zone->compact_cached_migrate_pfn[0] = start_pfn;
136         zone->compact_cached_migrate_pfn[1] = start_pfn;
137         zone->compact_cached_free_pfn = end_pfn;
138         zone->compact_blockskip_flush = false;
139
140         /* Walk the zone and mark every pageblock as suitable for isolation */
141         for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142                 struct page *page;
143
144                 cond_resched();
145
146                 if (!pfn_valid(pfn))
147                         continue;
148
149                 page = pfn_to_page(pfn);
150                 if (zone != page_zone(page))
151                         continue;
152
153                 clear_pageblock_skip(page);
154         }
155 }
156
157 void reset_isolation_suitable(pg_data_t *pgdat)
158 {
159         int zoneid;
160
161         for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
162                 struct zone *zone = &pgdat->node_zones[zoneid];
163                 if (!populated_zone(zone))
164                         continue;
165
166                 /* Only flush if a full compaction finished recently */
167                 if (zone->compact_blockskip_flush)
168                         __reset_isolation_suitable(zone);
169         }
170 }
171
172 /*
173  * If no pages were isolated then mark this pageblock to be skipped in the
174  * future. The information is later cleared by __reset_isolation_suitable().
175  */
176 static void update_pageblock_skip(struct compact_control *cc,
177                         struct page *page, unsigned long nr_isolated,
178                         bool migrate_scanner)
179 {
180         struct zone *zone = cc->zone;
181         unsigned long pfn;
182
183         if (cc->ignore_skip_hint)
184                 return;
185
186         if (!page)
187                 return;
188
189         if (nr_isolated)
190                 return;
191
192         set_pageblock_skip(page);
193
194         pfn = page_to_pfn(page);
195
196         /* Update where async and sync compaction should restart */
197         if (migrate_scanner) {
198                 if (cc->finished_update_migrate)
199                         return;
200                 if (pfn > zone->compact_cached_migrate_pfn[0])
201                         zone->compact_cached_migrate_pfn[0] = pfn;
202                 if (cc->mode != MIGRATE_ASYNC &&
203                     pfn > zone->compact_cached_migrate_pfn[1])
204                         zone->compact_cached_migrate_pfn[1] = pfn;
205         } else {
206                 if (cc->finished_update_free)
207                         return;
208                 if (pfn < zone->compact_cached_free_pfn)
209                         zone->compact_cached_free_pfn = pfn;
210         }
211 }
212 #else
213 static inline bool isolation_suitable(struct compact_control *cc,
214                                         struct page *page)
215 {
216         return true;
217 }
218
219 static void update_pageblock_skip(struct compact_control *cc,
220                         struct page *page, unsigned long nr_isolated,
221                         bool migrate_scanner)
222 {
223 }
224 #endif /* CONFIG_COMPACTION */
225
226 /*
227  * Compaction requires the taking of some coarse locks that are potentially
228  * very heavily contended. For async compaction, back out if the lock cannot
229  * be taken immediately. For sync compaction, spin on the lock if needed.
230  *
231  * Returns true if the lock is held
232  * Returns false if the lock is not held and compaction should abort
233  */
234 static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
235                                                 struct compact_control *cc)
236 {
237         if (cc->mode == MIGRATE_ASYNC) {
238                 if (!spin_trylock_irqsave(lock, *flags)) {
239                         cc->contended = COMPACT_CONTENDED_LOCK;
240                         return false;
241                 }
242         } else {
243                 spin_lock_irqsave(lock, *flags);
244         }
245
246         return true;
247 }
248
249 /*
250  * Compaction requires the taking of some coarse locks that are potentially
251  * very heavily contended. The lock should be periodically unlocked to avoid
252  * having disabled IRQs for a long time, even when there is nobody waiting on
253  * the lock. It might also be that allowing the IRQs will result in
254  * need_resched() becoming true. If scheduling is needed, async compaction
255  * aborts. Sync compaction schedules.
256  * Either compaction type will also abort if a fatal signal is pending.
257  * In either case if the lock was locked, it is dropped and not regained.
258  *
259  * Returns true if compaction should abort due to fatal signal pending, or
260  *              async compaction due to need_resched()
261  * Returns false when compaction can continue (sync compaction might have
262  *              scheduled)
263  */
264 static bool compact_unlock_should_abort(spinlock_t *lock,
265                 unsigned long flags, bool *locked, struct compact_control *cc)
266 {
267         if (*locked) {
268                 spin_unlock_irqrestore(lock, flags);
269                 *locked = false;
270         }
271
272         if (fatal_signal_pending(current)) {
273                 cc->contended = COMPACT_CONTENDED_SCHED;
274                 return true;
275         }
276
277         if (need_resched()) {
278                 if (cc->mode == MIGRATE_ASYNC) {
279                         cc->contended = COMPACT_CONTENDED_SCHED;
280                         return true;
281                 }
282                 cond_resched();
283         }
284
285         return false;
286 }
287
288 /*
289  * Aside from avoiding lock contention, compaction also periodically checks
290  * need_resched() and either schedules in sync compaction or aborts async
291  * compaction. This is similar to what compact_unlock_should_abort() does, but
292  * is used where no lock is concerned.
293  *
294  * Returns false when no scheduling was needed, or sync compaction scheduled.
295  * Returns true when async compaction should abort.
296  */
297 static inline bool compact_should_abort(struct compact_control *cc)
298 {
299         /* async compaction aborts if contended */
300         if (need_resched()) {
301                 if (cc->mode == MIGRATE_ASYNC) {
302                         cc->contended = COMPACT_CONTENDED_SCHED;
303                         return true;
304                 }
305
306                 cond_resched();
307         }
308
309         return false;
310 }
311
312 /* Returns true if the page is within a block suitable for migration to */
313 static bool suitable_migration_target(struct page *page)
314 {
315         /* If the page is a large free page, then disallow migration */
316         if (PageBuddy(page) && page_order(page) >= pageblock_order)
317                 return false;
318
319         /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
320         if (migrate_async_suitable(get_pageblock_migratetype(page)))
321                 return true;
322
323         /* Otherwise skip the block */
324         return false;
325 }
326
327 /*
328  * Isolate free pages onto a private freelist. If @strict is true, will abort
329  * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
330  * (even though it may still end up isolating some pages).
331  */
332 static unsigned long isolate_freepages_block(struct compact_control *cc,
333                                 unsigned long *start_pfn,
334                                 unsigned long end_pfn,
335                                 struct list_head *freelist,
336                                 bool strict)
337 {
338         int nr_scanned = 0, total_isolated = 0;
339         struct page *cursor, *valid_page = NULL;
340         unsigned long flags;
341         bool locked = false;
342         unsigned long blockpfn = *start_pfn;
343
344         cursor = pfn_to_page(blockpfn);
345
346         /* Isolate free pages. */
347         for (; blockpfn < end_pfn; blockpfn++, cursor++) {
348                 int isolated, i;
349                 struct page *page = cursor;
350
351                 /*
352                  * Periodically drop the lock (if held) regardless of its
353                  * contention, to give chance to IRQs. Abort if fatal signal
354                  * pending or async compaction detects need_resched()
355                  */
356                 if (!(blockpfn % SWAP_CLUSTER_MAX)
357                     && compact_unlock_should_abort(&cc->zone->lock, flags,
358                                                                 &locked, cc))
359                         break;
360
361                 nr_scanned++;
362                 if (!pfn_valid_within(blockpfn))
363                         goto isolate_fail;
364
365                 if (!valid_page)
366                         valid_page = page;
367                 if (!PageBuddy(page))
368                         goto isolate_fail;
369
370                 /*
371                  * If we already hold the lock, we can skip some rechecking.
372                  * Note that if we hold the lock now, checked_pageblock was
373                  * already set in some previous iteration (or strict is true),
374                  * so it is correct to skip the suitable migration target
375                  * recheck as well.
376                  */
377                 if (!locked) {
378                         /*
379                          * The zone lock must be held to isolate freepages.
380                          * Unfortunately this is a very coarse lock and can be
381                          * heavily contended if there are parallel allocations
382                          * or parallel compactions. For async compaction do not
383                          * spin on the lock and we acquire the lock as late as
384                          * possible.
385                          */
386                         locked = compact_trylock_irqsave(&cc->zone->lock,
387                                                                 &flags, cc);
388                         if (!locked)
389                                 break;
390
391                         /* Recheck this is a buddy page under lock */
392                         if (!PageBuddy(page))
393                                 goto isolate_fail;
394                 }
395
396                 /* Found a free page, break it into order-0 pages */
397                 isolated = split_free_page(page);
398                 total_isolated += isolated;
399                 for (i = 0; i < isolated; i++) {
400                         list_add(&page->lru, freelist);
401                         page++;
402                 }
403
404                 /* If a page was split, advance to the end of it */
405                 if (isolated) {
406                         blockpfn += isolated - 1;
407                         cursor += isolated - 1;
408                         continue;
409                 }
410
411 isolate_fail:
412                 if (strict)
413                         break;
414                 else
415                         continue;
416
417         }
418
419         /* Record how far we have got within the block */
420         *start_pfn = blockpfn;
421
422         trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
423
424         /*
425          * If strict isolation is requested by CMA then check that all the
426          * pages requested were isolated. If there were any failures, 0 is
427          * returned and CMA will fail.
428          */
429         if (strict && blockpfn < end_pfn)
430                 total_isolated = 0;
431
432         if (locked)
433                 spin_unlock_irqrestore(&cc->zone->lock, flags);
434
435         /* Update the pageblock-skip if the whole pageblock was scanned */
436         if (blockpfn == end_pfn)
437                 update_pageblock_skip(cc, valid_page, total_isolated, false);
438
439         count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
440         if (total_isolated)
441                 count_compact_events(COMPACTISOLATED, total_isolated);
442         return total_isolated;
443 }
444
445 /**
446  * isolate_freepages_range() - isolate free pages.
447  * @start_pfn: The first PFN to start isolating.
448  * @end_pfn:   The one-past-last PFN.
449  *
450  * Non-free pages, invalid PFNs, or zone boundaries within the
451  * [start_pfn, end_pfn) range are considered errors, cause function to
452  * undo its actions and return zero.
453  *
454  * Otherwise, function returns one-past-the-last PFN of isolated page
455  * (which may be greater then end_pfn if end fell in a middle of
456  * a free page).
457  */
458 unsigned long
459 isolate_freepages_range(struct compact_control *cc,
460                         unsigned long start_pfn, unsigned long end_pfn)
461 {
462         unsigned long isolated, pfn, block_end_pfn;
463         LIST_HEAD(freelist);
464
465         pfn = start_pfn;
466         block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
467
468         for (; pfn < end_pfn; pfn += isolated,
469                                 block_end_pfn += pageblock_nr_pages) {
470                 /* Protect pfn from changing by isolate_freepages_block */
471                 unsigned long isolate_start_pfn = pfn;
472
473                 block_end_pfn = min(block_end_pfn, end_pfn);
474
475                 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
476                         break;
477
478                 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
479                                                 block_end_pfn, &freelist, true);
480
481                 /*
482                  * In strict mode, isolate_freepages_block() returns 0 if
483                  * there are any holes in the block (ie. invalid PFNs or
484                  * non-free pages).
485                  */
486                 if (!isolated)
487                         break;
488
489                 /*
490                  * If we managed to isolate pages, it is always (1 << n) *
491                  * pageblock_nr_pages for some non-negative n.  (Max order
492                  * page may span two pageblocks).
493                  */
494         }
495
496         /* split_free_page does not map the pages */
497         map_pages(&freelist);
498
499         if (pfn < end_pfn) {
500                 /* Loop terminated early, cleanup. */
501                 release_freepages(&freelist);
502                 return 0;
503         }
504
505         /* We don't use freelists for anything. */
506         return pfn;
507 }
508
509 /* Update the number of anon and file isolated pages in the zone */
510 static void acct_isolated(struct zone *zone, struct compact_control *cc)
511 {
512         struct page *page;
513         unsigned int count[2] = { 0, };
514
515         if (list_empty(&cc->migratepages))
516                 return;
517
518         list_for_each_entry(page, &cc->migratepages, lru)
519                 count[!!page_is_file_cache(page)]++;
520
521         mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
522         mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
523 }
524
525 /* Similar to reclaim, but different enough that they don't share logic */
526 static bool too_many_isolated(struct zone *zone)
527 {
528         unsigned long active, inactive, isolated;
529
530         inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
531                                         zone_page_state(zone, NR_INACTIVE_ANON);
532         active = zone_page_state(zone, NR_ACTIVE_FILE) +
533                                         zone_page_state(zone, NR_ACTIVE_ANON);
534         isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
535                                         zone_page_state(zone, NR_ISOLATED_ANON);
536
537         return isolated > (inactive + active) / 2;
538 }
539
540 /**
541  * isolate_migratepages_block() - isolate all migrate-able pages within
542  *                                a single pageblock
543  * @cc:         Compaction control structure.
544  * @low_pfn:    The first PFN to isolate
545  * @end_pfn:    The one-past-the-last PFN to isolate, within same pageblock
546  * @isolate_mode: Isolation mode to be used.
547  *
548  * Isolate all pages that can be migrated from the range specified by
549  * [low_pfn, end_pfn). The range is expected to be within same pageblock.
550  * Returns zero if there is a fatal signal pending, otherwise PFN of the
551  * first page that was not scanned (which may be both less, equal to or more
552  * than end_pfn).
553  *
554  * The pages are isolated on cc->migratepages list (not required to be empty),
555  * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
556  * is neither read nor updated.
557  */
558 static unsigned long
559 isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
560                         unsigned long end_pfn, isolate_mode_t isolate_mode)
561 {
562         struct zone *zone = cc->zone;
563         unsigned long nr_scanned = 0, nr_isolated = 0;
564         struct list_head *migratelist = &cc->migratepages;
565         struct lruvec *lruvec;
566         unsigned long flags;
567         bool locked = false;
568         struct page *page = NULL, *valid_page = NULL;
569
570         /*
571          * Ensure that there are not too many pages isolated from the LRU
572          * list by either parallel reclaimers or compaction. If there are,
573          * delay for some time until fewer pages are isolated
574          */
575         while (unlikely(too_many_isolated(zone))) {
576                 /* async migration should just abort */
577                 if (cc->mode == MIGRATE_ASYNC)
578                         return 0;
579
580                 congestion_wait(BLK_RW_ASYNC, HZ/10);
581
582                 if (fatal_signal_pending(current))
583                         return 0;
584         }
585
586         if (compact_should_abort(cc))
587                 return 0;
588
589         /* Time to isolate some pages for migration */
590         for (; low_pfn < end_pfn; low_pfn++) {
591                 /*
592                  * Periodically drop the lock (if held) regardless of its
593                  * contention, to give chance to IRQs. Abort async compaction
594                  * if contended.
595                  */
596                 if (!(low_pfn % SWAP_CLUSTER_MAX)
597                     && compact_unlock_should_abort(&zone->lru_lock, flags,
598                                                                 &locked, cc))
599                         break;
600
601                 if (!pfn_valid_within(low_pfn))
602                         continue;
603                 nr_scanned++;
604
605                 page = pfn_to_page(low_pfn);
606
607                 if (!valid_page)
608                         valid_page = page;
609
610                 /*
611                  * Skip if free. page_order cannot be used without zone->lock
612                  * as nothing prevents parallel allocations or buddy merging.
613                  */
614                 if (PageBuddy(page))
615                         continue;
616
617                 /*
618                  * Check may be lockless but that's ok as we recheck later.
619                  * It's possible to migrate LRU pages and balloon pages
620                  * Skip any other type of page
621                  */
622                 if (!PageLRU(page)) {
623                         if (unlikely(balloon_page_movable(page))) {
624                                 if (locked && balloon_page_isolate(page)) {
625                                         /* Successfully isolated */
626                                         goto isolate_success;
627                                 }
628                         }
629                         continue;
630                 }
631
632                 /*
633                  * PageLRU is set. lru_lock normally excludes isolation
634                  * splitting and collapsing (collapsing has already happened
635                  * if PageLRU is set) but the lock is not necessarily taken
636                  * here and it is wasteful to take it just to check transhuge.
637                  * Check TransHuge without lock and skip the whole pageblock if
638                  * it's either a transhuge or hugetlbfs page, as calling
639                  * compound_order() without preventing THP from splitting the
640                  * page underneath us may return surprising results.
641                  */
642                 if (PageTransHuge(page)) {
643                         if (!locked)
644                                 low_pfn = ALIGN(low_pfn + 1,
645                                                 pageblock_nr_pages) - 1;
646                         else
647                                 low_pfn += (1 << compound_order(page)) - 1;
648
649                         continue;
650                 }
651
652                 /*
653                  * Migration will fail if an anonymous page is pinned in memory,
654                  * so avoid taking lru_lock and isolating it unnecessarily in an
655                  * admittedly racy check.
656                  */
657                 if (!page_mapping(page) &&
658                     page_count(page) > page_mapcount(page))
659                         continue;
660
661                 /* If we already hold the lock, we can skip some rechecking */
662                 if (!locked) {
663                         locked = compact_trylock_irqsave(&zone->lru_lock,
664                                                                 &flags, cc);
665                         if (!locked)
666                                 break;
667
668                         /* Recheck PageLRU and PageTransHuge under lock */
669                         if (!PageLRU(page))
670                                 continue;
671                         if (PageTransHuge(page)) {
672                                 low_pfn += (1 << compound_order(page)) - 1;
673                                 continue;
674                         }
675                 }
676
677                 lruvec = mem_cgroup_page_lruvec(page, zone);
678
679                 /* Try isolate the page */
680                 if (__isolate_lru_page(page, isolate_mode) != 0)
681                         continue;
682
683                 VM_BUG_ON_PAGE(PageTransCompound(page), page);
684
685                 /* Successfully isolated */
686                 del_page_from_lru_list(page, lruvec, page_lru(page));
687
688 isolate_success:
689                 cc->finished_update_migrate = true;
690                 list_add(&page->lru, migratelist);
691                 cc->nr_migratepages++;
692                 nr_isolated++;
693
694                 /* Avoid isolating too much */
695                 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
696                         ++low_pfn;
697                         break;
698                 }
699         }
700
701         if (locked)
702                 spin_unlock_irqrestore(&zone->lru_lock, flags);
703
704         /*
705          * Update the pageblock-skip information and cached scanner pfn,
706          * if the whole pageblock was scanned without isolating any page.
707          */
708         if (low_pfn == end_pfn)
709                 update_pageblock_skip(cc, valid_page, nr_isolated, true);
710
711         trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
712
713         count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
714         if (nr_isolated)
715                 count_compact_events(COMPACTISOLATED, nr_isolated);
716
717         return low_pfn;
718 }
719
720 /**
721  * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
722  * @cc:        Compaction control structure.
723  * @start_pfn: The first PFN to start isolating.
724  * @end_pfn:   The one-past-last PFN.
725  *
726  * Returns zero if isolation fails fatally due to e.g. pending signal.
727  * Otherwise, function returns one-past-the-last PFN of isolated page
728  * (which may be greater than end_pfn if end fell in a middle of a THP page).
729  */
730 unsigned long
731 isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
732                                                         unsigned long end_pfn)
733 {
734         unsigned long pfn, block_end_pfn;
735
736         /* Scan block by block. First and last block may be incomplete */
737         pfn = start_pfn;
738         block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
739
740         for (; pfn < end_pfn; pfn = block_end_pfn,
741                                 block_end_pfn += pageblock_nr_pages) {
742
743                 block_end_pfn = min(block_end_pfn, end_pfn);
744
745                 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
746                         continue;
747
748                 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
749                                                         ISOLATE_UNEVICTABLE);
750
751                 /*
752                  * In case of fatal failure, release everything that might
753                  * have been isolated in the previous iteration, and signal
754                  * the failure back to caller.
755                  */
756                 if (!pfn) {
757                         putback_movable_pages(&cc->migratepages);
758                         cc->nr_migratepages = 0;
759                         break;
760                 }
761         }
762         acct_isolated(cc->zone, cc);
763
764         return pfn;
765 }
766
767 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
768 #ifdef CONFIG_COMPACTION
769 /*
770  * Based on information in the current compact_control, find blocks
771  * suitable for isolating free pages from and then isolate them.
772  */
773 static void isolate_freepages(struct compact_control *cc)
774 {
775         struct zone *zone = cc->zone;
776         struct page *page;
777         unsigned long block_start_pfn;  /* start of current pageblock */
778         unsigned long isolate_start_pfn; /* exact pfn we start at */
779         unsigned long block_end_pfn;    /* end of current pageblock */
780         unsigned long low_pfn;       /* lowest pfn scanner is able to scan */
781         int nr_freepages = cc->nr_freepages;
782         struct list_head *freelist = &cc->freepages;
783
784         /*
785          * Initialise the free scanner. The starting point is where we last
786          * successfully isolated from, zone-cached value, or the end of the
787          * zone when isolating for the first time. For looping we also need
788          * this pfn aligned down to the pageblock boundary, because we do
789          * block_start_pfn -= pageblock_nr_pages in the for loop.
790          * For ending point, take care when isolating in last pageblock of a
791          * a zone which ends in the middle of a pageblock.
792          * The low boundary is the end of the pageblock the migration scanner
793          * is using.
794          */
795         isolate_start_pfn = cc->free_pfn;
796         block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
797         block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
798                                                 zone_end_pfn(zone));
799         low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
800
801         /*
802          * Isolate free pages until enough are available to migrate the
803          * pages on cc->migratepages. We stop searching if the migrate
804          * and free page scanners meet or enough free pages are isolated.
805          */
806         for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
807                                 block_end_pfn = block_start_pfn,
808                                 block_start_pfn -= pageblock_nr_pages,
809                                 isolate_start_pfn = block_start_pfn) {
810                 unsigned long isolated;
811
812                 /*
813                  * This can iterate a massively long zone without finding any
814                  * suitable migration targets, so periodically check if we need
815                  * to schedule, or even abort async compaction.
816                  */
817                 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
818                                                 && compact_should_abort(cc))
819                         break;
820
821                 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
822                                                                         zone);
823                 if (!page)
824                         continue;
825
826                 /* Check the block is suitable for migration */
827                 if (!suitable_migration_target(page))
828                         continue;
829
830                 /* If isolation recently failed, do not retry */
831                 if (!isolation_suitable(cc, page))
832                         continue;
833
834                 /* Found a block suitable for isolating free pages from. */
835                 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
836                                         block_end_pfn, freelist, false);
837                 nr_freepages += isolated;
838
839                 /*
840                  * Remember where the free scanner should restart next time,
841                  * which is where isolate_freepages_block() left off.
842                  * But if it scanned the whole pageblock, isolate_start_pfn
843                  * now points at block_end_pfn, which is the start of the next
844                  * pageblock.
845                  * In that case we will however want to restart at the start
846                  * of the previous pageblock.
847                  */
848                 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
849                                 isolate_start_pfn :
850                                 block_start_pfn - pageblock_nr_pages;
851
852                 /*
853                  * Set a flag that we successfully isolated in this pageblock.
854                  * In the next loop iteration, zone->compact_cached_free_pfn
855                  * will not be updated and thus it will effectively contain the
856                  * highest pageblock we isolated pages from.
857                  */
858                 if (isolated)
859                         cc->finished_update_free = true;
860
861                 /*
862                  * isolate_freepages_block() might have aborted due to async
863                  * compaction being contended
864                  */
865                 if (cc->contended)
866                         break;
867         }
868
869         /* split_free_page does not map the pages */
870         map_pages(freelist);
871
872         /*
873          * If we crossed the migrate scanner, we want to keep it that way
874          * so that compact_finished() may detect this
875          */
876         if (block_start_pfn < low_pfn)
877                 cc->free_pfn = cc->migrate_pfn;
878
879         cc->nr_freepages = nr_freepages;
880 }
881
882 /*
883  * This is a migrate-callback that "allocates" freepages by taking pages
884  * from the isolated freelists in the block we are migrating to.
885  */
886 static struct page *compaction_alloc(struct page *migratepage,
887                                         unsigned long data,
888                                         int **result)
889 {
890         struct compact_control *cc = (struct compact_control *)data;
891         struct page *freepage;
892
893         /*
894          * Isolate free pages if necessary, and if we are not aborting due to
895          * contention.
896          */
897         if (list_empty(&cc->freepages)) {
898                 if (!cc->contended)
899                         isolate_freepages(cc);
900
901                 if (list_empty(&cc->freepages))
902                         return NULL;
903         }
904
905         freepage = list_entry(cc->freepages.next, struct page, lru);
906         list_del(&freepage->lru);
907         cc->nr_freepages--;
908
909         return freepage;
910 }
911
912 /*
913  * This is a migrate-callback that "frees" freepages back to the isolated
914  * freelist.  All pages on the freelist are from the same zone, so there is no
915  * special handling needed for NUMA.
916  */
917 static void compaction_free(struct page *page, unsigned long data)
918 {
919         struct compact_control *cc = (struct compact_control *)data;
920
921         list_add(&page->lru, &cc->freepages);
922         cc->nr_freepages++;
923 }
924
925 /* possible outcome of isolate_migratepages */
926 typedef enum {
927         ISOLATE_ABORT,          /* Abort compaction now */
928         ISOLATE_NONE,           /* No pages isolated, continue scanning */
929         ISOLATE_SUCCESS,        /* Pages isolated, migrate */
930 } isolate_migrate_t;
931
932 /*
933  * Isolate all pages that can be migrated from the first suitable block,
934  * starting at the block pointed to by the migrate scanner pfn within
935  * compact_control.
936  */
937 static isolate_migrate_t isolate_migratepages(struct zone *zone,
938                                         struct compact_control *cc)
939 {
940         unsigned long low_pfn, end_pfn;
941         struct page *page;
942         const isolate_mode_t isolate_mode =
943                 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
944
945         /*
946          * Start at where we last stopped, or beginning of the zone as
947          * initialized by compact_zone()
948          */
949         low_pfn = cc->migrate_pfn;
950
951         /* Only scan within a pageblock boundary */
952         end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
953
954         /*
955          * Iterate over whole pageblocks until we find the first suitable.
956          * Do not cross the free scanner.
957          */
958         for (; end_pfn <= cc->free_pfn;
959                         low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
960
961                 /*
962                  * This can potentially iterate a massively long zone with
963                  * many pageblocks unsuitable, so periodically check if we
964                  * need to schedule, or even abort async compaction.
965                  */
966                 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
967                                                 && compact_should_abort(cc))
968                         break;
969
970                 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
971                 if (!page)
972                         continue;
973
974                 /* If isolation recently failed, do not retry */
975                 if (!isolation_suitable(cc, page))
976                         continue;
977
978                 /*
979                  * For async compaction, also only scan in MOVABLE blocks.
980                  * Async compaction is optimistic to see if the minimum amount
981                  * of work satisfies the allocation.
982                  */
983                 if (cc->mode == MIGRATE_ASYNC &&
984                     !migrate_async_suitable(get_pageblock_migratetype(page)))
985                         continue;
986
987                 /* Perform the isolation */
988                 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
989                                                                 isolate_mode);
990
991                 if (!low_pfn || cc->contended)
992                         return ISOLATE_ABORT;
993
994                 /*
995                  * Either we isolated something and proceed with migration. Or
996                  * we failed and compact_zone should decide if we should
997                  * continue or not.
998                  */
999                 break;
1000         }
1001
1002         acct_isolated(zone, cc);
1003         /* Record where migration scanner will be restarted */
1004         cc->migrate_pfn = low_pfn;
1005
1006         return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1007 }
1008
1009 static int compact_finished(struct zone *zone,
1010                             struct compact_control *cc)
1011 {
1012         unsigned int order;
1013         unsigned long watermark;
1014
1015         if (cc->contended || fatal_signal_pending(current))
1016                 return COMPACT_PARTIAL;
1017
1018         /* Compaction run completes if the migrate and free scanner meet */
1019         if (cc->free_pfn <= cc->migrate_pfn) {
1020                 /* Let the next compaction start anew. */
1021                 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1022                 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
1023                 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1024
1025                 /*
1026                  * Mark that the PG_migrate_skip information should be cleared
1027                  * by kswapd when it goes to sleep. kswapd does not set the
1028                  * flag itself as the decision to be clear should be directly
1029                  * based on an allocation request.
1030                  */
1031                 if (!current_is_kswapd())
1032                         zone->compact_blockskip_flush = true;
1033
1034                 return COMPACT_COMPLETE;
1035         }
1036
1037         /*
1038          * order == -1 is expected when compacting via
1039          * /proc/sys/vm/compact_memory
1040          */
1041         if (cc->order == -1)
1042                 return COMPACT_CONTINUE;
1043
1044         /* Compaction run is not finished if the watermark is not met */
1045         watermark = low_wmark_pages(zone);
1046         watermark += (1 << cc->order);
1047
1048         if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1049                 return COMPACT_CONTINUE;
1050
1051         /* Direct compactor: Is a suitable page free? */
1052         for (order = cc->order; order < MAX_ORDER; order++) {
1053                 struct free_area *area = &zone->free_area[order];
1054
1055                 /* Job done if page is free of the right migratetype */
1056                 if (!list_empty(&area->free_list[cc->migratetype]))
1057                         return COMPACT_PARTIAL;
1058
1059                 /* Job done if allocation would set block type */
1060                 if (cc->order >= pageblock_order && area->nr_free)
1061                         return COMPACT_PARTIAL;
1062         }
1063
1064         return COMPACT_CONTINUE;
1065 }
1066
1067 /*
1068  * compaction_suitable: Is this suitable to run compaction on this zone now?
1069  * Returns
1070  *   COMPACT_SKIPPED  - If there are too few free pages for compaction
1071  *   COMPACT_PARTIAL  - If the allocation would succeed without compaction
1072  *   COMPACT_CONTINUE - If compaction should run now
1073  */
1074 unsigned long compaction_suitable(struct zone *zone, int order)
1075 {
1076         int fragindex;
1077         unsigned long watermark;
1078
1079         /*
1080          * order == -1 is expected when compacting via
1081          * /proc/sys/vm/compact_memory
1082          */
1083         if (order == -1)
1084                 return COMPACT_CONTINUE;
1085
1086         /*
1087          * Watermarks for order-0 must be met for compaction. Note the 2UL.
1088          * This is because during migration, copies of pages need to be
1089          * allocated and for a short time, the footprint is higher
1090          */
1091         watermark = low_wmark_pages(zone) + (2UL << order);
1092         if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1093                 return COMPACT_SKIPPED;
1094
1095         /*
1096          * fragmentation index determines if allocation failures are due to
1097          * low memory or external fragmentation
1098          *
1099          * index of -1000 implies allocations might succeed depending on
1100          * watermarks
1101          * index towards 0 implies failure is due to lack of memory
1102          * index towards 1000 implies failure is due to fragmentation
1103          *
1104          * Only compact if a failure would be due to fragmentation.
1105          */
1106         fragindex = fragmentation_index(zone, order);
1107         if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1108                 return COMPACT_SKIPPED;
1109
1110         if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1111             0, 0))
1112                 return COMPACT_PARTIAL;
1113
1114         return COMPACT_CONTINUE;
1115 }
1116
1117 static int compact_zone(struct zone *zone, struct compact_control *cc)
1118 {
1119         int ret;
1120         unsigned long start_pfn = zone->zone_start_pfn;
1121         unsigned long end_pfn = zone_end_pfn(zone);
1122         const bool sync = cc->mode != MIGRATE_ASYNC;
1123
1124         ret = compaction_suitable(zone, cc->order);
1125         switch (ret) {
1126         case COMPACT_PARTIAL:
1127         case COMPACT_SKIPPED:
1128                 /* Compaction is likely to fail */
1129                 return ret;
1130         case COMPACT_CONTINUE:
1131                 /* Fall through to compaction */
1132                 ;
1133         }
1134
1135         /*
1136          * Clear pageblock skip if there were failures recently and compaction
1137          * is about to be retried after being deferred. kswapd does not do
1138          * this reset as it'll reset the cached information when going to sleep.
1139          */
1140         if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1141                 __reset_isolation_suitable(zone);
1142
1143         /*
1144          * Setup to move all movable pages to the end of the zone. Used cached
1145          * information on where the scanners should start but check that it
1146          * is initialised by ensuring the values are within zone boundaries.
1147          */
1148         cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1149         cc->free_pfn = zone->compact_cached_free_pfn;
1150         if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1151                 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1152                 zone->compact_cached_free_pfn = cc->free_pfn;
1153         }
1154         if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1155                 cc->migrate_pfn = start_pfn;
1156                 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1157                 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1158         }
1159
1160         trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1161
1162         migrate_prep_local();
1163
1164         while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
1165                 int err;
1166
1167                 switch (isolate_migratepages(zone, cc)) {
1168                 case ISOLATE_ABORT:
1169                         ret = COMPACT_PARTIAL;
1170                         putback_movable_pages(&cc->migratepages);
1171                         cc->nr_migratepages = 0;
1172                         goto out;
1173                 case ISOLATE_NONE:
1174                         continue;
1175                 case ISOLATE_SUCCESS:
1176                         ;
1177                 }
1178
1179                 err = migrate_pages(&cc->migratepages, compaction_alloc,
1180                                 compaction_free, (unsigned long)cc, cc->mode,
1181                                 MR_COMPACTION);
1182
1183                 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1184                                                         &cc->migratepages);
1185
1186                 /* All pages were either migrated or will be released */
1187                 cc->nr_migratepages = 0;
1188                 if (err) {
1189                         putback_movable_pages(&cc->migratepages);
1190                         /*
1191                          * migrate_pages() may return -ENOMEM when scanners meet
1192                          * and we want compact_finished() to detect it
1193                          */
1194                         if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
1195                                 ret = COMPACT_PARTIAL;
1196                                 goto out;
1197                         }
1198                 }
1199         }
1200
1201 out:
1202         /* Release free pages and check accounting */
1203         cc->nr_freepages -= release_freepages(&cc->freepages);
1204         VM_BUG_ON(cc->nr_freepages != 0);
1205
1206         trace_mm_compaction_end(ret);
1207
1208         return ret;
1209 }
1210
1211 static unsigned long compact_zone_order(struct zone *zone, int order,
1212                 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
1213 {
1214         unsigned long ret;
1215         struct compact_control cc = {
1216                 .nr_freepages = 0,
1217                 .nr_migratepages = 0,
1218                 .order = order,
1219                 .migratetype = allocflags_to_migratetype(gfp_mask),
1220                 .zone = zone,
1221                 .mode = mode,
1222         };
1223         INIT_LIST_HEAD(&cc.freepages);
1224         INIT_LIST_HEAD(&cc.migratepages);
1225
1226         ret = compact_zone(zone, &cc);
1227
1228         VM_BUG_ON(!list_empty(&cc.freepages));
1229         VM_BUG_ON(!list_empty(&cc.migratepages));
1230
1231         *contended = cc.contended;
1232         return ret;
1233 }
1234
1235 int sysctl_extfrag_threshold = 500;
1236
1237 /**
1238  * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1239  * @zonelist: The zonelist used for the current allocation
1240  * @order: The order of the current allocation
1241  * @gfp_mask: The GFP mask of the current allocation
1242  * @nodemask: The allowed nodes to allocate from
1243  * @mode: The migration mode for async, sync light, or sync migration
1244  * @contended: Return value that determines if compaction was aborted due to
1245  *             need_resched() or lock contention
1246  * @candidate_zone: Return the zone where we think allocation should succeed
1247  *
1248  * This is the main entry point for direct page compaction.
1249  */
1250 unsigned long try_to_compact_pages(struct zonelist *zonelist,
1251                         int order, gfp_t gfp_mask, nodemask_t *nodemask,
1252                         enum migrate_mode mode, int *contended,
1253                         struct zone **candidate_zone)
1254 {
1255         enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1256         int may_enter_fs = gfp_mask & __GFP_FS;
1257         int may_perform_io = gfp_mask & __GFP_IO;
1258         struct zoneref *z;
1259         struct zone *zone;
1260         int rc = COMPACT_DEFERRED;
1261         int alloc_flags = 0;
1262         int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */
1263
1264         *contended = COMPACT_CONTENDED_NONE;
1265
1266         /* Check if the GFP flags allow compaction */
1267         if (!order || !may_enter_fs || !may_perform_io)
1268                 return COMPACT_SKIPPED;
1269
1270 #ifdef CONFIG_CMA
1271         if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1272                 alloc_flags |= ALLOC_CMA;
1273 #endif
1274         /* Compact each zone in the list */
1275         for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1276                                                                 nodemask) {
1277                 int status;
1278                 int zone_contended;
1279
1280                 if (compaction_deferred(zone, order))
1281                         continue;
1282
1283                 status = compact_zone_order(zone, order, gfp_mask, mode,
1284                                                         &zone_contended);
1285                 rc = max(status, rc);
1286                 /*
1287                  * It takes at least one zone that wasn't lock contended
1288                  * to clear all_zones_contended.
1289                  */
1290                 all_zones_contended &= zone_contended;
1291
1292                 /* If a normal allocation would succeed, stop compacting */
1293                 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1294                                       alloc_flags)) {
1295                         *candidate_zone = zone;
1296                         /*
1297                          * We think the allocation will succeed in this zone,
1298                          * but it is not certain, hence the false. The caller
1299                          * will repeat this with true if allocation indeed
1300                          * succeeds in this zone.
1301                          */
1302                         compaction_defer_reset(zone, order, false);
1303                         /*
1304                          * It is possible that async compaction aborted due to
1305                          * need_resched() and the watermarks were ok thanks to
1306                          * somebody else freeing memory. The allocation can
1307                          * however still fail so we better signal the
1308                          * need_resched() contention anyway (this will not
1309                          * prevent the allocation attempt).
1310                          */
1311                         if (zone_contended == COMPACT_CONTENDED_SCHED)
1312                                 *contended = COMPACT_CONTENDED_SCHED;
1313
1314                         goto break_loop;
1315                 }
1316
1317                 if (mode != MIGRATE_ASYNC) {
1318                         /*
1319                          * We think that allocation won't succeed in this zone
1320                          * so we defer compaction there. If it ends up
1321                          * succeeding after all, it will be reset.
1322                          */
1323                         defer_compaction(zone, order);
1324                 }
1325
1326                 /*
1327                  * We might have stopped compacting due to need_resched() in
1328                  * async compaction, or due to a fatal signal detected. In that
1329                  * case do not try further zones and signal need_resched()
1330                  * contention.
1331                  */
1332                 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1333                                         || fatal_signal_pending(current)) {
1334                         *contended = COMPACT_CONTENDED_SCHED;
1335                         goto break_loop;
1336                 }
1337
1338                 continue;
1339 break_loop:
1340                 /*
1341                  * We might not have tried all the zones, so  be conservative
1342                  * and assume they are not all lock contended.
1343                  */
1344                 all_zones_contended = 0;
1345                 break;
1346         }
1347
1348         /*
1349          * If at least one zone wasn't deferred or skipped, we report if all
1350          * zones that were tried were lock contended.
1351          */
1352         if (rc > COMPACT_SKIPPED && all_zones_contended)
1353                 *contended = COMPACT_CONTENDED_LOCK;
1354
1355         return rc;
1356 }
1357
1358
1359 /* Compact all zones within a node */
1360 static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1361 {
1362         int zoneid;
1363         struct zone *zone;
1364
1365         for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1366
1367                 zone = &pgdat->node_zones[zoneid];
1368                 if (!populated_zone(zone))
1369                         continue;
1370
1371                 cc->nr_freepages = 0;
1372                 cc->nr_migratepages = 0;
1373                 cc->zone = zone;
1374                 INIT_LIST_HEAD(&cc->freepages);
1375                 INIT_LIST_HEAD(&cc->migratepages);
1376
1377                 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1378                         compact_zone(zone, cc);
1379
1380                 if (cc->order > 0) {
1381                         if (zone_watermark_ok(zone, cc->order,
1382                                                 low_wmark_pages(zone), 0, 0))
1383                                 compaction_defer_reset(zone, cc->order, false);
1384                 }
1385
1386                 VM_BUG_ON(!list_empty(&cc->freepages));
1387                 VM_BUG_ON(!list_empty(&cc->migratepages));
1388         }
1389 }
1390
1391 void compact_pgdat(pg_data_t *pgdat, int order)
1392 {
1393         struct compact_control cc = {
1394                 .order = order,
1395                 .mode = MIGRATE_ASYNC,
1396         };
1397
1398         if (!order)
1399                 return;
1400
1401         __compact_pgdat(pgdat, &cc);
1402 }
1403
1404 static void compact_node(int nid)
1405 {
1406         struct compact_control cc = {
1407                 .order = -1,
1408                 .mode = MIGRATE_SYNC,
1409                 .ignore_skip_hint = true,
1410         };
1411
1412         __compact_pgdat(NODE_DATA(nid), &cc);
1413 }
1414
1415 /* Compact all nodes in the system */
1416 static void compact_nodes(void)
1417 {
1418         int nid;
1419
1420         /* Flush pending updates to the LRU lists */
1421         lru_add_drain_all();
1422
1423         for_each_online_node(nid)
1424                 compact_node(nid);
1425 }
1426
1427 /* The written value is actually unused, all memory is compacted */
1428 int sysctl_compact_memory;
1429
1430 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1431 int sysctl_compaction_handler(struct ctl_table *table, int write,
1432                         void __user *buffer, size_t *length, loff_t *ppos)
1433 {
1434         if (write)
1435                 compact_nodes();
1436
1437         return 0;
1438 }
1439
1440 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1441                         void __user *buffer, size_t *length, loff_t *ppos)
1442 {
1443         proc_dointvec_minmax(table, write, buffer, length, ppos);
1444
1445         return 0;
1446 }
1447
1448 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1449 static ssize_t sysfs_compact_node(struct device *dev,
1450                         struct device_attribute *attr,
1451                         const char *buf, size_t count)
1452 {
1453         int nid = dev->id;
1454
1455         if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1456                 /* Flush pending updates to the LRU lists */
1457                 lru_add_drain_all();
1458
1459                 compact_node(nid);
1460         }
1461
1462         return count;
1463 }
1464 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1465
1466 int compaction_register_node(struct node *node)
1467 {
1468         return device_create_file(&node->dev, &dev_attr_compact);
1469 }
1470
1471 void compaction_unregister_node(struct node *node)
1472 {
1473         return device_remove_file(&node->dev, &dev_attr_compact);
1474 }
1475 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1476
1477 #endif /* CONFIG_COMPACTION */
This page took 0.119069 seconds and 4 git commands to generate.