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