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