blob: ec6dc1886f71fa2eb77c5629a9a0bc817613ac91 [file] [log] [blame]
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001#include <linux/debugfs.h>
2#include <linux/mm.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>
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07008#include <linux/jump_label.h>
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07009#include <linux/migrate.h>
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070010#include <linux/stackdepot.h>
11
Joonsoo Kim48c96a32014-12-12 16:56:01 -080012#include "internal.h"
13
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070014/*
15 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
16 * to use off stack temporal storage
17 */
18#define PAGE_OWNER_STACK_DEPTH (16)
19
Joonsoo Kim48c96a32014-12-12 16:56:01 -080020static bool page_owner_disabled = true;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070021DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080022
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070023static depot_stack_handle_t dummy_handle;
24static depot_stack_handle_t failure_handle;
25
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080026static void init_early_allocated_pages(void);
27
Joonsoo Kim48c96a32014-12-12 16:56:01 -080028static int early_page_owner_param(char *buf)
29{
30 if (!buf)
31 return -EINVAL;
32
33 if (strcmp(buf, "on") == 0)
34 page_owner_disabled = false;
35
36 return 0;
37}
38early_param("page_owner", early_page_owner_param);
39
40static bool need_page_owner(void)
41{
42 if (page_owner_disabled)
43 return false;
44
45 return true;
46}
47
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070048static noinline void register_dummy_stack(void)
49{
50 unsigned long entries[4];
51 struct stack_trace dummy;
52
53 dummy.nr_entries = 0;
54 dummy.max_entries = ARRAY_SIZE(entries);
55 dummy.entries = &entries[0];
56 dummy.skip = 0;
57
58 save_stack_trace(&dummy);
59 dummy_handle = depot_save_stack(&dummy, GFP_KERNEL);
60}
61
62static noinline void register_failure_stack(void)
63{
64 unsigned long entries[4];
65 struct stack_trace failure;
66
67 failure.nr_entries = 0;
68 failure.max_entries = ARRAY_SIZE(entries);
69 failure.entries = &entries[0];
70 failure.skip = 0;
71
72 save_stack_trace(&failure);
73 failure_handle = depot_save_stack(&failure, GFP_KERNEL);
74}
75
Joonsoo Kim48c96a32014-12-12 16:56:01 -080076static void init_page_owner(void)
77{
78 if (page_owner_disabled)
79 return;
80
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070081 register_dummy_stack();
82 register_failure_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070083 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080084 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080085}
86
87struct page_ext_operations page_owner_ops = {
88 .need = need_page_owner,
89 .init = init_page_owner,
90};
91
92void __reset_page_owner(struct page *page, unsigned int order)
93{
94 int i;
95 struct page_ext *page_ext;
96
97 for (i = 0; i < (1 << order); i++) {
98 page_ext = lookup_page_ext(page + i);
Yang Shif86e4272016-06-03 14:55:38 -070099 if (unlikely(!page_ext))
100 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800101 __clear_bit(PAGE_EXT_OWNER, &page_ext->flags);
102 }
103}
104
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700105static inline bool check_recursive_alloc(struct stack_trace *trace,
106 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800107{
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700108 int i, count;
Yang Shif86e4272016-06-03 14:55:38 -0700109
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700110 if (!trace->nr_entries)
111 return false;
112
113 for (i = 0, count = 0; i < trace->nr_entries; i++) {
114 if (trace->entries[i] == ip && ++count == 2)
115 return true;
116 }
117
118 return false;
119}
120
121static noinline depot_stack_handle_t save_stack(gfp_t flags)
122{
123 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800124 struct stack_trace trace = {
125 .nr_entries = 0,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700126 .entries = entries,
127 .max_entries = PAGE_OWNER_STACK_DEPTH,
128 .skip = 0
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800129 };
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700130 depot_stack_handle_t handle;
131
132 save_stack_trace(&trace);
133 if (trace.nr_entries != 0 &&
134 trace.entries[trace.nr_entries-1] == ULONG_MAX)
135 trace.nr_entries--;
136
137 /*
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.
143 */
144 if (check_recursive_alloc(&trace, _RET_IP_))
145 return dummy_handle;
146
147 handle = depot_save_stack(&trace, flags);
148 if (!handle)
149 handle = failure_handle;
150
151 return handle;
152}
153
154noinline void __set_page_owner(struct page *page, unsigned int order,
155 gfp_t gfp_mask)
156{
157 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800158
Yang Shif86e4272016-06-03 14:55:38 -0700159 if (unlikely(!page_ext))
160 return;
161
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700162 page_ext->handle = save_stack(gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800163 page_ext->order = order;
164 page_ext->gfp_mask = gfp_mask;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700165 page_ext->last_migrate_reason = -1;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800166
167 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
168}
169
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700170void __set_page_owner_migrate_reason(struct page *page, int reason)
171{
172 struct page_ext *page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700173 if (unlikely(!page_ext))
174 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700175
176 page_ext->last_migrate_reason = reason;
177}
178
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700179void __split_page_owner(struct page *page, unsigned int order)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700180{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700181 int i;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700182 struct page_ext *page_ext = lookup_page_ext(page);
183
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700184 if (unlikely(!page_ext))
185 return;
186
187 page_ext->order = 0;
188 for (i = 1; i < (1 << order); i++)
189 __copy_page_owner(page, page + i);
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700190}
191
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700192void __copy_page_owner(struct page *oldpage, struct page *newpage)
193{
194 struct page_ext *old_ext = lookup_page_ext(oldpage);
195 struct page_ext *new_ext = lookup_page_ext(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700196
Yang Shif86e4272016-06-03 14:55:38 -0700197 if (unlikely(!old_ext || !new_ext))
198 return;
199
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700200 new_ext->order = old_ext->order;
201 new_ext->gfp_mask = old_ext->gfp_mask;
Joonsoo Kima8efe1c2016-07-26 15:23:46 -0700202 new_ext->last_migrate_reason = old_ext->last_migrate_reason;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700203 new_ext->handle = old_ext->handle;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700204
205 /*
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.
213 */
214 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
215}
216
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800217static ssize_t
218print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700219 struct page *page, struct page_ext *page_ext,
220 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800221{
222 int ret;
223 int pageblock_mt, page_mt;
224 char *kbuf;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700225 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800226 struct stack_trace trace = {
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700227 .nr_entries = 0,
228 .entries = entries,
229 .max_entries = PAGE_OWNER_STACK_DEPTH,
230 .skip = 0
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800231 };
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800232
233 kbuf = kmalloc(count, GFP_KERNEL);
234 if (!kbuf)
235 return -ENOMEM;
236
237 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700238 "Page allocated via order %u, mask %#x(%pGg)\n",
239 page_ext->order, page_ext->gfp_mask,
240 &page_ext->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800241
242 if (ret >= count)
243 goto err;
244
245 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700246 pageblock_mt = get_pageblock_migratetype(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800247 page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
248 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700249 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800250 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700251 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800252 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700253 migratetype_names[pageblock_mt],
254 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800255
256 if (ret >= count)
257 goto err;
258
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700259 depot_fetch_stack(handle, &trace);
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800260 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800261 if (ret >= count)
262 goto err;
263
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700264 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]);
268 if (ret >= count)
269 goto err;
270 }
271
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800272 ret += snprintf(kbuf + ret, count - ret, "\n");
273 if (ret >= count)
274 goto err;
275
276 if (copy_to_user(buf, kbuf, ret))
277 ret = -EFAULT;
278
279 kfree(kbuf);
280 return ret;
281
282err:
283 kfree(kbuf);
284 return -ENOMEM;
285}
286
Vlastimil Babka4e462112016-03-15 14:56:21 -0700287void __dump_page_owner(struct page *page)
288{
289 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700290 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Vlastimil Babka4e462112016-03-15 14:56:21 -0700291 struct stack_trace trace = {
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700292 .nr_entries = 0,
293 .entries = entries,
294 .max_entries = PAGE_OWNER_STACK_DEPTH,
295 .skip = 0
Vlastimil Babka4e462112016-03-15 14:56:21 -0700296 };
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700297 depot_stack_handle_t handle;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700298 gfp_t gfp_mask;
299 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700300
Yang Shif86e4272016-06-03 14:55:38 -0700301 if (unlikely(!page_ext)) {
302 pr_alert("There is not page extension available.\n");
303 return;
304 }
Sudip Mukherjee82850272016-06-24 14:50:24 -0700305 gfp_mask = page_ext->gfp_mask;
306 mt = gfpflags_to_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700307
Vlastimil Babka4e462112016-03-15 14:56:21 -0700308 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
309 pr_alert("page_owner info is not active (free page?)\n");
310 return;
311 }
312
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700313 handle = READ_ONCE(page_ext->handle);
314 if (!handle) {
315 pr_alert("page_owner info is not active (free page?)\n");
316 return;
317 }
318
319 depot_fetch_stack(handle, &trace);
Joe Perches756a025f02016-03-17 14:19:47 -0700320 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);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700322 print_stack_trace(&trace, 0);
323
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]);
327}
328
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800329static ssize_t
330read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
331{
332 unsigned long pfn;
333 struct page *page;
334 struct page_ext *page_ext;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700335 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800336
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700337 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800338 return -EINVAL;
339
340 page = NULL;
341 pfn = min_low_pfn + *ppos;
342
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)
345 pfn++;
346
347 drain_all_pages(NULL);
348
349 /* Find an allocated page */
350 for (; pfn < max_pfn; pfn++) {
351 /*
352 * If the new page is in a new MAX_ORDER_NR_PAGES area,
353 * validate the area as existing, skip it if not
354 */
355 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
356 pfn += MAX_ORDER_NR_PAGES - 1;
357 continue;
358 }
359
360 /* Check for holes within a MAX_ORDER area */
361 if (!pfn_valid_within(pfn))
362 continue;
363
364 page = pfn_to_page(pfn);
365 if (PageBuddy(page)) {
366 unsigned long freepage_order = page_order_unsafe(page);
367
368 if (freepage_order < MAX_ORDER)
369 pfn += (1UL << freepage_order) - 1;
370 continue;
371 }
372
373 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700374 if (unlikely(!page_ext))
375 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800376
377 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800378 * Some pages could be missed by concurrent allocation or free,
379 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800380 */
381 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
382 continue;
383
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700384 /*
385 * Access to page_ext->handle isn't synchronous so we should
386 * be careful to access it.
387 */
388 handle = READ_ONCE(page_ext->handle);
389 if (!handle)
390 continue;
391
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800392 /* Record the next PFN to read in the file offset */
393 *ppos = (pfn - min_low_pfn) + 1;
394
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700395 return print_page_owner(buf, count, pfn, page,
396 page_ext, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800397 }
398
399 return 0;
400}
401
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800402static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
403{
404 struct page *page;
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;
409
410 /* Scan block by block. First and last block may be incomplete */
411 pfn = zone->zone_start_pfn;
412
413 /*
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
417 */
418 for (; pfn < end_pfn; ) {
419 if (!pfn_valid(pfn)) {
420 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
421 continue;
422 }
423
424 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
425 block_end_pfn = min(block_end_pfn, end_pfn);
426
427 page = pfn_to_page(pfn);
428
429 for (; pfn < block_end_pfn; pfn++) {
430 if (!pfn_valid_within(pfn))
431 continue;
432
433 page = pfn_to_page(pfn);
434
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700435 if (page_zone(page) != zone)
436 continue;
437
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800438 /*
439 * We are safe to check buddy flag and order, because
440 * this is init stage and only single thread runs.
441 */
442 if (PageBuddy(page)) {
443 pfn += (1UL << page_order(page)) - 1;
444 continue;
445 }
446
447 if (PageReserved(page))
448 continue;
449
450 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700451 if (unlikely(!page_ext))
452 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800453
454 /* Maybe overraping zone */
455 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
456 continue;
457
458 /* Found early allocated page */
459 set_page_owner(page, 0, 0);
460 count++;
461 }
462 }
463
464 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
465 pgdat->node_id, zone->name, count);
466}
467
468static void init_zones_in_node(pg_data_t *pgdat)
469{
470 struct zone *zone;
471 struct zone *node_zones = pgdat->node_zones;
472 unsigned long flags;
473
474 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
475 if (!populated_zone(zone))
476 continue;
477
478 spin_lock_irqsave(&zone->lock, flags);
479 init_pages_in_zone(pgdat, zone);
480 spin_unlock_irqrestore(&zone->lock, flags);
481 }
482}
483
484static void init_early_allocated_pages(void)
485{
486 pg_data_t *pgdat;
487
488 drain_all_pages(NULL);
489 for_each_online_pgdat(pgdat)
490 init_zones_in_node(pgdat);
491}
492
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800493static const struct file_operations proc_page_owner_operations = {
494 .read = read_page_owner,
495};
496
497static int __init pageowner_init(void)
498{
499 struct dentry *dentry;
500
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700501 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800502 pr_info("page_owner is disabled\n");
503 return 0;
504 }
505
506 dentry = debugfs_create_file("page_owner", S_IRUSR, NULL,
507 NULL, &proc_page_owner_operations);
508 if (IS_ERR(dentry))
509 return PTR_ERR(dentry);
510
511 return 0;
512}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400513late_initcall(pageowner_init)