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 }; 36 37 static bool page_owner_enabled = false; 38 DEFINE_STATIC_KEY_FALSE(page_owner_inited); 39 40 static depot_stack_handle_t dummy_handle; 41 static depot_stack_handle_t failure_handle; 42 static depot_stack_handle_t early_handle; 43 44 static void init_early_allocated_pages(void); 45 46 static int __init early_page_owner_param(char *buf) 47 { 48 return kstrtobool(buf, &page_owner_enabled); 49 } 50 early_param("page_owner", early_page_owner_param); 51 52 static __init bool need_page_owner(void) 53 { 54 return page_owner_enabled; 55 } 56 57 static __always_inline depot_stack_handle_t create_dummy_stack(void) 58 { 59 unsigned long entries[4]; 60 unsigned int nr_entries; 61 62 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); 63 return stack_depot_save(entries, nr_entries, GFP_KERNEL); 64 } 65 66 static noinline void register_dummy_stack(void) 67 { 68 dummy_handle = create_dummy_stack(); 69 } 70 71 static noinline void register_failure_stack(void) 72 { 73 failure_handle = create_dummy_stack(); 74 } 75 76 static noinline void register_early_stack(void) 77 { 78 early_handle = create_dummy_stack(); 79 } 80 81 static __init void init_page_owner(void) 82 { 83 if (!page_owner_enabled) 84 return; 85 86 stack_depot_init(); 87 88 register_dummy_stack(); 89 register_failure_stack(); 90 register_early_stack(); 91 static_branch_enable(&page_owner_inited); 92 init_early_allocated_pages(); 93 } 94 95 struct page_ext_operations page_owner_ops = { 96 .size = sizeof(struct page_owner), 97 .need = need_page_owner, 98 .init = init_page_owner, 99 }; 100 101 static inline struct page_owner *get_page_owner(struct page_ext *page_ext) 102 { 103 return (void *)page_ext + page_owner_ops.offset; 104 } 105 106 static noinline depot_stack_handle_t save_stack(gfp_t flags) 107 { 108 unsigned long entries[PAGE_OWNER_STACK_DEPTH]; 109 depot_stack_handle_t handle; 110 unsigned int nr_entries; 111 112 /* 113 * Avoid recursion. 114 * 115 * Sometimes page metadata allocation tracking requires more 116 * memory to be allocated: 117 * - when new stack trace is saved to stack depot 118 * - when backtrace itself is calculated (ia64) 119 */ 120 if (current->in_page_owner) 121 return dummy_handle; 122 current->in_page_owner = 1; 123 124 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); 125 handle = stack_depot_save(entries, nr_entries, flags); 126 if (!handle) 127 handle = failure_handle; 128 129 current->in_page_owner = 0; 130 return handle; 131 } 132 133 void __reset_page_owner(struct page *page, unsigned short order) 134 { 135 int i; 136 struct page_ext *page_ext; 137 depot_stack_handle_t handle; 138 struct page_owner *page_owner; 139 u64 free_ts_nsec = local_clock(); 140 141 page_ext = lookup_page_ext(page); 142 if (unlikely(!page_ext)) 143 return; 144 145 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); 146 for (i = 0; i < (1 << order); i++) { 147 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); 148 page_owner = get_page_owner(page_ext); 149 page_owner->free_handle = handle; 150 page_owner->free_ts_nsec = free_ts_nsec; 151 page_ext = page_ext_next(page_ext); 152 } 153 } 154 155 static inline void __set_page_owner_handle(struct page_ext *page_ext, 156 depot_stack_handle_t handle, 157 unsigned short order, gfp_t gfp_mask) 158 { 159 struct page_owner *page_owner; 160 int i; 161 162 for (i = 0; i < (1 << order); i++) { 163 page_owner = get_page_owner(page_ext); 164 page_owner->handle = handle; 165 page_owner->order = order; 166 page_owner->gfp_mask = gfp_mask; 167 page_owner->last_migrate_reason = -1; 168 page_owner->pid = current->pid; 169 page_owner->tgid = current->tgid; 170 page_owner->ts_nsec = local_clock(); 171 strlcpy(page_owner->comm, current->comm, 172 sizeof(page_owner->comm)); 173 __set_bit(PAGE_EXT_OWNER, &page_ext->flags); 174 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); 175 176 page_ext = page_ext_next(page_ext); 177 } 178 } 179 180 noinline void __set_page_owner(struct page *page, unsigned short order, 181 gfp_t gfp_mask) 182 { 183 struct page_ext *page_ext = lookup_page_ext(page); 184 depot_stack_handle_t handle; 185 186 if (unlikely(!page_ext)) 187 return; 188 189 handle = save_stack(gfp_mask); 190 __set_page_owner_handle(page_ext, handle, order, gfp_mask); 191 } 192 193 void __set_page_owner_migrate_reason(struct page *page, int reason) 194 { 195 struct page_ext *page_ext = lookup_page_ext(page); 196 struct page_owner *page_owner; 197 198 if (unlikely(!page_ext)) 199 return; 200 201 page_owner = get_page_owner(page_ext); 202 page_owner->last_migrate_reason = reason; 203 } 204 205 void __split_page_owner(struct page *page, unsigned int nr) 206 { 207 int i; 208 struct page_ext *page_ext = lookup_page_ext(page); 209 struct page_owner *page_owner; 210 211 if (unlikely(!page_ext)) 212 return; 213 214 for (i = 0; i < nr; i++) { 215 page_owner = get_page_owner(page_ext); 216 page_owner->order = 0; 217 page_ext = page_ext_next(page_ext); 218 } 219 } 220 221 void __folio_copy_owner(struct folio *newfolio, struct folio *old) 222 { 223 struct page_ext *old_ext = lookup_page_ext(&old->page); 224 struct page_ext *new_ext = lookup_page_ext(&newfolio->page); 225 struct page_owner *old_page_owner, *new_page_owner; 226 227 if (unlikely(!old_ext || !new_ext)) 228 return; 229 230 old_page_owner = get_page_owner(old_ext); 231 new_page_owner = get_page_owner(new_ext); 232 new_page_owner->order = old_page_owner->order; 233 new_page_owner->gfp_mask = old_page_owner->gfp_mask; 234 new_page_owner->last_migrate_reason = 235 old_page_owner->last_migrate_reason; 236 new_page_owner->handle = old_page_owner->handle; 237 new_page_owner->pid = old_page_owner->pid; 238 new_page_owner->tgid = old_page_owner->tgid; 239 new_page_owner->ts_nsec = old_page_owner->ts_nsec; 240 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; 241 strcpy(new_page_owner->comm, old_page_owner->comm); 242 243 /* 244 * We don't clear the bit on the old folio as it's going to be freed 245 * after migration. Until then, the info can be useful in case of 246 * a bug, and the overall stats will be off a bit only temporarily. 247 * Also, migrate_misplaced_transhuge_page() can still fail the 248 * migration and then we want the old folio to retain the info. But 249 * in that case we also don't need to explicitly clear the info from 250 * the new page, which will be freed. 251 */ 252 __set_bit(PAGE_EXT_OWNER, &new_ext->flags); 253 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags); 254 } 255 256 void pagetypeinfo_showmixedcount_print(struct seq_file *m, 257 pg_data_t *pgdat, struct zone *zone) 258 { 259 struct page *page; 260 struct page_ext *page_ext; 261 struct page_owner *page_owner; 262 unsigned long pfn, block_end_pfn; 263 unsigned long end_pfn = zone_end_pfn(zone); 264 unsigned long count[MIGRATE_TYPES] = { 0, }; 265 int pageblock_mt, page_mt; 266 int i; 267 268 /* Scan block by block. First and last block may be incomplete */ 269 pfn = zone->zone_start_pfn; 270 271 /* 272 * Walk the zone in pageblock_nr_pages steps. If a page block spans 273 * a zone boundary, it will be double counted between zones. This does 274 * not matter as the mixed block count will still be correct 275 */ 276 for (; pfn < end_pfn; ) { 277 page = pfn_to_online_page(pfn); 278 if (!page) { 279 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); 280 continue; 281 } 282 283 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 284 block_end_pfn = min(block_end_pfn, end_pfn); 285 286 pageblock_mt = get_pageblock_migratetype(page); 287 288 for (; pfn < block_end_pfn; pfn++) { 289 /* The pageblock is online, no need to recheck. */ 290 page = pfn_to_page(pfn); 291 292 if (page_zone(page) != zone) 293 continue; 294 295 if (PageBuddy(page)) { 296 unsigned long freepage_order; 297 298 freepage_order = buddy_order_unsafe(page); 299 if (freepage_order < MAX_ORDER) 300 pfn += (1UL << freepage_order) - 1; 301 continue; 302 } 303 304 if (PageReserved(page)) 305 continue; 306 307 page_ext = lookup_page_ext(page); 308 if (unlikely(!page_ext)) 309 continue; 310 311 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 312 continue; 313 314 page_owner = get_page_owner(page_ext); 315 page_mt = gfp_migratetype(page_owner->gfp_mask); 316 if (pageblock_mt != page_mt) { 317 if (is_migrate_cma(pageblock_mt)) 318 count[MIGRATE_MOVABLE]++; 319 else 320 count[pageblock_mt]++; 321 322 pfn = block_end_pfn; 323 break; 324 } 325 pfn += (1UL << page_owner->order) - 1; 326 } 327 } 328 329 /* Print counts */ 330 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); 331 for (i = 0; i < MIGRATE_TYPES; i++) 332 seq_printf(m, "%12lu ", count[i]); 333 seq_putc(m, '\n'); 334 } 335 336 /* 337 * Looking for memcg information and print it out 338 */ 339 static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret, 340 struct page *page) 341 { 342 #ifdef CONFIG_MEMCG 343 unsigned long memcg_data; 344 struct mem_cgroup *memcg; 345 bool online; 346 char name[80]; 347 348 rcu_read_lock(); 349 memcg_data = READ_ONCE(page->memcg_data); 350 if (!memcg_data) 351 goto out_unlock; 352 353 if (memcg_data & MEMCG_DATA_OBJCGS) 354 ret += scnprintf(kbuf + ret, count - ret, 355 "Slab cache page\n"); 356 357 memcg = page_memcg_check(page); 358 if (!memcg) 359 goto out_unlock; 360 361 online = (memcg->css.flags & CSS_ONLINE); 362 cgroup_name(memcg->css.cgroup, name, sizeof(name)); 363 ret += scnprintf(kbuf + ret, count - ret, 364 "Charged %sto %smemcg %s\n", 365 PageMemcgKmem(page) ? "(via objcg) " : "", 366 online ? "" : "offline ", 367 name); 368 out_unlock: 369 rcu_read_unlock(); 370 #endif /* CONFIG_MEMCG */ 371 372 return ret; 373 } 374 375 static ssize_t 376 print_page_owner(char __user *buf, size_t count, unsigned long pfn, 377 struct page *page, struct page_owner *page_owner, 378 depot_stack_handle_t handle) 379 { 380 int ret, pageblock_mt, page_mt; 381 char *kbuf; 382 383 count = min_t(size_t, count, PAGE_SIZE); 384 kbuf = kmalloc(count, GFP_KERNEL); 385 if (!kbuf) 386 return -ENOMEM; 387 388 ret = scnprintf(kbuf, count, 389 "Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns, free_ts %llu ns\n", 390 page_owner->order, page_owner->gfp_mask, 391 &page_owner->gfp_mask, page_owner->pid, 392 page_owner->tgid, page_owner->comm, 393 page_owner->ts_nsec, page_owner->free_ts_nsec); 394 395 /* Print information relevant to grouping pages by mobility */ 396 pageblock_mt = get_pageblock_migratetype(page); 397 page_mt = gfp_migratetype(page_owner->gfp_mask); 398 ret += scnprintf(kbuf + ret, count - ret, 399 "PFN %lu type %s Block %lu type %s Flags %pGp\n", 400 pfn, 401 migratetype_names[page_mt], 402 pfn >> pageblock_order, 403 migratetype_names[pageblock_mt], 404 &page->flags); 405 406 ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); 407 if (ret >= count) 408 goto err; 409 410 if (page_owner->last_migrate_reason != -1) { 411 ret += scnprintf(kbuf + ret, count - ret, 412 "Page has been migrated, last migrate reason: %s\n", 413 migrate_reason_names[page_owner->last_migrate_reason]); 414 } 415 416 ret = print_page_owner_memcg(kbuf, count, ret, page); 417 418 ret += snprintf(kbuf + ret, count - ret, "\n"); 419 if (ret >= count) 420 goto err; 421 422 if (copy_to_user(buf, kbuf, ret)) 423 ret = -EFAULT; 424 425 kfree(kbuf); 426 return ret; 427 428 err: 429 kfree(kbuf); 430 return -ENOMEM; 431 } 432 433 void __dump_page_owner(const struct page *page) 434 { 435 struct page_ext *page_ext = lookup_page_ext(page); 436 struct page_owner *page_owner; 437 depot_stack_handle_t handle; 438 gfp_t gfp_mask; 439 int mt; 440 441 if (unlikely(!page_ext)) { 442 pr_alert("There is not page extension available.\n"); 443 return; 444 } 445 446 page_owner = get_page_owner(page_ext); 447 gfp_mask = page_owner->gfp_mask; 448 mt = gfp_migratetype(gfp_mask); 449 450 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { 451 pr_alert("page_owner info is not present (never set?)\n"); 452 return; 453 } 454 455 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 456 pr_alert("page_owner tracks the page as allocated\n"); 457 else 458 pr_alert("page_owner tracks the page as freed\n"); 459 460 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", 461 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask, 462 page_owner->pid, page_owner->tgid, page_owner->comm, 463 page_owner->ts_nsec, page_owner->free_ts_nsec); 464 465 handle = READ_ONCE(page_owner->handle); 466 if (!handle) 467 pr_alert("page_owner allocation stack trace missing\n"); 468 else 469 stack_depot_print(handle); 470 471 handle = READ_ONCE(page_owner->free_handle); 472 if (!handle) { 473 pr_alert("page_owner free stack trace missing\n"); 474 } else { 475 pr_alert("page last free stack trace:\n"); 476 stack_depot_print(handle); 477 } 478 479 if (page_owner->last_migrate_reason != -1) 480 pr_alert("page has been migrated, last migrate reason: %s\n", 481 migrate_reason_names[page_owner->last_migrate_reason]); 482 } 483 484 static ssize_t 485 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) 486 { 487 unsigned long pfn; 488 struct page *page; 489 struct page_ext *page_ext; 490 struct page_owner *page_owner; 491 depot_stack_handle_t handle; 492 493 if (!static_branch_unlikely(&page_owner_inited)) 494 return -EINVAL; 495 496 page = NULL; 497 pfn = min_low_pfn + *ppos; 498 499 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ 500 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0) 501 pfn++; 502 503 drain_all_pages(NULL); 504 505 /* Find an allocated page */ 506 for (; pfn < max_pfn; pfn++) { 507 /* 508 * If the new page is in a new MAX_ORDER_NR_PAGES area, 509 * validate the area as existing, skip it if not 510 */ 511 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) { 512 pfn += MAX_ORDER_NR_PAGES - 1; 513 continue; 514 } 515 516 page = pfn_to_page(pfn); 517 if (PageBuddy(page)) { 518 unsigned long freepage_order = buddy_order_unsafe(page); 519 520 if (freepage_order < MAX_ORDER) 521 pfn += (1UL << freepage_order) - 1; 522 continue; 523 } 524 525 page_ext = lookup_page_ext(page); 526 if (unlikely(!page_ext)) 527 continue; 528 529 /* 530 * Some pages could be missed by concurrent allocation or free, 531 * because we don't hold the zone lock. 532 */ 533 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) 534 continue; 535 536 /* 537 * Although we do have the info about past allocation of free 538 * pages, it's not relevant for current memory usage. 539 */ 540 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) 541 continue; 542 543 page_owner = get_page_owner(page_ext); 544 545 /* 546 * Don't print "tail" pages of high-order allocations as that 547 * would inflate the stats. 548 */ 549 if (!IS_ALIGNED(pfn, 1 << page_owner->order)) 550 continue; 551 552 /* 553 * Access to page_ext->handle isn't synchronous so we should 554 * be careful to access it. 555 */ 556 handle = READ_ONCE(page_owner->handle); 557 if (!handle) 558 continue; 559 560 /* Record the next PFN to read in the file offset */ 561 *ppos = (pfn - min_low_pfn) + 1; 562 563 return print_page_owner(buf, count, pfn, page, 564 page_owner, handle); 565 } 566 567 return 0; 568 } 569 570 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) 571 { 572 unsigned long pfn = zone->zone_start_pfn; 573 unsigned long end_pfn = zone_end_pfn(zone); 574 unsigned long count = 0; 575 576 /* 577 * Walk the zone in pageblock_nr_pages steps. If a page block spans 578 * a zone boundary, it will be double counted between zones. This does 579 * not matter as the mixed block count will still be correct 580 */ 581 for (; pfn < end_pfn; ) { 582 unsigned long block_end_pfn; 583 584 if (!pfn_valid(pfn)) { 585 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); 586 continue; 587 } 588 589 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); 590 block_end_pfn = min(block_end_pfn, end_pfn); 591 592 for (; pfn < block_end_pfn; pfn++) { 593 struct page *page = pfn_to_page(pfn); 594 struct page_ext *page_ext; 595 596 if (page_zone(page) != zone) 597 continue; 598 599 /* 600 * To avoid having to grab zone->lock, be a little 601 * careful when reading buddy page order. The only 602 * danger is that we skip too much and potentially miss 603 * some early allocated pages, which is better than 604 * heavy lock contention. 605 */ 606 if (PageBuddy(page)) { 607 unsigned long order = buddy_order_unsafe(page); 608 609 if (order > 0 && order < MAX_ORDER) 610 pfn += (1UL << order) - 1; 611 continue; 612 } 613 614 if (PageReserved(page)) 615 continue; 616 617 page_ext = lookup_page_ext(page); 618 if (unlikely(!page_ext)) 619 continue; 620 621 /* Maybe overlapping zone */ 622 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) 623 continue; 624 625 /* Found early allocated page */ 626 __set_page_owner_handle(page_ext, early_handle, 627 0, 0); 628 count++; 629 } 630 cond_resched(); 631 } 632 633 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n", 634 pgdat->node_id, zone->name, count); 635 } 636 637 static void init_zones_in_node(pg_data_t *pgdat) 638 { 639 struct zone *zone; 640 struct zone *node_zones = pgdat->node_zones; 641 642 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { 643 if (!populated_zone(zone)) 644 continue; 645 646 init_pages_in_zone(pgdat, zone); 647 } 648 } 649 650 static void init_early_allocated_pages(void) 651 { 652 pg_data_t *pgdat; 653 654 for_each_online_pgdat(pgdat) 655 init_zones_in_node(pgdat); 656 } 657 658 static const struct file_operations proc_page_owner_operations = { 659 .read = read_page_owner, 660 }; 661 662 static int __init pageowner_init(void) 663 { 664 if (!static_branch_unlikely(&page_owner_inited)) { 665 pr_info("page_owner is disabled\n"); 666 return 0; 667 } 668 669 debugfs_create_file("page_owner", 0400, NULL, NULL, 670 &proc_page_owner_operations); 671 672 return 0; 673 } 674 late_initcall(pageowner_init) 675