xref: /linux/Documentation/mm/page_owner.rst (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 outstanding
28allocations, which gives us a quick overview of where the memory is going
29without the need to screen through all the pages and match the allocation
30and free operation.
31
32page owner is disabled by default. So, if you'd like to use it, you need
33to add "page_owner=on" to your boot cmdline. If the kernel is built
34with page owner and page owner is disabled in runtime due to not enabling
35boot option, runtime overhead is marginal. If disabled in runtime, it
36doesn't require memory to store owner information, so there is no runtime
37memory overhead. And, page owner inserts just two unlikely branches into
38the page allocator hotpath and if not enabled, then allocation is done
39like as the kernel without page owner. These two unlikely branches should
40not affect to allocation performance, especially if the static keys jump
41label patching functionality is available. Following is the kernel's code
42size change due to this facility.
43
44Although enabling page owner increases kernel size by several kilobytes,
45most of this code is outside page allocator and its hot path. Building
46the kernel with page owner and turning it on if needed would be great
47option to debug kernel memory problem.
48
49There is one notice that is caused by implementation detail. page owner
50stores information into the memory from struct page extension. This memory
51is initialized some time later than that page allocator starts in sparse
52memory system, so, until initialization, many pages can be allocated and
53they would have no owner information. To fix it up, these early allocated
54pages are investigated and marked as allocated in initialization phase.
55Although it doesn't mean that they have the right owner information,
56at least, we can tell whether the page is allocated or not,
57more accurately. On 2GB memory x86-64 VM box, 13343 early allocated pages
58are caught and marked, although they are mostly allocated from struct
59page extension feature. Anyway, after that, no page is left in
60un-tracking state.
61
62Usage
63=====
64
651) Build user-space helper::
66
67	cd tools/mm
68	make page_owner_sort
69
702) Enable page owner: add "page_owner=on" to boot cmdline.
71
723) Do the job that you want to debug.
73
744) Analyze information from page owner::
75
76	cat /sys/kernel/debug/page_owner_stacks/show_stacks > stacks.txt
77	cat stacks.txt
78	 prep_new_page+0xa9/0x120
79	 get_page_from_freelist+0x7e6/0x2140
80	 __alloc_pages+0x18a/0x370
81	 new_slab+0xc8/0x580
82	 ___slab_alloc+0x1f2/0xaf0
83	 __slab_alloc.isra.86+0x22/0x40
84	 kmem_cache_alloc+0x31b/0x350
85	 __khugepaged_enter+0x39/0x100
86	 dup_mmap+0x1c7/0x5ce
87	 copy_process+0x1afe/0x1c90
88	 kernel_clone+0x9a/0x3c0
89	 __do_sys_clone+0x66/0x90
90	 do_syscall_64+0x7f/0x160
91	 entry_SYSCALL_64_after_hwframe+0x6c/0x74
92	stack_count: 234
93	...
94	...
95	echo 7000 > /sys/kernel/debug/page_owner_stacks/count_threshold
96	cat /sys/kernel/debug/page_owner_stacks/show_stacks> stacks_7000.txt
97	cat stacks_7000.txt
98	 prep_new_page+0xa9/0x120
99	 get_page_from_freelist+0x7e6/0x2140
100	 __alloc_pages+0x18a/0x370
101	 alloc_pages_mpol+0xdf/0x1e0
102	 folio_alloc+0x14/0x50
103	 filemap_alloc_folio+0xb0/0x100
104	 page_cache_ra_unbounded+0x97/0x180
105	 filemap_fault+0x4b4/0x1200
106	 __do_fault+0x2d/0x110
107	 do_pte_missing+0x4b0/0xa30
108	 __handle_mm_fault+0x7fa/0xb70
109	 handle_mm_fault+0x125/0x300
110	 do_user_addr_fault+0x3c9/0x840
111	 exc_page_fault+0x68/0x150
112	 asm_exc_page_fault+0x22/0x30
113	stack_count: 8248
114	...
115
116	cat /sys/kernel/debug/page_owner > page_owner_full.txt
117	./page_owner_sort page_owner_full.txt sorted_page_owner.txt
118
119   The general output of ``page_owner_full.txt`` is as follows::
120
121	Page allocated via order XXX, ...
122	PFN XXX ...
123	// Detailed stack
124
125	Page allocated via order XXX, ...
126	PFN XXX ...
127	// Detailed stack
128    By default, it will do full pfn dump, to start with a given pfn,
129    page_owner supports fseek.
130
131    FILE *fp = fopen("/sys/kernel/debug/page_owner", "r");
132    fseek(fp, pfn_start, SEEK_SET);
133
134   The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows
135   in buf, uses regexp to extract the page order value, counts the times
136   and pages of buf, and finally sorts them according to the parameter(s).
137
138   See the result about who allocated each page
139   in the ``sorted_page_owner.txt``. General output::
140
141	XXX times, XXX pages:
142	Page allocated via order XXX, ...
143	// Detailed stack
144
145   By default, ``page_owner_sort`` is sorted according to the times of buf.
146   If you want to sort by the page nums of buf, use the ``-m`` parameter.
147   The detailed parameters are:
148
149   fundamental function::
150
151	Sort:
152		-a		Sort by memory allocation time.
153		-m		Sort by total memory.
154		-p		Sort by pid.
155		-P		Sort by tgid.
156		-n		Sort by task command name.
157		-r		Sort by memory release time.
158		-s		Sort by stack trace.
159		-t		Sort by times (default).
160		--sort <order>	Specify sorting order.  Sorting syntax is [+|-]key[,[+|-]key[,...]].
161				Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is
162				optional since default direction is increasing numerical or lexicographic
163				order. Mixed use of abbreviated and complete-form of keys is allowed.
164
165		Examples:
166				./page_owner_sort <input> <output> --sort=n,+pid,-tgid
167				./page_owner_sort <input> <output> --sort=at
168
169   additional function::
170
171	Cull:
172		--cull <rules>
173				Specify culling rules.Culling syntax is key[,key[,...]].Choose a
174				multi-letter key from the **STANDARD FORMAT SPECIFIERS** section.
175
176		<rules> is a single argument in the form of a comma-separated list,
177		which offers a way to specify individual culling rules.  The recognized
178		keywords are described in the **STANDARD FORMAT SPECIFIERS** section below.
179		<rules> can be specified by the sequence of keys k1,k2, ..., as described in
180		the STANDARD SORT KEYS section below. Mixed use of abbreviated and
181		complete-form of keys is allowed.
182
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
188	Filter:
189		-f		Filter out the information of blocks whose memory has been released.
190
191	Select:
192		--pid <pidlist>		Select by pid. This selects the blocks whose process ID
193					numbers appear in <pidlist>.
194		--tgid <tgidlist>	Select by tgid. This selects the blocks whose thread
195					group ID numbers appear in <tgidlist>.
196		--name <cmdlist>	Select by task command name. This selects the blocks whose
197					task command name appear in <cmdlist>.
198
199		<pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list,
200		which offers a way to specify individual selecting rules.
201
202
203		Examples:
204				./page_owner_sort <input> <output> --pid=1
205				./page_owner_sort <input> <output> --tgid=1,2,3
206				./page_owner_sort <input> <output> --name name1,name2
207
208STANDARD FORMAT SPECIFIERS
209==========================
210::
211
212  For --sort option:
213
214	KEY		LONG		DESCRIPTION
215	p		pid		process ID
216	tg		tgid		thread group ID
217	n		name		task command name
218	st		stacktrace	stack trace of the page allocation
219	T		txt		full text of block
220	ft		free_ts		timestamp of the page when it was released
221	at		alloc_ts	timestamp of the page when it was allocated
222	ator		allocator	memory allocator for pages
223
224  For --cull option:
225
226	KEY		LONG		DESCRIPTION
227	p		pid		process ID
228	tg		tgid		thread group ID
229	n		name		task command name
230	f		free		whether the page has been released or not
231	st		stacktrace	stack trace of the page allocation
232	ator		allocator	memory allocator for pages
233