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