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*ba6fe537SOscar SalvadorIt can also be used to show all the stacks and their outstanding 28*ba6fe537SOscar Salvadorallocations, which gives us a quick overview of where the memory is going 29*ba6fe537SOscar Salvadorwithout the need to screen through all the pages and match the allocation 30*ba6fe537SOscar Salvadorand free operation. 31*ba6fe537SOscar 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 76*ba6fe537SOscar Salvador cat /sys/kernel/debug/page_owner_stacks/show_stacks > stacks.txt 77*ba6fe537SOscar Salvador cat stacks.txt 78*ba6fe537SOscar Salvador prep_new_page+0xa9/0x120 79*ba6fe537SOscar Salvador get_page_from_freelist+0x7e6/0x2140 80*ba6fe537SOscar Salvador __alloc_pages+0x18a/0x370 81*ba6fe537SOscar Salvador new_slab+0xc8/0x580 82*ba6fe537SOscar Salvador ___slab_alloc+0x1f2/0xaf0 83*ba6fe537SOscar Salvador __slab_alloc.isra.86+0x22/0x40 84*ba6fe537SOscar Salvador kmem_cache_alloc+0x31b/0x350 85*ba6fe537SOscar Salvador __khugepaged_enter+0x39/0x100 86*ba6fe537SOscar Salvador dup_mmap+0x1c7/0x5ce 87*ba6fe537SOscar Salvador copy_process+0x1afe/0x1c90 88*ba6fe537SOscar Salvador kernel_clone+0x9a/0x3c0 89*ba6fe537SOscar Salvador __do_sys_clone+0x66/0x90 90*ba6fe537SOscar Salvador do_syscall_64+0x7f/0x160 91*ba6fe537SOscar Salvador entry_SYSCALL_64_after_hwframe+0x6c/0x74 92*ba6fe537SOscar Salvador stack_count: 234 93*ba6fe537SOscar Salvador ... 94*ba6fe537SOscar Salvador ... 95*ba6fe537SOscar Salvador echo 7000 > /sys/kernel/debug/page_owner_stacks/count_threshold 96*ba6fe537SOscar Salvador cat /sys/kernel/debug/page_owner_stacks/show_stacks> stacks_7000.txt 97*ba6fe537SOscar Salvador cat stacks_7000.txt 98*ba6fe537SOscar Salvador prep_new_page+0xa9/0x120 99*ba6fe537SOscar Salvador get_page_from_freelist+0x7e6/0x2140 100*ba6fe537SOscar Salvador __alloc_pages+0x18a/0x370 101*ba6fe537SOscar Salvador alloc_pages_mpol+0xdf/0x1e0 102*ba6fe537SOscar Salvador folio_alloc+0x14/0x50 103*ba6fe537SOscar Salvador filemap_alloc_folio+0xb0/0x100 104*ba6fe537SOscar Salvador page_cache_ra_unbounded+0x97/0x180 105*ba6fe537SOscar Salvador filemap_fault+0x4b4/0x1200 106*ba6fe537SOscar Salvador __do_fault+0x2d/0x110 107*ba6fe537SOscar Salvador do_pte_missing+0x4b0/0xa30 108*ba6fe537SOscar Salvador __handle_mm_fault+0x7fa/0xb70 109*ba6fe537SOscar Salvador handle_mm_fault+0x125/0x300 110*ba6fe537SOscar Salvador do_user_addr_fault+0x3c9/0x840 111*ba6fe537SOscar Salvador exc_page_fault+0x68/0x150 112*ba6fe537SOscar Salvador asm_exc_page_fault+0x22/0x30 113*ba6fe537SOscar Salvador stack_count: 8248 114*ba6fe537SOscar Salvador ... 115*ba6fe537SOscar Salvador 116ee65728eSMike Rapoport cat /sys/kernel/debug/page_owner > page_owner_full.txt 117ee65728eSMike Rapoport ./page_owner_sort page_owner_full.txt sorted_page_owner.txt 118ee65728eSMike Rapoport 119ee65728eSMike Rapoport The general output of ``page_owner_full.txt`` is as follows:: 120ee65728eSMike Rapoport 121ee65728eSMike Rapoport Page allocated via order XXX, ... 122ee65728eSMike Rapoport PFN XXX ... 123ee65728eSMike Rapoport // Detailed stack 124ee65728eSMike Rapoport 125ee65728eSMike Rapoport Page allocated via order XXX, ... 126ee65728eSMike Rapoport PFN XXX ... 127ee65728eSMike Rapoport // Detailed stack 1288f0efa81SKassey Li By default, it will do full pfn dump, to start with a given pfn, 1298f0efa81SKassey Li page_owner supports fseek. 1308f0efa81SKassey Li 1318f0efa81SKassey Li FILE *fp = fopen("/sys/kernel/debug/page_owner", "r"); 1328f0efa81SKassey Li fseek(fp, pfn_start, SEEK_SET); 133ee65728eSMike Rapoport 134ee65728eSMike Rapoport The ``page_owner_sort`` tool ignores ``PFN`` rows, puts the remaining rows 135ee65728eSMike Rapoport in buf, uses regexp to extract the page order value, counts the times 136ee65728eSMike Rapoport and pages of buf, and finally sorts them according to the parameter(s). 137ee65728eSMike Rapoport 138ee65728eSMike Rapoport See the result about who allocated each page 139ee65728eSMike Rapoport in the ``sorted_page_owner.txt``. General output:: 140ee65728eSMike Rapoport 141ee65728eSMike Rapoport XXX times, XXX pages: 142ee65728eSMike Rapoport Page allocated via order XXX, ... 143ee65728eSMike Rapoport // Detailed stack 144ee65728eSMike Rapoport 145ee65728eSMike Rapoport By default, ``page_owner_sort`` is sorted according to the times of buf. 146ee65728eSMike Rapoport If you want to sort by the page nums of buf, use the ``-m`` parameter. 147ee65728eSMike Rapoport The detailed parameters are: 148ee65728eSMike Rapoport 149ee65728eSMike Rapoport fundamental function:: 150ee65728eSMike Rapoport 151ee65728eSMike Rapoport Sort: 152ee65728eSMike Rapoport -a Sort by memory allocation time. 153ee65728eSMike Rapoport -m Sort by total memory. 154ee65728eSMike Rapoport -p Sort by pid. 155ee65728eSMike Rapoport -P Sort by tgid. 156ee65728eSMike Rapoport -n Sort by task command name. 157ee65728eSMike Rapoport -r Sort by memory release time. 158ee65728eSMike Rapoport -s Sort by stack trace. 159ee65728eSMike Rapoport -t Sort by times (default). 160ee65728eSMike Rapoport --sort <order> Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]]. 161ee65728eSMike Rapoport Choose a key from the **STANDARD FORMAT SPECIFIERS** section. The "+" is 162ee65728eSMike Rapoport optional since default direction is increasing numerical or lexicographic 163ee65728eSMike Rapoport order. Mixed use of abbreviated and complete-form of keys is allowed. 164ee65728eSMike Rapoport 165ee65728eSMike Rapoport Examples: 166ee65728eSMike Rapoport ./page_owner_sort <input> <output> --sort=n,+pid,-tgid 167ee65728eSMike Rapoport ./page_owner_sort <input> <output> --sort=at 168ee65728eSMike Rapoport 169ee65728eSMike Rapoport additional function:: 170ee65728eSMike Rapoport 171ee65728eSMike Rapoport Cull: 172ee65728eSMike Rapoport --cull <rules> 173ee65728eSMike Rapoport Specify culling rules.Culling syntax is key[,key[,...]].Choose a 174ee65728eSMike Rapoport multi-letter key from the **STANDARD FORMAT SPECIFIERS** section. 175ee65728eSMike Rapoport 176ee65728eSMike Rapoport <rules> is a single argument in the form of a comma-separated list, 177ee65728eSMike Rapoport which offers a way to specify individual culling rules. The recognized 178ee65728eSMike Rapoport keywords are described in the **STANDARD FORMAT SPECIFIERS** section below. 179ee65728eSMike Rapoport <rules> can be specified by the sequence of keys k1,k2, ..., as described in 180ee65728eSMike Rapoport the STANDARD SORT KEYS section below. Mixed use of abbreviated and 181ee65728eSMike Rapoport complete-form of keys is allowed. 182ee65728eSMike Rapoport 183ee65728eSMike Rapoport Examples: 184ee65728eSMike Rapoport ./page_owner_sort <input> <output> --cull=stacktrace 185ee65728eSMike Rapoport ./page_owner_sort <input> <output> --cull=st,pid,name 186ee65728eSMike Rapoport ./page_owner_sort <input> <output> --cull=n,f 187ee65728eSMike Rapoport 188ee65728eSMike Rapoport Filter: 189ee65728eSMike Rapoport -f Filter out the information of blocks whose memory has been released. 190ee65728eSMike Rapoport 191ee65728eSMike Rapoport Select: 192ee65728eSMike Rapoport --pid <pidlist> Select by pid. This selects the blocks whose process ID 193ee65728eSMike Rapoport numbers appear in <pidlist>. 194ee65728eSMike Rapoport --tgid <tgidlist> Select by tgid. This selects the blocks whose thread 195ee65728eSMike Rapoport group ID numbers appear in <tgidlist>. 196ee65728eSMike Rapoport --name <cmdlist> Select by task command name. This selects the blocks whose 197ee65728eSMike Rapoport task command name appear in <cmdlist>. 198ee65728eSMike Rapoport 199ee65728eSMike Rapoport <pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list, 200ee65728eSMike Rapoport which offers a way to specify individual selecting rules. 201ee65728eSMike Rapoport 202ee65728eSMike Rapoport 203ee65728eSMike Rapoport Examples: 204ee65728eSMike Rapoport ./page_owner_sort <input> <output> --pid=1 205ee65728eSMike Rapoport ./page_owner_sort <input> <output> --tgid=1,2,3 206ee65728eSMike Rapoport ./page_owner_sort <input> <output> --name name1,name2 207ee65728eSMike Rapoport 208ee65728eSMike RapoportSTANDARD FORMAT SPECIFIERS 209ee65728eSMike Rapoport========================== 210ee65728eSMike Rapoport:: 211ee65728eSMike Rapoport 212ee65728eSMike Rapoport For --sort option: 213ee65728eSMike Rapoport 214ee65728eSMike Rapoport KEY LONG DESCRIPTION 215ee65728eSMike Rapoport p pid process ID 216ee65728eSMike Rapoport tg tgid thread group ID 217ee65728eSMike Rapoport n name task command name 218ee65728eSMike Rapoport st stacktrace stack trace of the page allocation 219ee65728eSMike Rapoport T txt full text of block 220ee65728eSMike Rapoport ft free_ts timestamp of the page when it was released 221ee65728eSMike Rapoport at alloc_ts timestamp of the page when it was allocated 222ee65728eSMike Rapoport ator allocator memory allocator for pages 223ee65728eSMike Rapoport 224e7951a3eSChen Xiao For --cull option: 225ee65728eSMike Rapoport 226ee65728eSMike Rapoport KEY LONG DESCRIPTION 227ee65728eSMike Rapoport p pid process ID 228ee65728eSMike Rapoport tg tgid thread group ID 229ee65728eSMike Rapoport n name task command name 230ee65728eSMike Rapoport f free whether the page has been released or not 231ee65728eSMike Rapoport st stacktrace stack trace of the page allocation 232ee65728eSMike Rapoport ator allocator memory allocator for pages 233