2 * linux/mm/compaction.c
4 * Memory compaction for the reduction of external fragmentation. Note that
5 * this heavily depends upon page migration to do all the real heavy
8 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
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>
20 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/compaction.h>
25 static unsigned long release_freepages(struct list_head *freelist)
27 struct page *page, *next;
28 unsigned long count = 0;
30 list_for_each_entry_safe(page, next, freelist, lru) {
39 static void map_pages(struct list_head *list)
43 list_for_each_entry(page, list, lru) {
44 arch_alloc_page(page, 0);
45 kernel_map_pages(page, 1, 1);
49 static inline bool migrate_async_suitable(int migratetype)
51 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
54 #ifdef CONFIG_COMPACTION
55 /* Returns true if the pageblock should be scanned for pages to isolate. */
56 static inline bool isolation_suitable(struct compact_control *cc,
59 if (cc->ignore_skip_hint)
62 return !get_pageblock_skip(page);
66 * This function is called to clear all cached information on pageblocks that
67 * should be skipped for page isolation when the migrate and free page scanner
70 static void __reset_isolation_suitable(struct zone *zone)
72 unsigned long start_pfn = zone->zone_start_pfn;
73 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
76 zone->compact_cached_migrate_pfn = start_pfn;
77 zone->compact_cached_free_pfn = end_pfn;
78 zone->compact_blockskip_flush = false;
80 /* Walk the zone and mark every pageblock as suitable for isolation */
81 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
89 page = pfn_to_page(pfn);
90 if (zone != page_zone(page))
93 clear_pageblock_skip(page);
97 void reset_isolation_suitable(pg_data_t *pgdat)
101 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
102 struct zone *zone = &pgdat->node_zones[zoneid];
103 if (!populated_zone(zone))
106 /* Only flush if a full compaction finished recently */
107 if (zone->compact_blockskip_flush)
108 __reset_isolation_suitable(zone);
113 * If no pages were isolated then mark this pageblock to be skipped in the
114 * future. The information is later cleared by __reset_isolation_suitable().
116 static void update_pageblock_skip(struct compact_control *cc,
117 struct page *page, unsigned long nr_isolated,
118 bool migrate_scanner)
120 struct zone *zone = cc->zone;
125 unsigned long pfn = page_to_pfn(page);
126 set_pageblock_skip(page);
128 /* Update where compaction should restart */
129 if (migrate_scanner) {
130 if (!cc->finished_update_migrate &&
131 pfn > zone->compact_cached_migrate_pfn)
132 zone->compact_cached_migrate_pfn = pfn;
134 if (!cc->finished_update_free &&
135 pfn < zone->compact_cached_free_pfn)
136 zone->compact_cached_free_pfn = pfn;
141 static inline bool isolation_suitable(struct compact_control *cc,
147 static void update_pageblock_skip(struct compact_control *cc,
148 struct page *page, unsigned long nr_isolated,
149 bool migrate_scanner)
152 #endif /* CONFIG_COMPACTION */
154 static inline bool should_release_lock(spinlock_t *lock)
156 return need_resched() || spin_is_contended(lock);
160 * Compaction requires the taking of some coarse locks that are potentially
161 * very heavily contended. Check if the process needs to be scheduled or
162 * if the lock is contended. For async compaction, back out in the event
163 * if contention is severe. For sync compaction, schedule.
165 * Returns true if the lock is held.
166 * Returns false if the lock is released and compaction should abort
168 static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
169 bool locked, struct compact_control *cc)
171 if (should_release_lock(lock)) {
173 spin_unlock_irqrestore(lock, *flags);
177 /* async aborts if taking too long or contended */
179 cc->contended = true;
187 spin_lock_irqsave(lock, *flags);
191 static inline bool compact_trylock_irqsave(spinlock_t *lock,
192 unsigned long *flags, struct compact_control *cc)
194 return compact_checklock_irqsave(lock, flags, false, cc);
197 /* Returns true if the page is within a block suitable for migration to */
198 static bool suitable_migration_target(struct page *page)
200 int migratetype = get_pageblock_migratetype(page);
202 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
203 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
206 /* If the page is a large free page, then allow migration */
207 if (PageBuddy(page) && page_order(page) >= pageblock_order)
210 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
211 if (migrate_async_suitable(migratetype))
214 /* Otherwise skip the block */
218 static void compact_capture_page(struct compact_control *cc)
221 int mtype, mtype_low, mtype_high;
223 if (!cc->page || *cc->page)
227 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
228 * regardless of the migratetype of the freelist is is captured from.
229 * This is fine because the order for a high-order MIGRATE_MOVABLE
230 * allocation is typically at least a pageblock size and overall
231 * fragmentation is not impaired. Other allocation types must
232 * capture pages from their own migratelist because otherwise they
233 * could pollute other pageblocks like MIGRATE_MOVABLE with
234 * difficult to move pages and making fragmentation worse overall.
236 if (cc->migratetype == MIGRATE_MOVABLE) {
238 mtype_high = MIGRATE_PCPTYPES;
240 mtype_low = cc->migratetype;
241 mtype_high = cc->migratetype + 1;
244 /* Speculatively examine the free lists without zone lock */
245 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
247 for (order = cc->order; order < MAX_ORDER; order++) {
249 struct free_area *area;
250 area = &(cc->zone->free_area[order]);
251 if (list_empty(&area->free_list[mtype]))
254 /* Take the lock and attempt capture of the page */
255 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
257 if (!list_empty(&area->free_list[mtype])) {
258 page = list_entry(area->free_list[mtype].next,
260 if (capture_free_page(page, cc->order, mtype)) {
261 spin_unlock_irqrestore(&cc->zone->lock,
267 spin_unlock_irqrestore(&cc->zone->lock, flags);
273 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
274 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
275 * pages inside of the pageblock (even though it may still end up isolating
278 static unsigned long isolate_freepages_block(struct compact_control *cc,
279 unsigned long blockpfn,
280 unsigned long end_pfn,
281 struct list_head *freelist,
284 int nr_scanned = 0, total_isolated = 0;
285 struct page *cursor, *valid_page = NULL;
286 unsigned long nr_strict_required = end_pfn - blockpfn;
290 cursor = pfn_to_page(blockpfn);
292 /* Isolate free pages. */
293 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
295 struct page *page = cursor;
298 if (!pfn_valid_within(blockpfn))
302 if (!PageBuddy(page))
306 * The zone lock must be held to isolate freepages.
307 * Unfortunately this is a very coarse lock and can be
308 * heavily contended if there are parallel allocations
309 * or parallel compactions. For async compaction do not
310 * spin on the lock and we acquire the lock as late as
313 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
318 /* Recheck this is a suitable migration target under lock */
319 if (!strict && !suitable_migration_target(page))
322 /* Recheck this is a buddy page under lock */
323 if (!PageBuddy(page))
326 /* Found a free page, break it into order-0 pages */
327 isolated = split_free_page(page);
328 if (!isolated && strict)
330 total_isolated += isolated;
331 for (i = 0; i < isolated; i++) {
332 list_add(&page->lru, freelist);
336 /* If a page was split, advance to the end of it */
338 blockpfn += isolated - 1;
339 cursor += isolated - 1;
343 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
346 * If strict isolation is requested by CMA then check that all the
347 * pages requested were isolated. If there were any failures, 0 is
348 * returned and CMA will fail.
350 if (strict && nr_strict_required > total_isolated)
354 spin_unlock_irqrestore(&cc->zone->lock, flags);
356 /* Update the pageblock-skip if the whole pageblock was scanned */
357 if (blockpfn == end_pfn)
358 update_pageblock_skip(cc, valid_page, total_isolated, false);
360 return total_isolated;
364 * isolate_freepages_range() - isolate free pages.
365 * @start_pfn: The first PFN to start isolating.
366 * @end_pfn: The one-past-last PFN.
368 * Non-free pages, invalid PFNs, or zone boundaries within the
369 * [start_pfn, end_pfn) range are considered errors, cause function to
370 * undo its actions and return zero.
372 * Otherwise, function returns one-past-the-last PFN of isolated page
373 * (which may be greater then end_pfn if end fell in a middle of
377 isolate_freepages_range(struct compact_control *cc,
378 unsigned long start_pfn, unsigned long end_pfn)
380 unsigned long isolated, pfn, block_end_pfn;
383 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
384 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
388 * On subsequent iterations ALIGN() is actually not needed,
389 * but we keep it that we not to complicate the code.
391 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
392 block_end_pfn = min(block_end_pfn, end_pfn);
394 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
398 * In strict mode, isolate_freepages_block() returns 0 if
399 * there are any holes in the block (ie. invalid PFNs or
406 * If we managed to isolate pages, it is always (1 << n) *
407 * pageblock_nr_pages for some non-negative n. (Max order
408 * page may span two pageblocks).
412 /* split_free_page does not map the pages */
413 map_pages(&freelist);
416 /* Loop terminated early, cleanup. */
417 release_freepages(&freelist);
421 /* We don't use freelists for anything. */
425 /* Update the number of anon and file isolated pages in the zone */
426 static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
429 unsigned int count[2] = { 0, };
431 list_for_each_entry(page, &cc->migratepages, lru)
432 count[!!page_is_file_cache(page)]++;
434 /* If locked we can use the interrupt unsafe versions */
436 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
437 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
439 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
440 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
444 /* Similar to reclaim, but different enough that they don't share logic */
445 static bool too_many_isolated(struct zone *zone)
447 unsigned long active, inactive, isolated;
449 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
450 zone_page_state(zone, NR_INACTIVE_ANON);
451 active = zone_page_state(zone, NR_ACTIVE_FILE) +
452 zone_page_state(zone, NR_ACTIVE_ANON);
453 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
454 zone_page_state(zone, NR_ISOLATED_ANON);
456 return isolated > (inactive + active) / 2;
460 * isolate_migratepages_range() - isolate all migrate-able pages in range.
461 * @zone: Zone pages are in.
462 * @cc: Compaction control structure.
463 * @low_pfn: The first PFN of the range.
464 * @end_pfn: The one-past-the-last PFN of the range.
465 * @unevictable: true if it allows to isolate unevictable pages
467 * Isolate all pages that can be migrated from the range specified by
468 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
469 * pending), otherwise PFN of the first page that was not scanned
470 * (which may be both less, equal to or more then end_pfn).
472 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
475 * Apart from cc->migratepages and cc->nr_migratetypes this function
476 * does not modify any cc's fields, in particular it does not modify
477 * (or read for that matter) cc->migrate_pfn.
480 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
481 unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
483 unsigned long last_pageblock_nr = 0, pageblock_nr;
484 unsigned long nr_scanned = 0, nr_isolated = 0;
485 struct list_head *migratelist = &cc->migratepages;
486 isolate_mode_t mode = 0;
487 struct lruvec *lruvec;
490 struct page *page = NULL, *valid_page = NULL;
493 * Ensure that there are not too many pages isolated from the LRU
494 * list by either parallel reclaimers or compaction. If there are,
495 * delay for some time until fewer pages are isolated
497 while (unlikely(too_many_isolated(zone))) {
498 /* async migration should just abort */
502 congestion_wait(BLK_RW_ASYNC, HZ/10);
504 if (fatal_signal_pending(current))
508 /* Time to isolate some pages for migration */
510 for (; low_pfn < end_pfn; low_pfn++) {
511 /* give a chance to irqs before checking need_resched() */
512 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
513 if (should_release_lock(&zone->lru_lock)) {
514 spin_unlock_irqrestore(&zone->lru_lock, flags);
520 * migrate_pfn does not necessarily start aligned to a
521 * pageblock. Ensure that pfn_valid is called when moving
522 * into a new MAX_ORDER_NR_PAGES range in case of large
523 * memory holes within the zone
525 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
526 if (!pfn_valid(low_pfn)) {
527 low_pfn += MAX_ORDER_NR_PAGES - 1;
532 if (!pfn_valid_within(low_pfn))
537 * Get the page and ensure the page is within the same zone.
538 * See the comment in isolate_freepages about overlapping
539 * nodes. It is deliberate that the new zone lock is not taken
540 * as memory compaction should not move pages between nodes.
542 page = pfn_to_page(low_pfn);
543 if (page_zone(page) != zone)
549 /* If isolation recently failed, do not retry */
550 pageblock_nr = low_pfn >> pageblock_order;
551 if (!isolation_suitable(cc, page))
559 * For async migration, also only scan in MOVABLE blocks. Async
560 * migration is optimistic to see if the minimum amount of work
561 * satisfies the allocation
563 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
564 !migrate_async_suitable(get_pageblock_migratetype(page))) {
565 cc->finished_update_migrate = true;
570 * Check may be lockless but that's ok as we recheck later.
571 * It's possible to migrate LRU pages and balloon pages
572 * Skip any other type of page
574 if (!PageLRU(page)) {
575 if (unlikely(balloon_page_movable(page))) {
576 if (locked && balloon_page_isolate(page)) {
577 /* Successfully isolated */
578 cc->finished_update_migrate = true;
579 list_add(&page->lru, migratelist);
580 cc->nr_migratepages++;
582 goto check_compact_cluster;
589 * PageLRU is set. lru_lock normally excludes isolation
590 * splitting and collapsing (collapsing has already happened
591 * if PageLRU is set) but the lock is not necessarily taken
592 * here and it is wasteful to take it just to check transhuge.
593 * Check TransHuge without lock and skip the whole pageblock if
594 * it's either a transhuge or hugetlbfs page, as calling
595 * compound_order() without preventing THP from splitting the
596 * page underneath us may return surprising results.
598 if (PageTransHuge(page)) {
601 low_pfn += (1 << compound_order(page)) - 1;
605 /* Check if it is ok to still hold the lock */
606 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
608 if (!locked || fatal_signal_pending(current))
611 /* Recheck PageLRU and PageTransHuge under lock */
614 if (PageTransHuge(page)) {
615 low_pfn += (1 << compound_order(page)) - 1;
620 mode |= ISOLATE_ASYNC_MIGRATE;
623 mode |= ISOLATE_UNEVICTABLE;
625 lruvec = mem_cgroup_page_lruvec(page, zone);
627 /* Try isolate the page */
628 if (__isolate_lru_page(page, mode) != 0)
631 VM_BUG_ON(PageTransCompound(page));
633 /* Successfully isolated */
634 cc->finished_update_migrate = true;
635 del_page_from_lru_list(page, lruvec, page_lru(page));
636 list_add(&page->lru, migratelist);
637 cc->nr_migratepages++;
640 check_compact_cluster:
641 /* Avoid isolating too much */
642 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
650 low_pfn += pageblock_nr_pages;
651 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
652 last_pageblock_nr = pageblock_nr;
655 acct_isolated(zone, locked, cc);
658 spin_unlock_irqrestore(&zone->lru_lock, flags);
660 /* Update the pageblock-skip if the whole pageblock was scanned */
661 if (low_pfn == end_pfn)
662 update_pageblock_skip(cc, valid_page, nr_isolated, true);
664 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
669 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
670 #ifdef CONFIG_COMPACTION
672 * Based on information in the current compact_control, find blocks
673 * suitable for isolating free pages from and then isolate them.
675 static void isolate_freepages(struct zone *zone,
676 struct compact_control *cc)
679 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
680 int nr_freepages = cc->nr_freepages;
681 struct list_head *freelist = &cc->freepages;
684 * Initialise the free scanner. The starting point is where we last
685 * scanned from (or the end of the zone if starting). The low point
686 * is the end of the pageblock the migration scanner is using.
689 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
692 * Take care that if the migration scanner is at the end of the zone
693 * that the free scanner does not accidentally move to the next zone
694 * in the next isolation cycle.
696 high_pfn = min(low_pfn, pfn);
698 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
701 * Isolate free pages until enough are available to migrate the
702 * pages on cc->migratepages. We stop searching if the migrate
703 * and free page scanners meet or enough free pages are isolated.
705 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
706 pfn -= pageblock_nr_pages) {
707 unsigned long isolated;
713 * Check for overlapping nodes/zones. It's possible on some
714 * configurations to have a setup like
716 * i.e. it's possible that all pages within a zones range of
717 * pages do not belong to a single zone.
719 page = pfn_to_page(pfn);
720 if (page_zone(page) != zone)
723 /* Check the block is suitable for migration */
724 if (!suitable_migration_target(page))
727 /* If isolation recently failed, do not retry */
728 if (!isolation_suitable(cc, page))
731 /* Found a block suitable for isolating free pages from */
735 * As pfn may not start aligned, pfn+pageblock_nr_page
736 * may cross a MAX_ORDER_NR_PAGES boundary and miss
737 * a pfn_valid check. Ensure isolate_freepages_block()
738 * only scans within a pageblock
740 end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
741 end_pfn = min(end_pfn, zone_end_pfn);
742 isolated = isolate_freepages_block(cc, pfn, end_pfn,
744 nr_freepages += isolated;
747 * Record the highest PFN we isolated pages from. When next
748 * looking for free pages, the search will restart here as
749 * page migration may have returned some pages to the allocator
752 cc->finished_update_free = true;
753 high_pfn = max(high_pfn, pfn);
757 /* split_free_page does not map the pages */
760 cc->free_pfn = high_pfn;
761 cc->nr_freepages = nr_freepages;
765 * This is a migrate-callback that "allocates" freepages by taking pages
766 * from the isolated freelists in the block we are migrating to.
768 static struct page *compaction_alloc(struct page *migratepage,
772 struct compact_control *cc = (struct compact_control *)data;
773 struct page *freepage;
775 /* Isolate free pages if necessary */
776 if (list_empty(&cc->freepages)) {
777 isolate_freepages(cc->zone, cc);
779 if (list_empty(&cc->freepages))
783 freepage = list_entry(cc->freepages.next, struct page, lru);
784 list_del(&freepage->lru);
791 * We cannot control nr_migratepages and nr_freepages fully when migration is
792 * running as migrate_pages() has no knowledge of compact_control. When
793 * migration is complete, we count the number of pages on the lists by hand.
795 static void update_nr_listpages(struct compact_control *cc)
797 int nr_migratepages = 0;
798 int nr_freepages = 0;
801 list_for_each_entry(page, &cc->migratepages, lru)
803 list_for_each_entry(page, &cc->freepages, lru)
806 cc->nr_migratepages = nr_migratepages;
807 cc->nr_freepages = nr_freepages;
810 /* possible outcome of isolate_migratepages */
812 ISOLATE_ABORT, /* Abort compaction now */
813 ISOLATE_NONE, /* No pages isolated, continue scanning */
814 ISOLATE_SUCCESS, /* Pages isolated, migrate */
818 * Isolate all pages that can be migrated from the block pointed to by
819 * the migrate scanner within compact_control.
821 static isolate_migrate_t isolate_migratepages(struct zone *zone,
822 struct compact_control *cc)
824 unsigned long low_pfn, end_pfn;
826 /* Do not scan outside zone boundaries */
827 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
829 /* Only scan within a pageblock boundary */
830 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
832 /* Do not cross the free scanner or scan within a memory hole */
833 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
834 cc->migrate_pfn = end_pfn;
838 /* Perform the isolation */
839 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
840 if (!low_pfn || cc->contended)
841 return ISOLATE_ABORT;
843 cc->migrate_pfn = low_pfn;
845 return ISOLATE_SUCCESS;
848 static int compact_finished(struct zone *zone,
849 struct compact_control *cc)
851 unsigned long watermark;
853 if (fatal_signal_pending(current))
854 return COMPACT_PARTIAL;
856 /* Compaction run completes if the migrate and free scanner meet */
857 if (cc->free_pfn <= cc->migrate_pfn) {
859 * Mark that the PG_migrate_skip information should be cleared
860 * by kswapd when it goes to sleep. kswapd does not set the
861 * flag itself as the decision to be clear should be directly
862 * based on an allocation request.
864 if (!current_is_kswapd())
865 zone->compact_blockskip_flush = true;
867 return COMPACT_COMPLETE;
871 * order == -1 is expected when compacting via
872 * /proc/sys/vm/compact_memory
875 return COMPACT_CONTINUE;
877 /* Compaction run is not finished if the watermark is not met */
878 watermark = low_wmark_pages(zone);
879 watermark += (1 << cc->order);
881 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
882 return COMPACT_CONTINUE;
884 /* Direct compactor: Is a suitable page free? */
886 /* Was a suitable page captured? */
888 return COMPACT_PARTIAL;
891 for (order = cc->order; order < MAX_ORDER; order++) {
892 struct free_area *area = &zone->free_area[cc->order];
893 /* Job done if page is free of the right migratetype */
894 if (!list_empty(&area->free_list[cc->migratetype]))
895 return COMPACT_PARTIAL;
897 /* Job done if allocation would set block type */
898 if (cc->order >= pageblock_order && area->nr_free)
899 return COMPACT_PARTIAL;
903 return COMPACT_CONTINUE;
907 * compaction_suitable: Is this suitable to run compaction on this zone now?
909 * COMPACT_SKIPPED - If there are too few free pages for compaction
910 * COMPACT_PARTIAL - If the allocation would succeed without compaction
911 * COMPACT_CONTINUE - If compaction should run now
913 unsigned long compaction_suitable(struct zone *zone, int order)
916 unsigned long watermark;
919 * order == -1 is expected when compacting via
920 * /proc/sys/vm/compact_memory
923 return COMPACT_CONTINUE;
926 * Watermarks for order-0 must be met for compaction. Note the 2UL.
927 * This is because during migration, copies of pages need to be
928 * allocated and for a short time, the footprint is higher
930 watermark = low_wmark_pages(zone) + (2UL << order);
931 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
932 return COMPACT_SKIPPED;
935 * fragmentation index determines if allocation failures are due to
936 * low memory or external fragmentation
938 * index of -1000 implies allocations might succeed depending on
940 * index towards 0 implies failure is due to lack of memory
941 * index towards 1000 implies failure is due to fragmentation
943 * Only compact if a failure would be due to fragmentation.
945 fragindex = fragmentation_index(zone, order);
946 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
947 return COMPACT_SKIPPED;
949 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
951 return COMPACT_PARTIAL;
953 return COMPACT_CONTINUE;
956 static int compact_zone(struct zone *zone, struct compact_control *cc)
959 unsigned long start_pfn = zone->zone_start_pfn;
960 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
962 ret = compaction_suitable(zone, cc->order);
964 case COMPACT_PARTIAL:
965 case COMPACT_SKIPPED:
966 /* Compaction is likely to fail */
968 case COMPACT_CONTINUE:
969 /* Fall through to compaction */
974 * Setup to move all movable pages to the end of the zone. Used cached
975 * information on where the scanners should start but check that it
976 * is initialised by ensuring the values are within zone boundaries.
978 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
979 cc->free_pfn = zone->compact_cached_free_pfn;
980 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
981 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
982 zone->compact_cached_free_pfn = cc->free_pfn;
984 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
985 cc->migrate_pfn = start_pfn;
986 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
990 * Clear pageblock skip if there were failures recently and compaction
991 * is about to be retried after being deferred. kswapd does not do
992 * this reset as it'll reset the cached information when going to sleep.
994 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
995 __reset_isolation_suitable(zone);
997 migrate_prep_local();
999 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
1000 unsigned long nr_migrate, nr_remaining;
1003 switch (isolate_migratepages(zone, cc)) {
1005 ret = COMPACT_PARTIAL;
1006 putback_lru_pages(&cc->migratepages);
1007 cc->nr_migratepages = 0;
1011 case ISOLATE_SUCCESS:
1015 nr_migrate = cc->nr_migratepages;
1016 err = migrate_pages(&cc->migratepages, compaction_alloc,
1017 (unsigned long)cc, false,
1018 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
1019 update_nr_listpages(cc);
1020 nr_remaining = cc->nr_migratepages;
1022 count_vm_event(COMPACTBLOCKS);
1023 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
1025 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
1026 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
1029 /* Release LRU pages not migrated */
1031 putback_lru_pages(&cc->migratepages);
1032 cc->nr_migratepages = 0;
1033 if (err == -ENOMEM) {
1034 ret = COMPACT_PARTIAL;
1039 /* Capture a page now if it is a suitable size */
1040 compact_capture_page(cc);
1044 /* Release free pages and check accounting */
1045 cc->nr_freepages -= release_freepages(&cc->freepages);
1046 VM_BUG_ON(cc->nr_freepages != 0);
1051 static unsigned long compact_zone_order(struct zone *zone,
1052 int order, gfp_t gfp_mask,
1053 bool sync, bool *contended,
1057 struct compact_control cc = {
1059 .nr_migratepages = 0,
1061 .migratetype = allocflags_to_migratetype(gfp_mask),
1066 INIT_LIST_HEAD(&cc.freepages);
1067 INIT_LIST_HEAD(&cc.migratepages);
1069 ret = compact_zone(zone, &cc);
1071 VM_BUG_ON(!list_empty(&cc.freepages));
1072 VM_BUG_ON(!list_empty(&cc.migratepages));
1074 *contended = cc.contended;
1078 int sysctl_extfrag_threshold = 500;
1081 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1082 * @zonelist: The zonelist used for the current allocation
1083 * @order: The order of the current allocation
1084 * @gfp_mask: The GFP mask of the current allocation
1085 * @nodemask: The allowed nodes to allocate from
1086 * @sync: Whether migration is synchronous or not
1087 * @contended: Return value that is true if compaction was aborted due to lock contention
1088 * @page: Optionally capture a free page of the requested order during compaction
1090 * This is the main entry point for direct page compaction.
1092 unsigned long try_to_compact_pages(struct zonelist *zonelist,
1093 int order, gfp_t gfp_mask, nodemask_t *nodemask,
1094 bool sync, bool *contended, struct page **page)
1096 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1097 int may_enter_fs = gfp_mask & __GFP_FS;
1098 int may_perform_io = gfp_mask & __GFP_IO;
1101 int rc = COMPACT_SKIPPED;
1102 int alloc_flags = 0;
1104 /* Check if the GFP flags allow compaction */
1105 if (!order || !may_enter_fs || !may_perform_io)
1108 count_vm_event(COMPACTSTALL);
1111 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1112 alloc_flags |= ALLOC_CMA;
1114 /* Compact each zone in the list */
1115 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1119 status = compact_zone_order(zone, order, gfp_mask, sync,
1121 rc = max(status, rc);
1123 /* If a normal allocation would succeed, stop compacting */
1124 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1133 /* Compact all zones within a node */
1134 static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1139 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1141 zone = &pgdat->node_zones[zoneid];
1142 if (!populated_zone(zone))
1145 cc->nr_freepages = 0;
1146 cc->nr_migratepages = 0;
1148 INIT_LIST_HEAD(&cc->freepages);
1149 INIT_LIST_HEAD(&cc->migratepages);
1151 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1152 compact_zone(zone, cc);
1154 if (cc->order > 0) {
1155 int ok = zone_watermark_ok(zone, cc->order,
1156 low_wmark_pages(zone), 0, 0);
1157 if (ok && cc->order >= zone->compact_order_failed)
1158 zone->compact_order_failed = cc->order + 1;
1159 /* Currently async compaction is never deferred. */
1160 else if (!ok && cc->sync)
1161 defer_compaction(zone, cc->order);
1164 VM_BUG_ON(!list_empty(&cc->freepages));
1165 VM_BUG_ON(!list_empty(&cc->migratepages));
1171 int compact_pgdat(pg_data_t *pgdat, int order)
1173 struct compact_control cc = {
1179 return __compact_pgdat(pgdat, &cc);
1182 static int compact_node(int nid)
1184 struct compact_control cc = {
1190 return __compact_pgdat(NODE_DATA(nid), &cc);
1193 /* Compact all nodes in the system */
1194 static int compact_nodes(void)
1198 /* Flush pending updates to the LRU lists */
1199 lru_add_drain_all();
1201 for_each_online_node(nid)
1204 return COMPACT_COMPLETE;
1207 /* The written value is actually unused, all memory is compacted */
1208 int sysctl_compact_memory;
1210 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1211 int sysctl_compaction_handler(struct ctl_table *table, int write,
1212 void __user *buffer, size_t *length, loff_t *ppos)
1215 return compact_nodes();
1220 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1221 void __user *buffer, size_t *length, loff_t *ppos)
1223 proc_dointvec_minmax(table, write, buffer, length, ppos);
1228 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1229 ssize_t sysfs_compact_node(struct device *dev,
1230 struct device_attribute *attr,
1231 const char *buf, size_t count)
1235 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1236 /* Flush pending updates to the LRU lists */
1237 lru_add_drain_all();
1244 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1246 int compaction_register_node(struct node *node)
1248 return device_create_file(&node->dev, &dev_attr_compact);
1251 void compaction_unregister_node(struct node *node)
1253 return device_remove_file(&node->dev, &dev_attr_compact);
1255 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1257 #endif /* CONFIG_COMPACTION */