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