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, 333d097a6f6SMel Gorman struct page *page, unsigned long pfn) 334bb13ffebSMel Gorman { 335c89511abSMel Gorman struct zone *zone = cc->zone; 3366815bf3fSJoonsoo Kim 3372583d671SVlastimil Babka if (cc->no_set_skip_hint) 3386815bf3fSJoonsoo Kim return; 3396815bf3fSJoonsoo Kim 340bb13ffebSMel Gorman if (!page) 341bb13ffebSMel Gorman return; 342bb13ffebSMel Gorman 343bb13ffebSMel Gorman set_pageblock_skip(page); 344c89511abSMel Gorman 34535979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 34635979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 347c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 348c89511abSMel Gorman } 349bb13ffebSMel Gorman #else 350bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 351bb13ffebSMel Gorman struct page *page) 352bb13ffebSMel Gorman { 353bb13ffebSMel Gorman return true; 354bb13ffebSMel Gorman } 355bb13ffebSMel Gorman 356b527cfe5SVlastimil Babka static inline bool pageblock_skip_persistent(struct page *page) 35721dc7e02SDavid Rientjes { 35821dc7e02SDavid Rientjes return false; 35921dc7e02SDavid Rientjes } 36021dc7e02SDavid Rientjes 36121dc7e02SDavid Rientjes static inline void update_pageblock_skip(struct compact_control *cc, 362d097a6f6SMel Gorman struct page *page, unsigned long pfn) 363bb13ffebSMel Gorman { 364bb13ffebSMel Gorman } 365e380bebeSMel Gorman 366e380bebeSMel Gorman static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) 367e380bebeSMel Gorman { 368e380bebeSMel Gorman } 369e380bebeSMel Gorman 370e380bebeSMel Gorman static bool test_and_set_skip(struct compact_control *cc, struct page *page, 371e380bebeSMel Gorman unsigned long pfn) 372e380bebeSMel Gorman { 373e380bebeSMel Gorman return false; 374e380bebeSMel Gorman } 375bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 376bb13ffebSMel Gorman 3771f9efdefSVlastimil Babka /* 3788b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 379cb2dcaf0SMel Gorman * very heavily contended. For async compaction, trylock and record if the 380cb2dcaf0SMel Gorman * lock is contended. The lock will still be acquired but compaction will 381cb2dcaf0SMel Gorman * abort when the current block is finished regardless of success rate. 382cb2dcaf0SMel Gorman * Sync compaction acquires the lock. 3838b44d279SVlastimil Babka * 384cb2dcaf0SMel Gorman * Always returns true which makes it easier to track lock state in callers. 3851f9efdefSVlastimil Babka */ 386cb2dcaf0SMel Gorman static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, 3878b44d279SVlastimil Babka struct compact_control *cc) 3888b44d279SVlastimil Babka { 389cb2dcaf0SMel Gorman /* Track if the lock is contended in async mode */ 390cb2dcaf0SMel Gorman if (cc->mode == MIGRATE_ASYNC && !cc->contended) { 391cb2dcaf0SMel Gorman if (spin_trylock_irqsave(lock, *flags)) 392cb2dcaf0SMel Gorman return true; 393cb2dcaf0SMel Gorman 394c3486f53SVlastimil Babka cc->contended = true; 3958b44d279SVlastimil Babka } 3961f9efdefSVlastimil Babka 397cb2dcaf0SMel Gorman spin_lock_irqsave(lock, *flags); 3988b44d279SVlastimil Babka return true; 3992a1402aaSMel Gorman } 4002a1402aaSMel Gorman 40185aa125fSMichal Nazarewicz /* 402c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 4038b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 4048b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 4058b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 4068b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 4078b44d279SVlastimil Babka * aborts. Sync compaction schedules. 4088b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 4098b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 410c67fe375SMel Gorman * 4118b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 4128b44d279SVlastimil Babka * async compaction due to need_resched() 4138b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 4148b44d279SVlastimil Babka * scheduled) 415c67fe375SMel Gorman */ 4168b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 4178b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 418c67fe375SMel Gorman { 4198b44d279SVlastimil Babka if (*locked) { 4208b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 4218b44d279SVlastimil Babka *locked = false; 422c67fe375SMel Gorman } 423c67fe375SMel Gorman 4248b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 425c3486f53SVlastimil Babka cc->contended = true; 4268b44d279SVlastimil Babka return true; 4278b44d279SVlastimil Babka } 4288b44d279SVlastimil Babka 429cf66f070SMel Gorman cond_resched(); 430be976572SVlastimil Babka 431be976572SVlastimil Babka return false; 432be976572SVlastimil Babka } 433be976572SVlastimil Babka 434c67fe375SMel Gorman /* 4359e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 4369e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 4379e4be470SJerome Marchand * (even though it may still end up isolating some pages). 43885aa125fSMichal Nazarewicz */ 439f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 440e14c720eSVlastimil Babka unsigned long *start_pfn, 44185aa125fSMichal Nazarewicz unsigned long end_pfn, 44285aa125fSMichal Nazarewicz struct list_head *freelist, 443*4fca9730SMel Gorman unsigned int stride, 44485aa125fSMichal Nazarewicz bool strict) 445748446bbSMel Gorman { 446b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 447d097a6f6SMel Gorman struct page *cursor; 448b8b2d825SXiubo Li unsigned long flags = 0; 449f40d1e42SMel Gorman bool locked = false; 450e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 45166c64223SJoonsoo Kim unsigned int order; 452748446bbSMel Gorman 453*4fca9730SMel Gorman /* Strict mode is for isolation, speed is secondary */ 454*4fca9730SMel Gorman if (strict) 455*4fca9730SMel Gorman stride = 1; 456*4fca9730SMel Gorman 457748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 458748446bbSMel Gorman 459f40d1e42SMel Gorman /* Isolate free pages. */ 460*4fca9730SMel Gorman for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) { 46166c64223SJoonsoo Kim int isolated; 462748446bbSMel Gorman struct page *page = cursor; 463748446bbSMel Gorman 4648b44d279SVlastimil Babka /* 4658b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 4668b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 4678b44d279SVlastimil Babka * pending or async compaction detects need_resched() 4688b44d279SVlastimil Babka */ 4698b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 4708b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 4718b44d279SVlastimil Babka &locked, cc)) 4728b44d279SVlastimil Babka break; 4738b44d279SVlastimil Babka 474b7aba698SMel Gorman nr_scanned++; 475f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 4762af120bcSLaura Abbott goto isolate_fail; 4772af120bcSLaura Abbott 4789fcd6d2eSVlastimil Babka /* 4799fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 4809fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 4819fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 4829fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 4839fcd6d2eSVlastimil Babka */ 4849fcd6d2eSVlastimil Babka if (PageCompound(page)) { 48521dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 4869fcd6d2eSVlastimil Babka 487d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) { 48821dc7e02SDavid Rientjes blockpfn += (1UL << order) - 1; 48921dc7e02SDavid Rientjes cursor += (1UL << order) - 1; 4909fcd6d2eSVlastimil Babka } 4919fcd6d2eSVlastimil Babka goto isolate_fail; 4929fcd6d2eSVlastimil Babka } 4939fcd6d2eSVlastimil Babka 494f40d1e42SMel Gorman if (!PageBuddy(page)) 4952af120bcSLaura Abbott goto isolate_fail; 496f40d1e42SMel Gorman 497f40d1e42SMel Gorman /* 49869b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 49969b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 50069b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 50169b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 50269b7189fSVlastimil Babka * recheck as well. 50369b7189fSVlastimil Babka */ 50469b7189fSVlastimil Babka if (!locked) { 505cb2dcaf0SMel Gorman locked = compact_lock_irqsave(&cc->zone->lock, 5068b44d279SVlastimil Babka &flags, cc); 507f40d1e42SMel Gorman 508f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 509f40d1e42SMel Gorman if (!PageBuddy(page)) 5102af120bcSLaura Abbott goto isolate_fail; 51169b7189fSVlastimil Babka } 512748446bbSMel Gorman 51366c64223SJoonsoo Kim /* Found a free page, will break it into order-0 pages */ 51466c64223SJoonsoo Kim order = page_order(page); 51566c64223SJoonsoo Kim isolated = __isolate_free_page(page, order); 516a4f04f2cSDavid Rientjes if (!isolated) 517a4f04f2cSDavid Rientjes break; 51866c64223SJoonsoo Kim set_page_private(page, order); 519a4f04f2cSDavid Rientjes 520748446bbSMel Gorman total_isolated += isolated; 521a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 52266c64223SJoonsoo Kim list_add_tail(&page->lru, freelist); 52366c64223SJoonsoo Kim 524a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 525932ff6bbSJoonsoo Kim blockpfn += isolated; 526932ff6bbSJoonsoo Kim break; 527932ff6bbSJoonsoo Kim } 528a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 529748446bbSMel Gorman blockpfn += isolated - 1; 530748446bbSMel Gorman cursor += isolated - 1; 5312af120bcSLaura Abbott continue; 5322af120bcSLaura Abbott 5332af120bcSLaura Abbott isolate_fail: 5342af120bcSLaura Abbott if (strict) 5352af120bcSLaura Abbott break; 5362af120bcSLaura Abbott else 5372af120bcSLaura Abbott continue; 5382af120bcSLaura Abbott 539748446bbSMel Gorman } 540748446bbSMel Gorman 541a4f04f2cSDavid Rientjes if (locked) 542a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 543a4f04f2cSDavid Rientjes 5449fcd6d2eSVlastimil Babka /* 5459fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 5469fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 5479fcd6d2eSVlastimil Babka */ 5489fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 5499fcd6d2eSVlastimil Babka blockpfn = end_pfn; 5509fcd6d2eSVlastimil Babka 551e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 552e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 553e34d85f0SJoonsoo Kim 554e14c720eSVlastimil Babka /* Record how far we have got within the block */ 555e14c720eSVlastimil Babka *start_pfn = blockpfn; 556e14c720eSVlastimil Babka 557f40d1e42SMel Gorman /* 558f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 559f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 560f40d1e42SMel Gorman * returned and CMA will fail. 561f40d1e42SMel Gorman */ 5622af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 563f40d1e42SMel Gorman total_isolated = 0; 564f40d1e42SMel Gorman 5657f354a54SDavid Rientjes cc->total_free_scanned += nr_scanned; 566397487dbSMel Gorman if (total_isolated) 567010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 568748446bbSMel Gorman return total_isolated; 569748446bbSMel Gorman } 570748446bbSMel Gorman 57185aa125fSMichal Nazarewicz /** 57285aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 573e8b098fcSMike Rapoport * @cc: Compaction control structure. 57485aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 57585aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 57685aa125fSMichal Nazarewicz * 57785aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 57885aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 57985aa125fSMichal Nazarewicz * undo its actions and return zero. 58085aa125fSMichal Nazarewicz * 58185aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 58285aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 58385aa125fSMichal Nazarewicz * a free page). 58485aa125fSMichal Nazarewicz */ 585ff9543fdSMichal Nazarewicz unsigned long 586bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 587bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 58885aa125fSMichal Nazarewicz { 589e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 59085aa125fSMichal Nazarewicz LIST_HEAD(freelist); 59185aa125fSMichal Nazarewicz 5927d49d886SVlastimil Babka pfn = start_pfn; 59306b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 594e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 595e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 59606b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 5977d49d886SVlastimil Babka 5987d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 599e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 6007d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 601e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 602e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 6037d49d886SVlastimil Babka 60485aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 60585aa125fSMichal Nazarewicz 60658420016SJoonsoo Kim /* 60758420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 60858420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 60958420016SJoonsoo Kim * scanning range to right one. 61058420016SJoonsoo Kim */ 61158420016SJoonsoo Kim if (pfn >= block_end_pfn) { 61206b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 61306b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 61458420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 61558420016SJoonsoo Kim } 61658420016SJoonsoo Kim 617e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 618e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 6197d49d886SVlastimil Babka break; 6207d49d886SVlastimil Babka 621e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 622*4fca9730SMel Gorman block_end_pfn, &freelist, 0, true); 62385aa125fSMichal Nazarewicz 62485aa125fSMichal Nazarewicz /* 62585aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 62685aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 62785aa125fSMichal Nazarewicz * non-free pages). 62885aa125fSMichal Nazarewicz */ 62985aa125fSMichal Nazarewicz if (!isolated) 63085aa125fSMichal Nazarewicz break; 63185aa125fSMichal Nazarewicz 63285aa125fSMichal Nazarewicz /* 63385aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 63485aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 63585aa125fSMichal Nazarewicz * page may span two pageblocks). 63685aa125fSMichal Nazarewicz */ 63785aa125fSMichal Nazarewicz } 63885aa125fSMichal Nazarewicz 63966c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 6404469ab98SMel Gorman split_map_pages(&freelist); 64185aa125fSMichal Nazarewicz 64285aa125fSMichal Nazarewicz if (pfn < end_pfn) { 64385aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 64485aa125fSMichal Nazarewicz release_freepages(&freelist); 64585aa125fSMichal Nazarewicz return 0; 64685aa125fSMichal Nazarewicz } 64785aa125fSMichal Nazarewicz 64885aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 64985aa125fSMichal Nazarewicz return pfn; 65085aa125fSMichal Nazarewicz } 65185aa125fSMichal Nazarewicz 652748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 653748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 654748446bbSMel Gorman { 655bc693045SMinchan Kim unsigned long active, inactive, isolated; 656748446bbSMel Gorman 657599d0c95SMel Gorman inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) + 658599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON); 659599d0c95SMel Gorman active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) + 660599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON); 661599d0c95SMel Gorman isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) + 662599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON); 663748446bbSMel Gorman 664bc693045SMinchan Kim return isolated > (inactive + active) / 2; 665748446bbSMel Gorman } 666748446bbSMel Gorman 6672fe86e00SMichal Nazarewicz /** 668edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 669edc2ca61SVlastimil Babka * a single pageblock 6702fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 671edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 672edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 673edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6742fe86e00SMichal Nazarewicz * 6752fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 676edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 677edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 678edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 679edc2ca61SVlastimil Babka * than end_pfn). 6802fe86e00SMichal Nazarewicz * 681edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 682edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 683edc2ca61SVlastimil Babka * is neither read nor updated. 684748446bbSMel Gorman */ 685edc2ca61SVlastimil Babka static unsigned long 686edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 687edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 688748446bbSMel Gorman { 689edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 690b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 691fa9add64SHugh Dickins struct lruvec *lruvec; 692b8b2d825SXiubo Li unsigned long flags = 0; 6932a1402aaSMel Gorman bool locked = false; 694bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 695e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 696fdd048e1SVlastimil Babka bool skip_on_failure = false; 697fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 698e380bebeSMel Gorman bool skip_updated = false; 699748446bbSMel Gorman 700748446bbSMel Gorman /* 701748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 702748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 703748446bbSMel Gorman * delay for some time until fewer pages are isolated 704748446bbSMel Gorman */ 705748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 706f9e35b3bSMel Gorman /* async migration should just abort */ 707e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 7082fe86e00SMichal Nazarewicz return 0; 709f9e35b3bSMel Gorman 710748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 711748446bbSMel Gorman 712748446bbSMel Gorman if (fatal_signal_pending(current)) 7132fe86e00SMichal Nazarewicz return 0; 714748446bbSMel Gorman } 715748446bbSMel Gorman 716cf66f070SMel Gorman cond_resched(); 717aeef4b83SDavid Rientjes 718fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 719fdd048e1SVlastimil Babka skip_on_failure = true; 720fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 721fdd048e1SVlastimil Babka } 722fdd048e1SVlastimil Babka 723748446bbSMel Gorman /* Time to isolate some pages for migration */ 724748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 72529c0dde8SVlastimil Babka 726fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 727fdd048e1SVlastimil Babka /* 728fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 729fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 730fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 731fdd048e1SVlastimil Babka * hopefully succeed compaction. 732fdd048e1SVlastimil Babka */ 733fdd048e1SVlastimil Babka if (nr_isolated) 734fdd048e1SVlastimil Babka break; 735fdd048e1SVlastimil Babka 736fdd048e1SVlastimil Babka /* 737fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 738fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 739fdd048e1SVlastimil Babka * current block. Note we can't simply increase 740fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 741fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 742fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 743fdd048e1SVlastimil Babka * previous loop iteration. 744fdd048e1SVlastimil Babka */ 745fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 746fdd048e1SVlastimil Babka } 747fdd048e1SVlastimil Babka 7488b44d279SVlastimil Babka /* 7498b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 7508b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7518b44d279SVlastimil Babka * if contended. 7528b44d279SVlastimil Babka */ 7538b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 754a52633d8SMel Gorman && compact_unlock_should_abort(zone_lru_lock(zone), flags, 7558b44d279SVlastimil Babka &locked, cc)) 7568b44d279SVlastimil Babka break; 757b2eef8c0SAndrea Arcangeli 758748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 759fdd048e1SVlastimil Babka goto isolate_fail; 760b7aba698SMel Gorman nr_scanned++; 761748446bbSMel Gorman 762748446bbSMel Gorman page = pfn_to_page(low_pfn); 763dc908600SMel Gorman 764e380bebeSMel Gorman /* 765e380bebeSMel Gorman * Check if the pageblock has already been marked skipped. 766e380bebeSMel Gorman * Only the aligned PFN is checked as the caller isolates 767e380bebeSMel Gorman * COMPACT_CLUSTER_MAX at a time so the second call must 768e380bebeSMel Gorman * not falsely conclude that the block should be skipped. 769e380bebeSMel Gorman */ 770e380bebeSMel Gorman if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { 771e380bebeSMel Gorman if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { 772e380bebeSMel Gorman low_pfn = end_pfn; 773e380bebeSMel Gorman goto isolate_abort; 774e380bebeSMel Gorman } 775bb13ffebSMel Gorman valid_page = page; 776e380bebeSMel Gorman } 777bb13ffebSMel Gorman 778c122b208SJoonsoo Kim /* 77999c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 78099c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 78199c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 78299c0fd5eSVlastimil Babka * potential isolation targets. 7836c14466cSMel Gorman */ 78499c0fd5eSVlastimil Babka if (PageBuddy(page)) { 78599c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 78699c0fd5eSVlastimil Babka 78799c0fd5eSVlastimil Babka /* 78899c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 78999c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 79099c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 79199c0fd5eSVlastimil Babka */ 79299c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 79399c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 794748446bbSMel Gorman continue; 79599c0fd5eSVlastimil Babka } 796748446bbSMel Gorman 7979927af74SMel Gorman /* 79829c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 79929c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 80029c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 80129c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 80229c0dde8SVlastimil Babka * danger is skipping too much. 803bc835011SAndrea Arcangeli */ 80429c0dde8SVlastimil Babka if (PageCompound(page)) { 80521dc7e02SDavid Rientjes const unsigned int order = compound_order(page); 80629c0dde8SVlastimil Babka 807d3c85badSVlastimil Babka if (likely(order < MAX_ORDER)) 80821dc7e02SDavid Rientjes low_pfn += (1UL << order) - 1; 809fdd048e1SVlastimil Babka goto isolate_fail; 8102a1402aaSMel Gorman } 8112a1402aaSMel Gorman 812bda807d4SMinchan Kim /* 813bda807d4SMinchan Kim * Check may be lockless but that's ok as we recheck later. 814bda807d4SMinchan Kim * It's possible to migrate LRU and non-lru movable pages. 815bda807d4SMinchan Kim * Skip any other type of page 816bda807d4SMinchan Kim */ 817bda807d4SMinchan Kim if (!PageLRU(page)) { 818bda807d4SMinchan Kim /* 819bda807d4SMinchan Kim * __PageMovable can return false positive so we need 820bda807d4SMinchan Kim * to verify it under page_lock. 821bda807d4SMinchan Kim */ 822bda807d4SMinchan Kim if (unlikely(__PageMovable(page)) && 823bda807d4SMinchan Kim !PageIsolated(page)) { 824bda807d4SMinchan Kim if (locked) { 825a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), 826bda807d4SMinchan Kim flags); 827bda807d4SMinchan Kim locked = false; 828bda807d4SMinchan Kim } 829bda807d4SMinchan Kim 8309e5bcd61SYisheng Xie if (!isolate_movable_page(page, isolate_mode)) 831bda807d4SMinchan Kim goto isolate_success; 832bda807d4SMinchan Kim } 833bda807d4SMinchan Kim 834fdd048e1SVlastimil Babka goto isolate_fail; 835bda807d4SMinchan Kim } 83629c0dde8SVlastimil Babka 837119d6d59SDavid Rientjes /* 838119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 839119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 840119d6d59SDavid Rientjes * admittedly racy check. 841119d6d59SDavid Rientjes */ 842119d6d59SDavid Rientjes if (!page_mapping(page) && 843119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 844fdd048e1SVlastimil Babka goto isolate_fail; 845119d6d59SDavid Rientjes 84673e64c51SMichal Hocko /* 84773e64c51SMichal Hocko * Only allow to migrate anonymous pages in GFP_NOFS context 84873e64c51SMichal Hocko * because those do not depend on fs locks. 84973e64c51SMichal Hocko */ 85073e64c51SMichal Hocko if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page)) 85173e64c51SMichal Hocko goto isolate_fail; 85273e64c51SMichal Hocko 85369b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 85469b7189fSVlastimil Babka if (!locked) { 855cb2dcaf0SMel Gorman locked = compact_lock_irqsave(zone_lru_lock(zone), 8568b44d279SVlastimil Babka &flags, cc); 857e380bebeSMel Gorman 858e380bebeSMel Gorman /* Try get exclusive access under lock */ 859e380bebeSMel Gorman if (!skip_updated) { 860e380bebeSMel Gorman skip_updated = true; 861e380bebeSMel Gorman if (test_and_set_skip(cc, page, low_pfn)) 862e380bebeSMel Gorman goto isolate_abort; 863e380bebeSMel Gorman } 8642a1402aaSMel Gorman 86529c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 8662a1402aaSMel Gorman if (!PageLRU(page)) 867fdd048e1SVlastimil Babka goto isolate_fail; 86829c0dde8SVlastimil Babka 86929c0dde8SVlastimil Babka /* 87029c0dde8SVlastimil Babka * Page become compound since the non-locked check, 87129c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 87229c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 87329c0dde8SVlastimil Babka */ 87429c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 875d3c85badSVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 876fdd048e1SVlastimil Babka goto isolate_fail; 877bc835011SAndrea Arcangeli } 87869b7189fSVlastimil Babka } 879bc835011SAndrea Arcangeli 880599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 881fa9add64SHugh Dickins 882748446bbSMel Gorman /* Try isolate the page */ 883edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 884fdd048e1SVlastimil Babka goto isolate_fail; 885748446bbSMel Gorman 88629c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 887bc835011SAndrea Arcangeli 888748446bbSMel Gorman /* Successfully isolated */ 889fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 8906afcf8efSMing Ling inc_node_page_state(page, 8916afcf8efSMing Ling NR_ISOLATED_ANON + page_is_file_cache(page)); 892b6c75016SJoonsoo Kim 893b6c75016SJoonsoo Kim isolate_success: 894fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 895748446bbSMel Gorman cc->nr_migratepages++; 896b7aba698SMel Gorman nr_isolated++; 897748446bbSMel Gorman 898804d3121SMel Gorman /* 899804d3121SMel Gorman * Avoid isolating too much unless this block is being 900cb2dcaf0SMel Gorman * rescanned (e.g. dirty/writeback pages, parallel allocation) 901cb2dcaf0SMel Gorman * or a lock is contended. For contention, isolate quickly to 902cb2dcaf0SMel Gorman * potentially remove one source of contention. 903804d3121SMel Gorman */ 904cb2dcaf0SMel Gorman if (cc->nr_migratepages == COMPACT_CLUSTER_MAX && 905cb2dcaf0SMel Gorman !cc->rescan && !cc->contended) { 90631b8384aSHillf Danton ++low_pfn; 907748446bbSMel Gorman break; 908748446bbSMel Gorman } 909fdd048e1SVlastimil Babka 910fdd048e1SVlastimil Babka continue; 911fdd048e1SVlastimil Babka isolate_fail: 912fdd048e1SVlastimil Babka if (!skip_on_failure) 913fdd048e1SVlastimil Babka continue; 914fdd048e1SVlastimil Babka 915fdd048e1SVlastimil Babka /* 916fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 917fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 918fdd048e1SVlastimil Babka * page anyway. 919fdd048e1SVlastimil Babka */ 920fdd048e1SVlastimil Babka if (nr_isolated) { 921fdd048e1SVlastimil Babka if (locked) { 922a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 923fdd048e1SVlastimil Babka locked = false; 924fdd048e1SVlastimil Babka } 925fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 926fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 927fdd048e1SVlastimil Babka nr_isolated = 0; 928fdd048e1SVlastimil Babka } 929fdd048e1SVlastimil Babka 930fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 931fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 932fdd048e1SVlastimil Babka /* 933fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 934fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 935fdd048e1SVlastimil Babka */ 936fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 937fdd048e1SVlastimil Babka } 93831b8384aSHillf Danton } 939748446bbSMel Gorman 94099c0fd5eSVlastimil Babka /* 94199c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 94299c0fd5eSVlastimil Babka * the range to be scanned. 94399c0fd5eSVlastimil Babka */ 94499c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 94599c0fd5eSVlastimil Babka low_pfn = end_pfn; 94699c0fd5eSVlastimil Babka 947e380bebeSMel Gorman isolate_abort: 948c67fe375SMel Gorman if (locked) 949a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 950748446bbSMel Gorman 95150b5b094SVlastimil Babka /* 952804d3121SMel Gorman * Updated the cached scanner pfn once the pageblock has been scanned 953804d3121SMel Gorman * Pages will either be migrated in which case there is no point 954804d3121SMel Gorman * scanning in the near future or migration failed in which case the 955804d3121SMel Gorman * failure reason may persist. The block is marked for skipping if 956804d3121SMel Gorman * there were no pages isolated in the block or if the block is 957804d3121SMel Gorman * rescanned twice in a row. 95850b5b094SVlastimil Babka */ 959804d3121SMel Gorman if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { 960e380bebeSMel Gorman if (valid_page && !skip_updated) 961e380bebeSMel Gorman set_pageblock_skip(valid_page); 962e380bebeSMel Gorman update_cached_migrate(cc, low_pfn); 963e380bebeSMel Gorman } 964bb13ffebSMel Gorman 965e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 966e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 967b7aba698SMel Gorman 9687f354a54SDavid Rientjes cc->total_migrate_scanned += nr_scanned; 969397487dbSMel Gorman if (nr_isolated) 970010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 971397487dbSMel Gorman 9722fe86e00SMichal Nazarewicz return low_pfn; 9732fe86e00SMichal Nazarewicz } 9742fe86e00SMichal Nazarewicz 975edc2ca61SVlastimil Babka /** 976edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 977edc2ca61SVlastimil Babka * @cc: Compaction control structure. 978edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 979edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 980edc2ca61SVlastimil Babka * 981edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 982edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 983edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 984edc2ca61SVlastimil Babka */ 985edc2ca61SVlastimil Babka unsigned long 986edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 987edc2ca61SVlastimil Babka unsigned long end_pfn) 988edc2ca61SVlastimil Babka { 989e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 990edc2ca61SVlastimil Babka 991edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 992edc2ca61SVlastimil Babka pfn = start_pfn; 99306b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 994e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 995e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 99606b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 997edc2ca61SVlastimil Babka 998edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 999e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1000edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 1001edc2ca61SVlastimil Babka 1002edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 1003edc2ca61SVlastimil Babka 1004e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 1005e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 1006edc2ca61SVlastimil Babka continue; 1007edc2ca61SVlastimil Babka 1008edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 1009edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 1010edc2ca61SVlastimil Babka 101114af4a5eSHugh Dickins if (!pfn) 1012edc2ca61SVlastimil Babka break; 10136ea41c0cSJoonsoo Kim 10146ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 10156ea41c0cSJoonsoo Kim break; 1016edc2ca61SVlastimil Babka } 1017edc2ca61SVlastimil Babka 1018edc2ca61SVlastimil Babka return pfn; 1019edc2ca61SVlastimil Babka } 1020edc2ca61SVlastimil Babka 1021ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 1022ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 1023018e9a49SAndrew Morton 1024b682debdSVlastimil Babka static bool suitable_migration_source(struct compact_control *cc, 1025b682debdSVlastimil Babka struct page *page) 1026b682debdSVlastimil Babka { 1027282722b0SVlastimil Babka int block_mt; 1028282722b0SVlastimil Babka 10299bebefd5SMel Gorman if (pageblock_skip_persistent(page)) 10309bebefd5SMel Gorman return false; 10319bebefd5SMel Gorman 1032282722b0SVlastimil Babka if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) 1033b682debdSVlastimil Babka return true; 1034b682debdSVlastimil Babka 1035282722b0SVlastimil Babka block_mt = get_pageblock_migratetype(page); 1036282722b0SVlastimil Babka 1037282722b0SVlastimil Babka if (cc->migratetype == MIGRATE_MOVABLE) 1038282722b0SVlastimil Babka return is_migrate_movable(block_mt); 1039282722b0SVlastimil Babka else 1040282722b0SVlastimil Babka return block_mt == cc->migratetype; 1041b682debdSVlastimil Babka } 1042b682debdSVlastimil Babka 1043018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 10449f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc, 10459f7e3387SVlastimil Babka struct page *page) 1046018e9a49SAndrew Morton { 1047018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 1048018e9a49SAndrew Morton if (PageBuddy(page)) { 1049018e9a49SAndrew Morton /* 1050018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 1051018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 1052018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 1053018e9a49SAndrew Morton */ 1054018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 1055018e9a49SAndrew Morton return false; 1056018e9a49SAndrew Morton } 1057018e9a49SAndrew Morton 10581ef36db2SYisheng Xie if (cc->ignore_block_suitable) 10591ef36db2SYisheng Xie return true; 10601ef36db2SYisheng Xie 1061018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 1062b682debdSVlastimil Babka if (is_migrate_movable(get_pageblock_migratetype(page))) 1063018e9a49SAndrew Morton return true; 1064018e9a49SAndrew Morton 1065018e9a49SAndrew Morton /* Otherwise skip the block */ 1066018e9a49SAndrew Morton return false; 1067018e9a49SAndrew Morton } 1068018e9a49SAndrew Morton 106970b44595SMel Gorman static inline unsigned int 107070b44595SMel Gorman freelist_scan_limit(struct compact_control *cc) 107170b44595SMel Gorman { 107270b44595SMel Gorman return (COMPACT_CLUSTER_MAX >> cc->fast_search_fail) + 1; 107370b44595SMel Gorman } 107470b44595SMel Gorman 1075ff9543fdSMichal Nazarewicz /* 1076f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 1077f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 1078f2849aa0SVlastimil Babka */ 1079f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 1080f2849aa0SVlastimil Babka { 1081f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 1082f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 1083f2849aa0SVlastimil Babka } 1084f2849aa0SVlastimil Babka 10855a811889SMel Gorman /* 10865a811889SMel Gorman * Used when scanning for a suitable migration target which scans freelists 10875a811889SMel Gorman * in reverse. Reorders the list such as the unscanned pages are scanned 10885a811889SMel Gorman * first on the next iteration of the free scanner 10895a811889SMel Gorman */ 10905a811889SMel Gorman static void 10915a811889SMel Gorman move_freelist_head(struct list_head *freelist, struct page *freepage) 10925a811889SMel Gorman { 10935a811889SMel Gorman LIST_HEAD(sublist); 10945a811889SMel Gorman 10955a811889SMel Gorman if (!list_is_last(freelist, &freepage->lru)) { 10965a811889SMel Gorman list_cut_before(&sublist, freelist, &freepage->lru); 10975a811889SMel Gorman if (!list_empty(&sublist)) 10985a811889SMel Gorman list_splice_tail(&sublist, freelist); 10995a811889SMel Gorman } 11005a811889SMel Gorman } 11015a811889SMel Gorman 11025a811889SMel Gorman /* 11035a811889SMel Gorman * Similar to move_freelist_head except used by the migration scanner 11045a811889SMel Gorman * when scanning forward. It's possible for these list operations to 11055a811889SMel Gorman * move against each other if they search the free list exactly in 11065a811889SMel Gorman * lockstep. 11075a811889SMel Gorman */ 110870b44595SMel Gorman static void 110970b44595SMel Gorman move_freelist_tail(struct list_head *freelist, struct page *freepage) 111070b44595SMel Gorman { 111170b44595SMel Gorman LIST_HEAD(sublist); 111270b44595SMel Gorman 111370b44595SMel Gorman if (!list_is_first(freelist, &freepage->lru)) { 111470b44595SMel Gorman list_cut_position(&sublist, freelist, &freepage->lru); 111570b44595SMel Gorman if (!list_empty(&sublist)) 111670b44595SMel Gorman list_splice_tail(&sublist, freelist); 111770b44595SMel Gorman } 111870b44595SMel Gorman } 111970b44595SMel Gorman 11205a811889SMel Gorman static void 11215a811889SMel Gorman fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) 11225a811889SMel Gorman { 11235a811889SMel Gorman unsigned long start_pfn, end_pfn; 11245a811889SMel Gorman struct page *page = pfn_to_page(pfn); 11255a811889SMel Gorman 11265a811889SMel Gorman /* Do not search around if there are enough pages already */ 11275a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 11285a811889SMel Gorman return; 11295a811889SMel Gorman 11305a811889SMel Gorman /* Minimise scanning during async compaction */ 11315a811889SMel Gorman if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) 11325a811889SMel Gorman return; 11335a811889SMel Gorman 11345a811889SMel Gorman /* Pageblock boundaries */ 11355a811889SMel Gorman start_pfn = pageblock_start_pfn(pfn); 11365a811889SMel Gorman end_pfn = min(start_pfn + pageblock_nr_pages, zone_end_pfn(cc->zone)); 11375a811889SMel Gorman 11385a811889SMel Gorman /* Scan before */ 11395a811889SMel Gorman if (start_pfn != pfn) { 1140*4fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false); 11415a811889SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) 11425a811889SMel Gorman return; 11435a811889SMel Gorman } 11445a811889SMel Gorman 11455a811889SMel Gorman /* Scan after */ 11465a811889SMel Gorman start_pfn = pfn + nr_isolated; 11475a811889SMel Gorman if (start_pfn != end_pfn) 1148*4fca9730SMel Gorman isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false); 11495a811889SMel Gorman 11505a811889SMel Gorman /* Skip this pageblock in the future as it's full or nearly full */ 11515a811889SMel Gorman if (cc->nr_freepages < cc->nr_migratepages) 11525a811889SMel Gorman set_pageblock_skip(page); 11535a811889SMel Gorman } 11545a811889SMel Gorman 1155dbe2d4e4SMel Gorman /* Search orders in round-robin fashion */ 1156dbe2d4e4SMel Gorman static int next_search_order(struct compact_control *cc, int order) 1157dbe2d4e4SMel Gorman { 1158dbe2d4e4SMel Gorman order--; 1159dbe2d4e4SMel Gorman if (order < 0) 1160dbe2d4e4SMel Gorman order = cc->order - 1; 1161dbe2d4e4SMel Gorman 1162dbe2d4e4SMel Gorman /* Search wrapped around? */ 1163dbe2d4e4SMel Gorman if (order == cc->search_order) { 1164dbe2d4e4SMel Gorman cc->search_order--; 1165dbe2d4e4SMel Gorman if (cc->search_order < 0) 1166dbe2d4e4SMel Gorman cc->search_order = cc->order - 1; 1167dbe2d4e4SMel Gorman return -1; 1168dbe2d4e4SMel Gorman } 1169dbe2d4e4SMel Gorman 1170dbe2d4e4SMel Gorman return order; 1171dbe2d4e4SMel Gorman } 1172dbe2d4e4SMel Gorman 11735a811889SMel Gorman static unsigned long 11745a811889SMel Gorman fast_isolate_freepages(struct compact_control *cc) 11755a811889SMel Gorman { 11765a811889SMel Gorman unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1); 11775a811889SMel Gorman unsigned int nr_scanned = 0; 11785a811889SMel Gorman unsigned long low_pfn, min_pfn, high_pfn = 0, highest = 0; 11795a811889SMel Gorman unsigned long nr_isolated = 0; 11805a811889SMel Gorman unsigned long distance; 11815a811889SMel Gorman struct page *page = NULL; 11825a811889SMel Gorman bool scan_start = false; 11835a811889SMel Gorman int order; 11845a811889SMel Gorman 11855a811889SMel Gorman /* Full compaction passes in a negative order */ 11865a811889SMel Gorman if (cc->order <= 0) 11875a811889SMel Gorman return cc->free_pfn; 11885a811889SMel Gorman 11895a811889SMel Gorman /* 11905a811889SMel Gorman * If starting the scan, use a deeper search and use the highest 11915a811889SMel Gorman * PFN found if a suitable one is not found. 11925a811889SMel Gorman */ 11935a811889SMel Gorman if (cc->free_pfn == pageblock_start_pfn(zone_end_pfn(cc->zone) - 1)) { 11945a811889SMel Gorman limit = pageblock_nr_pages >> 1; 11955a811889SMel Gorman scan_start = true; 11965a811889SMel Gorman } 11975a811889SMel Gorman 11985a811889SMel Gorman /* 11995a811889SMel Gorman * Preferred point is in the top quarter of the scan space but take 12005a811889SMel Gorman * a pfn from the top half if the search is problematic. 12015a811889SMel Gorman */ 12025a811889SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn); 12035a811889SMel Gorman low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); 12045a811889SMel Gorman min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); 12055a811889SMel Gorman 12065a811889SMel Gorman if (WARN_ON_ONCE(min_pfn > low_pfn)) 12075a811889SMel Gorman low_pfn = min_pfn; 12085a811889SMel Gorman 1209dbe2d4e4SMel Gorman /* 1210dbe2d4e4SMel Gorman * Search starts from the last successful isolation order or the next 1211dbe2d4e4SMel Gorman * order to search after a previous failure 1212dbe2d4e4SMel Gorman */ 1213dbe2d4e4SMel Gorman cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); 1214dbe2d4e4SMel Gorman 1215dbe2d4e4SMel Gorman for (order = cc->search_order; 1216dbe2d4e4SMel Gorman !page && order >= 0; 1217dbe2d4e4SMel Gorman order = next_search_order(cc, order)) { 12185a811889SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 12195a811889SMel Gorman struct list_head *freelist; 12205a811889SMel Gorman struct page *freepage; 12215a811889SMel Gorman unsigned long flags; 12225a811889SMel Gorman unsigned int order_scanned = 0; 12235a811889SMel Gorman 12245a811889SMel Gorman if (!area->nr_free) 12255a811889SMel Gorman continue; 12265a811889SMel Gorman 12275a811889SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 12285a811889SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 12295a811889SMel Gorman list_for_each_entry_reverse(freepage, freelist, lru) { 12305a811889SMel Gorman unsigned long pfn; 12315a811889SMel Gorman 12325a811889SMel Gorman order_scanned++; 12335a811889SMel Gorman nr_scanned++; 12345a811889SMel Gorman pfn = page_to_pfn(freepage); 12355a811889SMel Gorman 12365a811889SMel Gorman if (pfn >= highest) 12375a811889SMel Gorman highest = pageblock_start_pfn(pfn); 12385a811889SMel Gorman 12395a811889SMel Gorman if (pfn >= low_pfn) { 12405a811889SMel Gorman cc->fast_search_fail = 0; 1241dbe2d4e4SMel Gorman cc->search_order = order; 12425a811889SMel Gorman page = freepage; 12435a811889SMel Gorman break; 12445a811889SMel Gorman } 12455a811889SMel Gorman 12465a811889SMel Gorman if (pfn >= min_pfn && pfn > high_pfn) { 12475a811889SMel Gorman high_pfn = pfn; 12485a811889SMel Gorman 12495a811889SMel Gorman /* Shorten the scan if a candidate is found */ 12505a811889SMel Gorman limit >>= 1; 12515a811889SMel Gorman } 12525a811889SMel Gorman 12535a811889SMel Gorman if (order_scanned >= limit) 12545a811889SMel Gorman break; 12555a811889SMel Gorman } 12565a811889SMel Gorman 12575a811889SMel Gorman /* Use a minimum pfn if a preferred one was not found */ 12585a811889SMel Gorman if (!page && high_pfn) { 12595a811889SMel Gorman page = pfn_to_page(high_pfn); 12605a811889SMel Gorman 12615a811889SMel Gorman /* Update freepage for the list reorder below */ 12625a811889SMel Gorman freepage = page; 12635a811889SMel Gorman } 12645a811889SMel Gorman 12655a811889SMel Gorman /* Reorder to so a future search skips recent pages */ 12665a811889SMel Gorman move_freelist_head(freelist, freepage); 12675a811889SMel Gorman 12685a811889SMel Gorman /* Isolate the page if available */ 12695a811889SMel Gorman if (page) { 12705a811889SMel Gorman if (__isolate_free_page(page, order)) { 12715a811889SMel Gorman set_page_private(page, order); 12725a811889SMel Gorman nr_isolated = 1 << order; 12735a811889SMel Gorman cc->nr_freepages += nr_isolated; 12745a811889SMel Gorman list_add_tail(&page->lru, &cc->freepages); 12755a811889SMel Gorman count_compact_events(COMPACTISOLATED, nr_isolated); 12765a811889SMel Gorman } else { 12775a811889SMel Gorman /* If isolation fails, abort the search */ 12785a811889SMel Gorman order = -1; 12795a811889SMel Gorman page = NULL; 12805a811889SMel Gorman } 12815a811889SMel Gorman } 12825a811889SMel Gorman 12835a811889SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 12845a811889SMel Gorman 12855a811889SMel Gorman /* 12865a811889SMel Gorman * Smaller scan on next order so the total scan ig related 12875a811889SMel Gorman * to freelist_scan_limit. 12885a811889SMel Gorman */ 12895a811889SMel Gorman if (order_scanned >= limit) 12905a811889SMel Gorman limit = min(1U, limit >> 1); 12915a811889SMel Gorman } 12925a811889SMel Gorman 12935a811889SMel Gorman if (!page) { 12945a811889SMel Gorman cc->fast_search_fail++; 12955a811889SMel Gorman if (scan_start) { 12965a811889SMel Gorman /* 12975a811889SMel Gorman * Use the highest PFN found above min. If one was 12985a811889SMel Gorman * not found, be pessemistic for direct compaction 12995a811889SMel Gorman * and use the min mark. 13005a811889SMel Gorman */ 13015a811889SMel Gorman if (highest) { 13025a811889SMel Gorman page = pfn_to_page(highest); 13035a811889SMel Gorman cc->free_pfn = highest; 13045a811889SMel Gorman } else { 13055a811889SMel Gorman if (cc->direct_compaction) { 13065a811889SMel Gorman page = pfn_to_page(min_pfn); 13075a811889SMel Gorman cc->free_pfn = min_pfn; 13085a811889SMel Gorman } 13095a811889SMel Gorman } 13105a811889SMel Gorman } 13115a811889SMel Gorman } 13125a811889SMel Gorman 1313d097a6f6SMel Gorman if (highest && highest >= cc->zone->compact_cached_free_pfn) { 1314d097a6f6SMel Gorman highest -= pageblock_nr_pages; 13155a811889SMel Gorman cc->zone->compact_cached_free_pfn = highest; 1316d097a6f6SMel Gorman } 13175a811889SMel Gorman 13185a811889SMel Gorman cc->total_free_scanned += nr_scanned; 13195a811889SMel Gorman if (!page) 13205a811889SMel Gorman return cc->free_pfn; 13215a811889SMel Gorman 13225a811889SMel Gorman low_pfn = page_to_pfn(page); 13235a811889SMel Gorman fast_isolate_around(cc, low_pfn, nr_isolated); 13245a811889SMel Gorman return low_pfn; 13255a811889SMel Gorman } 13265a811889SMel Gorman 1327f2849aa0SVlastimil Babka /* 1328ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 1329ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 1330ff9543fdSMichal Nazarewicz */ 1331edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 1332ff9543fdSMichal Nazarewicz { 1333edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 1334ff9543fdSMichal Nazarewicz struct page *page; 1335c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 1336e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 1337c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 1338c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 1339ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 1340*4fca9730SMel Gorman unsigned int stride; 13412fe86e00SMichal Nazarewicz 13425a811889SMel Gorman /* Try a small search of the free lists for a candidate */ 13435a811889SMel Gorman isolate_start_pfn = fast_isolate_freepages(cc); 13445a811889SMel Gorman if (cc->nr_freepages) 13455a811889SMel Gorman goto splitmap; 13465a811889SMel Gorman 1347ff9543fdSMichal Nazarewicz /* 1348ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 134949e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 1350e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 1351e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 1352c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 1353c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 1354c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 135549e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 135649e068f0SVlastimil Babka * is using. 1357ff9543fdSMichal Nazarewicz */ 1358e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 13595a811889SMel Gorman block_start_pfn = pageblock_start_pfn(isolate_start_pfn); 1360c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1361c96b9e50SVlastimil Babka zone_end_pfn(zone)); 136206b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 1363*4fca9730SMel Gorman stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; 13642fe86e00SMichal Nazarewicz 1365ff9543fdSMichal Nazarewicz /* 1366ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1367ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1368ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1369ff9543fdSMichal Nazarewicz */ 1370f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1371c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1372e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1373e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 1374*4fca9730SMel Gorman unsigned long nr_isolated; 1375*4fca9730SMel Gorman 1376f6ea3adbSDavid Rientjes /* 1377f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1378cb810ad2SMel Gorman * suitable migration targets, so periodically check resched. 1379f6ea3adbSDavid Rientjes */ 1380cb810ad2SMel Gorman if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) 1381cf66f070SMel Gorman cond_resched(); 1382f6ea3adbSDavid Rientjes 13837d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 13847d49d886SVlastimil Babka zone); 13857d49d886SVlastimil Babka if (!page) 1386ff9543fdSMichal Nazarewicz continue; 1387ff9543fdSMichal Nazarewicz 1388ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 13899f7e3387SVlastimil Babka if (!suitable_migration_target(cc, page)) 1390ff9543fdSMichal Nazarewicz continue; 139168e3e926SLinus Torvalds 1392bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1393bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1394bb13ffebSMel Gorman continue; 1395bb13ffebSMel Gorman 1396e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 1397*4fca9730SMel Gorman nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, 1398*4fca9730SMel Gorman block_end_pfn, freelist, stride, false); 1399ff9543fdSMichal Nazarewicz 1400d097a6f6SMel Gorman /* Update the skip hint if the full pageblock was scanned */ 1401d097a6f6SMel Gorman if (isolate_start_pfn == block_end_pfn) 1402d097a6f6SMel Gorman update_pageblock_skip(cc, page, block_start_pfn); 1403d097a6f6SMel Gorman 1404cb2dcaf0SMel Gorman /* Are enough freepages isolated? */ 1405cb2dcaf0SMel Gorman if (cc->nr_freepages >= cc->nr_migratepages) { 1406a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1407a46cbf3bSDavid Rientjes /* 1408a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1409a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1410a46cbf3bSDavid Rientjes */ 1411f5f61a32SVlastimil Babka isolate_start_pfn = 1412e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1413a46cbf3bSDavid Rientjes } 1414be976572SVlastimil Babka break; 1415a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1416f5f61a32SVlastimil Babka /* 1417a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1418a46cbf3bSDavid Rientjes * needlessly. 1419f5f61a32SVlastimil Babka */ 1420a46cbf3bSDavid Rientjes break; 1421f5f61a32SVlastimil Babka } 1422*4fca9730SMel Gorman 1423*4fca9730SMel Gorman /* Adjust stride depending on isolation */ 1424*4fca9730SMel Gorman if (nr_isolated) { 1425*4fca9730SMel Gorman stride = 1; 1426*4fca9730SMel Gorman continue; 1427*4fca9730SMel Gorman } 1428*4fca9730SMel Gorman stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); 1429c89511abSMel Gorman } 1430ff9543fdSMichal Nazarewicz 14317ed695e0SVlastimil Babka /* 1432f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1433f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1434f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1435f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 14367ed695e0SVlastimil Babka */ 1437f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 14385a811889SMel Gorman 14395a811889SMel Gorman splitmap: 14405a811889SMel Gorman /* __isolate_free_page() does not map the pages */ 14415a811889SMel Gorman split_map_pages(freelist); 1442748446bbSMel Gorman } 1443748446bbSMel Gorman 1444748446bbSMel Gorman /* 1445748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1446748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1447748446bbSMel Gorman */ 1448748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1449666feb21SMichal Hocko unsigned long data) 1450748446bbSMel Gorman { 1451748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1452748446bbSMel Gorman struct page *freepage; 1453748446bbSMel Gorman 1454748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1455edc2ca61SVlastimil Babka isolate_freepages(cc); 1456748446bbSMel Gorman 1457748446bbSMel Gorman if (list_empty(&cc->freepages)) 1458748446bbSMel Gorman return NULL; 1459748446bbSMel Gorman } 1460748446bbSMel Gorman 1461748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1462748446bbSMel Gorman list_del(&freepage->lru); 1463748446bbSMel Gorman cc->nr_freepages--; 1464748446bbSMel Gorman 1465748446bbSMel Gorman return freepage; 1466748446bbSMel Gorman } 1467748446bbSMel Gorman 1468748446bbSMel Gorman /* 1469d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1470d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1471d53aea3dSDavid Rientjes * special handling needed for NUMA. 1472d53aea3dSDavid Rientjes */ 1473d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1474d53aea3dSDavid Rientjes { 1475d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1476d53aea3dSDavid Rientjes 1477d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1478d53aea3dSDavid Rientjes cc->nr_freepages++; 1479d53aea3dSDavid Rientjes } 1480d53aea3dSDavid Rientjes 1481ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1482ff9543fdSMichal Nazarewicz typedef enum { 1483ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1484ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1485ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1486ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1487ff9543fdSMichal Nazarewicz 1488ff9543fdSMichal Nazarewicz /* 14895bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 14905bbe3547SEric B Munson * compactable pages. 14915bbe3547SEric B Munson */ 14925bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 14935bbe3547SEric B Munson 149470b44595SMel Gorman static inline void 149570b44595SMel Gorman update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) 149670b44595SMel Gorman { 149770b44595SMel Gorman if (cc->fast_start_pfn == ULONG_MAX) 149870b44595SMel Gorman return; 149970b44595SMel Gorman 150070b44595SMel Gorman if (!cc->fast_start_pfn) 150170b44595SMel Gorman cc->fast_start_pfn = pfn; 150270b44595SMel Gorman 150370b44595SMel Gorman cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); 150470b44595SMel Gorman } 150570b44595SMel Gorman 150670b44595SMel Gorman static inline unsigned long 150770b44595SMel Gorman reinit_migrate_pfn(struct compact_control *cc) 150870b44595SMel Gorman { 150970b44595SMel Gorman if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) 151070b44595SMel Gorman return cc->migrate_pfn; 151170b44595SMel Gorman 151270b44595SMel Gorman cc->migrate_pfn = cc->fast_start_pfn; 151370b44595SMel Gorman cc->fast_start_pfn = ULONG_MAX; 151470b44595SMel Gorman 151570b44595SMel Gorman return cc->migrate_pfn; 151670b44595SMel Gorman } 151770b44595SMel Gorman 151870b44595SMel Gorman /* 151970b44595SMel Gorman * Briefly search the free lists for a migration source that already has 152070b44595SMel Gorman * some free pages to reduce the number of pages that need migration 152170b44595SMel Gorman * before a pageblock is free. 152270b44595SMel Gorman */ 152370b44595SMel Gorman static unsigned long fast_find_migrateblock(struct compact_control *cc) 152470b44595SMel Gorman { 152570b44595SMel Gorman unsigned int limit = freelist_scan_limit(cc); 152670b44595SMel Gorman unsigned int nr_scanned = 0; 152770b44595SMel Gorman unsigned long distance; 152870b44595SMel Gorman unsigned long pfn = cc->migrate_pfn; 152970b44595SMel Gorman unsigned long high_pfn; 153070b44595SMel Gorman int order; 153170b44595SMel Gorman 153270b44595SMel Gorman /* Skip hints are relied on to avoid repeats on the fast search */ 153370b44595SMel Gorman if (cc->ignore_skip_hint) 153470b44595SMel Gorman return pfn; 153570b44595SMel Gorman 153670b44595SMel Gorman /* 153770b44595SMel Gorman * If the migrate_pfn is not at the start of a zone or the start 153870b44595SMel Gorman * of a pageblock then assume this is a continuation of a previous 153970b44595SMel Gorman * scan restarted due to COMPACT_CLUSTER_MAX. 154070b44595SMel Gorman */ 154170b44595SMel Gorman if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) 154270b44595SMel Gorman return pfn; 154370b44595SMel Gorman 154470b44595SMel Gorman /* 154570b44595SMel Gorman * For smaller orders, just linearly scan as the number of pages 154670b44595SMel Gorman * to migrate should be relatively small and does not necessarily 154770b44595SMel Gorman * justify freeing up a large block for a small allocation. 154870b44595SMel Gorman */ 154970b44595SMel Gorman if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) 155070b44595SMel Gorman return pfn; 155170b44595SMel Gorman 155270b44595SMel Gorman /* 155370b44595SMel Gorman * Only allow kcompactd and direct requests for movable pages to 155470b44595SMel Gorman * quickly clear out a MOVABLE pageblock for allocation. This 155570b44595SMel Gorman * reduces the risk that a large movable pageblock is freed for 155670b44595SMel Gorman * an unmovable/reclaimable small allocation. 155770b44595SMel Gorman */ 155870b44595SMel Gorman if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) 155970b44595SMel Gorman return pfn; 156070b44595SMel Gorman 156170b44595SMel Gorman /* 156270b44595SMel Gorman * When starting the migration scanner, pick any pageblock within the 156370b44595SMel Gorman * first half of the search space. Otherwise try and pick a pageblock 156470b44595SMel Gorman * within the first eighth to reduce the chances that a migration 156570b44595SMel Gorman * target later becomes a source. 156670b44595SMel Gorman */ 156770b44595SMel Gorman distance = (cc->free_pfn - cc->migrate_pfn) >> 1; 156870b44595SMel Gorman if (cc->migrate_pfn != cc->zone->zone_start_pfn) 156970b44595SMel Gorman distance >>= 2; 157070b44595SMel Gorman high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); 157170b44595SMel Gorman 157270b44595SMel Gorman for (order = cc->order - 1; 157370b44595SMel Gorman order >= PAGE_ALLOC_COSTLY_ORDER && pfn == cc->migrate_pfn && nr_scanned < limit; 157470b44595SMel Gorman order--) { 157570b44595SMel Gorman struct free_area *area = &cc->zone->free_area[order]; 157670b44595SMel Gorman struct list_head *freelist; 157770b44595SMel Gorman unsigned long flags; 157870b44595SMel Gorman struct page *freepage; 157970b44595SMel Gorman 158070b44595SMel Gorman if (!area->nr_free) 158170b44595SMel Gorman continue; 158270b44595SMel Gorman 158370b44595SMel Gorman spin_lock_irqsave(&cc->zone->lock, flags); 158470b44595SMel Gorman freelist = &area->free_list[MIGRATE_MOVABLE]; 158570b44595SMel Gorman list_for_each_entry(freepage, freelist, lru) { 158670b44595SMel Gorman unsigned long free_pfn; 158770b44595SMel Gorman 158870b44595SMel Gorman nr_scanned++; 158970b44595SMel Gorman free_pfn = page_to_pfn(freepage); 159070b44595SMel Gorman if (free_pfn < high_pfn) { 159170b44595SMel Gorman /* 159270b44595SMel Gorman * Avoid if skipped recently. Ideally it would 159370b44595SMel Gorman * move to the tail but even safe iteration of 159470b44595SMel Gorman * the list assumes an entry is deleted, not 159570b44595SMel Gorman * reordered. 159670b44595SMel Gorman */ 159770b44595SMel Gorman if (get_pageblock_skip(freepage)) { 159870b44595SMel Gorman if (list_is_last(freelist, &freepage->lru)) 159970b44595SMel Gorman break; 160070b44595SMel Gorman 160170b44595SMel Gorman continue; 160270b44595SMel Gorman } 160370b44595SMel Gorman 160470b44595SMel Gorman /* Reorder to so a future search skips recent pages */ 160570b44595SMel Gorman move_freelist_tail(freelist, freepage); 160670b44595SMel Gorman 1607e380bebeSMel Gorman update_fast_start_pfn(cc, free_pfn); 160870b44595SMel Gorman pfn = pageblock_start_pfn(free_pfn); 160970b44595SMel Gorman cc->fast_search_fail = 0; 161070b44595SMel Gorman set_pageblock_skip(freepage); 161170b44595SMel Gorman break; 161270b44595SMel Gorman } 161370b44595SMel Gorman 161470b44595SMel Gorman if (nr_scanned >= limit) { 161570b44595SMel Gorman cc->fast_search_fail++; 161670b44595SMel Gorman move_freelist_tail(freelist, freepage); 161770b44595SMel Gorman break; 161870b44595SMel Gorman } 161970b44595SMel Gorman } 162070b44595SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 162170b44595SMel Gorman } 162270b44595SMel Gorman 162370b44595SMel Gorman cc->total_migrate_scanned += nr_scanned; 162470b44595SMel Gorman 162570b44595SMel Gorman /* 162670b44595SMel Gorman * If fast scanning failed then use a cached entry for a page block 162770b44595SMel Gorman * that had free pages as the basis for starting a linear scan. 162870b44595SMel Gorman */ 162970b44595SMel Gorman if (pfn == cc->migrate_pfn) 163070b44595SMel Gorman pfn = reinit_migrate_pfn(cc); 163170b44595SMel Gorman 163270b44595SMel Gorman return pfn; 163370b44595SMel Gorman } 163470b44595SMel Gorman 16355bbe3547SEric B Munson /* 1636edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1637edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1638edc2ca61SVlastimil Babka * compact_control. 1639ff9543fdSMichal Nazarewicz */ 1640ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1641ff9543fdSMichal Nazarewicz struct compact_control *cc) 1642ff9543fdSMichal Nazarewicz { 1643e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1644e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1645e1409c32SJoonsoo Kim unsigned long low_pfn; 1646edc2ca61SVlastimil Babka struct page *page; 1647edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 16485bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 16491d2047feSHugh Dickins (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); 165070b44595SMel Gorman bool fast_find_block; 1651ff9543fdSMichal Nazarewicz 1652edc2ca61SVlastimil Babka /* 1653edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 165470b44595SMel Gorman * initialized by compact_zone(). The first failure will use 165570b44595SMel Gorman * the lowest PFN as the starting point for linear scanning. 1656edc2ca61SVlastimil Babka */ 165770b44595SMel Gorman low_pfn = fast_find_migrateblock(cc); 165806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1659e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1660e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1661ff9543fdSMichal Nazarewicz 166270b44595SMel Gorman /* 166370b44595SMel Gorman * fast_find_migrateblock marks a pageblock skipped so to avoid 166470b44595SMel Gorman * the isolation_suitable check below, check whether the fast 166570b44595SMel Gorman * search was successful. 166670b44595SMel Gorman */ 166770b44595SMel Gorman fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; 166870b44595SMel Gorman 1669ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 167006b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1671ff9543fdSMichal Nazarewicz 1672edc2ca61SVlastimil Babka /* 1673edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1674edc2ca61SVlastimil Babka * Do not cross the free scanner. 1675edc2ca61SVlastimil Babka */ 1676e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 167770b44595SMel Gorman fast_find_block = false, 1678e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1679e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1680e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1681edc2ca61SVlastimil Babka 1682edc2ca61SVlastimil Babka /* 1683edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1684edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1685cb810ad2SMel Gorman * need to schedule. 1686edc2ca61SVlastimil Babka */ 1687cb810ad2SMel Gorman if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) 1688cf66f070SMel Gorman cond_resched(); 1689edc2ca61SVlastimil Babka 1690e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1691e1409c32SJoonsoo Kim zone); 16927d49d886SVlastimil Babka if (!page) 1693edc2ca61SVlastimil Babka continue; 1694edc2ca61SVlastimil Babka 1695e380bebeSMel Gorman /* 1696e380bebeSMel Gorman * If isolation recently failed, do not retry. Only check the 1697e380bebeSMel Gorman * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock 1698e380bebeSMel Gorman * to be visited multiple times. Assume skip was checked 1699e380bebeSMel Gorman * before making it "skip" so other compaction instances do 1700e380bebeSMel Gorman * not scan the same block. 1701e380bebeSMel Gorman */ 1702e380bebeSMel Gorman if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && 1703e380bebeSMel Gorman !fast_find_block && !isolation_suitable(cc, page)) 1704edc2ca61SVlastimil Babka continue; 1705edc2ca61SVlastimil Babka 1706edc2ca61SVlastimil Babka /* 17079bebefd5SMel Gorman * For async compaction, also only scan in MOVABLE blocks 17089bebefd5SMel Gorman * without huge pages. Async compaction is optimistic to see 17099bebefd5SMel Gorman * if the minimum amount of work satisfies the allocation. 17109bebefd5SMel Gorman * The cached PFN is updated as it's possible that all 17119bebefd5SMel Gorman * remaining blocks between source and target are unsuitable 17129bebefd5SMel Gorman * and the compaction scanners fail to meet. 1713edc2ca61SVlastimil Babka */ 17149bebefd5SMel Gorman if (!suitable_migration_source(cc, page)) { 17159bebefd5SMel Gorman update_cached_migrate(cc, block_end_pfn); 1716edc2ca61SVlastimil Babka continue; 17179bebefd5SMel Gorman } 1718ff9543fdSMichal Nazarewicz 1719ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1720e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1721e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1722edc2ca61SVlastimil Babka 1723cb2dcaf0SMel Gorman if (!low_pfn) 1724ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1725ff9543fdSMichal Nazarewicz 1726edc2ca61SVlastimil Babka /* 1727edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1728edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1729edc2ca61SVlastimil Babka * continue or not. 1730edc2ca61SVlastimil Babka */ 1731edc2ca61SVlastimil Babka break; 1732edc2ca61SVlastimil Babka } 1733edc2ca61SVlastimil Babka 1734f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1735f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1736ff9543fdSMichal Nazarewicz 1737edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1738ff9543fdSMichal Nazarewicz } 1739ff9543fdSMichal Nazarewicz 174021c527a3SYaowei Bai /* 174121c527a3SYaowei Bai * order == -1 is expected when compacting via 174221c527a3SYaowei Bai * /proc/sys/vm/compact_memory 174321c527a3SYaowei Bai */ 174421c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 174521c527a3SYaowei Bai { 174621c527a3SYaowei Bai return order == -1; 174721c527a3SYaowei Bai } 174821c527a3SYaowei Bai 174940cacbcbSMel Gorman static enum compact_result __compact_finished(struct compact_control *cc) 1750748446bbSMel Gorman { 17518fb74b9fSMel Gorman unsigned int order; 1752d39773a0SVlastimil Babka const int migratetype = cc->migratetype; 1753cb2dcaf0SMel Gorman int ret; 1754748446bbSMel Gorman 1755753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1756f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 175755b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 175840cacbcbSMel Gorman reset_cached_positions(cc->zone); 175955b7c4c9SVlastimil Babka 176062997027SMel Gorman /* 176162997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1762accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 176362997027SMel Gorman * flag itself as the decision to be clear should be directly 176462997027SMel Gorman * based on an allocation request. 176562997027SMel Gorman */ 1766accf6242SVlastimil Babka if (cc->direct_compaction) 176740cacbcbSMel Gorman cc->zone->compact_blockskip_flush = true; 176862997027SMel Gorman 1769c8f7de0bSMichal Hocko if (cc->whole_zone) 1770748446bbSMel Gorman return COMPACT_COMPLETE; 1771c8f7de0bSMichal Hocko else 1772c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 1773bb13ffebSMel Gorman } 1774748446bbSMel Gorman 177521c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 177656de7263SMel Gorman return COMPACT_CONTINUE; 177756de7263SMel Gorman 1778baf6a9a1SVlastimil Babka /* 1779efe771c7SMel Gorman * Always finish scanning a pageblock to reduce the possibility of 1780efe771c7SMel Gorman * fallbacks in the future. This is particularly important when 1781efe771c7SMel Gorman * migration source is unmovable/reclaimable but it's not worth 1782efe771c7SMel Gorman * special casing. 1783baf6a9a1SVlastimil Babka */ 1784efe771c7SMel Gorman if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) 1785baf6a9a1SVlastimil Babka return COMPACT_CONTINUE; 1786baf6a9a1SVlastimil Babka 178756de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 1788cb2dcaf0SMel Gorman ret = COMPACT_NO_SUITABLE_PAGE; 178956de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 179040cacbcbSMel Gorman struct free_area *area = &cc->zone->free_area[order]; 17912149cdaeSJoonsoo Kim bool can_steal; 17928fb74b9fSMel Gorman 179356de7263SMel Gorman /* Job done if page is free of the right migratetype */ 17946d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 1795cf378319SVlastimil Babka return COMPACT_SUCCESS; 179656de7263SMel Gorman 17972149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 17982149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 17992149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 18002149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 1801cf378319SVlastimil Babka return COMPACT_SUCCESS; 18022149cdaeSJoonsoo Kim #endif 18032149cdaeSJoonsoo Kim /* 18042149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 18052149cdaeSJoonsoo Kim * other migratetype buddy lists. 18062149cdaeSJoonsoo Kim */ 18072149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 1808baf6a9a1SVlastimil Babka true, &can_steal) != -1) { 1809baf6a9a1SVlastimil Babka 1810baf6a9a1SVlastimil Babka /* movable pages are OK in any pageblock */ 1811baf6a9a1SVlastimil Babka if (migratetype == MIGRATE_MOVABLE) 1812cf378319SVlastimil Babka return COMPACT_SUCCESS; 1813baf6a9a1SVlastimil Babka 1814baf6a9a1SVlastimil Babka /* 1815baf6a9a1SVlastimil Babka * We are stealing for a non-movable allocation. Make 1816baf6a9a1SVlastimil Babka * sure we finish compacting the current pageblock 1817baf6a9a1SVlastimil Babka * first so it is as free as possible and we won't 1818baf6a9a1SVlastimil Babka * have to steal another one soon. This only applies 1819baf6a9a1SVlastimil Babka * to sync compaction, as async compaction operates 1820baf6a9a1SVlastimil Babka * on pageblocks of the same migratetype. 1821baf6a9a1SVlastimil Babka */ 1822baf6a9a1SVlastimil Babka if (cc->mode == MIGRATE_ASYNC || 1823baf6a9a1SVlastimil Babka IS_ALIGNED(cc->migrate_pfn, 1824baf6a9a1SVlastimil Babka pageblock_nr_pages)) { 1825baf6a9a1SVlastimil Babka return COMPACT_SUCCESS; 1826baf6a9a1SVlastimil Babka } 1827baf6a9a1SVlastimil Babka 1828cb2dcaf0SMel Gorman ret = COMPACT_CONTINUE; 1829cb2dcaf0SMel Gorman break; 1830baf6a9a1SVlastimil Babka } 183156de7263SMel Gorman } 183256de7263SMel Gorman 1833cb2dcaf0SMel Gorman if (cc->contended || fatal_signal_pending(current)) 1834cb2dcaf0SMel Gorman ret = COMPACT_CONTENDED; 1835cb2dcaf0SMel Gorman 1836cb2dcaf0SMel Gorman return ret; 1837837d026dSJoonsoo Kim } 1838837d026dSJoonsoo Kim 183940cacbcbSMel Gorman static enum compact_result compact_finished(struct compact_control *cc) 1840837d026dSJoonsoo Kim { 1841837d026dSJoonsoo Kim int ret; 1842837d026dSJoonsoo Kim 184340cacbcbSMel Gorman ret = __compact_finished(cc); 184440cacbcbSMel Gorman trace_mm_compaction_finished(cc->zone, cc->order, ret); 1845837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1846837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1847837d026dSJoonsoo Kim 1848837d026dSJoonsoo Kim return ret; 1849748446bbSMel Gorman } 1850748446bbSMel Gorman 18513e7d3449SMel Gorman /* 18523e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 18533e7d3449SMel Gorman * Returns 18543e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 1855cf378319SVlastimil Babka * COMPACT_SUCCESS - If the allocation would succeed without compaction 18563e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 18573e7d3449SMel Gorman */ 1858ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 1859c603844bSMel Gorman unsigned int alloc_flags, 186086a294a8SMichal Hocko int classzone_idx, 186186a294a8SMichal Hocko unsigned long wmark_target) 18623e7d3449SMel Gorman { 18633e7d3449SMel Gorman unsigned long watermark; 18643e7d3449SMel Gorman 186521c527a3SYaowei Bai if (is_via_compact_memory(order)) 18663957c776SMichal Hocko return COMPACT_CONTINUE; 18673957c776SMichal Hocko 1868a9214443SMel Gorman watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); 1869ebff3980SVlastimil Babka /* 1870ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1871ebff3980SVlastimil Babka * should be no need for compaction at all. 1872ebff3980SVlastimil Babka */ 1873ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1874ebff3980SVlastimil Babka alloc_flags)) 1875cf378319SVlastimil Babka return COMPACT_SUCCESS; 1876ebff3980SVlastimil Babka 18773957c776SMichal Hocko /* 18789861a62cSVlastimil Babka * Watermarks for order-0 must be met for compaction to be able to 1879984fdba6SVlastimil Babka * isolate free pages for migration targets. This means that the 1880984fdba6SVlastimil Babka * watermark and alloc_flags have to match, or be more pessimistic than 1881984fdba6SVlastimil Babka * the check in __isolate_free_page(). We don't use the direct 1882984fdba6SVlastimil Babka * compactor's alloc_flags, as they are not relevant for freepage 1883984fdba6SVlastimil Babka * isolation. We however do use the direct compactor's classzone_idx to 1884984fdba6SVlastimil Babka * skip over zones where lowmem reserves would prevent allocation even 1885984fdba6SVlastimil Babka * if compaction succeeds. 18868348faf9SVlastimil Babka * For costly orders, we require low watermark instead of min for 18878348faf9SVlastimil Babka * compaction to proceed to increase its chances. 1888d883c6cfSJoonsoo Kim * ALLOC_CMA is used, as pages in CMA pageblocks are considered 1889d883c6cfSJoonsoo Kim * suitable migration targets 18903e7d3449SMel Gorman */ 18918348faf9SVlastimil Babka watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? 18928348faf9SVlastimil Babka low_wmark_pages(zone) : min_wmark_pages(zone); 18938348faf9SVlastimil Babka watermark += compact_gap(order); 189486a294a8SMichal Hocko if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, 1895d883c6cfSJoonsoo Kim ALLOC_CMA, wmark_target)) 18963e7d3449SMel Gorman return COMPACT_SKIPPED; 18973e7d3449SMel Gorman 1898cc5c9f09SVlastimil Babka return COMPACT_CONTINUE; 1899cc5c9f09SVlastimil Babka } 1900cc5c9f09SVlastimil Babka 1901cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order, 1902cc5c9f09SVlastimil Babka unsigned int alloc_flags, 1903cc5c9f09SVlastimil Babka int classzone_idx) 1904cc5c9f09SVlastimil Babka { 1905cc5c9f09SVlastimil Babka enum compact_result ret; 1906cc5c9f09SVlastimil Babka int fragindex; 1907cc5c9f09SVlastimil Babka 1908cc5c9f09SVlastimil Babka ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, 1909cc5c9f09SVlastimil Babka zone_page_state(zone, NR_FREE_PAGES)); 19103e7d3449SMel Gorman /* 19113e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 19123e7d3449SMel Gorman * low memory or external fragmentation 19133e7d3449SMel Gorman * 1914ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1915ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 19163e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 19173e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 19183e7d3449SMel Gorman * 191920311420SVlastimil Babka * Only compact if a failure would be due to fragmentation. Also 192020311420SVlastimil Babka * ignore fragindex for non-costly orders where the alternative to 192120311420SVlastimil Babka * a successful reclaim/compaction is OOM. Fragindex and the 192220311420SVlastimil Babka * vm.extfrag_threshold sysctl is meant as a heuristic to prevent 192320311420SVlastimil Babka * excessive compaction for costly orders, but it should not be at the 192420311420SVlastimil Babka * expense of system stability. 19253e7d3449SMel Gorman */ 192620311420SVlastimil Babka if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { 19273e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 19283e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1929cc5c9f09SVlastimil Babka ret = COMPACT_NOT_SUITABLE_ZONE; 19303e7d3449SMel Gorman } 19313e7d3449SMel Gorman 1932837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1933837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1934837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1935837d026dSJoonsoo Kim 1936837d026dSJoonsoo Kim return ret; 1937837d026dSJoonsoo Kim } 1938837d026dSJoonsoo Kim 193986a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 194086a294a8SMichal Hocko int alloc_flags) 194186a294a8SMichal Hocko { 194286a294a8SMichal Hocko struct zone *zone; 194386a294a8SMichal Hocko struct zoneref *z; 194486a294a8SMichal Hocko 194586a294a8SMichal Hocko /* 194686a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 194786a294a8SMichal Hocko * retrying the reclaim. 194886a294a8SMichal Hocko */ 194986a294a8SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 195086a294a8SMichal Hocko ac->nodemask) { 195186a294a8SMichal Hocko unsigned long available; 195286a294a8SMichal Hocko enum compact_result compact_result; 195386a294a8SMichal Hocko 195486a294a8SMichal Hocko /* 195586a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 195686a294a8SMichal Hocko * want to trash just for a single high order allocation which 195786a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 195886a294a8SMichal Hocko * is happy about the watermark check. 195986a294a8SMichal Hocko */ 19605a1c84b4SMel Gorman available = zone_reclaimable_pages(zone) / order; 196186a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 196286a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 196386a294a8SMichal Hocko ac_classzone_idx(ac), available); 1964cc5c9f09SVlastimil Babka if (compact_result != COMPACT_SKIPPED) 196586a294a8SMichal Hocko return true; 196686a294a8SMichal Hocko } 196786a294a8SMichal Hocko 196886a294a8SMichal Hocko return false; 196986a294a8SMichal Hocko } 197086a294a8SMichal Hocko 197140cacbcbSMel Gorman static enum compact_result compact_zone(struct compact_control *cc) 1972748446bbSMel Gorman { 1973ea7ab982SMichal Hocko enum compact_result ret; 197440cacbcbSMel Gorman unsigned long start_pfn = cc->zone->zone_start_pfn; 197540cacbcbSMel Gorman unsigned long end_pfn = zone_end_pfn(cc->zone); 1976566e54e1SMel Gorman unsigned long last_migrated_pfn; 1977e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 19788854c55fSMel Gorman bool update_cached; 1979748446bbSMel Gorman 1980d39773a0SVlastimil Babka cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask); 198140cacbcbSMel Gorman ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, 1982ebff3980SVlastimil Babka cc->classzone_idx); 19833e7d3449SMel Gorman /* Compaction is likely to fail */ 1984cf378319SVlastimil Babka if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) 19853e7d3449SMel Gorman return ret; 1986c46649deSMichal Hocko 1987c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 1988c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 19893e7d3449SMel Gorman 1990c89511abSMel Gorman /* 1991d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1992accf6242SVlastimil Babka * is about to be retried after being deferred. 1993d3132e4bSVlastimil Babka */ 199440cacbcbSMel Gorman if (compaction_restarting(cc->zone, cc->order)) 199540cacbcbSMel Gorman __reset_isolation_suitable(cc->zone); 1996d3132e4bSVlastimil Babka 1997d3132e4bSVlastimil Babka /* 1998c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 199906ed2998SVlastimil Babka * information on where the scanners should start (unless we explicitly 200006ed2998SVlastimil Babka * want to compact the whole zone), but check that it is initialised 200106ed2998SVlastimil Babka * by ensuring the values are within zone boundaries. 2002c89511abSMel Gorman */ 200370b44595SMel Gorman cc->fast_start_pfn = 0; 200406ed2998SVlastimil Babka if (cc->whole_zone) { 200506ed2998SVlastimil Babka cc->migrate_pfn = start_pfn; 200606ed2998SVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 200706ed2998SVlastimil Babka } else { 200840cacbcbSMel Gorman cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; 200940cacbcbSMel Gorman cc->free_pfn = cc->zone->compact_cached_free_pfn; 2010623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 201106b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 201240cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = cc->free_pfn; 2013c89511abSMel Gorman } 2014623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 2015c89511abSMel Gorman cc->migrate_pfn = start_pfn; 201640cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 201740cacbcbSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 2018c89511abSMel Gorman } 2019c8f7de0bSMichal Hocko 2020c8f7de0bSMichal Hocko if (cc->migrate_pfn == start_pfn) 2021c8f7de0bSMichal Hocko cc->whole_zone = true; 202206ed2998SVlastimil Babka } 2023c8f7de0bSMichal Hocko 2024566e54e1SMel Gorman last_migrated_pfn = 0; 2025748446bbSMel Gorman 20268854c55fSMel Gorman /* 20278854c55fSMel Gorman * Migrate has separate cached PFNs for ASYNC and SYNC* migration on 20288854c55fSMel Gorman * the basis that some migrations will fail in ASYNC mode. However, 20298854c55fSMel Gorman * if the cached PFNs match and pageblocks are skipped due to having 20308854c55fSMel Gorman * no isolation candidates, then the sync state does not matter. 20318854c55fSMel Gorman * Until a pageblock with isolation candidates is found, keep the 20328854c55fSMel Gorman * cached PFNs in sync to avoid revisiting the same blocks. 20338854c55fSMel Gorman */ 20348854c55fSMel Gorman update_cached = !sync && 20358854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; 20368854c55fSMel Gorman 203716c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 203816c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 20390eb927c0SMel Gorman 2040748446bbSMel Gorman migrate_prep_local(); 2041748446bbSMel Gorman 204240cacbcbSMel Gorman while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { 20439d502c1cSMinchan Kim int err; 2044566e54e1SMel Gorman unsigned long start_pfn = cc->migrate_pfn; 2045748446bbSMel Gorman 2046804d3121SMel Gorman /* 2047804d3121SMel Gorman * Avoid multiple rescans which can happen if a page cannot be 2048804d3121SMel Gorman * isolated (dirty/writeback in async mode) or if the migrated 2049804d3121SMel Gorman * pages are being allocated before the pageblock is cleared. 2050804d3121SMel Gorman * The first rescan will capture the entire pageblock for 2051804d3121SMel Gorman * migration. If it fails, it'll be marked skip and scanning 2052804d3121SMel Gorman * will proceed as normal. 2053804d3121SMel Gorman */ 2054804d3121SMel Gorman cc->rescan = false; 2055804d3121SMel Gorman if (pageblock_start_pfn(last_migrated_pfn) == 2056804d3121SMel Gorman pageblock_start_pfn(start_pfn)) { 2057804d3121SMel Gorman cc->rescan = true; 2058804d3121SMel Gorman } 2059804d3121SMel Gorman 206040cacbcbSMel Gorman switch (isolate_migratepages(cc->zone, cc)) { 2061f9e35b3bSMel Gorman case ISOLATE_ABORT: 20622d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 20635733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 2064e64c5237SShaohua Li cc->nr_migratepages = 0; 2065566e54e1SMel Gorman last_migrated_pfn = 0; 2066f9e35b3bSMel Gorman goto out; 2067f9e35b3bSMel Gorman case ISOLATE_NONE: 20688854c55fSMel Gorman if (update_cached) { 20698854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[1] = 20708854c55fSMel Gorman cc->zone->compact_cached_migrate_pfn[0]; 20718854c55fSMel Gorman } 20728854c55fSMel Gorman 2073fdaf7f5cSVlastimil Babka /* 2074fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 2075fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 2076fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 2077fdaf7f5cSVlastimil Babka */ 2078fdaf7f5cSVlastimil Babka goto check_drain; 2079f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 20808854c55fSMel Gorman update_cached = false; 2081566e54e1SMel Gorman last_migrated_pfn = start_pfn; 2082f9e35b3bSMel Gorman ; 2083f9e35b3bSMel Gorman } 2084748446bbSMel Gorman 2085d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 2086e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 20877b2a2d4aSMel Gorman MR_COMPACTION); 2088748446bbSMel Gorman 2089f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 2090f8c9301fSVlastimil Babka &cc->migratepages); 2091748446bbSMel Gorman 2092f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 2093f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 20949d502c1cSMinchan Kim if (err) { 20955733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 20967ed695e0SVlastimil Babka /* 20977ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 20987ed695e0SVlastimil Babka * and we want compact_finished() to detect it 20997ed695e0SVlastimil Babka */ 2100f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 21012d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 21024bf2bba3SDavid Rientjes goto out; 2103748446bbSMel Gorman } 2104fdd048e1SVlastimil Babka /* 2105fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 2106fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 2107fdd048e1SVlastimil Babka */ 2108fdd048e1SVlastimil Babka if (cc->direct_compaction && 2109fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 2110fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 2111fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 2112fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 2113566e54e1SMel Gorman last_migrated_pfn = 0; 2114fdd048e1SVlastimil Babka } 21154bf2bba3SDavid Rientjes } 2116fdaf7f5cSVlastimil Babka 2117fdaf7f5cSVlastimil Babka check_drain: 2118fdaf7f5cSVlastimil Babka /* 2119fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 2120fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 2121fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 2122fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 2123fdaf7f5cSVlastimil Babka * would succeed. 2124fdaf7f5cSVlastimil Babka */ 2125566e54e1SMel Gorman if (cc->order > 0 && last_migrated_pfn) { 2126fdaf7f5cSVlastimil Babka int cpu; 2127fdaf7f5cSVlastimil Babka unsigned long current_block_start = 212806b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 2129fdaf7f5cSVlastimil Babka 2130566e54e1SMel Gorman if (last_migrated_pfn < current_block_start) { 2131fdaf7f5cSVlastimil Babka cpu = get_cpu(); 2132fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 213340cacbcbSMel Gorman drain_local_pages(cc->zone); 2134fdaf7f5cSVlastimil Babka put_cpu(); 2135fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 2136566e54e1SMel Gorman last_migrated_pfn = 0; 2137fdaf7f5cSVlastimil Babka } 2138fdaf7f5cSVlastimil Babka } 2139fdaf7f5cSVlastimil Babka 2140748446bbSMel Gorman } 2141748446bbSMel Gorman 2142f9e35b3bSMel Gorman out: 21436bace090SVlastimil Babka /* 21446bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 21456bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 21466bace090SVlastimil Babka */ 21476bace090SVlastimil Babka if (cc->nr_freepages > 0) { 21486bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 21496bace090SVlastimil Babka 21506bace090SVlastimil Babka cc->nr_freepages = 0; 21516bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 21526bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 215306b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 21546bace090SVlastimil Babka /* 21556bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 21566bace090SVlastimil Babka * already reset to zone end in compact_finished() 21576bace090SVlastimil Babka */ 215840cacbcbSMel Gorman if (free_pfn > cc->zone->compact_cached_free_pfn) 215940cacbcbSMel Gorman cc->zone->compact_cached_free_pfn = free_pfn; 21606bace090SVlastimil Babka } 2161748446bbSMel Gorman 21627f354a54SDavid Rientjes count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); 21637f354a54SDavid Rientjes count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); 21647f354a54SDavid Rientjes 216516c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 216616c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 21670eb927c0SMel Gorman 2168748446bbSMel Gorman return ret; 2169748446bbSMel Gorman } 217076ab0f53SMel Gorman 2171ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 2172c3486f53SVlastimil Babka gfp_t gfp_mask, enum compact_priority prio, 2173c603844bSMel Gorman unsigned int alloc_flags, int classzone_idx) 217456de7263SMel Gorman { 2175ea7ab982SMichal Hocko enum compact_result ret; 217656de7263SMel Gorman struct compact_control cc = { 217756de7263SMel Gorman .nr_freepages = 0, 217856de7263SMel Gorman .nr_migratepages = 0, 21797f354a54SDavid Rientjes .total_migrate_scanned = 0, 21807f354a54SDavid Rientjes .total_free_scanned = 0, 218156de7263SMel Gorman .order = order, 2182dbe2d4e4SMel Gorman .search_order = order, 21836d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 218456de7263SMel Gorman .zone = zone, 2185a5508cd8SVlastimil Babka .mode = (prio == COMPACT_PRIO_ASYNC) ? 2186a5508cd8SVlastimil Babka MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, 2187ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 2188ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 2189accf6242SVlastimil Babka .direct_compaction = true, 2190a8e025e5SVlastimil Babka .whole_zone = (prio == MIN_COMPACT_PRIORITY), 21919f7e3387SVlastimil Babka .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), 21929f7e3387SVlastimil Babka .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) 219356de7263SMel Gorman }; 219456de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 219556de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 219656de7263SMel Gorman 219740cacbcbSMel Gorman ret = compact_zone(&cc); 2198e64c5237SShaohua Li 2199e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 2200e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 2201e64c5237SShaohua Li 2202e64c5237SShaohua Li return ret; 220356de7263SMel Gorman } 220456de7263SMel Gorman 22055e771905SMel Gorman int sysctl_extfrag_threshold = 500; 22065e771905SMel Gorman 220756de7263SMel Gorman /** 220856de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 220956de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 22101a6d53a1SVlastimil Babka * @order: The order of the current allocation 22111a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 22121a6d53a1SVlastimil Babka * @ac: The context of current allocation 2213112d2d29SYang Shi * @prio: Determines how hard direct compaction should try to succeed 221456de7263SMel Gorman * 221556de7263SMel Gorman * This is the main entry point for direct page compaction. 221656de7263SMel Gorman */ 2217ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 2218c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 2219c3486f53SVlastimil Babka enum compact_priority prio) 222056de7263SMel Gorman { 222156de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 222256de7263SMel Gorman struct zoneref *z; 222356de7263SMel Gorman struct zone *zone; 22241d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 222556de7263SMel Gorman 222673e64c51SMichal Hocko /* 222773e64c51SMichal Hocko * Check if the GFP flags allow compaction - GFP_NOIO is really 222873e64c51SMichal Hocko * tricky context because the migration might require IO 222973e64c51SMichal Hocko */ 223073e64c51SMichal Hocko if (!may_perform_io) 223153853e2dSVlastimil Babka return COMPACT_SKIPPED; 223256de7263SMel Gorman 2233a5508cd8SVlastimil Babka trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); 2234837d026dSJoonsoo Kim 223556de7263SMel Gorman /* Compact each zone in the list */ 22361a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 22371a6d53a1SVlastimil Babka ac->nodemask) { 2238ea7ab982SMichal Hocko enum compact_result status; 223956de7263SMel Gorman 2240a8e025e5SVlastimil Babka if (prio > MIN_COMPACT_PRIORITY 2241a8e025e5SVlastimil Babka && compaction_deferred(zone, order)) { 22421d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 224353853e2dSVlastimil Babka continue; 22441d4746d3SMichal Hocko } 224553853e2dSVlastimil Babka 2246a5508cd8SVlastimil Babka status = compact_zone_order(zone, order, gfp_mask, prio, 2247c3486f53SVlastimil Babka alloc_flags, ac_classzone_idx(ac)); 224856de7263SMel Gorman rc = max(status, rc); 224956de7263SMel Gorman 22507ceb009aSVlastimil Babka /* The allocation should succeed, stop compacting */ 22517ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 225253853e2dSVlastimil Babka /* 225353853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 225453853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 225553853e2dSVlastimil Babka * will repeat this with true if allocation indeed 225653853e2dSVlastimil Babka * succeeds in this zone. 225753853e2dSVlastimil Babka */ 225853853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 22591f9efdefSVlastimil Babka 2260c3486f53SVlastimil Babka break; 22611f9efdefSVlastimil Babka } 22621f9efdefSVlastimil Babka 2263a5508cd8SVlastimil Babka if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || 2264c3486f53SVlastimil Babka status == COMPACT_PARTIAL_SKIPPED)) 226553853e2dSVlastimil Babka /* 226653853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 226753853e2dSVlastimil Babka * so we defer compaction there. If it ends up 226853853e2dSVlastimil Babka * succeeding after all, it will be reset. 226953853e2dSVlastimil Babka */ 227053853e2dSVlastimil Babka defer_compaction(zone, order); 22711f9efdefSVlastimil Babka 22721f9efdefSVlastimil Babka /* 22731f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 22741f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 2275c3486f53SVlastimil Babka * case do not try further zones 22761f9efdefSVlastimil Babka */ 2277c3486f53SVlastimil Babka if ((prio == COMPACT_PRIO_ASYNC && need_resched()) 2278c3486f53SVlastimil Babka || fatal_signal_pending(current)) 22791f9efdefSVlastimil Babka break; 22801f9efdefSVlastimil Babka } 22811f9efdefSVlastimil Babka 228256de7263SMel Gorman return rc; 228356de7263SMel Gorman } 228456de7263SMel Gorman 228556de7263SMel Gorman 228676ab0f53SMel Gorman /* Compact all zones within a node */ 22877103f16dSAndrew Morton static void compact_node(int nid) 22887be62de9SRik van Riel { 2289791cae96SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2290791cae96SVlastimil Babka int zoneid; 2291791cae96SVlastimil Babka struct zone *zone; 22927be62de9SRik van Riel struct compact_control cc = { 22937be62de9SRik van Riel .order = -1, 22947f354a54SDavid Rientjes .total_migrate_scanned = 0, 22957f354a54SDavid Rientjes .total_free_scanned = 0, 2296e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 229791ca9186SDavid Rientjes .ignore_skip_hint = true, 229806ed2998SVlastimil Babka .whole_zone = true, 229973e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 23007be62de9SRik van Riel }; 23017be62de9SRik van Riel 2302791cae96SVlastimil Babka 2303791cae96SVlastimil Babka for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 2304791cae96SVlastimil Babka 2305791cae96SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2306791cae96SVlastimil Babka if (!populated_zone(zone)) 2307791cae96SVlastimil Babka continue; 2308791cae96SVlastimil Babka 2309791cae96SVlastimil Babka cc.nr_freepages = 0; 2310791cae96SVlastimil Babka cc.nr_migratepages = 0; 2311791cae96SVlastimil Babka cc.zone = zone; 2312791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 2313791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 2314791cae96SVlastimil Babka 231540cacbcbSMel Gorman compact_zone(&cc); 2316791cae96SVlastimil Babka 2317791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2318791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2319791cae96SVlastimil Babka } 23207be62de9SRik van Riel } 23217be62de9SRik van Riel 232276ab0f53SMel Gorman /* Compact all nodes in the system */ 23237964c06dSJason Liu static void compact_nodes(void) 232476ab0f53SMel Gorman { 232576ab0f53SMel Gorman int nid; 232676ab0f53SMel Gorman 23278575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 23288575ec29SHugh Dickins lru_add_drain_all(); 23298575ec29SHugh Dickins 233076ab0f53SMel Gorman for_each_online_node(nid) 233176ab0f53SMel Gorman compact_node(nid); 233276ab0f53SMel Gorman } 233376ab0f53SMel Gorman 233476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 233576ab0f53SMel Gorman int sysctl_compact_memory; 233676ab0f53SMel Gorman 2337fec4eb2cSYaowei Bai /* 2338fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 2339fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 2340fec4eb2cSYaowei Bai */ 234176ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 234276ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 234376ab0f53SMel Gorman { 234476ab0f53SMel Gorman if (write) 23457964c06dSJason Liu compact_nodes(); 234676ab0f53SMel Gorman 234776ab0f53SMel Gorman return 0; 234876ab0f53SMel Gorman } 2349ed4a6d7fSMel Gorman 2350ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 235174e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 235210fbcf4cSKay Sievers struct device_attribute *attr, 2353ed4a6d7fSMel Gorman const char *buf, size_t count) 2354ed4a6d7fSMel Gorman { 23558575ec29SHugh Dickins int nid = dev->id; 23568575ec29SHugh Dickins 23578575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 23588575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 23598575ec29SHugh Dickins lru_add_drain_all(); 23608575ec29SHugh Dickins 23618575ec29SHugh Dickins compact_node(nid); 23628575ec29SHugh Dickins } 2363ed4a6d7fSMel Gorman 2364ed4a6d7fSMel Gorman return count; 2365ed4a6d7fSMel Gorman } 23660825a6f9SJoe Perches static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node); 2367ed4a6d7fSMel Gorman 2368ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 2369ed4a6d7fSMel Gorman { 237010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 2371ed4a6d7fSMel Gorman } 2372ed4a6d7fSMel Gorman 2373ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 2374ed4a6d7fSMel Gorman { 237510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 2376ed4a6d7fSMel Gorman } 2377ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 2378ff9543fdSMichal Nazarewicz 2379698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 2380698b1b30SVlastimil Babka { 2381172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 2382698b1b30SVlastimil Babka } 2383698b1b30SVlastimil Babka 2384698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 2385698b1b30SVlastimil Babka { 2386698b1b30SVlastimil Babka int zoneid; 2387698b1b30SVlastimil Babka struct zone *zone; 2388698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 2389698b1b30SVlastimil Babka 23906cd9dc3eSChen Feng for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { 2391698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2392698b1b30SVlastimil Babka 2393698b1b30SVlastimil Babka if (!populated_zone(zone)) 2394698b1b30SVlastimil Babka continue; 2395698b1b30SVlastimil Babka 2396698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 2397698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 2398698b1b30SVlastimil Babka return true; 2399698b1b30SVlastimil Babka } 2400698b1b30SVlastimil Babka 2401698b1b30SVlastimil Babka return false; 2402698b1b30SVlastimil Babka } 2403698b1b30SVlastimil Babka 2404698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 2405698b1b30SVlastimil Babka { 2406698b1b30SVlastimil Babka /* 2407698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 2408698b1b30SVlastimil Babka * order is allocatable. 2409698b1b30SVlastimil Babka */ 2410698b1b30SVlastimil Babka int zoneid; 2411698b1b30SVlastimil Babka struct zone *zone; 2412698b1b30SVlastimil Babka struct compact_control cc = { 2413698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 2414dbe2d4e4SMel Gorman .search_order = pgdat->kcompactd_max_order, 24157f354a54SDavid Rientjes .total_migrate_scanned = 0, 24167f354a54SDavid Rientjes .total_free_scanned = 0, 2417698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 2418698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 2419a0647dc9SDavid Rientjes .ignore_skip_hint = false, 242073e64c51SMichal Hocko .gfp_mask = GFP_KERNEL, 2421698b1b30SVlastimil Babka }; 2422698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 2423698b1b30SVlastimil Babka cc.classzone_idx); 24247f354a54SDavid Rientjes count_compact_event(KCOMPACTD_WAKE); 2425698b1b30SVlastimil Babka 24266cd9dc3eSChen Feng for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { 2427698b1b30SVlastimil Babka int status; 2428698b1b30SVlastimil Babka 2429698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 2430698b1b30SVlastimil Babka if (!populated_zone(zone)) 2431698b1b30SVlastimil Babka continue; 2432698b1b30SVlastimil Babka 2433698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 2434698b1b30SVlastimil Babka continue; 2435698b1b30SVlastimil Babka 2436698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 2437698b1b30SVlastimil Babka COMPACT_CONTINUE) 2438698b1b30SVlastimil Babka continue; 2439698b1b30SVlastimil Babka 2440698b1b30SVlastimil Babka cc.nr_freepages = 0; 2441698b1b30SVlastimil Babka cc.nr_migratepages = 0; 24427f354a54SDavid Rientjes cc.total_migrate_scanned = 0; 24437f354a54SDavid Rientjes cc.total_free_scanned = 0; 2444698b1b30SVlastimil Babka cc.zone = zone; 2445698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 2446698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 2447698b1b30SVlastimil Babka 2448172400c6SVlastimil Babka if (kthread_should_stop()) 2449172400c6SVlastimil Babka return; 245040cacbcbSMel Gorman status = compact_zone(&cc); 2451698b1b30SVlastimil Babka 24527ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 2453698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 2454c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 2455698b1b30SVlastimil Babka /* 2456bc3106b2SDavid Rientjes * Buddy pages may become stranded on pcps that could 2457bc3106b2SDavid Rientjes * otherwise coalesce on the zone's free area for 2458bc3106b2SDavid Rientjes * order >= cc.order. This is ratelimited by the 2459bc3106b2SDavid Rientjes * upcoming deferral. 2460bc3106b2SDavid Rientjes */ 2461bc3106b2SDavid Rientjes drain_all_pages(zone); 2462bc3106b2SDavid Rientjes 2463bc3106b2SDavid Rientjes /* 2464698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 2465698b1b30SVlastimil Babka * sync direct compaction does. 2466698b1b30SVlastimil Babka */ 2467698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 2468698b1b30SVlastimil Babka } 2469698b1b30SVlastimil Babka 24707f354a54SDavid Rientjes count_compact_events(KCOMPACTD_MIGRATE_SCANNED, 24717f354a54SDavid Rientjes cc.total_migrate_scanned); 24727f354a54SDavid Rientjes count_compact_events(KCOMPACTD_FREE_SCANNED, 24737f354a54SDavid Rientjes cc.total_free_scanned); 24747f354a54SDavid Rientjes 2475698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 2476698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 2477698b1b30SVlastimil Babka } 2478698b1b30SVlastimil Babka 2479698b1b30SVlastimil Babka /* 2480698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 2481698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 2482698b1b30SVlastimil Babka * our current ones 2483698b1b30SVlastimil Babka */ 2484698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 2485698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 2486698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 2487698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 2488698b1b30SVlastimil Babka } 2489698b1b30SVlastimil Babka 2490698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 2491698b1b30SVlastimil Babka { 2492698b1b30SVlastimil Babka if (!order) 2493698b1b30SVlastimil Babka return; 2494698b1b30SVlastimil Babka 2495698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 2496698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 2497698b1b30SVlastimil Babka 2498698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 2499698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 2500698b1b30SVlastimil Babka 25016818600fSDavidlohr Bueso /* 25026818600fSDavidlohr Bueso * Pairs with implicit barrier in wait_event_freezable() 25036818600fSDavidlohr Bueso * such that wakeups are not missed. 25046818600fSDavidlohr Bueso */ 25056818600fSDavidlohr Bueso if (!wq_has_sleeper(&pgdat->kcompactd_wait)) 2506698b1b30SVlastimil Babka return; 2507698b1b30SVlastimil Babka 2508698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 2509698b1b30SVlastimil Babka return; 2510698b1b30SVlastimil Babka 2511698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 2512698b1b30SVlastimil Babka classzone_idx); 2513698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 2514698b1b30SVlastimil Babka } 2515698b1b30SVlastimil Babka 2516698b1b30SVlastimil Babka /* 2517698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 2518698b1b30SVlastimil Babka * from the init process. 2519698b1b30SVlastimil Babka */ 2520698b1b30SVlastimil Babka static int kcompactd(void *p) 2521698b1b30SVlastimil Babka { 2522698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 2523698b1b30SVlastimil Babka struct task_struct *tsk = current; 2524698b1b30SVlastimil Babka 2525698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 2526698b1b30SVlastimil Babka 2527698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 2528698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 2529698b1b30SVlastimil Babka 2530698b1b30SVlastimil Babka set_freezable(); 2531698b1b30SVlastimil Babka 2532698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 2533698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 2534698b1b30SVlastimil Babka 2535698b1b30SVlastimil Babka while (!kthread_should_stop()) { 2536eb414681SJohannes Weiner unsigned long pflags; 2537eb414681SJohannes Weiner 2538698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 2539698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 2540698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 2541698b1b30SVlastimil Babka 2542eb414681SJohannes Weiner psi_memstall_enter(&pflags); 2543698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 2544eb414681SJohannes Weiner psi_memstall_leave(&pflags); 2545698b1b30SVlastimil Babka } 2546698b1b30SVlastimil Babka 2547698b1b30SVlastimil Babka return 0; 2548698b1b30SVlastimil Babka } 2549698b1b30SVlastimil Babka 2550698b1b30SVlastimil Babka /* 2551698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 2552698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 2553698b1b30SVlastimil Babka */ 2554698b1b30SVlastimil Babka int kcompactd_run(int nid) 2555698b1b30SVlastimil Babka { 2556698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2557698b1b30SVlastimil Babka int ret = 0; 2558698b1b30SVlastimil Babka 2559698b1b30SVlastimil Babka if (pgdat->kcompactd) 2560698b1b30SVlastimil Babka return 0; 2561698b1b30SVlastimil Babka 2562698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 2563698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 2564698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 2565698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 2566698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 2567698b1b30SVlastimil Babka } 2568698b1b30SVlastimil Babka return ret; 2569698b1b30SVlastimil Babka } 2570698b1b30SVlastimil Babka 2571698b1b30SVlastimil Babka /* 2572698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 2573698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 2574698b1b30SVlastimil Babka */ 2575698b1b30SVlastimil Babka void kcompactd_stop(int nid) 2576698b1b30SVlastimil Babka { 2577698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 2578698b1b30SVlastimil Babka 2579698b1b30SVlastimil Babka if (kcompactd) { 2580698b1b30SVlastimil Babka kthread_stop(kcompactd); 2581698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 2582698b1b30SVlastimil Babka } 2583698b1b30SVlastimil Babka } 2584698b1b30SVlastimil Babka 2585698b1b30SVlastimil Babka /* 2586698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 2587698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 2588698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 2589698b1b30SVlastimil Babka * restore their cpu bindings. 2590698b1b30SVlastimil Babka */ 2591e46b1db2SAnna-Maria Gleixner static int kcompactd_cpu_online(unsigned int cpu) 2592698b1b30SVlastimil Babka { 2593698b1b30SVlastimil Babka int nid; 2594698b1b30SVlastimil Babka 2595698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 2596698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2597698b1b30SVlastimil Babka const struct cpumask *mask; 2598698b1b30SVlastimil Babka 2599698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 2600698b1b30SVlastimil Babka 2601698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 2602698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 2603698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 2604698b1b30SVlastimil Babka } 2605e46b1db2SAnna-Maria Gleixner return 0; 2606698b1b30SVlastimil Babka } 2607698b1b30SVlastimil Babka 2608698b1b30SVlastimil Babka static int __init kcompactd_init(void) 2609698b1b30SVlastimil Babka { 2610698b1b30SVlastimil Babka int nid; 2611e46b1db2SAnna-Maria Gleixner int ret; 2612e46b1db2SAnna-Maria Gleixner 2613e46b1db2SAnna-Maria Gleixner ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 2614e46b1db2SAnna-Maria Gleixner "mm/compaction:online", 2615e46b1db2SAnna-Maria Gleixner kcompactd_cpu_online, NULL); 2616e46b1db2SAnna-Maria Gleixner if (ret < 0) { 2617e46b1db2SAnna-Maria Gleixner pr_err("kcompactd: failed to register hotplug callbacks.\n"); 2618e46b1db2SAnna-Maria Gleixner return ret; 2619e46b1db2SAnna-Maria Gleixner } 2620698b1b30SVlastimil Babka 2621698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 2622698b1b30SVlastimil Babka kcompactd_run(nid); 2623698b1b30SVlastimil Babka return 0; 2624698b1b30SVlastimil Babka } 2625698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 2626698b1b30SVlastimil Babka 2627ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 2628