1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/debugfs.h> 3 #include <linux/mm.h> 4 #include <linux/slab.h> 5 #include <linux/uaccess.h> 6 #include <linux/memblock.h> 7 #include <linux/stacktrace.h> 8 #include <linux/page_owner.h> 9 #include <linux/jump_label.h> 10 #include <linux/migrate.h> 11 #include <linux/stackdepot.h> 12 #include <linux/seq_file.h> 13 #include <linux/memcontrol.h> 14 #include <linux/sched/clock.h> 15 16 #include "internal.h" 17 18 /* 19 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack) 20 * to use off stack temporal storage 21 */ 22 #define PAGE_OWNER_STACK_DEPTH (16) 23 24 struct page_owner { 25 unsigned short order; 26 short last_migrate_reason; 27 gfp_t gfp_mask; 28 depot_stack_handle_t handle; 29 depot_stack_handle_t free_handle; 30 u64 ts_nsec; 31 u64 free_ts_nsec; 32 char comm[TASK_COMM_LEN]; 33 pid_t pid; 34 pid_t tgid; 35 pid_t free_pid; 36 pid_t free_tgid; 37 }; 38 39 struct stack { 40 struct stack_record *stack_record; 41 struct stack *next; 42 }; 43 static struct stack dummy_stack; 44 static struct stack failure_stack; 45 static struct stack *stack_list; 46 static DEFINE_SPINLOCK(stack_list_lock); 47 48 static bool page_owner_enabled __initdata; 49 DEFINE_STATIC_KEY_FALSE(page_owner_inited); 50 51 static depot_stack_handle_t dummy_handle; 52 static depot_stack_handle_t failure_handle; 53 static depot_stack_handle_t early_handle; 54 55 static void init_early_allocated_pages(void); 56 57 static inline void set_current_in_page_owner(void) 58 { 59 /* 60 * Avoid recursion. 61 * 62 * We might need to allocate more memory from page_owner code, so make 63 * sure to signal it in order to avoid recursion. 64 */ 65 current->in_page_owner = 1; 66 } 67 68 static inline void unset_current_in_page_owner(void) 69 { 70 current->in_page_owner = 0; 71 } 72 73 static int __init early_page_owner_param(char *buf) 74 { 75 int ret = kstrtobool(buf, &page_owner_enabled); 76 77 if (page_owner_enabled) 78 stack_depot_request_early_init(); 79 80 return ret; 81 } 82 early_param("page_owner", early_page_owner_param); 83 84 static __init bool need_page_owner(void) 85 { 86 return page_owner_enabled; 87 } 88 89 static __always_inline depot_stack_handle_t create_dummy_stack(void) 90 { 91 unsigned long entries[4]; 92 unsigned int nr_entries; 93 94 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); 95 return stack_depot_save(entries, nr_entries, GFP_KERNEL); 96 } 97 98 static noinline void register_dummy_stack(void) 99 { 100 dummy_handle = create_dummy_stack(); 101 } 102 103 static noinline void register_failure_stack(void) 104 { 105 failure_handle = create_dummy_stack(); 106 } 107 108 static noinline void register_early_stack(void) 109 { 110 early_handle = create_dummy_stack(); 111 } 112 113 static __init void init_page_owner(void) 114 { 115 if (!page_owner_enabled) 116 return; 117 118 register_dummy_stack(); 119 register_failure_stack(); 120 register_early_stack(); 121 static_branch_enable(&page_owner_inited); 122 init_early_allocated_pages(); 123 /* Initialize dummy and failure stacks and link them to stack_list */ 124 dummy_stack.stack_record = __stack_depot_get_stack_record(dummy_handle); 125 failure_stack.stack_record = __stack_depot_get_stack_record(failure_handle); 126 if (dummy_stack.stack_record) 127 refcount_set(&dummy_stack.stack_record->count, 1); 128 if (failure_stack.stack_record) 129 refcount_set(&failure_stack.stack_record->count, 1); 130 dummy_stack.next = &failure_stack; 131 stack_list = &dummy_stack; 132 } 133 134 struct page_ext_operations page_owner_ops = { 135 .size = sizeof(struct page_owner), 136 .need = need_page_owner, 137 .init = init_page_owner, 138 .need_shared_flags = true, 139 }; 140 141 static inline struct page_owner *get_page_owner(struct page_ext *page_ext) 142 { 143 return page_ext_data(page_ext, &page_owner_ops); 144 } 145 146 static noinline depot_stack_handle_t save_stack(gfp_t flags) 147 { 148 unsigned long entries[PAGE_OWNER_STACK_DEPTH]; 149 depot_stack_handle_t handle; 150 unsigned int nr_entries; 151 152 if (current->in_page_owner) 153 return dummy_handle; 154 155 set_current_in_page_owner(); 156 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); 157 handle = stack_depot_save(entries, nr_entries, flags); 158 if (!handle) 159 handle = failure_handle; 160 unset_current_in_page_owner(); 161 162 return handle; 163 } 164 165 static void add_stack_record_to_list(struct stack_record *stack_record, 166 gfp_t gfp_mask) 167 { 168 unsigned long flags; 169 struct stack *stack; 170 171 /* Filter gfp_mask the same way stackdepot does, for consistency */ 172 gfp_mask &= ~GFP_ZONEMASK; 173 gfp_mask &= (GFP_ATOMIC | GFP_KERNEL); 174 gfp_mask |= __GFP_NOWARN; 175 176 set_current_in_page_owner(); 177 stack = kmalloc(sizeof(*stack), gfp_mask); 178 if (!stack) { 179 unset_current_in_page_owner(); 180 return; 181 } 182 unset_current_in_page_owner(); 183 184 stack->stack_record = stack_record; 185 stack->next = NULL; 186 187 spin_lock_irqsave(&stack_list_lock, flags); 188 stack->next = stack_list; 189 /* 190 * This pairs with smp_load_acquire() from function 191 * stack_start(). This guarantees that stack_start() 192 * will see an updated stack_list before starting to 193 * traverse the list. 194 */ 195 smp_store_release(&stack_list, stack); 196 spin_unlock_irqrestore(&stack_list_lock, flags); 197 } 198 199 static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask) 200 { 201 struct stack_record *stack_record = __stack_depot_get_stack_record(handle); 202 203 if (!stack_record) 204 return; 205 206 /* 207 * New stack_record's that do not use STACK_DEPOT_FLAG_GET start 208 * with REFCOUNT_SATURATED to catch spurious increments of their 209 * refcount. 210 * Since we do not use STACK_DEPOT_FLAG_GET API, let us 211 * set a refcount of 1 ourselves. 212 */ 213 if (refcount_read(&stack_record->count) == REFCOUNT_SATURATED) { 214 int old = REFCOUNT_SATURATED; 215 216 if (atomic_try_cmpxchg_relaxed(&stack_record->count.refs, &old, 1)) 217 /* Add the new stack_record to our list */ 218 add_stack_record_to_list(stack_record, gfp_mask); 219 } 220 refcount_inc(&stack_record->count); 221 } 222 223 static void dec_stack_record_count(depot_stack_handle_t handle) 224 { 225 struct stack_record *stack_record = __stack_depot_get_stack_record(handle); 226 227 if (stack_record) 228 refcount_dec(&stack_record->count); 229 } 230 231 void __reset_page_owner(struct page *page, unsigned short order) 232 { 233 int i; 234 struct page_ext *page_ext; 235 depot_stack_handle_t handle; 236 depot_stack_handle_t alloc_handle; 237 struct page_owner *page_owner; 238 u64 free_ts_nsec = local_clock(); 239 240 page_ext = page_ext_get(page); 241 if (unlikely(!page_ext)) 242 return; 243 244 page_owner = get_page_owner(page_ext); 245 alloc_handle = page_owner->handle; 246 247 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); 248 for (i = 0; i < (1 << order); i++) { 249 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); 250 page_owner->free_handle = handle; 251 page_owner->free_ts_nsec = free_ts_nsec; 252 page_owner->free_pid = current->pid; 253 page_owner->free_tgid = current->tgid; 254 page_ext = page_ext_next(page_ext); 255 page_owner = get_page_owner(page_ext); 256 } 257 page_ext_put(page_ext); 258 if (alloc_handle != early_handle) 259 /* 260 * early_handle is being set as a handle for all those 261 * early allocated pages. See init_pages_in_zone(). 262 * Since their refcount is not being incremented because 263 * the machinery is not ready yet, we cannot decrement 264 * their refcount either. 265 */ 266 dec_stack_record_count(alloc_handle); 267 } 268 269 static inline void __set_page_owner_handle(struct page_ext *page_ext, 270 depot_stack_handle_t handle, 271 unsigned short order, gfp_t gfp_mask) 272 { 273 struct page_owner *page_owner; 274 int i; 275 u64 ts_nsec = local_clock(); 276 277 for (i = 0; i < (1 << order); i++) { 278 page_owner = get_page_owner(page_ext); 279 page_owner->handle = handle; 280 page_owner->order = order; 281 page_owner->gfp_mask = gfp_mask; 282 page_owner->last_migrate_reason = -1; 283 page_owner->pid = current->pid; 284 page_owner->tgid = current->tgid; 285 page_owner->ts_nsec = ts_nsec; 286 strscpy(page_owner->comm, current->comm, 287 sizeof(page_owner->comm)); 288 __set_bit(PAGE_EXT_OWNER, &page_ext->flags); 289 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); 290 291 page_ext = page_ext_next(page_ext); 292 } 293 } 294 295 noinline void __set_page_owner(struct page *page, unsigned short order, 296 gfp_t gfp_mask) 297 { 298 struct page_ext *page_ext; 299 depot_stack_handle_t handle; 300 301 handle = save_stack(gfp_mask); 302 303 page_ext = page_ext_get(page); 304 if (unlikely(!page_ext)) 305 return; 306 __set_page_owner_handle(page_ext, handle, order, gfp_mask); 307 page_ext_put(page_ext); 308 inc_stack_record_count(handle, gfp_mask); 309 } 310 311 void __set_page_owner_migrate_reason(struct page *page, int reason) 312 { 313 struct page_ext *page_ext = page_ext_get(page); 314 struct page_owner *page_owner; 315 316 if (unlikely(!page_ext)) 317 return; 318 319 page_owner = get_page_owner(page_ext); 320 page_owner->last_migrate_reason = reason; 321 page_ext_put(page_ext); 322 } 323 324 void __split_page_owner(struct page *page, int old_order, int new_order) 325 { 326 int i; 327 struct page_ext *page_ext = page_ext_get(page); 328 struct page_owner *page_owner; 329 330 if (unlikely(!page_ext)) 331 return; 332 333 for (i = 0; i < (1 << old_order); i++) { 334 page_owner = get_page_owner(page_ext); 335 page_owner->order = new_order; 336 page_ext = page_ext_next(page_ext); 337 } 338 page_ext_put(page_ext); 339 } 340 341 void __folio_copy_owner(struct folio *newfolio, struct folio *old) 342 { 343 struct page_ext *old_ext; 344 struct page_ext *new_ext; 345 struct page_owner *old_page_owner, *new_page_owner; 346 347 old_ext = page_ext_get(&old->page); 348 if (unlikely(!old_ext)) 349 return; 350 351 new_ext = page_ext_get(&newfolio->page); 352 if (unlikely(!new_ext)) { 353 page_ext_put(old_ext); 354 return; 355 } 356 357 old_page_owner = get_page_owner(old_ext); 358 new_page_owner = get_page_owner(new_ext); 359 new_page_owner->order = old_page_owner->order; 360 new_page_owner->gfp_mask = old_page_owner->gfp_mask; 361 new_page_owner->last_migrate_reason = 362 old_page_owner->last_migrate_reason; 363 new_page_owner->handle = old_page_owner->handle; 364 new_page_owner->pid = old_page_owner->pid; 365 new_page_owner->tgid = old_page_owner->tgid; 366 new_page_owner->free_pid = old_page_owner->free_pid; 367 new_page_owner->free_tgid = old_page_owner->free_tgid; 368 new_page_owner->ts_nsec = old_page_owner->ts_nsec; 369 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; 370 strcpy(new_page_owner->comm, old_page_owner->comm); 371 372 /* 373 * We don't clear the bit on the old folio as it's going to be freed 374 * after migration. Until then, the info can be useful in case of 375 * a bug, and the overall stats will be off a bit only temporarily. 376 * Also, migrate_misplaced_transhuge_page() can still fail the 377 * migration and then we want the old folio to retain the info. But 378 * in that case we also don't need to explicitly clear the info from 379 * the new page, which will be freed. 380 */ 381 __set_bit(PAGE_EXT_OWNER, &new_ext->flags); 382 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags); 383 page_ext_put(new_ext); 384 page_ext_put(old_ext); 385 } 386 387 void pagetypeinfo_showmixedcount_print(struct seq_file *m, 388 pg_data_t *pgdat, struct zone *zone) 389 { 390 struct page *page; 391 struct page_ext *page_ext; 392 struct page_owner *page_owner; 393 unsigned long pfn, block_end_pfn; 394 unsigned long end_pfn = zone_end_pfn(zone); 395 unsigned long count[MIGRATE_TYPES] = { 0, }; 396 int pageblock_mt, page_mt; 397 int i; 398 399 /* Scan block by block. First and last block may be incomplete */ 400 pfn = zone->zone_start_pfn; 401 402 /* 403 * Walk the zone in pageblock_nr_pages steps. If a page block spans 404 * a zone boundary, it will be double counted between zones. This does 405 * not matter as the mixed block count will still be correct 406 */ 407 for (; pfn < end_pfn; ) { 408 page = pfn_to_online_page(pfn); 409 if (!page) { 410 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); 411 continue; 412 } 413 414 block_end_pfn = pageblock_end_pfn(pfn); 415 block_end_pfn = min(block_end_pfn, end_pfn); 416 417 pageblock_mt = get_pageblock_migratetype(page); 418 419 for (; pfn < block_end_pfn; pfn++) { 420 /* The pageblock is online, no need to recheck. */ 421 page = pfn_to_page(pfn); 422 423 if (page_zone(page) != zone) 424 continue; 425 426 if (PageBuddy(page)) { 427 unsigned long freepage_order; 428 429 freepage_order = buddy_order_unsafe(page); 430 if (freepage_order <= MAX_PAGE_ORDER) 431 pfn += (1UL << freepage_order) - 1; 432 continue; 433 } 434 435 if (PageReserved(page)) 436 continue; 437 438 page_ext = page_ext_get(page); 439 if (unlikely(!page_ext)) 440 continue; 441 442 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 443 goto ext_put_continue; 444 445 page_owner = get_page_owner(page_ext); 446 page_mt = gfp_migratetype(page_owner->gfp_mask); 447 if (pageblock_mt != page_mt) { 448 if (is_migrate_cma(pageblock_mt)) 449 count[MIGRATE_MOVABLE]++; 450 else 451 count[pageblock_mt]++; 452 453 pfn = block_end_pfn; 454 page_ext_put(page_ext); 455 break; 456 } 457 pfn += (1UL << page_owner->order) - 1; 458 ext_put_continue: 459 page_ext_put(page_ext); 460 } 461 } 462 463 /* Print counts */ 464 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); 465 for (i = 0; i < MIGRATE_TYPES; i++) 466 seq_printf(m, "%12lu ", count[i]); 467 seq_putc(m, '\n'); 468 } 469 470 /* 471 * Looking for memcg information and print it out 472 */ 473 static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret, 474 struct page *page) 475 { 476 #ifdef CONFIG_MEMCG 477 unsigned long memcg_data; 478 struct mem_cgroup *memcg; 479 bool online; 480 char name[80]; 481 482 rcu_read_lock(); 483 memcg_data = READ_ONCE(page->memcg_data); 484 if (!memcg_data) 485 goto out_unlock; 486 487 if (memcg_data & MEMCG_DATA_OBJCGS) 488 ret += scnprintf(kbuf + ret, count - ret, 489 "Slab cache page\n"); 490 491 memcg = page_memcg_check(page); 492 if (!memcg) 493 goto out_unlock; 494 495 online = (memcg->css.flags & CSS_ONLINE); 496 cgroup_name(memcg->css.cgroup, name, sizeof(name)); 497 ret += scnprintf(kbuf + ret, count - ret, 498 "Charged %sto %smemcg %s\n", 499 PageMemcgKmem(page) ? "(via objcg) " : "", 500 online ? "" : "offline ", 501 name); 502 out_unlock: 503 rcu_read_unlock(); 504 #endif /* CONFIG_MEMCG */ 505 506 return ret; 507 } 508 509 static ssize_t 510 print_page_owner(char __user *buf, size_t count, unsigned long pfn, 511 struct page *page, struct page_owner *page_owner, 512 depot_stack_handle_t handle) 513 { 514 int ret, pageblock_mt, page_mt; 515 char *kbuf; 516 517 count = min_t(size_t, count, PAGE_SIZE); 518 kbuf = kmalloc(count, GFP_KERNEL); 519 if (!kbuf) 520 return -ENOMEM; 521 522 ret = scnprintf(kbuf, count, 523 "Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns\n", 524 page_owner->order, page_owner->gfp_mask, 525 &page_owner->gfp_mask, page_owner->pid, 526 page_owner->tgid, page_owner->comm, 527 page_owner->ts_nsec); 528 529 /* Print information relevant to grouping pages by mobility */ 530 pageblock_mt = get_pageblock_migratetype(page); 531 page_mt = gfp_migratetype(page_owner->gfp_mask); 532 ret += scnprintf(kbuf + ret, count - ret, 533 "PFN 0x%lx type %s Block %lu type %s Flags %pGp\n", 534 pfn, 535 migratetype_names[page_mt], 536 pfn >> pageblock_order, 537 migratetype_names[pageblock_mt], 538 &page->flags); 539 540 ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); 541 if (ret >= count) 542 goto err; 543 544 if (page_owner->last_migrate_reason != -1) { 545 ret += scnprintf(kbuf + ret, count - ret, 546 "Page has been migrated, last migrate reason: %s\n", 547 migrate_reason_names[page_owner->last_migrate_reason]); 548 } 549 550 ret = print_page_owner_memcg(kbuf, count, ret, page); 551 552 ret += snprintf(kbuf + ret, count - ret, "\n"); 553 if (ret >= count) 554 goto err; 555 556 if (copy_to_user(buf, kbuf, ret)) 557 ret = -EFAULT; 558 559 kfree(kbuf); 560 return ret; 561 562 err: 563 kfree(kbuf); 564 return -ENOMEM; 565 } 566 567 void __dump_page_owner(const struct page *page) 568 { 569 struct page_ext *page_ext = page_ext_get((void *)page); 570 struct page_owner *page_owner; 571 depot_stack_handle_t handle; 572 gfp_t gfp_mask; 573 int mt; 574 575 if (unlikely(!page_ext)) { 576 pr_alert("There is not page extension available.\n"); 577 return; 578 } 579 580 page_owner = get_page_owner(page_ext); 581 gfp_mask = page_owner->gfp_mask; 582 mt = gfp_migratetype(gfp_mask); 583 584 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { 585 pr_alert("page_owner info is not present (never set?)\n"); 586 page_ext_put(page_ext); 587 return; 588 } 589 590 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 591 pr_alert("page_owner tracks the page as allocated\n"); 592 else 593 pr_alert("page_owner tracks the page as freed\n"); 594 595 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu, free_ts %llu\n", 596 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask, 597 page_owner->pid, page_owner->tgid, page_owner->comm, 598 page_owner->ts_nsec, page_owner->free_ts_nsec); 599 600 handle = READ_ONCE(page_owner->handle); 601 if (!handle) 602 pr_alert("page_owner allocation stack trace missing\n"); 603 else 604 stack_depot_print(handle); 605 606 handle = READ_ONCE(page_owner->free_handle); 607 if (!handle) { 608 pr_alert("page_owner free stack trace missing\n"); 609 } else { 610 pr_alert("page last free pid %d tgid %d stack trace:\n", 611 page_owner->free_pid, page_owner->free_tgid); 612 stack_depot_print(handle); 613 } 614 615 if (page_owner->last_migrate_reason != -1) 616 pr_alert("page has been migrated, last migrate reason: %s\n", 617 migrate_reason_names[page_owner->last_migrate_reason]); 618 page_ext_put(page_ext); 619 } 620 621 static ssize_t 622 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) 623 { 624 unsigned long pfn; 625 struct page *page; 626 struct page_ext *page_ext; 627 struct page_owner *page_owner; 628 depot_stack_handle_t handle; 629 630 if (!static_branch_unlikely(&page_owner_inited)) 631 return -EINVAL; 632 633 page = NULL; 634 if (*ppos == 0) 635 pfn = min_low_pfn; 636 else 637 pfn = *ppos; 638 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ 639 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0) 640 pfn++; 641 642 /* Find an allocated page */ 643 for (; pfn < max_pfn; pfn++) { 644 /* 645 * This temporary page_owner is required so 646 * that we can avoid the context switches while holding 647 * the rcu lock and copying the page owner information to 648 * user through copy_to_user() or GFP_KERNEL allocations. 649 */ 650 struct page_owner page_owner_tmp; 651 652 /* 653 * If the new page is in a new MAX_ORDER_NR_PAGES area, 654 * validate the area as existing, skip it if not 655 */ 656 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) { 657 pfn += MAX_ORDER_NR_PAGES - 1; 658 continue; 659 } 660 661 page = pfn_to_page(pfn); 662 if (PageBuddy(page)) { 663 unsigned long freepage_order = buddy_order_unsafe(page); 664 665 if (freepage_order <= MAX_PAGE_ORDER) 666 pfn += (1UL << freepage_order) - 1; 667 continue; 668 } 669 670 page_ext = page_ext_get(page); 671 if (unlikely(!page_ext)) 672 continue; 673 674 /* 675 * Some pages could be missed by concurrent allocation or free, 676 * because we don't hold the zone lock. 677 */ 678 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) 679 goto ext_put_continue; 680 681 /* 682 * Although we do have the info about past allocation of free 683 * pages, it's not relevant for current memory usage. 684 */ 685 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 686 goto ext_put_continue; 687 688 page_owner = get_page_owner(page_ext); 689 690 /* 691 * Don't print "tail" pages of high-order allocations as that 692 * would inflate the stats. 693 */ 694 if (!IS_ALIGNED(pfn, 1 << page_owner->order)) 695 goto ext_put_continue; 696 697 /* 698 * Access to page_ext->handle isn't synchronous so we should 699 * be careful to access it. 700 */ 701 handle = READ_ONCE(page_owner->handle); 702 if (!handle) 703 goto ext_put_continue; 704 705 /* Record the next PFN to read in the file offset */ 706 *ppos = pfn + 1; 707 708 page_owner_tmp = *page_owner; 709 page_ext_put(page_ext); 710 return print_page_owner(buf, count, pfn, page, 711 &page_owner_tmp, handle); 712 ext_put_continue: 713 page_ext_put(page_ext); 714 } 715 716 return 0; 717 } 718 719 static loff_t lseek_page_owner(struct file *file, loff_t offset, int orig) 720 { 721 switch (orig) { 722 case SEEK_SET: 723 file->f_pos = offset; 724 break; 725 case SEEK_CUR: 726 file->f_pos += offset; 727 break; 728 default: 729 return -EINVAL; 730 } 731 return file->f_pos; 732 } 733 734 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) 735 { 736 unsigned long pfn = zone->zone_start_pfn; 737 unsigned long end_pfn = zone_end_pfn(zone); 738 unsigned long count = 0; 739 740 /* 741 * Walk the zone in pageblock_nr_pages steps. If a page block spans 742 * a zone boundary, it will be double counted between zones. This does 743 * not matter as the mixed block count will still be correct 744 */ 745 for (; pfn < end_pfn; ) { 746 unsigned long block_end_pfn; 747 748 if (!pfn_valid(pfn)) { 749 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); 750 continue; 751 } 752 753 block_end_pfn = pageblock_end_pfn(pfn); 754 block_end_pfn = min(block_end_pfn, end_pfn); 755 756 for (; pfn < block_end_pfn; pfn++) { 757 struct page *page = pfn_to_page(pfn); 758 struct page_ext *page_ext; 759 760 if (page_zone(page) != zone) 761 continue; 762 763 /* 764 * To avoid having to grab zone->lock, be a little 765 * careful when reading buddy page order. The only 766 * danger is that we skip too much and potentially miss 767 * some early allocated pages, which is better than 768 * heavy lock contention. 769 */ 770 if (PageBuddy(page)) { 771 unsigned long order = buddy_order_unsafe(page); 772 773 if (order > 0 && order <= MAX_PAGE_ORDER) 774 pfn += (1UL << order) - 1; 775 continue; 776 } 777 778 if (PageReserved(page)) 779 continue; 780 781 page_ext = page_ext_get(page); 782 if (unlikely(!page_ext)) 783 continue; 784 785 /* Maybe overlapping zone */ 786 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) 787 goto ext_put_continue; 788 789 /* Found early allocated page */ 790 __set_page_owner_handle(page_ext, early_handle, 791 0, 0); 792 count++; 793 ext_put_continue: 794 page_ext_put(page_ext); 795 } 796 cond_resched(); 797 } 798 799 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n", 800 pgdat->node_id, zone->name, count); 801 } 802 803 static void init_zones_in_node(pg_data_t *pgdat) 804 { 805 struct zone *zone; 806 struct zone *node_zones = pgdat->node_zones; 807 808 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { 809 if (!populated_zone(zone)) 810 continue; 811 812 init_pages_in_zone(pgdat, zone); 813 } 814 } 815 816 static void init_early_allocated_pages(void) 817 { 818 pg_data_t *pgdat; 819 820 for_each_online_pgdat(pgdat) 821 init_zones_in_node(pgdat); 822 } 823 824 static const struct file_operations proc_page_owner_operations = { 825 .read = read_page_owner, 826 .llseek = lseek_page_owner, 827 }; 828 829 static void *stack_start(struct seq_file *m, loff_t *ppos) 830 { 831 struct stack *stack; 832 833 if (*ppos == -1UL) 834 return NULL; 835 836 if (!*ppos) { 837 /* 838 * This pairs with smp_store_release() from function 839 * add_stack_record_to_list(), so we get a consistent 840 * value of stack_list. 841 */ 842 stack = smp_load_acquire(&stack_list); 843 } else { 844 stack = m->private; 845 stack = stack->next; 846 } 847 848 m->private = stack; 849 850 return stack; 851 } 852 853 static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) 854 { 855 struct stack *stack = v; 856 857 stack = stack->next; 858 *ppos = stack ? *ppos + 1 : -1UL; 859 m->private = stack; 860 861 return stack; 862 } 863 864 static unsigned long page_owner_stack_threshold; 865 866 static int stack_print(struct seq_file *m, void *v) 867 { 868 int i, stack_count; 869 struct stack *stack = v; 870 unsigned long *entries; 871 unsigned long nr_entries; 872 struct stack_record *stack_record = stack->stack_record; 873 874 if (!stack->stack_record) 875 return 0; 876 877 nr_entries = stack_record->size; 878 entries = stack_record->entries; 879 stack_count = refcount_read(&stack_record->count) - 1; 880 881 if (stack_count < 1 || stack_count < page_owner_stack_threshold) 882 return 0; 883 884 for (i = 0; i < nr_entries; i++) 885 seq_printf(m, " %pS\n", (void *)entries[i]); 886 seq_printf(m, "stack_count: %d\n\n", stack_count); 887 888 return 0; 889 } 890 891 static void stack_stop(struct seq_file *m, void *v) 892 { 893 } 894 895 static const struct seq_operations page_owner_stack_op = { 896 .start = stack_start, 897 .next = stack_next, 898 .stop = stack_stop, 899 .show = stack_print 900 }; 901 902 static int page_owner_stack_open(struct inode *inode, struct file *file) 903 { 904 return seq_open_private(file, &page_owner_stack_op, 0); 905 } 906 907 static const struct file_operations page_owner_stack_operations = { 908 .open = page_owner_stack_open, 909 .read = seq_read, 910 .llseek = seq_lseek, 911 .release = seq_release, 912 }; 913 914 static int page_owner_threshold_get(void *data, u64 *val) 915 { 916 *val = READ_ONCE(page_owner_stack_threshold); 917 return 0; 918 } 919 920 static int page_owner_threshold_set(void *data, u64 val) 921 { 922 WRITE_ONCE(page_owner_stack_threshold, val); 923 return 0; 924 } 925 926 DEFINE_SIMPLE_ATTRIBUTE(proc_page_owner_threshold, &page_owner_threshold_get, 927 &page_owner_threshold_set, "%llu"); 928 929 930 static int __init pageowner_init(void) 931 { 932 struct dentry *dir; 933 934 if (!static_branch_unlikely(&page_owner_inited)) { 935 pr_info("page_owner is disabled\n"); 936 return 0; 937 } 938 939 debugfs_create_file("page_owner", 0400, NULL, NULL, 940 &proc_page_owner_operations); 941 dir = debugfs_create_dir("page_owner_stacks", NULL); 942 debugfs_create_file("show_stacks", 0400, dir, NULL, 943 &page_owner_stack_operations); 944 debugfs_create_file("count_threshold", 0600, dir, NULL, 945 &proc_page_owner_threshold); 946 947 return 0; 948 } 949 late_initcall(pageowner_init) 950