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 /* 5485aa125fSMichal Nazarewicz * Isolate free pages onto a private freelist. Caller must hold zone->lock. 5585aa125fSMichal Nazarewicz * If @strict is true, will abort returning 0 on any invalid PFNs or non-free 5685aa125fSMichal Nazarewicz * pages inside of the pageblock (even though it may still end up isolating 5785aa125fSMichal Nazarewicz * some pages). 5885aa125fSMichal Nazarewicz */ 5985aa125fSMichal Nazarewicz static unsigned long isolate_freepages_block(unsigned long blockpfn, 6085aa125fSMichal Nazarewicz unsigned long end_pfn, 6185aa125fSMichal Nazarewicz struct list_head *freelist, 6285aa125fSMichal Nazarewicz bool strict) 63748446bbSMel Gorman { 64b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 65748446bbSMel Gorman struct page *cursor; 66748446bbSMel Gorman 67748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 68748446bbSMel Gorman 69748446bbSMel Gorman /* Isolate free pages. This assumes the block is valid */ 70748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 71748446bbSMel Gorman int isolated, i; 72748446bbSMel Gorman struct page *page = cursor; 73748446bbSMel Gorman 7485aa125fSMichal Nazarewicz if (!pfn_valid_within(blockpfn)) { 7585aa125fSMichal Nazarewicz if (strict) 7685aa125fSMichal Nazarewicz return 0; 77748446bbSMel Gorman continue; 7885aa125fSMichal Nazarewicz } 79b7aba698SMel Gorman nr_scanned++; 80748446bbSMel Gorman 8185aa125fSMichal Nazarewicz if (!PageBuddy(page)) { 8285aa125fSMichal Nazarewicz if (strict) 8385aa125fSMichal Nazarewicz return 0; 84748446bbSMel Gorman continue; 8585aa125fSMichal Nazarewicz } 86748446bbSMel Gorman 87748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 88748446bbSMel Gorman isolated = split_free_page(page); 8985aa125fSMichal Nazarewicz if (!isolated && strict) 9085aa125fSMichal Nazarewicz return 0; 91748446bbSMel Gorman total_isolated += isolated; 92748446bbSMel Gorman for (i = 0; i < isolated; i++) { 93748446bbSMel Gorman list_add(&page->lru, freelist); 94748446bbSMel Gorman page++; 95748446bbSMel Gorman } 96748446bbSMel Gorman 97748446bbSMel Gorman /* If a page was split, advance to the end of it */ 98748446bbSMel Gorman if (isolated) { 99748446bbSMel Gorman blockpfn += isolated - 1; 100748446bbSMel Gorman cursor += isolated - 1; 101748446bbSMel Gorman } 102748446bbSMel Gorman } 103748446bbSMel Gorman 104b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 105748446bbSMel Gorman return total_isolated; 106748446bbSMel Gorman } 107748446bbSMel Gorman 10885aa125fSMichal Nazarewicz /** 10985aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 11085aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 11185aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 11285aa125fSMichal Nazarewicz * 11385aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 11485aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 11585aa125fSMichal Nazarewicz * undo its actions and return zero. 11685aa125fSMichal Nazarewicz * 11785aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 11885aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 11985aa125fSMichal Nazarewicz * a free page). 12085aa125fSMichal Nazarewicz */ 121ff9543fdSMichal Nazarewicz unsigned long 12285aa125fSMichal Nazarewicz isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn) 12385aa125fSMichal Nazarewicz { 12485aa125fSMichal Nazarewicz unsigned long isolated, pfn, block_end_pfn, flags; 12585aa125fSMichal Nazarewicz struct zone *zone = NULL; 12685aa125fSMichal Nazarewicz LIST_HEAD(freelist); 12785aa125fSMichal Nazarewicz 12885aa125fSMichal Nazarewicz if (pfn_valid(start_pfn)) 12985aa125fSMichal Nazarewicz zone = page_zone(pfn_to_page(start_pfn)); 13085aa125fSMichal Nazarewicz 13185aa125fSMichal Nazarewicz for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) { 13285aa125fSMichal Nazarewicz if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn))) 13385aa125fSMichal Nazarewicz break; 13485aa125fSMichal Nazarewicz 13585aa125fSMichal Nazarewicz /* 13685aa125fSMichal Nazarewicz * On subsequent iterations ALIGN() is actually not needed, 13785aa125fSMichal Nazarewicz * but we keep it that we not to complicate the code. 13885aa125fSMichal Nazarewicz */ 13985aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 14085aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 14185aa125fSMichal Nazarewicz 14285aa125fSMichal Nazarewicz spin_lock_irqsave(&zone->lock, flags); 14385aa125fSMichal Nazarewicz isolated = isolate_freepages_block(pfn, block_end_pfn, 14485aa125fSMichal Nazarewicz &freelist, true); 14585aa125fSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 14685aa125fSMichal Nazarewicz 14785aa125fSMichal Nazarewicz /* 14885aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 14985aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 15085aa125fSMichal Nazarewicz * non-free pages). 15185aa125fSMichal Nazarewicz */ 15285aa125fSMichal Nazarewicz if (!isolated) 15385aa125fSMichal Nazarewicz break; 15485aa125fSMichal Nazarewicz 15585aa125fSMichal Nazarewicz /* 15685aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 15785aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 15885aa125fSMichal Nazarewicz * page may span two pageblocks). 15985aa125fSMichal Nazarewicz */ 16085aa125fSMichal Nazarewicz } 16185aa125fSMichal Nazarewicz 16285aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 16385aa125fSMichal Nazarewicz map_pages(&freelist); 16485aa125fSMichal Nazarewicz 16585aa125fSMichal Nazarewicz if (pfn < end_pfn) { 16685aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 16785aa125fSMichal Nazarewicz release_freepages(&freelist); 16885aa125fSMichal Nazarewicz return 0; 16985aa125fSMichal Nazarewicz } 17085aa125fSMichal Nazarewicz 17185aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 17285aa125fSMichal Nazarewicz return pfn; 17385aa125fSMichal Nazarewicz } 17485aa125fSMichal Nazarewicz 175748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 176748446bbSMel Gorman static void acct_isolated(struct zone *zone, struct compact_control *cc) 177748446bbSMel Gorman { 178748446bbSMel Gorman struct page *page; 179b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 180748446bbSMel Gorman 181b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 182b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 183748446bbSMel Gorman 184b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 185b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 186748446bbSMel Gorman } 187748446bbSMel Gorman 188748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 189748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 190748446bbSMel Gorman { 191bc693045SMinchan Kim unsigned long active, inactive, isolated; 192748446bbSMel Gorman 193748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 194748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 195bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 196bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 197748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 198748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 199748446bbSMel Gorman 200bc693045SMinchan Kim return isolated > (inactive + active) / 2; 201748446bbSMel Gorman } 202748446bbSMel Gorman 2032fe86e00SMichal Nazarewicz /** 2042fe86e00SMichal Nazarewicz * isolate_migratepages_range() - isolate all migrate-able pages in range. 2052fe86e00SMichal Nazarewicz * @zone: Zone pages are in. 2062fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 2072fe86e00SMichal Nazarewicz * @low_pfn: The first PFN of the range. 2082fe86e00SMichal Nazarewicz * @end_pfn: The one-past-the-last PFN of the range. 2092fe86e00SMichal Nazarewicz * 2102fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 2112fe86e00SMichal Nazarewicz * [low_pfn, end_pfn). Returns zero if there is a fatal signal 2122fe86e00SMichal Nazarewicz * pending), otherwise PFN of the first page that was not scanned 2132fe86e00SMichal Nazarewicz * (which may be both less, equal to or more then end_pfn). 2142fe86e00SMichal Nazarewicz * 2152fe86e00SMichal Nazarewicz * Assumes that cc->migratepages is empty and cc->nr_migratepages is 2162fe86e00SMichal Nazarewicz * zero. 2172fe86e00SMichal Nazarewicz * 2182fe86e00SMichal Nazarewicz * Apart from cc->migratepages and cc->nr_migratetypes this function 2192fe86e00SMichal Nazarewicz * does not modify any cc's fields, in particular it does not modify 2202fe86e00SMichal Nazarewicz * (or read for that matter) cc->migrate_pfn. 221748446bbSMel Gorman */ 222ff9543fdSMichal Nazarewicz unsigned long 2232fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc, 2242fe86e00SMichal Nazarewicz unsigned long low_pfn, unsigned long end_pfn) 225748446bbSMel Gorman { 2269927af74SMel Gorman unsigned long last_pageblock_nr = 0, pageblock_nr; 227b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 228748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 229f3fd4a61SKonstantin Khlebnikov isolate_mode_t mode = 0; 230fa9add64SHugh Dickins struct lruvec *lruvec; 231748446bbSMel Gorman 232748446bbSMel Gorman /* 233748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 234748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 235748446bbSMel Gorman * delay for some time until fewer pages are isolated 236748446bbSMel Gorman */ 237748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 238f9e35b3bSMel Gorman /* async migration should just abort */ 23968e3e926SLinus Torvalds if (!cc->sync) 2402fe86e00SMichal Nazarewicz return 0; 241f9e35b3bSMel Gorman 242748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 243748446bbSMel Gorman 244748446bbSMel Gorman if (fatal_signal_pending(current)) 2452fe86e00SMichal Nazarewicz return 0; 246748446bbSMel Gorman } 247748446bbSMel Gorman 248748446bbSMel Gorman /* Time to isolate some pages for migration */ 249b2eef8c0SAndrea Arcangeli cond_resched(); 250748446bbSMel Gorman spin_lock_irq(&zone->lru_lock); 251748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 252748446bbSMel Gorman struct page *page; 253b2eef8c0SAndrea Arcangeli bool locked = true; 254b2eef8c0SAndrea Arcangeli 255b2eef8c0SAndrea Arcangeli /* give a chance to irqs before checking need_resched() */ 256b2eef8c0SAndrea Arcangeli if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) { 257b2eef8c0SAndrea Arcangeli spin_unlock_irq(&zone->lru_lock); 258b2eef8c0SAndrea Arcangeli locked = false; 259b2eef8c0SAndrea Arcangeli } 260b2eef8c0SAndrea Arcangeli if (need_resched() || spin_is_contended(&zone->lru_lock)) { 261b2eef8c0SAndrea Arcangeli if (locked) 262b2eef8c0SAndrea Arcangeli spin_unlock_irq(&zone->lru_lock); 263b2eef8c0SAndrea Arcangeli cond_resched(); 264b2eef8c0SAndrea Arcangeli spin_lock_irq(&zone->lru_lock); 265b2eef8c0SAndrea Arcangeli if (fatal_signal_pending(current)) 266b2eef8c0SAndrea Arcangeli break; 267b2eef8c0SAndrea Arcangeli } else if (!locked) 268b2eef8c0SAndrea Arcangeli spin_lock_irq(&zone->lru_lock); 269b2eef8c0SAndrea Arcangeli 2700bf380bcSMel Gorman /* 2710bf380bcSMel Gorman * migrate_pfn does not necessarily start aligned to a 2720bf380bcSMel Gorman * pageblock. Ensure that pfn_valid is called when moving 2730bf380bcSMel Gorman * into a new MAX_ORDER_NR_PAGES range in case of large 2740bf380bcSMel Gorman * memory holes within the zone 2750bf380bcSMel Gorman */ 2760bf380bcSMel Gorman if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { 2770bf380bcSMel Gorman if (!pfn_valid(low_pfn)) { 2780bf380bcSMel Gorman low_pfn += MAX_ORDER_NR_PAGES - 1; 2790bf380bcSMel Gorman continue; 2800bf380bcSMel Gorman } 2810bf380bcSMel Gorman } 2820bf380bcSMel Gorman 283748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 284748446bbSMel Gorman continue; 285b7aba698SMel Gorman nr_scanned++; 286748446bbSMel Gorman 287dc908600SMel Gorman /* 288dc908600SMel Gorman * Get the page and ensure the page is within the same zone. 289dc908600SMel Gorman * See the comment in isolate_freepages about overlapping 290dc908600SMel Gorman * nodes. It is deliberate that the new zone lock is not taken 291dc908600SMel Gorman * as memory compaction should not move pages between nodes. 292dc908600SMel Gorman */ 293748446bbSMel Gorman page = pfn_to_page(low_pfn); 294dc908600SMel Gorman if (page_zone(page) != zone) 295dc908600SMel Gorman continue; 296dc908600SMel Gorman 297dc908600SMel Gorman /* Skip if free */ 298748446bbSMel Gorman if (PageBuddy(page)) 299748446bbSMel Gorman continue; 300748446bbSMel Gorman 3019927af74SMel Gorman /* 3029927af74SMel Gorman * For async migration, also only scan in MOVABLE blocks. Async 3039927af74SMel Gorman * migration is optimistic to see if the minimum amount of work 3049927af74SMel Gorman * satisfies the allocation 3059927af74SMel Gorman */ 3069927af74SMel Gorman pageblock_nr = low_pfn >> pageblock_order; 30768e3e926SLinus Torvalds if (!cc->sync && last_pageblock_nr != pageblock_nr && 30847118af0SMichal Nazarewicz !migrate_async_suitable(get_pageblock_migratetype(page))) { 3099927af74SMel Gorman low_pfn += pageblock_nr_pages; 3109927af74SMel Gorman low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 3119927af74SMel Gorman last_pageblock_nr = pageblock_nr; 3129927af74SMel Gorman continue; 3139927af74SMel Gorman } 3149927af74SMel Gorman 315bc835011SAndrea Arcangeli if (!PageLRU(page)) 316bc835011SAndrea Arcangeli continue; 317bc835011SAndrea Arcangeli 318bc835011SAndrea Arcangeli /* 319bc835011SAndrea Arcangeli * PageLRU is set, and lru_lock excludes isolation, 320bc835011SAndrea Arcangeli * splitting and collapsing (collapsing has already 321bc835011SAndrea Arcangeli * happened if PageLRU is set). 322bc835011SAndrea Arcangeli */ 323bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 324bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 325bc835011SAndrea Arcangeli continue; 326bc835011SAndrea Arcangeli } 327bc835011SAndrea Arcangeli 32868e3e926SLinus Torvalds if (!cc->sync) 329c8244935SMel Gorman mode |= ISOLATE_ASYNC_MIGRATE; 330c8244935SMel Gorman 331fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 332fa9add64SHugh Dickins 333748446bbSMel Gorman /* Try isolate the page */ 334f3fd4a61SKonstantin Khlebnikov if (__isolate_lru_page(page, mode) != 0) 335748446bbSMel Gorman continue; 336748446bbSMel Gorman 337bc835011SAndrea Arcangeli VM_BUG_ON(PageTransCompound(page)); 338bc835011SAndrea Arcangeli 339748446bbSMel Gorman /* Successfully isolated */ 340fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 341748446bbSMel Gorman list_add(&page->lru, migratelist); 342748446bbSMel Gorman cc->nr_migratepages++; 343b7aba698SMel Gorman nr_isolated++; 344748446bbSMel Gorman 345748446bbSMel Gorman /* Avoid isolating too much */ 34631b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 34731b8384aSHillf Danton ++low_pfn; 348748446bbSMel Gorman break; 349748446bbSMel Gorman } 35031b8384aSHillf Danton } 351748446bbSMel Gorman 352748446bbSMel Gorman acct_isolated(zone, cc); 353748446bbSMel Gorman 354748446bbSMel Gorman spin_unlock_irq(&zone->lru_lock); 355748446bbSMel Gorman 356b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 357b7aba698SMel Gorman 3582fe86e00SMichal Nazarewicz return low_pfn; 3592fe86e00SMichal Nazarewicz } 3602fe86e00SMichal Nazarewicz 361ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 362ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 363ff9543fdSMichal Nazarewicz 36468e3e926SLinus Torvalds /* Returns true if the page is within a block suitable for migration to */ 36568e3e926SLinus Torvalds static bool suitable_migration_target(struct page *page) 3662fe86e00SMichal Nazarewicz { 3672fe86e00SMichal Nazarewicz 368ff9543fdSMichal Nazarewicz int migratetype = get_pageblock_migratetype(page); 3692fe86e00SMichal Nazarewicz 370ff9543fdSMichal Nazarewicz /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 371ff9543fdSMichal Nazarewicz if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 37268e3e926SLinus Torvalds return false; 3732fe86e00SMichal Nazarewicz 374ff9543fdSMichal Nazarewicz /* If the page is a large free page, then allow migration */ 375ff9543fdSMichal Nazarewicz if (PageBuddy(page) && page_order(page) >= pageblock_order) 37668e3e926SLinus Torvalds return true; 377ff9543fdSMichal Nazarewicz 37847118af0SMichal Nazarewicz /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 37968e3e926SLinus Torvalds if (migrate_async_suitable(migratetype)) 38068e3e926SLinus Torvalds return true; 381ff9543fdSMichal Nazarewicz 382ff9543fdSMichal Nazarewicz /* Otherwise skip the block */ 38368e3e926SLinus Torvalds return false; 3842fe86e00SMichal Nazarewicz } 3852fe86e00SMichal Nazarewicz 386ff9543fdSMichal Nazarewicz /* 387ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 388ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 389ff9543fdSMichal Nazarewicz */ 390ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone, 391ff9543fdSMichal Nazarewicz struct compact_control *cc) 392ff9543fdSMichal Nazarewicz { 393ff9543fdSMichal Nazarewicz struct page *page; 394ff9543fdSMichal Nazarewicz unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; 395ff9543fdSMichal Nazarewicz unsigned long flags; 396ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 397ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 3982fe86e00SMichal Nazarewicz 399ff9543fdSMichal Nazarewicz /* 400ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 401ff9543fdSMichal Nazarewicz * scanned from (or the end of the zone if starting). The low point 402ff9543fdSMichal Nazarewicz * is the end of the pageblock the migration scanner is using. 403ff9543fdSMichal Nazarewicz */ 404ff9543fdSMichal Nazarewicz pfn = cc->free_pfn; 405ff9543fdSMichal Nazarewicz low_pfn = cc->migrate_pfn + pageblock_nr_pages; 4062fe86e00SMichal Nazarewicz 407ff9543fdSMichal Nazarewicz /* 408ff9543fdSMichal Nazarewicz * Take care that if the migration scanner is at the end of the zone 409ff9543fdSMichal Nazarewicz * that the free scanner does not accidentally move to the next zone 410ff9543fdSMichal Nazarewicz * in the next isolation cycle. 411ff9543fdSMichal Nazarewicz */ 412ff9543fdSMichal Nazarewicz high_pfn = min(low_pfn, pfn); 413ff9543fdSMichal Nazarewicz 414ff9543fdSMichal Nazarewicz zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 415ff9543fdSMichal Nazarewicz 416ff9543fdSMichal Nazarewicz /* 417ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 418ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 419ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 420ff9543fdSMichal Nazarewicz */ 421ff9543fdSMichal Nazarewicz for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; 422ff9543fdSMichal Nazarewicz pfn -= pageblock_nr_pages) { 423ff9543fdSMichal Nazarewicz unsigned long isolated; 424ff9543fdSMichal Nazarewicz 4257db8889aSRik van Riel /* 4267db8889aSRik van Riel * Skip ahead if another thread is compacting in the area 4277db8889aSRik van Riel * simultaneously. If we wrapped around, we can only skip 4287db8889aSRik van Riel * ahead if zone->compact_cached_free_pfn also wrapped to 4297db8889aSRik van Riel * above our starting point. 4307db8889aSRik van Riel */ 4317db8889aSRik van Riel if (cc->order > 0 && (!cc->wrapped || 4327db8889aSRik van Riel zone->compact_cached_free_pfn > 4337db8889aSRik van Riel cc->start_free_pfn)) 4347db8889aSRik van Riel pfn = min(pfn, zone->compact_cached_free_pfn); 4357db8889aSRik van Riel 436ff9543fdSMichal Nazarewicz if (!pfn_valid(pfn)) 437ff9543fdSMichal Nazarewicz continue; 438ff9543fdSMichal Nazarewicz 439ff9543fdSMichal Nazarewicz /* 440ff9543fdSMichal Nazarewicz * Check for overlapping nodes/zones. It's possible on some 441ff9543fdSMichal Nazarewicz * configurations to have a setup like 442ff9543fdSMichal Nazarewicz * node0 node1 node0 443ff9543fdSMichal Nazarewicz * i.e. it's possible that all pages within a zones range of 444ff9543fdSMichal Nazarewicz * pages do not belong to a single zone. 445ff9543fdSMichal Nazarewicz */ 446ff9543fdSMichal Nazarewicz page = pfn_to_page(pfn); 447ff9543fdSMichal Nazarewicz if (page_zone(page) != zone) 448ff9543fdSMichal Nazarewicz continue; 449ff9543fdSMichal Nazarewicz 450ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 45168e3e926SLinus Torvalds if (!suitable_migration_target(page)) 452ff9543fdSMichal Nazarewicz continue; 45368e3e926SLinus Torvalds 454ff9543fdSMichal Nazarewicz /* 455ff9543fdSMichal Nazarewicz * Found a block suitable for isolating free pages from. Now 456ff9543fdSMichal Nazarewicz * we disabled interrupts, double check things are ok and 457ff9543fdSMichal Nazarewicz * isolate the pages. This is to minimise the time IRQs 458ff9543fdSMichal Nazarewicz * are disabled 459ff9543fdSMichal Nazarewicz */ 460ff9543fdSMichal Nazarewicz isolated = 0; 461ff9543fdSMichal Nazarewicz spin_lock_irqsave(&zone->lock, flags); 46268e3e926SLinus Torvalds if (suitable_migration_target(page)) { 463ff9543fdSMichal Nazarewicz end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); 464ff9543fdSMichal Nazarewicz isolated = isolate_freepages_block(pfn, end_pfn, 465ff9543fdSMichal Nazarewicz freelist, false); 466ff9543fdSMichal Nazarewicz nr_freepages += isolated; 46768e3e926SLinus Torvalds } 468ff9543fdSMichal Nazarewicz spin_unlock_irqrestore(&zone->lock, flags); 469ff9543fdSMichal Nazarewicz 470ff9543fdSMichal Nazarewicz /* 471ff9543fdSMichal Nazarewicz * Record the highest PFN we isolated pages from. When next 472ff9543fdSMichal Nazarewicz * looking for free pages, the search will restart here as 473ff9543fdSMichal Nazarewicz * page migration may have returned some pages to the allocator 474ff9543fdSMichal Nazarewicz */ 4757db8889aSRik van Riel if (isolated) { 476ff9543fdSMichal Nazarewicz high_pfn = max(high_pfn, pfn); 4777db8889aSRik van Riel if (cc->order > 0) 4787db8889aSRik van Riel zone->compact_cached_free_pfn = high_pfn; 4797db8889aSRik van Riel } 480ff9543fdSMichal Nazarewicz } 481ff9543fdSMichal Nazarewicz 482ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 483ff9543fdSMichal Nazarewicz map_pages(freelist); 484ff9543fdSMichal Nazarewicz 485ff9543fdSMichal Nazarewicz cc->free_pfn = high_pfn; 486ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 487748446bbSMel Gorman } 488748446bbSMel Gorman 489748446bbSMel Gorman /* 490748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 491748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 492748446bbSMel Gorman */ 493748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 494748446bbSMel Gorman unsigned long data, 495748446bbSMel Gorman int **result) 496748446bbSMel Gorman { 497748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 498748446bbSMel Gorman struct page *freepage; 499748446bbSMel Gorman 500748446bbSMel Gorman /* Isolate free pages if necessary */ 501748446bbSMel Gorman if (list_empty(&cc->freepages)) { 502748446bbSMel Gorman isolate_freepages(cc->zone, cc); 503748446bbSMel Gorman 504748446bbSMel Gorman if (list_empty(&cc->freepages)) 505748446bbSMel Gorman return NULL; 506748446bbSMel Gorman } 507748446bbSMel Gorman 508748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 509748446bbSMel Gorman list_del(&freepage->lru); 510748446bbSMel Gorman cc->nr_freepages--; 511748446bbSMel Gorman 512748446bbSMel Gorman return freepage; 513748446bbSMel Gorman } 514748446bbSMel Gorman 515748446bbSMel Gorman /* 516748446bbSMel Gorman * We cannot control nr_migratepages and nr_freepages fully when migration is 517748446bbSMel Gorman * running as migrate_pages() has no knowledge of compact_control. When 518748446bbSMel Gorman * migration is complete, we count the number of pages on the lists by hand. 519748446bbSMel Gorman */ 520748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc) 521748446bbSMel Gorman { 522748446bbSMel Gorman int nr_migratepages = 0; 523748446bbSMel Gorman int nr_freepages = 0; 524748446bbSMel Gorman struct page *page; 525748446bbSMel Gorman 526748446bbSMel Gorman list_for_each_entry(page, &cc->migratepages, lru) 527748446bbSMel Gorman nr_migratepages++; 528748446bbSMel Gorman list_for_each_entry(page, &cc->freepages, lru) 529748446bbSMel Gorman nr_freepages++; 530748446bbSMel Gorman 531748446bbSMel Gorman cc->nr_migratepages = nr_migratepages; 532748446bbSMel Gorman cc->nr_freepages = nr_freepages; 533748446bbSMel Gorman } 534748446bbSMel Gorman 535ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 536ff9543fdSMichal Nazarewicz typedef enum { 537ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 538ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 539ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 540ff9543fdSMichal Nazarewicz } isolate_migrate_t; 541ff9543fdSMichal Nazarewicz 542ff9543fdSMichal Nazarewicz /* 543ff9543fdSMichal Nazarewicz * Isolate all pages that can be migrated from the block pointed to by 544ff9543fdSMichal Nazarewicz * the migrate scanner within compact_control. 545ff9543fdSMichal Nazarewicz */ 546ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 547ff9543fdSMichal Nazarewicz struct compact_control *cc) 548ff9543fdSMichal Nazarewicz { 549ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 550ff9543fdSMichal Nazarewicz 551ff9543fdSMichal Nazarewicz /* Do not scan outside zone boundaries */ 552ff9543fdSMichal Nazarewicz low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); 553ff9543fdSMichal Nazarewicz 554ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 555ff9543fdSMichal Nazarewicz end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); 556ff9543fdSMichal Nazarewicz 557ff9543fdSMichal Nazarewicz /* Do not cross the free scanner or scan within a memory hole */ 558ff9543fdSMichal Nazarewicz if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { 559ff9543fdSMichal Nazarewicz cc->migrate_pfn = end_pfn; 560ff9543fdSMichal Nazarewicz return ISOLATE_NONE; 561ff9543fdSMichal Nazarewicz } 562ff9543fdSMichal Nazarewicz 563ff9543fdSMichal Nazarewicz /* Perform the isolation */ 564ff9543fdSMichal Nazarewicz low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); 565ff9543fdSMichal Nazarewicz if (!low_pfn) 566ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 567ff9543fdSMichal Nazarewicz 568ff9543fdSMichal Nazarewicz cc->migrate_pfn = low_pfn; 569ff9543fdSMichal Nazarewicz 570ff9543fdSMichal Nazarewicz return ISOLATE_SUCCESS; 571ff9543fdSMichal Nazarewicz } 572ff9543fdSMichal Nazarewicz 5737db8889aSRik van Riel /* 5747db8889aSRik van Riel * Returns the start pfn of the last page block in a zone. This is the starting 5757db8889aSRik van Riel * point for full compaction of a zone. Compaction searches for free pages from 5767db8889aSRik van Riel * the end of each zone, while isolate_freepages_block scans forward inside each 5777db8889aSRik van Riel * page block. 5787db8889aSRik van Riel */ 5797db8889aSRik van Riel static unsigned long start_free_pfn(struct zone *zone) 5807db8889aSRik van Riel { 5817db8889aSRik van Riel unsigned long free_pfn; 5827db8889aSRik van Riel free_pfn = zone->zone_start_pfn + zone->spanned_pages; 5837db8889aSRik van Riel free_pfn &= ~(pageblock_nr_pages-1); 5847db8889aSRik van Riel return free_pfn; 5857db8889aSRik van Riel } 5867db8889aSRik van Riel 587748446bbSMel Gorman static int compact_finished(struct zone *zone, 588748446bbSMel Gorman struct compact_control *cc) 589748446bbSMel Gorman { 59056de7263SMel Gorman unsigned int order; 5915a03b051SAndrea Arcangeli unsigned long watermark; 59256de7263SMel Gorman 593748446bbSMel Gorman if (fatal_signal_pending(current)) 594748446bbSMel Gorman return COMPACT_PARTIAL; 595748446bbSMel Gorman 5967db8889aSRik van Riel /* 5977db8889aSRik van Riel * A full (order == -1) compaction run starts at the beginning and 5987db8889aSRik van Riel * end of a zone; it completes when the migrate and free scanner meet. 5997db8889aSRik van Riel * A partial (order > 0) compaction can start with the free scanner 6007db8889aSRik van Riel * at a random point in the zone, and may have to restart. 6017db8889aSRik van Riel */ 6027db8889aSRik van Riel if (cc->free_pfn <= cc->migrate_pfn) { 6037db8889aSRik van Riel if (cc->order > 0 && !cc->wrapped) { 6047db8889aSRik van Riel /* We started partway through; restart at the end. */ 6057db8889aSRik van Riel unsigned long free_pfn = start_free_pfn(zone); 6067db8889aSRik van Riel zone->compact_cached_free_pfn = free_pfn; 6077db8889aSRik van Riel cc->free_pfn = free_pfn; 6087db8889aSRik van Riel cc->wrapped = 1; 6097db8889aSRik van Riel return COMPACT_CONTINUE; 6107db8889aSRik van Riel } 6117db8889aSRik van Riel return COMPACT_COMPLETE; 6127db8889aSRik van Riel } 6137db8889aSRik van Riel 6147db8889aSRik van Riel /* We wrapped around and ended up where we started. */ 6157db8889aSRik van Riel if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn) 616748446bbSMel Gorman return COMPACT_COMPLETE; 617748446bbSMel Gorman 61882478fb7SJohannes Weiner /* 61982478fb7SJohannes Weiner * order == -1 is expected when compacting via 62082478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 62182478fb7SJohannes Weiner */ 62256de7263SMel Gorman if (cc->order == -1) 62356de7263SMel Gorman return COMPACT_CONTINUE; 62456de7263SMel Gorman 6253957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 6263957c776SMichal Hocko watermark = low_wmark_pages(zone); 6273957c776SMichal Hocko watermark += (1 << cc->order); 6283957c776SMichal Hocko 6293957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 6303957c776SMichal Hocko return COMPACT_CONTINUE; 6313957c776SMichal Hocko 63256de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 63356de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 63456de7263SMel Gorman /* Job done if page is free of the right migratetype */ 63556de7263SMel Gorman if (!list_empty(&zone->free_area[order].free_list[cc->migratetype])) 63656de7263SMel Gorman return COMPACT_PARTIAL; 63756de7263SMel Gorman 63856de7263SMel Gorman /* Job done if allocation would set block type */ 63956de7263SMel Gorman if (order >= pageblock_order && zone->free_area[order].nr_free) 64056de7263SMel Gorman return COMPACT_PARTIAL; 64156de7263SMel Gorman } 64256de7263SMel Gorman 643748446bbSMel Gorman return COMPACT_CONTINUE; 644748446bbSMel Gorman } 645748446bbSMel Gorman 6463e7d3449SMel Gorman /* 6473e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 6483e7d3449SMel Gorman * Returns 6493e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 6503e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 6513e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 6523e7d3449SMel Gorman */ 6533e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 6543e7d3449SMel Gorman { 6553e7d3449SMel Gorman int fragindex; 6563e7d3449SMel Gorman unsigned long watermark; 6573e7d3449SMel Gorman 6583e7d3449SMel Gorman /* 6593957c776SMichal Hocko * order == -1 is expected when compacting via 6603957c776SMichal Hocko * /proc/sys/vm/compact_memory 6613957c776SMichal Hocko */ 6623957c776SMichal Hocko if (order == -1) 6633957c776SMichal Hocko return COMPACT_CONTINUE; 6643957c776SMichal Hocko 6653957c776SMichal Hocko /* 6663e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 6673e7d3449SMel Gorman * This is because during migration, copies of pages need to be 6683e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 6693e7d3449SMel Gorman */ 6703e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 6713e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 6723e7d3449SMel Gorman return COMPACT_SKIPPED; 6733e7d3449SMel Gorman 6743e7d3449SMel Gorman /* 6753e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 6763e7d3449SMel Gorman * low memory or external fragmentation 6773e7d3449SMel Gorman * 678a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 679a582a738SShaohua Li * watermarks 6803e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 6813e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 6823e7d3449SMel Gorman * 6833e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 6843e7d3449SMel Gorman */ 6853e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 6863e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 6873e7d3449SMel Gorman return COMPACT_SKIPPED; 6883e7d3449SMel Gorman 689a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 690a582a738SShaohua Li 0, 0)) 6913e7d3449SMel Gorman return COMPACT_PARTIAL; 6923e7d3449SMel Gorman 6933e7d3449SMel Gorman return COMPACT_CONTINUE; 6943e7d3449SMel Gorman } 6953e7d3449SMel Gorman 696748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 697748446bbSMel Gorman { 698748446bbSMel Gorman int ret; 699748446bbSMel Gorman 7003e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 7013e7d3449SMel Gorman switch (ret) { 7023e7d3449SMel Gorman case COMPACT_PARTIAL: 7033e7d3449SMel Gorman case COMPACT_SKIPPED: 7043e7d3449SMel Gorman /* Compaction is likely to fail */ 7053e7d3449SMel Gorman return ret; 7063e7d3449SMel Gorman case COMPACT_CONTINUE: 7073e7d3449SMel Gorman /* Fall through to compaction */ 7083e7d3449SMel Gorman ; 7093e7d3449SMel Gorman } 7103e7d3449SMel Gorman 711748446bbSMel Gorman /* Setup to move all movable pages to the end of the zone */ 712748446bbSMel Gorman cc->migrate_pfn = zone->zone_start_pfn; 7137db8889aSRik van Riel 7147db8889aSRik van Riel if (cc->order > 0) { 7157db8889aSRik van Riel /* Incremental compaction. Start where the last one stopped. */ 7167db8889aSRik van Riel cc->free_pfn = zone->compact_cached_free_pfn; 7177db8889aSRik van Riel cc->start_free_pfn = cc->free_pfn; 7187db8889aSRik van Riel } else { 7197db8889aSRik van Riel /* Order == -1 starts at the end of the zone. */ 7207db8889aSRik van Riel cc->free_pfn = start_free_pfn(zone); 7217db8889aSRik van Riel } 722748446bbSMel Gorman 723748446bbSMel Gorman migrate_prep_local(); 724748446bbSMel Gorman 725748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 726748446bbSMel Gorman unsigned long nr_migrate, nr_remaining; 7279d502c1cSMinchan Kim int err; 728748446bbSMel Gorman 729f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 730f9e35b3bSMel Gorman case ISOLATE_ABORT: 731f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 732f9e35b3bSMel Gorman goto out; 733f9e35b3bSMel Gorman case ISOLATE_NONE: 734748446bbSMel Gorman continue; 735f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 736f9e35b3bSMel Gorman ; 737f9e35b3bSMel Gorman } 738748446bbSMel Gorman 739748446bbSMel Gorman nr_migrate = cc->nr_migratepages; 7409d502c1cSMinchan Kim err = migrate_pages(&cc->migratepages, compaction_alloc, 74168e3e926SLinus Torvalds (unsigned long)cc, false, 74268e3e926SLinus Torvalds cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); 743748446bbSMel Gorman update_nr_listpages(cc); 744748446bbSMel Gorman nr_remaining = cc->nr_migratepages; 745748446bbSMel Gorman 746748446bbSMel Gorman count_vm_event(COMPACTBLOCKS); 747748446bbSMel Gorman count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); 748748446bbSMel Gorman if (nr_remaining) 749748446bbSMel Gorman count_vm_events(COMPACTPAGEFAILED, nr_remaining); 750b7aba698SMel Gorman trace_mm_compaction_migratepages(nr_migrate - nr_remaining, 751b7aba698SMel Gorman nr_remaining); 752748446bbSMel Gorman 753748446bbSMel Gorman /* Release LRU pages not migrated */ 7549d502c1cSMinchan Kim if (err) { 755748446bbSMel Gorman putback_lru_pages(&cc->migratepages); 756748446bbSMel Gorman cc->nr_migratepages = 0; 7574bf2bba3SDavid Rientjes if (err == -ENOMEM) { 7584bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 7594bf2bba3SDavid Rientjes goto out; 760748446bbSMel Gorman } 7614bf2bba3SDavid Rientjes } 762748446bbSMel Gorman } 763748446bbSMel Gorman 764f9e35b3bSMel Gorman out: 765748446bbSMel Gorman /* Release free pages and check accounting */ 766748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 767748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 768748446bbSMel Gorman 769748446bbSMel Gorman return ret; 770748446bbSMel Gorman } 77176ab0f53SMel Gorman 772d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone, 77377f1fe6bSMel Gorman int order, gfp_t gfp_mask, 77468e3e926SLinus Torvalds bool sync) 77556de7263SMel Gorman { 77656de7263SMel Gorman struct compact_control cc = { 77756de7263SMel Gorman .nr_freepages = 0, 77856de7263SMel Gorman .nr_migratepages = 0, 77956de7263SMel Gorman .order = order, 78056de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 78156de7263SMel Gorman .zone = zone, 78268e3e926SLinus Torvalds .sync = sync, 78356de7263SMel Gorman }; 78456de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 78556de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 78656de7263SMel Gorman 78768e3e926SLinus Torvalds return compact_zone(zone, &cc); 78856de7263SMel Gorman } 78956de7263SMel Gorman 7905e771905SMel Gorman int sysctl_extfrag_threshold = 500; 7915e771905SMel Gorman 79256de7263SMel Gorman /** 79356de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 79456de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 79556de7263SMel Gorman * @order: The order of the current allocation 79656de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 79756de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 79877f1fe6bSMel Gorman * @sync: Whether migration is synchronous or not 79956de7263SMel Gorman * 80056de7263SMel Gorman * This is the main entry point for direct page compaction. 80156de7263SMel Gorman */ 80256de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 80377f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 80477f1fe6bSMel Gorman bool sync) 80556de7263SMel Gorman { 80656de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 80756de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 80856de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 80956de7263SMel Gorman struct zoneref *z; 81056de7263SMel Gorman struct zone *zone; 81156de7263SMel Gorman int rc = COMPACT_SKIPPED; 81256de7263SMel Gorman 81356de7263SMel Gorman /* 81456de7263SMel Gorman * Check whether it is worth even starting compaction. The order check is 81556de7263SMel Gorman * made because an assumption is made that the page allocator can satisfy 81656de7263SMel Gorman * the "cheaper" orders without taking special steps 81756de7263SMel Gorman */ 818c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 81956de7263SMel Gorman return rc; 82056de7263SMel Gorman 82156de7263SMel Gorman count_vm_event(COMPACTSTALL); 82256de7263SMel Gorman 82356de7263SMel Gorman /* Compact each zone in the list */ 82456de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 82556de7263SMel Gorman nodemask) { 82656de7263SMel Gorman int status; 82756de7263SMel Gorman 82868e3e926SLinus Torvalds status = compact_zone_order(zone, order, gfp_mask, sync); 82956de7263SMel Gorman rc = max(status, rc); 83056de7263SMel Gorman 8313e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 8323e7d3449SMel Gorman if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0)) 83356de7263SMel Gorman break; 83456de7263SMel Gorman } 83556de7263SMel Gorman 83656de7263SMel Gorman return rc; 83756de7263SMel Gorman } 83856de7263SMel Gorman 83956de7263SMel Gorman 84076ab0f53SMel Gorman /* Compact all zones within a node */ 8417be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 84276ab0f53SMel Gorman { 84376ab0f53SMel Gorman int zoneid; 84476ab0f53SMel Gorman struct zone *zone; 84576ab0f53SMel Gorman 84676ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 84776ab0f53SMel Gorman 84876ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 84976ab0f53SMel Gorman if (!populated_zone(zone)) 85076ab0f53SMel Gorman continue; 85176ab0f53SMel Gorman 8527be62de9SRik van Riel cc->nr_freepages = 0; 8537be62de9SRik van Riel cc->nr_migratepages = 0; 8547be62de9SRik van Riel cc->zone = zone; 8557be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 8567be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 85776ab0f53SMel Gorman 858aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 8597be62de9SRik van Riel compact_zone(zone, cc); 86076ab0f53SMel Gorman 861aff62249SRik van Riel if (cc->order > 0) { 862aff62249SRik van Riel int ok = zone_watermark_ok(zone, cc->order, 863aff62249SRik van Riel low_wmark_pages(zone), 0, 0); 864*c81758fbSMinchan Kim if (ok && cc->order >= zone->compact_order_failed) 865aff62249SRik van Riel zone->compact_order_failed = cc->order + 1; 866aff62249SRik van Riel /* Currently async compaction is never deferred. */ 86768e3e926SLinus Torvalds else if (!ok && cc->sync) 868aff62249SRik van Riel defer_compaction(zone, cc->order); 869aff62249SRik van Riel } 870aff62249SRik van Riel 8717be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 8727be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 87376ab0f53SMel Gorman } 87476ab0f53SMel Gorman 87576ab0f53SMel Gorman return 0; 87676ab0f53SMel Gorman } 87776ab0f53SMel Gorman 8787be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order) 8797be62de9SRik van Riel { 8807be62de9SRik van Riel struct compact_control cc = { 8817be62de9SRik van Riel .order = order, 88268e3e926SLinus Torvalds .sync = false, 8837be62de9SRik van Riel }; 8847be62de9SRik van Riel 8857be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 8867be62de9SRik van Riel } 8877be62de9SRik van Riel 8887be62de9SRik van Riel static int compact_node(int nid) 8897be62de9SRik van Riel { 8907be62de9SRik van Riel struct compact_control cc = { 8917be62de9SRik van Riel .order = -1, 89268e3e926SLinus Torvalds .sync = true, 8937be62de9SRik van Riel }; 8947be62de9SRik van Riel 8958575ec29SHugh Dickins return __compact_pgdat(NODE_DATA(nid), &cc); 8967be62de9SRik van Riel } 8977be62de9SRik van Riel 89876ab0f53SMel Gorman /* Compact all nodes in the system */ 89976ab0f53SMel Gorman static int compact_nodes(void) 90076ab0f53SMel Gorman { 90176ab0f53SMel Gorman int nid; 90276ab0f53SMel Gorman 9038575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 9048575ec29SHugh Dickins lru_add_drain_all(); 9058575ec29SHugh Dickins 90676ab0f53SMel Gorman for_each_online_node(nid) 90776ab0f53SMel Gorman compact_node(nid); 90876ab0f53SMel Gorman 90976ab0f53SMel Gorman return COMPACT_COMPLETE; 91076ab0f53SMel Gorman } 91176ab0f53SMel Gorman 91276ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 91376ab0f53SMel Gorman int sysctl_compact_memory; 91476ab0f53SMel Gorman 91576ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 91676ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 91776ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 91876ab0f53SMel Gorman { 91976ab0f53SMel Gorman if (write) 92076ab0f53SMel Gorman return compact_nodes(); 92176ab0f53SMel Gorman 92276ab0f53SMel Gorman return 0; 92376ab0f53SMel Gorman } 924ed4a6d7fSMel Gorman 9255e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 9265e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 9275e771905SMel Gorman { 9285e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 9295e771905SMel Gorman 9305e771905SMel Gorman return 0; 9315e771905SMel Gorman } 9325e771905SMel Gorman 933ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 93410fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev, 93510fbcf4cSKay Sievers struct device_attribute *attr, 936ed4a6d7fSMel Gorman const char *buf, size_t count) 937ed4a6d7fSMel Gorman { 9388575ec29SHugh Dickins int nid = dev->id; 9398575ec29SHugh Dickins 9408575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 9418575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 9428575ec29SHugh Dickins lru_add_drain_all(); 9438575ec29SHugh Dickins 9448575ec29SHugh Dickins compact_node(nid); 9458575ec29SHugh Dickins } 946ed4a6d7fSMel Gorman 947ed4a6d7fSMel Gorman return count; 948ed4a6d7fSMel Gorman } 94910fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 950ed4a6d7fSMel Gorman 951ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 952ed4a6d7fSMel Gorman { 95310fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 954ed4a6d7fSMel Gorman } 955ed4a6d7fSMel Gorman 956ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 957ed4a6d7fSMel Gorman { 95810fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 959ed4a6d7fSMel Gorman } 960ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 961ff9543fdSMichal Nazarewicz 962ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 963