1748446bbSMel Gorman /* 2748446bbSMel Gorman * linux/mm/compaction.c 3748446bbSMel Gorman * 4748446bbSMel Gorman * Memory compaction for the reduction of external fragmentation. Note that 5748446bbSMel Gorman * this heavily depends upon page migration to do all the real heavy 6748446bbSMel Gorman * lifting 7748446bbSMel Gorman * 8748446bbSMel Gorman * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> 9748446bbSMel Gorman */ 10748446bbSMel Gorman #include <linux/swap.h> 11748446bbSMel Gorman #include <linux/migrate.h> 12748446bbSMel Gorman #include <linux/compaction.h> 13748446bbSMel Gorman #include <linux/mm_inline.h> 14748446bbSMel Gorman #include <linux/backing-dev.h> 1576ab0f53SMel Gorman #include <linux/sysctl.h> 16ed4a6d7fSMel Gorman #include <linux/sysfs.h> 17bf6bddf1SRafael Aquini #include <linux/balloon_compaction.h> 18194159fbSMinchan Kim #include <linux/page-isolation.h> 19748446bbSMel Gorman #include "internal.h" 20748446bbSMel Gorman 21010fc29aSMinchan Kim #ifdef CONFIG_COMPACTION 22010fc29aSMinchan Kim static inline void count_compact_event(enum vm_event_item item) 23010fc29aSMinchan Kim { 24010fc29aSMinchan Kim count_vm_event(item); 25010fc29aSMinchan Kim } 26010fc29aSMinchan Kim 27010fc29aSMinchan Kim static inline void count_compact_events(enum vm_event_item item, long delta) 28010fc29aSMinchan Kim { 29010fc29aSMinchan Kim count_vm_events(item, delta); 30010fc29aSMinchan Kim } 31010fc29aSMinchan Kim #else 32010fc29aSMinchan Kim #define count_compact_event(item) do { } while (0) 33010fc29aSMinchan Kim #define count_compact_events(item, delta) do { } while (0) 34010fc29aSMinchan Kim #endif 35010fc29aSMinchan Kim 36ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 3716c4a097SJoonsoo Kim #ifdef CONFIG_TRACEPOINTS 3816c4a097SJoonsoo Kim static const char *const compaction_status_string[] = { 3916c4a097SJoonsoo Kim "deferred", 4016c4a097SJoonsoo Kim "skipped", 4116c4a097SJoonsoo Kim "continue", 4216c4a097SJoonsoo Kim "partial", 4316c4a097SJoonsoo Kim "complete", 44837d026dSJoonsoo Kim "no_suitable_page", 45837d026dSJoonsoo Kim "not_suitable_zone", 4616c4a097SJoonsoo Kim }; 4716c4a097SJoonsoo Kim #endif 48ff9543fdSMichal Nazarewicz 49b7aba698SMel Gorman #define CREATE_TRACE_POINTS 50b7aba698SMel Gorman #include <trace/events/compaction.h> 51b7aba698SMel Gorman 52748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 53748446bbSMel Gorman { 54748446bbSMel Gorman struct page *page, *next; 556bace090SVlastimil Babka unsigned long high_pfn = 0; 56748446bbSMel Gorman 57748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 586bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 59748446bbSMel Gorman list_del(&page->lru); 60748446bbSMel Gorman __free_page(page); 616bace090SVlastimil Babka if (pfn > high_pfn) 626bace090SVlastimil Babka high_pfn = pfn; 63748446bbSMel Gorman } 64748446bbSMel Gorman 656bace090SVlastimil Babka return high_pfn; 66748446bbSMel Gorman } 67748446bbSMel Gorman 68ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 69ff9543fdSMichal Nazarewicz { 70ff9543fdSMichal Nazarewicz struct page *page; 71ff9543fdSMichal Nazarewicz 72ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 73ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 74ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 75ff9543fdSMichal Nazarewicz } 76ff9543fdSMichal Nazarewicz } 77ff9543fdSMichal Nazarewicz 7847118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7947118af0SMichal Nazarewicz { 8047118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 8147118af0SMichal Nazarewicz } 8247118af0SMichal Nazarewicz 837d49d886SVlastimil Babka /* 847d49d886SVlastimil Babka * Check that the whole (or subset of) a pageblock given by the interval of 857d49d886SVlastimil Babka * [start_pfn, end_pfn) is valid and within the same zone, before scanning it 867d49d886SVlastimil Babka * with the migration of free compaction scanner. The scanners then need to 877d49d886SVlastimil Babka * use only pfn_valid_within() check for arches that allow holes within 887d49d886SVlastimil Babka * pageblocks. 897d49d886SVlastimil Babka * 907d49d886SVlastimil Babka * Return struct page pointer of start_pfn, or NULL if checks were not passed. 917d49d886SVlastimil Babka * 927d49d886SVlastimil Babka * It's possible on some configurations to have a setup like node0 node1 node0 937d49d886SVlastimil Babka * i.e. it's possible that all pages within a zones range of pages do not 947d49d886SVlastimil Babka * belong to a single zone. We assume that a border between node0 and node1 957d49d886SVlastimil Babka * can occur within a single pageblock, but not a node0 node1 node0 967d49d886SVlastimil Babka * interleaving within a single pageblock. It is therefore sufficient to check 977d49d886SVlastimil Babka * the first and last page of a pageblock and avoid checking each individual 987d49d886SVlastimil Babka * page in a pageblock. 997d49d886SVlastimil Babka */ 1007d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn, 1017d49d886SVlastimil Babka unsigned long end_pfn, struct zone *zone) 1027d49d886SVlastimil Babka { 1037d49d886SVlastimil Babka struct page *start_page; 1047d49d886SVlastimil Babka struct page *end_page; 1057d49d886SVlastimil Babka 1067d49d886SVlastimil Babka /* end_pfn is one past the range we are checking */ 1077d49d886SVlastimil Babka end_pfn--; 1087d49d886SVlastimil Babka 1097d49d886SVlastimil Babka if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) 1107d49d886SVlastimil Babka return NULL; 1117d49d886SVlastimil Babka 1127d49d886SVlastimil Babka start_page = pfn_to_page(start_pfn); 1137d49d886SVlastimil Babka 1147d49d886SVlastimil Babka if (page_zone(start_page) != zone) 1157d49d886SVlastimil Babka return NULL; 1167d49d886SVlastimil Babka 1177d49d886SVlastimil Babka end_page = pfn_to_page(end_pfn); 1187d49d886SVlastimil Babka 1197d49d886SVlastimil Babka /* This gives a shorter code than deriving page_zone(end_page) */ 1207d49d886SVlastimil Babka if (page_zone_id(start_page) != page_zone_id(end_page)) 1217d49d886SVlastimil Babka return NULL; 1227d49d886SVlastimil Babka 1237d49d886SVlastimil Babka return start_page; 1247d49d886SVlastimil Babka } 1257d49d886SVlastimil Babka 126bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 12724e2716fSJoonsoo Kim 12824e2716fSJoonsoo Kim /* Do not skip compaction more than 64 times */ 12924e2716fSJoonsoo Kim #define COMPACT_MAX_DEFER_SHIFT 6 13024e2716fSJoonsoo Kim 13124e2716fSJoonsoo Kim /* 13224e2716fSJoonsoo Kim * Compaction is deferred when compaction fails to result in a page 13324e2716fSJoonsoo Kim * allocation success. 1 << compact_defer_limit compactions are skipped up 13424e2716fSJoonsoo Kim * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT 13524e2716fSJoonsoo Kim */ 13624e2716fSJoonsoo Kim void defer_compaction(struct zone *zone, int order) 13724e2716fSJoonsoo Kim { 13824e2716fSJoonsoo Kim zone->compact_considered = 0; 13924e2716fSJoonsoo Kim zone->compact_defer_shift++; 14024e2716fSJoonsoo Kim 14124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 14224e2716fSJoonsoo Kim zone->compact_order_failed = order; 14324e2716fSJoonsoo Kim 14424e2716fSJoonsoo Kim if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) 14524e2716fSJoonsoo Kim zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; 14624e2716fSJoonsoo Kim 14724e2716fSJoonsoo Kim trace_mm_compaction_defer_compaction(zone, order); 14824e2716fSJoonsoo Kim } 14924e2716fSJoonsoo Kim 15024e2716fSJoonsoo Kim /* Returns true if compaction should be skipped this time */ 15124e2716fSJoonsoo Kim bool compaction_deferred(struct zone *zone, int order) 15224e2716fSJoonsoo Kim { 15324e2716fSJoonsoo Kim unsigned long defer_limit = 1UL << zone->compact_defer_shift; 15424e2716fSJoonsoo Kim 15524e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 15624e2716fSJoonsoo Kim return false; 15724e2716fSJoonsoo Kim 15824e2716fSJoonsoo Kim /* Avoid possible overflow */ 15924e2716fSJoonsoo Kim if (++zone->compact_considered > defer_limit) 16024e2716fSJoonsoo Kim zone->compact_considered = defer_limit; 16124e2716fSJoonsoo Kim 16224e2716fSJoonsoo Kim if (zone->compact_considered >= defer_limit) 16324e2716fSJoonsoo Kim return false; 16424e2716fSJoonsoo Kim 16524e2716fSJoonsoo Kim trace_mm_compaction_deferred(zone, order); 16624e2716fSJoonsoo Kim 16724e2716fSJoonsoo Kim return true; 16824e2716fSJoonsoo Kim } 16924e2716fSJoonsoo Kim 17024e2716fSJoonsoo Kim /* 17124e2716fSJoonsoo Kim * Update defer tracking counters after successful compaction of given order, 17224e2716fSJoonsoo Kim * which means an allocation either succeeded (alloc_success == true) or is 17324e2716fSJoonsoo Kim * expected to succeed. 17424e2716fSJoonsoo Kim */ 17524e2716fSJoonsoo Kim void compaction_defer_reset(struct zone *zone, int order, 17624e2716fSJoonsoo Kim bool alloc_success) 17724e2716fSJoonsoo Kim { 17824e2716fSJoonsoo Kim if (alloc_success) { 17924e2716fSJoonsoo Kim zone->compact_considered = 0; 18024e2716fSJoonsoo Kim zone->compact_defer_shift = 0; 18124e2716fSJoonsoo Kim } 18224e2716fSJoonsoo Kim if (order >= zone->compact_order_failed) 18324e2716fSJoonsoo Kim zone->compact_order_failed = order + 1; 18424e2716fSJoonsoo Kim 18524e2716fSJoonsoo Kim trace_mm_compaction_defer_reset(zone, order); 18624e2716fSJoonsoo Kim } 18724e2716fSJoonsoo Kim 18824e2716fSJoonsoo Kim /* Returns true if restarting compaction after many failures */ 18924e2716fSJoonsoo Kim bool compaction_restarting(struct zone *zone, int order) 19024e2716fSJoonsoo Kim { 19124e2716fSJoonsoo Kim if (order < zone->compact_order_failed) 19224e2716fSJoonsoo Kim return false; 19324e2716fSJoonsoo Kim 19424e2716fSJoonsoo Kim return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && 19524e2716fSJoonsoo Kim zone->compact_considered >= 1UL << zone->compact_defer_shift; 19624e2716fSJoonsoo Kim } 19724e2716fSJoonsoo Kim 198bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 199bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 200bb13ffebSMel Gorman struct page *page) 201bb13ffebSMel Gorman { 202bb13ffebSMel Gorman if (cc->ignore_skip_hint) 203bb13ffebSMel Gorman return true; 204bb13ffebSMel Gorman 205bb13ffebSMel Gorman return !get_pageblock_skip(page); 206bb13ffebSMel Gorman } 207bb13ffebSMel Gorman 208bb13ffebSMel Gorman /* 209bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 210bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 211bb13ffebSMel Gorman * meet. 212bb13ffebSMel Gorman */ 21362997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 214bb13ffebSMel Gorman { 215bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 216108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 217bb13ffebSMel Gorman unsigned long pfn; 218bb13ffebSMel Gorman 21935979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = start_pfn; 22035979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = start_pfn; 221c89511abSMel Gorman zone->compact_cached_free_pfn = end_pfn; 22262997027SMel Gorman zone->compact_blockskip_flush = false; 223bb13ffebSMel Gorman 224bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 225bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 226bb13ffebSMel Gorman struct page *page; 227bb13ffebSMel Gorman 228bb13ffebSMel Gorman cond_resched(); 229bb13ffebSMel Gorman 230bb13ffebSMel Gorman if (!pfn_valid(pfn)) 231bb13ffebSMel Gorman continue; 232bb13ffebSMel Gorman 233bb13ffebSMel Gorman page = pfn_to_page(pfn); 234bb13ffebSMel Gorman if (zone != page_zone(page)) 235bb13ffebSMel Gorman continue; 236bb13ffebSMel Gorman 237bb13ffebSMel Gorman clear_pageblock_skip(page); 238bb13ffebSMel Gorman } 239bb13ffebSMel Gorman } 240bb13ffebSMel Gorman 24162997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 24262997027SMel Gorman { 24362997027SMel Gorman int zoneid; 24462997027SMel Gorman 24562997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 24662997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 24762997027SMel Gorman if (!populated_zone(zone)) 24862997027SMel Gorman continue; 24962997027SMel Gorman 25062997027SMel Gorman /* Only flush if a full compaction finished recently */ 25162997027SMel Gorman if (zone->compact_blockskip_flush) 25262997027SMel Gorman __reset_isolation_suitable(zone); 25362997027SMel Gorman } 25462997027SMel Gorman } 25562997027SMel Gorman 256bb13ffebSMel Gorman /* 257bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 25862997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 259bb13ffebSMel Gorman */ 260c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 261c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 262edc2ca61SVlastimil Babka bool migrate_scanner) 263bb13ffebSMel Gorman { 264c89511abSMel Gorman struct zone *zone = cc->zone; 26535979ef3SDavid Rientjes unsigned long pfn; 2666815bf3fSJoonsoo Kim 2676815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 2686815bf3fSJoonsoo Kim return; 2696815bf3fSJoonsoo Kim 270bb13ffebSMel Gorman if (!page) 271bb13ffebSMel Gorman return; 272bb13ffebSMel Gorman 27335979ef3SDavid Rientjes if (nr_isolated) 27435979ef3SDavid Rientjes return; 27535979ef3SDavid Rientjes 276bb13ffebSMel Gorman set_pageblock_skip(page); 277c89511abSMel Gorman 27835979ef3SDavid Rientjes pfn = page_to_pfn(page); 27935979ef3SDavid Rientjes 28035979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 281c89511abSMel Gorman if (migrate_scanner) { 28235979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 28335979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 284e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 285e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 28635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 287c89511abSMel Gorman } else { 28835979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 289c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 290c89511abSMel Gorman } 291c89511abSMel Gorman } 292bb13ffebSMel Gorman #else 293bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 294bb13ffebSMel Gorman struct page *page) 295bb13ffebSMel Gorman { 296bb13ffebSMel Gorman return true; 297bb13ffebSMel Gorman } 298bb13ffebSMel Gorman 299c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 300c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 301edc2ca61SVlastimil Babka bool migrate_scanner) 302bb13ffebSMel Gorman { 303bb13ffebSMel Gorman } 304bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 305bb13ffebSMel Gorman 3061f9efdefSVlastimil Babka /* 3078b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 3088b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 3098b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 3108b44d279SVlastimil Babka * 3118b44d279SVlastimil Babka * Returns true if the lock is held 3128b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 3131f9efdefSVlastimil Babka */ 3148b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 3158b44d279SVlastimil Babka struct compact_control *cc) 3168b44d279SVlastimil Babka { 3178b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3188b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 3198b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 3208b44d279SVlastimil Babka return false; 3218b44d279SVlastimil Babka } 3228b44d279SVlastimil Babka } else { 3238b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 3248b44d279SVlastimil Babka } 3251f9efdefSVlastimil Babka 3268b44d279SVlastimil Babka return true; 3272a1402aaSMel Gorman } 3282a1402aaSMel Gorman 32985aa125fSMichal Nazarewicz /* 330c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 3318b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 3328b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 3338b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 3348b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 3358b44d279SVlastimil Babka * aborts. Sync compaction schedules. 3368b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 3378b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 338c67fe375SMel Gorman * 3398b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 3408b44d279SVlastimil Babka * async compaction due to need_resched() 3418b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 3428b44d279SVlastimil Babka * scheduled) 343c67fe375SMel Gorman */ 3448b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 3458b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 346c67fe375SMel Gorman { 3478b44d279SVlastimil Babka if (*locked) { 3488b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 3498b44d279SVlastimil Babka *locked = false; 350c67fe375SMel Gorman } 351c67fe375SMel Gorman 3528b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 3538b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3548b44d279SVlastimil Babka return true; 3558b44d279SVlastimil Babka } 3568b44d279SVlastimil Babka 3578b44d279SVlastimil Babka if (need_resched()) { 358e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 3598b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 3608b44d279SVlastimil Babka return true; 361c67fe375SMel Gorman } 362c67fe375SMel Gorman cond_resched(); 363c67fe375SMel Gorman } 364c67fe375SMel Gorman 3658b44d279SVlastimil Babka return false; 366c67fe375SMel Gorman } 367c67fe375SMel Gorman 368be976572SVlastimil Babka /* 369be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 370be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 3718b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 372be976572SVlastimil Babka * is used where no lock is concerned. 373be976572SVlastimil Babka * 374be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 375be976572SVlastimil Babka * Returns true when async compaction should abort. 376be976572SVlastimil Babka */ 377be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 378be976572SVlastimil Babka { 379be976572SVlastimil Babka /* async compaction aborts if contended */ 380be976572SVlastimil Babka if (need_resched()) { 381be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3821f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 383be976572SVlastimil Babka return true; 384be976572SVlastimil Babka } 385be976572SVlastimil Babka 386be976572SVlastimil Babka cond_resched(); 387be976572SVlastimil Babka } 388be976572SVlastimil Babka 389be976572SVlastimil Babka return false; 390be976572SVlastimil Babka } 391be976572SVlastimil Babka 392f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */ 393f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page) 394f40d1e42SMel Gorman { 3957d348b9eSJoonsoo Kim /* If the page is a large free page, then disallow migration */ 39699c0fd5eSVlastimil Babka if (PageBuddy(page)) { 39799c0fd5eSVlastimil Babka /* 39899c0fd5eSVlastimil Babka * We are checking page_order without zone->lock taken. But 39999c0fd5eSVlastimil Babka * the only small danger is that we skip a potentially suitable 40099c0fd5eSVlastimil Babka * pageblock, so it's not worth to check order for valid range. 40199c0fd5eSVlastimil Babka */ 40299c0fd5eSVlastimil Babka if (page_order_unsafe(page) >= pageblock_order) 4037d348b9eSJoonsoo Kim return false; 40499c0fd5eSVlastimil Babka } 405f40d1e42SMel Gorman 406f40d1e42SMel Gorman /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 4077d348b9eSJoonsoo Kim if (migrate_async_suitable(get_pageblock_migratetype(page))) 408f40d1e42SMel Gorman return true; 409f40d1e42SMel Gorman 410f40d1e42SMel Gorman /* Otherwise skip the block */ 411f40d1e42SMel Gorman return false; 412f40d1e42SMel Gorman } 413f40d1e42SMel Gorman 414c67fe375SMel Gorman /* 4159e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 4169e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 4179e4be470SJerome Marchand * (even though it may still end up isolating some pages). 41885aa125fSMichal Nazarewicz */ 419f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 420e14c720eSVlastimil Babka unsigned long *start_pfn, 42185aa125fSMichal Nazarewicz unsigned long end_pfn, 42285aa125fSMichal Nazarewicz struct list_head *freelist, 42385aa125fSMichal Nazarewicz bool strict) 424748446bbSMel Gorman { 425b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 426bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 427b8b2d825SXiubo Li unsigned long flags = 0; 428f40d1e42SMel Gorman bool locked = false; 429e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 430748446bbSMel Gorman 431748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 432748446bbSMel Gorman 433f40d1e42SMel Gorman /* Isolate free pages. */ 434748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 435748446bbSMel Gorman int isolated, i; 436748446bbSMel Gorman struct page *page = cursor; 437748446bbSMel Gorman 4388b44d279SVlastimil Babka /* 4398b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 4408b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 4418b44d279SVlastimil Babka * pending or async compaction detects need_resched() 4428b44d279SVlastimil Babka */ 4438b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 4448b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 4458b44d279SVlastimil Babka &locked, cc)) 4468b44d279SVlastimil Babka break; 4478b44d279SVlastimil Babka 448b7aba698SMel Gorman nr_scanned++; 449f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 4502af120bcSLaura Abbott goto isolate_fail; 4512af120bcSLaura Abbott 452bb13ffebSMel Gorman if (!valid_page) 453bb13ffebSMel Gorman valid_page = page; 454f40d1e42SMel Gorman if (!PageBuddy(page)) 4552af120bcSLaura Abbott goto isolate_fail; 456f40d1e42SMel Gorman 457f40d1e42SMel Gorman /* 45869b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 45969b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 46069b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 46169b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 46269b7189fSVlastimil Babka * recheck as well. 46369b7189fSVlastimil Babka */ 46469b7189fSVlastimil Babka if (!locked) { 46569b7189fSVlastimil Babka /* 466f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 467f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 468f40d1e42SMel Gorman * heavily contended if there are parallel allocations 469f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 470f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 471f40d1e42SMel Gorman * possible. 472f40d1e42SMel Gorman */ 4738b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4748b44d279SVlastimil Babka &flags, cc); 475f40d1e42SMel Gorman if (!locked) 476f40d1e42SMel Gorman break; 477f40d1e42SMel Gorman 478f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 479f40d1e42SMel Gorman if (!PageBuddy(page)) 4802af120bcSLaura Abbott goto isolate_fail; 48169b7189fSVlastimil Babka } 482748446bbSMel Gorman 483748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 484748446bbSMel Gorman isolated = split_free_page(page); 485748446bbSMel Gorman total_isolated += isolated; 486748446bbSMel Gorman for (i = 0; i < isolated; i++) { 487748446bbSMel Gorman list_add(&page->lru, freelist); 488748446bbSMel Gorman page++; 489748446bbSMel Gorman } 490748446bbSMel Gorman 491748446bbSMel Gorman /* If a page was split, advance to the end of it */ 492748446bbSMel Gorman if (isolated) { 493932ff6bbSJoonsoo Kim cc->nr_freepages += isolated; 494932ff6bbSJoonsoo Kim if (!strict && 495932ff6bbSJoonsoo Kim cc->nr_migratepages <= cc->nr_freepages) { 496932ff6bbSJoonsoo Kim blockpfn += isolated; 497932ff6bbSJoonsoo Kim break; 498932ff6bbSJoonsoo Kim } 499932ff6bbSJoonsoo Kim 500748446bbSMel Gorman blockpfn += isolated - 1; 501748446bbSMel Gorman cursor += isolated - 1; 5022af120bcSLaura Abbott continue; 503748446bbSMel Gorman } 5042af120bcSLaura Abbott 5052af120bcSLaura Abbott isolate_fail: 5062af120bcSLaura Abbott if (strict) 5072af120bcSLaura Abbott break; 5082af120bcSLaura Abbott else 5092af120bcSLaura Abbott continue; 5102af120bcSLaura Abbott 511748446bbSMel Gorman } 512748446bbSMel Gorman 513e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, 514e34d85f0SJoonsoo Kim nr_scanned, total_isolated); 515e34d85f0SJoonsoo Kim 516e14c720eSVlastimil Babka /* Record how far we have got within the block */ 517e14c720eSVlastimil Babka *start_pfn = blockpfn; 518e14c720eSVlastimil Babka 519f40d1e42SMel Gorman /* 520f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 521f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 522f40d1e42SMel Gorman * returned and CMA will fail. 523f40d1e42SMel Gorman */ 5242af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 525f40d1e42SMel Gorman total_isolated = 0; 526f40d1e42SMel Gorman 527f40d1e42SMel Gorman if (locked) 528f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 529f40d1e42SMel Gorman 530bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 531bb13ffebSMel Gorman if (blockpfn == end_pfn) 532edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 533bb13ffebSMel Gorman 534010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 535397487dbSMel Gorman if (total_isolated) 536010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 537748446bbSMel Gorman return total_isolated; 538748446bbSMel Gorman } 539748446bbSMel Gorman 54085aa125fSMichal Nazarewicz /** 54185aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 54285aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 54385aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 54485aa125fSMichal Nazarewicz * 54585aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 54685aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 54785aa125fSMichal Nazarewicz * undo its actions and return zero. 54885aa125fSMichal Nazarewicz * 54985aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 55085aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 55185aa125fSMichal Nazarewicz * a free page). 55285aa125fSMichal Nazarewicz */ 553ff9543fdSMichal Nazarewicz unsigned long 554bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 555bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 55685aa125fSMichal Nazarewicz { 557f40d1e42SMel Gorman unsigned long isolated, pfn, block_end_pfn; 55885aa125fSMichal Nazarewicz LIST_HEAD(freelist); 55985aa125fSMichal Nazarewicz 5607d49d886SVlastimil Babka pfn = start_pfn; 56185aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 5627d49d886SVlastimil Babka 5637d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 5647d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 565e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 566e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 5677d49d886SVlastimil Babka 56885aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 56985aa125fSMichal Nazarewicz 57058420016SJoonsoo Kim /* 57158420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 57258420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 57358420016SJoonsoo Kim * scanning range to right one. 57458420016SJoonsoo Kim */ 57558420016SJoonsoo Kim if (pfn >= block_end_pfn) { 57658420016SJoonsoo Kim block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 57758420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 57858420016SJoonsoo Kim } 57958420016SJoonsoo Kim 5807d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 5817d49d886SVlastimil Babka break; 5827d49d886SVlastimil Babka 583e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 584e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 58585aa125fSMichal Nazarewicz 58685aa125fSMichal Nazarewicz /* 58785aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 58885aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 58985aa125fSMichal Nazarewicz * non-free pages). 59085aa125fSMichal Nazarewicz */ 59185aa125fSMichal Nazarewicz if (!isolated) 59285aa125fSMichal Nazarewicz break; 59385aa125fSMichal Nazarewicz 59485aa125fSMichal Nazarewicz /* 59585aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 59685aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 59785aa125fSMichal Nazarewicz * page may span two pageblocks). 59885aa125fSMichal Nazarewicz */ 59985aa125fSMichal Nazarewicz } 60085aa125fSMichal Nazarewicz 60185aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 60285aa125fSMichal Nazarewicz map_pages(&freelist); 60385aa125fSMichal Nazarewicz 60485aa125fSMichal Nazarewicz if (pfn < end_pfn) { 60585aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 60685aa125fSMichal Nazarewicz release_freepages(&freelist); 60785aa125fSMichal Nazarewicz return 0; 60885aa125fSMichal Nazarewicz } 60985aa125fSMichal Nazarewicz 61085aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 61185aa125fSMichal Nazarewicz return pfn; 61285aa125fSMichal Nazarewicz } 61385aa125fSMichal Nazarewicz 614748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 615edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 616748446bbSMel Gorman { 617748446bbSMel Gorman struct page *page; 618b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 619748446bbSMel Gorman 620edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 621edc2ca61SVlastimil Babka return; 622edc2ca61SVlastimil Babka 623b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 624b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 625748446bbSMel Gorman 626c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 627c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 628c67fe375SMel Gorman } 629748446bbSMel Gorman 630748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 631748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 632748446bbSMel Gorman { 633bc693045SMinchan Kim unsigned long active, inactive, isolated; 634748446bbSMel Gorman 635748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 636748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 637bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 638bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 639748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 640748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 641748446bbSMel Gorman 642bc693045SMinchan Kim return isolated > (inactive + active) / 2; 643748446bbSMel Gorman } 644748446bbSMel Gorman 6452fe86e00SMichal Nazarewicz /** 646edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 647edc2ca61SVlastimil Babka * a single pageblock 6482fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 649edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 650edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 651edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 6522fe86e00SMichal Nazarewicz * 6532fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 654edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 655edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 656edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 657edc2ca61SVlastimil Babka * than end_pfn). 6582fe86e00SMichal Nazarewicz * 659edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 660edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 661edc2ca61SVlastimil Babka * is neither read nor updated. 662748446bbSMel Gorman */ 663edc2ca61SVlastimil Babka static unsigned long 664edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 665edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 666748446bbSMel Gorman { 667edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 668b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 669748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 670fa9add64SHugh Dickins struct lruvec *lruvec; 671b8b2d825SXiubo Li unsigned long flags = 0; 6722a1402aaSMel Gorman bool locked = false; 673bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 674e34d85f0SJoonsoo Kim unsigned long start_pfn = low_pfn; 675748446bbSMel Gorman 676748446bbSMel Gorman /* 677748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 678748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 679748446bbSMel Gorman * delay for some time until fewer pages are isolated 680748446bbSMel Gorman */ 681748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 682f9e35b3bSMel Gorman /* async migration should just abort */ 683e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6842fe86e00SMichal Nazarewicz return 0; 685f9e35b3bSMel Gorman 686748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 687748446bbSMel Gorman 688748446bbSMel Gorman if (fatal_signal_pending(current)) 6892fe86e00SMichal Nazarewicz return 0; 690748446bbSMel Gorman } 691748446bbSMel Gorman 692be976572SVlastimil Babka if (compact_should_abort(cc)) 693aeef4b83SDavid Rientjes return 0; 694aeef4b83SDavid Rientjes 695748446bbSMel Gorman /* Time to isolate some pages for migration */ 696748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 6978b44d279SVlastimil Babka /* 6988b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 6998b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 7008b44d279SVlastimil Babka * if contended. 7018b44d279SVlastimil Babka */ 7028b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 7038b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 7048b44d279SVlastimil Babka &locked, cc)) 7058b44d279SVlastimil Babka break; 706b2eef8c0SAndrea Arcangeli 707748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 708748446bbSMel Gorman continue; 709b7aba698SMel Gorman nr_scanned++; 710748446bbSMel Gorman 711748446bbSMel Gorman page = pfn_to_page(low_pfn); 712dc908600SMel Gorman 713bb13ffebSMel Gorman if (!valid_page) 714bb13ffebSMel Gorman valid_page = page; 715bb13ffebSMel Gorman 716c122b208SJoonsoo Kim /* 71799c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 71899c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 71999c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 72099c0fd5eSVlastimil Babka * potential isolation targets. 7216c14466cSMel Gorman */ 72299c0fd5eSVlastimil Babka if (PageBuddy(page)) { 72399c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 72499c0fd5eSVlastimil Babka 72599c0fd5eSVlastimil Babka /* 72699c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 72799c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 72899c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 72999c0fd5eSVlastimil Babka */ 73099c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 73199c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 732748446bbSMel Gorman continue; 73399c0fd5eSVlastimil Babka } 734748446bbSMel Gorman 7359927af74SMel Gorman /* 736bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 737bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 738bf6bddf1SRafael Aquini * Skip any other type of page 739bf6bddf1SRafael Aquini */ 740bf6bddf1SRafael Aquini if (!PageLRU(page)) { 741bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 742d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 743bf6bddf1SRafael Aquini /* Successfully isolated */ 744b6c75016SJoonsoo Kim goto isolate_success; 745bf6bddf1SRafael Aquini } 746bf6bddf1SRafael Aquini } 747bc835011SAndrea Arcangeli continue; 748bf6bddf1SRafael Aquini } 749bc835011SAndrea Arcangeli 750bc835011SAndrea Arcangeli /* 7512a1402aaSMel Gorman * PageLRU is set. lru_lock normally excludes isolation 7522a1402aaSMel Gorman * splitting and collapsing (collapsing has already happened 7532a1402aaSMel Gorman * if PageLRU is set) but the lock is not necessarily taken 7542a1402aaSMel Gorman * here and it is wasteful to take it just to check transhuge. 7552a1402aaSMel Gorman * Check TransHuge without lock and skip the whole pageblock if 7562a1402aaSMel Gorman * it's either a transhuge or hugetlbfs page, as calling 7572a1402aaSMel Gorman * compound_order() without preventing THP from splitting the 7582a1402aaSMel Gorman * page underneath us may return surprising results. 759bc835011SAndrea Arcangeli */ 760bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 7612a1402aaSMel Gorman if (!locked) 762edc2ca61SVlastimil Babka low_pfn = ALIGN(low_pfn + 1, 763edc2ca61SVlastimil Babka pageblock_nr_pages) - 1; 764edc2ca61SVlastimil Babka else 7652a1402aaSMel Gorman low_pfn += (1 << compound_order(page)) - 1; 766edc2ca61SVlastimil Babka 7672a1402aaSMel Gorman continue; 7682a1402aaSMel Gorman } 7692a1402aaSMel Gorman 770119d6d59SDavid Rientjes /* 771119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 772119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 773119d6d59SDavid Rientjes * admittedly racy check. 774119d6d59SDavid Rientjes */ 775119d6d59SDavid Rientjes if (!page_mapping(page) && 776119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 777119d6d59SDavid Rientjes continue; 778119d6d59SDavid Rientjes 77969b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 78069b7189fSVlastimil Babka if (!locked) { 7818b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7828b44d279SVlastimil Babka &flags, cc); 7838b44d279SVlastimil Babka if (!locked) 7842a1402aaSMel Gorman break; 7852a1402aaSMel Gorman 7862a1402aaSMel Gorman /* Recheck PageLRU and PageTransHuge under lock */ 7872a1402aaSMel Gorman if (!PageLRU(page)) 7882a1402aaSMel Gorman continue; 7892a1402aaSMel Gorman if (PageTransHuge(page)) { 790bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 791bc835011SAndrea Arcangeli continue; 792bc835011SAndrea Arcangeli } 79369b7189fSVlastimil Babka } 794bc835011SAndrea Arcangeli 795fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 796fa9add64SHugh Dickins 797748446bbSMel Gorman /* Try isolate the page */ 798edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 799748446bbSMel Gorman continue; 800748446bbSMel Gorman 801309381feSSasha Levin VM_BUG_ON_PAGE(PageTransCompound(page), page); 802bc835011SAndrea Arcangeli 803748446bbSMel Gorman /* Successfully isolated */ 804fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 805b6c75016SJoonsoo Kim 806b6c75016SJoonsoo Kim isolate_success: 807748446bbSMel Gorman list_add(&page->lru, migratelist); 808748446bbSMel Gorman cc->nr_migratepages++; 809b7aba698SMel Gorman nr_isolated++; 810748446bbSMel Gorman 811748446bbSMel Gorman /* Avoid isolating too much */ 81231b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 81331b8384aSHillf Danton ++low_pfn; 814748446bbSMel Gorman break; 815748446bbSMel Gorman } 81631b8384aSHillf Danton } 817748446bbSMel Gorman 81899c0fd5eSVlastimil Babka /* 81999c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 82099c0fd5eSVlastimil Babka * the range to be scanned. 82199c0fd5eSVlastimil Babka */ 82299c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 82399c0fd5eSVlastimil Babka low_pfn = end_pfn; 82499c0fd5eSVlastimil Babka 825c67fe375SMel Gorman if (locked) 826c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 827748446bbSMel Gorman 82850b5b094SVlastimil Babka /* 82950b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 83050b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 83150b5b094SVlastimil Babka */ 83235979ef3SDavid Rientjes if (low_pfn == end_pfn) 833edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 834bb13ffebSMel Gorman 835e34d85f0SJoonsoo Kim trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, 836e34d85f0SJoonsoo Kim nr_scanned, nr_isolated); 837b7aba698SMel Gorman 838010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 839397487dbSMel Gorman if (nr_isolated) 840010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 841397487dbSMel Gorman 8422fe86e00SMichal Nazarewicz return low_pfn; 8432fe86e00SMichal Nazarewicz } 8442fe86e00SMichal Nazarewicz 845edc2ca61SVlastimil Babka /** 846edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 847edc2ca61SVlastimil Babka * @cc: Compaction control structure. 848edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 849edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 850edc2ca61SVlastimil Babka * 851edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 852edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 853edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 854edc2ca61SVlastimil Babka */ 855edc2ca61SVlastimil Babka unsigned long 856edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 857edc2ca61SVlastimil Babka unsigned long end_pfn) 858edc2ca61SVlastimil Babka { 859edc2ca61SVlastimil Babka unsigned long pfn, block_end_pfn; 860edc2ca61SVlastimil Babka 861edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 862edc2ca61SVlastimil Babka pfn = start_pfn; 863edc2ca61SVlastimil Babka block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 864edc2ca61SVlastimil Babka 865edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 866edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 867edc2ca61SVlastimil Babka 868edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 869edc2ca61SVlastimil Babka 8707d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 871edc2ca61SVlastimil Babka continue; 872edc2ca61SVlastimil Babka 873edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 874edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 875edc2ca61SVlastimil Babka 876edc2ca61SVlastimil Babka /* 877edc2ca61SVlastimil Babka * In case of fatal failure, release everything that might 878edc2ca61SVlastimil Babka * have been isolated in the previous iteration, and signal 879edc2ca61SVlastimil Babka * the failure back to caller. 880edc2ca61SVlastimil Babka */ 881edc2ca61SVlastimil Babka if (!pfn) { 882edc2ca61SVlastimil Babka putback_movable_pages(&cc->migratepages); 883edc2ca61SVlastimil Babka cc->nr_migratepages = 0; 884edc2ca61SVlastimil Babka break; 885edc2ca61SVlastimil Babka } 8866ea41c0cSJoonsoo Kim 8876ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8886ea41c0cSJoonsoo Kim break; 889edc2ca61SVlastimil Babka } 890edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 891edc2ca61SVlastimil Babka 892edc2ca61SVlastimil Babka return pfn; 893edc2ca61SVlastimil Babka } 894edc2ca61SVlastimil Babka 895ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 896ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 897ff9543fdSMichal Nazarewicz /* 898ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 899ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 900ff9543fdSMichal Nazarewicz */ 901edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 902ff9543fdSMichal Nazarewicz { 903edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 904ff9543fdSMichal Nazarewicz struct page *page; 905c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 906e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 907c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 908c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 909ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 9102fe86e00SMichal Nazarewicz 911ff9543fdSMichal Nazarewicz /* 912ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 91349e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 914e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 915e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 916c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 917c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 918c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 91949e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 92049e068f0SVlastimil Babka * is using. 921ff9543fdSMichal Nazarewicz */ 922e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 923c96b9e50SVlastimil Babka block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); 924c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 925c96b9e50SVlastimil Babka zone_end_pfn(zone)); 9267ed695e0SVlastimil Babka low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); 9272fe86e00SMichal Nazarewicz 928ff9543fdSMichal Nazarewicz /* 929ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 930ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 931ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 932ff9543fdSMichal Nazarewicz */ 933932ff6bbSJoonsoo Kim for (; block_start_pfn >= low_pfn && 934932ff6bbSJoonsoo Kim cc->nr_migratepages > cc->nr_freepages; 935c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 936e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 937e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 938ff9543fdSMichal Nazarewicz 939f6ea3adbSDavid Rientjes /* 940f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 941f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 942be976572SVlastimil Babka * to schedule, or even abort async compaction. 943f6ea3adbSDavid Rientjes */ 944be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 945be976572SVlastimil Babka && compact_should_abort(cc)) 946be976572SVlastimil Babka break; 947f6ea3adbSDavid Rientjes 9487d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 9497d49d886SVlastimil Babka zone); 9507d49d886SVlastimil Babka if (!page) 951ff9543fdSMichal Nazarewicz continue; 952ff9543fdSMichal Nazarewicz 953ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 95468e3e926SLinus Torvalds if (!suitable_migration_target(page)) 955ff9543fdSMichal Nazarewicz continue; 95668e3e926SLinus Torvalds 957bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 958bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 959bb13ffebSMel Gorman continue; 960bb13ffebSMel Gorman 961e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 962932ff6bbSJoonsoo Kim isolate_freepages_block(cc, &isolate_start_pfn, 963c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 964ff9543fdSMichal Nazarewicz 965ff9543fdSMichal Nazarewicz /* 966e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 967e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 968e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 969e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 970e14c720eSVlastimil Babka * pageblock. 971e14c720eSVlastimil Babka * In that case we will however want to restart at the start 972e14c720eSVlastimil Babka * of the previous pageblock. 973e14c720eSVlastimil Babka */ 974e14c720eSVlastimil Babka cc->free_pfn = (isolate_start_pfn < block_end_pfn) ? 975e14c720eSVlastimil Babka isolate_start_pfn : 976e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 977e14c720eSVlastimil Babka 978e14c720eSVlastimil Babka /* 979be976572SVlastimil Babka * isolate_freepages_block() might have aborted due to async 980be976572SVlastimil Babka * compaction being contended 981be976572SVlastimil Babka */ 982be976572SVlastimil Babka if (cc->contended) 983be976572SVlastimil Babka break; 984c89511abSMel Gorman } 985ff9543fdSMichal Nazarewicz 986ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 987ff9543fdSMichal Nazarewicz map_pages(freelist); 988ff9543fdSMichal Nazarewicz 9897ed695e0SVlastimil Babka /* 9907ed695e0SVlastimil Babka * If we crossed the migrate scanner, we want to keep it that way 9917ed695e0SVlastimil Babka * so that compact_finished() may detect this 9927ed695e0SVlastimil Babka */ 993c96b9e50SVlastimil Babka if (block_start_pfn < low_pfn) 994e9ade569SVlastimil Babka cc->free_pfn = cc->migrate_pfn; 995748446bbSMel Gorman } 996748446bbSMel Gorman 997748446bbSMel Gorman /* 998748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 999748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 1000748446bbSMel Gorman */ 1001748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 1002748446bbSMel Gorman unsigned long data, 1003748446bbSMel Gorman int **result) 1004748446bbSMel Gorman { 1005748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 1006748446bbSMel Gorman struct page *freepage; 1007748446bbSMel Gorman 1008be976572SVlastimil Babka /* 1009be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 1010be976572SVlastimil Babka * contention. 1011be976572SVlastimil Babka */ 1012748446bbSMel Gorman if (list_empty(&cc->freepages)) { 1013be976572SVlastimil Babka if (!cc->contended) 1014edc2ca61SVlastimil Babka isolate_freepages(cc); 1015748446bbSMel Gorman 1016748446bbSMel Gorman if (list_empty(&cc->freepages)) 1017748446bbSMel Gorman return NULL; 1018748446bbSMel Gorman } 1019748446bbSMel Gorman 1020748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 1021748446bbSMel Gorman list_del(&freepage->lru); 1022748446bbSMel Gorman cc->nr_freepages--; 1023748446bbSMel Gorman 1024748446bbSMel Gorman return freepage; 1025748446bbSMel Gorman } 1026748446bbSMel Gorman 1027748446bbSMel Gorman /* 1028d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 1029d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 1030d53aea3dSDavid Rientjes * special handling needed for NUMA. 1031d53aea3dSDavid Rientjes */ 1032d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 1033d53aea3dSDavid Rientjes { 1034d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 1035d53aea3dSDavid Rientjes 1036d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 1037d53aea3dSDavid Rientjes cc->nr_freepages++; 1038d53aea3dSDavid Rientjes } 1039d53aea3dSDavid Rientjes 1040ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 1041ff9543fdSMichal Nazarewicz typedef enum { 1042ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 1043ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 1044ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 1045ff9543fdSMichal Nazarewicz } isolate_migrate_t; 1046ff9543fdSMichal Nazarewicz 1047ff9543fdSMichal Nazarewicz /* 1048edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 1049edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 1050edc2ca61SVlastimil Babka * compact_control. 1051ff9543fdSMichal Nazarewicz */ 1052ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 1053ff9543fdSMichal Nazarewicz struct compact_control *cc) 1054ff9543fdSMichal Nazarewicz { 1055ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 1056edc2ca61SVlastimil Babka struct page *page; 1057edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 1058edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 1059ff9543fdSMichal Nazarewicz 1060edc2ca61SVlastimil Babka /* 1061edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 1062edc2ca61SVlastimil Babka * initialized by compact_zone() 1063edc2ca61SVlastimil Babka */ 1064edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 1065ff9543fdSMichal Nazarewicz 1066ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 1067a9aacbccSMel Gorman end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); 1068ff9543fdSMichal Nazarewicz 1069edc2ca61SVlastimil Babka /* 1070edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 1071edc2ca61SVlastimil Babka * Do not cross the free scanner. 1072edc2ca61SVlastimil Babka */ 1073edc2ca61SVlastimil Babka for (; end_pfn <= cc->free_pfn; 1074edc2ca61SVlastimil Babka low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { 1075edc2ca61SVlastimil Babka 1076edc2ca61SVlastimil Babka /* 1077edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 1078edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1079edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1080edc2ca61SVlastimil Babka */ 1081edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1082edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1083edc2ca61SVlastimil Babka break; 1084edc2ca61SVlastimil Babka 10857d49d886SVlastimil Babka page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); 10867d49d886SVlastimil Babka if (!page) 1087edc2ca61SVlastimil Babka continue; 1088edc2ca61SVlastimil Babka 1089edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1090edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1091edc2ca61SVlastimil Babka continue; 1092edc2ca61SVlastimil Babka 1093edc2ca61SVlastimil Babka /* 1094edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1095edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1096edc2ca61SVlastimil Babka * of work satisfies the allocation. 1097edc2ca61SVlastimil Babka */ 1098edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1099edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1100edc2ca61SVlastimil Babka continue; 1101ff9543fdSMichal Nazarewicz 1102ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1103edc2ca61SVlastimil Babka low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, 1104edc2ca61SVlastimil Babka isolate_mode); 1105edc2ca61SVlastimil Babka 1106*ff59909aSHugh Dickins if (!low_pfn || cc->contended) { 1107*ff59909aSHugh Dickins acct_isolated(zone, cc); 1108ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1109*ff59909aSHugh Dickins } 1110ff9543fdSMichal Nazarewicz 1111edc2ca61SVlastimil Babka /* 1112edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1113edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1114edc2ca61SVlastimil Babka * continue or not. 1115edc2ca61SVlastimil Babka */ 1116edc2ca61SVlastimil Babka break; 1117edc2ca61SVlastimil Babka } 1118edc2ca61SVlastimil Babka 1119edc2ca61SVlastimil Babka acct_isolated(zone, cc); 11201d5bfe1fSVlastimil Babka /* 11211d5bfe1fSVlastimil Babka * Record where migration scanner will be restarted. If we end up in 11221d5bfe1fSVlastimil Babka * the same pageblock as the free scanner, make the scanners fully 11231d5bfe1fSVlastimil Babka * meet so that compact_finished() terminates compaction. 11241d5bfe1fSVlastimil Babka */ 11251d5bfe1fSVlastimil Babka cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn; 1126ff9543fdSMichal Nazarewicz 1127edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1128ff9543fdSMichal Nazarewicz } 1129ff9543fdSMichal Nazarewicz 1130837d026dSJoonsoo Kim static int __compact_finished(struct zone *zone, struct compact_control *cc, 11316d7ce559SDavid Rientjes const int migratetype) 1132748446bbSMel Gorman { 11338fb74b9fSMel Gorman unsigned int order; 11345a03b051SAndrea Arcangeli unsigned long watermark; 113556de7263SMel Gorman 1136be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 1137748446bbSMel Gorman return COMPACT_PARTIAL; 1138748446bbSMel Gorman 1139753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1140bb13ffebSMel Gorman if (cc->free_pfn <= cc->migrate_pfn) { 114155b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 114235979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 114335979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 114455b7c4c9SVlastimil Babka zone->compact_cached_free_pfn = zone_end_pfn(zone); 114555b7c4c9SVlastimil Babka 114662997027SMel Gorman /* 114762997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 114862997027SMel Gorman * by kswapd when it goes to sleep. kswapd does not set the 114962997027SMel Gorman * flag itself as the decision to be clear should be directly 115062997027SMel Gorman * based on an allocation request. 115162997027SMel Gorman */ 115262997027SMel Gorman if (!current_is_kswapd()) 115362997027SMel Gorman zone->compact_blockskip_flush = true; 115462997027SMel Gorman 1155748446bbSMel Gorman return COMPACT_COMPLETE; 1156bb13ffebSMel Gorman } 1157748446bbSMel Gorman 115882478fb7SJohannes Weiner /* 115982478fb7SJohannes Weiner * order == -1 is expected when compacting via 116082478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 116182478fb7SJohannes Weiner */ 116256de7263SMel Gorman if (cc->order == -1) 116356de7263SMel Gorman return COMPACT_CONTINUE; 116456de7263SMel Gorman 11653957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 11663957c776SMichal Hocko watermark = low_wmark_pages(zone); 11673957c776SMichal Hocko 1168ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1169ebff3980SVlastimil Babka cc->alloc_flags)) 11703957c776SMichal Hocko return COMPACT_CONTINUE; 11713957c776SMichal Hocko 117256de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 117356de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 11748fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 11758fb74b9fSMel Gorman 117656de7263SMel Gorman /* Job done if page is free of the right migratetype */ 11776d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 117856de7263SMel Gorman return COMPACT_PARTIAL; 117956de7263SMel Gorman 118056de7263SMel Gorman /* Job done if allocation would set block type */ 1181372549c2SJoonsoo Kim if (order >= pageblock_order && area->nr_free) 118256de7263SMel Gorman return COMPACT_PARTIAL; 118356de7263SMel Gorman } 118456de7263SMel Gorman 1185837d026dSJoonsoo Kim return COMPACT_NO_SUITABLE_PAGE; 1186837d026dSJoonsoo Kim } 1187837d026dSJoonsoo Kim 1188837d026dSJoonsoo Kim static int compact_finished(struct zone *zone, struct compact_control *cc, 1189837d026dSJoonsoo Kim const int migratetype) 1190837d026dSJoonsoo Kim { 1191837d026dSJoonsoo Kim int ret; 1192837d026dSJoonsoo Kim 1193837d026dSJoonsoo Kim ret = __compact_finished(zone, cc, migratetype); 1194837d026dSJoonsoo Kim trace_mm_compaction_finished(zone, cc->order, ret); 1195837d026dSJoonsoo Kim if (ret == COMPACT_NO_SUITABLE_PAGE) 1196837d026dSJoonsoo Kim ret = COMPACT_CONTINUE; 1197837d026dSJoonsoo Kim 1198837d026dSJoonsoo Kim return ret; 1199748446bbSMel Gorman } 1200748446bbSMel Gorman 12013e7d3449SMel Gorman /* 12023e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 12033e7d3449SMel Gorman * Returns 12043e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 12053e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 12063e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 12073e7d3449SMel Gorman */ 1208837d026dSJoonsoo Kim static unsigned long __compaction_suitable(struct zone *zone, int order, 1209ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 12103e7d3449SMel Gorman { 12113e7d3449SMel Gorman int fragindex; 12123e7d3449SMel Gorman unsigned long watermark; 12133e7d3449SMel Gorman 12143e7d3449SMel Gorman /* 12153957c776SMichal Hocko * order == -1 is expected when compacting via 12163957c776SMichal Hocko * /proc/sys/vm/compact_memory 12173957c776SMichal Hocko */ 12183957c776SMichal Hocko if (order == -1) 12193957c776SMichal Hocko return COMPACT_CONTINUE; 12203957c776SMichal Hocko 1221ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1222ebff3980SVlastimil Babka /* 1223ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1224ebff3980SVlastimil Babka * should be no need for compaction at all. 1225ebff3980SVlastimil Babka */ 1226ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1227ebff3980SVlastimil Babka alloc_flags)) 1228ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1229ebff3980SVlastimil Babka 12303957c776SMichal Hocko /* 12313e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 12323e7d3449SMel Gorman * This is because during migration, copies of pages need to be 12333e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 12343e7d3449SMel Gorman */ 1235ebff3980SVlastimil Babka watermark += (2UL << order); 1236ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 12373e7d3449SMel Gorman return COMPACT_SKIPPED; 12383e7d3449SMel Gorman 12393e7d3449SMel Gorman /* 12403e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 12413e7d3449SMel Gorman * low memory or external fragmentation 12423e7d3449SMel Gorman * 1243ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1244ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 12453e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 12463e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 12473e7d3449SMel Gorman * 12483e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 12493e7d3449SMel Gorman */ 12503e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 12513e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 1252837d026dSJoonsoo Kim return COMPACT_NOT_SUITABLE_ZONE; 12533e7d3449SMel Gorman 12543e7d3449SMel Gorman return COMPACT_CONTINUE; 12553e7d3449SMel Gorman } 12563e7d3449SMel Gorman 1257837d026dSJoonsoo Kim unsigned long compaction_suitable(struct zone *zone, int order, 1258837d026dSJoonsoo Kim int alloc_flags, int classzone_idx) 1259837d026dSJoonsoo Kim { 1260837d026dSJoonsoo Kim unsigned long ret; 1261837d026dSJoonsoo Kim 1262837d026dSJoonsoo Kim ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx); 1263837d026dSJoonsoo Kim trace_mm_compaction_suitable(zone, order, ret); 1264837d026dSJoonsoo Kim if (ret == COMPACT_NOT_SUITABLE_ZONE) 1265837d026dSJoonsoo Kim ret = COMPACT_SKIPPED; 1266837d026dSJoonsoo Kim 1267837d026dSJoonsoo Kim return ret; 1268837d026dSJoonsoo Kim } 1269837d026dSJoonsoo Kim 1270748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1271748446bbSMel Gorman { 1272748446bbSMel Gorman int ret; 1273c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1274108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 12756d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1276e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1277fdaf7f5cSVlastimil Babka unsigned long last_migrated_pfn = 0; 1278748446bbSMel Gorman 1279ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1280ebff3980SVlastimil Babka cc->classzone_idx); 12813e7d3449SMel Gorman switch (ret) { 12823e7d3449SMel Gorman case COMPACT_PARTIAL: 12833e7d3449SMel Gorman case COMPACT_SKIPPED: 12843e7d3449SMel Gorman /* Compaction is likely to fail */ 12853e7d3449SMel Gorman return ret; 12863e7d3449SMel Gorman case COMPACT_CONTINUE: 12873e7d3449SMel Gorman /* Fall through to compaction */ 12883e7d3449SMel Gorman ; 12893e7d3449SMel Gorman } 12903e7d3449SMel Gorman 1291c89511abSMel Gorman /* 1292d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1293d3132e4bSVlastimil Babka * is about to be retried after being deferred. kswapd does not do 1294d3132e4bSVlastimil Babka * this reset as it'll reset the cached information when going to sleep. 1295d3132e4bSVlastimil Babka */ 1296d3132e4bSVlastimil Babka if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) 1297d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1298d3132e4bSVlastimil Babka 1299d3132e4bSVlastimil Babka /* 1300c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1301c89511abSMel Gorman * information on where the scanners should start but check that it 1302c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1303c89511abSMel Gorman */ 1304e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1305c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1306c89511abSMel Gorman if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { 1307c89511abSMel Gorman cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); 1308c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1309c89511abSMel Gorman } 1310c89511abSMel Gorman if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { 1311c89511abSMel Gorman cc->migrate_pfn = start_pfn; 131235979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 131335979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1314c89511abSMel Gorman } 1315748446bbSMel Gorman 131616c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 131716c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 13180eb927c0SMel Gorman 1319748446bbSMel Gorman migrate_prep_local(); 1320748446bbSMel Gorman 13216d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 13226d7ce559SDavid Rientjes COMPACT_CONTINUE) { 13239d502c1cSMinchan Kim int err; 1324fdaf7f5cSVlastimil Babka unsigned long isolate_start_pfn = cc->migrate_pfn; 1325748446bbSMel Gorman 1326f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1327f9e35b3bSMel Gorman case ISOLATE_ABORT: 1328f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 13295733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1330e64c5237SShaohua Li cc->nr_migratepages = 0; 1331f9e35b3bSMel Gorman goto out; 1332f9e35b3bSMel Gorman case ISOLATE_NONE: 1333fdaf7f5cSVlastimil Babka /* 1334fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1335fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1336fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1337fdaf7f5cSVlastimil Babka */ 1338fdaf7f5cSVlastimil Babka goto check_drain; 1339f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1340f9e35b3bSMel Gorman ; 1341f9e35b3bSMel Gorman } 1342748446bbSMel Gorman 1343d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1344e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 13457b2a2d4aSMel Gorman MR_COMPACTION); 1346748446bbSMel Gorman 1347f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1348f8c9301fSVlastimil Babka &cc->migratepages); 1349748446bbSMel Gorman 1350f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1351f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 13529d502c1cSMinchan Kim if (err) { 13535733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 13547ed695e0SVlastimil Babka /* 13557ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 13567ed695e0SVlastimil Babka * and we want compact_finished() to detect it 13577ed695e0SVlastimil Babka */ 13587ed695e0SVlastimil Babka if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) { 13594bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 13604bf2bba3SDavid Rientjes goto out; 1361748446bbSMel Gorman } 13624bf2bba3SDavid Rientjes } 1363fdaf7f5cSVlastimil Babka 1364fdaf7f5cSVlastimil Babka /* 1365fdaf7f5cSVlastimil Babka * Record where we could have freed pages by migration and not 1366fdaf7f5cSVlastimil Babka * yet flushed them to buddy allocator. We use the pfn that 1367fdaf7f5cSVlastimil Babka * isolate_migratepages() started from in this loop iteration 1368fdaf7f5cSVlastimil Babka * - this is the lowest page that could have been isolated and 1369fdaf7f5cSVlastimil Babka * then freed by migration. 1370fdaf7f5cSVlastimil Babka */ 1371fdaf7f5cSVlastimil Babka if (!last_migrated_pfn) 1372fdaf7f5cSVlastimil Babka last_migrated_pfn = isolate_start_pfn; 1373fdaf7f5cSVlastimil Babka 1374fdaf7f5cSVlastimil Babka check_drain: 1375fdaf7f5cSVlastimil Babka /* 1376fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1377fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1378fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1379fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1380fdaf7f5cSVlastimil Babka * would succeed. 1381fdaf7f5cSVlastimil Babka */ 1382fdaf7f5cSVlastimil Babka if (cc->order > 0 && last_migrated_pfn) { 1383fdaf7f5cSVlastimil Babka int cpu; 1384fdaf7f5cSVlastimil Babka unsigned long current_block_start = 1385fdaf7f5cSVlastimil Babka cc->migrate_pfn & ~((1UL << cc->order) - 1); 1386fdaf7f5cSVlastimil Babka 1387fdaf7f5cSVlastimil Babka if (last_migrated_pfn < current_block_start) { 1388fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1389fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1390fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1391fdaf7f5cSVlastimil Babka put_cpu(); 1392fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 1393fdaf7f5cSVlastimil Babka last_migrated_pfn = 0; 1394fdaf7f5cSVlastimil Babka } 1395fdaf7f5cSVlastimil Babka } 1396fdaf7f5cSVlastimil Babka 1397748446bbSMel Gorman } 1398748446bbSMel Gorman 1399f9e35b3bSMel Gorman out: 14006bace090SVlastimil Babka /* 14016bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 14026bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 14036bace090SVlastimil Babka */ 14046bace090SVlastimil Babka if (cc->nr_freepages > 0) { 14056bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 14066bace090SVlastimil Babka 14076bace090SVlastimil Babka cc->nr_freepages = 0; 14086bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 14096bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 14106bace090SVlastimil Babka free_pfn &= ~(pageblock_nr_pages-1); 14116bace090SVlastimil Babka /* 14126bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 14136bace090SVlastimil Babka * already reset to zone end in compact_finished() 14146bace090SVlastimil Babka */ 14156bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 14166bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 14176bace090SVlastimil Babka } 1418748446bbSMel Gorman 141916c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 142016c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 14210eb927c0SMel Gorman 1422748446bbSMel Gorman return ret; 1423748446bbSMel Gorman } 142476ab0f53SMel Gorman 1425e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1426ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1427ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 142856de7263SMel Gorman { 1429e64c5237SShaohua Li unsigned long ret; 143056de7263SMel Gorman struct compact_control cc = { 143156de7263SMel Gorman .nr_freepages = 0, 143256de7263SMel Gorman .nr_migratepages = 0, 143356de7263SMel Gorman .order = order, 14346d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 143556de7263SMel Gorman .zone = zone, 1436e0b9daebSDavid Rientjes .mode = mode, 1437ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1438ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 143956de7263SMel Gorman }; 144056de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 144156de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 144256de7263SMel Gorman 1443e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1444e64c5237SShaohua Li 1445e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1446e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1447e64c5237SShaohua Li 1448e64c5237SShaohua Li *contended = cc.contended; 1449e64c5237SShaohua Li return ret; 145056de7263SMel Gorman } 145156de7263SMel Gorman 14525e771905SMel Gorman int sysctl_extfrag_threshold = 500; 14535e771905SMel Gorman 145456de7263SMel Gorman /** 145556de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 145656de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 14571a6d53a1SVlastimil Babka * @order: The order of the current allocation 14581a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 14591a6d53a1SVlastimil Babka * @ac: The context of current allocation 1460e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 14611f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 14621f9efdefSVlastimil Babka * need_resched() or lock contention 146356de7263SMel Gorman * 146456de7263SMel Gorman * This is the main entry point for direct page compaction. 146556de7263SMel Gorman */ 14661a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 14671a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 14681a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 146956de7263SMel Gorman { 147056de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 147156de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 147256de7263SMel Gorman struct zoneref *z; 147356de7263SMel Gorman struct zone *zone; 147453853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 14751f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 14761f9efdefSVlastimil Babka 14771f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 147856de7263SMel Gorman 14794ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1480c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 148153853e2dSVlastimil Babka return COMPACT_SKIPPED; 148256de7263SMel Gorman 1483837d026dSJoonsoo Kim trace_mm_compaction_try_to_compact_pages(order, gfp_mask, mode); 1484837d026dSJoonsoo Kim 148556de7263SMel Gorman /* Compact each zone in the list */ 14861a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 14871a6d53a1SVlastimil Babka ac->nodemask) { 148856de7263SMel Gorman int status; 14891f9efdefSVlastimil Babka int zone_contended; 149056de7263SMel Gorman 149153853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 149253853e2dSVlastimil Babka continue; 149353853e2dSVlastimil Babka 1494e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 14951a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 14961a6d53a1SVlastimil Babka ac->classzone_idx); 149756de7263SMel Gorman rc = max(status, rc); 14981f9efdefSVlastimil Babka /* 14991f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 15001f9efdefSVlastimil Babka * to clear all_zones_contended. 15011f9efdefSVlastimil Babka */ 15021f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 150356de7263SMel Gorman 15043e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1505ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 15061a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 150753853e2dSVlastimil Babka /* 150853853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 150953853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 151053853e2dSVlastimil Babka * will repeat this with true if allocation indeed 151153853e2dSVlastimil Babka * succeeds in this zone. 151253853e2dSVlastimil Babka */ 151353853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 15141f9efdefSVlastimil Babka /* 15151f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 15161f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 15171f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 15181f9efdefSVlastimil Babka * however still fail so we better signal the 15191f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 15201f9efdefSVlastimil Babka * prevent the allocation attempt). 15211f9efdefSVlastimil Babka */ 15221f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 15231f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15241f9efdefSVlastimil Babka 15251f9efdefSVlastimil Babka goto break_loop; 15261f9efdefSVlastimil Babka } 15271f9efdefSVlastimil Babka 1528f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 152953853e2dSVlastimil Babka /* 153053853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 153153853e2dSVlastimil Babka * so we defer compaction there. If it ends up 153253853e2dSVlastimil Babka * succeeding after all, it will be reset. 153353853e2dSVlastimil Babka */ 153453853e2dSVlastimil Babka defer_compaction(zone, order); 153553853e2dSVlastimil Babka } 15361f9efdefSVlastimil Babka 15371f9efdefSVlastimil Babka /* 15381f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 15391f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 15401f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 15411f9efdefSVlastimil Babka * contention. 15421f9efdefSVlastimil Babka */ 15431f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 15441f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 15451f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 15461f9efdefSVlastimil Babka goto break_loop; 154756de7263SMel Gorman } 154856de7263SMel Gorman 15491f9efdefSVlastimil Babka continue; 15501f9efdefSVlastimil Babka break_loop: 15511f9efdefSVlastimil Babka /* 15521f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 15531f9efdefSVlastimil Babka * and assume they are not all lock contended. 15541f9efdefSVlastimil Babka */ 15551f9efdefSVlastimil Babka all_zones_contended = 0; 15561f9efdefSVlastimil Babka break; 15571f9efdefSVlastimil Babka } 15581f9efdefSVlastimil Babka 15591f9efdefSVlastimil Babka /* 15601f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 15611f9efdefSVlastimil Babka * zones that were tried were lock contended. 15621f9efdefSVlastimil Babka */ 15631f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 15641f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 15651f9efdefSVlastimil Babka 156656de7263SMel Gorman return rc; 156756de7263SMel Gorman } 156856de7263SMel Gorman 156956de7263SMel Gorman 157076ab0f53SMel Gorman /* Compact all zones within a node */ 15717103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 157276ab0f53SMel Gorman { 157376ab0f53SMel Gorman int zoneid; 157476ab0f53SMel Gorman struct zone *zone; 157576ab0f53SMel Gorman 157676ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 157776ab0f53SMel Gorman 157876ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 157976ab0f53SMel Gorman if (!populated_zone(zone)) 158076ab0f53SMel Gorman continue; 158176ab0f53SMel Gorman 15827be62de9SRik van Riel cc->nr_freepages = 0; 15837be62de9SRik van Riel cc->nr_migratepages = 0; 15847be62de9SRik van Riel cc->zone = zone; 15857be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 15867be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 158776ab0f53SMel Gorman 1588aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 15897be62de9SRik van Riel compact_zone(zone, cc); 159076ab0f53SMel Gorman 1591aff62249SRik van Riel if (cc->order > 0) { 1592de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1593de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1594de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1595aff62249SRik van Riel } 1596aff62249SRik van Riel 15977be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 15987be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 159976ab0f53SMel Gorman } 160076ab0f53SMel Gorman } 160176ab0f53SMel Gorman 16027103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 16037be62de9SRik van Riel { 16047be62de9SRik van Riel struct compact_control cc = { 16057be62de9SRik van Riel .order = order, 1606e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 16077be62de9SRik van Riel }; 16087be62de9SRik van Riel 16093a7200afSMel Gorman if (!order) 16103a7200afSMel Gorman return; 16113a7200afSMel Gorman 16127103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 16137be62de9SRik van Riel } 16147be62de9SRik van Riel 16157103f16dSAndrew Morton static void compact_node(int nid) 16167be62de9SRik van Riel { 16177be62de9SRik van Riel struct compact_control cc = { 16187be62de9SRik van Riel .order = -1, 1619e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 162091ca9186SDavid Rientjes .ignore_skip_hint = true, 16217be62de9SRik van Riel }; 16227be62de9SRik van Riel 16237103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 16247be62de9SRik van Riel } 16257be62de9SRik van Riel 162676ab0f53SMel Gorman /* Compact all nodes in the system */ 16277964c06dSJason Liu static void compact_nodes(void) 162876ab0f53SMel Gorman { 162976ab0f53SMel Gorman int nid; 163076ab0f53SMel Gorman 16318575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 16328575ec29SHugh Dickins lru_add_drain_all(); 16338575ec29SHugh Dickins 163476ab0f53SMel Gorman for_each_online_node(nid) 163576ab0f53SMel Gorman compact_node(nid); 163676ab0f53SMel Gorman } 163776ab0f53SMel Gorman 163876ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 163976ab0f53SMel Gorman int sysctl_compact_memory; 164076ab0f53SMel Gorman 164176ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 164276ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 164376ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 164476ab0f53SMel Gorman { 164576ab0f53SMel Gorman if (write) 16467964c06dSJason Liu compact_nodes(); 164776ab0f53SMel Gorman 164876ab0f53SMel Gorman return 0; 164976ab0f53SMel Gorman } 1650ed4a6d7fSMel Gorman 16515e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 16525e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 16535e771905SMel Gorman { 16545e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 16555e771905SMel Gorman 16565e771905SMel Gorman return 0; 16575e771905SMel Gorman } 16585e771905SMel Gorman 1659ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 166074e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 166110fbcf4cSKay Sievers struct device_attribute *attr, 1662ed4a6d7fSMel Gorman const char *buf, size_t count) 1663ed4a6d7fSMel Gorman { 16648575ec29SHugh Dickins int nid = dev->id; 16658575ec29SHugh Dickins 16668575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 16678575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 16688575ec29SHugh Dickins lru_add_drain_all(); 16698575ec29SHugh Dickins 16708575ec29SHugh Dickins compact_node(nid); 16718575ec29SHugh Dickins } 1672ed4a6d7fSMel Gorman 1673ed4a6d7fSMel Gorman return count; 1674ed4a6d7fSMel Gorman } 167510fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1676ed4a6d7fSMel Gorman 1677ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1678ed4a6d7fSMel Gorman { 167910fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1680ed4a6d7fSMel Gorman } 1681ed4a6d7fSMel Gorman 1682ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1683ed4a6d7fSMel Gorman { 168410fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1685ed4a6d7fSMel Gorman } 1686ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1687ff9543fdSMichal Nazarewicz 1688ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1689