2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
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.
8 * (C) 2007 SGI, Christoph Lameter
9 * (C) 2011 Linux Foundation, Christoph Lameter
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>
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>
38 #include <linux/security.h>
40 spinlock_t ro_pages_lock
= __SPIN_LOCK_UNLOCKED();
42 #define check_cred_cache(s,r) \
44 if ((s->name) && !strcmp(s->name,"cred_jar_ro")) \
48 #define check_cred_cache(s,r)
49 #endif /* CONFIG_RKP_KDP */
51 #ifdef CONFIG_SLUB_DEBUG
52 #define SLUB_BUG() BUG()
55 #include <trace/events/kmem.h>
61 * 1. slab_mutex (Global Mutex)
63 * 3. slab_lock(page) (Only on some arches and for debugging)
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.
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
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.
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).
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
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.
99 * SLUB assigns one slab for allocation to each processor.
100 * Allocations only occur from these slabs called cpu slabs.
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.
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.
112 * Overloading of page flags that are otherwise used for LRU management.
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.
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.
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.
135 static inline int kmem_cache_debug(struct kmem_cache
*s
)
137 #ifdef CONFIG_SLUB_DEBUG
138 return unlikely(s
->flags
& SLAB_DEBUG_FLAGS
);
145 * Issues still to be resolved:
147 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
149 * - Variable sizing of the per node arrays
152 /* Enable to test recovery from slab corruption on boot */
153 #undef SLUB_RESILIENCY_TEST
155 /* Enable to log cmpxchg failures */
156 #undef SLUB_DEBUG_CMPXCHG
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.
162 #define MIN_PARTIAL 5
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.
169 #define MAX_PARTIAL 10
171 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
172 SLAB_POISON | SLAB_STORE_USER)
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
179 #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
182 * Set of flags that will prevent slab merging
184 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
185 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
188 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
189 SLAB_CACHE_DMA | SLAB_NOTRACK)
192 #define OO_MASK ((1 << OO_SHIFT) - 1)
193 #define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
195 /* Internal SLUB flags */
196 #define __OBJECT_POISON 0x80000000UL /* Poison object */
197 #define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
200 static struct notifier_block slab_notifier
;
204 * Tracking user of a slab.
206 #define TRACK_ADDRS_COUNT 16
208 unsigned long addr
; /* Called from address */
209 #ifdef CONFIG_STACKTRACE
210 unsigned long addrs
[TRACK_ADDRS_COUNT
]; /* Called from address */
212 int cpu
; /* Was running on cpu */
213 int pid
; /* Pid context */
214 unsigned long when
; /* When did the operation occur */
217 enum track_item
{ TRACK_ALLOC
, TRACK_FREE
};
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
);
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
)
228 static inline void sysfs_slab_remove(struct kmem_cache
*s
) { }
230 static inline void memcg_propagate_slab_attrs(struct kmem_cache
*s
) { }
233 static inline void stat(const struct kmem_cache
*s
, enum stat_item si
)
235 #ifdef CONFIG_SLUB_STATS
236 __this_cpu_inc(s
->cpu_slab
->stat
[si
]);
240 /********************************************************************
241 * Core slab cache functions
242 *******************************************************************/
244 static inline struct kmem_cache_node
*get_node(struct kmem_cache
*s
, int node
)
246 return s
->node
[node
];
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
)
258 base
= page_address(page
);
259 if (object
< base
|| object
>= base
+ page
->objects
* s
->size
||
260 (object
- base
) % s
->size
) {
267 static inline void *get_freepointer(struct kmem_cache
*s
, void *object
)
269 return *(void **)(object
+ s
->offset
);
272 static void prefetch_freepointer(const struct kmem_cache
*s
, void *object
)
274 prefetch(object
+ s
->offset
);
277 static inline void *get_freepointer_safe(struct kmem_cache
*s
, void *object
)
281 #ifdef CONFIG_DEBUG_PAGEALLOC
282 probe_kernel_read(&p
, (void **)(object
+ s
->offset
), sizeof(p
));
284 p
= get_freepointer(s
, object
);
289 static inline void set_freepointer(struct kmem_cache
*s
, void *object
, void *fp
)
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);
297 #endif /*CONFIG_RKP_KDP*/
298 *(void **)(object
+ s
->offset
) = fp
;
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;\
306 /* Determine object index from a given position */
307 static inline int slab_index(void *p
, struct kmem_cache
*s
, void *addr
)
309 return (p
- addr
) / s
->size
;
312 static inline size_t slab_ksize(const struct kmem_cache
*s
)
314 #ifdef CONFIG_SLUB_DEBUG
316 * Debugging requires use of the padding between object
317 * and whatever may come after it.
319 if (s
->flags
& (SLAB_RED_ZONE
| SLAB_POISON
))
320 return s
->object_size
;
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.
328 if (s
->flags
& (SLAB_DESTROY_BY_RCU
| SLAB_STORE_USER
))
331 * Else we can use all the padding etc for the allocation
336 static inline int order_objects(int order
, unsigned long size
, int reserved
)
338 return ((PAGE_SIZE
<< order
) - reserved
) / size
;
341 static inline struct kmem_cache_order_objects
oo_make(int order
,
342 unsigned long size
, int reserved
)
344 struct kmem_cache_order_objects x
= {
345 (order
<< OO_SHIFT
) + order_objects(order
, size
, reserved
)
351 static inline int oo_order(struct kmem_cache_order_objects x
)
353 return x
.x
>> OO_SHIFT
;
356 static inline int oo_objects(struct kmem_cache_order_objects x
)
358 return x
.x
& OO_MASK
;
362 * Per slab locking using the pagelock
364 static __always_inline
void slab_lock(struct page
*page
)
366 bit_spin_lock(PG_locked
, &page
->flags
);
369 static __always_inline
void slab_unlock(struct page
*page
)
371 __bit_spin_unlock(PG_locked
, &page
->flags
);
374 static inline void set_page_slub_counters(struct page
*page
, unsigned long counters_new
)
377 tmp
.counters
= counters_new
;
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.
384 page
->frozen
= tmp
.frozen
;
385 page
->inuse
= tmp
.inuse
;
386 page
->objects
= tmp
.objects
;
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
,
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
))
407 if (page
->freelist
== freelist_old
&& page
->counters
== counters_old
) {
408 page
->freelist
= freelist_new
;
409 set_page_slub_counters(page
, counters_new
);
417 stat(s
, CMPXCHG_DOUBLE_FAIL
);
419 #ifdef SLUB_DEBUG_CMPXCHG
420 printk(KERN_INFO
"%s %s: cmpxchg double redo ", n
, s
->name
);
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
,
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
))
443 local_irq_save(flags
);
445 if (page
->freelist
== freelist_old
&& page
->counters
== counters_old
) {
446 page
->freelist
= freelist_new
;
447 set_page_slub_counters(page
, counters_new
);
449 local_irq_restore(flags
);
453 local_irq_restore(flags
);
457 stat(s
, CMPXCHG_DOUBLE_FAIL
);
459 #ifdef SLUB_DEBUG_CMPXCHG
460 printk(KERN_INFO
"%s %s: cmpxchg double redo ", n
, s
->name
);
466 #ifdef CONFIG_SLUB_DEBUG
468 * Determine a map of object in use on a page.
470 * Node listlock must be held to guarantee that the page does
471 * not vanish from under us.
473 static void get_map(struct kmem_cache
*s
, struct page
*page
, unsigned long *map
)
476 void *addr
= page_address(page
);
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
);
488 #ifdef CONFIG_SLUB_DEBUG_ON
489 static int slub_debug
= DEBUG_DEFAULT_FLAGS
;
491 static int slub_debug
;
494 static char *slub_debug_slabs
;
495 static int disable_higher_order_debug
;
500 static void print_section(char *text
, u8
*addr
, unsigned int length
)
502 print_hex_dump(KERN_ERR
, text
, DUMP_PREFIX_ADDRESS
, 16, 1, addr
,
506 static struct track
*get_track(struct kmem_cache
*s
, void *object
,
507 enum track_item alloc
)
512 p
= object
+ s
->offset
+ sizeof(void *);
514 p
= object
+ s
->inuse
;
519 static void set_track(struct kmem_cache
*s
, void *object
,
520 enum track_item alloc
, unsigned long addr
)
522 struct track
*p
= get_track(s
, object
, alloc
);
524 #ifdef CONFIG_RKP_KDP
525 check_cred_cache(s
, );
526 #endif /* CONFIG_RKP_KDP */
528 #ifdef CONFIG_STACKTRACE
529 struct stack_trace trace
;
532 trace
.nr_entries
= 0;
533 trace
.max_entries
= TRACK_ADDRS_COUNT
;
534 trace
.entries
= p
->addrs
;
536 save_stack_trace(&trace
);
538 /* See rant in lockdep.c */
539 if (trace
.nr_entries
!= 0 &&
540 trace
.entries
[trace
.nr_entries
- 1] == ULONG_MAX
)
543 for (i
= trace
.nr_entries
; i
< TRACK_ADDRS_COUNT
; i
++)
547 p
->cpu
= smp_processor_id();
548 p
->pid
= current
->pid
;
551 memset(p
, 0, sizeof(struct track
));
554 static void init_tracking(struct kmem_cache
*s
, void *object
)
556 if (!(s
->flags
& SLAB_STORE_USER
))
559 set_track(s
, object
, TRACK_FREE
, 0UL);
560 set_track(s
, object
, TRACK_ALLOC
, 0UL);
563 static void print_track(const char *s
, struct track
*t
)
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
573 for (i
= 0; i
< TRACK_ADDRS_COUNT
; i
++)
575 printk(KERN_ERR
"\t%pS\n", (void *)t
->addrs
[i
]);
582 static void print_tracking(struct kmem_cache
*s
, void *object
)
584 if (!(s
->flags
& SLAB_STORE_USER
))
587 print_track("Allocated", get_track(s
, object
, TRACK_ALLOC
));
588 print_track("Freed", get_track(s
, object
, TRACK_FREE
));
591 static void print_page_info(struct page
*page
)
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
);
598 static void slab_bug(struct kmem_cache
*s
, char *fmt
, ...)
604 vsnprintf(buf
, sizeof(buf
), fmt
, 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");
612 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
615 static void slab_fix(struct kmem_cache
*s
, char *fmt
, ...)
621 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
623 printk(KERN_ERR
"FIX %s: %s\n", s
->name
, buf
);
626 static void print_trailer(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
628 unsigned int off
; /* Offset of last byte */
629 u8
*addr
= page_address(page
);
631 print_tracking(s
, p
);
633 print_page_info(page
);
635 printk(KERN_ERR
"INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
636 p
, p
- addr
, get_freepointer(s
, p
));
639 print_section("Bytes b4 ", p
- 16, 16);
641 print_section("Object ", p
, min_t(unsigned long, s
->object_size
,
643 if (s
->flags
& SLAB_RED_ZONE
)
644 print_section("Redzone ", p
+ s
->object_size
,
645 s
->inuse
- s
->object_size
);
648 off
= s
->offset
+ sizeof(void *);
652 if (s
->flags
& SLAB_STORE_USER
)
653 off
+= 2 * sizeof(struct track
);
656 /* Beginning of the filler is the free pointer */
657 print_section("Padding ", p
+ off
, s
->size
- off
);
662 static void object_err(struct kmem_cache
*s
, struct page
*page
,
663 u8
*object
, char *reason
)
665 slab_bug(s
, "%s", reason
);
666 print_trailer(s
, page
, object
);
669 static void slab_err(struct kmem_cache
*s
, struct page
*page
, const char *fmt
, ...)
675 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
677 slab_bug(s
, "%s", buf
);
678 print_page_info(page
);
682 static void init_object(struct kmem_cache
*s
, void *object
, u8 val
)
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
;
694 if (s
->flags
& SLAB_RED_ZONE
)
695 memset(p
+ s
->object_size
, val
, s
->inuse
- s
->object_size
);
698 static void restore_bytes(struct kmem_cache
*s
, char *message
, u8 data
,
699 void *from
, void *to
)
701 slab_fix(s
, "Restoring 0x%p-0x%p=0x%x\n", from
, to
- 1, data
);
702 memset(from
, data
, to
- from
);
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
)
712 #ifdef CONFIG_RKP_KDP
713 check_cred_cache(s
,1);
714 #endif /* CONFIG_RKP_KDP */
715 fault
= memchr_inv(start
, value
, bytes
);
720 while (end
> fault
&& end
[-1] == value
)
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
);
728 restore_bytes(s
, what
, value
, fault
, end
);
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.
740 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
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.
748 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
749 * 0xcc (RED_ACTIVE) for objects in use.
752 * Meta data starts here.
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.
760 * Padding is done using 0x5a (POISON_INUSE)
763 * Nothing is used beyond s->size.
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.
770 static int check_pad_bytes(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
772 unsigned long off
= s
->inuse
; /* The end of info */
775 /* Freepointer is placed after the object. */
776 off
+= sizeof(void *);
778 if (s
->flags
& SLAB_STORE_USER
)
779 /* We also have user information there */
780 off
+= 2 * sizeof(struct track
);
785 return check_bytes_and_report(s
, page
, p
, "Object padding",
786 p
+ off
, POISON_INUSE
, s
->size
- off
);
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
)
798 if (!(s
->flags
& SLAB_POISON
))
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
;
811 fault
= memchr_inv(end
- remainder
, POISON_INUSE
, remainder
);
814 while (end
> fault
&& end
[-1] == POISON_INUSE
)
817 slab_err(s
, page
, "Padding overwritten. 0x%p-0x%p", fault
, end
- 1);
818 print_section("Padding ", end
- remainder
, remainder
);
820 restore_bytes(s
, "slab padding", POISON_INUSE
, end
- remainder
, end
);
824 static int check_object(struct kmem_cache
*s
, struct page
*page
,
825 void *object
, u8 val
)
828 u8
*endobject
= object
+ s
->object_size
;
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
))
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
);
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)))
849 * check_pad_bytes cleans up on its own.
851 check_pad_bytes(s
, page
, p
);
854 if (!s
->offset
&& val
== SLUB_RED_ACTIVE
)
856 * Object and freepointer overlap. Cannot check
857 * freepointer while object is allocated.
861 /* Check free pointer validity */
862 if (!check_valid_pointer(s
, page
, get_freepointer(s
, p
))) {
863 object_err(s
, page
, p
, "Freepointer corrupt");
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.
869 set_freepointer(s
, p
, NULL
);
875 static int check_slab(struct kmem_cache
*s
, struct page
*page
)
879 VM_BUG_ON(!irqs_disabled());
881 if (!PageSlab(page
)) {
882 slab_err(s
, page
, "Not a valid slab page");
885 #ifdef CONFIG_RKP_KDP
887 * Skip this function for now
889 if (s
->name
&& !strcmp(s
->name
, "cred_jar_ro"))
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
);
898 if (page
->inuse
> page
->objects
) {
899 slab_err(s
, page
, "inuse %u > max %u",
900 s
->name
, page
->inuse
, page
->objects
);
903 /* Slab_pad_check fixes things up after itself */
904 slab_pad_check(s
, page
);
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.
912 static int on_freelist(struct kmem_cache
*s
, struct page
*page
, void *search
)
917 unsigned long max_objects
;
920 #ifdef CONFIG_RKP_KDP
921 check_cred_cache(s
,0);
922 #endif /* CONFIG_RKP_KDP */
923 while (fp
&& nr
<= page
->objects
) {
926 if (!check_valid_pointer(s
, page
, fp
)) {
928 object_err(s
, page
, object
,
929 "Freechain corrupt");
930 set_freepointer(s
, object
, NULL
);
933 slab_err(s
, page
, "Freepointer corrupt");
934 page
->freelist
= NULL
;
935 page
->inuse
= page
->objects
;
936 slab_fix(s
, "Freelist cleared");
942 fp
= get_freepointer(s
, object
);
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
;
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.");
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.");
962 return search
== NULL
;
965 static void trace(struct kmem_cache
*s
, struct page
*page
, void *object
,
968 if (s
->flags
& SLAB_TRACE
) {
969 printk(KERN_INFO
"TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
971 alloc
? "alloc" : "free",
976 print_section("Object ", (void *)object
, s
->object_size
);
983 * Hooks for other subsystems that check memory allocations. In a typical
984 * production configuration these hooks all should produce no code at all.
986 static inline int slab_pre_alloc_hook(struct kmem_cache
*s
, gfp_t flags
)
988 flags
&= gfp_allowed_mask
;
989 lockdep_trace_alloc(flags
);
990 might_sleep_if(flags
& __GFP_WAIT
);
992 return should_failslab(s
->object_size
, flags
, s
->flags
);
995 static inline void slab_post_alloc_hook(struct kmem_cache
*s
, gfp_t flags
, void *object
)
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
);
1002 static inline void slab_free_hook(struct kmem_cache
*s
, void *x
)
1004 kmemleak_free_recursive(x
, s
->flags
);
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.
1011 #if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
1013 unsigned long flags
;
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
);
1021 if (!(s
->flags
& SLAB_DEBUG_OBJECTS
))
1022 debug_check_no_obj_freed(x
, s
->object_size
);
1026 * Tracking of fully allocated slabs for debugging purposes.
1028 * list_lock must be held.
1030 static void add_full(struct kmem_cache
*s
,
1031 struct kmem_cache_node
*n
, struct page
*page
)
1033 #ifdef CONFIG_RKP_KDP
1034 check_cred_cache(s
, );
1035 #endif /* CONFIG_RKP_KDP */
1036 if (!(s
->flags
& SLAB_STORE_USER
))
1039 list_add(&page
->lru
, &n
->full
);
1043 * list_lock must be held.
1045 static void remove_full(struct kmem_cache
*s
, struct page
*page
)
1047 #ifdef CONFIG_RKP_KDP
1048 check_cred_cache(s
, );
1049 #endif /* CONFIG_RKP_KDP */
1050 if (!(s
->flags
& SLAB_STORE_USER
))
1053 list_del(&page
->lru
);
1056 /* Tracking of the number of slabs for debugging purposes */
1057 static inline unsigned long slabs_node(struct kmem_cache
*s
, int node
)
1059 struct kmem_cache_node
*n
= get_node(s
, node
);
1061 return atomic_long_read(&n
->nr_slabs
);
1064 static inline unsigned long node_nr_slabs(struct kmem_cache_node
*n
)
1066 return atomic_long_read(&n
->nr_slabs
);
1069 static inline void inc_slabs_node(struct kmem_cache
*s
, int node
, int objects
)
1071 struct kmem_cache_node
*n
= get_node(s
, node
);
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).
1080 atomic_long_inc(&n
->nr_slabs
);
1081 atomic_long_add(objects
, &n
->total_objects
);
1084 static inline void dec_slabs_node(struct kmem_cache
*s
, int node
, int objects
)
1086 struct kmem_cache_node
*n
= get_node(s
, node
);
1088 atomic_long_dec(&n
->nr_slabs
);
1089 atomic_long_sub(objects
, &n
->total_objects
);
1092 /* Object debug checks for alloc/free paths */
1093 static void setup_object_debug(struct kmem_cache
*s
, struct page
*page
,
1096 if (!(s
->flags
& (SLAB_STORE_USER
|SLAB_RED_ZONE
|__OBJECT_POISON
)))
1099 init_object(s
, object
, SLUB_RED_INACTIVE
);
1100 init_tracking(s
, object
);
1103 static noinline
int alloc_debug_processing(struct kmem_cache
*s
, struct page
*page
,
1104 void *object
, unsigned long addr
)
1106 #ifdef CONFIG_RKP_KDP
1107 check_cred_cache(s
,0);
1108 #endif /* CONFIG_RKP_KDP */
1109 if (!check_slab(s
, page
))
1112 if (!check_valid_pointer(s
, page
, object
)) {
1113 object_err(s
, page
, object
, "Freelist Pointer check fails");
1117 if (!check_object(s
, page
, object
, SLUB_RED_INACTIVE
))
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
);
1128 if (PageSlab(page
)) {
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.
1134 slab_fix(s
, "Marking all objects used");
1135 page
->inuse
= page
->objects
;
1136 page
->freelist
= NULL
;
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
)
1145 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
1147 #ifdef CONFIG_RKP_KDP
1148 check_cred_cache(s
,NULL
);
1149 #endif /* CONFIG_RKP_KDP */
1150 spin_lock_irqsave(&n
->list_lock
, *flags
);
1153 if (!check_slab(s
, page
))
1156 if (!check_valid_pointer(s
, page
, object
)) {
1157 slab_err(s
, page
, "Invalid object pointer 0x%p", object
);
1161 if (on_freelist(s
, page
, object
)) {
1162 object_err(s
, page
, object
, "Object already free");
1166 if (!check_object(s
, page
, object
, SLUB_RED_ACTIVE
))
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
) {
1175 "SLUB <none>: no slab for object 0x%p.\n",
1179 object_err(s
, page
, object
,
1180 "page slab pointer corrupt.");
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
);
1191 * Keep node_lock to preserve integrity
1192 * until the object is actually freed
1198 spin_unlock_irqrestore(&n
->list_lock
, *flags
);
1199 slab_fix(s
, "Object at 0x%p not freed", object
);
1203 static int __init
setup_slub_debug(char *str
)
1205 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1206 if (*str
++ != '=' || !*str
)
1208 * No options specified. Switch on full debugging.
1214 * No options but restriction on slabs. This means full
1215 * debugging for slabs matching a pattern.
1219 if (tolower(*str
) == 'o') {
1221 * Avoid enabling debugging on caches if its minimum order
1222 * would increase as a result.
1224 disable_higher_order_debug
= 1;
1231 * Switch off all debugging measures.
1236 * Determine which debug features should be switched on
1238 for (; *str
&& *str
!= ','; str
++) {
1239 switch (tolower(*str
)) {
1241 slub_debug
|= SLAB_DEBUG_FREE
;
1244 slub_debug
|= SLAB_RED_ZONE
;
1247 slub_debug
|= SLAB_POISON
;
1250 slub_debug
|= SLAB_STORE_USER
;
1253 slub_debug
|= SLAB_TRACE
;
1256 slub_debug
|= SLAB_FAILSLAB
;
1259 printk(KERN_ERR
"slub_debug option '%c' "
1260 "unknown. skipped\n", *str
);
1266 slub_debug_slabs
= str
+ 1;
1271 __setup("slub_debug", setup_slub_debug
);
1273 static unsigned long kmem_cache_flags(unsigned long object_size
,
1274 unsigned long flags
, const char *name
,
1275 void (*ctor
)(void *))
1278 * Enable debugging if selected on the kernel commandline.
1280 #ifdef CONFIG_RKP_KDP
1283 if (slub_debug
&& (!slub_debug_slabs
|| (name
&&
1284 !strncmp(slub_debug_slabs
, name
, strlen(slub_debug_slabs
)))))
1285 flags
|= slub_debug
;
1291 static inline void setup_object_debug(struct kmem_cache
*s
,
1292 struct page
*page
, void *object
) {}
1294 static inline int alloc_debug_processing(struct kmem_cache
*s
,
1295 struct page
*page
, void *object
, unsigned long addr
) { return 0; }
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
; }
1301 static inline int slab_pad_check(struct kmem_cache
*s
, struct page
*page
)
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 *))
1314 #define slub_debug 0
1316 #define disable_higher_order_debug 0
1318 static inline unsigned long slabs_node(struct kmem_cache
*s
, int node
)
1320 static inline unsigned long node_nr_slabs(struct kmem_cache_node
*n
)
1322 static inline void inc_slabs_node(struct kmem_cache
*s
, int node
,
1324 static inline void dec_slabs_node(struct kmem_cache
*s
, int node
,
1327 static inline int slab_pre_alloc_hook(struct kmem_cache
*s
, gfp_t flags
)
1330 static inline void slab_post_alloc_hook(struct kmem_cache
*s
, gfp_t flags
,
1333 static inline void slab_free_hook(struct kmem_cache
*s
, void *x
) {}
1335 #endif /* CONFIG_SLUB_DEBUG */
1338 * Slab allocation and freeing
1340 static inline struct page
*alloc_slab_page(gfp_t flags
, int node
,
1341 struct kmem_cache_order_objects oo
)
1343 int order
= oo_order(oo
);
1345 flags
|= __GFP_NOTRACK
;
1347 if (node
== NUMA_NO_NODE
)
1348 return alloc_pages(flags
, order
);
1350 return alloc_pages_exact_node(node
, flags
, order
);
1353 static struct page
*allocate_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
1356 struct kmem_cache_order_objects oo
= s
->oo
;
1357 #ifdef CONFIG_RKP_KDP
1358 void *virt_page
= NULL
;
1359 #endif /*CONFIG_RKP_KDP*/
1362 flags
&= gfp_allowed_mask
;
1364 if (flags
& __GFP_WAIT
)
1367 flags
|= s
->allocflags
;
1370 * Let the initial higher-order allocation fail under memory pressure
1371 * so we fall-back to the minimum order allocation.
1373 alloc_gfp
= (flags
| __GFP_NOWARN
| __GFP_NORETRY
) & ~__GFP_NOFAIL
;
1375 #ifdef CONFIG_RKP_KDP
1376 if (s
->name
&& !strcmp(s
->name
, "cred_jar_ro")) {
1378 virt_page
= rkp_ro_alloc();
1382 page
= virt_to_page(virt_page
);
1386 #endif /*CONFIG_RKP_KDP*/
1387 page
= alloc_slab_page(alloc_gfp
, node
, oo
);
1388 if (unlikely(!page
)) {
1391 * Allocation may have failed due to fragmentation.
1392 * Try a lower order alloc if possible
1394 page
= alloc_slab_page(flags
, node
, oo
);
1397 stat(s
, ORDER_FALLBACK
);
1400 #ifdef CONFIG_RKP_KDP
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
);
1407 kmemcheck_alloc_shadow(page
, oo_order(oo
), flags
, node
);
1410 * Objects from caches that have a constructor don't get
1411 * cleared when they're allocated, so we need to do it here.
1414 kmemcheck_mark_uninitialized_pages(page
, pages
);
1416 kmemcheck_mark_unallocated_pages(page
, pages
);
1419 if (flags
& __GFP_WAIT
)
1420 local_irq_disable();
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
,
1429 #ifdef CONFIG_RKP_KDP
1431 * We modify the following so that slab alloc for protected data
1432 * types are allocated from our own pool.
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
));
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
;
1448 static void setup_object(struct kmem_cache
*s
, struct page
*page
,
1451 setup_object_debug(s
, page
, object
);
1452 if (unlikely(s
->ctor
))
1455 #ifdef CONFIG_RKP_DMAP_PROT
1456 extern u8 rkp_started
;
1458 static struct page
*new_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
1466 BUG_ON(flags
& GFP_SLAB_BUG_MASK
);
1468 page
= allocate_slab(s
,
1469 flags
& (GFP_RECLAIM_MASK
| GFP_CONSTRAINT_MASK
), node
);
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
);
1481 start
= page_address(page
);
1483 if (unlikely(s
->flags
& SLAB_POISON
))
1484 memset(start
, POISON_INUSE
, PAGE_SIZE
<< order
);
1487 for_each_object(p
, s
, start
, page
->objects
) {
1488 setup_object(s
, page
, last
);
1489 set_freepointer(s
, last
, p
);
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);
1500 page
->freelist
= start
;
1501 page
->inuse
= page
->objects
;
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
)
1510 unsigned long flags
;
1511 unsigned long long sc
,va_page
;
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
);
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
;
1526 __free_memcg_kmem_pages(page
, order
);
1527 spin_unlock_irqrestore(&ro_pages_lock
,flags
);
1529 #endif /*CONFIG_RKP_KDP*/
1530 static void __free_slab(struct kmem_cache
*s
, struct page
*page
)
1532 int order
= compound_order(page
);
1533 int pages
= 1 << order
;
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);
1542 if (kmem_cache_debug(s
)) {
1545 slab_pad_check(s
, page
);
1546 for_each_object(p
, s
, page_address(page
),
1548 check_object(s
, page
, p
, SLUB_RED_INACTIVE
);
1551 kmemcheck_free_shadow(page
, compound_order(page
));
1553 mod_zone_page_state(page_zone(page
),
1554 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
1555 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
1558 __ClearPageSlabPfmemalloc(page
);
1559 __ClearPageSlab(page
);
1561 memcg_release_pages(s
, order
);
1562 page_mapcount_reset(page
);
1563 if (current
->reclaim_state
)
1564 current
->reclaim_state
->reclaimed_slab
+= pages
;
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
);
1572 __free_memcg_kmem_pages(page
, order
);
1575 #define need_reserve_slab_rcu \
1576 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1578 static void rcu_free_slab(struct rcu_head
*h
)
1582 if (need_reserve_slab_rcu
)
1583 page
= virt_to_head_page(h
);
1585 page
= container_of((struct list_head
*)h
, struct page
, lru
);
1587 __free_slab(page
->slab_cache
, page
);
1590 static void free_slab(struct kmem_cache
*s
, struct page
*page
)
1592 if (unlikely(s
->flags
& SLAB_DESTROY_BY_RCU
)) {
1593 struct rcu_head
*head
;
1595 if (need_reserve_slab_rcu
) {
1596 int order
= compound_order(page
);
1597 int offset
= (PAGE_SIZE
<< order
) - s
->reserved
;
1599 VM_BUG_ON(s
->reserved
!= sizeof(*head
));
1600 head
= page_address(page
) + offset
;
1603 * RCU free overloads the RCU head over the LRU
1605 head
= (void *)&page
->lru
;
1608 call_rcu(head
, rcu_free_slab
);
1610 __free_slab(s
, page
);
1613 static void discard_slab(struct kmem_cache
*s
, struct page
*page
)
1615 dec_slabs_node(s
, page_to_nid(page
), page
->objects
);
1620 * Management of partially allocated slabs.
1622 * list_lock must be held.
1624 static inline void add_partial(struct kmem_cache_node
*n
,
1625 struct page
*page
, int tail
)
1628 if (tail
== DEACTIVATE_TO_TAIL
)
1629 list_add_tail(&page
->lru
, &n
->partial
);
1631 list_add(&page
->lru
, &n
->partial
);
1635 * list_lock must be held.
1637 static inline void remove_partial(struct kmem_cache_node
*n
,
1640 list_del(&page
->lru
);
1645 * Remove slab from the partial list, freeze it and
1646 * return the pointer to the freelist.
1648 * Returns a list of objects or NULL if it fails.
1650 * Must hold list_lock since we modify the partial list.
1652 static inline void *acquire_slab(struct kmem_cache
*s
,
1653 struct kmem_cache_node
*n
, struct page
*page
,
1654 int mode
, int *objects
)
1657 unsigned long counters
;
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.
1665 freelist
= page
->freelist
;
1666 counters
= page
->counters
;
1667 new.counters
= counters
;
1668 *objects
= new.objects
- new.inuse
;
1670 new.inuse
= page
->objects
;
1671 new.freelist
= NULL
;
1673 new.freelist
= freelist
;
1676 VM_BUG_ON(new.frozen
);
1679 if (!__cmpxchg_double_slab(s
, page
,
1681 new.freelist
, new.counters
,
1685 remove_partial(n
, page
);
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
);
1694 * Try to allocate a partial slab from a specific node.
1696 static void *get_partial_node(struct kmem_cache
*s
, struct kmem_cache_node
*n
,
1697 struct kmem_cache_cpu
*c
, gfp_t flags
)
1699 struct page
*page
, *page2
;
1700 void *object
= NULL
;
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()
1710 if (!n
|| !n
->nr_partial
)
1713 spin_lock(&n
->list_lock
);
1714 list_for_each_entry_safe(page
, page2
, &n
->partial
, lru
) {
1717 if (!pfmemalloc_match(page
, flags
))
1720 t
= acquire_slab(s
, n
, page
, object
== NULL
, &objects
);
1724 available
+= objects
;
1727 stat(s
, ALLOC_FROM_PARTIAL
);
1730 put_cpu_partial(s
, page
, 0);
1731 stat(s
, CPU_PARTIAL_NODE
);
1733 if (kmem_cache_debug(s
) || available
> s
->cpu_partial
/ 2)
1737 spin_unlock(&n
->list_lock
);
1742 * Get a page from somewhere. Search in increasing NUMA distances.
1744 static void *get_any_partial(struct kmem_cache
*s
, gfp_t flags
,
1745 struct kmem_cache_cpu
*c
)
1748 struct zonelist
*zonelist
;
1751 enum zone_type high_zoneidx
= gfp_zone(flags
);
1753 unsigned int cpuset_mems_cookie
;
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.
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.
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.
1773 if (!s
->remote_node_defrag_ratio
||
1774 get_cycles() % 1024 > s
->remote_node_defrag_ratio
)
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
;
1783 n
= get_node(s
, zone_to_nid(zone
));
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
);
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.
1797 put_mems_allowed(cpuset_mems_cookie
);
1802 } while (!put_mems_allowed(cpuset_mems_cookie
));
1808 * Get a partial page, lock it and return it.
1810 static void *get_partial(struct kmem_cache
*s
, gfp_t flags
, int node
,
1811 struct kmem_cache_cpu
*c
)
1814 int searchnode
= (node
== NUMA_NO_NODE
) ? numa_node_id() : node
;
1816 object
= get_partial_node(s
, get_node(s
, searchnode
), c
, flags
);
1817 if (object
|| node
!= NUMA_NO_NODE
)
1820 return get_any_partial(s
, flags
, c
);
1823 #ifdef CONFIG_PREEMPT
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.
1829 #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1832 * No preemption supported therefore also no need to check for
1838 static inline unsigned long next_tid(unsigned long tid
)
1840 return tid
+ TID_STEP
;
1843 static inline unsigned int tid_to_cpu(unsigned long tid
)
1845 return tid
% TID_STEP
;
1848 static inline unsigned long tid_to_event(unsigned long tid
)
1850 return tid
/ TID_STEP
;
1853 static inline unsigned int init_tid(int cpu
)
1858 static inline void note_cmpxchg_failure(const char *n
,
1859 const struct kmem_cache
*s
, unsigned long tid
)
1861 #ifdef SLUB_DEBUG_CMPXCHG
1862 unsigned long actual_tid
= __this_cpu_read(s
->cpu_slab
->tid
);
1864 printk(KERN_INFO
"%s %s: cmpxchg redo ", n
, s
->name
);
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
));
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
));
1876 printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
1877 actual_tid
, tid
, next_tid(tid
));
1879 stat(s
, CMPXCHG_DOUBLE_CPU_FAIL
);
1882 static void init_kmem_cache_cpus(struct kmem_cache
*s
)
1886 for_each_possible_cpu(cpu
)
1887 per_cpu_ptr(s
->cpu_slab
, cpu
)->tid
= init_tid(cpu
);
1891 * Remove the cpu slab
1893 static void deactivate_slab(struct kmem_cache
*s
, struct page
*page
, void *freelist
)
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
));
1898 enum slab_modes l
= M_NONE
, m
= M_NONE
;
1900 int tail
= DEACTIVATE_TO_HEAD
;
1904 if (page
->freelist
) {
1905 stat(s
, DEACTIVATE_REMOTE_FREES
);
1906 tail
= DEACTIVATE_TO_TAIL
;
1910 * Stage one: Free all available per cpu objects back
1911 * to the page freelist while it is still frozen. Leave the
1914 * There is no need to take the list->lock because the page
1917 while (freelist
&& (nextfree
= get_freepointer(s
, freelist
))) {
1919 unsigned long counters
;
1922 prior
= page
->freelist
;
1923 counters
= page
->counters
;
1924 set_freepointer(s
, freelist
, prior
);
1925 new.counters
= counters
;
1927 VM_BUG_ON(!new.frozen
);
1929 } while (!__cmpxchg_double_slab(s
, page
,
1931 freelist
, new.counters
,
1932 "drain percpu freelist"));
1934 freelist
= nextfree
;
1938 * Stage two: Ensure that the page is unfrozen while the
1939 * list presence reflects the actual number of objects
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.
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
1953 old
.freelist
= page
->freelist
;
1954 old
.counters
= page
->counters
;
1955 VM_BUG_ON(!old
.frozen
);
1957 /* Determine target state of the slab */
1958 new.counters
= old
.counters
;
1961 set_freepointer(s
, freelist
, old
.freelist
);
1962 new.freelist
= freelist
;
1964 new.freelist
= old
.freelist
;
1968 if (!new.inuse
&& n
->nr_partial
> s
->min_partial
)
1970 else if (new.freelist
) {
1975 * Taking the spinlock removes the possiblity
1976 * that acquire_slab() will see a slab page that
1979 spin_lock(&n
->list_lock
);
1983 if (kmem_cache_debug(s
) && !lock
) {
1986 * This also ensures that the scanning of full
1987 * slabs from diagnostic functions will not see
1990 spin_lock(&n
->list_lock
);
1998 remove_partial(n
, page
);
2000 else if (l
== M_FULL
)
2002 remove_full(s
, page
);
2004 if (m
== M_PARTIAL
) {
2006 add_partial(n
, page
, tail
);
2009 } else if (m
== M_FULL
) {
2011 stat(s
, DEACTIVATE_FULL
);
2012 add_full(s
, n
, page
);
2018 if (!__cmpxchg_double_slab(s
, page
,
2019 old
.freelist
, old
.counters
,
2020 new.freelist
, new.counters
,
2025 spin_unlock(&n
->list_lock
);
2028 stat(s
, DEACTIVATE_EMPTY
);
2029 discard_slab(s
, page
);
2035 * Unfreeze all the cpu partial slabs.
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).
2041 static void unfreeze_partials(struct kmem_cache
*s
,
2042 struct kmem_cache_cpu
*c
)
2044 struct kmem_cache_node
*n
= NULL
, *n2
= NULL
;
2045 struct page
*page
, *discard_page
= NULL
;
2047 while ((page
= c
->partial
)) {
2051 c
->partial
= page
->next
;
2053 n2
= get_node(s
, page_to_nid(page
));
2056 spin_unlock(&n
->list_lock
);
2059 spin_lock(&n
->list_lock
);
2064 old
.freelist
= page
->freelist
;
2065 old
.counters
= page
->counters
;
2066 VM_BUG_ON(!old
.frozen
);
2068 new.counters
= old
.counters
;
2069 new.freelist
= old
.freelist
;
2073 } while (!__cmpxchg_double_slab(s
, page
,
2074 old
.freelist
, old
.counters
,
2075 new.freelist
, new.counters
,
2076 "unfreezing slab"));
2078 if (unlikely(!new.inuse
&& n
->nr_partial
> s
->min_partial
)) {
2079 page
->next
= discard_page
;
2080 discard_page
= page
;
2082 add_partial(n
, page
, DEACTIVATE_TO_TAIL
);
2083 stat(s
, FREE_ADD_PARTIAL
);
2088 spin_unlock(&n
->list_lock
);
2090 while (discard_page
) {
2091 page
= discard_page
;
2092 discard_page
= discard_page
->next
;
2094 stat(s
, DEACTIVATE_EMPTY
);
2095 discard_slab(s
, page
);
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.
2106 * If we did not find a slot then simply move all the partials to the
2107 * per node partial list.
2109 static void put_cpu_partial(struct kmem_cache
*s
, struct page
*page
, int drain
)
2111 struct page
*oldpage
;
2118 oldpage
= this_cpu_read(s
->cpu_slab
->partial
);
2121 pobjects
= oldpage
->pobjects
;
2122 pages
= oldpage
->pages
;
2123 if (drain
&& pobjects
> s
->cpu_partial
) {
2124 unsigned long flags
;
2126 * partial array is full. Move the existing
2127 * set to the per node partial list.
2129 local_irq_save(flags
);
2130 unfreeze_partials(s
, this_cpu_ptr(s
->cpu_slab
));
2131 local_irq_restore(flags
);
2135 stat(s
, CPU_PARTIAL_DRAIN
);
2140 pobjects
+= page
->objects
- page
->inuse
;
2142 page
->pages
= pages
;
2143 page
->pobjects
= pobjects
;
2144 page
->next
= oldpage
;
2146 } while (this_cpu_cmpxchg(s
->cpu_slab
->partial
, oldpage
, page
) != oldpage
);
2149 static inline void flush_slab(struct kmem_cache
*s
, struct kmem_cache_cpu
*c
)
2151 stat(s
, CPUSLAB_FLUSH
);
2152 deactivate_slab(s
, c
->page
, c
->freelist
);
2154 c
->tid
= next_tid(c
->tid
);
2162 * Called from IPI handler with interrupts disabled.
2164 static inline void __flush_cpu_slab(struct kmem_cache
*s
, int cpu
)
2166 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
2172 unfreeze_partials(s
, c
);
2176 static void flush_cpu_slab(void *d
)
2178 struct kmem_cache
*s
= d
;
2180 __flush_cpu_slab(s
, smp_processor_id());
2183 static bool has_cpu_slab(int cpu
, void *info
)
2185 struct kmem_cache
*s
= info
;
2186 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
2188 return c
->page
|| c
->partial
;
2191 static void flush_all(struct kmem_cache
*s
)
2193 on_each_cpu_cond(has_cpu_slab
, flush_cpu_slab
, s
, 1, GFP_ATOMIC
);
2197 * Check if the objects in a per cpu structure fit numa
2198 * locality expectations.
2200 static inline int node_match(struct page
*page
, int node
)
2203 if (!page
|| (node
!= NUMA_NO_NODE
&& page_to_nid(page
) != node
))
2209 static int count_free(struct page
*page
)
2211 return page
->objects
- page
->inuse
;
2214 static unsigned long count_partial(struct kmem_cache_node
*n
,
2215 int (*get_count
)(struct page
*))
2217 unsigned long flags
;
2218 unsigned long x
= 0;
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
);
2228 static inline unsigned long node_nr_objs(struct kmem_cache_node
*n
)
2230 #ifdef CONFIG_SLUB_DEBUG
2231 return atomic_long_read(&n
->total_objects
);
2237 static noinline
void
2238 slab_out_of_memory(struct kmem_cache
*s
, gfp_t gfpflags
, int nid
)
2243 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
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
));
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
);
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
;
2262 nr_free
= count_partial(n
, count_free
);
2263 nr_slabs
= node_nr_slabs(n
);
2264 nr_objs
= node_nr_objs(n
);
2267 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
2268 node
, nr_slabs
, nr_objs
, nr_free
);
2272 static inline void *new_slab_objects(struct kmem_cache
*s
, gfp_t flags
,
2273 int node
, struct kmem_cache_cpu
**pc
)
2276 struct kmem_cache_cpu
*c
= *pc
;
2279 freelist
= get_partial(s
, flags
, node
, c
);
2284 page
= new_slab(s
, flags
, node
);
2286 c
= __this_cpu_ptr(s
->cpu_slab
);
2291 * No other reference to the page yet so we can
2292 * muck around with it freely without cmpxchg
2294 freelist
= page
->freelist
;
2295 page
->freelist
= NULL
;
2297 stat(s
, ALLOC_SLAB
);
2306 static inline bool pfmemalloc_match(struct page
*page
, gfp_t gfpflags
)
2308 if (unlikely(PageSlabPfmemalloc(page
)))
2309 return gfp_pfmemalloc_allowed(gfpflags
);
2315 * Check the page->freelist of a page and either transfer the freelist to the per cpu freelist
2316 * or deactivate the page.
2318 * The page is still frozen if the return value is not NULL.
2320 * If this function returns NULL then the page has been unfrozen.
2322 * This function must be called with interrupt disabled.
2324 static inline void *get_freelist(struct kmem_cache
*s
, struct page
*page
)
2327 unsigned long counters
;
2331 freelist
= page
->freelist
;
2332 counters
= page
->counters
;
2334 new.counters
= counters
;
2335 VM_BUG_ON(!new.frozen
);
2337 new.inuse
= page
->objects
;
2338 new.frozen
= freelist
!= NULL
;
2340 } while (!__cmpxchg_double_slab(s
, page
,
2349 * Slow path. The lockless freelist is empty or we need to perform
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.
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.
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.
2364 static void *__slab_alloc(struct kmem_cache
*s
, gfp_t gfpflags
, int node
,
2365 unsigned long addr
, struct kmem_cache_cpu
*c
)
2369 unsigned long flags
;
2371 local_irq_save(flags
);
2372 #ifdef CONFIG_PREEMPT
2374 * We may have been preempted and rescheduled on a different
2375 * cpu before disabling interrupts. Need to reload cpu area
2378 c
= this_cpu_ptr(s
->cpu_slab
);
2386 if (unlikely(!node_match(page
, node
))) {
2387 stat(s
, ALLOC_NODE_MISMATCH
);
2388 deactivate_slab(s
, page
, c
->freelist
);
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
2399 if (unlikely(!pfmemalloc_match(page
, gfpflags
))) {
2400 deactivate_slab(s
, page
, c
->freelist
);
2406 /* must check again c->freelist in case of cpu migration or IRQ */
2407 freelist
= c
->freelist
;
2411 stat(s
, ALLOC_SLOWPATH
);
2413 freelist
= get_freelist(s
, page
);
2417 stat(s
, DEACTIVATE_BYPASS
);
2421 stat(s
, ALLOC_REFILL
);
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.
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
);
2438 page
= c
->page
= c
->partial
;
2439 c
->partial
= page
->next
;
2440 stat(s
, CPU_PARTIAL_ALLOC
);
2445 freelist
= new_slab_objects(s
, gfpflags
, node
, &c
);
2447 if (unlikely(!freelist
)) {
2448 if (!(gfpflags
& __GFP_NOWARN
) && printk_ratelimit())
2449 slab_out_of_memory(s
, gfpflags
, node
);
2451 local_irq_restore(flags
);
2456 if (likely(!kmem_cache_debug(s
) && pfmemalloc_match(page
, gfpflags
)))
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 */
2463 deactivate_slab(s
, page
, get_freepointer(s
, freelist
));
2466 local_irq_restore(flags
);
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.
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.
2478 * Otherwise we can simply pick the next object from the lockless free list.
2480 static __always_inline
void *slab_alloc_node(struct kmem_cache
*s
,
2481 gfp_t gfpflags
, int node
, unsigned long addr
)
2484 struct kmem_cache_cpu
*c
;
2488 if (slab_pre_alloc_hook(s
, gfpflags
))
2491 s
= memcg_kmem_get_cache(s
, gfpflags
);
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.
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.
2505 c
= __this_cpu_ptr(s
->cpu_slab
);
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.
2516 object
= c
->freelist
;
2518 if (unlikely(!object
|| !node_match(page
, node
)))
2519 object
= __slab_alloc(s
, gfpflags
, node
, addr
, c
);
2522 void *next_object
= get_freepointer_safe(s
, object
);
2525 * The cmpxchg will only match if there was no additional
2526 * operation and if we are on the right processor.
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
2533 * Since this is without lock semantics the protection is only against
2534 * code executing on this cpu *not* from access by other cpus.
2536 if (unlikely(!this_cpu_cmpxchg_double(
2537 s
->cpu_slab
->freelist
, s
->cpu_slab
->tid
,
2539 next_object
, next_tid(tid
)))) {
2541 note_cmpxchg_failure("slab_alloc", s
, tid
);
2544 prefetch_freepointer(s
, next_object
);
2545 stat(s
, ALLOC_FASTPATH
);
2548 if (unlikely(gfpflags
& __GFP_ZERO
) && object
)
2549 memset(object
, 0, s
->object_size
);
2551 slab_post_alloc_hook(s
, gfpflags
, object
);
2556 static __always_inline
void *slab_alloc(struct kmem_cache
*s
,
2557 gfp_t gfpflags
, unsigned long addr
)
2559 return slab_alloc_node(s
, gfpflags
, NUMA_NO_NODE
, addr
);
2562 void *kmem_cache_alloc(struct kmem_cache
*s
, gfp_t gfpflags
)
2564 void *ret
= slab_alloc(s
, gfpflags
, _RET_IP_
);
2566 trace_kmem_cache_alloc(_RET_IP_
, ret
, s
->object_size
, s
->size
, gfpflags
);
2570 EXPORT_SYMBOL(kmem_cache_alloc
);
2572 #ifdef CONFIG_TRACING
2573 void *kmem_cache_alloc_trace(struct kmem_cache
*s
, gfp_t gfpflags
, size_t size
)
2575 void *ret
= slab_alloc(s
, gfpflags
, _RET_IP_
);
2576 trace_kmalloc(_RET_IP_
, ret
, size
, s
->size
, gfpflags
);
2579 EXPORT_SYMBOL(kmem_cache_alloc_trace
);
2581 void *kmalloc_order_trace(size_t size
, gfp_t flags
, unsigned int order
)
2583 void *ret
= kmalloc_order(size
, flags
, order
);
2584 trace_kmalloc(_RET_IP_
, ret
, size
, PAGE_SIZE
<< order
, flags
);
2587 EXPORT_SYMBOL(kmalloc_order_trace
);
2591 void *kmem_cache_alloc_node(struct kmem_cache
*s
, gfp_t gfpflags
, int node
)
2593 void *ret
= slab_alloc_node(s
, gfpflags
, node
, _RET_IP_
);
2595 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
2596 s
->object_size
, s
->size
, gfpflags
, node
);
2600 EXPORT_SYMBOL(kmem_cache_alloc_node
);
2602 #ifdef CONFIG_TRACING
2603 void *kmem_cache_alloc_node_trace(struct kmem_cache
*s
,
2605 int node
, size_t size
)
2607 void *ret
= slab_alloc_node(s
, gfpflags
, node
, _RET_IP_
);
2609 trace_kmalloc_node(_RET_IP_
, ret
,
2610 size
, s
->size
, gfpflags
, node
);
2613 EXPORT_SYMBOL(kmem_cache_alloc_node_trace
);
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.
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.
2625 static void __slab_free(struct kmem_cache
*s
, struct page
*page
,
2626 void *x
, unsigned long addr
)
2629 void **object
= (void *)x
;
2632 unsigned long counters
;
2633 struct kmem_cache_node
*n
= NULL
;
2634 unsigned long uninitialized_var(flags
);
2636 stat(s
, FREE_SLOWPATH
);
2638 if (kmem_cache_debug(s
) &&
2639 !(n
= free_debug_processing(s
, page
, x
, addr
, &flags
)))
2644 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2647 prior
= page
->freelist
;
2648 counters
= page
->counters
;
2649 set_freepointer(s
, object
, prior
);
2650 new.counters
= counters
;
2651 was_frozen
= new.frozen
;
2653 if ((!new.inuse
|| !prior
) && !was_frozen
) {
2655 if (!kmem_cache_debug(s
) && !prior
)
2658 * Slab was on no list before and will be partially empty
2659 * We can defer the list move and instead freeze it.
2663 else { /* Needs to be taken off a list */
2665 n
= get_node(s
, page_to_nid(page
));
2667 * Speculatively acquire the list_lock.
2668 * If the cmpxchg does not succeed then we may
2669 * drop the list_lock without any processing.
2671 * Otherwise the list_lock will synchronize with
2672 * other processors updating the list of slabs.
2674 spin_lock_irqsave(&n
->list_lock
, flags
);
2679 } while (!cmpxchg_double_slab(s
, page
,
2681 object
, new.counters
,
2687 * If we just froze the page then put it onto the
2688 * per cpu partial list.
2690 if (new.frozen
&& !was_frozen
) {
2691 put_cpu_partial(s
, page
, 1);
2692 stat(s
, CPU_PARTIAL_FREE
);
2695 * The list lock was not taken therefore no list
2696 * activity can be necessary.
2699 stat(s
, FREE_FROZEN
);
2703 if (unlikely(!new.inuse
&& n
->nr_partial
> s
->min_partial
))
2707 * Objects left in the slab. If it was not on the partial list before
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
);
2715 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2721 * Slab on the partial list.
2723 remove_partial(n
, page
);
2724 stat(s
, FREE_REMOVE_PARTIAL
);
2726 /* Slab must be on the full list */
2727 remove_full(s
, page
);
2729 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2731 discard_slab(s
, page
);
2735 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2736 * can perform fastpath freeing without additional function calls.
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
2742 * If fastpath is not possible then fall back to __slab_free where we deal
2743 * with all sorts of special processing.
2745 static __always_inline
void slab_free(struct kmem_cache
*s
,
2746 struct page
*page
, void *x
, unsigned long addr
)
2748 void **object
= (void *)x
;
2749 struct kmem_cache_cpu
*c
;
2752 slab_free_hook(s
, x
);
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.
2762 c
= __this_cpu_ptr(s
->cpu_slab
);
2767 if (likely(page
== c
->page
)) {
2768 set_freepointer(s
, object
, c
->freelist
);
2770 if (unlikely(!this_cpu_cmpxchg_double(
2771 s
->cpu_slab
->freelist
, s
->cpu_slab
->tid
,
2773 object
, next_tid(tid
)))) {
2775 note_cmpxchg_failure("slab_free", s
, tid
);
2778 stat(s
, FREE_FASTPATH
);
2780 __slab_free(s
, page
, x
, addr
);
2784 void kmem_cache_free(struct kmem_cache
*s
, void *x
)
2786 s
= cache_from_obj(s
, x
);
2789 slab_free(s
, virt_to_head_page(x
), x
, _RET_IP_
);
2790 trace_kmem_cache_free(_RET_IP_
, x
);
2792 EXPORT_SYMBOL(kmem_cache_free
);
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
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
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.
2813 static int slub_min_order
;
2814 static int slub_max_order
= PAGE_ALLOC_COSTLY_ORDER
;
2815 static int slub_min_objects
;
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.)
2821 static int slub_nomerge
;
2824 * Calculate the order of allocation given an slab object size.
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
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.
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.
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.
2848 static inline int slab_order(int size
, int min_objects
,
2849 int max_order
, int fract_leftover
, int reserved
)
2853 int min_order
= slub_min_order
;
2855 if (order_objects(min_order
, size
, reserved
) > MAX_OBJS_PER_PAGE
)
2856 return get_order(size
* MAX_OBJS_PER_PAGE
) - 1;
2858 for (order
= max(min_order
,
2859 fls(min_objects
* size
- 1) - PAGE_SHIFT
);
2860 order
<= max_order
; order
++) {
2862 unsigned long slab_size
= PAGE_SIZE
<< order
;
2864 if (slab_size
< min_objects
* size
+ reserved
)
2867 rem
= (slab_size
- reserved
) % size
;
2869 if (rem
<= slab_size
/ fract_leftover
)
2877 static inline int calculate_order(int size
, int reserved
)
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.
2889 * First we reduce the acceptable waste in a slab. Then
2890 * we reduce the minimum objects required in a slab.
2892 min_objects
= slub_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
);
2898 while (min_objects
> 1) {
2900 while (fraction
>= 4) {
2901 order
= slab_order(size
, min_objects
,
2902 slub_max_order
, fraction
, reserved
);
2903 if (order
<= slub_max_order
)
2911 * We were unable to place multiple objects in a slab. Now
2912 * lets see if we can place a single object there.
2914 order
= slab_order(size
, 1, slub_max_order
, 1, reserved
);
2915 if (order
<= slub_max_order
)
2919 * Doh this slab cannot be placed using slub_max_order.
2921 order
= slab_order(size
, 1, MAX_ORDER
, 1, reserved
);
2922 if (order
< MAX_ORDER
)
2928 init_kmem_cache_node(struct kmem_cache_node
*n
)
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
);
2940 static inline int alloc_kmem_cache_cpus(struct kmem_cache
*s
)
2942 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE
<
2943 KMALLOC_SHIFT_HIGH
* sizeof(struct kmem_cache_cpu
));
2946 * Must align to double word boundary for the double cmpxchg
2947 * instructions to work; see __pcpu_double_call_return_bool().
2949 s
->cpu_slab
= __alloc_percpu(sizeof(struct kmem_cache_cpu
),
2950 2 * sizeof(void *));
2955 init_kmem_cache_cpus(s
);
2960 static struct kmem_cache
*kmem_cache_node
;
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
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.
2971 static void early_kmem_cache_node_alloc(int node
)
2974 struct kmem_cache_node
*n
;
2976 BUG_ON(kmem_cache_node
->size
< sizeof(struct kmem_cache_node
));
2978 page
= new_slab(kmem_cache_node
, GFP_NOWAIT
, node
);
2981 if (page_to_nid(page
) != node
) {
2982 printk(KERN_ERR
"SLUB: Unable to allocate memory from "
2984 printk(KERN_ERR
"SLUB: Allocating a useless per node structure "
2985 "in order to be able to continue\n");
2990 page
->freelist
= get_freepointer(kmem_cache_node
, n
);
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
);
2998 init_kmem_cache_node(n
);
2999 inc_slabs_node(kmem_cache_node
, node
, page
->objects
);
3001 add_partial(n
, page
, DEACTIVATE_TO_HEAD
);
3004 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
3008 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3009 struct kmem_cache_node
*n
= s
->node
[node
];
3012 kmem_cache_free(kmem_cache_node
, n
);
3014 s
->node
[node
] = NULL
;
3018 static int init_kmem_cache_nodes(struct kmem_cache
*s
)
3022 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3023 struct kmem_cache_node
*n
;
3025 if (slab_state
== DOWN
) {
3026 early_kmem_cache_node_alloc(node
);
3029 n
= kmem_cache_alloc_node(kmem_cache_node
,
3033 free_kmem_cache_nodes(s
);
3038 init_kmem_cache_node(n
);
3043 static void set_min_partial(struct kmem_cache
*s
, unsigned long min
)
3045 if (min
< MIN_PARTIAL
)
3047 else if (min
> MAX_PARTIAL
)
3049 s
->min_partial
= min
;
3053 * calculate_sizes() determines the order and the distribution of data within
3056 static int calculate_sizes(struct kmem_cache
*s
, int forced_order
)
3058 unsigned long flags
= s
->flags
;
3059 unsigned long size
= s
->object_size
;
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.
3067 size
= ALIGN(size
, sizeof(void *));
3069 #ifdef CONFIG_SLUB_DEBUG
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.
3075 if ((flags
& SLAB_POISON
) && !(flags
& SLAB_DESTROY_BY_RCU
) &&
3077 s
->flags
|= __OBJECT_POISON
;
3079 s
->flags
&= ~__OBJECT_POISON
;
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.
3087 if ((flags
& SLAB_RED_ZONE
) && size
== s
->object_size
)
3088 size
+= sizeof(void *);
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.
3097 if (((flags
& (SLAB_DESTROY_BY_RCU
| SLAB_POISON
)) ||
3100 * Relocate free pointer after the object if it is not
3101 * permitted to overwrite the first word of the object on
3104 * This is the case if we do RCU, have a constructor or
3105 * destructor or are poisoning the objects.
3108 size
+= sizeof(void *);
3111 #ifdef CONFIG_SLUB_DEBUG
3112 if (flags
& SLAB_STORE_USER
)
3114 * Need to store information about allocs and frees after
3117 size
+= 2 * sizeof(struct track
);
3119 if (flags
& SLAB_RED_ZONE
)
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
3127 size
+= sizeof(void *);
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.
3135 size
= ALIGN(size
, s
->align
);
3137 if (forced_order
>= 0)
3138 order
= forced_order
;
3140 order
= calculate_order(size
, s
->reserved
);
3147 s
->allocflags
|= __GFP_COMP
;
3149 if (s
->flags
& SLAB_CACHE_DMA
)
3150 s
->allocflags
|= GFP_DMA
;
3152 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
3153 s
->allocflags
|= __GFP_RECLAIMABLE
;
3156 * Determine the number of objects per slab
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
))
3163 return !!oo_objects(s
->oo
);
3166 static int kmem_cache_open(struct kmem_cache
*s
, unsigned long flags
)
3168 s
->flags
= kmem_cache_flags(s
->size
, flags
, s
->name
, s
->ctor
);
3171 if (need_reserve_slab_rcu
&& (s
->flags
& SLAB_DESTROY_BY_RCU
))
3172 s
->reserved
= sizeof(struct rcu_head
);
3174 if (!calculate_sizes(s
, -1))
3176 if (disable_higher_order_debug
) {
3178 * Disable debugging flags that store metadata if the min slab
3181 if (get_order(s
->size
) > get_order(s
->object_size
)) {
3182 s
->flags
&= ~DEBUG_METADATA_FLAGS
;
3184 if (!calculate_sizes(s
, -1))
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
;
3197 * The larger the object size is, the more pages we want on the partial
3198 * list to avoid pounding the page allocator excessively.
3200 set_min_partial(s
, ilog2(s
->size
) / 2);
3203 * cpu_partial determined the maximum number of objects kept in the
3204 * per cpu partial lists of a processor.
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.
3211 * This setting also determines
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.
3219 if (kmem_cache_debug(s
))
3221 else if (s
->size
>= PAGE_SIZE
)
3223 else if (s
->size
>= 1024)
3225 else if (s
->size
>= 256)
3226 s
->cpu_partial
= 13;
3228 s
->cpu_partial
= 30;
3231 s
->remote_node_defrag_ratio
= 1000;
3233 if (!init_kmem_cache_nodes(s
))
3236 if (alloc_kmem_cache_cpus(s
))
3239 free_kmem_cache_nodes(s
);
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
),
3249 static void list_slab_objects(struct kmem_cache
*s
, struct page
*page
,
3252 #ifdef CONFIG_SLUB_DEBUG
3253 void *addr
= page_address(page
);
3255 unsigned long *map
= kzalloc(BITS_TO_LONGS(page
->objects
) *
3256 sizeof(long), GFP_ATOMIC
);
3259 slab_err(s
, page
, text
, s
->name
);
3262 get_map(s
, page
, map
);
3263 for_each_object(p
, s
, addr
, page
->objects
) {
3265 if (!test_bit(slab_index(p
, s
, addr
), map
)) {
3266 printk(KERN_ERR
"INFO: Object 0x%p @offset=%tu\n",
3268 print_tracking(s
, p
);
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.
3281 static void free_partial(struct kmem_cache
*s
, struct kmem_cache_node
*n
)
3283 struct page
*page
, *h
;
3285 list_for_each_entry_safe(page
, h
, &n
->partial
, lru
) {
3287 remove_partial(n
, page
);
3288 discard_slab(s
, page
);
3290 list_slab_objects(s
, page
,
3291 "Objects remaining in %s on kmem_cache_close()");
3297 * Release all resources used by a slab cache.
3299 static inline int kmem_cache_close(struct kmem_cache
*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
);
3309 if (n
->nr_partial
|| slabs_node(s
, node
))
3312 free_percpu(s
->cpu_slab
);
3313 free_kmem_cache_nodes(s
);
3317 int __kmem_cache_shutdown(struct kmem_cache
*s
)
3319 int rc
= kmem_cache_close(s
);
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.
3330 mutex_unlock(&slab_mutex
);
3331 sysfs_slab_remove(s
);
3332 mutex_lock(&slab_mutex
);
3338 /********************************************************************
3340 *******************************************************************/
3342 static int __init
setup_slub_min_order(char *str
)
3344 get_option(&str
, &slub_min_order
);
3349 __setup("slub_min_order=", setup_slub_min_order
);
3351 static int __init
setup_slub_max_order(char *str
)
3353 get_option(&str
, &slub_max_order
);
3354 slub_max_order
= min(slub_max_order
, MAX_ORDER
- 1);
3359 __setup("slub_max_order=", setup_slub_max_order
);
3361 static int __init
setup_slub_min_objects(char *str
)
3363 get_option(&str
, &slub_min_objects
);
3368 __setup("slub_min_objects=", setup_slub_min_objects
);
3370 static int __init
setup_slub_nomerge(char *str
)
3376 __setup("slub_nomerge", setup_slub_nomerge
);
3378 void *__kmalloc(size_t size
, gfp_t flags
)
3380 struct kmem_cache
*s
;
3383 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
))
3384 return kmalloc_large(size
, flags
);
3386 s
= kmalloc_slab(size
, flags
);
3388 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3391 ret
= slab_alloc(s
, flags
, _RET_IP_
);
3393 trace_kmalloc(_RET_IP_
, ret
, size
, s
->size
, flags
);
3397 EXPORT_SYMBOL(__kmalloc
);
3400 static void *kmalloc_large_node(size_t size
, gfp_t flags
, int node
)
3405 flags
|= __GFP_COMP
| __GFP_NOTRACK
| __GFP_KMEMCG
;
3406 page
= alloc_pages_node(node
, flags
, get_order(size
));
3408 ptr
= page_address(page
);
3410 kmemleak_alloc(ptr
, size
, 1, flags
);
3414 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3416 struct kmem_cache
*s
;
3419 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
)) {
3420 ret
= kmalloc_large_node(size
, flags
, node
);
3422 trace_kmalloc_node(_RET_IP_
, ret
,
3423 size
, PAGE_SIZE
<< get_order(size
),
3429 s
= kmalloc_slab(size
, flags
);
3431 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3434 ret
= slab_alloc_node(s
, flags
, node
, _RET_IP_
);
3436 trace_kmalloc_node(_RET_IP_
, ret
, size
, s
->size
, flags
, node
);
3440 EXPORT_SYMBOL(__kmalloc_node
);
3443 size_t ksize(const void *object
)
3447 if (unlikely(object
== ZERO_SIZE_PTR
))
3450 page
= virt_to_head_page(object
);
3452 if (unlikely(!PageSlab(page
))) {
3453 WARN_ON(!PageCompound(page
));
3454 return PAGE_SIZE
<< compound_order(page
);
3457 return slab_ksize(page
->slab_cache
);
3459 EXPORT_SYMBOL(ksize
);
3461 #ifdef CONFIG_SLUB_DEBUG
3462 bool verify_mem_not_deleted(const void *x
)
3465 void *object
= (void *)x
;
3466 unsigned long flags
;
3469 if (unlikely(ZERO_OR_NULL_PTR(x
)))
3472 local_irq_save(flags
);
3474 page
= virt_to_head_page(x
);
3475 if (unlikely(!PageSlab(page
))) {
3476 /* maybe it was from stack? */
3482 if (on_freelist(page
->slab_cache
, page
, object
)) {
3483 object_err(page
->slab_cache
, page
, object
, "Object is on free-list");
3491 local_irq_restore(flags
);
3494 EXPORT_SYMBOL(verify_mem_not_deleted
);
3497 void kfree(const void *x
)
3500 void *object
= (void *)x
;
3502 trace_kfree(_RET_IP_
, x
);
3504 if (unlikely(ZERO_OR_NULL_PTR(x
)))
3507 page
= virt_to_head_page(x
);
3508 if (unlikely(!PageSlab(page
))) {
3509 BUG_ON(!PageCompound(page
));
3511 __free_memcg_kmem_pages(page
, compound_order(page
));
3514 slab_free(page
->slab_cache
, page
, object
, _RET_IP_
);
3516 EXPORT_SYMBOL(kfree
);
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.
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.
3528 int kmem_cache_shrink(struct kmem_cache
*s
)
3532 struct kmem_cache_node
*n
;
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
;
3540 if (!slabs_by_inuse
)
3544 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3545 n
= get_node(s
, node
);
3550 for (i
= 0; i
< objects
; i
++)
3551 INIT_LIST_HEAD(slabs_by_inuse
+ i
);
3553 spin_lock_irqsave(&n
->list_lock
, flags
);
3556 * Build lists indexed by the items in use in each slab.
3558 * Note that concurrent frees may occur while we hold the
3559 * list_lock. page->inuse here is the upper limit.
3561 list_for_each_entry_safe(page
, t
, &n
->partial
, lru
) {
3562 list_move(&page
->lru
, slabs_by_inuse
+ page
->inuse
);
3568 * Rebuild the partial list with the slabs filled up most
3569 * first and the least used slabs at the end.
3571 for (i
= objects
- 1; i
> 0; i
--)
3572 list_splice(slabs_by_inuse
+ i
, n
->partial
.prev
);
3574 spin_unlock_irqrestore(&n
->list_lock
, flags
);
3576 /* Release empty slabs */
3577 list_for_each_entry_safe(page
, t
, slabs_by_inuse
, lru
)
3578 discard_slab(s
, page
);
3581 kfree(slabs_by_inuse
);
3584 EXPORT_SYMBOL(kmem_cache_shrink
);
3586 static int slab_mem_going_offline_callback(void *arg
)
3588 struct kmem_cache
*s
;
3590 mutex_lock(&slab_mutex
);
3591 list_for_each_entry(s
, &slab_caches
, list
)
3592 kmem_cache_shrink(s
);
3593 mutex_unlock(&slab_mutex
);
3598 static void slab_mem_offline_callback(void *arg
)
3600 struct kmem_cache_node
*n
;
3601 struct kmem_cache
*s
;
3602 struct memory_notify
*marg
= arg
;
3605 offline_node
= marg
->status_change_nid_normal
;
3608 * If the node still has available memory. we need kmem_cache_node
3611 if (offline_node
< 0)
3614 mutex_lock(&slab_mutex
);
3615 list_for_each_entry(s
, &slab_caches
, list
) {
3616 n
= get_node(s
, offline_node
);
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.
3624 BUG_ON(slabs_node(s
, offline_node
));
3626 s
->node
[offline_node
] = NULL
;
3627 kmem_cache_free(kmem_cache_node
, n
);
3630 mutex_unlock(&slab_mutex
);
3633 static int slab_mem_going_online_callback(void *arg
)
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
;
3642 * If the node's memory is already available, then kmem_cache_node is
3643 * already created. Nothing to do.
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
3653 mutex_lock(&slab_mutex
);
3654 list_for_each_entry(s
, &slab_caches
, list
) {
3656 * XXX: kmem_cache_alloc_node will fallback to other nodes
3657 * since memory is not yet available from the node that
3660 n
= kmem_cache_alloc(kmem_cache_node
, GFP_KERNEL
);
3665 init_kmem_cache_node(n
);
3669 mutex_unlock(&slab_mutex
);
3673 static int slab_memory_callback(struct notifier_block
*self
,
3674 unsigned long action
, void *arg
)
3679 case MEM_GOING_ONLINE
:
3680 ret
= slab_mem_going_online_callback(arg
);
3682 case MEM_GOING_OFFLINE
:
3683 ret
= slab_mem_going_offline_callback(arg
);
3686 case MEM_CANCEL_ONLINE
:
3687 slab_mem_offline_callback(arg
);
3690 case MEM_CANCEL_OFFLINE
:
3694 ret
= notifier_from_errno(ret
);
3700 static struct notifier_block slab_memory_callback_nb
= {
3701 .notifier_call
= slab_memory_callback
,
3702 .priority
= SLAB_CALLBACK_PRI
,
3705 /********************************************************************
3706 * Basic setup of slabs
3707 *******************************************************************/
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.
3715 static struct kmem_cache
* __init
bootstrap(struct kmem_cache
*static_cache
)
3718 struct kmem_cache
*s
= kmem_cache_zalloc(kmem_cache
, GFP_NOWAIT
);
3720 memcpy(s
, static_cache
, kmem_cache
->object_size
);
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
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
);
3733 list_for_each_entry(p
, &n
->partial
, lru
)
3736 #ifdef CONFIG_SLUB_DEBUG
3737 #ifndef CONFIG_RKP_KDP
3738 list_for_each_entry(p
, &n
->full
, lru
)
3740 #endif /*CONFIG_RKP_KDP*/
3744 list_add(&s
->list
, &slab_caches
);
3748 void __init
kmem_cache_init(void)
3750 static __initdata
struct kmem_cache boot_kmem_cache
,
3751 boot_kmem_cache_node
;
3753 if (debug_guardpage_minorder())
3756 kmem_cache_node
= &boot_kmem_cache_node
;
3757 kmem_cache
= &boot_kmem_cache
;
3759 create_boot_cache(kmem_cache_node
, "kmem_cache_node",
3760 sizeof(struct kmem_cache_node
), SLAB_HWCACHE_ALIGN
);
3762 register_hotmemory_notifier(&slab_memory_callback_nb
);
3764 /* Able to allocate the per node structures */
3765 slab_state
= PARTIAL
;
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
);
3772 kmem_cache
= bootstrap(&boot_kmem_cache
);
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.
3779 kmem_cache_node
= bootstrap(&boot_kmem_cache_node
);
3781 /* Now we can use the kmem_cache to allocate kmalloc slabs */
3782 create_kmalloc_caches(0);
3785 register_cpu_notifier(&slab_notifier
);
3789 "SLUB: HWalign=%d, Order=%d-%d, MinObjects=%d,"
3790 " CPUs=%d, Nodes=%d\n",
3792 slub_min_order
, slub_max_order
, slub_min_objects
,
3793 nr_cpu_ids
, nr_node_ids
);
3796 void __init
kmem_cache_init_late(void)
3801 * Find a mergeable slab cache
3803 static int slab_unmergeable(struct kmem_cache
*s
)
3805 if (slub_nomerge
|| (s
->flags
& SLUB_NEVER_MERGE
))
3812 * We may have set a slab to be unmergeable during bootstrap.
3814 if (s
->refcount
< 0)
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 *))
3824 struct kmem_cache
*s
;
3826 if (slub_nomerge
|| (flags
& SLUB_NEVER_MERGE
))
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
);
3837 list_for_each_entry(s
, &slab_caches
, list
) {
3838 if (slab_unmergeable(s
))
3844 if ((flags
& SLUB_MERGE_SAME
) != (s
->flags
& SLUB_MERGE_SAME
))
3847 * Check if alignment is compatible.
3848 * Courtesy of Adrian Drzewiecki
3850 if ((s
->size
& ~(align
- 1)) != s
->size
)
3853 if (s
->size
- size
>= sizeof(void *))
3856 if (!cache_match_memcg(s
, memcg
))
3865 __kmem_cache_alias(struct mem_cgroup
*memcg
, const char *name
, size_t size
,
3866 size_t align
, unsigned long flags
, void (*ctor
)(void *))
3868 struct kmem_cache
*s
;
3870 s
= find_mergeable(memcg
, size
, align
, flags
, name
, ctor
);
3874 * Adjust the object sizes so that we clear
3875 * the complete object on kzalloc.
3877 s
->object_size
= max(s
->object_size
, (int)size
);
3878 s
->inuse
= max_t(int, s
->inuse
, ALIGN(size
, sizeof(void *)));
3880 if (sysfs_slab_alias(s
, name
)) {
3889 int __kmem_cache_create(struct kmem_cache
*s
, unsigned long flags
)
3893 err
= kmem_cache_open(s
, flags
);
3897 /* Mutex is not taken during early boot */
3898 if (slab_state
<= UP
)
3901 memcg_propagate_slab_attrs(s
);
3902 mutex_unlock(&slab_mutex
);
3903 err
= sysfs_slab_add(s
);
3904 mutex_lock(&slab_mutex
);
3907 kmem_cache_close(s
);
3914 * Use the cpu notifier to insure that the cpu slabs are flushed when
3917 static int __cpuinit
slab_cpuup_callback(struct notifier_block
*nfb
,
3918 unsigned long action
, void *hcpu
)
3920 long cpu
= (long)hcpu
;
3921 struct kmem_cache
*s
;
3922 unsigned long flags
;
3925 case CPU_UP_CANCELED
:
3926 case CPU_UP_CANCELED_FROZEN
:
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
);
3935 mutex_unlock(&slab_mutex
);
3943 static struct notifier_block __cpuinitdata slab_notifier
= {
3944 .notifier_call
= slab_cpuup_callback
3949 void *__kmalloc_track_caller(size_t size
, gfp_t gfpflags
, unsigned long caller
)
3951 struct kmem_cache
*s
;
3954 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
))
3955 return kmalloc_large(size
, gfpflags
);
3957 s
= kmalloc_slab(size
, gfpflags
);
3959 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3962 ret
= slab_alloc(s
, gfpflags
, caller
);
3964 /* Honor the call site pointer we received. */
3965 trace_kmalloc(caller
, ret
, size
, s
->size
, gfpflags
);
3971 void *__kmalloc_node_track_caller(size_t size
, gfp_t gfpflags
,
3972 int node
, unsigned long caller
)
3974 struct kmem_cache
*s
;
3977 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
)) {
3978 ret
= kmalloc_large_node(size
, gfpflags
, node
);
3980 trace_kmalloc_node(caller
, ret
,
3981 size
, PAGE_SIZE
<< get_order(size
),
3987 s
= kmalloc_slab(size
, gfpflags
);
3989 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3992 ret
= slab_alloc_node(s
, gfpflags
, node
, caller
);
3994 /* Honor the call site pointer we received. */
3995 trace_kmalloc_node(caller
, ret
, size
, s
->size
, gfpflags
, node
);
4002 static int count_inuse(struct page
*page
)
4007 static int count_total(struct page
*page
)
4009 return page
->objects
;
4013 #ifdef CONFIG_SLUB_DEBUG
4014 static int validate_slab(struct kmem_cache
*s
, struct page
*page
,
4018 void *addr
= page_address(page
);
4020 if (!check_slab(s
, page
) ||
4021 !on_freelist(s
, page
, NULL
))
4024 /* Now we know that a valid freelist exists */
4025 bitmap_zero(map
, page
->objects
);
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
))
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
))
4041 static void validate_slab_slab(struct kmem_cache
*s
, struct page
*page
,
4045 validate_slab(s
, page
, map
);
4049 static int validate_slab_node(struct kmem_cache
*s
,
4050 struct kmem_cache_node
*n
, unsigned long *map
)
4052 unsigned long count
= 0;
4054 unsigned long flags
;
4056 spin_lock_irqsave(&n
->list_lock
, flags
);
4058 list_for_each_entry(page
, &n
->partial
, lru
) {
4059 validate_slab_slab(s
, page
, map
);
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
);
4066 if (!(s
->flags
& SLAB_STORE_USER
))
4069 list_for_each_entry(page
, &n
->full
, lru
) {
4070 validate_slab_slab(s
, page
, map
);
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
));
4079 spin_unlock_irqrestore(&n
->list_lock
, flags
);
4083 static long validate_slab_cache(struct kmem_cache
*s
)
4086 unsigned long count
= 0;
4087 unsigned long *map
= kmalloc(BITS_TO_LONGS(oo_objects(s
->max
)) *
4088 sizeof(unsigned long), GFP_KERNEL
);
4094 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4095 struct kmem_cache_node
*n
= get_node(s
, node
);
4097 count
+= validate_slab_node(s
, n
, map
);
4103 * Generate lists of code addresses where slabcache objects are allocated
4108 unsigned long count
;
4115 DECLARE_BITMAP(cpus
, NR_CPUS
);
4121 unsigned long count
;
4122 struct location
*loc
;
4125 static void free_loc_track(struct loc_track
*t
)
4128 free_pages((unsigned long)t
->loc
,
4129 get_order(sizeof(struct location
) * t
->max
));
4132 static int alloc_loc_track(struct loc_track
*t
, unsigned long max
, gfp_t flags
)
4137 order
= get_order(sizeof(struct location
) * max
);
4139 l
= (void *)__get_free_pages(flags
, order
);
4144 memcpy(l
, t
->loc
, sizeof(struct location
) * t
->count
);
4152 static int add_location(struct loc_track
*t
, struct kmem_cache
*s
,
4153 const struct track
*track
)
4155 long start
, end
, pos
;
4157 unsigned long caddr
;
4158 unsigned long age
= jiffies
- track
->when
;
4164 pos
= start
+ (end
- start
+ 1) / 2;
4167 * There is nothing at "end". If we end up there
4168 * we need to add something to before end.
4173 caddr
= t
->loc
[pos
].addr
;
4174 if (track
->addr
== caddr
) {
4180 if (age
< l
->min_time
)
4182 if (age
> l
->max_time
)
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
;
4190 cpumask_set_cpu(track
->cpu
,
4191 to_cpumask(l
->cpus
));
4193 node_set(page_to_nid(virt_to_page(track
)), l
->nodes
);
4197 if (track
->addr
< caddr
)
4204 * Not found. Insert new tracking element.
4206 if (t
->count
>= t
->max
&& !alloc_loc_track(t
, 2 * t
->max
, GFP_ATOMIC
))
4212 (t
->count
- pos
) * sizeof(struct location
));
4215 l
->addr
= track
->addr
;
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
);
4228 static void process_slab(struct loc_track
*t
, struct kmem_cache
*s
,
4229 struct page
*page
, enum track_item alloc
,
4232 void *addr
= page_address(page
);
4235 bitmap_zero(map
, page
->objects
);
4236 get_map(s
, page
, map
);
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
));
4243 static int list_locations(struct kmem_cache
*s
, char *buf
,
4244 enum track_item alloc
)
4248 struct loc_track t
= { 0, 0, NULL
};
4250 unsigned long *map
= kmalloc(BITS_TO_LONGS(oo_objects(s
->max
)) *
4251 sizeof(unsigned long), GFP_KERNEL
);
4253 if (!map
|| !alloc_loc_track(&t
, PAGE_SIZE
/ sizeof(struct location
),
4256 return sprintf(buf
, "Out of memory\n");
4258 /* Push back cpu slabs */
4261 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4262 struct kmem_cache_node
*n
= get_node(s
, node
);
4263 unsigned long flags
;
4266 if (!atomic_long_read(&n
->nr_slabs
))
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
);
4277 for (i
= 0; i
< t
.count
; i
++) {
4278 struct location
*l
= &t
.loc
[i
];
4280 if (len
> PAGE_SIZE
- KSYM_SYMBOL_LEN
- 100)
4282 len
+= sprintf(buf
+ len
, "%7ld ", l
->count
);
4285 len
+= sprintf(buf
+ len
, "%pS", (void *)l
->addr
);
4287 len
+= sprintf(buf
+ len
, "<not-available>");
4289 if (l
->sum_time
!= l
->min_time
) {
4290 len
+= sprintf(buf
+ len
, " age=%ld/%ld/%ld",
4292 (long)div_u64(l
->sum_time
, l
->count
),
4295 len
+= sprintf(buf
+ len
, " age=%ld",
4298 if (l
->min_pid
!= l
->max_pid
)
4299 len
+= sprintf(buf
+ len
, " pid=%ld-%ld",
4300 l
->min_pid
, l
->max_pid
);
4302 len
+= sprintf(buf
+ len
, " pid=%ld",
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
));
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,
4320 len
+= sprintf(buf
+ len
, "\n");
4326 len
+= sprintf(buf
, "No data\n");
4331 #ifdef SLUB_RESILIENCY_TEST
4332 static void resiliency_test(void)
4336 BUILD_BUG_ON(KMALLOC_MIN_SIZE
> 16 || KMALLOC_SHIFT_HIGH
< 10);
4338 printk(KERN_ERR
"SLUB resiliency testing\n");
4339 printk(KERN_ERR
"-----------------------\n");
4340 printk(KERN_ERR
"A. Corruption after allocation\n");
4342 p
= kzalloc(16, GFP_KERNEL
);
4344 printk(KERN_ERR
"\n1. kmalloc-16: Clobber Redzone/next pointer"
4345 " 0x12->0x%p\n\n", p
+ 16);
4347 validate_slab_cache(kmalloc_caches
[4]);
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
);
4355 "If allocated object is overwritten then not detectable\n\n");
4357 validate_slab_cache(kmalloc_caches
[5]);
4358 p
= kzalloc(64, GFP_KERNEL
);
4359 p
+= 64 + (get_cycles() & 0xff) * sizeof(void *);
4361 printk(KERN_ERR
"\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4364 "If allocated object is overwritten then not detectable\n\n");
4365 validate_slab_cache(kmalloc_caches
[6]);
4367 printk(KERN_ERR
"\nB. Corruption after free\n");
4368 p
= kzalloc(128, GFP_KERNEL
);
4371 printk(KERN_ERR
"1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p
);
4372 validate_slab_cache(kmalloc_caches
[7]);
4374 p
= kzalloc(256, GFP_KERNEL
);
4377 printk(KERN_ERR
"\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
4379 validate_slab_cache(kmalloc_caches
[8]);
4381 p
= kzalloc(512, GFP_KERNEL
);
4384 printk(KERN_ERR
"\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p
);
4385 validate_slab_cache(kmalloc_caches
[9]);
4389 static void resiliency_test(void) {};
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 */
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)
4408 static ssize_t
show_slab_objects(struct kmem_cache
*s
,
4409 char *buf
, unsigned long flags
)
4411 unsigned long total
= 0;
4414 unsigned long *nodes
;
4415 unsigned long *per_cpu
;
4417 nodes
= kzalloc(2 * sizeof(unsigned long) * nr_node_ids
, GFP_KERNEL
);
4420 per_cpu
= nodes
+ nr_node_ids
;
4422 if (flags
& SO_CPU
) {
4425 for_each_possible_cpu(cpu
) {
4426 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
4430 page
= ACCESS_ONCE(c
->page
);
4434 node
= page_to_nid(page
);
4435 if (flags
& SO_TOTAL
)
4437 else if (flags
& SO_OBJECTS
)
4445 page
= ACCESS_ONCE(c
->partial
);
4447 node
= page_to_nid(page
);
4448 if (flags
& SO_TOTAL
)
4450 else if (flags
& SO_OBJECTS
)
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
);
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
);
4475 x
= atomic_long_read(&n
->nr_slabs
);
4482 if (flags
& SO_PARTIAL
) {
4483 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4484 struct kmem_cache_node
*n
= get_node(s
, node
);
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
);
4496 x
= sprintf(buf
, "%lu", total
);
4498 for_each_node_state(node
, N_NORMAL_MEMORY
)
4500 x
+= sprintf(buf
+ x
, " N%d=%lu",
4503 unlock_memory_hotplug();
4505 return x
+ sprintf(buf
+ x
, "\n");
4508 #ifdef CONFIG_SLUB_DEBUG
4509 static int any_slab_objects(struct kmem_cache
*s
)
4513 for_each_online_node(node
) {
4514 struct kmem_cache_node
*n
= get_node(s
, node
);
4519 if (atomic_long_read(&n
->total_objects
))
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)
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
);
4535 #define SLAB_ATTR_RO(_name) \
4536 static struct slab_attribute _name##_attr = \
4537 __ATTR(_name, 0400, _name##_show, NULL)
4539 #define SLAB_ATTR(_name) \
4540 static struct slab_attribute _name##_attr = \
4541 __ATTR(_name, 0600, _name##_show, _name##_store)
4543 static ssize_t
slab_size_show(struct kmem_cache
*s
, char *buf
)
4545 return sprintf(buf
, "%d\n", s
->size
);
4547 SLAB_ATTR_RO(slab_size
);
4549 static ssize_t
align_show(struct kmem_cache
*s
, char *buf
)
4551 return sprintf(buf
, "%d\n", s
->align
);
4553 SLAB_ATTR_RO(align
);
4555 static ssize_t
object_size_show(struct kmem_cache
*s
, char *buf
)
4557 return sprintf(buf
, "%d\n", s
->object_size
);
4559 SLAB_ATTR_RO(object_size
);
4561 static ssize_t
objs_per_slab_show(struct kmem_cache
*s
, char *buf
)
4563 return sprintf(buf
, "%d\n", oo_objects(s
->oo
));
4565 SLAB_ATTR_RO(objs_per_slab
);
4567 static ssize_t
order_store(struct kmem_cache
*s
,
4568 const char *buf
, size_t length
)
4570 unsigned long order
;
4573 err
= strict_strtoul(buf
, 10, &order
);
4577 if (order
> slub_max_order
|| order
< slub_min_order
)
4580 calculate_sizes(s
, order
);
4584 static ssize_t
order_show(struct kmem_cache
*s
, char *buf
)
4586 return sprintf(buf
, "%d\n", oo_order(s
->oo
));
4590 static ssize_t
min_partial_show(struct kmem_cache
*s
, char *buf
)
4592 return sprintf(buf
, "%lu\n", s
->min_partial
);
4595 static ssize_t
min_partial_store(struct kmem_cache
*s
, const char *buf
,
4601 err
= strict_strtoul(buf
, 10, &min
);
4605 set_min_partial(s
, min
);
4608 SLAB_ATTR(min_partial
);
4610 static ssize_t
cpu_partial_show(struct kmem_cache
*s
, char *buf
)
4612 return sprintf(buf
, "%u\n", s
->cpu_partial
);
4615 static ssize_t
cpu_partial_store(struct kmem_cache
*s
, const char *buf
,
4618 unsigned long objects
;
4621 err
= strict_strtoul(buf
, 10, &objects
);
4624 if (objects
&& kmem_cache_debug(s
))
4627 s
->cpu_partial
= objects
;
4631 SLAB_ATTR(cpu_partial
);
4633 static ssize_t
ctor_show(struct kmem_cache
*s
, char *buf
)
4637 return sprintf(buf
, "%pS\n", s
->ctor
);
4641 static ssize_t
aliases_show(struct kmem_cache
*s
, char *buf
)
4643 return sprintf(buf
, "%d\n", s
->refcount
- 1);
4645 SLAB_ATTR_RO(aliases
);
4647 static ssize_t
partial_show(struct kmem_cache
*s
, char *buf
)
4649 return show_slab_objects(s
, buf
, SO_PARTIAL
);
4651 SLAB_ATTR_RO(partial
);
4653 static ssize_t
cpu_slabs_show(struct kmem_cache
*s
, char *buf
)
4655 return show_slab_objects(s
, buf
, SO_CPU
);
4657 SLAB_ATTR_RO(cpu_slabs
);
4659 static ssize_t
objects_show(struct kmem_cache
*s
, char *buf
)
4661 return show_slab_objects(s
, buf
, SO_ALL
|SO_OBJECTS
);
4663 SLAB_ATTR_RO(objects
);
4665 static ssize_t
objects_partial_show(struct kmem_cache
*s
, char *buf
)
4667 return show_slab_objects(s
, buf
, SO_PARTIAL
|SO_OBJECTS
);
4669 SLAB_ATTR_RO(objects_partial
);
4671 static ssize_t
slabs_cpu_partial_show(struct kmem_cache
*s
, char *buf
)
4678 for_each_online_cpu(cpu
) {
4679 struct page
*page
= per_cpu_ptr(s
->cpu_slab
, cpu
)->partial
;
4682 pages
+= page
->pages
;
4683 objects
+= page
->pobjects
;
4687 len
= sprintf(buf
, "%d(%d)", objects
, pages
);
4690 for_each_online_cpu(cpu
) {
4691 struct page
*page
= per_cpu_ptr(s
->cpu_slab
, cpu
) ->partial
;
4693 if (page
&& len
< PAGE_SIZE
- 20)
4694 len
+= sprintf(buf
+ len
, " C%d=%d(%d)", cpu
,
4695 page
->pobjects
, page
->pages
);
4698 return len
+ sprintf(buf
+ len
, "\n");
4700 SLAB_ATTR_RO(slabs_cpu_partial
);
4702 static ssize_t
reclaim_account_show(struct kmem_cache
*s
, char *buf
)
4704 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RECLAIM_ACCOUNT
));
4707 static ssize_t
reclaim_account_store(struct kmem_cache
*s
,
4708 const char *buf
, size_t length
)
4710 s
->flags
&= ~SLAB_RECLAIM_ACCOUNT
;
4712 s
->flags
|= SLAB_RECLAIM_ACCOUNT
;
4715 SLAB_ATTR(reclaim_account
);
4717 static ssize_t
hwcache_align_show(struct kmem_cache
*s
, char *buf
)
4719 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_HWCACHE_ALIGN
));
4721 SLAB_ATTR_RO(hwcache_align
);
4723 #ifdef CONFIG_ZONE_DMA
4724 static ssize_t
cache_dma_show(struct kmem_cache
*s
, char *buf
)
4726 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_CACHE_DMA
));
4728 SLAB_ATTR_RO(cache_dma
);
4731 static ssize_t
destroy_by_rcu_show(struct kmem_cache
*s
, char *buf
)
4733 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DESTROY_BY_RCU
));
4735 SLAB_ATTR_RO(destroy_by_rcu
);
4737 static ssize_t
reserved_show(struct kmem_cache
*s
, char *buf
)
4739 return sprintf(buf
, "%d\n", s
->reserved
);
4741 SLAB_ATTR_RO(reserved
);
4743 #ifdef CONFIG_SLUB_DEBUG
4744 static ssize_t
slabs_show(struct kmem_cache
*s
, char *buf
)
4746 return show_slab_objects(s
, buf
, SO_ALL
);
4748 SLAB_ATTR_RO(slabs
);
4750 static ssize_t
total_objects_show(struct kmem_cache
*s
, char *buf
)
4752 return show_slab_objects(s
, buf
, SO_ALL
|SO_TOTAL
);
4754 SLAB_ATTR_RO(total_objects
);
4756 static ssize_t
sanity_checks_show(struct kmem_cache
*s
, char *buf
)
4758 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DEBUG_FREE
));
4761 static ssize_t
sanity_checks_store(struct kmem_cache
*s
,
4762 const char *buf
, size_t length
)
4764 s
->flags
&= ~SLAB_DEBUG_FREE
;
4765 if (buf
[0] == '1') {
4766 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4767 s
->flags
|= SLAB_DEBUG_FREE
;
4771 SLAB_ATTR(sanity_checks
);
4773 static ssize_t
trace_show(struct kmem_cache
*s
, char *buf
)
4775 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_TRACE
));
4778 static ssize_t
trace_store(struct kmem_cache
*s
, const char *buf
,
4781 s
->flags
&= ~SLAB_TRACE
;
4782 if (buf
[0] == '1') {
4783 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4784 s
->flags
|= SLAB_TRACE
;
4790 static ssize_t
red_zone_show(struct kmem_cache
*s
, char *buf
)
4792 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RED_ZONE
));
4795 static ssize_t
red_zone_store(struct kmem_cache
*s
,
4796 const char *buf
, size_t length
)
4798 if (any_slab_objects(s
))
4801 s
->flags
&= ~SLAB_RED_ZONE
;
4802 if (buf
[0] == '1') {
4803 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4804 s
->flags
|= SLAB_RED_ZONE
;
4806 calculate_sizes(s
, -1);
4809 SLAB_ATTR(red_zone
);
4811 static ssize_t
poison_show(struct kmem_cache
*s
, char *buf
)
4813 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_POISON
));
4816 static ssize_t
poison_store(struct kmem_cache
*s
,
4817 const char *buf
, size_t length
)
4819 if (any_slab_objects(s
))
4822 s
->flags
&= ~SLAB_POISON
;
4823 if (buf
[0] == '1') {
4824 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4825 s
->flags
|= SLAB_POISON
;
4827 calculate_sizes(s
, -1);
4832 static ssize_t
store_user_show(struct kmem_cache
*s
, char *buf
)
4834 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_STORE_USER
));
4837 static ssize_t
store_user_store(struct kmem_cache
*s
,
4838 const char *buf
, size_t length
)
4840 if (any_slab_objects(s
))
4843 s
->flags
&= ~SLAB_STORE_USER
;
4844 if (buf
[0] == '1') {
4845 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4846 s
->flags
|= SLAB_STORE_USER
;
4848 calculate_sizes(s
, -1);
4851 SLAB_ATTR(store_user
);
4853 static ssize_t
validate_show(struct kmem_cache
*s
, char *buf
)
4858 static ssize_t
validate_store(struct kmem_cache
*s
,
4859 const char *buf
, size_t length
)
4863 if (buf
[0] == '1') {
4864 ret
= validate_slab_cache(s
);
4870 SLAB_ATTR(validate
);
4872 static ssize_t
alloc_calls_show(struct kmem_cache
*s
, char *buf
)
4874 if (!(s
->flags
& SLAB_STORE_USER
))
4876 return list_locations(s
, buf
, TRACK_ALLOC
);
4878 SLAB_ATTR_RO(alloc_calls
);
4880 static ssize_t
free_calls_show(struct kmem_cache
*s
, char *buf
)
4882 if (!(s
->flags
& SLAB_STORE_USER
))
4884 return list_locations(s
, buf
, TRACK_FREE
);
4886 SLAB_ATTR_RO(free_calls
);
4887 #endif /* CONFIG_SLUB_DEBUG */
4889 #ifdef CONFIG_FAILSLAB
4890 static ssize_t
failslab_show(struct kmem_cache
*s
, char *buf
)
4892 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_FAILSLAB
));
4895 static ssize_t
failslab_store(struct kmem_cache
*s
, const char *buf
,
4898 s
->flags
&= ~SLAB_FAILSLAB
;
4900 s
->flags
|= SLAB_FAILSLAB
;
4903 SLAB_ATTR(failslab
);
4906 static ssize_t
shrink_show(struct kmem_cache
*s
, char *buf
)
4911 static ssize_t
shrink_store(struct kmem_cache
*s
,
4912 const char *buf
, size_t length
)
4914 if (buf
[0] == '1') {
4915 int rc
= kmem_cache_shrink(s
);
4926 static ssize_t
remote_node_defrag_ratio_show(struct kmem_cache
*s
, char *buf
)
4928 return sprintf(buf
, "%d\n", s
->remote_node_defrag_ratio
/ 10);
4931 static ssize_t
remote_node_defrag_ratio_store(struct kmem_cache
*s
,
4932 const char *buf
, size_t length
)
4934 unsigned long ratio
;
4937 err
= strict_strtoul(buf
, 10, &ratio
);
4942 s
->remote_node_defrag_ratio
= ratio
* 10;
4946 SLAB_ATTR(remote_node_defrag_ratio
);
4949 #ifdef CONFIG_SLUB_STATS
4950 static int show_stat(struct kmem_cache
*s
, char *buf
, enum stat_item si
)
4952 unsigned long sum
= 0;
4955 int *data
= kmalloc(nr_cpu_ids
* sizeof(int), GFP_KERNEL
);
4960 for_each_online_cpu(cpu
) {
4961 unsigned x
= per_cpu_ptr(s
->cpu_slab
, cpu
)->stat
[si
];
4967 len
= sprintf(buf
, "%lu", sum
);
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
]);
4976 return len
+ sprintf(buf
+ len
, "\n");
4979 static void clear_stat(struct kmem_cache
*s
, enum stat_item si
)
4983 for_each_online_cpu(cpu
)
4984 per_cpu_ptr(s
->cpu_slab
, cpu
)->stat
[si
] = 0;
4987 #define STAT_ATTR(si, text) \
4988 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4990 return show_stat(s, buf, si); \
4992 static ssize_t text##_store(struct kmem_cache *s, \
4993 const char *buf, size_t length) \
4995 if (buf[0] != '0') \
4997 clear_stat(s, si); \
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
);
5030 static struct attribute
*slab_attrs
[] = {
5031 &slab_size_attr
.attr
,
5032 &object_size_attr
.attr
,
5033 &objs_per_slab_attr
.attr
,
5035 &min_partial_attr
.attr
,
5036 &cpu_partial_attr
.attr
,
5038 &objects_partial_attr
.attr
,
5040 &cpu_slabs_attr
.attr
,
5044 &hwcache_align_attr
.attr
,
5045 &reclaim_account_attr
.attr
,
5046 &destroy_by_rcu_attr
.attr
,
5048 &reserved_attr
.attr
,
5049 &slabs_cpu_partial_attr
.attr
,
5050 #ifdef CONFIG_SLUB_DEBUG
5051 &total_objects_attr
.attr
,
5053 &sanity_checks_attr
.attr
,
5055 &red_zone_attr
.attr
,
5057 &store_user_attr
.attr
,
5058 &validate_attr
.attr
,
5059 &alloc_calls_attr
.attr
,
5060 &free_calls_attr
.attr
,
5062 #ifdef CONFIG_ZONE_DMA
5063 &cache_dma_attr
.attr
,
5066 &remote_node_defrag_ratio_attr
.attr
,
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
,
5096 #ifdef CONFIG_FAILSLAB
5097 &failslab_attr
.attr
,
5103 static struct attribute_group slab_attr_group
= {
5104 .attrs
= slab_attrs
,
5107 static ssize_t
slab_attr_show(struct kobject
*kobj
,
5108 struct attribute
*attr
,
5111 struct slab_attribute
*attribute
;
5112 struct kmem_cache
*s
;
5115 attribute
= to_slab_attr(attr
);
5118 if (!attribute
->show
)
5121 err
= attribute
->show(s
, buf
);
5126 static ssize_t
slab_attr_store(struct kobject
*kobj
,
5127 struct attribute
*attr
,
5128 const char *buf
, size_t len
)
5130 struct slab_attribute
*attribute
;
5131 struct kmem_cache
*s
;
5134 attribute
= to_slab_attr(attr
);
5137 if (!attribute
->store
)
5140 err
= attribute
->store(s
, buf
, len
);
5141 #ifdef CONFIG_MEMCG_KMEM
5142 if (slab_state
>= FULL
&& err
>= 0 && is_root_cache(s
)) {
5145 mutex_lock(&slab_mutex
);
5146 if (s
->max_attr_size
< len
)
5147 s
->max_attr_size
= len
;
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.
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
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.
5166 for_each_memcg_cache_index(i
) {
5167 struct kmem_cache
*c
= cache_from_memcg(s
, i
);
5169 attribute
->store(c
, buf
, len
);
5171 mutex_unlock(&slab_mutex
);
5177 static void memcg_propagate_slab_attrs(struct kmem_cache
*s
)
5179 #ifdef CONFIG_MEMCG_KMEM
5181 char *buffer
= NULL
;
5183 if (!is_root_cache(s
))
5187 * This mean this cache had no attribute written. Therefore, no point
5188 * in copying default values around
5190 if (!s
->max_attr_size
)
5193 for (i
= 0; i
< ARRAY_SIZE(slab_attrs
); i
++) {
5196 struct slab_attribute
*attr
= to_slab_attr(slab_attrs
[i
]);
5198 if (!attr
|| !attr
->store
|| !attr
->show
)
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.
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.
5212 else if (s
->max_attr_size
< ARRAY_SIZE(mbuf
))
5215 buffer
= (char *) get_zeroed_page(GFP_KERNEL
);
5216 if (WARN_ON(!buffer
))
5221 attr
->show(s
->memcg_params
->root_cache
, buf
);
5222 attr
->store(s
, buf
, strlen(buf
));
5226 free_page((unsigned long)buffer
);
5230 static const struct sysfs_ops slab_sysfs_ops
= {
5231 .show
= slab_attr_show
,
5232 .store
= slab_attr_store
,
5235 static struct kobj_type slab_ktype
= {
5236 .sysfs_ops
= &slab_sysfs_ops
,
5239 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
5241 struct kobj_type
*ktype
= get_ktype(kobj
);
5243 if (ktype
== &slab_ktype
)
5248 static const struct kset_uevent_ops slab_uevent_ops
= {
5249 .filter
= uevent_filter
,
5252 static struct kset
*slab_kset
;
5254 #define ID_STR_LENGTH 64
5256 /* Create a unique string id for a slab cache:
5258 * Format :[flags-]size
5260 static char *create_unique_id(struct kmem_cache
*s
)
5262 char *name
= kmalloc(ID_STR_LENGTH
, GFP_KERNEL
);
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
5275 if (s
->flags
& SLAB_CACHE_DMA
)
5277 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
5279 if (s
->flags
& SLAB_DEBUG_FREE
)
5281 if (!(s
->flags
& SLAB_NOTRACK
))
5285 p
+= sprintf(p
, "%07d", s
->size
);
5287 #ifdef CONFIG_MEMCG_KMEM
5288 if (!is_root_cache(s
))
5289 p
+= sprintf(p
, "-%08d", memcg_cache_id(s
->memcg_params
->memcg
));
5292 BUG_ON(p
> name
+ ID_STR_LENGTH
- 1);
5296 static int sysfs_slab_add(struct kmem_cache
*s
)
5300 int unmergeable
= slab_unmergeable(s
);
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.
5308 sysfs_remove_link(&slab_kset
->kobj
, s
->name
);
5312 * Create a unique name for the slab as a target
5315 name
= create_unique_id(s
);
5318 s
->kobj
.kset
= slab_kset
;
5319 err
= kobject_init_and_add(&s
->kobj
, &slab_ktype
, NULL
, name
);
5321 kobject_put(&s
->kobj
);
5325 err
= sysfs_create_group(&s
->kobj
, &slab_attr_group
);
5327 kobject_del(&s
->kobj
);
5328 kobject_put(&s
->kobj
);
5331 kobject_uevent(&s
->kobj
, KOBJ_ADD
);
5333 /* Setup first alias */
5334 sysfs_slab_alias(s
, s
->name
);
5340 static void sysfs_slab_remove(struct kmem_cache
*s
)
5342 if (slab_state
< FULL
)
5344 * Sysfs has not been setup yet so no need to remove the
5349 kobject_uevent(&s
->kobj
, KOBJ_REMOVE
);
5350 kobject_del(&s
->kobj
);
5351 kobject_put(&s
->kobj
);
5355 * Need to buffer aliases during bootup until sysfs becomes
5356 * available lest we lose that information.
5358 struct saved_alias
{
5359 struct kmem_cache
*s
;
5361 struct saved_alias
*next
;
5364 static struct saved_alias
*alias_list
;
5366 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *name
)
5368 struct saved_alias
*al
;
5370 if (slab_state
== FULL
) {
5372 * If we have a leftover link then remove it.
5374 sysfs_remove_link(&slab_kset
->kobj
, name
);
5375 return sysfs_create_link(&slab_kset
->kobj
, &s
->kobj
, name
);
5378 al
= kmalloc(sizeof(struct saved_alias
), GFP_KERNEL
);
5384 al
->next
= alias_list
;
5389 static int __init
slab_sysfs_init(void)
5391 struct kmem_cache
*s
;
5394 mutex_lock(&slab_mutex
);
5396 slab_kset
= kset_create_and_add("slab", &slab_uevent_ops
, kernel_kobj
);
5398 mutex_unlock(&slab_mutex
);
5399 printk(KERN_ERR
"Cannot register slab subsystem.\n");
5405 list_for_each_entry(s
, &slab_caches
, list
) {
5406 err
= sysfs_slab_add(s
);
5408 printk(KERN_ERR
"SLUB: Unable to add boot slab %s"
5409 " to sysfs\n", s
->name
);
5412 while (alias_list
) {
5413 struct saved_alias
*al
= alias_list
;
5415 alias_list
= alias_list
->next
;
5416 err
= sysfs_slab_alias(al
->s
, al
->name
);
5418 printk(KERN_ERR
"SLUB: Unable to add boot slab alias"
5419 " %s to sysfs\n", al
->name
);
5423 mutex_unlock(&slab_mutex
);
5428 __initcall(slab_sysfs_init
);
5429 #endif /* CONFIG_SYSFS */
5432 * The /proc/slabinfo ABI
5434 #ifdef CONFIG_SLABINFO
5435 void get_slabinfo(struct kmem_cache
*s
, struct slabinfo
*sinfo
)
5437 unsigned long nr_partials
= 0;
5438 unsigned long nr_slabs
= 0;
5439 unsigned long nr_objs
= 0;
5440 unsigned long nr_free
= 0;
5443 for_each_online_node(node
) {
5444 struct kmem_cache_node
*n
= get_node(s
, node
);
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
);
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
);
5463 void slabinfo_show_stats(struct seq_file
*m
, struct kmem_cache
*s
)
5467 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
5468 size_t count
, loff_t
*ppos
)
5472 #endif /* CONFIG_SLABINFO */