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 29010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 30010fc29aSMinchan Kim { 31010fc29aSMinchan Kim count_vm_event(item); 32010fc29aSMinchan Kim } 33010fc29aSMinchan Kim 34010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 35010fc29aSMinchan Kim { 36010fc29aSMinchan Kim count_vm_events(item, delta); 37010fc29aSMinchan Kim } 38010fc29aSMinchan Kim #else 39010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 40010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 41010fc29aSMinchan Kim #endif 42010fc29aSMinchan Kim 43ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 44ff9543fdSMichal Nazarewicz 45b7aba698SMel Gorman #define CREATE_TRACE_POINTS 46b7aba698SMel Gorman #include <trace/events/compaction.h> 47b7aba698SMel Gorman 4806b6640aSVlastimil Babka #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) 4906b6640aSVlastimil Babka #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) 5006b6640aSVlastimil Babka #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) 5106b6640aSVlastimil Babka #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) 5206b6640aSVlastimil Babka 53facdaa91SNitin Gupta /* 54facdaa91SNitin Gupta * Fragmentation score check interval for proactive compaction purposes. 55facdaa91SNitin Gupta */ 56d34c0a75SNitin Gupta static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500; 57facdaa91SNitin Gupta 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 11324e2716fSJoonsoo Kim 114bda807d4SMinchan Kim int PageMovable(struct page *page) 115bda807d4SMinchan Kim { 116bda807d4SMinchan Kim struct address_space *mapping; 117bda807d4SMinchan Kim 118bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 119bda807d4SMinchan Kim if (!__PageMovable(page)) 120bda807d4SMinchan Kim return 0; 121bda807d4SMinchan Kim 122bda807d4SMinchan Kim mapping = page_mapping(page); 123bda807d4SMinchan Kim if (mapping && mapping->a_ops && mapping->a_ops->isolate_page) 124bda807d4SMinchan Kim return 1; 125bda807d4SMinchan Kim 126bda807d4SMinchan Kim return 0; 127bda807d4SMinchan Kim } 128bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable); 129bda807d4SMinchan Kim 130bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping) 131bda807d4SMinchan Kim { 132bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 133bda807d4SMinchan Kim VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page); 134bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE); 135bda807d4SMinchan Kim } 136bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable); 137bda807d4SMinchan Kim 138bda807d4SMinchan Kim void __ClearPageMovable(struct page *page) 139bda807d4SMinchan Kim { 140bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageMovable(page), page); 141bda807d4SMinchan Kim /* 142bda807d4SMinchan Kim * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE 143bda807d4SMinchan Kim * flag so that VM can catch up released page by driver after isolation. 144bda807d4SMinchan Kim * With it, VM migration doesn't try to put it back. 145bda807d4SMinchan Kim */ 146bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)page->mapping & 147bda807d4SMinchan Kim PAGE_MAPPING_MOVABLE); 148bda807d4SMinchan Kim } 149bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable); 150bda807d4SMinchan Kim 15124e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 15224e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 15324e2716fSJoonsoo Kim 15424e2716fSJoonsoo Kim /* 15524e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 156860b3272SAlex Shi * allocation success. 1 << compact_defer_shift, compactions are skipped up 15724e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 15824e2716fSJoonsoo Kim */ 1592271b016SHui Su static void defer_compaction(struct zone *zone, int order) 16024e2716fSJoonsoo Kim { 16124e2716fSJoonsoo Kim zone->compact_considered = 0; 16224e2716fSJoonsoo Kim zone->compact_defer_shift++; 16324e2716fSJoonsoo Kim 16424e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 16524e2716fSJoonsoo Kim zone->compact_order_failed = order; 16624e2716fSJoonsoo Kim 16724e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 16824e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 16924e2716fSJoonsoo Kim 17024e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 17124e2716fSJoonsoo Kim } 17224e2716fSJoonsoo Kim 17324e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 1742271b016SHui Su static bool compaction_deferred(struct zone *zone, int order) 17524e2716fSJoonsoo Kim { 17624e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 17724e2716fSJoonsoo Kim 17824e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 17924e2716fSJoonsoo Kim return false; 18024e2716fSJoonsoo Kim 18124e2716fSJoonsoo Kim /* Avoid possible overflow */ 18262b35fe0SMateusz Nosek if (++zone->compact_considered >= defer_limit) { 18324e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 18424e2716fSJoonsoo Kim return false; 18562b35fe0SMateusz Nosek } 18624e2716fSJoonsoo Kim 18724e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 18824e2716fSJoonsoo Kim 18924e2716fSJoonsoo Kim return true; 19024e2716fSJoonsoo Kim } 19124e2716fSJoonsoo Kim 19224e2716fSJoonsoo Kim /* 19324e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 19424e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 19524e2716fSJoonsoo Kim * expected to succeed. 19624e2716fSJoonsoo Kim */ 19724e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 19824e2716fSJoonsoo Kim bool alloc_success) 19924e2716fSJoonsoo Kim { 20024e2716fSJoonsoo Kim if (alloc_success) { 20124e2716fSJoonsoo Kim zone->compact_considered = 0; 20224e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 20324e2716fSJoonsoo Kim } 20424e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 20524e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 20624e2716fSJoonsoo Kim 20724e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 20824e2716fSJoonsoo Kim } 20924e2716fSJoonsoo Kim 21024e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 2112271b016SHui Su static bool compaction_restarting(struct zone *zone, int order) 21224e2716fSJoonsoo Kim { 21324e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 21424e2716fSJoonsoo Kim return false; 21524e2716fSJoonsoo Kim 21624e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 21724e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 21824e2716fSJoonsoo Kim } 21924e2716fSJoonsoo Kim 220bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 221bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 222bb13ffebSMel Gorman struct page *page) 223bb13ffebSMel Gorman { 224bb13ffebSMel Gorman if (cc->ignore_skip_hint) 225bb13ffebSMel Gorman return true; 226bb13ffebSMel Gorman 227bb13ffebSMel Gorman return !get_pageblock_skip(page); 228bb13ffebSMel Gorman } 229bb13ffebSMel Gorman 23002333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 23102333641SVlastimil Babka { 23202333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 23302333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 234623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 23506b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 23602333641SVlastimil Babka } 23702333641SVlastimil Babka 238bb13ffebSMel Gorman /* 2392271b016SHui Su * Compound pages of >= pageblock_order should consistently be skipped until 240b527cfe5SVlastimil Babka * released. It is always pointless to compact pages of such order (if they are 241b527cfe5SVlastimil Babka * migratable), and the pageblocks they occupy cannot contain any free pages. 24221dc7e02SDavid Rientjes */ 243b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page) 24421dc7e02SDavid Rientjes { 245b527cfe5SVlastimil Babka if (!PageCompound(page)) 24621dc7e02SDavid Rientjes return false; 247b527cfe5SVlastimil Babka 248b527cfe5SVlastimil Babka page = compound_head(page); 249b527cfe5SVlastimil Babka 250b527cfe5SVlastimil Babka if (compound_order(page) >= pageblock_order) 25121dc7e02SDavid Rientjes return true; 252b527cfe5SVlastimil Babka 253b527cfe5SVlastimil Babka return false; 25421dc7e02SDavid Rientjes } 25521dc7e02SDavid Rientjes 256e332f741SMel Gorman static bool 257e332f741SMel Gorman __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, 258e332f741SMel Gorman bool check_target) 259e332f741SMel Gorman { 260e332f741SMel Gorman struct page *page = pfn_to_online_page(pfn); 2616b0868c8SMel Gorman struct page *block_page; 262e332f741SMel Gorman struct page *end_page; 263e332f741SMel Gorman unsigned long block_pfn; 264e332f741SMel Gorman 265e332f741SMel Gorman if (!page) 266e332f741SMel Gorman return false; 267e332f741SMel Gorman if (zone != page_zone(page)) 268e332f741SMel Gorman return false; 269e332f741SMel Gorman if (pageblock_skip_persistent(page)) 270e332f741SMel Gorman return false; 271e332f741SMel Gorman 272e332f741SMel Gorman /* 273e332f741SMel Gorman * If skip is already cleared do no further checking once the 274e332f741SMel Gorman * restart points have been set. 275e332f741SMel Gorman */ 276e332f741SMel Gorman if (check_source && check_target && !get_pageblock_skip(page)) 277e332f741SMel Gorman return true; 278e332f741SMel Gorman 279e332f741SMel Gorman /* 280e332f741SMel Gorman * If clearing skip for the target scanner, do not select a 281e332f741SMel Gorman * non-movable pageblock as the starting point. 282e332f741SMel Gorman */ 283e332f741SMel Gorman if (!check_source && check_target && 284e332f741SMel Gorman get_pageblock_migratetype(page) != MIGRATE_MOVABLE) 285e332f741SMel Gorman return false; 286e332f741SMel Gorman 2876b0868c8SMel Gorman /* Ensure the start of the pageblock or zone is online and valid */ 2886b0868c8SMel Gorman block_pfn = pageblock_start_pfn(pfn); 289a2e9a5afSVlastimil Babka block_pfn = max(block_pfn, zone->zone_start_pfn); 290a2e9a5afSVlastimil Babka block_page = pfn_to_online_page(block_pfn); 2916b0868c8SMel Gorman if (block_page) { 2926b0868c8SMel Gorman page = block_page; 2936b0868c8SMel Gorman pfn = block_pfn; 2946b0868c8SMel Gorman } 2956b0868c8SMel Gorman 2966b0868c8SMel Gorman /* Ensure the end of the pageblock or zone is online and valid */ 297a2e9a5afSVlastimil Babka block_pfn = pageblock_end_pfn(pfn) - 1; 2986b0868c8SMel Gorman block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); 2996b0868c8SMel Gorman end_page = pfn_to_online_page(block_pfn); 3006b0868c8SMel Gorman if (!end_page) 3016b0868c8SMel Gorman return false; 3026b0868c8SMel Gorman 303e332f741SMel Gorman /* 304e332f741SMel Gorman * Only clear the hint if a sample indicates there is either a 305e332f741SMel Gorman * free page or an LRU page in the block. One or other condition 306e332f741SMel Gorman * is necessary for the block to be a migration source/target. 307e332f741SMel Gorman */ 308e332f741SMel Gorman do { 309e332f741SMel Gorman if (check_source && PageLRU(page)) { 310e332f741SMel Gorman clear_pageblock_skip(page); 311e332f741SMel Gorman return true; 312e332f741SMel Gorman } 313e332f741SMel Gorman 314e332f741SMel Gorman if (check_target && PageBuddy(page)) { 315e332f741SMel Gorman clear_pageblock_skip(page); 316e332f741SMel Gorman return true; 317e332f741SMel Gorman } 318e332f741SMel Gorman 319e332f741SMel Gorman page += (1 << PAGE_ALLOC_COSTLY_ORDER); 320e332f741SMel Gorman pfn += (1 << PAGE_ALLOC_COSTLY_ORDER); 321a2e9a5afSVlastimil Babka } while (page <= end_page); 322e332f741SMel Gorman 323e332f741SMel Gorman return false; 324e332f741SMel Gorman } 325e332f741SMel Gorman 32621dc7e02SDavid Rientjes /* 327bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 328bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 329bb13ffebSMel Gorman * meet. 330bb13ffebSMel Gorman */ 33162997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 332bb13ffebSMel Gorman { 333e332f741SMel Gorman unsigned long migrate_pfn = zone->zone_start_pfn; 3346b0868c8SMel Gorman unsigned long free_pfn = zone_end_pfn(zone) - 1; 335e332f741SMel Gorman unsigned long reset_migrate = free_pfn; 336e332f741SMel Gorman unsigned long reset_free = migrate_pfn; 337e332f741SMel Gorman bool source_set = false; 338e332f741SMel Gorman bool free_set = false; 339e332f741SMel Gorman 340e332f741SMel Gorman if (!zone->compact_blockskip_flush) 341e332f741SMel Gorman return; 342bb13ffebSMel Gorman 34362997027SMel Gorman zone->compact_blockskip_flush = false; 344bb13ffebSMel Gorman 345e332f741SMel Gorman /* 346e332f741SMel Gorman * Walk the zone and update pageblock skip information. Source looks 347e332f741SMel Gorman * for PageLRU while target looks for PageBuddy. When the scanner 348e332f741SMel Gorman * is found, both PageBuddy and PageLRU are checked as the pageblock 349e332f741SMel Gorman * is suitable as both source and target. 350e332f741SMel Gorman */ 351e332f741SMel Gorman for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, 352e332f741SMel Gorman free_pfn -= pageblock_nr_pages) { 353bb13ffebSMel Gorman cond_resched(); 354bb13ffebSMel Gorman 355e332f741SMel Gorman /* Update the migrate PFN */ 356e332f741SMel Gorman if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && 357e332f741SMel Gorman migrate_pfn < reset_migrate) { 358e332f741SMel Gorman source_set = true; 359e332f741SMel Gorman reset_migrate = migrate_pfn; 360e332f741SMel Gorman zone->compact_init_migrate_pfn = reset_migrate; 361e332f741SMel Gorman zone->compact_cached_migrate_pfn[0] = reset_migrate; 362e332f741SMel Gorman zone->compact_cached_migrate_pfn[1] = reset_migrate; 363bb13ffebSMel Gorman } 36402333641SVlastimil Babka 365e332f741SMel Gorman /* Update the free PFN */ 366e332f741SMel Gorman if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && 367e332f741SMel Gorman free_pfn > reset_free) { 368e332f741SMel Gorman free_set = true; 369e332f741SMel Gorman reset_free = free_pfn; 370e332f741SMel Gorman zone->compact_init_free_pfn = reset_free; 371e332f741SMel Gorman zone->compact_cached_free_pfn = reset_free; 372e332f741SMel Gorman } 373e332f741SMel Gorman } 374e332f741SMel Gorman 375e332f741SMel Gorman /* Leave no distance if no suitable block was reset */ 376e332f741SMel Gorman if (reset_migrate >= reset_free) { 377e332f741SMel Gorman zone->compact_cached_migrate_pfn[0] = migrate_pfn; 378e332f741SMel Gorman zone->compact_cached_migrate_pfn[1] = migrate_pfn; 379e332f741SMel Gorman zone->compact_cached_free_pfn = free_pfn; 380e332f741SMel Gorman } 381bb13ffebSMel Gorman } 382bb13ffebSMel Gorman 38362997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 38462997027SMel Gorman { 38562997027SMel Gorman int zoneid; 38662997027SMel Gorman 38762997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 38862997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 38962997027SMel Gorman if (!populated_zone(zone)) 39062997027SMel Gorman continue; 39162997027SMel Gorman 39262997027SMel Gorman /* Only flush if a full compaction finished recently */ 39362997027SMel Gorman if (zone->compact_blockskip_flush) 39462997027SMel Gorman __reset_isolation_suitable(zone); 39562997027SMel Gorman } 39662997027SMel Gorman } 39762997027SMel Gorman 398bb13ffebSMel Gorman /* 399e380bebeSMel Gorman * Sets the pageblock skip bit if it was clear. Note that this is a hint as 400e380bebeSMel Gorman * locks are not required for read/writers. Returns true if it was already set. 401e380bebeSMel Gorman */ 402e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 403e380bebeSMel Gorman unsigned long pfn) 404e380bebeSMel Gorman { 405e380bebeSMel Gorman bool skip; 406e380bebeSMel Gorman 407e380bebeSMel Gorman /* Do no update if skip hint is being ignored */ 408e380bebeSMel Gorman if (cc->ignore_skip_hint) 409e380bebeSMel Gorman return false; 410e380bebeSMel Gorman 411e380bebeSMel Gorman if (!IS_ALIGNED(pfn, pageblock_nr_pages)) 412e380bebeSMel Gorman return false; 413e380bebeSMel Gorman 414e380bebeSMel Gorman skip = get_pageblock_skip(page); 415e380bebeSMel Gorman if (!skip && !cc->no_set_skip_hint) 416e380bebeSMel Gorman set_pageblock_skip(page); 417e380bebeSMel Gorman 418e380bebeSMel Gorman return skip; 419e380bebeSMel Gorman } 420e380bebeSMel Gorman 421e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 422e380bebeSMel Gorman { 423e380bebeSMel Gorman struct zone *zone = cc->zone; 424e380bebeSMel Gorman 425e380bebeSMel Gorman pfn = pageblock_end_pfn(pfn); 426e380bebeSMel Gorman 427e380bebeSMel Gorman /* Set for isolation rather than compaction */ 428e380bebeSMel Gorman if (cc->no_set_skip_hint) 429e380bebeSMel Gorman return; 430e380bebeSMel Gorman 431e380bebeSMel Gorman if (pfn > zone->compact_cached_migrate_pfn[0]) 432e380bebeSMel Gorman zone->compact_cached_migrate_pfn[0] = pfn; 433e380bebeSMel Gorman if (cc->mode != MIGRATE_ASYNC && 434e380bebeSMel Gorman pfn > zone->compact_cached_migrate_pfn[1]) 435e380bebeSMel Gorman zone->compact_cached_migrate_pfn[1] = pfn; 436e380bebeSMel Gorman } 437e380bebeSMel Gorman 438e380bebeSMel Gorman /* 439bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 44062997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 441bb13ffebSMel Gorman */ 442c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 443d097a6f6SMel Gorman struct page *page, unsigned long pfn) 444bb13ffebSMel Gorman { 445c89511abSMel Gorman struct zone *zone = cc->zone; 4466815bf3fSJoonsoo Kim 4472583d671SVlastimil Babka if (cc->no_set_skip_hint) 4486815bf3fSJoonsoo Kim return; 4496815bf3fSJoonsoo Kim 450bb13ffebSMel Gorman if (!page) 451bb13ffebSMel Gorman return; 452bb13ffebSMel Gorman 453bb13ffebSMel Gorman set_pageblock_skip(page); 454c89511abSMel Gorman 45535979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 45635979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 457c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 458c89511abSMel Gorman } 459bb13ffebSMel Gorman #else 460bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 461bb13ffebSMel Gorman struct page *page) 462bb13ffebSMel Gorman { 463bb13ffebSMel Gorman return true; 464bb13ffebSMel Gorman } 465bb13ffebSMel Gorman 466b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page) 46721dc7e02SDavid Rientjes { 46821dc7e02SDavid Rientjes return false; 46921dc7e02SDavid Rientjes } 47021dc7e02SDavid Rientjes 47121dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc, 472d097a6f6SMel Gorman struct page *page, unsigned long pfn) 473bb13ffebSMel Gorman { 474bb13ffebSMel Gorman } 475e380bebeSMel Gorman 476e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 477e380bebeSMel Gorman { 478e380bebeSMel Gorman } 479e380bebeSMel Gorman 480e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 481e380bebeSMel Gorman unsigned long pfn) 482e380bebeSMel Gorman { 483e380bebeSMel Gorman return false; 484e380bebeSMel Gorman } 485bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 486bb13ffebSMel Gorman 4871f9efdefSVlastimil Babka /* 4888b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 489cb2dcaf0SMel Gorman * very heavily contended. For async compaction, trylock and record if the 490cb2dcaf0SMel Gorman * lock is contended. The lock will still be acquired but compaction will 491cb2dcaf0SMel Gorman * abort when the current block is finished regardless of success rate. 492cb2dcaf0SMel Gorman * Sync compaction acquires the lock. 4938b44d279SVlastimil Babka * 494cb2dcaf0SMel Gorman * Always returns true which makes it easier to track lock state in callers. 4951f9efdefSVlastimil Babka */ 496cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, 4978b44d279SVlastimil Babka struct compact_control *cc) 49877337edeSJules Irenge __acquires(lock) 4998b44d279SVlastimil Babka { 500cb2dcaf0SMel Gorman /* Track if the lock is contended in async mode */ 501cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC && !cc->contended) { 502cb2dcaf0SMel Gorman if (spin_trylock_irqsave(lock, *flags)) 503cb2dcaf0SMel Gorman return true; 504cb2dcaf0SMel Gorman 505c3486f53SVlastimil Babka cc->contended = true; 5068b44d279SVlastimil Babka } 5071f9efdefSVlastimil Babka 508cb2dcaf0SMel Gorman spin_lock_irqsave(lock, *flags); 5098b44d279SVlastimil Babka return true; 5102a1402aaSMel Gorman } 5112a1402aaSMel Gorman 51285aa125fSMichal Nazarewicz /* 513c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 5148b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 5158b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 5168b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 5178b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 5188b44d279SVlastimil Babka * aborts. Sync compaction schedules. 5198b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 5208b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 521c67fe375SMel Gorman * 5228b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 5238b44d279SVlastimil Babka * async compaction due to need_resched() 5248b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 5258b44d279SVlastimil Babka * scheduled) 526c67fe375SMel Gorman */ 5278b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 5288b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 529c67fe375SMel Gorman { 5308b44d279SVlastimil Babka if (*locked) { 5318b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 5328b44d279SVlastimil Babka *locked = false; 533c67fe375SMel Gorman } 534c67fe375SMel Gorman 5358b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 536c3486f53SVlastimil Babka cc->contended = true; 5378b44d279SVlastimil Babka return true; 5388b44d279SVlastimil Babka } 5398b44d279SVlastimil Babka 540cf66f070SMel Gorman cond_resched(); 541be976572SVlastimil Babka 542be976572SVlastimil Babka return false; 543be976572SVlastimil Babka } 544be976572SVlastimil Babka 545c67fe375SMel Gorman /* 5469e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 5479e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 5489e4be470SJerome Marchand * (even though it may still end up isolating some pages). 54985aa125fSMichal Nazarewicz */ 550f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 551e14c720eSVlastimil Babka unsigned long *start_pfn, 55285aa125fSMichal Nazarewicz unsigned long end_pfn, 55385aa125fSMichal Nazarewicz struct list_head *freelist, 5544fca9730SMel Gorman unsigned int stride, 55585aa125fSMichal Nazarewicz bool strict) 556748446bbSMel Gorman { 557b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 558d097a6f6SMel Gorman struct page *cursor; 559b8b2d825SXiubo Li unsigned long flags = 0; 560f40d1e42SMel Gorman bool locked = false; 561e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 56266c64223SJoonsoo Kim unsigned int order; 563748446bbSMel Gorman 5644fca9730SMel Gorman /* Strict mode is for isolation, speed is secondary */ 5654fca9730SMel Gorman if (strict) 5664fca9730SMel Gorman stride = 1; 5674fca9730SMel Gorman 568748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 569748446bbSMel Gorman 570f40d1e42SMel Gorman /* Isolate free pages. */ 5714fca9730SMel Gorman for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) { 57266c64223SJoonsoo Kim int isolated; 573748446bbSMel Gorman struct page *page = cursor; 574748446bbSMel Gorman 5758b44d279SVlastimil Babka /* 5768b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 5778b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 5788b44d279SVlastimil Babka * pending or async compaction detects need_resched() 5798b44d279SVlastimil Babka */ 5808b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 5818b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 5828b44d279SVlastimil Babka &locked, cc)) 5838b44d279SVlastimil Babka break; 5848b44d279SVlastimil Babka 585b7aba698SMel Gorman nr_scanned++; 5862af120bcSLaura Abbott 5879fcd6d2eSVlastimil Babka /* 5889fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 5899fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 5909fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 5919fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 5929fcd6d2eSVlastimil Babka */ 5939fcd6d2eSVlastimil Babka if (PageCompound(page)) { 59421dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 5959fcd6d2eSVlastimil Babka 596d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) { 59721dc7e02SDavid Rientjes blockpfn += (1UL << order) - 1; 59821dc7e02SDavid Rientjes cursor += (1UL << order) - 1; 5999fcd6d2eSVlastimil Babka } 6009fcd6d2eSVlastimil Babka goto isolate_fail; 6019fcd6d2eSVlastimil Babka } 6029fcd6d2eSVlastimil Babka 603f40d1e42SMel Gorman if (!PageBuddy(page)) 6042af120bcSLaura Abbott goto isolate_fail; 605f40d1e42SMel Gorman 606f40d1e42SMel Gorman /* 60769b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 60869b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 60969b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 61069b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 61169b7189fSVlastimil Babka * recheck as well. 61269b7189fSVlastimil Babka */ 61369b7189fSVlastimil Babka if (!locked) { 614cb2dcaf0SMel Gorman locked = compact_lock_irqsave(&cc->zone->lock, 6158b44d279SVlastimil Babka &flags, cc); 616f40d1e42SMel Gorman 617f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 618f40d1e42SMel Gorman if (!PageBuddy(page)) 6192af120bcSLaura Abbott goto isolate_fail; 62069b7189fSVlastimil Babka } 621748446bbSMel Gorman 62266c64223SJoonsoo Kim /* Found a free page, will break it into order-0 pages */ 623ab130f91SMatthew Wilcox (Oracle) order = buddy_order(page); 62466c64223SJoonsoo Kim isolated = __isolate_free_page(page, order); 625a4f04f2cSDavid Rientjes if (!isolated) 626a4f04f2cSDavid Rientjes break; 62766c64223SJoonsoo Kim set_page_private(page, order); 628a4f04f2cSDavid Rientjes 629748446bbSMel Gorman total_isolated += isolated; 630a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 63166c64223SJoonsoo Kim list_add_tail(&page->lru, freelist); 63266c64223SJoonsoo Kim 633a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 634932ff6bbSJoonsoo Kim blockpfn += isolated; 635932ff6bbSJoonsoo Kim break; 636932ff6bbSJoonsoo Kim } 637a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 638748446bbSMel Gorman blockpfn += isolated - 1; 639748446bbSMel Gorman cursor += isolated - 1; 6402af120bcSLaura Abbott continue; 6412af120bcSLaura Abbott 6422af120bcSLaura Abbott isolate_fail: 6432af120bcSLaura Abbott if (strict) 6442af120bcSLaura Abbott break; 6452af120bcSLaura Abbott else 6462af120bcSLaura Abbott continue; 6472af120bcSLaura Abbott 648748446bbSMel Gorman } 649748446bbSMel Gorman 650a4f04f2cSDavid Rientjes if (locked) 651a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 652a4f04f2cSDavid Rientjes 6539fcd6d2eSVlastimil Babka /* 6549fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 6559fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 6569fcd6d2eSVlastimil Babka */ 6579fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 6589fcd6d2eSVlastimil Babka blockpfn = end_pfn; 6599fcd6d2eSVlastimil Babka 660e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 661e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 662e34d85f0SJoonsoo Kim 663e14c720eSVlastimil Babka /* Record how far we have got within the block */ 664e14c720eSVlastimil Babka *start_pfn = blockpfn; 665e14c720eSVlastimil Babka 666f40d1e42SMel Gorman /* 667f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 668f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 669f40d1e42SMel Gorman * returned and CMA will fail. 670f40d1e42SMel Gorman */ 6712af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 672f40d1e42SMel Gorman total_isolated = 0; 673f40d1e42SMel Gorman 6747f354a54SDavid Rientjes cc->total_free_scanned += nr_scanned; 675397487dbSMel Gorman if (total_isolated) 676010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 677748446bbSMel Gorman return total_isolated; 678748446bbSMel Gorman } 679748446bbSMel Gorman 68085aa125fSMichal Nazarewicz /** 68185aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 682e8b098fcSMike Rapoport * @cc: Compaction control structure. 68385aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 68485aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 68585aa125fSMichal Nazarewicz * 68685aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 68785aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 68885aa125fSMichal Nazarewicz * undo its actions and return zero. 68985aa125fSMichal Nazarewicz * 69085aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 69185aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 69285aa125fSMichal Nazarewicz * a free page). 69385aa125fSMichal Nazarewicz */ 694ff9543fdSMichal Nazarewicz unsigned long 695bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 696bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 69785aa125fSMichal Nazarewicz { 698e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 69985aa125fSMichal Nazarewicz LIST_HEAD(freelist); 70085aa125fSMichal Nazarewicz 7017d49d886SVlastimil Babka pfn = start_pfn; 70206b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 703e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 704e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 70506b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 7067d49d886SVlastimil Babka 7077d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 708e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 7097d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 710e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 711e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 7127d49d886SVlastimil Babka 71385aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 71485aa125fSMichal Nazarewicz 71558420016SJoonsoo Kim /* 71658420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 71758420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 71858420016SJoonsoo Kim * scanning range to right one. 71958420016SJoonsoo Kim */ 72058420016SJoonsoo Kim if (pfn >= block_end_pfn) { 72106b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 72206b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 72358420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 72458420016SJoonsoo Kim } 72558420016SJoonsoo Kim 726e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 727e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 7287d49d886SVlastimil Babka break; 7297d49d886SVlastimil Babka 730e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 7314fca9730SMel Gorman block_end_pfn, &freelist, 0, true); 73285aa125fSMichal Nazarewicz 73385aa125fSMichal Nazarewicz /* 73485aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 73585aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 73685aa125fSMichal Nazarewicz * non-free pages). 73785aa125fSMichal Nazarewicz */ 73885aa125fSMichal Nazarewicz if (!isolated) 73985aa125fSMichal Nazarewicz break; 74085aa125fSMichal Nazarewicz 74185aa125fSMichal Nazarewicz /* 74285aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 74385aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 74485aa125fSMichal Nazarewicz * page may span two pageblocks). 74585aa125fSMichal Nazarewicz */ 74685aa125fSMichal Nazarewicz } 74785aa125fSMichal Nazarewicz 74866c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 7494469ab98SMel Gorman split_map_pages(&freelist); 75085aa125fSMichal Nazarewicz 75185aa125fSMichal Nazarewicz if (pfn < end_pfn) { 75285aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 75385aa125fSMichal Nazarewicz release_freepages(&freelist); 75485aa125fSMichal Nazarewicz return 0; 75585aa125fSMichal Nazarewicz } 75685aa125fSMichal Nazarewicz 75785aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 75885aa125fSMichal Nazarewicz return pfn; 75985aa125fSMichal Nazarewicz } 76085aa125fSMichal Nazarewicz 761748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 7625f438eeeSAndrey Ryabinin static bool too_many_isolated(pg_data_t *pgdat) 763748446bbSMel Gorman { 764bc693045SMinchan Kim unsigned long active, inactive, isolated; 765748446bbSMel Gorman 7665f438eeeSAndrey Ryabinin inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + 7675f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_INACTIVE_ANON); 7685f438eeeSAndrey Ryabinin active = node_page_state(pgdat, NR_ACTIVE_FILE) + 7695f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_ACTIVE_ANON); 7705f438eeeSAndrey Ryabinin isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + 7715f438eeeSAndrey Ryabinin node_page_state(pgdat, NR_ISOLATED_ANON); 772748446bbSMel Gorman 773bc693045SMinchan Kim return isolated > (inactive + active) / 2; 774748446bbSMel Gorman } 775748446bbSMel Gorman 7762fe86e00SMichal Nazarewicz /** 777edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 778edc2ca61SVlastimil Babka * a single pageblock 7792fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 780edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 781edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 782edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 7832fe86e00SMichal Nazarewicz * 7842fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 785edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 786c2ad7a1fSOscar Salvador * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion, 787369fa227SOscar Salvador * -ENOMEM in case we could not allocate a page, or 0. 788c2ad7a1fSOscar Salvador * cc->migrate_pfn will contain the next pfn to scan. 7892fe86e00SMichal Nazarewicz * 790edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 791c2ad7a1fSOscar Salvador * and cc->nr_migratepages is updated accordingly. 792748446bbSMel Gorman */ 793c2ad7a1fSOscar Salvador static int 794edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 795edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 796748446bbSMel Gorman { 7975f438eeeSAndrey Ryabinin pg_data_t *pgdat = cc->zone->zone_pgdat; 798b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 799fa9add64SHugh Dickins struct lruvec *lruvec; 800b8b2d825SXiubo Li unsigned long flags = 0; 8016168d0daSAlex Shi struct lruvec *locked = NULL; 802bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 803e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 804fdd048e1SVlastimil Babka bool skip_on_failure = false; 805fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 806e380bebeSMel Gorman bool skip_updated = false; 807c2ad7a1fSOscar Salvador int ret = 0; 808c2ad7a1fSOscar Salvador 809c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn; 810748446bbSMel Gorman 811748446bbSMel Gorman /* 812748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 813748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 814748446bbSMel Gorman * delay for some time until fewer pages are isolated 815748446bbSMel Gorman */ 8165f438eeeSAndrey Ryabinin while (unlikely(too_many_isolated(pgdat))) { 817d20bdd57SZi Yan /* stop isolation if there are still pages not migrated */ 818d20bdd57SZi Yan if (cc->nr_migratepages) 819c2ad7a1fSOscar Salvador return -EAGAIN; 820d20bdd57SZi Yan 821f9e35b3bSMel Gorman /* async migration should just abort */ 822e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 823c2ad7a1fSOscar Salvador return -EAGAIN; 824f9e35b3bSMel Gorman 825748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 826748446bbSMel Gorman 827748446bbSMel Gorman if (fatal_signal_pending(current)) 828c2ad7a1fSOscar Salvador return -EINTR; 829748446bbSMel Gorman } 830748446bbSMel Gorman 831cf66f070SMel Gorman cond_resched(); 832aeef4b83SDavid Rientjes 833fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 834fdd048e1SVlastimil Babka skip_on_failure = true; 835fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 836fdd048e1SVlastimil Babka } 837fdd048e1SVlastimil Babka 838748446bbSMel Gorman /* Time to isolate some pages for migration */ 839748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 84029c0dde8SVlastimil Babka 841fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 842fdd048e1SVlastimil Babka /* 843fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 844fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 845fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 846fdd048e1SVlastimil Babka * hopefully succeed compaction. 847fdd048e1SVlastimil Babka */ 848fdd048e1SVlastimil Babka if (nr_isolated) 849fdd048e1SVlastimil Babka break; 850fdd048e1SVlastimil Babka 851fdd048e1SVlastimil Babka /* 852fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 853fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 854fdd048e1SVlastimil Babka * current block. Note we can't simply increase 855fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 856fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 857fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 858fdd048e1SVlastimil Babka * previous loop iteration. 859fdd048e1SVlastimil Babka */ 860fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 861fdd048e1SVlastimil Babka } 862fdd048e1SVlastimil Babka 8638b44d279SVlastimil Babka /* 8648b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 865670105a2SMel Gorman * contention, to give chance to IRQs. Abort completely if 866670105a2SMel Gorman * a fatal signal is pending. 8678b44d279SVlastimil Babka */ 8686168d0daSAlex Shi if (!(low_pfn % SWAP_CLUSTER_MAX)) { 8696168d0daSAlex Shi if (locked) { 8706168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 8716168d0daSAlex Shi locked = NULL; 8726168d0daSAlex Shi } 8736168d0daSAlex Shi 8746168d0daSAlex Shi if (fatal_signal_pending(current)) { 8756168d0daSAlex Shi cc->contended = true; 876c2ad7a1fSOscar Salvador ret = -EINTR; 8776168d0daSAlex Shi 878670105a2SMel Gorman goto fatal_pending; 879670105a2SMel Gorman } 880b2eef8c0SAndrea Arcangeli 8816168d0daSAlex Shi cond_resched(); 8826168d0daSAlex Shi } 8836168d0daSAlex Shi 884b7aba698SMel Gorman nr_scanned++; 885748446bbSMel Gorman 886748446bbSMel Gorman page = pfn_to_page(low_pfn); 887dc908600SMel Gorman 888e380bebeSMel Gorman /* 889e380bebeSMel Gorman * Check if the pageblock has already been marked skipped. 890e380bebeSMel Gorman * Only the aligned PFN is checked as the caller isolates 891e380bebeSMel Gorman * COMPACT_CLUSTER_MAX at a time so the second call must 892e380bebeSMel Gorman * not falsely conclude that the block should be skipped. 893e380bebeSMel Gorman */ 894e380bebeSMel Gorman if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { 895e380bebeSMel Gorman if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { 896e380bebeSMel Gorman low_pfn = end_pfn; 8979df41314SAlex Shi page = NULL; 898e380bebeSMel Gorman goto isolate_abort; 899e380bebeSMel Gorman } 900bb13ffebSMel Gorman valid_page = page; 901e380bebeSMel Gorman } 902bb13ffebSMel Gorman 903369fa227SOscar Salvador if (PageHuge(page) && cc->alloc_contig) { 904ae37c7ffSOscar Salvador ret = isolate_or_dissolve_huge_page(page, &cc->migratepages); 905369fa227SOscar Salvador 906369fa227SOscar Salvador /* 907369fa227SOscar Salvador * Fail isolation in case isolate_or_dissolve_huge_page() 908369fa227SOscar Salvador * reports an error. In case of -ENOMEM, abort right away. 909369fa227SOscar Salvador */ 910369fa227SOscar Salvador if (ret < 0) { 911369fa227SOscar Salvador /* Do not report -EBUSY down the chain */ 912369fa227SOscar Salvador if (ret == -EBUSY) 913369fa227SOscar Salvador ret = 0; 914369fa227SOscar Salvador low_pfn += (1UL << compound_order(page)) - 1; 915369fa227SOscar Salvador goto isolate_fail; 916369fa227SOscar Salvador } 917369fa227SOscar Salvador 918ae37c7ffSOscar Salvador if (PageHuge(page)) { 919ae37c7ffSOscar Salvador /* 920ae37c7ffSOscar Salvador * Hugepage was successfully isolated and placed 921ae37c7ffSOscar Salvador * on the cc->migratepages list. 922ae37c7ffSOscar Salvador */ 923ae37c7ffSOscar Salvador low_pfn += compound_nr(page) - 1; 924ae37c7ffSOscar Salvador goto isolate_success_no_list; 925ae37c7ffSOscar Salvador } 926ae37c7ffSOscar Salvador 927369fa227SOscar Salvador /* 928369fa227SOscar Salvador * Ok, the hugepage was dissolved. Now these pages are 929369fa227SOscar Salvador * Buddy and cannot be re-allocated because they are 930369fa227SOscar Salvador * isolated. Fall-through as the check below handles 931369fa227SOscar Salvador * Buddy pages. 932369fa227SOscar Salvador */ 933369fa227SOscar Salvador } 934369fa227SOscar Salvador 935c122b208SJoonsoo Kim /* 93699c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 93799c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 93899c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 93999c0fd5eSVlastimil Babka * potential isolation targets. 9406c14466cSMel Gorman */ 94199c0fd5eSVlastimil Babka if (PageBuddy(page)) { 942ab130f91SMatthew Wilcox (Oracle) unsigned long freepage_order = buddy_order_unsafe(page); 94399c0fd5eSVlastimil Babka 94499c0fd5eSVlastimil Babka /* 94599c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 94699c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 94799c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 94899c0fd5eSVlastimil Babka */ 94999c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 95099c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 951748446bbSMel Gorman continue; 95299c0fd5eSVlastimil Babka } 953748446bbSMel Gorman 9549927af74SMel Gorman /* 95529c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 9561da2f328SRik van Riel * hugetlbfs are not to be compacted unless we are attempting 9571da2f328SRik van Riel * an allocation much larger than the huge page size (eg CMA). 9581da2f328SRik van Riel * We can potentially save a lot of iterations if we skip them 9591da2f328SRik van Riel * at once. The check is racy, but we can consider only valid 9601da2f328SRik van Riel * values and the only danger is skipping too much. 961bc835011SAndrea Arcangeli */ 9621da2f328SRik van Riel if (PageCompound(page) && !cc->alloc_contig) { 96321dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 96429c0dde8SVlastimil Babka 965d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) 96621dc7e02SDavid Rientjes low_pfn += (1UL << order) - 1; 967fdd048e1SVlastimil Babka goto isolate_fail; 9682a1402aaSMel Gorman } 9692a1402aaSMel Gorman 970bda807d4SMinchan Kim /* 971bda807d4SMinchan Kim * Check may be lockless but that's ok as we recheck later. 972bda807d4SMinchan Kim * It's possible to migrate LRU and non-lru movable pages. 973bda807d4SMinchan Kim * Skip any other type of page 974bda807d4SMinchan Kim */ 975bda807d4SMinchan Kim if (!PageLRU(page)) { 976bda807d4SMinchan Kim /* 977bda807d4SMinchan Kim * __PageMovable can return false positive so we need 978bda807d4SMinchan Kim * to verify it under page_lock. 979bda807d4SMinchan Kim */ 980bda807d4SMinchan Kim if (unlikely(__PageMovable(page)) && 981bda807d4SMinchan Kim !PageIsolated(page)) { 982bda807d4SMinchan Kim if (locked) { 9836168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 9846168d0daSAlex Shi locked = NULL; 985bda807d4SMinchan Kim } 986bda807d4SMinchan Kim 9879e5bcd61SYisheng Xie if (!isolate_movable_page(page, isolate_mode)) 988bda807d4SMinchan Kim goto isolate_success; 989bda807d4SMinchan Kim } 990bda807d4SMinchan Kim 991fdd048e1SVlastimil Babka goto isolate_fail; 992bda807d4SMinchan Kim } 99329c0dde8SVlastimil Babka 994119d6d59SDavid Rientjes /* 995119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 996119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 997119d6d59SDavid Rientjes * admittedly racy check. 998119d6d59SDavid Rientjes */ 999119d6d59SDavid Rientjes if (!page_mapping(page) && 1000119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 1001fdd048e1SVlastimil Babka goto isolate_fail; 1002119d6d59SDavid Rientjes 100373e64c51SMichal Hocko /* 100473e64c51SMichal Hocko * Only allow to migrate anonymous pages in GFP_NOFS context 100573e64c51SMichal Hocko * because those do not depend on fs locks. 100673e64c51SMichal Hocko */ 100773e64c51SMichal Hocko if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page)) 100873e64c51SMichal Hocko goto isolate_fail; 100973e64c51SMichal Hocko 10109df41314SAlex Shi /* 10119df41314SAlex Shi * Be careful not to clear PageLRU until after we're 10129df41314SAlex Shi * sure the page is not being freed elsewhere -- the 10139df41314SAlex Shi * page release code relies on it. 10149df41314SAlex Shi */ 10159df41314SAlex Shi if (unlikely(!get_page_unless_zero(page))) 10169df41314SAlex Shi goto isolate_fail; 10179df41314SAlex Shi 1018c2135f7cSAlex Shi if (!__isolate_lru_page_prepare(page, isolate_mode)) 10199df41314SAlex Shi goto isolate_fail_put; 10209df41314SAlex Shi 10219df41314SAlex Shi /* Try isolate the page */ 10229df41314SAlex Shi if (!TestClearPageLRU(page)) 10239df41314SAlex Shi goto isolate_fail_put; 10249df41314SAlex Shi 1025*b1baabd9SMatthew Wilcox (Oracle) lruvec = folio_lruvec(page_folio(page)); 10266168d0daSAlex Shi 102769b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 10286168d0daSAlex Shi if (lruvec != locked) { 10296168d0daSAlex Shi if (locked) 10306168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 10316168d0daSAlex Shi 10326168d0daSAlex Shi compact_lock_irqsave(&lruvec->lru_lock, &flags, cc); 10336168d0daSAlex Shi locked = lruvec; 10346168d0daSAlex Shi 10356168d0daSAlex Shi lruvec_memcg_debug(lruvec, page); 1036e380bebeSMel Gorman 1037e380bebeSMel Gorman /* Try get exclusive access under lock */ 1038e380bebeSMel Gorman if (!skip_updated) { 1039e380bebeSMel Gorman skip_updated = true; 1040e380bebeSMel Gorman if (test_and_set_skip(cc, page, low_pfn)) 1041e380bebeSMel Gorman goto isolate_abort; 1042e380bebeSMel Gorman } 10432a1402aaSMel Gorman 104429c0dde8SVlastimil Babka /* 104529c0dde8SVlastimil Babka * Page become compound since the non-locked check, 104629c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 104729c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 104829c0dde8SVlastimil Babka */ 10491da2f328SRik van Riel if (unlikely(PageCompound(page) && !cc->alloc_contig)) { 1050d8c6546bSMatthew Wilcox (Oracle) low_pfn += compound_nr(page) - 1; 10519df41314SAlex Shi SetPageLRU(page); 10529df41314SAlex Shi goto isolate_fail_put; 1053bc835011SAndrea Arcangeli } 1054d99fd5feSAlex Shi } 1055fa9add64SHugh Dickins 10561da2f328SRik van Riel /* The whole page is taken off the LRU; skip the tail pages. */ 10571da2f328SRik van Riel if (PageCompound(page)) 10581da2f328SRik van Riel low_pfn += compound_nr(page) - 1; 1059bc835011SAndrea Arcangeli 1060748446bbSMel Gorman /* Successfully isolated */ 106146ae6b2cSYu Zhao del_page_from_lru_list(page, lruvec); 10621da2f328SRik van Riel mod_node_page_state(page_pgdat(page), 10639de4f22aSHuang Ying NR_ISOLATED_ANON + page_is_file_lru(page), 10646c357848SMatthew Wilcox (Oracle) thp_nr_pages(page)); 1065b6c75016SJoonsoo Kim 1066b6c75016SJoonsoo Kim isolate_success: 1067fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 1068ae37c7ffSOscar Salvador isolate_success_no_list: 106938935861SZi Yan cc->nr_migratepages += compound_nr(page); 107038935861SZi Yan nr_isolated += compound_nr(page); 1071748446bbSMel Gorman 1072804d3121SMel Gorman /* 1073804d3121SMel Gorman * Avoid isolating too much unless this block is being 1074cb2dcaf0SMel Gorman * rescanned (e.g. dirty/writeback pages, parallel allocation) 1075cb2dcaf0SMel Gorman * or a lock is contended. For contention, isolate quickly to 1076cb2dcaf0SMel Gorman * potentially remove one source of contention. 1077804d3121SMel Gorman */ 107838935861SZi Yan if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && 1079cb2dcaf0SMel Gorman !cc->rescan && !cc->contended) { 108031b8384aSHillf Danton ++low_pfn; 1081748446bbSMel Gorman break; 1082748446bbSMel Gorman } 1083fdd048e1SVlastimil Babka 1084fdd048e1SVlastimil Babka continue; 10859df41314SAlex Shi 10869df41314SAlex Shi isolate_fail_put: 10879df41314SAlex Shi /* Avoid potential deadlock in freeing page under lru_lock */ 10889df41314SAlex Shi if (locked) { 10896168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 10906168d0daSAlex Shi locked = NULL; 10919df41314SAlex Shi } 10929df41314SAlex Shi put_page(page); 10939df41314SAlex Shi 1094fdd048e1SVlastimil Babka isolate_fail: 1095369fa227SOscar Salvador if (!skip_on_failure && ret != -ENOMEM) 1096fdd048e1SVlastimil Babka continue; 1097fdd048e1SVlastimil Babka 1098fdd048e1SVlastimil Babka /* 1099fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 1100fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 1101fdd048e1SVlastimil Babka * page anyway. 1102fdd048e1SVlastimil Babka */ 1103fdd048e1SVlastimil Babka if (nr_isolated) { 1104fdd048e1SVlastimil Babka if (locked) { 11056168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 11066168d0daSAlex Shi locked = NULL; 1107fdd048e1SVlastimil Babka } 1108fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 1109fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 1110fdd048e1SVlastimil Babka nr_isolated = 0; 1111fdd048e1SVlastimil Babka } 1112fdd048e1SVlastimil Babka 1113fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 1114fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 1115fdd048e1SVlastimil Babka /* 1116fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 1117fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 1118fdd048e1SVlastimil Babka */ 1119fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 1120fdd048e1SVlastimil Babka } 1121369fa227SOscar Salvador 1122369fa227SOscar Salvador if (ret == -ENOMEM) 1123369fa227SOscar Salvador break; 112431b8384aSHillf Danton } 1125748446bbSMel Gorman 112699c0fd5eSVlastimil Babka /* 112799c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 112899c0fd5eSVlastimil Babka * the range to be scanned. 112999c0fd5eSVlastimil Babka */ 113099c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 113199c0fd5eSVlastimil Babka low_pfn = end_pfn; 113299c0fd5eSVlastimil Babka 11339df41314SAlex Shi page = NULL; 11349df41314SAlex Shi 1135e380bebeSMel Gorman isolate_abort: 1136c67fe375SMel Gorman if (locked) 11376168d0daSAlex Shi unlock_page_lruvec_irqrestore(locked, flags); 11389df41314SAlex Shi if (page) { 11399df41314SAlex Shi SetPageLRU(page); 11409df41314SAlex Shi put_page(page); 11419df41314SAlex Shi } 1142748446bbSMel Gorman 114350b5b094SVlastimil Babka /* 1144804d3121SMel Gorman * Updated the cached scanner pfn once the pageblock has been scanned 1145804d3121SMel Gorman * Pages will either be migrated in which case there is no point 1146804d3121SMel Gorman * scanning in the near future or migration failed in which case the 1147804d3121SMel Gorman * failure reason may persist. The block is marked for skipping if 1148804d3121SMel Gorman * there were no pages isolated in the block or if the block is 1149804d3121SMel Gorman * rescanned twice in a row. 115050b5b094SVlastimil Babka */ 1151804d3121SMel Gorman if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { 1152e380bebeSMel Gorman if (valid_page && !skip_updated) 1153e380bebeSMel Gorman set_pageblock_skip(valid_page); 1154e380bebeSMel Gorman update_cached_migrate(cc, low_pfn); 1155e380bebeSMel Gorman } 1156bb13ffebSMel Gorman 1157e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 1158e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 1159b7aba698SMel Gorman 1160670105a2SMel Gorman fatal_pending: 11617f354a54SDavid Rientjes cc->total_migrate_scanned += nr_scanned; 1162397487dbSMel Gorman if (nr_isolated) 1163010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 1164397487dbSMel Gorman 1165c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn; 1166c2ad7a1fSOscar Salvador 1167c2ad7a1fSOscar Salvador return ret; 11682fe86e00SMichal Nazarewicz } 11692fe86e00SMichal Nazarewicz 1170edc2ca61SVlastimil Babka /** 1171edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 1172edc2ca61SVlastimil Babka * @cc: Compaction control structure. 1173edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 1174edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 1175edc2ca61SVlastimil Babka * 1176369fa227SOscar Salvador * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM 1177369fa227SOscar Salvador * in case we could not allocate a page, or 0. 1178edc2ca61SVlastimil Babka */ 1179c2ad7a1fSOscar Salvador int 1180edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 1181edc2ca61SVlastimil Babka unsigned long end_pfn) 1182edc2ca61SVlastimil Babka { 1183e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 1184c2ad7a1fSOscar Salvador int ret = 0; 1185edc2ca61SVlastimil Babka 1186edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 1187edc2ca61SVlastimil Babka pfn = start_pfn; 118806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 1189e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 1190e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 119106b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 1192edc2ca61SVlastimil Babka 1193edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 1194e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1195edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 1196edc2ca61SVlastimil Babka 1197edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 1198edc2ca61SVlastimil Babka 1199e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 1200e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 1201edc2ca61SVlastimil Babka continue; 1202edc2ca61SVlastimil Babka 1203c2ad7a1fSOscar Salvador ret = isolate_migratepages_block(cc, pfn, block_end_pfn, 1204edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 1205edc2ca61SVlastimil Babka 1206c2ad7a1fSOscar Salvador if (ret) 1207edc2ca61SVlastimil Babka break; 12086ea41c0cSJoonsoo Kim 120938935861SZi Yan if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) 12106ea41c0cSJoonsoo Kim break; 1211edc2ca61SVlastimil Babka } 1212edc2ca61SVlastimil Babka 1213c2ad7a1fSOscar Salvador return ret; 1214edc2ca61SVlastimil Babka } 1215edc2ca61SVlastimil Babka 1216ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1217ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 1218018e9a49SAndrew Morton 1219b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc, 1220b682debdSVlastimil Babka struct page *page) 1221b682debdSVlastimil Babka { 1222282722b0SVlastimil Babka int block_mt; 1223282722b0SVlastimil Babka 12249bebefd5SMel Gorman if (pageblock_skip_persistent(page)) 12259bebefd5SMel Gorman return false; 12269bebefd5SMel Gorman 1227282722b0SVlastimil Babka if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) 1228b682debdSVlastimil Babka return true; 1229b682debdSVlastimil Babka 1230282722b0SVlastimil Babka block_mt = get_pageblock_migratetype(page); 1231282722b0SVlastimil Babka 1232282722b0SVlastimil Babka if (cc->migratetype == MIGRATE_MOVABLE) 1233282722b0SVlastimil Babka return is_migrate_movable(block_mt); 1234282722b0SVlastimil Babka else 1235282722b0SVlastimil Babka return block_mt == cc->migratetype; 1236b682debdSVlastimil Babka } 1237b682debdSVlastimil Babka 1238018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 12399f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc, 12409f7e3387SVlastimil Babka struct page *page) 1241018e9a49SAndrew Morton { 1242018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 1243018e9a49SAndrew Morton if (PageBuddy(page)) { 1244018e9a49SAndrew Morton /* 1245018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 1246018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 1247018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 1248018e9a49SAndrew Morton */ 1249ab130f91SMatthew Wilcox (Oracle) if (buddy_order_unsafe(page) >= pageblock_order) 1250018e9a49SAndrew Morton return false; 1251018e9a49SAndrew Morton } 1252018e9a49SAndrew Morton 12531ef36db2SYisheng Xie if (cc->ignore_block_suitable) 12541ef36db2SYisheng Xie return true; 12551ef36db2SYisheng Xie 1256018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 1257b682debdSVlastimil Babka if (is_migrate_movable(get_pageblock_migratetype(page))) 1258018e9a49SAndrew Morton return true; 1259018e9a49SAndrew Morton 1260018e9a49SAndrew Morton /* Otherwise skip the block */ 1261018e9a49SAndrew Morton return false; 1262018e9a49SAndrew Morton } 1263018e9a49SAndrew Morton 126470b44595SMel Gorman static inline unsigned int 126570b44595SMel Gorman freelist_scan_limit(struct compact_control *cc) 126670b44595SMel Gorman { 1267dd7ef7bdSQian Cai unsigned short shift = BITS_PER_LONG - 1; 1268dd7ef7bdSQian Cai 1269dd7ef7bdSQian Cai return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; 127070b44595SMel Gorman } 127170b44595SMel Gorman 1272ff9543fdSMichal Nazarewicz /* 1273f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 1274f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 1275f2849aa0SVlastimil Babka */ 1276f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 1277f2849aa0SVlastimil Babka { 1278f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 1279f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 1280f2849aa0SVlastimil Babka } 1281f2849aa0SVlastimil Babka 12825a811889SMel Gorman /* 12835a811889SMel Gorman * Used when scanning for a suitable migration target which scans freelists 12845a811889SMel Gorman * in reverse. Reorders the list such as the unscanned pages are scanned 12855a811889SMel Gorman * first on the next iteration of the free scanner 12865a811889SMel Gorman */ 12875a811889SMel Gorman static void 12885a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage) 12895a811889SMel Gorman { 12905a811889SMel Gorman LIST_HEAD(sublist); 12915a811889SMel Gorman 12925a811889SMel Gorman if (!list_is_last(freelist, &freepage->lru)) { 12935a811889SMel Gorman list_cut_before(&sublist, freelist, &freepage->lru); 12945a811889SMel Gorman list_splice_tail(&sublist, freelist); 12955a811889SMel Gorman } 12965a811889SMel Gorman } 12975a811889SMel Gorman 12985a811889SMel Gorman /* 12995a811889SMel Gorman * Similar to move_freelist_head except used by the migration scanner 13005a811889SMel Gorman * when scanning forward. It's possible for these list operations to 13015a811889SMel Gorman * move against each other if they search the free list exactly in 13025a811889SMel Gorman * lockstep. 13035a811889SMel Gorman */ 130470b44595SMel Gorman static void 130570b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage) 130670b44595SMel Gorman { 130770b44595SMel Gorman LIST_HEAD(sublist); 130870b44595SMel Gorman 130970b44595SMel Gorman if (!list_is_first(freelist, &freepage->lru)) { 131070b44595SMel Gorman list_cut_position(&sublist, freelist, &freepage->lru); 131170b44595SMel Gorman list_splice_tail(&sublist, freelist); 131270b44595SMel Gorman } 131370b44595SMel Gorman } 131470b44595SMel Gorman 13155a811889SMel Gorman static void 13165a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) 13175a811889SMel Gorman { 13185a811889SMel Gorman unsigned long start_pfn, end_pfn; 13196e2b7044SVlastimil Babka struct page *page; 13205a811889SMel Gorman 13215a811889SMel Gorman /* Do not search around if there are enough pages already */ 13225a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 13235a811889SMel Gorman return; 13245a811889SMel Gorman 13255a811889SMel Gorman /* Minimise scanning during async compaction */ 13265a811889SMel Gorman if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) 13275a811889SMel Gorman return; 13285a811889SMel Gorman 13295a811889SMel Gorman /* Pageblock boundaries */ 13306e2b7044SVlastimil Babka start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); 13316e2b7044SVlastimil Babka end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); 13326e2b7044SVlastimil Babka 13336e2b7044SVlastimil Babka page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); 13346e2b7044SVlastimil Babka if (!page) 13356e2b7044SVlastimil Babka return; 13365a811889SMel Gorman 13375a811889SMel Gorman /* Scan before */ 13385a811889SMel Gorman if (start_pfn != pfn) { 13394fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false); 13405a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 13415a811889SMel Gorman return; 13425a811889SMel Gorman } 13435a811889SMel Gorman 13445a811889SMel Gorman /* Scan after */ 13455a811889SMel Gorman start_pfn = pfn + nr_isolated; 134660fce36aSMel Gorman if (start_pfn < end_pfn) 13474fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false); 13485a811889SMel Gorman 13495a811889SMel Gorman /* Skip this pageblock in the future as it's full or nearly full */ 13505a811889SMel Gorman if (cc->nr_freepages < cc->nr_migratepages) 13515a811889SMel Gorman set_pageblock_skip(page); 13525a811889SMel Gorman } 13535a811889SMel Gorman 1354dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */ 1355dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order) 1356dbe2d4e4SMel Gorman { 1357dbe2d4e4SMel Gorman order--; 1358dbe2d4e4SMel Gorman if (order < 0) 1359dbe2d4e4SMel Gorman order = cc->order - 1; 1360dbe2d4e4SMel Gorman 1361dbe2d4e4SMel Gorman /* Search wrapped around? */ 1362dbe2d4e4SMel Gorman if (order == cc->search_order) { 1363dbe2d4e4SMel Gorman cc->search_order--; 1364dbe2d4e4SMel Gorman if (cc->search_order < 0) 1365dbe2d4e4SMel Gorman cc->search_order = cc->order - 1; 1366dbe2d4e4SMel Gorman return -1; 1367dbe2d4e4SMel Gorman } 1368dbe2d4e4SMel Gorman 1369dbe2d4e4SMel Gorman return order; 1370dbe2d4e4SMel Gorman } 1371dbe2d4e4SMel Gorman 13725a811889SMel Gorman static unsigned long 13735a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc) 13745a811889SMel Gorman { 1375b55ca526SWonhyuk Yang unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1); 13765a811889SMel Gorman unsigned int nr_scanned = 0; 137774e21484SRokudo Yan unsigned long low_pfn, min_pfn, highest = 0; 13785a811889SMel Gorman unsigned long nr_isolated = 0; 13795a811889SMel Gorman unsigned long distance; 13805a811889SMel Gorman struct page *page = NULL; 13815a811889SMel Gorman bool scan_start = false; 13825a811889SMel Gorman int order; 13835a811889SMel Gorman 13845a811889SMel Gorman /* Full compaction passes in a negative order */ 13855a811889SMel Gorman if (cc->order <= 0) 13865a811889SMel Gorman return cc->free_pfn; 13875a811889SMel Gorman 13885a811889SMel Gorman /* 13895a811889SMel Gorman * If starting the scan, use a deeper search and use the highest 13905a811889SMel Gorman * PFN found if a suitable one is not found. 13915a811889SMel Gorman */ 1392e332f741SMel Gorman if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { 13935a811889SMel Gorman limit = pageblock_nr_pages >> 1; 13945a811889SMel Gorman scan_start = true; 13955a811889SMel Gorman } 13965a811889SMel Gorman 13975a811889SMel Gorman /* 13985a811889SMel Gorman * Preferred point is in the top quarter of the scan space but take 13995a811889SMel Gorman * a pfn from the top half if the search is problematic. 14005a811889SMel Gorman */ 14015a811889SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn); 14025a811889SMel Gorman low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); 14035a811889SMel Gorman min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); 14045a811889SMel Gorman 14055a811889SMel Gorman if (WARN_ON_ONCE(min_pfn > low_pfn)) 14065a811889SMel Gorman low_pfn = min_pfn; 14075a811889SMel Gorman 1408dbe2d4e4SMel Gorman /* 1409dbe2d4e4SMel Gorman * Search starts from the last successful isolation order or the next 1410dbe2d4e4SMel Gorman * order to search after a previous failure 1411dbe2d4e4SMel Gorman */ 1412dbe2d4e4SMel Gorman cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); 1413dbe2d4e4SMel Gorman 1414dbe2d4e4SMel Gorman for (order = cc->search_order; 1415dbe2d4e4SMel Gorman !page && order >= 0; 1416dbe2d4e4SMel Gorman order = next_search_order(cc, order)) { 14175a811889SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 14185a811889SMel Gorman struct list_head *freelist; 14195a811889SMel Gorman struct page *freepage; 14205a811889SMel Gorman unsigned long flags; 14215a811889SMel Gorman unsigned int order_scanned = 0; 142274e21484SRokudo Yan unsigned long high_pfn = 0; 14235a811889SMel Gorman 14245a811889SMel Gorman if (!area->nr_free) 14255a811889SMel Gorman continue; 14265a811889SMel Gorman 14275a811889SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 14285a811889SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 14295a811889SMel Gorman list_for_each_entry_reverse(freepage, freelist, lru) { 14305a811889SMel Gorman unsigned long pfn; 14315a811889SMel Gorman 14325a811889SMel Gorman order_scanned++; 14335a811889SMel Gorman nr_scanned++; 14345a811889SMel Gorman pfn = page_to_pfn(freepage); 14355a811889SMel Gorman 14365a811889SMel Gorman if (pfn >= highest) 14376e2b7044SVlastimil Babka highest = max(pageblock_start_pfn(pfn), 14386e2b7044SVlastimil Babka cc->zone->zone_start_pfn); 14395a811889SMel Gorman 14405a811889SMel Gorman if (pfn >= low_pfn) { 14415a811889SMel Gorman cc->fast_search_fail = 0; 1442dbe2d4e4SMel Gorman cc->search_order = order; 14435a811889SMel Gorman page = freepage; 14445a811889SMel Gorman break; 14455a811889SMel Gorman } 14465a811889SMel Gorman 14475a811889SMel Gorman if (pfn >= min_pfn && pfn > high_pfn) { 14485a811889SMel Gorman high_pfn = pfn; 14495a811889SMel Gorman 14505a811889SMel Gorman /* Shorten the scan if a candidate is found */ 14515a811889SMel Gorman limit >>= 1; 14525a811889SMel Gorman } 14535a811889SMel Gorman 14545a811889SMel Gorman if (order_scanned >= limit) 14555a811889SMel Gorman break; 14565a811889SMel Gorman } 14575a811889SMel Gorman 14585a811889SMel Gorman /* Use a minimum pfn if a preferred one was not found */ 14595a811889SMel Gorman if (!page && high_pfn) { 14605a811889SMel Gorman page = pfn_to_page(high_pfn); 14615a811889SMel Gorman 14625a811889SMel Gorman /* Update freepage for the list reorder below */ 14635a811889SMel Gorman freepage = page; 14645a811889SMel Gorman } 14655a811889SMel Gorman 14665a811889SMel Gorman /* Reorder to so a future search skips recent pages */ 14675a811889SMel Gorman move_freelist_head(freelist, freepage); 14685a811889SMel Gorman 14695a811889SMel Gorman /* Isolate the page if available */ 14705a811889SMel Gorman if (page) { 14715a811889SMel Gorman if (__isolate_free_page(page, order)) { 14725a811889SMel Gorman set_page_private(page, order); 14735a811889SMel Gorman nr_isolated = 1 << order; 14745a811889SMel Gorman cc->nr_freepages += nr_isolated; 14755a811889SMel Gorman list_add_tail(&page->lru, &cc->freepages); 14765a811889SMel Gorman count_compact_events(COMPACTISOLATED, nr_isolated); 14775a811889SMel Gorman } else { 14785a811889SMel Gorman /* If isolation fails, abort the search */ 14795b56d996SQian Cai order = cc->search_order + 1; 14805a811889SMel Gorman page = NULL; 14815a811889SMel Gorman } 14825a811889SMel Gorman } 14835a811889SMel Gorman 14845a811889SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 14855a811889SMel Gorman 14865a811889SMel Gorman /* 1487b55ca526SWonhyuk Yang * Smaller scan on next order so the total scan is related 14885a811889SMel Gorman * to freelist_scan_limit. 14895a811889SMel Gorman */ 14905a811889SMel Gorman if (order_scanned >= limit) 1491b55ca526SWonhyuk Yang limit = max(1U, limit >> 1); 14925a811889SMel Gorman } 14935a811889SMel Gorman 14945a811889SMel Gorman if (!page) { 14955a811889SMel Gorman cc->fast_search_fail++; 14965a811889SMel Gorman if (scan_start) { 14975a811889SMel Gorman /* 14985a811889SMel Gorman * Use the highest PFN found above min. If one was 1499f3867755SEthon Paul * not found, be pessimistic for direct compaction 15005a811889SMel Gorman * and use the min mark. 15015a811889SMel Gorman */ 15025a811889SMel Gorman if (highest) { 15035a811889SMel Gorman page = pfn_to_page(highest); 15045a811889SMel Gorman cc->free_pfn = highest; 15055a811889SMel Gorman } else { 1506e577c8b6SSuzuki K Poulose if (cc->direct_compaction && pfn_valid(min_pfn)) { 150773a6e474SBaoquan He page = pageblock_pfn_to_page(min_pfn, 15086e2b7044SVlastimil Babka min(pageblock_end_pfn(min_pfn), 15096e2b7044SVlastimil Babka zone_end_pfn(cc->zone)), 151073a6e474SBaoquan He cc->zone); 15115a811889SMel Gorman cc->free_pfn = min_pfn; 15125a811889SMel Gorman } 15135a811889SMel Gorman } 15145a811889SMel Gorman } 15155a811889SMel Gorman } 15165a811889SMel Gorman 1517d097a6f6SMel Gorman if (highest && highest >= cc->zone->compact_cached_free_pfn) { 1518d097a6f6SMel Gorman highest -= pageblock_nr_pages; 15195a811889SMel Gorman cc->zone->compact_cached_free_pfn = highest; 1520d097a6f6SMel Gorman } 15215a811889SMel Gorman 15225a811889SMel Gorman cc->total_free_scanned += nr_scanned; 15235a811889SMel Gorman if (!page) 15245a811889SMel Gorman return cc->free_pfn; 15255a811889SMel Gorman 15265a811889SMel Gorman low_pfn = page_to_pfn(page); 15275a811889SMel Gorman fast_isolate_around(cc, low_pfn, nr_isolated); 15285a811889SMel Gorman return low_pfn; 15295a811889SMel Gorman } 15305a811889SMel Gorman 1531f2849aa0SVlastimil Babka /* 1532ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 1533ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 1534ff9543fdSMichal Nazarewicz */ 1535edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 1536ff9543fdSMichal Nazarewicz { 1537edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 1538ff9543fdSMichal Nazarewicz struct page *page; 1539c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 1540e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 1541c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 1542c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 1543ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 15444fca9730SMel Gorman unsigned int stride; 15452fe86e00SMichal Nazarewicz 15465a811889SMel Gorman /* Try a small search of the free lists for a candidate */ 15475a811889SMel Gorman isolate_start_pfn = fast_isolate_freepages(cc); 15485a811889SMel Gorman if (cc->nr_freepages) 15495a811889SMel Gorman goto splitmap; 15505a811889SMel Gorman 1551ff9543fdSMichal Nazarewicz /* 1552ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 155349e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 1554e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 1555e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 1556c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 1557c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 1558a1c1dbebSRandy Dunlap * zone which ends in the middle of a pageblock. 155949e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 156049e068f0SVlastimil Babka * is using. 1561ff9543fdSMichal Nazarewicz */ 1562e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 15635a811889SMel Gorman block_start_pfn = pageblock_start_pfn(isolate_start_pfn); 1564c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1565c96b9e50SVlastimil Babka zone_end_pfn(zone)); 156606b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 15674fca9730SMel Gorman stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; 15682fe86e00SMichal Nazarewicz 1569ff9543fdSMichal Nazarewicz /* 1570ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1571ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1572ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1573ff9543fdSMichal Nazarewicz */ 1574f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1575c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1576e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1577e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 15784fca9730SMel Gorman unsigned long nr_isolated; 15794fca9730SMel Gorman 1580f6ea3adbSDavid Rientjes /* 1581f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1582cb810ad2SMel Gorman * suitable migration targets, so periodically check resched. 1583f6ea3adbSDavid Rientjes */ 1584cb810ad2SMel Gorman if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) 1585cf66f070SMel Gorman cond_resched(); 1586f6ea3adbSDavid Rientjes 15877d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 15887d49d886SVlastimil Babka zone); 15897d49d886SVlastimil Babka if (!page) 1590ff9543fdSMichal Nazarewicz continue; 1591ff9543fdSMichal Nazarewicz 1592ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 15939f7e3387SVlastimil Babka if (!suitable_migration_target(cc, page)) 1594ff9543fdSMichal Nazarewicz continue; 159568e3e926SLinus Torvalds 1596bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1597bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1598bb13ffebSMel Gorman continue; 1599bb13ffebSMel Gorman 1600e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 16014fca9730SMel Gorman nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, 16024fca9730SMel Gorman block_end_pfn, freelist, stride, false); 1603ff9543fdSMichal Nazarewicz 1604d097a6f6SMel Gorman /* Update the skip hint if the full pageblock was scanned */ 1605d097a6f6SMel Gorman if (isolate_start_pfn == block_end_pfn) 1606d097a6f6SMel Gorman update_pageblock_skip(cc, page, block_start_pfn); 1607d097a6f6SMel Gorman 1608cb2dcaf0SMel Gorman /* Are enough freepages isolated? */ 1609cb2dcaf0SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) { 1610a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1611a46cbf3bSDavid Rientjes /* 1612a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1613a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1614a46cbf3bSDavid Rientjes */ 1615f5f61a32SVlastimil Babka isolate_start_pfn = 1616e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1617a46cbf3bSDavid Rientjes } 1618be976572SVlastimil Babka break; 1619a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1620f5f61a32SVlastimil Babka /* 1621a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1622a46cbf3bSDavid Rientjes * needlessly. 1623f5f61a32SVlastimil Babka */ 1624a46cbf3bSDavid Rientjes break; 1625f5f61a32SVlastimil Babka } 16264fca9730SMel Gorman 16274fca9730SMel Gorman /* Adjust stride depending on isolation */ 16284fca9730SMel Gorman if (nr_isolated) { 16294fca9730SMel Gorman stride = 1; 16304fca9730SMel Gorman continue; 16314fca9730SMel Gorman } 16324fca9730SMel Gorman stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); 1633c89511abSMel Gorman } 1634ff9543fdSMichal Nazarewicz 16357ed695e0SVlastimil Babka /* 1636f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1637f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1638f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1639f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 16407ed695e0SVlastimil Babka */ 1641f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 16425a811889SMel Gorman 16435a811889SMel Gorman splitmap: 16445a811889SMel Gorman /* __isolate_free_page() does not map the pages */ 16455a811889SMel Gorman split_map_pages(freelist); 1646748446bbSMel Gorman } 1647748446bbSMel Gorman 1648748446bbSMel Gorman /* 1649748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1650748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1651748446bbSMel Gorman */ 1652748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1653666feb21SMichal Hocko unsigned long data) 1654748446bbSMel Gorman { 1655748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1656748446bbSMel Gorman struct page *freepage; 1657748446bbSMel Gorman 1658748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1659edc2ca61SVlastimil Babka isolate_freepages(cc); 1660748446bbSMel Gorman 1661748446bbSMel Gorman if (list_empty(&cc->freepages)) 1662748446bbSMel Gorman return NULL; 1663748446bbSMel Gorman } 1664748446bbSMel Gorman 1665748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1666748446bbSMel Gorman list_del(&freepage->lru); 1667748446bbSMel Gorman cc->nr_freepages--; 1668748446bbSMel Gorman 1669748446bbSMel Gorman return freepage; 1670748446bbSMel Gorman } 1671748446bbSMel Gorman 1672748446bbSMel Gorman /* 1673d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1674d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1675d53aea3dSDavid Rientjes * special handling needed for NUMA. 1676d53aea3dSDavid Rientjes */ 1677d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1678d53aea3dSDavid Rientjes { 1679d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1680d53aea3dSDavid Rientjes 1681d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1682d53aea3dSDavid Rientjes cc->nr_freepages++; 1683d53aea3dSDavid Rientjes } 1684d53aea3dSDavid Rientjes 1685ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1686ff9543fdSMichal Nazarewicz typedef enum { 1687ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1688ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1689ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1690ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1691ff9543fdSMichal Nazarewicz 1692ff9543fdSMichal Nazarewicz /* 16935bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 16945bbe3547SEric B Munson * compactable pages. 16955bbe3547SEric B Munson */ 16966923aa0dSSebastian Andrzej Siewior #ifdef CONFIG_PREEMPT_RT 16976923aa0dSSebastian Andrzej Siewior int sysctl_compact_unevictable_allowed __read_mostly = 0; 16986923aa0dSSebastian Andrzej Siewior #else 16995bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 17006923aa0dSSebastian Andrzej Siewior #endif 17015bbe3547SEric B Munson 170270b44595SMel Gorman static inline void 170370b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) 170470b44595SMel Gorman { 170570b44595SMel Gorman if (cc->fast_start_pfn == ULONG_MAX) 170670b44595SMel Gorman return; 170770b44595SMel Gorman 170870b44595SMel Gorman if (!cc->fast_start_pfn) 170970b44595SMel Gorman cc->fast_start_pfn = pfn; 171070b44595SMel Gorman 171170b44595SMel Gorman cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); 171270b44595SMel Gorman } 171370b44595SMel Gorman 171470b44595SMel Gorman static inline unsigned long 171570b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc) 171670b44595SMel Gorman { 171770b44595SMel Gorman if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) 171870b44595SMel Gorman return cc->migrate_pfn; 171970b44595SMel Gorman 172070b44595SMel Gorman cc->migrate_pfn = cc->fast_start_pfn; 172170b44595SMel Gorman cc->fast_start_pfn = ULONG_MAX; 172270b44595SMel Gorman 172370b44595SMel Gorman return cc->migrate_pfn; 172470b44595SMel Gorman } 172570b44595SMel Gorman 172670b44595SMel Gorman /* 172770b44595SMel Gorman * Briefly search the free lists for a migration source that already has 172870b44595SMel Gorman * some free pages to reduce the number of pages that need migration 172970b44595SMel Gorman * before a pageblock is free. 173070b44595SMel Gorman */ 173170b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc) 173270b44595SMel Gorman { 173370b44595SMel Gorman unsigned int limit = freelist_scan_limit(cc); 173470b44595SMel Gorman unsigned int nr_scanned = 0; 173570b44595SMel Gorman unsigned long distance; 173670b44595SMel Gorman unsigned long pfn = cc->migrate_pfn; 173770b44595SMel Gorman unsigned long high_pfn; 173870b44595SMel Gorman int order; 173915d28d0dSWonhyuk Yang bool found_block = false; 174070b44595SMel Gorman 174170b44595SMel Gorman /* Skip hints are relied on to avoid repeats on the fast search */ 174270b44595SMel Gorman if (cc->ignore_skip_hint) 174370b44595SMel Gorman return pfn; 174470b44595SMel Gorman 174570b44595SMel Gorman /* 174670b44595SMel Gorman * If the migrate_pfn is not at the start of a zone or the start 174770b44595SMel Gorman * of a pageblock then assume this is a continuation of a previous 174870b44595SMel Gorman * scan restarted due to COMPACT_CLUSTER_MAX. 174970b44595SMel Gorman */ 175070b44595SMel Gorman if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) 175170b44595SMel Gorman return pfn; 175270b44595SMel Gorman 175370b44595SMel Gorman /* 175470b44595SMel Gorman * For smaller orders, just linearly scan as the number of pages 175570b44595SMel Gorman * to migrate should be relatively small and does not necessarily 175670b44595SMel Gorman * justify freeing up a large block for a small allocation. 175770b44595SMel Gorman */ 175870b44595SMel Gorman if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) 175970b44595SMel Gorman return pfn; 176070b44595SMel Gorman 176170b44595SMel Gorman /* 176270b44595SMel Gorman * Only allow kcompactd and direct requests for movable pages to 176370b44595SMel Gorman * quickly clear out a MOVABLE pageblock for allocation. This 176470b44595SMel Gorman * reduces the risk that a large movable pageblock is freed for 176570b44595SMel Gorman * an unmovable/reclaimable small allocation. 176670b44595SMel Gorman */ 176770b44595SMel Gorman if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) 176870b44595SMel Gorman return pfn; 176970b44595SMel Gorman 177070b44595SMel Gorman /* 177170b44595SMel Gorman * When starting the migration scanner, pick any pageblock within the 177270b44595SMel Gorman * first half of the search space. Otherwise try and pick a pageblock 177370b44595SMel Gorman * within the first eighth to reduce the chances that a migration 177470b44595SMel Gorman * target later becomes a source. 177570b44595SMel Gorman */ 177670b44595SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn) >> 1; 177770b44595SMel Gorman if (cc->migrate_pfn != cc->zone->zone_start_pfn) 177870b44595SMel Gorman distance >>= 2; 177970b44595SMel Gorman high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); 178070b44595SMel Gorman 178170b44595SMel Gorman for (order = cc->order - 1; 178215d28d0dSWonhyuk Yang order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; 178370b44595SMel Gorman order--) { 178470b44595SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 178570b44595SMel Gorman struct list_head *freelist; 178670b44595SMel Gorman unsigned long flags; 178770b44595SMel Gorman struct page *freepage; 178870b44595SMel Gorman 178970b44595SMel Gorman if (!area->nr_free) 179070b44595SMel Gorman continue; 179170b44595SMel Gorman 179270b44595SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 179370b44595SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 179470b44595SMel Gorman list_for_each_entry(freepage, freelist, lru) { 179570b44595SMel Gorman unsigned long free_pfn; 179670b44595SMel Gorman 179715d28d0dSWonhyuk Yang if (nr_scanned++ >= limit) { 179815d28d0dSWonhyuk Yang move_freelist_tail(freelist, freepage); 179915d28d0dSWonhyuk Yang break; 180015d28d0dSWonhyuk Yang } 180115d28d0dSWonhyuk Yang 180270b44595SMel Gorman free_pfn = page_to_pfn(freepage); 180370b44595SMel Gorman if (free_pfn < high_pfn) { 180470b44595SMel Gorman /* 180570b44595SMel Gorman * Avoid if skipped recently. Ideally it would 180670b44595SMel Gorman * move to the tail but even safe iteration of 180770b44595SMel Gorman * the list assumes an entry is deleted, not 180870b44595SMel Gorman * reordered. 180970b44595SMel Gorman */ 181015d28d0dSWonhyuk Yang if (get_pageblock_skip(freepage)) 181170b44595SMel Gorman continue; 181270b44595SMel Gorman 181370b44595SMel Gorman /* Reorder to so a future search skips recent pages */ 181470b44595SMel Gorman move_freelist_tail(freelist, freepage); 181570b44595SMel Gorman 1816e380bebeSMel Gorman update_fast_start_pfn(cc, free_pfn); 181770b44595SMel Gorman pfn = pageblock_start_pfn(free_pfn); 181870b44595SMel Gorman cc->fast_search_fail = 0; 181915d28d0dSWonhyuk Yang found_block = true; 182070b44595SMel Gorman set_pageblock_skip(freepage); 182170b44595SMel Gorman break; 182270b44595SMel Gorman } 182370b44595SMel Gorman } 182470b44595SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 182570b44595SMel Gorman } 182670b44595SMel Gorman 182770b44595SMel Gorman cc->total_migrate_scanned += nr_scanned; 182870b44595SMel Gorman 182970b44595SMel Gorman /* 183070b44595SMel Gorman * If fast scanning failed then use a cached entry for a page block 183170b44595SMel Gorman * that had free pages as the basis for starting a linear scan. 183270b44595SMel Gorman */ 183315d28d0dSWonhyuk Yang if (!found_block) { 183415d28d0dSWonhyuk Yang cc->fast_search_fail++; 183570b44595SMel Gorman pfn = reinit_migrate_pfn(cc); 183615d28d0dSWonhyuk Yang } 183770b44595SMel Gorman return pfn; 183870b44595SMel Gorman } 183970b44595SMel Gorman 18405bbe3547SEric B Munson /* 1841edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1842edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1843edc2ca61SVlastimil Babka * compact_control. 1844ff9543fdSMichal Nazarewicz */ 184532aaf055SPengfei Li static isolate_migrate_t isolate_migratepages(struct compact_control *cc) 1846ff9543fdSMichal Nazarewicz { 1847e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1848e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1849e1409c32SJoonsoo Kim unsigned long low_pfn; 1850edc2ca61SVlastimil Babka struct page *page; 1851edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 18525bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 18531d2047feSHugh Dickins (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); 185470b44595SMel Gorman bool fast_find_block; 1855ff9543fdSMichal Nazarewicz 1856edc2ca61SVlastimil Babka /* 1857edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 185870b44595SMel Gorman * initialized by compact_zone(). The first failure will use 185970b44595SMel Gorman * the lowest PFN as the starting point for linear scanning. 1860edc2ca61SVlastimil Babka */ 186170b44595SMel Gorman low_pfn = fast_find_migrateblock(cc); 186206b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 186332aaf055SPengfei Li if (block_start_pfn < cc->zone->zone_start_pfn) 186432aaf055SPengfei Li block_start_pfn = cc->zone->zone_start_pfn; 1865ff9543fdSMichal Nazarewicz 186670b44595SMel Gorman /* 186770b44595SMel Gorman * fast_find_migrateblock marks a pageblock skipped so to avoid 186870b44595SMel Gorman * the isolation_suitable check below, check whether the fast 186970b44595SMel Gorman * search was successful. 187070b44595SMel Gorman */ 187170b44595SMel Gorman fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; 187270b44595SMel Gorman 1873ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 187406b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1875ff9543fdSMichal Nazarewicz 1876edc2ca61SVlastimil Babka /* 1877edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1878edc2ca61SVlastimil Babka * Do not cross the free scanner. 1879edc2ca61SVlastimil Babka */ 1880e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 188170b44595SMel Gorman fast_find_block = false, 1882c2ad7a1fSOscar Salvador cc->migrate_pfn = low_pfn = block_end_pfn, 1883e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1884e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1885edc2ca61SVlastimil Babka 1886edc2ca61SVlastimil Babka /* 1887edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1888edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1889cb810ad2SMel Gorman * need to schedule. 1890edc2ca61SVlastimil Babka */ 1891cb810ad2SMel Gorman if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) 1892cf66f070SMel Gorman cond_resched(); 1893edc2ca61SVlastimil Babka 189432aaf055SPengfei Li page = pageblock_pfn_to_page(block_start_pfn, 189532aaf055SPengfei Li block_end_pfn, cc->zone); 18967d49d886SVlastimil Babka if (!page) 1897edc2ca61SVlastimil Babka continue; 1898edc2ca61SVlastimil Babka 1899e380bebeSMel Gorman /* 1900e380bebeSMel Gorman * If isolation recently failed, do not retry. Only check the 1901e380bebeSMel Gorman * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock 1902e380bebeSMel Gorman * to be visited multiple times. Assume skip was checked 1903e380bebeSMel Gorman * before making it "skip" so other compaction instances do 1904e380bebeSMel Gorman * not scan the same block. 1905e380bebeSMel Gorman */ 1906e380bebeSMel Gorman if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && 1907e380bebeSMel Gorman !fast_find_block && !isolation_suitable(cc, page)) 1908edc2ca61SVlastimil Babka continue; 1909edc2ca61SVlastimil Babka 1910edc2ca61SVlastimil Babka /* 19119bebefd5SMel Gorman * For async compaction, also only scan in MOVABLE blocks 19129bebefd5SMel Gorman * without huge pages. Async compaction is optimistic to see 19139bebefd5SMel Gorman * if the minimum amount of work satisfies the allocation. 19149bebefd5SMel Gorman * The cached PFN is updated as it's possible that all 19159bebefd5SMel Gorman * remaining blocks between source and target are unsuitable 19169bebefd5SMel Gorman * and the compaction scanners fail to meet. 1917edc2ca61SVlastimil Babka */ 19189bebefd5SMel Gorman if (!suitable_migration_source(cc, page)) { 19199bebefd5SMel Gorman update_cached_migrate(cc, block_end_pfn); 1920edc2ca61SVlastimil Babka continue; 19219bebefd5SMel Gorman } 1922ff9543fdSMichal Nazarewicz 1923ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1924c2ad7a1fSOscar Salvador if (isolate_migratepages_block(cc, low_pfn, block_end_pfn, 1925c2ad7a1fSOscar Salvador isolate_mode)) 1926ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1927ff9543fdSMichal Nazarewicz 1928edc2ca61SVlastimil Babka /* 1929edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1930edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1931edc2ca61SVlastimil Babka * continue or not. 1932edc2ca61SVlastimil Babka */ 1933edc2ca61SVlastimil Babka break; 1934edc2ca61SVlastimil Babka } 1935edc2ca61SVlastimil Babka 1936edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1937ff9543fdSMichal Nazarewicz } 1938ff9543fdSMichal Nazarewicz 193921c527a3SYaowei Bai /* 194021c527a3SYaowei Bai * order == -1 is expected when compacting via 194121c527a3SYaowei Bai * /proc/sys/vm/compact_memory 194221c527a3SYaowei Bai */ 194321c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 194421c527a3SYaowei Bai { 194521c527a3SYaowei Bai return order == -1; 194621c527a3SYaowei Bai } 194721c527a3SYaowei Bai 1948facdaa91SNitin Gupta static bool kswapd_is_running(pg_data_t *pgdat) 1949facdaa91SNitin Gupta { 1950b03fbd4fSPeter Zijlstra return pgdat->kswapd && task_is_running(pgdat->kswapd); 1951facdaa91SNitin Gupta } 1952facdaa91SNitin Gupta 1953facdaa91SNitin Gupta /* 1954facdaa91SNitin Gupta * A zone's fragmentation score is the external fragmentation wrt to the 195540d7e203SCharan Teja Reddy * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. 195640d7e203SCharan Teja Reddy */ 195740d7e203SCharan Teja Reddy static unsigned int fragmentation_score_zone(struct zone *zone) 195840d7e203SCharan Teja Reddy { 195940d7e203SCharan Teja Reddy return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); 196040d7e203SCharan Teja Reddy } 196140d7e203SCharan Teja Reddy 196240d7e203SCharan Teja Reddy /* 196340d7e203SCharan Teja Reddy * A weighted zone's fragmentation score is the external fragmentation 196440d7e203SCharan Teja Reddy * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It 196540d7e203SCharan Teja Reddy * returns a value in the range [0, 100]. 1966facdaa91SNitin Gupta * 1967facdaa91SNitin Gupta * The scaling factor ensures that proactive compaction focuses on larger 1968facdaa91SNitin Gupta * zones like ZONE_NORMAL, rather than smaller, specialized zones like 1969facdaa91SNitin Gupta * ZONE_DMA32. For smaller zones, the score value remains close to zero, 1970facdaa91SNitin Gupta * and thus never exceeds the high threshold for proactive compaction. 1971facdaa91SNitin Gupta */ 197240d7e203SCharan Teja Reddy static unsigned int fragmentation_score_zone_weighted(struct zone *zone) 1973facdaa91SNitin Gupta { 1974facdaa91SNitin Gupta unsigned long score; 1975facdaa91SNitin Gupta 197640d7e203SCharan Teja Reddy score = zone->present_pages * fragmentation_score_zone(zone); 1977facdaa91SNitin Gupta return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); 1978facdaa91SNitin Gupta } 1979facdaa91SNitin Gupta 1980facdaa91SNitin Gupta /* 1981facdaa91SNitin Gupta * The per-node proactive (background) compaction process is started by its 1982facdaa91SNitin Gupta * corresponding kcompactd thread when the node's fragmentation score 1983facdaa91SNitin Gupta * exceeds the high threshold. The compaction process remains active till 1984facdaa91SNitin Gupta * the node's score falls below the low threshold, or one of the back-off 1985facdaa91SNitin Gupta * conditions is met. 1986facdaa91SNitin Gupta */ 1987d34c0a75SNitin Gupta static unsigned int fragmentation_score_node(pg_data_t *pgdat) 1988facdaa91SNitin Gupta { 1989d34c0a75SNitin Gupta unsigned int score = 0; 1990facdaa91SNitin Gupta int zoneid; 1991facdaa91SNitin Gupta 1992facdaa91SNitin Gupta for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 1993facdaa91SNitin Gupta struct zone *zone; 1994facdaa91SNitin Gupta 1995facdaa91SNitin Gupta zone = &pgdat->node_zones[zoneid]; 199640d7e203SCharan Teja Reddy score += fragmentation_score_zone_weighted(zone); 1997facdaa91SNitin Gupta } 1998facdaa91SNitin Gupta 1999facdaa91SNitin Gupta return score; 2000facdaa91SNitin Gupta } 2001facdaa91SNitin Gupta 2002d34c0a75SNitin Gupta static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low) 2003facdaa91SNitin Gupta { 2004d34c0a75SNitin Gupta unsigned int wmark_low; 2005facdaa91SNitin Gupta 2006facdaa91SNitin Gupta /* 2007f0953a1bSIngo Molnar * Cap the low watermark to avoid excessive compaction 2008f0953a1bSIngo Molnar * activity in case a user sets the proactiveness tunable 2009facdaa91SNitin Gupta * close to 100 (maximum). 2010facdaa91SNitin Gupta */ 2011d34c0a75SNitin Gupta wmark_low = max(100U - sysctl_compaction_proactiveness, 5U); 2012d34c0a75SNitin Gupta return low ? wmark_low : min(wmark_low + 10, 100U); 2013facdaa91SNitin Gupta } 2014facdaa91SNitin Gupta 2015facdaa91SNitin Gupta static bool should_proactive_compact_node(pg_data_t *pgdat) 2016facdaa91SNitin Gupta { 2017facdaa91SNitin Gupta int wmark_high; 2018facdaa91SNitin Gupta 2019facdaa91SNitin Gupta if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) 2020facdaa91SNitin Gupta return false; 2021facdaa91SNitin Gupta 2022facdaa91SNitin Gupta wmark_high = fragmentation_score_wmark(pgdat, false); 2023facdaa91SNitin Gupta return fragmentation_score_node(pgdat) > wmark_high; 2024facdaa91SNitin Gupta } 2025facdaa91SNitin Gupta 202640cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc) 2027748446bbSMel Gorman { 20288fb74b9fSMel Gorman unsigned int order; 2029d39773a0SVlastimil Babka const int migratetype = cc->migratetype; 2030cb2dcaf0SMel Gorman int ret; 2031748446bbSMel Gorman 2032753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 2033f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 203455b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 203540cacbcbSMel Gorman reset_cached_positions(cc->zone); 203655b7c4c9SVlastimil Babka 203762997027SMel Gorman /* 203862997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 2039accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 204062997027SMel Gorman * flag itself as the decision to be clear should be directly 204162997027SMel Gorman * based on an allocation request. 204262997027SMel Gorman */ 2043accf6242SVlastimil Babka if (cc->direct_compaction) 204440cacbcbSMel Gorman cc->zone->compact_blockskip_flush = true; 204562997027SMel Gorman 2046c8f7de0bSMichal Hocko if (cc->whole_zone) 2047748446bbSMel Gorman return COMPACT_COMPLETE; 2048c8f7de0bSMichal Hocko else 2049c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 2050bb13ffebSMel Gorman } 2051748446bbSMel Gorman 2052facdaa91SNitin Gupta if (cc->proactive_compaction) { 2053facdaa91SNitin Gupta int score, wmark_low; 2054facdaa91SNitin Gupta pg_data_t *pgdat; 2055facdaa91SNitin Gupta 2056facdaa91SNitin Gupta pgdat = cc->zone->zone_pgdat; 2057facdaa91SNitin Gupta if (kswapd_is_running(pgdat)) 2058facdaa91SNitin Gupta return COMPACT_PARTIAL_SKIPPED; 2059facdaa91SNitin Gupta 2060facdaa91SNitin Gupta score = fragmentation_score_zone(cc->zone); 2061facdaa91SNitin Gupta wmark_low = fragmentation_score_wmark(pgdat, true); 2062facdaa91SNitin Gupta 2063facdaa91SNitin Gupta if (score > wmark_low) 2064facdaa91SNitin Gupta ret = COMPACT_CONTINUE; 2065facdaa91SNitin Gupta else 2066facdaa91SNitin Gupta ret = COMPACT_SUCCESS; 2067facdaa91SNitin Gupta 2068facdaa91SNitin Gupta goto out; 2069facdaa91SNitin Gupta } 2070facdaa91SNitin Gupta 207121c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 207256de7263SMel Gorman return COMPACT_CONTINUE; 207356de7263SMel Gorman 2074baf6a9a1SVlastimil Babka /* 2075efe771c7SMel Gorman * Always finish scanning a pageblock to reduce the possibility of 2076efe771c7SMel Gorman * fallbacks in the future. This is particularly important when 2077efe771c7SMel Gorman * migration source is unmovable/reclaimable but it's not worth 2078efe771c7SMel Gorman * special casing. 2079baf6a9a1SVlastimil Babka */ 2080efe771c7SMel Gorman if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) 2081baf6a9a1SVlastimil Babka return COMPACT_CONTINUE; 2082baf6a9a1SVlastimil Babka 208356de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 2084cb2dcaf0SMel Gorman ret = COMPACT_NO_SUITABLE_PAGE; 208556de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 208640cacbcbSMel Gorman struct free_area *area = &cc->zone->free_area[order]; 20872149cdaeSJoonsoo Kim bool can_steal; 20888fb74b9fSMel Gorman 208956de7263SMel Gorman /* Job done if page is free of the right migratetype */ 2090b03641afSDan Williams if (!free_area_empty(area, migratetype)) 2091cf378319SVlastimil Babka return COMPACT_SUCCESS; 209256de7263SMel Gorman 20932149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 20942149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 20952149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 2096b03641afSDan Williams !free_area_empty(area, MIGRATE_CMA)) 2097cf378319SVlastimil Babka return COMPACT_SUCCESS; 20982149cdaeSJoonsoo Kim #endif 20992149cdaeSJoonsoo Kim /* 21002149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 21012149cdaeSJoonsoo Kim * other migratetype buddy lists. 21022149cdaeSJoonsoo Kim */ 21032149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 2104baf6a9a1SVlastimil Babka true, &can_steal) != -1) { 2105baf6a9a1SVlastimil Babka 2106baf6a9a1SVlastimil Babka /* movable pages are OK in any pageblock */ 2107baf6a9a1SVlastimil Babka if (migratetype == MIGRATE_MOVABLE) 2108cf378319SVlastimil Babka return COMPACT_SUCCESS; 2109baf6a9a1SVlastimil Babka 2110baf6a9a1SVlastimil Babka /* 2111baf6a9a1SVlastimil Babka * We are stealing for a non-movable allocation. Make 2112baf6a9a1SVlastimil Babka * sure we finish compacting the current pageblock 2113baf6a9a1SVlastimil Babka * first so it is as free as possible and we won't 2114baf6a9a1SVlastimil Babka * have to steal another one soon. This only applies 2115baf6a9a1SVlastimil Babka * to sync compaction, as async compaction operates 2116baf6a9a1SVlastimil Babka * on pageblocks of the same migratetype. 2117baf6a9a1SVlastimil Babka */ 2118baf6a9a1SVlastimil Babka if (cc->mode == MIGRATE_ASYNC || 2119baf6a9a1SVlastimil Babka IS_ALIGNED(cc->migrate_pfn, 2120baf6a9a1SVlastimil Babka pageblock_nr_pages)) { 2121baf6a9a1SVlastimil Babka return COMPACT_SUCCESS; 2122baf6a9a1SVlastimil Babka } 2123baf6a9a1SVlastimil Babka 2124cb2dcaf0SMel Gorman ret = COMPACT_CONTINUE; 2125cb2dcaf0SMel Gorman break; 2126baf6a9a1SVlastimil Babka } 212756de7263SMel Gorman } 212856de7263SMel Gorman 2129facdaa91SNitin Gupta out: 2130cb2dcaf0SMel Gorman if (cc->contended || fatal_signal_pending(current)) 2131cb2dcaf0SMel Gorman ret = COMPACT_CONTENDED; 2132cb2dcaf0SMel Gorman 2133cb2dcaf0SMel Gorman return ret; 2134837d026dSJoonsoo Kim } 2135837d026dSJoonsoo Kim 213640cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc) 2137837d026dSJoonsoo Kim { 2138837d026dSJoonsoo Kim int ret; 2139837d026dSJoonsoo Kim 214040cacbcbSMel Gorman ret = __compact_finished(cc); 214140cacbcbSMel Gorman trace_mm_compaction_finished(cc->zone, cc->order, ret); 2142837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 2143837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 2144837d026dSJoonsoo Kim 2145837d026dSJoonsoo Kim return ret; 2146748446bbSMel Gorman } 2147748446bbSMel Gorman 2148ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 2149c603844bSMel Gorman unsigned int alloc_flags, 215097a225e6SJoonsoo Kim int highest_zoneidx, 215186a294a8SMichal Hocko unsigned long wmark_target) 21523e7d3449SMel Gorman { 21533e7d3449SMel Gorman unsigned long watermark; 21543e7d3449SMel Gorman 215521c527a3SYaowei Bai if (is_via_compact_memory(order)) 21563957c776SMichal Hocko return COMPACT_CONTINUE; 21573957c776SMichal Hocko 2158a9214443SMel Gorman watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); 2159ebff3980SVlastimil Babka /* 2160ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 2161ebff3980SVlastimil Babka * should be no need for compaction at all. 2162ebff3980SVlastimil Babka */ 216397a225e6SJoonsoo Kim if (zone_watermark_ok(zone, order, watermark, highest_zoneidx, 2164ebff3980SVlastimil Babka alloc_flags)) 2165cf378319SVlastimil Babka return COMPACT_SUCCESS; 2166ebff3980SVlastimil Babka 21673957c776SMichal Hocko /* 21689861a62cSVlastimil Babka * Watermarks for order-0 must be met for compaction to be able to 2169984fdba6SVlastimil Babka * isolate free pages for migration targets. This means that the 2170984fdba6SVlastimil Babka * watermark and alloc_flags have to match, or be more pessimistic than 2171984fdba6SVlastimil Babka * the check in __isolate_free_page(). We don't use the direct 2172984fdba6SVlastimil Babka * compactor's alloc_flags, as they are not relevant for freepage 217397a225e6SJoonsoo Kim * isolation. We however do use the direct compactor's highest_zoneidx 217497a225e6SJoonsoo Kim * to skip over zones where lowmem reserves would prevent allocation 217597a225e6SJoonsoo Kim * even if compaction succeeds. 21768348faf9SVlastimil Babka * For costly orders, we require low watermark instead of min for 21778348faf9SVlastimil Babka * compaction to proceed to increase its chances. 2178d883c6cfSJoonsoo Kim * ALLOC_CMA is used, as pages in CMA pageblocks are considered 2179d883c6cfSJoonsoo Kim * suitable migration targets 21803e7d3449SMel Gorman */ 21818348faf9SVlastimil Babka watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? 21828348faf9SVlastimil Babka low_wmark_pages(zone) : min_wmark_pages(zone); 21838348faf9SVlastimil Babka watermark += compact_gap(order); 218497a225e6SJoonsoo Kim if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, 2185d883c6cfSJoonsoo Kim ALLOC_CMA, wmark_target)) 21863e7d3449SMel Gorman return COMPACT_SKIPPED; 21873e7d3449SMel Gorman 2188cc5c9f09SVlastimil Babka return COMPACT_CONTINUE; 2189cc5c9f09SVlastimil Babka } 2190cc5c9f09SVlastimil Babka 21912b1a20c3SHui Su /* 21922b1a20c3SHui Su * compaction_suitable: Is this suitable to run compaction on this zone now? 21932b1a20c3SHui Su * Returns 21942b1a20c3SHui Su * COMPACT_SKIPPED - If there are too few free pages for compaction 21952b1a20c3SHui Su * COMPACT_SUCCESS - If the allocation would succeed without compaction 21962b1a20c3SHui Su * COMPACT_CONTINUE - If compaction should run now 21972b1a20c3SHui Su */ 2198cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order, 2199cc5c9f09SVlastimil Babka unsigned int alloc_flags, 220097a225e6SJoonsoo Kim int highest_zoneidx) 2201cc5c9f09SVlastimil Babka { 2202cc5c9f09SVlastimil Babka enum compact_result ret; 2203cc5c9f09SVlastimil Babka int fragindex; 2204cc5c9f09SVlastimil Babka 220597a225e6SJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx, 2206cc5c9f09SVlastimil Babka zone_page_state(zone, NR_FREE_PAGES)); 22073e7d3449SMel Gorman /* 22083e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 22093e7d3449SMel Gorman * low memory or external fragmentation 22103e7d3449SMel Gorman * 2211ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 2212ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 22133e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 22143e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 22153e7d3449SMel Gorman * 221620311420SVlastimil Babka * Only compact if a failure would be due to fragmentation. Also 221720311420SVlastimil Babka * ignore fragindex for non-costly orders where the alternative to 221820311420SVlastimil Babka * a successful reclaim/compaction is OOM. Fragindex and the 221920311420SVlastimil Babka * vm.extfrag_threshold sysctl is meant as a heuristic to prevent 222020311420SVlastimil Babka * excessive compaction for costly orders, but it should not be at the 222120311420SVlastimil Babka * expense of system stability. 22223e7d3449SMel Gorman */ 222320311420SVlastimil Babka if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { 22243e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 22253e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 2226cc5c9f09SVlastimil Babka ret = COMPACT_NOT_SUITABLE_ZONE; 22273e7d3449SMel Gorman } 22283e7d3449SMel Gorman 2229837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 2230837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 2231837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 2232837d026dSJoonsoo Kim 2233837d026dSJoonsoo Kim return ret; 2234837d026dSJoonsoo Kim } 2235837d026dSJoonsoo Kim 223686a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 223786a294a8SMichal Hocko int alloc_flags) 223886a294a8SMichal Hocko { 223986a294a8SMichal Hocko struct zone *zone; 224086a294a8SMichal Hocko struct zoneref *z; 224186a294a8SMichal Hocko 224286a294a8SMichal Hocko /* 224386a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 224486a294a8SMichal Hocko * retrying the reclaim. 224586a294a8SMichal Hocko */ 224697a225e6SJoonsoo Kim for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 224797a225e6SJoonsoo Kim ac->highest_zoneidx, ac->nodemask) { 224886a294a8SMichal Hocko unsigned long available; 224986a294a8SMichal Hocko enum compact_result compact_result; 225086a294a8SMichal Hocko 225186a294a8SMichal Hocko /* 225286a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 225386a294a8SMichal Hocko * want to trash just for a single high order allocation which 225486a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 225586a294a8SMichal Hocko * is happy about the watermark check. 225686a294a8SMichal Hocko */ 22575a1c84b4SMel Gorman available = zone_reclaimable_pages(zone) / order; 225886a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 225986a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 226097a225e6SJoonsoo Kim ac->highest_zoneidx, available); 2261cc5c9f09SVlastimil Babka if (compact_result != COMPACT_SKIPPED) 226286a294a8SMichal Hocko return true; 226386a294a8SMichal Hocko } 226486a294a8SMichal Hocko 226586a294a8SMichal Hocko return false; 226686a294a8SMichal Hocko } 226786a294a8SMichal Hocko 22685e1f0f09SMel Gorman static enum compact_result 22695e1f0f09SMel Gorman compact_zone(struct compact_control *cc, struct capture_control *capc) 2270748446bbSMel Gorman { 2271ea7ab982SMichal Hocko enum compact_result ret; 227240cacbcbSMel Gorman unsigned long start_pfn = cc->zone->zone_start_pfn; 227340cacbcbSMel Gorman unsigned long end_pfn = zone_end_pfn(cc->zone); 2274566e54e1SMel Gorman unsigned long last_migrated_pfn; 2275e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 22768854c55fSMel Gorman bool update_cached; 2277748446bbSMel Gorman 2278a94b5252SYafang Shao /* 2279a94b5252SYafang Shao * These counters track activities during zone compaction. Initialize 2280a94b5252SYafang Shao * them before compacting a new zone. 2281a94b5252SYafang Shao */ 2282a94b5252SYafang Shao cc->total_migrate_scanned = 0; 2283a94b5252SYafang Shao cc->total_free_scanned = 0; 2284a94b5252SYafang Shao cc->nr_migratepages = 0; 2285a94b5252SYafang Shao cc->nr_freepages = 0; 2286a94b5252SYafang Shao INIT_LIST_HEAD(&cc->freepages); 2287a94b5252SYafang Shao INIT_LIST_HEAD(&cc->migratepages); 2288a94b5252SYafang Shao 228901c0bfe0SWei Yang cc->migratetype = gfp_migratetype(cc->gfp_mask); 229040cacbcbSMel Gorman ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, 229197a225e6SJoonsoo Kim cc->highest_zoneidx); 22923e7d3449SMel Gorman /* Compaction is likely to fail */ 2293cf378319SVlastimil Babka if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) 22943e7d3449SMel Gorman return ret; 2295c46649deSMichal Hocko 2296c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 2297c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 22983e7d3449SMel Gorman 2299c89511abSMel Gorman /* 2300d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 2301accf6242SVlastimil Babka * is about to be retried after being deferred. 2302d3132e4bSVlastimil Babka */ 230340cacbcbSMel Gorman if (compaction_restarting(cc->zone, cc->order)) 230440cacbcbSMel Gorman __reset_isolation_suitable(cc->zone); 2305d3132e4bSVlastimil Babka 2306d3132e4bSVlastimil Babka /* 2307c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 230806ed2998SVlastimil Babka * information on where the scanners should start (unless we explicitly 230906ed2998SVlastimil Babka * want to compact the whole zone), but check that it is initialised 231006ed2998SVlastimil Babka * by ensuring the values are within zone boundaries. 2311c89511abSMel Gorman */ 231270b44595SMel Gorman cc->fast_start_pfn = 0; 231306ed2998SVlastimil Babka if (cc->whole_zone) { 231406ed2998SVlastimil Babka cc->migrate_pfn = start_pfn; 231506ed2998SVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 231606ed2998SVlastimil Babka } else { 231740cacbcbSMel Gorman cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; 231840cacbcbSMel Gorman cc->free_pfn = cc->zone->compact_cached_free_pfn; 2319623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 232006b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 232140cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = cc->free_pfn; 2322c89511abSMel Gorman } 2323623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 2324c89511abSMel Gorman cc->migrate_pfn = start_pfn; 232540cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 232640cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 2327c89511abSMel Gorman } 2328c8f7de0bSMichal Hocko 2329e332f741SMel Gorman if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) 2330c8f7de0bSMichal Hocko cc->whole_zone = true; 233106ed2998SVlastimil Babka } 2332c8f7de0bSMichal Hocko 2333566e54e1SMel Gorman last_migrated_pfn = 0; 2334748446bbSMel Gorman 23358854c55fSMel Gorman /* 23368854c55fSMel Gorman * Migrate has separate cached PFNs for ASYNC and SYNC* migration on 23378854c55fSMel Gorman * the basis that some migrations will fail in ASYNC mode. However, 23388854c55fSMel Gorman * if the cached PFNs match and pageblocks are skipped due to having 23398854c55fSMel Gorman * no isolation candidates, then the sync state does not matter. 23408854c55fSMel Gorman * Until a pageblock with isolation candidates is found, keep the 23418854c55fSMel Gorman * cached PFNs in sync to avoid revisiting the same blocks. 23428854c55fSMel Gorman */ 23438854c55fSMel Gorman update_cached = !sync && 23448854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; 23458854c55fSMel Gorman 234616c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 234716c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 23480eb927c0SMel Gorman 2349361a2a22SMinchan Kim /* lru_add_drain_all could be expensive with involving other CPUs */ 2350361a2a22SMinchan Kim lru_add_drain(); 2351748446bbSMel Gorman 235240cacbcbSMel Gorman while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { 23539d502c1cSMinchan Kim int err; 235419d3cf9dSYanfei Xu unsigned long iteration_start_pfn = cc->migrate_pfn; 2355748446bbSMel Gorman 2356804d3121SMel Gorman /* 2357804d3121SMel Gorman * Avoid multiple rescans which can happen if a page cannot be 2358804d3121SMel Gorman * isolated (dirty/writeback in async mode) or if the migrated 2359804d3121SMel Gorman * pages are being allocated before the pageblock is cleared. 2360804d3121SMel Gorman * The first rescan will capture the entire pageblock for 2361804d3121SMel Gorman * migration. If it fails, it'll be marked skip and scanning 2362804d3121SMel Gorman * will proceed as normal. 2363804d3121SMel Gorman */ 2364804d3121SMel Gorman cc->rescan = false; 2365804d3121SMel Gorman if (pageblock_start_pfn(last_migrated_pfn) == 236619d3cf9dSYanfei Xu pageblock_start_pfn(iteration_start_pfn)) { 2367804d3121SMel Gorman cc->rescan = true; 2368804d3121SMel Gorman } 2369804d3121SMel Gorman 237032aaf055SPengfei Li switch (isolate_migratepages(cc)) { 2371f9e35b3bSMel Gorman case ISOLATE_ABORT: 23722d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 23735733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 2374e64c5237SShaohua Li cc->nr_migratepages = 0; 2375f9e35b3bSMel Gorman goto out; 2376f9e35b3bSMel Gorman case ISOLATE_NONE: 23778854c55fSMel Gorman if (update_cached) { 23788854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = 23798854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0]; 23808854c55fSMel Gorman } 23818854c55fSMel Gorman 2382fdaf7f5cSVlastimil Babka /* 2383fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 2384fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 2385fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 2386fdaf7f5cSVlastimil Babka */ 2387fdaf7f5cSVlastimil Babka goto check_drain; 2388f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 23898854c55fSMel Gorman update_cached = false; 239019d3cf9dSYanfei Xu last_migrated_pfn = iteration_start_pfn; 2391f9e35b3bSMel Gorman } 2392748446bbSMel Gorman 2393d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 2394e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 23955ac95884SYang Shi MR_COMPACTION, NULL); 2396748446bbSMel Gorman 2397f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 2398f8c9301fSVlastimil Babka &cc->migratepages); 2399748446bbSMel Gorman 2400f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 2401f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 24029d502c1cSMinchan Kim if (err) { 24035733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 24047ed695e0SVlastimil Babka /* 24057ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 24067ed695e0SVlastimil Babka * and we want compact_finished() to detect it 24077ed695e0SVlastimil Babka */ 2408f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 24092d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 24104bf2bba3SDavid Rientjes goto out; 2411748446bbSMel Gorman } 2412fdd048e1SVlastimil Babka /* 2413fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 2414fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 2415fdd048e1SVlastimil Babka */ 2416fdd048e1SVlastimil Babka if (cc->direct_compaction && 2417fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 2418fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 2419fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 2420fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 2421566e54e1SMel Gorman last_migrated_pfn = 0; 2422fdd048e1SVlastimil Babka } 24234bf2bba3SDavid Rientjes } 2424fdaf7f5cSVlastimil Babka 2425fdaf7f5cSVlastimil Babka check_drain: 2426fdaf7f5cSVlastimil Babka /* 2427fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 2428fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 2429fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 2430fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 2431fdaf7f5cSVlastimil Babka * would succeed. 2432fdaf7f5cSVlastimil Babka */ 2433566e54e1SMel Gorman if (cc->order > 0 && last_migrated_pfn) { 2434fdaf7f5cSVlastimil Babka unsigned long current_block_start = 243506b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 2436fdaf7f5cSVlastimil Babka 2437566e54e1SMel Gorman if (last_migrated_pfn < current_block_start) { 2438b01b2141SIngo Molnar lru_add_drain_cpu_zone(cc->zone); 2439fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 2440566e54e1SMel Gorman last_migrated_pfn = 0; 2441fdaf7f5cSVlastimil Babka } 2442fdaf7f5cSVlastimil Babka } 2443fdaf7f5cSVlastimil Babka 24445e1f0f09SMel Gorman /* Stop if a page has been captured */ 24455e1f0f09SMel Gorman if (capc && capc->page) { 24465e1f0f09SMel Gorman ret = COMPACT_SUCCESS; 24475e1f0f09SMel Gorman break; 24485e1f0f09SMel Gorman } 2449748446bbSMel Gorman } 2450748446bbSMel Gorman 2451f9e35b3bSMel Gorman out: 24526bace090SVlastimil Babka /* 24536bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 24546bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 24556bace090SVlastimil Babka */ 24566bace090SVlastimil Babka if (cc->nr_freepages > 0) { 24576bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 24586bace090SVlastimil Babka 24596bace090SVlastimil Babka cc->nr_freepages = 0; 24606bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 24616bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 246206b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 24636bace090SVlastimil Babka /* 24646bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 24656bace090SVlastimil Babka * already reset to zone end in compact_finished() 24666bace090SVlastimil Babka */ 246740cacbcbSMel Gorman if (free_pfn > cc->zone->compact_cached_free_pfn) 246840cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = free_pfn; 24696bace090SVlastimil Babka } 2470748446bbSMel Gorman 24717f354a54SDavid Rientjes count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); 24727f354a54SDavid Rientjes count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); 24737f354a54SDavid Rientjes 247416c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 247516c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 24760eb927c0SMel Gorman 2477748446bbSMel Gorman return ret; 2478748446bbSMel Gorman } 247976ab0f53SMel Gorman 2480ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 2481c3486f53SVlastimil Babka gfp_t gfp_mask, enum compact_priority prio, 248297a225e6SJoonsoo Kim unsigned int alloc_flags, int highest_zoneidx, 24835e1f0f09SMel Gorman struct page **capture) 248456de7263SMel Gorman { 2485ea7ab982SMichal Hocko enum compact_result ret; 248656de7263SMel Gorman struct compact_control cc = { 248756de7263SMel Gorman .order = order, 2488dbe2d4e4SMel Gorman .search_order = order, 24896d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 249056de7263SMel Gorman .zone = zone, 2491a5508cd8SVlastimil Babka .mode = (prio == COMPACT_PRIO_ASYNC) ? 2492a5508cd8SVlastimil Babka MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, 2493ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 249497a225e6SJoonsoo Kim .highest_zoneidx = highest_zoneidx, 2495accf6242SVlastimil Babka .direct_compaction = true, 2496a8e025e5SVlastimil Babka .whole_zone = (prio == MIN_COMPACT_PRIORITY), 24979f7e3387SVlastimil Babka .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), 24989f7e3387SVlastimil Babka .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) 249956de7263SMel Gorman }; 25005e1f0f09SMel Gorman struct capture_control capc = { 25015e1f0f09SMel Gorman .cc = &cc, 25025e1f0f09SMel Gorman .page = NULL, 25035e1f0f09SMel Gorman }; 25045e1f0f09SMel Gorman 2505b9e20f0dSVlastimil Babka /* 2506b9e20f0dSVlastimil Babka * Make sure the structs are really initialized before we expose the 2507b9e20f0dSVlastimil Babka * capture control, in case we are interrupted and the interrupt handler 2508b9e20f0dSVlastimil Babka * frees a page. 2509b9e20f0dSVlastimil Babka */ 2510b9e20f0dSVlastimil Babka barrier(); 2511b9e20f0dSVlastimil Babka WRITE_ONCE(current->capture_control, &capc); 251256de7263SMel Gorman 25135e1f0f09SMel Gorman ret = compact_zone(&cc, &capc); 2514e64c5237SShaohua Li 2515e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 2516e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 2517e64c5237SShaohua Li 2518b9e20f0dSVlastimil Babka /* 2519b9e20f0dSVlastimil Babka * Make sure we hide capture control first before we read the captured 2520b9e20f0dSVlastimil Babka * page pointer, otherwise an interrupt could free and capture a page 2521b9e20f0dSVlastimil Babka * and we would leak it. 2522b9e20f0dSVlastimil Babka */ 2523b9e20f0dSVlastimil Babka WRITE_ONCE(current->capture_control, NULL); 2524b9e20f0dSVlastimil Babka *capture = READ_ONCE(capc.page); 252506dac2f4SCharan Teja Reddy /* 252606dac2f4SCharan Teja Reddy * Technically, it is also possible that compaction is skipped but 252706dac2f4SCharan Teja Reddy * the page is still captured out of luck(IRQ came and freed the page). 252806dac2f4SCharan Teja Reddy * Returning COMPACT_SUCCESS in such cases helps in properly accounting 252906dac2f4SCharan Teja Reddy * the COMPACT[STALL|FAIL] when compaction is skipped. 253006dac2f4SCharan Teja Reddy */ 253106dac2f4SCharan Teja Reddy if (*capture) 253206dac2f4SCharan Teja Reddy ret = COMPACT_SUCCESS; 25335e1f0f09SMel Gorman 2534e64c5237SShaohua Li return ret; 253556de7263SMel Gorman } 253656de7263SMel Gorman 25375e771905SMel Gorman int sysctl_extfrag_threshold = 500; 25385e771905SMel Gorman 253956de7263SMel Gorman /** 254056de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 254156de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 25421a6d53a1SVlastimil Babka * @order: The order of the current allocation 25431a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 25441a6d53a1SVlastimil Babka * @ac: The context of current allocation 2545112d2d29SYang Shi * @prio: Determines how hard direct compaction should try to succeed 25466467552cSVlastimil Babka * @capture: Pointer to free page created by compaction will be stored here 254756de7263SMel Gorman * 254856de7263SMel Gorman * This is the main entry point for direct page compaction. 254956de7263SMel Gorman */ 2550ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 2551c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 25525e1f0f09SMel Gorman enum compact_priority prio, struct page **capture) 255356de7263SMel Gorman { 255456de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 255556de7263SMel Gorman struct zoneref *z; 255656de7263SMel Gorman struct zone *zone; 25571d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 255856de7263SMel Gorman 255973e64c51SMichal Hocko /* 256073e64c51SMichal Hocko * Check if the GFP flags allow compaction - GFP_NOIO is really 256173e64c51SMichal Hocko * tricky context because the migration might require IO 256273e64c51SMichal Hocko */ 256373e64c51SMichal Hocko if (!may_perform_io) 256453853e2dSVlastimil Babka return COMPACT_SKIPPED; 256556de7263SMel Gorman 2566a5508cd8SVlastimil Babka trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); 2567837d026dSJoonsoo Kim 256856de7263SMel Gorman /* Compact each zone in the list */ 256997a225e6SJoonsoo Kim for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 257097a225e6SJoonsoo Kim ac->highest_zoneidx, ac->nodemask) { 2571ea7ab982SMichal Hocko enum compact_result status; 257256de7263SMel Gorman 2573a8e025e5SVlastimil Babka if (prio > MIN_COMPACT_PRIORITY 2574a8e025e5SVlastimil Babka && compaction_deferred(zone, order)) { 25751d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 257653853e2dSVlastimil Babka continue; 25771d4746d3SMichal Hocko } 257853853e2dSVlastimil Babka 2579a5508cd8SVlastimil Babka status = compact_zone_order(zone, order, gfp_mask, prio, 258097a225e6SJoonsoo Kim alloc_flags, ac->highest_zoneidx, capture); 258156de7263SMel Gorman rc = max(status, rc); 258256de7263SMel Gorman 25837ceb009aSVlastimil Babka /* The allocation should succeed, stop compacting */ 25847ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 258553853e2dSVlastimil Babka /* 258653853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 258753853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 258853853e2dSVlastimil Babka * will repeat this with true if allocation indeed 258953853e2dSVlastimil Babka * succeeds in this zone. 259053853e2dSVlastimil Babka */ 259153853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 25921f9efdefSVlastimil Babka 2593c3486f53SVlastimil Babka break; 25941f9efdefSVlastimil Babka } 25951f9efdefSVlastimil Babka 2596a5508cd8SVlastimil Babka if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || 2597c3486f53SVlastimil Babka status == COMPACT_PARTIAL_SKIPPED)) 259853853e2dSVlastimil Babka /* 259953853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 260053853e2dSVlastimil Babka * so we defer compaction there. If it ends up 260153853e2dSVlastimil Babka * succeeding after all, it will be reset. 260253853e2dSVlastimil Babka */ 260353853e2dSVlastimil Babka defer_compaction(zone, order); 26041f9efdefSVlastimil Babka 26051f9efdefSVlastimil Babka /* 26061f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 26071f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 2608c3486f53SVlastimil Babka * case do not try further zones 26091f9efdefSVlastimil Babka */ 2610c3486f53SVlastimil Babka if ((prio == COMPACT_PRIO_ASYNC && need_resched()) 2611c3486f53SVlastimil Babka || fatal_signal_pending(current)) 26121f9efdefSVlastimil Babka break; 26131f9efdefSVlastimil Babka } 26141f9efdefSVlastimil Babka 261556de7263SMel Gorman return rc; 261656de7263SMel Gorman } 261756de7263SMel Gorman 2618facdaa91SNitin Gupta /* 2619facdaa91SNitin Gupta * Compact all zones within a node till each zone's fragmentation score 2620facdaa91SNitin Gupta * reaches within proactive compaction thresholds (as determined by the 2621facdaa91SNitin Gupta * proactiveness tunable). 2622facdaa91SNitin Gupta * 2623facdaa91SNitin Gupta * It is possible that the function returns before reaching score targets 2624facdaa91SNitin Gupta * due to various back-off conditions, such as, contention on per-node or 2625facdaa91SNitin Gupta * per-zone locks. 2626facdaa91SNitin Gupta */ 2627facdaa91SNitin Gupta static void proactive_compact_node(pg_data_t *pgdat) 2628facdaa91SNitin Gupta { 2629facdaa91SNitin Gupta int zoneid; 2630facdaa91SNitin Gupta struct zone *zone; 2631facdaa91SNitin Gupta struct compact_control cc = { 2632facdaa91SNitin Gupta .order = -1, 2633facdaa91SNitin Gupta .mode = MIGRATE_SYNC_LIGHT, 2634facdaa91SNitin Gupta .ignore_skip_hint = true, 2635facdaa91SNitin Gupta .whole_zone = true, 2636facdaa91SNitin Gupta .gfp_mask = GFP_KERNEL, 2637facdaa91SNitin Gupta .proactive_compaction = true, 2638facdaa91SNitin Gupta }; 2639facdaa91SNitin Gupta 2640facdaa91SNitin Gupta for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2641facdaa91SNitin Gupta zone = &pgdat->node_zones[zoneid]; 2642facdaa91SNitin Gupta if (!populated_zone(zone)) 2643facdaa91SNitin Gupta continue; 2644facdaa91SNitin Gupta 2645facdaa91SNitin Gupta cc.zone = zone; 2646facdaa91SNitin Gupta 2647facdaa91SNitin Gupta compact_zone(&cc, NULL); 2648facdaa91SNitin Gupta 2649facdaa91SNitin Gupta VM_BUG_ON(!list_empty(&cc.freepages)); 2650facdaa91SNitin Gupta VM_BUG_ON(!list_empty(&cc.migratepages)); 2651facdaa91SNitin Gupta } 2652facdaa91SNitin Gupta } 265356de7263SMel Gorman 265476ab0f53SMel Gorman /* Compact all zones within a node */ 26557103f16dSAndrew Morton static void compact_node(int nid) 26567be62de9SRik van Riel { 2657791cae96SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2658791cae96SVlastimil Babka int zoneid; 2659791cae96SVlastimil Babka struct zone *zone; 26607be62de9SRik van Riel struct compact_control cc = { 26617be62de9SRik van Riel .order = -1, 2662e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 266391ca9186SDavid Rientjes .ignore_skip_hint = true, 266406ed2998SVlastimil Babka .whole_zone = true, 266573e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 26667be62de9SRik van Riel }; 26677be62de9SRik van Riel 2668791cae96SVlastimil Babka 2669791cae96SVlastimil Babka for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2670791cae96SVlastimil Babka 2671791cae96SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2672791cae96SVlastimil Babka if (!populated_zone(zone)) 2673791cae96SVlastimil Babka continue; 2674791cae96SVlastimil Babka 2675791cae96SVlastimil Babka cc.zone = zone; 2676791cae96SVlastimil Babka 26775e1f0f09SMel Gorman compact_zone(&cc, NULL); 2678791cae96SVlastimil Babka 2679791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2680791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2681791cae96SVlastimil Babka } 26827be62de9SRik van Riel } 26837be62de9SRik van Riel 268476ab0f53SMel Gorman /* Compact all nodes in the system */ 26857964c06dSJason Liu static void compact_nodes(void) 268676ab0f53SMel Gorman { 268776ab0f53SMel Gorman int nid; 268876ab0f53SMel Gorman 26898575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 26908575ec29SHugh Dickins lru_add_drain_all(); 26918575ec29SHugh Dickins 269276ab0f53SMel Gorman for_each_online_node(nid) 269376ab0f53SMel Gorman compact_node(nid); 269476ab0f53SMel Gorman } 269576ab0f53SMel Gorman 2696fec4eb2cSYaowei Bai /* 2697facdaa91SNitin Gupta * Tunable for proactive compaction. It determines how 2698facdaa91SNitin Gupta * aggressively the kernel should compact memory in the 2699facdaa91SNitin Gupta * background. It takes values in the range [0, 100]. 2700facdaa91SNitin Gupta */ 2701d34c0a75SNitin Gupta unsigned int __read_mostly sysctl_compaction_proactiveness = 20; 2702facdaa91SNitin Gupta 270365d759c8SCharan Teja Reddy int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write, 270465d759c8SCharan Teja Reddy void *buffer, size_t *length, loff_t *ppos) 270565d759c8SCharan Teja Reddy { 270665d759c8SCharan Teja Reddy int rc, nid; 270765d759c8SCharan Teja Reddy 270865d759c8SCharan Teja Reddy rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 270965d759c8SCharan Teja Reddy if (rc) 271065d759c8SCharan Teja Reddy return rc; 271165d759c8SCharan Teja Reddy 271265d759c8SCharan Teja Reddy if (write && sysctl_compaction_proactiveness) { 271365d759c8SCharan Teja Reddy for_each_online_node(nid) { 271465d759c8SCharan Teja Reddy pg_data_t *pgdat = NODE_DATA(nid); 271565d759c8SCharan Teja Reddy 271665d759c8SCharan Teja Reddy if (pgdat->proactive_compact_trigger) 271765d759c8SCharan Teja Reddy continue; 271865d759c8SCharan Teja Reddy 271965d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger = true; 272065d759c8SCharan Teja Reddy wake_up_interruptible(&pgdat->kcompactd_wait); 272165d759c8SCharan Teja Reddy } 272265d759c8SCharan Teja Reddy } 272365d759c8SCharan Teja Reddy 272465d759c8SCharan Teja Reddy return 0; 272565d759c8SCharan Teja Reddy } 272665d759c8SCharan Teja Reddy 2727facdaa91SNitin Gupta /* 2728fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 2729fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 2730fec4eb2cSYaowei Bai */ 273176ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 273232927393SChristoph Hellwig void *buffer, size_t *length, loff_t *ppos) 273376ab0f53SMel Gorman { 273476ab0f53SMel Gorman if (write) 27357964c06dSJason Liu compact_nodes(); 273676ab0f53SMel Gorman 273776ab0f53SMel Gorman return 0; 273876ab0f53SMel Gorman } 2739ed4a6d7fSMel Gorman 2740ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 274117adb230SYueHaibing static ssize_t compact_store(struct device *dev, 274210fbcf4cSKay Sievers struct device_attribute *attr, 2743ed4a6d7fSMel Gorman const char *buf, size_t count) 2744ed4a6d7fSMel Gorman { 27458575ec29SHugh Dickins int nid = dev->id; 27468575ec29SHugh Dickins 27478575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 27488575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 27498575ec29SHugh Dickins lru_add_drain_all(); 27508575ec29SHugh Dickins 27518575ec29SHugh Dickins compact_node(nid); 27528575ec29SHugh Dickins } 2753ed4a6d7fSMel Gorman 2754ed4a6d7fSMel Gorman return count; 2755ed4a6d7fSMel Gorman } 275617adb230SYueHaibing static DEVICE_ATTR_WO(compact); 2757ed4a6d7fSMel Gorman 2758ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 2759ed4a6d7fSMel Gorman { 276010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 2761ed4a6d7fSMel Gorman } 2762ed4a6d7fSMel Gorman 2763ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 2764ed4a6d7fSMel Gorman { 276510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 2766ed4a6d7fSMel Gorman } 2767ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 2768ff9543fdSMichal Nazarewicz 2769698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 2770698b1b30SVlastimil Babka { 277165d759c8SCharan Teja Reddy return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || 277265d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger; 2773698b1b30SVlastimil Babka } 2774698b1b30SVlastimil Babka 2775698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 2776698b1b30SVlastimil Babka { 2777698b1b30SVlastimil Babka int zoneid; 2778698b1b30SVlastimil Babka struct zone *zone; 277997a225e6SJoonsoo Kim enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; 2780698b1b30SVlastimil Babka 278197a225e6SJoonsoo Kim for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { 2782698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2783698b1b30SVlastimil Babka 2784698b1b30SVlastimil Babka if (!populated_zone(zone)) 2785698b1b30SVlastimil Babka continue; 2786698b1b30SVlastimil Babka 2787698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 278897a225e6SJoonsoo Kim highest_zoneidx) == COMPACT_CONTINUE) 2789698b1b30SVlastimil Babka return true; 2790698b1b30SVlastimil Babka } 2791698b1b30SVlastimil Babka 2792698b1b30SVlastimil Babka return false; 2793698b1b30SVlastimil Babka } 2794698b1b30SVlastimil Babka 2795698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 2796698b1b30SVlastimil Babka { 2797698b1b30SVlastimil Babka /* 2798698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 2799698b1b30SVlastimil Babka * order is allocatable. 2800698b1b30SVlastimil Babka */ 2801698b1b30SVlastimil Babka int zoneid; 2802698b1b30SVlastimil Babka struct zone *zone; 2803698b1b30SVlastimil Babka struct compact_control cc = { 2804698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 2805dbe2d4e4SMel Gorman .search_order = pgdat->kcompactd_max_order, 280697a225e6SJoonsoo Kim .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, 2807698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 2808a0647dc9SDavid Rientjes .ignore_skip_hint = false, 280973e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 2810698b1b30SVlastimil Babka }; 2811698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 281297a225e6SJoonsoo Kim cc.highest_zoneidx); 28137f354a54SDavid Rientjes count_compact_event(KCOMPACTD_WAKE); 2814698b1b30SVlastimil Babka 281597a225e6SJoonsoo Kim for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { 2816698b1b30SVlastimil Babka int status; 2817698b1b30SVlastimil Babka 2818698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2819698b1b30SVlastimil Babka if (!populated_zone(zone)) 2820698b1b30SVlastimil Babka continue; 2821698b1b30SVlastimil Babka 2822698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 2823698b1b30SVlastimil Babka continue; 2824698b1b30SVlastimil Babka 2825698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 2826698b1b30SVlastimil Babka COMPACT_CONTINUE) 2827698b1b30SVlastimil Babka continue; 2828698b1b30SVlastimil Babka 2829172400c6SVlastimil Babka if (kthread_should_stop()) 2830172400c6SVlastimil Babka return; 2831a94b5252SYafang Shao 2832a94b5252SYafang Shao cc.zone = zone; 28335e1f0f09SMel Gorman status = compact_zone(&cc, NULL); 2834698b1b30SVlastimil Babka 28357ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 2836698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 2837c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 2838698b1b30SVlastimil Babka /* 2839bc3106b2SDavid Rientjes * Buddy pages may become stranded on pcps that could 2840bc3106b2SDavid Rientjes * otherwise coalesce on the zone's free area for 2841bc3106b2SDavid Rientjes * order >= cc.order. This is ratelimited by the 2842bc3106b2SDavid Rientjes * upcoming deferral. 2843bc3106b2SDavid Rientjes */ 2844bc3106b2SDavid Rientjes drain_all_pages(zone); 2845bc3106b2SDavid Rientjes 2846bc3106b2SDavid Rientjes /* 2847698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 2848698b1b30SVlastimil Babka * sync direct compaction does. 2849698b1b30SVlastimil Babka */ 2850698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 2851698b1b30SVlastimil Babka } 2852698b1b30SVlastimil Babka 28537f354a54SDavid Rientjes count_compact_events(KCOMPACTD_MIGRATE_SCANNED, 28547f354a54SDavid Rientjes cc.total_migrate_scanned); 28557f354a54SDavid Rientjes count_compact_events(KCOMPACTD_FREE_SCANNED, 28567f354a54SDavid Rientjes cc.total_free_scanned); 28577f354a54SDavid Rientjes 2858698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2859698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2860698b1b30SVlastimil Babka } 2861698b1b30SVlastimil Babka 2862698b1b30SVlastimil Babka /* 2863698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 286497a225e6SJoonsoo Kim * the requested order/highest_zoneidx in case it was higher/tighter 286597a225e6SJoonsoo Kim * than our current ones 2866698b1b30SVlastimil Babka */ 2867698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 2868698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 286997a225e6SJoonsoo Kim if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) 287097a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; 2871698b1b30SVlastimil Babka } 2872698b1b30SVlastimil Babka 287397a225e6SJoonsoo Kim void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) 2874698b1b30SVlastimil Babka { 2875698b1b30SVlastimil Babka if (!order) 2876698b1b30SVlastimil Babka return; 2877698b1b30SVlastimil Babka 2878698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 2879698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 2880698b1b30SVlastimil Babka 288197a225e6SJoonsoo Kim if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) 288297a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = highest_zoneidx; 2883698b1b30SVlastimil Babka 28846818600fSDavidlohr Bueso /* 28856818600fSDavidlohr Bueso * Pairs with implicit barrier in wait_event_freezable() 28866818600fSDavidlohr Bueso * such that wakeups are not missed. 28876818600fSDavidlohr Bueso */ 28886818600fSDavidlohr Bueso if (!wq_has_sleeper(&pgdat->kcompactd_wait)) 2889698b1b30SVlastimil Babka return; 2890698b1b30SVlastimil Babka 2891698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 2892698b1b30SVlastimil Babka return; 2893698b1b30SVlastimil Babka 2894698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 289597a225e6SJoonsoo Kim highest_zoneidx); 2896698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 2897698b1b30SVlastimil Babka } 2898698b1b30SVlastimil Babka 2899698b1b30SVlastimil Babka /* 2900698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 2901698b1b30SVlastimil Babka * from the init process. 2902698b1b30SVlastimil Babka */ 2903698b1b30SVlastimil Babka static int kcompactd(void *p) 2904698b1b30SVlastimil Babka { 2905698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t *)p; 2906698b1b30SVlastimil Babka struct task_struct *tsk = current; 2907e1e92bfaSCharan Teja Reddy long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC); 2908e1e92bfaSCharan Teja Reddy long timeout = default_timeout; 2909698b1b30SVlastimil Babka 2910698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 2911698b1b30SVlastimil Babka 2912698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 2913698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 2914698b1b30SVlastimil Babka 2915698b1b30SVlastimil Babka set_freezable(); 2916698b1b30SVlastimil Babka 2917698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 291897a225e6SJoonsoo Kim pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; 2919698b1b30SVlastimil Babka 2920698b1b30SVlastimil Babka while (!kthread_should_stop()) { 2921eb414681SJohannes Weiner unsigned long pflags; 2922eb414681SJohannes Weiner 292365d759c8SCharan Teja Reddy /* 292465d759c8SCharan Teja Reddy * Avoid the unnecessary wakeup for proactive compaction 292565d759c8SCharan Teja Reddy * when it is disabled. 292665d759c8SCharan Teja Reddy */ 292765d759c8SCharan Teja Reddy if (!sysctl_compaction_proactiveness) 292865d759c8SCharan Teja Reddy timeout = MAX_SCHEDULE_TIMEOUT; 2929698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 2930facdaa91SNitin Gupta if (wait_event_freezable_timeout(pgdat->kcompactd_wait, 293165d759c8SCharan Teja Reddy kcompactd_work_requested(pgdat), timeout) && 293265d759c8SCharan Teja Reddy !pgdat->proactive_compact_trigger) { 2933698b1b30SVlastimil Babka 2934eb414681SJohannes Weiner psi_memstall_enter(&pflags); 2935698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 2936eb414681SJohannes Weiner psi_memstall_leave(&pflags); 2937e1e92bfaSCharan Teja Reddy /* 2938e1e92bfaSCharan Teja Reddy * Reset the timeout value. The defer timeout from 2939e1e92bfaSCharan Teja Reddy * proactive compaction is lost here but that is fine 2940e1e92bfaSCharan Teja Reddy * as the condition of the zone changing substantionally 2941e1e92bfaSCharan Teja Reddy * then carrying on with the previous defer interval is 2942e1e92bfaSCharan Teja Reddy * not useful. 2943e1e92bfaSCharan Teja Reddy */ 2944e1e92bfaSCharan Teja Reddy timeout = default_timeout; 2945facdaa91SNitin Gupta continue; 2946facdaa91SNitin Gupta } 2947facdaa91SNitin Gupta 2948e1e92bfaSCharan Teja Reddy /* 2949e1e92bfaSCharan Teja Reddy * Start the proactive work with default timeout. Based 2950e1e92bfaSCharan Teja Reddy * on the fragmentation score, this timeout is updated. 2951e1e92bfaSCharan Teja Reddy */ 2952e1e92bfaSCharan Teja Reddy timeout = default_timeout; 2953facdaa91SNitin Gupta if (should_proactive_compact_node(pgdat)) { 2954facdaa91SNitin Gupta unsigned int prev_score, score; 2955facdaa91SNitin Gupta 2956facdaa91SNitin Gupta prev_score = fragmentation_score_node(pgdat); 2957facdaa91SNitin Gupta proactive_compact_node(pgdat); 2958facdaa91SNitin Gupta score = fragmentation_score_node(pgdat); 2959facdaa91SNitin Gupta /* 2960facdaa91SNitin Gupta * Defer proactive compaction if the fragmentation 2961facdaa91SNitin Gupta * score did not go down i.e. no progress made. 2962facdaa91SNitin Gupta */ 2963e1e92bfaSCharan Teja Reddy if (unlikely(score >= prev_score)) 2964e1e92bfaSCharan Teja Reddy timeout = 2965e1e92bfaSCharan Teja Reddy default_timeout << COMPACT_MAX_DEFER_SHIFT; 2966facdaa91SNitin Gupta } 296765d759c8SCharan Teja Reddy if (unlikely(pgdat->proactive_compact_trigger)) 296865d759c8SCharan Teja Reddy pgdat->proactive_compact_trigger = false; 2969698b1b30SVlastimil Babka } 2970698b1b30SVlastimil Babka 2971698b1b30SVlastimil Babka return 0; 2972698b1b30SVlastimil Babka } 2973698b1b30SVlastimil Babka 2974698b1b30SVlastimil Babka /* 2975698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 2976698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 2977698b1b30SVlastimil Babka */ 2978698b1b30SVlastimil Babka int kcompactd_run(int nid) 2979698b1b30SVlastimil Babka { 2980698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2981698b1b30SVlastimil Babka int ret = 0; 2982698b1b30SVlastimil Babka 2983698b1b30SVlastimil Babka if (pgdat->kcompactd) 2984698b1b30SVlastimil Babka return 0; 2985698b1b30SVlastimil Babka 2986698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 2987698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 2988698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 2989698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 2990698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 2991698b1b30SVlastimil Babka } 2992698b1b30SVlastimil Babka return ret; 2993698b1b30SVlastimil Babka } 2994698b1b30SVlastimil Babka 2995698b1b30SVlastimil Babka /* 2996698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 2997698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 2998698b1b30SVlastimil Babka */ 2999698b1b30SVlastimil Babka void kcompactd_stop(int nid) 3000698b1b30SVlastimil Babka { 3001698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 3002698b1b30SVlastimil Babka 3003698b1b30SVlastimil Babka if (kcompactd) { 3004698b1b30SVlastimil Babka kthread_stop(kcompactd); 3005698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 3006698b1b30SVlastimil Babka } 3007698b1b30SVlastimil Babka } 3008698b1b30SVlastimil Babka 3009698b1b30SVlastimil Babka /* 3010698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 3011698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 3012698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 3013698b1b30SVlastimil Babka * restore their cpu bindings. 3014698b1b30SVlastimil Babka */ 3015e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu) 3016698b1b30SVlastimil Babka { 3017698b1b30SVlastimil Babka int nid; 3018698b1b30SVlastimil Babka 3019698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 3020698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 3021698b1b30SVlastimil Babka const struct cpumask *mask; 3022698b1b30SVlastimil Babka 3023698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 3024698b1b30SVlastimil Babka 3025698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 3026698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 3027698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 3028698b1b30SVlastimil Babka } 3029e46b1db2SAnna-Maria Gleixner return 0; 3030698b1b30SVlastimil Babka } 3031698b1b30SVlastimil Babka 3032698b1b30SVlastimil Babka static int __init kcompactd_init(void) 3033698b1b30SVlastimil Babka { 3034698b1b30SVlastimil Babka int nid; 3035e46b1db2SAnna-Maria Gleixner int ret; 3036e46b1db2SAnna-Maria Gleixner 3037e46b1db2SAnna-Maria Gleixner ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3038e46b1db2SAnna-Maria Gleixner "mm/compaction:online", 3039e46b1db2SAnna-Maria Gleixner kcompactd_cpu_online, NULL); 3040e46b1db2SAnna-Maria Gleixner if (ret < 0) { 3041e46b1db2SAnna-Maria Gleixner pr_err("kcompactd: failed to register hotplug callbacks.\n"); 3042e46b1db2SAnna-Maria Gleixner return ret; 3043e46b1db2SAnna-Maria Gleixner } 3044698b1b30SVlastimil Babka 3045698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 3046698b1b30SVlastimil Babka kcompactd_run(nid); 3047698b1b30SVlastimil Babka return 0; 3048698b1b30SVlastimil Babka } 3049698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 3050698b1b30SVlastimil Babka 3051ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 3052