debug.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/ctype.h>
  16. #include "internal.h"
  17. #include <trace/events/migrate.h>
  18. /*
  19. * Define EM() and EMe() so that MIGRATE_REASON from trace/events/migrate.h can
  20. * be used to populate migrate_reason_names[].
  21. */
  22. #undef EM
  23. #undef EMe
  24. #define EM(a, b) b,
  25. #define EMe(a, b) b
  26. const char *migrate_reason_names[MR_TYPES] = {
  27. MIGRATE_REASON
  28. };
  29. const struct trace_print_flags pageflag_names[] = {
  30. __def_pageflag_names,
  31. {0, NULL}
  32. };
  33. const struct trace_print_flags gfpflag_names[] = {
  34. __def_gfpflag_names,
  35. {0, NULL}
  36. };
  37. const struct trace_print_flags vmaflag_names[] = {
  38. __def_vmaflag_names,
  39. {0, NULL}
  40. };
  41. static void __dump_page(struct page *page)
  42. {
  43. struct folio *folio = page_folio(page);
  44. struct page *head = &folio->page;
  45. struct address_space *mapping;
  46. bool compound = PageCompound(page);
  47. /*
  48. * Accessing the pageblock without the zone lock. It could change to
  49. * "isolate" again in the meantime, but since we are just dumping the
  50. * state for debugging, it should be fine to accept a bit of
  51. * inaccuracy here due to racing.
  52. */
  53. bool page_cma = is_migrate_cma_page(page);
  54. int mapcount;
  55. char *type = "";
  56. if (page < head || (page >= head + MAX_ORDER_NR_PAGES)) {
  57. /*
  58. * Corrupt page, so we cannot call page_mapping. Instead, do a
  59. * safe subset of the steps that page_mapping() does. Caution:
  60. * this will be misleading for tail pages, PageSwapCache pages,
  61. * and potentially other situations. (See the page_mapping()
  62. * implementation for what's missing here.)
  63. */
  64. unsigned long tmp = (unsigned long)page->mapping;
  65. if (tmp & PAGE_MAPPING_ANON)
  66. mapping = NULL;
  67. else
  68. mapping = (void *)(tmp & ~PAGE_MAPPING_FLAGS);
  69. head = page;
  70. folio = (struct folio *)page;
  71. compound = false;
  72. } else {
  73. mapping = page_mapping(page);
  74. }
  75. /*
  76. * Avoid VM_BUG_ON() in page_mapcount().
  77. * page->_mapcount space in struct page is used by sl[aou]b pages to
  78. * encode own info.
  79. */
  80. mapcount = PageSlab(head) ? 0 : page_mapcount(page);
  81. pr_warn("page:%p refcount:%d mapcount:%d mapping:%p index:%#lx pfn:%#lx\n",
  82. page, page_ref_count(head), mapcount, mapping,
  83. page_to_pgoff(page), page_to_pfn(page));
  84. if (compound) {
  85. pr_warn("head:%p order:%u compound_mapcount:%d compound_pincount:%d\n",
  86. head, compound_order(head),
  87. folio_entire_mapcount(folio),
  88. head_compound_pincount(head));
  89. }
  90. #ifdef CONFIG_MEMCG
  91. if (head->memcg_data)
  92. pr_warn("memcg:%lx\n", head->memcg_data);
  93. #endif
  94. if (PageKsm(page))
  95. type = "ksm ";
  96. else if (PageAnon(page))
  97. type = "anon ";
  98. else if (mapping)
  99. dump_mapping(mapping);
  100. BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS + 1);
  101. pr_warn("%sflags: %pGp%s\n", type, &head->flags,
  102. page_cma ? " CMA" : "");
  103. print_hex_dump(KERN_WARNING, "raw: ", DUMP_PREFIX_NONE, 32,
  104. sizeof(unsigned long), page,
  105. sizeof(struct page), false);
  106. if (head != page)
  107. print_hex_dump(KERN_WARNING, "head: ", DUMP_PREFIX_NONE, 32,
  108. sizeof(unsigned long), head,
  109. sizeof(struct page), false);
  110. }
  111. void dump_page(struct page *page, const char *reason)
  112. {
  113. if (PagePoisoned(page))
  114. pr_warn("page:%p is uninitialized and poisoned", page);
  115. else
  116. __dump_page(page);
  117. if (reason)
  118. pr_warn("page dumped because: %s\n", reason);
  119. dump_page_owner(page);
  120. }
  121. EXPORT_SYMBOL(dump_page);
  122. #ifdef CONFIG_DEBUG_VM
  123. void dump_vma(const struct vm_area_struct *vma)
  124. {
  125. pr_emerg("vma %px start %px end %px mm %px\n"
  126. "prot %lx anon_vma %px vm_ops %px\n"
  127. "pgoff %lx file %px private_data %px\n"
  128. "flags: %#lx(%pGv)\n",
  129. vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm,
  130. (unsigned long)pgprot_val(vma->vm_page_prot),
  131. vma->anon_vma, vma->vm_ops, vma->vm_pgoff,
  132. vma->vm_file, vma->vm_private_data,
  133. vma->vm_flags, &vma->vm_flags);
  134. }
  135. EXPORT_SYMBOL(dump_vma);
  136. void dump_mm(const struct mm_struct *mm)
  137. {
  138. pr_emerg("mm %px task_size %lu\n"
  139. #ifdef CONFIG_MMU
  140. "get_unmapped_area %px\n"
  141. #endif
  142. "mmap_base %lu mmap_legacy_base %lu\n"
  143. "pgd %px mm_users %d mm_count %d pgtables_bytes %lu map_count %d\n"
  144. "hiwater_rss %lx hiwater_vm %lx total_vm %lx locked_vm %lx\n"
  145. "pinned_vm %llx data_vm %lx exec_vm %lx stack_vm %lx\n"
  146. "start_code %lx end_code %lx start_data %lx end_data %lx\n"
  147. "start_brk %lx brk %lx start_stack %lx\n"
  148. "arg_start %lx arg_end %lx env_start %lx env_end %lx\n"
  149. "binfmt %px flags %lx\n"
  150. #ifdef CONFIG_AIO
  151. "ioctx_table %px\n"
  152. #endif
  153. #ifdef CONFIG_MEMCG
  154. "owner %px "
  155. #endif
  156. "exe_file %px\n"
  157. #ifdef CONFIG_MMU_NOTIFIER
  158. "notifier_subscriptions %px\n"
  159. #endif
  160. #ifdef CONFIG_NUMA_BALANCING
  161. "numa_next_scan %lu numa_scan_offset %lu numa_scan_seq %d\n"
  162. #endif
  163. "tlb_flush_pending %d\n"
  164. "def_flags: %#lx(%pGv)\n",
  165. mm, mm->task_size,
  166. #ifdef CONFIG_MMU
  167. mm->get_unmapped_area,
  168. #endif
  169. mm->mmap_base, mm->mmap_legacy_base,
  170. mm->pgd, atomic_read(&mm->mm_users),
  171. atomic_read(&mm->mm_count),
  172. mm_pgtables_bytes(mm),
  173. mm->map_count,
  174. mm->hiwater_rss, mm->hiwater_vm, mm->total_vm, mm->locked_vm,
  175. (u64)atomic64_read(&mm->pinned_vm),
  176. mm->data_vm, mm->exec_vm, mm->stack_vm,
  177. mm->start_code, mm->end_code, mm->start_data, mm->end_data,
  178. mm->start_brk, mm->brk, mm->start_stack,
  179. mm->arg_start, mm->arg_end, mm->env_start, mm->env_end,
  180. mm->binfmt, mm->flags,
  181. #ifdef CONFIG_AIO
  182. mm->ioctx_table,
  183. #endif
  184. #ifdef CONFIG_MEMCG
  185. mm->owner,
  186. #endif
  187. mm->exe_file,
  188. #ifdef CONFIG_MMU_NOTIFIER
  189. mm->notifier_subscriptions,
  190. #endif
  191. #ifdef CONFIG_NUMA_BALANCING
  192. mm->numa_next_scan, mm->numa_scan_offset, mm->numa_scan_seq,
  193. #endif
  194. atomic_read(&mm->tlb_flush_pending),
  195. mm->def_flags, &mm->def_flags
  196. );
  197. }
  198. EXPORT_SYMBOL(dump_mm);
  199. static bool page_init_poisoning __read_mostly = true;
  200. static int __init setup_vm_debug(char *str)
  201. {
  202. bool __page_init_poisoning = true;
  203. /*
  204. * Calling vm_debug with no arguments is equivalent to requesting
  205. * to enable all debugging options we can control.
  206. */
  207. if (*str++ != '=' || !*str)
  208. goto out;
  209. __page_init_poisoning = false;
  210. if (*str == '-')
  211. goto out;
  212. while (*str) {
  213. switch (tolower(*str)) {
  214. case'p':
  215. __page_init_poisoning = true;
  216. break;
  217. default:
  218. pr_err("vm_debug option '%c' unknown. skipped\n",
  219. *str);
  220. }
  221. str++;
  222. }
  223. out:
  224. if (page_init_poisoning && !__page_init_poisoning)
  225. pr_warn("Page struct poisoning disabled by kernel command line option 'vm_debug'\n");
  226. page_init_poisoning = __page_init_poisoning;
  227. return 1;
  228. }
  229. __setup("vm_debug", setup_vm_debug);
  230. void page_init_poison(struct page *page, size_t size)
  231. {
  232. if (page_init_poisoning)
  233. memset(page, PAGE_POISON_PATTERN, size);
  234. }
  235. #endif /* CONFIG_DEBUG_VM */