~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/mm/allocation-profiling.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 .. SPDX-License-Identifier: GPL-2.0
  2 
  3 ===========================
  4 MEMORY ALLOCATION PROFILING
  5 ===========================
  6 
  7 Low overhead (suitable for production) accounting of all memory allocations,
  8 tracked by file and line number.
  9 
 10 Usage:
 11 kconfig options:
 12 - CONFIG_MEM_ALLOC_PROFILING
 13 
 14 - CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
 15 
 16 - CONFIG_MEM_ALLOC_PROFILING_DEBUG
 17   adds warnings for allocations that weren't accounted because of a
 18   missing annotation
 19 
 20 Boot parameter:
 21   sysctl.vm.mem_profiling=0|1|never
 22 
 23   When set to "never", memory allocation profiling overhead is minimized and it
 24   cannot be enabled at runtime (sysctl becomes read-only).
 25   When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=y, default value is "1".
 26   When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=n, default value is "never".
 27 
 28 sysctl:
 29   /proc/sys/vm/mem_profiling
 30 
 31 Runtime info:
 32   /proc/allocinfo
 33 
 34 Example output::
 35 
 36   root@moria-kvm:~# sort -g /proc/allocinfo|tail|numfmt --to=iec
 37         2.8M    22648 fs/kernfs/dir.c:615 func:__kernfs_new_node
 38         3.8M      953 mm/memory.c:4214 func:alloc_anon_folio
 39         4.0M     1010 drivers/staging/ctagmod/ctagmod.c:20 [ctagmod] func:ctagmod_start
 40         4.1M        4 net/netfilter/nf_conntrack_core.c:2567 func:nf_ct_alloc_hashtable
 41         6.0M     1532 mm/filemap.c:1919 func:__filemap_get_folio
 42         8.8M     2785 kernel/fork.c:307 func:alloc_thread_stack_node
 43          13M      234 block/blk-mq.c:3421 func:blk_mq_alloc_rqs
 44          14M     3520 mm/mm_init.c:2530 func:alloc_large_system_hash
 45          15M     3656 mm/readahead.c:247 func:page_cache_ra_unbounded
 46          55M     4887 mm/slub.c:2259 func:alloc_slab_page
 47         122M    31168 mm/page_ext.c:270 func:alloc_page_ext
 48 
 49 Theory of operation
 50 ===================
 51 
 52 Memory allocation profiling builds off of code tagging, which is a library for
 53 declaring static structs (that typically describe a file and line number in
 54 some way, hence code tagging) and then finding and operating on them at runtime,
 55 - i.e. iterating over them to print them in debugfs/procfs.
 56 
 57 To add accounting for an allocation call, we replace it with a macro
 58 invocation, alloc_hooks(), that
 59 - declares a code tag
 60 - stashes a pointer to it in task_struct
 61 - calls the real allocation function
 62 - and finally, restores the task_struct alloc tag pointer to its previous value.
 63 
 64 This allows for alloc_hooks() calls to be nested, with the most recent one
 65 taking effect. This is important for allocations internal to the mm/ code that
 66 do not properly belong to the outer allocation context and should be counted
 67 separately: for example, slab object extension vectors, or when the slab
 68 allocates pages from the page allocator.
 69 
 70 Thus, proper usage requires determining which function in an allocation call
 71 stack should be tagged. There are many helper functions that essentially wrap
 72 e.g. kmalloc() and do a little more work, then are called in multiple places;
 73 we'll generally want the accounting to happen in the callers of these helpers,
 74 not in the helpers themselves.
 75 
 76 To fix up a given helper, for example foo(), do the following:
 77 - switch its allocation call to the _noprof() version, e.g. kmalloc_noprof()
 78 
 79 - rename it to foo_noprof()
 80 
 81 - define a macro version of foo() like so:
 82 
 83   #define foo(...) alloc_hooks(foo_noprof(__VA_ARGS__))
 84 
 85 It's also possible to stash a pointer to an alloc tag in your own data structures.
 86 
 87 Do this when you're implementing a generic data structure that does allocations
 88 "on behalf of" some other code - for example, the rhashtable code. This way,
 89 instead of seeing a large line in /proc/allocinfo for rhashtable.c, we can
 90 break it out by rhashtable type.
 91 
 92 To do so:
 93 - Hook your data structure's init function, like any other allocation function.
 94 
 95 - Within your init function, use the convenience macro alloc_tag_record() to
 96   record alloc tag in your data structure.
 97 
 98 - Then, use the following form for your allocations:
 99   alloc_hooks_tag(ht->your_saved_tag, kmalloc_noprof(...))

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php