blob: feaa28b40c1c1aad848f88664b8ec22a92e79b68 [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
Joonsoo Kim48c96a32014-12-12 16:56:01 -080087static ssize_t
88print_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 Rogachev94f759d62015-02-11 15:28:34 -080094 struct stack_trace trace = {
95 .nr_entries = page_ext->nr_entries,
96 .entries = &page_ext->trace_entries[0],
97 };
Joonsoo Kim48c96a32014-12-12 16:56:01 -080098
99 kbuf = kmalloc(count, GFP_KERNEL);
100 if (!kbuf)
101 return -ENOMEM;
102
103 ret = snprintf(kbuf, count,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700104 "Page allocated via order %u, mask %#x(%pGg)\n",
105 page_ext->order, page_ext->gfp_mask,
106 &page_ext->gfp_mask);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800107
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 Babka60f30352016-03-15 14:56:08 -0700115 "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800116 pfn,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700117 migratetype_names[page_mt],
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800118 pfn >> pageblock_order,
Vlastimil Babka60f30352016-03-15 14:56:08 -0700119 migratetype_names[pageblock_mt],
120 page->flags, &page->flags);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800121
122 if (ret >= count)
123 goto err;
124
Sergei Rogachev94f759d62015-02-11 15:28:34 -0800125 ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800126 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
139err:
140 kfree(kbuf);
141 return -ENOMEM;
142}
143
144static ssize_t
145read_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 Babka7dd80b82016-03-15 14:56:12 -0700151 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800152 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 Kim61cf5fe2014-12-12 16:56:04 -0800190 * Some pages could be missed by concurrent allocation or free,
191 * because we don't hold the zone lock.
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800192 */
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 Kim61cf5fe2014-12-12 16:56:04 -0800205static 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
266static 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
282static 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 Kim48c96a32014-12-12 16:56:01 -0800291static const struct file_operations proc_page_owner_operations = {
292 .read = read_page_owner,
293};
294
295static int __init pageowner_init(void)
296{
297 struct dentry *dentry;
298
Vlastimil Babka7dd80b82016-03-15 14:56:12 -0700299 if (!static_branch_unlikely(&page_owner_inited)) {
Joonsoo Kim48c96a32014-12-12 16:56:01 -0800300 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 Gortmaker44c5af92015-05-01 21:57:34 -0400311late_initcall(pageowner_init)