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> 17748446bbSMel Gorman #include "internal.h" 18748446bbSMel Gorman 19ff9543fdSMichal Nazarewicz #if defined CONFIG_COMPACTION || defined CONFIG_CMA 20ff9543fdSMichal Nazarewicz 21b7aba698SMel Gorman #define CREATE_TRACE_POINTS 22b7aba698SMel Gorman #include <trace/events/compaction.h> 23b7aba698SMel Gorman 24748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 25748446bbSMel Gorman { 26748446bbSMel Gorman struct page *page, *next; 27748446bbSMel Gorman unsigned long count = 0; 28748446bbSMel Gorman 29748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 30748446bbSMel Gorman list_del(&page->lru); 31748446bbSMel Gorman __free_page(page); 32748446bbSMel Gorman count++; 33748446bbSMel Gorman } 34748446bbSMel Gorman 35748446bbSMel Gorman return count; 36748446bbSMel Gorman } 37748446bbSMel Gorman 38ff9543fdSMichal Nazarewicz static void map_pages(struct list_head *list) 39ff9543fdSMichal Nazarewicz { 40ff9543fdSMichal Nazarewicz struct page *page; 41ff9543fdSMichal Nazarewicz 42ff9543fdSMichal Nazarewicz list_for_each_entry(page, list, lru) { 43ff9543fdSMichal Nazarewicz arch_alloc_page(page, 0); 44ff9543fdSMichal Nazarewicz kernel_map_pages(page, 1, 1); 45ff9543fdSMichal Nazarewicz } 46ff9543fdSMichal Nazarewicz } 47ff9543fdSMichal Nazarewicz 4847118af0SMichal Nazarewicz static inline bool migrate_async_suitable(int migratetype) 4947118af0SMichal Nazarewicz { 5047118af0SMichal Nazarewicz return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE; 5147118af0SMichal Nazarewicz } 5247118af0SMichal Nazarewicz 53*2a1402aaSMel Gorman static inline bool should_release_lock(spinlock_t *lock) 54*2a1402aaSMel Gorman { 55*2a1402aaSMel Gorman return need_resched() || spin_is_contended(lock); 56*2a1402aaSMel Gorman } 57*2a1402aaSMel Gorman 5885aa125fSMichal Nazarewicz /* 59c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 60c67fe375SMel Gorman * very heavily contended. Check if the process needs to be scheduled or 61c67fe375SMel Gorman * if the lock is contended. For async compaction, back out in the event 62c67fe375SMel Gorman * if contention is severe. For sync compaction, schedule. 63c67fe375SMel Gorman * 64c67fe375SMel Gorman * Returns true if the lock is held. 65c67fe375SMel Gorman * Returns false if the lock is released and compaction should abort 66c67fe375SMel Gorman */ 67c67fe375SMel Gorman static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, 68c67fe375SMel Gorman bool locked, struct compact_control *cc) 69c67fe375SMel Gorman { 70*2a1402aaSMel Gorman if (should_release_lock(lock)) { 71c67fe375SMel Gorman if (locked) { 72c67fe375SMel Gorman spin_unlock_irqrestore(lock, *flags); 73c67fe375SMel Gorman locked = false; 74c67fe375SMel Gorman } 75c67fe375SMel Gorman 76c67fe375SMel Gorman /* async aborts if taking too long or contended */ 77c67fe375SMel Gorman if (!cc->sync) { 78e64c5237SShaohua Li cc->contended = true; 79c67fe375SMel Gorman return false; 80c67fe375SMel Gorman } 81c67fe375SMel Gorman 82c67fe375SMel Gorman cond_resched(); 83c67fe375SMel Gorman } 84c67fe375SMel Gorman 85c67fe375SMel Gorman if (!locked) 86c67fe375SMel Gorman spin_lock_irqsave(lock, *flags); 87c67fe375SMel Gorman return true; 88c67fe375SMel Gorman } 89c67fe375SMel Gorman 90c67fe375SMel Gorman static inline bool compact_trylock_irqsave(spinlock_t *lock, 91c67fe375SMel Gorman unsigned long *flags, struct compact_control *cc) 92c67fe375SMel Gorman { 93c67fe375SMel Gorman return compact_checklock_irqsave(lock, flags, false, cc); 94c67fe375SMel Gorman } 95c67fe375SMel Gorman 961fb3f8caSMel Gorman static void compact_capture_page(struct compact_control *cc) 971fb3f8caSMel Gorman { 981fb3f8caSMel Gorman unsigned long flags; 991fb3f8caSMel Gorman int mtype, mtype_low, mtype_high; 1001fb3f8caSMel Gorman 1011fb3f8caSMel Gorman if (!cc->page || *cc->page) 1021fb3f8caSMel Gorman return; 1031fb3f8caSMel Gorman 1041fb3f8caSMel Gorman /* 1051fb3f8caSMel Gorman * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP 1061fb3f8caSMel Gorman * regardless of the migratetype of the freelist is is captured from. 1071fb3f8caSMel Gorman * This is fine because the order for a high-order MIGRATE_MOVABLE 1081fb3f8caSMel Gorman * allocation is typically at least a pageblock size and overall 1091fb3f8caSMel Gorman * fragmentation is not impaired. Other allocation types must 1101fb3f8caSMel Gorman * capture pages from their own migratelist because otherwise they 1111fb3f8caSMel Gorman * could pollute other pageblocks like MIGRATE_MOVABLE with 1121fb3f8caSMel Gorman * difficult to move pages and making fragmentation worse overall. 1131fb3f8caSMel Gorman */ 1141fb3f8caSMel Gorman if (cc->migratetype == MIGRATE_MOVABLE) { 1151fb3f8caSMel Gorman mtype_low = 0; 1161fb3f8caSMel Gorman mtype_high = MIGRATE_PCPTYPES; 1171fb3f8caSMel Gorman } else { 1181fb3f8caSMel Gorman mtype_low = cc->migratetype; 1191fb3f8caSMel Gorman mtype_high = cc->migratetype + 1; 1201fb3f8caSMel Gorman } 1211fb3f8caSMel Gorman 1221fb3f8caSMel Gorman /* Speculatively examine the free lists without zone lock */ 1231fb3f8caSMel Gorman for (mtype = mtype_low; mtype < mtype_high; mtype++) { 1241fb3f8caSMel Gorman int order; 1251fb3f8caSMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 1261fb3f8caSMel Gorman struct page *page; 1271fb3f8caSMel Gorman struct free_area *area; 1281fb3f8caSMel Gorman area = &(cc->zone->free_area[order]); 1291fb3f8caSMel Gorman if (list_empty(&area->free_list[mtype])) 1301fb3f8caSMel Gorman continue; 1311fb3f8caSMel Gorman 1321fb3f8caSMel Gorman /* Take the lock and attempt capture of the page */ 1331fb3f8caSMel Gorman if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc)) 1341fb3f8caSMel Gorman return; 1351fb3f8caSMel Gorman if (!list_empty(&area->free_list[mtype])) { 1361fb3f8caSMel Gorman page = list_entry(area->free_list[mtype].next, 1371fb3f8caSMel Gorman struct page, lru); 1381fb3f8caSMel Gorman if (capture_free_page(page, cc->order, mtype)) { 1391fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, 1401fb3f8caSMel Gorman flags); 1411fb3f8caSMel Gorman *cc->page = page; 1421fb3f8caSMel Gorman return; 1431fb3f8caSMel Gorman } 1441fb3f8caSMel Gorman } 1451fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 1461fb3f8caSMel Gorman } 1471fb3f8caSMel Gorman } 1481fb3f8caSMel Gorman } 1491fb3f8caSMel Gorman 150c67fe375SMel Gorman /* 15185aa125fSMichal Nazarewicz * Isolate free pages onto a private freelist. Caller must hold zone->lock. 15285aa125fSMichal Nazarewicz * If @strict is true, will abort returning 0 on any invalid PFNs or non-free 15385aa125fSMichal Nazarewicz * pages inside of the pageblock (even though it may still end up isolating 15485aa125fSMichal Nazarewicz * some pages). 15585aa125fSMichal Nazarewicz */ 15685aa125fSMichal Nazarewicz static unsigned long isolate_freepages_block(unsigned long blockpfn, 15785aa125fSMichal Nazarewicz unsigned long end_pfn, 15885aa125fSMichal Nazarewicz struct list_head *freelist, 15985aa125fSMichal Nazarewicz bool strict) 160748446bbSMel Gorman { 161b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 162748446bbSMel Gorman struct page *cursor; 163748446bbSMel Gorman 164748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 165748446bbSMel Gorman 166748446bbSMel Gorman /* Isolate free pages. This assumes the block is valid */ 167748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 168748446bbSMel Gorman int isolated, i; 169748446bbSMel Gorman struct page *page = cursor; 170748446bbSMel Gorman 17185aa125fSMichal Nazarewicz if (!pfn_valid_within(blockpfn)) { 17285aa125fSMichal Nazarewicz if (strict) 17385aa125fSMichal Nazarewicz return 0; 174748446bbSMel Gorman continue; 17585aa125fSMichal Nazarewicz } 176b7aba698SMel Gorman nr_scanned++; 177748446bbSMel Gorman 17885aa125fSMichal Nazarewicz if (!PageBuddy(page)) { 17985aa125fSMichal Nazarewicz if (strict) 18085aa125fSMichal Nazarewicz return 0; 181748446bbSMel Gorman continue; 18285aa125fSMichal Nazarewicz } 183748446bbSMel Gorman 184748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 185748446bbSMel Gorman isolated = split_free_page(page); 18685aa125fSMichal Nazarewicz if (!isolated && strict) 18785aa125fSMichal Nazarewicz return 0; 188748446bbSMel Gorman total_isolated += isolated; 189748446bbSMel Gorman for (i = 0; i < isolated; i++) { 190748446bbSMel Gorman list_add(&page->lru, freelist); 191748446bbSMel Gorman page++; 192748446bbSMel Gorman } 193748446bbSMel Gorman 194748446bbSMel Gorman /* If a page was split, advance to the end of it */ 195748446bbSMel Gorman if (isolated) { 196748446bbSMel Gorman blockpfn += isolated - 1; 197748446bbSMel Gorman cursor += isolated - 1; 198748446bbSMel Gorman } 199748446bbSMel Gorman } 200748446bbSMel Gorman 201b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 202748446bbSMel Gorman return total_isolated; 203748446bbSMel Gorman } 204748446bbSMel Gorman 20585aa125fSMichal Nazarewicz /** 20685aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 20785aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 20885aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 20985aa125fSMichal Nazarewicz * 21085aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 21185aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 21285aa125fSMichal Nazarewicz * undo its actions and return zero. 21385aa125fSMichal Nazarewicz * 21485aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 21585aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 21685aa125fSMichal Nazarewicz * a free page). 21785aa125fSMichal Nazarewicz */ 218ff9543fdSMichal Nazarewicz unsigned long 21985aa125fSMichal Nazarewicz isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn) 22085aa125fSMichal Nazarewicz { 22185aa125fSMichal Nazarewicz unsigned long isolated, pfn, block_end_pfn, flags; 22285aa125fSMichal Nazarewicz struct zone *zone = NULL; 22385aa125fSMichal Nazarewicz LIST_HEAD(freelist); 22485aa125fSMichal Nazarewicz 22585aa125fSMichal Nazarewicz if (pfn_valid(start_pfn)) 22685aa125fSMichal Nazarewicz zone = page_zone(pfn_to_page(start_pfn)); 22785aa125fSMichal Nazarewicz 22885aa125fSMichal Nazarewicz for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) { 22985aa125fSMichal Nazarewicz if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn))) 23085aa125fSMichal Nazarewicz break; 23185aa125fSMichal Nazarewicz 23285aa125fSMichal Nazarewicz /* 23385aa125fSMichal Nazarewicz * On subsequent iterations ALIGN() is actually not needed, 23485aa125fSMichal Nazarewicz * but we keep it that we not to complicate the code. 23585aa125fSMichal Nazarewicz */ 23685aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 23785aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 23885aa125fSMichal Nazarewicz 23985aa125fSMichal Nazarewicz spin_lock_irqsave(&zone->lock, flags); 24085aa125fSMichal Nazarewicz isolated = isolate_freepages_block(pfn, block_end_pfn, 24185aa125fSMichal Nazarewicz &freelist, true); 24285aa125fSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 24385aa125fSMichal Nazarewicz 24485aa125fSMichal Nazarewicz /* 24585aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 24685aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 24785aa125fSMichal Nazarewicz * non-free pages). 24885aa125fSMichal Nazarewicz */ 24985aa125fSMichal Nazarewicz if (!isolated) 25085aa125fSMichal Nazarewicz break; 25185aa125fSMichal Nazarewicz 25285aa125fSMichal Nazarewicz /* 25385aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 25485aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 25585aa125fSMichal Nazarewicz * page may span two pageblocks). 25685aa125fSMichal Nazarewicz */ 25785aa125fSMichal Nazarewicz } 25885aa125fSMichal Nazarewicz 25985aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 26085aa125fSMichal Nazarewicz map_pages(&freelist); 26185aa125fSMichal Nazarewicz 26285aa125fSMichal Nazarewicz if (pfn < end_pfn) { 26385aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 26485aa125fSMichal Nazarewicz release_freepages(&freelist); 26585aa125fSMichal Nazarewicz return 0; 26685aa125fSMichal Nazarewicz } 26785aa125fSMichal Nazarewicz 26885aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 26985aa125fSMichal Nazarewicz return pfn; 27085aa125fSMichal Nazarewicz } 27185aa125fSMichal Nazarewicz 272748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 273c67fe375SMel Gorman static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc) 274748446bbSMel Gorman { 275748446bbSMel Gorman struct page *page; 276b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 277748446bbSMel Gorman 278b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 279b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 280748446bbSMel Gorman 281c67fe375SMel Gorman /* If locked we can use the interrupt unsafe versions */ 282c67fe375SMel Gorman if (locked) { 283b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 284b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 285c67fe375SMel Gorman } else { 286c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 287c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 288c67fe375SMel Gorman } 289748446bbSMel Gorman } 290748446bbSMel Gorman 291748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 292748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 293748446bbSMel Gorman { 294bc693045SMinchan Kim unsigned long active, inactive, isolated; 295748446bbSMel Gorman 296748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 297748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 298bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 299bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 300748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 301748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 302748446bbSMel Gorman 303bc693045SMinchan Kim return isolated > (inactive + active) / 2; 304748446bbSMel Gorman } 305748446bbSMel Gorman 3062fe86e00SMichal Nazarewicz /** 3072fe86e00SMichal Nazarewicz * isolate_migratepages_range() - isolate all migrate-able pages in range. 3082fe86e00SMichal Nazarewicz * @zone: Zone pages are in. 3092fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 3102fe86e00SMichal Nazarewicz * @low_pfn: The first PFN of the range. 3112fe86e00SMichal Nazarewicz * @end_pfn: The one-past-the-last PFN of the range. 3122fe86e00SMichal Nazarewicz * 3132fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 3142fe86e00SMichal Nazarewicz * [low_pfn, end_pfn). Returns zero if there is a fatal signal 3152fe86e00SMichal Nazarewicz * pending), otherwise PFN of the first page that was not scanned 3162fe86e00SMichal Nazarewicz * (which may be both less, equal to or more then end_pfn). 3172fe86e00SMichal Nazarewicz * 3182fe86e00SMichal Nazarewicz * Assumes that cc->migratepages is empty and cc->nr_migratepages is 3192fe86e00SMichal Nazarewicz * zero. 3202fe86e00SMichal Nazarewicz * 3212fe86e00SMichal Nazarewicz * Apart from cc->migratepages and cc->nr_migratetypes this function 3222fe86e00SMichal Nazarewicz * does not modify any cc's fields, in particular it does not modify 3232fe86e00SMichal Nazarewicz * (or read for that matter) cc->migrate_pfn. 324748446bbSMel Gorman */ 325ff9543fdSMichal Nazarewicz unsigned long 3262fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc, 3272fe86e00SMichal Nazarewicz unsigned long low_pfn, unsigned long end_pfn) 328748446bbSMel Gorman { 3299927af74SMel Gorman unsigned long last_pageblock_nr = 0, pageblock_nr; 330b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 331748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 332f3fd4a61SKonstantin Khlebnikov isolate_mode_t mode = 0; 333fa9add64SHugh Dickins struct lruvec *lruvec; 334c67fe375SMel Gorman unsigned long flags; 335*2a1402aaSMel Gorman bool locked = false; 336748446bbSMel Gorman 337748446bbSMel Gorman /* 338748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 339748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 340748446bbSMel Gorman * delay for some time until fewer pages are isolated 341748446bbSMel Gorman */ 342748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 343f9e35b3bSMel Gorman /* async migration should just abort */ 34468e3e926SLinus Torvalds if (!cc->sync) 3452fe86e00SMichal Nazarewicz return 0; 346f9e35b3bSMel Gorman 347748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 348748446bbSMel Gorman 349748446bbSMel Gorman if (fatal_signal_pending(current)) 3502fe86e00SMichal Nazarewicz return 0; 351748446bbSMel Gorman } 352748446bbSMel Gorman 353748446bbSMel Gorman /* Time to isolate some pages for migration */ 354b2eef8c0SAndrea Arcangeli cond_resched(); 355748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 356748446bbSMel Gorman struct page *page; 357b2eef8c0SAndrea Arcangeli 358b2eef8c0SAndrea Arcangeli /* give a chance to irqs before checking need_resched() */ 359*2a1402aaSMel Gorman if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) { 360*2a1402aaSMel Gorman if (should_release_lock(&zone->lru_lock)) { 361c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 362b2eef8c0SAndrea Arcangeli locked = false; 363b2eef8c0SAndrea Arcangeli } 364*2a1402aaSMel Gorman } 365b2eef8c0SAndrea Arcangeli 3660bf380bcSMel Gorman /* 3670bf380bcSMel Gorman * migrate_pfn does not necessarily start aligned to a 3680bf380bcSMel Gorman * pageblock. Ensure that pfn_valid is called when moving 3690bf380bcSMel Gorman * into a new MAX_ORDER_NR_PAGES range in case of large 3700bf380bcSMel Gorman * memory holes within the zone 3710bf380bcSMel Gorman */ 3720bf380bcSMel Gorman if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { 3730bf380bcSMel Gorman if (!pfn_valid(low_pfn)) { 3740bf380bcSMel Gorman low_pfn += MAX_ORDER_NR_PAGES - 1; 3750bf380bcSMel Gorman continue; 3760bf380bcSMel Gorman } 3770bf380bcSMel Gorman } 3780bf380bcSMel Gorman 379748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 380748446bbSMel Gorman continue; 381b7aba698SMel Gorman nr_scanned++; 382748446bbSMel Gorman 383dc908600SMel Gorman /* 384dc908600SMel Gorman * Get the page and ensure the page is within the same zone. 385dc908600SMel Gorman * See the comment in isolate_freepages about overlapping 386dc908600SMel Gorman * nodes. It is deliberate that the new zone lock is not taken 387dc908600SMel Gorman * as memory compaction should not move pages between nodes. 388dc908600SMel Gorman */ 389748446bbSMel Gorman page = pfn_to_page(low_pfn); 390dc908600SMel Gorman if (page_zone(page) != zone) 391dc908600SMel Gorman continue; 392dc908600SMel Gorman 393dc908600SMel Gorman /* Skip if free */ 394748446bbSMel Gorman if (PageBuddy(page)) 395748446bbSMel Gorman continue; 396748446bbSMel Gorman 3979927af74SMel Gorman /* 3989927af74SMel Gorman * For async migration, also only scan in MOVABLE blocks. Async 3999927af74SMel Gorman * migration is optimistic to see if the minimum amount of work 4009927af74SMel Gorman * satisfies the allocation 4019927af74SMel Gorman */ 4029927af74SMel Gorman pageblock_nr = low_pfn >> pageblock_order; 40368e3e926SLinus Torvalds if (!cc->sync && last_pageblock_nr != pageblock_nr && 40447118af0SMichal Nazarewicz !migrate_async_suitable(get_pageblock_migratetype(page))) { 405*2a1402aaSMel Gorman goto next_pageblock; 4069927af74SMel Gorman } 4079927af74SMel Gorman 408*2a1402aaSMel Gorman /* Check may be lockless but that's ok as we recheck later */ 409bc835011SAndrea Arcangeli if (!PageLRU(page)) 410bc835011SAndrea Arcangeli continue; 411bc835011SAndrea Arcangeli 412bc835011SAndrea Arcangeli /* 413*2a1402aaSMel Gorman * PageLRU is set. lru_lock normally excludes isolation 414*2a1402aaSMel Gorman * splitting and collapsing (collapsing has already happened 415*2a1402aaSMel Gorman * if PageLRU is set) but the lock is not necessarily taken 416*2a1402aaSMel Gorman * here and it is wasteful to take it just to check transhuge. 417*2a1402aaSMel Gorman * Check TransHuge without lock and skip the whole pageblock if 418*2a1402aaSMel Gorman * it's either a transhuge or hugetlbfs page, as calling 419*2a1402aaSMel Gorman * compound_order() without preventing THP from splitting the 420*2a1402aaSMel Gorman * page underneath us may return surprising results. 421bc835011SAndrea Arcangeli */ 422bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 423*2a1402aaSMel Gorman if (!locked) 424*2a1402aaSMel Gorman goto next_pageblock; 425*2a1402aaSMel Gorman low_pfn += (1 << compound_order(page)) - 1; 426*2a1402aaSMel Gorman continue; 427*2a1402aaSMel Gorman } 428*2a1402aaSMel Gorman 429*2a1402aaSMel Gorman /* Check if it is ok to still hold the lock */ 430*2a1402aaSMel Gorman locked = compact_checklock_irqsave(&zone->lru_lock, &flags, 431*2a1402aaSMel Gorman locked, cc); 432*2a1402aaSMel Gorman if (!locked || fatal_signal_pending(current)) 433*2a1402aaSMel Gorman break; 434*2a1402aaSMel Gorman 435*2a1402aaSMel Gorman /* Recheck PageLRU and PageTransHuge under lock */ 436*2a1402aaSMel Gorman if (!PageLRU(page)) 437*2a1402aaSMel Gorman continue; 438*2a1402aaSMel Gorman if (PageTransHuge(page)) { 439bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 440bc835011SAndrea Arcangeli continue; 441bc835011SAndrea Arcangeli } 442bc835011SAndrea Arcangeli 44368e3e926SLinus Torvalds if (!cc->sync) 444c8244935SMel Gorman mode |= ISOLATE_ASYNC_MIGRATE; 445c8244935SMel Gorman 446fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 447fa9add64SHugh Dickins 448748446bbSMel Gorman /* Try isolate the page */ 449f3fd4a61SKonstantin Khlebnikov if (__isolate_lru_page(page, mode) != 0) 450748446bbSMel Gorman continue; 451748446bbSMel Gorman 452bc835011SAndrea Arcangeli VM_BUG_ON(PageTransCompound(page)); 453bc835011SAndrea Arcangeli 454748446bbSMel Gorman /* Successfully isolated */ 455fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 456748446bbSMel Gorman list_add(&page->lru, migratelist); 457748446bbSMel Gorman cc->nr_migratepages++; 458b7aba698SMel Gorman nr_isolated++; 459748446bbSMel Gorman 460748446bbSMel Gorman /* Avoid isolating too much */ 46131b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 46231b8384aSHillf Danton ++low_pfn; 463748446bbSMel Gorman break; 464748446bbSMel Gorman } 465*2a1402aaSMel Gorman 466*2a1402aaSMel Gorman continue; 467*2a1402aaSMel Gorman 468*2a1402aaSMel Gorman next_pageblock: 469*2a1402aaSMel Gorman low_pfn += pageblock_nr_pages; 470*2a1402aaSMel Gorman low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 471*2a1402aaSMel Gorman last_pageblock_nr = pageblock_nr; 47231b8384aSHillf Danton } 473748446bbSMel Gorman 474c67fe375SMel Gorman acct_isolated(zone, locked, cc); 475748446bbSMel Gorman 476c67fe375SMel Gorman if (locked) 477c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 478748446bbSMel Gorman 479b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 480b7aba698SMel Gorman 4812fe86e00SMichal Nazarewicz return low_pfn; 4822fe86e00SMichal Nazarewicz } 4832fe86e00SMichal Nazarewicz 484ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 485ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 486ff9543fdSMichal Nazarewicz 48768e3e926SLinus Torvalds /* Returns true if the page is within a block suitable for migration to */ 48868e3e926SLinus Torvalds static bool suitable_migration_target(struct page *page) 4892fe86e00SMichal Nazarewicz { 4902fe86e00SMichal Nazarewicz 491ff9543fdSMichal Nazarewicz int migratetype = get_pageblock_migratetype(page); 4922fe86e00SMichal Nazarewicz 493ff9543fdSMichal Nazarewicz /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 494ff9543fdSMichal Nazarewicz if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 49568e3e926SLinus Torvalds return false; 4962fe86e00SMichal Nazarewicz 497ff9543fdSMichal Nazarewicz /* If the page is a large free page, then allow migration */ 498ff9543fdSMichal Nazarewicz if (PageBuddy(page) && page_order(page) >= pageblock_order) 49968e3e926SLinus Torvalds return true; 500ff9543fdSMichal Nazarewicz 50147118af0SMichal Nazarewicz /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 50268e3e926SLinus Torvalds if (migrate_async_suitable(migratetype)) 50368e3e926SLinus Torvalds return true; 504ff9543fdSMichal Nazarewicz 505ff9543fdSMichal Nazarewicz /* Otherwise skip the block */ 50668e3e926SLinus Torvalds return false; 5072fe86e00SMichal Nazarewicz } 5082fe86e00SMichal Nazarewicz 509ff9543fdSMichal Nazarewicz /* 510de74f1ccSMel Gorman * Returns the start pfn of the last page block in a zone. This is the starting 511de74f1ccSMel Gorman * point for full compaction of a zone. Compaction searches for free pages from 512de74f1ccSMel Gorman * the end of each zone, while isolate_freepages_block scans forward inside each 513de74f1ccSMel Gorman * page block. 514de74f1ccSMel Gorman */ 515de74f1ccSMel Gorman static unsigned long start_free_pfn(struct zone *zone) 516de74f1ccSMel Gorman { 517de74f1ccSMel Gorman unsigned long free_pfn; 518de74f1ccSMel Gorman free_pfn = zone->zone_start_pfn + zone->spanned_pages; 519de74f1ccSMel Gorman free_pfn &= ~(pageblock_nr_pages-1); 520de74f1ccSMel Gorman return free_pfn; 521de74f1ccSMel Gorman } 522de74f1ccSMel Gorman 523de74f1ccSMel Gorman /* 524ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 525ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 526ff9543fdSMichal Nazarewicz */ 527ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone, 528ff9543fdSMichal Nazarewicz struct compact_control *cc) 529ff9543fdSMichal Nazarewicz { 530ff9543fdSMichal Nazarewicz struct page *page; 531ff9543fdSMichal Nazarewicz unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; 532ff9543fdSMichal Nazarewicz unsigned long flags; 533ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 534ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 5352fe86e00SMichal Nazarewicz 536ff9543fdSMichal Nazarewicz /* 537ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 538ff9543fdSMichal Nazarewicz * scanned from (or the end of the zone if starting). The low point 539ff9543fdSMichal Nazarewicz * is the end of the pageblock the migration scanner is using. 540ff9543fdSMichal Nazarewicz */ 541ff9543fdSMichal Nazarewicz pfn = cc->free_pfn; 542ff9543fdSMichal Nazarewicz low_pfn = cc->migrate_pfn + pageblock_nr_pages; 5432fe86e00SMichal Nazarewicz 544ff9543fdSMichal Nazarewicz /* 545ff9543fdSMichal Nazarewicz * Take care that if the migration scanner is at the end of the zone 546ff9543fdSMichal Nazarewicz * that the free scanner does not accidentally move to the next zone 547ff9543fdSMichal Nazarewicz * in the next isolation cycle. 548ff9543fdSMichal Nazarewicz */ 549ff9543fdSMichal Nazarewicz high_pfn = min(low_pfn, pfn); 550ff9543fdSMichal Nazarewicz 551ff9543fdSMichal Nazarewicz zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 552ff9543fdSMichal Nazarewicz 553ff9543fdSMichal Nazarewicz /* 554ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 555ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 556ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 557ff9543fdSMichal Nazarewicz */ 558ff9543fdSMichal Nazarewicz for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; 559ff9543fdSMichal Nazarewicz pfn -= pageblock_nr_pages) { 560ff9543fdSMichal Nazarewicz unsigned long isolated; 561ff9543fdSMichal Nazarewicz 562ff9543fdSMichal Nazarewicz if (!pfn_valid(pfn)) 563ff9543fdSMichal Nazarewicz continue; 564ff9543fdSMichal Nazarewicz 565ff9543fdSMichal Nazarewicz /* 566ff9543fdSMichal Nazarewicz * Check for overlapping nodes/zones. It's possible on some 567ff9543fdSMichal Nazarewicz * configurations to have a setup like 568ff9543fdSMichal Nazarewicz * node0 node1 node0 569ff9543fdSMichal Nazarewicz * i.e. it's possible that all pages within a zones range of 570ff9543fdSMichal Nazarewicz * pages do not belong to a single zone. 571ff9543fdSMichal Nazarewicz */ 572ff9543fdSMichal Nazarewicz page = pfn_to_page(pfn); 573ff9543fdSMichal Nazarewicz if (page_zone(page) != zone) 574ff9543fdSMichal Nazarewicz continue; 575ff9543fdSMichal Nazarewicz 576ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 57768e3e926SLinus Torvalds if (!suitable_migration_target(page)) 578ff9543fdSMichal Nazarewicz continue; 57968e3e926SLinus Torvalds 580ff9543fdSMichal Nazarewicz /* 581ff9543fdSMichal Nazarewicz * Found a block suitable for isolating free pages from. Now 582ff9543fdSMichal Nazarewicz * we disabled interrupts, double check things are ok and 583ff9543fdSMichal Nazarewicz * isolate the pages. This is to minimise the time IRQs 584ff9543fdSMichal Nazarewicz * are disabled 585ff9543fdSMichal Nazarewicz */ 586ff9543fdSMichal Nazarewicz isolated = 0; 587c67fe375SMel Gorman 588c67fe375SMel Gorman /* 589c67fe375SMel Gorman * The zone lock must be held to isolate freepages. This 590c67fe375SMel Gorman * unfortunately this is a very coarse lock and can be 591c67fe375SMel Gorman * heavily contended if there are parallel allocations 592c67fe375SMel Gorman * or parallel compactions. For async compaction do not 593c67fe375SMel Gorman * spin on the lock 594c67fe375SMel Gorman */ 595c67fe375SMel Gorman if (!compact_trylock_irqsave(&zone->lock, &flags, cc)) 596c67fe375SMel Gorman break; 59768e3e926SLinus Torvalds if (suitable_migration_target(page)) { 598ff9543fdSMichal Nazarewicz end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); 599ff9543fdSMichal Nazarewicz isolated = isolate_freepages_block(pfn, end_pfn, 600ff9543fdSMichal Nazarewicz freelist, false); 601ff9543fdSMichal Nazarewicz nr_freepages += isolated; 60268e3e926SLinus Torvalds } 603ff9543fdSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 604ff9543fdSMichal Nazarewicz 605ff9543fdSMichal Nazarewicz /* 606ff9543fdSMichal Nazarewicz * Record the highest PFN we isolated pages from. When next 607ff9543fdSMichal Nazarewicz * looking for free pages, the search will restart here as 608ff9543fdSMichal Nazarewicz * page migration may have returned some pages to the allocator 609ff9543fdSMichal Nazarewicz */ 6107db8889aSRik van Riel if (isolated) { 611ff9543fdSMichal Nazarewicz high_pfn = max(high_pfn, pfn); 612de74f1ccSMel Gorman 613de74f1ccSMel Gorman /* 614de74f1ccSMel Gorman * If the free scanner has wrapped, update 615de74f1ccSMel Gorman * compact_cached_free_pfn to point to the highest 616de74f1ccSMel Gorman * pageblock with free pages. This reduces excessive 617de74f1ccSMel Gorman * scanning of full pageblocks near the end of the 618de74f1ccSMel Gorman * zone 619de74f1ccSMel Gorman */ 620de74f1ccSMel Gorman if (cc->order > 0 && cc->wrapped) 6217db8889aSRik van Riel zone->compact_cached_free_pfn = high_pfn; 6227db8889aSRik van Riel } 623ff9543fdSMichal Nazarewicz } 624ff9543fdSMichal Nazarewicz 625ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 626ff9543fdSMichal Nazarewicz map_pages(freelist); 627ff9543fdSMichal Nazarewicz 628ff9543fdSMichal Nazarewicz cc->free_pfn = high_pfn; 629ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 630de74f1ccSMel Gorman 631de74f1ccSMel Gorman /* If compact_cached_free_pfn is reset then set it now */ 632de74f1ccSMel Gorman if (cc->order > 0 && !cc->wrapped && 633de74f1ccSMel Gorman zone->compact_cached_free_pfn == start_free_pfn(zone)) 634de74f1ccSMel Gorman zone->compact_cached_free_pfn = high_pfn; 635748446bbSMel Gorman } 636748446bbSMel Gorman 637748446bbSMel Gorman /* 638748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 639748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 640748446bbSMel Gorman */ 641748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 642748446bbSMel Gorman unsigned long data, 643748446bbSMel Gorman int **result) 644748446bbSMel Gorman { 645748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 646748446bbSMel Gorman struct page *freepage; 647748446bbSMel Gorman 648748446bbSMel Gorman /* Isolate free pages if necessary */ 649748446bbSMel Gorman if (list_empty(&cc->freepages)) { 650748446bbSMel Gorman isolate_freepages(cc->zone, cc); 651748446bbSMel Gorman 652748446bbSMel Gorman if (list_empty(&cc->freepages)) 653748446bbSMel Gorman return NULL; 654748446bbSMel Gorman } 655748446bbSMel Gorman 656748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 657748446bbSMel Gorman list_del(&freepage->lru); 658748446bbSMel Gorman cc->nr_freepages--; 659748446bbSMel Gorman 660748446bbSMel Gorman return freepage; 661748446bbSMel Gorman } 662748446bbSMel Gorman 663748446bbSMel Gorman /* 664748446bbSMel Gorman * We cannot control nr_migratepages and nr_freepages fully when migration is 665748446bbSMel Gorman * running as migrate_pages() has no knowledge of compact_control. When 666748446bbSMel Gorman * migration is complete, we count the number of pages on the lists by hand. 667748446bbSMel Gorman */ 668748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc) 669748446bbSMel Gorman { 670748446bbSMel Gorman int nr_migratepages = 0; 671748446bbSMel Gorman int nr_freepages = 0; 672748446bbSMel Gorman struct page *page; 673748446bbSMel Gorman 674748446bbSMel Gorman list_for_each_entry(page, &cc->migratepages, lru) 675748446bbSMel Gorman nr_migratepages++; 676748446bbSMel Gorman list_for_each_entry(page, &cc->freepages, lru) 677748446bbSMel Gorman nr_freepages++; 678748446bbSMel Gorman 679748446bbSMel Gorman cc->nr_migratepages = nr_migratepages; 680748446bbSMel Gorman cc->nr_freepages = nr_freepages; 681748446bbSMel Gorman } 682748446bbSMel Gorman 683ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 684ff9543fdSMichal Nazarewicz typedef enum { 685ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 686ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 687ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 688ff9543fdSMichal Nazarewicz } isolate_migrate_t; 689ff9543fdSMichal Nazarewicz 690ff9543fdSMichal Nazarewicz /* 691ff9543fdSMichal Nazarewicz * Isolate all pages that can be migrated from the block pointed to by 692ff9543fdSMichal Nazarewicz * the migrate scanner within compact_control. 693ff9543fdSMichal Nazarewicz */ 694ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 695ff9543fdSMichal Nazarewicz struct compact_control *cc) 696ff9543fdSMichal Nazarewicz { 697ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 698ff9543fdSMichal Nazarewicz 699ff9543fdSMichal Nazarewicz /* Do not scan outside zone boundaries */ 700ff9543fdSMichal Nazarewicz low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); 701ff9543fdSMichal Nazarewicz 702ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 703ff9543fdSMichal Nazarewicz end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); 704ff9543fdSMichal Nazarewicz 705ff9543fdSMichal Nazarewicz /* Do not cross the free scanner or scan within a memory hole */ 706ff9543fdSMichal Nazarewicz if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { 707ff9543fdSMichal Nazarewicz cc->migrate_pfn = end_pfn; 708ff9543fdSMichal Nazarewicz return ISOLATE_NONE; 709ff9543fdSMichal Nazarewicz } 710ff9543fdSMichal Nazarewicz 711ff9543fdSMichal Nazarewicz /* Perform the isolation */ 712ff9543fdSMichal Nazarewicz low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); 713e64c5237SShaohua Li if (!low_pfn || cc->contended) 714ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 715ff9543fdSMichal Nazarewicz 716ff9543fdSMichal Nazarewicz cc->migrate_pfn = low_pfn; 717ff9543fdSMichal Nazarewicz 718ff9543fdSMichal Nazarewicz return ISOLATE_SUCCESS; 719ff9543fdSMichal Nazarewicz } 720ff9543fdSMichal Nazarewicz 721748446bbSMel Gorman static int compact_finished(struct zone *zone, 722748446bbSMel Gorman struct compact_control *cc) 723748446bbSMel Gorman { 7245a03b051SAndrea Arcangeli unsigned long watermark; 72556de7263SMel Gorman 726748446bbSMel Gorman if (fatal_signal_pending(current)) 727748446bbSMel Gorman return COMPACT_PARTIAL; 728748446bbSMel Gorman 7297db8889aSRik van Riel /* 7307db8889aSRik van Riel * A full (order == -1) compaction run starts at the beginning and 7317db8889aSRik van Riel * end of a zone; it completes when the migrate and free scanner meet. 7327db8889aSRik van Riel * A partial (order > 0) compaction can start with the free scanner 7337db8889aSRik van Riel * at a random point in the zone, and may have to restart. 7347db8889aSRik van Riel */ 7357db8889aSRik van Riel if (cc->free_pfn <= cc->migrate_pfn) { 7367db8889aSRik van Riel if (cc->order > 0 && !cc->wrapped) { 7377db8889aSRik van Riel /* We started partway through; restart at the end. */ 7387db8889aSRik van Riel unsigned long free_pfn = start_free_pfn(zone); 7397db8889aSRik van Riel zone->compact_cached_free_pfn = free_pfn; 7407db8889aSRik van Riel cc->free_pfn = free_pfn; 7417db8889aSRik van Riel cc->wrapped = 1; 7427db8889aSRik van Riel return COMPACT_CONTINUE; 7437db8889aSRik van Riel } 7447db8889aSRik van Riel return COMPACT_COMPLETE; 7457db8889aSRik van Riel } 7467db8889aSRik van Riel 7477db8889aSRik van Riel /* We wrapped around and ended up where we started. */ 7487db8889aSRik van Riel if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn) 749748446bbSMel Gorman return COMPACT_COMPLETE; 750748446bbSMel Gorman 75182478fb7SJohannes Weiner /* 75282478fb7SJohannes Weiner * order == -1 is expected when compacting via 75382478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 75482478fb7SJohannes Weiner */ 75556de7263SMel Gorman if (cc->order == -1) 75656de7263SMel Gorman return COMPACT_CONTINUE; 75756de7263SMel Gorman 7583957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 7593957c776SMichal Hocko watermark = low_wmark_pages(zone); 7603957c776SMichal Hocko watermark += (1 << cc->order); 7613957c776SMichal Hocko 7623957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 7633957c776SMichal Hocko return COMPACT_CONTINUE; 7643957c776SMichal Hocko 76556de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 7661fb3f8caSMel Gorman if (cc->page) { 7671fb3f8caSMel Gorman /* Was a suitable page captured? */ 7681fb3f8caSMel Gorman if (*cc->page) 7691fb3f8caSMel Gorman return COMPACT_PARTIAL; 7701fb3f8caSMel Gorman } else { 7711fb3f8caSMel Gorman unsigned int order; 77256de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 7731fb3f8caSMel Gorman struct free_area *area = &zone->free_area[cc->order]; 77456de7263SMel Gorman /* Job done if page is free of the right migratetype */ 7751fb3f8caSMel Gorman if (!list_empty(&area->free_list[cc->migratetype])) 77656de7263SMel Gorman return COMPACT_PARTIAL; 77756de7263SMel Gorman 77856de7263SMel Gorman /* Job done if allocation would set block type */ 7791fb3f8caSMel Gorman if (cc->order >= pageblock_order && area->nr_free) 78056de7263SMel Gorman return COMPACT_PARTIAL; 78156de7263SMel Gorman } 7821fb3f8caSMel Gorman } 78356de7263SMel Gorman 784748446bbSMel Gorman return COMPACT_CONTINUE; 785748446bbSMel Gorman } 786748446bbSMel Gorman 7873e7d3449SMel Gorman /* 7883e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 7893e7d3449SMel Gorman * Returns 7903e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 7913e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 7923e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 7933e7d3449SMel Gorman */ 7943e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 7953e7d3449SMel Gorman { 7963e7d3449SMel Gorman int fragindex; 7973e7d3449SMel Gorman unsigned long watermark; 7983e7d3449SMel Gorman 7993e7d3449SMel Gorman /* 8003957c776SMichal Hocko * order == -1 is expected when compacting via 8013957c776SMichal Hocko * /proc/sys/vm/compact_memory 8023957c776SMichal Hocko */ 8033957c776SMichal Hocko if (order == -1) 8043957c776SMichal Hocko return COMPACT_CONTINUE; 8053957c776SMichal Hocko 8063957c776SMichal Hocko /* 8073e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 8083e7d3449SMel Gorman * This is because during migration, copies of pages need to be 8093e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 8103e7d3449SMel Gorman */ 8113e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 8123e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 8133e7d3449SMel Gorman return COMPACT_SKIPPED; 8143e7d3449SMel Gorman 8153e7d3449SMel Gorman /* 8163e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 8173e7d3449SMel Gorman * low memory or external fragmentation 8183e7d3449SMel Gorman * 819a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 820a582a738SShaohua Li * watermarks 8213e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 8223e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 8233e7d3449SMel Gorman * 8243e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 8253e7d3449SMel Gorman */ 8263e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 8273e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 8283e7d3449SMel Gorman return COMPACT_SKIPPED; 8293e7d3449SMel Gorman 830a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 831a582a738SShaohua Li 0, 0)) 8323e7d3449SMel Gorman return COMPACT_PARTIAL; 8333e7d3449SMel Gorman 8343e7d3449SMel Gorman return COMPACT_CONTINUE; 8353e7d3449SMel Gorman } 8363e7d3449SMel Gorman 837748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 838748446bbSMel Gorman { 839748446bbSMel Gorman int ret; 840748446bbSMel Gorman 8413e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 8423e7d3449SMel Gorman switch (ret) { 8433e7d3449SMel Gorman case COMPACT_PARTIAL: 8443e7d3449SMel Gorman case COMPACT_SKIPPED: 8453e7d3449SMel Gorman /* Compaction is likely to fail */ 8463e7d3449SMel Gorman return ret; 8473e7d3449SMel Gorman case COMPACT_CONTINUE: 8483e7d3449SMel Gorman /* Fall through to compaction */ 8493e7d3449SMel Gorman ; 8503e7d3449SMel Gorman } 8513e7d3449SMel Gorman 852748446bbSMel Gorman /* Setup to move all movable pages to the end of the zone */ 853748446bbSMel Gorman cc->migrate_pfn = zone->zone_start_pfn; 8547db8889aSRik van Riel 8557db8889aSRik van Riel if (cc->order > 0) { 8567db8889aSRik van Riel /* Incremental compaction. Start where the last one stopped. */ 8577db8889aSRik van Riel cc->free_pfn = zone->compact_cached_free_pfn; 8587db8889aSRik van Riel cc->start_free_pfn = cc->free_pfn; 8597db8889aSRik van Riel } else { 8607db8889aSRik van Riel /* Order == -1 starts at the end of the zone. */ 8617db8889aSRik van Riel cc->free_pfn = start_free_pfn(zone); 8627db8889aSRik van Riel } 863748446bbSMel Gorman 864748446bbSMel Gorman migrate_prep_local(); 865748446bbSMel Gorman 866748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 867748446bbSMel Gorman unsigned long nr_migrate, nr_remaining; 8689d502c1cSMinchan Kim int err; 869748446bbSMel Gorman 870f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 871f9e35b3bSMel Gorman case ISOLATE_ABORT: 872f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 873e64c5237SShaohua Li putback_lru_pages(&cc->migratepages); 874e64c5237SShaohua Li cc->nr_migratepages = 0; 875f9e35b3bSMel Gorman goto out; 876f9e35b3bSMel Gorman case ISOLATE_NONE: 877748446bbSMel Gorman continue; 878f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 879f9e35b3bSMel Gorman ; 880f9e35b3bSMel Gorman } 881748446bbSMel Gorman 882748446bbSMel Gorman nr_migrate = cc->nr_migratepages; 8839d502c1cSMinchan Kim err = migrate_pages(&cc->migratepages, compaction_alloc, 88468e3e926SLinus Torvalds (unsigned long)cc, false, 88568e3e926SLinus Torvalds cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); 886748446bbSMel Gorman update_nr_listpages(cc); 887748446bbSMel Gorman nr_remaining = cc->nr_migratepages; 888748446bbSMel Gorman 889748446bbSMel Gorman count_vm_event(COMPACTBLOCKS); 890748446bbSMel Gorman count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); 891748446bbSMel Gorman if (nr_remaining) 892748446bbSMel Gorman count_vm_events(COMPACTPAGEFAILED, nr_remaining); 893b7aba698SMel Gorman trace_mm_compaction_migratepages(nr_migrate - nr_remaining, 894b7aba698SMel Gorman nr_remaining); 895748446bbSMel Gorman 896748446bbSMel Gorman /* Release LRU pages not migrated */ 8979d502c1cSMinchan Kim if (err) { 898748446bbSMel Gorman putback_lru_pages(&cc->migratepages); 899748446bbSMel Gorman cc->nr_migratepages = 0; 9004bf2bba3SDavid Rientjes if (err == -ENOMEM) { 9014bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 9024bf2bba3SDavid Rientjes goto out; 903748446bbSMel Gorman } 9044bf2bba3SDavid Rientjes } 9051fb3f8caSMel Gorman 9061fb3f8caSMel Gorman /* Capture a page now if it is a suitable size */ 9071fb3f8caSMel Gorman compact_capture_page(cc); 908748446bbSMel Gorman } 909748446bbSMel Gorman 910f9e35b3bSMel Gorman out: 911748446bbSMel Gorman /* Release free pages and check accounting */ 912748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 913748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 914748446bbSMel Gorman 915748446bbSMel Gorman return ret; 916748446bbSMel Gorman } 91776ab0f53SMel Gorman 918d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone, 91977f1fe6bSMel Gorman int order, gfp_t gfp_mask, 9201fb3f8caSMel Gorman bool sync, bool *contended, 9211fb3f8caSMel Gorman struct page **page) 92256de7263SMel Gorman { 923e64c5237SShaohua Li unsigned long ret; 92456de7263SMel Gorman struct compact_control cc = { 92556de7263SMel Gorman .nr_freepages = 0, 92656de7263SMel Gorman .nr_migratepages = 0, 92756de7263SMel Gorman .order = order, 92856de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 92956de7263SMel Gorman .zone = zone, 93068e3e926SLinus Torvalds .sync = sync, 9311fb3f8caSMel Gorman .page = page, 93256de7263SMel Gorman }; 93356de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 93456de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 93556de7263SMel Gorman 936e64c5237SShaohua Li ret = compact_zone(zone, &cc); 937e64c5237SShaohua Li 938e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 939e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 940e64c5237SShaohua Li 941e64c5237SShaohua Li *contended = cc.contended; 942e64c5237SShaohua Li return ret; 94356de7263SMel Gorman } 94456de7263SMel Gorman 9455e771905SMel Gorman int sysctl_extfrag_threshold = 500; 9465e771905SMel Gorman 94756de7263SMel Gorman /** 94856de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 94956de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 95056de7263SMel Gorman * @order: The order of the current allocation 95156de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 95256de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 95377f1fe6bSMel Gorman * @sync: Whether migration is synchronous or not 954661c4cb9SMel Gorman * @contended: Return value that is true if compaction was aborted due to lock contention 955661c4cb9SMel Gorman * @page: Optionally capture a free page of the requested order during compaction 95656de7263SMel Gorman * 95756de7263SMel Gorman * This is the main entry point for direct page compaction. 95856de7263SMel Gorman */ 95956de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 96077f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 9611fb3f8caSMel Gorman bool sync, bool *contended, struct page **page) 96256de7263SMel Gorman { 96356de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 96456de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 96556de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 96656de7263SMel Gorman struct zoneref *z; 96756de7263SMel Gorman struct zone *zone; 96856de7263SMel Gorman int rc = COMPACT_SKIPPED; 969d95ea5d1SBartlomiej Zolnierkiewicz int alloc_flags = 0; 97056de7263SMel Gorman 9714ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 972c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 97356de7263SMel Gorman return rc; 97456de7263SMel Gorman 97556de7263SMel Gorman count_vm_event(COMPACTSTALL); 97656de7263SMel Gorman 977d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA 978d95ea5d1SBartlomiej Zolnierkiewicz if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) 979d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags |= ALLOC_CMA; 980d95ea5d1SBartlomiej Zolnierkiewicz #endif 98156de7263SMel Gorman /* Compact each zone in the list */ 98256de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 98356de7263SMel Gorman nodemask) { 98456de7263SMel Gorman int status; 98556de7263SMel Gorman 986c67fe375SMel Gorman status = compact_zone_order(zone, order, gfp_mask, sync, 9871fb3f8caSMel Gorman contended, page); 98856de7263SMel Gorman rc = max(status, rc); 98956de7263SMel Gorman 9903e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 991d95ea5d1SBartlomiej Zolnierkiewicz if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 992d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags)) 99356de7263SMel Gorman break; 99456de7263SMel Gorman } 99556de7263SMel Gorman 99656de7263SMel Gorman return rc; 99756de7263SMel Gorman } 99856de7263SMel Gorman 99956de7263SMel Gorman 100076ab0f53SMel Gorman /* Compact all zones within a node */ 10017be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 100276ab0f53SMel Gorman { 100376ab0f53SMel Gorman int zoneid; 100476ab0f53SMel Gorman struct zone *zone; 100576ab0f53SMel Gorman 100676ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 100776ab0f53SMel Gorman 100876ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 100976ab0f53SMel Gorman if (!populated_zone(zone)) 101076ab0f53SMel Gorman continue; 101176ab0f53SMel Gorman 10127be62de9SRik van Riel cc->nr_freepages = 0; 10137be62de9SRik van Riel cc->nr_migratepages = 0; 10147be62de9SRik van Riel cc->zone = zone; 10157be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 10167be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 101776ab0f53SMel Gorman 1018aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 10197be62de9SRik van Riel compact_zone(zone, cc); 102076ab0f53SMel Gorman 1021aff62249SRik van Riel if (cc->order > 0) { 1022aff62249SRik van Riel int ok = zone_watermark_ok(zone, cc->order, 1023aff62249SRik van Riel low_wmark_pages(zone), 0, 0); 1024c81758fbSMinchan Kim if (ok && cc->order >= zone->compact_order_failed) 1025aff62249SRik van Riel zone->compact_order_failed = cc->order + 1; 1026aff62249SRik van Riel /* Currently async compaction is never deferred. */ 102768e3e926SLinus Torvalds else if (!ok && cc->sync) 1028aff62249SRik van Riel defer_compaction(zone, cc->order); 1029aff62249SRik van Riel } 1030aff62249SRik van Riel 10317be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 10327be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 103376ab0f53SMel Gorman } 103476ab0f53SMel Gorman 103576ab0f53SMel Gorman return 0; 103676ab0f53SMel Gorman } 103776ab0f53SMel Gorman 10387be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order) 10397be62de9SRik van Riel { 10407be62de9SRik van Riel struct compact_control cc = { 10417be62de9SRik van Riel .order = order, 104268e3e926SLinus Torvalds .sync = false, 10431fb3f8caSMel Gorman .page = NULL, 10447be62de9SRik van Riel }; 10457be62de9SRik van Riel 10467be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 10477be62de9SRik van Riel } 10487be62de9SRik van Riel 10497be62de9SRik van Riel static int compact_node(int nid) 10507be62de9SRik van Riel { 10517be62de9SRik van Riel struct compact_control cc = { 10527be62de9SRik van Riel .order = -1, 105368e3e926SLinus Torvalds .sync = true, 10541fb3f8caSMel Gorman .page = NULL, 10557be62de9SRik van Riel }; 10567be62de9SRik van Riel 10578575ec29SHugh Dickins return __compact_pgdat(NODE_DATA(nid), &cc); 10587be62de9SRik van Riel } 10597be62de9SRik van Riel 106076ab0f53SMel Gorman /* Compact all nodes in the system */ 106176ab0f53SMel Gorman static int compact_nodes(void) 106276ab0f53SMel Gorman { 106376ab0f53SMel Gorman int nid; 106476ab0f53SMel Gorman 10658575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 10668575ec29SHugh Dickins lru_add_drain_all(); 10678575ec29SHugh Dickins 106876ab0f53SMel Gorman for_each_online_node(nid) 106976ab0f53SMel Gorman compact_node(nid); 107076ab0f53SMel Gorman 107176ab0f53SMel Gorman return COMPACT_COMPLETE; 107276ab0f53SMel Gorman } 107376ab0f53SMel Gorman 107476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 107576ab0f53SMel Gorman int sysctl_compact_memory; 107676ab0f53SMel Gorman 107776ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 107876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 107976ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 108076ab0f53SMel Gorman { 108176ab0f53SMel Gorman if (write) 108276ab0f53SMel Gorman return compact_nodes(); 108376ab0f53SMel Gorman 108476ab0f53SMel Gorman return 0; 108576ab0f53SMel Gorman } 1086ed4a6d7fSMel Gorman 10875e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 10885e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 10895e771905SMel Gorman { 10905e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 10915e771905SMel Gorman 10925e771905SMel Gorman return 0; 10935e771905SMel Gorman } 10945e771905SMel Gorman 1095ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 109610fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev, 109710fbcf4cSKay Sievers struct device_attribute *attr, 1098ed4a6d7fSMel Gorman const char *buf, size_t count) 1099ed4a6d7fSMel Gorman { 11008575ec29SHugh Dickins int nid = dev->id; 11018575ec29SHugh Dickins 11028575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 11038575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 11048575ec29SHugh Dickins lru_add_drain_all(); 11058575ec29SHugh Dickins 11068575ec29SHugh Dickins compact_node(nid); 11078575ec29SHugh Dickins } 1108ed4a6d7fSMel Gorman 1109ed4a6d7fSMel Gorman return count; 1110ed4a6d7fSMel Gorman } 111110fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1112ed4a6d7fSMel Gorman 1113ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1114ed4a6d7fSMel Gorman { 111510fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1116ed4a6d7fSMel Gorman } 1117ed4a6d7fSMel Gorman 1118ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1119ed4a6d7fSMel Gorman { 112010fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1121ed4a6d7fSMel Gorman } 1122ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1123ff9543fdSMichal Nazarewicz 1124ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1125