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 */ 10*698b1b30SVlastimil 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> 21*698b1b30SVlastimil Babka #include <linux/kthread.h> 22*698b1b30SVlastimil 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 45748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 46748446bbSMel Gorman { 47748446bbSMel Gorman struct page *page, *next; 486bace090SVlastimil Babka unsigned long high_pfn = 0; 49748446bbSMel Gorman 50748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 516bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 52748446bbSMel Gorman list_del(&page->lru); 53748446bbSMel Gorman __free_page(page); 546bace090SVlastimil Babka if (pfn > high_pfn) 556bace090SVlastimil Babka high_pfn = pfn; 56748446bbSMel Gorman } 57748446bbSMel Gorman 586bace090SVlastimil Babka return high_pfn; 59748446bbSMel Gorman } 60748446bbSMel Gorman 61ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 62ff9543fdSMichal Nazarewicz { 63ff9543fdSMichal Nazarewicz struct page *page; 64ff9543fdSMichal Nazarewicz 65ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 66ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 67ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 68b8c73fc2SAndrey Ryabinin kasan_alloc_pages(page, 0); 69ff9543fdSMichal Nazarewicz } 70ff9543fdSMichal Nazarewicz } 71ff9543fdSMichal Nazarewicz 7247118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7347118af0SMichal Nazarewicz { 7447118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 7547118af0SMichal Nazarewicz } 7647118af0SMichal Nazarewicz 77bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 7824e2716fSJoonsoo Kim 7924e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 8024e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 8124e2716fSJoonsoo Kim 8224e2716fSJoonsoo Kim /* 8324e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 8424e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 8524e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 8624e2716fSJoonsoo Kim */ 8724e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 8824e2716fSJoonsoo Kim { 8924e2716fSJoonsoo Kim zone->compact_considered = 0; 9024e2716fSJoonsoo Kim zone->compact_defer_shift++; 9124e2716fSJoonsoo Kim 9224e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 9324e2716fSJoonsoo Kim zone->compact_order_failed = order; 9424e2716fSJoonsoo Kim 9524e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 9624e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 9724e2716fSJoonsoo Kim 9824e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 9924e2716fSJoonsoo Kim } 10024e2716fSJoonsoo Kim 10124e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 10224e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 10324e2716fSJoonsoo Kim { 10424e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 10524e2716fSJoonsoo Kim 10624e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 10724e2716fSJoonsoo Kim return false; 10824e2716fSJoonsoo Kim 10924e2716fSJoonsoo Kim /* Avoid possible overflow */ 11024e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 11124e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 11224e2716fSJoonsoo Kim 11324e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 11424e2716fSJoonsoo Kim return false; 11524e2716fSJoonsoo Kim 11624e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 11724e2716fSJoonsoo Kim 11824e2716fSJoonsoo Kim return true; 11924e2716fSJoonsoo Kim } 12024e2716fSJoonsoo Kim 12124e2716fSJoonsoo Kim /* 12224e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 12324e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 12424e2716fSJoonsoo Kim * expected to succeed. 12524e2716fSJoonsoo Kim */ 12624e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 12724e2716fSJoonsoo Kim bool alloc_success) 12824e2716fSJoonsoo Kim { 12924e2716fSJoonsoo Kim if (alloc_success) { 13024e2716fSJoonsoo Kim zone->compact_considered = 0; 13124e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 13224e2716fSJoonsoo Kim } 13324e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 13424e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 13524e2716fSJoonsoo Kim 13624e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 13724e2716fSJoonsoo Kim } 13824e2716fSJoonsoo Kim 13924e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 14024e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 14124e2716fSJoonsoo Kim { 14224e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14324e2716fSJoonsoo Kim return false; 14424e2716fSJoonsoo Kim 14524e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 14624e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 14724e2716fSJoonsoo Kim } 14824e2716fSJoonsoo Kim 149bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 150bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 151bb13ffebSMel Gorman struct page *page) 152bb13ffebSMel Gorman { 153bb13ffebSMel Gorman if (cc->ignore_skip_hint) 154bb13ffebSMel Gorman return true; 155bb13ffebSMel Gorman 156bb13ffebSMel Gorman return !get_pageblock_skip(page); 157bb13ffebSMel Gorman } 158bb13ffebSMel Gorman 15902333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 16002333641SVlastimil Babka { 16102333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 16202333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 163623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 164623446e4SJoonsoo Kim round_down(zone_end_pfn(zone) - 1, pageblock_nr_pages); 16502333641SVlastimil Babka } 16602333641SVlastimil Babka 167bb13ffebSMel Gorman /* 168bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 169bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 170bb13ffebSMel Gorman * meet. 171bb13ffebSMel Gorman */ 17262997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 173bb13ffebSMel Gorman { 174bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 175108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 176bb13ffebSMel Gorman unsigned long pfn; 177bb13ffebSMel Gorman 17862997027SMel Gorman zone->compact_blockskip_flush = false; 179bb13ffebSMel Gorman 180bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 181bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 182bb13ffebSMel Gorman struct page *page; 183bb13ffebSMel Gorman 184bb13ffebSMel Gorman cond_resched(); 185bb13ffebSMel Gorman 186bb13ffebSMel Gorman if (!pfn_valid(pfn)) 187bb13ffebSMel Gorman continue; 188bb13ffebSMel Gorman 189bb13ffebSMel Gorman page = pfn_to_page(pfn); 190bb13ffebSMel Gorman if (zone != page_zone(page)) 191bb13ffebSMel Gorman continue; 192bb13ffebSMel Gorman 193bb13ffebSMel Gorman clear_pageblock_skip(page); 194bb13ffebSMel Gorman } 19502333641SVlastimil Babka 19602333641SVlastimil Babka reset_cached_positions(zone); 197bb13ffebSMel Gorman } 198bb13ffebSMel Gorman 19962997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 20062997027SMel Gorman { 20162997027SMel Gorman int zoneid; 20262997027SMel Gorman 20362997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 20462997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 20562997027SMel Gorman if (!populated_zone(zone)) 20662997027SMel Gorman continue; 20762997027SMel Gorman 20862997027SMel Gorman /* Only flush if a full compaction finished recently */ 20962997027SMel Gorman if (zone->compact_blockskip_flush) 21062997027SMel Gorman __reset_isolation_suitable(zone); 21162997027SMel Gorman } 21262997027SMel Gorman } 21362997027SMel Gorman 214bb13ffebSMel Gorman /* 215bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 21662997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 217bb13ffebSMel Gorman */ 218c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 219c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 220edc2ca61SVlastimil Babka bool migrate_scanner) 221bb13ffebSMel Gorman { 222c89511abSMel Gorman struct zone *zone = cc->zone; 22335979ef3SDavid Rientjes unsigned long pfn; 2246815bf3fSJoonsoo Kim 2256815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2266815bf3fSJoonsoo Kim return; 2276815bf3fSJoonsoo Kim 228bb13ffebSMel Gorman if (!page) 229bb13ffebSMel Gorman return; 230bb13ffebSMel Gorman 23135979ef3SDavid Rientjes if (nr_isolated) 23235979ef3SDavid Rientjes return; 23335979ef3SDavid Rientjes 234bb13ffebSMel Gorman set_pageblock_skip(page); 235c89511abSMel Gorman 23635979ef3SDavid Rientjes pfn = page_to_pfn(page); 23735979ef3SDavid Rientjes 23835979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 239c89511abSMel Gorman if (migrate_scanner) { 24035979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 24135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 242e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 243e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 24435979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 245c89511abSMel Gorman } else { 24635979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 247c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 248c89511abSMel Gorman } 249c89511abSMel Gorman } 250bb13ffebSMel Gorman #else 251bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 252bb13ffebSMel Gorman struct page *page) 253bb13ffebSMel Gorman { 254bb13ffebSMel Gorman return true; 255bb13ffebSMel Gorman } 256bb13ffebSMel Gorman 257c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 258c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 259edc2ca61SVlastimil Babka bool migrate_scanner) 260bb13ffebSMel Gorman { 261bb13ffebSMel Gorman } 262bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 263bb13ffebSMel Gorman 2641f9efdefSVlastimil Babka /* 2658b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 2668b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 2678b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 2688b44d279SVlastimil Babka * 2698b44d279SVlastimil Babka * Returns true if the lock is held 2708b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 2711f9efdefSVlastimil Babka */ 2728b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 2738b44d279SVlastimil Babka struct compact_control *cc) 2748b44d279SVlastimil Babka { 2758b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 2768b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 2778b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 2788b44d279SVlastimil Babka return false; 2798b44d279SVlastimil Babka } 2808b44d279SVlastimil Babka } else { 2818b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 2828b44d279SVlastimil Babka } 2831f9efdefSVlastimil Babka 2848b44d279SVlastimil Babka return true; 2852a1402aaSMel Gorman } 2862a1402aaSMel Gorman 28785aa125fSMichal Nazarewicz /* 288c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 2898b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 2908b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 2918b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 2928b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 2938b44d279SVlastimil Babka * aborts. Sync compaction schedules. 2948b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 2958b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 296c67fe375SMel Gorman * 2978b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 2988b44d279SVlastimil Babka * async compaction due to need_resched() 2998b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3008b44d279SVlastimil Babka * scheduled) 301c67fe375SMel Gorman */ 3028b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3038b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 304c67fe375SMel Gorman { 3058b44d279SVlastimil Babka if (*locked) { 3068b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3078b44d279SVlastimil Babka *locked = false; 308c67fe375SMel Gorman } 309c67fe375SMel Gorman 3108b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 3118b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3128b44d279SVlastimil Babka return true; 3138b44d279SVlastimil Babka } 3148b44d279SVlastimil Babka 3158b44d279SVlastimil Babka if (need_resched()) { 316e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 3178b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3188b44d279SVlastimil Babka return true; 319c67fe375SMel Gorman } 320c67fe375SMel Gorman cond_resched(); 321c67fe375SMel Gorman } 322c67fe375SMel Gorman 3238b44d279SVlastimil Babka return false; 324c67fe375SMel Gorman } 325c67fe375SMel Gorman 326be976572SVlastimil Babka /* 327be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 328be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3298b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 330be976572SVlastimil Babka * is used where no lock is concerned. 331be976572SVlastimil Babka * 332be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 333be976572SVlastimil Babka * Returns true when async compaction should abort. 334be976572SVlastimil Babka */ 335be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 336be976572SVlastimil Babka { 337be976572SVlastimil Babka /* async compaction aborts if contended */ 338be976572SVlastimil Babka if (need_resched()) { 339be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3401f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 341be976572SVlastimil Babka return true; 342be976572SVlastimil Babka } 343be976572SVlastimil Babka 344be976572SVlastimil Babka cond_resched(); 345be976572SVlastimil Babka } 346be976572SVlastimil Babka 347be976572SVlastimil Babka return false; 348be976572SVlastimil Babka } 349be976572SVlastimil Babka 350c67fe375SMel Gorman /* 3519e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3529e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3539e4be470SJerome Marchand * (even though it may still end up isolating some pages). 35485aa125fSMichal Nazarewicz */ 355f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 356e14c720eSVlastimil Babka unsigned long *start_pfn, 35785aa125fSMichal Nazarewicz unsigned long end_pfn, 35885aa125fSMichal Nazarewicz struct list_head *freelist, 35985aa125fSMichal Nazarewicz bool strict) 360748446bbSMel Gorman { 361b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 362bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 363b8b2d825SXiubo Li unsigned long flags = 0; 364f40d1e42SMel Gorman bool locked = false; 365e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 366748446bbSMel Gorman 367748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 368748446bbSMel Gorman 369f40d1e42SMel Gorman /* Isolate free pages. */ 370748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 371748446bbSMel Gorman int isolated, i; 372748446bbSMel Gorman struct page *page = cursor; 373748446bbSMel Gorman 3748b44d279SVlastimil Babka /* 3758b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 3768b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 3778b44d279SVlastimil Babka * pending or async compaction detects need_resched() 3788b44d279SVlastimil Babka */ 3798b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 3808b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 3818b44d279SVlastimil Babka &locked, cc)) 3828b44d279SVlastimil Babka break; 3838b44d279SVlastimil Babka 384b7aba698SMel Gorman nr_scanned++; 385f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 3862af120bcSLaura Abbott goto isolate_fail; 3872af120bcSLaura Abbott 388bb13ffebSMel Gorman if (!valid_page) 389bb13ffebSMel Gorman valid_page = page; 3909fcd6d2eSVlastimil Babka 3919fcd6d2eSVlastimil Babka /* 3929fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 3939fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 3949fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 3959fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 3969fcd6d2eSVlastimil Babka */ 3979fcd6d2eSVlastimil Babka if (PageCompound(page)) { 3989fcd6d2eSVlastimil Babka unsigned int comp_order = compound_order(page); 3999fcd6d2eSVlastimil Babka 4009fcd6d2eSVlastimil Babka if (likely(comp_order < MAX_ORDER)) { 4019fcd6d2eSVlastimil Babka blockpfn += (1UL << comp_order) - 1; 4029fcd6d2eSVlastimil Babka cursor += (1UL << comp_order) - 1; 4039fcd6d2eSVlastimil Babka } 4049fcd6d2eSVlastimil Babka 4059fcd6d2eSVlastimil Babka goto isolate_fail; 4069fcd6d2eSVlastimil Babka } 4079fcd6d2eSVlastimil Babka 408f40d1e42SMel Gorman if (!PageBuddy(page)) 4092af120bcSLaura Abbott goto isolate_fail; 410f40d1e42SMel Gorman 411f40d1e42SMel Gorman /* 41269b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 41369b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 41469b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 41569b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 41669b7189fSVlastimil Babka * recheck as well. 41769b7189fSVlastimil Babka */ 41869b7189fSVlastimil Babka if (!locked) { 41969b7189fSVlastimil Babka /* 420f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 421f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 422f40d1e42SMel Gorman * heavily contended if there are parallel allocations 423f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 424f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 425f40d1e42SMel Gorman * possible. 426f40d1e42SMel Gorman */ 4278b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4288b44d279SVlastimil Babka &flags, cc); 429f40d1e42SMel Gorman if (!locked) 430f40d1e42SMel Gorman break; 431f40d1e42SMel Gorman 432f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 433f40d1e42SMel Gorman if (!PageBuddy(page)) 4342af120bcSLaura Abbott goto isolate_fail; 43569b7189fSVlastimil Babka } 436748446bbSMel Gorman 437748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 438748446bbSMel Gorman isolated = split_free_page(page); 439748446bbSMel Gorman total_isolated += isolated; 440748446bbSMel Gorman for (i = 0; i < isolated; i++) { 441748446bbSMel Gorman list_add(&page->lru, freelist); 442748446bbSMel Gorman page++; 443748446bbSMel Gorman } 444748446bbSMel Gorman 445748446bbSMel Gorman /* If a page was split, advance to the end of it */ 446748446bbSMel Gorman if (isolated) { 447932ff6bbSJoonsoo Kim cc->nr_freepages += isolated; 448932ff6bbSJoonsoo Kim if (!strict && 449932ff6bbSJoonsoo Kim cc->nr_migratepages <= cc->nr_freepages) { 450932ff6bbSJoonsoo Kim blockpfn += isolated; 451932ff6bbSJoonsoo Kim break; 452932ff6bbSJoonsoo Kim } 453932ff6bbSJoonsoo Kim 454748446bbSMel Gorman blockpfn += isolated - 1; 455748446bbSMel Gorman cursor += isolated - 1; 4562af120bcSLaura Abbott continue; 457748446bbSMel Gorman } 4582af120bcSLaura Abbott 4592af120bcSLaura Abbott isolate_fail: 4602af120bcSLaura Abbott if (strict) 4612af120bcSLaura Abbott break; 4622af120bcSLaura Abbott else 4632af120bcSLaura Abbott continue; 4642af120bcSLaura Abbott 465748446bbSMel Gorman } 466748446bbSMel Gorman 4679fcd6d2eSVlastimil Babka /* 4689fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 4699fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 4709fcd6d2eSVlastimil Babka */ 4719fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 4729fcd6d2eSVlastimil Babka blockpfn = end_pfn; 4739fcd6d2eSVlastimil Babka 474e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 475e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 476e34d85f0SJoonsoo Kim 477e14c720eSVlastimil Babka /* Record how far we have got within the block */ 478e14c720eSVlastimil Babka *start_pfn = blockpfn; 479e14c720eSVlastimil Babka 480f40d1e42SMel Gorman /* 481f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 482f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 483f40d1e42SMel Gorman * returned and CMA will fail. 484f40d1e42SMel Gorman */ 4852af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 486f40d1e42SMel Gorman total_isolated = 0; 487f40d1e42SMel Gorman 488f40d1e42SMel Gorman if (locked) 489f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 490f40d1e42SMel Gorman 491bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 492bb13ffebSMel Gorman if (blockpfn == end_pfn) 493edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 494bb13ffebSMel Gorman 495010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 496397487dbSMel Gorman if (total_isolated) 497010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 498748446bbSMel Gorman return total_isolated; 499748446bbSMel Gorman } 500748446bbSMel Gorman 50185aa125fSMichal Nazarewicz /** 50285aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 50385aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 50485aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 50585aa125fSMichal Nazarewicz * 50685aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 50785aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 50885aa125fSMichal Nazarewicz * undo its actions and return zero. 50985aa125fSMichal Nazarewicz * 51085aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 51185aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 51285aa125fSMichal Nazarewicz * a free page). 51385aa125fSMichal Nazarewicz */ 514ff9543fdSMichal Nazarewicz unsigned long 515bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 516bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 51785aa125fSMichal Nazarewicz { 518e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 51985aa125fSMichal Nazarewicz LIST_HEAD(freelist); 52085aa125fSMichal Nazarewicz 5217d49d886SVlastimil Babka pfn = start_pfn; 522e1409c32SJoonsoo Kim block_start_pfn = pfn & ~(pageblock_nr_pages - 1); 523e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 524e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 52585aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 5267d49d886SVlastimil Babka 5277d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 528e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 5297d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 530e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 531e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5327d49d886SVlastimil Babka 53385aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 53485aa125fSMichal Nazarewicz 53558420016SJoonsoo Kim /* 53658420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 53758420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 53858420016SJoonsoo Kim * scanning range to right one. 53958420016SJoonsoo Kim */ 54058420016SJoonsoo Kim if (pfn >= block_end_pfn) { 541e1409c32SJoonsoo Kim block_start_pfn = pfn & ~(pageblock_nr_pages - 1); 54258420016SJoonsoo Kim block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 54358420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 54458420016SJoonsoo Kim } 54558420016SJoonsoo Kim 546e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 547e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 5487d49d886SVlastimil Babka break; 5497d49d886SVlastimil Babka 550e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 551e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 55285aa125fSMichal Nazarewicz 55385aa125fSMichal Nazarewicz /* 55485aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 55585aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 55685aa125fSMichal Nazarewicz * non-free pages). 55785aa125fSMichal Nazarewicz */ 55885aa125fSMichal Nazarewicz if (!isolated) 55985aa125fSMichal Nazarewicz break; 56085aa125fSMichal Nazarewicz 56185aa125fSMichal Nazarewicz /* 56285aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 56385aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 56485aa125fSMichal Nazarewicz * page may span two pageblocks). 56585aa125fSMichal Nazarewicz */ 56685aa125fSMichal Nazarewicz } 56785aa125fSMichal Nazarewicz 56885aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 56985aa125fSMichal Nazarewicz map_pages(&freelist); 57085aa125fSMichal Nazarewicz 57185aa125fSMichal Nazarewicz if (pfn < end_pfn) { 57285aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 57385aa125fSMichal Nazarewicz release_freepages(&freelist); 57485aa125fSMichal Nazarewicz return 0; 57585aa125fSMichal Nazarewicz } 57685aa125fSMichal Nazarewicz 57785aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 57885aa125fSMichal Nazarewicz return pfn; 57985aa125fSMichal Nazarewicz } 58085aa125fSMichal Nazarewicz 581748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 582edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 583748446bbSMel Gorman { 584748446bbSMel Gorman struct page *page; 585b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 586748446bbSMel Gorman 587edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 588edc2ca61SVlastimil Babka return; 589edc2ca61SVlastimil Babka 590b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 591b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 592748446bbSMel Gorman 593c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 594c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 595c67fe375SMel Gorman } 596748446bbSMel Gorman 597748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 598748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 599748446bbSMel Gorman { 600bc693045SMinchan Kim unsigned long active, inactive, isolated; 601748446bbSMel Gorman 602748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 603748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 604bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 605bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 606748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 607748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 608748446bbSMel Gorman 609bc693045SMinchan Kim return isolated > (inactive + active) / 2; 610748446bbSMel Gorman } 611748446bbSMel Gorman 6122fe86e00SMichal Nazarewicz /** 613edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 614edc2ca61SVlastimil Babka * a single pageblock 6152fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 616edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 617edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 618edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6192fe86e00SMichal Nazarewicz * 6202fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 621edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 622edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 623edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 624edc2ca61SVlastimil Babka * than end_pfn). 6252fe86e00SMichal Nazarewicz * 626edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 627edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 628edc2ca61SVlastimil Babka * is neither read nor updated. 629748446bbSMel Gorman */ 630edc2ca61SVlastimil Babka static unsigned long 631edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 632edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 633748446bbSMel Gorman { 634edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 635b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 636748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 637fa9add64SHugh Dickins struct lruvec *lruvec; 638b8b2d825SXiubo Li unsigned long flags = 0; 6392a1402aaSMel Gorman bool locked = false; 640bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 641e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 642748446bbSMel Gorman 643748446bbSMel Gorman /* 644748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 645748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 646748446bbSMel Gorman * delay for some time until fewer pages are isolated 647748446bbSMel Gorman */ 648748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 649f9e35b3bSMel Gorman /* async migration should just abort */ 650e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6512fe86e00SMichal Nazarewicz return 0; 652f9e35b3bSMel Gorman 653748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 654748446bbSMel Gorman 655748446bbSMel Gorman if (fatal_signal_pending(current)) 6562fe86e00SMichal Nazarewicz return 0; 657748446bbSMel Gorman } 658748446bbSMel Gorman 659be976572SVlastimil Babka if (compact_should_abort(cc)) 660aeef4b83SDavid Rientjes return 0; 661aeef4b83SDavid Rientjes 662748446bbSMel Gorman /* Time to isolate some pages for migration */ 663748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 66429c0dde8SVlastimil Babka bool is_lru; 66529c0dde8SVlastimil Babka 6668b44d279SVlastimil Babka /* 6678b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 6688b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 6698b44d279SVlastimil Babka * if contended. 6708b44d279SVlastimil Babka */ 6718b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 6728b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 6738b44d279SVlastimil Babka &locked, cc)) 6748b44d279SVlastimil Babka break; 675b2eef8c0SAndrea Arcangeli 676748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 677748446bbSMel Gorman continue; 678b7aba698SMel Gorman nr_scanned++; 679748446bbSMel Gorman 680748446bbSMel Gorman page = pfn_to_page(low_pfn); 681dc908600SMel Gorman 682bb13ffebSMel Gorman if (!valid_page) 683bb13ffebSMel Gorman valid_page = page; 684bb13ffebSMel Gorman 685c122b208SJoonsoo Kim /* 68699c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 68799c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 68899c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 68999c0fd5eSVlastimil Babka * potential isolation targets. 6906c14466cSMel Gorman */ 69199c0fd5eSVlastimil Babka if (PageBuddy(page)) { 69299c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 69399c0fd5eSVlastimil Babka 69499c0fd5eSVlastimil Babka /* 69599c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 69699c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 69799c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 69899c0fd5eSVlastimil Babka */ 69999c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 70099c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 701748446bbSMel Gorman continue; 70299c0fd5eSVlastimil Babka } 703748446bbSMel Gorman 7049927af74SMel Gorman /* 705bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 706bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 707bf6bddf1SRafael Aquini * Skip any other type of page 708bf6bddf1SRafael Aquini */ 70929c0dde8SVlastimil Babka is_lru = PageLRU(page); 71029c0dde8SVlastimil Babka if (!is_lru) { 711bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 712d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 713bf6bddf1SRafael Aquini /* Successfully isolated */ 714b6c75016SJoonsoo Kim goto isolate_success; 715bf6bddf1SRafael Aquini } 716bf6bddf1SRafael Aquini } 717bf6bddf1SRafael Aquini } 718bc835011SAndrea Arcangeli 719bc835011SAndrea Arcangeli /* 72029c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 72129c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 72229c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 72329c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 72429c0dde8SVlastimil Babka * danger is skipping too much. 725bc835011SAndrea Arcangeli */ 72629c0dde8SVlastimil Babka if (PageCompound(page)) { 72729c0dde8SVlastimil Babka unsigned int comp_order = compound_order(page); 72829c0dde8SVlastimil Babka 72929c0dde8SVlastimil Babka if (likely(comp_order < MAX_ORDER)) 73029c0dde8SVlastimil Babka low_pfn += (1UL << comp_order) - 1; 731edc2ca61SVlastimil Babka 7322a1402aaSMel Gorman continue; 7332a1402aaSMel Gorman } 7342a1402aaSMel Gorman 73529c0dde8SVlastimil Babka if (!is_lru) 73629c0dde8SVlastimil Babka continue; 73729c0dde8SVlastimil Babka 738119d6d59SDavid Rientjes /* 739119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 740119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 741119d6d59SDavid Rientjes * admittedly racy check. 742119d6d59SDavid Rientjes */ 743119d6d59SDavid Rientjes if (!page_mapping(page) && 744119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 745119d6d59SDavid Rientjes continue; 746119d6d59SDavid Rientjes 74769b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 74869b7189fSVlastimil Babka if (!locked) { 7498b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7508b44d279SVlastimil Babka &flags, cc); 7518b44d279SVlastimil Babka if (!locked) 7522a1402aaSMel Gorman break; 7532a1402aaSMel Gorman 75429c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 7552a1402aaSMel Gorman if (!PageLRU(page)) 7562a1402aaSMel Gorman continue; 75729c0dde8SVlastimil Babka 75829c0dde8SVlastimil Babka /* 75929c0dde8SVlastimil Babka * Page become compound since the non-locked check, 76029c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 76129c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 76229c0dde8SVlastimil Babka */ 76329c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 76429c0dde8SVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 765bc835011SAndrea Arcangeli continue; 766bc835011SAndrea Arcangeli } 76769b7189fSVlastimil Babka } 768bc835011SAndrea Arcangeli 769fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 770fa9add64SHugh Dickins 771748446bbSMel Gorman /* Try isolate the page */ 772edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 773748446bbSMel Gorman continue; 774748446bbSMel Gorman 77529c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 776bc835011SAndrea Arcangeli 777748446bbSMel Gorman /* Successfully isolated */ 778fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 779b6c75016SJoonsoo Kim 780b6c75016SJoonsoo Kim isolate_success: 781748446bbSMel Gorman list_add(&page->lru, migratelist); 782748446bbSMel Gorman cc->nr_migratepages++; 783b7aba698SMel Gorman nr_isolated++; 784748446bbSMel Gorman 785748446bbSMel Gorman /* Avoid isolating too much */ 78631b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 78731b8384aSHillf Danton ++low_pfn; 788748446bbSMel Gorman break; 789748446bbSMel Gorman } 79031b8384aSHillf Danton } 791748446bbSMel Gorman 79299c0fd5eSVlastimil Babka /* 79399c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 79499c0fd5eSVlastimil Babka * the range to be scanned. 79599c0fd5eSVlastimil Babka */ 79699c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 79799c0fd5eSVlastimil Babka low_pfn = end_pfn; 79899c0fd5eSVlastimil Babka 799c67fe375SMel Gorman if (locked) 800c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 801748446bbSMel Gorman 80250b5b094SVlastimil Babka /* 80350b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 80450b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 80550b5b094SVlastimil Babka */ 80635979ef3SDavid Rientjes if (low_pfn == end_pfn) 807edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 808bb13ffebSMel Gorman 809e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 810e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 811b7aba698SMel Gorman 812010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 813397487dbSMel Gorman if (nr_isolated) 814010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 815397487dbSMel Gorman 8162fe86e00SMichal Nazarewicz return low_pfn; 8172fe86e00SMichal Nazarewicz } 8182fe86e00SMichal Nazarewicz 819edc2ca61SVlastimil Babka /** 820edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 821edc2ca61SVlastimil Babka * @cc: Compaction control structure. 822edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 823edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 824edc2ca61SVlastimil Babka * 825edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 826edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 827edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 828edc2ca61SVlastimil Babka */ 829edc2ca61SVlastimil Babka unsigned long 830edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 831edc2ca61SVlastimil Babka unsigned long end_pfn) 832edc2ca61SVlastimil Babka { 833e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 834edc2ca61SVlastimil Babka 835edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 836edc2ca61SVlastimil Babka pfn = start_pfn; 837e1409c32SJoonsoo Kim block_start_pfn = pfn & ~(pageblock_nr_pages - 1); 838e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 839e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 840edc2ca61SVlastimil Babka block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 841edc2ca61SVlastimil Babka 842edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 843e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 844edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 845edc2ca61SVlastimil Babka 846edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 847edc2ca61SVlastimil Babka 848e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 849e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 850edc2ca61SVlastimil Babka continue; 851edc2ca61SVlastimil Babka 852edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 853edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 854edc2ca61SVlastimil Babka 855edc2ca61SVlastimil Babka /* 856edc2ca61SVlastimil Babka * In case of fatal failure, release everything that might 857edc2ca61SVlastimil Babka * have been isolated in the previous iteration, and signal 858edc2ca61SVlastimil Babka * the failure back to caller. 859edc2ca61SVlastimil Babka */ 860edc2ca61SVlastimil Babka if (!pfn) { 861edc2ca61SVlastimil Babka putback_movable_pages(&cc->migratepages); 862edc2ca61SVlastimil Babka cc->nr_migratepages = 0; 863edc2ca61SVlastimil Babka break; 864edc2ca61SVlastimil Babka } 8656ea41c0cSJoonsoo Kim 8666ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8676ea41c0cSJoonsoo Kim break; 868edc2ca61SVlastimil Babka } 869edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 870edc2ca61SVlastimil Babka 871edc2ca61SVlastimil Babka return pfn; 872edc2ca61SVlastimil Babka } 873edc2ca61SVlastimil Babka 874ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 875ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 876018e9a49SAndrew Morton 877018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 878018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page) 879018e9a49SAndrew Morton { 880018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 881018e9a49SAndrew Morton if (PageBuddy(page)) { 882018e9a49SAndrew Morton /* 883018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 884018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 885018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 886018e9a49SAndrew Morton */ 887018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 888018e9a49SAndrew Morton return false; 889018e9a49SAndrew Morton } 890018e9a49SAndrew Morton 891018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 892018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 893018e9a49SAndrew Morton return true; 894018e9a49SAndrew Morton 895018e9a49SAndrew Morton /* Otherwise skip the block */ 896018e9a49SAndrew Morton return false; 897018e9a49SAndrew Morton } 898018e9a49SAndrew Morton 899ff9543fdSMichal Nazarewicz /* 900f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 901f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 902f2849aa0SVlastimil Babka */ 903f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 904f2849aa0SVlastimil Babka { 905f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 906f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 907f2849aa0SVlastimil Babka } 908f2849aa0SVlastimil Babka 909f2849aa0SVlastimil Babka /* 910ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 911ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 912ff9543fdSMichal Nazarewicz */ 913edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 914ff9543fdSMichal Nazarewicz { 915edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 916ff9543fdSMichal Nazarewicz struct page *page; 917c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 918e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 919c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 920c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 921ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9222fe86e00SMichal Nazarewicz 923ff9543fdSMichal Nazarewicz /* 924ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 92549e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 926e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 927e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 928c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 929c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 930c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 93149e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 93249e068f0SVlastimil Babka * is using. 933ff9543fdSMichal Nazarewicz */ 934e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 935c96b9e50SVlastimil Babka block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); 936c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 937c96b9e50SVlastimil Babka zone_end_pfn(zone)); 9387ed695e0SVlastimil Babka low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); 9392fe86e00SMichal Nazarewicz 940ff9543fdSMichal Nazarewicz /* 941ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 942ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 943ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 944ff9543fdSMichal Nazarewicz */ 945f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 946c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 947e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 948e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 949ff9543fdSMichal Nazarewicz 950f6ea3adbSDavid Rientjes /* 951f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 952f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 953be976572SVlastimil Babka * to schedule, or even abort async compaction. 954f6ea3adbSDavid Rientjes */ 955be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 956be976572SVlastimil Babka && compact_should_abort(cc)) 957be976572SVlastimil Babka break; 958f6ea3adbSDavid Rientjes 9597d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 9607d49d886SVlastimil Babka zone); 9617d49d886SVlastimil Babka if (!page) 962ff9543fdSMichal Nazarewicz continue; 963ff9543fdSMichal Nazarewicz 964ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 96568e3e926SLinus Torvalds if (!suitable_migration_target(page)) 966ff9543fdSMichal Nazarewicz continue; 96768e3e926SLinus Torvalds 968bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 969bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 970bb13ffebSMel Gorman continue; 971bb13ffebSMel Gorman 972e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 973932ff6bbSJoonsoo Kim isolate_freepages_block(cc, &isolate_start_pfn, 974c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 975ff9543fdSMichal Nazarewicz 976ff9543fdSMichal Nazarewicz /* 977f5f61a32SVlastimil Babka * If we isolated enough freepages, or aborted due to async 978f5f61a32SVlastimil Babka * compaction being contended, terminate the loop. 979e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 980e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 981e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 982e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 983e14c720eSVlastimil Babka * pageblock. 984e14c720eSVlastimil Babka * In that case we will however want to restart at the start 985e14c720eSVlastimil Babka * of the previous pageblock. 986e14c720eSVlastimil Babka */ 987f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 988f5f61a32SVlastimil Babka || cc->contended) { 989f5f61a32SVlastimil Babka if (isolate_start_pfn >= block_end_pfn) 990f5f61a32SVlastimil Babka isolate_start_pfn = 991e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 992be976572SVlastimil Babka break; 993f5f61a32SVlastimil Babka } else { 994f5f61a32SVlastimil Babka /* 995f5f61a32SVlastimil Babka * isolate_freepages_block() should not terminate 996f5f61a32SVlastimil Babka * prematurely unless contended, or isolated enough 997f5f61a32SVlastimil Babka */ 998f5f61a32SVlastimil Babka VM_BUG_ON(isolate_start_pfn < block_end_pfn); 999f5f61a32SVlastimil Babka } 1000c89511abSMel Gorman } 1001ff9543fdSMichal Nazarewicz 1002ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 1003ff9543fdSMichal Nazarewicz map_pages(freelist); 1004ff9543fdSMichal Nazarewicz 10057ed695e0SVlastimil Babka /* 1006f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1007f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1008f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1009f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 10107ed695e0SVlastimil Babka */ 1011f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1012748446bbSMel Gorman } 1013748446bbSMel Gorman 1014748446bbSMel Gorman /* 1015748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1016748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1017748446bbSMel Gorman */ 1018748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1019748446bbSMel Gorman unsigned long data, 1020748446bbSMel Gorman int **result) 1021748446bbSMel Gorman { 1022748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1023748446bbSMel Gorman struct page *freepage; 1024748446bbSMel Gorman 1025be976572SVlastimil Babka /* 1026be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1027be976572SVlastimil Babka * contention. 1028be976572SVlastimil Babka */ 1029748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1030be976572SVlastimil Babka if (!cc->contended) 1031edc2ca61SVlastimil Babka isolate_freepages(cc); 1032748446bbSMel Gorman 1033748446bbSMel Gorman if (list_empty(&cc->freepages)) 1034748446bbSMel Gorman return NULL; 1035748446bbSMel Gorman } 1036748446bbSMel Gorman 1037748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1038748446bbSMel Gorman list_del(&freepage->lru); 1039748446bbSMel Gorman cc->nr_freepages--; 1040748446bbSMel Gorman 1041748446bbSMel Gorman return freepage; 1042748446bbSMel Gorman } 1043748446bbSMel Gorman 1044748446bbSMel Gorman /* 1045d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1046d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1047d53aea3dSDavid Rientjes * special handling needed for NUMA. 1048d53aea3dSDavid Rientjes */ 1049d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1050d53aea3dSDavid Rientjes { 1051d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1052d53aea3dSDavid Rientjes 1053d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1054d53aea3dSDavid Rientjes cc->nr_freepages++; 1055d53aea3dSDavid Rientjes } 1056d53aea3dSDavid Rientjes 1057ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1058ff9543fdSMichal Nazarewicz typedef enum { 1059ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1060ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1061ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1062ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1063ff9543fdSMichal Nazarewicz 1064ff9543fdSMichal Nazarewicz /* 10655bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 10665bbe3547SEric B Munson * compactable pages. 10675bbe3547SEric B Munson */ 10685bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 10695bbe3547SEric B Munson 10705bbe3547SEric B Munson /* 1071edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1072edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1073edc2ca61SVlastimil Babka * compact_control. 1074ff9543fdSMichal Nazarewicz */ 1075ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1076ff9543fdSMichal Nazarewicz struct compact_control *cc) 1077ff9543fdSMichal Nazarewicz { 1078e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1079e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1080e1409c32SJoonsoo Kim unsigned long low_pfn; 10811a16718cSJoonsoo Kim unsigned long isolate_start_pfn; 1082edc2ca61SVlastimil Babka struct page *page; 1083edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 10845bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 1085edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1086ff9543fdSMichal Nazarewicz 1087edc2ca61SVlastimil Babka /* 1088edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1089edc2ca61SVlastimil Babka * initialized by compact_zone() 1090edc2ca61SVlastimil Babka */ 1091edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 1092e1409c32SJoonsoo Kim block_start_pfn = cc->migrate_pfn & ~(pageblock_nr_pages - 1); 1093e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1094e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1095ff9543fdSMichal Nazarewicz 1096ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 1097e1409c32SJoonsoo Kim block_end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); 1098ff9543fdSMichal Nazarewicz 1099edc2ca61SVlastimil Babka /* 1100edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1101edc2ca61SVlastimil Babka * Do not cross the free scanner. 1102edc2ca61SVlastimil Babka */ 1103e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 1104e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1105e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1106e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1107edc2ca61SVlastimil Babka 1108edc2ca61SVlastimil Babka /* 1109edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1110edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1111edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1112edc2ca61SVlastimil Babka */ 1113edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1114edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1115edc2ca61SVlastimil Babka break; 1116edc2ca61SVlastimil Babka 1117e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1118e1409c32SJoonsoo Kim zone); 11197d49d886SVlastimil Babka if (!page) 1120edc2ca61SVlastimil Babka continue; 1121edc2ca61SVlastimil Babka 1122edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1123edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1124edc2ca61SVlastimil Babka continue; 1125edc2ca61SVlastimil Babka 1126edc2ca61SVlastimil Babka /* 1127edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1128edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1129edc2ca61SVlastimil Babka * of work satisfies the allocation. 1130edc2ca61SVlastimil Babka */ 1131edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1132edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1133edc2ca61SVlastimil Babka continue; 1134ff9543fdSMichal Nazarewicz 1135ff9543fdSMichal Nazarewicz /* Perform the isolation */ 11361a16718cSJoonsoo Kim isolate_start_pfn = low_pfn; 1137e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1138e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1139edc2ca61SVlastimil Babka 1140ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1141ff59909aSHugh Dickins acct_isolated(zone, cc); 1142ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1143ff59909aSHugh Dickins } 1144ff9543fdSMichal Nazarewicz 1145edc2ca61SVlastimil Babka /* 11461a16718cSJoonsoo Kim * Record where we could have freed pages by migration and not 11471a16718cSJoonsoo Kim * yet flushed them to buddy allocator. 11481a16718cSJoonsoo Kim * - this is the lowest page that could have been isolated and 11491a16718cSJoonsoo Kim * then freed by migration. 11501a16718cSJoonsoo Kim */ 11511a16718cSJoonsoo Kim if (cc->nr_migratepages && !cc->last_migrated_pfn) 11521a16718cSJoonsoo Kim cc->last_migrated_pfn = isolate_start_pfn; 11531a16718cSJoonsoo Kim 11541a16718cSJoonsoo Kim /* 1155edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1156edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1157edc2ca61SVlastimil Babka * continue or not. 1158edc2ca61SVlastimil Babka */ 1159edc2ca61SVlastimil Babka break; 1160edc2ca61SVlastimil Babka } 1161edc2ca61SVlastimil Babka 1162edc2ca61SVlastimil Babka acct_isolated(zone, cc); 1163f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1164f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1165ff9543fdSMichal Nazarewicz 1166edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1167ff9543fdSMichal Nazarewicz } 1168ff9543fdSMichal Nazarewicz 116921c527a3SYaowei Bai /* 117021c527a3SYaowei Bai * order == -1 is expected when compacting via 117121c527a3SYaowei Bai * /proc/sys/vm/compact_memory 117221c527a3SYaowei Bai */ 117321c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 117421c527a3SYaowei Bai { 117521c527a3SYaowei Bai return order == -1; 117621c527a3SYaowei Bai } 117721c527a3SYaowei Bai 1178837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc, 11796d7ce559SDavid Rientjes const int migratetype) 1180748446bbSMel Gorman { 11818fb74b9fSMel Gorman unsigned int order; 11825a03b051SAndrea Arcangeli unsigned long watermark; 118356de7263SMel Gorman 1184be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 11852d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1186748446bbSMel Gorman 1187753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1188f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 118955b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 119002333641SVlastimil Babka reset_cached_positions(zone); 119155b7c4c9SVlastimil Babka 119262997027SMel Gorman /* 119362997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 119462997027SMel Gorman * by kswapd when it goes to sleep. kswapd does not set the 119562997027SMel Gorman * flag itself as the decision to be clear should be directly 119662997027SMel Gorman * based on an allocation request. 119762997027SMel Gorman */ 119862997027SMel Gorman if (!current_is_kswapd()) 119962997027SMel Gorman zone->compact_blockskip_flush = true; 120062997027SMel Gorman 1201748446bbSMel Gorman return COMPACT_COMPLETE; 1202bb13ffebSMel Gorman } 1203748446bbSMel Gorman 120421c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 120556de7263SMel Gorman return COMPACT_CONTINUE; 120656de7263SMel Gorman 12073957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 12083957c776SMichal Hocko watermark = low_wmark_pages(zone); 12093957c776SMichal Hocko 1210ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1211ebff3980SVlastimil Babka cc->alloc_flags)) 12123957c776SMichal Hocko return COMPACT_CONTINUE; 12133957c776SMichal Hocko 121456de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 121556de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 12168fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 12172149cdaeSJoonsoo Kim bool can_steal; 12188fb74b9fSMel Gorman 121956de7263SMel Gorman /* Job done if page is free of the right migratetype */ 12206d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 122156de7263SMel Gorman return COMPACT_PARTIAL; 122256de7263SMel Gorman 12232149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 12242149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 12252149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 12262149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 12272149cdaeSJoonsoo Kim return COMPACT_PARTIAL; 12282149cdaeSJoonsoo Kim #endif 12292149cdaeSJoonsoo Kim /* 12302149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 12312149cdaeSJoonsoo Kim * other migratetype buddy lists. 12322149cdaeSJoonsoo Kim */ 12332149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 12342149cdaeSJoonsoo Kim true, &can_steal) != -1) 123556de7263SMel Gorman return COMPACT_PARTIAL; 123656de7263SMel Gorman } 123756de7263SMel Gorman 1238837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1239837d026dSJoonsoo Kim } 1240837d026dSJoonsoo Kim 1241837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc, 1242837d026dSJoonsoo Kim const int migratetype) 1243837d026dSJoonsoo Kim { 1244837d026dSJoonsoo Kim int ret; 1245837d026dSJoonsoo Kim 1246837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1247837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1248837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1249837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1250837d026dSJoonsoo Kim 1251837d026dSJoonsoo Kim return ret; 1252748446bbSMel Gorman } 1253748446bbSMel Gorman 12543e7d3449SMel Gorman /* 12553e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 12563e7d3449SMel Gorman * Returns 12573e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 12583e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 12593e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 12603e7d3449SMel Gorman */ 1261837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order, 1262ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 12633e7d3449SMel Gorman { 12643e7d3449SMel Gorman int fragindex; 12653e7d3449SMel Gorman unsigned long watermark; 12663e7d3449SMel Gorman 126721c527a3SYaowei Bai if (is_via_compact_memory(order)) 12683957c776SMichal Hocko return COMPACT_CONTINUE; 12693957c776SMichal Hocko 1270ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1271ebff3980SVlastimil Babka /* 1272ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1273ebff3980SVlastimil Babka * should be no need for compaction at all. 1274ebff3980SVlastimil Babka */ 1275ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1276ebff3980SVlastimil Babka alloc_flags)) 1277ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1278ebff3980SVlastimil Babka 12793957c776SMichal Hocko /* 12803e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 12813e7d3449SMel Gorman * This is because during migration, copies of pages need to be 12823e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 12833e7d3449SMel Gorman */ 1284ebff3980SVlastimil Babka watermark += (2UL << order); 1285ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 12863e7d3449SMel Gorman return COMPACT_SKIPPED; 12873e7d3449SMel Gorman 12883e7d3449SMel Gorman /* 12893e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 12903e7d3449SMel Gorman * low memory or external fragmentation 12913e7d3449SMel Gorman * 1292ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1293ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 12943e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 12953e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 12963e7d3449SMel Gorman * 12973e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 12983e7d3449SMel Gorman */ 12993e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 13003e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1301837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 13023e7d3449SMel Gorman 13033e7d3449SMel Gorman return COMPACT_CONTINUE; 13043e7d3449SMel Gorman } 13053e7d3449SMel Gorman 1306837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order, 1307837d026dSJoonsoo Kim int alloc_flags, int classzone_idx) 1308837d026dSJoonsoo Kim { 1309837d026dSJoonsoo Kim unsigned long ret; 1310837d026dSJoonsoo Kim 1311837d026dSJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx); 1312837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1313837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1314837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1315837d026dSJoonsoo Kim 1316837d026dSJoonsoo Kim return ret; 1317837d026dSJoonsoo Kim } 1318837d026dSJoonsoo Kim 1319748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1320748446bbSMel Gorman { 1321748446bbSMel Gorman int ret; 1322c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1323108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 13246d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1325e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1326748446bbSMel Gorman 1327ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1328ebff3980SVlastimil Babka cc->classzone_idx); 13293e7d3449SMel Gorman switch (ret) { 13303e7d3449SMel Gorman case COMPACT_PARTIAL: 13313e7d3449SMel Gorman case COMPACT_SKIPPED: 13323e7d3449SMel Gorman /* Compaction is likely to fail */ 13333e7d3449SMel Gorman return ret; 13343e7d3449SMel Gorman case COMPACT_CONTINUE: 13353e7d3449SMel Gorman /* Fall through to compaction */ 13363e7d3449SMel Gorman ; 13373e7d3449SMel Gorman } 13383e7d3449SMel Gorman 1339c89511abSMel Gorman /* 1340d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1341d3132e4bSVlastimil Babka * is about to be retried after being deferred. kswapd does not do 1342d3132e4bSVlastimil Babka * this reset as it'll reset the cached information when going to sleep. 1343d3132e4bSVlastimil Babka */ 1344d3132e4bSVlastimil Babka if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) 1345d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1346d3132e4bSVlastimil Babka 1347d3132e4bSVlastimil Babka /* 1348c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1349c89511abSMel Gorman * information on where the scanners should start but check that it 1350c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1351c89511abSMel Gorman */ 1352e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1353c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1354623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 1355623446e4SJoonsoo Kim cc->free_pfn = round_down(end_pfn - 1, pageblock_nr_pages); 1356c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1357c89511abSMel Gorman } 1358623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1359c89511abSMel Gorman cc->migrate_pfn = start_pfn; 136035979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 136135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1362c89511abSMel Gorman } 13631a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1364748446bbSMel Gorman 136516c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 136616c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 13670eb927c0SMel Gorman 1368748446bbSMel Gorman migrate_prep_local(); 1369748446bbSMel Gorman 13706d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 13716d7ce559SDavid Rientjes COMPACT_CONTINUE) { 13729d502c1cSMinchan Kim int err; 1373748446bbSMel Gorman 1374f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1375f9e35b3bSMel Gorman case ISOLATE_ABORT: 13762d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 13775733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1378e64c5237SShaohua Li cc->nr_migratepages = 0; 1379f9e35b3bSMel Gorman goto out; 1380f9e35b3bSMel Gorman case ISOLATE_NONE: 1381fdaf7f5cSVlastimil Babka /* 1382fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1383fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1384fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1385fdaf7f5cSVlastimil Babka */ 1386fdaf7f5cSVlastimil Babka goto check_drain; 1387f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1388f9e35b3bSMel Gorman ; 1389f9e35b3bSMel Gorman } 1390748446bbSMel Gorman 1391d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1392e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 13937b2a2d4aSMel Gorman MR_COMPACTION); 1394748446bbSMel Gorman 1395f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1396f8c9301fSVlastimil Babka &cc->migratepages); 1397748446bbSMel Gorman 1398f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1399f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 14009d502c1cSMinchan Kim if (err) { 14015733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 14027ed695e0SVlastimil Babka /* 14037ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 14047ed695e0SVlastimil Babka * and we want compact_finished() to detect it 14057ed695e0SVlastimil Babka */ 1406f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 14072d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14084bf2bba3SDavid Rientjes goto out; 1409748446bbSMel Gorman } 14104bf2bba3SDavid Rientjes } 1411fdaf7f5cSVlastimil Babka 1412fdaf7f5cSVlastimil Babka check_drain: 1413fdaf7f5cSVlastimil Babka /* 1414fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1415fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1416fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1417fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1418fdaf7f5cSVlastimil Babka * would succeed. 1419fdaf7f5cSVlastimil Babka */ 14201a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1421fdaf7f5cSVlastimil Babka int cpu; 1422fdaf7f5cSVlastimil Babka unsigned long current_block_start = 1423fdaf7f5cSVlastimil Babka cc->migrate_pfn & ~((1UL << cc->order) - 1); 1424fdaf7f5cSVlastimil Babka 14251a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1426fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1427fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1428fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1429fdaf7f5cSVlastimil Babka put_cpu(); 1430fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 14311a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1432fdaf7f5cSVlastimil Babka } 1433fdaf7f5cSVlastimil Babka } 1434fdaf7f5cSVlastimil Babka 1435748446bbSMel Gorman } 1436748446bbSMel Gorman 1437f9e35b3bSMel Gorman out: 14386bace090SVlastimil Babka /* 14396bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 14406bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 14416bace090SVlastimil Babka */ 14426bace090SVlastimil Babka if (cc->nr_freepages > 0) { 14436bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 14446bace090SVlastimil Babka 14456bace090SVlastimil Babka cc->nr_freepages = 0; 14466bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 14476bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 14486bace090SVlastimil Babka free_pfn &= ~(pageblock_nr_pages-1); 14496bace090SVlastimil Babka /* 14506bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 14516bace090SVlastimil Babka * already reset to zone end in compact_finished() 14526bace090SVlastimil Babka */ 14536bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 14546bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 14556bace090SVlastimil Babka } 1456748446bbSMel Gorman 145716c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 145816c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 14590eb927c0SMel Gorman 14602d1e1041SVlastimil Babka if (ret == COMPACT_CONTENDED) 14612d1e1041SVlastimil Babka ret = COMPACT_PARTIAL; 14622d1e1041SVlastimil Babka 1463748446bbSMel Gorman return ret; 1464748446bbSMel Gorman } 146576ab0f53SMel Gorman 1466e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1467ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1468ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 146956de7263SMel Gorman { 1470e64c5237SShaohua Li unsigned long ret; 147156de7263SMel Gorman struct compact_control cc = { 147256de7263SMel Gorman .nr_freepages = 0, 147356de7263SMel Gorman .nr_migratepages = 0, 147456de7263SMel Gorman .order = order, 14756d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 147656de7263SMel Gorman .zone = zone, 1477e0b9daebSDavid Rientjes .mode = mode, 1478ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1479ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 148056de7263SMel Gorman }; 148156de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 148256de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 148356de7263SMel Gorman 1484e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1485e64c5237SShaohua Li 1486e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1487e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1488e64c5237SShaohua Li 1489e64c5237SShaohua Li *contended = cc.contended; 1490e64c5237SShaohua Li return ret; 149156de7263SMel Gorman } 149256de7263SMel Gorman 14935e771905SMel Gorman int sysctl_extfrag_threshold = 500; 14945e771905SMel Gorman 149556de7263SMel Gorman /** 149656de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 149756de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 14981a6d53a1SVlastimil Babka * @order: The order of the current allocation 14991a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 15001a6d53a1SVlastimil Babka * @ac: The context of current allocation 1501e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 15021f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 15031f9efdefSVlastimil Babka * need_resched() or lock contention 150456de7263SMel Gorman * 150556de7263SMel Gorman * This is the main entry point for direct page compaction. 150656de7263SMel Gorman */ 15071a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 15081a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 15091a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 151056de7263SMel Gorman { 151156de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 151256de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 151356de7263SMel Gorman struct zoneref *z; 151456de7263SMel Gorman struct zone *zone; 151553853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 15161f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 15171f9efdefSVlastimil Babka 15181f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 151956de7263SMel Gorman 15204ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1521c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 152253853e2dSVlastimil Babka return COMPACT_SKIPPED; 152356de7263SMel Gorman 1524837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1525837d026dSJoonsoo Kim 152656de7263SMel Gorman /* Compact each zone in the list */ 15271a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 15281a6d53a1SVlastimil Babka ac->nodemask) { 152956de7263SMel Gorman int status; 15301f9efdefSVlastimil Babka int zone_contended; 153156de7263SMel Gorman 153253853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 153353853e2dSVlastimil Babka continue; 153453853e2dSVlastimil Babka 1535e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 15361a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 15371a6d53a1SVlastimil Babka ac->classzone_idx); 153856de7263SMel Gorman rc = max(status, rc); 15391f9efdefSVlastimil Babka /* 15401f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 15411f9efdefSVlastimil Babka * to clear all_zones_contended. 15421f9efdefSVlastimil Babka */ 15431f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 154456de7263SMel Gorman 15453e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1546ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 15471a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 154853853e2dSVlastimil Babka /* 154953853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 155053853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 155153853e2dSVlastimil Babka * will repeat this with true if allocation indeed 155253853e2dSVlastimil Babka * succeeds in this zone. 155353853e2dSVlastimil Babka */ 155453853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 15551f9efdefSVlastimil Babka /* 15561f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 15571f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 15581f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 15591f9efdefSVlastimil Babka * however still fail so we better signal the 15601f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 15611f9efdefSVlastimil Babka * prevent the allocation attempt). 15621f9efdefSVlastimil Babka */ 15631f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 15641f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15651f9efdefSVlastimil Babka 15661f9efdefSVlastimil Babka goto break_loop; 15671f9efdefSVlastimil Babka } 15681f9efdefSVlastimil Babka 1569f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 157053853e2dSVlastimil Babka /* 157153853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 157253853e2dSVlastimil Babka * so we defer compaction there. If it ends up 157353853e2dSVlastimil Babka * succeeding after all, it will be reset. 157453853e2dSVlastimil Babka */ 157553853e2dSVlastimil Babka defer_compaction(zone, order); 157653853e2dSVlastimil Babka } 15771f9efdefSVlastimil Babka 15781f9efdefSVlastimil Babka /* 15791f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 15801f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 15811f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 15821f9efdefSVlastimil Babka * contention. 15831f9efdefSVlastimil Babka */ 15841f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 15851f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 15861f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15871f9efdefSVlastimil Babka goto break_loop; 158856de7263SMel Gorman } 158956de7263SMel Gorman 15901f9efdefSVlastimil Babka continue; 15911f9efdefSVlastimil Babka break_loop: 15921f9efdefSVlastimil Babka /* 15931f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 15941f9efdefSVlastimil Babka * and assume they are not all lock contended. 15951f9efdefSVlastimil Babka */ 15961f9efdefSVlastimil Babka all_zones_contended = 0; 15971f9efdefSVlastimil Babka break; 15981f9efdefSVlastimil Babka } 15991f9efdefSVlastimil Babka 16001f9efdefSVlastimil Babka /* 16011f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 16021f9efdefSVlastimil Babka * zones that were tried were lock contended. 16031f9efdefSVlastimil Babka */ 16041f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 16051f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 16061f9efdefSVlastimil Babka 160756de7263SMel Gorman return rc; 160856de7263SMel Gorman } 160956de7263SMel Gorman 161056de7263SMel Gorman 161176ab0f53SMel Gorman /* Compact all zones within a node */ 16127103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 161376ab0f53SMel Gorman { 161476ab0f53SMel Gorman int zoneid; 161576ab0f53SMel Gorman struct zone *zone; 161676ab0f53SMel Gorman 161776ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 161876ab0f53SMel Gorman 161976ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 162076ab0f53SMel Gorman if (!populated_zone(zone)) 162176ab0f53SMel Gorman continue; 162276ab0f53SMel Gorman 16237be62de9SRik van Riel cc->nr_freepages = 0; 16247be62de9SRik van Riel cc->nr_migratepages = 0; 16257be62de9SRik van Riel cc->zone = zone; 16267be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 16277be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 162876ab0f53SMel Gorman 1629195b0c60SGioh Kim /* 1630195b0c60SGioh Kim * When called via /proc/sys/vm/compact_memory 1631195b0c60SGioh Kim * this makes sure we compact the whole zone regardless of 1632195b0c60SGioh Kim * cached scanner positions. 1633195b0c60SGioh Kim */ 163421c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 1635195b0c60SGioh Kim __reset_isolation_suitable(zone); 1636195b0c60SGioh Kim 163721c527a3SYaowei Bai if (is_via_compact_memory(cc->order) || 163821c527a3SYaowei Bai !compaction_deferred(zone, cc->order)) 16397be62de9SRik van Riel compact_zone(zone, cc); 164076ab0f53SMel Gorman 164175469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->freepages)); 164275469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->migratepages)); 164375469345SJoonsoo Kim 164475469345SJoonsoo Kim if (is_via_compact_memory(cc->order)) 164575469345SJoonsoo Kim continue; 164675469345SJoonsoo Kim 1647de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1648de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1649de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1650aff62249SRik van Riel } 165176ab0f53SMel Gorman } 165276ab0f53SMel Gorman 16537103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 16547be62de9SRik van Riel { 16557be62de9SRik van Riel struct compact_control cc = { 16567be62de9SRik van Riel .order = order, 1657e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 16587be62de9SRik van Riel }; 16597be62de9SRik van Riel 16603a7200afSMel Gorman if (!order) 16613a7200afSMel Gorman return; 16623a7200afSMel Gorman 16637103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 16647be62de9SRik van Riel } 16657be62de9SRik van Riel 16667103f16dSAndrew Morton static void compact_node(int nid) 16677be62de9SRik van Riel { 16687be62de9SRik van Riel struct compact_control cc = { 16697be62de9SRik van Riel .order = -1, 1670e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 167191ca9186SDavid Rientjes .ignore_skip_hint = true, 16727be62de9SRik van Riel }; 16737be62de9SRik van Riel 16747103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 16757be62de9SRik van Riel } 16767be62de9SRik van Riel 167776ab0f53SMel Gorman /* Compact all nodes in the system */ 16787964c06dSJason Liu static void compact_nodes(void) 167976ab0f53SMel Gorman { 168076ab0f53SMel Gorman int nid; 168176ab0f53SMel Gorman 16828575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 16838575ec29SHugh Dickins lru_add_drain_all(); 16848575ec29SHugh Dickins 168576ab0f53SMel Gorman for_each_online_node(nid) 168676ab0f53SMel Gorman compact_node(nid); 168776ab0f53SMel Gorman } 168876ab0f53SMel Gorman 168976ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 169076ab0f53SMel Gorman int sysctl_compact_memory; 169176ab0f53SMel Gorman 1692fec4eb2cSYaowei Bai /* 1693fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1694fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1695fec4eb2cSYaowei Bai */ 169676ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 169776ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 169876ab0f53SMel Gorman { 169976ab0f53SMel Gorman if (write) 17007964c06dSJason Liu compact_nodes(); 170176ab0f53SMel Gorman 170276ab0f53SMel Gorman return 0; 170376ab0f53SMel Gorman } 1704ed4a6d7fSMel Gorman 17055e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 17065e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 17075e771905SMel Gorman { 17085e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 17095e771905SMel Gorman 17105e771905SMel Gorman return 0; 17115e771905SMel Gorman } 17125e771905SMel Gorman 1713ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 171474e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 171510fbcf4cSKay Sievers struct device_attribute *attr, 1716ed4a6d7fSMel Gorman const char *buf, size_t count) 1717ed4a6d7fSMel Gorman { 17188575ec29SHugh Dickins int nid = dev->id; 17198575ec29SHugh Dickins 17208575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 17218575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17228575ec29SHugh Dickins lru_add_drain_all(); 17238575ec29SHugh Dickins 17248575ec29SHugh Dickins compact_node(nid); 17258575ec29SHugh Dickins } 1726ed4a6d7fSMel Gorman 1727ed4a6d7fSMel Gorman return count; 1728ed4a6d7fSMel Gorman } 172910fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1730ed4a6d7fSMel Gorman 1731ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1732ed4a6d7fSMel Gorman { 173310fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1734ed4a6d7fSMel Gorman } 1735ed4a6d7fSMel Gorman 1736ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1737ed4a6d7fSMel Gorman { 173810fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1739ed4a6d7fSMel Gorman } 1740ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1741ff9543fdSMichal Nazarewicz 1742*698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 1743*698b1b30SVlastimil Babka { 1744*698b1b30SVlastimil Babka return pgdat->kcompactd_max_order > 0; 1745*698b1b30SVlastimil Babka } 1746*698b1b30SVlastimil Babka 1747*698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 1748*698b1b30SVlastimil Babka { 1749*698b1b30SVlastimil Babka int zoneid; 1750*698b1b30SVlastimil Babka struct zone *zone; 1751*698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 1752*698b1b30SVlastimil Babka 1753*698b1b30SVlastimil Babka for (zoneid = 0; zoneid < classzone_idx; zoneid++) { 1754*698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1755*698b1b30SVlastimil Babka 1756*698b1b30SVlastimil Babka if (!populated_zone(zone)) 1757*698b1b30SVlastimil Babka continue; 1758*698b1b30SVlastimil Babka 1759*698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 1760*698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 1761*698b1b30SVlastimil Babka return true; 1762*698b1b30SVlastimil Babka } 1763*698b1b30SVlastimil Babka 1764*698b1b30SVlastimil Babka return false; 1765*698b1b30SVlastimil Babka } 1766*698b1b30SVlastimil Babka 1767*698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 1768*698b1b30SVlastimil Babka { 1769*698b1b30SVlastimil Babka /* 1770*698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 1771*698b1b30SVlastimil Babka * order is allocatable. 1772*698b1b30SVlastimil Babka */ 1773*698b1b30SVlastimil Babka int zoneid; 1774*698b1b30SVlastimil Babka struct zone *zone; 1775*698b1b30SVlastimil Babka struct compact_control cc = { 1776*698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 1777*698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 1778*698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 1779*698b1b30SVlastimil Babka .ignore_skip_hint = true, 1780*698b1b30SVlastimil Babka 1781*698b1b30SVlastimil Babka }; 1782*698b1b30SVlastimil Babka bool success = false; 1783*698b1b30SVlastimil Babka 1784*698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 1785*698b1b30SVlastimil Babka cc.classzone_idx); 1786*698b1b30SVlastimil Babka count_vm_event(KCOMPACTD_WAKE); 1787*698b1b30SVlastimil Babka 1788*698b1b30SVlastimil Babka for (zoneid = 0; zoneid < cc.classzone_idx; zoneid++) { 1789*698b1b30SVlastimil Babka int status; 1790*698b1b30SVlastimil Babka 1791*698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1792*698b1b30SVlastimil Babka if (!populated_zone(zone)) 1793*698b1b30SVlastimil Babka continue; 1794*698b1b30SVlastimil Babka 1795*698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 1796*698b1b30SVlastimil Babka continue; 1797*698b1b30SVlastimil Babka 1798*698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 1799*698b1b30SVlastimil Babka COMPACT_CONTINUE) 1800*698b1b30SVlastimil Babka continue; 1801*698b1b30SVlastimil Babka 1802*698b1b30SVlastimil Babka cc.nr_freepages = 0; 1803*698b1b30SVlastimil Babka cc.nr_migratepages = 0; 1804*698b1b30SVlastimil Babka cc.zone = zone; 1805*698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1806*698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1807*698b1b30SVlastimil Babka 1808*698b1b30SVlastimil Babka status = compact_zone(zone, &cc); 1809*698b1b30SVlastimil Babka 1810*698b1b30SVlastimil Babka if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone), 1811*698b1b30SVlastimil Babka cc.classzone_idx, 0)) { 1812*698b1b30SVlastimil Babka success = true; 1813*698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 1814*698b1b30SVlastimil Babka } else if (status == COMPACT_COMPLETE) { 1815*698b1b30SVlastimil Babka /* 1816*698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 1817*698b1b30SVlastimil Babka * sync direct compaction does. 1818*698b1b30SVlastimil Babka */ 1819*698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 1820*698b1b30SVlastimil Babka } 1821*698b1b30SVlastimil Babka 1822*698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1823*698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1824*698b1b30SVlastimil Babka } 1825*698b1b30SVlastimil Babka 1826*698b1b30SVlastimil Babka /* 1827*698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 1828*698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 1829*698b1b30SVlastimil Babka * our current ones 1830*698b1b30SVlastimil Babka */ 1831*698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 1832*698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1833*698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 1834*698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1835*698b1b30SVlastimil Babka } 1836*698b1b30SVlastimil Babka 1837*698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 1838*698b1b30SVlastimil Babka { 1839*698b1b30SVlastimil Babka if (!order) 1840*698b1b30SVlastimil Babka return; 1841*698b1b30SVlastimil Babka 1842*698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 1843*698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 1844*698b1b30SVlastimil Babka 1845*698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 1846*698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 1847*698b1b30SVlastimil Babka 1848*698b1b30SVlastimil Babka if (!waitqueue_active(&pgdat->kcompactd_wait)) 1849*698b1b30SVlastimil Babka return; 1850*698b1b30SVlastimil Babka 1851*698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 1852*698b1b30SVlastimil Babka return; 1853*698b1b30SVlastimil Babka 1854*698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 1855*698b1b30SVlastimil Babka classzone_idx); 1856*698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 1857*698b1b30SVlastimil Babka } 1858*698b1b30SVlastimil Babka 1859*698b1b30SVlastimil Babka /* 1860*698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 1861*698b1b30SVlastimil Babka * from the init process. 1862*698b1b30SVlastimil Babka */ 1863*698b1b30SVlastimil Babka static int kcompactd(void *p) 1864*698b1b30SVlastimil Babka { 1865*698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 1866*698b1b30SVlastimil Babka struct task_struct *tsk = current; 1867*698b1b30SVlastimil Babka 1868*698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 1869*698b1b30SVlastimil Babka 1870*698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 1871*698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 1872*698b1b30SVlastimil Babka 1873*698b1b30SVlastimil Babka set_freezable(); 1874*698b1b30SVlastimil Babka 1875*698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1876*698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1877*698b1b30SVlastimil Babka 1878*698b1b30SVlastimil Babka while (!kthread_should_stop()) { 1879*698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 1880*698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 1881*698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 1882*698b1b30SVlastimil Babka 1883*698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 1884*698b1b30SVlastimil Babka } 1885*698b1b30SVlastimil Babka 1886*698b1b30SVlastimil Babka return 0; 1887*698b1b30SVlastimil Babka } 1888*698b1b30SVlastimil Babka 1889*698b1b30SVlastimil Babka /* 1890*698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 1891*698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 1892*698b1b30SVlastimil Babka */ 1893*698b1b30SVlastimil Babka int kcompactd_run(int nid) 1894*698b1b30SVlastimil Babka { 1895*698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1896*698b1b30SVlastimil Babka int ret = 0; 1897*698b1b30SVlastimil Babka 1898*698b1b30SVlastimil Babka if (pgdat->kcompactd) 1899*698b1b30SVlastimil Babka return 0; 1900*698b1b30SVlastimil Babka 1901*698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 1902*698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 1903*698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 1904*698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 1905*698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 1906*698b1b30SVlastimil Babka } 1907*698b1b30SVlastimil Babka return ret; 1908*698b1b30SVlastimil Babka } 1909*698b1b30SVlastimil Babka 1910*698b1b30SVlastimil Babka /* 1911*698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 1912*698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 1913*698b1b30SVlastimil Babka */ 1914*698b1b30SVlastimil Babka void kcompactd_stop(int nid) 1915*698b1b30SVlastimil Babka { 1916*698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 1917*698b1b30SVlastimil Babka 1918*698b1b30SVlastimil Babka if (kcompactd) { 1919*698b1b30SVlastimil Babka kthread_stop(kcompactd); 1920*698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 1921*698b1b30SVlastimil Babka } 1922*698b1b30SVlastimil Babka } 1923*698b1b30SVlastimil Babka 1924*698b1b30SVlastimil Babka /* 1925*698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 1926*698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 1927*698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 1928*698b1b30SVlastimil Babka * restore their cpu bindings. 1929*698b1b30SVlastimil Babka */ 1930*698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action, 1931*698b1b30SVlastimil Babka void *hcpu) 1932*698b1b30SVlastimil Babka { 1933*698b1b30SVlastimil Babka int nid; 1934*698b1b30SVlastimil Babka 1935*698b1b30SVlastimil Babka if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 1936*698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 1937*698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1938*698b1b30SVlastimil Babka const struct cpumask *mask; 1939*698b1b30SVlastimil Babka 1940*698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 1941*698b1b30SVlastimil Babka 1942*698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 1943*698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 1944*698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 1945*698b1b30SVlastimil Babka } 1946*698b1b30SVlastimil Babka } 1947*698b1b30SVlastimil Babka return NOTIFY_OK; 1948*698b1b30SVlastimil Babka } 1949*698b1b30SVlastimil Babka 1950*698b1b30SVlastimil Babka static int __init kcompactd_init(void) 1951*698b1b30SVlastimil Babka { 1952*698b1b30SVlastimil Babka int nid; 1953*698b1b30SVlastimil Babka 1954*698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 1955*698b1b30SVlastimil Babka kcompactd_run(nid); 1956*698b1b30SVlastimil Babka hotcpu_notifier(cpu_callback, 0); 1957*698b1b30SVlastimil Babka return 0; 1958*698b1b30SVlastimil Babka } 1959*698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 1960*698b1b30SVlastimil Babka 1961ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1962