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