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> |
| 8 | #include "internal.h" |
| 9 | |
| 10 | static bool page_owner_disabled = true; |
| 11 | bool page_owner_inited __read_mostly; |
| 12 | |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 13 | static void init_early_allocated_pages(void); |
| 14 | |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 15 | static int early_page_owner_param(char *buf) |
| 16 | { |
| 17 | if (!buf) |
| 18 | return -EINVAL; |
| 19 | |
| 20 | if (strcmp(buf, "on") == 0) |
| 21 | page_owner_disabled = false; |
| 22 | |
| 23 | return 0; |
| 24 | } |
| 25 | early_param("page_owner", early_page_owner_param); |
| 26 | |
| 27 | static bool need_page_owner(void) |
| 28 | { |
| 29 | if (page_owner_disabled) |
| 30 | return false; |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | static void init_page_owner(void) |
| 36 | { |
| 37 | if (page_owner_disabled) |
| 38 | return; |
| 39 | |
| 40 | page_owner_inited = true; |
Joonsoo Kim | 61cf5fe | 2014-12-12 16:56:04 -0800 | [diff] [blame] | 41 | init_early_allocated_pages(); |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | struct page_ext_operations page_owner_ops = { |
| 45 | .need = need_page_owner, |
| 46 | .init = init_page_owner, |
| 47 | }; |
| 48 | |
| 49 | void __reset_page_owner(struct page *page, unsigned int order) |
| 50 | { |
| 51 | int i; |
| 52 | struct page_ext *page_ext; |
| 53 | |
| 54 | for (i = 0; i < (1 << order); i++) { |
| 55 | page_ext = lookup_page_ext(page + i); |
| 56 | __clear_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask) |
| 61 | { |
| 62 | struct page_ext *page_ext; |
| 63 | struct stack_trace *trace; |
| 64 | |
| 65 | page_ext = lookup_page_ext(page); |
| 66 | |
| 67 | trace = &page_ext->trace; |
| 68 | trace->nr_entries = 0; |
| 69 | trace->max_entries = ARRAY_SIZE(page_ext->trace_entries); |
| 70 | trace->entries = &page_ext->trace_entries[0]; |
| 71 | trace->skip = 3; |
| 72 | save_stack_trace(&page_ext->trace); |
| 73 | |
| 74 | page_ext->order = order; |
| 75 | page_ext->gfp_mask = gfp_mask; |
| 76 | |
| 77 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); |
| 78 | } |
| 79 | |
| 80 | static ssize_t |
| 81 | print_page_owner(char __user *buf, size_t count, unsigned long pfn, |
| 82 | struct page *page, struct page_ext *page_ext) |
| 83 | { |
| 84 | int ret; |
| 85 | int pageblock_mt, page_mt; |
| 86 | char *kbuf; |
| 87 | |
| 88 | kbuf = kmalloc(count, GFP_KERNEL); |
| 89 | if (!kbuf) |
| 90 | return -ENOMEM; |
| 91 | |
| 92 | ret = snprintf(kbuf, count, |
| 93 | "Page allocated via order %u, mask 0x%x\n", |
| 94 | page_ext->order, page_ext->gfp_mask); |
| 95 | |
| 96 | if (ret >= count) |
| 97 | goto err; |
| 98 | |
| 99 | /* Print information relevant to grouping pages by mobility */ |
| 100 | pageblock_mt = get_pfnblock_migratetype(page, pfn); |
| 101 | page_mt = gfpflags_to_migratetype(page_ext->gfp_mask); |
| 102 | ret += snprintf(kbuf + ret, count - ret, |
| 103 | "PFN %lu Block %lu type %d %s Flags %s%s%s%s%s%s%s%s%s%s%s%s\n", |
| 104 | pfn, |
| 105 | pfn >> pageblock_order, |
| 106 | pageblock_mt, |
| 107 | pageblock_mt != page_mt ? "Fallback" : " ", |
| 108 | PageLocked(page) ? "K" : " ", |
| 109 | PageError(page) ? "E" : " ", |
| 110 | PageReferenced(page) ? "R" : " ", |
| 111 | PageUptodate(page) ? "U" : " ", |
| 112 | PageDirty(page) ? "D" : " ", |
| 113 | PageLRU(page) ? "L" : " ", |
| 114 | PageActive(page) ? "A" : " ", |
| 115 | PageSlab(page) ? "S" : " ", |
| 116 | PageWriteback(page) ? "W" : " ", |
| 117 | PageCompound(page) ? "C" : " ", |
| 118 | PageSwapCache(page) ? "B" : " ", |
| 119 | PageMappedToDisk(page) ? "M" : " "); |
| 120 | |
| 121 | if (ret >= count) |
| 122 | goto err; |
| 123 | |
| 124 | ret += snprint_stack_trace(kbuf + ret, count - ret, |
| 125 | &page_ext->trace, 0); |
| 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 | |
| 151 | if (!page_owner_inited) |
| 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 | |
| 299 | if (!page_owner_inited) { |
| 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 | } |
| 311 | module_init(pageowner_init) |