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> 19b8c73fc2SAndrey Ryabinin #include <linux/kasan.h> 20748446bbSMel Gorman #include "internal.h" 21748446bbSMel Gorman 22010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 23010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 24010fc29aSMinchan Kim { 25010fc29aSMinchan Kim count_vm_event(item); 26010fc29aSMinchan Kim } 27010fc29aSMinchan Kim 28010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 29010fc29aSMinchan Kim { 30010fc29aSMinchan Kim count_vm_events(item, delta); 31010fc29aSMinchan Kim } 32010fc29aSMinchan Kim #else 33010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 34010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 35010fc29aSMinchan Kim #endif 36010fc29aSMinchan Kim 37ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 38ff9543fdSMichal Nazarewicz 39b7aba698SMel Gorman #define CREATE_TRACE_POINTS 40b7aba698SMel Gorman #include <trace/events/compaction.h> 41b7aba698SMel Gorman 42748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 43748446bbSMel Gorman { 44748446bbSMel Gorman struct page *page, *next; 456bace090SVlastimil Babka unsigned long high_pfn = 0; 46748446bbSMel Gorman 47748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 486bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 49748446bbSMel Gorman list_del(&page->lru); 50748446bbSMel Gorman __free_page(page); 516bace090SVlastimil Babka if (pfn > high_pfn) 526bace090SVlastimil Babka high_pfn = pfn; 53748446bbSMel Gorman } 54748446bbSMel Gorman 556bace090SVlastimil Babka return high_pfn; 56748446bbSMel Gorman } 57748446bbSMel Gorman 58ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 59ff9543fdSMichal Nazarewicz { 60ff9543fdSMichal Nazarewicz struct page *page; 61ff9543fdSMichal Nazarewicz 62ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 63ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 64ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 65b8c73fc2SAndrey Ryabinin kasan_alloc_pages(page, 0); 66ff9543fdSMichal Nazarewicz } 67ff9543fdSMichal Nazarewicz } 68ff9543fdSMichal Nazarewicz 6947118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7047118af0SMichal Nazarewicz { 7147118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 7247118af0SMichal Nazarewicz } 7347118af0SMichal Nazarewicz 747d49d886SVlastimil Babka /* 757d49d886SVlastimil Babka * Check that the whole (or subset of) a pageblock given by the interval of 767d49d886SVlastimil Babka * [start_pfn, end_pfn) is valid and within the same zone, before scanning it 777d49d886SVlastimil Babka * with the migration of free compaction scanner. The scanners then need to 787d49d886SVlastimil Babka * use only pfn_valid_within() check for arches that allow holes within 797d49d886SVlastimil Babka * pageblocks. 807d49d886SVlastimil Babka * 817d49d886SVlastimil Babka * Return struct page pointer of start_pfn, or NULL if checks were not passed. 827d49d886SVlastimil Babka * 837d49d886SVlastimil Babka * It's possible on some configurations to have a setup like node0 node1 node0 847d49d886SVlastimil Babka * i.e. it's possible that all pages within a zones range of pages do not 857d49d886SVlastimil Babka * belong to a single zone. We assume that a border between node0 and node1 867d49d886SVlastimil Babka * can occur within a single pageblock, but not a node0 node1 node0 877d49d886SVlastimil Babka * interleaving within a single pageblock. It is therefore sufficient to check 887d49d886SVlastimil Babka * the first and last page of a pageblock and avoid checking each individual 897d49d886SVlastimil Babka * page in a pageblock. 907d49d886SVlastimil Babka */ 917d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn, 927d49d886SVlastimil Babka unsigned long end_pfn, struct zone *zone) 937d49d886SVlastimil Babka { 947d49d886SVlastimil Babka struct page *start_page; 957d49d886SVlastimil Babka struct page *end_page; 967d49d886SVlastimil Babka 977d49d886SVlastimil Babka /* end_pfn is one past the range we are checking */ 987d49d886SVlastimil Babka end_pfn--; 997d49d886SVlastimil Babka 1007d49d886SVlastimil Babka if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) 1017d49d886SVlastimil Babka return NULL; 1027d49d886SVlastimil Babka 1037d49d886SVlastimil Babka start_page = pfn_to_page(start_pfn); 1047d49d886SVlastimil Babka 1057d49d886SVlastimil Babka if (page_zone(start_page) != zone) 1067d49d886SVlastimil Babka return NULL; 1077d49d886SVlastimil Babka 1087d49d886SVlastimil Babka end_page = pfn_to_page(end_pfn); 1097d49d886SVlastimil Babka 1107d49d886SVlastimil Babka /* This gives a shorter code than deriving page_zone(end_page) */ 1117d49d886SVlastimil Babka if (page_zone_id(start_page) != page_zone_id(end_page)) 1127d49d886SVlastimil Babka return NULL; 1137d49d886SVlastimil Babka 1147d49d886SVlastimil Babka return start_page; 1157d49d886SVlastimil Babka } 1167d49d886SVlastimil Babka 117bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 11824e2716fSJoonsoo Kim 11924e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 12024e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 12124e2716fSJoonsoo Kim 12224e2716fSJoonsoo Kim /* 12324e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 12424e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 12524e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 12624e2716fSJoonsoo Kim */ 12724e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 12824e2716fSJoonsoo Kim { 12924e2716fSJoonsoo Kim zone->compact_considered = 0; 13024e2716fSJoonsoo Kim zone->compact_defer_shift++; 13124e2716fSJoonsoo Kim 13224e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 13324e2716fSJoonsoo Kim zone->compact_order_failed = order; 13424e2716fSJoonsoo Kim 13524e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 13624e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 13724e2716fSJoonsoo Kim 13824e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 13924e2716fSJoonsoo Kim } 14024e2716fSJoonsoo Kim 14124e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 14224e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 14324e2716fSJoonsoo Kim { 14424e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 14524e2716fSJoonsoo Kim 14624e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14724e2716fSJoonsoo Kim return false; 14824e2716fSJoonsoo Kim 14924e2716fSJoonsoo Kim /* Avoid possible overflow */ 15024e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 15124e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 15224e2716fSJoonsoo Kim 15324e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 15424e2716fSJoonsoo Kim return false; 15524e2716fSJoonsoo Kim 15624e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 15724e2716fSJoonsoo Kim 15824e2716fSJoonsoo Kim return true; 15924e2716fSJoonsoo Kim } 16024e2716fSJoonsoo Kim 16124e2716fSJoonsoo Kim /* 16224e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 16324e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 16424e2716fSJoonsoo Kim * expected to succeed. 16524e2716fSJoonsoo Kim */ 16624e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 16724e2716fSJoonsoo Kim bool alloc_success) 16824e2716fSJoonsoo Kim { 16924e2716fSJoonsoo Kim if (alloc_success) { 17024e2716fSJoonsoo Kim zone->compact_considered = 0; 17124e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 17224e2716fSJoonsoo Kim } 17324e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 17424e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 17524e2716fSJoonsoo Kim 17624e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 17724e2716fSJoonsoo Kim } 17824e2716fSJoonsoo Kim 17924e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 18024e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 18124e2716fSJoonsoo Kim { 18224e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 18324e2716fSJoonsoo Kim return false; 18424e2716fSJoonsoo Kim 18524e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 18624e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 18724e2716fSJoonsoo Kim } 18824e2716fSJoonsoo Kim 189bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 190bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 191bb13ffebSMel Gorman struct page *page) 192bb13ffebSMel Gorman { 193bb13ffebSMel Gorman if (cc->ignore_skip_hint) 194bb13ffebSMel Gorman return true; 195bb13ffebSMel Gorman 196bb13ffebSMel Gorman return !get_pageblock_skip(page); 197bb13ffebSMel Gorman } 198bb13ffebSMel Gorman 19902333641SVlastimil Babka static void reset_cached_positions(struct zone *zone) 20002333641SVlastimil Babka { 20102333641SVlastimil Babka zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 20202333641SVlastimil Babka zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 203*623446e4SJoonsoo Kim zone->compact_cached_free_pfn = 204*623446e4SJoonsoo Kim round_down(zone_end_pfn(zone) - 1, pageblock_nr_pages); 20502333641SVlastimil Babka } 20602333641SVlastimil Babka 207bb13ffebSMel Gorman /* 208bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 209bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 210bb13ffebSMel Gorman * meet. 211bb13ffebSMel Gorman */ 21262997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 213bb13ffebSMel Gorman { 214bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 215108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 216bb13ffebSMel Gorman unsigned long pfn; 217bb13ffebSMel Gorman 21862997027SMel Gorman zone->compact_blockskip_flush = false; 219bb13ffebSMel Gorman 220bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 221bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 222bb13ffebSMel Gorman struct page *page; 223bb13ffebSMel Gorman 224bb13ffebSMel Gorman cond_resched(); 225bb13ffebSMel Gorman 226bb13ffebSMel Gorman if (!pfn_valid(pfn)) 227bb13ffebSMel Gorman continue; 228bb13ffebSMel Gorman 229bb13ffebSMel Gorman page = pfn_to_page(pfn); 230bb13ffebSMel Gorman if (zone != page_zone(page)) 231bb13ffebSMel Gorman continue; 232bb13ffebSMel Gorman 233bb13ffebSMel Gorman clear_pageblock_skip(page); 234bb13ffebSMel Gorman } 23502333641SVlastimil Babka 23602333641SVlastimil Babka reset_cached_positions(zone); 237bb13ffebSMel Gorman } 238bb13ffebSMel Gorman 23962997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 24062997027SMel Gorman { 24162997027SMel Gorman int zoneid; 24262997027SMel Gorman 24362997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 24462997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 24562997027SMel Gorman if (!populated_zone(zone)) 24662997027SMel Gorman continue; 24762997027SMel Gorman 24862997027SMel Gorman /* Only flush if a full compaction finished recently */ 24962997027SMel Gorman if (zone->compact_blockskip_flush) 25062997027SMel Gorman __reset_isolation_suitable(zone); 25162997027SMel Gorman } 25262997027SMel Gorman } 25362997027SMel Gorman 254bb13ffebSMel Gorman /* 255bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 25662997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 257bb13ffebSMel Gorman */ 258c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 259c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 260edc2ca61SVlastimil Babka bool migrate_scanner) 261bb13ffebSMel Gorman { 262c89511abSMel Gorman struct zone *zone = cc->zone; 26335979ef3SDavid Rientjes unsigned long pfn; 2646815bf3fSJoonsoo Kim 2656815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2666815bf3fSJoonsoo Kim return; 2676815bf3fSJoonsoo Kim 268bb13ffebSMel Gorman if (!page) 269bb13ffebSMel Gorman return; 270bb13ffebSMel Gorman 27135979ef3SDavid Rientjes if (nr_isolated) 27235979ef3SDavid Rientjes return; 27335979ef3SDavid Rientjes 274bb13ffebSMel Gorman set_pageblock_skip(page); 275c89511abSMel Gorman 27635979ef3SDavid Rientjes pfn = page_to_pfn(page); 27735979ef3SDavid Rientjes 27835979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 279c89511abSMel Gorman if (migrate_scanner) { 28035979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 28135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 282e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 283e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 28435979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 285c89511abSMel Gorman } else { 28635979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 287c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 288c89511abSMel Gorman } 289c89511abSMel Gorman } 290bb13ffebSMel Gorman #else 291bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 292bb13ffebSMel Gorman struct page *page) 293bb13ffebSMel Gorman { 294bb13ffebSMel Gorman return true; 295bb13ffebSMel Gorman } 296bb13ffebSMel Gorman 297c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 298c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 299edc2ca61SVlastimil Babka bool migrate_scanner) 300bb13ffebSMel Gorman { 301bb13ffebSMel Gorman } 302bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 303bb13ffebSMel Gorman 3041f9efdefSVlastimil Babka /* 3058b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 3068b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 3078b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 3088b44d279SVlastimil Babka * 3098b44d279SVlastimil Babka * Returns true if the lock is held 3108b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 3111f9efdefSVlastimil Babka */ 3128b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 3138b44d279SVlastimil Babka struct compact_control *cc) 3148b44d279SVlastimil Babka { 3158b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3168b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 3178b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 3188b44d279SVlastimil Babka return false; 3198b44d279SVlastimil Babka } 3208b44d279SVlastimil Babka } else { 3218b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 3228b44d279SVlastimil Babka } 3231f9efdefSVlastimil Babka 3248b44d279SVlastimil Babka return true; 3252a1402aaSMel Gorman } 3262a1402aaSMel Gorman 32785aa125fSMichal Nazarewicz /* 328c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 3298b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 3308b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 3318b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 3328b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 3338b44d279SVlastimil Babka * aborts. Sync compaction schedules. 3348b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 3358b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 336c67fe375SMel Gorman * 3378b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 3388b44d279SVlastimil Babka * async compaction due to need_resched() 3398b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3408b44d279SVlastimil Babka * scheduled) 341c67fe375SMel Gorman */ 3428b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3438b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 344c67fe375SMel Gorman { 3458b44d279SVlastimil Babka if (*locked) { 3468b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3478b44d279SVlastimil Babka *locked = false; 348c67fe375SMel Gorman } 349c67fe375SMel Gorman 3508b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 3518b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3528b44d279SVlastimil Babka return true; 3538b44d279SVlastimil Babka } 3548b44d279SVlastimil Babka 3558b44d279SVlastimil Babka if (need_resched()) { 356e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 3578b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3588b44d279SVlastimil Babka return true; 359c67fe375SMel Gorman } 360c67fe375SMel Gorman cond_resched(); 361c67fe375SMel Gorman } 362c67fe375SMel Gorman 3638b44d279SVlastimil Babka return false; 364c67fe375SMel Gorman } 365c67fe375SMel Gorman 366be976572SVlastimil Babka /* 367be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 368be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3698b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 370be976572SVlastimil Babka * is used where no lock is concerned. 371be976572SVlastimil Babka * 372be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 373be976572SVlastimil Babka * Returns true when async compaction should abort. 374be976572SVlastimil Babka */ 375be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 376be976572SVlastimil Babka { 377be976572SVlastimil Babka /* async compaction aborts if contended */ 378be976572SVlastimil Babka if (need_resched()) { 379be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3801f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 381be976572SVlastimil Babka return true; 382be976572SVlastimil Babka } 383be976572SVlastimil Babka 384be976572SVlastimil Babka cond_resched(); 385be976572SVlastimil Babka } 386be976572SVlastimil Babka 387be976572SVlastimil Babka return false; 388be976572SVlastimil Babka } 389be976572SVlastimil Babka 390c67fe375SMel Gorman /* 3919e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3929e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3939e4be470SJerome Marchand * (even though it may still end up isolating some pages). 39485aa125fSMichal Nazarewicz */ 395f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 396e14c720eSVlastimil Babka unsigned long *start_pfn, 39785aa125fSMichal Nazarewicz unsigned long end_pfn, 39885aa125fSMichal Nazarewicz struct list_head *freelist, 39985aa125fSMichal Nazarewicz bool strict) 400748446bbSMel Gorman { 401b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 402bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 403b8b2d825SXiubo Li unsigned long flags = 0; 404f40d1e42SMel Gorman bool locked = false; 405e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 406748446bbSMel Gorman 407748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 408748446bbSMel Gorman 409f40d1e42SMel Gorman /* Isolate free pages. */ 410748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 411748446bbSMel Gorman int isolated, i; 412748446bbSMel Gorman struct page *page = cursor; 413748446bbSMel Gorman 4148b44d279SVlastimil Babka /* 4158b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 4168b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 4178b44d279SVlastimil Babka * pending or async compaction detects need_resched() 4188b44d279SVlastimil Babka */ 4198b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 4208b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 4218b44d279SVlastimil Babka &locked, cc)) 4228b44d279SVlastimil Babka break; 4238b44d279SVlastimil Babka 424b7aba698SMel Gorman nr_scanned++; 425f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 4262af120bcSLaura Abbott goto isolate_fail; 4272af120bcSLaura Abbott 428bb13ffebSMel Gorman if (!valid_page) 429bb13ffebSMel Gorman valid_page = page; 4309fcd6d2eSVlastimil Babka 4319fcd6d2eSVlastimil Babka /* 4329fcd6d2eSVlastimil Babka * For compound pages such as THP and hugetlbfs, we can save 4339fcd6d2eSVlastimil Babka * potentially a lot of iterations if we skip them at once. 4349fcd6d2eSVlastimil Babka * The check is racy, but we can consider only valid values 4359fcd6d2eSVlastimil Babka * and the only danger is skipping too much. 4369fcd6d2eSVlastimil Babka */ 4379fcd6d2eSVlastimil Babka if (PageCompound(page)) { 4389fcd6d2eSVlastimil Babka unsigned int comp_order = compound_order(page); 4399fcd6d2eSVlastimil Babka 4409fcd6d2eSVlastimil Babka if (likely(comp_order < MAX_ORDER)) { 4419fcd6d2eSVlastimil Babka blockpfn += (1UL << comp_order) - 1; 4429fcd6d2eSVlastimil Babka cursor += (1UL << comp_order) - 1; 4439fcd6d2eSVlastimil Babka } 4449fcd6d2eSVlastimil Babka 4459fcd6d2eSVlastimil Babka goto isolate_fail; 4469fcd6d2eSVlastimil Babka } 4479fcd6d2eSVlastimil Babka 448f40d1e42SMel Gorman if (!PageBuddy(page)) 4492af120bcSLaura Abbott goto isolate_fail; 450f40d1e42SMel Gorman 451f40d1e42SMel Gorman /* 45269b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 45369b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 45469b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 45569b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 45669b7189fSVlastimil Babka * recheck as well. 45769b7189fSVlastimil Babka */ 45869b7189fSVlastimil Babka if (!locked) { 45969b7189fSVlastimil Babka /* 460f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 461f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 462f40d1e42SMel Gorman * heavily contended if there are parallel allocations 463f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 464f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 465f40d1e42SMel Gorman * possible. 466f40d1e42SMel Gorman */ 4678b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4688b44d279SVlastimil Babka &flags, cc); 469f40d1e42SMel Gorman if (!locked) 470f40d1e42SMel Gorman break; 471f40d1e42SMel Gorman 472f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 473f40d1e42SMel Gorman if (!PageBuddy(page)) 4742af120bcSLaura Abbott goto isolate_fail; 47569b7189fSVlastimil Babka } 476748446bbSMel Gorman 477748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 478748446bbSMel Gorman isolated = split_free_page(page); 479748446bbSMel Gorman total_isolated += isolated; 480748446bbSMel Gorman for (i = 0; i < isolated; i++) { 481748446bbSMel Gorman list_add(&page->lru, freelist); 482748446bbSMel Gorman page++; 483748446bbSMel Gorman } 484748446bbSMel Gorman 485748446bbSMel Gorman /* If a page was split, advance to the end of it */ 486748446bbSMel Gorman if (isolated) { 487932ff6bbSJoonsoo Kim cc->nr_freepages += isolated; 488932ff6bbSJoonsoo Kim if (!strict && 489932ff6bbSJoonsoo Kim cc->nr_migratepages <= cc->nr_freepages) { 490932ff6bbSJoonsoo Kim blockpfn += isolated; 491932ff6bbSJoonsoo Kim break; 492932ff6bbSJoonsoo Kim } 493932ff6bbSJoonsoo Kim 494748446bbSMel Gorman blockpfn += isolated - 1; 495748446bbSMel Gorman cursor += isolated - 1; 4962af120bcSLaura Abbott continue; 497748446bbSMel Gorman } 4982af120bcSLaura Abbott 4992af120bcSLaura Abbott isolate_fail: 5002af120bcSLaura Abbott if (strict) 5012af120bcSLaura Abbott break; 5022af120bcSLaura Abbott else 5032af120bcSLaura Abbott continue; 5042af120bcSLaura Abbott 505748446bbSMel Gorman } 506748446bbSMel Gorman 5079fcd6d2eSVlastimil Babka /* 5089fcd6d2eSVlastimil Babka * There is a tiny chance that we have read bogus compound_order(), 5099fcd6d2eSVlastimil Babka * so be careful to not go outside of the pageblock. 5109fcd6d2eSVlastimil Babka */ 5119fcd6d2eSVlastimil Babka if (unlikely(blockpfn > end_pfn)) 5129fcd6d2eSVlastimil Babka blockpfn = end_pfn; 5139fcd6d2eSVlastimil Babka 514e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 515e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 516e34d85f0SJoonsoo Kim 517e14c720eSVlastimil Babka /* Record how far we have got within the block */ 518e14c720eSVlastimil Babka *start_pfn = blockpfn; 519e14c720eSVlastimil Babka 520f40d1e42SMel Gorman /* 521f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 522f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 523f40d1e42SMel Gorman * returned and CMA will fail. 524f40d1e42SMel Gorman */ 5252af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 526f40d1e42SMel Gorman total_isolated = 0; 527f40d1e42SMel Gorman 528f40d1e42SMel Gorman if (locked) 529f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 530f40d1e42SMel Gorman 531bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 532bb13ffebSMel Gorman if (blockpfn == end_pfn) 533edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 534bb13ffebSMel Gorman 535010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 536397487dbSMel Gorman if (total_isolated) 537010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 538748446bbSMel Gorman return total_isolated; 539748446bbSMel Gorman } 540748446bbSMel Gorman 54185aa125fSMichal Nazarewicz /** 54285aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 54385aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 54485aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 54585aa125fSMichal Nazarewicz * 54685aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 54785aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 54885aa125fSMichal Nazarewicz * undo its actions and return zero. 54985aa125fSMichal Nazarewicz * 55085aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 55185aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 55285aa125fSMichal Nazarewicz * a free page). 55385aa125fSMichal Nazarewicz */ 554ff9543fdSMichal Nazarewicz unsigned long 555bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 556bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 55785aa125fSMichal Nazarewicz { 558f40d1e42SMel Gorman unsigned long isolated, pfn, block_end_pfn; 55985aa125fSMichal Nazarewicz LIST_HEAD(freelist); 56085aa125fSMichal Nazarewicz 5617d49d886SVlastimil Babka pfn = start_pfn; 56285aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 5637d49d886SVlastimil Babka 5647d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 5657d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 566e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 567e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5687d49d886SVlastimil Babka 56985aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 57085aa125fSMichal Nazarewicz 57158420016SJoonsoo Kim /* 57258420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 57358420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 57458420016SJoonsoo Kim * scanning range to right one. 57558420016SJoonsoo Kim */ 57658420016SJoonsoo Kim if (pfn >= block_end_pfn) { 57758420016SJoonsoo Kim block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 57858420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 57958420016SJoonsoo Kim } 58058420016SJoonsoo Kim 5817d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 5827d49d886SVlastimil Babka break; 5837d49d886SVlastimil Babka 584e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 585e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 58685aa125fSMichal Nazarewicz 58785aa125fSMichal Nazarewicz /* 58885aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 58985aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 59085aa125fSMichal Nazarewicz * non-free pages). 59185aa125fSMichal Nazarewicz */ 59285aa125fSMichal Nazarewicz if (!isolated) 59385aa125fSMichal Nazarewicz break; 59485aa125fSMichal Nazarewicz 59585aa125fSMichal Nazarewicz /* 59685aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 59785aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 59885aa125fSMichal Nazarewicz * page may span two pageblocks). 59985aa125fSMichal Nazarewicz */ 60085aa125fSMichal Nazarewicz } 60185aa125fSMichal Nazarewicz 60285aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 60385aa125fSMichal Nazarewicz map_pages(&freelist); 60485aa125fSMichal Nazarewicz 60585aa125fSMichal Nazarewicz if (pfn < end_pfn) { 60685aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 60785aa125fSMichal Nazarewicz release_freepages(&freelist); 60885aa125fSMichal Nazarewicz return 0; 60985aa125fSMichal Nazarewicz } 61085aa125fSMichal Nazarewicz 61185aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 61285aa125fSMichal Nazarewicz return pfn; 61385aa125fSMichal Nazarewicz } 61485aa125fSMichal Nazarewicz 615748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 616edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 617748446bbSMel Gorman { 618748446bbSMel Gorman struct page *page; 619b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 620748446bbSMel Gorman 621edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 622edc2ca61SVlastimil Babka return; 623edc2ca61SVlastimil Babka 624b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 625b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 626748446bbSMel Gorman 627c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 628c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 629c67fe375SMel Gorman } 630748446bbSMel Gorman 631748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 632748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 633748446bbSMel Gorman { 634bc693045SMinchan Kim unsigned long active, inactive, isolated; 635748446bbSMel Gorman 636748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 637748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 638bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 639bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 640748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 641748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 642748446bbSMel Gorman 643bc693045SMinchan Kim return isolated > (inactive + active) / 2; 644748446bbSMel Gorman } 645748446bbSMel Gorman 6462fe86e00SMichal Nazarewicz /** 647edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 648edc2ca61SVlastimil Babka * a single pageblock 6492fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 650edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 651edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 652edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6532fe86e00SMichal Nazarewicz * 6542fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 655edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 656edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 657edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 658edc2ca61SVlastimil Babka * than end_pfn). 6592fe86e00SMichal Nazarewicz * 660edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 661edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 662edc2ca61SVlastimil Babka * is neither read nor updated. 663748446bbSMel Gorman */ 664edc2ca61SVlastimil Babka static unsigned long 665edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 666edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 667748446bbSMel Gorman { 668edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 669b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 670748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 671fa9add64SHugh Dickins struct lruvec *lruvec; 672b8b2d825SXiubo Li unsigned long flags = 0; 6732a1402aaSMel Gorman bool locked = false; 674bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 675e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 676748446bbSMel Gorman 677748446bbSMel Gorman /* 678748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 679748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 680748446bbSMel Gorman * delay for some time until fewer pages are isolated 681748446bbSMel Gorman */ 682748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 683f9e35b3bSMel Gorman /* async migration should just abort */ 684e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6852fe86e00SMichal Nazarewicz return 0; 686f9e35b3bSMel Gorman 687748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 688748446bbSMel Gorman 689748446bbSMel Gorman if (fatal_signal_pending(current)) 6902fe86e00SMichal Nazarewicz return 0; 691748446bbSMel Gorman } 692748446bbSMel Gorman 693be976572SVlastimil Babka if (compact_should_abort(cc)) 694aeef4b83SDavid Rientjes return 0; 695aeef4b83SDavid Rientjes 696748446bbSMel Gorman /* Time to isolate some pages for migration */ 697748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 69829c0dde8SVlastimil Babka bool is_lru; 69929c0dde8SVlastimil Babka 7008b44d279SVlastimil Babka /* 7018b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 7028b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7038b44d279SVlastimil Babka * if contended. 7048b44d279SVlastimil Babka */ 7058b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 7068b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 7078b44d279SVlastimil Babka &locked, cc)) 7088b44d279SVlastimil Babka break; 709b2eef8c0SAndrea Arcangeli 710748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 711748446bbSMel Gorman continue; 712b7aba698SMel Gorman nr_scanned++; 713748446bbSMel Gorman 714748446bbSMel Gorman page = pfn_to_page(low_pfn); 715dc908600SMel Gorman 716bb13ffebSMel Gorman if (!valid_page) 717bb13ffebSMel Gorman valid_page = page; 718bb13ffebSMel Gorman 719c122b208SJoonsoo Kim /* 72099c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 72199c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 72299c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 72399c0fd5eSVlastimil Babka * potential isolation targets. 7246c14466cSMel Gorman */ 72599c0fd5eSVlastimil Babka if (PageBuddy(page)) { 72699c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 72799c0fd5eSVlastimil Babka 72899c0fd5eSVlastimil Babka /* 72999c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 73099c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 73199c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 73299c0fd5eSVlastimil Babka */ 73399c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 73499c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 735748446bbSMel Gorman continue; 73699c0fd5eSVlastimil Babka } 737748446bbSMel Gorman 7389927af74SMel Gorman /* 739bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 740bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 741bf6bddf1SRafael Aquini * Skip any other type of page 742bf6bddf1SRafael Aquini */ 74329c0dde8SVlastimil Babka is_lru = PageLRU(page); 74429c0dde8SVlastimil Babka if (!is_lru) { 745bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 746d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 747bf6bddf1SRafael Aquini /* Successfully isolated */ 748b6c75016SJoonsoo Kim goto isolate_success; 749bf6bddf1SRafael Aquini } 750bf6bddf1SRafael Aquini } 751bf6bddf1SRafael Aquini } 752bc835011SAndrea Arcangeli 753bc835011SAndrea Arcangeli /* 75429c0dde8SVlastimil Babka * Regardless of being on LRU, compound pages such as THP and 75529c0dde8SVlastimil Babka * hugetlbfs are not to be compacted. We can potentially save 75629c0dde8SVlastimil Babka * a lot of iterations if we skip them at once. The check is 75729c0dde8SVlastimil Babka * racy, but we can consider only valid values and the only 75829c0dde8SVlastimil Babka * danger is skipping too much. 759bc835011SAndrea Arcangeli */ 76029c0dde8SVlastimil Babka if (PageCompound(page)) { 76129c0dde8SVlastimil Babka unsigned int comp_order = compound_order(page); 76229c0dde8SVlastimil Babka 76329c0dde8SVlastimil Babka if (likely(comp_order < MAX_ORDER)) 76429c0dde8SVlastimil Babka low_pfn += (1UL << comp_order) - 1; 765edc2ca61SVlastimil Babka 7662a1402aaSMel Gorman continue; 7672a1402aaSMel Gorman } 7682a1402aaSMel Gorman 76929c0dde8SVlastimil Babka if (!is_lru) 77029c0dde8SVlastimil Babka continue; 77129c0dde8SVlastimil Babka 772119d6d59SDavid Rientjes /* 773119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 774119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 775119d6d59SDavid Rientjes * admittedly racy check. 776119d6d59SDavid Rientjes */ 777119d6d59SDavid Rientjes if (!page_mapping(page) && 778119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 779119d6d59SDavid Rientjes continue; 780119d6d59SDavid Rientjes 78169b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 78269b7189fSVlastimil Babka if (!locked) { 7838b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7848b44d279SVlastimil Babka &flags, cc); 7858b44d279SVlastimil Babka if (!locked) 7862a1402aaSMel Gorman break; 7872a1402aaSMel Gorman 78829c0dde8SVlastimil Babka /* Recheck PageLRU and PageCompound under lock */ 7892a1402aaSMel Gorman if (!PageLRU(page)) 7902a1402aaSMel Gorman continue; 79129c0dde8SVlastimil Babka 79229c0dde8SVlastimil Babka /* 79329c0dde8SVlastimil Babka * Page become compound since the non-locked check, 79429c0dde8SVlastimil Babka * and it's on LRU. It can only be a THP so the order 79529c0dde8SVlastimil Babka * is safe to read and it's 0 for tail pages. 79629c0dde8SVlastimil Babka */ 79729c0dde8SVlastimil Babka if (unlikely(PageCompound(page))) { 79829c0dde8SVlastimil Babka low_pfn += (1UL << compound_order(page)) - 1; 799bc835011SAndrea Arcangeli continue; 800bc835011SAndrea Arcangeli } 80169b7189fSVlastimil Babka } 802bc835011SAndrea Arcangeli 803fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 804fa9add64SHugh Dickins 805748446bbSMel Gorman /* Try isolate the page */ 806edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 807748446bbSMel Gorman continue; 808748446bbSMel Gorman 80929c0dde8SVlastimil Babka VM_BUG_ON_PAGE(PageCompound(page), page); 810bc835011SAndrea Arcangeli 811748446bbSMel Gorman /* Successfully isolated */ 812fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 813b6c75016SJoonsoo Kim 814b6c75016SJoonsoo Kim isolate_success: 815748446bbSMel Gorman list_add(&page->lru, migratelist); 816748446bbSMel Gorman cc->nr_migratepages++; 817b7aba698SMel Gorman nr_isolated++; 818748446bbSMel Gorman 819748446bbSMel Gorman /* Avoid isolating too much */ 82031b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 82131b8384aSHillf Danton ++low_pfn; 822748446bbSMel Gorman break; 823748446bbSMel Gorman } 82431b8384aSHillf Danton } 825748446bbSMel Gorman 82699c0fd5eSVlastimil Babka /* 82799c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 82899c0fd5eSVlastimil Babka * the range to be scanned. 82999c0fd5eSVlastimil Babka */ 83099c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 83199c0fd5eSVlastimil Babka low_pfn = end_pfn; 83299c0fd5eSVlastimil Babka 833c67fe375SMel Gorman if (locked) 834c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 835748446bbSMel Gorman 83650b5b094SVlastimil Babka /* 83750b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 83850b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 83950b5b094SVlastimil Babka */ 84035979ef3SDavid Rientjes if (low_pfn == end_pfn) 841edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 842bb13ffebSMel Gorman 843e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 844e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 845b7aba698SMel Gorman 846010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 847397487dbSMel Gorman if (nr_isolated) 848010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 849397487dbSMel Gorman 8502fe86e00SMichal Nazarewicz return low_pfn; 8512fe86e00SMichal Nazarewicz } 8522fe86e00SMichal Nazarewicz 853edc2ca61SVlastimil Babka /** 854edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 855edc2ca61SVlastimil Babka * @cc: Compaction control structure. 856edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 857edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 858edc2ca61SVlastimil Babka * 859edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 860edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 861edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 862edc2ca61SVlastimil Babka */ 863edc2ca61SVlastimil Babka unsigned long 864edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 865edc2ca61SVlastimil Babka unsigned long end_pfn) 866edc2ca61SVlastimil Babka { 867edc2ca61SVlastimil Babka unsigned long pfn, block_end_pfn; 868edc2ca61SVlastimil Babka 869edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 870edc2ca61SVlastimil Babka pfn = start_pfn; 871edc2ca61SVlastimil Babka block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 872edc2ca61SVlastimil Babka 873edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 874edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 875edc2ca61SVlastimil Babka 876edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 877edc2ca61SVlastimil Babka 8787d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 879edc2ca61SVlastimil Babka continue; 880edc2ca61SVlastimil Babka 881edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 882edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 883edc2ca61SVlastimil Babka 884edc2ca61SVlastimil Babka /* 885edc2ca61SVlastimil Babka * In case of fatal failure, release everything that might 886edc2ca61SVlastimil Babka * have been isolated in the previous iteration, and signal 887edc2ca61SVlastimil Babka * the failure back to caller. 888edc2ca61SVlastimil Babka */ 889edc2ca61SVlastimil Babka if (!pfn) { 890edc2ca61SVlastimil Babka putback_movable_pages(&cc->migratepages); 891edc2ca61SVlastimil Babka cc->nr_migratepages = 0; 892edc2ca61SVlastimil Babka break; 893edc2ca61SVlastimil Babka } 8946ea41c0cSJoonsoo Kim 8956ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8966ea41c0cSJoonsoo Kim break; 897edc2ca61SVlastimil Babka } 898edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 899edc2ca61SVlastimil Babka 900edc2ca61SVlastimil Babka return pfn; 901edc2ca61SVlastimil Babka } 902edc2ca61SVlastimil Babka 903ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 904ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 905018e9a49SAndrew Morton 906018e9a49SAndrew Morton /* Returns true if the page is within a block suitable for migration to */ 907018e9a49SAndrew Morton static bool suitable_migration_target(struct page *page) 908018e9a49SAndrew Morton { 909018e9a49SAndrew Morton /* If the page is a large free page, then disallow migration */ 910018e9a49SAndrew Morton if (PageBuddy(page)) { 911018e9a49SAndrew Morton /* 912018e9a49SAndrew Morton * We are checking page_order without zone->lock taken. But 913018e9a49SAndrew Morton * the only small danger is that we skip a potentially suitable 914018e9a49SAndrew Morton * pageblock, so it's not worth to check order for valid range. 915018e9a49SAndrew Morton */ 916018e9a49SAndrew Morton if (page_order_unsafe(page) >= pageblock_order) 917018e9a49SAndrew Morton return false; 918018e9a49SAndrew Morton } 919018e9a49SAndrew Morton 920018e9a49SAndrew Morton /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 921018e9a49SAndrew Morton if (migrate_async_suitable(get_pageblock_migratetype(page))) 922018e9a49SAndrew Morton return true; 923018e9a49SAndrew Morton 924018e9a49SAndrew Morton /* Otherwise skip the block */ 925018e9a49SAndrew Morton return false; 926018e9a49SAndrew Morton } 927018e9a49SAndrew Morton 928ff9543fdSMichal Nazarewicz /* 929f2849aa0SVlastimil Babka * Test whether the free scanner has reached the same or lower pageblock than 930f2849aa0SVlastimil Babka * the migration scanner, and compaction should thus terminate. 931f2849aa0SVlastimil Babka */ 932f2849aa0SVlastimil Babka static inline bool compact_scanners_met(struct compact_control *cc) 933f2849aa0SVlastimil Babka { 934f2849aa0SVlastimil Babka return (cc->free_pfn >> pageblock_order) 935f2849aa0SVlastimil Babka <= (cc->migrate_pfn >> pageblock_order); 936f2849aa0SVlastimil Babka } 937f2849aa0SVlastimil Babka 938f2849aa0SVlastimil Babka /* 939ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 940ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 941ff9543fdSMichal Nazarewicz */ 942edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 943ff9543fdSMichal Nazarewicz { 944edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 945ff9543fdSMichal Nazarewicz struct page *page; 946c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 947e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 948c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 949c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 950ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9512fe86e00SMichal Nazarewicz 952ff9543fdSMichal Nazarewicz /* 953ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 95449e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 955e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 956e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 957c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 958c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 959c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 96049e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 96149e068f0SVlastimil Babka * is using. 962ff9543fdSMichal Nazarewicz */ 963e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 964c96b9e50SVlastimil Babka block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); 965c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 966c96b9e50SVlastimil Babka zone_end_pfn(zone)); 9677ed695e0SVlastimil Babka low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); 9682fe86e00SMichal Nazarewicz 969ff9543fdSMichal Nazarewicz /* 970ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 971ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 972ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 973ff9543fdSMichal Nazarewicz */ 974f5f61a32SVlastimil Babka for (; block_start_pfn >= low_pfn; 975c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 976e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 977e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 978ff9543fdSMichal Nazarewicz 979f6ea3adbSDavid Rientjes /* 980f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 981f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 982be976572SVlastimil Babka * to schedule, or even abort async compaction. 983f6ea3adbSDavid Rientjes */ 984be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 985be976572SVlastimil Babka && compact_should_abort(cc)) 986be976572SVlastimil Babka break; 987f6ea3adbSDavid Rientjes 9887d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 9897d49d886SVlastimil Babka zone); 9907d49d886SVlastimil Babka if (!page) 991ff9543fdSMichal Nazarewicz continue; 992ff9543fdSMichal Nazarewicz 993ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 99468e3e926SLinus Torvalds if (!suitable_migration_target(page)) 995ff9543fdSMichal Nazarewicz continue; 99668e3e926SLinus Torvalds 997bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 998bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 999bb13ffebSMel Gorman continue; 1000bb13ffebSMel Gorman 1001e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 1002932ff6bbSJoonsoo Kim isolate_freepages_block(cc, &isolate_start_pfn, 1003c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 1004ff9543fdSMichal Nazarewicz 1005ff9543fdSMichal Nazarewicz /* 1006f5f61a32SVlastimil Babka * If we isolated enough freepages, or aborted due to async 1007f5f61a32SVlastimil Babka * compaction being contended, terminate the loop. 1008e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 1009e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 1010e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 1011e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 1012e14c720eSVlastimil Babka * pageblock. 1013e14c720eSVlastimil Babka * In that case we will however want to restart at the start 1014e14c720eSVlastimil Babka * of the previous pageblock. 1015e14c720eSVlastimil Babka */ 1016f5f61a32SVlastimil Babka if ((cc->nr_freepages >= cc->nr_migratepages) 1017f5f61a32SVlastimil Babka || cc->contended) { 1018f5f61a32SVlastimil Babka if (isolate_start_pfn >= block_end_pfn) 1019f5f61a32SVlastimil Babka isolate_start_pfn = 1020e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 1021be976572SVlastimil Babka break; 1022f5f61a32SVlastimil Babka } else { 1023f5f61a32SVlastimil Babka /* 1024f5f61a32SVlastimil Babka * isolate_freepages_block() should not terminate 1025f5f61a32SVlastimil Babka * prematurely unless contended, or isolated enough 1026f5f61a32SVlastimil Babka */ 1027f5f61a32SVlastimil Babka VM_BUG_ON(isolate_start_pfn < block_end_pfn); 1028f5f61a32SVlastimil Babka } 1029c89511abSMel Gorman } 1030ff9543fdSMichal Nazarewicz 1031ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 1032ff9543fdSMichal Nazarewicz map_pages(freelist); 1033ff9543fdSMichal Nazarewicz 10347ed695e0SVlastimil Babka /* 1035f5f61a32SVlastimil Babka * Record where the free scanner will restart next time. Either we 1036f5f61a32SVlastimil Babka * broke from the loop and set isolate_start_pfn based on the last 1037f5f61a32SVlastimil Babka * call to isolate_freepages_block(), or we met the migration scanner 1038f5f61a32SVlastimil Babka * and the loop terminated due to isolate_start_pfn < low_pfn 10397ed695e0SVlastimil Babka */ 1040f5f61a32SVlastimil Babka cc->free_pfn = isolate_start_pfn; 1041748446bbSMel Gorman } 1042748446bbSMel Gorman 1043748446bbSMel Gorman /* 1044748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 1045748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1046748446bbSMel Gorman */ 1047748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1048748446bbSMel Gorman unsigned long data, 1049748446bbSMel Gorman int **result) 1050748446bbSMel Gorman { 1051748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1052748446bbSMel Gorman struct page *freepage; 1053748446bbSMel Gorman 1054be976572SVlastimil Babka /* 1055be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1056be976572SVlastimil Babka * contention. 1057be976572SVlastimil Babka */ 1058748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1059be976572SVlastimil Babka if (!cc->contended) 1060edc2ca61SVlastimil Babka isolate_freepages(cc); 1061748446bbSMel Gorman 1062748446bbSMel Gorman if (list_empty(&cc->freepages)) 1063748446bbSMel Gorman return NULL; 1064748446bbSMel Gorman } 1065748446bbSMel Gorman 1066748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1067748446bbSMel Gorman list_del(&freepage->lru); 1068748446bbSMel Gorman cc->nr_freepages--; 1069748446bbSMel Gorman 1070748446bbSMel Gorman return freepage; 1071748446bbSMel Gorman } 1072748446bbSMel Gorman 1073748446bbSMel Gorman /* 1074d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1075d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1076d53aea3dSDavid Rientjes * special handling needed for NUMA. 1077d53aea3dSDavid Rientjes */ 1078d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1079d53aea3dSDavid Rientjes { 1080d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1081d53aea3dSDavid Rientjes 1082d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1083d53aea3dSDavid Rientjes cc->nr_freepages++; 1084d53aea3dSDavid Rientjes } 1085d53aea3dSDavid Rientjes 1086ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1087ff9543fdSMichal Nazarewicz typedef enum { 1088ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1089ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1090ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1091ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1092ff9543fdSMichal Nazarewicz 1093ff9543fdSMichal Nazarewicz /* 10945bbe3547SEric B Munson * Allow userspace to control policy on scanning the unevictable LRU for 10955bbe3547SEric B Munson * compactable pages. 10965bbe3547SEric B Munson */ 10975bbe3547SEric B Munson int sysctl_compact_unevictable_allowed __read_mostly = 1; 10985bbe3547SEric B Munson 10995bbe3547SEric B Munson /* 1100edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1101edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1102edc2ca61SVlastimil Babka * compact_control. 1103ff9543fdSMichal Nazarewicz */ 1104ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1105ff9543fdSMichal Nazarewicz struct compact_control *cc) 1106ff9543fdSMichal Nazarewicz { 1107ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 11081a16718cSJoonsoo Kim unsigned long isolate_start_pfn; 1109edc2ca61SVlastimil Babka struct page *page; 1110edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 11115bbe3547SEric B Munson (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | 1112edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1113ff9543fdSMichal Nazarewicz 1114edc2ca61SVlastimil Babka /* 1115edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1116edc2ca61SVlastimil Babka * initialized by compact_zone() 1117edc2ca61SVlastimil Babka */ 1118edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 1119ff9543fdSMichal Nazarewicz 1120ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 1121a9aacbccSMel Gorman end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); 1122ff9543fdSMichal Nazarewicz 1123edc2ca61SVlastimil Babka /* 1124edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1125edc2ca61SVlastimil Babka * Do not cross the free scanner. 1126edc2ca61SVlastimil Babka */ 1127edc2ca61SVlastimil Babka for (; end_pfn <= cc->free_pfn; 1128edc2ca61SVlastimil Babka low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { 1129edc2ca61SVlastimil Babka 1130edc2ca61SVlastimil Babka /* 1131edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1132edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1133edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1134edc2ca61SVlastimil Babka */ 1135edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1136edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1137edc2ca61SVlastimil Babka break; 1138edc2ca61SVlastimil Babka 11397d49d886SVlastimil Babka page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); 11407d49d886SVlastimil Babka if (!page) 1141edc2ca61SVlastimil Babka continue; 1142edc2ca61SVlastimil Babka 1143edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1144edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1145edc2ca61SVlastimil Babka continue; 1146edc2ca61SVlastimil Babka 1147edc2ca61SVlastimil Babka /* 1148edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1149edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1150edc2ca61SVlastimil Babka * of work satisfies the allocation. 1151edc2ca61SVlastimil Babka */ 1152edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1153edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1154edc2ca61SVlastimil Babka continue; 1155ff9543fdSMichal Nazarewicz 1156ff9543fdSMichal Nazarewicz /* Perform the isolation */ 11571a16718cSJoonsoo Kim isolate_start_pfn = low_pfn; 1158edc2ca61SVlastimil Babka low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, 1159edc2ca61SVlastimil Babka isolate_mode); 1160edc2ca61SVlastimil Babka 1161ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1162ff59909aSHugh Dickins acct_isolated(zone, cc); 1163ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1164ff59909aSHugh Dickins } 1165ff9543fdSMichal Nazarewicz 1166edc2ca61SVlastimil Babka /* 11671a16718cSJoonsoo Kim * Record where we could have freed pages by migration and not 11681a16718cSJoonsoo Kim * yet flushed them to buddy allocator. 11691a16718cSJoonsoo Kim * - this is the lowest page that could have been isolated and 11701a16718cSJoonsoo Kim * then freed by migration. 11711a16718cSJoonsoo Kim */ 11721a16718cSJoonsoo Kim if (cc->nr_migratepages && !cc->last_migrated_pfn) 11731a16718cSJoonsoo Kim cc->last_migrated_pfn = isolate_start_pfn; 11741a16718cSJoonsoo Kim 11751a16718cSJoonsoo Kim /* 1176edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1177edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1178edc2ca61SVlastimil Babka * continue or not. 1179edc2ca61SVlastimil Babka */ 1180edc2ca61SVlastimil Babka break; 1181edc2ca61SVlastimil Babka } 1182edc2ca61SVlastimil Babka 1183edc2ca61SVlastimil Babka acct_isolated(zone, cc); 1184f2849aa0SVlastimil Babka /* Record where migration scanner will be restarted. */ 1185f2849aa0SVlastimil Babka cc->migrate_pfn = low_pfn; 1186ff9543fdSMichal Nazarewicz 1187edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1188ff9543fdSMichal Nazarewicz } 1189ff9543fdSMichal Nazarewicz 119021c527a3SYaowei Bai /* 119121c527a3SYaowei Bai * order == -1 is expected when compacting via 119221c527a3SYaowei Bai * /proc/sys/vm/compact_memory 119321c527a3SYaowei Bai */ 119421c527a3SYaowei Bai static inline bool is_via_compact_memory(int order) 119521c527a3SYaowei Bai { 119621c527a3SYaowei Bai return order == -1; 119721c527a3SYaowei Bai } 119821c527a3SYaowei Bai 1199837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc, 12006d7ce559SDavid Rientjes const int migratetype) 1201748446bbSMel Gorman { 12028fb74b9fSMel Gorman unsigned int order; 12035a03b051SAndrea Arcangeli unsigned long watermark; 120456de7263SMel Gorman 1205be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 12062d1e1041SVlastimil Babka return COMPACT_CONTENDED; 1207748446bbSMel Gorman 1208753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1209f2849aa0SVlastimil Babka if (compact_scanners_met(cc)) { 121055b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 121102333641SVlastimil Babka reset_cached_positions(zone); 121255b7c4c9SVlastimil Babka 121362997027SMel Gorman /* 121462997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 121562997027SMel Gorman * by kswapd when it goes to sleep. kswapd does not set the 121662997027SMel Gorman * flag itself as the decision to be clear should be directly 121762997027SMel Gorman * based on an allocation request. 121862997027SMel Gorman */ 121962997027SMel Gorman if (!current_is_kswapd()) 122062997027SMel Gorman zone->compact_blockskip_flush = true; 122162997027SMel Gorman 1222748446bbSMel Gorman return COMPACT_COMPLETE; 1223bb13ffebSMel Gorman } 1224748446bbSMel Gorman 122521c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 122656de7263SMel Gorman return COMPACT_CONTINUE; 122756de7263SMel Gorman 12283957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 12293957c776SMichal Hocko watermark = low_wmark_pages(zone); 12303957c776SMichal Hocko 1231ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1232ebff3980SVlastimil Babka cc->alloc_flags)) 12333957c776SMichal Hocko return COMPACT_CONTINUE; 12343957c776SMichal Hocko 123556de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 123656de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 12378fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 12382149cdaeSJoonsoo Kim bool can_steal; 12398fb74b9fSMel Gorman 124056de7263SMel Gorman /* Job done if page is free of the right migratetype */ 12416d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 124256de7263SMel Gorman return COMPACT_PARTIAL; 124356de7263SMel Gorman 12442149cdaeSJoonsoo Kim #ifdef CONFIG_CMA 12452149cdaeSJoonsoo Kim /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ 12462149cdaeSJoonsoo Kim if (migratetype == MIGRATE_MOVABLE && 12472149cdaeSJoonsoo Kim !list_empty(&area->free_list[MIGRATE_CMA])) 12482149cdaeSJoonsoo Kim return COMPACT_PARTIAL; 12492149cdaeSJoonsoo Kim #endif 12502149cdaeSJoonsoo Kim /* 12512149cdaeSJoonsoo Kim * Job done if allocation would steal freepages from 12522149cdaeSJoonsoo Kim * other migratetype buddy lists. 12532149cdaeSJoonsoo Kim */ 12542149cdaeSJoonsoo Kim if (find_suitable_fallback(area, order, migratetype, 12552149cdaeSJoonsoo Kim true, &can_steal) != -1) 125656de7263SMel Gorman return COMPACT_PARTIAL; 125756de7263SMel Gorman } 125856de7263SMel Gorman 1259837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1260837d026dSJoonsoo Kim } 1261837d026dSJoonsoo Kim 1262837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc, 1263837d026dSJoonsoo Kim const int migratetype) 1264837d026dSJoonsoo Kim { 1265837d026dSJoonsoo Kim int ret; 1266837d026dSJoonsoo Kim 1267837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1268837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1269837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1270837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1271837d026dSJoonsoo Kim 1272837d026dSJoonsoo Kim return ret; 1273748446bbSMel Gorman } 1274748446bbSMel Gorman 12753e7d3449SMel Gorman /* 12763e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 12773e7d3449SMel Gorman * Returns 12783e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 12793e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 12803e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 12813e7d3449SMel Gorman */ 1282837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order, 1283ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 12843e7d3449SMel Gorman { 12853e7d3449SMel Gorman int fragindex; 12863e7d3449SMel Gorman unsigned long watermark; 12873e7d3449SMel Gorman 128821c527a3SYaowei Bai if (is_via_compact_memory(order)) 12893957c776SMichal Hocko return COMPACT_CONTINUE; 12903957c776SMichal Hocko 1291ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1292ebff3980SVlastimil Babka /* 1293ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1294ebff3980SVlastimil Babka * should be no need for compaction at all. 1295ebff3980SVlastimil Babka */ 1296ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1297ebff3980SVlastimil Babka alloc_flags)) 1298ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1299ebff3980SVlastimil Babka 13003957c776SMichal Hocko /* 13013e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 13023e7d3449SMel Gorman * This is because during migration, copies of pages need to be 13033e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 13043e7d3449SMel Gorman */ 1305ebff3980SVlastimil Babka watermark += (2UL << order); 1306ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 13073e7d3449SMel Gorman return COMPACT_SKIPPED; 13083e7d3449SMel Gorman 13093e7d3449SMel Gorman /* 13103e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 13113e7d3449SMel Gorman * low memory or external fragmentation 13123e7d3449SMel Gorman * 1313ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1314ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 13153e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 13163e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 13173e7d3449SMel Gorman * 13183e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 13193e7d3449SMel Gorman */ 13203e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 13213e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1322837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 13233e7d3449SMel Gorman 13243e7d3449SMel Gorman return COMPACT_CONTINUE; 13253e7d3449SMel Gorman } 13263e7d3449SMel Gorman 1327837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order, 1328837d026dSJoonsoo Kim int alloc_flags, int classzone_idx) 1329837d026dSJoonsoo Kim { 1330837d026dSJoonsoo Kim unsigned long ret; 1331837d026dSJoonsoo Kim 1332837d026dSJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx); 1333837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1334837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1335837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1336837d026dSJoonsoo Kim 1337837d026dSJoonsoo Kim return ret; 1338837d026dSJoonsoo Kim } 1339837d026dSJoonsoo Kim 1340748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1341748446bbSMel Gorman { 1342748446bbSMel Gorman int ret; 1343c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1344108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 13456d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1346e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1347748446bbSMel Gorman 1348ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1349ebff3980SVlastimil Babka cc->classzone_idx); 13503e7d3449SMel Gorman switch (ret) { 13513e7d3449SMel Gorman case COMPACT_PARTIAL: 13523e7d3449SMel Gorman case COMPACT_SKIPPED: 13533e7d3449SMel Gorman /* Compaction is likely to fail */ 13543e7d3449SMel Gorman return ret; 13553e7d3449SMel Gorman case COMPACT_CONTINUE: 13563e7d3449SMel Gorman /* Fall through to compaction */ 13573e7d3449SMel Gorman ; 13583e7d3449SMel Gorman } 13593e7d3449SMel Gorman 1360c89511abSMel Gorman /* 1361d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1362d3132e4bSVlastimil Babka * is about to be retried after being deferred. kswapd does not do 1363d3132e4bSVlastimil Babka * this reset as it'll reset the cached information when going to sleep. 1364d3132e4bSVlastimil Babka */ 1365d3132e4bSVlastimil Babka if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) 1366d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1367d3132e4bSVlastimil Babka 1368d3132e4bSVlastimil Babka /* 1369c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1370c89511abSMel Gorman * information on where the scanners should start but check that it 1371c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1372c89511abSMel Gorman */ 1373e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1374c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1375*623446e4SJoonsoo Kim if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { 1376*623446e4SJoonsoo Kim cc->free_pfn = round_down(end_pfn - 1, pageblock_nr_pages); 1377c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1378c89511abSMel Gorman } 1379*623446e4SJoonsoo Kim if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { 1380c89511abSMel Gorman cc->migrate_pfn = start_pfn; 138135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 138235979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1383c89511abSMel Gorman } 13841a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1385748446bbSMel Gorman 138616c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 138716c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 13880eb927c0SMel Gorman 1389748446bbSMel Gorman migrate_prep_local(); 1390748446bbSMel Gorman 13916d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 13926d7ce559SDavid Rientjes COMPACT_CONTINUE) { 13939d502c1cSMinchan Kim int err; 1394748446bbSMel Gorman 1395f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1396f9e35b3bSMel Gorman case ISOLATE_ABORT: 13972d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 13985733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1399e64c5237SShaohua Li cc->nr_migratepages = 0; 1400f9e35b3bSMel Gorman goto out; 1401f9e35b3bSMel Gorman case ISOLATE_NONE: 1402fdaf7f5cSVlastimil Babka /* 1403fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1404fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1405fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1406fdaf7f5cSVlastimil Babka */ 1407fdaf7f5cSVlastimil Babka goto check_drain; 1408f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1409f9e35b3bSMel Gorman ; 1410f9e35b3bSMel Gorman } 1411748446bbSMel Gorman 1412d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1413e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 14147b2a2d4aSMel Gorman MR_COMPACTION); 1415748446bbSMel Gorman 1416f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1417f8c9301fSVlastimil Babka &cc->migratepages); 1418748446bbSMel Gorman 1419f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1420f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 14219d502c1cSMinchan Kim if (err) { 14225733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 14237ed695e0SVlastimil Babka /* 14247ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 14257ed695e0SVlastimil Babka * and we want compact_finished() to detect it 14267ed695e0SVlastimil Babka */ 1427f2849aa0SVlastimil Babka if (err == -ENOMEM && !compact_scanners_met(cc)) { 14282d1e1041SVlastimil Babka ret = COMPACT_CONTENDED; 14294bf2bba3SDavid Rientjes goto out; 1430748446bbSMel Gorman } 14314bf2bba3SDavid Rientjes } 1432fdaf7f5cSVlastimil Babka 1433fdaf7f5cSVlastimil Babka check_drain: 1434fdaf7f5cSVlastimil Babka /* 1435fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1436fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1437fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1438fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1439fdaf7f5cSVlastimil Babka * would succeed. 1440fdaf7f5cSVlastimil Babka */ 14411a16718cSJoonsoo Kim if (cc->order > 0 && cc->last_migrated_pfn) { 1442fdaf7f5cSVlastimil Babka int cpu; 1443fdaf7f5cSVlastimil Babka unsigned long current_block_start = 1444fdaf7f5cSVlastimil Babka cc->migrate_pfn & ~((1UL << cc->order) - 1); 1445fdaf7f5cSVlastimil Babka 14461a16718cSJoonsoo Kim if (cc->last_migrated_pfn < current_block_start) { 1447fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1448fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1449fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1450fdaf7f5cSVlastimil Babka put_cpu(); 1451fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 14521a16718cSJoonsoo Kim cc->last_migrated_pfn = 0; 1453fdaf7f5cSVlastimil Babka } 1454fdaf7f5cSVlastimil Babka } 1455fdaf7f5cSVlastimil Babka 1456748446bbSMel Gorman } 1457748446bbSMel Gorman 1458f9e35b3bSMel Gorman out: 14596bace090SVlastimil Babka /* 14606bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 14616bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 14626bace090SVlastimil Babka */ 14636bace090SVlastimil Babka if (cc->nr_freepages > 0) { 14646bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 14656bace090SVlastimil Babka 14666bace090SVlastimil Babka cc->nr_freepages = 0; 14676bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 14686bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 14696bace090SVlastimil Babka free_pfn &= ~(pageblock_nr_pages-1); 14706bace090SVlastimil Babka /* 14716bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 14726bace090SVlastimil Babka * already reset to zone end in compact_finished() 14736bace090SVlastimil Babka */ 14746bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 14756bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 14766bace090SVlastimil Babka } 1477748446bbSMel Gorman 147816c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 147916c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 14800eb927c0SMel Gorman 14812d1e1041SVlastimil Babka if (ret == COMPACT_CONTENDED) 14822d1e1041SVlastimil Babka ret = COMPACT_PARTIAL; 14832d1e1041SVlastimil Babka 1484748446bbSMel Gorman return ret; 1485748446bbSMel Gorman } 148676ab0f53SMel Gorman 1487e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1488ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1489ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 149056de7263SMel Gorman { 1491e64c5237SShaohua Li unsigned long ret; 149256de7263SMel Gorman struct compact_control cc = { 149356de7263SMel Gorman .nr_freepages = 0, 149456de7263SMel Gorman .nr_migratepages = 0, 149556de7263SMel Gorman .order = order, 14966d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 149756de7263SMel Gorman .zone = zone, 1498e0b9daebSDavid Rientjes .mode = mode, 1499ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1500ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 150156de7263SMel Gorman }; 150256de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 150356de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 150456de7263SMel Gorman 1505e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1506e64c5237SShaohua Li 1507e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1508e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1509e64c5237SShaohua Li 1510e64c5237SShaohua Li *contended = cc.contended; 1511e64c5237SShaohua Li return ret; 151256de7263SMel Gorman } 151356de7263SMel Gorman 15145e771905SMel Gorman int sysctl_extfrag_threshold = 500; 15155e771905SMel Gorman 151656de7263SMel Gorman /** 151756de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 151856de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 15191a6d53a1SVlastimil Babka * @order: The order of the current allocation 15201a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 15211a6d53a1SVlastimil Babka * @ac: The context of current allocation 1522e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 15231f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 15241f9efdefSVlastimil Babka * need_resched() or lock contention 152556de7263SMel Gorman * 152656de7263SMel Gorman * This is the main entry point for direct page compaction. 152756de7263SMel Gorman */ 15281a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 15291a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 15301a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 153156de7263SMel Gorman { 153256de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 153356de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 153456de7263SMel Gorman struct zoneref *z; 153556de7263SMel Gorman struct zone *zone; 153653853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 15371f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 15381f9efdefSVlastimil Babka 15391f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 154056de7263SMel Gorman 15414ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1542c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 154353853e2dSVlastimil Babka return COMPACT_SKIPPED; 154456de7263SMel Gorman 1545837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1546837d026dSJoonsoo Kim 154756de7263SMel Gorman /* Compact each zone in the list */ 15481a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 15491a6d53a1SVlastimil Babka ac->nodemask) { 155056de7263SMel Gorman int status; 15511f9efdefSVlastimil Babka int zone_contended; 155256de7263SMel Gorman 155353853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 155453853e2dSVlastimil Babka continue; 155553853e2dSVlastimil Babka 1556e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 15571a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 15581a6d53a1SVlastimil Babka ac->classzone_idx); 155956de7263SMel Gorman rc = max(status, rc); 15601f9efdefSVlastimil Babka /* 15611f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 15621f9efdefSVlastimil Babka * to clear all_zones_contended. 15631f9efdefSVlastimil Babka */ 15641f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 156556de7263SMel Gorman 15663e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1567ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 15681a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 156953853e2dSVlastimil Babka /* 157053853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 157153853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 157253853e2dSVlastimil Babka * will repeat this with true if allocation indeed 157353853e2dSVlastimil Babka * succeeds in this zone. 157453853e2dSVlastimil Babka */ 157553853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 15761f9efdefSVlastimil Babka /* 15771f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 15781f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 15791f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 15801f9efdefSVlastimil Babka * however still fail so we better signal the 15811f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 15821f9efdefSVlastimil Babka * prevent the allocation attempt). 15831f9efdefSVlastimil Babka */ 15841f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 15851f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15861f9efdefSVlastimil Babka 15871f9efdefSVlastimil Babka goto break_loop; 15881f9efdefSVlastimil Babka } 15891f9efdefSVlastimil Babka 1590f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 159153853e2dSVlastimil Babka /* 159253853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 159353853e2dSVlastimil Babka * so we defer compaction there. If it ends up 159453853e2dSVlastimil Babka * succeeding after all, it will be reset. 159553853e2dSVlastimil Babka */ 159653853e2dSVlastimil Babka defer_compaction(zone, order); 159753853e2dSVlastimil Babka } 15981f9efdefSVlastimil Babka 15991f9efdefSVlastimil Babka /* 16001f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 16011f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 16021f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 16031f9efdefSVlastimil Babka * contention. 16041f9efdefSVlastimil Babka */ 16051f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 16061f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 16071f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 16081f9efdefSVlastimil Babka goto break_loop; 160956de7263SMel Gorman } 161056de7263SMel Gorman 16111f9efdefSVlastimil Babka continue; 16121f9efdefSVlastimil Babka break_loop: 16131f9efdefSVlastimil Babka /* 16141f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 16151f9efdefSVlastimil Babka * and assume they are not all lock contended. 16161f9efdefSVlastimil Babka */ 16171f9efdefSVlastimil Babka all_zones_contended = 0; 16181f9efdefSVlastimil Babka break; 16191f9efdefSVlastimil Babka } 16201f9efdefSVlastimil Babka 16211f9efdefSVlastimil Babka /* 16221f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 16231f9efdefSVlastimil Babka * zones that were tried were lock contended. 16241f9efdefSVlastimil Babka */ 16251f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 16261f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 16271f9efdefSVlastimil Babka 162856de7263SMel Gorman return rc; 162956de7263SMel Gorman } 163056de7263SMel Gorman 163156de7263SMel Gorman 163276ab0f53SMel Gorman /* Compact all zones within a node */ 16337103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 163476ab0f53SMel Gorman { 163576ab0f53SMel Gorman int zoneid; 163676ab0f53SMel Gorman struct zone *zone; 163776ab0f53SMel Gorman 163876ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 163976ab0f53SMel Gorman 164076ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 164176ab0f53SMel Gorman if (!populated_zone(zone)) 164276ab0f53SMel Gorman continue; 164376ab0f53SMel Gorman 16447be62de9SRik van Riel cc->nr_freepages = 0; 16457be62de9SRik van Riel cc->nr_migratepages = 0; 16467be62de9SRik van Riel cc->zone = zone; 16477be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 16487be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 164976ab0f53SMel Gorman 1650195b0c60SGioh Kim /* 1651195b0c60SGioh Kim * When called via /proc/sys/vm/compact_memory 1652195b0c60SGioh Kim * this makes sure we compact the whole zone regardless of 1653195b0c60SGioh Kim * cached scanner positions. 1654195b0c60SGioh Kim */ 165521c527a3SYaowei Bai if (is_via_compact_memory(cc->order)) 1656195b0c60SGioh Kim __reset_isolation_suitable(zone); 1657195b0c60SGioh Kim 165821c527a3SYaowei Bai if (is_via_compact_memory(cc->order) || 165921c527a3SYaowei Bai !compaction_deferred(zone, cc->order)) 16607be62de9SRik van Riel compact_zone(zone, cc); 166176ab0f53SMel Gorman 166275469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->freepages)); 166375469345SJoonsoo Kim VM_BUG_ON(!list_empty(&cc->migratepages)); 166475469345SJoonsoo Kim 166575469345SJoonsoo Kim if (is_via_compact_memory(cc->order)) 166675469345SJoonsoo Kim continue; 166775469345SJoonsoo Kim 1668de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1669de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1670de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1671aff62249SRik van Riel } 167276ab0f53SMel Gorman } 167376ab0f53SMel Gorman 16747103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 16757be62de9SRik van Riel { 16767be62de9SRik van Riel struct compact_control cc = { 16777be62de9SRik van Riel .order = order, 1678e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 16797be62de9SRik van Riel }; 16807be62de9SRik van Riel 16813a7200afSMel Gorman if (!order) 16823a7200afSMel Gorman return; 16833a7200afSMel Gorman 16847103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 16857be62de9SRik van Riel } 16867be62de9SRik van Riel 16877103f16dSAndrew Morton static void compact_node(int nid) 16887be62de9SRik van Riel { 16897be62de9SRik van Riel struct compact_control cc = { 16907be62de9SRik van Riel .order = -1, 1691e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 169291ca9186SDavid Rientjes .ignore_skip_hint = true, 16937be62de9SRik van Riel }; 16947be62de9SRik van Riel 16957103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 16967be62de9SRik van Riel } 16977be62de9SRik van Riel 169876ab0f53SMel Gorman /* Compact all nodes in the system */ 16997964c06dSJason Liu static void compact_nodes(void) 170076ab0f53SMel Gorman { 170176ab0f53SMel Gorman int nid; 170276ab0f53SMel Gorman 17038575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17048575ec29SHugh Dickins lru_add_drain_all(); 17058575ec29SHugh Dickins 170676ab0f53SMel Gorman for_each_online_node(nid) 170776ab0f53SMel Gorman compact_node(nid); 170876ab0f53SMel Gorman } 170976ab0f53SMel Gorman 171076ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 171176ab0f53SMel Gorman int sysctl_compact_memory; 171276ab0f53SMel Gorman 1713fec4eb2cSYaowei Bai /* 1714fec4eb2cSYaowei Bai * This is the entry point for compacting all nodes via 1715fec4eb2cSYaowei Bai * /proc/sys/vm/compact_memory 1716fec4eb2cSYaowei Bai */ 171776ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 171876ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 171976ab0f53SMel Gorman { 172076ab0f53SMel Gorman if (write) 17217964c06dSJason Liu compact_nodes(); 172276ab0f53SMel Gorman 172376ab0f53SMel Gorman return 0; 172476ab0f53SMel Gorman } 1725ed4a6d7fSMel Gorman 17265e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 17275e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 17285e771905SMel Gorman { 17295e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 17305e771905SMel Gorman 17315e771905SMel Gorman return 0; 17325e771905SMel Gorman } 17335e771905SMel Gorman 1734ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 173574e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 173610fbcf4cSKay Sievers struct device_attribute *attr, 1737ed4a6d7fSMel Gorman const char *buf, size_t count) 1738ed4a6d7fSMel Gorman { 17398575ec29SHugh Dickins int nid = dev->id; 17408575ec29SHugh Dickins 17418575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 17428575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 17438575ec29SHugh Dickins lru_add_drain_all(); 17448575ec29SHugh Dickins 17458575ec29SHugh Dickins compact_node(nid); 17468575ec29SHugh Dickins } 1747ed4a6d7fSMel Gorman 1748ed4a6d7fSMel Gorman return count; 1749ed4a6d7fSMel Gorman } 175010fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1751ed4a6d7fSMel Gorman 1752ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1753ed4a6d7fSMel Gorman { 175410fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1755ed4a6d7fSMel Gorman } 1756ed4a6d7fSMel Gorman 1757ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1758ed4a6d7fSMel Gorman { 175910fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1760ed4a6d7fSMel Gorman } 1761ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1762ff9543fdSMichal Nazarewicz 1763ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1764