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>
17 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
18 * to use off stack temporal storage
20 #define PAGE_OWNER_STACK_DEPTH (16)
24 short last_migrate_reason;
26 depot_stack_handle_t handle;
27 #ifdef CONFIG_DEBUG_PAGEALLOC
28 depot_stack_handle_t free_handle;
32 static bool page_owner_disabled = true;
33 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
35 static depot_stack_handle_t dummy_handle;
36 static depot_stack_handle_t failure_handle;
37 static depot_stack_handle_t early_handle;
39 static void init_early_allocated_pages(void);
41 static int __init early_page_owner_param(char *buf)
46 if (strcmp(buf, "on") == 0)
47 page_owner_disabled = false;
51 early_param("page_owner", early_page_owner_param);
53 static bool need_page_owner(void)
55 if (page_owner_disabled)
61 static __always_inline depot_stack_handle_t create_dummy_stack(void)
63 unsigned long entries[4];
64 unsigned int nr_entries;
66 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
67 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
70 static noinline void register_dummy_stack(void)
72 dummy_handle = create_dummy_stack();
75 static noinline void register_failure_stack(void)
77 failure_handle = create_dummy_stack();
80 static noinline void register_early_stack(void)
82 early_handle = create_dummy_stack();
85 static void init_page_owner(void)
87 if (page_owner_disabled)
90 register_dummy_stack();
91 register_failure_stack();
92 register_early_stack();
93 static_branch_enable(&page_owner_inited);
94 init_early_allocated_pages();
97 struct page_ext_operations page_owner_ops = {
98 .size = sizeof(struct page_owner),
99 .need = need_page_owner,
100 .init = init_page_owner,
103 static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
105 return (void *)page_ext + page_owner_ops.offset;
108 static inline bool check_recursive_alloc(unsigned long *entries,
109 unsigned int nr_entries,
114 for (i = 0; i < nr_entries; i++) {
115 if (entries[i] == ip)
121 static noinline depot_stack_handle_t save_stack(gfp_t flags)
123 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
124 depot_stack_handle_t handle;
125 unsigned int nr_entries;
127 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
130 * We need to check recursion here because our request to
131 * stackdepot could trigger memory allocation to save new
132 * entry. New memory allocation would reach here and call
133 * stack_depot_save_entries() again if we don't catch it. There is
134 * still not enough memory in stackdepot so it would try to
135 * allocate memory again and loop forever.
137 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
140 handle = stack_depot_save(entries, nr_entries, flags);
142 handle = failure_handle;
147 void __reset_page_owner(struct page *page, unsigned int order)
150 struct page_ext *page_ext;
151 #ifdef CONFIG_DEBUG_PAGEALLOC
152 depot_stack_handle_t handle = 0;
153 struct page_owner *page_owner;
155 if (debug_pagealloc_enabled())
156 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
159 for (i = 0; i < (1 << order); i++) {
160 page_ext = lookup_page_ext(page + i);
161 if (unlikely(!page_ext))
163 __clear_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags);
164 #ifdef CONFIG_DEBUG_PAGEALLOC
165 if (debug_pagealloc_enabled()) {
166 page_owner = get_page_owner(page_ext);
167 page_owner->free_handle = handle;
173 static inline void __set_page_owner_handle(struct page *page,
174 struct page_ext *page_ext, depot_stack_handle_t handle,
175 unsigned int order, gfp_t gfp_mask)
177 struct page_owner *page_owner;
180 for (i = 0; i < (1 << order); i++) {
181 page_owner = get_page_owner(page_ext);
182 page_owner->handle = handle;
183 page_owner->order = order;
184 page_owner->gfp_mask = gfp_mask;
185 page_owner->last_migrate_reason = -1;
186 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
187 __set_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags);
189 page_ext = lookup_page_ext(page + i);
193 noinline void __set_page_owner(struct page *page, unsigned int order,
196 struct page_ext *page_ext = lookup_page_ext(page);
197 depot_stack_handle_t handle;
199 if (unlikely(!page_ext))
202 handle = save_stack(gfp_mask);
203 __set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
206 void __set_page_owner_migrate_reason(struct page *page, int reason)
208 struct page_ext *page_ext = lookup_page_ext(page);
209 struct page_owner *page_owner;
211 if (unlikely(!page_ext))
214 page_owner = get_page_owner(page_ext);
215 page_owner->last_migrate_reason = reason;
218 void __split_page_owner(struct page *page, unsigned int order)
221 struct page_ext *page_ext = lookup_page_ext(page);
222 struct page_owner *page_owner;
224 if (unlikely(!page_ext))
227 page_owner = get_page_owner(page_ext);
228 page_owner->order = 0;
229 for (i = 1; i < (1 << order); i++) {
230 page_ext = lookup_page_ext(page + i);
231 page_owner = get_page_owner(page_ext);
232 page_owner->order = 0;
236 void __copy_page_owner(struct page *oldpage, struct page *newpage)
238 struct page_ext *old_ext = lookup_page_ext(oldpage);
239 struct page_ext *new_ext = lookup_page_ext(newpage);
240 struct page_owner *old_page_owner, *new_page_owner;
242 if (unlikely(!old_ext || !new_ext))
245 old_page_owner = get_page_owner(old_ext);
246 new_page_owner = get_page_owner(new_ext);
247 new_page_owner->order = old_page_owner->order;
248 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
249 new_page_owner->last_migrate_reason =
250 old_page_owner->last_migrate_reason;
251 new_page_owner->handle = old_page_owner->handle;
254 * We don't clear the bit on the oldpage as it's going to be freed
255 * after migration. Until then, the info can be useful in case of
256 * a bug, and the overal stats will be off a bit only temporarily.
257 * Also, migrate_misplaced_transhuge_page() can still fail the
258 * migration and then we want the oldpage to retain the info. But
259 * in that case we also don't need to explicitly clear the info from
260 * the new page, which will be freed.
262 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
263 __set_bit(PAGE_EXT_OWNER_ACTIVE, &new_ext->flags);
266 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
267 pg_data_t *pgdat, struct zone *zone)
270 struct page_ext *page_ext;
271 struct page_owner *page_owner;
272 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
273 unsigned long end_pfn = pfn + zone->spanned_pages;
274 unsigned long count[MIGRATE_TYPES] = { 0, };
275 int pageblock_mt, page_mt;
278 /* Scan block by block. First and last block may be incomplete */
279 pfn = zone->zone_start_pfn;
282 * Walk the zone in pageblock_nr_pages steps. If a page block spans
283 * a zone boundary, it will be double counted between zones. This does
284 * not matter as the mixed block count will still be correct
286 for (; pfn < end_pfn; ) {
287 if (!pfn_valid(pfn)) {
288 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
292 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
293 block_end_pfn = min(block_end_pfn, end_pfn);
295 page = pfn_to_page(pfn);
296 pageblock_mt = get_pageblock_migratetype(page);
298 for (; pfn < block_end_pfn; pfn++) {
299 if (!pfn_valid_within(pfn))
302 page = pfn_to_page(pfn);
304 if (page_zone(page) != zone)
307 if (PageBuddy(page)) {
308 unsigned long freepage_order;
310 freepage_order = page_order_unsafe(page);
311 if (freepage_order < MAX_ORDER)
312 pfn += (1UL << freepage_order) - 1;
316 if (PageReserved(page))
319 page_ext = lookup_page_ext(page);
320 if (unlikely(!page_ext))
323 if (!test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
326 page_owner = get_page_owner(page_ext);
327 page_mt = gfpflags_to_migratetype(
328 page_owner->gfp_mask);
329 if (pageblock_mt != page_mt) {
330 if (is_migrate_cma(pageblock_mt))
331 count[MIGRATE_MOVABLE]++;
333 count[pageblock_mt]++;
338 pfn += (1UL << page_owner->order) - 1;
343 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
344 for (i = 0; i < MIGRATE_TYPES; i++)
345 seq_printf(m, "%12lu ", count[i]);
350 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
351 struct page *page, struct page_owner *page_owner,
352 depot_stack_handle_t handle)
354 int ret, pageblock_mt, page_mt;
355 unsigned long *entries;
356 unsigned int nr_entries;
359 count = min_t(size_t, count, PAGE_SIZE);
360 kbuf = kmalloc(count, GFP_KERNEL);
364 ret = snprintf(kbuf, count,
365 "Page allocated via order %u, mask %#x(%pGg)\n",
366 page_owner->order, page_owner->gfp_mask,
367 &page_owner->gfp_mask);
372 /* Print information relevant to grouping pages by mobility */
373 pageblock_mt = get_pageblock_migratetype(page);
374 page_mt = gfpflags_to_migratetype(page_owner->gfp_mask);
375 ret += snprintf(kbuf + ret, count - ret,
376 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
378 migratetype_names[page_mt],
379 pfn >> pageblock_order,
380 migratetype_names[pageblock_mt],
381 page->flags, &page->flags);
386 nr_entries = stack_depot_fetch(handle, &entries);
387 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
391 if (page_owner->last_migrate_reason != -1) {
392 ret += snprintf(kbuf + ret, count - ret,
393 "Page has been migrated, last migrate reason: %s\n",
394 migrate_reason_names[page_owner->last_migrate_reason]);
399 ret += snprintf(kbuf + ret, count - ret, "\n");
403 if (copy_to_user(buf, kbuf, ret))
414 void __dump_page_owner(struct page *page)
416 struct page_ext *page_ext = lookup_page_ext(page);
417 struct page_owner *page_owner;
418 depot_stack_handle_t handle;
419 unsigned long *entries;
420 unsigned int nr_entries;
424 if (unlikely(!page_ext)) {
425 pr_alert("There is not page extension available.\n");
429 page_owner = get_page_owner(page_ext);
430 gfp_mask = page_owner->gfp_mask;
431 mt = gfpflags_to_migratetype(gfp_mask);
433 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
434 pr_alert("page_owner info is not present (never set?)\n");
438 if (test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
439 pr_alert("page_owner tracks the page as allocated\n");
441 pr_alert("page_owner tracks the page as freed\n");
443 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
444 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask);
446 handle = READ_ONCE(page_owner->handle);
448 pr_alert("page_owner allocation stack trace missing\n");
450 nr_entries = stack_depot_fetch(handle, &entries);
451 stack_trace_print(entries, nr_entries, 0);
454 #ifdef CONFIG_DEBUG_PAGEALLOC
455 handle = READ_ONCE(page_owner->free_handle);
457 pr_alert("page_owner free stack trace missing\n");
459 nr_entries = stack_depot_fetch(handle, &entries);
460 pr_alert("page last free stack trace:\n");
461 stack_trace_print(entries, nr_entries, 0);
465 if (page_owner->last_migrate_reason != -1)
466 pr_alert("page has been migrated, last migrate reason: %s\n",
467 migrate_reason_names[page_owner->last_migrate_reason]);
471 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
475 struct page_ext *page_ext;
476 struct page_owner *page_owner;
477 depot_stack_handle_t handle;
479 if (!static_branch_unlikely(&page_owner_inited))
483 pfn = min_low_pfn + *ppos;
485 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
486 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
489 drain_all_pages(NULL);
491 /* Find an allocated page */
492 for (; pfn < max_pfn; pfn++) {
494 * If the new page is in a new MAX_ORDER_NR_PAGES area,
495 * validate the area as existing, skip it if not
497 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
498 pfn += MAX_ORDER_NR_PAGES - 1;
502 /* Check for holes within a MAX_ORDER area */
503 if (!pfn_valid_within(pfn))
506 page = pfn_to_page(pfn);
507 if (PageBuddy(page)) {
508 unsigned long freepage_order = page_order_unsafe(page);
510 if (freepage_order < MAX_ORDER)
511 pfn += (1UL << freepage_order) - 1;
515 page_ext = lookup_page_ext(page);
516 if (unlikely(!page_ext))
520 * Some pages could be missed by concurrent allocation or free,
521 * because we don't hold the zone lock.
523 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
527 * Although we do have the info about past allocation of free
528 * pages, it's not relevant for current memory usage.
530 if (!test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
533 page_owner = get_page_owner(page_ext);
536 * Don't print "tail" pages of high-order allocations as that
537 * would inflate the stats.
539 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
543 * Access to page_ext->handle isn't synchronous so we should
544 * be careful to access it.
546 handle = READ_ONCE(page_owner->handle);
550 /* Record the next PFN to read in the file offset */
551 *ppos = (pfn - min_low_pfn) + 1;
553 return print_page_owner(buf, count, pfn, page,
560 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
562 unsigned long pfn = zone->zone_start_pfn;
563 unsigned long end_pfn = zone_end_pfn(zone);
564 unsigned long count = 0;
567 * Walk the zone in pageblock_nr_pages steps. If a page block spans
568 * a zone boundary, it will be double counted between zones. This does
569 * not matter as the mixed block count will still be correct
571 for (; pfn < end_pfn; ) {
572 unsigned long block_end_pfn;
574 if (!pfn_valid(pfn)) {
575 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
579 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
580 block_end_pfn = min(block_end_pfn, end_pfn);
582 for (; pfn < block_end_pfn; pfn++) {
584 struct page_ext *page_ext;
586 if (!pfn_valid_within(pfn))
589 page = pfn_to_page(pfn);
591 if (page_zone(page) != zone)
595 * To avoid having to grab zone->lock, be a little
596 * careful when reading buddy page order. The only
597 * danger is that we skip too much and potentially miss
598 * some early allocated pages, which is better than
599 * heavy lock contention.
601 if (PageBuddy(page)) {
602 unsigned long order = page_order_unsafe(page);
604 if (order > 0 && order < MAX_ORDER)
605 pfn += (1UL << order) - 1;
609 if (PageReserved(page))
612 page_ext = lookup_page_ext(page);
613 if (unlikely(!page_ext))
616 /* Maybe overlapping zone */
617 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
620 /* Found early allocated page */
621 __set_page_owner_handle(page, page_ext, early_handle,
628 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
629 pgdat->node_id, zone->name, count);
632 static void init_zones_in_node(pg_data_t *pgdat)
635 struct zone *node_zones = pgdat->node_zones;
637 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
638 if (!populated_zone(zone))
641 init_pages_in_zone(pgdat, zone);
645 static void init_early_allocated_pages(void)
649 for_each_online_pgdat(pgdat)
650 init_zones_in_node(pgdat);
653 static const struct file_operations proc_page_owner_operations = {
654 .read = read_page_owner,
657 static int __init pageowner_init(void)
659 if (!static_branch_unlikely(&page_owner_inited)) {
660 pr_info("page_owner is disabled\n");
664 debugfs_create_file("page_owner", 0400, NULL, NULL,
665 &proc_page_owner_operations);
669 late_initcall(pageowner_init)