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 45*06b6640aSVlastimil Babka #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) 46*06b6640aSVlastimil Babka #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) 47*06b6640aSVlastimil Babka #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) 48*06b6640aSVlastimil Babka #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) 49*06b6640aSVlastimil 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 = 169*06b6640aSVlastimil 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; 527*06b6640aSVlastimil 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; 530*06b6640aSVlastimil 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) { 546*06b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 547*06b6640aSVlastimil 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 790748446bbSMel Gorman /* Avoid isolating too much */ 79131b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 79231b8384aSHillf Danton ++low_pfn; 793748446bbSMel Gorman break; 794748446bbSMel Gorman } 79531b8384aSHillf Danton } 796748446bbSMel Gorman 79799c0fd5eSVlastimil Babka /* 79899c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 79999c0fd5eSVlastimil Babka * the range to be scanned. 80099c0fd5eSVlastimil Babka */ 80199c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 80299c0fd5eSVlastimil Babka low_pfn = end_pfn; 80399c0fd5eSVlastimil Babka 804c67fe375SMel Gorman if (locked) 805c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 806748446bbSMel Gorman 80750b5b094SVlastimil Babka /* 80850b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 80950b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 81050b5b094SVlastimil Babka */ 81135979ef3SDavid Rientjes if (low_pfn == end_pfn) 812edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 813bb13ffebSMel Gorman 814e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 815e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 816b7aba698SMel Gorman 817010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 818397487dbSMel Gorman if (nr_isolated) 819010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 820397487dbSMel Gorman 8212fe86e00SMichal Nazarewicz return low_pfn; 8222fe86e00SMichal Nazarewicz } 8232fe86e00SMichal Nazarewicz 824edc2ca61SVlastimil Babka /** 825edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 826edc2ca61SVlastimil Babka * @cc: Compaction control structure. 827edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 828edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 829edc2ca61SVlastimil Babka * 830edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 831edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 832edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 833edc2ca61SVlastimil Babka */ 834edc2ca61SVlastimil Babka unsigned long 835edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 836edc2ca61SVlastimil Babka unsigned long end_pfn) 837edc2ca61SVlastimil Babka { 838e1409c32SJoonsoo Kim unsigned long pfn, block_start_pfn, block_end_pfn; 839edc2ca61SVlastimil Babka 840edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 841edc2ca61SVlastimil Babka pfn = start_pfn; 842*06b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(pfn); 843e1409c32SJoonsoo Kim if (block_start_pfn < cc->zone->zone_start_pfn) 844e1409c32SJoonsoo Kim block_start_pfn = cc->zone->zone_start_pfn; 845*06b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(pfn); 846edc2ca61SVlastimil Babka 847edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 848e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 849edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 850edc2ca61SVlastimil Babka 851edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 852edc2ca61SVlastimil Babka 853e1409c32SJoonsoo Kim if (!pageblock_pfn_to_page(block_start_pfn, 854e1409c32SJoonsoo Kim block_end_pfn, cc->zone)) 855edc2ca61SVlastimil Babka continue; 856edc2ca61SVlastimil Babka 857edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 858edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 859edc2ca61SVlastimil Babka 86014af4a5eSHugh Dickins if (!pfn) 861edc2ca61SVlastimil Babka break; 8626ea41c0cSJoonsoo Kim 8636ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8646ea41c0cSJoonsoo Kim break; 865edc2ca61SVlastimil Babka } 866edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 867edc2ca61SVlastimil Babka 868edc2ca61SVlastimil Babka return pfn; 869edc2ca61SVlastimil Babka } 870edc2ca61SVlastimil Babka 871ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 872ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 873018e9a49SAndrew Morton 874018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 875018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page) 876018e9a49SAndrew Morton { 877018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 878018e9a49SAndrew Morton if (PageBuddy(page)) { 879018e9a49SAndrew Morton /* 880018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 881018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 882018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 883018e9a49SAndrew Morton */ 884018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 885018e9a49SAndrew Morton return false; 886018e9a49SAndrew Morton } 887018e9a49SAndrew Morton 888018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 889018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 890018e9a49SAndrew Morton return true; 891018e9a49SAndrew Morton 892018e9a49SAndrew Morton /* Otherwise skip the block */ 893018e9a49SAndrew Morton return false; 894018e9a49SAndrew Morton } 895018e9a49SAndrew Morton 896ff9543fdSMichal Nazarewicz /* 897f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 898f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 899f2849aa0SVlastimil Babka */ 900f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 901f2849aa0SVlastimil Babka { 902f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 903f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 904f2849aa0SVlastimil Babka } 905f2849aa0SVlastimil Babka 906f2849aa0SVlastimil Babka /* 907ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 908ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 909ff9543fdSMichal Nazarewicz */ 910edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 911ff9543fdSMichal Nazarewicz { 912edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 913ff9543fdSMichal Nazarewicz struct page *page; 914c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 915e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 916c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 917c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 918ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9192fe86e00SMichal Nazarewicz 920ff9543fdSMichal Nazarewicz /* 921ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 92249e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 923e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 924e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 925c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 926c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 927c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 92849e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 92949e068f0SVlastimil Babka * is using. 930ff9543fdSMichal Nazarewicz */ 931e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 932*06b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(cc->free_pfn); 933c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 934c96b9e50SVlastimil Babka zone_end_pfn(zone)); 935*06b6640aSVlastimil Babka low_pfn = pageblock_end_pfn(cc->migrate_pfn); 9362fe86e00SMichal Nazarewicz 937ff9543fdSMichal Nazarewicz /* 938ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 939ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 940ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 941ff9543fdSMichal Nazarewicz */ 942f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 943c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 944e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 945e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 946ff9543fdSMichal Nazarewicz 947f6ea3adbSDavid Rientjes /* 948f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 949f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 950be976572SVlastimil Babka * to schedule, or even abort async compaction. 951f6ea3adbSDavid Rientjes */ 952be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 953be976572SVlastimil Babka && compact_should_abort(cc)) 954be976572SVlastimil Babka break; 955f6ea3adbSDavid Rientjes 9567d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 9577d49d886SVlastimil Babka zone); 9587d49d886SVlastimil Babka if (!page) 959ff9543fdSMichal Nazarewicz continue; 960ff9543fdSMichal Nazarewicz 961ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 96268e3e926SLinus Torvalds if (!suitable_migration_target(page)) 963ff9543fdSMichal Nazarewicz continue; 96468e3e926SLinus Torvalds 965bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 966bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 967bb13ffebSMel Gorman continue; 968bb13ffebSMel Gorman 969e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 970932ff6bbSJoonsoo Kim isolate_freepages_block(cc, &isolate_start_pfn, 971c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 972ff9543fdSMichal Nazarewicz 973ff9543fdSMichal Nazarewicz /* 974f5f61a32SVlastimil Babka * If we isolated enough freepages, or aborted due to async 975f5f61a32SVlastimil Babka * compaction being contended, terminate the loop. 976e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 977e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 978e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 979e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 980e14c720eSVlastimil Babka * pageblock. 981e14c720eSVlastimil Babka * In that case we will however want to restart at the start 982e14c720eSVlastimil Babka * of the previous pageblock. 983e14c720eSVlastimil Babka */ 984f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 985f5f61a32SVlastimil Babka || cc->contended) { 986f5f61a32SVlastimil Babka if (isolate_start_pfn >= block_end_pfn) 987f5f61a32SVlastimil Babka isolate_start_pfn = 988e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 989be976572SVlastimil Babka break; 990f5f61a32SVlastimil Babka } else { 991f5f61a32SVlastimil Babka /* 992f5f61a32SVlastimil Babka * isolate_freepages_block() should not terminate 993f5f61a32SVlastimil Babka * prematurely unless contended, or isolated enough 994f5f61a32SVlastimil Babka */ 995f5f61a32SVlastimil Babka VM_BUG_ON(isolate_start_pfn < block_end_pfn); 996f5f61a32SVlastimil Babka } 997c89511abSMel Gorman } 998ff9543fdSMichal Nazarewicz 999ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 1000ff9543fdSMichal Nazarewicz map_pages(freelist); 1001ff9543fdSMichal Nazarewicz 10027ed695e0SVlastimil Babka /* 1003f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1004f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1005f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1006f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 10077ed695e0SVlastimil Babka */ 1008f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1009748446bbSMel Gorman } 1010748446bbSMel Gorman 1011748446bbSMel Gorman /* 1012748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1013748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1014748446bbSMel Gorman */ 1015748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1016748446bbSMel Gorman unsigned long data, 1017748446bbSMel Gorman int **result) 1018748446bbSMel Gorman { 1019748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1020748446bbSMel Gorman struct page *freepage; 1021748446bbSMel Gorman 1022be976572SVlastimil Babka /* 1023be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1024be976572SVlastimil Babka * contention. 1025be976572SVlastimil Babka */ 1026748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1027be976572SVlastimil Babka if (!cc->contended) 1028edc2ca61SVlastimil Babka isolate_freepages(cc); 1029748446bbSMel Gorman 1030748446bbSMel Gorman if (list_empty(&cc->freepages)) 1031748446bbSMel Gorman return NULL; 1032748446bbSMel Gorman } 1033748446bbSMel Gorman 1034748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1035748446bbSMel Gorman list_del(&freepage->lru); 1036748446bbSMel Gorman cc->nr_freepages--; 1037748446bbSMel Gorman 1038748446bbSMel Gorman return freepage; 1039748446bbSMel Gorman } 1040748446bbSMel Gorman 1041748446bbSMel Gorman /* 1042d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1043d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1044d53aea3dSDavid Rientjes * special handling needed for NUMA. 1045d53aea3dSDavid Rientjes */ 1046d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1047d53aea3dSDavid Rientjes { 1048d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1049d53aea3dSDavid Rientjes 1050d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1051d53aea3dSDavid Rientjes cc->nr_freepages++; 1052d53aea3dSDavid Rientjes } 1053d53aea3dSDavid Rientjes 1054ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1055ff9543fdSMichal Nazarewicz typedef enum { 1056ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1057ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1058ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1059ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1060ff9543fdSMichal Nazarewicz 1061ff9543fdSMichal Nazarewicz /* 10625bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 10635bbe3547SEric B Munson * compactable pages. 10645bbe3547SEric B Munson */ 10655bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 10665bbe3547SEric B Munson 10675bbe3547SEric B Munson /* 1068edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1069edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1070edc2ca61SVlastimil Babka * compact_control. 1071ff9543fdSMichal Nazarewicz */ 1072ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1073ff9543fdSMichal Nazarewicz struct compact_control *cc) 1074ff9543fdSMichal Nazarewicz { 1075e1409c32SJoonsoo Kim unsigned long block_start_pfn; 1076e1409c32SJoonsoo Kim unsigned long block_end_pfn; 1077e1409c32SJoonsoo Kim unsigned long low_pfn; 10781a16718cSJoonsoo Kim unsigned long isolate_start_pfn; 1079edc2ca61SVlastimil Babka struct page *page; 1080edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 10815bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 1082edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1083ff9543fdSMichal Nazarewicz 1084edc2ca61SVlastimil Babka /* 1085edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1086edc2ca61SVlastimil Babka * initialized by compact_zone() 1087edc2ca61SVlastimil Babka */ 1088edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 1089*06b6640aSVlastimil Babka block_start_pfn = pageblock_start_pfn(low_pfn); 1090e1409c32SJoonsoo Kim if (block_start_pfn < zone->zone_start_pfn) 1091e1409c32SJoonsoo Kim block_start_pfn = zone->zone_start_pfn; 1092ff9543fdSMichal Nazarewicz 1093ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 1094*06b6640aSVlastimil Babka block_end_pfn = pageblock_end_pfn(low_pfn); 1095ff9543fdSMichal Nazarewicz 1096edc2ca61SVlastimil Babka /* 1097edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1098edc2ca61SVlastimil Babka * Do not cross the free scanner. 1099edc2ca61SVlastimil Babka */ 1100e1409c32SJoonsoo Kim for (; block_end_pfn <= cc->free_pfn; 1101e1409c32SJoonsoo Kim low_pfn = block_end_pfn, 1102e1409c32SJoonsoo Kim block_start_pfn = block_end_pfn, 1103e1409c32SJoonsoo Kim block_end_pfn += pageblock_nr_pages) { 1104edc2ca61SVlastimil Babka 1105edc2ca61SVlastimil Babka /* 1106edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1107edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1108edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1109edc2ca61SVlastimil Babka */ 1110edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1111edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1112edc2ca61SVlastimil Babka break; 1113edc2ca61SVlastimil Babka 1114e1409c32SJoonsoo Kim page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 1115e1409c32SJoonsoo Kim zone); 11167d49d886SVlastimil Babka if (!page) 1117edc2ca61SVlastimil Babka continue; 1118edc2ca61SVlastimil Babka 1119edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1120edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1121edc2ca61SVlastimil Babka continue; 1122edc2ca61SVlastimil Babka 1123edc2ca61SVlastimil Babka /* 1124edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1125edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1126edc2ca61SVlastimil Babka * of work satisfies the allocation. 1127edc2ca61SVlastimil Babka */ 1128edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1129edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1130edc2ca61SVlastimil Babka continue; 1131ff9543fdSMichal Nazarewicz 1132ff9543fdSMichal Nazarewicz /* Perform the isolation */ 11331a16718cSJoonsoo Kim isolate_start_pfn = low_pfn; 1134e1409c32SJoonsoo Kim low_pfn = isolate_migratepages_block(cc, low_pfn, 1135e1409c32SJoonsoo Kim block_end_pfn, isolate_mode); 1136edc2ca61SVlastimil Babka 1137ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1138ff59909aSHugh Dickins acct_isolated(zone, cc); 1139ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1140ff59909aSHugh Dickins } 1141ff9543fdSMichal Nazarewicz 1142edc2ca61SVlastimil Babka /* 11431a16718cSJoonsoo Kim * Record where we could have freed pages by migration and not 11441a16718cSJoonsoo Kim * yet flushed them to buddy allocator. 11451a16718cSJoonsoo Kim * - this is the lowest page that could have been isolated and 11461a16718cSJoonsoo Kim * then freed by migration. 11471a16718cSJoonsoo Kim */ 11481a16718cSJoonsoo Kim if (cc->nr_migratepages && !cc->last_migrated_pfn) 11491a16718cSJoonsoo Kim cc->last_migrated_pfn = isolate_start_pfn; 11501a16718cSJoonsoo Kim 11511a16718cSJoonsoo Kim /* 1152edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1153edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1154edc2ca61SVlastimil Babka * continue or not. 1155edc2ca61SVlastimil Babka */ 1156edc2ca61SVlastimil Babka break; 1157edc2ca61SVlastimil Babka } 1158edc2ca61SVlastimil Babka 1159edc2ca61SVlastimil Babka acct_isolated(zone, cc); 1160f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1161f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1162ff9543fdSMichal Nazarewicz 1163edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1164ff9543fdSMichal Nazarewicz } 1165ff9543fdSMichal Nazarewicz 116621c527a3SYaowei Bai /* 116721c527a3SYaowei Bai * order == -1 is expected when compacting via 116821c527a3SYaowei Bai * /proc/sys/vm/compact_memory 116921c527a3SYaowei Bai */ 117021c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 117121c527a3SYaowei Bai { 117221c527a3SYaowei Bai return order == -1; 117321c527a3SYaowei Bai } 117421c527a3SYaowei Bai 1175837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc, 11766d7ce559SDavid Rientjes const int migratetype) 1177748446bbSMel Gorman { 11788fb74b9fSMel Gorman unsigned int order; 11795a03b051SAndrea Arcangeli unsigned long watermark; 118056de7263SMel Gorman 1181be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 11822d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1183748446bbSMel Gorman 1184753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1185f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 118655b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 118702333641SVlastimil Babka reset_cached_positions(zone); 118855b7c4c9SVlastimil Babka 118962997027SMel Gorman /* 119062997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 1191accf6242SVlastimil Babka * by kswapd when it goes to sleep. kcompactd does not set the 119262997027SMel Gorman * flag itself as the decision to be clear should be directly 119362997027SMel Gorman * based on an allocation request. 119462997027SMel Gorman */ 1195accf6242SVlastimil Babka if (cc->direct_compaction) 119662997027SMel Gorman zone->compact_blockskip_flush = true; 119762997027SMel Gorman 1198748446bbSMel Gorman return COMPACT_COMPLETE; 1199bb13ffebSMel Gorman } 1200748446bbSMel Gorman 120121c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 120256de7263SMel Gorman return COMPACT_CONTINUE; 120356de7263SMel Gorman 12043957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 12053957c776SMichal Hocko watermark = low_wmark_pages(zone); 12063957c776SMichal Hocko 1207ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1208ebff3980SVlastimil Babka cc->alloc_flags)) 12093957c776SMichal Hocko return COMPACT_CONTINUE; 12103957c776SMichal Hocko 121156de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 121256de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 12138fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 12142149cdaeSJoonsoo Kim bool can_steal; 12158fb74b9fSMel Gorman 121656de7263SMel Gorman /* Job done if page is free of the right migratetype */ 12176d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 121856de7263SMel Gorman return COMPACT_PARTIAL; 121956de7263SMel Gorman 12202149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 12212149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 12222149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 12232149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 12242149cdaeSJoonsoo Kim return COMPACT_PARTIAL; 12252149cdaeSJoonsoo Kim #endif 12262149cdaeSJoonsoo Kim /* 12272149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 12282149cdaeSJoonsoo Kim * other migratetype buddy lists. 12292149cdaeSJoonsoo Kim */ 12302149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 12312149cdaeSJoonsoo Kim true, &can_steal) != -1) 123256de7263SMel Gorman return COMPACT_PARTIAL; 123356de7263SMel Gorman } 123456de7263SMel Gorman 1235837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1236837d026dSJoonsoo Kim } 1237837d026dSJoonsoo Kim 1238837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc, 1239837d026dSJoonsoo Kim const int migratetype) 1240837d026dSJoonsoo Kim { 1241837d026dSJoonsoo Kim int ret; 1242837d026dSJoonsoo Kim 1243837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1244837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1245837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1246837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1247837d026dSJoonsoo Kim 1248837d026dSJoonsoo Kim return ret; 1249748446bbSMel Gorman } 1250748446bbSMel Gorman 12513e7d3449SMel Gorman /* 12523e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 12533e7d3449SMel Gorman * Returns 12543e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 12553e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 12563e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 12573e7d3449SMel Gorman */ 1258837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order, 1259ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 12603e7d3449SMel Gorman { 12613e7d3449SMel Gorman int fragindex; 12623e7d3449SMel Gorman unsigned long watermark; 12633e7d3449SMel Gorman 126421c527a3SYaowei Bai if (is_via_compact_memory(order)) 12653957c776SMichal Hocko return COMPACT_CONTINUE; 12663957c776SMichal Hocko 1267ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1268ebff3980SVlastimil Babka /* 1269ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1270ebff3980SVlastimil Babka * should be no need for compaction at all. 1271ebff3980SVlastimil Babka */ 1272ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1273ebff3980SVlastimil Babka alloc_flags)) 1274ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1275ebff3980SVlastimil Babka 12763957c776SMichal Hocko /* 12773e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 12783e7d3449SMel Gorman * This is because during migration, copies of pages need to be 12793e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 12803e7d3449SMel Gorman */ 1281ebff3980SVlastimil Babka watermark += (2UL << order); 1282ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 12833e7d3449SMel Gorman return COMPACT_SKIPPED; 12843e7d3449SMel Gorman 12853e7d3449SMel Gorman /* 12863e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 12873e7d3449SMel Gorman * low memory or external fragmentation 12883e7d3449SMel Gorman * 1289ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1290ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 12913e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 12923e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 12933e7d3449SMel Gorman * 12943e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 12953e7d3449SMel Gorman */ 12963e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 12973e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1298837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 12993e7d3449SMel Gorman 13003e7d3449SMel Gorman return COMPACT_CONTINUE; 13013e7d3449SMel Gorman } 13023e7d3449SMel Gorman 1303837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order, 1304837d026dSJoonsoo Kim int alloc_flags, int classzone_idx) 1305837d026dSJoonsoo Kim { 1306837d026dSJoonsoo Kim unsigned long ret; 1307837d026dSJoonsoo Kim 1308837d026dSJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx); 1309837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1310837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1311837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1312837d026dSJoonsoo Kim 1313837d026dSJoonsoo Kim return ret; 1314837d026dSJoonsoo Kim } 1315837d026dSJoonsoo Kim 1316748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1317748446bbSMel Gorman { 1318748446bbSMel Gorman int ret; 1319c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1320108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 13216d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1322e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1323748446bbSMel Gorman 1324ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1325ebff3980SVlastimil Babka cc->classzone_idx); 13263e7d3449SMel Gorman switch (ret) { 13273e7d3449SMel Gorman case COMPACT_PARTIAL: 13283e7d3449SMel Gorman case COMPACT_SKIPPED: 13293e7d3449SMel Gorman /* Compaction is likely to fail */ 13303e7d3449SMel Gorman return ret; 13313e7d3449SMel Gorman case COMPACT_CONTINUE: 13323e7d3449SMel Gorman /* Fall through to compaction */ 13333e7d3449SMel Gorman ; 13343e7d3449SMel Gorman } 13353e7d3449SMel Gorman 1336c89511abSMel Gorman /* 1337d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1338accf6242SVlastimil Babka * is about to be retried after being deferred. 1339d3132e4bSVlastimil Babka */ 1340accf6242SVlastimil Babka if (compaction_restarting(zone, cc->order)) 1341d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1342d3132e4bSVlastimil Babka 1343d3132e4bSVlastimil Babka /* 1344c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1345c89511abSMel Gorman * information on where the scanners should start but check that it 1346c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1347c89511abSMel Gorman */ 1348e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1349c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1350623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 1351*06b6640aSVlastimil Babka cc->free_pfn = pageblock_start_pfn(end_pfn - 1); 1352c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1353c89511abSMel Gorman } 1354623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1355c89511abSMel Gorman cc->migrate_pfn = start_pfn; 135635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 135735979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1358c89511abSMel Gorman } 13591a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1360748446bbSMel Gorman 136116c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 136216c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 13630eb927c0SMel Gorman 1364748446bbSMel Gorman migrate_prep_local(); 1365748446bbSMel Gorman 13666d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 13676d7ce559SDavid Rientjes COMPACT_CONTINUE) { 13689d502c1cSMinchan Kim int err; 1369748446bbSMel Gorman 1370f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1371f9e35b3bSMel Gorman case ISOLATE_ABORT: 13722d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 13735733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1374e64c5237SShaohua Li cc->nr_migratepages = 0; 1375f9e35b3bSMel Gorman goto out; 1376f9e35b3bSMel Gorman case ISOLATE_NONE: 1377fdaf7f5cSVlastimil Babka /* 1378fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1379fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1380fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1381fdaf7f5cSVlastimil Babka */ 1382fdaf7f5cSVlastimil Babka goto check_drain; 1383f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1384f9e35b3bSMel Gorman ; 1385f9e35b3bSMel Gorman } 1386748446bbSMel Gorman 1387d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1388e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 13897b2a2d4aSMel Gorman MR_COMPACTION); 1390748446bbSMel Gorman 1391f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1392f8c9301fSVlastimil Babka &cc->migratepages); 1393748446bbSMel Gorman 1394f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1395f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 13969d502c1cSMinchan Kim if (err) { 13975733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 13987ed695e0SVlastimil Babka /* 13997ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 14007ed695e0SVlastimil Babka * and we want compact_finished() to detect it 14017ed695e0SVlastimil Babka */ 1402f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 14032d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14044bf2bba3SDavid Rientjes goto out; 1405748446bbSMel Gorman } 14064bf2bba3SDavid Rientjes } 1407fdaf7f5cSVlastimil Babka 1408fdaf7f5cSVlastimil Babka check_drain: 1409fdaf7f5cSVlastimil Babka /* 1410fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1411fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1412fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1413fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1414fdaf7f5cSVlastimil Babka * would succeed. 1415fdaf7f5cSVlastimil Babka */ 14161a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1417fdaf7f5cSVlastimil Babka int cpu; 1418fdaf7f5cSVlastimil Babka unsigned long current_block_start = 1419*06b6640aSVlastimil Babka block_start_pfn(cc->migrate_pfn, cc->order); 1420fdaf7f5cSVlastimil Babka 14211a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1422fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1423fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1424fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1425fdaf7f5cSVlastimil Babka put_cpu(); 1426fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 14271a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1428fdaf7f5cSVlastimil Babka } 1429fdaf7f5cSVlastimil Babka } 1430fdaf7f5cSVlastimil Babka 1431748446bbSMel Gorman } 1432748446bbSMel Gorman 1433f9e35b3bSMel Gorman out: 14346bace090SVlastimil Babka /* 14356bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 14366bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 14376bace090SVlastimil Babka */ 14386bace090SVlastimil Babka if (cc->nr_freepages > 0) { 14396bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 14406bace090SVlastimil Babka 14416bace090SVlastimil Babka cc->nr_freepages = 0; 14426bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 14436bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 1444*06b6640aSVlastimil Babka free_pfn = pageblock_start_pfn(free_pfn); 14456bace090SVlastimil Babka /* 14466bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 14476bace090SVlastimil Babka * already reset to zone end in compact_finished() 14486bace090SVlastimil Babka */ 14496bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 14506bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 14516bace090SVlastimil Babka } 1452748446bbSMel Gorman 145316c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 145416c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 14550eb927c0SMel Gorman 14562d1e1041SVlastimil Babka if (ret == COMPACT_CONTENDED) 14572d1e1041SVlastimil Babka ret = COMPACT_PARTIAL; 14582d1e1041SVlastimil Babka 1459748446bbSMel Gorman return ret; 1460748446bbSMel Gorman } 146176ab0f53SMel Gorman 1462e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1463ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1464ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 146556de7263SMel Gorman { 1466e64c5237SShaohua Li unsigned long ret; 146756de7263SMel Gorman struct compact_control cc = { 146856de7263SMel Gorman .nr_freepages = 0, 146956de7263SMel Gorman .nr_migratepages = 0, 147056de7263SMel Gorman .order = order, 14716d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 147256de7263SMel Gorman .zone = zone, 1473e0b9daebSDavid Rientjes .mode = mode, 1474ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1475ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 1476accf6242SVlastimil Babka .direct_compaction = true, 147756de7263SMel Gorman }; 147856de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 147956de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 148056de7263SMel Gorman 1481e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1482e64c5237SShaohua Li 1483e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1484e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1485e64c5237SShaohua Li 1486e64c5237SShaohua Li *contended = cc.contended; 1487e64c5237SShaohua Li return ret; 148856de7263SMel Gorman } 148956de7263SMel Gorman 14905e771905SMel Gorman int sysctl_extfrag_threshold = 500; 14915e771905SMel Gorman 149256de7263SMel Gorman /** 149356de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 149456de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 14951a6d53a1SVlastimil Babka * @order: The order of the current allocation 14961a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 14971a6d53a1SVlastimil Babka * @ac: The context of current allocation 1498e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 14991f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 15001f9efdefSVlastimil Babka * need_resched() or lock contention 150156de7263SMel Gorman * 150256de7263SMel Gorman * This is the main entry point for direct page compaction. 150356de7263SMel Gorman */ 15041a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 15051a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 15061a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 150756de7263SMel Gorman { 150856de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 150956de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 151056de7263SMel Gorman struct zoneref *z; 151156de7263SMel Gorman struct zone *zone; 151253853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 15131f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 15141f9efdefSVlastimil Babka 15151f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 151656de7263SMel Gorman 15174ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1518c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 151953853e2dSVlastimil Babka return COMPACT_SKIPPED; 152056de7263SMel Gorman 1521837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1522837d026dSJoonsoo Kim 152356de7263SMel Gorman /* Compact each zone in the list */ 15241a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 15251a6d53a1SVlastimil Babka ac->nodemask) { 152656de7263SMel Gorman int status; 15271f9efdefSVlastimil Babka int zone_contended; 152856de7263SMel Gorman 152953853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 153053853e2dSVlastimil Babka continue; 153153853e2dSVlastimil Babka 1532e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 15331a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 15341a6d53a1SVlastimil Babka ac->classzone_idx); 153556de7263SMel Gorman rc = max(status, rc); 15361f9efdefSVlastimil Babka /* 15371f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 15381f9efdefSVlastimil Babka * to clear all_zones_contended. 15391f9efdefSVlastimil Babka */ 15401f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 154156de7263SMel Gorman 15423e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1543ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 15441a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 154553853e2dSVlastimil Babka /* 154653853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 154753853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 154853853e2dSVlastimil Babka * will repeat this with true if allocation indeed 154953853e2dSVlastimil Babka * succeeds in this zone. 155053853e2dSVlastimil Babka */ 155153853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 15521f9efdefSVlastimil Babka /* 15531f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 15541f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 15551f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 15561f9efdefSVlastimil Babka * however still fail so we better signal the 15571f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 15581f9efdefSVlastimil Babka * prevent the allocation attempt). 15591f9efdefSVlastimil Babka */ 15601f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 15611f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15621f9efdefSVlastimil Babka 15631f9efdefSVlastimil Babka goto break_loop; 15641f9efdefSVlastimil Babka } 15651f9efdefSVlastimil Babka 1566f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 156753853e2dSVlastimil Babka /* 156853853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 156953853e2dSVlastimil Babka * so we defer compaction there. If it ends up 157053853e2dSVlastimil Babka * succeeding after all, it will be reset. 157153853e2dSVlastimil Babka */ 157253853e2dSVlastimil Babka defer_compaction(zone, order); 157353853e2dSVlastimil Babka } 15741f9efdefSVlastimil Babka 15751f9efdefSVlastimil Babka /* 15761f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 15771f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 15781f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 15791f9efdefSVlastimil Babka * contention. 15801f9efdefSVlastimil Babka */ 15811f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 15821f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 15831f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15841f9efdefSVlastimil Babka goto break_loop; 158556de7263SMel Gorman } 158656de7263SMel Gorman 15871f9efdefSVlastimil Babka continue; 15881f9efdefSVlastimil Babka break_loop: 15891f9efdefSVlastimil Babka /* 15901f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 15911f9efdefSVlastimil Babka * and assume they are not all lock contended. 15921f9efdefSVlastimil Babka */ 15931f9efdefSVlastimil Babka all_zones_contended = 0; 15941f9efdefSVlastimil Babka break; 15951f9efdefSVlastimil Babka } 15961f9efdefSVlastimil Babka 15971f9efdefSVlastimil Babka /* 15981f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 15991f9efdefSVlastimil Babka * zones that were tried were lock contended. 16001f9efdefSVlastimil Babka */ 16011f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 16021f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 16031f9efdefSVlastimil Babka 160456de7263SMel Gorman return rc; 160556de7263SMel Gorman } 160656de7263SMel Gorman 160756de7263SMel Gorman 160876ab0f53SMel Gorman /* Compact all zones within a node */ 16097103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 161076ab0f53SMel Gorman { 161176ab0f53SMel Gorman int zoneid; 161276ab0f53SMel Gorman struct zone *zone; 161376ab0f53SMel Gorman 161476ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 161576ab0f53SMel Gorman 161676ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 161776ab0f53SMel Gorman if (!populated_zone(zone)) 161876ab0f53SMel Gorman continue; 161976ab0f53SMel Gorman 16207be62de9SRik van Riel cc->nr_freepages = 0; 16217be62de9SRik van Riel cc->nr_migratepages = 0; 16227be62de9SRik van Riel cc->zone = zone; 16237be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 16247be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 162576ab0f53SMel Gorman 1626195b0c60SGioh Kim /* 1627195b0c60SGioh Kim * When called via /proc/sys/vm/compact_memory 1628195b0c60SGioh Kim * this makes sure we compact the whole zone regardless of 1629195b0c60SGioh Kim * cached scanner positions. 1630195b0c60SGioh Kim */ 163121c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 1632195b0c60SGioh Kim __reset_isolation_suitable(zone); 1633195b0c60SGioh Kim 163421c527a3SYaowei Bai if (is_via_compact_memory(cc->order) || 163521c527a3SYaowei Bai !compaction_deferred(zone, cc->order)) 16367be62de9SRik van Riel compact_zone(zone, cc); 163776ab0f53SMel Gorman 163875469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->freepages)); 163975469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->migratepages)); 164075469345SJoonsoo Kim 164175469345SJoonsoo Kim if (is_via_compact_memory(cc->order)) 164275469345SJoonsoo Kim continue; 164375469345SJoonsoo Kim 1644de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1645de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1646de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1647aff62249SRik van Riel } 164876ab0f53SMel Gorman } 164976ab0f53SMel Gorman 16507103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 16517be62de9SRik van Riel { 16527be62de9SRik van Riel struct compact_control cc = { 16537be62de9SRik van Riel .order = order, 1654e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 16557be62de9SRik van Riel }; 16567be62de9SRik van Riel 16573a7200afSMel Gorman if (!order) 16583a7200afSMel Gorman return; 16593a7200afSMel Gorman 16607103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 16617be62de9SRik van Riel } 16627be62de9SRik van Riel 16637103f16dSAndrew Morton static void compact_node(int nid) 16647be62de9SRik van Riel { 16657be62de9SRik van Riel struct compact_control cc = { 16667be62de9SRik van Riel .order = -1, 1667e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 166891ca9186SDavid Rientjes .ignore_skip_hint = true, 16697be62de9SRik van Riel }; 16707be62de9SRik van Riel 16717103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 16727be62de9SRik van Riel } 16737be62de9SRik van Riel 167476ab0f53SMel Gorman /* Compact all nodes in the system */ 16757964c06dSJason Liu static void compact_nodes(void) 167676ab0f53SMel Gorman { 167776ab0f53SMel Gorman int nid; 167876ab0f53SMel Gorman 16798575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 16808575ec29SHugh Dickins lru_add_drain_all(); 16818575ec29SHugh Dickins 168276ab0f53SMel Gorman for_each_online_node(nid) 168376ab0f53SMel Gorman compact_node(nid); 168476ab0f53SMel Gorman } 168576ab0f53SMel Gorman 168676ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 168776ab0f53SMel Gorman int sysctl_compact_memory; 168876ab0f53SMel Gorman 1689fec4eb2cSYaowei Bai /* 1690fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1691fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1692fec4eb2cSYaowei Bai */ 169376ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 169476ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 169576ab0f53SMel Gorman { 169676ab0f53SMel Gorman if (write) 16977964c06dSJason Liu compact_nodes(); 169876ab0f53SMel Gorman 169976ab0f53SMel Gorman return 0; 170076ab0f53SMel Gorman } 1701ed4a6d7fSMel Gorman 17025e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 17035e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 17045e771905SMel Gorman { 17055e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 17065e771905SMel Gorman 17075e771905SMel Gorman return 0; 17085e771905SMel Gorman } 17095e771905SMel Gorman 1710ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 171174e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 171210fbcf4cSKay Sievers struct device_attribute *attr, 1713ed4a6d7fSMel Gorman const char *buf, size_t count) 1714ed4a6d7fSMel Gorman { 17158575ec29SHugh Dickins int nid = dev->id; 17168575ec29SHugh Dickins 17178575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 17188575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17198575ec29SHugh Dickins lru_add_drain_all(); 17208575ec29SHugh Dickins 17218575ec29SHugh Dickins compact_node(nid); 17228575ec29SHugh Dickins } 1723ed4a6d7fSMel Gorman 1724ed4a6d7fSMel Gorman return count; 1725ed4a6d7fSMel Gorman } 172610fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1727ed4a6d7fSMel Gorman 1728ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1729ed4a6d7fSMel Gorman { 173010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1731ed4a6d7fSMel Gorman } 1732ed4a6d7fSMel Gorman 1733ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1734ed4a6d7fSMel Gorman { 173510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1736ed4a6d7fSMel Gorman } 1737ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1738ff9543fdSMichal Nazarewicz 1739698b1b30SVlastimil Babka static inline bool kcompactd_work_requested(pg_data_t *pgdat) 1740698b1b30SVlastimil Babka { 1741172400c6SVlastimil Babka return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); 1742698b1b30SVlastimil Babka } 1743698b1b30SVlastimil Babka 1744698b1b30SVlastimil Babka static bool kcompactd_node_suitable(pg_data_t *pgdat) 1745698b1b30SVlastimil Babka { 1746698b1b30SVlastimil Babka int zoneid; 1747698b1b30SVlastimil Babka struct zone *zone; 1748698b1b30SVlastimil Babka enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; 1749698b1b30SVlastimil Babka 1750698b1b30SVlastimil Babka for (zoneid = 0; zoneid < classzone_idx; zoneid++) { 1751698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1752698b1b30SVlastimil Babka 1753698b1b30SVlastimil Babka if (!populated_zone(zone)) 1754698b1b30SVlastimil Babka continue; 1755698b1b30SVlastimil Babka 1756698b1b30SVlastimil Babka if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, 1757698b1b30SVlastimil Babka classzone_idx) == COMPACT_CONTINUE) 1758698b1b30SVlastimil Babka return true; 1759698b1b30SVlastimil Babka } 1760698b1b30SVlastimil Babka 1761698b1b30SVlastimil Babka return false; 1762698b1b30SVlastimil Babka } 1763698b1b30SVlastimil Babka 1764698b1b30SVlastimil Babka static void kcompactd_do_work(pg_data_t *pgdat) 1765698b1b30SVlastimil Babka { 1766698b1b30SVlastimil Babka /* 1767698b1b30SVlastimil Babka * With no special task, compact all zones so that a page of requested 1768698b1b30SVlastimil Babka * order is allocatable. 1769698b1b30SVlastimil Babka */ 1770698b1b30SVlastimil Babka int zoneid; 1771698b1b30SVlastimil Babka struct zone *zone; 1772698b1b30SVlastimil Babka struct compact_control cc = { 1773698b1b30SVlastimil Babka .order = pgdat->kcompactd_max_order, 1774698b1b30SVlastimil Babka .classzone_idx = pgdat->kcompactd_classzone_idx, 1775698b1b30SVlastimil Babka .mode = MIGRATE_SYNC_LIGHT, 1776698b1b30SVlastimil Babka .ignore_skip_hint = true, 1777698b1b30SVlastimil Babka 1778698b1b30SVlastimil Babka }; 1779698b1b30SVlastimil Babka bool success = false; 1780698b1b30SVlastimil Babka 1781698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, 1782698b1b30SVlastimil Babka cc.classzone_idx); 1783698b1b30SVlastimil Babka count_vm_event(KCOMPACTD_WAKE); 1784698b1b30SVlastimil Babka 1785698b1b30SVlastimil Babka for (zoneid = 0; zoneid < cc.classzone_idx; zoneid++) { 1786698b1b30SVlastimil Babka int status; 1787698b1b30SVlastimil Babka 1788698b1b30SVlastimil Babka zone = &pgdat->node_zones[zoneid]; 1789698b1b30SVlastimil Babka if (!populated_zone(zone)) 1790698b1b30SVlastimil Babka continue; 1791698b1b30SVlastimil Babka 1792698b1b30SVlastimil Babka if (compaction_deferred(zone, cc.order)) 1793698b1b30SVlastimil Babka continue; 1794698b1b30SVlastimil Babka 1795698b1b30SVlastimil Babka if (compaction_suitable(zone, cc.order, 0, zoneid) != 1796698b1b30SVlastimil Babka COMPACT_CONTINUE) 1797698b1b30SVlastimil Babka continue; 1798698b1b30SVlastimil Babka 1799698b1b30SVlastimil Babka cc.nr_freepages = 0; 1800698b1b30SVlastimil Babka cc.nr_migratepages = 0; 1801698b1b30SVlastimil Babka cc.zone = zone; 1802698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.freepages); 1803698b1b30SVlastimil Babka INIT_LIST_HEAD(&cc.migratepages); 1804698b1b30SVlastimil Babka 1805172400c6SVlastimil Babka if (kthread_should_stop()) 1806172400c6SVlastimil Babka return; 1807698b1b30SVlastimil Babka status = compact_zone(zone, &cc); 1808698b1b30SVlastimil Babka 1809698b1b30SVlastimil Babka if (zone_watermark_ok(zone, cc.order, low_wmark_pages(zone), 1810698b1b30SVlastimil Babka cc.classzone_idx, 0)) { 1811698b1b30SVlastimil Babka success = true; 1812698b1b30SVlastimil Babka compaction_defer_reset(zone, cc.order, false); 1813698b1b30SVlastimil Babka } else if (status == COMPACT_COMPLETE) { 1814698b1b30SVlastimil Babka /* 1815698b1b30SVlastimil Babka * We use sync migration mode here, so we defer like 1816698b1b30SVlastimil Babka * sync direct compaction does. 1817698b1b30SVlastimil Babka */ 1818698b1b30SVlastimil Babka defer_compaction(zone, cc.order); 1819698b1b30SVlastimil Babka } 1820698b1b30SVlastimil Babka 1821698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.freepages)); 1822698b1b30SVlastimil Babka VM_BUG_ON(!list_empty(&cc.migratepages)); 1823698b1b30SVlastimil Babka } 1824698b1b30SVlastimil Babka 1825698b1b30SVlastimil Babka /* 1826698b1b30SVlastimil Babka * Regardless of success, we are done until woken up next. But remember 1827698b1b30SVlastimil Babka * the requested order/classzone_idx in case it was higher/tighter than 1828698b1b30SVlastimil Babka * our current ones 1829698b1b30SVlastimil Babka */ 1830698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order <= cc.order) 1831698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1832698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) 1833698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1834698b1b30SVlastimil Babka } 1835698b1b30SVlastimil Babka 1836698b1b30SVlastimil Babka void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) 1837698b1b30SVlastimil Babka { 1838698b1b30SVlastimil Babka if (!order) 1839698b1b30SVlastimil Babka return; 1840698b1b30SVlastimil Babka 1841698b1b30SVlastimil Babka if (pgdat->kcompactd_max_order < order) 1842698b1b30SVlastimil Babka pgdat->kcompactd_max_order = order; 1843698b1b30SVlastimil Babka 1844698b1b30SVlastimil Babka if (pgdat->kcompactd_classzone_idx > classzone_idx) 1845698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = classzone_idx; 1846698b1b30SVlastimil Babka 1847698b1b30SVlastimil Babka if (!waitqueue_active(&pgdat->kcompactd_wait)) 1848698b1b30SVlastimil Babka return; 1849698b1b30SVlastimil Babka 1850698b1b30SVlastimil Babka if (!kcompactd_node_suitable(pgdat)) 1851698b1b30SVlastimil Babka return; 1852698b1b30SVlastimil Babka 1853698b1b30SVlastimil Babka trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, 1854698b1b30SVlastimil Babka classzone_idx); 1855698b1b30SVlastimil Babka wake_up_interruptible(&pgdat->kcompactd_wait); 1856698b1b30SVlastimil Babka } 1857698b1b30SVlastimil Babka 1858698b1b30SVlastimil Babka /* 1859698b1b30SVlastimil Babka * The background compaction daemon, started as a kernel thread 1860698b1b30SVlastimil Babka * from the init process. 1861698b1b30SVlastimil Babka */ 1862698b1b30SVlastimil Babka static int kcompactd(void *p) 1863698b1b30SVlastimil Babka { 1864698b1b30SVlastimil Babka pg_data_t *pgdat = (pg_data_t*)p; 1865698b1b30SVlastimil Babka struct task_struct *tsk = current; 1866698b1b30SVlastimil Babka 1867698b1b30SVlastimil Babka const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 1868698b1b30SVlastimil Babka 1869698b1b30SVlastimil Babka if (!cpumask_empty(cpumask)) 1870698b1b30SVlastimil Babka set_cpus_allowed_ptr(tsk, cpumask); 1871698b1b30SVlastimil Babka 1872698b1b30SVlastimil Babka set_freezable(); 1873698b1b30SVlastimil Babka 1874698b1b30SVlastimil Babka pgdat->kcompactd_max_order = 0; 1875698b1b30SVlastimil Babka pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; 1876698b1b30SVlastimil Babka 1877698b1b30SVlastimil Babka while (!kthread_should_stop()) { 1878698b1b30SVlastimil Babka trace_mm_compaction_kcompactd_sleep(pgdat->node_id); 1879698b1b30SVlastimil Babka wait_event_freezable(pgdat->kcompactd_wait, 1880698b1b30SVlastimil Babka kcompactd_work_requested(pgdat)); 1881698b1b30SVlastimil Babka 1882698b1b30SVlastimil Babka kcompactd_do_work(pgdat); 1883698b1b30SVlastimil Babka } 1884698b1b30SVlastimil Babka 1885698b1b30SVlastimil Babka return 0; 1886698b1b30SVlastimil Babka } 1887698b1b30SVlastimil Babka 1888698b1b30SVlastimil Babka /* 1889698b1b30SVlastimil Babka * This kcompactd start function will be called by init and node-hot-add. 1890698b1b30SVlastimil Babka * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. 1891698b1b30SVlastimil Babka */ 1892698b1b30SVlastimil Babka int kcompactd_run(int nid) 1893698b1b30SVlastimil Babka { 1894698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1895698b1b30SVlastimil Babka int ret = 0; 1896698b1b30SVlastimil Babka 1897698b1b30SVlastimil Babka if (pgdat->kcompactd) 1898698b1b30SVlastimil Babka return 0; 1899698b1b30SVlastimil Babka 1900698b1b30SVlastimil Babka pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid); 1901698b1b30SVlastimil Babka if (IS_ERR(pgdat->kcompactd)) { 1902698b1b30SVlastimil Babka pr_err("Failed to start kcompactd on node %d\n", nid); 1903698b1b30SVlastimil Babka ret = PTR_ERR(pgdat->kcompactd); 1904698b1b30SVlastimil Babka pgdat->kcompactd = NULL; 1905698b1b30SVlastimil Babka } 1906698b1b30SVlastimil Babka return ret; 1907698b1b30SVlastimil Babka } 1908698b1b30SVlastimil Babka 1909698b1b30SVlastimil Babka /* 1910698b1b30SVlastimil Babka * Called by memory hotplug when all memory in a node is offlined. Caller must 1911698b1b30SVlastimil Babka * hold mem_hotplug_begin/end(). 1912698b1b30SVlastimil Babka */ 1913698b1b30SVlastimil Babka void kcompactd_stop(int nid) 1914698b1b30SVlastimil Babka { 1915698b1b30SVlastimil Babka struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; 1916698b1b30SVlastimil Babka 1917698b1b30SVlastimil Babka if (kcompactd) { 1918698b1b30SVlastimil Babka kthread_stop(kcompactd); 1919698b1b30SVlastimil Babka NODE_DATA(nid)->kcompactd = NULL; 1920698b1b30SVlastimil Babka } 1921698b1b30SVlastimil Babka } 1922698b1b30SVlastimil Babka 1923698b1b30SVlastimil Babka /* 1924698b1b30SVlastimil Babka * It's optimal to keep kcompactd on the same CPUs as their memory, but 1925698b1b30SVlastimil Babka * not required for correctness. So if the last cpu in a node goes 1926698b1b30SVlastimil Babka * away, we get changed to run anywhere: as the first one comes back, 1927698b1b30SVlastimil Babka * restore their cpu bindings. 1928698b1b30SVlastimil Babka */ 1929698b1b30SVlastimil Babka static int cpu_callback(struct notifier_block *nfb, unsigned long action, 1930698b1b30SVlastimil Babka void *hcpu) 1931698b1b30SVlastimil Babka { 1932698b1b30SVlastimil Babka int nid; 1933698b1b30SVlastimil Babka 1934698b1b30SVlastimil Babka if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 1935698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) { 1936698b1b30SVlastimil Babka pg_data_t *pgdat = NODE_DATA(nid); 1937698b1b30SVlastimil Babka const struct cpumask *mask; 1938698b1b30SVlastimil Babka 1939698b1b30SVlastimil Babka mask = cpumask_of_node(pgdat->node_id); 1940698b1b30SVlastimil Babka 1941698b1b30SVlastimil Babka if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 1942698b1b30SVlastimil Babka /* One of our CPUs online: restore mask */ 1943698b1b30SVlastimil Babka set_cpus_allowed_ptr(pgdat->kcompactd, mask); 1944698b1b30SVlastimil Babka } 1945698b1b30SVlastimil Babka } 1946698b1b30SVlastimil Babka return NOTIFY_OK; 1947698b1b30SVlastimil Babka } 1948698b1b30SVlastimil Babka 1949698b1b30SVlastimil Babka static int __init kcompactd_init(void) 1950698b1b30SVlastimil Babka { 1951698b1b30SVlastimil Babka int nid; 1952698b1b30SVlastimil Babka 1953698b1b30SVlastimil Babka for_each_node_state(nid, N_MEMORY) 1954698b1b30SVlastimil Babka kcompactd_run(nid); 1955698b1b30SVlastimil Babka hotcpu_notifier(cpu_callback, 0); 1956698b1b30SVlastimil Babka return 0; 1957698b1b30SVlastimil Babka } 1958698b1b30SVlastimil Babka subsys_initcall(kcompactd_init) 1959698b1b30SVlastimil Babka 1960ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1961