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 37*16c4a097SJoonsoo Kim #ifdef CONFIG_TRACEPOINTS 38*16c4a097SJoonsoo Kim static const char *const compaction_status_string[] = { 39*16c4a097SJoonsoo Kim "deferred", 40*16c4a097SJoonsoo Kim "skipped", 41*16c4a097SJoonsoo Kim "continue", 42*16c4a097SJoonsoo Kim "partial", 43*16c4a097SJoonsoo Kim "complete", 44*16c4a097SJoonsoo Kim }; 45*16c4a097SJoonsoo Kim #endif 46ff9543fdSMichal Nazarewicz 47b7aba698SMel Gorman #define CREATE_TRACE_POINTS 48b7aba698SMel Gorman #include <trace/events/compaction.h> 49b7aba698SMel Gorman 50748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 51748446bbSMel Gorman { 52748446bbSMel Gorman struct page *page, *next; 536bace090SVlastimil Babka unsigned long high_pfn = 0; 54748446bbSMel Gorman 55748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 566bace090SVlastimil Babka unsigned long pfn = page_to_pfn(page); 57748446bbSMel Gorman list_del(&page->lru); 58748446bbSMel Gorman __free_page(page); 596bace090SVlastimil Babka if (pfn > high_pfn) 606bace090SVlastimil Babka high_pfn = pfn; 61748446bbSMel Gorman } 62748446bbSMel Gorman 636bace090SVlastimil Babka return high_pfn; 64748446bbSMel Gorman } 65748446bbSMel Gorman 66ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 67ff9543fdSMichal Nazarewicz { 68ff9543fdSMichal Nazarewicz struct page *page; 69ff9543fdSMichal Nazarewicz 70ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 71ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 72ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 73ff9543fdSMichal Nazarewicz } 74ff9543fdSMichal Nazarewicz } 75ff9543fdSMichal Nazarewicz 7647118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 7747118af0SMichal Nazarewicz { 7847118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 7947118af0SMichal Nazarewicz } 8047118af0SMichal Nazarewicz 817d49d886SVlastimil Babka /* 827d49d886SVlastimil Babka * Check that the whole (or subset of) a pageblock given by the interval of 837d49d886SVlastimil Babka * [start_pfn, end_pfn) is valid and within the same zone, before scanning it 847d49d886SVlastimil Babka * with the migration of free compaction scanner. The scanners then need to 857d49d886SVlastimil Babka * use only pfn_valid_within() check for arches that allow holes within 867d49d886SVlastimil Babka * pageblocks. 877d49d886SVlastimil Babka * 887d49d886SVlastimil Babka * Return struct page pointer of start_pfn, or NULL if checks were not passed. 897d49d886SVlastimil Babka * 907d49d886SVlastimil Babka * It's possible on some configurations to have a setup like node0 node1 node0 917d49d886SVlastimil Babka * i.e. it's possible that all pages within a zones range of pages do not 927d49d886SVlastimil Babka * belong to a single zone. We assume that a border between node0 and node1 937d49d886SVlastimil Babka * can occur within a single pageblock, but not a node0 node1 node0 947d49d886SVlastimil Babka * interleaving within a single pageblock. It is therefore sufficient to check 957d49d886SVlastimil Babka * the first and last page of a pageblock and avoid checking each individual 967d49d886SVlastimil Babka * page in a pageblock. 977d49d886SVlastimil Babka */ 987d49d886SVlastimil Babka static struct page *pageblock_pfn_to_page(unsigned long start_pfn, 997d49d886SVlastimil Babka unsigned long end_pfn, struct zone *zone) 1007d49d886SVlastimil Babka { 1017d49d886SVlastimil Babka struct page *start_page; 1027d49d886SVlastimil Babka struct page *end_page; 1037d49d886SVlastimil Babka 1047d49d886SVlastimil Babka /* end_pfn is one past the range we are checking */ 1057d49d886SVlastimil Babka end_pfn--; 1067d49d886SVlastimil Babka 1077d49d886SVlastimil Babka if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) 1087d49d886SVlastimil Babka return NULL; 1097d49d886SVlastimil Babka 1107d49d886SVlastimil Babka start_page = pfn_to_page(start_pfn); 1117d49d886SVlastimil Babka 1127d49d886SVlastimil Babka if (page_zone(start_page) != zone) 1137d49d886SVlastimil Babka return NULL; 1147d49d886SVlastimil Babka 1157d49d886SVlastimil Babka end_page = pfn_to_page(end_pfn); 1167d49d886SVlastimil Babka 1177d49d886SVlastimil Babka /* This gives a shorter code than deriving page_zone(end_page) */ 1187d49d886SVlastimil Babka if (page_zone_id(start_page) != page_zone_id(end_page)) 1197d49d886SVlastimil Babka return NULL; 1207d49d886SVlastimil Babka 1217d49d886SVlastimil Babka return start_page; 1227d49d886SVlastimil Babka } 1237d49d886SVlastimil Babka 124bb13ffebSMel Gorman #ifdef CONFIG_COMPACTION 125bb13ffebSMel Gorman /* Returns true if the pageblock should be scanned for pages to isolate. */ 126bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 127bb13ffebSMel Gorman struct page *page) 128bb13ffebSMel Gorman { 129bb13ffebSMel Gorman if (cc->ignore_skip_hint) 130bb13ffebSMel Gorman return true; 131bb13ffebSMel Gorman 132bb13ffebSMel Gorman return !get_pageblock_skip(page); 133bb13ffebSMel Gorman } 134bb13ffebSMel Gorman 135bb13ffebSMel Gorman /* 136bb13ffebSMel Gorman * This function is called to clear all cached information on pageblocks that 137bb13ffebSMel Gorman * should be skipped for page isolation when the migrate and free page scanner 138bb13ffebSMel Gorman * meet. 139bb13ffebSMel Gorman */ 14062997027SMel Gorman static void __reset_isolation_suitable(struct zone *zone) 141bb13ffebSMel Gorman { 142bb13ffebSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 143108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 144bb13ffebSMel Gorman unsigned long pfn; 145bb13ffebSMel Gorman 14635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = start_pfn; 14735979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = start_pfn; 148c89511abSMel Gorman zone->compact_cached_free_pfn = end_pfn; 14962997027SMel Gorman zone->compact_blockskip_flush = false; 150bb13ffebSMel Gorman 151bb13ffebSMel Gorman /* Walk the zone and mark every pageblock as suitable for isolation */ 152bb13ffebSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 153bb13ffebSMel Gorman struct page *page; 154bb13ffebSMel Gorman 155bb13ffebSMel Gorman cond_resched(); 156bb13ffebSMel Gorman 157bb13ffebSMel Gorman if (!pfn_valid(pfn)) 158bb13ffebSMel Gorman continue; 159bb13ffebSMel Gorman 160bb13ffebSMel Gorman page = pfn_to_page(pfn); 161bb13ffebSMel Gorman if (zone != page_zone(page)) 162bb13ffebSMel Gorman continue; 163bb13ffebSMel Gorman 164bb13ffebSMel Gorman clear_pageblock_skip(page); 165bb13ffebSMel Gorman } 166bb13ffebSMel Gorman } 167bb13ffebSMel Gorman 16862997027SMel Gorman void reset_isolation_suitable(pg_data_t *pgdat) 16962997027SMel Gorman { 17062997027SMel Gorman int zoneid; 17162997027SMel Gorman 17262997027SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 17362997027SMel Gorman struct zone *zone = &pgdat->node_zones[zoneid]; 17462997027SMel Gorman if (!populated_zone(zone)) 17562997027SMel Gorman continue; 17662997027SMel Gorman 17762997027SMel Gorman /* Only flush if a full compaction finished recently */ 17862997027SMel Gorman if (zone->compact_blockskip_flush) 17962997027SMel Gorman __reset_isolation_suitable(zone); 18062997027SMel Gorman } 18162997027SMel Gorman } 18262997027SMel Gorman 183bb13ffebSMel Gorman /* 184bb13ffebSMel Gorman * If no pages were isolated then mark this pageblock to be skipped in the 18562997027SMel Gorman * future. The information is later cleared by __reset_isolation_suitable(). 186bb13ffebSMel Gorman */ 187c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 188c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 189edc2ca61SVlastimil Babka bool migrate_scanner) 190bb13ffebSMel Gorman { 191c89511abSMel Gorman struct zone *zone = cc->zone; 19235979ef3SDavid Rientjes unsigned long pfn; 1936815bf3fSJoonsoo Kim 1946815bf3fSJoonsoo Kim if (cc->ignore_skip_hint) 1956815bf3fSJoonsoo Kim return; 1966815bf3fSJoonsoo Kim 197bb13ffebSMel Gorman if (!page) 198bb13ffebSMel Gorman return; 199bb13ffebSMel Gorman 20035979ef3SDavid Rientjes if (nr_isolated) 20135979ef3SDavid Rientjes return; 20235979ef3SDavid Rientjes 203bb13ffebSMel Gorman set_pageblock_skip(page); 204c89511abSMel Gorman 20535979ef3SDavid Rientjes pfn = page_to_pfn(page); 20635979ef3SDavid Rientjes 20735979ef3SDavid Rientjes /* Update where async and sync compaction should restart */ 208c89511abSMel Gorman if (migrate_scanner) { 20935979ef3SDavid Rientjes if (pfn > zone->compact_cached_migrate_pfn[0]) 21035979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = pfn; 211e0b9daebSDavid Rientjes if (cc->mode != MIGRATE_ASYNC && 212e0b9daebSDavid Rientjes pfn > zone->compact_cached_migrate_pfn[1]) 21335979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = pfn; 214c89511abSMel Gorman } else { 21535979ef3SDavid Rientjes if (pfn < zone->compact_cached_free_pfn) 216c89511abSMel Gorman zone->compact_cached_free_pfn = pfn; 217c89511abSMel Gorman } 218c89511abSMel Gorman } 219bb13ffebSMel Gorman #else 220bb13ffebSMel Gorman static inline bool isolation_suitable(struct compact_control *cc, 221bb13ffebSMel Gorman struct page *page) 222bb13ffebSMel Gorman { 223bb13ffebSMel Gorman return true; 224bb13ffebSMel Gorman } 225bb13ffebSMel Gorman 226c89511abSMel Gorman static void update_pageblock_skip(struct compact_control *cc, 227c89511abSMel Gorman struct page *page, unsigned long nr_isolated, 228edc2ca61SVlastimil Babka bool migrate_scanner) 229bb13ffebSMel Gorman { 230bb13ffebSMel Gorman } 231bb13ffebSMel Gorman #endif /* CONFIG_COMPACTION */ 232bb13ffebSMel Gorman 2331f9efdefSVlastimil Babka /* 2348b44d279SVlastimil Babka * Compaction requires the taking of some coarse locks that are potentially 2358b44d279SVlastimil Babka * very heavily contended. For async compaction, back out if the lock cannot 2368b44d279SVlastimil Babka * be taken immediately. For sync compaction, spin on the lock if needed. 2378b44d279SVlastimil Babka * 2388b44d279SVlastimil Babka * Returns true if the lock is held 2398b44d279SVlastimil Babka * Returns false if the lock is not held and compaction should abort 2401f9efdefSVlastimil Babka */ 2418b44d279SVlastimil Babka static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, 2428b44d279SVlastimil Babka struct compact_control *cc) 2438b44d279SVlastimil Babka { 2448b44d279SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 2458b44d279SVlastimil Babka if (!spin_trylock_irqsave(lock, *flags)) { 2468b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_LOCK; 2478b44d279SVlastimil Babka return false; 2488b44d279SVlastimil Babka } 2498b44d279SVlastimil Babka } else { 2508b44d279SVlastimil Babka spin_lock_irqsave(lock, *flags); 2518b44d279SVlastimil Babka } 2521f9efdefSVlastimil Babka 2538b44d279SVlastimil Babka return true; 2542a1402aaSMel Gorman } 2552a1402aaSMel Gorman 25685aa125fSMichal Nazarewicz /* 257c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 2588b44d279SVlastimil Babka * very heavily contended. The lock should be periodically unlocked to avoid 2598b44d279SVlastimil Babka * having disabled IRQs for a long time, even when there is nobody waiting on 2608b44d279SVlastimil Babka * the lock. It might also be that allowing the IRQs will result in 2618b44d279SVlastimil Babka * need_resched() becoming true. If scheduling is needed, async compaction 2628b44d279SVlastimil Babka * aborts. Sync compaction schedules. 2638b44d279SVlastimil Babka * Either compaction type will also abort if a fatal signal is pending. 2648b44d279SVlastimil Babka * In either case if the lock was locked, it is dropped and not regained. 265c67fe375SMel Gorman * 2668b44d279SVlastimil Babka * Returns true if compaction should abort due to fatal signal pending, or 2678b44d279SVlastimil Babka * async compaction due to need_resched() 2688b44d279SVlastimil Babka * Returns false when compaction can continue (sync compaction might have 2698b44d279SVlastimil Babka * scheduled) 270c67fe375SMel Gorman */ 2718b44d279SVlastimil Babka static bool compact_unlock_should_abort(spinlock_t *lock, 2728b44d279SVlastimil Babka unsigned long flags, bool *locked, struct compact_control *cc) 273c67fe375SMel Gorman { 2748b44d279SVlastimil Babka if (*locked) { 2758b44d279SVlastimil Babka spin_unlock_irqrestore(lock, flags); 2768b44d279SVlastimil Babka *locked = false; 277c67fe375SMel Gorman } 278c67fe375SMel Gorman 2798b44d279SVlastimil Babka if (fatal_signal_pending(current)) { 2808b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 2818b44d279SVlastimil Babka return true; 2828b44d279SVlastimil Babka } 2838b44d279SVlastimil Babka 2848b44d279SVlastimil Babka if (need_resched()) { 285e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) { 2868b44d279SVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 2878b44d279SVlastimil Babka return true; 288c67fe375SMel Gorman } 289c67fe375SMel Gorman cond_resched(); 290c67fe375SMel Gorman } 291c67fe375SMel Gorman 2928b44d279SVlastimil Babka return false; 293c67fe375SMel Gorman } 294c67fe375SMel Gorman 295be976572SVlastimil Babka /* 296be976572SVlastimil Babka * Aside from avoiding lock contention, compaction also periodically checks 297be976572SVlastimil Babka * need_resched() and either schedules in sync compaction or aborts async 2988b44d279SVlastimil Babka * compaction. This is similar to what compact_unlock_should_abort() does, but 299be976572SVlastimil Babka * is used where no lock is concerned. 300be976572SVlastimil Babka * 301be976572SVlastimil Babka * Returns false when no scheduling was needed, or sync compaction scheduled. 302be976572SVlastimil Babka * Returns true when async compaction should abort. 303be976572SVlastimil Babka */ 304be976572SVlastimil Babka static inline bool compact_should_abort(struct compact_control *cc) 305be976572SVlastimil Babka { 306be976572SVlastimil Babka /* async compaction aborts if contended */ 307be976572SVlastimil Babka if (need_resched()) { 308be976572SVlastimil Babka if (cc->mode == MIGRATE_ASYNC) { 3091f9efdefSVlastimil Babka cc->contended = COMPACT_CONTENDED_SCHED; 310be976572SVlastimil Babka return true; 311be976572SVlastimil Babka } 312be976572SVlastimil Babka 313be976572SVlastimil Babka cond_resched(); 314be976572SVlastimil Babka } 315be976572SVlastimil Babka 316be976572SVlastimil Babka return false; 317be976572SVlastimil Babka } 318be976572SVlastimil Babka 319f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */ 320f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page) 321f40d1e42SMel Gorman { 3227d348b9eSJoonsoo Kim /* If the page is a large free page, then disallow migration */ 32399c0fd5eSVlastimil Babka if (PageBuddy(page)) { 32499c0fd5eSVlastimil Babka /* 32599c0fd5eSVlastimil Babka * We are checking page_order without zone->lock taken. But 32699c0fd5eSVlastimil Babka * the only small danger is that we skip a potentially suitable 32799c0fd5eSVlastimil Babka * pageblock, so it's not worth to check order for valid range. 32899c0fd5eSVlastimil Babka */ 32999c0fd5eSVlastimil Babka if (page_order_unsafe(page) >= pageblock_order) 3307d348b9eSJoonsoo Kim return false; 33199c0fd5eSVlastimil Babka } 332f40d1e42SMel Gorman 333f40d1e42SMel Gorman /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 3347d348b9eSJoonsoo Kim if (migrate_async_suitable(get_pageblock_migratetype(page))) 335f40d1e42SMel Gorman return true; 336f40d1e42SMel Gorman 337f40d1e42SMel Gorman /* Otherwise skip the block */ 338f40d1e42SMel Gorman return false; 339f40d1e42SMel Gorman } 340f40d1e42SMel Gorman 341c67fe375SMel Gorman /* 3429e4be470SJerome Marchand * Isolate free pages onto a private freelist. If @strict is true, will abort 3439e4be470SJerome Marchand * returning 0 on any invalid PFNs or non-free pages inside of the pageblock 3449e4be470SJerome Marchand * (even though it may still end up isolating some pages). 34585aa125fSMichal Nazarewicz */ 346f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 347e14c720eSVlastimil Babka unsigned long *start_pfn, 34885aa125fSMichal Nazarewicz unsigned long end_pfn, 34985aa125fSMichal Nazarewicz struct list_head *freelist, 35085aa125fSMichal Nazarewicz bool strict) 351748446bbSMel Gorman { 352b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 353bb13ffebSMel Gorman struct page *cursor, *valid_page = NULL; 354b8b2d825SXiubo Li unsigned long flags = 0; 355f40d1e42SMel Gorman bool locked = false; 356e14c720eSVlastimil Babka unsigned long blockpfn = *start_pfn; 357748446bbSMel Gorman 358748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 359748446bbSMel Gorman 360f40d1e42SMel Gorman /* Isolate free pages. */ 361748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 362748446bbSMel Gorman int isolated, i; 363748446bbSMel Gorman struct page *page = cursor; 364748446bbSMel Gorman 3658b44d279SVlastimil Babka /* 3668b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 3678b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort if fatal signal 3688b44d279SVlastimil Babka * pending or async compaction detects need_resched() 3698b44d279SVlastimil Babka */ 3708b44d279SVlastimil Babka if (!(blockpfn % SWAP_CLUSTER_MAX) 3718b44d279SVlastimil Babka && compact_unlock_should_abort(&cc->zone->lock, flags, 3728b44d279SVlastimil Babka &locked, cc)) 3738b44d279SVlastimil Babka break; 3748b44d279SVlastimil Babka 375b7aba698SMel Gorman nr_scanned++; 376f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 3772af120bcSLaura Abbott goto isolate_fail; 3782af120bcSLaura Abbott 379bb13ffebSMel Gorman if (!valid_page) 380bb13ffebSMel Gorman valid_page = page; 381f40d1e42SMel Gorman if (!PageBuddy(page)) 3822af120bcSLaura Abbott goto isolate_fail; 383f40d1e42SMel Gorman 384f40d1e42SMel Gorman /* 38569b7189fSVlastimil Babka * If we already hold the lock, we can skip some rechecking. 38669b7189fSVlastimil Babka * Note that if we hold the lock now, checked_pageblock was 38769b7189fSVlastimil Babka * already set in some previous iteration (or strict is true), 38869b7189fSVlastimil Babka * so it is correct to skip the suitable migration target 38969b7189fSVlastimil Babka * recheck as well. 39069b7189fSVlastimil Babka */ 39169b7189fSVlastimil Babka if (!locked) { 39269b7189fSVlastimil Babka /* 393f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 394f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 395f40d1e42SMel Gorman * heavily contended if there are parallel allocations 396f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 397f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 398f40d1e42SMel Gorman * possible. 399f40d1e42SMel Gorman */ 4008b44d279SVlastimil Babka locked = compact_trylock_irqsave(&cc->zone->lock, 4018b44d279SVlastimil Babka &flags, cc); 402f40d1e42SMel Gorman if (!locked) 403f40d1e42SMel Gorman break; 404f40d1e42SMel Gorman 405f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 406f40d1e42SMel Gorman if (!PageBuddy(page)) 4072af120bcSLaura Abbott goto isolate_fail; 40869b7189fSVlastimil Babka } 409748446bbSMel Gorman 410748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 411748446bbSMel Gorman isolated = split_free_page(page); 412748446bbSMel Gorman total_isolated += isolated; 413748446bbSMel Gorman for (i = 0; i < isolated; i++) { 414748446bbSMel Gorman list_add(&page->lru, freelist); 415748446bbSMel Gorman page++; 416748446bbSMel Gorman } 417748446bbSMel Gorman 418748446bbSMel Gorman /* If a page was split, advance to the end of it */ 419748446bbSMel Gorman if (isolated) { 420748446bbSMel Gorman blockpfn += isolated - 1; 421748446bbSMel Gorman cursor += isolated - 1; 4222af120bcSLaura Abbott continue; 423748446bbSMel Gorman } 4242af120bcSLaura Abbott 4252af120bcSLaura Abbott isolate_fail: 4262af120bcSLaura Abbott if (strict) 4272af120bcSLaura Abbott break; 4282af120bcSLaura Abbott else 4292af120bcSLaura Abbott continue; 4302af120bcSLaura Abbott 431748446bbSMel Gorman } 432748446bbSMel Gorman 433e14c720eSVlastimil Babka /* Record how far we have got within the block */ 434e14c720eSVlastimil Babka *start_pfn = blockpfn; 435e14c720eSVlastimil Babka 436b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 437f40d1e42SMel Gorman 438f40d1e42SMel Gorman /* 439f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 440f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 441f40d1e42SMel Gorman * returned and CMA will fail. 442f40d1e42SMel Gorman */ 4432af120bcSLaura Abbott if (strict && blockpfn < end_pfn) 444f40d1e42SMel Gorman total_isolated = 0; 445f40d1e42SMel Gorman 446f40d1e42SMel Gorman if (locked) 447f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 448f40d1e42SMel Gorman 449bb13ffebSMel Gorman /* Update the pageblock-skip if the whole pageblock was scanned */ 450bb13ffebSMel Gorman if (blockpfn == end_pfn) 451edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, total_isolated, false); 452bb13ffebSMel Gorman 453010fc29aSMinchan Kim count_compact_events(COMPACTFREE_SCANNED, nr_scanned); 454397487dbSMel Gorman if (total_isolated) 455010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, total_isolated); 456748446bbSMel Gorman return total_isolated; 457748446bbSMel Gorman } 458748446bbSMel Gorman 45985aa125fSMichal Nazarewicz /** 46085aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 46185aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 46285aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 46385aa125fSMichal Nazarewicz * 46485aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 46585aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 46685aa125fSMichal Nazarewicz * undo its actions and return zero. 46785aa125fSMichal Nazarewicz * 46885aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 46985aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 47085aa125fSMichal Nazarewicz * a free page). 47185aa125fSMichal Nazarewicz */ 472ff9543fdSMichal Nazarewicz unsigned long 473bb13ffebSMel Gorman isolate_freepages_range(struct compact_control *cc, 474bb13ffebSMel Gorman unsigned long start_pfn, unsigned long end_pfn) 47585aa125fSMichal Nazarewicz { 476f40d1e42SMel Gorman unsigned long isolated, pfn, block_end_pfn; 47785aa125fSMichal Nazarewicz LIST_HEAD(freelist); 47885aa125fSMichal Nazarewicz 4797d49d886SVlastimil Babka pfn = start_pfn; 48085aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 4817d49d886SVlastimil Babka 4827d49d886SVlastimil Babka for (; pfn < end_pfn; pfn += isolated, 4837d49d886SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 484e14c720eSVlastimil Babka /* Protect pfn from changing by isolate_freepages_block */ 485e14c720eSVlastimil Babka unsigned long isolate_start_pfn = pfn; 4867d49d886SVlastimil Babka 48785aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 48885aa125fSMichal Nazarewicz 48958420016SJoonsoo Kim /* 49058420016SJoonsoo Kim * pfn could pass the block_end_pfn if isolated freepage 49158420016SJoonsoo Kim * is more than pageblock order. In this case, we adjust 49258420016SJoonsoo Kim * scanning range to right one. 49358420016SJoonsoo Kim */ 49458420016SJoonsoo Kim if (pfn >= block_end_pfn) { 49558420016SJoonsoo Kim block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 49658420016SJoonsoo Kim block_end_pfn = min(block_end_pfn, end_pfn); 49758420016SJoonsoo Kim } 49858420016SJoonsoo Kim 4997d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 5007d49d886SVlastimil Babka break; 5017d49d886SVlastimil Babka 502e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 503e14c720eSVlastimil Babka block_end_pfn, &freelist, true); 50485aa125fSMichal Nazarewicz 50585aa125fSMichal Nazarewicz /* 50685aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 50785aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 50885aa125fSMichal Nazarewicz * non-free pages). 50985aa125fSMichal Nazarewicz */ 51085aa125fSMichal Nazarewicz if (!isolated) 51185aa125fSMichal Nazarewicz break; 51285aa125fSMichal Nazarewicz 51385aa125fSMichal Nazarewicz /* 51485aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 51585aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 51685aa125fSMichal Nazarewicz * page may span two pageblocks). 51785aa125fSMichal Nazarewicz */ 51885aa125fSMichal Nazarewicz } 51985aa125fSMichal Nazarewicz 52085aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 52185aa125fSMichal Nazarewicz map_pages(&freelist); 52285aa125fSMichal Nazarewicz 52385aa125fSMichal Nazarewicz if (pfn < end_pfn) { 52485aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 52585aa125fSMichal Nazarewicz release_freepages(&freelist); 52685aa125fSMichal Nazarewicz return 0; 52785aa125fSMichal Nazarewicz } 52885aa125fSMichal Nazarewicz 52985aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 53085aa125fSMichal Nazarewicz return pfn; 53185aa125fSMichal Nazarewicz } 53285aa125fSMichal Nazarewicz 533748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 534edc2ca61SVlastimil Babka static void acct_isolated(struct zone *zone, struct compact_control *cc) 535748446bbSMel Gorman { 536748446bbSMel Gorman struct page *page; 537b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 538748446bbSMel Gorman 539edc2ca61SVlastimil Babka if (list_empty(&cc->migratepages)) 540edc2ca61SVlastimil Babka return; 541edc2ca61SVlastimil Babka 542b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 543b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 544748446bbSMel Gorman 545c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 546c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 547c67fe375SMel Gorman } 548748446bbSMel Gorman 549748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 550748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 551748446bbSMel Gorman { 552bc693045SMinchan Kim unsigned long active, inactive, isolated; 553748446bbSMel Gorman 554748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 555748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 556bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 557bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 558748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 559748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 560748446bbSMel Gorman 561bc693045SMinchan Kim return isolated > (inactive + active) / 2; 562748446bbSMel Gorman } 563748446bbSMel Gorman 5642fe86e00SMichal Nazarewicz /** 565edc2ca61SVlastimil Babka * isolate_migratepages_block() - isolate all migrate-able pages within 566edc2ca61SVlastimil Babka * a single pageblock 5672fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 568edc2ca61SVlastimil Babka * @low_pfn: The first PFN to isolate 569edc2ca61SVlastimil Babka * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock 570edc2ca61SVlastimil Babka * @isolate_mode: Isolation mode to be used. 5712fe86e00SMichal Nazarewicz * 5722fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 573edc2ca61SVlastimil Babka * [low_pfn, end_pfn). The range is expected to be within same pageblock. 574edc2ca61SVlastimil Babka * Returns zero if there is a fatal signal pending, otherwise PFN of the 575edc2ca61SVlastimil Babka * first page that was not scanned (which may be both less, equal to or more 576edc2ca61SVlastimil Babka * than end_pfn). 5772fe86e00SMichal Nazarewicz * 578edc2ca61SVlastimil Babka * The pages are isolated on cc->migratepages list (not required to be empty), 579edc2ca61SVlastimil Babka * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field 580edc2ca61SVlastimil Babka * is neither read nor updated. 581748446bbSMel Gorman */ 582edc2ca61SVlastimil Babka static unsigned long 583edc2ca61SVlastimil Babka isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, 584edc2ca61SVlastimil Babka unsigned long end_pfn, isolate_mode_t isolate_mode) 585748446bbSMel Gorman { 586edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 587b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 588748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 589fa9add64SHugh Dickins struct lruvec *lruvec; 590b8b2d825SXiubo Li unsigned long flags = 0; 5912a1402aaSMel Gorman bool locked = false; 592bb13ffebSMel Gorman struct page *page = NULL, *valid_page = NULL; 593748446bbSMel Gorman 594748446bbSMel Gorman /* 595748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 596748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 597748446bbSMel Gorman * delay for some time until fewer pages are isolated 598748446bbSMel Gorman */ 599748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 600f9e35b3bSMel Gorman /* async migration should just abort */ 601e0b9daebSDavid Rientjes if (cc->mode == MIGRATE_ASYNC) 6022fe86e00SMichal Nazarewicz return 0; 603f9e35b3bSMel Gorman 604748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 605748446bbSMel Gorman 606748446bbSMel Gorman if (fatal_signal_pending(current)) 6072fe86e00SMichal Nazarewicz return 0; 608748446bbSMel Gorman } 609748446bbSMel Gorman 610be976572SVlastimil Babka if (compact_should_abort(cc)) 611aeef4b83SDavid Rientjes return 0; 612aeef4b83SDavid Rientjes 613748446bbSMel Gorman /* Time to isolate some pages for migration */ 614748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 6158b44d279SVlastimil Babka /* 6168b44d279SVlastimil Babka * Periodically drop the lock (if held) regardless of its 6178b44d279SVlastimil Babka * contention, to give chance to IRQs. Abort async compaction 6188b44d279SVlastimil Babka * if contended. 6198b44d279SVlastimil Babka */ 6208b44d279SVlastimil Babka if (!(low_pfn % SWAP_CLUSTER_MAX) 6218b44d279SVlastimil Babka && compact_unlock_should_abort(&zone->lru_lock, flags, 6228b44d279SVlastimil Babka &locked, cc)) 6238b44d279SVlastimil Babka break; 624b2eef8c0SAndrea Arcangeli 625748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 626748446bbSMel Gorman continue; 627b7aba698SMel Gorman nr_scanned++; 628748446bbSMel Gorman 629748446bbSMel Gorman page = pfn_to_page(low_pfn); 630dc908600SMel Gorman 631bb13ffebSMel Gorman if (!valid_page) 632bb13ffebSMel Gorman valid_page = page; 633bb13ffebSMel Gorman 634c122b208SJoonsoo Kim /* 63599c0fd5eSVlastimil Babka * Skip if free. We read page order here without zone lock 63699c0fd5eSVlastimil Babka * which is generally unsafe, but the race window is small and 63799c0fd5eSVlastimil Babka * the worst thing that can happen is that we skip some 63899c0fd5eSVlastimil Babka * potential isolation targets. 6396c14466cSMel Gorman */ 64099c0fd5eSVlastimil Babka if (PageBuddy(page)) { 64199c0fd5eSVlastimil Babka unsigned long freepage_order = page_order_unsafe(page); 64299c0fd5eSVlastimil Babka 64399c0fd5eSVlastimil Babka /* 64499c0fd5eSVlastimil Babka * Without lock, we cannot be sure that what we got is 64599c0fd5eSVlastimil Babka * a valid page order. Consider only values in the 64699c0fd5eSVlastimil Babka * valid order range to prevent low_pfn overflow. 64799c0fd5eSVlastimil Babka */ 64899c0fd5eSVlastimil Babka if (freepage_order > 0 && freepage_order < MAX_ORDER) 64999c0fd5eSVlastimil Babka low_pfn += (1UL << freepage_order) - 1; 650748446bbSMel Gorman continue; 65199c0fd5eSVlastimil Babka } 652748446bbSMel Gorman 6539927af74SMel Gorman /* 654bf6bddf1SRafael Aquini * Check may be lockless but that's ok as we recheck later. 655bf6bddf1SRafael Aquini * It's possible to migrate LRU pages and balloon pages 656bf6bddf1SRafael Aquini * Skip any other type of page 657bf6bddf1SRafael Aquini */ 658bf6bddf1SRafael Aquini if (!PageLRU(page)) { 659bf6bddf1SRafael Aquini if (unlikely(balloon_page_movable(page))) { 660d6d86c0aSKonstantin Khlebnikov if (balloon_page_isolate(page)) { 661bf6bddf1SRafael Aquini /* Successfully isolated */ 662b6c75016SJoonsoo Kim goto isolate_success; 663bf6bddf1SRafael Aquini } 664bf6bddf1SRafael Aquini } 665bc835011SAndrea Arcangeli continue; 666bf6bddf1SRafael Aquini } 667bc835011SAndrea Arcangeli 668bc835011SAndrea Arcangeli /* 6692a1402aaSMel Gorman * PageLRU is set. lru_lock normally excludes isolation 6702a1402aaSMel Gorman * splitting and collapsing (collapsing has already happened 6712a1402aaSMel Gorman * if PageLRU is set) but the lock is not necessarily taken 6722a1402aaSMel Gorman * here and it is wasteful to take it just to check transhuge. 6732a1402aaSMel Gorman * Check TransHuge without lock and skip the whole pageblock if 6742a1402aaSMel Gorman * it's either a transhuge or hugetlbfs page, as calling 6752a1402aaSMel Gorman * compound_order() without preventing THP from splitting the 6762a1402aaSMel Gorman * page underneath us may return surprising results. 677bc835011SAndrea Arcangeli */ 678bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 6792a1402aaSMel Gorman if (!locked) 680edc2ca61SVlastimil Babka low_pfn = ALIGN(low_pfn + 1, 681edc2ca61SVlastimil Babka pageblock_nr_pages) - 1; 682edc2ca61SVlastimil Babka else 6832a1402aaSMel Gorman low_pfn += (1 << compound_order(page)) - 1; 684edc2ca61SVlastimil Babka 6852a1402aaSMel Gorman continue; 6862a1402aaSMel Gorman } 6872a1402aaSMel Gorman 688119d6d59SDavid Rientjes /* 689119d6d59SDavid Rientjes * Migration will fail if an anonymous page is pinned in memory, 690119d6d59SDavid Rientjes * so avoid taking lru_lock and isolating it unnecessarily in an 691119d6d59SDavid Rientjes * admittedly racy check. 692119d6d59SDavid Rientjes */ 693119d6d59SDavid Rientjes if (!page_mapping(page) && 694119d6d59SDavid Rientjes page_count(page) > page_mapcount(page)) 695119d6d59SDavid Rientjes continue; 696119d6d59SDavid Rientjes 69769b7189fSVlastimil Babka /* If we already hold the lock, we can skip some rechecking */ 69869b7189fSVlastimil Babka if (!locked) { 6998b44d279SVlastimil Babka locked = compact_trylock_irqsave(&zone->lru_lock, 7008b44d279SVlastimil Babka &flags, cc); 7018b44d279SVlastimil Babka if (!locked) 7022a1402aaSMel Gorman break; 7032a1402aaSMel Gorman 7042a1402aaSMel Gorman /* Recheck PageLRU and PageTransHuge under lock */ 7052a1402aaSMel Gorman if (!PageLRU(page)) 7062a1402aaSMel Gorman continue; 7072a1402aaSMel Gorman if (PageTransHuge(page)) { 708bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 709bc835011SAndrea Arcangeli continue; 710bc835011SAndrea Arcangeli } 71169b7189fSVlastimil Babka } 712bc835011SAndrea Arcangeli 713fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 714fa9add64SHugh Dickins 715748446bbSMel Gorman /* Try isolate the page */ 716edc2ca61SVlastimil Babka if (__isolate_lru_page(page, isolate_mode) != 0) 717748446bbSMel Gorman continue; 718748446bbSMel Gorman 719309381feSSasha Levin VM_BUG_ON_PAGE(PageTransCompound(page), page); 720bc835011SAndrea Arcangeli 721748446bbSMel Gorman /* Successfully isolated */ 722fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 723b6c75016SJoonsoo Kim 724b6c75016SJoonsoo Kim isolate_success: 725748446bbSMel Gorman list_add(&page->lru, migratelist); 726748446bbSMel Gorman cc->nr_migratepages++; 727b7aba698SMel Gorman nr_isolated++; 728748446bbSMel Gorman 729748446bbSMel Gorman /* Avoid isolating too much */ 73031b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 73131b8384aSHillf Danton ++low_pfn; 732748446bbSMel Gorman break; 733748446bbSMel Gorman } 73431b8384aSHillf Danton } 735748446bbSMel Gorman 73699c0fd5eSVlastimil Babka /* 73799c0fd5eSVlastimil Babka * The PageBuddy() check could have potentially brought us outside 73899c0fd5eSVlastimil Babka * the range to be scanned. 73999c0fd5eSVlastimil Babka */ 74099c0fd5eSVlastimil Babka if (unlikely(low_pfn > end_pfn)) 74199c0fd5eSVlastimil Babka low_pfn = end_pfn; 74299c0fd5eSVlastimil Babka 743c67fe375SMel Gorman if (locked) 744c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 745748446bbSMel Gorman 74650b5b094SVlastimil Babka /* 74750b5b094SVlastimil Babka * Update the pageblock-skip information and cached scanner pfn, 74850b5b094SVlastimil Babka * if the whole pageblock was scanned without isolating any page. 74950b5b094SVlastimil Babka */ 75035979ef3SDavid Rientjes if (low_pfn == end_pfn) 751edc2ca61SVlastimil Babka update_pageblock_skip(cc, valid_page, nr_isolated, true); 752bb13ffebSMel Gorman 753b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 754b7aba698SMel Gorman 755010fc29aSMinchan Kim count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned); 756397487dbSMel Gorman if (nr_isolated) 757010fc29aSMinchan Kim count_compact_events(COMPACTISOLATED, nr_isolated); 758397487dbSMel Gorman 7592fe86e00SMichal Nazarewicz return low_pfn; 7602fe86e00SMichal Nazarewicz } 7612fe86e00SMichal Nazarewicz 762edc2ca61SVlastimil Babka /** 763edc2ca61SVlastimil Babka * isolate_migratepages_range() - isolate migrate-able pages in a PFN range 764edc2ca61SVlastimil Babka * @cc: Compaction control structure. 765edc2ca61SVlastimil Babka * @start_pfn: The first PFN to start isolating. 766edc2ca61SVlastimil Babka * @end_pfn: The one-past-last PFN. 767edc2ca61SVlastimil Babka * 768edc2ca61SVlastimil Babka * Returns zero if isolation fails fatally due to e.g. pending signal. 769edc2ca61SVlastimil Babka * Otherwise, function returns one-past-the-last PFN of isolated page 770edc2ca61SVlastimil Babka * (which may be greater than end_pfn if end fell in a middle of a THP page). 771edc2ca61SVlastimil Babka */ 772edc2ca61SVlastimil Babka unsigned long 773edc2ca61SVlastimil Babka isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, 774edc2ca61SVlastimil Babka unsigned long end_pfn) 775edc2ca61SVlastimil Babka { 776edc2ca61SVlastimil Babka unsigned long pfn, block_end_pfn; 777edc2ca61SVlastimil Babka 778edc2ca61SVlastimil Babka /* Scan block by block. First and last block may be incomplete */ 779edc2ca61SVlastimil Babka pfn = start_pfn; 780edc2ca61SVlastimil Babka block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 781edc2ca61SVlastimil Babka 782edc2ca61SVlastimil Babka for (; pfn < end_pfn; pfn = block_end_pfn, 783edc2ca61SVlastimil Babka block_end_pfn += pageblock_nr_pages) { 784edc2ca61SVlastimil Babka 785edc2ca61SVlastimil Babka block_end_pfn = min(block_end_pfn, end_pfn); 786edc2ca61SVlastimil Babka 7877d49d886SVlastimil Babka if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone)) 788edc2ca61SVlastimil Babka continue; 789edc2ca61SVlastimil Babka 790edc2ca61SVlastimil Babka pfn = isolate_migratepages_block(cc, pfn, block_end_pfn, 791edc2ca61SVlastimil Babka ISOLATE_UNEVICTABLE); 792edc2ca61SVlastimil Babka 793edc2ca61SVlastimil Babka /* 794edc2ca61SVlastimil Babka * In case of fatal failure, release everything that might 795edc2ca61SVlastimil Babka * have been isolated in the previous iteration, and signal 796edc2ca61SVlastimil Babka * the failure back to caller. 797edc2ca61SVlastimil Babka */ 798edc2ca61SVlastimil Babka if (!pfn) { 799edc2ca61SVlastimil Babka putback_movable_pages(&cc->migratepages); 800edc2ca61SVlastimil Babka cc->nr_migratepages = 0; 801edc2ca61SVlastimil Babka break; 802edc2ca61SVlastimil Babka } 8036ea41c0cSJoonsoo Kim 8046ea41c0cSJoonsoo Kim if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) 8056ea41c0cSJoonsoo Kim break; 806edc2ca61SVlastimil Babka } 807edc2ca61SVlastimil Babka acct_isolated(cc->zone, cc); 808edc2ca61SVlastimil Babka 809edc2ca61SVlastimil Babka return pfn; 810edc2ca61SVlastimil Babka } 811edc2ca61SVlastimil Babka 812ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 813ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 814ff9543fdSMichal Nazarewicz /* 815ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 816ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 817ff9543fdSMichal Nazarewicz */ 818edc2ca61SVlastimil Babka static void isolate_freepages(struct compact_control *cc) 819ff9543fdSMichal Nazarewicz { 820edc2ca61SVlastimil Babka struct zone *zone = cc->zone; 821ff9543fdSMichal Nazarewicz struct page *page; 822c96b9e50SVlastimil Babka unsigned long block_start_pfn; /* start of current pageblock */ 823e14c720eSVlastimil Babka unsigned long isolate_start_pfn; /* exact pfn we start at */ 824c96b9e50SVlastimil Babka unsigned long block_end_pfn; /* end of current pageblock */ 825c96b9e50SVlastimil Babka unsigned long low_pfn; /* lowest pfn scanner is able to scan */ 826ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 827ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 8282fe86e00SMichal Nazarewicz 829ff9543fdSMichal Nazarewicz /* 830ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 83149e068f0SVlastimil Babka * successfully isolated from, zone-cached value, or the end of the 832e14c720eSVlastimil Babka * zone when isolating for the first time. For looping we also need 833e14c720eSVlastimil Babka * this pfn aligned down to the pageblock boundary, because we do 834c96b9e50SVlastimil Babka * block_start_pfn -= pageblock_nr_pages in the for loop. 835c96b9e50SVlastimil Babka * For ending point, take care when isolating in last pageblock of a 836c96b9e50SVlastimil Babka * a zone which ends in the middle of a pageblock. 83749e068f0SVlastimil Babka * The low boundary is the end of the pageblock the migration scanner 83849e068f0SVlastimil Babka * is using. 839ff9543fdSMichal Nazarewicz */ 840e14c720eSVlastimil Babka isolate_start_pfn = cc->free_pfn; 841c96b9e50SVlastimil Babka block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1); 842c96b9e50SVlastimil Babka block_end_pfn = min(block_start_pfn + pageblock_nr_pages, 843c96b9e50SVlastimil Babka zone_end_pfn(zone)); 8447ed695e0SVlastimil Babka low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages); 8452fe86e00SMichal Nazarewicz 846ff9543fdSMichal Nazarewicz /* 847ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 848ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 849ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 850ff9543fdSMichal Nazarewicz */ 851c96b9e50SVlastimil Babka for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages; 852c96b9e50SVlastimil Babka block_end_pfn = block_start_pfn, 853e14c720eSVlastimil Babka block_start_pfn -= pageblock_nr_pages, 854e14c720eSVlastimil Babka isolate_start_pfn = block_start_pfn) { 855ff9543fdSMichal Nazarewicz unsigned long isolated; 856ff9543fdSMichal Nazarewicz 857f6ea3adbSDavid Rientjes /* 858f6ea3adbSDavid Rientjes * This can iterate a massively long zone without finding any 859f6ea3adbSDavid Rientjes * suitable migration targets, so periodically check if we need 860be976572SVlastimil Babka * to schedule, or even abort async compaction. 861f6ea3adbSDavid Rientjes */ 862be976572SVlastimil Babka if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 863be976572SVlastimil Babka && compact_should_abort(cc)) 864be976572SVlastimil Babka break; 865f6ea3adbSDavid Rientjes 8667d49d886SVlastimil Babka page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, 8677d49d886SVlastimil Babka zone); 8687d49d886SVlastimil Babka if (!page) 869ff9543fdSMichal Nazarewicz continue; 870ff9543fdSMichal Nazarewicz 871ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 87268e3e926SLinus Torvalds if (!suitable_migration_target(page)) 873ff9543fdSMichal Nazarewicz continue; 87468e3e926SLinus Torvalds 875bb13ffebSMel Gorman /* If isolation recently failed, do not retry */ 876bb13ffebSMel Gorman if (!isolation_suitable(cc, page)) 877bb13ffebSMel Gorman continue; 878bb13ffebSMel Gorman 879e14c720eSVlastimil Babka /* Found a block suitable for isolating free pages from. */ 880e14c720eSVlastimil Babka isolated = isolate_freepages_block(cc, &isolate_start_pfn, 881c96b9e50SVlastimil Babka block_end_pfn, freelist, false); 882ff9543fdSMichal Nazarewicz nr_freepages += isolated; 883ff9543fdSMichal Nazarewicz 884ff9543fdSMichal Nazarewicz /* 885e14c720eSVlastimil Babka * Remember where the free scanner should restart next time, 886e14c720eSVlastimil Babka * which is where isolate_freepages_block() left off. 887e14c720eSVlastimil Babka * But if it scanned the whole pageblock, isolate_start_pfn 888e14c720eSVlastimil Babka * now points at block_end_pfn, which is the start of the next 889e14c720eSVlastimil Babka * pageblock. 890e14c720eSVlastimil Babka * In that case we will however want to restart at the start 891e14c720eSVlastimil Babka * of the previous pageblock. 892e14c720eSVlastimil Babka */ 893e14c720eSVlastimil Babka cc->free_pfn = (isolate_start_pfn < block_end_pfn) ? 894e14c720eSVlastimil Babka isolate_start_pfn : 895e14c720eSVlastimil Babka block_start_pfn - pageblock_nr_pages; 896e14c720eSVlastimil Babka 897e14c720eSVlastimil Babka /* 898be976572SVlastimil Babka * isolate_freepages_block() might have aborted due to async 899be976572SVlastimil Babka * compaction being contended 900be976572SVlastimil Babka */ 901be976572SVlastimil Babka if (cc->contended) 902be976572SVlastimil Babka break; 903c89511abSMel Gorman } 904ff9543fdSMichal Nazarewicz 905ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 906ff9543fdSMichal Nazarewicz map_pages(freelist); 907ff9543fdSMichal Nazarewicz 9087ed695e0SVlastimil Babka /* 9097ed695e0SVlastimil Babka * If we crossed the migrate scanner, we want to keep it that way 9107ed695e0SVlastimil Babka * so that compact_finished() may detect this 9117ed695e0SVlastimil Babka */ 912c96b9e50SVlastimil Babka if (block_start_pfn < low_pfn) 913e9ade569SVlastimil Babka cc->free_pfn = cc->migrate_pfn; 914c96b9e50SVlastimil Babka 915ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 916748446bbSMel Gorman } 917748446bbSMel Gorman 918748446bbSMel Gorman /* 919748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 920748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 921748446bbSMel Gorman */ 922748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 923748446bbSMel Gorman unsigned long data, 924748446bbSMel Gorman int **result) 925748446bbSMel Gorman { 926748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 927748446bbSMel Gorman struct page *freepage; 928748446bbSMel Gorman 929be976572SVlastimil Babka /* 930be976572SVlastimil Babka * Isolate free pages if necessary, and if we are not aborting due to 931be976572SVlastimil Babka * contention. 932be976572SVlastimil Babka */ 933748446bbSMel Gorman if (list_empty(&cc->freepages)) { 934be976572SVlastimil Babka if (!cc->contended) 935edc2ca61SVlastimil Babka isolate_freepages(cc); 936748446bbSMel Gorman 937748446bbSMel Gorman if (list_empty(&cc->freepages)) 938748446bbSMel Gorman return NULL; 939748446bbSMel Gorman } 940748446bbSMel Gorman 941748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 942748446bbSMel Gorman list_del(&freepage->lru); 943748446bbSMel Gorman cc->nr_freepages--; 944748446bbSMel Gorman 945748446bbSMel Gorman return freepage; 946748446bbSMel Gorman } 947748446bbSMel Gorman 948748446bbSMel Gorman /* 949d53aea3dSDavid Rientjes * This is a migrate-callback that "frees" freepages back to the isolated 950d53aea3dSDavid Rientjes * freelist. All pages on the freelist are from the same zone, so there is no 951d53aea3dSDavid Rientjes * special handling needed for NUMA. 952d53aea3dSDavid Rientjes */ 953d53aea3dSDavid Rientjes static void compaction_free(struct page *page, unsigned long data) 954d53aea3dSDavid Rientjes { 955d53aea3dSDavid Rientjes struct compact_control *cc = (struct compact_control *)data; 956d53aea3dSDavid Rientjes 957d53aea3dSDavid Rientjes list_add(&page->lru, &cc->freepages); 958d53aea3dSDavid Rientjes cc->nr_freepages++; 959d53aea3dSDavid Rientjes } 960d53aea3dSDavid Rientjes 961ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 962ff9543fdSMichal Nazarewicz typedef enum { 963ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 964ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 965ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 966ff9543fdSMichal Nazarewicz } isolate_migrate_t; 967ff9543fdSMichal Nazarewicz 968ff9543fdSMichal Nazarewicz /* 969edc2ca61SVlastimil Babka * Isolate all pages that can be migrated from the first suitable block, 970edc2ca61SVlastimil Babka * starting at the block pointed to by the migrate scanner pfn within 971edc2ca61SVlastimil Babka * compact_control. 972ff9543fdSMichal Nazarewicz */ 973ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 974ff9543fdSMichal Nazarewicz struct compact_control *cc) 975ff9543fdSMichal Nazarewicz { 976ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 977edc2ca61SVlastimil Babka struct page *page; 978edc2ca61SVlastimil Babka const isolate_mode_t isolate_mode = 979edc2ca61SVlastimil Babka (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0); 980ff9543fdSMichal Nazarewicz 981edc2ca61SVlastimil Babka /* 982edc2ca61SVlastimil Babka * Start at where we last stopped, or beginning of the zone as 983edc2ca61SVlastimil Babka * initialized by compact_zone() 984edc2ca61SVlastimil Babka */ 985edc2ca61SVlastimil Babka low_pfn = cc->migrate_pfn; 986ff9543fdSMichal Nazarewicz 987ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 988a9aacbccSMel Gorman end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages); 989ff9543fdSMichal Nazarewicz 990edc2ca61SVlastimil Babka /* 991edc2ca61SVlastimil Babka * Iterate over whole pageblocks until we find the first suitable. 992edc2ca61SVlastimil Babka * Do not cross the free scanner. 993edc2ca61SVlastimil Babka */ 994edc2ca61SVlastimil Babka for (; end_pfn <= cc->free_pfn; 995edc2ca61SVlastimil Babka low_pfn = end_pfn, end_pfn += pageblock_nr_pages) { 996edc2ca61SVlastimil Babka 997edc2ca61SVlastimil Babka /* 998edc2ca61SVlastimil Babka * This can potentially iterate a massively long zone with 999edc2ca61SVlastimil Babka * many pageblocks unsuitable, so periodically check if we 1000edc2ca61SVlastimil Babka * need to schedule, or even abort async compaction. 1001edc2ca61SVlastimil Babka */ 1002edc2ca61SVlastimil Babka if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) 1003edc2ca61SVlastimil Babka && compact_should_abort(cc)) 1004edc2ca61SVlastimil Babka break; 1005edc2ca61SVlastimil Babka 10067d49d886SVlastimil Babka page = pageblock_pfn_to_page(low_pfn, end_pfn, zone); 10077d49d886SVlastimil Babka if (!page) 1008edc2ca61SVlastimil Babka continue; 1009edc2ca61SVlastimil Babka 1010edc2ca61SVlastimil Babka /* If isolation recently failed, do not retry */ 1011edc2ca61SVlastimil Babka if (!isolation_suitable(cc, page)) 1012edc2ca61SVlastimil Babka continue; 1013edc2ca61SVlastimil Babka 1014edc2ca61SVlastimil Babka /* 1015edc2ca61SVlastimil Babka * For async compaction, also only scan in MOVABLE blocks. 1016edc2ca61SVlastimil Babka * Async compaction is optimistic to see if the minimum amount 1017edc2ca61SVlastimil Babka * of work satisfies the allocation. 1018edc2ca61SVlastimil Babka */ 1019edc2ca61SVlastimil Babka if (cc->mode == MIGRATE_ASYNC && 1020edc2ca61SVlastimil Babka !migrate_async_suitable(get_pageblock_migratetype(page))) 1021edc2ca61SVlastimil Babka continue; 1022ff9543fdSMichal Nazarewicz 1023ff9543fdSMichal Nazarewicz /* Perform the isolation */ 1024edc2ca61SVlastimil Babka low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn, 1025edc2ca61SVlastimil Babka isolate_mode); 1026edc2ca61SVlastimil Babka 1027e64c5237SShaohua Li if (!low_pfn || cc->contended) 1028ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 1029ff9543fdSMichal Nazarewicz 1030edc2ca61SVlastimil Babka /* 1031edc2ca61SVlastimil Babka * Either we isolated something and proceed with migration. Or 1032edc2ca61SVlastimil Babka * we failed and compact_zone should decide if we should 1033edc2ca61SVlastimil Babka * continue or not. 1034edc2ca61SVlastimil Babka */ 1035edc2ca61SVlastimil Babka break; 1036edc2ca61SVlastimil Babka } 1037edc2ca61SVlastimil Babka 1038edc2ca61SVlastimil Babka acct_isolated(zone, cc); 10391d5bfe1fSVlastimil Babka /* 10401d5bfe1fSVlastimil Babka * Record where migration scanner will be restarted. If we end up in 10411d5bfe1fSVlastimil Babka * the same pageblock as the free scanner, make the scanners fully 10421d5bfe1fSVlastimil Babka * meet so that compact_finished() terminates compaction. 10431d5bfe1fSVlastimil Babka */ 10441d5bfe1fSVlastimil Babka cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn; 1045ff9543fdSMichal Nazarewicz 1046edc2ca61SVlastimil Babka return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; 1047ff9543fdSMichal Nazarewicz } 1048ff9543fdSMichal Nazarewicz 10496d7ce559SDavid Rientjes static int compact_finished(struct zone *zone, struct compact_control *cc, 10506d7ce559SDavid Rientjes const int migratetype) 1051748446bbSMel Gorman { 10528fb74b9fSMel Gorman unsigned int order; 10535a03b051SAndrea Arcangeli unsigned long watermark; 105456de7263SMel Gorman 1055be976572SVlastimil Babka if (cc->contended || fatal_signal_pending(current)) 1056748446bbSMel Gorman return COMPACT_PARTIAL; 1057748446bbSMel Gorman 1058753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 1059bb13ffebSMel Gorman if (cc->free_pfn <= cc->migrate_pfn) { 106055b7c4c9SVlastimil Babka /* Let the next compaction start anew. */ 106135979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; 106235979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; 106355b7c4c9SVlastimil Babka zone->compact_cached_free_pfn = zone_end_pfn(zone); 106455b7c4c9SVlastimil Babka 106562997027SMel Gorman /* 106662997027SMel Gorman * Mark that the PG_migrate_skip information should be cleared 106762997027SMel Gorman * by kswapd when it goes to sleep. kswapd does not set the 106862997027SMel Gorman * flag itself as the decision to be clear should be directly 106962997027SMel Gorman * based on an allocation request. 107062997027SMel Gorman */ 107162997027SMel Gorman if (!current_is_kswapd()) 107262997027SMel Gorman zone->compact_blockskip_flush = true; 107362997027SMel Gorman 1074748446bbSMel Gorman return COMPACT_COMPLETE; 1075bb13ffebSMel Gorman } 1076748446bbSMel Gorman 107782478fb7SJohannes Weiner /* 107882478fb7SJohannes Weiner * order == -1 is expected when compacting via 107982478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 108082478fb7SJohannes Weiner */ 108156de7263SMel Gorman if (cc->order == -1) 108256de7263SMel Gorman return COMPACT_CONTINUE; 108356de7263SMel Gorman 10843957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 10853957c776SMichal Hocko watermark = low_wmark_pages(zone); 10863957c776SMichal Hocko 1087ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx, 1088ebff3980SVlastimil Babka cc->alloc_flags)) 10893957c776SMichal Hocko return COMPACT_CONTINUE; 10903957c776SMichal Hocko 109156de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 109256de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 10938fb74b9fSMel Gorman struct free_area *area = &zone->free_area[order]; 10948fb74b9fSMel Gorman 109556de7263SMel Gorman /* Job done if page is free of the right migratetype */ 10966d7ce559SDavid Rientjes if (!list_empty(&area->free_list[migratetype])) 109756de7263SMel Gorman return COMPACT_PARTIAL; 109856de7263SMel Gorman 109956de7263SMel Gorman /* Job done if allocation would set block type */ 11001fb3f8caSMel Gorman if (cc->order >= pageblock_order && area->nr_free) 110156de7263SMel Gorman return COMPACT_PARTIAL; 110256de7263SMel Gorman } 110356de7263SMel Gorman 1104748446bbSMel Gorman return COMPACT_CONTINUE; 1105748446bbSMel Gorman } 1106748446bbSMel Gorman 11073e7d3449SMel Gorman /* 11083e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 11093e7d3449SMel Gorman * Returns 11103e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 11113e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 11123e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 11133e7d3449SMel Gorman */ 1114ebff3980SVlastimil Babka unsigned long compaction_suitable(struct zone *zone, int order, 1115ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 11163e7d3449SMel Gorman { 11173e7d3449SMel Gorman int fragindex; 11183e7d3449SMel Gorman unsigned long watermark; 11193e7d3449SMel Gorman 11203e7d3449SMel Gorman /* 11213957c776SMichal Hocko * order == -1 is expected when compacting via 11223957c776SMichal Hocko * /proc/sys/vm/compact_memory 11233957c776SMichal Hocko */ 11243957c776SMichal Hocko if (order == -1) 11253957c776SMichal Hocko return COMPACT_CONTINUE; 11263957c776SMichal Hocko 1127ebff3980SVlastimil Babka watermark = low_wmark_pages(zone); 1128ebff3980SVlastimil Babka /* 1129ebff3980SVlastimil Babka * If watermarks for high-order allocation are already met, there 1130ebff3980SVlastimil Babka * should be no need for compaction at all. 1131ebff3980SVlastimil Babka */ 1132ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, watermark, classzone_idx, 1133ebff3980SVlastimil Babka alloc_flags)) 1134ebff3980SVlastimil Babka return COMPACT_PARTIAL; 1135ebff3980SVlastimil Babka 11363957c776SMichal Hocko /* 11373e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 11383e7d3449SMel Gorman * This is because during migration, copies of pages need to be 11393e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 11403e7d3449SMel Gorman */ 1141ebff3980SVlastimil Babka watermark += (2UL << order); 1142ebff3980SVlastimil Babka if (!zone_watermark_ok(zone, 0, watermark, classzone_idx, alloc_flags)) 11433e7d3449SMel Gorman return COMPACT_SKIPPED; 11443e7d3449SMel Gorman 11453e7d3449SMel Gorman /* 11463e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 11473e7d3449SMel Gorman * low memory or external fragmentation 11483e7d3449SMel Gorman * 1149ebff3980SVlastimil Babka * index of -1000 would imply allocations might succeed depending on 1150ebff3980SVlastimil Babka * watermarks, but we already failed the high-order watermark check 11513e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 11523e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 11533e7d3449SMel Gorman * 11543e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 11553e7d3449SMel Gorman */ 11563e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 11573e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 11583e7d3449SMel Gorman return COMPACT_SKIPPED; 11593e7d3449SMel Gorman 11603e7d3449SMel Gorman return COMPACT_CONTINUE; 11613e7d3449SMel Gorman } 11623e7d3449SMel Gorman 1163748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 1164748446bbSMel Gorman { 1165748446bbSMel Gorman int ret; 1166c89511abSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 1167108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 11686d7ce559SDavid Rientjes const int migratetype = gfpflags_to_migratetype(cc->gfp_mask); 1169e0b9daebSDavid Rientjes const bool sync = cc->mode != MIGRATE_ASYNC; 1170fdaf7f5cSVlastimil Babka unsigned long last_migrated_pfn = 0; 1171748446bbSMel Gorman 1172ebff3980SVlastimil Babka ret = compaction_suitable(zone, cc->order, cc->alloc_flags, 1173ebff3980SVlastimil Babka cc->classzone_idx); 11743e7d3449SMel Gorman switch (ret) { 11753e7d3449SMel Gorman case COMPACT_PARTIAL: 11763e7d3449SMel Gorman case COMPACT_SKIPPED: 11773e7d3449SMel Gorman /* Compaction is likely to fail */ 11783e7d3449SMel Gorman return ret; 11793e7d3449SMel Gorman case COMPACT_CONTINUE: 11803e7d3449SMel Gorman /* Fall through to compaction */ 11813e7d3449SMel Gorman ; 11823e7d3449SMel Gorman } 11833e7d3449SMel Gorman 1184c89511abSMel Gorman /* 1185d3132e4bSVlastimil Babka * Clear pageblock skip if there were failures recently and compaction 1186d3132e4bSVlastimil Babka * is about to be retried after being deferred. kswapd does not do 1187d3132e4bSVlastimil Babka * this reset as it'll reset the cached information when going to sleep. 1188d3132e4bSVlastimil Babka */ 1189d3132e4bSVlastimil Babka if (compaction_restarting(zone, cc->order) && !current_is_kswapd()) 1190d3132e4bSVlastimil Babka __reset_isolation_suitable(zone); 1191d3132e4bSVlastimil Babka 1192d3132e4bSVlastimil Babka /* 1193c89511abSMel Gorman * Setup to move all movable pages to the end of the zone. Used cached 1194c89511abSMel Gorman * information on where the scanners should start but check that it 1195c89511abSMel Gorman * is initialised by ensuring the values are within zone boundaries. 1196c89511abSMel Gorman */ 1197e0b9daebSDavid Rientjes cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; 1198c89511abSMel Gorman cc->free_pfn = zone->compact_cached_free_pfn; 1199c89511abSMel Gorman if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) { 1200c89511abSMel Gorman cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1); 1201c89511abSMel Gorman zone->compact_cached_free_pfn = cc->free_pfn; 1202c89511abSMel Gorman } 1203c89511abSMel Gorman if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) { 1204c89511abSMel Gorman cc->migrate_pfn = start_pfn; 120535979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; 120635979ef3SDavid Rientjes zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; 1207c89511abSMel Gorman } 1208748446bbSMel Gorman 1209*16c4a097SJoonsoo Kim trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, 1210*16c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync); 12110eb927c0SMel Gorman 1212748446bbSMel Gorman migrate_prep_local(); 1213748446bbSMel Gorman 12146d7ce559SDavid Rientjes while ((ret = compact_finished(zone, cc, migratetype)) == 12156d7ce559SDavid Rientjes COMPACT_CONTINUE) { 12169d502c1cSMinchan Kim int err; 1217fdaf7f5cSVlastimil Babka unsigned long isolate_start_pfn = cc->migrate_pfn; 1218748446bbSMel Gorman 1219f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 1220f9e35b3bSMel Gorman case ISOLATE_ABORT: 1221f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 12225733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 1223e64c5237SShaohua Li cc->nr_migratepages = 0; 1224f9e35b3bSMel Gorman goto out; 1225f9e35b3bSMel Gorman case ISOLATE_NONE: 1226fdaf7f5cSVlastimil Babka /* 1227fdaf7f5cSVlastimil Babka * We haven't isolated and migrated anything, but 1228fdaf7f5cSVlastimil Babka * there might still be unflushed migrations from 1229fdaf7f5cSVlastimil Babka * previous cc->order aligned block. 1230fdaf7f5cSVlastimil Babka */ 1231fdaf7f5cSVlastimil Babka goto check_drain; 1232f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 1233f9e35b3bSMel Gorman ; 1234f9e35b3bSMel Gorman } 1235748446bbSMel Gorman 1236d53aea3dSDavid Rientjes err = migrate_pages(&cc->migratepages, compaction_alloc, 1237e0b9daebSDavid Rientjes compaction_free, (unsigned long)cc, cc->mode, 12387b2a2d4aSMel Gorman MR_COMPACTION); 1239748446bbSMel Gorman 1240f8c9301fSVlastimil Babka trace_mm_compaction_migratepages(cc->nr_migratepages, err, 1241f8c9301fSVlastimil Babka &cc->migratepages); 1242748446bbSMel Gorman 1243f8c9301fSVlastimil Babka /* All pages were either migrated or will be released */ 1244f8c9301fSVlastimil Babka cc->nr_migratepages = 0; 12459d502c1cSMinchan Kim if (err) { 12465733c7d1SRafael Aquini putback_movable_pages(&cc->migratepages); 12477ed695e0SVlastimil Babka /* 12487ed695e0SVlastimil Babka * migrate_pages() may return -ENOMEM when scanners meet 12497ed695e0SVlastimil Babka * and we want compact_finished() to detect it 12507ed695e0SVlastimil Babka */ 12517ed695e0SVlastimil Babka if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) { 12524bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 12534bf2bba3SDavid Rientjes goto out; 1254748446bbSMel Gorman } 12554bf2bba3SDavid Rientjes } 1256fdaf7f5cSVlastimil Babka 1257fdaf7f5cSVlastimil Babka /* 1258fdaf7f5cSVlastimil Babka * Record where we could have freed pages by migration and not 1259fdaf7f5cSVlastimil Babka * yet flushed them to buddy allocator. We use the pfn that 1260fdaf7f5cSVlastimil Babka * isolate_migratepages() started from in this loop iteration 1261fdaf7f5cSVlastimil Babka * - this is the lowest page that could have been isolated and 1262fdaf7f5cSVlastimil Babka * then freed by migration. 1263fdaf7f5cSVlastimil Babka */ 1264fdaf7f5cSVlastimil Babka if (!last_migrated_pfn) 1265fdaf7f5cSVlastimil Babka last_migrated_pfn = isolate_start_pfn; 1266fdaf7f5cSVlastimil Babka 1267fdaf7f5cSVlastimil Babka check_drain: 1268fdaf7f5cSVlastimil Babka /* 1269fdaf7f5cSVlastimil Babka * Has the migration scanner moved away from the previous 1270fdaf7f5cSVlastimil Babka * cc->order aligned block where we migrated from? If yes, 1271fdaf7f5cSVlastimil Babka * flush the pages that were freed, so that they can merge and 1272fdaf7f5cSVlastimil Babka * compact_finished() can detect immediately if allocation 1273fdaf7f5cSVlastimil Babka * would succeed. 1274fdaf7f5cSVlastimil Babka */ 1275fdaf7f5cSVlastimil Babka if (cc->order > 0 && last_migrated_pfn) { 1276fdaf7f5cSVlastimil Babka int cpu; 1277fdaf7f5cSVlastimil Babka unsigned long current_block_start = 1278fdaf7f5cSVlastimil Babka cc->migrate_pfn & ~((1UL << cc->order) - 1); 1279fdaf7f5cSVlastimil Babka 1280fdaf7f5cSVlastimil Babka if (last_migrated_pfn < current_block_start) { 1281fdaf7f5cSVlastimil Babka cpu = get_cpu(); 1282fdaf7f5cSVlastimil Babka lru_add_drain_cpu(cpu); 1283fdaf7f5cSVlastimil Babka drain_local_pages(zone); 1284fdaf7f5cSVlastimil Babka put_cpu(); 1285fdaf7f5cSVlastimil Babka /* No more flushing until we migrate again */ 1286fdaf7f5cSVlastimil Babka last_migrated_pfn = 0; 1287fdaf7f5cSVlastimil Babka } 1288fdaf7f5cSVlastimil Babka } 1289fdaf7f5cSVlastimil Babka 1290748446bbSMel Gorman } 1291748446bbSMel Gorman 1292f9e35b3bSMel Gorman out: 12936bace090SVlastimil Babka /* 12946bace090SVlastimil Babka * Release free pages and update where the free scanner should restart, 12956bace090SVlastimil Babka * so we don't leave any returned pages behind in the next attempt. 12966bace090SVlastimil Babka */ 12976bace090SVlastimil Babka if (cc->nr_freepages > 0) { 12986bace090SVlastimil Babka unsigned long free_pfn = release_freepages(&cc->freepages); 12996bace090SVlastimil Babka 13006bace090SVlastimil Babka cc->nr_freepages = 0; 13016bace090SVlastimil Babka VM_BUG_ON(free_pfn == 0); 13026bace090SVlastimil Babka /* The cached pfn is always the first in a pageblock */ 13036bace090SVlastimil Babka free_pfn &= ~(pageblock_nr_pages-1); 13046bace090SVlastimil Babka /* 13056bace090SVlastimil Babka * Only go back, not forward. The cached pfn might have been 13066bace090SVlastimil Babka * already reset to zone end in compact_finished() 13076bace090SVlastimil Babka */ 13086bace090SVlastimil Babka if (free_pfn > zone->compact_cached_free_pfn) 13096bace090SVlastimil Babka zone->compact_cached_free_pfn = free_pfn; 13106bace090SVlastimil Babka } 1311748446bbSMel Gorman 1312*16c4a097SJoonsoo Kim trace_mm_compaction_end(start_pfn, cc->migrate_pfn, 1313*16c4a097SJoonsoo Kim cc->free_pfn, end_pfn, sync, ret); 13140eb927c0SMel Gorman 1315748446bbSMel Gorman return ret; 1316748446bbSMel Gorman } 131776ab0f53SMel Gorman 1318e0b9daebSDavid Rientjes static unsigned long compact_zone_order(struct zone *zone, int order, 1319ebff3980SVlastimil Babka gfp_t gfp_mask, enum migrate_mode mode, int *contended, 1320ebff3980SVlastimil Babka int alloc_flags, int classzone_idx) 132156de7263SMel Gorman { 1322e64c5237SShaohua Li unsigned long ret; 132356de7263SMel Gorman struct compact_control cc = { 132456de7263SMel Gorman .nr_freepages = 0, 132556de7263SMel Gorman .nr_migratepages = 0, 132656de7263SMel Gorman .order = order, 13276d7ce559SDavid Rientjes .gfp_mask = gfp_mask, 132856de7263SMel Gorman .zone = zone, 1329e0b9daebSDavid Rientjes .mode = mode, 1330ebff3980SVlastimil Babka .alloc_flags = alloc_flags, 1331ebff3980SVlastimil Babka .classzone_idx = classzone_idx, 133256de7263SMel Gorman }; 133356de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 133456de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 133556de7263SMel Gorman 1336e64c5237SShaohua Li ret = compact_zone(zone, &cc); 1337e64c5237SShaohua Li 1338e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 1339e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 1340e64c5237SShaohua Li 1341e64c5237SShaohua Li *contended = cc.contended; 1342e64c5237SShaohua Li return ret; 134356de7263SMel Gorman } 134456de7263SMel Gorman 13455e771905SMel Gorman int sysctl_extfrag_threshold = 500; 13465e771905SMel Gorman 134756de7263SMel Gorman /** 134856de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 134956de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 13501a6d53a1SVlastimil Babka * @order: The order of the current allocation 13511a6d53a1SVlastimil Babka * @alloc_flags: The allocation flags of the current allocation 13521a6d53a1SVlastimil Babka * @ac: The context of current allocation 1353e0b9daebSDavid Rientjes * @mode: The migration mode for async, sync light, or sync migration 13541f9efdefSVlastimil Babka * @contended: Return value that determines if compaction was aborted due to 13551f9efdefSVlastimil Babka * need_resched() or lock contention 135656de7263SMel Gorman * 135756de7263SMel Gorman * This is the main entry point for direct page compaction. 135856de7263SMel Gorman */ 13591a6d53a1SVlastimil Babka unsigned long try_to_compact_pages(gfp_t gfp_mask, unsigned int order, 13601a6d53a1SVlastimil Babka int alloc_flags, const struct alloc_context *ac, 13611a6d53a1SVlastimil Babka enum migrate_mode mode, int *contended) 136256de7263SMel Gorman { 136356de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 136456de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 136556de7263SMel Gorman struct zoneref *z; 136656de7263SMel Gorman struct zone *zone; 136753853e2dSVlastimil Babka int rc = COMPACT_DEFERRED; 13681f9efdefSVlastimil Babka int all_zones_contended = COMPACT_CONTENDED_LOCK; /* init for &= op */ 13691f9efdefSVlastimil Babka 13701f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_NONE; 137156de7263SMel Gorman 13724ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 1373c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 137453853e2dSVlastimil Babka return COMPACT_SKIPPED; 137556de7263SMel Gorman 137656de7263SMel Gorman /* Compact each zone in the list */ 13771a6d53a1SVlastimil Babka for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, 13781a6d53a1SVlastimil Babka ac->nodemask) { 137956de7263SMel Gorman int status; 13801f9efdefSVlastimil Babka int zone_contended; 138156de7263SMel Gorman 138253853e2dSVlastimil Babka if (compaction_deferred(zone, order)) 138353853e2dSVlastimil Babka continue; 138453853e2dSVlastimil Babka 1385e0b9daebSDavid Rientjes status = compact_zone_order(zone, order, gfp_mask, mode, 13861a6d53a1SVlastimil Babka &zone_contended, alloc_flags, 13871a6d53a1SVlastimil Babka ac->classzone_idx); 138856de7263SMel Gorman rc = max(status, rc); 13891f9efdefSVlastimil Babka /* 13901f9efdefSVlastimil Babka * It takes at least one zone that wasn't lock contended 13911f9efdefSVlastimil Babka * to clear all_zones_contended. 13921f9efdefSVlastimil Babka */ 13931f9efdefSVlastimil Babka all_zones_contended &= zone_contended; 139456de7263SMel Gorman 13953e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 1396ebff3980SVlastimil Babka if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 13971a6d53a1SVlastimil Babka ac->classzone_idx, alloc_flags)) { 139853853e2dSVlastimil Babka /* 139953853e2dSVlastimil Babka * We think the allocation will succeed in this zone, 140053853e2dSVlastimil Babka * but it is not certain, hence the false. The caller 140153853e2dSVlastimil Babka * will repeat this with true if allocation indeed 140253853e2dSVlastimil Babka * succeeds in this zone. 140353853e2dSVlastimil Babka */ 140453853e2dSVlastimil Babka compaction_defer_reset(zone, order, false); 14051f9efdefSVlastimil Babka /* 14061f9efdefSVlastimil Babka * It is possible that async compaction aborted due to 14071f9efdefSVlastimil Babka * need_resched() and the watermarks were ok thanks to 14081f9efdefSVlastimil Babka * somebody else freeing memory. The allocation can 14091f9efdefSVlastimil Babka * however still fail so we better signal the 14101f9efdefSVlastimil Babka * need_resched() contention anyway (this will not 14111f9efdefSVlastimil Babka * prevent the allocation attempt). 14121f9efdefSVlastimil Babka */ 14131f9efdefSVlastimil Babka if (zone_contended == COMPACT_CONTENDED_SCHED) 14141f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 14151f9efdefSVlastimil Babka 14161f9efdefSVlastimil Babka goto break_loop; 14171f9efdefSVlastimil Babka } 14181f9efdefSVlastimil Babka 1419f8669795SVlastimil Babka if (mode != MIGRATE_ASYNC && status == COMPACT_COMPLETE) { 142053853e2dSVlastimil Babka /* 142153853e2dSVlastimil Babka * We think that allocation won't succeed in this zone 142253853e2dSVlastimil Babka * so we defer compaction there. If it ends up 142353853e2dSVlastimil Babka * succeeding after all, it will be reset. 142453853e2dSVlastimil Babka */ 142553853e2dSVlastimil Babka defer_compaction(zone, order); 142653853e2dSVlastimil Babka } 14271f9efdefSVlastimil Babka 14281f9efdefSVlastimil Babka /* 14291f9efdefSVlastimil Babka * We might have stopped compacting due to need_resched() in 14301f9efdefSVlastimil Babka * async compaction, or due to a fatal signal detected. In that 14311f9efdefSVlastimil Babka * case do not try further zones and signal need_resched() 14321f9efdefSVlastimil Babka * contention. 14331f9efdefSVlastimil Babka */ 14341f9efdefSVlastimil Babka if ((zone_contended == COMPACT_CONTENDED_SCHED) 14351f9efdefSVlastimil Babka || fatal_signal_pending(current)) { 14361f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_SCHED; 14371f9efdefSVlastimil Babka goto break_loop; 143856de7263SMel Gorman } 143956de7263SMel Gorman 14401f9efdefSVlastimil Babka continue; 14411f9efdefSVlastimil Babka break_loop: 14421f9efdefSVlastimil Babka /* 14431f9efdefSVlastimil Babka * We might not have tried all the zones, so be conservative 14441f9efdefSVlastimil Babka * and assume they are not all lock contended. 14451f9efdefSVlastimil Babka */ 14461f9efdefSVlastimil Babka all_zones_contended = 0; 14471f9efdefSVlastimil Babka break; 14481f9efdefSVlastimil Babka } 14491f9efdefSVlastimil Babka 14501f9efdefSVlastimil Babka /* 14511f9efdefSVlastimil Babka * If at least one zone wasn't deferred or skipped, we report if all 14521f9efdefSVlastimil Babka * zones that were tried were lock contended. 14531f9efdefSVlastimil Babka */ 14541f9efdefSVlastimil Babka if (rc > COMPACT_SKIPPED && all_zones_contended) 14551f9efdefSVlastimil Babka *contended = COMPACT_CONTENDED_LOCK; 14561f9efdefSVlastimil Babka 145756de7263SMel Gorman return rc; 145856de7263SMel Gorman } 145956de7263SMel Gorman 146056de7263SMel Gorman 146176ab0f53SMel Gorman /* Compact all zones within a node */ 14627103f16dSAndrew Morton static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 146376ab0f53SMel Gorman { 146476ab0f53SMel Gorman int zoneid; 146576ab0f53SMel Gorman struct zone *zone; 146676ab0f53SMel Gorman 146776ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 146876ab0f53SMel Gorman 146976ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 147076ab0f53SMel Gorman if (!populated_zone(zone)) 147176ab0f53SMel Gorman continue; 147276ab0f53SMel Gorman 14737be62de9SRik van Riel cc->nr_freepages = 0; 14747be62de9SRik van Riel cc->nr_migratepages = 0; 14757be62de9SRik van Riel cc->zone = zone; 14767be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 14777be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 147876ab0f53SMel Gorman 1479aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 14807be62de9SRik van Riel compact_zone(zone, cc); 148176ab0f53SMel Gorman 1482aff62249SRik van Riel if (cc->order > 0) { 1483de6c60a6SVlastimil Babka if (zone_watermark_ok(zone, cc->order, 1484de6c60a6SVlastimil Babka low_wmark_pages(zone), 0, 0)) 1485de6c60a6SVlastimil Babka compaction_defer_reset(zone, cc->order, false); 1486aff62249SRik van Riel } 1487aff62249SRik van Riel 14887be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 14897be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 149076ab0f53SMel Gorman } 149176ab0f53SMel Gorman } 149276ab0f53SMel Gorman 14937103f16dSAndrew Morton void compact_pgdat(pg_data_t *pgdat, int order) 14947be62de9SRik van Riel { 14957be62de9SRik van Riel struct compact_control cc = { 14967be62de9SRik van Riel .order = order, 1497e0b9daebSDavid Rientjes .mode = MIGRATE_ASYNC, 14987be62de9SRik van Riel }; 14997be62de9SRik van Riel 15003a7200afSMel Gorman if (!order) 15013a7200afSMel Gorman return; 15023a7200afSMel Gorman 15037103f16dSAndrew Morton __compact_pgdat(pgdat, &cc); 15047be62de9SRik van Riel } 15057be62de9SRik van Riel 15067103f16dSAndrew Morton static void compact_node(int nid) 15077be62de9SRik van Riel { 15087be62de9SRik van Riel struct compact_control cc = { 15097be62de9SRik van Riel .order = -1, 1510e0b9daebSDavid Rientjes .mode = MIGRATE_SYNC, 151191ca9186SDavid Rientjes .ignore_skip_hint = true, 15127be62de9SRik van Riel }; 15137be62de9SRik van Riel 15147103f16dSAndrew Morton __compact_pgdat(NODE_DATA(nid), &cc); 15157be62de9SRik van Riel } 15167be62de9SRik van Riel 151776ab0f53SMel Gorman /* Compact all nodes in the system */ 15187964c06dSJason Liu static void compact_nodes(void) 151976ab0f53SMel Gorman { 152076ab0f53SMel Gorman int nid; 152176ab0f53SMel Gorman 15228575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 15238575ec29SHugh Dickins lru_add_drain_all(); 15248575ec29SHugh Dickins 152576ab0f53SMel Gorman for_each_online_node(nid) 152676ab0f53SMel Gorman compact_node(nid); 152776ab0f53SMel Gorman } 152876ab0f53SMel Gorman 152976ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 153076ab0f53SMel Gorman int sysctl_compact_memory; 153176ab0f53SMel Gorman 153276ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 153376ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 153476ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 153576ab0f53SMel Gorman { 153676ab0f53SMel Gorman if (write) 15377964c06dSJason Liu compact_nodes(); 153876ab0f53SMel Gorman 153976ab0f53SMel Gorman return 0; 154076ab0f53SMel Gorman } 1541ed4a6d7fSMel Gorman 15425e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 15435e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 15445e771905SMel Gorman { 15455e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 15465e771905SMel Gorman 15475e771905SMel Gorman return 0; 15485e771905SMel Gorman } 15495e771905SMel Gorman 1550ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 155174e77fb9SRashika Kheria static ssize_t sysfs_compact_node(struct device *dev, 155210fbcf4cSKay Sievers struct device_attribute *attr, 1553ed4a6d7fSMel Gorman const char *buf, size_t count) 1554ed4a6d7fSMel Gorman { 15558575ec29SHugh Dickins int nid = dev->id; 15568575ec29SHugh Dickins 15578575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 15588575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 15598575ec29SHugh Dickins lru_add_drain_all(); 15608575ec29SHugh Dickins 15618575ec29SHugh Dickins compact_node(nid); 15628575ec29SHugh Dickins } 1563ed4a6d7fSMel Gorman 1564ed4a6d7fSMel Gorman return count; 1565ed4a6d7fSMel Gorman } 156610fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1567ed4a6d7fSMel Gorman 1568ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1569ed4a6d7fSMel Gorman { 157010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1571ed4a6d7fSMel Gorman } 1572ed4a6d7fSMel Gorman 1573ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1574ed4a6d7fSMel Gorman { 157510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1576ed4a6d7fSMel Gorman } 1577ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1578ff9543fdSMichal Nazarewicz 1579ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1580