page_owner.rst 8.6 KB

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