blob: b3e5f98f299b325e1b5c987b9285fa4efd37a082 [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 Markfd0328e2020-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 Markfd0328e2020-12-14 19:04:49 -080029 u64 ts_nsec;
Georgi Djakovf8765be2021-03-24 10:15:47 -070030 u64 free_ts_nsec;
Liam Markfd0328e2020-12-14 19:04:49 -080031 pid_t pid;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070032};
33
Vijayanand Jitta29d1a0e2021-03-22 18:22:20 +053034bool page_owner_enabled;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070035DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Pratyush Brahmaa9c0f622023-08-17 15:38:28 +053036EXPORT_SYMBOL_GPL(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080037
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070038static depot_stack_handle_t dummy_handle;
39static depot_stack_handle_t failure_handle;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070040static depot_stack_handle_t early_handle;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070041
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080042static void init_early_allocated_pages(void);
43
Dou Liyang11731942018-04-05 16:23:49 -070044static int __init early_page_owner_param(char *buf)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080045{
46 if (!buf)
47 return -EINVAL;
48
49 if (strcmp(buf, "on") == 0)
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070050 page_owner_enabled = true;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080051
52 return 0;
53}
54early_param("page_owner", early_page_owner_param);
55
56static bool need_page_owner(void)
57{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070058 return page_owner_enabled;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080059}
60
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070061static __always_inline depot_stack_handle_t create_dummy_stack(void)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070062{
63 unsigned long entries[4];
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020064 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070065
Thomas Gleixneraf52bf62019-04-25 11:45:03 +020066 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
67 return stack_depot_save(entries, nr_entries, GFP_KERNEL);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070068}
69
70static noinline void register_dummy_stack(void)
71{
72 dummy_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070073}
74
75static noinline void register_failure_stack(void)
76{
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070077 failure_handle = create_dummy_stack();
78}
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070079
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070080static noinline void register_early_stack(void)
81{
82 early_handle = create_dummy_stack();
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070083}
84
Joonsoo Kim48c96a32014-12-12 16:56:01 -080085static void init_page_owner(void)
86{
Vlastimil Babka0fe9a442019-10-14 14:11:44 -070087 if (!page_owner_enabled)
Joonsoo Kim48c96a32014-12-12 16:56:01 -080088 return;
89
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -070090 register_dummy_stack();
91 register_failure_stack();
Vlastimil Babkadab4ead2017-09-06 16:20:44 -070092 register_early_stack();
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070093 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080094 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080095}
96
97struct page_ext_operations page_owner_ops = {
Joonsoo Kim9300d8d2016-10-07 16:58:30 -070098 .size = sizeof(struct page_owner),
Joonsoo Kim48c96a32014-12-12 16:56:01 -080099 .need = need_page_owner,
100 .init = init_page_owner,
101};
102
Vijayanand Jittab76264c2021-01-05 11:33:53 +0530103struct page_owner *get_page_owner(struct page_ext *page_ext)
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700104{
105 return (void *)page_ext + page_owner_ops.offset;
106}
Vijayanand Jittab76264c2021-01-05 11:33:53 +0530107EXPORT_SYMBOL_GPL(get_page_owner);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700108
Vijayanand Jitta8f302082021-01-12 00:15:31 +0530109depot_stack_handle_t get_page_owner_handle(struct page_ext *page_ext, unsigned long pfn)
110{
111 struct page_owner *page_owner;
112 depot_stack_handle_t handle;
113
114 if (!page_owner_enabled)
115 return 0;
116
117 page_owner = get_page_owner(page_ext);
118
119 /* skip handle for tail pages of higher order allocations */
120 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
121 return 0;
122
123 handle = READ_ONCE(page_owner->handle);
124 return handle;
125}
126EXPORT_SYMBOL_GPL(get_page_owner_handle);
127
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200128static inline bool check_recursive_alloc(unsigned long *entries,
129 unsigned int nr_entries,
130 unsigned long ip)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800131{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200132 unsigned int i;
Yang Shif86e4272016-06-03 14:55:38 -0700133
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200134 for (i = 0; i < nr_entries; i++) {
135 if (entries[i] == ip)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700136 return true;
137 }
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700138 return false;
139}
140
141static noinline depot_stack_handle_t save_stack(gfp_t flags)
142{
143 unsigned long entries[PAGE_OWNER_STACK_DEPTH];
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700144 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200145 unsigned int nr_entries;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700146
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200147 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700148
149 /*
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200150 * We need to check recursion here because our request to
151 * stackdepot could trigger memory allocation to save new
152 * entry. New memory allocation would reach here and call
153 * stack_depot_save_entries() again if we don't catch it. There is
154 * still not enough memory in stackdepot so it would try to
155 * allocate memory again and loop forever.
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700156 */
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200157 if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700158 return dummy_handle;
159
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200160 handle = stack_depot_save(entries, nr_entries, flags);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700161 if (!handle)
162 handle = failure_handle;
163
164 return handle;
165}
166
Vlastimil Babka89745582019-09-23 15:34:42 -0700167void __reset_page_owner(struct page *page, unsigned int order)
168{
169 int i;
170 struct page_ext *page_ext;
Vlastimil Babka89745582019-09-23 15:34:42 -0700171 depot_stack_handle_t handle = 0;
172 struct page_owner *page_owner;
Georgi Djakovf8765be2021-03-24 10:15:47 -0700173 u64 free_ts_nsec = local_clock();
Vlastimil Babka89745582019-09-23 15:34:42 -0700174
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700175 handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
Vlastimil Babka89745582019-09-23 15:34:42 -0700176
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530177 page_ext = page_ext_get(page);
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700178 if (unlikely(!page_ext))
179 return;
Vlastimil Babka89745582019-09-23 15:34:42 -0700180 for (i = 0; i < (1 << order); i++) {
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700181 __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Vlastimil Babka0fe9a442019-10-14 14:11:44 -0700182 page_owner = get_page_owner(page_ext);
183 page_owner->free_handle = handle;
Georgi Djakovf8765be2021-03-24 10:15:47 -0700184 page_owner->free_ts_nsec = free_ts_nsec;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700185 page_ext = page_ext_next(page_ext);
Vlastimil Babka89745582019-09-23 15:34:42 -0700186 }
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530187 page_ext_put(page_ext);
Vlastimil Babka89745582019-09-23 15:34:42 -0700188}
189
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700190static inline void __set_page_owner_handle(struct page *page,
191 struct page_ext *page_ext, depot_stack_handle_t handle,
192 unsigned int order, gfp_t gfp_mask)
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700193{
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700194 struct page_owner *page_owner;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700195 int i;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800196
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700197 for (i = 0; i < (1 << order); i++) {
198 page_owner = get_page_owner(page_ext);
199 page_owner->handle = handle;
200 page_owner->order = order;
201 page_owner->gfp_mask = gfp_mask;
202 page_owner->last_migrate_reason = -1;
Liam Markfd0328e2020-12-14 19:04:49 -0800203 page_owner->pid = current->pid;
204 page_owner->ts_nsec = local_clock();
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700205 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700206 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800207
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700208 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700209 }
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800210}
211
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700212noinline void __set_page_owner(struct page *page, unsigned int order,
213 gfp_t gfp_mask)
214{
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530215 struct page_ext *page_ext;
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700216 depot_stack_handle_t handle;
217
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530218 handle = save_stack(gfp_mask);
219
220 page_ext = page_ext_get(page);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700221 if (unlikely(!page_ext))
222 return;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700223 __set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530224 page_ext_put(page_ext);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700225}
Pratyush Brahmaa9c0f622023-08-17 15:38:28 +0530226EXPORT_SYMBOL_GPL(__set_page_owner);
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700227
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700228void __set_page_owner_migrate_reason(struct page *page, int reason)
229{
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530230 struct page_ext *page_ext = page_ext_get(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700231 struct page_owner *page_owner;
232
Yang Shif86e4272016-06-03 14:55:38 -0700233 if (unlikely(!page_ext))
234 return;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700235
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700236 page_owner = get_page_owner(page_ext);
237 page_owner->last_migrate_reason = reason;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530238 page_ext_put(page_ext);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700239}
240
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700241void __split_page_owner(struct page *page, unsigned int nr)
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700242{
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700243 int i;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530244 struct page_ext *page_ext = page_ext_get(page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700245 struct page_owner *page_owner;
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700246
Joonsoo Kima9627bc2016-07-26 15:23:49 -0700247 if (unlikely(!page_ext))
248 return;
249
Matthew Wilcox (Oracle)8fb156c2020-10-15 20:05:29 -0700250 for (i = 0; i < nr; i++) {
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700251 page_owner = get_page_owner(page_ext);
252 page_owner->order = 0;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700253 page_ext = page_ext_next(page_ext);
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700254 }
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530255 page_ext_put(page_ext);
Joonsoo Kime2cfc912015-07-17 16:24:18 -0700256}
257
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700258void __copy_page_owner(struct page *oldpage, struct page *newpage)
259{
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530260 struct page_ext *old_ext;
261 struct page_ext *new_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700262 struct page_owner *old_page_owner, *new_page_owner;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700263
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530264 old_ext = page_ext_get(oldpage);
265 if (unlikely(!old_ext))
Yang Shif86e4272016-06-03 14:55:38 -0700266 return;
267
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530268 new_ext = page_ext_get(newpage);
269 if (unlikely(!new_ext)) {
270 page_ext_put(old_ext);
271 return;
272 }
273
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700274 old_page_owner = get_page_owner(old_ext);
275 new_page_owner = get_page_owner(new_ext);
276 new_page_owner->order = old_page_owner->order;
277 new_page_owner->gfp_mask = old_page_owner->gfp_mask;
278 new_page_owner->last_migrate_reason =
279 old_page_owner->last_migrate_reason;
280 new_page_owner->handle = old_page_owner->handle;
Liam Markfd0328e2020-12-14 19:04:49 -0800281 new_page_owner->pid = old_page_owner->pid;
282 new_page_owner->ts_nsec = old_page_owner->ts_nsec;
Georgi Djakovf8765be2021-03-24 10:15:47 -0700283 new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700284
285 /*
286 * We don't clear the bit on the oldpage as it's going to be freed
287 * after migration. Until then, the info can be useful in case of
288 * a bug, and the overal stats will be off a bit only temporarily.
289 * Also, migrate_misplaced_transhuge_page() can still fail the
290 * migration and then we want the oldpage to retain the info. But
291 * in that case we also don't need to explicitly clear the info from
292 * the new page, which will be freed.
293 */
294 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700295 __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530296 page_ext_put(new_ext);
297 page_ext_put(old_ext);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700298}
299
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700300void pagetypeinfo_showmixedcount_print(struct seq_file *m,
301 pg_data_t *pgdat, struct zone *zone)
302{
303 struct page *page;
304 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700305 struct page_owner *page_owner;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700306 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
307 unsigned long end_pfn = pfn + zone->spanned_pages;
308 unsigned long count[MIGRATE_TYPES] = { 0, };
309 int pageblock_mt, page_mt;
310 int i;
311
312 /* Scan block by block. First and last block may be incomplete */
313 pfn = zone->zone_start_pfn;
314
315 /*
316 * Walk the zone in pageblock_nr_pages steps. If a page block spans
317 * a zone boundary, it will be double counted between zones. This does
318 * not matter as the mixed block count will still be correct
319 */
320 for (; pfn < end_pfn; ) {
Qian Caia26ee562019-10-18 20:19:29 -0700321 page = pfn_to_online_page(pfn);
322 if (!page) {
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700323 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
324 continue;
325 }
326
327 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
328 block_end_pfn = min(block_end_pfn, end_pfn);
329
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700330 pageblock_mt = get_pageblock_migratetype(page);
331
332 for (; pfn < block_end_pfn; pfn++) {
333 if (!pfn_valid_within(pfn))
334 continue;
335
Qian Caia26ee562019-10-18 20:19:29 -0700336 /* The pageblock is online, no need to recheck. */
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700337 page = pfn_to_page(pfn);
338
339 if (page_zone(page) != zone)
340 continue;
341
342 if (PageBuddy(page)) {
Vinayak Menon727c0802017-07-10 15:49:17 -0700343 unsigned long freepage_order;
344
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700345 freepage_order = buddy_order_unsafe(page);
Vinayak Menon727c0802017-07-10 15:49:17 -0700346 if (freepage_order < MAX_ORDER)
347 pfn += (1UL << freepage_order) - 1;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700348 continue;
349 }
350
351 if (PageReserved(page))
352 continue;
353
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530354 page_ext = page_ext_get(page);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700355 if (unlikely(!page_ext))
356 continue;
357
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700358 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530359 goto ext_put_continue;
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700360
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700361 page_owner = get_page_owner(page_ext);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700362 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700363 if (pageblock_mt != page_mt) {
364 if (is_migrate_cma(pageblock_mt))
365 count[MIGRATE_MOVABLE]++;
366 else
367 count[pageblock_mt]++;
368
369 pfn = block_end_pfn;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530370 page_ext_put(page_ext);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700371 break;
372 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700373 pfn += (1UL << page_owner->order) - 1;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530374ext_put_continue:
375 page_ext_put(page_ext);
Joonsoo Kime2f612e2016-10-07 16:58:21 -0700376 }
377 }
378
379 /* Print counts */
380 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
381 for (i = 0; i < MIGRATE_TYPES; i++)
382 seq_printf(m, "%12lu ", count[i]);
383 seq_putc(m, '\n');
384}
385
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800386static ssize_t
387print_page_owner(char __user *buf, size_t count, unsigned long pfn,
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700388 struct page *page, struct page_owner *page_owner,
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700389 depot_stack_handle_t handle)
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800390{
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200391 int ret, pageblock_mt, page_mt;
392 unsigned long *entries;
393 unsigned int nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800394 char *kbuf;
395
Miles Chenc8f61cf2018-12-28 00:33:21 -0800396 count = min_t(size_t, count, PAGE_SIZE);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800397 kbuf = kmalloc(count, GFP_KERNEL);
398 if (!kbuf)
399 return -ENOMEM;
400
401 ret = snprintf(kbuf, count,
Georgi Djakovf8765be2021-03-24 10:15:47 -0700402 "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 -0700403 page_owner->order, page_owner->gfp_mask,
Liam Markfd0328e2020-12-14 19:04:49 -0800404 &page_owner->gfp_mask, page_owner->pid,
Georgi Djakovf8765be2021-03-24 10:15:47 -0700405 page_owner->ts_nsec, page_owner->free_ts_nsec);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800406
407 if (ret >= count)
408 goto err;
409
410 /* Print information relevant to grouping pages by mobility */
Mel Gorman0b423ca2016-05-19 17:14:27 -0700411 pageblock_mt = get_pageblock_migratetype(page);
Wei Yang01c0bfe2020-06-03 15:59:08 -0700412 page_mt = gfp_migratetype(page_owner->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800413 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700414 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800415 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700416 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800417 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700418 migratetype_names[pageblock_mt],
419 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800420
421 if (ret >= count)
422 goto err;
423
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200424 nr_entries = stack_depot_fetch(handle, &entries);
425 ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800426 if (ret >= count)
427 goto err;
428
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700429 if (page_owner->last_migrate_reason != -1) {
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700430 ret += snprintf(kbuf + ret, count - ret,
431 "Page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700432 migrate_reason_names[page_owner->last_migrate_reason]);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -0700433 if (ret >= count)
434 goto err;
435 }
436
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800437 ret += snprintf(kbuf + ret, count - ret, "\n");
438 if (ret >= count)
439 goto err;
440
441 if (copy_to_user(buf, kbuf, ret))
442 ret = -EFAULT;
443
444 kfree(kbuf);
445 return ret;
446
447err:
448 kfree(kbuf);
449 return -ENOMEM;
450}
451
Vlastimil Babka4e462112016-03-15 14:56:21 -0700452void __dump_page_owner(struct page *page)
453{
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530454 struct page_ext *page_ext = page_ext_get((void *)page);
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700455 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700456 depot_stack_handle_t handle;
Thomas Gleixneraf52bf62019-04-25 11:45:03 +0200457 unsigned long *entries;
458 unsigned int nr_entries;
Sudip Mukherjee82850272016-06-24 14:50:24 -0700459 gfp_t gfp_mask;
460 int mt;
Vlastimil Babka4e462112016-03-15 14:56:21 -0700461
Yang Shif86e4272016-06-03 14:55:38 -0700462 if (unlikely(!page_ext)) {
463 pr_alert("There is not page extension available.\n");
464 return;
465 }
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700466
467 page_owner = get_page_owner(page_ext);
468 gfp_mask = page_owner->gfp_mask;
Wei Yang01c0bfe2020-06-03 15:59:08 -0700469 mt = gfp_migratetype(gfp_mask);
Yang Shif86e4272016-06-03 14:55:38 -0700470
Vlastimil Babka4e462112016-03-15 14:56:21 -0700471 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700472 pr_alert("page_owner info is not present (never set?)\n");
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530473 page_ext_put(page_ext);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700474 return;
475 }
476
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700477 if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Vlastimil Babka373891672019-09-23 15:34:39 -0700478 pr_alert("page_owner tracks the page as allocated\n");
479 else
480 pr_alert("page_owner tracks the page as freed\n");
481
Georgi Djakovf8765be2021-03-24 10:15:47 -0700482 pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu, free_ts %llu\n",
Liam Markfd0328e2020-12-14 19:04:49 -0800483 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
Georgi Djakovf8765be2021-03-24 10:15:47 -0700484 page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec);
Vlastimil Babka373891672019-09-23 15:34:39 -0700485
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700486 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700487 if (!handle) {
Vlastimil Babka373891672019-09-23 15:34:39 -0700488 pr_alert("page_owner allocation stack trace missing\n");
489 } else {
490 nr_entries = stack_depot_fetch(handle, &entries);
491 stack_trace_print(entries, nr_entries, 0);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700492 }
493
Vlastimil Babka89745582019-09-23 15:34:42 -0700494 handle = READ_ONCE(page_owner->free_handle);
495 if (!handle) {
496 pr_alert("page_owner free stack trace missing\n");
497 } else {
498 nr_entries = stack_depot_fetch(handle, &entries);
499 pr_alert("page last free stack trace:\n");
500 stack_trace_print(entries, nr_entries, 0);
501 }
Vlastimil Babka89745582019-09-23 15:34:42 -0700502
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700503 if (page_owner->last_migrate_reason != -1)
Vlastimil Babka4e462112016-03-15 14:56:21 -0700504 pr_alert("page has been migrated, last migrate reason: %s\n",
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700505 migrate_reason_names[page_owner->last_migrate_reason]);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530506 page_ext_put(page_ext);
Vlastimil Babka4e462112016-03-15 14:56:21 -0700507}
508
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800509static ssize_t
510read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
511{
512 unsigned long pfn;
513 struct page *page;
514 struct page_ext *page_ext;
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700515 struct page_owner *page_owner;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700516 depot_stack_handle_t handle;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800517
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700518 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800519 return -EINVAL;
520
521 page = NULL;
522 pfn = min_low_pfn + *ppos;
523
524 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
525 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
526 pfn++;
527
528 drain_all_pages(NULL);
529
530 /* Find an allocated page */
531 for (; pfn < max_pfn; pfn++) {
532 /*
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530533 * This temporary page_owner is required so
534 * that we can avoid the context switches while holding
535 * the rcu lock and copying the page owner information to
536 * user through copy_to_user() or GFP_KERNEL allocations.
537 */
538 struct page_owner page_owner_tmp;
539
540 /*
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800541 * If the new page is in a new MAX_ORDER_NR_PAGES area,
542 * validate the area as existing, skip it if not
543 */
544 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
545 pfn += MAX_ORDER_NR_PAGES - 1;
546 continue;
547 }
548
549 /* Check for holes within a MAX_ORDER area */
550 if (!pfn_valid_within(pfn))
551 continue;
552
553 page = pfn_to_page(pfn);
554 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700555 unsigned long freepage_order = buddy_order_unsafe(page);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800556
557 if (freepage_order < MAX_ORDER)
558 pfn += (1UL << freepage_order) - 1;
559 continue;
560 }
561
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530562 page_ext = page_ext_get(page);
Yang Shif86e4272016-06-03 14:55:38 -0700563 if (unlikely(!page_ext))
564 continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800565
566 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800567 * Some pages could be missed by concurrent allocation or free,
568 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800569 */
570 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530571 goto ext_put_continue;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800572
Vlastimil Babka373891672019-09-23 15:34:39 -0700573 /*
574 * Although we do have the info about past allocation of free
575 * pages, it's not relevant for current memory usage.
576 */
Vlastimil Babkafdf3bf82019-10-14 14:11:47 -0700577 if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530578 goto ext_put_continue;
Vlastimil Babka373891672019-09-23 15:34:39 -0700579
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700580 page_owner = get_page_owner(page_ext);
581
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700582 /*
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700583 * Don't print "tail" pages of high-order allocations as that
584 * would inflate the stats.
585 */
586 if (!IS_ALIGNED(pfn, 1 << page_owner->order))
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530587 goto ext_put_continue;
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700588
589 /*
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700590 * Access to page_ext->handle isn't synchronous so we should
591 * be careful to access it.
592 */
Joonsoo Kim9300d8d2016-10-07 16:58:30 -0700593 handle = READ_ONCE(page_owner->handle);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700594 if (!handle)
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530595 goto ext_put_continue;
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700596
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800597 /* Record the next PFN to read in the file offset */
598 *ppos = (pfn - min_low_pfn) + 1;
599
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530600 page_owner_tmp = *page_owner;
601 page_ext_put(page_ext);
Joonsoo Kimf2ca0b52016-07-26 15:23:55 -0700602 return print_page_owner(buf, count, pfn, page,
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530603 &page_owner_tmp, handle);
604ext_put_continue:
605 page_ext_put(page_ext);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800606 }
607
608 return 0;
609}
610
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800611static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
612{
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800613 unsigned long pfn = zone->zone_start_pfn;
614 unsigned long end_pfn = zone_end_pfn(zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800615 unsigned long count = 0;
616
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800617 /*
618 * Walk the zone in pageblock_nr_pages steps. If a page block spans
619 * a zone boundary, it will be double counted between zones. This does
620 * not matter as the mixed block count will still be correct
621 */
622 for (; pfn < end_pfn; ) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800623 unsigned long block_end_pfn;
624
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800625 if (!pfn_valid(pfn)) {
626 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
627 continue;
628 }
629
630 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
631 block_end_pfn = min(block_end_pfn, end_pfn);
632
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800633 for (; pfn < block_end_pfn; pfn++) {
Oscar Salvador6787c1d2018-01-31 16:20:11 -0800634 struct page *page;
635 struct page_ext *page_ext;
636
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800637 if (!pfn_valid_within(pfn))
638 continue;
639
640 page = pfn_to_page(pfn);
641
Joonsoo Kim9d43f5a2016-05-19 17:12:13 -0700642 if (page_zone(page) != zone)
643 continue;
644
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800645 /*
Vlastimil Babka10903022017-09-06 16:20:51 -0700646 * To avoid having to grab zone->lock, be a little
647 * careful when reading buddy page order. The only
648 * danger is that we skip too much and potentially miss
649 * some early allocated pages, which is better than
650 * heavy lock contention.
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800651 */
652 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700653 unsigned long order = buddy_order_unsafe(page);
Vlastimil Babka10903022017-09-06 16:20:51 -0700654
655 if (order > 0 && order < MAX_ORDER)
656 pfn += (1UL << order) - 1;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800657 continue;
658 }
659
660 if (PageReserved(page))
661 continue;
662
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530663 page_ext = page_ext_get(page);
Yang Shif86e4272016-06-03 14:55:38 -0700664 if (unlikely(!page_ext))
665 continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800666
Vlastimil Babkadab4ead2017-09-06 16:20:44 -0700667 /* Maybe overlapping zone */
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800668 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530669 goto ext_put_continue;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800670
671 /* Found early allocated page */
Vlastimil Babka7e2f2a02019-09-23 15:34:36 -0700672 __set_page_owner_handle(page, page_ext, early_handle,
673 0, 0);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800674 count++;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530675ext_put_continue:
676 page_ext_put(page_ext);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800677 }
Vlastimil Babka10903022017-09-06 16:20:51 -0700678 cond_resched();
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800679 }
680
681 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
682 pgdat->node_id, zone->name, count);
683}
684
685static void init_zones_in_node(pg_data_t *pgdat)
686{
687 struct zone *zone;
688 struct zone *node_zones = pgdat->node_zones;
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800689
690 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
691 if (!populated_zone(zone))
692 continue;
693
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800694 init_pages_in_zone(pgdat, zone);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800695 }
696}
697
698static void init_early_allocated_pages(void)
699{
700 pg_data_t *pgdat;
701
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800702 for_each_online_pgdat(pgdat)
703 init_zones_in_node(pgdat);
704}
705
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800706static const struct file_operations proc_page_owner_operations = {
707 .read = read_page_owner,
708};
709
710static int __init pageowner_init(void)
711{
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700712 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800713 pr_info("page_owner is disabled\n");
714 return 0;
715 }
716
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800717 debugfs_create_file("page_owner", 0400, NULL, NULL,
718 &proc_page_owner_operations);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800719
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800720 return 0;
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800721}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400722late_initcall(pageowner_init)