]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - mm/page_owner.c
UBUNTU: [Config] Sync config with master
[mirror_ubuntu-artful-kernel.git] / mm / page_owner.c
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
13 static void init_early_allocated_pages(void);
14
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;
41 init_early_allocated_pages();
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 if (unlikely(!page_ext))
57 continue;
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 {
64 struct page_ext *page_ext = lookup_page_ext(page);
65
66 struct stack_trace trace = {
67 .nr_entries = 0,
68 .max_entries = ARRAY_SIZE(page_ext->trace_entries),
69 .entries = &page_ext->trace_entries[0],
70 .skip = 3,
71 };
72
73 if (unlikely(!page_ext))
74 return;
75
76 save_stack_trace(&trace);
77
78 page_ext->order = order;
79 page_ext->gfp_mask = gfp_mask;
80 page_ext->nr_entries = trace.nr_entries;
81
82 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
83 }
84
85 gfp_t __get_page_owner_gfp(struct page *page)
86 {
87 struct page_ext *page_ext = lookup_page_ext(page);
88 if (unlikely(!page_ext))
89 /*
90 * The caller just returns 0 if no valid gfp
91 * So return 0 here too.
92 */
93 return 0;
94
95 return page_ext->gfp_mask;
96 }
97
98 static ssize_t
99 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
100 struct page *page, struct page_ext *page_ext)
101 {
102 int ret;
103 int pageblock_mt, page_mt;
104 char *kbuf;
105 struct stack_trace trace = {
106 .nr_entries = page_ext->nr_entries,
107 .entries = &page_ext->trace_entries[0],
108 };
109
110 kbuf = kmalloc(count, GFP_KERNEL);
111 if (!kbuf)
112 return -ENOMEM;
113
114 ret = snprintf(kbuf, count,
115 "Page allocated via order %u, mask 0x%x\n",
116 page_ext->order, page_ext->gfp_mask);
117
118 if (ret >= count)
119 goto err;
120
121 /* Print information relevant to grouping pages by mobility */
122 pageblock_mt = get_pfnblock_migratetype(page, pfn);
123 page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
124 ret += snprintf(kbuf + ret, count - ret,
125 "PFN %lu Block %lu type %d %s Flags %s%s%s%s%s%s%s%s%s%s%s%s\n",
126 pfn,
127 pfn >> pageblock_order,
128 pageblock_mt,
129 pageblock_mt != page_mt ? "Fallback" : " ",
130 PageLocked(page) ? "K" : " ",
131 PageError(page) ? "E" : " ",
132 PageReferenced(page) ? "R" : " ",
133 PageUptodate(page) ? "U" : " ",
134 PageDirty(page) ? "D" : " ",
135 PageLRU(page) ? "L" : " ",
136 PageActive(page) ? "A" : " ",
137 PageSlab(page) ? "S" : " ",
138 PageWriteback(page) ? "W" : " ",
139 PageCompound(page) ? "C" : " ",
140 PageSwapCache(page) ? "B" : " ",
141 PageMappedToDisk(page) ? "M" : " ");
142
143 if (ret >= count)
144 goto err;
145
146 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
147 if (ret >= count)
148 goto err;
149
150 ret += snprintf(kbuf + ret, count - ret, "\n");
151 if (ret >= count)
152 goto err;
153
154 if (copy_to_user(buf, kbuf, ret))
155 ret = -EFAULT;
156
157 kfree(kbuf);
158 return ret;
159
160 err:
161 kfree(kbuf);
162 return -ENOMEM;
163 }
164
165 static ssize_t
166 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
167 {
168 unsigned long pfn;
169 struct page *page;
170 struct page_ext *page_ext;
171
172 if (!page_owner_inited)
173 return -EINVAL;
174
175 page = NULL;
176 pfn = min_low_pfn + *ppos;
177
178 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
179 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
180 pfn++;
181
182 drain_all_pages(NULL);
183
184 /* Find an allocated page */
185 for (; pfn < max_pfn; pfn++) {
186 /*
187 * If the new page is in a new MAX_ORDER_NR_PAGES area,
188 * validate the area as existing, skip it if not
189 */
190 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
191 pfn += MAX_ORDER_NR_PAGES - 1;
192 continue;
193 }
194
195 /* Check for holes within a MAX_ORDER area */
196 if (!pfn_valid_within(pfn))
197 continue;
198
199 page = pfn_to_page(pfn);
200 if (PageBuddy(page)) {
201 unsigned long freepage_order = page_order_unsafe(page);
202
203 if (freepage_order < MAX_ORDER)
204 pfn += (1UL << freepage_order) - 1;
205 continue;
206 }
207
208 page_ext = lookup_page_ext(page);
209 if (unlikely(!page_ext))
210 continue;
211
212 /*
213 * Some pages could be missed by concurrent allocation or free,
214 * because we don't hold the zone lock.
215 */
216 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
217 continue;
218
219 /* Record the next PFN to read in the file offset */
220 *ppos = (pfn - min_low_pfn) + 1;
221
222 return print_page_owner(buf, count, pfn, page, page_ext);
223 }
224
225 return 0;
226 }
227
228 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
229 {
230 struct page *page;
231 struct page_ext *page_ext;
232 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
233 unsigned long end_pfn = pfn + zone->spanned_pages;
234 unsigned long count = 0;
235
236 /* Scan block by block. First and last block may be incomplete */
237 pfn = zone->zone_start_pfn;
238
239 /*
240 * Walk the zone in pageblock_nr_pages steps. If a page block spans
241 * a zone boundary, it will be double counted between zones. This does
242 * not matter as the mixed block count will still be correct
243 */
244 for (; pfn < end_pfn; ) {
245 if (!pfn_valid(pfn)) {
246 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
247 continue;
248 }
249
250 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
251 block_end_pfn = min(block_end_pfn, end_pfn);
252
253 page = pfn_to_page(pfn);
254
255 for (; pfn < block_end_pfn; pfn++) {
256 if (!pfn_valid_within(pfn))
257 continue;
258
259 page = pfn_to_page(pfn);
260
261 /*
262 * We are safe to check buddy flag and order, because
263 * this is init stage and only single thread runs.
264 */
265 if (PageBuddy(page)) {
266 pfn += (1UL << page_order(page)) - 1;
267 continue;
268 }
269
270 if (PageReserved(page))
271 continue;
272
273 page_ext = lookup_page_ext(page);
274 if (unlikely(!page_ext))
275 continue;
276
277 /* Maybe overraping zone */
278 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
279 continue;
280
281 /* Found early allocated page */
282 set_page_owner(page, 0, 0);
283 count++;
284 }
285 }
286
287 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
288 pgdat->node_id, zone->name, count);
289 }
290
291 static void init_zones_in_node(pg_data_t *pgdat)
292 {
293 struct zone *zone;
294 struct zone *node_zones = pgdat->node_zones;
295 unsigned long flags;
296
297 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
298 if (!populated_zone(zone))
299 continue;
300
301 spin_lock_irqsave(&zone->lock, flags);
302 init_pages_in_zone(pgdat, zone);
303 spin_unlock_irqrestore(&zone->lock, flags);
304 }
305 }
306
307 static void init_early_allocated_pages(void)
308 {
309 pg_data_t *pgdat;
310
311 drain_all_pages(NULL);
312 for_each_online_pgdat(pgdat)
313 init_zones_in_node(pgdat);
314 }
315
316 static const struct file_operations proc_page_owner_operations = {
317 .read = read_page_owner,
318 };
319
320 static int __init pageowner_init(void)
321 {
322 struct dentry *dentry;
323
324 if (!page_owner_inited) {
325 pr_info("page_owner is disabled\n");
326 return 0;
327 }
328
329 dentry = debugfs_create_file("page_owner", S_IRUSR, NULL,
330 NULL, &proc_page_owner_operations);
331 if (IS_ERR(dentry))
332 return PTR_ERR(dentry);
333
334 return 0;
335 }
336 late_initcall(pageowner_init)