1.. _page_owner:
2
3==================================================
4page owner: Tracking about who allocated each page
5==================================================
6
7Introduction
8============
9
10page owner is for the tracking about who allocated each page.
11It can be used to debug memory leak or to find a memory hogger.
12When allocation happens, information about allocation such as call stack
13and order of pages is stored into certain storage for each page.
14When we need to know about status of all pages, we can get and analyze
15this information.
16
17Although we already have tracepoint for tracing page allocation/free,
18using it for analyzing who allocate each page is rather complex. We need
19to enlarge the trace buffer for preventing overlapping until userspace
20program launched. And, launched program continually dump out the trace
21buffer for later analysis and it would change system behaviour with more
22possibility rather than just keeping it in memory, so bad for debugging.
23
24page owner can also be used for various purposes. For example, accurate
25fragmentation statistics can be obtained through gfp flag information of
26each page. It is already implemented and activated if page owner is
27enabled. Other usages are more than welcome.
28
29page owner is disabled by default. So, if you'd like to use it, you need
30to add "page_owner=on" to your boot cmdline. If the kernel is built
31with page owner and page owner is disabled in runtime due to not enabling
32boot option, runtime overhead is marginal. If disabled in runtime, it
33doesn't require memory to store owner information, so there is no runtime
34memory overhead. And, page owner inserts just two unlikely branches into
35the page allocator hotpath and if not enabled, then allocation is done
36like as the kernel without page owner. These two unlikely branches should
37not affect to allocation performance, especially if the static keys jump
38label patching functionality is available. Following is the kernel's code
39size change due to this facility.
40
41- Without page owner::
42
43   text    data     bss     dec     hex filename
44   48392   2333     644   51369    c8a9 mm/page_alloc.o
45
46- With page owner::
47
48   text    data     bss     dec     hex filename
49   48800   2445     644   51889    cab1 mm/page_alloc.o
50   6662     108      29    6799    1a8f mm/page_owner.o
51   1025       8       8    1041     411 mm/page_ext.o
52
53Although, roughly, 8 KB code is added in total, page_alloc.o increase by
54520 bytes and less than half of it is in hotpath. Building the kernel with
55page owner and turning it on if needed would be great option to debug
56kernel memory problem.
57
58There is one notice that is caused by implementation detail. page owner
59stores information into the memory from struct page extension. This memory
60is initialized some time later than that page allocator starts in sparse
61memory system, so, until initialization, many pages can be allocated and
62they would have no owner information. To fix it up, these early allocated
63pages are investigated and marked as allocated in initialization phase.
64Although it doesn't mean that they have the right owner information,
65at least, we can tell whether the page is allocated or not,
66more accurately. On 2GB memory x86-64 VM box, 13343 early allocated pages
67are catched and marked, although they are mostly allocated from struct
68page extension feature. Anyway, after that, no page is left in
69un-tracking state.
70
71Usage
72=====
73
741) Build user-space helper::
75
76	cd tools/vm
77	make page_owner_sort
78
792) Enable page owner: add "page_owner=on" to boot cmdline.
80
813) Do the job that you want to debug.
82
834) Analyze information from page owner::
84
85	cat /sys/kernel/debug/page_owner > page_owner_full.txt
86	./page_owner_sort page_owner_full.txt sorted_page_owner.txt
87
88   The general output of ``page_owner_full.txt`` is as follows::
89
90	Page allocated via order XXX, ...
91	PFN XXX ...
92	// Detailed stack
93
94	Page allocated via order XXX, ...
95	PFN XXX ...
96	// Detailed stack
97
98   The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows
99   in buf, uses regexp to extract the page order value, counts the times
100   and pages of buf, and finally sorts them according to the parameter(s).
101
102   See the result about who allocated each page
103   in the ``sorted_page_owner.txt``. General output::
104
105	XXX times, XXX pages:
106	Page allocated via order XXX, ...
107	// Detailed stack
108
109   By default, ``page_owner_sort`` is sorted according to the times of buf.
110   If you want to sort by the page nums of buf, use the ``-m`` parameter.
111   The detailed parameters are:
112
113   fundamental function::
114
115	Sort:
116		-a		Sort by memory allocation time.
117		-m		Sort by total memory.
118		-p		Sort by pid.
119		-P		Sort by tgid.
120		-n		Sort by task command name.
121		-r		Sort by memory release time.
122		-s		Sort by stack trace.
123		-t		Sort by times (default).
124		--sort <order>	Specify sorting order.  Sorting syntax is [+|-]key[,[+|-]key[,...]].
125				Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is
126				optional since default direction is increasing numerical or lexicographic
127				order. Mixed use of abbreviated and complete-form of keys is allowed.
128
129		Examples:
130				./page_owner_sort <input> <output> --sort=n,+pid,-tgid
131				./page_owner_sort <input> <output> --sort=at
132
133   additional function::
134
135	Cull:
136		--cull <rules>
137				Specify culling rules.Culling syntax is key[,key[,...]].Choose a
138				multi-letter key from the **STANDARD FORMAT SPECIFIERS** section.
139
140		<rules> is a single argument in the form of a comma-separated list,
141		which offers a way to specify individual culling rules.  The recognized
142		keywords are described in the **STANDARD FORMAT SPECIFIERS** section below.
143		<rules> can be specified by the sequence of keys k1,k2, ..., as described in
144		the STANDARD SORT KEYS section below. Mixed use of abbreviated and
145		complete-form of keys is allowed.
146
147		Examples:
148				./page_owner_sort <input> <output> --cull=stacktrace
149				./page_owner_sort <input> <output> --cull=st,pid,name
150				./page_owner_sort <input> <output> --cull=n,f
151
152	Filter:
153		-f		Filter out the information of blocks whose memory has been released.
154
155	Select:
156		--pid <pidlist>		Select by pid. This selects the blocks whose process ID
157					numbers appear in <pidlist>.
158		--tgid <tgidlist>	Select by tgid. This selects the blocks whose thread
159					group ID numbers appear in <tgidlist>.
160		--name <cmdlist>	Select by task command name. This selects the blocks whose
161					task command name appear in <cmdlist>.
162
163		<pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list,
164		which offers a way to specify individual selecting rules.
165
166
167		Examples:
168				./page_owner_sort <input> <output> --pid=1
169				./page_owner_sort <input> <output> --tgid=1,2,3
170				./page_owner_sort <input> <output> --name name1,name2
171
172STANDARD FORMAT SPECIFIERS
173==========================
174::
175
176  For --sort option:
177
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
188  For --curl option:
189
190	KEY		LONG		DESCRIPTION
191	p		pid		process ID
192	tg		tgid		thread group ID
193	n		name		task command name
194	f		free		whether the page has been released or not
195	st		stacktrace	stack trace of the page allocation
196	ator		allocator	memory allocator for pages
197