page_owner.c 15 KB

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