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> 18bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h> 19194159fbSMinchan Kim #include <linux/page-isolation.h> 20b8c73fc2SAndrey Ryabinin #include <linux/kasan.h> 21698b1b30SVlastimil Babka #include <linux/kthread.h> 22698b1b30SVlastimil Babka #include <linux/freezer.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 { 68ff9543fdSMichal Nazarewicz struct page *page; 69ff9543fdSMichal Nazarewicz 70ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 71ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 72ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 73b8c73fc2SAndrey Ryabinin kasan_alloc_pages(page, 0); 74ff9543fdSMichal Nazarewicz } 75ff9543fdSMichal Nazarewicz } 76ff9543fdSMichal Nazarewicz 7747118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7847118af0SMichal Nazarewicz { 7947118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 8047118af0SMichal Nazarewicz } 8147118af0SMichal Nazarewicz 82bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 8324e2716fSJoonsoo Kim 8424e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 8524e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 8624e2716fSJoonsoo Kim 8724e2716fSJoonsoo Kim /* 8824e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 8924e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 9024e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 9124e2716fSJoonsoo Kim */ 9224e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 9324e2716fSJoonsoo Kim { 9424e2716fSJoonsoo Kim zone->compact_considered = 0; 9524e2716fSJoonsoo Kim zone->compact_defer_shift++; 9624e2716fSJoonsoo Kim 9724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 9824e2716fSJoonsoo Kim zone->compact_order_failed = order; 9924e2716fSJoonsoo Kim 10024e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 10124e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 10224e2716fSJoonsoo Kim 10324e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 10424e2716fSJoonsoo Kim } 10524e2716fSJoonsoo Kim 10624e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 10724e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 10824e2716fSJoonsoo Kim { 10924e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 11024e2716fSJoonsoo Kim 11124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 11224e2716fSJoonsoo Kim return false; 11324e2716fSJoonsoo Kim 11424e2716fSJoonsoo Kim /* Avoid possible overflow */ 11524e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 11624e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 11724e2716fSJoonsoo Kim 11824e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 11924e2716fSJoonsoo Kim return false; 12024e2716fSJoonsoo Kim 12124e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 12224e2716fSJoonsoo Kim 12324e2716fSJoonsoo Kim return true; 12424e2716fSJoonsoo Kim } 12524e2716fSJoonsoo Kim 12624e2716fSJoonsoo Kim /* 12724e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 12824e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 12924e2716fSJoonsoo Kim * expected to succeed. 13024e2716fSJoonsoo Kim */ 13124e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 13224e2716fSJoonsoo Kim bool alloc_success) 13324e2716fSJoonsoo Kim { 13424e2716fSJoonsoo Kim if (alloc_success) { 13524e2716fSJoonsoo Kim zone->compact_considered = 0; 13624e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 13724e2716fSJoonsoo Kim } 13824e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 13924e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 14024e2716fSJoonsoo Kim 14124e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 14224e2716fSJoonsoo Kim } 14324e2716fSJoonsoo Kim 14424e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 14524e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 14624e2716fSJoonsoo Kim { 14724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14824e2716fSJoonsoo Kim return false; 14924e2716fSJoonsoo Kim 15024e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 15124e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 15224e2716fSJoonsoo Kim } 15324e2716fSJoonsoo Kim 154bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 155bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 156bb13ffebSMel Gorman struct page *page) 157bb13ffebSMel Gorman { 158bb13ffebSMel Gorman if (cc->ignore_skip_hint) 159bb13ffebSMel Gorman return true; 160bb13ffebSMel Gorman 161bb13ffebSMel Gorman return !get_pageblock_skip(page); 162bb13ffebSMel Gorman } 163bb13ffebSMel Gorman 16402333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 16502333641SVlastimil Babka { 16602333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 16702333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 168623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 16906b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 17002333641SVlastimil Babka } 17102333641SVlastimil Babka 172bb13ffebSMel Gorman /* 173bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 174bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 175bb13ffebSMel Gorman * meet. 176bb13ffebSMel Gorman */ 17762997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 178bb13ffebSMel Gorman { 179bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 180108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 181bb13ffebSMel Gorman unsigned long pfn; 182bb13ffebSMel Gorman 18362997027SMel Gorman zone->compact_blockskip_flush = false; 184bb13ffebSMel Gorman 185bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 186bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 187bb13ffebSMel Gorman struct page *page; 188bb13ffebSMel Gorman 189bb13ffebSMel Gorman cond_resched(); 190bb13ffebSMel Gorman 191bb13ffebSMel Gorman if (!pfn_valid(pfn)) 192bb13ffebSMel Gorman continue; 193bb13ffebSMel Gorman 194bb13ffebSMel Gorman page = pfn_to_page(pfn); 195bb13ffebSMel Gorman if (zone != page_zone(page)) 196bb13ffebSMel Gorman continue; 197bb13ffebSMel Gorman 198bb13ffebSMel Gorman clear_pageblock_skip(page); 199bb13ffebSMel Gorman } 20002333641SVlastimil Babka 20102333641SVlastimil Babka reset_cached_positions(zone); 202bb13ffebSMel Gorman } 203bb13ffebSMel Gorman 20462997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 20562997027SMel Gorman { 20662997027SMel Gorman int zoneid; 20762997027SMel Gorman 20862997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 20962997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 21062997027SMel Gorman if (!populated_zone(zone)) 21162997027SMel Gorman continue; 21262997027SMel Gorman 21362997027SMel Gorman /* Only flush if a full compaction finished recently */ 21462997027SMel Gorman if (zone->compact_blockskip_flush) 21562997027SMel Gorman __reset_isolation_suitable(zone); 21662997027SMel Gorman } 21762997027SMel Gorman } 21862997027SMel Gorman 219bb13ffebSMel Gorman /* 220bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 22162997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 222bb13ffebSMel Gorman */ 223c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 224c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 225edc2ca61SVlastimil Babka bool migrate_scanner) 226bb13ffebSMel Gorman { 227c89511abSMel Gorman struct zone *zone = cc->zone; 22835979ef3SDavid Rientjes unsigned long pfn; 2296815bf3fSJoonsoo Kim 2306815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2316815bf3fSJoonsoo Kim return; 2326815bf3fSJoonsoo Kim 233bb13ffebSMel Gorman if (!page) 234bb13ffebSMel Gorman return; 235bb13ffebSMel Gorman 23635979ef3SDavid Rientjes if (nr_isolated) 23735979ef3SDavid Rientjes return; 23835979ef3SDavid Rientjes 239bb13ffebSMel Gorman set_pageblock_skip(page); 240c89511abSMel Gorman 24135979ef3SDavid Rientjes pfn = page_to_pfn(page); 24235979ef3SDavid Rientjes 24335979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 244c89511abSMel Gorman if (migrate_scanner) { 24535979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 24635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 247e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 248e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 24935979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 250c89511abSMel Gorman } else { 25135979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 252c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 253c89511abSMel Gorman } 254c89511abSMel Gorman } 255bb13ffebSMel Gorman #else 256bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 257bb13ffebSMel Gorman struct page *page) 258bb13ffebSMel Gorman { 259bb13ffebSMel Gorman return true; 260bb13ffebSMel Gorman } 261bb13ffebSMel Gorman 262c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 263c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 264edc2ca61SVlastimil Babka bool migrate_scanner) 265bb13ffebSMel Gorman { 266bb13ffebSMel Gorman } 267bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 268bb13ffebSMel Gorman 2691f9efdefSVlastimil Babka /* 2708b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 2718b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 2728b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 2738b44d279SVlastimil Babka * 2748b44d279SVlastimil Babka * Returns true if the lock is held 2758b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 2761f9efdefSVlastimil Babka */ 2778b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 2788b44d279SVlastimil Babka struct compact_control *cc) 2798b44d279SVlastimil Babka { 2808b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 2818b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 2828b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 2838b44d279SVlastimil Babka return false; 2848b44d279SVlastimil Babka } 2858b44d279SVlastimil Babka } else { 2868b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 2878b44d279SVlastimil Babka } 2881f9efdefSVlastimil Babka 2898b44d279SVlastimil Babka return true; 2902a1402aaSMel Gorman } 2912a1402aaSMel Gorman 29285aa125fSMichal Nazarewicz /* 293c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 2948b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 2958b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 2968b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 2978b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 2988b44d279SVlastimil Babka * aborts. Sync compaction schedules. 2998b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 3008b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 301c67fe375SMel Gorman * 3028b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 3038b44d279SVlastimil Babka * async compaction due to need_resched() 3048b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3058b44d279SVlastimil Babka * scheduled) 306c67fe375SMel Gorman */ 3078b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3088b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 309c67fe375SMel Gorman { 3108b44d279SVlastimil Babka if (*locked) { 3118b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3128b44d279SVlastimil Babka *locked = false; 313c67fe375SMel Gorman } 314c67fe375SMel Gorman 3158b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 3168b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3178b44d279SVlastimil Babka return true; 3188b44d279SVlastimil Babka } 3198b44d279SVlastimil Babka 3208b44d279SVlastimil Babka if (need_resched()) { 321e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 3228b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3238b44d279SVlastimil Babka return true; 324c67fe375SMel Gorman } 325c67fe375SMel Gorman cond_resched(); 326c67fe375SMel Gorman } 327c67fe375SMel Gorman 3288b44d279SVlastimil Babka return false; 329c67fe375SMel Gorman } 330c67fe375SMel Gorman 331be976572SVlastimil Babka /* 332be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 333be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3348b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 335be976572SVlastimil Babka * is used where no lock is concerned. 336be976572SVlastimil Babka * 337be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 338be976572SVlastimil Babka * Returns true when async compaction should abort. 339be976572SVlastimil Babka */ 340be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 341be976572SVlastimil Babka { 342be976572SVlastimil Babka /* async compaction aborts if contended */ 343be976572SVlastimil Babka if (need_resched()) { 344be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3451f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 346be976572SVlastimil Babka return true; 347be976572SVlastimil Babka } 348be976572SVlastimil Babka 349be976572SVlastimil Babka cond_resched(); 350be976572SVlastimil Babka } 351be976572SVlastimil Babka 352be976572SVlastimil Babka return false; 353be976572SVlastimil Babka } 354be976572SVlastimil Babka 355c67fe375SMel Gorman /* 3569e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3579e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3589e4be470SJerome Marchand * (even though it may still end up isolating some pages). 35985aa125fSMichal Nazarewicz */ 360f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 361e14c720eSVlastimil Babka unsigned long *start_pfn, 36285aa125fSMichal Nazarewicz unsigned long end_pfn, 36385aa125fSMichal Nazarewicz struct list_head *freelist, 36485aa125fSMichal Nazarewicz bool strict) 365748446bbSMel Gorman { 366b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 367bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 368b8b2d825SXiubo Li unsigned long flags = 0; 369f40d1e42SMel Gorman bool locked = false; 370e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 371748446bbSMel Gorman 372748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 373748446bbSMel Gorman 374f40d1e42SMel Gorman /* Isolate free pages. */ 375748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 376748446bbSMel Gorman int isolated, i; 377748446bbSMel Gorman struct page *page = cursor; 378748446bbSMel Gorman 3798b44d279SVlastimil Babka /* 3808b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 3818b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 3828b44d279SVlastimil Babka * pending or async compaction detects need_resched() 3838b44d279SVlastimil Babka */ 3848b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 3858b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 3868b44d279SVlastimil Babka &locked, cc)) 3878b44d279SVlastimil Babka break; 3888b44d279SVlastimil Babka 389b7aba698SMel Gorman nr_scanned++; 390f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 3912af120bcSLaura Abbott goto isolate_fail; 3922af120bcSLaura Abbott 393bb13ffebSMel Gorman if (!valid_page) 394bb13ffebSMel Gorman valid_page = page; 3959fcd6d2eSVlastimil Babka 3969fcd6d2eSVlastimil Babka /* 3979fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 3989fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 3999fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 4009fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 4019fcd6d2eSVlastimil Babka */ 4029fcd6d2eSVlastimil Babka if (PageCompound(page)) { 4039fcd6d2eSVlastimil Babka unsigned int comp_order = compound_order(page); 4049fcd6d2eSVlastimil Babka 4059fcd6d2eSVlastimil Babka if (likely(comp_order < MAX_ORDER)) { 4069fcd6d2eSVlastimil Babka blockpfn += (1UL << comp_order) - 1; 4079fcd6d2eSVlastimil Babka cursor += (1UL << comp_order) - 1; 4089fcd6d2eSVlastimil Babka } 4099fcd6d2eSVlastimil Babka 4109fcd6d2eSVlastimil Babka goto isolate_fail; 4119fcd6d2eSVlastimil Babka } 4129fcd6d2eSVlastimil Babka 413f40d1e42SMel Gorman if (!PageBuddy(page)) 4142af120bcSLaura Abbott goto isolate_fail; 415f40d1e42SMel Gorman 416f40d1e42SMel Gorman /* 41769b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 41869b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 41969b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 42069b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 42169b7189fSVlastimil Babka * recheck as well. 42269b7189fSVlastimil Babka */ 42369b7189fSVlastimil Babka if (!locked) { 42469b7189fSVlastimil Babka /* 425f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 426f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 427f40d1e42SMel Gorman * heavily contended if there are parallel allocations 428f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 429f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 430f40d1e42SMel Gorman * possible. 431f40d1e42SMel Gorman */ 4328b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4338b44d279SVlastimil Babka &flags, cc); 434f40d1e42SMel Gorman if (!locked) 435f40d1e42SMel Gorman break; 436f40d1e42SMel Gorman 437f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 438f40d1e42SMel Gorman if (!PageBuddy(page)) 4392af120bcSLaura Abbott goto isolate_fail; 44069b7189fSVlastimil Babka } 441748446bbSMel Gorman 442748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 443748446bbSMel Gorman isolated = split_free_page(page); 444a4f04f2cSDavid Rientjes if (!isolated) 445a4f04f2cSDavid Rientjes break; 446a4f04f2cSDavid Rientjes 447748446bbSMel Gorman total_isolated += isolated; 448a4f04f2cSDavid Rientjes cc->nr_freepages += isolated; 449748446bbSMel Gorman for (i = 0; i < isolated; i++) { 450748446bbSMel Gorman list_add(&page->lru, freelist); 451748446bbSMel Gorman page++; 452748446bbSMel Gorman } 453a4f04f2cSDavid Rientjes if (!strict && cc->nr_migratepages <= cc->nr_freepages) { 454932ff6bbSJoonsoo Kim blockpfn += isolated; 455932ff6bbSJoonsoo Kim break; 456932ff6bbSJoonsoo Kim } 457a4f04f2cSDavid Rientjes /* Advance to the end of split page */ 458748446bbSMel Gorman blockpfn += isolated - 1; 459748446bbSMel Gorman cursor += isolated - 1; 4602af120bcSLaura Abbott continue; 4612af120bcSLaura Abbott 4622af120bcSLaura Abbott isolate_fail: 4632af120bcSLaura Abbott if (strict) 4642af120bcSLaura Abbott break; 4652af120bcSLaura Abbott else 4662af120bcSLaura Abbott continue; 4672af120bcSLaura Abbott 468748446bbSMel Gorman } 469748446bbSMel Gorman 470a4f04f2cSDavid Rientjes if (locked) 471a4f04f2cSDavid Rientjes spin_unlock_irqrestore(&cc->zone->lock, flags); 472a4f04f2cSDavid Rientjes 4739fcd6d2eSVlastimil Babka /* 4749fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 4759fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 4769fcd6d2eSVlastimil Babka */ 4779fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 4789fcd6d2eSVlastimil Babka blockpfn = end_pfn; 4799fcd6d2eSVlastimil Babka 480e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 481e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 482e34d85f0SJoonsoo Kim 483e14c720eSVlastimil Babka /* Record how far we have got within the block */ 484e14c720eSVlastimil Babka *start_pfn = blockpfn; 485e14c720eSVlastimil Babka 486f40d1e42SMel Gorman /* 487f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 488f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 489f40d1e42SMel Gorman * returned and CMA will fail. 490f40d1e42SMel Gorman */ 4912af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 492f40d1e42SMel Gorman total_isolated = 0; 493f40d1e42SMel Gorman 494bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 495bb13ffebSMel Gorman if (blockpfn == end_pfn) 496edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 497bb13ffebSMel Gorman 498010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 499397487dbSMel Gorman if (total_isolated) 500010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 501748446bbSMel Gorman return total_isolated; 502748446bbSMel Gorman } 503748446bbSMel Gorman 50485aa125fSMichal Nazarewicz /** 50585aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 50685aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 50785aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 50885aa125fSMichal Nazarewicz * 50985aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 51085aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 51185aa125fSMichal Nazarewicz * undo its actions and return zero. 51285aa125fSMichal Nazarewicz * 51385aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 51485aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 51585aa125fSMichal Nazarewicz * a free page). 51685aa125fSMichal Nazarewicz */ 517ff9543fdSMichal Nazarewicz unsigned long 518bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 519bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 52085aa125fSMichal Nazarewicz { 521e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 52285aa125fSMichal Nazarewicz LIST_HEAD(freelist); 52385aa125fSMichal Nazarewicz 5247d49d886SVlastimil Babka pfn = start_pfn; 52506b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 526e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 527e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 52806b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 5297d49d886SVlastimil Babka 5307d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 531e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 5327d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 533e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 534e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5357d49d886SVlastimil Babka 53685aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 53785aa125fSMichal Nazarewicz 53858420016SJoonsoo Kim /* 53958420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 54058420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 54158420016SJoonsoo Kim * scanning range to right one. 54258420016SJoonsoo Kim */ 54358420016SJoonsoo Kim if (pfn >= block_end_pfn) { 54406b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 54506b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 54658420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 54758420016SJoonsoo Kim } 54858420016SJoonsoo Kim 549e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 550e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 5517d49d886SVlastimil Babka break; 5527d49d886SVlastimil Babka 553e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 554e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 55585aa125fSMichal Nazarewicz 55685aa125fSMichal Nazarewicz /* 55785aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 55885aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 55985aa125fSMichal Nazarewicz * non-free pages). 56085aa125fSMichal Nazarewicz */ 56185aa125fSMichal Nazarewicz if (!isolated) 56285aa125fSMichal Nazarewicz break; 56385aa125fSMichal Nazarewicz 56485aa125fSMichal Nazarewicz /* 56585aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 56685aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 56785aa125fSMichal Nazarewicz * page may span two pageblocks). 56885aa125fSMichal Nazarewicz */ 56985aa125fSMichal Nazarewicz } 57085aa125fSMichal Nazarewicz 57185aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 57285aa125fSMichal Nazarewicz map_pages(&freelist); 57385aa125fSMichal Nazarewicz 57485aa125fSMichal Nazarewicz if (pfn < end_pfn) { 57585aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 57685aa125fSMichal Nazarewicz release_freepages(&freelist); 57785aa125fSMichal Nazarewicz return 0; 57885aa125fSMichal Nazarewicz } 57985aa125fSMichal Nazarewicz 58085aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 58185aa125fSMichal Nazarewicz return pfn; 58285aa125fSMichal Nazarewicz } 58385aa125fSMichal Nazarewicz 584748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 585edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 586748446bbSMel Gorman { 587748446bbSMel Gorman struct page *page; 588b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 589748446bbSMel Gorman 590edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 591edc2ca61SVlastimil Babka return; 592edc2ca61SVlastimil Babka 593b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 594b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 595748446bbSMel Gorman 596c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 597c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 598c67fe375SMel Gorman } 599748446bbSMel Gorman 600748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 601748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 602748446bbSMel Gorman { 603bc693045SMinchan Kim unsigned long active, inactive, isolated; 604748446bbSMel Gorman 605748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 606748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 607bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 608bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 609748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 610748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 611748446bbSMel Gorman 612bc693045SMinchan Kim return isolated > (inactive + active) / 2; 613748446bbSMel Gorman } 614748446bbSMel Gorman 6152fe86e00SMichal Nazarewicz /** 616edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 617edc2ca61SVlastimil Babka * a single pageblock 6182fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 619edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 620edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 621edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6222fe86e00SMichal Nazarewicz * 6232fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 624edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 625edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 626edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 627edc2ca61SVlastimil Babka * than end_pfn). 6282fe86e00SMichal Nazarewicz * 629edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 630edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 631edc2ca61SVlastimil Babka * is neither read nor updated. 632748446bbSMel Gorman */ 633edc2ca61SVlastimil Babka static unsigned long 634edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 635edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 636748446bbSMel Gorman { 637edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 638b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 639fa9add64SHugh Dickins struct lruvec *lruvec; 640b8b2d825SXiubo Li unsigned long flags = 0; 6412a1402aaSMel Gorman bool locked = false; 642bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 643e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 644fdd048e1SVlastimil Babka bool skip_on_failure = false; 645fdd048e1SVlastimil Babka unsigned long next_skip_pfn = 0; 646748446bbSMel Gorman 647748446bbSMel Gorman /* 648748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 649748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 650748446bbSMel Gorman * delay for some time until fewer pages are isolated 651748446bbSMel Gorman */ 652748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 653f9e35b3bSMel Gorman /* async migration should just abort */ 654e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6552fe86e00SMichal Nazarewicz return 0; 656f9e35b3bSMel Gorman 657748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 658748446bbSMel Gorman 659748446bbSMel Gorman if (fatal_signal_pending(current)) 6602fe86e00SMichal Nazarewicz return 0; 661748446bbSMel Gorman } 662748446bbSMel Gorman 663be976572SVlastimil Babka if (compact_should_abort(cc)) 664aeef4b83SDavid Rientjes return 0; 665aeef4b83SDavid Rientjes 666fdd048e1SVlastimil Babka if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { 667fdd048e1SVlastimil Babka skip_on_failure = true; 668fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 669fdd048e1SVlastimil Babka } 670fdd048e1SVlastimil Babka 671748446bbSMel Gorman /* Time to isolate some pages for migration */ 672748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 67329c0dde8SVlastimil Babka bool is_lru; 67429c0dde8SVlastimil Babka 675fdd048e1SVlastimil Babka if (skip_on_failure && low_pfn >= next_skip_pfn) { 676fdd048e1SVlastimil Babka /* 677fdd048e1SVlastimil Babka * We have isolated all migration candidates in the 678fdd048e1SVlastimil Babka * previous order-aligned block, and did not skip it due 679fdd048e1SVlastimil Babka * to failure. We should migrate the pages now and 680fdd048e1SVlastimil Babka * hopefully succeed compaction. 681fdd048e1SVlastimil Babka */ 682fdd048e1SVlastimil Babka if (nr_isolated) 683fdd048e1SVlastimil Babka break; 684fdd048e1SVlastimil Babka 685fdd048e1SVlastimil Babka /* 686fdd048e1SVlastimil Babka * We failed to isolate in the previous order-aligned 687fdd048e1SVlastimil Babka * block. Set the new boundary to the end of the 688fdd048e1SVlastimil Babka * current block. Note we can't simply increase 689fdd048e1SVlastimil Babka * next_skip_pfn by 1 << order, as low_pfn might have 690fdd048e1SVlastimil Babka * been incremented by a higher number due to skipping 691fdd048e1SVlastimil Babka * a compound or a high-order buddy page in the 692fdd048e1SVlastimil Babka * previous loop iteration. 693fdd048e1SVlastimil Babka */ 694fdd048e1SVlastimil Babka next_skip_pfn = block_end_pfn(low_pfn, cc->order); 695fdd048e1SVlastimil Babka } 696fdd048e1SVlastimil Babka 6978b44d279SVlastimil Babka /* 6988b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 6998b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7008b44d279SVlastimil Babka * if contended. 7018b44d279SVlastimil Babka */ 7028b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 7038b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 7048b44d279SVlastimil Babka &locked, cc)) 7058b44d279SVlastimil Babka break; 706b2eef8c0SAndrea Arcangeli 707748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 708fdd048e1SVlastimil Babka goto isolate_fail; 709b7aba698SMel Gorman nr_scanned++; 710748446bbSMel Gorman 711748446bbSMel Gorman page = pfn_to_page(low_pfn); 712dc908600SMel Gorman 713bb13ffebSMel Gorman if (!valid_page) 714bb13ffebSMel Gorman valid_page = page; 715bb13ffebSMel Gorman 716c122b208SJoonsoo Kim /* 71799c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 71899c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 71999c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 72099c0fd5eSVlastimil Babka * potential isolation targets. 7216c14466cSMel Gorman */ 72299c0fd5eSVlastimil Babka if (PageBuddy(page)) { 72399c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 72499c0fd5eSVlastimil Babka 72599c0fd5eSVlastimil Babka /* 72699c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 72799c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 72899c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 72999c0fd5eSVlastimil Babka */ 73099c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 73199c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 732748446bbSMel Gorman continue; 73399c0fd5eSVlastimil Babka } 734748446bbSMel Gorman 7359927af74SMel Gorman /* 736bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 737bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 738bf6bddf1SRafael Aquini * Skip any other type of page 739bf6bddf1SRafael Aquini */ 74029c0dde8SVlastimil Babka is_lru = PageLRU(page); 74129c0dde8SVlastimil Babka if (!is_lru) { 742bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 743d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 744bf6bddf1SRafael Aquini /* Successfully isolated */ 745b6c75016SJoonsoo Kim goto isolate_success; 746bf6bddf1SRafael Aquini } 747bf6bddf1SRafael Aquini } 748bf6bddf1SRafael Aquini } 749bc835011SAndrea Arcangeli 750bc835011SAndrea Arcangeli /* 75129c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 75229c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 75329c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 75429c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 75529c0dde8SVlastimil Babka * danger is skipping too much. 756bc835011SAndrea Arcangeli */ 75729c0dde8SVlastimil Babka if (PageCompound(page)) { 75829c0dde8SVlastimil Babka unsigned int comp_order = compound_order(page); 75929c0dde8SVlastimil Babka 76029c0dde8SVlastimil Babka if (likely(comp_order < MAX_ORDER)) 76129c0dde8SVlastimil Babka low_pfn += (1UL << comp_order) - 1; 762edc2ca61SVlastimil Babka 763fdd048e1SVlastimil Babka goto isolate_fail; 7642a1402aaSMel Gorman } 7652a1402aaSMel Gorman 76629c0dde8SVlastimil Babka if (!is_lru) 767fdd048e1SVlastimil Babka goto isolate_fail; 76829c0dde8SVlastimil Babka 769119d6d59SDavid Rientjes /* 770119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 771119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 772119d6d59SDavid Rientjes * admittedly racy check. 773119d6d59SDavid Rientjes */ 774119d6d59SDavid Rientjes if (!page_mapping(page) && 775119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 776fdd048e1SVlastimil Babka goto isolate_fail; 777119d6d59SDavid Rientjes 77869b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 77969b7189fSVlastimil Babka if (!locked) { 7808b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7818b44d279SVlastimil Babka &flags, cc); 7828b44d279SVlastimil Babka if (!locked) 7832a1402aaSMel Gorman break; 7842a1402aaSMel Gorman 78529c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 7862a1402aaSMel Gorman if (!PageLRU(page)) 787fdd048e1SVlastimil Babka goto isolate_fail; 78829c0dde8SVlastimil Babka 78929c0dde8SVlastimil Babka /* 79029c0dde8SVlastimil Babka * Page become compound since the non-locked check, 79129c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 79229c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 79329c0dde8SVlastimil Babka */ 79429c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 79529c0dde8SVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 796fdd048e1SVlastimil Babka goto isolate_fail; 797bc835011SAndrea Arcangeli } 79869b7189fSVlastimil Babka } 799bc835011SAndrea Arcangeli 800fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 801fa9add64SHugh Dickins 802748446bbSMel Gorman /* Try isolate the page */ 803edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 804fdd048e1SVlastimil Babka goto isolate_fail; 805748446bbSMel Gorman 80629c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 807bc835011SAndrea Arcangeli 808748446bbSMel Gorman /* Successfully isolated */ 809fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 810b6c75016SJoonsoo Kim 811b6c75016SJoonsoo Kim isolate_success: 812fdd048e1SVlastimil Babka list_add(&page->lru, &cc->migratepages); 813748446bbSMel Gorman cc->nr_migratepages++; 814b7aba698SMel Gorman nr_isolated++; 815748446bbSMel Gorman 816a34753d2SVlastimil Babka /* 817a34753d2SVlastimil Babka * Record where we could have freed pages by migration and not 818a34753d2SVlastimil Babka * yet flushed them to buddy allocator. 819a34753d2SVlastimil Babka * - this is the lowest page that was isolated and likely be 820a34753d2SVlastimil Babka * then freed by migration. 821a34753d2SVlastimil Babka */ 822a34753d2SVlastimil Babka if (!cc->last_migrated_pfn) 823a34753d2SVlastimil Babka cc->last_migrated_pfn = low_pfn; 824a34753d2SVlastimil Babka 825748446bbSMel Gorman /* Avoid isolating too much */ 82631b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 82731b8384aSHillf Danton ++low_pfn; 828748446bbSMel Gorman break; 829748446bbSMel Gorman } 830fdd048e1SVlastimil Babka 831fdd048e1SVlastimil Babka continue; 832fdd048e1SVlastimil Babka isolate_fail: 833fdd048e1SVlastimil Babka if (!skip_on_failure) 834fdd048e1SVlastimil Babka continue; 835fdd048e1SVlastimil Babka 836fdd048e1SVlastimil Babka /* 837fdd048e1SVlastimil Babka * We have isolated some pages, but then failed. Release them 838fdd048e1SVlastimil Babka * instead of migrating, as we cannot form the cc->order buddy 839fdd048e1SVlastimil Babka * page anyway. 840fdd048e1SVlastimil Babka */ 841fdd048e1SVlastimil Babka if (nr_isolated) { 842fdd048e1SVlastimil Babka if (locked) { 843fdd048e1SVlastimil Babka spin_unlock_irqrestore(&zone->lru_lock, flags); 844fdd048e1SVlastimil Babka locked = false; 845fdd048e1SVlastimil Babka } 846fdd048e1SVlastimil Babka acct_isolated(zone, cc); 847fdd048e1SVlastimil Babka putback_movable_pages(&cc->migratepages); 848fdd048e1SVlastimil Babka cc->nr_migratepages = 0; 849fdd048e1SVlastimil Babka cc->last_migrated_pfn = 0; 850fdd048e1SVlastimil Babka nr_isolated = 0; 851fdd048e1SVlastimil Babka } 852fdd048e1SVlastimil Babka 853fdd048e1SVlastimil Babka if (low_pfn < next_skip_pfn) { 854fdd048e1SVlastimil Babka low_pfn = next_skip_pfn - 1; 855fdd048e1SVlastimil Babka /* 856fdd048e1SVlastimil Babka * The check near the loop beginning would have updated 857fdd048e1SVlastimil Babka * next_skip_pfn too, but this is a bit simpler. 858fdd048e1SVlastimil Babka */ 859fdd048e1SVlastimil Babka next_skip_pfn += 1UL << cc->order; 860fdd048e1SVlastimil Babka } 86131b8384aSHillf Danton } 862748446bbSMel Gorman 86399c0fd5eSVlastimil Babka /* 86499c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 86599c0fd5eSVlastimil Babka * the range to be scanned. 86699c0fd5eSVlastimil Babka */ 86799c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 86899c0fd5eSVlastimil Babka low_pfn = end_pfn; 86999c0fd5eSVlastimil Babka 870c67fe375SMel Gorman if (locked) 871c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 872748446bbSMel Gorman 87350b5b094SVlastimil Babka /* 87450b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 87550b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 87650b5b094SVlastimil Babka */ 87735979ef3SDavid Rientjes if (low_pfn == end_pfn) 878edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 879bb13ffebSMel Gorman 880e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 881e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 882b7aba698SMel Gorman 883010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 884397487dbSMel Gorman if (nr_isolated) 885010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 886397487dbSMel Gorman 8872fe86e00SMichal Nazarewicz return low_pfn; 8882fe86e00SMichal Nazarewicz } 8892fe86e00SMichal Nazarewicz 890edc2ca61SVlastimil Babka /** 891edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 892edc2ca61SVlastimil Babka * @cc: Compaction control structure. 893edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 894edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 895edc2ca61SVlastimil Babka * 896edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 897edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 898edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 899edc2ca61SVlastimil Babka */ 900edc2ca61SVlastimil Babka unsigned long 901edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 902edc2ca61SVlastimil Babka unsigned long end_pfn) 903edc2ca61SVlastimil Babka { 904e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 905edc2ca61SVlastimil Babka 906edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 907edc2ca61SVlastimil Babka pfn = start_pfn; 90806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 909e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 910e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 91106b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 912edc2ca61SVlastimil Babka 913edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 914e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 915edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 916edc2ca61SVlastimil Babka 917edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 918edc2ca61SVlastimil Babka 919e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 920e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 921edc2ca61SVlastimil Babka continue; 922edc2ca61SVlastimil Babka 923edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 924edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 925edc2ca61SVlastimil Babka 92614af4a5eSHugh Dickins if (!pfn) 927edc2ca61SVlastimil Babka break; 9286ea41c0cSJoonsoo Kim 9296ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 9306ea41c0cSJoonsoo Kim break; 931edc2ca61SVlastimil Babka } 932edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 933edc2ca61SVlastimil Babka 934edc2ca61SVlastimil Babka return pfn; 935edc2ca61SVlastimil Babka } 936edc2ca61SVlastimil Babka 937ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 938ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 939018e9a49SAndrew Morton 940018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 941018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page) 942018e9a49SAndrew Morton { 943018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 944018e9a49SAndrew Morton if (PageBuddy(page)) { 945018e9a49SAndrew Morton /* 946018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 947018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 948018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 949018e9a49SAndrew Morton */ 950018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 951018e9a49SAndrew Morton return false; 952018e9a49SAndrew Morton } 953018e9a49SAndrew Morton 954018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 955018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 956018e9a49SAndrew Morton return true; 957018e9a49SAndrew Morton 958018e9a49SAndrew Morton /* Otherwise skip the block */ 959018e9a49SAndrew Morton return false; 960018e9a49SAndrew Morton } 961018e9a49SAndrew Morton 962ff9543fdSMichal Nazarewicz /* 963f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 964f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 965f2849aa0SVlastimil Babka */ 966f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 967f2849aa0SVlastimil Babka { 968f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 969f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 970f2849aa0SVlastimil Babka } 971f2849aa0SVlastimil Babka 972f2849aa0SVlastimil Babka /* 973ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 974ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 975ff9543fdSMichal Nazarewicz */ 976edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 977ff9543fdSMichal Nazarewicz { 978edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 979ff9543fdSMichal Nazarewicz struct page *page; 980c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 981e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 982c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 983c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 984ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9852fe86e00SMichal Nazarewicz 986ff9543fdSMichal Nazarewicz /* 987ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 98849e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 989e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 990e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 991c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 992c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 993c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 99449e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 99549e068f0SVlastimil Babka * is using. 996ff9543fdSMichal Nazarewicz */ 997e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 99806b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(cc->free_pfn); 999c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 1000c96b9e50SVlastimil Babka zone_end_pfn(zone)); 100106b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 10022fe86e00SMichal Nazarewicz 1003ff9543fdSMichal Nazarewicz /* 1004ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 1005ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 1006ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 1007ff9543fdSMichal Nazarewicz */ 1008f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 1009c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 1010e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 1011e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 1012f6ea3adbSDavid Rientjes /* 1013f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 1014f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 1015be976572SVlastimil Babka * to schedule, or even abort async compaction. 1016f6ea3adbSDavid Rientjes */ 1017be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1018be976572SVlastimil Babka && compact_should_abort(cc)) 1019be976572SVlastimil Babka break; 1020f6ea3adbSDavid Rientjes 10217d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 10227d49d886SVlastimil Babka zone); 10237d49d886SVlastimil Babka if (!page) 1024ff9543fdSMichal Nazarewicz continue; 1025ff9543fdSMichal Nazarewicz 1026ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 102768e3e926SLinus Torvalds if (!suitable_migration_target(page)) 1028ff9543fdSMichal Nazarewicz continue; 102968e3e926SLinus Torvalds 1030bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 1031bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 1032bb13ffebSMel Gorman continue; 1033bb13ffebSMel Gorman 1034e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 1035*a46cbf3bSDavid Rientjes isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn, 1036*a46cbf3bSDavid Rientjes freelist, false); 1037ff9543fdSMichal Nazarewicz 1038ff9543fdSMichal Nazarewicz /* 1039*a46cbf3bSDavid Rientjes * If we isolated enough freepages, or aborted due to lock 1040*a46cbf3bSDavid Rientjes * contention, terminate. 1041e14c720eSVlastimil Babka */ 1042f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 1043f5f61a32SVlastimil Babka || cc->contended) { 1044*a46cbf3bSDavid Rientjes if (isolate_start_pfn >= block_end_pfn) { 1045*a46cbf3bSDavid Rientjes /* 1046*a46cbf3bSDavid Rientjes * Restart at previous pageblock if more 1047*a46cbf3bSDavid Rientjes * freepages can be isolated next time. 1048*a46cbf3bSDavid Rientjes */ 1049f5f61a32SVlastimil Babka isolate_start_pfn = 1050e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1051*a46cbf3bSDavid Rientjes } 1052be976572SVlastimil Babka break; 1053*a46cbf3bSDavid Rientjes } else if (isolate_start_pfn < block_end_pfn) { 1054f5f61a32SVlastimil Babka /* 1055*a46cbf3bSDavid Rientjes * If isolation failed early, do not continue 1056*a46cbf3bSDavid Rientjes * needlessly. 1057f5f61a32SVlastimil Babka */ 1058*a46cbf3bSDavid Rientjes break; 1059f5f61a32SVlastimil Babka } 1060c89511abSMel Gorman } 1061ff9543fdSMichal Nazarewicz 1062ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 1063ff9543fdSMichal Nazarewicz map_pages(freelist); 1064ff9543fdSMichal Nazarewicz 10657ed695e0SVlastimil Babka /* 1066f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1067f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1068f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1069f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 10707ed695e0SVlastimil Babka */ 1071f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1072748446bbSMel Gorman } 1073748446bbSMel Gorman 1074748446bbSMel Gorman /* 1075748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1076748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1077748446bbSMel Gorman */ 1078748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1079748446bbSMel Gorman unsigned long data, 1080748446bbSMel Gorman int **result) 1081748446bbSMel Gorman { 1082748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1083748446bbSMel Gorman struct page *freepage; 1084748446bbSMel Gorman 1085be976572SVlastimil Babka /* 1086be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1087be976572SVlastimil Babka * contention. 1088be976572SVlastimil Babka */ 1089748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1090be976572SVlastimil Babka if (!cc->contended) 1091edc2ca61SVlastimil Babka isolate_freepages(cc); 1092748446bbSMel Gorman 1093748446bbSMel Gorman if (list_empty(&cc->freepages)) 1094748446bbSMel Gorman return NULL; 1095748446bbSMel Gorman } 1096748446bbSMel Gorman 1097748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1098748446bbSMel Gorman list_del(&freepage->lru); 1099748446bbSMel Gorman cc->nr_freepages--; 1100748446bbSMel Gorman 1101748446bbSMel Gorman return freepage; 1102748446bbSMel Gorman } 1103748446bbSMel Gorman 1104748446bbSMel Gorman /* 1105d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1106d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1107d53aea3dSDavid Rientjes * special handling needed for NUMA. 1108d53aea3dSDavid Rientjes */ 1109d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1110d53aea3dSDavid Rientjes { 1111d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1112d53aea3dSDavid Rientjes 1113d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1114d53aea3dSDavid Rientjes cc->nr_freepages++; 1115d53aea3dSDavid Rientjes } 1116d53aea3dSDavid Rientjes 1117ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1118ff9543fdSMichal Nazarewicz typedef enum { 1119ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1120ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1121ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1122ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1123ff9543fdSMichal Nazarewicz 1124ff9543fdSMichal Nazarewicz /* 11255bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 11265bbe3547SEric B Munson * compactable pages. 11275bbe3547SEric B Munson */ 11285bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 11295bbe3547SEric B Munson 11305bbe3547SEric B Munson /* 1131edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1132edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1133edc2ca61SVlastimil Babka * compact_control. 1134ff9543fdSMichal Nazarewicz */ 1135ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1136ff9543fdSMichal Nazarewicz struct compact_control *cc) 1137ff9543fdSMichal Nazarewicz { 1138e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1139e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1140e1409c32SJoonsoo Kim unsigned long low_pfn; 1141edc2ca61SVlastimil Babka struct page *page; 1142edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 11435bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 1144edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1145ff9543fdSMichal Nazarewicz 1146edc2ca61SVlastimil Babka /* 1147edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1148edc2ca61SVlastimil Babka * initialized by compact_zone() 1149edc2ca61SVlastimil Babka */ 1150edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 115106b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1152e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1153e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1154ff9543fdSMichal Nazarewicz 1155ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 115606b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1157ff9543fdSMichal Nazarewicz 1158edc2ca61SVlastimil Babka /* 1159edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1160edc2ca61SVlastimil Babka * Do not cross the free scanner. 1161edc2ca61SVlastimil Babka */ 1162e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 1163e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1164e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1165e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1166edc2ca61SVlastimil Babka 1167edc2ca61SVlastimil Babka /* 1168edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1169edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1170edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1171edc2ca61SVlastimil Babka */ 1172edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1173edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1174edc2ca61SVlastimil Babka break; 1175edc2ca61SVlastimil Babka 1176e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1177e1409c32SJoonsoo Kim zone); 11787d49d886SVlastimil Babka if (!page) 1179edc2ca61SVlastimil Babka continue; 1180edc2ca61SVlastimil Babka 1181edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1182edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1183edc2ca61SVlastimil Babka continue; 1184edc2ca61SVlastimil Babka 1185edc2ca61SVlastimil Babka /* 1186edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1187edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1188edc2ca61SVlastimil Babka * of work satisfies the allocation. 1189edc2ca61SVlastimil Babka */ 1190edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1191edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1192edc2ca61SVlastimil Babka continue; 1193ff9543fdSMichal Nazarewicz 1194ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1195e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1196e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1197edc2ca61SVlastimil Babka 1198ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1199ff59909aSHugh Dickins acct_isolated(zone, cc); 1200ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1201ff59909aSHugh Dickins } 1202ff9543fdSMichal Nazarewicz 1203edc2ca61SVlastimil Babka /* 1204edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1205edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1206edc2ca61SVlastimil Babka * continue or not. 1207edc2ca61SVlastimil Babka */ 1208edc2ca61SVlastimil Babka break; 1209edc2ca61SVlastimil Babka } 1210edc2ca61SVlastimil Babka 1211edc2ca61SVlastimil Babka acct_isolated(zone, cc); 1212f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1213f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1214ff9543fdSMichal Nazarewicz 1215edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1216ff9543fdSMichal Nazarewicz } 1217ff9543fdSMichal Nazarewicz 121821c527a3SYaowei Bai /* 121921c527a3SYaowei Bai * order == -1 is expected when compacting via 122021c527a3SYaowei Bai * /proc/sys/vm/compact_memory 122121c527a3SYaowei Bai */ 122221c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 122321c527a3SYaowei Bai { 122421c527a3SYaowei Bai return order == -1; 122521c527a3SYaowei Bai } 122621c527a3SYaowei Bai 1227ea7ab982SMichal Hocko static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc, 12286d7ce559SDavid Rientjes const int migratetype) 1229748446bbSMel Gorman { 12308fb74b9fSMel Gorman unsigned int order; 12315a03b051SAndrea Arcangeli unsigned long watermark; 123256de7263SMel Gorman 1233be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 12342d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1235748446bbSMel Gorman 1236753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1237f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 123855b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 123902333641SVlastimil Babka reset_cached_positions(zone); 124055b7c4c9SVlastimil Babka 124162997027SMel Gorman /* 124262997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1243accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 124462997027SMel Gorman * flag itself as the decision to be clear should be directly 124562997027SMel Gorman * based on an allocation request. 124662997027SMel Gorman */ 1247accf6242SVlastimil Babka if (cc->direct_compaction) 124862997027SMel Gorman zone->compact_blockskip_flush = true; 124962997027SMel Gorman 1250c8f7de0bSMichal Hocko if (cc->whole_zone) 1251748446bbSMel Gorman return COMPACT_COMPLETE; 1252c8f7de0bSMichal Hocko else 1253c8f7de0bSMichal Hocko return COMPACT_PARTIAL_SKIPPED; 1254bb13ffebSMel Gorman } 1255748446bbSMel Gorman 125621c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 125756de7263SMel Gorman return COMPACT_CONTINUE; 125856de7263SMel Gorman 12593957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 12603957c776SMichal Hocko watermark = low_wmark_pages(zone); 12613957c776SMichal Hocko 1262ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1263ebff3980SVlastimil Babka cc->alloc_flags)) 12643957c776SMichal Hocko return COMPACT_CONTINUE; 12653957c776SMichal Hocko 126656de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 126756de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 12688fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 12692149cdaeSJoonsoo Kim bool can_steal; 12708fb74b9fSMel Gorman 127156de7263SMel Gorman /* Job done if page is free of the right migratetype */ 12726d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 127356de7263SMel Gorman return COMPACT_PARTIAL; 127456de7263SMel Gorman 12752149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 12762149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 12772149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 12782149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 12792149cdaeSJoonsoo Kim return COMPACT_PARTIAL; 12802149cdaeSJoonsoo Kim #endif 12812149cdaeSJoonsoo Kim /* 12822149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 12832149cdaeSJoonsoo Kim * other migratetype buddy lists. 12842149cdaeSJoonsoo Kim */ 12852149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 12862149cdaeSJoonsoo Kim true, &can_steal) != -1) 128756de7263SMel Gorman return COMPACT_PARTIAL; 128856de7263SMel Gorman } 128956de7263SMel Gorman 1290837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1291837d026dSJoonsoo Kim } 1292837d026dSJoonsoo Kim 1293ea7ab982SMichal Hocko static enum compact_result compact_finished(struct zone *zone, 1294ea7ab982SMichal Hocko struct compact_control *cc, 1295837d026dSJoonsoo Kim const int migratetype) 1296837d026dSJoonsoo Kim { 1297837d026dSJoonsoo Kim int ret; 1298837d026dSJoonsoo Kim 1299837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1300837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1301837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1302837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1303837d026dSJoonsoo Kim 1304837d026dSJoonsoo Kim return ret; 1305748446bbSMel Gorman } 1306748446bbSMel Gorman 13073e7d3449SMel Gorman /* 13083e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 13093e7d3449SMel Gorman * Returns 13103e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 13113e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 13123e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 13133e7d3449SMel Gorman */ 1314ea7ab982SMichal Hocko static enum compact_result __compaction_suitable(struct zone *zone, int order, 1315c603844bSMel Gorman unsigned int alloc_flags, 131686a294a8SMichal Hocko int classzone_idx, 131786a294a8SMichal Hocko unsigned long wmark_target) 13183e7d3449SMel Gorman { 13193e7d3449SMel Gorman int fragindex; 13203e7d3449SMel Gorman unsigned long watermark; 13213e7d3449SMel Gorman 132221c527a3SYaowei Bai if (is_via_compact_memory(order)) 13233957c776SMichal Hocko return COMPACT_CONTINUE; 13243957c776SMichal Hocko 1325ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1326ebff3980SVlastimil Babka /* 1327ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1328ebff3980SVlastimil Babka * should be no need for compaction at all. 1329ebff3980SVlastimil Babka */ 1330ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1331ebff3980SVlastimil Babka alloc_flags)) 1332ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1333ebff3980SVlastimil Babka 13343957c776SMichal Hocko /* 13353e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 13363e7d3449SMel Gorman * This is because during migration, copies of pages need to be 13373e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 13383e7d3449SMel Gorman */ 1339ebff3980SVlastimil Babka watermark += (2UL << order); 134086a294a8SMichal Hocko if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, 134186a294a8SMichal Hocko alloc_flags, wmark_target)) 13423e7d3449SMel Gorman return COMPACT_SKIPPED; 13433e7d3449SMel Gorman 13443e7d3449SMel Gorman /* 13453e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 13463e7d3449SMel Gorman * low memory or external fragmentation 13473e7d3449SMel Gorman * 1348ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1349ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 13503e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 13513e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 13523e7d3449SMel Gorman * 13533e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 13543e7d3449SMel Gorman */ 13553e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 13563e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1357837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 13583e7d3449SMel Gorman 13593e7d3449SMel Gorman return COMPACT_CONTINUE; 13603e7d3449SMel Gorman } 13613e7d3449SMel Gorman 1362ea7ab982SMichal Hocko enum compact_result compaction_suitable(struct zone *zone, int order, 1363c603844bSMel Gorman unsigned int alloc_flags, 1364c603844bSMel Gorman int classzone_idx) 1365837d026dSJoonsoo Kim { 1366ea7ab982SMichal Hocko enum compact_result ret; 1367837d026dSJoonsoo Kim 136886a294a8SMichal Hocko ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, 136986a294a8SMichal Hocko zone_page_state(zone, NR_FREE_PAGES)); 1370837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1371837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1372837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1373837d026dSJoonsoo Kim 1374837d026dSJoonsoo Kim return ret; 1375837d026dSJoonsoo Kim } 1376837d026dSJoonsoo Kim 137786a294a8SMichal Hocko bool compaction_zonelist_suitable(struct alloc_context *ac, int order, 137886a294a8SMichal Hocko int alloc_flags) 137986a294a8SMichal Hocko { 138086a294a8SMichal Hocko struct zone *zone; 138186a294a8SMichal Hocko struct zoneref *z; 138286a294a8SMichal Hocko 138386a294a8SMichal Hocko /* 138486a294a8SMichal Hocko * Make sure at least one zone would pass __compaction_suitable if we continue 138586a294a8SMichal Hocko * retrying the reclaim. 138686a294a8SMichal Hocko */ 138786a294a8SMichal Hocko for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 138886a294a8SMichal Hocko ac->nodemask) { 138986a294a8SMichal Hocko unsigned long available; 139086a294a8SMichal Hocko enum compact_result compact_result; 139186a294a8SMichal Hocko 139286a294a8SMichal Hocko /* 139386a294a8SMichal Hocko * Do not consider all the reclaimable memory because we do not 139486a294a8SMichal Hocko * want to trash just for a single high order allocation which 139586a294a8SMichal Hocko * is even not guaranteed to appear even if __compaction_suitable 139686a294a8SMichal Hocko * is happy about the watermark check. 139786a294a8SMichal Hocko */ 139886a294a8SMichal Hocko available = zone_reclaimable_pages(zone) / order; 139986a294a8SMichal Hocko available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 140086a294a8SMichal Hocko compact_result = __compaction_suitable(zone, order, alloc_flags, 140186a294a8SMichal Hocko ac_classzone_idx(ac), available); 140286a294a8SMichal Hocko if (compact_result != COMPACT_SKIPPED && 140386a294a8SMichal Hocko compact_result != COMPACT_NOT_SUITABLE_ZONE) 140486a294a8SMichal Hocko return true; 140586a294a8SMichal Hocko } 140686a294a8SMichal Hocko 140786a294a8SMichal Hocko return false; 140886a294a8SMichal Hocko } 140986a294a8SMichal Hocko 1410ea7ab982SMichal Hocko static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc) 1411748446bbSMel Gorman { 1412ea7ab982SMichal Hocko enum compact_result ret; 1413c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1414108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 14156d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1416e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1417748446bbSMel Gorman 1418ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1419ebff3980SVlastimil Babka cc->classzone_idx); 14203e7d3449SMel Gorman /* Compaction is likely to fail */ 1421c46649deSMichal Hocko if (ret == COMPACT_PARTIAL || ret == COMPACT_SKIPPED) 14223e7d3449SMel Gorman return ret; 1423c46649deSMichal Hocko 1424c46649deSMichal Hocko /* huh, compaction_suitable is returning something unexpected */ 1425c46649deSMichal Hocko VM_BUG_ON(ret != COMPACT_CONTINUE); 14263e7d3449SMel Gorman 1427c89511abSMel Gorman /* 1428d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1429accf6242SVlastimil Babka * is about to be retried after being deferred. 1430d3132e4bSVlastimil Babka */ 1431accf6242SVlastimil Babka if (compaction_restarting(zone, cc->order)) 1432d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1433d3132e4bSVlastimil Babka 1434d3132e4bSVlastimil Babka /* 1435c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1436c89511abSMel Gorman * information on where the scanners should start but check that it 1437c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1438c89511abSMel Gorman */ 1439e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1440c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1441623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 144206b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 1443c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1444c89511abSMel Gorman } 1445623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1446c89511abSMel Gorman cc->migrate_pfn = start_pfn; 144735979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 144835979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1449c89511abSMel Gorman } 1450c8f7de0bSMichal Hocko 1451c8f7de0bSMichal Hocko if (cc->migrate_pfn == start_pfn) 1452c8f7de0bSMichal Hocko cc->whole_zone = true; 1453c8f7de0bSMichal Hocko 14541a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1455748446bbSMel Gorman 145616c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 145716c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 14580eb927c0SMel Gorman 1459748446bbSMel Gorman migrate_prep_local(); 1460748446bbSMel Gorman 14616d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 14626d7ce559SDavid Rientjes COMPACT_CONTINUE) { 14639d502c1cSMinchan Kim int err; 1464748446bbSMel Gorman 1465f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1466f9e35b3bSMel Gorman case ISOLATE_ABORT: 14672d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14685733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1469e64c5237SShaohua Li cc->nr_migratepages = 0; 1470f9e35b3bSMel Gorman goto out; 1471f9e35b3bSMel Gorman case ISOLATE_NONE: 1472fdaf7f5cSVlastimil Babka /* 1473fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1474fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1475fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1476fdaf7f5cSVlastimil Babka */ 1477fdaf7f5cSVlastimil Babka goto check_drain; 1478f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1479f9e35b3bSMel Gorman ; 1480f9e35b3bSMel Gorman } 1481748446bbSMel Gorman 1482d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1483e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 14847b2a2d4aSMel Gorman MR_COMPACTION); 1485748446bbSMel Gorman 1486f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1487f8c9301fSVlastimil Babka &cc->migratepages); 1488748446bbSMel Gorman 1489f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1490f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 14919d502c1cSMinchan Kim if (err) { 14925733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 14937ed695e0SVlastimil Babka /* 14947ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 14957ed695e0SVlastimil Babka * and we want compact_finished() to detect it 14967ed695e0SVlastimil Babka */ 1497f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 14982d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14994bf2bba3SDavid Rientjes goto out; 1500748446bbSMel Gorman } 1501fdd048e1SVlastimil Babka /* 1502fdd048e1SVlastimil Babka * We failed to migrate at least one page in the current 1503fdd048e1SVlastimil Babka * order-aligned block, so skip the rest of it. 1504fdd048e1SVlastimil Babka */ 1505fdd048e1SVlastimil Babka if (cc->direct_compaction && 1506fdd048e1SVlastimil Babka (cc->mode == MIGRATE_ASYNC)) { 1507fdd048e1SVlastimil Babka cc->migrate_pfn = block_end_pfn( 1508fdd048e1SVlastimil Babka cc->migrate_pfn - 1, cc->order); 1509fdd048e1SVlastimil Babka /* Draining pcplists is useless in this case */ 1510fdd048e1SVlastimil Babka cc->last_migrated_pfn = 0; 1511fdd048e1SVlastimil Babka 1512fdd048e1SVlastimil Babka } 15134bf2bba3SDavid Rientjes } 1514fdaf7f5cSVlastimil Babka 1515fdaf7f5cSVlastimil Babka check_drain: 1516fdaf7f5cSVlastimil Babka /* 1517fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1518fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1519fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1520fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1521fdaf7f5cSVlastimil Babka * would succeed. 1522fdaf7f5cSVlastimil Babka */ 15231a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1524fdaf7f5cSVlastimil Babka int cpu; 1525fdaf7f5cSVlastimil Babka unsigned long current_block_start = 152606b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 1527fdaf7f5cSVlastimil Babka 15281a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1529fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1530fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1531fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1532fdaf7f5cSVlastimil Babka put_cpu(); 1533fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 15341a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1535fdaf7f5cSVlastimil Babka } 1536fdaf7f5cSVlastimil Babka } 1537fdaf7f5cSVlastimil Babka 1538748446bbSMel Gorman } 1539748446bbSMel Gorman 1540f9e35b3bSMel Gorman out: 15416bace090SVlastimil Babka /* 15426bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 15436bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 15446bace090SVlastimil Babka */ 15456bace090SVlastimil Babka if (cc->nr_freepages > 0) { 15466bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 15476bace090SVlastimil Babka 15486bace090SVlastimil Babka cc->nr_freepages = 0; 15496bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 15506bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 155106b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 15526bace090SVlastimil Babka /* 15536bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 15546bace090SVlastimil Babka * already reset to zone end in compact_finished() 15556bace090SVlastimil Babka */ 15566bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 15576bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 15586bace090SVlastimil Babka } 1559748446bbSMel Gorman 156016c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 156116c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 15620eb927c0SMel Gorman 15632d1e1041SVlastimil Babka if (ret == COMPACT_CONTENDED) 15642d1e1041SVlastimil Babka ret = COMPACT_PARTIAL; 15652d1e1041SVlastimil Babka 1566748446bbSMel Gorman return ret; 1567748446bbSMel Gorman } 156876ab0f53SMel Gorman 1569ea7ab982SMichal Hocko static enum compact_result compact_zone_order(struct zone *zone, int order, 1570ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1571c603844bSMel Gorman unsigned int alloc_flags, int classzone_idx) 157256de7263SMel Gorman { 1573ea7ab982SMichal Hocko enum compact_result ret; 157456de7263SMel Gorman struct compact_control cc = { 157556de7263SMel Gorman .nr_freepages = 0, 157656de7263SMel Gorman .nr_migratepages = 0, 157756de7263SMel Gorman .order = order, 15786d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 157956de7263SMel Gorman .zone = zone, 1580e0b9daebSDavid Rientjes .mode = mode, 1581ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1582ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 1583accf6242SVlastimil Babka .direct_compaction = true, 158456de7263SMel Gorman }; 158556de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 158656de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 158756de7263SMel Gorman 1588e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1589e64c5237SShaohua Li 1590e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1591e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1592e64c5237SShaohua Li 1593e64c5237SShaohua Li *contended = cc.contended; 1594e64c5237SShaohua Li return ret; 159556de7263SMel Gorman } 159656de7263SMel Gorman 15975e771905SMel Gorman int sysctl_extfrag_threshold = 500; 15985e771905SMel Gorman 159956de7263SMel Gorman /** 160056de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 160156de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 16021a6d53a1SVlastimil Babka * @order: The order of the current allocation 16031a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 16041a6d53a1SVlastimil Babka * @ac: The context of current allocation 1605e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 16061f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 16071f9efdefSVlastimil Babka * need_resched() or lock contention 160856de7263SMel Gorman * 160956de7263SMel Gorman * This is the main entry point for direct page compaction. 161056de7263SMel Gorman */ 1611ea7ab982SMichal Hocko enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 1612c603844bSMel Gorman unsigned int alloc_flags, const struct alloc_context *ac, 16131a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 161456de7263SMel Gorman { 161556de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 161656de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 161756de7263SMel Gorman struct zoneref *z; 161856de7263SMel Gorman struct zone *zone; 16191d4746d3SMichal Hocko enum compact_result rc = COMPACT_SKIPPED; 16201f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 16211f9efdefSVlastimil Babka 16221f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 162356de7263SMel Gorman 16244ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1625c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 162653853e2dSVlastimil Babka return COMPACT_SKIPPED; 162756de7263SMel Gorman 1628837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1629837d026dSJoonsoo Kim 163056de7263SMel Gorman /* Compact each zone in the list */ 16311a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 16321a6d53a1SVlastimil Babka ac->nodemask) { 1633ea7ab982SMichal Hocko enum compact_result status; 16341f9efdefSVlastimil Babka int zone_contended; 163556de7263SMel Gorman 16361d4746d3SMichal Hocko if (compaction_deferred(zone, order)) { 16371d4746d3SMichal Hocko rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); 163853853e2dSVlastimil Babka continue; 16391d4746d3SMichal Hocko } 164053853e2dSVlastimil Babka 1641e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 16421a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 164393ea9964SMel Gorman ac_classzone_idx(ac)); 164456de7263SMel Gorman rc = max(status, rc); 16451f9efdefSVlastimil Babka /* 16461f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 16471f9efdefSVlastimil Babka * to clear all_zones_contended. 16481f9efdefSVlastimil Babka */ 16491f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 165056de7263SMel Gorman 16513e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1652ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 165393ea9964SMel Gorman ac_classzone_idx(ac), alloc_flags)) { 165453853e2dSVlastimil Babka /* 165553853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 165653853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 165753853e2dSVlastimil Babka * will repeat this with true if allocation indeed 165853853e2dSVlastimil Babka * succeeds in this zone. 165953853e2dSVlastimil Babka */ 166053853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 16611f9efdefSVlastimil Babka /* 16621f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 16631f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 16641f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 16651f9efdefSVlastimil Babka * however still fail so we better signal the 16661f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 16671f9efdefSVlastimil Babka * prevent the allocation attempt). 16681f9efdefSVlastimil Babka */ 16691f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 16701f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 16711f9efdefSVlastimil Babka 16721f9efdefSVlastimil Babka goto break_loop; 16731f9efdefSVlastimil Babka } 16741f9efdefSVlastimil Babka 1675c8f7de0bSMichal Hocko if (mode != MIGRATE_ASYNC && (status == COMPACT_COMPLETE || 1676c8f7de0bSMichal Hocko status == COMPACT_PARTIAL_SKIPPED)) { 167753853e2dSVlastimil Babka /* 167853853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 167953853e2dSVlastimil Babka * so we defer compaction there. If it ends up 168053853e2dSVlastimil Babka * succeeding after all, it will be reset. 168153853e2dSVlastimil Babka */ 168253853e2dSVlastimil Babka defer_compaction(zone, order); 168353853e2dSVlastimil Babka } 16841f9efdefSVlastimil Babka 16851f9efdefSVlastimil Babka /* 16861f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 16871f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 16881f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 16891f9efdefSVlastimil Babka * contention. 16901f9efdefSVlastimil Babka */ 16911f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 16921f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 16931f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 16941f9efdefSVlastimil Babka goto break_loop; 169556de7263SMel Gorman } 169656de7263SMel Gorman 16971f9efdefSVlastimil Babka continue; 16981f9efdefSVlastimil Babka break_loop: 16991f9efdefSVlastimil Babka /* 17001f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 17011f9efdefSVlastimil Babka * and assume they are not all lock contended. 17021f9efdefSVlastimil Babka */ 17031f9efdefSVlastimil Babka all_zones_contended = 0; 17041f9efdefSVlastimil Babka break; 17051f9efdefSVlastimil Babka } 17061f9efdefSVlastimil Babka 17071f9efdefSVlastimil Babka /* 17081f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 17091f9efdefSVlastimil Babka * zones that were tried were lock contended. 17101f9efdefSVlastimil Babka */ 17111d4746d3SMichal Hocko if (rc > COMPACT_INACTIVE && all_zones_contended) 17121f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 17131f9efdefSVlastimil Babka 171456de7263SMel Gorman return rc; 171556de7263SMel Gorman } 171656de7263SMel Gorman 171756de7263SMel Gorman 171876ab0f53SMel Gorman /* Compact all zones within a node */ 17197103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 172076ab0f53SMel Gorman { 172176ab0f53SMel Gorman int zoneid; 172276ab0f53SMel Gorman struct zone *zone; 172376ab0f53SMel Gorman 172476ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 172576ab0f53SMel Gorman 172676ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 172776ab0f53SMel Gorman if (!populated_zone(zone)) 172876ab0f53SMel Gorman continue; 172976ab0f53SMel Gorman 17307be62de9SRik van Riel cc->nr_freepages = 0; 17317be62de9SRik van Riel cc->nr_migratepages = 0; 17327be62de9SRik van Riel cc->zone = zone; 17337be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 17347be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 173576ab0f53SMel Gorman 1736195b0c60SGioh Kim /* 1737195b0c60SGioh Kim * When called via /proc/sys/vm/compact_memory 1738195b0c60SGioh Kim * this makes sure we compact the whole zone regardless of 1739195b0c60SGioh Kim * cached scanner positions. 1740195b0c60SGioh Kim */ 174121c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 1742195b0c60SGioh Kim __reset_isolation_suitable(zone); 1743195b0c60SGioh Kim 174421c527a3SYaowei Bai if (is_via_compact_memory(cc->order) || 174521c527a3SYaowei Bai !compaction_deferred(zone, cc->order)) 17467be62de9SRik van Riel compact_zone(zone, cc); 174776ab0f53SMel Gorman 174875469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->freepages)); 174975469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->migratepages)); 175075469345SJoonsoo Kim 175175469345SJoonsoo Kim if (is_via_compact_memory(cc->order)) 175275469345SJoonsoo Kim continue; 175375469345SJoonsoo Kim 1754de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1755de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1756de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1757aff62249SRik van Riel } 175876ab0f53SMel Gorman } 175976ab0f53SMel Gorman 17607103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 17617be62de9SRik van Riel { 17627be62de9SRik van Riel struct compact_control cc = { 17637be62de9SRik van Riel .order = order, 1764e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 17657be62de9SRik van Riel }; 17667be62de9SRik van Riel 17673a7200afSMel Gorman if (!order) 17683a7200afSMel Gorman return; 17693a7200afSMel Gorman 17707103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 17717be62de9SRik van Riel } 17727be62de9SRik van Riel 17737103f16dSAndrew Morton static void compact_node(int nid) 17747be62de9SRik van Riel { 17757be62de9SRik van Riel struct compact_control cc = { 17767be62de9SRik van Riel .order = -1, 1777e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 177891ca9186SDavid Rientjes .ignore_skip_hint = true, 17797be62de9SRik van Riel }; 17807be62de9SRik van Riel 17817103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 17827be62de9SRik van Riel } 17837be62de9SRik van Riel 178476ab0f53SMel Gorman /* Compact all nodes in the system */ 17857964c06dSJason Liu static void compact_nodes(void) 178676ab0f53SMel Gorman { 178776ab0f53SMel Gorman int nid; 178876ab0f53SMel Gorman 17898575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17908575ec29SHugh Dickins lru_add_drain_all(); 17918575ec29SHugh Dickins 179276ab0f53SMel Gorman for_each_online_node(nid) 179376ab0f53SMel Gorman compact_node(nid); 179476ab0f53SMel Gorman } 179576ab0f53SMel Gorman 179676ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 179776ab0f53SMel Gorman int sysctl_compact_memory; 179876ab0f53SMel Gorman 1799fec4eb2cSYaowei Bai /* 1800fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1801fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1802fec4eb2cSYaowei Bai */ 180376ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 180476ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 180576ab0f53SMel Gorman { 180676ab0f53SMel Gorman if (write) 18077964c06dSJason Liu compact_nodes(); 180876ab0f53SMel Gorman 180976ab0f53SMel Gorman return 0; 181076ab0f53SMel Gorman } 1811ed4a6d7fSMel Gorman 18125e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 18135e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 18145e771905SMel Gorman { 18155e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 18165e771905SMel Gorman 18175e771905SMel Gorman return 0; 18185e771905SMel Gorman } 18195e771905SMel Gorman 1820ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 182174e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 182210fbcf4cSKay Sievers struct device_attribute *attr, 1823ed4a6d7fSMel Gorman const char *buf, size_t count) 1824ed4a6d7fSMel Gorman { 18258575ec29SHugh Dickins int nid = dev->id; 18268575ec29SHugh Dickins 18278575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 18288575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 18298575ec29SHugh Dickins lru_add_drain_all(); 18308575ec29SHugh Dickins 18318575ec29SHugh Dickins compact_node(nid); 18328575ec29SHugh Dickins } 1833ed4a6d7fSMel Gorman 1834ed4a6d7fSMel Gorman return count; 1835ed4a6d7fSMel Gorman } 183610fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1837ed4a6d7fSMel Gorman 1838ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1839ed4a6d7fSMel Gorman { 184010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1841ed4a6d7fSMel Gorman } 1842ed4a6d7fSMel Gorman 1843ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1844ed4a6d7fSMel Gorman { 184510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1846ed4a6d7fSMel Gorman } 1847ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1848ff9543fdSMichal Nazarewicz 1849698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 1850698b1b30SVlastimil Babka { 1851172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 1852698b1b30SVlastimil Babka } 1853698b1b30SVlastimil Babka 1854698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 1855698b1b30SVlastimil Babka { 1856698b1b30SVlastimil Babka int zoneid; 1857698b1b30SVlastimil Babka struct zone *zone; 1858698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 1859698b1b30SVlastimil Babka 18606cd9dc3eSChen Feng for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { 1861698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1862698b1b30SVlastimil Babka 1863698b1b30SVlastimil Babka if (!populated_zone(zone)) 1864698b1b30SVlastimil Babka continue; 1865698b1b30SVlastimil Babka 1866698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 1867698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 1868698b1b30SVlastimil Babka return true; 1869698b1b30SVlastimil Babka } 1870698b1b30SVlastimil Babka 1871698b1b30SVlastimil Babka return false; 1872698b1b30SVlastimil Babka } 1873698b1b30SVlastimil Babka 1874698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 1875698b1b30SVlastimil Babka { 1876698b1b30SVlastimil Babka /* 1877698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 1878698b1b30SVlastimil Babka * order is allocatable. 1879698b1b30SVlastimil Babka */ 1880698b1b30SVlastimil Babka int zoneid; 1881698b1b30SVlastimil Babka struct zone *zone; 1882698b1b30SVlastimil Babka struct compact_control cc = { 1883698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 1884698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 1885698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 1886698b1b30SVlastimil Babka .ignore_skip_hint = true, 1887698b1b30SVlastimil Babka 1888698b1b30SVlastimil Babka }; 1889698b1b30SVlastimil Babka bool success = false; 1890698b1b30SVlastimil Babka 1891698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 1892698b1b30SVlastimil Babka cc.classzone_idx); 1893698b1b30SVlastimil Babka count_vm_event(KCOMPACTD_WAKE); 1894698b1b30SVlastimil Babka 18956cd9dc3eSChen Feng for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { 1896698b1b30SVlastimil Babka int status; 1897698b1b30SVlastimil Babka 1898698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1899698b1b30SVlastimil Babka if (!populated_zone(zone)) 1900698b1b30SVlastimil Babka continue; 1901698b1b30SVlastimil Babka 1902698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 1903698b1b30SVlastimil Babka continue; 1904698b1b30SVlastimil Babka 1905698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 1906698b1b30SVlastimil Babka COMPACT_CONTINUE) 1907698b1b30SVlastimil Babka continue; 1908698b1b30SVlastimil Babka 1909698b1b30SVlastimil Babka cc.nr_freepages = 0; 1910698b1b30SVlastimil Babka cc.nr_migratepages = 0; 1911698b1b30SVlastimil Babka cc.zone = zone; 1912698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1913698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1914698b1b30SVlastimil Babka 1915172400c6SVlastimil Babka if (kthread_should_stop()) 1916172400c6SVlastimil Babka return; 1917698b1b30SVlastimil Babka status = compact_zone(zone, &cc); 1918698b1b30SVlastimil Babka 1919698b1b30SVlastimil Babka if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone), 1920698b1b30SVlastimil Babka cc.classzone_idx, 0)) { 1921698b1b30SVlastimil Babka success = true; 1922698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 1923c8f7de0bSMichal Hocko } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { 1924698b1b30SVlastimil Babka /* 1925698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 1926698b1b30SVlastimil Babka * sync direct compaction does. 1927698b1b30SVlastimil Babka */ 1928698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 1929698b1b30SVlastimil Babka } 1930698b1b30SVlastimil Babka 1931698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1932698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1933698b1b30SVlastimil Babka } 1934698b1b30SVlastimil Babka 1935698b1b30SVlastimil Babka /* 1936698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 1937698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 1938698b1b30SVlastimil Babka * our current ones 1939698b1b30SVlastimil Babka */ 1940698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 1941698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1942698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 1943698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1944698b1b30SVlastimil Babka } 1945698b1b30SVlastimil Babka 1946698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 1947698b1b30SVlastimil Babka { 1948698b1b30SVlastimil Babka if (!order) 1949698b1b30SVlastimil Babka return; 1950698b1b30SVlastimil Babka 1951698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 1952698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 1953698b1b30SVlastimil Babka 1954698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 1955698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 1956698b1b30SVlastimil Babka 1957698b1b30SVlastimil Babka if (!waitqueue_active(&pgdat->kcompactd_wait)) 1958698b1b30SVlastimil Babka return; 1959698b1b30SVlastimil Babka 1960698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 1961698b1b30SVlastimil Babka return; 1962698b1b30SVlastimil Babka 1963698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 1964698b1b30SVlastimil Babka classzone_idx); 1965698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 1966698b1b30SVlastimil Babka } 1967698b1b30SVlastimil Babka 1968698b1b30SVlastimil Babka /* 1969698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 1970698b1b30SVlastimil Babka * from the init process. 1971698b1b30SVlastimil Babka */ 1972698b1b30SVlastimil Babka static int kcompactd(void *p) 1973698b1b30SVlastimil Babka { 1974698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 1975698b1b30SVlastimil Babka struct task_struct *tsk = current; 1976698b1b30SVlastimil Babka 1977698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 1978698b1b30SVlastimil Babka 1979698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 1980698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 1981698b1b30SVlastimil Babka 1982698b1b30SVlastimil Babka set_freezable(); 1983698b1b30SVlastimil Babka 1984698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1985698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1986698b1b30SVlastimil Babka 1987698b1b30SVlastimil Babka while (!kthread_should_stop()) { 1988698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 1989698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 1990698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 1991698b1b30SVlastimil Babka 1992698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 1993698b1b30SVlastimil Babka } 1994698b1b30SVlastimil Babka 1995698b1b30SVlastimil Babka return 0; 1996698b1b30SVlastimil Babka } 1997698b1b30SVlastimil Babka 1998698b1b30SVlastimil Babka /* 1999698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 2000698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 2001698b1b30SVlastimil Babka */ 2002698b1b30SVlastimil Babka int kcompactd_run(int nid) 2003698b1b30SVlastimil Babka { 2004698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2005698b1b30SVlastimil Babka int ret = 0; 2006698b1b30SVlastimil Babka 2007698b1b30SVlastimil Babka if (pgdat->kcompactd) 2008698b1b30SVlastimil Babka return 0; 2009698b1b30SVlastimil Babka 2010698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 2011698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 2012698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 2013698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 2014698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 2015698b1b30SVlastimil Babka } 2016698b1b30SVlastimil Babka return ret; 2017698b1b30SVlastimil Babka } 2018698b1b30SVlastimil Babka 2019698b1b30SVlastimil Babka /* 2020698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 2021698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 2022698b1b30SVlastimil Babka */ 2023698b1b30SVlastimil Babka void kcompactd_stop(int nid) 2024698b1b30SVlastimil Babka { 2025698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 2026698b1b30SVlastimil Babka 2027698b1b30SVlastimil Babka if (kcompactd) { 2028698b1b30SVlastimil Babka kthread_stop(kcompactd); 2029698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 2030698b1b30SVlastimil Babka } 2031698b1b30SVlastimil Babka } 2032698b1b30SVlastimil Babka 2033698b1b30SVlastimil Babka /* 2034698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 2035698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 2036698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 2037698b1b30SVlastimil Babka * restore their cpu bindings. 2038698b1b30SVlastimil Babka */ 2039698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action, 2040698b1b30SVlastimil Babka void *hcpu) 2041698b1b30SVlastimil Babka { 2042698b1b30SVlastimil Babka int nid; 2043698b1b30SVlastimil Babka 2044698b1b30SVlastimil Babka if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 2045698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 2046698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 2047698b1b30SVlastimil Babka const struct cpumask *mask; 2048698b1b30SVlastimil Babka 2049698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 2050698b1b30SVlastimil Babka 2051698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 2052698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 2053698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 2054698b1b30SVlastimil Babka } 2055698b1b30SVlastimil Babka } 2056698b1b30SVlastimil Babka return NOTIFY_OK; 2057698b1b30SVlastimil Babka } 2058698b1b30SVlastimil Babka 2059698b1b30SVlastimil Babka static int __init kcompactd_init(void) 2060698b1b30SVlastimil Babka { 2061698b1b30SVlastimil Babka int nid; 2062698b1b30SVlastimil Babka 2063698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 2064698b1b30SVlastimil Babka kcompactd_run(nid); 2065698b1b30SVlastimil Babka hotcpu_notifier(cpu_callback, 0); 2066698b1b30SVlastimil Babka return 0; 2067698b1b30SVlastimil Babka } 2068698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 2069698b1b30SVlastimil Babka 2070ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 2071