• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/debugfs.h>
3 #include <linux/mm.h>
4 #include <linux/slab.h>
5 #include <linux/uaccess.h>
6 #include <linux/memblock.h>
7 #include <linux/stacktrace.h>
8 #include <linux/page_owner.h>
9 #include <linux/jump_label.h>
10 #include <linux/migrate.h>
11 #include <linux/stackdepot.h>
12 #include <linux/seq_file.h>
13 #include <linux/sched/clock.h>
14 
15 #include "internal.h"
16 
17 /*
18  * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
19  * to use off stack temporal storage
20  */
21 #define PAGE_OWNER_STACK_DEPTH (16)
22 
23 struct page_owner {
24 	unsigned short order;
25 	short last_migrate_reason;
26 	gfp_t gfp_mask;
27 	depot_stack_handle_t handle;
28 	depot_stack_handle_t free_handle;
29 	u64 ts_nsec;
30 	u64 free_ts_nsec;
31 	pid_t pid;
32 };
33 
34 bool page_owner_enabled;
35 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
36 EXPORT_SYMBOL_GPL(page_owner_inited);
37 
38 static depot_stack_handle_t dummy_handle;
39 static depot_stack_handle_t failure_handle;
40 static depot_stack_handle_t early_handle;
41 
42 static void init_early_allocated_pages(void);
43 
early_page_owner_param(char * buf)44 static int __init early_page_owner_param(char *buf)
45 {
46 	if (!buf)
47 		return -EINVAL;
48 
49 	if (strcmp(buf, "on") == 0)
50 		page_owner_enabled = true;
51 
52 	return 0;
53 }
54 early_param("page_owner", early_page_owner_param);
55 
need_page_owner(void)56 static bool need_page_owner(void)
57 {
58 	return page_owner_enabled;
59 }
60 
create_dummy_stack(void)61 static __always_inline depot_stack_handle_t create_dummy_stack(void)
62 {
63 	unsigned long entries[4];
64 	unsigned int nr_entries;
65 
66 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
67 	return stack_depot_save(entries, nr_entries, GFP_KERNEL);
68 }
69 
register_dummy_stack(void)70 static noinline void register_dummy_stack(void)
71 {
72 	dummy_handle = create_dummy_stack();
73 }
74 
register_failure_stack(void)75 static noinline void register_failure_stack(void)
76 {
77 	failure_handle = create_dummy_stack();
78 }
79 
register_early_stack(void)80 static noinline void register_early_stack(void)
81 {
82 	early_handle = create_dummy_stack();
83 }
84 
init_page_owner(void)85 static void init_page_owner(void)
86 {
87 	if (!page_owner_enabled)
88 		return;
89 
90 	register_dummy_stack();
91 	register_failure_stack();
92 	register_early_stack();
93 	static_branch_enable(&page_owner_inited);
94 	init_early_allocated_pages();
95 }
96 
97 struct page_ext_operations page_owner_ops = {
98 	.size = sizeof(struct page_owner),
99 	.need = need_page_owner,
100 	.init = init_page_owner,
101 };
102 
get_page_owner(struct page_ext * page_ext)103 struct page_owner *get_page_owner(struct page_ext *page_ext)
104 {
105 	return (void *)page_ext + page_owner_ops.offset;
106 }
107 EXPORT_SYMBOL_GPL(get_page_owner);
108 
get_page_owner_handle(struct page_ext * page_ext,unsigned long pfn)109 depot_stack_handle_t get_page_owner_handle(struct page_ext *page_ext, unsigned long pfn)
110 {
111 	struct page_owner *page_owner;
112 	depot_stack_handle_t handle;
113 
114 	if (!page_owner_enabled)
115 		return 0;
116 
117 	page_owner = get_page_owner(page_ext);
118 
119 	/* skip handle for tail pages of higher order allocations */
120 	if (!IS_ALIGNED(pfn, 1 << page_owner->order))
121 		return 0;
122 
123 	handle = READ_ONCE(page_owner->handle);
124 	return handle;
125 }
126 EXPORT_SYMBOL_GPL(get_page_owner_handle);
127 
check_recursive_alloc(unsigned long * entries,unsigned int nr_entries,unsigned long ip)128 static inline bool check_recursive_alloc(unsigned long *entries,
129 					 unsigned int nr_entries,
130 					 unsigned long ip)
131 {
132 	unsigned int i;
133 
134 	for (i = 0; i < nr_entries; i++) {
135 		if (entries[i] == ip)
136 			return true;
137 	}
138 	return false;
139 }
140 
save_stack(gfp_t flags)141 static noinline depot_stack_handle_t save_stack(gfp_t flags)
142 {
143 	unsigned long entries[PAGE_OWNER_STACK_DEPTH];
144 	depot_stack_handle_t handle;
145 	unsigned int nr_entries;
146 
147 	nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2);
148 
149 	/*
150 	 * We need to check recursion here because our request to
151 	 * stackdepot could trigger memory allocation to save new
152 	 * entry. New memory allocation would reach here and call
153 	 * stack_depot_save_entries() again if we don't catch it. There is
154 	 * still not enough memory in stackdepot so it would try to
155 	 * allocate memory again and loop forever.
156 	 */
157 	if (check_recursive_alloc(entries, nr_entries, _RET_IP_))
158 		return dummy_handle;
159 
160 	handle = stack_depot_save(entries, nr_entries, flags);
161 	if (!handle)
162 		handle = failure_handle;
163 
164 	return handle;
165 }
166 
__reset_page_owner(struct page * page,unsigned int order)167 void __reset_page_owner(struct page *page, unsigned int order)
168 {
169 	int i;
170 	struct page_ext *page_ext;
171 	depot_stack_handle_t handle = 0;
172 	struct page_owner *page_owner;
173 	u64 free_ts_nsec = local_clock();
174 
175 	handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
176 
177 	page_ext = page_ext_get(page);
178 	if (unlikely(!page_ext))
179 		return;
180 	for (i = 0; i < (1 << order); i++) {
181 		__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
182 		page_owner = get_page_owner(page_ext);
183 		page_owner->free_handle = handle;
184 		page_owner->free_ts_nsec = free_ts_nsec;
185 		page_ext = page_ext_next(page_ext);
186 	}
187 	page_ext_put(page_ext);
188 }
189 
__set_page_owner_handle(struct page * page,struct page_ext * page_ext,depot_stack_handle_t handle,unsigned int order,gfp_t gfp_mask)190 static inline void __set_page_owner_handle(struct page *page,
191 	struct page_ext *page_ext, depot_stack_handle_t handle,
192 	unsigned int order, gfp_t gfp_mask)
193 {
194 	struct page_owner *page_owner;
195 	int i;
196 
197 	for (i = 0; i < (1 << order); i++) {
198 		page_owner = get_page_owner(page_ext);
199 		page_owner->handle = handle;
200 		page_owner->order = order;
201 		page_owner->gfp_mask = gfp_mask;
202 		page_owner->last_migrate_reason = -1;
203 		page_owner->pid = current->pid;
204 		page_owner->ts_nsec = local_clock();
205 		__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
206 		__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
207 
208 		page_ext = page_ext_next(page_ext);
209 	}
210 }
211 
__set_page_owner(struct page * page,unsigned int order,gfp_t gfp_mask)212 noinline void __set_page_owner(struct page *page, unsigned int order,
213 					gfp_t gfp_mask)
214 {
215 	struct page_ext *page_ext;
216 	depot_stack_handle_t handle;
217 
218 	handle = save_stack(gfp_mask);
219 
220 	page_ext = page_ext_get(page);
221 	if (unlikely(!page_ext))
222 		return;
223 	__set_page_owner_handle(page, page_ext, handle, order, gfp_mask);
224 	page_ext_put(page_ext);
225 }
226 EXPORT_SYMBOL_GPL(__set_page_owner);
227 
__set_page_owner_migrate_reason(struct page * page,int reason)228 void __set_page_owner_migrate_reason(struct page *page, int reason)
229 {
230 	struct page_ext *page_ext = page_ext_get(page);
231 	struct page_owner *page_owner;
232 
233 	if (unlikely(!page_ext))
234 		return;
235 
236 	page_owner = get_page_owner(page_ext);
237 	page_owner->last_migrate_reason = reason;
238 	page_ext_put(page_ext);
239 }
240 
__split_page_owner(struct page * page,unsigned int nr)241 void __split_page_owner(struct page *page, unsigned int nr)
242 {
243 	int i;
244 	struct page_ext *page_ext = page_ext_get(page);
245 	struct page_owner *page_owner;
246 
247 	if (unlikely(!page_ext))
248 		return;
249 
250 	for (i = 0; i < nr; i++) {
251 		page_owner = get_page_owner(page_ext);
252 		page_owner->order = 0;
253 		page_ext = page_ext_next(page_ext);
254 	}
255 	page_ext_put(page_ext);
256 }
257 
__copy_page_owner(struct page * oldpage,struct page * newpage)258 void __copy_page_owner(struct page *oldpage, struct page *newpage)
259 {
260 	struct page_ext *old_ext;
261 	struct page_ext *new_ext;
262 	struct page_owner *old_page_owner, *new_page_owner;
263 
264 	old_ext = page_ext_get(oldpage);
265 	if (unlikely(!old_ext))
266 		return;
267 
268 	new_ext = page_ext_get(newpage);
269 	if (unlikely(!new_ext)) {
270 		page_ext_put(old_ext);
271 		return;
272 	}
273 
274 	old_page_owner = get_page_owner(old_ext);
275 	new_page_owner = get_page_owner(new_ext);
276 	new_page_owner->order = old_page_owner->order;
277 	new_page_owner->gfp_mask = old_page_owner->gfp_mask;
278 	new_page_owner->last_migrate_reason =
279 		old_page_owner->last_migrate_reason;
280 	new_page_owner->handle = old_page_owner->handle;
281 	new_page_owner->pid = old_page_owner->pid;
282 	new_page_owner->ts_nsec = old_page_owner->ts_nsec;
283 	new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
284 
285 	/*
286 	 * We don't clear the bit on the oldpage as it's going to be freed
287 	 * after migration. Until then, the info can be useful in case of
288 	 * a bug, and the overal stats will be off a bit only temporarily.
289 	 * Also, migrate_misplaced_transhuge_page() can still fail the
290 	 * migration and then we want the oldpage to retain the info. But
291 	 * in that case we also don't need to explicitly clear the info from
292 	 * the new page, which will be freed.
293 	 */
294 	__set_bit(PAGE_EXT_OWNER, &new_ext->flags);
295 	__set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
296 	page_ext_put(new_ext);
297 	page_ext_put(old_ext);
298 }
299 
pagetypeinfo_showmixedcount_print(struct seq_file * m,pg_data_t * pgdat,struct zone * zone)300 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
301 				       pg_data_t *pgdat, struct zone *zone)
302 {
303 	struct page *page;
304 	struct page_ext *page_ext;
305 	struct page_owner *page_owner;
306 	unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
307 	unsigned long end_pfn = pfn + zone->spanned_pages;
308 	unsigned long count[MIGRATE_TYPES] = { 0, };
309 	int pageblock_mt, page_mt;
310 	int i;
311 
312 	/* Scan block by block. First and last block may be incomplete */
313 	pfn = zone->zone_start_pfn;
314 
315 	/*
316 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
317 	 * a zone boundary, it will be double counted between zones. This does
318 	 * not matter as the mixed block count will still be correct
319 	 */
320 	for (; pfn < end_pfn; ) {
321 		page = pfn_to_online_page(pfn);
322 		if (!page) {
323 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
324 			continue;
325 		}
326 
327 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
328 		block_end_pfn = min(block_end_pfn, end_pfn);
329 
330 		pageblock_mt = get_pageblock_migratetype(page);
331 
332 		for (; pfn < block_end_pfn; pfn++) {
333 			if (!pfn_valid_within(pfn))
334 				continue;
335 
336 			/* The pageblock is online, no need to recheck. */
337 			page = pfn_to_page(pfn);
338 
339 			if (page_zone(page) != zone)
340 				continue;
341 
342 			if (PageBuddy(page)) {
343 				unsigned long freepage_order;
344 
345 				freepage_order = buddy_order_unsafe(page);
346 				if (freepage_order < MAX_ORDER)
347 					pfn += (1UL << freepage_order) - 1;
348 				continue;
349 			}
350 
351 			if (PageReserved(page))
352 				continue;
353 
354 			page_ext = page_ext_get(page);
355 			if (unlikely(!page_ext))
356 				continue;
357 
358 			if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
359 				goto ext_put_continue;
360 
361 			page_owner = get_page_owner(page_ext);
362 			page_mt = gfp_migratetype(page_owner->gfp_mask);
363 			if (pageblock_mt != page_mt) {
364 				if (is_migrate_cma(pageblock_mt))
365 					count[MIGRATE_MOVABLE]++;
366 				else
367 					count[pageblock_mt]++;
368 
369 				pfn = block_end_pfn;
370 				page_ext_put(page_ext);
371 				break;
372 			}
373 			pfn += (1UL << page_owner->order) - 1;
374 ext_put_continue:
375 			page_ext_put(page_ext);
376 		}
377 	}
378 
379 	/* Print counts */
380 	seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
381 	for (i = 0; i < MIGRATE_TYPES; i++)
382 		seq_printf(m, "%12lu ", count[i]);
383 	seq_putc(m, '\n');
384 }
385 
386 static ssize_t
print_page_owner(char __user * buf,size_t count,unsigned long pfn,struct page * page,struct page_owner * page_owner,depot_stack_handle_t handle)387 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
388 		struct page *page, struct page_owner *page_owner,
389 		depot_stack_handle_t handle)
390 {
391 	int ret, pageblock_mt, page_mt;
392 	unsigned long *entries;
393 	unsigned int nr_entries;
394 	char *kbuf;
395 
396 	count = min_t(size_t, count, PAGE_SIZE);
397 	kbuf = kmalloc(count, GFP_KERNEL);
398 	if (!kbuf)
399 		return -ENOMEM;
400 
401 	ret = snprintf(kbuf, count,
402 			"Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n",
403 			page_owner->order, page_owner->gfp_mask,
404 			&page_owner->gfp_mask, page_owner->pid,
405 			page_owner->ts_nsec, page_owner->free_ts_nsec);
406 
407 	if (ret >= count)
408 		goto err;
409 
410 	/* Print information relevant to grouping pages by mobility */
411 	pageblock_mt = get_pageblock_migratetype(page);
412 	page_mt  = gfp_migratetype(page_owner->gfp_mask);
413 	ret += snprintf(kbuf + ret, count - ret,
414 			"PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
415 			pfn,
416 			migratetype_names[page_mt],
417 			pfn >> pageblock_order,
418 			migratetype_names[pageblock_mt],
419 			page->flags, &page->flags);
420 
421 	if (ret >= count)
422 		goto err;
423 
424 	nr_entries = stack_depot_fetch(handle, &entries);
425 	ret += stack_trace_snprint(kbuf + ret, count - ret, entries, nr_entries, 0);
426 	if (ret >= count)
427 		goto err;
428 
429 	if (page_owner->last_migrate_reason != -1) {
430 		ret += snprintf(kbuf + ret, count - ret,
431 			"Page has been migrated, last migrate reason: %s\n",
432 			migrate_reason_names[page_owner->last_migrate_reason]);
433 		if (ret >= count)
434 			goto err;
435 	}
436 
437 	ret += snprintf(kbuf + ret, count - ret, "\n");
438 	if (ret >= count)
439 		goto err;
440 
441 	if (copy_to_user(buf, kbuf, ret))
442 		ret = -EFAULT;
443 
444 	kfree(kbuf);
445 	return ret;
446 
447 err:
448 	kfree(kbuf);
449 	return -ENOMEM;
450 }
451 
__dump_page_owner(struct page * page)452 void __dump_page_owner(struct page *page)
453 {
454 	struct page_ext *page_ext = page_ext_get((void *)page);
455 	struct page_owner *page_owner;
456 	depot_stack_handle_t handle;
457 	unsigned long *entries;
458 	unsigned int nr_entries;
459 	gfp_t gfp_mask;
460 	int mt;
461 
462 	if (unlikely(!page_ext)) {
463 		pr_alert("There is not page extension available.\n");
464 		return;
465 	}
466 
467 	page_owner = get_page_owner(page_ext);
468 	gfp_mask = page_owner->gfp_mask;
469 	mt = gfp_migratetype(gfp_mask);
470 
471 	if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
472 		pr_alert("page_owner info is not present (never set?)\n");
473 		page_ext_put(page_ext);
474 		return;
475 	}
476 
477 	if (test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
478 		pr_alert("page_owner tracks the page as allocated\n");
479 	else
480 		pr_alert("page_owner tracks the page as freed\n");
481 
482 	pr_alert("page last allocated via order %u, migratetype %s, gfp_mask %#x(%pGg), pid %d, ts %llu, free_ts %llu\n",
483 		 page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask,
484 		 page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec);
485 
486 	handle = READ_ONCE(page_owner->handle);
487 	if (!handle) {
488 		pr_alert("page_owner allocation stack trace missing\n");
489 	} else {
490 		nr_entries = stack_depot_fetch(handle, &entries);
491 		stack_trace_print(entries, nr_entries, 0);
492 	}
493 
494 	handle = READ_ONCE(page_owner->free_handle);
495 	if (!handle) {
496 		pr_alert("page_owner free stack trace missing\n");
497 	} else {
498 		nr_entries = stack_depot_fetch(handle, &entries);
499 		pr_alert("page last free stack trace:\n");
500 		stack_trace_print(entries, nr_entries, 0);
501 	}
502 
503 	if (page_owner->last_migrate_reason != -1)
504 		pr_alert("page has been migrated, last migrate reason: %s\n",
505 			migrate_reason_names[page_owner->last_migrate_reason]);
506 	page_ext_put(page_ext);
507 }
508 
509 static ssize_t
read_page_owner(struct file * file,char __user * buf,size_t count,loff_t * ppos)510 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
511 {
512 	unsigned long pfn;
513 	struct page *page;
514 	struct page_ext *page_ext;
515 	struct page_owner *page_owner;
516 	depot_stack_handle_t handle;
517 
518 	if (!static_branch_unlikely(&page_owner_inited))
519 		return -EINVAL;
520 
521 	page = NULL;
522 	pfn = min_low_pfn + *ppos;
523 
524 	/* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
525 	while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
526 		pfn++;
527 
528 	drain_all_pages(NULL);
529 
530 	/* Find an allocated page */
531 	for (; pfn < max_pfn; pfn++) {
532 		/*
533 		 * This temporary page_owner is required so
534 		 * that we can avoid the context switches while holding
535 		 * the rcu lock and copying the page owner information to
536 		 * user through copy_to_user() or GFP_KERNEL allocations.
537 		 */
538 		struct page_owner page_owner_tmp;
539 
540 		/*
541 		 * If the new page is in a new MAX_ORDER_NR_PAGES area,
542 		 * validate the area as existing, skip it if not
543 		 */
544 		if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
545 			pfn += MAX_ORDER_NR_PAGES - 1;
546 			continue;
547 		}
548 
549 		/* Check for holes within a MAX_ORDER area */
550 		if (!pfn_valid_within(pfn))
551 			continue;
552 
553 		page = pfn_to_page(pfn);
554 		if (PageBuddy(page)) {
555 			unsigned long freepage_order = buddy_order_unsafe(page);
556 
557 			if (freepage_order < MAX_ORDER)
558 				pfn += (1UL << freepage_order) - 1;
559 			continue;
560 		}
561 
562 		page_ext = page_ext_get(page);
563 		if (unlikely(!page_ext))
564 			continue;
565 
566 		/*
567 		 * Some pages could be missed by concurrent allocation or free,
568 		 * because we don't hold the zone lock.
569 		 */
570 		if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
571 			goto ext_put_continue;
572 
573 		/*
574 		 * Although we do have the info about past allocation of free
575 		 * pages, it's not relevant for current memory usage.
576 		 */
577 		if (!test_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags))
578 			goto ext_put_continue;
579 
580 		page_owner = get_page_owner(page_ext);
581 
582 		/*
583 		 * Don't print "tail" pages of high-order allocations as that
584 		 * would inflate the stats.
585 		 */
586 		if (!IS_ALIGNED(pfn, 1 << page_owner->order))
587 			goto ext_put_continue;
588 
589 		/*
590 		 * Access to page_ext->handle isn't synchronous so we should
591 		 * be careful to access it.
592 		 */
593 		handle = READ_ONCE(page_owner->handle);
594 		if (!handle)
595 			goto ext_put_continue;
596 
597 		/* Record the next PFN to read in the file offset */
598 		*ppos = (pfn - min_low_pfn) + 1;
599 
600 		page_owner_tmp = *page_owner;
601 		page_ext_put(page_ext);
602 		return print_page_owner(buf, count, pfn, page,
603 				&page_owner_tmp, handle);
604 ext_put_continue:
605 		page_ext_put(page_ext);
606 	}
607 
608 	return 0;
609 }
610 
init_pages_in_zone(pg_data_t * pgdat,struct zone * zone)611 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
612 {
613 	unsigned long pfn = zone->zone_start_pfn;
614 	unsigned long end_pfn = zone_end_pfn(zone);
615 	unsigned long count = 0;
616 
617 	/*
618 	 * Walk the zone in pageblock_nr_pages steps. If a page block spans
619 	 * a zone boundary, it will be double counted between zones. This does
620 	 * not matter as the mixed block count will still be correct
621 	 */
622 	for (; pfn < end_pfn; ) {
623 		unsigned long block_end_pfn;
624 
625 		if (!pfn_valid(pfn)) {
626 			pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
627 			continue;
628 		}
629 
630 		block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
631 		block_end_pfn = min(block_end_pfn, end_pfn);
632 
633 		for (; pfn < block_end_pfn; pfn++) {
634 			struct page *page;
635 			struct page_ext *page_ext;
636 
637 			if (!pfn_valid_within(pfn))
638 				continue;
639 
640 			page = pfn_to_page(pfn);
641 
642 			if (page_zone(page) != zone)
643 				continue;
644 
645 			/*
646 			 * To avoid having to grab zone->lock, be a little
647 			 * careful when reading buddy page order. The only
648 			 * danger is that we skip too much and potentially miss
649 			 * some early allocated pages, which is better than
650 			 * heavy lock contention.
651 			 */
652 			if (PageBuddy(page)) {
653 				unsigned long order = buddy_order_unsafe(page);
654 
655 				if (order > 0 && order < MAX_ORDER)
656 					pfn += (1UL << order) - 1;
657 				continue;
658 			}
659 
660 			if (PageReserved(page))
661 				continue;
662 
663 			page_ext = page_ext_get(page);
664 			if (unlikely(!page_ext))
665 				continue;
666 
667 			/* Maybe overlapping zone */
668 			if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
669 				goto ext_put_continue;
670 
671 			/* Found early allocated page */
672 			__set_page_owner_handle(page, page_ext, early_handle,
673 						0, 0);
674 			count++;
675 ext_put_continue:
676 			page_ext_put(page_ext);
677 		}
678 		cond_resched();
679 	}
680 
681 	pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
682 		pgdat->node_id, zone->name, count);
683 }
684 
init_zones_in_node(pg_data_t * pgdat)685 static void init_zones_in_node(pg_data_t *pgdat)
686 {
687 	struct zone *zone;
688 	struct zone *node_zones = pgdat->node_zones;
689 
690 	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
691 		if (!populated_zone(zone))
692 			continue;
693 
694 		init_pages_in_zone(pgdat, zone);
695 	}
696 }
697 
init_early_allocated_pages(void)698 static void init_early_allocated_pages(void)
699 {
700 	pg_data_t *pgdat;
701 
702 	for_each_online_pgdat(pgdat)
703 		init_zones_in_node(pgdat);
704 }
705 
706 static const struct file_operations proc_page_owner_operations = {
707 	.read		= read_page_owner,
708 };
709 
pageowner_init(void)710 static int __init pageowner_init(void)
711 {
712 	if (!static_branch_unlikely(&page_owner_inited)) {
713 		pr_info("page_owner is disabled\n");
714 		return 0;
715 	}
716 
717 	debugfs_create_file("page_owner", 0400, NULL, NULL,
718 			    &proc_page_owner_operations);
719 
720 	return 0;
721 }
722 late_initcall(pageowner_init)
723