1748446bbSMel Gorman /* 2748446bbSMel Gorman * linux/mm/compaction.c 3748446bbSMel Gorman * 4748446bbSMel Gorman * Memory compaction for the reduction of external fragmentation. Note that 5748446bbSMel Gorman * this heavily depends upon page migration to do all the real heavy 6748446bbSMel Gorman * lifting 7748446bbSMel Gorman * 8748446bbSMel Gorman * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> 9748446bbSMel Gorman */ 10698b1b30SVlastimil Babka #include <linux/cpu.h> 11748446bbSMel Gorman #include <linux/swap.h> 12748446bbSMel Gorman #include <linux/migrate.h> 13748446bbSMel Gorman #include <linux/compaction.h> 14748446bbSMel Gorman #include <linux/mm_inline.h> 15748446bbSMel Gorman #include <linux/backing-dev.h> 1676ab0f53SMel Gorman #include <linux/sysctl.h> 17ed4a6d7fSMel Gorman #include <linux/sysfs.h> 18194159fbSMinchan Kim #include <linux/page-isolation.h> 19b8c73fc2SAndrey Ryabinin #include <linux/kasan.h> 20698b1b30SVlastimil Babka #include <linux/kthread.h> 21698b1b30SVlastimil Babka #include <linux/freezer.h> 2283358eceSJoonsoo Kim #include <linux/page_owner.h> 23748446bbSMel Gorman #include "internal.h" 24748446bbSMel Gorman 25010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 26010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 27010fc29aSMinchan Kim { 28010fc29aSMinchan Kim count_vm_event(item); 29010fc29aSMinchan Kim } 30010fc29aSMinchan Kim 31010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 32010fc29aSMinchan Kim { 33010fc29aSMinchan Kim count_vm_events(item, delta); 34010fc29aSMinchan Kim } 35010fc29aSMinchan Kim #else 36010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 37010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 38010fc29aSMinchan Kim #endif 39010fc29aSMinchan Kim 40ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 41ff9543fdSMichal Nazarewicz 42b7aba698SMel Gorman #define CREATE_TRACE_POINTS 43b7aba698SMel Gorman #include <trace/events/compaction.h> 44b7aba698SMel Gorman 4506b6640aSVlastimil Babka #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) 4606b6640aSVlastimil Babka #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) 4706b6640aSVlastimil Babka #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) 4806b6640aSVlastimil Babka #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) 4906b6640aSVlastimil Babka 50748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 51748446bbSMel Gorman { 52748446bbSMel Gorman struct page *page, *next; 536bace090SVlastimil Babka unsigned long high_pfn = 0; 54748446bbSMel Gorman 55748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 566bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 57748446bbSMel Gorman list_del(&page->lru); 58748446bbSMel Gorman __free_page(page); 596bace090SVlastimil Babka if (pfn > high_pfn) 606bace090SVlastimil Babka high_pfn = pfn; 61748446bbSMel Gorman } 62748446bbSMel Gorman 636bace090SVlastimil Babka return high_pfn; 64748446bbSMel Gorman } 65748446bbSMel Gorman 66ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 67ff9543fdSMichal Nazarewicz { 6866c64223SJoonsoo Kim unsigned int i, order, nr_pages; 6966c64223SJoonsoo Kim struct page *page, *next; 7066c64223SJoonsoo Kim LIST_HEAD(tmp_list); 71ff9543fdSMichal Nazarewicz 7266c64223SJoonsoo Kim list_for_each_entry_safe(page, next, list, lru) { 7366c64223SJoonsoo Kim list_del(&page->lru); 7466c64223SJoonsoo Kim 7566c64223SJoonsoo Kim order = page_private(page); 7666c64223SJoonsoo Kim nr_pages = 1 << order; 7766c64223SJoonsoo Kim 7846f24fd8SJoonsoo Kim post_alloc_hook(page, order, __GFP_MOVABLE); 7966c64223SJoonsoo Kim if (order) 8066c64223SJoonsoo Kim split_page(page, order); 8166c64223SJoonsoo Kim 8266c64223SJoonsoo Kim for (i = 0; i < nr_pages; i++) { 8366c64223SJoonsoo Kim list_add(&page->lru, &tmp_list); 8466c64223SJoonsoo Kim page++; 85ff9543fdSMichal Nazarewicz } 86ff9543fdSMichal Nazarewicz } 87ff9543fdSMichal Nazarewicz 8866c64223SJoonsoo Kim list_splice(&tmp_list, list); 8966c64223SJoonsoo Kim } 9066c64223SJoonsoo Kim 9147118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 9247118af0SMichal Nazarewicz { 9347118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 9447118af0SMichal Nazarewicz } 9547118af0SMichal Nazarewicz 96bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 9724e2716fSJoonsoo Kim 98bda807d4SMinchan Kim int PageMovable(struct page *page) 99bda807d4SMinchan Kim { 100bda807d4SMinchan Kim struct address_space *mapping; 101bda807d4SMinchan Kim 102bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 103bda807d4SMinchan Kim if (!__PageMovable(page)) 104bda807d4SMinchan Kim return 0; 105bda807d4SMinchan Kim 106bda807d4SMinchan Kim mapping = page_mapping(page); 107bda807d4SMinchan Kim if (mapping && mapping->a_ops && mapping->a_ops->isolate_page) 108bda807d4SMinchan Kim return 1; 109bda807d4SMinchan Kim 110bda807d4SMinchan Kim return 0; 111bda807d4SMinchan Kim } 112bda807d4SMinchan Kim EXPORT_SYMBOL(PageMovable); 113bda807d4SMinchan Kim 114bda807d4SMinchan Kim void __SetPageMovable(struct page *page, struct address_space *mapping) 115bda807d4SMinchan Kim { 116bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 117bda807d4SMinchan Kim VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page); 118bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE); 119bda807d4SMinchan Kim } 120bda807d4SMinchan Kim EXPORT_SYMBOL(__SetPageMovable); 121bda807d4SMinchan Kim 122bda807d4SMinchan Kim void __ClearPageMovable(struct page *page) 123bda807d4SMinchan Kim { 124bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageLocked(page), page); 125bda807d4SMinchan Kim VM_BUG_ON_PAGE(!PageMovable(page), page); 126bda807d4SMinchan Kim /* 127bda807d4SMinchan Kim * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE 128bda807d4SMinchan Kim * flag so that VM can catch up released page by driver after isolation. 129bda807d4SMinchan Kim * With it, VM migration doesn't try to put it back. 130bda807d4SMinchan Kim */ 131bda807d4SMinchan Kim page->mapping = (void *)((unsigned long)page->mapping & 132bda807d4SMinchan Kim PAGE_MAPPING_MOVABLE); 133bda807d4SMinchan Kim } 134bda807d4SMinchan Kim EXPORT_SYMBOL(__ClearPageMovable); 135bda807d4SMinchan Kim 13624e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 13724e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 13824e2716fSJoonsoo Kim 13924e2716fSJoonsoo Kim /* 14024e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 14124e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 14224e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 14324e2716fSJoonsoo Kim */ 14424e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 14524e2716fSJoonsoo Kim { 14624e2716fSJoonsoo Kim zone->compact_considered = 0; 14724e2716fSJoonsoo Kim zone->compact_defer_shift++; 14824e2716fSJoonsoo Kim 14924e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 15024e2716fSJoonsoo Kim zone->compact_order_failed = order; 15124e2716fSJoonsoo Kim 15224e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 15324e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 15424e2716fSJoonsoo Kim 15524e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 15624e2716fSJoonsoo Kim } 15724e2716fSJoonsoo Kim 15824e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 15924e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 16024e2716fSJoonsoo Kim { 16124e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 16224e2716fSJoonsoo Kim 16324e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 16424e2716fSJoonsoo Kim return false; 16524e2716fSJoonsoo Kim 16624e2716fSJoonsoo Kim /* Avoid possible overflow */ 16724e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 16824e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 16924e2716fSJoonsoo Kim 17024e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 17124e2716fSJoonsoo Kim return false; 17224e2716fSJoonsoo Kim 17324e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 17424e2716fSJoonsoo Kim 17524e2716fSJoonsoo Kim return true; 17624e2716fSJoonsoo Kim } 17724e2716fSJoonsoo Kim 17824e2716fSJoonsoo Kim /* 17924e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 18024e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 18124e2716fSJoonsoo Kim * expected to succeed. 18224e2716fSJoonsoo Kim */ 18324e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 18424e2716fSJoonsoo Kim bool alloc_success) 18524e2716fSJoonsoo Kim { 18624e2716fSJoonsoo Kim if (alloc_success) { 18724e2716fSJoonsoo Kim zone->compact_considered = 0; 18824e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 18924e2716fSJoonsoo Kim } 19024e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 19124e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 19224e2716fSJoonsoo Kim 19324e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 19424e2716fSJoonsoo Kim } 19524e2716fSJoonsoo Kim 19624e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 19724e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 19824e2716fSJoonsoo Kim { 19924e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 20024e2716fSJoonsoo Kim return false; 20124e2716fSJoonsoo Kim 20224e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 20324e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 20424e2716fSJoonsoo Kim } 20524e2716fSJoonsoo Kim 206bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 207bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 208bb13ffebSMel Gorman struct page *page) 209bb13ffebSMel Gorman { 210bb13ffebSMel Gorman if (cc->ignore_skip_hint) 211bb13ffebSMel Gorman return true; 212bb13ffebSMel Gorman 213bb13ffebSMel Gorman return !get_pageblock_skip(page); 214bb13ffebSMel Gorman } 215bb13ffebSMel Gorman 21602333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 21702333641SVlastimil Babka { 21802333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 21902333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 220623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 22106b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 22202333641SVlastimil Babka } 22302333641SVlastimil Babka 224bb13ffebSMel Gorman /* 225bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 226bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 227bb13ffebSMel Gorman * meet. 228bb13ffebSMel Gorman */ 22962997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 230bb13ffebSMel Gorman { 231bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 232108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 233bb13ffebSMel Gorman unsigned long pfn; 234bb13ffebSMel Gorman 23562997027SMel Gorman zone->compact_blockskip_flush = false; 236bb13ffebSMel Gorman 237bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 238bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 239bb13ffebSMel Gorman struct page *page; 240bb13ffebSMel Gorman 241bb13ffebSMel Gorman cond_resched(); 242bb13ffebSMel Gorman 243bb13ffebSMel Gorman if (!pfn_valid(pfn)) 244bb13ffebSMel Gorman continue; 245bb13ffebSMel Gorman 246bb13ffebSMel Gorman page = pfn_to_page(pfn); 247bb13ffebSMel Gorman if (zone != page_zone(page)) 248bb13ffebSMel Gorman continue; 249bb13ffebSMel Gorman 250bb13ffebSMel Gorman clear_pageblock_skip(page); 251bb13ffebSMel Gorman } 25202333641SVlastimil Babka 25302333641SVlastimil Babka reset_cached_positions(zone); 254bb13ffebSMel Gorman } 255bb13ffebSMel Gorman 25662997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 25762997027SMel Gorman { 25862997027SMel Gorman int zoneid; 25962997027SMel Gorman 26062997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 26162997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 26262997027SMel Gorman if (!populated_zone(zone)) 26362997027SMel Gorman continue; 26462997027SMel Gorman 26562997027SMel Gorman /* Only flush if a full compaction finished recently */ 26662997027SMel Gorman if (zone->compact_blockskip_flush) 26762997027SMel Gorman __reset_isolation_suitable(zone); 26862997027SMel Gorman } 26962997027SMel Gorman } 27062997027SMel Gorman 271bb13ffebSMel Gorman /* 272bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 27362997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 274bb13ffebSMel Gorman */ 275c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 276c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 277edc2ca61SVlastimil Babka bool migrate_scanner) 278bb13ffebSMel Gorman { 279c89511abSMel Gorman struct zone *zone = cc->zone; 28035979ef3SDavid Rientjes unsigned long pfn; 2816815bf3fSJoonsoo Kim 2826815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2836815bf3fSJoonsoo Kim return; 2846815bf3fSJoonsoo Kim 285bb13ffebSMel Gorman if (!page) 286bb13ffebSMel Gorman return; 287bb13ffebSMel Gorman 28835979ef3SDavid Rientjes if (nr_isolated) 28935979ef3SDavid Rientjes return; 29035979ef3SDavid Rientjes 291bb13ffebSMel Gorman set_pageblock_skip(page); 292c89511abSMel Gorman 29335979ef3SDavid Rientjes pfn = page_to_pfn(page); 29435979ef3SDavid Rientjes 29535979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 296c89511abSMel Gorman if (migrate_scanner) { 29735979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 29835979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 299e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 300e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 30135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 302c89511abSMel Gorman } else { 30335979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 304c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 305c89511abSMel Gorman } 306c89511abSMel Gorman } 307bb13ffebSMel Gorman #else 308bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 309bb13ffebSMel Gorman struct page *page) 310bb13ffebSMel Gorman { 311bb13ffebSMel Gorman return true; 312bb13ffebSMel Gorman } 313bb13ffebSMel Gorman 314c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 315c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 316edc2ca61SVlastimil Babka bool migrate_scanner) 317bb13ffebSMel Gorman { 318bb13ffebSMel Gorman } 319bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 320bb13ffebSMel Gorman 3211f9efdefSVlastimil Babka /* 3228b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 3238b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 3248b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 3258b44d279SVlastimil Babka * 3268b44d279SVlastimil Babka * Returns true if the lock is held 3278b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 3281f9efdefSVlastimil Babka */ 3298b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 3308b44d279SVlastimil Babka struct compact_control *cc) 3318b44d279SVlastimil Babka { 3328b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3338b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 334c3486f53SVlastimil Babka cc->contended = true; 3358b44d279SVlastimil Babka return false; 3368b44d279SVlastimil Babka } 3378b44d279SVlastimil Babka } else { 3388b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 3398b44d279SVlastimil Babka } 3401f9efdefSVlastimil Babka 3418b44d279SVlastimil Babka return true; 3422a1402aaSMel Gorman } 3432a1402aaSMel Gorman 34485aa125fSMichal Nazarewicz /* 345c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 3468b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 3478b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 3488b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 3498b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 3508b44d279SVlastimil Babka * aborts. Sync compaction schedules. 3518b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 3528b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 353c67fe375SMel Gorman * 3548b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 3558b44d279SVlastimil Babka * async compaction due to need_resched() 3568b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3578b44d279SVlastimil Babka * scheduled) 358c67fe375SMel Gorman */ 3598b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3608b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 361c67fe375SMel Gorman { 3628b44d279SVlastimil Babka if (*locked) { 3638b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3648b44d279SVlastimil Babka *locked = false; 365c67fe375SMel Gorman } 366c67fe375SMel Gorman 3678b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 368c3486f53SVlastimil Babka cc->contended = true; 3698b44d279SVlastimil Babka return true; 3708b44d279SVlastimil Babka } 3718b44d279SVlastimil Babka 3728b44d279SVlastimil Babka if (need_resched()) { 373e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 374c3486f53SVlastimil Babka cc->contended = true; 3758b44d279SVlastimil Babka return true; 376c67fe375SMel Gorman } 377c67fe375SMel Gorman cond_resched(); 378c67fe375SMel Gorman } 379c67fe375SMel Gorman 3808b44d279SVlastimil Babka return false; 381c67fe375SMel Gorman } 382c67fe375SMel Gorman 383be976572SVlastimil Babka /* 384be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 385be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3868b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 387be976572SVlastimil Babka * is used where no lock is concerned. 388be976572SVlastimil Babka * 389be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 390be976572SVlastimil Babka * Returns true when async compaction should abort. 391be976572SVlastimil Babka */ 392be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 393be976572SVlastimil Babka { 394be976572SVlastimil Babka /* async compaction aborts if contended */ 395be976572SVlastimil Babka if (need_resched()) { 396be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 397c3486f53SVlastimil Babka cc->contended = true; 398be976572SVlastimil Babka return true; 399be976572SVlastimil Babka } 400be976572SVlastimil Babka 401be976572SVlastimil Babka cond_resched(); 402be976572SVlastimil Babka } 403be976572SVlastimil Babka 404be976572SVlastimil Babka return false; 405be976572SVlastimil Babka } 406be976572SVlastimil Babka 407c67fe375SMel Gorman /* 4089e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 4099e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 4109e4be470SJerome Marchand * (even though it may still end up isolating some pages). 41185aa125fSMichal Nazarewicz */ 412f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 413e14c720eSVlastimil Babka unsigned long *start_pfn, 41485aa125fSMichal Nazarewicz unsigned long end_pfn, 41585aa125fSMichal Nazarewicz struct list_head *freelist, 41685aa125fSMichal Nazarewicz bool strict) 417748446bbSMel Gorman { 418b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 419bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 420b8b2d825SXiubo Li unsigned long flags = 0; 421f40d1e42SMel Gorman bool locked = false; 422e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 42366c64223SJoonsoo Kim unsigned int order; 424748446bbSMel Gorman 425748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 426748446bbSMel Gorman 427f40d1e42SMel Gorman /* Isolate free pages. */ 428748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 42966c64223SJoonsoo Kim int isolated; 430748446bbSMel Gorman struct page *page = cursor; 431748446bbSMel Gorman 4328b44d279SVlastimil Babka /* 4338b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 4348b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 4358b44d279SVlastimil Babka * pending or async compaction detects need_resched() 4368b44d279SVlastimil Babka */ 4378b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 4388b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 4398b44d279SVlastimil Babka &locked, cc)) 4408b44d279SVlastimil Babka break; 4418b44d279SVlastimil Babka 442b7aba698SMel Gorman nr_scanned++; 443f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 4442af120bcSLaura Abbott goto isolate_fail; 4452af120bcSLaura Abbott 446bb13ffebSMel Gorman if (!valid_page) 447bb13ffebSMel Gorman valid_page = page; 4489fcd6d2eSVlastimil Babka 4499fcd6d2eSVlastimil Babka /* 4509fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 4519fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 4529fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 4539fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 4549fcd6d2eSVlastimil Babka */ 4559fcd6d2eSVlastimil Babka if (PageCompound(page)) { 4569fcd6d2eSVlastimil Babka unsigned int comp_order = compound_order(page); 4579fcd6d2eSVlastimil Babka 4589fcd6d2eSVlastimil Babka if (likely(comp_order < MAX_ORDER)) { 4599fcd6d2eSVlastimil Babka blockpfn += (1UL << comp_order) - 1; 4609fcd6d2eSVlastimil Babka cursor += (1UL << comp_order) - 1; 4619fcd6d2eSVlastimil Babka } 4629fcd6d2eSVlastimil Babka 4639fcd6d2eSVlastimil Babka goto isolate_fail; 4649fcd6d2eSVlastimil Babka } 4659fcd6d2eSVlastimil Babka 466f40d1e42SMel Gorman if (!PageBuddy(page)) 4672af120bcSLaura Abbott goto isolate_fail; 468f40d1e42SMel Gorman 469f40d1e42SMel Gorman /* 47069b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 47169b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 47269b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 47369b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 47469b7189fSVlastimil Babka * recheck as well. 47569b7189fSVlastimil Babka */ 47669b7189fSVlastimil Babka if (!locked) { 47769b7189fSVlastimil Babka /* 478f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 479f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 480f40d1e42SMel Gorman * heavily contended if there are parallel allocations 481f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 482f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 483f40d1e42SMel Gorman * possible. 484f40d1e42SMel Gorman */ 4858b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4868b44d279SVlastimil Babka &flags, cc); 487f40d1e42SMel Gorman if (!locked) 488f40d1e42SMel Gorman break; 489f40d1e42SMel Gorman 490f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 491f40d1e42SMel Gorman if (!PageBuddy(page)) 4922af120bcSLaura Abbott goto isolate_fail; 49369b7189fSVlastimil Babka } 494748446bbSMel Gorman 49566c64223SJoonsoo Kim /* Found a free page, will break it into order-0 pages */ 49666c64223SJoonsoo Kim order = page_order(page); 49766c64223SJoonsoo Kim isolated = __isolate_free_page(page, order); 498a4f04f2cSDavid Rientjes if (!isolated) 499a4f04f2cSDavid Rientjes break; 50066c64223SJoonsoo Kim set_page_private(page, order); 501a4f04f2cSDavid Rientjes 502748446bbSMel Gorman total_isolated += isolated; 503a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 50466c64223SJoonsoo Kim list_add_tail(&page->lru, freelist); 50566c64223SJoonsoo Kim 506a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 507932ff6bbSJoonsoo Kim blockpfn += isolated; 508932ff6bbSJoonsoo Kim break; 509932ff6bbSJoonsoo Kim } 510a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 511748446bbSMel Gorman blockpfn += isolated - 1; 512748446bbSMel Gorman cursor += isolated - 1; 5132af120bcSLaura Abbott continue; 5142af120bcSLaura Abbott 5152af120bcSLaura Abbott isolate_fail: 5162af120bcSLaura Abbott if (strict) 5172af120bcSLaura Abbott break; 5182af120bcSLaura Abbott else 5192af120bcSLaura Abbott continue; 5202af120bcSLaura Abbott 521748446bbSMel Gorman } 522748446bbSMel Gorman 523a4f04f2cSDavid Rientjes if (locked) 524a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 525a4f04f2cSDavid Rientjes 5269fcd6d2eSVlastimil Babka /* 5279fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 5289fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 5299fcd6d2eSVlastimil Babka */ 5309fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 5319fcd6d2eSVlastimil Babka blockpfn = end_pfn; 5329fcd6d2eSVlastimil Babka 533e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 534e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 535e34d85f0SJoonsoo Kim 536e14c720eSVlastimil Babka /* Record how far we have got within the block */ 537e14c720eSVlastimil Babka *start_pfn = blockpfn; 538e14c720eSVlastimil Babka 539f40d1e42SMel Gorman /* 540f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 541f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 542f40d1e42SMel Gorman * returned and CMA will fail. 543f40d1e42SMel Gorman */ 5442af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 545f40d1e42SMel Gorman total_isolated = 0; 546f40d1e42SMel Gorman 547bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 548bb13ffebSMel Gorman if (blockpfn == end_pfn) 549edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 550bb13ffebSMel Gorman 551010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 552397487dbSMel Gorman if (total_isolated) 553010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 554748446bbSMel Gorman return total_isolated; 555748446bbSMel Gorman } 556748446bbSMel Gorman 55785aa125fSMichal Nazarewicz /** 55885aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 55985aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 56085aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 56185aa125fSMichal Nazarewicz * 56285aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 56385aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 56485aa125fSMichal Nazarewicz * undo its actions and return zero. 56585aa125fSMichal Nazarewicz * 56685aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 56785aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 56885aa125fSMichal Nazarewicz * a free page). 56985aa125fSMichal Nazarewicz */ 570ff9543fdSMichal Nazarewicz unsigned long 571bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 572bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 57385aa125fSMichal Nazarewicz { 574e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 57585aa125fSMichal Nazarewicz LIST_HEAD(freelist); 57685aa125fSMichal Nazarewicz 5777d49d886SVlastimil Babka pfn = start_pfn; 57806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 579e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 580e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 58106b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 5827d49d886SVlastimil Babka 5837d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 584e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 5857d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 586e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 587e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5887d49d886SVlastimil Babka 58985aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 59085aa125fSMichal Nazarewicz 59158420016SJoonsoo Kim /* 59258420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 59358420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 59458420016SJoonsoo Kim * scanning range to right one. 59558420016SJoonsoo Kim */ 59658420016SJoonsoo Kim if (pfn >= block_end_pfn) { 59706b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 59806b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 59958420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 60058420016SJoonsoo Kim } 60158420016SJoonsoo Kim 602e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 603e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 6047d49d886SVlastimil Babka break; 6057d49d886SVlastimil Babka 606e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 607e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 60885aa125fSMichal Nazarewicz 60985aa125fSMichal Nazarewicz /* 61085aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 61185aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 61285aa125fSMichal Nazarewicz * non-free pages). 61385aa125fSMichal Nazarewicz */ 61485aa125fSMichal Nazarewicz if (!isolated) 61585aa125fSMichal Nazarewicz break; 61685aa125fSMichal Nazarewicz 61785aa125fSMichal Nazarewicz /* 61885aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 61985aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 62085aa125fSMichal Nazarewicz * page may span two pageblocks). 62185aa125fSMichal Nazarewicz */ 62285aa125fSMichal Nazarewicz } 62385aa125fSMichal Nazarewicz 62466c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 62585aa125fSMichal Nazarewicz map_pages(&freelist); 62685aa125fSMichal Nazarewicz 62785aa125fSMichal Nazarewicz if (pfn < end_pfn) { 62885aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 62985aa125fSMichal Nazarewicz release_freepages(&freelist); 63085aa125fSMichal Nazarewicz return 0; 63185aa125fSMichal Nazarewicz } 63285aa125fSMichal Nazarewicz 63385aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 63485aa125fSMichal Nazarewicz return pfn; 63585aa125fSMichal Nazarewicz } 63685aa125fSMichal Nazarewicz 637748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 638748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 639748446bbSMel Gorman { 640bc693045SMinchan Kim unsigned long active, inactive, isolated; 641748446bbSMel Gorman 642599d0c95SMel Gorman inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) + 643599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON); 644599d0c95SMel Gorman active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) + 645599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON); 646599d0c95SMel Gorman isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) + 647599d0c95SMel Gorman node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON); 648748446bbSMel Gorman 649bc693045SMinchan Kim return isolated > (inactive + active) / 2; 650748446bbSMel Gorman } 651748446bbSMel Gorman 6522fe86e00SMichal Nazarewicz /** 653edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 654edc2ca61SVlastimil Babka * a single pageblock 6552fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 656edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 657edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 658edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6592fe86e00SMichal Nazarewicz * 6602fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 661edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 662edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 663edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 664edc2ca61SVlastimil Babka * than end_pfn). 6652fe86e00SMichal Nazarewicz * 666edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 667edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 668edc2ca61SVlastimil Babka * is neither read nor updated. 669748446bbSMel Gorman */ 670edc2ca61SVlastimil Babka static unsigned long 671edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 672edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 673748446bbSMel Gorman { 674edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 675b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 676fa9add64SHugh Dickins struct lruvec *lruvec; 677b8b2d825SXiubo Li unsigned long flags = 0; 6782a1402aaSMel Gorman bool locked = false; 679bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 680e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 681fdd048e1SVlastimil Babka bool skip_on_failure = false; 682fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 683748446bbSMel Gorman 684748446bbSMel Gorman /* 685748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 686748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 687748446bbSMel Gorman * delay for some time until fewer pages are isolated 688748446bbSMel Gorman */ 689748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 690f9e35b3bSMel Gorman /* async migration should just abort */ 691e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6922fe86e00SMichal Nazarewicz return 0; 693f9e35b3bSMel Gorman 694748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 695748446bbSMel Gorman 696748446bbSMel Gorman if (fatal_signal_pending(current)) 6972fe86e00SMichal Nazarewicz return 0; 698748446bbSMel Gorman } 699748446bbSMel Gorman 700be976572SVlastimil Babka if (compact_should_abort(cc)) 701aeef4b83SDavid Rientjes return 0; 702aeef4b83SDavid Rientjes 703fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 704fdd048e1SVlastimil Babka skip_on_failure = true; 705fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 706fdd048e1SVlastimil Babka } 707fdd048e1SVlastimil Babka 708748446bbSMel Gorman /* Time to isolate some pages for migration */ 709748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 71029c0dde8SVlastimil Babka 711fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 712fdd048e1SVlastimil Babka /* 713fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 714fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 715fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 716fdd048e1SVlastimil Babka * hopefully succeed compaction. 717fdd048e1SVlastimil Babka */ 718fdd048e1SVlastimil Babka if (nr_isolated) 719fdd048e1SVlastimil Babka break; 720fdd048e1SVlastimil Babka 721fdd048e1SVlastimil Babka /* 722fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 723fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 724fdd048e1SVlastimil Babka * current block. Note we can't simply increase 725fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 726fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 727fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 728fdd048e1SVlastimil Babka * previous loop iteration. 729fdd048e1SVlastimil Babka */ 730fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 731fdd048e1SVlastimil Babka } 732fdd048e1SVlastimil Babka 7338b44d279SVlastimil Babka /* 7348b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 7358b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7368b44d279SVlastimil Babka * if contended. 7378b44d279SVlastimil Babka */ 7388b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 739a52633d8SMel Gorman && compact_unlock_should_abort(zone_lru_lock(zone), flags, 7408b44d279SVlastimil Babka &locked, cc)) 7418b44d279SVlastimil Babka break; 742b2eef8c0SAndrea Arcangeli 743748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 744fdd048e1SVlastimil Babka goto isolate_fail; 745b7aba698SMel Gorman nr_scanned++; 746748446bbSMel Gorman 747748446bbSMel Gorman page = pfn_to_page(low_pfn); 748dc908600SMel Gorman 749bb13ffebSMel Gorman if (!valid_page) 750bb13ffebSMel Gorman valid_page = page; 751bb13ffebSMel Gorman 752c122b208SJoonsoo Kim /* 75399c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 75499c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 75599c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 75699c0fd5eSVlastimil Babka * potential isolation targets. 7576c14466cSMel Gorman */ 75899c0fd5eSVlastimil Babka if (PageBuddy(page)) { 75999c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 76099c0fd5eSVlastimil Babka 76199c0fd5eSVlastimil Babka /* 76299c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 76399c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 76499c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 76599c0fd5eSVlastimil Babka */ 76699c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 76799c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 768748446bbSMel Gorman continue; 76999c0fd5eSVlastimil Babka } 770748446bbSMel Gorman 7719927af74SMel Gorman /* 77229c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 77329c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 77429c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 77529c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 77629c0dde8SVlastimil Babka * danger is skipping too much. 777bc835011SAndrea Arcangeli */ 77829c0dde8SVlastimil Babka if (PageCompound(page)) { 77929c0dde8SVlastimil Babka unsigned int comp_order = compound_order(page); 78029c0dde8SVlastimil Babka 78129c0dde8SVlastimil Babka if (likely(comp_order < MAX_ORDER)) 78229c0dde8SVlastimil Babka low_pfn += (1UL << comp_order) - 1; 783edc2ca61SVlastimil Babka 784fdd048e1SVlastimil Babka goto isolate_fail; 7852a1402aaSMel Gorman } 7862a1402aaSMel Gorman 787bda807d4SMinchan Kim /* 788bda807d4SMinchan Kim * Check may be lockless but that's ok as we recheck later. 789bda807d4SMinchan Kim * It's possible to migrate LRU and non-lru movable pages. 790bda807d4SMinchan Kim * Skip any other type of page 791bda807d4SMinchan Kim */ 792bda807d4SMinchan Kim if (!PageLRU(page)) { 793bda807d4SMinchan Kim /* 794bda807d4SMinchan Kim * __PageMovable can return false positive so we need 795bda807d4SMinchan Kim * to verify it under page_lock. 796bda807d4SMinchan Kim */ 797bda807d4SMinchan Kim if (unlikely(__PageMovable(page)) && 798bda807d4SMinchan Kim !PageIsolated(page)) { 799bda807d4SMinchan Kim if (locked) { 800a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), 801bda807d4SMinchan Kim flags); 802bda807d4SMinchan Kim locked = false; 803bda807d4SMinchan Kim } 804bda807d4SMinchan Kim 805bda807d4SMinchan Kim if (isolate_movable_page(page, isolate_mode)) 806bda807d4SMinchan Kim goto isolate_success; 807bda807d4SMinchan Kim } 808bda807d4SMinchan Kim 809fdd048e1SVlastimil Babka goto isolate_fail; 810bda807d4SMinchan Kim } 81129c0dde8SVlastimil Babka 812119d6d59SDavid Rientjes /* 813119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 814119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 815119d6d59SDavid Rientjes * admittedly racy check. 816119d6d59SDavid Rientjes */ 817119d6d59SDavid Rientjes if (!page_mapping(page) && 818119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 819fdd048e1SVlastimil Babka goto isolate_fail; 820119d6d59SDavid Rientjes 82169b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 82269b7189fSVlastimil Babka if (!locked) { 823a52633d8SMel Gorman locked = compact_trylock_irqsave(zone_lru_lock(zone), 8248b44d279SVlastimil Babka &flags, cc); 8258b44d279SVlastimil Babka if (!locked) 8262a1402aaSMel Gorman break; 8272a1402aaSMel Gorman 82829c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 8292a1402aaSMel Gorman if (!PageLRU(page)) 830fdd048e1SVlastimil Babka goto isolate_fail; 83129c0dde8SVlastimil Babka 83229c0dde8SVlastimil Babka /* 83329c0dde8SVlastimil Babka * Page become compound since the non-locked check, 83429c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 83529c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 83629c0dde8SVlastimil Babka */ 83729c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 83829c0dde8SVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 839fdd048e1SVlastimil Babka goto isolate_fail; 840bc835011SAndrea Arcangeli } 84169b7189fSVlastimil Babka } 842bc835011SAndrea Arcangeli 843599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 844fa9add64SHugh Dickins 845748446bbSMel Gorman /* Try isolate the page */ 846edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 847fdd048e1SVlastimil Babka goto isolate_fail; 848748446bbSMel Gorman 84929c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 850bc835011SAndrea Arcangeli 851748446bbSMel Gorman /* Successfully isolated */ 852fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 853*6afcf8efSMing Ling inc_node_page_state(page, 854*6afcf8efSMing Ling NR_ISOLATED_ANON + page_is_file_cache(page)); 855b6c75016SJoonsoo Kim 856b6c75016SJoonsoo Kim isolate_success: 857fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 858748446bbSMel Gorman cc->nr_migratepages++; 859b7aba698SMel Gorman nr_isolated++; 860748446bbSMel Gorman 861a34753d2SVlastimil Babka /* 862a34753d2SVlastimil Babka * Record where we could have freed pages by migration and not 863a34753d2SVlastimil Babka * yet flushed them to buddy allocator. 864a34753d2SVlastimil Babka * - this is the lowest page that was isolated and likely be 865a34753d2SVlastimil Babka * then freed by migration. 866a34753d2SVlastimil Babka */ 867a34753d2SVlastimil Babka if (!cc->last_migrated_pfn) 868a34753d2SVlastimil Babka cc->last_migrated_pfn = low_pfn; 869a34753d2SVlastimil Babka 870748446bbSMel Gorman /* Avoid isolating too much */ 87131b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 87231b8384aSHillf Danton ++low_pfn; 873748446bbSMel Gorman break; 874748446bbSMel Gorman } 875fdd048e1SVlastimil Babka 876fdd048e1SVlastimil Babka continue; 877fdd048e1SVlastimil Babka isolate_fail: 878fdd048e1SVlastimil Babka if (!skip_on_failure) 879fdd048e1SVlastimil Babka continue; 880fdd048e1SVlastimil Babka 881fdd048e1SVlastimil Babka /* 882fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 883fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 884fdd048e1SVlastimil Babka * page anyway. 885fdd048e1SVlastimil Babka */ 886fdd048e1SVlastimil Babka if (nr_isolated) { 887fdd048e1SVlastimil Babka if (locked) { 888a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 889fdd048e1SVlastimil Babka locked = false; 890fdd048e1SVlastimil Babka } 891fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 892fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 893fdd048e1SVlastimil Babka cc->last_migrated_pfn = 0; 894fdd048e1SVlastimil Babka nr_isolated = 0; 895fdd048e1SVlastimil Babka } 896fdd048e1SVlastimil Babka 897fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 898fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 899fdd048e1SVlastimil Babka /* 900fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 901fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 902fdd048e1SVlastimil Babka */ 903fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 904fdd048e1SVlastimil Babka } 90531b8384aSHillf Danton } 906748446bbSMel Gorman 90799c0fd5eSVlastimil Babka /* 90899c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 90999c0fd5eSVlastimil Babka * the range to be scanned. 91099c0fd5eSVlastimil Babka */ 91199c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 91299c0fd5eSVlastimil Babka low_pfn = end_pfn; 91399c0fd5eSVlastimil Babka 914c67fe375SMel Gorman if (locked) 915a52633d8SMel Gorman spin_unlock_irqrestore(zone_lru_lock(zone), flags); 916748446bbSMel Gorman 91750b5b094SVlastimil Babka /* 91850b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 91950b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 92050b5b094SVlastimil Babka */ 92135979ef3SDavid Rientjes if (low_pfn == end_pfn) 922edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 923bb13ffebSMel Gorman 924e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 925e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 926b7aba698SMel Gorman 927010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 928397487dbSMel Gorman if (nr_isolated) 929010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 930397487dbSMel Gorman 9312fe86e00SMichal Nazarewicz return low_pfn; 9322fe86e00SMichal Nazarewicz } 9332fe86e00SMichal Nazarewicz 934edc2ca61SVlastimil Babka /** 935edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 936edc2ca61SVlastimil Babka * @cc: Compaction control structure. 937edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 938edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 939edc2ca61SVlastimil Babka * 940edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 941edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 942edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 943edc2ca61SVlastimil Babka */ 944edc2ca61SVlastimil Babka unsigned long 945edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 946edc2ca61SVlastimil Babka unsigned long end_pfn) 947edc2ca61SVlastimil Babka { 948e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 949edc2ca61SVlastimil Babka 950edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 951edc2ca61SVlastimil Babka pfn = start_pfn; 95206b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 953e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 954e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 95506b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 956edc2ca61SVlastimil Babka 957edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 958e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 959edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 960edc2ca61SVlastimil Babka 961edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 962edc2ca61SVlastimil Babka 963e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 964e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 965edc2ca61SVlastimil Babka continue; 966edc2ca61SVlastimil Babka 967edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 968edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 969edc2ca61SVlastimil Babka 97014af4a5eSHugh Dickins if (!pfn) 971edc2ca61SVlastimil Babka break; 9726ea41c0cSJoonsoo Kim 9736ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 9746ea41c0cSJoonsoo Kim break; 975edc2ca61SVlastimil Babka } 976edc2ca61SVlastimil Babka 977edc2ca61SVlastimil Babka return pfn; 978edc2ca61SVlastimil Babka } 979edc2ca61SVlastimil Babka 980ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 981ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 982018e9a49SAndrew Morton 983018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 9849f7e3387SVlastimil Babka static bool suitable_migration_target(struct compact_control *cc, 9859f7e3387SVlastimil Babka struct page *page) 986018e9a49SAndrew Morton { 9879f7e3387SVlastimil Babka if (cc->ignore_block_suitable) 9889f7e3387SVlastimil Babka return true; 9899f7e3387SVlastimil Babka 990018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 991018e9a49SAndrew Morton if (PageBuddy(page)) { 992018e9a49SAndrew Morton /* 993018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 994018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 995018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 996018e9a49SAndrew Morton */ 997018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 998018e9a49SAndrew Morton return false; 999018e9a49SAndrew Morton } 1000018e9a49SAndrew Morton 1001018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 1002018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 1003018e9a49SAndrew Morton return true; 1004018e9a49SAndrew Morton 1005018e9a49SAndrew Morton /* Otherwise skip the block */ 1006018e9a49SAndrew Morton return false; 1007018e9a49SAndrew Morton } 1008018e9a49SAndrew Morton 1009ff9543fdSMichal Nazarewicz /* 1010f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 1011f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 1012f2849aa0SVlastimil Babka */ 1013f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 1014f2849aa0SVlastimil Babka { 1015f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 1016f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 1017f2849aa0SVlastimil Babka } 1018f2849aa0SVlastimil Babka 1019f2849aa0SVlastimil Babka /* 1020ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 1021ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 1022ff9543fdSMichal Nazarewicz */ 1023edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 1024ff9543fdSMichal Nazarewicz { 1025edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 1026ff9543fdSMichal Nazarewicz struct page *page; 1027c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 1028e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 1029c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 1030c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 1031ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 10322fe86e00SMichal Nazarewicz 1033ff9543fdSMichal Nazarewicz /* 1034ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 103549e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 1036e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 1037e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 1038c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 1039c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 1040c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 104149e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 104249e068f0SVlastimil Babka * is using. 1043ff9543fdSMichal Nazarewicz */ 1044e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 104506b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(cc->free_pfn); 1046c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1047c96b9e50SVlastimil Babka zone_end_pfn(zone)); 104806b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 10492fe86e00SMichal Nazarewicz 1050ff9543fdSMichal Nazarewicz /* 1051ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1052ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1053ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1054ff9543fdSMichal Nazarewicz */ 1055f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1056c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1057e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1058e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 1059f6ea3adbSDavid Rientjes /* 1060f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1061f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 1062be976572SVlastimil Babka * to schedule, or even abort async compaction. 1063f6ea3adbSDavid Rientjes */ 1064be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1065be976572SVlastimil Babka && compact_should_abort(cc)) 1066be976572SVlastimil Babka break; 1067f6ea3adbSDavid Rientjes 10687d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 10697d49d886SVlastimil Babka zone); 10707d49d886SVlastimil Babka if (!page) 1071ff9543fdSMichal Nazarewicz continue; 1072ff9543fdSMichal Nazarewicz 1073ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 10749f7e3387SVlastimil Babka if (!suitable_migration_target(cc, page)) 1075ff9543fdSMichal Nazarewicz continue; 107668e3e926SLinus Torvalds 1077bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1078bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1079bb13ffebSMel Gorman continue; 1080bb13ffebSMel Gorman 1081e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 1082a46cbf3bSDavid Rientjes isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn, 1083a46cbf3bSDavid Rientjes freelist, false); 1084ff9543fdSMichal Nazarewicz 1085ff9543fdSMichal Nazarewicz /* 1086a46cbf3bSDavid Rientjes * If we isolated enough freepages, or aborted due to lock 1087a46cbf3bSDavid Rientjes * contention, terminate. 1088e14c720eSVlastimil Babka */ 1089f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 1090f5f61a32SVlastimil Babka || cc->contended) { 1091a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1092a46cbf3bSDavid Rientjes /* 1093a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1094a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1095a46cbf3bSDavid Rientjes */ 1096f5f61a32SVlastimil Babka isolate_start_pfn = 1097e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1098a46cbf3bSDavid Rientjes } 1099be976572SVlastimil Babka break; 1100a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1101f5f61a32SVlastimil Babka /* 1102a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1103a46cbf3bSDavid Rientjes * needlessly. 1104f5f61a32SVlastimil Babka */ 1105a46cbf3bSDavid Rientjes break; 1106f5f61a32SVlastimil Babka } 1107c89511abSMel Gorman } 1108ff9543fdSMichal Nazarewicz 110966c64223SJoonsoo Kim /* __isolate_free_page() does not map the pages */ 1110ff9543fdSMichal Nazarewicz map_pages(freelist); 1111ff9543fdSMichal Nazarewicz 11127ed695e0SVlastimil Babka /* 1113f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1114f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1115f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1116f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 11177ed695e0SVlastimil Babka */ 1118f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1119748446bbSMel Gorman } 1120748446bbSMel Gorman 1121748446bbSMel Gorman /* 1122748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1123748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1124748446bbSMel Gorman */ 1125748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1126748446bbSMel Gorman unsigned long data, 1127748446bbSMel Gorman int **result) 1128748446bbSMel Gorman { 1129748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1130748446bbSMel Gorman struct page *freepage; 1131748446bbSMel Gorman 1132be976572SVlastimil Babka /* 1133be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1134be976572SVlastimil Babka * contention. 1135be976572SVlastimil Babka */ 1136748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1137be976572SVlastimil Babka if (!cc->contended) 1138edc2ca61SVlastimil Babka isolate_freepages(cc); 1139748446bbSMel Gorman 1140748446bbSMel Gorman if (list_empty(&cc->freepages)) 1141748446bbSMel Gorman return NULL; 1142748446bbSMel Gorman } 1143748446bbSMel Gorman 1144748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1145748446bbSMel Gorman list_del(&freepage->lru); 1146748446bbSMel Gorman cc->nr_freepages--; 1147748446bbSMel Gorman 1148748446bbSMel Gorman return freepage; 1149748446bbSMel Gorman } 1150748446bbSMel Gorman 1151748446bbSMel Gorman /* 1152d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1153d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1154d53aea3dSDavid Rientjes * special handling needed for NUMA. 1155d53aea3dSDavid Rientjes */ 1156d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1157d53aea3dSDavid Rientjes { 1158d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1159d53aea3dSDavid Rientjes 1160d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1161d53aea3dSDavid Rientjes cc->nr_freepages++; 1162d53aea3dSDavid Rientjes } 1163d53aea3dSDavid Rientjes 1164ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1165ff9543fdSMichal Nazarewicz typedef enum { 1166ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1167ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1168ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1169ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1170ff9543fdSMichal Nazarewicz 1171ff9543fdSMichal Nazarewicz /* 11725bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 11735bbe3547SEric B Munson * compactable pages. 11745bbe3547SEric B Munson */ 11755bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 11765bbe3547SEric B Munson 11775bbe3547SEric B Munson /* 1178edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1179edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1180edc2ca61SVlastimil Babka * compact_control. 1181ff9543fdSMichal Nazarewicz */ 1182ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1183ff9543fdSMichal Nazarewicz struct compact_control *cc) 1184ff9543fdSMichal Nazarewicz { 1185e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1186e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1187e1409c32SJoonsoo Kim unsigned long low_pfn; 1188edc2ca61SVlastimil Babka struct page *page; 1189edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 11905bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 11911d2047feSHugh Dickins (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1192ff9543fdSMichal Nazarewicz 1193edc2ca61SVlastimil Babka /* 1194edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1195edc2ca61SVlastimil Babka * initialized by compact_zone() 1196edc2ca61SVlastimil Babka */ 1197edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 119806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1199e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1200e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1201ff9543fdSMichal Nazarewicz 1202ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 120306b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1204ff9543fdSMichal Nazarewicz 1205edc2ca61SVlastimil Babka /* 1206edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1207edc2ca61SVlastimil Babka * Do not cross the free scanner. 1208edc2ca61SVlastimil Babka */ 1209e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 1210e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1211e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1212e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1213edc2ca61SVlastimil Babka 1214edc2ca61SVlastimil Babka /* 1215edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1216edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1217edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1218edc2ca61SVlastimil Babka */ 1219edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1220edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1221edc2ca61SVlastimil Babka break; 1222edc2ca61SVlastimil Babka 1223e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1224e1409c32SJoonsoo Kim zone); 12257d49d886SVlastimil Babka if (!page) 1226edc2ca61SVlastimil Babka continue; 1227edc2ca61SVlastimil Babka 1228edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1229edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1230edc2ca61SVlastimil Babka continue; 1231edc2ca61SVlastimil Babka 1232edc2ca61SVlastimil Babka /* 1233edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1234edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1235edc2ca61SVlastimil Babka * of work satisfies the allocation. 1236edc2ca61SVlastimil Babka */ 1237edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1238edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1239edc2ca61SVlastimil Babka continue; 1240ff9543fdSMichal Nazarewicz 1241ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1242e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1243e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1244edc2ca61SVlastimil Babka 1245*6afcf8efSMing Ling if (!low_pfn || cc->contended) 1246ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1247ff9543fdSMichal Nazarewicz 1248edc2ca61SVlastimil Babka /* 1249edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1250edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1251edc2ca61SVlastimil Babka * continue or not. 1252edc2ca61SVlastimil Babka */ 1253edc2ca61SVlastimil Babka break; 1254edc2ca61SVlastimil Babka } 1255edc2ca61SVlastimil Babka 1256f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1257f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1258ff9543fdSMichal Nazarewicz 1259edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1260ff9543fdSMichal Nazarewicz } 1261ff9543fdSMichal Nazarewicz 126221c527a3SYaowei Bai /* 126321c527a3SYaowei Bai * order == -1 is expected when compacting via 126421c527a3SYaowei Bai * /proc/sys/vm/compact_memory 126521c527a3SYaowei Bai */ 126621c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 126721c527a3SYaowei Bai { 126821c527a3SYaowei Bai return order == -1; 126921c527a3SYaowei Bai } 127021c527a3SYaowei Bai 1271ea7ab982SMichal Hocko static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc, 12726d7ce559SDavid Rientjes const int migratetype) 1273748446bbSMel Gorman { 12748fb74b9fSMel Gorman unsigned int order; 12755a03b051SAndrea Arcangeli unsigned long watermark; 127656de7263SMel Gorman 1277be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 12782d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1279748446bbSMel Gorman 1280753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1281f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 128255b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 128302333641SVlastimil Babka reset_cached_positions(zone); 128455b7c4c9SVlastimil Babka 128562997027SMel Gorman /* 128662997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1287accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 128862997027SMel Gorman * flag itself as the decision to be clear should be directly 128962997027SMel Gorman * based on an allocation request. 129062997027SMel Gorman */ 1291accf6242SVlastimil Babka if (cc->direct_compaction) 129262997027SMel Gorman zone->compact_blockskip_flush = true; 129362997027SMel Gorman 1294c8f7de0bSMichal Hocko if (cc->whole_zone) 1295748446bbSMel Gorman return COMPACT_COMPLETE; 1296c8f7de0bSMichal Hocko else 1297c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 1298bb13ffebSMel Gorman } 1299748446bbSMel Gorman 130021c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 130156de7263SMel Gorman return COMPACT_CONTINUE; 130256de7263SMel Gorman 13033957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 1304f2b8228cSVlastimil Babka watermark = zone->watermark[cc->alloc_flags & ALLOC_WMARK_MASK]; 13053957c776SMichal Hocko 1306ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1307ebff3980SVlastimil Babka cc->alloc_flags)) 13083957c776SMichal Hocko return COMPACT_CONTINUE; 13093957c776SMichal Hocko 131056de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 131156de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 13128fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 13132149cdaeSJoonsoo Kim bool can_steal; 13148fb74b9fSMel Gorman 131556de7263SMel Gorman /* Job done if page is free of the right migratetype */ 13166d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 1317cf378319SVlastimil Babka return COMPACT_SUCCESS; 131856de7263SMel Gorman 13192149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 13202149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 13212149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 13222149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 1323cf378319SVlastimil Babka return COMPACT_SUCCESS; 13242149cdaeSJoonsoo Kim #endif 13252149cdaeSJoonsoo Kim /* 13262149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 13272149cdaeSJoonsoo Kim * other migratetype buddy lists. 13282149cdaeSJoonsoo Kim */ 13292149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 13302149cdaeSJoonsoo Kim true, &can_steal) != -1) 1331cf378319SVlastimil Babka return COMPACT_SUCCESS; 133256de7263SMel Gorman } 133356de7263SMel Gorman 1334837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1335837d026dSJoonsoo Kim } 1336837d026dSJoonsoo Kim 1337ea7ab982SMichal Hocko static enum compact_result compact_finished(struct zone *zone, 1338ea7ab982SMichal Hocko struct compact_control *cc, 1339837d026dSJoonsoo Kim const int migratetype) 1340837d026dSJoonsoo Kim { 1341837d026dSJoonsoo Kim int ret; 1342837d026dSJoonsoo Kim 1343837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1344837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1345837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1346837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1347837d026dSJoonsoo Kim 1348837d026dSJoonsoo Kim return ret; 1349748446bbSMel Gorman } 1350748446bbSMel Gorman 13513e7d3449SMel Gorman /* 13523e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 13533e7d3449SMel Gorman * Returns 13543e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 1355cf378319SVlastimil Babka * COMPACT_SUCCESS - If the allocation would succeed without compaction 13563e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 13573e7d3449SMel Gorman */ 1358ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 1359c603844bSMel Gorman unsigned int alloc_flags, 136086a294a8SMichal Hocko int classzone_idx, 136186a294a8SMichal Hocko unsigned long wmark_target) 13623e7d3449SMel Gorman { 13633e7d3449SMel Gorman unsigned long watermark; 13643e7d3449SMel Gorman 136521c527a3SYaowei Bai if (is_via_compact_memory(order)) 13663957c776SMichal Hocko return COMPACT_CONTINUE; 13673957c776SMichal Hocko 1368f2b8228cSVlastimil Babka watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK]; 1369ebff3980SVlastimil Babka /* 1370ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1371ebff3980SVlastimil Babka * should be no need for compaction at all. 1372ebff3980SVlastimil Babka */ 1373ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1374ebff3980SVlastimil Babka alloc_flags)) 1375cf378319SVlastimil Babka return COMPACT_SUCCESS; 1376ebff3980SVlastimil Babka 13773957c776SMichal Hocko /* 13789861a62cSVlastimil Babka * Watermarks for order-0 must be met for compaction to be able to 1379984fdba6SVlastimil Babka * isolate free pages for migration targets. This means that the 1380984fdba6SVlastimil Babka * watermark and alloc_flags have to match, or be more pessimistic than 1381984fdba6SVlastimil Babka * the check in __isolate_free_page(). We don't use the direct 1382984fdba6SVlastimil Babka * compactor's alloc_flags, as they are not relevant for freepage 1383984fdba6SVlastimil Babka * isolation. We however do use the direct compactor's classzone_idx to 1384984fdba6SVlastimil Babka * skip over zones where lowmem reserves would prevent allocation even 1385984fdba6SVlastimil Babka * if compaction succeeds. 13868348faf9SVlastimil Babka * For costly orders, we require low watermark instead of min for 13878348faf9SVlastimil Babka * compaction to proceed to increase its chances. 1388984fdba6SVlastimil Babka * ALLOC_CMA is used, as pages in CMA pageblocks are considered 1389984fdba6SVlastimil Babka * suitable migration targets 13903e7d3449SMel Gorman */ 13918348faf9SVlastimil Babka watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? 13928348faf9SVlastimil Babka low_wmark_pages(zone) : min_wmark_pages(zone); 13938348faf9SVlastimil Babka watermark += compact_gap(order); 139486a294a8SMichal Hocko if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, 1395984fdba6SVlastimil Babka ALLOC_CMA, wmark_target)) 13963e7d3449SMel Gorman return COMPACT_SKIPPED; 13973e7d3449SMel Gorman 1398cc5c9f09SVlastimil Babka return COMPACT_CONTINUE; 1399cc5c9f09SVlastimil Babka } 1400cc5c9f09SVlastimil Babka 1401cc5c9f09SVlastimil Babka enum compact_result compaction_suitable(struct zone *zone, int order, 1402cc5c9f09SVlastimil Babka unsigned int alloc_flags, 1403cc5c9f09SVlastimil Babka int classzone_idx) 1404cc5c9f09SVlastimil Babka { 1405cc5c9f09SVlastimil Babka enum compact_result ret; 1406cc5c9f09SVlastimil Babka int fragindex; 1407cc5c9f09SVlastimil Babka 1408cc5c9f09SVlastimil Babka ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, 1409cc5c9f09SVlastimil Babka zone_page_state(zone, NR_FREE_PAGES)); 14103e7d3449SMel Gorman /* 14113e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 14123e7d3449SMel Gorman * low memory or external fragmentation 14133e7d3449SMel Gorman * 1414ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1415ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 14163e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 14173e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 14183e7d3449SMel Gorman * 141920311420SVlastimil Babka * Only compact if a failure would be due to fragmentation. Also 142020311420SVlastimil Babka * ignore fragindex for non-costly orders where the alternative to 142120311420SVlastimil Babka * a successful reclaim/compaction is OOM. Fragindex and the 142220311420SVlastimil Babka * vm.extfrag_threshold sysctl is meant as a heuristic to prevent 142320311420SVlastimil Babka * excessive compaction for costly orders, but it should not be at the 142420311420SVlastimil Babka * expense of system stability. 14253e7d3449SMel Gorman */ 142620311420SVlastimil Babka if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) { 14273e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 14283e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1429cc5c9f09SVlastimil Babka ret = COMPACT_NOT_SUITABLE_ZONE; 14303e7d3449SMel Gorman } 14313e7d3449SMel Gorman 1432837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1433837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1434837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1435837d026dSJoonsoo Kim 1436837d026dSJoonsoo Kim return ret; 1437837d026dSJoonsoo Kim } 1438837d026dSJoonsoo Kim 143986a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 144086a294a8SMichal Hocko int alloc_flags) 144186a294a8SMichal Hocko { 144286a294a8SMichal Hocko struct zone *zone; 144386a294a8SMichal Hocko struct zoneref *z; 144486a294a8SMichal Hocko 144586a294a8SMichal Hocko /* 144686a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 144786a294a8SMichal Hocko * retrying the reclaim. 144886a294a8SMichal Hocko */ 144986a294a8SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 145086a294a8SMichal Hocko ac->nodemask) { 145186a294a8SMichal Hocko unsigned long available; 145286a294a8SMichal Hocko enum compact_result compact_result; 145386a294a8SMichal Hocko 145486a294a8SMichal Hocko /* 145586a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 145686a294a8SMichal Hocko * want to trash just for a single high order allocation which 145786a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 145886a294a8SMichal Hocko * is happy about the watermark check. 145986a294a8SMichal Hocko */ 14605a1c84b4SMel Gorman available = zone_reclaimable_pages(zone) / order; 146186a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 146286a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 146386a294a8SMichal Hocko ac_classzone_idx(ac), available); 1464cc5c9f09SVlastimil Babka if (compact_result != COMPACT_SKIPPED) 146586a294a8SMichal Hocko return true; 146686a294a8SMichal Hocko } 146786a294a8SMichal Hocko 146886a294a8SMichal Hocko return false; 146986a294a8SMichal Hocko } 147086a294a8SMichal Hocko 1471ea7ab982SMichal Hocko static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc) 1472748446bbSMel Gorman { 1473ea7ab982SMichal Hocko enum compact_result ret; 1474c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1475108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 14766d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1477e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1478748446bbSMel Gorman 1479ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1480ebff3980SVlastimil Babka cc->classzone_idx); 14813e7d3449SMel Gorman /* Compaction is likely to fail */ 1482cf378319SVlastimil Babka if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) 14833e7d3449SMel Gorman return ret; 1484c46649deSMichal Hocko 1485c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 1486c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 14873e7d3449SMel Gorman 1488c89511abSMel Gorman /* 1489d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1490accf6242SVlastimil Babka * is about to be retried after being deferred. 1491d3132e4bSVlastimil Babka */ 1492accf6242SVlastimil Babka if (compaction_restarting(zone, cc->order)) 1493d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1494d3132e4bSVlastimil Babka 1495d3132e4bSVlastimil Babka /* 1496c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 149706ed2998SVlastimil Babka * information on where the scanners should start (unless we explicitly 149806ed2998SVlastimil Babka * want to compact the whole zone), but check that it is initialised 149906ed2998SVlastimil Babka * by ensuring the values are within zone boundaries. 1500c89511abSMel Gorman */ 150106ed2998SVlastimil Babka if (cc->whole_zone) { 150206ed2998SVlastimil Babka cc->migrate_pfn = start_pfn; 150306ed2998SVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 150406ed2998SVlastimil Babka } else { 1505e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1506c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1507623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 150806b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 1509c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1510c89511abSMel Gorman } 1511623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1512c89511abSMel Gorman cc->migrate_pfn = start_pfn; 151335979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 151435979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1515c89511abSMel Gorman } 1516c8f7de0bSMichal Hocko 1517c8f7de0bSMichal Hocko if (cc->migrate_pfn == start_pfn) 1518c8f7de0bSMichal Hocko cc->whole_zone = true; 151906ed2998SVlastimil Babka } 1520c8f7de0bSMichal Hocko 15211a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1522748446bbSMel Gorman 152316c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 152416c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 15250eb927c0SMel Gorman 1526748446bbSMel Gorman migrate_prep_local(); 1527748446bbSMel Gorman 15286d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 15296d7ce559SDavid Rientjes COMPACT_CONTINUE) { 15309d502c1cSMinchan Kim int err; 1531748446bbSMel Gorman 1532f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1533f9e35b3bSMel Gorman case ISOLATE_ABORT: 15342d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 15355733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1536e64c5237SShaohua Li cc->nr_migratepages = 0; 1537f9e35b3bSMel Gorman goto out; 1538f9e35b3bSMel Gorman case ISOLATE_NONE: 1539fdaf7f5cSVlastimil Babka /* 1540fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1541fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1542fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1543fdaf7f5cSVlastimil Babka */ 1544fdaf7f5cSVlastimil Babka goto check_drain; 1545f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1546f9e35b3bSMel Gorman ; 1547f9e35b3bSMel Gorman } 1548748446bbSMel Gorman 1549d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1550e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 15517b2a2d4aSMel Gorman MR_COMPACTION); 1552748446bbSMel Gorman 1553f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1554f8c9301fSVlastimil Babka &cc->migratepages); 1555748446bbSMel Gorman 1556f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1557f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 15589d502c1cSMinchan Kim if (err) { 15595733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 15607ed695e0SVlastimil Babka /* 15617ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 15627ed695e0SVlastimil Babka * and we want compact_finished() to detect it 15637ed695e0SVlastimil Babka */ 1564f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 15652d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 15664bf2bba3SDavid Rientjes goto out; 1567748446bbSMel Gorman } 1568fdd048e1SVlastimil Babka /* 1569fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 1570fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 1571fdd048e1SVlastimil Babka */ 1572fdd048e1SVlastimil Babka if (cc->direct_compaction && 1573fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 1574fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 1575fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 1576fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 1577fdd048e1SVlastimil Babka cc->last_migrated_pfn = 0; 1578fdd048e1SVlastimil Babka 1579fdd048e1SVlastimil Babka } 15804bf2bba3SDavid Rientjes } 1581fdaf7f5cSVlastimil Babka 1582fdaf7f5cSVlastimil Babka check_drain: 1583fdaf7f5cSVlastimil Babka /* 1584fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1585fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1586fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1587fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1588fdaf7f5cSVlastimil Babka * would succeed. 1589fdaf7f5cSVlastimil Babka */ 15901a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1591fdaf7f5cSVlastimil Babka int cpu; 1592fdaf7f5cSVlastimil Babka unsigned long current_block_start = 159306b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 1594fdaf7f5cSVlastimil Babka 15951a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1596fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1597fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1598fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1599fdaf7f5cSVlastimil Babka put_cpu(); 1600fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 16011a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1602fdaf7f5cSVlastimil Babka } 1603fdaf7f5cSVlastimil Babka } 1604fdaf7f5cSVlastimil Babka 1605748446bbSMel Gorman } 1606748446bbSMel Gorman 1607f9e35b3bSMel Gorman out: 16086bace090SVlastimil Babka /* 16096bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 16106bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 16116bace090SVlastimil Babka */ 16126bace090SVlastimil Babka if (cc->nr_freepages > 0) { 16136bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 16146bace090SVlastimil Babka 16156bace090SVlastimil Babka cc->nr_freepages = 0; 16166bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 16176bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 161806b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 16196bace090SVlastimil Babka /* 16206bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 16216bace090SVlastimil Babka * already reset to zone end in compact_finished() 16226bace090SVlastimil Babka */ 16236bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 16246bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 16256bace090SVlastimil Babka } 1626748446bbSMel Gorman 162716c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 162816c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 16290eb927c0SMel Gorman 1630748446bbSMel Gorman return ret; 1631748446bbSMel Gorman } 163276ab0f53SMel Gorman 1633ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 1634c3486f53SVlastimil Babka gfp_t gfp_mask, enum compact_priority prio, 1635c603844bSMel Gorman unsigned int alloc_flags, int classzone_idx) 163656de7263SMel Gorman { 1637ea7ab982SMichal Hocko enum compact_result ret; 163856de7263SMel Gorman struct compact_control cc = { 163956de7263SMel Gorman .nr_freepages = 0, 164056de7263SMel Gorman .nr_migratepages = 0, 164156de7263SMel Gorman .order = order, 16426d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 164356de7263SMel Gorman .zone = zone, 1644a5508cd8SVlastimil Babka .mode = (prio == COMPACT_PRIO_ASYNC) ? 1645a5508cd8SVlastimil Babka MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, 1646ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1647ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 1648accf6242SVlastimil Babka .direct_compaction = true, 1649a8e025e5SVlastimil Babka .whole_zone = (prio == MIN_COMPACT_PRIORITY), 16509f7e3387SVlastimil Babka .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), 16519f7e3387SVlastimil Babka .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) 165256de7263SMel Gorman }; 165356de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 165456de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 165556de7263SMel Gorman 1656e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1657e64c5237SShaohua Li 1658e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1659e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1660e64c5237SShaohua Li 1661e64c5237SShaohua Li return ret; 166256de7263SMel Gorman } 166356de7263SMel Gorman 16645e771905SMel Gorman int sysctl_extfrag_threshold = 500; 16655e771905SMel Gorman 166656de7263SMel Gorman /** 166756de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 166856de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 16691a6d53a1SVlastimil Babka * @order: The order of the current allocation 16701a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 16711a6d53a1SVlastimil Babka * @ac: The context of current allocation 1672e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 167356de7263SMel Gorman * 167456de7263SMel Gorman * This is the main entry point for direct page compaction. 167556de7263SMel Gorman */ 1676ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 1677c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 1678c3486f53SVlastimil Babka enum compact_priority prio) 167956de7263SMel Gorman { 168056de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 168156de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 168256de7263SMel Gorman struct zoneref *z; 168356de7263SMel Gorman struct zone *zone; 16841d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 168556de7263SMel Gorman 16864ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1687b2b331f9SGanesh Mahendran if (!may_enter_fs || !may_perform_io) 168853853e2dSVlastimil Babka return COMPACT_SKIPPED; 168956de7263SMel Gorman 1690a5508cd8SVlastimil Babka trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); 1691837d026dSJoonsoo Kim 169256de7263SMel Gorman /* Compact each zone in the list */ 16931a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 16941a6d53a1SVlastimil Babka ac->nodemask) { 1695ea7ab982SMichal Hocko enum compact_result status; 169656de7263SMel Gorman 1697a8e025e5SVlastimil Babka if (prio > MIN_COMPACT_PRIORITY 1698a8e025e5SVlastimil Babka && compaction_deferred(zone, order)) { 16991d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 170053853e2dSVlastimil Babka continue; 17011d4746d3SMichal Hocko } 170253853e2dSVlastimil Babka 1703a5508cd8SVlastimil Babka status = compact_zone_order(zone, order, gfp_mask, prio, 1704c3486f53SVlastimil Babka alloc_flags, ac_classzone_idx(ac)); 170556de7263SMel Gorman rc = max(status, rc); 170656de7263SMel Gorman 17077ceb009aSVlastimil Babka /* The allocation should succeed, stop compacting */ 17087ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 170953853e2dSVlastimil Babka /* 171053853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 171153853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 171253853e2dSVlastimil Babka * will repeat this with true if allocation indeed 171353853e2dSVlastimil Babka * succeeds in this zone. 171453853e2dSVlastimil Babka */ 171553853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 17161f9efdefSVlastimil Babka 1717c3486f53SVlastimil Babka break; 17181f9efdefSVlastimil Babka } 17191f9efdefSVlastimil Babka 1720a5508cd8SVlastimil Babka if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || 1721c3486f53SVlastimil Babka status == COMPACT_PARTIAL_SKIPPED)) 172253853e2dSVlastimil Babka /* 172353853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 172453853e2dSVlastimil Babka * so we defer compaction there. If it ends up 172553853e2dSVlastimil Babka * succeeding after all, it will be reset. 172653853e2dSVlastimil Babka */ 172753853e2dSVlastimil Babka defer_compaction(zone, order); 17281f9efdefSVlastimil Babka 17291f9efdefSVlastimil Babka /* 17301f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 17311f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 1732c3486f53SVlastimil Babka * case do not try further zones 17331f9efdefSVlastimil Babka */ 1734c3486f53SVlastimil Babka if ((prio == COMPACT_PRIO_ASYNC && need_resched()) 1735c3486f53SVlastimil Babka || fatal_signal_pending(current)) 17361f9efdefSVlastimil Babka break; 17371f9efdefSVlastimil Babka } 17381f9efdefSVlastimil Babka 173956de7263SMel Gorman return rc; 174056de7263SMel Gorman } 174156de7263SMel Gorman 174256de7263SMel Gorman 174376ab0f53SMel Gorman /* Compact all zones within a node */ 17447103f16dSAndrew Morton static void compact_node(int nid) 17457be62de9SRik van Riel { 1746791cae96SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1747791cae96SVlastimil Babka int zoneid; 1748791cae96SVlastimil Babka struct zone *zone; 17497be62de9SRik van Riel struct compact_control cc = { 17507be62de9SRik van Riel .order = -1, 1751e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 175291ca9186SDavid Rientjes .ignore_skip_hint = true, 175306ed2998SVlastimil Babka .whole_zone = true, 17547be62de9SRik van Riel }; 17557be62de9SRik van Riel 1756791cae96SVlastimil Babka 1757791cae96SVlastimil Babka for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 1758791cae96SVlastimil Babka 1759791cae96SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1760791cae96SVlastimil Babka if (!populated_zone(zone)) 1761791cae96SVlastimil Babka continue; 1762791cae96SVlastimil Babka 1763791cae96SVlastimil Babka cc.nr_freepages = 0; 1764791cae96SVlastimil Babka cc.nr_migratepages = 0; 1765791cae96SVlastimil Babka cc.zone = zone; 1766791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1767791cae96SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1768791cae96SVlastimil Babka 1769791cae96SVlastimil Babka compact_zone(zone, &cc); 1770791cae96SVlastimil Babka 1771791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1772791cae96SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1773791cae96SVlastimil Babka } 17747be62de9SRik van Riel } 17757be62de9SRik van Riel 177676ab0f53SMel Gorman /* Compact all nodes in the system */ 17777964c06dSJason Liu static void compact_nodes(void) 177876ab0f53SMel Gorman { 177976ab0f53SMel Gorman int nid; 178076ab0f53SMel Gorman 17818575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17828575ec29SHugh Dickins lru_add_drain_all(); 17838575ec29SHugh Dickins 178476ab0f53SMel Gorman for_each_online_node(nid) 178576ab0f53SMel Gorman compact_node(nid); 178676ab0f53SMel Gorman } 178776ab0f53SMel Gorman 178876ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 178976ab0f53SMel Gorman int sysctl_compact_memory; 179076ab0f53SMel Gorman 1791fec4eb2cSYaowei Bai /* 1792fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1793fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1794fec4eb2cSYaowei Bai */ 179576ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 179676ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 179776ab0f53SMel Gorman { 179876ab0f53SMel Gorman if (write) 17997964c06dSJason Liu compact_nodes(); 180076ab0f53SMel Gorman 180176ab0f53SMel Gorman return 0; 180276ab0f53SMel Gorman } 1803ed4a6d7fSMel Gorman 18045e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 18055e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 18065e771905SMel Gorman { 18075e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 18085e771905SMel Gorman 18095e771905SMel Gorman return 0; 18105e771905SMel Gorman } 18115e771905SMel Gorman 1812ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 181374e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 181410fbcf4cSKay Sievers struct device_attribute *attr, 1815ed4a6d7fSMel Gorman const char *buf, size_t count) 1816ed4a6d7fSMel Gorman { 18178575ec29SHugh Dickins int nid = dev->id; 18188575ec29SHugh Dickins 18198575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 18208575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 18218575ec29SHugh Dickins lru_add_drain_all(); 18228575ec29SHugh Dickins 18238575ec29SHugh Dickins compact_node(nid); 18248575ec29SHugh Dickins } 1825ed4a6d7fSMel Gorman 1826ed4a6d7fSMel Gorman return count; 1827ed4a6d7fSMel Gorman } 182810fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1829ed4a6d7fSMel Gorman 1830ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1831ed4a6d7fSMel Gorman { 183210fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1833ed4a6d7fSMel Gorman } 1834ed4a6d7fSMel Gorman 1835ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1836ed4a6d7fSMel Gorman { 183710fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1838ed4a6d7fSMel Gorman } 1839ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1840ff9543fdSMichal Nazarewicz 1841698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 1842698b1b30SVlastimil Babka { 1843172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 1844698b1b30SVlastimil Babka } 1845698b1b30SVlastimil Babka 1846698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 1847698b1b30SVlastimil Babka { 1848698b1b30SVlastimil Babka int zoneid; 1849698b1b30SVlastimil Babka struct zone *zone; 1850698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 1851698b1b30SVlastimil Babka 18526cd9dc3eSChen Feng for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { 1853698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1854698b1b30SVlastimil Babka 1855698b1b30SVlastimil Babka if (!populated_zone(zone)) 1856698b1b30SVlastimil Babka continue; 1857698b1b30SVlastimil Babka 1858698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 1859698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 1860698b1b30SVlastimil Babka return true; 1861698b1b30SVlastimil Babka } 1862698b1b30SVlastimil Babka 1863698b1b30SVlastimil Babka return false; 1864698b1b30SVlastimil Babka } 1865698b1b30SVlastimil Babka 1866698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 1867698b1b30SVlastimil Babka { 1868698b1b30SVlastimil Babka /* 1869698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 1870698b1b30SVlastimil Babka * order is allocatable. 1871698b1b30SVlastimil Babka */ 1872698b1b30SVlastimil Babka int zoneid; 1873698b1b30SVlastimil Babka struct zone *zone; 1874698b1b30SVlastimil Babka struct compact_control cc = { 1875698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 1876698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 1877698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 1878698b1b30SVlastimil Babka .ignore_skip_hint = true, 1879698b1b30SVlastimil Babka 1880698b1b30SVlastimil Babka }; 1881698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 1882698b1b30SVlastimil Babka cc.classzone_idx); 1883698b1b30SVlastimil Babka count_vm_event(KCOMPACTD_WAKE); 1884698b1b30SVlastimil Babka 18856cd9dc3eSChen Feng for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { 1886698b1b30SVlastimil Babka int status; 1887698b1b30SVlastimil Babka 1888698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1889698b1b30SVlastimil Babka if (!populated_zone(zone)) 1890698b1b30SVlastimil Babka continue; 1891698b1b30SVlastimil Babka 1892698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 1893698b1b30SVlastimil Babka continue; 1894698b1b30SVlastimil Babka 1895698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 1896698b1b30SVlastimil Babka COMPACT_CONTINUE) 1897698b1b30SVlastimil Babka continue; 1898698b1b30SVlastimil Babka 1899698b1b30SVlastimil Babka cc.nr_freepages = 0; 1900698b1b30SVlastimil Babka cc.nr_migratepages = 0; 1901698b1b30SVlastimil Babka cc.zone = zone; 1902698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1903698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1904698b1b30SVlastimil Babka 1905172400c6SVlastimil Babka if (kthread_should_stop()) 1906172400c6SVlastimil Babka return; 1907698b1b30SVlastimil Babka status = compact_zone(zone, &cc); 1908698b1b30SVlastimil Babka 19097ceb009aSVlastimil Babka if (status == COMPACT_SUCCESS) { 1910698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 1911c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 1912698b1b30SVlastimil Babka /* 1913698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 1914698b1b30SVlastimil Babka * sync direct compaction does. 1915698b1b30SVlastimil Babka */ 1916698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 1917698b1b30SVlastimil Babka } 1918698b1b30SVlastimil Babka 1919698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1920698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1921698b1b30SVlastimil Babka } 1922698b1b30SVlastimil Babka 1923698b1b30SVlastimil Babka /* 1924698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 1925698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 1926698b1b30SVlastimil Babka * our current ones 1927698b1b30SVlastimil Babka */ 1928698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 1929698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1930698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 1931698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1932698b1b30SVlastimil Babka } 1933698b1b30SVlastimil Babka 1934698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 1935698b1b30SVlastimil Babka { 1936698b1b30SVlastimil Babka if (!order) 1937698b1b30SVlastimil Babka return; 1938698b1b30SVlastimil Babka 1939698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 1940698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 1941698b1b30SVlastimil Babka 1942698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 1943698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 1944698b1b30SVlastimil Babka 1945698b1b30SVlastimil Babka if (!waitqueue_active(&pgdat->kcompactd_wait)) 1946698b1b30SVlastimil Babka return; 1947698b1b30SVlastimil Babka 1948698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 1949698b1b30SVlastimil Babka return; 1950698b1b30SVlastimil Babka 1951698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 1952698b1b30SVlastimil Babka classzone_idx); 1953698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 1954698b1b30SVlastimil Babka } 1955698b1b30SVlastimil Babka 1956698b1b30SVlastimil Babka /* 1957698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 1958698b1b30SVlastimil Babka * from the init process. 1959698b1b30SVlastimil Babka */ 1960698b1b30SVlastimil Babka static int kcompactd(void *p) 1961698b1b30SVlastimil Babka { 1962698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 1963698b1b30SVlastimil Babka struct task_struct *tsk = current; 1964698b1b30SVlastimil Babka 1965698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 1966698b1b30SVlastimil Babka 1967698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 1968698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 1969698b1b30SVlastimil Babka 1970698b1b30SVlastimil Babka set_freezable(); 1971698b1b30SVlastimil Babka 1972698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1973698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1974698b1b30SVlastimil Babka 1975698b1b30SVlastimil Babka while (!kthread_should_stop()) { 1976698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 1977698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 1978698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 1979698b1b30SVlastimil Babka 1980698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 1981698b1b30SVlastimil Babka } 1982698b1b30SVlastimil Babka 1983698b1b30SVlastimil Babka return 0; 1984698b1b30SVlastimil Babka } 1985698b1b30SVlastimil Babka 1986698b1b30SVlastimil Babka /* 1987698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 1988698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 1989698b1b30SVlastimil Babka */ 1990698b1b30SVlastimil Babka int kcompactd_run(int nid) 1991698b1b30SVlastimil Babka { 1992698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1993698b1b30SVlastimil Babka int ret = 0; 1994698b1b30SVlastimil Babka 1995698b1b30SVlastimil Babka if (pgdat->kcompactd) 1996698b1b30SVlastimil Babka return 0; 1997698b1b30SVlastimil Babka 1998698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 1999698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 2000698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 2001698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 2002698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 2003698b1b30SVlastimil Babka } 2004698b1b30SVlastimil Babka return ret; 2005698b1b30SVlastimil Babka } 2006698b1b30SVlastimil Babka 2007698b1b30SVlastimil Babka /* 2008698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 2009698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 2010698b1b30SVlastimil Babka */ 2011698b1b30SVlastimil Babka void kcompactd_stop(int nid) 2012698b1b30SVlastimil Babka { 2013698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 2014698b1b30SVlastimil Babka 2015698b1b30SVlastimil Babka if (kcompactd) { 2016698b1b30SVlastimil Babka kthread_stop(kcompactd); 2017698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 2018698b1b30SVlastimil Babka } 2019698b1b30SVlastimil Babka } 2020698b1b30SVlastimil Babka 2021698b1b30SVlastimil Babka /* 2022698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 2023698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 2024698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 2025698b1b30SVlastimil Babka * restore their cpu bindings. 2026698b1b30SVlastimil Babka */ 2027698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action, 2028698b1b30SVlastimil Babka void *hcpu) 2029698b1b30SVlastimil Babka { 2030698b1b30SVlastimil Babka int nid; 2031698b1b30SVlastimil Babka 2032698b1b30SVlastimil Babka if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 2033698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 2034698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2035698b1b30SVlastimil Babka const struct cpumask *mask; 2036698b1b30SVlastimil Babka 2037698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 2038698b1b30SVlastimil Babka 2039698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 2040698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 2041698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 2042698b1b30SVlastimil Babka } 2043698b1b30SVlastimil Babka } 2044698b1b30SVlastimil Babka return NOTIFY_OK; 2045698b1b30SVlastimil Babka } 2046698b1b30SVlastimil Babka 2047698b1b30SVlastimil Babka static int __init kcompactd_init(void) 2048698b1b30SVlastimil Babka { 2049698b1b30SVlastimil Babka int nid; 2050698b1b30SVlastimil Babka 2051698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 2052698b1b30SVlastimil Babka kcompactd_run(nid); 2053698b1b30SVlastimil Babka hotcpu_notifier(cpu_callback, 0); 2054698b1b30SVlastimil Babka return 0; 2055698b1b30SVlastimil Babka } 2056698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 2057698b1b30SVlastimil Babka 2058ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 2059