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