| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * linux/mm/compaction.c |
| 4 | * |
| 5 | * Memory compaction for the reduction of external fragmentation. Note that |
| 6 | * this heavily depends upon page migration to do all the real heavy |
| 7 | * lifting |
| 8 | * |
| 9 | * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie> |
| 10 | */ |
| 11 | #include <linux/cpu.h> |
| 12 | #include <linux/swap.h> |
| 13 | #include <linux/migrate.h> |
| 14 | #include <linux/compaction.h> |
| 15 | #include <linux/mm_inline.h> |
| 16 | #include <linux/sched/signal.h> |
| 17 | #include <linux/backing-dev.h> |
| 18 | #include <linux/sysctl.h> |
| 19 | #include <linux/sysfs.h> |
| 20 | #include <linux/page-isolation.h> |
| 21 | #include <linux/kasan.h> |
| 22 | #include <linux/kthread.h> |
| 23 | #include <linux/freezer.h> |
| 24 | #include <linux/page_owner.h> |
| 25 | #include <linux/psi.h> |
| 26 | #include <linux/cpuset.h> |
| 27 | #include "internal.h" |
| 28 | |
| 29 | #ifdef CONFIG_COMPACTION |
| 30 | /* |
| 31 | * Fragmentation score check interval for proactive compaction purposes. |
| 32 | */ |
| 33 | #define HPAGE_FRAG_CHECK_INTERVAL_MSEC (500) |
| 34 | |
| 35 | static inline void count_compact_event(enum vm_event_item item) |
| 36 | { |
| 37 | count_vm_event(item); |
| 38 | } |
| 39 | |
| 40 | static inline void count_compact_events(enum vm_event_item item, long delta) |
| 41 | { |
| 42 | count_vm_events(item, delta); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * order == -1 is expected when compacting proactively via |
| 47 | * 1. /proc/sys/vm/compact_memory |
| 48 | * 2. /sys/devices/system/node/nodex/compact |
| 49 | * 3. /proc/sys/vm/compaction_proactiveness |
| 50 | */ |
| 51 | static inline bool is_via_compact_memory(int order) |
| 52 | { |
| 53 | return order == -1; |
| 54 | } |
| 55 | |
| 56 | #else |
| 57 | #define count_compact_event(item) do { } while (0) |
| 58 | #define count_compact_events(item, delta) do { } while (0) |
| 59 | static inline bool is_via_compact_memory(int order) { return false; } |
| 60 | #endif |
| 61 | |
| 62 | #if defined CONFIG_COMPACTION || defined CONFIG_CMA |
| 63 | |
| 64 | #define CREATE_TRACE_POINTS |
| 65 | #include <trace/events/compaction.h> |
| 66 | |
| 67 | #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order)) |
| 68 | #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order)) |
| 69 | |
| 70 | /* |
| 71 | * Page order with-respect-to which proactive compaction |
| 72 | * calculates external fragmentation, which is used as |
| 73 | * the "fragmentation score" of a node/zone. |
| 74 | */ |
| 75 | #if defined CONFIG_TRANSPARENT_HUGEPAGE |
| 76 | #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER |
| 77 | #elif defined CONFIG_HUGETLBFS |
| 78 | #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER |
| 79 | #else |
| 80 | #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT) |
| 81 | #endif |
| 82 | |
| 83 | static struct page *mark_allocated_noprof(struct page *page, unsigned int order, gfp_t gfp_flags) |
| 84 | { |
| 85 | post_alloc_hook(page, order, __GFP_MOVABLE); |
| 86 | set_page_refcounted(page); |
| 87 | return page; |
| 88 | } |
| 89 | #define mark_allocated(...) alloc_hooks(mark_allocated_noprof(__VA_ARGS__)) |
| 90 | |
| 91 | static unsigned long release_free_list(struct list_head *freepages) |
| 92 | { |
| 93 | int order; |
| 94 | unsigned long high_pfn = 0; |
| 95 | |
| 96 | for (order = 0; order < NR_PAGE_ORDERS; order++) { |
| 97 | struct page *page, *next; |
| 98 | |
| 99 | list_for_each_entry_safe(page, next, &freepages[order], lru) { |
| 100 | unsigned long pfn = page_to_pfn(page); |
| 101 | |
| 102 | list_del(&page->lru); |
| 103 | /* |
| 104 | * Convert free pages into post allocation pages, so |
| 105 | * that we can free them via __free_page. |
| 106 | */ |
| 107 | mark_allocated(page, order, __GFP_MOVABLE); |
| 108 | __free_pages(page, order); |
| 109 | if (pfn > high_pfn) |
| 110 | high_pfn = pfn; |
| 111 | } |
| 112 | } |
| 113 | return high_pfn; |
| 114 | } |
| 115 | |
| 116 | #ifdef CONFIG_COMPACTION |
| 117 | bool PageMovable(struct page *page) |
| 118 | { |
| 119 | const struct movable_operations *mops; |
| 120 | |
| 121 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 122 | if (!__PageMovable(page)) |
| 123 | return false; |
| 124 | |
| 125 | mops = page_movable_ops(page); |
| 126 | if (mops) |
| 127 | return true; |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | void __SetPageMovable(struct page *page, const struct movable_operations *mops) |
| 133 | { |
| 134 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 135 | VM_BUG_ON_PAGE((unsigned long)mops & PAGE_MAPPING_MOVABLE, page); |
| 136 | page->mapping = (void *)((unsigned long)mops | PAGE_MAPPING_MOVABLE); |
| 137 | } |
| 138 | EXPORT_SYMBOL(__SetPageMovable); |
| 139 | |
| 140 | void __ClearPageMovable(struct page *page) |
| 141 | { |
| 142 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 143 | /* |
| 144 | * This page still has the type of a movable page, but it's |
| 145 | * actually not movable any more. |
| 146 | */ |
| 147 | page->mapping = (void *)PAGE_MAPPING_MOVABLE; |
| 148 | } |
| 149 | EXPORT_SYMBOL(__ClearPageMovable); |
| 150 | |
| 151 | /* Do not skip compaction more than 64 times */ |
| 152 | #define COMPACT_MAX_DEFER_SHIFT 6 |
| 153 | |
| 154 | /* |
| 155 | * Compaction is deferred when compaction fails to result in a page |
| 156 | * allocation success. 1 << compact_defer_shift, compactions are skipped up |
| 157 | * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT |
| 158 | */ |
| 159 | static void defer_compaction(struct zone *zone, int order) |
| 160 | { |
| 161 | zone->compact_considered = 0; |
| 162 | zone->compact_defer_shift++; |
| 163 | |
| 164 | if (order < zone->compact_order_failed) |
| 165 | zone->compact_order_failed = order; |
| 166 | |
| 167 | if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT) |
| 168 | zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT; |
| 169 | |
| 170 | trace_mm_compaction_defer_compaction(zone, order); |
| 171 | } |
| 172 | |
| 173 | /* Returns true if compaction should be skipped this time */ |
| 174 | static bool compaction_deferred(struct zone *zone, int order) |
| 175 | { |
| 176 | unsigned long defer_limit = 1UL << zone->compact_defer_shift; |
| 177 | |
| 178 | if (order < zone->compact_order_failed) |
| 179 | return false; |
| 180 | |
| 181 | /* Avoid possible overflow */ |
| 182 | if (++zone->compact_considered >= defer_limit) { |
| 183 | zone->compact_considered = defer_limit; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | trace_mm_compaction_deferred(zone, order); |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Update defer tracking counters after successful compaction of given order, |
| 194 | * which means an allocation either succeeded (alloc_success == true) or is |
| 195 | * expected to succeed. |
| 196 | */ |
| 197 | void compaction_defer_reset(struct zone *zone, int order, |
| 198 | bool alloc_success) |
| 199 | { |
| 200 | if (alloc_success) { |
| 201 | zone->compact_considered = 0; |
| 202 | zone->compact_defer_shift = 0; |
| 203 | } |
| 204 | if (order >= zone->compact_order_failed) |
| 205 | zone->compact_order_failed = order + 1; |
| 206 | |
| 207 | trace_mm_compaction_defer_reset(zone, order); |
| 208 | } |
| 209 | |
| 210 | /* Returns true if restarting compaction after many failures */ |
| 211 | static bool compaction_restarting(struct zone *zone, int order) |
| 212 | { |
| 213 | if (order < zone->compact_order_failed) |
| 214 | return false; |
| 215 | |
| 216 | return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT && |
| 217 | zone->compact_considered >= 1UL << zone->compact_defer_shift; |
| 218 | } |
| 219 | |
| 220 | /* Returns true if the pageblock should be scanned for pages to isolate. */ |
| 221 | static inline bool isolation_suitable(struct compact_control *cc, |
| 222 | struct page *page) |
| 223 | { |
| 224 | if (cc->ignore_skip_hint) |
| 225 | return true; |
| 226 | |
| 227 | return !get_pageblock_skip(page); |
| 228 | } |
| 229 | |
| 230 | static void reset_cached_positions(struct zone *zone) |
| 231 | { |
| 232 | zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn; |
| 233 | zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn; |
| 234 | zone->compact_cached_free_pfn = |
| 235 | pageblock_start_pfn(zone_end_pfn(zone) - 1); |
| 236 | } |
| 237 | |
| 238 | #ifdef CONFIG_SPARSEMEM |
| 239 | /* |
| 240 | * If the PFN falls into an offline section, return the start PFN of the |
| 241 | * next online section. If the PFN falls into an online section or if |
| 242 | * there is no next online section, return 0. |
| 243 | */ |
| 244 | static unsigned long skip_offline_sections(unsigned long start_pfn) |
| 245 | { |
| 246 | unsigned long start_nr = pfn_to_section_nr(start_pfn); |
| 247 | |
| 248 | if (online_section_nr(start_nr)) |
| 249 | return 0; |
| 250 | |
| 251 | while (++start_nr <= __highest_present_section_nr) { |
| 252 | if (online_section_nr(start_nr)) |
| 253 | return section_nr_to_pfn(start_nr); |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * If the PFN falls into an offline section, return the end PFN of the |
| 261 | * next online section in reverse. If the PFN falls into an online section |
| 262 | * or if there is no next online section in reverse, return 0. |
| 263 | */ |
| 264 | static unsigned long skip_offline_sections_reverse(unsigned long start_pfn) |
| 265 | { |
| 266 | unsigned long start_nr = pfn_to_section_nr(start_pfn); |
| 267 | |
| 268 | if (!start_nr || online_section_nr(start_nr)) |
| 269 | return 0; |
| 270 | |
| 271 | while (start_nr-- > 0) { |
| 272 | if (online_section_nr(start_nr)) |
| 273 | return section_nr_to_pfn(start_nr) + PAGES_PER_SECTION; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | #else |
| 279 | static unsigned long skip_offline_sections(unsigned long start_pfn) |
| 280 | { |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static unsigned long skip_offline_sections_reverse(unsigned long start_pfn) |
| 285 | { |
| 286 | return 0; |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | /* |
| 291 | * Compound pages of >= pageblock_order should consistently be skipped until |
| 292 | * released. It is always pointless to compact pages of such order (if they are |
| 293 | * migratable), and the pageblocks they occupy cannot contain any free pages. |
| 294 | */ |
| 295 | static bool pageblock_skip_persistent(struct page *page) |
| 296 | { |
| 297 | if (!PageCompound(page)) |
| 298 | return false; |
| 299 | |
| 300 | page = compound_head(page); |
| 301 | |
| 302 | if (compound_order(page) >= pageblock_order) |
| 303 | return true; |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | static bool |
| 309 | __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source, |
| 310 | bool check_target) |
| 311 | { |
| 312 | struct page *page = pfn_to_online_page(pfn); |
| 313 | struct page *block_page; |
| 314 | struct page *end_page; |
| 315 | unsigned long block_pfn; |
| 316 | |
| 317 | if (!page) |
| 318 | return false; |
| 319 | if (zone != page_zone(page)) |
| 320 | return false; |
| 321 | if (pageblock_skip_persistent(page)) |
| 322 | return false; |
| 323 | |
| 324 | /* |
| 325 | * If skip is already cleared do no further checking once the |
| 326 | * restart points have been set. |
| 327 | */ |
| 328 | if (check_source && check_target && !get_pageblock_skip(page)) |
| 329 | return true; |
| 330 | |
| 331 | /* |
| 332 | * If clearing skip for the target scanner, do not select a |
| 333 | * non-movable pageblock as the starting point. |
| 334 | */ |
| 335 | if (!check_source && check_target && |
| 336 | get_pageblock_migratetype(page) != MIGRATE_MOVABLE) |
| 337 | return false; |
| 338 | |
| 339 | /* Ensure the start of the pageblock or zone is online and valid */ |
| 340 | block_pfn = pageblock_start_pfn(pfn); |
| 341 | block_pfn = max(block_pfn, zone->zone_start_pfn); |
| 342 | block_page = pfn_to_online_page(block_pfn); |
| 343 | if (block_page) { |
| 344 | page = block_page; |
| 345 | pfn = block_pfn; |
| 346 | } |
| 347 | |
| 348 | /* Ensure the end of the pageblock or zone is online and valid */ |
| 349 | block_pfn = pageblock_end_pfn(pfn) - 1; |
| 350 | block_pfn = min(block_pfn, zone_end_pfn(zone) - 1); |
| 351 | end_page = pfn_to_online_page(block_pfn); |
| 352 | if (!end_page) |
| 353 | return false; |
| 354 | |
| 355 | /* |
| 356 | * Only clear the hint if a sample indicates there is either a |
| 357 | * free page or an LRU page in the block. One or other condition |
| 358 | * is necessary for the block to be a migration source/target. |
| 359 | */ |
| 360 | do { |
| 361 | if (check_source && PageLRU(page)) { |
| 362 | clear_pageblock_skip(page); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | if (check_target && PageBuddy(page)) { |
| 367 | clear_pageblock_skip(page); |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | page += (1 << PAGE_ALLOC_COSTLY_ORDER); |
| 372 | } while (page <= end_page); |
| 373 | |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * This function is called to clear all cached information on pageblocks that |
| 379 | * should be skipped for page isolation when the migrate and free page scanner |
| 380 | * meet. |
| 381 | */ |
| 382 | static void __reset_isolation_suitable(struct zone *zone) |
| 383 | { |
| 384 | unsigned long migrate_pfn = zone->zone_start_pfn; |
| 385 | unsigned long free_pfn = zone_end_pfn(zone) - 1; |
| 386 | unsigned long reset_migrate = free_pfn; |
| 387 | unsigned long reset_free = migrate_pfn; |
| 388 | bool source_set = false; |
| 389 | bool free_set = false; |
| 390 | |
| 391 | /* Only flush if a full compaction finished recently */ |
| 392 | if (!zone->compact_blockskip_flush) |
| 393 | return; |
| 394 | |
| 395 | zone->compact_blockskip_flush = false; |
| 396 | |
| 397 | /* |
| 398 | * Walk the zone and update pageblock skip information. Source looks |
| 399 | * for PageLRU while target looks for PageBuddy. When the scanner |
| 400 | * is found, both PageBuddy and PageLRU are checked as the pageblock |
| 401 | * is suitable as both source and target. |
| 402 | */ |
| 403 | for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages, |
| 404 | free_pfn -= pageblock_nr_pages) { |
| 405 | cond_resched(); |
| 406 | |
| 407 | /* Update the migrate PFN */ |
| 408 | if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) && |
| 409 | migrate_pfn < reset_migrate) { |
| 410 | source_set = true; |
| 411 | reset_migrate = migrate_pfn; |
| 412 | zone->compact_init_migrate_pfn = reset_migrate; |
| 413 | zone->compact_cached_migrate_pfn[0] = reset_migrate; |
| 414 | zone->compact_cached_migrate_pfn[1] = reset_migrate; |
| 415 | } |
| 416 | |
| 417 | /* Update the free PFN */ |
| 418 | if (__reset_isolation_pfn(zone, free_pfn, free_set, true) && |
| 419 | free_pfn > reset_free) { |
| 420 | free_set = true; |
| 421 | reset_free = free_pfn; |
| 422 | zone->compact_init_free_pfn = reset_free; |
| 423 | zone->compact_cached_free_pfn = reset_free; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Leave no distance if no suitable block was reset */ |
| 428 | if (reset_migrate >= reset_free) { |
| 429 | zone->compact_cached_migrate_pfn[0] = migrate_pfn; |
| 430 | zone->compact_cached_migrate_pfn[1] = migrate_pfn; |
| 431 | zone->compact_cached_free_pfn = free_pfn; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void reset_isolation_suitable(pg_data_t *pgdat) |
| 436 | { |
| 437 | int zoneid; |
| 438 | |
| 439 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 440 | struct zone *zone = &pgdat->node_zones[zoneid]; |
| 441 | if (!populated_zone(zone)) |
| 442 | continue; |
| 443 | |
| 444 | __reset_isolation_suitable(zone); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Sets the pageblock skip bit if it was clear. Note that this is a hint as |
| 450 | * locks are not required for read/writers. Returns true if it was already set. |
| 451 | */ |
| 452 | static bool test_and_set_skip(struct compact_control *cc, struct page *page) |
| 453 | { |
| 454 | bool skip; |
| 455 | |
| 456 | /* Do not update if skip hint is being ignored */ |
| 457 | if (cc->ignore_skip_hint) |
| 458 | return false; |
| 459 | |
| 460 | skip = get_pageblock_skip(page); |
| 461 | if (!skip && !cc->no_set_skip_hint) |
| 462 | set_pageblock_skip(page); |
| 463 | |
| 464 | return skip; |
| 465 | } |
| 466 | |
| 467 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
| 468 | { |
| 469 | struct zone *zone = cc->zone; |
| 470 | |
| 471 | /* Set for isolation rather than compaction */ |
| 472 | if (cc->no_set_skip_hint) |
| 473 | return; |
| 474 | |
| 475 | pfn = pageblock_end_pfn(pfn); |
| 476 | |
| 477 | /* Update where async and sync compaction should restart */ |
| 478 | if (pfn > zone->compact_cached_migrate_pfn[0]) |
| 479 | zone->compact_cached_migrate_pfn[0] = pfn; |
| 480 | if (cc->mode != MIGRATE_ASYNC && |
| 481 | pfn > zone->compact_cached_migrate_pfn[1]) |
| 482 | zone->compact_cached_migrate_pfn[1] = pfn; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * If no pages were isolated then mark this pageblock to be skipped in the |
| 487 | * future. The information is later cleared by __reset_isolation_suitable(). |
| 488 | */ |
| 489 | static void update_pageblock_skip(struct compact_control *cc, |
| 490 | struct page *page, unsigned long pfn) |
| 491 | { |
| 492 | struct zone *zone = cc->zone; |
| 493 | |
| 494 | if (cc->no_set_skip_hint) |
| 495 | return; |
| 496 | |
| 497 | set_pageblock_skip(page); |
| 498 | |
| 499 | if (pfn < zone->compact_cached_free_pfn) |
| 500 | zone->compact_cached_free_pfn = pfn; |
| 501 | } |
| 502 | #else |
| 503 | static inline bool isolation_suitable(struct compact_control *cc, |
| 504 | struct page *page) |
| 505 | { |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | static inline bool pageblock_skip_persistent(struct page *page) |
| 510 | { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | static inline void update_pageblock_skip(struct compact_control *cc, |
| 515 | struct page *page, unsigned long pfn) |
| 516 | { |
| 517 | } |
| 518 | |
| 519 | static void update_cached_migrate(struct compact_control *cc, unsigned long pfn) |
| 520 | { |
| 521 | } |
| 522 | |
| 523 | static bool test_and_set_skip(struct compact_control *cc, struct page *page) |
| 524 | { |
| 525 | return false; |
| 526 | } |
| 527 | #endif /* CONFIG_COMPACTION */ |
| 528 | |
| 529 | /* |
| 530 | * Compaction requires the taking of some coarse locks that are potentially |
| 531 | * very heavily contended. For async compaction, trylock and record if the |
| 532 | * lock is contended. The lock will still be acquired but compaction will |
| 533 | * abort when the current block is finished regardless of success rate. |
| 534 | * Sync compaction acquires the lock. |
| 535 | * |
| 536 | * Always returns true which makes it easier to track lock state in callers. |
| 537 | */ |
| 538 | static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags, |
| 539 | struct compact_control *cc) |
| 540 | __acquires(lock) |
| 541 | { |
| 542 | /* Track if the lock is contended in async mode */ |
| 543 | if (cc->mode == MIGRATE_ASYNC && !cc->contended) { |
| 544 | if (spin_trylock_irqsave(lock, *flags)) |
| 545 | return true; |
| 546 | |
| 547 | cc->contended = true; |
| 548 | } |
| 549 | |
| 550 | spin_lock_irqsave(lock, *flags); |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Compaction requires the taking of some coarse locks that are potentially |
| 556 | * very heavily contended. The lock should be periodically unlocked to avoid |
| 557 | * having disabled IRQs for a long time, even when there is nobody waiting on |
| 558 | * the lock. It might also be that allowing the IRQs will result in |
| 559 | * need_resched() becoming true. If scheduling is needed, compaction schedules. |
| 560 | * Either compaction type will also abort if a fatal signal is pending. |
| 561 | * In either case if the lock was locked, it is dropped and not regained. |
| 562 | * |
| 563 | * Returns true if compaction should abort due to fatal signal pending. |
| 564 | * Returns false when compaction can continue. |
| 565 | */ |
| 566 | static bool compact_unlock_should_abort(spinlock_t *lock, |
| 567 | unsigned long flags, bool *locked, struct compact_control *cc) |
| 568 | { |
| 569 | if (*locked) { |
| 570 | spin_unlock_irqrestore(lock, flags); |
| 571 | *locked = false; |
| 572 | } |
| 573 | |
| 574 | if (fatal_signal_pending(current)) { |
| 575 | cc->contended = true; |
| 576 | return true; |
| 577 | } |
| 578 | |
| 579 | cond_resched(); |
| 580 | |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Isolate free pages onto a private freelist. If @strict is true, will abort |
| 586 | * returning 0 on any invalid PFNs or non-free pages inside of the pageblock |
| 587 | * (even though it may still end up isolating some pages). |
| 588 | */ |
| 589 | static unsigned long isolate_freepages_block(struct compact_control *cc, |
| 590 | unsigned long *start_pfn, |
| 591 | unsigned long end_pfn, |
| 592 | struct list_head *freelist, |
| 593 | unsigned int stride, |
| 594 | bool strict) |
| 595 | { |
| 596 | int nr_scanned = 0, total_isolated = 0; |
| 597 | struct page *page; |
| 598 | unsigned long flags = 0; |
| 599 | bool locked = false; |
| 600 | unsigned long blockpfn = *start_pfn; |
| 601 | unsigned int order; |
| 602 | |
| 603 | /* Strict mode is for isolation, speed is secondary */ |
| 604 | if (strict) |
| 605 | stride = 1; |
| 606 | |
| 607 | page = pfn_to_page(blockpfn); |
| 608 | |
| 609 | /* Isolate free pages. */ |
| 610 | for (; blockpfn < end_pfn; blockpfn += stride, page += stride) { |
| 611 | int isolated; |
| 612 | |
| 613 | /* |
| 614 | * Periodically drop the lock (if held) regardless of its |
| 615 | * contention, to give chance to IRQs. Abort if fatal signal |
| 616 | * pending. |
| 617 | */ |
| 618 | if (!(blockpfn % COMPACT_CLUSTER_MAX) |
| 619 | && compact_unlock_should_abort(&cc->zone->lock, flags, |
| 620 | &locked, cc)) |
| 621 | break; |
| 622 | |
| 623 | nr_scanned++; |
| 624 | |
| 625 | /* |
| 626 | * For compound pages such as THP and hugetlbfs, we can save |
| 627 | * potentially a lot of iterations if we skip them at once. |
| 628 | * The check is racy, but we can consider only valid values |
| 629 | * and the only danger is skipping too much. |
| 630 | */ |
| 631 | if (PageCompound(page)) { |
| 632 | const unsigned int order = compound_order(page); |
| 633 | |
| 634 | if ((order <= MAX_PAGE_ORDER) && |
| 635 | (blockpfn + (1UL << order) <= end_pfn)) { |
| 636 | blockpfn += (1UL << order) - 1; |
| 637 | page += (1UL << order) - 1; |
| 638 | nr_scanned += (1UL << order) - 1; |
| 639 | } |
| 640 | |
| 641 | goto isolate_fail; |
| 642 | } |
| 643 | |
| 644 | if (!PageBuddy(page)) |
| 645 | goto isolate_fail; |
| 646 | |
| 647 | /* If we already hold the lock, we can skip some rechecking. */ |
| 648 | if (!locked) { |
| 649 | locked = compact_lock_irqsave(&cc->zone->lock, |
| 650 | &flags, cc); |
| 651 | |
| 652 | /* Recheck this is a buddy page under lock */ |
| 653 | if (!PageBuddy(page)) |
| 654 | goto isolate_fail; |
| 655 | } |
| 656 | |
| 657 | /* Found a free page, will break it into order-0 pages */ |
| 658 | order = buddy_order(page); |
| 659 | isolated = __isolate_free_page(page, order); |
| 660 | if (!isolated) |
| 661 | break; |
| 662 | set_page_private(page, order); |
| 663 | |
| 664 | nr_scanned += isolated - 1; |
| 665 | total_isolated += isolated; |
| 666 | cc->nr_freepages += isolated; |
| 667 | list_add_tail(&page->lru, &freelist[order]); |
| 668 | |
| 669 | if (!strict && cc->nr_migratepages <= cc->nr_freepages) { |
| 670 | blockpfn += isolated; |
| 671 | break; |
| 672 | } |
| 673 | /* Advance to the end of split page */ |
| 674 | blockpfn += isolated - 1; |
| 675 | page += isolated - 1; |
| 676 | continue; |
| 677 | |
| 678 | isolate_fail: |
| 679 | if (strict) |
| 680 | break; |
| 681 | |
| 682 | } |
| 683 | |
| 684 | if (locked) |
| 685 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 686 | |
| 687 | /* |
| 688 | * Be careful to not go outside of the pageblock. |
| 689 | */ |
| 690 | if (unlikely(blockpfn > end_pfn)) |
| 691 | blockpfn = end_pfn; |
| 692 | |
| 693 | trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn, |
| 694 | nr_scanned, total_isolated); |
| 695 | |
| 696 | /* Record how far we have got within the block */ |
| 697 | *start_pfn = blockpfn; |
| 698 | |
| 699 | /* |
| 700 | * If strict isolation is requested by CMA then check that all the |
| 701 | * pages requested were isolated. If there were any failures, 0 is |
| 702 | * returned and CMA will fail. |
| 703 | */ |
| 704 | if (strict && blockpfn < end_pfn) |
| 705 | total_isolated = 0; |
| 706 | |
| 707 | cc->total_free_scanned += nr_scanned; |
| 708 | if (total_isolated) |
| 709 | count_compact_events(COMPACTISOLATED, total_isolated); |
| 710 | return total_isolated; |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * isolate_freepages_range() - isolate free pages. |
| 715 | * @cc: Compaction control structure. |
| 716 | * @start_pfn: The first PFN to start isolating. |
| 717 | * @end_pfn: The one-past-last PFN. |
| 718 | * |
| 719 | * Non-free pages, invalid PFNs, or zone boundaries within the |
| 720 | * [start_pfn, end_pfn) range are considered errors, cause function to |
| 721 | * undo its actions and return zero. cc->freepages[] are empty. |
| 722 | * |
| 723 | * Otherwise, function returns one-past-the-last PFN of isolated page |
| 724 | * (which may be greater then end_pfn if end fell in a middle of |
| 725 | * a free page). cc->freepages[] contain free pages isolated. |
| 726 | */ |
| 727 | unsigned long |
| 728 | isolate_freepages_range(struct compact_control *cc, |
| 729 | unsigned long start_pfn, unsigned long end_pfn) |
| 730 | { |
| 731 | unsigned long isolated, pfn, block_start_pfn, block_end_pfn; |
| 732 | int order; |
| 733 | |
| 734 | for (order = 0; order < NR_PAGE_ORDERS; order++) |
| 735 | INIT_LIST_HEAD(&cc->freepages[order]); |
| 736 | |
| 737 | pfn = start_pfn; |
| 738 | block_start_pfn = pageblock_start_pfn(pfn); |
| 739 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 740 | block_start_pfn = cc->zone->zone_start_pfn; |
| 741 | block_end_pfn = pageblock_end_pfn(pfn); |
| 742 | |
| 743 | for (; pfn < end_pfn; pfn += isolated, |
| 744 | block_start_pfn = block_end_pfn, |
| 745 | block_end_pfn += pageblock_nr_pages) { |
| 746 | /* Protect pfn from changing by isolate_freepages_block */ |
| 747 | unsigned long isolate_start_pfn = pfn; |
| 748 | |
| 749 | /* |
| 750 | * pfn could pass the block_end_pfn if isolated freepage |
| 751 | * is more than pageblock order. In this case, we adjust |
| 752 | * scanning range to right one. |
| 753 | */ |
| 754 | if (pfn >= block_end_pfn) { |
| 755 | block_start_pfn = pageblock_start_pfn(pfn); |
| 756 | block_end_pfn = pageblock_end_pfn(pfn); |
| 757 | } |
| 758 | |
| 759 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 760 | |
| 761 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 762 | block_end_pfn, cc->zone)) |
| 763 | break; |
| 764 | |
| 765 | isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
| 766 | block_end_pfn, cc->freepages, 0, true); |
| 767 | |
| 768 | /* |
| 769 | * In strict mode, isolate_freepages_block() returns 0 if |
| 770 | * there are any holes in the block (ie. invalid PFNs or |
| 771 | * non-free pages). |
| 772 | */ |
| 773 | if (!isolated) |
| 774 | break; |
| 775 | |
| 776 | /* |
| 777 | * If we managed to isolate pages, it is always (1 << n) * |
| 778 | * pageblock_nr_pages for some non-negative n. (Max order |
| 779 | * page may span two pageblocks). |
| 780 | */ |
| 781 | } |
| 782 | |
| 783 | if (pfn < end_pfn) { |
| 784 | /* Loop terminated early, cleanup. */ |
| 785 | release_free_list(cc->freepages); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /* We don't use freelists for anything. */ |
| 790 | return pfn; |
| 791 | } |
| 792 | |
| 793 | /* Similar to reclaim, but different enough that they don't share logic */ |
| 794 | static bool too_many_isolated(struct compact_control *cc) |
| 795 | { |
| 796 | pg_data_t *pgdat = cc->zone->zone_pgdat; |
| 797 | bool too_many; |
| 798 | |
| 799 | unsigned long active, inactive, isolated; |
| 800 | |
| 801 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE) + |
| 802 | node_page_state(pgdat, NR_INACTIVE_ANON); |
| 803 | active = node_page_state(pgdat, NR_ACTIVE_FILE) + |
| 804 | node_page_state(pgdat, NR_ACTIVE_ANON); |
| 805 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE) + |
| 806 | node_page_state(pgdat, NR_ISOLATED_ANON); |
| 807 | |
| 808 | /* |
| 809 | * Allow GFP_NOFS to isolate past the limit set for regular |
| 810 | * compaction runs. This prevents an ABBA deadlock when other |
| 811 | * compactors have already isolated to the limit, but are |
| 812 | * blocked on filesystem locks held by the GFP_NOFS thread. |
| 813 | */ |
| 814 | if (cc->gfp_mask & __GFP_FS) { |
| 815 | inactive >>= 3; |
| 816 | active >>= 3; |
| 817 | } |
| 818 | |
| 819 | too_many = isolated > (inactive + active) / 2; |
| 820 | if (!too_many) |
| 821 | wake_throttle_isolated(pgdat); |
| 822 | |
| 823 | return too_many; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * skip_isolation_on_order() - determine when to skip folio isolation based on |
| 828 | * folio order and compaction target order |
| 829 | * @order: to-be-isolated folio order |
| 830 | * @target_order: compaction target order |
| 831 | * |
| 832 | * This avoids unnecessary folio isolations during compaction. |
| 833 | */ |
| 834 | static bool skip_isolation_on_order(int order, int target_order) |
| 835 | { |
| 836 | /* |
| 837 | * Unless we are performing global compaction (i.e., |
| 838 | * is_via_compact_memory), skip any folios that are larger than the |
| 839 | * target order: we wouldn't be here if we'd have a free folio with |
| 840 | * the desired target_order, so migrating this folio would likely fail |
| 841 | * later. |
| 842 | */ |
| 843 | if (!is_via_compact_memory(target_order) && order >= target_order) |
| 844 | return true; |
| 845 | /* |
| 846 | * We limit memory compaction to pageblocks and won't try |
| 847 | * creating free blocks of memory that are larger than that. |
| 848 | */ |
| 849 | return order >= pageblock_order; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * isolate_migratepages_block() - isolate all migrate-able pages within |
| 854 | * a single pageblock |
| 855 | * @cc: Compaction control structure. |
| 856 | * @low_pfn: The first PFN to isolate |
| 857 | * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock |
| 858 | * @mode: Isolation mode to be used. |
| 859 | * |
| 860 | * Isolate all pages that can be migrated from the range specified by |
| 861 | * [low_pfn, end_pfn). The range is expected to be within same pageblock. |
| 862 | * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion, |
| 863 | * -ENOMEM in case we could not allocate a page, or 0. |
| 864 | * cc->migrate_pfn will contain the next pfn to scan. |
| 865 | * |
| 866 | * The pages are isolated on cc->migratepages list (not required to be empty), |
| 867 | * and cc->nr_migratepages is updated accordingly. |
| 868 | */ |
| 869 | static int |
| 870 | isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, |
| 871 | unsigned long end_pfn, isolate_mode_t mode) |
| 872 | { |
| 873 | pg_data_t *pgdat = cc->zone->zone_pgdat; |
| 874 | unsigned long nr_scanned = 0, nr_isolated = 0; |
| 875 | struct lruvec *lruvec; |
| 876 | unsigned long flags = 0; |
| 877 | struct lruvec *locked = NULL; |
| 878 | struct folio *folio = NULL; |
| 879 | struct page *page = NULL, *valid_page = NULL; |
| 880 | struct address_space *mapping; |
| 881 | unsigned long start_pfn = low_pfn; |
| 882 | bool skip_on_failure = false; |
| 883 | unsigned long next_skip_pfn = 0; |
| 884 | bool skip_updated = false; |
| 885 | int ret = 0; |
| 886 | |
| 887 | cc->migrate_pfn = low_pfn; |
| 888 | |
| 889 | /* |
| 890 | * Ensure that there are not too many pages isolated from the LRU |
| 891 | * list by either parallel reclaimers or compaction. If there are, |
| 892 | * delay for some time until fewer pages are isolated |
| 893 | */ |
| 894 | while (unlikely(too_many_isolated(cc))) { |
| 895 | /* stop isolation if there are still pages not migrated */ |
| 896 | if (cc->nr_migratepages) |
| 897 | return -EAGAIN; |
| 898 | |
| 899 | /* async migration should just abort */ |
| 900 | if (cc->mode == MIGRATE_ASYNC) |
| 901 | return -EAGAIN; |
| 902 | |
| 903 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); |
| 904 | |
| 905 | if (fatal_signal_pending(current)) |
| 906 | return -EINTR; |
| 907 | } |
| 908 | |
| 909 | cond_resched(); |
| 910 | |
| 911 | if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) { |
| 912 | skip_on_failure = true; |
| 913 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 914 | } |
| 915 | |
| 916 | /* Time to isolate some pages for migration */ |
| 917 | for (; low_pfn < end_pfn; low_pfn++) { |
| 918 | bool is_dirty, is_unevictable; |
| 919 | |
| 920 | if (skip_on_failure && low_pfn >= next_skip_pfn) { |
| 921 | /* |
| 922 | * We have isolated all migration candidates in the |
| 923 | * previous order-aligned block, and did not skip it due |
| 924 | * to failure. We should migrate the pages now and |
| 925 | * hopefully succeed compaction. |
| 926 | */ |
| 927 | if (nr_isolated) |
| 928 | break; |
| 929 | |
| 930 | /* |
| 931 | * We failed to isolate in the previous order-aligned |
| 932 | * block. Set the new boundary to the end of the |
| 933 | * current block. Note we can't simply increase |
| 934 | * next_skip_pfn by 1 << order, as low_pfn might have |
| 935 | * been incremented by a higher number due to skipping |
| 936 | * a compound or a high-order buddy page in the |
| 937 | * previous loop iteration. |
| 938 | */ |
| 939 | next_skip_pfn = block_end_pfn(low_pfn, cc->order); |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Periodically drop the lock (if held) regardless of its |
| 944 | * contention, to give chance to IRQs. Abort completely if |
| 945 | * a fatal signal is pending. |
| 946 | */ |
| 947 | if (!(low_pfn % COMPACT_CLUSTER_MAX)) { |
| 948 | if (locked) { |
| 949 | unlock_page_lruvec_irqrestore(locked, flags); |
| 950 | locked = NULL; |
| 951 | } |
| 952 | |
| 953 | if (fatal_signal_pending(current)) { |
| 954 | cc->contended = true; |
| 955 | ret = -EINTR; |
| 956 | |
| 957 | goto fatal_pending; |
| 958 | } |
| 959 | |
| 960 | cond_resched(); |
| 961 | } |
| 962 | |
| 963 | nr_scanned++; |
| 964 | |
| 965 | page = pfn_to_page(low_pfn); |
| 966 | |
| 967 | /* |
| 968 | * Check if the pageblock has already been marked skipped. |
| 969 | * Only the first PFN is checked as the caller isolates |
| 970 | * COMPACT_CLUSTER_MAX at a time so the second call must |
| 971 | * not falsely conclude that the block should be skipped. |
| 972 | */ |
| 973 | if (!valid_page && (pageblock_aligned(low_pfn) || |
| 974 | low_pfn == cc->zone->zone_start_pfn)) { |
| 975 | if (!isolation_suitable(cc, page)) { |
| 976 | low_pfn = end_pfn; |
| 977 | folio = NULL; |
| 978 | goto isolate_abort; |
| 979 | } |
| 980 | valid_page = page; |
| 981 | } |
| 982 | |
| 983 | if (PageHuge(page)) { |
| 984 | const unsigned int order = compound_order(page); |
| 985 | /* |
| 986 | * skip hugetlbfs if we are not compacting for pages |
| 987 | * bigger than its order. THPs and other compound pages |
| 988 | * are handled below. |
| 989 | */ |
| 990 | if (!cc->alloc_contig) { |
| 991 | |
| 992 | if (order <= MAX_PAGE_ORDER) { |
| 993 | low_pfn += (1UL << order) - 1; |
| 994 | nr_scanned += (1UL << order) - 1; |
| 995 | } |
| 996 | goto isolate_fail; |
| 997 | } |
| 998 | /* for alloc_contig case */ |
| 999 | if (locked) { |
| 1000 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1001 | locked = NULL; |
| 1002 | } |
| 1003 | |
| 1004 | folio = page_folio(page); |
| 1005 | ret = isolate_or_dissolve_huge_folio(folio, &cc->migratepages); |
| 1006 | |
| 1007 | /* |
| 1008 | * Fail isolation in case isolate_or_dissolve_huge_folio() |
| 1009 | * reports an error. In case of -ENOMEM, abort right away. |
| 1010 | */ |
| 1011 | if (ret < 0) { |
| 1012 | /* Do not report -EBUSY down the chain */ |
| 1013 | if (ret == -EBUSY) |
| 1014 | ret = 0; |
| 1015 | low_pfn += (1UL << order) - 1; |
| 1016 | nr_scanned += (1UL << order) - 1; |
| 1017 | goto isolate_fail; |
| 1018 | } |
| 1019 | |
| 1020 | if (folio_test_hugetlb(folio)) { |
| 1021 | /* |
| 1022 | * Hugepage was successfully isolated and placed |
| 1023 | * on the cc->migratepages list. |
| 1024 | */ |
| 1025 | low_pfn += folio_nr_pages(folio) - 1; |
| 1026 | goto isolate_success_no_list; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Ok, the hugepage was dissolved. Now these pages are |
| 1031 | * Buddy and cannot be re-allocated because they are |
| 1032 | * isolated. Fall-through as the check below handles |
| 1033 | * Buddy pages. |
| 1034 | */ |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Skip if free. We read page order here without zone lock |
| 1039 | * which is generally unsafe, but the race window is small and |
| 1040 | * the worst thing that can happen is that we skip some |
| 1041 | * potential isolation targets. |
| 1042 | */ |
| 1043 | if (PageBuddy(page)) { |
| 1044 | unsigned long freepage_order = buddy_order_unsafe(page); |
| 1045 | |
| 1046 | /* |
| 1047 | * Without lock, we cannot be sure that what we got is |
| 1048 | * a valid page order. Consider only values in the |
| 1049 | * valid order range to prevent low_pfn overflow. |
| 1050 | */ |
| 1051 | if (freepage_order > 0 && freepage_order <= MAX_PAGE_ORDER) { |
| 1052 | low_pfn += (1UL << freepage_order) - 1; |
| 1053 | nr_scanned += (1UL << freepage_order) - 1; |
| 1054 | } |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * Regardless of being on LRU, compound pages such as THP |
| 1060 | * (hugetlbfs is handled above) are not to be compacted unless |
| 1061 | * we are attempting an allocation larger than the compound |
| 1062 | * page size. We can potentially save a lot of iterations if we |
| 1063 | * skip them at once. The check is racy, but we can consider |
| 1064 | * only valid values and the only danger is skipping too much. |
| 1065 | */ |
| 1066 | if (PageCompound(page) && !cc->alloc_contig) { |
| 1067 | const unsigned int order = compound_order(page); |
| 1068 | |
| 1069 | /* Skip based on page order and compaction target order. */ |
| 1070 | if (skip_isolation_on_order(order, cc->order)) { |
| 1071 | if (order <= MAX_PAGE_ORDER) { |
| 1072 | low_pfn += (1UL << order) - 1; |
| 1073 | nr_scanned += (1UL << order) - 1; |
| 1074 | } |
| 1075 | goto isolate_fail; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Check may be lockless but that's ok as we recheck later. |
| 1081 | * It's possible to migrate LRU and non-lru movable pages. |
| 1082 | * Skip any other type of page |
| 1083 | */ |
| 1084 | if (!PageLRU(page)) { |
| 1085 | /* |
| 1086 | * __PageMovable can return false positive so we need |
| 1087 | * to verify it under page_lock. |
| 1088 | */ |
| 1089 | if (unlikely(__PageMovable(page)) && |
| 1090 | !PageIsolated(page)) { |
| 1091 | if (locked) { |
| 1092 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1093 | locked = NULL; |
| 1094 | } |
| 1095 | |
| 1096 | if (isolate_movable_page(page, mode)) { |
| 1097 | folio = page_folio(page); |
| 1098 | goto isolate_success; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | goto isolate_fail; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Be careful not to clear PageLRU until after we're |
| 1107 | * sure the page is not being freed elsewhere -- the |
| 1108 | * page release code relies on it. |
| 1109 | */ |
| 1110 | folio = folio_get_nontail_page(page); |
| 1111 | if (unlikely(!folio)) |
| 1112 | goto isolate_fail; |
| 1113 | |
| 1114 | /* |
| 1115 | * Migration will fail if an anonymous page is pinned in memory, |
| 1116 | * so avoid taking lru_lock and isolating it unnecessarily in an |
| 1117 | * admittedly racy check. |
| 1118 | */ |
| 1119 | mapping = folio_mapping(folio); |
| 1120 | if (!mapping && (folio_ref_count(folio) - 1) > folio_mapcount(folio)) |
| 1121 | goto isolate_fail_put; |
| 1122 | |
| 1123 | /* |
| 1124 | * Only allow to migrate anonymous pages in GFP_NOFS context |
| 1125 | * because those do not depend on fs locks. |
| 1126 | */ |
| 1127 | if (!(cc->gfp_mask & __GFP_FS) && mapping) |
| 1128 | goto isolate_fail_put; |
| 1129 | |
| 1130 | /* Only take pages on LRU: a check now makes later tests safe */ |
| 1131 | if (!folio_test_lru(folio)) |
| 1132 | goto isolate_fail_put; |
| 1133 | |
| 1134 | is_unevictable = folio_test_unevictable(folio); |
| 1135 | |
| 1136 | /* Compaction might skip unevictable pages but CMA takes them */ |
| 1137 | if (!(mode & ISOLATE_UNEVICTABLE) && is_unevictable) |
| 1138 | goto isolate_fail_put; |
| 1139 | |
| 1140 | /* |
| 1141 | * To minimise LRU disruption, the caller can indicate with |
| 1142 | * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages |
| 1143 | * it will be able to migrate without blocking - clean pages |
| 1144 | * for the most part. PageWriteback would require blocking. |
| 1145 | */ |
| 1146 | if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_writeback(folio)) |
| 1147 | goto isolate_fail_put; |
| 1148 | |
| 1149 | is_dirty = folio_test_dirty(folio); |
| 1150 | |
| 1151 | if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) || |
| 1152 | (mapping && is_unevictable)) { |
| 1153 | bool migrate_dirty = true; |
| 1154 | bool is_inaccessible; |
| 1155 | |
| 1156 | /* |
| 1157 | * Only folios without mappings or that have |
| 1158 | * a ->migrate_folio callback are possible to migrate |
| 1159 | * without blocking. |
| 1160 | * |
| 1161 | * Folios from inaccessible mappings are not migratable. |
| 1162 | * |
| 1163 | * However, we can be racing with truncation, which can |
| 1164 | * free the mapping that we need to check. Truncation |
| 1165 | * holds the folio lock until after the folio is removed |
| 1166 | * from the page so holding it ourselves is sufficient. |
| 1167 | * |
| 1168 | * To avoid locking the folio just to check inaccessible, |
| 1169 | * assume every inaccessible folio is also unevictable, |
| 1170 | * which is a cheaper test. If our assumption goes |
| 1171 | * wrong, it's not a correctness bug, just potentially |
| 1172 | * wasted cycles. |
| 1173 | */ |
| 1174 | if (!folio_trylock(folio)) |
| 1175 | goto isolate_fail_put; |
| 1176 | |
| 1177 | mapping = folio_mapping(folio); |
| 1178 | if ((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) { |
| 1179 | migrate_dirty = !mapping || |
| 1180 | mapping->a_ops->migrate_folio; |
| 1181 | } |
| 1182 | is_inaccessible = mapping && mapping_inaccessible(mapping); |
| 1183 | folio_unlock(folio); |
| 1184 | if (!migrate_dirty || is_inaccessible) |
| 1185 | goto isolate_fail_put; |
| 1186 | } |
| 1187 | |
| 1188 | /* Try isolate the folio */ |
| 1189 | if (!folio_test_clear_lru(folio)) |
| 1190 | goto isolate_fail_put; |
| 1191 | |
| 1192 | lruvec = folio_lruvec(folio); |
| 1193 | |
| 1194 | /* If we already hold the lock, we can skip some rechecking */ |
| 1195 | if (lruvec != locked) { |
| 1196 | if (locked) |
| 1197 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1198 | |
| 1199 | compact_lock_irqsave(&lruvec->lru_lock, &flags, cc); |
| 1200 | locked = lruvec; |
| 1201 | |
| 1202 | lruvec_memcg_debug(lruvec, folio); |
| 1203 | |
| 1204 | /* |
| 1205 | * Try get exclusive access under lock. If marked for |
| 1206 | * skip, the scan is aborted unless the current context |
| 1207 | * is a rescan to reach the end of the pageblock. |
| 1208 | */ |
| 1209 | if (!skip_updated && valid_page) { |
| 1210 | skip_updated = true; |
| 1211 | if (test_and_set_skip(cc, valid_page) && |
| 1212 | !cc->finish_pageblock) { |
| 1213 | low_pfn = end_pfn; |
| 1214 | goto isolate_abort; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Check LRU folio order under the lock |
| 1220 | */ |
| 1221 | if (unlikely(skip_isolation_on_order(folio_order(folio), |
| 1222 | cc->order) && |
| 1223 | !cc->alloc_contig)) { |
| 1224 | low_pfn += folio_nr_pages(folio) - 1; |
| 1225 | nr_scanned += folio_nr_pages(folio) - 1; |
| 1226 | folio_set_lru(folio); |
| 1227 | goto isolate_fail_put; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /* The folio is taken off the LRU */ |
| 1232 | if (folio_test_large(folio)) |
| 1233 | low_pfn += folio_nr_pages(folio) - 1; |
| 1234 | |
| 1235 | /* Successfully isolated */ |
| 1236 | lruvec_del_folio(lruvec, folio); |
| 1237 | node_stat_mod_folio(folio, |
| 1238 | NR_ISOLATED_ANON + folio_is_file_lru(folio), |
| 1239 | folio_nr_pages(folio)); |
| 1240 | |
| 1241 | isolate_success: |
| 1242 | list_add(&folio->lru, &cc->migratepages); |
| 1243 | isolate_success_no_list: |
| 1244 | cc->nr_migratepages += folio_nr_pages(folio); |
| 1245 | nr_isolated += folio_nr_pages(folio); |
| 1246 | nr_scanned += folio_nr_pages(folio) - 1; |
| 1247 | |
| 1248 | /* |
| 1249 | * Avoid isolating too much unless this block is being |
| 1250 | * fully scanned (e.g. dirty/writeback pages, parallel allocation) |
| 1251 | * or a lock is contended. For contention, isolate quickly to |
| 1252 | * potentially remove one source of contention. |
| 1253 | */ |
| 1254 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX && |
| 1255 | !cc->finish_pageblock && !cc->contended) { |
| 1256 | ++low_pfn; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | continue; |
| 1261 | |
| 1262 | isolate_fail_put: |
| 1263 | /* Avoid potential deadlock in freeing page under lru_lock */ |
| 1264 | if (locked) { |
| 1265 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1266 | locked = NULL; |
| 1267 | } |
| 1268 | folio_put(folio); |
| 1269 | |
| 1270 | isolate_fail: |
| 1271 | if (!skip_on_failure && ret != -ENOMEM) |
| 1272 | continue; |
| 1273 | |
| 1274 | /* |
| 1275 | * We have isolated some pages, but then failed. Release them |
| 1276 | * instead of migrating, as we cannot form the cc->order buddy |
| 1277 | * page anyway. |
| 1278 | */ |
| 1279 | if (nr_isolated) { |
| 1280 | if (locked) { |
| 1281 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1282 | locked = NULL; |
| 1283 | } |
| 1284 | putback_movable_pages(&cc->migratepages); |
| 1285 | cc->nr_migratepages = 0; |
| 1286 | nr_isolated = 0; |
| 1287 | } |
| 1288 | |
| 1289 | if (low_pfn < next_skip_pfn) { |
| 1290 | low_pfn = next_skip_pfn - 1; |
| 1291 | /* |
| 1292 | * The check near the loop beginning would have updated |
| 1293 | * next_skip_pfn too, but this is a bit simpler. |
| 1294 | */ |
| 1295 | next_skip_pfn += 1UL << cc->order; |
| 1296 | } |
| 1297 | |
| 1298 | if (ret == -ENOMEM) |
| 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * The PageBuddy() check could have potentially brought us outside |
| 1304 | * the range to be scanned. |
| 1305 | */ |
| 1306 | if (unlikely(low_pfn > end_pfn)) |
| 1307 | low_pfn = end_pfn; |
| 1308 | |
| 1309 | folio = NULL; |
| 1310 | |
| 1311 | isolate_abort: |
| 1312 | if (locked) |
| 1313 | unlock_page_lruvec_irqrestore(locked, flags); |
| 1314 | if (folio) { |
| 1315 | folio_set_lru(folio); |
| 1316 | folio_put(folio); |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * Update the cached scanner pfn once the pageblock has been scanned. |
| 1321 | * Pages will either be migrated in which case there is no point |
| 1322 | * scanning in the near future or migration failed in which case the |
| 1323 | * failure reason may persist. The block is marked for skipping if |
| 1324 | * there were no pages isolated in the block or if the block is |
| 1325 | * rescanned twice in a row. |
| 1326 | */ |
| 1327 | if (low_pfn == end_pfn && (!nr_isolated || cc->finish_pageblock)) { |
| 1328 | if (!cc->no_set_skip_hint && valid_page && !skip_updated) |
| 1329 | set_pageblock_skip(valid_page); |
| 1330 | update_cached_migrate(cc, low_pfn); |
| 1331 | } |
| 1332 | |
| 1333 | trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn, |
| 1334 | nr_scanned, nr_isolated); |
| 1335 | |
| 1336 | fatal_pending: |
| 1337 | cc->total_migrate_scanned += nr_scanned; |
| 1338 | if (nr_isolated) |
| 1339 | count_compact_events(COMPACTISOLATED, nr_isolated); |
| 1340 | |
| 1341 | cc->migrate_pfn = low_pfn; |
| 1342 | |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | /** |
| 1347 | * isolate_migratepages_range() - isolate migrate-able pages in a PFN range |
| 1348 | * @cc: Compaction control structure. |
| 1349 | * @start_pfn: The first PFN to start isolating. |
| 1350 | * @end_pfn: The one-past-last PFN. |
| 1351 | * |
| 1352 | * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM |
| 1353 | * in case we could not allocate a page, or 0. |
| 1354 | */ |
| 1355 | int |
| 1356 | isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn, |
| 1357 | unsigned long end_pfn) |
| 1358 | { |
| 1359 | unsigned long pfn, block_start_pfn, block_end_pfn; |
| 1360 | int ret = 0; |
| 1361 | |
| 1362 | /* Scan block by block. First and last block may be incomplete */ |
| 1363 | pfn = start_pfn; |
| 1364 | block_start_pfn = pageblock_start_pfn(pfn); |
| 1365 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 1366 | block_start_pfn = cc->zone->zone_start_pfn; |
| 1367 | block_end_pfn = pageblock_end_pfn(pfn); |
| 1368 | |
| 1369 | for (; pfn < end_pfn; pfn = block_end_pfn, |
| 1370 | block_start_pfn = block_end_pfn, |
| 1371 | block_end_pfn += pageblock_nr_pages) { |
| 1372 | |
| 1373 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 1374 | |
| 1375 | if (!pageblock_pfn_to_page(block_start_pfn, |
| 1376 | block_end_pfn, cc->zone)) |
| 1377 | continue; |
| 1378 | |
| 1379 | ret = isolate_migratepages_block(cc, pfn, block_end_pfn, |
| 1380 | ISOLATE_UNEVICTABLE); |
| 1381 | |
| 1382 | if (ret) |
| 1383 | break; |
| 1384 | |
| 1385 | if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX) |
| 1386 | break; |
| 1387 | } |
| 1388 | |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
| 1392 | #endif /* CONFIG_COMPACTION || CONFIG_CMA */ |
| 1393 | #ifdef CONFIG_COMPACTION |
| 1394 | |
| 1395 | static bool suitable_migration_source(struct compact_control *cc, |
| 1396 | struct page *page) |
| 1397 | { |
| 1398 | int block_mt; |
| 1399 | |
| 1400 | if (pageblock_skip_persistent(page)) |
| 1401 | return false; |
| 1402 | |
| 1403 | if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction) |
| 1404 | return true; |
| 1405 | |
| 1406 | block_mt = get_pageblock_migratetype(page); |
| 1407 | |
| 1408 | if (cc->migratetype == MIGRATE_MOVABLE) |
| 1409 | return is_migrate_movable(block_mt); |
| 1410 | else |
| 1411 | return block_mt == cc->migratetype; |
| 1412 | } |
| 1413 | |
| 1414 | /* Returns true if the page is within a block suitable for migration to */ |
| 1415 | static bool suitable_migration_target(struct compact_control *cc, |
| 1416 | struct page *page) |
| 1417 | { |
| 1418 | /* If the page is a large free page, then disallow migration */ |
| 1419 | if (PageBuddy(page)) { |
| 1420 | int order = cc->order > 0 ? cc->order : pageblock_order; |
| 1421 | |
| 1422 | /* |
| 1423 | * We are checking page_order without zone->lock taken. But |
| 1424 | * the only small danger is that we skip a potentially suitable |
| 1425 | * pageblock, so it's not worth to check order for valid range. |
| 1426 | */ |
| 1427 | if (buddy_order_unsafe(page) >= order) |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | if (cc->ignore_block_suitable) |
| 1432 | return true; |
| 1433 | |
| 1434 | /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */ |
| 1435 | if (is_migrate_movable(get_pageblock_migratetype(page))) |
| 1436 | return true; |
| 1437 | |
| 1438 | /* Otherwise skip the block */ |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | static inline unsigned int |
| 1443 | freelist_scan_limit(struct compact_control *cc) |
| 1444 | { |
| 1445 | unsigned short shift = BITS_PER_LONG - 1; |
| 1446 | |
| 1447 | return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Test whether the free scanner has reached the same or lower pageblock than |
| 1452 | * the migration scanner, and compaction should thus terminate. |
| 1453 | */ |
| 1454 | static inline bool compact_scanners_met(struct compact_control *cc) |
| 1455 | { |
| 1456 | return (cc->free_pfn >> pageblock_order) |
| 1457 | <= (cc->migrate_pfn >> pageblock_order); |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * Used when scanning for a suitable migration target which scans freelists |
| 1462 | * in reverse. Reorders the list such as the unscanned pages are scanned |
| 1463 | * first on the next iteration of the free scanner |
| 1464 | */ |
| 1465 | static void |
| 1466 | move_freelist_head(struct list_head *freelist, struct page *freepage) |
| 1467 | { |
| 1468 | LIST_HEAD(sublist); |
| 1469 | |
| 1470 | if (!list_is_first(&freepage->buddy_list, freelist)) { |
| 1471 | list_cut_before(&sublist, freelist, &freepage->buddy_list); |
| 1472 | list_splice_tail(&sublist, freelist); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Similar to move_freelist_head except used by the migration scanner |
| 1478 | * when scanning forward. It's possible for these list operations to |
| 1479 | * move against each other if they search the free list exactly in |
| 1480 | * lockstep. |
| 1481 | */ |
| 1482 | static void |
| 1483 | move_freelist_tail(struct list_head *freelist, struct page *freepage) |
| 1484 | { |
| 1485 | LIST_HEAD(sublist); |
| 1486 | |
| 1487 | if (!list_is_last(&freepage->buddy_list, freelist)) { |
| 1488 | list_cut_position(&sublist, freelist, &freepage->buddy_list); |
| 1489 | list_splice_tail(&sublist, freelist); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | static void |
| 1494 | fast_isolate_around(struct compact_control *cc, unsigned long pfn) |
| 1495 | { |
| 1496 | unsigned long start_pfn, end_pfn; |
| 1497 | struct page *page; |
| 1498 | |
| 1499 | /* Do not search around if there are enough pages already */ |
| 1500 | if (cc->nr_freepages >= cc->nr_migratepages) |
| 1501 | return; |
| 1502 | |
| 1503 | /* Minimise scanning during async compaction */ |
| 1504 | if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC) |
| 1505 | return; |
| 1506 | |
| 1507 | /* Pageblock boundaries */ |
| 1508 | start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn); |
| 1509 | end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)); |
| 1510 | |
| 1511 | page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone); |
| 1512 | if (!page) |
| 1513 | return; |
| 1514 | |
| 1515 | isolate_freepages_block(cc, &start_pfn, end_pfn, cc->freepages, 1, false); |
| 1516 | |
| 1517 | /* Skip this pageblock in the future as it's full or nearly full */ |
| 1518 | if (start_pfn == end_pfn && !cc->no_set_skip_hint) |
| 1519 | set_pageblock_skip(page); |
| 1520 | } |
| 1521 | |
| 1522 | /* Search orders in round-robin fashion */ |
| 1523 | static int next_search_order(struct compact_control *cc, int order) |
| 1524 | { |
| 1525 | order--; |
| 1526 | if (order < 0) |
| 1527 | order = cc->order - 1; |
| 1528 | |
| 1529 | /* Search wrapped around? */ |
| 1530 | if (order == cc->search_order) { |
| 1531 | cc->search_order--; |
| 1532 | if (cc->search_order < 0) |
| 1533 | cc->search_order = cc->order - 1; |
| 1534 | return -1; |
| 1535 | } |
| 1536 | |
| 1537 | return order; |
| 1538 | } |
| 1539 | |
| 1540 | static void fast_isolate_freepages(struct compact_control *cc) |
| 1541 | { |
| 1542 | unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1); |
| 1543 | unsigned int nr_scanned = 0, total_isolated = 0; |
| 1544 | unsigned long low_pfn, min_pfn, highest = 0; |
| 1545 | unsigned long nr_isolated = 0; |
| 1546 | unsigned long distance; |
| 1547 | struct page *page = NULL; |
| 1548 | bool scan_start = false; |
| 1549 | int order; |
| 1550 | |
| 1551 | /* Full compaction passes in a negative order */ |
| 1552 | if (cc->order <= 0) |
| 1553 | return; |
| 1554 | |
| 1555 | /* |
| 1556 | * If starting the scan, use a deeper search and use the highest |
| 1557 | * PFN found if a suitable one is not found. |
| 1558 | */ |
| 1559 | if (cc->free_pfn >= cc->zone->compact_init_free_pfn) { |
| 1560 | limit = pageblock_nr_pages >> 1; |
| 1561 | scan_start = true; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * Preferred point is in the top quarter of the scan space but take |
| 1566 | * a pfn from the top half if the search is problematic. |
| 1567 | */ |
| 1568 | distance = (cc->free_pfn - cc->migrate_pfn); |
| 1569 | low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2)); |
| 1570 | min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1)); |
| 1571 | |
| 1572 | if (WARN_ON_ONCE(min_pfn > low_pfn)) |
| 1573 | low_pfn = min_pfn; |
| 1574 | |
| 1575 | /* |
| 1576 | * Search starts from the last successful isolation order or the next |
| 1577 | * order to search after a previous failure |
| 1578 | */ |
| 1579 | cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order); |
| 1580 | |
| 1581 | for (order = cc->search_order; |
| 1582 | !page && order >= 0; |
| 1583 | order = next_search_order(cc, order)) { |
| 1584 | struct free_area *area = &cc->zone->free_area[order]; |
| 1585 | struct list_head *freelist; |
| 1586 | struct page *freepage; |
| 1587 | unsigned long flags; |
| 1588 | unsigned int order_scanned = 0; |
| 1589 | unsigned long high_pfn = 0; |
| 1590 | |
| 1591 | if (!area->nr_free) |
| 1592 | continue; |
| 1593 | |
| 1594 | spin_lock_irqsave(&cc->zone->lock, flags); |
| 1595 | freelist = &area->free_list[MIGRATE_MOVABLE]; |
| 1596 | list_for_each_entry_reverse(freepage, freelist, buddy_list) { |
| 1597 | unsigned long pfn; |
| 1598 | |
| 1599 | order_scanned++; |
| 1600 | nr_scanned++; |
| 1601 | pfn = page_to_pfn(freepage); |
| 1602 | |
| 1603 | if (pfn >= highest) |
| 1604 | highest = max(pageblock_start_pfn(pfn), |
| 1605 | cc->zone->zone_start_pfn); |
| 1606 | |
| 1607 | if (pfn >= low_pfn) { |
| 1608 | cc->fast_search_fail = 0; |
| 1609 | cc->search_order = order; |
| 1610 | page = freepage; |
| 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | if (pfn >= min_pfn && pfn > high_pfn) { |
| 1615 | high_pfn = pfn; |
| 1616 | |
| 1617 | /* Shorten the scan if a candidate is found */ |
| 1618 | limit >>= 1; |
| 1619 | } |
| 1620 | |
| 1621 | if (order_scanned >= limit) |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | /* Use a maximum candidate pfn if a preferred one was not found */ |
| 1626 | if (!page && high_pfn) { |
| 1627 | page = pfn_to_page(high_pfn); |
| 1628 | |
| 1629 | /* Update freepage for the list reorder below */ |
| 1630 | freepage = page; |
| 1631 | } |
| 1632 | |
| 1633 | /* Reorder to so a future search skips recent pages */ |
| 1634 | move_freelist_head(freelist, freepage); |
| 1635 | |
| 1636 | /* Isolate the page if available */ |
| 1637 | if (page) { |
| 1638 | if (__isolate_free_page(page, order)) { |
| 1639 | set_page_private(page, order); |
| 1640 | nr_isolated = 1 << order; |
| 1641 | nr_scanned += nr_isolated - 1; |
| 1642 | total_isolated += nr_isolated; |
| 1643 | cc->nr_freepages += nr_isolated; |
| 1644 | list_add_tail(&page->lru, &cc->freepages[order]); |
| 1645 | count_compact_events(COMPACTISOLATED, nr_isolated); |
| 1646 | } else { |
| 1647 | /* If isolation fails, abort the search */ |
| 1648 | order = cc->search_order + 1; |
| 1649 | page = NULL; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 1654 | |
| 1655 | /* Skip fast search if enough freepages isolated */ |
| 1656 | if (cc->nr_freepages >= cc->nr_migratepages) |
| 1657 | break; |
| 1658 | |
| 1659 | /* |
| 1660 | * Smaller scan on next order so the total scan is related |
| 1661 | * to freelist_scan_limit. |
| 1662 | */ |
| 1663 | if (order_scanned >= limit) |
| 1664 | limit = max(1U, limit >> 1); |
| 1665 | } |
| 1666 | |
| 1667 | trace_mm_compaction_fast_isolate_freepages(min_pfn, cc->free_pfn, |
| 1668 | nr_scanned, total_isolated); |
| 1669 | |
| 1670 | if (!page) { |
| 1671 | cc->fast_search_fail++; |
| 1672 | if (scan_start) { |
| 1673 | /* |
| 1674 | * Use the highest PFN found above min. If one was |
| 1675 | * not found, be pessimistic for direct compaction |
| 1676 | * and use the min mark. |
| 1677 | */ |
| 1678 | if (highest >= min_pfn) { |
| 1679 | page = pfn_to_page(highest); |
| 1680 | cc->free_pfn = highest; |
| 1681 | } else { |
| 1682 | if (cc->direct_compaction && pfn_valid(min_pfn)) { |
| 1683 | page = pageblock_pfn_to_page(min_pfn, |
| 1684 | min(pageblock_end_pfn(min_pfn), |
| 1685 | zone_end_pfn(cc->zone)), |
| 1686 | cc->zone); |
| 1687 | if (page && !suitable_migration_target(cc, page)) |
| 1688 | page = NULL; |
| 1689 | |
| 1690 | cc->free_pfn = min_pfn; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | if (highest && highest >= cc->zone->compact_cached_free_pfn) { |
| 1697 | highest -= pageblock_nr_pages; |
| 1698 | cc->zone->compact_cached_free_pfn = highest; |
| 1699 | } |
| 1700 | |
| 1701 | cc->total_free_scanned += nr_scanned; |
| 1702 | if (!page) |
| 1703 | return; |
| 1704 | |
| 1705 | low_pfn = page_to_pfn(page); |
| 1706 | fast_isolate_around(cc, low_pfn); |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * Based on information in the current compact_control, find blocks |
| 1711 | * suitable for isolating free pages from and then isolate them. |
| 1712 | */ |
| 1713 | static void isolate_freepages(struct compact_control *cc) |
| 1714 | { |
| 1715 | struct zone *zone = cc->zone; |
| 1716 | struct page *page; |
| 1717 | unsigned long block_start_pfn; /* start of current pageblock */ |
| 1718 | unsigned long isolate_start_pfn; /* exact pfn we start at */ |
| 1719 | unsigned long block_end_pfn; /* end of current pageblock */ |
| 1720 | unsigned long low_pfn; /* lowest pfn scanner is able to scan */ |
| 1721 | unsigned int stride; |
| 1722 | |
| 1723 | /* Try a small search of the free lists for a candidate */ |
| 1724 | fast_isolate_freepages(cc); |
| 1725 | if (cc->nr_freepages) |
| 1726 | return; |
| 1727 | |
| 1728 | /* |
| 1729 | * Initialise the free scanner. The starting point is where we last |
| 1730 | * successfully isolated from, zone-cached value, or the end of the |
| 1731 | * zone when isolating for the first time. For looping we also need |
| 1732 | * this pfn aligned down to the pageblock boundary, because we do |
| 1733 | * block_start_pfn -= pageblock_nr_pages in the for loop. |
| 1734 | * For ending point, take care when isolating in last pageblock of a |
| 1735 | * zone which ends in the middle of a pageblock. |
| 1736 | * The low boundary is the end of the pageblock the migration scanner |
| 1737 | * is using. |
| 1738 | */ |
| 1739 | isolate_start_pfn = cc->free_pfn; |
| 1740 | block_start_pfn = pageblock_start_pfn(isolate_start_pfn); |
| 1741 | block_end_pfn = min(block_start_pfn + pageblock_nr_pages, |
| 1742 | zone_end_pfn(zone)); |
| 1743 | low_pfn = pageblock_end_pfn(cc->migrate_pfn); |
| 1744 | stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1; |
| 1745 | |
| 1746 | /* |
| 1747 | * Isolate free pages until enough are available to migrate the |
| 1748 | * pages on cc->migratepages. We stop searching if the migrate |
| 1749 | * and free page scanners meet or enough free pages are isolated. |
| 1750 | */ |
| 1751 | for (; block_start_pfn >= low_pfn; |
| 1752 | block_end_pfn = block_start_pfn, |
| 1753 | block_start_pfn -= pageblock_nr_pages, |
| 1754 | isolate_start_pfn = block_start_pfn) { |
| 1755 | unsigned long nr_isolated; |
| 1756 | |
| 1757 | /* |
| 1758 | * This can iterate a massively long zone without finding any |
| 1759 | * suitable migration targets, so periodically check resched. |
| 1760 | */ |
| 1761 | if (!(block_start_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) |
| 1762 | cond_resched(); |
| 1763 | |
| 1764 | page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn, |
| 1765 | zone); |
| 1766 | if (!page) { |
| 1767 | unsigned long next_pfn; |
| 1768 | |
| 1769 | next_pfn = skip_offline_sections_reverse(block_start_pfn); |
| 1770 | if (next_pfn) |
| 1771 | block_start_pfn = max(next_pfn, low_pfn); |
| 1772 | |
| 1773 | continue; |
| 1774 | } |
| 1775 | |
| 1776 | /* Check the block is suitable for migration */ |
| 1777 | if (!suitable_migration_target(cc, page)) |
| 1778 | continue; |
| 1779 | |
| 1780 | /* If isolation recently failed, do not retry */ |
| 1781 | if (!isolation_suitable(cc, page)) |
| 1782 | continue; |
| 1783 | |
| 1784 | /* Found a block suitable for isolating free pages from. */ |
| 1785 | nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn, |
| 1786 | block_end_pfn, cc->freepages, stride, false); |
| 1787 | |
| 1788 | /* Update the skip hint if the full pageblock was scanned */ |
| 1789 | if (isolate_start_pfn == block_end_pfn) |
| 1790 | update_pageblock_skip(cc, page, block_start_pfn - |
| 1791 | pageblock_nr_pages); |
| 1792 | |
| 1793 | /* Are enough freepages isolated? */ |
| 1794 | if (cc->nr_freepages >= cc->nr_migratepages) { |
| 1795 | if (isolate_start_pfn >= block_end_pfn) { |
| 1796 | /* |
| 1797 | * Restart at previous pageblock if more |
| 1798 | * freepages can be isolated next time. |
| 1799 | */ |
| 1800 | isolate_start_pfn = |
| 1801 | block_start_pfn - pageblock_nr_pages; |
| 1802 | } |
| 1803 | break; |
| 1804 | } else if (isolate_start_pfn < block_end_pfn) { |
| 1805 | /* |
| 1806 | * If isolation failed early, do not continue |
| 1807 | * needlessly. |
| 1808 | */ |
| 1809 | break; |
| 1810 | } |
| 1811 | |
| 1812 | /* Adjust stride depending on isolation */ |
| 1813 | if (nr_isolated) { |
| 1814 | stride = 1; |
| 1815 | continue; |
| 1816 | } |
| 1817 | stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1); |
| 1818 | } |
| 1819 | |
| 1820 | /* |
| 1821 | * Record where the free scanner will restart next time. Either we |
| 1822 | * broke from the loop and set isolate_start_pfn based on the last |
| 1823 | * call to isolate_freepages_block(), or we met the migration scanner |
| 1824 | * and the loop terminated due to isolate_start_pfn < low_pfn |
| 1825 | */ |
| 1826 | cc->free_pfn = isolate_start_pfn; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * This is a migrate-callback that "allocates" freepages by taking pages |
| 1831 | * from the isolated freelists in the block we are migrating to. |
| 1832 | */ |
| 1833 | static struct folio *compaction_alloc_noprof(struct folio *src, unsigned long data) |
| 1834 | { |
| 1835 | struct compact_control *cc = (struct compact_control *)data; |
| 1836 | struct folio *dst; |
| 1837 | int order = folio_order(src); |
| 1838 | bool has_isolated_pages = false; |
| 1839 | int start_order; |
| 1840 | struct page *freepage; |
| 1841 | unsigned long size; |
| 1842 | |
| 1843 | again: |
| 1844 | for (start_order = order; start_order < NR_PAGE_ORDERS; start_order++) |
| 1845 | if (!list_empty(&cc->freepages[start_order])) |
| 1846 | break; |
| 1847 | |
| 1848 | /* no free pages in the list */ |
| 1849 | if (start_order == NR_PAGE_ORDERS) { |
| 1850 | if (has_isolated_pages) |
| 1851 | return NULL; |
| 1852 | isolate_freepages(cc); |
| 1853 | has_isolated_pages = true; |
| 1854 | goto again; |
| 1855 | } |
| 1856 | |
| 1857 | freepage = list_first_entry(&cc->freepages[start_order], struct page, |
| 1858 | lru); |
| 1859 | size = 1 << start_order; |
| 1860 | |
| 1861 | list_del(&freepage->lru); |
| 1862 | |
| 1863 | while (start_order > order) { |
| 1864 | start_order--; |
| 1865 | size >>= 1; |
| 1866 | |
| 1867 | list_add(&freepage[size].lru, &cc->freepages[start_order]); |
| 1868 | set_page_private(&freepage[size], start_order); |
| 1869 | } |
| 1870 | dst = (struct folio *)freepage; |
| 1871 | |
| 1872 | post_alloc_hook(&dst->page, order, __GFP_MOVABLE); |
| 1873 | set_page_refcounted(&dst->page); |
| 1874 | if (order) |
| 1875 | prep_compound_page(&dst->page, order); |
| 1876 | cc->nr_freepages -= 1 << order; |
| 1877 | cc->nr_migratepages -= 1 << order; |
| 1878 | return page_rmappable_folio(&dst->page); |
| 1879 | } |
| 1880 | |
| 1881 | static struct folio *compaction_alloc(struct folio *src, unsigned long data) |
| 1882 | { |
| 1883 | return alloc_hooks(compaction_alloc_noprof(src, data)); |
| 1884 | } |
| 1885 | |
| 1886 | /* |
| 1887 | * This is a migrate-callback that "frees" freepages back to the isolated |
| 1888 | * freelist. All pages on the freelist are from the same zone, so there is no |
| 1889 | * special handling needed for NUMA. |
| 1890 | */ |
| 1891 | static void compaction_free(struct folio *dst, unsigned long data) |
| 1892 | { |
| 1893 | struct compact_control *cc = (struct compact_control *)data; |
| 1894 | int order = folio_order(dst); |
| 1895 | struct page *page = &dst->page; |
| 1896 | |
| 1897 | if (folio_put_testzero(dst)) { |
| 1898 | free_pages_prepare(page, order); |
| 1899 | list_add(&dst->lru, &cc->freepages[order]); |
| 1900 | cc->nr_freepages += 1 << order; |
| 1901 | } |
| 1902 | cc->nr_migratepages += 1 << order; |
| 1903 | /* |
| 1904 | * someone else has referenced the page, we cannot take it back to our |
| 1905 | * free list. |
| 1906 | */ |
| 1907 | } |
| 1908 | |
| 1909 | /* possible outcome of isolate_migratepages */ |
| 1910 | typedef enum { |
| 1911 | ISOLATE_ABORT, /* Abort compaction now */ |
| 1912 | ISOLATE_NONE, /* No pages isolated, continue scanning */ |
| 1913 | ISOLATE_SUCCESS, /* Pages isolated, migrate */ |
| 1914 | } isolate_migrate_t; |
| 1915 | |
| 1916 | /* |
| 1917 | * Allow userspace to control policy on scanning the unevictable LRU for |
| 1918 | * compactable pages. |
| 1919 | */ |
| 1920 | static int sysctl_compact_unevictable_allowed __read_mostly = CONFIG_COMPACT_UNEVICTABLE_DEFAULT; |
| 1921 | /* |
| 1922 | * Tunable for proactive compaction. It determines how |
| 1923 | * aggressively the kernel should compact memory in the |
| 1924 | * background. It takes values in the range [0, 100]. |
| 1925 | */ |
| 1926 | static unsigned int __read_mostly sysctl_compaction_proactiveness = 20; |
| 1927 | static int sysctl_extfrag_threshold = 500; |
| 1928 | static int __read_mostly sysctl_compact_memory; |
| 1929 | |
| 1930 | static inline void |
| 1931 | update_fast_start_pfn(struct compact_control *cc, unsigned long pfn) |
| 1932 | { |
| 1933 | if (cc->fast_start_pfn == ULONG_MAX) |
| 1934 | return; |
| 1935 | |
| 1936 | if (!cc->fast_start_pfn) |
| 1937 | cc->fast_start_pfn = pfn; |
| 1938 | |
| 1939 | cc->fast_start_pfn = min(cc->fast_start_pfn, pfn); |
| 1940 | } |
| 1941 | |
| 1942 | static inline unsigned long |
| 1943 | reinit_migrate_pfn(struct compact_control *cc) |
| 1944 | { |
| 1945 | if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX) |
| 1946 | return cc->migrate_pfn; |
| 1947 | |
| 1948 | cc->migrate_pfn = cc->fast_start_pfn; |
| 1949 | cc->fast_start_pfn = ULONG_MAX; |
| 1950 | |
| 1951 | return cc->migrate_pfn; |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * Briefly search the free lists for a migration source that already has |
| 1956 | * some free pages to reduce the number of pages that need migration |
| 1957 | * before a pageblock is free. |
| 1958 | */ |
| 1959 | static unsigned long fast_find_migrateblock(struct compact_control *cc) |
| 1960 | { |
| 1961 | unsigned int limit = freelist_scan_limit(cc); |
| 1962 | unsigned int nr_scanned = 0; |
| 1963 | unsigned long distance; |
| 1964 | unsigned long pfn = cc->migrate_pfn; |
| 1965 | unsigned long high_pfn; |
| 1966 | int order; |
| 1967 | bool found_block = false; |
| 1968 | |
| 1969 | /* Skip hints are relied on to avoid repeats on the fast search */ |
| 1970 | if (cc->ignore_skip_hint) |
| 1971 | return pfn; |
| 1972 | |
| 1973 | /* |
| 1974 | * If the pageblock should be finished then do not select a different |
| 1975 | * pageblock. |
| 1976 | */ |
| 1977 | if (cc->finish_pageblock) |
| 1978 | return pfn; |
| 1979 | |
| 1980 | /* |
| 1981 | * If the migrate_pfn is not at the start of a zone or the start |
| 1982 | * of a pageblock then assume this is a continuation of a previous |
| 1983 | * scan restarted due to COMPACT_CLUSTER_MAX. |
| 1984 | */ |
| 1985 | if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn)) |
| 1986 | return pfn; |
| 1987 | |
| 1988 | /* |
| 1989 | * For smaller orders, just linearly scan as the number of pages |
| 1990 | * to migrate should be relatively small and does not necessarily |
| 1991 | * justify freeing up a large block for a small allocation. |
| 1992 | */ |
| 1993 | if (cc->order <= PAGE_ALLOC_COSTLY_ORDER) |
| 1994 | return pfn; |
| 1995 | |
| 1996 | /* |
| 1997 | * Only allow kcompactd and direct requests for movable pages to |
| 1998 | * quickly clear out a MOVABLE pageblock for allocation. This |
| 1999 | * reduces the risk that a large movable pageblock is freed for |
| 2000 | * an unmovable/reclaimable small allocation. |
| 2001 | */ |
| 2002 | if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE) |
| 2003 | return pfn; |
| 2004 | |
| 2005 | /* |
| 2006 | * When starting the migration scanner, pick any pageblock within the |
| 2007 | * first half of the search space. Otherwise try and pick a pageblock |
| 2008 | * within the first eighth to reduce the chances that a migration |
| 2009 | * target later becomes a source. |
| 2010 | */ |
| 2011 | distance = (cc->free_pfn - cc->migrate_pfn) >> 1; |
| 2012 | if (cc->migrate_pfn != cc->zone->zone_start_pfn) |
| 2013 | distance >>= 2; |
| 2014 | high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance); |
| 2015 | |
| 2016 | for (order = cc->order - 1; |
| 2017 | order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit; |
| 2018 | order--) { |
| 2019 | struct free_area *area = &cc->zone->free_area[order]; |
| 2020 | struct list_head *freelist; |
| 2021 | unsigned long flags; |
| 2022 | struct page *freepage; |
| 2023 | |
| 2024 | if (!area->nr_free) |
| 2025 | continue; |
| 2026 | |
| 2027 | spin_lock_irqsave(&cc->zone->lock, flags); |
| 2028 | freelist = &area->free_list[MIGRATE_MOVABLE]; |
| 2029 | list_for_each_entry(freepage, freelist, buddy_list) { |
| 2030 | unsigned long free_pfn; |
| 2031 | |
| 2032 | if (nr_scanned++ >= limit) { |
| 2033 | move_freelist_tail(freelist, freepage); |
| 2034 | break; |
| 2035 | } |
| 2036 | |
| 2037 | free_pfn = page_to_pfn(freepage); |
| 2038 | if (free_pfn < high_pfn) { |
| 2039 | /* |
| 2040 | * Avoid if skipped recently. Ideally it would |
| 2041 | * move to the tail but even safe iteration of |
| 2042 | * the list assumes an entry is deleted, not |
| 2043 | * reordered. |
| 2044 | */ |
| 2045 | if (get_pageblock_skip(freepage)) |
| 2046 | continue; |
| 2047 | |
| 2048 | /* Reorder to so a future search skips recent pages */ |
| 2049 | move_freelist_tail(freelist, freepage); |
| 2050 | |
| 2051 | update_fast_start_pfn(cc, free_pfn); |
| 2052 | pfn = pageblock_start_pfn(free_pfn); |
| 2053 | if (pfn < cc->zone->zone_start_pfn) |
| 2054 | pfn = cc->zone->zone_start_pfn; |
| 2055 | cc->fast_search_fail = 0; |
| 2056 | found_block = true; |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | spin_unlock_irqrestore(&cc->zone->lock, flags); |
| 2061 | } |
| 2062 | |
| 2063 | cc->total_migrate_scanned += nr_scanned; |
| 2064 | |
| 2065 | /* |
| 2066 | * If fast scanning failed then use a cached entry for a page block |
| 2067 | * that had free pages as the basis for starting a linear scan. |
| 2068 | */ |
| 2069 | if (!found_block) { |
| 2070 | cc->fast_search_fail++; |
| 2071 | pfn = reinit_migrate_pfn(cc); |
| 2072 | } |
| 2073 | return pfn; |
| 2074 | } |
| 2075 | |
| 2076 | /* |
| 2077 | * Isolate all pages that can be migrated from the first suitable block, |
| 2078 | * starting at the block pointed to by the migrate scanner pfn within |
| 2079 | * compact_control. |
| 2080 | */ |
| 2081 | static isolate_migrate_t isolate_migratepages(struct compact_control *cc) |
| 2082 | { |
| 2083 | unsigned long block_start_pfn; |
| 2084 | unsigned long block_end_pfn; |
| 2085 | unsigned long low_pfn; |
| 2086 | struct page *page; |
| 2087 | const isolate_mode_t isolate_mode = |
| 2088 | (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) | |
| 2089 | (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0); |
| 2090 | bool fast_find_block; |
| 2091 | |
| 2092 | /* |
| 2093 | * Start at where we last stopped, or beginning of the zone as |
| 2094 | * initialized by compact_zone(). The first failure will use |
| 2095 | * the lowest PFN as the starting point for linear scanning. |
| 2096 | */ |
| 2097 | low_pfn = fast_find_migrateblock(cc); |
| 2098 | block_start_pfn = pageblock_start_pfn(low_pfn); |
| 2099 | if (block_start_pfn < cc->zone->zone_start_pfn) |
| 2100 | block_start_pfn = cc->zone->zone_start_pfn; |
| 2101 | |
| 2102 | /* |
| 2103 | * fast_find_migrateblock() has already ensured the pageblock is not |
| 2104 | * set with a skipped flag, so to avoid the isolation_suitable check |
| 2105 | * below again, check whether the fast search was successful. |
| 2106 | */ |
| 2107 | fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail; |
| 2108 | |
| 2109 | /* Only scan within a pageblock boundary */ |
| 2110 | block_end_pfn = pageblock_end_pfn(low_pfn); |
| 2111 | |
| 2112 | /* |
| 2113 | * Iterate over whole pageblocks until we find the first suitable. |
| 2114 | * Do not cross the free scanner. |
| 2115 | */ |
| 2116 | for (; block_end_pfn <= cc->free_pfn; |
| 2117 | fast_find_block = false, |
| 2118 | cc->migrate_pfn = low_pfn = block_end_pfn, |
| 2119 | block_start_pfn = block_end_pfn, |
| 2120 | block_end_pfn += pageblock_nr_pages) { |
| 2121 | |
| 2122 | /* |
| 2123 | * This can potentially iterate a massively long zone with |
| 2124 | * many pageblocks unsuitable, so periodically check if we |
| 2125 | * need to schedule. |
| 2126 | */ |
| 2127 | if (!(low_pfn % (COMPACT_CLUSTER_MAX * pageblock_nr_pages))) |
| 2128 | cond_resched(); |
| 2129 | |
| 2130 | page = pageblock_pfn_to_page(block_start_pfn, |
| 2131 | block_end_pfn, cc->zone); |
| 2132 | if (!page) { |
| 2133 | unsigned long next_pfn; |
| 2134 | |
| 2135 | next_pfn = skip_offline_sections(block_start_pfn); |
| 2136 | if (next_pfn) |
| 2137 | block_end_pfn = min(next_pfn, cc->free_pfn); |
| 2138 | continue; |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * If isolation recently failed, do not retry. Only check the |
| 2143 | * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock |
| 2144 | * to be visited multiple times. Assume skip was checked |
| 2145 | * before making it "skip" so other compaction instances do |
| 2146 | * not scan the same block. |
| 2147 | */ |
| 2148 | if ((pageblock_aligned(low_pfn) || |
| 2149 | low_pfn == cc->zone->zone_start_pfn) && |
| 2150 | !fast_find_block && !isolation_suitable(cc, page)) |
| 2151 | continue; |
| 2152 | |
| 2153 | /* |
| 2154 | * For async direct compaction, only scan the pageblocks of the |
| 2155 | * same migratetype without huge pages. Async direct compaction |
| 2156 | * is optimistic to see if the minimum amount of work satisfies |
| 2157 | * the allocation. The cached PFN is updated as it's possible |
| 2158 | * that all remaining blocks between source and target are |
| 2159 | * unsuitable and the compaction scanners fail to meet. |
| 2160 | */ |
| 2161 | if (!suitable_migration_source(cc, page)) { |
| 2162 | update_cached_migrate(cc, block_end_pfn); |
| 2163 | continue; |
| 2164 | } |
| 2165 | |
| 2166 | /* Perform the isolation */ |
| 2167 | if (isolate_migratepages_block(cc, low_pfn, block_end_pfn, |
| 2168 | isolate_mode)) |
| 2169 | return ISOLATE_ABORT; |
| 2170 | |
| 2171 | /* |
| 2172 | * Either we isolated something and proceed with migration. Or |
| 2173 | * we failed and compact_zone should decide if we should |
| 2174 | * continue or not. |
| 2175 | */ |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; |
| 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | * Determine whether kswapd is (or recently was!) running on this node. |
| 2184 | * |
| 2185 | * pgdat_kswapd_lock() pins pgdat->kswapd, so a concurrent kswapd_stop() can't |
| 2186 | * zero it. |
| 2187 | */ |
| 2188 | static bool kswapd_is_running(pg_data_t *pgdat) |
| 2189 | { |
| 2190 | bool running; |
| 2191 | |
| 2192 | pgdat_kswapd_lock(pgdat); |
| 2193 | running = pgdat->kswapd && task_is_running(pgdat->kswapd); |
| 2194 | pgdat_kswapd_unlock(pgdat); |
| 2195 | |
| 2196 | return running; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * A zone's fragmentation score is the external fragmentation wrt to the |
| 2201 | * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100]. |
| 2202 | */ |
| 2203 | static unsigned int fragmentation_score_zone(struct zone *zone) |
| 2204 | { |
| 2205 | return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER); |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | * A weighted zone's fragmentation score is the external fragmentation |
| 2210 | * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It |
| 2211 | * returns a value in the range [0, 100]. |
| 2212 | * |
| 2213 | * The scaling factor ensures that proactive compaction focuses on larger |
| 2214 | * zones like ZONE_NORMAL, rather than smaller, specialized zones like |
| 2215 | * ZONE_DMA32. For smaller zones, the score value remains close to zero, |
| 2216 | * and thus never exceeds the high threshold for proactive compaction. |
| 2217 | */ |
| 2218 | static unsigned int fragmentation_score_zone_weighted(struct zone *zone) |
| 2219 | { |
| 2220 | unsigned long score; |
| 2221 | |
| 2222 | score = zone->present_pages * fragmentation_score_zone(zone); |
| 2223 | return div64_ul(score, zone->zone_pgdat->node_present_pages + 1); |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * The per-node proactive (background) compaction process is started by its |
| 2228 | * corresponding kcompactd thread when the node's fragmentation score |
| 2229 | * exceeds the high threshold. The compaction process remains active till |
| 2230 | * the node's score falls below the low threshold, or one of the back-off |
| 2231 | * conditions is met. |
| 2232 | */ |
| 2233 | static unsigned int fragmentation_score_node(pg_data_t *pgdat) |
| 2234 | { |
| 2235 | unsigned int score = 0; |
| 2236 | int zoneid; |
| 2237 | |
| 2238 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 2239 | struct zone *zone; |
| 2240 | |
| 2241 | zone = &pgdat->node_zones[zoneid]; |
| 2242 | if (!populated_zone(zone)) |
| 2243 | continue; |
| 2244 | score += fragmentation_score_zone_weighted(zone); |
| 2245 | } |
| 2246 | |
| 2247 | return score; |
| 2248 | } |
| 2249 | |
| 2250 | static unsigned int fragmentation_score_wmark(bool low) |
| 2251 | { |
| 2252 | unsigned int wmark_low, leeway; |
| 2253 | |
| 2254 | wmark_low = 100U - sysctl_compaction_proactiveness; |
| 2255 | leeway = min(10U, wmark_low / 2); |
| 2256 | return low ? wmark_low : min(wmark_low + leeway, 100U); |
| 2257 | } |
| 2258 | |
| 2259 | static bool should_proactive_compact_node(pg_data_t *pgdat) |
| 2260 | { |
| 2261 | int wmark_high; |
| 2262 | |
| 2263 | if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat)) |
| 2264 | return false; |
| 2265 | |
| 2266 | wmark_high = fragmentation_score_wmark(false); |
| 2267 | return fragmentation_score_node(pgdat) > wmark_high; |
| 2268 | } |
| 2269 | |
| 2270 | static enum compact_result __compact_finished(struct compact_control *cc) |
| 2271 | { |
| 2272 | unsigned int order; |
| 2273 | const int migratetype = cc->migratetype; |
| 2274 | int ret; |
| 2275 | |
| 2276 | /* Compaction run completes if the migrate and free scanner meet */ |
| 2277 | if (compact_scanners_met(cc)) { |
| 2278 | /* Let the next compaction start anew. */ |
| 2279 | reset_cached_positions(cc->zone); |
| 2280 | |
| 2281 | /* |
| 2282 | * Mark that the PG_migrate_skip information should be cleared |
| 2283 | * by kswapd when it goes to sleep. kcompactd does not set the |
| 2284 | * flag itself as the decision to be clear should be directly |
| 2285 | * based on an allocation request. |
| 2286 | */ |
| 2287 | if (cc->direct_compaction) |
| 2288 | cc->zone->compact_blockskip_flush = true; |
| 2289 | |
| 2290 | if (cc->whole_zone) |
| 2291 | return COMPACT_COMPLETE; |
| 2292 | else |
| 2293 | return COMPACT_PARTIAL_SKIPPED; |
| 2294 | } |
| 2295 | |
| 2296 | if (cc->proactive_compaction) { |
| 2297 | int score, wmark_low; |
| 2298 | pg_data_t *pgdat; |
| 2299 | |
| 2300 | pgdat = cc->zone->zone_pgdat; |
| 2301 | if (kswapd_is_running(pgdat)) |
| 2302 | return COMPACT_PARTIAL_SKIPPED; |
| 2303 | |
| 2304 | score = fragmentation_score_zone(cc->zone); |
| 2305 | wmark_low = fragmentation_score_wmark(true); |
| 2306 | |
| 2307 | if (score > wmark_low) |
| 2308 | ret = COMPACT_CONTINUE; |
| 2309 | else |
| 2310 | ret = COMPACT_SUCCESS; |
| 2311 | |
| 2312 | goto out; |
| 2313 | } |
| 2314 | |
| 2315 | if (is_via_compact_memory(cc->order)) |
| 2316 | return COMPACT_CONTINUE; |
| 2317 | |
| 2318 | /* |
| 2319 | * Always finish scanning a pageblock to reduce the possibility of |
| 2320 | * fallbacks in the future. This is particularly important when |
| 2321 | * migration source is unmovable/reclaimable but it's not worth |
| 2322 | * special casing. |
| 2323 | */ |
| 2324 | if (!pageblock_aligned(cc->migrate_pfn)) |
| 2325 | return COMPACT_CONTINUE; |
| 2326 | |
| 2327 | /* |
| 2328 | * When defrag_mode is enabled, make kcompactd target |
| 2329 | * watermarks in whole pageblocks. Because they can be stolen |
| 2330 | * without polluting, no further fallback checks are needed. |
| 2331 | */ |
| 2332 | if (defrag_mode && !cc->direct_compaction) { |
| 2333 | if (__zone_watermark_ok(cc->zone, cc->order, |
| 2334 | high_wmark_pages(cc->zone), |
| 2335 | cc->highest_zoneidx, cc->alloc_flags, |
| 2336 | zone_page_state(cc->zone, |
| 2337 | NR_FREE_PAGES_BLOCKS))) |
| 2338 | return COMPACT_SUCCESS; |
| 2339 | |
| 2340 | return COMPACT_CONTINUE; |
| 2341 | } |
| 2342 | |
| 2343 | /* Direct compactor: Is a suitable page free? */ |
| 2344 | ret = COMPACT_NO_SUITABLE_PAGE; |
| 2345 | for (order = cc->order; order < NR_PAGE_ORDERS; order++) { |
| 2346 | struct free_area *area = &cc->zone->free_area[order]; |
| 2347 | |
| 2348 | /* Job done if page is free of the right migratetype */ |
| 2349 | if (!free_area_empty(area, migratetype)) |
| 2350 | return COMPACT_SUCCESS; |
| 2351 | |
| 2352 | #ifdef CONFIG_CMA |
| 2353 | /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */ |
| 2354 | if (migratetype == MIGRATE_MOVABLE && |
| 2355 | !free_area_empty(area, MIGRATE_CMA)) |
| 2356 | return COMPACT_SUCCESS; |
| 2357 | #endif |
| 2358 | /* |
| 2359 | * Job done if allocation would steal freepages from |
| 2360 | * other migratetype buddy lists. |
| 2361 | */ |
| 2362 | if (find_suitable_fallback(area, order, migratetype, true) >= 0) |
| 2363 | /* |
| 2364 | * Movable pages are OK in any pageblock. If we are |
| 2365 | * stealing for a non-movable allocation, make sure |
| 2366 | * we finish compacting the current pageblock first |
| 2367 | * (which is assured by the above migrate_pfn align |
| 2368 | * check) so it is as free as possible and we won't |
| 2369 | * have to steal another one soon. |
| 2370 | */ |
| 2371 | return COMPACT_SUCCESS; |
| 2372 | } |
| 2373 | |
| 2374 | out: |
| 2375 | if (cc->contended || fatal_signal_pending(current)) |
| 2376 | ret = COMPACT_CONTENDED; |
| 2377 | |
| 2378 | return ret; |
| 2379 | } |
| 2380 | |
| 2381 | static enum compact_result compact_finished(struct compact_control *cc) |
| 2382 | { |
| 2383 | int ret; |
| 2384 | |
| 2385 | ret = __compact_finished(cc); |
| 2386 | trace_mm_compaction_finished(cc->zone, cc->order, ret); |
| 2387 | if (ret == COMPACT_NO_SUITABLE_PAGE) |
| 2388 | ret = COMPACT_CONTINUE; |
| 2389 | |
| 2390 | return ret; |
| 2391 | } |
| 2392 | |
| 2393 | static bool __compaction_suitable(struct zone *zone, int order, |
| 2394 | unsigned long watermark, int highest_zoneidx, |
| 2395 | unsigned long free_pages) |
| 2396 | { |
| 2397 | /* |
| 2398 | * Watermarks for order-0 must be met for compaction to be able to |
| 2399 | * isolate free pages for migration targets. This means that the |
| 2400 | * watermark have to match, or be more pessimistic than the check in |
| 2401 | * __isolate_free_page(). |
| 2402 | * |
| 2403 | * For costly orders, we require a higher watermark for compaction to |
| 2404 | * proceed to increase its chances. |
| 2405 | * |
| 2406 | * We use the direct compactor's highest_zoneidx to skip over zones |
| 2407 | * where lowmem reserves would prevent allocation even if compaction |
| 2408 | * succeeds. |
| 2409 | * |
| 2410 | * ALLOC_CMA is used, as pages in CMA pageblocks are considered |
| 2411 | * suitable migration targets. |
| 2412 | */ |
| 2413 | watermark += compact_gap(order); |
| 2414 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
| 2415 | watermark += low_wmark_pages(zone) - min_wmark_pages(zone); |
| 2416 | return __zone_watermark_ok(zone, 0, watermark, highest_zoneidx, |
| 2417 | ALLOC_CMA, free_pages); |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * compaction_suitable: Is this suitable to run compaction on this zone now? |
| 2422 | */ |
| 2423 | bool compaction_suitable(struct zone *zone, int order, unsigned long watermark, |
| 2424 | int highest_zoneidx) |
| 2425 | { |
| 2426 | enum compact_result compact_result; |
| 2427 | bool suitable; |
| 2428 | |
| 2429 | suitable = __compaction_suitable(zone, order, watermark, highest_zoneidx, |
| 2430 | zone_page_state(zone, NR_FREE_PAGES)); |
| 2431 | /* |
| 2432 | * fragmentation index determines if allocation failures are due to |
| 2433 | * low memory or external fragmentation |
| 2434 | * |
| 2435 | * index of -1000 would imply allocations might succeed depending on |
| 2436 | * watermarks, but we already failed the high-order watermark check |
| 2437 | * index towards 0 implies failure is due to lack of memory |
| 2438 | * index towards 1000 implies failure is due to fragmentation |
| 2439 | * |
| 2440 | * Only compact if a failure would be due to fragmentation. Also |
| 2441 | * ignore fragindex for non-costly orders where the alternative to |
| 2442 | * a successful reclaim/compaction is OOM. Fragindex and the |
| 2443 | * vm.extfrag_threshold sysctl is meant as a heuristic to prevent |
| 2444 | * excessive compaction for costly orders, but it should not be at the |
| 2445 | * expense of system stability. |
| 2446 | */ |
| 2447 | if (suitable) { |
| 2448 | compact_result = COMPACT_CONTINUE; |
| 2449 | if (order > PAGE_ALLOC_COSTLY_ORDER) { |
| 2450 | int fragindex = fragmentation_index(zone, order); |
| 2451 | |
| 2452 | if (fragindex >= 0 && |
| 2453 | fragindex <= sysctl_extfrag_threshold) { |
| 2454 | suitable = false; |
| 2455 | compact_result = COMPACT_NOT_SUITABLE_ZONE; |
| 2456 | } |
| 2457 | } |
| 2458 | } else { |
| 2459 | compact_result = COMPACT_SKIPPED; |
| 2460 | } |
| 2461 | |
| 2462 | trace_mm_compaction_suitable(zone, order, compact_result); |
| 2463 | |
| 2464 | return suitable; |
| 2465 | } |
| 2466 | |
| 2467 | /* Used by direct reclaimers */ |
| 2468 | bool compaction_zonelist_suitable(struct alloc_context *ac, int order, |
| 2469 | int alloc_flags) |
| 2470 | { |
| 2471 | struct zone *zone; |
| 2472 | struct zoneref *z; |
| 2473 | |
| 2474 | /* |
| 2475 | * Make sure at least one zone would pass __compaction_suitable if we continue |
| 2476 | * retrying the reclaim. |
| 2477 | */ |
| 2478 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
| 2479 | ac->highest_zoneidx, ac->nodemask) { |
| 2480 | unsigned long available; |
| 2481 | |
| 2482 | /* |
| 2483 | * Do not consider all the reclaimable memory because we do not |
| 2484 | * want to trash just for a single high order allocation which |
| 2485 | * is even not guaranteed to appear even if __compaction_suitable |
| 2486 | * is happy about the watermark check. |
| 2487 | */ |
| 2488 | available = zone_reclaimable_pages(zone) / order; |
| 2489 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
| 2490 | if (__compaction_suitable(zone, order, min_wmark_pages(zone), |
| 2491 | ac->highest_zoneidx, available)) |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
| 2495 | return false; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Should we do compaction for target allocation order. |
| 2500 | * Return COMPACT_SUCCESS if allocation for target order can be already |
| 2501 | * satisfied |
| 2502 | * Return COMPACT_SKIPPED if compaction for target order is likely to fail |
| 2503 | * Return COMPACT_CONTINUE if compaction for target order should be ran |
| 2504 | */ |
| 2505 | static enum compact_result |
| 2506 | compaction_suit_allocation_order(struct zone *zone, unsigned int order, |
| 2507 | int highest_zoneidx, unsigned int alloc_flags, |
| 2508 | bool async, bool kcompactd) |
| 2509 | { |
| 2510 | unsigned long free_pages; |
| 2511 | unsigned long watermark; |
| 2512 | |
| 2513 | if (kcompactd && defrag_mode) |
| 2514 | free_pages = zone_page_state(zone, NR_FREE_PAGES_BLOCKS); |
| 2515 | else |
| 2516 | free_pages = zone_page_state(zone, NR_FREE_PAGES); |
| 2517 | |
| 2518 | watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); |
| 2519 | if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx, |
| 2520 | alloc_flags, free_pages)) |
| 2521 | return COMPACT_SUCCESS; |
| 2522 | |
| 2523 | /* |
| 2524 | * For unmovable allocations (without ALLOC_CMA), check if there is enough |
| 2525 | * free memory in the non-CMA pageblocks. Otherwise compaction could form |
| 2526 | * the high-order page in CMA pageblocks, which would not help the |
| 2527 | * allocation to succeed. However, limit the check to costly order async |
| 2528 | * compaction (such as opportunistic THP attempts) because there is the |
| 2529 | * possibility that compaction would migrate pages from non-CMA to CMA |
| 2530 | * pageblock. |
| 2531 | */ |
| 2532 | if (order > PAGE_ALLOC_COSTLY_ORDER && async && |
| 2533 | !(alloc_flags & ALLOC_CMA)) { |
| 2534 | if (!__zone_watermark_ok(zone, 0, watermark + compact_gap(order), |
| 2535 | highest_zoneidx, 0, |
| 2536 | zone_page_state(zone, NR_FREE_PAGES))) |
| 2537 | return COMPACT_SKIPPED; |
| 2538 | } |
| 2539 | |
| 2540 | if (!compaction_suitable(zone, order, watermark, highest_zoneidx)) |
| 2541 | return COMPACT_SKIPPED; |
| 2542 | |
| 2543 | return COMPACT_CONTINUE; |
| 2544 | } |
| 2545 | |
| 2546 | static enum compact_result |
| 2547 | compact_zone(struct compact_control *cc, struct capture_control *capc) |
| 2548 | { |
| 2549 | enum compact_result ret; |
| 2550 | unsigned long start_pfn = cc->zone->zone_start_pfn; |
| 2551 | unsigned long end_pfn = zone_end_pfn(cc->zone); |
| 2552 | unsigned long last_migrated_pfn; |
| 2553 | const bool sync = cc->mode != MIGRATE_ASYNC; |
| 2554 | bool update_cached; |
| 2555 | unsigned int nr_succeeded = 0, nr_migratepages; |
| 2556 | int order; |
| 2557 | |
| 2558 | /* |
| 2559 | * These counters track activities during zone compaction. Initialize |
| 2560 | * them before compacting a new zone. |
| 2561 | */ |
| 2562 | cc->total_migrate_scanned = 0; |
| 2563 | cc->total_free_scanned = 0; |
| 2564 | cc->nr_migratepages = 0; |
| 2565 | cc->nr_freepages = 0; |
| 2566 | for (order = 0; order < NR_PAGE_ORDERS; order++) |
| 2567 | INIT_LIST_HEAD(&cc->freepages[order]); |
| 2568 | INIT_LIST_HEAD(&cc->migratepages); |
| 2569 | |
| 2570 | cc->migratetype = gfp_migratetype(cc->gfp_mask); |
| 2571 | |
| 2572 | if (!is_via_compact_memory(cc->order)) { |
| 2573 | ret = compaction_suit_allocation_order(cc->zone, cc->order, |
| 2574 | cc->highest_zoneidx, |
| 2575 | cc->alloc_flags, |
| 2576 | cc->mode == MIGRATE_ASYNC, |
| 2577 | !cc->direct_compaction); |
| 2578 | if (ret != COMPACT_CONTINUE) |
| 2579 | return ret; |
| 2580 | } |
| 2581 | |
| 2582 | /* |
| 2583 | * Clear pageblock skip if there were failures recently and compaction |
| 2584 | * is about to be retried after being deferred. |
| 2585 | */ |
| 2586 | if (compaction_restarting(cc->zone, cc->order)) |
| 2587 | __reset_isolation_suitable(cc->zone); |
| 2588 | |
| 2589 | /* |
| 2590 | * Setup to move all movable pages to the end of the zone. Used cached |
| 2591 | * information on where the scanners should start (unless we explicitly |
| 2592 | * want to compact the whole zone), but check that it is initialised |
| 2593 | * by ensuring the values are within zone boundaries. |
| 2594 | */ |
| 2595 | cc->fast_start_pfn = 0; |
| 2596 | if (cc->whole_zone) { |
| 2597 | cc->migrate_pfn = start_pfn; |
| 2598 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
| 2599 | } else { |
| 2600 | cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync]; |
| 2601 | cc->free_pfn = cc->zone->compact_cached_free_pfn; |
| 2602 | if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) { |
| 2603 | cc->free_pfn = pageblock_start_pfn(end_pfn - 1); |
| 2604 | cc->zone->compact_cached_free_pfn = cc->free_pfn; |
| 2605 | } |
| 2606 | if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) { |
| 2607 | cc->migrate_pfn = start_pfn; |
| 2608 | cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn; |
| 2609 | cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn; |
| 2610 | } |
| 2611 | |
| 2612 | if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn) |
| 2613 | cc->whole_zone = true; |
| 2614 | } |
| 2615 | |
| 2616 | last_migrated_pfn = 0; |
| 2617 | |
| 2618 | /* |
| 2619 | * Migrate has separate cached PFNs for ASYNC and SYNC* migration on |
| 2620 | * the basis that some migrations will fail in ASYNC mode. However, |
| 2621 | * if the cached PFNs match and pageblocks are skipped due to having |
| 2622 | * no isolation candidates, then the sync state does not matter. |
| 2623 | * Until a pageblock with isolation candidates is found, keep the |
| 2624 | * cached PFNs in sync to avoid revisiting the same blocks. |
| 2625 | */ |
| 2626 | update_cached = !sync && |
| 2627 | cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1]; |
| 2628 | |
| 2629 | trace_mm_compaction_begin(cc, start_pfn, end_pfn, sync); |
| 2630 | |
| 2631 | /* lru_add_drain_all could be expensive with involving other CPUs */ |
| 2632 | lru_add_drain(); |
| 2633 | |
| 2634 | while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) { |
| 2635 | int err; |
| 2636 | unsigned long iteration_start_pfn = cc->migrate_pfn; |
| 2637 | |
| 2638 | /* |
| 2639 | * Avoid multiple rescans of the same pageblock which can |
| 2640 | * happen if a page cannot be isolated (dirty/writeback in |
| 2641 | * async mode) or if the migrated pages are being allocated |
| 2642 | * before the pageblock is cleared. The first rescan will |
| 2643 | * capture the entire pageblock for migration. If it fails, |
| 2644 | * it'll be marked skip and scanning will proceed as normal. |
| 2645 | */ |
| 2646 | cc->finish_pageblock = false; |
| 2647 | if (pageblock_start_pfn(last_migrated_pfn) == |
| 2648 | pageblock_start_pfn(iteration_start_pfn)) { |
| 2649 | cc->finish_pageblock = true; |
| 2650 | } |
| 2651 | |
| 2652 | rescan: |
| 2653 | switch (isolate_migratepages(cc)) { |
| 2654 | case ISOLATE_ABORT: |
| 2655 | ret = COMPACT_CONTENDED; |
| 2656 | putback_movable_pages(&cc->migratepages); |
| 2657 | cc->nr_migratepages = 0; |
| 2658 | goto out; |
| 2659 | case ISOLATE_NONE: |
| 2660 | if (update_cached) { |
| 2661 | cc->zone->compact_cached_migrate_pfn[1] = |
| 2662 | cc->zone->compact_cached_migrate_pfn[0]; |
| 2663 | } |
| 2664 | |
| 2665 | /* |
| 2666 | * We haven't isolated and migrated anything, but |
| 2667 | * there might still be unflushed migrations from |
| 2668 | * previous cc->order aligned block. |
| 2669 | */ |
| 2670 | goto check_drain; |
| 2671 | case ISOLATE_SUCCESS: |
| 2672 | update_cached = false; |
| 2673 | last_migrated_pfn = max(cc->zone->zone_start_pfn, |
| 2674 | pageblock_start_pfn(cc->migrate_pfn - 1)); |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * Record the number of pages to migrate since the |
| 2679 | * compaction_alloc/free() will update cc->nr_migratepages |
| 2680 | * properly. |
| 2681 | */ |
| 2682 | nr_migratepages = cc->nr_migratepages; |
| 2683 | err = migrate_pages(&cc->migratepages, compaction_alloc, |
| 2684 | compaction_free, (unsigned long)cc, cc->mode, |
| 2685 | MR_COMPACTION, &nr_succeeded); |
| 2686 | |
| 2687 | trace_mm_compaction_migratepages(nr_migratepages, nr_succeeded); |
| 2688 | |
| 2689 | /* All pages were either migrated or will be released */ |
| 2690 | cc->nr_migratepages = 0; |
| 2691 | if (err) { |
| 2692 | putback_movable_pages(&cc->migratepages); |
| 2693 | /* |
| 2694 | * migrate_pages() may return -ENOMEM when scanners meet |
| 2695 | * and we want compact_finished() to detect it |
| 2696 | */ |
| 2697 | if (err == -ENOMEM && !compact_scanners_met(cc)) { |
| 2698 | ret = COMPACT_CONTENDED; |
| 2699 | goto out; |
| 2700 | } |
| 2701 | /* |
| 2702 | * If an ASYNC or SYNC_LIGHT fails to migrate a page |
| 2703 | * within the pageblock_order-aligned block and |
| 2704 | * fast_find_migrateblock may be used then scan the |
| 2705 | * remainder of the pageblock. This will mark the |
| 2706 | * pageblock "skip" to avoid rescanning in the near |
| 2707 | * future. This will isolate more pages than necessary |
| 2708 | * for the request but avoid loops due to |
| 2709 | * fast_find_migrateblock revisiting blocks that were |
| 2710 | * recently partially scanned. |
| 2711 | */ |
| 2712 | if (!pageblock_aligned(cc->migrate_pfn) && |
| 2713 | !cc->ignore_skip_hint && !cc->finish_pageblock && |
| 2714 | (cc->mode < MIGRATE_SYNC)) { |
| 2715 | cc->finish_pageblock = true; |
| 2716 | |
| 2717 | /* |
| 2718 | * Draining pcplists does not help THP if |
| 2719 | * any page failed to migrate. Even after |
| 2720 | * drain, the pageblock will not be free. |
| 2721 | */ |
| 2722 | if (cc->order == COMPACTION_HPAGE_ORDER) |
| 2723 | last_migrated_pfn = 0; |
| 2724 | |
| 2725 | goto rescan; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | /* Stop if a page has been captured */ |
| 2730 | if (capc && capc->page) { |
| 2731 | ret = COMPACT_SUCCESS; |
| 2732 | break; |
| 2733 | } |
| 2734 | |
| 2735 | check_drain: |
| 2736 | /* |
| 2737 | * Has the migration scanner moved away from the previous |
| 2738 | * cc->order aligned block where we migrated from? If yes, |
| 2739 | * flush the pages that were freed, so that they can merge and |
| 2740 | * compact_finished() can detect immediately if allocation |
| 2741 | * would succeed. |
| 2742 | */ |
| 2743 | if (cc->order > 0 && last_migrated_pfn) { |
| 2744 | unsigned long current_block_start = |
| 2745 | block_start_pfn(cc->migrate_pfn, cc->order); |
| 2746 | |
| 2747 | if (last_migrated_pfn < current_block_start) { |
| 2748 | lru_add_drain_cpu_zone(cc->zone); |
| 2749 | /* No more flushing until we migrate again */ |
| 2750 | last_migrated_pfn = 0; |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | out: |
| 2756 | /* |
| 2757 | * Release free pages and update where the free scanner should restart, |
| 2758 | * so we don't leave any returned pages behind in the next attempt. |
| 2759 | */ |
| 2760 | if (cc->nr_freepages > 0) { |
| 2761 | unsigned long free_pfn = release_free_list(cc->freepages); |
| 2762 | |
| 2763 | cc->nr_freepages = 0; |
| 2764 | VM_BUG_ON(free_pfn == 0); |
| 2765 | /* The cached pfn is always the first in a pageblock */ |
| 2766 | free_pfn = pageblock_start_pfn(free_pfn); |
| 2767 | /* |
| 2768 | * Only go back, not forward. The cached pfn might have been |
| 2769 | * already reset to zone end in compact_finished() |
| 2770 | */ |
| 2771 | if (free_pfn > cc->zone->compact_cached_free_pfn) |
| 2772 | cc->zone->compact_cached_free_pfn = free_pfn; |
| 2773 | } |
| 2774 | |
| 2775 | count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned); |
| 2776 | count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned); |
| 2777 | |
| 2778 | trace_mm_compaction_end(cc, start_pfn, end_pfn, sync, ret); |
| 2779 | |
| 2780 | VM_BUG_ON(!list_empty(&cc->migratepages)); |
| 2781 | |
| 2782 | return ret; |
| 2783 | } |
| 2784 | |
| 2785 | static enum compact_result compact_zone_order(struct zone *zone, int order, |
| 2786 | gfp_t gfp_mask, enum compact_priority prio, |
| 2787 | unsigned int alloc_flags, int highest_zoneidx, |
| 2788 | struct page **capture) |
| 2789 | { |
| 2790 | enum compact_result ret; |
| 2791 | struct compact_control cc = { |
| 2792 | .order = order, |
| 2793 | .search_order = order, |
| 2794 | .gfp_mask = gfp_mask, |
| 2795 | .zone = zone, |
| 2796 | .mode = (prio == COMPACT_PRIO_ASYNC) ? |
| 2797 | MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT, |
| 2798 | .alloc_flags = alloc_flags, |
| 2799 | .highest_zoneidx = highest_zoneidx, |
| 2800 | .direct_compaction = true, |
| 2801 | .whole_zone = (prio == MIN_COMPACT_PRIORITY), |
| 2802 | .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY), |
| 2803 | .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY) |
| 2804 | }; |
| 2805 | struct capture_control capc = { |
| 2806 | .cc = &cc, |
| 2807 | .page = NULL, |
| 2808 | }; |
| 2809 | |
| 2810 | /* |
| 2811 | * Make sure the structs are really initialized before we expose the |
| 2812 | * capture control, in case we are interrupted and the interrupt handler |
| 2813 | * frees a page. |
| 2814 | */ |
| 2815 | barrier(); |
| 2816 | WRITE_ONCE(current->capture_control, &capc); |
| 2817 | |
| 2818 | ret = compact_zone(&cc, &capc); |
| 2819 | |
| 2820 | /* |
| 2821 | * Make sure we hide capture control first before we read the captured |
| 2822 | * page pointer, otherwise an interrupt could free and capture a page |
| 2823 | * and we would leak it. |
| 2824 | */ |
| 2825 | WRITE_ONCE(current->capture_control, NULL); |
| 2826 | *capture = READ_ONCE(capc.page); |
| 2827 | /* |
| 2828 | * Technically, it is also possible that compaction is skipped but |
| 2829 | * the page is still captured out of luck(IRQ came and freed the page). |
| 2830 | * Returning COMPACT_SUCCESS in such cases helps in properly accounting |
| 2831 | * the COMPACT[STALL|FAIL] when compaction is skipped. |
| 2832 | */ |
| 2833 | if (*capture) |
| 2834 | ret = COMPACT_SUCCESS; |
| 2835 | |
| 2836 | return ret; |
| 2837 | } |
| 2838 | |
| 2839 | /** |
| 2840 | * try_to_compact_pages - Direct compact to satisfy a high-order allocation |
| 2841 | * @gfp_mask: The GFP mask of the current allocation |
| 2842 | * @order: The order of the current allocation |
| 2843 | * @alloc_flags: The allocation flags of the current allocation |
| 2844 | * @ac: The context of current allocation |
| 2845 | * @prio: Determines how hard direct compaction should try to succeed |
| 2846 | * @capture: Pointer to free page created by compaction will be stored here |
| 2847 | * |
| 2848 | * This is the main entry point for direct page compaction. |
| 2849 | */ |
| 2850 | enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order, |
| 2851 | unsigned int alloc_flags, const struct alloc_context *ac, |
| 2852 | enum compact_priority prio, struct page **capture) |
| 2853 | { |
| 2854 | struct zoneref *z; |
| 2855 | struct zone *zone; |
| 2856 | enum compact_result rc = COMPACT_SKIPPED; |
| 2857 | |
| 2858 | if (!gfp_compaction_allowed(gfp_mask)) |
| 2859 | return COMPACT_SKIPPED; |
| 2860 | |
| 2861 | trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio); |
| 2862 | |
| 2863 | /* Compact each zone in the list */ |
| 2864 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
| 2865 | ac->highest_zoneidx, ac->nodemask) { |
| 2866 | enum compact_result status; |
| 2867 | |
| 2868 | if (cpusets_enabled() && |
| 2869 | (alloc_flags & ALLOC_CPUSET) && |
| 2870 | !__cpuset_zone_allowed(zone, gfp_mask)) |
| 2871 | continue; |
| 2872 | |
| 2873 | if (prio > MIN_COMPACT_PRIORITY |
| 2874 | && compaction_deferred(zone, order)) { |
| 2875 | rc = max_t(enum compact_result, COMPACT_DEFERRED, rc); |
| 2876 | continue; |
| 2877 | } |
| 2878 | |
| 2879 | status = compact_zone_order(zone, order, gfp_mask, prio, |
| 2880 | alloc_flags, ac->highest_zoneidx, capture); |
| 2881 | rc = max(status, rc); |
| 2882 | |
| 2883 | /* The allocation should succeed, stop compacting */ |
| 2884 | if (status == COMPACT_SUCCESS) { |
| 2885 | /* |
| 2886 | * We think the allocation will succeed in this zone, |
| 2887 | * but it is not certain, hence the false. The caller |
| 2888 | * will repeat this with true if allocation indeed |
| 2889 | * succeeds in this zone. |
| 2890 | */ |
| 2891 | compaction_defer_reset(zone, order, false); |
| 2892 | |
| 2893 | break; |
| 2894 | } |
| 2895 | |
| 2896 | if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE || |
| 2897 | status == COMPACT_PARTIAL_SKIPPED)) |
| 2898 | /* |
| 2899 | * We think that allocation won't succeed in this zone |
| 2900 | * so we defer compaction there. If it ends up |
| 2901 | * succeeding after all, it will be reset. |
| 2902 | */ |
| 2903 | defer_compaction(zone, order); |
| 2904 | |
| 2905 | /* |
| 2906 | * We might have stopped compacting due to need_resched() in |
| 2907 | * async compaction, or due to a fatal signal detected. In that |
| 2908 | * case do not try further zones |
| 2909 | */ |
| 2910 | if ((prio == COMPACT_PRIO_ASYNC && need_resched()) |
| 2911 | || fatal_signal_pending(current)) |
| 2912 | break; |
| 2913 | } |
| 2914 | |
| 2915 | return rc; |
| 2916 | } |
| 2917 | |
| 2918 | /* |
| 2919 | * compact_node() - compact all zones within a node |
| 2920 | * @pgdat: The node page data |
| 2921 | * @proactive: Whether the compaction is proactive |
| 2922 | * |
| 2923 | * For proactive compaction, compact till each zone's fragmentation score |
| 2924 | * reaches within proactive compaction thresholds (as determined by the |
| 2925 | * proactiveness tunable), it is possible that the function returns before |
| 2926 | * reaching score targets due to various back-off conditions, such as, |
| 2927 | * contention on per-node or per-zone locks. |
| 2928 | */ |
| 2929 | static int compact_node(pg_data_t *pgdat, bool proactive) |
| 2930 | { |
| 2931 | int zoneid; |
| 2932 | struct zone *zone; |
| 2933 | struct compact_control cc = { |
| 2934 | .order = -1, |
| 2935 | .mode = proactive ? MIGRATE_SYNC_LIGHT : MIGRATE_SYNC, |
| 2936 | .ignore_skip_hint = true, |
| 2937 | .whole_zone = true, |
| 2938 | .gfp_mask = GFP_KERNEL, |
| 2939 | .proactive_compaction = proactive, |
| 2940 | }; |
| 2941 | |
| 2942 | for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) { |
| 2943 | zone = &pgdat->node_zones[zoneid]; |
| 2944 | if (!populated_zone(zone)) |
| 2945 | continue; |
| 2946 | |
| 2947 | if (fatal_signal_pending(current)) |
| 2948 | return -EINTR; |
| 2949 | |
| 2950 | cc.zone = zone; |
| 2951 | |
| 2952 | compact_zone(&cc, NULL); |
| 2953 | |
| 2954 | if (proactive) { |
| 2955 | count_compact_events(KCOMPACTD_MIGRATE_SCANNED, |
| 2956 | cc.total_migrate_scanned); |
| 2957 | count_compact_events(KCOMPACTD_FREE_SCANNED, |
| 2958 | cc.total_free_scanned); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | return 0; |
| 2963 | } |
| 2964 | |
| 2965 | /* Compact all zones of all nodes in the system */ |
| 2966 | static int compact_nodes(void) |
| 2967 | { |
| 2968 | int ret, nid; |
| 2969 | |
| 2970 | /* Flush pending updates to the LRU lists */ |
| 2971 | lru_add_drain_all(); |
| 2972 | |
| 2973 | for_each_online_node(nid) { |
| 2974 | ret = compact_node(NODE_DATA(nid), false); |
| 2975 | if (ret) |
| 2976 | return ret; |
| 2977 | } |
| 2978 | |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | static int compaction_proactiveness_sysctl_handler(const struct ctl_table *table, int write, |
| 2983 | void *buffer, size_t *length, loff_t *ppos) |
| 2984 | { |
| 2985 | int rc, nid; |
| 2986 | |
| 2987 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 2988 | if (rc) |
| 2989 | return rc; |
| 2990 | |
| 2991 | if (write && sysctl_compaction_proactiveness) { |
| 2992 | for_each_online_node(nid) { |
| 2993 | pg_data_t *pgdat = NODE_DATA(nid); |
| 2994 | |
| 2995 | if (pgdat->proactive_compact_trigger) |
| 2996 | continue; |
| 2997 | |
| 2998 | pgdat->proactive_compact_trigger = true; |
| 2999 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, -1, |
| 3000 | pgdat->nr_zones - 1); |
| 3001 | wake_up_interruptible(&pgdat->kcompactd_wait); |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
| 3008 | /* |
| 3009 | * This is the entry point for compacting all nodes via |
| 3010 | * /proc/sys/vm/compact_memory |
| 3011 | */ |
| 3012 | static int sysctl_compaction_handler(const struct ctl_table *table, int write, |
| 3013 | void *buffer, size_t *length, loff_t *ppos) |
| 3014 | { |
| 3015 | int ret; |
| 3016 | |
| 3017 | ret = proc_dointvec(table, write, buffer, length, ppos); |
| 3018 | if (ret) |
| 3019 | return ret; |
| 3020 | |
| 3021 | if (sysctl_compact_memory != 1) |
| 3022 | return -EINVAL; |
| 3023 | |
| 3024 | if (write) |
| 3025 | ret = compact_nodes(); |
| 3026 | |
| 3027 | return ret; |
| 3028 | } |
| 3029 | |
| 3030 | #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA) |
| 3031 | static ssize_t compact_store(struct device *dev, |
| 3032 | struct device_attribute *attr, |
| 3033 | const char *buf, size_t count) |
| 3034 | { |
| 3035 | int nid = dev->id; |
| 3036 | |
| 3037 | if (nid >= 0 && nid < nr_node_ids && node_online(nid)) { |
| 3038 | /* Flush pending updates to the LRU lists */ |
| 3039 | lru_add_drain_all(); |
| 3040 | |
| 3041 | compact_node(NODE_DATA(nid), false); |
| 3042 | } |
| 3043 | |
| 3044 | return count; |
| 3045 | } |
| 3046 | static DEVICE_ATTR_WO(compact); |
| 3047 | |
| 3048 | int compaction_register_node(struct node *node) |
| 3049 | { |
| 3050 | return device_create_file(&node->dev, &dev_attr_compact); |
| 3051 | } |
| 3052 | |
| 3053 | void compaction_unregister_node(struct node *node) |
| 3054 | { |
| 3055 | device_remove_file(&node->dev, &dev_attr_compact); |
| 3056 | } |
| 3057 | #endif /* CONFIG_SYSFS && CONFIG_NUMA */ |
| 3058 | |
| 3059 | static inline bool kcompactd_work_requested(pg_data_t *pgdat) |
| 3060 | { |
| 3061 | return pgdat->kcompactd_max_order > 0 || kthread_should_stop() || |
| 3062 | pgdat->proactive_compact_trigger; |
| 3063 | } |
| 3064 | |
| 3065 | static bool kcompactd_node_suitable(pg_data_t *pgdat) |
| 3066 | { |
| 3067 | int zoneid; |
| 3068 | struct zone *zone; |
| 3069 | enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx; |
| 3070 | enum compact_result ret; |
| 3071 | unsigned int alloc_flags = defrag_mode ? |
| 3072 | ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN; |
| 3073 | |
| 3074 | for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) { |
| 3075 | zone = &pgdat->node_zones[zoneid]; |
| 3076 | |
| 3077 | if (!populated_zone(zone)) |
| 3078 | continue; |
| 3079 | |
| 3080 | ret = compaction_suit_allocation_order(zone, |
| 3081 | pgdat->kcompactd_max_order, |
| 3082 | highest_zoneidx, alloc_flags, |
| 3083 | false, true); |
| 3084 | if (ret == COMPACT_CONTINUE) |
| 3085 | return true; |
| 3086 | } |
| 3087 | |
| 3088 | return false; |
| 3089 | } |
| 3090 | |
| 3091 | static void kcompactd_do_work(pg_data_t *pgdat) |
| 3092 | { |
| 3093 | /* |
| 3094 | * With no special task, compact all zones so that a page of requested |
| 3095 | * order is allocatable. |
| 3096 | */ |
| 3097 | int zoneid; |
| 3098 | struct zone *zone; |
| 3099 | struct compact_control cc = { |
| 3100 | .order = pgdat->kcompactd_max_order, |
| 3101 | .search_order = pgdat->kcompactd_max_order, |
| 3102 | .highest_zoneidx = pgdat->kcompactd_highest_zoneidx, |
| 3103 | .mode = MIGRATE_SYNC_LIGHT, |
| 3104 | .ignore_skip_hint = false, |
| 3105 | .gfp_mask = GFP_KERNEL, |
| 3106 | .alloc_flags = defrag_mode ? ALLOC_WMARK_HIGH : ALLOC_WMARK_MIN, |
| 3107 | }; |
| 3108 | enum compact_result ret; |
| 3109 | |
| 3110 | trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order, |
| 3111 | cc.highest_zoneidx); |
| 3112 | count_compact_event(KCOMPACTD_WAKE); |
| 3113 | |
| 3114 | for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) { |
| 3115 | int status; |
| 3116 | |
| 3117 | zone = &pgdat->node_zones[zoneid]; |
| 3118 | if (!populated_zone(zone)) |
| 3119 | continue; |
| 3120 | |
| 3121 | if (compaction_deferred(zone, cc.order)) |
| 3122 | continue; |
| 3123 | |
| 3124 | ret = compaction_suit_allocation_order(zone, |
| 3125 | cc.order, zoneid, cc.alloc_flags, |
| 3126 | false, true); |
| 3127 | if (ret != COMPACT_CONTINUE) |
| 3128 | continue; |
| 3129 | |
| 3130 | if (kthread_should_stop()) |
| 3131 | return; |
| 3132 | |
| 3133 | cc.zone = zone; |
| 3134 | status = compact_zone(&cc, NULL); |
| 3135 | |
| 3136 | if (status == COMPACT_SUCCESS) { |
| 3137 | compaction_defer_reset(zone, cc.order, false); |
| 3138 | } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) { |
| 3139 | /* |
| 3140 | * Buddy pages may become stranded on pcps that could |
| 3141 | * otherwise coalesce on the zone's free area for |
| 3142 | * order >= cc.order. This is ratelimited by the |
| 3143 | * upcoming deferral. |
| 3144 | */ |
| 3145 | drain_all_pages(zone); |
| 3146 | |
| 3147 | /* |
| 3148 | * We use sync migration mode here, so we defer like |
| 3149 | * sync direct compaction does. |
| 3150 | */ |
| 3151 | defer_compaction(zone, cc.order); |
| 3152 | } |
| 3153 | |
| 3154 | count_compact_events(KCOMPACTD_MIGRATE_SCANNED, |
| 3155 | cc.total_migrate_scanned); |
| 3156 | count_compact_events(KCOMPACTD_FREE_SCANNED, |
| 3157 | cc.total_free_scanned); |
| 3158 | } |
| 3159 | |
| 3160 | /* |
| 3161 | * Regardless of success, we are done until woken up next. But remember |
| 3162 | * the requested order/highest_zoneidx in case it was higher/tighter |
| 3163 | * than our current ones |
| 3164 | */ |
| 3165 | if (pgdat->kcompactd_max_order <= cc.order) |
| 3166 | pgdat->kcompactd_max_order = 0; |
| 3167 | if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx) |
| 3168 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
| 3169 | } |
| 3170 | |
| 3171 | void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx) |
| 3172 | { |
| 3173 | if (!order) |
| 3174 | return; |
| 3175 | |
| 3176 | if (pgdat->kcompactd_max_order < order) |
| 3177 | pgdat->kcompactd_max_order = order; |
| 3178 | |
| 3179 | if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx) |
| 3180 | pgdat->kcompactd_highest_zoneidx = highest_zoneidx; |
| 3181 | |
| 3182 | /* |
| 3183 | * Pairs with implicit barrier in wait_event_freezable() |
| 3184 | * such that wakeups are not missed. |
| 3185 | */ |
| 3186 | if (!wq_has_sleeper(&pgdat->kcompactd_wait)) |
| 3187 | return; |
| 3188 | |
| 3189 | if (!kcompactd_node_suitable(pgdat)) |
| 3190 | return; |
| 3191 | |
| 3192 | trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order, |
| 3193 | highest_zoneidx); |
| 3194 | wake_up_interruptible(&pgdat->kcompactd_wait); |
| 3195 | } |
| 3196 | |
| 3197 | /* |
| 3198 | * The background compaction daemon, started as a kernel thread |
| 3199 | * from the init process. |
| 3200 | */ |
| 3201 | static int kcompactd(void *p) |
| 3202 | { |
| 3203 | pg_data_t *pgdat = (pg_data_t *)p; |
| 3204 | long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC); |
| 3205 | long timeout = default_timeout; |
| 3206 | |
| 3207 | current->flags |= PF_KCOMPACTD; |
| 3208 | set_freezable(); |
| 3209 | |
| 3210 | pgdat->kcompactd_max_order = 0; |
| 3211 | pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1; |
| 3212 | |
| 3213 | while (!kthread_should_stop()) { |
| 3214 | unsigned long pflags; |
| 3215 | |
| 3216 | /* |
| 3217 | * Avoid the unnecessary wakeup for proactive compaction |
| 3218 | * when it is disabled. |
| 3219 | */ |
| 3220 | if (!sysctl_compaction_proactiveness) |
| 3221 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 3222 | trace_mm_compaction_kcompactd_sleep(pgdat->node_id); |
| 3223 | if (wait_event_freezable_timeout(pgdat->kcompactd_wait, |
| 3224 | kcompactd_work_requested(pgdat), timeout) && |
| 3225 | !pgdat->proactive_compact_trigger) { |
| 3226 | |
| 3227 | psi_memstall_enter(&pflags); |
| 3228 | kcompactd_do_work(pgdat); |
| 3229 | psi_memstall_leave(&pflags); |
| 3230 | /* |
| 3231 | * Reset the timeout value. The defer timeout from |
| 3232 | * proactive compaction is lost here but that is fine |
| 3233 | * as the condition of the zone changing substantionally |
| 3234 | * then carrying on with the previous defer interval is |
| 3235 | * not useful. |
| 3236 | */ |
| 3237 | timeout = default_timeout; |
| 3238 | continue; |
| 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * Start the proactive work with default timeout. Based |
| 3243 | * on the fragmentation score, this timeout is updated. |
| 3244 | */ |
| 3245 | timeout = default_timeout; |
| 3246 | if (should_proactive_compact_node(pgdat)) { |
| 3247 | unsigned int prev_score, score; |
| 3248 | |
| 3249 | prev_score = fragmentation_score_node(pgdat); |
| 3250 | compact_node(pgdat, true); |
| 3251 | score = fragmentation_score_node(pgdat); |
| 3252 | /* |
| 3253 | * Defer proactive compaction if the fragmentation |
| 3254 | * score did not go down i.e. no progress made. |
| 3255 | */ |
| 3256 | if (unlikely(score >= prev_score)) |
| 3257 | timeout = |
| 3258 | default_timeout << COMPACT_MAX_DEFER_SHIFT; |
| 3259 | } |
| 3260 | if (unlikely(pgdat->proactive_compact_trigger)) |
| 3261 | pgdat->proactive_compact_trigger = false; |
| 3262 | } |
| 3263 | |
| 3264 | current->flags &= ~PF_KCOMPACTD; |
| 3265 | |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * This kcompactd start function will be called by init and node-hot-add. |
| 3271 | * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added. |
| 3272 | */ |
| 3273 | void __meminit kcompactd_run(int nid) |
| 3274 | { |
| 3275 | pg_data_t *pgdat = NODE_DATA(nid); |
| 3276 | |
| 3277 | if (pgdat->kcompactd) |
| 3278 | return; |
| 3279 | |
| 3280 | pgdat->kcompactd = kthread_create_on_node(kcompactd, pgdat, nid, "kcompactd%d", nid); |
| 3281 | if (IS_ERR(pgdat->kcompactd)) { |
| 3282 | pr_err("Failed to start kcompactd on node %d\n", nid); |
| 3283 | pgdat->kcompactd = NULL; |
| 3284 | } else { |
| 3285 | wake_up_process(pgdat->kcompactd); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* |
| 3290 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
| 3291 | * be holding mem_hotplug_begin/done(). |
| 3292 | */ |
| 3293 | void __meminit kcompactd_stop(int nid) |
| 3294 | { |
| 3295 | struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd; |
| 3296 | |
| 3297 | if (kcompactd) { |
| 3298 | kthread_stop(kcompactd); |
| 3299 | NODE_DATA(nid)->kcompactd = NULL; |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | static int proc_dointvec_minmax_warn_RT_change(const struct ctl_table *table, |
| 3304 | int write, void *buffer, size_t *lenp, loff_t *ppos) |
| 3305 | { |
| 3306 | int ret, old; |
| 3307 | |
| 3308 | if (!IS_ENABLED(CONFIG_PREEMPT_RT) || !write) |
| 3309 | return proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
| 3310 | |
| 3311 | old = *(int *)table->data; |
| 3312 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
| 3313 | if (ret) |
| 3314 | return ret; |
| 3315 | if (old != *(int *)table->data) |
| 3316 | pr_warn_once("sysctl attribute %s changed by %s[%d]\n", |
| 3317 | table->procname, current->comm, |
| 3318 | task_pid_nr(current)); |
| 3319 | return ret; |
| 3320 | } |
| 3321 | |
| 3322 | static const struct ctl_table vm_compaction[] = { |
| 3323 | { |
| 3324 | .procname = "compact_memory", |
| 3325 | .data = &sysctl_compact_memory, |
| 3326 | .maxlen = sizeof(int), |
| 3327 | .mode = 0200, |
| 3328 | .proc_handler = sysctl_compaction_handler, |
| 3329 | }, |
| 3330 | { |
| 3331 | .procname = "compaction_proactiveness", |
| 3332 | .data = &sysctl_compaction_proactiveness, |
| 3333 | .maxlen = sizeof(sysctl_compaction_proactiveness), |
| 3334 | .mode = 0644, |
| 3335 | .proc_handler = compaction_proactiveness_sysctl_handler, |
| 3336 | .extra1 = SYSCTL_ZERO, |
| 3337 | .extra2 = SYSCTL_ONE_HUNDRED, |
| 3338 | }, |
| 3339 | { |
| 3340 | .procname = "extfrag_threshold", |
| 3341 | .data = &sysctl_extfrag_threshold, |
| 3342 | .maxlen = sizeof(int), |
| 3343 | .mode = 0644, |
| 3344 | .proc_handler = proc_dointvec_minmax, |
| 3345 | .extra1 = SYSCTL_ZERO, |
| 3346 | .extra2 = SYSCTL_ONE_THOUSAND, |
| 3347 | }, |
| 3348 | { |
| 3349 | .procname = "compact_unevictable_allowed", |
| 3350 | .data = &sysctl_compact_unevictable_allowed, |
| 3351 | .maxlen = sizeof(int), |
| 3352 | .mode = 0644, |
| 3353 | .proc_handler = proc_dointvec_minmax_warn_RT_change, |
| 3354 | .extra1 = SYSCTL_ZERO, |
| 3355 | .extra2 = SYSCTL_ONE, |
| 3356 | }, |
| 3357 | }; |
| 3358 | |
| 3359 | static int __init kcompactd_init(void) |
| 3360 | { |
| 3361 | int nid; |
| 3362 | |
| 3363 | for_each_node_state(nid, N_MEMORY) |
| 3364 | kcompactd_run(nid); |
| 3365 | register_sysctl_init("vm", vm_compaction); |
| 3366 | return 0; |
| 3367 | } |
| 3368 | subsys_initcall(kcompactd_init) |
| 3369 | |
| 3370 | #endif /* CONFIG_COMPACTION */ |