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