page_owner.c 15 KB

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