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 | ||
633b0764 | 2028 | static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain); |
8ba00bb6 | 2029 | static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags); |
49e22585 | 2030 | |
81819f0f | 2031 | /* |
672bba3a | 2032 | * Try to allocate a partial slab from a specific node. |
81819f0f | 2033 | */ |
8ba00bb6 | 2034 | static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n, |
4b1f449d | 2035 | struct page **ret_page, gfp_t gfpflags) |
81819f0f | 2036 | { |
49e22585 CL |
2037 | struct page *page, *page2; |
2038 | void *object = NULL; | |
e5d9998f | 2039 | unsigned int available = 0; |
4b1f449d | 2040 | unsigned long flags; |
633b0764 | 2041 | int objects; |
81819f0f CL |
2042 | |
2043 | /* | |
2044 | * Racy check. If we mistakenly see no partial slabs then we | |
2045 | * just allocate an empty slab. If we mistakenly try to get a | |
70b6d25e | 2046 | * partial slab and there is none available then get_partial() |
672bba3a | 2047 | * will return NULL. |
81819f0f CL |
2048 | */ |
2049 | if (!n || !n->nr_partial) | |
2050 | return NULL; | |
2051 | ||
4b1f449d | 2052 | spin_lock_irqsave(&n->list_lock, flags); |
916ac052 | 2053 | list_for_each_entry_safe(page, page2, &n->partial, slab_list) { |
8ba00bb6 | 2054 | void *t; |
49e22585 | 2055 | |
4b1f449d | 2056 | if (!pfmemalloc_match(page, gfpflags)) |
8ba00bb6 JK |
2057 | continue; |
2058 | ||
633b0764 | 2059 | t = acquire_slab(s, n, page, object == NULL, &objects); |
49e22585 | 2060 | if (!t) |
9b1ea29b | 2061 | break; |
49e22585 | 2062 | |
633b0764 | 2063 | available += objects; |
12d79634 | 2064 | if (!object) { |
75c8ff28 | 2065 | *ret_page = page; |
49e22585 | 2066 | stat(s, ALLOC_FROM_PARTIAL); |
49e22585 | 2067 | object = t; |
49e22585 | 2068 | } else { |
633b0764 | 2069 | put_cpu_partial(s, page, 0); |
8028dcea | 2070 | stat(s, CPU_PARTIAL_NODE); |
49e22585 | 2071 | } |
345c905d | 2072 | if (!kmem_cache_has_cpu_partial(s) |
e6d0e1dc | 2073 | || available > slub_cpu_partial(s) / 2) |
49e22585 CL |
2074 | break; |
2075 | ||
497b66f2 | 2076 | } |
4b1f449d | 2077 | spin_unlock_irqrestore(&n->list_lock, flags); |
497b66f2 | 2078 | return object; |
81819f0f CL |
2079 | } |
2080 | ||
2081 | /* | |
672bba3a | 2082 | * Get a page from somewhere. Search in increasing NUMA distances. |
81819f0f | 2083 | */ |
de3ec035 | 2084 | static void *get_any_partial(struct kmem_cache *s, gfp_t flags, |
75c8ff28 | 2085 | struct page **ret_page) |
81819f0f CL |
2086 | { |
2087 | #ifdef CONFIG_NUMA | |
2088 | struct zonelist *zonelist; | |
dd1a239f | 2089 | struct zoneref *z; |
54a6eb5c | 2090 | struct zone *zone; |
97a225e6 | 2091 | enum zone_type highest_zoneidx = gfp_zone(flags); |
497b66f2 | 2092 | void *object; |
cc9a6c87 | 2093 | unsigned int cpuset_mems_cookie; |
81819f0f CL |
2094 | |
2095 | /* | |
672bba3a CL |
2096 | * The defrag ratio allows a configuration of the tradeoffs between |
2097 | * inter node defragmentation and node local allocations. A lower | |
2098 | * defrag_ratio increases the tendency to do local allocations | |
2099 | * instead of attempting to obtain partial slabs from other nodes. | |
81819f0f | 2100 | * |
672bba3a CL |
2101 | * If the defrag_ratio is set to 0 then kmalloc() always |
2102 | * returns node local objects. If the ratio is higher then kmalloc() | |
2103 | * may return off node objects because partial slabs are obtained | |
2104 | * from other nodes and filled up. | |
81819f0f | 2105 | * |
43efd3ea LP |
2106 | * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100 |
2107 | * (which makes defrag_ratio = 1000) then every (well almost) | |
2108 | * allocation will first attempt to defrag slab caches on other nodes. | |
2109 | * This means scanning over all nodes to look for partial slabs which | |
2110 | * may be expensive if we do it every time we are trying to find a slab | |
672bba3a | 2111 | * with available objects. |
81819f0f | 2112 | */ |
9824601e CL |
2113 | if (!s->remote_node_defrag_ratio || |
2114 | get_cycles() % 1024 > s->remote_node_defrag_ratio) | |
81819f0f CL |
2115 | return NULL; |
2116 | ||
cc9a6c87 | 2117 | do { |
d26914d1 | 2118 | cpuset_mems_cookie = read_mems_allowed_begin(); |
2a389610 | 2119 | zonelist = node_zonelist(mempolicy_slab_node(), flags); |
97a225e6 | 2120 | for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) { |
cc9a6c87 MG |
2121 | struct kmem_cache_node *n; |
2122 | ||
2123 | n = get_node(s, zone_to_nid(zone)); | |
2124 | ||
dee2f8aa | 2125 | if (n && cpuset_zone_allowed(zone, flags) && |
cc9a6c87 | 2126 | n->nr_partial > s->min_partial) { |
75c8ff28 | 2127 | object = get_partial_node(s, n, ret_page, flags); |
cc9a6c87 MG |
2128 | if (object) { |
2129 | /* | |
d26914d1 MG |
2130 | * Don't check read_mems_allowed_retry() |
2131 | * here - if mems_allowed was updated in | |
2132 | * parallel, that was a harmless race | |
2133 | * between allocation and the cpuset | |
2134 | * update | |
cc9a6c87 | 2135 | */ |
cc9a6c87 MG |
2136 | return object; |
2137 | } | |
c0ff7453 | 2138 | } |
81819f0f | 2139 | } |
d26914d1 | 2140 | } while (read_mems_allowed_retry(cpuset_mems_cookie)); |
6dfd1b65 | 2141 | #endif /* CONFIG_NUMA */ |
81819f0f CL |
2142 | return NULL; |
2143 | } | |
2144 | ||
2145 | /* | |
2146 | * Get a partial page, lock it and return it. | |
2147 | */ | |
497b66f2 | 2148 | static void *get_partial(struct kmem_cache *s, gfp_t flags, int node, |
75c8ff28 | 2149 | struct page **ret_page) |
81819f0f | 2150 | { |
497b66f2 | 2151 | void *object; |
a561ce00 JK |
2152 | int searchnode = node; |
2153 | ||
2154 | if (node == NUMA_NO_NODE) | |
2155 | searchnode = numa_mem_id(); | |
81819f0f | 2156 | |
75c8ff28 | 2157 | object = get_partial_node(s, get_node(s, searchnode), ret_page, flags); |
497b66f2 CL |
2158 | if (object || node != NUMA_NO_NODE) |
2159 | return object; | |
81819f0f | 2160 | |
75c8ff28 | 2161 | return get_any_partial(s, flags, ret_page); |
81819f0f CL |
2162 | } |
2163 | ||
923717cb | 2164 | #ifdef CONFIG_PREEMPTION |
8a5ec0ba | 2165 | /* |
0d645ed1 | 2166 | * Calculate the next globally unique transaction for disambiguation |
8a5ec0ba CL |
2167 | * during cmpxchg. The transactions start with the cpu number and are then |
2168 | * incremented by CONFIG_NR_CPUS. | |
2169 | */ | |
2170 | #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS) | |
2171 | #else | |
2172 | /* | |
2173 | * No preemption supported therefore also no need to check for | |
2174 | * different cpus. | |
2175 | */ | |
2176 | #define TID_STEP 1 | |
2177 | #endif | |
2178 | ||
2179 | static inline unsigned long next_tid(unsigned long tid) | |
2180 | { | |
2181 | return tid + TID_STEP; | |
2182 | } | |
2183 | ||
9d5f0be0 | 2184 | #ifdef SLUB_DEBUG_CMPXCHG |
8a5ec0ba CL |
2185 | static inline unsigned int tid_to_cpu(unsigned long tid) |
2186 | { | |
2187 | return tid % TID_STEP; | |
2188 | } | |
2189 | ||
2190 | static inline unsigned long tid_to_event(unsigned long tid) | |
2191 | { | |
2192 | return tid / TID_STEP; | |
2193 | } | |
9d5f0be0 | 2194 | #endif |
8a5ec0ba CL |
2195 | |
2196 | static inline unsigned int init_tid(int cpu) | |
2197 | { | |
2198 | return cpu; | |
2199 | } | |
2200 | ||
2201 | static inline void note_cmpxchg_failure(const char *n, | |
2202 | const struct kmem_cache *s, unsigned long tid) | |
2203 | { | |
2204 | #ifdef SLUB_DEBUG_CMPXCHG | |
2205 | unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid); | |
2206 | ||
f9f58285 | 2207 | pr_info("%s %s: cmpxchg redo ", n, s->name); |
8a5ec0ba | 2208 | |
923717cb | 2209 | #ifdef CONFIG_PREEMPTION |
8a5ec0ba | 2210 | if (tid_to_cpu(tid) != tid_to_cpu(actual_tid)) |
f9f58285 | 2211 | pr_warn("due to cpu change %d -> %d\n", |
8a5ec0ba CL |
2212 | tid_to_cpu(tid), tid_to_cpu(actual_tid)); |
2213 | else | |
2214 | #endif | |
2215 | if (tid_to_event(tid) != tid_to_event(actual_tid)) | |
f9f58285 | 2216 | pr_warn("due to cpu running other code. Event %ld->%ld\n", |
8a5ec0ba CL |
2217 | tid_to_event(tid), tid_to_event(actual_tid)); |
2218 | else | |
f9f58285 | 2219 | pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n", |
8a5ec0ba CL |
2220 | actual_tid, tid, next_tid(tid)); |
2221 | #endif | |
4fdccdfb | 2222 | stat(s, CMPXCHG_DOUBLE_CPU_FAIL); |
8a5ec0ba CL |
2223 | } |
2224 | ||
788e1aad | 2225 | static void init_kmem_cache_cpus(struct kmem_cache *s) |
8a5ec0ba | 2226 | { |
8a5ec0ba CL |
2227 | int cpu; |
2228 | ||
2229 | for_each_possible_cpu(cpu) | |
2230 | per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu); | |
8a5ec0ba | 2231 | } |
2cfb7455 | 2232 | |
81819f0f | 2233 | /* |
a019d201 VB |
2234 | * Finishes removing the cpu slab. Merges cpu's freelist with page's freelist, |
2235 | * unfreezes the slabs and puts it on the proper list. | |
2236 | * Assumes the slab has been already safely taken away from kmem_cache_cpu | |
2237 | * by the caller. | |
81819f0f | 2238 | */ |
d0e0ac97 | 2239 | static void deactivate_slab(struct kmem_cache *s, struct page *page, |
a019d201 | 2240 | void *freelist) |
81819f0f | 2241 | { |
2cfb7455 | 2242 | enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE }; |
2cfb7455 | 2243 | struct kmem_cache_node *n = get_node(s, page_to_nid(page)); |
d930ff03 | 2244 | int lock = 0, free_delta = 0; |
2cfb7455 | 2245 | enum slab_modes l = M_NONE, m = M_NONE; |
d930ff03 | 2246 | void *nextfree, *freelist_iter, *freelist_tail; |
136333d1 | 2247 | int tail = DEACTIVATE_TO_HEAD; |
3406e91b | 2248 | unsigned long flags = 0; |
2cfb7455 CL |
2249 | struct page new; |
2250 | struct page old; | |
2251 | ||
2252 | if (page->freelist) { | |
84e554e6 | 2253 | stat(s, DEACTIVATE_REMOTE_FREES); |
136333d1 | 2254 | tail = DEACTIVATE_TO_TAIL; |
2cfb7455 CL |
2255 | } |
2256 | ||
894b8788 | 2257 | /* |
d930ff03 VB |
2258 | * Stage one: Count the objects on cpu's freelist as free_delta and |
2259 | * remember the last object in freelist_tail for later splicing. | |
2cfb7455 | 2260 | */ |
d930ff03 VB |
2261 | freelist_tail = NULL; |
2262 | freelist_iter = freelist; | |
2263 | while (freelist_iter) { | |
2264 | nextfree = get_freepointer(s, freelist_iter); | |
2cfb7455 | 2265 | |
52f23478 DZ |
2266 | /* |
2267 | * If 'nextfree' is invalid, it is possible that the object at | |
d930ff03 VB |
2268 | * 'freelist_iter' is already corrupted. So isolate all objects |
2269 | * starting at 'freelist_iter' by skipping them. | |
52f23478 | 2270 | */ |
d930ff03 | 2271 | if (freelist_corrupted(s, page, &freelist_iter, nextfree)) |
52f23478 DZ |
2272 | break; |
2273 | ||
d930ff03 VB |
2274 | freelist_tail = freelist_iter; |
2275 | free_delta++; | |
2cfb7455 | 2276 | |
d930ff03 | 2277 | freelist_iter = nextfree; |
2cfb7455 CL |
2278 | } |
2279 | ||
894b8788 | 2280 | /* |
d930ff03 VB |
2281 | * Stage two: Unfreeze the page while splicing the per-cpu |
2282 | * freelist to the head of page's freelist. | |
2283 | * | |
2284 | * Ensure that the page is unfrozen while the list presence | |
2285 | * reflects the actual number of objects during unfreeze. | |
2cfb7455 CL |
2286 | * |
2287 | * We setup the list membership and then perform a cmpxchg | |
2288 | * with the count. If there is a mismatch then the page | |
2289 | * is not unfrozen but the page is on the wrong list. | |
2290 | * | |
2291 | * Then we restart the process which may have to remove | |
2292 | * the page from the list that we just put it on again | |
2293 | * because the number of objects in the slab may have | |
2294 | * changed. | |
894b8788 | 2295 | */ |
2cfb7455 | 2296 | redo: |
894b8788 | 2297 | |
d930ff03 VB |
2298 | old.freelist = READ_ONCE(page->freelist); |
2299 | old.counters = READ_ONCE(page->counters); | |
a0132ac0 | 2300 | VM_BUG_ON(!old.frozen); |
7c2e132c | 2301 | |
2cfb7455 CL |
2302 | /* Determine target state of the slab */ |
2303 | new.counters = old.counters; | |
d930ff03 VB |
2304 | if (freelist_tail) { |
2305 | new.inuse -= free_delta; | |
2306 | set_freepointer(s, freelist_tail, old.freelist); | |
2cfb7455 CL |
2307 | new.freelist = freelist; |
2308 | } else | |
2309 | new.freelist = old.freelist; | |
2310 | ||
2311 | new.frozen = 0; | |
2312 | ||
8a5b20ae | 2313 | if (!new.inuse && n->nr_partial >= s->min_partial) |
2cfb7455 CL |
2314 | m = M_FREE; |
2315 | else if (new.freelist) { | |
2316 | m = M_PARTIAL; | |
2317 | if (!lock) { | |
2318 | lock = 1; | |
2319 | /* | |
8bb4e7a2 | 2320 | * Taking the spinlock removes the possibility |
2cfb7455 CL |
2321 | * that acquire_slab() will see a slab page that |
2322 | * is frozen | |
2323 | */ | |
3406e91b | 2324 | spin_lock_irqsave(&n->list_lock, flags); |
2cfb7455 CL |
2325 | } |
2326 | } else { | |
2327 | m = M_FULL; | |
965c4848 | 2328 | if (kmem_cache_debug_flags(s, SLAB_STORE_USER) && !lock) { |
2cfb7455 CL |
2329 | lock = 1; |
2330 | /* | |
2331 | * This also ensures that the scanning of full | |
2332 | * slabs from diagnostic functions will not see | |
2333 | * any frozen slabs. | |
2334 | */ | |
3406e91b | 2335 | spin_lock_irqsave(&n->list_lock, flags); |
2cfb7455 CL |
2336 | } |
2337 | } | |
2338 | ||
2339 | if (l != m) { | |
2cfb7455 | 2340 | if (l == M_PARTIAL) |
2cfb7455 | 2341 | remove_partial(n, page); |
2cfb7455 | 2342 | else if (l == M_FULL) |
c65c1877 | 2343 | remove_full(s, n, page); |
2cfb7455 | 2344 | |
88349a28 | 2345 | if (m == M_PARTIAL) |
2cfb7455 | 2346 | add_partial(n, page, tail); |
88349a28 | 2347 | else if (m == M_FULL) |
2cfb7455 | 2348 | add_full(s, n, page); |
2cfb7455 CL |
2349 | } |
2350 | ||
2351 | l = m; | |
3406e91b | 2352 | if (!cmpxchg_double_slab(s, page, |
2cfb7455 CL |
2353 | old.freelist, old.counters, |
2354 | new.freelist, new.counters, | |
2355 | "unfreezing slab")) | |
2356 | goto redo; | |
2357 | ||
2cfb7455 | 2358 | if (lock) |
3406e91b | 2359 | spin_unlock_irqrestore(&n->list_lock, flags); |
2cfb7455 | 2360 | |
88349a28 WY |
2361 | if (m == M_PARTIAL) |
2362 | stat(s, tail); | |
2363 | else if (m == M_FULL) | |
2364 | stat(s, DEACTIVATE_FULL); | |
2365 | else if (m == M_FREE) { | |
2cfb7455 CL |
2366 | stat(s, DEACTIVATE_EMPTY); |
2367 | discard_slab(s, page); | |
2368 | stat(s, FREE_SLAB); | |
894b8788 | 2369 | } |
81819f0f CL |
2370 | } |
2371 | ||
345c905d | 2372 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
fc1455f4 VB |
2373 | static void __unfreeze_partials(struct kmem_cache *s, struct page *partial_page) |
2374 | { | |
43d77867 | 2375 | struct kmem_cache_node *n = NULL, *n2 = NULL; |
fc1455f4 | 2376 | struct page *page, *discard_page = NULL; |
7cf9f3ba | 2377 | unsigned long flags = 0; |
49e22585 | 2378 | |
c2f973ba | 2379 | while (partial_page) { |
49e22585 CL |
2380 | struct page new; |
2381 | struct page old; | |
2382 | ||
c2f973ba VB |
2383 | page = partial_page; |
2384 | partial_page = page->next; | |
43d77867 JK |
2385 | |
2386 | n2 = get_node(s, page_to_nid(page)); | |
2387 | if (n != n2) { | |
2388 | if (n) | |
7cf9f3ba | 2389 | spin_unlock_irqrestore(&n->list_lock, flags); |
43d77867 JK |
2390 | |
2391 | n = n2; | |
7cf9f3ba | 2392 | spin_lock_irqsave(&n->list_lock, flags); |
43d77867 | 2393 | } |
49e22585 CL |
2394 | |
2395 | do { | |
2396 | ||
2397 | old.freelist = page->freelist; | |
2398 | old.counters = page->counters; | |
a0132ac0 | 2399 | VM_BUG_ON(!old.frozen); |
49e22585 CL |
2400 | |
2401 | new.counters = old.counters; | |
2402 | new.freelist = old.freelist; | |
2403 | ||
2404 | new.frozen = 0; | |
2405 | ||
d24ac77f | 2406 | } while (!__cmpxchg_double_slab(s, page, |
49e22585 CL |
2407 | old.freelist, old.counters, |
2408 | new.freelist, new.counters, | |
2409 | "unfreezing slab")); | |
2410 | ||
8a5b20ae | 2411 | if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) { |
9ada1934 SL |
2412 | page->next = discard_page; |
2413 | discard_page = page; | |
43d77867 JK |
2414 | } else { |
2415 | add_partial(n, page, DEACTIVATE_TO_TAIL); | |
2416 | stat(s, FREE_ADD_PARTIAL); | |
49e22585 CL |
2417 | } |
2418 | } | |
2419 | ||
2420 | if (n) | |
7cf9f3ba | 2421 | spin_unlock_irqrestore(&n->list_lock, flags); |
8de06a6f | 2422 | |
9ada1934 SL |
2423 | while (discard_page) { |
2424 | page = discard_page; | |
2425 | discard_page = discard_page->next; | |
2426 | ||
2427 | stat(s, DEACTIVATE_EMPTY); | |
2428 | discard_slab(s, page); | |
2429 | stat(s, FREE_SLAB); | |
2430 | } | |
fc1455f4 | 2431 | } |
f3ab8b6b | 2432 | |
fc1455f4 VB |
2433 | /* |
2434 | * Unfreeze all the cpu partial slabs. | |
2435 | */ | |
2436 | static void unfreeze_partials(struct kmem_cache *s) | |
2437 | { | |
2438 | struct page *partial_page; | |
2439 | unsigned long flags; | |
2440 | ||
2441 | local_irq_save(flags); | |
2442 | partial_page = this_cpu_read(s->cpu_slab->partial); | |
2443 | this_cpu_write(s->cpu_slab->partial, NULL); | |
2444 | local_irq_restore(flags); | |
2445 | ||
2446 | if (partial_page) | |
2447 | __unfreeze_partials(s, partial_page); | |
2448 | } | |
2449 | ||
2450 | static void unfreeze_partials_cpu(struct kmem_cache *s, | |
2451 | struct kmem_cache_cpu *c) | |
2452 | { | |
2453 | struct page *partial_page; | |
2454 | ||
2455 | partial_page = slub_percpu_partial(c); | |
2456 | c->partial = NULL; | |
2457 | ||
2458 | if (partial_page) | |
2459 | __unfreeze_partials(s, partial_page); | |
49e22585 CL |
2460 | } |
2461 | ||
fc1455f4 VB |
2462 | #else /* CONFIG_SLUB_CPU_PARTIAL */ |
2463 | ||
2464 | static inline void unfreeze_partials(struct kmem_cache *s) { } | |
2465 | static inline void unfreeze_partials_cpu(struct kmem_cache *s, | |
2466 | struct kmem_cache_cpu *c) { } | |
2467 | ||
2468 | #endif /* CONFIG_SLUB_CPU_PARTIAL */ | |
2469 | ||
49e22585 | 2470 | /* |
9234bae9 WY |
2471 | * Put a page that was just frozen (in __slab_free|get_partial_node) into a |
2472 | * partial page slot if available. | |
49e22585 CL |
2473 | * |
2474 | * If we did not find a slot then simply move all the partials to the | |
2475 | * per node partial list. | |
2476 | */ | |
633b0764 | 2477 | static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) |
49e22585 | 2478 | { |
345c905d | 2479 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
49e22585 CL |
2480 | struct page *oldpage; |
2481 | int pages; | |
2482 | int pobjects; | |
2483 | ||
d6e0b7fa | 2484 | preempt_disable(); |
49e22585 CL |
2485 | do { |
2486 | pages = 0; | |
2487 | pobjects = 0; | |
2488 | oldpage = this_cpu_read(s->cpu_slab->partial); | |
2489 | ||
2490 | if (oldpage) { | |
2491 | pobjects = oldpage->pobjects; | |
2492 | pages = oldpage->pages; | |
bbd4e305 | 2493 | if (drain && pobjects > slub_cpu_partial(s)) { |
49e22585 CL |
2494 | /* |
2495 | * partial array is full. Move the existing | |
2496 | * set to the per node partial list. | |
2497 | */ | |
fc1455f4 | 2498 | unfreeze_partials(s); |
e24fc410 | 2499 | oldpage = NULL; |
49e22585 CL |
2500 | pobjects = 0; |
2501 | pages = 0; | |
8028dcea | 2502 | stat(s, CPU_PARTIAL_DRAIN); |
49e22585 CL |
2503 | } |
2504 | } | |
2505 | ||
2506 | pages++; | |
2507 | pobjects += page->objects - page->inuse; | |
2508 | ||
2509 | page->pages = pages; | |
2510 | page->pobjects = pobjects; | |
2511 | page->next = oldpage; | |
2512 | ||
d0e0ac97 CG |
2513 | } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) |
2514 | != oldpage); | |
d6e0b7fa | 2515 | preempt_enable(); |
6dfd1b65 | 2516 | #endif /* CONFIG_SLUB_CPU_PARTIAL */ |
49e22585 CL |
2517 | } |
2518 | ||
dfb4f096 | 2519 | static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c) |
81819f0f | 2520 | { |
5a836bf6 SAS |
2521 | unsigned long flags; |
2522 | struct page *page; | |
2523 | void *freelist; | |
2524 | ||
2525 | local_irq_save(flags); | |
2526 | ||
2527 | page = c->page; | |
2528 | freelist = c->freelist; | |
c17dda40 | 2529 | |
a019d201 VB |
2530 | c->page = NULL; |
2531 | c->freelist = NULL; | |
c17dda40 | 2532 | c->tid = next_tid(c->tid); |
a019d201 | 2533 | |
5a836bf6 | 2534 | local_irq_restore(flags); |
a019d201 | 2535 | |
5a836bf6 SAS |
2536 | if (page) { |
2537 | deactivate_slab(s, page, freelist); | |
2538 | stat(s, CPUSLAB_FLUSH); | |
2539 | } | |
81819f0f CL |
2540 | } |
2541 | ||
0c710013 | 2542 | static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu) |
81819f0f | 2543 | { |
9dfc6e68 | 2544 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu); |
08beb547 VB |
2545 | void *freelist = c->freelist; |
2546 | struct page *page = c->page; | |
81819f0f | 2547 | |
08beb547 VB |
2548 | c->page = NULL; |
2549 | c->freelist = NULL; | |
2550 | c->tid = next_tid(c->tid); | |
2551 | ||
2552 | if (page) { | |
2553 | deactivate_slab(s, page, freelist); | |
2554 | stat(s, CPUSLAB_FLUSH); | |
2555 | } | |
49e22585 | 2556 | |
fc1455f4 | 2557 | unfreeze_partials_cpu(s, c); |
81819f0f CL |
2558 | } |
2559 | ||
5a836bf6 SAS |
2560 | struct slub_flush_work { |
2561 | struct work_struct work; | |
2562 | struct kmem_cache *s; | |
2563 | bool skip; | |
2564 | }; | |
2565 | ||
fc1455f4 VB |
2566 | /* |
2567 | * Flush cpu slab. | |
2568 | * | |
5a836bf6 | 2569 | * Called from CPU work handler with migration disabled. |
fc1455f4 | 2570 | */ |
5a836bf6 | 2571 | static void flush_cpu_slab(struct work_struct *w) |
81819f0f | 2572 | { |
5a836bf6 SAS |
2573 | struct kmem_cache *s; |
2574 | struct kmem_cache_cpu *c; | |
2575 | struct slub_flush_work *sfw; | |
2576 | ||
2577 | sfw = container_of(w, struct slub_flush_work, work); | |
2578 | ||
2579 | s = sfw->s; | |
2580 | c = this_cpu_ptr(s->cpu_slab); | |
fc1455f4 VB |
2581 | |
2582 | if (c->page) | |
2583 | flush_slab(s, c); | |
81819f0f | 2584 | |
fc1455f4 | 2585 | unfreeze_partials(s); |
81819f0f CL |
2586 | } |
2587 | ||
5a836bf6 | 2588 | static bool has_cpu_slab(int cpu, struct kmem_cache *s) |
a8364d55 | 2589 | { |
a8364d55 GBY |
2590 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu); |
2591 | ||
a93cf07b | 2592 | return c->page || slub_percpu_partial(c); |
a8364d55 GBY |
2593 | } |
2594 | ||
5a836bf6 SAS |
2595 | static DEFINE_MUTEX(flush_lock); |
2596 | static DEFINE_PER_CPU(struct slub_flush_work, slub_flush); | |
2597 | ||
2598 | static void flush_all_cpus_locked(struct kmem_cache *s) | |
2599 | { | |
2600 | struct slub_flush_work *sfw; | |
2601 | unsigned int cpu; | |
2602 | ||
2603 | lockdep_assert_cpus_held(); | |
2604 | mutex_lock(&flush_lock); | |
2605 | ||
2606 | for_each_online_cpu(cpu) { | |
2607 | sfw = &per_cpu(slub_flush, cpu); | |
2608 | if (!has_cpu_slab(cpu, s)) { | |
2609 | sfw->skip = true; | |
2610 | continue; | |
2611 | } | |
2612 | INIT_WORK(&sfw->work, flush_cpu_slab); | |
2613 | sfw->skip = false; | |
2614 | sfw->s = s; | |
2615 | schedule_work_on(cpu, &sfw->work); | |
2616 | } | |
2617 | ||
2618 | for_each_online_cpu(cpu) { | |
2619 | sfw = &per_cpu(slub_flush, cpu); | |
2620 | if (sfw->skip) | |
2621 | continue; | |
2622 | flush_work(&sfw->work); | |
2623 | } | |
2624 | ||
2625 | mutex_unlock(&flush_lock); | |
2626 | } | |
2627 | ||
81819f0f CL |
2628 | static void flush_all(struct kmem_cache *s) |
2629 | { | |
5a836bf6 SAS |
2630 | cpus_read_lock(); |
2631 | flush_all_cpus_locked(s); | |
2632 | cpus_read_unlock(); | |
81819f0f CL |
2633 | } |
2634 | ||
a96a87bf SAS |
2635 | /* |
2636 | * Use the cpu notifier to insure that the cpu slabs are flushed when | |
2637 | * necessary. | |
2638 | */ | |
2639 | static int slub_cpu_dead(unsigned int cpu) | |
2640 | { | |
2641 | struct kmem_cache *s; | |
a96a87bf SAS |
2642 | |
2643 | mutex_lock(&slab_mutex); | |
0e7ac738 | 2644 | list_for_each_entry(s, &slab_caches, list) |
a96a87bf | 2645 | __flush_cpu_slab(s, cpu); |
a96a87bf SAS |
2646 | mutex_unlock(&slab_mutex); |
2647 | return 0; | |
2648 | } | |
2649 | ||
dfb4f096 CL |
2650 | /* |
2651 | * Check if the objects in a per cpu structure fit numa | |
2652 | * locality expectations. | |
2653 | */ | |
57d437d2 | 2654 | static inline int node_match(struct page *page, int node) |
dfb4f096 CL |
2655 | { |
2656 | #ifdef CONFIG_NUMA | |
6159d0f5 | 2657 | if (node != NUMA_NO_NODE && page_to_nid(page) != node) |
dfb4f096 CL |
2658 | return 0; |
2659 | #endif | |
2660 | return 1; | |
2661 | } | |
2662 | ||
9a02d699 | 2663 | #ifdef CONFIG_SLUB_DEBUG |
781b2ba6 PE |
2664 | static int count_free(struct page *page) |
2665 | { | |
2666 | return page->objects - page->inuse; | |
2667 | } | |
2668 | ||
9a02d699 DR |
2669 | static inline unsigned long node_nr_objs(struct kmem_cache_node *n) |
2670 | { | |
2671 | return atomic_long_read(&n->total_objects); | |
2672 | } | |
2673 | #endif /* CONFIG_SLUB_DEBUG */ | |
2674 | ||
2675 | #if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS) | |
781b2ba6 PE |
2676 | static unsigned long count_partial(struct kmem_cache_node *n, |
2677 | int (*get_count)(struct page *)) | |
2678 | { | |
2679 | unsigned long flags; | |
2680 | unsigned long x = 0; | |
2681 | struct page *page; | |
2682 | ||
2683 | spin_lock_irqsave(&n->list_lock, flags); | |
916ac052 | 2684 | list_for_each_entry(page, &n->partial, slab_list) |
781b2ba6 PE |
2685 | x += get_count(page); |
2686 | spin_unlock_irqrestore(&n->list_lock, flags); | |
2687 | return x; | |
2688 | } | |
9a02d699 | 2689 | #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */ |
26c02cf0 | 2690 | |
781b2ba6 PE |
2691 | static noinline void |
2692 | slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid) | |
2693 | { | |
9a02d699 DR |
2694 | #ifdef CONFIG_SLUB_DEBUG |
2695 | static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL, | |
2696 | DEFAULT_RATELIMIT_BURST); | |
781b2ba6 | 2697 | int node; |
fa45dc25 | 2698 | struct kmem_cache_node *n; |
781b2ba6 | 2699 | |
9a02d699 DR |
2700 | if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs)) |
2701 | return; | |
2702 | ||
5b3810e5 VB |
2703 | pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n", |
2704 | nid, gfpflags, &gfpflags); | |
19af27af | 2705 | pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n", |
f9f58285 FF |
2706 | s->name, s->object_size, s->size, oo_order(s->oo), |
2707 | oo_order(s->min)); | |
781b2ba6 | 2708 | |
3b0efdfa | 2709 | if (oo_order(s->min) > get_order(s->object_size)) |
f9f58285 FF |
2710 | pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n", |
2711 | s->name); | |
fa5ec8a1 | 2712 | |
fa45dc25 | 2713 | for_each_kmem_cache_node(s, node, n) { |
781b2ba6 PE |
2714 | unsigned long nr_slabs; |
2715 | unsigned long nr_objs; | |
2716 | unsigned long nr_free; | |
2717 | ||
26c02cf0 AB |
2718 | nr_free = count_partial(n, count_free); |
2719 | nr_slabs = node_nr_slabs(n); | |
2720 | nr_objs = node_nr_objs(n); | |
781b2ba6 | 2721 | |
f9f58285 | 2722 | pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n", |
781b2ba6 PE |
2723 | node, nr_slabs, nr_objs, nr_free); |
2724 | } | |
9a02d699 | 2725 | #endif |
781b2ba6 PE |
2726 | } |
2727 | ||
072bb0aa MG |
2728 | static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags) |
2729 | { | |
2730 | if (unlikely(PageSlabPfmemalloc(page))) | |
2731 | return gfp_pfmemalloc_allowed(gfpflags); | |
2732 | ||
2733 | return true; | |
2734 | } | |
2735 | ||
0b303fb4 VB |
2736 | /* |
2737 | * A variant of pfmemalloc_match() that tests page flags without asserting | |
2738 | * PageSlab. Intended for opportunistic checks before taking a lock and | |
2739 | * rechecking that nobody else freed the page under us. | |
2740 | */ | |
2741 | static inline bool pfmemalloc_match_unsafe(struct page *page, gfp_t gfpflags) | |
2742 | { | |
2743 | if (unlikely(__PageSlabPfmemalloc(page))) | |
2744 | return gfp_pfmemalloc_allowed(gfpflags); | |
2745 | ||
2746 | return true; | |
2747 | } | |
2748 | ||
213eeb9f | 2749 | /* |
d0e0ac97 CG |
2750 | * Check the page->freelist of a page and either transfer the freelist to the |
2751 | * per cpu freelist or deactivate the page. | |
213eeb9f CL |
2752 | * |
2753 | * The page is still frozen if the return value is not NULL. | |
2754 | * | |
2755 | * If this function returns NULL then the page has been unfrozen. | |
d24ac77f JK |
2756 | * |
2757 | * This function must be called with interrupt disabled. | |
213eeb9f CL |
2758 | */ |
2759 | static inline void *get_freelist(struct kmem_cache *s, struct page *page) | |
2760 | { | |
2761 | struct page new; | |
2762 | unsigned long counters; | |
2763 | void *freelist; | |
2764 | ||
2765 | do { | |
2766 | freelist = page->freelist; | |
2767 | counters = page->counters; | |
6faa6833 | 2768 | |
213eeb9f | 2769 | new.counters = counters; |
a0132ac0 | 2770 | VM_BUG_ON(!new.frozen); |
213eeb9f CL |
2771 | |
2772 | new.inuse = page->objects; | |
2773 | new.frozen = freelist != NULL; | |
2774 | ||
d24ac77f | 2775 | } while (!__cmpxchg_double_slab(s, page, |
213eeb9f CL |
2776 | freelist, counters, |
2777 | NULL, new.counters, | |
2778 | "get_freelist")); | |
2779 | ||
2780 | return freelist; | |
2781 | } | |
2782 | ||
81819f0f | 2783 | /* |
894b8788 CL |
2784 | * Slow path. The lockless freelist is empty or we need to perform |
2785 | * debugging duties. | |
2786 | * | |
894b8788 CL |
2787 | * Processing is still very fast if new objects have been freed to the |
2788 | * regular freelist. In that case we simply take over the regular freelist | |
2789 | * as the lockless freelist and zap the regular freelist. | |
81819f0f | 2790 | * |
894b8788 CL |
2791 | * If that is not working then we fall back to the partial lists. We take the |
2792 | * first element of the freelist as the object to allocate now and move the | |
2793 | * rest of the freelist to the lockless freelist. | |
81819f0f | 2794 | * |
894b8788 | 2795 | * And if we were unable to get a new slab from the partial slab lists then |
6446faa2 CL |
2796 | * we need to allocate a new slab. This is the slowest path since it involves |
2797 | * a call to the page allocator and the setup of a new slab. | |
a380a3c7 | 2798 | * |
e500059b | 2799 | * Version of __slab_alloc to use when we know that preemption is |
a380a3c7 | 2800 | * already disabled (which is the case for bulk allocation). |
81819f0f | 2801 | */ |
a380a3c7 | 2802 | static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, |
ce71e27c | 2803 | unsigned long addr, struct kmem_cache_cpu *c) |
81819f0f | 2804 | { |
6faa6833 | 2805 | void *freelist; |
f6e7def7 | 2806 | struct page *page; |
e500059b | 2807 | unsigned long flags; |
81819f0f | 2808 | |
9f986d99 AW |
2809 | stat(s, ALLOC_SLOWPATH); |
2810 | ||
0b303fb4 VB |
2811 | reread_page: |
2812 | ||
2813 | page = READ_ONCE(c->page); | |
0715e6c5 VB |
2814 | if (!page) { |
2815 | /* | |
2816 | * if the node is not online or has no normal memory, just | |
2817 | * ignore the node constraint | |
2818 | */ | |
2819 | if (unlikely(node != NUMA_NO_NODE && | |
7e1fa93d | 2820 | !node_isset(node, slab_nodes))) |
0715e6c5 | 2821 | node = NUMA_NO_NODE; |
81819f0f | 2822 | goto new_slab; |
0715e6c5 | 2823 | } |
49e22585 | 2824 | redo: |
6faa6833 | 2825 | |
57d437d2 | 2826 | if (unlikely(!node_match(page, node))) { |
0715e6c5 VB |
2827 | /* |
2828 | * same as above but node_match() being false already | |
2829 | * implies node != NUMA_NO_NODE | |
2830 | */ | |
7e1fa93d | 2831 | if (!node_isset(node, slab_nodes)) { |
0715e6c5 VB |
2832 | node = NUMA_NO_NODE; |
2833 | goto redo; | |
2834 | } else { | |
a561ce00 | 2835 | stat(s, ALLOC_NODE_MISMATCH); |
0b303fb4 | 2836 | goto deactivate_slab; |
a561ce00 | 2837 | } |
fc59c053 | 2838 | } |
6446faa2 | 2839 | |
072bb0aa MG |
2840 | /* |
2841 | * By rights, we should be searching for a slab page that was | |
2842 | * PFMEMALLOC but right now, we are losing the pfmemalloc | |
2843 | * information when the page leaves the per-cpu allocator | |
2844 | */ | |
0b303fb4 VB |
2845 | if (unlikely(!pfmemalloc_match_unsafe(page, gfpflags))) |
2846 | goto deactivate_slab; | |
072bb0aa | 2847 | |
0b303fb4 VB |
2848 | /* must check again c->page in case IRQ handler changed it */ |
2849 | local_irq_save(flags); | |
2850 | if (unlikely(page != c->page)) { | |
2851 | local_irq_restore(flags); | |
2852 | goto reread_page; | |
2853 | } | |
6faa6833 CL |
2854 | freelist = c->freelist; |
2855 | if (freelist) | |
73736e03 | 2856 | goto load_freelist; |
03e404af | 2857 | |
f6e7def7 | 2858 | freelist = get_freelist(s, page); |
6446faa2 | 2859 | |
6faa6833 | 2860 | if (!freelist) { |
03e404af | 2861 | c->page = NULL; |
fa417ab7 | 2862 | local_irq_restore(flags); |
03e404af | 2863 | stat(s, DEACTIVATE_BYPASS); |
fc59c053 | 2864 | goto new_slab; |
03e404af | 2865 | } |
6446faa2 | 2866 | |
84e554e6 | 2867 | stat(s, ALLOC_REFILL); |
6446faa2 | 2868 | |
894b8788 | 2869 | load_freelist: |
0b303fb4 VB |
2870 | |
2871 | lockdep_assert_irqs_disabled(); | |
2872 | ||
507effea CL |
2873 | /* |
2874 | * freelist is pointing to the list of objects to be used. | |
2875 | * page is pointing to the page from which the objects are obtained. | |
2876 | * That page must be frozen for per cpu allocations to work. | |
2877 | */ | |
a0132ac0 | 2878 | VM_BUG_ON(!c->page->frozen); |
6faa6833 | 2879 | c->freelist = get_freepointer(s, freelist); |
8a5ec0ba | 2880 | c->tid = next_tid(c->tid); |
e500059b | 2881 | local_irq_restore(flags); |
6faa6833 | 2882 | return freelist; |
81819f0f | 2883 | |
0b303fb4 VB |
2884 | deactivate_slab: |
2885 | ||
2886 | local_irq_save(flags); | |
2887 | if (page != c->page) { | |
2888 | local_irq_restore(flags); | |
2889 | goto reread_page; | |
2890 | } | |
a019d201 VB |
2891 | freelist = c->freelist; |
2892 | c->page = NULL; | |
2893 | c->freelist = NULL; | |
fa417ab7 | 2894 | local_irq_restore(flags); |
cfdf836e | 2895 | deactivate_slab(s, page, freelist); |
0b303fb4 | 2896 | |
81819f0f | 2897 | new_slab: |
2cfb7455 | 2898 | |
a93cf07b | 2899 | if (slub_percpu_partial(c)) { |
fa417ab7 VB |
2900 | local_irq_save(flags); |
2901 | if (unlikely(c->page)) { | |
2902 | local_irq_restore(flags); | |
2903 | goto reread_page; | |
2904 | } | |
4b1f449d VB |
2905 | if (unlikely(!slub_percpu_partial(c))) { |
2906 | local_irq_restore(flags); | |
fa417ab7 | 2907 | goto new_objects; /* stolen by an IRQ handler */ |
4b1f449d | 2908 | } |
fa417ab7 | 2909 | |
a93cf07b WY |
2910 | page = c->page = slub_percpu_partial(c); |
2911 | slub_set_percpu_partial(c, page); | |
0b303fb4 | 2912 | local_irq_restore(flags); |
49e22585 | 2913 | stat(s, CPU_PARTIAL_ALLOC); |
49e22585 | 2914 | goto redo; |
81819f0f CL |
2915 | } |
2916 | ||
fa417ab7 VB |
2917 | new_objects: |
2918 | ||
75c8ff28 | 2919 | freelist = get_partial(s, gfpflags, node, &page); |
3f2b77e3 | 2920 | if (freelist) |
2a904905 VB |
2921 | goto check_new_page; |
2922 | ||
e500059b | 2923 | put_cpu_ptr(s->cpu_slab); |
53a0de06 | 2924 | page = new_slab(s, gfpflags, node); |
e500059b | 2925 | c = get_cpu_ptr(s->cpu_slab); |
01ad8a7b | 2926 | |
53a0de06 | 2927 | if (unlikely(!page)) { |
9a02d699 | 2928 | slab_out_of_memory(s, gfpflags, node); |
f4697436 | 2929 | return NULL; |
81819f0f | 2930 | } |
2cfb7455 | 2931 | |
53a0de06 VB |
2932 | /* |
2933 | * No other reference to the page yet so we can | |
2934 | * muck around with it freely without cmpxchg | |
2935 | */ | |
2936 | freelist = page->freelist; | |
2937 | page->freelist = NULL; | |
2938 | ||
2939 | stat(s, ALLOC_SLAB); | |
53a0de06 | 2940 | |
2a904905 | 2941 | check_new_page: |
2cfb7455 | 2942 | |
1572df7c | 2943 | if (kmem_cache_debug(s)) { |
fa417ab7 | 2944 | if (!alloc_debug_processing(s, page, freelist, addr)) { |
1572df7c VB |
2945 | /* Slab failed checks. Next slab needed */ |
2946 | goto new_slab; | |
fa417ab7 | 2947 | } else { |
1572df7c VB |
2948 | /* |
2949 | * For debug case, we don't load freelist so that all | |
2950 | * allocations go through alloc_debug_processing() | |
2951 | */ | |
2952 | goto return_single; | |
fa417ab7 | 2953 | } |
1572df7c VB |
2954 | } |
2955 | ||
2956 | if (unlikely(!pfmemalloc_match(page, gfpflags))) | |
2957 | /* | |
2958 | * For !pfmemalloc_match() case we don't load freelist so that | |
2959 | * we don't make further mismatched allocations easier. | |
2960 | */ | |
2961 | goto return_single; | |
2962 | ||
cfdf836e VB |
2963 | retry_load_page: |
2964 | ||
9f101ee8 | 2965 | local_irq_save(flags); |
cfdf836e VB |
2966 | if (unlikely(c->page)) { |
2967 | void *flush_freelist = c->freelist; | |
2968 | struct page *flush_page = c->page; | |
2969 | ||
2970 | c->page = NULL; | |
2971 | c->freelist = NULL; | |
2972 | c->tid = next_tid(c->tid); | |
2973 | ||
2974 | local_irq_restore(flags); | |
2975 | ||
2976 | deactivate_slab(s, flush_page, flush_freelist); | |
2977 | ||
2978 | stat(s, CPUSLAB_FLUSH); | |
2979 | ||
2980 | goto retry_load_page; | |
2981 | } | |
3f2b77e3 VB |
2982 | c->page = page; |
2983 | ||
1572df7c VB |
2984 | goto load_freelist; |
2985 | ||
2986 | return_single: | |
894b8788 | 2987 | |
a019d201 | 2988 | deactivate_slab(s, page, get_freepointer(s, freelist)); |
6faa6833 | 2989 | return freelist; |
894b8788 CL |
2990 | } |
2991 | ||
a380a3c7 | 2992 | /* |
e500059b VB |
2993 | * A wrapper for ___slab_alloc() for contexts where preemption is not yet |
2994 | * disabled. Compensates for possible cpu changes by refetching the per cpu area | |
2995 | * pointer. | |
a380a3c7 CL |
2996 | */ |
2997 | static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node, | |
2998 | unsigned long addr, struct kmem_cache_cpu *c) | |
2999 | { | |
3000 | void *p; | |
a380a3c7 | 3001 | |
e500059b | 3002 | #ifdef CONFIG_PREEMPT_COUNT |
a380a3c7 CL |
3003 | /* |
3004 | * We may have been preempted and rescheduled on a different | |
e500059b | 3005 | * cpu before disabling preemption. Need to reload cpu area |
a380a3c7 CL |
3006 | * pointer. |
3007 | */ | |
e500059b | 3008 | c = get_cpu_ptr(s->cpu_slab); |
a380a3c7 CL |
3009 | #endif |
3010 | ||
3011 | p = ___slab_alloc(s, gfpflags, node, addr, c); | |
e500059b VB |
3012 | #ifdef CONFIG_PREEMPT_COUNT |
3013 | put_cpu_ptr(s->cpu_slab); | |
3014 | #endif | |
a380a3c7 CL |
3015 | return p; |
3016 | } | |
3017 | ||
0f181f9f AP |
3018 | /* |
3019 | * If the object has been wiped upon free, make sure it's fully initialized by | |
3020 | * zeroing out freelist pointer. | |
3021 | */ | |
3022 | static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s, | |
3023 | void *obj) | |
3024 | { | |
3025 | if (unlikely(slab_want_init_on_free(s)) && obj) | |
ce5716c6 AK |
3026 | memset((void *)((char *)kasan_reset_tag(obj) + s->offset), |
3027 | 0, sizeof(void *)); | |
0f181f9f AP |
3028 | } |
3029 | ||
894b8788 CL |
3030 | /* |
3031 | * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc) | |
3032 | * have the fastpath folded into their functions. So no function call | |
3033 | * overhead for requests that can be satisfied on the fastpath. | |
3034 | * | |
3035 | * The fastpath works by first checking if the lockless freelist can be used. | |
3036 | * If not then __slab_alloc is called for slow processing. | |
3037 | * | |
3038 | * Otherwise we can simply pick the next object from the lockless free list. | |
3039 | */ | |
2b847c3c | 3040 | static __always_inline void *slab_alloc_node(struct kmem_cache *s, |
b89fb5ef | 3041 | gfp_t gfpflags, int node, unsigned long addr, size_t orig_size) |
894b8788 | 3042 | { |
03ec0ed5 | 3043 | void *object; |
dfb4f096 | 3044 | struct kmem_cache_cpu *c; |
57d437d2 | 3045 | struct page *page; |
8a5ec0ba | 3046 | unsigned long tid; |
964d4bd3 | 3047 | struct obj_cgroup *objcg = NULL; |
da844b78 | 3048 | bool init = false; |
1f84260c | 3049 | |
964d4bd3 | 3050 | s = slab_pre_alloc_hook(s, &objcg, 1, gfpflags); |
8135be5a | 3051 | if (!s) |
773ff60e | 3052 | return NULL; |
b89fb5ef AP |
3053 | |
3054 | object = kfence_alloc(s, orig_size, gfpflags); | |
3055 | if (unlikely(object)) | |
3056 | goto out; | |
3057 | ||
8a5ec0ba | 3058 | redo: |
8a5ec0ba CL |
3059 | /* |
3060 | * Must read kmem_cache cpu data via this cpu ptr. Preemption is | |
3061 | * enabled. We may switch back and forth between cpus while | |
3062 | * reading from one cpu area. That does not matter as long | |
3063 | * as we end up on the original cpu again when doing the cmpxchg. | |
7cccd80b | 3064 | * |
9b4bc85a VB |
3065 | * We must guarantee that tid and kmem_cache_cpu are retrieved on the |
3066 | * same cpu. We read first the kmem_cache_cpu pointer and use it to read | |
3067 | * the tid. If we are preempted and switched to another cpu between the | |
3068 | * two reads, it's OK as the two are still associated with the same cpu | |
3069 | * and cmpxchg later will validate the cpu. | |
8a5ec0ba | 3070 | */ |
9b4bc85a VB |
3071 | c = raw_cpu_ptr(s->cpu_slab); |
3072 | tid = READ_ONCE(c->tid); | |
9aabf810 JK |
3073 | |
3074 | /* | |
3075 | * Irqless object alloc/free algorithm used here depends on sequence | |
3076 | * of fetching cpu_slab's data. tid should be fetched before anything | |
3077 | * on c to guarantee that object and page associated with previous tid | |
3078 | * won't be used with current tid. If we fetch tid first, object and | |
3079 | * page could be one associated with next tid and our alloc/free | |
3080 | * request will be failed. In this case, we will retry. So, no problem. | |
3081 | */ | |
3082 | barrier(); | |
8a5ec0ba | 3083 | |
8a5ec0ba CL |
3084 | /* |
3085 | * The transaction ids are globally unique per cpu and per operation on | |
3086 | * a per cpu queue. Thus they can be guarantee that the cmpxchg_double | |
3087 | * occurs on the right processor and that there was no operation on the | |
3088 | * linked list in between. | |
3089 | */ | |
8a5ec0ba | 3090 | |
9dfc6e68 | 3091 | object = c->freelist; |
57d437d2 | 3092 | page = c->page; |
22e4663e | 3093 | if (unlikely(!object || !page || !node_match(page, node))) { |
dfb4f096 | 3094 | object = __slab_alloc(s, gfpflags, node, addr, c); |
8eae1492 | 3095 | } else { |
0ad9500e ED |
3096 | void *next_object = get_freepointer_safe(s, object); |
3097 | ||
8a5ec0ba | 3098 | /* |
25985edc | 3099 | * The cmpxchg will only match if there was no additional |
8a5ec0ba CL |
3100 | * operation and if we are on the right processor. |
3101 | * | |
d0e0ac97 CG |
3102 | * The cmpxchg does the following atomically (without lock |
3103 | * semantics!) | |
8a5ec0ba CL |
3104 | * 1. Relocate first pointer to the current per cpu area. |
3105 | * 2. Verify that tid and freelist have not been changed | |
3106 | * 3. If they were not changed replace tid and freelist | |
3107 | * | |
d0e0ac97 CG |
3108 | * Since this is without lock semantics the protection is only |
3109 | * against code executing on this cpu *not* from access by | |
3110 | * other cpus. | |
8a5ec0ba | 3111 | */ |
933393f5 | 3112 | if (unlikely(!this_cpu_cmpxchg_double( |
8a5ec0ba CL |
3113 | s->cpu_slab->freelist, s->cpu_slab->tid, |
3114 | object, tid, | |
0ad9500e | 3115 | next_object, next_tid(tid)))) { |
8a5ec0ba CL |
3116 | |
3117 | note_cmpxchg_failure("slab_alloc", s, tid); | |
3118 | goto redo; | |
3119 | } | |
0ad9500e | 3120 | prefetch_freepointer(s, next_object); |
84e554e6 | 3121 | stat(s, ALLOC_FASTPATH); |
894b8788 | 3122 | } |
0f181f9f | 3123 | |
ce5716c6 | 3124 | maybe_wipe_obj_freeptr(s, object); |
da844b78 | 3125 | init = slab_want_init_on_alloc(gfpflags, s); |
d07dbea4 | 3126 | |
b89fb5ef | 3127 | out: |
da844b78 | 3128 | slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init); |
5a896d9e | 3129 | |
894b8788 | 3130 | return object; |
81819f0f CL |
3131 | } |
3132 | ||
2b847c3c | 3133 | static __always_inline void *slab_alloc(struct kmem_cache *s, |
b89fb5ef | 3134 | gfp_t gfpflags, unsigned long addr, size_t orig_size) |
2b847c3c | 3135 | { |
b89fb5ef | 3136 | return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr, orig_size); |
2b847c3c EG |
3137 | } |
3138 | ||
81819f0f CL |
3139 | void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags) |
3140 | { | |
b89fb5ef | 3141 | void *ret = slab_alloc(s, gfpflags, _RET_IP_, s->object_size); |
5b882be4 | 3142 | |
d0e0ac97 CG |
3143 | trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size, |
3144 | s->size, gfpflags); | |
5b882be4 EGM |
3145 | |
3146 | return ret; | |
81819f0f CL |
3147 | } |
3148 | EXPORT_SYMBOL(kmem_cache_alloc); | |
3149 | ||
0f24f128 | 3150 | #ifdef CONFIG_TRACING |
4a92379b RK |
3151 | void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) |
3152 | { | |
b89fb5ef | 3153 | void *ret = slab_alloc(s, gfpflags, _RET_IP_, size); |
4a92379b | 3154 | trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags); |
0116523c | 3155 | ret = kasan_kmalloc(s, ret, size, gfpflags); |
4a92379b RK |
3156 | return ret; |
3157 | } | |
3158 | EXPORT_SYMBOL(kmem_cache_alloc_trace); | |
5b882be4 EGM |
3159 | #endif |
3160 | ||
81819f0f CL |
3161 | #ifdef CONFIG_NUMA |
3162 | void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) | |
3163 | { | |
b89fb5ef | 3164 | void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_, s->object_size); |
5b882be4 | 3165 | |
ca2b84cb | 3166 | trace_kmem_cache_alloc_node(_RET_IP_, ret, |
3b0efdfa | 3167 | s->object_size, s->size, gfpflags, node); |
5b882be4 EGM |
3168 | |
3169 | return ret; | |
81819f0f CL |
3170 | } |
3171 | EXPORT_SYMBOL(kmem_cache_alloc_node); | |
81819f0f | 3172 | |
0f24f128 | 3173 | #ifdef CONFIG_TRACING |
4a92379b | 3174 | void *kmem_cache_alloc_node_trace(struct kmem_cache *s, |
5b882be4 | 3175 | gfp_t gfpflags, |
4a92379b | 3176 | int node, size_t size) |
5b882be4 | 3177 | { |
b89fb5ef | 3178 | void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_, size); |
4a92379b RK |
3179 | |
3180 | trace_kmalloc_node(_RET_IP_, ret, | |
3181 | size, s->size, gfpflags, node); | |
0316bec2 | 3182 | |
0116523c | 3183 | ret = kasan_kmalloc(s, ret, size, gfpflags); |
4a92379b | 3184 | return ret; |
5b882be4 | 3185 | } |
4a92379b | 3186 | EXPORT_SYMBOL(kmem_cache_alloc_node_trace); |
5b882be4 | 3187 | #endif |
6dfd1b65 | 3188 | #endif /* CONFIG_NUMA */ |
5b882be4 | 3189 | |
81819f0f | 3190 | /* |
94e4d712 | 3191 | * Slow path handling. This may still be called frequently since objects |
894b8788 | 3192 | * have a longer lifetime than the cpu slabs in most processing loads. |
81819f0f | 3193 | * |
894b8788 CL |
3194 | * So we still attempt to reduce cache line usage. Just take the slab |
3195 | * lock and free the item. If there is no additional partial page | |
3196 | * handling required then we can return immediately. | |
81819f0f | 3197 | */ |
894b8788 | 3198 | static void __slab_free(struct kmem_cache *s, struct page *page, |
81084651 JDB |
3199 | void *head, void *tail, int cnt, |
3200 | unsigned long addr) | |
3201 | ||
81819f0f CL |
3202 | { |
3203 | void *prior; | |
2cfb7455 | 3204 | int was_frozen; |
2cfb7455 CL |
3205 | struct page new; |
3206 | unsigned long counters; | |
3207 | struct kmem_cache_node *n = NULL; | |
3f649ab7 | 3208 | unsigned long flags; |
81819f0f | 3209 | |
8a5ec0ba | 3210 | stat(s, FREE_SLOWPATH); |
81819f0f | 3211 | |
b89fb5ef AP |
3212 | if (kfence_free(head)) |
3213 | return; | |
3214 | ||
19c7ff9e | 3215 | if (kmem_cache_debug(s) && |
282acb43 | 3216 | !free_debug_processing(s, page, head, tail, cnt, addr)) |
80f08c19 | 3217 | return; |
6446faa2 | 3218 | |
2cfb7455 | 3219 | do { |
837d678d JK |
3220 | if (unlikely(n)) { |
3221 | spin_unlock_irqrestore(&n->list_lock, flags); | |
3222 | n = NULL; | |
3223 | } | |
2cfb7455 CL |
3224 | prior = page->freelist; |
3225 | counters = page->counters; | |
81084651 | 3226 | set_freepointer(s, tail, prior); |
2cfb7455 CL |
3227 | new.counters = counters; |
3228 | was_frozen = new.frozen; | |
81084651 | 3229 | new.inuse -= cnt; |
837d678d | 3230 | if ((!new.inuse || !prior) && !was_frozen) { |
49e22585 | 3231 | |
c65c1877 | 3232 | if (kmem_cache_has_cpu_partial(s) && !prior) { |
49e22585 CL |
3233 | |
3234 | /* | |
d0e0ac97 CG |
3235 | * Slab was on no list before and will be |
3236 | * partially empty | |
3237 | * We can defer the list move and instead | |
3238 | * freeze it. | |
49e22585 CL |
3239 | */ |
3240 | new.frozen = 1; | |
3241 | ||
c65c1877 | 3242 | } else { /* Needs to be taken off a list */ |
49e22585 | 3243 | |
b455def2 | 3244 | n = get_node(s, page_to_nid(page)); |
49e22585 CL |
3245 | /* |
3246 | * Speculatively acquire the list_lock. | |
3247 | * If the cmpxchg does not succeed then we may | |
3248 | * drop the list_lock without any processing. | |
3249 | * | |
3250 | * Otherwise the list_lock will synchronize with | |
3251 | * other processors updating the list of slabs. | |
3252 | */ | |
3253 | spin_lock_irqsave(&n->list_lock, flags); | |
3254 | ||
3255 | } | |
2cfb7455 | 3256 | } |
81819f0f | 3257 | |
2cfb7455 CL |
3258 | } while (!cmpxchg_double_slab(s, page, |
3259 | prior, counters, | |
81084651 | 3260 | head, new.counters, |
2cfb7455 | 3261 | "__slab_free")); |
81819f0f | 3262 | |
2cfb7455 | 3263 | if (likely(!n)) { |
49e22585 | 3264 | |
c270cf30 AW |
3265 | if (likely(was_frozen)) { |
3266 | /* | |
3267 | * The list lock was not taken therefore no list | |
3268 | * activity can be necessary. | |
3269 | */ | |
3270 | stat(s, FREE_FROZEN); | |
3271 | } else if (new.frozen) { | |
3272 | /* | |
3273 | * If we just froze the page then put it onto the | |
3274 | * per cpu partial list. | |
3275 | */ | |
49e22585 | 3276 | put_cpu_partial(s, page, 1); |
8028dcea AS |
3277 | stat(s, CPU_PARTIAL_FREE); |
3278 | } | |
c270cf30 | 3279 | |
b455def2 L |
3280 | return; |
3281 | } | |
81819f0f | 3282 | |
8a5b20ae | 3283 | if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) |
837d678d JK |
3284 | goto slab_empty; |
3285 | ||
81819f0f | 3286 | /* |
837d678d JK |
3287 | * Objects left in the slab. If it was not on the partial list before |
3288 | * then add it. | |
81819f0f | 3289 | */ |
345c905d | 3290 | if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) { |
a4d3f891 | 3291 | remove_full(s, n, page); |
837d678d JK |
3292 | add_partial(n, page, DEACTIVATE_TO_TAIL); |
3293 | stat(s, FREE_ADD_PARTIAL); | |
8ff12cfc | 3294 | } |
80f08c19 | 3295 | spin_unlock_irqrestore(&n->list_lock, flags); |
81819f0f CL |
3296 | return; |
3297 | ||
3298 | slab_empty: | |
a973e9dd | 3299 | if (prior) { |
81819f0f | 3300 | /* |
6fbabb20 | 3301 | * Slab on the partial list. |
81819f0f | 3302 | */ |
5cc6eee8 | 3303 | remove_partial(n, page); |
84e554e6 | 3304 | stat(s, FREE_REMOVE_PARTIAL); |
c65c1877 | 3305 | } else { |
6fbabb20 | 3306 | /* Slab must be on the full list */ |
c65c1877 PZ |
3307 | remove_full(s, n, page); |
3308 | } | |
2cfb7455 | 3309 | |
80f08c19 | 3310 | spin_unlock_irqrestore(&n->list_lock, flags); |
84e554e6 | 3311 | stat(s, FREE_SLAB); |
81819f0f | 3312 | discard_slab(s, page); |
81819f0f CL |
3313 | } |
3314 | ||
894b8788 CL |
3315 | /* |
3316 | * Fastpath with forced inlining to produce a kfree and kmem_cache_free that | |
3317 | * can perform fastpath freeing without additional function calls. | |
3318 | * | |
3319 | * The fastpath is only possible if we are freeing to the current cpu slab | |
3320 | * of this processor. This typically the case if we have just allocated | |
3321 | * the item before. | |
3322 | * | |
3323 | * If fastpath is not possible then fall back to __slab_free where we deal | |
3324 | * with all sorts of special processing. | |
81084651 JDB |
3325 | * |
3326 | * Bulk free of a freelist with several objects (all pointing to the | |
3327 | * same page) possible by specifying head and tail ptr, plus objects | |
3328 | * count (cnt). Bulk free indicated by tail pointer being set. | |
894b8788 | 3329 | */ |
80a9201a AP |
3330 | static __always_inline void do_slab_free(struct kmem_cache *s, |
3331 | struct page *page, void *head, void *tail, | |
3332 | int cnt, unsigned long addr) | |
894b8788 | 3333 | { |
81084651 | 3334 | void *tail_obj = tail ? : head; |
dfb4f096 | 3335 | struct kmem_cache_cpu *c; |
8a5ec0ba | 3336 | unsigned long tid; |
964d4bd3 | 3337 | |
d1b2cf6c | 3338 | memcg_slab_free_hook(s, &head, 1); |
8a5ec0ba CL |
3339 | redo: |
3340 | /* | |
3341 | * Determine the currently cpus per cpu slab. | |
3342 | * The cpu may change afterward. However that does not matter since | |
3343 | * data is retrieved via this pointer. If we are on the same cpu | |
2ae44005 | 3344 | * during the cmpxchg then the free will succeed. |
8a5ec0ba | 3345 | */ |
9b4bc85a VB |
3346 | c = raw_cpu_ptr(s->cpu_slab); |
3347 | tid = READ_ONCE(c->tid); | |
c016b0bd | 3348 | |
9aabf810 JK |
3349 | /* Same with comment on barrier() in slab_alloc_node() */ |
3350 | barrier(); | |
c016b0bd | 3351 | |
442b06bc | 3352 | if (likely(page == c->page)) { |
5076190d LT |
3353 | void **freelist = READ_ONCE(c->freelist); |
3354 | ||
3355 | set_freepointer(s, tail_obj, freelist); | |
8a5ec0ba | 3356 | |
933393f5 | 3357 | if (unlikely(!this_cpu_cmpxchg_double( |
8a5ec0ba | 3358 | s->cpu_slab->freelist, s->cpu_slab->tid, |
5076190d | 3359 | freelist, tid, |
81084651 | 3360 | head, next_tid(tid)))) { |
8a5ec0ba CL |
3361 | |
3362 | note_cmpxchg_failure("slab_free", s, tid); | |
3363 | goto redo; | |
3364 | } | |
84e554e6 | 3365 | stat(s, FREE_FASTPATH); |
894b8788 | 3366 | } else |
81084651 | 3367 | __slab_free(s, page, head, tail_obj, cnt, addr); |
894b8788 | 3368 | |
894b8788 CL |
3369 | } |
3370 | ||
80a9201a AP |
3371 | static __always_inline void slab_free(struct kmem_cache *s, struct page *page, |
3372 | void *head, void *tail, int cnt, | |
3373 | unsigned long addr) | |
3374 | { | |
80a9201a | 3375 | /* |
c3895391 AK |
3376 | * With KASAN enabled slab_free_freelist_hook modifies the freelist |
3377 | * to remove objects, whose reuse must be delayed. | |
80a9201a | 3378 | */ |
c3895391 AK |
3379 | if (slab_free_freelist_hook(s, &head, &tail)) |
3380 | do_slab_free(s, page, head, tail, cnt, addr); | |
80a9201a AP |
3381 | } |
3382 | ||
2bd926b4 | 3383 | #ifdef CONFIG_KASAN_GENERIC |
80a9201a AP |
3384 | void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) |
3385 | { | |
3386 | do_slab_free(cache, virt_to_head_page(x), x, NULL, 1, addr); | |
3387 | } | |
3388 | #endif | |
3389 | ||
81819f0f CL |
3390 | void kmem_cache_free(struct kmem_cache *s, void *x) |
3391 | { | |
b9ce5ef4 GC |
3392 | s = cache_from_obj(s, x); |
3393 | if (!s) | |
79576102 | 3394 | return; |
81084651 | 3395 | slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_); |
3544de8e | 3396 | trace_kmem_cache_free(_RET_IP_, x, s->name); |
81819f0f CL |
3397 | } |
3398 | EXPORT_SYMBOL(kmem_cache_free); | |
3399 | ||
d0ecd894 | 3400 | struct detached_freelist { |
fbd02630 | 3401 | struct page *page; |
d0ecd894 JDB |
3402 | void *tail; |
3403 | void *freelist; | |
3404 | int cnt; | |
376bf125 | 3405 | struct kmem_cache *s; |
d0ecd894 | 3406 | }; |
fbd02630 | 3407 | |
1ed7ce57 | 3408 | static inline void free_nonslab_page(struct page *page, void *object) |
f227f0fa SB |
3409 | { |
3410 | unsigned int order = compound_order(page); | |
3411 | ||
3412 | VM_BUG_ON_PAGE(!PageCompound(page), page); | |
1ed7ce57 | 3413 | kfree_hook(object); |
f227f0fa SB |
3414 | mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, -(PAGE_SIZE << order)); |
3415 | __free_pages(page, order); | |
3416 | } | |
3417 | ||
d0ecd894 JDB |
3418 | /* |
3419 | * This function progressively scans the array with free objects (with | |
3420 | * a limited look ahead) and extract objects belonging to the same | |
3421 | * page. It builds a detached freelist directly within the given | |
3422 | * page/objects. This can happen without any need for | |
3423 | * synchronization, because the objects are owned by running process. | |
3424 | * The freelist is build up as a single linked list in the objects. | |
3425 | * The idea is, that this detached freelist can then be bulk | |
3426 | * transferred to the real freelist(s), but only requiring a single | |
3427 | * synchronization primitive. Look ahead in the array is limited due | |
3428 | * to performance reasons. | |
3429 | */ | |
376bf125 JDB |
3430 | static inline |
3431 | int build_detached_freelist(struct kmem_cache *s, size_t size, | |
3432 | void **p, struct detached_freelist *df) | |
d0ecd894 JDB |
3433 | { |
3434 | size_t first_skipped_index = 0; | |
3435 | int lookahead = 3; | |
3436 | void *object; | |
ca257195 | 3437 | struct page *page; |
fbd02630 | 3438 | |
d0ecd894 JDB |
3439 | /* Always re-init detached_freelist */ |
3440 | df->page = NULL; | |
fbd02630 | 3441 | |
d0ecd894 JDB |
3442 | do { |
3443 | object = p[--size]; | |
ca257195 | 3444 | /* Do we need !ZERO_OR_NULL_PTR(object) here? (for kfree) */ |
d0ecd894 | 3445 | } while (!object && size); |
3eed034d | 3446 | |
d0ecd894 JDB |
3447 | if (!object) |
3448 | return 0; | |
fbd02630 | 3449 | |
ca257195 JDB |
3450 | page = virt_to_head_page(object); |
3451 | if (!s) { | |
3452 | /* Handle kalloc'ed objects */ | |
3453 | if (unlikely(!PageSlab(page))) { | |
1ed7ce57 | 3454 | free_nonslab_page(page, object); |
ca257195 JDB |
3455 | p[size] = NULL; /* mark object processed */ |
3456 | return size; | |
3457 | } | |
3458 | /* Derive kmem_cache from object */ | |
3459 | df->s = page->slab_cache; | |
3460 | } else { | |
3461 | df->s = cache_from_obj(s, object); /* Support for memcg */ | |
3462 | } | |
376bf125 | 3463 | |
b89fb5ef | 3464 | if (is_kfence_address(object)) { |
d57a964e | 3465 | slab_free_hook(df->s, object, false); |
b89fb5ef AP |
3466 | __kfence_free(object); |
3467 | p[size] = NULL; /* mark object processed */ | |
3468 | return size; | |
3469 | } | |
3470 | ||
d0ecd894 | 3471 | /* Start new detached freelist */ |
ca257195 | 3472 | df->page = page; |
376bf125 | 3473 | set_freepointer(df->s, object, NULL); |
d0ecd894 JDB |
3474 | df->tail = object; |
3475 | df->freelist = object; | |
3476 | p[size] = NULL; /* mark object processed */ | |
3477 | df->cnt = 1; | |
3478 | ||
3479 | while (size) { | |
3480 | object = p[--size]; | |
3481 | if (!object) | |
3482 | continue; /* Skip processed objects */ | |
3483 | ||
3484 | /* df->page is always set at this point */ | |
3485 | if (df->page == virt_to_head_page(object)) { | |
3486 | /* Opportunity build freelist */ | |
376bf125 | 3487 | set_freepointer(df->s, object, df->freelist); |
d0ecd894 JDB |
3488 | df->freelist = object; |
3489 | df->cnt++; | |
3490 | p[size] = NULL; /* mark object processed */ | |
3491 | ||
3492 | continue; | |
fbd02630 | 3493 | } |
d0ecd894 JDB |
3494 | |
3495 | /* Limit look ahead search */ | |
3496 | if (!--lookahead) | |
3497 | break; | |
3498 | ||
3499 | if (!first_skipped_index) | |
3500 | first_skipped_index = size + 1; | |
fbd02630 | 3501 | } |
d0ecd894 JDB |
3502 | |
3503 | return first_skipped_index; | |
3504 | } | |
3505 | ||
d0ecd894 | 3506 | /* Note that interrupts must be enabled when calling this function. */ |
376bf125 | 3507 | void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p) |
d0ecd894 JDB |
3508 | { |
3509 | if (WARN_ON(!size)) | |
3510 | return; | |
3511 | ||
d1b2cf6c | 3512 | memcg_slab_free_hook(s, p, size); |
d0ecd894 JDB |
3513 | do { |
3514 | struct detached_freelist df; | |
3515 | ||
3516 | size = build_detached_freelist(s, size, p, &df); | |
84582c8a | 3517 | if (!df.page) |
d0ecd894 JDB |
3518 | continue; |
3519 | ||
457c82c3 | 3520 | slab_free(df.s, df.page, df.freelist, df.tail, df.cnt, _RET_IP_); |
d0ecd894 | 3521 | } while (likely(size)); |
484748f0 CL |
3522 | } |
3523 | EXPORT_SYMBOL(kmem_cache_free_bulk); | |
3524 | ||
994eb764 | 3525 | /* Note that interrupts must be enabled when calling this function. */ |
865762a8 JDB |
3526 | int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, |
3527 | void **p) | |
484748f0 | 3528 | { |
994eb764 JDB |
3529 | struct kmem_cache_cpu *c; |
3530 | int i; | |
964d4bd3 | 3531 | struct obj_cgroup *objcg = NULL; |
994eb764 | 3532 | |
03ec0ed5 | 3533 | /* memcg and kmem_cache debug support */ |
964d4bd3 | 3534 | s = slab_pre_alloc_hook(s, &objcg, size, flags); |
03ec0ed5 JDB |
3535 | if (unlikely(!s)) |
3536 | return false; | |
994eb764 JDB |
3537 | /* |
3538 | * Drain objects in the per cpu slab, while disabling local | |
3539 | * IRQs, which protects against PREEMPT and interrupts | |
3540 | * handlers invoking normal fastpath. | |
3541 | */ | |
e500059b | 3542 | c = get_cpu_ptr(s->cpu_slab); |
994eb764 | 3543 | local_irq_disable(); |
994eb764 JDB |
3544 | |
3545 | for (i = 0; i < size; i++) { | |
b89fb5ef | 3546 | void *object = kfence_alloc(s, s->object_size, flags); |
994eb764 | 3547 | |
b89fb5ef AP |
3548 | if (unlikely(object)) { |
3549 | p[i] = object; | |
3550 | continue; | |
3551 | } | |
3552 | ||
3553 | object = c->freelist; | |
ebe909e0 | 3554 | if (unlikely(!object)) { |
fd4d9c7d JH |
3555 | /* |
3556 | * We may have removed an object from c->freelist using | |
3557 | * the fastpath in the previous iteration; in that case, | |
3558 | * c->tid has not been bumped yet. | |
3559 | * Since ___slab_alloc() may reenable interrupts while | |
3560 | * allocating memory, we should bump c->tid now. | |
3561 | */ | |
3562 | c->tid = next_tid(c->tid); | |
3563 | ||
e500059b VB |
3564 | local_irq_enable(); |
3565 | ||
ebe909e0 JDB |
3566 | /* |
3567 | * Invoking slow path likely have side-effect | |
3568 | * of re-populating per CPU c->freelist | |
3569 | */ | |
87098373 | 3570 | p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE, |
ebe909e0 | 3571 | _RET_IP_, c); |
87098373 CL |
3572 | if (unlikely(!p[i])) |
3573 | goto error; | |
3574 | ||
ebe909e0 | 3575 | c = this_cpu_ptr(s->cpu_slab); |
0f181f9f AP |
3576 | maybe_wipe_obj_freeptr(s, p[i]); |
3577 | ||
e500059b VB |
3578 | local_irq_disable(); |
3579 | ||
ebe909e0 JDB |
3580 | continue; /* goto for-loop */ |
3581 | } | |
994eb764 JDB |
3582 | c->freelist = get_freepointer(s, object); |
3583 | p[i] = object; | |
0f181f9f | 3584 | maybe_wipe_obj_freeptr(s, p[i]); |
994eb764 JDB |
3585 | } |
3586 | c->tid = next_tid(c->tid); | |
3587 | local_irq_enable(); | |
e500059b | 3588 | put_cpu_ptr(s->cpu_slab); |
994eb764 | 3589 | |
da844b78 AK |
3590 | /* |
3591 | * memcg and kmem_cache debug support and memory initialization. | |
3592 | * Done outside of the IRQ disabled fastpath loop. | |
3593 | */ | |
3594 | slab_post_alloc_hook(s, objcg, flags, size, p, | |
3595 | slab_want_init_on_alloc(flags, s)); | |
865762a8 | 3596 | return i; |
87098373 | 3597 | error: |
e500059b | 3598 | put_cpu_ptr(s->cpu_slab); |
da844b78 | 3599 | slab_post_alloc_hook(s, objcg, flags, i, p, false); |
03ec0ed5 | 3600 | __kmem_cache_free_bulk(s, i, p); |
865762a8 | 3601 | return 0; |
484748f0 CL |
3602 | } |
3603 | EXPORT_SYMBOL(kmem_cache_alloc_bulk); | |
3604 | ||
3605 | ||
81819f0f | 3606 | /* |
672bba3a CL |
3607 | * Object placement in a slab is made very easy because we always start at |
3608 | * offset 0. If we tune the size of the object to the alignment then we can | |
3609 | * get the required alignment by putting one properly sized object after | |
3610 | * another. | |
81819f0f CL |
3611 | * |
3612 | * Notice that the allocation order determines the sizes of the per cpu | |
3613 | * caches. Each processor has always one slab available for allocations. | |
3614 | * Increasing the allocation order reduces the number of times that slabs | |
672bba3a | 3615 | * must be moved on and off the partial lists and is therefore a factor in |
81819f0f | 3616 | * locking overhead. |
81819f0f CL |
3617 | */ |
3618 | ||
3619 | /* | |
f0953a1b | 3620 | * Minimum / Maximum order of slab pages. This influences locking overhead |
81819f0f CL |
3621 | * and slab fragmentation. A higher order reduces the number of partial slabs |
3622 | * and increases the number of allocations possible without having to | |
3623 | * take the list_lock. | |
3624 | */ | |
19af27af AD |
3625 | static unsigned int slub_min_order; |
3626 | static unsigned int slub_max_order = PAGE_ALLOC_COSTLY_ORDER; | |
3627 | static unsigned int slub_min_objects; | |
81819f0f | 3628 | |
81819f0f CL |
3629 | /* |
3630 | * Calculate the order of allocation given an slab object size. | |
3631 | * | |
672bba3a CL |
3632 | * The order of allocation has significant impact on performance and other |
3633 | * system components. Generally order 0 allocations should be preferred since | |
3634 | * order 0 does not cause fragmentation in the page allocator. Larger objects | |
3635 | * be problematic to put into order 0 slabs because there may be too much | |
c124f5b5 | 3636 | * unused space left. We go to a higher order if more than 1/16th of the slab |
672bba3a CL |
3637 | * would be wasted. |
3638 | * | |
3639 | * In order to reach satisfactory performance we must ensure that a minimum | |
3640 | * number of objects is in one slab. Otherwise we may generate too much | |
3641 | * activity on the partial lists which requires taking the list_lock. This is | |
3642 | * less a concern for large slabs though which are rarely used. | |
81819f0f | 3643 | * |
672bba3a CL |
3644 | * slub_max_order specifies the order where we begin to stop considering the |
3645 | * number of objects in a slab as critical. If we reach slub_max_order then | |
3646 | * we try to keep the page order as low as possible. So we accept more waste | |
3647 | * of space in favor of a small page order. | |
81819f0f | 3648 | * |
672bba3a CL |
3649 | * Higher order allocations also allow the placement of more objects in a |
3650 | * slab and thereby reduce object handling overhead. If the user has | |
dc84207d | 3651 | * requested a higher minimum order then we start with that one instead of |
672bba3a | 3652 | * the smallest order which will fit the object. |
81819f0f | 3653 | */ |
19af27af AD |
3654 | static inline unsigned int slab_order(unsigned int size, |
3655 | unsigned int min_objects, unsigned int max_order, | |
9736d2a9 | 3656 | unsigned int fract_leftover) |
81819f0f | 3657 | { |
19af27af AD |
3658 | unsigned int min_order = slub_min_order; |
3659 | unsigned int order; | |
81819f0f | 3660 | |
9736d2a9 | 3661 | if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE) |
210b5c06 | 3662 | return get_order(size * MAX_OBJS_PER_PAGE) - 1; |
39b26464 | 3663 | |
9736d2a9 | 3664 | for (order = max(min_order, (unsigned int)get_order(min_objects * size)); |
5e6d444e | 3665 | order <= max_order; order++) { |
81819f0f | 3666 | |
19af27af AD |
3667 | unsigned int slab_size = (unsigned int)PAGE_SIZE << order; |
3668 | unsigned int rem; | |
81819f0f | 3669 | |
9736d2a9 | 3670 | rem = slab_size % size; |
81819f0f | 3671 | |
5e6d444e | 3672 | if (rem <= slab_size / fract_leftover) |
81819f0f | 3673 | break; |
81819f0f | 3674 | } |
672bba3a | 3675 | |
81819f0f CL |
3676 | return order; |
3677 | } | |
3678 | ||
9736d2a9 | 3679 | static inline int calculate_order(unsigned int size) |
5e6d444e | 3680 | { |
19af27af AD |
3681 | unsigned int order; |
3682 | unsigned int min_objects; | |
3683 | unsigned int max_objects; | |
3286222f | 3684 | unsigned int nr_cpus; |
5e6d444e CL |
3685 | |
3686 | /* | |
3687 | * Attempt to find best configuration for a slab. This | |
3688 | * works by first attempting to generate a layout with | |
3689 | * the best configuration and backing off gradually. | |
3690 | * | |
422ff4d7 | 3691 | * First we increase the acceptable waste in a slab. Then |
5e6d444e CL |
3692 | * we reduce the minimum objects required in a slab. |
3693 | */ | |
3694 | min_objects = slub_min_objects; | |
3286222f VB |
3695 | if (!min_objects) { |
3696 | /* | |
3697 | * Some architectures will only update present cpus when | |
3698 | * onlining them, so don't trust the number if it's just 1. But | |
3699 | * we also don't want to use nr_cpu_ids always, as on some other | |
3700 | * architectures, there can be many possible cpus, but never | |
3701 | * onlined. Here we compromise between trying to avoid too high | |
3702 | * order on systems that appear larger than they are, and too | |
3703 | * low order on systems that appear smaller than they are. | |
3704 | */ | |
3705 | nr_cpus = num_present_cpus(); | |
3706 | if (nr_cpus <= 1) | |
3707 | nr_cpus = nr_cpu_ids; | |
3708 | min_objects = 4 * (fls(nr_cpus) + 1); | |
3709 | } | |
9736d2a9 | 3710 | max_objects = order_objects(slub_max_order, size); |
e8120ff1 ZY |
3711 | min_objects = min(min_objects, max_objects); |
3712 | ||
5e6d444e | 3713 | while (min_objects > 1) { |
19af27af AD |
3714 | unsigned int fraction; |
3715 | ||
c124f5b5 | 3716 | fraction = 16; |
5e6d444e CL |
3717 | while (fraction >= 4) { |
3718 | order = slab_order(size, min_objects, | |
9736d2a9 | 3719 | slub_max_order, fraction); |
5e6d444e CL |
3720 | if (order <= slub_max_order) |
3721 | return order; | |
3722 | fraction /= 2; | |
3723 | } | |
5086c389 | 3724 | min_objects--; |
5e6d444e CL |
3725 | } |
3726 | ||
3727 | /* | |
3728 | * We were unable to place multiple objects in a slab. Now | |
3729 | * lets see if we can place a single object there. | |
3730 | */ | |
9736d2a9 | 3731 | order = slab_order(size, 1, slub_max_order, 1); |
5e6d444e CL |
3732 | if (order <= slub_max_order) |
3733 | return order; | |
3734 | ||
3735 | /* | |
3736 | * Doh this slab cannot be placed using slub_max_order. | |
3737 | */ | |
9736d2a9 | 3738 | order = slab_order(size, 1, MAX_ORDER, 1); |
818cf590 | 3739 | if (order < MAX_ORDER) |
5e6d444e CL |
3740 | return order; |
3741 | return -ENOSYS; | |
3742 | } | |
3743 | ||
5595cffc | 3744 | static void |
4053497d | 3745 | init_kmem_cache_node(struct kmem_cache_node *n) |
81819f0f CL |
3746 | { |
3747 | n->nr_partial = 0; | |
81819f0f CL |
3748 | spin_lock_init(&n->list_lock); |
3749 | INIT_LIST_HEAD(&n->partial); | |
8ab1372f | 3750 | #ifdef CONFIG_SLUB_DEBUG |
0f389ec6 | 3751 | atomic_long_set(&n->nr_slabs, 0); |
02b71b70 | 3752 | atomic_long_set(&n->total_objects, 0); |
643b1138 | 3753 | INIT_LIST_HEAD(&n->full); |
8ab1372f | 3754 | #endif |
81819f0f CL |
3755 | } |
3756 | ||
55136592 | 3757 | static inline int alloc_kmem_cache_cpus(struct kmem_cache *s) |
4c93c355 | 3758 | { |
6c182dc0 | 3759 | BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE < |
95a05b42 | 3760 | KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu)); |
4c93c355 | 3761 | |
8a5ec0ba | 3762 | /* |
d4d84fef CM |
3763 | * Must align to double word boundary for the double cmpxchg |
3764 | * instructions to work; see __pcpu_double_call_return_bool(). | |
8a5ec0ba | 3765 | */ |
d4d84fef CM |
3766 | s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu), |
3767 | 2 * sizeof(void *)); | |
8a5ec0ba CL |
3768 | |
3769 | if (!s->cpu_slab) | |
3770 | return 0; | |
3771 | ||
3772 | init_kmem_cache_cpus(s); | |
4c93c355 | 3773 | |
8a5ec0ba | 3774 | return 1; |
4c93c355 | 3775 | } |
4c93c355 | 3776 | |
51df1142 CL |
3777 | static struct kmem_cache *kmem_cache_node; |
3778 | ||
81819f0f CL |
3779 | /* |
3780 | * No kmalloc_node yet so do it by hand. We know that this is the first | |
3781 | * slab on the node for this slabcache. There are no concurrent accesses | |
3782 | * possible. | |
3783 | * | |
721ae22a ZYW |
3784 | * Note that this function only works on the kmem_cache_node |
3785 | * when allocating for the kmem_cache_node. This is used for bootstrapping | |
4c93c355 | 3786 | * memory on a fresh node that has no slab structures yet. |
81819f0f | 3787 | */ |
55136592 | 3788 | static void early_kmem_cache_node_alloc(int node) |
81819f0f CL |
3789 | { |
3790 | struct page *page; | |
3791 | struct kmem_cache_node *n; | |
3792 | ||
51df1142 | 3793 | BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node)); |
81819f0f | 3794 | |
51df1142 | 3795 | page = new_slab(kmem_cache_node, GFP_NOWAIT, node); |
81819f0f CL |
3796 | |
3797 | BUG_ON(!page); | |
a2f92ee7 | 3798 | if (page_to_nid(page) != node) { |
f9f58285 FF |
3799 | pr_err("SLUB: Unable to allocate memory from node %d\n", node); |
3800 | pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n"); | |
a2f92ee7 CL |
3801 | } |
3802 | ||
81819f0f CL |
3803 | n = page->freelist; |
3804 | BUG_ON(!n); | |
8ab1372f | 3805 | #ifdef CONFIG_SLUB_DEBUG |
f7cb1933 | 3806 | init_object(kmem_cache_node, n, SLUB_RED_ACTIVE); |
51df1142 | 3807 | init_tracking(kmem_cache_node, n); |
8ab1372f | 3808 | #endif |
da844b78 | 3809 | n = kasan_slab_alloc(kmem_cache_node, n, GFP_KERNEL, false); |
12b22386 AK |
3810 | page->freelist = get_freepointer(kmem_cache_node, n); |
3811 | page->inuse = 1; | |
3812 | page->frozen = 0; | |
3813 | kmem_cache_node->node[node] = n; | |
4053497d | 3814 | init_kmem_cache_node(n); |
51df1142 | 3815 | inc_slabs_node(kmem_cache_node, node, page->objects); |
6446faa2 | 3816 | |
67b6c900 | 3817 | /* |
1e4dd946 SR |
3818 | * No locks need to be taken here as it has just been |
3819 | * initialized and there is no concurrent access. | |
67b6c900 | 3820 | */ |
1e4dd946 | 3821 | __add_partial(n, page, DEACTIVATE_TO_HEAD); |
81819f0f CL |
3822 | } |
3823 | ||
3824 | static void free_kmem_cache_nodes(struct kmem_cache *s) | |
3825 | { | |
3826 | int node; | |
fa45dc25 | 3827 | struct kmem_cache_node *n; |
81819f0f | 3828 | |
fa45dc25 | 3829 | for_each_kmem_cache_node(s, node, n) { |
81819f0f | 3830 | s->node[node] = NULL; |
ea37df54 | 3831 | kmem_cache_free(kmem_cache_node, n); |
81819f0f CL |
3832 | } |
3833 | } | |
3834 | ||
52b4b950 DS |
3835 | void __kmem_cache_release(struct kmem_cache *s) |
3836 | { | |
210e7a43 | 3837 | cache_random_seq_destroy(s); |
52b4b950 DS |
3838 | free_percpu(s->cpu_slab); |
3839 | free_kmem_cache_nodes(s); | |
3840 | } | |
3841 | ||
55136592 | 3842 | static int init_kmem_cache_nodes(struct kmem_cache *s) |
81819f0f CL |
3843 | { |
3844 | int node; | |
81819f0f | 3845 | |
7e1fa93d | 3846 | for_each_node_mask(node, slab_nodes) { |
81819f0f CL |
3847 | struct kmem_cache_node *n; |
3848 | ||
73367bd8 | 3849 | if (slab_state == DOWN) { |
55136592 | 3850 | early_kmem_cache_node_alloc(node); |
73367bd8 AD |
3851 | continue; |
3852 | } | |
51df1142 | 3853 | n = kmem_cache_alloc_node(kmem_cache_node, |
55136592 | 3854 | GFP_KERNEL, node); |
81819f0f | 3855 | |
73367bd8 AD |
3856 | if (!n) { |
3857 | free_kmem_cache_nodes(s); | |
3858 | return 0; | |
81819f0f | 3859 | } |
73367bd8 | 3860 | |
4053497d | 3861 | init_kmem_cache_node(n); |
ea37df54 | 3862 | s->node[node] = n; |
81819f0f CL |
3863 | } |
3864 | return 1; | |
3865 | } | |
81819f0f | 3866 | |
c0bdb232 | 3867 | static void set_min_partial(struct kmem_cache *s, unsigned long min) |
3b89d7d8 DR |
3868 | { |
3869 | if (min < MIN_PARTIAL) | |
3870 | min = MIN_PARTIAL; | |
3871 | else if (min > MAX_PARTIAL) | |
3872 | min = MAX_PARTIAL; | |
3873 | s->min_partial = min; | |
3874 | } | |
3875 | ||
e6d0e1dc WY |
3876 | static void set_cpu_partial(struct kmem_cache *s) |
3877 | { | |
3878 | #ifdef CONFIG_SLUB_CPU_PARTIAL | |
3879 | /* | |
3880 | * cpu_partial determined the maximum number of objects kept in the | |
3881 | * per cpu partial lists of a processor. | |
3882 | * | |
3883 | * Per cpu partial lists mainly contain slabs that just have one | |
3884 | * object freed. If they are used for allocation then they can be | |
3885 | * filled up again with minimal effort. The slab will never hit the | |
3886 | * per node partial lists and therefore no locking will be required. | |
3887 | * | |
3888 | * This setting also determines | |
3889 | * | |
3890 | * A) The number of objects from per cpu partial slabs dumped to the | |
3891 | * per node list when we reach the limit. | |
3892 | * B) The number of objects in cpu partial slabs to extract from the | |
3893 | * per node list when we run out of per cpu objects. We only fetch | |
3894 | * 50% to keep some capacity around for frees. | |
3895 | */ | |
3896 | if (!kmem_cache_has_cpu_partial(s)) | |
bbd4e305 | 3897 | slub_set_cpu_partial(s, 0); |
e6d0e1dc | 3898 | else if (s->size >= PAGE_SIZE) |
bbd4e305 | 3899 | slub_set_cpu_partial(s, 2); |
e6d0e1dc | 3900 | else if (s->size >= 1024) |
bbd4e305 | 3901 | slub_set_cpu_partial(s, 6); |
e6d0e1dc | 3902 | else if (s->size >= 256) |
bbd4e305 | 3903 | slub_set_cpu_partial(s, 13); |
e6d0e1dc | 3904 | else |
bbd4e305 | 3905 | slub_set_cpu_partial(s, 30); |
e6d0e1dc WY |
3906 | #endif |
3907 | } | |
3908 | ||
81819f0f CL |
3909 | /* |
3910 | * calculate_sizes() determines the order and the distribution of data within | |
3911 | * a slab object. | |
3912 | */ | |
06b285dc | 3913 | static int calculate_sizes(struct kmem_cache *s, int forced_order) |
81819f0f | 3914 | { |
d50112ed | 3915 | slab_flags_t flags = s->flags; |
be4a7988 | 3916 | unsigned int size = s->object_size; |
19af27af | 3917 | unsigned int order; |
81819f0f | 3918 | |
d8b42bf5 CL |
3919 | /* |
3920 | * Round up object size to the next word boundary. We can only | |
3921 | * place the free pointer at word boundaries and this determines | |
3922 | * the possible location of the free pointer. | |
3923 | */ | |
3924 | size = ALIGN(size, sizeof(void *)); | |
3925 | ||
3926 | #ifdef CONFIG_SLUB_DEBUG | |
81819f0f CL |
3927 | /* |
3928 | * Determine if we can poison the object itself. If the user of | |
3929 | * the slab may touch the object after free or before allocation | |
3930 | * then we should never poison the object itself. | |
3931 | */ | |
5f0d5a3a | 3932 | if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) && |
c59def9f | 3933 | !s->ctor) |
81819f0f CL |
3934 | s->flags |= __OBJECT_POISON; |
3935 | else | |
3936 | s->flags &= ~__OBJECT_POISON; | |
3937 | ||
81819f0f CL |
3938 | |
3939 | /* | |
672bba3a | 3940 | * If we are Redzoning then check if there is some space between the |
81819f0f | 3941 | * end of the object and the free pointer. If not then add an |
672bba3a | 3942 | * additional word to have some bytes to store Redzone information. |
81819f0f | 3943 | */ |
3b0efdfa | 3944 | if ((flags & SLAB_RED_ZONE) && size == s->object_size) |
81819f0f | 3945 | size += sizeof(void *); |
41ecc55b | 3946 | #endif |
81819f0f CL |
3947 | |
3948 | /* | |
672bba3a | 3949 | * With that we have determined the number of bytes in actual use |
e41a49fa | 3950 | * by the object and redzoning. |
81819f0f CL |
3951 | */ |
3952 | s->inuse = size; | |
3953 | ||
74c1d3e0 KC |
3954 | if ((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) || |
3955 | ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) || | |
3956 | s->ctor) { | |
81819f0f CL |
3957 | /* |
3958 | * Relocate free pointer after the object if it is not | |
3959 | * permitted to overwrite the first word of the object on | |
3960 | * kmem_cache_free. | |
3961 | * | |
3962 | * This is the case if we do RCU, have a constructor or | |
74c1d3e0 KC |
3963 | * destructor, are poisoning the objects, or are |
3964 | * redzoning an object smaller than sizeof(void *). | |
cbfc35a4 WL |
3965 | * |
3966 | * The assumption that s->offset >= s->inuse means free | |
3967 | * pointer is outside of the object is used in the | |
3968 | * freeptr_outside_object() function. If that is no | |
3969 | * longer true, the function needs to be modified. | |
81819f0f CL |
3970 | */ |
3971 | s->offset = size; | |
3972 | size += sizeof(void *); | |
e41a49fa | 3973 | } else { |
3202fa62 KC |
3974 | /* |
3975 | * Store freelist pointer near middle of object to keep | |
3976 | * it away from the edges of the object to avoid small | |
3977 | * sized over/underflows from neighboring allocations. | |
3978 | */ | |
e41a49fa | 3979 | s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *)); |
81819f0f CL |
3980 | } |
3981 | ||
c12b3c62 | 3982 | #ifdef CONFIG_SLUB_DEBUG |
81819f0f CL |
3983 | if (flags & SLAB_STORE_USER) |
3984 | /* | |
3985 | * Need to store information about allocs and frees after | |
3986 | * the object. | |
3987 | */ | |
3988 | size += 2 * sizeof(struct track); | |
80a9201a | 3989 | #endif |
81819f0f | 3990 | |
80a9201a AP |
3991 | kasan_cache_create(s, &size, &s->flags); |
3992 | #ifdef CONFIG_SLUB_DEBUG | |
d86bd1be | 3993 | if (flags & SLAB_RED_ZONE) { |
81819f0f CL |
3994 | /* |
3995 | * Add some empty padding so that we can catch | |
3996 | * overwrites from earlier objects rather than let | |
3997 | * tracking information or the free pointer be | |
0211a9c8 | 3998 | * corrupted if a user writes before the start |
81819f0f CL |
3999 | * of the object. |
4000 | */ | |
4001 | size += sizeof(void *); | |
d86bd1be JK |
4002 | |
4003 | s->red_left_pad = sizeof(void *); | |
4004 | s->red_left_pad = ALIGN(s->red_left_pad, s->align); | |
4005 | size += s->red_left_pad; | |
4006 | } | |
41ecc55b | 4007 | #endif |
672bba3a | 4008 | |
81819f0f CL |
4009 | /* |
4010 | * SLUB stores one object immediately after another beginning from | |
4011 | * offset 0. In order to align the objects we have to simply size | |
4012 | * each object to conform to the alignment. | |
4013 | */ | |
45906855 | 4014 | size = ALIGN(size, s->align); |
81819f0f | 4015 | s->size = size; |
4138fdfc | 4016 | s->reciprocal_size = reciprocal_value(size); |
06b285dc CL |
4017 | if (forced_order >= 0) |
4018 | order = forced_order; | |
4019 | else | |
9736d2a9 | 4020 | order = calculate_order(size); |
81819f0f | 4021 | |
19af27af | 4022 | if ((int)order < 0) |
81819f0f CL |
4023 | return 0; |
4024 | ||
b7a49f0d | 4025 | s->allocflags = 0; |
834f3d11 | 4026 | if (order) |
b7a49f0d CL |
4027 | s->allocflags |= __GFP_COMP; |
4028 | ||
4029 | if (s->flags & SLAB_CACHE_DMA) | |
2c59dd65 | 4030 | s->allocflags |= GFP_DMA; |
b7a49f0d | 4031 | |
6d6ea1e9 NB |
4032 | if (s->flags & SLAB_CACHE_DMA32) |
4033 | s->allocflags |= GFP_DMA32; | |
4034 | ||
b7a49f0d CL |
4035 | if (s->flags & SLAB_RECLAIM_ACCOUNT) |
4036 | s->allocflags |= __GFP_RECLAIMABLE; | |
4037 | ||
81819f0f CL |
4038 | /* |
4039 | * Determine the number of objects per slab | |
4040 | */ | |
9736d2a9 MW |
4041 | s->oo = oo_make(order, size); |
4042 | s->min = oo_make(get_order(size), size); | |
205ab99d CL |
4043 | if (oo_objects(s->oo) > oo_objects(s->max)) |
4044 | s->max = s->oo; | |
81819f0f | 4045 | |
834f3d11 | 4046 | return !!oo_objects(s->oo); |
81819f0f CL |
4047 | } |
4048 | ||
d50112ed | 4049 | static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags) |
81819f0f | 4050 | { |
37540008 | 4051 | s->flags = kmem_cache_flags(s->size, flags, s->name); |
2482ddec KC |
4052 | #ifdef CONFIG_SLAB_FREELIST_HARDENED |
4053 | s->random = get_random_long(); | |
4054 | #endif | |
81819f0f | 4055 | |
06b285dc | 4056 | if (!calculate_sizes(s, -1)) |
81819f0f | 4057 | goto error; |
3de47213 DR |
4058 | if (disable_higher_order_debug) { |
4059 | /* | |
4060 | * Disable debugging flags that store metadata if the min slab | |
4061 | * order increased. | |
4062 | */ | |
3b0efdfa | 4063 | if (get_order(s->size) > get_order(s->object_size)) { |
3de47213 DR |
4064 | s->flags &= ~DEBUG_METADATA_FLAGS; |
4065 | s->offset = 0; | |
4066 | if (!calculate_sizes(s, -1)) | |
4067 | goto error; | |
4068 | } | |
4069 | } | |
81819f0f | 4070 | |
2565409f HC |
4071 | #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \ |
4072 | defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE) | |
149daaf3 | 4073 | if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0) |
b789ef51 CL |
4074 | /* Enable fast mode */ |
4075 | s->flags |= __CMPXCHG_DOUBLE; | |
4076 | #endif | |
4077 | ||
3b89d7d8 DR |
4078 | /* |
4079 | * The larger the object size is, the more pages we want on the partial | |
4080 | * list to avoid pounding the page allocator excessively. | |
4081 | */ | |
49e22585 CL |
4082 | set_min_partial(s, ilog2(s->size) / 2); |
4083 | ||
e6d0e1dc | 4084 | set_cpu_partial(s); |
49e22585 | 4085 | |
81819f0f | 4086 | #ifdef CONFIG_NUMA |
e2cb96b7 | 4087 | s->remote_node_defrag_ratio = 1000; |
81819f0f | 4088 | #endif |
210e7a43 TG |
4089 | |
4090 | /* Initialize the pre-computed randomized freelist if slab is up */ | |
4091 | if (slab_state >= UP) { | |
4092 | if (init_cache_random_seq(s)) | |
4093 | goto error; | |
4094 | } | |
4095 | ||
55136592 | 4096 | if (!init_kmem_cache_nodes(s)) |
dfb4f096 | 4097 | goto error; |
81819f0f | 4098 | |
55136592 | 4099 | if (alloc_kmem_cache_cpus(s)) |
278b1bb1 | 4100 | return 0; |
ff12059e | 4101 | |
4c93c355 | 4102 | free_kmem_cache_nodes(s); |
81819f0f | 4103 | error: |
278b1bb1 | 4104 | return -EINVAL; |
81819f0f | 4105 | } |
81819f0f | 4106 | |
33b12c38 | 4107 | static void list_slab_objects(struct kmem_cache *s, struct page *page, |
55860d96 | 4108 | const char *text) |
33b12c38 CL |
4109 | { |
4110 | #ifdef CONFIG_SLUB_DEBUG | |
4111 | void *addr = page_address(page); | |
a2b4ae8b | 4112 | unsigned long flags; |
55860d96 | 4113 | unsigned long *map; |
33b12c38 | 4114 | void *p; |
aa456c7a | 4115 | |
945cf2b6 | 4116 | slab_err(s, page, text, s->name); |
a2b4ae8b | 4117 | slab_lock(page, &flags); |
33b12c38 | 4118 | |
90e9f6a6 | 4119 | map = get_map(s, page); |
33b12c38 CL |
4120 | for_each_object(p, s, addr, page->objects) { |
4121 | ||
4138fdfc | 4122 | if (!test_bit(__obj_to_index(s, addr, p), map)) { |
96b94abc | 4123 | pr_err("Object 0x%p @offset=%tu\n", p, p - addr); |
33b12c38 CL |
4124 | print_tracking(s, p); |
4125 | } | |
4126 | } | |
55860d96 | 4127 | put_map(map); |
a2b4ae8b | 4128 | slab_unlock(page, &flags); |
33b12c38 CL |
4129 | #endif |
4130 | } | |
4131 | ||
81819f0f | 4132 | /* |
599870b1 | 4133 | * Attempt to free all partial slabs on a node. |
52b4b950 DS |
4134 | * This is called from __kmem_cache_shutdown(). We must take list_lock |
4135 | * because sysfs file might still access partial list after the shutdowning. | |
81819f0f | 4136 | */ |
599870b1 | 4137 | static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) |
81819f0f | 4138 | { |
60398923 | 4139 | LIST_HEAD(discard); |
81819f0f CL |
4140 | struct page *page, *h; |
4141 | ||
52b4b950 DS |
4142 | BUG_ON(irqs_disabled()); |
4143 | spin_lock_irq(&n->list_lock); | |
916ac052 | 4144 | list_for_each_entry_safe(page, h, &n->partial, slab_list) { |
81819f0f | 4145 | if (!page->inuse) { |
52b4b950 | 4146 | remove_partial(n, page); |
916ac052 | 4147 | list_add(&page->slab_list, &discard); |
33b12c38 CL |
4148 | } else { |
4149 | list_slab_objects(s, page, | |
55860d96 | 4150 | "Objects remaining in %s on __kmem_cache_shutdown()"); |
599870b1 | 4151 | } |
33b12c38 | 4152 | } |
52b4b950 | 4153 | spin_unlock_irq(&n->list_lock); |
60398923 | 4154 | |
916ac052 | 4155 | list_for_each_entry_safe(page, h, &discard, slab_list) |
60398923 | 4156 | discard_slab(s, page); |
81819f0f CL |
4157 | } |
4158 | ||
f9e13c0a SB |
4159 | bool __kmem_cache_empty(struct kmem_cache *s) |
4160 | { | |
4161 | int node; | |
4162 | struct kmem_cache_node *n; | |
4163 | ||
4164 | for_each_kmem_cache_node(s, node, n) | |
4165 | if (n->nr_partial || slabs_node(s, node)) | |
4166 | return false; | |
4167 | return true; | |
4168 | } | |
4169 | ||
81819f0f | 4170 | /* |
672bba3a | 4171 | * Release all resources used by a slab cache. |
81819f0f | 4172 | */ |
52b4b950 | 4173 | int __kmem_cache_shutdown(struct kmem_cache *s) |
81819f0f CL |
4174 | { |
4175 | int node; | |
fa45dc25 | 4176 | struct kmem_cache_node *n; |
81819f0f | 4177 | |
5a836bf6 | 4178 | flush_all_cpus_locked(s); |
81819f0f | 4179 | /* Attempt to free all objects */ |
fa45dc25 | 4180 | for_each_kmem_cache_node(s, node, n) { |
599870b1 CL |
4181 | free_partial(s, n); |
4182 | if (n->nr_partial || slabs_node(s, node)) | |
81819f0f CL |
4183 | return 1; |
4184 | } | |
81819f0f CL |
4185 | return 0; |
4186 | } | |
4187 | ||
5bb1bb35 | 4188 | #ifdef CONFIG_PRINTK |
8e7f37f2 PM |
4189 | void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct page *page) |
4190 | { | |
4191 | void *base; | |
4192 | int __maybe_unused i; | |
4193 | unsigned int objnr; | |
4194 | void *objp; | |
4195 | void *objp0; | |
4196 | struct kmem_cache *s = page->slab_cache; | |
4197 | struct track __maybe_unused *trackp; | |
4198 | ||
4199 | kpp->kp_ptr = object; | |
4200 | kpp->kp_page = page; | |
4201 | kpp->kp_slab_cache = s; | |
4202 | base = page_address(page); | |
4203 | objp0 = kasan_reset_tag(object); | |
4204 | #ifdef CONFIG_SLUB_DEBUG | |
4205 | objp = restore_red_left(s, objp0); | |
4206 | #else | |
4207 | objp = objp0; | |
4208 | #endif | |
4209 | objnr = obj_to_index(s, page, objp); | |
4210 | kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp); | |
4211 | objp = base + s->size * objnr; | |
4212 | kpp->kp_objp = objp; | |
4213 | if (WARN_ON_ONCE(objp < base || objp >= base + page->objects * s->size || (objp - base) % s->size) || | |
4214 | !(s->flags & SLAB_STORE_USER)) | |
4215 | return; | |
4216 | #ifdef CONFIG_SLUB_DEBUG | |
0cbc124b | 4217 | objp = fixup_red_left(s, objp); |
8e7f37f2 PM |
4218 | trackp = get_track(s, objp, TRACK_ALLOC); |
4219 | kpp->kp_ret = (void *)trackp->addr; | |
ae14c63a LT |
4220 | #ifdef CONFIG_STACKTRACE |
4221 | for (i = 0; i < KS_ADDRS_COUNT && i < TRACK_ADDRS_COUNT; i++) { | |
4222 | kpp->kp_stack[i] = (void *)trackp->addrs[i]; | |
4223 | if (!kpp->kp_stack[i]) | |
4224 | break; | |
4225 | } | |
78869146 | 4226 | |
ae14c63a LT |
4227 | trackp = get_track(s, objp, TRACK_FREE); |
4228 | for (i = 0; i < KS_ADDRS_COUNT && i < TRACK_ADDRS_COUNT; i++) { | |
4229 | kpp->kp_free_stack[i] = (void *)trackp->addrs[i]; | |
4230 | if (!kpp->kp_free_stack[i]) | |
4231 | break; | |
e548eaa1 | 4232 | } |
8e7f37f2 PM |
4233 | #endif |
4234 | #endif | |
4235 | } | |
5bb1bb35 | 4236 | #endif |
8e7f37f2 | 4237 | |
81819f0f CL |
4238 | /******************************************************************** |
4239 | * Kmalloc subsystem | |
4240 | *******************************************************************/ | |
4241 | ||
81819f0f CL |
4242 | static int __init setup_slub_min_order(char *str) |
4243 | { | |
19af27af | 4244 | get_option(&str, (int *)&slub_min_order); |
81819f0f CL |
4245 | |
4246 | return 1; | |
4247 | } | |
4248 | ||
4249 | __setup("slub_min_order=", setup_slub_min_order); | |
4250 | ||
4251 | static int __init setup_slub_max_order(char *str) | |
4252 | { | |
19af27af AD |
4253 | get_option(&str, (int *)&slub_max_order); |
4254 | slub_max_order = min(slub_max_order, (unsigned int)MAX_ORDER - 1); | |
81819f0f CL |
4255 | |
4256 | return 1; | |
4257 | } | |
4258 | ||
4259 | __setup("slub_max_order=", setup_slub_max_order); | |
4260 | ||
4261 | static int __init setup_slub_min_objects(char *str) | |
4262 | { | |
19af27af | 4263 | get_option(&str, (int *)&slub_min_objects); |
81819f0f CL |
4264 | |
4265 | return 1; | |
4266 | } | |
4267 | ||
4268 | __setup("slub_min_objects=", setup_slub_min_objects); | |
4269 | ||
81819f0f CL |
4270 | void *__kmalloc(size_t size, gfp_t flags) |
4271 | { | |
aadb4bc4 | 4272 | struct kmem_cache *s; |
5b882be4 | 4273 | void *ret; |
81819f0f | 4274 | |
95a05b42 | 4275 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) |
eada35ef | 4276 | return kmalloc_large(size, flags); |
aadb4bc4 | 4277 | |
2c59dd65 | 4278 | s = kmalloc_slab(size, flags); |
aadb4bc4 CL |
4279 | |
4280 | if (unlikely(ZERO_OR_NULL_PTR(s))) | |
6cb8f913 CL |
4281 | return s; |
4282 | ||
b89fb5ef | 4283 | ret = slab_alloc(s, flags, _RET_IP_, size); |
5b882be4 | 4284 | |
ca2b84cb | 4285 | trace_kmalloc(_RET_IP_, ret, size, s->size, flags); |
5b882be4 | 4286 | |
0116523c | 4287 | ret = kasan_kmalloc(s, ret, size, flags); |
0316bec2 | 4288 | |
5b882be4 | 4289 | return ret; |
81819f0f CL |
4290 | } |
4291 | EXPORT_SYMBOL(__kmalloc); | |
4292 | ||
5d1f57e4 | 4293 | #ifdef CONFIG_NUMA |
f619cfe1 CL |
4294 | static void *kmalloc_large_node(size_t size, gfp_t flags, int node) |
4295 | { | |
b1eeab67 | 4296 | struct page *page; |
e4f7c0b4 | 4297 | void *ptr = NULL; |
6a486c0a | 4298 | unsigned int order = get_order(size); |
f619cfe1 | 4299 | |
75f296d9 | 4300 | flags |= __GFP_COMP; |
6a486c0a VB |
4301 | page = alloc_pages_node(node, flags, order); |
4302 | if (page) { | |
e4f7c0b4 | 4303 | ptr = page_address(page); |
96403bfe MS |
4304 | mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, |
4305 | PAGE_SIZE << order); | |
6a486c0a | 4306 | } |
e4f7c0b4 | 4307 | |
0116523c | 4308 | return kmalloc_large_node_hook(ptr, size, flags); |
f619cfe1 CL |
4309 | } |
4310 | ||
81819f0f CL |
4311 | void *__kmalloc_node(size_t size, gfp_t flags, int node) |
4312 | { | |
aadb4bc4 | 4313 | struct kmem_cache *s; |
5b882be4 | 4314 | void *ret; |
81819f0f | 4315 | |
95a05b42 | 4316 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { |
5b882be4 EGM |
4317 | ret = kmalloc_large_node(size, flags, node); |
4318 | ||
ca2b84cb EGM |
4319 | trace_kmalloc_node(_RET_IP_, ret, |
4320 | size, PAGE_SIZE << get_order(size), | |
4321 | flags, node); | |
5b882be4 EGM |
4322 | |
4323 | return ret; | |
4324 | } | |
aadb4bc4 | 4325 | |
2c59dd65 | 4326 | s = kmalloc_slab(size, flags); |
aadb4bc4 CL |
4327 | |
4328 | if (unlikely(ZERO_OR_NULL_PTR(s))) | |
6cb8f913 CL |
4329 | return s; |
4330 | ||
b89fb5ef | 4331 | ret = slab_alloc_node(s, flags, node, _RET_IP_, size); |
5b882be4 | 4332 | |
ca2b84cb | 4333 | trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node); |
5b882be4 | 4334 | |
0116523c | 4335 | ret = kasan_kmalloc(s, ret, size, flags); |
0316bec2 | 4336 | |
5b882be4 | 4337 | return ret; |
81819f0f CL |
4338 | } |
4339 | EXPORT_SYMBOL(__kmalloc_node); | |
6dfd1b65 | 4340 | #endif /* CONFIG_NUMA */ |
81819f0f | 4341 | |
ed18adc1 KC |
4342 | #ifdef CONFIG_HARDENED_USERCOPY |
4343 | /* | |
afcc90f8 KC |
4344 | * Rejects incorrectly sized objects and objects that are to be copied |
4345 | * to/from userspace but do not fall entirely within the containing slab | |
4346 | * cache's usercopy region. | |
ed18adc1 KC |
4347 | * |
4348 | * Returns NULL if check passes, otherwise const char * to name of cache | |
4349 | * to indicate an error. | |
4350 | */ | |
f4e6e289 KC |
4351 | void __check_heap_object(const void *ptr, unsigned long n, struct page *page, |
4352 | bool to_user) | |
ed18adc1 KC |
4353 | { |
4354 | struct kmem_cache *s; | |
44065b2e | 4355 | unsigned int offset; |
ed18adc1 | 4356 | size_t object_size; |
b89fb5ef | 4357 | bool is_kfence = is_kfence_address(ptr); |
ed18adc1 | 4358 | |
96fedce2 AK |
4359 | ptr = kasan_reset_tag(ptr); |
4360 | ||
ed18adc1 KC |
4361 | /* Find object and usable object size. */ |
4362 | s = page->slab_cache; | |
ed18adc1 KC |
4363 | |
4364 | /* Reject impossible pointers. */ | |
4365 | if (ptr < page_address(page)) | |
f4e6e289 KC |
4366 | usercopy_abort("SLUB object not in SLUB page?!", NULL, |
4367 | to_user, 0, n); | |
ed18adc1 KC |
4368 | |
4369 | /* Find offset within object. */ | |
b89fb5ef AP |
4370 | if (is_kfence) |
4371 | offset = ptr - kfence_object_start(ptr); | |
4372 | else | |
4373 | offset = (ptr - page_address(page)) % s->size; | |
ed18adc1 KC |
4374 | |
4375 | /* Adjust for redzone and reject if within the redzone. */ | |
b89fb5ef | 4376 | if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) { |
ed18adc1 | 4377 | if (offset < s->red_left_pad) |
f4e6e289 KC |
4378 | usercopy_abort("SLUB object in left red zone", |
4379 | s->name, to_user, offset, n); | |
ed18adc1 KC |
4380 | offset -= s->red_left_pad; |
4381 | } | |
4382 | ||
afcc90f8 KC |
4383 | /* Allow address range falling entirely within usercopy region. */ |
4384 | if (offset >= s->useroffset && | |
4385 | offset - s->useroffset <= s->usersize && | |
4386 | n <= s->useroffset - offset + s->usersize) | |
f4e6e289 | 4387 | return; |
ed18adc1 | 4388 | |
afcc90f8 KC |
4389 | /* |
4390 | * If the copy is still within the allocated object, produce | |
4391 | * a warning instead of rejecting the copy. This is intended | |
4392 | * to be a temporary method to find any missing usercopy | |
4393 | * whitelists. | |
4394 | */ | |
4395 | object_size = slab_ksize(s); | |
2d891fbc KC |
4396 | if (usercopy_fallback && |
4397 | offset <= object_size && n <= object_size - offset) { | |
afcc90f8 KC |
4398 | usercopy_warn("SLUB object", s->name, to_user, offset, n); |
4399 | return; | |
4400 | } | |
ed18adc1 | 4401 | |
f4e6e289 | 4402 | usercopy_abort("SLUB object", s->name, to_user, offset, n); |
ed18adc1 KC |
4403 | } |
4404 | #endif /* CONFIG_HARDENED_USERCOPY */ | |
4405 | ||
10d1f8cb | 4406 | size_t __ksize(const void *object) |
81819f0f | 4407 | { |
272c1d21 | 4408 | struct page *page; |
81819f0f | 4409 | |
ef8b4520 | 4410 | if (unlikely(object == ZERO_SIZE_PTR)) |
272c1d21 CL |
4411 | return 0; |
4412 | ||
294a80a8 | 4413 | page = virt_to_head_page(object); |
294a80a8 | 4414 | |
76994412 PE |
4415 | if (unlikely(!PageSlab(page))) { |
4416 | WARN_ON(!PageCompound(page)); | |
a50b854e | 4417 | return page_size(page); |
76994412 | 4418 | } |
81819f0f | 4419 | |
1b4f59e3 | 4420 | return slab_ksize(page->slab_cache); |
81819f0f | 4421 | } |
10d1f8cb | 4422 | EXPORT_SYMBOL(__ksize); |
81819f0f CL |
4423 | |
4424 | void kfree(const void *x) | |
4425 | { | |
81819f0f | 4426 | struct page *page; |
5bb983b0 | 4427 | void *object = (void *)x; |
81819f0f | 4428 | |
2121db74 PE |
4429 | trace_kfree(_RET_IP_, x); |
4430 | ||
2408c550 | 4431 | if (unlikely(ZERO_OR_NULL_PTR(x))) |
81819f0f CL |
4432 | return; |
4433 | ||
b49af68f | 4434 | page = virt_to_head_page(x); |
aadb4bc4 | 4435 | if (unlikely(!PageSlab(page))) { |
1ed7ce57 | 4436 | free_nonslab_page(page, object); |
aadb4bc4 CL |
4437 | return; |
4438 | } | |
81084651 | 4439 | slab_free(page->slab_cache, page, object, NULL, 1, _RET_IP_); |
81819f0f CL |
4440 | } |
4441 | EXPORT_SYMBOL(kfree); | |
4442 | ||
832f37f5 VD |
4443 | #define SHRINK_PROMOTE_MAX 32 |
4444 | ||
2086d26a | 4445 | /* |
832f37f5 VD |
4446 | * kmem_cache_shrink discards empty slabs and promotes the slabs filled |
4447 | * up most to the head of the partial lists. New allocations will then | |
4448 | * fill those up and thus they can be removed from the partial lists. | |
672bba3a CL |
4449 | * |
4450 | * The slabs with the least items are placed last. This results in them | |
4451 | * being allocated from last increasing the chance that the last objects | |
4452 | * are freed in them. | |
2086d26a | 4453 | */ |
5a836bf6 | 4454 | static int __kmem_cache_do_shrink(struct kmem_cache *s) |
2086d26a CL |
4455 | { |
4456 | int node; | |
4457 | int i; | |
4458 | struct kmem_cache_node *n; | |
4459 | struct page *page; | |
4460 | struct page *t; | |
832f37f5 VD |
4461 | struct list_head discard; |
4462 | struct list_head promote[SHRINK_PROMOTE_MAX]; | |
2086d26a | 4463 | unsigned long flags; |
ce3712d7 | 4464 | int ret = 0; |
2086d26a | 4465 | |
fa45dc25 | 4466 | for_each_kmem_cache_node(s, node, n) { |
832f37f5 VD |
4467 | INIT_LIST_HEAD(&discard); |
4468 | for (i = 0; i < SHRINK_PROMOTE_MAX; i++) | |
4469 | INIT_LIST_HEAD(promote + i); | |
2086d26a CL |
4470 | |
4471 | spin_lock_irqsave(&n->list_lock, flags); | |
4472 | ||
4473 | /* | |
832f37f5 | 4474 | * Build lists of slabs to discard or promote. |
2086d26a | 4475 | * |
672bba3a CL |
4476 | * Note that concurrent frees may occur while we hold the |
4477 | * list_lock. page->inuse here is the upper limit. | |
2086d26a | 4478 | */ |
916ac052 | 4479 | list_for_each_entry_safe(page, t, &n->partial, slab_list) { |
832f37f5 VD |
4480 | int free = page->objects - page->inuse; |
4481 | ||
4482 | /* Do not reread page->inuse */ | |
4483 | barrier(); | |
4484 | ||
4485 | /* We do not keep full slabs on the list */ | |
4486 | BUG_ON(free <= 0); | |
4487 | ||
4488 | if (free == page->objects) { | |
916ac052 | 4489 | list_move(&page->slab_list, &discard); |
69cb8e6b | 4490 | n->nr_partial--; |
832f37f5 | 4491 | } else if (free <= SHRINK_PROMOTE_MAX) |
916ac052 | 4492 | list_move(&page->slab_list, promote + free - 1); |
2086d26a CL |
4493 | } |
4494 | ||
2086d26a | 4495 | /* |
832f37f5 VD |
4496 | * Promote the slabs filled up most to the head of the |
4497 | * partial list. | |
2086d26a | 4498 | */ |
832f37f5 VD |
4499 | for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--) |
4500 | list_splice(promote + i, &n->partial); | |
2086d26a | 4501 | |
2086d26a | 4502 | spin_unlock_irqrestore(&n->list_lock, flags); |
69cb8e6b CL |
4503 | |
4504 | /* Release empty slabs */ | |
916ac052 | 4505 | list_for_each_entry_safe(page, t, &discard, slab_list) |
69cb8e6b | 4506 | discard_slab(s, page); |
ce3712d7 VD |
4507 | |
4508 | if (slabs_node(s, node)) | |
4509 | ret = 1; | |
2086d26a CL |
4510 | } |
4511 | ||
ce3712d7 | 4512 | return ret; |
2086d26a | 4513 | } |
2086d26a | 4514 | |
5a836bf6 SAS |
4515 | int __kmem_cache_shrink(struct kmem_cache *s) |
4516 | { | |
4517 | flush_all(s); | |
4518 | return __kmem_cache_do_shrink(s); | |
4519 | } | |
4520 | ||
b9049e23 YG |
4521 | static int slab_mem_going_offline_callback(void *arg) |
4522 | { | |
4523 | struct kmem_cache *s; | |
4524 | ||
18004c5d | 4525 | mutex_lock(&slab_mutex); |
5a836bf6 SAS |
4526 | list_for_each_entry(s, &slab_caches, list) { |
4527 | flush_all_cpus_locked(s); | |
4528 | __kmem_cache_do_shrink(s); | |
4529 | } | |
18004c5d | 4530 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4531 | |
4532 | return 0; | |
4533 | } | |
4534 | ||
4535 | static void slab_mem_offline_callback(void *arg) | |
4536 | { | |
b9049e23 YG |
4537 | struct memory_notify *marg = arg; |
4538 | int offline_node; | |
4539 | ||
b9d5ab25 | 4540 | offline_node = marg->status_change_nid_normal; |
b9049e23 YG |
4541 | |
4542 | /* | |
4543 | * If the node still has available memory. we need kmem_cache_node | |
4544 | * for it yet. | |
4545 | */ | |
4546 | if (offline_node < 0) | |
4547 | return; | |
4548 | ||
18004c5d | 4549 | mutex_lock(&slab_mutex); |
7e1fa93d | 4550 | node_clear(offline_node, slab_nodes); |
666716fd VB |
4551 | /* |
4552 | * We no longer free kmem_cache_node structures here, as it would be | |
4553 | * racy with all get_node() users, and infeasible to protect them with | |
4554 | * slab_mutex. | |
4555 | */ | |
18004c5d | 4556 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4557 | } |
4558 | ||
4559 | static int slab_mem_going_online_callback(void *arg) | |
4560 | { | |
4561 | struct kmem_cache_node *n; | |
4562 | struct kmem_cache *s; | |
4563 | struct memory_notify *marg = arg; | |
b9d5ab25 | 4564 | int nid = marg->status_change_nid_normal; |
b9049e23 YG |
4565 | int ret = 0; |
4566 | ||
4567 | /* | |
4568 | * If the node's memory is already available, then kmem_cache_node is | |
4569 | * already created. Nothing to do. | |
4570 | */ | |
4571 | if (nid < 0) | |
4572 | return 0; | |
4573 | ||
4574 | /* | |
0121c619 | 4575 | * We are bringing a node online. No memory is available yet. We must |
b9049e23 YG |
4576 | * allocate a kmem_cache_node structure in order to bring the node |
4577 | * online. | |
4578 | */ | |
18004c5d | 4579 | mutex_lock(&slab_mutex); |
b9049e23 | 4580 | list_for_each_entry(s, &slab_caches, list) { |
666716fd VB |
4581 | /* |
4582 | * The structure may already exist if the node was previously | |
4583 | * onlined and offlined. | |
4584 | */ | |
4585 | if (get_node(s, nid)) | |
4586 | continue; | |
b9049e23 YG |
4587 | /* |
4588 | * XXX: kmem_cache_alloc_node will fallback to other nodes | |
4589 | * since memory is not yet available from the node that | |
4590 | * is brought up. | |
4591 | */ | |
8de66a0c | 4592 | n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL); |
b9049e23 YG |
4593 | if (!n) { |
4594 | ret = -ENOMEM; | |
4595 | goto out; | |
4596 | } | |
4053497d | 4597 | init_kmem_cache_node(n); |
b9049e23 YG |
4598 | s->node[nid] = n; |
4599 | } | |
7e1fa93d VB |
4600 | /* |
4601 | * Any cache created after this point will also have kmem_cache_node | |
4602 | * initialized for the new node. | |
4603 | */ | |
4604 | node_set(nid, slab_nodes); | |
b9049e23 | 4605 | out: |
18004c5d | 4606 | mutex_unlock(&slab_mutex); |
b9049e23 YG |
4607 | return ret; |
4608 | } | |
4609 | ||
4610 | static int slab_memory_callback(struct notifier_block *self, | |
4611 | unsigned long action, void *arg) | |
4612 | { | |
4613 | int ret = 0; | |
4614 | ||
4615 | switch (action) { | |
4616 | case MEM_GOING_ONLINE: | |
4617 | ret = slab_mem_going_online_callback(arg); | |
4618 | break; | |
4619 | case MEM_GOING_OFFLINE: | |
4620 | ret = slab_mem_going_offline_callback(arg); | |
4621 | break; | |
4622 | case MEM_OFFLINE: | |
4623 | case MEM_CANCEL_ONLINE: | |
4624 | slab_mem_offline_callback(arg); | |
4625 | break; | |
4626 | case MEM_ONLINE: | |
4627 | case MEM_CANCEL_OFFLINE: | |
4628 | break; | |
4629 | } | |
dc19f9db KH |
4630 | if (ret) |
4631 | ret = notifier_from_errno(ret); | |
4632 | else | |
4633 | ret = NOTIFY_OK; | |
b9049e23 YG |
4634 | return ret; |
4635 | } | |
4636 | ||
3ac38faa AM |
4637 | static struct notifier_block slab_memory_callback_nb = { |
4638 | .notifier_call = slab_memory_callback, | |
4639 | .priority = SLAB_CALLBACK_PRI, | |
4640 | }; | |
b9049e23 | 4641 | |
81819f0f CL |
4642 | /******************************************************************** |
4643 | * Basic setup of slabs | |
4644 | *******************************************************************/ | |
4645 | ||
51df1142 CL |
4646 | /* |
4647 | * Used for early kmem_cache structures that were allocated using | |
dffb4d60 CL |
4648 | * the page allocator. Allocate them properly then fix up the pointers |
4649 | * that may be pointing to the wrong kmem_cache structure. | |
51df1142 CL |
4650 | */ |
4651 | ||
dffb4d60 | 4652 | static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache) |
51df1142 CL |
4653 | { |
4654 | int node; | |
dffb4d60 | 4655 | struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); |
fa45dc25 | 4656 | struct kmem_cache_node *n; |
51df1142 | 4657 | |
dffb4d60 | 4658 | memcpy(s, static_cache, kmem_cache->object_size); |
51df1142 | 4659 | |
7d557b3c GC |
4660 | /* |
4661 | * This runs very early, and only the boot processor is supposed to be | |
4662 | * up. Even if it weren't true, IRQs are not up so we couldn't fire | |
4663 | * IPIs around. | |
4664 | */ | |
4665 | __flush_cpu_slab(s, smp_processor_id()); | |
fa45dc25 | 4666 | for_each_kmem_cache_node(s, node, n) { |
51df1142 CL |
4667 | struct page *p; |
4668 | ||
916ac052 | 4669 | list_for_each_entry(p, &n->partial, slab_list) |
fa45dc25 | 4670 | p->slab_cache = s; |
51df1142 | 4671 | |
607bf324 | 4672 | #ifdef CONFIG_SLUB_DEBUG |
916ac052 | 4673 | list_for_each_entry(p, &n->full, slab_list) |
fa45dc25 | 4674 | p->slab_cache = s; |
51df1142 | 4675 | #endif |
51df1142 | 4676 | } |
dffb4d60 CL |
4677 | list_add(&s->list, &slab_caches); |
4678 | return s; | |
51df1142 CL |
4679 | } |
4680 | ||
81819f0f CL |
4681 | void __init kmem_cache_init(void) |
4682 | { | |
dffb4d60 CL |
4683 | static __initdata struct kmem_cache boot_kmem_cache, |
4684 | boot_kmem_cache_node; | |
7e1fa93d | 4685 | int node; |
51df1142 | 4686 | |
fc8d8620 SG |
4687 | if (debug_guardpage_minorder()) |
4688 | slub_max_order = 0; | |
4689 | ||
79270291 SB |
4690 | /* Print slub debugging pointers without hashing */ |
4691 | if (__slub_debug_enabled()) | |
4692 | no_hash_pointers_enable(NULL); | |
4693 | ||
dffb4d60 CL |
4694 | kmem_cache_node = &boot_kmem_cache_node; |
4695 | kmem_cache = &boot_kmem_cache; | |
51df1142 | 4696 | |
7e1fa93d VB |
4697 | /* |
4698 | * Initialize the nodemask for which we will allocate per node | |
4699 | * structures. Here we don't need taking slab_mutex yet. | |
4700 | */ | |
4701 | for_each_node_state(node, N_NORMAL_MEMORY) | |
4702 | node_set(node, slab_nodes); | |
4703 | ||
dffb4d60 | 4704 | create_boot_cache(kmem_cache_node, "kmem_cache_node", |
8eb8284b | 4705 | sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0); |
b9049e23 | 4706 | |
3ac38faa | 4707 | register_hotmemory_notifier(&slab_memory_callback_nb); |
81819f0f CL |
4708 | |
4709 | /* Able to allocate the per node structures */ | |
4710 | slab_state = PARTIAL; | |
4711 | ||
dffb4d60 CL |
4712 | create_boot_cache(kmem_cache, "kmem_cache", |
4713 | offsetof(struct kmem_cache, node) + | |
4714 | nr_node_ids * sizeof(struct kmem_cache_node *), | |
8eb8284b | 4715 | SLAB_HWCACHE_ALIGN, 0, 0); |
8a13a4cc | 4716 | |
dffb4d60 | 4717 | kmem_cache = bootstrap(&boot_kmem_cache); |
dffb4d60 | 4718 | kmem_cache_node = bootstrap(&boot_kmem_cache_node); |
51df1142 CL |
4719 | |
4720 | /* Now we can use the kmem_cache to allocate kmalloc slabs */ | |
34cc6990 | 4721 | setup_kmalloc_cache_index_table(); |
f97d5f63 | 4722 | create_kmalloc_caches(0); |
81819f0f | 4723 | |
210e7a43 TG |
4724 | /* Setup random freelists for each cache */ |
4725 | init_freelist_randomization(); | |
4726 | ||
a96a87bf SAS |
4727 | cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", NULL, |
4728 | slub_cpu_dead); | |
81819f0f | 4729 | |
b9726c26 | 4730 | pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%u\n", |
f97d5f63 | 4731 | cache_line_size(), |
81819f0f CL |
4732 | slub_min_order, slub_max_order, slub_min_objects, |
4733 | nr_cpu_ids, nr_node_ids); | |
4734 | } | |
4735 | ||
7e85ee0c PE |
4736 | void __init kmem_cache_init_late(void) |
4737 | { | |
7e85ee0c PE |
4738 | } |
4739 | ||
2633d7a0 | 4740 | struct kmem_cache * |
f4957d5b | 4741 | __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, |
d50112ed | 4742 | slab_flags_t flags, void (*ctor)(void *)) |
81819f0f | 4743 | { |
10befea9 | 4744 | struct kmem_cache *s; |
81819f0f | 4745 | |
a44cb944 | 4746 | s = find_mergeable(size, align, flags, name, ctor); |
81819f0f CL |
4747 | if (s) { |
4748 | s->refcount++; | |
84d0ddd6 | 4749 | |
81819f0f CL |
4750 | /* |
4751 | * Adjust the object sizes so that we clear | |
4752 | * the complete object on kzalloc. | |
4753 | */ | |
1b473f29 | 4754 | s->object_size = max(s->object_size, size); |
52ee6d74 | 4755 | s->inuse = max(s->inuse, ALIGN(size, sizeof(void *))); |
6446faa2 | 4756 | |
7b8f3b66 | 4757 | if (sysfs_slab_alias(s, name)) { |
7b8f3b66 | 4758 | s->refcount--; |
cbb79694 | 4759 | s = NULL; |
7b8f3b66 | 4760 | } |
a0e1d1be | 4761 | } |
6446faa2 | 4762 | |
cbb79694 CL |
4763 | return s; |
4764 | } | |
84c1cf62 | 4765 | |
d50112ed | 4766 | int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags) |
cbb79694 | 4767 | { |
aac3a166 PE |
4768 | int err; |
4769 | ||
4770 | err = kmem_cache_open(s, flags); | |
4771 | if (err) | |
4772 | return err; | |
20cea968 | 4773 | |
45530c44 CL |
4774 | /* Mutex is not taken during early boot */ |
4775 | if (slab_state <= UP) | |
4776 | return 0; | |
4777 | ||
aac3a166 | 4778 | err = sysfs_slab_add(s); |
aac3a166 | 4779 | if (err) |
52b4b950 | 4780 | __kmem_cache_release(s); |
20cea968 | 4781 | |
64dd6849 FM |
4782 | if (s->flags & SLAB_STORE_USER) |
4783 | debugfs_slab_add(s); | |
4784 | ||
aac3a166 | 4785 | return err; |
81819f0f | 4786 | } |
81819f0f | 4787 | |
ce71e27c | 4788 | void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller) |
81819f0f | 4789 | { |
aadb4bc4 | 4790 | struct kmem_cache *s; |
94b528d0 | 4791 | void *ret; |
aadb4bc4 | 4792 | |
95a05b42 | 4793 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) |
eada35ef PE |
4794 | return kmalloc_large(size, gfpflags); |
4795 | ||
2c59dd65 | 4796 | s = kmalloc_slab(size, gfpflags); |
81819f0f | 4797 | |
2408c550 | 4798 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
6cb8f913 | 4799 | return s; |
81819f0f | 4800 | |
b89fb5ef | 4801 | ret = slab_alloc(s, gfpflags, caller, size); |
94b528d0 | 4802 | |
25985edc | 4803 | /* Honor the call site pointer we received. */ |
ca2b84cb | 4804 | trace_kmalloc(caller, ret, size, s->size, gfpflags); |
94b528d0 EGM |
4805 | |
4806 | return ret; | |
81819f0f | 4807 | } |
fd7cb575 | 4808 | EXPORT_SYMBOL(__kmalloc_track_caller); |
81819f0f | 4809 | |
5d1f57e4 | 4810 | #ifdef CONFIG_NUMA |
81819f0f | 4811 | void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags, |
ce71e27c | 4812 | int node, unsigned long caller) |
81819f0f | 4813 | { |
aadb4bc4 | 4814 | struct kmem_cache *s; |
94b528d0 | 4815 | void *ret; |
aadb4bc4 | 4816 | |
95a05b42 | 4817 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { |
d3e14aa3 XF |
4818 | ret = kmalloc_large_node(size, gfpflags, node); |
4819 | ||
4820 | trace_kmalloc_node(caller, ret, | |
4821 | size, PAGE_SIZE << get_order(size), | |
4822 | gfpflags, node); | |
4823 | ||
4824 | return ret; | |
4825 | } | |
eada35ef | 4826 | |
2c59dd65 | 4827 | s = kmalloc_slab(size, gfpflags); |
81819f0f | 4828 | |
2408c550 | 4829 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
6cb8f913 | 4830 | return s; |
81819f0f | 4831 | |
b89fb5ef | 4832 | ret = slab_alloc_node(s, gfpflags, node, caller, size); |
94b528d0 | 4833 | |
25985edc | 4834 | /* Honor the call site pointer we received. */ |
ca2b84cb | 4835 | trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node); |
94b528d0 EGM |
4836 | |
4837 | return ret; | |
81819f0f | 4838 | } |
fd7cb575 | 4839 | EXPORT_SYMBOL(__kmalloc_node_track_caller); |
5d1f57e4 | 4840 | #endif |
81819f0f | 4841 | |
ab4d5ed5 | 4842 | #ifdef CONFIG_SYSFS |
205ab99d CL |
4843 | static int count_inuse(struct page *page) |
4844 | { | |
4845 | return page->inuse; | |
4846 | } | |
4847 | ||
4848 | static int count_total(struct page *page) | |
4849 | { | |
4850 | return page->objects; | |
4851 | } | |
ab4d5ed5 | 4852 | #endif |
205ab99d | 4853 | |
ab4d5ed5 | 4854 | #ifdef CONFIG_SLUB_DEBUG |
0a19e7dd VB |
4855 | static void validate_slab(struct kmem_cache *s, struct page *page, |
4856 | unsigned long *obj_map) | |
53e15af0 CL |
4857 | { |
4858 | void *p; | |
a973e9dd | 4859 | void *addr = page_address(page); |
a2b4ae8b | 4860 | unsigned long flags; |
90e9f6a6 | 4861 | |
a2b4ae8b | 4862 | slab_lock(page, &flags); |
53e15af0 | 4863 | |
dd98afd4 | 4864 | if (!check_slab(s, page) || !on_freelist(s, page, NULL)) |
90e9f6a6 | 4865 | goto unlock; |
53e15af0 CL |
4866 | |
4867 | /* Now we know that a valid freelist exists */ | |
0a19e7dd | 4868 | __fill_map(obj_map, s, page); |
5f80b13a | 4869 | for_each_object(p, s, addr, page->objects) { |
0a19e7dd | 4870 | u8 val = test_bit(__obj_to_index(s, addr, p), obj_map) ? |
dd98afd4 | 4871 | SLUB_RED_INACTIVE : SLUB_RED_ACTIVE; |
53e15af0 | 4872 | |
dd98afd4 YZ |
4873 | if (!check_object(s, page, p, val)) |
4874 | break; | |
4875 | } | |
90e9f6a6 | 4876 | unlock: |
a2b4ae8b | 4877 | slab_unlock(page, &flags); |
53e15af0 CL |
4878 | } |
4879 | ||
434e245d | 4880 | static int validate_slab_node(struct kmem_cache *s, |
0a19e7dd | 4881 | struct kmem_cache_node *n, unsigned long *obj_map) |
53e15af0 CL |
4882 | { |
4883 | unsigned long count = 0; | |
4884 | struct page *page; | |
4885 | unsigned long flags; | |
4886 | ||
4887 | spin_lock_irqsave(&n->list_lock, flags); | |
4888 | ||
916ac052 | 4889 | list_for_each_entry(page, &n->partial, slab_list) { |
0a19e7dd | 4890 | validate_slab(s, page, obj_map); |
53e15af0 CL |
4891 | count++; |
4892 | } | |
1f9f78b1 | 4893 | if (count != n->nr_partial) { |
f9f58285 FF |
4894 | pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n", |
4895 | s->name, count, n->nr_partial); | |
1f9f78b1 OG |
4896 | slab_add_kunit_errors(); |
4897 | } | |
53e15af0 CL |
4898 | |
4899 | if (!(s->flags & SLAB_STORE_USER)) | |
4900 | goto out; | |
4901 | ||
916ac052 | 4902 | list_for_each_entry(page, &n->full, slab_list) { |
0a19e7dd | 4903 | validate_slab(s, page, obj_map); |
53e15af0 CL |
4904 | count++; |
4905 | } | |
1f9f78b1 | 4906 | if (count != atomic_long_read(&n->nr_slabs)) { |
f9f58285 FF |
4907 | pr_err("SLUB: %s %ld slabs counted but counter=%ld\n", |
4908 | s->name, count, atomic_long_read(&n->nr_slabs)); | |
1f9f78b1 OG |
4909 | slab_add_kunit_errors(); |
4910 | } | |
53e15af0 CL |
4911 | |
4912 | out: | |
4913 | spin_unlock_irqrestore(&n->list_lock, flags); | |
4914 | return count; | |
4915 | } | |
4916 | ||
1f9f78b1 | 4917 | long validate_slab_cache(struct kmem_cache *s) |
53e15af0 CL |
4918 | { |
4919 | int node; | |
4920 | unsigned long count = 0; | |
fa45dc25 | 4921 | struct kmem_cache_node *n; |
0a19e7dd VB |
4922 | unsigned long *obj_map; |
4923 | ||
4924 | obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL); | |
4925 | if (!obj_map) | |
4926 | return -ENOMEM; | |
53e15af0 CL |
4927 | |
4928 | flush_all(s); | |
fa45dc25 | 4929 | for_each_kmem_cache_node(s, node, n) |
0a19e7dd VB |
4930 | count += validate_slab_node(s, n, obj_map); |
4931 | ||
4932 | bitmap_free(obj_map); | |
90e9f6a6 | 4933 | |
53e15af0 CL |
4934 | return count; |
4935 | } | |
1f9f78b1 OG |
4936 | EXPORT_SYMBOL(validate_slab_cache); |
4937 | ||
64dd6849 | 4938 | #ifdef CONFIG_DEBUG_FS |
88a420e4 | 4939 | /* |
672bba3a | 4940 | * Generate lists of code addresses where slabcache objects are allocated |
88a420e4 CL |
4941 | * and freed. |
4942 | */ | |
4943 | ||
4944 | struct location { | |
4945 | unsigned long count; | |
ce71e27c | 4946 | unsigned long addr; |
45edfa58 CL |
4947 | long long sum_time; |
4948 | long min_time; | |
4949 | long max_time; | |
4950 | long min_pid; | |
4951 | long max_pid; | |
174596a0 | 4952 | DECLARE_BITMAP(cpus, NR_CPUS); |
45edfa58 | 4953 | nodemask_t nodes; |
88a420e4 CL |
4954 | }; |
4955 | ||
4956 | struct loc_track { | |
4957 | unsigned long max; | |
4958 | unsigned long count; | |
4959 | struct location *loc; | |
4960 | }; | |
4961 | ||
64dd6849 FM |
4962 | static struct dentry *slab_debugfs_root; |
4963 | ||
88a420e4 CL |
4964 | static void free_loc_track(struct loc_track *t) |
4965 | { | |
4966 | if (t->max) | |
4967 | free_pages((unsigned long)t->loc, | |
4968 | get_order(sizeof(struct location) * t->max)); | |
4969 | } | |
4970 | ||
68dff6a9 | 4971 | static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags) |
88a420e4 CL |
4972 | { |
4973 | struct location *l; | |
4974 | int order; | |
4975 | ||
88a420e4 CL |
4976 | order = get_order(sizeof(struct location) * max); |
4977 | ||
68dff6a9 | 4978 | l = (void *)__get_free_pages(flags, order); |
88a420e4 CL |
4979 | if (!l) |
4980 | return 0; | |
4981 | ||
4982 | if (t->count) { | |
4983 | memcpy(l, t->loc, sizeof(struct location) * t->count); | |
4984 | free_loc_track(t); | |
4985 | } | |
4986 | t->max = max; | |
4987 | t->loc = l; | |
4988 | return 1; | |
4989 | } | |
4990 | ||
4991 | static int add_location(struct loc_track *t, struct kmem_cache *s, | |
45edfa58 | 4992 | const struct track *track) |
88a420e4 CL |
4993 | { |
4994 | long start, end, pos; | |
4995 | struct location *l; | |
ce71e27c | 4996 | unsigned long caddr; |
45edfa58 | 4997 | unsigned long age = jiffies - track->when; |
88a420e4 CL |
4998 | |
4999 | start = -1; | |
5000 | end = t->count; | |
5001 | ||
5002 | for ( ; ; ) { | |
5003 | pos = start + (end - start + 1) / 2; | |
5004 | ||
5005 | /* | |
5006 | * There is nothing at "end". If we end up there | |
5007 | * we need to add something to before end. | |
5008 | */ | |
5009 | if (pos == end) | |
5010 | break; | |
5011 | ||
5012 | caddr = t->loc[pos].addr; | |
45edfa58 CL |
5013 | if (track->addr == caddr) { |
5014 | ||
5015 | l = &t->loc[pos]; | |
5016 | l->count++; | |
5017 | if (track->when) { | |
5018 | l->sum_time += age; | |
5019 | if (age < l->min_time) | |
5020 | l->min_time = age; | |
5021 | if (age > l->max_time) | |
5022 | l->max_time = age; | |
5023 | ||
5024 | if (track->pid < l->min_pid) | |
5025 | l->min_pid = track->pid; | |
5026 | if (track->pid > l->max_pid) | |
5027 | l->max_pid = track->pid; | |
5028 | ||
174596a0 RR |
5029 | cpumask_set_cpu(track->cpu, |
5030 | to_cpumask(l->cpus)); | |
45edfa58 CL |
5031 | } |
5032 | node_set(page_to_nid(virt_to_page(track)), l->nodes); | |
88a420e4 CL |
5033 | return 1; |
5034 | } | |
5035 | ||
45edfa58 | 5036 | if (track->addr < caddr) |
88a420e4 CL |
5037 | end = pos; |
5038 | else | |
5039 | start = pos; | |
5040 | } | |
5041 | ||
5042 | /* | |
672bba3a | 5043 | * Not found. Insert new tracking element. |
88a420e4 | 5044 | */ |
68dff6a9 | 5045 | if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC)) |
88a420e4 CL |
5046 | return 0; |
5047 | ||
5048 | l = t->loc + pos; | |
5049 | if (pos < t->count) | |
5050 | memmove(l + 1, l, | |
5051 | (t->count - pos) * sizeof(struct location)); | |
5052 | t->count++; | |
5053 | l->count = 1; | |
45edfa58 CL |
5054 | l->addr = track->addr; |
5055 | l->sum_time = age; | |
5056 | l->min_time = age; | |
5057 | l->max_time = age; | |
5058 | l->min_pid = track->pid; | |
5059 | l->max_pid = track->pid; | |
174596a0 RR |
5060 | cpumask_clear(to_cpumask(l->cpus)); |
5061 | cpumask_set_cpu(track->cpu, to_cpumask(l->cpus)); | |
45edfa58 CL |
5062 | nodes_clear(l->nodes); |
5063 | node_set(page_to_nid(virt_to_page(track)), l->nodes); | |
88a420e4 CL |
5064 | return 1; |
5065 | } | |
5066 | ||
5067 | static void process_slab(struct loc_track *t, struct kmem_cache *s, | |
b3fd64e1 VB |
5068 | struct page *page, enum track_item alloc, |
5069 | unsigned long *obj_map) | |
88a420e4 | 5070 | { |
a973e9dd | 5071 | void *addr = page_address(page); |
88a420e4 CL |
5072 | void *p; |
5073 | ||
b3fd64e1 VB |
5074 | __fill_map(obj_map, s, page); |
5075 | ||
224a88be | 5076 | for_each_object(p, s, addr, page->objects) |
b3fd64e1 | 5077 | if (!test_bit(__obj_to_index(s, addr, p), obj_map)) |
45edfa58 | 5078 | add_location(t, s, get_track(s, p, alloc)); |
88a420e4 | 5079 | } |
64dd6849 | 5080 | #endif /* CONFIG_DEBUG_FS */ |
6dfd1b65 | 5081 | #endif /* CONFIG_SLUB_DEBUG */ |
88a420e4 | 5082 | |
ab4d5ed5 | 5083 | #ifdef CONFIG_SYSFS |
81819f0f | 5084 | enum slab_stat_type { |
205ab99d CL |
5085 | SL_ALL, /* All slabs */ |
5086 | SL_PARTIAL, /* Only partially allocated slabs */ | |
5087 | SL_CPU, /* Only slabs used for cpu caches */ | |
5088 | SL_OBJECTS, /* Determine allocated objects not slabs */ | |
5089 | SL_TOTAL /* Determine object capacity not slabs */ | |
81819f0f CL |
5090 | }; |
5091 | ||
205ab99d | 5092 | #define SO_ALL (1 << SL_ALL) |
81819f0f CL |
5093 | #define SO_PARTIAL (1 << SL_PARTIAL) |
5094 | #define SO_CPU (1 << SL_CPU) | |
5095 | #define SO_OBJECTS (1 << SL_OBJECTS) | |
205ab99d | 5096 | #define SO_TOTAL (1 << SL_TOTAL) |
81819f0f | 5097 | |
62e5c4b4 | 5098 | static ssize_t show_slab_objects(struct kmem_cache *s, |
bf16d19a | 5099 | char *buf, unsigned long flags) |
81819f0f CL |
5100 | { |
5101 | unsigned long total = 0; | |
81819f0f CL |
5102 | int node; |
5103 | int x; | |
5104 | unsigned long *nodes; | |
bf16d19a | 5105 | int len = 0; |
81819f0f | 5106 | |
6396bb22 | 5107 | nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL); |
62e5c4b4 CG |
5108 | if (!nodes) |
5109 | return -ENOMEM; | |
81819f0f | 5110 | |
205ab99d CL |
5111 | if (flags & SO_CPU) { |
5112 | int cpu; | |
81819f0f | 5113 | |
205ab99d | 5114 | for_each_possible_cpu(cpu) { |
d0e0ac97 CG |
5115 | struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, |
5116 | cpu); | |
ec3ab083 | 5117 | int node; |
49e22585 | 5118 | struct page *page; |
dfb4f096 | 5119 | |
4db0c3c2 | 5120 | page = READ_ONCE(c->page); |
ec3ab083 CL |
5121 | if (!page) |
5122 | continue; | |
205ab99d | 5123 | |
ec3ab083 CL |
5124 | node = page_to_nid(page); |
5125 | if (flags & SO_TOTAL) | |
5126 | x = page->objects; | |
5127 | else if (flags & SO_OBJECTS) | |
5128 | x = page->inuse; | |
5129 | else | |
5130 | x = 1; | |
49e22585 | 5131 | |
ec3ab083 CL |
5132 | total += x; |
5133 | nodes[node] += x; | |
5134 | ||
a93cf07b | 5135 | page = slub_percpu_partial_read_once(c); |
49e22585 | 5136 | if (page) { |
8afb1474 LZ |
5137 | node = page_to_nid(page); |
5138 | if (flags & SO_TOTAL) | |
5139 | WARN_ON_ONCE(1); | |
5140 | else if (flags & SO_OBJECTS) | |
5141 | WARN_ON_ONCE(1); | |
5142 | else | |
5143 | x = page->pages; | |
bc6697d8 ED |
5144 | total += x; |
5145 | nodes[node] += x; | |
49e22585 | 5146 | } |
81819f0f CL |
5147 | } |
5148 | } | |
5149 | ||
e4f8e513 QC |
5150 | /* |
5151 | * It is impossible to take "mem_hotplug_lock" here with "kernfs_mutex" | |
5152 | * already held which will conflict with an existing lock order: | |
5153 | * | |
5154 | * mem_hotplug_lock->slab_mutex->kernfs_mutex | |
5155 | * | |
5156 | * We don't really need mem_hotplug_lock (to hold off | |
5157 | * slab_mem_going_offline_callback) here because slab's memory hot | |
5158 | * unplug code doesn't destroy the kmem_cache->node[] data. | |
5159 | */ | |
5160 | ||
ab4d5ed5 | 5161 | #ifdef CONFIG_SLUB_DEBUG |
205ab99d | 5162 | if (flags & SO_ALL) { |
fa45dc25 CL |
5163 | struct kmem_cache_node *n; |
5164 | ||
5165 | for_each_kmem_cache_node(s, node, n) { | |
205ab99d | 5166 | |
d0e0ac97 CG |
5167 | if (flags & SO_TOTAL) |
5168 | x = atomic_long_read(&n->total_objects); | |
5169 | else if (flags & SO_OBJECTS) | |
5170 | x = atomic_long_read(&n->total_objects) - | |
5171 | count_partial(n, count_free); | |
81819f0f | 5172 | else |
205ab99d | 5173 | x = atomic_long_read(&n->nr_slabs); |
81819f0f CL |
5174 | total += x; |
5175 | nodes[node] += x; | |
5176 | } | |
5177 | ||
ab4d5ed5 CL |
5178 | } else |
5179 | #endif | |
5180 | if (flags & SO_PARTIAL) { | |
fa45dc25 | 5181 | struct kmem_cache_node *n; |
81819f0f | 5182 | |
fa45dc25 | 5183 | for_each_kmem_cache_node(s, node, n) { |
205ab99d CL |
5184 | if (flags & SO_TOTAL) |
5185 | x = count_partial(n, count_total); | |
5186 | else if (flags & SO_OBJECTS) | |
5187 | x = count_partial(n, count_inuse); | |
81819f0f | 5188 | else |
205ab99d | 5189 | x = n->nr_partial; |
81819f0f CL |
5190 | total += x; |
5191 | nodes[node] += x; | |
5192 | } | |
5193 | } | |
bf16d19a JP |
5194 | |
5195 | len += sysfs_emit_at(buf, len, "%lu", total); | |
81819f0f | 5196 | #ifdef CONFIG_NUMA |
bf16d19a | 5197 | for (node = 0; node < nr_node_ids; node++) { |
81819f0f | 5198 | if (nodes[node]) |
bf16d19a JP |
5199 | len += sysfs_emit_at(buf, len, " N%d=%lu", |
5200 | node, nodes[node]); | |
5201 | } | |
81819f0f | 5202 | #endif |
bf16d19a | 5203 | len += sysfs_emit_at(buf, len, "\n"); |
81819f0f | 5204 | kfree(nodes); |
bf16d19a JP |
5205 | |
5206 | return len; | |
81819f0f CL |
5207 | } |
5208 | ||
81819f0f | 5209 | #define to_slab_attr(n) container_of(n, struct slab_attribute, attr) |
497888cf | 5210 | #define to_slab(n) container_of(n, struct kmem_cache, kobj) |
81819f0f CL |
5211 | |
5212 | struct slab_attribute { | |
5213 | struct attribute attr; | |
5214 | ssize_t (*show)(struct kmem_cache *s, char *buf); | |
5215 | ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count); | |
5216 | }; | |
5217 | ||
5218 | #define SLAB_ATTR_RO(_name) \ | |
ab067e99 VK |
5219 | static struct slab_attribute _name##_attr = \ |
5220 | __ATTR(_name, 0400, _name##_show, NULL) | |
81819f0f CL |
5221 | |
5222 | #define SLAB_ATTR(_name) \ | |
5223 | static struct slab_attribute _name##_attr = \ | |
ab067e99 | 5224 | __ATTR(_name, 0600, _name##_show, _name##_store) |
81819f0f | 5225 | |
81819f0f CL |
5226 | static ssize_t slab_size_show(struct kmem_cache *s, char *buf) |
5227 | { | |
bf16d19a | 5228 | return sysfs_emit(buf, "%u\n", s->size); |
81819f0f CL |
5229 | } |
5230 | SLAB_ATTR_RO(slab_size); | |
5231 | ||
5232 | static ssize_t align_show(struct kmem_cache *s, char *buf) | |
5233 | { | |
bf16d19a | 5234 | return sysfs_emit(buf, "%u\n", s->align); |
81819f0f CL |
5235 | } |
5236 | SLAB_ATTR_RO(align); | |
5237 | ||
5238 | static ssize_t object_size_show(struct kmem_cache *s, char *buf) | |
5239 | { | |
bf16d19a | 5240 | return sysfs_emit(buf, "%u\n", s->object_size); |
81819f0f CL |
5241 | } |
5242 | SLAB_ATTR_RO(object_size); | |
5243 | ||
5244 | static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf) | |
5245 | { | |
bf16d19a | 5246 | return sysfs_emit(buf, "%u\n", oo_objects(s->oo)); |
81819f0f CL |
5247 | } |
5248 | SLAB_ATTR_RO(objs_per_slab); | |
5249 | ||
5250 | static ssize_t order_show(struct kmem_cache *s, char *buf) | |
5251 | { | |
bf16d19a | 5252 | return sysfs_emit(buf, "%u\n", oo_order(s->oo)); |
81819f0f | 5253 | } |
32a6f409 | 5254 | SLAB_ATTR_RO(order); |
81819f0f | 5255 | |
73d342b1 DR |
5256 | static ssize_t min_partial_show(struct kmem_cache *s, char *buf) |
5257 | { | |
bf16d19a | 5258 | return sysfs_emit(buf, "%lu\n", s->min_partial); |
73d342b1 DR |
5259 | } |
5260 | ||
5261 | static ssize_t min_partial_store(struct kmem_cache *s, const char *buf, | |
5262 | size_t length) | |
5263 | { | |
5264 | unsigned long min; | |
5265 | int err; | |
5266 | ||
3dbb95f7 | 5267 | err = kstrtoul(buf, 10, &min); |
73d342b1 DR |
5268 | if (err) |
5269 | return err; | |
5270 | ||
c0bdb232 | 5271 | set_min_partial(s, min); |
73d342b1 DR |
5272 | return length; |
5273 | } | |
5274 | SLAB_ATTR(min_partial); | |
5275 | ||
49e22585 CL |
5276 | static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf) |
5277 | { | |
bf16d19a | 5278 | return sysfs_emit(buf, "%u\n", slub_cpu_partial(s)); |
49e22585 CL |
5279 | } |
5280 | ||
5281 | static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf, | |
5282 | size_t length) | |
5283 | { | |
e5d9998f | 5284 | unsigned int objects; |
49e22585 CL |
5285 | int err; |
5286 | ||
e5d9998f | 5287 | err = kstrtouint(buf, 10, &objects); |
49e22585 CL |
5288 | if (err) |
5289 | return err; | |
345c905d | 5290 | if (objects && !kmem_cache_has_cpu_partial(s)) |
74ee4ef1 | 5291 | return -EINVAL; |
49e22585 | 5292 | |
e6d0e1dc | 5293 | slub_set_cpu_partial(s, objects); |
49e22585 CL |
5294 | flush_all(s); |
5295 | return length; | |
5296 | } | |
5297 | SLAB_ATTR(cpu_partial); | |
5298 | ||
81819f0f CL |
5299 | static ssize_t ctor_show(struct kmem_cache *s, char *buf) |
5300 | { | |
62c70bce JP |
5301 | if (!s->ctor) |
5302 | return 0; | |
bf16d19a | 5303 | return sysfs_emit(buf, "%pS\n", s->ctor); |
81819f0f CL |
5304 | } |
5305 | SLAB_ATTR_RO(ctor); | |
5306 | ||
81819f0f CL |
5307 | static ssize_t aliases_show(struct kmem_cache *s, char *buf) |
5308 | { | |
bf16d19a | 5309 | return sysfs_emit(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1); |
81819f0f CL |
5310 | } |
5311 | SLAB_ATTR_RO(aliases); | |
5312 | ||
81819f0f CL |
5313 | static ssize_t partial_show(struct kmem_cache *s, char *buf) |
5314 | { | |
d9acf4b7 | 5315 | return show_slab_objects(s, buf, SO_PARTIAL); |
81819f0f CL |
5316 | } |
5317 | SLAB_ATTR_RO(partial); | |
5318 | ||
5319 | static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf) | |
5320 | { | |
d9acf4b7 | 5321 | return show_slab_objects(s, buf, SO_CPU); |
81819f0f CL |
5322 | } |
5323 | SLAB_ATTR_RO(cpu_slabs); | |
5324 | ||
5325 | static ssize_t objects_show(struct kmem_cache *s, char *buf) | |
5326 | { | |
205ab99d | 5327 | return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS); |
81819f0f CL |
5328 | } |
5329 | SLAB_ATTR_RO(objects); | |
5330 | ||
205ab99d CL |
5331 | static ssize_t objects_partial_show(struct kmem_cache *s, char *buf) |
5332 | { | |
5333 | return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS); | |
5334 | } | |
5335 | SLAB_ATTR_RO(objects_partial); | |
5336 | ||
49e22585 CL |
5337 | static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf) |
5338 | { | |
5339 | int objects = 0; | |
5340 | int pages = 0; | |
5341 | int cpu; | |
bf16d19a | 5342 | int len = 0; |
49e22585 CL |
5343 | |
5344 | for_each_online_cpu(cpu) { | |
a93cf07b WY |
5345 | struct page *page; |
5346 | ||
5347 | page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu)); | |
49e22585 CL |
5348 | |
5349 | if (page) { | |
5350 | pages += page->pages; | |
5351 | objects += page->pobjects; | |
5352 | } | |
5353 | } | |
5354 | ||
bf16d19a | 5355 | len += sysfs_emit_at(buf, len, "%d(%d)", objects, pages); |
49e22585 CL |
5356 | |
5357 | #ifdef CONFIG_SMP | |
5358 | for_each_online_cpu(cpu) { | |
a93cf07b WY |
5359 | struct page *page; |
5360 | ||
5361 | page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu)); | |
bf16d19a JP |
5362 | if (page) |
5363 | len += sysfs_emit_at(buf, len, " C%d=%d(%d)", | |
5364 | cpu, page->pobjects, page->pages); | |
49e22585 CL |
5365 | } |
5366 | #endif | |
bf16d19a JP |
5367 | len += sysfs_emit_at(buf, len, "\n"); |
5368 | ||
5369 | return len; | |
49e22585 CL |
5370 | } |
5371 | SLAB_ATTR_RO(slabs_cpu_partial); | |
5372 | ||
a5a84755 CL |
5373 | static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf) |
5374 | { | |
bf16d19a | 5375 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT)); |
a5a84755 | 5376 | } |
8f58119a | 5377 | SLAB_ATTR_RO(reclaim_account); |
a5a84755 CL |
5378 | |
5379 | static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf) | |
5380 | { | |
bf16d19a | 5381 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN)); |
a5a84755 CL |
5382 | } |
5383 | SLAB_ATTR_RO(hwcache_align); | |
5384 | ||
5385 | #ifdef CONFIG_ZONE_DMA | |
5386 | static ssize_t cache_dma_show(struct kmem_cache *s, char *buf) | |
5387 | { | |
bf16d19a | 5388 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA)); |
a5a84755 CL |
5389 | } |
5390 | SLAB_ATTR_RO(cache_dma); | |
5391 | #endif | |
5392 | ||
8eb8284b DW |
5393 | static ssize_t usersize_show(struct kmem_cache *s, char *buf) |
5394 | { | |
bf16d19a | 5395 | return sysfs_emit(buf, "%u\n", s->usersize); |
8eb8284b DW |
5396 | } |
5397 | SLAB_ATTR_RO(usersize); | |
5398 | ||
a5a84755 CL |
5399 | static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf) |
5400 | { | |
bf16d19a | 5401 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU)); |
a5a84755 CL |
5402 | } |
5403 | SLAB_ATTR_RO(destroy_by_rcu); | |
5404 | ||
ab4d5ed5 | 5405 | #ifdef CONFIG_SLUB_DEBUG |
a5a84755 CL |
5406 | static ssize_t slabs_show(struct kmem_cache *s, char *buf) |
5407 | { | |
5408 | return show_slab_objects(s, buf, SO_ALL); | |
5409 | } | |
5410 | SLAB_ATTR_RO(slabs); | |
5411 | ||
205ab99d CL |
5412 | static ssize_t total_objects_show(struct kmem_cache *s, char *buf) |
5413 | { | |
5414 | return show_slab_objects(s, buf, SO_ALL|SO_TOTAL); | |
5415 | } | |
5416 | SLAB_ATTR_RO(total_objects); | |
5417 | ||
81819f0f CL |
5418 | static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf) |
5419 | { | |
bf16d19a | 5420 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS)); |
81819f0f | 5421 | } |
060807f8 | 5422 | SLAB_ATTR_RO(sanity_checks); |
81819f0f CL |
5423 | |
5424 | static ssize_t trace_show(struct kmem_cache *s, char *buf) | |
5425 | { | |
bf16d19a | 5426 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TRACE)); |
81819f0f | 5427 | } |
060807f8 | 5428 | SLAB_ATTR_RO(trace); |
81819f0f | 5429 | |
81819f0f CL |
5430 | static ssize_t red_zone_show(struct kmem_cache *s, char *buf) |
5431 | { | |
bf16d19a | 5432 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE)); |
81819f0f CL |
5433 | } |
5434 | ||
ad38b5b1 | 5435 | SLAB_ATTR_RO(red_zone); |
81819f0f CL |
5436 | |
5437 | static ssize_t poison_show(struct kmem_cache *s, char *buf) | |
5438 | { | |
bf16d19a | 5439 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_POISON)); |
81819f0f CL |
5440 | } |
5441 | ||
ad38b5b1 | 5442 | SLAB_ATTR_RO(poison); |
81819f0f CL |
5443 | |
5444 | static ssize_t store_user_show(struct kmem_cache *s, char *buf) | |
5445 | { | |
bf16d19a | 5446 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_STORE_USER)); |
81819f0f CL |
5447 | } |
5448 | ||
ad38b5b1 | 5449 | SLAB_ATTR_RO(store_user); |
81819f0f | 5450 | |
53e15af0 CL |
5451 | static ssize_t validate_show(struct kmem_cache *s, char *buf) |
5452 | { | |
5453 | return 0; | |
5454 | } | |
5455 | ||
5456 | static ssize_t validate_store(struct kmem_cache *s, | |
5457 | const char *buf, size_t length) | |
5458 | { | |
434e245d CL |
5459 | int ret = -EINVAL; |
5460 | ||
5461 | if (buf[0] == '1') { | |
5462 | ret = validate_slab_cache(s); | |
5463 | if (ret >= 0) | |
5464 | ret = length; | |
5465 | } | |
5466 | return ret; | |
53e15af0 CL |
5467 | } |
5468 | SLAB_ATTR(validate); | |
a5a84755 | 5469 | |
a5a84755 CL |
5470 | #endif /* CONFIG_SLUB_DEBUG */ |
5471 | ||
5472 | #ifdef CONFIG_FAILSLAB | |
5473 | static ssize_t failslab_show(struct kmem_cache *s, char *buf) | |
5474 | { | |
bf16d19a | 5475 | return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB)); |
a5a84755 | 5476 | } |
060807f8 | 5477 | SLAB_ATTR_RO(failslab); |
ab4d5ed5 | 5478 | #endif |
53e15af0 | 5479 | |
2086d26a CL |
5480 | static ssize_t shrink_show(struct kmem_cache *s, char *buf) |
5481 | { | |
5482 | return 0; | |
5483 | } | |
5484 | ||
5485 | static ssize_t shrink_store(struct kmem_cache *s, | |
5486 | const char *buf, size_t length) | |
5487 | { | |
832f37f5 | 5488 | if (buf[0] == '1') |
10befea9 | 5489 | kmem_cache_shrink(s); |
832f37f5 | 5490 | else |
2086d26a CL |
5491 | return -EINVAL; |
5492 | return length; | |
5493 | } | |
5494 | SLAB_ATTR(shrink); | |
5495 | ||
81819f0f | 5496 | #ifdef CONFIG_NUMA |
9824601e | 5497 | static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf) |
81819f0f | 5498 | { |
bf16d19a | 5499 | return sysfs_emit(buf, "%u\n", s->remote_node_defrag_ratio / 10); |
81819f0f CL |
5500 | } |
5501 | ||
9824601e | 5502 | static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s, |
81819f0f CL |
5503 | const char *buf, size_t length) |
5504 | { | |
eb7235eb | 5505 | unsigned int ratio; |
0121c619 CL |
5506 | int err; |
5507 | ||
eb7235eb | 5508 | err = kstrtouint(buf, 10, &ratio); |
0121c619 CL |
5509 | if (err) |
5510 | return err; | |
eb7235eb AD |
5511 | if (ratio > 100) |
5512 | return -ERANGE; | |
0121c619 | 5513 | |
eb7235eb | 5514 | s->remote_node_defrag_ratio = ratio * 10; |
81819f0f | 5515 | |
81819f0f CL |
5516 | return length; |
5517 | } | |
9824601e | 5518 | SLAB_ATTR(remote_node_defrag_ratio); |
81819f0f CL |
5519 | #endif |
5520 | ||
8ff12cfc | 5521 | #ifdef CONFIG_SLUB_STATS |
8ff12cfc CL |
5522 | static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si) |
5523 | { | |
5524 | unsigned long sum = 0; | |
5525 | int cpu; | |
bf16d19a | 5526 | int len = 0; |
6da2ec56 | 5527 | int *data = kmalloc_array(nr_cpu_ids, sizeof(int), GFP_KERNEL); |
8ff12cfc CL |
5528 | |
5529 | if (!data) | |
5530 | return -ENOMEM; | |
5531 | ||
5532 | for_each_online_cpu(cpu) { | |
9dfc6e68 | 5533 | unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si]; |
8ff12cfc CL |
5534 | |
5535 | data[cpu] = x; | |
5536 | sum += x; | |
5537 | } | |
5538 | ||
bf16d19a | 5539 | len += sysfs_emit_at(buf, len, "%lu", sum); |
8ff12cfc | 5540 | |
50ef37b9 | 5541 | #ifdef CONFIG_SMP |
8ff12cfc | 5542 | for_each_online_cpu(cpu) { |
bf16d19a JP |
5543 | if (data[cpu]) |
5544 | len += sysfs_emit_at(buf, len, " C%d=%u", | |
5545 | cpu, data[cpu]); | |
8ff12cfc | 5546 | } |
50ef37b9 | 5547 | #endif |
8ff12cfc | 5548 | kfree(data); |
bf16d19a JP |
5549 | len += sysfs_emit_at(buf, len, "\n"); |
5550 | ||
5551 | return len; | |
8ff12cfc CL |
5552 | } |
5553 | ||
78eb00cc DR |
5554 | static void clear_stat(struct kmem_cache *s, enum stat_item si) |
5555 | { | |
5556 | int cpu; | |
5557 | ||
5558 | for_each_online_cpu(cpu) | |
9dfc6e68 | 5559 | per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0; |
78eb00cc DR |
5560 | } |
5561 | ||
8ff12cfc CL |
5562 | #define STAT_ATTR(si, text) \ |
5563 | static ssize_t text##_show(struct kmem_cache *s, char *buf) \ | |
5564 | { \ | |
5565 | return show_stat(s, buf, si); \ | |
5566 | } \ | |
78eb00cc DR |
5567 | static ssize_t text##_store(struct kmem_cache *s, \ |
5568 | const char *buf, size_t length) \ | |
5569 | { \ | |
5570 | if (buf[0] != '0') \ | |
5571 | return -EINVAL; \ | |
5572 | clear_stat(s, si); \ | |
5573 | return length; \ | |
5574 | } \ | |
5575 | SLAB_ATTR(text); \ | |
8ff12cfc CL |
5576 | |
5577 | STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath); | |
5578 | STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath); | |
5579 | STAT_ATTR(FREE_FASTPATH, free_fastpath); | |
5580 | STAT_ATTR(FREE_SLOWPATH, free_slowpath); | |
5581 | STAT_ATTR(FREE_FROZEN, free_frozen); | |
5582 | STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial); | |
5583 | STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial); | |
5584 | STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial); | |
5585 | STAT_ATTR(ALLOC_SLAB, alloc_slab); | |
5586 | STAT_ATTR(ALLOC_REFILL, alloc_refill); | |
e36a2652 | 5587 | STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch); |
8ff12cfc CL |
5588 | STAT_ATTR(FREE_SLAB, free_slab); |
5589 | STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush); | |
5590 | STAT_ATTR(DEACTIVATE_FULL, deactivate_full); | |
5591 | STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty); | |
5592 | STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head); | |
5593 | STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail); | |
5594 | STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees); | |
03e404af | 5595 | STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass); |
65c3376a | 5596 | STAT_ATTR(ORDER_FALLBACK, order_fallback); |
b789ef51 CL |
5597 | STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail); |
5598 | STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail); | |
49e22585 CL |
5599 | STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc); |
5600 | STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free); | |
8028dcea AS |
5601 | STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node); |
5602 | STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain); | |
6dfd1b65 | 5603 | #endif /* CONFIG_SLUB_STATS */ |
8ff12cfc | 5604 | |
06428780 | 5605 | static struct attribute *slab_attrs[] = { |
81819f0f CL |
5606 | &slab_size_attr.attr, |
5607 | &object_size_attr.attr, | |
5608 | &objs_per_slab_attr.attr, | |
5609 | &order_attr.attr, | |
73d342b1 | 5610 | &min_partial_attr.attr, |
49e22585 | 5611 | &cpu_partial_attr.attr, |
81819f0f | 5612 | &objects_attr.attr, |
205ab99d | 5613 | &objects_partial_attr.attr, |
81819f0f CL |
5614 | &partial_attr.attr, |
5615 | &cpu_slabs_attr.attr, | |
5616 | &ctor_attr.attr, | |
81819f0f CL |
5617 | &aliases_attr.attr, |
5618 | &align_attr.attr, | |
81819f0f CL |
5619 | &hwcache_align_attr.attr, |
5620 | &reclaim_account_attr.attr, | |
5621 | &destroy_by_rcu_attr.attr, | |
a5a84755 | 5622 | &shrink_attr.attr, |
49e22585 | 5623 | &slabs_cpu_partial_attr.attr, |
ab4d5ed5 | 5624 | #ifdef CONFIG_SLUB_DEBUG |
a5a84755 CL |
5625 | &total_objects_attr.attr, |
5626 | &slabs_attr.attr, | |
5627 | &sanity_checks_attr.attr, | |
5628 | &trace_attr.attr, | |
81819f0f CL |
5629 | &red_zone_attr.attr, |
5630 | &poison_attr.attr, | |
5631 | &store_user_attr.attr, | |
53e15af0 | 5632 | &validate_attr.attr, |
ab4d5ed5 | 5633 | #endif |
81819f0f CL |
5634 | #ifdef CONFIG_ZONE_DMA |
5635 | &cache_dma_attr.attr, | |
5636 | #endif | |
5637 | #ifdef CONFIG_NUMA | |
9824601e | 5638 | &remote_node_defrag_ratio_attr.attr, |
8ff12cfc CL |
5639 | #endif |
5640 | #ifdef CONFIG_SLUB_STATS | |
5641 | &alloc_fastpath_attr.attr, | |
5642 | &alloc_slowpath_attr.attr, | |
5643 | &free_fastpath_attr.attr, | |
5644 | &free_slowpath_attr.attr, | |
5645 | &free_frozen_attr.attr, | |
5646 | &free_add_partial_attr.attr, | |
5647 | &free_remove_partial_attr.attr, | |
5648 | &alloc_from_partial_attr.attr, | |
5649 | &alloc_slab_attr.attr, | |
5650 | &alloc_refill_attr.attr, | |
e36a2652 | 5651 | &alloc_node_mismatch_attr.attr, |
8ff12cfc CL |
5652 | &free_slab_attr.attr, |
5653 | &cpuslab_flush_attr.attr, | |
5654 | &deactivate_full_attr.attr, | |
5655 | &deactivate_empty_attr.attr, | |
5656 | &deactivate_to_head_attr.attr, | |
5657 | &deactivate_to_tail_attr.attr, | |
5658 | &deactivate_remote_frees_attr.attr, | |
03e404af | 5659 | &deactivate_bypass_attr.attr, |
65c3376a | 5660 | &order_fallback_attr.attr, |
b789ef51 CL |
5661 | &cmpxchg_double_fail_attr.attr, |
5662 | &cmpxchg_double_cpu_fail_attr.attr, | |
49e22585 CL |
5663 | &cpu_partial_alloc_attr.attr, |
5664 | &cpu_partial_free_attr.attr, | |
8028dcea AS |
5665 | &cpu_partial_node_attr.attr, |
5666 | &cpu_partial_drain_attr.attr, | |
81819f0f | 5667 | #endif |
4c13dd3b DM |
5668 | #ifdef CONFIG_FAILSLAB |
5669 | &failslab_attr.attr, | |
5670 | #endif | |
8eb8284b | 5671 | &usersize_attr.attr, |
4c13dd3b | 5672 | |
81819f0f CL |
5673 | NULL |
5674 | }; | |
5675 | ||
1fdaaa23 | 5676 | static const struct attribute_group slab_attr_group = { |
81819f0f CL |
5677 | .attrs = slab_attrs, |
5678 | }; | |
5679 | ||
5680 | static ssize_t slab_attr_show(struct kobject *kobj, | |
5681 | struct attribute *attr, | |
5682 | char *buf) | |
5683 | { | |
5684 | struct slab_attribute *attribute; | |
5685 | struct kmem_cache *s; | |
5686 | int err; | |
5687 | ||
5688 | attribute = to_slab_attr(attr); | |
5689 | s = to_slab(kobj); | |
5690 | ||
5691 | if (!attribute->show) | |
5692 | return -EIO; | |
5693 | ||
5694 | err = attribute->show(s, buf); | |
5695 | ||
5696 | return err; | |
5697 | } | |
5698 | ||
5699 | static ssize_t slab_attr_store(struct kobject *kobj, | |
5700 | struct attribute *attr, | |
5701 | const char *buf, size_t len) | |
5702 | { | |
5703 | struct slab_attribute *attribute; | |
5704 | struct kmem_cache *s; | |
5705 | int err; | |
5706 | ||
5707 | attribute = to_slab_attr(attr); | |
5708 | s = to_slab(kobj); | |
5709 | ||
5710 | if (!attribute->store) | |
5711 | return -EIO; | |
5712 | ||
5713 | err = attribute->store(s, buf, len); | |
81819f0f CL |
5714 | return err; |
5715 | } | |
5716 | ||
41a21285 CL |
5717 | static void kmem_cache_release(struct kobject *k) |
5718 | { | |
5719 | slab_kmem_cache_release(to_slab(k)); | |
5720 | } | |
5721 | ||
52cf25d0 | 5722 | static const struct sysfs_ops slab_sysfs_ops = { |
81819f0f CL |
5723 | .show = slab_attr_show, |
5724 | .store = slab_attr_store, | |
5725 | }; | |
5726 | ||
5727 | static struct kobj_type slab_ktype = { | |
5728 | .sysfs_ops = &slab_sysfs_ops, | |
41a21285 | 5729 | .release = kmem_cache_release, |
81819f0f CL |
5730 | }; |
5731 | ||
27c3a314 | 5732 | static struct kset *slab_kset; |
81819f0f | 5733 | |
9a41707b VD |
5734 | static inline struct kset *cache_kset(struct kmem_cache *s) |
5735 | { | |
9a41707b VD |
5736 | return slab_kset; |
5737 | } | |
5738 | ||
81819f0f CL |
5739 | #define ID_STR_LENGTH 64 |
5740 | ||
5741 | /* Create a unique string id for a slab cache: | |
6446faa2 CL |
5742 | * |
5743 | * Format :[flags-]size | |
81819f0f CL |
5744 | */ |
5745 | static char *create_unique_id(struct kmem_cache *s) | |
5746 | { | |
5747 | char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL); | |
5748 | char *p = name; | |
5749 | ||
5750 | BUG_ON(!name); | |
5751 | ||
5752 | *p++ = ':'; | |
5753 | /* | |
5754 | * First flags affecting slabcache operations. We will only | |
5755 | * get here for aliasable slabs so we do not need to support | |
5756 | * too many flags. The flags here must cover all flags that | |
5757 | * are matched during merging to guarantee that the id is | |
5758 | * unique. | |
5759 | */ | |
5760 | if (s->flags & SLAB_CACHE_DMA) | |
5761 | *p++ = 'd'; | |
6d6ea1e9 NB |
5762 | if (s->flags & SLAB_CACHE_DMA32) |
5763 | *p++ = 'D'; | |
81819f0f CL |
5764 | if (s->flags & SLAB_RECLAIM_ACCOUNT) |
5765 | *p++ = 'a'; | |
becfda68 | 5766 | if (s->flags & SLAB_CONSISTENCY_CHECKS) |
81819f0f | 5767 | *p++ = 'F'; |
230e9fc2 VD |
5768 | if (s->flags & SLAB_ACCOUNT) |
5769 | *p++ = 'A'; | |
81819f0f CL |
5770 | if (p != name + 1) |
5771 | *p++ = '-'; | |
44065b2e | 5772 | p += sprintf(p, "%07u", s->size); |
2633d7a0 | 5773 | |
81819f0f CL |
5774 | BUG_ON(p > name + ID_STR_LENGTH - 1); |
5775 | return name; | |
5776 | } | |
5777 | ||
5778 | static int sysfs_slab_add(struct kmem_cache *s) | |
5779 | { | |
5780 | int err; | |
5781 | const char *name; | |
1663f26d | 5782 | struct kset *kset = cache_kset(s); |
45530c44 | 5783 | int unmergeable = slab_unmergeable(s); |
81819f0f | 5784 | |
1663f26d TH |
5785 | if (!kset) { |
5786 | kobject_init(&s->kobj, &slab_ktype); | |
5787 | return 0; | |
5788 | } | |
5789 | ||
11066386 MC |
5790 | if (!unmergeable && disable_higher_order_debug && |
5791 | (slub_debug & DEBUG_METADATA_FLAGS)) | |
5792 | unmergeable = 1; | |
5793 | ||
81819f0f CL |
5794 | if (unmergeable) { |
5795 | /* | |
5796 | * Slabcache can never be merged so we can use the name proper. | |
5797 | * This is typically the case for debug situations. In that | |
5798 | * case we can catch duplicate names easily. | |
5799 | */ | |
27c3a314 | 5800 | sysfs_remove_link(&slab_kset->kobj, s->name); |
81819f0f CL |
5801 | name = s->name; |
5802 | } else { | |
5803 | /* | |
5804 | * Create a unique name for the slab as a target | |
5805 | * for the symlinks. | |
5806 | */ | |
5807 | name = create_unique_id(s); | |
5808 | } | |
5809 | ||
1663f26d | 5810 | s->kobj.kset = kset; |
26e4f205 | 5811 | err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name); |
757fed1d | 5812 | if (err) |
80da026a | 5813 | goto out; |
81819f0f CL |
5814 | |
5815 | err = sysfs_create_group(&s->kobj, &slab_attr_group); | |
54b6a731 DJ |
5816 | if (err) |
5817 | goto out_del_kobj; | |
9a41707b | 5818 | |
81819f0f CL |
5819 | if (!unmergeable) { |
5820 | /* Setup first alias */ | |
5821 | sysfs_slab_alias(s, s->name); | |
81819f0f | 5822 | } |
54b6a731 DJ |
5823 | out: |
5824 | if (!unmergeable) | |
5825 | kfree(name); | |
5826 | return err; | |
5827 | out_del_kobj: | |
5828 | kobject_del(&s->kobj); | |
54b6a731 | 5829 | goto out; |
81819f0f CL |
5830 | } |
5831 | ||
d50d82fa MP |
5832 | void sysfs_slab_unlink(struct kmem_cache *s) |
5833 | { | |
5834 | if (slab_state >= FULL) | |
5835 | kobject_del(&s->kobj); | |
5836 | } | |
5837 | ||
bf5eb3de TH |
5838 | void sysfs_slab_release(struct kmem_cache *s) |
5839 | { | |
5840 | if (slab_state >= FULL) | |
5841 | kobject_put(&s->kobj); | |
81819f0f CL |
5842 | } |
5843 | ||
5844 | /* | |
5845 | * Need to buffer aliases during bootup until sysfs becomes | |
9f6c708e | 5846 | * available lest we lose that information. |
81819f0f CL |
5847 | */ |
5848 | struct saved_alias { | |
5849 | struct kmem_cache *s; | |
5850 | const char *name; | |
5851 | struct saved_alias *next; | |
5852 | }; | |
5853 | ||
5af328a5 | 5854 | static struct saved_alias *alias_list; |
81819f0f CL |
5855 | |
5856 | static int sysfs_slab_alias(struct kmem_cache *s, const char *name) | |
5857 | { | |
5858 | struct saved_alias *al; | |
5859 | ||
97d06609 | 5860 | if (slab_state == FULL) { |
81819f0f CL |
5861 | /* |
5862 | * If we have a leftover link then remove it. | |
5863 | */ | |
27c3a314 GKH |
5864 | sysfs_remove_link(&slab_kset->kobj, name); |
5865 | return sysfs_create_link(&slab_kset->kobj, &s->kobj, name); | |
81819f0f CL |
5866 | } |
5867 | ||
5868 | al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL); | |
5869 | if (!al) | |
5870 | return -ENOMEM; | |
5871 | ||
5872 | al->s = s; | |
5873 | al->name = name; | |
5874 | al->next = alias_list; | |
5875 | alias_list = al; | |
5876 | return 0; | |
5877 | } | |
5878 | ||
5879 | static int __init slab_sysfs_init(void) | |
5880 | { | |
5b95a4ac | 5881 | struct kmem_cache *s; |
81819f0f CL |
5882 | int err; |
5883 | ||
18004c5d | 5884 | mutex_lock(&slab_mutex); |
2bce6485 | 5885 | |
d7660ce5 | 5886 | slab_kset = kset_create_and_add("slab", NULL, kernel_kobj); |
27c3a314 | 5887 | if (!slab_kset) { |
18004c5d | 5888 | mutex_unlock(&slab_mutex); |
f9f58285 | 5889 | pr_err("Cannot register slab subsystem.\n"); |
81819f0f CL |
5890 | return -ENOSYS; |
5891 | } | |
5892 | ||
97d06609 | 5893 | slab_state = FULL; |
26a7bd03 | 5894 | |
5b95a4ac | 5895 | list_for_each_entry(s, &slab_caches, list) { |
26a7bd03 | 5896 | err = sysfs_slab_add(s); |
5d540fb7 | 5897 | if (err) |
f9f58285 FF |
5898 | pr_err("SLUB: Unable to add boot slab %s to sysfs\n", |
5899 | s->name); | |
26a7bd03 | 5900 | } |
81819f0f CL |
5901 | |
5902 | while (alias_list) { | |
5903 | struct saved_alias *al = alias_list; | |
5904 | ||
5905 | alias_list = alias_list->next; | |
5906 | err = sysfs_slab_alias(al->s, al->name); | |
5d540fb7 | 5907 | if (err) |
f9f58285 FF |
5908 | pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n", |
5909 | al->name); | |
81819f0f CL |
5910 | kfree(al); |
5911 | } | |
5912 | ||
18004c5d | 5913 | mutex_unlock(&slab_mutex); |
81819f0f CL |
5914 | return 0; |
5915 | } | |
5916 | ||
5917 | __initcall(slab_sysfs_init); | |
ab4d5ed5 | 5918 | #endif /* CONFIG_SYSFS */ |
57ed3eda | 5919 | |
64dd6849 FM |
5920 | #if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS) |
5921 | static int slab_debugfs_show(struct seq_file *seq, void *v) | |
5922 | { | |
5923 | ||
5924 | struct location *l; | |
5925 | unsigned int idx = *(unsigned int *)v; | |
5926 | struct loc_track *t = seq->private; | |
5927 | ||
5928 | if (idx < t->count) { | |
5929 | l = &t->loc[idx]; | |
5930 | ||
5931 | seq_printf(seq, "%7ld ", l->count); | |
5932 | ||
5933 | if (l->addr) | |
5934 | seq_printf(seq, "%pS", (void *)l->addr); | |
5935 | else | |
5936 | seq_puts(seq, "<not-available>"); | |
5937 | ||
5938 | if (l->sum_time != l->min_time) { | |
5939 | seq_printf(seq, " age=%ld/%llu/%ld", | |
5940 | l->min_time, div_u64(l->sum_time, l->count), | |
5941 | l->max_time); | |
5942 | } else | |
5943 | seq_printf(seq, " age=%ld", l->min_time); | |
5944 | ||
5945 | if (l->min_pid != l->max_pid) | |
5946 | seq_printf(seq, " pid=%ld-%ld", l->min_pid, l->max_pid); | |
5947 | else | |
5948 | seq_printf(seq, " pid=%ld", | |
5949 | l->min_pid); | |
5950 | ||
5951 | if (num_online_cpus() > 1 && !cpumask_empty(to_cpumask(l->cpus))) | |
5952 | seq_printf(seq, " cpus=%*pbl", | |
5953 | cpumask_pr_args(to_cpumask(l->cpus))); | |
5954 | ||
5955 | if (nr_online_nodes > 1 && !nodes_empty(l->nodes)) | |
5956 | seq_printf(seq, " nodes=%*pbl", | |
5957 | nodemask_pr_args(&l->nodes)); | |
5958 | ||
5959 | seq_puts(seq, "\n"); | |
5960 | } | |
5961 | ||
5962 | if (!idx && !t->count) | |
5963 | seq_puts(seq, "No data\n"); | |
5964 | ||
5965 | return 0; | |
5966 | } | |
5967 | ||
5968 | static void slab_debugfs_stop(struct seq_file *seq, void *v) | |
5969 | { | |
5970 | } | |
5971 | ||
5972 | static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos) | |
5973 | { | |
5974 | struct loc_track *t = seq->private; | |
5975 | ||
5976 | v = ppos; | |
5977 | ++*ppos; | |
5978 | if (*ppos <= t->count) | |
5979 | return v; | |
5980 | ||
5981 | return NULL; | |
5982 | } | |
5983 | ||
5984 | static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos) | |
5985 | { | |
5986 | return ppos; | |
5987 | } | |
5988 | ||
5989 | static const struct seq_operations slab_debugfs_sops = { | |
5990 | .start = slab_debugfs_start, | |
5991 | .next = slab_debugfs_next, | |
5992 | .stop = slab_debugfs_stop, | |
5993 | .show = slab_debugfs_show, | |
5994 | }; | |
5995 | ||
5996 | static int slab_debug_trace_open(struct inode *inode, struct file *filep) | |
5997 | { | |
5998 | ||
5999 | struct kmem_cache_node *n; | |
6000 | enum track_item alloc; | |
6001 | int node; | |
6002 | struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops, | |
6003 | sizeof(struct loc_track)); | |
6004 | struct kmem_cache *s = file_inode(filep)->i_private; | |
b3fd64e1 VB |
6005 | unsigned long *obj_map; |
6006 | ||
6007 | obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL); | |
6008 | if (!obj_map) | |
6009 | return -ENOMEM; | |
64dd6849 FM |
6010 | |
6011 | if (strcmp(filep->f_path.dentry->d_name.name, "alloc_traces") == 0) | |
6012 | alloc = TRACK_ALLOC; | |
6013 | else | |
6014 | alloc = TRACK_FREE; | |
6015 | ||
b3fd64e1 VB |
6016 | if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL)) { |
6017 | bitmap_free(obj_map); | |
64dd6849 | 6018 | return -ENOMEM; |
b3fd64e1 | 6019 | } |
64dd6849 | 6020 | |
64dd6849 FM |
6021 | for_each_kmem_cache_node(s, node, n) { |
6022 | unsigned long flags; | |
6023 | struct page *page; | |
6024 | ||
6025 | if (!atomic_long_read(&n->nr_slabs)) | |
6026 | continue; | |
6027 | ||
6028 | spin_lock_irqsave(&n->list_lock, flags); | |
6029 | list_for_each_entry(page, &n->partial, slab_list) | |
b3fd64e1 | 6030 | process_slab(t, s, page, alloc, obj_map); |
64dd6849 | 6031 | list_for_each_entry(page, &n->full, slab_list) |
b3fd64e1 | 6032 | process_slab(t, s, page, alloc, obj_map); |
64dd6849 FM |
6033 | spin_unlock_irqrestore(&n->list_lock, flags); |
6034 | } | |
6035 | ||
b3fd64e1 | 6036 | bitmap_free(obj_map); |
64dd6849 FM |
6037 | return 0; |
6038 | } | |
6039 | ||
6040 | static int slab_debug_trace_release(struct inode *inode, struct file *file) | |
6041 | { | |
6042 | struct seq_file *seq = file->private_data; | |
6043 | struct loc_track *t = seq->private; | |
6044 | ||
6045 | free_loc_track(t); | |
6046 | return seq_release_private(inode, file); | |
6047 | } | |
6048 | ||
6049 | static const struct file_operations slab_debugfs_fops = { | |
6050 | .open = slab_debug_trace_open, | |
6051 | .read = seq_read, | |
6052 | .llseek = seq_lseek, | |
6053 | .release = slab_debug_trace_release, | |
6054 | }; | |
6055 | ||
6056 | static void debugfs_slab_add(struct kmem_cache *s) | |
6057 | { | |
6058 | struct dentry *slab_cache_dir; | |
6059 | ||
6060 | if (unlikely(!slab_debugfs_root)) | |
6061 | return; | |
6062 | ||
6063 | slab_cache_dir = debugfs_create_dir(s->name, slab_debugfs_root); | |
6064 | ||
6065 | debugfs_create_file("alloc_traces", 0400, | |
6066 | slab_cache_dir, s, &slab_debugfs_fops); | |
6067 | ||
6068 | debugfs_create_file("free_traces", 0400, | |
6069 | slab_cache_dir, s, &slab_debugfs_fops); | |
6070 | } | |
6071 | ||
6072 | void debugfs_slab_release(struct kmem_cache *s) | |
6073 | { | |
6074 | debugfs_remove_recursive(debugfs_lookup(s->name, slab_debugfs_root)); | |
6075 | } | |
6076 | ||
6077 | static int __init slab_debugfs_init(void) | |
6078 | { | |
6079 | struct kmem_cache *s; | |
6080 | ||
6081 | slab_debugfs_root = debugfs_create_dir("slab", NULL); | |
6082 | ||
6083 | list_for_each_entry(s, &slab_caches, list) | |
6084 | if (s->flags & SLAB_STORE_USER) | |
6085 | debugfs_slab_add(s); | |
6086 | ||
6087 | return 0; | |
6088 | ||
6089 | } | |
6090 | __initcall(slab_debugfs_init); | |
6091 | #endif | |
57ed3eda PE |
6092 | /* |
6093 | * The /proc/slabinfo ABI | |
6094 | */ | |
5b365771 | 6095 | #ifdef CONFIG_SLUB_DEBUG |
0d7561c6 | 6096 | void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo) |
57ed3eda | 6097 | { |
57ed3eda | 6098 | unsigned long nr_slabs = 0; |
205ab99d CL |
6099 | unsigned long nr_objs = 0; |
6100 | unsigned long nr_free = 0; | |
57ed3eda | 6101 | int node; |
fa45dc25 | 6102 | struct kmem_cache_node *n; |
57ed3eda | 6103 | |
fa45dc25 | 6104 | for_each_kmem_cache_node(s, node, n) { |
c17fd13e WL |
6105 | nr_slabs += node_nr_slabs(n); |
6106 | nr_objs += node_nr_objs(n); | |
205ab99d | 6107 | nr_free += count_partial(n, count_free); |
57ed3eda PE |
6108 | } |
6109 | ||
0d7561c6 GC |
6110 | sinfo->active_objs = nr_objs - nr_free; |
6111 | sinfo->num_objs = nr_objs; | |
6112 | sinfo->active_slabs = nr_slabs; | |
6113 | sinfo->num_slabs = nr_slabs; | |
6114 | sinfo->objects_per_slab = oo_objects(s->oo); | |
6115 | sinfo->cache_order = oo_order(s->oo); | |
57ed3eda PE |
6116 | } |
6117 | ||
0d7561c6 | 6118 | void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s) |
7b3c3a50 | 6119 | { |
7b3c3a50 AD |
6120 | } |
6121 | ||
b7454ad3 GC |
6122 | ssize_t slabinfo_write(struct file *file, const char __user *buffer, |
6123 | size_t count, loff_t *ppos) | |
7b3c3a50 | 6124 | { |
b7454ad3 | 6125 | return -EIO; |
7b3c3a50 | 6126 | } |
5b365771 | 6127 | #endif /* CONFIG_SLUB_DEBUG */ |