slub.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. .. _slub:
  2. ==========================
  3. Short users guide for SLUB
  4. ==========================
  5. The basic philosophy of SLUB is very different from SLAB. SLAB
  6. requires rebuilding the kernel to activate debug options for all
  7. slab caches. SLUB always includes full debugging but it is off by default.
  8. SLUB can enable debugging only for selected slabs in order to avoid
  9. an impact on overall system performance which may make a bug more
  10. difficult to find.
  11. In order to switch debugging on one can add an option ``slub_debug``
  12. to the kernel command line. That will enable full debugging for
  13. all slabs.
  14. Typically one would then use the ``slabinfo`` command to get statistical
  15. data and perform operation on the slabs. By default ``slabinfo`` only lists
  16. slabs that have data in them. See "slabinfo -h" for more options when
  17. running the command. ``slabinfo`` can be compiled with
  18. ::
  19. gcc -o slabinfo tools/vm/slabinfo.c
  20. Some of the modes of operation of ``slabinfo`` require that slub debugging
  21. be enabled on the command line. F.e. no tracking information will be
  22. available without debugging on and validation can only partially
  23. be performed if debugging was not switched on.
  24. Some more sophisticated uses of slub_debug:
  25. -------------------------------------------
  26. Parameters may be given to ``slub_debug``. If none is specified then full
  27. debugging is enabled. Format:
  28. slub_debug=<Debug-Options>
  29. Enable options for all slabs
  30. slub_debug=<Debug-Options>,<slab name>
  31. Enable options only for select slabs
  32. Possible debug options are::
  33. F Sanity checks on (enables SLAB_DEBUG_CONSISTENCY_CHECKS
  34. Sorry SLAB legacy issues)
  35. Z Red zoning
  36. P Poisoning (object and padding)
  37. U User tracking (free and alloc)
  38. T Trace (please only use on single slabs)
  39. A Toggle failslab filter mark for the cache
  40. O Switch debugging off for caches that would have
  41. caused higher minimum slab orders
  42. - Switch all debugging off (useful if the kernel is
  43. configured with CONFIG_SLUB_DEBUG_ON)
  44. F.e. in order to boot just with sanity checks and red zoning one would specify::
  45. slub_debug=FZ
  46. Trying to find an issue in the dentry cache? Try::
  47. slub_debug=,dentry
  48. to only enable debugging on the dentry cache.
  49. Red zoning and tracking may realign the slab. We can just apply sanity checks
  50. to the dentry cache with::
  51. slub_debug=F,dentry
  52. Debugging options may require the minimum possible slab order to increase as
  53. a result of storing the metadata (for example, caches with PAGE_SIZE object
  54. sizes). This has a higher liklihood of resulting in slab allocation errors
  55. in low memory situations or if there's high fragmentation of memory. To
  56. switch off debugging for such caches by default, use::
  57. slub_debug=O
  58. In case you forgot to enable debugging on the kernel command line: It is
  59. possible to enable debugging manually when the kernel is up. Look at the
  60. contents of::
  61. /sys/kernel/slab/<slab name>/
  62. Look at the writable files. Writing 1 to them will enable the
  63. corresponding debug option. All options can be set on a slab that does
  64. not contain objects. If the slab already contains objects then sanity checks
  65. and tracing may only be enabled. The other options may cause the realignment
  66. of objects.
  67. Careful with tracing: It may spew out lots of information and never stop if
  68. used on the wrong slab.
  69. Slab merging
  70. ============
  71. If no debug options are specified then SLUB may merge similar slabs together
  72. in order to reduce overhead and increase cache hotness of objects.
  73. ``slabinfo -a`` displays which slabs were merged together.
  74. Slab validation
  75. ===============
  76. SLUB can validate all object if the kernel was booted with slub_debug. In
  77. order to do so you must have the ``slabinfo`` tool. Then you can do
  78. ::
  79. slabinfo -v
  80. which will test all objects. Output will be generated to the syslog.
  81. This also works in a more limited way if boot was without slab debug.
  82. In that case ``slabinfo -v`` simply tests all reachable objects. Usually
  83. these are in the cpu slabs and the partial slabs. Full slabs are not
  84. tracked by SLUB in a non debug situation.
  85. Getting more performance
  86. ========================
  87. To some degree SLUB's performance is limited by the need to take the
  88. list_lock once in a while to deal with partial slabs. That overhead is
  89. governed by the order of the allocation for each slab. The allocations
  90. can be influenced by kernel parameters:
  91. .. slub_min_objects=x (default 4)
  92. .. slub_min_order=x (default 0)
  93. .. slub_max_order=x (default 3 (PAGE_ALLOC_COSTLY_ORDER))
  94. ``slub_min_objects``
  95. allows to specify how many objects must at least fit into one
  96. slab in order for the allocation order to be acceptable. In
  97. general slub will be able to perform this number of
  98. allocations on a slab without consulting centralized resources
  99. (list_lock) where contention may occur.
  100. ``slub_min_order``
  101. specifies a minim order of slabs. A similar effect like
  102. ``slub_min_objects``.
  103. ``slub_max_order``
  104. specified the order at which ``slub_min_objects`` should no
  105. longer be checked. This is useful to avoid SLUB trying to
  106. generate super large order pages to fit ``slub_min_objects``
  107. of a slab cache with large object sizes into one high order
  108. page. Setting command line parameter
  109. ``debug_guardpage_minorder=N`` (N > 0), forces setting
  110. ``slub_max_order`` to 0, what cause minimum possible order of
  111. slabs allocation.
  112. SLUB Debug output
  113. =================
  114. Here is a sample of slub debug output::
  115. ====================================================================
  116. BUG kmalloc-8: Redzone overwritten
  117. --------------------------------------------------------------------
  118. INFO: 0xc90f6d28-0xc90f6d2b. First byte 0x00 instead of 0xcc
  119. INFO: Slab 0xc528c530 flags=0x400000c3 inuse=61 fp=0xc90f6d58
  120. INFO: Object 0xc90f6d20 @offset=3360 fp=0xc90f6d58
  121. INFO: Allocated in get_modalias+0x61/0xf5 age=53 cpu=1 pid=554
  122. Bytes b4 0xc90f6d10: 00 00 00 00 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a ........ZZZZZZZZ
  123. Object 0xc90f6d20: 31 30 31 39 2e 30 30 35 1019.005
  124. Redzone 0xc90f6d28: 00 cc cc cc .
  125. Padding 0xc90f6d50: 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ
  126. [<c010523d>] dump_trace+0x63/0x1eb
  127. [<c01053df>] show_trace_log_lvl+0x1a/0x2f
  128. [<c010601d>] show_trace+0x12/0x14
  129. [<c0106035>] dump_stack+0x16/0x18
  130. [<c017e0fa>] object_err+0x143/0x14b
  131. [<c017e2cc>] check_object+0x66/0x234
  132. [<c017eb43>] __slab_free+0x239/0x384
  133. [<c017f446>] kfree+0xa6/0xc6
  134. [<c02e2335>] get_modalias+0xb9/0xf5
  135. [<c02e23b7>] dmi_dev_uevent+0x27/0x3c
  136. [<c027866a>] dev_uevent+0x1ad/0x1da
  137. [<c0205024>] kobject_uevent_env+0x20a/0x45b
  138. [<c020527f>] kobject_uevent+0xa/0xf
  139. [<c02779f1>] store_uevent+0x4f/0x58
  140. [<c027758e>] dev_attr_store+0x29/0x2f
  141. [<c01bec4f>] sysfs_write_file+0x16e/0x19c
  142. [<c0183ba7>] vfs_write+0xd1/0x15a
  143. [<c01841d7>] sys_write+0x3d/0x72
  144. [<c0104112>] sysenter_past_esp+0x5f/0x99
  145. [<b7f7b410>] 0xb7f7b410
  146. =======================
  147. FIX kmalloc-8: Restoring Redzone 0xc90f6d28-0xc90f6d2b=0xcc
  148. If SLUB encounters a corrupted object (full detection requires the kernel
  149. to be booted with slub_debug) then the following output will be dumped
  150. into the syslog:
  151. 1. Description of the problem encountered
  152. This will be a message in the system log starting with::
  153. ===============================================
  154. BUG <slab cache affected>: <What went wrong>
  155. -----------------------------------------------
  156. INFO: <corruption start>-<corruption_end> <more info>
  157. INFO: Slab <address> <slab information>
  158. INFO: Object <address> <object information>
  159. INFO: Allocated in <kernel function> age=<jiffies since alloc> cpu=<allocated by
  160. cpu> pid=<pid of the process>
  161. INFO: Freed in <kernel function> age=<jiffies since free> cpu=<freed by cpu>
  162. pid=<pid of the process>
  163. (Object allocation / free information is only available if SLAB_STORE_USER is
  164. set for the slab. slub_debug sets that option)
  165. 2. The object contents if an object was involved.
  166. Various types of lines can follow the BUG SLUB line:
  167. Bytes b4 <address> : <bytes>
  168. Shows a few bytes before the object where the problem was detected.
  169. Can be useful if the corruption does not stop with the start of the
  170. object.
  171. Object <address> : <bytes>
  172. The bytes of the object. If the object is inactive then the bytes
  173. typically contain poison values. Any non-poison value shows a
  174. corruption by a write after free.
  175. Redzone <address> : <bytes>
  176. The Redzone following the object. The Redzone is used to detect
  177. writes after the object. All bytes should always have the same
  178. value. If there is any deviation then it is due to a write after
  179. the object boundary.
  180. (Redzone information is only available if SLAB_RED_ZONE is set.
  181. slub_debug sets that option)
  182. Padding <address> : <bytes>
  183. Unused data to fill up the space in order to get the next object
  184. properly aligned. In the debug case we make sure that there are
  185. at least 4 bytes of padding. This allows the detection of writes
  186. before the object.
  187. 3. A stackdump
  188. The stackdump describes the location where the error was detected. The cause
  189. of the corruption is may be more likely found by looking at the function that
  190. allocated or freed the object.
  191. 4. Report on how the problem was dealt with in order to ensure the continued
  192. operation of the system.
  193. These are messages in the system log beginning with::
  194. FIX <slab cache affected>: <corrective action taken>
  195. In the above sample SLUB found that the Redzone of an active object has
  196. been overwritten. Here a string of 8 characters was written into a slab that
  197. has the length of 8 characters. However, a 8 character string needs a
  198. terminating 0. That zero has overwritten the first byte of the Redzone field.
  199. After reporting the details of the issue encountered the FIX SLUB message
  200. tells us that SLUB has restored the Redzone to its proper value and then
  201. system operations continue.
  202. Emergency operations
  203. ====================
  204. Minimal debugging (sanity checks alone) can be enabled by booting with::
  205. slub_debug=F
  206. This will be generally be enough to enable the resiliency features of slub
  207. which will keep the system running even if a bad kernel component will
  208. keep corrupting objects. This may be important for production systems.
  209. Performance will be impacted by the sanity checks and there will be a
  210. continual stream of error messages to the syslog but no additional memory
  211. will be used (unlike full debugging).
  212. No guarantees. The kernel component still needs to be fixed. Performance
  213. may be optimized further by locating the slab that experiences corruption
  214. and enabling debugging only for that cache
  215. I.e.::
  216. slub_debug=F,dentry
  217. If the corruption occurs by writing after the end of the object then it
  218. may be advisable to enable a Redzone to avoid corrupting the beginning
  219. of other objects::
  220. slub_debug=FZ,dentry
  221. Extended slabinfo mode and plotting
  222. ===================================
  223. The ``slabinfo`` tool has a special 'extended' ('-X') mode that includes:
  224. - Slabcache Totals
  225. - Slabs sorted by size (up to -N <num> slabs, default 1)
  226. - Slabs sorted by loss (up to -N <num> slabs, default 1)
  227. Additionally, in this mode ``slabinfo`` does not dynamically scale
  228. sizes (G/M/K) and reports everything in bytes (this functionality is
  229. also available to other slabinfo modes via '-B' option) which makes
  230. reporting more precise and accurate. Moreover, in some sense the `-X'
  231. mode also simplifies the analysis of slabs' behaviour, because its
  232. output can be plotted using the ``slabinfo-gnuplot.sh`` script. So it
  233. pushes the analysis from looking through the numbers (tons of numbers)
  234. to something easier -- visual analysis.
  235. To generate plots:
  236. a) collect slabinfo extended records, for example::
  237. while [ 1 ]; do slabinfo -X >> FOO_STATS; sleep 1; done
  238. b) pass stats file(-s) to ``slabinfo-gnuplot.sh`` script::
  239. slabinfo-gnuplot.sh FOO_STATS [FOO_STATS2 .. FOO_STATSN]
  240. The ``slabinfo-gnuplot.sh`` script will pre-processes the collected records
  241. and generates 3 png files (and 3 pre-processing cache files) per STATS
  242. file:
  243. - Slabcache Totals: FOO_STATS-totals.png
  244. - Slabs sorted by size: FOO_STATS-slabs-by-size.png
  245. - Slabs sorted by loss: FOO_STATS-slabs-by-loss.png
  246. Another use case, when ``slabinfo-gnuplot.sh`` can be useful, is when you
  247. need to compare slabs' behaviour "prior to" and "after" some code
  248. modification. To help you out there, ``slabinfo-gnuplot.sh`` script
  249. can 'merge' the `Slabcache Totals` sections from different
  250. measurements. To visually compare N plots:
  251. a) Collect as many STATS1, STATS2, .. STATSN files as you need::
  252. while [ 1 ]; do slabinfo -X >> STATS<X>; sleep 1; done
  253. b) Pre-process those STATS files::
  254. slabinfo-gnuplot.sh STATS1 STATS2 .. STATSN
  255. c) Execute ``slabinfo-gnuplot.sh`` in '-t' mode, passing all of the
  256. generated pre-processed \*-totals::
  257. slabinfo-gnuplot.sh -t STATS1-totals STATS2-totals .. STATSN-totals
  258. This will produce a single plot (png file).
  259. Plots, expectedly, can be large so some fluctuations or small spikes
  260. can go unnoticed. To deal with that, ``slabinfo-gnuplot.sh`` has two
  261. options to 'zoom-in'/'zoom-out':
  262. a) ``-s %d,%d`` -- overwrites the default image width and heigh
  263. b) ``-r %d,%d`` -- specifies a range of samples to use (for example,
  264. in ``slabinfo -X >> FOO_STATS; sleep 1;`` case, using a ``-r
  265. 40,60`` range will plot only samples collected between 40th and
  266. 60th seconds).
  267. Christoph Lameter, May 30, 2007
  268. Sergey Senozhatsky, October 23, 2015