1 // SPDX-License-Identifier: GPL-2.0
3 * linux/mm/compaction.c
5 * Memory compaction for the reduction of external fragmentation. Note that
6 * this heavily depends upon page migration to do all the real heavy
9 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
11 #include <linux/cpu.h>
12 #include <linux/swap.h>
13 #include <linux/migrate.h>
14 #include <linux/compaction.h>
15 #include <linux/mm_inline.h>
16 #include <linux/sched/signal.h>
17 #include <linux/backing-dev.h>
18 #include <linux/sysctl.h>
19 #include <linux/sysfs.h>
20 #include <linux/page-isolation.h>
21 #include <linux/kasan.h>
22 #include <linux/kthread.h>
23 #include <linux/freezer.h>
24 #include <linux/page_owner.h>
25 #include <linux/psi.h>
26 #include <linux/cpuset.h>
29 #ifdef CONFIG_COMPACTION
31 * Fragmentation score check interval for proactive compaction purposes.
33 #define HPAGE_FRAG_CHECK_INTERVAL_MSEC (500)
35 static inline void count_compact_event(enum vm_event_item item)
40 static inline void count_compact_events(enum vm_event_item item, long delta)
42 count_vm_events(item, delta);
46 * order == -1 is expected when compacting proactively via
47 * 1. /proc/sys/vm/compact_memory
48 * 2. /sys/devices/system/node/nodex/compact
49 * 3. /proc/sys/vm/compaction_proactiveness
51 static inline bool is_via_compact_memory(int order)
57 #define count_compact_event(item) do { } while (0)
58 #define count_compact_events(item, delta) do { } while (0)
59 static inline bool is_via_compact_memory(int order) { return false; }
62 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/compaction.h>
67 #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
68 #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
71 * Page order with-respect-to which proactive compaction
72 * calculates external fragmentation, which is used as
73 * the "fragmentation score" of a node/zone.
75 #if defined CONFIG_TRANSPARENT_HUGEPAGE
76 #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER
77 #elif defined CONFIG_HUGETLBFS
78 #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER
80 #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
83 static struct page *mark_allocated_noprof(struct page *page, unsigned int order, gfp_t gfp_flags)
85 post_alloc_hook(page, order, __GFP_MOVABLE);
86 set_page_refcounted(page);
89 #define mark_allocated(...) alloc_hooks(mark_allocated_noprof(__VA_ARGS__))
91 static unsigned long release_free_list(struct list_head *freepages)
94 unsigned long high_pfn = 0;
96 for (order = 0; order < NR_PAGE_ORDERS; order++) {
97 struct page *page, *next;
99 list_for_each_entry_safe(page, next, &freepages[order], lru) {
100 unsigned long pfn = page_to_pfn(page);
102 list_del(&page->lru);
104 * Convert free pages into post allocation pages, so
105 * that we can free them via __free_page.
107 mark_allocated(page, order, __GFP_MOVABLE);
108 __free_pages(page, order);
116 #ifdef CONFIG_COMPACTION
117 bool PageMovable(struct page *page)
119 const struct movable_operations *mops;
121 VM_BUG_ON_PAGE(!PageLocked(page), page);
122 if (!__PageMovable(page))
125 mops = page_movable_ops(page);
132 void __SetPageMovable(struct page *page, const struct movable_operations *mops)
134 VM_BUG_ON_PAGE(!PageLocked(page), page);
135 VM_BUG_ON_PAGE((unsigned long)mops & PAGE_MAPPING_MOVABLE, page);
136 page->mapping = (void *)((unsigned long)mops | PAGE_MAPPING_MOVABLE);
138 EXPORT_SYMBOL(__SetPageMovable);
140 void __ClearPageMovable(struct page *page)
142 VM_BUG_ON_PAGE(!PageMovable(page), page);
144 * This page still has the type of a movable page, but it's
145 * actually not movable any more.
147 page->mapping = (void *)PAGE_MAPPING_MOVABLE;
149 EXPORT_SYMBOL(__ClearPageMovable);
151 /* Do not skip compaction more than 64 times */
152 #define COMPACT_MAX_DEFER_SHIFT 6
155 * Compaction is deferred when compaction fails to result in a page
156 * allocation success. 1 << compact_defer_shift, compactions are skipped up
157 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
159 static void defer_compaction(struct zone *zone, int order)
161 zone->compact_considered = 0;
162 zone->compact_defer_shift++;
164 if (order < zone->compact_order_failed)
165 zone->compact_order_failed = order;
167 if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
168 zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
170 trace_mm_compaction_defer_compaction(zone, order);
173 /* Returns true if compaction should be skipped this time */
174 static bool compaction_deferred(struct zone *zone, int order)
176 unsigned long defer_limit = 1UL << zone->compact_defer_shift;
178 if (order < zone->compact_order_failed)
181 /* Avoid possible overflow */
182 if (++zone->compact_considered >= defer_limit) {
183 zone->compact_considered = defer_limit;
187 trace_mm_compaction_deferred(zone, order);
193 * Update defer tracking counters after successful compaction of given order,
194 * which means an allocation either succeeded (alloc_success == true) or is
195 * expected to succeed.
197 void compaction_defer_reset(struct zone *zone, int order,
201 zone->compact_considered = 0;
202 zone->compact_defer_shift = 0;
204 if (order >= zone->compact_order_failed)
205 zone->compact_order_failed = order + 1;
207 trace_mm_compaction_defer_reset(zone, order);
210 /* Returns true if restarting compaction after many failures */
211 static bool compaction_restarting(struct zone *zone, int order)
213 if (order < zone->compact_order_failed)
216 return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
217 zone->compact_considered >= 1UL << zone->compact_defer_shift;
220 /* Returns true if the pageblock should be scanned for pages to isolate. */
221 static inline bool isolation_suitable(struct compact_control *cc,
224 if (cc->ignore_skip_hint)
227 return !get_pageblock_skip(page);
230 static void reset_cached_positions(struct zone *zone)
232 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
233 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
234 zone->compact_cached_free_pfn =
235 pageblock_start_pfn(zone_end_pfn(zone) - 1);
238 #ifdef CONFIG_SPARSEMEM
240 * If the PFN falls into an offline section, return the start PFN of the
241 * next online section. If the PFN falls into an online section or if
242 * there is no next online section, return 0.
244 static unsigned long skip_offline_sections(unsigned long start_pfn)
246 unsigned long start_nr = pfn_to_section_nr(start_pfn);
248 if (online_section_nr(start_nr))
251 while (++start_nr <= __highest_present_section_nr) {
252 if (online_section_nr(start_nr))
253 return section_nr_to_pfn(start_nr);
260 * If the PFN falls into an offline section, return the end PFN of the
261 * next online section in reverse. If the PFN falls into an online section
262 * or if there is no next online section in reverse, return 0.
264 static unsigned long skip_offline_sections_reverse(unsigned long start_pfn)
266 unsigned long start_nr = pfn_to_section_nr(start_pfn);
268 if (!start_nr || online_section_nr(start_nr))
271 while (start_nr-- > 0) {
272 if (online_section_nr(start_nr))
273 return section_nr_to_pfn(start_nr) + PAGES_PER_SECTION;
279 static unsigned long skip_offline_sections(unsigned long start_pfn)
284 static unsigned long skip_offline_sections_reverse(unsigned long start_pfn)
291 * Compound pages of >= pageblock_order should consistently be skipped until
292 * released. It is always pointless to compact pages of such order (if they are
293 * migratable), and the pageblocks they occupy cannot contain any free pages.
295 static bool pageblock_skip_persistent(struct page *page)
297 if (!PageCompound(page))
300 page = compound_head(page);
302 if (compound_order(page) >= pageblock_order)
309 __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
312 struct page *page = pfn_to_online_page(pfn);
313 struct page *block_page;
314 struct page *end_page;
315 unsigned long block_pfn;
319 if (zone != page_zone(page))
321 if (pageblock_skip_persistent(page))
325 * If skip is already cleared do no further checking once the
326 * restart points have been set.
328 if (check_source && check_target && !get_pageblock_skip(page))
332 * If clearing skip for the target scanner, do not select a
333 * non-movable pageblock as the starting point.
335 if (!check_source && check_target &&
336 get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
339 /* Ensure the start of the pageblock or zone is online and valid */
340 block_pfn = pageblock_start_pfn(pfn);
341 block_pfn = max(block_pfn, zone->zone_start_pfn);
342 block_page = pfn_to_online_page(block_pfn);
348 /* Ensure the end of the pageblock or zone is online and valid */
349 block_pfn = pageblock_end_pfn(pfn) - 1;
350 block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
351 end_page = pfn_to_online_page(block_pfn);
356 * Only clear the hint if a sample indicates there is either a
357 * free page or an LRU page in the block. One or other condition
358 * is necessary for the block to be a migration source/target.
361 if (check_source && PageLRU(page)) {
362 clear_pageblock_skip(page);
366 if (check_target && PageBuddy(page)) {
367 clear_pageblock_skip(page);
371 page += (1 << PAGE_ALLOC_COSTLY_ORDER);
372 } while (page <= end_page);
378 * This function is called to clear all cached information on pageblocks that
379 * should be skipped for page isolation when the migrate and free page scanner
382 static void __reset_isolation_suitable(struct zone *zone)
384 unsigned long migrate_pfn = zone->zone_start_pfn;
385 unsigned long free_pfn = zone_end_pfn(zone) - 1;
386 unsigned long reset_migrate = free_pfn;
387 unsigned long reset_free = migrate_pfn;
388 bool source_set = false;
389 bool free_set = false;
391 /* Only flush if a full compaction finished recently */
392 if (!zone->compact_blockskip_flush)
395 zone->compact_blockskip_flush = false;
398 * Walk the zone and update pageblock skip information. Source looks
399 * for PageLRU while target looks for PageBuddy. When the scanner
400 * is found, both PageBuddy and PageLRU are checked as the pageblock
401 * is suitable as both source and target.
403 for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
404 free_pfn -= pageblock_nr_pages) {
407 /* Update the migrate PFN */
408 if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
409 migrate_pfn < reset_migrate) {
411 reset_migrate = migrate_pfn;
412 zone->compact_init_migrate_pfn = reset_migrate;
413 zone->compact_cached_migrate_pfn[0] = reset_migrate;
414 zone->compact_cached_migrate_pfn[1] = reset_migrate;
417 /* Update the free PFN */
418 if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
419 free_pfn > reset_free) {
421 reset_free = free_pfn;
422 zone->compact_init_free_pfn = reset_free;
423 zone->compact_cached_free_pfn = reset_free;
427 /* Leave no distance if no suitable block was reset */
428 if (reset_migrate >= reset_free) {
429 zone->compact_cached_migrate_pfn[0] = migrate_pfn;
430 zone->compact_cached_migrate_pfn[1] = migrate_pfn;
431 zone->compact_cached_free_pfn = free_pfn;
435 void reset_isolation_suitable(pg_data_t *pgdat)
439 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
440 struct zone *zone = &pgdat->node_zones[zoneid];
441 if (!populated_zone(zone))
444 __reset_isolation_suitable(zone);
449 * Sets the pageblock skip bit if it was clear. Note that this is a hint as
450 * locks are not required for read/writers. Returns true if it was already set.
452 static bool test_and_set_skip(struct compact_control *cc, struct page *page)
456 /* Do not update if skip hint is being ignored */
457 if (cc->ignore_skip_hint)
460 skip = get_pageblock_skip(page);
461 if (!skip && !cc->no_set_skip_hint)
462 set_pageblock_skip(page);
467 static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
469 struct zone *zone = cc->zone;
471 /* Set for isolation rather than compaction */
472 if (cc->no_set_skip_hint)
475 pfn = pageblock_end_pfn(pfn);
477 /* Update where async and sync compaction should restart */
478 if (pfn > zone->compact_cached_migrate_pfn[0])
479 zone->compact_cached_migrate_pfn[0] = pfn;
480 if (cc->mode != MIGRATE_ASYNC &&
481 pfn > zone->compact_cached_migrate_pfn[1])
482 zone->compact_cached_migrate_pfn[1] = pfn;
486 * If no pages were isolated then mark this pageblock to be skipped in the
487 * future. The information is later cleared by __reset_isolation_suitable().
489 static void update_pageblock_skip(struct compact_control *cc,
490 struct page *page, unsigned long pfn)
492 struct zone *zone = cc->zone;
494 if (cc->no_set_skip_hint)
497 set_pageblock_skip(page);
499 if (pfn < zone->compact_cached_free_pfn)
500 zone->compact_cached_free_pfn = pfn;
503 static inline bool isolation_suitable(struct compact_control *cc,
509 static inline bool pageblock_skip_persistent(struct page *page)
514 static inline void update_pageblock_skip(struct compact_control *cc,
515 struct page *page, unsigned long pfn)
519 static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
523 static bool test_and_set_skip(struct compact_control *cc, struct page *page)
527 #endif /* CONFIG_COMPACTION */
530 * Compaction requires the taking of some coarse locks that are potentially
531 * very heavily contended. For async compaction, trylock and record if the
532 * lock is contended. The lock will still be acquired but compaction will
533 * abort when the current block is finished regardless of success rate.
534 * Sync compaction acquires the lock.
536 * Always returns true which makes it easier to track lock state in callers.
538 static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
539 struct compact_control *cc)
542 /* Track if the lock is contended in async mode */
543 if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
544 if (spin_trylock_irqsave(lock, *flags))
547 cc->contended = true;
550 spin_lock_irqsave(lock, *flags);
555 * Compaction requires the taking of some coarse locks that are potentially
556 * very heavily contended. The lock should be periodically unlocked to avoid
557 * having disabled IRQs for a long time, even when there is nobody waiting on
558 * the lock. It might also be that allowing the IRQs will result in
559 * need_resched() becoming true. If scheduling is needed, compaction schedules.
560 * Either compaction type will also abort if a fatal signal is pending.
561 * In either case if the lock was locked, it is dropped and not regained.
563 * Returns true if compaction should abort due to fatal signal pending.
564 * Returns false when compaction can continue.
566 static bool compact_unlock_should_abort(spinlock_t *lock,
567 unsigned long flags, bool *locked, struct compact_control *cc)
570 spin_unlock_irqrestore(lock, flags);
574 if (fatal_signal_pending(current)) {
575 cc->contended = true;
585 * Isolate free pages onto a private freelist. If @strict is true, will abort
586 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
587 * (even though it may still end up isolating some pages).
589 static unsigned long isolate_freepages_block(struct compact_control *cc,
590 unsigned long *start_pfn,
591 unsigned long end_pfn,
592 struct list_head *freelist,
596 int nr_scanned = 0, total_isolated = 0;
598 unsigned long flags = 0;
600 unsigned long blockpfn = *start_pfn;
603 /* Strict mode is for isolation, speed is secondary */
607 page = pfn_to_page(blockpfn);
609 /* Isolate free pages. */
610 for (; blockpfn < end_pfn; blockpfn += stride, page += stride) {
614 * Periodically drop the lock (if held) regardless of its
615 * contention, to give chance to IRQs. Abort if fatal signal
618 if (!(blockpfn % COMPACT_CLUSTER_MAX)
619 && compact_unlock_should_abort(&cc->zone->lock, flags,
626 * For compound pages such as THP and hugetlbfs, we can save
627 * potentially a lot of iterations if we skip them at once.
628 * The check is racy, but we can consider only valid values
629 * and the only danger is skipping too much.
631 if (PageCompound(page)) {
632 const unsigned int order = compound_order(page);
634 if ((order <= MAX_PAGE_ORDER) &&
635 (blockpfn + (1UL << order) <= end_pfn)) {
636 blockpfn += (1UL << order) - 1;
637 page += (1UL << order) - 1;
638 nr_scanned += (1UL << order) - 1;
644 if (!PageBuddy(page))
647 /* If we already hold the lock, we can skip some rechecking. */
649 locked = compact_lock_irqsave(&cc->zone->lock,
652 /* Recheck this is a buddy page under lock */
653 if (!PageBuddy(page))
657 /* Found a free page, will break it into order-0 pages */
658 order = buddy_order(page);
659 isolated = __isolate_free_page(page, order);
662 set_page_private(page, order);
664 nr_scanned += isolated - 1;
665 total_isolated += isolated;
666 cc->nr_freepages += isolated;
667 list_add_tail(&page->lru, &freelist[order]);
669 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
670 blockpfn += isolated;
673 /* Advance to the end of split page */
674 blockpfn += isolated - 1;
675 page += isolated - 1;
685 spin_unlock_irqrestore(&cc->zone->lock, flags);
688 * Be careful to not go outside of the pageblock.
690 if (unlikely(blockpfn > end_pfn))
693 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
694 nr_scanned, total_isolated);
696 /* Record how far we have got within the block */
697 *start_pfn = blockpfn;
700 * If strict isolation is requested by CMA then check that all the
701 * pages requested were isolated. If there were any failures, 0 is
702 * returned and CMA will fail.
704 if (strict && blockpfn < end_pfn)
707 cc->total_free_scanned += nr_scanned;
709 count_compact_events(COMPACTISOLATED, total_isolated);
710 return total_isolated;
714 * isolate_freepages_range() - isolate free pages.
715 * @cc: Compaction control structure.
716 * @start_pfn: The first PFN to start isolating.
717 * @end_pfn: The one-past-last PFN.
719 * Non-free pages, invalid PFNs, or zone boundaries within the
720 * [start_pfn, end_pfn) range are considered errors, cause function to
721 * undo its actions and return zero. cc->freepages[] are empty.
723 * Otherwise, function returns one-past-the-last PFN of isolated page
724 * (which may be greater then end_pfn if end fell in a middle of
725 * a free page). cc->freepages[] contain free pages isolated.
728 isolate_freepages_range(struct compact_control *cc,
729 unsigned long start_pfn, unsigned long end_pfn)
731 unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
734 for (order = 0; order < NR_PAGE_ORDERS; order++)
735 INIT_LIST_HEAD(&cc->freepages[order]);
738 block_start_pfn = pageblock_start_pfn(pfn);
739 if (block_start_pfn < cc->zone->zone_start_pfn)
740 block_start_pfn = cc->zone->zone_start_pfn;
741 block_end_pfn = pageblock_end_pfn(pfn);
743 for (; pfn < end_pfn; pfn += isolated,
744 block_start_pfn = block_end_pfn,
745 block_end_pfn += pageblock_nr_pages) {
746 /* Protect pfn from changing by isolate_freepages_block */
747 unsigned long isolate_start_pfn = pfn;
750 * pfn could pass the block_end_pfn if isolated freepage
751 * is more than pageblock order. In this case, we adjust
752 * scanning range to right one.
754 if (pfn >= block_end_pfn) {
755 block_start_pfn = pageblock_start_pfn(pfn);
756 block_end_pfn = pageblock_end_pfn(pfn);
759 block_end_pfn = min(block_end_pfn, end_pfn);
761 if (!pageblock_pfn_to_page(block_start_pfn,
762 block_end_pfn, cc->zone))
765 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
766 block_end_pfn, cc->freepages, 0, true);
769 * In strict mode, isolate_freepages_block() returns 0 if
770 * there are any holes in the block (ie. invalid PFNs or
777 * If we managed to isolate pages, it is always (1 << n) *
778 * pageblock_nr_pages for some non-negative n. (Max order
779 * page may span two pageblocks).
784 /* Loop terminated early, cleanup. */
785 release_free_list(cc->freepages);
789 /* We don't use freelists for anything. */
793 /* Similar to reclaim, but different enough that they don't share logic */
794 static bool too_many_isolated(struct compact_control *cc)
796 pg_data_t *pgdat = cc->zone->zone_pgdat;
799 unsigned long active, inactive, isolated;
801 inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
802 node_page_state(pgdat, NR_INACTIVE_ANON);
803 active = node_page_state(pgdat, NR_ACTIVE_FILE) +
804 node_page_state(pgdat, NR_ACTIVE_ANON);
805 isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
806 node_page_state(pgdat, NR_ISOLATED_ANON);
809 * Allow GFP_NOFS to isolate past the limit set for regular
810 * compaction runs. This prevents an ABBA deadlock when other
811 * compactors have already isolated to the limit, but are
812 * blocked on filesystem locks held by the GFP_NOFS thread.
814 if (cc->gfp_mask & __GFP_FS) {
819 too_many = isolated > (inactive + active) / 2;
821 wake_throttle_isolated(pgdat);
827 * skip_isolation_on_order() - determine when to skip folio isolation based on
828 * folio order and compaction target order
829 * @order: to-be-isolated folio order
830 * @target_order: compaction target order
832 * This avoids unnecessary folio isolations during compaction.
834 static bool skip_isolation_on_order(int order, int target_order)
837 * Unless we are performing global compaction (i.e.,
838 * is_via_compact_memory), skip any folios that are larger than the
839 * target order: we wouldn't be here if we'd have a free folio with
840 * the desired target_order, so migrating this folio would likely fail
843 if (!is_via_compact_memory(target_order) && order >= target_order)
846 * We limit memory compaction to pageblocks and won't try
847 * creating free blocks of memory that are larger than that.
849 return order >= pageblock_order;
853 * isolate_migratepages_block() - isolate all migrate-able pages within
855 * @cc: Compaction control structure.
856 * @low_pfn: The first PFN to isolate
857 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
858 * @mode: Isolation mode to be used.
860 * Isolate all pages that can be migrated from the range specified by
861 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
862 * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion,
863 * -ENOMEM in case we could not allocate a page, or 0.
864 * cc->migrate_pfn will contain the next pfn to scan.
866 * The pages are isolated on cc->migratepages list (not required to be empty),
867 * and cc->nr_migratepages is updated accordingly.
870 isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
871 unsigned long end_pfn, isolate_mode_t mode)
873 pg_data_t *pgdat = cc->zone->zone_pgdat;
874 unsigned long nr_scanned = 0, nr_isolated = 0;
875 struct lruvec *lruvec;
876 unsigned long flags = 0;
877 struct lruvec *locked = NULL;
878 struct folio *folio = NULL;
879 struct page *page = NULL, *valid_page = NULL;
880 struct address_space *mapping;
881 unsigned long start_pfn = low_pfn;
882 bool skip_on_failure = false;
883 unsigned long next_skip_pfn = 0;
884 bool skip_updated = false;
887 cc->migrate_pfn = low_pfn;
890 * Ensure that there are not too many pages isolated from the LRU
891 * list by either parallel reclaimers or compaction. If there are,
892 * delay for some time until fewer pages are isolated
894 while (unlikely(too_many_isolated(cc))) {
895 /* stop isolation if there are still pages not migrated */
896 if (cc->nr_migratepages)
899 /* async migration should just abort */
900 if (cc->mode == MIGRATE_ASYNC)
903 reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED);
905 if (fatal_signal_pending(current))
911 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
912 skip_on_failure = true;
913 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
916 /* Time to isolate some pages for migration */
917 for (; low_pfn < end_pfn; low_pfn++) {
918 bool is_dirty, is_unevictable;
920 if (skip_on_failure && low_pfn >= next_skip_pfn) {
922 * We have isolated all migration candidates in the
923 * previous order-aligned block, and did not skip it due
924 * to failure. We should migrate the pages now and
925 * hopefully succeed compaction.
931 * We failed to isolate in the previous order-aligned
932 * block. Set the new boundary to the end of the
933 * current block. Note we can't simply increase
934 * next_skip_pfn by 1 << order, as low_pfn might have
935 * been incremented by a higher number due to skipping
936 * a compound or a high-order buddy page in the
937 * previous loop iteration.
939 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
943 * Periodically drop the lock (if held) regardless of its
944 * contention, to give chance to IRQs. Abort completely if
945 * a fatal signal is pending.
947 if (!(low_pfn % COMPACT_CLUSTER_MAX)) {
949 unlock_page_lruvec_irqrestore(locked, flags);
953 if (fatal_signal_pending(current)) {
954 cc->contended = true;
965 page = pfn_to_page(low_pfn);
968 * Check if the pageblock has already been marked skipped.
969 * Only the first PFN is checked as the caller isolates
970 * COMPACT_CLUSTER_MAX at a time so the second call must
971 * not falsely conclude that the block should be skipped.
973 if (!valid_page && (pageblock_aligned(low_pfn) ||
974 low_pfn == cc->zone->zone_start_pfn)) {
975 if (!isolation_suitable(cc, page)) {
983 if (PageHuge(page)) {
984 const unsigned int order = compound_order(page);
986 * skip hugetlbfs if we are not compacting for pages
987 * bigger than its order. THPs and other compound pages
990 if (!cc->alloc_contig) {
992 if (order <= MAX_PAGE_ORDER) {
993 low_pfn += (1UL << order) - 1;
994 nr_scanned += (1UL << order) - 1;
998 /* for alloc_contig case */
1000 unlock_page_lruvec_irqrestore(locked, flags);
1004 folio = page_folio(page);
1005 ret = isolate_or_dissolve_huge_folio(folio, &cc->migratepages);
1008 * Fail isolation in case isolate_or_dissolve_huge_folio()
1009 * reports an error. In case of -ENOMEM, abort right away.
1012 /* Do not report -EBUSY down the chain */
1015 low_pfn += (1UL << order) - 1;
1016 nr_scanned += (1UL << order) - 1;
1020 if (folio_test_hugetlb(folio)) {
1022 * Hugepage was successfully isolated and placed
1023 * on the cc->migratepages list.
1025 low_pfn += folio_nr_pages(folio) - 1;
1026 goto isolate_success_no_list;
1030 * Ok, the hugepage was dissolved. Now these pages are
1031 * Buddy and cannot be re-allocated because they are
1032 * isolated. Fall-through as the check below handles
1038 * Skip if free. We read page order here without zone lock
1039 * which is generally unsafe, but the race window is small and
1040 * the worst thing that can happen is that we skip some
1041 * potential isolation targets.
1043 if (PageBuddy(page)) {
1044 unsigned long freepage_order = buddy_order_unsafe(page);
1047 * Without lock, we cannot be sure that what we got is
1048 * a valid page order. Consider only values in the
1049 * valid order range to prevent low_pfn overflow.
1051 if (freepage_order > 0 && freepage_order <= MAX_PAGE_ORDER) {
1052 low_pfn += (1UL << freepage_order) - 1;
1053 nr_scanned += (1UL << freepage_order) - 1;
1059 * Regardless of being on LRU, compound pages such as THP
1060 * (hugetlbfs is handled above) are not to be compacted unless
1061 * we are attempting an allocation larger than the compound
1062 * page size. We can potentially save a lot of iterations if we
1063 * skip them at once. The check is racy, but we can consider
1064 * only valid values and the only danger is skipping too much.
1066 if (PageCompound(page) && !cc->alloc_contig) {
1067 const unsigned int order = compound_order(page);
1069 /* Skip based on page order and compaction target order. */
1070 if (skip_isolation_on_order(order, cc->order)) {
1071 if (order <= MAX_PAGE_ORDER) {
1072 low_pfn += (1UL << order) - 1;
1073 nr_scanned += (1UL << order) - 1;
1080 * Check may be lockless but that's ok as we recheck later.
1081 * It's possible to migrate LRU and non-lru movable pages.
1082 * Skip any other type of page
1084 if (!PageLRU(page)) {
1086 * __PageMovable can return false positive so we need
1087 * to verify it under page_lock.
1089 if (unlikely(__PageMovable(page)) &&
1090 !PageIsolated(page)) {
1092 unlock_page_lruvec_irqrestore(locked, flags);
1096 if (isolate_movable_page(page, mode)) {
1097 folio = page_folio(page);
1098 goto isolate_success;
1106 * Be careful not to clear PageLRU until after we're
1107 * sure the page is not being freed elsewhere -- the
1108 * page release code relies on it.
1110 folio = folio_get_nontail_page(page);
1111 if (unlikely(!folio))
1115 * Migration will fail if an anonymous page is pinned in memory,
1116 * so avoid taking lru_lock and isolating it unnecessarily in an
1117 * admittedly racy check.
1119 mapping = folio_mapping(folio);
1120 if (!mapping && (folio_ref_count(folio) - 1) > folio_mapcount(folio))
1121 goto isolate_fail_put;
1124 * Only allow to migrate anonymous pages in GFP_NOFS context
1125 * because those do not depend on fs locks.
1127 if (!(cc->gfp_mask & __GFP_FS) && mapping)
1128 goto isolate_fail_put;
1130 /* Only take pages on LRU: a check now makes later tests safe */
1131 if (!folio_test_lru(folio))
1132 goto isolate_fail_put;
1134 is_unevictable = folio_test_unevictable(folio);
1136 /* Compaction might skip unevictable pages but CMA takes them */
1137 if (!(mode & ISOLATE_UNEVICTABLE) && is_unevictable)
1138 goto isolate_fail_put;
1141 * To minimise LRU disruption, the caller can indicate with
1142 * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages
1143 * it will be able to migrate without blocking - clean pages
1144 * for the most part. PageWriteback would require blocking.
1146 if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_writeback(folio))
1147 goto isolate_fail_put;
1149 is_dirty = folio_test_dirty(folio);
1151 if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) ||
1152 (mapping && is_unevictable)) {
1153 bool migrate_dirty = true;
1154 bool is_inaccessible;
1157 * Only folios without mappings or that have
1158 * a ->migrate_folio callback are possible to migrate
1161 * Folios from inaccessible mappings are not migratable.
1163 * However, we can be racing with truncation, which can
1164 * free the mapping that we need to check. Truncation
1165 * holds the folio lock until after the folio is removed
1166 * from the page so holding it ourselves is sufficient.
1168 * To avoid locking the folio just to check inaccessible,
1169 * assume every inaccessible folio is also unevictable,
1170 * which is a cheaper test. If our assumption goes
1171 * wrong, it's not a correctness bug, just potentially
1174 if (!folio_trylock(folio))
1175 goto isolate_fail_put;
1177 mapping = folio_mapping(folio);
1178 if ((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) {
1179 migrate_dirty = !mapping ||
1180 mapping->a_ops->migrate_folio;
1182 is_inaccessible = mapping && mapping_inaccessible(mapping);
1183 folio_unlock(folio);
1184 if (!migrate_dirty || is_inaccessible)
1185 goto isolate_fail_put;
1188 /* Try isolate the folio */
1189 if (!folio_test_clear_lru(folio))
1190 goto isolate_fail_put;
1192 lruvec = folio_lruvec(folio);
1194 /* If we already hold the lock, we can skip some rechecking */
1195 if (lruvec != locked) {
1197 unlock_page_lruvec_irqrestore(locked, flags);
1199 compact_lock_irqsave(&lruvec->lru_lock, &flags, cc);
1202 lruvec_memcg_debug(lruvec, folio);
1205 * Try get exclusive access under lock. If marked for
1206 * skip, the scan is aborted unless the current context
1207 * is a rescan to reach the end of the pageblock.
1209 if (!skip_updated && valid_page) {
1210 skip_updated = true;
1211 if (test_and_set_skip(cc, valid_page) &&
1212 !cc->finish_pageblock) {
1219 * Check LRU folio order under the lock
1221 if (unlikely(skip_isolation_on_order(folio_order(folio),
1223 !cc->alloc_contig)) {
1224 low_pfn += folio_nr_pages(folio) - 1;
1225 nr_scanned += folio_nr_pages(folio) - 1;
1226 folio_set_lru(folio);
1227 goto isolate_fail_put;
1231 /* The folio is taken off the LRU */
1232 if (folio_test_large(folio))
1233 low_pfn += folio_nr_pages(folio) - 1;
1235 /* Successfully isolated */
1236 lruvec_del_folio(lruvec, folio);
1237 node_stat_mod_folio(folio,
1238 NR_ISOLATED_ANON + folio_is_file_lru(folio),
1239 folio_nr_pages(folio));
1242 list_add(&folio->lru, &cc->migratepages);
1243 isolate_success_no_list:
1244 cc->nr_migratepages += folio_nr_pages(folio);
1245 nr_isolated += folio_nr_pages(folio);
1246 nr_scanned += folio_nr_pages(folio) - 1;
1249 * Avoid isolating too much unless this block is being
1250 * fully scanned (e.g. dirty/writeback pages, parallel allocation)
1251 * or a lock is contended. For contention, isolate quickly to
1252 * potentially remove one source of contention.
1254 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
1255 !cc->finish_pageblock && !cc->contended) {
1263 /* Avoid potential deadlock in freeing page under lru_lock */
1265 unlock_page_lruvec_irqrestore(locked, flags);
1271 if (!skip_on_failure && ret != -ENOMEM)
1275 * We have isolated some pages, but then failed. Release them
1276 * instead of migrating, as we cannot form the cc->order buddy
1281 unlock_page_lruvec_irqrestore(locked, flags);
1284 putback_movable_pages(&cc->migratepages);
1285 cc->nr_migratepages = 0;
1289 if (low_pfn < next_skip_pfn) {
1290 low_pfn = next_skip_pfn - 1;
1292 * The check near the loop beginning would have updated
1293 * next_skip_pfn too, but this is a bit simpler.
1295 next_skip_pfn += 1UL << cc->order;
1303 * The PageBuddy() check could have potentially brought us outside
1304 * the range to be scanned.
1306 if (unlikely(low_pfn > end_pfn))
1313 unlock_page_lruvec_irqrestore(locked, flags);
1315 folio_set_lru(folio);
1320 * Update the cached scanner pfn once the pageblock has been scanned.
1321 * Pages will either be migrated in which case there is no point
1322 * scanning in the near future or migration failed in which case the
1323 * failure reason may persist. The block is marked for skipping if
1324 * there were no pages isolated in the block or if the block is
1325 * rescanned twice in a row.
1327 if (low_pfn == end_pfn && (!nr_isolated || cc->finish_pageblock)) {
1328 if (!cc->no_set_skip_hint && valid_page && !skip_updated)
1329 set_pageblock_skip(valid_page);
1330 update_cached_migrate(cc, low_pfn);
1333 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1334 nr_scanned, nr_isolated);
1337 cc->total_migrate_scanned += nr_scanned;
1339 count_compact_events(COMPACTISOLATED, nr_isolated);
1341 cc->migrate_pfn = low_pfn;
1347 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1348 * @cc: Compaction control structure.
1349 * @start_pfn: The first PFN to start isolating.
1350 * @end_pfn: The one-past-last PFN.
1352 * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM
1353 * in case we could not allocate a page, or 0.
1356 isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1357 unsigned long end_pfn)
1359 unsigned long pfn, block_start_pfn, block_end_pfn;
1362 /* Scan block by block. First and last block may be incomplete */
1364 block_start_pfn = pageblock_start_pfn(pfn);
1365 if (block_start_pfn < cc->zone->zone_start_pfn)
1366 block_start_pfn = cc->zone->zone_start_pfn;
1367 block_end_pfn = pageblock_end_pfn(pfn);
1369 for (; pfn < end_pfn; pfn = block_end_pfn,
1370 block_start_pfn = block_end_pfn,
1371 block_end_pfn += pageblock_nr_pages) {
1373 block_end_pfn = min(block_end_pfn, end_pfn);
1375 if (!pageblock_pfn_to_page(block_start_pfn,
1376 block_end_pfn, cc->zone))
1379 ret = isolate_migratepages_block(cc, pfn, block_end_pfn,
1380 ISOLATE_UNEVICTABLE);
1385 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
1392 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1393 #ifdef CONFIG_COMPACTION
1395 static bool suitable_migration_source(struct compact_control *cc,
1400 if (pageblock_skip_persistent(page))
1403 if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1406 block_mt = get_pageblock_migratetype(page);
1408 if (cc->migratetype == MIGRATE_MOVABLE)
1409 return is_migrate_movable(block_mt);
1411 return block_mt == cc->migratetype;
1414 /* Returns true if the page is within a block suitable for migration to */
1415 static bool suitable_migration_target(struct compact_control *cc,
1418 /* If the page is a large free page, then disallow migration */
1419 if (PageBuddy(page)) {
1420 int order = cc->order > 0 ? cc->order : pageblock_order;
1423 * We are checking page_order without zone->lock taken. But
1424 * the only small danger is that we skip a potentially suitable
1425 * pageblock, so it's not worth to check order for valid range.
1427 if (buddy_order_unsafe(page) >= order)
1431 if (cc->ignore_block_suitable)
1434 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1435 if (is_migrate_movable(get_pageblock_migratetype(page)))
1438 /* Otherwise skip the block */
1442 static inline unsigned int
1443 freelist_scan_limit(struct compact_control *cc)
1445 unsigned short shift = BITS_PER_LONG - 1;
1447 return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
1451 * Test whether the free scanner has reached the same or lower pageblock than
1452 * the migration scanner, and compaction should thus terminate.
1454 static inline bool compact_scanners_met(struct compact_control *cc)
1456 return (cc->free_pfn >> pageblock_order)
1457 <= (cc->migrate_pfn >> pageblock_order);
1461 * Used when scanning for a suitable migration target which scans freelists
1462 * in reverse. Reorders the list such as the unscanned pages are scanned
1463 * first on the next iteration of the free scanner
1466 move_freelist_head(struct list_head *freelist, struct page *freepage)
1470 if (!list_is_first(&freepage->buddy_list, freelist)) {
1471 list_cut_before(&sublist, freelist, &freepage->buddy_list);
1472 list_splice_tail(&sublist, freelist);
1477 * Similar to move_freelist_head except used by the migration scanner
1478 * when scanning forward. It's possible for these list operations to
1479 * move against each other if they search the free list exactly in
1483 move_freelist_tail(struct list_head *freelist, struct page *freepage)
1487 if (!list_is_last(&freepage->buddy_list, freelist)) {
1488 list_cut_position(&sublist, freelist, &freepage->buddy_list);
1489 list_splice_tail(&sublist, freelist);
1494 fast_isolate_around(struct compact_control *cc, unsigned long pfn)
1496 unsigned long start_pfn, end_pfn;
1499 /* Do not search around if there are enough pages already */
1500 if (cc->nr_freepages >= cc->nr_migratepages)
1503 /* Minimise scanning during async compaction */
1504 if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
1507 /* Pageblock boundaries */
1508 start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
1509 end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
1511 page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
1515 isolate_freepages_block(cc, &start_pfn, end_pfn, cc->freepages, 1, false);
1517 /* Skip this pageblock in the future as it's full or nearly full */
1518 if (start_pfn == end_pfn && !cc->no_set_skip_hint)
1519 set_pageblock_skip(page);
1522 /* Search orders in round-robin fashion */
1523 static int next_search_order(struct compact_control *cc, int order)
1527 order = cc->order - 1;
1529 /* Search wrapped around? */
1530 if (order == cc->search_order) {
1532 if (cc->search_order < 0)
1533 cc->search_order = cc->order - 1;
1540 static void fast_isolate_freepages(struct compact_control *cc)
1542 unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1);
1543 unsigned int nr_scanned = 0, total_isolated = 0;
1544 unsigned long low_pfn, min_pfn, highest = 0;
1545 unsigned long nr_isolated = 0;
1546 unsigned long distance;
1547 struct page *page = NULL;
1548 bool scan_start = false;
1551 /* Full compaction passes in a negative order */
1556 * If starting the scan, use a deeper search and use the highest
1557 * PFN found if a suitable one is not found.
1559 if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
1560 limit = pageblock_nr_pages >> 1;
1565 * Preferred point is in the top quarter of the scan space but take
1566 * a pfn from the top half if the search is problematic.
1568 distance = (cc->free_pfn - cc->migrate_pfn);
1569 low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
1570 min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
1572 if (WARN_ON_ONCE(min_pfn > low_pfn))
1576 * Search starts from the last successful isolation order or the next
1577 * order to search after a previous failure
1579 cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1581 for (order = cc->search_order;
1582 !page && order >= 0;
1583 order = next_search_order(cc, order)) {
1584 struct free_area *area = &cc->zone->free_area[order];
1585 struct list_head *freelist;
1586 struct page *freepage;
1587 unsigned long flags;
1588 unsigned int order_scanned = 0;
1589 unsigned long high_pfn = 0;
1594 spin_lock_irqsave(&cc->zone->lock, flags);
1595 freelist = &area->free_list[MIGRATE_MOVABLE];
1596 list_for_each_entry_reverse(freepage, freelist, buddy_list) {
1601 pfn = page_to_pfn(freepage);
1604 highest = max(pageblock_start_pfn(pfn),
1605 cc->zone->zone_start_pfn);
1607 if (pfn >= low_pfn) {
1608 cc->fast_search_fail = 0;
1609 cc->search_order = order;
1614 if (pfn >= min_pfn && pfn > high_pfn) {
1617 /* Shorten the scan if a candidate is found */
1621 if (order_scanned >= limit)
1625 /* Use a maximum candidate pfn if a preferred one was not found */
1626 if (!page && high_pfn) {
1627 page = pfn_to_page(high_pfn);
1629 /* Update freepage for the list reorder below */
1633 /* Reorder to so a future search skips recent pages */
1634 move_freelist_head(freelist, freepage);
1636 /* Isolate the page if available */
1638 if (__isolate_free_page(page, order)) {
1639 set_page_private(page, order);
1640 nr_isolated = 1 << order;
1641 nr_scanned += nr_isolated - 1;
1642 total_isolated += nr_isolated;
1643 cc->nr_freepages += nr_isolated;
1644 list_add_tail(&page->lru, &cc->freepages[order]);
1645 count_compact_events(COMPACTISOLATED, nr_isolated);
1647 /* If isolation fails, abort the search */
1648 order = cc->search_order + 1;
1653 spin_unlock_irqrestore(&cc->zone->lock, flags);
1655 /* Skip fast search if enough freepages isolated */
1656 if (cc->nr_freepages >= cc->nr_migratepages)
1660 * Smaller scan on next order so the total scan is related
1661 * to freelist_scan_limit.
1663 if (order_scanned >= limit)
1664 limit = max(1U, limit >> 1);
1667 trace_mm_compaction_fast_isolate_freepages(min_pfn, cc->free_pfn,
1668 nr_scanned, total_isolated);
1671 cc->fast_search_fail++;
1674 * Use the highest PFN found above min. If one was
1675 * not found, be pessimistic for direct compaction
1676 * and use the min mark.
1678 if (highest >= min_pfn) {
1679 page = pfn_to_page(highest);
1680 cc->free_pfn = highest;
1682 if (cc->direct_compaction && pfn_valid(min_pfn)) {
1683 page = pageblock_pfn_to_page(min_pfn,
1684 min(pageblock_end_pfn(min_pfn),
1685 zone_end_pfn(cc->zone)),
1687 if (page && !suitable_migration_target(cc, page))
1690 cc->free_pfn = min_pfn;
1696 if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1697 highest -= pageblock_nr_pages;
1698 cc->zone->compact_cached_free_pfn = highest;
1701 cc->total_free_scanned += nr_scanned;
1705 low_pfn = page_to_pfn(page);
1706 fast_isolate_around(cc, low_pfn);
1710 * Based on information in the current compact_control, find blocks
1711 * suitable for isolating free pages from and then isolate them.
1713 static void isolate_freepages(struct compact_control *cc)
1715 struct zone *zone = cc->zone;
1717 unsigned long block_start_pfn; /* start of current pageblock */
1718 unsigned long isolate_start_pfn; /* exact pfn we start at */
1719 unsigned long block_end_pfn; /* end of current pageblock */
1720 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
1721 unsigned int stride;
1723 /* Try a small search of the free lists for a candidate */
1724 fast_isolate_freepages(cc);
1725 if (cc->nr_freepages)
1729 * Initialise the free scanner. The starting point is where we last
1730 * successfully isolated from, zone-cached value, or the end of the
1731 * zone when isolating for the first time. For looping we also need
1732 * this pfn aligned down to the pageblock boundary, because we do
1733 * block_start_pfn -= pageblock_nr_pages in the for loop.
1734 * For ending point, take care when isolating in last pageblock of a
1735 * zone which ends in the middle of a pageblock.
1736 * The low boundary is the end of the pageblock the migration scanner
1739 isolate_start_pfn = cc->free_pfn;
1740 block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
1741 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1742 zone_end_pfn(zone));
1743 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
1744 stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
1747 * Isolate free pages until enough are available to migrate the
1748 * pages on cc->migratepages. We stop searching if the migrate
1749 * and free page scanners meet or enough free pages are isolated.
1751 for (; block_start_pfn >= low_pfn;
1752 block_end_pfn = block_start_pfn,
1753 block_start_pfn -= pageblock_nr_pages,
1754 isolate_start_pfn = block_start_pfn) {
1755 unsigned long nr_isolated;
1758 * This can iterate a massively long zone without finding any
1759 * suitable migration targets, so periodically check resched.
1761 if (!(block_start_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages)))
1764 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1767 unsigned long next_pfn;
1769 next_pfn = skip_offline_sections_reverse(block_start_pfn);
1771 block_start_pfn = max(next_pfn, low_pfn);
1776 /* Check the block is suitable for migration */
1777 if (!suitable_migration_target(cc, page))
1780 /* If isolation recently failed, do not retry */
1781 if (!isolation_suitable(cc, page))
1784 /* Found a block suitable for isolating free pages from. */
1785 nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
1786 block_end_pfn, cc->freepages, stride, false);
1788 /* Update the skip hint if the full pageblock was scanned */
1789 if (isolate_start_pfn == block_end_pfn)
1790 update_pageblock_skip(cc, page, block_start_pfn -
1791 pageblock_nr_pages);
1793 /* Are enough freepages isolated? */
1794 if (cc->nr_freepages >= cc->nr_migratepages) {
1795 if (isolate_start_pfn >= block_end_pfn) {
1797 * Restart at previous pageblock if more
1798 * freepages can be isolated next time.
1801 block_start_pfn - pageblock_nr_pages;
1804 } else if (isolate_start_pfn < block_end_pfn) {
1806 * If isolation failed early, do not continue
1812 /* Adjust stride depending on isolation */
1817 stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1821 * Record where the free scanner will restart next time. Either we
1822 * broke from the loop and set isolate_start_pfn based on the last
1823 * call to isolate_freepages_block(), or we met the migration scanner
1824 * and the loop terminated due to isolate_start_pfn < low_pfn
1826 cc->free_pfn = isolate_start_pfn;
1830 * This is a migrate-callback that "allocates" freepages by taking pages
1831 * from the isolated freelists in the block we are migrating to.
1833 static struct folio *compaction_alloc_noprof(struct folio *src, unsigned long data)
1835 struct compact_control *cc = (struct compact_control *)data;
1837 int order = folio_order(src);
1838 bool has_isolated_pages = false;
1840 struct page *freepage;
1844 for (start_order = order; start_order < NR_PAGE_ORDERS; start_order++)
1845 if (!list_empty(&cc->freepages[start_order]))
1848 /* no free pages in the list */
1849 if (start_order == NR_PAGE_ORDERS) {
1850 if (has_isolated_pages)
1852 isolate_freepages(cc);
1853 has_isolated_pages = true;
1857 freepage = list_first_entry(&cc->freepages[start_order], struct page,
1859 size = 1 << start_order;
1861 list_del(&freepage->lru);
1863 while (start_order > order) {
1867 list_add(&freepage[size].lru, &cc->freepages[start_order]);
1868 set_page_private(&freepage[size], start_order);
1870 dst = (struct folio *)freepage;
1872 post_alloc_hook(&dst->page, order, __GFP_MOVABLE);
1873 set_page_refcounted(&dst->page);
1875 prep_compound_page(&dst->page, order);
1876 cc->nr_freepages -= 1 << order;
1877 cc->nr_migratepages -= 1 << order;
1878 return page_rmappable_folio(&dst->page);
1881 static struct folio *compaction_alloc(struct folio *src, unsigned long data)
1883 return alloc_hooks(compaction_alloc_noprof(src, data));
1887 * This is a migrate-callback that "frees" freepages back to the isolated
1888 * freelist. All pages on the freelist are from the same zone, so there is no
1889 * special handling needed for NUMA.
1891 static void compaction_free(struct folio *dst, unsigned long data)
1893 struct compact_control *cc = (struct compact_control *)data;
1894 int order = folio_order(dst);
1895 struct page *page = &dst->page;
1897 if (folio_put_testzero(dst)) {
1898 free_pages_prepare(page, order);
1899 list_add(&dst->lru, &cc->freepages[order]);
1900 cc->nr_freepages += 1 << order;
1902 cc->nr_migratepages += 1 << order;
1904 * someone else has referenced the page, we cannot take it back to our
1909 /* possible outcome of isolate_migratepages */
1911 ISOLATE_ABORT, /* Abort compaction now */
1912 ISOLATE_NONE, /* No pages isolated, continue scanning */
1913 ISOLATE_SUCCESS, /* Pages isolated, migrate */
1914 } isolate_migrate_t;
1917 * Allow userspace to control policy on scanning the unevictable LRU for
1918 * compactable pages.
1920 static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNEVICTABLE_DEFAULT;
1922 * Tunable for proactive compaction. It determines how
1923 * aggressively the kernel should compact memory in the
1924 * background. It takes values in the range [0, 100].
1926 static unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
1927 static int sysctl_extfrag_threshold = 500;
1928 static int __read_mostly sysctl_compact_memory;
1931 update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
1933 if (cc->fast_start_pfn == ULONG_MAX)
1936 if (!cc->fast_start_pfn)
1937 cc->fast_start_pfn = pfn;
1939 cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
1942 static inline unsigned long
1943 reinit_migrate_pfn(struct compact_control *cc)
1945 if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
1946 return cc->migrate_pfn;
1948 cc->migrate_pfn = cc->fast_start_pfn;
1949 cc->fast_start_pfn = ULONG_MAX;
1951 return cc->migrate_pfn;
1955 * Briefly search the free lists for a migration source that already has
1956 * some free pages to reduce the number of pages that need migration
1957 * before a pageblock is free.
1959 static unsigned long fast_find_migrateblock(struct compact_control *cc)
1961 unsigned int limit = freelist_scan_limit(cc);
1962 unsigned int nr_scanned = 0;
1963 unsigned long distance;
1964 unsigned long pfn = cc->migrate_pfn;
1965 unsigned long high_pfn;
1967 bool found_block = false;
1969 /* Skip hints are relied on to avoid repeats on the fast search */
1970 if (cc->ignore_skip_hint)
1974 * If the pageblock should be finished then do not select a different
1977 if (cc->finish_pageblock)
1981 * If the migrate_pfn is not at the start of a zone or the start
1982 * of a pageblock then assume this is a continuation of a previous
1983 * scan restarted due to COMPACT_CLUSTER_MAX.
1985 if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
1989 * For smaller orders, just linearly scan as the number of pages
1990 * to migrate should be relatively small and does not necessarily
1991 * justify freeing up a large block for a small allocation.
1993 if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
1997 * Only allow kcompactd and direct requests for movable pages to
1998 * quickly clear out a MOVABLE pageblock for allocation. This
1999 * reduces the risk that a large movable pageblock is freed for
2000 * an unmovable/reclaimable small allocation.
2002 if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
2006 * When starting the migration scanner, pick any pageblock within the
2007 * first half of the search space. Otherwise try and pick a pageblock
2008 * within the first eighth to reduce the chances that a migration
2009 * target later becomes a source.
2011 distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
2012 if (cc->migrate_pfn != cc->zone->zone_start_pfn)
2014 high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
2016 for (order = cc->order - 1;
2017 order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit;
2019 struct free_area *area = &cc->zone->free_area[order];
2020 struct list_head *freelist;
2021 unsigned long flags;
2022 struct page *freepage;
2027 spin_lock_irqsave(&cc->zone->lock, flags);
2028 freelist = &area->free_list[MIGRATE_MOVABLE];
2029 list_for_each_entry(freepage, freelist, buddy_list) {
2030 unsigned long free_pfn;
2032 if (nr_scanned++ >= limit) {
2033 move_freelist_tail(freelist, freepage);
2037 free_pfn = page_to_pfn(freepage);
2038 if (free_pfn < high_pfn) {
2040 * Avoid if skipped recently. Ideally it would
2041 * move to the tail but even safe iteration of
2042 * the list assumes an entry is deleted, not
2045 if (get_pageblock_skip(freepage))
2048 /* Reorder to so a future search skips recent pages */
2049 move_freelist_tail(freelist, freepage);
2051 update_fast_start_pfn(cc, free_pfn);
2052 pfn = pageblock_start_pfn(free_pfn);
2053 if (pfn < cc->zone->zone_start_pfn)
2054 pfn = cc->zone->zone_start_pfn;
2055 cc->fast_search_fail = 0;
2060 spin_unlock_irqrestore(&cc->zone->lock, flags);
2063 cc->total_migrate_scanned += nr_scanned;
2066 * If fast scanning failed then use a cached entry for a page block
2067 * that had free pages as the basis for starting a linear scan.
2070 cc->fast_search_fail++;
2071 pfn = reinit_migrate_pfn(cc);
2077 * Isolate all pages that can be migrated from the first suitable block,
2078 * starting at the block pointed to by the migrate scanner pfn within
2081 static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
2083 unsigned long block_start_pfn;
2084 unsigned long block_end_pfn;
2085 unsigned long low_pfn;
2087 const isolate_mode_t isolate_mode =
2088 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
2089 (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
2090 bool fast_find_block;
2093 * Start at where we last stopped, or beginning of the zone as
2094 * initialized by compact_zone(). The first failure will use
2095 * the lowest PFN as the starting point for linear scanning.
2097 low_pfn = fast_find_migrateblock(cc);
2098 block_start_pfn = pageblock_start_pfn(low_pfn);
2099 if (block_start_pfn < cc->zone->zone_start_pfn)
2100 block_start_pfn = cc->zone->zone_start_pfn;
2103 * fast_find_migrateblock() has already ensured the pageblock is not
2104 * set with a skipped flag, so to avoid the isolation_suitable check
2105 * below again, check whether the fast search was successful.
2107 fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
2109 /* Only scan within a pageblock boundary */
2110 block_end_pfn = pageblock_end_pfn(low_pfn);
2113 * Iterate over whole pageblocks until we find the first suitable.
2114 * Do not cross the free scanner.
2116 for (; block_end_pfn <= cc->free_pfn;
2117 fast_find_block = false,
2118 cc->migrate_pfn = low_pfn = block_end_pfn,
2119 block_start_pfn = block_end_pfn,
2120 block_end_pfn += pageblock_nr_pages) {
2123 * This can potentially iterate a massively long zone with
2124 * many pageblocks unsuitable, so periodically check if we
2127 if (!(low_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages)))
2130 page = pageblock_pfn_to_page(block_start_pfn,
2131 block_end_pfn, cc->zone);
2133 unsigned long next_pfn;
2135 next_pfn = skip_offline_sections(block_start_pfn);
2137 block_end_pfn = min(next_pfn, cc->free_pfn);
2142 * If isolation recently failed, do not retry. Only check the
2143 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
2144 * to be visited multiple times. Assume skip was checked
2145 * before making it "skip" so other compaction instances do
2146 * not scan the same block.
2148 if ((pageblock_aligned(low_pfn) ||
2149 low_pfn == cc->zone->zone_start_pfn) &&
2150 !fast_find_block && !isolation_suitable(cc, page))
2154 * For async direct compaction, only scan the pageblocks of the
2155 * same migratetype without huge pages. Async direct compaction
2156 * is optimistic to see if the minimum amount of work satisfies
2157 * the allocation. The cached PFN is updated as it's possible
2158 * that all remaining blocks between source and target are
2159 * unsuitable and the compaction scanners fail to meet.
2161 if (!suitable_migration_source(cc, page)) {
2162 update_cached_migrate(cc, block_end_pfn);
2166 /* Perform the isolation */
2167 if (isolate_migratepages_block(cc, low_pfn, block_end_pfn,
2169 return ISOLATE_ABORT;
2172 * Either we isolated something and proceed with migration. Or
2173 * we failed and compact_zone should decide if we should
2179 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
2183 * Determine whether kswapd is (or recently was!) running on this node.
2185 * pgdat_kswapd_lock() pins pgdat->kswapd, so a concurrent kswapd_stop() can't
2188 static bool kswapd_is_running(pg_data_t *pgdat)
2192 pgdat_kswapd_lock(pgdat);
2193 running = pgdat->kswapd && task_is_running(pgdat->kswapd);
2194 pgdat_kswapd_unlock(pgdat);
2200 * A zone's fragmentation score is the external fragmentation wrt to the
2201 * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100].
2203 static unsigned int fragmentation_score_zone(struct zone *zone)
2205 return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
2209 * A weighted zone's fragmentation score is the external fragmentation
2210 * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It
2211 * returns a value in the range [0, 100].
2213 * The scaling factor ensures that proactive compaction focuses on larger
2214 * zones like ZONE_NORMAL, rather than smaller, specialized zones like
2215 * ZONE_DMA32. For smaller zones, the score value remains close to zero,
2216 * and thus never exceeds the high threshold for proactive compaction.
2218 static unsigned int fragmentation_score_zone_weighted(struct zone *zone)
2220 unsigned long score;
2222 score = zone->present_pages * fragmentation_score_zone(zone);
2223 return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
2227 * The per-node proactive (background) compaction process is started by its
2228 * corresponding kcompactd thread when the node's fragmentation score
2229 * exceeds the high threshold. The compaction process remains active till
2230 * the node's score falls below the low threshold, or one of the back-off
2231 * conditions is met.
2233 static unsigned int fragmentation_score_node(pg_data_t *pgdat)
2235 unsigned int score = 0;
2238 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2241 zone = &pgdat->node_zones[zoneid];
2242 if (!populated_zone(zone))
2244 score += fragmentation_score_zone_weighted(zone);
2250 static unsigned int fragmentation_score_wmark(bool low)
2252 unsigned int wmark_low, leeway;
2254 wmark_low = 100U - sysctl_compaction_proactiveness;
2255 leeway = min(10U, wmark_low / 2);
2256 return low ? wmark_low : min(wmark_low + leeway, 100U);
2259 static bool should_proactive_compact_node(pg_data_t *pgdat)
2263 if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
2266 wmark_high = fragmentation_score_wmark(false);
2267 return fragmentation_score_node(pgdat) > wmark_high;
2270 static enum compact_result __compact_finished(struct compact_control *cc)
2273 const int migratetype = cc->migratetype;
2276 /* Compaction run completes if the migrate and free scanner meet */
2277 if (compact_scanners_met(cc)) {
2278 /* Let the next compaction start anew. */
2279 reset_cached_positions(cc->zone);
2282 * Mark that the PG_migrate_skip information should be cleared
2283 * by kswapd when it goes to sleep. kcompactd does not set the
2284 * flag itself as the decision to be clear should be directly
2285 * based on an allocation request.
2287 if (cc->direct_compaction)
2288 cc->zone->compact_blockskip_flush = true;
2291 return COMPACT_COMPLETE;
2293 return COMPACT_PARTIAL_SKIPPED;
2296 if (cc->proactive_compaction) {
2297 int score, wmark_low;
2300 pgdat = cc->zone->zone_pgdat;
2301 if (kswapd_is_running(pgdat))
2302 return COMPACT_PARTIAL_SKIPPED;
2304 score = fragmentation_score_zone(cc->zone);
2305 wmark_low = fragmentation_score_wmark(true);
2307 if (score > wmark_low)
2308 ret = COMPACT_CONTINUE;
2310 ret = COMPACT_SUCCESS;
2315 if (is_via_compact_memory(cc->order))
2316 return COMPACT_CONTINUE;
2319 * Always finish scanning a pageblock to reduce the possibility of
2320 * fallbacks in the future. This is particularly important when
2321 * migration source is unmovable/reclaimable but it's not worth
2324 if (!pageblock_aligned(cc->migrate_pfn))
2325 return COMPACT_CONTINUE;
2328 * When defrag_mode is enabled, make kcompactd target
2329 * watermarks in whole pageblocks. Because they can be stolen
2330 * without polluting, no further fallback checks are needed.
2332 if (defrag_mode && !cc->direct_compaction) {
2333 if (__zone_watermark_ok(cc->zone, cc->order,
2334 high_wmark_pages(cc->zone),
2335 cc->highest_zoneidx, cc->alloc_flags,
2336 zone_page_state(cc->zone,
2337 NR_FREE_PAGES_BLOCKS)))
2338 return COMPACT_SUCCESS;
2340 return COMPACT_CONTINUE;
2343 /* Direct compactor: Is a suitable page free? */
2344 ret = COMPACT_NO_SUITABLE_PAGE;
2345 for (order = cc->order; order < NR_PAGE_ORDERS; order++) {
2346 struct free_area *area = &cc->zone->free_area[order];
2348 /* Job done if page is free of the right migratetype */
2349 if (!free_area_empty(area, migratetype))
2350 return COMPACT_SUCCESS;
2353 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
2354 if (migratetype == MIGRATE_MOVABLE &&
2355 !free_area_empty(area, MIGRATE_CMA))
2356 return COMPACT_SUCCESS;
2359 * Job done if allocation would steal freepages from
2360 * other migratetype buddy lists.
2362 if (find_suitable_fallback(area, order, migratetype, true) >= 0)
2364 * Movable pages are OK in any pageblock. If we are
2365 * stealing for a non-movable allocation, make sure
2366 * we finish compacting the current pageblock first
2367 * (which is assured by the above migrate_pfn align
2368 * check) so it is as free as possible and we won't
2369 * have to steal another one soon.
2371 return COMPACT_SUCCESS;
2375 if (cc->contended || fatal_signal_pending(current))
2376 ret = COMPACT_CONTENDED;
2381 static enum compact_result compact_finished(struct compact_control *cc)
2385 ret = __compact_finished(cc);
2386 trace_mm_compaction_finished(cc->zone, cc->order, ret);
2387 if (ret == COMPACT_NO_SUITABLE_PAGE)
2388 ret = COMPACT_CONTINUE;
2393 static bool __compaction_suitable(struct zone *zone, int order,
2394 unsigned long watermark, int highest_zoneidx,
2395 unsigned long free_pages)
2398 * Watermarks for order-0 must be met for compaction to be able to
2399 * isolate free pages for migration targets. This means that the
2400 * watermark have to match, or be more pessimistic than the check in
2401 * __isolate_free_page().
2403 * For costly orders, we require a higher watermark for compaction to
2404 * proceed to increase its chances.
2406 * We use the direct compactor's highest_zoneidx to skip over zones
2407 * where lowmem reserves would prevent allocation even if compaction
2410 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2411 * suitable migration targets.
2413 watermark += compact_gap(order);
2414 if (order > PAGE_ALLOC_COSTLY_ORDER)
2415 watermark += low_wmark_pages(zone) - min_wmark_pages(zone);
2416 return __zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
2417 ALLOC_CMA, free_pages);
2421 * compaction_suitable: Is this suitable to run compaction on this zone now?
2423 bool compaction_suitable(struct zone *zone, int order, unsigned long watermark,
2424 int highest_zoneidx)
2426 enum compact_result compact_result;
2429 suitable = __compaction_suitable(zone, order, watermark, highest_zoneidx,
2430 zone_page_state(zone, NR_FREE_PAGES));
2432 * fragmentation index determines if allocation failures are due to
2433 * low memory or external fragmentation
2435 * index of -1000 would imply allocations might succeed depending on
2436 * watermarks, but we already failed the high-order watermark check
2437 * index towards 0 implies failure is due to lack of memory
2438 * index towards 1000 implies failure is due to fragmentation
2440 * Only compact if a failure would be due to fragmentation. Also
2441 * ignore fragindex for non-costly orders where the alternative to
2442 * a successful reclaim/compaction is OOM. Fragindex and the
2443 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
2444 * excessive compaction for costly orders, but it should not be at the
2445 * expense of system stability.
2448 compact_result = COMPACT_CONTINUE;
2449 if (order > PAGE_ALLOC_COSTLY_ORDER) {
2450 int fragindex = fragmentation_index(zone, order);
2452 if (fragindex >= 0 &&
2453 fragindex <= sysctl_extfrag_threshold) {
2455 compact_result = COMPACT_NOT_SUITABLE_ZONE;
2459 compact_result = COMPACT_SKIPPED;
2462 trace_mm_compaction_suitable(zone, order, compact_result);
2467 /* Used by direct reclaimers */
2468 bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
2475 * Make sure at least one zone would pass __compaction_suitable if we continue
2476 * retrying the reclaim.
2478 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2479 ac->highest_zoneidx, ac->nodemask) {
2480 unsigned long available;
2483 * Do not consider all the reclaimable memory because we do not
2484 * want to trash just for a single high order allocation which
2485 * is even not guaranteed to appear even if __compaction_suitable
2486 * is happy about the watermark check.
2488 available = zone_reclaimable_pages(zone) / order;
2489 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
2490 if (__compaction_suitable(zone, order, min_wmark_pages(zone),
2491 ac->highest_zoneidx, available))
2499 * Should we do compaction for target allocation order.
2500 * Return COMPACT_SUCCESS if allocation for target order can be already
2502 * Return COMPACT_SKIPPED if compaction for target order is likely to fail
2503 * Return COMPACT_CONTINUE if compaction for target order should be ran
2505 static enum compact_result
2506 compaction_suit_allocation_order(struct zone *zone, unsigned int order,
2507 int highest_zoneidx, unsigned int alloc_flags,
2508 bool async, bool kcompactd)
2510 unsigned long free_pages;
2511 unsigned long watermark;
2513 if (kcompactd && defrag_mode)
2514 free_pages = zone_page_state(zone, NR_FREE_PAGES_BLOCKS);
2516 free_pages = zone_page_state(zone, NR_FREE_PAGES);
2518 watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
2519 if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx,
2520 alloc_flags, free_pages))
2521 return COMPACT_SUCCESS;
2524 * For unmovable allocations (without ALLOC_CMA), check if there is enough
2525 * free memory in the non-CMA pageblocks. Otherwise compaction could form
2526 * the high-order page in CMA pageblocks, which would not help the
2527 * allocation to succeed. However, limit the check to costly order async
2528 * compaction (such as opportunistic THP attempts) because there is the
2529 * possibility that compaction would migrate pages from non-CMA to CMA
2532 if (order > PAGE_ALLOC_COSTLY_ORDER && async &&
2533 !(alloc_flags & ALLOC_CMA)) {
2534 if (!__zone_watermark_ok(zone, 0, watermark + compact_gap(order),
2536 zone_page_state(zone, NR_FREE_PAGES)))
2537 return COMPACT_SKIPPED;
2540 if (!compaction_suitable(zone, order, watermark, highest_zoneidx))
2541 return COMPACT_SKIPPED;
2543 return COMPACT_CONTINUE;
2546 static enum compact_result
2547 compact_zone(struct compact_control *cc, struct capture_control *capc)
2549 enum compact_result ret;
2550 unsigned long start_pfn = cc->zone->zone_start_pfn;
2551 unsigned long end_pfn = zone_end_pfn(cc->zone);
2552 unsigned long last_migrated_pfn;
2553 const bool sync = cc->mode != MIGRATE_ASYNC;
2555 unsigned int nr_succeeded = 0, nr_migratepages;
2559 * These counters track activities during zone compaction. Initialize
2560 * them before compacting a new zone.
2562 cc->total_migrate_scanned = 0;
2563 cc->total_free_scanned = 0;
2564 cc->nr_migratepages = 0;
2565 cc->nr_freepages = 0;
2566 for (order = 0; order < NR_PAGE_ORDERS; order++)
2567 INIT_LIST_HEAD(&cc->freepages[order]);
2568 INIT_LIST_HEAD(&cc->migratepages);
2570 cc->migratetype = gfp_migratetype(cc->gfp_mask);
2572 if (!is_via_compact_memory(cc->order)) {
2573 ret = compaction_suit_allocation_order(cc->zone, cc->order,
2574 cc->highest_zoneidx,
2576 cc->mode == MIGRATE_ASYNC,
2577 !cc->direct_compaction);
2578 if (ret != COMPACT_CONTINUE)
2583 * Clear pageblock skip if there were failures recently and compaction
2584 * is about to be retried after being deferred.
2586 if (compaction_restarting(cc->zone, cc->order))
2587 __reset_isolation_suitable(cc->zone);
2590 * Setup to move all movable pages to the end of the zone. Used cached
2591 * information on where the scanners should start (unless we explicitly
2592 * want to compact the whole zone), but check that it is initialised
2593 * by ensuring the values are within zone boundaries.
2595 cc->fast_start_pfn = 0;
2596 if (cc->whole_zone) {
2597 cc->migrate_pfn = start_pfn;
2598 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2600 cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
2601 cc->free_pfn = cc->zone->compact_cached_free_pfn;
2602 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
2603 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2604 cc->zone->compact_cached_free_pfn = cc->free_pfn;
2606 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2607 cc->migrate_pfn = start_pfn;
2608 cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
2609 cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2612 if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
2613 cc->whole_zone = true;
2616 last_migrated_pfn = 0;
2619 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
2620 * the basis that some migrations will fail in ASYNC mode. However,
2621 * if the cached PFNs match and pageblocks are skipped due to having
2622 * no isolation candidates, then the sync state does not matter.
2623 * Until a pageblock with isolation candidates is found, keep the
2624 * cached PFNs in sync to avoid revisiting the same blocks.
2626 update_cached = !sync &&
2627 cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
2629 trace_mm_compaction_begin(cc, start_pfn, end_pfn, sync);
2631 /* lru_add_drain_all could be expensive with involving other CPUs */
2634 while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
2636 unsigned long iteration_start_pfn = cc->migrate_pfn;
2639 * Avoid multiple rescans of the same pageblock which can
2640 * happen if a page cannot be isolated (dirty/writeback in
2641 * async mode) or if the migrated pages are being allocated
2642 * before the pageblock is cleared. The first rescan will
2643 * capture the entire pageblock for migration. If it fails,
2644 * it'll be marked skip and scanning will proceed as normal.
2646 cc->finish_pageblock = false;
2647 if (pageblock_start_pfn(last_migrated_pfn) ==
2648 pageblock_start_pfn(iteration_start_pfn)) {
2649 cc->finish_pageblock = true;
2653 switch (isolate_migratepages(cc)) {
2655 ret = COMPACT_CONTENDED;
2656 putback_movable_pages(&cc->migratepages);
2657 cc->nr_migratepages = 0;
2660 if (update_cached) {
2661 cc->zone->compact_cached_migrate_pfn[1] =
2662 cc->zone->compact_cached_migrate_pfn[0];
2666 * We haven't isolated and migrated anything, but
2667 * there might still be unflushed migrations from
2668 * previous cc->order aligned block.
2671 case ISOLATE_SUCCESS:
2672 update_cached = false;
2673 last_migrated_pfn = max(cc->zone->zone_start_pfn,
2674 pageblock_start_pfn(cc->migrate_pfn - 1));
2678 * Record the number of pages to migrate since the
2679 * compaction_alloc/free() will update cc->nr_migratepages
2682 nr_migratepages = cc->nr_migratepages;
2683 err = migrate_pages(&cc->migratepages, compaction_alloc,
2684 compaction_free, (unsigned long)cc, cc->mode,
2685 MR_COMPACTION, &nr_succeeded);
2687 trace_mm_compaction_migratepages(nr_migratepages, nr_succeeded);
2689 /* All pages were either migrated or will be released */
2690 cc->nr_migratepages = 0;
2692 putback_movable_pages(&cc->migratepages);
2694 * migrate_pages() may return -ENOMEM when scanners meet
2695 * and we want compact_finished() to detect it
2697 if (err == -ENOMEM && !compact_scanners_met(cc)) {
2698 ret = COMPACT_CONTENDED;
2702 * If an ASYNC or SYNC_LIGHT fails to migrate a page
2703 * within the pageblock_order-aligned block and
2704 * fast_find_migrateblock may be used then scan the
2705 * remainder of the pageblock. This will mark the
2706 * pageblock "skip" to avoid rescanning in the near
2707 * future. This will isolate more pages than necessary
2708 * for the request but avoid loops due to
2709 * fast_find_migrateblock revisiting blocks that were
2710 * recently partially scanned.
2712 if (!pageblock_aligned(cc->migrate_pfn) &&
2713 !cc->ignore_skip_hint && !cc->finish_pageblock &&
2714 (cc->mode < MIGRATE_SYNC)) {
2715 cc->finish_pageblock = true;
2718 * Draining pcplists does not help THP if
2719 * any page failed to migrate. Even after
2720 * drain, the pageblock will not be free.
2722 if (cc->order == COMPACTION_HPAGE_ORDER)
2723 last_migrated_pfn = 0;
2729 /* Stop if a page has been captured */
2730 if (capc && capc->page) {
2731 ret = COMPACT_SUCCESS;
2737 * Has the migration scanner moved away from the previous
2738 * cc->order aligned block where we migrated from? If yes,
2739 * flush the pages that were freed, so that they can merge and
2740 * compact_finished() can detect immediately if allocation
2743 if (cc->order > 0 && last_migrated_pfn) {
2744 unsigned long current_block_start =
2745 block_start_pfn(cc->migrate_pfn, cc->order);
2747 if (last_migrated_pfn < current_block_start) {
2748 lru_add_drain_cpu_zone(cc->zone);
2749 /* No more flushing until we migrate again */
2750 last_migrated_pfn = 0;
2757 * Release free pages and update where the free scanner should restart,
2758 * so we don't leave any returned pages behind in the next attempt.
2760 if (cc->nr_freepages > 0) {
2761 unsigned long free_pfn = release_free_list(cc->freepages);
2763 cc->nr_freepages = 0;
2764 VM_BUG_ON(free_pfn == 0);
2765 /* The cached pfn is always the first in a pageblock */
2766 free_pfn = pageblock_start_pfn(free_pfn);
2768 * Only go back, not forward. The cached pfn might have been
2769 * already reset to zone end in compact_finished()
2771 if (free_pfn > cc->zone->compact_cached_free_pfn)
2772 cc->zone->compact_cached_free_pfn = free_pfn;
2775 count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
2776 count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
2778 trace_mm_compaction_end(cc, start_pfn, end_pfn, sync, ret);
2780 VM_BUG_ON(!list_empty(&cc->migratepages));
2785 static enum compact_result compact_zone_order(struct zone *zone, int order,
2786 gfp_t gfp_mask, enum compact_priority prio,
2787 unsigned int alloc_flags, int highest_zoneidx,
2788 struct page **capture)
2790 enum compact_result ret;
2791 struct compact_control cc = {
2793 .search_order = order,
2794 .gfp_mask = gfp_mask,
2796 .mode = (prio == COMPACT_PRIO_ASYNC) ?
2797 MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
2798 .alloc_flags = alloc_flags,
2799 .highest_zoneidx = highest_zoneidx,
2800 .direct_compaction = true,
2801 .whole_zone = (prio == MIN_COMPACT_PRIORITY),
2802 .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
2803 .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
2805 struct capture_control capc = {
2811 * Make sure the structs are really initialized before we expose the
2812 * capture control, in case we are interrupted and the interrupt handler
2816 WRITE_ONCE(current->capture_control, &capc);
2818 ret = compact_zone(&cc, &capc);
2821 * Make sure we hide capture control first before we read the captured
2822 * page pointer, otherwise an interrupt could free and capture a page
2823 * and we would leak it.
2825 WRITE_ONCE(current->capture_control, NULL);
2826 *capture = READ_ONCE(capc.page);
2828 * Technically, it is also possible that compaction is skipped but
2829 * the page is still captured out of luck(IRQ came and freed the page).
2830 * Returning COMPACT_SUCCESS in such cases helps in properly accounting
2831 * the COMPACT[STALL|FAIL] when compaction is skipped.
2834 ret = COMPACT_SUCCESS;
2840 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
2841 * @gfp_mask: The GFP mask of the current allocation
2842 * @order: The order of the current allocation
2843 * @alloc_flags: The allocation flags of the current allocation
2844 * @ac: The context of current allocation
2845 * @prio: Determines how hard direct compaction should try to succeed
2846 * @capture: Pointer to free page created by compaction will be stored here
2848 * This is the main entry point for direct page compaction.
2850 enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
2851 unsigned int alloc_flags, const struct alloc_context *ac,
2852 enum compact_priority prio, struct page **capture)
2856 enum compact_result rc = COMPACT_SKIPPED;
2858 if (!gfp_compaction_allowed(gfp_mask))
2859 return COMPACT_SKIPPED;
2861 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
2863 /* Compact each zone in the list */
2864 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2865 ac->highest_zoneidx, ac->nodemask) {
2866 enum compact_result status;
2868 if (cpusets_enabled() &&
2869 (alloc_flags & ALLOC_CPUSET) &&
2870 !__cpuset_zone_allowed(zone, gfp_mask))
2873 if (prio > MIN_COMPACT_PRIORITY
2874 && compaction_deferred(zone, order)) {
2875 rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
2879 status = compact_zone_order(zone, order, gfp_mask, prio,
2880 alloc_flags, ac->highest_zoneidx, capture);
2881 rc = max(status, rc);
2883 /* The allocation should succeed, stop compacting */
2884 if (status == COMPACT_SUCCESS) {
2886 * We think the allocation will succeed in this zone,
2887 * but it is not certain, hence the false. The caller
2888 * will repeat this with true if allocation indeed
2889 * succeeds in this zone.
2891 compaction_defer_reset(zone, order, false);
2896 if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
2897 status == COMPACT_PARTIAL_SKIPPED))
2899 * We think that allocation won't succeed in this zone
2900 * so we defer compaction there. If it ends up
2901 * succeeding after all, it will be reset.
2903 defer_compaction(zone, order);
2906 * We might have stopped compacting due to need_resched() in
2907 * async compaction, or due to a fatal signal detected. In that
2908 * case do not try further zones
2910 if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2911 || fatal_signal_pending(current))
2919 * compact_node() - compact all zones within a node
2920 * @pgdat: The node page data
2921 * @proactive: Whether the compaction is proactive
2923 * For proactive compaction, compact till each zone's fragmentation score
2924 * reaches within proactive compaction thresholds (as determined by the
2925 * proactiveness tunable), it is possible that the function returns before
2926 * reaching score targets due to various back-off conditions, such as,
2927 * contention on per-node or per-zone locks.
2929 static int compact_node(pg_data_t *pgdat, bool proactive)
2933 struct compact_control cc = {
2935 .mode = proactive ? MIGRATE_SYNC_LIGHT : MIGRATE_SYNC,
2936 .ignore_skip_hint = true,
2938 .gfp_mask = GFP_KERNEL,
2939 .proactive_compaction = proactive,
2942 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2943 zone = &pgdat->node_zones[zoneid];
2944 if (!populated_zone(zone))
2947 if (fatal_signal_pending(current))
2952 compact_zone(&cc, NULL);
2955 count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
2956 cc.total_migrate_scanned);
2957 count_compact_events(KCOMPACTD_FREE_SCANNED,
2958 cc.total_free_scanned);
2965 /* Compact all zones of all nodes in the system */
2966 static int compact_nodes(void)
2970 /* Flush pending updates to the LRU lists */
2971 lru_add_drain_all();
2973 for_each_online_node(nid) {
2974 ret = compact_node(NODE_DATA(nid), false);
2982 static int compaction_proactiveness_sysctl_handler(const struct ctl_table *table, int write,
2983 void *buffer, size_t *length, loff_t *ppos)
2987 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
2991 if (write && sysctl_compaction_proactiveness) {
2992 for_each_online_node(nid) {
2993 pg_data_t *pgdat = NODE_DATA(nid);
2995 if (pgdat->proactive_compact_trigger)
2998 pgdat->proactive_compact_trigger = true;
2999 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, -1,
3000 pgdat->nr_zones - 1);
3001 wake_up_interruptible(&pgdat->kcompactd_wait);
3009 * This is the entry point for compacting all nodes via
3010 * /proc/sys/vm/compact_memory
3012 static int sysctl_compaction_handler(const struct ctl_table *table, int write,
3013 void *buffer, size_t *length, loff_t *ppos)
3017 ret = proc_dointvec(table, write, buffer, length, ppos);
3021 if (sysctl_compact_memory != 1)
3025 ret = compact_nodes();
3030 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
3031 static ssize_t compact_store(struct device *dev,
3032 struct device_attribute *attr,
3033 const char *buf, size_t count)
3037 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
3038 /* Flush pending updates to the LRU lists */
3039 lru_add_drain_all();
3041 compact_node(NODE_DATA(nid), false);
3046 static DEVICE_ATTR_WO(compact);
3048 int compaction_register_node(struct node *node)
3050 return device_create_file(&node->dev, &dev_attr_compact);
3053 void compaction_unregister_node(struct node *node)
3055 device_remove_file(&node->dev, &dev_attr_compact);
3057 #endif /* CONFIG_SYSFS && CONFIG_NUMA */
3059 static inline bool kcompactd_work_requested(pg_data_t *pgdat)
3061 return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
3062 pgdat->proactive_compact_trigger;
3065 static bool kcompactd_node_suitable(pg_data_t *pgdat)
3069 enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
3070 enum compact_result ret;
3071 unsigned int alloc_flags = defrag_mode ?
3072 ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN;
3074 for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
3075 zone = &pgdat->node_zones[zoneid];
3077 if (!populated_zone(zone))
3080 ret = compaction_suit_allocation_order(zone,
3081 pgdat->kcompactd_max_order,
3082 highest_zoneidx, alloc_flags,
3084 if (ret == COMPACT_CONTINUE)
3091 static void kcompactd_do_work(pg_data_t *pgdat)
3094 * With no special task, compact all zones so that a page of requested
3095 * order is allocatable.
3099 struct compact_control cc = {
3100 .order = pgdat->kcompactd_max_order,
3101 .search_order = pgdat->kcompactd_max_order,
3102 .highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
3103 .mode = MIGRATE_SYNC_LIGHT,
3104 .ignore_skip_hint = false,
3105 .gfp_mask = GFP_KERNEL,
3106 .alloc_flags = defrag_mode ? ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN,
3108 enum compact_result ret;
3110 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
3111 cc.highest_zoneidx);
3112 count_compact_event(KCOMPACTD_WAKE);
3114 for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
3117 zone = &pgdat->node_zones[zoneid];
3118 if (!populated_zone(zone))
3121 if (compaction_deferred(zone, cc.order))
3124 ret = compaction_suit_allocation_order(zone,
3125 cc.order, zoneid, cc.alloc_flags,
3127 if (ret != COMPACT_CONTINUE)
3130 if (kthread_should_stop())
3134 status = compact_zone(&cc, NULL);
3136 if (status == COMPACT_SUCCESS) {
3137 compaction_defer_reset(zone, cc.order, false);
3138 } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
3140 * Buddy pages may become stranded on pcps that could
3141 * otherwise coalesce on the zone's free area for
3142 * order >= cc.order. This is ratelimited by the
3143 * upcoming deferral.
3145 drain_all_pages(zone);
3148 * We use sync migration mode here, so we defer like
3149 * sync direct compaction does.
3151 defer_compaction(zone, cc.order);
3154 count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
3155 cc.total_migrate_scanned);
3156 count_compact_events(KCOMPACTD_FREE_SCANNED,
3157 cc.total_free_scanned);
3161 * Regardless of success, we are done until woken up next. But remember
3162 * the requested order/highest_zoneidx in case it was higher/tighter
3163 * than our current ones
3165 if (pgdat->kcompactd_max_order <= cc.order)
3166 pgdat->kcompactd_max_order = 0;
3167 if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
3168 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
3171 void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
3176 if (pgdat->kcompactd_max_order < order)
3177 pgdat->kcompactd_max_order = order;
3179 if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
3180 pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
3183 * Pairs with implicit barrier in wait_event_freezable()
3184 * such that wakeups are not missed.
3186 if (!wq_has_sleeper(&pgdat->kcompactd_wait))
3189 if (!kcompactd_node_suitable(pgdat))
3192 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
3194 wake_up_interruptible(&pgdat->kcompactd_wait);
3198 * The background compaction daemon, started as a kernel thread
3199 * from the init process.
3201 static int kcompactd(void *p)
3203 pg_data_t *pgdat = (pg_data_t *)p;
3204 long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC);
3205 long timeout = default_timeout;
3207 current->flags |= PF_KCOMPACTD;
3210 pgdat->kcompactd_max_order = 0;
3211 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
3213 while (!kthread_should_stop()) {
3214 unsigned long pflags;
3217 * Avoid the unnecessary wakeup for proactive compaction
3218 * when it is disabled.
3220 if (!sysctl_compaction_proactiveness)
3221 timeout = MAX_SCHEDULE_TIMEOUT;
3222 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
3223 if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
3224 kcompactd_work_requested(pgdat), timeout) &&
3225 !pgdat->proactive_compact_trigger) {
3227 psi_memstall_enter(&pflags);
3228 kcompactd_do_work(pgdat);
3229 psi_memstall_leave(&pflags);
3231 * Reset the timeout value. The defer timeout from
3232 * proactive compaction is lost here but that is fine
3233 * as the condition of the zone changing substantionally
3234 * then carrying on with the previous defer interval is
3237 timeout = default_timeout;
3242 * Start the proactive work with default timeout. Based
3243 * on the fragmentation score, this timeout is updated.
3245 timeout = default_timeout;
3246 if (should_proactive_compact_node(pgdat)) {
3247 unsigned int prev_score, score;
3249 prev_score = fragmentation_score_node(pgdat);
3250 compact_node(pgdat, true);
3251 score = fragmentation_score_node(pgdat);
3253 * Defer proactive compaction if the fragmentation
3254 * score did not go down i.e. no progress made.
3256 if (unlikely(score >= prev_score))
3258 default_timeout << COMPACT_MAX_DEFER_SHIFT;
3260 if (unlikely(pgdat->proactive_compact_trigger))
3261 pgdat->proactive_compact_trigger = false;
3264 current->flags &= ~PF_KCOMPACTD;
3270 * This kcompactd start function will be called by init and node-hot-add.
3271 * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
3273 void __meminit kcompactd_run(int nid)
3275 pg_data_t *pgdat = NODE_DATA(nid);
3277 if (pgdat->kcompactd)
3280 pgdat->kcompactd = kthread_create_on_node(kcompactd, pgdat, nid, "kcompactd%d", nid);
3281 if (IS_ERR(pgdat->kcompactd)) {
3282 pr_err("Failed to start kcompactd on node %d\n", nid);
3283 pgdat->kcompactd = NULL;
3285 wake_up_process(pgdat->kcompactd);
3290 * Called by memory hotplug when all memory in a node is offlined. Caller must
3291 * be holding mem_hotplug_begin/done().
3293 void __meminit kcompactd_stop(int nid)
3295 struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
3298 kthread_stop(kcompactd);
3299 NODE_DATA(nid)->kcompactd = NULL;
3303 static int proc_dointvec_minmax_warn_RT_change(const struct ctl_table *table,
3304 int write, void *buffer, size_t *lenp, loff_t *ppos)
3308 if (!IS_ENABLED(CONFIG_PREEMPT_RT) || !write)
3309 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
3311 old = *(int *)table->data;
3312 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
3315 if (old != *(int *)table->data)
3316 pr_warn_once("sysctl attribute %s changed by %s[%d]\n",
3317 table->procname, current->comm,
3318 task_pid_nr(current));
3322 static const struct ctl_table vm_compaction[] = {
3324 .procname = "compact_memory",
3325 .data = &sysctl_compact_memory,
3326 .maxlen = sizeof(int),
3328 .proc_handler = sysctl_compaction_handler,
3331 .procname = "compaction_proactiveness",
3332 .data = &sysctl_compaction_proactiveness,
3333 .maxlen = sizeof(sysctl_compaction_proactiveness),
3335 .proc_handler = compaction_proactiveness_sysctl_handler,
3336 .extra1 = SYSCTL_ZERO,
3337 .extra2 = SYSCTL_ONE_HUNDRED,
3340 .procname = "extfrag_threshold",
3341 .data = &sysctl_extfrag_threshold,
3342 .maxlen = sizeof(int),
3344 .proc_handler = proc_dointvec_minmax,
3345 .extra1 = SYSCTL_ZERO,
3346 .extra2 = SYSCTL_ONE_THOUSAND,
3349 .procname = "compact_unevictable_allowed",
3350 .data = &sysctl_compact_unevictable_allowed,
3351 .maxlen = sizeof(int),
3353 .proc_handler = proc_dointvec_minmax_warn_RT_change,
3354 .extra1 = SYSCTL_ZERO,
3355 .extra2 = SYSCTL_ONE,
3359 static int __init kcompactd_init(void)
3363 for_each_node_state(nid, N_MEMORY)
3365 register_sysctl_init("vm", vm_compaction);
3368 subsys_initcall(kcompactd_init)
3370 #endif /* CONFIG_COMPACTION */