slub.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. ==========================
  2. Short users guide for SLUB
  3. ==========================
  4. The basic philosophy of SLUB is very different from SLAB. SLAB
  5. requires rebuilding the kernel to activate debug options for all
  6. slab caches. SLUB always includes full debugging but it is off by default.
  7. SLUB can enable debugging only for selected slabs in order to avoid
  8. an impact on overall system performance which may make a bug more
  9. difficult to find.
  10. In order to switch debugging on one can add an option ``slab_debug``
  11. to the kernel command line. That will enable full debugging for
  12. all slabs.
  13. Typically one would then use the ``slabinfo`` command to get statistical
  14. data and perform operation on the slabs. By default ``slabinfo`` only lists
  15. slabs that have data in them. See "slabinfo -h" for more options when
  16. running the command. ``slabinfo`` can be compiled with
  17. ::
  18. gcc -o slabinfo tools/mm/slabinfo.c
  19. Some of the modes of operation of ``slabinfo`` require that slub debugging
  20. be enabled on the command line. F.e. no tracking information will be
  21. available without debugging on and validation can only partially
  22. be performed if debugging was not switched on.
  23. Some more sophisticated uses of slab_debug:
  24. -------------------------------------------
  25. Parameters may be given to ``slab_debug``. If none is specified then full
  26. debugging is enabled. Format:
  27. slab_debug=<Debug-Options>
  28. Enable options for all slabs
  29. slab_debug=<Debug-Options>,<slab name1>,<slab name2>,...
  30. Enable options only for select slabs (no spaces
  31. after a comma)
  32. Multiple blocks of options for all slabs or selected slabs can be given, with
  33. blocks of options delimited by ';'. The last of "all slabs" blocks is applied
  34. to all slabs except those that match one of the "select slabs" block. Options
  35. of the first "select slabs" blocks that matches the slab's name are applied.
  36. Possible debug options are::
  37. F Sanity checks on (enables SLAB_DEBUG_CONSISTENCY_CHECKS
  38. Sorry SLAB legacy issues)
  39. Z Red zoning
  40. P Poisoning (object and padding)
  41. U User tracking (free and alloc)
  42. T Trace (please only use on single slabs)
  43. A Enable failslab filter mark for the cache
  44. O Switch debugging off for caches that would have
  45. caused higher minimum slab orders
  46. - Switch all debugging off (useful if the kernel is
  47. configured with CONFIG_SLUB_DEBUG_ON)
  48. F.e. in order to boot just with sanity checks and red zoning one would specify::
  49. slab_debug=FZ
  50. Trying to find an issue in the dentry cache? Try::
  51. slab_debug=,dentry
  52. to only enable debugging on the dentry cache. You may use an asterisk at the
  53. end of the slab name, in order to cover all slabs with the same prefix. For
  54. example, here's how you can poison the dentry cache as well as all kmalloc
  55. slabs::
  56. slab_debug=P,kmalloc-*,dentry
  57. Red zoning and tracking may realign the slab. We can just apply sanity checks
  58. to the dentry cache with::
  59. slab_debug=F,dentry
  60. Debugging options may require the minimum possible slab order to increase as
  61. a result of storing the metadata (for example, caches with PAGE_SIZE object
  62. sizes). This has a higher likelihood of resulting in slab allocation errors
  63. in low memory situations or if there's high fragmentation of memory. To
  64. switch off debugging for such caches by default, use::
  65. slab_debug=O
  66. You can apply different options to different list of slab names, using blocks
  67. of options. This will enable red zoning for dentry and user tracking for
  68. kmalloc. All other slabs will not get any debugging enabled::
  69. slab_debug=Z,dentry;U,kmalloc-*
  70. You can also enable options (e.g. sanity checks and poisoning) for all caches
  71. except some that are deemed too performance critical and don't need to be
  72. debugged by specifying global debug options followed by a list of slab names
  73. with "-" as options::
  74. slab_debug=FZ;-,zs_handle,zspage
  75. The state of each debug option for a slab can be found in the respective files
  76. under::
  77. /sys/kernel/slab/<slab name>/
  78. If the file contains 1, the option is enabled, 0 means disabled. The debug
  79. options from the ``slab_debug`` parameter translate to the following files::
  80. F sanity_checks
  81. Z red_zone
  82. P poison
  83. U store_user
  84. T trace
  85. A failslab
  86. failslab file is writable, so writing 1 or 0 will enable or disable
  87. the option at runtime. Write returns -EINVAL if cache is an alias.
  88. Careful with tracing: It may spew out lots of information and never stop if
  89. used on the wrong slab.
  90. Slab merging
  91. ============
  92. If no debug options are specified then SLUB may merge similar slabs together
  93. in order to reduce overhead and increase cache hotness of objects.
  94. ``slabinfo -a`` displays which slabs were merged together.
  95. Slab validation
  96. ===============
  97. SLUB can validate all object if the kernel was booted with slab_debug. In
  98. order to do so you must have the ``slabinfo`` tool. Then you can do
  99. ::
  100. slabinfo -v
  101. which will test all objects. Output will be generated to the syslog.
  102. This also works in a more limited way if boot was without slab debug.
  103. In that case ``slabinfo -v`` simply tests all reachable objects. Usually
  104. these are in the cpu slabs and the partial slabs. Full slabs are not
  105. tracked by SLUB in a non debug situation.
  106. Getting more performance
  107. ========================
  108. To some degree SLUB's performance is limited by the need to take the
  109. list_lock once in a while to deal with partial slabs. That overhead is
  110. governed by the order of the allocation for each slab. The allocations
  111. can be influenced by kernel parameters:
  112. .. slab_min_objects=x (default: automatically scaled by number of cpus)
  113. .. slab_min_order=x (default 0)
  114. .. slab_max_order=x (default 3 (PAGE_ALLOC_COSTLY_ORDER))
  115. ``slab_min_objects``
  116. allows to specify how many objects must at least fit into one
  117. slab in order for the allocation order to be acceptable. In
  118. general slub will be able to perform this number of
  119. allocations on a slab without consulting centralized resources
  120. (list_lock) where contention may occur.
  121. ``slab_min_order``
  122. specifies a minimum order of slabs. A similar effect like
  123. ``slab_min_objects``.
  124. ``slab_max_order``
  125. specified the order at which ``slab_min_objects`` should no
  126. longer be checked. This is useful to avoid SLUB trying to
  127. generate super large order pages to fit ``slab_min_objects``
  128. of a slab cache with large object sizes into one high order
  129. page. Setting command line parameter
  130. ``debug_guardpage_minorder=N`` (N > 0), forces setting
  131. ``slab_max_order`` to 0, what cause minimum possible order of
  132. slabs allocation.
  133. SLUB Debug output
  134. =================
  135. Here is a sample of slub debug output::
  136. ====================================================================
  137. BUG kmalloc-8: Right Redzone overwritten
  138. --------------------------------------------------------------------
  139. INFO: 0xc90f6d28-0xc90f6d2b. First byte 0x00 instead of 0xcc
  140. INFO: Slab 0xc528c530 flags=0x400000c3 inuse=61 fp=0xc90f6d58
  141. INFO: Object 0xc90f6d20 @offset=3360 fp=0xc90f6d58
  142. INFO: Allocated in get_modalias+0x61/0xf5 age=53 cpu=1 pid=554
  143. Bytes b4 (0xc90f6d10): 00 00 00 00 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a ........ZZZZZZZZ
  144. Object (0xc90f6d20): 31 30 31 39 2e 30 30 35 1019.005
  145. Redzone (0xc90f6d28): 00 cc cc cc .
  146. Padding (0xc90f6d50): 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ
  147. [<c010523d>] dump_trace+0x63/0x1eb
  148. [<c01053df>] show_trace_log_lvl+0x1a/0x2f
  149. [<c010601d>] show_trace+0x12/0x14
  150. [<c0106035>] dump_stack+0x16/0x18
  151. [<c017e0fa>] object_err+0x143/0x14b
  152. [<c017e2cc>] check_object+0x66/0x234
  153. [<c017eb43>] __slab_free+0x239/0x384
  154. [<c017f446>] kfree+0xa6/0xc6
  155. [<c02e2335>] get_modalias+0xb9/0xf5
  156. [<c02e23b7>] dmi_dev_uevent+0x27/0x3c
  157. [<c027866a>] dev_uevent+0x1ad/0x1da
  158. [<c0205024>] kobject_uevent_env+0x20a/0x45b
  159. [<c020527f>] kobject_uevent+0xa/0xf
  160. [<c02779f1>] store_uevent+0x4f/0x58
  161. [<c027758e>] dev_attr_store+0x29/0x2f
  162. [<c01bec4f>] sysfs_write_file+0x16e/0x19c
  163. [<c0183ba7>] vfs_write+0xd1/0x15a
  164. [<c01841d7>] sys_write+0x3d/0x72
  165. [<c0104112>] sysenter_past_esp+0x5f/0x99
  166. [<b7f7b410>] 0xb7f7b410
  167. =======================
  168. FIX kmalloc-8: Restoring Redzone 0xc90f6d28-0xc90f6d2b=0xcc
  169. If SLUB encounters a corrupted object (full detection requires the kernel
  170. to be booted with slab_debug) then the following output will be dumped
  171. into the syslog:
  172. 1. Description of the problem encountered
  173. This will be a message in the system log starting with::
  174. ===============================================
  175. BUG <slab cache affected>: <What went wrong>
  176. -----------------------------------------------
  177. INFO: <corruption start>-<corruption_end> <more info>
  178. INFO: Slab <address> <slab information>
  179. INFO: Object <address> <object information>
  180. INFO: Allocated in <kernel function> age=<jiffies since alloc> cpu=<allocated by
  181. cpu> pid=<pid of the process>
  182. INFO: Freed in <kernel function> age=<jiffies since free> cpu=<freed by cpu>
  183. pid=<pid of the process>
  184. (Object allocation / free information is only available if SLAB_STORE_USER is
  185. set for the slab. slab_debug sets that option)
  186. 2. The object contents if an object was involved.
  187. Various types of lines can follow the BUG SLUB line:
  188. Bytes b4 <address> : <bytes>
  189. Shows a few bytes before the object where the problem was detected.
  190. Can be useful if the corruption does not stop with the start of the
  191. object.
  192. Object <address> : <bytes>
  193. The bytes of the object. If the object is inactive then the bytes
  194. typically contain poison values. Any non-poison value shows a
  195. corruption by a write after free.
  196. Redzone <address> : <bytes>
  197. The Redzone following the object. The Redzone is used to detect
  198. writes after the object. All bytes should always have the same
  199. value. If there is any deviation then it is due to a write after
  200. the object boundary.
  201. (Redzone information is only available if SLAB_RED_ZONE is set.
  202. slab_debug sets that option)
  203. Padding <address> : <bytes>
  204. Unused data to fill up the space in order to get the next object
  205. properly aligned. In the debug case we make sure that there are
  206. at least 4 bytes of padding. This allows the detection of writes
  207. before the object.
  208. 3. A stackdump
  209. The stackdump describes the location where the error was detected. The cause
  210. of the corruption is may be more likely found by looking at the function that
  211. allocated or freed the object.
  212. 4. Report on how the problem was dealt with in order to ensure the continued
  213. operation of the system.
  214. These are messages in the system log beginning with::
  215. FIX <slab cache affected>: <corrective action taken>
  216. In the above sample SLUB found that the Redzone of an active object has
  217. been overwritten. Here a string of 8 characters was written into a slab that
  218. has the length of 8 characters. However, a 8 character string needs a
  219. terminating 0. That zero has overwritten the first byte of the Redzone field.
  220. After reporting the details of the issue encountered the FIX SLUB message
  221. tells us that SLUB has restored the Redzone to its proper value and then
  222. system operations continue.
  223. Emergency operations
  224. ====================
  225. Minimal debugging (sanity checks alone) can be enabled by booting with::
  226. slab_debug=F
  227. This will be generally be enough to enable the resiliency features of slub
  228. which will keep the system running even if a bad kernel component will
  229. keep corrupting objects. This may be important for production systems.
  230. Performance will be impacted by the sanity checks and there will be a
  231. continual stream of error messages to the syslog but no additional memory
  232. will be used (unlike full debugging).
  233. No guarantees. The kernel component still needs to be fixed. Performance
  234. may be optimized further by locating the slab that experiences corruption
  235. and enabling debugging only for that cache
  236. I.e.::
  237. slab_debug=F,dentry
  238. If the corruption occurs by writing after the end of the object then it
  239. may be advisable to enable a Redzone to avoid corrupting the beginning
  240. of other objects::
  241. slab_debug=FZ,dentry
  242. Extended slabinfo mode and plotting
  243. ===================================
  244. The ``slabinfo`` tool has a special 'extended' ('-X') mode that includes:
  245. - Slabcache Totals
  246. - Slabs sorted by size (up to -N <num> slabs, default 1)
  247. - Slabs sorted by loss (up to -N <num> slabs, default 1)
  248. Additionally, in this mode ``slabinfo`` does not dynamically scale
  249. sizes (G/M/K) and reports everything in bytes (this functionality is
  250. also available to other slabinfo modes via '-B' option) which makes
  251. reporting more precise and accurate. Moreover, in some sense the `-X'
  252. mode also simplifies the analysis of slabs' behaviour, because its
  253. output can be plotted using the ``slabinfo-gnuplot.sh`` script. So it
  254. pushes the analysis from looking through the numbers (tons of numbers)
  255. to something easier -- visual analysis.
  256. To generate plots:
  257. a) collect slabinfo extended records, for example::
  258. while [ 1 ]; do slabinfo -X >> FOO_STATS; sleep 1; done
  259. b) pass stats file(-s) to ``slabinfo-gnuplot.sh`` script::
  260. slabinfo-gnuplot.sh FOO_STATS [FOO_STATS2 .. FOO_STATSN]
  261. The ``slabinfo-gnuplot.sh`` script will pre-processes the collected records
  262. and generates 3 png files (and 3 pre-processing cache files) per STATS
  263. file:
  264. - Slabcache Totals: FOO_STATS-totals.png
  265. - Slabs sorted by size: FOO_STATS-slabs-by-size.png
  266. - Slabs sorted by loss: FOO_STATS-slabs-by-loss.png
  267. Another use case, when ``slabinfo-gnuplot.sh`` can be useful, is when you
  268. need to compare slabs' behaviour "prior to" and "after" some code
  269. modification. To help you out there, ``slabinfo-gnuplot.sh`` script
  270. can 'merge' the `Slabcache Totals` sections from different
  271. measurements. To visually compare N plots:
  272. a) Collect as many STATS1, STATS2, .. STATSN files as you need::
  273. while [ 1 ]; do slabinfo -X >> STATS<X>; sleep 1; done
  274. b) Pre-process those STATS files::
  275. slabinfo-gnuplot.sh STATS1 STATS2 .. STATSN
  276. c) Execute ``slabinfo-gnuplot.sh`` in '-t' mode, passing all of the
  277. generated pre-processed \*-totals::
  278. slabinfo-gnuplot.sh -t STATS1-totals STATS2-totals .. STATSN-totals
  279. This will produce a single plot (png file).
  280. Plots, expectedly, can be large so some fluctuations or small spikes
  281. can go unnoticed. To deal with that, ``slabinfo-gnuplot.sh`` has two
  282. options to 'zoom-in'/'zoom-out':
  283. a) ``-s %d,%d`` -- overwrites the default image width and height
  284. b) ``-r %d,%d`` -- specifies a range of samples to use (for example,
  285. in ``slabinfo -X >> FOO_STATS; sleep 1;`` case, using a ``-r
  286. 40,60`` range will plot only samples collected between 40th and
  287. 60th seconds).
  288. DebugFS files for SLUB
  289. ======================
  290. For more information about current state of SLUB caches with the user tracking
  291. debug option enabled, debugfs files are available, typically under
  292. /sys/kernel/debug/slab/<cache>/ (created only for caches with enabled user
  293. tracking). There are 2 types of these files with the following debug
  294. information:
  295. 1. alloc_traces::
  296. Prints information about unique allocation traces of the currently
  297. allocated objects. The output is sorted by frequency of each trace.
  298. Information in the output:
  299. Number of objects, allocating function, possible memory wastage of
  300. kmalloc objects(total/per-object), minimal/average/maximal jiffies
  301. since alloc, pid range of the allocating processes, cpu mask of
  302. allocating cpus, numa node mask of origins of memory, and stack trace.
  303. Example:::
  304. 338 pci_alloc_dev+0x2c/0xa0 waste=521872/1544 age=290837/291891/293509 pid=1 cpus=106 nodes=0-1
  305. __kmem_cache_alloc_node+0x11f/0x4e0
  306. kmalloc_trace+0x26/0xa0
  307. pci_alloc_dev+0x2c/0xa0
  308. pci_scan_single_device+0xd2/0x150
  309. pci_scan_slot+0xf7/0x2d0
  310. pci_scan_child_bus_extend+0x4e/0x360
  311. acpi_pci_root_create+0x32e/0x3b0
  312. pci_acpi_scan_root+0x2b9/0x2d0
  313. acpi_pci_root_add.cold.11+0x110/0xb0a
  314. acpi_bus_attach+0x262/0x3f0
  315. device_for_each_child+0xb7/0x110
  316. acpi_dev_for_each_child+0x77/0xa0
  317. acpi_bus_attach+0x108/0x3f0
  318. device_for_each_child+0xb7/0x110
  319. acpi_dev_for_each_child+0x77/0xa0
  320. acpi_bus_attach+0x108/0x3f0
  321. 2. free_traces::
  322. Prints information about unique freeing traces of the currently allocated
  323. objects. The freeing traces thus come from the previous life-cycle of the
  324. objects and are reported as not available for objects allocated for the first
  325. time. The output is sorted by frequency of each trace.
  326. Information in the output:
  327. Number of objects, freeing function, minimal/average/maximal jiffies since free,
  328. pid range of the freeing processes, cpu mask of freeing cpus, and stack trace.
  329. Example:::
  330. 1980 <not-available> age=4294912290 pid=0 cpus=0
  331. 51 acpi_ut_update_ref_count+0x6a6/0x782 age=236886/237027/237772 pid=1 cpus=1
  332. kfree+0x2db/0x420
  333. acpi_ut_update_ref_count+0x6a6/0x782
  334. acpi_ut_update_object_reference+0x1ad/0x234
  335. acpi_ut_remove_reference+0x7d/0x84
  336. acpi_rs_get_prt_method_data+0x97/0xd6
  337. acpi_get_irq_routing_table+0x82/0xc4
  338. acpi_pci_irq_find_prt_entry+0x8e/0x2e0
  339. acpi_pci_irq_lookup+0x3a/0x1e0
  340. acpi_pci_irq_enable+0x77/0x240
  341. pcibios_enable_device+0x39/0x40
  342. do_pci_enable_device.part.0+0x5d/0xe0
  343. pci_enable_device_flags+0xfc/0x120
  344. pci_enable_device+0x13/0x20
  345. virtio_pci_probe+0x9e/0x170
  346. local_pci_probe+0x48/0x80
  347. pci_device_probe+0x105/0x1c0
  348. Christoph Lameter, May 30, 2007
  349. Sergey Senozhatsky, October 23, 2015