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