blob: 49a77e018871ccd4a56baf10d1bf66d9fa0da08c [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 depot_stack_handle_t free_handle;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070028};
29
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070030static bool page_owner_enabled = false;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070031DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080032
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070033static depot_stack_handle_t dummy_handle;
34static depot_stack_handle_t failure_handle;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070035static depot_stack_handle_t early_handle;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070036
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080037static void init_early_allocated_pages(void);
38
Dou Liyang11731942018-04-05 16:23:49 -070039static int __init early_page_owner_param(char *buf)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080040{
41 if (!buf)
42 return -EINVAL;
43
44 if (strcmp(buf, "on") == 0)
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070045 page_owner_enabled = true;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080046
47 return 0;
48}
49early_param("page_owner", early_page_owner_param);
50
51static bool need_page_owner(void)
52{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070053 return page_owner_enabled;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080054}
55
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070056static __always_inline depot_stack_handle_t create_dummy_stack(void)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070057{
58 unsigned long entries[4];
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020059 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070060
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020061 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
62 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070063}
64
65static noinline void register_dummy_stack(void)
66{
67 dummy_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070068}
69
70static noinline void register_failure_stack(void)
71{
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070072 failure_handle = create_dummy_stack();
73}
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070074
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070075static noinline void register_early_stack(void)
76{
77 early_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070078}
79
Joonsoo Kim48c96a32014-12-12 16:56:01 -080080static void init_page_owner(void)
81{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070082 if (!page_owner_enabled)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080083 return;
84
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070085 register_dummy_stack();
86 register_failure_stack();
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070087 register_early_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070088 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080089 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080090}
91
92struct page_ext_operations page_owner_ops = {
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070093 .size = sizeof(struct page_owner),
Joonsoo Kim48c96a32014-12-12 16:56:01 -080094 .need = need_page_owner,
95 .init = init_page_owner,
96};
97
Vijayanand Jittab76264c2021-01-05 11:33:53 +053098struct page_owner *get_page_owner(struct page_ext *page_ext)
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070099{
100 return (void *)page_ext + page_owner_ops.offset;
101}
Vijayanand Jittab76264c2021-01-05 11:33:53 +0530102EXPORT_SYMBOL_GPL(get_page_owner);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700103
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200104static inline bool check_recursive_alloc(unsigned long *entries,
105 unsigned int nr_entries,
106 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800107{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200108 unsigned int i;
Yang Shif86e4272016-06-03 14:55:38 -0700109
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200110 for (i = 0; i < nr_entries; i++) {
111 if (entries[i] == ip)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700112 return true;
113 }
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700114 return false;
115}
116
117static noinline depot_stack_handle_t save_stack(gfp_t flags)
118{
119 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700120 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200121 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700122
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200123 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700124
125 /*
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200126 * We need to check recursion here because our request to
127 * stackdepot could trigger memory allocation to save new
128 * entry. New memory allocation would reach here and call
129 * stack_depot_save_entries() again if we don't catch it. There is
130 * still not enough memory in stackdepot so it would try to
131 * allocate memory again and loop forever.
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700132 */
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200133 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700134 return dummy_handle;
135
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200136 handle = stack_depot_save(entries, nr_entries, flags);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700137 if (!handle)
138 handle = failure_handle;
139
140 return handle;
141}
142
Vlastimil Babka89745582019-09-23 15:34:42 -0700143void __reset_page_owner(struct page *page, unsigned int order)
144{
145 int i;
146 struct page_ext *page_ext;
Vlastimil Babka89745582019-09-23 15:34:42 -0700147 depot_stack_handle_t handle = 0;
148 struct page_owner *page_owner;
149
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700150 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
Vlastimil Babka89745582019-09-23 15:34:42 -0700151
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700152 page_ext = lookup_page_ext(page);
153 if (unlikely(!page_ext))
154 return;
Vlastimil Babka89745582019-09-23 15:34:42 -0700155 for (i = 0; i < (1 << order); i++) {
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700156 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700157 page_owner = get_page_owner(page_ext);
158 page_owner->free_handle = handle;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700159 page_ext = page_ext_next(page_ext);
Vlastimil Babka89745582019-09-23 15:34:42 -0700160 }
161}
162
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700163static inline void __set_page_owner_handle(struct page *page,
164 struct page_ext *page_ext, depot_stack_handle_t handle,
165 unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700166{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700167 struct page_owner *page_owner;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700168 int i;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800169
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700170 for (i = 0; i < (1 << order); i++) {
171 page_owner = get_page_owner(page_ext);
172 page_owner->handle = handle;
173 page_owner->order = order;
174 page_owner->gfp_mask = gfp_mask;
175 page_owner->last_migrate_reason = -1;
176 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700177 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800178
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700179 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700180 }
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800181}
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);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700193 __set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700194}
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
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700208void __split_page_owner(struct page *page, unsigned int nr)
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
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700217 for (i = 0; i < nr; i++) {
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700218 page_owner = get_page_owner(page_ext);
219 page_owner->order = 0;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700220 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700221 }
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700222}
223
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700224void __copy_page_owner(struct page *oldpage, struct page *newpage)
225{
226 struct page_ext *old_ext = lookup_page_ext(oldpage);
227 struct page_ext *new_ext = lookup_page_ext(newpage);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700228 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700229
Yang Shif86e4272016-06-03 14:55:38 -0700230 if (unlikely(!old_ext || !new_ext))
231 return;
232
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700233 old_page_owner = get_page_owner(old_ext);
234 new_page_owner = get_page_owner(new_ext);
235 new_page_owner->order = old_page_owner->order;
236 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
237 new_page_owner->last_migrate_reason =
238 old_page_owner->last_migrate_reason;
239 new_page_owner->handle = old_page_owner->handle;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700240
241 /*
242 * We don't clear the bit on the oldpage as it's going to be freed
243 * after migration. Until then, the info can be useful in case of
244 * a bug, and the overal stats will be off a bit only temporarily.
245 * Also, migrate_misplaced_transhuge_page() can still fail the
246 * migration and then we want the oldpage to retain the info. But
247 * in that case we also don't need to explicitly clear the info from
248 * the new page, which will be freed.
249 */
250 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700251 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700252}
253
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700254void pagetypeinfo_showmixedcount_print(struct seq_file *m,
255 pg_data_t *pgdat, struct zone *zone)
256{
257 struct page *page;
258 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700259 struct page_owner *page_owner;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700260 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
261 unsigned long end_pfn = pfn + zone->spanned_pages;
262 unsigned long count[MIGRATE_TYPES] = { 0, };
263 int pageblock_mt, page_mt;
264 int i;
265
266 /* Scan block by block. First and last block may be incomplete */
267 pfn = zone->zone_start_pfn;
268
269 /*
270 * Walk the zone in pageblock_nr_pages steps. If a page block spans
271 * a zone boundary, it will be double counted between zones. This does
272 * not matter as the mixed block count will still be correct
273 */
274 for (; pfn < end_pfn; ) {
Qian Caia26ee562019-10-18 20:19:29 -0700275 page = pfn_to_online_page(pfn);
276 if (!page) {
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700277 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
278 continue;
279 }
280
281 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
282 block_end_pfn = min(block_end_pfn, end_pfn);
283
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700284 pageblock_mt = get_pageblock_migratetype(page);
285
286 for (; pfn < block_end_pfn; pfn++) {
287 if (!pfn_valid_within(pfn))
288 continue;
289
Qian Caia26ee562019-10-18 20:19:29 -0700290 /* The pageblock is online, no need to recheck. */
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700291 page = pfn_to_page(pfn);
292
293 if (page_zone(page) != zone)
294 continue;
295
296 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700297 unsigned long freepage_order;
298
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700299 freepage_order = buddy_order_unsafe(page);
Vinayak Menon727c0802017-07-10 15:49:17 -0700300 if (freepage_order < MAX_ORDER)
301 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700302 continue;
303 }
304
305 if (PageReserved(page))
306 continue;
307
308 page_ext = lookup_page_ext(page);
309 if (unlikely(!page_ext))
310 continue;
311
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700312 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700313 continue;
314
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700315 page_owner = get_page_owner(page_ext);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700316 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700317 if (pageblock_mt != page_mt) {
318 if (is_migrate_cma(pageblock_mt))
319 count[MIGRATE_MOVABLE]++;
320 else
321 count[pageblock_mt]++;
322
323 pfn = block_end_pfn;
324 break;
325 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700326 pfn += (1UL << page_owner->order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700327 }
328 }
329
330 /* Print counts */
331 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
332 for (i = 0; i < MIGRATE_TYPES; i++)
333 seq_printf(m, "%12lu ", count[i]);
334 seq_putc(m, '\n');
335}
336
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800337static ssize_t
338print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700339 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700340 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800341{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200342 int ret, pageblock_mt, page_mt;
343 unsigned long *entries;
344 unsigned int nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800345 char *kbuf;
346
Miles Chenc8f61cf2018-12-28 00:33:21 -0800347 count = min_t(size_t, count, PAGE_SIZE);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800348 kbuf = kmalloc(count, GFP_KERNEL);
349 if (!kbuf)
350 return -ENOMEM;
351
352 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700353 "Page allocated via order %u, mask %#x(%pGg)\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700354 page_owner->order, page_owner->gfp_mask,
355 &page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800356
357 if (ret >= count)
358 goto err;
359
360 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700361 pageblock_mt = get_pageblock_migratetype(page);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700362 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800363 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700364 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800365 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700366 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800367 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700368 migratetype_names[pageblock_mt],
369 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800370
371 if (ret >= count)
372 goto err;
373
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200374 nr_entries = stack_depot_fetch(handle, &entries);
375 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800376 if (ret >= count)
377 goto err;
378
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700379 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700380 ret += snprintf(kbuf + ret, count - ret,
381 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700382 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700383 if (ret >= count)
384 goto err;
385 }
386
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800387 ret += snprintf(kbuf + ret, count - ret, "\n");
388 if (ret >= count)
389 goto err;
390
391 if (copy_to_user(buf, kbuf, ret))
392 ret = -EFAULT;
393
394 kfree(kbuf);
395 return ret;
396
397err:
398 kfree(kbuf);
399 return -ENOMEM;
400}
401
Vlastimil Babka4e462112016-03-15 14:56:21 -0700402void __dump_page_owner(struct page *page)
403{
404 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700405 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700406 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200407 unsigned long *entries;
408 unsigned int nr_entries;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700409 gfp_t gfp_mask;
410 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700411
Yang Shif86e4272016-06-03 14:55:38 -0700412 if (unlikely(!page_ext)) {
413 pr_alert("There is not page extension available.\n");
414 return;
415 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700416
417 page_owner = get_page_owner(page_ext);
418 gfp_mask = page_owner->gfp_mask;
Wei Yang01c0bfe2020-06-03 15:59:08 -0700419 mt = gfp_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700420
Vlastimil Babka4e462112016-03-15 14:56:21 -0700421 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700422 pr_alert("page_owner info is not present (never set?)\n");
Vlastimil Babka4e462112016-03-15 14:56:21 -0700423 return;
424 }
425
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700426 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700427 pr_alert("page_owner tracks the page as allocated\n");
428 else
429 pr_alert("page_owner tracks the page as freed\n");
430
431 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
432 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask);
433
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700434 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700435 if (!handle) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700436 pr_alert("page_owner allocation stack trace missing\n");
437 } else {
438 nr_entries = stack_depot_fetch(handle, &entries);
439 stack_trace_print(entries, nr_entries, 0);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700440 }
441
Vlastimil Babka89745582019-09-23 15:34:42 -0700442 handle = READ_ONCE(page_owner->free_handle);
443 if (!handle) {
444 pr_alert("page_owner free stack trace missing\n");
445 } else {
446 nr_entries = stack_depot_fetch(handle, &entries);
447 pr_alert("page last free stack trace:\n");
448 stack_trace_print(entries, nr_entries, 0);
449 }
Vlastimil Babka89745582019-09-23 15:34:42 -0700450
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700451 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700452 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700453 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700454}
455
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800456static ssize_t
457read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
458{
459 unsigned long pfn;
460 struct page *page;
461 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700462 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700463 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800464
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700465 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800466 return -EINVAL;
467
468 page = NULL;
469 pfn = min_low_pfn + *ppos;
470
471 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
472 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
473 pfn++;
474
475 drain_all_pages(NULL);
476
477 /* Find an allocated page */
478 for (; pfn < max_pfn; pfn++) {
479 /*
480 * If the new page is in a new MAX_ORDER_NR_PAGES area,
481 * validate the area as existing, skip it if not
482 */
483 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
484 pfn += MAX_ORDER_NR_PAGES - 1;
485 continue;
486 }
487
488 /* Check for holes within a MAX_ORDER area */
489 if (!pfn_valid_within(pfn))
490 continue;
491
492 page = pfn_to_page(pfn);
493 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700494 unsigned long freepage_order = buddy_order_unsafe(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800495
496 if (freepage_order < MAX_ORDER)
497 pfn += (1UL << freepage_order) - 1;
498 continue;
499 }
500
501 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700502 if (unlikely(!page_ext))
503 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800504
505 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800506 * Some pages could be missed by concurrent allocation or free,
507 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800508 */
509 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
510 continue;
511
Vlastimil Babka373891672019-09-23 15:34:39 -0700512 /*
513 * Although we do have the info about past allocation of free
514 * pages, it's not relevant for current memory usage.
515 */
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700516 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700517 continue;
518
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700519 page_owner = get_page_owner(page_ext);
520
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700521 /*
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700522 * Don't print "tail" pages of high-order allocations as that
523 * would inflate the stats.
524 */
525 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
526 continue;
527
528 /*
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700529 * Access to page_ext->handle isn't synchronous so we should
530 * be careful to access it.
531 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700532 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700533 if (!handle)
534 continue;
535
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800536 /* Record the next PFN to read in the file offset */
537 *ppos = (pfn - min_low_pfn) + 1;
538
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700539 return print_page_owner(buf, count, pfn, page,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700540 page_owner, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800541 }
542
543 return 0;
544}
545
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800546static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
547{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800548 unsigned long pfn = zone->zone_start_pfn;
549 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800550 unsigned long count = 0;
551
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800552 /*
553 * Walk the zone in pageblock_nr_pages steps. If a page block spans
554 * a zone boundary, it will be double counted between zones. This does
555 * not matter as the mixed block count will still be correct
556 */
557 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800558 unsigned long block_end_pfn;
559
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800560 if (!pfn_valid(pfn)) {
561 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
562 continue;
563 }
564
565 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
566 block_end_pfn = min(block_end_pfn, end_pfn);
567
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800568 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800569 struct page *page;
570 struct page_ext *page_ext;
571
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800572 if (!pfn_valid_within(pfn))
573 continue;
574
575 page = pfn_to_page(pfn);
576
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700577 if (page_zone(page) != zone)
578 continue;
579
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800580 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700581 * To avoid having to grab zone->lock, be a little
582 * careful when reading buddy page order. The only
583 * danger is that we skip too much and potentially miss
584 * some early allocated pages, which is better than
585 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800586 */
587 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700588 unsigned long order = buddy_order_unsafe(page);
Vlastimil Babka10903022017-09-06 16:20:51 -0700589
590 if (order > 0 && order < MAX_ORDER)
591 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800592 continue;
593 }
594
595 if (PageReserved(page))
596 continue;
597
598 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700599 if (unlikely(!page_ext))
600 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800601
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700602 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800603 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
604 continue;
605
606 /* Found early allocated page */
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700607 __set_page_owner_handle(page, page_ext, early_handle,
608 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800609 count++;
610 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700611 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800612 }
613
614 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
615 pgdat->node_id, zone->name, count);
616}
617
618static void init_zones_in_node(pg_data_t *pgdat)
619{
620 struct zone *zone;
621 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800622
623 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
624 if (!populated_zone(zone))
625 continue;
626
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800627 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800628 }
629}
630
631static void init_early_allocated_pages(void)
632{
633 pg_data_t *pgdat;
634
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800635 for_each_online_pgdat(pgdat)
636 init_zones_in_node(pgdat);
637}
638
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800639static const struct file_operations proc_page_owner_operations = {
640 .read = read_page_owner,
641};
642
643static int __init pageowner_init(void)
644{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700645 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800646 pr_info("page_owner is disabled\n");
647 return 0;
648 }
649
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800650 debugfs_create_file("page_owner", 0400, NULL, NULL,
651 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800652
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800653 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800654}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400655late_initcall(pageowner_init)