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