1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/debugfs.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>
19 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
20 * to use off stack temporal storage
22 #define PAGE_OWNER_STACK_DEPTH (16)
26 short last_migrate_reason;
28 depot_stack_handle_t handle;
29 depot_stack_handle_t free_handle;
32 char comm[TASK_COMM_LEN];
39 static bool page_owner_enabled __initdata;
40 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
42 static depot_stack_handle_t dummy_handle;
43 static depot_stack_handle_t failure_handle;
44 static depot_stack_handle_t early_handle;
46 static void init_early_allocated_pages(void);
48 static int __init early_page_owner_param(char *buf)
50 int ret = kstrtobool(buf, &page_owner_enabled);
52 if (page_owner_enabled)
53 stack_depot_request_early_init();
57 early_param("page_owner", early_page_owner_param);
59 static __init bool need_page_owner(void)
61 return page_owner_enabled;
64 static __always_inline depot_stack_handle_t create_dummy_stack(void)
66 unsigned long entries[4];
67 unsigned int nr_entries;
69 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
70 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
73 static noinline void register_dummy_stack(void)
75 dummy_handle = create_dummy_stack();
78 static noinline void register_failure_stack(void)
80 failure_handle = create_dummy_stack();
83 static noinline void register_early_stack(void)
85 early_handle = create_dummy_stack();
88 static __init void init_page_owner(void)
90 if (!page_owner_enabled)
93 register_dummy_stack();
94 register_failure_stack();
95 register_early_stack();
96 static_branch_enable(&page_owner_inited);
97 init_early_allocated_pages();
100 struct page_ext_operations page_owner_ops = {
101 .size = sizeof(struct page_owner),
102 .need = need_page_owner,
103 .init = init_page_owner,
104 .need_shared_flags = true,
107 static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
109 return page_ext_data(page_ext, &page_owner_ops);
112 static noinline depot_stack_handle_t save_stack(gfp_t flags)
114 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
115 depot_stack_handle_t handle;
116 unsigned int nr_entries;
121 * Sometimes page metadata allocation tracking requires more
122 * memory to be allocated:
123 * - when new stack trace is saved to stack depot
124 * - when backtrace itself is calculated (ia64)
126 if (current->in_page_owner)
128 current->in_page_owner = 1;
130 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
131 handle = stack_depot_save(entries, nr_entries, flags);
133 handle = failure_handle;
135 current->in_page_owner = 0;
139 void __reset_page_owner(struct page *page, unsigned short order)
142 struct page_ext *page_ext;
143 depot_stack_handle_t handle;
144 struct page_owner *page_owner;
145 u64 free_ts_nsec = local_clock();
147 page_ext = page_ext_get(page);
148 if (unlikely(!page_ext))
151 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
152 for (i = 0; i < (1 << order); i++) {
153 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
154 page_owner = get_page_owner(page_ext);
155 page_owner->free_handle = handle;
156 page_owner->free_ts_nsec = free_ts_nsec;
157 page_owner->free_pid = current->pid;
158 page_owner->free_tgid = current->tgid;
159 page_ext = page_ext_next(page_ext);
161 page_ext_put(page_ext);
164 static inline void __set_page_owner_handle(struct page_ext *page_ext,
165 depot_stack_handle_t handle,
166 unsigned short order, gfp_t gfp_mask)
168 struct page_owner *page_owner;
170 u64 ts_nsec = local_clock();
172 for (i = 0; i < (1 << order); i++) {
173 page_owner = get_page_owner(page_ext);
174 page_owner->handle = handle;
175 page_owner->order = order;
176 page_owner->gfp_mask = gfp_mask;
177 page_owner->last_migrate_reason = -1;
178 page_owner->pid = current->pid;
179 page_owner->tgid = current->tgid;
180 page_owner->ts_nsec = ts_nsec;
181 strscpy(page_owner->comm, current->comm,
182 sizeof(page_owner->comm));
183 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
184 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
186 page_ext = page_ext_next(page_ext);
190 noinline void __set_page_owner(struct page *page, unsigned short order,
193 struct page_ext *page_ext;
194 depot_stack_handle_t handle;
196 handle = save_stack(gfp_mask);
198 page_ext = page_ext_get(page);
199 if (unlikely(!page_ext))
201 __set_page_owner_handle(page_ext, handle, order, gfp_mask);
202 page_ext_put(page_ext);
205 void __set_page_owner_migrate_reason(struct page *page, int reason)
207 struct page_ext *page_ext = page_ext_get(page);
208 struct page_owner *page_owner;
210 if (unlikely(!page_ext))
213 page_owner = get_page_owner(page_ext);
214 page_owner->last_migrate_reason = reason;
215 page_ext_put(page_ext);
218 void __split_page_owner(struct page *page, unsigned int nr)
221 struct page_ext *page_ext = page_ext_get(page);
222 struct page_owner *page_owner;
224 if (unlikely(!page_ext))
227 for (i = 0; i < nr; i++) {
228 page_owner = get_page_owner(page_ext);
229 page_owner->order = 0;
230 page_ext = page_ext_next(page_ext);
232 page_ext_put(page_ext);
235 void __folio_copy_owner(struct folio *newfolio, struct folio *old)
237 struct page_ext *old_ext;
238 struct page_ext *new_ext;
239 struct page_owner *old_page_owner, *new_page_owner;
241 old_ext = page_ext_get(&old->page);
242 if (unlikely(!old_ext))
245 new_ext = page_ext_get(&newfolio->page);
246 if (unlikely(!new_ext)) {
247 page_ext_put(old_ext);
251 old_page_owner = get_page_owner(old_ext);
252 new_page_owner = get_page_owner(new_ext);
253 new_page_owner->order = old_page_owner->order;
254 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
255 new_page_owner->last_migrate_reason =
256 old_page_owner->last_migrate_reason;
257 new_page_owner->handle = old_page_owner->handle;
258 new_page_owner->pid = old_page_owner->pid;
259 new_page_owner->tgid = old_page_owner->tgid;
260 new_page_owner->free_pid = old_page_owner->free_pid;
261 new_page_owner->free_tgid = old_page_owner->free_tgid;
262 new_page_owner->ts_nsec = old_page_owner->ts_nsec;
263 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
264 strcpy(new_page_owner->comm, old_page_owner->comm);
267 * We don't clear the bit on the old folio as it's going to be freed
268 * after migration. Until then, the info can be useful in case of
269 * a bug, and the overall stats will be off a bit only temporarily.
270 * Also, migrate_misplaced_transhuge_page() can still fail the
271 * migration and then we want the old folio to retain the info. But
272 * in that case we also don't need to explicitly clear the info from
273 * the new page, which will be freed.
275 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
276 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
277 page_ext_put(new_ext);
278 page_ext_put(old_ext);
281 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
282 pg_data_t *pgdat, struct zone *zone)
285 struct page_ext *page_ext;
286 struct page_owner *page_owner;
287 unsigned long pfn, block_end_pfn;
288 unsigned long end_pfn = zone_end_pfn(zone);
289 unsigned long count[MIGRATE_TYPES] = { 0, };
290 int pageblock_mt, page_mt;
293 /* Scan block by block. First and last block may be incomplete */
294 pfn = zone->zone_start_pfn;
297 * Walk the zone in pageblock_nr_pages steps. If a page block spans
298 * a zone boundary, it will be double counted between zones. This does
299 * not matter as the mixed block count will still be correct
301 for (; pfn < end_pfn; ) {
302 page = pfn_to_online_page(pfn);
304 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
308 block_end_pfn = pageblock_end_pfn(pfn);
309 block_end_pfn = min(block_end_pfn, end_pfn);
311 pageblock_mt = get_pageblock_migratetype(page);
313 for (; pfn < block_end_pfn; pfn++) {
314 /* The pageblock is online, no need to recheck. */
315 page = pfn_to_page(pfn);
317 if (page_zone(page) != zone)
320 if (PageBuddy(page)) {
321 unsigned long freepage_order;
323 freepage_order = buddy_order_unsafe(page);
324 if (freepage_order <= MAX_ORDER)
325 pfn += (1UL << freepage_order) - 1;
329 if (PageReserved(page))
332 page_ext = page_ext_get(page);
333 if (unlikely(!page_ext))
336 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
337 goto ext_put_continue;
339 page_owner = get_page_owner(page_ext);
340 page_mt = gfp_migratetype(page_owner->gfp_mask);
341 if (pageblock_mt != page_mt) {
342 if (is_migrate_cma(pageblock_mt))
343 count[MIGRATE_MOVABLE]++;
345 count[pageblock_mt]++;
348 page_ext_put(page_ext);
351 pfn += (1UL << page_owner->order) - 1;
353 page_ext_put(page_ext);
358 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
359 for (i = 0; i < MIGRATE_TYPES; i++)
360 seq_printf(m, "%12lu ", count[i]);
365 * Looking for memcg information and print it out
367 static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret,
371 unsigned long memcg_data;
372 struct mem_cgroup *memcg;
377 memcg_data = READ_ONCE(page->memcg_data);
381 if (memcg_data & MEMCG_DATA_OBJCGS)
382 ret += scnprintf(kbuf + ret, count - ret,
383 "Slab cache page\n");
385 memcg = page_memcg_check(page);
389 online = (memcg->css.flags & CSS_ONLINE);
390 cgroup_name(memcg->css.cgroup, name, sizeof(name));
391 ret += scnprintf(kbuf + ret, count - ret,
392 "Charged %sto %smemcg %s\n",
393 PageMemcgKmem(page) ? "(via objcg) " : "",
394 online ? "" : "offline ",
398 #endif /* CONFIG_MEMCG */
404 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
405 struct page *page, struct page_owner *page_owner,
406 depot_stack_handle_t handle)
408 int ret, pageblock_mt, page_mt;
411 count = min_t(size_t, count, PAGE_SIZE);
412 kbuf = kmalloc(count, GFP_KERNEL);
416 ret = scnprintf(kbuf, count,
417 "Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns\n",
418 page_owner->order, page_owner->gfp_mask,
419 &page_owner->gfp_mask, page_owner->pid,
420 page_owner->tgid, page_owner->comm,
421 page_owner->ts_nsec);
423 /* Print information relevant to grouping pages by mobility */
424 pageblock_mt = get_pageblock_migratetype(page);
425 page_mt = gfp_migratetype(page_owner->gfp_mask);
426 ret += scnprintf(kbuf + ret, count - ret,
427 "PFN 0x%lx type %s Block %lu type %s Flags %pGp\n",
429 migratetype_names[page_mt],
430 pfn >> pageblock_order,
431 migratetype_names[pageblock_mt],
434 ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0);
438 if (page_owner->last_migrate_reason != -1) {
439 ret += scnprintf(kbuf + ret, count - ret,
440 "Page has been migrated, last migrate reason: %s\n",
441 migrate_reason_names[page_owner->last_migrate_reason]);
444 ret = print_page_owner_memcg(kbuf, count, ret, page);
446 ret += snprintf(kbuf + ret, count - ret, "\n");
450 if (copy_to_user(buf, kbuf, ret))
461 void __dump_page_owner(const struct page *page)
463 struct page_ext *page_ext = page_ext_get((void *)page);
464 struct page_owner *page_owner;
465 depot_stack_handle_t handle;
469 if (unlikely(!page_ext)) {
470 pr_alert("There is not page extension available.\n");
474 page_owner = get_page_owner(page_ext);
475 gfp_mask = page_owner->gfp_mask;
476 mt = gfp_migratetype(gfp_mask);
478 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
479 pr_alert("page_owner info is not present (never set?)\n");
480 page_ext_put(page_ext);
484 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
485 pr_alert("page_owner tracks the page as allocated\n");
487 pr_alert("page_owner tracks the page as freed\n");
489 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",
490 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
491 page_owner->pid, page_owner->tgid, page_owner->comm,
492 page_owner->ts_nsec, page_owner->free_ts_nsec);
494 handle = READ_ONCE(page_owner->handle);
496 pr_alert("page_owner allocation stack trace missing\n");
498 stack_depot_print(handle);
500 handle = READ_ONCE(page_owner->free_handle);
502 pr_alert("page_owner free stack trace missing\n");
504 pr_alert("page last free pid %d tgid %d stack trace:\n",
505 page_owner->free_pid, page_owner->free_tgid);
506 stack_depot_print(handle);
509 if (page_owner->last_migrate_reason != -1)
510 pr_alert("page has been migrated, last migrate reason: %s\n",
511 migrate_reason_names[page_owner->last_migrate_reason]);
512 page_ext_put(page_ext);
516 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
520 struct page_ext *page_ext;
521 struct page_owner *page_owner;
522 depot_stack_handle_t handle;
524 if (!static_branch_unlikely(&page_owner_inited))
532 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
533 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
536 /* Find an allocated page */
537 for (; pfn < max_pfn; pfn++) {
539 * This temporary page_owner is required so
540 * that we can avoid the context switches while holding
541 * the rcu lock and copying the page owner information to
542 * user through copy_to_user() or GFP_KERNEL allocations.
544 struct page_owner page_owner_tmp;
547 * If the new page is in a new MAX_ORDER_NR_PAGES area,
548 * validate the area as existing, skip it if not
550 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
551 pfn += MAX_ORDER_NR_PAGES - 1;
555 page = pfn_to_page(pfn);
556 if (PageBuddy(page)) {
557 unsigned long freepage_order = buddy_order_unsafe(page);
559 if (freepage_order <= MAX_ORDER)
560 pfn += (1UL << freepage_order) - 1;
564 page_ext = page_ext_get(page);
565 if (unlikely(!page_ext))
569 * Some pages could be missed by concurrent allocation or free,
570 * because we don't hold the zone lock.
572 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
573 goto ext_put_continue;
576 * Although we do have the info about past allocation of free
577 * pages, it's not relevant for current memory usage.
579 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
580 goto ext_put_continue;
582 page_owner = get_page_owner(page_ext);
585 * Don't print "tail" pages of high-order allocations as that
586 * would inflate the stats.
588 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
589 goto ext_put_continue;
592 * Access to page_ext->handle isn't synchronous so we should
593 * be careful to access it.
595 handle = READ_ONCE(page_owner->handle);
597 goto ext_put_continue;
599 /* Record the next PFN to read in the file offset */
602 page_owner_tmp = *page_owner;
603 page_ext_put(page_ext);
604 return print_page_owner(buf, count, pfn, page,
605 &page_owner_tmp, handle);
607 page_ext_put(page_ext);
613 static loff_t lseek_page_owner(struct file *file, loff_t offset, int orig)
617 file->f_pos = offset;
620 file->f_pos += offset;
628 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
630 unsigned long pfn = zone->zone_start_pfn;
631 unsigned long end_pfn = zone_end_pfn(zone);
632 unsigned long count = 0;
635 * Walk the zone in pageblock_nr_pages steps. If a page block spans
636 * a zone boundary, it will be double counted between zones. This does
637 * not matter as the mixed block count will still be correct
639 for (; pfn < end_pfn; ) {
640 unsigned long block_end_pfn;
642 if (!pfn_valid(pfn)) {
643 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
647 block_end_pfn = pageblock_end_pfn(pfn);
648 block_end_pfn = min(block_end_pfn, end_pfn);
650 for (; pfn < block_end_pfn; pfn++) {
651 struct page *page = pfn_to_page(pfn);
652 struct page_ext *page_ext;
654 if (page_zone(page) != zone)
658 * To avoid having to grab zone->lock, be a little
659 * careful when reading buddy page order. The only
660 * danger is that we skip too much and potentially miss
661 * some early allocated pages, which is better than
662 * heavy lock contention.
664 if (PageBuddy(page)) {
665 unsigned long order = buddy_order_unsafe(page);
667 if (order > 0 && order <= MAX_ORDER)
668 pfn += (1UL << order) - 1;
672 if (PageReserved(page))
675 page_ext = page_ext_get(page);
676 if (unlikely(!page_ext))
679 /* Maybe overlapping zone */
680 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
681 goto ext_put_continue;
683 /* Found early allocated page */
684 __set_page_owner_handle(page_ext, early_handle,
688 page_ext_put(page_ext);
693 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
694 pgdat->node_id, zone->name, count);
697 static void init_zones_in_node(pg_data_t *pgdat)
700 struct zone *node_zones = pgdat->node_zones;
702 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
703 if (!populated_zone(zone))
706 init_pages_in_zone(pgdat, zone);
710 static void init_early_allocated_pages(void)
714 for_each_online_pgdat(pgdat)
715 init_zones_in_node(pgdat);
718 static const struct file_operations proc_page_owner_operations = {
719 .read = read_page_owner,
720 .llseek = lseek_page_owner,
723 static int __init pageowner_init(void)
725 if (!static_branch_unlikely(&page_owner_inited)) {
726 pr_info("page_owner is disabled\n");
730 debugfs_create_file("page_owner", 0400, NULL, NULL,
731 &proc_page_owner_operations);
735 late_initcall(pageowner_init)