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 19b7aba698SMel Gorman #define CREATE_TRACE_POINTS 20b7aba698SMel Gorman #include <trace/events/compaction.h> 21b7aba698SMel Gorman 22748446bbSMel Gorman /* 23748446bbSMel Gorman * compact_control is used to track pages being migrated and the free pages 24748446bbSMel Gorman * they are being migrated to during memory compaction. The free_pfn starts 25748446bbSMel Gorman * at the end of a zone and migrate_pfn begins at the start. Movable pages 26748446bbSMel Gorman * are moved to the end of a zone during a compaction run and the run 27748446bbSMel Gorman * completes when free_pfn <= migrate_pfn 28748446bbSMel Gorman */ 29748446bbSMel Gorman struct compact_control { 30748446bbSMel Gorman struct list_head freepages; /* List of free pages to migrate to */ 31748446bbSMel Gorman struct list_head migratepages; /* List of pages being migrated */ 32748446bbSMel Gorman unsigned long nr_freepages; /* Number of isolated free pages */ 33748446bbSMel Gorman unsigned long nr_migratepages; /* Number of pages to migrate */ 34748446bbSMel Gorman unsigned long free_pfn; /* isolate_freepages search base */ 35748446bbSMel Gorman unsigned long migrate_pfn; /* isolate_migratepages search base */ 3677f1fe6bSMel Gorman bool sync; /* Synchronous migration */ 37748446bbSMel Gorman 3856de7263SMel Gorman unsigned int order; /* order a direct compactor needs */ 3956de7263SMel Gorman int migratetype; /* MOVABLE, RECLAIMABLE etc */ 40748446bbSMel Gorman struct zone *zone; 41748446bbSMel Gorman }; 42748446bbSMel Gorman 43748446bbSMel Gorman static unsigned long release_freepages(struct list_head *freelist) 44748446bbSMel Gorman { 45748446bbSMel Gorman struct page *page, *next; 46748446bbSMel Gorman unsigned long count = 0; 47748446bbSMel Gorman 48748446bbSMel Gorman list_for_each_entry_safe(page, next, freelist, lru) { 49748446bbSMel Gorman list_del(&page->lru); 50748446bbSMel Gorman __free_page(page); 51748446bbSMel Gorman count++; 52748446bbSMel Gorman } 53748446bbSMel Gorman 54748446bbSMel Gorman return count; 55748446bbSMel Gorman } 56748446bbSMel Gorman 57748446bbSMel Gorman /* Isolate free pages onto a private freelist. Must hold zone->lock */ 58748446bbSMel Gorman static unsigned long isolate_freepages_block(struct zone *zone, 59748446bbSMel Gorman unsigned long blockpfn, 60748446bbSMel Gorman struct list_head *freelist) 61748446bbSMel Gorman { 62748446bbSMel Gorman unsigned long zone_end_pfn, end_pfn; 63b7aba698SMel Gorman int nr_scanned = 0, total_isolated = 0; 64748446bbSMel Gorman struct page *cursor; 65748446bbSMel Gorman 66748446bbSMel Gorman /* Get the last PFN we should scan for free pages at */ 67748446bbSMel Gorman zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; 68748446bbSMel Gorman end_pfn = min(blockpfn + pageblock_nr_pages, zone_end_pfn); 69748446bbSMel Gorman 70748446bbSMel Gorman /* Find the first usable PFN in the block to initialse page cursor */ 71748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++) { 72748446bbSMel Gorman if (pfn_valid_within(blockpfn)) 73748446bbSMel Gorman break; 74748446bbSMel Gorman } 75748446bbSMel Gorman cursor = pfn_to_page(blockpfn); 76748446bbSMel Gorman 77748446bbSMel Gorman /* Isolate free pages. This assumes the block is valid */ 78748446bbSMel Gorman for (; blockpfn < end_pfn; blockpfn++, cursor++) { 79748446bbSMel Gorman int isolated, i; 80748446bbSMel Gorman struct page *page = cursor; 81748446bbSMel Gorman 82748446bbSMel Gorman if (!pfn_valid_within(blockpfn)) 83748446bbSMel Gorman continue; 84b7aba698SMel Gorman nr_scanned++; 85748446bbSMel Gorman 86748446bbSMel Gorman if (!PageBuddy(page)) 87748446bbSMel Gorman continue; 88748446bbSMel Gorman 89748446bbSMel Gorman /* Found a free page, break it into order-0 pages */ 90748446bbSMel Gorman isolated = split_free_page(page); 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 108748446bbSMel Gorman /* Returns true if the page is within a block suitable for migration to */ 109748446bbSMel Gorman static bool suitable_migration_target(struct page *page) 110748446bbSMel Gorman { 111748446bbSMel Gorman 112748446bbSMel Gorman int migratetype = get_pageblock_migratetype(page); 113748446bbSMel Gorman 114748446bbSMel Gorman /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */ 115748446bbSMel Gorman if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE) 116748446bbSMel Gorman return false; 117748446bbSMel Gorman 118748446bbSMel Gorman /* If the page is a large free page, then allow migration */ 119748446bbSMel Gorman if (PageBuddy(page) && page_order(page) >= pageblock_order) 120748446bbSMel Gorman return true; 121748446bbSMel Gorman 122748446bbSMel Gorman /* If the block is MIGRATE_MOVABLE, allow migration */ 123748446bbSMel Gorman if (migratetype == MIGRATE_MOVABLE) 124748446bbSMel Gorman return true; 125748446bbSMel Gorman 126748446bbSMel Gorman /* Otherwise skip the block */ 127748446bbSMel Gorman return false; 128748446bbSMel Gorman } 129748446bbSMel Gorman 130748446bbSMel Gorman /* 131748446bbSMel Gorman * Based on information in the current compact_control, find blocks 132748446bbSMel Gorman * suitable for isolating free pages from and then isolate them. 133748446bbSMel Gorman */ 134748446bbSMel Gorman static void isolate_freepages(struct zone *zone, 135748446bbSMel Gorman struct compact_control *cc) 136748446bbSMel Gorman { 137748446bbSMel Gorman struct page *page; 138748446bbSMel Gorman unsigned long high_pfn, low_pfn, pfn; 139748446bbSMel Gorman unsigned long flags; 140748446bbSMel Gorman int nr_freepages = cc->nr_freepages; 141748446bbSMel Gorman struct list_head *freelist = &cc->freepages; 142748446bbSMel Gorman 1437454f4baSMel Gorman /* 1447454f4baSMel Gorman * Initialise the free scanner. The starting point is where we last 1457454f4baSMel Gorman * scanned from (or the end of the zone if starting). The low point 1467454f4baSMel Gorman * is the end of the pageblock the migration scanner is using. 1477454f4baSMel Gorman */ 148748446bbSMel Gorman pfn = cc->free_pfn; 149748446bbSMel Gorman low_pfn = cc->migrate_pfn + pageblock_nr_pages; 1507454f4baSMel Gorman 1517454f4baSMel Gorman /* 1527454f4baSMel Gorman * Take care that if the migration scanner is at the end of the zone 1537454f4baSMel Gorman * that the free scanner does not accidentally move to the next zone 1547454f4baSMel Gorman * in the next isolation cycle. 1557454f4baSMel Gorman */ 1567454f4baSMel Gorman high_pfn = min(low_pfn, pfn); 157748446bbSMel Gorman 158748446bbSMel Gorman /* 159748446bbSMel Gorman * Isolate free pages until enough are available to migrate the 160748446bbSMel Gorman * pages on cc->migratepages. We stop searching if the migrate 161748446bbSMel Gorman * and free page scanners meet or enough free pages are isolated. 162748446bbSMel Gorman */ 163748446bbSMel Gorman for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages; 164748446bbSMel Gorman pfn -= pageblock_nr_pages) { 165748446bbSMel Gorman unsigned long isolated; 166748446bbSMel Gorman 167748446bbSMel Gorman if (!pfn_valid(pfn)) 168748446bbSMel Gorman continue; 169748446bbSMel Gorman 170748446bbSMel Gorman /* 171748446bbSMel Gorman * Check for overlapping nodes/zones. It's possible on some 172748446bbSMel Gorman * configurations to have a setup like 173748446bbSMel Gorman * node0 node1 node0 174748446bbSMel Gorman * i.e. it's possible that all pages within a zones range of 175748446bbSMel Gorman * pages do not belong to a single zone. 176748446bbSMel Gorman */ 177748446bbSMel Gorman page = pfn_to_page(pfn); 178748446bbSMel Gorman if (page_zone(page) != zone) 179748446bbSMel Gorman continue; 180748446bbSMel Gorman 181748446bbSMel Gorman /* Check the block is suitable for migration */ 182748446bbSMel Gorman if (!suitable_migration_target(page)) 183748446bbSMel Gorman continue; 184748446bbSMel Gorman 185602605a4SMel Gorman /* 186602605a4SMel Gorman * Found a block suitable for isolating free pages from. Now 187602605a4SMel Gorman * we disabled interrupts, double check things are ok and 188602605a4SMel Gorman * isolate the pages. This is to minimise the time IRQs 189602605a4SMel Gorman * are disabled 190602605a4SMel Gorman */ 191602605a4SMel Gorman isolated = 0; 192602605a4SMel Gorman spin_lock_irqsave(&zone->lock, flags); 193602605a4SMel Gorman if (suitable_migration_target(page)) { 194748446bbSMel Gorman isolated = isolate_freepages_block(zone, pfn, freelist); 195748446bbSMel Gorman nr_freepages += isolated; 196602605a4SMel Gorman } 197602605a4SMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 198748446bbSMel Gorman 199748446bbSMel Gorman /* 200748446bbSMel Gorman * Record the highest PFN we isolated pages from. When next 201748446bbSMel Gorman * looking for free pages, the search will restart here as 202748446bbSMel Gorman * page migration may have returned some pages to the allocator 203748446bbSMel Gorman */ 204748446bbSMel Gorman if (isolated) 205748446bbSMel Gorman high_pfn = max(high_pfn, pfn); 206748446bbSMel Gorman } 207748446bbSMel Gorman 208748446bbSMel Gorman /* split_free_page does not map the pages */ 209748446bbSMel Gorman list_for_each_entry(page, freelist, lru) { 210748446bbSMel Gorman arch_alloc_page(page, 0); 211748446bbSMel Gorman kernel_map_pages(page, 1, 1); 212748446bbSMel Gorman } 213748446bbSMel Gorman 214748446bbSMel Gorman cc->free_pfn = high_pfn; 215748446bbSMel Gorman cc->nr_freepages = nr_freepages; 216748446bbSMel Gorman } 217748446bbSMel Gorman 218748446bbSMel Gorman /* Update the number of anon and file isolated pages in the zone */ 219748446bbSMel Gorman static void acct_isolated(struct zone *zone, struct compact_control *cc) 220748446bbSMel Gorman { 221748446bbSMel Gorman struct page *page; 222b9e84ac1SMinchan Kim unsigned int count[2] = { 0, }; 223748446bbSMel Gorman 224b9e84ac1SMinchan Kim list_for_each_entry(page, &cc->migratepages, lru) 225b9e84ac1SMinchan Kim count[!!page_is_file_cache(page)]++; 226748446bbSMel Gorman 227b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]); 228b9e84ac1SMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]); 229748446bbSMel Gorman } 230748446bbSMel Gorman 231748446bbSMel Gorman /* Similar to reclaim, but different enough that they don't share logic */ 232748446bbSMel Gorman static bool too_many_isolated(struct zone *zone) 233748446bbSMel Gorman { 234bc693045SMinchan Kim unsigned long active, inactive, isolated; 235748446bbSMel Gorman 236748446bbSMel Gorman inactive = zone_page_state(zone, NR_INACTIVE_FILE) + 237748446bbSMel Gorman zone_page_state(zone, NR_INACTIVE_ANON); 238bc693045SMinchan Kim active = zone_page_state(zone, NR_ACTIVE_FILE) + 239bc693045SMinchan Kim zone_page_state(zone, NR_ACTIVE_ANON); 240748446bbSMel Gorman isolated = zone_page_state(zone, NR_ISOLATED_FILE) + 241748446bbSMel Gorman zone_page_state(zone, NR_ISOLATED_ANON); 242748446bbSMel Gorman 243bc693045SMinchan Kim return isolated > (inactive + active) / 2; 244748446bbSMel Gorman } 245748446bbSMel Gorman 246f9e35b3bSMel Gorman /* possible outcome of isolate_migratepages */ 247f9e35b3bSMel Gorman typedef enum { 248f9e35b3bSMel Gorman ISOLATE_ABORT, /* Abort compaction now */ 249f9e35b3bSMel Gorman ISOLATE_NONE, /* No pages isolated, continue scanning */ 250f9e35b3bSMel Gorman ISOLATE_SUCCESS, /* Pages isolated, migrate */ 251f9e35b3bSMel Gorman } isolate_migrate_t; 252f9e35b3bSMel Gorman 253748446bbSMel Gorman /* 254748446bbSMel Gorman * Isolate all pages that can be migrated from the block pointed to by 255748446bbSMel Gorman * the migrate scanner within compact_control. 256748446bbSMel Gorman */ 257f9e35b3bSMel Gorman static isolate_migrate_t isolate_migratepages(struct zone *zone, 258748446bbSMel Gorman struct compact_control *cc) 259748446bbSMel Gorman { 260748446bbSMel Gorman unsigned long low_pfn, end_pfn; 2619927af74SMel Gorman unsigned long last_pageblock_nr = 0, pageblock_nr; 262b7aba698SMel Gorman unsigned long nr_scanned = 0, nr_isolated = 0; 263748446bbSMel Gorman struct list_head *migratelist = &cc->migratepages; 26439deaf85SMinchan Kim isolate_mode_t mode = ISOLATE_ACTIVE|ISOLATE_INACTIVE; 265748446bbSMel Gorman 266748446bbSMel Gorman /* Do not scan outside zone boundaries */ 267748446bbSMel Gorman low_pfn = max(cc->migrate_pfn, zone->zone_start_pfn); 268748446bbSMel Gorman 269748446bbSMel Gorman /* Only scan within a pageblock boundary */ 270748446bbSMel Gorman end_pfn = ALIGN(low_pfn + pageblock_nr_pages, pageblock_nr_pages); 271748446bbSMel Gorman 272748446bbSMel Gorman /* Do not cross the free scanner or scan within a memory hole */ 273748446bbSMel Gorman if (end_pfn > cc->free_pfn || !pfn_valid(low_pfn)) { 274748446bbSMel Gorman cc->migrate_pfn = end_pfn; 275f9e35b3bSMel Gorman return ISOLATE_NONE; 276748446bbSMel Gorman } 277748446bbSMel Gorman 278748446bbSMel Gorman /* 279748446bbSMel Gorman * Ensure that there are not too many pages isolated from the LRU 280748446bbSMel Gorman * list by either parallel reclaimers or compaction. If there are, 281748446bbSMel Gorman * delay for some time until fewer pages are isolated 282748446bbSMel Gorman */ 283748446bbSMel Gorman while (unlikely(too_many_isolated(zone))) { 284f9e35b3bSMel Gorman /* async migration should just abort */ 285f9e35b3bSMel Gorman if (!cc->sync) 286f9e35b3bSMel Gorman return ISOLATE_ABORT; 287f9e35b3bSMel Gorman 288748446bbSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 289748446bbSMel Gorman 290748446bbSMel Gorman if (fatal_signal_pending(current)) 291f9e35b3bSMel Gorman return ISOLATE_ABORT; 292748446bbSMel Gorman } 293748446bbSMel Gorman 294748446bbSMel Gorman /* Time to isolate some pages for migration */ 295b2eef8c0SAndrea Arcangeli cond_resched(); 296748446bbSMel Gorman spin_lock_irq(&zone->lru_lock); 297748446bbSMel Gorman for (; low_pfn < end_pfn; low_pfn++) { 298748446bbSMel Gorman struct page *page; 299b2eef8c0SAndrea Arcangeli bool locked = true; 300b2eef8c0SAndrea Arcangeli 301b2eef8c0SAndrea Arcangeli /* give a chance to irqs before checking need_resched() */ 302b2eef8c0SAndrea Arcangeli if (!((low_pfn+1) % SWAP_CLUSTER_MAX)) { 303b2eef8c0SAndrea Arcangeli spin_unlock_irq(&zone->lru_lock); 304b2eef8c0SAndrea Arcangeli locked = false; 305b2eef8c0SAndrea Arcangeli } 306b2eef8c0SAndrea Arcangeli if (need_resched() || spin_is_contended(&zone->lru_lock)) { 307b2eef8c0SAndrea Arcangeli if (locked) 308b2eef8c0SAndrea Arcangeli spin_unlock_irq(&zone->lru_lock); 309b2eef8c0SAndrea Arcangeli cond_resched(); 310b2eef8c0SAndrea Arcangeli spin_lock_irq(&zone->lru_lock); 311b2eef8c0SAndrea Arcangeli if (fatal_signal_pending(current)) 312b2eef8c0SAndrea Arcangeli break; 313b2eef8c0SAndrea Arcangeli } else if (!locked) 314b2eef8c0SAndrea Arcangeli spin_lock_irq(&zone->lru_lock); 315b2eef8c0SAndrea Arcangeli 3160bf380bcSMel Gorman /* 3170bf380bcSMel Gorman * migrate_pfn does not necessarily start aligned to a 3180bf380bcSMel Gorman * pageblock. Ensure that pfn_valid is called when moving 3190bf380bcSMel Gorman * into a new MAX_ORDER_NR_PAGES range in case of large 3200bf380bcSMel Gorman * memory holes within the zone 3210bf380bcSMel Gorman */ 3220bf380bcSMel Gorman if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) { 3230bf380bcSMel Gorman if (!pfn_valid(low_pfn)) { 3240bf380bcSMel Gorman low_pfn += MAX_ORDER_NR_PAGES - 1; 3250bf380bcSMel Gorman continue; 3260bf380bcSMel Gorman } 3270bf380bcSMel Gorman } 3280bf380bcSMel Gorman 329748446bbSMel Gorman if (!pfn_valid_within(low_pfn)) 330748446bbSMel Gorman continue; 331b7aba698SMel Gorman nr_scanned++; 332748446bbSMel Gorman 333dc908600SMel Gorman /* 334dc908600SMel Gorman * Get the page and ensure the page is within the same zone. 335dc908600SMel Gorman * See the comment in isolate_freepages about overlapping 336dc908600SMel Gorman * nodes. It is deliberate that the new zone lock is not taken 337dc908600SMel Gorman * as memory compaction should not move pages between nodes. 338dc908600SMel Gorman */ 339748446bbSMel Gorman page = pfn_to_page(low_pfn); 340dc908600SMel Gorman if (page_zone(page) != zone) 341dc908600SMel Gorman continue; 342dc908600SMel Gorman 343dc908600SMel Gorman /* Skip if free */ 344748446bbSMel Gorman if (PageBuddy(page)) 345748446bbSMel Gorman continue; 346748446bbSMel Gorman 3479927af74SMel Gorman /* 3489927af74SMel Gorman * For async migration, also only scan in MOVABLE blocks. Async 3499927af74SMel Gorman * migration is optimistic to see if the minimum amount of work 3509927af74SMel Gorman * satisfies the allocation 3519927af74SMel Gorman */ 3529927af74SMel Gorman pageblock_nr = low_pfn >> pageblock_order; 3539927af74SMel Gorman if (!cc->sync && last_pageblock_nr != pageblock_nr && 3549927af74SMel Gorman get_pageblock_migratetype(page) != MIGRATE_MOVABLE) { 3559927af74SMel Gorman low_pfn += pageblock_nr_pages; 3569927af74SMel Gorman low_pfn = ALIGN(low_pfn, pageblock_nr_pages) - 1; 3579927af74SMel Gorman last_pageblock_nr = pageblock_nr; 3589927af74SMel Gorman continue; 3599927af74SMel Gorman } 3609927af74SMel Gorman 361bc835011SAndrea Arcangeli if (!PageLRU(page)) 362bc835011SAndrea Arcangeli continue; 363bc835011SAndrea Arcangeli 364bc835011SAndrea Arcangeli /* 365bc835011SAndrea Arcangeli * PageLRU is set, and lru_lock excludes isolation, 366bc835011SAndrea Arcangeli * splitting and collapsing (collapsing has already 367bc835011SAndrea Arcangeli * happened if PageLRU is set). 368bc835011SAndrea Arcangeli */ 369bc835011SAndrea Arcangeli if (PageTransHuge(page)) { 370bc835011SAndrea Arcangeli low_pfn += (1 << compound_order(page)) - 1; 371bc835011SAndrea Arcangeli continue; 372bc835011SAndrea Arcangeli } 373bc835011SAndrea Arcangeli 374c8244935SMel Gorman if (!cc->sync) 375c8244935SMel Gorman mode |= ISOLATE_ASYNC_MIGRATE; 376c8244935SMel Gorman 377748446bbSMel Gorman /* Try isolate the page */ 37839deaf85SMinchan Kim if (__isolate_lru_page(page, mode, 0) != 0) 379748446bbSMel Gorman continue; 380748446bbSMel Gorman 381bc835011SAndrea Arcangeli VM_BUG_ON(PageTransCompound(page)); 382bc835011SAndrea Arcangeli 383748446bbSMel Gorman /* Successfully isolated */ 384748446bbSMel Gorman del_page_from_lru_list(zone, page, page_lru(page)); 385748446bbSMel Gorman list_add(&page->lru, migratelist); 386748446bbSMel Gorman cc->nr_migratepages++; 387b7aba698SMel Gorman nr_isolated++; 388748446bbSMel Gorman 389748446bbSMel Gorman /* Avoid isolating too much */ 39031b8384aSHillf Danton if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { 39131b8384aSHillf Danton ++low_pfn; 392748446bbSMel Gorman break; 393748446bbSMel Gorman } 39431b8384aSHillf Danton } 395748446bbSMel Gorman 396748446bbSMel Gorman acct_isolated(zone, cc); 397748446bbSMel Gorman 398748446bbSMel Gorman spin_unlock_irq(&zone->lru_lock); 399748446bbSMel Gorman cc->migrate_pfn = low_pfn; 400748446bbSMel Gorman 401b7aba698SMel Gorman trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated); 402b7aba698SMel Gorman 403f9e35b3bSMel Gorman return ISOLATE_SUCCESS; 404748446bbSMel Gorman } 405748446bbSMel Gorman 406748446bbSMel Gorman /* 407748446bbSMel Gorman * This is a migrate-callback that "allocates" freepages by taking pages 408748446bbSMel Gorman * from the isolated freelists in the block we are migrating to. 409748446bbSMel Gorman */ 410748446bbSMel Gorman static struct page *compaction_alloc(struct page *migratepage, 411748446bbSMel Gorman unsigned long data, 412748446bbSMel Gorman int **result) 413748446bbSMel Gorman { 414748446bbSMel Gorman struct compact_control *cc = (struct compact_control *)data; 415748446bbSMel Gorman struct page *freepage; 416748446bbSMel Gorman 417748446bbSMel Gorman /* Isolate free pages if necessary */ 418748446bbSMel Gorman if (list_empty(&cc->freepages)) { 419748446bbSMel Gorman isolate_freepages(cc->zone, cc); 420748446bbSMel Gorman 421748446bbSMel Gorman if (list_empty(&cc->freepages)) 422748446bbSMel Gorman return NULL; 423748446bbSMel Gorman } 424748446bbSMel Gorman 425748446bbSMel Gorman freepage = list_entry(cc->freepages.next, struct page, lru); 426748446bbSMel Gorman list_del(&freepage->lru); 427748446bbSMel Gorman cc->nr_freepages--; 428748446bbSMel Gorman 429748446bbSMel Gorman return freepage; 430748446bbSMel Gorman } 431748446bbSMel Gorman 432748446bbSMel Gorman /* 433748446bbSMel Gorman * We cannot control nr_migratepages and nr_freepages fully when migration is 434748446bbSMel Gorman * running as migrate_pages() has no knowledge of compact_control. When 435748446bbSMel Gorman * migration is complete, we count the number of pages on the lists by hand. 436748446bbSMel Gorman */ 437748446bbSMel Gorman static void update_nr_listpages(struct compact_control *cc) 438748446bbSMel Gorman { 439748446bbSMel Gorman int nr_migratepages = 0; 440748446bbSMel Gorman int nr_freepages = 0; 441748446bbSMel Gorman struct page *page; 442748446bbSMel Gorman 443748446bbSMel Gorman list_for_each_entry(page, &cc->migratepages, lru) 444748446bbSMel Gorman nr_migratepages++; 445748446bbSMel Gorman list_for_each_entry(page, &cc->freepages, lru) 446748446bbSMel Gorman nr_freepages++; 447748446bbSMel Gorman 448748446bbSMel Gorman cc->nr_migratepages = nr_migratepages; 449748446bbSMel Gorman cc->nr_freepages = nr_freepages; 450748446bbSMel Gorman } 451748446bbSMel Gorman 452748446bbSMel Gorman static int compact_finished(struct zone *zone, 453748446bbSMel Gorman struct compact_control *cc) 454748446bbSMel Gorman { 45556de7263SMel Gorman unsigned int order; 4565a03b051SAndrea Arcangeli unsigned long watermark; 45756de7263SMel Gorman 458748446bbSMel Gorman if (fatal_signal_pending(current)) 459748446bbSMel Gorman return COMPACT_PARTIAL; 460748446bbSMel Gorman 461748446bbSMel Gorman /* Compaction run completes if the migrate and free scanner meet */ 462748446bbSMel Gorman if (cc->free_pfn <= cc->migrate_pfn) 463748446bbSMel Gorman return COMPACT_COMPLETE; 464748446bbSMel Gorman 46582478fb7SJohannes Weiner /* 46682478fb7SJohannes Weiner * order == -1 is expected when compacting via 46782478fb7SJohannes Weiner * /proc/sys/vm/compact_memory 46882478fb7SJohannes Weiner */ 46956de7263SMel Gorman if (cc->order == -1) 47056de7263SMel Gorman return COMPACT_CONTINUE; 47156de7263SMel Gorman 4723957c776SMichal Hocko /* Compaction run is not finished if the watermark is not met */ 4733957c776SMichal Hocko watermark = low_wmark_pages(zone); 4743957c776SMichal Hocko watermark += (1 << cc->order); 4753957c776SMichal Hocko 4763957c776SMichal Hocko if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0)) 4773957c776SMichal Hocko return COMPACT_CONTINUE; 4783957c776SMichal Hocko 47956de7263SMel Gorman /* Direct compactor: Is a suitable page free? */ 48056de7263SMel Gorman for (order = cc->order; order < MAX_ORDER; order++) { 48156de7263SMel Gorman /* Job done if page is free of the right migratetype */ 48256de7263SMel Gorman if (!list_empty(&zone->free_area[order].free_list[cc->migratetype])) 48356de7263SMel Gorman return COMPACT_PARTIAL; 48456de7263SMel Gorman 48556de7263SMel Gorman /* Job done if allocation would set block type */ 48656de7263SMel Gorman if (order >= pageblock_order && zone->free_area[order].nr_free) 48756de7263SMel Gorman return COMPACT_PARTIAL; 48856de7263SMel Gorman } 48956de7263SMel Gorman 490748446bbSMel Gorman return COMPACT_CONTINUE; 491748446bbSMel Gorman } 492748446bbSMel Gorman 4933e7d3449SMel Gorman /* 4943e7d3449SMel Gorman * compaction_suitable: Is this suitable to run compaction on this zone now? 4953e7d3449SMel Gorman * Returns 4963e7d3449SMel Gorman * COMPACT_SKIPPED - If there are too few free pages for compaction 4973e7d3449SMel Gorman * COMPACT_PARTIAL - If the allocation would succeed without compaction 4983e7d3449SMel Gorman * COMPACT_CONTINUE - If compaction should run now 4993e7d3449SMel Gorman */ 5003e7d3449SMel Gorman unsigned long compaction_suitable(struct zone *zone, int order) 5013e7d3449SMel Gorman { 5023e7d3449SMel Gorman int fragindex; 5033e7d3449SMel Gorman unsigned long watermark; 5043e7d3449SMel Gorman 5053e7d3449SMel Gorman /* 5063957c776SMichal Hocko * order == -1 is expected when compacting via 5073957c776SMichal Hocko * /proc/sys/vm/compact_memory 5083957c776SMichal Hocko */ 5093957c776SMichal Hocko if (order == -1) 5103957c776SMichal Hocko return COMPACT_CONTINUE; 5113957c776SMichal Hocko 5123957c776SMichal Hocko /* 5133e7d3449SMel Gorman * Watermarks for order-0 must be met for compaction. Note the 2UL. 5143e7d3449SMel Gorman * This is because during migration, copies of pages need to be 5153e7d3449SMel Gorman * allocated and for a short time, the footprint is higher 5163e7d3449SMel Gorman */ 5173e7d3449SMel Gorman watermark = low_wmark_pages(zone) + (2UL << order); 5183e7d3449SMel Gorman if (!zone_watermark_ok(zone, 0, watermark, 0, 0)) 5193e7d3449SMel Gorman return COMPACT_SKIPPED; 5203e7d3449SMel Gorman 5213e7d3449SMel Gorman /* 5223e7d3449SMel Gorman * fragmentation index determines if allocation failures are due to 5233e7d3449SMel Gorman * low memory or external fragmentation 5243e7d3449SMel Gorman * 525a582a738SShaohua Li * index of -1000 implies allocations might succeed depending on 526a582a738SShaohua Li * watermarks 5273e7d3449SMel Gorman * index towards 0 implies failure is due to lack of memory 5283e7d3449SMel Gorman * index towards 1000 implies failure is due to fragmentation 5293e7d3449SMel Gorman * 5303e7d3449SMel Gorman * Only compact if a failure would be due to fragmentation. 5313e7d3449SMel Gorman */ 5323e7d3449SMel Gorman fragindex = fragmentation_index(zone, order); 5333e7d3449SMel Gorman if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold) 5343e7d3449SMel Gorman return COMPACT_SKIPPED; 5353e7d3449SMel Gorman 536a582a738SShaohua Li if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark, 537a582a738SShaohua Li 0, 0)) 5383e7d3449SMel Gorman return COMPACT_PARTIAL; 5393e7d3449SMel Gorman 5403e7d3449SMel Gorman return COMPACT_CONTINUE; 5413e7d3449SMel Gorman } 5423e7d3449SMel Gorman 543748446bbSMel Gorman static int compact_zone(struct zone *zone, struct compact_control *cc) 544748446bbSMel Gorman { 545748446bbSMel Gorman int ret; 546748446bbSMel Gorman 5473e7d3449SMel Gorman ret = compaction_suitable(zone, cc->order); 5483e7d3449SMel Gorman switch (ret) { 5493e7d3449SMel Gorman case COMPACT_PARTIAL: 5503e7d3449SMel Gorman case COMPACT_SKIPPED: 5513e7d3449SMel Gorman /* Compaction is likely to fail */ 5523e7d3449SMel Gorman return ret; 5533e7d3449SMel Gorman case COMPACT_CONTINUE: 5543e7d3449SMel Gorman /* Fall through to compaction */ 5553e7d3449SMel Gorman ; 5563e7d3449SMel Gorman } 5573e7d3449SMel Gorman 558748446bbSMel Gorman /* Setup to move all movable pages to the end of the zone */ 559748446bbSMel Gorman cc->migrate_pfn = zone->zone_start_pfn; 560748446bbSMel Gorman cc->free_pfn = cc->migrate_pfn + zone->spanned_pages; 561748446bbSMel Gorman cc->free_pfn &= ~(pageblock_nr_pages-1); 562748446bbSMel Gorman 563748446bbSMel Gorman migrate_prep_local(); 564748446bbSMel Gorman 565748446bbSMel Gorman while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { 566748446bbSMel Gorman unsigned long nr_migrate, nr_remaining; 5679d502c1cSMinchan Kim int err; 568748446bbSMel Gorman 569f9e35b3bSMel Gorman switch (isolate_migratepages(zone, cc)) { 570f9e35b3bSMel Gorman case ISOLATE_ABORT: 571f9e35b3bSMel Gorman ret = COMPACT_PARTIAL; 572f9e35b3bSMel Gorman goto out; 573f9e35b3bSMel Gorman case ISOLATE_NONE: 574748446bbSMel Gorman continue; 575f9e35b3bSMel Gorman case ISOLATE_SUCCESS: 576f9e35b3bSMel Gorman ; 577f9e35b3bSMel Gorman } 578748446bbSMel Gorman 579748446bbSMel Gorman nr_migrate = cc->nr_migratepages; 5809d502c1cSMinchan Kim err = migrate_pages(&cc->migratepages, compaction_alloc, 5817f0f2496SMel Gorman (unsigned long)cc, false, 582a6bc32b8SMel Gorman cc->sync ? MIGRATE_SYNC_LIGHT : MIGRATE_ASYNC); 583748446bbSMel Gorman update_nr_listpages(cc); 584748446bbSMel Gorman nr_remaining = cc->nr_migratepages; 585748446bbSMel Gorman 586748446bbSMel Gorman count_vm_event(COMPACTBLOCKS); 587748446bbSMel Gorman count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining); 588748446bbSMel Gorman if (nr_remaining) 589748446bbSMel Gorman count_vm_events(COMPACTPAGEFAILED, nr_remaining); 590b7aba698SMel Gorman trace_mm_compaction_migratepages(nr_migrate - nr_remaining, 591b7aba698SMel Gorman nr_remaining); 592748446bbSMel Gorman 593748446bbSMel Gorman /* Release LRU pages not migrated */ 5949d502c1cSMinchan Kim if (err) { 595748446bbSMel Gorman putback_lru_pages(&cc->migratepages); 596748446bbSMel Gorman cc->nr_migratepages = 0; 597748446bbSMel Gorman } 598748446bbSMel Gorman 599748446bbSMel Gorman } 600748446bbSMel Gorman 601f9e35b3bSMel Gorman out: 602748446bbSMel Gorman /* Release free pages and check accounting */ 603748446bbSMel Gorman cc->nr_freepages -= release_freepages(&cc->freepages); 604748446bbSMel Gorman VM_BUG_ON(cc->nr_freepages != 0); 605748446bbSMel Gorman 606748446bbSMel Gorman return ret; 607748446bbSMel Gorman } 60876ab0f53SMel Gorman 609d43a87e6SKyungmin Park static unsigned long compact_zone_order(struct zone *zone, 61077f1fe6bSMel Gorman int order, gfp_t gfp_mask, 611d527caf2SAndrea Arcangeli bool sync) 61256de7263SMel Gorman { 61356de7263SMel Gorman struct compact_control cc = { 61456de7263SMel Gorman .nr_freepages = 0, 61556de7263SMel Gorman .nr_migratepages = 0, 61656de7263SMel Gorman .order = order, 61756de7263SMel Gorman .migratetype = allocflags_to_migratetype(gfp_mask), 61856de7263SMel Gorman .zone = zone, 61977f1fe6bSMel Gorman .sync = sync, 62056de7263SMel Gorman }; 62156de7263SMel Gorman INIT_LIST_HEAD(&cc.freepages); 62256de7263SMel Gorman INIT_LIST_HEAD(&cc.migratepages); 62356de7263SMel Gorman 62456de7263SMel Gorman return compact_zone(zone, &cc); 62556de7263SMel Gorman } 62656de7263SMel Gorman 6275e771905SMel Gorman int sysctl_extfrag_threshold = 500; 6285e771905SMel Gorman 62956de7263SMel Gorman /** 63056de7263SMel Gorman * try_to_compact_pages - Direct compact to satisfy a high-order allocation 63156de7263SMel Gorman * @zonelist: The zonelist used for the current allocation 63256de7263SMel Gorman * @order: The order of the current allocation 63356de7263SMel Gorman * @gfp_mask: The GFP mask of the current allocation 63456de7263SMel Gorman * @nodemask: The allowed nodes to allocate from 63577f1fe6bSMel Gorman * @sync: Whether migration is synchronous or not 63656de7263SMel Gorman * 63756de7263SMel Gorman * This is the main entry point for direct page compaction. 63856de7263SMel Gorman */ 63956de7263SMel Gorman unsigned long try_to_compact_pages(struct zonelist *zonelist, 64077f1fe6bSMel Gorman int order, gfp_t gfp_mask, nodemask_t *nodemask, 64177f1fe6bSMel Gorman bool sync) 64256de7263SMel Gorman { 64356de7263SMel Gorman enum zone_type high_zoneidx = gfp_zone(gfp_mask); 64456de7263SMel Gorman int may_enter_fs = gfp_mask & __GFP_FS; 64556de7263SMel Gorman int may_perform_io = gfp_mask & __GFP_IO; 64656de7263SMel Gorman struct zoneref *z; 64756de7263SMel Gorman struct zone *zone; 64856de7263SMel Gorman int rc = COMPACT_SKIPPED; 64956de7263SMel Gorman 65056de7263SMel Gorman /* 65156de7263SMel Gorman * Check whether it is worth even starting compaction. The order check is 65256de7263SMel Gorman * made because an assumption is made that the page allocator can satisfy 65356de7263SMel Gorman * the "cheaper" orders without taking special steps 65456de7263SMel Gorman */ 655c5a73c3dSAndrea Arcangeli if (!order || !may_enter_fs || !may_perform_io) 65656de7263SMel Gorman return rc; 65756de7263SMel Gorman 65856de7263SMel Gorman count_vm_event(COMPACTSTALL); 65956de7263SMel Gorman 66056de7263SMel Gorman /* Compact each zone in the list */ 66156de7263SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx, 66256de7263SMel Gorman nodemask) { 66356de7263SMel Gorman int status; 66456de7263SMel Gorman 665d527caf2SAndrea Arcangeli status = compact_zone_order(zone, order, gfp_mask, sync); 66656de7263SMel Gorman rc = max(status, rc); 66756de7263SMel Gorman 6683e7d3449SMel Gorman /* If a normal allocation would succeed, stop compacting */ 6693e7d3449SMel Gorman if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0, 0)) 67056de7263SMel Gorman break; 67156de7263SMel Gorman } 67256de7263SMel Gorman 67356de7263SMel Gorman return rc; 67456de7263SMel Gorman } 67556de7263SMel Gorman 67656de7263SMel Gorman 67776ab0f53SMel Gorman /* Compact all zones within a node */ 6787be62de9SRik van Riel static int __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc) 67976ab0f53SMel Gorman { 68076ab0f53SMel Gorman int zoneid; 68176ab0f53SMel Gorman struct zone *zone; 68276ab0f53SMel Gorman 68376ab0f53SMel Gorman /* Flush pending updates to the LRU lists */ 68476ab0f53SMel Gorman lru_add_drain_all(); 68576ab0f53SMel Gorman 68676ab0f53SMel Gorman for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { 68776ab0f53SMel Gorman 68876ab0f53SMel Gorman zone = &pgdat->node_zones[zoneid]; 68976ab0f53SMel Gorman if (!populated_zone(zone)) 69076ab0f53SMel Gorman continue; 69176ab0f53SMel Gorman 6927be62de9SRik van Riel cc->nr_freepages = 0; 6937be62de9SRik van Riel cc->nr_migratepages = 0; 6947be62de9SRik van Riel cc->zone = zone; 6957be62de9SRik van Riel INIT_LIST_HEAD(&cc->freepages); 6967be62de9SRik van Riel INIT_LIST_HEAD(&cc->migratepages); 69776ab0f53SMel Gorman 698*aff62249SRik van Riel if (cc->order < 0 || !compaction_deferred(zone, cc->order)) 6997be62de9SRik van Riel compact_zone(zone, cc); 70076ab0f53SMel Gorman 701*aff62249SRik van Riel if (cc->order > 0) { 702*aff62249SRik van Riel int ok = zone_watermark_ok(zone, cc->order, 703*aff62249SRik van Riel low_wmark_pages(zone), 0, 0); 704*aff62249SRik van Riel if (ok && cc->order > zone->compact_order_failed) 705*aff62249SRik van Riel zone->compact_order_failed = cc->order + 1; 706*aff62249SRik van Riel /* Currently async compaction is never deferred. */ 707*aff62249SRik van Riel else if (!ok && cc->sync) 708*aff62249SRik van Riel defer_compaction(zone, cc->order); 709*aff62249SRik van Riel } 710*aff62249SRik van Riel 7117be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->freepages)); 7127be62de9SRik van Riel VM_BUG_ON(!list_empty(&cc->migratepages)); 71376ab0f53SMel Gorman } 71476ab0f53SMel Gorman 71576ab0f53SMel Gorman return 0; 71676ab0f53SMel Gorman } 71776ab0f53SMel Gorman 7187be62de9SRik van Riel int compact_pgdat(pg_data_t *pgdat, int order) 7197be62de9SRik van Riel { 7207be62de9SRik van Riel struct compact_control cc = { 7217be62de9SRik van Riel .order = order, 7227be62de9SRik van Riel .sync = false, 7237be62de9SRik van Riel }; 7247be62de9SRik van Riel 7257be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 7267be62de9SRik van Riel } 7277be62de9SRik van Riel 7287be62de9SRik van Riel static int compact_node(int nid) 7297be62de9SRik van Riel { 7307be62de9SRik van Riel pg_data_t *pgdat; 7317be62de9SRik van Riel struct compact_control cc = { 7327be62de9SRik van Riel .order = -1, 7337be62de9SRik van Riel .sync = true, 7347be62de9SRik van Riel }; 7357be62de9SRik van Riel 7367be62de9SRik van Riel if (nid < 0 || nid >= nr_node_ids || !node_online(nid)) 7377be62de9SRik van Riel return -EINVAL; 7387be62de9SRik van Riel pgdat = NODE_DATA(nid); 7397be62de9SRik van Riel 7407be62de9SRik van Riel return __compact_pgdat(pgdat, &cc); 7417be62de9SRik van Riel } 7427be62de9SRik van Riel 74376ab0f53SMel Gorman /* Compact all nodes in the system */ 74476ab0f53SMel Gorman static int compact_nodes(void) 74576ab0f53SMel Gorman { 74676ab0f53SMel Gorman int nid; 74776ab0f53SMel Gorman 74876ab0f53SMel Gorman for_each_online_node(nid) 74976ab0f53SMel Gorman compact_node(nid); 75076ab0f53SMel Gorman 75176ab0f53SMel Gorman return COMPACT_COMPLETE; 75276ab0f53SMel Gorman } 75376ab0f53SMel Gorman 75476ab0f53SMel Gorman /* The written value is actually unused, all memory is compacted */ 75576ab0f53SMel Gorman int sysctl_compact_memory; 75676ab0f53SMel Gorman 75776ab0f53SMel Gorman /* This is the entry point for compacting all nodes via /proc/sys/vm */ 75876ab0f53SMel Gorman int sysctl_compaction_handler(struct ctl_table *table, int write, 75976ab0f53SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 76076ab0f53SMel Gorman { 76176ab0f53SMel Gorman if (write) 76276ab0f53SMel Gorman return compact_nodes(); 76376ab0f53SMel Gorman 76476ab0f53SMel Gorman return 0; 76576ab0f53SMel Gorman } 766ed4a6d7fSMel Gorman 7675e771905SMel Gorman int sysctl_extfrag_handler(struct ctl_table *table, int write, 7685e771905SMel Gorman void __user *buffer, size_t *length, loff_t *ppos) 7695e771905SMel Gorman { 7705e771905SMel Gorman proc_dointvec_minmax(table, write, buffer, length, ppos); 7715e771905SMel Gorman 7725e771905SMel Gorman return 0; 7735e771905SMel Gorman } 7745e771905SMel Gorman 775ed4a6d7fSMel Gorman #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) 77610fbcf4cSKay Sievers ssize_t sysfs_compact_node(struct device *dev, 77710fbcf4cSKay Sievers struct device_attribute *attr, 778ed4a6d7fSMel Gorman const char *buf, size_t count) 779ed4a6d7fSMel Gorman { 780ed4a6d7fSMel Gorman compact_node(dev->id); 781ed4a6d7fSMel Gorman 782ed4a6d7fSMel Gorman return count; 783ed4a6d7fSMel Gorman } 78410fbcf4cSKay Sievers static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node); 785ed4a6d7fSMel Gorman 786ed4a6d7fSMel Gorman int compaction_register_node(struct node *node) 787ed4a6d7fSMel Gorman { 78810fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_compact); 789ed4a6d7fSMel Gorman } 790ed4a6d7fSMel Gorman 791ed4a6d7fSMel Gorman void compaction_unregister_node(struct node *node) 792ed4a6d7fSMel Gorman { 79310fbcf4cSKay Sievers return device_remove_file(&node->dev, &dev_attr_compact); 794ed4a6d7fSMel Gorman } 795ed4a6d7fSMel Gorman #endif /* CONFIG_SYSFS && CONFIG_NUMA */ 796