1 #include <linux/debugfs.h>
3 #include <linux/slab.h>
4 #include <linux/uaccess.h>
5 #include <linux/bootmem.h>
6 #include <linux/stacktrace.h>
7 #include <linux/page_owner.h>
8 #include <linux/jump_label.h>
9 #include <linux/migrate.h>
10 #include <linux/stackdepot.h>
11 #include <linux/seq_file.h>
16 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
17 * to use off stack temporal storage
19 #define PAGE_OWNER_STACK_DEPTH (16)
21 static bool page_owner_disabled
= true;
22 DEFINE_STATIC_KEY_FALSE(page_owner_inited
);
24 static depot_stack_handle_t dummy_handle
;
25 static depot_stack_handle_t failure_handle
;
27 static void init_early_allocated_pages(void);
29 static int early_page_owner_param(char *buf
)
34 if (strcmp(buf
, "on") == 0)
35 page_owner_disabled
= false;
39 early_param("page_owner", early_page_owner_param
);
41 static bool need_page_owner(void)
43 if (page_owner_disabled
)
49 static noinline
void register_dummy_stack(void)
51 unsigned long entries
[4];
52 struct stack_trace dummy
;
55 dummy
.max_entries
= ARRAY_SIZE(entries
);
56 dummy
.entries
= &entries
[0];
59 save_stack_trace(&dummy
);
60 dummy_handle
= depot_save_stack(&dummy
, GFP_KERNEL
);
63 static noinline
void register_failure_stack(void)
65 unsigned long entries
[4];
66 struct stack_trace failure
;
68 failure
.nr_entries
= 0;
69 failure
.max_entries
= ARRAY_SIZE(entries
);
70 failure
.entries
= &entries
[0];
73 save_stack_trace(&failure
);
74 failure_handle
= depot_save_stack(&failure
, GFP_KERNEL
);
77 static void init_page_owner(void)
79 if (page_owner_disabled
)
82 register_dummy_stack();
83 register_failure_stack();
84 static_branch_enable(&page_owner_inited
);
85 init_early_allocated_pages();
88 struct page_ext_operations page_owner_ops
= {
89 .need
= need_page_owner
,
90 .init
= init_page_owner
,
93 void __reset_page_owner(struct page
*page
, unsigned int order
)
96 struct page_ext
*page_ext
;
98 for (i
= 0; i
< (1 << order
); i
++) {
99 page_ext
= lookup_page_ext(page
+ i
);
100 if (unlikely(!page_ext
))
102 __clear_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
106 static inline bool check_recursive_alloc(struct stack_trace
*trace
,
111 if (!trace
->nr_entries
)
114 for (i
= 0, count
= 0; i
< trace
->nr_entries
; i
++) {
115 if (trace
->entries
[i
] == ip
&& ++count
== 2)
122 static noinline depot_stack_handle_t
save_stack(gfp_t flags
)
124 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
125 struct stack_trace trace
= {
128 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
131 depot_stack_handle_t handle
;
133 save_stack_trace(&trace
);
134 if (trace
.nr_entries
!= 0 &&
135 trace
.entries
[trace
.nr_entries
-1] == ULONG_MAX
)
139 * We need to check recursion here because our request to stackdepot
140 * could trigger memory allocation to save new entry. New memory
141 * allocation would reach here and call depot_save_stack() again
142 * if we don't catch it. There is still not enough memory in stackdepot
143 * so it would try to allocate memory again and loop forever.
145 if (check_recursive_alloc(&trace
, _RET_IP_
))
148 handle
= depot_save_stack(&trace
, flags
);
150 handle
= failure_handle
;
155 noinline
void __set_page_owner(struct page
*page
, unsigned int order
,
158 struct page_ext
*page_ext
= lookup_page_ext(page
);
160 if (unlikely(!page_ext
))
163 page_ext
->handle
= save_stack(gfp_mask
);
164 page_ext
->order
= order
;
165 page_ext
->gfp_mask
= gfp_mask
;
166 page_ext
->last_migrate_reason
= -1;
168 __set_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
171 void __set_page_owner_migrate_reason(struct page
*page
, int reason
)
173 struct page_ext
*page_ext
= lookup_page_ext(page
);
174 if (unlikely(!page_ext
))
177 page_ext
->last_migrate_reason
= reason
;
180 void __split_page_owner(struct page
*page
, unsigned int order
)
183 struct page_ext
*page_ext
= lookup_page_ext(page
);
185 if (unlikely(!page_ext
))
189 for (i
= 1; i
< (1 << order
); i
++)
190 __copy_page_owner(page
, page
+ i
);
193 void __copy_page_owner(struct page
*oldpage
, struct page
*newpage
)
195 struct page_ext
*old_ext
= lookup_page_ext(oldpage
);
196 struct page_ext
*new_ext
= lookup_page_ext(newpage
);
198 if (unlikely(!old_ext
|| !new_ext
))
201 new_ext
->order
= old_ext
->order
;
202 new_ext
->gfp_mask
= old_ext
->gfp_mask
;
203 new_ext
->last_migrate_reason
= old_ext
->last_migrate_reason
;
204 new_ext
->handle
= old_ext
->handle
;
207 * We don't clear the bit on the oldpage as it's going to be freed
208 * after migration. Until then, the info can be useful in case of
209 * a bug, and the overal stats will be off a bit only temporarily.
210 * Also, migrate_misplaced_transhuge_page() can still fail the
211 * migration and then we want the oldpage to retain the info. But
212 * in that case we also don't need to explicitly clear the info from
213 * the new page, which will be freed.
215 __set_bit(PAGE_EXT_OWNER
, &new_ext
->flags
);
218 void pagetypeinfo_showmixedcount_print(struct seq_file
*m
,
219 pg_data_t
*pgdat
, struct zone
*zone
)
222 struct page_ext
*page_ext
;
223 unsigned long pfn
= zone
->zone_start_pfn
, block_end_pfn
;
224 unsigned long end_pfn
= pfn
+ zone
->spanned_pages
;
225 unsigned long count
[MIGRATE_TYPES
] = { 0, };
226 int pageblock_mt
, page_mt
;
229 /* Scan block by block. First and last block may be incomplete */
230 pfn
= zone
->zone_start_pfn
;
233 * Walk the zone in pageblock_nr_pages steps. If a page block spans
234 * a zone boundary, it will be double counted between zones. This does
235 * not matter as the mixed block count will still be correct
237 for (; pfn
< end_pfn
; ) {
238 if (!pfn_valid(pfn
)) {
239 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
243 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
244 block_end_pfn
= min(block_end_pfn
, end_pfn
);
246 page
= pfn_to_page(pfn
);
247 pageblock_mt
= get_pageblock_migratetype(page
);
249 for (; pfn
< block_end_pfn
; pfn
++) {
250 if (!pfn_valid_within(pfn
))
253 page
= pfn_to_page(pfn
);
255 if (page_zone(page
) != zone
)
258 if (PageBuddy(page
)) {
259 pfn
+= (1UL << page_order(page
)) - 1;
263 if (PageReserved(page
))
266 page_ext
= lookup_page_ext(page
);
267 if (unlikely(!page_ext
))
270 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
273 page_mt
= gfpflags_to_migratetype(page_ext
->gfp_mask
);
274 if (pageblock_mt
!= page_mt
) {
275 if (is_migrate_cma(pageblock_mt
))
276 count
[MIGRATE_MOVABLE
]++;
278 count
[pageblock_mt
]++;
283 pfn
+= (1UL << page_ext
->order
) - 1;
288 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
289 for (i
= 0; i
< MIGRATE_TYPES
; i
++)
290 seq_printf(m
, "%12lu ", count
[i
]);
295 print_page_owner(char __user
*buf
, size_t count
, unsigned long pfn
,
296 struct page
*page
, struct page_ext
*page_ext
,
297 depot_stack_handle_t handle
)
300 int pageblock_mt
, page_mt
;
302 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
303 struct stack_trace trace
= {
306 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
310 kbuf
= kmalloc(count
, GFP_KERNEL
);
314 ret
= snprintf(kbuf
, count
,
315 "Page allocated via order %u, mask %#x(%pGg)\n",
316 page_ext
->order
, page_ext
->gfp_mask
,
317 &page_ext
->gfp_mask
);
322 /* Print information relevant to grouping pages by mobility */
323 pageblock_mt
= get_pageblock_migratetype(page
);
324 page_mt
= gfpflags_to_migratetype(page_ext
->gfp_mask
);
325 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
326 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
328 migratetype_names
[page_mt
],
329 pfn
>> pageblock_order
,
330 migratetype_names
[pageblock_mt
],
331 page
->flags
, &page
->flags
);
336 depot_fetch_stack(handle
, &trace
);
337 ret
+= snprint_stack_trace(kbuf
+ ret
, count
- ret
, &trace
, 0);
341 if (page_ext
->last_migrate_reason
!= -1) {
342 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
343 "Page has been migrated, last migrate reason: %s\n",
344 migrate_reason_names
[page_ext
->last_migrate_reason
]);
349 ret
+= snprintf(kbuf
+ ret
, count
- ret
, "\n");
353 if (copy_to_user(buf
, kbuf
, ret
))
364 void __dump_page_owner(struct page
*page
)
366 struct page_ext
*page_ext
= lookup_page_ext(page
);
367 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
368 struct stack_trace trace
= {
371 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
374 depot_stack_handle_t handle
;
378 if (unlikely(!page_ext
)) {
379 pr_alert("There is not page extension available.\n");
382 gfp_mask
= page_ext
->gfp_mask
;
383 mt
= gfpflags_to_migratetype(gfp_mask
);
385 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
)) {
386 pr_alert("page_owner info is not active (free page?)\n");
390 handle
= READ_ONCE(page_ext
->handle
);
392 pr_alert("page_owner info is not active (free page?)\n");
396 depot_fetch_stack(handle
, &trace
);
397 pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
398 page_ext
->order
, migratetype_names
[mt
], gfp_mask
, &gfp_mask
);
399 print_stack_trace(&trace
, 0);
401 if (page_ext
->last_migrate_reason
!= -1)
402 pr_alert("page has been migrated, last migrate reason: %s\n",
403 migrate_reason_names
[page_ext
->last_migrate_reason
]);
407 read_page_owner(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
411 struct page_ext
*page_ext
;
412 depot_stack_handle_t handle
;
414 if (!static_branch_unlikely(&page_owner_inited
))
418 pfn
= min_low_pfn
+ *ppos
;
420 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
421 while (!pfn_valid(pfn
) && (pfn
& (MAX_ORDER_NR_PAGES
- 1)) != 0)
424 drain_all_pages(NULL
);
426 /* Find an allocated page */
427 for (; pfn
< max_pfn
; pfn
++) {
429 * If the new page is in a new MAX_ORDER_NR_PAGES area,
430 * validate the area as existing, skip it if not
432 if ((pfn
& (MAX_ORDER_NR_PAGES
- 1)) == 0 && !pfn_valid(pfn
)) {
433 pfn
+= MAX_ORDER_NR_PAGES
- 1;
437 /* Check for holes within a MAX_ORDER area */
438 if (!pfn_valid_within(pfn
))
441 page
= pfn_to_page(pfn
);
442 if (PageBuddy(page
)) {
443 unsigned long freepage_order
= page_order_unsafe(page
);
445 if (freepage_order
< MAX_ORDER
)
446 pfn
+= (1UL << freepage_order
) - 1;
450 page_ext
= lookup_page_ext(page
);
451 if (unlikely(!page_ext
))
455 * Some pages could be missed by concurrent allocation or free,
456 * because we don't hold the zone lock.
458 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
462 * Access to page_ext->handle isn't synchronous so we should
463 * be careful to access it.
465 handle
= READ_ONCE(page_ext
->handle
);
469 /* Record the next PFN to read in the file offset */
470 *ppos
= (pfn
- min_low_pfn
) + 1;
472 return print_page_owner(buf
, count
, pfn
, page
,
479 static void init_pages_in_zone(pg_data_t
*pgdat
, struct zone
*zone
)
482 struct page_ext
*page_ext
;
483 unsigned long pfn
= zone
->zone_start_pfn
, block_end_pfn
;
484 unsigned long end_pfn
= pfn
+ zone
->spanned_pages
;
485 unsigned long count
= 0;
487 /* Scan block by block. First and last block may be incomplete */
488 pfn
= zone
->zone_start_pfn
;
491 * Walk the zone in pageblock_nr_pages steps. If a page block spans
492 * a zone boundary, it will be double counted between zones. This does
493 * not matter as the mixed block count will still be correct
495 for (; pfn
< end_pfn
; ) {
496 if (!pfn_valid(pfn
)) {
497 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
501 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
502 block_end_pfn
= min(block_end_pfn
, end_pfn
);
504 page
= pfn_to_page(pfn
);
506 for (; pfn
< block_end_pfn
; pfn
++) {
507 if (!pfn_valid_within(pfn
))
510 page
= pfn_to_page(pfn
);
512 if (page_zone(page
) != zone
)
516 * We are safe to check buddy flag and order, because
517 * this is init stage and only single thread runs.
519 if (PageBuddy(page
)) {
520 pfn
+= (1UL << page_order(page
)) - 1;
524 if (PageReserved(page
))
527 page_ext
= lookup_page_ext(page
);
528 if (unlikely(!page_ext
))
531 /* Maybe overraping zone */
532 if (test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
535 /* Found early allocated page */
536 set_page_owner(page
, 0, 0);
541 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
542 pgdat
->node_id
, zone
->name
, count
);
545 static void init_zones_in_node(pg_data_t
*pgdat
)
548 struct zone
*node_zones
= pgdat
->node_zones
;
551 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
552 if (!populated_zone(zone
))
555 spin_lock_irqsave(&zone
->lock
, flags
);
556 init_pages_in_zone(pgdat
, zone
);
557 spin_unlock_irqrestore(&zone
->lock
, flags
);
561 static void init_early_allocated_pages(void)
565 drain_all_pages(NULL
);
566 for_each_online_pgdat(pgdat
)
567 init_zones_in_node(pgdat
);
570 static const struct file_operations proc_page_owner_operations
= {
571 .read
= read_page_owner
,
574 static int __init
pageowner_init(void)
576 struct dentry
*dentry
;
578 if (!static_branch_unlikely(&page_owner_inited
)) {
579 pr_info("page_owner is disabled\n");
583 dentry
= debugfs_create_file("page_owner", S_IRUSR
, NULL
,
584 NULL
, &proc_page_owner_operations
);
586 return PTR_ERR(dentry
);
590 late_initcall(pageowner_init
)