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>
19 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/compaction.h>
24 static unsigned long release_freepages(struct list_head *freelist)
26 struct page *page, *next;
27 unsigned long count = 0;
29 list_for_each_entry_safe(page, next, freelist, lru) {
38 static void map_pages(struct list_head *list)
42 list_for_each_entry(page, list, lru) {
43 arch_alloc_page(page, 0);
44 kernel_map_pages(page, 1, 1);
48 static inline bool migrate_async_suitable(int migratetype)
50 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
53 #ifdef CONFIG_COMPACTION
54 /* Returns true if the pageblock should be scanned for pages to isolate. */
55 static inline bool isolation_suitable(struct compact_control *cc,
58 if (cc->ignore_skip_hint)
61 return !get_pageblock_skip(page);
65 * This function is called to clear all cached information on pageblocks that
66 * should be skipped for page isolation when the migrate and free page scanner
69 static void reset_isolation_suitable(struct zone *zone)
71 unsigned long start_pfn = zone->zone_start_pfn;
72 unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
76 * Do not reset more than once every five seconds. If allocations are
77 * failing sufficiently quickly to allow this to happen then continually
78 * scanning for compaction is not going to help. The choice of five
79 * seconds is arbitrary but will mitigate excessive scanning.
81 if (time_before(jiffies, zone->compact_blockskip_expire))
83 zone->compact_blockskip_expire = jiffies + (HZ * 5);
85 /* Walk the zone and mark every pageblock as suitable for isolation */
86 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
94 page = pfn_to_page(pfn);
95 if (zone != page_zone(page))
98 clear_pageblock_skip(page);
103 * If no pages were isolated then mark this pageblock to be skipped in the
104 * future. The information is later cleared by reset_isolation_suitable().
106 static void update_pageblock_skip(struct page *page, unsigned long nr_isolated)
112 set_pageblock_skip(page);
115 static inline bool isolation_suitable(struct compact_control *cc,
121 static void update_pageblock_skip(struct page *page, unsigned long nr_isolated)
124 #endif /* CONFIG_COMPACTION */
126 static inline bool should_release_lock(spinlock_t *lock)
128 return need_resched() || spin_is_contended(lock);
132 * Compaction requires the taking of some coarse locks that are potentially
133 * very heavily contended. Check if the process needs to be scheduled or
134 * if the lock is contended. For async compaction, back out in the event
135 * if contention is severe. For sync compaction, schedule.
137 * Returns true if the lock is held.
138 * Returns false if the lock is released and compaction should abort
140 static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags,
141 bool locked, struct compact_control *cc)
143 if (should_release_lock(lock)) {
145 spin_unlock_irqrestore(lock, *flags);
149 /* async aborts if taking too long or contended */
151 cc->contended = true;
159 spin_lock_irqsave(lock, *flags);
163 static inline bool compact_trylock_irqsave(spinlock_t *lock,
164 unsigned long *flags, struct compact_control *cc)
166 return compact_checklock_irqsave(lock, flags, false, cc);
169 /* Returns true if the page is within a block suitable for migration to */
170 static bool suitable_migration_target(struct page *page)
172 int migratetype = get_pageblock_migratetype(page);
174 /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
175 if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
178 /* If the page is a large free page, then allow migration */
179 if (PageBuddy(page) && page_order(page) >= pageblock_order)
182 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
183 if (migrate_async_suitable(migratetype))
186 /* Otherwise skip the block */
190 static void compact_capture_page(struct compact_control *cc)
193 int mtype, mtype_low, mtype_high;
195 if (!cc->page || *cc->page)
199 * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP
200 * regardless of the migratetype of the freelist is is captured from.
201 * This is fine because the order for a high-order MIGRATE_MOVABLE
202 * allocation is typically at least a pageblock size and overall
203 * fragmentation is not impaired. Other allocation types must
204 * capture pages from their own migratelist because otherwise they
205 * could pollute other pageblocks like MIGRATE_MOVABLE with
206 * difficult to move pages and making fragmentation worse overall.
208 if (cc->migratetype == MIGRATE_MOVABLE) {
210 mtype_high = MIGRATE_PCPTYPES;
212 mtype_low = cc->migratetype;
213 mtype_high = cc->migratetype + 1;
216 /* Speculatively examine the free lists without zone lock */
217 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
219 for (order = cc->order; order < MAX_ORDER; order++) {
221 struct free_area *area;
222 area = &(cc->zone->free_area[order]);
223 if (list_empty(&area->free_list[mtype]))
226 /* Take the lock and attempt capture of the page */
227 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
229 if (!list_empty(&area->free_list[mtype])) {
230 page = list_entry(area->free_list[mtype].next,
232 if (capture_free_page(page, cc->order, mtype)) {
233 spin_unlock_irqrestore(&cc->zone->lock,
239 spin_unlock_irqrestore(&cc->zone->lock, flags);
245 * Isolate free pages onto a private freelist. Caller must hold zone->lock.
246 * If @strict is true, will abort returning 0 on any invalid PFNs or non-free
247 * pages inside of the pageblock (even though it may still end up isolating
250 static unsigned long isolate_freepages_block(struct compact_control *cc,
251 unsigned long blockpfn,
252 unsigned long end_pfn,
253 struct list_head *freelist,
256 int nr_scanned = 0, total_isolated = 0;
257 struct page *cursor, *valid_page = NULL;
258 unsigned long nr_strict_required = end_pfn - blockpfn;
262 cursor = pfn_to_page(blockpfn);
264 /* Isolate free pages. */
265 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
267 struct page *page = cursor;
270 if (!pfn_valid_within(blockpfn))
274 if (!PageBuddy(page))
278 * The zone lock must be held to isolate freepages.
279 * Unfortunately this is a very coarse lock and can be
280 * heavily contended if there are parallel allocations
281 * or parallel compactions. For async compaction do not
282 * spin on the lock and we acquire the lock as late as
285 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
290 /* Recheck this is a suitable migration target under lock */
291 if (!strict && !suitable_migration_target(page))
294 /* Recheck this is a buddy page under lock */
295 if (!PageBuddy(page))
298 /* Found a free page, break it into order-0 pages */
299 isolated = split_free_page(page);
300 if (!isolated && strict)
302 total_isolated += isolated;
303 for (i = 0; i < isolated; i++) {
304 list_add(&page->lru, freelist);
308 /* If a page was split, advance to the end of it */
310 blockpfn += isolated - 1;
311 cursor += isolated - 1;
315 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
318 * If strict isolation is requested by CMA then check that all the
319 * pages requested were isolated. If there were any failures, 0 is
320 * returned and CMA will fail.
322 if (strict && nr_strict_required != total_isolated)
326 spin_unlock_irqrestore(&cc->zone->lock, flags);
328 /* Update the pageblock-skip if the whole pageblock was scanned */
329 if (blockpfn == end_pfn)
330 update_pageblock_skip(valid_page, total_isolated);
332 return total_isolated;
336 * isolate_freepages_range() - isolate free pages.
337 * @start_pfn: The first PFN to start isolating.
338 * @end_pfn: The one-past-last PFN.
340 * Non-free pages, invalid PFNs, or zone boundaries within the
341 * [start_pfn, end_pfn) range are considered errors, cause function to
342 * undo its actions and return zero.
344 * Otherwise, function returns one-past-the-last PFN of isolated page
345 * (which may be greater then end_pfn if end fell in a middle of
349 isolate_freepages_range(struct compact_control *cc,
350 unsigned long start_pfn, unsigned long end_pfn)
352 unsigned long isolated, pfn, block_end_pfn;
355 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
356 if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
360 * On subsequent iterations ALIGN() is actually not needed,
361 * but we keep it that we not to complicate the code.
363 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
364 block_end_pfn = min(block_end_pfn, end_pfn);
366 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
370 * In strict mode, isolate_freepages_block() returns 0 if
371 * there are any holes in the block (ie. invalid PFNs or
378 * If we managed to isolate pages, it is always (1 << n) *
379 * pageblock_nr_pages for some non-negative n. (Max order
380 * page may span two pageblocks).
384 /* split_free_page does not map the pages */
385 map_pages(&freelist);
388 /* Loop terminated early, cleanup. */
389 release_freepages(&freelist);
393 /* We don't use freelists for anything. */
397 /* Update the number of anon and file isolated pages in the zone */
398 static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc)
401 unsigned int count[2] = { 0, };
403 list_for_each_entry(page, &cc->migratepages, lru)
404 count[!!page_is_file_cache(page)]++;
406 /* If locked we can use the interrupt unsafe versions */
408 __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
409 __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
411 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
412 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
416 /* Similar to reclaim, but different enough that they don't share logic */
417 static bool too_many_isolated(struct zone *zone)
419 unsigned long active, inactive, isolated;
421 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
422 zone_page_state(zone, NR_INACTIVE_ANON);
423 active = zone_page_state(zone, NR_ACTIVE_FILE) +
424 zone_page_state(zone, NR_ACTIVE_ANON);
425 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
426 zone_page_state(zone, NR_ISOLATED_ANON);
428 return isolated > (inactive + active) / 2;
432 * isolate_migratepages_range() - isolate all migrate-able pages in range.
433 * @zone: Zone pages are in.
434 * @cc: Compaction control structure.
435 * @low_pfn: The first PFN of the range.
436 * @end_pfn: The one-past-the-last PFN of the range.
438 * Isolate all pages that can be migrated from the range specified by
439 * [low_pfn, end_pfn). Returns zero if there is a fatal signal
440 * pending), otherwise PFN of the first page that was not scanned
441 * (which may be both less, equal to or more then end_pfn).
443 * Assumes that cc->migratepages is empty and cc->nr_migratepages is
446 * Apart from cc->migratepages and cc->nr_migratetypes this function
447 * does not modify any cc's fields, in particular it does not modify
448 * (or read for that matter) cc->migrate_pfn.
451 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
452 unsigned long low_pfn, unsigned long end_pfn)
454 unsigned long last_pageblock_nr = 0, pageblock_nr;
455 unsigned long nr_scanned = 0, nr_isolated = 0;
456 struct list_head *migratelist = &cc->migratepages;
457 isolate_mode_t mode = 0;
458 struct lruvec *lruvec;
461 struct page *page = NULL, *valid_page = NULL;
464 * Ensure that there are not too many pages isolated from the LRU
465 * list by either parallel reclaimers or compaction. If there are,
466 * delay for some time until fewer pages are isolated
468 while (unlikely(too_many_isolated(zone))) {
469 /* async migration should just abort */
473 congestion_wait(BLK_RW_ASYNC, HZ/10);
475 if (fatal_signal_pending(current))
479 /* Time to isolate some pages for migration */
481 for (; low_pfn < end_pfn; low_pfn++) {
482 /* give a chance to irqs before checking need_resched() */
483 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
484 if (should_release_lock(&zone->lru_lock)) {
485 spin_unlock_irqrestore(&zone->lru_lock, flags);
491 * migrate_pfn does not necessarily start aligned to a
492 * pageblock. Ensure that pfn_valid is called when moving
493 * into a new MAX_ORDER_NR_PAGES range in case of large
494 * memory holes within the zone
496 if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
497 if (!pfn_valid(low_pfn)) {
498 low_pfn += MAX_ORDER_NR_PAGES - 1;
503 if (!pfn_valid_within(low_pfn))
508 * Get the page and ensure the page is within the same zone.
509 * See the comment in isolate_freepages about overlapping
510 * nodes. It is deliberate that the new zone lock is not taken
511 * as memory compaction should not move pages between nodes.
513 page = pfn_to_page(low_pfn);
514 if (page_zone(page) != zone)
520 /* If isolation recently failed, do not retry */
521 pageblock_nr = low_pfn >> pageblock_order;
522 if (!isolation_suitable(cc, page))
530 * For async migration, also only scan in MOVABLE blocks. Async
531 * migration is optimistic to see if the minimum amount of work
532 * satisfies the allocation
534 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
535 !migrate_async_suitable(get_pageblock_migratetype(page))) {
539 /* Check may be lockless but that's ok as we recheck later */
544 * PageLRU is set. lru_lock normally excludes isolation
545 * splitting and collapsing (collapsing has already happened
546 * if PageLRU is set) but the lock is not necessarily taken
547 * here and it is wasteful to take it just to check transhuge.
548 * Check TransHuge without lock and skip the whole pageblock if
549 * it's either a transhuge or hugetlbfs page, as calling
550 * compound_order() without preventing THP from splitting the
551 * page underneath us may return surprising results.
553 if (PageTransHuge(page)) {
556 low_pfn += (1 << compound_order(page)) - 1;
560 /* Check if it is ok to still hold the lock */
561 locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
563 if (!locked || fatal_signal_pending(current))
566 /* Recheck PageLRU and PageTransHuge under lock */
569 if (PageTransHuge(page)) {
570 low_pfn += (1 << compound_order(page)) - 1;
575 mode |= ISOLATE_ASYNC_MIGRATE;
577 lruvec = mem_cgroup_page_lruvec(page, zone);
579 /* Try isolate the page */
580 if (__isolate_lru_page(page, mode) != 0)
583 VM_BUG_ON(PageTransCompound(page));
585 /* Successfully isolated */
586 del_page_from_lru_list(page, lruvec, page_lru(page));
587 list_add(&page->lru, migratelist);
588 cc->nr_migratepages++;
591 /* Avoid isolating too much */
592 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
600 low_pfn += pageblock_nr_pages;
601 low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1;
602 last_pageblock_nr = pageblock_nr;
605 acct_isolated(zone, locked, cc);
608 spin_unlock_irqrestore(&zone->lru_lock, flags);
610 /* Update the pageblock-skip if the whole pageblock was scanned */
611 if (low_pfn == end_pfn)
612 update_pageblock_skip(valid_page, nr_isolated);
614 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
619 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
620 #ifdef CONFIG_COMPACTION
622 * Based on information in the current compact_control, find blocks
623 * suitable for isolating free pages from and then isolate them.
625 static void isolate_freepages(struct zone *zone,
626 struct compact_control *cc)
629 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
630 int nr_freepages = cc->nr_freepages;
631 struct list_head *freelist = &cc->freepages;
634 * Initialise the free scanner. The starting point is where we last
635 * scanned from (or the end of the zone if starting). The low point
636 * is the end of the pageblock the migration scanner is using.
639 low_pfn = cc->migrate_pfn + pageblock_nr_pages;
642 * Take care that if the migration scanner is at the end of the zone
643 * that the free scanner does not accidentally move to the next zone
644 * in the next isolation cycle.
646 high_pfn = min(low_pfn, pfn);
648 zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages;
651 * Isolate free pages until enough are available to migrate the
652 * pages on cc->migratepages. We stop searching if the migrate
653 * and free page scanners meet or enough free pages are isolated.
655 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
656 pfn -= pageblock_nr_pages) {
657 unsigned long isolated;
663 * Check for overlapping nodes/zones. It's possible on some
664 * configurations to have a setup like
666 * i.e. it's possible that all pages within a zones range of
667 * pages do not belong to a single zone.
669 page = pfn_to_page(pfn);
670 if (page_zone(page) != zone)
673 /* Check the block is suitable for migration */
674 if (!suitable_migration_target(page))
677 /* If isolation recently failed, do not retry */
678 if (!isolation_suitable(cc, page))
681 /* Found a block suitable for isolating free pages from */
683 end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
684 isolated = isolate_freepages_block(cc, pfn, end_pfn,
686 nr_freepages += isolated;
689 * Record the highest PFN we isolated pages from. When next
690 * looking for free pages, the search will restart here as
691 * page migration may have returned some pages to the allocator
694 high_pfn = max(high_pfn, pfn);
697 /* split_free_page does not map the pages */
700 cc->free_pfn = high_pfn;
701 cc->nr_freepages = nr_freepages;
705 * This is a migrate-callback that "allocates" freepages by taking pages
706 * from the isolated freelists in the block we are migrating to.
708 static struct page *compaction_alloc(struct page *migratepage,
712 struct compact_control *cc = (struct compact_control *)data;
713 struct page *freepage;
715 /* Isolate free pages if necessary */
716 if (list_empty(&cc->freepages)) {
717 isolate_freepages(cc->zone, cc);
719 if (list_empty(&cc->freepages))
723 freepage = list_entry(cc->freepages.next, struct page, lru);
724 list_del(&freepage->lru);
731 * We cannot control nr_migratepages and nr_freepages fully when migration is
732 * running as migrate_pages() has no knowledge of compact_control. When
733 * migration is complete, we count the number of pages on the lists by hand.
735 static void update_nr_listpages(struct compact_control *cc)
737 int nr_migratepages = 0;
738 int nr_freepages = 0;
741 list_for_each_entry(page, &cc->migratepages, lru)
743 list_for_each_entry(page, &cc->freepages, lru)
746 cc->nr_migratepages = nr_migratepages;
747 cc->nr_freepages = nr_freepages;
750 /* possible outcome of isolate_migratepages */
752 ISOLATE_ABORT, /* Abort compaction now */
753 ISOLATE_NONE, /* No pages isolated, continue scanning */
754 ISOLATE_SUCCESS, /* Pages isolated, migrate */
758 * Isolate all pages that can be migrated from the block pointed to by
759 * the migrate scanner within compact_control.
761 static isolate_migrate_t isolate_migratepages(struct zone *zone,
762 struct compact_control *cc)
764 unsigned long low_pfn, end_pfn;
766 /* Do not scan outside zone boundaries */
767 low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn);
769 /* Only scan within a pageblock boundary */
770 end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages);
772 /* Do not cross the free scanner or scan within a memory hole */
773 if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) {
774 cc->migrate_pfn = end_pfn;
778 /* Perform the isolation */
779 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
780 if (!low_pfn || cc->contended)
781 return ISOLATE_ABORT;
783 cc->migrate_pfn = low_pfn;
785 return ISOLATE_SUCCESS;
788 static int compact_finished(struct zone *zone,
789 struct compact_control *cc)
791 unsigned long watermark;
793 if (fatal_signal_pending(current))
794 return COMPACT_PARTIAL;
796 /* Compaction run completes if the migrate and free scanner meet */
797 if (cc->free_pfn <= cc->migrate_pfn) {
798 reset_isolation_suitable(cc->zone);
799 return COMPACT_COMPLETE;
803 * order == -1 is expected when compacting via
804 * /proc/sys/vm/compact_memory
807 return COMPACT_CONTINUE;
809 /* Compaction run is not finished if the watermark is not met */
810 watermark = low_wmark_pages(zone);
811 watermark += (1 << cc->order);
813 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
814 return COMPACT_CONTINUE;
816 /* Direct compactor: Is a suitable page free? */
818 /* Was a suitable page captured? */
820 return COMPACT_PARTIAL;
823 for (order = cc->order; order < MAX_ORDER; order++) {
824 struct free_area *area = &zone->free_area[cc->order];
825 /* Job done if page is free of the right migratetype */
826 if (!list_empty(&area->free_list[cc->migratetype]))
827 return COMPACT_PARTIAL;
829 /* Job done if allocation would set block type */
830 if (cc->order >= pageblock_order && area->nr_free)
831 return COMPACT_PARTIAL;
835 return COMPACT_CONTINUE;
839 * compaction_suitable: Is this suitable to run compaction on this zone now?
841 * COMPACT_SKIPPED - If there are too few free pages for compaction
842 * COMPACT_PARTIAL - If the allocation would succeed without compaction
843 * COMPACT_CONTINUE - If compaction should run now
845 unsigned long compaction_suitable(struct zone *zone, int order)
848 unsigned long watermark;
851 * order == -1 is expected when compacting via
852 * /proc/sys/vm/compact_memory
855 return COMPACT_CONTINUE;
858 * Watermarks for order-0 must be met for compaction. Note the 2UL.
859 * This is because during migration, copies of pages need to be
860 * allocated and for a short time, the footprint is higher
862 watermark = low_wmark_pages(zone) + (2UL << order);
863 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
864 return COMPACT_SKIPPED;
867 * fragmentation index determines if allocation failures are due to
868 * low memory or external fragmentation
870 * index of -1000 implies allocations might succeed depending on
872 * index towards 0 implies failure is due to lack of memory
873 * index towards 1000 implies failure is due to fragmentation
875 * Only compact if a failure would be due to fragmentation.
877 fragindex = fragmentation_index(zone, order);
878 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
879 return COMPACT_SKIPPED;
881 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
883 return COMPACT_PARTIAL;
885 return COMPACT_CONTINUE;
888 static int compact_zone(struct zone *zone, struct compact_control *cc)
892 ret = compaction_suitable(zone, cc->order);
894 case COMPACT_PARTIAL:
895 case COMPACT_SKIPPED:
896 /* Compaction is likely to fail */
898 case COMPACT_CONTINUE:
899 /* Fall through to compaction */
903 /* Setup to move all movable pages to the end of the zone */
904 cc->migrate_pfn = zone->zone_start_pfn;
905 cc->free_pfn = cc->migrate_pfn + zone->spanned_pages;
906 cc->free_pfn &= ~(pageblock_nr_pages-1);
908 /* Clear pageblock skip if there are numerous alloc failures */
909 if (zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT)
910 reset_isolation_suitable(zone);
912 migrate_prep_local();
914 while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
915 unsigned long nr_migrate, nr_remaining;
918 switch (isolate_migratepages(zone, cc)) {
920 ret = COMPACT_PARTIAL;
921 putback_lru_pages(&cc->migratepages);
922 cc->nr_migratepages = 0;
926 case ISOLATE_SUCCESS:
930 nr_migrate = cc->nr_migratepages;
931 err = migrate_pages(&cc->migratepages, compaction_alloc,
932 (unsigned long)cc, false,
933 cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC);
934 update_nr_listpages(cc);
935 nr_remaining = cc->nr_migratepages;
937 count_vm_event(COMPACTBLOCKS);
938 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
940 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
941 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
944 /* Release LRU pages not migrated */
946 putback_lru_pages(&cc->migratepages);
947 cc->nr_migratepages = 0;
948 if (err == -ENOMEM) {
949 ret = COMPACT_PARTIAL;
954 /* Capture a page now if it is a suitable size */
955 compact_capture_page(cc);
959 /* Release free pages and check accounting */
960 cc->nr_freepages -= release_freepages(&cc->freepages);
961 VM_BUG_ON(cc->nr_freepages != 0);
966 static unsigned long compact_zone_order(struct zone *zone,
967 int order, gfp_t gfp_mask,
968 bool sync, bool *contended,
972 struct compact_control cc = {
974 .nr_migratepages = 0,
976 .migratetype = allocflags_to_migratetype(gfp_mask),
981 INIT_LIST_HEAD(&cc.freepages);
982 INIT_LIST_HEAD(&cc.migratepages);
984 ret = compact_zone(zone, &cc);
986 VM_BUG_ON(!list_empty(&cc.freepages));
987 VM_BUG_ON(!list_empty(&cc.migratepages));
989 *contended = cc.contended;
993 int sysctl_extfrag_threshold = 500;
996 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
997 * @zonelist: The zonelist used for the current allocation
998 * @order: The order of the current allocation
999 * @gfp_mask: The GFP mask of the current allocation
1000 * @nodemask: The allowed nodes to allocate from
1001 * @sync: Whether migration is synchronous or not
1002 * @contended: Return value that is true if compaction was aborted due to lock contention
1003 * @page: Optionally capture a free page of the requested order during compaction
1005 * This is the main entry point for direct page compaction.
1007 unsigned long try_to_compact_pages(struct zonelist *zonelist,
1008 int order, gfp_t gfp_mask, nodemask_t *nodemask,
1009 bool sync, bool *contended, struct page **page)
1011 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1012 int may_enter_fs = gfp_mask & __GFP_FS;
1013 int may_perform_io = gfp_mask & __GFP_IO;
1016 int rc = COMPACT_SKIPPED;
1017 int alloc_flags = 0;
1019 /* Check if the GFP flags allow compaction */
1020 if (!order || !may_enter_fs || !may_perform_io)
1023 count_vm_event(COMPACTSTALL);
1026 if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1027 alloc_flags |= ALLOC_CMA;
1029 /* Compact each zone in the list */
1030 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1034 status = compact_zone_order(zone, order, gfp_mask, sync,
1036 rc = max(status, rc);
1038 /* If a normal allocation would succeed, stop compacting */
1039 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1048 /* Compact all zones within a node */
1049 static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1054 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1056 zone = &pgdat->node_zones[zoneid];
1057 if (!populated_zone(zone))
1060 cc->nr_freepages = 0;
1061 cc->nr_migratepages = 0;
1063 INIT_LIST_HEAD(&cc->freepages);
1064 INIT_LIST_HEAD(&cc->migratepages);
1066 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1067 compact_zone(zone, cc);
1069 if (cc->order > 0) {
1070 int ok = zone_watermark_ok(zone, cc->order,
1071 low_wmark_pages(zone), 0, 0);
1072 if (ok && cc->order >= zone->compact_order_failed)
1073 zone->compact_order_failed = cc->order + 1;
1074 /* Currently async compaction is never deferred. */
1075 else if (!ok && cc->sync)
1076 defer_compaction(zone, cc->order);
1079 VM_BUG_ON(!list_empty(&cc->freepages));
1080 VM_BUG_ON(!list_empty(&cc->migratepages));
1086 int compact_pgdat(pg_data_t *pgdat, int order)
1088 struct compact_control cc = {
1094 return __compact_pgdat(pgdat, &cc);
1097 static int compact_node(int nid)
1099 struct compact_control cc = {
1105 return __compact_pgdat(NODE_DATA(nid), &cc);
1108 /* Compact all nodes in the system */
1109 static int compact_nodes(void)
1113 /* Flush pending updates to the LRU lists */
1114 lru_add_drain_all();
1116 for_each_online_node(nid)
1119 return COMPACT_COMPLETE;
1122 /* The written value is actually unused, all memory is compacted */
1123 int sysctl_compact_memory;
1125 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1126 int sysctl_compaction_handler(struct ctl_table *table, int write,
1127 void __user *buffer, size_t *length, loff_t *ppos)
1130 return compact_nodes();
1135 int sysctl_extfrag_handler(struct ctl_table *table, int write,
1136 void __user *buffer, size_t *length, loff_t *ppos)
1138 proc_dointvec_minmax(table, write, buffer, length, ppos);
1143 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1144 ssize_t sysfs_compact_node(struct device *dev,
1145 struct device_attribute *attr,
1146 const char *buf, size_t count)
1150 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1151 /* Flush pending updates to the LRU lists */
1152 lru_add_drain_all();
1159 static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1161 int compaction_register_node(struct node *node)
1163 return device_create_file(&node->dev, &dev_attr_compact);
1166 void compaction_unregister_node(struct node *node)
1168 return device_remove_file(&node->dev, &dev_attr_compact);
1170 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1172 #endif /* CONFIG_COMPACTION */