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