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>
18 #include <linux/page-isolation.h>
21 #ifdef CONFIG_COMPACTION
22 static inline void count_compact_event(enum vm_event_item item
)
27 static inline void count_compact_events(enum vm_event_item item
, long delta
)
29 count_vm_events(item
, delta
);
32 #define count_compact_event(item) do { } while (0)
33 #define count_compact_events(item, delta) do { } while (0)
36 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/compaction.h>
41 static unsigned long release_freepages(struct list_head
*freelist
)
43 struct page
*page
, *next
;
44 unsigned long count
= 0;
46 list_for_each_entry_safe(page
, next
, freelist
, lru
) {
55 static void map_pages(struct list_head
*list
)
59 list_for_each_entry(page
, list
, lru
) {
60 arch_alloc_page(page
, 0);
61 kernel_map_pages(page
, 1, 1);
65 static inline bool migrate_async_suitable(int migratetype
)
67 return is_migrate_cma(migratetype
) || migratetype
== MIGRATE_MOVABLE
;
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
77 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
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.
87 static struct page
*pageblock_pfn_to_page(unsigned long start_pfn
,
88 unsigned long end_pfn
, struct zone
*zone
)
90 struct page
*start_page
;
91 struct page
*end_page
;
93 /* end_pfn is one past the range we are checking */
96 if (!pfn_valid(start_pfn
) || !pfn_valid(end_pfn
))
99 start_page
= pfn_to_page(start_pfn
);
101 if (page_zone(start_page
) != zone
)
104 end_page
= pfn_to_page(end_pfn
);
106 /* This gives a shorter code than deriving page_zone(end_page) */
107 if (page_zone_id(start_page
) != page_zone_id(end_page
))
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
,
118 if (cc
->ignore_skip_hint
)
121 return !get_pageblock_skip(page
);
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
129 static void __reset_isolation_suitable(struct zone
*zone
)
131 unsigned long start_pfn
= zone
->zone_start_pfn
;
132 unsigned long end_pfn
= zone_end_pfn(zone
);
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;
140 /* Walk the zone and mark every pageblock as suitable for isolation */
141 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= pageblock_nr_pages
) {
149 page
= pfn_to_page(pfn
);
150 if (zone
!= page_zone(page
))
153 clear_pageblock_skip(page
);
157 void reset_isolation_suitable(pg_data_t
*pgdat
)
161 for (zoneid
= 0; zoneid
< MAX_NR_ZONES
; zoneid
++) {
162 struct zone
*zone
= &pgdat
->node_zones
[zoneid
];
163 if (!populated_zone(zone
))
166 /* Only flush if a full compaction finished recently */
167 if (zone
->compact_blockskip_flush
)
168 __reset_isolation_suitable(zone
);
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().
176 static void update_pageblock_skip(struct compact_control
*cc
,
177 struct page
*page
, unsigned long nr_isolated
,
178 bool migrate_scanner
)
180 struct zone
*zone
= cc
->zone
;
183 if (cc
->ignore_skip_hint
)
192 set_pageblock_skip(page
);
194 pfn
= page_to_pfn(page
);
196 /* Update where async and sync compaction should restart */
197 if (migrate_scanner
) {
198 if (cc
->finished_update_migrate
)
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
;
206 if (cc
->finished_update_free
)
208 if (pfn
< zone
->compact_cached_free_pfn
)
209 zone
->compact_cached_free_pfn
= pfn
;
213 static inline bool isolation_suitable(struct compact_control
*cc
,
219 static void update_pageblock_skip(struct compact_control
*cc
,
220 struct page
*page
, unsigned long nr_isolated
,
221 bool migrate_scanner
)
224 #endif /* CONFIG_COMPACTION */
226 static int should_release_lock(spinlock_t
*lock
)
229 * Sched contention has higher priority here as we may potentially
230 * have to abort whole compaction ASAP. Returning with lock contention
231 * means we will try another zone, and further decisions are
232 * influenced only when all zones are lock contended. That means
233 * potentially missing a lock contention is less critical.
236 return COMPACT_CONTENDED_SCHED
;
237 else if (spin_is_contended(lock
))
238 return COMPACT_CONTENDED_LOCK
;
240 return COMPACT_CONTENDED_NONE
;
244 * Compaction requires the taking of some coarse locks that are potentially
245 * very heavily contended. Check if the process needs to be scheduled or
246 * if the lock is contended. For async compaction, back out in the event
247 * if contention is severe. For sync compaction, schedule.
249 * Returns true if the lock is held.
250 * Returns false if the lock is released and compaction should abort
252 static bool compact_checklock_irqsave(spinlock_t
*lock
, unsigned long *flags
,
253 bool locked
, struct compact_control
*cc
)
255 int contended
= should_release_lock(lock
);
259 spin_unlock_irqrestore(lock
, *flags
);
263 /* async aborts if taking too long or contended */
264 if (cc
->mode
== MIGRATE_ASYNC
) {
265 cc
->contended
= contended
;
273 spin_lock_irqsave(lock
, *flags
);
278 * Aside from avoiding lock contention, compaction also periodically checks
279 * need_resched() and either schedules in sync compaction or aborts async
280 * compaction. This is similar to what compact_checklock_irqsave() does, but
281 * is used where no lock is concerned.
283 * Returns false when no scheduling was needed, or sync compaction scheduled.
284 * Returns true when async compaction should abort.
286 static inline bool compact_should_abort(struct compact_control
*cc
)
288 /* async compaction aborts if contended */
289 if (need_resched()) {
290 if (cc
->mode
== MIGRATE_ASYNC
) {
291 cc
->contended
= COMPACT_CONTENDED_SCHED
;
301 /* Returns true if the page is within a block suitable for migration to */
302 static bool suitable_migration_target(struct page
*page
)
304 /* If the page is a large free page, then disallow migration */
305 if (PageBuddy(page
) && page_order(page
) >= pageblock_order
)
308 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
309 if (migrate_async_suitable(get_pageblock_migratetype(page
)))
312 /* Otherwise skip the block */
317 * Isolate free pages onto a private freelist. If @strict is true, will abort
318 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
319 * (even though it may still end up isolating some pages).
321 static unsigned long isolate_freepages_block(struct compact_control
*cc
,
322 unsigned long blockpfn
,
323 unsigned long end_pfn
,
324 struct list_head
*freelist
,
327 int nr_scanned
= 0, total_isolated
= 0;
328 struct page
*cursor
, *valid_page
= NULL
;
332 cursor
= pfn_to_page(blockpfn
);
334 /* Isolate free pages. */
335 for (; blockpfn
< end_pfn
; blockpfn
++, cursor
++) {
337 struct page
*page
= cursor
;
340 if (!pfn_valid_within(blockpfn
))
345 if (!PageBuddy(page
))
349 * The zone lock must be held to isolate freepages.
350 * Unfortunately this is a very coarse lock and can be
351 * heavily contended if there are parallel allocations
352 * or parallel compactions. For async compaction do not
353 * spin on the lock and we acquire the lock as late as
356 locked
= compact_checklock_irqsave(&cc
->zone
->lock
, &flags
,
361 /* Recheck this is a buddy page under lock */
362 if (!PageBuddy(page
))
365 /* Found a free page, break it into order-0 pages */
366 isolated
= split_free_page(page
);
367 total_isolated
+= isolated
;
368 for (i
= 0; i
< isolated
; i
++) {
369 list_add(&page
->lru
, freelist
);
373 /* If a page was split, advance to the end of it */
375 blockpfn
+= isolated
- 1;
376 cursor
+= isolated
- 1;
388 trace_mm_compaction_isolate_freepages(nr_scanned
, total_isolated
);
391 * If strict isolation is requested by CMA then check that all the
392 * pages requested were isolated. If there were any failures, 0 is
393 * returned and CMA will fail.
395 if (strict
&& blockpfn
< end_pfn
)
399 spin_unlock_irqrestore(&cc
->zone
->lock
, flags
);
401 /* Update the pageblock-skip if the whole pageblock was scanned */
402 if (blockpfn
== end_pfn
)
403 update_pageblock_skip(cc
, valid_page
, total_isolated
, false);
405 count_compact_events(COMPACTFREE_SCANNED
, nr_scanned
);
407 count_compact_events(COMPACTISOLATED
, total_isolated
);
408 return total_isolated
;
412 * isolate_freepages_range() - isolate free pages.
413 * @start_pfn: The first PFN to start isolating.
414 * @end_pfn: The one-past-last PFN.
416 * Non-free pages, invalid PFNs, or zone boundaries within the
417 * [start_pfn, end_pfn) range are considered errors, cause function to
418 * undo its actions and return zero.
420 * Otherwise, function returns one-past-the-last PFN of isolated page
421 * (which may be greater then end_pfn if end fell in a middle of
425 isolate_freepages_range(struct compact_control
*cc
,
426 unsigned long start_pfn
, unsigned long end_pfn
)
428 unsigned long isolated
, pfn
, block_end_pfn
;
432 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
434 for (; pfn
< end_pfn
; pfn
+= isolated
,
435 block_end_pfn
+= pageblock_nr_pages
) {
437 block_end_pfn
= min(block_end_pfn
, end_pfn
);
439 if (!pageblock_pfn_to_page(pfn
, block_end_pfn
, cc
->zone
))
442 isolated
= isolate_freepages_block(cc
, pfn
, block_end_pfn
,
446 * In strict mode, isolate_freepages_block() returns 0 if
447 * there are any holes in the block (ie. invalid PFNs or
454 * If we managed to isolate pages, it is always (1 << n) *
455 * pageblock_nr_pages for some non-negative n. (Max order
456 * page may span two pageblocks).
460 /* split_free_page does not map the pages */
461 map_pages(&freelist
);
464 /* Loop terminated early, cleanup. */
465 release_freepages(&freelist
);
469 /* We don't use freelists for anything. */
473 /* Update the number of anon and file isolated pages in the zone */
474 static void acct_isolated(struct zone
*zone
, struct compact_control
*cc
)
477 unsigned int count
[2] = { 0, };
479 if (list_empty(&cc
->migratepages
))
482 list_for_each_entry(page
, &cc
->migratepages
, lru
)
483 count
[!!page_is_file_cache(page
)]++;
485 mod_zone_page_state(zone
, NR_ISOLATED_ANON
, count
[0]);
486 mod_zone_page_state(zone
, NR_ISOLATED_FILE
, count
[1]);
489 /* Similar to reclaim, but different enough that they don't share logic */
490 static bool too_many_isolated(struct zone
*zone
)
492 unsigned long active
, inactive
, isolated
;
494 inactive
= zone_page_state(zone
, NR_INACTIVE_FILE
) +
495 zone_page_state(zone
, NR_INACTIVE_ANON
);
496 active
= zone_page_state(zone
, NR_ACTIVE_FILE
) +
497 zone_page_state(zone
, NR_ACTIVE_ANON
);
498 isolated
= zone_page_state(zone
, NR_ISOLATED_FILE
) +
499 zone_page_state(zone
, NR_ISOLATED_ANON
);
501 return isolated
> (inactive
+ active
) / 2;
505 * isolate_migratepages_block() - isolate all migrate-able pages within
507 * @cc: Compaction control structure.
508 * @low_pfn: The first PFN to isolate
509 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
510 * @isolate_mode: Isolation mode to be used.
512 * Isolate all pages that can be migrated from the range specified by
513 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
514 * Returns zero if there is a fatal signal pending, otherwise PFN of the
515 * first page that was not scanned (which may be both less, equal to or more
518 * The pages are isolated on cc->migratepages list (not required to be empty),
519 * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
520 * is neither read nor updated.
523 isolate_migratepages_block(struct compact_control
*cc
, unsigned long low_pfn
,
524 unsigned long end_pfn
, isolate_mode_t isolate_mode
)
526 struct zone
*zone
= cc
->zone
;
527 unsigned long nr_scanned
= 0, nr_isolated
= 0;
528 struct list_head
*migratelist
= &cc
->migratepages
;
529 struct lruvec
*lruvec
;
532 struct page
*page
= NULL
, *valid_page
= NULL
;
535 * Ensure that there are not too many pages isolated from the LRU
536 * list by either parallel reclaimers or compaction. If there are,
537 * delay for some time until fewer pages are isolated
539 while (unlikely(too_many_isolated(zone
))) {
540 /* async migration should just abort */
541 if (cc
->mode
== MIGRATE_ASYNC
)
544 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
546 if (fatal_signal_pending(current
))
550 if (compact_should_abort(cc
))
553 /* Time to isolate some pages for migration */
554 for (; low_pfn
< end_pfn
; low_pfn
++) {
555 /* give a chance to irqs before checking need_resched() */
556 if (locked
&& !(low_pfn
% SWAP_CLUSTER_MAX
)) {
557 if (should_release_lock(&zone
->lru_lock
)) {
558 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
563 if (!pfn_valid_within(low_pfn
))
567 page
= pfn_to_page(low_pfn
);
573 * Skip if free. page_order cannot be used without zone->lock
574 * as nothing prevents parallel allocations or buddy merging.
580 * Check may be lockless but that's ok as we recheck later.
581 * It's possible to migrate LRU pages and balloon pages
582 * Skip any other type of page
584 if (!PageLRU(page
)) {
585 if (unlikely(balloon_page_movable(page
))) {
586 if (locked
&& balloon_page_isolate(page
)) {
587 /* Successfully isolated */
588 goto isolate_success
;
595 * PageLRU is set. lru_lock normally excludes isolation
596 * splitting and collapsing (collapsing has already happened
597 * if PageLRU is set) but the lock is not necessarily taken
598 * here and it is wasteful to take it just to check transhuge.
599 * Check TransHuge without lock and skip the whole pageblock if
600 * it's either a transhuge or hugetlbfs page, as calling
601 * compound_order() without preventing THP from splitting the
602 * page underneath us may return surprising results.
604 if (PageTransHuge(page
)) {
606 low_pfn
= ALIGN(low_pfn
+ 1,
607 pageblock_nr_pages
) - 1;
609 low_pfn
+= (1 << compound_order(page
)) - 1;
615 * Migration will fail if an anonymous page is pinned in memory,
616 * so avoid taking lru_lock and isolating it unnecessarily in an
617 * admittedly racy check.
619 if (!page_mapping(page
) &&
620 page_count(page
) > page_mapcount(page
))
623 /* Check if it is ok to still hold the lock */
624 locked
= compact_checklock_irqsave(&zone
->lru_lock
, &flags
,
626 if (!locked
|| fatal_signal_pending(current
))
629 /* Recheck PageLRU and PageTransHuge under lock */
632 if (PageTransHuge(page
)) {
633 low_pfn
+= (1 << compound_order(page
)) - 1;
637 lruvec
= mem_cgroup_page_lruvec(page
, zone
);
639 /* Try isolate the page */
640 if (__isolate_lru_page(page
, isolate_mode
) != 0)
643 VM_BUG_ON_PAGE(PageTransCompound(page
), page
);
645 /* Successfully isolated */
646 del_page_from_lru_list(page
, lruvec
, page_lru(page
));
649 cc
->finished_update_migrate
= true;
650 list_add(&page
->lru
, migratelist
);
651 cc
->nr_migratepages
++;
654 /* Avoid isolating too much */
655 if (cc
->nr_migratepages
== COMPACT_CLUSTER_MAX
) {
662 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
665 * Update the pageblock-skip information and cached scanner pfn,
666 * if the whole pageblock was scanned without isolating any page.
668 if (low_pfn
== end_pfn
)
669 update_pageblock_skip(cc
, valid_page
, nr_isolated
, true);
671 trace_mm_compaction_isolate_migratepages(nr_scanned
, nr_isolated
);
673 count_compact_events(COMPACTMIGRATE_SCANNED
, nr_scanned
);
675 count_compact_events(COMPACTISOLATED
, nr_isolated
);
681 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
682 * @cc: Compaction control structure.
683 * @start_pfn: The first PFN to start isolating.
684 * @end_pfn: The one-past-last PFN.
686 * Returns zero if isolation fails fatally due to e.g. pending signal.
687 * Otherwise, function returns one-past-the-last PFN of isolated page
688 * (which may be greater than end_pfn if end fell in a middle of a THP page).
691 isolate_migratepages_range(struct compact_control
*cc
, unsigned long start_pfn
,
692 unsigned long end_pfn
)
694 unsigned long pfn
, block_end_pfn
;
696 /* Scan block by block. First and last block may be incomplete */
698 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
700 for (; pfn
< end_pfn
; pfn
= block_end_pfn
,
701 block_end_pfn
+= pageblock_nr_pages
) {
703 block_end_pfn
= min(block_end_pfn
, end_pfn
);
705 if (!pageblock_pfn_to_page(pfn
, block_end_pfn
, cc
->zone
))
708 pfn
= isolate_migratepages_block(cc
, pfn
, block_end_pfn
,
709 ISOLATE_UNEVICTABLE
);
712 * In case of fatal failure, release everything that might
713 * have been isolated in the previous iteration, and signal
714 * the failure back to caller.
717 putback_movable_pages(&cc
->migratepages
);
718 cc
->nr_migratepages
= 0;
722 acct_isolated(cc
->zone
, cc
);
727 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
728 #ifdef CONFIG_COMPACTION
730 * Based on information in the current compact_control, find blocks
731 * suitable for isolating free pages from and then isolate them.
733 static void isolate_freepages(struct compact_control
*cc
)
735 struct zone
*zone
= cc
->zone
;
737 unsigned long block_start_pfn
; /* start of current pageblock */
738 unsigned long block_end_pfn
; /* end of current pageblock */
739 unsigned long low_pfn
; /* lowest pfn scanner is able to scan */
740 int nr_freepages
= cc
->nr_freepages
;
741 struct list_head
*freelist
= &cc
->freepages
;
744 * Initialise the free scanner. The starting point is where we last
745 * successfully isolated from, zone-cached value, or the end of the
746 * zone when isolating for the first time. We need this aligned to
747 * the pageblock boundary, because we do
748 * block_start_pfn -= pageblock_nr_pages in the for loop.
749 * For ending point, take care when isolating in last pageblock of a
750 * a zone which ends in the middle of a pageblock.
751 * The low boundary is the end of the pageblock the migration scanner
754 block_start_pfn
= cc
->free_pfn
& ~(pageblock_nr_pages
-1);
755 block_end_pfn
= min(block_start_pfn
+ pageblock_nr_pages
,
757 low_pfn
= ALIGN(cc
->migrate_pfn
+ 1, pageblock_nr_pages
);
760 * Isolate free pages until enough are available to migrate the
761 * pages on cc->migratepages. We stop searching if the migrate
762 * and free page scanners meet or enough free pages are isolated.
764 for (; block_start_pfn
>= low_pfn
&& cc
->nr_migratepages
> nr_freepages
;
765 block_end_pfn
= block_start_pfn
,
766 block_start_pfn
-= pageblock_nr_pages
) {
767 unsigned long isolated
;
770 * This can iterate a massively long zone without finding any
771 * suitable migration targets, so periodically check if we need
772 * to schedule, or even abort async compaction.
774 if (!(block_start_pfn
% (SWAP_CLUSTER_MAX
* pageblock_nr_pages
))
775 && compact_should_abort(cc
))
778 page
= pageblock_pfn_to_page(block_start_pfn
, block_end_pfn
,
783 /* Check the block is suitable for migration */
784 if (!suitable_migration_target(page
))
787 /* If isolation recently failed, do not retry */
788 if (!isolation_suitable(cc
, page
))
791 /* Found a block suitable for isolating free pages from */
792 cc
->free_pfn
= block_start_pfn
;
793 isolated
= isolate_freepages_block(cc
, block_start_pfn
,
794 block_end_pfn
, freelist
, false);
795 nr_freepages
+= isolated
;
798 * Set a flag that we successfully isolated in this pageblock.
799 * In the next loop iteration, zone->compact_cached_free_pfn
800 * will not be updated and thus it will effectively contain the
801 * highest pageblock we isolated pages from.
804 cc
->finished_update_free
= true;
807 * isolate_freepages_block() might have aborted due to async
808 * compaction being contended
814 /* split_free_page does not map the pages */
818 * If we crossed the migrate scanner, we want to keep it that way
819 * so that compact_finished() may detect this
821 if (block_start_pfn
< low_pfn
)
822 cc
->free_pfn
= cc
->migrate_pfn
;
824 cc
->nr_freepages
= nr_freepages
;
828 * This is a migrate-callback that "allocates" freepages by taking pages
829 * from the isolated freelists in the block we are migrating to.
831 static struct page
*compaction_alloc(struct page
*migratepage
,
835 struct compact_control
*cc
= (struct compact_control
*)data
;
836 struct page
*freepage
;
839 * Isolate free pages if necessary, and if we are not aborting due to
842 if (list_empty(&cc
->freepages
)) {
844 isolate_freepages(cc
);
846 if (list_empty(&cc
->freepages
))
850 freepage
= list_entry(cc
->freepages
.next
, struct page
, lru
);
851 list_del(&freepage
->lru
);
858 * This is a migrate-callback that "frees" freepages back to the isolated
859 * freelist. All pages on the freelist are from the same zone, so there is no
860 * special handling needed for NUMA.
862 static void compaction_free(struct page
*page
, unsigned long data
)
864 struct compact_control
*cc
= (struct compact_control
*)data
;
866 list_add(&page
->lru
, &cc
->freepages
);
870 /* possible outcome of isolate_migratepages */
872 ISOLATE_ABORT
, /* Abort compaction now */
873 ISOLATE_NONE
, /* No pages isolated, continue scanning */
874 ISOLATE_SUCCESS
, /* Pages isolated, migrate */
878 * Isolate all pages that can be migrated from the first suitable block,
879 * starting at the block pointed to by the migrate scanner pfn within
882 static isolate_migrate_t
isolate_migratepages(struct zone
*zone
,
883 struct compact_control
*cc
)
885 unsigned long low_pfn
, end_pfn
;
887 const isolate_mode_t isolate_mode
=
888 (cc
->mode
== MIGRATE_ASYNC
? ISOLATE_ASYNC_MIGRATE
: 0);
891 * Start at where we last stopped, or beginning of the zone as
892 * initialized by compact_zone()
894 low_pfn
= cc
->migrate_pfn
;
896 /* Only scan within a pageblock boundary */
897 end_pfn
= ALIGN(low_pfn
+ 1, pageblock_nr_pages
);
900 * Iterate over whole pageblocks until we find the first suitable.
901 * Do not cross the free scanner.
903 for (; end_pfn
<= cc
->free_pfn
;
904 low_pfn
= end_pfn
, end_pfn
+= pageblock_nr_pages
) {
907 * This can potentially iterate a massively long zone with
908 * many pageblocks unsuitable, so periodically check if we
909 * need to schedule, or even abort async compaction.
911 if (!(low_pfn
% (SWAP_CLUSTER_MAX
* pageblock_nr_pages
))
912 && compact_should_abort(cc
))
915 page
= pageblock_pfn_to_page(low_pfn
, end_pfn
, zone
);
919 /* If isolation recently failed, do not retry */
920 if (!isolation_suitable(cc
, page
))
924 * For async compaction, also only scan in MOVABLE blocks.
925 * Async compaction is optimistic to see if the minimum amount
926 * of work satisfies the allocation.
928 if (cc
->mode
== MIGRATE_ASYNC
&&
929 !migrate_async_suitable(get_pageblock_migratetype(page
)))
932 /* Perform the isolation */
933 low_pfn
= isolate_migratepages_block(cc
, low_pfn
, end_pfn
,
936 if (!low_pfn
|| cc
->contended
)
937 return ISOLATE_ABORT
;
940 * Either we isolated something and proceed with migration. Or
941 * we failed and compact_zone should decide if we should
947 acct_isolated(zone
, cc
);
948 /* Record where migration scanner will be restarted */
949 cc
->migrate_pfn
= low_pfn
;
951 return cc
->nr_migratepages
? ISOLATE_SUCCESS
: ISOLATE_NONE
;
954 static int compact_finished(struct zone
*zone
,
955 struct compact_control
*cc
)
958 unsigned long watermark
;
960 if (cc
->contended
|| fatal_signal_pending(current
))
961 return COMPACT_PARTIAL
;
963 /* Compaction run completes if the migrate and free scanner meet */
964 if (cc
->free_pfn
<= cc
->migrate_pfn
) {
965 /* Let the next compaction start anew. */
966 zone
->compact_cached_migrate_pfn
[0] = zone
->zone_start_pfn
;
967 zone
->compact_cached_migrate_pfn
[1] = zone
->zone_start_pfn
;
968 zone
->compact_cached_free_pfn
= zone_end_pfn(zone
);
971 * Mark that the PG_migrate_skip information should be cleared
972 * by kswapd when it goes to sleep. kswapd does not set the
973 * flag itself as the decision to be clear should be directly
974 * based on an allocation request.
976 if (!current_is_kswapd())
977 zone
->compact_blockskip_flush
= true;
979 return COMPACT_COMPLETE
;
983 * order == -1 is expected when compacting via
984 * /proc/sys/vm/compact_memory
987 return COMPACT_CONTINUE
;
989 /* Compaction run is not finished if the watermark is not met */
990 watermark
= low_wmark_pages(zone
);
991 watermark
+= (1 << cc
->order
);
993 if (!zone_watermark_ok(zone
, cc
->order
, watermark
, 0, 0))
994 return COMPACT_CONTINUE
;
996 /* Direct compactor: Is a suitable page free? */
997 for (order
= cc
->order
; order
< MAX_ORDER
; order
++) {
998 struct free_area
*area
= &zone
->free_area
[order
];
1000 /* Job done if page is free of the right migratetype */
1001 if (!list_empty(&area
->free_list
[cc
->migratetype
]))
1002 return COMPACT_PARTIAL
;
1004 /* Job done if allocation would set block type */
1005 if (cc
->order
>= pageblock_order
&& area
->nr_free
)
1006 return COMPACT_PARTIAL
;
1009 return COMPACT_CONTINUE
;
1013 * compaction_suitable: Is this suitable to run compaction on this zone now?
1015 * COMPACT_SKIPPED - If there are too few free pages for compaction
1016 * COMPACT_PARTIAL - If the allocation would succeed without compaction
1017 * COMPACT_CONTINUE - If compaction should run now
1019 unsigned long compaction_suitable(struct zone
*zone
, int order
)
1022 unsigned long watermark
;
1025 * order == -1 is expected when compacting via
1026 * /proc/sys/vm/compact_memory
1029 return COMPACT_CONTINUE
;
1032 * Watermarks for order-0 must be met for compaction. Note the 2UL.
1033 * This is because during migration, copies of pages need to be
1034 * allocated and for a short time, the footprint is higher
1036 watermark
= low_wmark_pages(zone
) + (2UL << order
);
1037 if (!zone_watermark_ok(zone
, 0, watermark
, 0, 0))
1038 return COMPACT_SKIPPED
;
1041 * fragmentation index determines if allocation failures are due to
1042 * low memory or external fragmentation
1044 * index of -1000 implies allocations might succeed depending on
1046 * index towards 0 implies failure is due to lack of memory
1047 * index towards 1000 implies failure is due to fragmentation
1049 * Only compact if a failure would be due to fragmentation.
1051 fragindex
= fragmentation_index(zone
, order
);
1052 if (fragindex
>= 0 && fragindex
<= sysctl_extfrag_threshold
)
1053 return COMPACT_SKIPPED
;
1055 if (fragindex
== -1000 && zone_watermark_ok(zone
, order
, watermark
,
1057 return COMPACT_PARTIAL
;
1059 return COMPACT_CONTINUE
;
1062 static int compact_zone(struct zone
*zone
, struct compact_control
*cc
)
1065 unsigned long start_pfn
= zone
->zone_start_pfn
;
1066 unsigned long end_pfn
= zone_end_pfn(zone
);
1067 const bool sync
= cc
->mode
!= MIGRATE_ASYNC
;
1069 ret
= compaction_suitable(zone
, cc
->order
);
1071 case COMPACT_PARTIAL
:
1072 case COMPACT_SKIPPED
:
1073 /* Compaction is likely to fail */
1075 case COMPACT_CONTINUE
:
1076 /* Fall through to compaction */
1081 * Clear pageblock skip if there were failures recently and compaction
1082 * is about to be retried after being deferred. kswapd does not do
1083 * this reset as it'll reset the cached information when going to sleep.
1085 if (compaction_restarting(zone
, cc
->order
) && !current_is_kswapd())
1086 __reset_isolation_suitable(zone
);
1089 * Setup to move all movable pages to the end of the zone. Used cached
1090 * information on where the scanners should start but check that it
1091 * is initialised by ensuring the values are within zone boundaries.
1093 cc
->migrate_pfn
= zone
->compact_cached_migrate_pfn
[sync
];
1094 cc
->free_pfn
= zone
->compact_cached_free_pfn
;
1095 if (cc
->free_pfn
< start_pfn
|| cc
->free_pfn
> end_pfn
) {
1096 cc
->free_pfn
= end_pfn
& ~(pageblock_nr_pages
-1);
1097 zone
->compact_cached_free_pfn
= cc
->free_pfn
;
1099 if (cc
->migrate_pfn
< start_pfn
|| cc
->migrate_pfn
> end_pfn
) {
1100 cc
->migrate_pfn
= start_pfn
;
1101 zone
->compact_cached_migrate_pfn
[0] = cc
->migrate_pfn
;
1102 zone
->compact_cached_migrate_pfn
[1] = cc
->migrate_pfn
;
1105 trace_mm_compaction_begin(start_pfn
, cc
->migrate_pfn
, cc
->free_pfn
, end_pfn
);
1107 migrate_prep_local();
1109 while ((ret
= compact_finished(zone
, cc
)) == COMPACT_CONTINUE
) {
1112 switch (isolate_migratepages(zone
, cc
)) {
1114 ret
= COMPACT_PARTIAL
;
1115 putback_movable_pages(&cc
->migratepages
);
1116 cc
->nr_migratepages
= 0;
1120 case ISOLATE_SUCCESS
:
1124 err
= migrate_pages(&cc
->migratepages
, compaction_alloc
,
1125 compaction_free
, (unsigned long)cc
, cc
->mode
,
1128 trace_mm_compaction_migratepages(cc
->nr_migratepages
, err
,
1131 /* All pages were either migrated or will be released */
1132 cc
->nr_migratepages
= 0;
1134 putback_movable_pages(&cc
->migratepages
);
1136 * migrate_pages() may return -ENOMEM when scanners meet
1137 * and we want compact_finished() to detect it
1139 if (err
== -ENOMEM
&& cc
->free_pfn
> cc
->migrate_pfn
) {
1140 ret
= COMPACT_PARTIAL
;
1147 /* Release free pages and check accounting */
1148 cc
->nr_freepages
-= release_freepages(&cc
->freepages
);
1149 VM_BUG_ON(cc
->nr_freepages
!= 0);
1151 trace_mm_compaction_end(ret
);
1156 static unsigned long compact_zone_order(struct zone
*zone
, int order
,
1157 gfp_t gfp_mask
, enum migrate_mode mode
, int *contended
)
1160 struct compact_control cc
= {
1162 .nr_migratepages
= 0,
1164 .migratetype
= allocflags_to_migratetype(gfp_mask
),
1168 INIT_LIST_HEAD(&cc
.freepages
);
1169 INIT_LIST_HEAD(&cc
.migratepages
);
1171 ret
= compact_zone(zone
, &cc
);
1173 VM_BUG_ON(!list_empty(&cc
.freepages
));
1174 VM_BUG_ON(!list_empty(&cc
.migratepages
));
1176 *contended
= cc
.contended
;
1180 int sysctl_extfrag_threshold
= 500;
1183 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
1184 * @zonelist: The zonelist used for the current allocation
1185 * @order: The order of the current allocation
1186 * @gfp_mask: The GFP mask of the current allocation
1187 * @nodemask: The allowed nodes to allocate from
1188 * @mode: The migration mode for async, sync light, or sync migration
1189 * @contended: Return value that determines if compaction was aborted due to
1190 * need_resched() or lock contention
1191 * @candidate_zone: Return the zone where we think allocation should succeed
1193 * This is the main entry point for direct page compaction.
1195 unsigned long try_to_compact_pages(struct zonelist
*zonelist
,
1196 int order
, gfp_t gfp_mask
, nodemask_t
*nodemask
,
1197 enum migrate_mode mode
, int *contended
,
1198 struct zone
**candidate_zone
)
1200 enum zone_type high_zoneidx
= gfp_zone(gfp_mask
);
1201 int may_enter_fs
= gfp_mask
& __GFP_FS
;
1202 int may_perform_io
= gfp_mask
& __GFP_IO
;
1205 int rc
= COMPACT_DEFERRED
;
1206 int alloc_flags
= 0;
1207 int all_zones_contended
= COMPACT_CONTENDED_LOCK
; /* init for &= op */
1209 *contended
= COMPACT_CONTENDED_NONE
;
1211 /* Check if the GFP flags allow compaction */
1212 if (!order
|| !may_enter_fs
|| !may_perform_io
)
1213 return COMPACT_SKIPPED
;
1216 if (allocflags_to_migratetype(gfp_mask
) == MIGRATE_MOVABLE
)
1217 alloc_flags
|= ALLOC_CMA
;
1219 /* Compact each zone in the list */
1220 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
, high_zoneidx
,
1225 if (compaction_deferred(zone
, order
))
1228 status
= compact_zone_order(zone
, order
, gfp_mask
, mode
,
1230 rc
= max(status
, rc
);
1232 * It takes at least one zone that wasn't lock contended
1233 * to clear all_zones_contended.
1235 all_zones_contended
&= zone_contended
;
1237 /* If a normal allocation would succeed, stop compacting */
1238 if (zone_watermark_ok(zone
, order
, low_wmark_pages(zone
), 0,
1240 *candidate_zone
= zone
;
1242 * We think the allocation will succeed in this zone,
1243 * but it is not certain, hence the false. The caller
1244 * will repeat this with true if allocation indeed
1245 * succeeds in this zone.
1247 compaction_defer_reset(zone
, order
, false);
1249 * It is possible that async compaction aborted due to
1250 * need_resched() and the watermarks were ok thanks to
1251 * somebody else freeing memory. The allocation can
1252 * however still fail so we better signal the
1253 * need_resched() contention anyway (this will not
1254 * prevent the allocation attempt).
1256 if (zone_contended
== COMPACT_CONTENDED_SCHED
)
1257 *contended
= COMPACT_CONTENDED_SCHED
;
1262 if (mode
!= MIGRATE_ASYNC
) {
1264 * We think that allocation won't succeed in this zone
1265 * so we defer compaction there. If it ends up
1266 * succeeding after all, it will be reset.
1268 defer_compaction(zone
, order
);
1272 * We might have stopped compacting due to need_resched() in
1273 * async compaction, or due to a fatal signal detected. In that
1274 * case do not try further zones and signal need_resched()
1277 if ((zone_contended
== COMPACT_CONTENDED_SCHED
)
1278 || fatal_signal_pending(current
)) {
1279 *contended
= COMPACT_CONTENDED_SCHED
;
1286 * We might not have tried all the zones, so be conservative
1287 * and assume they are not all lock contended.
1289 all_zones_contended
= 0;
1294 * If at least one zone wasn't deferred or skipped, we report if all
1295 * zones that were tried were lock contended.
1297 if (rc
> COMPACT_SKIPPED
&& all_zones_contended
)
1298 *contended
= COMPACT_CONTENDED_LOCK
;
1304 /* Compact all zones within a node */
1305 static void __compact_pgdat(pg_data_t
*pgdat
, struct compact_control
*cc
)
1310 for (zoneid
= 0; zoneid
< MAX_NR_ZONES
; zoneid
++) {
1312 zone
= &pgdat
->node_zones
[zoneid
];
1313 if (!populated_zone(zone
))
1316 cc
->nr_freepages
= 0;
1317 cc
->nr_migratepages
= 0;
1319 INIT_LIST_HEAD(&cc
->freepages
);
1320 INIT_LIST_HEAD(&cc
->migratepages
);
1322 if (cc
->order
== -1 || !compaction_deferred(zone
, cc
->order
))
1323 compact_zone(zone
, cc
);
1325 if (cc
->order
> 0) {
1326 if (zone_watermark_ok(zone
, cc
->order
,
1327 low_wmark_pages(zone
), 0, 0))
1328 compaction_defer_reset(zone
, cc
->order
, false);
1331 VM_BUG_ON(!list_empty(&cc
->freepages
));
1332 VM_BUG_ON(!list_empty(&cc
->migratepages
));
1336 void compact_pgdat(pg_data_t
*pgdat
, int order
)
1338 struct compact_control cc
= {
1340 .mode
= MIGRATE_ASYNC
,
1346 __compact_pgdat(pgdat
, &cc
);
1349 static void compact_node(int nid
)
1351 struct compact_control cc
= {
1353 .mode
= MIGRATE_SYNC
,
1354 .ignore_skip_hint
= true,
1357 __compact_pgdat(NODE_DATA(nid
), &cc
);
1360 /* Compact all nodes in the system */
1361 static void compact_nodes(void)
1365 /* Flush pending updates to the LRU lists */
1366 lru_add_drain_all();
1368 for_each_online_node(nid
)
1372 /* The written value is actually unused, all memory is compacted */
1373 int sysctl_compact_memory
;
1375 /* This is the entry point for compacting all nodes via /proc/sys/vm */
1376 int sysctl_compaction_handler(struct ctl_table
*table
, int write
,
1377 void __user
*buffer
, size_t *length
, loff_t
*ppos
)
1385 int sysctl_extfrag_handler(struct ctl_table
*table
, int write
,
1386 void __user
*buffer
, size_t *length
, loff_t
*ppos
)
1388 proc_dointvec_minmax(table
, write
, buffer
, length
, ppos
);
1393 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1394 static ssize_t
sysfs_compact_node(struct device
*dev
,
1395 struct device_attribute
*attr
,
1396 const char *buf
, size_t count
)
1400 if (nid
>= 0 && nid
< nr_node_ids
&& node_online(nid
)) {
1401 /* Flush pending updates to the LRU lists */
1402 lru_add_drain_all();
1409 static DEVICE_ATTR(compact
, S_IWUSR
, NULL
, sysfs_compact_node
);
1411 int compaction_register_node(struct node
*node
)
1413 return device_create_file(&node
->dev
, &dev_attr_compact
);
1416 void compaction_unregister_node(struct node
*node
)
1418 return device_remove_file(&node
->dev
, &dev_attr_compact
);
1420 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
1422 #endif /* CONFIG_COMPACTION */