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