Commit | Line | Data |
---|---|---|
48c96a36 JK |
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> | |
7dd80b8a | 8 | #include <linux/jump_label.h> |
48c96a36 JK |
9 | #include "internal.h" |
10 | ||
11 | static bool page_owner_disabled = true; | |
7dd80b8a | 12 | DEFINE_STATIC_KEY_FALSE(page_owner_inited); |
48c96a36 | 13 | |
61cf5feb JK |
14 | static void init_early_allocated_pages(void); |
15 | ||
48c96a36 JK |
16 | static 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 | } | |
26 | early_param("page_owner", early_page_owner_param); | |
27 | ||
28 | static bool need_page_owner(void) | |
29 | { | |
30 | if (page_owner_disabled) | |
31 | return false; | |
32 | ||
33 | return true; | |
34 | } | |
35 | ||
36 | static void init_page_owner(void) | |
37 | { | |
38 | if (page_owner_disabled) | |
39 | return; | |
40 | ||
7dd80b8a | 41 | static_branch_enable(&page_owner_inited); |
61cf5feb | 42 | init_early_allocated_pages(); |
48c96a36 JK |
43 | } |
44 | ||
45 | struct page_ext_operations page_owner_ops = { | |
46 | .need = need_page_owner, | |
47 | .init = init_page_owner, | |
48 | }; | |
49 | ||
50 | void __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 | ||
61 | void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask) | |
62 | { | |
94f759d6 SR |
63 | 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 | }; | |
48c96a36 | 70 | |
94f759d6 | 71 | save_stack_trace(&trace); |
48c96a36 JK |
72 | |
73 | page_ext->order = order; | |
74 | page_ext->gfp_mask = gfp_mask; | |
94f759d6 | 75 | page_ext->nr_entries = trace.nr_entries; |
48c96a36 JK |
76 | |
77 | __set_bit(PAGE_EXT_OWNER, &page_ext->flags); | |
78 | } | |
79 | ||
e2cfc911 JK |
80 | gfp_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 | ||
d435edca VB |
87 | void __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 | ||
48c96a36 JK |
112 | static ssize_t |
113 | print_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; | |
94f759d6 SR |
119 | struct stack_trace trace = { |
120 | .nr_entries = page_ext->nr_entries, | |
121 | .entries = &page_ext->trace_entries[0], | |
122 | }; | |
48c96a36 JK |
123 | |
124 | kbuf = kmalloc(count, GFP_KERNEL); | |
125 | if (!kbuf) | |
126 | return -ENOMEM; | |
127 | ||
128 | ret = snprintf(kbuf, count, | |
60f30350 VB |
129 | "Page allocated via order %u, mask %#x(%pGg)\n", |
130 | page_ext->order, page_ext->gfp_mask, | |
131 | &page_ext->gfp_mask); | |
48c96a36 JK |
132 | |
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, | |
60f30350 | 140 | "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n", |
48c96a36 | 141 | pfn, |
60f30350 | 142 | migratetype_names[page_mt], |
48c96a36 | 143 | pfn >> pageblock_order, |
60f30350 VB |
144 | migratetype_names[pageblock_mt], |
145 | page->flags, &page->flags); | |
48c96a36 JK |
146 | |
147 | if (ret >= count) | |
148 | goto err; | |
149 | ||
94f759d6 | 150 | ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0); |
48c96a36 JK |
151 | 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 | ||
164 | err: | |
165 | kfree(kbuf); | |
166 | return -ENOMEM; | |
167 | } | |
168 | ||
169 | static ssize_t | |
170 | read_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 | ||
7dd80b8a | 176 | if (!static_branch_unlikely(&page_owner_inited)) |
48c96a36 JK |
177 | 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 | /* | |
61cf5feb JK |
215 | * Some pages could be missed by concurrent allocation or free, |
216 | * because we don't hold the zone lock. | |
48c96a36 JK |
217 | */ |
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 | ||
61cf5feb JK |
230 | static 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 | ||
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 | ||
48c96a36 JK |
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 | ||
7dd80b8a | 324 | if (!static_branch_unlikely(&page_owner_inited)) { |
48c96a36 JK |
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 | } | |
44c5af96 | 336 | late_initcall(pageowner_init) |