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 */ 10748446bbSMel Gorman #include <linux/swap.h> 11748446bbSMel Gorman #include <linux/migrate.h> 12748446bbSMel Gorman #include <linux/compaction.h> 13748446bbSMel Gorman #include <linux/mm_inline.h> 14748446bbSMel Gorman #include <linux/backing-dev.h> 1576ab0f53SMel Gorman #include <linux/sysctl.h> 16ed4a6d7fSMel Gorman #include <linux/sysfs.h> 17bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h> 18194159fbSMinchan Kim #include <linux/page-isolation.h> 19748446bbSMel Gorman #include "internal.h" 20748446bbSMel Gorman 21010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 22010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 23010fc29aSMinchan Kim { 24010fc29aSMinchan Kim count_vm_event(item); 25010fc29aSMinchan Kim } 26010fc29aSMinchan Kim 27010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 28010fc29aSMinchan Kim { 29010fc29aSMinchan Kim count_vm_events(item, delta); 30010fc29aSMinchan Kim } 31010fc29aSMinchan Kim #else 32010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 33010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 34010fc29aSMinchan Kim #endif 35010fc29aSMinchan Kim 36ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 37ff9543fdSMichal Nazarewicz 38b7aba698SMel Gorman #define CREATE_TRACE_POINTS 39b7aba698SMel Gorman #include <trace/events/compaction.h> 40b7aba698SMel Gorman 41748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 42748446bbSMel Gorman { 43748446bbSMel Gorman struct page *page, *next; 44748446bbSMel Gorman unsigned long count = 0; 45748446bbSMel Gorman 46748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 47748446bbSMel Gorman list_del(&page->lru); 48748446bbSMel Gorman __free_page(page); 49748446bbSMel Gorman count++; 50748446bbSMel Gorman } 51748446bbSMel Gorman 52748446bbSMel Gorman return count; 53748446bbSMel Gorman } 54748446bbSMel Gorman 55ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 56ff9543fdSMichal Nazarewicz { 57ff9543fdSMichal Nazarewicz struct page *page; 58ff9543fdSMichal Nazarewicz 59ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 60ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 61ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 62ff9543fdSMichal Nazarewicz } 63ff9543fdSMichal Nazarewicz } 64ff9543fdSMichal Nazarewicz 6547118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 6647118af0SMichal Nazarewicz { 6747118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 6847118af0SMichal Nazarewicz } 6947118af0SMichal Nazarewicz 707d49d886SVlastimil Babka /* 717d49d886SVlastimil Babka * Check that the whole (or subset of) a pageblock given by the interval of 727d49d886SVlastimil Babka * [start_pfn, end_pfn) is valid and within the same zone, before scanning it 737d49d886SVlastimil Babka * with the migration of free compaction scanner. The scanners then need to 747d49d886SVlastimil Babka * use only pfn_valid_within() check for arches that allow holes within 757d49d886SVlastimil Babka * pageblocks. 767d49d886SVlastimil Babka * 777d49d886SVlastimil Babka * Return struct page pointer of start_pfn, or NULL if checks were not passed. 787d49d886SVlastimil Babka * 797d49d886SVlastimil Babka * It's possible on some configurations to have a setup like node0 node1 node0 807d49d886SVlastimil Babka * i.e. it's possible that all pages within a zones range of pages do not 817d49d886SVlastimil Babka * belong to a single zone. We assume that a border between node0 and node1 827d49d886SVlastimil Babka * can occur within a single pageblock, but not a node0 node1 node0 837d49d886SVlastimil Babka * interleaving within a single pageblock. It is therefore sufficient to check 847d49d886SVlastimil Babka * the first and last page of a pageblock and avoid checking each individual 857d49d886SVlastimil Babka * page in a pageblock. 867d49d886SVlastimil Babka */ 877d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn, 887d49d886SVlastimil Babka unsigned long end_pfn, struct zone *zone) 897d49d886SVlastimil Babka { 907d49d886SVlastimil Babka struct page *start_page; 917d49d886SVlastimil Babka struct page *end_page; 927d49d886SVlastimil Babka 937d49d886SVlastimil Babka /* end_pfn is one past the range we are checking */ 947d49d886SVlastimil Babka end_pfn--; 957d49d886SVlastimil Babka 967d49d886SVlastimil Babka if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) 977d49d886SVlastimil Babka return NULL; 987d49d886SVlastimil Babka 997d49d886SVlastimil Babka start_page = pfn_to_page(start_pfn); 1007d49d886SVlastimil Babka 1017d49d886SVlastimil Babka if (page_zone(start_page) != zone) 1027d49d886SVlastimil Babka return NULL; 1037d49d886SVlastimil Babka 1047d49d886SVlastimil Babka end_page = pfn_to_page(end_pfn); 1057d49d886SVlastimil Babka 1067d49d886SVlastimil Babka /* This gives a shorter code than deriving page_zone(end_page) */ 1077d49d886SVlastimil Babka if (page_zone_id(start_page) != page_zone_id(end_page)) 1087d49d886SVlastimil Babka return NULL; 1097d49d886SVlastimil Babka 1107d49d886SVlastimil Babka return start_page; 1117d49d886SVlastimil Babka } 1127d49d886SVlastimil Babka 113bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 114bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 115bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 116bb13ffebSMel Gorman struct page *page) 117bb13ffebSMel Gorman { 118bb13ffebSMel Gorman if (cc->ignore_skip_hint) 119bb13ffebSMel Gorman return true; 120bb13ffebSMel Gorman 121bb13ffebSMel Gorman return !get_pageblock_skip(page); 122bb13ffebSMel Gorman } 123bb13ffebSMel Gorman 124bb13ffebSMel Gorman /* 125bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 126bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 127bb13ffebSMel Gorman * meet. 128bb13ffebSMel Gorman */ 12962997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 130bb13ffebSMel Gorman { 131bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 132108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 133bb13ffebSMel Gorman unsigned long pfn; 134bb13ffebSMel Gorman 13535979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = start_pfn; 13635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = start_pfn; 137c89511abSMel Gorman zone->compact_cached_free_pfn = end_pfn; 13862997027SMel Gorman zone->compact_blockskip_flush = false; 139bb13ffebSMel Gorman 140bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 141bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 142bb13ffebSMel Gorman struct page *page; 143bb13ffebSMel Gorman 144bb13ffebSMel Gorman cond_resched(); 145bb13ffebSMel Gorman 146bb13ffebSMel Gorman if (!pfn_valid(pfn)) 147bb13ffebSMel Gorman continue; 148bb13ffebSMel Gorman 149bb13ffebSMel Gorman page = pfn_to_page(pfn); 150bb13ffebSMel Gorman if (zone != page_zone(page)) 151bb13ffebSMel Gorman continue; 152bb13ffebSMel Gorman 153bb13ffebSMel Gorman clear_pageblock_skip(page); 154bb13ffebSMel Gorman } 155bb13ffebSMel Gorman } 156bb13ffebSMel Gorman 15762997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 15862997027SMel Gorman { 15962997027SMel Gorman int zoneid; 16062997027SMel Gorman 16162997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 16262997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 16362997027SMel Gorman if (!populated_zone(zone)) 16462997027SMel Gorman continue; 16562997027SMel Gorman 16662997027SMel Gorman /* Only flush if a full compaction finished recently */ 16762997027SMel Gorman if (zone->compact_blockskip_flush) 16862997027SMel Gorman __reset_isolation_suitable(zone); 16962997027SMel Gorman } 17062997027SMel Gorman } 17162997027SMel Gorman 172bb13ffebSMel Gorman /* 173bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 17462997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 175bb13ffebSMel Gorman */ 176c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 177c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 178edc2ca61SVlastimil Babka bool migrate_scanner) 179bb13ffebSMel Gorman { 180c89511abSMel Gorman struct zone *zone = cc->zone; 18135979ef3SDavid Rientjes unsigned long pfn; 1826815bf3fSJoonsoo Kim 1836815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 1846815bf3fSJoonsoo Kim return; 1856815bf3fSJoonsoo Kim 186bb13ffebSMel Gorman if (!page) 187bb13ffebSMel Gorman return; 188bb13ffebSMel Gorman 18935979ef3SDavid Rientjes if (nr_isolated) 19035979ef3SDavid Rientjes return; 19135979ef3SDavid Rientjes 192bb13ffebSMel Gorman set_pageblock_skip(page); 193c89511abSMel Gorman 19435979ef3SDavid Rientjes pfn = page_to_pfn(page); 19535979ef3SDavid Rientjes 19635979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 197c89511abSMel Gorman if (migrate_scanner) { 19835979ef3SDavid Rientjes if (cc->finished_update_migrate) 19935979ef3SDavid Rientjes return; 20035979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 20135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 202e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 203e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 20435979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 205c89511abSMel Gorman } else { 20635979ef3SDavid Rientjes if (cc->finished_update_free) 20735979ef3SDavid Rientjes return; 20835979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 209c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 210c89511abSMel Gorman } 211c89511abSMel Gorman } 212bb13ffebSMel Gorman #else 213bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 214bb13ffebSMel Gorman struct page *page) 215bb13ffebSMel Gorman { 216bb13ffebSMel Gorman return true; 217bb13ffebSMel Gorman } 218bb13ffebSMel Gorman 219c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 220c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 221edc2ca61SVlastimil Babka bool migrate_scanner) 222bb13ffebSMel Gorman { 223bb13ffebSMel Gorman } 224bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 225bb13ffebSMel Gorman 2261f9efdefSVlastimil Babka /* 2278b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 2288b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 2298b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 2308b44d279SVlastimil Babka * 2318b44d279SVlastimil Babka * Returns true if the lock is held 2328b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 2331f9efdefSVlastimil Babka */ 2348b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 2358b44d279SVlastimil Babka struct compact_control *cc) 2368b44d279SVlastimil Babka { 2378b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 2388b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 2398b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 2408b44d279SVlastimil Babka return false; 2418b44d279SVlastimil Babka } 2428b44d279SVlastimil Babka } else { 2438b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 2448b44d279SVlastimil Babka } 2451f9efdefSVlastimil Babka 2468b44d279SVlastimil Babka return true; 2472a1402aaSMel Gorman } 2482a1402aaSMel Gorman 24985aa125fSMichal Nazarewicz /* 250c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 2518b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 2528b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 2538b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 2548b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 2558b44d279SVlastimil Babka * aborts. Sync compaction schedules. 2568b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 2578b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 258c67fe375SMel Gorman * 2598b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 2608b44d279SVlastimil Babka * async compaction due to need_resched() 2618b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 2628b44d279SVlastimil Babka * scheduled) 263c67fe375SMel Gorman */ 2648b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 2658b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 266c67fe375SMel Gorman { 2678b44d279SVlastimil Babka if (*locked) { 2688b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 2698b44d279SVlastimil Babka *locked = false; 270c67fe375SMel Gorman } 271c67fe375SMel Gorman 2728b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 2738b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 2748b44d279SVlastimil Babka return true; 2758b44d279SVlastimil Babka } 2768b44d279SVlastimil Babka 2778b44d279SVlastimil Babka if (need_resched()) { 278e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 2798b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 2808b44d279SVlastimil Babka return true; 281c67fe375SMel Gorman } 282c67fe375SMel Gorman cond_resched(); 283c67fe375SMel Gorman } 284c67fe375SMel Gorman 2858b44d279SVlastimil Babka return false; 286c67fe375SMel Gorman } 287c67fe375SMel Gorman 288be976572SVlastimil Babka /* 289be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 290be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 2918b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 292be976572SVlastimil Babka * is used where no lock is concerned. 293be976572SVlastimil Babka * 294be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 295be976572SVlastimil Babka * Returns true when async compaction should abort. 296be976572SVlastimil Babka */ 297be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 298be976572SVlastimil Babka { 299be976572SVlastimil Babka /* async compaction aborts if contended */ 300be976572SVlastimil Babka if (need_resched()) { 301be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3021f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 303be976572SVlastimil Babka return true; 304be976572SVlastimil Babka } 305be976572SVlastimil Babka 306be976572SVlastimil Babka cond_resched(); 307be976572SVlastimil Babka } 308be976572SVlastimil Babka 309be976572SVlastimil Babka return false; 310be976572SVlastimil Babka } 311be976572SVlastimil Babka 312f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */ 313f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page) 314f40d1e42SMel Gorman { 3157d348b9eSJoonsoo Kim /* If the page is a large free page, then disallow migration */ 316f40d1e42SMel Gorman if (PageBuddy(page) && page_order(page) >= pageblock_order) 3177d348b9eSJoonsoo Kim return false; 318f40d1e42SMel Gorman 319f40d1e42SMel Gorman /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 3207d348b9eSJoonsoo Kim if (migrate_async_suitable(get_pageblock_migratetype(page))) 321f40d1e42SMel Gorman return true; 322f40d1e42SMel Gorman 323f40d1e42SMel Gorman /* Otherwise skip the block */ 324f40d1e42SMel Gorman return false; 325f40d1e42SMel Gorman } 326f40d1e42SMel Gorman 327c67fe375SMel Gorman /* 3289e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3299e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3309e4be470SJerome Marchand * (even though it may still end up isolating some pages). 33185aa125fSMichal Nazarewicz */ 332f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 333f40d1e42SMel Gorman unsigned long blockpfn, 33485aa125fSMichal Nazarewicz unsigned long end_pfn, 33585aa125fSMichal Nazarewicz struct list_head *freelist, 33685aa125fSMichal Nazarewicz bool strict) 337748446bbSMel Gorman { 338b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 339bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 340f40d1e42SMel Gorman unsigned long flags; 341f40d1e42SMel Gorman bool locked = false; 342748446bbSMel Gorman 343748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 344748446bbSMel Gorman 345f40d1e42SMel Gorman /* Isolate free pages. */ 346748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 347748446bbSMel Gorman int isolated, i; 348748446bbSMel Gorman struct page *page = cursor; 349748446bbSMel Gorman 3508b44d279SVlastimil Babka /* 3518b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 3528b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 3538b44d279SVlastimil Babka * pending or async compaction detects need_resched() 3548b44d279SVlastimil Babka */ 3558b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 3568b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 3578b44d279SVlastimil Babka &locked, cc)) 3588b44d279SVlastimil Babka break; 3598b44d279SVlastimil Babka 360b7aba698SMel Gorman nr_scanned++; 361f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 3622af120bcSLaura Abbott goto isolate_fail; 3632af120bcSLaura Abbott 364bb13ffebSMel Gorman if (!valid_page) 365bb13ffebSMel Gorman valid_page = page; 366f40d1e42SMel Gorman if (!PageBuddy(page)) 3672af120bcSLaura Abbott goto isolate_fail; 368f40d1e42SMel Gorman 369f40d1e42SMel Gorman /* 370*69b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 371*69b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 372*69b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 373*69b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 374*69b7189fSVlastimil Babka * recheck as well. 375*69b7189fSVlastimil Babka */ 376*69b7189fSVlastimil Babka if (!locked) { 377*69b7189fSVlastimil Babka /* 378f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 379f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 380f40d1e42SMel Gorman * heavily contended if there are parallel allocations 381f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 382f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 383f40d1e42SMel Gorman * possible. 384f40d1e42SMel Gorman */ 3858b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 3868b44d279SVlastimil Babka &flags, cc); 387f40d1e42SMel Gorman if (!locked) 388f40d1e42SMel Gorman break; 389f40d1e42SMel Gorman 390f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 391f40d1e42SMel Gorman if (!PageBuddy(page)) 3922af120bcSLaura Abbott goto isolate_fail; 393*69b7189fSVlastimil Babka } 394748446bbSMel Gorman 395748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 396748446bbSMel Gorman isolated = split_free_page(page); 397748446bbSMel Gorman total_isolated += isolated; 398748446bbSMel Gorman for (i = 0; i < isolated; i++) { 399748446bbSMel Gorman list_add(&page->lru, freelist); 400748446bbSMel Gorman page++; 401748446bbSMel Gorman } 402748446bbSMel Gorman 403748446bbSMel Gorman /* If a page was split, advance to the end of it */ 404748446bbSMel Gorman if (isolated) { 405748446bbSMel Gorman blockpfn += isolated - 1; 406748446bbSMel Gorman cursor += isolated - 1; 4072af120bcSLaura Abbott continue; 408748446bbSMel Gorman } 4092af120bcSLaura Abbott 4102af120bcSLaura Abbott isolate_fail: 4112af120bcSLaura Abbott if (strict) 4122af120bcSLaura Abbott break; 4132af120bcSLaura Abbott else 4142af120bcSLaura Abbott continue; 4152af120bcSLaura Abbott 416748446bbSMel Gorman } 417748446bbSMel Gorman 418b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 419f40d1e42SMel Gorman 420f40d1e42SMel Gorman /* 421f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 422f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 423f40d1e42SMel Gorman * returned and CMA will fail. 424f40d1e42SMel Gorman */ 4252af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 426f40d1e42SMel Gorman total_isolated = 0; 427f40d1e42SMel Gorman 428f40d1e42SMel Gorman if (locked) 429f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 430f40d1e42SMel Gorman 431bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 432bb13ffebSMel Gorman if (blockpfn == end_pfn) 433edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 434bb13ffebSMel Gorman 435010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 436397487dbSMel Gorman if (total_isolated) 437010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 438748446bbSMel Gorman return total_isolated; 439748446bbSMel Gorman } 440748446bbSMel Gorman 44185aa125fSMichal Nazarewicz /** 44285aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 44385aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 44485aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 44585aa125fSMichal Nazarewicz * 44685aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 44785aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 44885aa125fSMichal Nazarewicz * undo its actions and return zero. 44985aa125fSMichal Nazarewicz * 45085aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 45185aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 45285aa125fSMichal Nazarewicz * a free page). 45385aa125fSMichal Nazarewicz */ 454ff9543fdSMichal Nazarewicz unsigned long 455bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 456bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 45785aa125fSMichal Nazarewicz { 458f40d1e42SMel Gorman unsigned long isolated, pfn, block_end_pfn; 45985aa125fSMichal Nazarewicz LIST_HEAD(freelist); 46085aa125fSMichal Nazarewicz 4617d49d886SVlastimil Babka pfn = start_pfn; 46285aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 4637d49d886SVlastimil Babka 4647d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 4657d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 4667d49d886SVlastimil Babka 46785aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 46885aa125fSMichal Nazarewicz 4697d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 4707d49d886SVlastimil Babka break; 4717d49d886SVlastimil Babka 472bb13ffebSMel Gorman isolated = isolate_freepages_block(cc, pfn, block_end_pfn, 47385aa125fSMichal Nazarewicz &freelist, true); 47485aa125fSMichal Nazarewicz 47585aa125fSMichal Nazarewicz /* 47685aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 47785aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 47885aa125fSMichal Nazarewicz * non-free pages). 47985aa125fSMichal Nazarewicz */ 48085aa125fSMichal Nazarewicz if (!isolated) 48185aa125fSMichal Nazarewicz break; 48285aa125fSMichal Nazarewicz 48385aa125fSMichal Nazarewicz /* 48485aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 48585aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 48685aa125fSMichal Nazarewicz * page may span two pageblocks). 48785aa125fSMichal Nazarewicz */ 48885aa125fSMichal Nazarewicz } 48985aa125fSMichal Nazarewicz 49085aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 49185aa125fSMichal Nazarewicz map_pages(&freelist); 49285aa125fSMichal Nazarewicz 49385aa125fSMichal Nazarewicz if (pfn < end_pfn) { 49485aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 49585aa125fSMichal Nazarewicz release_freepages(&freelist); 49685aa125fSMichal Nazarewicz return 0; 49785aa125fSMichal Nazarewicz } 49885aa125fSMichal Nazarewicz 49985aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 50085aa125fSMichal Nazarewicz return pfn; 50185aa125fSMichal Nazarewicz } 50285aa125fSMichal Nazarewicz 503748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 504edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 505748446bbSMel Gorman { 506748446bbSMel Gorman struct page *page; 507b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 508748446bbSMel Gorman 509edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 510edc2ca61SVlastimil Babka return; 511edc2ca61SVlastimil Babka 512b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 513b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 514748446bbSMel Gorman 515c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 516c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 517c67fe375SMel Gorman } 518748446bbSMel Gorman 519748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 520748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 521748446bbSMel Gorman { 522bc693045SMinchan Kim unsigned long active, inactive, isolated; 523748446bbSMel Gorman 524748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 525748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 526bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 527bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 528748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 529748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 530748446bbSMel Gorman 531bc693045SMinchan Kim return isolated > (inactive + active) / 2; 532748446bbSMel Gorman } 533748446bbSMel Gorman 5342fe86e00SMichal Nazarewicz /** 535edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 536edc2ca61SVlastimil Babka * a single pageblock 5372fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 538edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 539edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 540edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 5412fe86e00SMichal Nazarewicz * 5422fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 543edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 544edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 545edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 546edc2ca61SVlastimil Babka * than end_pfn). 5472fe86e00SMichal Nazarewicz * 548edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 549edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 550edc2ca61SVlastimil Babka * is neither read nor updated. 551748446bbSMel Gorman */ 552edc2ca61SVlastimil Babka static unsigned long 553edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 554edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 555748446bbSMel Gorman { 556edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 557b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 558748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 559fa9add64SHugh Dickins struct lruvec *lruvec; 560c67fe375SMel Gorman unsigned long flags; 5612a1402aaSMel Gorman bool locked = false; 562bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 563748446bbSMel Gorman 564748446bbSMel Gorman /* 565748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 566748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 567748446bbSMel Gorman * delay for some time until fewer pages are isolated 568748446bbSMel Gorman */ 569748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 570f9e35b3bSMel Gorman /* async migration should just abort */ 571e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 5722fe86e00SMichal Nazarewicz return 0; 573f9e35b3bSMel Gorman 574748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 575748446bbSMel Gorman 576748446bbSMel Gorman if (fatal_signal_pending(current)) 5772fe86e00SMichal Nazarewicz return 0; 578748446bbSMel Gorman } 579748446bbSMel Gorman 580be976572SVlastimil Babka if (compact_should_abort(cc)) 581aeef4b83SDavid Rientjes return 0; 582aeef4b83SDavid Rientjes 583748446bbSMel Gorman /* Time to isolate some pages for migration */ 584748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 5858b44d279SVlastimil Babka /* 5868b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 5878b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 5888b44d279SVlastimil Babka * if contended. 5898b44d279SVlastimil Babka */ 5908b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 5918b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 5928b44d279SVlastimil Babka &locked, cc)) 5938b44d279SVlastimil Babka break; 594b2eef8c0SAndrea Arcangeli 595748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 596748446bbSMel Gorman continue; 597b7aba698SMel Gorman nr_scanned++; 598748446bbSMel Gorman 599748446bbSMel Gorman page = pfn_to_page(low_pfn); 600dc908600SMel Gorman 601bb13ffebSMel Gorman if (!valid_page) 602bb13ffebSMel Gorman valid_page = page; 603bb13ffebSMel Gorman 604c122b208SJoonsoo Kim /* 6056c14466cSMel Gorman * Skip if free. page_order cannot be used without zone->lock 6066c14466cSMel Gorman * as nothing prevents parallel allocations or buddy merging. 6076c14466cSMel Gorman */ 608748446bbSMel Gorman if (PageBuddy(page)) 609748446bbSMel Gorman continue; 610748446bbSMel Gorman 6119927af74SMel Gorman /* 612bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 613bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 614bf6bddf1SRafael Aquini * Skip any other type of page 615bf6bddf1SRafael Aquini */ 616bf6bddf1SRafael Aquini if (!PageLRU(page)) { 617bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 618bf6bddf1SRafael Aquini if (locked && balloon_page_isolate(page)) { 619bf6bddf1SRafael Aquini /* Successfully isolated */ 620b6c75016SJoonsoo Kim goto isolate_success; 621bf6bddf1SRafael Aquini } 622bf6bddf1SRafael Aquini } 623bc835011SAndrea Arcangeli continue; 624bf6bddf1SRafael Aquini } 625bc835011SAndrea Arcangeli 626bc835011SAndrea Arcangeli /* 6272a1402aaSMel Gorman * PageLRU is set. lru_lock normally excludes isolation 6282a1402aaSMel Gorman * splitting and collapsing (collapsing has already happened 6292a1402aaSMel Gorman * if PageLRU is set) but the lock is not necessarily taken 6302a1402aaSMel Gorman * here and it is wasteful to take it just to check transhuge. 6312a1402aaSMel Gorman * Check TransHuge without lock and skip the whole pageblock if 6322a1402aaSMel Gorman * it's either a transhuge or hugetlbfs page, as calling 6332a1402aaSMel Gorman * compound_order() without preventing THP from splitting the 6342a1402aaSMel Gorman * page underneath us may return surprising results. 635bc835011SAndrea Arcangeli */ 636bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 6372a1402aaSMel Gorman if (!locked) 638edc2ca61SVlastimil Babka low_pfn = ALIGN(low_pfn + 1, 639edc2ca61SVlastimil Babka pageblock_nr_pages) - 1; 640edc2ca61SVlastimil Babka else 6412a1402aaSMel Gorman low_pfn += (1 << compound_order(page)) - 1; 642edc2ca61SVlastimil Babka 6432a1402aaSMel Gorman continue; 6442a1402aaSMel Gorman } 6452a1402aaSMel Gorman 646119d6d59SDavid Rientjes /* 647119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 648119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 649119d6d59SDavid Rientjes * admittedly racy check. 650119d6d59SDavid Rientjes */ 651119d6d59SDavid Rientjes if (!page_mapping(page) && 652119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 653119d6d59SDavid Rientjes continue; 654119d6d59SDavid Rientjes 655*69b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 656*69b7189fSVlastimil Babka if (!locked) { 6578b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 6588b44d279SVlastimil Babka &flags, cc); 6598b44d279SVlastimil Babka if (!locked) 6602a1402aaSMel Gorman break; 6612a1402aaSMel Gorman 6622a1402aaSMel Gorman /* Recheck PageLRU and PageTransHuge under lock */ 6632a1402aaSMel Gorman if (!PageLRU(page)) 6642a1402aaSMel Gorman continue; 6652a1402aaSMel Gorman if (PageTransHuge(page)) { 666bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 667bc835011SAndrea Arcangeli continue; 668bc835011SAndrea Arcangeli } 669*69b7189fSVlastimil Babka } 670bc835011SAndrea Arcangeli 671fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 672fa9add64SHugh Dickins 673748446bbSMel Gorman /* Try isolate the page */ 674edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 675748446bbSMel Gorman continue; 676748446bbSMel Gorman 677309381feSSasha Levin VM_BUG_ON_PAGE(PageTransCompound(page), page); 678bc835011SAndrea Arcangeli 679748446bbSMel Gorman /* Successfully isolated */ 680fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 681b6c75016SJoonsoo Kim 682b6c75016SJoonsoo Kim isolate_success: 683b6c75016SJoonsoo Kim cc->finished_update_migrate = true; 684748446bbSMel Gorman list_add(&page->lru, migratelist); 685748446bbSMel Gorman cc->nr_migratepages++; 686b7aba698SMel Gorman nr_isolated++; 687748446bbSMel Gorman 688748446bbSMel Gorman /* Avoid isolating too much */ 68931b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 69031b8384aSHillf Danton ++low_pfn; 691748446bbSMel Gorman break; 692748446bbSMel Gorman } 69331b8384aSHillf Danton } 694748446bbSMel Gorman 695c67fe375SMel Gorman if (locked) 696c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 697748446bbSMel Gorman 69850b5b094SVlastimil Babka /* 69950b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 70050b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 70150b5b094SVlastimil Babka */ 70235979ef3SDavid Rientjes if (low_pfn == end_pfn) 703edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 704bb13ffebSMel Gorman 705b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 706b7aba698SMel Gorman 707010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 708397487dbSMel Gorman if (nr_isolated) 709010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 710397487dbSMel Gorman 7112fe86e00SMichal Nazarewicz return low_pfn; 7122fe86e00SMichal Nazarewicz } 7132fe86e00SMichal Nazarewicz 714edc2ca61SVlastimil Babka /** 715edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 716edc2ca61SVlastimil Babka * @cc: Compaction control structure. 717edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 718edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 719edc2ca61SVlastimil Babka * 720edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 721edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 722edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 723edc2ca61SVlastimil Babka */ 724edc2ca61SVlastimil Babka unsigned long 725edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 726edc2ca61SVlastimil Babka unsigned long end_pfn) 727edc2ca61SVlastimil Babka { 728edc2ca61SVlastimil Babka unsigned long pfn, block_end_pfn; 729edc2ca61SVlastimil Babka 730edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 731edc2ca61SVlastimil Babka pfn = start_pfn; 732edc2ca61SVlastimil Babka block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 733edc2ca61SVlastimil Babka 734edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 735edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 736edc2ca61SVlastimil Babka 737edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 738edc2ca61SVlastimil Babka 7397d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 740edc2ca61SVlastimil Babka continue; 741edc2ca61SVlastimil Babka 742edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 743edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 744edc2ca61SVlastimil Babka 745edc2ca61SVlastimil Babka /* 746edc2ca61SVlastimil Babka * In case of fatal failure, release everything that might 747edc2ca61SVlastimil Babka * have been isolated in the previous iteration, and signal 748edc2ca61SVlastimil Babka * the failure back to caller. 749edc2ca61SVlastimil Babka */ 750edc2ca61SVlastimil Babka if (!pfn) { 751edc2ca61SVlastimil Babka putback_movable_pages(&cc->migratepages); 752edc2ca61SVlastimil Babka cc->nr_migratepages = 0; 753edc2ca61SVlastimil Babka break; 754edc2ca61SVlastimil Babka } 755edc2ca61SVlastimil Babka } 756edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 757edc2ca61SVlastimil Babka 758edc2ca61SVlastimil Babka return pfn; 759edc2ca61SVlastimil Babka } 760edc2ca61SVlastimil Babka 761ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 762ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 763ff9543fdSMichal Nazarewicz /* 764ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 765ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 766ff9543fdSMichal Nazarewicz */ 767edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 768ff9543fdSMichal Nazarewicz { 769edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 770ff9543fdSMichal Nazarewicz struct page *page; 771c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 772c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 773c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 774ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 775ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 7762fe86e00SMichal Nazarewicz 777ff9543fdSMichal Nazarewicz /* 778ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 77949e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 78049e068f0SVlastimil Babka * zone when isolating for the first time. We need this aligned to 781c96b9e50SVlastimil Babka * the pageblock boundary, because we do 782c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 783c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 784c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 78549e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 78649e068f0SVlastimil Babka * is using. 787ff9543fdSMichal Nazarewicz */ 788c96b9e50SVlastimil Babka block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); 789c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 790c96b9e50SVlastimil Babka zone_end_pfn(zone)); 7917ed695e0SVlastimil Babka low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); 7922fe86e00SMichal Nazarewicz 793ff9543fdSMichal Nazarewicz /* 794ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 795ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 796ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 797ff9543fdSMichal Nazarewicz */ 798c96b9e50SVlastimil Babka for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages; 799c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 800c96b9e50SVlastimil Babka block_start_pfn -= pageblock_nr_pages) { 801ff9543fdSMichal Nazarewicz unsigned long isolated; 802ff9543fdSMichal Nazarewicz 803f6ea3adbSDavid Rientjes /* 804f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 805f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 806be976572SVlastimil Babka * to schedule, or even abort async compaction. 807f6ea3adbSDavid Rientjes */ 808be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 809be976572SVlastimil Babka && compact_should_abort(cc)) 810be976572SVlastimil Babka break; 811f6ea3adbSDavid Rientjes 8127d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 8137d49d886SVlastimil Babka zone); 8147d49d886SVlastimil Babka if (!page) 815ff9543fdSMichal Nazarewicz continue; 816ff9543fdSMichal Nazarewicz 817ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 81868e3e926SLinus Torvalds if (!suitable_migration_target(page)) 819ff9543fdSMichal Nazarewicz continue; 82068e3e926SLinus Torvalds 821bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 822bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 823bb13ffebSMel Gorman continue; 824bb13ffebSMel Gorman 825f40d1e42SMel Gorman /* Found a block suitable for isolating free pages from */ 826e9ade569SVlastimil Babka cc->free_pfn = block_start_pfn; 827c96b9e50SVlastimil Babka isolated = isolate_freepages_block(cc, block_start_pfn, 828c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 829ff9543fdSMichal Nazarewicz nr_freepages += isolated; 830ff9543fdSMichal Nazarewicz 831ff9543fdSMichal Nazarewicz /* 832e9ade569SVlastimil Babka * Set a flag that we successfully isolated in this pageblock. 833e9ade569SVlastimil Babka * In the next loop iteration, zone->compact_cached_free_pfn 834e9ade569SVlastimil Babka * will not be updated and thus it will effectively contain the 835e9ade569SVlastimil Babka * highest pageblock we isolated pages from. 836ff9543fdSMichal Nazarewicz */ 837e9ade569SVlastimil Babka if (isolated) 838c89511abSMel Gorman cc->finished_update_free = true; 839be976572SVlastimil Babka 840be976572SVlastimil Babka /* 841be976572SVlastimil Babka * isolate_freepages_block() might have aborted due to async 842be976572SVlastimil Babka * compaction being contended 843be976572SVlastimil Babka */ 844be976572SVlastimil Babka if (cc->contended) 845be976572SVlastimil Babka break; 846c89511abSMel Gorman } 847ff9543fdSMichal Nazarewicz 848ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 849ff9543fdSMichal Nazarewicz map_pages(freelist); 850ff9543fdSMichal Nazarewicz 8517ed695e0SVlastimil Babka /* 8527ed695e0SVlastimil Babka * If we crossed the migrate scanner, we want to keep it that way 8537ed695e0SVlastimil Babka * so that compact_finished() may detect this 8547ed695e0SVlastimil Babka */ 855c96b9e50SVlastimil Babka if (block_start_pfn < low_pfn) 856e9ade569SVlastimil Babka cc->free_pfn = cc->migrate_pfn; 857c96b9e50SVlastimil Babka 858ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 859748446bbSMel Gorman } 860748446bbSMel Gorman 861748446bbSMel Gorman /* 862748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 863748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 864748446bbSMel Gorman */ 865748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 866748446bbSMel Gorman unsigned long data, 867748446bbSMel Gorman int **result) 868748446bbSMel Gorman { 869748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 870748446bbSMel Gorman struct page *freepage; 871748446bbSMel Gorman 872be976572SVlastimil Babka /* 873be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 874be976572SVlastimil Babka * contention. 875be976572SVlastimil Babka */ 876748446bbSMel Gorman if (list_empty(&cc->freepages)) { 877be976572SVlastimil Babka if (!cc->contended) 878edc2ca61SVlastimil Babka isolate_freepages(cc); 879748446bbSMel Gorman 880748446bbSMel Gorman if (list_empty(&cc->freepages)) 881748446bbSMel Gorman return NULL; 882748446bbSMel Gorman } 883748446bbSMel Gorman 884748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 885748446bbSMel Gorman list_del(&freepage->lru); 886748446bbSMel Gorman cc->nr_freepages--; 887748446bbSMel Gorman 888748446bbSMel Gorman return freepage; 889748446bbSMel Gorman } 890748446bbSMel Gorman 891748446bbSMel Gorman /* 892d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 893d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 894d53aea3dSDavid Rientjes * special handling needed for NUMA. 895d53aea3dSDavid Rientjes */ 896d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 897d53aea3dSDavid Rientjes { 898d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 899d53aea3dSDavid Rientjes 900d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 901d53aea3dSDavid Rientjes cc->nr_freepages++; 902d53aea3dSDavid Rientjes } 903d53aea3dSDavid Rientjes 904ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 905ff9543fdSMichal Nazarewicz typedef enum { 906ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 907ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 908ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 909ff9543fdSMichal Nazarewicz } isolate_migrate_t; 910ff9543fdSMichal Nazarewicz 911ff9543fdSMichal Nazarewicz /* 912edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 913edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 914edc2ca61SVlastimil Babka * compact_control. 915ff9543fdSMichal Nazarewicz */ 916ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 917ff9543fdSMichal Nazarewicz struct compact_control *cc) 918ff9543fdSMichal Nazarewicz { 919ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 920edc2ca61SVlastimil Babka struct page *page; 921edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 922edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 923ff9543fdSMichal Nazarewicz 924edc2ca61SVlastimil Babka /* 925edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 926edc2ca61SVlastimil Babka * initialized by compact_zone() 927edc2ca61SVlastimil Babka */ 928edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 929ff9543fdSMichal Nazarewicz 930ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 931a9aacbccSMel Gorman end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); 932ff9543fdSMichal Nazarewicz 933edc2ca61SVlastimil Babka /* 934edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 935edc2ca61SVlastimil Babka * Do not cross the free scanner. 936edc2ca61SVlastimil Babka */ 937edc2ca61SVlastimil Babka for (; end_pfn <= cc->free_pfn; 938edc2ca61SVlastimil Babka low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { 939edc2ca61SVlastimil Babka 940edc2ca61SVlastimil Babka /* 941edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 942edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 943edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 944edc2ca61SVlastimil Babka */ 945edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 946edc2ca61SVlastimil Babka && compact_should_abort(cc)) 947edc2ca61SVlastimil Babka break; 948edc2ca61SVlastimil Babka 9497d49d886SVlastimil Babka page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); 9507d49d886SVlastimil Babka if (!page) 951edc2ca61SVlastimil Babka continue; 952edc2ca61SVlastimil Babka 953edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 954edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 955edc2ca61SVlastimil Babka continue; 956edc2ca61SVlastimil Babka 957edc2ca61SVlastimil Babka /* 958edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 959edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 960edc2ca61SVlastimil Babka * of work satisfies the allocation. 961edc2ca61SVlastimil Babka */ 962edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 963edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 964edc2ca61SVlastimil Babka continue; 965ff9543fdSMichal Nazarewicz 966ff9543fdSMichal Nazarewicz /* Perform the isolation */ 967edc2ca61SVlastimil Babka low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, 968edc2ca61SVlastimil Babka isolate_mode); 969edc2ca61SVlastimil Babka 970e64c5237SShaohua Li if (!low_pfn || cc->contended) 971ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 972ff9543fdSMichal Nazarewicz 973edc2ca61SVlastimil Babka /* 974edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 975edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 976edc2ca61SVlastimil Babka * continue or not. 977edc2ca61SVlastimil Babka */ 978edc2ca61SVlastimil Babka break; 979edc2ca61SVlastimil Babka } 980edc2ca61SVlastimil Babka 981edc2ca61SVlastimil Babka acct_isolated(zone, cc); 982edc2ca61SVlastimil Babka /* Record where migration scanner will be restarted */ 983ff9543fdSMichal Nazarewicz cc->migrate_pfn = low_pfn; 984ff9543fdSMichal Nazarewicz 985edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 986ff9543fdSMichal Nazarewicz } 987ff9543fdSMichal Nazarewicz 988748446bbSMel Gorman static int compact_finished(struct zone *zone, 989748446bbSMel Gorman struct compact_control *cc) 990748446bbSMel Gorman { 9918fb74b9fSMel Gorman unsigned int order; 9925a03b051SAndrea Arcangeli unsigned long watermark; 99356de7263SMel Gorman 994be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 995748446bbSMel Gorman return COMPACT_PARTIAL; 996748446bbSMel Gorman 997753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 998bb13ffebSMel Gorman if (cc->free_pfn <= cc->migrate_pfn) { 99955b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 100035979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 100135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 100255b7c4c9SVlastimil Babka zone->compact_cached_free_pfn = zone_end_pfn(zone); 100355b7c4c9SVlastimil Babka 100462997027SMel Gorman /* 100562997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 100662997027SMel Gorman * by kswapd when it goes to sleep. kswapd does not set the 100762997027SMel Gorman * flag itself as the decision to be clear should be directly 100862997027SMel Gorman * based on an allocation request. 100962997027SMel Gorman */ 101062997027SMel Gorman if (!current_is_kswapd()) 101162997027SMel Gorman zone->compact_blockskip_flush = true; 101262997027SMel Gorman 1013748446bbSMel Gorman return COMPACT_COMPLETE; 1014bb13ffebSMel Gorman } 1015748446bbSMel Gorman 101682478fb7SJohannes Weiner /* 101782478fb7SJohannes Weiner * order == -1 is expected when compacting via 101882478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 101982478fb7SJohannes Weiner */ 102056de7263SMel Gorman if (cc->order == -1) 102156de7263SMel Gorman return COMPACT_CONTINUE; 102256de7263SMel Gorman 10233957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 10243957c776SMichal Hocko watermark = low_wmark_pages(zone); 10253957c776SMichal Hocko watermark += (1 << cc->order); 10263957c776SMichal Hocko 10273957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 10283957c776SMichal Hocko return COMPACT_CONTINUE; 10293957c776SMichal Hocko 103056de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 103156de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 10328fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 10338fb74b9fSMel Gorman 103456de7263SMel Gorman /* Job done if page is free of the right migratetype */ 10351fb3f8caSMel Gorman if (!list_empty(&area->free_list[cc->migratetype])) 103656de7263SMel Gorman return COMPACT_PARTIAL; 103756de7263SMel Gorman 103856de7263SMel Gorman /* Job done if allocation would set block type */ 10391fb3f8caSMel Gorman if (cc->order >= pageblock_order && area->nr_free) 104056de7263SMel Gorman return COMPACT_PARTIAL; 104156de7263SMel Gorman } 104256de7263SMel Gorman 1043748446bbSMel Gorman return COMPACT_CONTINUE; 1044748446bbSMel Gorman } 1045748446bbSMel Gorman 10463e7d3449SMel Gorman /* 10473e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 10483e7d3449SMel Gorman * Returns 10493e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 10503e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 10513e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 10523e7d3449SMel Gorman */ 10533e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 10543e7d3449SMel Gorman { 10553e7d3449SMel Gorman int fragindex; 10563e7d3449SMel Gorman unsigned long watermark; 10573e7d3449SMel Gorman 10583e7d3449SMel Gorman /* 10593957c776SMichal Hocko * order == -1 is expected when compacting via 10603957c776SMichal Hocko * /proc/sys/vm/compact_memory 10613957c776SMichal Hocko */ 10623957c776SMichal Hocko if (order == -1) 10633957c776SMichal Hocko return COMPACT_CONTINUE; 10643957c776SMichal Hocko 10653957c776SMichal Hocko /* 10663e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 10673e7d3449SMel Gorman * This is because during migration, copies of pages need to be 10683e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 10693e7d3449SMel Gorman */ 10703e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 10713e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 10723e7d3449SMel Gorman return COMPACT_SKIPPED; 10733e7d3449SMel Gorman 10743e7d3449SMel Gorman /* 10753e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 10763e7d3449SMel Gorman * low memory or external fragmentation 10773e7d3449SMel Gorman * 1078a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 1079a582a738SShaohua Li * watermarks 10803e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 10813e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 10823e7d3449SMel Gorman * 10833e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 10843e7d3449SMel Gorman */ 10853e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 10863e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 10873e7d3449SMel Gorman return COMPACT_SKIPPED; 10883e7d3449SMel Gorman 1089a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 1090a582a738SShaohua Li 0, 0)) 10913e7d3449SMel Gorman return COMPACT_PARTIAL; 10923e7d3449SMel Gorman 10933e7d3449SMel Gorman return COMPACT_CONTINUE; 10943e7d3449SMel Gorman } 10953e7d3449SMel Gorman 1096748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1097748446bbSMel Gorman { 1098748446bbSMel Gorman int ret; 1099c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1100108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 1101e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1102748446bbSMel Gorman 11033e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 11043e7d3449SMel Gorman switch (ret) { 11053e7d3449SMel Gorman case COMPACT_PARTIAL: 11063e7d3449SMel Gorman case COMPACT_SKIPPED: 11073e7d3449SMel Gorman /* Compaction is likely to fail */ 11083e7d3449SMel Gorman return ret; 11093e7d3449SMel Gorman case COMPACT_CONTINUE: 11103e7d3449SMel Gorman /* Fall through to compaction */ 11113e7d3449SMel Gorman ; 11123e7d3449SMel Gorman } 11133e7d3449SMel Gorman 1114c89511abSMel Gorman /* 1115d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1116d3132e4bSVlastimil Babka * is about to be retried after being deferred. kswapd does not do 1117d3132e4bSVlastimil Babka * this reset as it'll reset the cached information when going to sleep. 1118d3132e4bSVlastimil Babka */ 1119d3132e4bSVlastimil Babka if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) 1120d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1121d3132e4bSVlastimil Babka 1122d3132e4bSVlastimil Babka /* 1123c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1124c89511abSMel Gorman * information on where the scanners should start but check that it 1125c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1126c89511abSMel Gorman */ 1127e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1128c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1129c89511abSMel Gorman if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { 1130c89511abSMel Gorman cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); 1131c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1132c89511abSMel Gorman } 1133c89511abSMel Gorman if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { 1134c89511abSMel Gorman cc->migrate_pfn = start_pfn; 113535979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 113635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1137c89511abSMel Gorman } 1138748446bbSMel Gorman 11390eb927c0SMel Gorman trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn); 11400eb927c0SMel Gorman 1141748446bbSMel Gorman migrate_prep_local(); 1142748446bbSMel Gorman 1143748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 11449d502c1cSMinchan Kim int err; 1145748446bbSMel Gorman 1146f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1147f9e35b3bSMel Gorman case ISOLATE_ABORT: 1148f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 11495733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1150e64c5237SShaohua Li cc->nr_migratepages = 0; 1151f9e35b3bSMel Gorman goto out; 1152f9e35b3bSMel Gorman case ISOLATE_NONE: 1153748446bbSMel Gorman continue; 1154f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1155f9e35b3bSMel Gorman ; 1156f9e35b3bSMel Gorman } 1157748446bbSMel Gorman 1158d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1159e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 11607b2a2d4aSMel Gorman MR_COMPACTION); 1161748446bbSMel Gorman 1162f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1163f8c9301fSVlastimil Babka &cc->migratepages); 1164748446bbSMel Gorman 1165f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1166f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 11679d502c1cSMinchan Kim if (err) { 11685733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 11697ed695e0SVlastimil Babka /* 11707ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 11717ed695e0SVlastimil Babka * and we want compact_finished() to detect it 11727ed695e0SVlastimil Babka */ 11737ed695e0SVlastimil Babka if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) { 11744bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 11754bf2bba3SDavid Rientjes goto out; 1176748446bbSMel Gorman } 11774bf2bba3SDavid Rientjes } 1178748446bbSMel Gorman } 1179748446bbSMel Gorman 1180f9e35b3bSMel Gorman out: 1181748446bbSMel Gorman /* Release free pages and check accounting */ 1182748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 1183748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 1184748446bbSMel Gorman 11850eb927c0SMel Gorman trace_mm_compaction_end(ret); 11860eb927c0SMel Gorman 1187748446bbSMel Gorman return ret; 1188748446bbSMel Gorman } 118976ab0f53SMel Gorman 1190e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 11911f9efdefSVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended) 119256de7263SMel Gorman { 1193e64c5237SShaohua Li unsigned long ret; 119456de7263SMel Gorman struct compact_control cc = { 119556de7263SMel Gorman .nr_freepages = 0, 119656de7263SMel Gorman .nr_migratepages = 0, 119756de7263SMel Gorman .order = order, 119856de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 119956de7263SMel Gorman .zone = zone, 1200e0b9daebSDavid Rientjes .mode = mode, 120156de7263SMel Gorman }; 120256de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 120356de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 120456de7263SMel Gorman 1205e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1206e64c5237SShaohua Li 1207e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1208e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1209e64c5237SShaohua Li 1210e64c5237SShaohua Li *contended = cc.contended; 1211e64c5237SShaohua Li return ret; 121256de7263SMel Gorman } 121356de7263SMel Gorman 12145e771905SMel Gorman int sysctl_extfrag_threshold = 500; 12155e771905SMel Gorman 121656de7263SMel Gorman /** 121756de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 121856de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 121956de7263SMel Gorman * @order: The order of the current allocation 122056de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 122156de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 1222e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 12231f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 12241f9efdefSVlastimil Babka * need_resched() or lock contention 122553853e2dSVlastimil Babka * @candidate_zone: Return the zone where we think allocation should succeed 122656de7263SMel Gorman * 122756de7263SMel Gorman * This is the main entry point for direct page compaction. 122856de7263SMel Gorman */ 122956de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 123077f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 12311f9efdefSVlastimil Babka enum migrate_mode mode, int *contended, 123253853e2dSVlastimil Babka struct zone **candidate_zone) 123356de7263SMel Gorman { 123456de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 123556de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 123656de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 123756de7263SMel Gorman struct zoneref *z; 123856de7263SMel Gorman struct zone *zone; 123953853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 1240d95ea5d1SBartlomiej Zolnierkiewicz int alloc_flags = 0; 12411f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 12421f9efdefSVlastimil Babka 12431f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 124456de7263SMel Gorman 12454ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1246c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 124753853e2dSVlastimil Babka return COMPACT_SKIPPED; 124856de7263SMel Gorman 1249d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA 1250d95ea5d1SBartlomiej Zolnierkiewicz if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) 1251d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags |= ALLOC_CMA; 1252d95ea5d1SBartlomiej Zolnierkiewicz #endif 125356de7263SMel Gorman /* Compact each zone in the list */ 125456de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 125556de7263SMel Gorman nodemask) { 125656de7263SMel Gorman int status; 12571f9efdefSVlastimil Babka int zone_contended; 125856de7263SMel Gorman 125953853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 126053853e2dSVlastimil Babka continue; 126153853e2dSVlastimil Babka 1262e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 12631f9efdefSVlastimil Babka &zone_contended); 126456de7263SMel Gorman rc = max(status, rc); 12651f9efdefSVlastimil Babka /* 12661f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 12671f9efdefSVlastimil Babka * to clear all_zones_contended. 12681f9efdefSVlastimil Babka */ 12691f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 127056de7263SMel Gorman 12713e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1272d95ea5d1SBartlomiej Zolnierkiewicz if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 127353853e2dSVlastimil Babka alloc_flags)) { 127453853e2dSVlastimil Babka *candidate_zone = zone; 127553853e2dSVlastimil Babka /* 127653853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 127753853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 127853853e2dSVlastimil Babka * will repeat this with true if allocation indeed 127953853e2dSVlastimil Babka * succeeds in this zone. 128053853e2dSVlastimil Babka */ 128153853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 12821f9efdefSVlastimil Babka /* 12831f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 12841f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 12851f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 12861f9efdefSVlastimil Babka * however still fail so we better signal the 12871f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 12881f9efdefSVlastimil Babka * prevent the allocation attempt). 12891f9efdefSVlastimil Babka */ 12901f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 12911f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 12921f9efdefSVlastimil Babka 12931f9efdefSVlastimil Babka goto break_loop; 12941f9efdefSVlastimil Babka } 12951f9efdefSVlastimil Babka 12961f9efdefSVlastimil Babka if (mode != MIGRATE_ASYNC) { 129753853e2dSVlastimil Babka /* 129853853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 129953853e2dSVlastimil Babka * so we defer compaction there. If it ends up 130053853e2dSVlastimil Babka * succeeding after all, it will be reset. 130153853e2dSVlastimil Babka */ 130253853e2dSVlastimil Babka defer_compaction(zone, order); 130353853e2dSVlastimil Babka } 13041f9efdefSVlastimil Babka 13051f9efdefSVlastimil Babka /* 13061f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 13071f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 13081f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 13091f9efdefSVlastimil Babka * contention. 13101f9efdefSVlastimil Babka */ 13111f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 13121f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 13131f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 13141f9efdefSVlastimil Babka goto break_loop; 131556de7263SMel Gorman } 131656de7263SMel Gorman 13171f9efdefSVlastimil Babka continue; 13181f9efdefSVlastimil Babka break_loop: 13191f9efdefSVlastimil Babka /* 13201f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 13211f9efdefSVlastimil Babka * and assume they are not all lock contended. 13221f9efdefSVlastimil Babka */ 13231f9efdefSVlastimil Babka all_zones_contended = 0; 13241f9efdefSVlastimil Babka break; 13251f9efdefSVlastimil Babka } 13261f9efdefSVlastimil Babka 13271f9efdefSVlastimil Babka /* 13281f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 13291f9efdefSVlastimil Babka * zones that were tried were lock contended. 13301f9efdefSVlastimil Babka */ 13311f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 13321f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 13331f9efdefSVlastimil Babka 133456de7263SMel Gorman return rc; 133556de7263SMel Gorman } 133656de7263SMel Gorman 133756de7263SMel Gorman 133876ab0f53SMel Gorman /* Compact all zones within a node */ 13397103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 134076ab0f53SMel Gorman { 134176ab0f53SMel Gorman int zoneid; 134276ab0f53SMel Gorman struct zone *zone; 134376ab0f53SMel Gorman 134476ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 134576ab0f53SMel Gorman 134676ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 134776ab0f53SMel Gorman if (!populated_zone(zone)) 134876ab0f53SMel Gorman continue; 134976ab0f53SMel Gorman 13507be62de9SRik van Riel cc->nr_freepages = 0; 13517be62de9SRik van Riel cc->nr_migratepages = 0; 13527be62de9SRik van Riel cc->zone = zone; 13537be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 13547be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 135576ab0f53SMel Gorman 1356aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 13577be62de9SRik van Riel compact_zone(zone, cc); 135876ab0f53SMel Gorman 1359aff62249SRik van Riel if (cc->order > 0) { 1360de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1361de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1362de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1363aff62249SRik van Riel } 1364aff62249SRik van Riel 13657be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 13667be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 136776ab0f53SMel Gorman } 136876ab0f53SMel Gorman } 136976ab0f53SMel Gorman 13707103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 13717be62de9SRik van Riel { 13727be62de9SRik van Riel struct compact_control cc = { 13737be62de9SRik van Riel .order = order, 1374e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 13757be62de9SRik van Riel }; 13767be62de9SRik van Riel 13773a7200afSMel Gorman if (!order) 13783a7200afSMel Gorman return; 13793a7200afSMel Gorman 13807103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 13817be62de9SRik van Riel } 13827be62de9SRik van Riel 13837103f16dSAndrew Morton static void compact_node(int nid) 13847be62de9SRik van Riel { 13857be62de9SRik van Riel struct compact_control cc = { 13867be62de9SRik van Riel .order = -1, 1387e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 138891ca9186SDavid Rientjes .ignore_skip_hint = true, 13897be62de9SRik van Riel }; 13907be62de9SRik van Riel 13917103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 13927be62de9SRik van Riel } 13937be62de9SRik van Riel 139476ab0f53SMel Gorman /* Compact all nodes in the system */ 13957964c06dSJason Liu static void compact_nodes(void) 139676ab0f53SMel Gorman { 139776ab0f53SMel Gorman int nid; 139876ab0f53SMel Gorman 13998575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 14008575ec29SHugh Dickins lru_add_drain_all(); 14018575ec29SHugh Dickins 140276ab0f53SMel Gorman for_each_online_node(nid) 140376ab0f53SMel Gorman compact_node(nid); 140476ab0f53SMel Gorman } 140576ab0f53SMel Gorman 140676ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 140776ab0f53SMel Gorman int sysctl_compact_memory; 140876ab0f53SMel Gorman 140976ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 141076ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 141176ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 141276ab0f53SMel Gorman { 141376ab0f53SMel Gorman if (write) 14147964c06dSJason Liu compact_nodes(); 141576ab0f53SMel Gorman 141676ab0f53SMel Gorman return 0; 141776ab0f53SMel Gorman } 1418ed4a6d7fSMel Gorman 14195e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 14205e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 14215e771905SMel Gorman { 14225e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 14235e771905SMel Gorman 14245e771905SMel Gorman return 0; 14255e771905SMel Gorman } 14265e771905SMel Gorman 1427ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 142874e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 142910fbcf4cSKay Sievers struct device_attribute *attr, 1430ed4a6d7fSMel Gorman const char *buf, size_t count) 1431ed4a6d7fSMel Gorman { 14328575ec29SHugh Dickins int nid = dev->id; 14338575ec29SHugh Dickins 14348575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 14358575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 14368575ec29SHugh Dickins lru_add_drain_all(); 14378575ec29SHugh Dickins 14388575ec29SHugh Dickins compact_node(nid); 14398575ec29SHugh Dickins } 1440ed4a6d7fSMel Gorman 1441ed4a6d7fSMel Gorman return count; 1442ed4a6d7fSMel Gorman } 144310fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1444ed4a6d7fSMel Gorman 1445ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1446ed4a6d7fSMel Gorman { 144710fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1448ed4a6d7fSMel Gorman } 1449ed4a6d7fSMel Gorman 1450ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1451ed4a6d7fSMel Gorman { 145210fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1453ed4a6d7fSMel Gorman } 1454ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1455ff9543fdSMichal Nazarewicz 1456ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1457