1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/debugfs.h>
3 #include <linux/mm.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>
13 #include <linux/sched/clock.h>
14 
15 #include "internal.h"
16 
17 /*
18  * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
19  * to use off stack temporal storage
20  */
21 #define PAGE_OWNER_STACK_DEPTH (16)
22 
23 struct page_owner {
24 	unsigned short order;
25 	short last_migrate_reason;
26 	gfp_t gfp_mask;
27 	depot_stack_handle_t handle;
28 	depot_stack_handle_t free_handle;
29 	u64 ts_nsec;
30 	u64 free_ts_nsec;
31 	pid_t pid;
32 };
33 
34 static bool page_owner_enabled = false;
35 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
36 
37 static depot_stack_handle_t dummy_handle;
38 static depot_stack_handle_t failure_handle;
39 static depot_stack_handle_t early_handle;
40 
41 static void init_early_allocated_pages(void);
42 
early_page_owner_param(char * buf)43 static int __init early_page_owner_param(char *buf)
44 {
45 	return kstrtobool(buf, &page_owner_enabled);
46 }
47 early_param("page_owner", early_page_owner_param);
48 
need_page_owner(void)49 static bool need_page_owner(void)
50 {
51 	return page_owner_enabled;
52 }
53 
create_dummy_stack(void)54 static __always_inline depot_stack_handle_t create_dummy_stack(void)
55 {
56 	unsigned long entries[4];
57 	unsigned int nr_entries;
58 
59 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
60 	return stack_depot_save(entries, nr_entries, GFP_KERNEL);
61 }
62 
register_dummy_stack(void)63 static noinline void register_dummy_stack(void)
64 {
65 	dummy_handle = create_dummy_stack();
66 }
67 
register_failure_stack(void)68 static noinline void register_failure_stack(void)
69 {
70 	failure_handle = create_dummy_stack();
71 }
72 
register_early_stack(void)73 static noinline void register_early_stack(void)
74 {
75 	early_handle = create_dummy_stack();
76 }
77 
init_page_owner(void)78 static void init_page_owner(void)
79 {
80 	if (!page_owner_enabled)
81 		return;
82 
83 	register_dummy_stack();
84 	register_failure_stack();
85 	register_early_stack();
86 	static_branch_enable(&page_owner_inited);
87 	init_early_allocated_pages();
88 }
89 
90 struct page_ext_operations page_owner_ops = {
91 	.size = sizeof(struct page_owner),
92 	.need = need_page_owner,
93 	.init = init_page_owner,
94 };
95 
get_page_owner(struct page_ext * page_ext)96 static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
97 {
98 	return (void *)page_ext + page_owner_ops.offset;
99 }
100 
save_stack(gfp_t flags)101 static noinline depot_stack_handle_t save_stack(gfp_t flags)
102 {
103 	unsigned long entries[PAGE_OWNER_STACK_DEPTH];
104 	depot_stack_handle_t handle;
105 	unsigned int nr_entries;
106 
107 	/*
108 	 * Avoid recursion.
109 	 *
110 	 * Sometimes page metadata allocation tracking requires more
111 	 * memory to be allocated:
112 	 * - when new stack trace is saved to stack depot
113 	 * - when backtrace itself is calculated (ia64)
114 	 */
115 	if (current->in_page_owner)
116 		return dummy_handle;
117 	current->in_page_owner = 1;
118 
119 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
120 	handle = stack_depot_save(entries, nr_entries, flags);
121 	if (!handle)
122 		handle = failure_handle;
123 
124 	current->in_page_owner = 0;
125 	return handle;
126 }
127 
__reset_page_owner(struct page * page,unsigned int order)128 void __reset_page_owner(struct page *page, unsigned int order)
129 {
130 	int i;
131 	struct page_ext *page_ext;
132 	depot_stack_handle_t handle;
133 	struct page_owner *page_owner;
134 	u64 free_ts_nsec = local_clock();
135 
136 	page_ext = lookup_page_ext(page);
137 	if (unlikely(!page_ext))
138 		return;
139 
140 	handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
141 	for (i = 0; i < (1 << order); i++) {
142 		__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
143 		page_owner = get_page_owner(page_ext);
144 		page_owner->free_handle = handle;
145 		page_owner->free_ts_nsec = free_ts_nsec;
146 		page_ext = page_ext_next(page_ext);
147 	}
148 }
149 
__set_page_owner_handle(struct page_ext * page_ext,depot_stack_handle_t handle,unsigned int order,gfp_t gfp_mask)150 static inline void __set_page_owner_handle(struct page_ext *page_ext,
151 					depot_stack_handle_t handle,
152 					unsigned int order, gfp_t gfp_mask)
153 {
154 	struct page_owner *page_owner;
155 	int i;
156 
157 	for (i = 0; i < (1 << order); i++) {
158 		page_owner = get_page_owner(page_ext);
159 		page_owner->handle = handle;
160 		page_owner->order = order;
161 		page_owner->gfp_mask = gfp_mask;
162 		page_owner->last_migrate_reason = -1;
163 		page_owner->pid = current->pid;
164 		page_owner->ts_nsec = local_clock();
165 		__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
166 		__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
167 
168 		page_ext = page_ext_next(page_ext);
169 	}
170 }
171 
__set_page_owner(struct page * page,unsigned int order,gfp_t gfp_mask)172 noinline void __set_page_owner(struct page *page, unsigned int order,
173 					gfp_t gfp_mask)
174 {
175 	struct page_ext *page_ext = lookup_page_ext(page);
176 	depot_stack_handle_t handle;
177 
178 	if (unlikely(!page_ext))
179 		return;
180 
181 	handle = save_stack(gfp_mask);
182 	__set_page_owner_handle(page_ext, handle, order, gfp_mask);
183 }
184 
__set_page_owner_migrate_reason(struct page * page,int reason)185 void __set_page_owner_migrate_reason(struct page *page, int reason)
186 {
187 	struct page_ext *page_ext = lookup_page_ext(page);
188 	struct page_owner *page_owner;
189 
190 	if (unlikely(!page_ext))
191 		return;
192 
193 	page_owner = get_page_owner(page_ext);
194 	page_owner->last_migrate_reason = reason;
195 }
196 
__split_page_owner(struct page * page,unsigned int nr)197 void __split_page_owner(struct page *page, unsigned int nr)
198 {
199 	int i;
200 	struct page_ext *page_ext = lookup_page_ext(page);
201 	struct page_owner *page_owner;
202 
203 	if (unlikely(!page_ext))
204 		return;
205 
206 	for (i = 0; i < nr; i++) {
207 		page_owner = get_page_owner(page_ext);
208 		page_owner->order = 0;
209 		page_ext = page_ext_next(page_ext);
210 	}
211 }
212 
__copy_page_owner(struct page * oldpage,struct page * newpage)213 void __copy_page_owner(struct page *oldpage, struct page *newpage)
214 {
215 	struct page_ext *old_ext = lookup_page_ext(oldpage);
216 	struct page_ext *new_ext = lookup_page_ext(newpage);
217 	struct page_owner *old_page_owner, *new_page_owner;
218 
219 	if (unlikely(!old_ext || !new_ext))
220 		return;
221 
222 	old_page_owner = get_page_owner(old_ext);
223 	new_page_owner = get_page_owner(new_ext);
224 	new_page_owner->order = old_page_owner->order;
225 	new_page_owner->gfp_mask = old_page_owner->gfp_mask;
226 	new_page_owner->last_migrate_reason =
227 		old_page_owner->last_migrate_reason;
228 	new_page_owner->handle = old_page_owner->handle;
229 	new_page_owner->pid = old_page_owner->pid;
230 	new_page_owner->ts_nsec = old_page_owner->ts_nsec;
231 	new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
232 
233 	/*
234 	 * We don't clear the bit on the oldpage as it's going to be freed
235 	 * after migration. Until then, the info can be useful in case of
236 	 * a bug, and the overall stats will be off a bit only temporarily.
237 	 * Also, migrate_misplaced_transhuge_page() can still fail the
238 	 * migration and then we want the oldpage to retain the info. But
239 	 * in that case we also don't need to explicitly clear the info from
240 	 * the new page, which will be freed.
241 	 */
242 	__set_bit(PAGE_EXT_OWNER, &new_ext->flags);
243 	__set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
244 }
245 
pagetypeinfo_showmixedcount_print(struct seq_file * m,pg_data_t * pgdat,struct zone * zone)246 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
247 				       pg_data_t *pgdat, struct zone *zone)
248 {
249 	struct page *page;
250 	struct page_ext *page_ext;
251 	struct page_owner *page_owner;
252 	unsigned long pfn, block_end_pfn;
253 	unsigned long end_pfn = zone_end_pfn(zone);
254 	unsigned long count[MIGRATE_TYPES] = { 0, };
255 	int pageblock_mt, page_mt;
256 	int i;
257 
258 	/* Scan block by block. First and last block may be incomplete */
259 	pfn = zone->zone_start_pfn;
260 
261 	/*
262 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
263 	 * a zone boundary, it will be double counted between zones. This does
264 	 * not matter as the mixed block count will still be correct
265 	 */
266 	for (; pfn < end_pfn; ) {
267 		page = pfn_to_online_page(pfn);
268 		if (!page) {
269 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
270 			continue;
271 		}
272 
273 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
274 		block_end_pfn = min(block_end_pfn, end_pfn);
275 
276 		pageblock_mt = get_pageblock_migratetype(page);
277 
278 		for (; pfn < block_end_pfn; pfn++) {
279 			/* The pageblock is online, no need to recheck. */
280 			page = pfn_to_page(pfn);
281 
282 			if (page_zone(page) != zone)
283 				continue;
284 
285 			if (PageBuddy(page)) {
286 				unsigned long freepage_order;
287 
288 				freepage_order = buddy_order_unsafe(page);
289 				if (freepage_order < MAX_ORDER)
290 					pfn += (1UL << freepage_order) - 1;
291 				continue;
292 			}
293 
294 			if (PageReserved(page))
295 				continue;
296 
297 			page_ext = lookup_page_ext(page);
298 			if (unlikely(!page_ext))
299 				continue;
300 
301 			if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
302 				continue;
303 
304 			page_owner = get_page_owner(page_ext);
305 			page_mt = gfp_migratetype(page_owner->gfp_mask);
306 			if (pageblock_mt != page_mt) {
307 				if (is_migrate_cma(pageblock_mt))
308 					count[MIGRATE_MOVABLE]++;
309 				else
310 					count[pageblock_mt]++;
311 
312 				pfn = block_end_pfn;
313 				break;
314 			}
315 			pfn += (1UL << page_owner->order) - 1;
316 		}
317 	}
318 
319 	/* Print counts */
320 	seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
321 	for (i = 0; i < MIGRATE_TYPES; i++)
322 		seq_printf(m, "%12lu ", count[i]);
323 	seq_putc(m, '\n');
324 }
325 
326 static ssize_t
print_page_owner(char __user * buf,size_t count,unsigned long pfn,struct page * page,struct page_owner * page_owner,depot_stack_handle_t handle)327 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
328 		struct page *page, struct page_owner *page_owner,
329 		depot_stack_handle_t handle)
330 {
331 	int ret, pageblock_mt, page_mt;
332 	unsigned long *entries;
333 	unsigned int nr_entries;
334 	char *kbuf;
335 
336 	count = min_t(size_t, count, PAGE_SIZE);
337 	kbuf = kmalloc(count, GFP_KERNEL);
338 	if (!kbuf)
339 		return -ENOMEM;
340 
341 	ret = snprintf(kbuf, count,
342 			"Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n",
343 			page_owner->order, page_owner->gfp_mask,
344 			&page_owner->gfp_mask, page_owner->pid,
345 			page_owner->ts_nsec, page_owner->free_ts_nsec);
346 
347 	if (ret >= count)
348 		goto err;
349 
350 	/* Print information relevant to grouping pages by mobility */
351 	pageblock_mt = get_pageblock_migratetype(page);
352 	page_mt  = gfp_migratetype(page_owner->gfp_mask);
353 	ret += snprintf(kbuf + ret, count - ret,
354 			"PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
355 			pfn,
356 			migratetype_names[page_mt],
357 			pfn >> pageblock_order,
358 			migratetype_names[pageblock_mt],
359 			page->flags, &page->flags);
360 
361 	if (ret >= count)
362 		goto err;
363 
364 	nr_entries = stack_depot_fetch(handle, &entries);
365 	ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
366 	if (ret >= count)
367 		goto err;
368 
369 	if (page_owner->last_migrate_reason != -1) {
370 		ret += snprintf(kbuf + ret, count - ret,
371 			"Page has been migrated, last migrate reason: %s\n",
372 			migrate_reason_names[page_owner->last_migrate_reason]);
373 		if (ret >= count)
374 			goto err;
375 	}
376 
377 	ret += snprintf(kbuf + ret, count - ret, "\n");
378 	if (ret >= count)
379 		goto err;
380 
381 	if (copy_to_user(buf, kbuf, ret))
382 		ret = -EFAULT;
383 
384 	kfree(kbuf);
385 	return ret;
386 
387 err:
388 	kfree(kbuf);
389 	return -ENOMEM;
390 }
391 
__dump_page_owner(const struct page * page)392 void __dump_page_owner(const struct page *page)
393 {
394 	struct page_ext *page_ext = lookup_page_ext(page);
395 	struct page_owner *page_owner;
396 	depot_stack_handle_t handle;
397 	unsigned long *entries;
398 	unsigned int nr_entries;
399 	gfp_t gfp_mask;
400 	int mt;
401 
402 	if (unlikely(!page_ext)) {
403 		pr_alert("There is not page extension available.\n");
404 		return;
405 	}
406 
407 	page_owner = get_page_owner(page_ext);
408 	gfp_mask = page_owner->gfp_mask;
409 	mt = gfp_migratetype(gfp_mask);
410 
411 	if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
412 		pr_alert("page_owner info is not present (never set?)\n");
413 		return;
414 	}
415 
416 	if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
417 		pr_alert("page_owner tracks the page as allocated\n");
418 	else
419 		pr_alert("page_owner tracks the page as freed\n");
420 
421 	pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu, free_ts %llu\n",
422 		 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
423 		 page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec);
424 
425 	handle = READ_ONCE(page_owner->handle);
426 	if (!handle) {
427 		pr_alert("page_owner allocation stack trace missing\n");
428 	} else {
429 		nr_entries = stack_depot_fetch(handle, &entries);
430 		stack_trace_print(entries, nr_entries, 0);
431 	}
432 
433 	handle = READ_ONCE(page_owner->free_handle);
434 	if (!handle) {
435 		pr_alert("page_owner free stack trace missing\n");
436 	} else {
437 		nr_entries = stack_depot_fetch(handle, &entries);
438 		pr_alert("page last free stack trace:\n");
439 		stack_trace_print(entries, nr_entries, 0);
440 	}
441 
442 	if (page_owner->last_migrate_reason != -1)
443 		pr_alert("page has been migrated, last migrate reason: %s\n",
444 			migrate_reason_names[page_owner->last_migrate_reason]);
445 }
446 
447 static ssize_t
read_page_owner(struct file * file,char __user * buf,size_t count,loff_t * ppos)448 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
449 {
450 	unsigned long pfn;
451 	struct page *page;
452 	struct page_ext *page_ext;
453 	struct page_owner *page_owner;
454 	depot_stack_handle_t handle;
455 
456 	if (!static_branch_unlikely(&page_owner_inited))
457 		return -EINVAL;
458 
459 	page = NULL;
460 	pfn = min_low_pfn + *ppos;
461 
462 	/* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
463 	while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
464 		pfn++;
465 
466 	drain_all_pages(NULL);
467 
468 	/* Find an allocated page */
469 	for (; pfn < max_pfn; pfn++) {
470 		/*
471 		 * If the new page is in a new MAX_ORDER_NR_PAGES area,
472 		 * validate the area as existing, skip it if not
473 		 */
474 		if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
475 			pfn += MAX_ORDER_NR_PAGES - 1;
476 			continue;
477 		}
478 
479 		page = pfn_to_page(pfn);
480 		if (PageBuddy(page)) {
481 			unsigned long freepage_order = buddy_order_unsafe(page);
482 
483 			if (freepage_order < MAX_ORDER)
484 				pfn += (1UL << freepage_order) - 1;
485 			continue;
486 		}
487 
488 		page_ext = lookup_page_ext(page);
489 		if (unlikely(!page_ext))
490 			continue;
491 
492 		/*
493 		 * Some pages could be missed by concurrent allocation or free,
494 		 * because we don't hold the zone lock.
495 		 */
496 		if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
497 			continue;
498 
499 		/*
500 		 * Although we do have the info about past allocation of free
501 		 * pages, it's not relevant for current memory usage.
502 		 */
503 		if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
504 			continue;
505 
506 		page_owner = get_page_owner(page_ext);
507 
508 		/*
509 		 * Don't print "tail" pages of high-order allocations as that
510 		 * would inflate the stats.
511 		 */
512 		if (!IS_ALIGNED(pfn, 1 << page_owner->order))
513 			continue;
514 
515 		/*
516 		 * Access to page_ext->handle isn't synchronous so we should
517 		 * be careful to access it.
518 		 */
519 		handle = READ_ONCE(page_owner->handle);
520 		if (!handle)
521 			continue;
522 
523 		/* Record the next PFN to read in the file offset */
524 		*ppos = (pfn - min_low_pfn) + 1;
525 
526 		return print_page_owner(buf, count, pfn, page,
527 				page_owner, handle);
528 	}
529 
530 	return 0;
531 }
532 
init_pages_in_zone(pg_data_t * pgdat,struct zone * zone)533 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
534 {
535 	unsigned long pfn = zone->zone_start_pfn;
536 	unsigned long end_pfn = zone_end_pfn(zone);
537 	unsigned long count = 0;
538 
539 	/*
540 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
541 	 * a zone boundary, it will be double counted between zones. This does
542 	 * not matter as the mixed block count will still be correct
543 	 */
544 	for (; pfn < end_pfn; ) {
545 		unsigned long block_end_pfn;
546 
547 		if (!pfn_valid(pfn)) {
548 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
549 			continue;
550 		}
551 
552 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
553 		block_end_pfn = min(block_end_pfn, end_pfn);
554 
555 		for (; pfn < block_end_pfn; pfn++) {
556 			struct page *page = pfn_to_page(pfn);
557 			struct page_ext *page_ext;
558 
559 			if (page_zone(page) != zone)
560 				continue;
561 
562 			/*
563 			 * To avoid having to grab zone->lock, be a little
564 			 * careful when reading buddy page order. The only
565 			 * danger is that we skip too much and potentially miss
566 			 * some early allocated pages, which is better than
567 			 * heavy lock contention.
568 			 */
569 			if (PageBuddy(page)) {
570 				unsigned long order = buddy_order_unsafe(page);
571 
572 				if (order > 0 && order < MAX_ORDER)
573 					pfn += (1UL << order) - 1;
574 				continue;
575 			}
576 
577 			if (PageReserved(page))
578 				continue;
579 
580 			page_ext = lookup_page_ext(page);
581 			if (unlikely(!page_ext))
582 				continue;
583 
584 			/* Maybe overlapping zone */
585 			if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
586 				continue;
587 
588 			/* Found early allocated page */
589 			__set_page_owner_handle(page_ext, early_handle,
590 						0, 0);
591 			count++;
592 		}
593 		cond_resched();
594 	}
595 
596 	pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
597 		pgdat->node_id, zone->name, count);
598 }
599 
init_zones_in_node(pg_data_t * pgdat)600 static void init_zones_in_node(pg_data_t *pgdat)
601 {
602 	struct zone *zone;
603 	struct zone *node_zones = pgdat->node_zones;
604 
605 	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
606 		if (!populated_zone(zone))
607 			continue;
608 
609 		init_pages_in_zone(pgdat, zone);
610 	}
611 }
612 
init_early_allocated_pages(void)613 static void init_early_allocated_pages(void)
614 {
615 	pg_data_t *pgdat;
616 
617 	for_each_online_pgdat(pgdat)
618 		init_zones_in_node(pgdat);
619 }
620 
621 static const struct file_operations proc_page_owner_operations = {
622 	.read		= read_page_owner,
623 };
624 
pageowner_init(void)625 static int __init pageowner_init(void)
626 {
627 	if (!static_branch_unlikely(&page_owner_inited)) {
628 		pr_info("page_owner is disabled\n");
629 		return 0;
630 	}
631 
632 	debugfs_create_file("page_owner", 0400, NULL, NULL,
633 			    &proc_page_owner_operations);
634 
635 	return 0;
636 }
637 late_initcall(pageowner_init)
638