page_owner.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/bootmem.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 "internal.h"
  14. /*
  15. * TODO: teach PAGE_OWNER_STACK_DEPTH (__dump_page_owner and save_stack)
  16. * to use off stack temporal storage
  17. */
  18. #define PAGE_OWNER_STACK_DEPTH (16)
  19. struct page_owner {
  20. unsigned short order;
  21. short last_migrate_reason;
  22. gfp_t gfp_mask;
  23. depot_stack_handle_t handle;
  24. };
  25. static bool page_owner_disabled = true;
  26. DEFINE_STATIC_KEY_FALSE(page_owner_inited);
  27. static depot_stack_handle_t dummy_handle;
  28. static depot_stack_handle_t failure_handle;
  29. static depot_stack_handle_t early_handle;
  30. static void init_early_allocated_pages(void);
  31. static int __init early_page_owner_param(char *buf)
  32. {
  33. if (!buf)
  34. return -EINVAL;
  35. if (strcmp(buf, "on") == 0)
  36. page_owner_disabled = false;
  37. return 0;
  38. }
  39. early_param("page_owner", early_page_owner_param);
  40. static bool need_page_owner(void)
  41. {
  42. if (page_owner_disabled)
  43. return false;
  44. return true;
  45. }
  46. static __always_inline depot_stack_handle_t create_dummy_stack(void)
  47. {
  48. unsigned long entries[4];
  49. struct stack_trace dummy;
  50. dummy.nr_entries = 0;
  51. dummy.max_entries = ARRAY_SIZE(entries);
  52. dummy.entries = &entries[0];
  53. dummy.skip = 0;
  54. save_stack_trace(&dummy);
  55. return depot_save_stack(&dummy, GFP_KERNEL);
  56. }
  57. static noinline void register_dummy_stack(void)
  58. {
  59. dummy_handle = create_dummy_stack();
  60. }
  61. static noinline void register_failure_stack(void)
  62. {
  63. failure_handle = create_dummy_stack();
  64. }
  65. static noinline void register_early_stack(void)
  66. {
  67. early_handle = create_dummy_stack();
  68. }
  69. static void init_page_owner(void)
  70. {
  71. if (page_owner_disabled)
  72. return;
  73. register_dummy_stack();
  74. register_failure_stack();
  75. register_early_stack();
  76. static_branch_enable(&page_owner_inited);
  77. init_early_allocated_pages();
  78. }
  79. struct page_ext_operations page_owner_ops = {
  80. .size = sizeof(struct page_owner),
  81. .need = need_page_owner,
  82. .init = init_page_owner,
  83. };
  84. static inline struct page_owner *get_page_owner(struct page_ext *page_ext)
  85. {
  86. return (void *)page_ext + page_owner_ops.offset;
  87. }
  88. void __reset_page_owner(struct page *page, unsigned int order)
  89. {
  90. int i;
  91. struct page_ext *page_ext;
  92. for (i = 0; i < (1 << order); i++) {
  93. page_ext = lookup_page_ext(page + i);
  94. if (unlikely(!page_ext))
  95. continue;
  96. __clear_bit(PAGE_EXT_OWNER, &page_ext->flags);
  97. }
  98. }
  99. static inline bool check_recursive_alloc(struct stack_trace *trace,
  100. unsigned long ip)
  101. {
  102. int i;
  103. if (!trace->nr_entries)
  104. return false;
  105. for (i = 0; i < trace->nr_entries; i++) {
  106. if (trace->entries[i] == ip)
  107. return true;
  108. }
  109. return false;
  110. }
  111. static noinline depot_stack_handle_t save_stack(gfp_t flags)
  112. {
  113. unsigned long entries[PAGE_OWNER_STACK_DEPTH];
  114. struct stack_trace trace = {
  115. .nr_entries = 0,
  116. .entries = entries,
  117. .max_entries = PAGE_OWNER_STACK_DEPTH,
  118. .skip = 2
  119. };
  120. depot_stack_handle_t handle;
  121. save_stack_trace(&trace);
  122. if (trace.nr_entries != 0 &&
  123. trace.entries[trace.nr_entries-1] == ULONG_MAX)
  124. trace.nr_entries--;
  125. /*
  126. * We need to check recursion here because our request to stackdepot
  127. * could trigger memory allocation to save new entry. New memory
  128. * allocation would reach here and call depot_save_stack() again
  129. * if we don't catch it. There is still not enough memory in stackdepot
  130. * so it would try to allocate memory again and loop forever.
  131. */
  132. if (check_recursive_alloc(&trace, _RET_IP_))
  133. return dummy_handle;
  134. handle = depot_save_stack(&trace, flags);
  135. if (!handle)
  136. handle = failure_handle;
  137. return handle;
  138. }
  139. static inline void __set_page_owner_handle(struct page_ext *page_ext,
  140. depot_stack_handle_t handle, unsigned int order, gfp_t gfp_mask)
  141. {
  142. struct page_owner *page_owner;
  143. page_owner = get_page_owner(page_ext);
  144. page_owner->handle = handle;
  145. page_owner->order = order;
  146. page_owner->gfp_mask = gfp_mask;
  147. page_owner->last_migrate_reason = -1;
  148. __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
  149. }
  150. noinline void __set_page_owner(struct page *page, unsigned int order,
  151. gfp_t gfp_mask)
  152. {
  153. struct page_ext *page_ext = lookup_page_ext(page);
  154. depot_stack_handle_t handle;
  155. if (unlikely(!page_ext))
  156. return;
  157. handle = save_stack(gfp_mask);
  158. __set_page_owner_handle(page_ext, handle, order, gfp_mask);
  159. }
  160. void __set_page_owner_migrate_reason(struct page *page, int reason)
  161. {
  162. struct page_ext *page_ext = lookup_page_ext(page);
  163. struct page_owner *page_owner;
  164. if (unlikely(!page_ext))
  165. return;
  166. page_owner = get_page_owner(page_ext);
  167. page_owner->last_migrate_reason = reason;
  168. }
  169. void __split_page_owner(struct page *page, unsigned int order)
  170. {
  171. int i;
  172. struct page_ext *page_ext = lookup_page_ext(page);
  173. struct page_owner *page_owner;
  174. if (unlikely(!page_ext))
  175. return;
  176. page_owner = get_page_owner(page_ext);
  177. page_owner->order = 0;
  178. for (i = 1; i < (1 << order); i++)
  179. __copy_page_owner(page, page + i);
  180. }
  181. void __copy_page_owner(struct page *oldpage, struct page *newpage)
  182. {
  183. struct page_ext *old_ext = lookup_page_ext(oldpage);
  184. struct page_ext *new_ext = lookup_page_ext(newpage);
  185. struct page_owner *old_page_owner, *new_page_owner;
  186. if (unlikely(!old_ext || !new_ext))
  187. return;
  188. old_page_owner = get_page_owner(old_ext);
  189. new_page_owner = get_page_owner(new_ext);
  190. new_page_owner->order = old_page_owner->order;
  191. new_page_owner->gfp_mask = old_page_owner->gfp_mask;
  192. new_page_owner->last_migrate_reason =
  193. old_page_owner->last_migrate_reason;
  194. new_page_owner->handle = old_page_owner->handle;
  195. /*
  196. * We don't clear the bit on the oldpage as it's going to be freed
  197. * after migration. Until then, the info can be useful in case of
  198. * a bug, and the overal stats will be off a bit only temporarily.
  199. * Also, migrate_misplaced_transhuge_page() can still fail the
  200. * migration and then we want the oldpage to retain the info. But
  201. * in that case we also don't need to explicitly clear the info from
  202. * the new page, which will be freed.
  203. */
  204. __set_bit(PAGE_EXT_OWNER, &new_ext->flags);
  205. }
  206. void pagetypeinfo_showmixedcount_print(struct seq_file *m,
  207. pg_data_t *pgdat, struct zone *zone)
  208. {
  209. struct page *page;
  210. struct page_ext *page_ext;
  211. struct page_owner *page_owner;
  212. unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
  213. unsigned long end_pfn = pfn + zone->spanned_pages;
  214. unsigned long count[MIGRATE_TYPES] = { 0, };
  215. int pageblock_mt, page_mt;
  216. int i;
  217. /* Scan block by block. First and last block may be incomplete */
  218. pfn = zone->zone_start_pfn;
  219. /*
  220. * Walk the zone in pageblock_nr_pages steps. If a page block spans
  221. * a zone boundary, it will be double counted between zones. This does
  222. * not matter as the mixed block count will still be correct
  223. */
  224. for (; pfn < end_pfn; ) {
  225. page = pfn_to_online_page(pfn);
  226. if (!page) {
  227. pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
  228. continue;
  229. }
  230. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  231. block_end_pfn = min(block_end_pfn, end_pfn);
  232. pageblock_mt = get_pageblock_migratetype(page);
  233. for (; pfn < block_end_pfn; pfn++) {
  234. if (!pfn_valid_within(pfn))
  235. continue;
  236. /* The pageblock is online, no need to recheck. */
  237. page = pfn_to_page(pfn);
  238. if (page_zone(page) != zone)
  239. continue;
  240. if (PageBuddy(page)) {
  241. unsigned long freepage_order;
  242. freepage_order = page_order_unsafe(page);
  243. if (freepage_order < MAX_ORDER)
  244. pfn += (1UL << freepage_order) - 1;
  245. continue;
  246. }
  247. if (PageReserved(page))
  248. continue;
  249. page_ext = lookup_page_ext(page);
  250. if (unlikely(!page_ext))
  251. continue;
  252. if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
  253. continue;
  254. page_owner = get_page_owner(page_ext);
  255. page_mt = gfpflags_to_migratetype(
  256. page_owner->gfp_mask);
  257. if (pageblock_mt != page_mt) {
  258. if (is_migrate_cma(pageblock_mt))
  259. count[MIGRATE_MOVABLE]++;
  260. else
  261. count[pageblock_mt]++;
  262. pfn = block_end_pfn;
  263. break;
  264. }
  265. pfn += (1UL << page_owner->order) - 1;
  266. }
  267. }
  268. /* Print counts */
  269. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  270. for (i = 0; i < MIGRATE_TYPES; i++)
  271. seq_printf(m, "%12lu ", count[i]);
  272. seq_putc(m, '\n');
  273. }
  274. static ssize_t
  275. print_page_owner(char __user *buf, size_t count, unsigned long pfn,
  276. struct page *page, struct page_owner *page_owner,
  277. depot_stack_handle_t handle)
  278. {
  279. int ret;
  280. int pageblock_mt, page_mt;
  281. char *kbuf;
  282. unsigned long entries[PAGE_OWNER_STACK_DEPTH];
  283. struct stack_trace trace = {
  284. .nr_entries = 0,
  285. .entries = entries,
  286. .max_entries = PAGE_OWNER_STACK_DEPTH,
  287. .skip = 0
  288. };
  289. kbuf = kmalloc(count, GFP_KERNEL);
  290. if (!kbuf)
  291. return -ENOMEM;
  292. ret = snprintf(kbuf, count,
  293. "Page allocated via order %u, mask %#x(%pGg)\n",
  294. page_owner->order, page_owner->gfp_mask,
  295. &page_owner->gfp_mask);
  296. if (ret >= count)
  297. goto err;
  298. /* Print information relevant to grouping pages by mobility */
  299. pageblock_mt = get_pageblock_migratetype(page);
  300. page_mt = gfpflags_to_migratetype(page_owner->gfp_mask);
  301. ret += snprintf(kbuf + ret, count - ret,
  302. "PFN %lu type %s Block %lu type %s Flags %#lx(%pGp)\n",
  303. pfn,
  304. migratetype_names[page_mt],
  305. pfn >> pageblock_order,
  306. migratetype_names[pageblock_mt],
  307. page->flags, &page->flags);
  308. if (ret >= count)
  309. goto err;
  310. depot_fetch_stack(handle, &trace);
  311. ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
  312. if (ret >= count)
  313. goto err;
  314. if (page_owner->last_migrate_reason != -1) {
  315. ret += snprintf(kbuf + ret, count - ret,
  316. "Page has been migrated, last migrate reason: %s\n",
  317. migrate_reason_names[page_owner->last_migrate_reason]);
  318. if (ret >= count)
  319. goto err;
  320. }
  321. ret += snprintf(kbuf + ret, count - ret, "\n");
  322. if (ret >= count)
  323. goto err;
  324. if (copy_to_user(buf, kbuf, ret))
  325. ret = -EFAULT;
  326. kfree(kbuf);
  327. return ret;
  328. err:
  329. kfree(kbuf);
  330. return -ENOMEM;
  331. }
  332. void __dump_page_owner(struct page *page)
  333. {
  334. struct page_ext *page_ext = lookup_page_ext(page);
  335. struct page_owner *page_owner;
  336. unsigned long entries[PAGE_OWNER_STACK_DEPTH];
  337. struct stack_trace trace = {
  338. .nr_entries = 0,
  339. .entries = entries,
  340. .max_entries = PAGE_OWNER_STACK_DEPTH,
  341. .skip = 0
  342. };
  343. depot_stack_handle_t handle;
  344. gfp_t gfp_mask;
  345. int mt;
  346. if (unlikely(!page_ext)) {
  347. pr_alert("There is not page extension available.\n");
  348. return;
  349. }
  350. page_owner = get_page_owner(page_ext);
  351. gfp_mask = page_owner->gfp_mask;
  352. mt = gfpflags_to_migratetype(gfp_mask);
  353. if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags)) {
  354. pr_alert("page_owner info is not active (free page?)\n");
  355. return;
  356. }
  357. handle = READ_ONCE(page_owner->handle);
  358. if (!handle) {
  359. pr_alert("page_owner info is not active (free page?)\n");
  360. return;
  361. }
  362. depot_fetch_stack(handle, &trace);
  363. pr_alert("page allocated via order %u, migratetype %s, gfp_mask %#x(%pGg)\n",
  364. page_owner->order, migratetype_names[mt], gfp_mask, &gfp_mask);
  365. print_stack_trace(&trace, 0);
  366. if (page_owner->last_migrate_reason != -1)
  367. pr_alert("page has been migrated, last migrate reason: %s\n",
  368. migrate_reason_names[page_owner->last_migrate_reason]);
  369. }
  370. static ssize_t
  371. read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  372. {
  373. unsigned long pfn;
  374. struct page *page;
  375. struct page_ext *page_ext;
  376. struct page_owner *page_owner;
  377. depot_stack_handle_t handle;
  378. if (!static_branch_unlikely(&page_owner_inited))
  379. return -EINVAL;
  380. page = NULL;
  381. pfn = min_low_pfn + *ppos;
  382. /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
  383. while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
  384. pfn++;
  385. drain_all_pages(NULL);
  386. /* Find an allocated page */
  387. for (; pfn < max_pfn; pfn++) {
  388. /*
  389. * If the new page is in a new MAX_ORDER_NR_PAGES area,
  390. * validate the area as existing, skip it if not
  391. */
  392. if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
  393. pfn += MAX_ORDER_NR_PAGES - 1;
  394. continue;
  395. }
  396. /* Check for holes within a MAX_ORDER area */
  397. if (!pfn_valid_within(pfn))
  398. continue;
  399. page = pfn_to_page(pfn);
  400. if (PageBuddy(page)) {
  401. unsigned long freepage_order = page_order_unsafe(page);
  402. if (freepage_order < MAX_ORDER)
  403. pfn += (1UL << freepage_order) - 1;
  404. continue;
  405. }
  406. page_ext = lookup_page_ext(page);
  407. if (unlikely(!page_ext))
  408. continue;
  409. /*
  410. * Some pages could be missed by concurrent allocation or free,
  411. * because we don't hold the zone lock.
  412. */
  413. if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
  414. continue;
  415. page_owner = get_page_owner(page_ext);
  416. /*
  417. * Access to page_ext->handle isn't synchronous so we should
  418. * be careful to access it.
  419. */
  420. handle = READ_ONCE(page_owner->handle);
  421. if (!handle)
  422. continue;
  423. /* Record the next PFN to read in the file offset */
  424. *ppos = (pfn - min_low_pfn) + 1;
  425. return print_page_owner(buf, count, pfn, page,
  426. page_owner, handle);
  427. }
  428. return 0;
  429. }
  430. static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
  431. {
  432. unsigned long pfn = zone->zone_start_pfn;
  433. unsigned long end_pfn = zone_end_pfn(zone);
  434. unsigned long count = 0;
  435. /*
  436. * Walk the zone in pageblock_nr_pages steps. If a page block spans
  437. * a zone boundary, it will be double counted between zones. This does
  438. * not matter as the mixed block count will still be correct
  439. */
  440. for (; pfn < end_pfn; ) {
  441. unsigned long block_end_pfn;
  442. if (!pfn_valid(pfn)) {
  443. pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
  444. continue;
  445. }
  446. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  447. block_end_pfn = min(block_end_pfn, end_pfn);
  448. for (; pfn < block_end_pfn; pfn++) {
  449. struct page *page;
  450. struct page_ext *page_ext;
  451. if (!pfn_valid_within(pfn))
  452. continue;
  453. page = pfn_to_page(pfn);
  454. if (page_zone(page) != zone)
  455. continue;
  456. /*
  457. * To avoid having to grab zone->lock, be a little
  458. * careful when reading buddy page order. The only
  459. * danger is that we skip too much and potentially miss
  460. * some early allocated pages, which is better than
  461. * heavy lock contention.
  462. */
  463. if (PageBuddy(page)) {
  464. unsigned long order = page_order_unsafe(page);
  465. if (order > 0 && order < MAX_ORDER)
  466. pfn += (1UL << order) - 1;
  467. continue;
  468. }
  469. if (PageReserved(page))
  470. continue;
  471. page_ext = lookup_page_ext(page);
  472. if (unlikely(!page_ext))
  473. continue;
  474. /* Maybe overlapping zone */
  475. if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
  476. continue;
  477. /* Found early allocated page */
  478. __set_page_owner_handle(page_ext, early_handle, 0, 0);
  479. count++;
  480. }
  481. cond_resched();
  482. }
  483. pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
  484. pgdat->node_id, zone->name, count);
  485. }
  486. static void init_zones_in_node(pg_data_t *pgdat)
  487. {
  488. struct zone *zone;
  489. struct zone *node_zones = pgdat->node_zones;
  490. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  491. if (!populated_zone(zone))
  492. continue;
  493. init_pages_in_zone(pgdat, zone);
  494. }
  495. }
  496. static void init_early_allocated_pages(void)
  497. {
  498. pg_data_t *pgdat;
  499. for_each_online_pgdat(pgdat)
  500. init_zones_in_node(pgdat);
  501. }
  502. static const struct file_operations proc_page_owner_operations = {
  503. .read = read_page_owner,
  504. };
  505. static int __init pageowner_init(void)
  506. {
  507. struct dentry *dentry;
  508. if (!static_branch_unlikely(&page_owner_inited)) {
  509. pr_info("page_owner is disabled\n");
  510. return 0;
  511. }
  512. dentry = debugfs_create_file("page_owner", 0400, NULL,
  513. NULL, &proc_page_owner_operations);
  514. return PTR_ERR_OR_ZERO(dentry);
  515. }
  516. late_initcall(pageowner_init)