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>
12 static bool page_owner_disabled
= true;
13 DEFINE_STATIC_KEY_FALSE(page_owner_inited
);
15 static void init_early_allocated_pages(void);
17 static int early_page_owner_param(char *buf
)
22 if (strcmp(buf
, "on") == 0)
23 page_owner_disabled
= false;
27 early_param("page_owner", early_page_owner_param
);
29 static bool need_page_owner(void)
31 if (page_owner_disabled
)
37 static void init_page_owner(void)
39 if (page_owner_disabled
)
42 static_branch_enable(&page_owner_inited
);
43 init_early_allocated_pages();
46 struct page_ext_operations page_owner_ops
= {
47 .need
= need_page_owner
,
48 .init
= init_page_owner
,
51 void __reset_page_owner(struct page
*page
, unsigned int order
)
54 struct page_ext
*page_ext
;
56 for (i
= 0; i
< (1 << order
); i
++) {
57 page_ext
= lookup_page_ext(page
+ i
);
58 __clear_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
62 void __set_page_owner(struct page
*page
, unsigned int order
, gfp_t gfp_mask
)
64 struct page_ext
*page_ext
= lookup_page_ext(page
);
65 struct stack_trace trace
= {
67 .max_entries
= ARRAY_SIZE(page_ext
->trace_entries
),
68 .entries
= &page_ext
->trace_entries
[0],
72 save_stack_trace(&trace
);
74 page_ext
->order
= order
;
75 page_ext
->gfp_mask
= gfp_mask
;
76 page_ext
->nr_entries
= trace
.nr_entries
;
77 page_ext
->last_migrate_reason
= -1;
79 __set_bit(PAGE_EXT_OWNER
, &page_ext
->flags
);
82 void __set_page_owner_migrate_reason(struct page
*page
, int reason
)
84 struct page_ext
*page_ext
= lookup_page_ext(page
);
86 page_ext
->last_migrate_reason
= reason
;
89 gfp_t
__get_page_owner_gfp(struct page
*page
)
91 struct page_ext
*page_ext
= lookup_page_ext(page
);
93 return page_ext
->gfp_mask
;
96 void __copy_page_owner(struct page
*oldpage
, struct page
*newpage
)
98 struct page_ext
*old_ext
= lookup_page_ext(oldpage
);
99 struct page_ext
*new_ext
= lookup_page_ext(newpage
);
102 new_ext
->order
= old_ext
->order
;
103 new_ext
->gfp_mask
= old_ext
->gfp_mask
;
104 new_ext
->nr_entries
= old_ext
->nr_entries
;
106 for (i
= 0; i
< ARRAY_SIZE(new_ext
->trace_entries
); i
++)
107 new_ext
->trace_entries
[i
] = old_ext
->trace_entries
[i
];
110 * We don't clear the bit on the oldpage as it's going to be freed
111 * after migration. Until then, the info can be useful in case of
112 * a bug, and the overal stats will be off a bit only temporarily.
113 * Also, migrate_misplaced_transhuge_page() can still fail the
114 * migration and then we want the oldpage to retain the info. But
115 * in that case we also don't need to explicitly clear the info from
116 * the new page, which will be freed.
118 __set_bit(PAGE_EXT_OWNER
, &new_ext
->flags
);
122 print_page_owner(char __user
*buf
, size_t count
, unsigned long pfn
,
123 struct page
*page
, struct page_ext
*page_ext
)
126 int pageblock_mt
, page_mt
;
128 struct stack_trace trace
= {
129 .nr_entries
= page_ext
->nr_entries
,
130 .entries
= &page_ext
->trace_entries
[0],
133 kbuf
= kmalloc(count
, GFP_KERNEL
);
137 ret
= snprintf(kbuf
, count
,
138 "Page allocated via order %u, mask %#x(%pGg)\n",
139 page_ext
->order
, page_ext
->gfp_mask
,
140 &page_ext
->gfp_mask
);
145 /* Print information relevant to grouping pages by mobility */
146 pageblock_mt
= get_pfnblock_migratetype(page
, pfn
);
147 page_mt
= gfpflags_to_migratetype(page_ext
->gfp_mask
);
148 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
149 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
151 migratetype_names
[page_mt
],
152 pfn
>> pageblock_order
,
153 migratetype_names
[pageblock_mt
],
154 page
->flags
, &page
->flags
);
159 ret
+= snprint_stack_trace(kbuf
+ ret
, count
- ret
, &trace
, 0);
163 if (page_ext
->last_migrate_reason
!= -1) {
164 ret
+= snprintf(kbuf
+ ret
, count
- ret
,
165 "Page has been migrated, last migrate reason: %s\n",
166 migrate_reason_names
[page_ext
->last_migrate_reason
]);
171 ret
+= snprintf(kbuf
+ ret
, count
- ret
, "\n");
175 if (copy_to_user(buf
, kbuf
, ret
))
186 void __dump_page_owner(struct page
*page
)
188 struct page_ext
*page_ext
= lookup_page_ext(page
);
189 struct stack_trace trace
= {
190 .nr_entries
= page_ext
->nr_entries
,
191 .entries
= &page_ext
->trace_entries
[0],
193 gfp_t gfp_mask
= page_ext
->gfp_mask
;
194 int mt
= gfpflags_to_migratetype(gfp_mask
);
196 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
)) {
197 pr_alert("page_owner info is not active (free page?)\n");
201 pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
202 page_ext
->order
, migratetype_names
[mt
], gfp_mask
, &gfp_mask
);
203 print_stack_trace(&trace
, 0);
205 if (page_ext
->last_migrate_reason
!= -1)
206 pr_alert("page has been migrated, last migrate reason: %s\n",
207 migrate_reason_names
[page_ext
->last_migrate_reason
]);
211 read_page_owner(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
215 struct page_ext
*page_ext
;
217 if (!static_branch_unlikely(&page_owner_inited
))
221 pfn
= min_low_pfn
+ *ppos
;
223 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
224 while (!pfn_valid(pfn
) && (pfn
& (MAX_ORDER_NR_PAGES
- 1)) != 0)
227 drain_all_pages(NULL
);
229 /* Find an allocated page */
230 for (; pfn
< max_pfn
; pfn
++) {
232 * If the new page is in a new MAX_ORDER_NR_PAGES area,
233 * validate the area as existing, skip it if not
235 if ((pfn
& (MAX_ORDER_NR_PAGES
- 1)) == 0 && !pfn_valid(pfn
)) {
236 pfn
+= MAX_ORDER_NR_PAGES
- 1;
240 /* Check for holes within a MAX_ORDER area */
241 if (!pfn_valid_within(pfn
))
244 page
= pfn_to_page(pfn
);
245 if (PageBuddy(page
)) {
246 unsigned long freepage_order
= page_order_unsafe(page
);
248 if (freepage_order
< MAX_ORDER
)
249 pfn
+= (1UL << freepage_order
) - 1;
253 page_ext
= lookup_page_ext(page
);
256 * Some pages could be missed by concurrent allocation or free,
257 * because we don't hold the zone lock.
259 if (!test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
262 /* Record the next PFN to read in the file offset */
263 *ppos
= (pfn
- min_low_pfn
) + 1;
265 return print_page_owner(buf
, count
, pfn
, page
, page_ext
);
271 static void init_pages_in_zone(pg_data_t
*pgdat
, struct zone
*zone
)
274 struct page_ext
*page_ext
;
275 unsigned long pfn
= zone
->zone_start_pfn
, block_end_pfn
;
276 unsigned long end_pfn
= pfn
+ zone
->spanned_pages
;
277 unsigned long count
= 0;
279 /* Scan block by block. First and last block may be incomplete */
280 pfn
= zone
->zone_start_pfn
;
283 * Walk the zone in pageblock_nr_pages steps. If a page block spans
284 * a zone boundary, it will be double counted between zones. This does
285 * not matter as the mixed block count will still be correct
287 for (; pfn
< end_pfn
; ) {
288 if (!pfn_valid(pfn
)) {
289 pfn
= ALIGN(pfn
+ 1, MAX_ORDER_NR_PAGES
);
293 block_end_pfn
= ALIGN(pfn
+ 1, pageblock_nr_pages
);
294 block_end_pfn
= min(block_end_pfn
, end_pfn
);
296 page
= pfn_to_page(pfn
);
298 for (; pfn
< block_end_pfn
; pfn
++) {
299 if (!pfn_valid_within(pfn
))
302 page
= pfn_to_page(pfn
);
305 * We are safe to check buddy flag and order, because
306 * this is init stage and only single thread runs.
308 if (PageBuddy(page
)) {
309 pfn
+= (1UL << page_order(page
)) - 1;
313 if (PageReserved(page
))
316 page_ext
= lookup_page_ext(page
);
318 /* Maybe overraping zone */
319 if (test_bit(PAGE_EXT_OWNER
, &page_ext
->flags
))
322 /* Found early allocated page */
323 set_page_owner(page
, 0, 0);
328 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
329 pgdat
->node_id
, zone
->name
, count
);
332 static void init_zones_in_node(pg_data_t
*pgdat
)
335 struct zone
*node_zones
= pgdat
->node_zones
;
338 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
339 if (!populated_zone(zone
))
342 spin_lock_irqsave(&zone
->lock
, flags
);
343 init_pages_in_zone(pgdat
, zone
);
344 spin_unlock_irqrestore(&zone
->lock
, flags
);
348 static void init_early_allocated_pages(void)
352 drain_all_pages(NULL
);
353 for_each_online_pgdat(pgdat
)
354 init_zones_in_node(pgdat
);
357 static const struct file_operations proc_page_owner_operations
= {
358 .read
= read_page_owner
,
361 static int __init
pageowner_init(void)
363 struct dentry
*dentry
;
365 if (!static_branch_unlikely(&page_owner_inited
)) {
366 pr_info("page_owner is disabled\n");
370 dentry
= debugfs_create_file("page_owner", S_IRUSR
, NULL
,
371 NULL
, &proc_page_owner_operations
);
373 return PTR_ERR(dentry
);
377 late_initcall(pageowner_init
)