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/sched/clock.h>
18 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
19 * to use off stack temporal storage
21 #define PAGE_OWNER_STACK_DEPTH (16)
25 short last_migrate_reason;
27 depot_stack_handle_t handle;
28 depot_stack_handle_t free_handle;
33 static bool page_owner_enabled = false;
34 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
36 static depot_stack_handle_t dummy_handle;
37 static depot_stack_handle_t failure_handle;
38 static depot_stack_handle_t early_handle;
40 static void init_early_allocated_pages(void);
42 static int __init early_page_owner_param(char *buf)
47 if (strcmp(buf, "on") == 0)
48 page_owner_enabled = true;
52 early_param("page_owner", early_page_owner_param);
54 static bool need_page_owner(void)
56 return page_owner_enabled;
59 static __always_inline depot_stack_handle_t create_dummy_stack(void)
61 unsigned long entries[4];
62 unsigned int nr_entries;
64 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
65 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
68 static noinline void register_dummy_stack(void)
70 dummy_handle = create_dummy_stack();
73 static noinline void register_failure_stack(void)
75 failure_handle = create_dummy_stack();
78 static noinline void register_early_stack(void)
80 early_handle = create_dummy_stack();
83 static void init_page_owner(void)
85 if (!page_owner_enabled)
88 register_dummy_stack();
89 register_failure_stack();
90 register_early_stack();
91 static_branch_enable(&page_owner_inited);
92 init_early_allocated_pages();
95 struct page_ext_operations page_owner_ops = {
96 .size = sizeof(struct page_owner),
97 .need = need_page_owner,
98 .init = init_page_owner,
101 static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
103 return (void *)page_ext + page_owner_ops.offset;
106 static inline bool check_recursive_alloc(unsigned long *entries,
107 unsigned int nr_entries,
112 for (i = 0; i < nr_entries; i++) {
113 if (entries[i] == ip)
119 static noinline depot_stack_handle_t save_stack(gfp_t flags)
121 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
122 depot_stack_handle_t handle;
123 unsigned int nr_entries;
125 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
128 * We need to check recursion here because our request to
129 * stackdepot could trigger memory allocation to save new
130 * entry. New memory allocation would reach here and call
131 * stack_depot_save_entries() again if we don't catch it. There is
132 * still not enough memory in stackdepot so it would try to
133 * allocate memory again and loop forever.
135 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
138 handle = stack_depot_save(entries, nr_entries, flags);
140 handle = failure_handle;
145 void __reset_page_owner(struct page *page, unsigned int order)
148 struct page_ext *page_ext;
149 depot_stack_handle_t handle = 0;
150 struct page_owner *page_owner;
152 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
154 page_ext = lookup_page_ext(page);
155 if (unlikely(!page_ext))
157 for (i = 0; i < (1 << order); i++) {
158 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
159 page_owner = get_page_owner(page_ext);
160 page_owner->free_handle = handle;
161 page_ext = page_ext_next(page_ext);
165 static inline void __set_page_owner_handle(struct page *page,
166 struct page_ext *page_ext, depot_stack_handle_t handle,
167 unsigned int order, gfp_t gfp_mask)
169 struct page_owner *page_owner;
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->ts_nsec = local_clock();
180 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
181 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
183 page_ext = page_ext_next(page_ext);
187 noinline void __set_page_owner(struct page *page, unsigned int order,
190 struct page_ext *page_ext = lookup_page_ext(page);
191 depot_stack_handle_t handle;
193 if (unlikely(!page_ext))
196 handle = save_stack(gfp_mask);
197 __set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
200 void __set_page_owner_migrate_reason(struct page *page, int reason)
202 struct page_ext *page_ext = lookup_page_ext(page);
203 struct page_owner *page_owner;
205 if (unlikely(!page_ext))
208 page_owner = get_page_owner(page_ext);
209 page_owner->last_migrate_reason = reason;
212 void __split_page_owner(struct page *page, unsigned int nr)
215 struct page_ext *page_ext = lookup_page_ext(page);
216 struct page_owner *page_owner;
218 if (unlikely(!page_ext))
221 for (i = 0; i < nr; i++) {
222 page_owner = get_page_owner(page_ext);
223 page_owner->order = 0;
224 page_ext = page_ext_next(page_ext);
228 void __copy_page_owner(struct page *oldpage, struct page *newpage)
230 struct page_ext *old_ext = lookup_page_ext(oldpage);
231 struct page_ext *new_ext = lookup_page_ext(newpage);
232 struct page_owner *old_page_owner, *new_page_owner;
234 if (unlikely(!old_ext || !new_ext))
237 old_page_owner = get_page_owner(old_ext);
238 new_page_owner = get_page_owner(new_ext);
239 new_page_owner->order = old_page_owner->order;
240 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
241 new_page_owner->last_migrate_reason =
242 old_page_owner->last_migrate_reason;
243 new_page_owner->handle = old_page_owner->handle;
244 new_page_owner->pid = old_page_owner->pid;
245 new_page_owner->ts_nsec = old_page_owner->ts_nsec;
248 * We don't clear the bit on the oldpage as it's going to be freed
249 * after migration. Until then, the info can be useful in case of
250 * a bug, and the overal stats will be off a bit only temporarily.
251 * Also, migrate_misplaced_transhuge_page() can still fail the
252 * migration and then we want the oldpage to retain the info. But
253 * in that case we also don't need to explicitly clear the info from
254 * the new page, which will be freed.
256 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
257 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
260 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
261 pg_data_t *pgdat, struct zone *zone)
264 struct page_ext *page_ext;
265 struct page_owner *page_owner;
266 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
267 unsigned long end_pfn = pfn + zone->spanned_pages;
268 unsigned long count[MIGRATE_TYPES] = { 0, };
269 int pageblock_mt, page_mt;
272 /* Scan block by block. First and last block may be incomplete */
273 pfn = zone->zone_start_pfn;
276 * Walk the zone in pageblock_nr_pages steps. If a page block spans
277 * a zone boundary, it will be double counted between zones. This does
278 * not matter as the mixed block count will still be correct
280 for (; pfn < end_pfn; ) {
281 page = pfn_to_online_page(pfn);
283 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
287 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
288 block_end_pfn = min(block_end_pfn, end_pfn);
290 pageblock_mt = get_pageblock_migratetype(page);
292 for (; pfn < block_end_pfn; pfn++) {
293 if (!pfn_valid_within(pfn))
296 /* The pageblock is online, no need to recheck. */
297 page = pfn_to_page(pfn);
299 if (page_zone(page) != zone)
302 if (PageBuddy(page)) {
303 unsigned long freepage_order;
305 freepage_order = buddy_order_unsafe(page);
306 if (freepage_order < MAX_ORDER)
307 pfn += (1UL << freepage_order) - 1;
311 if (PageReserved(page))
314 page_ext = lookup_page_ext(page);
315 if (unlikely(!page_ext))
318 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
321 page_owner = get_page_owner(page_ext);
322 page_mt = gfp_migratetype(page_owner->gfp_mask);
323 if (pageblock_mt != page_mt) {
324 if (is_migrate_cma(pageblock_mt))
325 count[MIGRATE_MOVABLE]++;
327 count[pageblock_mt]++;
332 pfn += (1UL << page_owner->order) - 1;
337 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
338 for (i = 0; i < MIGRATE_TYPES; i++)
339 seq_printf(m, "%12lu ", count[i]);
344 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
345 struct page *page, struct page_owner *page_owner,
346 depot_stack_handle_t handle)
348 int ret, pageblock_mt, page_mt;
349 unsigned long *entries;
350 unsigned int nr_entries;
353 count = min_t(size_t, count, PAGE_SIZE);
354 kbuf = kmalloc(count, GFP_KERNEL);
358 ret = snprintf(kbuf, count,
359 "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns\n",
360 page_owner->order, page_owner->gfp_mask,
361 &page_owner->gfp_mask, page_owner->pid,
362 page_owner->ts_nsec);
367 /* Print information relevant to grouping pages by mobility */
368 pageblock_mt = get_pageblock_migratetype(page);
369 page_mt = gfp_migratetype(page_owner->gfp_mask);
370 ret += snprintf(kbuf + ret, count - ret,
371 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
373 migratetype_names[page_mt],
374 pfn >> pageblock_order,
375 migratetype_names[pageblock_mt],
376 page->flags, &page->flags);
381 nr_entries = stack_depot_fetch(handle, &entries);
382 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
386 if (page_owner->last_migrate_reason != -1) {
387 ret += snprintf(kbuf + ret, count - ret,
388 "Page has been migrated, last migrate reason: %s\n",
389 migrate_reason_names[page_owner->last_migrate_reason]);
394 ret += snprintf(kbuf + ret, count - ret, "\n");
398 if (copy_to_user(buf, kbuf, ret))
409 void __dump_page_owner(struct page *page)
411 struct page_ext *page_ext = lookup_page_ext(page);
412 struct page_owner *page_owner;
413 depot_stack_handle_t handle;
414 unsigned long *entries;
415 unsigned int nr_entries;
419 if (unlikely(!page_ext)) {
420 pr_alert("There is not page extension available.\n");
424 page_owner = get_page_owner(page_ext);
425 gfp_mask = page_owner->gfp_mask;
426 mt = gfp_migratetype(gfp_mask);
428 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
429 pr_alert("page_owner info is not present (never set?)\n");
433 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
434 pr_alert("page_owner tracks the page as allocated\n");
436 pr_alert("page_owner tracks the page as freed\n");
438 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu\n",
439 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
440 page_owner->pid, page_owner->ts_nsec);
442 handle = READ_ONCE(page_owner->handle);
444 pr_alert("page_owner allocation stack trace missing\n");
446 nr_entries = stack_depot_fetch(handle, &entries);
447 stack_trace_print(entries, nr_entries, 0);
450 handle = READ_ONCE(page_owner->free_handle);
452 pr_alert("page_owner free stack trace missing\n");
454 nr_entries = stack_depot_fetch(handle, &entries);
455 pr_alert("page last free stack trace:\n");
456 stack_trace_print(entries, nr_entries, 0);
459 if (page_owner->last_migrate_reason != -1)
460 pr_alert("page has been migrated, last migrate reason: %s\n",
461 migrate_reason_names[page_owner->last_migrate_reason]);
465 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
469 struct page_ext *page_ext;
470 struct page_owner *page_owner;
471 depot_stack_handle_t handle;
473 if (!static_branch_unlikely(&page_owner_inited))
477 pfn = min_low_pfn + *ppos;
479 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
480 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
483 drain_all_pages(NULL);
485 /* Find an allocated page */
486 for (; pfn < max_pfn; pfn++) {
488 * If the new page is in a new MAX_ORDER_NR_PAGES area,
489 * validate the area as existing, skip it if not
491 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
492 pfn += MAX_ORDER_NR_PAGES - 1;
496 /* Check for holes within a MAX_ORDER area */
497 if (!pfn_valid_within(pfn))
500 page = pfn_to_page(pfn);
501 if (PageBuddy(page)) {
502 unsigned long freepage_order = buddy_order_unsafe(page);
504 if (freepage_order < MAX_ORDER)
505 pfn += (1UL << freepage_order) - 1;
509 page_ext = lookup_page_ext(page);
510 if (unlikely(!page_ext))
514 * Some pages could be missed by concurrent allocation or free,
515 * because we don't hold the zone lock.
517 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
521 * Although we do have the info about past allocation of free
522 * pages, it's not relevant for current memory usage.
524 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
527 page_owner = get_page_owner(page_ext);
530 * Don't print "tail" pages of high-order allocations as that
531 * would inflate the stats.
533 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
537 * Access to page_ext->handle isn't synchronous so we should
538 * be careful to access it.
540 handle = READ_ONCE(page_owner->handle);
544 /* Record the next PFN to read in the file offset */
545 *ppos = (pfn - min_low_pfn) + 1;
547 return print_page_owner(buf, count, pfn, page,
554 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
556 unsigned long pfn = zone->zone_start_pfn;
557 unsigned long end_pfn = zone_end_pfn(zone);
558 unsigned long count = 0;
561 * Walk the zone in pageblock_nr_pages steps. If a page block spans
562 * a zone boundary, it will be double counted between zones. This does
563 * not matter as the mixed block count will still be correct
565 for (; pfn < end_pfn; ) {
566 unsigned long block_end_pfn;
568 if (!pfn_valid(pfn)) {
569 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
573 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
574 block_end_pfn = min(block_end_pfn, end_pfn);
576 for (; pfn < block_end_pfn; pfn++) {
578 struct page_ext *page_ext;
580 if (!pfn_valid_within(pfn))
583 page = pfn_to_page(pfn);
585 if (page_zone(page) != zone)
589 * To avoid having to grab zone->lock, be a little
590 * careful when reading buddy page order. The only
591 * danger is that we skip too much and potentially miss
592 * some early allocated pages, which is better than
593 * heavy lock contention.
595 if (PageBuddy(page)) {
596 unsigned long order = buddy_order_unsafe(page);
598 if (order > 0 && order < MAX_ORDER)
599 pfn += (1UL << order) - 1;
603 if (PageReserved(page))
606 page_ext = lookup_page_ext(page);
607 if (unlikely(!page_ext))
610 /* Maybe overlapping zone */
611 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
614 /* Found early allocated page */
615 __set_page_owner_handle(page, page_ext, early_handle,
622 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
623 pgdat->node_id, zone->name, count);
626 static void init_zones_in_node(pg_data_t *pgdat)
629 struct zone *node_zones = pgdat->node_zones;
631 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
632 if (!populated_zone(zone))
635 init_pages_in_zone(pgdat, zone);
639 static void init_early_allocated_pages(void)
643 for_each_online_pgdat(pgdat)
644 init_zones_in_node(pgdat);
647 static const struct file_operations proc_page_owner_operations = {
648 .read = read_page_owner,
651 static int __init pageowner_init(void)
653 if (!static_branch_unlikely(&page_owner_inited)) {
654 pr_info("page_owner is disabled\n");
658 debugfs_create_file("page_owner", 0400, NULL, NULL,
659 &proc_page_owner_operations);
663 late_initcall(pageowner_init)