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