xref: /linux/mm/page_owner.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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/memcontrol.h>
14 #include <linux/sched/clock.h>
15 
16 #include "internal.h"
17 
18 /*
19  * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
20  * to use off stack temporal storage
21  */
22 #define PAGE_OWNER_STACK_DEPTH (16)
23 
24 struct page_owner {
25 	unsigned short order;
26 	short last_migrate_reason;
27 	gfp_t gfp_mask;
28 	depot_stack_handle_t handle;
29 	depot_stack_handle_t free_handle;
30 	u64 ts_nsec;
31 	u64 free_ts_nsec;
32 	char comm[TASK_COMM_LEN];
33 	pid_t pid;
34 	pid_t tgid;
35 	pid_t free_pid;
36 	pid_t free_tgid;
37 };
38 
39 struct stack {
40 	struct stack_record *stack_record;
41 	struct stack *next;
42 };
43 static struct stack dummy_stack;
44 static struct stack failure_stack;
45 static struct stack *stack_list;
46 static DEFINE_SPINLOCK(stack_list_lock);
47 
48 static bool page_owner_enabled __initdata;
49 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
50 
51 static depot_stack_handle_t dummy_handle;
52 static depot_stack_handle_t failure_handle;
53 static depot_stack_handle_t early_handle;
54 
55 static void init_early_allocated_pages(void);
56 
57 static int __init early_page_owner_param(char *buf)
58 {
59 	int ret = kstrtobool(buf, &page_owner_enabled);
60 
61 	if (page_owner_enabled)
62 		stack_depot_request_early_init();
63 
64 	return ret;
65 }
66 early_param("page_owner", early_page_owner_param);
67 
68 static __init bool need_page_owner(void)
69 {
70 	return page_owner_enabled;
71 }
72 
73 static __always_inline depot_stack_handle_t create_dummy_stack(void)
74 {
75 	unsigned long entries[4];
76 	unsigned int nr_entries;
77 
78 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
79 	return stack_depot_save(entries, nr_entries, GFP_KERNEL);
80 }
81 
82 static noinline void register_dummy_stack(void)
83 {
84 	dummy_handle = create_dummy_stack();
85 }
86 
87 static noinline void register_failure_stack(void)
88 {
89 	failure_handle = create_dummy_stack();
90 }
91 
92 static noinline void register_early_stack(void)
93 {
94 	early_handle = create_dummy_stack();
95 }
96 
97 static __init void init_page_owner(void)
98 {
99 	if (!page_owner_enabled)
100 		return;
101 
102 	register_dummy_stack();
103 	register_failure_stack();
104 	register_early_stack();
105 	static_branch_enable(&page_owner_inited);
106 	init_early_allocated_pages();
107 	/* Initialize dummy and failure stacks and link them to stack_list */
108 	dummy_stack.stack_record = __stack_depot_get_stack_record(dummy_handle);
109 	failure_stack.stack_record = __stack_depot_get_stack_record(failure_handle);
110 	if (dummy_stack.stack_record)
111 		refcount_set(&dummy_stack.stack_record->count, 1);
112 	if (failure_stack.stack_record)
113 		refcount_set(&failure_stack.stack_record->count, 1);
114 	dummy_stack.next = &failure_stack;
115 	stack_list = &dummy_stack;
116 }
117 
118 struct page_ext_operations page_owner_ops = {
119 	.size = sizeof(struct page_owner),
120 	.need = need_page_owner,
121 	.init = init_page_owner,
122 	.need_shared_flags = true,
123 };
124 
125 static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
126 {
127 	return page_ext_data(page_ext, &page_owner_ops);
128 }
129 
130 static noinline depot_stack_handle_t save_stack(gfp_t flags)
131 {
132 	unsigned long entries[PAGE_OWNER_STACK_DEPTH];
133 	depot_stack_handle_t handle;
134 	unsigned int nr_entries;
135 
136 	/*
137 	 * Avoid recursion.
138 	 *
139 	 * Sometimes page metadata allocation tracking requires more
140 	 * memory to be allocated:
141 	 * - when new stack trace is saved to stack depot
142 	 */
143 	if (current->in_page_owner)
144 		return dummy_handle;
145 	current->in_page_owner = 1;
146 
147 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
148 	handle = stack_depot_save(entries, nr_entries, flags);
149 	if (!handle)
150 		handle = failure_handle;
151 
152 	current->in_page_owner = 0;
153 	return handle;
154 }
155 
156 static void add_stack_record_to_list(struct stack_record *stack_record,
157 				     gfp_t gfp_mask)
158 {
159 	unsigned long flags;
160 	struct stack *stack;
161 
162 	/* Filter gfp_mask the same way stackdepot does, for consistency */
163 	gfp_mask &= ~GFP_ZONEMASK;
164 	gfp_mask &= (GFP_ATOMIC | GFP_KERNEL);
165 	gfp_mask |= __GFP_NOWARN;
166 
167 	stack = kmalloc(sizeof(*stack), gfp_mask);
168 	if (!stack)
169 		return;
170 
171 	stack->stack_record = stack_record;
172 	stack->next = NULL;
173 
174 	spin_lock_irqsave(&stack_list_lock, flags);
175 	stack->next = stack_list;
176 	/*
177 	 * This pairs with smp_load_acquire() from function
178 	 * stack_start(). This guarantees that stack_start()
179 	 * will see an updated stack_list before starting to
180 	 * traverse the list.
181 	 */
182 	smp_store_release(&stack_list, stack);
183 	spin_unlock_irqrestore(&stack_list_lock, flags);
184 }
185 
186 static void inc_stack_record_count(depot_stack_handle_t handle, gfp_t gfp_mask)
187 {
188 	struct stack_record *stack_record = __stack_depot_get_stack_record(handle);
189 
190 	if (!stack_record)
191 		return;
192 
193 	/*
194 	 * New stack_record's that do not use STACK_DEPOT_FLAG_GET start
195 	 * with REFCOUNT_SATURATED to catch spurious increments of their
196 	 * refcount.
197 	 * Since we do not use STACK_DEPOT_FLAG_GET API, let us
198 	 * set a refcount of 1 ourselves.
199 	 */
200 	if (refcount_read(&stack_record->count) == REFCOUNT_SATURATED) {
201 		int old = REFCOUNT_SATURATED;
202 
203 		if (atomic_try_cmpxchg_relaxed(&stack_record->count.refs, &old, 1))
204 			/* Add the new stack_record to our list */
205 			add_stack_record_to_list(stack_record, gfp_mask);
206 	}
207 	refcount_inc(&stack_record->count);
208 }
209 
210 static void dec_stack_record_count(depot_stack_handle_t handle)
211 {
212 	struct stack_record *stack_record = __stack_depot_get_stack_record(handle);
213 
214 	if (stack_record)
215 		refcount_dec(&stack_record->count);
216 }
217 
218 void __reset_page_owner(struct page *page, unsigned short order)
219 {
220 	int i;
221 	struct page_ext *page_ext;
222 	depot_stack_handle_t handle;
223 	depot_stack_handle_t alloc_handle;
224 	struct page_owner *page_owner;
225 	u64 free_ts_nsec = local_clock();
226 
227 	page_ext = page_ext_get(page);
228 	if (unlikely(!page_ext))
229 		return;
230 
231 	page_owner = get_page_owner(page_ext);
232 	alloc_handle = page_owner->handle;
233 
234 	handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
235 	for (i = 0; i < (1 << order); i++) {
236 		__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
237 		page_owner->free_handle = handle;
238 		page_owner->free_ts_nsec = free_ts_nsec;
239 		page_owner->free_pid = current->pid;
240 		page_owner->free_tgid = current->tgid;
241 		page_ext = page_ext_next(page_ext);
242 		page_owner = get_page_owner(page_ext);
243 	}
244 	page_ext_put(page_ext);
245 	if (alloc_handle != early_handle)
246 		/*
247 		 * early_handle is being set as a handle for all those
248 		 * early allocated pages. See init_pages_in_zone().
249 		 * Since their refcount is not being incremented because
250 		 * the machinery is not ready yet, we cannot decrement
251 		 * their refcount either.
252 		 */
253 		dec_stack_record_count(alloc_handle);
254 }
255 
256 static inline void __set_page_owner_handle(struct page_ext *page_ext,
257 					depot_stack_handle_t handle,
258 					unsigned short order, gfp_t gfp_mask)
259 {
260 	struct page_owner *page_owner;
261 	int i;
262 	u64 ts_nsec = local_clock();
263 
264 	for (i = 0; i < (1 << order); i++) {
265 		page_owner = get_page_owner(page_ext);
266 		page_owner->handle = handle;
267 		page_owner->order = order;
268 		page_owner->gfp_mask = gfp_mask;
269 		page_owner->last_migrate_reason = -1;
270 		page_owner->pid = current->pid;
271 		page_owner->tgid = current->tgid;
272 		page_owner->ts_nsec = ts_nsec;
273 		strscpy(page_owner->comm, current->comm,
274 			sizeof(page_owner->comm));
275 		__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
276 		__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
277 
278 		page_ext = page_ext_next(page_ext);
279 	}
280 }
281 
282 noinline void __set_page_owner(struct page *page, unsigned short order,
283 					gfp_t gfp_mask)
284 {
285 	struct page_ext *page_ext;
286 	depot_stack_handle_t handle;
287 
288 	handle = save_stack(gfp_mask);
289 
290 	page_ext = page_ext_get(page);
291 	if (unlikely(!page_ext))
292 		return;
293 	__set_page_owner_handle(page_ext, handle, order, gfp_mask);
294 	page_ext_put(page_ext);
295 	inc_stack_record_count(handle, gfp_mask);
296 }
297 
298 void __set_page_owner_migrate_reason(struct page *page, int reason)
299 {
300 	struct page_ext *page_ext = page_ext_get(page);
301 	struct page_owner *page_owner;
302 
303 	if (unlikely(!page_ext))
304 		return;
305 
306 	page_owner = get_page_owner(page_ext);
307 	page_owner->last_migrate_reason = reason;
308 	page_ext_put(page_ext);
309 }
310 
311 void __split_page_owner(struct page *page, int old_order, int new_order)
312 {
313 	int i;
314 	struct page_ext *page_ext = page_ext_get(page);
315 	struct page_owner *page_owner;
316 
317 	if (unlikely(!page_ext))
318 		return;
319 
320 	for (i = 0; i < (1 << old_order); i++) {
321 		page_owner = get_page_owner(page_ext);
322 		page_owner->order = new_order;
323 		page_ext = page_ext_next(page_ext);
324 	}
325 	page_ext_put(page_ext);
326 }
327 
328 void __folio_copy_owner(struct folio *newfolio, struct folio *old)
329 {
330 	struct page_ext *old_ext;
331 	struct page_ext *new_ext;
332 	struct page_owner *old_page_owner, *new_page_owner;
333 
334 	old_ext = page_ext_get(&old->page);
335 	if (unlikely(!old_ext))
336 		return;
337 
338 	new_ext = page_ext_get(&newfolio->page);
339 	if (unlikely(!new_ext)) {
340 		page_ext_put(old_ext);
341 		return;
342 	}
343 
344 	old_page_owner = get_page_owner(old_ext);
345 	new_page_owner = get_page_owner(new_ext);
346 	new_page_owner->order = old_page_owner->order;
347 	new_page_owner->gfp_mask = old_page_owner->gfp_mask;
348 	new_page_owner->last_migrate_reason =
349 		old_page_owner->last_migrate_reason;
350 	new_page_owner->handle = old_page_owner->handle;
351 	new_page_owner->pid = old_page_owner->pid;
352 	new_page_owner->tgid = old_page_owner->tgid;
353 	new_page_owner->free_pid = old_page_owner->free_pid;
354 	new_page_owner->free_tgid = old_page_owner->free_tgid;
355 	new_page_owner->ts_nsec = old_page_owner->ts_nsec;
356 	new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
357 	strcpy(new_page_owner->comm, old_page_owner->comm);
358 
359 	/*
360 	 * We don't clear the bit on the old folio as it's going to be freed
361 	 * after migration. Until then, the info can be useful in case of
362 	 * a bug, and the overall stats will be off a bit only temporarily.
363 	 * Also, migrate_misplaced_transhuge_page() can still fail the
364 	 * migration and then we want the old folio to retain the info. But
365 	 * in that case we also don't need to explicitly clear the info from
366 	 * the new page, which will be freed.
367 	 */
368 	__set_bit(PAGE_EXT_OWNER, &new_ext->flags);
369 	__set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
370 	page_ext_put(new_ext);
371 	page_ext_put(old_ext);
372 }
373 
374 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
375 				       pg_data_t *pgdat, struct zone *zone)
376 {
377 	struct page *page;
378 	struct page_ext *page_ext;
379 	struct page_owner *page_owner;
380 	unsigned long pfn, block_end_pfn;
381 	unsigned long end_pfn = zone_end_pfn(zone);
382 	unsigned long count[MIGRATE_TYPES] = { 0, };
383 	int pageblock_mt, page_mt;
384 	int i;
385 
386 	/* Scan block by block. First and last block may be incomplete */
387 	pfn = zone->zone_start_pfn;
388 
389 	/*
390 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
391 	 * a zone boundary, it will be double counted between zones. This does
392 	 * not matter as the mixed block count will still be correct
393 	 */
394 	for (; pfn < end_pfn; ) {
395 		page = pfn_to_online_page(pfn);
396 		if (!page) {
397 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
398 			continue;
399 		}
400 
401 		block_end_pfn = pageblock_end_pfn(pfn);
402 		block_end_pfn = min(block_end_pfn, end_pfn);
403 
404 		pageblock_mt = get_pageblock_migratetype(page);
405 
406 		for (; pfn < block_end_pfn; pfn++) {
407 			/* The pageblock is online, no need to recheck. */
408 			page = pfn_to_page(pfn);
409 
410 			if (page_zone(page) != zone)
411 				continue;
412 
413 			if (PageBuddy(page)) {
414 				unsigned long freepage_order;
415 
416 				freepage_order = buddy_order_unsafe(page);
417 				if (freepage_order <= MAX_PAGE_ORDER)
418 					pfn += (1UL << freepage_order) - 1;
419 				continue;
420 			}
421 
422 			if (PageReserved(page))
423 				continue;
424 
425 			page_ext = page_ext_get(page);
426 			if (unlikely(!page_ext))
427 				continue;
428 
429 			if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
430 				goto ext_put_continue;
431 
432 			page_owner = get_page_owner(page_ext);
433 			page_mt = gfp_migratetype(page_owner->gfp_mask);
434 			if (pageblock_mt != page_mt) {
435 				if (is_migrate_cma(pageblock_mt))
436 					count[MIGRATE_MOVABLE]++;
437 				else
438 					count[pageblock_mt]++;
439 
440 				pfn = block_end_pfn;
441 				page_ext_put(page_ext);
442 				break;
443 			}
444 			pfn += (1UL << page_owner->order) - 1;
445 ext_put_continue:
446 			page_ext_put(page_ext);
447 		}
448 	}
449 
450 	/* Print counts */
451 	seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
452 	for (i = 0; i < MIGRATE_TYPES; i++)
453 		seq_printf(m, "%12lu ", count[i]);
454 	seq_putc(m, '\n');
455 }
456 
457 /*
458  * Looking for memcg information and print it out
459  */
460 static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret,
461 					 struct page *page)
462 {
463 #ifdef CONFIG_MEMCG
464 	unsigned long memcg_data;
465 	struct mem_cgroup *memcg;
466 	bool online;
467 	char name[80];
468 
469 	rcu_read_lock();
470 	memcg_data = READ_ONCE(page->memcg_data);
471 	if (!memcg_data)
472 		goto out_unlock;
473 
474 	if (memcg_data & MEMCG_DATA_OBJCGS)
475 		ret += scnprintf(kbuf + ret, count - ret,
476 				"Slab cache page\n");
477 
478 	memcg = page_memcg_check(page);
479 	if (!memcg)
480 		goto out_unlock;
481 
482 	online = (memcg->css.flags & CSS_ONLINE);
483 	cgroup_name(memcg->css.cgroup, name, sizeof(name));
484 	ret += scnprintf(kbuf + ret, count - ret,
485 			"Charged %sto %smemcg %s\n",
486 			PageMemcgKmem(page) ? "(via objcg) " : "",
487 			online ? "" : "offline ",
488 			name);
489 out_unlock:
490 	rcu_read_unlock();
491 #endif /* CONFIG_MEMCG */
492 
493 	return ret;
494 }
495 
496 static ssize_t
497 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
498 		struct page *page, struct page_owner *page_owner,
499 		depot_stack_handle_t handle)
500 {
501 	int ret, pageblock_mt, page_mt;
502 	char *kbuf;
503 
504 	count = min_t(size_t, count, PAGE_SIZE);
505 	kbuf = kmalloc(count, GFP_KERNEL);
506 	if (!kbuf)
507 		return -ENOMEM;
508 
509 	ret = scnprintf(kbuf, count,
510 			"Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns\n",
511 			page_owner->order, page_owner->gfp_mask,
512 			&page_owner->gfp_mask, page_owner->pid,
513 			page_owner->tgid, page_owner->comm,
514 			page_owner->ts_nsec);
515 
516 	/* Print information relevant to grouping pages by mobility */
517 	pageblock_mt = get_pageblock_migratetype(page);
518 	page_mt  = gfp_migratetype(page_owner->gfp_mask);
519 	ret += scnprintf(kbuf + ret, count - ret,
520 			"PFN 0x%lx type %s Block %lu type %s Flags %pGp\n",
521 			pfn,
522 			migratetype_names[page_mt],
523 			pfn >> pageblock_order,
524 			migratetype_names[pageblock_mt],
525 			&page->flags);
526 
527 	ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0);
528 	if (ret >= count)
529 		goto err;
530 
531 	if (page_owner->last_migrate_reason != -1) {
532 		ret += scnprintf(kbuf + ret, count - ret,
533 			"Page has been migrated, last migrate reason: %s\n",
534 			migrate_reason_names[page_owner->last_migrate_reason]);
535 	}
536 
537 	ret = print_page_owner_memcg(kbuf, count, ret, page);
538 
539 	ret += snprintf(kbuf + ret, count - ret, "\n");
540 	if (ret >= count)
541 		goto err;
542 
543 	if (copy_to_user(buf, kbuf, ret))
544 		ret = -EFAULT;
545 
546 	kfree(kbuf);
547 	return ret;
548 
549 err:
550 	kfree(kbuf);
551 	return -ENOMEM;
552 }
553 
554 void __dump_page_owner(const struct page *page)
555 {
556 	struct page_ext *page_ext = page_ext_get((void *)page);
557 	struct page_owner *page_owner;
558 	depot_stack_handle_t handle;
559 	gfp_t gfp_mask;
560 	int mt;
561 
562 	if (unlikely(!page_ext)) {
563 		pr_alert("There is not page extension available.\n");
564 		return;
565 	}
566 
567 	page_owner = get_page_owner(page_ext);
568 	gfp_mask = page_owner->gfp_mask;
569 	mt = gfp_migratetype(gfp_mask);
570 
571 	if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
572 		pr_alert("page_owner info is not present (never set?)\n");
573 		page_ext_put(page_ext);
574 		return;
575 	}
576 
577 	if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
578 		pr_alert("page_owner tracks the page as allocated\n");
579 	else
580 		pr_alert("page_owner tracks the page as freed\n");
581 
582 	pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu, free_ts %llu\n",
583 		 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
584 		 page_owner->pid, page_owner->tgid, page_owner->comm,
585 		 page_owner->ts_nsec, page_owner->free_ts_nsec);
586 
587 	handle = READ_ONCE(page_owner->handle);
588 	if (!handle)
589 		pr_alert("page_owner allocation stack trace missing\n");
590 	else
591 		stack_depot_print(handle);
592 
593 	handle = READ_ONCE(page_owner->free_handle);
594 	if (!handle) {
595 		pr_alert("page_owner free stack trace missing\n");
596 	} else {
597 		pr_alert("page last free pid %d tgid %d stack trace:\n",
598 			  page_owner->free_pid, page_owner->free_tgid);
599 		stack_depot_print(handle);
600 	}
601 
602 	if (page_owner->last_migrate_reason != -1)
603 		pr_alert("page has been migrated, last migrate reason: %s\n",
604 			migrate_reason_names[page_owner->last_migrate_reason]);
605 	page_ext_put(page_ext);
606 }
607 
608 static ssize_t
609 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
610 {
611 	unsigned long pfn;
612 	struct page *page;
613 	struct page_ext *page_ext;
614 	struct page_owner *page_owner;
615 	depot_stack_handle_t handle;
616 
617 	if (!static_branch_unlikely(&page_owner_inited))
618 		return -EINVAL;
619 
620 	page = NULL;
621 	if (*ppos == 0)
622 		pfn = min_low_pfn;
623 	else
624 		pfn = *ppos;
625 	/* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
626 	while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
627 		pfn++;
628 
629 	/* Find an allocated page */
630 	for (; pfn < max_pfn; pfn++) {
631 		/*
632 		 * This temporary page_owner is required so
633 		 * that we can avoid the context switches while holding
634 		 * the rcu lock and copying the page owner information to
635 		 * user through copy_to_user() or GFP_KERNEL allocations.
636 		 */
637 		struct page_owner page_owner_tmp;
638 
639 		/*
640 		 * If the new page is in a new MAX_ORDER_NR_PAGES area,
641 		 * validate the area as existing, skip it if not
642 		 */
643 		if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
644 			pfn += MAX_ORDER_NR_PAGES - 1;
645 			continue;
646 		}
647 
648 		page = pfn_to_page(pfn);
649 		if (PageBuddy(page)) {
650 			unsigned long freepage_order = buddy_order_unsafe(page);
651 
652 			if (freepage_order <= MAX_PAGE_ORDER)
653 				pfn += (1UL << freepage_order) - 1;
654 			continue;
655 		}
656 
657 		page_ext = page_ext_get(page);
658 		if (unlikely(!page_ext))
659 			continue;
660 
661 		/*
662 		 * Some pages could be missed by concurrent allocation or free,
663 		 * because we don't hold the zone lock.
664 		 */
665 		if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
666 			goto ext_put_continue;
667 
668 		/*
669 		 * Although we do have the info about past allocation of free
670 		 * pages, it's not relevant for current memory usage.
671 		 */
672 		if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
673 			goto ext_put_continue;
674 
675 		page_owner = get_page_owner(page_ext);
676 
677 		/*
678 		 * Don't print "tail" pages of high-order allocations as that
679 		 * would inflate the stats.
680 		 */
681 		if (!IS_ALIGNED(pfn, 1 << page_owner->order))
682 			goto ext_put_continue;
683 
684 		/*
685 		 * Access to page_ext->handle isn't synchronous so we should
686 		 * be careful to access it.
687 		 */
688 		handle = READ_ONCE(page_owner->handle);
689 		if (!handle)
690 			goto ext_put_continue;
691 
692 		/* Record the next PFN to read in the file offset */
693 		*ppos = pfn + 1;
694 
695 		page_owner_tmp = *page_owner;
696 		page_ext_put(page_ext);
697 		return print_page_owner(buf, count, pfn, page,
698 				&page_owner_tmp, handle);
699 ext_put_continue:
700 		page_ext_put(page_ext);
701 	}
702 
703 	return 0;
704 }
705 
706 static loff_t lseek_page_owner(struct file *file, loff_t offset, int orig)
707 {
708 	switch (orig) {
709 	case SEEK_SET:
710 		file->f_pos = offset;
711 		break;
712 	case SEEK_CUR:
713 		file->f_pos += offset;
714 		break;
715 	default:
716 		return -EINVAL;
717 	}
718 	return file->f_pos;
719 }
720 
721 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
722 {
723 	unsigned long pfn = zone->zone_start_pfn;
724 	unsigned long end_pfn = zone_end_pfn(zone);
725 	unsigned long count = 0;
726 
727 	/*
728 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
729 	 * a zone boundary, it will be double counted between zones. This does
730 	 * not matter as the mixed block count will still be correct
731 	 */
732 	for (; pfn < end_pfn; ) {
733 		unsigned long block_end_pfn;
734 
735 		if (!pfn_valid(pfn)) {
736 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
737 			continue;
738 		}
739 
740 		block_end_pfn = pageblock_end_pfn(pfn);
741 		block_end_pfn = min(block_end_pfn, end_pfn);
742 
743 		for (; pfn < block_end_pfn; pfn++) {
744 			struct page *page = pfn_to_page(pfn);
745 			struct page_ext *page_ext;
746 
747 			if (page_zone(page) != zone)
748 				continue;
749 
750 			/*
751 			 * To avoid having to grab zone->lock, be a little
752 			 * careful when reading buddy page order. The only
753 			 * danger is that we skip too much and potentially miss
754 			 * some early allocated pages, which is better than
755 			 * heavy lock contention.
756 			 */
757 			if (PageBuddy(page)) {
758 				unsigned long order = buddy_order_unsafe(page);
759 
760 				if (order > 0 && order <= MAX_PAGE_ORDER)
761 					pfn += (1UL << order) - 1;
762 				continue;
763 			}
764 
765 			if (PageReserved(page))
766 				continue;
767 
768 			page_ext = page_ext_get(page);
769 			if (unlikely(!page_ext))
770 				continue;
771 
772 			/* Maybe overlapping zone */
773 			if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
774 				goto ext_put_continue;
775 
776 			/* Found early allocated page */
777 			__set_page_owner_handle(page_ext, early_handle,
778 						0, 0);
779 			count++;
780 ext_put_continue:
781 			page_ext_put(page_ext);
782 		}
783 		cond_resched();
784 	}
785 
786 	pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
787 		pgdat->node_id, zone->name, count);
788 }
789 
790 static void init_zones_in_node(pg_data_t *pgdat)
791 {
792 	struct zone *zone;
793 	struct zone *node_zones = pgdat->node_zones;
794 
795 	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
796 		if (!populated_zone(zone))
797 			continue;
798 
799 		init_pages_in_zone(pgdat, zone);
800 	}
801 }
802 
803 static void init_early_allocated_pages(void)
804 {
805 	pg_data_t *pgdat;
806 
807 	for_each_online_pgdat(pgdat)
808 		init_zones_in_node(pgdat);
809 }
810 
811 static const struct file_operations proc_page_owner_operations = {
812 	.read		= read_page_owner,
813 	.llseek		= lseek_page_owner,
814 };
815 
816 static void *stack_start(struct seq_file *m, loff_t *ppos)
817 {
818 	struct stack *stack;
819 
820 	if (*ppos == -1UL)
821 		return NULL;
822 
823 	if (!*ppos) {
824 		/*
825 		 * This pairs with smp_store_release() from function
826 		 * add_stack_record_to_list(), so we get a consistent
827 		 * value of stack_list.
828 		 */
829 		stack = smp_load_acquire(&stack_list);
830 	} else {
831 		stack = m->private;
832 		stack = stack->next;
833 	}
834 
835 	m->private = stack;
836 
837 	return stack;
838 }
839 
840 static void *stack_next(struct seq_file *m, void *v, loff_t *ppos)
841 {
842 	struct stack *stack = v;
843 
844 	stack = stack->next;
845 	*ppos = stack ? *ppos + 1 : -1UL;
846 	m->private = stack;
847 
848 	return stack;
849 }
850 
851 static unsigned long page_owner_stack_threshold;
852 
853 static int stack_print(struct seq_file *m, void *v)
854 {
855 	int i, stack_count;
856 	struct stack *stack = v;
857 	unsigned long *entries;
858 	unsigned long nr_entries;
859 	struct stack_record *stack_record = stack->stack_record;
860 
861 	if (!stack->stack_record)
862 		return 0;
863 
864 	nr_entries = stack_record->size;
865 	entries = stack_record->entries;
866 	stack_count = refcount_read(&stack_record->count) - 1;
867 
868 	if (stack_count < 1 || stack_count < page_owner_stack_threshold)
869 		return 0;
870 
871 	for (i = 0; i < nr_entries; i++)
872 		seq_printf(m, " %pS\n", (void *)entries[i]);
873 	seq_printf(m, "stack_count: %d\n\n", stack_count);
874 
875 	return 0;
876 }
877 
878 static void stack_stop(struct seq_file *m, void *v)
879 {
880 }
881 
882 static const struct seq_operations page_owner_stack_op = {
883 	.start	= stack_start,
884 	.next	= stack_next,
885 	.stop	= stack_stop,
886 	.show	= stack_print
887 };
888 
889 static int page_owner_stack_open(struct inode *inode, struct file *file)
890 {
891 	return seq_open_private(file, &page_owner_stack_op, 0);
892 }
893 
894 static const struct file_operations page_owner_stack_operations = {
895 	.open		= page_owner_stack_open,
896 	.read		= seq_read,
897 	.llseek		= seq_lseek,
898 	.release	= seq_release,
899 };
900 
901 static int page_owner_threshold_get(void *data, u64 *val)
902 {
903 	*val = READ_ONCE(page_owner_stack_threshold);
904 	return 0;
905 }
906 
907 static int page_owner_threshold_set(void *data, u64 val)
908 {
909 	WRITE_ONCE(page_owner_stack_threshold, val);
910 	return 0;
911 }
912 
913 DEFINE_SIMPLE_ATTRIBUTE(proc_page_owner_threshold, &page_owner_threshold_get,
914 			&page_owner_threshold_set, "%llu");
915 
916 
917 static int __init pageowner_init(void)
918 {
919 	struct dentry *dir;
920 
921 	if (!static_branch_unlikely(&page_owner_inited)) {
922 		pr_info("page_owner is disabled\n");
923 		return 0;
924 	}
925 
926 	debugfs_create_file("page_owner", 0400, NULL, NULL,
927 			    &proc_page_owner_operations);
928 	dir = debugfs_create_dir("page_owner_stacks", NULL);
929 	debugfs_create_file("show_stacks", 0400, dir, NULL,
930 			    &page_owner_stack_operations);
931 	debugfs_create_file("count_threshold", 0600, dir, NULL,
932 			    &proc_page_owner_threshold);
933 
934 	return 0;
935 }
936 late_initcall(pageowner_init)
937