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