| .. | .. |
|---|
| 50 | 50 | #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order) |
|---|
| 51 | 51 | #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order) |
|---|
| 52 | 52 | |
|---|
| 53 | +/* |
|---|
| 54 | + * Fragmentation score check interval for proactive compaction purposes. |
|---|
| 55 | + */ |
|---|
| 56 | +static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500; |
|---|
| 57 | + |
|---|
| 58 | +/* |
|---|
| 59 | + * Page order with-respect-to which proactive compaction |
|---|
| 60 | + * calculates external fragmentation, which is used as |
|---|
| 61 | + * the "fragmentation score" of a node/zone. |
|---|
| 62 | + */ |
|---|
| 63 | +#if defined CONFIG_TRANSPARENT_HUGEPAGE |
|---|
| 64 | +#define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER |
|---|
| 65 | +#elif defined CONFIG_HUGETLBFS |
|---|
| 66 | +#define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER |
|---|
| 67 | +#else |
|---|
| 68 | +#define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) |
|---|
| 69 | +#endif |
|---|
| 70 | + |
|---|
| 53 | 71 | static unsigned long release_freepages(struct list_head *freelist) |
|---|
| 54 | 72 | { |
|---|
| 55 | 73 | struct page *page, *next; |
|---|
| .. | .. |
|---|
| 66 | 84 | return high_pfn; |
|---|
| 67 | 85 | } |
|---|
| 68 | 86 | |
|---|
| 69 | | -static void map_pages(struct list_head *list) |
|---|
| 87 | +static void split_map_pages(struct list_head *list) |
|---|
| 70 | 88 | { |
|---|
| 71 | 89 | unsigned int i, order, nr_pages; |
|---|
| 72 | 90 | struct page *page, *next; |
|---|
| .. | .. |
|---|
| 136 | 154 | |
|---|
| 137 | 155 | /* |
|---|
| 138 | 156 | * Compaction is deferred when compaction fails to result in a page |
|---|
| 139 | | - * allocation success. 1 << compact_defer_limit compactions are skipped up |
|---|
| 157 | + * allocation success. 1 << compact_defer_shift, compactions are skipped up |
|---|
| 140 | 158 | * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT |
|---|
| 141 | 159 | */ |
|---|
| 142 | 160 | void defer_compaction(struct zone *zone, int order) |
|---|
| .. | .. |
|---|
| 162 | 180 | return false; |
|---|
| 163 | 181 | |
|---|
| 164 | 182 | /* Avoid possible overflow */ |
|---|
| 165 | | - if (++zone->compact_considered > defer_limit) |
|---|
| 183 | + if (++zone->compact_considered >= defer_limit) { |
|---|
| 166 | 184 | zone->compact_considered = defer_limit; |
|---|
| 167 | | - |
|---|
| 168 | | - if (zone->compact_considered >= defer_limit) |
|---|
| 169 | 185 | return false; |
|---|
| 186 | + } |
|---|
| 170 | 187 | |
|---|
| 171 | 188 | trace_mm_compaction_deferred(zone, order); |
|---|
| 172 | 189 | |
|---|
| .. | .. |
|---|
| 237 | 254 | return false; |
|---|
| 238 | 255 | } |
|---|
| 239 | 256 | |
|---|
| 257 | +static bool |
|---|
| 258 | +__reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, |
|---|
| 259 | + bool check_target) |
|---|
| 260 | +{ |
|---|
| 261 | + struct page *page = pfn_to_online_page(pfn); |
|---|
| 262 | + struct page *block_page; |
|---|
| 263 | + struct page *end_page; |
|---|
| 264 | + unsigned long block_pfn; |
|---|
| 265 | + |
|---|
| 266 | + if (!page) |
|---|
| 267 | + return false; |
|---|
| 268 | + if (zone != page_zone(page)) |
|---|
| 269 | + return false; |
|---|
| 270 | + if (pageblock_skip_persistent(page)) |
|---|
| 271 | + return false; |
|---|
| 272 | + |
|---|
| 273 | + /* |
|---|
| 274 | + * If skip is already cleared do no further checking once the |
|---|
| 275 | + * restart points have been set. |
|---|
| 276 | + */ |
|---|
| 277 | + if (check_source && check_target && !get_pageblock_skip(page)) |
|---|
| 278 | + return true; |
|---|
| 279 | + |
|---|
| 280 | + /* |
|---|
| 281 | + * If clearing skip for the target scanner, do not select a |
|---|
| 282 | + * non-movable pageblock as the starting point. |
|---|
| 283 | + */ |
|---|
| 284 | + if (!check_source && check_target && |
|---|
| 285 | + get_pageblock_migratetype(page) != MIGRATE_MOVABLE) |
|---|
| 286 | + return false; |
|---|
| 287 | + |
|---|
| 288 | + /* Ensure the start of the pageblock or zone is online and valid */ |
|---|
| 289 | + block_pfn = pageblock_start_pfn(pfn); |
|---|
| 290 | + block_pfn = max(block_pfn, zone->zone_start_pfn); |
|---|
| 291 | + block_page = pfn_to_online_page(block_pfn); |
|---|
| 292 | + if (block_page) { |
|---|
| 293 | + page = block_page; |
|---|
| 294 | + pfn = block_pfn; |
|---|
| 295 | + } |
|---|
| 296 | + |
|---|
| 297 | + /* Ensure the end of the pageblock or zone is online and valid */ |
|---|
| 298 | + block_pfn = pageblock_end_pfn(pfn) - 1; |
|---|
| 299 | + block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); |
|---|
| 300 | + end_page = pfn_to_online_page(block_pfn); |
|---|
| 301 | + if (!end_page) |
|---|
| 302 | + return false; |
|---|
| 303 | + |
|---|
| 304 | + /* |
|---|
| 305 | + * Only clear the hint if a sample indicates there is either a |
|---|
| 306 | + * free page or an LRU page in the block. One or other condition |
|---|
| 307 | + * is necessary for the block to be a migration source/target. |
|---|
| 308 | + */ |
|---|
| 309 | + do { |
|---|
| 310 | + if (pfn_valid_within(pfn)) { |
|---|
| 311 | + if (check_source && PageLRU(page)) { |
|---|
| 312 | + clear_pageblock_skip(page); |
|---|
| 313 | + return true; |
|---|
| 314 | + } |
|---|
| 315 | + |
|---|
| 316 | + if (check_target && PageBuddy(page)) { |
|---|
| 317 | + clear_pageblock_skip(page); |
|---|
| 318 | + return true; |
|---|
| 319 | + } |
|---|
| 320 | + } |
|---|
| 321 | + |
|---|
| 322 | + page += (1 << PAGE_ALLOC_COSTLY_ORDER); |
|---|
| 323 | + pfn += (1 << PAGE_ALLOC_COSTLY_ORDER); |
|---|
| 324 | + } while (page <= end_page); |
|---|
| 325 | + |
|---|
| 326 | + return false; |
|---|
| 327 | +} |
|---|
| 328 | + |
|---|
| 240 | 329 | /* |
|---|
| 241 | 330 | * This function is called to clear all cached information on pageblocks that |
|---|
| 242 | 331 | * should be skipped for page isolation when the migrate and free page scanner |
|---|
| .. | .. |
|---|
| 244 | 333 | */ |
|---|
| 245 | 334 | static void __reset_isolation_suitable(struct zone *zone) |
|---|
| 246 | 335 | { |
|---|
| 247 | | - unsigned long start_pfn = zone->zone_start_pfn; |
|---|
| 248 | | - unsigned long end_pfn = zone_end_pfn(zone); |
|---|
| 249 | | - unsigned long pfn; |
|---|
| 336 | + unsigned long migrate_pfn = zone->zone_start_pfn; |
|---|
| 337 | + unsigned long free_pfn = zone_end_pfn(zone) - 1; |
|---|
| 338 | + unsigned long reset_migrate = free_pfn; |
|---|
| 339 | + unsigned long reset_free = migrate_pfn; |
|---|
| 340 | + bool source_set = false; |
|---|
| 341 | + bool free_set = false; |
|---|
| 342 | + |
|---|
| 343 | + if (!zone->compact_blockskip_flush) |
|---|
| 344 | + return; |
|---|
| 250 | 345 | |
|---|
| 251 | 346 | zone->compact_blockskip_flush = false; |
|---|
| 252 | 347 | |
|---|
| 253 | | - /* Walk the zone and mark every pageblock as suitable for isolation */ |
|---|
| 254 | | - for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { |
|---|
| 255 | | - struct page *page; |
|---|
| 256 | | - |
|---|
| 348 | + /* |
|---|
| 349 | + * Walk the zone and update pageblock skip information. Source looks |
|---|
| 350 | + * for PageLRU while target looks for PageBuddy. When the scanner |
|---|
| 351 | + * is found, both PageBuddy and PageLRU are checked as the pageblock |
|---|
| 352 | + * is suitable as both source and target. |
|---|
| 353 | + */ |
|---|
| 354 | + for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, |
|---|
| 355 | + free_pfn -= pageblock_nr_pages) { |
|---|
| 257 | 356 | cond_resched(); |
|---|
| 258 | 357 | |
|---|
| 259 | | - page = pfn_to_online_page(pfn); |
|---|
| 260 | | - if (!page) |
|---|
| 261 | | - continue; |
|---|
| 262 | | - if (zone != page_zone(page)) |
|---|
| 263 | | - continue; |
|---|
| 264 | | - if (pageblock_skip_persistent(page)) |
|---|
| 265 | | - continue; |
|---|
| 358 | + /* Update the migrate PFN */ |
|---|
| 359 | + if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && |
|---|
| 360 | + migrate_pfn < reset_migrate) { |
|---|
| 361 | + source_set = true; |
|---|
| 362 | + reset_migrate = migrate_pfn; |
|---|
| 363 | + zone->compact_init_migrate_pfn = reset_migrate; |
|---|
| 364 | + zone->compact_cached_migrate_pfn[0] = reset_migrate; |
|---|
| 365 | + zone->compact_cached_migrate_pfn[1] = reset_migrate; |
|---|
| 366 | + } |
|---|
| 266 | 367 | |
|---|
| 267 | | - clear_pageblock_skip(page); |
|---|
| 368 | + /* Update the free PFN */ |
|---|
| 369 | + if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && |
|---|
| 370 | + free_pfn > reset_free) { |
|---|
| 371 | + free_set = true; |
|---|
| 372 | + reset_free = free_pfn; |
|---|
| 373 | + zone->compact_init_free_pfn = reset_free; |
|---|
| 374 | + zone->compact_cached_free_pfn = reset_free; |
|---|
| 375 | + } |
|---|
| 268 | 376 | } |
|---|
| 269 | 377 | |
|---|
| 270 | | - reset_cached_positions(zone); |
|---|
| 378 | + /* Leave no distance if no suitable block was reset */ |
|---|
| 379 | + if (reset_migrate >= reset_free) { |
|---|
| 380 | + zone->compact_cached_migrate_pfn[0] = migrate_pfn; |
|---|
| 381 | + zone->compact_cached_migrate_pfn[1] = migrate_pfn; |
|---|
| 382 | + zone->compact_cached_free_pfn = free_pfn; |
|---|
| 383 | + } |
|---|
| 271 | 384 | } |
|---|
| 272 | 385 | |
|---|
| 273 | 386 | void reset_isolation_suitable(pg_data_t *pgdat) |
|---|
| .. | .. |
|---|
| 286 | 399 | } |
|---|
| 287 | 400 | |
|---|
| 288 | 401 | /* |
|---|
| 402 | + * Sets the pageblock skip bit if it was clear. Note that this is a hint as |
|---|
| 403 | + * locks are not required for read/writers. Returns true if it was already set. |
|---|
| 404 | + */ |
|---|
| 405 | +static bool test_and_set_skip(struct compact_control *cc, struct page *page, |
|---|
| 406 | + unsigned long pfn) |
|---|
| 407 | +{ |
|---|
| 408 | + bool skip; |
|---|
| 409 | + |
|---|
| 410 | + /* Do no update if skip hint is being ignored */ |
|---|
| 411 | + if (cc->ignore_skip_hint) |
|---|
| 412 | + return false; |
|---|
| 413 | + |
|---|
| 414 | + if (!IS_ALIGNED(pfn, pageblock_nr_pages)) |
|---|
| 415 | + return false; |
|---|
| 416 | + |
|---|
| 417 | + skip = get_pageblock_skip(page); |
|---|
| 418 | + if (!skip && !cc->no_set_skip_hint) |
|---|
| 419 | + set_pageblock_skip(page); |
|---|
| 420 | + |
|---|
| 421 | + return skip; |
|---|
| 422 | +} |
|---|
| 423 | + |
|---|
| 424 | +static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
|---|
| 425 | +{ |
|---|
| 426 | + struct zone *zone = cc->zone; |
|---|
| 427 | + |
|---|
| 428 | + pfn = pageblock_end_pfn(pfn); |
|---|
| 429 | + |
|---|
| 430 | + /* Set for isolation rather than compaction */ |
|---|
| 431 | + if (cc->no_set_skip_hint) |
|---|
| 432 | + return; |
|---|
| 433 | + |
|---|
| 434 | + if (pfn > zone->compact_cached_migrate_pfn[0]) |
|---|
| 435 | + zone->compact_cached_migrate_pfn[0] = pfn; |
|---|
| 436 | + if (cc->mode != MIGRATE_ASYNC && |
|---|
| 437 | + pfn > zone->compact_cached_migrate_pfn[1]) |
|---|
| 438 | + zone->compact_cached_migrate_pfn[1] = pfn; |
|---|
| 439 | +} |
|---|
| 440 | + |
|---|
| 441 | +/* |
|---|
| 289 | 442 | * If no pages were isolated then mark this pageblock to be skipped in the |
|---|
| 290 | 443 | * future. The information is later cleared by __reset_isolation_suitable(). |
|---|
| 291 | 444 | */ |
|---|
| 292 | 445 | static void update_pageblock_skip(struct compact_control *cc, |
|---|
| 293 | | - struct page *page, unsigned long nr_isolated, |
|---|
| 294 | | - bool migrate_scanner) |
|---|
| 446 | + struct page *page, unsigned long pfn) |
|---|
| 295 | 447 | { |
|---|
| 296 | 448 | struct zone *zone = cc->zone; |
|---|
| 297 | | - unsigned long pfn; |
|---|
| 298 | 449 | |
|---|
| 299 | 450 | if (cc->no_set_skip_hint) |
|---|
| 300 | 451 | return; |
|---|
| .. | .. |
|---|
| 302 | 453 | if (!page) |
|---|
| 303 | 454 | return; |
|---|
| 304 | 455 | |
|---|
| 305 | | - if (nr_isolated) |
|---|
| 306 | | - return; |
|---|
| 307 | | - |
|---|
| 308 | 456 | set_pageblock_skip(page); |
|---|
| 309 | 457 | |
|---|
| 310 | | - pfn = page_to_pfn(page); |
|---|
| 311 | | - |
|---|
| 312 | 458 | /* Update where async and sync compaction should restart */ |
|---|
| 313 | | - if (migrate_scanner) { |
|---|
| 314 | | - if (pfn > zone->compact_cached_migrate_pfn[0]) |
|---|
| 315 | | - zone->compact_cached_migrate_pfn[0] = pfn; |
|---|
| 316 | | - if (cc->mode != MIGRATE_ASYNC && |
|---|
| 317 | | - pfn > zone->compact_cached_migrate_pfn[1]) |
|---|
| 318 | | - zone->compact_cached_migrate_pfn[1] = pfn; |
|---|
| 319 | | - } else { |
|---|
| 320 | | - if (pfn < zone->compact_cached_free_pfn) |
|---|
| 321 | | - zone->compact_cached_free_pfn = pfn; |
|---|
| 322 | | - } |
|---|
| 459 | + if (pfn < zone->compact_cached_free_pfn) |
|---|
| 460 | + zone->compact_cached_free_pfn = pfn; |
|---|
| 323 | 461 | } |
|---|
| 324 | 462 | #else |
|---|
| 325 | 463 | static inline bool isolation_suitable(struct compact_control *cc, |
|---|
| .. | .. |
|---|
| 334 | 472 | } |
|---|
| 335 | 473 | |
|---|
| 336 | 474 | static inline void update_pageblock_skip(struct compact_control *cc, |
|---|
| 337 | | - struct page *page, unsigned long nr_isolated, |
|---|
| 338 | | - bool migrate_scanner) |
|---|
| 475 | + struct page *page, unsigned long pfn) |
|---|
| 339 | 476 | { |
|---|
| 477 | +} |
|---|
| 478 | + |
|---|
| 479 | +static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
|---|
| 480 | +{ |
|---|
| 481 | +} |
|---|
| 482 | + |
|---|
| 483 | +static bool test_and_set_skip(struct compact_control *cc, struct page *page, |
|---|
| 484 | + unsigned long pfn) |
|---|
| 485 | +{ |
|---|
| 486 | + return false; |
|---|
| 340 | 487 | } |
|---|
| 341 | 488 | #endif /* CONFIG_COMPACTION */ |
|---|
| 342 | 489 | |
|---|
| 343 | 490 | /* |
|---|
| 344 | 491 | * Compaction requires the taking of some coarse locks that are potentially |
|---|
| 345 | | - * very heavily contended. For async compaction, back out if the lock cannot |
|---|
| 346 | | - * be taken immediately. For sync compaction, spin on the lock if needed. |
|---|
| 492 | + * very heavily contended. For async compaction, trylock and record if the |
|---|
| 493 | + * lock is contended. The lock will still be acquired but compaction will |
|---|
| 494 | + * abort when the current block is finished regardless of success rate. |
|---|
| 495 | + * Sync compaction acquires the lock. |
|---|
| 347 | 496 | * |
|---|
| 348 | | - * Returns true if the lock is held |
|---|
| 349 | | - * Returns false if the lock is not held and compaction should abort |
|---|
| 497 | + * Always returns true which makes it easier to track lock state in callers. |
|---|
| 350 | 498 | */ |
|---|
| 351 | | -static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags, |
|---|
| 499 | +static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, |
|---|
| 352 | 500 | struct compact_control *cc) |
|---|
| 501 | + __acquires(lock) |
|---|
| 353 | 502 | { |
|---|
| 354 | | - if (cc->mode == MIGRATE_ASYNC) { |
|---|
| 355 | | - if (!spin_trylock_irqsave(lock, *flags)) { |
|---|
| 356 | | - cc->contended = true; |
|---|
| 357 | | - return false; |
|---|
| 358 | | - } |
|---|
| 359 | | - } else { |
|---|
| 360 | | - spin_lock_irqsave(lock, *flags); |
|---|
| 503 | + /* Track if the lock is contended in async mode */ |
|---|
| 504 | + if (cc->mode == MIGRATE_ASYNC && !cc->contended) { |
|---|
| 505 | + if (spin_trylock_irqsave(lock, *flags)) |
|---|
| 506 | + return true; |
|---|
| 507 | + |
|---|
| 508 | + cc->contended = true; |
|---|
| 361 | 509 | } |
|---|
| 362 | 510 | |
|---|
| 511 | + spin_lock_irqsave(lock, *flags); |
|---|
| 363 | 512 | return true; |
|---|
| 364 | 513 | } |
|---|
| 365 | 514 | |
|---|
| .. | .. |
|---|
| 391 | 540 | return true; |
|---|
| 392 | 541 | } |
|---|
| 393 | 542 | |
|---|
| 394 | | - if (need_resched()) { |
|---|
| 395 | | - if (cc->mode == MIGRATE_ASYNC) { |
|---|
| 396 | | - cc->contended = true; |
|---|
| 397 | | - return true; |
|---|
| 398 | | - } |
|---|
| 399 | | - cond_resched(); |
|---|
| 400 | | - } |
|---|
| 401 | | - |
|---|
| 402 | | - return false; |
|---|
| 403 | | -} |
|---|
| 404 | | - |
|---|
| 405 | | -/* |
|---|
| 406 | | - * Aside from avoiding lock contention, compaction also periodically checks |
|---|
| 407 | | - * need_resched() and either schedules in sync compaction or aborts async |
|---|
| 408 | | - * compaction. This is similar to what compact_unlock_should_abort() does, but |
|---|
| 409 | | - * is used where no lock is concerned. |
|---|
| 410 | | - * |
|---|
| 411 | | - * Returns false when no scheduling was needed, or sync compaction scheduled. |
|---|
| 412 | | - * Returns true when async compaction should abort. |
|---|
| 413 | | - */ |
|---|
| 414 | | -static inline bool compact_should_abort(struct compact_control *cc) |
|---|
| 415 | | -{ |
|---|
| 416 | | - /* async compaction aborts if contended */ |
|---|
| 417 | | - if (need_resched()) { |
|---|
| 418 | | - if (cc->mode == MIGRATE_ASYNC) { |
|---|
| 419 | | - cc->contended = true; |
|---|
| 420 | | - return true; |
|---|
| 421 | | - } |
|---|
| 422 | | - |
|---|
| 423 | | - cond_resched(); |
|---|
| 424 | | - } |
|---|
| 543 | + cond_resched(); |
|---|
| 425 | 544 | |
|---|
| 426 | 545 | return false; |
|---|
| 427 | 546 | } |
|---|
| .. | .. |
|---|
| 435 | 554 | unsigned long *start_pfn, |
|---|
| 436 | 555 | unsigned long end_pfn, |
|---|
| 437 | 556 | struct list_head *freelist, |
|---|
| 557 | + unsigned int stride, |
|---|
| 438 | 558 | bool strict) |
|---|
| 439 | 559 | { |
|---|
| 440 | 560 | int nr_scanned = 0, total_isolated = 0; |
|---|
| 441 | | - struct page *cursor, *valid_page = NULL; |
|---|
| 561 | + struct page *cursor; |
|---|
| 442 | 562 | unsigned long flags = 0; |
|---|
| 443 | 563 | bool locked = false; |
|---|
| 444 | 564 | unsigned long blockpfn = *start_pfn; |
|---|
| 445 | 565 | unsigned int order; |
|---|
| 446 | 566 | |
|---|
| 567 | + /* Strict mode is for isolation, speed is secondary */ |
|---|
| 568 | + if (strict) |
|---|
| 569 | + stride = 1; |
|---|
| 570 | + |
|---|
| 447 | 571 | cursor = pfn_to_page(blockpfn); |
|---|
| 448 | 572 | |
|---|
| 449 | 573 | /* Isolate free pages. */ |
|---|
| 450 | | - for (; blockpfn < end_pfn; blockpfn++, cursor++) { |
|---|
| 574 | + for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) { |
|---|
| 451 | 575 | int isolated; |
|---|
| 452 | 576 | struct page *page = cursor; |
|---|
| 453 | 577 | |
|---|
| .. | .. |
|---|
| 464 | 588 | nr_scanned++; |
|---|
| 465 | 589 | if (!pfn_valid_within(blockpfn)) |
|---|
| 466 | 590 | goto isolate_fail; |
|---|
| 467 | | - |
|---|
| 468 | | - if (!valid_page) |
|---|
| 469 | | - valid_page = page; |
|---|
| 470 | 591 | |
|---|
| 471 | 592 | /* |
|---|
| 472 | 593 | * For compound pages such as THP and hugetlbfs, we can save |
|---|
| .. | .. |
|---|
| 495 | 616 | * recheck as well. |
|---|
| 496 | 617 | */ |
|---|
| 497 | 618 | if (!locked) { |
|---|
| 498 | | - /* |
|---|
| 499 | | - * The zone lock must be held to isolate freepages. |
|---|
| 500 | | - * Unfortunately this is a very coarse lock and can be |
|---|
| 501 | | - * heavily contended if there are parallel allocations |
|---|
| 502 | | - * or parallel compactions. For async compaction do not |
|---|
| 503 | | - * spin on the lock and we acquire the lock as late as |
|---|
| 504 | | - * possible. |
|---|
| 505 | | - */ |
|---|
| 506 | | - locked = compact_trylock_irqsave(&cc->zone->lock, |
|---|
| 619 | + locked = compact_lock_irqsave(&cc->zone->lock, |
|---|
| 507 | 620 | &flags, cc); |
|---|
| 508 | | - if (!locked) |
|---|
| 509 | | - break; |
|---|
| 510 | 621 | |
|---|
| 511 | 622 | /* Recheck this is a buddy page under lock */ |
|---|
| 512 | 623 | if (!PageBuddy(page)) |
|---|
| .. | .. |
|---|
| 514 | 625 | } |
|---|
| 515 | 626 | |
|---|
| 516 | 627 | /* Found a free page, will break it into order-0 pages */ |
|---|
| 517 | | - order = page_order(page); |
|---|
| 628 | + order = buddy_order(page); |
|---|
| 518 | 629 | isolated = __isolate_free_page(page, order); |
|---|
| 519 | 630 | if (!isolated) |
|---|
| 520 | 631 | break; |
|---|
| .. | .. |
|---|
| 564 | 675 | */ |
|---|
| 565 | 676 | if (strict && blockpfn < end_pfn) |
|---|
| 566 | 677 | total_isolated = 0; |
|---|
| 567 | | - |
|---|
| 568 | | - /* Update the pageblock-skip if the whole pageblock was scanned */ |
|---|
| 569 | | - if (blockpfn == end_pfn) |
|---|
| 570 | | - update_pageblock_skip(cc, valid_page, total_isolated, false); |
|---|
| 571 | 678 | |
|---|
| 572 | 679 | cc->total_free_scanned += nr_scanned; |
|---|
| 573 | 680 | if (total_isolated) |
|---|
| .. | .. |
|---|
| 626 | 733 | break; |
|---|
| 627 | 734 | |
|---|
| 628 | 735 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
|---|
| 629 | | - block_end_pfn, &freelist, true); |
|---|
| 736 | + block_end_pfn, &freelist, 0, true); |
|---|
| 630 | 737 | |
|---|
| 631 | 738 | /* |
|---|
| 632 | 739 | * In strict mode, isolate_freepages_block() returns 0 if |
|---|
| .. | .. |
|---|
| 644 | 751 | } |
|---|
| 645 | 752 | |
|---|
| 646 | 753 | /* __isolate_free_page() does not map the pages */ |
|---|
| 647 | | - map_pages(&freelist); |
|---|
| 754 | + split_map_pages(&freelist); |
|---|
| 648 | 755 | |
|---|
| 649 | 756 | if (pfn < end_pfn) { |
|---|
| 650 | 757 | /* Loop terminated early, cleanup. */ |
|---|
| .. | .. |
|---|
| 656 | 763 | return pfn; |
|---|
| 657 | 764 | } |
|---|
| 658 | 765 | |
|---|
| 766 | +#ifdef CONFIG_COMPACTION |
|---|
| 767 | +unsigned long isolate_and_split_free_page(struct page *page, |
|---|
| 768 | + struct list_head *list) |
|---|
| 769 | +{ |
|---|
| 770 | + unsigned long isolated; |
|---|
| 771 | + unsigned int order; |
|---|
| 772 | + |
|---|
| 773 | + if (!PageBuddy(page)) |
|---|
| 774 | + return 0; |
|---|
| 775 | + |
|---|
| 776 | + order = buddy_order(page); |
|---|
| 777 | + isolated = __isolate_free_page(page, order); |
|---|
| 778 | + if (!isolated) |
|---|
| 779 | + return 0; |
|---|
| 780 | + |
|---|
| 781 | + set_page_private(page, order); |
|---|
| 782 | + list_add(&page->lru, list); |
|---|
| 783 | + |
|---|
| 784 | + split_map_pages(list); |
|---|
| 785 | + |
|---|
| 786 | + return isolated; |
|---|
| 787 | +} |
|---|
| 788 | +EXPORT_SYMBOL_GPL(isolate_and_split_free_page); |
|---|
| 789 | +#endif |
|---|
| 790 | + |
|---|
| 659 | 791 | /* Similar to reclaim, but different enough that they don't share logic */ |
|---|
| 660 | | -static bool too_many_isolated(struct zone *zone) |
|---|
| 792 | +static bool too_many_isolated(pg_data_t *pgdat) |
|---|
| 661 | 793 | { |
|---|
| 662 | 794 | unsigned long active, inactive, isolated; |
|---|
| 663 | 795 | |
|---|
| 664 | | - inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) + |
|---|
| 665 | | - node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON); |
|---|
| 666 | | - active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) + |
|---|
| 667 | | - node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON); |
|---|
| 668 | | - isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) + |
|---|
| 669 | | - node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON); |
|---|
| 796 | + inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + |
|---|
| 797 | + node_page_state(pgdat, NR_INACTIVE_ANON); |
|---|
| 798 | + active = node_page_state(pgdat, NR_ACTIVE_FILE) + |
|---|
| 799 | + node_page_state(pgdat, NR_ACTIVE_ANON); |
|---|
| 800 | + isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + |
|---|
| 801 | + node_page_state(pgdat, NR_ISOLATED_ANON); |
|---|
| 670 | 802 | |
|---|
| 671 | 803 | return isolated > (inactive + active) / 2; |
|---|
| 672 | 804 | } |
|---|
| .. | .. |
|---|
| 693 | 825 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
|---|
| 694 | 826 | unsigned long end_pfn, isolate_mode_t isolate_mode) |
|---|
| 695 | 827 | { |
|---|
| 696 | | - struct zone *zone = cc->zone; |
|---|
| 828 | + pg_data_t *pgdat = cc->zone->zone_pgdat; |
|---|
| 697 | 829 | unsigned long nr_scanned = 0, nr_isolated = 0; |
|---|
| 698 | 830 | struct lruvec *lruvec; |
|---|
| 699 | 831 | unsigned long flags = 0; |
|---|
| .. | .. |
|---|
| 702 | 834 | unsigned long start_pfn = low_pfn; |
|---|
| 703 | 835 | bool skip_on_failure = false; |
|---|
| 704 | 836 | unsigned long next_skip_pfn = 0; |
|---|
| 837 | + bool skip_updated = false; |
|---|
| 705 | 838 | |
|---|
| 706 | 839 | /* |
|---|
| 707 | 840 | * Ensure that there are not too many pages isolated from the LRU |
|---|
| 708 | 841 | * list by either parallel reclaimers or compaction. If there are, |
|---|
| 709 | 842 | * delay for some time until fewer pages are isolated |
|---|
| 710 | 843 | */ |
|---|
| 711 | | - while (unlikely(too_many_isolated(zone))) { |
|---|
| 844 | + while (unlikely(too_many_isolated(pgdat))) { |
|---|
| 845 | + /* stop isolation if there are still pages not migrated */ |
|---|
| 846 | + if (cc->nr_migratepages) |
|---|
| 847 | + return 0; |
|---|
| 848 | + |
|---|
| 712 | 849 | /* async migration should just abort */ |
|---|
| 713 | 850 | if (cc->mode == MIGRATE_ASYNC) |
|---|
| 714 | 851 | return 0; |
|---|
| .. | .. |
|---|
| 719 | 856 | return 0; |
|---|
| 720 | 857 | } |
|---|
| 721 | 858 | |
|---|
| 722 | | - if (compact_should_abort(cc)) |
|---|
| 723 | | - return 0; |
|---|
| 859 | + cond_resched(); |
|---|
| 724 | 860 | |
|---|
| 725 | 861 | if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { |
|---|
| 726 | 862 | skip_on_failure = true; |
|---|
| .. | .. |
|---|
| 754 | 890 | |
|---|
| 755 | 891 | /* |
|---|
| 756 | 892 | * Periodically drop the lock (if held) regardless of its |
|---|
| 757 | | - * contention, to give chance to IRQs. Abort async compaction |
|---|
| 758 | | - * if contended. |
|---|
| 893 | + * contention, to give chance to IRQs. Abort completely if |
|---|
| 894 | + * a fatal signal is pending. |
|---|
| 759 | 895 | */ |
|---|
| 760 | 896 | if (!(low_pfn % SWAP_CLUSTER_MAX) |
|---|
| 761 | | - && compact_unlock_should_abort(zone_lru_lock(zone), flags, |
|---|
| 762 | | - &locked, cc)) |
|---|
| 763 | | - break; |
|---|
| 897 | + && compact_unlock_should_abort(&pgdat->lru_lock, |
|---|
| 898 | + flags, &locked, cc)) { |
|---|
| 899 | + low_pfn = 0; |
|---|
| 900 | + goto fatal_pending; |
|---|
| 901 | + } |
|---|
| 764 | 902 | |
|---|
| 765 | 903 | if (!pfn_valid_within(low_pfn)) |
|---|
| 766 | 904 | goto isolate_fail; |
|---|
| .. | .. |
|---|
| 768 | 906 | |
|---|
| 769 | 907 | page = pfn_to_page(low_pfn); |
|---|
| 770 | 908 | |
|---|
| 771 | | - if (!valid_page) |
|---|
| 909 | + /* |
|---|
| 910 | + * Check if the pageblock has already been marked skipped. |
|---|
| 911 | + * Only the aligned PFN is checked as the caller isolates |
|---|
| 912 | + * COMPACT_CLUSTER_MAX at a time so the second call must |
|---|
| 913 | + * not falsely conclude that the block should be skipped. |
|---|
| 914 | + */ |
|---|
| 915 | + if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) { |
|---|
| 916 | + if (!cc->ignore_skip_hint && get_pageblock_skip(page)) { |
|---|
| 917 | + low_pfn = end_pfn; |
|---|
| 918 | + goto isolate_abort; |
|---|
| 919 | + } |
|---|
| 772 | 920 | valid_page = page; |
|---|
| 921 | + } |
|---|
| 773 | 922 | |
|---|
| 774 | 923 | /* |
|---|
| 775 | 924 | * Skip if free. We read page order here without zone lock |
|---|
| .. | .. |
|---|
| 778 | 927 | * potential isolation targets. |
|---|
| 779 | 928 | */ |
|---|
| 780 | 929 | if (PageBuddy(page)) { |
|---|
| 781 | | - unsigned long freepage_order = page_order_unsafe(page); |
|---|
| 930 | + unsigned long freepage_order = buddy_order_unsafe(page); |
|---|
| 782 | 931 | |
|---|
| 783 | 932 | /* |
|---|
| 784 | 933 | * Without lock, we cannot be sure that what we got is |
|---|
| .. | .. |
|---|
| 792 | 941 | |
|---|
| 793 | 942 | /* |
|---|
| 794 | 943 | * Regardless of being on LRU, compound pages such as THP and |
|---|
| 795 | | - * hugetlbfs are not to be compacted. We can potentially save |
|---|
| 796 | | - * a lot of iterations if we skip them at once. The check is |
|---|
| 797 | | - * racy, but we can consider only valid values and the only |
|---|
| 798 | | - * danger is skipping too much. |
|---|
| 944 | + * hugetlbfs are not to be compacted unless we are attempting |
|---|
| 945 | + * an allocation much larger than the huge page size (eg CMA). |
|---|
| 946 | + * We can potentially save a lot of iterations if we skip them |
|---|
| 947 | + * at once. The check is racy, but we can consider only valid |
|---|
| 948 | + * values and the only danger is skipping too much. |
|---|
| 799 | 949 | */ |
|---|
| 800 | | - if (PageCompound(page)) { |
|---|
| 950 | + if (PageCompound(page) && !cc->alloc_contig) { |
|---|
| 801 | 951 | const unsigned int order = compound_order(page); |
|---|
| 802 | 952 | |
|---|
| 803 | 953 | if (likely(order < MAX_ORDER)) |
|---|
| .. | .. |
|---|
| 818 | 968 | if (unlikely(__PageMovable(page)) && |
|---|
| 819 | 969 | !PageIsolated(page)) { |
|---|
| 820 | 970 | if (locked) { |
|---|
| 821 | | - spin_unlock_irqrestore(zone_lru_lock(zone), |
|---|
| 971 | + spin_unlock_irqrestore(&pgdat->lru_lock, |
|---|
| 822 | 972 | flags); |
|---|
| 823 | 973 | locked = false; |
|---|
| 824 | 974 | } |
|---|
| .. | .. |
|---|
| 848 | 998 | |
|---|
| 849 | 999 | /* If we already hold the lock, we can skip some rechecking */ |
|---|
| 850 | 1000 | if (!locked) { |
|---|
| 851 | | - locked = compact_trylock_irqsave(zone_lru_lock(zone), |
|---|
| 1001 | + locked = compact_lock_irqsave(&pgdat->lru_lock, |
|---|
| 852 | 1002 | &flags, cc); |
|---|
| 853 | | - if (!locked) |
|---|
| 854 | | - break; |
|---|
| 1003 | + |
|---|
| 1004 | + /* Try get exclusive access under lock */ |
|---|
| 1005 | + if (!skip_updated) { |
|---|
| 1006 | + skip_updated = true; |
|---|
| 1007 | + if (test_and_set_skip(cc, page, low_pfn)) |
|---|
| 1008 | + goto isolate_abort; |
|---|
| 1009 | + } |
|---|
| 855 | 1010 | |
|---|
| 856 | 1011 | /* Recheck PageLRU and PageCompound under lock */ |
|---|
| 857 | 1012 | if (!PageLRU(page)) |
|---|
| .. | .. |
|---|
| 862 | 1017 | * and it's on LRU. It can only be a THP so the order |
|---|
| 863 | 1018 | * is safe to read and it's 0 for tail pages. |
|---|
| 864 | 1019 | */ |
|---|
| 865 | | - if (unlikely(PageCompound(page))) { |
|---|
| 866 | | - low_pfn += (1UL << compound_order(page)) - 1; |
|---|
| 1020 | + if (unlikely(PageCompound(page) && !cc->alloc_contig)) { |
|---|
| 1021 | + low_pfn += compound_nr(page) - 1; |
|---|
| 867 | 1022 | goto isolate_fail; |
|---|
| 868 | 1023 | } |
|---|
| 869 | 1024 | } |
|---|
| 870 | 1025 | |
|---|
| 871 | | - lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); |
|---|
| 1026 | + lruvec = mem_cgroup_page_lruvec(page, pgdat); |
|---|
| 872 | 1027 | |
|---|
| 873 | 1028 | /* Try isolate the page */ |
|---|
| 874 | 1029 | if (__isolate_lru_page(page, isolate_mode) != 0) |
|---|
| 875 | 1030 | goto isolate_fail; |
|---|
| 876 | 1031 | |
|---|
| 877 | | - VM_BUG_ON_PAGE(PageCompound(page), page); |
|---|
| 1032 | + /* The whole page is taken off the LRU; skip the tail pages. */ |
|---|
| 1033 | + if (PageCompound(page)) |
|---|
| 1034 | + low_pfn += compound_nr(page) - 1; |
|---|
| 878 | 1035 | |
|---|
| 879 | 1036 | /* Successfully isolated */ |
|---|
| 880 | 1037 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
|---|
| 881 | | - inc_node_page_state(page, |
|---|
| 882 | | - NR_ISOLATED_ANON + page_is_file_cache(page)); |
|---|
| 1038 | + mod_node_page_state(page_pgdat(page), |
|---|
| 1039 | + NR_ISOLATED_ANON + page_is_file_lru(page), |
|---|
| 1040 | + thp_nr_pages(page)); |
|---|
| 883 | 1041 | |
|---|
| 884 | 1042 | isolate_success: |
|---|
| 885 | 1043 | list_add(&page->lru, &cc->migratepages); |
|---|
| 886 | | - cc->nr_migratepages++; |
|---|
| 887 | | - nr_isolated++; |
|---|
| 1044 | + cc->nr_migratepages += compound_nr(page); |
|---|
| 1045 | + nr_isolated += compound_nr(page); |
|---|
| 888 | 1046 | |
|---|
| 889 | 1047 | /* |
|---|
| 890 | | - * Record where we could have freed pages by migration and not |
|---|
| 891 | | - * yet flushed them to buddy allocator. |
|---|
| 892 | | - * - this is the lowest page that was isolated and likely be |
|---|
| 893 | | - * then freed by migration. |
|---|
| 1048 | + * Avoid isolating too much unless this block is being |
|---|
| 1049 | + * rescanned (e.g. dirty/writeback pages, parallel allocation) |
|---|
| 1050 | + * or a lock is contended. For contention, isolate quickly to |
|---|
| 1051 | + * potentially remove one source of contention. |
|---|
| 894 | 1052 | */ |
|---|
| 895 | | - if (!cc->last_migrated_pfn) |
|---|
| 896 | | - cc->last_migrated_pfn = low_pfn; |
|---|
| 897 | | - |
|---|
| 898 | | - /* Avoid isolating too much */ |
|---|
| 899 | | - if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) { |
|---|
| 1053 | + if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && |
|---|
| 1054 | + !cc->rescan && !cc->contended) { |
|---|
| 900 | 1055 | ++low_pfn; |
|---|
| 901 | 1056 | break; |
|---|
| 902 | 1057 | } |
|---|
| .. | .. |
|---|
| 913 | 1068 | */ |
|---|
| 914 | 1069 | if (nr_isolated) { |
|---|
| 915 | 1070 | if (locked) { |
|---|
| 916 | | - spin_unlock_irqrestore(zone_lru_lock(zone), flags); |
|---|
| 1071 | + spin_unlock_irqrestore(&pgdat->lru_lock, flags); |
|---|
| 917 | 1072 | locked = false; |
|---|
| 918 | 1073 | } |
|---|
| 919 | 1074 | putback_movable_pages(&cc->migratepages); |
|---|
| 920 | 1075 | cc->nr_migratepages = 0; |
|---|
| 921 | | - cc->last_migrated_pfn = 0; |
|---|
| 922 | 1076 | nr_isolated = 0; |
|---|
| 923 | 1077 | } |
|---|
| 924 | 1078 | |
|---|
| .. | .. |
|---|
| 939 | 1093 | if (unlikely(low_pfn > end_pfn)) |
|---|
| 940 | 1094 | low_pfn = end_pfn; |
|---|
| 941 | 1095 | |
|---|
| 1096 | +isolate_abort: |
|---|
| 942 | 1097 | if (locked) |
|---|
| 943 | | - spin_unlock_irqrestore(zone_lru_lock(zone), flags); |
|---|
| 1098 | + spin_unlock_irqrestore(&pgdat->lru_lock, flags); |
|---|
| 944 | 1099 | |
|---|
| 945 | 1100 | /* |
|---|
| 946 | | - * Update the pageblock-skip information and cached scanner pfn, |
|---|
| 947 | | - * if the whole pageblock was scanned without isolating any page. |
|---|
| 1101 | + * Updated the cached scanner pfn once the pageblock has been scanned |
|---|
| 1102 | + * Pages will either be migrated in which case there is no point |
|---|
| 1103 | + * scanning in the near future or migration failed in which case the |
|---|
| 1104 | + * failure reason may persist. The block is marked for skipping if |
|---|
| 1105 | + * there were no pages isolated in the block or if the block is |
|---|
| 1106 | + * rescanned twice in a row. |
|---|
| 948 | 1107 | */ |
|---|
| 949 | | - if (low_pfn == end_pfn) |
|---|
| 950 | | - update_pageblock_skip(cc, valid_page, nr_isolated, true); |
|---|
| 1108 | + if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) { |
|---|
| 1109 | + if (valid_page && !skip_updated) |
|---|
| 1110 | + set_pageblock_skip(valid_page); |
|---|
| 1111 | + update_cached_migrate(cc, low_pfn); |
|---|
| 1112 | + } |
|---|
| 951 | 1113 | |
|---|
| 952 | 1114 | trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, |
|---|
| 953 | 1115 | nr_scanned, nr_isolated); |
|---|
| 954 | 1116 | |
|---|
| 1117 | +fatal_pending: |
|---|
| 955 | 1118 | cc->total_migrate_scanned += nr_scanned; |
|---|
| 956 | 1119 | if (nr_isolated) |
|---|
| 957 | 1120 | count_compact_events(COMPACTISOLATED, nr_isolated); |
|---|
| .. | .. |
|---|
| 998 | 1161 | if (!pfn) |
|---|
| 999 | 1162 | break; |
|---|
| 1000 | 1163 | |
|---|
| 1001 | | - if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) |
|---|
| 1164 | + if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) |
|---|
| 1002 | 1165 | break; |
|---|
| 1003 | 1166 | } |
|---|
| 1004 | 1167 | |
|---|
| .. | .. |
|---|
| 1012 | 1175 | struct page *page) |
|---|
| 1013 | 1176 | { |
|---|
| 1014 | 1177 | int block_mt; |
|---|
| 1178 | + |
|---|
| 1179 | + if (pageblock_skip_persistent(page)) |
|---|
| 1180 | + return false; |
|---|
| 1015 | 1181 | |
|---|
| 1016 | 1182 | if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) |
|---|
| 1017 | 1183 | return true; |
|---|
| .. | .. |
|---|
| 1035 | 1201 | * the only small danger is that we skip a potentially suitable |
|---|
| 1036 | 1202 | * pageblock, so it's not worth to check order for valid range. |
|---|
| 1037 | 1203 | */ |
|---|
| 1038 | | - if (page_order_unsafe(page) >= pageblock_order) |
|---|
| 1204 | + if (buddy_order_unsafe(page) >= pageblock_order) |
|---|
| 1039 | 1205 | return false; |
|---|
| 1040 | 1206 | } |
|---|
| 1041 | 1207 | |
|---|
| .. | .. |
|---|
| 1050 | 1216 | return false; |
|---|
| 1051 | 1217 | } |
|---|
| 1052 | 1218 | |
|---|
| 1219 | +static inline unsigned int |
|---|
| 1220 | +freelist_scan_limit(struct compact_control *cc) |
|---|
| 1221 | +{ |
|---|
| 1222 | + unsigned short shift = BITS_PER_LONG - 1; |
|---|
| 1223 | + |
|---|
| 1224 | + return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; |
|---|
| 1225 | +} |
|---|
| 1226 | + |
|---|
| 1053 | 1227 | /* |
|---|
| 1054 | 1228 | * Test whether the free scanner has reached the same or lower pageblock than |
|---|
| 1055 | 1229 | * the migration scanner, and compaction should thus terminate. |
|---|
| .. | .. |
|---|
| 1058 | 1232 | { |
|---|
| 1059 | 1233 | return (cc->free_pfn >> pageblock_order) |
|---|
| 1060 | 1234 | <= (cc->migrate_pfn >> pageblock_order); |
|---|
| 1235 | +} |
|---|
| 1236 | + |
|---|
| 1237 | +/* |
|---|
| 1238 | + * Used when scanning for a suitable migration target which scans freelists |
|---|
| 1239 | + * in reverse. Reorders the list such as the unscanned pages are scanned |
|---|
| 1240 | + * first on the next iteration of the free scanner |
|---|
| 1241 | + */ |
|---|
| 1242 | +static void |
|---|
| 1243 | +move_freelist_head(struct list_head *freelist, struct page *freepage) |
|---|
| 1244 | +{ |
|---|
| 1245 | + LIST_HEAD(sublist); |
|---|
| 1246 | + |
|---|
| 1247 | + if (!list_is_last(freelist, &freepage->lru)) { |
|---|
| 1248 | + list_cut_before(&sublist, freelist, &freepage->lru); |
|---|
| 1249 | + if (!list_empty(&sublist)) |
|---|
| 1250 | + list_splice_tail(&sublist, freelist); |
|---|
| 1251 | + } |
|---|
| 1252 | +} |
|---|
| 1253 | + |
|---|
| 1254 | +/* |
|---|
| 1255 | + * Similar to move_freelist_head except used by the migration scanner |
|---|
| 1256 | + * when scanning forward. It's possible for these list operations to |
|---|
| 1257 | + * move against each other if they search the free list exactly in |
|---|
| 1258 | + * lockstep. |
|---|
| 1259 | + */ |
|---|
| 1260 | +static void |
|---|
| 1261 | +move_freelist_tail(struct list_head *freelist, struct page *freepage) |
|---|
| 1262 | +{ |
|---|
| 1263 | + LIST_HEAD(sublist); |
|---|
| 1264 | + |
|---|
| 1265 | + if (!list_is_first(freelist, &freepage->lru)) { |
|---|
| 1266 | + list_cut_position(&sublist, freelist, &freepage->lru); |
|---|
| 1267 | + if (!list_empty(&sublist)) |
|---|
| 1268 | + list_splice_tail(&sublist, freelist); |
|---|
| 1269 | + } |
|---|
| 1270 | +} |
|---|
| 1271 | + |
|---|
| 1272 | +static void |
|---|
| 1273 | +fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) |
|---|
| 1274 | +{ |
|---|
| 1275 | + unsigned long start_pfn, end_pfn; |
|---|
| 1276 | + struct page *page; |
|---|
| 1277 | + |
|---|
| 1278 | + /* Do not search around if there are enough pages already */ |
|---|
| 1279 | + if (cc->nr_freepages >= cc->nr_migratepages) |
|---|
| 1280 | + return; |
|---|
| 1281 | + |
|---|
| 1282 | + /* Minimise scanning during async compaction */ |
|---|
| 1283 | + if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) |
|---|
| 1284 | + return; |
|---|
| 1285 | + |
|---|
| 1286 | + /* Pageblock boundaries */ |
|---|
| 1287 | + start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); |
|---|
| 1288 | + end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); |
|---|
| 1289 | + |
|---|
| 1290 | + page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); |
|---|
| 1291 | + if (!page) |
|---|
| 1292 | + return; |
|---|
| 1293 | + |
|---|
| 1294 | + /* Scan before */ |
|---|
| 1295 | + if (start_pfn != pfn) { |
|---|
| 1296 | + isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false); |
|---|
| 1297 | + if (cc->nr_freepages >= cc->nr_migratepages) |
|---|
| 1298 | + return; |
|---|
| 1299 | + } |
|---|
| 1300 | + |
|---|
| 1301 | + /* Scan after */ |
|---|
| 1302 | + start_pfn = pfn + nr_isolated; |
|---|
| 1303 | + if (start_pfn < end_pfn) |
|---|
| 1304 | + isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false); |
|---|
| 1305 | + |
|---|
| 1306 | + /* Skip this pageblock in the future as it's full or nearly full */ |
|---|
| 1307 | + if (cc->nr_freepages < cc->nr_migratepages) |
|---|
| 1308 | + set_pageblock_skip(page); |
|---|
| 1309 | +} |
|---|
| 1310 | + |
|---|
| 1311 | +/* Search orders in round-robin fashion */ |
|---|
| 1312 | +static int next_search_order(struct compact_control *cc, int order) |
|---|
| 1313 | +{ |
|---|
| 1314 | + order--; |
|---|
| 1315 | + if (order < 0) |
|---|
| 1316 | + order = cc->order - 1; |
|---|
| 1317 | + |
|---|
| 1318 | + /* Search wrapped around? */ |
|---|
| 1319 | + if (order == cc->search_order) { |
|---|
| 1320 | + cc->search_order--; |
|---|
| 1321 | + if (cc->search_order < 0) |
|---|
| 1322 | + cc->search_order = cc->order - 1; |
|---|
| 1323 | + return -1; |
|---|
| 1324 | + } |
|---|
| 1325 | + |
|---|
| 1326 | + return order; |
|---|
| 1327 | +} |
|---|
| 1328 | + |
|---|
| 1329 | +static unsigned long |
|---|
| 1330 | +fast_isolate_freepages(struct compact_control *cc) |
|---|
| 1331 | +{ |
|---|
| 1332 | + unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1); |
|---|
| 1333 | + unsigned int nr_scanned = 0; |
|---|
| 1334 | + unsigned long low_pfn, min_pfn, highest = 0; |
|---|
| 1335 | + unsigned long nr_isolated = 0; |
|---|
| 1336 | + unsigned long distance; |
|---|
| 1337 | + struct page *page = NULL; |
|---|
| 1338 | + bool scan_start = false; |
|---|
| 1339 | + int order; |
|---|
| 1340 | + |
|---|
| 1341 | + /* Full compaction passes in a negative order */ |
|---|
| 1342 | + if (cc->order <= 0) |
|---|
| 1343 | + return cc->free_pfn; |
|---|
| 1344 | + |
|---|
| 1345 | + /* |
|---|
| 1346 | + * If starting the scan, use a deeper search and use the highest |
|---|
| 1347 | + * PFN found if a suitable one is not found. |
|---|
| 1348 | + */ |
|---|
| 1349 | + if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { |
|---|
| 1350 | + limit = pageblock_nr_pages >> 1; |
|---|
| 1351 | + scan_start = true; |
|---|
| 1352 | + } |
|---|
| 1353 | + |
|---|
| 1354 | + /* |
|---|
| 1355 | + * Preferred point is in the top quarter of the scan space but take |
|---|
| 1356 | + * a pfn from the top half if the search is problematic. |
|---|
| 1357 | + */ |
|---|
| 1358 | + distance = (cc->free_pfn - cc->migrate_pfn); |
|---|
| 1359 | + low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); |
|---|
| 1360 | + min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); |
|---|
| 1361 | + |
|---|
| 1362 | + if (WARN_ON_ONCE(min_pfn > low_pfn)) |
|---|
| 1363 | + low_pfn = min_pfn; |
|---|
| 1364 | + |
|---|
| 1365 | + /* |
|---|
| 1366 | + * Search starts from the last successful isolation order or the next |
|---|
| 1367 | + * order to search after a previous failure |
|---|
| 1368 | + */ |
|---|
| 1369 | + cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); |
|---|
| 1370 | + |
|---|
| 1371 | + for (order = cc->search_order; |
|---|
| 1372 | + !page && order >= 0; |
|---|
| 1373 | + order = next_search_order(cc, order)) { |
|---|
| 1374 | + struct free_area *area = &cc->zone->free_area[order]; |
|---|
| 1375 | + struct list_head *freelist; |
|---|
| 1376 | + struct page *freepage; |
|---|
| 1377 | + unsigned long flags; |
|---|
| 1378 | + unsigned int order_scanned = 0; |
|---|
| 1379 | + unsigned long high_pfn = 0; |
|---|
| 1380 | + |
|---|
| 1381 | + if (!area->nr_free) |
|---|
| 1382 | + continue; |
|---|
| 1383 | + |
|---|
| 1384 | + spin_lock_irqsave(&cc->zone->lock, flags); |
|---|
| 1385 | + freelist = &area->free_list[MIGRATE_MOVABLE]; |
|---|
| 1386 | + list_for_each_entry_reverse(freepage, freelist, lru) { |
|---|
| 1387 | + unsigned long pfn; |
|---|
| 1388 | + |
|---|
| 1389 | + order_scanned++; |
|---|
| 1390 | + nr_scanned++; |
|---|
| 1391 | + pfn = page_to_pfn(freepage); |
|---|
| 1392 | + |
|---|
| 1393 | + if (pfn >= highest) |
|---|
| 1394 | + highest = max(pageblock_start_pfn(pfn), |
|---|
| 1395 | + cc->zone->zone_start_pfn); |
|---|
| 1396 | + |
|---|
| 1397 | + if (pfn >= low_pfn) { |
|---|
| 1398 | + cc->fast_search_fail = 0; |
|---|
| 1399 | + cc->search_order = order; |
|---|
| 1400 | + page = freepage; |
|---|
| 1401 | + break; |
|---|
| 1402 | + } |
|---|
| 1403 | + |
|---|
| 1404 | + if (pfn >= min_pfn && pfn > high_pfn) { |
|---|
| 1405 | + high_pfn = pfn; |
|---|
| 1406 | + |
|---|
| 1407 | + /* Shorten the scan if a candidate is found */ |
|---|
| 1408 | + limit >>= 1; |
|---|
| 1409 | + } |
|---|
| 1410 | + |
|---|
| 1411 | + if (order_scanned >= limit) |
|---|
| 1412 | + break; |
|---|
| 1413 | + } |
|---|
| 1414 | + |
|---|
| 1415 | + /* Use a minimum pfn if a preferred one was not found */ |
|---|
| 1416 | + if (!page && high_pfn) { |
|---|
| 1417 | + page = pfn_to_page(high_pfn); |
|---|
| 1418 | + |
|---|
| 1419 | + /* Update freepage for the list reorder below */ |
|---|
| 1420 | + freepage = page; |
|---|
| 1421 | + } |
|---|
| 1422 | + |
|---|
| 1423 | + /* Reorder to so a future search skips recent pages */ |
|---|
| 1424 | + move_freelist_head(freelist, freepage); |
|---|
| 1425 | + |
|---|
| 1426 | + /* Isolate the page if available */ |
|---|
| 1427 | + if (page) { |
|---|
| 1428 | + if (__isolate_free_page(page, order)) { |
|---|
| 1429 | + set_page_private(page, order); |
|---|
| 1430 | + nr_isolated = 1 << order; |
|---|
| 1431 | + cc->nr_freepages += nr_isolated; |
|---|
| 1432 | + list_add_tail(&page->lru, &cc->freepages); |
|---|
| 1433 | + count_compact_events(COMPACTISOLATED, nr_isolated); |
|---|
| 1434 | + } else { |
|---|
| 1435 | + /* If isolation fails, abort the search */ |
|---|
| 1436 | + order = cc->search_order + 1; |
|---|
| 1437 | + page = NULL; |
|---|
| 1438 | + } |
|---|
| 1439 | + } |
|---|
| 1440 | + |
|---|
| 1441 | + spin_unlock_irqrestore(&cc->zone->lock, flags); |
|---|
| 1442 | + |
|---|
| 1443 | + /* |
|---|
| 1444 | + * Smaller scan on next order so the total scan ig related |
|---|
| 1445 | + * to freelist_scan_limit. |
|---|
| 1446 | + */ |
|---|
| 1447 | + if (order_scanned >= limit) |
|---|
| 1448 | + limit = min(1U, limit >> 1); |
|---|
| 1449 | + } |
|---|
| 1450 | + |
|---|
| 1451 | + if (!page) { |
|---|
| 1452 | + cc->fast_search_fail++; |
|---|
| 1453 | + if (scan_start) { |
|---|
| 1454 | + /* |
|---|
| 1455 | + * Use the highest PFN found above min. If one was |
|---|
| 1456 | + * not found, be pessimistic for direct compaction |
|---|
| 1457 | + * and use the min mark. |
|---|
| 1458 | + */ |
|---|
| 1459 | + if (highest) { |
|---|
| 1460 | + page = pfn_to_page(highest); |
|---|
| 1461 | + cc->free_pfn = highest; |
|---|
| 1462 | + } else { |
|---|
| 1463 | + if (cc->direct_compaction && pfn_valid(min_pfn)) { |
|---|
| 1464 | + page = pageblock_pfn_to_page(min_pfn, |
|---|
| 1465 | + min(pageblock_end_pfn(min_pfn), |
|---|
| 1466 | + zone_end_pfn(cc->zone)), |
|---|
| 1467 | + cc->zone); |
|---|
| 1468 | + cc->free_pfn = min_pfn; |
|---|
| 1469 | + } |
|---|
| 1470 | + } |
|---|
| 1471 | + } |
|---|
| 1472 | + } |
|---|
| 1473 | + |
|---|
| 1474 | + if (highest && highest >= cc->zone->compact_cached_free_pfn) { |
|---|
| 1475 | + highest -= pageblock_nr_pages; |
|---|
| 1476 | + cc->zone->compact_cached_free_pfn = highest; |
|---|
| 1477 | + } |
|---|
| 1478 | + |
|---|
| 1479 | + cc->total_free_scanned += nr_scanned; |
|---|
| 1480 | + if (!page) |
|---|
| 1481 | + return cc->free_pfn; |
|---|
| 1482 | + |
|---|
| 1483 | + low_pfn = page_to_pfn(page); |
|---|
| 1484 | + fast_isolate_around(cc, low_pfn, nr_isolated); |
|---|
| 1485 | + return low_pfn; |
|---|
| 1061 | 1486 | } |
|---|
| 1062 | 1487 | |
|---|
| 1063 | 1488 | /* |
|---|
| .. | .. |
|---|
| 1073 | 1498 | unsigned long block_end_pfn; /* end of current pageblock */ |
|---|
| 1074 | 1499 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
|---|
| 1075 | 1500 | struct list_head *freelist = &cc->freepages; |
|---|
| 1501 | + unsigned int stride; |
|---|
| 1502 | + |
|---|
| 1503 | + /* Try a small search of the free lists for a candidate */ |
|---|
| 1504 | + isolate_start_pfn = fast_isolate_freepages(cc); |
|---|
| 1505 | + if (cc->nr_freepages) |
|---|
| 1506 | + goto splitmap; |
|---|
| 1076 | 1507 | |
|---|
| 1077 | 1508 | /* |
|---|
| 1078 | 1509 | * Initialise the free scanner. The starting point is where we last |
|---|
| .. | .. |
|---|
| 1081 | 1512 | * this pfn aligned down to the pageblock boundary, because we do |
|---|
| 1082 | 1513 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
|---|
| 1083 | 1514 | * For ending point, take care when isolating in last pageblock of a |
|---|
| 1084 | | - * a zone which ends in the middle of a pageblock. |
|---|
| 1515 | + * zone which ends in the middle of a pageblock. |
|---|
| 1085 | 1516 | * The low boundary is the end of the pageblock the migration scanner |
|---|
| 1086 | 1517 | * is using. |
|---|
| 1087 | 1518 | */ |
|---|
| 1088 | 1519 | isolate_start_pfn = cc->free_pfn; |
|---|
| 1089 | | - block_start_pfn = pageblock_start_pfn(cc->free_pfn); |
|---|
| 1520 | + block_start_pfn = pageblock_start_pfn(isolate_start_pfn); |
|---|
| 1090 | 1521 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
|---|
| 1091 | 1522 | zone_end_pfn(zone)); |
|---|
| 1092 | 1523 | low_pfn = pageblock_end_pfn(cc->migrate_pfn); |
|---|
| 1524 | + stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; |
|---|
| 1093 | 1525 | |
|---|
| 1094 | 1526 | /* |
|---|
| 1095 | 1527 | * Isolate free pages until enough are available to migrate the |
|---|
| .. | .. |
|---|
| 1100 | 1532 | block_end_pfn = block_start_pfn, |
|---|
| 1101 | 1533 | block_start_pfn -= pageblock_nr_pages, |
|---|
| 1102 | 1534 | isolate_start_pfn = block_start_pfn) { |
|---|
| 1535 | + unsigned long nr_isolated; |
|---|
| 1536 | + |
|---|
| 1103 | 1537 | /* |
|---|
| 1104 | 1538 | * This can iterate a massively long zone without finding any |
|---|
| 1105 | | - * suitable migration targets, so periodically check if we need |
|---|
| 1106 | | - * to schedule, or even abort async compaction. |
|---|
| 1539 | + * suitable migration targets, so periodically check resched. |
|---|
| 1107 | 1540 | */ |
|---|
| 1108 | | - if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
|---|
| 1109 | | - && compact_should_abort(cc)) |
|---|
| 1110 | | - break; |
|---|
| 1541 | + if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) |
|---|
| 1542 | + cond_resched(); |
|---|
| 1111 | 1543 | |
|---|
| 1112 | 1544 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
|---|
| 1113 | 1545 | zone); |
|---|
| .. | .. |
|---|
| 1123 | 1555 | continue; |
|---|
| 1124 | 1556 | |
|---|
| 1125 | 1557 | /* Found a block suitable for isolating free pages from. */ |
|---|
| 1126 | | - isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn, |
|---|
| 1127 | | - freelist, false); |
|---|
| 1558 | + nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
|---|
| 1559 | + block_end_pfn, freelist, stride, false); |
|---|
| 1128 | 1560 | |
|---|
| 1129 | | - /* |
|---|
| 1130 | | - * If we isolated enough freepages, or aborted due to lock |
|---|
| 1131 | | - * contention, terminate. |
|---|
| 1132 | | - */ |
|---|
| 1133 | | - if ((cc->nr_freepages >= cc->nr_migratepages) |
|---|
| 1134 | | - || cc->contended) { |
|---|
| 1561 | + /* Update the skip hint if the full pageblock was scanned */ |
|---|
| 1562 | + if (isolate_start_pfn == block_end_pfn) |
|---|
| 1563 | + update_pageblock_skip(cc, page, block_start_pfn); |
|---|
| 1564 | + |
|---|
| 1565 | + /* Are enough freepages isolated? */ |
|---|
| 1566 | + if (cc->nr_freepages >= cc->nr_migratepages) { |
|---|
| 1135 | 1567 | if (isolate_start_pfn >= block_end_pfn) { |
|---|
| 1136 | 1568 | /* |
|---|
| 1137 | 1569 | * Restart at previous pageblock if more |
|---|
| .. | .. |
|---|
| 1148 | 1580 | */ |
|---|
| 1149 | 1581 | break; |
|---|
| 1150 | 1582 | } |
|---|
| 1151 | | - } |
|---|
| 1152 | 1583 | |
|---|
| 1153 | | - /* __isolate_free_page() does not map the pages */ |
|---|
| 1154 | | - map_pages(freelist); |
|---|
| 1584 | + /* Adjust stride depending on isolation */ |
|---|
| 1585 | + if (nr_isolated) { |
|---|
| 1586 | + stride = 1; |
|---|
| 1587 | + continue; |
|---|
| 1588 | + } |
|---|
| 1589 | + stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); |
|---|
| 1590 | + } |
|---|
| 1155 | 1591 | |
|---|
| 1156 | 1592 | /* |
|---|
| 1157 | 1593 | * Record where the free scanner will restart next time. Either we |
|---|
| .. | .. |
|---|
| 1160 | 1596 | * and the loop terminated due to isolate_start_pfn < low_pfn |
|---|
| 1161 | 1597 | */ |
|---|
| 1162 | 1598 | cc->free_pfn = isolate_start_pfn; |
|---|
| 1599 | + |
|---|
| 1600 | +splitmap: |
|---|
| 1601 | + /* __isolate_free_page() does not map the pages */ |
|---|
| 1602 | + split_map_pages(freelist); |
|---|
| 1163 | 1603 | } |
|---|
| 1164 | 1604 | |
|---|
| 1165 | 1605 | /* |
|---|
| .. | .. |
|---|
| 1172 | 1612 | struct compact_control *cc = (struct compact_control *)data; |
|---|
| 1173 | 1613 | struct page *freepage; |
|---|
| 1174 | 1614 | |
|---|
| 1175 | | - /* |
|---|
| 1176 | | - * Isolate free pages if necessary, and if we are not aborting due to |
|---|
| 1177 | | - * contention. |
|---|
| 1178 | | - */ |
|---|
| 1179 | 1615 | if (list_empty(&cc->freepages)) { |
|---|
| 1180 | | - if (!cc->contended) |
|---|
| 1181 | | - isolate_freepages(cc); |
|---|
| 1616 | + isolate_freepages(cc); |
|---|
| 1182 | 1617 | |
|---|
| 1183 | 1618 | if (list_empty(&cc->freepages)) |
|---|
| 1184 | 1619 | return NULL; |
|---|
| .. | .. |
|---|
| 1215 | 1650 | * Allow userspace to control policy on scanning the unevictable LRU for |
|---|
| 1216 | 1651 | * compactable pages. |
|---|
| 1217 | 1652 | */ |
|---|
| 1653 | +#ifdef CONFIG_PREEMPT_RT |
|---|
| 1654 | +int sysctl_compact_unevictable_allowed __read_mostly = 0; |
|---|
| 1655 | +#else |
|---|
| 1218 | 1656 | int sysctl_compact_unevictable_allowed __read_mostly = 1; |
|---|
| 1657 | +#endif |
|---|
| 1658 | + |
|---|
| 1659 | +static inline void |
|---|
| 1660 | +update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) |
|---|
| 1661 | +{ |
|---|
| 1662 | + if (cc->fast_start_pfn == ULONG_MAX) |
|---|
| 1663 | + return; |
|---|
| 1664 | + |
|---|
| 1665 | + if (!cc->fast_start_pfn) |
|---|
| 1666 | + cc->fast_start_pfn = pfn; |
|---|
| 1667 | + |
|---|
| 1668 | + cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); |
|---|
| 1669 | +} |
|---|
| 1670 | + |
|---|
| 1671 | +static inline unsigned long |
|---|
| 1672 | +reinit_migrate_pfn(struct compact_control *cc) |
|---|
| 1673 | +{ |
|---|
| 1674 | + if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) |
|---|
| 1675 | + return cc->migrate_pfn; |
|---|
| 1676 | + |
|---|
| 1677 | + cc->migrate_pfn = cc->fast_start_pfn; |
|---|
| 1678 | + cc->fast_start_pfn = ULONG_MAX; |
|---|
| 1679 | + |
|---|
| 1680 | + return cc->migrate_pfn; |
|---|
| 1681 | +} |
|---|
| 1682 | + |
|---|
| 1683 | +/* |
|---|
| 1684 | + * Briefly search the free lists for a migration source that already has |
|---|
| 1685 | + * some free pages to reduce the number of pages that need migration |
|---|
| 1686 | + * before a pageblock is free. |
|---|
| 1687 | + */ |
|---|
| 1688 | +static unsigned long fast_find_migrateblock(struct compact_control *cc) |
|---|
| 1689 | +{ |
|---|
| 1690 | + unsigned int limit = freelist_scan_limit(cc); |
|---|
| 1691 | + unsigned int nr_scanned = 0; |
|---|
| 1692 | + unsigned long distance; |
|---|
| 1693 | + unsigned long pfn = cc->migrate_pfn; |
|---|
| 1694 | + unsigned long high_pfn; |
|---|
| 1695 | + int order; |
|---|
| 1696 | + bool found_block = false; |
|---|
| 1697 | + |
|---|
| 1698 | + /* Skip hints are relied on to avoid repeats on the fast search */ |
|---|
| 1699 | + if (cc->ignore_skip_hint) |
|---|
| 1700 | + return pfn; |
|---|
| 1701 | + |
|---|
| 1702 | + /* |
|---|
| 1703 | + * If the migrate_pfn is not at the start of a zone or the start |
|---|
| 1704 | + * of a pageblock then assume this is a continuation of a previous |
|---|
| 1705 | + * scan restarted due to COMPACT_CLUSTER_MAX. |
|---|
| 1706 | + */ |
|---|
| 1707 | + if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) |
|---|
| 1708 | + return pfn; |
|---|
| 1709 | + |
|---|
| 1710 | + /* |
|---|
| 1711 | + * For smaller orders, just linearly scan as the number of pages |
|---|
| 1712 | + * to migrate should be relatively small and does not necessarily |
|---|
| 1713 | + * justify freeing up a large block for a small allocation. |
|---|
| 1714 | + */ |
|---|
| 1715 | + if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) |
|---|
| 1716 | + return pfn; |
|---|
| 1717 | + |
|---|
| 1718 | + /* |
|---|
| 1719 | + * Only allow kcompactd and direct requests for movable pages to |
|---|
| 1720 | + * quickly clear out a MOVABLE pageblock for allocation. This |
|---|
| 1721 | + * reduces the risk that a large movable pageblock is freed for |
|---|
| 1722 | + * an unmovable/reclaimable small allocation. |
|---|
| 1723 | + */ |
|---|
| 1724 | + if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) |
|---|
| 1725 | + return pfn; |
|---|
| 1726 | + |
|---|
| 1727 | + /* |
|---|
| 1728 | + * When starting the migration scanner, pick any pageblock within the |
|---|
| 1729 | + * first half of the search space. Otherwise try and pick a pageblock |
|---|
| 1730 | + * within the first eighth to reduce the chances that a migration |
|---|
| 1731 | + * target later becomes a source. |
|---|
| 1732 | + */ |
|---|
| 1733 | + distance = (cc->free_pfn - cc->migrate_pfn) >> 1; |
|---|
| 1734 | + if (cc->migrate_pfn != cc->zone->zone_start_pfn) |
|---|
| 1735 | + distance >>= 2; |
|---|
| 1736 | + high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); |
|---|
| 1737 | + |
|---|
| 1738 | + for (order = cc->order - 1; |
|---|
| 1739 | + order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; |
|---|
| 1740 | + order--) { |
|---|
| 1741 | + struct free_area *area = &cc->zone->free_area[order]; |
|---|
| 1742 | + struct list_head *freelist; |
|---|
| 1743 | + unsigned long flags; |
|---|
| 1744 | + struct page *freepage; |
|---|
| 1745 | + |
|---|
| 1746 | + if (!area->nr_free) |
|---|
| 1747 | + continue; |
|---|
| 1748 | + |
|---|
| 1749 | + spin_lock_irqsave(&cc->zone->lock, flags); |
|---|
| 1750 | + freelist = &area->free_list[MIGRATE_MOVABLE]; |
|---|
| 1751 | + list_for_each_entry(freepage, freelist, lru) { |
|---|
| 1752 | + unsigned long free_pfn; |
|---|
| 1753 | + |
|---|
| 1754 | + if (nr_scanned++ >= limit) { |
|---|
| 1755 | + move_freelist_tail(freelist, freepage); |
|---|
| 1756 | + break; |
|---|
| 1757 | + } |
|---|
| 1758 | + |
|---|
| 1759 | + free_pfn = page_to_pfn(freepage); |
|---|
| 1760 | + if (free_pfn < high_pfn) { |
|---|
| 1761 | + /* |
|---|
| 1762 | + * Avoid if skipped recently. Ideally it would |
|---|
| 1763 | + * move to the tail but even safe iteration of |
|---|
| 1764 | + * the list assumes an entry is deleted, not |
|---|
| 1765 | + * reordered. |
|---|
| 1766 | + */ |
|---|
| 1767 | + if (get_pageblock_skip(freepage)) |
|---|
| 1768 | + continue; |
|---|
| 1769 | + |
|---|
| 1770 | + /* Reorder to so a future search skips recent pages */ |
|---|
| 1771 | + move_freelist_tail(freelist, freepage); |
|---|
| 1772 | + |
|---|
| 1773 | + update_fast_start_pfn(cc, free_pfn); |
|---|
| 1774 | + pfn = pageblock_start_pfn(free_pfn); |
|---|
| 1775 | + if (pfn < cc->zone->zone_start_pfn) |
|---|
| 1776 | + pfn = cc->zone->zone_start_pfn; |
|---|
| 1777 | + cc->fast_search_fail = 0; |
|---|
| 1778 | + found_block = true; |
|---|
| 1779 | + set_pageblock_skip(freepage); |
|---|
| 1780 | + break; |
|---|
| 1781 | + } |
|---|
| 1782 | + } |
|---|
| 1783 | + spin_unlock_irqrestore(&cc->zone->lock, flags); |
|---|
| 1784 | + } |
|---|
| 1785 | + |
|---|
| 1786 | + cc->total_migrate_scanned += nr_scanned; |
|---|
| 1787 | + |
|---|
| 1788 | + /* |
|---|
| 1789 | + * If fast scanning failed then use a cached entry for a page block |
|---|
| 1790 | + * that had free pages as the basis for starting a linear scan. |
|---|
| 1791 | + */ |
|---|
| 1792 | + if (!found_block) { |
|---|
| 1793 | + cc->fast_search_fail++; |
|---|
| 1794 | + pfn = reinit_migrate_pfn(cc); |
|---|
| 1795 | + } |
|---|
| 1796 | + return pfn; |
|---|
| 1797 | +} |
|---|
| 1219 | 1798 | |
|---|
| 1220 | 1799 | /* |
|---|
| 1221 | 1800 | * Isolate all pages that can be migrated from the first suitable block, |
|---|
| 1222 | 1801 | * starting at the block pointed to by the migrate scanner pfn within |
|---|
| 1223 | 1802 | * compact_control. |
|---|
| 1224 | 1803 | */ |
|---|
| 1225 | | -static isolate_migrate_t isolate_migratepages(struct zone *zone, |
|---|
| 1226 | | - struct compact_control *cc) |
|---|
| 1804 | +static isolate_migrate_t isolate_migratepages(struct compact_control *cc) |
|---|
| 1227 | 1805 | { |
|---|
| 1228 | 1806 | unsigned long block_start_pfn; |
|---|
| 1229 | 1807 | unsigned long block_end_pfn; |
|---|
| .. | .. |
|---|
| 1232 | 1810 | const isolate_mode_t isolate_mode = |
|---|
| 1233 | 1811 | (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | |
|---|
| 1234 | 1812 | (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
|---|
| 1813 | + bool fast_find_block; |
|---|
| 1235 | 1814 | |
|---|
| 1236 | 1815 | /* |
|---|
| 1237 | 1816 | * Start at where we last stopped, or beginning of the zone as |
|---|
| 1238 | | - * initialized by compact_zone() |
|---|
| 1817 | + * initialized by compact_zone(). The first failure will use |
|---|
| 1818 | + * the lowest PFN as the starting point for linear scanning. |
|---|
| 1239 | 1819 | */ |
|---|
| 1240 | | - low_pfn = cc->migrate_pfn; |
|---|
| 1820 | + low_pfn = fast_find_migrateblock(cc); |
|---|
| 1241 | 1821 | block_start_pfn = pageblock_start_pfn(low_pfn); |
|---|
| 1242 | | - if (block_start_pfn < zone->zone_start_pfn) |
|---|
| 1243 | | - block_start_pfn = zone->zone_start_pfn; |
|---|
| 1822 | + if (block_start_pfn < cc->zone->zone_start_pfn) |
|---|
| 1823 | + block_start_pfn = cc->zone->zone_start_pfn; |
|---|
| 1824 | + |
|---|
| 1825 | + /* |
|---|
| 1826 | + * fast_find_migrateblock marks a pageblock skipped so to avoid |
|---|
| 1827 | + * the isolation_suitable check below, check whether the fast |
|---|
| 1828 | + * search was successful. |
|---|
| 1829 | + */ |
|---|
| 1830 | + fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; |
|---|
| 1244 | 1831 | |
|---|
| 1245 | 1832 | /* Only scan within a pageblock boundary */ |
|---|
| 1246 | 1833 | block_end_pfn = pageblock_end_pfn(low_pfn); |
|---|
| .. | .. |
|---|
| 1250 | 1837 | * Do not cross the free scanner. |
|---|
| 1251 | 1838 | */ |
|---|
| 1252 | 1839 | for (; block_end_pfn <= cc->free_pfn; |
|---|
| 1840 | + fast_find_block = false, |
|---|
| 1253 | 1841 | low_pfn = block_end_pfn, |
|---|
| 1254 | 1842 | block_start_pfn = block_end_pfn, |
|---|
| 1255 | 1843 | block_end_pfn += pageblock_nr_pages) { |
|---|
| .. | .. |
|---|
| 1257 | 1845 | /* |
|---|
| 1258 | 1846 | * This can potentially iterate a massively long zone with |
|---|
| 1259 | 1847 | * many pageblocks unsuitable, so periodically check if we |
|---|
| 1260 | | - * need to schedule, or even abort async compaction. |
|---|
| 1848 | + * need to schedule. |
|---|
| 1261 | 1849 | */ |
|---|
| 1262 | | - if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)) |
|---|
| 1263 | | - && compact_should_abort(cc)) |
|---|
| 1264 | | - break; |
|---|
| 1850 | + if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))) |
|---|
| 1851 | + cond_resched(); |
|---|
| 1265 | 1852 | |
|---|
| 1266 | | - page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
|---|
| 1267 | | - zone); |
|---|
| 1853 | + page = pageblock_pfn_to_page(block_start_pfn, |
|---|
| 1854 | + block_end_pfn, cc->zone); |
|---|
| 1268 | 1855 | if (!page) |
|---|
| 1269 | 1856 | continue; |
|---|
| 1270 | 1857 | |
|---|
| 1271 | | - /* If isolation recently failed, do not retry */ |
|---|
| 1272 | | - if (!isolation_suitable(cc, page)) |
|---|
| 1858 | + /* |
|---|
| 1859 | + * If isolation recently failed, do not retry. Only check the |
|---|
| 1860 | + * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock |
|---|
| 1861 | + * to be visited multiple times. Assume skip was checked |
|---|
| 1862 | + * before making it "skip" so other compaction instances do |
|---|
| 1863 | + * not scan the same block. |
|---|
| 1864 | + */ |
|---|
| 1865 | + if (IS_ALIGNED(low_pfn, pageblock_nr_pages) && |
|---|
| 1866 | + !fast_find_block && !isolation_suitable(cc, page)) |
|---|
| 1273 | 1867 | continue; |
|---|
| 1274 | 1868 | |
|---|
| 1275 | 1869 | /* |
|---|
| 1276 | | - * For async compaction, also only scan in MOVABLE blocks. |
|---|
| 1277 | | - * Async compaction is optimistic to see if the minimum amount |
|---|
| 1278 | | - * of work satisfies the allocation. |
|---|
| 1870 | + * For async compaction, also only scan in MOVABLE blocks |
|---|
| 1871 | + * without huge pages. Async compaction is optimistic to see |
|---|
| 1872 | + * if the minimum amount of work satisfies the allocation. |
|---|
| 1873 | + * The cached PFN is updated as it's possible that all |
|---|
| 1874 | + * remaining blocks between source and target are unsuitable |
|---|
| 1875 | + * and the compaction scanners fail to meet. |
|---|
| 1279 | 1876 | */ |
|---|
| 1280 | | - if (!suitable_migration_source(cc, page)) |
|---|
| 1877 | + if (!suitable_migration_source(cc, page)) { |
|---|
| 1878 | + update_cached_migrate(cc, block_end_pfn); |
|---|
| 1281 | 1879 | continue; |
|---|
| 1880 | + } |
|---|
| 1282 | 1881 | |
|---|
| 1283 | 1882 | /* Perform the isolation */ |
|---|
| 1284 | 1883 | low_pfn = isolate_migratepages_block(cc, low_pfn, |
|---|
| 1285 | 1884 | block_end_pfn, isolate_mode); |
|---|
| 1286 | 1885 | |
|---|
| 1287 | | - if (!low_pfn || cc->contended) |
|---|
| 1886 | + if (!low_pfn) |
|---|
| 1288 | 1887 | return ISOLATE_ABORT; |
|---|
| 1289 | 1888 | |
|---|
| 1290 | 1889 | /* |
|---|
| .. | .. |
|---|
| 1310 | 1909 | return order == -1; |
|---|
| 1311 | 1910 | } |
|---|
| 1312 | 1911 | |
|---|
| 1313 | | -static enum compact_result __compact_finished(struct zone *zone, |
|---|
| 1314 | | - struct compact_control *cc) |
|---|
| 1912 | +static bool kswapd_is_running(pg_data_t *pgdat) |
|---|
| 1913 | +{ |
|---|
| 1914 | + return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING); |
|---|
| 1915 | +} |
|---|
| 1916 | + |
|---|
| 1917 | +/* |
|---|
| 1918 | + * A zone's fragmentation score is the external fragmentation wrt to the |
|---|
| 1919 | + * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. |
|---|
| 1920 | + */ |
|---|
| 1921 | +static unsigned int fragmentation_score_zone(struct zone *zone) |
|---|
| 1922 | +{ |
|---|
| 1923 | + return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); |
|---|
| 1924 | +} |
|---|
| 1925 | + |
|---|
| 1926 | +/* |
|---|
| 1927 | + * A weighted zone's fragmentation score is the external fragmentation |
|---|
| 1928 | + * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It |
|---|
| 1929 | + * returns a value in the range [0, 100]. |
|---|
| 1930 | + * |
|---|
| 1931 | + * The scaling factor ensures that proactive compaction focuses on larger |
|---|
| 1932 | + * zones like ZONE_NORMAL, rather than smaller, specialized zones like |
|---|
| 1933 | + * ZONE_DMA32. For smaller zones, the score value remains close to zero, |
|---|
| 1934 | + * and thus never exceeds the high threshold for proactive compaction. |
|---|
| 1935 | + */ |
|---|
| 1936 | +static unsigned int fragmentation_score_zone_weighted(struct zone *zone) |
|---|
| 1937 | +{ |
|---|
| 1938 | + unsigned long score; |
|---|
| 1939 | + |
|---|
| 1940 | + score = zone->present_pages * fragmentation_score_zone(zone); |
|---|
| 1941 | + return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); |
|---|
| 1942 | +} |
|---|
| 1943 | + |
|---|
| 1944 | +/* |
|---|
| 1945 | + * The per-node proactive (background) compaction process is started by its |
|---|
| 1946 | + * corresponding kcompactd thread when the node's fragmentation score |
|---|
| 1947 | + * exceeds the high threshold. The compaction process remains active till |
|---|
| 1948 | + * the node's score falls below the low threshold, or one of the back-off |
|---|
| 1949 | + * conditions is met. |
|---|
| 1950 | + */ |
|---|
| 1951 | +static unsigned int fragmentation_score_node(pg_data_t *pgdat) |
|---|
| 1952 | +{ |
|---|
| 1953 | + unsigned int score = 0; |
|---|
| 1954 | + int zoneid; |
|---|
| 1955 | + |
|---|
| 1956 | + for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
|---|
| 1957 | + struct zone *zone; |
|---|
| 1958 | + |
|---|
| 1959 | + zone = &pgdat->node_zones[zoneid]; |
|---|
| 1960 | + score += fragmentation_score_zone_weighted(zone); |
|---|
| 1961 | + } |
|---|
| 1962 | + |
|---|
| 1963 | + return score; |
|---|
| 1964 | +} |
|---|
| 1965 | + |
|---|
| 1966 | +static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low) |
|---|
| 1967 | +{ |
|---|
| 1968 | + unsigned int wmark_low; |
|---|
| 1969 | + |
|---|
| 1970 | + /* |
|---|
| 1971 | + * Cap the low watermak to avoid excessive compaction |
|---|
| 1972 | + * activity in case a user sets the proactivess tunable |
|---|
| 1973 | + * close to 100 (maximum). |
|---|
| 1974 | + */ |
|---|
| 1975 | + wmark_low = max(100U - sysctl_compaction_proactiveness, 5U); |
|---|
| 1976 | + return low ? wmark_low : min(wmark_low + 10, 100U); |
|---|
| 1977 | +} |
|---|
| 1978 | + |
|---|
| 1979 | +static bool should_proactive_compact_node(pg_data_t *pgdat) |
|---|
| 1980 | +{ |
|---|
| 1981 | + int wmark_high; |
|---|
| 1982 | + |
|---|
| 1983 | + if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) |
|---|
| 1984 | + return false; |
|---|
| 1985 | + |
|---|
| 1986 | + wmark_high = fragmentation_score_wmark(pgdat, false); |
|---|
| 1987 | + return fragmentation_score_node(pgdat) > wmark_high; |
|---|
| 1988 | +} |
|---|
| 1989 | + |
|---|
| 1990 | +static enum compact_result __compact_finished(struct compact_control *cc) |
|---|
| 1315 | 1991 | { |
|---|
| 1316 | 1992 | unsigned int order; |
|---|
| 1317 | 1993 | const int migratetype = cc->migratetype; |
|---|
| 1318 | | - |
|---|
| 1319 | | - if (cc->contended || fatal_signal_pending(current)) |
|---|
| 1320 | | - return COMPACT_CONTENDED; |
|---|
| 1994 | + int ret; |
|---|
| 1321 | 1995 | |
|---|
| 1322 | 1996 | /* Compaction run completes if the migrate and free scanner meet */ |
|---|
| 1323 | 1997 | if (compact_scanners_met(cc)) { |
|---|
| 1324 | 1998 | /* Let the next compaction start anew. */ |
|---|
| 1325 | | - reset_cached_positions(zone); |
|---|
| 1999 | + reset_cached_positions(cc->zone); |
|---|
| 1326 | 2000 | |
|---|
| 1327 | 2001 | /* |
|---|
| 1328 | 2002 | * Mark that the PG_migrate_skip information should be cleared |
|---|
| .. | .. |
|---|
| 1331 | 2005 | * based on an allocation request. |
|---|
| 1332 | 2006 | */ |
|---|
| 1333 | 2007 | if (cc->direct_compaction) |
|---|
| 1334 | | - zone->compact_blockskip_flush = true; |
|---|
| 2008 | + cc->zone->compact_blockskip_flush = true; |
|---|
| 1335 | 2009 | |
|---|
| 1336 | 2010 | if (cc->whole_zone) |
|---|
| 1337 | 2011 | return COMPACT_COMPLETE; |
|---|
| .. | .. |
|---|
| 1339 | 2013 | return COMPACT_PARTIAL_SKIPPED; |
|---|
| 1340 | 2014 | } |
|---|
| 1341 | 2015 | |
|---|
| 2016 | + if (cc->proactive_compaction) { |
|---|
| 2017 | + int score, wmark_low; |
|---|
| 2018 | + pg_data_t *pgdat; |
|---|
| 2019 | + |
|---|
| 2020 | + pgdat = cc->zone->zone_pgdat; |
|---|
| 2021 | + if (kswapd_is_running(pgdat)) |
|---|
| 2022 | + return COMPACT_PARTIAL_SKIPPED; |
|---|
| 2023 | + |
|---|
| 2024 | + score = fragmentation_score_zone(cc->zone); |
|---|
| 2025 | + wmark_low = fragmentation_score_wmark(pgdat, true); |
|---|
| 2026 | + |
|---|
| 2027 | + if (score > wmark_low) |
|---|
| 2028 | + ret = COMPACT_CONTINUE; |
|---|
| 2029 | + else |
|---|
| 2030 | + ret = COMPACT_SUCCESS; |
|---|
| 2031 | + |
|---|
| 2032 | + goto out; |
|---|
| 2033 | + } |
|---|
| 2034 | + |
|---|
| 1342 | 2035 | if (is_via_compact_memory(cc->order)) |
|---|
| 1343 | 2036 | return COMPACT_CONTINUE; |
|---|
| 1344 | 2037 | |
|---|
| 1345 | | - if (cc->finishing_block) { |
|---|
| 1346 | | - /* |
|---|
| 1347 | | - * We have finished the pageblock, but better check again that |
|---|
| 1348 | | - * we really succeeded. |
|---|
| 1349 | | - */ |
|---|
| 1350 | | - if (IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) |
|---|
| 1351 | | - cc->finishing_block = false; |
|---|
| 1352 | | - else |
|---|
| 1353 | | - return COMPACT_CONTINUE; |
|---|
| 1354 | | - } |
|---|
| 2038 | + /* |
|---|
| 2039 | + * Always finish scanning a pageblock to reduce the possibility of |
|---|
| 2040 | + * fallbacks in the future. This is particularly important when |
|---|
| 2041 | + * migration source is unmovable/reclaimable but it's not worth |
|---|
| 2042 | + * special casing. |
|---|
| 2043 | + */ |
|---|
| 2044 | + if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages)) |
|---|
| 2045 | + return COMPACT_CONTINUE; |
|---|
| 1355 | 2046 | |
|---|
| 1356 | 2047 | /* Direct compactor: Is a suitable page free? */ |
|---|
| 2048 | + ret = COMPACT_NO_SUITABLE_PAGE; |
|---|
| 1357 | 2049 | for (order = cc->order; order < MAX_ORDER; order++) { |
|---|
| 1358 | | - struct free_area *area = &zone->free_area[order]; |
|---|
| 2050 | + struct free_area *area = &cc->zone->free_area[order]; |
|---|
| 1359 | 2051 | bool can_steal; |
|---|
| 1360 | 2052 | |
|---|
| 1361 | 2053 | /* Job done if page is free of the right migratetype */ |
|---|
| 1362 | | - if (!list_empty(&area->free_list[migratetype])) |
|---|
| 2054 | + if (!free_area_empty(area, migratetype)) |
|---|
| 1363 | 2055 | return COMPACT_SUCCESS; |
|---|
| 1364 | 2056 | |
|---|
| 1365 | 2057 | #ifdef CONFIG_CMA |
|---|
| 1366 | 2058 | /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ |
|---|
| 1367 | 2059 | if (migratetype == MIGRATE_MOVABLE && |
|---|
| 1368 | | - !list_empty(&area->free_list[MIGRATE_CMA])) |
|---|
| 2060 | + !free_area_empty(area, MIGRATE_CMA)) |
|---|
| 1369 | 2061 | return COMPACT_SUCCESS; |
|---|
| 1370 | 2062 | #endif |
|---|
| 1371 | 2063 | /* |
|---|
| .. | .. |
|---|
| 1393 | 2085 | return COMPACT_SUCCESS; |
|---|
| 1394 | 2086 | } |
|---|
| 1395 | 2087 | |
|---|
| 1396 | | - cc->finishing_block = true; |
|---|
| 1397 | | - return COMPACT_CONTINUE; |
|---|
| 2088 | + ret = COMPACT_CONTINUE; |
|---|
| 2089 | + break; |
|---|
| 1398 | 2090 | } |
|---|
| 1399 | 2091 | } |
|---|
| 1400 | 2092 | |
|---|
| 1401 | | - return COMPACT_NO_SUITABLE_PAGE; |
|---|
| 2093 | +out: |
|---|
| 2094 | + if (cc->contended || fatal_signal_pending(current)) |
|---|
| 2095 | + ret = COMPACT_CONTENDED; |
|---|
| 2096 | + |
|---|
| 2097 | + return ret; |
|---|
| 1402 | 2098 | } |
|---|
| 1403 | 2099 | |
|---|
| 1404 | | -static enum compact_result compact_finished(struct zone *zone, |
|---|
| 1405 | | - struct compact_control *cc) |
|---|
| 2100 | +static enum compact_result compact_finished(struct compact_control *cc) |
|---|
| 1406 | 2101 | { |
|---|
| 1407 | 2102 | int ret; |
|---|
| 1408 | 2103 | |
|---|
| 1409 | | - ret = __compact_finished(zone, cc); |
|---|
| 1410 | | - trace_mm_compaction_finished(zone, cc->order, ret); |
|---|
| 2104 | + ret = __compact_finished(cc); |
|---|
| 2105 | + trace_mm_compaction_finished(cc->zone, cc->order, ret); |
|---|
| 1411 | 2106 | if (ret == COMPACT_NO_SUITABLE_PAGE) |
|---|
| 1412 | 2107 | ret = COMPACT_CONTINUE; |
|---|
| 1413 | 2108 | |
|---|
| .. | .. |
|---|
| 1423 | 2118 | */ |
|---|
| 1424 | 2119 | static enum compact_result __compaction_suitable(struct zone *zone, int order, |
|---|
| 1425 | 2120 | unsigned int alloc_flags, |
|---|
| 1426 | | - int classzone_idx, |
|---|
| 2121 | + int highest_zoneidx, |
|---|
| 1427 | 2122 | unsigned long wmark_target) |
|---|
| 1428 | 2123 | { |
|---|
| 1429 | 2124 | unsigned long watermark; |
|---|
| .. | .. |
|---|
| 1431 | 2126 | if (is_via_compact_memory(order)) |
|---|
| 1432 | 2127 | return COMPACT_CONTINUE; |
|---|
| 1433 | 2128 | |
|---|
| 1434 | | - watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK]; |
|---|
| 2129 | + watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); |
|---|
| 1435 | 2130 | /* |
|---|
| 1436 | 2131 | * If watermarks for high-order allocation are already met, there |
|---|
| 1437 | 2132 | * should be no need for compaction at all. |
|---|
| 1438 | 2133 | */ |
|---|
| 1439 | | - if (zone_watermark_ok(zone, order, watermark, classzone_idx, |
|---|
| 2134 | + if (zone_watermark_ok(zone, order, watermark, highest_zoneidx, |
|---|
| 1440 | 2135 | alloc_flags)) |
|---|
| 1441 | 2136 | return COMPACT_SUCCESS; |
|---|
| 1442 | 2137 | |
|---|
| .. | .. |
|---|
| 1446 | 2141 | * watermark and alloc_flags have to match, or be more pessimistic than |
|---|
| 1447 | 2142 | * the check in __isolate_free_page(). We don't use the direct |
|---|
| 1448 | 2143 | * compactor's alloc_flags, as they are not relevant for freepage |
|---|
| 1449 | | - * isolation. We however do use the direct compactor's classzone_idx to |
|---|
| 1450 | | - * skip over zones where lowmem reserves would prevent allocation even |
|---|
| 1451 | | - * if compaction succeeds. |
|---|
| 2144 | + * isolation. We however do use the direct compactor's highest_zoneidx |
|---|
| 2145 | + * to skip over zones where lowmem reserves would prevent allocation |
|---|
| 2146 | + * even if compaction succeeds. |
|---|
| 1452 | 2147 | * For costly orders, we require low watermark instead of min for |
|---|
| 1453 | 2148 | * compaction to proceed to increase its chances. |
|---|
| 1454 | 2149 | * ALLOC_CMA is used, as pages in CMA pageblocks are considered |
|---|
| .. | .. |
|---|
| 1457 | 2152 | watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? |
|---|
| 1458 | 2153 | low_wmark_pages(zone) : min_wmark_pages(zone); |
|---|
| 1459 | 2154 | watermark += compact_gap(order); |
|---|
| 1460 | | - if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx, |
|---|
| 2155 | + if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, |
|---|
| 1461 | 2156 | ALLOC_CMA, wmark_target)) |
|---|
| 1462 | 2157 | return COMPACT_SKIPPED; |
|---|
| 1463 | 2158 | |
|---|
| .. | .. |
|---|
| 1466 | 2161 | |
|---|
| 1467 | 2162 | enum compact_result compaction_suitable(struct zone *zone, int order, |
|---|
| 1468 | 2163 | unsigned int alloc_flags, |
|---|
| 1469 | | - int classzone_idx) |
|---|
| 2164 | + int highest_zoneidx) |
|---|
| 1470 | 2165 | { |
|---|
| 1471 | 2166 | enum compact_result ret; |
|---|
| 1472 | 2167 | int fragindex; |
|---|
| 1473 | 2168 | |
|---|
| 1474 | | - ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx, |
|---|
| 2169 | + ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx, |
|---|
| 1475 | 2170 | zone_page_state(zone, NR_FREE_PAGES)); |
|---|
| 1476 | 2171 | /* |
|---|
| 1477 | 2172 | * fragmentation index determines if allocation failures are due to |
|---|
| .. | .. |
|---|
| 1512 | 2207 | * Make sure at least one zone would pass __compaction_suitable if we continue |
|---|
| 1513 | 2208 | * retrying the reclaim. |
|---|
| 1514 | 2209 | */ |
|---|
| 1515 | | - for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
|---|
| 1516 | | - ac->nodemask) { |
|---|
| 2210 | + for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
|---|
| 2211 | + ac->highest_zoneidx, ac->nodemask) { |
|---|
| 1517 | 2212 | unsigned long available; |
|---|
| 1518 | 2213 | enum compact_result compact_result; |
|---|
| 1519 | 2214 | |
|---|
| .. | .. |
|---|
| 1526 | 2221 | available = zone_reclaimable_pages(zone) / order; |
|---|
| 1527 | 2222 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
|---|
| 1528 | 2223 | compact_result = __compaction_suitable(zone, order, alloc_flags, |
|---|
| 1529 | | - ac_classzone_idx(ac), available); |
|---|
| 2224 | + ac->highest_zoneidx, available); |
|---|
| 1530 | 2225 | if (compact_result != COMPACT_SKIPPED) |
|---|
| 1531 | 2226 | return true; |
|---|
| 1532 | 2227 | } |
|---|
| .. | .. |
|---|
| 1534 | 2229 | return false; |
|---|
| 1535 | 2230 | } |
|---|
| 1536 | 2231 | |
|---|
| 1537 | | -static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc) |
|---|
| 2232 | +static enum compact_result |
|---|
| 2233 | +compact_zone(struct compact_control *cc, struct capture_control *capc) |
|---|
| 1538 | 2234 | { |
|---|
| 1539 | 2235 | enum compact_result ret; |
|---|
| 1540 | | - unsigned long start_pfn = zone->zone_start_pfn; |
|---|
| 1541 | | - unsigned long end_pfn = zone_end_pfn(zone); |
|---|
| 2236 | + unsigned long start_pfn = cc->zone->zone_start_pfn; |
|---|
| 2237 | + unsigned long end_pfn = zone_end_pfn(cc->zone); |
|---|
| 2238 | + unsigned long last_migrated_pfn; |
|---|
| 1542 | 2239 | const bool sync = cc->mode != MIGRATE_ASYNC; |
|---|
| 2240 | + bool update_cached; |
|---|
| 1543 | 2241 | |
|---|
| 1544 | 2242 | /* |
|---|
| 1545 | 2243 | * These counters track activities during zone compaction. Initialize |
|---|
| .. | .. |
|---|
| 1552 | 2250 | INIT_LIST_HEAD(&cc->freepages); |
|---|
| 1553 | 2251 | INIT_LIST_HEAD(&cc->migratepages); |
|---|
| 1554 | 2252 | |
|---|
| 1555 | | - cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask); |
|---|
| 1556 | | - ret = compaction_suitable(zone, cc->order, cc->alloc_flags, |
|---|
| 1557 | | - cc->classzone_idx); |
|---|
| 2253 | + cc->migratetype = gfp_migratetype(cc->gfp_mask); |
|---|
| 2254 | + ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags, |
|---|
| 2255 | + cc->highest_zoneidx); |
|---|
| 1558 | 2256 | /* Compaction is likely to fail */ |
|---|
| 1559 | 2257 | if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED) |
|---|
| 1560 | 2258 | return ret; |
|---|
| .. | .. |
|---|
| 1566 | 2264 | * Clear pageblock skip if there were failures recently and compaction |
|---|
| 1567 | 2265 | * is about to be retried after being deferred. |
|---|
| 1568 | 2266 | */ |
|---|
| 1569 | | - if (compaction_restarting(zone, cc->order)) |
|---|
| 1570 | | - __reset_isolation_suitable(zone); |
|---|
| 2267 | + if (compaction_restarting(cc->zone, cc->order)) |
|---|
| 2268 | + __reset_isolation_suitable(cc->zone); |
|---|
| 1571 | 2269 | |
|---|
| 1572 | 2270 | /* |
|---|
| 1573 | 2271 | * Setup to move all movable pages to the end of the zone. Used cached |
|---|
| .. | .. |
|---|
| 1575 | 2273 | * want to compact the whole zone), but check that it is initialised |
|---|
| 1576 | 2274 | * by ensuring the values are within zone boundaries. |
|---|
| 1577 | 2275 | */ |
|---|
| 2276 | + cc->fast_start_pfn = 0; |
|---|
| 1578 | 2277 | if (cc->whole_zone) { |
|---|
| 1579 | 2278 | cc->migrate_pfn = start_pfn; |
|---|
| 1580 | 2279 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
|---|
| 1581 | 2280 | } else { |
|---|
| 1582 | | - cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync]; |
|---|
| 1583 | | - cc->free_pfn = zone->compact_cached_free_pfn; |
|---|
| 2281 | + cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; |
|---|
| 2282 | + cc->free_pfn = cc->zone->compact_cached_free_pfn; |
|---|
| 1584 | 2283 | if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { |
|---|
| 1585 | 2284 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
|---|
| 1586 | | - zone->compact_cached_free_pfn = cc->free_pfn; |
|---|
| 2285 | + cc->zone->compact_cached_free_pfn = cc->free_pfn; |
|---|
| 1587 | 2286 | } |
|---|
| 1588 | 2287 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { |
|---|
| 1589 | 2288 | cc->migrate_pfn = start_pfn; |
|---|
| 1590 | | - zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
|---|
| 1591 | | - zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
|---|
| 2289 | + cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
|---|
| 2290 | + cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
|---|
| 1592 | 2291 | } |
|---|
| 1593 | 2292 | |
|---|
| 1594 | | - if (cc->migrate_pfn == start_pfn) |
|---|
| 2293 | + if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) |
|---|
| 1595 | 2294 | cc->whole_zone = true; |
|---|
| 1596 | 2295 | } |
|---|
| 1597 | 2296 | |
|---|
| 1598 | | - cc->last_migrated_pfn = 0; |
|---|
| 2297 | + last_migrated_pfn = 0; |
|---|
| 2298 | + |
|---|
| 2299 | + /* |
|---|
| 2300 | + * Migrate has separate cached PFNs for ASYNC and SYNC* migration on |
|---|
| 2301 | + * the basis that some migrations will fail in ASYNC mode. However, |
|---|
| 2302 | + * if the cached PFNs match and pageblocks are skipped due to having |
|---|
| 2303 | + * no isolation candidates, then the sync state does not matter. |
|---|
| 2304 | + * Until a pageblock with isolation candidates is found, keep the |
|---|
| 2305 | + * cached PFNs in sync to avoid revisiting the same blocks. |
|---|
| 2306 | + */ |
|---|
| 2307 | + update_cached = !sync && |
|---|
| 2308 | + cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; |
|---|
| 1599 | 2309 | |
|---|
| 1600 | 2310 | trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, |
|---|
| 1601 | 2311 | cc->free_pfn, end_pfn, sync); |
|---|
| 1602 | 2312 | |
|---|
| 1603 | | - migrate_prep_local(); |
|---|
| 2313 | + /* lru_add_drain_all could be expensive with involving other CPUs */ |
|---|
| 2314 | + lru_add_drain(); |
|---|
| 1604 | 2315 | |
|---|
| 1605 | | - while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) { |
|---|
| 2316 | + while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { |
|---|
| 1606 | 2317 | int err; |
|---|
| 2318 | + unsigned long start_pfn = cc->migrate_pfn; |
|---|
| 1607 | 2319 | |
|---|
| 1608 | | - switch (isolate_migratepages(zone, cc)) { |
|---|
| 2320 | + /* |
|---|
| 2321 | + * Avoid multiple rescans which can happen if a page cannot be |
|---|
| 2322 | + * isolated (dirty/writeback in async mode) or if the migrated |
|---|
| 2323 | + * pages are being allocated before the pageblock is cleared. |
|---|
| 2324 | + * The first rescan will capture the entire pageblock for |
|---|
| 2325 | + * migration. If it fails, it'll be marked skip and scanning |
|---|
| 2326 | + * will proceed as normal. |
|---|
| 2327 | + */ |
|---|
| 2328 | + cc->rescan = false; |
|---|
| 2329 | + if (pageblock_start_pfn(last_migrated_pfn) == |
|---|
| 2330 | + pageblock_start_pfn(start_pfn)) { |
|---|
| 2331 | + cc->rescan = true; |
|---|
| 2332 | + } |
|---|
| 2333 | + |
|---|
| 2334 | + switch (isolate_migratepages(cc)) { |
|---|
| 1609 | 2335 | case ISOLATE_ABORT: |
|---|
| 1610 | 2336 | ret = COMPACT_CONTENDED; |
|---|
| 1611 | 2337 | putback_movable_pages(&cc->migratepages); |
|---|
| 1612 | 2338 | cc->nr_migratepages = 0; |
|---|
| 1613 | 2339 | goto out; |
|---|
| 1614 | 2340 | case ISOLATE_NONE: |
|---|
| 2341 | + if (update_cached) { |
|---|
| 2342 | + cc->zone->compact_cached_migrate_pfn[1] = |
|---|
| 2343 | + cc->zone->compact_cached_migrate_pfn[0]; |
|---|
| 2344 | + } |
|---|
| 2345 | + |
|---|
| 1615 | 2346 | /* |
|---|
| 1616 | 2347 | * We haven't isolated and migrated anything, but |
|---|
| 1617 | 2348 | * there might still be unflushed migrations from |
|---|
| .. | .. |
|---|
| 1619 | 2350 | */ |
|---|
| 1620 | 2351 | goto check_drain; |
|---|
| 1621 | 2352 | case ISOLATE_SUCCESS: |
|---|
| 2353 | + update_cached = false; |
|---|
| 2354 | + last_migrated_pfn = start_pfn; |
|---|
| 1622 | 2355 | ; |
|---|
| 1623 | 2356 | } |
|---|
| 1624 | 2357 | |
|---|
| .. | .. |
|---|
| 1650 | 2383 | cc->migrate_pfn = block_end_pfn( |
|---|
| 1651 | 2384 | cc->migrate_pfn - 1, cc->order); |
|---|
| 1652 | 2385 | /* Draining pcplists is useless in this case */ |
|---|
| 1653 | | - cc->last_migrated_pfn = 0; |
|---|
| 1654 | | - |
|---|
| 2386 | + last_migrated_pfn = 0; |
|---|
| 1655 | 2387 | } |
|---|
| 1656 | 2388 | } |
|---|
| 1657 | 2389 | |
|---|
| .. | .. |
|---|
| 1663 | 2395 | * compact_finished() can detect immediately if allocation |
|---|
| 1664 | 2396 | * would succeed. |
|---|
| 1665 | 2397 | */ |
|---|
| 1666 | | - if (cc->order > 0 && cc->last_migrated_pfn) { |
|---|
| 1667 | | - int cpu; |
|---|
| 2398 | + if (cc->order > 0 && last_migrated_pfn) { |
|---|
| 1668 | 2399 | unsigned long current_block_start = |
|---|
| 1669 | 2400 | block_start_pfn(cc->migrate_pfn, cc->order); |
|---|
| 1670 | 2401 | |
|---|
| 1671 | | - if (cc->last_migrated_pfn < current_block_start) { |
|---|
| 1672 | | - cpu = get_cpu(); |
|---|
| 1673 | | - lru_add_drain_cpu(cpu); |
|---|
| 1674 | | - drain_local_pages(zone); |
|---|
| 1675 | | - put_cpu(); |
|---|
| 2402 | + if (last_migrated_pfn < current_block_start) { |
|---|
| 2403 | + lru_add_drain_cpu_zone(cc->zone); |
|---|
| 1676 | 2404 | /* No more flushing until we migrate again */ |
|---|
| 1677 | | - cc->last_migrated_pfn = 0; |
|---|
| 2405 | + last_migrated_pfn = 0; |
|---|
| 1678 | 2406 | } |
|---|
| 1679 | 2407 | } |
|---|
| 1680 | 2408 | |
|---|
| 2409 | + /* Stop if a page has been captured */ |
|---|
| 2410 | + if (capc && capc->page) { |
|---|
| 2411 | + ret = COMPACT_SUCCESS; |
|---|
| 2412 | + break; |
|---|
| 2413 | + } |
|---|
| 1681 | 2414 | } |
|---|
| 1682 | 2415 | |
|---|
| 1683 | 2416 | out: |
|---|
| .. | .. |
|---|
| 1696 | 2429 | * Only go back, not forward. The cached pfn might have been |
|---|
| 1697 | 2430 | * already reset to zone end in compact_finished() |
|---|
| 1698 | 2431 | */ |
|---|
| 1699 | | - if (free_pfn > zone->compact_cached_free_pfn) |
|---|
| 1700 | | - zone->compact_cached_free_pfn = free_pfn; |
|---|
| 2432 | + if (free_pfn > cc->zone->compact_cached_free_pfn) |
|---|
| 2433 | + cc->zone->compact_cached_free_pfn = free_pfn; |
|---|
| 1701 | 2434 | } |
|---|
| 1702 | 2435 | |
|---|
| 1703 | 2436 | count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); |
|---|
| .. | .. |
|---|
| 1711 | 2444 | |
|---|
| 1712 | 2445 | static enum compact_result compact_zone_order(struct zone *zone, int order, |
|---|
| 1713 | 2446 | gfp_t gfp_mask, enum compact_priority prio, |
|---|
| 1714 | | - unsigned int alloc_flags, int classzone_idx) |
|---|
| 2447 | + unsigned int alloc_flags, int highest_zoneidx, |
|---|
| 2448 | + struct page **capture) |
|---|
| 1715 | 2449 | { |
|---|
| 1716 | 2450 | enum compact_result ret; |
|---|
| 1717 | 2451 | struct compact_control cc = { |
|---|
| 1718 | 2452 | .order = order, |
|---|
| 2453 | + .search_order = order, |
|---|
| 1719 | 2454 | .gfp_mask = gfp_mask, |
|---|
| 1720 | 2455 | .zone = zone, |
|---|
| 1721 | 2456 | .mode = (prio == COMPACT_PRIO_ASYNC) ? |
|---|
| 1722 | 2457 | MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, |
|---|
| 1723 | 2458 | .alloc_flags = alloc_flags, |
|---|
| 1724 | | - .classzone_idx = classzone_idx, |
|---|
| 2459 | + .highest_zoneidx = highest_zoneidx, |
|---|
| 1725 | 2460 | .direct_compaction = true, |
|---|
| 1726 | 2461 | .whole_zone = (prio == MIN_COMPACT_PRIORITY), |
|---|
| 1727 | 2462 | .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), |
|---|
| 1728 | 2463 | .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) |
|---|
| 1729 | 2464 | }; |
|---|
| 2465 | + struct capture_control capc = { |
|---|
| 2466 | + .cc = &cc, |
|---|
| 2467 | + .page = NULL, |
|---|
| 2468 | + }; |
|---|
| 1730 | 2469 | |
|---|
| 1731 | | - ret = compact_zone(zone, &cc); |
|---|
| 2470 | + /* |
|---|
| 2471 | + * Make sure the structs are really initialized before we expose the |
|---|
| 2472 | + * capture control, in case we are interrupted and the interrupt handler |
|---|
| 2473 | + * frees a page. |
|---|
| 2474 | + */ |
|---|
| 2475 | + barrier(); |
|---|
| 2476 | + WRITE_ONCE(current->capture_control, &capc); |
|---|
| 2477 | + |
|---|
| 2478 | + ret = compact_zone(&cc, &capc); |
|---|
| 1732 | 2479 | |
|---|
| 1733 | 2480 | VM_BUG_ON(!list_empty(&cc.freepages)); |
|---|
| 1734 | 2481 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
|---|
| 2482 | + |
|---|
| 2483 | + /* |
|---|
| 2484 | + * Make sure we hide capture control first before we read the captured |
|---|
| 2485 | + * page pointer, otherwise an interrupt could free and capture a page |
|---|
| 2486 | + * and we would leak it. |
|---|
| 2487 | + */ |
|---|
| 2488 | + WRITE_ONCE(current->capture_control, NULL); |
|---|
| 2489 | + *capture = READ_ONCE(capc.page); |
|---|
| 1735 | 2490 | |
|---|
| 1736 | 2491 | return ret; |
|---|
| 1737 | 2492 | } |
|---|
| .. | .. |
|---|
| 1745 | 2500 | * @alloc_flags: The allocation flags of the current allocation |
|---|
| 1746 | 2501 | * @ac: The context of current allocation |
|---|
| 1747 | 2502 | * @prio: Determines how hard direct compaction should try to succeed |
|---|
| 2503 | + * @capture: Pointer to free page created by compaction will be stored here |
|---|
| 1748 | 2504 | * |
|---|
| 1749 | 2505 | * This is the main entry point for direct page compaction. |
|---|
| 1750 | 2506 | */ |
|---|
| 1751 | 2507 | enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, |
|---|
| 1752 | 2508 | unsigned int alloc_flags, const struct alloc_context *ac, |
|---|
| 1753 | | - enum compact_priority prio) |
|---|
| 2509 | + enum compact_priority prio, struct page **capture) |
|---|
| 1754 | 2510 | { |
|---|
| 1755 | 2511 | int may_perform_io = gfp_mask & __GFP_IO; |
|---|
| 1756 | 2512 | struct zoneref *z; |
|---|
| .. | .. |
|---|
| 1767 | 2523 | trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); |
|---|
| 1768 | 2524 | |
|---|
| 1769 | 2525 | /* Compact each zone in the list */ |
|---|
| 1770 | | - for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
|---|
| 1771 | | - ac->nodemask) { |
|---|
| 2526 | + for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
|---|
| 2527 | + ac->highest_zoneidx, ac->nodemask) { |
|---|
| 1772 | 2528 | enum compact_result status; |
|---|
| 1773 | 2529 | |
|---|
| 1774 | 2530 | if (prio > MIN_COMPACT_PRIORITY |
|---|
| .. | .. |
|---|
| 1778 | 2534 | } |
|---|
| 1779 | 2535 | |
|---|
| 1780 | 2536 | status = compact_zone_order(zone, order, gfp_mask, prio, |
|---|
| 1781 | | - alloc_flags, ac_classzone_idx(ac)); |
|---|
| 2537 | + alloc_flags, ac->highest_zoneidx, capture); |
|---|
| 1782 | 2538 | rc = max(status, rc); |
|---|
| 1783 | 2539 | |
|---|
| 1784 | 2540 | /* The allocation should succeed, stop compacting */ |
|---|
| .. | .. |
|---|
| 1816 | 2572 | return rc; |
|---|
| 1817 | 2573 | } |
|---|
| 1818 | 2574 | |
|---|
| 2575 | +/* |
|---|
| 2576 | + * Compact all zones within a node till each zone's fragmentation score |
|---|
| 2577 | + * reaches within proactive compaction thresholds (as determined by the |
|---|
| 2578 | + * proactiveness tunable). |
|---|
| 2579 | + * |
|---|
| 2580 | + * It is possible that the function returns before reaching score targets |
|---|
| 2581 | + * due to various back-off conditions, such as, contention on per-node or |
|---|
| 2582 | + * per-zone locks. |
|---|
| 2583 | + */ |
|---|
| 2584 | +static void proactive_compact_node(pg_data_t *pgdat) |
|---|
| 2585 | +{ |
|---|
| 2586 | + int zoneid; |
|---|
| 2587 | + struct zone *zone; |
|---|
| 2588 | + struct compact_control cc = { |
|---|
| 2589 | + .order = -1, |
|---|
| 2590 | + .mode = MIGRATE_SYNC_LIGHT, |
|---|
| 2591 | + .ignore_skip_hint = true, |
|---|
| 2592 | + .whole_zone = true, |
|---|
| 2593 | + .gfp_mask = GFP_KERNEL, |
|---|
| 2594 | + .proactive_compaction = true, |
|---|
| 2595 | + }; |
|---|
| 2596 | + |
|---|
| 2597 | + for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
|---|
| 2598 | + zone = &pgdat->node_zones[zoneid]; |
|---|
| 2599 | + if (!populated_zone(zone)) |
|---|
| 2600 | + continue; |
|---|
| 2601 | + |
|---|
| 2602 | + cc.zone = zone; |
|---|
| 2603 | + |
|---|
| 2604 | + compact_zone(&cc, NULL); |
|---|
| 2605 | + |
|---|
| 2606 | + VM_BUG_ON(!list_empty(&cc.freepages)); |
|---|
| 2607 | + VM_BUG_ON(!list_empty(&cc.migratepages)); |
|---|
| 2608 | + } |
|---|
| 2609 | +} |
|---|
| 1819 | 2610 | |
|---|
| 1820 | 2611 | /* Compact all zones within a node */ |
|---|
| 1821 | 2612 | static void compact_node(int nid) |
|---|
| .. | .. |
|---|
| 1840 | 2631 | |
|---|
| 1841 | 2632 | cc.zone = zone; |
|---|
| 1842 | 2633 | |
|---|
| 1843 | | - compact_zone(zone, &cc); |
|---|
| 2634 | + compact_zone(&cc, NULL); |
|---|
| 1844 | 2635 | |
|---|
| 1845 | 2636 | VM_BUG_ON(!list_empty(&cc.freepages)); |
|---|
| 1846 | 2637 | VM_BUG_ON(!list_empty(&cc.migratepages)); |
|---|
| .. | .. |
|---|
| 1863 | 2654 | int sysctl_compact_memory; |
|---|
| 1864 | 2655 | |
|---|
| 1865 | 2656 | /* |
|---|
| 1866 | | - * This is the entry point for compacting all nodes via |
|---|
| 1867 | | - * /proc/sys/vm/compact_memory |
|---|
| 2657 | + * Tunable for proactive compaction. It determines how |
|---|
| 2658 | + * aggressively the kernel should compact memory in the |
|---|
| 2659 | + * background. It takes values in the range [0, 100]. |
|---|
| 1868 | 2660 | */ |
|---|
| 1869 | | -int sysctl_compaction_handler(struct ctl_table *table, int write, |
|---|
| 1870 | | - void __user *buffer, size_t *length, loff_t *ppos) |
|---|
| 2661 | +unsigned int __read_mostly sysctl_compaction_proactiveness = 20; |
|---|
| 2662 | + |
|---|
| 2663 | +int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write, |
|---|
| 2664 | + void *buffer, size_t *length, loff_t *ppos) |
|---|
| 1871 | 2665 | { |
|---|
| 1872 | | - if (write) |
|---|
| 1873 | | - compact_nodes(); |
|---|
| 2666 | + int rc, nid; |
|---|
| 2667 | + |
|---|
| 2668 | + rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
|---|
| 2669 | + if (rc) |
|---|
| 2670 | + return rc; |
|---|
| 2671 | + |
|---|
| 2672 | + if (write && sysctl_compaction_proactiveness) { |
|---|
| 2673 | + for_each_online_node(nid) { |
|---|
| 2674 | + pg_data_t *pgdat = NODE_DATA(nid); |
|---|
| 2675 | + |
|---|
| 2676 | + if (pgdat->proactive_compact_trigger) |
|---|
| 2677 | + continue; |
|---|
| 2678 | + |
|---|
| 2679 | + pgdat->proactive_compact_trigger = true; |
|---|
| 2680 | + wake_up_interruptible(&pgdat->kcompactd_wait); |
|---|
| 2681 | + } |
|---|
| 2682 | + } |
|---|
| 1874 | 2683 | |
|---|
| 1875 | 2684 | return 0; |
|---|
| 1876 | 2685 | } |
|---|
| 1877 | 2686 | |
|---|
| 1878 | | -int sysctl_extfrag_handler(struct ctl_table *table, int write, |
|---|
| 1879 | | - void __user *buffer, size_t *length, loff_t *ppos) |
|---|
| 2687 | +/* |
|---|
| 2688 | + * This is the entry point for compacting all nodes via |
|---|
| 2689 | + * /proc/sys/vm/compact_memory |
|---|
| 2690 | + */ |
|---|
| 2691 | +int sysctl_compaction_handler(struct ctl_table *table, int write, |
|---|
| 2692 | + void *buffer, size_t *length, loff_t *ppos) |
|---|
| 1880 | 2693 | { |
|---|
| 1881 | | - proc_dointvec_minmax(table, write, buffer, length, ppos); |
|---|
| 2694 | + if (write) |
|---|
| 2695 | + compact_nodes(); |
|---|
| 1882 | 2696 | |
|---|
| 1883 | 2697 | return 0; |
|---|
| 1884 | 2698 | } |
|---|
| .. | .. |
|---|
| 1914 | 2728 | |
|---|
| 1915 | 2729 | static inline bool kcompactd_work_requested(pg_data_t *pgdat) |
|---|
| 1916 | 2730 | { |
|---|
| 1917 | | - return pgdat->kcompactd_max_order > 0 || kthread_should_stop(); |
|---|
| 2731 | + return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || |
|---|
| 2732 | + pgdat->proactive_compact_trigger; |
|---|
| 1918 | 2733 | } |
|---|
| 1919 | 2734 | |
|---|
| 1920 | 2735 | static bool kcompactd_node_suitable(pg_data_t *pgdat) |
|---|
| 1921 | 2736 | { |
|---|
| 1922 | 2737 | int zoneid; |
|---|
| 1923 | 2738 | struct zone *zone; |
|---|
| 1924 | | - enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx; |
|---|
| 2739 | + enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; |
|---|
| 1925 | 2740 | |
|---|
| 1926 | | - for (zoneid = 0; zoneid <= classzone_idx; zoneid++) { |
|---|
| 2741 | + for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { |
|---|
| 1927 | 2742 | zone = &pgdat->node_zones[zoneid]; |
|---|
| 1928 | 2743 | |
|---|
| 1929 | 2744 | if (!populated_zone(zone)) |
|---|
| 1930 | 2745 | continue; |
|---|
| 1931 | 2746 | |
|---|
| 1932 | 2747 | if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0, |
|---|
| 1933 | | - classzone_idx) == COMPACT_CONTINUE) |
|---|
| 2748 | + highest_zoneidx) == COMPACT_CONTINUE) |
|---|
| 1934 | 2749 | return true; |
|---|
| 1935 | 2750 | } |
|---|
| 1936 | 2751 | |
|---|
| .. | .. |
|---|
| 1947 | 2762 | struct zone *zone; |
|---|
| 1948 | 2763 | struct compact_control cc = { |
|---|
| 1949 | 2764 | .order = pgdat->kcompactd_max_order, |
|---|
| 1950 | | - .classzone_idx = pgdat->kcompactd_classzone_idx, |
|---|
| 2765 | + .search_order = pgdat->kcompactd_max_order, |
|---|
| 2766 | + .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, |
|---|
| 1951 | 2767 | .mode = MIGRATE_SYNC_LIGHT, |
|---|
| 1952 | 2768 | .ignore_skip_hint = false, |
|---|
| 1953 | 2769 | .gfp_mask = GFP_KERNEL, |
|---|
| 1954 | 2770 | }; |
|---|
| 1955 | 2771 | trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, |
|---|
| 1956 | | - cc.classzone_idx); |
|---|
| 2772 | + cc.highest_zoneidx); |
|---|
| 1957 | 2773 | count_compact_event(KCOMPACTD_WAKE); |
|---|
| 1958 | 2774 | |
|---|
| 1959 | | - for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) { |
|---|
| 2775 | + for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { |
|---|
| 1960 | 2776 | int status; |
|---|
| 1961 | 2777 | |
|---|
| 1962 | 2778 | zone = &pgdat->node_zones[zoneid]; |
|---|
| .. | .. |
|---|
| 1974 | 2790 | return; |
|---|
| 1975 | 2791 | |
|---|
| 1976 | 2792 | cc.zone = zone; |
|---|
| 1977 | | - status = compact_zone(zone, &cc); |
|---|
| 2793 | + status = compact_zone(&cc, NULL); |
|---|
| 1978 | 2794 | |
|---|
| 1979 | 2795 | if (status == COMPACT_SUCCESS) { |
|---|
| 1980 | 2796 | compaction_defer_reset(zone, cc.order, false); |
|---|
| .. | .. |
|---|
| 2005 | 2821 | |
|---|
| 2006 | 2822 | /* |
|---|
| 2007 | 2823 | * Regardless of success, we are done until woken up next. But remember |
|---|
| 2008 | | - * the requested order/classzone_idx in case it was higher/tighter than |
|---|
| 2009 | | - * our current ones |
|---|
| 2824 | + * the requested order/highest_zoneidx in case it was higher/tighter |
|---|
| 2825 | + * than our current ones |
|---|
| 2010 | 2826 | */ |
|---|
| 2011 | 2827 | if (pgdat->kcompactd_max_order <= cc.order) |
|---|
| 2012 | 2828 | pgdat->kcompactd_max_order = 0; |
|---|
| 2013 | | - if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx) |
|---|
| 2014 | | - pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; |
|---|
| 2829 | + if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) |
|---|
| 2830 | + pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
|---|
| 2015 | 2831 | } |
|---|
| 2016 | 2832 | |
|---|
| 2017 | | -void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx) |
|---|
| 2833 | +void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) |
|---|
| 2018 | 2834 | { |
|---|
| 2019 | 2835 | if (!order) |
|---|
| 2020 | 2836 | return; |
|---|
| .. | .. |
|---|
| 2022 | 2838 | if (pgdat->kcompactd_max_order < order) |
|---|
| 2023 | 2839 | pgdat->kcompactd_max_order = order; |
|---|
| 2024 | 2840 | |
|---|
| 2025 | | - if (pgdat->kcompactd_classzone_idx > classzone_idx) |
|---|
| 2026 | | - pgdat->kcompactd_classzone_idx = classzone_idx; |
|---|
| 2841 | + if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) |
|---|
| 2842 | + pgdat->kcompactd_highest_zoneidx = highest_zoneidx; |
|---|
| 2027 | 2843 | |
|---|
| 2028 | 2844 | /* |
|---|
| 2029 | 2845 | * Pairs with implicit barrier in wait_event_freezable() |
|---|
| .. | .. |
|---|
| 2036 | 2852 | return; |
|---|
| 2037 | 2853 | |
|---|
| 2038 | 2854 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, |
|---|
| 2039 | | - classzone_idx); |
|---|
| 2855 | + highest_zoneidx); |
|---|
| 2040 | 2856 | wake_up_interruptible(&pgdat->kcompactd_wait); |
|---|
| 2041 | 2857 | } |
|---|
| 2042 | 2858 | |
|---|
| .. | .. |
|---|
| 2048 | 2864 | { |
|---|
| 2049 | 2865 | pg_data_t *pgdat = (pg_data_t*)p; |
|---|
| 2050 | 2866 | struct task_struct *tsk = current; |
|---|
| 2867 | + unsigned int proactive_defer = 0; |
|---|
| 2051 | 2868 | |
|---|
| 2052 | 2869 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
|---|
| 2053 | 2870 | |
|---|
| .. | .. |
|---|
| 2057 | 2874 | set_freezable(); |
|---|
| 2058 | 2875 | |
|---|
| 2059 | 2876 | pgdat->kcompactd_max_order = 0; |
|---|
| 2060 | | - pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1; |
|---|
| 2877 | + pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
|---|
| 2061 | 2878 | |
|---|
| 2062 | 2879 | while (!kthread_should_stop()) { |
|---|
| 2063 | 2880 | unsigned long pflags; |
|---|
| 2881 | + long timeout; |
|---|
| 2064 | 2882 | |
|---|
| 2883 | + timeout = sysctl_compaction_proactiveness ? |
|---|
| 2884 | + msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC) : |
|---|
| 2885 | + MAX_SCHEDULE_TIMEOUT; |
|---|
| 2065 | 2886 | trace_mm_compaction_kcompactd_sleep(pgdat->node_id); |
|---|
| 2066 | | - wait_event_freezable(pgdat->kcompactd_wait, |
|---|
| 2067 | | - kcompactd_work_requested(pgdat)); |
|---|
| 2887 | + if (wait_event_freezable_timeout(pgdat->kcompactd_wait, |
|---|
| 2888 | + kcompactd_work_requested(pgdat), timeout) && |
|---|
| 2889 | + !pgdat->proactive_compact_trigger) { |
|---|
| 2068 | 2890 | |
|---|
| 2069 | | - psi_memstall_enter(&pflags); |
|---|
| 2070 | | - kcompactd_do_work(pgdat); |
|---|
| 2071 | | - psi_memstall_leave(&pflags); |
|---|
| 2891 | + psi_memstall_enter(&pflags); |
|---|
| 2892 | + kcompactd_do_work(pgdat); |
|---|
| 2893 | + psi_memstall_leave(&pflags); |
|---|
| 2894 | + continue; |
|---|
| 2895 | + } |
|---|
| 2896 | + |
|---|
| 2897 | + /* kcompactd wait timeout */ |
|---|
| 2898 | + if (should_proactive_compact_node(pgdat)) { |
|---|
| 2899 | + unsigned int prev_score, score; |
|---|
| 2900 | + |
|---|
| 2901 | + /* |
|---|
| 2902 | + * On wakeup of proactive compaction by sysctl |
|---|
| 2903 | + * write, ignore the accumulated defer score. |
|---|
| 2904 | + * Anyway, if the proactive compaction didn't |
|---|
| 2905 | + * make any progress for the new value, it will |
|---|
| 2906 | + * be further deferred by 2^COMPACT_MAX_DEFER_SHIFT |
|---|
| 2907 | + * times. |
|---|
| 2908 | + */ |
|---|
| 2909 | + if (proactive_defer && |
|---|
| 2910 | + !pgdat->proactive_compact_trigger) { |
|---|
| 2911 | + proactive_defer--; |
|---|
| 2912 | + continue; |
|---|
| 2913 | + } |
|---|
| 2914 | + |
|---|
| 2915 | + prev_score = fragmentation_score_node(pgdat); |
|---|
| 2916 | + proactive_compact_node(pgdat); |
|---|
| 2917 | + score = fragmentation_score_node(pgdat); |
|---|
| 2918 | + /* |
|---|
| 2919 | + * Defer proactive compaction if the fragmentation |
|---|
| 2920 | + * score did not go down i.e. no progress made. |
|---|
| 2921 | + */ |
|---|
| 2922 | + proactive_defer = score < prev_score ? |
|---|
| 2923 | + 0 : 1 << COMPACT_MAX_DEFER_SHIFT; |
|---|
| 2924 | + } |
|---|
| 2925 | + if (pgdat->proactive_compact_trigger) |
|---|
| 2926 | + pgdat->proactive_compact_trigger = false; |
|---|
| 2072 | 2927 | } |
|---|
| 2073 | 2928 | |
|---|
| 2074 | 2929 | return 0; |
|---|