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); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 36 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 37 | static depot_stack_handle_t dummy_handle; |
| 38 | static depot_stack_handle_t failure_handle; |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 39 | static depot_stack_handle_t early_handle; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 40 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 41 | static void init_early_allocated_pages(void); |
| 42 | |
Dou Liyang | 1173194 | 2018-04-05 16:23:49 -0700 | [diff] [blame] | 43 | static int __init early_page_owner_param(char *buf) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 44 | { |
| 45 | if (!buf) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | if (strcmp(buf, "on") == 0) |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 49 | page_owner_enabled = true; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | early_param("page_owner", early_page_owner_param); |
| 54 | |
| 55 | static bool need_page_owner(void) |
| 56 | { |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 57 | return page_owner_enabled; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 60 | static __always_inline depot_stack_handle_t create_dummy_stack(void) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 61 | { |
| 62 | unsigned long entries[4]; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 63 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 64 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 65 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); |
| 66 | return stack_depot_save(entries, nr_entries, GFP_KERNEL); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static noinline void register_dummy_stack(void) |
| 70 | { |
| 71 | dummy_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static noinline void register_failure_stack(void) |
| 75 | { |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 76 | failure_handle = create_dummy_stack(); |
| 77 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 78 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 79 | static noinline void register_early_stack(void) |
| 80 | { |
| 81 | early_handle = create_dummy_stack(); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 84 | static void init_page_owner(void) |
| 85 | { |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 86 | if (!page_owner_enabled) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 87 | return; |
| 88 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 89 | register_dummy_stack(); |
| 90 | register_failure_stack(); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 91 | register_early_stack(); |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 92 | static_branch_enable(&page_owner_inited); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 93 | init_early_allocated_pages(); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | struct page_ext_operations page_owner_ops = { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 97 | .size = sizeof(struct page_owner), |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 98 | .need = need_page_owner, |
| 99 | .init = init_page_owner, |
| 100 | }; |
| 101 | |
Vijayanand Jitta | b76264c | 2021-01-05 11:33:53 +0530 | [diff] [blame] | 102 | struct page_owner *get_page_owner(struct page_ext *page_ext) |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 103 | { |
| 104 | return (void *)page_ext + page_owner_ops.offset; |
| 105 | } |
Vijayanand Jitta | b76264c | 2021-01-05 11:33:53 +0530 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(get_page_owner); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 107 | |
Vijayanand Jitta | 8f30208 | 2021-01-12 00:15:31 +0530 | [diff] [blame] | 108 | depot_stack_handle_t get_page_owner_handle(struct page_ext *page_ext, unsigned long pfn) |
| 109 | { |
| 110 | struct page_owner *page_owner; |
| 111 | depot_stack_handle_t handle; |
| 112 | |
| 113 | if (!page_owner_enabled) |
| 114 | return 0; |
| 115 | |
| 116 | page_owner = get_page_owner(page_ext); |
| 117 | |
| 118 | /* skip handle for tail pages of higher order allocations */ |
| 119 | if (!IS_ALIGNED(pfn, 1 << page_owner->order)) |
| 120 | return 0; |
| 121 | |
| 122 | handle = READ_ONCE(page_owner->handle); |
| 123 | return handle; |
| 124 | } |
| 125 | EXPORT_SYMBOL_GPL(get_page_owner_handle); |
| 126 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 127 | static inline bool check_recursive_alloc(unsigned long *entries, |
| 128 | unsigned int nr_entries, |
| 129 | unsigned long ip) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 130 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 131 | unsigned int i; |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 132 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 133 | for (i = 0; i < nr_entries; i++) { |
| 134 | if (entries[i] == ip) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | static noinline depot_stack_handle_t save_stack(gfp_t flags) |
| 141 | { |
| 142 | unsigned long entries[PAGE_OWNER_STACK_DEPTH]; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 143 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 144 | unsigned int nr_entries; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 145 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 146 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 147 | |
| 148 | /* |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 149 | * We need to check recursion here because our request to |
| 150 | * stackdepot could trigger memory allocation to save new |
| 151 | * entry. New memory allocation would reach here and call |
| 152 | * stack_depot_save_entries() again if we don't catch it. There is |
| 153 | * still not enough memory in stackdepot so it would try to |
| 154 | * allocate memory again and loop forever. |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 155 | */ |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 156 | if (check_recursive_alloc(entries, nr_entries, _RET_IP_)) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 157 | return dummy_handle; |
| 158 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 159 | handle = stack_depot_save(entries, nr_entries, flags); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 160 | if (!handle) |
| 161 | handle = failure_handle; |
| 162 | |
| 163 | return handle; |
| 164 | } |
| 165 | |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 166 | void __reset_page_owner(struct page *page, unsigned int order) |
| 167 | { |
| 168 | int i; |
| 169 | struct page_ext *page_ext; |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 170 | depot_stack_handle_t handle = 0; |
| 171 | struct page_owner *page_owner; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 172 | u64 free_ts_nsec = local_clock(); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 173 | |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 174 | handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 175 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 176 | page_ext = lookup_page_ext(page); |
| 177 | if (unlikely(!page_ext)) |
| 178 | return; |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 179 | for (i = 0; i < (1 << order); i++) { |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 180 | __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 181 | page_owner = get_page_owner(page_ext); |
| 182 | page_owner->free_handle = handle; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 183 | page_owner->free_ts_nsec = free_ts_nsec; |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 184 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 188 | static inline void __set_page_owner_handle(struct page *page, |
| 189 | struct page_ext *page_ext, depot_stack_handle_t handle, |
| 190 | unsigned int order, gfp_t gfp_mask) |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 191 | { |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 192 | struct page_owner *page_owner; |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 193 | int i; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 194 | |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 195 | for (i = 0; i < (1 << order); i++) { |
| 196 | page_owner = get_page_owner(page_ext); |
| 197 | page_owner->handle = handle; |
| 198 | page_owner->order = order; |
| 199 | page_owner->gfp_mask = gfp_mask; |
| 200 | page_owner->last_migrate_reason = -1; |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 201 | page_owner->pid = current->pid; |
| 202 | page_owner->ts_nsec = local_clock(); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 203 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 204 | __set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 205 | |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 206 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 207 | } |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 210 | noinline void __set_page_owner(struct page *page, unsigned int order, |
| 211 | gfp_t gfp_mask) |
| 212 | { |
| 213 | struct page_ext *page_ext = lookup_page_ext(page); |
| 214 | depot_stack_handle_t handle; |
| 215 | |
| 216 | if (unlikely(!page_ext)) |
| 217 | return; |
| 218 | |
| 219 | handle = save_stack(gfp_mask); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 220 | __set_page_owner_handle(page, page_ext, handle, order, gfp_mask); |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 223 | void __set_page_owner_migrate_reason(struct page *page, int reason) |
| 224 | { |
| 225 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 226 | struct page_owner *page_owner; |
| 227 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 228 | if (unlikely(!page_ext)) |
| 229 | return; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 230 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 231 | page_owner = get_page_owner(page_ext); |
| 232 | page_owner->last_migrate_reason = reason; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Matthew Wilcox (Oracle) | 8fb156c | 2020-10-15 20:05:29 -0700 | [diff] [blame] | 235 | void __split_page_owner(struct page *page, unsigned int nr) |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 236 | { |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 237 | int i; |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 238 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 239 | struct page_owner *page_owner; |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 240 | |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 241 | if (unlikely(!page_ext)) |
| 242 | return; |
| 243 | |
Matthew Wilcox (Oracle) | 8fb156c | 2020-10-15 20:05:29 -0700 | [diff] [blame] | 244 | for (i = 0; i < nr; i++) { |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 245 | page_owner = get_page_owner(page_ext); |
| 246 | page_owner->order = 0; |
Vlastimil Babka | 5556cfe | 2019-10-14 14:11:40 -0700 | [diff] [blame] | 247 | page_ext = page_ext_next(page_ext); |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 248 | } |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 251 | void __copy_page_owner(struct page *oldpage, struct page *newpage) |
| 252 | { |
| 253 | struct page_ext *old_ext = lookup_page_ext(oldpage); |
| 254 | struct page_ext *new_ext = lookup_page_ext(newpage); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 255 | struct page_owner *old_page_owner, *new_page_owner; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 256 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 257 | if (unlikely(!old_ext || !new_ext)) |
| 258 | return; |
| 259 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 260 | old_page_owner = get_page_owner(old_ext); |
| 261 | new_page_owner = get_page_owner(new_ext); |
| 262 | new_page_owner->order = old_page_owner->order; |
| 263 | new_page_owner->gfp_mask = old_page_owner->gfp_mask; |
| 264 | new_page_owner->last_migrate_reason = |
| 265 | old_page_owner->last_migrate_reason; |
| 266 | new_page_owner->handle = old_page_owner->handle; |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 267 | new_page_owner->pid = old_page_owner->pid; |
| 268 | new_page_owner->ts_nsec = old_page_owner->ts_nsec; |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 269 | new_page_owner->free_ts_nsec = old_page_owner->ts_nsec; |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * We don't clear the bit on the oldpage as it's going to be freed |
| 273 | * after migration. Until then, the info can be useful in case of |
| 274 | * a bug, and the overal stats will be off a bit only temporarily. |
| 275 | * Also, migrate_misplaced_transhuge_page() can still fail the |
| 276 | * migration and then we want the oldpage to retain the info. But |
| 277 | * in that case we also don't need to explicitly clear the info from |
| 278 | * the new page, which will be freed. |
| 279 | */ |
| 280 | __set_bit(PAGE_EXT_OWNER, &new_ext->flags); |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 281 | __set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags); |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 284 | void pagetypeinfo_showmixedcount_print(struct seq_file *m, |
| 285 | pg_data_t *pgdat, struct zone *zone) |
| 286 | { |
| 287 | struct page *page; |
| 288 | struct page_ext *page_ext; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 289 | struct page_owner *page_owner; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 290 | unsigned long pfn = zone->zone_start_pfn, block_end_pfn; |
| 291 | unsigned long end_pfn = pfn + zone->spanned_pages; |
| 292 | unsigned long count[MIGRATE_TYPES] = { 0, }; |
| 293 | int pageblock_mt, page_mt; |
| 294 | int i; |
| 295 | |
| 296 | /* Scan block by block. First and last block may be incomplete */ |
| 297 | pfn = zone->zone_start_pfn; |
| 298 | |
| 299 | /* |
| 300 | * Walk the zone in pageblock_nr_pages steps. If a page block spans |
| 301 | * a zone boundary, it will be double counted between zones. This does |
| 302 | * not matter as the mixed block count will still be correct |
| 303 | */ |
| 304 | for (; pfn < end_pfn; ) { |
Qian Cai | a26ee56 | 2019-10-18 20:19:29 -0700 | [diff] [blame] | 305 | page = pfn_to_online_page(pfn); |
| 306 | if (!page) { |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 307 | pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 312 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 313 | |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 314 | pageblock_mt = get_pageblock_migratetype(page); |
| 315 | |
| 316 | for (; pfn < block_end_pfn; pfn++) { |
| 317 | if (!pfn_valid_within(pfn)) |
| 318 | continue; |
| 319 | |
Qian Cai | a26ee56 | 2019-10-18 20:19:29 -0700 | [diff] [blame] | 320 | /* The pageblock is online, no need to recheck. */ |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 321 | page = pfn_to_page(pfn); |
| 322 | |
| 323 | if (page_zone(page) != zone) |
| 324 | continue; |
| 325 | |
| 326 | if (PageBuddy(page)) { |
Vinayak Menon | 727c080 | 2017-07-10 15:49:17 -0700 | [diff] [blame] | 327 | unsigned long freepage_order; |
| 328 | |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 329 | freepage_order = buddy_order_unsafe(page); |
Vinayak Menon | 727c080 | 2017-07-10 15:49:17 -0700 | [diff] [blame] | 330 | if (freepage_order < MAX_ORDER) |
| 331 | pfn += (1UL << freepage_order) - 1; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 332 | continue; |
| 333 | } |
| 334 | |
| 335 | if (PageReserved(page)) |
| 336 | continue; |
| 337 | |
| 338 | page_ext = lookup_page_ext(page); |
| 339 | if (unlikely(!page_ext)) |
| 340 | continue; |
| 341 | |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 342 | if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 343 | continue; |
| 344 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 345 | page_owner = get_page_owner(page_ext); |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 346 | page_mt = gfp_migratetype(page_owner->gfp_mask); |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 347 | if (pageblock_mt != page_mt) { |
| 348 | if (is_migrate_cma(pageblock_mt)) |
| 349 | count[MIGRATE_MOVABLE]++; |
| 350 | else |
| 351 | count[pageblock_mt]++; |
| 352 | |
| 353 | pfn = block_end_pfn; |
| 354 | break; |
| 355 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 356 | pfn += (1UL << page_owner->order) - 1; |
Joonsoo Kim | e2f612e | 2016-10-07 16:58:21 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Print counts */ |
| 361 | seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); |
| 362 | for (i = 0; i < MIGRATE_TYPES; i++) |
| 363 | seq_printf(m, "%12lu ", count[i]); |
| 364 | seq_putc(m, '\n'); |
| 365 | } |
| 366 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 367 | static ssize_t |
| 368 | print_page_owner(char __user *buf, size_t count, unsigned long pfn, |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 369 | struct page *page, struct page_owner *page_owner, |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 370 | depot_stack_handle_t handle) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 371 | { |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 372 | int ret, pageblock_mt, page_mt; |
| 373 | unsigned long *entries; |
| 374 | unsigned int nr_entries; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 375 | char *kbuf; |
| 376 | |
Miles Chen | c8f61cf | 2018-12-28 00:33:21 -0800 | [diff] [blame] | 377 | count = min_t(size_t, count, PAGE_SIZE); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 378 | kbuf = kmalloc(count, GFP_KERNEL); |
| 379 | if (!kbuf) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | ret = snprintf(kbuf, count, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 383 | "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] | 384 | page_owner->order, page_owner->gfp_mask, |
Liam Mark | fd0328e | 2020-12-14 19:04:49 -0800 | [diff] [blame] | 385 | &page_owner->gfp_mask, page_owner->pid, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 386 | page_owner->ts_nsec, page_owner->free_ts_nsec); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 387 | |
| 388 | if (ret >= count) |
| 389 | goto err; |
| 390 | |
| 391 | /* Print information relevant to grouping pages by mobility */ |
Mel Gorman | 0b423ca | 2016-05-19 17:14:27 -0700 | [diff] [blame] | 392 | pageblock_mt = get_pageblock_migratetype(page); |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 393 | page_mt = gfp_migratetype(page_owner->gfp_mask); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 394 | ret += snprintf(kbuf + ret, count - ret, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 395 | "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n", |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 396 | pfn, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 397 | migratetype_names[page_mt], |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 398 | pfn >> pageblock_order, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 399 | migratetype_names[pageblock_mt], |
| 400 | page->flags, &page->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 401 | |
| 402 | if (ret >= count) |
| 403 | goto err; |
| 404 | |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 405 | nr_entries = stack_depot_fetch(handle, &entries); |
| 406 | ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 407 | if (ret >= count) |
| 408 | goto err; |
| 409 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 410 | if (page_owner->last_migrate_reason != -1) { |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 411 | ret += snprintf(kbuf + ret, count - ret, |
| 412 | "Page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 413 | migrate_reason_names[page_owner->last_migrate_reason]); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 414 | if (ret >= count) |
| 415 | goto err; |
| 416 | } |
| 417 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 418 | ret += snprintf(kbuf + ret, count - ret, "\n"); |
| 419 | if (ret >= count) |
| 420 | goto err; |
| 421 | |
| 422 | if (copy_to_user(buf, kbuf, ret)) |
| 423 | ret = -EFAULT; |
| 424 | |
| 425 | kfree(kbuf); |
| 426 | return ret; |
| 427 | |
| 428 | err: |
| 429 | kfree(kbuf); |
| 430 | return -ENOMEM; |
| 431 | } |
| 432 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 433 | void __dump_page_owner(struct page *page) |
| 434 | { |
| 435 | struct page_ext *page_ext = lookup_page_ext(page); |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 436 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 437 | depot_stack_handle_t handle; |
Thomas Gleixner | af52bf6 | 2019-04-25 11:45:03 +0200 | [diff] [blame] | 438 | unsigned long *entries; |
| 439 | unsigned int nr_entries; |
Sudip Mukherjee | 8285027 | 2016-06-24 14:50:24 -0700 | [diff] [blame] | 440 | gfp_t gfp_mask; |
| 441 | int mt; |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 442 | |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 443 | if (unlikely(!page_ext)) { |
| 444 | pr_alert("There is not page extension available.\n"); |
| 445 | return; |
| 446 | } |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 447 | |
| 448 | page_owner = get_page_owner(page_ext); |
| 449 | gfp_mask = page_owner->gfp_mask; |
Wei Yang | 01c0bfe | 2020-06-03 15:59:08 -0700 | [diff] [blame] | 450 | mt = gfp_migratetype(gfp_mask); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 451 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 452 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 453 | pr_alert("page_owner info is not present (never set?)\n"); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 457 | if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 458 | pr_alert("page_owner tracks the page as allocated\n"); |
| 459 | else |
| 460 | pr_alert("page_owner tracks the page as freed\n"); |
| 461 | |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 462 | 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] | 463 | page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask, |
Georgi Djakov | f8765be | 2021-03-24 10:15:47 -0700 | [diff] [blame] | 464 | page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec); |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 465 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 466 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 467 | if (!handle) { |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 468 | pr_alert("page_owner allocation stack trace missing\n"); |
| 469 | } else { |
| 470 | nr_entries = stack_depot_fetch(handle, &entries); |
| 471 | stack_trace_print(entries, nr_entries, 0); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 474 | handle = READ_ONCE(page_owner->free_handle); |
| 475 | if (!handle) { |
| 476 | pr_alert("page_owner free stack trace missing\n"); |
| 477 | } else { |
| 478 | nr_entries = stack_depot_fetch(handle, &entries); |
| 479 | pr_alert("page last free stack trace:\n"); |
| 480 | stack_trace_print(entries, nr_entries, 0); |
| 481 | } |
Vlastimil Babka | 8974558 | 2019-09-23 15:34:42 -0700 | [diff] [blame] | 482 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 483 | if (page_owner->last_migrate_reason != -1) |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 484 | pr_alert("page has been migrated, last migrate reason: %s\n", |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 485 | migrate_reason_names[page_owner->last_migrate_reason]); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 488 | static ssize_t |
| 489 | read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 490 | { |
| 491 | unsigned long pfn; |
| 492 | struct page *page; |
| 493 | struct page_ext *page_ext; |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 494 | struct page_owner *page_owner; |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 495 | depot_stack_handle_t handle; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 496 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 497 | if (!static_branch_unlikely(&page_owner_inited)) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 498 | return -EINVAL; |
| 499 | |
| 500 | page = NULL; |
| 501 | pfn = min_low_pfn + *ppos; |
| 502 | |
| 503 | /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ |
| 504 | while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0) |
| 505 | pfn++; |
| 506 | |
| 507 | drain_all_pages(NULL); |
| 508 | |
| 509 | /* Find an allocated page */ |
| 510 | for (; pfn < max_pfn; pfn++) { |
| 511 | /* |
| 512 | * If the new page is in a new MAX_ORDER_NR_PAGES area, |
| 513 | * validate the area as existing, skip it if not |
| 514 | */ |
| 515 | if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) { |
| 516 | pfn += MAX_ORDER_NR_PAGES - 1; |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | /* Check for holes within a MAX_ORDER area */ |
| 521 | if (!pfn_valid_within(pfn)) |
| 522 | continue; |
| 523 | |
| 524 | page = pfn_to_page(pfn); |
| 525 | if (PageBuddy(page)) { |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 526 | unsigned long freepage_order = buddy_order_unsafe(page); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 527 | |
| 528 | if (freepage_order < MAX_ORDER) |
| 529 | pfn += (1UL << freepage_order) - 1; |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 534 | if (unlikely(!page_ext)) |
| 535 | continue; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 536 | |
| 537 | /* |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 538 | * Some pages could be missed by concurrent allocation or free, |
| 539 | * because we don't hold the zone lock. |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 540 | */ |
| 541 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 542 | continue; |
| 543 | |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 544 | /* |
| 545 | * Although we do have the info about past allocation of free |
| 546 | * pages, it's not relevant for current memory usage. |
| 547 | */ |
Vlastimil Babka | fdf3bf8 | 2019-10-14 14:11:47 -0700 | [diff] [blame] | 548 | if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags)) |
Vlastimil Babka | 37389167 | 2019-09-23 15:34:39 -0700 | [diff] [blame] | 549 | continue; |
| 550 | |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 551 | page_owner = get_page_owner(page_ext); |
| 552 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 553 | /* |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 554 | * Don't print "tail" pages of high-order allocations as that |
| 555 | * would inflate the stats. |
| 556 | */ |
| 557 | if (!IS_ALIGNED(pfn, 1 << page_owner->order)) |
| 558 | continue; |
| 559 | |
| 560 | /* |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 561 | * Access to page_ext->handle isn't synchronous so we should |
| 562 | * be careful to access it. |
| 563 | */ |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 564 | handle = READ_ONCE(page_owner->handle); |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 565 | if (!handle) |
| 566 | continue; |
| 567 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 568 | /* Record the next PFN to read in the file offset */ |
| 569 | *ppos = (pfn - min_low_pfn) + 1; |
| 570 | |
Joonsoo Kim | f2ca0b5 | 2016-07-26 15:23:55 -0700 | [diff] [blame] | 571 | return print_page_owner(buf, count, pfn, page, |
Joonsoo Kim | 9300d8d | 2016-10-07 16:58:30 -0700 | [diff] [blame] | 572 | page_owner, handle); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 578 | static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) |
| 579 | { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 580 | unsigned long pfn = zone->zone_start_pfn; |
| 581 | unsigned long end_pfn = zone_end_pfn(zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 582 | unsigned long count = 0; |
| 583 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 584 | /* |
| 585 | * Walk the zone in pageblock_nr_pages steps. If a page block spans |
| 586 | * a zone boundary, it will be double counted between zones. This does |
| 587 | * not matter as the mixed block count will still be correct |
| 588 | */ |
| 589 | for (; pfn < end_pfn; ) { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 590 | unsigned long block_end_pfn; |
| 591 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 592 | if (!pfn_valid(pfn)) { |
| 593 | pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); |
| 594 | continue; |
| 595 | } |
| 596 | |
| 597 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 598 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 599 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 600 | for (; pfn < block_end_pfn; pfn++) { |
Oscar Salvador | 6787c1d | 2018-01-31 16:20:11 -0800 | [diff] [blame] | 601 | struct page *page; |
| 602 | struct page_ext *page_ext; |
| 603 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 604 | if (!pfn_valid_within(pfn)) |
| 605 | continue; |
| 606 | |
| 607 | page = pfn_to_page(pfn); |
| 608 | |
Joonsoo Kim | 9d43f5a | 2016-05-19 17:12:13 -0700 | [diff] [blame] | 609 | if (page_zone(page) != zone) |
| 610 | continue; |
| 611 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 612 | /* |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 613 | * To avoid having to grab zone->lock, be a little |
| 614 | * careful when reading buddy page order. The only |
| 615 | * danger is that we skip too much and potentially miss |
| 616 | * some early allocated pages, which is better than |
| 617 | * heavy lock contention. |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 618 | */ |
| 619 | if (PageBuddy(page)) { |
Matthew Wilcox (Oracle) | ab130f91 | 2020-10-15 20:10:15 -0700 | [diff] [blame] | 620 | unsigned long order = buddy_order_unsafe(page); |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 621 | |
| 622 | if (order > 0 && order < MAX_ORDER) |
| 623 | pfn += (1UL << order) - 1; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 624 | continue; |
| 625 | } |
| 626 | |
| 627 | if (PageReserved(page)) |
| 628 | continue; |
| 629 | |
| 630 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 631 | if (unlikely(!page_ext)) |
| 632 | continue; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 633 | |
Vlastimil Babka | dab4ead | 2017-09-06 16:20:44 -0700 | [diff] [blame] | 634 | /* Maybe overlapping zone */ |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 635 | if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 636 | continue; |
| 637 | |
| 638 | /* Found early allocated page */ |
Vlastimil Babka | 7e2f2a0 | 2019-09-23 15:34:36 -0700 | [diff] [blame] | 639 | __set_page_owner_handle(page, page_ext, early_handle, |
| 640 | 0, 0); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 641 | count++; |
| 642 | } |
Vlastimil Babka | 1090302 | 2017-09-06 16:20:51 -0700 | [diff] [blame] | 643 | cond_resched(); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n", |
| 647 | pgdat->node_id, zone->name, count); |
| 648 | } |
| 649 | |
| 650 | static void init_zones_in_node(pg_data_t *pgdat) |
| 651 | { |
| 652 | struct zone *zone; |
| 653 | struct zone *node_zones = pgdat->node_zones; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 654 | |
| 655 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { |
| 656 | if (!populated_zone(zone)) |
| 657 | continue; |
| 658 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 659 | init_pages_in_zone(pgdat, zone); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
| 663 | static void init_early_allocated_pages(void) |
| 664 | { |
| 665 | pg_data_t *pgdat; |
| 666 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 667 | for_each_online_pgdat(pgdat) |
| 668 | init_zones_in_node(pgdat); |
| 669 | } |
| 670 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 671 | static const struct file_operations proc_page_owner_operations = { |
| 672 | .read = read_page_owner, |
| 673 | }; |
| 674 | |
| 675 | static int __init pageowner_init(void) |
| 676 | { |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 677 | if (!static_branch_unlikely(&page_owner_inited)) { |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 678 | pr_info("page_owner is disabled\n"); |
| 679 | return 0; |
| 680 | } |
| 681 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 682 | debugfs_create_file("page_owner", 0400, NULL, NULL, |
| 683 | &proc_page_owner_operations); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 684 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 685 | return 0; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 686 | } |
Paul Gortmaker | 44c5af9 | 2015-05-01 21:57:34 -0400 | [diff] [blame] | 687 | late_initcall(pageowner_init) |