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