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 532a1402aaSMel Gorman static inline bool should_release_lock(spinlock_t *lock) 542a1402aaSMel Gorman { 552a1402aaSMel Gorman return need_resched() || spin_is_contended(lock); 562a1402aaSMel Gorman } 572a1402aaSMel Gorman 5885aa125fSMichal Nazarewicz /* 59c67fe375SMel Gorman * Compaction requires the taking of some coarse locks that are potentially 60c67fe375SMel Gorman * very heavily contended. Check if the process needs to be scheduled or 61c67fe375SMel Gorman * if the lock is contended. For async compaction, back out in the event 62c67fe375SMel Gorman * if contention is severe. For sync compaction, schedule. 63c67fe375SMel Gorman * 64c67fe375SMel Gorman * Returns true if the lock is held. 65c67fe375SMel Gorman * Returns false if the lock is released and compaction should abort 66c67fe375SMel Gorman */ 67c67fe375SMel Gorman static bool compact_checklock_irqsave(spinlock_t *lock, unsigned long *flags, 68c67fe375SMel Gorman bool locked, struct compact_control *cc) 69c67fe375SMel Gorman { 702a1402aaSMel Gorman if (should_release_lock(lock)) { 71c67fe375SMel Gorman if (locked) { 72c67fe375SMel Gorman spin_unlock_irqrestore(lock, *flags); 73c67fe375SMel Gorman locked = false; 74c67fe375SMel Gorman } 75c67fe375SMel Gorman 76c67fe375SMel Gorman /* async aborts if taking too long or contended */ 77c67fe375SMel Gorman if (!cc->sync) { 78e64c5237SShaohua Li cc->contended = true; 79c67fe375SMel Gorman return false; 80c67fe375SMel Gorman } 81c67fe375SMel Gorman 82c67fe375SMel Gorman cond_resched(); 83c67fe375SMel Gorman } 84c67fe375SMel Gorman 85c67fe375SMel Gorman if (!locked) 86c67fe375SMel Gorman spin_lock_irqsave(lock, *flags); 87c67fe375SMel Gorman return true; 88c67fe375SMel Gorman } 89c67fe375SMel Gorman 90c67fe375SMel Gorman static inline bool compact_trylock_irqsave(spinlock_t *lock, 91c67fe375SMel Gorman unsigned long *flags, struct compact_control *cc) 92c67fe375SMel Gorman { 93c67fe375SMel Gorman return compact_checklock_irqsave(lock, flags, false, cc); 94c67fe375SMel Gorman } 95c67fe375SMel Gorman 96f40d1e42SMel Gorman /* Returns true if the page is within a block suitable for migration to */ 97f40d1e42SMel Gorman static bool suitable_migration_target(struct page *page) 98f40d1e42SMel Gorman { 99f40d1e42SMel Gorman int migratetype = get_pageblock_migratetype(page); 100f40d1e42SMel Gorman 101f40d1e42SMel Gorman /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 102f40d1e42SMel Gorman if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 103f40d1e42SMel Gorman return false; 104f40d1e42SMel Gorman 105f40d1e42SMel Gorman /* If the page is a large free page, then allow migration */ 106f40d1e42SMel Gorman if (PageBuddy(page) && page_order(page) >= pageblock_order) 107f40d1e42SMel Gorman return true; 108f40d1e42SMel Gorman 109f40d1e42SMel Gorman /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ 110f40d1e42SMel Gorman if (migrate_async_suitable(migratetype)) 111f40d1e42SMel Gorman return true; 112f40d1e42SMel Gorman 113f40d1e42SMel Gorman /* Otherwise skip the block */ 114f40d1e42SMel Gorman return false; 115f40d1e42SMel Gorman } 116f40d1e42SMel Gorman 1171fb3f8caSMel Gorman static void compact_capture_page(struct compact_control *cc) 1181fb3f8caSMel Gorman { 1191fb3f8caSMel Gorman unsigned long flags; 1201fb3f8caSMel Gorman int mtype, mtype_low, mtype_high; 1211fb3f8caSMel Gorman 1221fb3f8caSMel Gorman if (!cc->page || *cc->page) 1231fb3f8caSMel Gorman return; 1241fb3f8caSMel Gorman 1251fb3f8caSMel Gorman /* 1261fb3f8caSMel Gorman * For MIGRATE_MOVABLE allocations we capture a suitable page ASAP 1271fb3f8caSMel Gorman * regardless of the migratetype of the freelist is is captured from. 1281fb3f8caSMel Gorman * This is fine because the order for a high-order MIGRATE_MOVABLE 1291fb3f8caSMel Gorman * allocation is typically at least a pageblock size and overall 1301fb3f8caSMel Gorman * fragmentation is not impaired. Other allocation types must 1311fb3f8caSMel Gorman * capture pages from their own migratelist because otherwise they 1321fb3f8caSMel Gorman * could pollute other pageblocks like MIGRATE_MOVABLE with 1331fb3f8caSMel Gorman * difficult to move pages and making fragmentation worse overall. 1341fb3f8caSMel Gorman */ 1351fb3f8caSMel Gorman if (cc->migratetype == MIGRATE_MOVABLE) { 1361fb3f8caSMel Gorman mtype_low = 0; 1371fb3f8caSMel Gorman mtype_high = MIGRATE_PCPTYPES; 1381fb3f8caSMel Gorman } else { 1391fb3f8caSMel Gorman mtype_low = cc->migratetype; 1401fb3f8caSMel Gorman mtype_high = cc->migratetype + 1; 1411fb3f8caSMel Gorman } 1421fb3f8caSMel Gorman 1431fb3f8caSMel Gorman /* Speculatively examine the free lists without zone lock */ 1441fb3f8caSMel Gorman for (mtype = mtype_low; mtype < mtype_high; mtype++) { 1451fb3f8caSMel Gorman int order; 1461fb3f8caSMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 1471fb3f8caSMel Gorman struct page *page; 1481fb3f8caSMel Gorman struct free_area *area; 1491fb3f8caSMel Gorman area = &(cc->zone->free_area[order]); 1501fb3f8caSMel Gorman if (list_empty(&area->free_list[mtype])) 1511fb3f8caSMel Gorman continue; 1521fb3f8caSMel Gorman 1531fb3f8caSMel Gorman /* Take the lock and attempt capture of the page */ 1541fb3f8caSMel Gorman if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc)) 1551fb3f8caSMel Gorman return; 1561fb3f8caSMel Gorman if (!list_empty(&area->free_list[mtype])) { 1571fb3f8caSMel Gorman page = list_entry(area->free_list[mtype].next, 1581fb3f8caSMel Gorman struct page, lru); 1591fb3f8caSMel Gorman if (capture_free_page(page, cc->order, mtype)) { 1601fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, 1611fb3f8caSMel Gorman flags); 1621fb3f8caSMel Gorman *cc->page = page; 1631fb3f8caSMel Gorman return; 1641fb3f8caSMel Gorman } 1651fb3f8caSMel Gorman } 1661fb3f8caSMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 1671fb3f8caSMel Gorman } 1681fb3f8caSMel Gorman } 1691fb3f8caSMel Gorman } 1701fb3f8caSMel Gorman 171c67fe375SMel Gorman /* 17285aa125fSMichal Nazarewicz * Isolate free pages onto a private freelist. Caller must hold zone->lock. 17385aa125fSMichal Nazarewicz * If @strict is true, will abort returning 0 on any invalid PFNs or non-free 17485aa125fSMichal Nazarewicz * pages inside of the pageblock (even though it may still end up isolating 17585aa125fSMichal Nazarewicz * some pages). 17685aa125fSMichal Nazarewicz */ 177f40d1e42SMel Gorman static unsigned long isolate_freepages_block(struct compact_control *cc, 178f40d1e42SMel Gorman unsigned long blockpfn, 17985aa125fSMichal Nazarewicz unsigned long end_pfn, 18085aa125fSMichal Nazarewicz struct list_head *freelist, 18185aa125fSMichal Nazarewicz bool strict) 182748446bbSMel Gorman { 183b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 184748446bbSMel Gorman struct page *cursor; 185f40d1e42SMel Gorman unsigned long nr_strict_required = end_pfn - blockpfn; 186f40d1e42SMel Gorman unsigned long flags; 187f40d1e42SMel Gorman bool locked = false; 188748446bbSMel Gorman 189748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 190748446bbSMel Gorman 191f40d1e42SMel Gorman /* Isolate free pages. */ 192748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 193748446bbSMel Gorman int isolated, i; 194748446bbSMel Gorman struct page *page = cursor; 195748446bbSMel Gorman 196b7aba698SMel Gorman nr_scanned++; 197f40d1e42SMel Gorman if (!pfn_valid_within(blockpfn)) 198748446bbSMel Gorman continue; 199f40d1e42SMel Gorman if (!PageBuddy(page)) 200f40d1e42SMel Gorman continue; 201f40d1e42SMel Gorman 202f40d1e42SMel Gorman /* 203f40d1e42SMel Gorman * The zone lock must be held to isolate freepages. 204f40d1e42SMel Gorman * Unfortunately this is a very coarse lock and can be 205f40d1e42SMel Gorman * heavily contended if there are parallel allocations 206f40d1e42SMel Gorman * or parallel compactions. For async compaction do not 207f40d1e42SMel Gorman * spin on the lock and we acquire the lock as late as 208f40d1e42SMel Gorman * possible. 209f40d1e42SMel Gorman */ 210f40d1e42SMel Gorman locked = compact_checklock_irqsave(&cc->zone->lock, &flags, 211f40d1e42SMel Gorman locked, cc); 212f40d1e42SMel Gorman if (!locked) 213f40d1e42SMel Gorman break; 214f40d1e42SMel Gorman 215f40d1e42SMel Gorman /* Recheck this is a suitable migration target under lock */ 216f40d1e42SMel Gorman if (!strict && !suitable_migration_target(page)) 217f40d1e42SMel Gorman break; 218f40d1e42SMel Gorman 219f40d1e42SMel Gorman /* Recheck this is a buddy page under lock */ 220f40d1e42SMel Gorman if (!PageBuddy(page)) 221f40d1e42SMel Gorman continue; 222748446bbSMel Gorman 223748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 224748446bbSMel Gorman isolated = split_free_page(page); 22585aa125fSMichal Nazarewicz if (!isolated && strict) 226f40d1e42SMel Gorman break; 227748446bbSMel Gorman total_isolated += isolated; 228748446bbSMel Gorman for (i = 0; i < isolated; i++) { 229748446bbSMel Gorman list_add(&page->lru, freelist); 230748446bbSMel Gorman page++; 231748446bbSMel Gorman } 232748446bbSMel Gorman 233748446bbSMel Gorman /* If a page was split, advance to the end of it */ 234748446bbSMel Gorman if (isolated) { 235748446bbSMel Gorman blockpfn += isolated - 1; 236748446bbSMel Gorman cursor += isolated - 1; 237748446bbSMel Gorman } 238748446bbSMel Gorman } 239748446bbSMel Gorman 240b7aba698SMel Gorman trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated); 241f40d1e42SMel Gorman 242f40d1e42SMel Gorman /* 243f40d1e42SMel Gorman * If strict isolation is requested by CMA then check that all the 244f40d1e42SMel Gorman * pages requested were isolated. If there were any failures, 0 is 245f40d1e42SMel Gorman * returned and CMA will fail. 246f40d1e42SMel Gorman */ 247f40d1e42SMel Gorman if (strict && nr_strict_required != total_isolated) 248f40d1e42SMel Gorman total_isolated = 0; 249f40d1e42SMel Gorman 250f40d1e42SMel Gorman if (locked) 251f40d1e42SMel Gorman spin_unlock_irqrestore(&cc->zone->lock, flags); 252f40d1e42SMel Gorman 253748446bbSMel Gorman return total_isolated; 254748446bbSMel Gorman } 255748446bbSMel Gorman 25685aa125fSMichal Nazarewicz /** 25785aa125fSMichal Nazarewicz * isolate_freepages_range() - isolate free pages. 25885aa125fSMichal Nazarewicz * @start_pfn: The first PFN to start isolating. 25985aa125fSMichal Nazarewicz * @end_pfn: The one-past-last PFN. 26085aa125fSMichal Nazarewicz * 26185aa125fSMichal Nazarewicz * Non-free pages, invalid PFNs, or zone boundaries within the 26285aa125fSMichal Nazarewicz * [start_pfn, end_pfn) range are considered errors, cause function to 26385aa125fSMichal Nazarewicz * undo its actions and return zero. 26485aa125fSMichal Nazarewicz * 26585aa125fSMichal Nazarewicz * Otherwise, function returns one-past-the-last PFN of isolated page 26685aa125fSMichal Nazarewicz * (which may be greater then end_pfn if end fell in a middle of 26785aa125fSMichal Nazarewicz * a free page). 26885aa125fSMichal Nazarewicz */ 269ff9543fdSMichal Nazarewicz unsigned long 27085aa125fSMichal Nazarewicz isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn) 27185aa125fSMichal Nazarewicz { 272f40d1e42SMel Gorman unsigned long isolated, pfn, block_end_pfn; 27385aa125fSMichal Nazarewicz struct zone *zone = NULL; 27485aa125fSMichal Nazarewicz LIST_HEAD(freelist); 27585aa125fSMichal Nazarewicz 276f40d1e42SMel Gorman /* cc needed for isolate_freepages_block to acquire zone->lock */ 277f40d1e42SMel Gorman struct compact_control cc = { 278f40d1e42SMel Gorman .sync = true, 279f40d1e42SMel Gorman }; 280f40d1e42SMel Gorman 28185aa125fSMichal Nazarewicz if (pfn_valid(start_pfn)) 282f40d1e42SMel Gorman cc.zone = zone = page_zone(pfn_to_page(start_pfn)); 28385aa125fSMichal Nazarewicz 28485aa125fSMichal Nazarewicz for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) { 28585aa125fSMichal Nazarewicz if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn))) 28685aa125fSMichal Nazarewicz break; 28785aa125fSMichal Nazarewicz 28885aa125fSMichal Nazarewicz /* 28985aa125fSMichal Nazarewicz * On subsequent iterations ALIGN() is actually not needed, 29085aa125fSMichal Nazarewicz * but we keep it that we not to complicate the code. 29185aa125fSMichal Nazarewicz */ 29285aa125fSMichal Nazarewicz block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 29385aa125fSMichal Nazarewicz block_end_pfn = min(block_end_pfn, end_pfn); 29485aa125fSMichal Nazarewicz 295f40d1e42SMel Gorman isolated = isolate_freepages_block(&cc, pfn, block_end_pfn, 29685aa125fSMichal Nazarewicz &freelist, true); 29785aa125fSMichal Nazarewicz 29885aa125fSMichal Nazarewicz /* 29985aa125fSMichal Nazarewicz * In strict mode, isolate_freepages_block() returns 0 if 30085aa125fSMichal Nazarewicz * there are any holes in the block (ie. invalid PFNs or 30185aa125fSMichal Nazarewicz * non-free pages). 30285aa125fSMichal Nazarewicz */ 30385aa125fSMichal Nazarewicz if (!isolated) 30485aa125fSMichal Nazarewicz break; 30585aa125fSMichal Nazarewicz 30685aa125fSMichal Nazarewicz /* 30785aa125fSMichal Nazarewicz * If we managed to isolate pages, it is always (1 << n) * 30885aa125fSMichal Nazarewicz * pageblock_nr_pages for some non-negative n. (Max order 30985aa125fSMichal Nazarewicz * page may span two pageblocks). 31085aa125fSMichal Nazarewicz */ 31185aa125fSMichal Nazarewicz } 31285aa125fSMichal Nazarewicz 31385aa125fSMichal Nazarewicz /* split_free_page does not map the pages */ 31485aa125fSMichal Nazarewicz map_pages(&freelist); 31585aa125fSMichal Nazarewicz 31685aa125fSMichal Nazarewicz if (pfn < end_pfn) { 31785aa125fSMichal Nazarewicz /* Loop terminated early, cleanup. */ 31885aa125fSMichal Nazarewicz release_freepages(&freelist); 31985aa125fSMichal Nazarewicz return 0; 32085aa125fSMichal Nazarewicz } 32185aa125fSMichal Nazarewicz 32285aa125fSMichal Nazarewicz /* We don't use freelists for anything. */ 32385aa125fSMichal Nazarewicz return pfn; 32485aa125fSMichal Nazarewicz } 32585aa125fSMichal Nazarewicz 326748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 327c67fe375SMel Gorman static void acct_isolated(struct zone *zone, bool locked, struct compact_control *cc) 328748446bbSMel Gorman { 329748446bbSMel Gorman struct page *page; 330b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 331748446bbSMel Gorman 332b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 333b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 334748446bbSMel Gorman 335c67fe375SMel Gorman /* If locked we can use the interrupt unsafe versions */ 336c67fe375SMel Gorman if (locked) { 337b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 338b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 339c67fe375SMel Gorman } else { 340c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 341c67fe375SMel Gorman mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 342c67fe375SMel Gorman } 343748446bbSMel Gorman } 344748446bbSMel Gorman 345748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 346748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 347748446bbSMel Gorman { 348bc693045SMinchan Kim unsigned long active, inactive, isolated; 349748446bbSMel Gorman 350748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 351748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 352bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 353bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 354748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 355748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 356748446bbSMel Gorman 357bc693045SMinchan Kim return isolated > (inactive + active) / 2; 358748446bbSMel Gorman } 359748446bbSMel Gorman 3602fe86e00SMichal Nazarewicz /** 3612fe86e00SMichal Nazarewicz * isolate_migratepages_range() - isolate all migrate-able pages in range. 3622fe86e00SMichal Nazarewicz * @zone: Zone pages are in. 3632fe86e00SMichal Nazarewicz * @cc: Compaction control structure. 3642fe86e00SMichal Nazarewicz * @low_pfn: The first PFN of the range. 3652fe86e00SMichal Nazarewicz * @end_pfn: The one-past-the-last PFN of the range. 3662fe86e00SMichal Nazarewicz * 3672fe86e00SMichal Nazarewicz * Isolate all pages that can be migrated from the range specified by 3682fe86e00SMichal Nazarewicz * [low_pfn, end_pfn). Returns zero if there is a fatal signal 3692fe86e00SMichal Nazarewicz * pending), otherwise PFN of the first page that was not scanned 3702fe86e00SMichal Nazarewicz * (which may be both less, equal to or more then end_pfn). 3712fe86e00SMichal Nazarewicz * 3722fe86e00SMichal Nazarewicz * Assumes that cc->migratepages is empty and cc->nr_migratepages is 3732fe86e00SMichal Nazarewicz * zero. 3742fe86e00SMichal Nazarewicz * 3752fe86e00SMichal Nazarewicz * Apart from cc->migratepages and cc->nr_migratetypes this function 3762fe86e00SMichal Nazarewicz * does not modify any cc's fields, in particular it does not modify 3772fe86e00SMichal Nazarewicz * (or read for that matter) cc->migrate_pfn. 378748446bbSMel Gorman */ 379ff9543fdSMichal Nazarewicz unsigned long 3802fe86e00SMichal Nazarewicz isolate_migratepages_range(struct zone *zone, struct compact_control *cc, 3812fe86e00SMichal Nazarewicz unsigned long low_pfn, unsigned long end_pfn) 382748446bbSMel Gorman { 3839927af74SMel Gorman unsigned long last_pageblock_nr = 0, pageblock_nr; 384b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 385748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 386f3fd4a61SKonstantin Khlebnikov isolate_mode_t mode = 0; 387fa9add64SHugh Dickins struct lruvec *lruvec; 388c67fe375SMel Gorman unsigned long flags; 3892a1402aaSMel Gorman bool locked = false; 390748446bbSMel Gorman 391748446bbSMel Gorman /* 392748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 393748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 394748446bbSMel Gorman * delay for some time until fewer pages are isolated 395748446bbSMel Gorman */ 396748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 397f9e35b3bSMel Gorman /* async migration should just abort */ 39868e3e926SLinus Torvalds if (!cc->sync) 3992fe86e00SMichal Nazarewicz return 0; 400f9e35b3bSMel Gorman 401748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 402748446bbSMel Gorman 403748446bbSMel Gorman if (fatal_signal_pending(current)) 4042fe86e00SMichal Nazarewicz return 0; 405748446bbSMel Gorman } 406748446bbSMel Gorman 407748446bbSMel Gorman /* Time to isolate some pages for migration */ 408b2eef8c0SAndrea Arcangeli cond_resched(); 409748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 410748446bbSMel Gorman struct page *page; 411b2eef8c0SAndrea Arcangeli 412b2eef8c0SAndrea Arcangeli /* give a chance to irqs before checking need_resched() */ 4132a1402aaSMel Gorman if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) { 4142a1402aaSMel Gorman if (should_release_lock(&zone->lru_lock)) { 415c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 416b2eef8c0SAndrea Arcangeli locked = false; 417b2eef8c0SAndrea Arcangeli } 4182a1402aaSMel Gorman } 419b2eef8c0SAndrea Arcangeli 4200bf380bcSMel Gorman /* 4210bf380bcSMel Gorman * migrate_pfn does not necessarily start aligned to a 4220bf380bcSMel Gorman * pageblock. Ensure that pfn_valid is called when moving 4230bf380bcSMel Gorman * into a new MAX_ORDER_NR_PAGES range in case of large 4240bf380bcSMel Gorman * memory holes within the zone 4250bf380bcSMel Gorman */ 4260bf380bcSMel Gorman if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { 4270bf380bcSMel Gorman if (!pfn_valid(low_pfn)) { 4280bf380bcSMel Gorman low_pfn += MAX_ORDER_NR_PAGES - 1; 4290bf380bcSMel Gorman continue; 4300bf380bcSMel Gorman } 4310bf380bcSMel Gorman } 4320bf380bcSMel Gorman 433748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 434748446bbSMel Gorman continue; 435b7aba698SMel Gorman nr_scanned++; 436748446bbSMel Gorman 437dc908600SMel Gorman /* 438dc908600SMel Gorman * Get the page and ensure the page is within the same zone. 439dc908600SMel Gorman * See the comment in isolate_freepages about overlapping 440dc908600SMel Gorman * nodes. It is deliberate that the new zone lock is not taken 441dc908600SMel Gorman * as memory compaction should not move pages between nodes. 442dc908600SMel Gorman */ 443748446bbSMel Gorman page = pfn_to_page(low_pfn); 444dc908600SMel Gorman if (page_zone(page) != zone) 445dc908600SMel Gorman continue; 446dc908600SMel Gorman 447dc908600SMel Gorman /* Skip if free */ 448748446bbSMel Gorman if (PageBuddy(page)) 449748446bbSMel Gorman continue; 450748446bbSMel Gorman 4519927af74SMel Gorman /* 4529927af74SMel Gorman * For async migration, also only scan in MOVABLE blocks. Async 4539927af74SMel Gorman * migration is optimistic to see if the minimum amount of work 4549927af74SMel Gorman * satisfies the allocation 4559927af74SMel Gorman */ 4569927af74SMel Gorman pageblock_nr = low_pfn >> pageblock_order; 45768e3e926SLinus Torvalds if (!cc->sync && last_pageblock_nr != pageblock_nr && 45847118af0SMichal Nazarewicz !migrate_async_suitable(get_pageblock_migratetype(page))) { 4592a1402aaSMel Gorman goto next_pageblock; 4609927af74SMel Gorman } 4619927af74SMel Gorman 4622a1402aaSMel Gorman /* Check may be lockless but that's ok as we recheck later */ 463bc835011SAndrea Arcangeli if (!PageLRU(page)) 464bc835011SAndrea Arcangeli continue; 465bc835011SAndrea Arcangeli 466bc835011SAndrea Arcangeli /* 4672a1402aaSMel Gorman * PageLRU is set. lru_lock normally excludes isolation 4682a1402aaSMel Gorman * splitting and collapsing (collapsing has already happened 4692a1402aaSMel Gorman * if PageLRU is set) but the lock is not necessarily taken 4702a1402aaSMel Gorman * here and it is wasteful to take it just to check transhuge. 4712a1402aaSMel Gorman * Check TransHuge without lock and skip the whole pageblock if 4722a1402aaSMel Gorman * it's either a transhuge or hugetlbfs page, as calling 4732a1402aaSMel Gorman * compound_order() without preventing THP from splitting the 4742a1402aaSMel Gorman * page underneath us may return surprising results. 475bc835011SAndrea Arcangeli */ 476bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 4772a1402aaSMel Gorman if (!locked) 4782a1402aaSMel Gorman goto next_pageblock; 4792a1402aaSMel Gorman low_pfn += (1 << compound_order(page)) - 1; 4802a1402aaSMel Gorman continue; 4812a1402aaSMel Gorman } 4822a1402aaSMel Gorman 4832a1402aaSMel Gorman /* Check if it is ok to still hold the lock */ 4842a1402aaSMel Gorman locked = compact_checklock_irqsave(&zone->lru_lock, &flags, 4852a1402aaSMel Gorman locked, cc); 4862a1402aaSMel Gorman if (!locked || fatal_signal_pending(current)) 4872a1402aaSMel Gorman break; 4882a1402aaSMel Gorman 4892a1402aaSMel Gorman /* Recheck PageLRU and PageTransHuge under lock */ 4902a1402aaSMel Gorman if (!PageLRU(page)) 4912a1402aaSMel Gorman continue; 4922a1402aaSMel Gorman if (PageTransHuge(page)) { 493bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 494bc835011SAndrea Arcangeli continue; 495bc835011SAndrea Arcangeli } 496bc835011SAndrea Arcangeli 49768e3e926SLinus Torvalds if (!cc->sync) 498c8244935SMel Gorman mode |= ISOLATE_ASYNC_MIGRATE; 499c8244935SMel Gorman 500fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 501fa9add64SHugh Dickins 502748446bbSMel Gorman /* Try isolate the page */ 503f3fd4a61SKonstantin Khlebnikov if (__isolate_lru_page(page, mode) != 0) 504748446bbSMel Gorman continue; 505748446bbSMel Gorman 506bc835011SAndrea Arcangeli VM_BUG_ON(PageTransCompound(page)); 507bc835011SAndrea Arcangeli 508748446bbSMel Gorman /* Successfully isolated */ 509fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_lru(page)); 510748446bbSMel Gorman list_add(&page->lru, migratelist); 511748446bbSMel Gorman cc->nr_migratepages++; 512b7aba698SMel Gorman nr_isolated++; 513748446bbSMel Gorman 514748446bbSMel Gorman /* Avoid isolating too much */ 51531b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 51631b8384aSHillf Danton ++low_pfn; 517748446bbSMel Gorman break; 518748446bbSMel Gorman } 5192a1402aaSMel Gorman 5202a1402aaSMel Gorman continue; 5212a1402aaSMel Gorman 5222a1402aaSMel Gorman next_pageblock: 5232a1402aaSMel Gorman low_pfn += pageblock_nr_pages; 5242a1402aaSMel Gorman low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 5252a1402aaSMel Gorman last_pageblock_nr = pageblock_nr; 52631b8384aSHillf Danton } 527748446bbSMel Gorman 528c67fe375SMel Gorman acct_isolated(zone, locked, cc); 529748446bbSMel Gorman 530c67fe375SMel Gorman if (locked) 531c67fe375SMel Gorman spin_unlock_irqrestore(&zone->lru_lock, flags); 532748446bbSMel Gorman 533b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 534b7aba698SMel Gorman 5352fe86e00SMichal Nazarewicz return low_pfn; 5362fe86e00SMichal Nazarewicz } 5372fe86e00SMichal Nazarewicz 538ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION || CONFIG_CMA */ 539ff9543fdSMichal Nazarewicz #ifdef CONFIG_COMPACTION 540ff9543fdSMichal Nazarewicz /* 541ff9543fdSMichal Nazarewicz * Based on information in the current compact_control, find blocks 542ff9543fdSMichal Nazarewicz * suitable for isolating free pages from and then isolate them. 543ff9543fdSMichal Nazarewicz */ 544ff9543fdSMichal Nazarewicz static void isolate_freepages(struct zone *zone, 545ff9543fdSMichal Nazarewicz struct compact_control *cc) 546ff9543fdSMichal Nazarewicz { 547ff9543fdSMichal Nazarewicz struct page *page; 548ff9543fdSMichal Nazarewicz unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn; 549ff9543fdSMichal Nazarewicz int nr_freepages = cc->nr_freepages; 550ff9543fdSMichal Nazarewicz struct list_head *freelist = &cc->freepages; 5512fe86e00SMichal Nazarewicz 552ff9543fdSMichal Nazarewicz /* 553ff9543fdSMichal Nazarewicz * Initialise the free scanner. The starting point is where we last 554ff9543fdSMichal Nazarewicz * scanned from (or the end of the zone if starting). The low point 555ff9543fdSMichal Nazarewicz * is the end of the pageblock the migration scanner is using. 556ff9543fdSMichal Nazarewicz */ 557ff9543fdSMichal Nazarewicz pfn = cc->free_pfn; 558ff9543fdSMichal Nazarewicz low_pfn = cc->migrate_pfn + pageblock_nr_pages; 5592fe86e00SMichal Nazarewicz 560ff9543fdSMichal Nazarewicz /* 561ff9543fdSMichal Nazarewicz * Take care that if the migration scanner is at the end of the zone 562ff9543fdSMichal Nazarewicz * that the free scanner does not accidentally move to the next zone 563ff9543fdSMichal Nazarewicz * in the next isolation cycle. 564ff9543fdSMichal Nazarewicz */ 565ff9543fdSMichal Nazarewicz high_pfn = min(low_pfn, pfn); 566ff9543fdSMichal Nazarewicz 567ff9543fdSMichal Nazarewicz zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 568ff9543fdSMichal Nazarewicz 569ff9543fdSMichal Nazarewicz /* 570ff9543fdSMichal Nazarewicz * Isolate free pages until enough are available to migrate the 571ff9543fdSMichal Nazarewicz * pages on cc->migratepages. We stop searching if the migrate 572ff9543fdSMichal Nazarewicz * and free page scanners meet or enough free pages are isolated. 573ff9543fdSMichal Nazarewicz */ 574ff9543fdSMichal Nazarewicz for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; 575ff9543fdSMichal Nazarewicz pfn -= pageblock_nr_pages) { 576ff9543fdSMichal Nazarewicz unsigned long isolated; 577ff9543fdSMichal Nazarewicz 578ff9543fdSMichal Nazarewicz if (!pfn_valid(pfn)) 579ff9543fdSMichal Nazarewicz continue; 580ff9543fdSMichal Nazarewicz 581ff9543fdSMichal Nazarewicz /* 582ff9543fdSMichal Nazarewicz * Check for overlapping nodes/zones. It's possible on some 583ff9543fdSMichal Nazarewicz * configurations to have a setup like 584ff9543fdSMichal Nazarewicz * node0 node1 node0 585ff9543fdSMichal Nazarewicz * i.e. it's possible that all pages within a zones range of 586ff9543fdSMichal Nazarewicz * pages do not belong to a single zone. 587ff9543fdSMichal Nazarewicz */ 588ff9543fdSMichal Nazarewicz page = pfn_to_page(pfn); 589ff9543fdSMichal Nazarewicz if (page_zone(page) != zone) 590ff9543fdSMichal Nazarewicz continue; 591ff9543fdSMichal Nazarewicz 592ff9543fdSMichal Nazarewicz /* Check the block is suitable for migration */ 59368e3e926SLinus Torvalds if (!suitable_migration_target(page)) 594ff9543fdSMichal Nazarewicz continue; 59568e3e926SLinus Torvalds 596f40d1e42SMel Gorman /* Found a block suitable for isolating free pages from */ 597ff9543fdSMichal Nazarewicz isolated = 0; 598ff9543fdSMichal Nazarewicz end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn); 599f40d1e42SMel Gorman isolated = isolate_freepages_block(cc, pfn, end_pfn, 600ff9543fdSMichal Nazarewicz freelist, false); 601ff9543fdSMichal Nazarewicz nr_freepages += isolated; 602ff9543fdSMichal Nazarewicz 603ff9543fdSMichal Nazarewicz /* 604ff9543fdSMichal Nazarewicz * Record the highest PFN we isolated pages from. When next 605ff9543fdSMichal Nazarewicz * looking for free pages, the search will restart here as 606ff9543fdSMichal Nazarewicz * page migration may have returned some pages to the allocator 607ff9543fdSMichal Nazarewicz */ 608*753341a4SMel Gorman if (isolated) 609ff9543fdSMichal Nazarewicz high_pfn = max(high_pfn, pfn); 610ff9543fdSMichal Nazarewicz } 611ff9543fdSMichal Nazarewicz 612ff9543fdSMichal Nazarewicz /* split_free_page does not map the pages */ 613ff9543fdSMichal Nazarewicz map_pages(freelist); 614ff9543fdSMichal Nazarewicz 615ff9543fdSMichal Nazarewicz cc->free_pfn = high_pfn; 616ff9543fdSMichal Nazarewicz cc->nr_freepages = nr_freepages; 617748446bbSMel Gorman } 618748446bbSMel Gorman 619748446bbSMel Gorman /* 620748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 621748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 622748446bbSMel Gorman */ 623748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 624748446bbSMel Gorman unsigned long data, 625748446bbSMel Gorman int **result) 626748446bbSMel Gorman { 627748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 628748446bbSMel Gorman struct page *freepage; 629748446bbSMel Gorman 630748446bbSMel Gorman /* Isolate free pages if necessary */ 631748446bbSMel Gorman if (list_empty(&cc->freepages)) { 632748446bbSMel Gorman isolate_freepages(cc->zone, cc); 633748446bbSMel Gorman 634748446bbSMel Gorman if (list_empty(&cc->freepages)) 635748446bbSMel Gorman return NULL; 636748446bbSMel Gorman } 637748446bbSMel Gorman 638748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 639748446bbSMel Gorman list_del(&freepage->lru); 640748446bbSMel Gorman cc->nr_freepages--; 641748446bbSMel Gorman 642748446bbSMel Gorman return freepage; 643748446bbSMel Gorman } 644748446bbSMel Gorman 645748446bbSMel Gorman /* 646748446bbSMel Gorman * We cannot control nr_migratepages and nr_freepages fully when migration is 647748446bbSMel Gorman * running as migrate_pages() has no knowledge of compact_control. When 648748446bbSMel Gorman * migration is complete, we count the number of pages on the lists by hand. 649748446bbSMel Gorman */ 650748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc) 651748446bbSMel Gorman { 652748446bbSMel Gorman int nr_migratepages = 0; 653748446bbSMel Gorman int nr_freepages = 0; 654748446bbSMel Gorman struct page *page; 655748446bbSMel Gorman 656748446bbSMel Gorman list_for_each_entry(page, &cc->migratepages, lru) 657748446bbSMel Gorman nr_migratepages++; 658748446bbSMel Gorman list_for_each_entry(page, &cc->freepages, lru) 659748446bbSMel Gorman nr_freepages++; 660748446bbSMel Gorman 661748446bbSMel Gorman cc->nr_migratepages = nr_migratepages; 662748446bbSMel Gorman cc->nr_freepages = nr_freepages; 663748446bbSMel Gorman } 664748446bbSMel Gorman 665ff9543fdSMichal Nazarewicz /* possible outcome of isolate_migratepages */ 666ff9543fdSMichal Nazarewicz typedef enum { 667ff9543fdSMichal Nazarewicz ISOLATE_ABORT, /* Abort compaction now */ 668ff9543fdSMichal Nazarewicz ISOLATE_NONE, /* No pages isolated, continue scanning */ 669ff9543fdSMichal Nazarewicz ISOLATE_SUCCESS, /* Pages isolated, migrate */ 670ff9543fdSMichal Nazarewicz } isolate_migrate_t; 671ff9543fdSMichal Nazarewicz 672ff9543fdSMichal Nazarewicz /* 673ff9543fdSMichal Nazarewicz * Isolate all pages that can be migrated from the block pointed to by 674ff9543fdSMichal Nazarewicz * the migrate scanner within compact_control. 675ff9543fdSMichal Nazarewicz */ 676ff9543fdSMichal Nazarewicz static isolate_migrate_t isolate_migratepages(struct zone *zone, 677ff9543fdSMichal Nazarewicz struct compact_control *cc) 678ff9543fdSMichal Nazarewicz { 679ff9543fdSMichal Nazarewicz unsigned long low_pfn, end_pfn; 680ff9543fdSMichal Nazarewicz 681ff9543fdSMichal Nazarewicz /* Do not scan outside zone boundaries */ 682ff9543fdSMichal Nazarewicz low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); 683ff9543fdSMichal Nazarewicz 684ff9543fdSMichal Nazarewicz /* Only scan within a pageblock boundary */ 685ff9543fdSMichal Nazarewicz end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); 686ff9543fdSMichal Nazarewicz 687ff9543fdSMichal Nazarewicz /* Do not cross the free scanner or scan within a memory hole */ 688ff9543fdSMichal Nazarewicz if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { 689ff9543fdSMichal Nazarewicz cc->migrate_pfn = end_pfn; 690ff9543fdSMichal Nazarewicz return ISOLATE_NONE; 691ff9543fdSMichal Nazarewicz } 692ff9543fdSMichal Nazarewicz 693ff9543fdSMichal Nazarewicz /* Perform the isolation */ 694ff9543fdSMichal Nazarewicz low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn); 695e64c5237SShaohua Li if (!low_pfn || cc->contended) 696ff9543fdSMichal Nazarewicz return ISOLATE_ABORT; 697ff9543fdSMichal Nazarewicz 698ff9543fdSMichal Nazarewicz cc->migrate_pfn = low_pfn; 699ff9543fdSMichal Nazarewicz 700ff9543fdSMichal Nazarewicz return ISOLATE_SUCCESS; 701ff9543fdSMichal Nazarewicz } 702ff9543fdSMichal Nazarewicz 703748446bbSMel Gorman static int compact_finished(struct zone *zone, 704748446bbSMel Gorman struct compact_control *cc) 705748446bbSMel Gorman { 7065a03b051SAndrea Arcangeli unsigned long watermark; 70756de7263SMel Gorman 708748446bbSMel Gorman if (fatal_signal_pending(current)) 709748446bbSMel Gorman return COMPACT_PARTIAL; 710748446bbSMel Gorman 711*753341a4SMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 712*753341a4SMel Gorman if (cc->free_pfn <= cc->migrate_pfn) 713748446bbSMel Gorman return COMPACT_COMPLETE; 714748446bbSMel Gorman 71582478fb7SJohannes Weiner /* 71682478fb7SJohannes Weiner * order == -1 is expected when compacting via 71782478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 71882478fb7SJohannes Weiner */ 71956de7263SMel Gorman if (cc->order == -1) 72056de7263SMel Gorman return COMPACT_CONTINUE; 72156de7263SMel Gorman 7223957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 7233957c776SMichal Hocko watermark = low_wmark_pages(zone); 7243957c776SMichal Hocko watermark += (1 << cc->order); 7253957c776SMichal Hocko 7263957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 7273957c776SMichal Hocko return COMPACT_CONTINUE; 7283957c776SMichal Hocko 72956de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 7301fb3f8caSMel Gorman if (cc->page) { 7311fb3f8caSMel Gorman /* Was a suitable page captured? */ 7321fb3f8caSMel Gorman if (*cc->page) 7331fb3f8caSMel Gorman return COMPACT_PARTIAL; 7341fb3f8caSMel Gorman } else { 7351fb3f8caSMel Gorman unsigned int order; 73656de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 7371fb3f8caSMel Gorman struct free_area *area = &zone->free_area[cc->order]; 73856de7263SMel Gorman /* Job done if page is free of the right migratetype */ 7391fb3f8caSMel Gorman if (!list_empty(&area->free_list[cc->migratetype])) 74056de7263SMel Gorman return COMPACT_PARTIAL; 74156de7263SMel Gorman 74256de7263SMel Gorman /* Job done if allocation would set block type */ 7431fb3f8caSMel Gorman if (cc->order >= pageblock_order && area->nr_free) 74456de7263SMel Gorman return COMPACT_PARTIAL; 74556de7263SMel Gorman } 7461fb3f8caSMel Gorman } 74756de7263SMel Gorman 748748446bbSMel Gorman return COMPACT_CONTINUE; 749748446bbSMel Gorman } 750748446bbSMel Gorman 7513e7d3449SMel Gorman /* 7523e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 7533e7d3449SMel Gorman * Returns 7543e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 7553e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 7563e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 7573e7d3449SMel Gorman */ 7583e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 7593e7d3449SMel Gorman { 7603e7d3449SMel Gorman int fragindex; 7613e7d3449SMel Gorman unsigned long watermark; 7623e7d3449SMel Gorman 7633e7d3449SMel Gorman /* 7643957c776SMichal Hocko * order == -1 is expected when compacting via 7653957c776SMichal Hocko * /proc/sys/vm/compact_memory 7663957c776SMichal Hocko */ 7673957c776SMichal Hocko if (order == -1) 7683957c776SMichal Hocko return COMPACT_CONTINUE; 7693957c776SMichal Hocko 7703957c776SMichal Hocko /* 7713e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 7723e7d3449SMel Gorman * This is because during migration, copies of pages need to be 7733e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 7743e7d3449SMel Gorman */ 7753e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 7763e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 7773e7d3449SMel Gorman return COMPACT_SKIPPED; 7783e7d3449SMel Gorman 7793e7d3449SMel Gorman /* 7803e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 7813e7d3449SMel Gorman * low memory or external fragmentation 7823e7d3449SMel Gorman * 783a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 784a582a738SShaohua Li * watermarks 7853e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 7863e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 7873e7d3449SMel Gorman * 7883e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 7893e7d3449SMel Gorman */ 7903e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 7913e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 7923e7d3449SMel Gorman return COMPACT_SKIPPED; 7933e7d3449SMel Gorman 794a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 795a582a738SShaohua Li 0, 0)) 7963e7d3449SMel Gorman return COMPACT_PARTIAL; 7973e7d3449SMel Gorman 7983e7d3449SMel Gorman return COMPACT_CONTINUE; 7993e7d3449SMel Gorman } 8003e7d3449SMel Gorman 801748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 802748446bbSMel Gorman { 803748446bbSMel Gorman int ret; 804748446bbSMel Gorman 8053e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 8063e7d3449SMel Gorman switch (ret) { 8073e7d3449SMel Gorman case COMPACT_PARTIAL: 8083e7d3449SMel Gorman case COMPACT_SKIPPED: 8093e7d3449SMel Gorman /* Compaction is likely to fail */ 8103e7d3449SMel Gorman return ret; 8113e7d3449SMel Gorman case COMPACT_CONTINUE: 8123e7d3449SMel Gorman /* Fall through to compaction */ 8133e7d3449SMel Gorman ; 8143e7d3449SMel Gorman } 8153e7d3449SMel Gorman 816748446bbSMel Gorman /* Setup to move all movable pages to the end of the zone */ 817748446bbSMel Gorman cc->migrate_pfn = zone->zone_start_pfn; 818*753341a4SMel Gorman cc->free_pfn = cc->migrate_pfn + zone->spanned_pages; 819*753341a4SMel Gorman cc->free_pfn &= ~(pageblock_nr_pages-1); 820748446bbSMel Gorman 821748446bbSMel Gorman migrate_prep_local(); 822748446bbSMel Gorman 823748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 824748446bbSMel Gorman unsigned long nr_migrate, nr_remaining; 8259d502c1cSMinchan Kim int err; 826748446bbSMel Gorman 827f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 828f9e35b3bSMel Gorman case ISOLATE_ABORT: 829f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 830e64c5237SShaohua Li putback_lru_pages(&cc->migratepages); 831e64c5237SShaohua Li cc->nr_migratepages = 0; 832f9e35b3bSMel Gorman goto out; 833f9e35b3bSMel Gorman case ISOLATE_NONE: 834748446bbSMel Gorman continue; 835f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 836f9e35b3bSMel Gorman ; 837f9e35b3bSMel Gorman } 838748446bbSMel Gorman 839748446bbSMel Gorman nr_migrate = cc->nr_migratepages; 8409d502c1cSMinchan Kim err = migrate_pages(&cc->migratepages, compaction_alloc, 84168e3e926SLinus Torvalds (unsigned long)cc, false, 84268e3e926SLinus Torvalds cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); 843748446bbSMel Gorman update_nr_listpages(cc); 844748446bbSMel Gorman nr_remaining = cc->nr_migratepages; 845748446bbSMel Gorman 846748446bbSMel Gorman count_vm_event(COMPACTBLOCKS); 847748446bbSMel Gorman count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); 848748446bbSMel Gorman if (nr_remaining) 849748446bbSMel Gorman count_vm_events(COMPACTPAGEFAILED, nr_remaining); 850b7aba698SMel Gorman trace_mm_compaction_migratepages(nr_migrate - nr_remaining, 851b7aba698SMel Gorman nr_remaining); 852748446bbSMel Gorman 853748446bbSMel Gorman /* Release LRU pages not migrated */ 8549d502c1cSMinchan Kim if (err) { 855748446bbSMel Gorman putback_lru_pages(&cc->migratepages); 856748446bbSMel Gorman cc->nr_migratepages = 0; 8574bf2bba3SDavid Rientjes if (err == -ENOMEM) { 8584bf2bba3SDavid Rientjes ret = COMPACT_PARTIAL; 8594bf2bba3SDavid Rientjes goto out; 860748446bbSMel Gorman } 8614bf2bba3SDavid Rientjes } 8621fb3f8caSMel Gorman 8631fb3f8caSMel Gorman /* Capture a page now if it is a suitable size */ 8641fb3f8caSMel Gorman compact_capture_page(cc); 865748446bbSMel Gorman } 866748446bbSMel Gorman 867f9e35b3bSMel Gorman out: 868748446bbSMel Gorman /* Release free pages and check accounting */ 869748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 870748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 871748446bbSMel Gorman 872748446bbSMel Gorman return ret; 873748446bbSMel Gorman } 87476ab0f53SMel Gorman 875d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone, 87677f1fe6bSMel Gorman int order, gfp_t gfp_mask, 8771fb3f8caSMel Gorman bool sync, bool *contended, 8781fb3f8caSMel Gorman struct page **page) 87956de7263SMel Gorman { 880e64c5237SShaohua Li unsigned long ret; 88156de7263SMel Gorman struct compact_control cc = { 88256de7263SMel Gorman .nr_freepages = 0, 88356de7263SMel Gorman .nr_migratepages = 0, 88456de7263SMel Gorman .order = order, 88556de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 88656de7263SMel Gorman .zone = zone, 88768e3e926SLinus Torvalds .sync = sync, 8881fb3f8caSMel Gorman .page = page, 88956de7263SMel Gorman }; 89056de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 89156de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 89256de7263SMel Gorman 893e64c5237SShaohua Li ret = compact_zone(zone, &cc); 894e64c5237SShaohua Li 895e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.freepages)); 896e64c5237SShaohua Li VM_BUG_ON(!list_empty(&cc.migratepages)); 897e64c5237SShaohua Li 898e64c5237SShaohua Li *contended = cc.contended; 899e64c5237SShaohua Li return ret; 90056de7263SMel Gorman } 90156de7263SMel Gorman 9025e771905SMel Gorman int sysctl_extfrag_threshold = 500; 9035e771905SMel Gorman 90456de7263SMel Gorman /** 90556de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 90656de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 90756de7263SMel Gorman * @order: The order of the current allocation 90856de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 90956de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 91077f1fe6bSMel Gorman * @sync: Whether migration is synchronous or not 911661c4cb9SMel Gorman * @contended: Return value that is true if compaction was aborted due to lock contention 912661c4cb9SMel Gorman * @page: Optionally capture a free page of the requested order during compaction 91356de7263SMel Gorman * 91456de7263SMel Gorman * This is the main entry point for direct page compaction. 91556de7263SMel Gorman */ 91656de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 91777f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 9181fb3f8caSMel Gorman bool sync, bool *contended, struct page **page) 91956de7263SMel Gorman { 92056de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 92156de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 92256de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 92356de7263SMel Gorman struct zoneref *z; 92456de7263SMel Gorman struct zone *zone; 92556de7263SMel Gorman int rc = COMPACT_SKIPPED; 926d95ea5d1SBartlomiej Zolnierkiewicz int alloc_flags = 0; 92756de7263SMel Gorman 9284ffb6335SMel Gorman /* Check if the GFP flags allow compaction */ 929c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 93056de7263SMel Gorman return rc; 93156de7263SMel Gorman 93256de7263SMel Gorman count_vm_event(COMPACTSTALL); 93356de7263SMel Gorman 934d95ea5d1SBartlomiej Zolnierkiewicz #ifdef CONFIG_CMA 935d95ea5d1SBartlomiej Zolnierkiewicz if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) 936d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags |= ALLOC_CMA; 937d95ea5d1SBartlomiej Zolnierkiewicz #endif 93856de7263SMel Gorman /* Compact each zone in the list */ 93956de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 94056de7263SMel Gorman nodemask) { 94156de7263SMel Gorman int status; 94256de7263SMel Gorman 943c67fe375SMel Gorman status = compact_zone_order(zone, order, gfp_mask, sync, 9441fb3f8caSMel Gorman contended, page); 94556de7263SMel Gorman rc = max(status, rc); 94656de7263SMel Gorman 9473e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 948d95ea5d1SBartlomiej Zolnierkiewicz if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 949d95ea5d1SBartlomiej Zolnierkiewicz alloc_flags)) 95056de7263SMel Gorman break; 95156de7263SMel Gorman } 95256de7263SMel Gorman 95356de7263SMel Gorman return rc; 95456de7263SMel Gorman } 95556de7263SMel Gorman 95656de7263SMel Gorman 95776ab0f53SMel Gorman /* Compact all zones within a node */ 9587be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 95976ab0f53SMel Gorman { 96076ab0f53SMel Gorman int zoneid; 96176ab0f53SMel Gorman struct zone *zone; 96276ab0f53SMel Gorman 96376ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 96476ab0f53SMel Gorman 96576ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 96676ab0f53SMel Gorman if (!populated_zone(zone)) 96776ab0f53SMel Gorman continue; 96876ab0f53SMel Gorman 9697be62de9SRik van Riel cc->nr_freepages = 0; 9707be62de9SRik van Riel cc->nr_migratepages = 0; 9717be62de9SRik van Riel cc->zone = zone; 9727be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 9737be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 97476ab0f53SMel Gorman 975aad6ec37SDan Carpenter if (cc->order == -1 || !compaction_deferred(zone, cc->order)) 9767be62de9SRik van Riel compact_zone(zone, cc); 97776ab0f53SMel Gorman 978aff62249SRik van Riel if (cc->order > 0) { 979aff62249SRik van Riel int ok = zone_watermark_ok(zone, cc->order, 980aff62249SRik van Riel low_wmark_pages(zone), 0, 0); 981c81758fbSMinchan Kim if (ok && cc->order >= zone->compact_order_failed) 982aff62249SRik van Riel zone->compact_order_failed = cc->order + 1; 983aff62249SRik van Riel /* Currently async compaction is never deferred. */ 98468e3e926SLinus Torvalds else if (!ok && cc->sync) 985aff62249SRik van Riel defer_compaction(zone, cc->order); 986aff62249SRik van Riel } 987aff62249SRik van Riel 9887be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 9897be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 99076ab0f53SMel Gorman } 99176ab0f53SMel Gorman 99276ab0f53SMel Gorman return 0; 99376ab0f53SMel Gorman } 99476ab0f53SMel Gorman 9957be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order) 9967be62de9SRik van Riel { 9977be62de9SRik van Riel struct compact_control cc = { 9987be62de9SRik van Riel .order = order, 99968e3e926SLinus Torvalds .sync = false, 10001fb3f8caSMel Gorman .page = NULL, 10017be62de9SRik van Riel }; 10027be62de9SRik van Riel 10037be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 10047be62de9SRik van Riel } 10057be62de9SRik van Riel 10067be62de9SRik van Riel static int compact_node(int nid) 10077be62de9SRik van Riel { 10087be62de9SRik van Riel struct compact_control cc = { 10097be62de9SRik van Riel .order = -1, 101068e3e926SLinus Torvalds .sync = true, 10111fb3f8caSMel Gorman .page = NULL, 10127be62de9SRik van Riel }; 10137be62de9SRik van Riel 10148575ec29SHugh Dickins return __compact_pgdat(NODE_DATA(nid), &cc); 10157be62de9SRik van Riel } 10167be62de9SRik van Riel 101776ab0f53SMel Gorman /* Compact all nodes in the system */ 101876ab0f53SMel Gorman static int compact_nodes(void) 101976ab0f53SMel Gorman { 102076ab0f53SMel Gorman int nid; 102176ab0f53SMel Gorman 10228575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 10238575ec29SHugh Dickins lru_add_drain_all(); 10248575ec29SHugh Dickins 102576ab0f53SMel Gorman for_each_online_node(nid) 102676ab0f53SMel Gorman compact_node(nid); 102776ab0f53SMel Gorman 102876ab0f53SMel Gorman return COMPACT_COMPLETE; 102976ab0f53SMel Gorman } 103076ab0f53SMel Gorman 103176ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 103276ab0f53SMel Gorman int sysctl_compact_memory; 103376ab0f53SMel Gorman 103476ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 103576ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 103676ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 103776ab0f53SMel Gorman { 103876ab0f53SMel Gorman if (write) 103976ab0f53SMel Gorman return compact_nodes(); 104076ab0f53SMel Gorman 104176ab0f53SMel Gorman return 0; 104276ab0f53SMel Gorman } 1043ed4a6d7fSMel Gorman 10445e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 10455e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 10465e771905SMel Gorman { 10475e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 10485e771905SMel Gorman 10495e771905SMel Gorman return 0; 10505e771905SMel Gorman } 10515e771905SMel Gorman 1052ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 105310fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev, 105410fbcf4cSKay Sievers struct device_attribute *attr, 1055ed4a6d7fSMel Gorman const char *buf, size_t count) 1056ed4a6d7fSMel Gorman { 10578575ec29SHugh Dickins int nid = dev->id; 10588575ec29SHugh Dickins 10598575ec29SHugh Dickins if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { 10608575ec29SHugh Dickins /* Flush pending updates to the LRU lists */ 10618575ec29SHugh Dickins lru_add_drain_all(); 10628575ec29SHugh Dickins 10638575ec29SHugh Dickins compact_node(nid); 10648575ec29SHugh Dickins } 1065ed4a6d7fSMel Gorman 1066ed4a6d7fSMel Gorman return count; 1067ed4a6d7fSMel Gorman } 106810fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 1069ed4a6d7fSMel Gorman 1070ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 1071ed4a6d7fSMel Gorman { 107210fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 1073ed4a6d7fSMel Gorman } 1074ed4a6d7fSMel Gorman 1075ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 1076ed4a6d7fSMel Gorman { 107710fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 1078ed4a6d7fSMel Gorman } 1079ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 1080ff9543fdSMichal Nazarewicz 1081ff9543fdSMichal Nazarewicz #endif /* CONFIG_COMPACTION */ 1082