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