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