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