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>
15 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
16 * to use off stack temporal storage
18 #define PAGE_OWNER_STACK_DEPTH (16)
20 static bool page_owner_disabled
= true;
21 DEFINE_STATIC_KEY_FALSE(page_owner_inited
);
23 static depot_stack_handle_t dummy_handle
;
24 static depot_stack_handle_t failure_handle
;
26 static void init_early_allocated_pages(void);
28 static int early_page_owner_param(char *buf
)
33 if (strcmp(buf
, "on") == 0)
34 page_owner_disabled
= false;
38 early_param("page_owner", early_page_owner_param
);
40 static bool need_page_owner(void)
42 if (page_owner_disabled
)
48 static noinline
void register_dummy_stack(void)
50 unsigned long entries
[4];
51 struct stack_trace dummy
;
54 dummy
.max_entries
= ARRAY_SIZE(entries
);
55 dummy
.entries
= &entries
[0];
58 save_stack_trace(&dummy
);
59 dummy_handle
= depot_save_stack(&dummy
, GFP_KERNEL
);
62 static noinline
void register_failure_stack(void)
64 unsigned long entries
[4];
65 struct stack_trace failure
;
67 failure
.nr_entries
= 0;
68 failure
.max_entries
= ARRAY_SIZE(entries
);
69 failure
.entries
= &entries
[0];
72 save_stack_trace(&failure
);
73 failure_handle
= depot_save_stack(&failure
, GFP_KERNEL
);
76 static void init_page_owner(void)
78 if (page_owner_disabled
)
81 register_dummy_stack();
82 register_failure_stack();
83 static_branch_enable(&page_owner_inited
);
84 init_early_allocated_pages();
87 struct page_ext_operations page_owner_ops
= {
88 .need
= need_page_owner
,
89 .init
= init_page_owner
,
92 void __reset_page_owner(struct page
*page
, unsigned int order
)
95 struct page_ext
*page_ext
;
97 for (i
= 0; i
< (1 << order
); i
++) {
98 page_ext
= lookup_page_ext(page
+ i
);
99 if (unlikely(!page_ext
))
101 __clear_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
105 static inline bool check_recursive_alloc(struct stack_trace
*trace
,
110 if (!trace
->nr_entries
)
113 for (i
= 0, count
= 0; i
< trace
->nr_entries
; i
++) {
114 if (trace
->entries
[i
] == ip
&& ++count
== 2)
121 static noinline depot_stack_handle_t
save_stack(gfp_t flags
)
123 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
124 struct stack_trace trace
= {
127 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
130 depot_stack_handle_t handle
;
132 save_stack_trace(&trace
);
133 if (trace
.nr_entries
!= 0 &&
134 trace
.entries
[trace
.nr_entries
-1] == ULONG_MAX
)
138 * We need to check recursion here because our request to stackdepot
139 * could trigger memory allocation to save new entry. New memory
140 * allocation would reach here and call depot_save_stack() again
141 * if we don't catch it. There is still not enough memory in stackdepot
142 * so it would try to allocate memory again and loop forever.
144 if (check_recursive_alloc(&trace
, _RET_IP_
))
147 handle
= depot_save_stack(&trace
, flags
);
149 handle
= failure_handle
;
154 noinline
void __set_page_owner(struct page
*page
, unsigned int order
,
157 struct page_ext
*page_ext
= lookup_page_ext(page
);
159 if (unlikely(!page_ext
))
162 page_ext
->handle
= save_stack(gfp_mask
);
163 page_ext
->order
= order
;
164 page_ext
->gfp_mask
= gfp_mask
;
165 page_ext
->last_migrate_reason
= -1;
167 __set_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
170 void __set_page_owner_migrate_reason(struct page
*page
, int reason
)
172 struct page_ext
*page_ext
= lookup_page_ext(page
);
173 if (unlikely(!page_ext
))
176 page_ext
->last_migrate_reason
= reason
;
179 void __split_page_owner(struct page
*page
, unsigned int order
)
182 struct page_ext
*page_ext
= lookup_page_ext(page
);
184 if (unlikely(!page_ext
))
188 for (i
= 1; i
< (1 << order
); i
++)
189 __copy_page_owner(page
, page
+ i
);
192 void __copy_page_owner(struct page
*oldpage
, struct page
*newpage
)
194 struct page_ext
*old_ext
= lookup_page_ext(oldpage
);
195 struct page_ext
*new_ext
= lookup_page_ext(newpage
);
197 if (unlikely(!old_ext
|| !new_ext
))
200 new_ext
->order
= old_ext
->order
;
201 new_ext
->gfp_mask
= old_ext
->gfp_mask
;
202 new_ext
->last_migrate_reason
= old_ext
->last_migrate_reason
;
203 new_ext
->handle
= old_ext
->handle
;
206 * We don't clear the bit on the oldpage as it's going to be freed
207 * after migration. Until then, the info can be useful in case of
208 * a bug, and the overal stats will be off a bit only temporarily.
209 * Also, migrate_misplaced_transhuge_page() can still fail the
210 * migration and then we want the oldpage to retain the info. But
211 * in that case we also don't need to explicitly clear the info from
212 * the new page, which will be freed.
214 __set_bit(PAGE_EXT_OWNER
, &new_ext
->flags
);
218 print_page_owner(char __user
*buf
, size_t count
, unsigned long pfn
,
219 struct page
*page
, struct page_ext
*page_ext
,
220 depot_stack_handle_t handle
)
223 int pageblock_mt
, page_mt
;
225 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
226 struct stack_trace trace
= {
229 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
233 kbuf
= kmalloc(count
, GFP_KERNEL
);
237 ret
= snprintf(kbuf
, count
,
238 "Page allocated via order %u, mask %#x(%pGg)\n",
239 page_ext
->order
, page_ext
->gfp_mask
,
240 &page_ext
->gfp_mask
);
245 /* Print information relevant to grouping pages by mobility */
246 pageblock_mt
= get_pageblock_migratetype(page
);
247 page_mt
= gfpflags_to_migratetype(page_ext
->gfp_mask
);
248 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
249 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
251 migratetype_names
[page_mt
],
252 pfn
>> pageblock_order
,
253 migratetype_names
[pageblock_mt
],
254 page
->flags
, &page
->flags
);
259 depot_fetch_stack(handle
, &trace
);
260 ret
+= snprint_stack_trace(kbuf
+ ret
, count
- ret
, &trace
, 0);
264 if (page_ext
->last_migrate_reason
!= -1) {
265 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
266 "Page has been migrated, last migrate reason: %s\n",
267 migrate_reason_names
[page_ext
->last_migrate_reason
]);
272 ret
+= snprintf(kbuf
+ ret
, count
- ret
, "\n");
276 if (copy_to_user(buf
, kbuf
, ret
))
287 void __dump_page_owner(struct page
*page
)
289 struct page_ext
*page_ext
= lookup_page_ext(page
);
290 unsigned long entries
[PAGE_OWNER_STACK_DEPTH
];
291 struct stack_trace trace
= {
294 .max_entries
= PAGE_OWNER_STACK_DEPTH
,
297 depot_stack_handle_t handle
;
301 if (unlikely(!page_ext
)) {
302 pr_alert("There is not page extension available.\n");
305 gfp_mask
= page_ext
->gfp_mask
;
306 mt
= gfpflags_to_migratetype(gfp_mask
);
308 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
)) {
309 pr_alert("page_owner info is not active (free page?)\n");
313 handle
= READ_ONCE(page_ext
->handle
);
315 pr_alert("page_owner info is not active (free page?)\n");
319 depot_fetch_stack(handle
, &trace
);
320 pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
321 page_ext
->order
, migratetype_names
[mt
], gfp_mask
, &gfp_mask
);
322 print_stack_trace(&trace
, 0);
324 if (page_ext
->last_migrate_reason
!= -1)
325 pr_alert("page has been migrated, last migrate reason: %s\n",
326 migrate_reason_names
[page_ext
->last_migrate_reason
]);
330 read_page_owner(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
334 struct page_ext
*page_ext
;
335 depot_stack_handle_t handle
;
337 if (!static_branch_unlikely(&page_owner_inited
))
341 pfn
= min_low_pfn
+ *ppos
;
343 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
344 while (!pfn_valid(pfn
) && (pfn
& (MAX_ORDER_NR_PAGES
- 1)) != 0)
347 drain_all_pages(NULL
);
349 /* Find an allocated page */
350 for (; pfn
< max_pfn
; pfn
++) {
352 * If the new page is in a new MAX_ORDER_NR_PAGES area,
353 * validate the area as existing, skip it if not
355 if ((pfn
& (MAX_ORDER_NR_PAGES
- 1)) == 0 && !pfn_valid(pfn
)) {
356 pfn
+= MAX_ORDER_NR_PAGES
- 1;
360 /* Check for holes within a MAX_ORDER area */
361 if (!pfn_valid_within(pfn
))
364 page
= pfn_to_page(pfn
);
365 if (PageBuddy(page
)) {
366 unsigned long freepage_order
= page_order_unsafe(page
);
368 if (freepage_order
< MAX_ORDER
)
369 pfn
+= (1UL << freepage_order
) - 1;
373 page_ext
= lookup_page_ext(page
);
374 if (unlikely(!page_ext
))
378 * Some pages could be missed by concurrent allocation or free,
379 * because we don't hold the zone lock.
381 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
385 * Access to page_ext->handle isn't synchronous so we should
386 * be careful to access it.
388 handle
= READ_ONCE(page_ext
->handle
);
392 /* Record the next PFN to read in the file offset */
393 *ppos
= (pfn
- min_low_pfn
) + 1;
395 return print_page_owner(buf
, count
, pfn
, page
,
402 static void init_pages_in_zone(pg_data_t
*pgdat
, struct zone
*zone
)
405 struct page_ext
*page_ext
;
406 unsigned long pfn
= zone
->zone_start_pfn
, block_end_pfn
;
407 unsigned long end_pfn
= pfn
+ zone
->spanned_pages
;
408 unsigned long count
= 0;
410 /* Scan block by block. First and last block may be incomplete */
411 pfn
= zone
->zone_start_pfn
;
414 * Walk the zone in pageblock_nr_pages steps. If a page block spans
415 * a zone boundary, it will be double counted between zones. This does
416 * not matter as the mixed block count will still be correct
418 for (; pfn
< end_pfn
; ) {
419 if (!pfn_valid(pfn
)) {
420 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
424 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
425 block_end_pfn
= min(block_end_pfn
, end_pfn
);
427 page
= pfn_to_page(pfn
);
429 for (; pfn
< block_end_pfn
; pfn
++) {
430 if (!pfn_valid_within(pfn
))
433 page
= pfn_to_page(pfn
);
435 if (page_zone(page
) != zone
)
439 * We are safe to check buddy flag and order, because
440 * this is init stage and only single thread runs.
442 if (PageBuddy(page
)) {
443 pfn
+= (1UL << page_order(page
)) - 1;
447 if (PageReserved(page
))
450 page_ext
= lookup_page_ext(page
);
451 if (unlikely(!page_ext
))
454 /* Maybe overraping zone */
455 if (test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
458 /* Found early allocated page */
459 set_page_owner(page
, 0, 0);
464 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
465 pgdat
->node_id
, zone
->name
, count
);
468 static void init_zones_in_node(pg_data_t
*pgdat
)
471 struct zone
*node_zones
= pgdat
->node_zones
;
474 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
475 if (!populated_zone(zone
))
478 spin_lock_irqsave(&zone
->lock
, flags
);
479 init_pages_in_zone(pgdat
, zone
);
480 spin_unlock_irqrestore(&zone
->lock
, flags
);
484 static void init_early_allocated_pages(void)
488 drain_all_pages(NULL
);
489 for_each_online_pgdat(pgdat
)
490 init_zones_in_node(pgdat
);
493 static const struct file_operations proc_page_owner_operations
= {
494 .read
= read_page_owner
,
497 static int __init
pageowner_init(void)
499 struct dentry
*dentry
;
501 if (!static_branch_unlikely(&page_owner_inited
)) {
502 pr_info("page_owner is disabled\n");
506 dentry
= debugfs_create_file("page_owner", S_IRUSR
, NULL
,
507 NULL
, &proc_page_owner_operations
);
509 return PTR_ERR(dentry
);
513 late_initcall(pageowner_init
)