debug.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/debug.c
  4. *
  5. * mm/ specific debug routines.
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/trace_events.h>
  11. #include <linux/memcontrol.h>
  12. #include <trace/events/mmflags.h>
  13. #include <linux/migrate.h>
  14. #include <linux/page_owner.h>
  15. #include <linux/page_pinner.h>
  16. #include <linux/ctype.h>
  17. #include "internal.h"
  18. const char *migrate_reason_names[MR_TYPES] = {
  19. "compaction",
  20. "memory_failure",
  21. "memory_hotplug",
  22. "syscall_or_cpuset",
  23. "mempolicy_mbind",
  24. "numa_misplaced",
  25. "cma",
  26. };
  27. const struct trace_print_flags pageflag_names[] = {
  28. __def_pageflag_names,
  29. {0, NULL}
  30. };
  31. const struct trace_print_flags gfpflag_names[] = {
  32. __def_gfpflag_names,
  33. {0, NULL}
  34. };
  35. const struct trace_print_flags vmaflag_names[] = {
  36. __def_vmaflag_names,
  37. {0, NULL}
  38. };
  39. void __dump_page(struct page *page, const char *reason)
  40. {
  41. struct page *head = compound_head(page);
  42. struct address_space *mapping;
  43. bool page_poisoned = PagePoisoned(page);
  44. bool compound = PageCompound(page);
  45. /*
  46. * Accessing the pageblock without the zone lock. It could change to
  47. * "isolate" again in the meantime, but since we are just dumping the
  48. * state for debugging, it should be fine to accept a bit of
  49. * inaccuracy here due to racing.
  50. */
  51. bool page_cma = is_migrate_cma_page(page);
  52. int mapcount;
  53. char *type = "";
  54. /*
  55. * If struct page is poisoned don't access Page*() functions as that
  56. * leads to recursive loop. Page*() check for poisoned pages, and calls
  57. * dump_page() when detected.
  58. */
  59. if (page_poisoned) {
  60. pr_warn("page:%px is uninitialized and poisoned", page);
  61. goto hex_only;
  62. }
  63. if (page < head || (page >= head + MAX_ORDER_NR_PAGES)) {
  64. /*
  65. * Corrupt page, so we cannot call page_mapping. Instead, do a
  66. * safe subset of the steps that page_mapping() does. Caution:
  67. * this will be misleading for tail pages, PageSwapCache pages,
  68. * and potentially other situations. (See the page_mapping()
  69. * implementation for what's missing here.)
  70. */
  71. unsigned long tmp = (unsigned long)page->mapping;
  72. if (tmp & PAGE_MAPPING_ANON)
  73. mapping = NULL;
  74. else
  75. mapping = (void *)(tmp & ~PAGE_MAPPING_FLAGS);
  76. head = page;
  77. compound = false;
  78. } else {
  79. mapping = page_mapping(page);
  80. }
  81. /*
  82. * Avoid VM_BUG_ON() in page_mapcount().
  83. * page->_mapcount space in struct page is used by sl[aou]b pages to
  84. * encode own info.
  85. */
  86. mapcount = PageSlab(head) ? 0 : page_mapcount(page);
  87. pr_warn("page:%p refcount:%d mapcount:%d mapping:%p index:%#lx pfn:%#lx\n",
  88. page, page_ref_count(head), mapcount, mapping,
  89. page_to_pgoff(page), page_to_pfn(page));
  90. if (compound) {
  91. if (hpage_pincount_available(page)) {
  92. pr_warn("head:%p order:%u compound_mapcount:%d compound_pincount:%d\n",
  93. head, compound_order(head),
  94. head_compound_mapcount(head),
  95. head_compound_pincount(head));
  96. } else {
  97. pr_warn("head:%p order:%u compound_mapcount:%d\n",
  98. head, compound_order(head),
  99. head_compound_mapcount(head));
  100. }
  101. }
  102. if (PageKsm(page))
  103. type = "ksm ";
  104. else if (PageAnon(page))
  105. type = "anon ";
  106. else if (mapping) {
  107. struct inode *host;
  108. const struct address_space_operations *a_ops;
  109. struct hlist_node *dentry_first;
  110. struct dentry *dentry_ptr;
  111. struct dentry dentry;
  112. unsigned long ino;
  113. /*
  114. * mapping can be invalid pointer and we don't want to crash
  115. * accessing it, so probe everything depending on it carefully
  116. */
  117. if (get_kernel_nofault(host, &mapping->host) ||
  118. get_kernel_nofault(a_ops, &mapping->a_ops)) {
  119. pr_warn("failed to read mapping contents, not a valid kernel address?\n");
  120. goto out_mapping;
  121. }
  122. if (!host) {
  123. pr_warn("aops:%ps\n", a_ops);
  124. goto out_mapping;
  125. }
  126. if (get_kernel_nofault(dentry_first, &host->i_dentry.first) ||
  127. get_kernel_nofault(ino, &host->i_ino)) {
  128. pr_warn("aops:%ps with invalid host inode %px\n",
  129. a_ops, host);
  130. goto out_mapping;
  131. }
  132. if (!dentry_first) {
  133. pr_warn("aops:%ps ino:%lx\n", a_ops, ino);
  134. goto out_mapping;
  135. }
  136. dentry_ptr = container_of(dentry_first, struct dentry, d_u.d_alias);
  137. if (get_kernel_nofault(dentry, dentry_ptr)) {
  138. pr_warn("aops:%ps ino:%lx with invalid dentry %px\n",
  139. a_ops, ino, dentry_ptr);
  140. } else {
  141. /*
  142. * if dentry is corrupted, the %pd handler may still
  143. * crash, but it's unlikely that we reach here with a
  144. * corrupted struct page
  145. */
  146. pr_warn("aops:%ps ino:%lx dentry name:\"%pd\"\n",
  147. a_ops, ino, &dentry);
  148. }
  149. }
  150. out_mapping:
  151. BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS + 1);
  152. pr_warn("%sflags: %#lx(%pGp)%s\n", type, head->flags, &head->flags,
  153. page_cma ? " CMA" : "");
  154. hex_only:
  155. print_hex_dump(KERN_WARNING, "raw: ", DUMP_PREFIX_NONE, 32,
  156. sizeof(unsigned long), page,
  157. sizeof(struct page), false);
  158. if (head != page)
  159. print_hex_dump(KERN_WARNING, "head: ", DUMP_PREFIX_NONE, 32,
  160. sizeof(unsigned long), head,
  161. sizeof(struct page), false);
  162. if (reason)
  163. pr_warn("page dumped because: %s\n", reason);
  164. #ifdef CONFIG_MEMCG
  165. if (!page_poisoned && page->mem_cgroup)
  166. pr_warn("page->mem_cgroup:%px\n", page->mem_cgroup);
  167. #endif
  168. }
  169. void dump_page(struct page *page, const char *reason)
  170. {
  171. __dump_page(page, reason);
  172. dump_page_owner(page);
  173. dump_page_pinner(page);
  174. }
  175. EXPORT_SYMBOL(dump_page);
  176. #ifdef CONFIG_DEBUG_VM
  177. void dump_vma(const struct vm_area_struct *vma)
  178. {
  179. pr_emerg("vma %px start %px end %px\n"
  180. "next %px prev %px mm %px\n"
  181. "prot %lx anon_vma %px vm_ops %px\n"
  182. "pgoff %lx file %px private_data %px\n"
  183. "flags: %#lx(%pGv)\n",
  184. vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_next,
  185. vma->vm_prev, vma->vm_mm,
  186. (unsigned long)pgprot_val(vma->vm_page_prot),
  187. vma->anon_vma, vma->vm_ops, vma->vm_pgoff,
  188. vma->vm_file, vma->vm_private_data,
  189. vma->vm_flags, &vma->vm_flags);
  190. }
  191. EXPORT_SYMBOL(dump_vma);
  192. void dump_mm(const struct mm_struct *mm)
  193. {
  194. pr_emerg("mm %px mmap %px seqnum %llu task_size %lu\n"
  195. #ifdef CONFIG_MMU
  196. "get_unmapped_area %px\n"
  197. #endif
  198. "mmap_base %lu mmap_legacy_base %lu highest_vm_end %lu\n"
  199. "pgd %px mm_users %d mm_count %d pgtables_bytes %lu map_count %d\n"
  200. "hiwater_rss %lx hiwater_vm %lx total_vm %lx locked_vm %lx\n"
  201. "pinned_vm %llx data_vm %lx exec_vm %lx stack_vm %lx\n"
  202. "start_code %lx end_code %lx start_data %lx end_data %lx\n"
  203. "start_brk %lx brk %lx start_stack %lx\n"
  204. "arg_start %lx arg_end %lx env_start %lx env_end %lx\n"
  205. "binfmt %px flags %lx core_state %px\n"
  206. #ifdef CONFIG_AIO
  207. "ioctx_table %px\n"
  208. #endif
  209. #ifdef CONFIG_MEMCG
  210. "owner %px "
  211. #endif
  212. "exe_file %px\n"
  213. #ifdef CONFIG_MMU_NOTIFIER
  214. "notifier_subscriptions %px\n"
  215. #endif
  216. #ifdef CONFIG_NUMA_BALANCING
  217. "numa_next_scan %lu numa_scan_offset %lu numa_scan_seq %d\n"
  218. #endif
  219. "tlb_flush_pending %d\n"
  220. "def_flags: %#lx(%pGv)\n",
  221. mm, mm->mmap, (long long) mm->vmacache_seqnum, mm->task_size,
  222. #ifdef CONFIG_MMU
  223. mm->get_unmapped_area,
  224. #endif
  225. mm->mmap_base, mm->mmap_legacy_base, mm->highest_vm_end,
  226. mm->pgd, atomic_read(&mm->mm_users),
  227. atomic_read(&mm->mm_count),
  228. mm_pgtables_bytes(mm),
  229. mm->map_count,
  230. mm->hiwater_rss, mm->hiwater_vm, mm->total_vm, mm->locked_vm,
  231. (u64)atomic64_read(&mm->pinned_vm),
  232. mm->data_vm, mm->exec_vm, mm->stack_vm,
  233. mm->start_code, mm->end_code, mm->start_data, mm->end_data,
  234. mm->start_brk, mm->brk, mm->start_stack,
  235. mm->arg_start, mm->arg_end, mm->env_start, mm->env_end,
  236. mm->binfmt, mm->flags, mm->core_state,
  237. #ifdef CONFIG_AIO
  238. mm->ioctx_table,
  239. #endif
  240. #ifdef CONFIG_MEMCG
  241. mm->owner,
  242. #endif
  243. mm->exe_file,
  244. #ifdef CONFIG_MMU_NOTIFIER
  245. mm->notifier_subscriptions,
  246. #endif
  247. #ifdef CONFIG_NUMA_BALANCING
  248. mm->numa_next_scan, mm->numa_scan_offset, mm->numa_scan_seq,
  249. #endif
  250. atomic_read(&mm->tlb_flush_pending),
  251. mm->def_flags, &mm->def_flags
  252. );
  253. }
  254. static bool page_init_poisoning __read_mostly = true;
  255. static int __init setup_vm_debug(char *str)
  256. {
  257. bool __page_init_poisoning = true;
  258. /*
  259. * Calling vm_debug with no arguments is equivalent to requesting
  260. * to enable all debugging options we can control.
  261. */
  262. if (*str++ != '=' || !*str)
  263. goto out;
  264. __page_init_poisoning = false;
  265. if (*str == '-')
  266. goto out;
  267. while (*str) {
  268. switch (tolower(*str)) {
  269. case'p':
  270. __page_init_poisoning = true;
  271. break;
  272. default:
  273. pr_err("vm_debug option '%c' unknown. skipped\n",
  274. *str);
  275. }
  276. str++;
  277. }
  278. out:
  279. if (page_init_poisoning && !__page_init_poisoning)
  280. pr_warn("Page struct poisoning disabled by kernel command line option 'vm_debug'\n");
  281. page_init_poisoning = __page_init_poisoning;
  282. return 1;
  283. }
  284. __setup("vm_debug", setup_vm_debug);
  285. void page_init_poison(struct page *page, size_t size)
  286. {
  287. if (page_init_poisoning)
  288. memset(page, PAGE_POISON_PATTERN, size);
  289. }
  290. EXPORT_SYMBOL_GPL(page_init_poison);
  291. #endif /* CONFIG_DEBUG_VM */