blob: df277e6bc3c6a6c69f598929f623556833a3b4ef [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;
27};
28
Joonsoo Kim48c96a32014-12-12 16:56:01 -080029static bool page_owner_disabled = true;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070030DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080031
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070032static depot_stack_handle_t dummy_handle;
33static depot_stack_handle_t failure_handle;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070034static depot_stack_handle_t early_handle;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070035
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080036static void init_early_allocated_pages(void);
37
Dou Liyang11731942018-04-05 16:23:49 -070038static int __init early_page_owner_param(char *buf)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080039{
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
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070058static __always_inline depot_stack_handle_t create_dummy_stack(void)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070059{
60 unsigned long entries[4];
61 struct stack_trace dummy;
62
63 dummy.nr_entries = 0;
64 dummy.max_entries = ARRAY_SIZE(entries);
65 dummy.entries = &entries[0];
66 dummy.skip = 0;
67
68 save_stack_trace(&dummy);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070069 return depot_save_stack(&dummy, GFP_KERNEL);
70}
71
72static noinline void register_dummy_stack(void)
73{
74 dummy_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070075}
76
77static noinline void register_failure_stack(void)
78{
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070079 failure_handle = create_dummy_stack();
80}
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070081
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070082static noinline void register_early_stack(void)
83{
84 early_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070085}
86
Joonsoo Kim48c96a32014-12-12 16:56:01 -080087static void init_page_owner(void)
88{
89 if (page_owner_disabled)
90 return;
91
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070092 register_dummy_stack();
93 register_failure_stack();
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070094 register_early_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070095 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080096 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080097}
98
99struct page_ext_operations page_owner_ops = {
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700100 .size = sizeof(struct page_owner),
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800101 .need = need_page_owner,
102 .init = init_page_owner,
103};
104
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700105static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
106{
107 return (void *)page_ext + page_owner_ops.offset;
108}
109
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800110void __reset_page_owner(struct page *page, unsigned int order)
111{
112 int i;
113 struct page_ext *page_ext;
114
115 for (i = 0; i < (1 << order); i++) {
116 page_ext = lookup_page_ext(page + i);
Yang Shif86e4272016-06-03 14:55:38 -0700117 if (unlikely(!page_ext))
118 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800119 __clear_bit(PAGE_EXT_OWNER, &page_ext->flags);
120 }
121}
122
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700123static inline bool check_recursive_alloc(struct stack_trace *trace,
124 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800125{
Maninder Singh299815a2018-03-28 16:01:05 -0700126 int i;
Yang Shif86e4272016-06-03 14:55:38 -0700127
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700128 if (!trace->nr_entries)
129 return false;
130
Maninder Singh299815a2018-03-28 16:01:05 -0700131 for (i = 0; i < trace->nr_entries; i++) {
132 if (trace->entries[i] == ip)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700133 return true;
134 }
135
136 return false;
137}
138
139static noinline depot_stack_handle_t save_stack(gfp_t flags)
140{
141 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800142 struct stack_trace trace = {
143 .nr_entries = 0,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700144 .entries = entries,
145 .max_entries = PAGE_OWNER_STACK_DEPTH,
Prakash Gupta5f48f0b2017-09-13 16:28:35 -0700146 .skip = 2
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800147 };
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700148 depot_stack_handle_t handle;
149
150 save_stack_trace(&trace);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700151
152 /*
153 * We need to check recursion here because our request to stackdepot
154 * could trigger memory allocation to save new entry. New memory
155 * allocation would reach here and call depot_save_stack() again
156 * if we don't catch it. There is still not enough memory in stackdepot
157 * so it would try to allocate memory again and loop forever.
158 */
159 if (check_recursive_alloc(&trace, _RET_IP_))
160 return dummy_handle;
161
162 handle = depot_save_stack(&trace, flags);
163 if (!handle)
164 handle = failure_handle;
165
166 return handle;
167}
168
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700169static inline void __set_page_owner_handle(struct page_ext *page_ext,
170 depot_stack_handle_t handle, unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700171{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700172 struct page_owner *page_owner;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800173
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700174 page_owner = get_page_owner(page_ext);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700175 page_owner->handle = handle;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700176 page_owner->order = order;
177 page_owner->gfp_mask = gfp_mask;
178 page_owner->last_migrate_reason = -1;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800179
180 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
181}
182
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700183noinline void __set_page_owner(struct page *page, unsigned int order,
184 gfp_t gfp_mask)
185{
186 struct page_ext *page_ext = lookup_page_ext(page);
187 depot_stack_handle_t handle;
188
189 if (unlikely(!page_ext))
190 return;
191
192 handle = save_stack(gfp_mask);
193 __set_page_owner_handle(page_ext, handle, order, gfp_mask);
194}
195
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700196void __set_page_owner_migrate_reason(struct page *page, int reason)
197{
198 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700199 struct page_owner *page_owner;
200
Yang Shif86e4272016-06-03 14:55:38 -0700201 if (unlikely(!page_ext))
202 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700203
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700204 page_owner = get_page_owner(page_ext);
205 page_owner->last_migrate_reason = reason;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700206}
207
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700208void __split_page_owner(struct page *page, unsigned int order)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700209{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700210 int i;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700211 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700212 struct page_owner *page_owner;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700213
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700214 if (unlikely(!page_ext))
215 return;
216
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700217 page_owner = get_page_owner(page_ext);
218 page_owner->order = 0;
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700219 for (i = 1; i < (1 << order); i++)
220 __copy_page_owner(page, page + i);
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700221}
222
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700223void __copy_page_owner(struct page *oldpage, struct page *newpage)
224{
225 struct page_ext *old_ext = lookup_page_ext(oldpage);
226 struct page_ext *new_ext = lookup_page_ext(newpage);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700227 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700228
Yang Shif86e4272016-06-03 14:55:38 -0700229 if (unlikely(!old_ext || !new_ext))
230 return;
231
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700232 old_page_owner = get_page_owner(old_ext);
233 new_page_owner = get_page_owner(new_ext);
234 new_page_owner->order = old_page_owner->order;
235 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
236 new_page_owner->last_migrate_reason =
237 old_page_owner->last_migrate_reason;
238 new_page_owner->handle = old_page_owner->handle;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700239
240 /*
241 * We don't clear the bit on the oldpage as it's going to be freed
242 * after migration. Until then, the info can be useful in case of
243 * a bug, and the overal stats will be off a bit only temporarily.
244 * Also, migrate_misplaced_transhuge_page() can still fail the
245 * migration and then we want the oldpage to retain the info. But
246 * in that case we also don't need to explicitly clear the info from
247 * the new page, which will be freed.
248 */
249 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
250}
251
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700252void pagetypeinfo_showmixedcount_print(struct seq_file *m,
253 pg_data_t *pgdat, struct zone *zone)
254{
255 struct page *page;
256 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700257 struct page_owner *page_owner;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700258 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
259 unsigned long end_pfn = pfn + zone->spanned_pages;
260 unsigned long count[MIGRATE_TYPES] = { 0, };
261 int pageblock_mt, page_mt;
262 int i;
263
264 /* Scan block by block. First and last block may be incomplete */
265 pfn = zone->zone_start_pfn;
266
267 /*
268 * Walk the zone in pageblock_nr_pages steps. If a page block spans
269 * a zone boundary, it will be double counted between zones. This does
270 * not matter as the mixed block count will still be correct
271 */
272 for (; pfn < end_pfn; ) {
273 if (!pfn_valid(pfn)) {
274 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
275 continue;
276 }
277
278 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
279 block_end_pfn = min(block_end_pfn, end_pfn);
280
281 page = pfn_to_page(pfn);
282 pageblock_mt = get_pageblock_migratetype(page);
283
284 for (; pfn < block_end_pfn; pfn++) {
285 if (!pfn_valid_within(pfn))
286 continue;
287
288 page = pfn_to_page(pfn);
289
290 if (page_zone(page) != zone)
291 continue;
292
293 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700294 unsigned long freepage_order;
295
296 freepage_order = page_order_unsafe(page);
297 if (freepage_order < MAX_ORDER)
298 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700299 continue;
300 }
301
302 if (PageReserved(page))
303 continue;
304
305 page_ext = lookup_page_ext(page);
306 if (unlikely(!page_ext))
307 continue;
308
309 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
310 continue;
311
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700312 page_owner = get_page_owner(page_ext);
313 page_mt = gfpflags_to_migratetype(
314 page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700315 if (pageblock_mt != page_mt) {
316 if (is_migrate_cma(pageblock_mt))
317 count[MIGRATE_MOVABLE]++;
318 else
319 count[pageblock_mt]++;
320
321 pfn = block_end_pfn;
322 break;
323 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700324 pfn += (1UL << page_owner->order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700325 }
326 }
327
328 /* Print counts */
329 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
330 for (i = 0; i < MIGRATE_TYPES; i++)
331 seq_printf(m, "%12lu ", count[i]);
332 seq_putc(m, '\n');
333}
334
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800335static ssize_t
336print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700337 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700338 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800339{
340 int ret;
341 int pageblock_mt, page_mt;
342 char *kbuf;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700343 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800344 struct stack_trace trace = {
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700345 .nr_entries = 0,
346 .entries = entries,
347 .max_entries = PAGE_OWNER_STACK_DEPTH,
348 .skip = 0
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800349 };
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800350
Miles Chenc8f61cf2018-12-28 00:33:21 -0800351 count = min_t(size_t, count, PAGE_SIZE);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800352 kbuf = kmalloc(count, GFP_KERNEL);
353 if (!kbuf)
354 return -ENOMEM;
355
356 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700357 "Page allocated via order %u, mask %#x(%pGg)\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700358 page_owner->order, page_owner->gfp_mask,
359 &page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800360
361 if (ret >= count)
362 goto err;
363
364 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700365 pageblock_mt = get_pageblock_migratetype(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700366 page_mt = gfpflags_to_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800367 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700368 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800369 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700370 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800371 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700372 migratetype_names[pageblock_mt],
373 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800374
375 if (ret >= count)
376 goto err;
377
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700378 depot_fetch_stack(handle, &trace);
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800379 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800380 if (ret >= count)
381 goto err;
382
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700383 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700384 ret += snprintf(kbuf + ret, count - ret,
385 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700386 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700387 if (ret >= count)
388 goto err;
389 }
390
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800391 ret += snprintf(kbuf + ret, count - ret, "\n");
392 if (ret >= count)
393 goto err;
394
395 if (copy_to_user(buf, kbuf, ret))
396 ret = -EFAULT;
397
398 kfree(kbuf);
399 return ret;
400
401err:
402 kfree(kbuf);
403 return -ENOMEM;
404}
405
Vlastimil Babka4e462112016-03-15 14:56:21 -0700406void __dump_page_owner(struct page *page)
407{
408 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700409 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700410 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Vlastimil Babka4e462112016-03-15 14:56:21 -0700411 struct stack_trace trace = {
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700412 .nr_entries = 0,
413 .entries = entries,
414 .max_entries = PAGE_OWNER_STACK_DEPTH,
415 .skip = 0
Vlastimil Babka4e462112016-03-15 14:56:21 -0700416 };
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700417 depot_stack_handle_t handle;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700418 gfp_t gfp_mask;
419 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700420
Yang Shif86e4272016-06-03 14:55:38 -0700421 if (unlikely(!page_ext)) {
422 pr_alert("There is not page extension available.\n");
423 return;
424 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700425
426 page_owner = get_page_owner(page_ext);
427 gfp_mask = page_owner->gfp_mask;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700428 mt = gfpflags_to_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700429
Vlastimil Babka4e462112016-03-15 14:56:21 -0700430 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
431 pr_alert("page_owner info is not active (free page?)\n");
432 return;
433 }
434
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700435 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700436 if (!handle) {
437 pr_alert("page_owner info is not active (free page?)\n");
438 return;
439 }
440
441 depot_fetch_stack(handle, &trace);
Joe Perches756a0252016-03-17 14:19:47 -0700442 pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700443 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700444 print_stack_trace(&trace, 0);
445
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700446 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700447 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700448 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700449}
450
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800451static ssize_t
452read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
453{
454 unsigned long pfn;
455 struct page *page;
456 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700457 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700458 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800459
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700460 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800461 return -EINVAL;
462
463 page = NULL;
464 pfn = min_low_pfn + *ppos;
465
466 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
467 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
468 pfn++;
469
470 drain_all_pages(NULL);
471
472 /* Find an allocated page */
473 for (; pfn < max_pfn; pfn++) {
474 /*
475 * If the new page is in a new MAX_ORDER_NR_PAGES area,
476 * validate the area as existing, skip it if not
477 */
478 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
479 pfn += MAX_ORDER_NR_PAGES - 1;
480 continue;
481 }
482
483 /* Check for holes within a MAX_ORDER area */
484 if (!pfn_valid_within(pfn))
485 continue;
486
487 page = pfn_to_page(pfn);
488 if (PageBuddy(page)) {
489 unsigned long freepage_order = page_order_unsafe(page);
490
491 if (freepage_order < MAX_ORDER)
492 pfn += (1UL << freepage_order) - 1;
493 continue;
494 }
495
496 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700497 if (unlikely(!page_ext))
498 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800499
500 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800501 * Some pages could be missed by concurrent allocation or free,
502 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800503 */
504 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
505 continue;
506
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700507 page_owner = get_page_owner(page_ext);
508
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700509 /*
510 * Access to page_ext->handle isn't synchronous so we should
511 * be careful to access it.
512 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700513 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700514 if (!handle)
515 continue;
516
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800517 /* Record the next PFN to read in the file offset */
518 *ppos = (pfn - min_low_pfn) + 1;
519
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700520 return print_page_owner(buf, count, pfn, page,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700521 page_owner, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800522 }
523
524 return 0;
525}
526
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800527static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
528{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800529 unsigned long pfn = zone->zone_start_pfn;
530 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800531 unsigned long count = 0;
532
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800533 /*
534 * Walk the zone in pageblock_nr_pages steps. If a page block spans
535 * a zone boundary, it will be double counted between zones. This does
536 * not matter as the mixed block count will still be correct
537 */
538 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800539 unsigned long block_end_pfn;
540
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800541 if (!pfn_valid(pfn)) {
542 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
543 continue;
544 }
545
546 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
547 block_end_pfn = min(block_end_pfn, end_pfn);
548
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800549 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800550 struct page *page;
551 struct page_ext *page_ext;
552
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800553 if (!pfn_valid_within(pfn))
554 continue;
555
556 page = pfn_to_page(pfn);
557
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700558 if (page_zone(page) != zone)
559 continue;
560
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800561 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700562 * To avoid having to grab zone->lock, be a little
563 * careful when reading buddy page order. The only
564 * danger is that we skip too much and potentially miss
565 * some early allocated pages, which is better than
566 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800567 */
568 if (PageBuddy(page)) {
Vlastimil Babka10903022017-09-06 16:20:51 -0700569 unsigned long order = page_order_unsafe(page);
570
571 if (order > 0 && order < MAX_ORDER)
572 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800573 continue;
574 }
575
576 if (PageReserved(page))
577 continue;
578
579 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700580 if (unlikely(!page_ext))
581 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800582
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700583 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800584 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
585 continue;
586
587 /* Found early allocated page */
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700588 __set_page_owner_handle(page_ext, early_handle, 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800589 count++;
590 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700591 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800592 }
593
594 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
595 pgdat->node_id, zone->name, count);
596}
597
598static void init_zones_in_node(pg_data_t *pgdat)
599{
600 struct zone *zone;
601 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800602
603 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
604 if (!populated_zone(zone))
605 continue;
606
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800607 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800608 }
609}
610
611static void init_early_allocated_pages(void)
612{
613 pg_data_t *pgdat;
614
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800615 for_each_online_pgdat(pgdat)
616 init_zones_in_node(pgdat);
617}
618
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800619static const struct file_operations proc_page_owner_operations = {
620 .read = read_page_owner,
621};
622
623static int __init pageowner_init(void)
624{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700625 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800626 pr_info("page_owner is disabled\n");
627 return 0;
628 }
629
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800630 debugfs_create_file("page_owner", 0400, NULL, NULL,
631 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800632
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800633 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800634}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400635late_initcall(pageowner_init)