page_owner.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ==================================================
  2. page owner: Tracking about who allocated each page
  3. ==================================================
  4. Introduction
  5. ============
  6. page owner is for the tracking about who allocated each page.
  7. It can be used to debug memory leak or to find a memory hogger.
  8. When allocation happens, information about allocation such as call stack
  9. and order of pages is stored into certain storage for each page.
  10. When we need to know about status of all pages, we can get and analyze
  11. this information.
  12. Although we already have tracepoint for tracing page allocation/free,
  13. using it for analyzing who allocate each page is rather complex. We need
  14. to enlarge the trace buffer for preventing overlapping until userspace
  15. program launched. And, launched program continually dump out the trace
  16. buffer for later analysis and it would change system behaviour with more
  17. possibility rather than just keeping it in memory, so bad for debugging.
  18. page owner can also be used for various purposes. For example, accurate
  19. fragmentation statistics can be obtained through gfp flag information of
  20. each page. It is already implemented and activated if page owner is
  21. enabled. Other usages are more than welcome.
  22. It can also be used to show all the stacks and their current number of
  23. allocated base pages, which gives us a quick overview of where the memory
  24. is going without the need to screen through all the pages and match the
  25. allocation and free operation. It's also possible to show only a numeric
  26. identifier of all the stacks (without stack traces) and their number of
  27. allocated base pages (faster to read and parse, eg, for monitoring) that
  28. can be matched with stacks later (show_handles and show_stacks_handles).
  29. page owner is disabled by default. So, if you'd like to use it, you need
  30. to add "page_owner=on" to your boot cmdline. If the kernel is built
  31. with page owner and page owner is disabled in runtime due to not enabling
  32. boot option, runtime overhead is marginal. If disabled in runtime, it
  33. doesn't require memory to store owner information, so there is no runtime
  34. memory overhead. And, page owner inserts just two unlikely branches into
  35. the page allocator hotpath and if not enabled, then allocation is done
  36. like as the kernel without page owner. These two unlikely branches should
  37. not affect to allocation performance, especially if the static keys jump
  38. label patching functionality is available. Following is the kernel's code
  39. size change due to this facility.
  40. Although enabling page owner increases kernel size by several kilobytes,
  41. most of this code is outside page allocator and its hot path. Building
  42. the kernel with page owner and turning it on if needed would be great
  43. option to debug kernel memory problem.
  44. There is one notice that is caused by implementation detail. page owner
  45. stores information into the memory from struct page extension. This memory
  46. is initialized some time later than that page allocator starts in sparse
  47. memory system, so, until initialization, many pages can be allocated and
  48. they would have no owner information. To fix it up, these early allocated
  49. pages are investigated and marked as allocated in initialization phase.
  50. Although it doesn't mean that they have the right owner information,
  51. at least, we can tell whether the page is allocated or not,
  52. more accurately. On 2GB memory x86-64 VM box, 13343 early allocated pages
  53. are caught and marked, although they are mostly allocated from struct
  54. page extension feature. Anyway, after that, no page is left in
  55. un-tracking state.
  56. Usage
  57. =====
  58. 1) Build user-space helper::
  59. cd tools/mm
  60. make page_owner_sort
  61. 2) Enable page owner: add "page_owner=on" to boot cmdline.
  62. 3) Do the job that you want to debug.
  63. 4) Analyze information from page owner::
  64. cat /sys/kernel/debug/page_owner_stacks/show_stacks > stacks.txt
  65. cat stacks.txt
  66. post_alloc_hook+0x177/0x1a0
  67. get_page_from_freelist+0xd01/0xd80
  68. __alloc_pages+0x39e/0x7e0
  69. allocate_slab+0xbc/0x3f0
  70. ___slab_alloc+0x528/0x8a0
  71. kmem_cache_alloc+0x224/0x3b0
  72. sk_prot_alloc+0x58/0x1a0
  73. sk_alloc+0x32/0x4f0
  74. inet_create+0x427/0xb50
  75. __sock_create+0x2e4/0x650
  76. inet_ctl_sock_create+0x30/0x180
  77. igmp_net_init+0xc1/0x130
  78. ops_init+0x167/0x410
  79. setup_net+0x304/0xa60
  80. copy_net_ns+0x29b/0x4a0
  81. create_new_namespaces+0x4a1/0x820
  82. nr_base_pages: 16
  83. ...
  84. ...
  85. echo 7000 > /sys/kernel/debug/page_owner_stacks/count_threshold
  86. cat /sys/kernel/debug/page_owner_stacks/show_stacks> stacks_7000.txt
  87. cat stacks_7000.txt
  88. post_alloc_hook+0x177/0x1a0
  89. get_page_from_freelist+0xd01/0xd80
  90. __alloc_pages+0x39e/0x7e0
  91. alloc_pages_mpol+0x22e/0x490
  92. folio_alloc+0xd5/0x110
  93. filemap_alloc_folio+0x78/0x230
  94. page_cache_ra_order+0x287/0x6f0
  95. filemap_get_pages+0x517/0x1160
  96. filemap_read+0x304/0x9f0
  97. xfs_file_buffered_read+0xe6/0x1d0 [xfs]
  98. xfs_file_read_iter+0x1f0/0x380 [xfs]
  99. __kernel_read+0x3b9/0x730
  100. kernel_read_file+0x309/0x4d0
  101. __do_sys_finit_module+0x381/0x730
  102. do_syscall_64+0x8d/0x150
  103. entry_SYSCALL_64_after_hwframe+0x62/0x6a
  104. nr_base_pages: 20824
  105. ...
  106. cat /sys/kernel/debug/page_owner_stacks/show_handles > handles_7000.txt
  107. cat handles_7000.txt
  108. handle: 42
  109. nr_base_pages: 20824
  110. ...
  111. cat /sys/kernel/debug/page_owner_stacks/show_stacks_handles > stacks_handles.txt
  112. cat stacks_handles.txt
  113. post_alloc_hook+0x177/0x1a0
  114. get_page_from_freelist+0xd01/0xd80
  115. __alloc_pages+0x39e/0x7e0
  116. alloc_pages_mpol+0x22e/0x490
  117. folio_alloc+0xd5/0x110
  118. filemap_alloc_folio+0x78/0x230
  119. page_cache_ra_order+0x287/0x6f0
  120. filemap_get_pages+0x517/0x1160
  121. filemap_read+0x304/0x9f0
  122. xfs_file_buffered_read+0xe6/0x1d0 [xfs]
  123. xfs_file_read_iter+0x1f0/0x380 [xfs]
  124. __kernel_read+0x3b9/0x730
  125. kernel_read_file+0x309/0x4d0
  126. __do_sys_finit_module+0x381/0x730
  127. do_syscall_64+0x8d/0x150
  128. entry_SYSCALL_64_after_hwframe+0x62/0x6a
  129. handle: 42
  130. ...
  131. cat /sys/kernel/debug/page_owner > page_owner_full.txt
  132. ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
  133. The general output of ``page_owner_full.txt`` is as follows::
  134. Page allocated via order XXX, ...
  135. PFN XXX ...
  136. // Detailed stack
  137. Page allocated via order XXX, ...
  138. PFN XXX ...
  139. // Detailed stack
  140. By default, it will do full pfn dump, to start with a given pfn,
  141. page_owner supports fseek.
  142. FILE *fp = fopen("/sys/kernel/debug/page_owner", "r");
  143. fseek(fp, pfn_start, SEEK_SET);
  144. The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows
  145. in buf, uses regexp to extract the page order value, counts the times
  146. and pages of buf, and finally sorts them according to the parameter(s).
  147. See the result about who allocated each page
  148. in the ``sorted_page_owner.txt``. General output::
  149. XXX times, XXX pages:
  150. Page allocated via order XXX, ...
  151. // Detailed stack
  152. By default, ``page_owner_sort`` is sorted according to the times of buf.
  153. If you want to sort by the page nums of buf, use the ``-m`` parameter.
  154. The detailed parameters are:
  155. fundamental function::
  156. Sort:
  157. -a Sort by memory allocation time.
  158. -m Sort by total memory.
  159. -p Sort by pid.
  160. -P Sort by tgid.
  161. -n Sort by task command name.
  162. -r Sort by memory release time.
  163. -s Sort by stack trace.
  164. -t Sort by times (default).
  165. --sort <order> Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]].
  166. Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is
  167. optional since default direction is increasing numerical or lexicographic
  168. order. Mixed use of abbreviated and complete-form of keys is allowed.
  169. Examples:
  170. ./page_owner_sort <input> <output> --sort=n,+pid,-tgid
  171. ./page_owner_sort <input> <output> --sort=at
  172. additional function::
  173. Cull:
  174. --cull <rules>
  175. Specify culling rules.Culling syntax is key[,key[,...]].Choose a
  176. multi-letter key from the **STANDARD FORMAT SPECIFIERS** section.
  177. <rules> is a single argument in the form of a comma-separated list,
  178. which offers a way to specify individual culling rules. The recognized
  179. keywords are described in the **STANDARD FORMAT SPECIFIERS** section below.
  180. <rules> can be specified by the sequence of keys k1,k2, ..., as described in
  181. the STANDARD SORT KEYS section below. Mixed use of abbreviated and
  182. complete-form of keys is allowed.
  183. Examples:
  184. ./page_owner_sort <input> <output> --cull=stacktrace
  185. ./page_owner_sort <input> <output> --cull=st,pid,name
  186. ./page_owner_sort <input> <output> --cull=n,f
  187. Filter:
  188. -f Filter out the information of blocks whose memory has been released.
  189. Select:
  190. --pid <pidlist> Select by pid. This selects the blocks whose process ID
  191. numbers appear in <pidlist>.
  192. --tgid <tgidlist> Select by tgid. This selects the blocks whose thread
  193. group ID numbers appear in <tgidlist>.
  194. --name <cmdlist> Select by task command name. This selects the blocks whose
  195. task command name appear in <cmdlist>.
  196. <pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list,
  197. which offers a way to specify individual selecting rules.
  198. Examples:
  199. ./page_owner_sort <input> <output> --pid=1
  200. ./page_owner_sort <input> <output> --tgid=1,2,3
  201. ./page_owner_sort <input> <output> --name name1,name2
  202. STANDARD FORMAT SPECIFIERS
  203. ==========================
  204. ::
  205. For --sort option:
  206. KEY LONG DESCRIPTION
  207. p pid process ID
  208. tg tgid thread group ID
  209. n name task command name
  210. st stacktrace stack trace of the page allocation
  211. T txt full text of block
  212. ft free_ts timestamp of the page when it was released
  213. at alloc_ts timestamp of the page when it was allocated
  214. ator allocator memory allocator for pages
  215. For --cull option:
  216. KEY LONG DESCRIPTION
  217. p pid process ID
  218. tg tgid thread group ID
  219. n name task command name
  220. f free whether the page has been released or not
  221. st stacktrace stack trace of the page allocation
  222. ator allocator memory allocator for pages