page_owner.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "internal.h"
  9. static bool page_owner_disabled = true;
  10. bool page_owner_inited __read_mostly;
  11. static void init_early_allocated_pages(void);
  12. static int early_page_owner_param(char *buf)
  13. {
  14. if (!buf)
  15. return -EINVAL;
  16. if (strcmp(buf, "on") == 0)
  17. page_owner_disabled = false;
  18. return 0;
  19. }
  20. early_param("page_owner", early_page_owner_param);
  21. static bool need_page_owner(void)
  22. {
  23. if (page_owner_disabled)
  24. return false;
  25. return true;
  26. }
  27. static void init_page_owner(void)
  28. {
  29. if (page_owner_disabled)
  30. return;
  31. page_owner_inited = true;
  32. init_early_allocated_pages();
  33. }
  34. struct page_ext_operations page_owner_ops = {
  35. .need = need_page_owner,
  36. .init = init_page_owner,
  37. };
  38. void __reset_page_owner(struct page *page, unsigned int order)
  39. {
  40. int i;
  41. struct page_ext *page_ext;
  42. for (i = 0; i < (1 << order); i++) {
  43. page_ext = lookup_page_ext(page + i);
  44. if (unlikely(!page_ext))
  45. continue;
  46. __clear_bit(PAGE_EXT_OWNER, &page_ext->flags);
  47. }
  48. }
  49. void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask)
  50. {
  51. struct page_ext *page_ext = lookup_page_ext(page);
  52. struct stack_trace trace = {
  53. .nr_entries = 0,
  54. .max_entries = ARRAY_SIZE(page_ext->trace_entries),
  55. .entries = &page_ext->trace_entries[0],
  56. .skip = 3,
  57. };
  58. if (unlikely(!page_ext))
  59. return;
  60. save_stack_trace(&trace);
  61. page_ext->order = order;
  62. page_ext->gfp_mask = gfp_mask;
  63. page_ext->nr_entries = trace.nr_entries;
  64. __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
  65. }
  66. gfp_t __get_page_owner_gfp(struct page *page)
  67. {
  68. struct page_ext *page_ext = lookup_page_ext(page);
  69. if (unlikely(!page_ext))
  70. /*
  71. * The caller just returns 0 if no valid gfp
  72. * So return 0 here too.
  73. */
  74. return 0;
  75. return page_ext->gfp_mask;
  76. }
  77. static ssize_t
  78. print_page_owner(char __user *buf, size_t count, unsigned long pfn,
  79. struct page *page, struct page_ext *page_ext)
  80. {
  81. int ret;
  82. int pageblock_mt, page_mt;
  83. char *kbuf;
  84. struct stack_trace trace = {
  85. .nr_entries = page_ext->nr_entries,
  86. .entries = &page_ext->trace_entries[0],
  87. };
  88. kbuf = kmalloc(count, GFP_KERNEL);
  89. if (!kbuf)
  90. return -ENOMEM;
  91. ret = snprintf(kbuf, count,
  92. "Page allocated via order %u, mask 0x%x\n",
  93. page_ext->order, page_ext->gfp_mask);
  94. if (ret >= count)
  95. goto err;
  96. /* Print information relevant to grouping pages by mobility */
  97. pageblock_mt = get_pfnblock_migratetype(page, pfn);
  98. page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
  99. ret += snprintf(kbuf + ret, count - ret,
  100. "PFN %lu Block %lu type %d %s Flags %s%s%s%s%s%s%s%s%s%s%s%s\n",
  101. pfn,
  102. pfn >> pageblock_order,
  103. pageblock_mt,
  104. pageblock_mt != page_mt ? "Fallback" : " ",
  105. PageLocked(page) ? "K" : " ",
  106. PageError(page) ? "E" : " ",
  107. PageReferenced(page) ? "R" : " ",
  108. PageUptodate(page) ? "U" : " ",
  109. PageDirty(page) ? "D" : " ",
  110. PageLRU(page) ? "L" : " ",
  111. PageActive(page) ? "A" : " ",
  112. PageSlab(page) ? "S" : " ",
  113. PageWriteback(page) ? "W" : " ",
  114. PageCompound(page) ? "C" : " ",
  115. PageSwapCache(page) ? "B" : " ",
  116. PageMappedToDisk(page) ? "M" : " ");
  117. if (ret >= count)
  118. goto err;
  119. ret += snprint_stack_trace(kbuf + ret, count - ret, &trace, 0);
  120. if (ret >= count)
  121. goto err;
  122. ret += snprintf(kbuf + ret, count - ret, "\n");
  123. if (ret >= count)
  124. goto err;
  125. if (copy_to_user(buf, kbuf, ret))
  126. ret = -EFAULT;
  127. kfree(kbuf);
  128. return ret;
  129. err:
  130. kfree(kbuf);
  131. return -ENOMEM;
  132. }
  133. static ssize_t
  134. read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  135. {
  136. unsigned long pfn;
  137. struct page *page;
  138. struct page_ext *page_ext;
  139. if (!page_owner_inited)
  140. return -EINVAL;
  141. page = NULL;
  142. pfn = min_low_pfn + *ppos;
  143. /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
  144. while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
  145. pfn++;
  146. drain_all_pages(NULL);
  147. /* Find an allocated page */
  148. for (; pfn < max_pfn; pfn++) {
  149. /*
  150. * If the new page is in a new MAX_ORDER_NR_PAGES area,
  151. * validate the area as existing, skip it if not
  152. */
  153. if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
  154. pfn += MAX_ORDER_NR_PAGES - 1;
  155. continue;
  156. }
  157. /* Check for holes within a MAX_ORDER area */
  158. if (!pfn_valid_within(pfn))
  159. continue;
  160. page = pfn_to_page(pfn);
  161. if (PageBuddy(page)) {
  162. unsigned long freepage_order = page_order_unsafe(page);
  163. if (freepage_order < MAX_ORDER)
  164. pfn += (1UL << freepage_order) - 1;
  165. continue;
  166. }
  167. page_ext = lookup_page_ext(page);
  168. if (unlikely(!page_ext))
  169. continue;
  170. /*
  171. * Some pages could be missed by concurrent allocation or free,
  172. * because we don't hold the zone lock.
  173. */
  174. if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
  175. continue;
  176. /* Record the next PFN to read in the file offset */
  177. *ppos = (pfn - min_low_pfn) + 1;
  178. return print_page_owner(buf, count, pfn, page, page_ext);
  179. }
  180. return 0;
  181. }
  182. static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
  183. {
  184. struct page *page;
  185. struct page_ext *page_ext;
  186. unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
  187. unsigned long end_pfn = pfn + zone->spanned_pages;
  188. unsigned long count = 0;
  189. /* Scan block by block. First and last block may be incomplete */
  190. pfn = zone->zone_start_pfn;
  191. /*
  192. * Walk the zone in pageblock_nr_pages steps. If a page block spans
  193. * a zone boundary, it will be double counted between zones. This does
  194. * not matter as the mixed block count will still be correct
  195. */
  196. for (; pfn < end_pfn; ) {
  197. if (!pfn_valid(pfn)) {
  198. pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
  199. continue;
  200. }
  201. block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
  202. block_end_pfn = min(block_end_pfn, end_pfn);
  203. page = pfn_to_page(pfn);
  204. for (; pfn < block_end_pfn; pfn++) {
  205. if (!pfn_valid_within(pfn))
  206. continue;
  207. page = pfn_to_page(pfn);
  208. /*
  209. * We are safe to check buddy flag and order, because
  210. * this is init stage and only single thread runs.
  211. */
  212. if (PageBuddy(page)) {
  213. pfn += (1UL << page_order(page)) - 1;
  214. continue;
  215. }
  216. if (PageReserved(page))
  217. continue;
  218. page_ext = lookup_page_ext(page);
  219. if (unlikely(!page_ext))
  220. continue;
  221. /* Maybe overraping zone */
  222. if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
  223. continue;
  224. /* Found early allocated page */
  225. set_page_owner(page, 0, 0);
  226. count++;
  227. }
  228. }
  229. pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
  230. pgdat->node_id, zone->name, count);
  231. }
  232. static void init_zones_in_node(pg_data_t *pgdat)
  233. {
  234. struct zone *zone;
  235. struct zone *node_zones = pgdat->node_zones;
  236. unsigned long flags;
  237. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  238. if (!populated_zone(zone))
  239. continue;
  240. spin_lock_irqsave(&zone->lock, flags);
  241. init_pages_in_zone(pgdat, zone);
  242. spin_unlock_irqrestore(&zone->lock, flags);
  243. }
  244. }
  245. static void init_early_allocated_pages(void)
  246. {
  247. pg_data_t *pgdat;
  248. drain_all_pages(NULL);
  249. for_each_online_pgdat(pgdat)
  250. init_zones_in_node(pgdat);
  251. }
  252. static const struct file_operations proc_page_owner_operations = {
  253. .read = read_page_owner,
  254. };
  255. static int __init pageowner_init(void)
  256. {
  257. struct dentry *dentry;
  258. if (!page_owner_inited) {
  259. pr_info("page_owner is disabled\n");
  260. return 0;
  261. }
  262. dentry = debugfs_create_file("page_owner", S_IRUSR, NULL,
  263. NULL, &proc_page_owner_operations);
  264. if (IS_ERR(dentry))
  265. return PTR_ERR(dentry);
  266. return 0;
  267. }
  268. late_initcall(pageowner_init)