blob: 5c941ca9861d123f2b33cd6a8b646c0945201a24 [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>
Liam Mark9cc7e96a2020-12-14 19:04:49 -080013#include <linux/sched/clock.h>
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070014
Joonsoo Kim48c96a32014-12-12 16:56:01 -080015#include "internal.h"
16
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070017/*
18 * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
19 * to use off stack temporal storage
20 */
21#define PAGE_OWNER_STACK_DEPTH (16)
22
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070023struct page_owner {
Ayush Mittal6b4c54e2017-11-15 17:34:30 -080024 unsigned short order;
25 short last_migrate_reason;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070026 gfp_t gfp_mask;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070027 depot_stack_handle_t handle;
Vlastimil Babka89745582019-09-23 15:34:42 -070028 depot_stack_handle_t free_handle;
Liam Mark9cc7e96a2020-12-14 19:04:49 -080029 u64 ts_nsec;
Georgi Djakov866b4852021-04-29 22:54:57 -070030 u64 free_ts_nsec;
Liam Mark9cc7e96a2020-12-14 19:04:49 -080031 pid_t pid;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070032};
33
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070034static bool page_owner_enabled = false;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070035DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080036
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070037static depot_stack_handle_t dummy_handle;
38static depot_stack_handle_t failure_handle;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070039static depot_stack_handle_t early_handle;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070040
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080041static void init_early_allocated_pages(void);
42
Dou Liyang11731942018-04-05 16:23:49 -070043static int __init early_page_owner_param(char *buf)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080044{
Sergei Trofimovich608b5d62021-04-29 22:55:05 -070045 return kstrtobool(buf, &page_owner_enabled);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080046}
47early_param("page_owner", early_page_owner_param);
48
49static bool need_page_owner(void)
50{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070051 return page_owner_enabled;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080052}
53
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070054static __always_inline depot_stack_handle_t create_dummy_stack(void)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070055{
56 unsigned long entries[4];
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020057 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070058
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020059 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
60 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070061}
62
63static noinline void register_dummy_stack(void)
64{
65 dummy_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070066}
67
68static noinline void register_failure_stack(void)
69{
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070070 failure_handle = create_dummy_stack();
71}
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070072
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070073static noinline void register_early_stack(void)
74{
75 early_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070076}
77
Joonsoo Kim48c96a32014-12-12 16:56:01 -080078static void init_page_owner(void)
79{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070080 if (!page_owner_enabled)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080081 return;
82
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070083 register_dummy_stack();
84 register_failure_stack();
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070085 register_early_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070086 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080087 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080088}
89
90struct page_ext_operations page_owner_ops = {
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070091 .size = sizeof(struct page_owner),
Joonsoo Kim48c96a32014-12-12 16:56:01 -080092 .need = need_page_owner,
93 .init = init_page_owner,
94};
95
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070096static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
97{
98 return (void *)page_ext + page_owner_ops.offset;
99}
100
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200101static inline bool check_recursive_alloc(unsigned long *entries,
102 unsigned int nr_entries,
103 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800104{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200105 unsigned int i;
Yang Shif86e4272016-06-03 14:55:38 -0700106
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200107 for (i = 0; i < nr_entries; i++) {
108 if (entries[i] == ip)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700109 return true;
110 }
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700111 return false;
112}
113
114static noinline depot_stack_handle_t save_stack(gfp_t flags)
115{
116 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700117 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200118 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700119
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200120 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700121
122 /*
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200123 * We need to check recursion here because our request to
124 * stackdepot could trigger memory allocation to save new
125 * entry. New memory allocation would reach here and call
126 * stack_depot_save_entries() again if we don't catch it. There is
127 * still not enough memory in stackdepot so it would try to
128 * allocate memory again and loop forever.
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700129 */
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200130 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700131 return dummy_handle;
132
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200133 handle = stack_depot_save(entries, nr_entries, flags);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700134 if (!handle)
135 handle = failure_handle;
136
137 return handle;
138}
139
Vlastimil Babka89745582019-09-23 15:34:42 -0700140void __reset_page_owner(struct page *page, unsigned int order)
141{
142 int i;
143 struct page_ext *page_ext;
Sergei Trofimovichfab765c2021-04-29 22:55:02 -0700144 depot_stack_handle_t handle;
Vlastimil Babka89745582019-09-23 15:34:42 -0700145 struct page_owner *page_owner;
Georgi Djakov866b4852021-04-29 22:54:57 -0700146 u64 free_ts_nsec = local_clock();
Vlastimil Babka89745582019-09-23 15:34:42 -0700147
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700148 page_ext = lookup_page_ext(page);
149 if (unlikely(!page_ext))
150 return;
Sergei Trofimovichfab765c2021-04-29 22:55:02 -0700151
152 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
Vlastimil Babka89745582019-09-23 15:34:42 -0700153 for (i = 0; i < (1 << order); i++) {
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700154 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700155 page_owner = get_page_owner(page_ext);
156 page_owner->free_handle = handle;
Georgi Djakov866b4852021-04-29 22:54:57 -0700157 page_owner->free_ts_nsec = free_ts_nsec;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700158 page_ext = page_ext_next(page_ext);
Vlastimil Babka89745582019-09-23 15:34:42 -0700159 }
160}
161
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700162static inline void __set_page_owner_handle(struct page_ext *page_ext,
163 depot_stack_handle_t handle,
164 unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700165{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700166 struct page_owner *page_owner;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700167 int i;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800168
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700169 for (i = 0; i < (1 << order); i++) {
170 page_owner = get_page_owner(page_ext);
171 page_owner->handle = handle;
172 page_owner->order = order;
173 page_owner->gfp_mask = gfp_mask;
174 page_owner->last_migrate_reason = -1;
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800175 page_owner->pid = current->pid;
176 page_owner->ts_nsec = local_clock();
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700177 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700178 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800179
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700180 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700181 }
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800182}
183
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700184noinline void __set_page_owner(struct page *page, unsigned int order,
185 gfp_t gfp_mask)
186{
187 struct page_ext *page_ext = lookup_page_ext(page);
188 depot_stack_handle_t handle;
189
190 if (unlikely(!page_ext))
191 return;
192
193 handle = save_stack(gfp_mask);
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700194 __set_page_owner_handle(page_ext, handle, order, gfp_mask);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700195}
196
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700197void __set_page_owner_migrate_reason(struct page *page, int reason)
198{
199 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700200 struct page_owner *page_owner;
201
Yang Shif86e4272016-06-03 14:55:38 -0700202 if (unlikely(!page_ext))
203 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700204
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700205 page_owner = get_page_owner(page_ext);
206 page_owner->last_migrate_reason = reason;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700207}
208
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700209void __split_page_owner(struct page *page, unsigned int nr)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700210{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700211 int i;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700212 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700213 struct page_owner *page_owner;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700214
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700215 if (unlikely(!page_ext))
216 return;
217
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700218 for (i = 0; i < nr; i++) {
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700219 page_owner = get_page_owner(page_ext);
220 page_owner->order = 0;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700221 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700222 }
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700223}
224
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700225void __copy_page_owner(struct page *oldpage, struct page *newpage)
226{
227 struct page_ext *old_ext = lookup_page_ext(oldpage);
228 struct page_ext *new_ext = lookup_page_ext(newpage);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700229 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700230
Yang Shif86e4272016-06-03 14:55:38 -0700231 if (unlikely(!old_ext || !new_ext))
232 return;
233
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700234 old_page_owner = get_page_owner(old_ext);
235 new_page_owner = get_page_owner(new_ext);
236 new_page_owner->order = old_page_owner->order;
237 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
238 new_page_owner->last_migrate_reason =
239 old_page_owner->last_migrate_reason;
240 new_page_owner->handle = old_page_owner->handle;
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800241 new_page_owner->pid = old_page_owner->pid;
242 new_page_owner->ts_nsec = old_page_owner->ts_nsec;
Georgi Djakov866b4852021-04-29 22:54:57 -0700243 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700244
245 /*
246 * We don't clear the bit on the oldpage as it's going to be freed
247 * after migration. Until then, the info can be useful in case of
248 * a bug, and the overal stats will be off a bit only temporarily.
249 * Also, migrate_misplaced_transhuge_page() can still fail the
250 * migration and then we want the oldpage to retain the info. But
251 * in that case we also don't need to explicitly clear the info from
252 * the new page, which will be freed.
253 */
254 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700255 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700256}
257
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700258void pagetypeinfo_showmixedcount_print(struct seq_file *m,
259 pg_data_t *pgdat, struct zone *zone)
260{
261 struct page *page;
262 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700263 struct page_owner *page_owner;
Miaohe Lin1d2cae82021-02-24 12:01:39 -0800264 unsigned long pfn, block_end_pfn;
265 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700266 unsigned long count[MIGRATE_TYPES] = { 0, };
267 int pageblock_mt, page_mt;
268 int i;
269
270 /* Scan block by block. First and last block may be incomplete */
271 pfn = zone->zone_start_pfn;
272
273 /*
274 * Walk the zone in pageblock_nr_pages steps. If a page block spans
275 * a zone boundary, it will be double counted between zones. This does
276 * not matter as the mixed block count will still be correct
277 */
278 for (; pfn < end_pfn; ) {
Qian Caia26ee562019-10-18 20:19:29 -0700279 page = pfn_to_online_page(pfn);
280 if (!page) {
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700281 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
282 continue;
283 }
284
285 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
286 block_end_pfn = min(block_end_pfn, end_pfn);
287
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700288 pageblock_mt = get_pageblock_migratetype(page);
289
290 for (; pfn < block_end_pfn; pfn++) {
291 if (!pfn_valid_within(pfn))
292 continue;
293
Qian Caia26ee562019-10-18 20:19:29 -0700294 /* The pageblock is online, no need to recheck. */
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700295 page = pfn_to_page(pfn);
296
297 if (page_zone(page) != zone)
298 continue;
299
300 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700301 unsigned long freepage_order;
302
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700303 freepage_order = buddy_order_unsafe(page);
Vinayak Menon727c0802017-07-10 15:49:17 -0700304 if (freepage_order < MAX_ORDER)
305 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700306 continue;
307 }
308
309 if (PageReserved(page))
310 continue;
311
312 page_ext = lookup_page_ext(page);
313 if (unlikely(!page_ext))
314 continue;
315
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700316 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700317 continue;
318
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700319 page_owner = get_page_owner(page_ext);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700320 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700321 if (pageblock_mt != page_mt) {
322 if (is_migrate_cma(pageblock_mt))
323 count[MIGRATE_MOVABLE]++;
324 else
325 count[pageblock_mt]++;
326
327 pfn = block_end_pfn;
328 break;
329 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700330 pfn += (1UL << page_owner->order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700331 }
332 }
333
334 /* Print counts */
335 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
336 for (i = 0; i < MIGRATE_TYPES; i++)
337 seq_printf(m, "%12lu ", count[i]);
338 seq_putc(m, '\n');
339}
340
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800341static ssize_t
342print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700343 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700344 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800345{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200346 int ret, pageblock_mt, page_mt;
347 unsigned long *entries;
348 unsigned int nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800349 char *kbuf;
350
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,
Georgi Djakov866b4852021-04-29 22:54:57 -0700357 "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700358 page_owner->order, page_owner->gfp_mask,
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800359 &page_owner->gfp_mask, page_owner->pid,
Georgi Djakov866b4852021-04-29 22:54:57 -0700360 page_owner->ts_nsec, page_owner->free_ts_nsec);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800361
362 if (ret >= count)
363 goto err;
364
365 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700366 pageblock_mt = get_pageblock_migratetype(page);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700367 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800368 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700369 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800370 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700371 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800372 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700373 migratetype_names[pageblock_mt],
374 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800375
376 if (ret >= count)
377 goto err;
378
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200379 nr_entries = stack_depot_fetch(handle, &entries);
380 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800381 if (ret >= count)
382 goto err;
383
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700384 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700385 ret += snprintf(kbuf + ret, count - ret,
386 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700387 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700388 if (ret >= count)
389 goto err;
390 }
391
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800392 ret += snprintf(kbuf + ret, count - ret, "\n");
393 if (ret >= count)
394 goto err;
395
396 if (copy_to_user(buf, kbuf, ret))
397 ret = -EFAULT;
398
399 kfree(kbuf);
400 return ret;
401
402err:
403 kfree(kbuf);
404 return -ENOMEM;
405}
406
Vlastimil Babka4e462112016-03-15 14:56:21 -0700407void __dump_page_owner(struct page *page)
408{
409 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700410 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700411 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200412 unsigned long *entries;
413 unsigned int nr_entries;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700414 gfp_t gfp_mask;
415 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700416
Yang Shif86e4272016-06-03 14:55:38 -0700417 if (unlikely(!page_ext)) {
418 pr_alert("There is not page extension available.\n");
419 return;
420 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700421
422 page_owner = get_page_owner(page_ext);
423 gfp_mask = page_owner->gfp_mask;
Wei Yang01c0bfe2020-06-03 15:59:08 -0700424 mt = gfp_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700425
Vlastimil Babka4e462112016-03-15 14:56:21 -0700426 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700427 pr_alert("page_owner info is not present (never set?)\n");
Vlastimil Babka4e462112016-03-15 14:56:21 -0700428 return;
429 }
430
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700431 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700432 pr_alert("page_owner tracks the page as allocated\n");
433 else
434 pr_alert("page_owner tracks the page as freed\n");
435
Georgi Djakov866b4852021-04-29 22:54:57 -0700436 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu, free_ts %llu\n",
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800437 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
Georgi Djakov866b4852021-04-29 22:54:57 -0700438 page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec);
Vlastimil Babka373891672019-09-23 15:34:39 -0700439
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700440 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700441 if (!handle) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700442 pr_alert("page_owner allocation stack trace missing\n");
443 } else {
444 nr_entries = stack_depot_fetch(handle, &entries);
445 stack_trace_print(entries, nr_entries, 0);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700446 }
447
Vlastimil Babka89745582019-09-23 15:34:42 -0700448 handle = READ_ONCE(page_owner->free_handle);
449 if (!handle) {
450 pr_alert("page_owner free stack trace missing\n");
451 } else {
452 nr_entries = stack_depot_fetch(handle, &entries);
453 pr_alert("page last free stack trace:\n");
454 stack_trace_print(entries, nr_entries, 0);
455 }
Vlastimil Babka89745582019-09-23 15:34:42 -0700456
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700457 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700458 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700459 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700460}
461
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800462static ssize_t
463read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
464{
465 unsigned long pfn;
466 struct page *page;
467 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700468 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700469 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800470
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700471 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800472 return -EINVAL;
473
474 page = NULL;
475 pfn = min_low_pfn + *ppos;
476
477 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
478 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
479 pfn++;
480
481 drain_all_pages(NULL);
482
483 /* Find an allocated page */
484 for (; pfn < max_pfn; pfn++) {
485 /*
486 * If the new page is in a new MAX_ORDER_NR_PAGES area,
487 * validate the area as existing, skip it if not
488 */
489 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
490 pfn += MAX_ORDER_NR_PAGES - 1;
491 continue;
492 }
493
494 /* Check for holes within a MAX_ORDER area */
495 if (!pfn_valid_within(pfn))
496 continue;
497
498 page = pfn_to_page(pfn);
499 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700500 unsigned long freepage_order = buddy_order_unsafe(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800501
502 if (freepage_order < MAX_ORDER)
503 pfn += (1UL << freepage_order) - 1;
504 continue;
505 }
506
507 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700508 if (unlikely(!page_ext))
509 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800510
511 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800512 * Some pages could be missed by concurrent allocation or free,
513 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800514 */
515 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
516 continue;
517
Vlastimil Babka373891672019-09-23 15:34:39 -0700518 /*
519 * Although we do have the info about past allocation of free
520 * pages, it's not relevant for current memory usage.
521 */
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700522 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700523 continue;
524
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700525 page_owner = get_page_owner(page_ext);
526
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700527 /*
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700528 * Don't print "tail" pages of high-order allocations as that
529 * would inflate the stats.
530 */
531 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
532 continue;
533
534 /*
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700535 * Access to page_ext->handle isn't synchronous so we should
536 * be careful to access it.
537 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700538 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700539 if (!handle)
540 continue;
541
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800542 /* Record the next PFN to read in the file offset */
543 *ppos = (pfn - min_low_pfn) + 1;
544
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700545 return print_page_owner(buf, count, pfn, page,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700546 page_owner, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800547 }
548
549 return 0;
550}
551
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800552static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
553{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800554 unsigned long pfn = zone->zone_start_pfn;
555 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800556 unsigned long count = 0;
557
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800558 /*
559 * Walk the zone in pageblock_nr_pages steps. If a page block spans
560 * a zone boundary, it will be double counted between zones. This does
561 * not matter as the mixed block count will still be correct
562 */
563 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800564 unsigned long block_end_pfn;
565
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800566 if (!pfn_valid(pfn)) {
567 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
568 continue;
569 }
570
571 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
572 block_end_pfn = min(block_end_pfn, end_pfn);
573
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800574 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800575 struct page *page;
576 struct page_ext *page_ext;
577
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800578 if (!pfn_valid_within(pfn))
579 continue;
580
581 page = pfn_to_page(pfn);
582
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700583 if (page_zone(page) != zone)
584 continue;
585
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800586 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700587 * To avoid having to grab zone->lock, be a little
588 * careful when reading buddy page order. The only
589 * danger is that we skip too much and potentially miss
590 * some early allocated pages, which is better than
591 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800592 */
593 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700594 unsigned long order = buddy_order_unsafe(page);
Vlastimil Babka10903022017-09-06 16:20:51 -0700595
596 if (order > 0 && order < MAX_ORDER)
597 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800598 continue;
599 }
600
601 if (PageReserved(page))
602 continue;
603
604 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700605 if (unlikely(!page_ext))
606 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800607
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700608 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800609 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
610 continue;
611
612 /* Found early allocated page */
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700613 __set_page_owner_handle(page_ext, early_handle,
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700614 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800615 count++;
616 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700617 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800618 }
619
620 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
621 pgdat->node_id, zone->name, count);
622}
623
624static void init_zones_in_node(pg_data_t *pgdat)
625{
626 struct zone *zone;
627 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800628
629 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
630 if (!populated_zone(zone))
631 continue;
632
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800633 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800634 }
635}
636
637static void init_early_allocated_pages(void)
638{
639 pg_data_t *pgdat;
640
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800641 for_each_online_pgdat(pgdat)
642 init_zones_in_node(pgdat);
643}
644
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800645static const struct file_operations proc_page_owner_operations = {
646 .read = read_page_owner,
647};
648
649static int __init pageowner_init(void)
650{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700651 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800652 pr_info("page_owner is disabled\n");
653 return 0;
654 }
655
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800656 debugfs_create_file("page_owner", 0400, NULL, NULL,
657 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800658
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800659 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800660}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400661late_initcall(pageowner_init)