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 | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 10 | #include "internal.h" |
| 11 | |
| 12 | static bool page_owner_disabled = true; |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 13 | DEFINE_STATIC_KEY_FALSE(page_owner_inited); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 14 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 15 | static void init_early_allocated_pages(void); |
| 16 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 17 | static int early_page_owner_param(char *buf) |
| 18 | { |
| 19 | if (!buf) |
| 20 | return -EINVAL; |
| 21 | |
| 22 | if (strcmp(buf, "on") == 0) |
| 23 | page_owner_disabled = false; |
| 24 | |
| 25 | return 0; |
| 26 | } |
| 27 | early_param("page_owner", early_page_owner_param); |
| 28 | |
| 29 | static bool need_page_owner(void) |
| 30 | { |
| 31 | if (page_owner_disabled) |
| 32 | return false; |
| 33 | |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | static void init_page_owner(void) |
| 38 | { |
| 39 | if (page_owner_disabled) |
| 40 | return; |
| 41 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 42 | static_branch_enable(&page_owner_inited); |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 43 | init_early_allocated_pages(); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | struct page_ext_operations page_owner_ops = { |
| 47 | .need = need_page_owner, |
| 48 | .init = init_page_owner, |
| 49 | }; |
| 50 | |
| 51 | void __reset_page_owner(struct page *page, unsigned int order) |
| 52 | { |
| 53 | int i; |
| 54 | struct page_ext *page_ext; |
| 55 | |
| 56 | for (i = 0; i < (1 << order); i++) { |
| 57 | page_ext = lookup_page_ext(page + i); |
| 58 | __clear_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask) |
| 63 | { |
Sergei Rogachev | 94f759d6 | 2015-02-11 15:28:34 -0800 | [diff] [blame] | 64 | struct page_ext *page_ext = lookup_page_ext(page); |
| 65 | struct stack_trace trace = { |
| 66 | .nr_entries = 0, |
| 67 | .max_entries = ARRAY_SIZE(page_ext->trace_entries), |
| 68 | .entries = &page_ext->trace_entries[0], |
| 69 | .skip = 3, |
| 70 | }; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 71 | |
Sergei Rogachev | 94f759d6 | 2015-02-11 15:28:34 -0800 | [diff] [blame] | 72 | save_stack_trace(&trace); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 73 | |
| 74 | page_ext->order = order; |
| 75 | page_ext->gfp_mask = gfp_mask; |
Sergei Rogachev | 94f759d6 | 2015-02-11 15:28:34 -0800 | [diff] [blame] | 76 | page_ext->nr_entries = trace.nr_entries; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 77 | page_ext->last_migrate_reason = -1; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 78 | |
| 79 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 80 | } |
| 81 | |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 82 | void __set_page_owner_migrate_reason(struct page *page, int reason) |
| 83 | { |
| 84 | struct page_ext *page_ext = lookup_page_ext(page); |
| 85 | |
| 86 | page_ext->last_migrate_reason = reason; |
| 87 | } |
| 88 | |
Joonsoo Kim | e2cfc91 | 2015-07-17 16:24:18 -0700 | [diff] [blame] | 89 | gfp_t __get_page_owner_gfp(struct page *page) |
| 90 | { |
| 91 | struct page_ext *page_ext = lookup_page_ext(page); |
| 92 | |
| 93 | return page_ext->gfp_mask; |
| 94 | } |
| 95 | |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 96 | void __copy_page_owner(struct page *oldpage, struct page *newpage) |
| 97 | { |
| 98 | struct page_ext *old_ext = lookup_page_ext(oldpage); |
| 99 | struct page_ext *new_ext = lookup_page_ext(newpage); |
| 100 | int i; |
| 101 | |
| 102 | new_ext->order = old_ext->order; |
| 103 | new_ext->gfp_mask = old_ext->gfp_mask; |
| 104 | new_ext->nr_entries = old_ext->nr_entries; |
| 105 | |
| 106 | for (i = 0; i < ARRAY_SIZE(new_ext->trace_entries); i++) |
| 107 | new_ext->trace_entries[i] = old_ext->trace_entries[i]; |
| 108 | |
| 109 | /* |
| 110 | * We don't clear the bit on the oldpage as it's going to be freed |
| 111 | * after migration. Until then, the info can be useful in case of |
| 112 | * a bug, and the overal stats will be off a bit only temporarily. |
| 113 | * Also, migrate_misplaced_transhuge_page() can still fail the |
| 114 | * migration and then we want the oldpage to retain the info. But |
| 115 | * in that case we also don't need to explicitly clear the info from |
| 116 | * the new page, which will be freed. |
| 117 | */ |
| 118 | __set_bit(PAGE_EXT_OWNER, &new_ext->flags); |
| 119 | } |
| 120 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 121 | static ssize_t |
| 122 | print_page_owner(char __user *buf, size_t count, unsigned long pfn, |
| 123 | struct page *page, struct page_ext *page_ext) |
| 124 | { |
| 125 | int ret; |
| 126 | int pageblock_mt, page_mt; |
| 127 | char *kbuf; |
Sergei Rogachev | 94f759d6 | 2015-02-11 15:28:34 -0800 | [diff] [blame] | 128 | struct stack_trace trace = { |
| 129 | .nr_entries = page_ext->nr_entries, |
| 130 | .entries = &page_ext->trace_entries[0], |
| 131 | }; |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 132 | |
| 133 | kbuf = kmalloc(count, GFP_KERNEL); |
| 134 | if (!kbuf) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | ret = snprintf(kbuf, count, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 138 | "Page allocated via order %u, mask %#x(%pGg)\n", |
| 139 | page_ext->order, page_ext->gfp_mask, |
| 140 | &page_ext->gfp_mask); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 141 | |
| 142 | if (ret >= count) |
| 143 | goto err; |
| 144 | |
| 145 | /* Print information relevant to grouping pages by mobility */ |
Mel Gorman | 0b423ca | 2016-05-19 17:14:27 -0700 | [diff] [blame] | 146 | pageblock_mt = get_pageblock_migratetype(page); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 147 | page_mt = gfpflags_to_migratetype(page_ext->gfp_mask); |
| 148 | ret += snprintf(kbuf + ret, count - ret, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 149 | "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n", |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 150 | pfn, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 151 | migratetype_names[page_mt], |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 152 | pfn >> pageblock_order, |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 153 | migratetype_names[pageblock_mt], |
| 154 | page->flags, &page->flags); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 155 | |
| 156 | if (ret >= count) |
| 157 | goto err; |
| 158 | |
Sergei Rogachev | 94f759d6 | 2015-02-11 15:28:34 -0800 | [diff] [blame] | 159 | ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 160 | if (ret >= count) |
| 161 | goto err; |
| 162 | |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 163 | if (page_ext->last_migrate_reason != -1) { |
| 164 | ret += snprintf(kbuf + ret, count - ret, |
| 165 | "Page has been migrated, last migrate reason: %s\n", |
| 166 | migrate_reason_names[page_ext->last_migrate_reason]); |
| 167 | if (ret >= count) |
| 168 | goto err; |
| 169 | } |
| 170 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 171 | ret += snprintf(kbuf + ret, count - ret, "\n"); |
| 172 | if (ret >= count) |
| 173 | goto err; |
| 174 | |
| 175 | if (copy_to_user(buf, kbuf, ret)) |
| 176 | ret = -EFAULT; |
| 177 | |
| 178 | kfree(kbuf); |
| 179 | return ret; |
| 180 | |
| 181 | err: |
| 182 | kfree(kbuf); |
| 183 | return -ENOMEM; |
| 184 | } |
| 185 | |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 186 | void __dump_page_owner(struct page *page) |
| 187 | { |
| 188 | struct page_ext *page_ext = lookup_page_ext(page); |
| 189 | struct stack_trace trace = { |
| 190 | .nr_entries = page_ext->nr_entries, |
| 191 | .entries = &page_ext->trace_entries[0], |
| 192 | }; |
| 193 | gfp_t gfp_mask = page_ext->gfp_mask; |
| 194 | int mt = gfpflags_to_migratetype(gfp_mask); |
| 195 | |
| 196 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) { |
| 197 | pr_alert("page_owner info is not active (free page?)\n"); |
| 198 | return; |
| 199 | } |
| 200 | |
Joe Perches | 756a025f0 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 201 | pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n", |
| 202 | page_ext->order, migratetype_names[mt], gfp_mask, &gfp_mask); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 203 | print_stack_trace(&trace, 0); |
| 204 | |
| 205 | if (page_ext->last_migrate_reason != -1) |
| 206 | pr_alert("page has been migrated, last migrate reason: %s\n", |
| 207 | migrate_reason_names[page_ext->last_migrate_reason]); |
| 208 | } |
| 209 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 210 | static ssize_t |
| 211 | read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 212 | { |
| 213 | unsigned long pfn; |
| 214 | struct page *page; |
| 215 | struct page_ext *page_ext; |
| 216 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 217 | if (!static_branch_unlikely(&page_owner_inited)) |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | |
| 220 | page = NULL; |
| 221 | pfn = min_low_pfn + *ppos; |
| 222 | |
| 223 | /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */ |
| 224 | while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0) |
| 225 | pfn++; |
| 226 | |
| 227 | drain_all_pages(NULL); |
| 228 | |
| 229 | /* Find an allocated page */ |
| 230 | for (; pfn < max_pfn; pfn++) { |
| 231 | /* |
| 232 | * If the new page is in a new MAX_ORDER_NR_PAGES area, |
| 233 | * validate the area as existing, skip it if not |
| 234 | */ |
| 235 | if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) { |
| 236 | pfn += MAX_ORDER_NR_PAGES - 1; |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | /* Check for holes within a MAX_ORDER area */ |
| 241 | if (!pfn_valid_within(pfn)) |
| 242 | continue; |
| 243 | |
| 244 | page = pfn_to_page(pfn); |
| 245 | if (PageBuddy(page)) { |
| 246 | unsigned long freepage_order = page_order_unsafe(page); |
| 247 | |
| 248 | if (freepage_order < MAX_ORDER) |
| 249 | pfn += (1UL << freepage_order) - 1; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | page_ext = lookup_page_ext(page); |
| 254 | |
| 255 | /* |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 256 | * Some pages could be missed by concurrent allocation or free, |
| 257 | * because we don't hold the zone lock. |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 258 | */ |
| 259 | if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 260 | continue; |
| 261 | |
| 262 | /* Record the next PFN to read in the file offset */ |
| 263 | *ppos = (pfn - min_low_pfn) + 1; |
| 264 | |
| 265 | return print_page_owner(buf, count, pfn, page, page_ext); |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 271 | static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone) |
| 272 | { |
| 273 | struct page *page; |
| 274 | struct page_ext *page_ext; |
| 275 | unsigned long pfn = zone->zone_start_pfn, block_end_pfn; |
| 276 | unsigned long end_pfn = pfn + zone->spanned_pages; |
| 277 | unsigned long count = 0; |
| 278 | |
| 279 | /* Scan block by block. First and last block may be incomplete */ |
| 280 | pfn = zone->zone_start_pfn; |
| 281 | |
| 282 | /* |
| 283 | * Walk the zone in pageblock_nr_pages steps. If a page block spans |
| 284 | * a zone boundary, it will be double counted between zones. This does |
| 285 | * not matter as the mixed block count will still be correct |
| 286 | */ |
| 287 | for (; pfn < end_pfn; ) { |
| 288 | if (!pfn_valid(pfn)) { |
| 289 | pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES); |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages); |
| 294 | block_end_pfn = min(block_end_pfn, end_pfn); |
| 295 | |
| 296 | page = pfn_to_page(pfn); |
| 297 | |
| 298 | for (; pfn < block_end_pfn; pfn++) { |
| 299 | if (!pfn_valid_within(pfn)) |
| 300 | continue; |
| 301 | |
| 302 | page = pfn_to_page(pfn); |
| 303 | |
Joonsoo Kim | 9d43f5a | 2016-05-19 17:12:13 -0700 | [diff] [blame] | 304 | if (page_zone(page) != zone) |
| 305 | continue; |
| 306 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 307 | /* |
| 308 | * We are safe to check buddy flag and order, because |
| 309 | * this is init stage and only single thread runs. |
| 310 | */ |
| 311 | if (PageBuddy(page)) { |
| 312 | pfn += (1UL << page_order(page)) - 1; |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | if (PageReserved(page)) |
| 317 | continue; |
| 318 | |
| 319 | page_ext = lookup_page_ext(page); |
| 320 | |
| 321 | /* Maybe overraping zone */ |
| 322 | if (test_bit(PAGE_EXT_OWNER, &page_ext->flags)) |
| 323 | continue; |
| 324 | |
| 325 | /* Found early allocated page */ |
| 326 | set_page_owner(page, 0, 0); |
| 327 | count++; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n", |
| 332 | pgdat->node_id, zone->name, count); |
| 333 | } |
| 334 | |
| 335 | static void init_zones_in_node(pg_data_t *pgdat) |
| 336 | { |
| 337 | struct zone *zone; |
| 338 | struct zone *node_zones = pgdat->node_zones; |
| 339 | unsigned long flags; |
| 340 | |
| 341 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { |
| 342 | if (!populated_zone(zone)) |
| 343 | continue; |
| 344 | |
| 345 | spin_lock_irqsave(&zone->lock, flags); |
| 346 | init_pages_in_zone(pgdat, zone); |
| 347 | spin_unlock_irqrestore(&zone->lock, flags); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static void init_early_allocated_pages(void) |
| 352 | { |
| 353 | pg_data_t *pgdat; |
| 354 | |
| 355 | drain_all_pages(NULL); |
| 356 | for_each_online_pgdat(pgdat) |
| 357 | init_zones_in_node(pgdat); |
| 358 | } |
| 359 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 360 | static const struct file_operations proc_page_owner_operations = { |
| 361 | .read = read_page_owner, |
| 362 | }; |
| 363 | |
| 364 | static int __init pageowner_init(void) |
| 365 | { |
| 366 | struct dentry *dentry; |
| 367 | |
Vlastimil Babka | 7dd80b8 | 2016-03-15 14:56:12 -0700 | [diff] [blame] | 368 | if (!static_branch_unlikely(&page_owner_inited)) { |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 369 | pr_info("page_owner is disabled\n"); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | dentry = debugfs_create_file("page_owner", S_IRUSR, NULL, |
| 374 | NULL, &proc_page_owner_operations); |
| 375 | if (IS_ERR(dentry)) |
| 376 | return PTR_ERR(dentry); |
| 377 | |
| 378 | return 0; |
| 379 | } |
Paul Gortmaker | 44c5af9 | 2015-05-01 21:57:34 -0400 | [diff] [blame] | 380 | late_initcall(pageowner_init) |