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 depot_stack_handle_t free_handle
;
30 static bool page_owner_enabled
= false;
31 DEFINE_STATIC_KEY_FALSE(page_owner_inited
);
33 static depot_stack_handle_t dummy_handle
;
34 static depot_stack_handle_t failure_handle
;
35 static depot_stack_handle_t early_handle
;
37 static void init_early_allocated_pages(void);
39 static int __init
early_page_owner_param(char *buf
)
44 if (strcmp(buf
, "on") == 0)
45 page_owner_enabled
= true;
49 early_param("page_owner", early_page_owner_param
);
51 static bool need_page_owner(void)
53 return page_owner_enabled
;
56 static __always_inline depot_stack_handle_t
create_dummy_stack(void)
58 unsigned long entries
[4];
59 unsigned int nr_entries
;
61 nr_entries
= stack_trace_save(entries
, ARRAY_SIZE(entries
), 0);
62 return stack_depot_save(entries
, nr_entries
, GFP_KERNEL
);
65 static noinline
void register_dummy_stack(void)
67 dummy_handle
= create_dummy_stack();
70 static noinline
void register_failure_stack(void)
72 failure_handle
= create_dummy_stack();
75 static noinline
void register_early_stack(void)
77 early_handle
= create_dummy_stack();
80 static void init_page_owner(void)
82 if (!page_owner_enabled
)
85 register_dummy_stack();
86 register_failure_stack();
87 register_early_stack();
88 static_branch_enable(&page_owner_inited
);
89 init_early_allocated_pages();
92 struct page_ext_operations page_owner_ops
= {
93 .size
= sizeof(struct page_owner
),
94 .need
= need_page_owner
,
95 .init
= init_page_owner
,
98 static inline struct page_owner
*get_page_owner(struct page_ext
*page_ext
)
100 return (void *)page_ext
+ page_owner_ops
.offset
;
103 static inline bool check_recursive_alloc(unsigned long *entries
,
104 unsigned int nr_entries
,
109 for (i
= 0; i
< nr_entries
; i
++) {
110 if (entries
[i
] == ip
)
116 static noinline depot_stack_handle_t
save_stack(gfp_t flags
)
118 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
119 depot_stack_handle_t handle
;
120 unsigned int nr_entries
;
122 nr_entries
= stack_trace_save(entries
, ARRAY_SIZE(entries
), 2);
125 * We need to check recursion here because our request to
126 * stackdepot could trigger memory allocation to save new
127 * entry. New memory allocation would reach here and call
128 * stack_depot_save_entries() again if we don't catch it. There is
129 * still not enough memory in stackdepot so it would try to
130 * allocate memory again and loop forever.
132 if (check_recursive_alloc(entries
, nr_entries
, _RET_IP_
))
135 handle
= stack_depot_save(entries
, nr_entries
, flags
);
137 handle
= failure_handle
;
142 void __reset_page_owner(struct page
*page
, unsigned int order
)
145 struct page_ext
*page_ext
;
146 depot_stack_handle_t handle
= 0;
147 struct page_owner
*page_owner
;
149 handle
= save_stack(GFP_NOWAIT
| __GFP_NOWARN
);
151 page_ext
= lookup_page_ext(page
);
152 if (unlikely(!page_ext
))
154 for (i
= 0; i
< (1 << order
); i
++) {
155 __clear_bit(PAGE_EXT_OWNER_ALLOCATED
, &page_ext
->flags
);
156 page_owner
= get_page_owner(page_ext
);
157 page_owner
->free_handle
= handle
;
158 page_ext
= page_ext_next(page_ext
);
162 static inline void __set_page_owner_handle(struct page
*page
,
163 struct page_ext
*page_ext
, depot_stack_handle_t handle
,
164 unsigned int order
, gfp_t gfp_mask
)
166 struct page_owner
*page_owner
;
169 for (i
= 0; i
< (1 << order
); i
++) {
170 page_owner
= get_page_owner(page_ext
);
171 page_owner
->handle
= handle
;
172 page_owner
->order
= order
;
173 page_owner
->gfp_mask
= gfp_mask
;
174 page_owner
->last_migrate_reason
= -1;
175 __set_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
176 __set_bit(PAGE_EXT_OWNER_ALLOCATED
, &page_ext
->flags
);
178 page_ext
= page_ext_next(page_ext
);
182 noinline
void __set_page_owner(struct page
*page
, unsigned int order
,
185 struct page_ext
*page_ext
= lookup_page_ext(page
);
186 depot_stack_handle_t handle
;
188 if (unlikely(!page_ext
))
191 handle
= save_stack(gfp_mask
);
192 __set_page_owner_handle(page
, page_ext
, handle
, order
, gfp_mask
);
195 void __set_page_owner_migrate_reason(struct page
*page
, int reason
)
197 struct page_ext
*page_ext
= lookup_page_ext(page
);
198 struct page_owner
*page_owner
;
200 if (unlikely(!page_ext
))
203 page_owner
= get_page_owner(page_ext
);
204 page_owner
->last_migrate_reason
= reason
;
207 void __split_page_owner(struct page
*page
, unsigned int order
)
210 struct page_ext
*page_ext
= lookup_page_ext(page
);
211 struct page_owner
*page_owner
;
213 if (unlikely(!page_ext
))
216 for (i
= 0; i
< (1 << order
); i
++) {
217 page_owner
= get_page_owner(page_ext
);
218 page_owner
->order
= 0;
219 page_ext
= page_ext_next(page_ext
);
223 void __copy_page_owner(struct page
*oldpage
, struct page
*newpage
)
225 struct page_ext
*old_ext
= lookup_page_ext(oldpage
);
226 struct page_ext
*new_ext
= lookup_page_ext(newpage
);
227 struct page_owner
*old_page_owner
, *new_page_owner
;
229 if (unlikely(!old_ext
|| !new_ext
))
232 old_page_owner
= get_page_owner(old_ext
);
233 new_page_owner
= get_page_owner(new_ext
);
234 new_page_owner
->order
= old_page_owner
->order
;
235 new_page_owner
->gfp_mask
= old_page_owner
->gfp_mask
;
236 new_page_owner
->last_migrate_reason
=
237 old_page_owner
->last_migrate_reason
;
238 new_page_owner
->handle
= old_page_owner
->handle
;
241 * We don't clear the bit on the oldpage as it's going to be freed
242 * after migration. Until then, the info can be useful in case of
243 * a bug, and the overal stats will be off a bit only temporarily.
244 * Also, migrate_misplaced_transhuge_page() can still fail the
245 * migration and then we want the oldpage to retain the info. But
246 * in that case we also don't need to explicitly clear the info from
247 * the new page, which will be freed.
249 __set_bit(PAGE_EXT_OWNER
, &new_ext
->flags
);
250 __set_bit(PAGE_EXT_OWNER_ALLOCATED
, &new_ext
->flags
);
253 void pagetypeinfo_showmixedcount_print(struct seq_file
*m
,
254 pg_data_t
*pgdat
, struct zone
*zone
)
257 struct page_ext
*page_ext
;
258 struct page_owner
*page_owner
;
259 unsigned long pfn
= zone
->zone_start_pfn
, block_end_pfn
;
260 unsigned long end_pfn
= pfn
+ zone
->spanned_pages
;
261 unsigned long count
[MIGRATE_TYPES
] = { 0, };
262 int pageblock_mt
, page_mt
;
265 /* Scan block by block. First and last block may be incomplete */
266 pfn
= zone
->zone_start_pfn
;
269 * Walk the zone in pageblock_nr_pages steps. If a page block spans
270 * a zone boundary, it will be double counted between zones. This does
271 * not matter as the mixed block count will still be correct
273 for (; pfn
< end_pfn
; ) {
274 page
= pfn_to_online_page(pfn
);
276 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
280 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
281 block_end_pfn
= min(block_end_pfn
, end_pfn
);
283 pageblock_mt
= get_pageblock_migratetype(page
);
285 for (; pfn
< block_end_pfn
; pfn
++) {
286 if (!pfn_valid_within(pfn
))
289 /* The pageblock is online, no need to recheck. */
290 page
= pfn_to_page(pfn
);
292 if (page_zone(page
) != zone
)
295 if (PageBuddy(page
)) {
296 unsigned long freepage_order
;
298 freepage_order
= page_order_unsafe(page
);
299 if (freepage_order
< MAX_ORDER
)
300 pfn
+= (1UL << freepage_order
) - 1;
304 if (PageReserved(page
))
307 page_ext
= lookup_page_ext(page
);
308 if (unlikely(!page_ext
))
311 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED
, &page_ext
->flags
))
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
]++;
320 count
[pageblock_mt
]++;
325 pfn
+= (1UL << page_owner
->order
) - 1;
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
]);
337 print_page_owner(char __user
*buf
, size_t count
, unsigned long pfn
,
338 struct page
*page
, struct page_owner
*page_owner
,
339 depot_stack_handle_t handle
)
341 int ret
, pageblock_mt
, page_mt
;
342 unsigned long *entries
;
343 unsigned int nr_entries
;
346 count
= min_t(size_t, count
, PAGE_SIZE
);
347 kbuf
= kmalloc(count
, GFP_KERNEL
);
351 ret
= snprintf(kbuf
, count
,
352 "Page allocated via order %u, mask %#x(%pGg)\n",
353 page_owner
->order
, page_owner
->gfp_mask
,
354 &page_owner
->gfp_mask
);
359 /* Print information relevant to grouping pages by mobility */
360 pageblock_mt
= get_pageblock_migratetype(page
);
361 page_mt
= gfp_migratetype(page_owner
->gfp_mask
);
362 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
363 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
365 migratetype_names
[page_mt
],
366 pfn
>> pageblock_order
,
367 migratetype_names
[pageblock_mt
],
368 page
->flags
, &page
->flags
);
373 nr_entries
= stack_depot_fetch(handle
, &entries
);
374 ret
+= stack_trace_snprint(kbuf
+ ret
, count
- ret
, entries
, nr_entries
, 0);
378 if (page_owner
->last_migrate_reason
!= -1) {
379 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
380 "Page has been migrated, last migrate reason: %s\n",
381 migrate_reason_names
[page_owner
->last_migrate_reason
]);
386 ret
+= snprintf(kbuf
+ ret
, count
- ret
, "\n");
390 if (copy_to_user(buf
, kbuf
, ret
))
401 void __dump_page_owner(struct page
*page
)
403 struct page_ext
*page_ext
= lookup_page_ext(page
);
404 struct page_owner
*page_owner
;
405 depot_stack_handle_t handle
;
406 unsigned long *entries
;
407 unsigned int nr_entries
;
411 if (unlikely(!page_ext
)) {
412 pr_alert("There is not page extension available.\n");
416 page_owner
= get_page_owner(page_ext
);
417 gfp_mask
= page_owner
->gfp_mask
;
418 mt
= gfp_migratetype(gfp_mask
);
420 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
)) {
421 pr_alert("page_owner info is not present (never set?)\n");
425 if (test_bit(PAGE_EXT_OWNER_ALLOCATED
, &page_ext
->flags
))
426 pr_alert("page_owner tracks the page as allocated\n");
428 pr_alert("page_owner tracks the page as freed\n");
430 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
431 page_owner
->order
, migratetype_names
[mt
], gfp_mask
, &gfp_mask
);
433 handle
= READ_ONCE(page_owner
->handle
);
435 pr_alert("page_owner allocation stack trace missing\n");
437 nr_entries
= stack_depot_fetch(handle
, &entries
);
438 stack_trace_print(entries
, nr_entries
, 0);
441 handle
= READ_ONCE(page_owner
->free_handle
);
443 pr_alert("page_owner free stack trace missing\n");
445 nr_entries
= stack_depot_fetch(handle
, &entries
);
446 pr_alert("page last free stack trace:\n");
447 stack_trace_print(entries
, nr_entries
, 0);
450 if (page_owner
->last_migrate_reason
!= -1)
451 pr_alert("page has been migrated, last migrate reason: %s\n",
452 migrate_reason_names
[page_owner
->last_migrate_reason
]);
456 read_page_owner(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
460 struct page_ext
*page_ext
;
461 struct page_owner
*page_owner
;
462 depot_stack_handle_t handle
;
464 if (!static_branch_unlikely(&page_owner_inited
))
468 pfn
= min_low_pfn
+ *ppos
;
470 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
471 while (!pfn_valid(pfn
) && (pfn
& (MAX_ORDER_NR_PAGES
- 1)) != 0)
474 drain_all_pages(NULL
);
476 /* Find an allocated page */
477 for (; pfn
< max_pfn
; pfn
++) {
479 * If the new page is in a new MAX_ORDER_NR_PAGES area,
480 * validate the area as existing, skip it if not
482 if ((pfn
& (MAX_ORDER_NR_PAGES
- 1)) == 0 && !pfn_valid(pfn
)) {
483 pfn
+= MAX_ORDER_NR_PAGES
- 1;
487 /* Check for holes within a MAX_ORDER area */
488 if (!pfn_valid_within(pfn
))
491 page
= pfn_to_page(pfn
);
492 if (PageBuddy(page
)) {
493 unsigned long freepage_order
= page_order_unsafe(page
);
495 if (freepage_order
< MAX_ORDER
)
496 pfn
+= (1UL << freepage_order
) - 1;
500 page_ext
= lookup_page_ext(page
);
501 if (unlikely(!page_ext
))
505 * Some pages could be missed by concurrent allocation or free,
506 * because we don't hold the zone lock.
508 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
512 * Although we do have the info about past allocation of free
513 * pages, it's not relevant for current memory usage.
515 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED
, &page_ext
->flags
))
518 page_owner
= get_page_owner(page_ext
);
521 * Don't print "tail" pages of high-order allocations as that
522 * would inflate the stats.
524 if (!IS_ALIGNED(pfn
, 1 << page_owner
->order
))
528 * Access to page_ext->handle isn't synchronous so we should
529 * be careful to access it.
531 handle
= READ_ONCE(page_owner
->handle
);
535 /* Record the next PFN to read in the file offset */
536 *ppos
= (pfn
- min_low_pfn
) + 1;
538 return print_page_owner(buf
, count
, pfn
, page
,
545 static void init_pages_in_zone(pg_data_t
*pgdat
, struct zone
*zone
)
547 unsigned long pfn
= zone
->zone_start_pfn
;
548 unsigned long end_pfn
= zone_end_pfn(zone
);
549 unsigned long count
= 0;
552 * Walk the zone in pageblock_nr_pages steps. If a page block spans
553 * a zone boundary, it will be double counted between zones. This does
554 * not matter as the mixed block count will still be correct
556 for (; pfn
< end_pfn
; ) {
557 unsigned long block_end_pfn
;
559 if (!pfn_valid(pfn
)) {
560 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
564 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
565 block_end_pfn
= min(block_end_pfn
, end_pfn
);
567 for (; pfn
< block_end_pfn
; pfn
++) {
569 struct page_ext
*page_ext
;
571 if (!pfn_valid_within(pfn
))
574 page
= pfn_to_page(pfn
);
576 if (page_zone(page
) != zone
)
580 * To avoid having to grab zone->lock, be a little
581 * careful when reading buddy page order. The only
582 * danger is that we skip too much and potentially miss
583 * some early allocated pages, which is better than
584 * heavy lock contention.
586 if (PageBuddy(page
)) {
587 unsigned long order
= page_order_unsafe(page
);
589 if (order
> 0 && order
< MAX_ORDER
)
590 pfn
+= (1UL << order
) - 1;
594 if (PageReserved(page
))
597 page_ext
= lookup_page_ext(page
);
598 if (unlikely(!page_ext
))
601 /* Maybe overlapping zone */
602 if (test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
605 /* Found early allocated page */
606 __set_page_owner_handle(page
, page_ext
, early_handle
,
613 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
614 pgdat
->node_id
, zone
->name
, count
);
617 static void init_zones_in_node(pg_data_t
*pgdat
)
620 struct zone
*node_zones
= pgdat
->node_zones
;
622 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
623 if (!populated_zone(zone
))
626 init_pages_in_zone(pgdat
, zone
);
630 static void init_early_allocated_pages(void)
634 for_each_online_pgdat(pgdat
)
635 init_zones_in_node(pgdat
);
638 static const struct file_operations proc_page_owner_operations
= {
639 .read
= read_page_owner
,
642 static int __init
pageowner_init(void)
644 if (!static_branch_unlikely(&page_owner_inited
)) {
645 pr_info("page_owner is disabled\n");
649 debugfs_create_file("page_owner", 0400, NULL
, NULL
,
650 &proc_page_owner_operations
);
654 late_initcall(pageowner_init
)