blob: f51a57e92aa380cbea774ce97bebcaf3de7e4740 [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
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700101static noinline depot_stack_handle_t save_stack(gfp_t flags)
102{
103 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700104 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200105 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700106
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700107 /*
Sergei Trofimovich8e9b16c2021-04-29 22:55:08 -0700108 * Avoid recursion.
109 *
110 * Sometimes page metadata allocation tracking requires more
111 * memory to be allocated:
112 * - when new stack trace is saved to stack depot
113 * - when backtrace itself is calculated (ia64)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700114 */
Sergei Trofimovich8e9b16c2021-04-29 22:55:08 -0700115 if (current->in_page_owner)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700116 return dummy_handle;
Sergei Trofimovich8e9b16c2021-04-29 22:55:08 -0700117 current->in_page_owner = 1;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700118
Sergei Trofimovich8e9b16c2021-04-29 22:55:08 -0700119 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200120 handle = stack_depot_save(entries, nr_entries, flags);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700121 if (!handle)
122 handle = failure_handle;
123
Sergei Trofimovich8e9b16c2021-04-29 22:55:08 -0700124 current->in_page_owner = 0;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700125 return handle;
126}
127
Vlastimil Babka89745582019-09-23 15:34:42 -0700128void __reset_page_owner(struct page *page, unsigned int order)
129{
130 int i;
131 struct page_ext *page_ext;
Sergei Trofimovichfab765c2021-04-29 22:55:02 -0700132 depot_stack_handle_t handle;
Vlastimil Babka89745582019-09-23 15:34:42 -0700133 struct page_owner *page_owner;
Georgi Djakov866b4852021-04-29 22:54:57 -0700134 u64 free_ts_nsec = local_clock();
Vlastimil Babka89745582019-09-23 15:34:42 -0700135
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700136 page_ext = lookup_page_ext(page);
137 if (unlikely(!page_ext))
138 return;
Sergei Trofimovichfab765c2021-04-29 22:55:02 -0700139
140 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
Vlastimil Babka89745582019-09-23 15:34:42 -0700141 for (i = 0; i < (1 << order); i++) {
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700142 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700143 page_owner = get_page_owner(page_ext);
144 page_owner->free_handle = handle;
Georgi Djakov866b4852021-04-29 22:54:57 -0700145 page_owner->free_ts_nsec = free_ts_nsec;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700146 page_ext = page_ext_next(page_ext);
Vlastimil Babka89745582019-09-23 15:34:42 -0700147 }
148}
149
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700150static inline void __set_page_owner_handle(struct page_ext *page_ext,
151 depot_stack_handle_t handle,
152 unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700153{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700154 struct page_owner *page_owner;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700155 int i;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800156
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700157 for (i = 0; i < (1 << order); i++) {
158 page_owner = get_page_owner(page_ext);
159 page_owner->handle = handle;
160 page_owner->order = order;
161 page_owner->gfp_mask = gfp_mask;
162 page_owner->last_migrate_reason = -1;
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800163 page_owner->pid = current->pid;
164 page_owner->ts_nsec = local_clock();
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700165 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700166 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800167
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700168 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700169 }
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800170}
171
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700172noinline void __set_page_owner(struct page *page, unsigned int order,
173 gfp_t gfp_mask)
174{
175 struct page_ext *page_ext = lookup_page_ext(page);
176 depot_stack_handle_t handle;
177
178 if (unlikely(!page_ext))
179 return;
180
181 handle = save_stack(gfp_mask);
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700182 __set_page_owner_handle(page_ext, handle, order, gfp_mask);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700183}
184
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700185void __set_page_owner_migrate_reason(struct page *page, int reason)
186{
187 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700188 struct page_owner *page_owner;
189
Yang Shif86e4272016-06-03 14:55:38 -0700190 if (unlikely(!page_ext))
191 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700192
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700193 page_owner = get_page_owner(page_ext);
194 page_owner->last_migrate_reason = reason;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700195}
196
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700197void __split_page_owner(struct page *page, unsigned int nr)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700198{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700199 int i;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700200 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700201 struct page_owner *page_owner;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700202
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700203 if (unlikely(!page_ext))
204 return;
205
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700206 for (i = 0; i < nr; i++) {
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700207 page_owner = get_page_owner(page_ext);
208 page_owner->order = 0;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700209 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700210 }
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700211}
212
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700213void __copy_page_owner(struct page *oldpage, struct page *newpage)
214{
215 struct page_ext *old_ext = lookup_page_ext(oldpage);
216 struct page_ext *new_ext = lookup_page_ext(newpage);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700217 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700218
Yang Shif86e4272016-06-03 14:55:38 -0700219 if (unlikely(!old_ext || !new_ext))
220 return;
221
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700222 old_page_owner = get_page_owner(old_ext);
223 new_page_owner = get_page_owner(new_ext);
224 new_page_owner->order = old_page_owner->order;
225 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
226 new_page_owner->last_migrate_reason =
227 old_page_owner->last_migrate_reason;
228 new_page_owner->handle = old_page_owner->handle;
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800229 new_page_owner->pid = old_page_owner->pid;
230 new_page_owner->ts_nsec = old_page_owner->ts_nsec;
Georgi Djakov866b4852021-04-29 22:54:57 -0700231 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700232
233 /*
234 * We don't clear the bit on the oldpage as it's going to be freed
235 * after migration. Until then, the info can be useful in case of
Ingo Molnarf0953a12021-05-06 18:06:47 -0700236 * a bug, and the overall stats will be off a bit only temporarily.
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700237 * Also, migrate_misplaced_transhuge_page() can still fail the
238 * migration and then we want the oldpage to retain the info. But
239 * in that case we also don't need to explicitly clear the info from
240 * the new page, which will be freed.
241 */
242 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700243 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700244}
245
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700246void pagetypeinfo_showmixedcount_print(struct seq_file *m,
247 pg_data_t *pgdat, struct zone *zone)
248{
249 struct page *page;
250 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700251 struct page_owner *page_owner;
Miaohe Lin1d2cae82021-02-24 12:01:39 -0800252 unsigned long pfn, block_end_pfn;
253 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700254 unsigned long count[MIGRATE_TYPES] = { 0, };
255 int pageblock_mt, page_mt;
256 int i;
257
258 /* Scan block by block. First and last block may be incomplete */
259 pfn = zone->zone_start_pfn;
260
261 /*
262 * Walk the zone in pageblock_nr_pages steps. If a page block spans
263 * a zone boundary, it will be double counted between zones. This does
264 * not matter as the mixed block count will still be correct
265 */
266 for (; pfn < end_pfn; ) {
Qian Caia26ee562019-10-18 20:19:29 -0700267 page = pfn_to_online_page(pfn);
268 if (!page) {
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700269 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
270 continue;
271 }
272
273 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
274 block_end_pfn = min(block_end_pfn, end_pfn);
275
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700276 pageblock_mt = get_pageblock_migratetype(page);
277
278 for (; pfn < block_end_pfn; pfn++) {
279 if (!pfn_valid_within(pfn))
280 continue;
281
Qian Caia26ee562019-10-18 20:19:29 -0700282 /* The pageblock is online, no need to recheck. */
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700283 page = pfn_to_page(pfn);
284
285 if (page_zone(page) != zone)
286 continue;
287
288 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700289 unsigned long freepage_order;
290
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700291 freepage_order = buddy_order_unsafe(page);
Vinayak Menon727c0802017-07-10 15:49:17 -0700292 if (freepage_order < MAX_ORDER)
293 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700294 continue;
295 }
296
297 if (PageReserved(page))
298 continue;
299
300 page_ext = lookup_page_ext(page);
301 if (unlikely(!page_ext))
302 continue;
303
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700304 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700305 continue;
306
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700307 page_owner = get_page_owner(page_ext);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700308 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700309 if (pageblock_mt != page_mt) {
310 if (is_migrate_cma(pageblock_mt))
311 count[MIGRATE_MOVABLE]++;
312 else
313 count[pageblock_mt]++;
314
315 pfn = block_end_pfn;
316 break;
317 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700318 pfn += (1UL << page_owner->order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700319 }
320 }
321
322 /* Print counts */
323 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
324 for (i = 0; i < MIGRATE_TYPES; i++)
325 seq_printf(m, "%12lu ", count[i]);
326 seq_putc(m, '\n');
327}
328
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800329static ssize_t
330print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700331 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700332 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800333{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200334 int ret, pageblock_mt, page_mt;
335 unsigned long *entries;
336 unsigned int nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800337 char *kbuf;
338
Miles Chenc8f61cf2018-12-28 00:33:21 -0800339 count = min_t(size_t, count, PAGE_SIZE);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800340 kbuf = kmalloc(count, GFP_KERNEL);
341 if (!kbuf)
342 return -ENOMEM;
343
344 ret = snprintf(kbuf, count,
Georgi Djakov866b4852021-04-29 22:54:57 -0700345 "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 -0700346 page_owner->order, page_owner->gfp_mask,
Liam Mark9cc7e96a2020-12-14 19:04:49 -0800347 &page_owner->gfp_mask, page_owner->pid,
Georgi Djakov866b4852021-04-29 22:54:57 -0700348 page_owner->ts_nsec, page_owner->free_ts_nsec);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800349
350 if (ret >= count)
351 goto err;
352
353 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700354 pageblock_mt = get_pageblock_migratetype(page);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700355 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800356 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700357 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800358 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700359 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800360 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700361 migratetype_names[pageblock_mt],
362 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800363
364 if (ret >= count)
365 goto err;
366
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200367 nr_entries = stack_depot_fetch(handle, &entries);
368 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800369 if (ret >= count)
370 goto err;
371
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700372 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700373 ret += snprintf(kbuf + ret, count - ret,
374 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700375 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700376 if (ret >= count)
377 goto err;
378 }
379
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800380 ret += snprintf(kbuf + ret, count - ret, "\n");
381 if (ret >= count)
382 goto err;
383
384 if (copy_to_user(buf, kbuf, ret))
385 ret = -EFAULT;
386
387 kfree(kbuf);
388 return ret;
389
390err:
391 kfree(kbuf);
392 return -ENOMEM;
393}
394
Matthew Wilcox (Oracle)8bf6f452021-06-28 19:41:16 -0700395void __dump_page_owner(const struct page *page)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700396{
397 struct page_ext *page_ext = lookup_page_ext(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700398 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700399 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200400 unsigned long *entries;
401 unsigned int nr_entries;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700402 gfp_t gfp_mask;
403 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700404
Yang Shif86e4272016-06-03 14:55:38 -0700405 if (unlikely(!page_ext)) {
406 pr_alert("There is not page extension available.\n");
407 return;
408 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700409
410 page_owner = get_page_owner(page_ext);
411 gfp_mask = page_owner->gfp_mask;
Wei Yang01c0bfe2020-06-03 15:59:08 -0700412 mt = gfp_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700413
Vlastimil Babka4e462112016-03-15 14:56:21 -0700414 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700415 pr_alert("page_owner info is not present (never set?)\n");
Vlastimil Babka4e462112016-03-15 14:56:21 -0700416 return;
417 }
418
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700419 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700420 pr_alert("page_owner tracks the page as allocated\n");
421 else
422 pr_alert("page_owner tracks the page as freed\n");
423
Georgi Djakov866b4852021-04-29 22:54:57 -0700424 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 -0800425 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
Georgi Djakov866b4852021-04-29 22:54:57 -0700426 page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec);
Vlastimil Babka373891672019-09-23 15:34:39 -0700427
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700428 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700429 if (!handle) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700430 pr_alert("page_owner allocation stack trace missing\n");
431 } else {
432 nr_entries = stack_depot_fetch(handle, &entries);
433 stack_trace_print(entries, nr_entries, 0);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700434 }
435
Vlastimil Babka89745582019-09-23 15:34:42 -0700436 handle = READ_ONCE(page_owner->free_handle);
437 if (!handle) {
438 pr_alert("page_owner free stack trace missing\n");
439 } else {
440 nr_entries = stack_depot_fetch(handle, &entries);
441 pr_alert("page last free stack trace:\n");
442 stack_trace_print(entries, nr_entries, 0);
443 }
Vlastimil Babka89745582019-09-23 15:34:42 -0700444
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700445 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700446 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700447 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700448}
449
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800450static ssize_t
451read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
452{
453 unsigned long pfn;
454 struct page *page;
455 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700456 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700457 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800458
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700459 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800460 return -EINVAL;
461
462 page = NULL;
463 pfn = min_low_pfn + *ppos;
464
465 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
466 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
467 pfn++;
468
469 drain_all_pages(NULL);
470
471 /* Find an allocated page */
472 for (; pfn < max_pfn; pfn++) {
473 /*
474 * If the new page is in a new MAX_ORDER_NR_PAGES area,
475 * validate the area as existing, skip it if not
476 */
477 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
478 pfn += MAX_ORDER_NR_PAGES - 1;
479 continue;
480 }
481
482 /* Check for holes within a MAX_ORDER area */
483 if (!pfn_valid_within(pfn))
484 continue;
485
486 page = pfn_to_page(pfn);
487 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700488 unsigned long freepage_order = buddy_order_unsafe(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800489
490 if (freepage_order < MAX_ORDER)
491 pfn += (1UL << freepage_order) - 1;
492 continue;
493 }
494
495 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700496 if (unlikely(!page_ext))
497 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800498
499 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800500 * Some pages could be missed by concurrent allocation or free,
501 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800502 */
503 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
504 continue;
505
Vlastimil Babka373891672019-09-23 15:34:39 -0700506 /*
507 * Although we do have the info about past allocation of free
508 * pages, it's not relevant for current memory usage.
509 */
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700510 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700511 continue;
512
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700513 page_owner = get_page_owner(page_ext);
514
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700515 /*
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700516 * Don't print "tail" pages of high-order allocations as that
517 * would inflate the stats.
518 */
519 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
520 continue;
521
522 /*
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700523 * Access to page_ext->handle isn't synchronous so we should
524 * be careful to access it.
525 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700526 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700527 if (!handle)
528 continue;
529
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800530 /* Record the next PFN to read in the file offset */
531 *ppos = (pfn - min_low_pfn) + 1;
532
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700533 return print_page_owner(buf, count, pfn, page,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700534 page_owner, handle);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800535 }
536
537 return 0;
538}
539
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800540static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
541{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800542 unsigned long pfn = zone->zone_start_pfn;
543 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800544 unsigned long count = 0;
545
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800546 /*
547 * Walk the zone in pageblock_nr_pages steps. If a page block spans
548 * a zone boundary, it will be double counted between zones. This does
549 * not matter as the mixed block count will still be correct
550 */
551 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800552 unsigned long block_end_pfn;
553
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800554 if (!pfn_valid(pfn)) {
555 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
556 continue;
557 }
558
559 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
560 block_end_pfn = min(block_end_pfn, end_pfn);
561
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800562 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800563 struct page *page;
564 struct page_ext *page_ext;
565
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800566 if (!pfn_valid_within(pfn))
567 continue;
568
569 page = pfn_to_page(pfn);
570
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700571 if (page_zone(page) != zone)
572 continue;
573
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800574 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700575 * To avoid having to grab zone->lock, be a little
576 * careful when reading buddy page order. The only
577 * danger is that we skip too much and potentially miss
578 * some early allocated pages, which is better than
579 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800580 */
581 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700582 unsigned long order = buddy_order_unsafe(page);
Vlastimil Babka10903022017-09-06 16:20:51 -0700583
584 if (order > 0 && order < MAX_ORDER)
585 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800586 continue;
587 }
588
589 if (PageReserved(page))
590 continue;
591
592 page_ext = lookup_page_ext(page);
Yang Shif86e4272016-06-03 14:55:38 -0700593 if (unlikely(!page_ext))
594 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800595
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700596 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800597 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
598 continue;
599
600 /* Found early allocated page */
zhongjiang-ali64ea78d2021-04-29 22:55:00 -0700601 __set_page_owner_handle(page_ext, early_handle,
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700602 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800603 count++;
604 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700605 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800606 }
607
608 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
609 pgdat->node_id, zone->name, count);
610}
611
612static void init_zones_in_node(pg_data_t *pgdat)
613{
614 struct zone *zone;
615 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800616
617 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
618 if (!populated_zone(zone))
619 continue;
620
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800621 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800622 }
623}
624
625static void init_early_allocated_pages(void)
626{
627 pg_data_t *pgdat;
628
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800629 for_each_online_pgdat(pgdat)
630 init_zones_in_node(pgdat);
631}
632
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800633static const struct file_operations proc_page_owner_operations = {
634 .read = read_page_owner,
635};
636
637static int __init pageowner_init(void)
638{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700639 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800640 pr_info("page_owner is disabled\n");
641 return 0;
642 }
643
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800644 debugfs_create_file("page_owner", 0400, NULL, NULL,
645 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800646
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800647 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800648}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400649late_initcall(pageowner_init)