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