1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2748446bbSMel Gorman /* 3748446bbSMel Gorman * linux/mm/compaction.c 4748446bbSMel Gorman * 5748446bbSMel Gorman * Memory compaction for the reduction of external fragmentation. Note that 6748446bbSMel Gorman * this heavily depends upon page migration to do all the real heavy 7748446bbSMel Gorman * lifting 8748446bbSMel Gorman * 9748446bbSMel Gorman * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> 10748446bbSMel Gorman */ 11698b1b30SVlastimil Babka #include <linux/cpu.h> 12748446bbSMel Gorman #include <linux/swap.h> 13748446bbSMel Gorman #include <linux/migrate.h> 14748446bbSMel Gorman #include <linux/compaction.h> 15748446bbSMel Gorman #include <linux/mm_inline.h> 16174cd4b1SIngo Molnar #include <linux/sched/signal.h> 17748446bbSMel Gorman #include <linux/backing-dev.h> 1876ab0f53SMel Gorman #include <linux/sysctl.h> 19ed4a6d7fSMel Gorman #include <linux/sysfs.h> 20194159fbSMinchan Kim #include <linux/page-isolation.h> 21b8c73fc2SAndrey Ryabinin #include <linux/kasan.h> 22698b1b30SVlastimil Babka #include <linux/kthread.h> 23698b1b30SVlastimil Babka #include <linux/freezer.h> 2483358eceSJoonsoo Kim #include <linux/page_owner.h> 25eb414681SJohannes Weiner #include <linux/psi.h> 26748446bbSMel Gorman #include "internal.h" 27748446bbSMel Gorman 28010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 2931ca72faSCharan Teja Kalla /* 3031ca72faSCharan Teja Kalla * Fragmentation score check interval for proactive compaction purposes. 3131ca72faSCharan Teja Kalla */ 3231ca72faSCharan Teja Kalla #define HPAGE_FRAG_CHECK_INTERVAL_MSEC (500) 3331ca72faSCharan Teja Kalla 34010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 35010fc29aSMinchan Kim { 36010fc29aSMinchan Kim count_vm_event(item); 37010fc29aSMinchan Kim } 38010fc29aSMinchan Kim 39010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 40010fc29aSMinchan Kim { 41010fc29aSMinchan Kim count_vm_events(item, delta); 42010fc29aSMinchan Kim } 43010fc29aSMinchan Kim #else 44010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 45010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 46010fc29aSMinchan Kim #endif 47010fc29aSMinchan Kim 48ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 49ff9543fdSMichal Nazarewicz 50b7aba698SMel Gorman #define CREATE_TRACE_POINTS 51b7aba698SMel Gorman #include <trace/events/compaction.h> 52b7aba698SMel Gorman 5306b6640aSVlastimil Babka #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) 5406b6640aSVlastimil Babka #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) 5506b6640aSVlastimil Babka #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) 5606b6640aSVlastimil Babka #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) 5706b6640aSVlastimil Babka 58facdaa91SNitin Gupta /* 59facdaa91SNitin Gupta * Page order with-respect-to which proactive compaction 60facdaa91SNitin Gupta * calculates external fragmentation, which is used as 61facdaa91SNitin Gupta * the "fragmentation score" of a node/zone. 62facdaa91SNitin Gupta */ 63facdaa91SNitin Gupta #if defined CONFIG_TRANSPARENT_HUGEPAGE 64facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER 6525788738SNitin Gupta #elif defined CONFIG_HUGETLBFS 66facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER 67facdaa91SNitin Gupta #else 68facdaa91SNitin Gupta #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) 69facdaa91SNitin Gupta #endif 70facdaa91SNitin Gupta 71748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 72748446bbSMel Gorman { 73748446bbSMel Gorman struct page *page, *next; 746bace090SVlastimil Babka unsigned long high_pfn = 0; 75748446bbSMel Gorman 76748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 776bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 78748446bbSMel Gorman list_del(&page->lru); 79748446bbSMel Gorman __free_page(page); 806bace090SVlastimil Babka if (pfn > high_pfn) 816bace090SVlastimil Babka high_pfn = pfn; 82748446bbSMel Gorman } 83748446bbSMel Gorman 846bace090SVlastimil Babka return high_pfn; 85748446bbSMel Gorman } 86748446bbSMel Gorman 874469ab98SMel Gorman static void split_map_pages(struct list_head *list) 88ff9543fdSMichal Nazarewicz { 8966c64223SJoonsoo Kim unsigned int i, order, nr_pages; 9066c64223SJoonsoo Kim struct page *page, *next; 9166c64223SJoonsoo Kim LIST_HEAD(tmp_list); 92ff9543fdSMichal Nazarewicz 9366c64223SJoonsoo Kim list_for_each_entry_safe(page, next, list, lru) { 9466c64223SJoonsoo Kim list_del(&page->lru); 9566c64223SJoonsoo Kim 9666c64223SJoonsoo Kim order = page_private(page); 9766c64223SJoonsoo Kim nr_pages = 1 << order; 9866c64223SJoonsoo Kim 9946f24fd8SJoonsoo Kim post_alloc_hook(page, order, __GFP_MOVABLE); 10066c64223SJoonsoo Kim if (order) 10166c64223SJoonsoo Kim split_page(page, order); 10266c64223SJoonsoo Kim 10366c64223SJoonsoo Kim for (i = 0; i < nr_pages; i++) { 10466c64223SJoonsoo Kim list_add(&page->lru, &tmp_list); 10566c64223SJoonsoo Kim page++; 106ff9543fdSMichal Nazarewicz } 107ff9543fdSMichal Nazarewicz } 108ff9543fdSMichal Nazarewicz 10966c64223SJoonsoo Kim list_splice(&tmp_list, list); 11066c64223SJoonsoo Kim } 11166c64223SJoonsoo Kim 112bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 11368f2736aSMatthew Wilcox (Oracle) bool PageMovable(struct page *page) 114bda807d4SMinchan Kim { 11568f2736aSMatthew Wilcox (Oracle) const struct movable_operations *mops; 116bda807d4SMinchan Kim 117bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 118bda807d4SMinchan Kim if (!__PageMovable(page)) 11968f2736aSMatthew Wilcox (Oracle) return false; 120bda807d4SMinchan Kim 12168f2736aSMatthew Wilcox (Oracle) mops = page_movable_ops(page); 12268f2736aSMatthew Wilcox (Oracle) if (mops) 12368f2736aSMatthew Wilcox (Oracle) return true; 124bda807d4SMinchan Kim 12568f2736aSMatthew Wilcox (Oracle) return false; 126bda807d4SMinchan Kim } 127bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable); 128bda807d4SMinchan Kim 12968f2736aSMatthew Wilcox (Oracle) void __SetPageMovable(struct page *page, const struct movable_operations *mops) 130bda807d4SMinchan Kim { 131bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 13268f2736aSMatthew Wilcox (Oracle) VM_BUG_ON_PAGE((unsigned long)mops & PAGE_MAPPING_MOVABLE, page); 13368f2736aSMatthew Wilcox (Oracle) page->mapping = (void *)((unsigned long)mops | PAGE_MAPPING_MOVABLE); 134bda807d4SMinchan Kim } 135bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable); 136bda807d4SMinchan Kim 137bda807d4SMinchan Kim void __ClearPageMovable(struct page *page) 138bda807d4SMinchan Kim { 139bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageMovable(page), page); 140bda807d4SMinchan Kim /* 14168f2736aSMatthew Wilcox (Oracle) * This page still has the type of a movable page, but it's 14268f2736aSMatthew Wilcox (Oracle) * actually not movable any more. 143bda807d4SMinchan Kim */ 14468f2736aSMatthew Wilcox (Oracle) page->mapping = (void *)PAGE_MAPPING_MOVABLE; 145bda807d4SMinchan Kim } 146bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable); 147bda807d4SMinchan Kim 14824e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 14924e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 15024e2716fSJoonsoo Kim 15124e2716fSJoonsoo Kim /* 15224e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 153860b3272SAlex Shi * allocation success. 1 << compact_defer_shift, compactions are skipped up 15424e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 15524e2716fSJoonsoo Kim */ 1562271b016SHui Su static void defer_compaction(struct zone *zone, int order) 15724e2716fSJoonsoo Kim { 15824e2716fSJoonsoo Kim zone->compact_considered = 0; 15924e2716fSJoonsoo Kim zone->compact_defer_shift++; 16024e2716fSJoonsoo Kim 16124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 16224e2716fSJoonsoo Kim zone->compact_order_failed = order; 16324e2716fSJoonsoo Kim 16424e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 16524e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 16624e2716fSJoonsoo Kim 16724e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 16824e2716fSJoonsoo Kim } 16924e2716fSJoonsoo Kim 17024e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 1712271b016SHui Su static bool compaction_deferred(struct zone *zone, int order) 17224e2716fSJoonsoo Kim { 17324e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 17424e2716fSJoonsoo Kim 17524e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 17624e2716fSJoonsoo Kim return false; 17724e2716fSJoonsoo Kim 17824e2716fSJoonsoo Kim /* Avoid possible overflow */ 17962b35fe0SMateusz Nosek if (++zone->compact_considered >= defer_limit) { 18024e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 18124e2716fSJoonsoo Kim return false; 18262b35fe0SMateusz Nosek } 18324e2716fSJoonsoo Kim 18424e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 18524e2716fSJoonsoo Kim 18624e2716fSJoonsoo Kim return true; 18724e2716fSJoonsoo Kim } 18824e2716fSJoonsoo Kim 18924e2716fSJoonsoo Kim /* 19024e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 19124e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 19224e2716fSJoonsoo Kim * expected to succeed. 19324e2716fSJoonsoo Kim */ 19424e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 19524e2716fSJoonsoo Kim bool alloc_success) 19624e2716fSJoonsoo Kim { 19724e2716fSJoonsoo Kim if (alloc_success) { 19824e2716fSJoonsoo Kim zone->compact_considered = 0; 19924e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 20024e2716fSJoonsoo Kim } 20124e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 20224e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 20324e2716fSJoonsoo Kim 20424e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 20524e2716fSJoonsoo Kim } 20624e2716fSJoonsoo Kim 20724e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 2082271b016SHui Su static bool compaction_restarting(struct zone *zone, int order) 20924e2716fSJoonsoo Kim { 21024e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 21124e2716fSJoonsoo Kim return false; 21224e2716fSJoonsoo Kim 21324e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 21424e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 21524e2716fSJoonsoo Kim } 21624e2716fSJoonsoo Kim 217bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 218bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 219bb13ffebSMel Gorman struct page *page) 220bb13ffebSMel Gorman { 221bb13ffebSMel Gorman if (cc->ignore_skip_hint) 222bb13ffebSMel Gorman return true; 223bb13ffebSMel Gorman 224bb13ffebSMel Gorman return !get_pageblock_skip(page); 225bb13ffebSMel Gorman } 226bb13ffebSMel Gorman 22702333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 22802333641SVlastimil Babka { 22902333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 23002333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 231623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 23206b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 23302333641SVlastimil Babka } 23402333641SVlastimil Babka 235bb13ffebSMel Gorman /* 2362271b016SHui Su * Compound pages of >= pageblock_order should consistently be skipped until 237b527cfe5SVlastimil Babka * released. It is always pointless to compact pages of such order (if they are 238b527cfe5SVlastimil Babka * migratable), and the pageblocks they occupy cannot contain any free pages. 23921dc7e02SDavid Rientjes */ 240b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page) 24121dc7e02SDavid Rientjes { 242b527cfe5SVlastimil Babka if (!PageCompound(page)) 24321dc7e02SDavid Rientjes return false; 244b527cfe5SVlastimil Babka 245b527cfe5SVlastimil Babka page = compound_head(page); 246b527cfe5SVlastimil Babka 247b527cfe5SVlastimil Babka if (compound_order(page) >= pageblock_order) 24821dc7e02SDavid Rientjes return true; 249b527cfe5SVlastimil Babka 250b527cfe5SVlastimil Babka return false; 25121dc7e02SDavid Rientjes } 25221dc7e02SDavid Rientjes 253e332f741SMel Gorman static bool 254e332f741SMel Gorman __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, 255e332f741SMel Gorman bool check_target) 256e332f741SMel Gorman { 257e332f741SMel Gorman struct page *page = pfn_to_online_page(pfn); 2586b0868c8SMel Gorman struct page *block_page; 259e332f741SMel Gorman struct page *end_page; 260e332f741SMel Gorman unsigned long block_pfn; 261e332f741SMel Gorman 262e332f741SMel Gorman if (!page) 263e332f741SMel Gorman return false; 264e332f741SMel Gorman if (zone != page_zone(page)) 265e332f741SMel Gorman return false; 266e332f741SMel Gorman if (pageblock_skip_persistent(page)) 267e332f741SMel Gorman return false; 268e332f741SMel Gorman 269e332f741SMel Gorman /* 270e332f741SMel Gorman * If skip is already cleared do no further checking once the 271e332f741SMel Gorman * restart points have been set. 272e332f741SMel Gorman */ 273e332f741SMel Gorman if (check_source && check_target && !get_pageblock_skip(page)) 274e332f741SMel Gorman return true; 275e332f741SMel Gorman 276e332f741SMel Gorman /* 277e332f741SMel Gorman * If clearing skip for the target scanner, do not select a 278e332f741SMel Gorman * non-movable pageblock as the starting point. 279e332f741SMel Gorman */ 280e332f741SMel Gorman if (!check_source && check_target && 281e332f741SMel Gorman get_pageblock_migratetype(page) != MIGRATE_MOVABLE) 282e332f741SMel Gorman return false; 283e332f741SMel Gorman 2846b0868c8SMel Gorman /* Ensure the start of the pageblock or zone is online and valid */ 2856b0868c8SMel Gorman block_pfn = pageblock_start_pfn(pfn); 286a2e9a5afSVlastimil Babka block_pfn = max(block_pfn, zone->zone_start_pfn); 287a2e9a5afSVlastimil Babka block_page = pfn_to_online_page(block_pfn); 2886b0868c8SMel Gorman if (block_page) { 2896b0868c8SMel Gorman page = block_page; 2906b0868c8SMel Gorman pfn = block_pfn; 2916b0868c8SMel Gorman } 2926b0868c8SMel Gorman 2936b0868c8SMel Gorman /* Ensure the end of the pageblock or zone is online and valid */ 294a2e9a5afSVlastimil Babka block_pfn = pageblock_end_pfn(pfn) - 1; 2956b0868c8SMel Gorman block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); 2966b0868c8SMel Gorman end_page = pfn_to_online_page(block_pfn); 2976b0868c8SMel Gorman if (!end_page) 2986b0868c8SMel Gorman return false; 2996b0868c8SMel Gorman 300e332f741SMel Gorman /* 301e332f741SMel Gorman * Only clear the hint if a sample indicates there is either a 302e332f741SMel Gorman * free page or an LRU page in the block. One or other condition 303e332f741SMel Gorman * is necessary for the block to be a migration source/target. 304e332f741SMel Gorman */ 305e332f741SMel Gorman do { 306e332f741SMel Gorman if (check_source && PageLRU(page)) { 307e332f741SMel Gorman clear_pageblock_skip(page); 308e332f741SMel Gorman return true; 309e332f741SMel Gorman } 310e332f741SMel Gorman 311e332f741SMel Gorman if (check_target && PageBuddy(page)) { 312e332f741SMel Gorman clear_pageblock_skip(page); 313e332f741SMel Gorman return true; 314e332f741SMel Gorman } 315e332f741SMel Gorman 316e332f741SMel Gorman page += (1 << PAGE_ALLOC_COSTLY_ORDER); 317a2e9a5afSVlastimil Babka } while (page <= end_page); 318e332f741SMel Gorman 319e332f741SMel Gorman return false; 320e332f741SMel Gorman } 321e332f741SMel Gorman 32221dc7e02SDavid Rientjes /* 323bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 324bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 325bb13ffebSMel Gorman * meet. 326bb13ffebSMel Gorman */ 32762997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 328bb13ffebSMel Gorman { 329e332f741SMel Gorman unsigned long migrate_pfn = zone->zone_start_pfn; 3306b0868c8SMel Gorman unsigned long free_pfn = zone_end_pfn(zone) - 1; 331e332f741SMel Gorman unsigned long reset_migrate = free_pfn; 332e332f741SMel Gorman unsigned long reset_free = migrate_pfn; 333e332f741SMel Gorman bool source_set = false; 334e332f741SMel Gorman bool free_set = false; 335e332f741SMel Gorman 336e332f741SMel Gorman if (!zone->compact_blockskip_flush) 337e332f741SMel Gorman return; 338bb13ffebSMel Gorman 33962997027SMel Gorman zone->compact_blockskip_flush = false; 340bb13ffebSMel Gorman 341e332f741SMel Gorman /* 342e332f741SMel Gorman * Walk the zone and update pageblock skip information. Source looks 343e332f741SMel Gorman * for PageLRU while target looks for PageBuddy. When the scanner 344e332f741SMel Gorman * is found, both PageBuddy and PageLRU are checked as the pageblock 345e332f741SMel Gorman * is suitable as both source and target. 346e332f741SMel Gorman */ 347e332f741SMel Gorman for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, 348e332f741SMel Gorman free_pfn -= pageblock_nr_pages) { 349bb13ffebSMel Gorman cond_resched(); 350bb13ffebSMel Gorman 351e332f741SMel Gorman /* Update the migrate PFN */ 352e332f741SMel Gorman if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && 353e332f741SMel Gorman migrate_pfn < reset_migrate) { 354e332f741SMel Gorman source_set = true; 355e332f741SMel Gorman reset_migrate = migrate_pfn; 356e332f741SMel Gorman zone->compact_init_migrate_pfn = reset_migrate; 357e332f741SMel Gorman zone->compact_cached_migrate_pfn[0] = reset_migrate; 358e332f741SMel Gorman zone->compact_cached_migrate_pfn[1] = reset_migrate; 359bb13ffebSMel Gorman } 36002333641SVlastimil Babka 361e332f741SMel Gorman /* Update the free PFN */ 362e332f741SMel Gorman if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && 363e332f741SMel Gorman free_pfn > reset_free) { 364e332f741SMel Gorman free_set = true; 365e332f741SMel Gorman reset_free = free_pfn; 366e332f741SMel Gorman zone->compact_init_free_pfn = reset_free; 367e332f741SMel Gorman zone->compact_cached_free_pfn = reset_free; 368e332f741SMel Gorman } 369e332f741SMel Gorman } 370e332f741SMel Gorman 371e332f741SMel Gorman /* Leave no distance if no suitable block was reset */ 372e332f741SMel Gorman if (reset_migrate >= reset_free) { 373e332f741SMel Gorman zone->compact_cached_migrate_pfn[0] = migrate_pfn; 374e332f741SMel Gorman zone->compact_cached_migrate_pfn[1] = migrate_pfn; 375e332f741SMel Gorman zone->compact_cached_free_pfn = free_pfn; 376e332f741SMel Gorman } 377bb13ffebSMel Gorman } 378bb13ffebSMel Gorman 37962997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 38062997027SMel Gorman { 38162997027SMel Gorman int zoneid; 38262997027SMel Gorman 38362997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 38462997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 38562997027SMel Gorman if (!populated_zone(zone)) 38662997027SMel Gorman continue; 38762997027SMel Gorman 38862997027SMel Gorman /* Only flush if a full compaction finished recently */ 38962997027SMel Gorman if (zone->compact_blockskip_flush) 39062997027SMel Gorman __reset_isolation_suitable(zone); 39162997027SMel Gorman } 39262997027SMel Gorman } 39362997027SMel Gorman 394bb13ffebSMel Gorman /* 395e380bebeSMel Gorman * Sets the pageblock skip bit if it was clear. Note that this is a hint as 396e380bebeSMel Gorman * locks are not required for read/writers. Returns true if it was already set. 397e380bebeSMel Gorman */ 398e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 399e380bebeSMel Gorman unsigned long pfn) 400e380bebeSMel Gorman { 401e380bebeSMel Gorman bool skip; 402e380bebeSMel Gorman 403e380bebeSMel Gorman /* Do no update if skip hint is being ignored */ 404e380bebeSMel Gorman if (cc->ignore_skip_hint) 405e380bebeSMel Gorman return false; 406e380bebeSMel Gorman 407e380bebeSMel Gorman if (!IS_ALIGNED(pfn, pageblock_nr_pages)) 408e380bebeSMel Gorman return false; 409e380bebeSMel Gorman 410e380bebeSMel Gorman skip = get_pageblock_skip(page); 411e380bebeSMel Gorman if (!skip && !cc->no_set_skip_hint) 412e380bebeSMel Gorman set_pageblock_skip(page); 413e380bebeSMel Gorman 414e380bebeSMel Gorman return skip; 415e380bebeSMel Gorman } 416e380bebeSMel Gorman 417e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 418e380bebeSMel Gorman { 419e380bebeSMel Gorman struct zone *zone = cc->zone; 420e380bebeSMel Gorman 421e380bebeSMel Gorman pfn = pageblock_end_pfn(pfn); 422e380bebeSMel Gorman 423e380bebeSMel Gorman /* Set for isolation rather than compaction */ 424e380bebeSMel Gorman if (cc->no_set_skip_hint) 425e380bebeSMel Gorman return; 426e380bebeSMel Gorman 427e380bebeSMel Gorman if (pfn > zone->compact_cached_migrate_pfn[0]) 428e380bebeSMel Gorman zone->compact_cached_migrate_pfn[0] = pfn; 429e380bebeSMel Gorman if (cc->mode != MIGRATE_ASYNC && 430e380bebeSMel Gorman pfn > zone->compact_cached_migrate_pfn[1]) 431e380bebeSMel Gorman zone->compact_cached_migrate_pfn[1] = pfn; 432e380bebeSMel Gorman } 433e380bebeSMel Gorman 434e380bebeSMel Gorman /* 435bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 43662997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 437bb13ffebSMel Gorman */ 438c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 439d097a6f6SMel Gorman struct page *page, unsigned long pfn) 440bb13ffebSMel Gorman { 441c89511abSMel Gorman struct zone *zone = cc->zone; 4426815bf3fSJoonsoo Kim 4432583d671SVlastimil Babka if (cc->no_set_skip_hint) 4446815bf3fSJoonsoo Kim return; 4456815bf3fSJoonsoo Kim 446bb13ffebSMel Gorman if (!page) 447bb13ffebSMel Gorman return; 448bb13ffebSMel Gorman 449bb13ffebSMel Gorman set_pageblock_skip(page); 450c89511abSMel Gorman 45135979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 45235979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 453c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 454c89511abSMel Gorman } 455bb13ffebSMel Gorman #else 456bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 457bb13ffebSMel Gorman struct page *page) 458bb13ffebSMel Gorman { 459bb13ffebSMel Gorman return true; 460bb13ffebSMel Gorman } 461bb13ffebSMel Gorman 462b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page) 46321dc7e02SDavid Rientjes { 46421dc7e02SDavid Rientjes return false; 46521dc7e02SDavid Rientjes } 46621dc7e02SDavid Rientjes 46721dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc, 468d097a6f6SMel Gorman struct page *page, unsigned long pfn) 469bb13ffebSMel Gorman { 470bb13ffebSMel Gorman } 471e380bebeSMel Gorman 472e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 473e380bebeSMel Gorman { 474e380bebeSMel Gorman } 475e380bebeSMel Gorman 476e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 477e380bebeSMel Gorman unsigned long pfn) 478e380bebeSMel Gorman { 479e380bebeSMel Gorman return false; 480e380bebeSMel Gorman } 481bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 482bb13ffebSMel Gorman 4831f9efdefSVlastimil Babka /* 4848b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 485cb2dcaf0SMel Gorman * very heavily contended. For async compaction, trylock and record if the 486cb2dcaf0SMel Gorman * lock is contended. The lock will still be acquired but compaction will 487cb2dcaf0SMel Gorman * abort when the current block is finished regardless of success rate. 488cb2dcaf0SMel Gorman * Sync compaction acquires the lock. 4898b44d279SVlastimil Babka * 490cb2dcaf0SMel Gorman * Always returns true which makes it easier to track lock state in callers. 4911f9efdefSVlastimil Babka */ 492cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, 4938b44d279SVlastimil Babka struct compact_control *cc) 49477337edeSJules Irenge __acquires(lock) 4958b44d279SVlastimil Babka { 496cb2dcaf0SMel Gorman /* Track if the lock is contended in async mode */ 497cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC && !cc->contended) { 498cb2dcaf0SMel Gorman if (spin_trylock_irqsave(lock, *flags)) 499cb2dcaf0SMel Gorman return true; 500cb2dcaf0SMel Gorman 501c3486f53SVlastimil Babka cc->contended = true; 5028b44d279SVlastimil Babka } 5031f9efdefSVlastimil Babka 504cb2dcaf0SMel Gorman spin_lock_irqsave(lock, *flags); 5058b44d279SVlastimil Babka return true; 5062a1402aaSMel Gorman } 5072a1402aaSMel Gorman 50885aa125fSMichal Nazarewicz /* 509c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 5108b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 5118b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 5128b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 513d56c1584SMiaohe Lin * need_resched() becoming true. If scheduling is needed, compaction schedules. 5148b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 5158b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 516c67fe375SMel Gorman * 517d56c1584SMiaohe Lin * Returns true if compaction should abort due to fatal signal pending. 518d56c1584SMiaohe Lin * Returns false when compaction can continue. 519c67fe375SMel Gorman */ 5208b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 5218b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 522c67fe375SMel Gorman { 5238b44d279SVlastimil Babka if (*locked) { 5248b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 5258b44d279SVlastimil Babka *locked = false; 526c67fe375SMel Gorman } 527c67fe375SMel Gorman 5288b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 529c3486f53SVlastimil Babka cc->contended = true; 5308b44d279SVlastimil Babka return true; 5318b44d279SVlastimil Babka } 5328b44d279SVlastimil Babka 533cf66f070SMel Gorman cond_resched(); 534be976572SVlastimil Babka 535be976572SVlastimil Babka return false; 536be976572SVlastimil Babka } 537be976572SVlastimil Babka 538c67fe375SMel Gorman /* 5399e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 5409e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 5419e4be470SJerome Marchand * (even though it may still end up isolating some pages). 54285aa125fSMichal Nazarewicz */ 543f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 544e14c720eSVlastimil Babka unsigned long *start_pfn, 54585aa125fSMichal Nazarewicz unsigned long end_pfn, 54685aa125fSMichal Nazarewicz struct list_head *freelist, 5474fca9730SMel Gorman unsigned int stride, 54885aa125fSMichal Nazarewicz bool strict) 549748446bbSMel Gorman { 550b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 551d097a6f6SMel Gorman struct page *cursor; 552b8b2d825SXiubo Li unsigned long flags = 0; 553f40d1e42SMel Gorman bool locked = false; 554e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 55566c64223SJoonsoo Kim unsigned int order; 556748446bbSMel Gorman 5574fca9730SMel Gorman /* Strict mode is for isolation, speed is secondary */ 5584fca9730SMel Gorman if (strict) 5594fca9730SMel Gorman stride = 1; 5604fca9730SMel Gorman 561748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 562748446bbSMel Gorman 563f40d1e42SMel Gorman /* Isolate free pages. */ 5644fca9730SMel Gorman for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) { 56566c64223SJoonsoo Kim int isolated; 566748446bbSMel Gorman struct page *page = cursor; 567748446bbSMel Gorman 5688b44d279SVlastimil Babka /* 5698b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 5708b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 571d56c1584SMiaohe Lin * pending. 5728b44d279SVlastimil Babka */ 573c036ddffSMiaohe Lin if (!(blockpfn % COMPACT_CLUSTER_MAX) 5748b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 5758b44d279SVlastimil Babka &locked, cc)) 5768b44d279SVlastimil Babka break; 5778b44d279SVlastimil Babka 578b7aba698SMel Gorman nr_scanned++; 5792af120bcSLaura Abbott 5809fcd6d2eSVlastimil Babka /* 5819fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 5829fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 5839fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 5849fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 5859fcd6d2eSVlastimil Babka */ 5869fcd6d2eSVlastimil Babka if (PageCompound(page)) { 58721dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 5889fcd6d2eSVlastimil Babka 589d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) { 59021dc7e02SDavid Rientjes blockpfn += (1UL << order) - 1; 59121dc7e02SDavid Rientjes cursor += (1UL << order) - 1; 5929fcd6d2eSVlastimil Babka } 5939fcd6d2eSVlastimil Babka goto isolate_fail; 5949fcd6d2eSVlastimil Babka } 5959fcd6d2eSVlastimil Babka 596f40d1e42SMel Gorman if (!PageBuddy(page)) 5972af120bcSLaura Abbott goto isolate_fail; 598f40d1e42SMel Gorman 59985f73e6dSMiaohe Lin /* If we already hold the lock, we can skip some rechecking. */ 60069b7189fSVlastimil Babka if (!locked) { 601cb2dcaf0SMel Gorman locked = compact_lock_irqsave(&cc->zone->lock, 6028b44d279SVlastimil Babka &flags, cc); 603f40d1e42SMel Gorman 604f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 605f40d1e42SMel Gorman if (!PageBuddy(page)) 6062af120bcSLaura Abbott goto isolate_fail; 60769b7189fSVlastimil Babka } 608748446bbSMel Gorman 60966c64223SJoonsoo Kim /* Found a free page, will break it into order-0 pages */ 610ab130f91SMatthew Wilcox (Oracle) order = buddy_order(page); 61166c64223SJoonsoo Kim isolated = __isolate_free_page(page, order); 612a4f04f2cSDavid Rientjes if (!isolated) 613a4f04f2cSDavid Rientjes break; 61466c64223SJoonsoo Kim set_page_private(page, order); 615a4f04f2cSDavid Rientjes 616b717d6b9SWilliam Lam nr_scanned += isolated - 1; 617748446bbSMel Gorman total_isolated += isolated; 618a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 61966c64223SJoonsoo Kim list_add_tail(&page->lru, freelist); 62066c64223SJoonsoo Kim 621a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 622932ff6bbSJoonsoo Kim blockpfn += isolated; 623932ff6bbSJoonsoo Kim break; 624932ff6bbSJoonsoo Kim } 625a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 626748446bbSMel Gorman blockpfn += isolated - 1; 627748446bbSMel Gorman cursor += isolated - 1; 6282af120bcSLaura Abbott continue; 6292af120bcSLaura Abbott 6302af120bcSLaura Abbott isolate_fail: 6312af120bcSLaura Abbott if (strict) 6322af120bcSLaura Abbott break; 6332af120bcSLaura Abbott else 6342af120bcSLaura Abbott continue; 6352af120bcSLaura Abbott 636748446bbSMel Gorman } 637748446bbSMel Gorman 638a4f04f2cSDavid Rientjes if (locked) 639a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 640a4f04f2cSDavid Rientjes 6419fcd6d2eSVlastimil Babka /* 6429fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 6439fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 6449fcd6d2eSVlastimil Babka */ 6459fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 6469fcd6d2eSVlastimil Babka blockpfn = end_pfn; 6479fcd6d2eSVlastimil Babka 648e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 649e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 650e34d85f0SJoonsoo Kim 651e14c720eSVlastimil Babka /* Record how far we have got within the block */ 652e14c720eSVlastimil Babka *start_pfn = blockpfn; 653e14c720eSVlastimil Babka 654f40d1e42SMel Gorman /* 655f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 656f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 657f40d1e42SMel Gorman * returned and CMA will fail. 658f40d1e42SMel Gorman */ 6592af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 660f40d1e42SMel Gorman total_isolated = 0; 661f40d1e42SMel Gorman 6627f354a54SDavid Rientjes cc->total_free_scanned += nr_scanned; 663397487dbSMel Gorman if (total_isolated) 664010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 665748446bbSMel Gorman return total_isolated; 666748446bbSMel Gorman } 667748446bbSMel Gorman 66885aa125fSMichal Nazarewicz /** 66985aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 670e8b098fcSMike Rapoport * @cc: Compaction control structure. 67185aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 67285aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 67385aa125fSMichal Nazarewicz * 67485aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 67585aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 67685aa125fSMichal Nazarewicz * undo its actions and return zero. 67785aa125fSMichal Nazarewicz * 67885aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 67985aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 68085aa125fSMichal Nazarewicz * a free page). 68185aa125fSMichal Nazarewicz */ 682ff9543fdSMichal Nazarewicz unsigned long 683bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 684bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 68585aa125fSMichal Nazarewicz { 686e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 68785aa125fSMichal Nazarewicz LIST_HEAD(freelist); 68885aa125fSMichal Nazarewicz 6897d49d886SVlastimil Babka pfn = start_pfn; 69006b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 691e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 692e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 69306b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 6947d49d886SVlastimil Babka 6957d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 696e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 6977d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 698e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 699e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 7007d49d886SVlastimil Babka 70185aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 70285aa125fSMichal Nazarewicz 70358420016SJoonsoo Kim /* 70458420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 70558420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 70658420016SJoonsoo Kim * scanning range to right one. 70758420016SJoonsoo Kim */ 70858420016SJoonsoo Kim if (pfn >= block_end_pfn) { 70906b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 71006b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 71158420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 71258420016SJoonsoo Kim } 71358420016SJoonsoo Kim 714e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 715e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 7167d49d886SVlastimil Babka break; 7177d49d886SVlastimil Babka 718e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 7194fca9730SMel Gorman block_end_pfn, &freelist, 0, true); 72085aa125fSMichal Nazarewicz 72185aa125fSMichal Nazarewicz /* 72285aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 72385aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 72485aa125fSMichal Nazarewicz * non-free pages). 72585aa125fSMichal Nazarewicz */ 72685aa125fSMichal Nazarewicz if (!isolated) 72785aa125fSMichal Nazarewicz break; 72885aa125fSMichal Nazarewicz 72985aa125fSMichal Nazarewicz /* 73085aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 73185aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 73285aa125fSMichal Nazarewicz * page may span two pageblocks). 73385aa125fSMichal Nazarewicz */ 73485aa125fSMichal Nazarewicz } 73585aa125fSMichal Nazarewicz 73666c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 7374469ab98SMel Gorman split_map_pages(&freelist); 73885aa125fSMichal Nazarewicz 73985aa125fSMichal Nazarewicz if (pfn < end_pfn) { 74085aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 74185aa125fSMichal Nazarewicz release_freepages(&freelist); 74285aa125fSMichal Nazarewicz return 0; 74385aa125fSMichal Nazarewicz } 74485aa125fSMichal Nazarewicz 74585aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 74685aa125fSMichal Nazarewicz return pfn; 74785aa125fSMichal Nazarewicz } 74885aa125fSMichal Nazarewicz 749748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 7505f438eeeSAndrey Ryabinin static bool too_many_isolated(pg_data_t *pgdat) 751748446bbSMel Gorman { 752d818fca1SMel Gorman bool too_many; 753d818fca1SMel Gorman 754bc693045SMinchan Kim unsigned long active, inactive, isolated; 755748446bbSMel Gorman 7565f438eeeSAndrey Ryabinin inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + 7575f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_INACTIVE_ANON); 7585f438eeeSAndrey Ryabinin active = node_page_state(pgdat, NR_ACTIVE_FILE) + 7595f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_ACTIVE_ANON); 7605f438eeeSAndrey Ryabinin isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + 7615f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_ISOLATED_ANON); 762748446bbSMel Gorman 763d818fca1SMel Gorman too_many = isolated > (inactive + active) / 2; 764d818fca1SMel Gorman if (!too_many) 765d818fca1SMel Gorman wake_throttle_isolated(pgdat); 766d818fca1SMel Gorman 767d818fca1SMel Gorman return too_many; 768748446bbSMel Gorman } 769748446bbSMel Gorman 7702fe86e00SMichal Nazarewicz /** 771edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 772edc2ca61SVlastimil Babka * a single pageblock 7732fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 774edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 775edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 77689f6c88aSHugh Dickins * @mode: Isolation mode to be used. 7772fe86e00SMichal Nazarewicz * 7782fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 779edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 780c2ad7a1fSOscar Salvador * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion, 781369fa227SOscar Salvador * -ENOMEM in case we could not allocate a page, or 0. 782c2ad7a1fSOscar Salvador * cc->migrate_pfn will contain the next pfn to scan. 7832fe86e00SMichal Nazarewicz * 784edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 785c2ad7a1fSOscar Salvador * and cc->nr_migratepages is updated accordingly. 786748446bbSMel Gorman */ 787c2ad7a1fSOscar Salvador static int 788edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 78989f6c88aSHugh Dickins unsigned long end_pfn, isolate_mode_t mode) 790748446bbSMel Gorman { 7915f438eeeSAndrey Ryabinin pg_data_t *pgdat = cc->zone->zone_pgdat; 792b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 793fa9add64SHugh Dickins struct lruvec *lruvec; 794b8b2d825SXiubo Li unsigned long flags = 0; 7956168d0daSAlex Shi struct lruvec *locked = NULL; 796bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 79789f6c88aSHugh Dickins struct address_space *mapping; 798e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 799fdd048e1SVlastimil Babka bool skip_on_failure = false; 800fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 801e380bebeSMel Gorman bool skip_updated = false; 802c2ad7a1fSOscar Salvador int ret = 0; 803c2ad7a1fSOscar Salvador 804c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn; 805748446bbSMel Gorman 806748446bbSMel Gorman /* 807748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 808748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 809748446bbSMel Gorman * delay for some time until fewer pages are isolated 810748446bbSMel Gorman */ 8115f438eeeSAndrey Ryabinin while (unlikely(too_many_isolated(pgdat))) { 812d20bdd57SZi Yan /* stop isolation if there are still pages not migrated */ 813d20bdd57SZi Yan if (cc->nr_migratepages) 814c2ad7a1fSOscar Salvador return -EAGAIN; 815d20bdd57SZi Yan 816f9e35b3bSMel Gorman /* async migration should just abort */ 817e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 818c2ad7a1fSOscar Salvador return -EAGAIN; 819f9e35b3bSMel Gorman 820c3f4a9a2SMel Gorman reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); 821748446bbSMel Gorman 822748446bbSMel Gorman if (fatal_signal_pending(current)) 823c2ad7a1fSOscar Salvador return -EINTR; 824748446bbSMel Gorman } 825748446bbSMel Gorman 826cf66f070SMel Gorman cond_resched(); 827aeef4b83SDavid Rientjes 828fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 829fdd048e1SVlastimil Babka skip_on_failure = true; 830fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 831fdd048e1SVlastimil Babka } 832fdd048e1SVlastimil Babka 833748446bbSMel Gorman /* Time to isolate some pages for migration */ 834748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 83529c0dde8SVlastimil Babka 836fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 837fdd048e1SVlastimil Babka /* 838fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 839fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 840fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 841fdd048e1SVlastimil Babka * hopefully succeed compaction. 842fdd048e1SVlastimil Babka */ 843fdd048e1SVlastimil Babka if (nr_isolated) 844fdd048e1SVlastimil Babka break; 845fdd048e1SVlastimil Babka 846fdd048e1SVlastimil Babka /* 847fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 848fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 849fdd048e1SVlastimil Babka * current block. Note we can't simply increase 850fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 851fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 852fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 853fdd048e1SVlastimil Babka * previous loop iteration. 854fdd048e1SVlastimil Babka */ 855fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 856fdd048e1SVlastimil Babka } 857fdd048e1SVlastimil Babka 8588b44d279SVlastimil Babka /* 8598b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 860670105a2SMel Gorman * contention, to give chance to IRQs. Abort completely if 861670105a2SMel Gorman * a fatal signal is pending. 8628b44d279SVlastimil Babka */ 863c036ddffSMiaohe Lin if (!(low_pfn % COMPACT_CLUSTER_MAX)) { 8646168d0daSAlex Shi if (locked) { 8656168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 8666168d0daSAlex Shi locked = NULL; 8676168d0daSAlex Shi } 8686168d0daSAlex Shi 8696168d0daSAlex Shi if (fatal_signal_pending(current)) { 8706168d0daSAlex Shi cc->contended = true; 871c2ad7a1fSOscar Salvador ret = -EINTR; 8726168d0daSAlex Shi 873670105a2SMel Gorman goto fatal_pending; 874670105a2SMel Gorman } 875b2eef8c0SAndrea Arcangeli 8766168d0daSAlex Shi cond_resched(); 8776168d0daSAlex Shi } 8786168d0daSAlex Shi 879b7aba698SMel Gorman nr_scanned++; 880748446bbSMel Gorman 881748446bbSMel Gorman page = pfn_to_page(low_pfn); 882dc908600SMel Gorman 883e380bebeSMel Gorman /* 884e380bebeSMel Gorman * Check if the pageblock has already been marked skipped. 885e380bebeSMel Gorman * Only the aligned PFN is checked as the caller isolates 886e380bebeSMel Gorman * COMPACT_CLUSTER_MAX at a time so the second call must 887e380bebeSMel Gorman * not falsely conclude that the block should be skipped. 888e380bebeSMel Gorman */ 889e380bebeSMel Gorman if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { 8904af12d04SMiaohe Lin if (!isolation_suitable(cc, page)) { 891e380bebeSMel Gorman low_pfn = end_pfn; 8929df41314SAlex Shi page = NULL; 893e380bebeSMel Gorman goto isolate_abort; 894e380bebeSMel Gorman } 895bb13ffebSMel Gorman valid_page = page; 896e380bebeSMel Gorman } 897bb13ffebSMel Gorman 898369fa227SOscar Salvador if (PageHuge(page) && cc->alloc_contig) { 899ae37c7ffSOscar Salvador ret = isolate_or_dissolve_huge_page(page, &cc->migratepages); 900369fa227SOscar Salvador 901369fa227SOscar Salvador /* 902369fa227SOscar Salvador * Fail isolation in case isolate_or_dissolve_huge_page() 903369fa227SOscar Salvador * reports an error. In case of -ENOMEM, abort right away. 904369fa227SOscar Salvador */ 905369fa227SOscar Salvador if (ret < 0) { 906369fa227SOscar Salvador /* Do not report -EBUSY down the chain */ 907369fa227SOscar Salvador if (ret == -EBUSY) 908369fa227SOscar Salvador ret = 0; 90966fe1cf7SMiaohe Lin low_pfn += compound_nr(page) - 1; 910369fa227SOscar Salvador goto isolate_fail; 911369fa227SOscar Salvador } 912369fa227SOscar Salvador 913ae37c7ffSOscar Salvador if (PageHuge(page)) { 914ae37c7ffSOscar Salvador /* 915ae37c7ffSOscar Salvador * Hugepage was successfully isolated and placed 916ae37c7ffSOscar Salvador * on the cc->migratepages list. 917ae37c7ffSOscar Salvador */ 918ae37c7ffSOscar Salvador low_pfn += compound_nr(page) - 1; 919ae37c7ffSOscar Salvador goto isolate_success_no_list; 920ae37c7ffSOscar Salvador } 921ae37c7ffSOscar Salvador 922369fa227SOscar Salvador /* 923369fa227SOscar Salvador * Ok, the hugepage was dissolved. Now these pages are 924369fa227SOscar Salvador * Buddy and cannot be re-allocated because they are 925369fa227SOscar Salvador * isolated. Fall-through as the check below handles 926369fa227SOscar Salvador * Buddy pages. 927369fa227SOscar Salvador */ 928369fa227SOscar Salvador } 929369fa227SOscar Salvador 930c122b208SJoonsoo Kim /* 93199c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 93299c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 93399c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 93499c0fd5eSVlastimil Babka * potential isolation targets. 9356c14466cSMel Gorman */ 93699c0fd5eSVlastimil Babka if (PageBuddy(page)) { 937ab130f91SMatthew Wilcox (Oracle) unsigned long freepage_order = buddy_order_unsafe(page); 93899c0fd5eSVlastimil Babka 93999c0fd5eSVlastimil Babka /* 94099c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 94199c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 94299c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 94399c0fd5eSVlastimil Babka */ 94499c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 94599c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 946748446bbSMel Gorman continue; 94799c0fd5eSVlastimil Babka } 948748446bbSMel Gorman 9499927af74SMel Gorman /* 95029c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 9511da2f328SRik van Riel * hugetlbfs are not to be compacted unless we are attempting 9521da2f328SRik van Riel * an allocation much larger than the huge page size (eg CMA). 9531da2f328SRik van Riel * We can potentially save a lot of iterations if we skip them 9541da2f328SRik van Riel * at once. The check is racy, but we can consider only valid 9551da2f328SRik van Riel * values and the only danger is skipping too much. 956bc835011SAndrea Arcangeli */ 9571da2f328SRik van Riel if (PageCompound(page) && !cc->alloc_contig) { 95821dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 95929c0dde8SVlastimil Babka 960d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) 96121dc7e02SDavid Rientjes low_pfn += (1UL << order) - 1; 962fdd048e1SVlastimil Babka goto isolate_fail; 9632a1402aaSMel Gorman } 9642a1402aaSMel Gorman 965bda807d4SMinchan Kim /* 966bda807d4SMinchan Kim * Check may be lockless but that's ok as we recheck later. 967bda807d4SMinchan Kim * It's possible to migrate LRU and non-lru movable pages. 968bda807d4SMinchan Kim * Skip any other type of page 969bda807d4SMinchan Kim */ 970bda807d4SMinchan Kim if (!PageLRU(page)) { 971bda807d4SMinchan Kim /* 972bda807d4SMinchan Kim * __PageMovable can return false positive so we need 973bda807d4SMinchan Kim * to verify it under page_lock. 974bda807d4SMinchan Kim */ 975bda807d4SMinchan Kim if (unlikely(__PageMovable(page)) && 976bda807d4SMinchan Kim !PageIsolated(page)) { 977bda807d4SMinchan Kim if (locked) { 9786168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 9796168d0daSAlex Shi locked = NULL; 980bda807d4SMinchan Kim } 981bda807d4SMinchan Kim 98289f6c88aSHugh Dickins if (!isolate_movable_page(page, mode)) 983bda807d4SMinchan Kim goto isolate_success; 984bda807d4SMinchan Kim } 985bda807d4SMinchan Kim 986fdd048e1SVlastimil Babka goto isolate_fail; 987bda807d4SMinchan Kim } 98829c0dde8SVlastimil Babka 989119d6d59SDavid Rientjes /* 990119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 991119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 992119d6d59SDavid Rientjes * admittedly racy check. 993119d6d59SDavid Rientjes */ 99489f6c88aSHugh Dickins mapping = page_mapping(page); 99589f6c88aSHugh Dickins if (!mapping && page_count(page) > page_mapcount(page)) 996fdd048e1SVlastimil Babka goto isolate_fail; 997119d6d59SDavid Rientjes 99873e64c51SMichal Hocko /* 99973e64c51SMichal Hocko * Only allow to migrate anonymous pages in GFP_NOFS context 100073e64c51SMichal Hocko * because those do not depend on fs locks. 100173e64c51SMichal Hocko */ 100289f6c88aSHugh Dickins if (!(cc->gfp_mask & __GFP_FS) && mapping) 100373e64c51SMichal Hocko goto isolate_fail; 100473e64c51SMichal Hocko 10059df41314SAlex Shi /* 10069df41314SAlex Shi * Be careful not to clear PageLRU until after we're 10079df41314SAlex Shi * sure the page is not being freed elsewhere -- the 10089df41314SAlex Shi * page release code relies on it. 10099df41314SAlex Shi */ 10109df41314SAlex Shi if (unlikely(!get_page_unless_zero(page))) 10119df41314SAlex Shi goto isolate_fail; 10129df41314SAlex Shi 101389f6c88aSHugh Dickins /* Only take pages on LRU: a check now makes later tests safe */ 101489f6c88aSHugh Dickins if (!PageLRU(page)) 10159df41314SAlex Shi goto isolate_fail_put; 10169df41314SAlex Shi 101789f6c88aSHugh Dickins /* Compaction might skip unevictable pages but CMA takes them */ 101889f6c88aSHugh Dickins if (!(mode & ISOLATE_UNEVICTABLE) && PageUnevictable(page)) 101989f6c88aSHugh Dickins goto isolate_fail_put; 102089f6c88aSHugh Dickins 102189f6c88aSHugh Dickins /* 102289f6c88aSHugh Dickins * To minimise LRU disruption, the caller can indicate with 102389f6c88aSHugh Dickins * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages 102489f6c88aSHugh Dickins * it will be able to migrate without blocking - clean pages 102589f6c88aSHugh Dickins * for the most part. PageWriteback would require blocking. 102689f6c88aSHugh Dickins */ 102789f6c88aSHugh Dickins if ((mode & ISOLATE_ASYNC_MIGRATE) && PageWriteback(page)) 102889f6c88aSHugh Dickins goto isolate_fail_put; 102989f6c88aSHugh Dickins 103089f6c88aSHugh Dickins if ((mode & ISOLATE_ASYNC_MIGRATE) && PageDirty(page)) { 103189f6c88aSHugh Dickins bool migrate_dirty; 103289f6c88aSHugh Dickins 103389f6c88aSHugh Dickins /* 103489f6c88aSHugh Dickins * Only pages without mappings or that have a 10359d0ddc0cSMatthew Wilcox (Oracle) * ->migrate_folio callback are possible to migrate 103689f6c88aSHugh Dickins * without blocking. However, we can be racing with 103789f6c88aSHugh Dickins * truncation so it's necessary to lock the page 103889f6c88aSHugh Dickins * to stabilise the mapping as truncation holds 103989f6c88aSHugh Dickins * the page lock until after the page is removed 104089f6c88aSHugh Dickins * from the page cache. 104189f6c88aSHugh Dickins */ 104289f6c88aSHugh Dickins if (!trylock_page(page)) 104389f6c88aSHugh Dickins goto isolate_fail_put; 104489f6c88aSHugh Dickins 104589f6c88aSHugh Dickins mapping = page_mapping(page); 10465490da4fSMatthew Wilcox (Oracle) migrate_dirty = !mapping || 10479d0ddc0cSMatthew Wilcox (Oracle) mapping->a_ops->migrate_folio; 104889f6c88aSHugh Dickins unlock_page(page); 104989f6c88aSHugh Dickins if (!migrate_dirty) 105089f6c88aSHugh Dickins goto isolate_fail_put; 105189f6c88aSHugh Dickins } 105289f6c88aSHugh Dickins 10539df41314SAlex Shi /* Try isolate the page */ 10549df41314SAlex Shi if (!TestClearPageLRU(page)) 10559df41314SAlex Shi goto isolate_fail_put; 10569df41314SAlex Shi 1057b1baabd9SMatthew Wilcox (Oracle) lruvec = folio_lruvec(page_folio(page)); 10586168d0daSAlex Shi 105969b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 10606168d0daSAlex Shi if (lruvec != locked) { 10616168d0daSAlex Shi if (locked) 10626168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 10636168d0daSAlex Shi 10646168d0daSAlex Shi compact_lock_irqsave(&lruvec->lru_lock, &flags, cc); 10656168d0daSAlex Shi locked = lruvec; 10666168d0daSAlex Shi 1067e809c3feSMatthew Wilcox (Oracle) lruvec_memcg_debug(lruvec, page_folio(page)); 1068e380bebeSMel Gorman 1069e380bebeSMel Gorman /* Try get exclusive access under lock */ 1070e380bebeSMel Gorman if (!skip_updated) { 1071e380bebeSMel Gorman skip_updated = true; 1072e380bebeSMel Gorman if (test_and_set_skip(cc, page, low_pfn)) 1073e380bebeSMel Gorman goto isolate_abort; 1074e380bebeSMel Gorman } 10752a1402aaSMel Gorman 107629c0dde8SVlastimil Babka /* 107729c0dde8SVlastimil Babka * Page become compound since the non-locked check, 107829c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 107929c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 108029c0dde8SVlastimil Babka */ 10811da2f328SRik van Riel if (unlikely(PageCompound(page) && !cc->alloc_contig)) { 1082d8c6546bSMatthew Wilcox (Oracle) low_pfn += compound_nr(page) - 1; 10839df41314SAlex Shi SetPageLRU(page); 10849df41314SAlex Shi goto isolate_fail_put; 1085bc835011SAndrea Arcangeli } 1086d99fd5feSAlex Shi } 1087fa9add64SHugh Dickins 10881da2f328SRik van Riel /* The whole page is taken off the LRU; skip the tail pages. */ 10891da2f328SRik van Riel if (PageCompound(page)) 10901da2f328SRik van Riel low_pfn += compound_nr(page) - 1; 1091bc835011SAndrea Arcangeli 1092748446bbSMel Gorman /* Successfully isolated */ 109346ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 10941da2f328SRik van Riel mod_node_page_state(page_pgdat(page), 10959de4f22aSHuang Ying NR_ISOLATED_ANON + page_is_file_lru(page), 10966c357848SMatthew Wilcox (Oracle) thp_nr_pages(page)); 1097b6c75016SJoonsoo Kim 1098b6c75016SJoonsoo Kim isolate_success: 1099fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 1100ae37c7ffSOscar Salvador isolate_success_no_list: 110138935861SZi Yan cc->nr_migratepages += compound_nr(page); 110238935861SZi Yan nr_isolated += compound_nr(page); 1103b717d6b9SWilliam Lam nr_scanned += compound_nr(page) - 1; 1104748446bbSMel Gorman 1105804d3121SMel Gorman /* 1106804d3121SMel Gorman * Avoid isolating too much unless this block is being 1107cb2dcaf0SMel Gorman * rescanned (e.g. dirty/writeback pages, parallel allocation) 1108cb2dcaf0SMel Gorman * or a lock is contended. For contention, isolate quickly to 1109cb2dcaf0SMel Gorman * potentially remove one source of contention. 1110804d3121SMel Gorman */ 111138935861SZi Yan if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && 1112cb2dcaf0SMel Gorman !cc->rescan && !cc->contended) { 111331b8384aSHillf Danton ++low_pfn; 1114748446bbSMel Gorman break; 1115748446bbSMel Gorman } 1116fdd048e1SVlastimil Babka 1117fdd048e1SVlastimil Babka continue; 11189df41314SAlex Shi 11199df41314SAlex Shi isolate_fail_put: 11209df41314SAlex Shi /* Avoid potential deadlock in freeing page under lru_lock */ 11219df41314SAlex Shi if (locked) { 11226168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 11236168d0daSAlex Shi locked = NULL; 11249df41314SAlex Shi } 11259df41314SAlex Shi put_page(page); 11269df41314SAlex Shi 1127fdd048e1SVlastimil Babka isolate_fail: 1128369fa227SOscar Salvador if (!skip_on_failure && ret != -ENOMEM) 1129fdd048e1SVlastimil Babka continue; 1130fdd048e1SVlastimil Babka 1131fdd048e1SVlastimil Babka /* 1132fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 1133fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 1134fdd048e1SVlastimil Babka * page anyway. 1135fdd048e1SVlastimil Babka */ 1136fdd048e1SVlastimil Babka if (nr_isolated) { 1137fdd048e1SVlastimil Babka if (locked) { 11386168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 11396168d0daSAlex Shi locked = NULL; 1140fdd048e1SVlastimil Babka } 1141fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 1142fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 1143fdd048e1SVlastimil Babka nr_isolated = 0; 1144fdd048e1SVlastimil Babka } 1145fdd048e1SVlastimil Babka 1146fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 1147fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 1148fdd048e1SVlastimil Babka /* 1149fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 1150fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 1151fdd048e1SVlastimil Babka */ 1152fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 1153fdd048e1SVlastimil Babka } 1154369fa227SOscar Salvador 1155369fa227SOscar Salvador if (ret == -ENOMEM) 1156369fa227SOscar Salvador break; 115731b8384aSHillf Danton } 1158748446bbSMel Gorman 115999c0fd5eSVlastimil Babka /* 116099c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 116199c0fd5eSVlastimil Babka * the range to be scanned. 116299c0fd5eSVlastimil Babka */ 116399c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 116499c0fd5eSVlastimil Babka low_pfn = end_pfn; 116599c0fd5eSVlastimil Babka 11669df41314SAlex Shi page = NULL; 11679df41314SAlex Shi 1168e380bebeSMel Gorman isolate_abort: 1169c67fe375SMel Gorman if (locked) 11706168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 11719df41314SAlex Shi if (page) { 11729df41314SAlex Shi SetPageLRU(page); 11739df41314SAlex Shi put_page(page); 11749df41314SAlex Shi } 1175748446bbSMel Gorman 117650b5b094SVlastimil Babka /* 1177804d3121SMel Gorman * Updated the cached scanner pfn once the pageblock has been scanned 1178804d3121SMel Gorman * Pages will either be migrated in which case there is no point 1179804d3121SMel Gorman * scanning in the near future or migration failed in which case the 1180804d3121SMel Gorman * failure reason may persist. The block is marked for skipping if 1181804d3121SMel Gorman * there were no pages isolated in the block or if the block is 1182804d3121SMel Gorman * rescanned twice in a row. 118350b5b094SVlastimil Babka */ 1184804d3121SMel Gorman if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { 1185e380bebeSMel Gorman if (valid_page && !skip_updated) 1186e380bebeSMel Gorman set_pageblock_skip(valid_page); 1187e380bebeSMel Gorman update_cached_migrate(cc, low_pfn); 1188e380bebeSMel Gorman } 1189bb13ffebSMel Gorman 1190e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 1191e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 1192b7aba698SMel Gorman 1193670105a2SMel Gorman fatal_pending: 11947f354a54SDavid Rientjes cc->total_migrate_scanned += nr_scanned; 1195397487dbSMel Gorman if (nr_isolated) 1196010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 1197397487dbSMel Gorman 1198c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn; 1199c2ad7a1fSOscar Salvador 1200c2ad7a1fSOscar Salvador return ret; 12012fe86e00SMichal Nazarewicz } 12022fe86e00SMichal Nazarewicz 1203edc2ca61SVlastimil Babka /** 1204edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 1205edc2ca61SVlastimil Babka * @cc: Compaction control structure. 1206edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 1207edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 1208edc2ca61SVlastimil Babka * 1209369fa227SOscar Salvador * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM 1210369fa227SOscar Salvador * in case we could not allocate a page, or 0. 1211edc2ca61SVlastimil Babka */ 1212c2ad7a1fSOscar Salvador int 1213edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 1214edc2ca61SVlastimil Babka unsigned long end_pfn) 1215edc2ca61SVlastimil Babka { 1216e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 1217c2ad7a1fSOscar Salvador int ret = 0; 1218edc2ca61SVlastimil Babka 1219edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 1220edc2ca61SVlastimil Babka pfn = start_pfn; 122106b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 1222e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 1223e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 122406b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 1225edc2ca61SVlastimil Babka 1226edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 1227e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1228edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 1229edc2ca61SVlastimil Babka 1230edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 1231edc2ca61SVlastimil Babka 1232e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 1233e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 1234edc2ca61SVlastimil Babka continue; 1235edc2ca61SVlastimil Babka 1236c2ad7a1fSOscar Salvador ret = isolate_migratepages_block(cc, pfn, block_end_pfn, 1237edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 1238edc2ca61SVlastimil Babka 1239c2ad7a1fSOscar Salvador if (ret) 1240edc2ca61SVlastimil Babka break; 12416ea41c0cSJoonsoo Kim 124238935861SZi Yan if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) 12436ea41c0cSJoonsoo Kim break; 1244edc2ca61SVlastimil Babka } 1245edc2ca61SVlastimil Babka 1246c2ad7a1fSOscar Salvador return ret; 1247edc2ca61SVlastimil Babka } 1248edc2ca61SVlastimil Babka 1249ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1250ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 1251018e9a49SAndrew Morton 1252b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc, 1253b682debdSVlastimil Babka struct page *page) 1254b682debdSVlastimil Babka { 1255282722b0SVlastimil Babka int block_mt; 1256282722b0SVlastimil Babka 12579bebefd5SMel Gorman if (pageblock_skip_persistent(page)) 12589bebefd5SMel Gorman return false; 12599bebefd5SMel Gorman 1260282722b0SVlastimil Babka if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) 1261b682debdSVlastimil Babka return true; 1262b682debdSVlastimil Babka 1263282722b0SVlastimil Babka block_mt = get_pageblock_migratetype(page); 1264282722b0SVlastimil Babka 1265282722b0SVlastimil Babka if (cc->migratetype == MIGRATE_MOVABLE) 1266282722b0SVlastimil Babka return is_migrate_movable(block_mt); 1267282722b0SVlastimil Babka else 1268282722b0SVlastimil Babka return block_mt == cc->migratetype; 1269b682debdSVlastimil Babka } 1270b682debdSVlastimil Babka 1271018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 12729f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc, 12739f7e3387SVlastimil Babka struct page *page) 1274018e9a49SAndrew Morton { 1275018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 1276018e9a49SAndrew Morton if (PageBuddy(page)) { 1277018e9a49SAndrew Morton /* 1278018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 1279018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 1280018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 1281018e9a49SAndrew Morton */ 1282ab130f91SMatthew Wilcox (Oracle) if (buddy_order_unsafe(page) >= pageblock_order) 1283018e9a49SAndrew Morton return false; 1284018e9a49SAndrew Morton } 1285018e9a49SAndrew Morton 12861ef36db2SYisheng Xie if (cc->ignore_block_suitable) 12871ef36db2SYisheng Xie return true; 12881ef36db2SYisheng Xie 1289018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 1290b682debdSVlastimil Babka if (is_migrate_movable(get_pageblock_migratetype(page))) 1291018e9a49SAndrew Morton return true; 1292018e9a49SAndrew Morton 1293018e9a49SAndrew Morton /* Otherwise skip the block */ 1294018e9a49SAndrew Morton return false; 1295018e9a49SAndrew Morton } 1296018e9a49SAndrew Morton 129770b44595SMel Gorman static inline unsigned int 129870b44595SMel Gorman freelist_scan_limit(struct compact_control *cc) 129970b44595SMel Gorman { 1300dd7ef7bdSQian Cai unsigned short shift = BITS_PER_LONG - 1; 1301dd7ef7bdSQian Cai 1302dd7ef7bdSQian Cai return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; 130370b44595SMel Gorman } 130470b44595SMel Gorman 1305ff9543fdSMichal Nazarewicz /* 1306f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 1307f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 1308f2849aa0SVlastimil Babka */ 1309f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 1310f2849aa0SVlastimil Babka { 1311f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 1312f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 1313f2849aa0SVlastimil Babka } 1314f2849aa0SVlastimil Babka 13155a811889SMel Gorman /* 13165a811889SMel Gorman * Used when scanning for a suitable migration target which scans freelists 13175a811889SMel Gorman * in reverse. Reorders the list such as the unscanned pages are scanned 13185a811889SMel Gorman * first on the next iteration of the free scanner 13195a811889SMel Gorman */ 13205a811889SMel Gorman static void 13215a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage) 13225a811889SMel Gorman { 13235a811889SMel Gorman LIST_HEAD(sublist); 13245a811889SMel Gorman 13255a811889SMel Gorman if (!list_is_last(freelist, &freepage->lru)) { 13265a811889SMel Gorman list_cut_before(&sublist, freelist, &freepage->lru); 13275a811889SMel Gorman list_splice_tail(&sublist, freelist); 13285a811889SMel Gorman } 13295a811889SMel Gorman } 13305a811889SMel Gorman 13315a811889SMel Gorman /* 13325a811889SMel Gorman * Similar to move_freelist_head except used by the migration scanner 13335a811889SMel Gorman * when scanning forward. It's possible for these list operations to 13345a811889SMel Gorman * move against each other if they search the free list exactly in 13355a811889SMel Gorman * lockstep. 13365a811889SMel Gorman */ 133770b44595SMel Gorman static void 133870b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage) 133970b44595SMel Gorman { 134070b44595SMel Gorman LIST_HEAD(sublist); 134170b44595SMel Gorman 134270b44595SMel Gorman if (!list_is_first(freelist, &freepage->lru)) { 134370b44595SMel Gorman list_cut_position(&sublist, freelist, &freepage->lru); 134470b44595SMel Gorman list_splice_tail(&sublist, freelist); 134570b44595SMel Gorman } 134670b44595SMel Gorman } 134770b44595SMel Gorman 13485a811889SMel Gorman static void 13495a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) 13505a811889SMel Gorman { 13515a811889SMel Gorman unsigned long start_pfn, end_pfn; 13526e2b7044SVlastimil Babka struct page *page; 13535a811889SMel Gorman 13545a811889SMel Gorman /* Do not search around if there are enough pages already */ 13555a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 13565a811889SMel Gorman return; 13575a811889SMel Gorman 13585a811889SMel Gorman /* Minimise scanning during async compaction */ 13595a811889SMel Gorman if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) 13605a811889SMel Gorman return; 13615a811889SMel Gorman 13625a811889SMel Gorman /* Pageblock boundaries */ 13636e2b7044SVlastimil Babka start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); 13646e2b7044SVlastimil Babka end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); 13656e2b7044SVlastimil Babka 13666e2b7044SVlastimil Babka page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); 13676e2b7044SVlastimil Babka if (!page) 13686e2b7044SVlastimil Babka return; 13695a811889SMel Gorman 13705a811889SMel Gorman /* Scan before */ 13715a811889SMel Gorman if (start_pfn != pfn) { 13724fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false); 13735a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 13745a811889SMel Gorman return; 13755a811889SMel Gorman } 13765a811889SMel Gorman 13775a811889SMel Gorman /* Scan after */ 13785a811889SMel Gorman start_pfn = pfn + nr_isolated; 137960fce36aSMel Gorman if (start_pfn < end_pfn) 13804fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false); 13815a811889SMel Gorman 13825a811889SMel Gorman /* Skip this pageblock in the future as it's full or nearly full */ 13835a811889SMel Gorman if (cc->nr_freepages < cc->nr_migratepages) 13845a811889SMel Gorman set_pageblock_skip(page); 13855a811889SMel Gorman } 13865a811889SMel Gorman 1387dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */ 1388dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order) 1389dbe2d4e4SMel Gorman { 1390dbe2d4e4SMel Gorman order--; 1391dbe2d4e4SMel Gorman if (order < 0) 1392dbe2d4e4SMel Gorman order = cc->order - 1; 1393dbe2d4e4SMel Gorman 1394dbe2d4e4SMel Gorman /* Search wrapped around? */ 1395dbe2d4e4SMel Gorman if (order == cc->search_order) { 1396dbe2d4e4SMel Gorman cc->search_order--; 1397dbe2d4e4SMel Gorman if (cc->search_order < 0) 1398dbe2d4e4SMel Gorman cc->search_order = cc->order - 1; 1399dbe2d4e4SMel Gorman return -1; 1400dbe2d4e4SMel Gorman } 1401dbe2d4e4SMel Gorman 1402dbe2d4e4SMel Gorman return order; 1403dbe2d4e4SMel Gorman } 1404dbe2d4e4SMel Gorman 14055a811889SMel Gorman static unsigned long 14065a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc) 14075a811889SMel Gorman { 1408b55ca526SWonhyuk Yang unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1); 14095a811889SMel Gorman unsigned int nr_scanned = 0; 141074e21484SRokudo Yan unsigned long low_pfn, min_pfn, highest = 0; 14115a811889SMel Gorman unsigned long nr_isolated = 0; 14125a811889SMel Gorman unsigned long distance; 14135a811889SMel Gorman struct page *page = NULL; 14145a811889SMel Gorman bool scan_start = false; 14155a811889SMel Gorman int order; 14165a811889SMel Gorman 14175a811889SMel Gorman /* Full compaction passes in a negative order */ 14185a811889SMel Gorman if (cc->order <= 0) 14195a811889SMel Gorman return cc->free_pfn; 14205a811889SMel Gorman 14215a811889SMel Gorman /* 14225a811889SMel Gorman * If starting the scan, use a deeper search and use the highest 14235a811889SMel Gorman * PFN found if a suitable one is not found. 14245a811889SMel Gorman */ 1425e332f741SMel Gorman if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { 14265a811889SMel Gorman limit = pageblock_nr_pages >> 1; 14275a811889SMel Gorman scan_start = true; 14285a811889SMel Gorman } 14295a811889SMel Gorman 14305a811889SMel Gorman /* 14315a811889SMel Gorman * Preferred point is in the top quarter of the scan space but take 14325a811889SMel Gorman * a pfn from the top half if the search is problematic. 14335a811889SMel Gorman */ 14345a811889SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn); 14355a811889SMel Gorman low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); 14365a811889SMel Gorman min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); 14375a811889SMel Gorman 14385a811889SMel Gorman if (WARN_ON_ONCE(min_pfn > low_pfn)) 14395a811889SMel Gorman low_pfn = min_pfn; 14405a811889SMel Gorman 1441dbe2d4e4SMel Gorman /* 1442dbe2d4e4SMel Gorman * Search starts from the last successful isolation order or the next 1443dbe2d4e4SMel Gorman * order to search after a previous failure 1444dbe2d4e4SMel Gorman */ 1445dbe2d4e4SMel Gorman cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); 1446dbe2d4e4SMel Gorman 1447dbe2d4e4SMel Gorman for (order = cc->search_order; 1448dbe2d4e4SMel Gorman !page && order >= 0; 1449dbe2d4e4SMel Gorman order = next_search_order(cc, order)) { 14505a811889SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 14515a811889SMel Gorman struct list_head *freelist; 14525a811889SMel Gorman struct page *freepage; 14535a811889SMel Gorman unsigned long flags; 14545a811889SMel Gorman unsigned int order_scanned = 0; 145574e21484SRokudo Yan unsigned long high_pfn = 0; 14565a811889SMel Gorman 14575a811889SMel Gorman if (!area->nr_free) 14585a811889SMel Gorman continue; 14595a811889SMel Gorman 14605a811889SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 14615a811889SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 14625a811889SMel Gorman list_for_each_entry_reverse(freepage, freelist, lru) { 14635a811889SMel Gorman unsigned long pfn; 14645a811889SMel Gorman 14655a811889SMel Gorman order_scanned++; 14665a811889SMel Gorman nr_scanned++; 14675a811889SMel Gorman pfn = page_to_pfn(freepage); 14685a811889SMel Gorman 14695a811889SMel Gorman if (pfn >= highest) 14706e2b7044SVlastimil Babka highest = max(pageblock_start_pfn(pfn), 14716e2b7044SVlastimil Babka cc->zone->zone_start_pfn); 14725a811889SMel Gorman 14735a811889SMel Gorman if (pfn >= low_pfn) { 14745a811889SMel Gorman cc->fast_search_fail = 0; 1475dbe2d4e4SMel Gorman cc->search_order = order; 14765a811889SMel Gorman page = freepage; 14775a811889SMel Gorman break; 14785a811889SMel Gorman } 14795a811889SMel Gorman 14805a811889SMel Gorman if (pfn >= min_pfn && pfn > high_pfn) { 14815a811889SMel Gorman high_pfn = pfn; 14825a811889SMel Gorman 14835a811889SMel Gorman /* Shorten the scan if a candidate is found */ 14845a811889SMel Gorman limit >>= 1; 14855a811889SMel Gorman } 14865a811889SMel Gorman 14875a811889SMel Gorman if (order_scanned >= limit) 14885a811889SMel Gorman break; 14895a811889SMel Gorman } 14905a811889SMel Gorman 14915a811889SMel Gorman /* Use a minimum pfn if a preferred one was not found */ 14925a811889SMel Gorman if (!page && high_pfn) { 14935a811889SMel Gorman page = pfn_to_page(high_pfn); 14945a811889SMel Gorman 14955a811889SMel Gorman /* Update freepage for the list reorder below */ 14965a811889SMel Gorman freepage = page; 14975a811889SMel Gorman } 14985a811889SMel Gorman 14995a811889SMel Gorman /* Reorder to so a future search skips recent pages */ 15005a811889SMel Gorman move_freelist_head(freelist, freepage); 15015a811889SMel Gorman 15025a811889SMel Gorman /* Isolate the page if available */ 15035a811889SMel Gorman if (page) { 15045a811889SMel Gorman if (__isolate_free_page(page, order)) { 15055a811889SMel Gorman set_page_private(page, order); 15065a811889SMel Gorman nr_isolated = 1 << order; 1507b717d6b9SWilliam Lam nr_scanned += nr_isolated - 1; 15085a811889SMel Gorman cc->nr_freepages += nr_isolated; 15095a811889SMel Gorman list_add_tail(&page->lru, &cc->freepages); 15105a811889SMel Gorman count_compact_events(COMPACTISOLATED, nr_isolated); 15115a811889SMel Gorman } else { 15125a811889SMel Gorman /* If isolation fails, abort the search */ 15135b56d996SQian Cai order = cc->search_order + 1; 15145a811889SMel Gorman page = NULL; 15155a811889SMel Gorman } 15165a811889SMel Gorman } 15175a811889SMel Gorman 15185a811889SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 15195a811889SMel Gorman 15205a811889SMel Gorman /* 1521b55ca526SWonhyuk Yang * Smaller scan on next order so the total scan is related 15225a811889SMel Gorman * to freelist_scan_limit. 15235a811889SMel Gorman */ 15245a811889SMel Gorman if (order_scanned >= limit) 1525b55ca526SWonhyuk Yang limit = max(1U, limit >> 1); 15265a811889SMel Gorman } 15275a811889SMel Gorman 15285a811889SMel Gorman if (!page) { 15295a811889SMel Gorman cc->fast_search_fail++; 15305a811889SMel Gorman if (scan_start) { 15315a811889SMel Gorman /* 15325a811889SMel Gorman * Use the highest PFN found above min. If one was 1533f3867755SEthon Paul * not found, be pessimistic for direct compaction 15345a811889SMel Gorman * and use the min mark. 15355a811889SMel Gorman */ 1536ca2864e5SMiaohe Lin if (highest >= min_pfn) { 15375a811889SMel Gorman page = pfn_to_page(highest); 15385a811889SMel Gorman cc->free_pfn = highest; 15395a811889SMel Gorman } else { 1540e577c8b6SSuzuki K Poulose if (cc->direct_compaction && pfn_valid(min_pfn)) { 154173a6e474SBaoquan He page = pageblock_pfn_to_page(min_pfn, 15426e2b7044SVlastimil Babka min(pageblock_end_pfn(min_pfn), 15436e2b7044SVlastimil Babka zone_end_pfn(cc->zone)), 154473a6e474SBaoquan He cc->zone); 15455a811889SMel Gorman cc->free_pfn = min_pfn; 15465a811889SMel Gorman } 15475a811889SMel Gorman } 15485a811889SMel Gorman } 15495a811889SMel Gorman } 15505a811889SMel Gorman 1551d097a6f6SMel Gorman if (highest && highest >= cc->zone->compact_cached_free_pfn) { 1552d097a6f6SMel Gorman highest -= pageblock_nr_pages; 15535a811889SMel Gorman cc->zone->compact_cached_free_pfn = highest; 1554d097a6f6SMel Gorman } 15555a811889SMel Gorman 15565a811889SMel Gorman cc->total_free_scanned += nr_scanned; 15575a811889SMel Gorman if (!page) 15585a811889SMel Gorman return cc->free_pfn; 15595a811889SMel Gorman 15605a811889SMel Gorman low_pfn = page_to_pfn(page); 15615a811889SMel Gorman fast_isolate_around(cc, low_pfn, nr_isolated); 15625a811889SMel Gorman return low_pfn; 15635a811889SMel Gorman } 15645a811889SMel Gorman 1565f2849aa0SVlastimil Babka /* 1566ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 1567ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 1568ff9543fdSMichal Nazarewicz */ 1569edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 1570ff9543fdSMichal Nazarewicz { 1571edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 1572ff9543fdSMichal Nazarewicz struct page *page; 1573c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 1574e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 1575c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 1576c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 1577ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 15784fca9730SMel Gorman unsigned int stride; 15792fe86e00SMichal Nazarewicz 15805a811889SMel Gorman /* Try a small search of the free lists for a candidate */ 158100bc102fSMiaohe Lin fast_isolate_freepages(cc); 15825a811889SMel Gorman if (cc->nr_freepages) 15835a811889SMel Gorman goto splitmap; 15845a811889SMel Gorman 1585ff9543fdSMichal Nazarewicz /* 1586ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 158749e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 1588e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 1589e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 1590c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 1591c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 1592a1c1dbebSRandy Dunlap * zone which ends in the middle of a pageblock. 159349e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 159449e068f0SVlastimil Babka * is using. 1595ff9543fdSMichal Nazarewicz */ 1596e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 15975a811889SMel Gorman block_start_pfn = pageblock_start_pfn(isolate_start_pfn); 1598c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1599c96b9e50SVlastimil Babka zone_end_pfn(zone)); 160006b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 16014fca9730SMel Gorman stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; 16022fe86e00SMichal Nazarewicz 1603ff9543fdSMichal Nazarewicz /* 1604ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1605ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1606ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1607ff9543fdSMichal Nazarewicz */ 1608f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1609c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1610e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1611e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 16124fca9730SMel Gorman unsigned long nr_isolated; 16134fca9730SMel Gorman 1614f6ea3adbSDavid Rientjes /* 1615f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1616cb810ad2SMel Gorman * suitable migration targets, so periodically check resched. 1617f6ea3adbSDavid Rientjes */ 1618c036ddffSMiaohe Lin if (!(block_start_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) 1619cf66f070SMel Gorman cond_resched(); 1620f6ea3adbSDavid Rientjes 16217d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 16227d49d886SVlastimil Babka zone); 16237d49d886SVlastimil Babka if (!page) 1624ff9543fdSMichal Nazarewicz continue; 1625ff9543fdSMichal Nazarewicz 1626ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 16279f7e3387SVlastimil Babka if (!suitable_migration_target(cc, page)) 1628ff9543fdSMichal Nazarewicz continue; 162968e3e926SLinus Torvalds 1630bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1631bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1632bb13ffebSMel Gorman continue; 1633bb13ffebSMel Gorman 1634e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 16354fca9730SMel Gorman nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, 16364fca9730SMel Gorman block_end_pfn, freelist, stride, false); 1637ff9543fdSMichal Nazarewicz 1638d097a6f6SMel Gorman /* Update the skip hint if the full pageblock was scanned */ 1639d097a6f6SMel Gorman if (isolate_start_pfn == block_end_pfn) 1640d097a6f6SMel Gorman update_pageblock_skip(cc, page, block_start_pfn); 1641d097a6f6SMel Gorman 1642cb2dcaf0SMel Gorman /* Are enough freepages isolated? */ 1643cb2dcaf0SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) { 1644a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1645a46cbf3bSDavid Rientjes /* 1646a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1647a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1648a46cbf3bSDavid Rientjes */ 1649f5f61a32SVlastimil Babka isolate_start_pfn = 1650e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1651a46cbf3bSDavid Rientjes } 1652be976572SVlastimil Babka break; 1653a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1654f5f61a32SVlastimil Babka /* 1655a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1656a46cbf3bSDavid Rientjes * needlessly. 1657f5f61a32SVlastimil Babka */ 1658a46cbf3bSDavid Rientjes break; 1659f5f61a32SVlastimil Babka } 16604fca9730SMel Gorman 16614fca9730SMel Gorman /* Adjust stride depending on isolation */ 16624fca9730SMel Gorman if (nr_isolated) { 16634fca9730SMel Gorman stride = 1; 16644fca9730SMel Gorman continue; 16654fca9730SMel Gorman } 16664fca9730SMel Gorman stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); 1667c89511abSMel Gorman } 1668ff9543fdSMichal Nazarewicz 16697ed695e0SVlastimil Babka /* 1670f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1671f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1672f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1673f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 16747ed695e0SVlastimil Babka */ 1675f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 16765a811889SMel Gorman 16775a811889SMel Gorman splitmap: 16785a811889SMel Gorman /* __isolate_free_page() does not map the pages */ 16795a811889SMel Gorman split_map_pages(freelist); 1680748446bbSMel Gorman } 1681748446bbSMel Gorman 1682748446bbSMel Gorman /* 1683748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1684748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1685748446bbSMel Gorman */ 1686748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1687666feb21SMichal Hocko unsigned long data) 1688748446bbSMel Gorman { 1689748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1690748446bbSMel Gorman struct page *freepage; 1691748446bbSMel Gorman 1692748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1693edc2ca61SVlastimil Babka isolate_freepages(cc); 1694748446bbSMel Gorman 1695748446bbSMel Gorman if (list_empty(&cc->freepages)) 1696748446bbSMel Gorman return NULL; 1697748446bbSMel Gorman } 1698748446bbSMel Gorman 1699748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1700748446bbSMel Gorman list_del(&freepage->lru); 1701748446bbSMel Gorman cc->nr_freepages--; 1702748446bbSMel Gorman 1703748446bbSMel Gorman return freepage; 1704748446bbSMel Gorman } 1705748446bbSMel Gorman 1706748446bbSMel Gorman /* 1707d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1708d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1709d53aea3dSDavid Rientjes * special handling needed for NUMA. 1710d53aea3dSDavid Rientjes */ 1711d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1712d53aea3dSDavid Rientjes { 1713d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1714d53aea3dSDavid Rientjes 1715d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1716d53aea3dSDavid Rientjes cc->nr_freepages++; 1717d53aea3dSDavid Rientjes } 1718d53aea3dSDavid Rientjes 1719ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1720ff9543fdSMichal Nazarewicz typedef enum { 1721ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1722ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1723ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1724ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1725ff9543fdSMichal Nazarewicz 1726ff9543fdSMichal Nazarewicz /* 17275bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 17285bbe3547SEric B Munson * compactable pages. 17295bbe3547SEric B Munson */ 1730*c7e0b3d0SThomas Gleixner int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNEVICTABLE_DEFAULT; 17315bbe3547SEric B Munson 173270b44595SMel Gorman static inline void 173370b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) 173470b44595SMel Gorman { 173570b44595SMel Gorman if (cc->fast_start_pfn == ULONG_MAX) 173670b44595SMel Gorman return; 173770b44595SMel Gorman 173870b44595SMel Gorman if (!cc->fast_start_pfn) 173970b44595SMel Gorman cc->fast_start_pfn = pfn; 174070b44595SMel Gorman 174170b44595SMel Gorman cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); 174270b44595SMel Gorman } 174370b44595SMel Gorman 174470b44595SMel Gorman static inline unsigned long 174570b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc) 174670b44595SMel Gorman { 174770b44595SMel Gorman if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) 174870b44595SMel Gorman return cc->migrate_pfn; 174970b44595SMel Gorman 175070b44595SMel Gorman cc->migrate_pfn = cc->fast_start_pfn; 175170b44595SMel Gorman cc->fast_start_pfn = ULONG_MAX; 175270b44595SMel Gorman 175370b44595SMel Gorman return cc->migrate_pfn; 175470b44595SMel Gorman } 175570b44595SMel Gorman 175670b44595SMel Gorman /* 175770b44595SMel Gorman * Briefly search the free lists for a migration source that already has 175870b44595SMel Gorman * some free pages to reduce the number of pages that need migration 175970b44595SMel Gorman * before a pageblock is free. 176070b44595SMel Gorman */ 176170b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc) 176270b44595SMel Gorman { 176370b44595SMel Gorman unsigned int limit = freelist_scan_limit(cc); 176470b44595SMel Gorman unsigned int nr_scanned = 0; 176570b44595SMel Gorman unsigned long distance; 176670b44595SMel Gorman unsigned long pfn = cc->migrate_pfn; 176770b44595SMel Gorman unsigned long high_pfn; 176870b44595SMel Gorman int order; 176915d28d0dSWonhyuk Yang bool found_block = false; 177070b44595SMel Gorman 177170b44595SMel Gorman /* Skip hints are relied on to avoid repeats on the fast search */ 177270b44595SMel Gorman if (cc->ignore_skip_hint) 177370b44595SMel Gorman return pfn; 177470b44595SMel Gorman 177570b44595SMel Gorman /* 177670b44595SMel Gorman * If the migrate_pfn is not at the start of a zone or the start 177770b44595SMel Gorman * of a pageblock then assume this is a continuation of a previous 177870b44595SMel Gorman * scan restarted due to COMPACT_CLUSTER_MAX. 177970b44595SMel Gorman */ 178070b44595SMel Gorman if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) 178170b44595SMel Gorman return pfn; 178270b44595SMel Gorman 178370b44595SMel Gorman /* 178470b44595SMel Gorman * For smaller orders, just linearly scan as the number of pages 178570b44595SMel Gorman * to migrate should be relatively small and does not necessarily 178670b44595SMel Gorman * justify freeing up a large block for a small allocation. 178770b44595SMel Gorman */ 178870b44595SMel Gorman if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) 178970b44595SMel Gorman return pfn; 179070b44595SMel Gorman 179170b44595SMel Gorman /* 179270b44595SMel Gorman * Only allow kcompactd and direct requests for movable pages to 179370b44595SMel Gorman * quickly clear out a MOVABLE pageblock for allocation. This 179470b44595SMel Gorman * reduces the risk that a large movable pageblock is freed for 179570b44595SMel Gorman * an unmovable/reclaimable small allocation. 179670b44595SMel Gorman */ 179770b44595SMel Gorman if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) 179870b44595SMel Gorman return pfn; 179970b44595SMel Gorman 180070b44595SMel Gorman /* 180170b44595SMel Gorman * When starting the migration scanner, pick any pageblock within the 180270b44595SMel Gorman * first half of the search space. Otherwise try and pick a pageblock 180370b44595SMel Gorman * within the first eighth to reduce the chances that a migration 180470b44595SMel Gorman * target later becomes a source. 180570b44595SMel Gorman */ 180670b44595SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn) >> 1; 180770b44595SMel Gorman if (cc->migrate_pfn != cc->zone->zone_start_pfn) 180870b44595SMel Gorman distance >>= 2; 180970b44595SMel Gorman high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); 181070b44595SMel Gorman 181170b44595SMel Gorman for (order = cc->order - 1; 181215d28d0dSWonhyuk Yang order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; 181370b44595SMel Gorman order--) { 181470b44595SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 181570b44595SMel Gorman struct list_head *freelist; 181670b44595SMel Gorman unsigned long flags; 181770b44595SMel Gorman struct page *freepage; 181870b44595SMel Gorman 181970b44595SMel Gorman if (!area->nr_free) 182070b44595SMel Gorman continue; 182170b44595SMel Gorman 182270b44595SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 182370b44595SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 182470b44595SMel Gorman list_for_each_entry(freepage, freelist, lru) { 182570b44595SMel Gorman unsigned long free_pfn; 182670b44595SMel Gorman 182715d28d0dSWonhyuk Yang if (nr_scanned++ >= limit) { 182815d28d0dSWonhyuk Yang move_freelist_tail(freelist, freepage); 182915d28d0dSWonhyuk Yang break; 183015d28d0dSWonhyuk Yang } 183115d28d0dSWonhyuk Yang 183270b44595SMel Gorman free_pfn = page_to_pfn(freepage); 183370b44595SMel Gorman if (free_pfn < high_pfn) { 183470b44595SMel Gorman /* 183570b44595SMel Gorman * Avoid if skipped recently. Ideally it would 183670b44595SMel Gorman * move to the tail but even safe iteration of 183770b44595SMel Gorman * the list assumes an entry is deleted, not 183870b44595SMel Gorman * reordered. 183970b44595SMel Gorman */ 184015d28d0dSWonhyuk Yang if (get_pageblock_skip(freepage)) 184170b44595SMel Gorman continue; 184270b44595SMel Gorman 184370b44595SMel Gorman /* Reorder to so a future search skips recent pages */ 184470b44595SMel Gorman move_freelist_tail(freelist, freepage); 184570b44595SMel Gorman 1846e380bebeSMel Gorman update_fast_start_pfn(cc, free_pfn); 184770b44595SMel Gorman pfn = pageblock_start_pfn(free_pfn); 1848bbe832b9SRei Yamamoto if (pfn < cc->zone->zone_start_pfn) 1849bbe832b9SRei Yamamoto pfn = cc->zone->zone_start_pfn; 185070b44595SMel Gorman cc->fast_search_fail = 0; 185115d28d0dSWonhyuk Yang found_block = true; 185270b44595SMel Gorman set_pageblock_skip(freepage); 185370b44595SMel Gorman break; 185470b44595SMel Gorman } 185570b44595SMel Gorman } 185670b44595SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 185770b44595SMel Gorman } 185870b44595SMel Gorman 185970b44595SMel Gorman cc->total_migrate_scanned += nr_scanned; 186070b44595SMel Gorman 186170b44595SMel Gorman /* 186270b44595SMel Gorman * If fast scanning failed then use a cached entry for a page block 186370b44595SMel Gorman * that had free pages as the basis for starting a linear scan. 186470b44595SMel Gorman */ 186515d28d0dSWonhyuk Yang if (!found_block) { 186615d28d0dSWonhyuk Yang cc->fast_search_fail++; 186770b44595SMel Gorman pfn = reinit_migrate_pfn(cc); 186815d28d0dSWonhyuk Yang } 186970b44595SMel Gorman return pfn; 187070b44595SMel Gorman } 187170b44595SMel Gorman 18725bbe3547SEric B Munson /* 1873edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1874edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1875edc2ca61SVlastimil Babka * compact_control. 1876ff9543fdSMichal Nazarewicz */ 187732aaf055SPengfei Li static isolate_migrate_t isolate_migratepages(struct compact_control *cc) 1878ff9543fdSMichal Nazarewicz { 1879e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1880e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1881e1409c32SJoonsoo Kim unsigned long low_pfn; 1882edc2ca61SVlastimil Babka struct page *page; 1883edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 18845bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 18851d2047feSHugh Dickins (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); 188670b44595SMel Gorman bool fast_find_block; 1887ff9543fdSMichal Nazarewicz 1888edc2ca61SVlastimil Babka /* 1889edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 189070b44595SMel Gorman * initialized by compact_zone(). The first failure will use 189170b44595SMel Gorman * the lowest PFN as the starting point for linear scanning. 1892edc2ca61SVlastimil Babka */ 189370b44595SMel Gorman low_pfn = fast_find_migrateblock(cc); 189406b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 189532aaf055SPengfei Li if (block_start_pfn < cc->zone->zone_start_pfn) 189632aaf055SPengfei Li block_start_pfn = cc->zone->zone_start_pfn; 1897ff9543fdSMichal Nazarewicz 189870b44595SMel Gorman /* 189970b44595SMel Gorman * fast_find_migrateblock marks a pageblock skipped so to avoid 190070b44595SMel Gorman * the isolation_suitable check below, check whether the fast 190170b44595SMel Gorman * search was successful. 190270b44595SMel Gorman */ 190370b44595SMel Gorman fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; 190470b44595SMel Gorman 1905ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 190606b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1907ff9543fdSMichal Nazarewicz 1908edc2ca61SVlastimil Babka /* 1909edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1910edc2ca61SVlastimil Babka * Do not cross the free scanner. 1911edc2ca61SVlastimil Babka */ 1912e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 191370b44595SMel Gorman fast_find_block = false, 1914c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn = block_end_pfn, 1915e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1916e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1917edc2ca61SVlastimil Babka 1918edc2ca61SVlastimil Babka /* 1919edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1920edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1921cb810ad2SMel Gorman * need to schedule. 1922edc2ca61SVlastimil Babka */ 1923c036ddffSMiaohe Lin if (!(low_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) 1924cf66f070SMel Gorman cond_resched(); 1925edc2ca61SVlastimil Babka 192632aaf055SPengfei Li page = pageblock_pfn_to_page(block_start_pfn, 192732aaf055SPengfei Li block_end_pfn, cc->zone); 19287d49d886SVlastimil Babka if (!page) 1929edc2ca61SVlastimil Babka continue; 1930edc2ca61SVlastimil Babka 1931e380bebeSMel Gorman /* 1932e380bebeSMel Gorman * If isolation recently failed, do not retry. Only check the 1933e380bebeSMel Gorman * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock 1934e380bebeSMel Gorman * to be visited multiple times. Assume skip was checked 1935e380bebeSMel Gorman * before making it "skip" so other compaction instances do 1936e380bebeSMel Gorman * not scan the same block. 1937e380bebeSMel Gorman */ 1938e380bebeSMel Gorman if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && 1939e380bebeSMel Gorman !fast_find_block && !isolation_suitable(cc, page)) 1940edc2ca61SVlastimil Babka continue; 1941edc2ca61SVlastimil Babka 1942edc2ca61SVlastimil Babka /* 1943556162bfSMiaohe Lin * For async direct compaction, only scan the pageblocks of the 1944556162bfSMiaohe Lin * same migratetype without huge pages. Async direct compaction 1945556162bfSMiaohe Lin * is optimistic to see if the minimum amount of work satisfies 1946556162bfSMiaohe Lin * the allocation. The cached PFN is updated as it's possible 1947556162bfSMiaohe Lin * that all remaining blocks between source and target are 1948556162bfSMiaohe Lin * unsuitable and the compaction scanners fail to meet. 1949edc2ca61SVlastimil Babka */ 19509bebefd5SMel Gorman if (!suitable_migration_source(cc, page)) { 19519bebefd5SMel Gorman update_cached_migrate(cc, block_end_pfn); 1952edc2ca61SVlastimil Babka continue; 19539bebefd5SMel Gorman } 1954ff9543fdSMichal Nazarewicz 1955ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1956c2ad7a1fSOscar Salvador if (isolate_migratepages_block(cc, low_pfn, block_end_pfn, 1957c2ad7a1fSOscar Salvador isolate_mode)) 1958ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1959ff9543fdSMichal Nazarewicz 1960edc2ca61SVlastimil Babka /* 1961edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1962edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1963edc2ca61SVlastimil Babka * continue or not. 1964edc2ca61SVlastimil Babka */ 1965edc2ca61SVlastimil Babka break; 1966edc2ca61SVlastimil Babka } 1967edc2ca61SVlastimil Babka 1968edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1969ff9543fdSMichal Nazarewicz } 1970ff9543fdSMichal Nazarewicz 197121c527a3SYaowei Bai /* 197221c527a3SYaowei Bai * order == -1 is expected when compacting via 197321c527a3SYaowei Bai * /proc/sys/vm/compact_memory 197421c527a3SYaowei Bai */ 197521c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 197621c527a3SYaowei Bai { 197721c527a3SYaowei Bai return order == -1; 197821c527a3SYaowei Bai } 197921c527a3SYaowei Bai 1980facdaa91SNitin Gupta static bool kswapd_is_running(pg_data_t *pgdat) 1981facdaa91SNitin Gupta { 1982b03fbd4fSPeter Zijlstra return pgdat->kswapd && task_is_running(pgdat->kswapd); 1983facdaa91SNitin Gupta } 1984facdaa91SNitin Gupta 1985facdaa91SNitin Gupta /* 1986facdaa91SNitin Gupta * A zone's fragmentation score is the external fragmentation wrt to the 198740d7e203SCharan Teja Reddy * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. 198840d7e203SCharan Teja Reddy */ 198940d7e203SCharan Teja Reddy static unsigned int fragmentation_score_zone(struct zone *zone) 199040d7e203SCharan Teja Reddy { 199140d7e203SCharan Teja Reddy return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); 199240d7e203SCharan Teja Reddy } 199340d7e203SCharan Teja Reddy 199440d7e203SCharan Teja Reddy /* 199540d7e203SCharan Teja Reddy * A weighted zone's fragmentation score is the external fragmentation 199640d7e203SCharan Teja Reddy * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It 199740d7e203SCharan Teja Reddy * returns a value in the range [0, 100]. 1998facdaa91SNitin Gupta * 1999facdaa91SNitin Gupta * The scaling factor ensures that proactive compaction focuses on larger 2000facdaa91SNitin Gupta * zones like ZONE_NORMAL, rather than smaller, specialized zones like 2001facdaa91SNitin Gupta * ZONE_DMA32. For smaller zones, the score value remains close to zero, 2002facdaa91SNitin Gupta * and thus never exceeds the high threshold for proactive compaction. 2003facdaa91SNitin Gupta */ 200440d7e203SCharan Teja Reddy static unsigned int fragmentation_score_zone_weighted(struct zone *zone) 2005facdaa91SNitin Gupta { 2006facdaa91SNitin Gupta unsigned long score; 2007facdaa91SNitin Gupta 200840d7e203SCharan Teja Reddy score = zone->present_pages * fragmentation_score_zone(zone); 2009facdaa91SNitin Gupta return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); 2010facdaa91SNitin Gupta } 2011facdaa91SNitin Gupta 2012facdaa91SNitin Gupta /* 2013facdaa91SNitin Gupta * The per-node proactive (background) compaction process is started by its 2014facdaa91SNitin Gupta * corresponding kcompactd thread when the node's fragmentation score 2015facdaa91SNitin Gupta * exceeds the high threshold. The compaction process remains active till 2016facdaa91SNitin Gupta * the node's score falls below the low threshold, or one of the back-off 2017facdaa91SNitin Gupta * conditions is met. 2018facdaa91SNitin Gupta */ 2019d34c0a75SNitin Gupta static unsigned int fragmentation_score_node(pg_data_t *pgdat) 2020facdaa91SNitin Gupta { 2021d34c0a75SNitin Gupta unsigned int score = 0; 2022facdaa91SNitin Gupta int zoneid; 2023facdaa91SNitin Gupta 2024facdaa91SNitin Gupta for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2025facdaa91SNitin Gupta struct zone *zone; 2026facdaa91SNitin Gupta 2027facdaa91SNitin Gupta zone = &pgdat->node_zones[zoneid]; 202840d7e203SCharan Teja Reddy score += fragmentation_score_zone_weighted(zone); 2029facdaa91SNitin Gupta } 2030facdaa91SNitin Gupta 2031facdaa91SNitin Gupta return score; 2032facdaa91SNitin Gupta } 2033facdaa91SNitin Gupta 2034d34c0a75SNitin Gupta static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low) 2035facdaa91SNitin Gupta { 2036d34c0a75SNitin Gupta unsigned int wmark_low; 2037facdaa91SNitin Gupta 2038facdaa91SNitin Gupta /* 2039f0953a1bSIngo Molnar * Cap the low watermark to avoid excessive compaction 2040f0953a1bSIngo Molnar * activity in case a user sets the proactiveness tunable 2041facdaa91SNitin Gupta * close to 100 (maximum). 2042facdaa91SNitin Gupta */ 2043d34c0a75SNitin Gupta wmark_low = max(100U - sysctl_compaction_proactiveness, 5U); 2044d34c0a75SNitin Gupta return low ? wmark_low : min(wmark_low + 10, 100U); 2045facdaa91SNitin Gupta } 2046facdaa91SNitin Gupta 2047facdaa91SNitin Gupta static bool should_proactive_compact_node(pg_data_t *pgdat) 2048facdaa91SNitin Gupta { 2049facdaa91SNitin Gupta int wmark_high; 2050facdaa91SNitin Gupta 2051facdaa91SNitin Gupta if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) 2052facdaa91SNitin Gupta return false; 2053facdaa91SNitin Gupta 2054facdaa91SNitin Gupta wmark_high = fragmentation_score_wmark(pgdat, false); 2055facdaa91SNitin Gupta return fragmentation_score_node(pgdat) > wmark_high; 2056facdaa91SNitin Gupta } 2057facdaa91SNitin Gupta 205840cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc) 2059748446bbSMel Gorman { 20608fb74b9fSMel Gorman unsigned int order; 2061d39773a0SVlastimil Babka const int migratetype = cc->migratetype; 2062cb2dcaf0SMel Gorman int ret; 2063748446bbSMel Gorman 2064753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 2065f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 206655b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 206740cacbcbSMel Gorman reset_cached_positions(cc->zone); 206855b7c4c9SVlastimil Babka 206962997027SMel Gorman /* 207062997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 2071accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 207262997027SMel Gorman * flag itself as the decision to be clear should be directly 207362997027SMel Gorman * based on an allocation request. 207462997027SMel Gorman */ 2075accf6242SVlastimil Babka if (cc->direct_compaction) 207640cacbcbSMel Gorman cc->zone->compact_blockskip_flush = true; 207762997027SMel Gorman 2078c8f7de0bSMichal Hocko if (cc->whole_zone) 2079748446bbSMel Gorman return COMPACT_COMPLETE; 2080c8f7de0bSMichal Hocko else 2081c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 2082bb13ffebSMel Gorman } 2083748446bbSMel Gorman 2084facdaa91SNitin Gupta if (cc->proactive_compaction) { 2085facdaa91SNitin Gupta int score, wmark_low; 2086facdaa91SNitin Gupta pg_data_t *pgdat; 2087facdaa91SNitin Gupta 2088facdaa91SNitin Gupta pgdat = cc->zone->zone_pgdat; 2089facdaa91SNitin Gupta if (kswapd_is_running(pgdat)) 2090facdaa91SNitin Gupta return COMPACT_PARTIAL_SKIPPED; 2091facdaa91SNitin Gupta 2092facdaa91SNitin Gupta score = fragmentation_score_zone(cc->zone); 2093facdaa91SNitin Gupta wmark_low = fragmentation_score_wmark(pgdat, true); 2094facdaa91SNitin Gupta 2095facdaa91SNitin Gupta if (score > wmark_low) 2096facdaa91SNitin Gupta ret = COMPACT_CONTINUE; 2097facdaa91SNitin Gupta else 2098facdaa91SNitin Gupta ret = COMPACT_SUCCESS; 2099facdaa91SNitin Gupta 2100facdaa91SNitin Gupta goto out; 2101facdaa91SNitin Gupta } 2102facdaa91SNitin Gupta 210321c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 210456de7263SMel Gorman return COMPACT_CONTINUE; 210556de7263SMel Gorman 2106baf6a9a1SVlastimil Babka /* 2107efe771c7SMel Gorman * Always finish scanning a pageblock to reduce the possibility of 2108efe771c7SMel Gorman * fallbacks in the future. This is particularly important when 2109efe771c7SMel Gorman * migration source is unmovable/reclaimable but it's not worth 2110efe771c7SMel Gorman * special casing. 2111baf6a9a1SVlastimil Babka */ 2112efe771c7SMel Gorman if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) 2113baf6a9a1SVlastimil Babka return COMPACT_CONTINUE; 2114baf6a9a1SVlastimil Babka 211556de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 2116cb2dcaf0SMel Gorman ret = COMPACT_NO_SUITABLE_PAGE; 211756de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 211840cacbcbSMel Gorman struct free_area *area = &cc->zone->free_area[order]; 21192149cdaeSJoonsoo Kim bool can_steal; 21208fb74b9fSMel Gorman 212156de7263SMel Gorman /* Job done if page is free of the right migratetype */ 2122b03641afSDan Williams if (!free_area_empty(area, migratetype)) 2123cf378319SVlastimil Babka return COMPACT_SUCCESS; 212456de7263SMel Gorman 21252149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 21262149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 21272149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 2128b03641afSDan Williams !free_area_empty(area, MIGRATE_CMA)) 2129cf378319SVlastimil Babka return COMPACT_SUCCESS; 21302149cdaeSJoonsoo Kim #endif 21312149cdaeSJoonsoo Kim /* 21322149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 21332149cdaeSJoonsoo Kim * other migratetype buddy lists. 21342149cdaeSJoonsoo Kim */ 21352149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 2136fa599c44SMiaohe Lin true, &can_steal) != -1) 2137baf6a9a1SVlastimil Babka /* 2138fa599c44SMiaohe Lin * Movable pages are OK in any pageblock. If we are 2139fa599c44SMiaohe Lin * stealing for a non-movable allocation, make sure 2140fa599c44SMiaohe Lin * we finish compacting the current pageblock first 2141fa599c44SMiaohe Lin * (which is assured by the above migrate_pfn align 2142fa599c44SMiaohe Lin * check) so it is as free as possible and we won't 2143fa599c44SMiaohe Lin * have to steal another one soon. 2144baf6a9a1SVlastimil Babka */ 2145baf6a9a1SVlastimil Babka return COMPACT_SUCCESS; 2146baf6a9a1SVlastimil Babka } 2147baf6a9a1SVlastimil Babka 2148facdaa91SNitin Gupta out: 2149cb2dcaf0SMel Gorman if (cc->contended || fatal_signal_pending(current)) 2150cb2dcaf0SMel Gorman ret = COMPACT_CONTENDED; 2151cb2dcaf0SMel Gorman 2152cb2dcaf0SMel Gorman return ret; 2153837d026dSJoonsoo Kim } 2154837d026dSJoonsoo Kim 215540cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc) 2156837d026dSJoonsoo Kim { 2157837d026dSJoonsoo Kim int ret; 2158837d026dSJoonsoo Kim 215940cacbcbSMel Gorman ret = __compact_finished(cc); 216040cacbcbSMel Gorman trace_mm_compaction_finished(cc->zone, cc->order, ret); 2161837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 2162837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 2163837d026dSJoonsoo Kim 2164837d026dSJoonsoo Kim return ret; 2165748446bbSMel Gorman } 2166748446bbSMel Gorman 2167ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 2168c603844bSMel Gorman unsigned int alloc_flags, 216997a225e6SJoonsoo Kim int highest_zoneidx, 217086a294a8SMichal Hocko unsigned long wmark_target) 21713e7d3449SMel Gorman { 21723e7d3449SMel Gorman unsigned long watermark; 21733e7d3449SMel Gorman 217421c527a3SYaowei Bai if (is_via_compact_memory(order)) 21753957c776SMichal Hocko return COMPACT_CONTINUE; 21763957c776SMichal Hocko 2177a9214443SMel Gorman watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); 2178ebff3980SVlastimil Babka /* 2179ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 2180ebff3980SVlastimil Babka * should be no need for compaction at all. 2181ebff3980SVlastimil Babka */ 218297a225e6SJoonsoo Kim if (zone_watermark_ok(zone, order, watermark, highest_zoneidx, 2183ebff3980SVlastimil Babka alloc_flags)) 2184cf378319SVlastimil Babka return COMPACT_SUCCESS; 2185ebff3980SVlastimil Babka 21863957c776SMichal Hocko /* 21879861a62cSVlastimil Babka * Watermarks for order-0 must be met for compaction to be able to 2188984fdba6SVlastimil Babka * isolate free pages for migration targets. This means that the 2189984fdba6SVlastimil Babka * watermark and alloc_flags have to match, or be more pessimistic than 2190984fdba6SVlastimil Babka * the check in __isolate_free_page(). We don't use the direct 2191984fdba6SVlastimil Babka * compactor's alloc_flags, as they are not relevant for freepage 219297a225e6SJoonsoo Kim * isolation. We however do use the direct compactor's highest_zoneidx 219397a225e6SJoonsoo Kim * to skip over zones where lowmem reserves would prevent allocation 219497a225e6SJoonsoo Kim * even if compaction succeeds. 21958348faf9SVlastimil Babka * For costly orders, we require low watermark instead of min for 21968348faf9SVlastimil Babka * compaction to proceed to increase its chances. 2197d883c6cfSJoonsoo Kim * ALLOC_CMA is used, as pages in CMA pageblocks are considered 2198d883c6cfSJoonsoo Kim * suitable migration targets 21993e7d3449SMel Gorman */ 22008348faf9SVlastimil Babka watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? 22018348faf9SVlastimil Babka low_wmark_pages(zone) : min_wmark_pages(zone); 22028348faf9SVlastimil Babka watermark += compact_gap(order); 220397a225e6SJoonsoo Kim if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, 2204d883c6cfSJoonsoo Kim ALLOC_CMA, wmark_target)) 22053e7d3449SMel Gorman return COMPACT_SKIPPED; 22063e7d3449SMel Gorman 2207cc5c9f09SVlastimil Babka return COMPACT_CONTINUE; 2208cc5c9f09SVlastimil Babka } 2209cc5c9f09SVlastimil Babka 22102b1a20c3SHui Su /* 22112b1a20c3SHui Su * compaction_suitable: Is this suitable to run compaction on this zone now? 22122b1a20c3SHui Su * Returns 22132b1a20c3SHui Su * COMPACT_SKIPPED - If there are too few free pages for compaction 22142b1a20c3SHui Su * COMPACT_SUCCESS - If the allocation would succeed without compaction 22152b1a20c3SHui Su * COMPACT_CONTINUE - If compaction should run now 22162b1a20c3SHui Su */ 2217cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order, 2218cc5c9f09SVlastimil Babka unsigned int alloc_flags, 221997a225e6SJoonsoo Kim int highest_zoneidx) 2220cc5c9f09SVlastimil Babka { 2221cc5c9f09SVlastimil Babka enum compact_result ret; 2222cc5c9f09SVlastimil Babka int fragindex; 2223cc5c9f09SVlastimil Babka 222497a225e6SJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx, 2225cc5c9f09SVlastimil Babka zone_page_state(zone, NR_FREE_PAGES)); 22263e7d3449SMel Gorman /* 22273e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 22283e7d3449SMel Gorman * low memory or external fragmentation 22293e7d3449SMel Gorman * 2230ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 2231ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 22323e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 22333e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 22343e7d3449SMel Gorman * 223520311420SVlastimil Babka * Only compact if a failure would be due to fragmentation. Also 223620311420SVlastimil Babka * ignore fragindex for non-costly orders where the alternative to 223720311420SVlastimil Babka * a successful reclaim/compaction is OOM. Fragindex and the 223820311420SVlastimil Babka * vm.extfrag_threshold sysctl is meant as a heuristic to prevent 223920311420SVlastimil Babka * excessive compaction for costly orders, but it should not be at the 224020311420SVlastimil Babka * expense of system stability. 22413e7d3449SMel Gorman */ 224220311420SVlastimil Babka if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { 22433e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 22443e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 2245cc5c9f09SVlastimil Babka ret = COMPACT_NOT_SUITABLE_ZONE; 22463e7d3449SMel Gorman } 22473e7d3449SMel Gorman 2248837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 2249837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 2250837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 2251837d026dSJoonsoo Kim 2252837d026dSJoonsoo Kim return ret; 2253837d026dSJoonsoo Kim } 2254837d026dSJoonsoo Kim 225586a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 225686a294a8SMichal Hocko int alloc_flags) 225786a294a8SMichal Hocko { 225886a294a8SMichal Hocko struct zone *zone; 225986a294a8SMichal Hocko struct zoneref *z; 226086a294a8SMichal Hocko 226186a294a8SMichal Hocko /* 226286a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 226386a294a8SMichal Hocko * retrying the reclaim. 226486a294a8SMichal Hocko */ 226597a225e6SJoonsoo Kim for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 226697a225e6SJoonsoo Kim ac->highest_zoneidx, ac->nodemask) { 226786a294a8SMichal Hocko unsigned long available; 226886a294a8SMichal Hocko enum compact_result compact_result; 226986a294a8SMichal Hocko 227086a294a8SMichal Hocko /* 227186a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 227286a294a8SMichal Hocko * want to trash just for a single high order allocation which 227386a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 227486a294a8SMichal Hocko * is happy about the watermark check. 227586a294a8SMichal Hocko */ 22765a1c84b4SMel Gorman available = zone_reclaimable_pages(zone) / order; 227786a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 227886a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 227997a225e6SJoonsoo Kim ac->highest_zoneidx, available); 2280cff387d6SMiaohe Lin if (compact_result == COMPACT_CONTINUE) 228186a294a8SMichal Hocko return true; 228286a294a8SMichal Hocko } 228386a294a8SMichal Hocko 228486a294a8SMichal Hocko return false; 228586a294a8SMichal Hocko } 228686a294a8SMichal Hocko 22875e1f0f09SMel Gorman static enum compact_result 22885e1f0f09SMel Gorman compact_zone(struct compact_control *cc, struct capture_control *capc) 2289748446bbSMel Gorman { 2290ea7ab982SMichal Hocko enum compact_result ret; 229140cacbcbSMel Gorman unsigned long start_pfn = cc->zone->zone_start_pfn; 229240cacbcbSMel Gorman unsigned long end_pfn = zone_end_pfn(cc->zone); 2293566e54e1SMel Gorman unsigned long last_migrated_pfn; 2294e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 22958854c55fSMel Gorman bool update_cached; 229684b328aaSBaolin Wang unsigned int nr_succeeded = 0; 2297748446bbSMel Gorman 2298a94b5252SYafang Shao /* 2299a94b5252SYafang Shao * These counters track activities during zone compaction. Initialize 2300a94b5252SYafang Shao * them before compacting a new zone. 2301a94b5252SYafang Shao */ 2302a94b5252SYafang Shao cc->total_migrate_scanned = 0; 2303a94b5252SYafang Shao cc->total_free_scanned = 0; 2304a94b5252SYafang Shao cc->nr_migratepages = 0; 2305a94b5252SYafang Shao cc->nr_freepages = 0; 2306a94b5252SYafang Shao INIT_LIST_HEAD(&cc->freepages); 2307a94b5252SYafang Shao INIT_LIST_HEAD(&cc->migratepages); 2308a94b5252SYafang Shao 230901c0bfe0SWei Yang cc->migratetype = gfp_migratetype(cc->gfp_mask); 231040cacbcbSMel Gorman ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, 231197a225e6SJoonsoo Kim cc->highest_zoneidx); 23123e7d3449SMel Gorman /* Compaction is likely to fail */ 2313cf378319SVlastimil Babka if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) 23143e7d3449SMel Gorman return ret; 2315c46649deSMichal Hocko 2316c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 2317c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 23183e7d3449SMel Gorman 2319c89511abSMel Gorman /* 2320d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 2321accf6242SVlastimil Babka * is about to be retried after being deferred. 2322d3132e4bSVlastimil Babka */ 232340cacbcbSMel Gorman if (compaction_restarting(cc->zone, cc->order)) 232440cacbcbSMel Gorman __reset_isolation_suitable(cc->zone); 2325d3132e4bSVlastimil Babka 2326d3132e4bSVlastimil Babka /* 2327c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 232806ed2998SVlastimil Babka * information on where the scanners should start (unless we explicitly 232906ed2998SVlastimil Babka * want to compact the whole zone), but check that it is initialised 233006ed2998SVlastimil Babka * by ensuring the values are within zone boundaries. 2331c89511abSMel Gorman */ 233270b44595SMel Gorman cc->fast_start_pfn = 0; 233306ed2998SVlastimil Babka if (cc->whole_zone) { 233406ed2998SVlastimil Babka cc->migrate_pfn = start_pfn; 233506ed2998SVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 233606ed2998SVlastimil Babka } else { 233740cacbcbSMel Gorman cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; 233840cacbcbSMel Gorman cc->free_pfn = cc->zone->compact_cached_free_pfn; 2339623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 234006b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 234140cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = cc->free_pfn; 2342c89511abSMel Gorman } 2343623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 2344c89511abSMel Gorman cc->migrate_pfn = start_pfn; 234540cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 234640cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 2347c89511abSMel Gorman } 2348c8f7de0bSMichal Hocko 2349e332f741SMel Gorman if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) 2350c8f7de0bSMichal Hocko cc->whole_zone = true; 235106ed2998SVlastimil Babka } 2352c8f7de0bSMichal Hocko 2353566e54e1SMel Gorman last_migrated_pfn = 0; 2354748446bbSMel Gorman 23558854c55fSMel Gorman /* 23568854c55fSMel Gorman * Migrate has separate cached PFNs for ASYNC and SYNC* migration on 23578854c55fSMel Gorman * the basis that some migrations will fail in ASYNC mode. However, 23588854c55fSMel Gorman * if the cached PFNs match and pageblocks are skipped due to having 23598854c55fSMel Gorman * no isolation candidates, then the sync state does not matter. 23608854c55fSMel Gorman * Until a pageblock with isolation candidates is found, keep the 23618854c55fSMel Gorman * cached PFNs in sync to avoid revisiting the same blocks. 23628854c55fSMel Gorman */ 23638854c55fSMel Gorman update_cached = !sync && 23648854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; 23658854c55fSMel Gorman 2366abd4349fSBaolin Wang trace_mm_compaction_begin(cc, start_pfn, end_pfn, sync); 23670eb927c0SMel Gorman 2368361a2a22SMinchan Kim /* lru_add_drain_all could be expensive with involving other CPUs */ 2369361a2a22SMinchan Kim lru_add_drain(); 2370748446bbSMel Gorman 237140cacbcbSMel Gorman while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { 23729d502c1cSMinchan Kim int err; 237319d3cf9dSYanfei Xu unsigned long iteration_start_pfn = cc->migrate_pfn; 2374748446bbSMel Gorman 2375804d3121SMel Gorman /* 2376804d3121SMel Gorman * Avoid multiple rescans which can happen if a page cannot be 2377804d3121SMel Gorman * isolated (dirty/writeback in async mode) or if the migrated 2378804d3121SMel Gorman * pages are being allocated before the pageblock is cleared. 2379804d3121SMel Gorman * The first rescan will capture the entire pageblock for 2380804d3121SMel Gorman * migration. If it fails, it'll be marked skip and scanning 2381804d3121SMel Gorman * will proceed as normal. 2382804d3121SMel Gorman */ 2383804d3121SMel Gorman cc->rescan = false; 2384804d3121SMel Gorman if (pageblock_start_pfn(last_migrated_pfn) == 238519d3cf9dSYanfei Xu pageblock_start_pfn(iteration_start_pfn)) { 2386804d3121SMel Gorman cc->rescan = true; 2387804d3121SMel Gorman } 2388804d3121SMel Gorman 238932aaf055SPengfei Li switch (isolate_migratepages(cc)) { 2390f9e35b3bSMel Gorman case ISOLATE_ABORT: 23912d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 23925733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 2393e64c5237SShaohua Li cc->nr_migratepages = 0; 2394f9e35b3bSMel Gorman goto out; 2395f9e35b3bSMel Gorman case ISOLATE_NONE: 23968854c55fSMel Gorman if (update_cached) { 23978854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = 23988854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0]; 23998854c55fSMel Gorman } 24008854c55fSMel Gorman 2401fdaf7f5cSVlastimil Babka /* 2402fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 2403fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 2404fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 2405fdaf7f5cSVlastimil Babka */ 2406fdaf7f5cSVlastimil Babka goto check_drain; 2407f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 24088854c55fSMel Gorman update_cached = false; 240919d3cf9dSYanfei Xu last_migrated_pfn = iteration_start_pfn; 2410f9e35b3bSMel Gorman } 2411748446bbSMel Gorman 2412d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 2413e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 241484b328aaSBaolin Wang MR_COMPACTION, &nr_succeeded); 2415748446bbSMel Gorman 2416abd4349fSBaolin Wang trace_mm_compaction_migratepages(cc, nr_succeeded); 2417748446bbSMel Gorman 2418f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 2419f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 24209d502c1cSMinchan Kim if (err) { 24215733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 24227ed695e0SVlastimil Babka /* 24237ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 24247ed695e0SVlastimil Babka * and we want compact_finished() to detect it 24257ed695e0SVlastimil Babka */ 2426f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 24272d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 24284bf2bba3SDavid Rientjes goto out; 2429748446bbSMel Gorman } 2430fdd048e1SVlastimil Babka /* 2431fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 2432fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 2433fdd048e1SVlastimil Babka */ 2434fdd048e1SVlastimil Babka if (cc->direct_compaction && 2435fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 2436fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 2437fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 2438fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 2439566e54e1SMel Gorman last_migrated_pfn = 0; 2440fdd048e1SVlastimil Babka } 24414bf2bba3SDavid Rientjes } 2442fdaf7f5cSVlastimil Babka 2443fdaf7f5cSVlastimil Babka check_drain: 2444fdaf7f5cSVlastimil Babka /* 2445fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 2446fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 2447fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 2448fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 2449fdaf7f5cSVlastimil Babka * would succeed. 2450fdaf7f5cSVlastimil Babka */ 2451566e54e1SMel Gorman if (cc->order > 0 && last_migrated_pfn) { 2452fdaf7f5cSVlastimil Babka unsigned long current_block_start = 245306b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 2454fdaf7f5cSVlastimil Babka 2455566e54e1SMel Gorman if (last_migrated_pfn < current_block_start) { 2456b01b2141SIngo Molnar lru_add_drain_cpu_zone(cc->zone); 2457fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 2458566e54e1SMel Gorman last_migrated_pfn = 0; 2459fdaf7f5cSVlastimil Babka } 2460fdaf7f5cSVlastimil Babka } 2461fdaf7f5cSVlastimil Babka 24625e1f0f09SMel Gorman /* Stop if a page has been captured */ 24635e1f0f09SMel Gorman if (capc && capc->page) { 24645e1f0f09SMel Gorman ret = COMPACT_SUCCESS; 24655e1f0f09SMel Gorman break; 24665e1f0f09SMel Gorman } 2467748446bbSMel Gorman } 2468748446bbSMel Gorman 2469f9e35b3bSMel Gorman out: 24706bace090SVlastimil Babka /* 24716bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 24726bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 24736bace090SVlastimil Babka */ 24746bace090SVlastimil Babka if (cc->nr_freepages > 0) { 24756bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 24766bace090SVlastimil Babka 24776bace090SVlastimil Babka cc->nr_freepages = 0; 24786bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 24796bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 248006b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 24816bace090SVlastimil Babka /* 24826bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 24836bace090SVlastimil Babka * already reset to zone end in compact_finished() 24846bace090SVlastimil Babka */ 248540cacbcbSMel Gorman if (free_pfn > cc->zone->compact_cached_free_pfn) 248640cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = free_pfn; 24876bace090SVlastimil Babka } 2488748446bbSMel Gorman 24897f354a54SDavid Rientjes count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); 24907f354a54SDavid Rientjes count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); 24917f354a54SDavid Rientjes 2492abd4349fSBaolin Wang trace_mm_compaction_end(cc, start_pfn, end_pfn, sync, ret); 24930eb927c0SMel Gorman 2494748446bbSMel Gorman return ret; 2495748446bbSMel Gorman } 249676ab0f53SMel Gorman 2497ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 2498c3486f53SVlastimil Babka gfp_t gfp_mask, enum compact_priority prio, 249997a225e6SJoonsoo Kim unsigned int alloc_flags, int highest_zoneidx, 25005e1f0f09SMel Gorman struct page **capture) 250156de7263SMel Gorman { 2502ea7ab982SMichal Hocko enum compact_result ret; 250356de7263SMel Gorman struct compact_control cc = { 250456de7263SMel Gorman .order = order, 2505dbe2d4e4SMel Gorman .search_order = order, 25066d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 250756de7263SMel Gorman .zone = zone, 2508a5508cd8SVlastimil Babka .mode = (prio == COMPACT_PRIO_ASYNC) ? 2509a5508cd8SVlastimil Babka MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, 2510ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 251197a225e6SJoonsoo Kim .highest_zoneidx = highest_zoneidx, 2512accf6242SVlastimil Babka .direct_compaction = true, 2513a8e025e5SVlastimil Babka .whole_zone = (prio == MIN_COMPACT_PRIORITY), 25149f7e3387SVlastimil Babka .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), 25159f7e3387SVlastimil Babka .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) 251656de7263SMel Gorman }; 25175e1f0f09SMel Gorman struct capture_control capc = { 25185e1f0f09SMel Gorman .cc = &cc, 25195e1f0f09SMel Gorman .page = NULL, 25205e1f0f09SMel Gorman }; 25215e1f0f09SMel Gorman 2522b9e20f0dSVlastimil Babka /* 2523b9e20f0dSVlastimil Babka * Make sure the structs are really initialized before we expose the 2524b9e20f0dSVlastimil Babka * capture control, in case we are interrupted and the interrupt handler 2525b9e20f0dSVlastimil Babka * frees a page. 2526b9e20f0dSVlastimil Babka */ 2527b9e20f0dSVlastimil Babka barrier(); 2528b9e20f0dSVlastimil Babka WRITE_ONCE(current->capture_control, &capc); 252956de7263SMel Gorman 25305e1f0f09SMel Gorman ret = compact_zone(&cc, &capc); 2531e64c5237SShaohua Li 2532e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 2533e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 2534e64c5237SShaohua Li 2535b9e20f0dSVlastimil Babka /* 2536b9e20f0dSVlastimil Babka * Make sure we hide capture control first before we read the captured 2537b9e20f0dSVlastimil Babka * page pointer, otherwise an interrupt could free and capture a page 2538b9e20f0dSVlastimil Babka * and we would leak it. 2539b9e20f0dSVlastimil Babka */ 2540b9e20f0dSVlastimil Babka WRITE_ONCE(current->capture_control, NULL); 2541b9e20f0dSVlastimil Babka *capture = READ_ONCE(capc.page); 254206dac2f4SCharan Teja Reddy /* 254306dac2f4SCharan Teja Reddy * Technically, it is also possible that compaction is skipped but 254406dac2f4SCharan Teja Reddy * the page is still captured out of luck(IRQ came and freed the page). 254506dac2f4SCharan Teja Reddy * Returning COMPACT_SUCCESS in such cases helps in properly accounting 254606dac2f4SCharan Teja Reddy * the COMPACT[STALL|FAIL] when compaction is skipped. 254706dac2f4SCharan Teja Reddy */ 254806dac2f4SCharan Teja Reddy if (*capture) 254906dac2f4SCharan Teja Reddy ret = COMPACT_SUCCESS; 25505e1f0f09SMel Gorman 2551e64c5237SShaohua Li return ret; 255256de7263SMel Gorman } 255356de7263SMel Gorman 25545e771905SMel Gorman int sysctl_extfrag_threshold = 500; 25555e771905SMel Gorman 255656de7263SMel Gorman /** 255756de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 255856de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 25591a6d53a1SVlastimil Babka * @order: The order of the current allocation 25601a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 25611a6d53a1SVlastimil Babka * @ac: The context of current allocation 2562112d2d29SYang Shi * @prio: Determines how hard direct compaction should try to succeed 25636467552cSVlastimil Babka * @capture: Pointer to free page created by compaction will be stored here 256456de7263SMel Gorman * 256556de7263SMel Gorman * This is the main entry point for direct page compaction. 256656de7263SMel Gorman */ 2567ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 2568c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 25695e1f0f09SMel Gorman enum compact_priority prio, struct page **capture) 257056de7263SMel Gorman { 2571fe573327SVasily Averin int may_perform_io = (__force int)(gfp_mask & __GFP_IO); 257256de7263SMel Gorman struct zoneref *z; 257356de7263SMel Gorman struct zone *zone; 25741d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 257556de7263SMel Gorman 257673e64c51SMichal Hocko /* 257773e64c51SMichal Hocko * Check if the GFP flags allow compaction - GFP_NOIO is really 257873e64c51SMichal Hocko * tricky context because the migration might require IO 257973e64c51SMichal Hocko */ 258073e64c51SMichal Hocko if (!may_perform_io) 258153853e2dSVlastimil Babka return COMPACT_SKIPPED; 258256de7263SMel Gorman 2583a5508cd8SVlastimil Babka trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); 2584837d026dSJoonsoo Kim 258556de7263SMel Gorman /* Compact each zone in the list */ 258697a225e6SJoonsoo Kim for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 258797a225e6SJoonsoo Kim ac->highest_zoneidx, ac->nodemask) { 2588ea7ab982SMichal Hocko enum compact_result status; 258956de7263SMel Gorman 2590a8e025e5SVlastimil Babka if (prio > MIN_COMPACT_PRIORITY 2591a8e025e5SVlastimil Babka && compaction_deferred(zone, order)) { 25921d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 259353853e2dSVlastimil Babka continue; 25941d4746d3SMichal Hocko } 259553853e2dSVlastimil Babka 2596a5508cd8SVlastimil Babka status = compact_zone_order(zone, order, gfp_mask, prio, 259797a225e6SJoonsoo Kim alloc_flags, ac->highest_zoneidx, capture); 259856de7263SMel Gorman rc = max(status, rc); 259956de7263SMel Gorman 26007ceb009aSVlastimil Babka /* The allocation should succeed, stop compacting */ 26017ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 260253853e2dSVlastimil Babka /* 260353853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 260453853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 260553853e2dSVlastimil Babka * will repeat this with true if allocation indeed 260653853e2dSVlastimil Babka * succeeds in this zone. 260753853e2dSVlastimil Babka */ 260853853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 26091f9efdefSVlastimil Babka 2610c3486f53SVlastimil Babka break; 26111f9efdefSVlastimil Babka } 26121f9efdefSVlastimil Babka 2613a5508cd8SVlastimil Babka if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || 2614c3486f53SVlastimil Babka status == COMPACT_PARTIAL_SKIPPED)) 261553853e2dSVlastimil Babka /* 261653853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 261753853e2dSVlastimil Babka * so we defer compaction there. If it ends up 261853853e2dSVlastimil Babka * succeeding after all, it will be reset. 261953853e2dSVlastimil Babka */ 262053853e2dSVlastimil Babka defer_compaction(zone, order); 26211f9efdefSVlastimil Babka 26221f9efdefSVlastimil Babka /* 26231f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 26241f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 2625c3486f53SVlastimil Babka * case do not try further zones 26261f9efdefSVlastimil Babka */ 2627c3486f53SVlastimil Babka if ((prio == COMPACT_PRIO_ASYNC && need_resched()) 2628c3486f53SVlastimil Babka || fatal_signal_pending(current)) 26291f9efdefSVlastimil Babka break; 26301f9efdefSVlastimil Babka } 26311f9efdefSVlastimil Babka 263256de7263SMel Gorman return rc; 263356de7263SMel Gorman } 263456de7263SMel Gorman 2635facdaa91SNitin Gupta /* 2636facdaa91SNitin Gupta * Compact all zones within a node till each zone's fragmentation score 2637facdaa91SNitin Gupta * reaches within proactive compaction thresholds (as determined by the 2638facdaa91SNitin Gupta * proactiveness tunable). 2639facdaa91SNitin Gupta * 2640facdaa91SNitin Gupta * It is possible that the function returns before reaching score targets 2641facdaa91SNitin Gupta * due to various back-off conditions, such as, contention on per-node or 2642facdaa91SNitin Gupta * per-zone locks. 2643facdaa91SNitin Gupta */ 2644facdaa91SNitin Gupta static void proactive_compact_node(pg_data_t *pgdat) 2645facdaa91SNitin Gupta { 2646facdaa91SNitin Gupta int zoneid; 2647facdaa91SNitin Gupta struct zone *zone; 2648facdaa91SNitin Gupta struct compact_control cc = { 2649facdaa91SNitin Gupta .order = -1, 2650facdaa91SNitin Gupta .mode = MIGRATE_SYNC_LIGHT, 2651facdaa91SNitin Gupta .ignore_skip_hint = true, 2652facdaa91SNitin Gupta .whole_zone = true, 2653facdaa91SNitin Gupta .gfp_mask = GFP_KERNEL, 2654facdaa91SNitin Gupta .proactive_compaction = true, 2655facdaa91SNitin Gupta }; 2656facdaa91SNitin Gupta 2657facdaa91SNitin Gupta for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2658facdaa91SNitin Gupta zone = &pgdat->node_zones[zoneid]; 2659facdaa91SNitin Gupta if (!populated_zone(zone)) 2660facdaa91SNitin Gupta continue; 2661facdaa91SNitin Gupta 2662facdaa91SNitin Gupta cc.zone = zone; 2663facdaa91SNitin Gupta 2664facdaa91SNitin Gupta compact_zone(&cc, NULL); 2665facdaa91SNitin Gupta 2666facdaa91SNitin Gupta VM_BUG_ON(!list_empty(&cc.freepages)); 2667facdaa91SNitin Gupta VM_BUG_ON(!list_empty(&cc.migratepages)); 2668facdaa91SNitin Gupta } 2669facdaa91SNitin Gupta } 267056de7263SMel Gorman 267176ab0f53SMel Gorman /* Compact all zones within a node */ 26727103f16dSAndrew Morton static void compact_node(int nid) 26737be62de9SRik van Riel { 2674791cae96SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2675791cae96SVlastimil Babka int zoneid; 2676791cae96SVlastimil Babka struct zone *zone; 26777be62de9SRik van Riel struct compact_control cc = { 26787be62de9SRik van Riel .order = -1, 2679e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 268091ca9186SDavid Rientjes .ignore_skip_hint = true, 268106ed2998SVlastimil Babka .whole_zone = true, 268273e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 26837be62de9SRik van Riel }; 26847be62de9SRik van Riel 2685791cae96SVlastimil Babka 2686791cae96SVlastimil Babka for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2687791cae96SVlastimil Babka 2688791cae96SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2689791cae96SVlastimil Babka if (!populated_zone(zone)) 2690791cae96SVlastimil Babka continue; 2691791cae96SVlastimil Babka 2692791cae96SVlastimil Babka cc.zone = zone; 2693791cae96SVlastimil Babka 26945e1f0f09SMel Gorman compact_zone(&cc, NULL); 2695791cae96SVlastimil Babka 2696791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2697791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2698791cae96SVlastimil Babka } 26997be62de9SRik van Riel } 27007be62de9SRik van Riel 270176ab0f53SMel Gorman /* Compact all nodes in the system */ 27027964c06dSJason Liu static void compact_nodes(void) 270376ab0f53SMel Gorman { 270476ab0f53SMel Gorman int nid; 270576ab0f53SMel Gorman 27068575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 27078575ec29SHugh Dickins lru_add_drain_all(); 27088575ec29SHugh Dickins 270976ab0f53SMel Gorman for_each_online_node(nid) 271076ab0f53SMel Gorman compact_node(nid); 271176ab0f53SMel Gorman } 271276ab0f53SMel Gorman 2713fec4eb2cSYaowei Bai /* 2714facdaa91SNitin Gupta * Tunable for proactive compaction. It determines how 2715facdaa91SNitin Gupta * aggressively the kernel should compact memory in the 2716facdaa91SNitin Gupta * background. It takes values in the range [0, 100]. 2717facdaa91SNitin Gupta */ 2718d34c0a75SNitin Gupta unsigned int __read_mostly sysctl_compaction_proactiveness = 20; 2719facdaa91SNitin Gupta 272065d759c8SCharan Teja Reddy int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write, 272165d759c8SCharan Teja Reddy void *buffer, size_t *length, loff_t *ppos) 272265d759c8SCharan Teja Reddy { 272365d759c8SCharan Teja Reddy int rc, nid; 272465d759c8SCharan Teja Reddy 272565d759c8SCharan Teja Reddy rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 272665d759c8SCharan Teja Reddy if (rc) 272765d759c8SCharan Teja Reddy return rc; 272865d759c8SCharan Teja Reddy 272965d759c8SCharan Teja Reddy if (write && sysctl_compaction_proactiveness) { 273065d759c8SCharan Teja Reddy for_each_online_node(nid) { 273165d759c8SCharan Teja Reddy pg_data_t *pgdat = NODE_DATA(nid); 273265d759c8SCharan Teja Reddy 273365d759c8SCharan Teja Reddy if (pgdat->proactive_compact_trigger) 273465d759c8SCharan Teja Reddy continue; 273565d759c8SCharan Teja Reddy 273665d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger = true; 273765d759c8SCharan Teja Reddy wake_up_interruptible(&pgdat->kcompactd_wait); 273865d759c8SCharan Teja Reddy } 273965d759c8SCharan Teja Reddy } 274065d759c8SCharan Teja Reddy 274165d759c8SCharan Teja Reddy return 0; 274265d759c8SCharan Teja Reddy } 274365d759c8SCharan Teja Reddy 2744facdaa91SNitin Gupta /* 2745fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 2746fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 2747fec4eb2cSYaowei Bai */ 274876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 274932927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 275076ab0f53SMel Gorman { 275176ab0f53SMel Gorman if (write) 27527964c06dSJason Liu compact_nodes(); 275376ab0f53SMel Gorman 275476ab0f53SMel Gorman return 0; 275576ab0f53SMel Gorman } 2756ed4a6d7fSMel Gorman 2757ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 275817adb230SYueHaibing static ssize_t compact_store(struct device *dev, 275910fbcf4cSKay Sievers struct device_attribute *attr, 2760ed4a6d7fSMel Gorman const char *buf, size_t count) 2761ed4a6d7fSMel Gorman { 27628575ec29SHugh Dickins int nid = dev->id; 27638575ec29SHugh Dickins 27648575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 27658575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 27668575ec29SHugh Dickins lru_add_drain_all(); 27678575ec29SHugh Dickins 27688575ec29SHugh Dickins compact_node(nid); 27698575ec29SHugh Dickins } 2770ed4a6d7fSMel Gorman 2771ed4a6d7fSMel Gorman return count; 2772ed4a6d7fSMel Gorman } 277317adb230SYueHaibing static DEVICE_ATTR_WO(compact); 2774ed4a6d7fSMel Gorman 2775ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 2776ed4a6d7fSMel Gorman { 277710fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 2778ed4a6d7fSMel Gorman } 2779ed4a6d7fSMel Gorman 2780ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 2781ed4a6d7fSMel Gorman { 278210fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 2783ed4a6d7fSMel Gorman } 2784ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 2785ff9543fdSMichal Nazarewicz 2786698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 2787698b1b30SVlastimil Babka { 278865d759c8SCharan Teja Reddy return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || 278965d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger; 2790698b1b30SVlastimil Babka } 2791698b1b30SVlastimil Babka 2792698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 2793698b1b30SVlastimil Babka { 2794698b1b30SVlastimil Babka int zoneid; 2795698b1b30SVlastimil Babka struct zone *zone; 279697a225e6SJoonsoo Kim enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; 2797698b1b30SVlastimil Babka 279897a225e6SJoonsoo Kim for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { 2799698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2800698b1b30SVlastimil Babka 2801698b1b30SVlastimil Babka if (!populated_zone(zone)) 2802698b1b30SVlastimil Babka continue; 2803698b1b30SVlastimil Babka 2804698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 280597a225e6SJoonsoo Kim highest_zoneidx) == COMPACT_CONTINUE) 2806698b1b30SVlastimil Babka return true; 2807698b1b30SVlastimil Babka } 2808698b1b30SVlastimil Babka 2809698b1b30SVlastimil Babka return false; 2810698b1b30SVlastimil Babka } 2811698b1b30SVlastimil Babka 2812698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 2813698b1b30SVlastimil Babka { 2814698b1b30SVlastimil Babka /* 2815698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 2816698b1b30SVlastimil Babka * order is allocatable. 2817698b1b30SVlastimil Babka */ 2818698b1b30SVlastimil Babka int zoneid; 2819698b1b30SVlastimil Babka struct zone *zone; 2820698b1b30SVlastimil Babka struct compact_control cc = { 2821698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 2822dbe2d4e4SMel Gorman .search_order = pgdat->kcompactd_max_order, 282397a225e6SJoonsoo Kim .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, 2824698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 2825a0647dc9SDavid Rientjes .ignore_skip_hint = false, 282673e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 2827698b1b30SVlastimil Babka }; 2828698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 282997a225e6SJoonsoo Kim cc.highest_zoneidx); 28307f354a54SDavid Rientjes count_compact_event(KCOMPACTD_WAKE); 2831698b1b30SVlastimil Babka 283297a225e6SJoonsoo Kim for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { 2833698b1b30SVlastimil Babka int status; 2834698b1b30SVlastimil Babka 2835698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2836698b1b30SVlastimil Babka if (!populated_zone(zone)) 2837698b1b30SVlastimil Babka continue; 2838698b1b30SVlastimil Babka 2839698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 2840698b1b30SVlastimil Babka continue; 2841698b1b30SVlastimil Babka 2842698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 2843698b1b30SVlastimil Babka COMPACT_CONTINUE) 2844698b1b30SVlastimil Babka continue; 2845698b1b30SVlastimil Babka 2846172400c6SVlastimil Babka if (kthread_should_stop()) 2847172400c6SVlastimil Babka return; 2848a94b5252SYafang Shao 2849a94b5252SYafang Shao cc.zone = zone; 28505e1f0f09SMel Gorman status = compact_zone(&cc, NULL); 2851698b1b30SVlastimil Babka 28527ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 2853698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 2854c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 2855698b1b30SVlastimil Babka /* 2856bc3106b2SDavid Rientjes * Buddy pages may become stranded on pcps that could 2857bc3106b2SDavid Rientjes * otherwise coalesce on the zone's free area for 2858bc3106b2SDavid Rientjes * order >= cc.order. This is ratelimited by the 2859bc3106b2SDavid Rientjes * upcoming deferral. 2860bc3106b2SDavid Rientjes */ 2861bc3106b2SDavid Rientjes drain_all_pages(zone); 2862bc3106b2SDavid Rientjes 2863bc3106b2SDavid Rientjes /* 2864698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 2865698b1b30SVlastimil Babka * sync direct compaction does. 2866698b1b30SVlastimil Babka */ 2867698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 2868698b1b30SVlastimil Babka } 2869698b1b30SVlastimil Babka 28707f354a54SDavid Rientjes count_compact_events(KCOMPACTD_MIGRATE_SCANNED, 28717f354a54SDavid Rientjes cc.total_migrate_scanned); 28727f354a54SDavid Rientjes count_compact_events(KCOMPACTD_FREE_SCANNED, 28737f354a54SDavid Rientjes cc.total_free_scanned); 28747f354a54SDavid Rientjes 2875698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2876698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2877698b1b30SVlastimil Babka } 2878698b1b30SVlastimil Babka 2879698b1b30SVlastimil Babka /* 2880698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 288197a225e6SJoonsoo Kim * the requested order/highest_zoneidx in case it was higher/tighter 288297a225e6SJoonsoo Kim * than our current ones 2883698b1b30SVlastimil Babka */ 2884698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 2885698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 288697a225e6SJoonsoo Kim if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) 288797a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; 2888698b1b30SVlastimil Babka } 2889698b1b30SVlastimil Babka 289097a225e6SJoonsoo Kim void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) 2891698b1b30SVlastimil Babka { 2892698b1b30SVlastimil Babka if (!order) 2893698b1b30SVlastimil Babka return; 2894698b1b30SVlastimil Babka 2895698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 2896698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 2897698b1b30SVlastimil Babka 289897a225e6SJoonsoo Kim if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) 289997a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = highest_zoneidx; 2900698b1b30SVlastimil Babka 29016818600fSDavidlohr Bueso /* 29026818600fSDavidlohr Bueso * Pairs with implicit barrier in wait_event_freezable() 29036818600fSDavidlohr Bueso * such that wakeups are not missed. 29046818600fSDavidlohr Bueso */ 29056818600fSDavidlohr Bueso if (!wq_has_sleeper(&pgdat->kcompactd_wait)) 2906698b1b30SVlastimil Babka return; 2907698b1b30SVlastimil Babka 2908698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 2909698b1b30SVlastimil Babka return; 2910698b1b30SVlastimil Babka 2911698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 291297a225e6SJoonsoo Kim highest_zoneidx); 2913698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 2914698b1b30SVlastimil Babka } 2915698b1b30SVlastimil Babka 2916698b1b30SVlastimil Babka /* 2917698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 2918698b1b30SVlastimil Babka * from the init process. 2919698b1b30SVlastimil Babka */ 2920698b1b30SVlastimil Babka static int kcompactd(void *p) 2921698b1b30SVlastimil Babka { 2922698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t *)p; 2923698b1b30SVlastimil Babka struct task_struct *tsk = current; 2924e1e92bfaSCharan Teja Reddy long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC); 2925e1e92bfaSCharan Teja Reddy long timeout = default_timeout; 2926698b1b30SVlastimil Babka 2927698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 2928698b1b30SVlastimil Babka 2929698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 2930698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 2931698b1b30SVlastimil Babka 2932698b1b30SVlastimil Babka set_freezable(); 2933698b1b30SVlastimil Babka 2934698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 293597a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; 2936698b1b30SVlastimil Babka 2937698b1b30SVlastimil Babka while (!kthread_should_stop()) { 2938eb414681SJohannes Weiner unsigned long pflags; 2939eb414681SJohannes Weiner 294065d759c8SCharan Teja Reddy /* 294165d759c8SCharan Teja Reddy * Avoid the unnecessary wakeup for proactive compaction 294265d759c8SCharan Teja Reddy * when it is disabled. 294365d759c8SCharan Teja Reddy */ 294465d759c8SCharan Teja Reddy if (!sysctl_compaction_proactiveness) 294565d759c8SCharan Teja Reddy timeout = MAX_SCHEDULE_TIMEOUT; 2946698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 2947facdaa91SNitin Gupta if (wait_event_freezable_timeout(pgdat->kcompactd_wait, 294865d759c8SCharan Teja Reddy kcompactd_work_requested(pgdat), timeout) && 294965d759c8SCharan Teja Reddy !pgdat->proactive_compact_trigger) { 2950698b1b30SVlastimil Babka 2951eb414681SJohannes Weiner psi_memstall_enter(&pflags); 2952698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 2953eb414681SJohannes Weiner psi_memstall_leave(&pflags); 2954e1e92bfaSCharan Teja Reddy /* 2955e1e92bfaSCharan Teja Reddy * Reset the timeout value. The defer timeout from 2956e1e92bfaSCharan Teja Reddy * proactive compaction is lost here but that is fine 2957e1e92bfaSCharan Teja Reddy * as the condition of the zone changing substantionally 2958e1e92bfaSCharan Teja Reddy * then carrying on with the previous defer interval is 2959e1e92bfaSCharan Teja Reddy * not useful. 2960e1e92bfaSCharan Teja Reddy */ 2961e1e92bfaSCharan Teja Reddy timeout = default_timeout; 2962facdaa91SNitin Gupta continue; 2963facdaa91SNitin Gupta } 2964facdaa91SNitin Gupta 2965e1e92bfaSCharan Teja Reddy /* 2966e1e92bfaSCharan Teja Reddy * Start the proactive work with default timeout. Based 2967e1e92bfaSCharan Teja Reddy * on the fragmentation score, this timeout is updated. 2968e1e92bfaSCharan Teja Reddy */ 2969e1e92bfaSCharan Teja Reddy timeout = default_timeout; 2970facdaa91SNitin Gupta if (should_proactive_compact_node(pgdat)) { 2971facdaa91SNitin Gupta unsigned int prev_score, score; 2972facdaa91SNitin Gupta 2973facdaa91SNitin Gupta prev_score = fragmentation_score_node(pgdat); 2974facdaa91SNitin Gupta proactive_compact_node(pgdat); 2975facdaa91SNitin Gupta score = fragmentation_score_node(pgdat); 2976facdaa91SNitin Gupta /* 2977facdaa91SNitin Gupta * Defer proactive compaction if the fragmentation 2978facdaa91SNitin Gupta * score did not go down i.e. no progress made. 2979facdaa91SNitin Gupta */ 2980e1e92bfaSCharan Teja Reddy if (unlikely(score >= prev_score)) 2981e1e92bfaSCharan Teja Reddy timeout = 2982e1e92bfaSCharan Teja Reddy default_timeout << COMPACT_MAX_DEFER_SHIFT; 2983facdaa91SNitin Gupta } 298465d759c8SCharan Teja Reddy if (unlikely(pgdat->proactive_compact_trigger)) 298565d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger = false; 2986698b1b30SVlastimil Babka } 2987698b1b30SVlastimil Babka 2988698b1b30SVlastimil Babka return 0; 2989698b1b30SVlastimil Babka } 2990698b1b30SVlastimil Babka 2991698b1b30SVlastimil Babka /* 2992698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 2993698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 2994698b1b30SVlastimil Babka */ 2995024c61eaSMiaohe Lin void kcompactd_run(int nid) 2996698b1b30SVlastimil Babka { 2997698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2998698b1b30SVlastimil Babka 2999698b1b30SVlastimil Babka if (pgdat->kcompactd) 3000024c61eaSMiaohe Lin return; 3001698b1b30SVlastimil Babka 3002698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 3003698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 3004698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 3005698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 3006698b1b30SVlastimil Babka } 3007698b1b30SVlastimil Babka } 3008698b1b30SVlastimil Babka 3009698b1b30SVlastimil Babka /* 3010698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 3011e8da368aSYun-Ze Li * be holding mem_hotplug_begin/done(). 3012698b1b30SVlastimil Babka */ 3013698b1b30SVlastimil Babka void kcompactd_stop(int nid) 3014698b1b30SVlastimil Babka { 3015698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 3016698b1b30SVlastimil Babka 3017698b1b30SVlastimil Babka if (kcompactd) { 3018698b1b30SVlastimil Babka kthread_stop(kcompactd); 3019698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 3020698b1b30SVlastimil Babka } 3021698b1b30SVlastimil Babka } 3022698b1b30SVlastimil Babka 3023698b1b30SVlastimil Babka /* 3024698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 3025698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 3026698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 3027698b1b30SVlastimil Babka * restore their cpu bindings. 3028698b1b30SVlastimil Babka */ 3029e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu) 3030698b1b30SVlastimil Babka { 3031698b1b30SVlastimil Babka int nid; 3032698b1b30SVlastimil Babka 3033698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 3034698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 3035698b1b30SVlastimil Babka const struct cpumask *mask; 3036698b1b30SVlastimil Babka 3037698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 3038698b1b30SVlastimil Babka 3039698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 3040698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 30413109de30SMiaohe Lin if (pgdat->kcompactd) 3042698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 3043698b1b30SVlastimil Babka } 3044e46b1db2SAnna-Maria Gleixner return 0; 3045698b1b30SVlastimil Babka } 3046698b1b30SVlastimil Babka 3047698b1b30SVlastimil Babka static int __init kcompactd_init(void) 3048698b1b30SVlastimil Babka { 3049698b1b30SVlastimil Babka int nid; 3050e46b1db2SAnna-Maria Gleixner int ret; 3051e46b1db2SAnna-Maria Gleixner 3052e46b1db2SAnna-Maria Gleixner ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3053e46b1db2SAnna-Maria Gleixner "mm/compaction:online", 3054e46b1db2SAnna-Maria Gleixner kcompactd_cpu_online, NULL); 3055e46b1db2SAnna-Maria Gleixner if (ret < 0) { 3056e46b1db2SAnna-Maria Gleixner pr_err("kcompactd: failed to register hotplug callbacks.\n"); 3057e46b1db2SAnna-Maria Gleixner return ret; 3058e46b1db2SAnna-Maria Gleixner } 3059698b1b30SVlastimil Babka 3060698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 3061698b1b30SVlastimil Babka kcompactd_run(nid); 3062698b1b30SVlastimil Babka return 0; 3063698b1b30SVlastimil Babka } 3064698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 3065698b1b30SVlastimil Babka 3066ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 3067