blob: 774b55623212c9e6719af228221ccf98b668f82f [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>
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07008#include <linux/jump_label.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -08009#include "internal.h"
10
11static bool page_owner_disabled = true;
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070012DEFINE_STATIC_KEY_FALSE(page_owner_inited);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080013
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080014static void init_early_allocated_pages(void);
15
Joonsoo Kim48c96a32014-12-12 16:56:01 -080016static 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}
26early_param("page_owner", early_page_owner_param);
27
28static bool need_page_owner(void)
29{
30 if (page_owner_disabled)
31 return false;
32
33 return true;
34}
35
36static void init_page_owner(void)
37{
38 if (page_owner_disabled)
39 return;
40
Vlastimil Babka7dd80b82016-03-15 14:56:12 -070041 static_branch_enable(&page_owner_inited);
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -080042 init_early_allocated_pages();
Joonsoo Kim48c96a32014-12-12 16:56:01 -080043}
44
45struct page_ext_operations page_owner_ops = {
46 .need = need_page_owner,
47 .init = init_page_owner,
48};
49
50void __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
61void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask)
62{
Sergei Rogachev94f759d62015-02-11 15:28:34 -080063 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 Kim48c96a32014-12-12 16:56:01 -080070
Sergei Rogachev94f759d62015-02-11 15:28:34 -080071 save_stack_trace(&trace);
Joonsoo Kim48c96a32014-12-12 16:56:01 -080072
73 page_ext->order = order;
74 page_ext->gfp_mask = gfp_mask;
Sergei Rogachev94f759d62015-02-11 15:28:34 -080075 page_ext->nr_entries = trace.nr_entries;
Joonsoo Kim48c96a32014-12-12 16:56:01 -080076
77 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
78}
79
Joonsoo Kime2cfc912015-07-17 16:24:18 -070080gfp_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
Vlastimil Babkad435edc2016-03-15 14:56:15 -070087void __copy_page_owner(struct page *oldpage, struct page *newpage)
88{
89 struct page_ext *old_ext = lookup_page_ext(oldpage);
90 struct page_ext *new_ext = lookup_page_ext(newpage);
91 int i;
92
93 new_ext->order = old_ext->order;
94 new_ext->gfp_mask = old_ext->gfp_mask;
95 new_ext->nr_entries = old_ext->nr_entries;
96
97 for (i = 0; i < ARRAY_SIZE(new_ext->trace_entries); i++)
98 new_ext->trace_entries[i] = old_ext->trace_entries[i];
99
100 /*
101 * We don't clear the bit on the oldpage as it's going to be freed
102 * after migration. Until then, the info can be useful in case of
103 * a bug, and the overal stats will be off a bit only temporarily.
104 * Also, migrate_misplaced_transhuge_page() can still fail the
105 * migration and then we want the oldpage to retain the info. But
106 * in that case we also don't need to explicitly clear the info from
107 * the new page, which will be freed.
108 */
109 __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
110}
111
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800112static ssize_t
113print_page_owner(char __user *buf, size_t count, unsigned long pfn,
114 struct page *page, struct page_ext *page_ext)
115{
116 int ret;
117 int pageblock_mt, page_mt;
118 char *kbuf;
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800119 struct stack_trace trace = {
120 .nr_entries = page_ext->nr_entries,
121 .entries = &page_ext->trace_entries[0],
122 };
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800123
124 kbuf = kmalloc(count, GFP_KERNEL);
125 if (!kbuf)
126 return -ENOMEM;
127
128 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700129 "Page allocated via order %u, mask %#x(%pGg)\n",
130 page_ext->order, page_ext->gfp_mask,
131 &page_ext->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800132
133 if (ret >= count)
134 goto err;
135
136 /* Print information relevant to grouping pages by mobility */
137 pageblock_mt = get_pfnblock_migratetype(page, pfn);
138 page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
139 ret += snprintf(kbuf + ret, count - ret,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700140 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800141 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700142 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800143 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700144 migratetype_names[pageblock_mt],
145 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800146
147 if (ret >= count)
148 goto err;
149
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800150 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800151 if (ret >= count)
152 goto err;
153
154 ret += snprintf(kbuf + ret, count - ret, "\n");
155 if (ret >= count)
156 goto err;
157
158 if (copy_to_user(buf, kbuf, ret))
159 ret = -EFAULT;
160
161 kfree(kbuf);
162 return ret;
163
164err:
165 kfree(kbuf);
166 return -ENOMEM;
167}
168
169static ssize_t
170read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
171{
172 unsigned long pfn;
173 struct page *page;
174 struct page_ext *page_ext;
175
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700176 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800177 return -EINVAL;
178
179 page = NULL;
180 pfn = min_low_pfn + *ppos;
181
182 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
183 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
184 pfn++;
185
186 drain_all_pages(NULL);
187
188 /* Find an allocated page */
189 for (; pfn < max_pfn; pfn++) {
190 /*
191 * If the new page is in a new MAX_ORDER_NR_PAGES area,
192 * validate the area as existing, skip it if not
193 */
194 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
195 pfn += MAX_ORDER_NR_PAGES - 1;
196 continue;
197 }
198
199 /* Check for holes within a MAX_ORDER area */
200 if (!pfn_valid_within(pfn))
201 continue;
202
203 page = pfn_to_page(pfn);
204 if (PageBuddy(page)) {
205 unsigned long freepage_order = page_order_unsafe(page);
206
207 if (freepage_order < MAX_ORDER)
208 pfn += (1UL << freepage_order) - 1;
209 continue;
210 }
211
212 page_ext = lookup_page_ext(page);
213
214 /*
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800215 * Some pages could be missed by concurrent allocation or free,
216 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800217 */
218 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
219 continue;
220
221 /* Record the next PFN to read in the file offset */
222 *ppos = (pfn - min_low_pfn) + 1;
223
224 return print_page_owner(buf, count, pfn, page, page_ext);
225 }
226
227 return 0;
228}
229
Joonsoo Kim61cf5fe2014-12-12 16:56:04 -0800230static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
231{
232 struct page *page;
233 struct page_ext *page_ext;
234 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
235 unsigned long end_pfn = pfn + zone->spanned_pages;
236 unsigned long count = 0;
237
238 /* Scan block by block. First and last block may be incomplete */
239 pfn = zone->zone_start_pfn;
240
241 /*
242 * Walk the zone in pageblock_nr_pages steps. If a page block spans
243 * a zone boundary, it will be double counted between zones. This does
244 * not matter as the mixed block count will still be correct
245 */
246 for (; pfn < end_pfn; ) {
247 if (!pfn_valid(pfn)) {
248 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
249 continue;
250 }
251
252 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
253 block_end_pfn = min(block_end_pfn, end_pfn);
254
255 page = pfn_to_page(pfn);
256
257 for (; pfn < block_end_pfn; pfn++) {
258 if (!pfn_valid_within(pfn))
259 continue;
260
261 page = pfn_to_page(pfn);
262
263 /*
264 * We are safe to check buddy flag and order, because
265 * this is init stage and only single thread runs.
266 */
267 if (PageBuddy(page)) {
268 pfn += (1UL << page_order(page)) - 1;
269 continue;
270 }
271
272 if (PageReserved(page))
273 continue;
274
275 page_ext = lookup_page_ext(page);
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
291static 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
307static 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
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800316static const struct file_operations proc_page_owner_operations = {
317 .read = read_page_owner,
318};
319
320static int __init pageowner_init(void)
321{
322 struct dentry *dentry;
323
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700324 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800325 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}
Paul Gortmaker44c5af92015-05-01 21:57:34 -0400336late_initcall(pageowner_init)