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