3 ==================================================
4 page owner: Tracking about who allocated each page
5 ==================================================
10 page owner is for the tracking about who allocated each page.
11 It can be used to debug memory leak or to find a memory hogger.
12 When allocation happens, information about allocation such as call stack
13 and order of pages is stored into certain storage for each page.
14 When we need to know about status of all pages, we can get and analyze
17 Although we already have tracepoint for tracing page allocation/free,
18 using it for analyzing who allocate each page is rather complex. We need
19 to enlarge the trace buffer for preventing overlapping until userspace
20 program launched. And, launched program continually dump out the trace
21 buffer for later analysis and it would change system behaviour with more
22 possibility rather than just keeping it in memory, so bad for debugging.
24 page owner can also be used for various purposes. For example, accurate
25 fragmentation statistics can be obtained through gfp flag information of
26 each page. It is already implemented and activated if page owner is
27 enabled. Other usages are more than welcome.
29 page owner is disabled by default. So, if you'd like to use it, you need
30 to add "page_owner=on" to your boot cmdline. If the kernel is built
31 with page owner and page owner is disabled in runtime due to not enabling
32 boot option, runtime overhead is marginal. If disabled in runtime, it
33 doesn't require memory to store owner information, so there is no runtime
34 memory overhead. And, page owner inserts just two unlikely branches into
35 the page allocator hotpath and if not enabled, then allocation is done
36 like as the kernel without page owner. These two unlikely branches should
37 not affect to allocation performance, especially if the static keys jump
38 label patching functionality is available. Following is the kernel's code
39 size change due to this facility.
41 - Without page owner::
43 text data bss dec hex filename
44 48392 2333 644 51369 c8a9 mm/page_alloc.o
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
53 Although, roughly, 8 KB code is added in total, page_alloc.o increase by
54 520 bytes and less than half of it is in hotpath. Building the kernel with
55 page owner and turning it on if needed would be great option to debug
56 kernel memory problem.
58 There is one notice that is caused by implementation detail. page owner
59 stores information into the memory from struct page extension. This memory
60 is initialized some time later than that page allocator starts in sparse
61 memory system, so, until initialization, many pages can be allocated and
62 they would have no owner information. To fix it up, these early allocated
63 pages are investigated and marked as allocated in initialization phase.
64 Although it doesn't mean that they have the right owner information,
65 at least, we can tell whether the page is allocated or not,
66 more accurately. On 2GB memory x86-64 VM box, 13343 early allocated pages
67 are catched and marked, although they are mostly allocated from struct
68 page extension feature. Anyway, after that, no page is left in
74 1) Build user-space helper::
79 2) Enable page owner: add "page_owner=on" to boot cmdline.
81 3) Do the job that you want to debug.
83 4) Analyze information from page owner::
85 cat /sys/kernel/debug/page_owner > page_owner_full.txt
86 ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
88 The general output of ``page_owner_full.txt`` is as follows::
90 Page allocated via order XXX, ...
94 Page allocated via order XXX, ...
97 By default, it will do full pfn dump, to start with a given pfn,
98 page_owner supports fseek.
100 FILE *fp = fopen("/sys/kernel/debug/page_owner", "r");
101 fseek(fp, pfn_start, SEEK_SET);
103 The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows
104 in buf, uses regexp to extract the page order value, counts the times
105 and pages of buf, and finally sorts them according to the parameter(s).
107 See the result about who allocated each page
108 in the ``sorted_page_owner.txt``. General output::
110 XXX times, XXX pages:
111 Page allocated via order XXX, ...
114 By default, ``page_owner_sort`` is sorted according to the times of buf.
115 If you want to sort by the page nums of buf, use the ``-m`` parameter.
116 The detailed parameters are:
118 fundamental function::
121 -a Sort by memory allocation time.
122 -m Sort by total memory.
125 -n Sort by task command name.
126 -r Sort by memory release time.
127 -s Sort by stack trace.
128 -t Sort by times (default).
129 --sort <order> Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]].
130 Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is
131 optional since default direction is increasing numerical or lexicographic
132 order. Mixed use of abbreviated and complete-form of keys is allowed.
135 ./page_owner_sort <input> <output> --sort=n,+pid,-tgid
136 ./page_owner_sort <input> <output> --sort=at
138 additional function::
142 Specify culling rules.Culling syntax is key[,key[,...]].Choose a
143 multi-letter key from the **STANDARD FORMAT SPECIFIERS** section.
145 <rules> is a single argument in the form of a comma-separated list,
146 which offers a way to specify individual culling rules. The recognized
147 keywords are described in the **STANDARD FORMAT SPECIFIERS** section below.
148 <rules> can be specified by the sequence of keys k1,k2, ..., as described in
149 the STANDARD SORT KEYS section below. Mixed use of abbreviated and
150 complete-form of keys is allowed.
153 ./page_owner_sort <input> <output> --cull=stacktrace
154 ./page_owner_sort <input> <output> --cull=st,pid,name
155 ./page_owner_sort <input> <output> --cull=n,f
158 -f Filter out the information of blocks whose memory has been released.
161 --pid <pidlist> Select by pid. This selects the blocks whose process ID
162 numbers appear in <pidlist>.
163 --tgid <tgidlist> Select by tgid. This selects the blocks whose thread
164 group ID numbers appear in <tgidlist>.
165 --name <cmdlist> Select by task command name. This selects the blocks whose
166 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.
173 ./page_owner_sort <input> <output> --pid=1
174 ./page_owner_sort <input> <output> --tgid=1,2,3
175 ./page_owner_sort <input> <output> --name name1,name2
177 STANDARD FORMAT SPECIFIERS
178 ==========================
185 tg tgid thread group ID
186 n name task command name
187 st stacktrace stack trace of the page allocation
188 T txt full text of block
189 ft free_ts timestamp of the page when it was released
190 at alloc_ts timestamp of the page when it was allocated
191 ator allocator memory allocator for pages
197 tg tgid thread group ID
198 n name task command name
199 f free whether the page has been released or not
200 st stacktrace stack trace of the page allocation
201 ator allocator memory allocator for pages