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 53748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 54748446bbSMel Gorman { 55748446bbSMel Gorman struct page *page, *next; 566bace090SVlastimil Babka unsigned long high_pfn = 0; 57748446bbSMel Gorman 58748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 596bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 60748446bbSMel Gorman list_del(&page->lru); 61748446bbSMel Gorman __free_page(page); 626bace090SVlastimil Babka if (pfn > high_pfn) 636bace090SVlastimil Babka high_pfn = pfn; 64748446bbSMel Gorman } 65748446bbSMel Gorman 666bace090SVlastimil Babka return high_pfn; 67748446bbSMel Gorman } 68748446bbSMel Gorman 694469ab98SMel Gorman static void split_map_pages(struct list_head *list) 70ff9543fdSMichal Nazarewicz { 7166c64223SJoonsoo Kim unsigned int i, order, nr_pages; 7266c64223SJoonsoo Kim struct page *page, *next; 7366c64223SJoonsoo Kim LIST_HEAD(tmp_list); 74ff9543fdSMichal Nazarewicz 7566c64223SJoonsoo Kim list_for_each_entry_safe(page, next, list, lru) { 7666c64223SJoonsoo Kim list_del(&page->lru); 7766c64223SJoonsoo Kim 7866c64223SJoonsoo Kim order = page_private(page); 7966c64223SJoonsoo Kim nr_pages = 1 << order; 8066c64223SJoonsoo Kim 8146f24fd8SJoonsoo Kim post_alloc_hook(page, order, __GFP_MOVABLE); 8266c64223SJoonsoo Kim if (order) 8366c64223SJoonsoo Kim split_page(page, order); 8466c64223SJoonsoo Kim 8566c64223SJoonsoo Kim for (i = 0; i < nr_pages; i++) { 8666c64223SJoonsoo Kim list_add(&page->lru, &tmp_list); 8766c64223SJoonsoo Kim page++; 88ff9543fdSMichal Nazarewicz } 89ff9543fdSMichal Nazarewicz } 90ff9543fdSMichal Nazarewicz 9166c64223SJoonsoo Kim list_splice(&tmp_list, list); 9266c64223SJoonsoo Kim } 9366c64223SJoonsoo Kim 94bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 9524e2716fSJoonsoo Kim 96bda807d4SMinchan Kim int PageMovable(struct page *page) 97bda807d4SMinchan Kim { 98bda807d4SMinchan Kim struct address_space *mapping; 99bda807d4SMinchan Kim 100bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 101bda807d4SMinchan Kim if (!__PageMovable(page)) 102bda807d4SMinchan Kim return 0; 103bda807d4SMinchan Kim 104bda807d4SMinchan Kim mapping = page_mapping(page); 105bda807d4SMinchan Kim if (mapping && mapping->a_ops && mapping->a_ops->isolate_page) 106bda807d4SMinchan Kim return 1; 107bda807d4SMinchan Kim 108bda807d4SMinchan Kim return 0; 109bda807d4SMinchan Kim } 110bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable); 111bda807d4SMinchan Kim 112bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping) 113bda807d4SMinchan Kim { 114bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 115bda807d4SMinchan Kim VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page); 116bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE); 117bda807d4SMinchan Kim } 118bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable); 119bda807d4SMinchan Kim 120bda807d4SMinchan Kim void __ClearPageMovable(struct page *page) 121bda807d4SMinchan Kim { 122bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 123bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageMovable(page), page); 124bda807d4SMinchan Kim /* 125bda807d4SMinchan Kim * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE 126bda807d4SMinchan Kim * flag so that VM can catch up released page by driver after isolation. 127bda807d4SMinchan Kim * With it, VM migration doesn't try to put it back. 128bda807d4SMinchan Kim */ 129bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)page->mapping & 130bda807d4SMinchan Kim PAGE_MAPPING_MOVABLE); 131bda807d4SMinchan Kim } 132bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable); 133bda807d4SMinchan Kim 13424e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 13524e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 13624e2716fSJoonsoo Kim 13724e2716fSJoonsoo Kim /* 13824e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 13924e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 14024e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 14124e2716fSJoonsoo Kim */ 14224e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 14324e2716fSJoonsoo Kim { 14424e2716fSJoonsoo Kim zone->compact_considered = 0; 14524e2716fSJoonsoo Kim zone->compact_defer_shift++; 14624e2716fSJoonsoo Kim 14724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14824e2716fSJoonsoo Kim zone->compact_order_failed = order; 14924e2716fSJoonsoo Kim 15024e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 15124e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 15224e2716fSJoonsoo Kim 15324e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 15424e2716fSJoonsoo Kim } 15524e2716fSJoonsoo Kim 15624e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 15724e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 15824e2716fSJoonsoo Kim { 15924e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 16024e2716fSJoonsoo Kim 16124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 16224e2716fSJoonsoo Kim return false; 16324e2716fSJoonsoo Kim 16424e2716fSJoonsoo Kim /* Avoid possible overflow */ 16524e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 16624e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 16724e2716fSJoonsoo Kim 16824e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 16924e2716fSJoonsoo Kim return false; 17024e2716fSJoonsoo Kim 17124e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 17224e2716fSJoonsoo Kim 17324e2716fSJoonsoo Kim return true; 17424e2716fSJoonsoo Kim } 17524e2716fSJoonsoo Kim 17624e2716fSJoonsoo Kim /* 17724e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 17824e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 17924e2716fSJoonsoo Kim * expected to succeed. 18024e2716fSJoonsoo Kim */ 18124e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 18224e2716fSJoonsoo Kim bool alloc_success) 18324e2716fSJoonsoo Kim { 18424e2716fSJoonsoo Kim if (alloc_success) { 18524e2716fSJoonsoo Kim zone->compact_considered = 0; 18624e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 18724e2716fSJoonsoo Kim } 18824e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 18924e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 19024e2716fSJoonsoo Kim 19124e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 19224e2716fSJoonsoo Kim } 19324e2716fSJoonsoo Kim 19424e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 19524e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 19624e2716fSJoonsoo Kim { 19724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 19824e2716fSJoonsoo Kim return false; 19924e2716fSJoonsoo Kim 20024e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 20124e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 20224e2716fSJoonsoo Kim } 20324e2716fSJoonsoo Kim 204bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 205bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 206bb13ffebSMel Gorman struct page *page) 207bb13ffebSMel Gorman { 208bb13ffebSMel Gorman if (cc->ignore_skip_hint) 209bb13ffebSMel Gorman return true; 210bb13ffebSMel Gorman 211bb13ffebSMel Gorman return !get_pageblock_skip(page); 212bb13ffebSMel Gorman } 213bb13ffebSMel Gorman 21402333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 21502333641SVlastimil Babka { 21602333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 21702333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 218623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 21906b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 22002333641SVlastimil Babka } 22102333641SVlastimil Babka 222bb13ffebSMel Gorman /* 223b527cfe5SVlastimil Babka * Compound pages of >= pageblock_order should consistenly be skipped until 224b527cfe5SVlastimil Babka * released. It is always pointless to compact pages of such order (if they are 225b527cfe5SVlastimil Babka * migratable), and the pageblocks they occupy cannot contain any free pages. 22621dc7e02SDavid Rientjes */ 227b527cfe5SVlastimil Babka static bool pageblock_skip_persistent(struct page *page) 22821dc7e02SDavid Rientjes { 229b527cfe5SVlastimil Babka if (!PageCompound(page)) 23021dc7e02SDavid Rientjes return false; 231b527cfe5SVlastimil Babka 232b527cfe5SVlastimil Babka page = compound_head(page); 233b527cfe5SVlastimil Babka 234b527cfe5SVlastimil Babka if (compound_order(page) >= pageblock_order) 23521dc7e02SDavid Rientjes return true; 236b527cfe5SVlastimil Babka 237b527cfe5SVlastimil Babka return false; 23821dc7e02SDavid Rientjes } 23921dc7e02SDavid Rientjes 24021dc7e02SDavid Rientjes /* 241bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 242bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 243bb13ffebSMel Gorman * meet. 244bb13ffebSMel Gorman */ 24562997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 246bb13ffebSMel Gorman { 247bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 248108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 249bb13ffebSMel Gorman unsigned long pfn; 250bb13ffebSMel Gorman 25162997027SMel Gorman zone->compact_blockskip_flush = false; 252bb13ffebSMel Gorman 253bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 254bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 255bb13ffebSMel Gorman struct page *page; 256bb13ffebSMel Gorman 257bb13ffebSMel Gorman cond_resched(); 258bb13ffebSMel Gorman 259ccbe1e4dSMichal Hocko page = pfn_to_online_page(pfn); 260ccbe1e4dSMichal Hocko if (!page) 261bb13ffebSMel Gorman continue; 262bb13ffebSMel Gorman if (zone != page_zone(page)) 263bb13ffebSMel Gorman continue; 264b527cfe5SVlastimil Babka if (pageblock_skip_persistent(page)) 26521dc7e02SDavid Rientjes continue; 266bb13ffebSMel Gorman 267bb13ffebSMel Gorman clear_pageblock_skip(page); 268bb13ffebSMel Gorman } 26902333641SVlastimil Babka 27002333641SVlastimil Babka reset_cached_positions(zone); 271bb13ffebSMel Gorman } 272bb13ffebSMel Gorman 27362997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 27462997027SMel Gorman { 27562997027SMel Gorman int zoneid; 27662997027SMel Gorman 27762997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 27862997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 27962997027SMel Gorman if (!populated_zone(zone)) 28062997027SMel Gorman continue; 28162997027SMel Gorman 28262997027SMel Gorman /* Only flush if a full compaction finished recently */ 28362997027SMel Gorman if (zone->compact_blockskip_flush) 28462997027SMel Gorman __reset_isolation_suitable(zone); 28562997027SMel Gorman } 28662997027SMel Gorman } 28762997027SMel Gorman 288bb13ffebSMel Gorman /* 289e380bebeSMel Gorman * Sets the pageblock skip bit if it was clear. Note that this is a hint as 290e380bebeSMel Gorman * locks are not required for read/writers. Returns true if it was already set. 291e380bebeSMel Gorman */ 292e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 293e380bebeSMel Gorman unsigned long pfn) 294e380bebeSMel Gorman { 295e380bebeSMel Gorman bool skip; 296e380bebeSMel Gorman 297e380bebeSMel Gorman /* Do no update if skip hint is being ignored */ 298e380bebeSMel Gorman if (cc->ignore_skip_hint) 299e380bebeSMel Gorman return false; 300e380bebeSMel Gorman 301e380bebeSMel Gorman if (!IS_ALIGNED(pfn, pageblock_nr_pages)) 302e380bebeSMel Gorman return false; 303e380bebeSMel Gorman 304e380bebeSMel Gorman skip = get_pageblock_skip(page); 305e380bebeSMel Gorman if (!skip && !cc->no_set_skip_hint) 306e380bebeSMel Gorman set_pageblock_skip(page); 307e380bebeSMel Gorman 308e380bebeSMel Gorman return skip; 309e380bebeSMel Gorman } 310e380bebeSMel Gorman 311e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 312e380bebeSMel Gorman { 313e380bebeSMel Gorman struct zone *zone = cc->zone; 314e380bebeSMel Gorman 315e380bebeSMel Gorman pfn = pageblock_end_pfn(pfn); 316e380bebeSMel Gorman 317e380bebeSMel Gorman /* Set for isolation rather than compaction */ 318e380bebeSMel Gorman if (cc->no_set_skip_hint) 319e380bebeSMel Gorman return; 320e380bebeSMel Gorman 321e380bebeSMel Gorman if (pfn > zone->compact_cached_migrate_pfn[0]) 322e380bebeSMel Gorman zone->compact_cached_migrate_pfn[0] = pfn; 323e380bebeSMel Gorman if (cc->mode != MIGRATE_ASYNC && 324e380bebeSMel Gorman pfn > zone->compact_cached_migrate_pfn[1]) 325e380bebeSMel Gorman zone->compact_cached_migrate_pfn[1] = pfn; 326e380bebeSMel Gorman } 327e380bebeSMel Gorman 328e380bebeSMel Gorman /* 329bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 33062997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 331bb13ffebSMel Gorman */ 332c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 333e380bebeSMel Gorman struct page *page, unsigned long nr_isolated) 334bb13ffebSMel Gorman { 335c89511abSMel Gorman struct zone *zone = cc->zone; 33635979ef3SDavid Rientjes unsigned long pfn; 3376815bf3fSJoonsoo Kim 3382583d671SVlastimil Babka if (cc->no_set_skip_hint) 3396815bf3fSJoonsoo Kim return; 3406815bf3fSJoonsoo Kim 341bb13ffebSMel Gorman if (!page) 342bb13ffebSMel Gorman return; 343bb13ffebSMel Gorman 34435979ef3SDavid Rientjes if (nr_isolated) 34535979ef3SDavid Rientjes return; 34635979ef3SDavid Rientjes 347bb13ffebSMel Gorman set_pageblock_skip(page); 348c89511abSMel Gorman 34935979ef3SDavid Rientjes pfn = page_to_pfn(page); 35035979ef3SDavid Rientjes 35135979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 35235979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 353c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 354c89511abSMel Gorman } 355bb13ffebSMel Gorman #else 356bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 357bb13ffebSMel Gorman struct page *page) 358bb13ffebSMel Gorman { 359bb13ffebSMel Gorman return true; 360bb13ffebSMel Gorman } 361bb13ffebSMel Gorman 362b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page) 36321dc7e02SDavid Rientjes { 36421dc7e02SDavid Rientjes return false; 36521dc7e02SDavid Rientjes } 36621dc7e02SDavid Rientjes 36721dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc, 368e380bebeSMel Gorman struct page *page, unsigned long nr_isolated) 369bb13ffebSMel Gorman { 370bb13ffebSMel Gorman } 371e380bebeSMel Gorman 372e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 373e380bebeSMel Gorman { 374e380bebeSMel Gorman } 375e380bebeSMel Gorman 376e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 377e380bebeSMel Gorman unsigned long pfn) 378e380bebeSMel Gorman { 379e380bebeSMel Gorman return false; 380e380bebeSMel Gorman } 381bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 382bb13ffebSMel Gorman 3831f9efdefSVlastimil Babka /* 3848b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 385cb2dcaf0SMel Gorman * very heavily contended. For async compaction, trylock and record if the 386cb2dcaf0SMel Gorman * lock is contended. The lock will still be acquired but compaction will 387cb2dcaf0SMel Gorman * abort when the current block is finished regardless of success rate. 388cb2dcaf0SMel Gorman * Sync compaction acquires the lock. 3898b44d279SVlastimil Babka * 390cb2dcaf0SMel Gorman * Always returns true which makes it easier to track lock state in callers. 3911f9efdefSVlastimil Babka */ 392cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, 3938b44d279SVlastimil Babka struct compact_control *cc) 3948b44d279SVlastimil Babka { 395cb2dcaf0SMel Gorman /* Track if the lock is contended in async mode */ 396cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC && !cc->contended) { 397cb2dcaf0SMel Gorman if (spin_trylock_irqsave(lock, *flags)) 398cb2dcaf0SMel Gorman return true; 399cb2dcaf0SMel Gorman 400c3486f53SVlastimil Babka cc->contended = true; 4018b44d279SVlastimil Babka } 4021f9efdefSVlastimil Babka 403cb2dcaf0SMel Gorman spin_lock_irqsave(lock, *flags); 4048b44d279SVlastimil Babka return true; 4052a1402aaSMel Gorman } 4062a1402aaSMel Gorman 40785aa125fSMichal Nazarewicz /* 408c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 4098b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 4108b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 4118b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 4128b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 4138b44d279SVlastimil Babka * aborts. Sync compaction schedules. 4148b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 4158b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 416c67fe375SMel Gorman * 4178b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 4188b44d279SVlastimil Babka * async compaction due to need_resched() 4198b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 4208b44d279SVlastimil Babka * scheduled) 421c67fe375SMel Gorman */ 4228b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 4238b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 424c67fe375SMel Gorman { 4258b44d279SVlastimil Babka if (*locked) { 4268b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 4278b44d279SVlastimil Babka *locked = false; 428c67fe375SMel Gorman } 429c67fe375SMel Gorman 4308b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 431c3486f53SVlastimil Babka cc->contended = true; 4328b44d279SVlastimil Babka return true; 4338b44d279SVlastimil Babka } 4348b44d279SVlastimil Babka 4358b44d279SVlastimil Babka if (need_resched()) { 436cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC) 437c3486f53SVlastimil Babka cc->contended = true; 438c67fe375SMel Gorman cond_resched(); 439c67fe375SMel Gorman } 440c67fe375SMel Gorman 4418b44d279SVlastimil Babka return false; 442c67fe375SMel Gorman } 443c67fe375SMel Gorman 444be976572SVlastimil Babka /* 445be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 446be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 4478b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 448be976572SVlastimil Babka * is used where no lock is concerned. 449be976572SVlastimil Babka * 450be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 451be976572SVlastimil Babka * Returns true when async compaction should abort. 452be976572SVlastimil Babka */ 453be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 454be976572SVlastimil Babka { 455be976572SVlastimil Babka /* async compaction aborts if contended */ 456be976572SVlastimil Babka if (need_resched()) { 457cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC) 458c3486f53SVlastimil Babka cc->contended = true; 459be976572SVlastimil Babka 460be976572SVlastimil Babka cond_resched(); 461be976572SVlastimil Babka } 462be976572SVlastimil Babka 463be976572SVlastimil Babka return false; 464be976572SVlastimil Babka } 465be976572SVlastimil Babka 466c67fe375SMel Gorman /* 4679e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 4689e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 4699e4be470SJerome Marchand * (even though it may still end up isolating some pages). 47085aa125fSMichal Nazarewicz */ 471f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 472e14c720eSVlastimil Babka unsigned long *start_pfn, 47385aa125fSMichal Nazarewicz unsigned long end_pfn, 47485aa125fSMichal Nazarewicz struct list_head *freelist, 47585aa125fSMichal Nazarewicz bool strict) 476748446bbSMel Gorman { 477b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 478bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 479b8b2d825SXiubo Li unsigned long flags = 0; 480f40d1e42SMel Gorman bool locked = false; 481e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 48266c64223SJoonsoo Kim unsigned int order; 483748446bbSMel Gorman 484748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 485748446bbSMel Gorman 486f40d1e42SMel Gorman /* Isolate free pages. */ 487748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 48866c64223SJoonsoo Kim int isolated; 489748446bbSMel Gorman struct page *page = cursor; 490748446bbSMel Gorman 4918b44d279SVlastimil Babka /* 4928b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 4938b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 4948b44d279SVlastimil Babka * pending or async compaction detects need_resched() 4958b44d279SVlastimil Babka */ 4968b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 4978b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 4988b44d279SVlastimil Babka &locked, cc)) 4998b44d279SVlastimil Babka break; 5008b44d279SVlastimil Babka 501b7aba698SMel Gorman nr_scanned++; 502f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 5032af120bcSLaura Abbott goto isolate_fail; 5042af120bcSLaura Abbott 505bb13ffebSMel Gorman if (!valid_page) 506bb13ffebSMel Gorman valid_page = page; 5079fcd6d2eSVlastimil Babka 5089fcd6d2eSVlastimil Babka /* 5099fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 5109fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 5119fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 5129fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 5139fcd6d2eSVlastimil Babka */ 5149fcd6d2eSVlastimil Babka if (PageCompound(page)) { 51521dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 5169fcd6d2eSVlastimil Babka 517d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) { 51821dc7e02SDavid Rientjes blockpfn += (1UL << order) - 1; 51921dc7e02SDavid Rientjes cursor += (1UL << order) - 1; 5209fcd6d2eSVlastimil Babka } 5219fcd6d2eSVlastimil Babka goto isolate_fail; 5229fcd6d2eSVlastimil Babka } 5239fcd6d2eSVlastimil Babka 524f40d1e42SMel Gorman if (!PageBuddy(page)) 5252af120bcSLaura Abbott goto isolate_fail; 526f40d1e42SMel Gorman 527f40d1e42SMel Gorman /* 52869b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 52969b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 53069b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 53169b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 53269b7189fSVlastimil Babka * recheck as well. 53369b7189fSVlastimil Babka */ 53469b7189fSVlastimil Babka if (!locked) { 535cb2dcaf0SMel Gorman locked = compact_lock_irqsave(&cc->zone->lock, 5368b44d279SVlastimil Babka &flags, cc); 537f40d1e42SMel Gorman 538f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 539f40d1e42SMel Gorman if (!PageBuddy(page)) 5402af120bcSLaura Abbott goto isolate_fail; 54169b7189fSVlastimil Babka } 542748446bbSMel Gorman 54366c64223SJoonsoo Kim /* Found a free page, will break it into order-0 pages */ 54466c64223SJoonsoo Kim order = page_order(page); 54566c64223SJoonsoo Kim isolated = __isolate_free_page(page, order); 546a4f04f2cSDavid Rientjes if (!isolated) 547a4f04f2cSDavid Rientjes break; 54866c64223SJoonsoo Kim set_page_private(page, order); 549a4f04f2cSDavid Rientjes 550748446bbSMel Gorman total_isolated += isolated; 551a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 55266c64223SJoonsoo Kim list_add_tail(&page->lru, freelist); 55366c64223SJoonsoo Kim 554a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 555932ff6bbSJoonsoo Kim blockpfn += isolated; 556932ff6bbSJoonsoo Kim break; 557932ff6bbSJoonsoo Kim } 558a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 559748446bbSMel Gorman blockpfn += isolated - 1; 560748446bbSMel Gorman cursor += isolated - 1; 5612af120bcSLaura Abbott continue; 5622af120bcSLaura Abbott 5632af120bcSLaura Abbott isolate_fail: 5642af120bcSLaura Abbott if (strict) 5652af120bcSLaura Abbott break; 5662af120bcSLaura Abbott else 5672af120bcSLaura Abbott continue; 5682af120bcSLaura Abbott 569748446bbSMel Gorman } 570748446bbSMel Gorman 571a4f04f2cSDavid Rientjes if (locked) 572a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 573a4f04f2cSDavid Rientjes 5749fcd6d2eSVlastimil Babka /* 5759fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 5769fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 5779fcd6d2eSVlastimil Babka */ 5789fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 5799fcd6d2eSVlastimil Babka blockpfn = end_pfn; 5809fcd6d2eSVlastimil Babka 581e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 582e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 583e34d85f0SJoonsoo Kim 584e14c720eSVlastimil Babka /* Record how far we have got within the block */ 585e14c720eSVlastimil Babka *start_pfn = blockpfn; 586e14c720eSVlastimil Babka 587f40d1e42SMel Gorman /* 588f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 589f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 590f40d1e42SMel Gorman * returned and CMA will fail. 591f40d1e42SMel Gorman */ 5922af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 593f40d1e42SMel Gorman total_isolated = 0; 594f40d1e42SMel Gorman 595bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 596bb13ffebSMel Gorman if (blockpfn == end_pfn) 597e380bebeSMel Gorman update_pageblock_skip(cc, valid_page, total_isolated); 598bb13ffebSMel Gorman 5997f354a54SDavid Rientjes cc->total_free_scanned += nr_scanned; 600397487dbSMel Gorman if (total_isolated) 601010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 602748446bbSMel Gorman return total_isolated; 603748446bbSMel Gorman } 604748446bbSMel Gorman 60585aa125fSMichal Nazarewicz /** 60685aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 607e8b098fcSMike Rapoport * @cc: Compaction control structure. 60885aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 60985aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 61085aa125fSMichal Nazarewicz * 61185aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 61285aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 61385aa125fSMichal Nazarewicz * undo its actions and return zero. 61485aa125fSMichal Nazarewicz * 61585aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 61685aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 61785aa125fSMichal Nazarewicz * a free page). 61885aa125fSMichal Nazarewicz */ 619ff9543fdSMichal Nazarewicz unsigned long 620bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 621bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 62285aa125fSMichal Nazarewicz { 623e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 62485aa125fSMichal Nazarewicz LIST_HEAD(freelist); 62585aa125fSMichal Nazarewicz 6267d49d886SVlastimil Babka pfn = start_pfn; 62706b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 628e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 629e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 63006b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 6317d49d886SVlastimil Babka 6327d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 633e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 6347d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 635e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 636e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 6377d49d886SVlastimil Babka 63885aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 63985aa125fSMichal Nazarewicz 64058420016SJoonsoo Kim /* 64158420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 64258420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 64358420016SJoonsoo Kim * scanning range to right one. 64458420016SJoonsoo Kim */ 64558420016SJoonsoo Kim if (pfn >= block_end_pfn) { 64606b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 64706b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 64858420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 64958420016SJoonsoo Kim } 65058420016SJoonsoo Kim 651e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 652e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 6537d49d886SVlastimil Babka break; 6547d49d886SVlastimil Babka 655e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 656e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 65785aa125fSMichal Nazarewicz 65885aa125fSMichal Nazarewicz /* 65985aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 66085aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 66185aa125fSMichal Nazarewicz * non-free pages). 66285aa125fSMichal Nazarewicz */ 66385aa125fSMichal Nazarewicz if (!isolated) 66485aa125fSMichal Nazarewicz break; 66585aa125fSMichal Nazarewicz 66685aa125fSMichal Nazarewicz /* 66785aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 66885aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 66985aa125fSMichal Nazarewicz * page may span two pageblocks). 67085aa125fSMichal Nazarewicz */ 67185aa125fSMichal Nazarewicz } 67285aa125fSMichal Nazarewicz 67366c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 6744469ab98SMel Gorman split_map_pages(&freelist); 67585aa125fSMichal Nazarewicz 67685aa125fSMichal Nazarewicz if (pfn < end_pfn) { 67785aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 67885aa125fSMichal Nazarewicz release_freepages(&freelist); 67985aa125fSMichal Nazarewicz return 0; 68085aa125fSMichal Nazarewicz } 68185aa125fSMichal Nazarewicz 68285aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 68385aa125fSMichal Nazarewicz return pfn; 68485aa125fSMichal Nazarewicz } 68585aa125fSMichal Nazarewicz 686748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 687748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 688748446bbSMel Gorman { 689bc693045SMinchan Kim unsigned long active, inactive, isolated; 690748446bbSMel Gorman 691599d0c95SMel Gorman inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) + 692599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON); 693599d0c95SMel Gorman active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) + 694599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON); 695599d0c95SMel Gorman isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) + 696599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON); 697748446bbSMel Gorman 698bc693045SMinchan Kim return isolated > (inactive + active) / 2; 699748446bbSMel Gorman } 700748446bbSMel Gorman 7012fe86e00SMichal Nazarewicz /** 702edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 703edc2ca61SVlastimil Babka * a single pageblock 7042fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 705edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 706edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 707edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 7082fe86e00SMichal Nazarewicz * 7092fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 710edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 711edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 712edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 713edc2ca61SVlastimil Babka * than end_pfn). 7142fe86e00SMichal Nazarewicz * 715edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 716edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 717edc2ca61SVlastimil Babka * is neither read nor updated. 718748446bbSMel Gorman */ 719edc2ca61SVlastimil Babka static unsigned long 720edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 721edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 722748446bbSMel Gorman { 723edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 724b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 725fa9add64SHugh Dickins struct lruvec *lruvec; 726b8b2d825SXiubo Li unsigned long flags = 0; 7272a1402aaSMel Gorman bool locked = false; 728bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 729e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 730fdd048e1SVlastimil Babka bool skip_on_failure = false; 731fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 732e380bebeSMel Gorman bool skip_updated = false; 733748446bbSMel Gorman 734748446bbSMel Gorman /* 735748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 736748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 737748446bbSMel Gorman * delay for some time until fewer pages are isolated 738748446bbSMel Gorman */ 739748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 740f9e35b3bSMel Gorman /* async migration should just abort */ 741e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 7422fe86e00SMichal Nazarewicz return 0; 743f9e35b3bSMel Gorman 744748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 745748446bbSMel Gorman 746748446bbSMel Gorman if (fatal_signal_pending(current)) 7472fe86e00SMichal Nazarewicz return 0; 748748446bbSMel Gorman } 749748446bbSMel Gorman 750be976572SVlastimil Babka if (compact_should_abort(cc)) 751aeef4b83SDavid Rientjes return 0; 752aeef4b83SDavid Rientjes 753fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 754fdd048e1SVlastimil Babka skip_on_failure = true; 755fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 756fdd048e1SVlastimil Babka } 757fdd048e1SVlastimil Babka 758748446bbSMel Gorman /* Time to isolate some pages for migration */ 759748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 76029c0dde8SVlastimil Babka 761fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 762fdd048e1SVlastimil Babka /* 763fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 764fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 765fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 766fdd048e1SVlastimil Babka * hopefully succeed compaction. 767fdd048e1SVlastimil Babka */ 768fdd048e1SVlastimil Babka if (nr_isolated) 769fdd048e1SVlastimil Babka break; 770fdd048e1SVlastimil Babka 771fdd048e1SVlastimil Babka /* 772fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 773fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 774fdd048e1SVlastimil Babka * current block. Note we can't simply increase 775fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 776fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 777fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 778fdd048e1SVlastimil Babka * previous loop iteration. 779fdd048e1SVlastimil Babka */ 780fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 781fdd048e1SVlastimil Babka } 782fdd048e1SVlastimil Babka 7838b44d279SVlastimil Babka /* 7848b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 7858b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7868b44d279SVlastimil Babka * if contended. 7878b44d279SVlastimil Babka */ 7888b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 789a52633d8SMel Gorman && compact_unlock_should_abort(zone_lru_lock(zone), flags, 7908b44d279SVlastimil Babka &locked, cc)) 7918b44d279SVlastimil Babka break; 792b2eef8c0SAndrea Arcangeli 793748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 794fdd048e1SVlastimil Babka goto isolate_fail; 795b7aba698SMel Gorman nr_scanned++; 796748446bbSMel Gorman 797748446bbSMel Gorman page = pfn_to_page(low_pfn); 798dc908600SMel Gorman 799e380bebeSMel Gorman /* 800e380bebeSMel Gorman * Check if the pageblock has already been marked skipped. 801e380bebeSMel Gorman * Only the aligned PFN is checked as the caller isolates 802e380bebeSMel Gorman * COMPACT_CLUSTER_MAX at a time so the second call must 803e380bebeSMel Gorman * not falsely conclude that the block should be skipped. 804e380bebeSMel Gorman */ 805e380bebeSMel Gorman if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { 806e380bebeSMel Gorman if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { 807e380bebeSMel Gorman low_pfn = end_pfn; 808e380bebeSMel Gorman goto isolate_abort; 809e380bebeSMel Gorman } 810bb13ffebSMel Gorman valid_page = page; 811e380bebeSMel Gorman } 812bb13ffebSMel Gorman 813c122b208SJoonsoo Kim /* 81499c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 81599c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 81699c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 81799c0fd5eSVlastimil Babka * potential isolation targets. 8186c14466cSMel Gorman */ 81999c0fd5eSVlastimil Babka if (PageBuddy(page)) { 82099c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 82199c0fd5eSVlastimil Babka 82299c0fd5eSVlastimil Babka /* 82399c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 82499c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 82599c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 82699c0fd5eSVlastimil Babka */ 82799c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 82899c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 829748446bbSMel Gorman continue; 83099c0fd5eSVlastimil Babka } 831748446bbSMel Gorman 8329927af74SMel Gorman /* 83329c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 83429c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 83529c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 83629c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 83729c0dde8SVlastimil Babka * danger is skipping too much. 838bc835011SAndrea Arcangeli */ 83929c0dde8SVlastimil Babka if (PageCompound(page)) { 84021dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 84129c0dde8SVlastimil Babka 842d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) 84321dc7e02SDavid Rientjes low_pfn += (1UL << order) - 1; 844fdd048e1SVlastimil Babka goto isolate_fail; 8452a1402aaSMel Gorman } 8462a1402aaSMel Gorman 847bda807d4SMinchan Kim /* 848bda807d4SMinchan Kim * Check may be lockless but that's ok as we recheck later. 849bda807d4SMinchan Kim * It's possible to migrate LRU and non-lru movable pages. 850bda807d4SMinchan Kim * Skip any other type of page 851bda807d4SMinchan Kim */ 852bda807d4SMinchan Kim if (!PageLRU(page)) { 853bda807d4SMinchan Kim /* 854bda807d4SMinchan Kim * __PageMovable can return false positive so we need 855bda807d4SMinchan Kim * to verify it under page_lock. 856bda807d4SMinchan Kim */ 857bda807d4SMinchan Kim if (unlikely(__PageMovable(page)) && 858bda807d4SMinchan Kim !PageIsolated(page)) { 859bda807d4SMinchan Kim if (locked) { 860a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), 861bda807d4SMinchan Kim flags); 862bda807d4SMinchan Kim locked = false; 863bda807d4SMinchan Kim } 864bda807d4SMinchan Kim 8659e5bcd61SYisheng Xie if (!isolate_movable_page(page, isolate_mode)) 866bda807d4SMinchan Kim goto isolate_success; 867bda807d4SMinchan Kim } 868bda807d4SMinchan Kim 869fdd048e1SVlastimil Babka goto isolate_fail; 870bda807d4SMinchan Kim } 87129c0dde8SVlastimil Babka 872119d6d59SDavid Rientjes /* 873119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 874119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 875119d6d59SDavid Rientjes * admittedly racy check. 876119d6d59SDavid Rientjes */ 877119d6d59SDavid Rientjes if (!page_mapping(page) && 878119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 879fdd048e1SVlastimil Babka goto isolate_fail; 880119d6d59SDavid Rientjes 88173e64c51SMichal Hocko /* 88273e64c51SMichal Hocko * Only allow to migrate anonymous pages in GFP_NOFS context 88373e64c51SMichal Hocko * because those do not depend on fs locks. 88473e64c51SMichal Hocko */ 88573e64c51SMichal Hocko if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page)) 88673e64c51SMichal Hocko goto isolate_fail; 88773e64c51SMichal Hocko 88869b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 88969b7189fSVlastimil Babka if (!locked) { 890cb2dcaf0SMel Gorman locked = compact_lock_irqsave(zone_lru_lock(zone), 8918b44d279SVlastimil Babka &flags, cc); 892e380bebeSMel Gorman 893e380bebeSMel Gorman /* Try get exclusive access under lock */ 894e380bebeSMel Gorman if (!skip_updated) { 895e380bebeSMel Gorman skip_updated = true; 896e380bebeSMel Gorman if (test_and_set_skip(cc, page, low_pfn)) 897e380bebeSMel Gorman goto isolate_abort; 898e380bebeSMel Gorman } 8992a1402aaSMel Gorman 90029c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 9012a1402aaSMel Gorman if (!PageLRU(page)) 902fdd048e1SVlastimil Babka goto isolate_fail; 90329c0dde8SVlastimil Babka 90429c0dde8SVlastimil Babka /* 90529c0dde8SVlastimil Babka * Page become compound since the non-locked check, 90629c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 90729c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 90829c0dde8SVlastimil Babka */ 90929c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 910d3c85badSVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 911fdd048e1SVlastimil Babka goto isolate_fail; 912bc835011SAndrea Arcangeli } 91369b7189fSVlastimil Babka } 914bc835011SAndrea Arcangeli 915599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 916fa9add64SHugh Dickins 917748446bbSMel Gorman /* Try isolate the page */ 918edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 919fdd048e1SVlastimil Babka goto isolate_fail; 920748446bbSMel Gorman 92129c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 922bc835011SAndrea Arcangeli 923748446bbSMel Gorman /* Successfully isolated */ 924fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 9256afcf8efSMing Ling inc_node_page_state(page, 9266afcf8efSMing Ling NR_ISOLATED_ANON + page_is_file_cache(page)); 927b6c75016SJoonsoo Kim 928b6c75016SJoonsoo Kim isolate_success: 929fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 930748446bbSMel Gorman cc->nr_migratepages++; 931b7aba698SMel Gorman nr_isolated++; 932748446bbSMel Gorman 933804d3121SMel Gorman /* 934804d3121SMel Gorman * Avoid isolating too much unless this block is being 935cb2dcaf0SMel Gorman * rescanned (e.g. dirty/writeback pages, parallel allocation) 936cb2dcaf0SMel Gorman * or a lock is contended. For contention, isolate quickly to 937cb2dcaf0SMel Gorman * potentially remove one source of contention. 938804d3121SMel Gorman */ 939cb2dcaf0SMel Gorman if (cc->nr_migratepages == COMPACT_CLUSTER_MAX && 940cb2dcaf0SMel Gorman !cc->rescan && !cc->contended) { 94131b8384aSHillf Danton ++low_pfn; 942748446bbSMel Gorman break; 943748446bbSMel Gorman } 944fdd048e1SVlastimil Babka 945fdd048e1SVlastimil Babka continue; 946fdd048e1SVlastimil Babka isolate_fail: 947fdd048e1SVlastimil Babka if (!skip_on_failure) 948fdd048e1SVlastimil Babka continue; 949fdd048e1SVlastimil Babka 950fdd048e1SVlastimil Babka /* 951fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 952fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 953fdd048e1SVlastimil Babka * page anyway. 954fdd048e1SVlastimil Babka */ 955fdd048e1SVlastimil Babka if (nr_isolated) { 956fdd048e1SVlastimil Babka if (locked) { 957a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 958fdd048e1SVlastimil Babka locked = false; 959fdd048e1SVlastimil Babka } 960fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 961fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 962fdd048e1SVlastimil Babka nr_isolated = 0; 963fdd048e1SVlastimil Babka } 964fdd048e1SVlastimil Babka 965fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 966fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 967fdd048e1SVlastimil Babka /* 968fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 969fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 970fdd048e1SVlastimil Babka */ 971fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 972fdd048e1SVlastimil Babka } 97331b8384aSHillf Danton } 974748446bbSMel Gorman 97599c0fd5eSVlastimil Babka /* 97699c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 97799c0fd5eSVlastimil Babka * the range to be scanned. 97899c0fd5eSVlastimil Babka */ 97999c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 98099c0fd5eSVlastimil Babka low_pfn = end_pfn; 98199c0fd5eSVlastimil Babka 982e380bebeSMel Gorman isolate_abort: 983c67fe375SMel Gorman if (locked) 984a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 985748446bbSMel Gorman 98650b5b094SVlastimil Babka /* 987804d3121SMel Gorman * Updated the cached scanner pfn once the pageblock has been scanned 988804d3121SMel Gorman * Pages will either be migrated in which case there is no point 989804d3121SMel Gorman * scanning in the near future or migration failed in which case the 990804d3121SMel Gorman * failure reason may persist. The block is marked for skipping if 991804d3121SMel Gorman * there were no pages isolated in the block or if the block is 992804d3121SMel Gorman * rescanned twice in a row. 99350b5b094SVlastimil Babka */ 994804d3121SMel Gorman if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { 995e380bebeSMel Gorman if (valid_page && !skip_updated) 996e380bebeSMel Gorman set_pageblock_skip(valid_page); 997e380bebeSMel Gorman update_cached_migrate(cc, low_pfn); 998e380bebeSMel Gorman } 999bb13ffebSMel Gorman 1000e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 1001e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 1002b7aba698SMel Gorman 10037f354a54SDavid Rientjes cc->total_migrate_scanned += nr_scanned; 1004397487dbSMel Gorman if (nr_isolated) 1005010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 1006397487dbSMel Gorman 10072fe86e00SMichal Nazarewicz return low_pfn; 10082fe86e00SMichal Nazarewicz } 10092fe86e00SMichal Nazarewicz 1010edc2ca61SVlastimil Babka /** 1011edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 1012edc2ca61SVlastimil Babka * @cc: Compaction control structure. 1013edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 1014edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 1015edc2ca61SVlastimil Babka * 1016edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 1017edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 1018edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 1019edc2ca61SVlastimil Babka */ 1020edc2ca61SVlastimil Babka unsigned long 1021edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 1022edc2ca61SVlastimil Babka unsigned long end_pfn) 1023edc2ca61SVlastimil Babka { 1024e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 1025edc2ca61SVlastimil Babka 1026edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 1027edc2ca61SVlastimil Babka pfn = start_pfn; 102806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 1029e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 1030e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 103106b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 1032edc2ca61SVlastimil Babka 1033edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 1034e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1035edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 1036edc2ca61SVlastimil Babka 1037edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 1038edc2ca61SVlastimil Babka 1039e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 1040e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 1041edc2ca61SVlastimil Babka continue; 1042edc2ca61SVlastimil Babka 1043edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 1044edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 1045edc2ca61SVlastimil Babka 104614af4a5eSHugh Dickins if (!pfn) 1047edc2ca61SVlastimil Babka break; 10486ea41c0cSJoonsoo Kim 10496ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 10506ea41c0cSJoonsoo Kim break; 1051edc2ca61SVlastimil Babka } 1052edc2ca61SVlastimil Babka 1053edc2ca61SVlastimil Babka return pfn; 1054edc2ca61SVlastimil Babka } 1055edc2ca61SVlastimil Babka 1056ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1057ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 1058018e9a49SAndrew Morton 1059b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc, 1060b682debdSVlastimil Babka struct page *page) 1061b682debdSVlastimil Babka { 1062282722b0SVlastimil Babka int block_mt; 1063282722b0SVlastimil Babka 1064*9bebefd5SMel Gorman if (pageblock_skip_persistent(page)) 1065*9bebefd5SMel Gorman return false; 1066*9bebefd5SMel Gorman 1067282722b0SVlastimil Babka if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) 1068b682debdSVlastimil Babka return true; 1069b682debdSVlastimil Babka 1070282722b0SVlastimil Babka block_mt = get_pageblock_migratetype(page); 1071282722b0SVlastimil Babka 1072282722b0SVlastimil Babka if (cc->migratetype == MIGRATE_MOVABLE) 1073282722b0SVlastimil Babka return is_migrate_movable(block_mt); 1074282722b0SVlastimil Babka else 1075282722b0SVlastimil Babka return block_mt == cc->migratetype; 1076b682debdSVlastimil Babka } 1077b682debdSVlastimil Babka 1078018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 10799f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc, 10809f7e3387SVlastimil Babka struct page *page) 1081018e9a49SAndrew Morton { 1082018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 1083018e9a49SAndrew Morton if (PageBuddy(page)) { 1084018e9a49SAndrew Morton /* 1085018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 1086018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 1087018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 1088018e9a49SAndrew Morton */ 1089018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 1090018e9a49SAndrew Morton return false; 1091018e9a49SAndrew Morton } 1092018e9a49SAndrew Morton 10931ef36db2SYisheng Xie if (cc->ignore_block_suitable) 10941ef36db2SYisheng Xie return true; 10951ef36db2SYisheng Xie 1096018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 1097b682debdSVlastimil Babka if (is_migrate_movable(get_pageblock_migratetype(page))) 1098018e9a49SAndrew Morton return true; 1099018e9a49SAndrew Morton 1100018e9a49SAndrew Morton /* Otherwise skip the block */ 1101018e9a49SAndrew Morton return false; 1102018e9a49SAndrew Morton } 1103018e9a49SAndrew Morton 110470b44595SMel Gorman static inline unsigned int 110570b44595SMel Gorman freelist_scan_limit(struct compact_control *cc) 110670b44595SMel Gorman { 110770b44595SMel Gorman return (COMPACT_CLUSTER_MAX >> cc->fast_search_fail) + 1; 110870b44595SMel Gorman } 110970b44595SMel Gorman 1110ff9543fdSMichal Nazarewicz /* 1111f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 1112f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 1113f2849aa0SVlastimil Babka */ 1114f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 1115f2849aa0SVlastimil Babka { 1116f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 1117f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 1118f2849aa0SVlastimil Babka } 1119f2849aa0SVlastimil Babka 11205a811889SMel Gorman /* 11215a811889SMel Gorman * Used when scanning for a suitable migration target which scans freelists 11225a811889SMel Gorman * in reverse. Reorders the list such as the unscanned pages are scanned 11235a811889SMel Gorman * first on the next iteration of the free scanner 11245a811889SMel Gorman */ 11255a811889SMel Gorman static void 11265a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage) 11275a811889SMel Gorman { 11285a811889SMel Gorman LIST_HEAD(sublist); 11295a811889SMel Gorman 11305a811889SMel Gorman if (!list_is_last(freelist, &freepage->lru)) { 11315a811889SMel Gorman list_cut_before(&sublist, freelist, &freepage->lru); 11325a811889SMel Gorman if (!list_empty(&sublist)) 11335a811889SMel Gorman list_splice_tail(&sublist, freelist); 11345a811889SMel Gorman } 11355a811889SMel Gorman } 11365a811889SMel Gorman 11375a811889SMel Gorman /* 11385a811889SMel Gorman * Similar to move_freelist_head except used by the migration scanner 11395a811889SMel Gorman * when scanning forward. It's possible for these list operations to 11405a811889SMel Gorman * move against each other if they search the free list exactly in 11415a811889SMel Gorman * lockstep. 11425a811889SMel Gorman */ 114370b44595SMel Gorman static void 114470b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage) 114570b44595SMel Gorman { 114670b44595SMel Gorman LIST_HEAD(sublist); 114770b44595SMel Gorman 114870b44595SMel Gorman if (!list_is_first(freelist, &freepage->lru)) { 114970b44595SMel Gorman list_cut_position(&sublist, freelist, &freepage->lru); 115070b44595SMel Gorman if (!list_empty(&sublist)) 115170b44595SMel Gorman list_splice_tail(&sublist, freelist); 115270b44595SMel Gorman } 115370b44595SMel Gorman } 115470b44595SMel Gorman 11555a811889SMel Gorman static void 11565a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) 11575a811889SMel Gorman { 11585a811889SMel Gorman unsigned long start_pfn, end_pfn; 11595a811889SMel Gorman struct page *page = pfn_to_page(pfn); 11605a811889SMel Gorman 11615a811889SMel Gorman /* Do not search around if there are enough pages already */ 11625a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 11635a811889SMel Gorman return; 11645a811889SMel Gorman 11655a811889SMel Gorman /* Minimise scanning during async compaction */ 11665a811889SMel Gorman if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) 11675a811889SMel Gorman return; 11685a811889SMel Gorman 11695a811889SMel Gorman /* Pageblock boundaries */ 11705a811889SMel Gorman start_pfn = pageblock_start_pfn(pfn); 11715a811889SMel Gorman end_pfn = min(start_pfn + pageblock_nr_pages, zone_end_pfn(cc->zone)); 11725a811889SMel Gorman 11735a811889SMel Gorman /* Scan before */ 11745a811889SMel Gorman if (start_pfn != pfn) { 11755a811889SMel Gorman isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, false); 11765a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 11775a811889SMel Gorman return; 11785a811889SMel Gorman } 11795a811889SMel Gorman 11805a811889SMel Gorman /* Scan after */ 11815a811889SMel Gorman start_pfn = pfn + nr_isolated; 11825a811889SMel Gorman if (start_pfn != end_pfn) 11835a811889SMel Gorman isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, false); 11845a811889SMel Gorman 11855a811889SMel Gorman /* Skip this pageblock in the future as it's full or nearly full */ 11865a811889SMel Gorman if (cc->nr_freepages < cc->nr_migratepages) 11875a811889SMel Gorman set_pageblock_skip(page); 11885a811889SMel Gorman } 11895a811889SMel Gorman 11905a811889SMel Gorman static unsigned long 11915a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc) 11925a811889SMel Gorman { 11935a811889SMel Gorman unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1); 11945a811889SMel Gorman unsigned int nr_scanned = 0; 11955a811889SMel Gorman unsigned long low_pfn, min_pfn, high_pfn = 0, highest = 0; 11965a811889SMel Gorman unsigned long nr_isolated = 0; 11975a811889SMel Gorman unsigned long distance; 11985a811889SMel Gorman struct page *page = NULL; 11995a811889SMel Gorman bool scan_start = false; 12005a811889SMel Gorman int order; 12015a811889SMel Gorman 12025a811889SMel Gorman /* Full compaction passes in a negative order */ 12035a811889SMel Gorman if (cc->order <= 0) 12045a811889SMel Gorman return cc->free_pfn; 12055a811889SMel Gorman 12065a811889SMel Gorman /* 12075a811889SMel Gorman * If starting the scan, use a deeper search and use the highest 12085a811889SMel Gorman * PFN found if a suitable one is not found. 12095a811889SMel Gorman */ 12105a811889SMel Gorman if (cc->free_pfn == pageblock_start_pfn(zone_end_pfn(cc->zone) - 1)) { 12115a811889SMel Gorman limit = pageblock_nr_pages >> 1; 12125a811889SMel Gorman scan_start = true; 12135a811889SMel Gorman } 12145a811889SMel Gorman 12155a811889SMel Gorman /* 12165a811889SMel Gorman * Preferred point is in the top quarter of the scan space but take 12175a811889SMel Gorman * a pfn from the top half if the search is problematic. 12185a811889SMel Gorman */ 12195a811889SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn); 12205a811889SMel Gorman low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); 12215a811889SMel Gorman min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); 12225a811889SMel Gorman 12235a811889SMel Gorman if (WARN_ON_ONCE(min_pfn > low_pfn)) 12245a811889SMel Gorman low_pfn = min_pfn; 12255a811889SMel Gorman 12265a811889SMel Gorman for (order = cc->order - 1; 12275a811889SMel Gorman order >= 0 && !page; 12285a811889SMel Gorman order--) { 12295a811889SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 12305a811889SMel Gorman struct list_head *freelist; 12315a811889SMel Gorman struct page *freepage; 12325a811889SMel Gorman unsigned long flags; 12335a811889SMel Gorman unsigned int order_scanned = 0; 12345a811889SMel Gorman 12355a811889SMel Gorman if (!area->nr_free) 12365a811889SMel Gorman continue; 12375a811889SMel Gorman 12385a811889SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 12395a811889SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 12405a811889SMel Gorman list_for_each_entry_reverse(freepage, freelist, lru) { 12415a811889SMel Gorman unsigned long pfn; 12425a811889SMel Gorman 12435a811889SMel Gorman order_scanned++; 12445a811889SMel Gorman nr_scanned++; 12455a811889SMel Gorman pfn = page_to_pfn(freepage); 12465a811889SMel Gorman 12475a811889SMel Gorman if (pfn >= highest) 12485a811889SMel Gorman highest = pageblock_start_pfn(pfn); 12495a811889SMel Gorman 12505a811889SMel Gorman if (pfn >= low_pfn) { 12515a811889SMel Gorman cc->fast_search_fail = 0; 12525a811889SMel Gorman page = freepage; 12535a811889SMel Gorman break; 12545a811889SMel Gorman } 12555a811889SMel Gorman 12565a811889SMel Gorman if (pfn >= min_pfn && pfn > high_pfn) { 12575a811889SMel Gorman high_pfn = pfn; 12585a811889SMel Gorman 12595a811889SMel Gorman /* Shorten the scan if a candidate is found */ 12605a811889SMel Gorman limit >>= 1; 12615a811889SMel Gorman } 12625a811889SMel Gorman 12635a811889SMel Gorman if (order_scanned >= limit) 12645a811889SMel Gorman break; 12655a811889SMel Gorman } 12665a811889SMel Gorman 12675a811889SMel Gorman /* Use a minimum pfn if a preferred one was not found */ 12685a811889SMel Gorman if (!page && high_pfn) { 12695a811889SMel Gorman page = pfn_to_page(high_pfn); 12705a811889SMel Gorman 12715a811889SMel Gorman /* Update freepage for the list reorder below */ 12725a811889SMel Gorman freepage = page; 12735a811889SMel Gorman } 12745a811889SMel Gorman 12755a811889SMel Gorman /* Reorder to so a future search skips recent pages */ 12765a811889SMel Gorman move_freelist_head(freelist, freepage); 12775a811889SMel Gorman 12785a811889SMel Gorman /* Isolate the page if available */ 12795a811889SMel Gorman if (page) { 12805a811889SMel Gorman if (__isolate_free_page(page, order)) { 12815a811889SMel Gorman set_page_private(page, order); 12825a811889SMel Gorman nr_isolated = 1 << order; 12835a811889SMel Gorman cc->nr_freepages += nr_isolated; 12845a811889SMel Gorman list_add_tail(&page->lru, &cc->freepages); 12855a811889SMel Gorman count_compact_events(COMPACTISOLATED, nr_isolated); 12865a811889SMel Gorman } else { 12875a811889SMel Gorman /* If isolation fails, abort the search */ 12885a811889SMel Gorman order = -1; 12895a811889SMel Gorman page = NULL; 12905a811889SMel Gorman } 12915a811889SMel Gorman } 12925a811889SMel Gorman 12935a811889SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 12945a811889SMel Gorman 12955a811889SMel Gorman /* 12965a811889SMel Gorman * Smaller scan on next order so the total scan ig related 12975a811889SMel Gorman * to freelist_scan_limit. 12985a811889SMel Gorman */ 12995a811889SMel Gorman if (order_scanned >= limit) 13005a811889SMel Gorman limit = min(1U, limit >> 1); 13015a811889SMel Gorman } 13025a811889SMel Gorman 13035a811889SMel Gorman if (!page) { 13045a811889SMel Gorman cc->fast_search_fail++; 13055a811889SMel Gorman if (scan_start) { 13065a811889SMel Gorman /* 13075a811889SMel Gorman * Use the highest PFN found above min. If one was 13085a811889SMel Gorman * not found, be pessemistic for direct compaction 13095a811889SMel Gorman * and use the min mark. 13105a811889SMel Gorman */ 13115a811889SMel Gorman if (highest) { 13125a811889SMel Gorman page = pfn_to_page(highest); 13135a811889SMel Gorman cc->free_pfn = highest; 13145a811889SMel Gorman } else { 13155a811889SMel Gorman if (cc->direct_compaction) { 13165a811889SMel Gorman page = pfn_to_page(min_pfn); 13175a811889SMel Gorman cc->free_pfn = min_pfn; 13185a811889SMel Gorman } 13195a811889SMel Gorman } 13205a811889SMel Gorman } 13215a811889SMel Gorman } 13225a811889SMel Gorman 13235a811889SMel Gorman if (highest && highest > cc->zone->compact_cached_free_pfn) 13245a811889SMel Gorman cc->zone->compact_cached_free_pfn = highest; 13255a811889SMel Gorman 13265a811889SMel Gorman cc->total_free_scanned += nr_scanned; 13275a811889SMel Gorman if (!page) 13285a811889SMel Gorman return cc->free_pfn; 13295a811889SMel Gorman 13305a811889SMel Gorman low_pfn = page_to_pfn(page); 13315a811889SMel Gorman fast_isolate_around(cc, low_pfn, nr_isolated); 13325a811889SMel Gorman return low_pfn; 13335a811889SMel Gorman } 13345a811889SMel Gorman 1335f2849aa0SVlastimil Babka /* 1336ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 1337ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 1338ff9543fdSMichal Nazarewicz */ 1339edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 1340ff9543fdSMichal Nazarewicz { 1341edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 1342ff9543fdSMichal Nazarewicz struct page *page; 1343c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 1344e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 1345c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 1346c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 1347ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 13482fe86e00SMichal Nazarewicz 13495a811889SMel Gorman /* Try a small search of the free lists for a candidate */ 13505a811889SMel Gorman isolate_start_pfn = fast_isolate_freepages(cc); 13515a811889SMel Gorman if (cc->nr_freepages) 13525a811889SMel Gorman goto splitmap; 13535a811889SMel Gorman 1354ff9543fdSMichal Nazarewicz /* 1355ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 135649e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 1357e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 1358e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 1359c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 1360c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 1361c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 136249e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 136349e068f0SVlastimil Babka * is using. 1364ff9543fdSMichal Nazarewicz */ 1365e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 13665a811889SMel Gorman block_start_pfn = pageblock_start_pfn(isolate_start_pfn); 1367c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1368c96b9e50SVlastimil Babka zone_end_pfn(zone)); 136906b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 13702fe86e00SMichal Nazarewicz 1371ff9543fdSMichal Nazarewicz /* 1372ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1373ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1374ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1375ff9543fdSMichal Nazarewicz */ 1376f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1377c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1378e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1379e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 1380f6ea3adbSDavid Rientjes /* 1381f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1382f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 1383be976572SVlastimil Babka * to schedule, or even abort async compaction. 1384f6ea3adbSDavid Rientjes */ 1385be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1386be976572SVlastimil Babka && compact_should_abort(cc)) 1387be976572SVlastimil Babka break; 1388f6ea3adbSDavid Rientjes 13897d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 13907d49d886SVlastimil Babka zone); 13917d49d886SVlastimil Babka if (!page) 1392ff9543fdSMichal Nazarewicz continue; 1393ff9543fdSMichal Nazarewicz 1394ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 13959f7e3387SVlastimil Babka if (!suitable_migration_target(cc, page)) 1396ff9543fdSMichal Nazarewicz continue; 139768e3e926SLinus Torvalds 1398bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1399bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1400bb13ffebSMel Gorman continue; 1401bb13ffebSMel Gorman 1402e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 1403a46cbf3bSDavid Rientjes isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn, 1404a46cbf3bSDavid Rientjes freelist, false); 1405ff9543fdSMichal Nazarewicz 1406cb2dcaf0SMel Gorman /* Are enough freepages isolated? */ 1407cb2dcaf0SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) { 1408a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1409a46cbf3bSDavid Rientjes /* 1410a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1411a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1412a46cbf3bSDavid Rientjes */ 1413f5f61a32SVlastimil Babka isolate_start_pfn = 1414e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1415a46cbf3bSDavid Rientjes } 1416be976572SVlastimil Babka break; 1417a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1418f5f61a32SVlastimil Babka /* 1419a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1420a46cbf3bSDavid Rientjes * needlessly. 1421f5f61a32SVlastimil Babka */ 1422a46cbf3bSDavid Rientjes break; 1423f5f61a32SVlastimil Babka } 1424c89511abSMel Gorman } 1425ff9543fdSMichal Nazarewicz 14267ed695e0SVlastimil Babka /* 1427f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1428f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1429f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1430f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 14317ed695e0SVlastimil Babka */ 1432f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 14335a811889SMel Gorman 14345a811889SMel Gorman splitmap: 14355a811889SMel Gorman /* __isolate_free_page() does not map the pages */ 14365a811889SMel Gorman split_map_pages(freelist); 1437748446bbSMel Gorman } 1438748446bbSMel Gorman 1439748446bbSMel Gorman /* 1440748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1441748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1442748446bbSMel Gorman */ 1443748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1444666feb21SMichal Hocko unsigned long data) 1445748446bbSMel Gorman { 1446748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1447748446bbSMel Gorman struct page *freepage; 1448748446bbSMel Gorman 1449748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1450edc2ca61SVlastimil Babka isolate_freepages(cc); 1451748446bbSMel Gorman 1452748446bbSMel Gorman if (list_empty(&cc->freepages)) 1453748446bbSMel Gorman return NULL; 1454748446bbSMel Gorman } 1455748446bbSMel Gorman 1456748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1457748446bbSMel Gorman list_del(&freepage->lru); 1458748446bbSMel Gorman cc->nr_freepages--; 1459748446bbSMel Gorman 1460748446bbSMel Gorman return freepage; 1461748446bbSMel Gorman } 1462748446bbSMel Gorman 1463748446bbSMel Gorman /* 1464d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1465d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1466d53aea3dSDavid Rientjes * special handling needed for NUMA. 1467d53aea3dSDavid Rientjes */ 1468d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1469d53aea3dSDavid Rientjes { 1470d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1471d53aea3dSDavid Rientjes 1472d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1473d53aea3dSDavid Rientjes cc->nr_freepages++; 1474d53aea3dSDavid Rientjes } 1475d53aea3dSDavid Rientjes 1476ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1477ff9543fdSMichal Nazarewicz typedef enum { 1478ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1479ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1480ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1481ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1482ff9543fdSMichal Nazarewicz 1483ff9543fdSMichal Nazarewicz /* 14845bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 14855bbe3547SEric B Munson * compactable pages. 14865bbe3547SEric B Munson */ 14875bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 14885bbe3547SEric B Munson 148970b44595SMel Gorman static inline void 149070b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) 149170b44595SMel Gorman { 149270b44595SMel Gorman if (cc->fast_start_pfn == ULONG_MAX) 149370b44595SMel Gorman return; 149470b44595SMel Gorman 149570b44595SMel Gorman if (!cc->fast_start_pfn) 149670b44595SMel Gorman cc->fast_start_pfn = pfn; 149770b44595SMel Gorman 149870b44595SMel Gorman cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); 149970b44595SMel Gorman } 150070b44595SMel Gorman 150170b44595SMel Gorman static inline unsigned long 150270b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc) 150370b44595SMel Gorman { 150470b44595SMel Gorman if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) 150570b44595SMel Gorman return cc->migrate_pfn; 150670b44595SMel Gorman 150770b44595SMel Gorman cc->migrate_pfn = cc->fast_start_pfn; 150870b44595SMel Gorman cc->fast_start_pfn = ULONG_MAX; 150970b44595SMel Gorman 151070b44595SMel Gorman return cc->migrate_pfn; 151170b44595SMel Gorman } 151270b44595SMel Gorman 151370b44595SMel Gorman /* 151470b44595SMel Gorman * Briefly search the free lists for a migration source that already has 151570b44595SMel Gorman * some free pages to reduce the number of pages that need migration 151670b44595SMel Gorman * before a pageblock is free. 151770b44595SMel Gorman */ 151870b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc) 151970b44595SMel Gorman { 152070b44595SMel Gorman unsigned int limit = freelist_scan_limit(cc); 152170b44595SMel Gorman unsigned int nr_scanned = 0; 152270b44595SMel Gorman unsigned long distance; 152370b44595SMel Gorman unsigned long pfn = cc->migrate_pfn; 152470b44595SMel Gorman unsigned long high_pfn; 152570b44595SMel Gorman int order; 152670b44595SMel Gorman 152770b44595SMel Gorman /* Skip hints are relied on to avoid repeats on the fast search */ 152870b44595SMel Gorman if (cc->ignore_skip_hint) 152970b44595SMel Gorman return pfn; 153070b44595SMel Gorman 153170b44595SMel Gorman /* 153270b44595SMel Gorman * If the migrate_pfn is not at the start of a zone or the start 153370b44595SMel Gorman * of a pageblock then assume this is a continuation of a previous 153470b44595SMel Gorman * scan restarted due to COMPACT_CLUSTER_MAX. 153570b44595SMel Gorman */ 153670b44595SMel Gorman if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) 153770b44595SMel Gorman return pfn; 153870b44595SMel Gorman 153970b44595SMel Gorman /* 154070b44595SMel Gorman * For smaller orders, just linearly scan as the number of pages 154170b44595SMel Gorman * to migrate should be relatively small and does not necessarily 154270b44595SMel Gorman * justify freeing up a large block for a small allocation. 154370b44595SMel Gorman */ 154470b44595SMel Gorman if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) 154570b44595SMel Gorman return pfn; 154670b44595SMel Gorman 154770b44595SMel Gorman /* 154870b44595SMel Gorman * Only allow kcompactd and direct requests for movable pages to 154970b44595SMel Gorman * quickly clear out a MOVABLE pageblock for allocation. This 155070b44595SMel Gorman * reduces the risk that a large movable pageblock is freed for 155170b44595SMel Gorman * an unmovable/reclaimable small allocation. 155270b44595SMel Gorman */ 155370b44595SMel Gorman if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) 155470b44595SMel Gorman return pfn; 155570b44595SMel Gorman 155670b44595SMel Gorman /* 155770b44595SMel Gorman * When starting the migration scanner, pick any pageblock within the 155870b44595SMel Gorman * first half of the search space. Otherwise try and pick a pageblock 155970b44595SMel Gorman * within the first eighth to reduce the chances that a migration 156070b44595SMel Gorman * target later becomes a source. 156170b44595SMel Gorman */ 156270b44595SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn) >> 1; 156370b44595SMel Gorman if (cc->migrate_pfn != cc->zone->zone_start_pfn) 156470b44595SMel Gorman distance >>= 2; 156570b44595SMel Gorman high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); 156670b44595SMel Gorman 156770b44595SMel Gorman for (order = cc->order - 1; 156870b44595SMel Gorman order >= PAGE_ALLOC_COSTLY_ORDER && pfn == cc->migrate_pfn && nr_scanned < limit; 156970b44595SMel Gorman order--) { 157070b44595SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 157170b44595SMel Gorman struct list_head *freelist; 157270b44595SMel Gorman unsigned long flags; 157370b44595SMel Gorman struct page *freepage; 157470b44595SMel Gorman 157570b44595SMel Gorman if (!area->nr_free) 157670b44595SMel Gorman continue; 157770b44595SMel Gorman 157870b44595SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 157970b44595SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 158070b44595SMel Gorman list_for_each_entry(freepage, freelist, lru) { 158170b44595SMel Gorman unsigned long free_pfn; 158270b44595SMel Gorman 158370b44595SMel Gorman nr_scanned++; 158470b44595SMel Gorman free_pfn = page_to_pfn(freepage); 158570b44595SMel Gorman if (free_pfn < high_pfn) { 158670b44595SMel Gorman /* 158770b44595SMel Gorman * Avoid if skipped recently. Ideally it would 158870b44595SMel Gorman * move to the tail but even safe iteration of 158970b44595SMel Gorman * the list assumes an entry is deleted, not 159070b44595SMel Gorman * reordered. 159170b44595SMel Gorman */ 159270b44595SMel Gorman if (get_pageblock_skip(freepage)) { 159370b44595SMel Gorman if (list_is_last(freelist, &freepage->lru)) 159470b44595SMel Gorman break; 159570b44595SMel Gorman 159670b44595SMel Gorman continue; 159770b44595SMel Gorman } 159870b44595SMel Gorman 159970b44595SMel Gorman /* Reorder to so a future search skips recent pages */ 160070b44595SMel Gorman move_freelist_tail(freelist, freepage); 160170b44595SMel Gorman 1602e380bebeSMel Gorman update_fast_start_pfn(cc, free_pfn); 160370b44595SMel Gorman pfn = pageblock_start_pfn(free_pfn); 160470b44595SMel Gorman cc->fast_search_fail = 0; 160570b44595SMel Gorman set_pageblock_skip(freepage); 160670b44595SMel Gorman break; 160770b44595SMel Gorman } 160870b44595SMel Gorman 160970b44595SMel Gorman if (nr_scanned >= limit) { 161070b44595SMel Gorman cc->fast_search_fail++; 161170b44595SMel Gorman move_freelist_tail(freelist, freepage); 161270b44595SMel Gorman break; 161370b44595SMel Gorman } 161470b44595SMel Gorman } 161570b44595SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 161670b44595SMel Gorman } 161770b44595SMel Gorman 161870b44595SMel Gorman cc->total_migrate_scanned += nr_scanned; 161970b44595SMel Gorman 162070b44595SMel Gorman /* 162170b44595SMel Gorman * If fast scanning failed then use a cached entry for a page block 162270b44595SMel Gorman * that had free pages as the basis for starting a linear scan. 162370b44595SMel Gorman */ 162470b44595SMel Gorman if (pfn == cc->migrate_pfn) 162570b44595SMel Gorman pfn = reinit_migrate_pfn(cc); 162670b44595SMel Gorman 162770b44595SMel Gorman return pfn; 162870b44595SMel Gorman } 162970b44595SMel Gorman 16305bbe3547SEric B Munson /* 1631edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1632edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1633edc2ca61SVlastimil Babka * compact_control. 1634ff9543fdSMichal Nazarewicz */ 1635ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1636ff9543fdSMichal Nazarewicz struct compact_control *cc) 1637ff9543fdSMichal Nazarewicz { 1638e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1639e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1640e1409c32SJoonsoo Kim unsigned long low_pfn; 1641edc2ca61SVlastimil Babka struct page *page; 1642edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 16435bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 16441d2047feSHugh Dickins (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); 164570b44595SMel Gorman bool fast_find_block; 1646ff9543fdSMichal Nazarewicz 1647edc2ca61SVlastimil Babka /* 1648edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 164970b44595SMel Gorman * initialized by compact_zone(). The first failure will use 165070b44595SMel Gorman * the lowest PFN as the starting point for linear scanning. 1651edc2ca61SVlastimil Babka */ 165270b44595SMel Gorman low_pfn = fast_find_migrateblock(cc); 165306b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1654e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1655e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1656ff9543fdSMichal Nazarewicz 165770b44595SMel Gorman /* 165870b44595SMel Gorman * fast_find_migrateblock marks a pageblock skipped so to avoid 165970b44595SMel Gorman * the isolation_suitable check below, check whether the fast 166070b44595SMel Gorman * search was successful. 166170b44595SMel Gorman */ 166270b44595SMel Gorman fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; 166370b44595SMel Gorman 1664ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 166506b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1666ff9543fdSMichal Nazarewicz 1667edc2ca61SVlastimil Babka /* 1668edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1669edc2ca61SVlastimil Babka * Do not cross the free scanner. 1670edc2ca61SVlastimil Babka */ 1671e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 167270b44595SMel Gorman fast_find_block = false, 1673e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1674e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1675e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1676edc2ca61SVlastimil Babka 1677edc2ca61SVlastimil Babka /* 1678edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1679edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1680edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1681edc2ca61SVlastimil Babka */ 1682edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1683edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1684edc2ca61SVlastimil Babka break; 1685edc2ca61SVlastimil Babka 1686e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1687e1409c32SJoonsoo Kim zone); 16887d49d886SVlastimil Babka if (!page) 1689edc2ca61SVlastimil Babka continue; 1690edc2ca61SVlastimil Babka 1691e380bebeSMel Gorman /* 1692e380bebeSMel Gorman * If isolation recently failed, do not retry. Only check the 1693e380bebeSMel Gorman * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock 1694e380bebeSMel Gorman * to be visited multiple times. Assume skip was checked 1695e380bebeSMel Gorman * before making it "skip" so other compaction instances do 1696e380bebeSMel Gorman * not scan the same block. 1697e380bebeSMel Gorman */ 1698e380bebeSMel Gorman if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && 1699e380bebeSMel Gorman !fast_find_block && !isolation_suitable(cc, page)) 1700edc2ca61SVlastimil Babka continue; 1701edc2ca61SVlastimil Babka 1702edc2ca61SVlastimil Babka /* 1703*9bebefd5SMel Gorman * For async compaction, also only scan in MOVABLE blocks 1704*9bebefd5SMel Gorman * without huge pages. Async compaction is optimistic to see 1705*9bebefd5SMel Gorman * if the minimum amount of work satisfies the allocation. 1706*9bebefd5SMel Gorman * The cached PFN is updated as it's possible that all 1707*9bebefd5SMel Gorman * remaining blocks between source and target are unsuitable 1708*9bebefd5SMel Gorman * and the compaction scanners fail to meet. 1709edc2ca61SVlastimil Babka */ 1710*9bebefd5SMel Gorman if (!suitable_migration_source(cc, page)) { 1711*9bebefd5SMel Gorman update_cached_migrate(cc, block_end_pfn); 1712edc2ca61SVlastimil Babka continue; 1713*9bebefd5SMel Gorman } 1714ff9543fdSMichal Nazarewicz 1715ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1716e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1717e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1718edc2ca61SVlastimil Babka 1719cb2dcaf0SMel Gorman if (!low_pfn) 1720ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1721ff9543fdSMichal Nazarewicz 1722edc2ca61SVlastimil Babka /* 1723edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1724edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1725edc2ca61SVlastimil Babka * continue or not. 1726edc2ca61SVlastimil Babka */ 1727edc2ca61SVlastimil Babka break; 1728edc2ca61SVlastimil Babka } 1729edc2ca61SVlastimil Babka 1730f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1731f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1732ff9543fdSMichal Nazarewicz 1733edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1734ff9543fdSMichal Nazarewicz } 1735ff9543fdSMichal Nazarewicz 173621c527a3SYaowei Bai /* 173721c527a3SYaowei Bai * order == -1 is expected when compacting via 173821c527a3SYaowei Bai * /proc/sys/vm/compact_memory 173921c527a3SYaowei Bai */ 174021c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 174121c527a3SYaowei Bai { 174221c527a3SYaowei Bai return order == -1; 174321c527a3SYaowei Bai } 174421c527a3SYaowei Bai 174540cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc) 1746748446bbSMel Gorman { 17478fb74b9fSMel Gorman unsigned int order; 1748d39773a0SVlastimil Babka const int migratetype = cc->migratetype; 1749cb2dcaf0SMel Gorman int ret; 1750748446bbSMel Gorman 1751753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1752f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 175355b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 175440cacbcbSMel Gorman reset_cached_positions(cc->zone); 175555b7c4c9SVlastimil Babka 175662997027SMel Gorman /* 175762997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1758accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 175962997027SMel Gorman * flag itself as the decision to be clear should be directly 176062997027SMel Gorman * based on an allocation request. 176162997027SMel Gorman */ 1762accf6242SVlastimil Babka if (cc->direct_compaction) 176340cacbcbSMel Gorman cc->zone->compact_blockskip_flush = true; 176462997027SMel Gorman 1765c8f7de0bSMichal Hocko if (cc->whole_zone) 1766748446bbSMel Gorman return COMPACT_COMPLETE; 1767c8f7de0bSMichal Hocko else 1768c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 1769bb13ffebSMel Gorman } 1770748446bbSMel Gorman 177121c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 177256de7263SMel Gorman return COMPACT_CONTINUE; 177356de7263SMel Gorman 1774baf6a9a1SVlastimil Babka /* 1775efe771c7SMel Gorman * Always finish scanning a pageblock to reduce the possibility of 1776efe771c7SMel Gorman * fallbacks in the future. This is particularly important when 1777efe771c7SMel Gorman * migration source is unmovable/reclaimable but it's not worth 1778efe771c7SMel Gorman * special casing. 1779baf6a9a1SVlastimil Babka */ 1780efe771c7SMel Gorman if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) 1781baf6a9a1SVlastimil Babka return COMPACT_CONTINUE; 1782baf6a9a1SVlastimil Babka 178356de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 1784cb2dcaf0SMel Gorman ret = COMPACT_NO_SUITABLE_PAGE; 178556de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 178640cacbcbSMel Gorman struct free_area *area = &cc->zone->free_area[order]; 17872149cdaeSJoonsoo Kim bool can_steal; 17888fb74b9fSMel Gorman 178956de7263SMel Gorman /* Job done if page is free of the right migratetype */ 17906d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 1791cf378319SVlastimil Babka return COMPACT_SUCCESS; 179256de7263SMel Gorman 17932149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 17942149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 17952149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 17962149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 1797cf378319SVlastimil Babka return COMPACT_SUCCESS; 17982149cdaeSJoonsoo Kim #endif 17992149cdaeSJoonsoo Kim /* 18002149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 18012149cdaeSJoonsoo Kim * other migratetype buddy lists. 18022149cdaeSJoonsoo Kim */ 18032149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 1804baf6a9a1SVlastimil Babka true, &can_steal) != -1) { 1805baf6a9a1SVlastimil Babka 1806baf6a9a1SVlastimil Babka /* movable pages are OK in any pageblock */ 1807baf6a9a1SVlastimil Babka if (migratetype == MIGRATE_MOVABLE) 1808cf378319SVlastimil Babka return COMPACT_SUCCESS; 1809baf6a9a1SVlastimil Babka 1810baf6a9a1SVlastimil Babka /* 1811baf6a9a1SVlastimil Babka * We are stealing for a non-movable allocation. Make 1812baf6a9a1SVlastimil Babka * sure we finish compacting the current pageblock 1813baf6a9a1SVlastimil Babka * first so it is as free as possible and we won't 1814baf6a9a1SVlastimil Babka * have to steal another one soon. This only applies 1815baf6a9a1SVlastimil Babka * to sync compaction, as async compaction operates 1816baf6a9a1SVlastimil Babka * on pageblocks of the same migratetype. 1817baf6a9a1SVlastimil Babka */ 1818baf6a9a1SVlastimil Babka if (cc->mode == MIGRATE_ASYNC || 1819baf6a9a1SVlastimil Babka IS_ALIGNED(cc->migrate_pfn, 1820baf6a9a1SVlastimil Babka pageblock_nr_pages)) { 1821baf6a9a1SVlastimil Babka return COMPACT_SUCCESS; 1822baf6a9a1SVlastimil Babka } 1823baf6a9a1SVlastimil Babka 1824cb2dcaf0SMel Gorman ret = COMPACT_CONTINUE; 1825cb2dcaf0SMel Gorman break; 1826baf6a9a1SVlastimil Babka } 182756de7263SMel Gorman } 182856de7263SMel Gorman 1829cb2dcaf0SMel Gorman if (cc->contended || fatal_signal_pending(current)) 1830cb2dcaf0SMel Gorman ret = COMPACT_CONTENDED; 1831cb2dcaf0SMel Gorman 1832cb2dcaf0SMel Gorman return ret; 1833837d026dSJoonsoo Kim } 1834837d026dSJoonsoo Kim 183540cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc) 1836837d026dSJoonsoo Kim { 1837837d026dSJoonsoo Kim int ret; 1838837d026dSJoonsoo Kim 183940cacbcbSMel Gorman ret = __compact_finished(cc); 184040cacbcbSMel Gorman trace_mm_compaction_finished(cc->zone, cc->order, ret); 1841837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1842837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1843837d026dSJoonsoo Kim 1844837d026dSJoonsoo Kim return ret; 1845748446bbSMel Gorman } 1846748446bbSMel Gorman 18473e7d3449SMel Gorman /* 18483e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 18493e7d3449SMel Gorman * Returns 18503e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 1851cf378319SVlastimil Babka * COMPACT_SUCCESS - If the allocation would succeed without compaction 18523e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 18533e7d3449SMel Gorman */ 1854ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 1855c603844bSMel Gorman unsigned int alloc_flags, 185686a294a8SMichal Hocko int classzone_idx, 185786a294a8SMichal Hocko unsigned long wmark_target) 18583e7d3449SMel Gorman { 18593e7d3449SMel Gorman unsigned long watermark; 18603e7d3449SMel Gorman 186121c527a3SYaowei Bai if (is_via_compact_memory(order)) 18623957c776SMichal Hocko return COMPACT_CONTINUE; 18633957c776SMichal Hocko 1864a9214443SMel Gorman watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); 1865ebff3980SVlastimil Babka /* 1866ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1867ebff3980SVlastimil Babka * should be no need for compaction at all. 1868ebff3980SVlastimil Babka */ 1869ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1870ebff3980SVlastimil Babka alloc_flags)) 1871cf378319SVlastimil Babka return COMPACT_SUCCESS; 1872ebff3980SVlastimil Babka 18733957c776SMichal Hocko /* 18749861a62cSVlastimil Babka * Watermarks for order-0 must be met for compaction to be able to 1875984fdba6SVlastimil Babka * isolate free pages for migration targets. This means that the 1876984fdba6SVlastimil Babka * watermark and alloc_flags have to match, or be more pessimistic than 1877984fdba6SVlastimil Babka * the check in __isolate_free_page(). We don't use the direct 1878984fdba6SVlastimil Babka * compactor's alloc_flags, as they are not relevant for freepage 1879984fdba6SVlastimil Babka * isolation. We however do use the direct compactor's classzone_idx to 1880984fdba6SVlastimil Babka * skip over zones where lowmem reserves would prevent allocation even 1881984fdba6SVlastimil Babka * if compaction succeeds. 18828348faf9SVlastimil Babka * For costly orders, we require low watermark instead of min for 18838348faf9SVlastimil Babka * compaction to proceed to increase its chances. 1884d883c6cfSJoonsoo Kim * ALLOC_CMA is used, as pages in CMA pageblocks are considered 1885d883c6cfSJoonsoo Kim * suitable migration targets 18863e7d3449SMel Gorman */ 18878348faf9SVlastimil Babka watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? 18888348faf9SVlastimil Babka low_wmark_pages(zone) : min_wmark_pages(zone); 18898348faf9SVlastimil Babka watermark += compact_gap(order); 189086a294a8SMichal Hocko if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, 1891d883c6cfSJoonsoo Kim ALLOC_CMA, wmark_target)) 18923e7d3449SMel Gorman return COMPACT_SKIPPED; 18933e7d3449SMel Gorman 1894cc5c9f09SVlastimil Babka return COMPACT_CONTINUE; 1895cc5c9f09SVlastimil Babka } 1896cc5c9f09SVlastimil Babka 1897cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order, 1898cc5c9f09SVlastimil Babka unsigned int alloc_flags, 1899cc5c9f09SVlastimil Babka int classzone_idx) 1900cc5c9f09SVlastimil Babka { 1901cc5c9f09SVlastimil Babka enum compact_result ret; 1902cc5c9f09SVlastimil Babka int fragindex; 1903cc5c9f09SVlastimil Babka 1904cc5c9f09SVlastimil Babka ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, 1905cc5c9f09SVlastimil Babka zone_page_state(zone, NR_FREE_PAGES)); 19063e7d3449SMel Gorman /* 19073e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 19083e7d3449SMel Gorman * low memory or external fragmentation 19093e7d3449SMel Gorman * 1910ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1911ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 19123e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 19133e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 19143e7d3449SMel Gorman * 191520311420SVlastimil Babka * Only compact if a failure would be due to fragmentation. Also 191620311420SVlastimil Babka * ignore fragindex for non-costly orders where the alternative to 191720311420SVlastimil Babka * a successful reclaim/compaction is OOM. Fragindex and the 191820311420SVlastimil Babka * vm.extfrag_threshold sysctl is meant as a heuristic to prevent 191920311420SVlastimil Babka * excessive compaction for costly orders, but it should not be at the 192020311420SVlastimil Babka * expense of system stability. 19213e7d3449SMel Gorman */ 192220311420SVlastimil Babka if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { 19233e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 19243e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1925cc5c9f09SVlastimil Babka ret = COMPACT_NOT_SUITABLE_ZONE; 19263e7d3449SMel Gorman } 19273e7d3449SMel Gorman 1928837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1929837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1930837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1931837d026dSJoonsoo Kim 1932837d026dSJoonsoo Kim return ret; 1933837d026dSJoonsoo Kim } 1934837d026dSJoonsoo Kim 193586a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 193686a294a8SMichal Hocko int alloc_flags) 193786a294a8SMichal Hocko { 193886a294a8SMichal Hocko struct zone *zone; 193986a294a8SMichal Hocko struct zoneref *z; 194086a294a8SMichal Hocko 194186a294a8SMichal Hocko /* 194286a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 194386a294a8SMichal Hocko * retrying the reclaim. 194486a294a8SMichal Hocko */ 194586a294a8SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 194686a294a8SMichal Hocko ac->nodemask) { 194786a294a8SMichal Hocko unsigned long available; 194886a294a8SMichal Hocko enum compact_result compact_result; 194986a294a8SMichal Hocko 195086a294a8SMichal Hocko /* 195186a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 195286a294a8SMichal Hocko * want to trash just for a single high order allocation which 195386a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 195486a294a8SMichal Hocko * is happy about the watermark check. 195586a294a8SMichal Hocko */ 19565a1c84b4SMel Gorman available = zone_reclaimable_pages(zone) / order; 195786a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 195886a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 195986a294a8SMichal Hocko ac_classzone_idx(ac), available); 1960cc5c9f09SVlastimil Babka if (compact_result != COMPACT_SKIPPED) 196186a294a8SMichal Hocko return true; 196286a294a8SMichal Hocko } 196386a294a8SMichal Hocko 196486a294a8SMichal Hocko return false; 196586a294a8SMichal Hocko } 196686a294a8SMichal Hocko 196740cacbcbSMel Gorman static enum compact_result compact_zone(struct compact_control *cc) 1968748446bbSMel Gorman { 1969ea7ab982SMichal Hocko enum compact_result ret; 197040cacbcbSMel Gorman unsigned long start_pfn = cc->zone->zone_start_pfn; 197140cacbcbSMel Gorman unsigned long end_pfn = zone_end_pfn(cc->zone); 1972566e54e1SMel Gorman unsigned long last_migrated_pfn; 1973e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1974748446bbSMel Gorman 1975d39773a0SVlastimil Babka cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask); 197640cacbcbSMel Gorman ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, 1977ebff3980SVlastimil Babka cc->classzone_idx); 19783e7d3449SMel Gorman /* Compaction is likely to fail */ 1979cf378319SVlastimil Babka if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) 19803e7d3449SMel Gorman return ret; 1981c46649deSMichal Hocko 1982c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 1983c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 19843e7d3449SMel Gorman 1985c89511abSMel Gorman /* 1986d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1987accf6242SVlastimil Babka * is about to be retried after being deferred. 1988d3132e4bSVlastimil Babka */ 198940cacbcbSMel Gorman if (compaction_restarting(cc->zone, cc->order)) 199040cacbcbSMel Gorman __reset_isolation_suitable(cc->zone); 1991d3132e4bSVlastimil Babka 1992d3132e4bSVlastimil Babka /* 1993c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 199406ed2998SVlastimil Babka * information on where the scanners should start (unless we explicitly 199506ed2998SVlastimil Babka * want to compact the whole zone), but check that it is initialised 199606ed2998SVlastimil Babka * by ensuring the values are within zone boundaries. 1997c89511abSMel Gorman */ 199870b44595SMel Gorman cc->fast_start_pfn = 0; 199906ed2998SVlastimil Babka if (cc->whole_zone) { 200006ed2998SVlastimil Babka cc->migrate_pfn = start_pfn; 200106ed2998SVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 200206ed2998SVlastimil Babka } else { 200340cacbcbSMel Gorman cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; 200440cacbcbSMel Gorman cc->free_pfn = cc->zone->compact_cached_free_pfn; 2005623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 200606b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 200740cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = cc->free_pfn; 2008c89511abSMel Gorman } 2009623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 2010c89511abSMel Gorman cc->migrate_pfn = start_pfn; 201140cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 201240cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 2013c89511abSMel Gorman } 2014c8f7de0bSMichal Hocko 2015c8f7de0bSMichal Hocko if (cc->migrate_pfn == start_pfn) 2016c8f7de0bSMichal Hocko cc->whole_zone = true; 201706ed2998SVlastimil Babka } 2018c8f7de0bSMichal Hocko 2019566e54e1SMel Gorman last_migrated_pfn = 0; 2020748446bbSMel Gorman 202116c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 202216c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 20230eb927c0SMel Gorman 2024748446bbSMel Gorman migrate_prep_local(); 2025748446bbSMel Gorman 202640cacbcbSMel Gorman while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { 20279d502c1cSMinchan Kim int err; 2028566e54e1SMel Gorman unsigned long start_pfn = cc->migrate_pfn; 2029748446bbSMel Gorman 2030804d3121SMel Gorman /* 2031804d3121SMel Gorman * Avoid multiple rescans which can happen if a page cannot be 2032804d3121SMel Gorman * isolated (dirty/writeback in async mode) or if the migrated 2033804d3121SMel Gorman * pages are being allocated before the pageblock is cleared. 2034804d3121SMel Gorman * The first rescan will capture the entire pageblock for 2035804d3121SMel Gorman * migration. If it fails, it'll be marked skip and scanning 2036804d3121SMel Gorman * will proceed as normal. 2037804d3121SMel Gorman */ 2038804d3121SMel Gorman cc->rescan = false; 2039804d3121SMel Gorman if (pageblock_start_pfn(last_migrated_pfn) == 2040804d3121SMel Gorman pageblock_start_pfn(start_pfn)) { 2041804d3121SMel Gorman cc->rescan = true; 2042804d3121SMel Gorman } 2043804d3121SMel Gorman 204440cacbcbSMel Gorman switch (isolate_migratepages(cc->zone, cc)) { 2045f9e35b3bSMel Gorman case ISOLATE_ABORT: 20462d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 20475733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 2048e64c5237SShaohua Li cc->nr_migratepages = 0; 2049566e54e1SMel Gorman last_migrated_pfn = 0; 2050f9e35b3bSMel Gorman goto out; 2051f9e35b3bSMel Gorman case ISOLATE_NONE: 2052fdaf7f5cSVlastimil Babka /* 2053fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 2054fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 2055fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 2056fdaf7f5cSVlastimil Babka */ 2057fdaf7f5cSVlastimil Babka goto check_drain; 2058f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 2059566e54e1SMel Gorman last_migrated_pfn = start_pfn; 2060f9e35b3bSMel Gorman ; 2061f9e35b3bSMel Gorman } 2062748446bbSMel Gorman 2063d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 2064e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 20657b2a2d4aSMel Gorman MR_COMPACTION); 2066748446bbSMel Gorman 2067f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 2068f8c9301fSVlastimil Babka &cc->migratepages); 2069748446bbSMel Gorman 2070f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 2071f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 20729d502c1cSMinchan Kim if (err) { 20735733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 20747ed695e0SVlastimil Babka /* 20757ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 20767ed695e0SVlastimil Babka * and we want compact_finished() to detect it 20777ed695e0SVlastimil Babka */ 2078f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 20792d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 20804bf2bba3SDavid Rientjes goto out; 2081748446bbSMel Gorman } 2082fdd048e1SVlastimil Babka /* 2083fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 2084fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 2085fdd048e1SVlastimil Babka */ 2086fdd048e1SVlastimil Babka if (cc->direct_compaction && 2087fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 2088fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 2089fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 2090fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 2091566e54e1SMel Gorman last_migrated_pfn = 0; 2092fdd048e1SVlastimil Babka } 20934bf2bba3SDavid Rientjes } 2094fdaf7f5cSVlastimil Babka 2095fdaf7f5cSVlastimil Babka check_drain: 2096fdaf7f5cSVlastimil Babka /* 2097fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 2098fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 2099fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 2100fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 2101fdaf7f5cSVlastimil Babka * would succeed. 2102fdaf7f5cSVlastimil Babka */ 2103566e54e1SMel Gorman if (cc->order > 0 && last_migrated_pfn) { 2104fdaf7f5cSVlastimil Babka int cpu; 2105fdaf7f5cSVlastimil Babka unsigned long current_block_start = 210606b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 2107fdaf7f5cSVlastimil Babka 2108566e54e1SMel Gorman if (last_migrated_pfn < current_block_start) { 2109fdaf7f5cSVlastimil Babka cpu = get_cpu(); 2110fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 211140cacbcbSMel Gorman drain_local_pages(cc->zone); 2112fdaf7f5cSVlastimil Babka put_cpu(); 2113fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 2114566e54e1SMel Gorman last_migrated_pfn = 0; 2115fdaf7f5cSVlastimil Babka } 2116fdaf7f5cSVlastimil Babka } 2117fdaf7f5cSVlastimil Babka 2118748446bbSMel Gorman } 2119748446bbSMel Gorman 2120f9e35b3bSMel Gorman out: 21216bace090SVlastimil Babka /* 21226bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 21236bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 21246bace090SVlastimil Babka */ 21256bace090SVlastimil Babka if (cc->nr_freepages > 0) { 21266bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 21276bace090SVlastimil Babka 21286bace090SVlastimil Babka cc->nr_freepages = 0; 21296bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 21306bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 213106b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 21326bace090SVlastimil Babka /* 21336bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 21346bace090SVlastimil Babka * already reset to zone end in compact_finished() 21356bace090SVlastimil Babka */ 213640cacbcbSMel Gorman if (free_pfn > cc->zone->compact_cached_free_pfn) 213740cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = free_pfn; 21386bace090SVlastimil Babka } 2139748446bbSMel Gorman 21407f354a54SDavid Rientjes count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); 21417f354a54SDavid Rientjes count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); 21427f354a54SDavid Rientjes 214316c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 214416c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 21450eb927c0SMel Gorman 2146748446bbSMel Gorman return ret; 2147748446bbSMel Gorman } 214876ab0f53SMel Gorman 2149ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 2150c3486f53SVlastimil Babka gfp_t gfp_mask, enum compact_priority prio, 2151c603844bSMel Gorman unsigned int alloc_flags, int classzone_idx) 215256de7263SMel Gorman { 2153ea7ab982SMichal Hocko enum compact_result ret; 215456de7263SMel Gorman struct compact_control cc = { 215556de7263SMel Gorman .nr_freepages = 0, 215656de7263SMel Gorman .nr_migratepages = 0, 21577f354a54SDavid Rientjes .total_migrate_scanned = 0, 21587f354a54SDavid Rientjes .total_free_scanned = 0, 215956de7263SMel Gorman .order = order, 21606d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 216156de7263SMel Gorman .zone = zone, 2162a5508cd8SVlastimil Babka .mode = (prio == COMPACT_PRIO_ASYNC) ? 2163a5508cd8SVlastimil Babka MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, 2164ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 2165ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 2166accf6242SVlastimil Babka .direct_compaction = true, 2167a8e025e5SVlastimil Babka .whole_zone = (prio == MIN_COMPACT_PRIORITY), 21689f7e3387SVlastimil Babka .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), 21699f7e3387SVlastimil Babka .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) 217056de7263SMel Gorman }; 217156de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 217256de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 217356de7263SMel Gorman 217440cacbcbSMel Gorman ret = compact_zone(&cc); 2175e64c5237SShaohua Li 2176e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 2177e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 2178e64c5237SShaohua Li 2179e64c5237SShaohua Li return ret; 218056de7263SMel Gorman } 218156de7263SMel Gorman 21825e771905SMel Gorman int sysctl_extfrag_threshold = 500; 21835e771905SMel Gorman 218456de7263SMel Gorman /** 218556de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 218656de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 21871a6d53a1SVlastimil Babka * @order: The order of the current allocation 21881a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 21891a6d53a1SVlastimil Babka * @ac: The context of current allocation 2190112d2d29SYang Shi * @prio: Determines how hard direct compaction should try to succeed 219156de7263SMel Gorman * 219256de7263SMel Gorman * This is the main entry point for direct page compaction. 219356de7263SMel Gorman */ 2194ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 2195c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 2196c3486f53SVlastimil Babka enum compact_priority prio) 219756de7263SMel Gorman { 219856de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 219956de7263SMel Gorman struct zoneref *z; 220056de7263SMel Gorman struct zone *zone; 22011d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 220256de7263SMel Gorman 220373e64c51SMichal Hocko /* 220473e64c51SMichal Hocko * Check if the GFP flags allow compaction - GFP_NOIO is really 220573e64c51SMichal Hocko * tricky context because the migration might require IO 220673e64c51SMichal Hocko */ 220773e64c51SMichal Hocko if (!may_perform_io) 220853853e2dSVlastimil Babka return COMPACT_SKIPPED; 220956de7263SMel Gorman 2210a5508cd8SVlastimil Babka trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); 2211837d026dSJoonsoo Kim 221256de7263SMel Gorman /* Compact each zone in the list */ 22131a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 22141a6d53a1SVlastimil Babka ac->nodemask) { 2215ea7ab982SMichal Hocko enum compact_result status; 221656de7263SMel Gorman 2217a8e025e5SVlastimil Babka if (prio > MIN_COMPACT_PRIORITY 2218a8e025e5SVlastimil Babka && compaction_deferred(zone, order)) { 22191d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 222053853e2dSVlastimil Babka continue; 22211d4746d3SMichal Hocko } 222253853e2dSVlastimil Babka 2223a5508cd8SVlastimil Babka status = compact_zone_order(zone, order, gfp_mask, prio, 2224c3486f53SVlastimil Babka alloc_flags, ac_classzone_idx(ac)); 222556de7263SMel Gorman rc = max(status, rc); 222656de7263SMel Gorman 22277ceb009aSVlastimil Babka /* The allocation should succeed, stop compacting */ 22287ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 222953853e2dSVlastimil Babka /* 223053853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 223153853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 223253853e2dSVlastimil Babka * will repeat this with true if allocation indeed 223353853e2dSVlastimil Babka * succeeds in this zone. 223453853e2dSVlastimil Babka */ 223553853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 22361f9efdefSVlastimil Babka 2237c3486f53SVlastimil Babka break; 22381f9efdefSVlastimil Babka } 22391f9efdefSVlastimil Babka 2240a5508cd8SVlastimil Babka if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || 2241c3486f53SVlastimil Babka status == COMPACT_PARTIAL_SKIPPED)) 224253853e2dSVlastimil Babka /* 224353853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 224453853e2dSVlastimil Babka * so we defer compaction there. If it ends up 224553853e2dSVlastimil Babka * succeeding after all, it will be reset. 224653853e2dSVlastimil Babka */ 224753853e2dSVlastimil Babka defer_compaction(zone, order); 22481f9efdefSVlastimil Babka 22491f9efdefSVlastimil Babka /* 22501f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 22511f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 2252c3486f53SVlastimil Babka * case do not try further zones 22531f9efdefSVlastimil Babka */ 2254c3486f53SVlastimil Babka if ((prio == COMPACT_PRIO_ASYNC && need_resched()) 2255c3486f53SVlastimil Babka || fatal_signal_pending(current)) 22561f9efdefSVlastimil Babka break; 22571f9efdefSVlastimil Babka } 22581f9efdefSVlastimil Babka 225956de7263SMel Gorman return rc; 226056de7263SMel Gorman } 226156de7263SMel Gorman 226256de7263SMel Gorman 226376ab0f53SMel Gorman /* Compact all zones within a node */ 22647103f16dSAndrew Morton static void compact_node(int nid) 22657be62de9SRik van Riel { 2266791cae96SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2267791cae96SVlastimil Babka int zoneid; 2268791cae96SVlastimil Babka struct zone *zone; 22697be62de9SRik van Riel struct compact_control cc = { 22707be62de9SRik van Riel .order = -1, 22717f354a54SDavid Rientjes .total_migrate_scanned = 0, 22727f354a54SDavid Rientjes .total_free_scanned = 0, 2273e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 227491ca9186SDavid Rientjes .ignore_skip_hint = true, 227506ed2998SVlastimil Babka .whole_zone = true, 227673e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 22777be62de9SRik van Riel }; 22787be62de9SRik van Riel 2279791cae96SVlastimil Babka 2280791cae96SVlastimil Babka for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2281791cae96SVlastimil Babka 2282791cae96SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2283791cae96SVlastimil Babka if (!populated_zone(zone)) 2284791cae96SVlastimil Babka continue; 2285791cae96SVlastimil Babka 2286791cae96SVlastimil Babka cc.nr_freepages = 0; 2287791cae96SVlastimil Babka cc.nr_migratepages = 0; 2288791cae96SVlastimil Babka cc.zone = zone; 2289791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 2290791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 2291791cae96SVlastimil Babka 229240cacbcbSMel Gorman compact_zone(&cc); 2293791cae96SVlastimil Babka 2294791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2295791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2296791cae96SVlastimil Babka } 22977be62de9SRik van Riel } 22987be62de9SRik van Riel 229976ab0f53SMel Gorman /* Compact all nodes in the system */ 23007964c06dSJason Liu static void compact_nodes(void) 230176ab0f53SMel Gorman { 230276ab0f53SMel Gorman int nid; 230376ab0f53SMel Gorman 23048575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 23058575ec29SHugh Dickins lru_add_drain_all(); 23068575ec29SHugh Dickins 230776ab0f53SMel Gorman for_each_online_node(nid) 230876ab0f53SMel Gorman compact_node(nid); 230976ab0f53SMel Gorman } 231076ab0f53SMel Gorman 231176ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 231276ab0f53SMel Gorman int sysctl_compact_memory; 231376ab0f53SMel Gorman 2314fec4eb2cSYaowei Bai /* 2315fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 2316fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 2317fec4eb2cSYaowei Bai */ 231876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 231976ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 232076ab0f53SMel Gorman { 232176ab0f53SMel Gorman if (write) 23227964c06dSJason Liu compact_nodes(); 232376ab0f53SMel Gorman 232476ab0f53SMel Gorman return 0; 232576ab0f53SMel Gorman } 2326ed4a6d7fSMel Gorman 2327ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 232874e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 232910fbcf4cSKay Sievers struct device_attribute *attr, 2330ed4a6d7fSMel Gorman const char *buf, size_t count) 2331ed4a6d7fSMel Gorman { 23328575ec29SHugh Dickins int nid = dev->id; 23338575ec29SHugh Dickins 23348575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 23358575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 23368575ec29SHugh Dickins lru_add_drain_all(); 23378575ec29SHugh Dickins 23388575ec29SHugh Dickins compact_node(nid); 23398575ec29SHugh Dickins } 2340ed4a6d7fSMel Gorman 2341ed4a6d7fSMel Gorman return count; 2342ed4a6d7fSMel Gorman } 23430825a6f9SJoe Perches static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node); 2344ed4a6d7fSMel Gorman 2345ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 2346ed4a6d7fSMel Gorman { 234710fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 2348ed4a6d7fSMel Gorman } 2349ed4a6d7fSMel Gorman 2350ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 2351ed4a6d7fSMel Gorman { 235210fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 2353ed4a6d7fSMel Gorman } 2354ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 2355ff9543fdSMichal Nazarewicz 2356698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 2357698b1b30SVlastimil Babka { 2358172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 2359698b1b30SVlastimil Babka } 2360698b1b30SVlastimil Babka 2361698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 2362698b1b30SVlastimil Babka { 2363698b1b30SVlastimil Babka int zoneid; 2364698b1b30SVlastimil Babka struct zone *zone; 2365698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 2366698b1b30SVlastimil Babka 23676cd9dc3eSChen Feng for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { 2368698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2369698b1b30SVlastimil Babka 2370698b1b30SVlastimil Babka if (!populated_zone(zone)) 2371698b1b30SVlastimil Babka continue; 2372698b1b30SVlastimil Babka 2373698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 2374698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 2375698b1b30SVlastimil Babka return true; 2376698b1b30SVlastimil Babka } 2377698b1b30SVlastimil Babka 2378698b1b30SVlastimil Babka return false; 2379698b1b30SVlastimil Babka } 2380698b1b30SVlastimil Babka 2381698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 2382698b1b30SVlastimil Babka { 2383698b1b30SVlastimil Babka /* 2384698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 2385698b1b30SVlastimil Babka * order is allocatable. 2386698b1b30SVlastimil Babka */ 2387698b1b30SVlastimil Babka int zoneid; 2388698b1b30SVlastimil Babka struct zone *zone; 2389698b1b30SVlastimil Babka struct compact_control cc = { 2390698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 23917f354a54SDavid Rientjes .total_migrate_scanned = 0, 23927f354a54SDavid Rientjes .total_free_scanned = 0, 2393698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 2394698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 2395a0647dc9SDavid Rientjes .ignore_skip_hint = false, 239673e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 2397698b1b30SVlastimil Babka }; 2398698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 2399698b1b30SVlastimil Babka cc.classzone_idx); 24007f354a54SDavid Rientjes count_compact_event(KCOMPACTD_WAKE); 2401698b1b30SVlastimil Babka 24026cd9dc3eSChen Feng for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { 2403698b1b30SVlastimil Babka int status; 2404698b1b30SVlastimil Babka 2405698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2406698b1b30SVlastimil Babka if (!populated_zone(zone)) 2407698b1b30SVlastimil Babka continue; 2408698b1b30SVlastimil Babka 2409698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 2410698b1b30SVlastimil Babka continue; 2411698b1b30SVlastimil Babka 2412698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 2413698b1b30SVlastimil Babka COMPACT_CONTINUE) 2414698b1b30SVlastimil Babka continue; 2415698b1b30SVlastimil Babka 2416698b1b30SVlastimil Babka cc.nr_freepages = 0; 2417698b1b30SVlastimil Babka cc.nr_migratepages = 0; 24187f354a54SDavid Rientjes cc.total_migrate_scanned = 0; 24197f354a54SDavid Rientjes cc.total_free_scanned = 0; 2420698b1b30SVlastimil Babka cc.zone = zone; 2421698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 2422698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 2423698b1b30SVlastimil Babka 2424172400c6SVlastimil Babka if (kthread_should_stop()) 2425172400c6SVlastimil Babka return; 242640cacbcbSMel Gorman status = compact_zone(&cc); 2427698b1b30SVlastimil Babka 24287ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 2429698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 2430c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 2431698b1b30SVlastimil Babka /* 2432bc3106b2SDavid Rientjes * Buddy pages may become stranded on pcps that could 2433bc3106b2SDavid Rientjes * otherwise coalesce on the zone's free area for 2434bc3106b2SDavid Rientjes * order >= cc.order. This is ratelimited by the 2435bc3106b2SDavid Rientjes * upcoming deferral. 2436bc3106b2SDavid Rientjes */ 2437bc3106b2SDavid Rientjes drain_all_pages(zone); 2438bc3106b2SDavid Rientjes 2439bc3106b2SDavid Rientjes /* 2440698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 2441698b1b30SVlastimil Babka * sync direct compaction does. 2442698b1b30SVlastimil Babka */ 2443698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 2444698b1b30SVlastimil Babka } 2445698b1b30SVlastimil Babka 24467f354a54SDavid Rientjes count_compact_events(KCOMPACTD_MIGRATE_SCANNED, 24477f354a54SDavid Rientjes cc.total_migrate_scanned); 24487f354a54SDavid Rientjes count_compact_events(KCOMPACTD_FREE_SCANNED, 24497f354a54SDavid Rientjes cc.total_free_scanned); 24507f354a54SDavid Rientjes 2451698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2452698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2453698b1b30SVlastimil Babka } 2454698b1b30SVlastimil Babka 2455698b1b30SVlastimil Babka /* 2456698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 2457698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 2458698b1b30SVlastimil Babka * our current ones 2459698b1b30SVlastimil Babka */ 2460698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 2461698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 2462698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 2463698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 2464698b1b30SVlastimil Babka } 2465698b1b30SVlastimil Babka 2466698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 2467698b1b30SVlastimil Babka { 2468698b1b30SVlastimil Babka if (!order) 2469698b1b30SVlastimil Babka return; 2470698b1b30SVlastimil Babka 2471698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 2472698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 2473698b1b30SVlastimil Babka 2474698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 2475698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 2476698b1b30SVlastimil Babka 24776818600fSDavidlohr Bueso /* 24786818600fSDavidlohr Bueso * Pairs with implicit barrier in wait_event_freezable() 24796818600fSDavidlohr Bueso * such that wakeups are not missed. 24806818600fSDavidlohr Bueso */ 24816818600fSDavidlohr Bueso if (!wq_has_sleeper(&pgdat->kcompactd_wait)) 2482698b1b30SVlastimil Babka return; 2483698b1b30SVlastimil Babka 2484698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 2485698b1b30SVlastimil Babka return; 2486698b1b30SVlastimil Babka 2487698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 2488698b1b30SVlastimil Babka classzone_idx); 2489698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 2490698b1b30SVlastimil Babka } 2491698b1b30SVlastimil Babka 2492698b1b30SVlastimil Babka /* 2493698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 2494698b1b30SVlastimil Babka * from the init process. 2495698b1b30SVlastimil Babka */ 2496698b1b30SVlastimil Babka static int kcompactd(void *p) 2497698b1b30SVlastimil Babka { 2498698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 2499698b1b30SVlastimil Babka struct task_struct *tsk = current; 2500698b1b30SVlastimil Babka 2501698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 2502698b1b30SVlastimil Babka 2503698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 2504698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 2505698b1b30SVlastimil Babka 2506698b1b30SVlastimil Babka set_freezable(); 2507698b1b30SVlastimil Babka 2508698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 2509698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 2510698b1b30SVlastimil Babka 2511698b1b30SVlastimil Babka while (!kthread_should_stop()) { 2512eb414681SJohannes Weiner unsigned long pflags; 2513eb414681SJohannes Weiner 2514698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 2515698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 2516698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 2517698b1b30SVlastimil Babka 2518eb414681SJohannes Weiner psi_memstall_enter(&pflags); 2519698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 2520eb414681SJohannes Weiner psi_memstall_leave(&pflags); 2521698b1b30SVlastimil Babka } 2522698b1b30SVlastimil Babka 2523698b1b30SVlastimil Babka return 0; 2524698b1b30SVlastimil Babka } 2525698b1b30SVlastimil Babka 2526698b1b30SVlastimil Babka /* 2527698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 2528698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 2529698b1b30SVlastimil Babka */ 2530698b1b30SVlastimil Babka int kcompactd_run(int nid) 2531698b1b30SVlastimil Babka { 2532698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2533698b1b30SVlastimil Babka int ret = 0; 2534698b1b30SVlastimil Babka 2535698b1b30SVlastimil Babka if (pgdat->kcompactd) 2536698b1b30SVlastimil Babka return 0; 2537698b1b30SVlastimil Babka 2538698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 2539698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 2540698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 2541698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 2542698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 2543698b1b30SVlastimil Babka } 2544698b1b30SVlastimil Babka return ret; 2545698b1b30SVlastimil Babka } 2546698b1b30SVlastimil Babka 2547698b1b30SVlastimil Babka /* 2548698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 2549698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 2550698b1b30SVlastimil Babka */ 2551698b1b30SVlastimil Babka void kcompactd_stop(int nid) 2552698b1b30SVlastimil Babka { 2553698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 2554698b1b30SVlastimil Babka 2555698b1b30SVlastimil Babka if (kcompactd) { 2556698b1b30SVlastimil Babka kthread_stop(kcompactd); 2557698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 2558698b1b30SVlastimil Babka } 2559698b1b30SVlastimil Babka } 2560698b1b30SVlastimil Babka 2561698b1b30SVlastimil Babka /* 2562698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 2563698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 2564698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 2565698b1b30SVlastimil Babka * restore their cpu bindings. 2566698b1b30SVlastimil Babka */ 2567e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu) 2568698b1b30SVlastimil Babka { 2569698b1b30SVlastimil Babka int nid; 2570698b1b30SVlastimil Babka 2571698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 2572698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2573698b1b30SVlastimil Babka const struct cpumask *mask; 2574698b1b30SVlastimil Babka 2575698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 2576698b1b30SVlastimil Babka 2577698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 2578698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 2579698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 2580698b1b30SVlastimil Babka } 2581e46b1db2SAnna-Maria Gleixner return 0; 2582698b1b30SVlastimil Babka } 2583698b1b30SVlastimil Babka 2584698b1b30SVlastimil Babka static int __init kcompactd_init(void) 2585698b1b30SVlastimil Babka { 2586698b1b30SVlastimil Babka int nid; 2587e46b1db2SAnna-Maria Gleixner int ret; 2588e46b1db2SAnna-Maria Gleixner 2589e46b1db2SAnna-Maria Gleixner ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 2590e46b1db2SAnna-Maria Gleixner "mm/compaction:online", 2591e46b1db2SAnna-Maria Gleixner kcompactd_cpu_online, NULL); 2592e46b1db2SAnna-Maria Gleixner if (ret < 0) { 2593e46b1db2SAnna-Maria Gleixner pr_err("kcompactd: failed to register hotplug callbacks.\n"); 2594e46b1db2SAnna-Maria Gleixner return ret; 2595e46b1db2SAnna-Maria Gleixner } 2596698b1b30SVlastimil Babka 2597698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 2598698b1b30SVlastimil Babka kcompactd_run(nid); 2599698b1b30SVlastimil Babka return 0; 2600698b1b30SVlastimil Babka } 2601698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 2602698b1b30SVlastimil Babka 2603ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 2604