blob: dee931184788399c920646cef75e29e8fd347186 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Joonsoo Kim48c96a32014-12-12 16:56:01 -08002#include <linux/debugfs.h>
3#include <linux/mm.h>
4#include <linux/slab.h>
5#include <linux/uaccess.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07006#include <linux/memblock.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -08007#include <linux/stacktrace.h>
8#include <linux/page_owner.h>
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07009#include <linux/jump_label.h>
Vlastimil Babka7cd12b42016-03-15 14:56:18 -070010#include <linux/migrate.h>
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070011#include <linux/stackdepot.h>
Joonsoo Kime2f612e2016-10-07 16:58:21 -070012#include <linux/seq_file.h>
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070013
Joonsoo Kim48c96a32014-12-12 16:56:01 -080014#include "internal.h"
15
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070016/*
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
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070022struct page_owner {
Ayush Mittal6b4c54e2017-11-15 17:34:30 -080023 unsigned short order;
24 short last_migrate_reason;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070025 gfp_t gfp_mask;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070026 depot_stack_handle_t handle;
Vlastimil Babka89745582019-09-23 15:34:42 -070027#ifdef CONFIG_DEBUG_PAGEALLOC
28 depot_stack_handle_t free_handle;
29#endif
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070030};
31
Joonsoo Kim48c96a32014-12-12 16:56:01 -080032static bool page_owner_disabled = true;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070033DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080034
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070035static depot_stack_handle_t dummy_handle;
36static depot_stack_handle_t failure_handle;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070037static depot_stack_handle_t early_handle;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070038
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080039static void init_early_allocated_pages(void);
40
Dou Liyang11731942018-04-05 16:23:49 -070041static int __init early_page_owner_param(char *buf)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080042{
43 if (!buf)
44 return -EINVAL;
45
46 if (strcmp(buf, "on") == 0)
47 page_owner_disabled = false;
48
49 return 0;
50}
51early_param("page_owner", early_page_owner_param);
52
53static bool need_page_owner(void)
54{
55 if (page_owner_disabled)
56 return false;
57
58 return true;
59}
60
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070061static __always_inline depot_stack_handle_t create_dummy_stack(void)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070062{
63 unsigned long entries[4];
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020064 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070065
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020066 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
67 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070068}
69
70static noinline void register_dummy_stack(void)
71{
72 dummy_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070073}
74
75static noinline void register_failure_stack(void)
76{
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070077 failure_handle = create_dummy_stack();
78}
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070079
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070080static noinline void register_early_stack(void)
81{
82 early_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070083}
84
Joonsoo Kim48c96a32014-12-12 16:56:01 -080085static void init_page_owner(void)
86{
87 if (page_owner_disabled)
88 return;
89
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070090 register_dummy_stack();
91 register_failure_stack();
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070092 register_early_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070093 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080094 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080095}
96
97struct page_ext_operations page_owner_ops = {
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070098 .size = sizeof(struct page_owner),
Joonsoo Kim48c96a32014-12-12 16:56:01 -080099 .need = need_page_owner,
100 .init = init_page_owner,
101};
102
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700103static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
104{
105 return (void *)page_ext + page_owner_ops.offset;
106}
107
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200108static inline bool check_recursive_alloc(unsigned long *entries,
109 unsigned int nr_entries,
110 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800111{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200112 unsigned int i;
Yang Shif86e4272016-06-03 14:55:38 -0700113
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200114 for (i = 0; i < nr_entries; i++) {
115 if (entries[i] == ip)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700116 return true;
117 }
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700118 return false;
119}
120
121static noinline depot_stack_handle_t save_stack(gfp_t flags)
122{
123 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700124 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200125 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700126
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200127 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700128
129 /*
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200130 * 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.
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700136 */
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200137 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700138 return dummy_handle;
139
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200140 handle = stack_depot_save(entries, nr_entries, flags);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700141 if (!handle)
142 handle = failure_handle;
143
144 return handle;
145}
146
Vlastimil Babka89745582019-09-23 15:34:42 -0700147void __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
159 for (i = 0; i < (1 << order); i++) {
160 page_ext = lookup_page_ext(page + i);
161 if (unlikely(!page_ext))
162 continue;
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
170 }
171}
172
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700173static inline void __set_page_owner_handle(struct page *page,
174 struct page_ext *page_ext, depot_stack_handle_t handle,
175 unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700176{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700177 struct page_owner *page_owner;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700178 int i;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800179
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700180 for (i = 0; i < (1 << order); i++) {
181 page_owner = get_page_owner(page_ext);
182 page_owner->handle = handle;
183 page_owner->order = order;
184 page_owner->gfp_mask = gfp_mask;
185 page_owner->last_migrate_reason = -1;
186 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
Vlastimil Babka373891672019-09-23 15:34:39 -0700187 __set_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800188
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700189 page_ext = lookup_page_ext(page + i);
190 }
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800191}
192
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700193noinline void __set_page_owner(struct page *page, unsigned int order,
194 gfp_t gfp_mask)
195{
196 struct page_ext *page_ext = lookup_page_ext(page);
197 depot_stack_handle_t handle;
198
199 if (unlikely(!page_ext))
200 return;
201
202 handle = save_stack(gfp_mask);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700203 __set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700204}
205
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700206void __set_page_owner_migrate_reason(struct page *page, int reason)
207{
208 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700209 struct page_owner *page_owner;
210
Yang Shif86e4272016-06-03 14:55:38 -0700211 if (unlikely(!page_ext))
212 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700213
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700214 page_owner = get_page_owner(page_ext);
215 page_owner->last_migrate_reason = reason;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700216}
217
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700218void __split_page_owner(struct page *page, unsigned int order)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700219{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700220 int i;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700221 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700222 struct page_owner *page_owner;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700223
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700224 if (unlikely(!page_ext))
225 return;
226
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700227 page_owner = get_page_owner(page_ext);
228 page_owner->order = 0;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700229 for (i = 1; i < (1 << order); i++) {
230 page_ext = lookup_page_ext(page + i);
231 page_owner = get_page_owner(page_ext);
232 page_owner->order = 0;
233 }
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700234}
235
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700236void __copy_page_owner(struct page *oldpage, struct page *newpage)
237{
238 struct page_ext *old_ext = lookup_page_ext(oldpage);
239 struct page_ext *new_ext = lookup_page_ext(newpage);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700240 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700241
Yang Shif86e4272016-06-03 14:55:38 -0700242 if (unlikely(!old_ext || !new_ext))
243 return;
244
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700245 old_page_owner = get_page_owner(old_ext);
246 new_page_owner = get_page_owner(new_ext);
247 new_page_owner->order = old_page_owner->order;
248 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
249 new_page_owner->last_migrate_reason =
250 old_page_owner->last_migrate_reason;
251 new_page_owner->handle = old_page_owner->handle;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700252
253 /*
254 * We don't clear the bit on the oldpage as it's going to be freed
255 * after migration. Until then, the info can be useful in case of
256 * a bug, and the overal stats will be off a bit only temporarily.
257 * Also, migrate_misplaced_transhuge_page() can still fail the
258 * migration and then we want the oldpage to retain the info. But
259 * in that case we also don't need to explicitly clear the info from
260 * the new page, which will be freed.
261 */
262 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
Vlastimil Babka373891672019-09-23 15:34:39 -0700263 __set_bit(PAGE_EXT_OWNER_ACTIVE, &new_ext->flags);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700264}
265
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700266void pagetypeinfo_showmixedcount_print(struct seq_file *m,
267 pg_data_t *pgdat, struct zone *zone)
268{
269 struct page *page;
270 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700271 struct page_owner *page_owner;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700272 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
273 unsigned long end_pfn = pfn + zone->spanned_pages;
274 unsigned long count[MIGRATE_TYPES] = { 0, };
275 int pageblock_mt, page_mt;
276 int i;
277
278 /* Scan block by block. First and last block may be incomplete */
279 pfn = zone->zone_start_pfn;
280
281 /*
282 * Walk the zone in pageblock_nr_pages steps. If a page block spans
283 * a zone boundary, it will be double counted between zones. This does
284 * not matter as the mixed block count will still be correct
285 */
286 for (; pfn < end_pfn; ) {
287 if (!pfn_valid(pfn)) {
288 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
289 continue;
290 }
291
292 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
293 block_end_pfn = min(block_end_pfn, end_pfn);
294
295 page = pfn_to_page(pfn);
296 pageblock_mt = get_pageblock_migratetype(page);
297
298 for (; pfn < block_end_pfn; pfn++) {
299 if (!pfn_valid_within(pfn))
300 continue;
301
302 page = pfn_to_page(pfn);
303
304 if (page_zone(page) != zone)
305 continue;
306
307 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700308 unsigned long freepage_order;
309
310 freepage_order = page_order_unsafe(page);
311 if (freepage_order < MAX_ORDER)
312 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700313 continue;
314 }
315
316 if (PageReserved(page))
317 continue;
318
319 page_ext = lookup_page_ext(page);
320 if (unlikely(!page_ext))
321 continue;
322
Vlastimil Babka373891672019-09-23 15:34:39 -0700323 if (!test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700324 continue;
325
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700326 page_owner = get_page_owner(page_ext);
327 page_mt = gfpflags_to_migratetype(
328 page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700329 if (pageblock_mt != page_mt) {
330 if (is_migrate_cma(pageblock_mt))
331 count[MIGRATE_MOVABLE]++;
332 else
333 count[pageblock_mt]++;
334
335 pfn = block_end_pfn;
336 break;
337 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700338 pfn += (1UL << page_owner->order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700339 }
340 }
341
342 /* Print counts */
343 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
344 for (i = 0; i < MIGRATE_TYPES; i++)
345 seq_printf(m, "%12lu ", count[i]);
346 seq_putc(m, '\n');
347}
348
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800349static ssize_t
350print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700351 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700352 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800353{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200354 int ret, pageblock_mt, page_mt;
355 unsigned long *entries;
356 unsigned int nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800357 char *kbuf;
358
Miles Chenc8f61cf2018-12-28 00:33:21 -0800359 count = min_t(size_t, count, PAGE_SIZE);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800360 kbuf = kmalloc(count, GFP_KERNEL);
361 if (!kbuf)
362 return -ENOMEM;
363
364 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700365 "Page allocated via order %u, mask %#x(%pGg)\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700366 page_owner->order, page_owner->gfp_mask,
367 &page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800368
369 if (ret >= count)
370 goto err;
371
372 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700373 pageblock_mt = get_pageblock_migratetype(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700374 page_mt = gfpflags_to_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800375 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700376 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800377 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700378 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800379 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700380 migratetype_names[pageblock_mt],
381 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800382
383 if (ret >= count)
384 goto err;
385
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200386 nr_entries = stack_depot_fetch(handle, &entries);
387 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800388 if (ret >= count)
389 goto err;
390
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700391 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700392 ret += snprintf(kbuf + ret, count - ret,
393 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700394 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700395 if (ret >= count)
396 goto err;
397 }
398
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800399 ret += snprintf(kbuf + ret, count - ret, "\n");
400 if (ret >= count)
401 goto err;
402
403 if (copy_to_user(buf, kbuf, ret))
404 ret = -EFAULT;
405
406 kfree(kbuf);
407 return ret;
408
409err:
410 kfree(kbuf);
411 return -ENOMEM;
412}
413
Vlastimil Babka4e462112016-03-15 14:56:21 -0700414void __dump_page_owner(struct page *page)
415{
416 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700417 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700418 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200419 unsigned long *entries;
420 unsigned int nr_entries;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700421 gfp_t gfp_mask;
422 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700423
Yang Shif86e4272016-06-03 14:55:38 -0700424 if (unlikely(!page_ext)) {
425 pr_alert("There is not page extension available.\n");
426 return;
427 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700428
429 page_owner = get_page_owner(page_ext);
430 gfp_mask = page_owner->gfp_mask;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700431 mt = gfpflags_to_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700432
Vlastimil Babka4e462112016-03-15 14:56:21 -0700433 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700434 pr_alert("page_owner info is not present (never set?)\n");
Vlastimil Babka4e462112016-03-15 14:56:21 -0700435 return;
436 }
437
Vlastimil Babka373891672019-09-23 15:34:39 -0700438 if (test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
439 pr_alert("page_owner tracks the page as allocated\n");
440 else
441 pr_alert("page_owner tracks the page as freed\n");
442
443 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
444 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask);
445
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700446 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700447 if (!handle) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700448 pr_alert("page_owner allocation stack trace missing\n");
449 } else {
450 nr_entries = stack_depot_fetch(handle, &entries);
451 stack_trace_print(entries, nr_entries, 0);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700452 }
453
Vlastimil Babka89745582019-09-23 15:34:42 -0700454#ifdef CONFIG_DEBUG_PAGEALLOC
455 handle = READ_ONCE(page_owner->free_handle);
456 if (!handle) {
457 pr_alert("page_owner free stack trace missing\n");
458 } else {
459 nr_entries = stack_depot_fetch(handle, &entries);
460 pr_alert("page last free stack trace:\n");
461 stack_trace_print(entries, nr_entries, 0);
462 }
463#endif
464
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700465 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700466 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700467 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700468}
469
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800470static ssize_t
471read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
472{
473 unsigned long pfn;
474 struct page *page;
475 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700476 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700477 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800478
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700479 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800480 return -EINVAL;
481
482 page = NULL;
483 pfn = min_low_pfn + *ppos;
484
485 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
486 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
487 pfn++;
488
489 drain_all_pages(NULL);
490
491 /* Find an allocated page */
492 for (; pfn < max_pfn; pfn++) {
493 /*
494 * If the new page is in a new MAX_ORDER_NR_PAGES area,
495 * validate the area as existing, skip it if not
496 */
497 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
498 pfn += MAX_ORDER_NR_PAGES - 1;
499 continue;
500 }
501
502 /* Check for holes within a MAX_ORDER area */
503 if (!pfn_valid_within(pfn))
504 continue;
505
506 page = pfn_to_page(pfn);
507 if (PageBuddy(page)) {
508 unsigned long freepage_order = page_order_unsafe(page);
509
510 if (freepage_order < MAX_ORDER)
511 pfn += (1UL << freepage_order) - 1;
512 continue;
513 }
514
515 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700516 if (unlikely(!page_ext))
517 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800518
519 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800520 * Some pages could be missed by concurrent allocation or free,
521 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800522 */
523 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
524 continue;
525
Vlastimil Babka373891672019-09-23 15:34:39 -0700526 /*
527 * Although we do have the info about past allocation of free
528 * pages, it's not relevant for current memory usage.
529 */
530 if (!test_bit(PAGE_EXT_OWNER_ACTIVE, &page_ext->flags))
531 continue;
532
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700533 page_owner = get_page_owner(page_ext);
534
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700535 /*
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700536 * Don't print "tail" pages of high-order allocations as that
537 * would inflate the stats.
538 */
539 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
540 continue;
541
542 /*
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700543 * Access to page_ext->handle isn't synchronous so we should
544 * be careful to access it.
545 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700546 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700547 if (!handle)
548 continue;
549
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800550 /* Record the next PFN to read in the file offset */
551 *ppos = (pfn - min_low_pfn) + 1;
552
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700553 return print_page_owner(buf, count, pfn, page,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700554 page_owner, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800555 }
556
557 return 0;
558}
559
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800560static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
561{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800562 unsigned long pfn = zone->zone_start_pfn;
563 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800564 unsigned long count = 0;
565
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800566 /*
567 * Walk the zone in pageblock_nr_pages steps. If a page block spans
568 * a zone boundary, it will be double counted between zones. This does
569 * not matter as the mixed block count will still be correct
570 */
571 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800572 unsigned long block_end_pfn;
573
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800574 if (!pfn_valid(pfn)) {
575 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
576 continue;
577 }
578
579 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
580 block_end_pfn = min(block_end_pfn, end_pfn);
581
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800582 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800583 struct page *page;
584 struct page_ext *page_ext;
585
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800586 if (!pfn_valid_within(pfn))
587 continue;
588
589 page = pfn_to_page(pfn);
590
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700591 if (page_zone(page) != zone)
592 continue;
593
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800594 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700595 * To avoid having to grab zone->lock, be a little
596 * careful when reading buddy page order. The only
597 * danger is that we skip too much and potentially miss
598 * some early allocated pages, which is better than
599 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800600 */
601 if (PageBuddy(page)) {
Vlastimil Babka10903022017-09-06 16:20:51 -0700602 unsigned long order = page_order_unsafe(page);
603
604 if (order > 0 && order < MAX_ORDER)
605 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800606 continue;
607 }
608
609 if (PageReserved(page))
610 continue;
611
612 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700613 if (unlikely(!page_ext))
614 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800615
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700616 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800617 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
618 continue;
619
620 /* Found early allocated page */
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700621 __set_page_owner_handle(page, page_ext, early_handle,
622 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800623 count++;
624 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700625 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800626 }
627
628 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
629 pgdat->node_id, zone->name, count);
630}
631
632static void init_zones_in_node(pg_data_t *pgdat)
633{
634 struct zone *zone;
635 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800636
637 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
638 if (!populated_zone(zone))
639 continue;
640
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800641 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800642 }
643}
644
645static void init_early_allocated_pages(void)
646{
647 pg_data_t *pgdat;
648
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800649 for_each_online_pgdat(pgdat)
650 init_zones_in_node(pgdat);
651}
652
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800653static const struct file_operations proc_page_owner_operations = {
654 .read = read_page_owner,
655};
656
657static int __init pageowner_init(void)
658{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700659 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800660 pr_info("page_owner is disabled\n");
661 return 0;
662 }
663
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800664 debugfs_create_file("page_owner", 0400, NULL, NULL,
665 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800666
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800667 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800668}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400669late_initcall(pageowner_init)