allocation-profiling.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===========================
  3. MEMORY ALLOCATION PROFILING
  4. ===========================
  5. Low overhead (suitable for production) accounting of all memory allocations,
  6. tracked by file and line number.
  7. Usage:
  8. kconfig options:
  9. - CONFIG_MEM_ALLOC_PROFILING
  10. - CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
  11. - CONFIG_MEM_ALLOC_PROFILING_DEBUG
  12. adds warnings for allocations that weren't accounted because of a
  13. missing annotation
  14. Boot parameter:
  15. sysctl.vm.mem_profiling={0|1|never}[,compressed]
  16. When set to "never", memory allocation profiling overhead is minimized and it
  17. cannot be enabled at runtime (sysctl becomes read-only).
  18. When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=y, default value is "1".
  19. When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=n, default value is "never".
  20. "compressed" optional parameter will try to store page tag references in a
  21. compact format, avoiding page extensions. This results in improved performance
  22. and memory consumption, however it might fail depending on system configuration.
  23. If compression fails, a warning is issued and memory allocation profiling gets
  24. disabled.
  25. sysctl:
  26. /proc/sys/vm/mem_profiling
  27. 1: Enable memory profiling.
  28. 0: Disable memory profiling.
  29. The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT.
  30. When CONFIG_MEM_ALLOC_PROFILING_DEBUG=y, this control is read-only to avoid
  31. warnings produced by allocations made while profiling is disabled and freed
  32. when it's enabled.
  33. Runtime info:
  34. /proc/allocinfo
  35. Example output::
  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. Theory of operation
  49. ===================
  50. Memory allocation profiling builds off of code tagging, which is a library for
  51. declaring static structs (that typically describe a file and line number in
  52. some way, hence code tagging) and then finding and operating on them at runtime,
  53. - i.e. iterating over them to print them in debugfs/procfs.
  54. To add accounting for an allocation call, we replace it with a macro
  55. invocation, alloc_hooks(), that
  56. - declares a code tag
  57. - stashes a pointer to it in task_struct
  58. - calls the real allocation function
  59. - and finally, restores the task_struct alloc tag pointer to its previous value.
  60. This allows for alloc_hooks() calls to be nested, with the most recent one
  61. taking effect. This is important for allocations internal to the mm/ code that
  62. do not properly belong to the outer allocation context and should be counted
  63. separately: for example, slab object extension vectors, or when the slab
  64. allocates pages from the page allocator.
  65. Thus, proper usage requires determining which function in an allocation call
  66. stack should be tagged. There are many helper functions that essentially wrap
  67. e.g. kmalloc() and do a little more work, then are called in multiple places;
  68. we'll generally want the accounting to happen in the callers of these helpers,
  69. not in the helpers themselves.
  70. To fix up a given helper, for example foo(), do the following:
  71. - switch its allocation call to the _noprof() version, e.g. kmalloc_noprof()
  72. - rename it to foo_noprof()
  73. - define a macro version of foo() like so:
  74. #define foo(...) alloc_hooks(foo_noprof(__VA_ARGS__))
  75. It's also possible to stash a pointer to an alloc tag in your own data structures.
  76. Do this when you're implementing a generic data structure that does allocations
  77. "on behalf of" some other code - for example, the rhashtable code. This way,
  78. instead of seeing a large line in /proc/allocinfo for rhashtable.c, we can
  79. break it out by rhashtable type.
  80. To do so:
  81. - Hook your data structure's init function, like any other allocation function.
  82. - Within your init function, use the convenience macro alloc_tag_record() to
  83. record alloc tag in your data structure.
  84. - Then, use the following form for your allocations:
  85. alloc_hooks_tag(ht->your_saved_tag, kmalloc_noprof(...))