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

source code of linux/mm/page_owner.c