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 5385aa125fSMichal Nazarewicz /* 54c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 55c67fe375SMel Gorman * very heavily contended. Check if the process needs to be scheduled or 56c67fe375SMel Gorman * if the lock is contended. For async compaction, back out in the event 57c67fe375SMel Gorman * if contention is severe. For sync compaction, schedule. 58c67fe375SMel Gorman * 59c67fe375SMel Gorman * Returns true if the lock is held. 60c67fe375SMel Gorman * Returns false if the lock is released and compaction should abort 61c67fe375SMel Gorman */ 62c67fe375SMel Gorman static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, 63c67fe375SMel Gorman bool locked, struct compact_control *cc) 64c67fe375SMel Gorman { 65c67fe375SMel Gorman if (need_resched() || spin_is_contended(lock)) { 66c67fe375SMel Gorman if (locked) { 67c67fe375SMel Gorman spin_unlock_irqrestore(lock, *flags); 68c67fe375SMel Gorman locked = false; 69c67fe375SMel Gorman } 70c67fe375SMel Gorman 71c67fe375SMel Gorman /* async aborts if taking too long or contended */ 72c67fe375SMel Gorman if (!cc->sync) { 73e64c5237SShaohua Li cc->contended = true; 74c67fe375SMel Gorman return false; 75c67fe375SMel Gorman } 76c67fe375SMel Gorman 77c67fe375SMel Gorman cond_resched(); 78c67fe375SMel Gorman } 79c67fe375SMel Gorman 80c67fe375SMel Gorman if (!locked) 81c67fe375SMel Gorman spin_lock_irqsave(lock, *flags); 82c67fe375SMel Gorman return true; 83c67fe375SMel Gorman } 84c67fe375SMel Gorman 85c67fe375SMel Gorman static inline bool compact_trylock_irqsave(spinlock_t *lock, 86c67fe375SMel Gorman unsigned long *flags, struct compact_control *cc) 87c67fe375SMel Gorman { 88c67fe375SMel Gorman return compact_checklock_irqsave(lock, flags, false, cc); 89c67fe375SMel Gorman } 90c67fe375SMel Gorman 911fb3f8caSMel Gorman static void compact_capture_page(struct compact_control *cc) 921fb3f8caSMel Gorman { 931fb3f8caSMel Gorman unsigned long flags; 941fb3f8caSMel Gorman int mtype, mtype_low, mtype_high; 951fb3f8caSMel Gorman 961fb3f8caSMel Gorman if (!cc->page || *cc->page) 971fb3f8caSMel Gorman return; 981fb3f8caSMel Gorman 991fb3f8caSMel Gorman /* 1001fb3f8caSMel Gorman * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP 1011fb3f8caSMel Gorman * regardless of the migratetype of the freelist is is captured from. 1021fb3f8caSMel Gorman * This is fine because the order for a high-order MIGRATE_MOVABLE 1031fb3f8caSMel Gorman * allocation is typically at least a pageblock size and overall 1041fb3f8caSMel Gorman * fragmentation is not impaired. Other allocation types must 1051fb3f8caSMel Gorman * capture pages from their own migratelist because otherwise they 1061fb3f8caSMel Gorman * could pollute other pageblocks like MIGRATE_MOVABLE with 1071fb3f8caSMel Gorman * difficult to move pages and making fragmentation worse overall. 1081fb3f8caSMel Gorman */ 1091fb3f8caSMel Gorman if (cc->migratetype == MIGRATE_MOVABLE) { 1101fb3f8caSMel Gorman mtype_low = 0; 1111fb3f8caSMel Gorman mtype_high = MIGRATE_PCPTYPES; 1121fb3f8caSMel Gorman } else { 1131fb3f8caSMel Gorman mtype_low = cc->migratetype; 1141fb3f8caSMel Gorman mtype_high = cc->migratetype + 1; 1151fb3f8caSMel Gorman } 1161fb3f8caSMel Gorman 1171fb3f8caSMel Gorman /* Speculatively examine the free lists without zone lock */ 1181fb3f8caSMel Gorman for (mtype = mtype_low; mtype < mtype_high; mtype++) { 1191fb3f8caSMel Gorman int order; 1201fb3f8caSMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 1211fb3f8caSMel Gorman struct page *page; 1221fb3f8caSMel Gorman struct free_area *area; 1231fb3f8caSMel Gorman area = &(cc->zone->free_area[order]); 1241fb3f8caSMel Gorman if (list_empty(&area->free_list[mtype])) 1251fb3f8caSMel Gorman continue; 1261fb3f8caSMel Gorman 1271fb3f8caSMel Gorman /* Take the lock and attempt capture of the page */ 1281fb3f8caSMel Gorman if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc)) 1291fb3f8caSMel Gorman return; 1301fb3f8caSMel Gorman if (!list_empty(&area->free_list[mtype])) { 1311fb3f8caSMel Gorman page = list_entry(area->free_list[mtype].next, 1321fb3f8caSMel Gorman struct page, lru); 1331fb3f8caSMel Gorman if (capture_free_page(page, cc->order, mtype)) { 1341fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, 1351fb3f8caSMel Gorman flags); 1361fb3f8caSMel Gorman *cc->page = page; 1371fb3f8caSMel Gorman return; 1381fb3f8caSMel Gorman } 1391fb3f8caSMel Gorman } 1401fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 1411fb3f8caSMel Gorman } 1421fb3f8caSMel Gorman } 1431fb3f8caSMel Gorman } 1441fb3f8caSMel Gorman 145c67fe375SMel Gorman /* 14685aa125fSMichal Nazarewicz * Isolate free pages onto a private freelist. Caller must hold zone->lock. 14785aa125fSMichal Nazarewicz * If @strict is true, will abort returning 0 on any invalid PFNs or non-free 14885aa125fSMichal Nazarewicz * pages inside of the pageblock (even though it may still end up isolating 14985aa125fSMichal Nazarewicz * some pages). 15085aa125fSMichal Nazarewicz */ 15185aa125fSMichal Nazarewicz static unsigned long isolate_freepages_block(unsigned long blockpfn, 15285aa125fSMichal Nazarewicz unsigned long end_pfn, 15385aa125fSMichal Nazarewicz struct list_head *freelist, 15485aa125fSMichal Nazarewicz bool strict) 155748446bbSMel Gorman { 156b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 157748446bbSMel Gorman struct page *cursor; 158748446bbSMel Gorman 159748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 160748446bbSMel Gorman 161748446bbSMel Gorman /* Isolate free pages. This assumes the block is valid */ 162748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 163748446bbSMel Gorman int isolated, i; 164748446bbSMel Gorman struct page *page = cursor; 165748446bbSMel Gorman 16685aa125fSMichal Nazarewicz if (!pfn_valid_within(blockpfn)) { 16785aa125fSMichal Nazarewicz if (strict) 16885aa125fSMichal Nazarewicz return 0; 169748446bbSMel Gorman continue; 17085aa125fSMichal Nazarewicz } 171b7aba698SMel Gorman nr_scanned++; 172748446bbSMel Gorman 17385aa125fSMichal Nazarewicz if (!PageBuddy(page)) { 17485aa125fSMichal Nazarewicz if (strict) 17585aa125fSMichal Nazarewicz return 0; 176748446bbSMel Gorman continue; 17785aa125fSMichal Nazarewicz } 178748446bbSMel Gorman 179748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 180748446bbSMel Gorman isolated = split_free_page(page); 18185aa125fSMichal Nazarewicz if (!isolated && strict) 18285aa125fSMichal Nazarewicz return 0; 183748446bbSMel Gorman total_isolated += isolated; 184748446bbSMel Gorman for (i = 0; i < isolated; i++) { 185748446bbSMel Gorman list_add(&page->lru, freelist); 186748446bbSMel Gorman page++; 187748446bbSMel Gorman } 188748446bbSMel Gorman 189748446bbSMel Gorman /* If a page was split, advance to the end of it */ 190748446bbSMel Gorman if (isolated) { 191748446bbSMel Gorman blockpfn += isolated - 1; 192748446bbSMel Gorman cursor += isolated - 1; 193748446bbSMel Gorman } 194748446bbSMel Gorman } 195748446bbSMel Gorman 196b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 197748446bbSMel Gorman return total_isolated; 198748446bbSMel Gorman } 199748446bbSMel Gorman 20085aa125fSMichal Nazarewicz /** 20185aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 20285aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 20385aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 20485aa125fSMichal Nazarewicz * 20585aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 20685aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 20785aa125fSMichal Nazarewicz * undo its actions and return zero. 20885aa125fSMichal Nazarewicz * 20985aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 21085aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 21185aa125fSMichal Nazarewicz * a free page). 21285aa125fSMichal Nazarewicz */ 213ff9543fdSMichal Nazarewicz unsigned long 21485aa125fSMichal Nazarewicz isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn) 21585aa125fSMichal Nazarewicz { 21685aa125fSMichal Nazarewicz unsigned long isolated, pfn, block_end_pfn, flags; 21785aa125fSMichal Nazarewicz struct zone *zone = NULL; 21885aa125fSMichal Nazarewicz LIST_HEAD(freelist); 21985aa125fSMichal Nazarewicz 22085aa125fSMichal Nazarewicz if (pfn_valid(start_pfn)) 22185aa125fSMichal Nazarewicz zone = page_zone(pfn_to_page(start_pfn)); 22285aa125fSMichal Nazarewicz 22385aa125fSMichal Nazarewicz for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) { 22485aa125fSMichal Nazarewicz if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn))) 22585aa125fSMichal Nazarewicz break; 22685aa125fSMichal Nazarewicz 22785aa125fSMichal Nazarewicz /* 22885aa125fSMichal Nazarewicz * On subsequent iterations ALIGN() is actually not needed, 22985aa125fSMichal Nazarewicz * but we keep it that we not to complicate the code. 23085aa125fSMichal Nazarewicz */ 23185aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 23285aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 23385aa125fSMichal Nazarewicz 23485aa125fSMichal Nazarewicz spin_lock_irqsave(&zone->lock, flags); 23585aa125fSMichal Nazarewicz isolated = isolate_freepages_block(pfn, block_end_pfn, 23685aa125fSMichal Nazarewicz &freelist, true); 23785aa125fSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 23885aa125fSMichal Nazarewicz 23985aa125fSMichal Nazarewicz /* 24085aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 24185aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 24285aa125fSMichal Nazarewicz * non-free pages). 24385aa125fSMichal Nazarewicz */ 24485aa125fSMichal Nazarewicz if (!isolated) 24585aa125fSMichal Nazarewicz break; 24685aa125fSMichal Nazarewicz 24785aa125fSMichal Nazarewicz /* 24885aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 24985aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 25085aa125fSMichal Nazarewicz * page may span two pageblocks). 25185aa125fSMichal Nazarewicz */ 25285aa125fSMichal Nazarewicz } 25385aa125fSMichal Nazarewicz 25485aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 25585aa125fSMichal Nazarewicz map_pages(&freelist); 25685aa125fSMichal Nazarewicz 25785aa125fSMichal Nazarewicz if (pfn < end_pfn) { 25885aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 25985aa125fSMichal Nazarewicz release_freepages(&freelist); 26085aa125fSMichal Nazarewicz return 0; 26185aa125fSMichal Nazarewicz } 26285aa125fSMichal Nazarewicz 26385aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 26485aa125fSMichal Nazarewicz return pfn; 26585aa125fSMichal Nazarewicz } 26685aa125fSMichal Nazarewicz 267748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 268c67fe375SMel Gorman static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc) 269748446bbSMel Gorman { 270748446bbSMel Gorman struct page *page; 271b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 272748446bbSMel Gorman 273b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 274b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 275748446bbSMel Gorman 276c67fe375SMel Gorman /* If locked we can use the interrupt unsafe versions */ 277c67fe375SMel Gorman if (locked) { 278b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 279b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 280c67fe375SMel Gorman } else { 281c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 282c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 283c67fe375SMel Gorman } 284748446bbSMel Gorman } 285748446bbSMel Gorman 286748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 287748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 288748446bbSMel Gorman { 289bc693045SMinchan Kim unsigned long active, inactive, isolated; 290748446bbSMel Gorman 291748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 292748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 293bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 294bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 295748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 296748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 297748446bbSMel Gorman 298bc693045SMinchan Kim return isolated > (inactive + active) / 2; 299748446bbSMel Gorman } 300748446bbSMel Gorman 3012fe86e00SMichal Nazarewicz /** 3022fe86e00SMichal Nazarewicz * isolate_migratepages_range() - isolate all migrate-able pages in range. 3032fe86e00SMichal Nazarewicz * @zone: Zone pages are in. 3042fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 3052fe86e00SMichal Nazarewicz * @low_pfn: The first PFN of the range. 3062fe86e00SMichal Nazarewicz * @end_pfn: The one-past-the-last PFN of the range. 3072fe86e00SMichal Nazarewicz * 3082fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 3092fe86e00SMichal Nazarewicz * [low_pfn, end_pfn). Returns zero if there is a fatal signal 3102fe86e00SMichal Nazarewicz * pending), otherwise PFN of the first page that was not scanned 3112fe86e00SMichal Nazarewicz * (which may be both less, equal to or more then end_pfn). 3122fe86e00SMichal Nazarewicz * 3132fe86e00SMichal Nazarewicz * Assumes that cc->migratepages is empty and cc->nr_migratepages is 3142fe86e00SMichal Nazarewicz * zero. 3152fe86e00SMichal Nazarewicz * 3162fe86e00SMichal Nazarewicz * Apart from cc->migratepages and cc->nr_migratetypes this function 3172fe86e00SMichal Nazarewicz * does not modify any cc's fields, in particular it does not modify 3182fe86e00SMichal Nazarewicz * (or read for that matter) cc->migrate_pfn. 319748446bbSMel Gorman */ 320ff9543fdSMichal Nazarewicz unsigned long 3212fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc, 3222fe86e00SMichal Nazarewicz unsigned long low_pfn, unsigned long end_pfn) 323748446bbSMel Gorman { 3249927af74SMel Gorman unsigned long last_pageblock_nr = 0, pageblock_nr; 325b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 326748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 327f3fd4a61SKonstantin Khlebnikov isolate_mode_t mode = 0; 328fa9add64SHugh Dickins struct lruvec *lruvec; 329c67fe375SMel Gorman unsigned long flags; 330c67fe375SMel Gorman bool locked; 331748446bbSMel Gorman 332748446bbSMel Gorman /* 333748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 334748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 335748446bbSMel Gorman * delay for some time until fewer pages are isolated 336748446bbSMel Gorman */ 337748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 338f9e35b3bSMel Gorman /* async migration should just abort */ 33968e3e926SLinus Torvalds if (!cc->sync) 3402fe86e00SMichal Nazarewicz return 0; 341f9e35b3bSMel Gorman 342748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 343748446bbSMel Gorman 344748446bbSMel Gorman if (fatal_signal_pending(current)) 3452fe86e00SMichal Nazarewicz return 0; 346748446bbSMel Gorman } 347748446bbSMel Gorman 348748446bbSMel Gorman /* Time to isolate some pages for migration */ 349b2eef8c0SAndrea Arcangeli cond_resched(); 350c67fe375SMel Gorman spin_lock_irqsave(&zone->lru_lock, flags); 351c67fe375SMel Gorman locked = true; 352748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 353748446bbSMel Gorman struct page *page; 354b2eef8c0SAndrea Arcangeli 355b2eef8c0SAndrea Arcangeli /* give a chance to irqs before checking need_resched() */ 356b2eef8c0SAndrea Arcangeli if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) { 357c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 358b2eef8c0SAndrea Arcangeli locked = false; 359b2eef8c0SAndrea Arcangeli } 360c67fe375SMel Gorman 361c67fe375SMel Gorman /* Check if it is ok to still hold the lock */ 362c67fe375SMel Gorman locked = compact_checklock_irqsave(&zone->lru_lock, &flags, 363c67fe375SMel Gorman locked, cc); 3643cc668f4SMel Gorman if (!locked || fatal_signal_pending(current)) 365b2eef8c0SAndrea Arcangeli break; 366b2eef8c0SAndrea Arcangeli 3670bf380bcSMel Gorman /* 3680bf380bcSMel Gorman * migrate_pfn does not necessarily start aligned to a 3690bf380bcSMel Gorman * pageblock. Ensure that pfn_valid is called when moving 3700bf380bcSMel Gorman * into a new MAX_ORDER_NR_PAGES range in case of large 3710bf380bcSMel Gorman * memory holes within the zone 3720bf380bcSMel Gorman */ 3730bf380bcSMel Gorman if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { 3740bf380bcSMel Gorman if (!pfn_valid(low_pfn)) { 3750bf380bcSMel Gorman low_pfn += MAX_ORDER_NR_PAGES - 1; 3760bf380bcSMel Gorman continue; 3770bf380bcSMel Gorman } 3780bf380bcSMel Gorman } 3790bf380bcSMel Gorman 380748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 381748446bbSMel Gorman continue; 382b7aba698SMel Gorman nr_scanned++; 383748446bbSMel Gorman 384dc908600SMel Gorman /* 385dc908600SMel Gorman * Get the page and ensure the page is within the same zone. 386dc908600SMel Gorman * See the comment in isolate_freepages about overlapping 387dc908600SMel Gorman * nodes. It is deliberate that the new zone lock is not taken 388dc908600SMel Gorman * as memory compaction should not move pages between nodes. 389dc908600SMel Gorman */ 390748446bbSMel Gorman page = pfn_to_page(low_pfn); 391dc908600SMel Gorman if (page_zone(page) != zone) 392dc908600SMel Gorman continue; 393dc908600SMel Gorman 394dc908600SMel Gorman /* Skip if free */ 395748446bbSMel Gorman if (PageBuddy(page)) 396748446bbSMel Gorman continue; 397748446bbSMel Gorman 3989927af74SMel Gorman /* 3999927af74SMel Gorman * For async migration, also only scan in MOVABLE blocks. Async 4009927af74SMel Gorman * migration is optimistic to see if the minimum amount of work 4019927af74SMel Gorman * satisfies the allocation 4029927af74SMel Gorman */ 4039927af74SMel Gorman pageblock_nr = low_pfn >> pageblock_order; 40468e3e926SLinus Torvalds if (!cc->sync && last_pageblock_nr != pageblock_nr && 40547118af0SMichal Nazarewicz !migrate_async_suitable(get_pageblock_migratetype(page))) { 4069927af74SMel Gorman low_pfn += pageblock_nr_pages; 4079927af74SMel Gorman low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 4089927af74SMel Gorman last_pageblock_nr = pageblock_nr; 4099927af74SMel Gorman continue; 4109927af74SMel Gorman } 4119927af74SMel Gorman 412bc835011SAndrea Arcangeli if (!PageLRU(page)) 413bc835011SAndrea Arcangeli continue; 414bc835011SAndrea Arcangeli 415bc835011SAndrea Arcangeli /* 416bc835011SAndrea Arcangeli * PageLRU is set, and lru_lock excludes isolation, 417bc835011SAndrea Arcangeli * splitting and collapsing (collapsing has already 418bc835011SAndrea Arcangeli * happened if PageLRU is set). 419bc835011SAndrea Arcangeli */ 420bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 421bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 422bc835011SAndrea Arcangeli continue; 423bc835011SAndrea Arcangeli } 424bc835011SAndrea Arcangeli 42568e3e926SLinus Torvalds if (!cc->sync) 426c8244935SMel Gorman mode |= ISOLATE_ASYNC_MIGRATE; 427c8244935SMel Gorman 428fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 429fa9add64SHugh Dickins 430748446bbSMel Gorman /* Try isolate the page */ 431f3fd4a61SKonstantin Khlebnikov if (__isolate_lru_page(page, mode) != 0) 432748446bbSMel Gorman continue; 433748446bbSMel Gorman 434bc835011SAndrea Arcangeli VM_BUG_ON(PageTransCompound(page)); 435bc835011SAndrea Arcangeli 436748446bbSMel Gorman /* Successfully isolated */ 437fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 438748446bbSMel Gorman list_add(&page->lru, migratelist); 439748446bbSMel Gorman cc->nr_migratepages++; 440b7aba698SMel Gorman nr_isolated++; 441748446bbSMel Gorman 442748446bbSMel Gorman /* Avoid isolating too much */ 44331b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 44431b8384aSHillf Danton ++low_pfn; 445748446bbSMel Gorman break; 446748446bbSMel Gorman } 44731b8384aSHillf Danton } 448748446bbSMel Gorman 449c67fe375SMel Gorman acct_isolated(zone, locked, cc); 450748446bbSMel Gorman 451c67fe375SMel Gorman if (locked) 452c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 453748446bbSMel Gorman 454b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 455b7aba698SMel Gorman 4562fe86e00SMichal Nazarewicz return low_pfn; 4572fe86e00SMichal Nazarewicz } 4582fe86e00SMichal Nazarewicz 459ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 460ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 461ff9543fdSMichal Nazarewicz 46268e3e926SLinus Torvalds /* Returns true if the page is within a block suitable for migration to */ 46368e3e926SLinus Torvalds static bool suitable_migration_target(struct page *page) 4642fe86e00SMichal Nazarewicz { 4652fe86e00SMichal Nazarewicz 466ff9543fdSMichal Nazarewicz int migratetype = get_pageblock_migratetype(page); 4672fe86e00SMichal Nazarewicz 468ff9543fdSMichal Nazarewicz /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 469ff9543fdSMichal Nazarewicz if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 47068e3e926SLinus Torvalds return false; 4712fe86e00SMichal Nazarewicz 472ff9543fdSMichal Nazarewicz /* If the page is a large free page, then allow migration */ 473ff9543fdSMichal Nazarewicz if (PageBuddy(page) && page_order(page) >= pageblock_order) 47468e3e926SLinus Torvalds return true; 475ff9543fdSMichal Nazarewicz 47647118af0SMichal Nazarewicz /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 47768e3e926SLinus Torvalds if (migrate_async_suitable(migratetype)) 47868e3e926SLinus Torvalds return true; 479ff9543fdSMichal Nazarewicz 480ff9543fdSMichal Nazarewicz /* Otherwise skip the block */ 48168e3e926SLinus Torvalds return false; 4822fe86e00SMichal Nazarewicz } 4832fe86e00SMichal Nazarewicz 484ff9543fdSMichal Nazarewicz /* 485de74f1ccSMel Gorman * Returns the start pfn of the last page block in a zone. This is the starting 486de74f1ccSMel Gorman * point for full compaction of a zone. Compaction searches for free pages from 487de74f1ccSMel Gorman * the end of each zone, while isolate_freepages_block scans forward inside each 488de74f1ccSMel Gorman * page block. 489de74f1ccSMel Gorman */ 490de74f1ccSMel Gorman static unsigned long start_free_pfn(struct zone *zone) 491de74f1ccSMel Gorman { 492de74f1ccSMel Gorman unsigned long free_pfn; 493de74f1ccSMel Gorman free_pfn = zone->zone_start_pfn + zone->spanned_pages; 494de74f1ccSMel Gorman free_pfn &= ~(pageblock_nr_pages-1); 495de74f1ccSMel Gorman return free_pfn; 496de74f1ccSMel Gorman } 497de74f1ccSMel Gorman 498de74f1ccSMel Gorman /* 499ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 500ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 501ff9543fdSMichal Nazarewicz */ 502ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone, 503ff9543fdSMichal Nazarewicz struct compact_control *cc) 504ff9543fdSMichal Nazarewicz { 505ff9543fdSMichal Nazarewicz struct page *page; 506ff9543fdSMichal Nazarewicz unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; 507ff9543fdSMichal Nazarewicz unsigned long flags; 508ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 509ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 5102fe86e00SMichal Nazarewicz 511ff9543fdSMichal Nazarewicz /* 512ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 513ff9543fdSMichal Nazarewicz * scanned from (or the end of the zone if starting). The low point 514ff9543fdSMichal Nazarewicz * is the end of the pageblock the migration scanner is using. 515ff9543fdSMichal Nazarewicz */ 516ff9543fdSMichal Nazarewicz pfn = cc->free_pfn; 517ff9543fdSMichal Nazarewicz low_pfn = cc->migrate_pfn + pageblock_nr_pages; 5182fe86e00SMichal Nazarewicz 519ff9543fdSMichal Nazarewicz /* 520ff9543fdSMichal Nazarewicz * Take care that if the migration scanner is at the end of the zone 521ff9543fdSMichal Nazarewicz * that the free scanner does not accidentally move to the next zone 522ff9543fdSMichal Nazarewicz * in the next isolation cycle. 523ff9543fdSMichal Nazarewicz */ 524ff9543fdSMichal Nazarewicz high_pfn = min(low_pfn, pfn); 525ff9543fdSMichal Nazarewicz 526ff9543fdSMichal Nazarewicz zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 527ff9543fdSMichal Nazarewicz 528ff9543fdSMichal Nazarewicz /* 529ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 530ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 531ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 532ff9543fdSMichal Nazarewicz */ 533ff9543fdSMichal Nazarewicz for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; 534ff9543fdSMichal Nazarewicz pfn -= pageblock_nr_pages) { 535ff9543fdSMichal Nazarewicz unsigned long isolated; 536ff9543fdSMichal Nazarewicz 537ff9543fdSMichal Nazarewicz if (!pfn_valid(pfn)) 538ff9543fdSMichal Nazarewicz continue; 539ff9543fdSMichal Nazarewicz 540ff9543fdSMichal Nazarewicz /* 541ff9543fdSMichal Nazarewicz * Check for overlapping nodes/zones. It's possible on some 542ff9543fdSMichal Nazarewicz * configurations to have a setup like 543ff9543fdSMichal Nazarewicz * node0 node1 node0 544ff9543fdSMichal Nazarewicz * i.e. it's possible that all pages within a zones range of 545ff9543fdSMichal Nazarewicz * pages do not belong to a single zone. 546ff9543fdSMichal Nazarewicz */ 547ff9543fdSMichal Nazarewicz page = pfn_to_page(pfn); 548ff9543fdSMichal Nazarewicz if (page_zone(page) != zone) 549ff9543fdSMichal Nazarewicz continue; 550ff9543fdSMichal Nazarewicz 551ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 55268e3e926SLinus Torvalds if (!suitable_migration_target(page)) 553ff9543fdSMichal Nazarewicz continue; 55468e3e926SLinus Torvalds 555ff9543fdSMichal Nazarewicz /* 556ff9543fdSMichal Nazarewicz * Found a block suitable for isolating free pages from. Now 557ff9543fdSMichal Nazarewicz * we disabled interrupts, double check things are ok and 558ff9543fdSMichal Nazarewicz * isolate the pages. This is to minimise the time IRQs 559ff9543fdSMichal Nazarewicz * are disabled 560ff9543fdSMichal Nazarewicz */ 561ff9543fdSMichal Nazarewicz isolated = 0; 562c67fe375SMel Gorman 563c67fe375SMel Gorman /* 564c67fe375SMel Gorman * The zone lock must be held to isolate freepages. This 565c67fe375SMel Gorman * unfortunately this is a very coarse lock and can be 566c67fe375SMel Gorman * heavily contended if there are parallel allocations 567c67fe375SMel Gorman * or parallel compactions. For async compaction do not 568c67fe375SMel Gorman * spin on the lock 569c67fe375SMel Gorman */ 570c67fe375SMel Gorman if (!compact_trylock_irqsave(&zone->lock, &flags, cc)) 571c67fe375SMel Gorman break; 57268e3e926SLinus Torvalds if (suitable_migration_target(page)) { 573ff9543fdSMichal Nazarewicz end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); 574ff9543fdSMichal Nazarewicz isolated = isolate_freepages_block(pfn, end_pfn, 575ff9543fdSMichal Nazarewicz freelist, false); 576ff9543fdSMichal Nazarewicz nr_freepages += isolated; 57768e3e926SLinus Torvalds } 578ff9543fdSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 579ff9543fdSMichal Nazarewicz 580ff9543fdSMichal Nazarewicz /* 581ff9543fdSMichal Nazarewicz * Record the highest PFN we isolated pages from. When next 582ff9543fdSMichal Nazarewicz * looking for free pages, the search will restart here as 583ff9543fdSMichal Nazarewicz * page migration may have returned some pages to the allocator 584ff9543fdSMichal Nazarewicz */ 5857db8889aSRik van Riel if (isolated) { 586ff9543fdSMichal Nazarewicz high_pfn = max(high_pfn, pfn); 587de74f1ccSMel Gorman 588de74f1ccSMel Gorman /* 589de74f1ccSMel Gorman * If the free scanner has wrapped, update 590de74f1ccSMel Gorman * compact_cached_free_pfn to point to the highest 591de74f1ccSMel Gorman * pageblock with free pages. This reduces excessive 592de74f1ccSMel Gorman * scanning of full pageblocks near the end of the 593de74f1ccSMel Gorman * zone 594de74f1ccSMel Gorman */ 595de74f1ccSMel Gorman if (cc->order > 0 && cc->wrapped) 5967db8889aSRik van Riel zone->compact_cached_free_pfn = high_pfn; 5977db8889aSRik van Riel } 598ff9543fdSMichal Nazarewicz } 599ff9543fdSMichal Nazarewicz 600ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 601ff9543fdSMichal Nazarewicz map_pages(freelist); 602ff9543fdSMichal Nazarewicz 603ff9543fdSMichal Nazarewicz cc->free_pfn = high_pfn; 604ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 605de74f1ccSMel Gorman 606de74f1ccSMel Gorman /* If compact_cached_free_pfn is reset then set it now */ 607de74f1ccSMel Gorman if (cc->order > 0 && !cc->wrapped && 608de74f1ccSMel Gorman zone->compact_cached_free_pfn == start_free_pfn(zone)) 609de74f1ccSMel Gorman zone->compact_cached_free_pfn = high_pfn; 610748446bbSMel Gorman } 611748446bbSMel Gorman 612748446bbSMel Gorman /* 613748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 614748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 615748446bbSMel Gorman */ 616748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 617748446bbSMel Gorman unsigned long data, 618748446bbSMel Gorman int **result) 619748446bbSMel Gorman { 620748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 621748446bbSMel Gorman struct page *freepage; 622748446bbSMel Gorman 623748446bbSMel Gorman /* Isolate free pages if necessary */ 624748446bbSMel Gorman if (list_empty(&cc->freepages)) { 625748446bbSMel Gorman isolate_freepages(cc->zone, cc); 626748446bbSMel Gorman 627748446bbSMel Gorman if (list_empty(&cc->freepages)) 628748446bbSMel Gorman return NULL; 629748446bbSMel Gorman } 630748446bbSMel Gorman 631748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 632748446bbSMel Gorman list_del(&freepage->lru); 633748446bbSMel Gorman cc->nr_freepages--; 634748446bbSMel Gorman 635748446bbSMel Gorman return freepage; 636748446bbSMel Gorman } 637748446bbSMel Gorman 638748446bbSMel Gorman /* 639748446bbSMel Gorman * We cannot control nr_migratepages and nr_freepages fully when migration is 640748446bbSMel Gorman * running as migrate_pages() has no knowledge of compact_control. When 641748446bbSMel Gorman * migration is complete, we count the number of pages on the lists by hand. 642748446bbSMel Gorman */ 643748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc) 644748446bbSMel Gorman { 645748446bbSMel Gorman int nr_migratepages = 0; 646748446bbSMel Gorman int nr_freepages = 0; 647748446bbSMel Gorman struct page *page; 648748446bbSMel Gorman 649748446bbSMel Gorman list_for_each_entry(page, &cc->migratepages, lru) 650748446bbSMel Gorman nr_migratepages++; 651748446bbSMel Gorman list_for_each_entry(page, &cc->freepages, lru) 652748446bbSMel Gorman nr_freepages++; 653748446bbSMel Gorman 654748446bbSMel Gorman cc->nr_migratepages = nr_migratepages; 655748446bbSMel Gorman cc->nr_freepages = nr_freepages; 656748446bbSMel Gorman } 657748446bbSMel Gorman 658ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 659ff9543fdSMichal Nazarewicz typedef enum { 660ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 661ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 662ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 663ff9543fdSMichal Nazarewicz } isolate_migrate_t; 664ff9543fdSMichal Nazarewicz 665ff9543fdSMichal Nazarewicz /* 666ff9543fdSMichal Nazarewicz * Isolate all pages that can be migrated from the block pointed to by 667ff9543fdSMichal Nazarewicz * the migrate scanner within compact_control. 668ff9543fdSMichal Nazarewicz */ 669ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 670ff9543fdSMichal Nazarewicz struct compact_control *cc) 671ff9543fdSMichal Nazarewicz { 672ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 673ff9543fdSMichal Nazarewicz 674ff9543fdSMichal Nazarewicz /* Do not scan outside zone boundaries */ 675ff9543fdSMichal Nazarewicz low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); 676ff9543fdSMichal Nazarewicz 677ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 678ff9543fdSMichal Nazarewicz end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); 679ff9543fdSMichal Nazarewicz 680ff9543fdSMichal Nazarewicz /* Do not cross the free scanner or scan within a memory hole */ 681ff9543fdSMichal Nazarewicz if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { 682ff9543fdSMichal Nazarewicz cc->migrate_pfn = end_pfn; 683ff9543fdSMichal Nazarewicz return ISOLATE_NONE; 684ff9543fdSMichal Nazarewicz } 685ff9543fdSMichal Nazarewicz 686ff9543fdSMichal Nazarewicz /* Perform the isolation */ 687ff9543fdSMichal Nazarewicz low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); 688e64c5237SShaohua Li if (!low_pfn || cc->contended) 689ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 690ff9543fdSMichal Nazarewicz 691ff9543fdSMichal Nazarewicz cc->migrate_pfn = low_pfn; 692ff9543fdSMichal Nazarewicz 693ff9543fdSMichal Nazarewicz return ISOLATE_SUCCESS; 694ff9543fdSMichal Nazarewicz } 695ff9543fdSMichal Nazarewicz 696748446bbSMel Gorman static int compact_finished(struct zone *zone, 697748446bbSMel Gorman struct compact_control *cc) 698748446bbSMel Gorman { 6995a03b051SAndrea Arcangeli unsigned long watermark; 70056de7263SMel Gorman 701748446bbSMel Gorman if (fatal_signal_pending(current)) 702748446bbSMel Gorman return COMPACT_PARTIAL; 703748446bbSMel Gorman 7047db8889aSRik van Riel /* 7057db8889aSRik van Riel * A full (order == -1) compaction run starts at the beginning and 7067db8889aSRik van Riel * end of a zone; it completes when the migrate and free scanner meet. 7077db8889aSRik van Riel * A partial (order > 0) compaction can start with the free scanner 7087db8889aSRik van Riel * at a random point in the zone, and may have to restart. 7097db8889aSRik van Riel */ 7107db8889aSRik van Riel if (cc->free_pfn <= cc->migrate_pfn) { 7117db8889aSRik van Riel if (cc->order > 0 && !cc->wrapped) { 7127db8889aSRik van Riel /* We started partway through; restart at the end. */ 7137db8889aSRik van Riel unsigned long free_pfn = start_free_pfn(zone); 7147db8889aSRik van Riel zone->compact_cached_free_pfn = free_pfn; 7157db8889aSRik van Riel cc->free_pfn = free_pfn; 7167db8889aSRik van Riel cc->wrapped = 1; 7177db8889aSRik van Riel return COMPACT_CONTINUE; 7187db8889aSRik van Riel } 7197db8889aSRik van Riel return COMPACT_COMPLETE; 7207db8889aSRik van Riel } 7217db8889aSRik van Riel 7227db8889aSRik van Riel /* We wrapped around and ended up where we started. */ 7237db8889aSRik van Riel if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn) 724748446bbSMel Gorman return COMPACT_COMPLETE; 725748446bbSMel Gorman 72682478fb7SJohannes Weiner /* 72782478fb7SJohannes Weiner * order == -1 is expected when compacting via 72882478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 72982478fb7SJohannes Weiner */ 73056de7263SMel Gorman if (cc->order == -1) 73156de7263SMel Gorman return COMPACT_CONTINUE; 73256de7263SMel Gorman 7333957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 7343957c776SMichal Hocko watermark = low_wmark_pages(zone); 7353957c776SMichal Hocko watermark += (1 << cc->order); 7363957c776SMichal Hocko 7373957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 7383957c776SMichal Hocko return COMPACT_CONTINUE; 7393957c776SMichal Hocko 74056de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 7411fb3f8caSMel Gorman if (cc->page) { 7421fb3f8caSMel Gorman /* Was a suitable page captured? */ 7431fb3f8caSMel Gorman if (*cc->page) 7441fb3f8caSMel Gorman return COMPACT_PARTIAL; 7451fb3f8caSMel Gorman } else { 7461fb3f8caSMel Gorman unsigned int order; 74756de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 7481fb3f8caSMel Gorman struct free_area *area = &zone->free_area[cc->order]; 74956de7263SMel Gorman /* Job done if page is free of the right migratetype */ 7501fb3f8caSMel Gorman if (!list_empty(&area->free_list[cc->migratetype])) 75156de7263SMel Gorman return COMPACT_PARTIAL; 75256de7263SMel Gorman 75356de7263SMel Gorman /* Job done if allocation would set block type */ 7541fb3f8caSMel Gorman if (cc->order >= pageblock_order && area->nr_free) 75556de7263SMel Gorman return COMPACT_PARTIAL; 75656de7263SMel Gorman } 7571fb3f8caSMel Gorman } 75856de7263SMel Gorman 759748446bbSMel Gorman return COMPACT_CONTINUE; 760748446bbSMel Gorman } 761748446bbSMel Gorman 7623e7d3449SMel Gorman /* 7633e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 7643e7d3449SMel Gorman * Returns 7653e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 7663e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 7673e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 7683e7d3449SMel Gorman */ 7693e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 7703e7d3449SMel Gorman { 7713e7d3449SMel Gorman int fragindex; 7723e7d3449SMel Gorman unsigned long watermark; 7733e7d3449SMel Gorman 7743e7d3449SMel Gorman /* 7753957c776SMichal Hocko * order == -1 is expected when compacting via 7763957c776SMichal Hocko * /proc/sys/vm/compact_memory 7773957c776SMichal Hocko */ 7783957c776SMichal Hocko if (order == -1) 7793957c776SMichal Hocko return COMPACT_CONTINUE; 7803957c776SMichal Hocko 7813957c776SMichal Hocko /* 7823e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 7833e7d3449SMel Gorman * This is because during migration, copies of pages need to be 7843e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 7853e7d3449SMel Gorman */ 7863e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 7873e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 7883e7d3449SMel Gorman return COMPACT_SKIPPED; 7893e7d3449SMel Gorman 7903e7d3449SMel Gorman /* 7913e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 7923e7d3449SMel Gorman * low memory or external fragmentation 7933e7d3449SMel Gorman * 794a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 795a582a738SShaohua Li * watermarks 7963e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 7973e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 7983e7d3449SMel Gorman * 7993e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 8003e7d3449SMel Gorman */ 8013e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 8023e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 8033e7d3449SMel Gorman return COMPACT_SKIPPED; 8043e7d3449SMel Gorman 805a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 806a582a738SShaohua Li 0, 0)) 8073e7d3449SMel Gorman return COMPACT_PARTIAL; 8083e7d3449SMel Gorman 8093e7d3449SMel Gorman return COMPACT_CONTINUE; 8103e7d3449SMel Gorman } 8113e7d3449SMel Gorman 812748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 813748446bbSMel Gorman { 814748446bbSMel Gorman int ret; 815748446bbSMel Gorman 8163e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 8173e7d3449SMel Gorman switch (ret) { 8183e7d3449SMel Gorman case COMPACT_PARTIAL: 8193e7d3449SMel Gorman case COMPACT_SKIPPED: 8203e7d3449SMel Gorman /* Compaction is likely to fail */ 8213e7d3449SMel Gorman return ret; 8223e7d3449SMel Gorman case COMPACT_CONTINUE: 8233e7d3449SMel Gorman /* Fall through to compaction */ 8243e7d3449SMel Gorman ; 8253e7d3449SMel Gorman } 8263e7d3449SMel Gorman 827748446bbSMel Gorman /* Setup to move all movable pages to the end of the zone */ 828748446bbSMel Gorman cc->migrate_pfn = zone->zone_start_pfn; 8297db8889aSRik van Riel 8307db8889aSRik van Riel if (cc->order > 0) { 8317db8889aSRik van Riel /* Incremental compaction. Start where the last one stopped. */ 8327db8889aSRik van Riel cc->free_pfn = zone->compact_cached_free_pfn; 8337db8889aSRik van Riel cc->start_free_pfn = cc->free_pfn; 8347db8889aSRik van Riel } else { 8357db8889aSRik van Riel /* Order == -1 starts at the end of the zone. */ 8367db8889aSRik van Riel cc->free_pfn = start_free_pfn(zone); 8377db8889aSRik van Riel } 838748446bbSMel Gorman 839748446bbSMel Gorman migrate_prep_local(); 840748446bbSMel Gorman 841748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 842748446bbSMel Gorman unsigned long nr_migrate, nr_remaining; 8439d502c1cSMinchan Kim int err; 844748446bbSMel Gorman 845f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 846f9e35b3bSMel Gorman case ISOLATE_ABORT: 847f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 848e64c5237SShaohua Li putback_lru_pages(&cc->migratepages); 849e64c5237SShaohua Li cc->nr_migratepages = 0; 850f9e35b3bSMel Gorman goto out; 851f9e35b3bSMel Gorman case ISOLATE_NONE: 852748446bbSMel Gorman continue; 853f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 854f9e35b3bSMel Gorman ; 855f9e35b3bSMel Gorman } 856748446bbSMel Gorman 857748446bbSMel Gorman nr_migrate = cc->nr_migratepages; 8589d502c1cSMinchan Kim err = migrate_pages(&cc->migratepages, compaction_alloc, 85968e3e926SLinus Torvalds (unsigned long)cc, false, 86068e3e926SLinus Torvalds cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); 861748446bbSMel Gorman update_nr_listpages(cc); 862748446bbSMel Gorman nr_remaining = cc->nr_migratepages; 863748446bbSMel Gorman 864748446bbSMel Gorman count_vm_event(COMPACTBLOCKS); 865748446bbSMel Gorman count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); 866748446bbSMel Gorman if (nr_remaining) 867748446bbSMel Gorman count_vm_events(COMPACTPAGEFAILED, nr_remaining); 868b7aba698SMel Gorman trace_mm_compaction_migratepages(nr_migrate - nr_remaining, 869b7aba698SMel Gorman nr_remaining); 870748446bbSMel Gorman 871748446bbSMel Gorman /* Release LRU pages not migrated */ 8729d502c1cSMinchan Kim if (err) { 873748446bbSMel Gorman putback_lru_pages(&cc->migratepages); 874748446bbSMel Gorman cc->nr_migratepages = 0; 8754bf2bba3SDavid Rientjes if (err == -ENOMEM) { 8764bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 8774bf2bba3SDavid Rientjes goto out; 878748446bbSMel Gorman } 8794bf2bba3SDavid Rientjes } 8801fb3f8caSMel Gorman 8811fb3f8caSMel Gorman /* Capture a page now if it is a suitable size */ 8821fb3f8caSMel Gorman compact_capture_page(cc); 883748446bbSMel Gorman } 884748446bbSMel Gorman 885f9e35b3bSMel Gorman out: 886748446bbSMel Gorman /* Release free pages and check accounting */ 887748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 888748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 889748446bbSMel Gorman 890748446bbSMel Gorman return ret; 891748446bbSMel Gorman } 89276ab0f53SMel Gorman 893d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone, 89477f1fe6bSMel Gorman int order, gfp_t gfp_mask, 8951fb3f8caSMel Gorman bool sync, bool *contended, 8961fb3f8caSMel Gorman struct page **page) 89756de7263SMel Gorman { 898e64c5237SShaohua Li unsigned long ret; 89956de7263SMel Gorman struct compact_control cc = { 90056de7263SMel Gorman .nr_freepages = 0, 90156de7263SMel Gorman .nr_migratepages = 0, 90256de7263SMel Gorman .order = order, 90356de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 90456de7263SMel Gorman .zone = zone, 90568e3e926SLinus Torvalds .sync = sync, 9061fb3f8caSMel Gorman .page = page, 90756de7263SMel Gorman }; 90856de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 90956de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 91056de7263SMel Gorman 911e64c5237SShaohua Li ret = compact_zone(zone, &cc); 912e64c5237SShaohua Li 913e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 914e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 915e64c5237SShaohua Li 916e64c5237SShaohua Li *contended = cc.contended; 917e64c5237SShaohua Li return ret; 91856de7263SMel Gorman } 91956de7263SMel Gorman 9205e771905SMel Gorman int sysctl_extfrag_threshold = 500; 9215e771905SMel Gorman 92256de7263SMel Gorman /** 92356de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 92456de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 92556de7263SMel Gorman * @order: The order of the current allocation 92656de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 92756de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 92877f1fe6bSMel Gorman * @sync: Whether migration is synchronous or not 929*661c4cb9SMel Gorman * @contended: Return value that is true if compaction was aborted due to lock contention 930*661c4cb9SMel Gorman * @page: Optionally capture a free page of the requested order during compaction 93156de7263SMel Gorman * 93256de7263SMel Gorman * This is the main entry point for direct page compaction. 93356de7263SMel Gorman */ 93456de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 93577f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 9361fb3f8caSMel Gorman bool sync, bool *contended, struct page **page) 93756de7263SMel Gorman { 93856de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 93956de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 94056de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 94156de7263SMel Gorman struct zoneref *z; 94256de7263SMel Gorman struct zone *zone; 94356de7263SMel Gorman int rc = COMPACT_SKIPPED; 944d95ea5d1SBartlomiej Zolnierkiewicz int alloc_flags = 0; 94556de7263SMel Gorman 9464ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 947c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 94856de7263SMel Gorman return rc; 94956de7263SMel Gorman 95056de7263SMel Gorman count_vm_event(COMPACTSTALL); 95156de7263SMel Gorman 952d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA 953d95ea5d1SBartlomiej Zolnierkiewicz if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) 954d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags |= ALLOC_CMA; 955d95ea5d1SBartlomiej Zolnierkiewicz #endif 95656de7263SMel Gorman /* Compact each zone in the list */ 95756de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 95856de7263SMel Gorman nodemask) { 95956de7263SMel Gorman int status; 96056de7263SMel Gorman 961c67fe375SMel Gorman status = compact_zone_order(zone, order, gfp_mask, sync, 9621fb3f8caSMel Gorman contended, page); 96356de7263SMel Gorman rc = max(status, rc); 96456de7263SMel Gorman 9653e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 966d95ea5d1SBartlomiej Zolnierkiewicz if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 967d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags)) 96856de7263SMel Gorman break; 96956de7263SMel Gorman } 97056de7263SMel Gorman 97156de7263SMel Gorman return rc; 97256de7263SMel Gorman } 97356de7263SMel Gorman 97456de7263SMel Gorman 97576ab0f53SMel Gorman /* Compact all zones within a node */ 9767be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 97776ab0f53SMel Gorman { 97876ab0f53SMel Gorman int zoneid; 97976ab0f53SMel Gorman struct zone *zone; 98076ab0f53SMel Gorman 98176ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 98276ab0f53SMel Gorman 98376ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 98476ab0f53SMel Gorman if (!populated_zone(zone)) 98576ab0f53SMel Gorman continue; 98676ab0f53SMel Gorman 9877be62de9SRik van Riel cc->nr_freepages = 0; 9887be62de9SRik van Riel cc->nr_migratepages = 0; 9897be62de9SRik van Riel cc->zone = zone; 9907be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 9917be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 99276ab0f53SMel Gorman 993aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 9947be62de9SRik van Riel compact_zone(zone, cc); 99576ab0f53SMel Gorman 996aff62249SRik van Riel if (cc->order > 0) { 997aff62249SRik van Riel int ok = zone_watermark_ok(zone, cc->order, 998aff62249SRik van Riel low_wmark_pages(zone), 0, 0); 999c81758fbSMinchan Kim if (ok && cc->order >= zone->compact_order_failed) 1000aff62249SRik van Riel zone->compact_order_failed = cc->order + 1; 1001aff62249SRik van Riel /* Currently async compaction is never deferred. */ 100268e3e926SLinus Torvalds else if (!ok && cc->sync) 1003aff62249SRik van Riel defer_compaction(zone, cc->order); 1004aff62249SRik van Riel } 1005aff62249SRik van Riel 10067be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 10077be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 100876ab0f53SMel Gorman } 100976ab0f53SMel Gorman 101076ab0f53SMel Gorman return 0; 101176ab0f53SMel Gorman } 101276ab0f53SMel Gorman 10137be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order) 10147be62de9SRik van Riel { 10157be62de9SRik van Riel struct compact_control cc = { 10167be62de9SRik van Riel .order = order, 101768e3e926SLinus Torvalds .sync = false, 10181fb3f8caSMel Gorman .page = NULL, 10197be62de9SRik van Riel }; 10207be62de9SRik van Riel 10217be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 10227be62de9SRik van Riel } 10237be62de9SRik van Riel 10247be62de9SRik van Riel static int compact_node(int nid) 10257be62de9SRik van Riel { 10267be62de9SRik van Riel struct compact_control cc = { 10277be62de9SRik van Riel .order = -1, 102868e3e926SLinus Torvalds .sync = true, 10291fb3f8caSMel Gorman .page = NULL, 10307be62de9SRik van Riel }; 10317be62de9SRik van Riel 10328575ec29SHugh Dickins return __compact_pgdat(NODE_DATA(nid), &cc); 10337be62de9SRik van Riel } 10347be62de9SRik van Riel 103576ab0f53SMel Gorman /* Compact all nodes in the system */ 103676ab0f53SMel Gorman static int compact_nodes(void) 103776ab0f53SMel Gorman { 103876ab0f53SMel Gorman int nid; 103976ab0f53SMel Gorman 10408575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 10418575ec29SHugh Dickins lru_add_drain_all(); 10428575ec29SHugh Dickins 104376ab0f53SMel Gorman for_each_online_node(nid) 104476ab0f53SMel Gorman compact_node(nid); 104576ab0f53SMel Gorman 104676ab0f53SMel Gorman return COMPACT_COMPLETE; 104776ab0f53SMel Gorman } 104876ab0f53SMel Gorman 104976ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 105076ab0f53SMel Gorman int sysctl_compact_memory; 105176ab0f53SMel Gorman 105276ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 105376ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 105476ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 105576ab0f53SMel Gorman { 105676ab0f53SMel Gorman if (write) 105776ab0f53SMel Gorman return compact_nodes(); 105876ab0f53SMel Gorman 105976ab0f53SMel Gorman return 0; 106076ab0f53SMel Gorman } 1061ed4a6d7fSMel Gorman 10625e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 10635e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 10645e771905SMel Gorman { 10655e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 10665e771905SMel Gorman 10675e771905SMel Gorman return 0; 10685e771905SMel Gorman } 10695e771905SMel Gorman 1070ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 107110fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev, 107210fbcf4cSKay Sievers struct device_attribute *attr, 1073ed4a6d7fSMel Gorman const char *buf, size_t count) 1074ed4a6d7fSMel Gorman { 10758575ec29SHugh Dickins int nid = dev->id; 10768575ec29SHugh Dickins 10778575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 10788575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 10798575ec29SHugh Dickins lru_add_drain_all(); 10808575ec29SHugh Dickins 10818575ec29SHugh Dickins compact_node(nid); 10828575ec29SHugh Dickins } 1083ed4a6d7fSMel Gorman 1084ed4a6d7fSMel Gorman return count; 1085ed4a6d7fSMel Gorman } 108610fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1087ed4a6d7fSMel Gorman 1088ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1089ed4a6d7fSMel Gorman { 109010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1091ed4a6d7fSMel Gorman } 1092ed4a6d7fSMel Gorman 1093ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1094ed4a6d7fSMel Gorman { 109510fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1096ed4a6d7fSMel Gorman } 1097ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1098ff9543fdSMichal Nazarewicz 1099ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1100