]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
81819f0f CL |
2 | /* |
3 | * SLUB: A slab allocator that limits cache line use instead of queuing | |
4 | * objects in per cpu and per node lists. | |
5 | * | |
dc84207d | 6 | * The allocator synchronizes using per slab locks or atomic operations |
881db7fb | 7 | * and only uses a centralized lock to manage a pool of partial slabs. |
81819f0f | 8 | * |
cde53535 | 9 | * (C) 2007 SGI, Christoph Lameter |
881db7fb | 10 | * (C) 2011 Linux Foundation, Christoph Lameter |
81819f0f CL |
11 | */ |
12 | ||
13 | #include <linux/mm.h> | |
1eb5ac64 | 14 | #include <linux/swap.h> /* struct reclaim_state */ |
81819f0f CL |
15 | #include <linux/module.h> |
16 | #include <linux/bit_spinlock.h> | |
17 | #include <linux/interrupt.h> | |
1b3865d0 | 18 | #include <linux/swab.h> |
81819f0f CL |
19 | #include <linux/bitops.h> |
20 | #include <linux/slab.h> | |
97d06609 | 21 | #include "slab.h" |
7b3c3a50 | 22 | #include <linux/proc_fs.h> |
81819f0f | 23 | #include <linux/seq_file.h> |
a79316c6 | 24 | #include <linux/kasan.h> |
68ef169a | 25 | #include <linux/kmsan.h> |
81819f0f CL |
26 | #include <linux/cpu.h> |
27 | #include <linux/cpuset.h> | |
28 | #include <linux/mempolicy.h> | |
29 | #include <linux/ctype.h> | |
5cf909c5 | 30 | #include <linux/stackdepot.h> |
3ac7fe5a | 31 | #include <linux/debugobjects.h> |
81819f0f | 32 | #include <linux/kallsyms.h> |
b89fb5ef | 33 | #include <linux/kfence.h> |
b9049e23 | 34 | #include <linux/memory.h> |
f8bd2258 | 35 | #include <linux/math64.h> |
773ff60e | 36 | #include <linux/fault-inject.h> |
bfa71457 | 37 | #include <linux/stacktrace.h> |
4de900b4 | 38 | #include <linux/prefetch.h> |
2633d7a0 | 39 | #include <linux/memcontrol.h> |
2482ddec | 40 | #include <linux/random.h> |
1f9f78b1 | 41 | #include <kunit/test.h> |
553c0369 | 42 | #include <linux/sort.h> |
81819f0f | 43 | |
64dd6849 | 44 | #include <linux/debugfs.h> |
4a92379b RK |
45 | #include <trace/events/kmem.h> |
46 | ||
072bb0aa MG |
47 | #include "internal.h" |
48 | ||
81819f0f CL |
49 | /* |
50 | * Lock order: | |
18004c5d | 51 | * 1. slab_mutex (Global Mutex) |
bd0e7491 VB |
52 | * 2. node->list_lock (Spinlock) |
53 | * 3. kmem_cache->cpu_slab->lock (Local lock) | |
41bec7c3 | 54 | * 4. slab_lock(slab) (Only on some arches) |
bd0e7491 | 55 | * 5. object_map_lock (Only for debugging) |
81819f0f | 56 | * |
18004c5d | 57 | * slab_mutex |
881db7fb | 58 | * |
18004c5d | 59 | * The role of the slab_mutex is to protect the list of all the slabs |
881db7fb | 60 | * and to synchronize major metadata changes to slab cache structures. |
bd0e7491 VB |
61 | * Also synchronizes memory hotplug callbacks. |
62 | * | |
63 | * slab_lock | |
64 | * | |
65 | * The slab_lock is a wrapper around the page lock, thus it is a bit | |
66 | * spinlock. | |
881db7fb | 67 | * |
41bec7c3 VB |
68 | * The slab_lock is only used on arches that do not have the ability |
69 | * to do a cmpxchg_double. It only protects: | |
70 | * | |
c2092c12 VB |
71 | * A. slab->freelist -> List of free objects in a slab |
72 | * B. slab->inuse -> Number of objects in use | |
73 | * C. slab->objects -> Number of objects in slab | |
74 | * D. slab->frozen -> frozen state | |
881db7fb | 75 | * |
bd0e7491 VB |
76 | * Frozen slabs |
77 | * | |
881db7fb | 78 | * If a slab is frozen then it is exempt from list management. It is not |
632b2ef0 | 79 | * on any list except per cpu partial list. The processor that froze the |
c2092c12 | 80 | * slab is the one who can perform list operations on the slab. Other |
632b2ef0 LX |
81 | * processors may put objects onto the freelist but the processor that |
82 | * froze the slab is the only one that can retrieve the objects from the | |
c2092c12 | 83 | * slab's freelist. |
81819f0f | 84 | * |
bd0e7491 VB |
85 | * list_lock |
86 | * | |
81819f0f CL |
87 | * The list_lock protects the partial and full list on each node and |
88 | * the partial slab counter. If taken then no new slabs may be added or | |
89 | * removed from the lists nor make the number of partial slabs be modified. | |
90 | * (Note that the total number of slabs is an atomic value that may be | |
91 | * modified without taking the list lock). | |
92 | * | |
93 | * The list_lock is a centralized lock and thus we avoid taking it as | |
94 | * much as possible. As long as SLUB does not have to handle partial | |
95 | * slabs, operations can continue without any centralized lock. F.e. | |
96 | * allocating a long series of objects that fill up slabs does not require | |
97 | * the list lock. | |
bd0e7491 | 98 | * |
41bec7c3 VB |
99 | * For debug caches, all allocations are forced to go through a list_lock |
100 | * protected region to serialize against concurrent validation. | |
101 | * | |
bd0e7491 VB |
102 | * cpu_slab->lock local lock |
103 | * | |
104 | * This locks protect slowpath manipulation of all kmem_cache_cpu fields | |
105 | * except the stat counters. This is a percpu structure manipulated only by | |
106 | * the local cpu, so the lock protects against being preempted or interrupted | |
107 | * by an irq. Fast path operations rely on lockless operations instead. | |
1f04b07d TG |
108 | * |
109 | * On PREEMPT_RT, the local lock neither disables interrupts nor preemption | |
110 | * which means the lockless fastpath cannot be used as it might interfere with | |
111 | * an in-progress slow path operations. In this case the local lock is always | |
112 | * taken but it still utilizes the freelist for the common operations. | |
bd0e7491 VB |
113 | * |
114 | * lockless fastpaths | |
115 | * | |
116 | * The fast path allocation (slab_alloc_node()) and freeing (do_slab_free()) | |
117 | * are fully lockless when satisfied from the percpu slab (and when | |
118 | * cmpxchg_double is possible to use, otherwise slab_lock is taken). | |
119 | * They also don't disable preemption or migration or irqs. They rely on | |
120 | * the transaction id (tid) field to detect being preempted or moved to | |
121 | * another cpu. | |
122 | * | |
123 | * irq, preemption, migration considerations | |
124 | * | |
125 | * Interrupts are disabled as part of list_lock or local_lock operations, or | |
126 | * around the slab_lock operation, in order to make the slab allocator safe | |
127 | * to use in the context of an irq. | |
128 | * | |
129 | * In addition, preemption (or migration on PREEMPT_RT) is disabled in the | |
130 | * allocation slowpath, bulk allocation, and put_cpu_partial(), so that the | |
131 | * local cpu doesn't change in the process and e.g. the kmem_cache_cpu pointer | |
132 | * doesn't have to be revalidated in each section protected by the local lock. | |
81819f0f CL |
133 | * |
134 | * SLUB assigns one slab for allocation to each processor. | |
135 | * Allocations only occur from these slabs called cpu slabs. | |
136 | * | |
672bba3a CL |
137 | * Slabs with free elements are kept on a partial list and during regular |
138 | * operations no list for full slabs is used. If an object in a full slab is | |
81819f0f | 139 | * freed then the slab will show up again on the partial lists. |
672bba3a CL |
140 | * We track full slabs for debugging purposes though because otherwise we |
141 | * cannot scan all objects. | |
81819f0f CL |
142 | * |
143 | * Slabs are freed when they become empty. Teardown and setup is | |
144 | * minimal so we rely on the page allocators per cpu caches for | |
145 | * fast frees and allocs. | |
146 | * | |
c2092c12 | 147 | * slab->frozen The slab is frozen and exempt from list processing. |
4b6f0750 CL |
148 | * This means that the slab is dedicated to a purpose |
149 | * such as satisfying allocations for a specific | |
150 | * processor. Objects may be freed in the slab while | |
151 | * it is frozen but slab_free will then skip the usual | |
152 | * list operations. It is up to the processor holding | |
153 | * the slab to integrate the slab into the slab lists | |
154 | * when the slab is no longer needed. | |
155 | * | |
156 | * One use of this flag is to mark slabs that are | |
157 | * used for allocations. Then such a slab becomes a cpu | |
158 | * slab. The cpu slab may be equipped with an additional | |
dfb4f096 | 159 | * freelist that allows lockless access to |
894b8788 CL |
160 | * free objects in addition to the regular freelist |
161 | * that requires the slab lock. | |
81819f0f | 162 | * |
aed68148 | 163 | * SLAB_DEBUG_FLAGS Slab requires special handling due to debug |
81819f0f | 164 | * options set. This moves slab handling out of |
894b8788 | 165 | * the fast path and disables lockless freelists. |
81819f0f CL |
166 | */ |
167 | ||
25c00c50 VB |
168 | /* |
169 | * We could simply use migrate_disable()/enable() but as long as it's a | |
170 | * function call even on !PREEMPT_RT, use inline preempt_disable() there. | |
171 | */ | |
172 | #ifndef CONFIG_PREEMPT_RT | |
1f04b07d TG |
173 | #define slub_get_cpu_ptr(var) get_cpu_ptr(var) |
174 | #define slub_put_cpu_ptr(var) put_cpu_ptr(var) | |
175 | #define USE_LOCKLESS_FAST_PATH() (true) | |
25c00c50 VB |
176 | #else |
177 | #define slub_get_cpu_ptr(var) \ | |
178 | ({ \ | |
179 | migrate_disable(); \ | |
180 | this_cpu_ptr(var); \ | |
181 | }) | |
182 | #define slub_put_cpu_ptr(var) \ | |
183 | do { \ | |
184 | (void)(var); \ | |
185 | migrate_enable(); \ | |
186 | } while (0) | |
1f04b07d | 187 | #define USE_LOCKLESS_FAST_PATH() (false) |
25c00c50 VB |
188 | #endif |
189 | ||
ca0cab65 VB |
190 | #ifdef CONFIG_SLUB_DEBUG |
191 | #ifdef CONFIG_SLUB_DEBUG_ON | |
192 | DEFINE_STATIC_KEY_TRUE(slub_debug_enabled); | |
193 | #else | |
194 | DEFINE_STATIC_KEY_FALSE(slub_debug_enabled); | |
195 | #endif | |
79270291 | 196 | #endif /* CONFIG_SLUB_DEBUG */ |
ca0cab65 | 197 | |
6edf2576 FT |
198 | /* Structure holding parameters for get_partial() call chain */ |
199 | struct partial_context { | |
200 | struct slab **slab; | |
201 | gfp_t flags; | |
202 | unsigned int orig_size; | |
203 | }; | |
204 | ||
59052e89 VB |
205 | static inline bool kmem_cache_debug(struct kmem_cache *s) |
206 | { | |
207 | return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS); | |
af537b0a | 208 | } |
5577bd8a | 209 | |
6edf2576 FT |
210 | static inline bool slub_debug_orig_size(struct kmem_cache *s) |
211 | { | |
212 | return (kmem_cache_debug_flags(s, SLAB_STORE_USER) && | |
213 | (s->flags & SLAB_KMALLOC)); | |
214 | } | |
215 | ||
117d54df | 216 | void *fixup_red_left(struct kmem_cache *s, void *p) |
d86bd1be | 217 | { |
59052e89 | 218 | if (kmem_cache_debug_flags(s, SLAB_RED_ZONE)) |
d86bd1be JK |
219 | p += s->red_left_pad; |
220 | ||
221 | return p; | |
222 | } | |
223 | ||
345c905d JK |
224 | static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s) |
225 | { | |
226 | #ifdef CONFIG_SLUB_CPU_PARTIAL | |
227 | return !kmem_cache_debug(s); | |
228 | #else | |
229 | return false; | |
230 | #endif | |
231 | } | |
232 | ||
81819f0f CL |
233 | /* |
234 | * Issues still to be resolved: | |
235 | * | |
81819f0f CL |
236 | * - Support PAGE_ALLOC_DEBUG. Should be easy to do. |
237 | * | |
81819f0f CL |
238 | * - Variable sizing of the per node arrays |
239 | */ | |
240 | ||
b789ef51 CL |
241 | /* Enable to log cmpxchg failures */ |
242 | #undef SLUB_DEBUG_CMPXCHG | |
243 | ||
5a8a3c1f | 244 | #ifndef CONFIG_SLUB_TINY |
2086d26a | 245 | /* |
dc84207d | 246 | * Minimum number of partial slabs. These will be left on the partial |
2086d26a CL |
247 | * lists even if they are empty. kmem_cache_shrink may reclaim them. |
248 | */ | |
76be8950 | 249 | #define MIN_PARTIAL 5 |
e95eed57 | 250 | |
2086d26a CL |
251 | /* |
252 | * Maximum number of desirable partial slabs. | |
253 | * The existence of more partial slabs makes kmem_cache_shrink | |
721ae22a | 254 | * sort the partial list by the number of objects in use. |
2086d26a CL |
255 | */ |
256 | #define MAX_PARTIAL 10 | |
5a8a3c1f VB |
257 | #else |
258 | #define MIN_PARTIAL 0 | |
259 | #define MAX_PARTIAL 0 | |
260 | #endif | |
2086d26a | 261 | |
becfda68 | 262 | #define DEBUG_DEFAULT_FLAGS (SLAB_CONSISTENCY_CHECKS | SLAB_RED_ZONE | \ |
81819f0f | 263 | SLAB_POISON | SLAB_STORE_USER) |
672bba3a | 264 | |
149daaf3 LA |
265 | /* |
266 | * These debug flags cannot use CMPXCHG because there might be consistency | |
267 | * issues when checking or reading debug information | |
268 | */ | |
269 | #define SLAB_NO_CMPXCHG (SLAB_CONSISTENCY_CHECKS | SLAB_STORE_USER | \ | |
270 | SLAB_TRACE) | |
271 | ||
272 | ||
fa5ec8a1 | 273 | /* |
3de47213 DR |
274 | * Debugging flags that require metadata to be stored in the slab. These get |
275 | * disabled when slub_debug=O is used and a cache's min order increases with | |
276 | * metadata. | |
fa5ec8a1 | 277 | */ |
3de47213 | 278 | #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER) |
fa5ec8a1 | 279 | |
210b5c06 CG |
280 | #define OO_SHIFT 16 |
281 | #define OO_MASK ((1 << OO_SHIFT) - 1) | |
c2092c12 | 282 | #define MAX_OBJS_PER_PAGE 32767 /* since slab.objects is u15 */ |
210b5c06 | 283 | |
81819f0f | 284 | /* Internal SLUB flags */ |
d50112ed | 285 | /* Poison object */ |
4fd0b46e | 286 | #define __OBJECT_POISON ((slab_flags_t __force)0x80000000U) |
d50112ed | 287 | /* Use cmpxchg_double */ |
4fd0b46e | 288 | #define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U) |
81819f0f | 289 | |
02cbc874 CL |
290 | /* |
291 | * Tracking user of a slab. | |
292 | */ | |
d6543e39 | 293 | #define TRACK_ADDRS_COUNT 16 |
02cbc874 | 294 | struct track { |
ce71e27c | 295 | unsigned long addr; /* Called from address */ |
5cf909c5 OG |
296 | #ifdef CONFIG_STACKDEPOT |
297 | depot_stack_handle_t handle; | |
d6543e39 | 298 | #endif |
02cbc874 CL |
299 | int cpu; /* Was running on cpu */ |
300 | int pid; /* Pid context */ | |
301 | unsigned long when; /* When did the operation occur */ | |
302 | }; | |
303 | ||
304 | enum track_item { TRACK_ALLOC, TRACK_FREE }; | |
305 | ||
b1a413a3 | 306 | #ifdef SLAB_SUPPORTS_SYSFS |
81819f0f CL |
307 | static int sysfs_slab_add(struct kmem_cache *); |
308 | static int sysfs_slab_alias(struct kmem_cache *, const char *); | |
81819f0f | 309 | #else |
0c710013 CL |
310 | static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; } |
311 | static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p) | |
312 | { return 0; } | |
81819f0f CL |
313 | #endif |
314 | ||
64dd6849 FM |
315 | #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG) |
316 | static void debugfs_slab_add(struct kmem_cache *); | |
317 | #else | |
318 | static inline void debugfs_slab_add(struct kmem_cache *s) { } | |
319 | #endif | |
320 | ||
4fdccdfb | 321 | static inline void stat(const struct kmem_cache *s, enum stat_item si) |
8ff12cfc CL |
322 | { |
323 | #ifdef CONFIG_SLUB_STATS | |
88da03a6 CL |
324 | /* |
325 | * The rmw is racy on a preemptible kernel but this is acceptable, so | |
326 | * avoid this_cpu_add()'s irq-disable overhead. | |
327 | */ | |
328 | raw_cpu_inc(s->cpu_slab->stat[si]); | |
8ff12cfc CL |
329 | #endif |
330 | } | |
331 | ||
7e1fa93d VB |
332 | /* |
333 | * Tracks for which NUMA nodes we have kmem_cache_nodes allocated. | |
334 | * Corresponds to node_state[N_NORMAL_MEMORY], but can temporarily | |
335 | * differ during memory hotplug/hotremove operations. | |
336 | * Protected by slab_mutex. | |
337 | */ | |
338 | static nodemask_t slab_nodes; | |
339 | ||
e45cc288 ML |
340 | /* |
341 | * Workqueue used for flush_cpu_slab(). | |
342 | */ | |
343 | static struct workqueue_struct *flushwq; | |
344 | ||
81819f0f CL |
345 | /******************************************************************** |
346 | * Core slab cache functions | |
347 | *******************************************************************/ | |
348 | ||
2482ddec KC |
349 | /* |
350 | * Returns freelist pointer (ptr). With hardening, this is obfuscated | |
351 | * with an XOR of the address where the pointer is held and a per-cache | |
352 | * random number. | |
353 | */ | |
354 | static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr, | |
355 | unsigned long ptr_addr) | |
356 | { | |
357 | #ifdef CONFIG_SLAB_FREELIST_HARDENED | |
d36a63a9 | 358 | /* |
aa1ef4d7 | 359 | * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged. |
d36a63a9 AK |
360 | * Normally, this doesn't cause any issues, as both set_freepointer() |
361 | * and get_freepointer() are called with a pointer with the same tag. | |
362 | * However, there are some issues with CONFIG_SLUB_DEBUG code. For | |
363 | * example, when __free_slub() iterates over objects in a cache, it | |
364 | * passes untagged pointers to check_object(). check_object() in turns | |
365 | * calls get_freepointer() with an untagged pointer, which causes the | |
366 | * freepointer to be restored incorrectly. | |
367 | */ | |
368 | return (void *)((unsigned long)ptr ^ s->random ^ | |
1ad53d9f | 369 | swab((unsigned long)kasan_reset_tag((void *)ptr_addr))); |
2482ddec KC |
370 | #else |
371 | return ptr; | |
372 | #endif | |
373 | } | |
374 | ||
375 | /* Returns the freelist pointer recorded at location ptr_addr. */ | |
376 | static inline void *freelist_dereference(const struct kmem_cache *s, | |
377 | void *ptr_addr) | |
378 | { | |
379 | return freelist_ptr(s, (void *)*(unsigned long *)(ptr_addr), | |
380 | (unsigned long)ptr_addr); | |
381 | } | |
382 | ||
7656c72b CL |
383 | static inline void *get_freepointer(struct kmem_cache *s, void *object) |
384 | { | |
aa1ef4d7 | 385 | object = kasan_reset_tag(object); |
2482ddec | 386 | return freelist_dereference(s, object + s->offset); |
7656c72b CL |
387 | } |
388 | ||
0ad9500e ED |
389 | static void prefetch_freepointer(const struct kmem_cache *s, void *object) |
390 | { | |
04b4b006 | 391 | prefetchw(object + s->offset); |
0ad9500e ED |
392 | } |
393 | ||
68ef169a AP |
394 | /* |
395 | * When running under KMSAN, get_freepointer_safe() may return an uninitialized | |
396 | * pointer value in the case the current thread loses the race for the next | |
397 | * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in | |
398 | * slab_alloc_node() will fail, so the uninitialized value won't be used, but | |
399 | * KMSAN will still check all arguments of cmpxchg because of imperfect | |
400 | * handling of inline assembly. | |
401 | * To work around this problem, we apply __no_kmsan_checks to ensure that | |
402 | * get_freepointer_safe() returns initialized memory. | |
403 | */ | |
404 | __no_kmsan_checks | |
1393d9a1 CL |
405 | static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) |
406 | { | |
2482ddec | 407 | unsigned long freepointer_addr; |
1393d9a1 CL |
408 | void *p; |
409 | ||
8e57f8ac | 410 | if (!debug_pagealloc_enabled_static()) |
922d566c JK |
411 | return get_freepointer(s, object); |
412 | ||
f70b0049 | 413 | object = kasan_reset_tag(object); |
2482ddec | 414 | freepointer_addr = (unsigned long)object + s->offset; |
fe557319 | 415 | copy_from_kernel_nofault(&p, (void **)freepointer_addr, sizeof(p)); |
2482ddec | 416 | return freelist_ptr(s, p, freepointer_addr); |
1393d9a1 CL |
417 | } |
418 | ||
7656c72b CL |
419 | static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) |
420 | { | |
2482ddec KC |
421 | unsigned long freeptr_addr = (unsigned long)object + s->offset; |
422 | ||
ce6fa91b AP |
423 | #ifdef CONFIG_SLAB_FREELIST_HARDENED |
424 | BUG_ON(object == fp); /* naive detection of double free or corruption */ | |
425 | #endif | |
426 | ||
aa1ef4d7 | 427 | freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr); |
2482ddec | 428 | *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr); |
7656c72b CL |
429 | } |
430 | ||
431 | /* Loop over all objects in a slab */ | |
224a88be | 432 | #define for_each_object(__p, __s, __addr, __objects) \ |
d86bd1be JK |
433 | for (__p = fixup_red_left(__s, __addr); \ |
434 | __p < (__addr) + (__objects) * (__s)->size; \ | |
435 | __p += (__s)->size) | |
7656c72b | 436 | |
9736d2a9 | 437 | static inline unsigned int order_objects(unsigned int order, unsigned int size) |
ab9a0f19 | 438 | { |
9736d2a9 | 439 | return ((unsigned int)PAGE_SIZE << order) / size; |
ab9a0f19 LJ |
440 | } |
441 | ||
19af27af | 442 | static inline struct kmem_cache_order_objects oo_make(unsigned int order, |
9736d2a9 | 443 | unsigned int size) |
834f3d11 CL |
444 | { |
445 | struct kmem_cache_order_objects x = { | |
9736d2a9 | 446 | (order << OO_SHIFT) + order_objects(order, size) |
834f3d11 CL |
447 | }; |
448 | ||
449 | return x; | |
450 | } | |
451 | ||
19af27af | 452 | static inline unsigned int oo_order(struct kmem_cache_order_objects x) |
834f3d11 | 453 | { |
210b5c06 | 454 | return x.x >> OO_SHIFT; |
834f3d11 CL |
455 | } |
456 | ||
19af27af | 457 | static inline unsigned int oo_objects(struct kmem_cache_order_objects x) |
834f3d11 | 458 | { |
210b5c06 | 459 | return x.x & OO_MASK; |
834f3d11 CL |
460 | } |
461 | ||
b47291ef VB |
462 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
463 | static void slub_set_cpu_partial(struct kmem_cache *s, unsigned int nr_objects) | |
464 | { | |
bb192ed9 | 465 | unsigned int nr_slabs; |
b47291ef VB |
466 | |
467 | s->cpu_partial = nr_objects; | |
468 | ||
469 | /* | |
470 | * We take the number of objects but actually limit the number of | |
c2092c12 VB |
471 | * slabs on the per cpu partial list, in order to limit excessive |
472 | * growth of the list. For simplicity we assume that the slabs will | |
b47291ef VB |
473 | * be half-full. |
474 | */ | |
bb192ed9 VB |
475 | nr_slabs = DIV_ROUND_UP(nr_objects * 2, oo_objects(s->oo)); |
476 | s->cpu_partial_slabs = nr_slabs; | |
b47291ef VB |
477 | } |
478 | #else | |
479 | static inline void | |
480 | slub_set_cpu_partial(struct kmem_cache *s, unsigned int nr_objects) | |
481 | { | |
482 | } | |
483 | #endif /* CONFIG_SLUB_CPU_PARTIAL */ | |
484 | ||
881db7fb CL |
485 | /* |
486 | * Per slab locking using the pagelock | |
487 | */ | |
5875e598 | 488 | static __always_inline void slab_lock(struct slab *slab) |
881db7fb | 489 | { |
0393895b VB |
490 | struct page *page = slab_page(slab); |
491 | ||
48c935ad | 492 | VM_BUG_ON_PAGE(PageTail(page), page); |
881db7fb CL |
493 | bit_spin_lock(PG_locked, &page->flags); |
494 | } | |
495 | ||
5875e598 | 496 | static __always_inline void slab_unlock(struct slab *slab) |
881db7fb | 497 | { |
0393895b VB |
498 | struct page *page = slab_page(slab); |
499 | ||
48c935ad | 500 | VM_BUG_ON_PAGE(PageTail(page), page); |
881db7fb CL |
501 | __bit_spin_unlock(PG_locked, &page->flags); |
502 | } | |
503 | ||
a2b4ae8b VB |
504 | /* |
505 | * Interrupts must be disabled (for the fallback code to work right), typically | |
5875e598 VB |
506 | * by an _irqsave() lock variant. On PREEMPT_RT the preempt_disable(), which is |
507 | * part of bit_spin_lock(), is sufficient because the policy is not to allow any | |
508 | * allocation/ free operation in hardirq context. Therefore nothing can | |
509 | * interrupt the operation. | |
a2b4ae8b | 510 | */ |
bb192ed9 | 511 | static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab, |
1d07171c CL |
512 | void *freelist_old, unsigned long counters_old, |
513 | void *freelist_new, unsigned long counters_new, | |
514 | const char *n) | |
515 | { | |
1f04b07d | 516 | if (USE_LOCKLESS_FAST_PATH()) |
a2b4ae8b | 517 | lockdep_assert_irqs_disabled(); |
2565409f HC |
518 | #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ |
519 | defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) | |
1d07171c | 520 | if (s->flags & __CMPXCHG_DOUBLE) { |
bb192ed9 | 521 | if (cmpxchg_double(&slab->freelist, &slab->counters, |
0aa9a13d DC |
522 | freelist_old, counters_old, |
523 | freelist_new, counters_new)) | |
6f6528a1 | 524 | return true; |
1d07171c CL |
525 | } else |
526 | #endif | |
527 | { | |
5875e598 | 528 | slab_lock(slab); |
bb192ed9 VB |
529 | if (slab->freelist == freelist_old && |
530 | slab->counters == counters_old) { | |
531 | slab->freelist = freelist_new; | |
532 | slab->counters = counters_new; | |
5875e598 | 533 | slab_unlock(slab); |
6f6528a1 | 534 | return true; |
1d07171c | 535 | } |
5875e598 | 536 | slab_unlock(slab); |
1d07171c CL |
537 | } |
538 | ||
539 | cpu_relax(); | |
540 | stat(s, CMPXCHG_DOUBLE_FAIL); | |
541 | ||
542 | #ifdef SLUB_DEBUG_CMPXCHG | |
f9f58285 | 543 | pr_info("%s %s: cmpxchg double redo ", n, s->name); |
1d07171c CL |
544 | #endif |
545 | ||
6f6528a1 | 546 | return false; |
1d07171c CL |
547 | } |
548 | ||
bb192ed9 | 549 | static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab, |
b789ef51 CL |
550 | void *freelist_old, unsigned long counters_old, |
551 | void *freelist_new, unsigned long counters_new, | |
552 | const char *n) | |
553 | { | |
2565409f HC |
554 | #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ |
555 | defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) | |
b789ef51 | 556 | if (s->flags & __CMPXCHG_DOUBLE) { |
bb192ed9 | 557 | if (cmpxchg_double(&slab->freelist, &slab->counters, |
0aa9a13d DC |
558 | freelist_old, counters_old, |
559 | freelist_new, counters_new)) | |
6f6528a1 | 560 | return true; |
b789ef51 CL |
561 | } else |
562 | #endif | |
563 | { | |
1d07171c CL |
564 | unsigned long flags; |
565 | ||
566 | local_irq_save(flags); | |
5875e598 | 567 | slab_lock(slab); |
bb192ed9 VB |
568 | if (slab->freelist == freelist_old && |
569 | slab->counters == counters_old) { | |
570 | slab->freelist = freelist_new; | |
571 | slab->counters = counters_new; | |
5875e598 | 572 | slab_unlock(slab); |
1d07171c | 573 | local_irq_restore(flags); |
6f6528a1 | 574 | return true; |
b789ef51 | 575 | } |
5875e598 | 576 | slab_unlock(slab); |
1d07171c | 577 | local_irq_restore(flags); |
b789ef51 CL |
578 | } |
579 | ||
580 | cpu_relax(); | |
581 | stat(s, CMPXCHG_DOUBLE_FAIL); | |
582 | ||
583 | #ifdef SLUB_DEBUG_CMPXCHG | |
f9f58285 | 584 | pr_info("%s %s: cmpxchg double redo ", n, s->name); |
b789ef51 CL |
585 | #endif |
586 | ||
6f6528a1 | 587 | return false; |
b789ef51 CL |
588 | } |
589 | ||
41ecc55b | 590 | #ifdef CONFIG_SLUB_DEBUG |
90e9f6a6 | 591 | static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)]; |
4ef3f5a3 | 592 | static DEFINE_SPINLOCK(object_map_lock); |
90e9f6a6 | 593 | |
b3fd64e1 | 594 | static void __fill_map(unsigned long *obj_map, struct kmem_cache *s, |
bb192ed9 | 595 | struct slab *slab) |
b3fd64e1 | 596 | { |
bb192ed9 | 597 | void *addr = slab_address(slab); |
b3fd64e1 VB |
598 | void *p; |
599 | ||
bb192ed9 | 600 | bitmap_zero(obj_map, slab->objects); |
b3fd64e1 | 601 | |
bb192ed9 | 602 | for (p = slab->freelist; p; p = get_freepointer(s, p)) |
b3fd64e1 VB |
603 | set_bit(__obj_to_index(s, addr, p), obj_map); |
604 | } | |
605 | ||
1f9f78b1 OG |
606 | #if IS_ENABLED(CONFIG_KUNIT) |
607 | static bool slab_add_kunit_errors(void) | |
608 | { | |
609 | struct kunit_resource *resource; | |
610 | ||
611 | if (likely(!current->kunit_test)) | |
612 | return false; | |
613 | ||
614 | resource = kunit_find_named_resource(current->kunit_test, "slab_errors"); | |
615 | if (!resource) | |
616 | return false; | |
617 | ||
618 | (*(int *)resource->data)++; | |
619 | kunit_put_resource(resource); | |
620 | return true; | |
621 | } | |
622 | #else | |
623 | static inline bool slab_add_kunit_errors(void) { return false; } | |
624 | #endif | |
625 | ||
870b1fbb | 626 | static inline unsigned int size_from_object(struct kmem_cache *s) |
d86bd1be JK |
627 | { |
628 | if (s->flags & SLAB_RED_ZONE) | |
629 | return s->size - s->red_left_pad; | |
630 | ||
631 | return s->size; | |
632 | } | |
633 | ||
634 | static inline void *restore_red_left(struct kmem_cache *s, void *p) | |
635 | { | |
636 | if (s->flags & SLAB_RED_ZONE) | |
637 | p -= s->red_left_pad; | |
638 | ||
639 | return p; | |
640 | } | |
641 | ||
41ecc55b CL |
642 | /* |
643 | * Debug settings: | |
644 | */ | |
89d3c87e | 645 | #if defined(CONFIG_SLUB_DEBUG_ON) |
d50112ed | 646 | static slab_flags_t slub_debug = DEBUG_DEFAULT_FLAGS; |
f0630fff | 647 | #else |
d50112ed | 648 | static slab_flags_t slub_debug; |
f0630fff | 649 | #endif |
41ecc55b | 650 | |
e17f1dfb | 651 | static char *slub_debug_string; |
fa5ec8a1 | 652 | static int disable_higher_order_debug; |
41ecc55b | 653 | |
a79316c6 AR |
654 | /* |
655 | * slub is about to manipulate internal object metadata. This memory lies | |
656 | * outside the range of the allocated object, so accessing it would normally | |
657 | * be reported by kasan as a bounds error. metadata_access_enable() is used | |
658 | * to tell kasan that these accesses are OK. | |
659 | */ | |
660 | static inline void metadata_access_enable(void) | |
661 | { | |
662 | kasan_disable_current(); | |
663 | } | |
664 | ||
665 | static inline void metadata_access_disable(void) | |
666 | { | |
667 | kasan_enable_current(); | |
668 | } | |
669 | ||
81819f0f CL |
670 | /* |
671 | * Object debugging | |
672 | */ | |
d86bd1be JK |
673 | |
674 | /* Verify that a pointer has an address that is valid within a slab page */ | |
675 | static inline int check_valid_pointer(struct kmem_cache *s, | |
bb192ed9 | 676 | struct slab *slab, void *object) |
d86bd1be JK |
677 | { |
678 | void *base; | |
679 | ||
680 | if (!object) | |
681 | return 1; | |
682 | ||
bb192ed9 | 683 | base = slab_address(slab); |
338cfaad | 684 | object = kasan_reset_tag(object); |
d86bd1be | 685 | object = restore_red_left(s, object); |
bb192ed9 | 686 | if (object < base || object >= base + slab->objects * s->size || |
d86bd1be JK |
687 | (object - base) % s->size) { |
688 | return 0; | |
689 | } | |
690 | ||
691 | return 1; | |
692 | } | |
693 | ||
aa2efd5e DT |
694 | static void print_section(char *level, char *text, u8 *addr, |
695 | unsigned int length) | |
81819f0f | 696 | { |
a79316c6 | 697 | metadata_access_enable(); |
340caf17 KYL |
698 | print_hex_dump(level, text, DUMP_PREFIX_ADDRESS, |
699 | 16, 1, kasan_reset_tag((void *)addr), length, 1); | |
a79316c6 | 700 | metadata_access_disable(); |
81819f0f CL |
701 | } |
702 | ||
cbfc35a4 WL |
703 | /* |
704 | * See comment in calculate_sizes(). | |
705 | */ | |
706 | static inline bool freeptr_outside_object(struct kmem_cache *s) | |
707 | { | |
708 | return s->offset >= s->inuse; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Return offset of the end of info block which is inuse + free pointer if | |
713 | * not overlapping with object. | |
714 | */ | |
715 | static inline unsigned int get_info_end(struct kmem_cache *s) | |
716 | { | |
717 | if (freeptr_outside_object(s)) | |
718 | return s->inuse + sizeof(void *); | |
719 | else | |
720 | return s->inuse; | |
721 | } | |
722 | ||
81819f0f CL |
723 | static struct track *get_track(struct kmem_cache *s, void *object, |
724 | enum track_item alloc) | |
725 | { | |
726 | struct track *p; | |
727 | ||
cbfc35a4 | 728 | p = object + get_info_end(s); |
81819f0f | 729 | |
aa1ef4d7 | 730 | return kasan_reset_tag(p + alloc); |
81819f0f CL |
731 | } |
732 | ||
5cf909c5 | 733 | #ifdef CONFIG_STACKDEPOT |
c4cf6785 SAS |
734 | static noinline depot_stack_handle_t set_track_prepare(void) |
735 | { | |
736 | depot_stack_handle_t handle; | |
5cf909c5 | 737 | unsigned long entries[TRACK_ADDRS_COUNT]; |
0cd1a029 | 738 | unsigned int nr_entries; |
ae14c63a | 739 | |
5cf909c5 | 740 | nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3); |
c4cf6785 SAS |
741 | handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT); |
742 | ||
743 | return handle; | |
744 | } | |
745 | #else | |
746 | static inline depot_stack_handle_t set_track_prepare(void) | |
747 | { | |
748 | return 0; | |
749 | } | |
d6543e39 | 750 | #endif |
5cf909c5 | 751 | |
c4cf6785 SAS |
752 | static void set_track_update(struct kmem_cache *s, void *object, |
753 | enum track_item alloc, unsigned long addr, | |
754 | depot_stack_handle_t handle) | |
755 | { | |
756 | struct track *p = get_track(s, object, alloc); | |
757 | ||
758 | #ifdef CONFIG_STACKDEPOT | |
759 | p->handle = handle; | |
760 | #endif | |
0cd1a029 VB |
761 | p->addr = addr; |
762 | p->cpu = smp_processor_id(); | |
763 | p->pid = current->pid; | |
764 | p->when = jiffies; | |
81819f0f CL |
765 | } |
766 | ||
c4cf6785 SAS |
767 | static __always_inline void set_track(struct kmem_cache *s, void *object, |
768 | enum track_item alloc, unsigned long addr) | |
769 | { | |
770 | depot_stack_handle_t handle = set_track_prepare(); | |
771 | ||
772 | set_track_update(s, object, alloc, addr, handle); | |
773 | } | |
774 | ||
81819f0f CL |
775 | static void init_tracking(struct kmem_cache *s, void *object) |
776 | { | |
0cd1a029 VB |
777 | struct track *p; |
778 | ||
24922684 CL |
779 | if (!(s->flags & SLAB_STORE_USER)) |
780 | return; | |
781 | ||
0cd1a029 VB |
782 | p = get_track(s, object, TRACK_ALLOC); |
783 | memset(p, 0, 2*sizeof(struct track)); | |
81819f0f CL |
784 | } |
785 | ||
86609d33 | 786 | static void print_track(const char *s, struct track *t, unsigned long pr_time) |
81819f0f | 787 | { |
5cf909c5 OG |
788 | depot_stack_handle_t handle __maybe_unused; |
789 | ||
81819f0f CL |
790 | if (!t->addr) |
791 | return; | |
792 | ||
96b94abc | 793 | pr_err("%s in %pS age=%lu cpu=%u pid=%d\n", |
86609d33 | 794 | s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid); |
5cf909c5 OG |
795 | #ifdef CONFIG_STACKDEPOT |
796 | handle = READ_ONCE(t->handle); | |
797 | if (handle) | |
798 | stack_depot_print(handle); | |
799 | else | |
800 | pr_err("object allocation/free stack trace missing\n"); | |
d6543e39 | 801 | #endif |
24922684 CL |
802 | } |
803 | ||
e42f174e | 804 | void print_tracking(struct kmem_cache *s, void *object) |
24922684 | 805 | { |
86609d33 | 806 | unsigned long pr_time = jiffies; |
24922684 CL |
807 | if (!(s->flags & SLAB_STORE_USER)) |
808 | return; | |
809 | ||
86609d33 CP |
810 | print_track("Allocated", get_track(s, object, TRACK_ALLOC), pr_time); |
811 | print_track("Freed", get_track(s, object, TRACK_FREE), pr_time); | |
24922684 CL |
812 | } |
813 | ||
fb012e27 | 814 | static void print_slab_info(const struct slab *slab) |
24922684 | 815 | { |
fb012e27 | 816 | struct folio *folio = (struct folio *)slab_folio(slab); |
24922684 | 817 | |
fb012e27 MWO |
818 | pr_err("Slab 0x%p objects=%u used=%u fp=0x%p flags=%pGp\n", |
819 | slab, slab->objects, slab->inuse, slab->freelist, | |
820 | folio_flags(folio, 0)); | |
24922684 CL |
821 | } |
822 | ||
6edf2576 FT |
823 | /* |
824 | * kmalloc caches has fixed sizes (mostly power of 2), and kmalloc() API | |
825 | * family will round up the real request size to these fixed ones, so | |
826 | * there could be an extra area than what is requested. Save the original | |
827 | * request size in the meta data area, for better debug and sanity check. | |
828 | */ | |
829 | static inline void set_orig_size(struct kmem_cache *s, | |
830 | void *object, unsigned int orig_size) | |
831 | { | |
832 | void *p = kasan_reset_tag(object); | |
833 | ||
834 | if (!slub_debug_orig_size(s)) | |
835 | return; | |
836 | ||
837 | p += get_info_end(s); | |
838 | p += sizeof(struct track) * 2; | |
839 | ||
840 | *(unsigned int *)p = orig_size; | |
841 | } | |
842 | ||
843 | static inline unsigned int get_orig_size(struct kmem_cache *s, void *object) | |
844 | { | |
845 | void *p = kasan_reset_tag(object); | |
846 | ||
847 | if (!slub_debug_orig_size(s)) | |
848 | return s->object_size; | |
849 | ||
850 | p += get_info_end(s); | |
851 | p += sizeof(struct track) * 2; | |
852 | ||
853 | return *(unsigned int *)p; | |
854 | } | |
855 | ||
24922684 CL |
856 | static void slab_bug(struct kmem_cache *s, char *fmt, ...) |
857 | { | |
ecc42fbe | 858 | struct va_format vaf; |
24922684 | 859 | va_list args; |
24922684 CL |
860 | |
861 | va_start(args, fmt); | |
ecc42fbe FF |
862 | vaf.fmt = fmt; |
863 | vaf.va = &args; | |
f9f58285 | 864 | pr_err("=============================================================================\n"); |
ecc42fbe | 865 | pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf); |
f9f58285 | 866 | pr_err("-----------------------------------------------------------------------------\n\n"); |
ecc42fbe | 867 | va_end(args); |
81819f0f CL |
868 | } |
869 | ||
582d1212 | 870 | __printf(2, 3) |
24922684 CL |
871 | static void slab_fix(struct kmem_cache *s, char *fmt, ...) |
872 | { | |
ecc42fbe | 873 | struct va_format vaf; |
24922684 | 874 | va_list args; |
24922684 | 875 | |
1f9f78b1 OG |
876 | if (slab_add_kunit_errors()) |
877 | return; | |
878 | ||
24922684 | 879 | va_start(args, fmt); |
ecc42fbe FF |
880 | vaf.fmt = fmt; |
881 | vaf.va = &args; | |
882 | pr_err("FIX %s: %pV\n", s->name, &vaf); | |
24922684 | 883 | va_end(args); |
24922684 CL |
884 | } |
885 | ||
bb192ed9 | 886 | static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p) |
81819f0f CL |
887 | { |
888 | unsigned int off; /* Offset of last byte */ | |
bb192ed9 | 889 | u8 *addr = slab_address(slab); |
24922684 CL |
890 | |
891 | print_tracking(s, p); | |
892 | ||
bb192ed9 | 893 | print_slab_info(slab); |
24922684 | 894 | |
96b94abc | 895 | pr_err("Object 0x%p @offset=%tu fp=0x%p\n\n", |
f9f58285 | 896 | p, p - addr, get_freepointer(s, p)); |
24922684 | 897 | |
d86bd1be | 898 | if (s->flags & SLAB_RED_ZONE) |
8669dbab | 899 | print_section(KERN_ERR, "Redzone ", p - s->red_left_pad, |
aa2efd5e | 900 | s->red_left_pad); |
d86bd1be | 901 | else if (p > addr + 16) |
aa2efd5e | 902 | print_section(KERN_ERR, "Bytes b4 ", p - 16, 16); |
81819f0f | 903 | |
8669dbab | 904 | print_section(KERN_ERR, "Object ", p, |
1b473f29 | 905 | min_t(unsigned int, s->object_size, PAGE_SIZE)); |
81819f0f | 906 | if (s->flags & SLAB_RED_ZONE) |
8669dbab | 907 | print_section(KERN_ERR, "Redzone ", p + s->object_size, |
3b0efdfa | 908 | s->inuse - s->object_size); |
81819f0f | 909 | |
cbfc35a4 | 910 | off = get_info_end(s); |
81819f0f | 911 | |
24922684 | 912 | if (s->flags & SLAB_STORE_USER) |
81819f0f | 913 | off += 2 * sizeof(struct track); |
81819f0f | 914 | |
6edf2576 FT |
915 | if (slub_debug_orig_size(s)) |
916 | off += sizeof(unsigned int); | |
917 | ||
80a9201a AP |
918 | off += kasan_metadata_size(s); |
919 | ||
d86bd1be | 920 | if (off != size_from_object(s)) |
81819f0f | 921 | /* Beginning of the filler is the free pointer */ |
8669dbab | 922 | print_section(KERN_ERR, "Padding ", p + off, |
aa2efd5e | 923 | size_from_object(s) - off); |
24922684 CL |
924 | |
925 | dump_stack(); | |
81819f0f CL |
926 | } |
927 | ||
bb192ed9 | 928 | static void object_err(struct kmem_cache *s, struct slab *slab, |
81819f0f CL |
929 | u8 *object, char *reason) |
930 | { | |
1f9f78b1 OG |
931 | if (slab_add_kunit_errors()) |
932 | return; | |
933 | ||
3dc50637 | 934 | slab_bug(s, "%s", reason); |
bb192ed9 | 935 | print_trailer(s, slab, object); |
65ebdeef | 936 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
81819f0f CL |
937 | } |
938 | ||
bb192ed9 | 939 | static bool freelist_corrupted(struct kmem_cache *s, struct slab *slab, |
ae16d059 VB |
940 | void **freelist, void *nextfree) |
941 | { | |
942 | if ((s->flags & SLAB_CONSISTENCY_CHECKS) && | |
bb192ed9 VB |
943 | !check_valid_pointer(s, slab, nextfree) && freelist) { |
944 | object_err(s, slab, *freelist, "Freechain corrupt"); | |
ae16d059 VB |
945 | *freelist = NULL; |
946 | slab_fix(s, "Isolate corrupted freechain"); | |
947 | return true; | |
948 | } | |
949 | ||
950 | return false; | |
951 | } | |
952 | ||
bb192ed9 | 953 | static __printf(3, 4) void slab_err(struct kmem_cache *s, struct slab *slab, |
d0e0ac97 | 954 | const char *fmt, ...) |
81819f0f CL |
955 | { |
956 | va_list args; | |
957 | char buf[100]; | |
958 | ||
1f9f78b1 OG |
959 | if (slab_add_kunit_errors()) |
960 | return; | |
961 | ||
24922684 CL |
962 | va_start(args, fmt); |
963 | vsnprintf(buf, sizeof(buf), fmt, args); | |
81819f0f | 964 | va_end(args); |
3dc50637 | 965 | slab_bug(s, "%s", buf); |
bb192ed9 | 966 | print_slab_info(slab); |
81819f0f | 967 | dump_stack(); |
65ebdeef | 968 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
81819f0f CL |
969 | } |
970 | ||
f7cb1933 | 971 | static void init_object(struct kmem_cache *s, void *object, u8 val) |
81819f0f | 972 | { |
aa1ef4d7 | 973 | u8 *p = kasan_reset_tag(object); |
81819f0f | 974 | |
d86bd1be JK |
975 | if (s->flags & SLAB_RED_ZONE) |
976 | memset(p - s->red_left_pad, val, s->red_left_pad); | |
977 | ||
81819f0f | 978 | if (s->flags & __OBJECT_POISON) { |
3b0efdfa CL |
979 | memset(p, POISON_FREE, s->object_size - 1); |
980 | p[s->object_size - 1] = POISON_END; | |
81819f0f CL |
981 | } |
982 | ||
983 | if (s->flags & SLAB_RED_ZONE) | |
3b0efdfa | 984 | memset(p + s->object_size, val, s->inuse - s->object_size); |
81819f0f CL |
985 | } |
986 | ||
24922684 CL |
987 | static void restore_bytes(struct kmem_cache *s, char *message, u8 data, |
988 | void *from, void *to) | |
989 | { | |
582d1212 | 990 | slab_fix(s, "Restoring %s 0x%p-0x%p=0x%x", message, from, to - 1, data); |
24922684 CL |
991 | memset(from, data, to - from); |
992 | } | |
993 | ||
bb192ed9 | 994 | static int check_bytes_and_report(struct kmem_cache *s, struct slab *slab, |
24922684 | 995 | u8 *object, char *what, |
06428780 | 996 | u8 *start, unsigned int value, unsigned int bytes) |
24922684 CL |
997 | { |
998 | u8 *fault; | |
999 | u8 *end; | |
bb192ed9 | 1000 | u8 *addr = slab_address(slab); |
24922684 | 1001 | |
a79316c6 | 1002 | metadata_access_enable(); |
aa1ef4d7 | 1003 | fault = memchr_inv(kasan_reset_tag(start), value, bytes); |
a79316c6 | 1004 | metadata_access_disable(); |
24922684 CL |
1005 | if (!fault) |
1006 | return 1; | |
1007 | ||
1008 | end = start + bytes; | |
1009 | while (end > fault && end[-1] == value) | |
1010 | end--; | |
1011 | ||
1f9f78b1 OG |
1012 | if (slab_add_kunit_errors()) |
1013 | goto skip_bug_print; | |
1014 | ||
24922684 | 1015 | slab_bug(s, "%s overwritten", what); |
96b94abc | 1016 | pr_err("0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n", |
e1b70dd1 MC |
1017 | fault, end - 1, fault - addr, |
1018 | fault[0], value); | |
bb192ed9 | 1019 | print_trailer(s, slab, object); |
65ebdeef | 1020 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
24922684 | 1021 | |
1f9f78b1 | 1022 | skip_bug_print: |
24922684 CL |
1023 | restore_bytes(s, what, value, fault, end); |
1024 | return 0; | |
81819f0f CL |
1025 | } |
1026 | ||
81819f0f CL |
1027 | /* |
1028 | * Object layout: | |
1029 | * | |
1030 | * object address | |
1031 | * Bytes of the object to be managed. | |
1032 | * If the freepointer may overlay the object then the free | |
cbfc35a4 | 1033 | * pointer is at the middle of the object. |
672bba3a | 1034 | * |
81819f0f CL |
1035 | * Poisoning uses 0x6b (POISON_FREE) and the last byte is |
1036 | * 0xa5 (POISON_END) | |
1037 | * | |
3b0efdfa | 1038 | * object + s->object_size |
81819f0f | 1039 | * Padding to reach word boundary. This is also used for Redzoning. |
672bba3a | 1040 | * Padding is extended by another word if Redzoning is enabled and |
3b0efdfa | 1041 | * object_size == inuse. |
672bba3a | 1042 | * |
81819f0f CL |
1043 | * We fill with 0xbb (RED_INACTIVE) for inactive objects and with |
1044 | * 0xcc (RED_ACTIVE) for objects in use. | |
1045 | * | |
1046 | * object + s->inuse | |
672bba3a CL |
1047 | * Meta data starts here. |
1048 | * | |
81819f0f CL |
1049 | * A. Free pointer (if we cannot overwrite object on free) |
1050 | * B. Tracking data for SLAB_STORE_USER | |
6edf2576 FT |
1051 | * C. Original request size for kmalloc object (SLAB_STORE_USER enabled) |
1052 | * D. Padding to reach required alignment boundary or at minimum | |
6446faa2 | 1053 | * one word if debugging is on to be able to detect writes |
672bba3a CL |
1054 | * before the word boundary. |
1055 | * | |
1056 | * Padding is done using 0x5a (POISON_INUSE) | |
81819f0f CL |
1057 | * |
1058 | * object + s->size | |
672bba3a | 1059 | * Nothing is used beyond s->size. |
81819f0f | 1060 | * |
3b0efdfa | 1061 | * If slabcaches are merged then the object_size and inuse boundaries are mostly |
672bba3a | 1062 | * ignored. And therefore no slab options that rely on these boundaries |
81819f0f CL |
1063 | * may be used with merged slabcaches. |
1064 | */ | |
1065 | ||
bb192ed9 | 1066 | static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p) |
81819f0f | 1067 | { |
cbfc35a4 | 1068 | unsigned long off = get_info_end(s); /* The end of info */ |
81819f0f | 1069 | |
6edf2576 | 1070 | if (s->flags & SLAB_STORE_USER) { |
81819f0f CL |
1071 | /* We also have user information there */ |
1072 | off += 2 * sizeof(struct track); | |
1073 | ||
6edf2576 FT |
1074 | if (s->flags & SLAB_KMALLOC) |
1075 | off += sizeof(unsigned int); | |
1076 | } | |
1077 | ||
80a9201a AP |
1078 | off += kasan_metadata_size(s); |
1079 | ||
d86bd1be | 1080 | if (size_from_object(s) == off) |
81819f0f CL |
1081 | return 1; |
1082 | ||
bb192ed9 | 1083 | return check_bytes_and_report(s, slab, p, "Object padding", |
d86bd1be | 1084 | p + off, POISON_INUSE, size_from_object(s) - off); |
81819f0f CL |
1085 | } |
1086 | ||
39b26464 | 1087 | /* Check the pad bytes at the end of a slab page */ |
a204e6d6 | 1088 | static void slab_pad_check(struct kmem_cache *s, struct slab *slab) |
81819f0f | 1089 | { |
24922684 CL |
1090 | u8 *start; |
1091 | u8 *fault; | |
1092 | u8 *end; | |
5d682681 | 1093 | u8 *pad; |
24922684 CL |
1094 | int length; |
1095 | int remainder; | |
81819f0f CL |
1096 | |
1097 | if (!(s->flags & SLAB_POISON)) | |
a204e6d6 | 1098 | return; |
81819f0f | 1099 | |
bb192ed9 VB |
1100 | start = slab_address(slab); |
1101 | length = slab_size(slab); | |
39b26464 CL |
1102 | end = start + length; |
1103 | remainder = length % s->size; | |
81819f0f | 1104 | if (!remainder) |
a204e6d6 | 1105 | return; |
81819f0f | 1106 | |
5d682681 | 1107 | pad = end - remainder; |
a79316c6 | 1108 | metadata_access_enable(); |
aa1ef4d7 | 1109 | fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder); |
a79316c6 | 1110 | metadata_access_disable(); |
24922684 | 1111 | if (!fault) |
a204e6d6 | 1112 | return; |
24922684 CL |
1113 | while (end > fault && end[-1] == POISON_INUSE) |
1114 | end--; | |
1115 | ||
bb192ed9 | 1116 | slab_err(s, slab, "Padding overwritten. 0x%p-0x%p @offset=%tu", |
e1b70dd1 | 1117 | fault, end - 1, fault - start); |
5d682681 | 1118 | print_section(KERN_ERR, "Padding ", pad, remainder); |
24922684 | 1119 | |
5d682681 | 1120 | restore_bytes(s, "slab padding", POISON_INUSE, fault, end); |
81819f0f CL |
1121 | } |
1122 | ||
bb192ed9 | 1123 | static int check_object(struct kmem_cache *s, struct slab *slab, |
f7cb1933 | 1124 | void *object, u8 val) |
81819f0f CL |
1125 | { |
1126 | u8 *p = object; | |
3b0efdfa | 1127 | u8 *endobject = object + s->object_size; |
81819f0f CL |
1128 | |
1129 | if (s->flags & SLAB_RED_ZONE) { | |
bb192ed9 | 1130 | if (!check_bytes_and_report(s, slab, object, "Left Redzone", |
d86bd1be JK |
1131 | object - s->red_left_pad, val, s->red_left_pad)) |
1132 | return 0; | |
1133 | ||
bb192ed9 | 1134 | if (!check_bytes_and_report(s, slab, object, "Right Redzone", |
3b0efdfa | 1135 | endobject, val, s->inuse - s->object_size)) |
81819f0f | 1136 | return 0; |
81819f0f | 1137 | } else { |
3b0efdfa | 1138 | if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) { |
bb192ed9 | 1139 | check_bytes_and_report(s, slab, p, "Alignment padding", |
d0e0ac97 CG |
1140 | endobject, POISON_INUSE, |
1141 | s->inuse - s->object_size); | |
3adbefee | 1142 | } |
81819f0f CL |
1143 | } |
1144 | ||
1145 | if (s->flags & SLAB_POISON) { | |
f7cb1933 | 1146 | if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) && |
bb192ed9 | 1147 | (!check_bytes_and_report(s, slab, p, "Poison", p, |
3b0efdfa | 1148 | POISON_FREE, s->object_size - 1) || |
bb192ed9 | 1149 | !check_bytes_and_report(s, slab, p, "End Poison", |
3b0efdfa | 1150 | p + s->object_size - 1, POISON_END, 1))) |
81819f0f | 1151 | return 0; |
81819f0f CL |
1152 | /* |
1153 | * check_pad_bytes cleans up on its own. | |
1154 | */ | |
bb192ed9 | 1155 | check_pad_bytes(s, slab, p); |
81819f0f CL |
1156 | } |
1157 | ||
cbfc35a4 | 1158 | if (!freeptr_outside_object(s) && val == SLUB_RED_ACTIVE) |
81819f0f CL |
1159 | /* |
1160 | * Object and freepointer overlap. Cannot check | |
1161 | * freepointer while object is allocated. | |
1162 | */ | |
1163 | return 1; | |
1164 | ||
1165 | /* Check free pointer validity */ | |
bb192ed9 VB |
1166 | if (!check_valid_pointer(s, slab, get_freepointer(s, p))) { |
1167 | object_err(s, slab, p, "Freepointer corrupt"); | |
81819f0f | 1168 | /* |
9f6c708e | 1169 | * No choice but to zap it and thus lose the remainder |
81819f0f | 1170 | * of the free objects in this slab. May cause |
672bba3a | 1171 | * another error because the object count is now wrong. |
81819f0f | 1172 | */ |
a973e9dd | 1173 | set_freepointer(s, p, NULL); |
81819f0f CL |
1174 | return 0; |
1175 | } | |
1176 | return 1; | |
1177 | } | |
1178 | ||
bb192ed9 | 1179 | static int check_slab(struct kmem_cache *s, struct slab *slab) |
81819f0f | 1180 | { |
39b26464 CL |
1181 | int maxobj; |
1182 | ||
bb192ed9 VB |
1183 | if (!folio_test_slab(slab_folio(slab))) { |
1184 | slab_err(s, slab, "Not a valid slab page"); | |
81819f0f CL |
1185 | return 0; |
1186 | } | |
39b26464 | 1187 | |
bb192ed9 VB |
1188 | maxobj = order_objects(slab_order(slab), s->size); |
1189 | if (slab->objects > maxobj) { | |
1190 | slab_err(s, slab, "objects %u > max %u", | |
1191 | slab->objects, maxobj); | |
39b26464 CL |
1192 | return 0; |
1193 | } | |
bb192ed9 VB |
1194 | if (slab->inuse > slab->objects) { |
1195 | slab_err(s, slab, "inuse %u > max %u", | |
1196 | slab->inuse, slab->objects); | |
81819f0f CL |
1197 | return 0; |
1198 | } | |
1199 | /* Slab_pad_check fixes things up after itself */ | |
bb192ed9 | 1200 | slab_pad_check(s, slab); |
81819f0f CL |
1201 | return 1; |
1202 | } | |
1203 | ||
1204 | /* | |
c2092c12 | 1205 | * Determine if a certain object in a slab is on the freelist. Must hold the |
672bba3a | 1206 | * slab lock to guarantee that the chains are in a consistent state. |
81819f0f | 1207 | */ |
bb192ed9 | 1208 | static int on_freelist(struct kmem_cache *s, struct slab *slab, void *search) |
81819f0f CL |
1209 | { |
1210 | int nr = 0; | |
881db7fb | 1211 | void *fp; |
81819f0f | 1212 | void *object = NULL; |
f6edde9c | 1213 | int max_objects; |
81819f0f | 1214 | |
bb192ed9 VB |
1215 | fp = slab->freelist; |
1216 | while (fp && nr <= slab->objects) { | |
81819f0f CL |
1217 | if (fp == search) |
1218 | return 1; | |
bb192ed9 | 1219 | if (!check_valid_pointer(s, slab, fp)) { |
81819f0f | 1220 | if (object) { |
bb192ed9 | 1221 | object_err(s, slab, object, |
81819f0f | 1222 | "Freechain corrupt"); |
a973e9dd | 1223 | set_freepointer(s, object, NULL); |
81819f0f | 1224 | } else { |
bb192ed9 VB |
1225 | slab_err(s, slab, "Freepointer corrupt"); |
1226 | slab->freelist = NULL; | |
1227 | slab->inuse = slab->objects; | |
24922684 | 1228 | slab_fix(s, "Freelist cleared"); |
81819f0f CL |
1229 | return 0; |
1230 | } | |
1231 | break; | |
1232 | } | |
1233 | object = fp; | |
1234 | fp = get_freepointer(s, object); | |
1235 | nr++; | |
1236 | } | |
1237 | ||
bb192ed9 | 1238 | max_objects = order_objects(slab_order(slab), s->size); |
210b5c06 CG |
1239 | if (max_objects > MAX_OBJS_PER_PAGE) |
1240 | max_objects = MAX_OBJS_PER_PAGE; | |
224a88be | 1241 | |
bb192ed9 VB |
1242 | if (slab->objects != max_objects) { |
1243 | slab_err(s, slab, "Wrong number of objects. Found %d but should be %d", | |
1244 | slab->objects, max_objects); | |
1245 | slab->objects = max_objects; | |
582d1212 | 1246 | slab_fix(s, "Number of objects adjusted"); |
224a88be | 1247 | } |
bb192ed9 VB |
1248 | if (slab->inuse != slab->objects - nr) { |
1249 | slab_err(s, slab, "Wrong object count. Counter is %d but counted were %d", | |
1250 | slab->inuse, slab->objects - nr); | |
1251 | slab->inuse = slab->objects - nr; | |
582d1212 | 1252 | slab_fix(s, "Object count adjusted"); |
81819f0f CL |
1253 | } |
1254 | return search == NULL; | |
1255 | } | |
1256 | ||
bb192ed9 | 1257 | static void trace(struct kmem_cache *s, struct slab *slab, void *object, |
0121c619 | 1258 | int alloc) |
3ec09742 CL |
1259 | { |
1260 | if (s->flags & SLAB_TRACE) { | |
f9f58285 | 1261 | pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n", |
3ec09742 CL |
1262 | s->name, |
1263 | alloc ? "alloc" : "free", | |
bb192ed9 VB |
1264 | object, slab->inuse, |
1265 | slab->freelist); | |
3ec09742 CL |
1266 | |
1267 | if (!alloc) | |
aa2efd5e | 1268 | print_section(KERN_INFO, "Object ", (void *)object, |
d0e0ac97 | 1269 | s->object_size); |
3ec09742 CL |
1270 | |
1271 | dump_stack(); | |
1272 | } | |
1273 | } | |
1274 | ||
643b1138 | 1275 | /* |
672bba3a | 1276 | * Tracking of fully allocated slabs for debugging purposes. |
643b1138 | 1277 | */ |
5cc6eee8 | 1278 | static void add_full(struct kmem_cache *s, |
bb192ed9 | 1279 | struct kmem_cache_node *n, struct slab *slab) |
643b1138 | 1280 | { |
5cc6eee8 CL |
1281 | if (!(s->flags & SLAB_STORE_USER)) |
1282 | return; | |
1283 | ||
255d0884 | 1284 | lockdep_assert_held(&n->list_lock); |
bb192ed9 | 1285 | list_add(&slab->slab_list, &n->full); |
643b1138 CL |
1286 | } |
1287 | ||
bb192ed9 | 1288 | static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct slab *slab) |
643b1138 | 1289 | { |
643b1138 CL |
1290 | if (!(s->flags & SLAB_STORE_USER)) |
1291 | return; | |
1292 | ||
255d0884 | 1293 | lockdep_assert_held(&n->list_lock); |
bb192ed9 | 1294 | list_del(&slab->slab_list); |
643b1138 CL |
1295 | } |
1296 | ||
0f389ec6 CL |
1297 | /* Tracking of the number of slabs for debugging purposes */ |
1298 | static inline unsigned long slabs_node(struct kmem_cache *s, int node) | |
1299 | { | |
1300 | struct kmem_cache_node *n = get_node(s, node); | |
1301 | ||
1302 | return atomic_long_read(&n->nr_slabs); | |
1303 | } | |
1304 | ||
26c02cf0 AB |
1305 | static inline unsigned long node_nr_slabs(struct kmem_cache_node *n) |
1306 | { | |
1307 | return atomic_long_read(&n->nr_slabs); | |
1308 | } | |
1309 | ||
205ab99d | 1310 | static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects) |
0f389ec6 CL |
1311 | { |
1312 | struct kmem_cache_node *n = get_node(s, node); | |
1313 | ||
1314 | /* | |
1315 | * May be called early in order to allocate a slab for the | |
1316 | * kmem_cache_node structure. Solve the chicken-egg | |
1317 | * dilemma by deferring the increment of the count during | |
1318 | * bootstrap (see early_kmem_cache_node_alloc). | |
1319 | */ | |
338b2642 | 1320 | if (likely(n)) { |
0f389ec6 | 1321 | atomic_long_inc(&n->nr_slabs); |
205ab99d CL |
1322 | atomic_long_add(objects, &n->total_objects); |
1323 | } | |
0f389ec6 | 1324 | } |
205ab99d | 1325 | static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects) |
0f389ec6 CL |
1326 | { |
1327 | struct kmem_cache_node *n = get_node(s, node); | |
1328 | ||
1329 | atomic_long_dec(&n->nr_slabs); | |
205ab99d | 1330 | atomic_long_sub(objects, &n->total_objects); |
0f389ec6 CL |
1331 | } |
1332 | ||
1333 | /* Object debug checks for alloc/free paths */ | |
c0f81a94 | 1334 | static void setup_object_debug(struct kmem_cache *s, void *object) |
3ec09742 | 1335 | { |
8fc8d666 | 1336 | if (!kmem_cache_debug_flags(s, SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)) |
3ec09742 CL |
1337 | return; |
1338 | ||
f7cb1933 | 1339 | init_object(s, object, SLUB_RED_INACTIVE); |
3ec09742 CL |
1340 | init_tracking(s, object); |
1341 | } | |
1342 | ||
a50b854e | 1343 | static |
bb192ed9 | 1344 | void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) |
a7101224 | 1345 | { |
8fc8d666 | 1346 | if (!kmem_cache_debug_flags(s, SLAB_POISON)) |
a7101224 AK |
1347 | return; |
1348 | ||
1349 | metadata_access_enable(); | |
bb192ed9 | 1350 | memset(kasan_reset_tag(addr), POISON_INUSE, slab_size(slab)); |
a7101224 AK |
1351 | metadata_access_disable(); |
1352 | } | |
1353 | ||
becfda68 | 1354 | static inline int alloc_consistency_checks(struct kmem_cache *s, |
bb192ed9 | 1355 | struct slab *slab, void *object) |
81819f0f | 1356 | { |
bb192ed9 | 1357 | if (!check_slab(s, slab)) |
becfda68 | 1358 | return 0; |
81819f0f | 1359 | |
bb192ed9 VB |
1360 | if (!check_valid_pointer(s, slab, object)) { |
1361 | object_err(s, slab, object, "Freelist Pointer check fails"); | |
becfda68 | 1362 | return 0; |
81819f0f CL |
1363 | } |
1364 | ||
bb192ed9 | 1365 | if (!check_object(s, slab, object, SLUB_RED_INACTIVE)) |
becfda68 LA |
1366 | return 0; |
1367 | ||
1368 | return 1; | |
1369 | } | |
1370 | ||
1371 | static noinline int alloc_debug_processing(struct kmem_cache *s, | |
6edf2576 | 1372 | struct slab *slab, void *object, int orig_size) |
becfda68 LA |
1373 | { |
1374 | if (s->flags & SLAB_CONSISTENCY_CHECKS) { | |
bb192ed9 | 1375 | if (!alloc_consistency_checks(s, slab, object)) |
becfda68 LA |
1376 | goto bad; |
1377 | } | |
81819f0f | 1378 | |
c7323a5a | 1379 | /* Success. Perform special debug activities for allocs */ |
bb192ed9 | 1380 | trace(s, slab, object, 1); |
6edf2576 | 1381 | set_orig_size(s, object, orig_size); |
f7cb1933 | 1382 | init_object(s, object, SLUB_RED_ACTIVE); |
81819f0f | 1383 | return 1; |
3ec09742 | 1384 | |
81819f0f | 1385 | bad: |
bb192ed9 | 1386 | if (folio_test_slab(slab_folio(slab))) { |
81819f0f CL |
1387 | /* |
1388 | * If this is a slab page then lets do the best we can | |
1389 | * to avoid issues in the future. Marking all objects | |
672bba3a | 1390 | * as used avoids touching the remaining objects. |
81819f0f | 1391 | */ |
24922684 | 1392 | slab_fix(s, "Marking all objects used"); |
bb192ed9 VB |
1393 | slab->inuse = slab->objects; |
1394 | slab->freelist = NULL; | |
81819f0f CL |
1395 | } |
1396 | return 0; | |
1397 | } | |
1398 | ||
becfda68 | 1399 | static inline int free_consistency_checks(struct kmem_cache *s, |
bb192ed9 | 1400 | struct slab *slab, void *object, unsigned long addr) |
81819f0f | 1401 | { |
bb192ed9 VB |
1402 | if (!check_valid_pointer(s, slab, object)) { |
1403 | slab_err(s, slab, "Invalid object pointer 0x%p", object); | |
becfda68 | 1404 | return 0; |
81819f0f CL |
1405 | } |
1406 | ||
bb192ed9 VB |
1407 | if (on_freelist(s, slab, object)) { |
1408 | object_err(s, slab, object, "Object already free"); | |
becfda68 | 1409 | return 0; |
81819f0f CL |
1410 | } |
1411 | ||
bb192ed9 | 1412 | if (!check_object(s, slab, object, SLUB_RED_ACTIVE)) |
becfda68 | 1413 | return 0; |
81819f0f | 1414 | |
bb192ed9 VB |
1415 | if (unlikely(s != slab->slab_cache)) { |
1416 | if (!folio_test_slab(slab_folio(slab))) { | |
1417 | slab_err(s, slab, "Attempt to free object(0x%p) outside of slab", | |
756a025f | 1418 | object); |
bb192ed9 | 1419 | } else if (!slab->slab_cache) { |
f9f58285 FF |
1420 | pr_err("SLUB <none>: no slab for object 0x%p.\n", |
1421 | object); | |
70d71228 | 1422 | dump_stack(); |
06428780 | 1423 | } else |
bb192ed9 | 1424 | object_err(s, slab, object, |
24922684 | 1425 | "page slab pointer corrupt."); |
becfda68 LA |
1426 | return 0; |
1427 | } | |
1428 | return 1; | |
1429 | } | |
1430 | ||
e17f1dfb VB |
1431 | /* |
1432 | * Parse a block of slub_debug options. Blocks are delimited by ';' | |
1433 | * | |
1434 | * @str: start of block | |
1435 | * @flags: returns parsed flags, or DEBUG_DEFAULT_FLAGS if none specified | |
1436 | * @slabs: return start of list of slabs, or NULL when there's no list | |
1437 | * @init: assume this is initial parsing and not per-kmem-create parsing | |
1438 | * | |
1439 | * returns the start of next block if there's any, or NULL | |
1440 | */ | |
1441 | static char * | |
1442 | parse_slub_debug_flags(char *str, slab_flags_t *flags, char **slabs, bool init) | |
41ecc55b | 1443 | { |
e17f1dfb | 1444 | bool higher_order_disable = false; |
f0630fff | 1445 | |
e17f1dfb VB |
1446 | /* Skip any completely empty blocks */ |
1447 | while (*str && *str == ';') | |
1448 | str++; | |
1449 | ||
1450 | if (*str == ',') { | |
f0630fff CL |
1451 | /* |
1452 | * No options but restriction on slabs. This means full | |
1453 | * debugging for slabs matching a pattern. | |
1454 | */ | |
e17f1dfb | 1455 | *flags = DEBUG_DEFAULT_FLAGS; |
f0630fff | 1456 | goto check_slabs; |
e17f1dfb VB |
1457 | } |
1458 | *flags = 0; | |
f0630fff | 1459 | |
e17f1dfb VB |
1460 | /* Determine which debug features should be switched on */ |
1461 | for (; *str && *str != ',' && *str != ';'; str++) { | |
f0630fff | 1462 | switch (tolower(*str)) { |
e17f1dfb VB |
1463 | case '-': |
1464 | *flags = 0; | |
1465 | break; | |
f0630fff | 1466 | case 'f': |
e17f1dfb | 1467 | *flags |= SLAB_CONSISTENCY_CHECKS; |
f0630fff CL |
1468 | break; |
1469 | case 'z': | |
e17f1dfb | 1470 | *flags |= SLAB_RED_ZONE; |
f0630fff CL |
1471 | break; |
1472 | case 'p': | |
e17f1dfb | 1473 | *flags |= SLAB_POISON; |
f0630fff CL |
1474 | break; |
1475 | case 'u': | |
e17f1dfb | 1476 | *flags |= SLAB_STORE_USER; |
f0630fff CL |
1477 | break; |
1478 | case 't': | |
e17f1dfb | 1479 | *flags |= SLAB_TRACE; |
f0630fff | 1480 | break; |
4c13dd3b | 1481 | case 'a': |
e17f1dfb | 1482 | *flags |= SLAB_FAILSLAB; |
4c13dd3b | 1483 | break; |
08303a73 CA |
1484 | case 'o': |
1485 | /* | |
1486 | * Avoid enabling debugging on caches if its minimum | |
1487 | * order would increase as a result. | |
1488 | */ | |
e17f1dfb | 1489 | higher_order_disable = true; |
08303a73 | 1490 | break; |
f0630fff | 1491 | default: |
e17f1dfb VB |
1492 | if (init) |
1493 | pr_err("slub_debug option '%c' unknown. skipped\n", *str); | |
f0630fff | 1494 | } |
41ecc55b | 1495 | } |
f0630fff | 1496 | check_slabs: |
41ecc55b | 1497 | if (*str == ',') |
e17f1dfb VB |
1498 | *slabs = ++str; |
1499 | else | |
1500 | *slabs = NULL; | |
1501 | ||
1502 | /* Skip over the slab list */ | |
1503 | while (*str && *str != ';') | |
1504 | str++; | |
1505 | ||
1506 | /* Skip any completely empty blocks */ | |
1507 | while (*str && *str == ';') | |
1508 | str++; | |
1509 | ||
1510 | if (init && higher_order_disable) | |
1511 | disable_higher_order_debug = 1; | |
1512 | ||
1513 | if (*str) | |
1514 | return str; | |
1515 | else | |
1516 | return NULL; | |
1517 | } | |
1518 | ||
1519 | static int __init setup_slub_debug(char *str) | |
1520 | { | |
1521 | slab_flags_t flags; | |
a7f1d485 | 1522 | slab_flags_t global_flags; |
e17f1dfb VB |
1523 | char *saved_str; |
1524 | char *slab_list; | |
1525 | bool global_slub_debug_changed = false; | |
1526 | bool slab_list_specified = false; | |
1527 | ||
a7f1d485 | 1528 | global_flags = DEBUG_DEFAULT_FLAGS; |
e17f1dfb VB |
1529 | if (*str++ != '=' || !*str) |
1530 | /* | |
1531 | * No options specified. Switch on full debugging. | |
1532 | */ | |
1533 | goto out; | |
1534 | ||
1535 | saved_str = str; | |
1536 | while (str) { | |
1537 | str = parse_slub_debug_flags(str, &flags, &slab_list, true); | |
1538 | ||
1539 | if (!slab_list) { | |
a7f1d485 | 1540 | global_flags = flags; |
e17f1dfb VB |
1541 | global_slub_debug_changed = true; |
1542 | } else { | |
1543 | slab_list_specified = true; | |
5cf909c5 OG |
1544 | if (flags & SLAB_STORE_USER) |
1545 | stack_depot_want_early_init(); | |
e17f1dfb VB |
1546 | } |
1547 | } | |
1548 | ||
1549 | /* | |
1550 | * For backwards compatibility, a single list of flags with list of | |
a7f1d485 VB |
1551 | * slabs means debugging is only changed for those slabs, so the global |
1552 | * slub_debug should be unchanged (0 or DEBUG_DEFAULT_FLAGS, depending | |
1553 | * on CONFIG_SLUB_DEBUG_ON). We can extended that to multiple lists as | |
e17f1dfb VB |
1554 | * long as there is no option specifying flags without a slab list. |
1555 | */ | |
1556 | if (slab_list_specified) { | |
1557 | if (!global_slub_debug_changed) | |
a7f1d485 | 1558 | global_flags = slub_debug; |
e17f1dfb VB |
1559 | slub_debug_string = saved_str; |
1560 | } | |
f0630fff | 1561 | out: |
a7f1d485 | 1562 | slub_debug = global_flags; |
5cf909c5 OG |
1563 | if (slub_debug & SLAB_STORE_USER) |
1564 | stack_depot_want_early_init(); | |
ca0cab65 VB |
1565 | if (slub_debug != 0 || slub_debug_string) |
1566 | static_branch_enable(&slub_debug_enabled); | |
02ac47d0 SB |
1567 | else |
1568 | static_branch_disable(&slub_debug_enabled); | |
6471384a AP |
1569 | if ((static_branch_unlikely(&init_on_alloc) || |
1570 | static_branch_unlikely(&init_on_free)) && | |
1571 | (slub_debug & SLAB_POISON)) | |
1572 | pr_info("mem auto-init: SLAB_POISON will take precedence over init_on_alloc/init_on_free\n"); | |
41ecc55b CL |
1573 | return 1; |
1574 | } | |
1575 | ||
1576 | __setup("slub_debug", setup_slub_debug); | |
1577 | ||
c5fd3ca0 AT |
1578 | /* |
1579 | * kmem_cache_flags - apply debugging options to the cache | |
1580 | * @object_size: the size of an object without meta data | |
1581 | * @flags: flags to set | |
1582 | * @name: name of the cache | |
c5fd3ca0 AT |
1583 | * |
1584 | * Debug option(s) are applied to @flags. In addition to the debug | |
1585 | * option(s), if a slab name (or multiple) is specified i.e. | |
1586 | * slub_debug=<Debug-Options>,<slab name1>,<slab name2> ... | |
1587 | * then only the select slabs will receive the debug option(s). | |
1588 | */ | |
0293d1fd | 1589 | slab_flags_t kmem_cache_flags(unsigned int object_size, |
37540008 | 1590 | slab_flags_t flags, const char *name) |
41ecc55b | 1591 | { |
c5fd3ca0 AT |
1592 | char *iter; |
1593 | size_t len; | |
e17f1dfb VB |
1594 | char *next_block; |
1595 | slab_flags_t block_flags; | |
ca220593 JB |
1596 | slab_flags_t slub_debug_local = slub_debug; |
1597 | ||
a285909f HY |
1598 | if (flags & SLAB_NO_USER_FLAGS) |
1599 | return flags; | |
1600 | ||
ca220593 JB |
1601 | /* |
1602 | * If the slab cache is for debugging (e.g. kmemleak) then | |
1603 | * don't store user (stack trace) information by default, | |
1604 | * but let the user enable it via the command line below. | |
1605 | */ | |
1606 | if (flags & SLAB_NOLEAKTRACE) | |
1607 | slub_debug_local &= ~SLAB_STORE_USER; | |
c5fd3ca0 | 1608 | |
c5fd3ca0 | 1609 | len = strlen(name); |
e17f1dfb VB |
1610 | next_block = slub_debug_string; |
1611 | /* Go through all blocks of debug options, see if any matches our slab's name */ | |
1612 | while (next_block) { | |
1613 | next_block = parse_slub_debug_flags(next_block, &block_flags, &iter, false); | |
1614 | if (!iter) | |
1615 | continue; | |
1616 | /* Found a block that has a slab list, search it */ | |
1617 | while (*iter) { | |
1618 | char *end, *glob; | |
1619 | size_t cmplen; | |
1620 | ||
1621 | end = strchrnul(iter, ','); | |
1622 | if (next_block && next_block < end) | |
1623 | end = next_block - 1; | |
1624 | ||
1625 | glob = strnchr(iter, end - iter, '*'); | |
1626 | if (glob) | |
1627 | cmplen = glob - iter; | |
1628 | else | |
1629 | cmplen = max_t(size_t, len, (end - iter)); | |
c5fd3ca0 | 1630 | |
e17f1dfb VB |
1631 | if (!strncmp(name, iter, cmplen)) { |
1632 | flags |= block_flags; | |
1633 | return flags; | |
1634 | } | |
c5fd3ca0 | 1635 | |
e17f1dfb VB |
1636 | if (!*end || *end == ';') |
1637 | break; | |
1638 | iter = end + 1; | |
c5fd3ca0 | 1639 | } |
c5fd3ca0 | 1640 | } |
ba0268a8 | 1641 | |
ca220593 | 1642 | return flags | slub_debug_local; |
41ecc55b | 1643 | } |
b4a64718 | 1644 | #else /* !CONFIG_SLUB_DEBUG */ |
c0f81a94 | 1645 | static inline void setup_object_debug(struct kmem_cache *s, void *object) {} |
a50b854e | 1646 | static inline |
bb192ed9 | 1647 | void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {} |
41ecc55b | 1648 | |
3ec09742 | 1649 | static inline int alloc_debug_processing(struct kmem_cache *s, |
6edf2576 | 1650 | struct slab *slab, void *object, int orig_size) { return 0; } |
41ecc55b | 1651 | |
c7323a5a | 1652 | static inline void free_debug_processing( |
bb192ed9 | 1653 | struct kmem_cache *s, struct slab *slab, |
81084651 | 1654 | void *head, void *tail, int bulk_cnt, |
c7323a5a | 1655 | unsigned long addr) {} |
41ecc55b | 1656 | |
a204e6d6 | 1657 | static inline void slab_pad_check(struct kmem_cache *s, struct slab *slab) {} |
bb192ed9 | 1658 | static inline int check_object(struct kmem_cache *s, struct slab *slab, |
f7cb1933 | 1659 | void *object, u8 val) { return 1; } |
c7323a5a VB |
1660 | static inline void set_track(struct kmem_cache *s, void *object, |
1661 | enum track_item alloc, unsigned long addr) {} | |
5cc6eee8 | 1662 | static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n, |
bb192ed9 | 1663 | struct slab *slab) {} |
c65c1877 | 1664 | static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, |
bb192ed9 | 1665 | struct slab *slab) {} |
0293d1fd | 1666 | slab_flags_t kmem_cache_flags(unsigned int object_size, |
37540008 | 1667 | slab_flags_t flags, const char *name) |
ba0268a8 CL |
1668 | { |
1669 | return flags; | |
1670 | } | |
41ecc55b | 1671 | #define slub_debug 0 |
0f389ec6 | 1672 | |
fdaa45e9 IM |
1673 | #define disable_higher_order_debug 0 |
1674 | ||
0f389ec6 CL |
1675 | static inline unsigned long slabs_node(struct kmem_cache *s, int node) |
1676 | { return 0; } | |
26c02cf0 AB |
1677 | static inline unsigned long node_nr_slabs(struct kmem_cache_node *n) |
1678 | { return 0; } | |
205ab99d CL |
1679 | static inline void inc_slabs_node(struct kmem_cache *s, int node, |
1680 | int objects) {} | |
1681 | static inline void dec_slabs_node(struct kmem_cache *s, int node, | |
1682 | int objects) {} | |
7d550c56 | 1683 | |
bb192ed9 | 1684 | static bool freelist_corrupted(struct kmem_cache *s, struct slab *slab, |
dc07a728 | 1685 | void **freelist, void *nextfree) |
52f23478 DZ |
1686 | { |
1687 | return false; | |
1688 | } | |
02e72cc6 AR |
1689 | #endif /* CONFIG_SLUB_DEBUG */ |
1690 | ||
1691 | /* | |
1692 | * Hooks for other subsystems that check memory allocations. In a typical | |
1693 | * production configuration these hooks all should produce no code at all. | |
1694 | */ | |
d57a964e AK |
1695 | static __always_inline bool slab_free_hook(struct kmem_cache *s, |
1696 | void *x, bool init) | |
d56791b3 RB |
1697 | { |
1698 | kmemleak_free_recursive(x, s->flags); | |
68ef169a | 1699 | kmsan_slab_free(s, x); |
7d550c56 | 1700 | |
84048039 | 1701 | debug_check_no_locks_freed(x, s->object_size); |
02e72cc6 | 1702 | |
02e72cc6 AR |
1703 | if (!(s->flags & SLAB_DEBUG_OBJECTS)) |
1704 | debug_check_no_obj_freed(x, s->object_size); | |
0316bec2 | 1705 | |
cfbe1636 ME |
1706 | /* Use KCSAN to help debug racy use-after-free. */ |
1707 | if (!(s->flags & SLAB_TYPESAFE_BY_RCU)) | |
1708 | __kcsan_check_access(x, s->object_size, | |
1709 | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); | |
1710 | ||
d57a964e AK |
1711 | /* |
1712 | * As memory initialization might be integrated into KASAN, | |
1713 | * kasan_slab_free and initialization memset's must be | |
1714 | * kept together to avoid discrepancies in behavior. | |
1715 | * | |
1716 | * The initialization memset's clear the object and the metadata, | |
1717 | * but don't touch the SLAB redzone. | |
1718 | */ | |
1719 | if (init) { | |
1720 | int rsize; | |
1721 | ||
1722 | if (!kasan_has_integrated_init()) | |
1723 | memset(kasan_reset_tag(x), 0, s->object_size); | |
1724 | rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; | |
1725 | memset((char *)kasan_reset_tag(x) + s->inuse, 0, | |
1726 | s->size - s->inuse - rsize); | |
1727 | } | |
1728 | /* KASAN might put x into memory quarantine, delaying its reuse. */ | |
1729 | return kasan_slab_free(s, x, init); | |
02e72cc6 | 1730 | } |
205ab99d | 1731 | |
c3895391 | 1732 | static inline bool slab_free_freelist_hook(struct kmem_cache *s, |
899447f6 ML |
1733 | void **head, void **tail, |
1734 | int *cnt) | |
81084651 | 1735 | { |
6471384a AP |
1736 | |
1737 | void *object; | |
1738 | void *next = *head; | |
1739 | void *old_tail = *tail ? *tail : *head; | |
6471384a | 1740 | |
b89fb5ef | 1741 | if (is_kfence_address(next)) { |
d57a964e | 1742 | slab_free_hook(s, next, false); |
b89fb5ef AP |
1743 | return true; |
1744 | } | |
1745 | ||
aea4df4c LA |
1746 | /* Head and tail of the reconstructed freelist */ |
1747 | *head = NULL; | |
1748 | *tail = NULL; | |
1b7e816f | 1749 | |
aea4df4c LA |
1750 | do { |
1751 | object = next; | |
1752 | next = get_freepointer(s, object); | |
1753 | ||
c3895391 | 1754 | /* If object's reuse doesn't have to be delayed */ |
d57a964e | 1755 | if (!slab_free_hook(s, object, slab_want_init_on_free(s))) { |
c3895391 AK |
1756 | /* Move object to the new freelist */ |
1757 | set_freepointer(s, object, *head); | |
1758 | *head = object; | |
1759 | if (!*tail) | |
1760 | *tail = object; | |
899447f6 ML |
1761 | } else { |
1762 | /* | |
1763 | * Adjust the reconstructed freelist depth | |
1764 | * accordingly if object's reuse is delayed. | |
1765 | */ | |
1766 | --(*cnt); | |
c3895391 AK |
1767 | } |
1768 | } while (object != old_tail); | |
1769 | ||
1770 | if (*head == *tail) | |
1771 | *tail = NULL; | |
1772 | ||
1773 | return *head != NULL; | |
81084651 JDB |
1774 | } |
1775 | ||
c0f81a94 | 1776 | static void *setup_object(struct kmem_cache *s, void *object) |
588f8ba9 | 1777 | { |
c0f81a94 | 1778 | setup_object_debug(s, object); |
4d176711 | 1779 | object = kasan_init_slab_obj(s, object); |
588f8ba9 TG |
1780 | if (unlikely(s->ctor)) { |
1781 | kasan_unpoison_object_data(s, object); | |
1782 | s->ctor(object); | |
1783 | kasan_poison_object_data(s, object); | |
1784 | } | |
4d176711 | 1785 | return object; |
588f8ba9 TG |
1786 | } |
1787 | ||
81819f0f CL |
1788 | /* |
1789 | * Slab allocation and freeing | |
1790 | */ | |
a485e1da XS |
1791 | static inline struct slab *alloc_slab_page(gfp_t flags, int node, |
1792 | struct kmem_cache_order_objects oo) | |
65c3376a | 1793 | { |
45387b8c VB |
1794 | struct folio *folio; |
1795 | struct slab *slab; | |
19af27af | 1796 | unsigned int order = oo_order(oo); |
65c3376a | 1797 | |
2154a336 | 1798 | if (node == NUMA_NO_NODE) |
45387b8c | 1799 | folio = (struct folio *)alloc_pages(flags, order); |
65c3376a | 1800 | else |
45387b8c | 1801 | folio = (struct folio *)__alloc_pages_node(node, flags, order); |
5dfb4175 | 1802 | |
45387b8c VB |
1803 | if (!folio) |
1804 | return NULL; | |
1805 | ||
1806 | slab = folio_slab(folio); | |
1807 | __folio_set_slab(folio); | |
1808 | if (page_is_pfmemalloc(folio_page(folio, 0))) | |
1809 | slab_set_pfmemalloc(slab); | |
1810 | ||
1811 | return slab; | |
65c3376a CL |
1812 | } |
1813 | ||
210e7a43 TG |
1814 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
1815 | /* Pre-initialize the random sequence cache */ | |
1816 | static int init_cache_random_seq(struct kmem_cache *s) | |
1817 | { | |
19af27af | 1818 | unsigned int count = oo_objects(s->oo); |
210e7a43 | 1819 | int err; |
210e7a43 | 1820 | |
a810007a SR |
1821 | /* Bailout if already initialised */ |
1822 | if (s->random_seq) | |
1823 | return 0; | |
1824 | ||
210e7a43 TG |
1825 | err = cache_random_seq_create(s, count, GFP_KERNEL); |
1826 | if (err) { | |
1827 | pr_err("SLUB: Unable to initialize free list for %s\n", | |
1828 | s->name); | |
1829 | return err; | |
1830 | } | |
1831 | ||
1832 | /* Transform to an offset on the set of pages */ | |
1833 | if (s->random_seq) { | |
19af27af AD |
1834 | unsigned int i; |
1835 | ||
210e7a43 TG |
1836 | for (i = 0; i < count; i++) |
1837 | s->random_seq[i] *= s->size; | |
1838 | } | |
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | /* Initialize each random sequence freelist per cache */ | |
1843 | static void __init init_freelist_randomization(void) | |
1844 | { | |
1845 | struct kmem_cache *s; | |
1846 | ||
1847 | mutex_lock(&slab_mutex); | |
1848 | ||
1849 | list_for_each_entry(s, &slab_caches, list) | |
1850 | init_cache_random_seq(s); | |
1851 | ||
1852 | mutex_unlock(&slab_mutex); | |
1853 | } | |
1854 | ||
1855 | /* Get the next entry on the pre-computed freelist randomized */ | |
bb192ed9 | 1856 | static void *next_freelist_entry(struct kmem_cache *s, struct slab *slab, |
210e7a43 TG |
1857 | unsigned long *pos, void *start, |
1858 | unsigned long page_limit, | |
1859 | unsigned long freelist_count) | |
1860 | { | |
1861 | unsigned int idx; | |
1862 | ||
1863 | /* | |
1864 | * If the target page allocation failed, the number of objects on the | |
1865 | * page might be smaller than the usual size defined by the cache. | |
1866 | */ | |
1867 | do { | |
1868 | idx = s->random_seq[*pos]; | |
1869 | *pos += 1; | |
1870 | if (*pos >= freelist_count) | |
1871 | *pos = 0; | |
1872 | } while (unlikely(idx >= page_limit)); | |
1873 | ||
1874 | return (char *)start + idx; | |
1875 | } | |
1876 | ||
1877 | /* Shuffle the single linked freelist based on a random pre-computed sequence */ | |
bb192ed9 | 1878 | static bool shuffle_freelist(struct kmem_cache *s, struct slab *slab) |
210e7a43 TG |
1879 | { |
1880 | void *start; | |
1881 | void *cur; | |
1882 | void *next; | |
1883 | unsigned long idx, pos, page_limit, freelist_count; | |
1884 | ||
bb192ed9 | 1885 | if (slab->objects < 2 || !s->random_seq) |
210e7a43 TG |
1886 | return false; |
1887 | ||
1888 | freelist_count = oo_objects(s->oo); | |
81895a65 | 1889 | pos = prandom_u32_max(freelist_count); |
210e7a43 | 1890 | |
bb192ed9 VB |
1891 | page_limit = slab->objects * s->size; |
1892 | start = fixup_red_left(s, slab_address(slab)); | |
210e7a43 TG |
1893 | |
1894 | /* First entry is used as the base of the freelist */ | |
bb192ed9 | 1895 | cur = next_freelist_entry(s, slab, &pos, start, page_limit, |
210e7a43 | 1896 | freelist_count); |
c0f81a94 | 1897 | cur = setup_object(s, cur); |
bb192ed9 | 1898 | slab->freelist = cur; |
210e7a43 | 1899 | |
bb192ed9 VB |
1900 | for (idx = 1; idx < slab->objects; idx++) { |
1901 | next = next_freelist_entry(s, slab, &pos, start, page_limit, | |
210e7a43 | 1902 | freelist_count); |
c0f81a94 | 1903 | next = setup_object(s, next); |
210e7a43 TG |
1904 | set_freepointer(s, cur, next); |
1905 | cur = next; | |
1906 | } | |
210e7a43 TG |
1907 | set_freepointer(s, cur, NULL); |
1908 | ||
1909 | return true; | |
1910 | } | |
1911 | #else | |
1912 | static inline int init_cache_random_seq(struct kmem_cache *s) | |
1913 | { | |
1914 | return 0; | |
1915 | } | |
1916 | static inline void init_freelist_randomization(void) { } | |
bb192ed9 | 1917 | static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab) |
210e7a43 TG |
1918 | { |
1919 | return false; | |
1920 | } | |
1921 | #endif /* CONFIG_SLAB_FREELIST_RANDOM */ | |
1922 | ||
bb192ed9 | 1923 | static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) |
81819f0f | 1924 | { |
bb192ed9 | 1925 | struct slab *slab; |
834f3d11 | 1926 | struct kmem_cache_order_objects oo = s->oo; |
ba52270d | 1927 | gfp_t alloc_gfp; |
4d176711 | 1928 | void *start, *p, *next; |
a50b854e | 1929 | int idx; |
210e7a43 | 1930 | bool shuffle; |
81819f0f | 1931 | |
7e0528da CL |
1932 | flags &= gfp_allowed_mask; |
1933 | ||
b7a49f0d | 1934 | flags |= s->allocflags; |
e12ba74d | 1935 | |
ba52270d PE |
1936 | /* |
1937 | * Let the initial higher-order allocation fail under memory pressure | |
1938 | * so we fall-back to the minimum order allocation. | |
1939 | */ | |
1940 | alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL; | |
d0164adc | 1941 | if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min)) |
27c08f75 | 1942 | alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~__GFP_RECLAIM; |
ba52270d | 1943 | |
a485e1da | 1944 | slab = alloc_slab_page(alloc_gfp, node, oo); |
bb192ed9 | 1945 | if (unlikely(!slab)) { |
65c3376a | 1946 | oo = s->min; |
80c3a998 | 1947 | alloc_gfp = flags; |
65c3376a CL |
1948 | /* |
1949 | * Allocation may have failed due to fragmentation. | |
1950 | * Try a lower order alloc if possible | |
1951 | */ | |
a485e1da | 1952 | slab = alloc_slab_page(alloc_gfp, node, oo); |
bb192ed9 | 1953 | if (unlikely(!slab)) |
c7323a5a | 1954 | return NULL; |
588f8ba9 | 1955 | stat(s, ORDER_FALLBACK); |
65c3376a | 1956 | } |
5a896d9e | 1957 | |
bb192ed9 | 1958 | slab->objects = oo_objects(oo); |
c7323a5a VB |
1959 | slab->inuse = 0; |
1960 | slab->frozen = 0; | |
81819f0f | 1961 | |
bb192ed9 | 1962 | account_slab(slab, oo_order(oo), s, flags); |
1f3147b4 | 1963 | |
bb192ed9 | 1964 | slab->slab_cache = s; |
81819f0f | 1965 | |
6e48a966 | 1966 | kasan_poison_slab(slab); |
81819f0f | 1967 | |
bb192ed9 | 1968 | start = slab_address(slab); |
81819f0f | 1969 | |
bb192ed9 | 1970 | setup_slab_debug(s, slab, start); |
0316bec2 | 1971 | |
bb192ed9 | 1972 | shuffle = shuffle_freelist(s, slab); |
210e7a43 TG |
1973 | |
1974 | if (!shuffle) { | |
4d176711 | 1975 | start = fixup_red_left(s, start); |
c0f81a94 | 1976 | start = setup_object(s, start); |
bb192ed9 VB |
1977 | slab->freelist = start; |
1978 | for (idx = 0, p = start; idx < slab->objects - 1; idx++) { | |
18e50661 | 1979 | next = p + s->size; |
c0f81a94 | 1980 | next = setup_object(s, next); |
18e50661 AK |
1981 | set_freepointer(s, p, next); |
1982 | p = next; | |
1983 | } | |
1984 | set_freepointer(s, p, NULL); | |
81819f0f | 1985 | } |
81819f0f | 1986 | |
bb192ed9 | 1987 | return slab; |
81819f0f CL |
1988 | } |
1989 | ||
bb192ed9 | 1990 | static struct slab *new_slab(struct kmem_cache *s, gfp_t flags, int node) |
588f8ba9 | 1991 | { |
44405099 LL |
1992 | if (unlikely(flags & GFP_SLAB_BUG_MASK)) |
1993 | flags = kmalloc_fix_flags(flags); | |
588f8ba9 | 1994 | |
53a0de06 VB |
1995 | WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO)); |
1996 | ||
588f8ba9 TG |
1997 | return allocate_slab(s, |
1998 | flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node); | |
1999 | } | |
2000 | ||
4020b4a2 | 2001 | static void __free_slab(struct kmem_cache *s, struct slab *slab) |
81819f0f | 2002 | { |
4020b4a2 VB |
2003 | struct folio *folio = slab_folio(slab); |
2004 | int order = folio_order(folio); | |
834f3d11 | 2005 | int pages = 1 << order; |
81819f0f | 2006 | |
8fc8d666 | 2007 | if (kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) { |
81819f0f CL |
2008 | void *p; |
2009 | ||
bb192ed9 | 2010 | slab_pad_check(s, slab); |
4020b4a2 | 2011 | for_each_object(p, s, slab_address(slab), slab->objects) |
bb192ed9 | 2012 | check_object(s, slab, p, SLUB_RED_INACTIVE); |
81819f0f CL |
2013 | } |
2014 | ||
4020b4a2 VB |
2015 | __slab_clear_pfmemalloc(slab); |
2016 | __folio_clear_slab(folio); | |
2017 | folio->mapping = NULL; | |
1eb5ac64 NP |
2018 | if (current->reclaim_state) |
2019 | current->reclaim_state->reclaimed_slab += pages; | |
4020b4a2 VB |
2020 | unaccount_slab(slab, order, s); |
2021 | __free_pages(folio_page(folio, 0), order); | |
81819f0f CL |
2022 | } |
2023 | ||
2024 | static void rcu_free_slab(struct rcu_head *h) | |
2025 | { | |
bb192ed9 | 2026 | struct slab *slab = container_of(h, struct slab, rcu_head); |
da9a638c | 2027 | |
bb192ed9 | 2028 | __free_slab(slab->slab_cache, slab); |
81819f0f CL |
2029 | } |
2030 | ||
bb192ed9 | 2031 | static void free_slab(struct kmem_cache *s, struct slab *slab) |
81819f0f | 2032 | { |
5f0d5a3a | 2033 | if (unlikely(s->flags & SLAB_TYPESAFE_BY_RCU)) { |
bb192ed9 | 2034 | call_rcu(&slab->rcu_head, rcu_free_slab); |
81819f0f | 2035 | } else |
bb192ed9 | 2036 | __free_slab(s, slab); |
81819f0f CL |
2037 | } |
2038 | ||
bb192ed9 | 2039 | static void discard_slab(struct kmem_cache *s, struct slab *slab) |
81819f0f | 2040 | { |
bb192ed9 VB |
2041 | dec_slabs_node(s, slab_nid(slab), slab->objects); |
2042 | free_slab(s, slab); | |
81819f0f CL |
2043 | } |
2044 | ||
2045 | /* | |
5cc6eee8 | 2046 | * Management of partially allocated slabs. |
81819f0f | 2047 | */ |
1e4dd946 | 2048 | static inline void |
bb192ed9 | 2049 | __add_partial(struct kmem_cache_node *n, struct slab *slab, int tail) |
81819f0f | 2050 | { |
e95eed57 | 2051 | n->nr_partial++; |
136333d1 | 2052 | if (tail == DEACTIVATE_TO_TAIL) |
bb192ed9 | 2053 | list_add_tail(&slab->slab_list, &n->partial); |
7c2e132c | 2054 | else |
bb192ed9 | 2055 | list_add(&slab->slab_list, &n->partial); |
81819f0f CL |
2056 | } |
2057 | ||
1e4dd946 | 2058 | static inline void add_partial(struct kmem_cache_node *n, |
bb192ed9 | 2059 | struct slab *slab, int tail) |
62e346a8 | 2060 | { |
c65c1877 | 2061 | lockdep_assert_held(&n->list_lock); |
bb192ed9 | 2062 | __add_partial(n, slab, tail); |
1e4dd946 | 2063 | } |
c65c1877 | 2064 | |
1e4dd946 | 2065 | static inline void remove_partial(struct kmem_cache_node *n, |
bb192ed9 | 2066 | struct slab *slab) |
1e4dd946 SR |
2067 | { |
2068 | lockdep_assert_held(&n->list_lock); | |
bb192ed9 | 2069 | list_del(&slab->slab_list); |
52b4b950 | 2070 | n->nr_partial--; |
1e4dd946 SR |
2071 | } |
2072 | ||
c7323a5a VB |
2073 | /* |
2074 | * Called only for kmem_cache_debug() caches instead of acquire_slab(), with a | |
2075 | * slab from the n->partial list. Remove only a single object from the slab, do | |
2076 | * the alloc_debug_processing() checks and leave the slab on the list, or move | |
2077 | * it to full list if it was the last free object. | |
2078 | */ | |
2079 | static void *alloc_single_from_partial(struct kmem_cache *s, | |
6edf2576 | 2080 | struct kmem_cache_node *n, struct slab *slab, int orig_size) |
c7323a5a VB |
2081 | { |
2082 | void *object; | |
2083 | ||
2084 | lockdep_assert_held(&n->list_lock); | |
2085 | ||
2086 | object = slab->freelist; | |
2087 | slab->freelist = get_freepointer(s, object); | |
2088 | slab->inuse++; | |
2089 | ||
6edf2576 | 2090 | if (!alloc_debug_processing(s, slab, object, orig_size)) { |
c7323a5a VB |
2091 | remove_partial(n, slab); |
2092 | return NULL; | |
2093 | } | |
2094 | ||
2095 | if (slab->inuse == slab->objects) { | |
2096 | remove_partial(n, slab); | |
2097 | add_full(s, n, slab); | |
2098 | } | |
2099 | ||
2100 | return object; | |
2101 | } | |
2102 | ||
2103 | /* | |
2104 | * Called only for kmem_cache_debug() caches to allocate from a freshly | |
2105 | * allocated slab. Allocate a single object instead of whole freelist | |
2106 | * and put the slab to the partial (or full) list. | |
2107 | */ | |
2108 | static void *alloc_single_from_new_slab(struct kmem_cache *s, | |
6edf2576 | 2109 | struct slab *slab, int orig_size) |
c7323a5a VB |
2110 | { |
2111 | int nid = slab_nid(slab); | |
2112 | struct kmem_cache_node *n = get_node(s, nid); | |
2113 | unsigned long flags; | |
2114 | void *object; | |
2115 | ||
2116 | ||
2117 | object = slab->freelist; | |
2118 | slab->freelist = get_freepointer(s, object); | |
2119 | slab->inuse = 1; | |
2120 | ||
6edf2576 | 2121 | if (!alloc_debug_processing(s, slab, object, orig_size)) |
c7323a5a VB |
2122 | /* |
2123 | * It's not really expected that this would fail on a | |
2124 | * freshly allocated slab, but a concurrent memory | |
2125 | * corruption in theory could cause that. | |
2126 | */ | |
2127 | return NULL; | |
2128 | ||
2129 | spin_lock_irqsave(&n->list_lock, flags); | |
2130 | ||
2131 | if (slab->inuse == slab->objects) | |
2132 | add_full(s, n, slab); | |
2133 | else | |
2134 | add_partial(n, slab, DEACTIVATE_TO_HEAD); | |
2135 | ||
2136 | inc_slabs_node(s, nid, slab->objects); | |
2137 | spin_unlock_irqrestore(&n->list_lock, flags); | |
2138 | ||
2139 | return object; | |
2140 | } | |
2141 | ||
81819f0f | 2142 | /* |
7ced3719 CL |
2143 | * Remove slab from the partial list, freeze it and |
2144 | * return the pointer to the freelist. | |
81819f0f | 2145 | * |
497b66f2 | 2146 | * Returns a list of objects or NULL if it fails. |
81819f0f | 2147 | */ |
497b66f2 | 2148 | static inline void *acquire_slab(struct kmem_cache *s, |
bb192ed9 | 2149 | struct kmem_cache_node *n, struct slab *slab, |
b47291ef | 2150 | int mode) |
81819f0f | 2151 | { |
2cfb7455 CL |
2152 | void *freelist; |
2153 | unsigned long counters; | |
bb192ed9 | 2154 | struct slab new; |
2cfb7455 | 2155 | |
c65c1877 PZ |
2156 | lockdep_assert_held(&n->list_lock); |
2157 | ||
2cfb7455 CL |
2158 | /* |
2159 | * Zap the freelist and set the frozen bit. | |
2160 | * The old freelist is the list of objects for the | |
2161 | * per cpu allocation list. | |
2162 | */ | |
bb192ed9 VB |
2163 | freelist = slab->freelist; |
2164 | counters = slab->counters; | |
7ced3719 | 2165 | new.counters = counters; |
23910c50 | 2166 | if (mode) { |
bb192ed9 | 2167 | new.inuse = slab->objects; |
23910c50 PE |
2168 | new.freelist = NULL; |
2169 | } else { | |
2170 | new.freelist = freelist; | |
2171 | } | |
2cfb7455 | 2172 | |
a0132ac0 | 2173 | VM_BUG_ON(new.frozen); |
7ced3719 | 2174 | new.frozen = 1; |
2cfb7455 | 2175 | |
bb192ed9 | 2176 | if (!__cmpxchg_double_slab(s, slab, |
2cfb7455 | 2177 | freelist, counters, |
02d7633f | 2178 | new.freelist, new.counters, |
7ced3719 | 2179 | "acquire_slab")) |
7ced3719 | 2180 | return NULL; |
2cfb7455 | 2181 | |
bb192ed9 | 2182 | remove_partial(n, slab); |
7ced3719 | 2183 | WARN_ON(!freelist); |
49e22585 | 2184 | return freelist; |
81819f0f CL |
2185 | } |
2186 | ||
e0a043aa | 2187 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
bb192ed9 | 2188 | static void put_cpu_partial(struct kmem_cache *s, struct slab *slab, int drain); |
e0a043aa | 2189 | #else |
bb192ed9 | 2190 | static inline void put_cpu_partial(struct kmem_cache *s, struct slab *slab, |
e0a043aa VB |
2191 | int drain) { } |
2192 | #endif | |
01b34d16 | 2193 | static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags); |
49e22585 | 2194 | |
81819f0f | 2195 | /* |
672bba3a | 2196 | * Try to allocate a partial slab from a specific node. |
81819f0f | 2197 | */ |
8ba00bb6 | 2198 | static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n, |
6edf2576 | 2199 | struct partial_context *pc) |
81819f0f | 2200 | { |
bb192ed9 | 2201 | struct slab *slab, *slab2; |
49e22585 | 2202 | void *object = NULL; |
4b1f449d | 2203 | unsigned long flags; |
bb192ed9 | 2204 | unsigned int partial_slabs = 0; |
81819f0f CL |
2205 | |
2206 | /* | |
2207 | * Racy check. If we mistakenly see no partial slabs then we | |
2208 | * just allocate an empty slab. If we mistakenly try to get a | |
70b6d25e | 2209 | * partial slab and there is none available then get_partial() |
672bba3a | 2210 | * will return NULL. |
81819f0f CL |
2211 | */ |
2212 | if (!n || !n->nr_partial) | |
2213 | return NULL; | |
2214 | ||
4b1f449d | 2215 | spin_lock_irqsave(&n->list_lock, flags); |
bb192ed9 | 2216 | list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) { |
8ba00bb6 | 2217 | void *t; |
49e22585 | 2218 | |
6edf2576 | 2219 | if (!pfmemalloc_match(slab, pc->flags)) |
8ba00bb6 JK |
2220 | continue; |
2221 | ||
c7323a5a | 2222 | if (kmem_cache_debug(s)) { |
6edf2576 FT |
2223 | object = alloc_single_from_partial(s, n, slab, |
2224 | pc->orig_size); | |
c7323a5a VB |
2225 | if (object) |
2226 | break; | |
2227 | continue; | |
2228 | } | |
2229 | ||
bb192ed9 | 2230 | t = acquire_slab(s, n, slab, object == NULL); |
49e22585 | 2231 | if (!t) |
9b1ea29b | 2232 | break; |
49e22585 | 2233 | |
12d79634 | 2234 | if (!object) { |
6edf2576 | 2235 | *pc->slab = slab; |
49e22585 | 2236 | stat(s, ALLOC_FROM_PARTIAL); |
49e22585 | 2237 | object = t; |
49e22585 | 2238 | } else { |
bb192ed9 | 2239 | put_cpu_partial(s, slab, 0); |
8028dcea | 2240 | stat(s, CPU_PARTIAL_NODE); |
bb192ed9 | 2241 | partial_slabs++; |
49e22585 | 2242 | } |
b47291ef | 2243 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
345c905d | 2244 | if (!kmem_cache_has_cpu_partial(s) |
bb192ed9 | 2245 | || partial_slabs > s->cpu_partial_slabs / 2) |
49e22585 | 2246 | break; |
b47291ef VB |
2247 | #else |
2248 | break; | |
2249 | #endif | |
49e22585 | 2250 | |
497b66f2 | 2251 | } |
4b1f449d | 2252 | spin_unlock_irqrestore(&n->list_lock, flags); |
497b66f2 | 2253 | return object; |
81819f0f CL |
2254 | } |
2255 | ||
2256 | /* | |
c2092c12 | 2257 | * Get a slab from somewhere. Search in increasing NUMA distances. |
81819f0f | 2258 | */ |
6edf2576 | 2259 | static void *get_any_partial(struct kmem_cache *s, struct partial_context *pc) |
81819f0f CL |
2260 | { |
2261 | #ifdef CONFIG_NUMA | |
2262 | struct zonelist *zonelist; | |
dd1a239f | 2263 | struct zoneref *z; |
54a6eb5c | 2264 | struct zone *zone; |
6edf2576 | 2265 | enum zone_type highest_zoneidx = gfp_zone(pc->flags); |
497b66f2 | 2266 | void *object; |
cc9a6c87 | 2267 | unsigned int cpuset_mems_cookie; |
81819f0f CL |
2268 | |
2269 | /* | |
672bba3a CL |
2270 | * The defrag ratio allows a configuration of the tradeoffs between |
2271 | * inter node defragmentation and node local allocations. A lower | |
2272 | * defrag_ratio increases the tendency to do local allocations | |
2273 | * instead of attempting to obtain partial slabs from other nodes. | |
81819f0f | 2274 | * |
672bba3a CL |
2275 | * If the defrag_ratio is set to 0 then kmalloc() always |
2276 | * returns node local objects. If the ratio is higher then kmalloc() | |
2277 | * may return off node objects because partial slabs are obtained | |
2278 | * from other nodes and filled up. | |
81819f0f | 2279 | * |
43efd3ea LP |
2280 | * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100 |
2281 | * (which makes defrag_ratio = 1000) then every (well almost) | |
2282 | * allocation will first attempt to defrag slab caches on other nodes. | |
2283 | * This means scanning over all nodes to look for partial slabs which | |
2284 | * may be expensive if we do it every time we are trying to find a slab | |
672bba3a | 2285 | * with available objects. |
81819f0f | 2286 | */ |
9824601e CL |
2287 | if (!s->remote_node_defrag_ratio || |
2288 | get_cycles() % 1024 > s->remote_node_defrag_ratio) | |
81819f0f CL |
2289 | return NULL; |
2290 | ||
cc9a6c87 | 2291 | do { |
d26914d1 | 2292 | cpuset_mems_cookie = read_mems_allowed_begin(); |
6edf2576 | 2293 | zonelist = node_zonelist(mempolicy_slab_node(), pc->flags); |
97a225e6 | 2294 | for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) { |
cc9a6c87 MG |
2295 | struct kmem_cache_node *n; |
2296 | ||
2297 | n = get_node(s, zone_to_nid(zone)); | |
2298 | ||
6edf2576 | 2299 | if (n && cpuset_zone_allowed(zone, pc->flags) && |
cc9a6c87 | 2300 | n->nr_partial > s->min_partial) { |
6edf2576 | 2301 | object = get_partial_node(s, n, pc); |
cc9a6c87 MG |
2302 | if (object) { |
2303 | /* | |
d26914d1 MG |
2304 | * Don't check read_mems_allowed_retry() |
2305 | * here - if mems_allowed was updated in | |
2306 | * parallel, that was a harmless race | |
2307 | * between allocation and the cpuset | |
2308 | * update | |
cc9a6c87 | 2309 | */ |
cc9a6c87 MG |
2310 | return object; |
2311 | } | |
c0ff7453 | 2312 | } |
81819f0f | 2313 | } |
d26914d1 | 2314 | } while (read_mems_allowed_retry(cpuset_mems_cookie)); |
6dfd1b65 | 2315 | #endif /* CONFIG_NUMA */ |
81819f0f CL |
2316 | return NULL; |
2317 | } | |
2318 | ||
2319 | /* | |
c2092c12 | 2320 | * Get a partial slab, lock it and return it. |
81819f0f | 2321 | */ |
6edf2576 | 2322 | static void *get_partial(struct kmem_cache *s, int node, struct partial_context *pc) |
81819f0f | 2323 | { |
497b66f2 | 2324 | void *object; |
a561ce00 JK |
2325 | int searchnode = node; |
2326 | ||
2327 | if (node == NUMA_NO_NODE) | |
2328 | searchnode = numa_mem_id(); | |
81819f0f | 2329 | |
6edf2576 | 2330 | object = get_partial_node(s, get_node(s, searchnode), pc); |
497b66f2 CL |
2331 | if (object || node != NUMA_NO_NODE) |
2332 | return object; | |
81819f0f | 2333 | |
6edf2576 | 2334 | return get_any_partial(s, pc); |
81819f0f CL |
2335 | } |
2336 | ||
923717cb | 2337 | #ifdef CONFIG_PREEMPTION |
8a5ec0ba | 2338 | /* |
0d645ed1 | 2339 | * Calculate the next globally unique transaction for disambiguation |
8a5ec0ba CL |
2340 | * during cmpxchg. The transactions start with the cpu number and are then |
2341 | * incremented by CONFIG_NR_CPUS. | |
2342 | */ | |
2343 | #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS) | |
2344 | #else | |
2345 | /* | |
2346 | * No preemption supported therefore also no need to check for | |
2347 | * different cpus. | |
2348 | */ | |
2349 | #define TID_STEP 1 | |
2350 | #endif | |
2351 | ||
2352 | static inline unsigned long next_tid(unsigned long tid) | |
2353 | { | |
2354 | return tid + TID_STEP; | |
2355 | } | |
2356 | ||
9d5f0be0 | 2357 | #ifdef SLUB_DEBUG_CMPXCHG |
8a5ec0ba CL |
2358 | static inline unsigned int tid_to_cpu(unsigned long tid) |
2359 | { | |
2360 | return tid % TID_STEP; | |
2361 | } | |
2362 | ||
2363 | static inline unsigned long tid_to_event(unsigned long tid) | |
2364 | { | |
2365 | return tid / TID_STEP; | |
2366 | } | |
9d5f0be0 | 2367 | #endif |
8a5ec0ba CL |
2368 | |
2369 | static inline unsigned int init_tid(int cpu) | |
2370 | { | |
2371 | return cpu; | |
2372 | } | |
2373 | ||
2374 | static inline void note_cmpxchg_failure(const char *n, | |
2375 | const struct kmem_cache *s, unsigned long tid) | |
2376 | { | |
2377 | #ifdef SLUB_DEBUG_CMPXCHG | |
2378 | unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid); | |
2379 | ||
f9f58285 | 2380 | pr_info("%s %s: cmpxchg redo ", n, s->name); |
8a5ec0ba | 2381 | |
923717cb | 2382 | #ifdef CONFIG_PREEMPTION |
8a5ec0ba | 2383 | if (tid_to_cpu(tid) != tid_to_cpu(actual_tid)) |
f9f58285 | 2384 | pr_warn("due to cpu change %d -> %d\n", |
8a5ec0ba CL |
2385 | tid_to_cpu(tid), tid_to_cpu(actual_tid)); |
2386 | else | |
2387 | #endif | |
2388 | if (tid_to_event(tid) != tid_to_event(actual_tid)) | |
f9f58285 | 2389 | pr_warn("due to cpu running other code. Event %ld->%ld\n", |
8a5ec0ba CL |
2390 | tid_to_event(tid), tid_to_event(actual_tid)); |
2391 | else | |
f9f58285 | 2392 | pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n", |
8a5ec0ba CL |
2393 | actual_tid, tid, next_tid(tid)); |
2394 | #endif | |
4fdccdfb | 2395 | stat(s, CMPXCHG_DOUBLE_CPU_FAIL); |
8a5ec0ba CL |
2396 | } |
2397 | ||
788e1aad | 2398 | static void init_kmem_cache_cpus(struct kmem_cache *s) |
8a5ec0ba | 2399 | { |
8a5ec0ba | 2400 | int cpu; |
bd0e7491 | 2401 | struct kmem_cache_cpu *c; |
8a5ec0ba | 2402 | |
bd0e7491 VB |
2403 | for_each_possible_cpu(cpu) { |
2404 | c = per_cpu_ptr(s->cpu_slab, cpu); | |
2405 | local_lock_init(&c->lock); | |
2406 | c->tid = init_tid(cpu); | |
2407 | } | |
8a5ec0ba | 2408 | } |
2cfb7455 | 2409 | |
81819f0f | 2410 | /* |
c2092c12 | 2411 | * Finishes removing the cpu slab. Merges cpu's freelist with slab's freelist, |
a019d201 VB |
2412 | * unfreezes the slabs and puts it on the proper list. |
2413 | * Assumes the slab has been already safely taken away from kmem_cache_cpu | |
2414 | * by the caller. | |
81819f0f | 2415 | */ |
bb192ed9 | 2416 | static void deactivate_slab(struct kmem_cache *s, struct slab *slab, |
a019d201 | 2417 | void *freelist) |
81819f0f | 2418 | { |
6d3a16d0 | 2419 | enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE, M_FULL_NOLIST }; |
bb192ed9 | 2420 | struct kmem_cache_node *n = get_node(s, slab_nid(slab)); |
6d3a16d0 HY |
2421 | int free_delta = 0; |
2422 | enum slab_modes mode = M_NONE; | |
d930ff03 | 2423 | void *nextfree, *freelist_iter, *freelist_tail; |
136333d1 | 2424 | int tail = DEACTIVATE_TO_HEAD; |
3406e91b | 2425 | unsigned long flags = 0; |
bb192ed9 VB |
2426 | struct slab new; |
2427 | struct slab old; | |
2cfb7455 | 2428 | |
bb192ed9 | 2429 | if (slab->freelist) { |
84e554e6 | 2430 | stat(s, DEACTIVATE_REMOTE_FREES); |
136333d1 | 2431 | tail = DEACTIVATE_TO_TAIL; |
2cfb7455 CL |
2432 | } |
2433 | ||
894b8788 | 2434 | /* |
d930ff03 VB |
2435 | * Stage one: Count the objects on cpu's freelist as free_delta and |
2436 | * remember the last object in freelist_tail for later splicing. | |
2cfb7455 | 2437 | */ |
d930ff03 VB |
2438 | freelist_tail = NULL; |
2439 | freelist_iter = freelist; | |
2440 | while (freelist_iter) { | |
2441 | nextfree = get_freepointer(s, freelist_iter); | |
2cfb7455 | 2442 | |
52f23478 DZ |
2443 | /* |
2444 | * If 'nextfree' is invalid, it is possible that the object at | |
d930ff03 VB |
2445 | * 'freelist_iter' is already corrupted. So isolate all objects |
2446 | * starting at 'freelist_iter' by skipping them. | |
52f23478 | 2447 | */ |
bb192ed9 | 2448 | if (freelist_corrupted(s, slab, &freelist_iter, nextfree)) |
52f23478 DZ |
2449 | break; |
2450 | ||
d930ff03 VB |
2451 | freelist_tail = freelist_iter; |
2452 | free_delta++; | |
2cfb7455 | 2453 | |
d930ff03 | 2454 | freelist_iter = nextfree; |
2cfb7455 CL |
2455 | } |
2456 | ||
894b8788 | 2457 | /* |
c2092c12 VB |
2458 | * Stage two: Unfreeze the slab while splicing the per-cpu |
2459 | * freelist to the head of slab's freelist. | |
d930ff03 | 2460 | * |
c2092c12 | 2461 | * Ensure that the slab is unfrozen while the list presence |
d930ff03 | 2462 | * reflects the actual number of objects during unfreeze. |
2cfb7455 | 2463 | * |
6d3a16d0 HY |
2464 | * We first perform cmpxchg holding lock and insert to list |
2465 | * when it succeed. If there is mismatch then the slab is not | |
2466 | * unfrozen and number of objects in the slab may have changed. | |
2467 | * Then release lock and retry cmpxchg again. | |
894b8788 | 2468 | */ |
2cfb7455 | 2469 | redo: |
894b8788 | 2470 | |
bb192ed9 VB |
2471 | old.freelist = READ_ONCE(slab->freelist); |
2472 | old.counters = READ_ONCE(slab->counters); | |
a0132ac0 | 2473 | VM_BUG_ON(!old.frozen); |
7c2e132c | 2474 | |
2cfb7455 CL |
2475 | /* Determine target state of the slab */ |
2476 | new.counters = old.counters; | |
d930ff03 VB |
2477 | if (freelist_tail) { |
2478 | new.inuse -= free_delta; | |
2479 | set_freepointer(s, freelist_tail, old.freelist); | |
2cfb7455 CL |
2480 | new.freelist = freelist; |
2481 | } else | |
2482 | new.freelist = old.freelist; | |
2483 | ||
2484 | new.frozen = 0; | |
2485 | ||
6d3a16d0 HY |
2486 | if (!new.inuse && n->nr_partial >= s->min_partial) { |
2487 | mode = M_FREE; | |
2488 | } else if (new.freelist) { | |
2489 | mode = M_PARTIAL; | |
2490 | /* | |
2491 | * Taking the spinlock removes the possibility that | |
2492 | * acquire_slab() will see a slab that is frozen | |
2493 | */ | |
2494 | spin_lock_irqsave(&n->list_lock, flags); | |
2495 | } else if (kmem_cache_debug_flags(s, SLAB_STORE_USER)) { | |
2496 | mode = M_FULL; | |
2497 | /* | |
2498 | * This also ensures that the scanning of full | |
2499 | * slabs from diagnostic functions will not see | |
2500 | * any frozen slabs. | |
2501 | */ | |
2502 | spin_lock_irqsave(&n->list_lock, flags); | |
2cfb7455 | 2503 | } else { |
6d3a16d0 | 2504 | mode = M_FULL_NOLIST; |
2cfb7455 CL |
2505 | } |
2506 | ||
2cfb7455 | 2507 | |
bb192ed9 | 2508 | if (!cmpxchg_double_slab(s, slab, |
2cfb7455 CL |
2509 | old.freelist, old.counters, |
2510 | new.freelist, new.counters, | |
6d3a16d0 HY |
2511 | "unfreezing slab")) { |
2512 | if (mode == M_PARTIAL || mode == M_FULL) | |
2513 | spin_unlock_irqrestore(&n->list_lock, flags); | |
2cfb7455 | 2514 | goto redo; |
6d3a16d0 | 2515 | } |
2cfb7455 | 2516 | |
2cfb7455 | 2517 | |
6d3a16d0 HY |
2518 | if (mode == M_PARTIAL) { |
2519 | add_partial(n, slab, tail); | |
2520 | spin_unlock_irqrestore(&n->list_lock, flags); | |
88349a28 | 2521 | stat(s, tail); |
6d3a16d0 | 2522 | } else if (mode == M_FREE) { |
2cfb7455 | 2523 | stat(s, DEACTIVATE_EMPTY); |
bb192ed9 | 2524 | discard_slab(s, slab); |
2cfb7455 | 2525 | stat(s, FREE_SLAB); |
6d3a16d0 HY |
2526 | } else if (mode == M_FULL) { |
2527 | add_full(s, n, slab); | |
2528 | spin_unlock_irqrestore(&n->list_lock, flags); | |
2529 | stat(s, DEACTIVATE_FULL); | |
2530 | } else if (mode == M_FULL_NOLIST) { | |
2531 | stat(s, DEACTIVATE_FULL); | |
894b8788 | 2532 | } |
81819f0f CL |
2533 | } |
2534 | ||
345c905d | 2535 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
bb192ed9 | 2536 | static void __unfreeze_partials(struct kmem_cache *s, struct slab *partial_slab) |
fc1455f4 | 2537 | { |
43d77867 | 2538 | struct kmem_cache_node *n = NULL, *n2 = NULL; |
bb192ed9 | 2539 | struct slab *slab, *slab_to_discard = NULL; |
7cf9f3ba | 2540 | unsigned long flags = 0; |
49e22585 | 2541 | |
bb192ed9 VB |
2542 | while (partial_slab) { |
2543 | struct slab new; | |
2544 | struct slab old; | |
49e22585 | 2545 | |
bb192ed9 VB |
2546 | slab = partial_slab; |
2547 | partial_slab = slab->next; | |
43d77867 | 2548 | |
bb192ed9 | 2549 | n2 = get_node(s, slab_nid(slab)); |
43d77867 JK |
2550 | if (n != n2) { |
2551 | if (n) | |
7cf9f3ba | 2552 | spin_unlock_irqrestore(&n->list_lock, flags); |
43d77867 JK |
2553 | |
2554 | n = n2; | |
7cf9f3ba | 2555 | spin_lock_irqsave(&n->list_lock, flags); |
43d77867 | 2556 | } |
49e22585 CL |
2557 | |
2558 | do { | |
2559 | ||
bb192ed9 VB |
2560 | old.freelist = slab->freelist; |
2561 | old.counters = slab->counters; | |
a0132ac0 | 2562 | VM_BUG_ON(!old.frozen); |
49e22585 CL |
2563 | |
2564 | new.counters = old.counters; | |
2565 | new.freelist = old.freelist; | |
2566 | ||
2567 | new.frozen = 0; | |
2568 | ||
bb192ed9 | 2569 | } while (!__cmpxchg_double_slab(s, slab, |
49e22585 CL |
2570 | old.freelist, old.counters, |
2571 | new.freelist, new.counters, | |
2572 | "unfreezing slab")); | |
2573 | ||
8a5b20ae | 2574 | if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) { |
bb192ed9 VB |
2575 | slab->next = slab_to_discard; |
2576 | slab_to_discard = slab; | |
43d77867 | 2577 | } else { |
bb192ed9 | 2578 | add_partial(n, slab, DEACTIVATE_TO_TAIL); |
43d77867 | 2579 | stat(s, FREE_ADD_PARTIAL); |
49e22585 CL |
2580 | } |
2581 | } | |
2582 | ||
2583 | if (n) | |
7cf9f3ba | 2584 | spin_unlock_irqrestore(&n->list_lock, flags); |
8de06a6f | 2585 | |
bb192ed9 VB |
2586 | while (slab_to_discard) { |
2587 | slab = slab_to_discard; | |
2588 | slab_to_discard = slab_to_discard->next; | |
9ada1934 SL |
2589 | |
2590 | stat(s, DEACTIVATE_EMPTY); | |
bb192ed9 | 2591 | discard_slab(s, slab); |
9ada1934 SL |
2592 | stat(s, FREE_SLAB); |
2593 | } | |
fc1455f4 | 2594 | } |
f3ab8b6b | 2595 | |
fc1455f4 VB |
2596 | /* |
2597 | * Unfreeze all the cpu partial slabs. | |
2598 | */ | |
2599 | static void unfreeze_partials(struct kmem_cache *s) | |
2600 | { | |
bb192ed9 | 2601 | struct slab *partial_slab; |
fc1455f4 VB |
2602 | unsigned long flags; |
2603 | ||
bd0e7491 | 2604 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
bb192ed9 | 2605 | partial_slab = this_cpu_read(s->cpu_slab->partial); |
fc1455f4 | 2606 | this_cpu_write(s->cpu_slab->partial, NULL); |
bd0e7491 | 2607 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
fc1455f4 | 2608 | |
bb192ed9 VB |
2609 | if (partial_slab) |
2610 | __unfreeze_partials(s, partial_slab); | |
fc1455f4 VB |
2611 | } |
2612 | ||
2613 | static void unfreeze_partials_cpu(struct kmem_cache *s, | |
2614 | struct kmem_cache_cpu *c) | |
2615 | { | |
bb192ed9 | 2616 | struct slab *partial_slab; |
fc1455f4 | 2617 | |
bb192ed9 | 2618 | partial_slab = slub_percpu_partial(c); |
fc1455f4 VB |
2619 | c->partial = NULL; |
2620 | ||
bb192ed9 VB |
2621 | if (partial_slab) |
2622 | __unfreeze_partials(s, partial_slab); | |
49e22585 CL |
2623 | } |
2624 | ||
2625 | /* | |
c2092c12 VB |
2626 | * Put a slab that was just frozen (in __slab_free|get_partial_node) into a |
2627 | * partial slab slot if available. | |
49e22585 CL |
2628 | * |
2629 | * If we did not find a slot then simply move all the partials to the | |
2630 | * per node partial list. | |
2631 | */ | |
bb192ed9 | 2632 | static void put_cpu_partial(struct kmem_cache *s, struct slab *slab, int drain) |
49e22585 | 2633 | { |
bb192ed9 VB |
2634 | struct slab *oldslab; |
2635 | struct slab *slab_to_unfreeze = NULL; | |
e0a043aa | 2636 | unsigned long flags; |
bb192ed9 | 2637 | int slabs = 0; |
49e22585 | 2638 | |
bd0e7491 | 2639 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
49e22585 | 2640 | |
bb192ed9 | 2641 | oldslab = this_cpu_read(s->cpu_slab->partial); |
e0a043aa | 2642 | |
bb192ed9 VB |
2643 | if (oldslab) { |
2644 | if (drain && oldslab->slabs >= s->cpu_partial_slabs) { | |
e0a043aa VB |
2645 | /* |
2646 | * Partial array is full. Move the existing set to the | |
2647 | * per node partial list. Postpone the actual unfreezing | |
2648 | * outside of the critical section. | |
2649 | */ | |
bb192ed9 VB |
2650 | slab_to_unfreeze = oldslab; |
2651 | oldslab = NULL; | |
e0a043aa | 2652 | } else { |
bb192ed9 | 2653 | slabs = oldslab->slabs; |
49e22585 | 2654 | } |
e0a043aa | 2655 | } |
49e22585 | 2656 | |
bb192ed9 | 2657 | slabs++; |
49e22585 | 2658 | |
bb192ed9 VB |
2659 | slab->slabs = slabs; |
2660 | slab->next = oldslab; | |
49e22585 | 2661 | |
bb192ed9 | 2662 | this_cpu_write(s->cpu_slab->partial, slab); |
e0a043aa | 2663 | |
bd0e7491 | 2664 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
e0a043aa | 2665 | |
bb192ed9 VB |
2666 | if (slab_to_unfreeze) { |
2667 | __unfreeze_partials(s, slab_to_unfreeze); | |
e0a043aa VB |
2668 | stat(s, CPU_PARTIAL_DRAIN); |
2669 | } | |
49e22585 CL |
2670 | } |
2671 | ||
e0a043aa VB |
2672 | #else /* CONFIG_SLUB_CPU_PARTIAL */ |
2673 | ||
2674 | static inline void unfreeze_partials(struct kmem_cache *s) { } | |
2675 | static inline void unfreeze_partials_cpu(struct kmem_cache *s, | |
2676 | struct kmem_cache_cpu *c) { } | |
2677 | ||
2678 | #endif /* CONFIG_SLUB_CPU_PARTIAL */ | |
2679 | ||
dfb4f096 | 2680 | static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c) |
81819f0f | 2681 | { |
5a836bf6 | 2682 | unsigned long flags; |
bb192ed9 | 2683 | struct slab *slab; |
5a836bf6 SAS |
2684 | void *freelist; |
2685 | ||
bd0e7491 | 2686 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
5a836bf6 | 2687 | |
bb192ed9 | 2688 | slab = c->slab; |
5a836bf6 | 2689 | freelist = c->freelist; |
c17dda40 | 2690 | |
bb192ed9 | 2691 | c->slab = NULL; |
a019d201 | 2692 | c->freelist = NULL; |
c17dda40 | 2693 | c->tid = next_tid(c->tid); |
a019d201 | 2694 | |
bd0e7491 | 2695 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
a019d201 | 2696 | |
bb192ed9 VB |
2697 | if (slab) { |
2698 | deactivate_slab(s, slab, freelist); | |
5a836bf6 SAS |
2699 | stat(s, CPUSLAB_FLUSH); |
2700 | } | |
81819f0f CL |
2701 | } |
2702 | ||
0c710013 | 2703 | static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu) |
81819f0f | 2704 | { |
9dfc6e68 | 2705 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu); |
08beb547 | 2706 | void *freelist = c->freelist; |
bb192ed9 | 2707 | struct slab *slab = c->slab; |
81819f0f | 2708 | |
bb192ed9 | 2709 | c->slab = NULL; |
08beb547 VB |
2710 | c->freelist = NULL; |
2711 | c->tid = next_tid(c->tid); | |
2712 | ||
bb192ed9 VB |
2713 | if (slab) { |
2714 | deactivate_slab(s, slab, freelist); | |
08beb547 VB |
2715 | stat(s, CPUSLAB_FLUSH); |
2716 | } | |
49e22585 | 2717 | |
fc1455f4 | 2718 | unfreeze_partials_cpu(s, c); |
81819f0f CL |
2719 | } |
2720 | ||
5a836bf6 SAS |
2721 | struct slub_flush_work { |
2722 | struct work_struct work; | |
2723 | struct kmem_cache *s; | |
2724 | bool skip; | |
2725 | }; | |
2726 | ||
fc1455f4 VB |
2727 | /* |
2728 | * Flush cpu slab. | |
2729 | * | |
5a836bf6 | 2730 | * Called from CPU work handler with migration disabled. |
fc1455f4 | 2731 | */ |
5a836bf6 | 2732 | static void flush_cpu_slab(struct work_struct *w) |
81819f0f | 2733 | { |
5a836bf6 SAS |
2734 | struct kmem_cache *s; |
2735 | struct kmem_cache_cpu *c; | |
2736 | struct slub_flush_work *sfw; | |
2737 | ||
2738 | sfw = container_of(w, struct slub_flush_work, work); | |
2739 | ||
2740 | s = sfw->s; | |
2741 | c = this_cpu_ptr(s->cpu_slab); | |
fc1455f4 | 2742 | |
bb192ed9 | 2743 | if (c->slab) |
fc1455f4 | 2744 | flush_slab(s, c); |
81819f0f | 2745 | |
fc1455f4 | 2746 | unfreeze_partials(s); |
81819f0f CL |
2747 | } |
2748 | ||
5a836bf6 | 2749 | static bool has_cpu_slab(int cpu, struct kmem_cache *s) |
a8364d55 | 2750 | { |
a8364d55 GBY |
2751 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu); |
2752 | ||
bb192ed9 | 2753 | return c->slab || slub_percpu_partial(c); |
a8364d55 GBY |
2754 | } |
2755 | ||
5a836bf6 SAS |
2756 | static DEFINE_MUTEX(flush_lock); |
2757 | static DEFINE_PER_CPU(struct slub_flush_work, slub_flush); | |
2758 | ||
2759 | static void flush_all_cpus_locked(struct kmem_cache *s) | |
2760 | { | |
2761 | struct slub_flush_work *sfw; | |
2762 | unsigned int cpu; | |
2763 | ||
2764 | lockdep_assert_cpus_held(); | |
2765 | mutex_lock(&flush_lock); | |
2766 | ||
2767 | for_each_online_cpu(cpu) { | |
2768 | sfw = &per_cpu(slub_flush, cpu); | |
2769 | if (!has_cpu_slab(cpu, s)) { | |
2770 | sfw->skip = true; | |
2771 | continue; | |
2772 | } | |
2773 | INIT_WORK(&sfw->work, flush_cpu_slab); | |
2774 | sfw->skip = false; | |
2775 | sfw->s = s; | |
e45cc288 | 2776 | queue_work_on(cpu, flushwq, &sfw->work); |
5a836bf6 SAS |
2777 | } |
2778 | ||
2779 | for_each_online_cpu(cpu) { | |
2780 | sfw = &per_cpu(slub_flush, cpu); | |
2781 | if (sfw->skip) | |
2782 | continue; | |
2783 | flush_work(&sfw->work); | |
2784 | } | |
2785 | ||
2786 | mutex_unlock(&flush_lock); | |
2787 | } | |
2788 | ||
81819f0f CL |
2789 | static void flush_all(struct kmem_cache *s) |
2790 | { | |
5a836bf6 SAS |
2791 | cpus_read_lock(); |
2792 | flush_all_cpus_locked(s); | |
2793 | cpus_read_unlock(); | |
81819f0f CL |
2794 | } |
2795 | ||
a96a87bf SAS |
2796 | /* |
2797 | * Use the cpu notifier to insure that the cpu slabs are flushed when | |
2798 | * necessary. | |
2799 | */ | |
2800 | static int slub_cpu_dead(unsigned int cpu) | |
2801 | { | |
2802 | struct kmem_cache *s; | |
a96a87bf SAS |
2803 | |
2804 | mutex_lock(&slab_mutex); | |
0e7ac738 | 2805 | list_for_each_entry(s, &slab_caches, list) |
a96a87bf | 2806 | __flush_cpu_slab(s, cpu); |
a96a87bf SAS |
2807 | mutex_unlock(&slab_mutex); |
2808 | return 0; | |
2809 | } | |
2810 | ||
dfb4f096 CL |
2811 | /* |
2812 | * Check if the objects in a per cpu structure fit numa | |
2813 | * locality expectations. | |
2814 | */ | |
bb192ed9 | 2815 | static inline int node_match(struct slab *slab, int node) |
dfb4f096 CL |
2816 | { |
2817 | #ifdef CONFIG_NUMA | |
bb192ed9 | 2818 | if (node != NUMA_NO_NODE && slab_nid(slab) != node) |
dfb4f096 CL |
2819 | return 0; |
2820 | #endif | |
2821 | return 1; | |
2822 | } | |
2823 | ||
9a02d699 | 2824 | #ifdef CONFIG_SLUB_DEBUG |
bb192ed9 | 2825 | static int count_free(struct slab *slab) |
781b2ba6 | 2826 | { |
bb192ed9 | 2827 | return slab->objects - slab->inuse; |
781b2ba6 PE |
2828 | } |
2829 | ||
9a02d699 DR |
2830 | static inline unsigned long node_nr_objs(struct kmem_cache_node *n) |
2831 | { | |
2832 | return atomic_long_read(&n->total_objects); | |
2833 | } | |
a579b056 VB |
2834 | |
2835 | /* Supports checking bulk free of a constructed freelist */ | |
c7323a5a | 2836 | static noinline void free_debug_processing( |
a579b056 VB |
2837 | struct kmem_cache *s, struct slab *slab, |
2838 | void *head, void *tail, int bulk_cnt, | |
2839 | unsigned long addr) | |
2840 | { | |
2841 | struct kmem_cache_node *n = get_node(s, slab_nid(slab)); | |
c7323a5a | 2842 | struct slab *slab_free = NULL; |
a579b056 VB |
2843 | void *object = head; |
2844 | int cnt = 0; | |
c7323a5a VB |
2845 | unsigned long flags; |
2846 | bool checks_ok = false; | |
a579b056 VB |
2847 | depot_stack_handle_t handle = 0; |
2848 | ||
2849 | if (s->flags & SLAB_STORE_USER) | |
2850 | handle = set_track_prepare(); | |
2851 | ||
2852 | spin_lock_irqsave(&n->list_lock, flags); | |
a579b056 VB |
2853 | |
2854 | if (s->flags & SLAB_CONSISTENCY_CHECKS) { | |
2855 | if (!check_slab(s, slab)) | |
2856 | goto out; | |
2857 | } | |
2858 | ||
c7323a5a VB |
2859 | if (slab->inuse < bulk_cnt) { |
2860 | slab_err(s, slab, "Slab has %d allocated objects but %d are to be freed\n", | |
2861 | slab->inuse, bulk_cnt); | |
2862 | goto out; | |
2863 | } | |
2864 | ||
a579b056 | 2865 | next_object: |
c7323a5a VB |
2866 | |
2867 | if (++cnt > bulk_cnt) | |
2868 | goto out_cnt; | |
a579b056 VB |
2869 | |
2870 | if (s->flags & SLAB_CONSISTENCY_CHECKS) { | |
2871 | if (!free_consistency_checks(s, slab, object, addr)) | |
2872 | goto out; | |
2873 | } | |
2874 | ||
2875 | if (s->flags & SLAB_STORE_USER) | |
2876 | set_track_update(s, object, TRACK_FREE, addr, handle); | |
2877 | trace(s, slab, object, 0); | |
2878 | /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */ | |
2879 | init_object(s, object, SLUB_RED_INACTIVE); | |
2880 | ||
2881 | /* Reached end of constructed freelist yet? */ | |
2882 | if (object != tail) { | |
2883 | object = get_freepointer(s, object); | |
2884 | goto next_object; | |
2885 | } | |
c7323a5a | 2886 | checks_ok = true; |
a579b056 | 2887 | |
c7323a5a | 2888 | out_cnt: |
a579b056 | 2889 | if (cnt != bulk_cnt) |
c7323a5a | 2890 | slab_err(s, slab, "Bulk free expected %d objects but found %d\n", |
a579b056 VB |
2891 | bulk_cnt, cnt); |
2892 | ||
c7323a5a VB |
2893 | out: |
2894 | if (checks_ok) { | |
2895 | void *prior = slab->freelist; | |
2896 | ||
2897 | /* Perform the actual freeing while we still hold the locks */ | |
2898 | slab->inuse -= cnt; | |
2899 | set_freepointer(s, tail, prior); | |
2900 | slab->freelist = head; | |
2901 | ||
b731e357 FT |
2902 | /* |
2903 | * If the slab is empty, and node's partial list is full, | |
2904 | * it should be discarded anyway no matter it's on full or | |
2905 | * partial list. | |
2906 | */ | |
2907 | if (slab->inuse == 0 && n->nr_partial >= s->min_partial) | |
2908 | slab_free = slab; | |
2909 | ||
c7323a5a | 2910 | if (!prior) { |
b731e357 | 2911 | /* was on full list */ |
c7323a5a | 2912 | remove_full(s, n, slab); |
b731e357 FT |
2913 | if (!slab_free) { |
2914 | add_partial(n, slab, DEACTIVATE_TO_TAIL); | |
2915 | stat(s, FREE_ADD_PARTIAL); | |
2916 | } | |
2917 | } else if (slab_free) { | |
c7323a5a VB |
2918 | remove_partial(n, slab); |
2919 | stat(s, FREE_REMOVE_PARTIAL); | |
2920 | } | |
c7323a5a VB |
2921 | } |
2922 | ||
2923 | if (slab_free) { | |
2924 | /* | |
2925 | * Update the counters while still holding n->list_lock to | |
2926 | * prevent spurious validation warnings | |
2927 | */ | |
2928 | dec_slabs_node(s, slab_nid(slab_free), slab_free->objects); | |
2929 | } | |
2930 | ||
a579b056 | 2931 | spin_unlock_irqrestore(&n->list_lock, flags); |
c7323a5a VB |
2932 | |
2933 | if (!checks_ok) | |
a579b056 | 2934 | slab_fix(s, "Object at 0x%p not freed", object); |
c7323a5a VB |
2935 | |
2936 | if (slab_free) { | |
2937 | stat(s, FREE_SLAB); | |
2938 | free_slab(s, slab_free); | |
2939 | } | |
a579b056 | 2940 | } |
9a02d699 DR |
2941 | #endif /* CONFIG_SLUB_DEBUG */ |
2942 | ||
b1a413a3 | 2943 | #if defined(CONFIG_SLUB_DEBUG) || defined(SLAB_SUPPORTS_SYSFS) |
781b2ba6 | 2944 | static unsigned long count_partial(struct kmem_cache_node *n, |
bb192ed9 | 2945 | int (*get_count)(struct slab *)) |
781b2ba6 PE |
2946 | { |
2947 | unsigned long flags; | |
2948 | unsigned long x = 0; | |
bb192ed9 | 2949 | struct slab *slab; |
781b2ba6 PE |
2950 | |
2951 | spin_lock_irqsave(&n->list_lock, flags); | |
bb192ed9 VB |
2952 | list_for_each_entry(slab, &n->partial, slab_list) |
2953 | x += get_count(slab); | |
781b2ba6 PE |
2954 | spin_unlock_irqrestore(&n->list_lock, flags); |
2955 | return x; | |
2956 | } | |
b1a413a3 | 2957 | #endif /* CONFIG_SLUB_DEBUG || SLAB_SUPPORTS_SYSFS */ |
26c02cf0 | 2958 | |
781b2ba6 PE |
2959 | static noinline void |
2960 | slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid) | |
2961 | { | |
9a02d699 DR |
2962 | #ifdef CONFIG_SLUB_DEBUG |
2963 | static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL, | |
2964 | DEFAULT_RATELIMIT_BURST); | |
781b2ba6 | 2965 | int node; |
fa45dc25 | 2966 | struct kmem_cache_node *n; |
781b2ba6 | 2967 | |
9a02d699 DR |
2968 | if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs)) |
2969 | return; | |
2970 | ||
5b3810e5 VB |
2971 | pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n", |
2972 | nid, gfpflags, &gfpflags); | |
19af27af | 2973 | pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n", |
f9f58285 FF |
2974 | s->name, s->object_size, s->size, oo_order(s->oo), |
2975 | oo_order(s->min)); | |
781b2ba6 | 2976 | |
3b0efdfa | 2977 | if (oo_order(s->min) > get_order(s->object_size)) |
f9f58285 FF |
2978 | pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n", |
2979 | s->name); | |
fa5ec8a1 | 2980 | |
fa45dc25 | 2981 | for_each_kmem_cache_node(s, node, n) { |
781b2ba6 PE |
2982 | unsigned long nr_slabs; |
2983 | unsigned long nr_objs; | |
2984 | unsigned long nr_free; | |
2985 | ||
26c02cf0 AB |
2986 | nr_free = count_partial(n, count_free); |
2987 | nr_slabs = node_nr_slabs(n); | |
2988 | nr_objs = node_nr_objs(n); | |
781b2ba6 | 2989 | |
f9f58285 | 2990 | pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n", |
781b2ba6 PE |
2991 | node, nr_slabs, nr_objs, nr_free); |
2992 | } | |
9a02d699 | 2993 | #endif |
781b2ba6 PE |
2994 | } |
2995 | ||
01b34d16 | 2996 | static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags) |
072bb0aa | 2997 | { |
01b34d16 | 2998 | if (unlikely(slab_test_pfmemalloc(slab))) |
0b303fb4 VB |
2999 | return gfp_pfmemalloc_allowed(gfpflags); |
3000 | ||
3001 | return true; | |
3002 | } | |
3003 | ||
213eeb9f | 3004 | /* |
c2092c12 VB |
3005 | * Check the slab->freelist and either transfer the freelist to the |
3006 | * per cpu freelist or deactivate the slab. | |
213eeb9f | 3007 | * |
c2092c12 | 3008 | * The slab is still frozen if the return value is not NULL. |
213eeb9f | 3009 | * |
c2092c12 | 3010 | * If this function returns NULL then the slab has been unfrozen. |
213eeb9f | 3011 | */ |
bb192ed9 | 3012 | static inline void *get_freelist(struct kmem_cache *s, struct slab *slab) |
213eeb9f | 3013 | { |
bb192ed9 | 3014 | struct slab new; |
213eeb9f CL |
3015 | unsigned long counters; |
3016 | void *freelist; | |
3017 | ||
bd0e7491 VB |
3018 | lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock)); |
3019 | ||
213eeb9f | 3020 | do { |
bb192ed9 VB |
3021 | freelist = slab->freelist; |
3022 | counters = slab->counters; | |
6faa6833 | 3023 | |
213eeb9f | 3024 | new.counters = counters; |
a0132ac0 | 3025 | VM_BUG_ON(!new.frozen); |
213eeb9f | 3026 | |
bb192ed9 | 3027 | new.inuse = slab->objects; |
213eeb9f CL |
3028 | new.frozen = freelist != NULL; |
3029 | ||
bb192ed9 | 3030 | } while (!__cmpxchg_double_slab(s, slab, |
213eeb9f CL |
3031 | freelist, counters, |
3032 | NULL, new.counters, | |
3033 | "get_freelist")); | |
3034 | ||
3035 | return freelist; | |
3036 | } | |
3037 | ||
81819f0f | 3038 | /* |
894b8788 CL |
3039 | * Slow path. The lockless freelist is empty or we need to perform |
3040 | * debugging duties. | |
3041 | * | |
894b8788 CL |
3042 | * Processing is still very fast if new objects have been freed to the |
3043 | * regular freelist. In that case we simply take over the regular freelist | |
3044 | * as the lockless freelist and zap the regular freelist. | |
81819f0f | 3045 | * |
894b8788 CL |
3046 | * If that is not working then we fall back to the partial lists. We take the |
3047 | * first element of the freelist as the object to allocate now and move the | |
3048 | * rest of the freelist to the lockless freelist. | |
81819f0f | 3049 | * |
894b8788 | 3050 | * And if we were unable to get a new slab from the partial slab lists then |
6446faa2 CL |
3051 | * we need to allocate a new slab. This is the slowest path since it involves |
3052 | * a call to the page allocator and the setup of a new slab. | |
a380a3c7 | 3053 | * |
e500059b | 3054 | * Version of __slab_alloc to use when we know that preemption is |
a380a3c7 | 3055 | * already disabled (which is the case for bulk allocation). |
81819f0f | 3056 | */ |
a380a3c7 | 3057 | static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, |
6edf2576 | 3058 | unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size) |
81819f0f | 3059 | { |
6faa6833 | 3060 | void *freelist; |
bb192ed9 | 3061 | struct slab *slab; |
e500059b | 3062 | unsigned long flags; |
6edf2576 | 3063 | struct partial_context pc; |
81819f0f | 3064 | |
9f986d99 AW |
3065 | stat(s, ALLOC_SLOWPATH); |
3066 | ||
c2092c12 | 3067 | reread_slab: |
0b303fb4 | 3068 | |
bb192ed9 VB |
3069 | slab = READ_ONCE(c->slab); |
3070 | if (!slab) { | |
0715e6c5 VB |
3071 | /* |
3072 | * if the node is not online or has no normal memory, just | |
3073 | * ignore the node constraint | |
3074 | */ | |
3075 | if (unlikely(node != NUMA_NO_NODE && | |
7e1fa93d | 3076 | !node_isset(node, slab_nodes))) |
0715e6c5 | 3077 | node = NUMA_NO_NODE; |
81819f0f | 3078 | goto new_slab; |
0715e6c5 | 3079 | } |
49e22585 | 3080 | redo: |
6faa6833 | 3081 | |
bb192ed9 | 3082 | if (unlikely(!node_match(slab, node))) { |
0715e6c5 VB |
3083 | /* |
3084 | * same as above but node_match() being false already | |
3085 | * implies node != NUMA_NO_NODE | |
3086 | */ | |
7e1fa93d | 3087 | if (!node_isset(node, slab_nodes)) { |
0715e6c5 | 3088 | node = NUMA_NO_NODE; |
0715e6c5 | 3089 | } else { |
a561ce00 | 3090 | stat(s, ALLOC_NODE_MISMATCH); |
0b303fb4 | 3091 | goto deactivate_slab; |
a561ce00 | 3092 | } |
fc59c053 | 3093 | } |
6446faa2 | 3094 | |
072bb0aa MG |
3095 | /* |
3096 | * By rights, we should be searching for a slab page that was | |
3097 | * PFMEMALLOC but right now, we are losing the pfmemalloc | |
3098 | * information when the page leaves the per-cpu allocator | |
3099 | */ | |
bb192ed9 | 3100 | if (unlikely(!pfmemalloc_match(slab, gfpflags))) |
0b303fb4 | 3101 | goto deactivate_slab; |
072bb0aa | 3102 | |
c2092c12 | 3103 | /* must check again c->slab in case we got preempted and it changed */ |
bd0e7491 | 3104 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
bb192ed9 | 3105 | if (unlikely(slab != c->slab)) { |
bd0e7491 | 3106 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
c2092c12 | 3107 | goto reread_slab; |
0b303fb4 | 3108 | } |
6faa6833 CL |
3109 | freelist = c->freelist; |
3110 | if (freelist) | |
73736e03 | 3111 | goto load_freelist; |
03e404af | 3112 | |
bb192ed9 | 3113 | freelist = get_freelist(s, slab); |
6446faa2 | 3114 | |
6faa6833 | 3115 | if (!freelist) { |
bb192ed9 | 3116 | c->slab = NULL; |
eeaa345e | 3117 | c->tid = next_tid(c->tid); |
bd0e7491 | 3118 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
03e404af | 3119 | stat(s, DEACTIVATE_BYPASS); |
fc59c053 | 3120 | goto new_slab; |
03e404af | 3121 | } |
6446faa2 | 3122 | |
84e554e6 | 3123 | stat(s, ALLOC_REFILL); |
6446faa2 | 3124 | |
894b8788 | 3125 | load_freelist: |
0b303fb4 | 3126 | |
bd0e7491 | 3127 | lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock)); |
0b303fb4 | 3128 | |
507effea CL |
3129 | /* |
3130 | * freelist is pointing to the list of objects to be used. | |
c2092c12 VB |
3131 | * slab is pointing to the slab from which the objects are obtained. |
3132 | * That slab must be frozen for per cpu allocations to work. | |
507effea | 3133 | */ |
bb192ed9 | 3134 | VM_BUG_ON(!c->slab->frozen); |
6faa6833 | 3135 | c->freelist = get_freepointer(s, freelist); |
8a5ec0ba | 3136 | c->tid = next_tid(c->tid); |
bd0e7491 | 3137 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
6faa6833 | 3138 | return freelist; |
81819f0f | 3139 | |
0b303fb4 VB |
3140 | deactivate_slab: |
3141 | ||
bd0e7491 | 3142 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
bb192ed9 | 3143 | if (slab != c->slab) { |
bd0e7491 | 3144 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
c2092c12 | 3145 | goto reread_slab; |
0b303fb4 | 3146 | } |
a019d201 | 3147 | freelist = c->freelist; |
bb192ed9 | 3148 | c->slab = NULL; |
a019d201 | 3149 | c->freelist = NULL; |
eeaa345e | 3150 | c->tid = next_tid(c->tid); |
bd0e7491 | 3151 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
bb192ed9 | 3152 | deactivate_slab(s, slab, freelist); |
0b303fb4 | 3153 | |
81819f0f | 3154 | new_slab: |
2cfb7455 | 3155 | |
a93cf07b | 3156 | if (slub_percpu_partial(c)) { |
bd0e7491 | 3157 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
bb192ed9 | 3158 | if (unlikely(c->slab)) { |
bd0e7491 | 3159 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
c2092c12 | 3160 | goto reread_slab; |
fa417ab7 | 3161 | } |
4b1f449d | 3162 | if (unlikely(!slub_percpu_partial(c))) { |
bd0e7491 | 3163 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
25c00c50 VB |
3164 | /* we were preempted and partial list got empty */ |
3165 | goto new_objects; | |
4b1f449d | 3166 | } |
fa417ab7 | 3167 | |
bb192ed9 VB |
3168 | slab = c->slab = slub_percpu_partial(c); |
3169 | slub_set_percpu_partial(c, slab); | |
bd0e7491 | 3170 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
49e22585 | 3171 | stat(s, CPU_PARTIAL_ALLOC); |
49e22585 | 3172 | goto redo; |
81819f0f CL |
3173 | } |
3174 | ||
fa417ab7 VB |
3175 | new_objects: |
3176 | ||
6edf2576 FT |
3177 | pc.flags = gfpflags; |
3178 | pc.slab = &slab; | |
3179 | pc.orig_size = orig_size; | |
3180 | freelist = get_partial(s, node, &pc); | |
3f2b77e3 | 3181 | if (freelist) |
c2092c12 | 3182 | goto check_new_slab; |
2a904905 | 3183 | |
25c00c50 | 3184 | slub_put_cpu_ptr(s->cpu_slab); |
bb192ed9 | 3185 | slab = new_slab(s, gfpflags, node); |
25c00c50 | 3186 | c = slub_get_cpu_ptr(s->cpu_slab); |
01ad8a7b | 3187 | |
bb192ed9 | 3188 | if (unlikely(!slab)) { |
9a02d699 | 3189 | slab_out_of_memory(s, gfpflags, node); |
f4697436 | 3190 | return NULL; |
81819f0f | 3191 | } |
2cfb7455 | 3192 | |
c7323a5a VB |
3193 | stat(s, ALLOC_SLAB); |
3194 | ||
3195 | if (kmem_cache_debug(s)) { | |
6edf2576 | 3196 | freelist = alloc_single_from_new_slab(s, slab, orig_size); |
c7323a5a VB |
3197 | |
3198 | if (unlikely(!freelist)) | |
3199 | goto new_objects; | |
3200 | ||
3201 | if (s->flags & SLAB_STORE_USER) | |
3202 | set_track(s, freelist, TRACK_ALLOC, addr); | |
3203 | ||
3204 | return freelist; | |
3205 | } | |
3206 | ||
53a0de06 | 3207 | /* |
c2092c12 | 3208 | * No other reference to the slab yet so we can |
53a0de06 VB |
3209 | * muck around with it freely without cmpxchg |
3210 | */ | |
bb192ed9 VB |
3211 | freelist = slab->freelist; |
3212 | slab->freelist = NULL; | |
c7323a5a VB |
3213 | slab->inuse = slab->objects; |
3214 | slab->frozen = 1; | |
53a0de06 | 3215 | |
c7323a5a | 3216 | inc_slabs_node(s, slab_nid(slab), slab->objects); |
53a0de06 | 3217 | |
c2092c12 | 3218 | check_new_slab: |
2cfb7455 | 3219 | |
1572df7c | 3220 | if (kmem_cache_debug(s)) { |
c7323a5a VB |
3221 | /* |
3222 | * For debug caches here we had to go through | |
3223 | * alloc_single_from_partial() so just store the tracking info | |
3224 | * and return the object | |
3225 | */ | |
3226 | if (s->flags & SLAB_STORE_USER) | |
3227 | set_track(s, freelist, TRACK_ALLOC, addr); | |
6edf2576 | 3228 | |
c7323a5a | 3229 | return freelist; |
1572df7c VB |
3230 | } |
3231 | ||
c7323a5a | 3232 | if (unlikely(!pfmemalloc_match(slab, gfpflags))) { |
1572df7c VB |
3233 | /* |
3234 | * For !pfmemalloc_match() case we don't load freelist so that | |
3235 | * we don't make further mismatched allocations easier. | |
3236 | */ | |
c7323a5a VB |
3237 | deactivate_slab(s, slab, get_freepointer(s, freelist)); |
3238 | return freelist; | |
3239 | } | |
1572df7c | 3240 | |
c2092c12 | 3241 | retry_load_slab: |
cfdf836e | 3242 | |
bd0e7491 | 3243 | local_lock_irqsave(&s->cpu_slab->lock, flags); |
bb192ed9 | 3244 | if (unlikely(c->slab)) { |
cfdf836e | 3245 | void *flush_freelist = c->freelist; |
bb192ed9 | 3246 | struct slab *flush_slab = c->slab; |
cfdf836e | 3247 | |
bb192ed9 | 3248 | c->slab = NULL; |
cfdf836e VB |
3249 | c->freelist = NULL; |
3250 | c->tid = next_tid(c->tid); | |
3251 | ||
bd0e7491 | 3252 | local_unlock_irqrestore(&s->cpu_slab->lock, flags); |
cfdf836e | 3253 | |
bb192ed9 | 3254 | deactivate_slab(s, flush_slab, flush_freelist); |
cfdf836e VB |
3255 | |
3256 | stat(s, CPUSLAB_FLUSH); | |
3257 | ||
c2092c12 | 3258 | goto retry_load_slab; |
cfdf836e | 3259 | } |
bb192ed9 | 3260 | c->slab = slab; |
3f2b77e3 | 3261 | |
1572df7c | 3262 | goto load_freelist; |
894b8788 CL |
3263 | } |
3264 | ||
a380a3c7 | 3265 | /* |
e500059b VB |
3266 | * A wrapper for ___slab_alloc() for contexts where preemption is not yet |
3267 | * disabled. Compensates for possible cpu changes by refetching the per cpu area | |
3268 | * pointer. | |
a380a3c7 CL |
3269 | */ |
3270 | static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, | |
6edf2576 | 3271 | unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size) |
a380a3c7 CL |
3272 | { |
3273 | void *p; | |
a380a3c7 | 3274 | |
e500059b | 3275 | #ifdef CONFIG_PREEMPT_COUNT |
a380a3c7 CL |
3276 | /* |
3277 | * We may have been preempted and rescheduled on a different | |
e500059b | 3278 | * cpu before disabling preemption. Need to reload cpu area |
a380a3c7 CL |
3279 | * pointer. |
3280 | */ | |
25c00c50 | 3281 | c = slub_get_cpu_ptr(s->cpu_slab); |
a380a3c7 CL |
3282 | #endif |
3283 | ||
6edf2576 | 3284 | p = ___slab_alloc(s, gfpflags, node, addr, c, orig_size); |
e500059b | 3285 | #ifdef CONFIG_PREEMPT_COUNT |
25c00c50 | 3286 | slub_put_cpu_ptr(s->cpu_slab); |
e500059b | 3287 | #endif |
a380a3c7 CL |
3288 | return p; |
3289 | } | |
3290 | ||
0f181f9f AP |
3291 | /* |
3292 | * If the object has been wiped upon free, make sure it's fully initialized by | |
3293 | * zeroing out freelist pointer. | |
3294 | */ | |
3295 | static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s, | |
3296 | void *obj) | |
3297 | { | |
3298 | if (unlikely(slab_want_init_on_free(s)) && obj) | |
ce5716c6 AK |
3299 | memset((void *)((char *)kasan_reset_tag(obj) + s->offset), |
3300 | 0, sizeof(void *)); | |
0f181f9f AP |
3301 | } |
3302 | ||
894b8788 CL |
3303 | /* |
3304 | * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc) | |
3305 | * have the fastpath folded into their functions. So no function call | |
3306 | * overhead for requests that can be satisfied on the fastpath. | |
3307 | * | |
3308 | * The fastpath works by first checking if the lockless freelist can be used. | |
3309 | * If not then __slab_alloc is called for slow processing. | |
3310 | * | |
3311 | * Otherwise we can simply pick the next object from the lockless free list. | |
3312 | */ | |
88f2ef73 | 3313 | static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru, |
b89fb5ef | 3314 | gfp_t gfpflags, int node, unsigned long addr, size_t orig_size) |
894b8788 | 3315 | { |
03ec0ed5 | 3316 | void *object; |
dfb4f096 | 3317 | struct kmem_cache_cpu *c; |
bb192ed9 | 3318 | struct slab *slab; |
8a5ec0ba | 3319 | unsigned long tid; |
964d4bd3 | 3320 | struct obj_cgroup *objcg = NULL; |
da844b78 | 3321 | bool init = false; |
1f84260c | 3322 | |
88f2ef73 | 3323 | s = slab_pre_alloc_hook(s, lru, &objcg, 1, gfpflags); |
8135be5a | 3324 | if (!s) |
773ff60e | 3325 | return NULL; |
b89fb5ef AP |
3326 | |
3327 | object = kfence_alloc(s, orig_size, gfpflags); | |
3328 | if (unlikely(object)) | |
3329 | goto out; | |
3330 | ||
8a5ec0ba | 3331 | redo: |
8a5ec0ba CL |
3332 | /* |
3333 | * Must read kmem_cache cpu data via this cpu ptr. Preemption is | |
3334 | * enabled. We may switch back and forth between cpus while | |
3335 | * reading from one cpu area. That does not matter as long | |
3336 | * as we end up on the original cpu again when doing the cmpxchg. | |
7cccd80b | 3337 | * |
9b4bc85a VB |
3338 | * We must guarantee that tid and kmem_cache_cpu are retrieved on the |
3339 | * same cpu. We read first the kmem_cache_cpu pointer and use it to read | |
3340 | * the tid. If we are preempted and switched to another cpu between the | |
3341 | * two reads, it's OK as the two are still associated with the same cpu | |
3342 | * and cmpxchg later will validate the cpu. | |
8a5ec0ba | 3343 | */ |
9b4bc85a VB |
3344 | c = raw_cpu_ptr(s->cpu_slab); |
3345 | tid = READ_ONCE(c->tid); | |
9aabf810 JK |
3346 | |
3347 | /* | |
3348 | * Irqless object alloc/free algorithm used here depends on sequence | |
3349 | * of fetching cpu_slab's data. tid should be fetched before anything | |
c2092c12 | 3350 | * on c to guarantee that object and slab associated with previous tid |
9aabf810 | 3351 | * won't be used with current tid. If we fetch tid first, object and |
c2092c12 | 3352 | * slab could be one associated with next tid and our alloc/free |
9aabf810 JK |
3353 | * request will be failed. In this case, we will retry. So, no problem. |
3354 | */ | |
3355 | barrier(); | |
8a5ec0ba | 3356 | |
8a5ec0ba CL |
3357 | /* |
3358 | * The transaction ids are globally unique per cpu and per operation on | |
3359 | * a per cpu queue. Thus they can be guarantee that the cmpxchg_double | |
3360 | * occurs on the right processor and that there was no operation on the | |
3361 | * linked list in between. | |
3362 | */ | |
8a5ec0ba | 3363 | |
9dfc6e68 | 3364 | object = c->freelist; |
bb192ed9 | 3365 | slab = c->slab; |
1f04b07d TG |
3366 | |
3367 | if (!USE_LOCKLESS_FAST_PATH() || | |
bb192ed9 | 3368 | unlikely(!object || !slab || !node_match(slab, node))) { |
6edf2576 | 3369 | object = __slab_alloc(s, gfpflags, node, addr, c, orig_size); |
8eae1492 | 3370 | } else { |
0ad9500e ED |
3371 | void *next_object = get_freepointer_safe(s, object); |
3372 | ||
8a5ec0ba | 3373 | /* |
25985edc | 3374 | * The cmpxchg will only match if there was no additional |
8a5ec0ba CL |
3375 | * operation and if we are on the right processor. |
3376 | * | |
d0e0ac97 CG |
3377 | * The cmpxchg does the following atomically (without lock |
3378 | * semantics!) | |
8a5ec0ba CL |
3379 | * 1. Relocate first pointer to the current per cpu area. |
3380 | * 2. Verify that tid and freelist have not been changed | |
3381 | * 3. If they were not changed replace tid and freelist | |
3382 | * | |
d0e0ac97 CG |
3383 | * Since this is without lock semantics the protection is only |
3384 | * against code executing on this cpu *not* from access by | |
3385 | * other cpus. | |
8a5ec0ba | 3386 | */ |
933393f5 | 3387 | if (unlikely(!this_cpu_cmpxchg_double( |
8a5ec0ba CL |
3388 | s->cpu_slab->freelist, s->cpu_slab->tid, |
3389 | object, tid, | |
0ad9500e | 3390 | next_object, next_tid(tid)))) { |
8a5ec0ba CL |
3391 | |
3392 | note_cmpxchg_failure("slab_alloc", s, tid); | |
3393 | goto redo; | |
3394 | } | |
0ad9500e | 3395 | prefetch_freepointer(s, next_object); |
84e554e6 | 3396 | stat(s, ALLOC_FASTPATH); |
894b8788 | 3397 | } |
0f181f9f | 3398 | |
ce5716c6 | 3399 | maybe_wipe_obj_freeptr(s, object); |
da844b78 | 3400 | init = slab_want_init_on_alloc(gfpflags, s); |
d07dbea4 | 3401 | |
b89fb5ef | 3402 | out: |
da844b78 | 3403 | slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init); |
5a896d9e | 3404 | |
894b8788 | 3405 | return object; |
81819f0f CL |
3406 | } |
3407 | ||
88f2ef73 | 3408 | static __always_inline void *slab_alloc(struct kmem_cache *s, struct list_lru *lru, |
b89fb5ef | 3409 | gfp_t gfpflags, unsigned long addr, size_t orig_size) |
2b847c3c | 3410 | { |
88f2ef73 | 3411 | return slab_alloc_node(s, lru, gfpflags, NUMA_NO_NODE, addr, orig_size); |
2b847c3c EG |
3412 | } |
3413 | ||
88f2ef73 MS |
3414 | static __always_inline |
3415 | void *__kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, | |
3416 | gfp_t gfpflags) | |
81819f0f | 3417 | { |
88f2ef73 | 3418 | void *ret = slab_alloc(s, lru, gfpflags, _RET_IP_, s->object_size); |
5b882be4 | 3419 | |
2c1d697f | 3420 | trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, NUMA_NO_NODE); |
5b882be4 EGM |
3421 | |
3422 | return ret; | |
81819f0f | 3423 | } |
88f2ef73 MS |
3424 | |
3425 | void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags) | |
3426 | { | |
3427 | return __kmem_cache_alloc_lru(s, NULL, gfpflags); | |
3428 | } | |
81819f0f CL |
3429 | EXPORT_SYMBOL(kmem_cache_alloc); |
3430 | ||
88f2ef73 MS |
3431 | void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, |
3432 | gfp_t gfpflags) | |
3433 | { | |
3434 | return __kmem_cache_alloc_lru(s, lru, gfpflags); | |
3435 | } | |
3436 | EXPORT_SYMBOL(kmem_cache_alloc_lru); | |
3437 | ||
ed4cd17e HY |
3438 | void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, |
3439 | int node, size_t orig_size, | |
3440 | unsigned long caller) | |
4a92379b | 3441 | { |
ed4cd17e HY |
3442 | return slab_alloc_node(s, NULL, gfpflags, node, |
3443 | caller, orig_size); | |
4a92379b | 3444 | } |
5b882be4 | 3445 | |
81819f0f CL |
3446 | void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) |
3447 | { | |
88f2ef73 | 3448 | void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, s->object_size); |
5b882be4 | 3449 | |
2c1d697f | 3450 | trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, node); |
5b882be4 EGM |
3451 | |
3452 | return ret; | |
81819f0f CL |
3453 | } |
3454 | EXPORT_SYMBOL(kmem_cache_alloc_node); | |
81819f0f CL |
3455 | |
3456 | /* | |
94e4d712 | 3457 | * Slow path handling. This may still be called frequently since objects |
894b8788 | 3458 | * have a longer lifetime than the cpu slabs in most processing loads. |
81819f0f | 3459 | * |
894b8788 | 3460 | * So we still attempt to reduce cache line usage. Just take the slab |
c2092c12 | 3461 | * lock and free the item. If there is no additional partial slab |
894b8788 | 3462 | * handling required then we can return immediately. |
81819f0f | 3463 | */ |
bb192ed9 | 3464 | static void __slab_free(struct kmem_cache *s, struct slab *slab, |
81084651 JDB |
3465 | void *head, void *tail, int cnt, |
3466 | unsigned long addr) | |
3467 | ||
81819f0f CL |
3468 | { |
3469 | void *prior; | |
2cfb7455 | 3470 | int was_frozen; |
bb192ed9 | 3471 | struct slab new; |
2cfb7455 CL |
3472 | unsigned long counters; |
3473 | struct kmem_cache_node *n = NULL; | |
3f649ab7 | 3474 | unsigned long flags; |
81819f0f | 3475 | |
8a5ec0ba | 3476 | stat(s, FREE_SLOWPATH); |
81819f0f | 3477 | |
b89fb5ef AP |
3478 | if (kfence_free(head)) |
3479 | return; | |
3480 | ||
c7323a5a VB |
3481 | if (kmem_cache_debug(s)) { |
3482 | free_debug_processing(s, slab, head, tail, cnt, addr); | |
80f08c19 | 3483 | return; |
c7323a5a | 3484 | } |
6446faa2 | 3485 | |
2cfb7455 | 3486 | do { |
837d678d JK |
3487 | if (unlikely(n)) { |
3488 | spin_unlock_irqrestore(&n->list_lock, flags); | |
3489 | n = NULL; | |
3490 | } | |
bb192ed9 VB |
3491 | prior = slab->freelist; |
3492 | counters = slab->counters; | |
81084651 | 3493 | set_freepointer(s, tail, prior); |
2cfb7455 CL |
3494 | new.counters = counters; |
3495 | was_frozen = new.frozen; | |
81084651 | 3496 | new.inuse -= cnt; |
837d678d | 3497 | if ((!new.inuse || !prior) && !was_frozen) { |
49e22585 | 3498 | |
c65c1877 | 3499 | if (kmem_cache_has_cpu_partial(s) && !prior) { |
49e22585 CL |
3500 | |
3501 | /* | |
d0e0ac97 CG |
3502 | * Slab was on no list before and will be |
3503 | * partially empty | |
3504 | * We can defer the list move and instead | |
3505 | * freeze it. | |
49e22585 CL |
3506 | */ |
3507 | new.frozen = 1; | |
3508 | ||
c65c1877 | 3509 | } else { /* Needs to be taken off a list */ |
49e22585 | 3510 | |
bb192ed9 | 3511 | n = get_node(s, slab_nid(slab)); |
49e22585 CL |
3512 | /* |
3513 | * Speculatively acquire the list_lock. | |
3514 | * If the cmpxchg does not succeed then we may | |
3515 | * drop the list_lock without any processing. | |
3516 | * | |
3517 | * Otherwise the list_lock will synchronize with | |
3518 | * other processors updating the list of slabs. | |
3519 | */ | |
3520 | spin_lock_irqsave(&n->list_lock, flags); | |
3521 | ||
3522 | } | |
2cfb7455 | 3523 | } |
81819f0f | 3524 | |
bb192ed9 | 3525 | } while (!cmpxchg_double_slab(s, slab, |
2cfb7455 | 3526 | prior, counters, |
81084651 | 3527 | head, new.counters, |
2cfb7455 | 3528 | "__slab_free")); |
81819f0f | 3529 | |
2cfb7455 | 3530 | if (likely(!n)) { |
49e22585 | 3531 | |
c270cf30 AW |
3532 | if (likely(was_frozen)) { |
3533 | /* | |
3534 | * The list lock was not taken therefore no list | |
3535 | * activity can be necessary. | |
3536 | */ | |
3537 | stat(s, FREE_FROZEN); | |
3538 | } else if (new.frozen) { | |
3539 | /* | |
c2092c12 | 3540 | * If we just froze the slab then put it onto the |
c270cf30 AW |
3541 | * per cpu partial list. |
3542 | */ | |
bb192ed9 | 3543 | put_cpu_partial(s, slab, 1); |
8028dcea AS |
3544 | stat(s, CPU_PARTIAL_FREE); |
3545 | } | |
c270cf30 | 3546 | |
b455def2 L |
3547 | return; |
3548 | } | |
81819f0f | 3549 | |
8a5b20ae | 3550 | if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) |
837d678d JK |
3551 | goto slab_empty; |
3552 | ||
81819f0f | 3553 | /* |
837d678d JK |
3554 | * Objects left in the slab. If it was not on the partial list before |
3555 | * then add it. | |
81819f0f | 3556 | */ |
345c905d | 3557 | if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) { |
bb192ed9 VB |
3558 | remove_full(s, n, slab); |
3559 | add_partial(n, slab, DEACTIVATE_TO_TAIL); | |
837d678d | 3560 | stat(s, FREE_ADD_PARTIAL); |
8ff12cfc | 3561 | } |
80f08c19 | 3562 | spin_unlock_irqrestore(&n->list_lock, flags); |
81819f0f CL |
3563 | return; |
3564 | ||
3565 | slab_empty: | |
a973e9dd | 3566 | if (prior) { |
81819f0f | 3567 | /* |
6fbabb20 | 3568 | * Slab on the partial list. |
81819f0f | 3569 | */ |
bb192ed9 | 3570 | remove_partial(n, slab); |
84e554e6 | 3571 | stat(s, FREE_REMOVE_PARTIAL); |
c65c1877 | 3572 | } else { |
6fbabb20 | 3573 | /* Slab must be on the full list */ |
bb192ed9 | 3574 | remove_full(s, n, slab); |
c65c1877 | 3575 | } |
2cfb7455 | 3576 | |
80f08c19 | 3577 | spin_unlock_irqrestore(&n->list_lock, flags); |
84e554e6 | 3578 | stat(s, FREE_SLAB); |
bb192ed9 | 3579 | discard_slab(s, slab); |
81819f0f CL |
3580 | } |
3581 | ||
894b8788 CL |
3582 | /* |
3583 | * Fastpath with forced inlining to produce a kfree and kmem_cache_free that | |
3584 | * can perform fastpath freeing without additional function calls. | |
3585 | * | |
3586 | * The fastpath is only possible if we are freeing to the current cpu slab | |
3587 | * of this processor. This typically the case if we have just allocated | |
3588 | * the item before. | |
3589 | * | |
3590 | * If fastpath is not possible then fall back to __slab_free where we deal | |
3591 | * with all sorts of special processing. | |
81084651 JDB |
3592 | * |
3593 | * Bulk free of a freelist with several objects (all pointing to the | |
c2092c12 | 3594 | * same slab) possible by specifying head and tail ptr, plus objects |
81084651 | 3595 | * count (cnt). Bulk free indicated by tail pointer being set. |
894b8788 | 3596 | */ |
80a9201a | 3597 | static __always_inline void do_slab_free(struct kmem_cache *s, |
bb192ed9 | 3598 | struct slab *slab, void *head, void *tail, |
80a9201a | 3599 | int cnt, unsigned long addr) |
894b8788 | 3600 | { |
81084651 | 3601 | void *tail_obj = tail ? : head; |
dfb4f096 | 3602 | struct kmem_cache_cpu *c; |
8a5ec0ba | 3603 | unsigned long tid; |
1f04b07d | 3604 | void **freelist; |
964d4bd3 | 3605 | |
8a5ec0ba CL |
3606 | redo: |
3607 | /* | |
3608 | * Determine the currently cpus per cpu slab. | |
3609 | * The cpu may change afterward. However that does not matter since | |
3610 | * data is retrieved via this pointer. If we are on the same cpu | |
2ae44005 | 3611 | * during the cmpxchg then the free will succeed. |
8a5ec0ba | 3612 | */ |
9b4bc85a VB |
3613 | c = raw_cpu_ptr(s->cpu_slab); |
3614 | tid = READ_ONCE(c->tid); | |
c016b0bd | 3615 | |
9aabf810 JK |
3616 | /* Same with comment on barrier() in slab_alloc_node() */ |
3617 | barrier(); | |
c016b0bd | 3618 | |
1f04b07d TG |
3619 | if (unlikely(slab != c->slab)) { |
3620 | __slab_free(s, slab, head, tail_obj, cnt, addr); | |
3621 | return; | |
3622 | } | |
3623 | ||
3624 | if (USE_LOCKLESS_FAST_PATH()) { | |
3625 | freelist = READ_ONCE(c->freelist); | |
5076190d LT |
3626 | |
3627 | set_freepointer(s, tail_obj, freelist); | |
8a5ec0ba | 3628 | |
933393f5 | 3629 | if (unlikely(!this_cpu_cmpxchg_double( |
8a5ec0ba | 3630 | s->cpu_slab->freelist, s->cpu_slab->tid, |
5076190d | 3631 | freelist, tid, |
81084651 | 3632 | head, next_tid(tid)))) { |
8a5ec0ba CL |
3633 | |
3634 | note_cmpxchg_failure("slab_free", s, tid); | |
3635 | goto redo; | |
3636 | } | |
1f04b07d TG |
3637 | } else { |
3638 | /* Update the free list under the local lock */ | |
bd0e7491 VB |
3639 | local_lock(&s->cpu_slab->lock); |
3640 | c = this_cpu_ptr(s->cpu_slab); | |
bb192ed9 | 3641 | if (unlikely(slab != c->slab)) { |
bd0e7491 VB |
3642 | local_unlock(&s->cpu_slab->lock); |
3643 | goto redo; | |
3644 | } | |
3645 | tid = c->tid; | |
3646 | freelist = c->freelist; | |
3647 | ||
3648 | set_freepointer(s, tail_obj, freelist); | |
3649 | c->freelist = head; | |
3650 | c->tid = next_tid(tid); | |
3651 | ||
3652 | local_unlock(&s->cpu_slab->lock); | |
1f04b07d TG |
3653 | } |
3654 | stat(s, FREE_FASTPATH); | |
894b8788 CL |
3655 | } |
3656 | ||
bb192ed9 | 3657 | static __always_inline void slab_free(struct kmem_cache *s, struct slab *slab, |
b77d5b1b | 3658 | void *head, void *tail, void **p, int cnt, |
80a9201a AP |
3659 | unsigned long addr) |
3660 | { | |
b77d5b1b | 3661 | memcg_slab_free_hook(s, slab, p, cnt); |
80a9201a | 3662 | /* |
c3895391 AK |
3663 | * With KASAN enabled slab_free_freelist_hook modifies the freelist |
3664 | * to remove objects, whose reuse must be delayed. | |
80a9201a | 3665 | */ |
899447f6 | 3666 | if (slab_free_freelist_hook(s, &head, &tail, &cnt)) |
bb192ed9 | 3667 | do_slab_free(s, slab, head, tail, cnt, addr); |
80a9201a AP |
3668 | } |
3669 | ||
2bd926b4 | 3670 | #ifdef CONFIG_KASAN_GENERIC |
80a9201a AP |
3671 | void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) |
3672 | { | |
bb192ed9 | 3673 | do_slab_free(cache, virt_to_slab(x), x, NULL, 1, addr); |
80a9201a AP |
3674 | } |
3675 | #endif | |
3676 | ||
ed4cd17e HY |
3677 | void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller) |
3678 | { | |
3679 | slab_free(s, virt_to_slab(x), x, NULL, &x, 1, caller); | |
3680 | } | |
3681 | ||
81819f0f CL |
3682 | void kmem_cache_free(struct kmem_cache *s, void *x) |
3683 | { | |
b9ce5ef4 GC |
3684 | s = cache_from_obj(s, x); |
3685 | if (!s) | |
79576102 | 3686 | return; |
2c1d697f | 3687 | trace_kmem_cache_free(_RET_IP_, x, s); |
b77d5b1b | 3688 | slab_free(s, virt_to_slab(x), x, NULL, &x, 1, _RET_IP_); |
81819f0f CL |
3689 | } |
3690 | EXPORT_SYMBOL(kmem_cache_free); | |
3691 | ||
d0ecd894 | 3692 | struct detached_freelist { |
cc465c3b | 3693 | struct slab *slab; |
d0ecd894 JDB |
3694 | void *tail; |
3695 | void *freelist; | |
3696 | int cnt; | |
376bf125 | 3697 | struct kmem_cache *s; |
d0ecd894 | 3698 | }; |
fbd02630 | 3699 | |
d0ecd894 JDB |
3700 | /* |
3701 | * This function progressively scans the array with free objects (with | |
3702 | * a limited look ahead) and extract objects belonging to the same | |
cc465c3b MWO |
3703 | * slab. It builds a detached freelist directly within the given |
3704 | * slab/objects. This can happen without any need for | |
d0ecd894 JDB |
3705 | * synchronization, because the objects are owned by running process. |
3706 | * The freelist is build up as a single linked list in the objects. | |
3707 | * The idea is, that this detached freelist can then be bulk | |
3708 | * transferred to the real freelist(s), but only requiring a single | |
3709 | * synchronization primitive. Look ahead in the array is limited due | |
3710 | * to performance reasons. | |
3711 | */ | |
376bf125 JDB |
3712 | static inline |
3713 | int build_detached_freelist(struct kmem_cache *s, size_t size, | |
3714 | void **p, struct detached_freelist *df) | |
d0ecd894 | 3715 | { |
d0ecd894 JDB |
3716 | int lookahead = 3; |
3717 | void *object; | |
cc465c3b | 3718 | struct folio *folio; |
b77d5b1b | 3719 | size_t same; |
fbd02630 | 3720 | |
b77d5b1b | 3721 | object = p[--size]; |
cc465c3b | 3722 | folio = virt_to_folio(object); |
ca257195 JDB |
3723 | if (!s) { |
3724 | /* Handle kalloc'ed objects */ | |
cc465c3b | 3725 | if (unlikely(!folio_test_slab(folio))) { |
d835eef4 | 3726 | free_large_kmalloc(folio, object); |
b77d5b1b | 3727 | df->slab = NULL; |
ca257195 JDB |
3728 | return size; |
3729 | } | |
3730 | /* Derive kmem_cache from object */ | |
b77d5b1b MS |
3731 | df->slab = folio_slab(folio); |
3732 | df->s = df->slab->slab_cache; | |
ca257195 | 3733 | } else { |
b77d5b1b | 3734 | df->slab = folio_slab(folio); |
ca257195 JDB |
3735 | df->s = cache_from_obj(s, object); /* Support for memcg */ |
3736 | } | |
376bf125 | 3737 | |
d0ecd894 | 3738 | /* Start new detached freelist */ |
d0ecd894 JDB |
3739 | df->tail = object; |
3740 | df->freelist = object; | |
d0ecd894 JDB |
3741 | df->cnt = 1; |
3742 | ||
b77d5b1b MS |
3743 | if (is_kfence_address(object)) |
3744 | return size; | |
3745 | ||
3746 | set_freepointer(df->s, object, NULL); | |
3747 | ||
3748 | same = size; | |
d0ecd894 JDB |
3749 | while (size) { |
3750 | object = p[--size]; | |
cc465c3b MWO |
3751 | /* df->slab is always set at this point */ |
3752 | if (df->slab == virt_to_slab(object)) { | |
d0ecd894 | 3753 | /* Opportunity build freelist */ |
376bf125 | 3754 | set_freepointer(df->s, object, df->freelist); |
d0ecd894 JDB |
3755 | df->freelist = object; |
3756 | df->cnt++; | |
b77d5b1b MS |
3757 | same--; |
3758 | if (size != same) | |
3759 | swap(p[size], p[same]); | |
d0ecd894 | 3760 | continue; |
fbd02630 | 3761 | } |
d0ecd894 JDB |
3762 | |
3763 | /* Limit look ahead search */ | |
3764 | if (!--lookahead) | |
3765 | break; | |
fbd02630 | 3766 | } |
d0ecd894 | 3767 | |
b77d5b1b | 3768 | return same; |
d0ecd894 JDB |
3769 | } |
3770 | ||
d0ecd894 | 3771 | /* Note that interrupts must be enabled when calling this function. */ |
376bf125 | 3772 | void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p) |
d0ecd894 | 3773 | { |
2055e67b | 3774 | if (!size) |
d0ecd894 JDB |
3775 | return; |
3776 | ||
3777 | do { | |
3778 | struct detached_freelist df; | |
3779 | ||
3780 | size = build_detached_freelist(s, size, p, &df); | |
cc465c3b | 3781 | if (!df.slab) |
d0ecd894 JDB |
3782 | continue; |
3783 | ||
b77d5b1b MS |
3784 | slab_free(df.s, df.slab, df.freelist, df.tail, &p[size], df.cnt, |
3785 | _RET_IP_); | |
d0ecd894 | 3786 | } while (likely(size)); |
484748f0 CL |
3787 | } |
3788 | EXPORT_SYMBOL(kmem_cache_free_bulk); | |
3789 | ||
994eb764 | 3790 | /* Note that interrupts must be enabled when calling this function. */ |
865762a8 JDB |
3791 | int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, |
3792 | void **p) | |
484748f0 | 3793 | { |
994eb764 JDB |
3794 | struct kmem_cache_cpu *c; |
3795 | int i; | |
964d4bd3 | 3796 | struct obj_cgroup *objcg = NULL; |
994eb764 | 3797 | |
03ec0ed5 | 3798 | /* memcg and kmem_cache debug support */ |
88f2ef73 | 3799 | s = slab_pre_alloc_hook(s, NULL, &objcg, size, flags); |
03ec0ed5 JDB |
3800 | if (unlikely(!s)) |
3801 | return false; | |
994eb764 JDB |
3802 | /* |
3803 | * Drain objects in the per cpu slab, while disabling local | |
3804 | * IRQs, which protects against PREEMPT and interrupts | |
3805 | * handlers invoking normal fastpath. | |
3806 | */ | |
25c00c50 | 3807 | c = slub_get_cpu_ptr(s->cpu_slab); |
bd0e7491 | 3808 | local_lock_irq(&s->cpu_slab->lock); |
994eb764 JDB |
3809 | |
3810 | for (i = 0; i < size; i++) { | |
b89fb5ef | 3811 | void *object = kfence_alloc(s, s->object_size, flags); |
994eb764 | 3812 | |
b89fb5ef AP |
3813 | if (unlikely(object)) { |
3814 | p[i] = object; | |
3815 | continue; | |
3816 | } | |
3817 | ||
3818 | object = c->freelist; | |
ebe909e0 | 3819 | if (unlikely(!object)) { |
fd4d9c7d JH |
3820 | /* |
3821 | * We may have removed an object from c->freelist using | |
3822 | * the fastpath in the previous iteration; in that case, | |
3823 | * c->tid has not been bumped yet. | |
3824 | * Since ___slab_alloc() may reenable interrupts while | |
3825 | * allocating memory, we should bump c->tid now. | |
3826 | */ | |
3827 | c->tid = next_tid(c->tid); | |
3828 | ||
bd0e7491 | 3829 | local_unlock_irq(&s->cpu_slab->lock); |
e500059b | 3830 | |
ebe909e0 JDB |
3831 | /* |
3832 | * Invoking slow path likely have side-effect | |
3833 | * of re-populating per CPU c->freelist | |
3834 | */ | |
87098373 | 3835 | p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE, |
6edf2576 | 3836 | _RET_IP_, c, s->object_size); |
87098373 CL |
3837 | if (unlikely(!p[i])) |
3838 | goto error; | |
3839 | ||
ebe909e0 | 3840 | c = this_cpu_ptr(s->cpu_slab); |
0f181f9f AP |
3841 | maybe_wipe_obj_freeptr(s, p[i]); |
3842 | ||
bd0e7491 | 3843 | local_lock_irq(&s->cpu_slab->lock); |
e500059b | 3844 | |
ebe909e0 JDB |
3845 | continue; /* goto for-loop */ |
3846 | } | |
994eb764 JDB |
3847 | c->freelist = get_freepointer(s, object); |
3848 | p[i] = object; | |
0f181f9f | 3849 | maybe_wipe_obj_freeptr(s, p[i]); |
994eb764 JDB |
3850 | } |
3851 | c->tid = next_tid(c->tid); | |
bd0e7491 | 3852 | local_unlock_irq(&s->cpu_slab->lock); |
25c00c50 | 3853 | slub_put_cpu_ptr(s->cpu_slab); |
994eb764 | 3854 | |
da844b78 AK |
3855 | /* |
3856 | * memcg and kmem_cache debug support and memory initialization. | |
3857 | * Done outside of the IRQ disabled fastpath loop. | |
3858 | */ | |
3859 | slab_post_alloc_hook(s, objcg, flags, size, p, | |
3860 | slab_want_init_on_alloc(flags, s)); | |
865762a8 | 3861 | return i; |
87098373 | 3862 | error: |
25c00c50 | 3863 | slub_put_cpu_ptr(s->cpu_slab); |
da844b78 | 3864 | slab_post_alloc_hook(s, objcg, flags, i, p, false); |
2055e67b | 3865 | kmem_cache_free_bulk(s, i, p); |
865762a8 | 3866 | return 0; |
484748f0 CL |
3867 | } |
3868 | EXPORT_SYMBOL(kmem_cache_alloc_bulk); | |
3869 | ||
3870 | ||
81819f0f | 3871 | /* |
672bba3a CL |
3872 | * Object placement in a slab is made very easy because we always start at |
3873 | * offset 0. If we tune the size of the object to the alignment then we can | |
3874 | * get the required alignment by putting one properly sized object after | |
3875 | * another. | |
81819f0f CL |
3876 | * |
3877 | * Notice that the allocation order determines the sizes of the per cpu | |
3878 | * caches. Each processor has always one slab available for allocations. | |
3879 | * Increasing the allocation order reduces the number of times that slabs | |
672bba3a | 3880 | * must be moved on and off the partial lists and is therefore a factor in |
81819f0f | 3881 | * locking overhead. |
81819f0f CL |
3882 | */ |
3883 | ||
3884 | /* | |
f0953a1b | 3885 | * Minimum / Maximum order of slab pages. This influences locking overhead |
81819f0f CL |
3886 | * and slab fragmentation. A higher order reduces the number of partial slabs |
3887 | * and increases the number of allocations possible without having to | |
3888 | * take the list_lock. | |
3889 | */ | |
19af27af | 3890 | static unsigned int slub_min_order; |
90ce872c VB |
3891 | static unsigned int slub_max_order = |
3892 | IS_ENABLED(CONFIG_SLUB_TINY) ? 1 : PAGE_ALLOC_COSTLY_ORDER; | |
19af27af | 3893 | static unsigned int slub_min_objects; |
81819f0f | 3894 | |
81819f0f CL |
3895 | /* |
3896 | * Calculate the order of allocation given an slab object size. | |
3897 | * | |
672bba3a CL |
3898 | * The order of allocation has significant impact on performance and other |
3899 | * system components. Generally order 0 allocations should be preferred since | |
3900 | * order 0 does not cause fragmentation in the page allocator. Larger objects | |
3901 | * be problematic to put into order 0 slabs because there may be too much | |
c124f5b5 | 3902 | * unused space left. We go to a higher order if more than 1/16th of the slab |
672bba3a CL |
3903 | * would be wasted. |
3904 | * | |
3905 | * In order to reach satisfactory performance we must ensure that a minimum | |
3906 | * number of objects is in one slab. Otherwise we may generate too much | |
3907 | * activity on the partial lists which requires taking the list_lock. This is | |
3908 | * less a concern for large slabs though which are rarely used. | |
81819f0f | 3909 | * |
672bba3a CL |
3910 | * slub_max_order specifies the order where we begin to stop considering the |
3911 | * number of objects in a slab as critical. If we reach slub_max_order then | |
3912 | * we try to keep the page order as low as possible. So we accept more waste | |
3913 | * of space in favor of a small page order. | |
81819f0f | 3914 | * |
672bba3a CL |
3915 | * Higher order allocations also allow the placement of more objects in a |
3916 | * slab and thereby reduce object handling overhead. If the user has | |
dc84207d | 3917 | * requested a higher minimum order then we start with that one instead of |
672bba3a | 3918 | * the smallest order which will fit the object. |
81819f0f | 3919 | */ |
d122019b | 3920 | static inline unsigned int calc_slab_order(unsigned int size, |
19af27af | 3921 | unsigned int min_objects, unsigned int max_order, |
9736d2a9 | 3922 | unsigned int fract_leftover) |
81819f0f | 3923 | { |
19af27af AD |
3924 | unsigned int min_order = slub_min_order; |
3925 | unsigned int order; | |
81819f0f | 3926 | |
9736d2a9 | 3927 | if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE) |
210b5c06 | 3928 | return get_order(size * MAX_OBJS_PER_PAGE) - 1; |
39b26464 | 3929 | |
9736d2a9 | 3930 | for (order = max(min_order, (unsigned int)get_order(min_objects * size)); |
5e6d444e | 3931 | order <= max_order; order++) { |
81819f0f | 3932 | |
19af27af AD |
3933 | unsigned int slab_size = (unsigned int)PAGE_SIZE << order; |
3934 | unsigned int rem; | |
81819f0f | 3935 | |
9736d2a9 | 3936 | rem = slab_size % size; |
81819f0f | 3937 | |
5e6d444e | 3938 | if (rem <= slab_size / fract_leftover) |
81819f0f | 3939 | break; |
81819f0f | 3940 | } |
672bba3a | 3941 | |
81819f0f CL |
3942 | return order; |
3943 | } | |
3944 | ||
9736d2a9 | 3945 | static inline int calculate_order(unsigned int size) |
5e6d444e | 3946 | { |
19af27af AD |
3947 | unsigned int order; |
3948 | unsigned int min_objects; | |
3949 | unsigned int max_objects; | |
3286222f | 3950 | unsigned int nr_cpus; |
5e6d444e CL |
3951 | |
3952 | /* | |
3953 | * Attempt to find best configuration for a slab. This | |
3954 | * works by first attempting to generate a layout with | |
3955 | * the best configuration and backing off gradually. | |
3956 | * | |
422ff4d7 | 3957 | * First we increase the acceptable waste in a slab. Then |
5e6d444e CL |
3958 | * we reduce the minimum objects required in a slab. |
3959 | */ | |
3960 | min_objects = slub_min_objects; | |
3286222f VB |
3961 | if (!min_objects) { |
3962 | /* | |
3963 | * Some architectures will only update present cpus when | |
3964 | * onlining them, so don't trust the number if it's just 1. But | |
3965 | * we also don't want to use nr_cpu_ids always, as on some other | |
3966 | * architectures, there can be many possible cpus, but never | |
3967 | * onlined. Here we compromise between trying to avoid too high | |
3968 | * order on systems that appear larger than they are, and too | |
3969 | * low order on systems that appear smaller than they are. | |
3970 | */ | |
3971 | nr_cpus = num_present_cpus(); | |
3972 | if (nr_cpus <= 1) | |
3973 | nr_cpus = nr_cpu_ids; | |
3974 | min_objects = 4 * (fls(nr_cpus) + 1); | |
3975 | } | |
9736d2a9 | 3976 | max_objects = order_objects(slub_max_order, size); |
e8120ff1 ZY |
3977 | min_objects = min(min_objects, max_objects); |
3978 | ||
5e6d444e | 3979 | while (min_objects > 1) { |
19af27af AD |
3980 | unsigned int fraction; |
3981 | ||
c124f5b5 | 3982 | fraction = 16; |
5e6d444e | 3983 | while (fraction >= 4) { |
d122019b | 3984 | order = calc_slab_order(size, min_objects, |
9736d2a9 | 3985 | slub_max_order, fraction); |
5e6d444e CL |
3986 | if (order <= slub_max_order) |
3987 | return order; | |
3988 | fraction /= 2; | |
3989 | } | |
5086c389 | 3990 | min_objects--; |
5e6d444e CL |
3991 | } |
3992 | ||
3993 | /* | |
3994 | * We were unable to place multiple objects in a slab. Now | |
3995 | * lets see if we can place a single object there. | |
3996 | */ | |
d122019b | 3997 | order = calc_slab_order(size, 1, slub_max_order, 1); |
5e6d444e CL |
3998 | if (order <= slub_max_order) |
3999 | return order; | |
4000 | ||
4001 | /* | |
4002 | * Doh this slab cannot be placed using slub_max_order. | |
4003 | */ | |
d122019b | 4004 | order = calc_slab_order(size, 1, MAX_ORDER, 1); |
818cf590 | 4005 | if (order < MAX_ORDER) |
5e6d444e CL |
4006 | return order; |
4007 | return -ENOSYS; | |
4008 | } | |
4009 | ||
5595cffc | 4010 | static void |
4053497d | 4011 | init_kmem_cache_node(struct kmem_cache_node *n) |
81819f0f CL |
4012 | { |
4013 | n->nr_partial = 0; | |
81819f0f CL |
4014 | spin_lock_init(&n->list_lock); |
4015 | INIT_LIST_HEAD(&n->partial); | |
8ab1372f | 4016 | #ifdef CONFIG_SLUB_DEBUG |
0f389ec6 | 4017 | atomic_long_set(&n->nr_slabs, 0); |
02b71b70 | 4018 | atomic_long_set(&n->total_objects, 0); |
643b1138 | 4019 | INIT_LIST_HEAD(&n->full); |
8ab1372f | 4020 | #endif |
81819f0f CL |
4021 | } |
4022 | ||
55136592 | 4023 | static inline int alloc_kmem_cache_cpus(struct kmem_cache *s) |
4c93c355 | 4024 | { |
6c182dc0 | 4025 | BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE < |
95a05b42 | 4026 | KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu)); |
4c93c355 | 4027 | |
8a5ec0ba | 4028 | /* |
d4d84fef CM |
4029 | * Must align to double word boundary for the double cmpxchg |
4030 | * instructions to work; see __pcpu_double_call_return_bool(). | |
8a5ec0ba | 4031 | */ |
d4d84fef CM |
4032 | s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu), |
4033 | 2 * sizeof(void *)); | |
8a5ec0ba CL |
4034 | |
4035 | if (!s->cpu_slab) | |
4036 | return 0; | |
4037 | ||
4038 | init_kmem_cache_cpus(s); | |
4c93c355 | 4039 | |
8a5ec0ba | 4040 | return 1; |
4c93c355 | 4041 | } |
4c93c355 | 4042 | |
51df1142 CL |
4043 | static struct kmem_cache *kmem_cache_node; |
4044 | ||
81819f0f CL |
4045 | /* |
4046 | * No kmalloc_node yet so do it by hand. We know that this is the first | |
4047 | * slab on the node for this slabcache. There are no concurrent accesses | |
4048 | * possible. | |
4049 | * | |
721ae22a ZYW |
4050 | * Note that this function only works on the kmem_cache_node |
4051 | * when allocating for the kmem_cache_node. This is used for bootstrapping | |
4c93c355 | 4052 | * memory on a fresh node that has no slab structures yet. |
81819f0f | 4053 | */ |
55136592 | 4054 | static void early_kmem_cache_node_alloc(int node) |
81819f0f | 4055 | { |
bb192ed9 | 4056 | struct slab *slab; |
81819f0f CL |
4057 | struct kmem_cache_node *n; |
4058 | ||
51df1142 | 4059 | BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node)); |
81819f0f | 4060 | |
bb192ed9 | 4061 | slab = new_slab(kmem_cache_node, GFP_NOWAIT, node); |
81819f0f | 4062 | |
bb192ed9 | 4063 | BUG_ON(!slab); |
c7323a5a | 4064 | inc_slabs_node(kmem_cache_node, slab_nid(slab), slab->objects); |
bb192ed9 | 4065 | if (slab_nid(slab) != node) { |
f9f58285 FF |
4066 | pr_err("SLUB: Unable to allocate memory from node %d\n", node); |
4067 | pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n"); | |
a2f92ee7 CL |
4068 | } |
4069 | ||
bb192ed9 | 4070 | n = slab->freelist; |
81819f0f | 4071 | BUG_ON(!n); |
8ab1372f | 4072 | #ifdef CONFIG_SLUB_DEBUG |
f7cb1933 | 4073 | init_object(kmem_cache_node, n, SLUB_RED_ACTIVE); |
51df1142 | 4074 | init_tracking(kmem_cache_node, n); |
8ab1372f | 4075 | #endif |
da844b78 | 4076 | n = kasan_slab_alloc(kmem_cache_node, n, GFP_KERNEL, false); |
bb192ed9 VB |
4077 | slab->freelist = get_freepointer(kmem_cache_node, n); |
4078 | slab->inuse = 1; | |
12b22386 | 4079 | kmem_cache_node->node[node] = n; |
4053497d | 4080 | init_kmem_cache_node(n); |
bb192ed9 | 4081 | inc_slabs_node(kmem_cache_node, node, slab->objects); |
6446faa2 | 4082 | |
67b6c900 | 4083 | /* |
1e4dd946 SR |
4084 | * No locks need to be taken here as it has just been |
4085 | * initialized and there is no concurrent access. | |
67b6c900 | 4086 | */ |
bb192ed9 | 4087 | __add_partial(n, slab, DEACTIVATE_TO_HEAD); |
81819f0f CL |
4088 | } |
4089 | ||
4090 | static void free_kmem_cache_nodes(struct kmem_cache *s) | |
4091 | { | |
4092 | int node; | |
fa45dc25 | 4093 | struct kmem_cache_node *n; |
81819f0f | 4094 | |
fa45dc25 | 4095 | for_each_kmem_cache_node(s, node, n) { |
81819f0f | 4096 | s->node[node] = NULL; |
ea37df54 | 4097 | kmem_cache_free(kmem_cache_node, n); |
81819f0f CL |
4098 | } |
4099 | } | |
4100 | ||
52b4b950 DS |
4101 | void __kmem_cache_release(struct kmem_cache *s) |
4102 | { | |
210e7a43 | 4103 | cache_random_seq_destroy(s); |
52b4b950 DS |
4104 | free_percpu(s->cpu_slab); |
4105 | free_kmem_cache_nodes(s); | |
4106 | } | |
4107 | ||
55136592 | 4108 | static int init_kmem_cache_nodes(struct kmem_cache *s) |
81819f0f CL |
4109 | { |
4110 | int node; | |
81819f0f | 4111 | |
7e1fa93d | 4112 | for_each_node_mask(node, slab_nodes) { |
81819f0f CL |
4113 | struct kmem_cache_node *n; |
4114 | ||
73367bd8 | 4115 | if (slab_state == DOWN) { |
55136592 | 4116 | early_kmem_cache_node_alloc(node); |
73367bd8 AD |
4117 | continue; |
4118 | } | |
51df1142 | 4119 | n = kmem_cache_alloc_node(kmem_cache_node, |
55136592 | 4120 | GFP_KERNEL, node); |
81819f0f | 4121 | |
73367bd8 AD |
4122 | if (!n) { |
4123 | free_kmem_cache_nodes(s); | |
4124 | return 0; | |
81819f0f | 4125 | } |
73367bd8 | 4126 | |
4053497d | 4127 | init_kmem_cache_node(n); |
ea37df54 | 4128 | s->node[node] = n; |
81819f0f CL |
4129 | } |
4130 | return 1; | |
4131 | } | |
81819f0f | 4132 | |
e6d0e1dc WY |
4133 | static void set_cpu_partial(struct kmem_cache *s) |
4134 | { | |
4135 | #ifdef CONFIG_SLUB_CPU_PARTIAL | |
b47291ef VB |
4136 | unsigned int nr_objects; |
4137 | ||
e6d0e1dc WY |
4138 | /* |
4139 | * cpu_partial determined the maximum number of objects kept in the | |
4140 | * per cpu partial lists of a processor. | |
4141 | * | |
4142 | * Per cpu partial lists mainly contain slabs that just have one | |
4143 | * object freed. If they are used for allocation then they can be | |
4144 | * filled up again with minimal effort. The slab will never hit the | |
4145 | * per node partial lists and therefore no locking will be required. | |
4146 | * | |
b47291ef VB |
4147 | * For backwards compatibility reasons, this is determined as number |
4148 | * of objects, even though we now limit maximum number of pages, see | |
4149 | * slub_set_cpu_partial() | |
e6d0e1dc WY |
4150 | */ |
4151 | if (!kmem_cache_has_cpu_partial(s)) | |
b47291ef | 4152 | nr_objects = 0; |
e6d0e1dc | 4153 | else if (s->size >= PAGE_SIZE) |
b47291ef | 4154 | nr_objects = 6; |
e6d0e1dc | 4155 | else if (s->size >= 1024) |
23e98ad1 | 4156 | nr_objects = 24; |
e6d0e1dc | 4157 | else if (s->size >= 256) |
23e98ad1 | 4158 | nr_objects = 52; |
e6d0e1dc | 4159 | else |
23e98ad1 | 4160 | nr_objects = 120; |
b47291ef VB |
4161 | |
4162 | slub_set_cpu_partial(s, nr_objects); | |
e6d0e1dc WY |
4163 | #endif |
4164 | } | |
4165 | ||
81819f0f CL |
4166 | /* |
4167 | * calculate_sizes() determines the order and the distribution of data within | |
4168 | * a slab object. | |
4169 | */ | |
ae44d81d | 4170 | static int calculate_sizes(struct kmem_cache *s) |
81819f0f | 4171 | { |
d50112ed | 4172 | slab_flags_t flags = s->flags; |
be4a7988 | 4173 | unsigned int size = s->object_size; |
19af27af | 4174 | unsigned int order; |
81819f0f | 4175 | |
d8b42bf5 CL |
4176 | /* |
4177 | * Round up object size to the next word boundary. We can only | |
4178 | * place the free pointer at word boundaries and this determines | |
4179 | * the possible location of the free pointer. | |
4180 | */ | |
4181 | size = ALIGN(size, sizeof(void *)); | |
4182 | ||
4183 | #ifdef CONFIG_SLUB_DEBUG | |
81819f0f CL |
4184 | /* |
4185 | * Determine if we can poison the object itself. If the user of | |
4186 | * the slab may touch the object after free or before allocation | |
4187 | * then we should never poison the object itself. | |
4188 | */ | |
5f0d5a3a | 4189 | if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) && |
c59def9f | 4190 | !s->ctor) |
81819f0f CL |
4191 | s->flags |= __OBJECT_POISON; |
4192 | else | |
4193 | s->flags &= ~__OBJECT_POISON; | |
4194 | ||
81819f0f CL |
4195 | |
4196 | /* | |
672bba3a | 4197 | * If we are Redzoning then check if there is some space between the |
81819f0f | 4198 | * end of the object and the free pointer. If not then add an |
672bba3a | 4199 | * additional word to have some bytes to store Redzone information. |
81819f0f | 4200 | */ |
3b0efdfa | 4201 | if ((flags & SLAB_RED_ZONE) && size == s->object_size) |
81819f0f | 4202 | size += sizeof(void *); |
41ecc55b | 4203 | #endif |
81819f0f CL |
4204 | |
4205 | /* | |
672bba3a | 4206 | * With that we have determined the number of bytes in actual use |
e41a49fa | 4207 | * by the object and redzoning. |
81819f0f CL |
4208 | */ |
4209 | s->inuse = size; | |
4210 | ||
74c1d3e0 KC |
4211 | if ((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) || |
4212 | ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) || | |
4213 | s->ctor) { | |
81819f0f CL |
4214 | /* |
4215 | * Relocate free pointer after the object if it is not | |
4216 | * permitted to overwrite the first word of the object on | |
4217 | * kmem_cache_free. | |
4218 | * | |
4219 | * This is the case if we do RCU, have a constructor or | |
74c1d3e0 KC |
4220 | * destructor, are poisoning the objects, or are |
4221 | * redzoning an object smaller than sizeof(void *). | |
cbfc35a4 WL |
4222 | * |
4223 | * The assumption that s->offset >= s->inuse means free | |
4224 | * pointer is outside of the object is used in the | |
4225 | * freeptr_outside_object() function. If that is no | |
4226 | * longer true, the function needs to be modified. | |
81819f0f CL |
4227 | */ |
4228 | s->offset = size; | |
4229 | size += sizeof(void *); | |
e41a49fa | 4230 | } else { |
3202fa62 KC |
4231 | /* |
4232 | * Store freelist pointer near middle of object to keep | |
4233 | * it away from the edges of the object to avoid small | |
4234 | * sized over/underflows from neighboring allocations. | |
4235 | */ | |
e41a49fa | 4236 | s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *)); |
81819f0f CL |
4237 | } |
4238 | ||
c12b3c62 | 4239 | #ifdef CONFIG_SLUB_DEBUG |
6edf2576 | 4240 | if (flags & SLAB_STORE_USER) { |
81819f0f CL |
4241 | /* |
4242 | * Need to store information about allocs and frees after | |
4243 | * the object. | |
4244 | */ | |
4245 | size += 2 * sizeof(struct track); | |
6edf2576 FT |
4246 | |
4247 | /* Save the original kmalloc request size */ | |
4248 | if (flags & SLAB_KMALLOC) | |
4249 | size += sizeof(unsigned int); | |
4250 | } | |
80a9201a | 4251 | #endif |
81819f0f | 4252 | |
80a9201a AP |
4253 | kasan_cache_create(s, &size, &s->flags); |
4254 | #ifdef CONFIG_SLUB_DEBUG | |
d86bd1be | 4255 | if (flags & SLAB_RED_ZONE) { |
81819f0f CL |
4256 | /* |
4257 | * Add some empty padding so that we can catch | |
4258 | * overwrites from earlier objects rather than let | |
4259 | * tracking information or the free pointer be | |
0211a9c8 | 4260 | * corrupted if a user writes before the start |
81819f0f CL |
4261 | * of the object. |
4262 | */ | |
4263 | size += sizeof(void *); | |
d86bd1be JK |
4264 | |
4265 | s->red_left_pad = sizeof(void *); | |
4266 | s->red_left_pad = ALIGN(s->red_left_pad, s->align); | |
4267 | size += s->red_left_pad; | |
4268 | } | |
41ecc55b | 4269 | #endif |
672bba3a | 4270 | |
81819f0f CL |
4271 | /* |
4272 | * SLUB stores one object immediately after another beginning from | |
4273 | * offset 0. In order to align the objects we have to simply size | |
4274 | * each object to conform to the alignment. | |
4275 | */ | |
45906855 | 4276 | size = ALIGN(size, s->align); |
81819f0f | 4277 | s->size = size; |
4138fdfc | 4278 | s->reciprocal_size = reciprocal_value(size); |
ae44d81d | 4279 | order = calculate_order(size); |
81819f0f | 4280 | |
19af27af | 4281 | if ((int)order < 0) |
81819f0f CL |
4282 | return 0; |
4283 | ||
b7a49f0d | 4284 | s->allocflags = 0; |
834f3d11 | 4285 | if (order) |
b7a49f0d CL |
4286 | s->allocflags |= __GFP_COMP; |
4287 | ||
4288 | if (s->flags & SLAB_CACHE_DMA) | |
2c59dd65 | 4289 | s->allocflags |= GFP_DMA; |
b7a49f0d | 4290 | |
6d6ea1e9 NB |
4291 | if (s->flags & SLAB_CACHE_DMA32) |
4292 | s->allocflags |= GFP_DMA32; | |
4293 | ||
b7a49f0d CL |
4294 | if (s->flags & SLAB_RECLAIM_ACCOUNT) |
4295 | s->allocflags |= __GFP_RECLAIMABLE; | |
4296 | ||
81819f0f CL |
4297 | /* |
4298 | * Determine the number of objects per slab | |
4299 | */ | |
9736d2a9 MW |
4300 | s->oo = oo_make(order, size); |
4301 | s->min = oo_make(get_order(size), size); | |
81819f0f | 4302 | |
834f3d11 | 4303 | return !!oo_objects(s->oo); |
81819f0f CL |
4304 | } |
4305 | ||
d50112ed | 4306 | static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags) |
81819f0f | 4307 | { |
37540008 | 4308 | s->flags = kmem_cache_flags(s->size, flags, s->name); |
2482ddec KC |
4309 | #ifdef CONFIG_SLAB_FREELIST_HARDENED |
4310 | s->random = get_random_long(); | |
4311 | #endif | |
81819f0f | 4312 | |
ae44d81d | 4313 | if (!calculate_sizes(s)) |
81819f0f | 4314 | goto error; |
3de47213 DR |
4315 | if (disable_higher_order_debug) { |
4316 | /* | |
4317 | * Disable debugging flags that store metadata if the min slab | |
4318 | * order increased. | |
4319 | */ | |
3b0efdfa | 4320 | if (get_order(s->size) > get_order(s->object_size)) { |
3de47213 DR |
4321 | s->flags &= ~DEBUG_METADATA_FLAGS; |
4322 | s->offset = 0; | |
ae44d81d | 4323 | if (!calculate_sizes(s)) |
3de47213 DR |
4324 | goto error; |
4325 | } | |
4326 | } | |
81819f0f | 4327 | |
2565409f HC |
4328 | #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ |
4329 | defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) | |
149daaf3 | 4330 | if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0) |
b789ef51 CL |
4331 | /* Enable fast mode */ |
4332 | s->flags |= __CMPXCHG_DOUBLE; | |
4333 | #endif | |
4334 | ||
3b89d7d8 | 4335 | /* |
c2092c12 | 4336 | * The larger the object size is, the more slabs we want on the partial |
3b89d7d8 DR |
4337 | * list to avoid pounding the page allocator excessively. |
4338 | */ | |
5182f3c9 HY |
4339 | s->min_partial = min_t(unsigned long, MAX_PARTIAL, ilog2(s->size) / 2); |
4340 | s->min_partial = max_t(unsigned long, MIN_PARTIAL, s->min_partial); | |
49e22585 | 4341 | |
e6d0e1dc | 4342 | set_cpu_partial(s); |
49e22585 | 4343 | |
81819f0f | 4344 | #ifdef CONFIG_NUMA |
e2cb96b7 | 4345 | s->remote_node_defrag_ratio = 1000; |
81819f0f | 4346 | #endif |
210e7a43 TG |
4347 | |
4348 | /* Initialize the pre-computed randomized freelist if slab is up */ | |
4349 | if (slab_state >= UP) { | |
4350 | if (init_cache_random_seq(s)) | |
4351 | goto error; | |
4352 | } | |
4353 | ||
55136592 | 4354 | if (!init_kmem_cache_nodes(s)) |
dfb4f096 | 4355 | goto error; |
81819f0f | 4356 | |
55136592 | 4357 | if (alloc_kmem_cache_cpus(s)) |
278b1bb1 | 4358 | return 0; |
ff12059e | 4359 | |
81819f0f | 4360 | error: |
9037c576 | 4361 | __kmem_cache_release(s); |
278b1bb1 | 4362 | return -EINVAL; |
81819f0f | 4363 | } |
81819f0f | 4364 | |
bb192ed9 | 4365 | static void list_slab_objects(struct kmem_cache *s, struct slab *slab, |
55860d96 | 4366 | const char *text) |
33b12c38 CL |
4367 | { |
4368 | #ifdef CONFIG_SLUB_DEBUG | |
bb192ed9 | 4369 | void *addr = slab_address(slab); |
33b12c38 | 4370 | void *p; |
aa456c7a | 4371 | |
bb192ed9 | 4372 | slab_err(s, slab, text, s->name); |
33b12c38 | 4373 | |
4ef3f5a3 VB |
4374 | spin_lock(&object_map_lock); |
4375 | __fill_map(object_map, s, slab); | |
4376 | ||
bb192ed9 | 4377 | for_each_object(p, s, addr, slab->objects) { |
33b12c38 | 4378 | |
4ef3f5a3 | 4379 | if (!test_bit(__obj_to_index(s, addr, p), object_map)) { |
96b94abc | 4380 | pr_err("Object 0x%p @offset=%tu\n", p, p - addr); |
33b12c38 CL |
4381 | print_tracking(s, p); |
4382 | } | |
4383 | } | |
4ef3f5a3 | 4384 | spin_unlock(&object_map_lock); |
33b12c38 CL |
4385 | #endif |
4386 | } | |
4387 | ||
81819f0f | 4388 | /* |
599870b1 | 4389 | * Attempt to free all partial slabs on a node. |
52b4b950 DS |
4390 | * This is called from __kmem_cache_shutdown(). We must take list_lock |
4391 | * because sysfs file might still access partial list after the shutdowning. | |
81819f0f | 4392 | */ |
599870b1 | 4393 | static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) |
81819f0f | 4394 | { |
60398923 | 4395 | LIST_HEAD(discard); |
bb192ed9 | 4396 | struct slab *slab, *h; |
81819f0f | 4397 | |
52b4b950 DS |
4398 | BUG_ON(irqs_disabled()); |
4399 | spin_lock_irq(&n->list_lock); | |
bb192ed9 VB |
4400 | list_for_each_entry_safe(slab, h, &n->partial, slab_list) { |
4401 | if (!slab->inuse) { | |
4402 | remove_partial(n, slab); | |
4403 | list_add(&slab->slab_list, &discard); | |
33b12c38 | 4404 | } else { |
bb192ed9 | 4405 | list_slab_objects(s, slab, |
55860d96 | 4406 | "Objects remaining in %s on __kmem_cache_shutdown()"); |
599870b1 | 4407 | } |
33b12c38 | 4408 | } |
52b4b950 | 4409 | spin_unlock_irq(&n->list_lock); |
60398923 | 4410 | |
bb192ed9 VB |
4411 | list_for_each_entry_safe(slab, h, &discard, slab_list) |
4412 | discard_slab(s, slab); | |
81819f0f CL |
4413 | } |
4414 | ||
f9e13c0a SB |
4415 | bool __kmem_cache_empty(struct kmem_cache *s) |
4416 | { | |
4417 | int node; | |
4418 | struct kmem_cache_node *n; | |
4419 | ||
4420 | for_each_kmem_cache_node(s, node, n) | |
4421 | if (n->nr_partial || slabs_node(s, node)) | |
4422 | return false; | |
4423 | return true; | |
4424 | } | |
4425 | ||
81819f0f | 4426 | /* |
672bba3a | 4427 | * Release all resources used by a slab cache. |
81819f0f | 4428 | */ |
52b4b950 | 4429 | int __kmem_cache_shutdown(struct kmem_cache *s) |
81819f0f CL |
4430 | { |
4431 | int node; | |
fa45dc25 | 4432 | struct kmem_cache_node *n; |
81819f0f | 4433 | |
5a836bf6 | 4434 | flush_all_cpus_locked(s); |
81819f0f | 4435 | /* Attempt to free all objects */ |
fa45dc25 | 4436 | for_each_kmem_cache_node(s, node, n) { |
599870b1 CL |
4437 | free_partial(s, n); |
4438 | if (n->nr_partial || slabs_node(s, node)) | |
81819f0f CL |
4439 | return 1; |
4440 | } | |
81819f0f CL |
4441 | return 0; |
4442 | } | |
4443 | ||
5bb1bb35 | 4444 | #ifdef CONFIG_PRINTK |
2dfe63e6 | 4445 | void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) |
8e7f37f2 PM |
4446 | { |
4447 | void *base; | |
4448 | int __maybe_unused i; | |
4449 | unsigned int objnr; | |
4450 | void *objp; | |
4451 | void *objp0; | |
7213230a | 4452 | struct kmem_cache *s = slab->slab_cache; |
8e7f37f2 PM |
4453 | struct track __maybe_unused *trackp; |
4454 | ||
4455 | kpp->kp_ptr = object; | |
7213230a | 4456 | kpp->kp_slab = slab; |
8e7f37f2 | 4457 | kpp->kp_slab_cache = s; |
7213230a | 4458 | base = slab_address(slab); |
8e7f37f2 PM |
4459 | objp0 = kasan_reset_tag(object); |
4460 | #ifdef CONFIG_SLUB_DEBUG | |
4461 | objp = restore_red_left(s, objp0); | |
4462 | #else | |
4463 | objp = objp0; | |
4464 | #endif | |
40f3bf0c | 4465 | objnr = obj_to_index(s, slab, objp); |
8e7f37f2 PM |
4466 | kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp); |
4467 | objp = base + s->size * objnr; | |
4468 | kpp->kp_objp = objp; | |
7213230a MWO |
4469 | if (WARN_ON_ONCE(objp < base || objp >= base + slab->objects * s->size |
4470 | || (objp - base) % s->size) || | |
8e7f37f2 PM |
4471 | !(s->flags & SLAB_STORE_USER)) |
4472 | return; | |
4473 | #ifdef CONFIG_SLUB_DEBUG | |
0cbc124b | 4474 | objp = fixup_red_left(s, objp); |
8e7f37f2 PM |
4475 | trackp = get_track(s, objp, TRACK_ALLOC); |
4476 | kpp->kp_ret = (void *)trackp->addr; | |
5cf909c5 OG |
4477 | #ifdef CONFIG_STACKDEPOT |
4478 | { | |
4479 | depot_stack_handle_t handle; | |
4480 | unsigned long *entries; | |
4481 | unsigned int nr_entries; | |
78869146 | 4482 | |
5cf909c5 OG |
4483 | handle = READ_ONCE(trackp->handle); |
4484 | if (handle) { | |
4485 | nr_entries = stack_depot_fetch(handle, &entries); | |
4486 | for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++) | |
4487 | kpp->kp_stack[i] = (void *)entries[i]; | |
4488 | } | |
78869146 | 4489 | |
5cf909c5 OG |
4490 | trackp = get_track(s, objp, TRACK_FREE); |
4491 | handle = READ_ONCE(trackp->handle); | |
4492 | if (handle) { | |
4493 | nr_entries = stack_depot_fetch(handle, &entries); | |
4494 | for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++) | |
4495 | kpp->kp_free_stack[i] = (void *)entries[i]; | |
4496 | } | |
e548eaa1 | 4497 | } |
8e7f37f2 PM |
4498 | #endif |
4499 | #endif | |
4500 | } | |
5bb1bb35 | 4501 | #endif |
8e7f37f2 | 4502 | |
81819f0f CL |
4503 | /******************************************************************** |
4504 | * Kmalloc subsystem | |
4505 | *******************************************************************/ | |
4506 | ||
81819f0f CL |
4507 | static int __init setup_slub_min_order(char *str) |
4508 | { | |
19af27af | 4509 | get_option(&str, (int *)&slub_min_order); |
81819f0f CL |
4510 | |
4511 | return 1; | |
4512 | } | |
4513 | ||
4514 | __setup("slub_min_order=", setup_slub_min_order); | |
4515 | ||
4516 | static int __init setup_slub_max_order(char *str) | |
4517 | { | |
19af27af AD |
4518 | get_option(&str, (int *)&slub_max_order); |
4519 | slub_max_order = min(slub_max_order, (unsigned int)MAX_ORDER - 1); | |
81819f0f CL |
4520 | |
4521 | return 1; | |
4522 | } | |
4523 | ||
4524 | __setup("slub_max_order=", setup_slub_max_order); | |
4525 | ||
4526 | static int __init setup_slub_min_objects(char *str) | |
4527 | { | |
19af27af | 4528 | get_option(&str, (int *)&slub_min_objects); |
81819f0f CL |
4529 | |
4530 | return 1; | |
4531 | } | |
4532 | ||
4533 | __setup("slub_min_objects=", setup_slub_min_objects); | |
4534 | ||
ed18adc1 KC |
4535 | #ifdef CONFIG_HARDENED_USERCOPY |
4536 | /* | |
afcc90f8 KC |
4537 | * Rejects incorrectly sized objects and objects that are to be copied |
4538 | * to/from userspace but do not fall entirely within the containing slab | |
4539 | * cache's usercopy region. | |
ed18adc1 KC |
4540 | * |
4541 | * Returns NULL if check passes, otherwise const char * to name of cache | |
4542 | * to indicate an error. | |
4543 | */ | |
0b3eb091 MWO |
4544 | void __check_heap_object(const void *ptr, unsigned long n, |
4545 | const struct slab *slab, bool to_user) | |
ed18adc1 KC |
4546 | { |
4547 | struct kmem_cache *s; | |
44065b2e | 4548 | unsigned int offset; |
b89fb5ef | 4549 | bool is_kfence = is_kfence_address(ptr); |
ed18adc1 | 4550 | |
96fedce2 AK |
4551 | ptr = kasan_reset_tag(ptr); |
4552 | ||
ed18adc1 | 4553 | /* Find object and usable object size. */ |
0b3eb091 | 4554 | s = slab->slab_cache; |
ed18adc1 KC |
4555 | |
4556 | /* Reject impossible pointers. */ | |
0b3eb091 | 4557 | if (ptr < slab_address(slab)) |
f4e6e289 KC |
4558 | usercopy_abort("SLUB object not in SLUB page?!", NULL, |
4559 | to_user, 0, n); | |
ed18adc1 KC |
4560 | |
4561 | /* Find offset within object. */ | |
b89fb5ef AP |
4562 | if (is_kfence) |
4563 | offset = ptr - kfence_object_start(ptr); | |
4564 | else | |
0b3eb091 | 4565 | offset = (ptr - slab_address(slab)) % s->size; |
ed18adc1 KC |
4566 | |
4567 | /* Adjust for redzone and reject if within the redzone. */ | |
b89fb5ef | 4568 | if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) { |
ed18adc1 | 4569 | if (offset < s->red_left_pad) |
f4e6e289 KC |
4570 | usercopy_abort("SLUB object in left red zone", |
4571 | s->name, to_user, offset, n); | |
ed18adc1 KC |
4572 | offset -= s->red_left_pad; |
4573 | } | |
4574 | ||
afcc90f8 KC |
4575 | /* Allow address range falling entirely within usercopy region. */ |
4576 | if (offset >= s->useroffset && | |
4577 | offset - s->useroffset <= s->usersize && | |
4578 | n <= s->useroffset - offset + s->usersize) | |
f4e6e289 | 4579 | return; |
ed18adc1 | 4580 | |
f4e6e289 | 4581 | usercopy_abort("SLUB object", s->name, to_user, offset, n); |
ed18adc1 KC |
4582 | } |
4583 | #endif /* CONFIG_HARDENED_USERCOPY */ | |
4584 | ||
832f37f5 VD |
4585 | #define SHRINK_PROMOTE_MAX 32 |
4586 | ||
2086d26a | 4587 | /* |
832f37f5 VD |
4588 | * kmem_cache_shrink discards empty slabs and promotes the slabs filled |
4589 | * up most to the head of the partial lists. New allocations will then | |
4590 | * fill those up and thus they can be removed from the partial lists. | |
672bba3a CL |
4591 | * |
4592 | * The slabs with the least items are placed last. This results in them | |
4593 | * being allocated from last increasing the chance that the last objects | |
4594 | * are freed in them. | |
2086d26a | 4595 | */ |
5a836bf6 | 4596 | static int __kmem_cache_do_shrink(struct kmem_cache *s) |
2086d26a CL |
4597 | { |
4598 | int node; | |
4599 | int i; | |
4600 | struct kmem_cache_node *n; | |
bb192ed9 VB |
4601 | struct slab *slab; |
4602 | struct slab *t; | |
832f37f5 VD |
4603 | struct list_head discard; |
4604 | struct list_head promote[SHRINK_PROMOTE_MAX]; | |
2086d26a | 4605 | unsigned long flags; |
ce3712d7 | 4606 | int ret = 0; |
2086d26a | 4607 | |
fa45dc25 | 4608 | for_each_kmem_cache_node(s, node, n) { |
832f37f5 VD |
4609 | INIT_LIST_HEAD(&discard); |
4610 | for (i = 0; i < SHRINK_PROMOTE_MAX; i++) | |
4611 | INIT_LIST_HEAD(promote + i); | |
2086d26a CL |
4612 | |
4613 | spin_lock_irqsave(&n->list_lock, flags); | |
4614 | ||
4615 | /* | |
832f37f5 | 4616 | * Build lists of slabs to discard or promote. |
2086d26a | 4617 | * |
672bba3a | 4618 | * Note that concurrent frees may occur while we hold the |
c2092c12 | 4619 | * list_lock. slab->inuse here is the upper limit. |
2086d26a | 4620 | */ |
bb192ed9 VB |
4621 | list_for_each_entry_safe(slab, t, &n->partial, slab_list) { |
4622 | int free = slab->objects - slab->inuse; | |
832f37f5 | 4623 | |
c2092c12 | 4624 | /* Do not reread slab->inuse */ |
832f37f5 VD |
4625 | barrier(); |
4626 | ||
4627 | /* We do not keep full slabs on the list */ | |
4628 | BUG_ON(free <= 0); | |
4629 | ||
bb192ed9 VB |
4630 | if (free == slab->objects) { |
4631 | list_move(&slab->slab_list, &discard); | |
69cb8e6b | 4632 | n->nr_partial--; |
c7323a5a | 4633 | dec_slabs_node(s, node, slab->objects); |
832f37f5 | 4634 | } else if (free <= SHRINK_PROMOTE_MAX) |
bb192ed9 | 4635 | list_move(&slab->slab_list, promote + free - 1); |
2086d26a CL |
4636 | } |
4637 | ||
2086d26a | 4638 | /* |
832f37f5 VD |
4639 | * Promote the slabs filled up most to the head of the |
4640 | * partial list. | |
2086d26a | 4641 | */ |
832f37f5 VD |
4642 | for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--) |
4643 | list_splice(promote + i, &n->partial); | |
2086d26a | 4644 | |
2086d26a | 4645 | spin_unlock_irqrestore(&n->list_lock, flags); |
69cb8e6b CL |
4646 | |
4647 | /* Release empty slabs */ | |
bb192ed9 | 4648 | list_for_each_entry_safe(slab, t, &discard, slab_list) |
c7323a5a | 4649 | free_slab(s, slab); |
ce3712d7 VD |
4650 | |
4651 | if (slabs_node(s, node)) | |
4652 | ret = 1; | |
2086d26a CL |
4653 | } |
4654 | ||
ce3712d7 | 4655 | return ret; |
2086d26a | 4656 | } |
2086d26a | 4657 | |
5a836bf6 SAS |
4658 | int __kmem_cache_shrink(struct kmem_cache *s) |
4659 | { | |
4660 | flush_all(s); | |
4661 | return __kmem_cache_do_shrink(s); | |
4662 | } | |
4663 | ||
b9049e23 YG |
4664 | static int slab_mem_going_offline_callback(void *arg) |
4665 | { | |
4666 | struct kmem_cache *s; | |
4667 | ||
18004c5d | 4668 | mutex_lock(&slab_mutex); |
5a836bf6 SAS |
4669 | list_for_each_entry(s, &slab_caches, list) { |
4670 | flush_all_cpus_locked(s); | |
4671 | __kmem_cache_do_shrink(s); | |
4672 | } | |
18004c5d | 4673 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4674 | |
4675 | return 0; | |
4676 | } | |
4677 | ||
4678 | static void slab_mem_offline_callback(void *arg) | |
4679 | { | |
b9049e23 YG |
4680 | struct memory_notify *marg = arg; |
4681 | int offline_node; | |
4682 | ||
b9d5ab25 | 4683 | offline_node = marg->status_change_nid_normal; |
b9049e23 YG |
4684 | |
4685 | /* | |
4686 | * If the node still has available memory. we need kmem_cache_node | |
4687 | * for it yet. | |
4688 | */ | |
4689 | if (offline_node < 0) | |
4690 | return; | |
4691 | ||
18004c5d | 4692 | mutex_lock(&slab_mutex); |
7e1fa93d | 4693 | node_clear(offline_node, slab_nodes); |
666716fd VB |
4694 | /* |
4695 | * We no longer free kmem_cache_node structures here, as it would be | |
4696 | * racy with all get_node() users, and infeasible to protect them with | |
4697 | * slab_mutex. | |
4698 | */ | |
18004c5d | 4699 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4700 | } |
4701 | ||
4702 | static int slab_mem_going_online_callback(void *arg) | |
4703 | { | |
4704 | struct kmem_cache_node *n; | |
4705 | struct kmem_cache *s; | |
4706 | struct memory_notify *marg = arg; | |
b9d5ab25 | 4707 | int nid = marg->status_change_nid_normal; |
b9049e23 YG |
4708 | int ret = 0; |
4709 | ||
4710 | /* | |
4711 | * If the node's memory is already available, then kmem_cache_node is | |
4712 | * already created. Nothing to do. | |
4713 | */ | |
4714 | if (nid < 0) | |
4715 | return 0; | |
4716 | ||
4717 | /* | |
0121c619 | 4718 | * We are bringing a node online. No memory is available yet. We must |
b9049e23 YG |
4719 | * allocate a kmem_cache_node structure in order to bring the node |
4720 | * online. | |
4721 | */ | |
18004c5d | 4722 | mutex_lock(&slab_mutex); |
b9049e23 | 4723 | list_for_each_entry(s, &slab_caches, list) { |
666716fd VB |
4724 | /* |
4725 | * The structure may already exist if the node was previously | |
4726 | * onlined and offlined. | |
4727 | */ | |
4728 | if (get_node(s, nid)) | |
4729 | continue; | |
b9049e23 YG |
4730 | /* |
4731 | * XXX: kmem_cache_alloc_node will fallback to other nodes | |
4732 | * since memory is not yet available from the node that | |
4733 | * is brought up. | |
4734 | */ | |
8de66a0c | 4735 | n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL); |
b9049e23 YG |
4736 | if (!n) { |
4737 | ret = -ENOMEM; | |
4738 | goto out; | |
4739 | } | |
4053497d | 4740 | init_kmem_cache_node(n); |
b9049e23 YG |
4741 | s->node[nid] = n; |
4742 | } | |
7e1fa93d VB |
4743 | /* |
4744 | * Any cache created after this point will also have kmem_cache_node | |
4745 | * initialized for the new node. | |
4746 | */ | |
4747 | node_set(nid, slab_nodes); | |
b9049e23 | 4748 | out: |
18004c5d | 4749 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4750 | return ret; |
4751 | } | |
4752 | ||
4753 | static int slab_memory_callback(struct notifier_block *self, | |
4754 | unsigned long action, void *arg) | |
4755 | { | |
4756 | int ret = 0; | |
4757 | ||
4758 | switch (action) { | |
4759 | case MEM_GOING_ONLINE: | |
4760 | ret = slab_mem_going_online_callback(arg); | |
4761 | break; | |
4762 | case MEM_GOING_OFFLINE: | |
4763 | ret = slab_mem_going_offline_callback(arg); | |
4764 | break; | |
4765 | case MEM_OFFLINE: | |
4766 | case MEM_CANCEL_ONLINE: | |
4767 | slab_mem_offline_callback(arg); | |
4768 | break; | |
4769 | case MEM_ONLINE: | |
4770 | case MEM_CANCEL_OFFLINE: | |
4771 | break; | |
4772 | } | |
dc19f9db KH |
4773 | if (ret) |
4774 | ret = notifier_from_errno(ret); | |
4775 | else | |
4776 | ret = NOTIFY_OK; | |
b9049e23 YG |
4777 | return ret; |
4778 | } | |
4779 | ||
3ac38faa AM |
4780 | static struct notifier_block slab_memory_callback_nb = { |
4781 | .notifier_call = slab_memory_callback, | |
4782 | .priority = SLAB_CALLBACK_PRI, | |
4783 | }; | |
b9049e23 | 4784 | |
81819f0f CL |
4785 | /******************************************************************** |
4786 | * Basic setup of slabs | |
4787 | *******************************************************************/ | |
4788 | ||
51df1142 CL |
4789 | /* |
4790 | * Used for early kmem_cache structures that were allocated using | |
dffb4d60 CL |
4791 | * the page allocator. Allocate them properly then fix up the pointers |
4792 | * that may be pointing to the wrong kmem_cache structure. | |
51df1142 CL |
4793 | */ |
4794 | ||
dffb4d60 | 4795 | static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache) |
51df1142 CL |
4796 | { |
4797 | int node; | |
dffb4d60 | 4798 | struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); |
fa45dc25 | 4799 | struct kmem_cache_node *n; |
51df1142 | 4800 | |
dffb4d60 | 4801 | memcpy(s, static_cache, kmem_cache->object_size); |
51df1142 | 4802 | |
7d557b3c GC |
4803 | /* |
4804 | * This runs very early, and only the boot processor is supposed to be | |
4805 | * up. Even if it weren't true, IRQs are not up so we couldn't fire | |
4806 | * IPIs around. | |
4807 | */ | |
4808 | __flush_cpu_slab(s, smp_processor_id()); | |
fa45dc25 | 4809 | for_each_kmem_cache_node(s, node, n) { |
bb192ed9 | 4810 | struct slab *p; |
51df1142 | 4811 | |
916ac052 | 4812 | list_for_each_entry(p, &n->partial, slab_list) |
fa45dc25 | 4813 | p->slab_cache = s; |
51df1142 | 4814 | |
607bf324 | 4815 | #ifdef CONFIG_SLUB_DEBUG |
916ac052 | 4816 | list_for_each_entry(p, &n->full, slab_list) |
fa45dc25 | 4817 | p->slab_cache = s; |
51df1142 | 4818 | #endif |
51df1142 | 4819 | } |
dffb4d60 CL |
4820 | list_add(&s->list, &slab_caches); |
4821 | return s; | |
51df1142 CL |
4822 | } |
4823 | ||
81819f0f CL |
4824 | void __init kmem_cache_init(void) |
4825 | { | |
dffb4d60 CL |
4826 | static __initdata struct kmem_cache boot_kmem_cache, |
4827 | boot_kmem_cache_node; | |
7e1fa93d | 4828 | int node; |
51df1142 | 4829 | |
fc8d8620 SG |
4830 | if (debug_guardpage_minorder()) |
4831 | slub_max_order = 0; | |
4832 | ||
79270291 SB |
4833 | /* Print slub debugging pointers without hashing */ |
4834 | if (__slub_debug_enabled()) | |
4835 | no_hash_pointers_enable(NULL); | |
4836 | ||
dffb4d60 CL |
4837 | kmem_cache_node = &boot_kmem_cache_node; |
4838 | kmem_cache = &boot_kmem_cache; | |
51df1142 | 4839 | |
7e1fa93d VB |
4840 | /* |
4841 | * Initialize the nodemask for which we will allocate per node | |
4842 | * structures. Here we don't need taking slab_mutex yet. | |
4843 | */ | |
4844 | for_each_node_state(node, N_NORMAL_MEMORY) | |
4845 | node_set(node, slab_nodes); | |
4846 | ||
dffb4d60 | 4847 | create_boot_cache(kmem_cache_node, "kmem_cache_node", |
8eb8284b | 4848 | sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0); |
b9049e23 | 4849 | |
3ac38faa | 4850 | register_hotmemory_notifier(&slab_memory_callback_nb); |
81819f0f CL |
4851 | |
4852 | /* Able to allocate the per node structures */ | |
4853 | slab_state = PARTIAL; | |
4854 | ||
dffb4d60 CL |
4855 | create_boot_cache(kmem_cache, "kmem_cache", |
4856 | offsetof(struct kmem_cache, node) + | |
4857 | nr_node_ids * sizeof(struct kmem_cache_node *), | |
8eb8284b | 4858 | SLAB_HWCACHE_ALIGN, 0, 0); |
8a13a4cc | 4859 | |
dffb4d60 | 4860 | kmem_cache = bootstrap(&boot_kmem_cache); |
dffb4d60 | 4861 | kmem_cache_node = bootstrap(&boot_kmem_cache_node); |
51df1142 CL |
4862 | |
4863 | /* Now we can use the kmem_cache to allocate kmalloc slabs */ | |
34cc6990 | 4864 | setup_kmalloc_cache_index_table(); |
f97d5f63 | 4865 | create_kmalloc_caches(0); |
81819f0f | 4866 | |
210e7a43 TG |
4867 | /* Setup random freelists for each cache */ |
4868 | init_freelist_randomization(); | |
4869 | ||
a96a87bf SAS |
4870 | cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", NULL, |
4871 | slub_cpu_dead); | |
81819f0f | 4872 | |
b9726c26 | 4873 | pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%u\n", |
f97d5f63 | 4874 | cache_line_size(), |
81819f0f CL |
4875 | slub_min_order, slub_max_order, slub_min_objects, |
4876 | nr_cpu_ids, nr_node_ids); | |
4877 | } | |
4878 | ||
7e85ee0c PE |
4879 | void __init kmem_cache_init_late(void) |
4880 | { | |
e45cc288 ML |
4881 | flushwq = alloc_workqueue("slub_flushwq", WQ_MEM_RECLAIM, 0); |
4882 | WARN_ON(!flushwq); | |
7e85ee0c PE |
4883 | } |
4884 | ||
2633d7a0 | 4885 | struct kmem_cache * |
f4957d5b | 4886 | __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, |
d50112ed | 4887 | slab_flags_t flags, void (*ctor)(void *)) |
81819f0f | 4888 | { |
10befea9 | 4889 | struct kmem_cache *s; |
81819f0f | 4890 | |
a44cb944 | 4891 | s = find_mergeable(size, align, flags, name, ctor); |
81819f0f | 4892 | if (s) { |
efb93527 XS |
4893 | if (sysfs_slab_alias(s, name)) |
4894 | return NULL; | |
4895 | ||
81819f0f | 4896 | s->refcount++; |
84d0ddd6 | 4897 | |
81819f0f CL |
4898 | /* |
4899 | * Adjust the object sizes so that we clear | |
4900 | * the complete object on kzalloc. | |
4901 | */ | |
1b473f29 | 4902 | s->object_size = max(s->object_size, size); |
52ee6d74 | 4903 | s->inuse = max(s->inuse, ALIGN(size, sizeof(void *))); |
a0e1d1be | 4904 | } |
6446faa2 | 4905 | |
cbb79694 CL |
4906 | return s; |
4907 | } | |
84c1cf62 | 4908 | |
d50112ed | 4909 | int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags) |
cbb79694 | 4910 | { |
aac3a166 PE |
4911 | int err; |
4912 | ||
4913 | err = kmem_cache_open(s, flags); | |
4914 | if (err) | |
4915 | return err; | |
20cea968 | 4916 | |
45530c44 CL |
4917 | /* Mutex is not taken during early boot */ |
4918 | if (slab_state <= UP) | |
4919 | return 0; | |
4920 | ||
aac3a166 | 4921 | err = sysfs_slab_add(s); |
67823a54 | 4922 | if (err) { |
52b4b950 | 4923 | __kmem_cache_release(s); |
67823a54 ML |
4924 | return err; |
4925 | } | |
20cea968 | 4926 | |
64dd6849 FM |
4927 | if (s->flags & SLAB_STORE_USER) |
4928 | debugfs_slab_add(s); | |
4929 | ||
67823a54 | 4930 | return 0; |
81819f0f | 4931 | } |
81819f0f | 4932 | |
b1a413a3 | 4933 | #ifdef SLAB_SUPPORTS_SYSFS |
bb192ed9 | 4934 | static int count_inuse(struct slab *slab) |
205ab99d | 4935 | { |
bb192ed9 | 4936 | return slab->inuse; |
205ab99d CL |
4937 | } |
4938 | ||
bb192ed9 | 4939 | static int count_total(struct slab *slab) |
205ab99d | 4940 | { |
bb192ed9 | 4941 | return slab->objects; |
205ab99d | 4942 | } |
ab4d5ed5 | 4943 | #endif |
205ab99d | 4944 | |
ab4d5ed5 | 4945 | #ifdef CONFIG_SLUB_DEBUG |
bb192ed9 | 4946 | static void validate_slab(struct kmem_cache *s, struct slab *slab, |
0a19e7dd | 4947 | unsigned long *obj_map) |
53e15af0 CL |
4948 | { |
4949 | void *p; | |
bb192ed9 | 4950 | void *addr = slab_address(slab); |
53e15af0 | 4951 | |
bb192ed9 | 4952 | if (!check_slab(s, slab) || !on_freelist(s, slab, NULL)) |
41bec7c3 | 4953 | return; |
53e15af0 CL |
4954 | |
4955 | /* Now we know that a valid freelist exists */ | |
bb192ed9 VB |
4956 | __fill_map(obj_map, s, slab); |
4957 | for_each_object(p, s, addr, slab->objects) { | |
0a19e7dd | 4958 | u8 val = test_bit(__obj_to_index(s, addr, p), obj_map) ? |
dd98afd4 | 4959 | SLUB_RED_INACTIVE : SLUB_RED_ACTIVE; |
53e15af0 | 4960 | |
bb192ed9 | 4961 | if (!check_object(s, slab, p, val)) |
dd98afd4 YZ |
4962 | break; |
4963 | } | |
53e15af0 CL |
4964 | } |
4965 | ||
434e245d | 4966 | static int validate_slab_node(struct kmem_cache *s, |
0a19e7dd | 4967 | struct kmem_cache_node *n, unsigned long *obj_map) |
53e15af0 CL |
4968 | { |
4969 | unsigned long count = 0; | |
bb192ed9 | 4970 | struct slab *slab; |
53e15af0 CL |
4971 | unsigned long flags; |
4972 | ||
4973 | spin_lock_irqsave(&n->list_lock, flags); | |
4974 | ||
bb192ed9 VB |
4975 | list_for_each_entry(slab, &n->partial, slab_list) { |
4976 | validate_slab(s, slab, obj_map); | |
53e15af0 CL |
4977 | count++; |
4978 | } | |
1f9f78b1 | 4979 | if (count != n->nr_partial) { |
f9f58285 FF |
4980 | pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n", |
4981 | s->name, count, n->nr_partial); | |
1f9f78b1 OG |
4982 | slab_add_kunit_errors(); |
4983 | } | |
53e15af0 CL |
4984 | |
4985 | if (!(s->flags & SLAB_STORE_USER)) | |
4986 | goto out; | |
4987 | ||
bb192ed9 VB |
4988 | list_for_each_entry(slab, &n->full, slab_list) { |
4989 | validate_slab(s, slab, obj_map); | |
53e15af0 CL |
4990 | count++; |
4991 | } | |
1f9f78b1 | 4992 | if (count != atomic_long_read(&n->nr_slabs)) { |
f9f58285 FF |
4993 | pr_err("SLUB: %s %ld slabs counted but counter=%ld\n", |
4994 | s->name, count, atomic_long_read(&n->nr_slabs)); | |
1f9f78b1 OG |
4995 | slab_add_kunit_errors(); |
4996 | } | |
53e15af0 CL |
4997 | |
4998 | out: | |
4999 | spin_unlock_irqrestore(&n->list_lock, flags); | |
5000 | return count; | |
5001 | } | |
5002 | ||
1f9f78b1 | 5003 | long validate_slab_cache(struct kmem_cache *s) |
53e15af0 CL |
5004 | { |
5005 | int node; | |
5006 | unsigned long count = 0; | |
fa45dc25 | 5007 | struct kmem_cache_node *n; |
0a19e7dd VB |
5008 | unsigned long *obj_map; |
5009 | ||
5010 | obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL); | |
5011 | if (!obj_map) | |
5012 | return -ENOMEM; | |
53e15af0 CL |
5013 | |
5014 | flush_all(s); | |
fa45dc25 | 5015 | for_each_kmem_cache_node(s, node, n) |
0a19e7dd VB |
5016 | count += validate_slab_node(s, n, obj_map); |
5017 | ||
5018 | bitmap_free(obj_map); | |
90e9f6a6 | 5019 | |
53e15af0 CL |
5020 | return count; |
5021 | } | |
1f9f78b1 OG |
5022 | EXPORT_SYMBOL(validate_slab_cache); |
5023 | ||
64dd6849 | 5024 | #ifdef CONFIG_DEBUG_FS |
88a420e4 | 5025 | /* |
672bba3a | 5026 | * Generate lists of code addresses where slabcache objects are allocated |
88a420e4 CL |
5027 | * and freed. |
5028 | */ | |
5029 | ||
5030 | struct location { | |
8ea9fb92 | 5031 | depot_stack_handle_t handle; |
88a420e4 | 5032 | unsigned long count; |
ce71e27c | 5033 | unsigned long addr; |
6edf2576 | 5034 | unsigned long waste; |
45edfa58 CL |
5035 | long long sum_time; |
5036 | long min_time; | |
5037 | long max_time; | |
5038 | long min_pid; | |
5039 | long max_pid; | |
174596a0 | 5040 | DECLARE_BITMAP(cpus, NR_CPUS); |
45edfa58 | 5041 | nodemask_t nodes; |
88a420e4 CL |
5042 | }; |
5043 | ||
5044 | struct loc_track { | |
5045 | unsigned long max; | |
5046 | unsigned long count; | |
5047 | struct location *loc; | |
005a79e5 | 5048 | loff_t idx; |
88a420e4 CL |
5049 | }; |
5050 | ||
64dd6849 FM |
5051 | static struct dentry *slab_debugfs_root; |
5052 | ||
88a420e4 CL |
5053 | static void free_loc_track(struct loc_track *t) |
5054 | { | |
5055 | if (t->max) | |
5056 | free_pages((unsigned long)t->loc, | |
5057 | get_order(sizeof(struct location) * t->max)); | |
5058 | } | |
5059 | ||
68dff6a9 | 5060 | static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags) |
88a420e4 CL |
5061 | { |
5062 | struct location *l; | |
5063 | int order; | |
5064 | ||
88a420e4 CL |
5065 | order = get_order(sizeof(struct location) * max); |
5066 | ||
68dff6a9 | 5067 | l = (void *)__get_free_pages(flags, order); |
88a420e4 CL |
5068 | if (!l) |
5069 | return 0; | |
5070 | ||
5071 | if (t->count) { | |
5072 | memcpy(l, t->loc, sizeof(struct location) * t->count); | |
5073 | free_loc_track(t); | |
5074 | } | |
5075 | t->max = max; | |
5076 | t->loc = l; | |
5077 | return 1; | |
5078 | } | |
5079 | ||
5080 | static int add_location(struct loc_track *t, struct kmem_cache *s, | |
6edf2576 FT |
5081 | const struct track *track, |
5082 | unsigned int orig_size) | |
88a420e4 CL |
5083 | { |
5084 | long start, end, pos; | |
5085 | struct location *l; | |
6edf2576 | 5086 | unsigned long caddr, chandle, cwaste; |
45edfa58 | 5087 | unsigned long age = jiffies - track->when; |
8ea9fb92 | 5088 | depot_stack_handle_t handle = 0; |
6edf2576 | 5089 | unsigned int waste = s->object_size - orig_size; |
88a420e4 | 5090 | |
8ea9fb92 OG |
5091 | #ifdef CONFIG_STACKDEPOT |
5092 | handle = READ_ONCE(track->handle); | |
5093 | #endif | |
88a420e4 CL |
5094 | start = -1; |
5095 | end = t->count; | |
5096 | ||
5097 | for ( ; ; ) { | |
5098 | pos = start + (end - start + 1) / 2; | |
5099 | ||
5100 | /* | |
5101 | * There is nothing at "end". If we end up there | |
5102 | * we need to add something to before end. | |
5103 | */ | |
5104 | if (pos == end) | |
5105 | break; | |
5106 | ||
6edf2576 FT |
5107 | l = &t->loc[pos]; |
5108 | caddr = l->addr; | |
5109 | chandle = l->handle; | |
5110 | cwaste = l->waste; | |
5111 | if ((track->addr == caddr) && (handle == chandle) && | |
5112 | (waste == cwaste)) { | |
45edfa58 | 5113 | |
45edfa58 CL |
5114 | l->count++; |
5115 | if (track->when) { | |
5116 | l->sum_time += age; | |
5117 | if (age < l->min_time) | |
5118 | l->min_time = age; | |
5119 | if (age > l->max_time) | |
5120 | l->max_time = age; | |
5121 | ||
5122 | if (track->pid < l->min_pid) | |
5123 | l->min_pid = track->pid; | |
5124 | if (track->pid > l->max_pid) | |
5125 | l->max_pid = track->pid; | |
5126 | ||
174596a0 RR |
5127 | cpumask_set_cpu(track->cpu, |
5128 | to_cpumask(l->cpus)); | |
45edfa58 CL |
5129 | } |
5130 | node_set(page_to_nid(virt_to_page(track)), l->nodes); | |
88a420e4 CL |
5131 | return 1; |
5132 | } | |
5133 | ||
45edfa58 | 5134 | if (track->addr < caddr) |
88a420e4 | 5135 | end = pos; |
8ea9fb92 OG |
5136 | else if (track->addr == caddr && handle < chandle) |
5137 | end = pos; | |
6edf2576 FT |
5138 | else if (track->addr == caddr && handle == chandle && |
5139 | waste < cwaste) | |
5140 | end = pos; | |
88a420e4 CL |
5141 | else |
5142 | start = pos; | |
5143 | } | |
5144 | ||
5145 | /* | |
672bba3a | 5146 | * Not found. Insert new tracking element. |
88a420e4 | 5147 | */ |
68dff6a9 | 5148 | if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC)) |
88a420e4 CL |
5149 | return 0; |
5150 | ||
5151 | l = t->loc + pos; | |
5152 | if (pos < t->count) | |
5153 | memmove(l + 1, l, | |
5154 | (t->count - pos) * sizeof(struct location)); | |
5155 | t->count++; | |
5156 | l->count = 1; | |
45edfa58 CL |
5157 | l->addr = track->addr; |
5158 | l->sum_time = age; | |
5159 | l->min_time = age; | |
5160 | l->max_time = age; | |
5161 | l->min_pid = track->pid; | |
5162 | l->max_pid = track->pid; | |
8ea9fb92 | 5163 | l->handle = handle; |
6edf2576 | 5164 | l->waste = waste; |
174596a0 RR |
5165 | cpumask_clear(to_cpumask(l->cpus)); |
5166 | cpumask_set_cpu(track->cpu, to_cpumask(l->cpus)); | |
45edfa58 CL |
5167 | nodes_clear(l->nodes); |
5168 | node_set(page_to_nid(virt_to_page(track)), l->nodes); | |
88a420e4 CL |
5169 | return 1; |
5170 | } | |
5171 | ||
5172 | static void process_slab(struct loc_track *t, struct kmem_cache *s, | |
bb192ed9 | 5173 | struct slab *slab, enum track_item alloc, |
b3fd64e1 | 5174 | unsigned long *obj_map) |
88a420e4 | 5175 | { |
bb192ed9 | 5176 | void *addr = slab_address(slab); |
6edf2576 | 5177 | bool is_alloc = (alloc == TRACK_ALLOC); |
88a420e4 CL |
5178 | void *p; |
5179 | ||
bb192ed9 | 5180 | __fill_map(obj_map, s, slab); |
b3fd64e1 | 5181 | |
bb192ed9 | 5182 | for_each_object(p, s, addr, slab->objects) |
b3fd64e1 | 5183 | if (!test_bit(__obj_to_index(s, addr, p), obj_map)) |
6edf2576 FT |
5184 | add_location(t, s, get_track(s, p, alloc), |
5185 | is_alloc ? get_orig_size(s, p) : | |
5186 | s->object_size); | |
88a420e4 | 5187 | } |
64dd6849 | 5188 | #endif /* CONFIG_DEBUG_FS */ |
6dfd1b65 | 5189 | #endif /* CONFIG_SLUB_DEBUG */ |
88a420e4 | 5190 | |
b1a413a3 | 5191 | #ifdef SLAB_SUPPORTS_SYSFS |
81819f0f | 5192 | enum slab_stat_type { |
205ab99d CL |
5193 | SL_ALL, /* All slabs */ |
5194 | SL_PARTIAL, /* Only partially allocated slabs */ | |
5195 | SL_CPU, /* Only slabs used for cpu caches */ | |
5196 | SL_OBJECTS, /* Determine allocated objects not slabs */ | |
5197 | SL_TOTAL /* Determine object capacity not slabs */ | |
81819f0f CL |
5198 | }; |
5199 | ||
205ab99d | 5200 | #define SO_ALL (1 << SL_ALL) |
81819f0f CL |
5201 | #define SO_PARTIAL (1 << SL_PARTIAL) |
5202 | #define SO_CPU (1 << SL_CPU) | |
5203 | #define SO_OBJECTS (1 << SL_OBJECTS) | |
205ab99d | 5204 | #define SO_TOTAL (1 << SL_TOTAL) |
81819f0f | 5205 | |
62e5c4b4 | 5206 | static ssize_t show_slab_objects(struct kmem_cache *s, |
bf16d19a | 5207 | char *buf, unsigned long flags) |
81819f0f CL |
5208 | { |
5209 | unsigned long total = 0; | |
81819f0f CL |
5210 | int node; |
5211 | int x; | |
5212 | unsigned long *nodes; | |
bf16d19a | 5213 | int len = 0; |
81819f0f | 5214 | |
6396bb22 | 5215 | nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL); |
62e5c4b4 CG |
5216 | if (!nodes) |
5217 | return -ENOMEM; | |
81819f0f | 5218 | |
205ab99d CL |
5219 | if (flags & SO_CPU) { |
5220 | int cpu; | |
81819f0f | 5221 | |
205ab99d | 5222 | for_each_possible_cpu(cpu) { |
d0e0ac97 CG |
5223 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, |
5224 | cpu); | |
ec3ab083 | 5225 | int node; |
bb192ed9 | 5226 | struct slab *slab; |
dfb4f096 | 5227 | |
bb192ed9 VB |
5228 | slab = READ_ONCE(c->slab); |
5229 | if (!slab) | |
ec3ab083 | 5230 | continue; |
205ab99d | 5231 | |
bb192ed9 | 5232 | node = slab_nid(slab); |
ec3ab083 | 5233 | if (flags & SO_TOTAL) |
bb192ed9 | 5234 | x = slab->objects; |
ec3ab083 | 5235 | else if (flags & SO_OBJECTS) |
bb192ed9 | 5236 | x = slab->inuse; |
ec3ab083 CL |
5237 | else |
5238 | x = 1; | |
49e22585 | 5239 | |
ec3ab083 CL |
5240 | total += x; |
5241 | nodes[node] += x; | |
5242 | ||
9c01e9af | 5243 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
bb192ed9 VB |
5244 | slab = slub_percpu_partial_read_once(c); |
5245 | if (slab) { | |
5246 | node = slab_nid(slab); | |
8afb1474 LZ |
5247 | if (flags & SO_TOTAL) |
5248 | WARN_ON_ONCE(1); | |
5249 | else if (flags & SO_OBJECTS) | |
5250 | WARN_ON_ONCE(1); | |
5251 | else | |
bb192ed9 | 5252 | x = slab->slabs; |
bc6697d8 ED |
5253 | total += x; |
5254 | nodes[node] += x; | |
49e22585 | 5255 | } |
9c01e9af | 5256 | #endif |
81819f0f CL |
5257 | } |
5258 | } | |
5259 | ||
e4f8e513 QC |
5260 | /* |
5261 | * It is impossible to take "mem_hotplug_lock" here with "kernfs_mutex" | |
5262 | * already held which will conflict with an existing lock order: | |
5263 | * | |
5264 | * mem_hotplug_lock->slab_mutex->kernfs_mutex | |
5265 | * | |
5266 | * We don't really need mem_hotplug_lock (to hold off | |
5267 | * slab_mem_going_offline_callback) here because slab's memory hot | |
5268 | * unplug code doesn't destroy the kmem_cache->node[] data. | |
5269 | */ | |
5270 | ||
ab4d5ed5 | 5271 | #ifdef CONFIG_SLUB_DEBUG |
205ab99d | 5272 | if (flags & SO_ALL) { |
fa45dc25 CL |
5273 | struct kmem_cache_node *n; |
5274 | ||
5275 | for_each_kmem_cache_node(s, node, n) { | |
205ab99d | 5276 | |
d0e0ac97 CG |
5277 | if (flags & SO_TOTAL) |
5278 | x = atomic_long_read(&n->total_objects); | |
5279 | else if (flags & SO_OBJECTS) | |
5280 | x = atomic_long_read(&n->total_objects) - | |
5281 | count_partial(n, count_free); | |
81819f0f | 5282 | else |
205ab99d | 5283 | x = atomic_long_read(&n->nr_slabs); |
81819f0f CL |
5284 | total += x; |
5285 | nodes[node] += x; | |
5286 | } | |
5287 | ||
ab4d5ed5 CL |
5288 | } else |
5289 | #endif | |
5290 | if (flags & SO_PARTIAL) { | |
fa45dc25 | 5291 | struct kmem_cache_node *n; |
81819f0f | 5292 | |
fa45dc25 | 5293 | for_each_kmem_cache_node(s, node, n) { |
205ab99d CL |
5294 | if (flags & SO_TOTAL) |
5295 | x = count_partial(n, count_total); | |
5296 | else if (flags & SO_OBJECTS) | |
5297 | x = count_partial(n, count_inuse); | |
81819f0f | 5298 | else |
205ab99d | 5299 | x = n->nr_partial; |
81819f0f CL |
5300 | total += x; |
5301 | nodes[node] += x; | |
5302 | } | |
5303 | } | |
bf16d19a JP |
5304 | |
5305 | len += sysfs_emit_at(buf, len, "%lu", total); | |
81819f0f | 5306 | #ifdef CONFIG_NUMA |
bf16d19a | 5307 | for (node = 0; node < nr_node_ids; node++) { |
81819f0f | 5308 | if (nodes[node]) |
bf16d19a JP |
5309 | len += sysfs_emit_at(buf, len, " N%d=%lu", |
5310 | node, nodes[node]); | |
5311 | } | |
81819f0f | 5312 | #endif |
bf16d19a | 5313 | len += sysfs_emit_at(buf, len, "\n"); |
81819f0f | 5314 | kfree(nodes); |
bf16d19a JP |
5315 | |
5316 | return len; | |
81819f0f CL |
5317 | } |
5318 | ||
81819f0f | 5319 | #define to_slab_attr(n) container_of(n, struct slab_attribute, attr) |
497888cf | 5320 | #define to_slab(n) container_of(n, struct kmem_cache, kobj) |
81819f0f CL |
5321 | |
5322 | struct slab_attribute { | |
5323 | struct attribute attr; | |
5324 | ssize_t (*show)(struct kmem_cache *s, char *buf); | |
5325 | ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count); | |
5326 | }; | |
5327 | ||
5328 | #define SLAB_ATTR_RO(_name) \ | |
d1d28bd9 | 5329 | static struct slab_attribute _name##_attr = __ATTR_RO_MODE(_name, 0400) |
81819f0f CL |
5330 | |
5331 | #define SLAB_ATTR(_name) \ | |
d1d28bd9 | 5332 | static struct slab_attribute _name##_attr = __ATTR_RW_MODE(_name, 0600) |
81819f0f | 5333 | |
81819f0f CL |
5334 | static ssize_t slab_size_show(struct kmem_cache *s, char *buf) |
5335 | { | |
bf16d19a | 5336 | return sysfs_emit(buf, "%u\n", s->size); |
81819f0f CL |
5337 | } |
5338 | SLAB_ATTR_RO(slab_size); | |
5339 | ||
5340 | static ssize_t align_show(struct kmem_cache *s, char *buf) | |
5341 | { | |
bf16d19a | 5342 | return sysfs_emit(buf, "%u\n", s->align); |
81819f0f CL |
5343 | } |
5344 | SLAB_ATTR_RO(align); | |
5345 | ||
5346 | static ssize_t object_size_show(struct kmem_cache *s, char *buf) | |
5347 | { | |
bf16d19a | 5348 | return sysfs_emit(buf, "%u\n", s->object_size); |
81819f0f CL |
5349 | } |
5350 | SLAB_ATTR_RO(object_size); | |
5351 | ||
5352 | static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf) | |
5353 | { | |
bf16d19a | 5354 | return sysfs_emit(buf, "%u\n", oo_objects(s->oo)); |
81819f0f CL |
5355 | } |
5356 | SLAB_ATTR_RO(objs_per_slab); | |
5357 | ||
5358 | static ssize_t order_show(struct kmem_cache *s, char *buf) | |
5359 | { | |
bf16d19a | 5360 | return sysfs_emit(buf, "%u\n", oo_order(s->oo)); |
81819f0f | 5361 | } |
32a6f409 | 5362 | SLAB_ATTR_RO(order); |
81819f0f | 5363 | |
73d342b1 DR |
5364 | static ssize_t min_partial_show(struct kmem_cache *s, char *buf) |
5365 | { | |
bf16d19a | 5366 | return sysfs_emit(buf, "%lu\n", s->min_partial); |
73d342b1 DR |
5367 | } |
5368 | ||
5369 | static ssize_t min_partial_store(struct kmem_cache *s, const char *buf, | |
5370 | size_t length) | |
5371 | { | |
5372 | unsigned long min; | |
5373 | int err; | |
5374 | ||
3dbb95f7 | 5375 | err = kstrtoul(buf, 10, &min); |
73d342b1 DR |
5376 | if (err) |
5377 | return err; | |
5378 | ||
5182f3c9 | 5379 | s->min_partial = min; |
73d342b1 DR |
5380 | return length; |
5381 | } | |
5382 | SLAB_ATTR(min_partial); | |
5383 | ||
49e22585 CL |
5384 | static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf) |
5385 | { | |
b47291ef VB |
5386 | unsigned int nr_partial = 0; |
5387 | #ifdef CONFIG_SLUB_CPU_PARTIAL | |
5388 | nr_partial = s->cpu_partial; | |
5389 | #endif | |
5390 | ||
5391 | return sysfs_emit(buf, "%u\n", nr_partial); | |
49e22585 CL |
5392 | } |
5393 | ||
5394 | static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf, | |
5395 | size_t length) | |
5396 | { | |
e5d9998f | 5397 | unsigned int objects; |
49e22585 CL |
5398 | int err; |
5399 | ||
e5d9998f | 5400 | err = kstrtouint(buf, 10, &objects); |
49e22585 CL |
5401 | if (err) |
5402 | return err; | |
345c905d | 5403 | if (objects && !kmem_cache_has_cpu_partial(s)) |
74ee4ef1 | 5404 | return -EINVAL; |
49e22585 | 5405 | |
e6d0e1dc | 5406 | slub_set_cpu_partial(s, objects); |
49e22585 CL |
5407 | flush_all(s); |
5408 | return length; | |
5409 | } | |
5410 | SLAB_ATTR(cpu_partial); | |
5411 | ||
81819f0f CL |
5412 | static ssize_t ctor_show(struct kmem_cache *s, char *buf) |
5413 | { | |
62c70bce JP |
5414 | if (!s->ctor) |
5415 | return 0; | |
bf16d19a | 5416 | return sysfs_emit(buf, "%pS\n", s->ctor); |
81819f0f CL |
5417 | } |
5418 | SLAB_ATTR_RO(ctor); | |
5419 | ||
81819f0f CL |
5420 | static ssize_t aliases_show(struct kmem_cache *s, char *buf) |
5421 | { | |
bf16d19a | 5422 | return sysfs_emit(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1); |
81819f0f CL |
5423 | } |
5424 | SLAB_ATTR_RO(aliases); | |
5425 | ||
81819f0f CL |
5426 | static ssize_t partial_show(struct kmem_cache *s, char *buf) |
5427 | { | |
d9acf4b7 | 5428 | return show_slab_objects(s, buf, SO_PARTIAL); |
81819f0f CL |
5429 | } |
5430 | SLAB_ATTR_RO(partial); | |
5431 | ||
5432 | static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf) | |
5433 | { | |
d9acf4b7 | 5434 | return show_slab_objects(s, buf, SO_CPU); |
81819f0f CL |
5435 | } |
5436 | SLAB_ATTR_RO(cpu_slabs); | |
5437 | ||
5438 | static ssize_t objects_show(struct kmem_cache *s, char *buf) | |
5439 | { | |
205ab99d | 5440 | return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS); |
81819f0f CL |
5441 | } |
5442 | SLAB_ATTR_RO(objects); | |
5443 | ||
205ab99d CL |
5444 | static ssize_t objects_partial_show(struct kmem_cache *s, char *buf) |
5445 | { | |
5446 | return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS); | |
5447 | } | |
5448 | SLAB_ATTR_RO(objects_partial); | |
5449 | ||
49e22585 CL |
5450 | static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf) |
5451 | { | |
5452 | int objects = 0; | |
bb192ed9 | 5453 | int slabs = 0; |
9c01e9af | 5454 | int cpu __maybe_unused; |
bf16d19a | 5455 | int len = 0; |
49e22585 | 5456 | |
9c01e9af | 5457 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
49e22585 | 5458 | for_each_online_cpu(cpu) { |
bb192ed9 | 5459 | struct slab *slab; |
a93cf07b | 5460 | |
bb192ed9 | 5461 | slab = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu)); |
49e22585 | 5462 | |
bb192ed9 VB |
5463 | if (slab) |
5464 | slabs += slab->slabs; | |
49e22585 | 5465 | } |
9c01e9af | 5466 | #endif |
49e22585 | 5467 | |
c2092c12 | 5468 | /* Approximate half-full slabs, see slub_set_cpu_partial() */ |
bb192ed9 VB |
5469 | objects = (slabs * oo_objects(s->oo)) / 2; |
5470 | len += sysfs_emit_at(buf, len, "%d(%d)", objects, slabs); | |
49e22585 | 5471 | |
9c01e9af | 5472 | #if defined(CONFIG_SLUB_CPU_PARTIAL) && defined(CONFIG_SMP) |
49e22585 | 5473 | for_each_online_cpu(cpu) { |
bb192ed9 | 5474 | struct slab *slab; |
a93cf07b | 5475 | |
bb192ed9 VB |
5476 | slab = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu)); |
5477 | if (slab) { | |
5478 | slabs = READ_ONCE(slab->slabs); | |
5479 | objects = (slabs * oo_objects(s->oo)) / 2; | |
bf16d19a | 5480 | len += sysfs_emit_at(buf, len, " C%d=%d(%d)", |
bb192ed9 | 5481 | cpu, objects, slabs); |
b47291ef | 5482 | } |
49e22585 CL |
5483 | } |
5484 | #endif | |
bf16d19a JP |
5485 | len += sysfs_emit_at(buf, len, "\n"); |
5486 | ||
5487 | return len; | |
49e22585 CL |
5488 | } |
5489 | SLAB_ATTR_RO(slabs_cpu_partial); | |
5490 | ||
a5a84755 CL |
5491 | static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf) |
5492 | { | |
bf16d19a | 5493 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT)); |
a5a84755 | 5494 | } |
8f58119a | 5495 | SLAB_ATTR_RO(reclaim_account); |
a5a84755 CL |
5496 | |
5497 | static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf) | |
5498 | { | |
bf16d19a | 5499 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN)); |
a5a84755 CL |
5500 | } |
5501 | SLAB_ATTR_RO(hwcache_align); | |
5502 | ||
5503 | #ifdef CONFIG_ZONE_DMA | |
5504 | static ssize_t cache_dma_show(struct kmem_cache *s, char *buf) | |
5505 | { | |
bf16d19a | 5506 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA)); |
a5a84755 CL |
5507 | } |
5508 | SLAB_ATTR_RO(cache_dma); | |
5509 | #endif | |
5510 | ||
346907ce | 5511 | #ifdef CONFIG_HARDENED_USERCOPY |
8eb8284b DW |
5512 | static ssize_t usersize_show(struct kmem_cache *s, char *buf) |
5513 | { | |
bf16d19a | 5514 | return sysfs_emit(buf, "%u\n", s->usersize); |
8eb8284b DW |
5515 | } |
5516 | SLAB_ATTR_RO(usersize); | |
346907ce | 5517 | #endif |
8eb8284b | 5518 | |
a5a84755 CL |
5519 | static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf) |
5520 | { | |
bf16d19a | 5521 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU)); |
a5a84755 CL |
5522 | } |
5523 | SLAB_ATTR_RO(destroy_by_rcu); | |
5524 | ||
ab4d5ed5 | 5525 | #ifdef CONFIG_SLUB_DEBUG |
a5a84755 CL |
5526 | static ssize_t slabs_show(struct kmem_cache *s, char *buf) |
5527 | { | |
5528 | return show_slab_objects(s, buf, SO_ALL); | |
5529 | } | |
5530 | SLAB_ATTR_RO(slabs); | |
5531 | ||
205ab99d CL |
5532 | static ssize_t total_objects_show(struct kmem_cache *s, char *buf) |
5533 | { | |
5534 | return show_slab_objects(s, buf, SO_ALL|SO_TOTAL); | |
5535 | } | |
5536 | SLAB_ATTR_RO(total_objects); | |
5537 | ||
81819f0f CL |
5538 | static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf) |
5539 | { | |
bf16d19a | 5540 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS)); |
81819f0f | 5541 | } |
060807f8 | 5542 | SLAB_ATTR_RO(sanity_checks); |
81819f0f CL |
5543 | |
5544 | static ssize_t trace_show(struct kmem_cache *s, char *buf) | |
5545 | { | |
bf16d19a | 5546 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TRACE)); |
81819f0f | 5547 | } |
060807f8 | 5548 | SLAB_ATTR_RO(trace); |
81819f0f | 5549 | |
81819f0f CL |
5550 | static ssize_t red_zone_show(struct kmem_cache *s, char *buf) |
5551 | { | |
bf16d19a | 5552 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE)); |
81819f0f CL |
5553 | } |
5554 | ||
ad38b5b1 | 5555 | SLAB_ATTR_RO(red_zone); |
81819f0f CL |
5556 | |
5557 | static ssize_t poison_show(struct kmem_cache *s, char *buf) | |
5558 | { | |
bf16d19a | 5559 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_POISON)); |
81819f0f CL |
5560 | } |
5561 | ||
ad38b5b1 | 5562 | SLAB_ATTR_RO(poison); |
81819f0f CL |
5563 | |
5564 | static ssize_t store_user_show(struct kmem_cache *s, char *buf) | |
5565 | { | |
bf16d19a | 5566 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_STORE_USER)); |
81819f0f CL |
5567 | } |
5568 | ||
ad38b5b1 | 5569 | SLAB_ATTR_RO(store_user); |
81819f0f | 5570 | |
53e15af0 CL |
5571 | static ssize_t validate_show(struct kmem_cache *s, char *buf) |
5572 | { | |
5573 | return 0; | |
5574 | } | |
5575 | ||
5576 | static ssize_t validate_store(struct kmem_cache *s, | |
5577 | const char *buf, size_t length) | |
5578 | { | |
434e245d CL |
5579 | int ret = -EINVAL; |
5580 | ||
c7323a5a | 5581 | if (buf[0] == '1' && kmem_cache_debug(s)) { |
434e245d CL |
5582 | ret = validate_slab_cache(s); |
5583 | if (ret >= 0) | |
5584 | ret = length; | |
5585 | } | |
5586 | return ret; | |
53e15af0 CL |
5587 | } |
5588 | SLAB_ATTR(validate); | |
a5a84755 | 5589 | |
a5a84755 CL |
5590 | #endif /* CONFIG_SLUB_DEBUG */ |
5591 | ||
5592 | #ifdef CONFIG_FAILSLAB | |
5593 | static ssize_t failslab_show(struct kmem_cache *s, char *buf) | |
5594 | { | |
bf16d19a | 5595 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB)); |
a5a84755 | 5596 | } |
060807f8 | 5597 | SLAB_ATTR_RO(failslab); |
ab4d5ed5 | 5598 | #endif |
53e15af0 | 5599 | |
2086d26a CL |
5600 | static ssize_t shrink_show(struct kmem_cache *s, char *buf) |
5601 | { | |
5602 | return 0; | |
5603 | } | |
5604 | ||
5605 | static ssize_t shrink_store(struct kmem_cache *s, | |
5606 | const char *buf, size_t length) | |
5607 | { | |
832f37f5 | 5608 | if (buf[0] == '1') |
10befea9 | 5609 | kmem_cache_shrink(s); |
832f37f5 | 5610 | else |
2086d26a CL |
5611 | return -EINVAL; |
5612 | return length; | |
5613 | } | |
5614 | SLAB_ATTR(shrink); | |
5615 | ||
81819f0f | 5616 | #ifdef CONFIG_NUMA |
9824601e | 5617 | static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf) |
81819f0f | 5618 | { |
bf16d19a | 5619 | return sysfs_emit(buf, "%u\n", s->remote_node_defrag_ratio / 10); |
81819f0f CL |
5620 | } |
5621 | ||
9824601e | 5622 | static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s, |
81819f0f CL |
5623 | const char *buf, size_t length) |
5624 | { | |
eb7235eb | 5625 | unsigned int ratio; |
0121c619 CL |
5626 | int err; |
5627 | ||
eb7235eb | 5628 | err = kstrtouint(buf, 10, &ratio); |
0121c619 CL |
5629 | if (err) |
5630 | return err; | |
eb7235eb AD |
5631 | if (ratio > 100) |
5632 | return -ERANGE; | |
0121c619 | 5633 | |
eb7235eb | 5634 | s->remote_node_defrag_ratio = ratio * 10; |
81819f0f | 5635 | |
81819f0f CL |
5636 | return length; |
5637 | } | |
9824601e | 5638 | SLAB_ATTR(remote_node_defrag_ratio); |
81819f0f CL |
5639 | #endif |
5640 | ||
8ff12cfc | 5641 | #ifdef CONFIG_SLUB_STATS |
8ff12cfc CL |
5642 | static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si) |
5643 | { | |
5644 | unsigned long sum = 0; | |
5645 | int cpu; | |
bf16d19a | 5646 | int len = 0; |
6da2ec56 | 5647 | int *data = kmalloc_array(nr_cpu_ids, sizeof(int), GFP_KERNEL); |
8ff12cfc CL |
5648 | |
5649 | if (!data) | |
5650 | return -ENOMEM; | |
5651 | ||
5652 | for_each_online_cpu(cpu) { | |
9dfc6e68 | 5653 | unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si]; |
8ff12cfc CL |
5654 | |
5655 | data[cpu] = x; | |
5656 | sum += x; | |
5657 | } | |
5658 | ||
bf16d19a | 5659 | len += sysfs_emit_at(buf, len, "%lu", sum); |
8ff12cfc | 5660 | |
50ef37b9 | 5661 | #ifdef CONFIG_SMP |
8ff12cfc | 5662 | for_each_online_cpu(cpu) { |
bf16d19a JP |
5663 | if (data[cpu]) |
5664 | len += sysfs_emit_at(buf, len, " C%d=%u", | |
5665 | cpu, data[cpu]); | |
8ff12cfc | 5666 | } |
50ef37b9 | 5667 | #endif |
8ff12cfc | 5668 | kfree(data); |
bf16d19a JP |
5669 | len += sysfs_emit_at(buf, len, "\n"); |
5670 | ||
5671 | return len; | |
8ff12cfc CL |
5672 | } |
5673 | ||
78eb00cc DR |
5674 | static void clear_stat(struct kmem_cache *s, enum stat_item si) |
5675 | { | |
5676 | int cpu; | |
5677 | ||
5678 | for_each_online_cpu(cpu) | |
9dfc6e68 | 5679 | per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0; |
78eb00cc DR |
5680 | } |
5681 | ||
8ff12cfc CL |
5682 | #define STAT_ATTR(si, text) \ |
5683 | static ssize_t text##_show(struct kmem_cache *s, char *buf) \ | |
5684 | { \ | |
5685 | return show_stat(s, buf, si); \ | |
5686 | } \ | |
78eb00cc DR |
5687 | static ssize_t text##_store(struct kmem_cache *s, \ |
5688 | const char *buf, size_t length) \ | |
5689 | { \ | |
5690 | if (buf[0] != '0') \ | |
5691 | return -EINVAL; \ | |
5692 | clear_stat(s, si); \ | |
5693 | return length; \ | |
5694 | } \ | |
5695 | SLAB_ATTR(text); \ | |
8ff12cfc CL |
5696 | |
5697 | STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath); | |
5698 | STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath); | |
5699 | STAT_ATTR(FREE_FASTPATH, free_fastpath); | |
5700 | STAT_ATTR(FREE_SLOWPATH, free_slowpath); | |
5701 | STAT_ATTR(FREE_FROZEN, free_frozen); | |
5702 | STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial); | |
5703 | STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial); | |
5704 | STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial); | |
5705 | STAT_ATTR(ALLOC_SLAB, alloc_slab); | |
5706 | STAT_ATTR(ALLOC_REFILL, alloc_refill); | |
e36a2652 | 5707 | STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch); |
8ff12cfc CL |
5708 | STAT_ATTR(FREE_SLAB, free_slab); |
5709 | STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush); | |
5710 | STAT_ATTR(DEACTIVATE_FULL, deactivate_full); | |
5711 | STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty); | |
5712 | STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head); | |
5713 | STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail); | |
5714 | STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees); | |
03e404af | 5715 | STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass); |
65c3376a | 5716 | STAT_ATTR(ORDER_FALLBACK, order_fallback); |
b789ef51 CL |
5717 | STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail); |
5718 | STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail); | |
49e22585 CL |
5719 | STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc); |
5720 | STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free); | |
8028dcea AS |
5721 | STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node); |
5722 | STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain); | |
6dfd1b65 | 5723 | #endif /* CONFIG_SLUB_STATS */ |
8ff12cfc | 5724 | |
b84e04f1 IK |
5725 | #ifdef CONFIG_KFENCE |
5726 | static ssize_t skip_kfence_show(struct kmem_cache *s, char *buf) | |
5727 | { | |
5728 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_SKIP_KFENCE)); | |
5729 | } | |
5730 | ||
5731 | static ssize_t skip_kfence_store(struct kmem_cache *s, | |
5732 | const char *buf, size_t length) | |
5733 | { | |
5734 | int ret = length; | |
5735 | ||
5736 | if (buf[0] == '0') | |
5737 | s->flags &= ~SLAB_SKIP_KFENCE; | |
5738 | else if (buf[0] == '1') | |
5739 | s->flags |= SLAB_SKIP_KFENCE; | |
5740 | else | |
5741 | ret = -EINVAL; | |
5742 | ||
5743 | return ret; | |
5744 | } | |
5745 | SLAB_ATTR(skip_kfence); | |
5746 | #endif | |
5747 | ||
06428780 | 5748 | static struct attribute *slab_attrs[] = { |
81819f0f CL |
5749 | &slab_size_attr.attr, |
5750 | &object_size_attr.attr, | |
5751 | &objs_per_slab_attr.attr, | |
5752 | &order_attr.attr, | |
73d342b1 | 5753 | &min_partial_attr.attr, |
49e22585 | 5754 | &cpu_partial_attr.attr, |
81819f0f | 5755 | &objects_attr.attr, |
205ab99d | 5756 | &objects_partial_attr.attr, |
81819f0f CL |
5757 | &partial_attr.attr, |
5758 | &cpu_slabs_attr.attr, | |
5759 | &ctor_attr.attr, | |
81819f0f CL |
5760 | &aliases_attr.attr, |
5761 | &align_attr.attr, | |
81819f0f CL |
5762 | &hwcache_align_attr.attr, |
5763 | &reclaim_account_attr.attr, | |
5764 | &destroy_by_rcu_attr.attr, | |
a5a84755 | 5765 | &shrink_attr.attr, |
49e22585 | 5766 | &slabs_cpu_partial_attr.attr, |
ab4d5ed5 | 5767 | #ifdef CONFIG_SLUB_DEBUG |
a5a84755 CL |
5768 | &total_objects_attr.attr, |
5769 | &slabs_attr.attr, | |
5770 | &sanity_checks_attr.attr, | |
5771 | &trace_attr.attr, | |
81819f0f CL |
5772 | &red_zone_attr.attr, |
5773 | &poison_attr.attr, | |
5774 | &store_user_attr.attr, | |
53e15af0 | 5775 | &validate_attr.attr, |
ab4d5ed5 | 5776 | #endif |
81819f0f CL |
5777 | #ifdef CONFIG_ZONE_DMA |
5778 | &cache_dma_attr.attr, | |
5779 | #endif | |
5780 | #ifdef CONFIG_NUMA | |
9824601e | 5781 | &remote_node_defrag_ratio_attr.attr, |
8ff12cfc CL |
5782 | #endif |
5783 | #ifdef CONFIG_SLUB_STATS | |
5784 | &alloc_fastpath_attr.attr, | |
5785 | &alloc_slowpath_attr.attr, | |
5786 | &free_fastpath_attr.attr, | |
5787 | &free_slowpath_attr.attr, | |
5788 | &free_frozen_attr.attr, | |
5789 | &free_add_partial_attr.attr, | |
5790 | &free_remove_partial_attr.attr, | |
5791 | &alloc_from_partial_attr.attr, | |
5792 | &alloc_slab_attr.attr, | |
5793 | &alloc_refill_attr.attr, | |
e36a2652 | 5794 | &alloc_node_mismatch_attr.attr, |
8ff12cfc CL |
5795 | &free_slab_attr.attr, |
5796 | &cpuslab_flush_attr.attr, | |
5797 | &deactivate_full_attr.attr, | |
5798 | &deactivate_empty_attr.attr, | |
5799 | &deactivate_to_head_attr.attr, | |
5800 | &deactivate_to_tail_attr.attr, | |
5801 | &deactivate_remote_frees_attr.attr, | |
03e404af | 5802 | &deactivate_bypass_attr.attr, |
65c3376a | 5803 | &order_fallback_attr.attr, |
b789ef51 CL |
5804 | &cmpxchg_double_fail_attr.attr, |
5805 | &cmpxchg_double_cpu_fail_attr.attr, | |
49e22585 CL |
5806 | &cpu_partial_alloc_attr.attr, |
5807 | &cpu_partial_free_attr.attr, | |
8028dcea AS |
5808 | &cpu_partial_node_attr.attr, |
5809 | &cpu_partial_drain_attr.attr, | |
81819f0f | 5810 | #endif |
4c13dd3b DM |
5811 | #ifdef CONFIG_FAILSLAB |
5812 | &failslab_attr.attr, | |
5813 | #endif | |
346907ce | 5814 | #ifdef CONFIG_HARDENED_USERCOPY |
8eb8284b | 5815 | &usersize_attr.attr, |
346907ce | 5816 | #endif |
b84e04f1 IK |
5817 | #ifdef CONFIG_KFENCE |
5818 | &skip_kfence_attr.attr, | |
5819 | #endif | |
4c13dd3b | 5820 | |
81819f0f CL |
5821 | NULL |
5822 | }; | |
5823 | ||
1fdaaa23 | 5824 | static const struct attribute_group slab_attr_group = { |
81819f0f CL |
5825 | .attrs = slab_attrs, |
5826 | }; | |
5827 | ||
5828 | static ssize_t slab_attr_show(struct kobject *kobj, | |
5829 | struct attribute *attr, | |
5830 | char *buf) | |
5831 | { | |
5832 | struct slab_attribute *attribute; | |
5833 | struct kmem_cache *s; | |
81819f0f CL |
5834 | |
5835 | attribute = to_slab_attr(attr); | |
5836 | s = to_slab(kobj); | |
5837 | ||
5838 | if (!attribute->show) | |
5839 | return -EIO; | |
5840 | ||
2bfbb027 | 5841 | return attribute->show(s, buf); |
81819f0f CL |
5842 | } |
5843 | ||
5844 | static ssize_t slab_attr_store(struct kobject *kobj, | |
5845 | struct attribute *attr, | |
5846 | const char *buf, size_t len) | |
5847 | { | |
5848 | struct slab_attribute *attribute; | |
5849 | struct kmem_cache *s; | |
81819f0f CL |
5850 | |
5851 | attribute = to_slab_attr(attr); | |
5852 | s = to_slab(kobj); | |
5853 | ||
5854 | if (!attribute->store) | |
5855 | return -EIO; | |
5856 | ||
2bfbb027 | 5857 | return attribute->store(s, buf, len); |
81819f0f CL |
5858 | } |
5859 | ||
41a21285 CL |
5860 | static void kmem_cache_release(struct kobject *k) |
5861 | { | |
5862 | slab_kmem_cache_release(to_slab(k)); | |
5863 | } | |
5864 | ||
52cf25d0 | 5865 | static const struct sysfs_ops slab_sysfs_ops = { |
81819f0f CL |
5866 | .show = slab_attr_show, |
5867 | .store = slab_attr_store, | |
5868 | }; | |
5869 | ||
5870 | static struct kobj_type slab_ktype = { | |
5871 | .sysfs_ops = &slab_sysfs_ops, | |
41a21285 | 5872 | .release = kmem_cache_release, |
81819f0f CL |
5873 | }; |
5874 | ||
27c3a314 | 5875 | static struct kset *slab_kset; |
81819f0f | 5876 | |
9a41707b VD |
5877 | static inline struct kset *cache_kset(struct kmem_cache *s) |
5878 | { | |
9a41707b VD |
5879 | return slab_kset; |
5880 | } | |
5881 | ||
d65360f2 | 5882 | #define ID_STR_LENGTH 32 |
81819f0f CL |
5883 | |
5884 | /* Create a unique string id for a slab cache: | |
6446faa2 CL |
5885 | * |
5886 | * Format :[flags-]size | |
81819f0f CL |
5887 | */ |
5888 | static char *create_unique_id(struct kmem_cache *s) | |
5889 | { | |
5890 | char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL); | |
5891 | char *p = name; | |
5892 | ||
7e9c323c CY |
5893 | if (!name) |
5894 | return ERR_PTR(-ENOMEM); | |
81819f0f CL |
5895 | |
5896 | *p++ = ':'; | |
5897 | /* | |
5898 | * First flags affecting slabcache operations. We will only | |
5899 | * get here for aliasable slabs so we do not need to support | |
5900 | * too many flags. The flags here must cover all flags that | |
5901 | * are matched during merging to guarantee that the id is | |
5902 | * unique. | |
5903 | */ | |
5904 | if (s->flags & SLAB_CACHE_DMA) | |
5905 | *p++ = 'd'; | |
6d6ea1e9 NB |
5906 | if (s->flags & SLAB_CACHE_DMA32) |
5907 | *p++ = 'D'; | |
81819f0f CL |
5908 | if (s->flags & SLAB_RECLAIM_ACCOUNT) |
5909 | *p++ = 'a'; | |
becfda68 | 5910 | if (s->flags & SLAB_CONSISTENCY_CHECKS) |
81819f0f | 5911 | *p++ = 'F'; |
230e9fc2 VD |
5912 | if (s->flags & SLAB_ACCOUNT) |
5913 | *p++ = 'A'; | |
81819f0f CL |
5914 | if (p != name + 1) |
5915 | *p++ = '-'; | |
d65360f2 | 5916 | p += snprintf(p, ID_STR_LENGTH - (p - name), "%07u", s->size); |
2633d7a0 | 5917 | |
d65360f2 CY |
5918 | if (WARN_ON(p > name + ID_STR_LENGTH - 1)) { |
5919 | kfree(name); | |
5920 | return ERR_PTR(-EINVAL); | |
5921 | } | |
68ef169a | 5922 | kmsan_unpoison_memory(name, p - name); |
81819f0f CL |
5923 | return name; |
5924 | } | |
5925 | ||
5926 | static int sysfs_slab_add(struct kmem_cache *s) | |
5927 | { | |
5928 | int err; | |
5929 | const char *name; | |
1663f26d | 5930 | struct kset *kset = cache_kset(s); |
45530c44 | 5931 | int unmergeable = slab_unmergeable(s); |
81819f0f | 5932 | |
1663f26d TH |
5933 | if (!kset) { |
5934 | kobject_init(&s->kobj, &slab_ktype); | |
5935 | return 0; | |
5936 | } | |
5937 | ||
11066386 MC |
5938 | if (!unmergeable && disable_higher_order_debug && |
5939 | (slub_debug & DEBUG_METADATA_FLAGS)) | |
5940 | unmergeable = 1; | |
5941 | ||
81819f0f CL |
5942 | if (unmergeable) { |
5943 | /* | |
5944 | * Slabcache can never be merged so we can use the name proper. | |
5945 | * This is typically the case for debug situations. In that | |
5946 | * case we can catch duplicate names easily. | |
5947 | */ | |
27c3a314 | 5948 | sysfs_remove_link(&slab_kset->kobj, s->name); |
81819f0f CL |
5949 | name = s->name; |
5950 | } else { | |
5951 | /* | |
5952 | * Create a unique name for the slab as a target | |
5953 | * for the symlinks. | |
5954 | */ | |
5955 | name = create_unique_id(s); | |
7e9c323c CY |
5956 | if (IS_ERR(name)) |
5957 | return PTR_ERR(name); | |
81819f0f CL |
5958 | } |
5959 | ||
1663f26d | 5960 | s->kobj.kset = kset; |
26e4f205 | 5961 | err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name); |
757fed1d | 5962 | if (err) |
80da026a | 5963 | goto out; |
81819f0f CL |
5964 | |
5965 | err = sysfs_create_group(&s->kobj, &slab_attr_group); | |
54b6a731 DJ |
5966 | if (err) |
5967 | goto out_del_kobj; | |
9a41707b | 5968 | |
81819f0f CL |
5969 | if (!unmergeable) { |
5970 | /* Setup first alias */ | |
5971 | sysfs_slab_alias(s, s->name); | |
81819f0f | 5972 | } |
54b6a731 DJ |
5973 | out: |
5974 | if (!unmergeable) | |
5975 | kfree(name); | |
5976 | return err; | |
5977 | out_del_kobj: | |
5978 | kobject_del(&s->kobj); | |
54b6a731 | 5979 | goto out; |
81819f0f CL |
5980 | } |
5981 | ||
d50d82fa MP |
5982 | void sysfs_slab_unlink(struct kmem_cache *s) |
5983 | { | |
5984 | if (slab_state >= FULL) | |
5985 | kobject_del(&s->kobj); | |
5986 | } | |
5987 | ||
bf5eb3de TH |
5988 | void sysfs_slab_release(struct kmem_cache *s) |
5989 | { | |
5990 | if (slab_state >= FULL) | |
5991 | kobject_put(&s->kobj); | |
81819f0f CL |
5992 | } |
5993 | ||
5994 | /* | |
5995 | * Need to buffer aliases during bootup until sysfs becomes | |
9f6c708e | 5996 | * available lest we lose that information. |
81819f0f CL |
5997 | */ |
5998 | struct saved_alias { | |
5999 | struct kmem_cache *s; | |
6000 | const char *name; | |
6001 | struct saved_alias *next; | |
6002 | }; | |
6003 | ||
5af328a5 | 6004 | static struct saved_alias *alias_list; |
81819f0f CL |
6005 | |
6006 | static int sysfs_slab_alias(struct kmem_cache *s, const char *name) | |
6007 | { | |
6008 | struct saved_alias *al; | |
6009 | ||
97d06609 | 6010 | if (slab_state == FULL) { |
81819f0f CL |
6011 | /* |
6012 | * If we have a leftover link then remove it. | |
6013 | */ | |
27c3a314 GKH |
6014 | sysfs_remove_link(&slab_kset->kobj, name); |
6015 | return sysfs_create_link(&slab_kset->kobj, &s->kobj, name); | |
81819f0f CL |
6016 | } |
6017 | ||
6018 | al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL); | |
6019 | if (!al) | |
6020 | return -ENOMEM; | |
6021 | ||
6022 | al->s = s; | |
6023 | al->name = name; | |
6024 | al->next = alias_list; | |
6025 | alias_list = al; | |
68ef169a | 6026 | kmsan_unpoison_memory(al, sizeof(*al)); |
81819f0f CL |
6027 | return 0; |
6028 | } | |
6029 | ||
6030 | static int __init slab_sysfs_init(void) | |
6031 | { | |
5b95a4ac | 6032 | struct kmem_cache *s; |
81819f0f CL |
6033 | int err; |
6034 | ||
18004c5d | 6035 | mutex_lock(&slab_mutex); |
2bce6485 | 6036 | |
d7660ce5 | 6037 | slab_kset = kset_create_and_add("slab", NULL, kernel_kobj); |
27c3a314 | 6038 | if (!slab_kset) { |
18004c5d | 6039 | mutex_unlock(&slab_mutex); |
f9f58285 | 6040 | pr_err("Cannot register slab subsystem.\n"); |
81819f0f CL |
6041 | return -ENOSYS; |
6042 | } | |
6043 | ||
97d06609 | 6044 | slab_state = FULL; |
26a7bd03 | 6045 | |
5b95a4ac | 6046 | list_for_each_entry(s, &slab_caches, list) { |
26a7bd03 | 6047 | err = sysfs_slab_add(s); |
5d540fb7 | 6048 | if (err) |
f9f58285 FF |
6049 | pr_err("SLUB: Unable to add boot slab %s to sysfs\n", |
6050 | s->name); | |
26a7bd03 | 6051 | } |
81819f0f CL |
6052 | |
6053 | while (alias_list) { | |
6054 | struct saved_alias *al = alias_list; | |
6055 | ||
6056 | alias_list = alias_list->next; | |
6057 | err = sysfs_slab_alias(al->s, al->name); | |
5d540fb7 | 6058 | if (err) |
f9f58285 FF |
6059 | pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n", |
6060 | al->name); | |
81819f0f CL |
6061 | kfree(al); |
6062 | } | |
6063 | ||
18004c5d | 6064 | mutex_unlock(&slab_mutex); |
81819f0f CL |
6065 | return 0; |
6066 | } | |
6067 | ||
6068 | __initcall(slab_sysfs_init); | |
b1a413a3 | 6069 | #endif /* SLAB_SUPPORTS_SYSFS */ |
57ed3eda | 6070 | |
64dd6849 FM |
6071 | #if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS) |
6072 | static int slab_debugfs_show(struct seq_file *seq, void *v) | |
6073 | { | |
64dd6849 | 6074 | struct loc_track *t = seq->private; |
005a79e5 GS |
6075 | struct location *l; |
6076 | unsigned long idx; | |
64dd6849 | 6077 | |
005a79e5 | 6078 | idx = (unsigned long) t->idx; |
64dd6849 FM |
6079 | if (idx < t->count) { |
6080 | l = &t->loc[idx]; | |
6081 | ||
6082 | seq_printf(seq, "%7ld ", l->count); | |
6083 | ||
6084 | if (l->addr) | |
6085 | seq_printf(seq, "%pS", (void *)l->addr); | |
6086 | else | |
6087 | seq_puts(seq, "<not-available>"); | |
6088 | ||
6edf2576 FT |
6089 | if (l->waste) |
6090 | seq_printf(seq, " waste=%lu/%lu", | |
6091 | l->count * l->waste, l->waste); | |
6092 | ||
64dd6849 FM |
6093 | if (l->sum_time != l->min_time) { |
6094 | seq_printf(seq, " age=%ld/%llu/%ld", | |
6095 | l->min_time, div_u64(l->sum_time, l->count), | |
6096 | l->max_time); | |
6097 | } else | |
6098 | seq_printf(seq, " age=%ld", l->min_time); | |
6099 | ||
6100 | if (l->min_pid != l->max_pid) | |
6101 | seq_printf(seq, " pid=%ld-%ld", l->min_pid, l->max_pid); | |
6102 | else | |
6103 | seq_printf(seq, " pid=%ld", | |
6104 | l->min_pid); | |
6105 | ||
6106 | if (num_online_cpus() > 1 && !cpumask_empty(to_cpumask(l->cpus))) | |
6107 | seq_printf(seq, " cpus=%*pbl", | |
6108 | cpumask_pr_args(to_cpumask(l->cpus))); | |
6109 | ||
6110 | if (nr_online_nodes > 1 && !nodes_empty(l->nodes)) | |
6111 | seq_printf(seq, " nodes=%*pbl", | |
6112 | nodemask_pr_args(&l->nodes)); | |
6113 | ||
8ea9fb92 OG |
6114 | #ifdef CONFIG_STACKDEPOT |
6115 | { | |
6116 | depot_stack_handle_t handle; | |
6117 | unsigned long *entries; | |
6118 | unsigned int nr_entries, j; | |
6119 | ||
6120 | handle = READ_ONCE(l->handle); | |
6121 | if (handle) { | |
6122 | nr_entries = stack_depot_fetch(handle, &entries); | |
6123 | seq_puts(seq, "\n"); | |
6124 | for (j = 0; j < nr_entries; j++) | |
6125 | seq_printf(seq, " %pS\n", (void *)entries[j]); | |
6126 | } | |
6127 | } | |
6128 | #endif | |
64dd6849 FM |
6129 | seq_puts(seq, "\n"); |
6130 | } | |
6131 | ||
6132 | if (!idx && !t->count) | |
6133 | seq_puts(seq, "No data\n"); | |
6134 | ||
6135 | return 0; | |
6136 | } | |
6137 | ||
6138 | static void slab_debugfs_stop(struct seq_file *seq, void *v) | |
6139 | { | |
6140 | } | |
6141 | ||
6142 | static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos) | |
6143 | { | |
6144 | struct loc_track *t = seq->private; | |
6145 | ||
005a79e5 | 6146 | t->idx = ++(*ppos); |
64dd6849 | 6147 | if (*ppos <= t->count) |
005a79e5 | 6148 | return ppos; |
64dd6849 FM |
6149 | |
6150 | return NULL; | |
6151 | } | |
6152 | ||
553c0369 OG |
6153 | static int cmp_loc_by_count(const void *a, const void *b, const void *data) |
6154 | { | |
6155 | struct location *loc1 = (struct location *)a; | |
6156 | struct location *loc2 = (struct location *)b; | |
6157 | ||
6158 | if (loc1->count > loc2->count) | |
6159 | return -1; | |
6160 | else | |
6161 | return 1; | |
6162 | } | |
6163 | ||
64dd6849 FM |
6164 | static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos) |
6165 | { | |
005a79e5 GS |
6166 | struct loc_track *t = seq->private; |
6167 | ||
6168 | t->idx = *ppos; | |
64dd6849 FM |
6169 | return ppos; |
6170 | } | |
6171 | ||
6172 | static const struct seq_operations slab_debugfs_sops = { | |
6173 | .start = slab_debugfs_start, | |
6174 | .next = slab_debugfs_next, | |
6175 | .stop = slab_debugfs_stop, | |
6176 | .show = slab_debugfs_show, | |
6177 | }; | |
6178 | ||
6179 | static int slab_debug_trace_open(struct inode *inode, struct file *filep) | |
6180 | { | |
6181 | ||
6182 | struct kmem_cache_node *n; | |
6183 | enum track_item alloc; | |
6184 | int node; | |
6185 | struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops, | |
6186 | sizeof(struct loc_track)); | |
6187 | struct kmem_cache *s = file_inode(filep)->i_private; | |
b3fd64e1 VB |
6188 | unsigned long *obj_map; |
6189 | ||
2127d225 ML |
6190 | if (!t) |
6191 | return -ENOMEM; | |
6192 | ||
b3fd64e1 | 6193 | obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL); |
2127d225 ML |
6194 | if (!obj_map) { |
6195 | seq_release_private(inode, filep); | |
b3fd64e1 | 6196 | return -ENOMEM; |
2127d225 | 6197 | } |
64dd6849 FM |
6198 | |
6199 | if (strcmp(filep->f_path.dentry->d_name.name, "alloc_traces") == 0) | |
6200 | alloc = TRACK_ALLOC; | |
6201 | else | |
6202 | alloc = TRACK_FREE; | |
6203 | ||
b3fd64e1 VB |
6204 | if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL)) { |
6205 | bitmap_free(obj_map); | |
2127d225 | 6206 | seq_release_private(inode, filep); |
64dd6849 | 6207 | return -ENOMEM; |
b3fd64e1 | 6208 | } |
64dd6849 | 6209 | |
64dd6849 FM |
6210 | for_each_kmem_cache_node(s, node, n) { |
6211 | unsigned long flags; | |
bb192ed9 | 6212 | struct slab *slab; |
64dd6849 FM |
6213 | |
6214 | if (!atomic_long_read(&n->nr_slabs)) | |
6215 | continue; | |
6216 | ||
6217 | spin_lock_irqsave(&n->list_lock, flags); | |
bb192ed9 VB |
6218 | list_for_each_entry(slab, &n->partial, slab_list) |
6219 | process_slab(t, s, slab, alloc, obj_map); | |
6220 | list_for_each_entry(slab, &n->full, slab_list) | |
6221 | process_slab(t, s, slab, alloc, obj_map); | |
64dd6849 FM |
6222 | spin_unlock_irqrestore(&n->list_lock, flags); |
6223 | } | |
6224 | ||
553c0369 OG |
6225 | /* Sort locations by count */ |
6226 | sort_r(t->loc, t->count, sizeof(struct location), | |
6227 | cmp_loc_by_count, NULL, NULL); | |
6228 | ||
b3fd64e1 | 6229 | bitmap_free(obj_map); |
64dd6849 FM |
6230 | return 0; |
6231 | } | |
6232 | ||
6233 | static int slab_debug_trace_release(struct inode *inode, struct file *file) | |
6234 | { | |
6235 | struct seq_file *seq = file->private_data; | |
6236 | struct loc_track *t = seq->private; | |
6237 | ||
6238 | free_loc_track(t); | |
6239 | return seq_release_private(inode, file); | |
6240 | } | |
6241 | ||
6242 | static const struct file_operations slab_debugfs_fops = { | |
6243 | .open = slab_debug_trace_open, | |
6244 | .read = seq_read, | |
6245 | .llseek = seq_lseek, | |
6246 | .release = slab_debug_trace_release, | |
6247 | }; | |
6248 | ||
6249 | static void debugfs_slab_add(struct kmem_cache *s) | |
6250 | { | |
6251 | struct dentry *slab_cache_dir; | |
6252 | ||
6253 | if (unlikely(!slab_debugfs_root)) | |
6254 | return; | |
6255 | ||
6256 | slab_cache_dir = debugfs_create_dir(s->name, slab_debugfs_root); | |
6257 | ||
6258 | debugfs_create_file("alloc_traces", 0400, | |
6259 | slab_cache_dir, s, &slab_debugfs_fops); | |
6260 | ||
6261 | debugfs_create_file("free_traces", 0400, | |
6262 | slab_cache_dir, s, &slab_debugfs_fops); | |
6263 | } | |
6264 | ||
6265 | void debugfs_slab_release(struct kmem_cache *s) | |
6266 | { | |
6267 | debugfs_remove_recursive(debugfs_lookup(s->name, slab_debugfs_root)); | |
6268 | } | |
6269 | ||
6270 | static int __init slab_debugfs_init(void) | |
6271 | { | |
6272 | struct kmem_cache *s; | |
6273 | ||
6274 | slab_debugfs_root = debugfs_create_dir("slab", NULL); | |
6275 | ||
6276 | list_for_each_entry(s, &slab_caches, list) | |
6277 | if (s->flags & SLAB_STORE_USER) | |
6278 | debugfs_slab_add(s); | |
6279 | ||
6280 | return 0; | |
6281 | ||
6282 | } | |
6283 | __initcall(slab_debugfs_init); | |
6284 | #endif | |
57ed3eda PE |
6285 | /* |
6286 | * The /proc/slabinfo ABI | |
6287 | */ | |
5b365771 | 6288 | #ifdef CONFIG_SLUB_DEBUG |
0d7561c6 | 6289 | void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo) |
57ed3eda | 6290 | { |
57ed3eda | 6291 | unsigned long nr_slabs = 0; |
205ab99d CL |
6292 | unsigned long nr_objs = 0; |
6293 | unsigned long nr_free = 0; | |
57ed3eda | 6294 | int node; |
fa45dc25 | 6295 | struct kmem_cache_node *n; |
57ed3eda | 6296 | |
fa45dc25 | 6297 | for_each_kmem_cache_node(s, node, n) { |
c17fd13e WL |
6298 | nr_slabs += node_nr_slabs(n); |
6299 | nr_objs += node_nr_objs(n); | |
205ab99d | 6300 | nr_free += count_partial(n, count_free); |
57ed3eda PE |
6301 | } |
6302 | ||
0d7561c6 GC |
6303 | sinfo->active_objs = nr_objs - nr_free; |
6304 | sinfo->num_objs = nr_objs; | |
6305 | sinfo->active_slabs = nr_slabs; | |
6306 | sinfo->num_slabs = nr_slabs; | |
6307 | sinfo->objects_per_slab = oo_objects(s->oo); | |
6308 | sinfo->cache_order = oo_order(s->oo); | |
57ed3eda PE |
6309 | } |
6310 | ||
0d7561c6 | 6311 | void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s) |
7b3c3a50 | 6312 | { |
7b3c3a50 AD |
6313 | } |
6314 | ||
b7454ad3 GC |
6315 | ssize_t slabinfo_write(struct file *file, const char __user *buffer, |
6316 | size_t count, loff_t *ppos) | |
7b3c3a50 | 6317 | { |
b7454ad3 | 6318 | return -EIO; |
7b3c3a50 | 6319 | } |
5b365771 | 6320 | #endif /* CONFIG_SLUB_DEBUG */ |