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