1748446bbSMel Gorman /* 2748446bbSMel Gorman * linux/mm/compaction.c 3748446bbSMel Gorman * 4748446bbSMel Gorman * Memory compaction for the reduction of external fragmentation. Note that 5748446bbSMel Gorman * this heavily depends upon page migration to do all the real heavy 6748446bbSMel Gorman * lifting 7748446bbSMel Gorman * 8748446bbSMel Gorman * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> 9748446bbSMel Gorman */ 10698b1b30SVlastimil Babka #include <linux/cpu.h> 11748446bbSMel Gorman #include <linux/swap.h> 12748446bbSMel Gorman #include <linux/migrate.h> 13748446bbSMel Gorman #include <linux/compaction.h> 14748446bbSMel Gorman #include <linux/mm_inline.h> 15748446bbSMel Gorman #include <linux/backing-dev.h> 1676ab0f53SMel Gorman #include <linux/sysctl.h> 17ed4a6d7fSMel Gorman #include <linux/sysfs.h> 18bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h> 19194159fbSMinchan Kim #include <linux/page-isolation.h> 20b8c73fc2SAndrey Ryabinin #include <linux/kasan.h> 21698b1b30SVlastimil Babka #include <linux/kthread.h> 22698b1b30SVlastimil Babka #include <linux/freezer.h> 23748446bbSMel Gorman #include "internal.h" 24748446bbSMel Gorman 25010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 26010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 27010fc29aSMinchan Kim { 28010fc29aSMinchan Kim count_vm_event(item); 29010fc29aSMinchan Kim } 30010fc29aSMinchan Kim 31010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 32010fc29aSMinchan Kim { 33010fc29aSMinchan Kim count_vm_events(item, delta); 34010fc29aSMinchan Kim } 35010fc29aSMinchan Kim #else 36010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 37010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 38010fc29aSMinchan Kim #endif 39010fc29aSMinchan Kim 40ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 41ff9543fdSMichal Nazarewicz 42b7aba698SMel Gorman #define CREATE_TRACE_POINTS 43b7aba698SMel Gorman #include <trace/events/compaction.h> 44b7aba698SMel Gorman 4506b6640aSVlastimil Babka #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) 4606b6640aSVlastimil Babka #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) 4706b6640aSVlastimil Babka #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) 4806b6640aSVlastimil Babka #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) 4906b6640aSVlastimil Babka 50748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 51748446bbSMel Gorman { 52748446bbSMel Gorman struct page *page, *next; 536bace090SVlastimil Babka unsigned long high_pfn = 0; 54748446bbSMel Gorman 55748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 566bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 57748446bbSMel Gorman list_del(&page->lru); 58748446bbSMel Gorman __free_page(page); 596bace090SVlastimil Babka if (pfn > high_pfn) 606bace090SVlastimil Babka high_pfn = pfn; 61748446bbSMel Gorman } 62748446bbSMel Gorman 636bace090SVlastimil Babka return high_pfn; 64748446bbSMel Gorman } 65748446bbSMel Gorman 66ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 67ff9543fdSMichal Nazarewicz { 68ff9543fdSMichal Nazarewicz struct page *page; 69ff9543fdSMichal Nazarewicz 70ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 71ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 72ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 73b8c73fc2SAndrey Ryabinin kasan_alloc_pages(page, 0); 74ff9543fdSMichal Nazarewicz } 75ff9543fdSMichal Nazarewicz } 76ff9543fdSMichal Nazarewicz 7747118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7847118af0SMichal Nazarewicz { 7947118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 8047118af0SMichal Nazarewicz } 8147118af0SMichal Nazarewicz 82bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 8324e2716fSJoonsoo Kim 8424e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 8524e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 8624e2716fSJoonsoo Kim 8724e2716fSJoonsoo Kim /* 8824e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 8924e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 9024e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 9124e2716fSJoonsoo Kim */ 9224e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 9324e2716fSJoonsoo Kim { 9424e2716fSJoonsoo Kim zone->compact_considered = 0; 9524e2716fSJoonsoo Kim zone->compact_defer_shift++; 9624e2716fSJoonsoo Kim 9724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 9824e2716fSJoonsoo Kim zone->compact_order_failed = order; 9924e2716fSJoonsoo Kim 10024e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 10124e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 10224e2716fSJoonsoo Kim 10324e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 10424e2716fSJoonsoo Kim } 10524e2716fSJoonsoo Kim 10624e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 10724e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 10824e2716fSJoonsoo Kim { 10924e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 11024e2716fSJoonsoo Kim 11124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 11224e2716fSJoonsoo Kim return false; 11324e2716fSJoonsoo Kim 11424e2716fSJoonsoo Kim /* Avoid possible overflow */ 11524e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 11624e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 11724e2716fSJoonsoo Kim 11824e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 11924e2716fSJoonsoo Kim return false; 12024e2716fSJoonsoo Kim 12124e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 12224e2716fSJoonsoo Kim 12324e2716fSJoonsoo Kim return true; 12424e2716fSJoonsoo Kim } 12524e2716fSJoonsoo Kim 12624e2716fSJoonsoo Kim /* 12724e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 12824e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 12924e2716fSJoonsoo Kim * expected to succeed. 13024e2716fSJoonsoo Kim */ 13124e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 13224e2716fSJoonsoo Kim bool alloc_success) 13324e2716fSJoonsoo Kim { 13424e2716fSJoonsoo Kim if (alloc_success) { 13524e2716fSJoonsoo Kim zone->compact_considered = 0; 13624e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 13724e2716fSJoonsoo Kim } 13824e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 13924e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 14024e2716fSJoonsoo Kim 14124e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 14224e2716fSJoonsoo Kim } 14324e2716fSJoonsoo Kim 14424e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 14524e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 14624e2716fSJoonsoo Kim { 14724e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14824e2716fSJoonsoo Kim return false; 14924e2716fSJoonsoo Kim 15024e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 15124e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 15224e2716fSJoonsoo Kim } 15324e2716fSJoonsoo Kim 154bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 155bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 156bb13ffebSMel Gorman struct page *page) 157bb13ffebSMel Gorman { 158bb13ffebSMel Gorman if (cc->ignore_skip_hint) 159bb13ffebSMel Gorman return true; 160bb13ffebSMel Gorman 161bb13ffebSMel Gorman return !get_pageblock_skip(page); 162bb13ffebSMel Gorman } 163bb13ffebSMel Gorman 16402333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 16502333641SVlastimil Babka { 16602333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 16702333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 168623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 16906b6640aSVlastimil Babka pageblock_start_pfn(zone_end_pfn(zone) - 1); 17002333641SVlastimil Babka } 17102333641SVlastimil Babka 172bb13ffebSMel Gorman /* 173bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 174bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 175bb13ffebSMel Gorman * meet. 176bb13ffebSMel Gorman */ 17762997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 178bb13ffebSMel Gorman { 179bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 180108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 181bb13ffebSMel Gorman unsigned long pfn; 182bb13ffebSMel Gorman 18362997027SMel Gorman zone->compact_blockskip_flush = false; 184bb13ffebSMel Gorman 185bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 186bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 187bb13ffebSMel Gorman struct page *page; 188bb13ffebSMel Gorman 189bb13ffebSMel Gorman cond_resched(); 190bb13ffebSMel Gorman 191bb13ffebSMel Gorman if (!pfn_valid(pfn)) 192bb13ffebSMel Gorman continue; 193bb13ffebSMel Gorman 194bb13ffebSMel Gorman page = pfn_to_page(pfn); 195bb13ffebSMel Gorman if (zone != page_zone(page)) 196bb13ffebSMel Gorman continue; 197bb13ffebSMel Gorman 198bb13ffebSMel Gorman clear_pageblock_skip(page); 199bb13ffebSMel Gorman } 20002333641SVlastimil Babka 20102333641SVlastimil Babka reset_cached_positions(zone); 202bb13ffebSMel Gorman } 203bb13ffebSMel Gorman 20462997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 20562997027SMel Gorman { 20662997027SMel Gorman int zoneid; 20762997027SMel Gorman 20862997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 20962997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 21062997027SMel Gorman if (!populated_zone(zone)) 21162997027SMel Gorman continue; 21262997027SMel Gorman 21362997027SMel Gorman /* Only flush if a full compaction finished recently */ 21462997027SMel Gorman if (zone->compact_blockskip_flush) 21562997027SMel Gorman __reset_isolation_suitable(zone); 21662997027SMel Gorman } 21762997027SMel Gorman } 21862997027SMel Gorman 219bb13ffebSMel Gorman /* 220bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 22162997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 222bb13ffebSMel Gorman */ 223c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 224c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 225edc2ca61SVlastimil Babka bool migrate_scanner) 226bb13ffebSMel Gorman { 227c89511abSMel Gorman struct zone *zone = cc->zone; 22835979ef3SDavid Rientjes unsigned long pfn; 2296815bf3fSJoonsoo Kim 2306815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2316815bf3fSJoonsoo Kim return; 2326815bf3fSJoonsoo Kim 233bb13ffebSMel Gorman if (!page) 234bb13ffebSMel Gorman return; 235bb13ffebSMel Gorman 23635979ef3SDavid Rientjes if (nr_isolated) 23735979ef3SDavid Rientjes return; 23835979ef3SDavid Rientjes 239bb13ffebSMel Gorman set_pageblock_skip(page); 240c89511abSMel Gorman 24135979ef3SDavid Rientjes pfn = page_to_pfn(page); 24235979ef3SDavid Rientjes 24335979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 244c89511abSMel Gorman if (migrate_scanner) { 24535979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 24635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 247e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 248e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 24935979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 250c89511abSMel Gorman } else { 25135979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 252c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 253c89511abSMel Gorman } 254c89511abSMel Gorman } 255bb13ffebSMel Gorman #else 256bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 257bb13ffebSMel Gorman struct page *page) 258bb13ffebSMel Gorman { 259bb13ffebSMel Gorman return true; 260bb13ffebSMel Gorman } 261bb13ffebSMel Gorman 262c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 263c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 264edc2ca61SVlastimil Babka bool migrate_scanner) 265bb13ffebSMel Gorman { 266bb13ffebSMel Gorman } 267bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 268bb13ffebSMel Gorman 2691f9efdefSVlastimil Babka /* 2708b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 2718b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 2728b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 2738b44d279SVlastimil Babka * 2748b44d279SVlastimil Babka * Returns true if the lock is held 2758b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 2761f9efdefSVlastimil Babka */ 2778b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 2788b44d279SVlastimil Babka struct compact_control *cc) 2798b44d279SVlastimil Babka { 2808b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 2818b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 2828b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 2838b44d279SVlastimil Babka return false; 2848b44d279SVlastimil Babka } 2858b44d279SVlastimil Babka } else { 2868b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 2878b44d279SVlastimil Babka } 2881f9efdefSVlastimil Babka 2898b44d279SVlastimil Babka return true; 2902a1402aaSMel Gorman } 2912a1402aaSMel Gorman 29285aa125fSMichal Nazarewicz /* 293c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 2948b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 2958b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 2968b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 2978b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 2988b44d279SVlastimil Babka * aborts. Sync compaction schedules. 2998b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 3008b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 301c67fe375SMel Gorman * 3028b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 3038b44d279SVlastimil Babka * async compaction due to need_resched() 3048b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3058b44d279SVlastimil Babka * scheduled) 306c67fe375SMel Gorman */ 3078b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3088b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 309c67fe375SMel Gorman { 3108b44d279SVlastimil Babka if (*locked) { 3118b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3128b44d279SVlastimil Babka *locked = false; 313c67fe375SMel Gorman } 314c67fe375SMel Gorman 3158b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 3168b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3178b44d279SVlastimil Babka return true; 3188b44d279SVlastimil Babka } 3198b44d279SVlastimil Babka 3208b44d279SVlastimil Babka if (need_resched()) { 321e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 3228b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3238b44d279SVlastimil Babka return true; 324c67fe375SMel Gorman } 325c67fe375SMel Gorman cond_resched(); 326c67fe375SMel Gorman } 327c67fe375SMel Gorman 3288b44d279SVlastimil Babka return false; 329c67fe375SMel Gorman } 330c67fe375SMel Gorman 331be976572SVlastimil Babka /* 332be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 333be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3348b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 335be976572SVlastimil Babka * is used where no lock is concerned. 336be976572SVlastimil Babka * 337be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 338be976572SVlastimil Babka * Returns true when async compaction should abort. 339be976572SVlastimil Babka */ 340be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 341be976572SVlastimil Babka { 342be976572SVlastimil Babka /* async compaction aborts if contended */ 343be976572SVlastimil Babka if (need_resched()) { 344be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3451f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 346be976572SVlastimil Babka return true; 347be976572SVlastimil Babka } 348be976572SVlastimil Babka 349be976572SVlastimil Babka cond_resched(); 350be976572SVlastimil Babka } 351be976572SVlastimil Babka 352be976572SVlastimil Babka return false; 353be976572SVlastimil Babka } 354be976572SVlastimil Babka 355c67fe375SMel Gorman /* 3569e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3579e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3589e4be470SJerome Marchand * (even though it may still end up isolating some pages). 35985aa125fSMichal Nazarewicz */ 360f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 361e14c720eSVlastimil Babka unsigned long *start_pfn, 36285aa125fSMichal Nazarewicz unsigned long end_pfn, 36385aa125fSMichal Nazarewicz struct list_head *freelist, 36485aa125fSMichal Nazarewicz bool strict) 365748446bbSMel Gorman { 366b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 367bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 368b8b2d825SXiubo Li unsigned long flags = 0; 369f40d1e42SMel Gorman bool locked = false; 370e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 371748446bbSMel Gorman 372748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 373748446bbSMel Gorman 374f40d1e42SMel Gorman /* Isolate free pages. */ 375748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 376748446bbSMel Gorman int isolated, i; 377748446bbSMel Gorman struct page *page = cursor; 378748446bbSMel Gorman 3798b44d279SVlastimil Babka /* 3808b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 3818b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 3828b44d279SVlastimil Babka * pending or async compaction detects need_resched() 3838b44d279SVlastimil Babka */ 3848b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 3858b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 3868b44d279SVlastimil Babka &locked, cc)) 3878b44d279SVlastimil Babka break; 3888b44d279SVlastimil Babka 389b7aba698SMel Gorman nr_scanned++; 390f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 3912af120bcSLaura Abbott goto isolate_fail; 3922af120bcSLaura Abbott 393bb13ffebSMel Gorman if (!valid_page) 394bb13ffebSMel Gorman valid_page = page; 3959fcd6d2eSVlastimil Babka 3969fcd6d2eSVlastimil Babka /* 3979fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 3989fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 3999fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 4009fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 4019fcd6d2eSVlastimil Babka */ 4029fcd6d2eSVlastimil Babka if (PageCompound(page)) { 4039fcd6d2eSVlastimil Babka unsigned int comp_order = compound_order(page); 4049fcd6d2eSVlastimil Babka 4059fcd6d2eSVlastimil Babka if (likely(comp_order < MAX_ORDER)) { 4069fcd6d2eSVlastimil Babka blockpfn += (1UL << comp_order) - 1; 4079fcd6d2eSVlastimil Babka cursor += (1UL << comp_order) - 1; 4089fcd6d2eSVlastimil Babka } 4099fcd6d2eSVlastimil Babka 4109fcd6d2eSVlastimil Babka goto isolate_fail; 4119fcd6d2eSVlastimil Babka } 4129fcd6d2eSVlastimil Babka 413f40d1e42SMel Gorman if (!PageBuddy(page)) 4142af120bcSLaura Abbott goto isolate_fail; 415f40d1e42SMel Gorman 416f40d1e42SMel Gorman /* 41769b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 41869b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 41969b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 42069b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 42169b7189fSVlastimil Babka * recheck as well. 42269b7189fSVlastimil Babka */ 42369b7189fSVlastimil Babka if (!locked) { 42469b7189fSVlastimil Babka /* 425f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 426f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 427f40d1e42SMel Gorman * heavily contended if there are parallel allocations 428f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 429f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 430f40d1e42SMel Gorman * possible. 431f40d1e42SMel Gorman */ 4328b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4338b44d279SVlastimil Babka &flags, cc); 434f40d1e42SMel Gorman if (!locked) 435f40d1e42SMel Gorman break; 436f40d1e42SMel Gorman 437f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 438f40d1e42SMel Gorman if (!PageBuddy(page)) 4392af120bcSLaura Abbott goto isolate_fail; 44069b7189fSVlastimil Babka } 441748446bbSMel Gorman 442748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 443748446bbSMel Gorman isolated = split_free_page(page); 444748446bbSMel Gorman total_isolated += isolated; 445748446bbSMel Gorman for (i = 0; i < isolated; i++) { 446748446bbSMel Gorman list_add(&page->lru, freelist); 447748446bbSMel Gorman page++; 448748446bbSMel Gorman } 449748446bbSMel Gorman 450748446bbSMel Gorman /* If a page was split, advance to the end of it */ 451748446bbSMel Gorman if (isolated) { 452932ff6bbSJoonsoo Kim cc->nr_freepages += isolated; 453932ff6bbSJoonsoo Kim if (!strict && 454932ff6bbSJoonsoo Kim cc->nr_migratepages <= cc->nr_freepages) { 455932ff6bbSJoonsoo Kim blockpfn += isolated; 456932ff6bbSJoonsoo Kim break; 457932ff6bbSJoonsoo Kim } 458932ff6bbSJoonsoo Kim 459748446bbSMel Gorman blockpfn += isolated - 1; 460748446bbSMel Gorman cursor += isolated - 1; 4612af120bcSLaura Abbott continue; 462748446bbSMel Gorman } 4632af120bcSLaura Abbott 4642af120bcSLaura Abbott isolate_fail: 4652af120bcSLaura Abbott if (strict) 4662af120bcSLaura Abbott break; 4672af120bcSLaura Abbott else 4682af120bcSLaura Abbott continue; 4692af120bcSLaura Abbott 470748446bbSMel Gorman } 471748446bbSMel Gorman 4729fcd6d2eSVlastimil Babka /* 4739fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 4749fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 4759fcd6d2eSVlastimil Babka */ 4769fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 4779fcd6d2eSVlastimil Babka blockpfn = end_pfn; 4789fcd6d2eSVlastimil Babka 479e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 480e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 481e34d85f0SJoonsoo Kim 482e14c720eSVlastimil Babka /* Record how far we have got within the block */ 483e14c720eSVlastimil Babka *start_pfn = blockpfn; 484e14c720eSVlastimil Babka 485f40d1e42SMel Gorman /* 486f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 487f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 488f40d1e42SMel Gorman * returned and CMA will fail. 489f40d1e42SMel Gorman */ 4902af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 491f40d1e42SMel Gorman total_isolated = 0; 492f40d1e42SMel Gorman 493f40d1e42SMel Gorman if (locked) 494f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 495f40d1e42SMel Gorman 496bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 497bb13ffebSMel Gorman if (blockpfn == end_pfn) 498edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 499bb13ffebSMel Gorman 500010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 501397487dbSMel Gorman if (total_isolated) 502010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 503748446bbSMel Gorman return total_isolated; 504748446bbSMel Gorman } 505748446bbSMel Gorman 50685aa125fSMichal Nazarewicz /** 50785aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 50885aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 50985aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 51085aa125fSMichal Nazarewicz * 51185aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 51285aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 51385aa125fSMichal Nazarewicz * undo its actions and return zero. 51485aa125fSMichal Nazarewicz * 51585aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 51685aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 51785aa125fSMichal Nazarewicz * a free page). 51885aa125fSMichal Nazarewicz */ 519ff9543fdSMichal Nazarewicz unsigned long 520bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 521bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 52285aa125fSMichal Nazarewicz { 523e1409c32SJoonsoo Kim unsigned long isolated, pfn, block_start_pfn, block_end_pfn; 52485aa125fSMichal Nazarewicz LIST_HEAD(freelist); 52585aa125fSMichal Nazarewicz 5267d49d886SVlastimil Babka pfn = start_pfn; 52706b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 528e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 529e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 53006b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 5317d49d886SVlastimil Babka 5327d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 533e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 5347d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 535e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 536e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5377d49d886SVlastimil Babka 53885aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 53985aa125fSMichal Nazarewicz 54058420016SJoonsoo Kim /* 54158420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 54258420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 54358420016SJoonsoo Kim * scanning range to right one. 54458420016SJoonsoo Kim */ 54558420016SJoonsoo Kim if (pfn >= block_end_pfn) { 54606b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 54706b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 54858420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 54958420016SJoonsoo Kim } 55058420016SJoonsoo Kim 551e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 552e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 5537d49d886SVlastimil Babka break; 5547d49d886SVlastimil Babka 555e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 556e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 55785aa125fSMichal Nazarewicz 55885aa125fSMichal Nazarewicz /* 55985aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 56085aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 56185aa125fSMichal Nazarewicz * non-free pages). 56285aa125fSMichal Nazarewicz */ 56385aa125fSMichal Nazarewicz if (!isolated) 56485aa125fSMichal Nazarewicz break; 56585aa125fSMichal Nazarewicz 56685aa125fSMichal Nazarewicz /* 56785aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 56885aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 56985aa125fSMichal Nazarewicz * page may span two pageblocks). 57085aa125fSMichal Nazarewicz */ 57185aa125fSMichal Nazarewicz } 57285aa125fSMichal Nazarewicz 57385aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 57485aa125fSMichal Nazarewicz map_pages(&freelist); 57585aa125fSMichal Nazarewicz 57685aa125fSMichal Nazarewicz if (pfn < end_pfn) { 57785aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 57885aa125fSMichal Nazarewicz release_freepages(&freelist); 57985aa125fSMichal Nazarewicz return 0; 58085aa125fSMichal Nazarewicz } 58185aa125fSMichal Nazarewicz 58285aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 58385aa125fSMichal Nazarewicz return pfn; 58485aa125fSMichal Nazarewicz } 58585aa125fSMichal Nazarewicz 586748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 587edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 588748446bbSMel Gorman { 589748446bbSMel Gorman struct page *page; 590b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 591748446bbSMel Gorman 592edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 593edc2ca61SVlastimil Babka return; 594edc2ca61SVlastimil Babka 595b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 596b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 597748446bbSMel Gorman 598c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 599c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 600c67fe375SMel Gorman } 601748446bbSMel Gorman 602748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 603748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 604748446bbSMel Gorman { 605bc693045SMinchan Kim unsigned long active, inactive, isolated; 606748446bbSMel Gorman 607748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 608748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 609bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 610bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 611748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 612748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 613748446bbSMel Gorman 614bc693045SMinchan Kim return isolated > (inactive + active) / 2; 615748446bbSMel Gorman } 616748446bbSMel Gorman 6172fe86e00SMichal Nazarewicz /** 618edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 619edc2ca61SVlastimil Babka * a single pageblock 6202fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 621edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 622edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 623edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6242fe86e00SMichal Nazarewicz * 6252fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 626edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 627edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 628edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 629edc2ca61SVlastimil Babka * than end_pfn). 6302fe86e00SMichal Nazarewicz * 631edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 632edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 633edc2ca61SVlastimil Babka * is neither read nor updated. 634748446bbSMel Gorman */ 635edc2ca61SVlastimil Babka static unsigned long 636edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 637edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 638748446bbSMel Gorman { 639edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 640b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 641748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 642fa9add64SHugh Dickins struct lruvec *lruvec; 643b8b2d825SXiubo Li unsigned long flags = 0; 6442a1402aaSMel Gorman bool locked = false; 645bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 646e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 647748446bbSMel Gorman 648748446bbSMel Gorman /* 649748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 650748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 651748446bbSMel Gorman * delay for some time until fewer pages are isolated 652748446bbSMel Gorman */ 653748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 654f9e35b3bSMel Gorman /* async migration should just abort */ 655e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6562fe86e00SMichal Nazarewicz return 0; 657f9e35b3bSMel Gorman 658748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 659748446bbSMel Gorman 660748446bbSMel Gorman if (fatal_signal_pending(current)) 6612fe86e00SMichal Nazarewicz return 0; 662748446bbSMel Gorman } 663748446bbSMel Gorman 664be976572SVlastimil Babka if (compact_should_abort(cc)) 665aeef4b83SDavid Rientjes return 0; 666aeef4b83SDavid Rientjes 667748446bbSMel Gorman /* Time to isolate some pages for migration */ 668748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 66929c0dde8SVlastimil Babka bool is_lru; 67029c0dde8SVlastimil Babka 6718b44d279SVlastimil Babka /* 6728b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 6738b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 6748b44d279SVlastimil Babka * if contended. 6758b44d279SVlastimil Babka */ 6768b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 6778b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 6788b44d279SVlastimil Babka &locked, cc)) 6798b44d279SVlastimil Babka break; 680b2eef8c0SAndrea Arcangeli 681748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 682748446bbSMel Gorman continue; 683b7aba698SMel Gorman nr_scanned++; 684748446bbSMel Gorman 685748446bbSMel Gorman page = pfn_to_page(low_pfn); 686dc908600SMel Gorman 687bb13ffebSMel Gorman if (!valid_page) 688bb13ffebSMel Gorman valid_page = page; 689bb13ffebSMel Gorman 690c122b208SJoonsoo Kim /* 69199c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 69299c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 69399c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 69499c0fd5eSVlastimil Babka * potential isolation targets. 6956c14466cSMel Gorman */ 69699c0fd5eSVlastimil Babka if (PageBuddy(page)) { 69799c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 69899c0fd5eSVlastimil Babka 69999c0fd5eSVlastimil Babka /* 70099c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 70199c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 70299c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 70399c0fd5eSVlastimil Babka */ 70499c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 70599c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 706748446bbSMel Gorman continue; 70799c0fd5eSVlastimil Babka } 708748446bbSMel Gorman 7099927af74SMel Gorman /* 710bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 711bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 712bf6bddf1SRafael Aquini * Skip any other type of page 713bf6bddf1SRafael Aquini */ 71429c0dde8SVlastimil Babka is_lru = PageLRU(page); 71529c0dde8SVlastimil Babka if (!is_lru) { 716bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 717d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 718bf6bddf1SRafael Aquini /* Successfully isolated */ 719b6c75016SJoonsoo Kim goto isolate_success; 720bf6bddf1SRafael Aquini } 721bf6bddf1SRafael Aquini } 722bf6bddf1SRafael Aquini } 723bc835011SAndrea Arcangeli 724bc835011SAndrea Arcangeli /* 72529c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 72629c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 72729c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 72829c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 72929c0dde8SVlastimil Babka * danger is skipping too much. 730bc835011SAndrea Arcangeli */ 73129c0dde8SVlastimil Babka if (PageCompound(page)) { 73229c0dde8SVlastimil Babka unsigned int comp_order = compound_order(page); 73329c0dde8SVlastimil Babka 73429c0dde8SVlastimil Babka if (likely(comp_order < MAX_ORDER)) 73529c0dde8SVlastimil Babka low_pfn += (1UL << comp_order) - 1; 736edc2ca61SVlastimil Babka 7372a1402aaSMel Gorman continue; 7382a1402aaSMel Gorman } 7392a1402aaSMel Gorman 74029c0dde8SVlastimil Babka if (!is_lru) 74129c0dde8SVlastimil Babka continue; 74229c0dde8SVlastimil Babka 743119d6d59SDavid Rientjes /* 744119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 745119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 746119d6d59SDavid Rientjes * admittedly racy check. 747119d6d59SDavid Rientjes */ 748119d6d59SDavid Rientjes if (!page_mapping(page) && 749119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 750119d6d59SDavid Rientjes continue; 751119d6d59SDavid Rientjes 75269b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 75369b7189fSVlastimil Babka if (!locked) { 7548b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7558b44d279SVlastimil Babka &flags, cc); 7568b44d279SVlastimil Babka if (!locked) 7572a1402aaSMel Gorman break; 7582a1402aaSMel Gorman 75929c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 7602a1402aaSMel Gorman if (!PageLRU(page)) 7612a1402aaSMel Gorman continue; 76229c0dde8SVlastimil Babka 76329c0dde8SVlastimil Babka /* 76429c0dde8SVlastimil Babka * Page become compound since the non-locked check, 76529c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 76629c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 76729c0dde8SVlastimil Babka */ 76829c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 76929c0dde8SVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 770bc835011SAndrea Arcangeli continue; 771bc835011SAndrea Arcangeli } 77269b7189fSVlastimil Babka } 773bc835011SAndrea Arcangeli 774fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 775fa9add64SHugh Dickins 776748446bbSMel Gorman /* Try isolate the page */ 777edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 778748446bbSMel Gorman continue; 779748446bbSMel Gorman 78029c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 781bc835011SAndrea Arcangeli 782748446bbSMel Gorman /* Successfully isolated */ 783fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 784b6c75016SJoonsoo Kim 785b6c75016SJoonsoo Kim isolate_success: 786748446bbSMel Gorman list_add(&page->lru, migratelist); 787748446bbSMel Gorman cc->nr_migratepages++; 788b7aba698SMel Gorman nr_isolated++; 789748446bbSMel Gorman 790*a34753d2SVlastimil Babka /* 791*a34753d2SVlastimil Babka * Record where we could have freed pages by migration and not 792*a34753d2SVlastimil Babka * yet flushed them to buddy allocator. 793*a34753d2SVlastimil Babka * - this is the lowest page that was isolated and likely be 794*a34753d2SVlastimil Babka * then freed by migration. 795*a34753d2SVlastimil Babka */ 796*a34753d2SVlastimil Babka if (!cc->last_migrated_pfn) 797*a34753d2SVlastimil Babka cc->last_migrated_pfn = low_pfn; 798*a34753d2SVlastimil Babka 799748446bbSMel Gorman /* Avoid isolating too much */ 80031b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 80131b8384aSHillf Danton ++low_pfn; 802748446bbSMel Gorman break; 803748446bbSMel Gorman } 80431b8384aSHillf Danton } 805748446bbSMel Gorman 80699c0fd5eSVlastimil Babka /* 80799c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 80899c0fd5eSVlastimil Babka * the range to be scanned. 80999c0fd5eSVlastimil Babka */ 81099c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 81199c0fd5eSVlastimil Babka low_pfn = end_pfn; 81299c0fd5eSVlastimil Babka 813c67fe375SMel Gorman if (locked) 814c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 815748446bbSMel Gorman 81650b5b094SVlastimil Babka /* 81750b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 81850b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 81950b5b094SVlastimil Babka */ 82035979ef3SDavid Rientjes if (low_pfn == end_pfn) 821edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 822bb13ffebSMel Gorman 823e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 824e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 825b7aba698SMel Gorman 826010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 827397487dbSMel Gorman if (nr_isolated) 828010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 829397487dbSMel Gorman 8302fe86e00SMichal Nazarewicz return low_pfn; 8312fe86e00SMichal Nazarewicz } 8322fe86e00SMichal Nazarewicz 833edc2ca61SVlastimil Babka /** 834edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 835edc2ca61SVlastimil Babka * @cc: Compaction control structure. 836edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 837edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 838edc2ca61SVlastimil Babka * 839edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 840edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 841edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 842edc2ca61SVlastimil Babka */ 843edc2ca61SVlastimil Babka unsigned long 844edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 845edc2ca61SVlastimil Babka unsigned long end_pfn) 846edc2ca61SVlastimil Babka { 847e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 848edc2ca61SVlastimil Babka 849edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 850edc2ca61SVlastimil Babka pfn = start_pfn; 85106b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 852e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 853e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 85406b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 855edc2ca61SVlastimil Babka 856edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 857e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 858edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 859edc2ca61SVlastimil Babka 860edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 861edc2ca61SVlastimil Babka 862e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 863e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 864edc2ca61SVlastimil Babka continue; 865edc2ca61SVlastimil Babka 866edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 867edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 868edc2ca61SVlastimil Babka 86914af4a5eSHugh Dickins if (!pfn) 870edc2ca61SVlastimil Babka break; 8716ea41c0cSJoonsoo Kim 8726ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8736ea41c0cSJoonsoo Kim break; 874edc2ca61SVlastimil Babka } 875edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 876edc2ca61SVlastimil Babka 877edc2ca61SVlastimil Babka return pfn; 878edc2ca61SVlastimil Babka } 879edc2ca61SVlastimil Babka 880ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 881ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 882018e9a49SAndrew Morton 883018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 884018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page) 885018e9a49SAndrew Morton { 886018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 887018e9a49SAndrew Morton if (PageBuddy(page)) { 888018e9a49SAndrew Morton /* 889018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 890018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 891018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 892018e9a49SAndrew Morton */ 893018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 894018e9a49SAndrew Morton return false; 895018e9a49SAndrew Morton } 896018e9a49SAndrew Morton 897018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 898018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 899018e9a49SAndrew Morton return true; 900018e9a49SAndrew Morton 901018e9a49SAndrew Morton /* Otherwise skip the block */ 902018e9a49SAndrew Morton return false; 903018e9a49SAndrew Morton } 904018e9a49SAndrew Morton 905ff9543fdSMichal Nazarewicz /* 906f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 907f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 908f2849aa0SVlastimil Babka */ 909f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 910f2849aa0SVlastimil Babka { 911f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 912f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 913f2849aa0SVlastimil Babka } 914f2849aa0SVlastimil Babka 915f2849aa0SVlastimil Babka /* 916ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 917ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 918ff9543fdSMichal Nazarewicz */ 919edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 920ff9543fdSMichal Nazarewicz { 921edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 922ff9543fdSMichal Nazarewicz struct page *page; 923c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 924e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 925c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 926c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 927ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9282fe86e00SMichal Nazarewicz 929ff9543fdSMichal Nazarewicz /* 930ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 93149e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 932e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 933e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 934c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 935c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 936c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 93749e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 93849e068f0SVlastimil Babka * is using. 939ff9543fdSMichal Nazarewicz */ 940e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 94106b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(cc->free_pfn); 942c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 943c96b9e50SVlastimil Babka zone_end_pfn(zone)); 94406b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 9452fe86e00SMichal Nazarewicz 946ff9543fdSMichal Nazarewicz /* 947ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 948ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 949ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 950ff9543fdSMichal Nazarewicz */ 951f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 952c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 953e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 954e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 955ff9543fdSMichal Nazarewicz 956f6ea3adbSDavid Rientjes /* 957f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 958f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 959be976572SVlastimil Babka * to schedule, or even abort async compaction. 960f6ea3adbSDavid Rientjes */ 961be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 962be976572SVlastimil Babka && compact_should_abort(cc)) 963be976572SVlastimil Babka break; 964f6ea3adbSDavid Rientjes 9657d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 9667d49d886SVlastimil Babka zone); 9677d49d886SVlastimil Babka if (!page) 968ff9543fdSMichal Nazarewicz continue; 969ff9543fdSMichal Nazarewicz 970ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 97168e3e926SLinus Torvalds if (!suitable_migration_target(page)) 972ff9543fdSMichal Nazarewicz continue; 97368e3e926SLinus Torvalds 974bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 975bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 976bb13ffebSMel Gorman continue; 977bb13ffebSMel Gorman 978e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 979932ff6bbSJoonsoo Kim isolate_freepages_block(cc, &isolate_start_pfn, 980c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 981ff9543fdSMichal Nazarewicz 982ff9543fdSMichal Nazarewicz /* 983f5f61a32SVlastimil Babka * If we isolated enough freepages, or aborted due to async 984f5f61a32SVlastimil Babka * compaction being contended, terminate the loop. 985e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 986e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 987e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 988e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 989e14c720eSVlastimil Babka * pageblock. 990e14c720eSVlastimil Babka * In that case we will however want to restart at the start 991e14c720eSVlastimil Babka * of the previous pageblock. 992e14c720eSVlastimil Babka */ 993f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 994f5f61a32SVlastimil Babka || cc->contended) { 995f5f61a32SVlastimil Babka if (isolate_start_pfn >= block_end_pfn) 996f5f61a32SVlastimil Babka isolate_start_pfn = 997e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 998be976572SVlastimil Babka break; 999f5f61a32SVlastimil Babka } else { 1000f5f61a32SVlastimil Babka /* 1001f5f61a32SVlastimil Babka * isolate_freepages_block() should not terminate 1002f5f61a32SVlastimil Babka * prematurely unless contended, or isolated enough 1003f5f61a32SVlastimil Babka */ 1004f5f61a32SVlastimil Babka VM_BUG_ON(isolate_start_pfn < block_end_pfn); 1005f5f61a32SVlastimil Babka } 1006c89511abSMel Gorman } 1007ff9543fdSMichal Nazarewicz 1008ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 1009ff9543fdSMichal Nazarewicz map_pages(freelist); 1010ff9543fdSMichal Nazarewicz 10117ed695e0SVlastimil Babka /* 1012f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1013f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1014f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1015f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 10167ed695e0SVlastimil Babka */ 1017f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1018748446bbSMel Gorman } 1019748446bbSMel Gorman 1020748446bbSMel Gorman /* 1021748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1022748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1023748446bbSMel Gorman */ 1024748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1025748446bbSMel Gorman unsigned long data, 1026748446bbSMel Gorman int **result) 1027748446bbSMel Gorman { 1028748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1029748446bbSMel Gorman struct page *freepage; 1030748446bbSMel Gorman 1031be976572SVlastimil Babka /* 1032be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1033be976572SVlastimil Babka * contention. 1034be976572SVlastimil Babka */ 1035748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1036be976572SVlastimil Babka if (!cc->contended) 1037edc2ca61SVlastimil Babka isolate_freepages(cc); 1038748446bbSMel Gorman 1039748446bbSMel Gorman if (list_empty(&cc->freepages)) 1040748446bbSMel Gorman return NULL; 1041748446bbSMel Gorman } 1042748446bbSMel Gorman 1043748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1044748446bbSMel Gorman list_del(&freepage->lru); 1045748446bbSMel Gorman cc->nr_freepages--; 1046748446bbSMel Gorman 1047748446bbSMel Gorman return freepage; 1048748446bbSMel Gorman } 1049748446bbSMel Gorman 1050748446bbSMel Gorman /* 1051d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1052d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1053d53aea3dSDavid Rientjes * special handling needed for NUMA. 1054d53aea3dSDavid Rientjes */ 1055d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1056d53aea3dSDavid Rientjes { 1057d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1058d53aea3dSDavid Rientjes 1059d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1060d53aea3dSDavid Rientjes cc->nr_freepages++; 1061d53aea3dSDavid Rientjes } 1062d53aea3dSDavid Rientjes 1063ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1064ff9543fdSMichal Nazarewicz typedef enum { 1065ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1066ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1067ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1068ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1069ff9543fdSMichal Nazarewicz 1070ff9543fdSMichal Nazarewicz /* 10715bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 10725bbe3547SEric B Munson * compactable pages. 10735bbe3547SEric B Munson */ 10745bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 10755bbe3547SEric B Munson 10765bbe3547SEric B Munson /* 1077edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1078edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1079edc2ca61SVlastimil Babka * compact_control. 1080ff9543fdSMichal Nazarewicz */ 1081ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1082ff9543fdSMichal Nazarewicz struct compact_control *cc) 1083ff9543fdSMichal Nazarewicz { 1084e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1085e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1086e1409c32SJoonsoo Kim unsigned long low_pfn; 1087edc2ca61SVlastimil Babka struct page *page; 1088edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 10895bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 1090edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1091ff9543fdSMichal Nazarewicz 1092edc2ca61SVlastimil Babka /* 1093edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1094edc2ca61SVlastimil Babka * initialized by compact_zone() 1095edc2ca61SVlastimil Babka */ 1096edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 109706b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1098e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1099e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1100ff9543fdSMichal Nazarewicz 1101ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 110206b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1103ff9543fdSMichal Nazarewicz 1104edc2ca61SVlastimil Babka /* 1105edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1106edc2ca61SVlastimil Babka * Do not cross the free scanner. 1107edc2ca61SVlastimil Babka */ 1108e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 1109e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1110e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1111e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1112edc2ca61SVlastimil Babka 1113edc2ca61SVlastimil Babka /* 1114edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1115edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1116edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1117edc2ca61SVlastimil Babka */ 1118edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1119edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1120edc2ca61SVlastimil Babka break; 1121edc2ca61SVlastimil Babka 1122e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1123e1409c32SJoonsoo Kim zone); 11247d49d886SVlastimil Babka if (!page) 1125edc2ca61SVlastimil Babka continue; 1126edc2ca61SVlastimil Babka 1127edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1128edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1129edc2ca61SVlastimil Babka continue; 1130edc2ca61SVlastimil Babka 1131edc2ca61SVlastimil Babka /* 1132edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1133edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1134edc2ca61SVlastimil Babka * of work satisfies the allocation. 1135edc2ca61SVlastimil Babka */ 1136edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1137edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1138edc2ca61SVlastimil Babka continue; 1139ff9543fdSMichal Nazarewicz 1140ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1141e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1142e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1143edc2ca61SVlastimil Babka 1144ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1145ff59909aSHugh Dickins acct_isolated(zone, cc); 1146ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1147ff59909aSHugh Dickins } 1148ff9543fdSMichal Nazarewicz 1149edc2ca61SVlastimil Babka /* 1150edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1151edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1152edc2ca61SVlastimil Babka * continue or not. 1153edc2ca61SVlastimil Babka */ 1154edc2ca61SVlastimil Babka break; 1155edc2ca61SVlastimil Babka } 1156edc2ca61SVlastimil Babka 1157edc2ca61SVlastimil Babka acct_isolated(zone, cc); 1158f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1159f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1160ff9543fdSMichal Nazarewicz 1161edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1162ff9543fdSMichal Nazarewicz } 1163ff9543fdSMichal Nazarewicz 116421c527a3SYaowei Bai /* 116521c527a3SYaowei Bai * order == -1 is expected when compacting via 116621c527a3SYaowei Bai * /proc/sys/vm/compact_memory 116721c527a3SYaowei Bai */ 116821c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 116921c527a3SYaowei Bai { 117021c527a3SYaowei Bai return order == -1; 117121c527a3SYaowei Bai } 117221c527a3SYaowei Bai 1173837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc, 11746d7ce559SDavid Rientjes const int migratetype) 1175748446bbSMel Gorman { 11768fb74b9fSMel Gorman unsigned int order; 11775a03b051SAndrea Arcangeli unsigned long watermark; 117856de7263SMel Gorman 1179be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 11802d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1181748446bbSMel Gorman 1182753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1183f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 118455b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 118502333641SVlastimil Babka reset_cached_positions(zone); 118655b7c4c9SVlastimil Babka 118762997027SMel Gorman /* 118862997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1189accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 119062997027SMel Gorman * flag itself as the decision to be clear should be directly 119162997027SMel Gorman * based on an allocation request. 119262997027SMel Gorman */ 1193accf6242SVlastimil Babka if (cc->direct_compaction) 119462997027SMel Gorman zone->compact_blockskip_flush = true; 119562997027SMel Gorman 1196748446bbSMel Gorman return COMPACT_COMPLETE; 1197bb13ffebSMel Gorman } 1198748446bbSMel Gorman 119921c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 120056de7263SMel Gorman return COMPACT_CONTINUE; 120156de7263SMel Gorman 12023957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 12033957c776SMichal Hocko watermark = low_wmark_pages(zone); 12043957c776SMichal Hocko 1205ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1206ebff3980SVlastimil Babka cc->alloc_flags)) 12073957c776SMichal Hocko return COMPACT_CONTINUE; 12083957c776SMichal Hocko 120956de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 121056de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 12118fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 12122149cdaeSJoonsoo Kim bool can_steal; 12138fb74b9fSMel Gorman 121456de7263SMel Gorman /* Job done if page is free of the right migratetype */ 12156d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 121656de7263SMel Gorman return COMPACT_PARTIAL; 121756de7263SMel Gorman 12182149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 12192149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 12202149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 12212149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 12222149cdaeSJoonsoo Kim return COMPACT_PARTIAL; 12232149cdaeSJoonsoo Kim #endif 12242149cdaeSJoonsoo Kim /* 12252149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 12262149cdaeSJoonsoo Kim * other migratetype buddy lists. 12272149cdaeSJoonsoo Kim */ 12282149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 12292149cdaeSJoonsoo Kim true, &can_steal) != -1) 123056de7263SMel Gorman return COMPACT_PARTIAL; 123156de7263SMel Gorman } 123256de7263SMel Gorman 1233837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1234837d026dSJoonsoo Kim } 1235837d026dSJoonsoo Kim 1236837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc, 1237837d026dSJoonsoo Kim const int migratetype) 1238837d026dSJoonsoo Kim { 1239837d026dSJoonsoo Kim int ret; 1240837d026dSJoonsoo Kim 1241837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1242837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1243837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1244837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1245837d026dSJoonsoo Kim 1246837d026dSJoonsoo Kim return ret; 1247748446bbSMel Gorman } 1248748446bbSMel Gorman 12493e7d3449SMel Gorman /* 12503e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 12513e7d3449SMel Gorman * Returns 12523e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 12533e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 12543e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 12553e7d3449SMel Gorman */ 1256837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order, 1257ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 12583e7d3449SMel Gorman { 12593e7d3449SMel Gorman int fragindex; 12603e7d3449SMel Gorman unsigned long watermark; 12613e7d3449SMel Gorman 126221c527a3SYaowei Bai if (is_via_compact_memory(order)) 12633957c776SMichal Hocko return COMPACT_CONTINUE; 12643957c776SMichal Hocko 1265ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1266ebff3980SVlastimil Babka /* 1267ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1268ebff3980SVlastimil Babka * should be no need for compaction at all. 1269ebff3980SVlastimil Babka */ 1270ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1271ebff3980SVlastimil Babka alloc_flags)) 1272ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1273ebff3980SVlastimil Babka 12743957c776SMichal Hocko /* 12753e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 12763e7d3449SMel Gorman * This is because during migration, copies of pages need to be 12773e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 12783e7d3449SMel Gorman */ 1279ebff3980SVlastimil Babka watermark += (2UL << order); 1280ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 12813e7d3449SMel Gorman return COMPACT_SKIPPED; 12823e7d3449SMel Gorman 12833e7d3449SMel Gorman /* 12843e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 12853e7d3449SMel Gorman * low memory or external fragmentation 12863e7d3449SMel Gorman * 1287ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1288ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 12893e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 12903e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 12913e7d3449SMel Gorman * 12923e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 12933e7d3449SMel Gorman */ 12943e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 12953e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1296837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 12973e7d3449SMel Gorman 12983e7d3449SMel Gorman return COMPACT_CONTINUE; 12993e7d3449SMel Gorman } 13003e7d3449SMel Gorman 1301837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order, 1302837d026dSJoonsoo Kim int alloc_flags, int classzone_idx) 1303837d026dSJoonsoo Kim { 1304837d026dSJoonsoo Kim unsigned long ret; 1305837d026dSJoonsoo Kim 1306837d026dSJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx); 1307837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1308837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1309837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1310837d026dSJoonsoo Kim 1311837d026dSJoonsoo Kim return ret; 1312837d026dSJoonsoo Kim } 1313837d026dSJoonsoo Kim 1314748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1315748446bbSMel Gorman { 1316748446bbSMel Gorman int ret; 1317c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1318108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 13196d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1320e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1321748446bbSMel Gorman 1322ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1323ebff3980SVlastimil Babka cc->classzone_idx); 13243e7d3449SMel Gorman switch (ret) { 13253e7d3449SMel Gorman case COMPACT_PARTIAL: 13263e7d3449SMel Gorman case COMPACT_SKIPPED: 13273e7d3449SMel Gorman /* Compaction is likely to fail */ 13283e7d3449SMel Gorman return ret; 13293e7d3449SMel Gorman case COMPACT_CONTINUE: 13303e7d3449SMel Gorman /* Fall through to compaction */ 13313e7d3449SMel Gorman ; 13323e7d3449SMel Gorman } 13333e7d3449SMel Gorman 1334c89511abSMel Gorman /* 1335d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1336accf6242SVlastimil Babka * is about to be retried after being deferred. 1337d3132e4bSVlastimil Babka */ 1338accf6242SVlastimil Babka if (compaction_restarting(zone, cc->order)) 1339d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1340d3132e4bSVlastimil Babka 1341d3132e4bSVlastimil Babka /* 1342c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1343c89511abSMel Gorman * information on where the scanners should start but check that it 1344c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1345c89511abSMel Gorman */ 1346e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1347c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1348623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 134906b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 1350c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1351c89511abSMel Gorman } 1352623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1353c89511abSMel Gorman cc->migrate_pfn = start_pfn; 135435979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 135535979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1356c89511abSMel Gorman } 13571a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1358748446bbSMel Gorman 135916c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 136016c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 13610eb927c0SMel Gorman 1362748446bbSMel Gorman migrate_prep_local(); 1363748446bbSMel Gorman 13646d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 13656d7ce559SDavid Rientjes COMPACT_CONTINUE) { 13669d502c1cSMinchan Kim int err; 1367748446bbSMel Gorman 1368f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1369f9e35b3bSMel Gorman case ISOLATE_ABORT: 13702d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 13715733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1372e64c5237SShaohua Li cc->nr_migratepages = 0; 1373f9e35b3bSMel Gorman goto out; 1374f9e35b3bSMel Gorman case ISOLATE_NONE: 1375fdaf7f5cSVlastimil Babka /* 1376fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1377fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1378fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1379fdaf7f5cSVlastimil Babka */ 1380fdaf7f5cSVlastimil Babka goto check_drain; 1381f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1382f9e35b3bSMel Gorman ; 1383f9e35b3bSMel Gorman } 1384748446bbSMel Gorman 1385d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1386e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 13877b2a2d4aSMel Gorman MR_COMPACTION); 1388748446bbSMel Gorman 1389f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1390f8c9301fSVlastimil Babka &cc->migratepages); 1391748446bbSMel Gorman 1392f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1393f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 13949d502c1cSMinchan Kim if (err) { 13955733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 13967ed695e0SVlastimil Babka /* 13977ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 13987ed695e0SVlastimil Babka * and we want compact_finished() to detect it 13997ed695e0SVlastimil Babka */ 1400f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 14012d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14024bf2bba3SDavid Rientjes goto out; 1403748446bbSMel Gorman } 14044bf2bba3SDavid Rientjes } 1405fdaf7f5cSVlastimil Babka 1406fdaf7f5cSVlastimil Babka check_drain: 1407fdaf7f5cSVlastimil Babka /* 1408fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1409fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1410fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1411fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1412fdaf7f5cSVlastimil Babka * would succeed. 1413fdaf7f5cSVlastimil Babka */ 14141a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1415fdaf7f5cSVlastimil Babka int cpu; 1416fdaf7f5cSVlastimil Babka unsigned long current_block_start = 141706b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 1418fdaf7f5cSVlastimil Babka 14191a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1420fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1421fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1422fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1423fdaf7f5cSVlastimil Babka put_cpu(); 1424fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 14251a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1426fdaf7f5cSVlastimil Babka } 1427fdaf7f5cSVlastimil Babka } 1428fdaf7f5cSVlastimil Babka 1429748446bbSMel Gorman } 1430748446bbSMel Gorman 1431f9e35b3bSMel Gorman out: 14326bace090SVlastimil Babka /* 14336bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 14346bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 14356bace090SVlastimil Babka */ 14366bace090SVlastimil Babka if (cc->nr_freepages > 0) { 14376bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 14386bace090SVlastimil Babka 14396bace090SVlastimil Babka cc->nr_freepages = 0; 14406bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 14416bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 144206b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 14436bace090SVlastimil Babka /* 14446bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 14456bace090SVlastimil Babka * already reset to zone end in compact_finished() 14466bace090SVlastimil Babka */ 14476bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 14486bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 14496bace090SVlastimil Babka } 1450748446bbSMel Gorman 145116c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 145216c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 14530eb927c0SMel Gorman 14542d1e1041SVlastimil Babka if (ret == COMPACT_CONTENDED) 14552d1e1041SVlastimil Babka ret = COMPACT_PARTIAL; 14562d1e1041SVlastimil Babka 1457748446bbSMel Gorman return ret; 1458748446bbSMel Gorman } 145976ab0f53SMel Gorman 1460e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1461ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1462ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 146356de7263SMel Gorman { 1464e64c5237SShaohua Li unsigned long ret; 146556de7263SMel Gorman struct compact_control cc = { 146656de7263SMel Gorman .nr_freepages = 0, 146756de7263SMel Gorman .nr_migratepages = 0, 146856de7263SMel Gorman .order = order, 14696d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 147056de7263SMel Gorman .zone = zone, 1471e0b9daebSDavid Rientjes .mode = mode, 1472ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1473ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 1474accf6242SVlastimil Babka .direct_compaction = true, 147556de7263SMel Gorman }; 147656de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 147756de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 147856de7263SMel Gorman 1479e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1480e64c5237SShaohua Li 1481e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1482e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1483e64c5237SShaohua Li 1484e64c5237SShaohua Li *contended = cc.contended; 1485e64c5237SShaohua Li return ret; 148656de7263SMel Gorman } 148756de7263SMel Gorman 14885e771905SMel Gorman int sysctl_extfrag_threshold = 500; 14895e771905SMel Gorman 149056de7263SMel Gorman /** 149156de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 149256de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 14931a6d53a1SVlastimil Babka * @order: The order of the current allocation 14941a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 14951a6d53a1SVlastimil Babka * @ac: The context of current allocation 1496e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 14971f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 14981f9efdefSVlastimil Babka * need_resched() or lock contention 149956de7263SMel Gorman * 150056de7263SMel Gorman * This is the main entry point for direct page compaction. 150156de7263SMel Gorman */ 15021a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 15031a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 15041a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 150556de7263SMel Gorman { 150656de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 150756de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 150856de7263SMel Gorman struct zoneref *z; 150956de7263SMel Gorman struct zone *zone; 151053853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 15111f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 15121f9efdefSVlastimil Babka 15131f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 151456de7263SMel Gorman 15154ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1516c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 151753853e2dSVlastimil Babka return COMPACT_SKIPPED; 151856de7263SMel Gorman 1519837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1520837d026dSJoonsoo Kim 152156de7263SMel Gorman /* Compact each zone in the list */ 15221a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 15231a6d53a1SVlastimil Babka ac->nodemask) { 152456de7263SMel Gorman int status; 15251f9efdefSVlastimil Babka int zone_contended; 152656de7263SMel Gorman 152753853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 152853853e2dSVlastimil Babka continue; 152953853e2dSVlastimil Babka 1530e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 15311a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 15321a6d53a1SVlastimil Babka ac->classzone_idx); 153356de7263SMel Gorman rc = max(status, rc); 15341f9efdefSVlastimil Babka /* 15351f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 15361f9efdefSVlastimil Babka * to clear all_zones_contended. 15371f9efdefSVlastimil Babka */ 15381f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 153956de7263SMel Gorman 15403e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1541ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 15421a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 154353853e2dSVlastimil Babka /* 154453853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 154553853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 154653853e2dSVlastimil Babka * will repeat this with true if allocation indeed 154753853e2dSVlastimil Babka * succeeds in this zone. 154853853e2dSVlastimil Babka */ 154953853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 15501f9efdefSVlastimil Babka /* 15511f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 15521f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 15531f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 15541f9efdefSVlastimil Babka * however still fail so we better signal the 15551f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 15561f9efdefSVlastimil Babka * prevent the allocation attempt). 15571f9efdefSVlastimil Babka */ 15581f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 15591f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15601f9efdefSVlastimil Babka 15611f9efdefSVlastimil Babka goto break_loop; 15621f9efdefSVlastimil Babka } 15631f9efdefSVlastimil Babka 1564f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 156553853e2dSVlastimil Babka /* 156653853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 156753853e2dSVlastimil Babka * so we defer compaction there. If it ends up 156853853e2dSVlastimil Babka * succeeding after all, it will be reset. 156953853e2dSVlastimil Babka */ 157053853e2dSVlastimil Babka defer_compaction(zone, order); 157153853e2dSVlastimil Babka } 15721f9efdefSVlastimil Babka 15731f9efdefSVlastimil Babka /* 15741f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 15751f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 15761f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 15771f9efdefSVlastimil Babka * contention. 15781f9efdefSVlastimil Babka */ 15791f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 15801f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 15811f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15821f9efdefSVlastimil Babka goto break_loop; 158356de7263SMel Gorman } 158456de7263SMel Gorman 15851f9efdefSVlastimil Babka continue; 15861f9efdefSVlastimil Babka break_loop: 15871f9efdefSVlastimil Babka /* 15881f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 15891f9efdefSVlastimil Babka * and assume they are not all lock contended. 15901f9efdefSVlastimil Babka */ 15911f9efdefSVlastimil Babka all_zones_contended = 0; 15921f9efdefSVlastimil Babka break; 15931f9efdefSVlastimil Babka } 15941f9efdefSVlastimil Babka 15951f9efdefSVlastimil Babka /* 15961f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 15971f9efdefSVlastimil Babka * zones that were tried were lock contended. 15981f9efdefSVlastimil Babka */ 15991f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 16001f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 16011f9efdefSVlastimil Babka 160256de7263SMel Gorman return rc; 160356de7263SMel Gorman } 160456de7263SMel Gorman 160556de7263SMel Gorman 160676ab0f53SMel Gorman /* Compact all zones within a node */ 16077103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 160876ab0f53SMel Gorman { 160976ab0f53SMel Gorman int zoneid; 161076ab0f53SMel Gorman struct zone *zone; 161176ab0f53SMel Gorman 161276ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 161376ab0f53SMel Gorman 161476ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 161576ab0f53SMel Gorman if (!populated_zone(zone)) 161676ab0f53SMel Gorman continue; 161776ab0f53SMel Gorman 16187be62de9SRik van Riel cc->nr_freepages = 0; 16197be62de9SRik van Riel cc->nr_migratepages = 0; 16207be62de9SRik van Riel cc->zone = zone; 16217be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 16227be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 162376ab0f53SMel Gorman 1624195b0c60SGioh Kim /* 1625195b0c60SGioh Kim * When called via /proc/sys/vm/compact_memory 1626195b0c60SGioh Kim * this makes sure we compact the whole zone regardless of 1627195b0c60SGioh Kim * cached scanner positions. 1628195b0c60SGioh Kim */ 162921c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 1630195b0c60SGioh Kim __reset_isolation_suitable(zone); 1631195b0c60SGioh Kim 163221c527a3SYaowei Bai if (is_via_compact_memory(cc->order) || 163321c527a3SYaowei Bai !compaction_deferred(zone, cc->order)) 16347be62de9SRik van Riel compact_zone(zone, cc); 163576ab0f53SMel Gorman 163675469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->freepages)); 163775469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->migratepages)); 163875469345SJoonsoo Kim 163975469345SJoonsoo Kim if (is_via_compact_memory(cc->order)) 164075469345SJoonsoo Kim continue; 164175469345SJoonsoo Kim 1642de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1643de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1644de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1645aff62249SRik van Riel } 164676ab0f53SMel Gorman } 164776ab0f53SMel Gorman 16487103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 16497be62de9SRik van Riel { 16507be62de9SRik van Riel struct compact_control cc = { 16517be62de9SRik van Riel .order = order, 1652e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 16537be62de9SRik van Riel }; 16547be62de9SRik van Riel 16553a7200afSMel Gorman if (!order) 16563a7200afSMel Gorman return; 16573a7200afSMel Gorman 16587103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 16597be62de9SRik van Riel } 16607be62de9SRik van Riel 16617103f16dSAndrew Morton static void compact_node(int nid) 16627be62de9SRik van Riel { 16637be62de9SRik van Riel struct compact_control cc = { 16647be62de9SRik van Riel .order = -1, 1665e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 166691ca9186SDavid Rientjes .ignore_skip_hint = true, 16677be62de9SRik van Riel }; 16687be62de9SRik van Riel 16697103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 16707be62de9SRik van Riel } 16717be62de9SRik van Riel 167276ab0f53SMel Gorman /* Compact all nodes in the system */ 16737964c06dSJason Liu static void compact_nodes(void) 167476ab0f53SMel Gorman { 167576ab0f53SMel Gorman int nid; 167676ab0f53SMel Gorman 16778575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 16788575ec29SHugh Dickins lru_add_drain_all(); 16798575ec29SHugh Dickins 168076ab0f53SMel Gorman for_each_online_node(nid) 168176ab0f53SMel Gorman compact_node(nid); 168276ab0f53SMel Gorman } 168376ab0f53SMel Gorman 168476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 168576ab0f53SMel Gorman int sysctl_compact_memory; 168676ab0f53SMel Gorman 1687fec4eb2cSYaowei Bai /* 1688fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1689fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1690fec4eb2cSYaowei Bai */ 169176ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 169276ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 169376ab0f53SMel Gorman { 169476ab0f53SMel Gorman if (write) 16957964c06dSJason Liu compact_nodes(); 169676ab0f53SMel Gorman 169776ab0f53SMel Gorman return 0; 169876ab0f53SMel Gorman } 1699ed4a6d7fSMel Gorman 17005e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 17015e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 17025e771905SMel Gorman { 17035e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 17045e771905SMel Gorman 17055e771905SMel Gorman return 0; 17065e771905SMel Gorman } 17075e771905SMel Gorman 1708ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 170974e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 171010fbcf4cSKay Sievers struct device_attribute *attr, 1711ed4a6d7fSMel Gorman const char *buf, size_t count) 1712ed4a6d7fSMel Gorman { 17138575ec29SHugh Dickins int nid = dev->id; 17148575ec29SHugh Dickins 17158575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 17168575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17178575ec29SHugh Dickins lru_add_drain_all(); 17188575ec29SHugh Dickins 17198575ec29SHugh Dickins compact_node(nid); 17208575ec29SHugh Dickins } 1721ed4a6d7fSMel Gorman 1722ed4a6d7fSMel Gorman return count; 1723ed4a6d7fSMel Gorman } 172410fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1725ed4a6d7fSMel Gorman 1726ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1727ed4a6d7fSMel Gorman { 172810fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1729ed4a6d7fSMel Gorman } 1730ed4a6d7fSMel Gorman 1731ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1732ed4a6d7fSMel Gorman { 173310fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1734ed4a6d7fSMel Gorman } 1735ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1736ff9543fdSMichal Nazarewicz 1737698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 1738698b1b30SVlastimil Babka { 1739172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 1740698b1b30SVlastimil Babka } 1741698b1b30SVlastimil Babka 1742698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 1743698b1b30SVlastimil Babka { 1744698b1b30SVlastimil Babka int zoneid; 1745698b1b30SVlastimil Babka struct zone *zone; 1746698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 1747698b1b30SVlastimil Babka 1748698b1b30SVlastimil Babka for (zoneid = 0; zoneid < classzone_idx; zoneid++) { 1749698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1750698b1b30SVlastimil Babka 1751698b1b30SVlastimil Babka if (!populated_zone(zone)) 1752698b1b30SVlastimil Babka continue; 1753698b1b30SVlastimil Babka 1754698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 1755698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 1756698b1b30SVlastimil Babka return true; 1757698b1b30SVlastimil Babka } 1758698b1b30SVlastimil Babka 1759698b1b30SVlastimil Babka return false; 1760698b1b30SVlastimil Babka } 1761698b1b30SVlastimil Babka 1762698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 1763698b1b30SVlastimil Babka { 1764698b1b30SVlastimil Babka /* 1765698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 1766698b1b30SVlastimil Babka * order is allocatable. 1767698b1b30SVlastimil Babka */ 1768698b1b30SVlastimil Babka int zoneid; 1769698b1b30SVlastimil Babka struct zone *zone; 1770698b1b30SVlastimil Babka struct compact_control cc = { 1771698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 1772698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 1773698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 1774698b1b30SVlastimil Babka .ignore_skip_hint = true, 1775698b1b30SVlastimil Babka 1776698b1b30SVlastimil Babka }; 1777698b1b30SVlastimil Babka bool success = false; 1778698b1b30SVlastimil Babka 1779698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 1780698b1b30SVlastimil Babka cc.classzone_idx); 1781698b1b30SVlastimil Babka count_vm_event(KCOMPACTD_WAKE); 1782698b1b30SVlastimil Babka 1783698b1b30SVlastimil Babka for (zoneid = 0; zoneid < cc.classzone_idx; zoneid++) { 1784698b1b30SVlastimil Babka int status; 1785698b1b30SVlastimil Babka 1786698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1787698b1b30SVlastimil Babka if (!populated_zone(zone)) 1788698b1b30SVlastimil Babka continue; 1789698b1b30SVlastimil Babka 1790698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 1791698b1b30SVlastimil Babka continue; 1792698b1b30SVlastimil Babka 1793698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 1794698b1b30SVlastimil Babka COMPACT_CONTINUE) 1795698b1b30SVlastimil Babka continue; 1796698b1b30SVlastimil Babka 1797698b1b30SVlastimil Babka cc.nr_freepages = 0; 1798698b1b30SVlastimil Babka cc.nr_migratepages = 0; 1799698b1b30SVlastimil Babka cc.zone = zone; 1800698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1801698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1802698b1b30SVlastimil Babka 1803172400c6SVlastimil Babka if (kthread_should_stop()) 1804172400c6SVlastimil Babka return; 1805698b1b30SVlastimil Babka status = compact_zone(zone, &cc); 1806698b1b30SVlastimil Babka 1807698b1b30SVlastimil Babka if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone), 1808698b1b30SVlastimil Babka cc.classzone_idx, 0)) { 1809698b1b30SVlastimil Babka success = true; 1810698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 1811698b1b30SVlastimil Babka } else if (status == COMPACT_COMPLETE) { 1812698b1b30SVlastimil Babka /* 1813698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 1814698b1b30SVlastimil Babka * sync direct compaction does. 1815698b1b30SVlastimil Babka */ 1816698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 1817698b1b30SVlastimil Babka } 1818698b1b30SVlastimil Babka 1819698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1820698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1821698b1b30SVlastimil Babka } 1822698b1b30SVlastimil Babka 1823698b1b30SVlastimil Babka /* 1824698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 1825698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 1826698b1b30SVlastimil Babka * our current ones 1827698b1b30SVlastimil Babka */ 1828698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 1829698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1830698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 1831698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1832698b1b30SVlastimil Babka } 1833698b1b30SVlastimil Babka 1834698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 1835698b1b30SVlastimil Babka { 1836698b1b30SVlastimil Babka if (!order) 1837698b1b30SVlastimil Babka return; 1838698b1b30SVlastimil Babka 1839698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 1840698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 1841698b1b30SVlastimil Babka 1842698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 1843698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 1844698b1b30SVlastimil Babka 1845698b1b30SVlastimil Babka if (!waitqueue_active(&pgdat->kcompactd_wait)) 1846698b1b30SVlastimil Babka return; 1847698b1b30SVlastimil Babka 1848698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 1849698b1b30SVlastimil Babka return; 1850698b1b30SVlastimil Babka 1851698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 1852698b1b30SVlastimil Babka classzone_idx); 1853698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 1854698b1b30SVlastimil Babka } 1855698b1b30SVlastimil Babka 1856698b1b30SVlastimil Babka /* 1857698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 1858698b1b30SVlastimil Babka * from the init process. 1859698b1b30SVlastimil Babka */ 1860698b1b30SVlastimil Babka static int kcompactd(void *p) 1861698b1b30SVlastimil Babka { 1862698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 1863698b1b30SVlastimil Babka struct task_struct *tsk = current; 1864698b1b30SVlastimil Babka 1865698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 1866698b1b30SVlastimil Babka 1867698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 1868698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 1869698b1b30SVlastimil Babka 1870698b1b30SVlastimil Babka set_freezable(); 1871698b1b30SVlastimil Babka 1872698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1873698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1874698b1b30SVlastimil Babka 1875698b1b30SVlastimil Babka while (!kthread_should_stop()) { 1876698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 1877698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 1878698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 1879698b1b30SVlastimil Babka 1880698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 1881698b1b30SVlastimil Babka } 1882698b1b30SVlastimil Babka 1883698b1b30SVlastimil Babka return 0; 1884698b1b30SVlastimil Babka } 1885698b1b30SVlastimil Babka 1886698b1b30SVlastimil Babka /* 1887698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 1888698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 1889698b1b30SVlastimil Babka */ 1890698b1b30SVlastimil Babka int kcompactd_run(int nid) 1891698b1b30SVlastimil Babka { 1892698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1893698b1b30SVlastimil Babka int ret = 0; 1894698b1b30SVlastimil Babka 1895698b1b30SVlastimil Babka if (pgdat->kcompactd) 1896698b1b30SVlastimil Babka return 0; 1897698b1b30SVlastimil Babka 1898698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 1899698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 1900698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 1901698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 1902698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 1903698b1b30SVlastimil Babka } 1904698b1b30SVlastimil Babka return ret; 1905698b1b30SVlastimil Babka } 1906698b1b30SVlastimil Babka 1907698b1b30SVlastimil Babka /* 1908698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 1909698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 1910698b1b30SVlastimil Babka */ 1911698b1b30SVlastimil Babka void kcompactd_stop(int nid) 1912698b1b30SVlastimil Babka { 1913698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 1914698b1b30SVlastimil Babka 1915698b1b30SVlastimil Babka if (kcompactd) { 1916698b1b30SVlastimil Babka kthread_stop(kcompactd); 1917698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 1918698b1b30SVlastimil Babka } 1919698b1b30SVlastimil Babka } 1920698b1b30SVlastimil Babka 1921698b1b30SVlastimil Babka /* 1922698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 1923698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 1924698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 1925698b1b30SVlastimil Babka * restore their cpu bindings. 1926698b1b30SVlastimil Babka */ 1927698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action, 1928698b1b30SVlastimil Babka void *hcpu) 1929698b1b30SVlastimil Babka { 1930698b1b30SVlastimil Babka int nid; 1931698b1b30SVlastimil Babka 1932698b1b30SVlastimil Babka if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 1933698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 1934698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1935698b1b30SVlastimil Babka const struct cpumask *mask; 1936698b1b30SVlastimil Babka 1937698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 1938698b1b30SVlastimil Babka 1939698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 1940698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 1941698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 1942698b1b30SVlastimil Babka } 1943698b1b30SVlastimil Babka } 1944698b1b30SVlastimil Babka return NOTIFY_OK; 1945698b1b30SVlastimil Babka } 1946698b1b30SVlastimil Babka 1947698b1b30SVlastimil Babka static int __init kcompactd_init(void) 1948698b1b30SVlastimil Babka { 1949698b1b30SVlastimil Babka int nid; 1950698b1b30SVlastimil Babka 1951698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 1952698b1b30SVlastimil Babka kcompactd_run(nid); 1953698b1b30SVlastimil Babka hotcpu_notifier(cpu_callback, 0); 1954698b1b30SVlastimil Babka return 0; 1955698b1b30SVlastimil Babka } 1956698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 1957698b1b30SVlastimil Babka 1958ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1959