Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 2 | #include <linux/debugfs.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <linux/slab.h> |
| 5 | #include <linux/uaccess.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 6 | #include <linux/memblock.h> |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 7 | #include <linux/stacktrace.h> |
| 8 | #include <linux/page_owner.h> |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 9 | #include <linux/jump_label.h> |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 10 | #include <linux/migrate.h> |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 11 | #include <linux/stackdepot.h> |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 12 | #include <linux/seq_file.h> |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 13 | #include <linux/sched/clock.h> |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 14 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 15 | #include "internal.h" |
| 16 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 17 | /* |
| 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 Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 23 | struct page_owner { |
Ayush Mittal | 6b4c54e | 2017-11-15 17:34:30 -0800 | [diff] [blame] | 24 | unsigned short order; |
| 25 | short last_migrate_reason; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 26 | gfp_t gfp_mask; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 27 | depot_stack_handle_t handle; |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 28 | depot_stack_handle_t free_handle; |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 29 | u64 ts_nsec; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 30 | u64 free_ts_nsec; |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 31 | pid_t pid; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Vijayanand Jitta | 29d1a0e | 2021-03-22 18:22:20 +0530 | [diff] [blame] | 34 | bool page_owner_enabled; |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 35 | DEFINE_STATIC_KEY_FALSE(page_owner_inited); |
Pratyush Brahma | a9c0f62 | 2023-08-17 15:38:28 +0530 | [diff] [blame] | 36 | EXPORT_SYMBOL_GPL(page_owner_inited); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 37 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 38 | static depot_stack_handle_t dummy_handle; |
| 39 | static depot_stack_handle_t failure_handle; |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 40 | static depot_stack_handle_t early_handle; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 41 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 42 | static void init_early_allocated_pages(void); |
| 43 | |
Dou Liyang | 1173194 | 2018-04-05 16:23:49 -0700 | [diff] [blame] | 44 | static int __init early_page_owner_param(char *buf) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 45 | { |
| 46 | if (!buf) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | if (strcmp(buf, "on") == 0) |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 50 | page_owner_enabled = true; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | early_param("page_owner", early_page_owner_param); |
| 55 | |
| 56 | static bool need_page_owner(void) |
| 57 | { |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 58 | return page_owner_enabled; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 61 | static __always_inline depot_stack_handle_t create_dummy_stack(void) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 62 | { |
| 63 | unsigned long entries[4]; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 64 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 65 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 66 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); |
| 67 | return stack_depot_save(entries, nr_entries, GFP_KERNEL); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static noinline void register_dummy_stack(void) |
| 71 | { |
| 72 | dummy_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static noinline void register_failure_stack(void) |
| 76 | { |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 77 | failure_handle = create_dummy_stack(); |
| 78 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 79 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 80 | static noinline void register_early_stack(void) |
| 81 | { |
| 82 | early_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 85 | static void init_page_owner(void) |
| 86 | { |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 87 | if (!page_owner_enabled) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 88 | return; |
| 89 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 90 | register_dummy_stack(); |
| 91 | register_failure_stack(); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 92 | register_early_stack(); |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 93 | static_branch_enable(&page_owner_inited); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 94 | init_early_allocated_pages(); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | struct page_ext_operations page_owner_ops = { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 98 | .size = sizeof(struct page_owner), |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 99 | .need = need_page_owner, |
| 100 | .init = init_page_owner, |
| 101 | }; |
| 102 | |
Vijayanand Jitta | b76264c | 2021-01-05 11:33:53 +0530 | [diff] [blame] | 103 | struct page_owner *get_page_owner(struct page_ext *page_ext) |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 104 | { |
| 105 | return (void *)page_ext + page_owner_ops.offset; |
| 106 | } |
Vijayanand Jitta | b76264c | 2021-01-05 11:33:53 +0530 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(get_page_owner); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 108 | |
Vijayanand Jitta | 8f30208 | 2021-01-12 00:15:31 +0530 | [diff] [blame] | 109 | depot_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 | } |
| 126 | EXPORT_SYMBOL_GPL(get_page_owner_handle); |
| 127 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 128 | static inline bool check_recursive_alloc(unsigned long *entries, |
| 129 | unsigned int nr_entries, |
| 130 | unsigned long ip) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 131 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 132 | unsigned int i; |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 133 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 134 | for (i = 0; i < nr_entries; i++) { |
| 135 | if (entries[i] == ip) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
| 141 | static noinline depot_stack_handle_t save_stack(gfp_t flags) |
| 142 | { |
| 143 | unsigned long entries[PAGE_OWNER_STACK_DEPTH]; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 144 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 145 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 146 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 147 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 150 | * 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 Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 156 | */ |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 157 | if (check_recursive_alloc(entries, nr_entries, _RET_IP_)) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 158 | return dummy_handle; |
| 159 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 160 | handle = stack_depot_save(entries, nr_entries, flags); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 161 | if (!handle) |
| 162 | handle = failure_handle; |
| 163 | |
| 164 | return handle; |
| 165 | } |
| 166 | |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 167 | void __reset_page_owner(struct page *page, unsigned int order) |
| 168 | { |
| 169 | int i; |
| 170 | struct page_ext *page_ext; |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 171 | depot_stack_handle_t handle = 0; |
| 172 | struct page_owner *page_owner; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 173 | u64 free_ts_nsec = local_clock(); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 174 | |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 175 | handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 176 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 177 | page_ext = page_ext_get(page); |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 178 | if (unlikely(!page_ext)) |
| 179 | return; |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 180 | for (i = 0; i < (1 << order); i++) { |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 181 | __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 182 | page_owner = get_page_owner(page_ext); |
| 183 | page_owner->free_handle = handle; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 184 | page_owner->free_ts_nsec = free_ts_nsec; |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 185 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 186 | } |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 187 | page_ext_put(page_ext); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 190 | static 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 Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 193 | { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 194 | struct page_owner *page_owner; |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 195 | int i; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 196 | |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 197 | 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 Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 203 | page_owner->pid = current->pid; |
| 204 | page_owner->ts_nsec = local_clock(); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 205 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 206 | __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 207 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 208 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 209 | } |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 212 | noinline void __set_page_owner(struct page *page, unsigned int order, |
| 213 | gfp_t gfp_mask) |
| 214 | { |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 215 | struct page_ext *page_ext; |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 216 | depot_stack_handle_t handle; |
| 217 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 218 | handle = save_stack(gfp_mask); |
| 219 | |
| 220 | page_ext = page_ext_get(page); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 221 | if (unlikely(!page_ext)) |
| 222 | return; |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 223 | __set_page_owner_handle(page, page_ext, handle, order, gfp_mask); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 224 | page_ext_put(page_ext); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 225 | } |
Pratyush Brahma | a9c0f62 | 2023-08-17 15:38:28 +0530 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(__set_page_owner); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 227 | |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 228 | void __set_page_owner_migrate_reason(struct page *page, int reason) |
| 229 | { |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 230 | struct page_ext *page_ext = page_ext_get(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 231 | struct page_owner *page_owner; |
| 232 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 233 | if (unlikely(!page_ext)) |
| 234 | return; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 235 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 236 | page_owner = get_page_owner(page_ext); |
| 237 | page_owner->last_migrate_reason = reason; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 238 | page_ext_put(page_ext); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Matthew Wilcox (Oracle) | 8fb156c | 2020-10-15 20:05:29 -0700 | [diff] [blame] | 241 | void __split_page_owner(struct page *page, unsigned int nr) |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 242 | { |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 243 | int i; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 244 | struct page_ext *page_ext = page_ext_get(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 245 | struct page_owner *page_owner; |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 246 | |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 247 | if (unlikely(!page_ext)) |
| 248 | return; |
| 249 | |
Matthew Wilcox (Oracle) | 8fb156c | 2020-10-15 20:05:29 -0700 | [diff] [blame] | 250 | for (i = 0; i < nr; i++) { |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 251 | page_owner = get_page_owner(page_ext); |
| 252 | page_owner->order = 0; |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 253 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 254 | } |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 255 | page_ext_put(page_ext); |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 258 | void __copy_page_owner(struct page *oldpage, struct page *newpage) |
| 259 | { |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 260 | struct page_ext *old_ext; |
| 261 | struct page_ext *new_ext; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 262 | struct page_owner *old_page_owner, *new_page_owner; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 263 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 264 | old_ext = page_ext_get(oldpage); |
| 265 | if (unlikely(!old_ext)) |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 266 | return; |
| 267 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 268 | new_ext = page_ext_get(newpage); |
| 269 | if (unlikely(!new_ext)) { |
| 270 | page_ext_put(old_ext); |
| 271 | return; |
| 272 | } |
| 273 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 274 | 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 Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 281 | new_page_owner->pid = old_page_owner->pid; |
| 282 | new_page_owner->ts_nsec = old_page_owner->ts_nsec; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 283 | new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 284 | |
| 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 Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 295 | __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 296 | page_ext_put(new_ext); |
| 297 | page_ext_put(old_ext); |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 300 | void 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 Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 305 | struct page_owner *page_owner; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 306 | 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 Cai | a26ee56 | 2019-10-18 20:19:29 -0700 | [diff] [blame] | 321 | page = pfn_to_online_page(pfn); |
| 322 | if (!page) { |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 323 | 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 Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 330 | pageblock_mt = get_pageblock_migratetype(page); |
| 331 | |
| 332 | for (; pfn < block_end_pfn; pfn++) { |
| 333 | if (!pfn_valid_within(pfn)) |
| 334 | continue; |
| 335 | |
Qian Cai | a26ee56 | 2019-10-18 20:19:29 -0700 | [diff] [blame] | 336 | /* The pageblock is online, no need to recheck. */ |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 337 | page = pfn_to_page(pfn); |
| 338 | |
| 339 | if (page_zone(page) != zone) |
| 340 | continue; |
| 341 | |
| 342 | if (PageBuddy(page)) { |
Vinayak Menon | 727c080 | 2017-07-10 15:49:17 -0700 | [diff] [blame] | 343 | unsigned long freepage_order; |
| 344 | |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 345 | freepage_order = buddy_order_unsafe(page); |
Vinayak Menon | 727c080 | 2017-07-10 15:49:17 -0700 | [diff] [blame] | 346 | if (freepage_order < MAX_ORDER) |
| 347 | pfn += (1UL << freepage_order) - 1; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
| 351 | if (PageReserved(page)) |
| 352 | continue; |
| 353 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 354 | page_ext = page_ext_get(page); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 355 | if (unlikely(!page_ext)) |
| 356 | continue; |
| 357 | |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 358 | if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 359 | goto ext_put_continue; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 360 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 361 | page_owner = get_page_owner(page_ext); |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 362 | page_mt = gfp_migratetype(page_owner->gfp_mask); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 363 | 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 Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 370 | page_ext_put(page_ext); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 371 | break; |
| 372 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 373 | pfn += (1UL << page_owner->order) - 1; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 374 | ext_put_continue: |
| 375 | page_ext_put(page_ext); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 376 | } |
| 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 Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 386 | static ssize_t |
| 387 | print_page_owner(char __user *buf, size_t count, unsigned long pfn, |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 388 | struct page *page, struct page_owner *page_owner, |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 389 | depot_stack_handle_t handle) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 390 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 391 | int ret, pageblock_mt, page_mt; |
| 392 | unsigned long *entries; |
| 393 | unsigned int nr_entries; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 394 | char *kbuf; |
| 395 | |
Miles Chen | c8f61cf | 2018-12-28 00:33:21 -0800 | [diff] [blame] | 396 | count = min_t(size_t, count, PAGE_SIZE); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 397 | kbuf = kmalloc(count, GFP_KERNEL); |
| 398 | if (!kbuf) |
| 399 | return -ENOMEM; |
| 400 | |
| 401 | ret = snprintf(kbuf, count, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 402 | "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 403 | page_owner->order, page_owner->gfp_mask, |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 404 | &page_owner->gfp_mask, page_owner->pid, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 405 | page_owner->ts_nsec, page_owner->free_ts_nsec); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 406 | |
| 407 | if (ret >= count) |
| 408 | goto err; |
| 409 | |
| 410 | /* Print information relevant to grouping pages by mobility */ |
Mel Gorman | 0b423ca | 2016-05-19 17:14:27 -0700 | [diff] [blame] | 411 | pageblock_mt = get_pageblock_migratetype(page); |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 412 | page_mt = gfp_migratetype(page_owner->gfp_mask); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 413 | ret += snprintf(kbuf + ret, count - ret, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 414 | "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n", |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 415 | pfn, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 416 | migratetype_names[page_mt], |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 417 | pfn >> pageblock_order, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 418 | migratetype_names[pageblock_mt], |
| 419 | page->flags, &page->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 420 | |
| 421 | if (ret >= count) |
| 422 | goto err; |
| 423 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 424 | nr_entries = stack_depot_fetch(handle, &entries); |
| 425 | ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 426 | if (ret >= count) |
| 427 | goto err; |
| 428 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 429 | if (page_owner->last_migrate_reason != -1) { |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 430 | ret += snprintf(kbuf + ret, count - ret, |
| 431 | "Page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 432 | migrate_reason_names[page_owner->last_migrate_reason]); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 433 | if (ret >= count) |
| 434 | goto err; |
| 435 | } |
| 436 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 437 | 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 | |
| 447 | err: |
| 448 | kfree(kbuf); |
| 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 452 | void __dump_page_owner(struct page *page) |
| 453 | { |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 454 | struct page_ext *page_ext = page_ext_get((void *)page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 455 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 456 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 457 | unsigned long *entries; |
| 458 | unsigned int nr_entries; |
Sudip Mukherjee | 8285027 | 2016-06-24 14:50:24 -0700 | [diff] [blame] | 459 | gfp_t gfp_mask; |
| 460 | int mt; |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 461 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 462 | if (unlikely(!page_ext)) { |
| 463 | pr_alert("There is not page extension available.\n"); |
| 464 | return; |
| 465 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 466 | |
| 467 | page_owner = get_page_owner(page_ext); |
| 468 | gfp_mask = page_owner->gfp_mask; |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 469 | mt = gfp_migratetype(gfp_mask); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 470 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 471 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 472 | pr_alert("page_owner info is not present (never set?)\n"); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 473 | page_ext_put(page_ext); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 474 | return; |
| 475 | } |
| 476 | |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 477 | if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 478 | 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 Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 482 | pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu, free_ts %llu\n", |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 483 | page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 484 | page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec); |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 485 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 486 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 487 | if (!handle) { |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 488 | 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 Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 494 | 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 Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 502 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 503 | if (page_owner->last_migrate_reason != -1) |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 504 | pr_alert("page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 505 | migrate_reason_names[page_owner->last_migrate_reason]); |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 506 | page_ext_put(page_ext); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 509 | static ssize_t |
| 510 | read_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 Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 515 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 516 | depot_stack_handle_t handle; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 517 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 518 | if (!static_branch_unlikely(&page_owner_inited)) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 519 | 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 Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 533 | * 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 Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 541 | * 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) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 555 | unsigned long freepage_order = buddy_order_unsafe(page); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 556 | |
| 557 | if (freepage_order < MAX_ORDER) |
| 558 | pfn += (1UL << freepage_order) - 1; |
| 559 | continue; |
| 560 | } |
| 561 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 562 | page_ext = page_ext_get(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 563 | if (unlikely(!page_ext)) |
| 564 | continue; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 565 | |
| 566 | /* |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 567 | * Some pages could be missed by concurrent allocation or free, |
| 568 | * because we don't hold the zone lock. |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 569 | */ |
| 570 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 571 | goto ext_put_continue; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 572 | |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 573 | /* |
| 574 | * Although we do have the info about past allocation of free |
| 575 | * pages, it's not relevant for current memory usage. |
| 576 | */ |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 577 | if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 578 | goto ext_put_continue; |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 579 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 580 | page_owner = get_page_owner(page_ext); |
| 581 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 582 | /* |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 583 | * 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 Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 587 | goto ext_put_continue; |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 590 | * Access to page_ext->handle isn't synchronous so we should |
| 591 | * be careful to access it. |
| 592 | */ |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 593 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 594 | if (!handle) |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 595 | goto ext_put_continue; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 596 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 597 | /* Record the next PFN to read in the file offset */ |
| 598 | *ppos = (pfn - min_low_pfn) + 1; |
| 599 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 600 | page_owner_tmp = *page_owner; |
| 601 | page_ext_put(page_ext); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 602 | return print_page_owner(buf, count, pfn, page, |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 603 | &page_owner_tmp, handle); |
| 604 | ext_put_continue: |
| 605 | page_ext_put(page_ext); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 611 | static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) |
| 612 | { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 613 | unsigned long pfn = zone->zone_start_pfn; |
| 614 | unsigned long end_pfn = zone_end_pfn(zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 615 | unsigned long count = 0; |
| 616 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 617 | /* |
| 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 Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 623 | unsigned long block_end_pfn; |
| 624 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 625 | 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 Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 633 | for (; pfn < block_end_pfn; pfn++) { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 634 | struct page *page; |
| 635 | struct page_ext *page_ext; |
| 636 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 637 | if (!pfn_valid_within(pfn)) |
| 638 | continue; |
| 639 | |
| 640 | page = pfn_to_page(pfn); |
| 641 | |
Joonsoo Kim | 9d43f5a | 2016-05-19 17:12:13 -0700 | [diff] [blame] | 642 | if (page_zone(page) != zone) |
| 643 | continue; |
| 644 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 645 | /* |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 646 | * 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 Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 651 | */ |
| 652 | if (PageBuddy(page)) { |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 653 | unsigned long order = buddy_order_unsafe(page); |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 654 | |
| 655 | if (order > 0 && order < MAX_ORDER) |
| 656 | pfn += (1UL << order) - 1; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 657 | continue; |
| 658 | } |
| 659 | |
| 660 | if (PageReserved(page)) |
| 661 | continue; |
| 662 | |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 663 | page_ext = page_ext_get(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 664 | if (unlikely(!page_ext)) |
| 665 | continue; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 666 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 667 | /* Maybe overlapping zone */ |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 668 | if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 669 | goto ext_put_continue; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 670 | |
| 671 | /* Found early allocated page */ |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 672 | __set_page_owner_handle(page, page_ext, early_handle, |
| 673 | 0, 0); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 674 | count++; |
Charan Teja Kalla | 2b3f9b8 | 2022-08-18 19:20:00 +0530 | [diff] [blame] | 675 | ext_put_continue: |
| 676 | page_ext_put(page_ext); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 677 | } |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 678 | cond_resched(); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 679 | } |
| 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 | |
| 685 | static void init_zones_in_node(pg_data_t *pgdat) |
| 686 | { |
| 687 | struct zone *zone; |
| 688 | struct zone *node_zones = pgdat->node_zones; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 689 | |
| 690 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { |
| 691 | if (!populated_zone(zone)) |
| 692 | continue; |
| 693 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 694 | init_pages_in_zone(pgdat, zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | static void init_early_allocated_pages(void) |
| 699 | { |
| 700 | pg_data_t *pgdat; |
| 701 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 702 | for_each_online_pgdat(pgdat) |
| 703 | init_zones_in_node(pgdat); |
| 704 | } |
| 705 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 706 | static const struct file_operations proc_page_owner_operations = { |
| 707 | .read = read_page_owner, |
| 708 | }; |
| 709 | |
| 710 | static int __init pageowner_init(void) |
| 711 | { |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 712 | if (!static_branch_unlikely(&page_owner_inited)) { |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 713 | pr_info("page_owner is disabled\n"); |
| 714 | return 0; |
| 715 | } |
| 716 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 717 | debugfs_create_file("page_owner", 0400, NULL, NULL, |
| 718 | &proc_page_owner_operations); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 719 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 720 | return 0; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 721 | } |
Paul Gortmaker | 44c5af9 | 2015-05-01 21:57:34 -0400 | [diff] [blame] | 722 | late_initcall(pageowner_init) |