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