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>
36 #include <linux/aee.h>
38 #include <trace/events/kmem.h>
39 #include <mach/mtk_memcfg.h>
47 pr_alert("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
54 * 1. slab_mutex (Global Mutex)
56 * 3. slab_lock(page) (Only on some arches and for debugging)
60 * The role of the slab_mutex is to protect the list of all the slabs
61 * and to synchronize major metadata changes to slab cache structures.
63 * The slab_lock is only used for debugging and on arches that do not
64 * have the ability to do a cmpxchg_double. It only protects the second
65 * double word in the page struct. Meaning
66 * A. page->freelist -> List of object free in a page
67 * B. page->counters -> Counters of objects
68 * C. page->frozen -> frozen state
70 * If a slab is frozen then it is exempt from list management. It is not
71 * on any list. The processor that froze the slab is the one who can
72 * perform list operations on the page. Other processors may put objects
73 * onto the freelist but the processor that froze the slab is the only
74 * one that can retrieve the objects from the page's freelist.
76 * The list_lock protects the partial and full list on each node and
77 * the partial slab counter. If taken then no new slabs may be added or
78 * removed from the lists nor make the number of partial slabs be modified.
79 * (Note that the total number of slabs is an atomic value that may be
80 * modified without taking the list lock).
82 * The list_lock is a centralized lock and thus we avoid taking it as
83 * much as possible. As long as SLUB does not have to handle partial
84 * slabs, operations can continue without any centralized lock. F.e.
85 * allocating a long series of objects that fill up slabs does not require
87 * Interrupts are disabled during allocation and deallocation in order to
88 * make the slab allocator safe to use in the context of an irq. In addition
89 * interrupts are disabled to ensure that the processor does not change
90 * while handling per_cpu slabs, due to kernel preemption.
92 * SLUB assigns one slab for allocation to each processor.
93 * Allocations only occur from these slabs called cpu slabs.
95 * Slabs with free elements are kept on a partial list and during regular
96 * operations no list for full slabs is used. If an object in a full slab is
97 * freed then the slab will show up again on the partial lists.
98 * We track full slabs for debugging purposes though because otherwise we
99 * cannot scan all objects.
101 * Slabs are freed when they become empty. Teardown and setup is
102 * minimal so we rely on the page allocators per cpu caches for
103 * fast frees and allocs.
105 * Overloading of page flags that are otherwise used for LRU management.
107 * PageActive The slab is frozen and exempt from list processing.
108 * This means that the slab is dedicated to a purpose
109 * such as satisfying allocations for a specific
110 * processor. Objects may be freed in the slab while
111 * it is frozen but slab_free will then skip the usual
112 * list operations. It is up to the processor holding
113 * the slab to integrate the slab into the slab lists
114 * when the slab is no longer needed.
116 * One use of this flag is to mark slabs that are
117 * used for allocations. Then such a slab becomes a cpu
118 * slab. The cpu slab may be equipped with an additional
119 * freelist that allows lockless access to
120 * free objects in addition to the regular freelist
121 * that requires the slab lock.
123 * PageError Slab requires special handling due to debug
124 * options set. This moves slab handling out of
125 * the fast path and disables lockless freelists.
128 static inline int kmem_cache_debug(struct kmem_cache
*s
)
130 #ifdef CONFIG_SLUB_DEBUG
131 return unlikely(s
->flags
& SLAB_DEBUG_FLAGS
);
138 * Issues still to be resolved:
140 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
142 * - Variable sizing of the per node arrays
145 /* Enable to test recovery from slab corruption on boot */
146 #undef SLUB_RESILIENCY_TEST
148 /* Enable to log cmpxchg failures */
149 #undef SLUB_DEBUG_CMPXCHG
152 * Mininum number of partial slabs. These will be left on the partial
153 * lists even if they are empty. kmem_cache_shrink may reclaim them.
155 #define MIN_PARTIAL 5
158 * Maximum number of desirable partial slabs.
159 * The existence of more partial slabs makes kmem_cache_shrink
160 * sort the partial list by the number of objects in the.
162 #define MAX_PARTIAL 10
164 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
165 SLAB_POISON | SLAB_STORE_USER)
168 * Debugging flags that require metadata to be stored in the slab. These get
169 * disabled when slub_debug=O is used and a cache's min order increases with
172 #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
175 * Set of flags that will prevent slab merging
177 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
178 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
181 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
182 SLAB_CACHE_DMA | SLAB_NOTRACK)
185 #define OO_MASK ((1 << OO_SHIFT) - 1)
186 #define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
188 /* Internal SLUB flags */
189 #define __OBJECT_POISON 0x80000000UL /* Poison object */
190 #define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
193 static struct notifier_block slab_notifier
;
197 * Tracking user of a slab.
199 #define TRACK_ADDRS_COUNT 16
201 unsigned long addr
; /* Called from address */
202 #ifdef CONFIG_STACKTRACE
203 unsigned long addrs
[TRACK_ADDRS_COUNT
]; /* Called from address */
205 int cpu
; /* Was running on cpu */
206 int pid
; /* Pid context */
207 unsigned long when
; /* When did the operation occur */
210 enum track_item
{ TRACK_FREE
, TRACK_ALLOC
};
213 static int sysfs_slab_add(struct kmem_cache
*);
214 static int sysfs_slab_alias(struct kmem_cache
*, const char *);
215 static void sysfs_slab_remove(struct kmem_cache
*);
216 static void memcg_propagate_slab_attrs(struct kmem_cache
*s
);
218 static inline int sysfs_slab_add(struct kmem_cache
*s
) { return 0; }
219 static inline int sysfs_slab_alias(struct kmem_cache
*s
, const char *p
)
221 static inline void sysfs_slab_remove(struct kmem_cache
*s
) { }
223 static inline void memcg_propagate_slab_attrs(struct kmem_cache
*s
) { }
226 static inline void stat(const struct kmem_cache
*s
, enum stat_item si
)
228 #ifdef CONFIG_SLUB_STATS
229 __this_cpu_inc(s
->cpu_slab
->stat
[si
]);
233 /********************************************************************
234 * Core slab cache functions
235 *******************************************************************/
237 static inline struct kmem_cache_node
*get_node(struct kmem_cache
*s
, int node
)
239 return s
->node
[node
];
242 /* Verify that a pointer has an address that is valid within a slab page */
243 static inline int check_valid_pointer(struct kmem_cache
*s
,
244 struct page
*page
, const void *object
)
251 base
= page_address(page
);
252 if (object
< base
|| object
>= base
+ page
->objects
* s
->size
||
253 (object
- base
) % s
->size
) {
260 static inline void *get_freepointer(struct kmem_cache
*s
, void *object
)
262 return *(void **)(object
+ s
->offset
);
265 static void prefetch_freepointer(const struct kmem_cache
*s
, void *object
)
267 prefetch(object
+ s
->offset
);
270 static inline void *get_freepointer_safe(struct kmem_cache
*s
, void *object
)
274 #ifdef CONFIG_DEBUG_PAGEALLOC
275 probe_kernel_read(&p
, (void **)(object
+ s
->offset
), sizeof(p
));
277 p
= get_freepointer(s
, object
);
282 static inline void set_freepointer(struct kmem_cache
*s
, void *object
, void *fp
)
284 *(void **)(object
+ s
->offset
) = fp
;
287 /* Loop over all objects in a slab */
288 #define for_each_object(__p, __s, __addr, __objects) \
289 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
292 /* Determine object index from a given position */
293 static inline int slab_index(void *p
, struct kmem_cache
*s
, void *addr
)
295 return (p
- addr
) / s
->size
;
298 static inline size_t slab_ksize(const struct kmem_cache
*s
)
300 #ifdef CONFIG_SLUB_DEBUG
302 * Debugging requires use of the padding between object
303 * and whatever may come after it.
305 if (s
->flags
& (SLAB_RED_ZONE
| SLAB_POISON
))
306 return s
->object_size
;
310 * If we have the need to store the freelist pointer
311 * back there or track user information then we can
312 * only use the space before that information.
314 if (s
->flags
& (SLAB_DESTROY_BY_RCU
| SLAB_STORE_USER
))
317 * Else we can use all the padding etc for the allocation
322 static inline int order_objects(int order
, unsigned long size
, int reserved
)
324 return ((PAGE_SIZE
<< order
) - reserved
) / size
;
327 static inline struct kmem_cache_order_objects
oo_make(int order
,
328 unsigned long size
, int reserved
)
330 struct kmem_cache_order_objects x
= {
331 (order
<< OO_SHIFT
) + order_objects(order
, size
, reserved
)
337 static inline int oo_order(struct kmem_cache_order_objects x
)
339 return x
.x
>> OO_SHIFT
;
342 static inline int oo_objects(struct kmem_cache_order_objects x
)
344 return x
.x
& OO_MASK
;
348 * Per slab locking using the pagelock
350 static __always_inline
void slab_lock(struct page
*page
)
352 bit_spin_lock(PG_locked
, &page
->flags
);
355 static __always_inline
void slab_unlock(struct page
*page
)
357 __bit_spin_unlock(PG_locked
, &page
->flags
);
360 static inline void set_page_slub_counters(struct page
*page
, unsigned long counters_new
)
363 tmp
.counters
= counters_new
;
365 * page->counters can cover frozen/inuse/objects as well
366 * as page->_count. If we assign to ->counters directly
367 * we run the risk of losing updates to page->_count, so
368 * be careful and only assign to the fields we need.
370 page
->frozen
= tmp
.frozen
;
371 page
->inuse
= tmp
.inuse
;
372 page
->objects
= tmp
.objects
;
375 /* Interrupts must be disabled (for the fallback code to work right) */
376 static inline bool __cmpxchg_double_slab(struct kmem_cache
*s
, struct page
*page
,
377 void *freelist_old
, unsigned long counters_old
,
378 void *freelist_new
, unsigned long counters_new
,
381 VM_BUG_ON(!irqs_disabled());
382 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
383 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
384 if (s
->flags
& __CMPXCHG_DOUBLE
) {
385 if (cmpxchg_double(&page
->freelist
, &page
->counters
,
386 freelist_old
, counters_old
,
387 freelist_new
, counters_new
))
393 if (page
->freelist
== freelist_old
&& page
->counters
== counters_old
) {
394 page
->freelist
= freelist_new
;
395 set_page_slub_counters(page
, counters_new
);
403 stat(s
, CMPXCHG_DOUBLE_FAIL
);
405 #ifdef SLUB_DEBUG_CMPXCHG
406 printk(KERN_INFO
"%s %s: cmpxchg double redo ", n
, s
->name
);
412 static inline bool cmpxchg_double_slab(struct kmem_cache
*s
, struct page
*page
,
413 void *freelist_old
, unsigned long counters_old
,
414 void *freelist_new
, unsigned long counters_new
,
417 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
418 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
419 if (s
->flags
& __CMPXCHG_DOUBLE
) {
420 if (cmpxchg_double(&page
->freelist
, &page
->counters
,
421 freelist_old
, counters_old
,
422 freelist_new
, counters_new
))
429 local_irq_save(flags
);
431 if (page
->freelist
== freelist_old
&& page
->counters
== counters_old
) {
432 page
->freelist
= freelist_new
;
433 set_page_slub_counters(page
, counters_new
);
435 local_irq_restore(flags
);
439 local_irq_restore(flags
);
443 stat(s
, CMPXCHG_DOUBLE_FAIL
);
445 #ifdef SLUB_DEBUG_CMPXCHG
446 printk(KERN_INFO
"%s %s: cmpxchg double redo ", n
, s
->name
);
452 #ifdef CONFIG_SLUB_DEBUG
454 * Determine a map of object in use on a page.
456 * Node listlock must be held to guarantee that the page does
457 * not vanish from under us.
459 static void get_map(struct kmem_cache
*s
, struct page
*page
, unsigned long *map
)
462 void *addr
= page_address(page
);
464 for (p
= page
->freelist
; p
; p
= get_freepointer(s
, p
))
465 set_bit(slab_index(p
, s
, addr
), map
);
471 #ifdef CONFIG_SLUB_DEBUG_ON
472 static int slub_debug
= DEBUG_DEFAULT_FLAGS
;
474 static int slub_debug
;
477 static char *slub_debug_slabs
;
478 static int disable_higher_order_debug
;
483 static void print_section(char *text
, u8
*addr
, unsigned int length
)
485 print_hex_dump(KERN_ERR
, text
, DUMP_PREFIX_ADDRESS
, 16, 1, addr
,
489 static struct track
*get_track(struct kmem_cache
*s
, void *object
,
490 enum track_item alloc
)
495 p
= object
+ s
->offset
+ sizeof(void *);
497 p
= object
+ s
->inuse
;
502 static void set_track(struct kmem_cache
*s
, void *object
,
503 enum track_item alloc
, unsigned long addr
)
505 struct track
*p
= get_track(s
, object
, alloc
);
508 #ifdef CONFIG_STACKTRACE
509 struct stack_trace trace
;
512 trace
.nr_entries
= 0;
513 trace
.max_entries
= TRACK_ADDRS_COUNT
;
514 trace
.entries
= p
->addrs
;
516 save_stack_trace(&trace
);
518 /* See rant in lockdep.c */
519 if (trace
.nr_entries
!= 0 &&
520 trace
.entries
[trace
.nr_entries
- 1] == ULONG_MAX
)
523 for (i
= trace
.nr_entries
; i
< TRACK_ADDRS_COUNT
; i
++)
527 p
->cpu
= smp_processor_id();
528 p
->pid
= current
->pid
;
531 memset(p
, 0, sizeof(struct track
));
534 static void init_tracking(struct kmem_cache
*s
, void *object
)
536 if (!(s
->flags
& SLAB_STORE_USER
))
539 set_track(s
, object
, TRACK_FREE
, 0UL);
540 set_track(s
, object
, TRACK_ALLOC
, 0UL);
543 static void print_track(const char *s
, struct track
*t
)
548 printk(KERN_ERR
"INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
549 s
, (void *)t
->addr
, jiffies
- t
->when
, t
->cpu
, t
->pid
);
550 #ifdef CONFIG_STACKTRACE
553 for (i
= 0; i
< TRACK_ADDRS_COUNT
; i
++)
555 printk(KERN_ERR
"\t%pS\n", (void *)t
->addrs
[i
]);
562 static void print_tracking(struct kmem_cache
*s
, void *object
)
564 if (!(s
->flags
& SLAB_STORE_USER
))
567 print_track("Allocated", get_track(s
, object
, TRACK_ALLOC
));
568 print_track("Freed", get_track(s
, object
, TRACK_FREE
));
571 static void print_page_info(struct page
*page
)
573 printk(KERN_ERR
"INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
574 page
, page
->objects
, page
->inuse
, page
->freelist
, page
->flags
);
578 static void slab_bug(struct kmem_cache
*s
, char *fmt
, ...)
584 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
586 printk(KERN_ERR
"========================================"
587 "=====================================\n");
588 printk(KERN_ERR
"BUG %s (%s): %s\n", s
->name
, print_tainted(), buf
);
589 printk(KERN_ERR
"----------------------------------------"
590 "-------------------------------------\n\n");
592 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
595 static void slab_fix(struct kmem_cache
*s
, char *fmt
, ...)
601 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
603 printk(KERN_ERR
"FIX %s: %s\n", s
->name
, buf
);
606 static void print_trailer(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
608 unsigned int off
; /* Offset of last byte */
609 u8
*addr
= page_address(page
);
611 print_tracking(s
, p
);
613 print_page_info(page
);
615 printk(KERN_ERR
"INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
616 p
, p
- addr
, get_freepointer(s
, p
));
619 print_section("Bytes b4 ", p
- 16, 16);
621 print_section("Object ", p
, min_t(unsigned long, s
->object_size
,
623 if (s
->flags
& SLAB_RED_ZONE
)
624 print_section("Redzone ", p
+ s
->object_size
,
625 s
->inuse
- s
->object_size
);
628 off
= s
->offset
+ sizeof(void *);
632 if (s
->flags
& SLAB_STORE_USER
)
633 off
+= 2 * sizeof(struct track
);
636 /* Beginning of the filler is the free pointer */
637 print_section("Padding ", p
+ off
, s
->size
- off
);
642 static void object_err(struct kmem_cache
*s
, struct page
*page
,
643 u8
*object
, char *reason
)
645 slab_bug(s
, "%s", reason
);
646 print_trailer(s
, page
, object
);
650 static void slab_err(struct kmem_cache
*s
, struct page
*page
, const char *fmt
, ...)
656 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
658 slab_bug(s
, "%s", buf
);
659 print_page_info(page
);
664 static void init_object(struct kmem_cache
*s
, void *object
, u8 val
)
668 if (s
->flags
& __OBJECT_POISON
) {
669 memset(p
, POISON_FREE
, s
->object_size
- 1);
670 p
[s
->object_size
- 1] = POISON_END
;
673 if (s
->flags
& SLAB_RED_ZONE
)
674 memset(p
+ s
->object_size
, val
, s
->inuse
- s
->object_size
);
677 static void restore_bytes(struct kmem_cache
*s
, char *message
, u8 data
,
678 void *from
, void *to
)
680 slab_fix(s
, "Restoring 0x%p-0x%p=0x%x\n", from
, to
- 1, data
);
681 memset(from
, data
, to
- from
);
684 static int check_bytes_and_report(struct kmem_cache
*s
, struct page
*page
,
685 u8
*object
, char *what
,
686 u8
*start
, unsigned int value
, unsigned int bytes
)
691 fault
= memchr_inv(start
, value
, bytes
);
696 while (end
> fault
&& end
[-1] == value
)
699 slab_bug(s
, "%s overwritten", what
);
700 printk(KERN_ERR
"INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
701 fault
, end
- 1, fault
[0], value
);
702 print_trailer(s
, page
, object
);
704 /* trigger BUG before restore_bytes */
706 restore_bytes(s
, what
, value
, fault
, end
);
715 * Bytes of the object to be managed.
716 * If the freepointer may overlay the object then the free
717 * pointer is the first word of the object.
719 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
722 * object + s->object_size
723 * Padding to reach word boundary. This is also used for Redzoning.
724 * Padding is extended by another word if Redzoning is enabled and
725 * object_size == inuse.
727 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
728 * 0xcc (RED_ACTIVE) for objects in use.
731 * Meta data starts here.
733 * A. Free pointer (if we cannot overwrite object on free)
734 * B. Tracking data for SLAB_STORE_USER
735 * C. Padding to reach required alignment boundary or at mininum
736 * one word if debugging is on to be able to detect writes
737 * before the word boundary.
739 * Padding is done using 0x5a (POISON_INUSE)
742 * Nothing is used beyond s->size.
744 * If slabcaches are merged then the object_size and inuse boundaries are mostly
745 * ignored. And therefore no slab options that rely on these boundaries
746 * may be used with merged slabcaches.
749 static int check_pad_bytes(struct kmem_cache
*s
, struct page
*page
, u8
*p
)
751 unsigned long off
= s
->inuse
; /* The end of info */
754 /* Freepointer is placed after the object. */
755 off
+= sizeof(void *);
757 if (s
->flags
& SLAB_STORE_USER
)
758 /* We also have user information there */
759 off
+= 2 * sizeof(struct track
);
764 return check_bytes_and_report(s
, page
, p
, "Object padding",
765 p
+ off
, POISON_INUSE
, s
->size
- off
);
768 /* Check the pad bytes at the end of a slab page */
769 static int slab_pad_check(struct kmem_cache
*s
, struct page
*page
)
777 if (!(s
->flags
& SLAB_POISON
))
780 start
= page_address(page
);
781 length
= (PAGE_SIZE
<< compound_order(page
)) - s
->reserved
;
782 end
= start
+ length
;
783 remainder
= length
% s
->size
;
787 fault
= memchr_inv(end
- remainder
, POISON_INUSE
, remainder
);
790 while (end
> fault
&& end
[-1] == POISON_INUSE
)
793 slab_err(s
, page
, "Padding overwritten. 0x%p-0x%p", fault
, end
- 1);
794 print_section("Padding ", end
- remainder
, remainder
);
796 restore_bytes(s
, "slab padding", POISON_INUSE
, end
- remainder
, end
);
800 static int check_object(struct kmem_cache
*s
, struct page
*page
,
801 void *object
, u8 val
)
804 u8
*endobject
= object
+ s
->object_size
;
806 if (s
->flags
& SLAB_RED_ZONE
) {
807 if (!check_bytes_and_report(s
, page
, object
, "Redzone",
808 endobject
, val
, s
->inuse
- s
->object_size
))
811 if ((s
->flags
& SLAB_POISON
) && s
->object_size
< s
->inuse
) {
812 check_bytes_and_report(s
, page
, p
, "Alignment padding",
813 endobject
, POISON_INUSE
, s
->inuse
- s
->object_size
);
817 if (s
->flags
& SLAB_POISON
) {
818 if (val
!= SLUB_RED_ACTIVE
&& (s
->flags
& __OBJECT_POISON
) &&
819 (!check_bytes_and_report(s
, page
, p
, "Poison", p
,
820 POISON_FREE
, s
->object_size
- 1) ||
821 !check_bytes_and_report(s
, page
, p
, "Poison",
822 p
+ s
->object_size
- 1, POISON_END
, 1)))
825 * check_pad_bytes cleans up on its own.
827 check_pad_bytes(s
, page
, p
);
830 if (!s
->offset
&& val
== SLUB_RED_ACTIVE
)
832 * Object and freepointer overlap. Cannot check
833 * freepointer while object is allocated.
837 /* Check free pointer validity */
838 if (!check_valid_pointer(s
, page
, get_freepointer(s
, p
))) {
839 object_err(s
, page
, p
, "Freepointer corrupt");
841 * No choice but to zap it and thus lose the remainder
842 * of the free objects in this slab. May cause
843 * another error because the object count is now wrong.
845 set_freepointer(s
, p
, NULL
);
851 static int check_slab(struct kmem_cache
*s
, struct page
*page
)
855 VM_BUG_ON(!irqs_disabled());
857 if (!PageSlab(page
)) {
858 slab_err(s
, page
, "Not a valid slab page");
862 maxobj
= order_objects(compound_order(page
), s
->size
, s
->reserved
);
863 if (page
->objects
> maxobj
) {
864 pr_alert("page->objects: %d, maxobj: %d, comporder: %d", page
->objects
,
865 maxobj
, compound_order(page
));
866 pr_alert("s->size %d, s->reserved: %d", s
->size
, s
->reserved
);
867 print_section("page: ", (void *)page
, sizeof(struct page
));
868 print_section("kmem_cache: ", (void *)s
, sizeof(struct kmem_cache
));
869 slab_err(s
, page
, "objects %u > max %u",
870 page
->objects
, maxobj
);
873 if (page
->inuse
> page
->objects
) {
874 slab_err(s
, page
, "inuse %u > max %u",
875 page
->inuse
, page
->objects
);
878 /* Slab_pad_check fixes things up after itself */
879 slab_pad_check(s
, page
);
884 * Determine if a certain object on a page is on the freelist. Must hold the
885 * slab lock to guarantee that the chains are in a consistent state.
887 static int on_freelist(struct kmem_cache
*s
, struct page
*page
, void *search
)
892 unsigned long max_objects
;
895 while (fp
&& nr
<= page
->objects
) {
898 if (!check_valid_pointer(s
, page
, fp
)) {
900 object_err(s
, page
, object
,
901 "Freechain corrupt");
902 set_freepointer(s
, object
, NULL
);
905 slab_err(s
, page
, "Freepointer corrupt");
906 page
->freelist
= NULL
;
907 page
->inuse
= page
->objects
;
908 slab_fix(s
, "Freelist cleared");
914 fp
= get_freepointer(s
, object
);
918 max_objects
= order_objects(compound_order(page
), s
->size
, s
->reserved
);
919 if (max_objects
> MAX_OBJS_PER_PAGE
)
920 max_objects
= MAX_OBJS_PER_PAGE
;
922 if (page
->objects
!= max_objects
) {
923 slab_err(s
, page
, "Wrong number of objects. Found %d but "
924 "should be %d", page
->objects
, max_objects
);
925 page
->objects
= max_objects
;
926 slab_fix(s
, "Number of objects adjusted.");
928 if (page
->inuse
!= page
->objects
- nr
) {
929 slab_err(s
, page
, "Wrong object count. Counter is %d but "
930 "counted were %d", page
->inuse
, page
->objects
- nr
);
931 page
->inuse
= page
->objects
- nr
;
932 slab_fix(s
, "Object count adjusted.");
934 return search
== NULL
;
937 static void trace(struct kmem_cache
*s
, struct page
*page
, void *object
,
940 if (s
->flags
& SLAB_TRACE
) {
941 printk(KERN_INFO
"TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
943 alloc
? "alloc" : "free",
948 print_section("Object ", (void *)object
, s
->object_size
);
955 * Hooks for other subsystems that check memory allocations. In a typical
956 * production configuration these hooks all should produce no code at all.
958 static inline int slab_pre_alloc_hook(struct kmem_cache
*s
, gfp_t flags
)
960 flags
&= gfp_allowed_mask
;
961 lockdep_trace_alloc(flags
);
962 might_sleep_if(flags
& __GFP_WAIT
);
964 return should_failslab(s
->object_size
, flags
, s
->flags
);
967 static inline void slab_post_alloc_hook(struct kmem_cache
*s
, gfp_t flags
, void *object
)
969 flags
&= gfp_allowed_mask
;
970 kmemcheck_slab_alloc(s
, flags
, object
, slab_ksize(s
));
971 kmemleak_alloc_recursive(object
, s
->object_size
, 1, s
->flags
, flags
);
974 static inline void slab_free_hook(struct kmem_cache
*s
, void *x
)
976 kmemleak_free_recursive(x
, s
->flags
);
979 * Trouble is that we may no longer disable interupts in the fast path
980 * So in order to make the debug calls that expect irqs to be
981 * disabled we need to disable interrupts temporarily.
983 #if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
987 local_irq_save(flags
);
988 kmemcheck_slab_free(s
, x
, s
->object_size
);
989 debug_check_no_locks_freed(x
, s
->object_size
);
990 local_irq_restore(flags
);
993 if (!(s
->flags
& SLAB_DEBUG_OBJECTS
))
994 debug_check_no_obj_freed(x
, s
->object_size
);
998 * Tracking of fully allocated slabs for debugging purposes.
1000 * list_lock must be held.
1002 static void add_full(struct kmem_cache
*s
,
1003 struct kmem_cache_node
*n
, struct page
*page
)
1005 if (!(s
->flags
& SLAB_STORE_USER
))
1008 list_add(&page
->lru
, &n
->full
);
1012 * list_lock must be held.
1014 static void remove_full(struct kmem_cache
*s
, struct page
*page
)
1016 if (!(s
->flags
& SLAB_STORE_USER
))
1019 list_del(&page
->lru
);
1022 /* Tracking of the number of slabs for debugging purposes */
1023 static inline unsigned long slabs_node(struct kmem_cache
*s
, int node
)
1025 struct kmem_cache_node
*n
= get_node(s
, node
);
1027 return atomic_long_read(&n
->nr_slabs
);
1030 static inline unsigned long node_nr_slabs(struct kmem_cache_node
*n
)
1032 return atomic_long_read(&n
->nr_slabs
);
1035 static inline void inc_slabs_node(struct kmem_cache
*s
, int node
, int objects
)
1037 struct kmem_cache_node
*n
= get_node(s
, node
);
1040 * May be called early in order to allocate a slab for the
1041 * kmem_cache_node structure. Solve the chicken-egg
1042 * dilemma by deferring the increment of the count during
1043 * bootstrap (see early_kmem_cache_node_alloc).
1046 atomic_long_inc(&n
->nr_slabs
);
1047 atomic_long_add(objects
, &n
->total_objects
);
1050 static inline void dec_slabs_node(struct kmem_cache
*s
, int node
, int objects
)
1052 struct kmem_cache_node
*n
= get_node(s
, node
);
1054 atomic_long_dec(&n
->nr_slabs
);
1055 atomic_long_sub(objects
, &n
->total_objects
);
1058 /* Object debug checks for alloc/free paths */
1059 static void setup_object_debug(struct kmem_cache
*s
, struct page
*page
,
1062 if (!(s
->flags
& (SLAB_STORE_USER
|SLAB_RED_ZONE
|__OBJECT_POISON
)))
1065 init_object(s
, object
, SLUB_RED_INACTIVE
);
1066 init_tracking(s
, object
);
1069 static noinline
int alloc_debug_processing(struct kmem_cache
*s
, struct page
*page
,
1070 void *object
, unsigned long addr
)
1072 if (!check_slab(s
, page
))
1075 if (!check_valid_pointer(s
, page
, object
)) {
1076 object_err(s
, page
, object
, "Freelist Pointer check fails");
1080 if (!check_object(s
, page
, object
, SLUB_RED_INACTIVE
))
1083 /* Success perform special debug activities for allocs */
1084 if (s
->flags
& SLAB_STORE_USER
)
1085 set_track(s
, object
, TRACK_ALLOC
, addr
);
1086 trace(s
, page
, object
, 1);
1087 init_object(s
, object
, SLUB_RED_ACTIVE
);
1091 if (PageSlab(page
)) {
1093 * If this is a slab page then lets do the best we can
1094 * to avoid issues in the future. Marking all objects
1095 * as used avoids touching the remaining objects.
1097 slab_fix(s
, "Marking all objects used");
1098 page
->inuse
= page
->objects
;
1099 page
->freelist
= NULL
;
1104 static noinline
struct kmem_cache_node
*free_debug_processing(
1105 struct kmem_cache
*s
, struct page
*page
, void *object
,
1106 unsigned long addr
, unsigned long *flags
)
1108 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
1110 spin_lock_irqsave(&n
->list_lock
, *flags
);
1113 if (!check_slab(s
, page
))
1116 if (!check_valid_pointer(s
, page
, object
)) {
1117 slab_err(s
, page
, "Invalid object pointer 0x%p", object
);
1121 if (on_freelist(s
, page
, object
)) {
1122 object_err(s
, page
, object
, "Object already free");
1126 if (!check_object(s
, page
, object
, SLUB_RED_ACTIVE
))
1129 if (unlikely(s
!= page
->slab_cache
)) {
1130 if (!PageSlab(page
)) {
1131 slab_err(s
, page
, "Attempt to free object(0x%p) "
1132 "outside of slab", object
);
1133 } else if (!page
->slab_cache
) {
1135 "SLUB <none>: no slab for object 0x%p.\n",
1139 object_err(s
, page
, object
,
1140 "page slab pointer corrupt.");
1144 if (s
->flags
& SLAB_STORE_USER
)
1145 set_track(s
, object
, TRACK_FREE
, addr
);
1146 trace(s
, page
, object
, 0);
1147 init_object(s
, object
, SLUB_RED_INACTIVE
);
1151 * Keep node_lock to preserve integrity
1152 * until the object is actually freed
1158 spin_unlock_irqrestore(&n
->list_lock
, *flags
);
1159 slab_fix(s
, "Object at 0x%p not freed", object
);
1163 static int __init
setup_slub_debug(char *str
)
1165 slub_debug
= DEBUG_DEFAULT_FLAGS
;
1166 if (*str
++ != '=' || !*str
)
1168 * No options specified. Switch on full debugging.
1174 * No options but restriction on slabs. This means full
1175 * debugging for slabs matching a pattern.
1179 if (tolower(*str
) == 'o') {
1181 * Avoid enabling debugging on caches if its minimum order
1182 * would increase as a result.
1184 disable_higher_order_debug
= 1;
1191 * Switch off all debugging measures.
1196 * Determine which debug features should be switched on
1198 for (; *str
&& *str
!= ','; str
++) {
1199 switch (tolower(*str
)) {
1201 slub_debug
|= SLAB_DEBUG_FREE
;
1204 slub_debug
|= SLAB_RED_ZONE
;
1207 slub_debug
|= SLAB_POISON
;
1210 slub_debug
|= SLAB_STORE_USER
;
1213 slub_debug
|= SLAB_TRACE
;
1216 slub_debug
|= SLAB_FAILSLAB
;
1219 printk(KERN_ERR
"slub_debug option '%c' "
1220 "unknown. skipped\n", *str
);
1226 slub_debug_slabs
= str
+ 1;
1231 __setup("slub_debug", setup_slub_debug
);
1233 static unsigned long kmem_cache_flags(unsigned long object_size
,
1234 unsigned long flags
, const char *name
,
1235 void (*ctor
)(void *))
1238 * Enable debugging if selected on the kernel commandline.
1240 if(flags
& SLAB_NO_DEBUG
) {
1244 if (slub_debug
&& (!slub_debug_slabs
|| (name
&&
1245 !strncmp(slub_debug_slabs
, name
, strlen(slub_debug_slabs
)))))
1246 flags
|= slub_debug
;
1251 static inline void setup_object_debug(struct kmem_cache
*s
,
1252 struct page
*page
, void *object
) {}
1254 static inline int alloc_debug_processing(struct kmem_cache
*s
,
1255 struct page
*page
, void *object
, unsigned long addr
) { return 0; }
1257 static inline struct kmem_cache_node
*free_debug_processing(
1258 struct kmem_cache
*s
, struct page
*page
, void *object
,
1259 unsigned long addr
, unsigned long *flags
) { return NULL
; }
1261 static inline int slab_pad_check(struct kmem_cache
*s
, struct page
*page
)
1263 static inline int check_object(struct kmem_cache
*s
, struct page
*page
,
1264 void *object
, u8 val
) { return 1; }
1265 static inline void add_full(struct kmem_cache
*s
, struct kmem_cache_node
*n
,
1266 struct page
*page
) {}
1267 static inline void remove_full(struct kmem_cache
*s
, struct page
*page
) {}
1268 static inline unsigned long kmem_cache_flags(unsigned long object_size
,
1269 unsigned long flags
, const char *name
,
1270 void (*ctor
)(void *))
1274 #define slub_debug 0
1276 #define disable_higher_order_debug 0
1278 static inline unsigned long slabs_node(struct kmem_cache
*s
, int node
)
1280 static inline unsigned long node_nr_slabs(struct kmem_cache_node
*n
)
1282 static inline void inc_slabs_node(struct kmem_cache
*s
, int node
,
1284 static inline void dec_slabs_node(struct kmem_cache
*s
, int node
,
1287 static inline int slab_pre_alloc_hook(struct kmem_cache
*s
, gfp_t flags
)
1290 static inline void slab_post_alloc_hook(struct kmem_cache
*s
, gfp_t flags
,
1293 static inline void slab_free_hook(struct kmem_cache
*s
, void *x
) {}
1295 #endif /* CONFIG_SLUB_DEBUG */
1298 * Slab allocation and freeing
1300 static inline struct page
*alloc_slab_page(gfp_t flags
, int node
,
1301 struct kmem_cache_order_objects oo
)
1303 int order
= oo_order(oo
);
1305 flags
|= __GFP_NOTRACK
;
1307 if (node
== NUMA_NO_NODE
)
1308 #ifndef CONFIG_MTK_PAGERECORDER
1309 return alloc_pages(flags
, order
);
1311 return alloc_pages_nopagedebug(flags
, order
);
1314 return alloc_pages_exact_node(node
, flags
, order
);
1317 static struct page
*allocate_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
1320 struct kmem_cache_order_objects oo
= s
->oo
;
1323 flags
&= gfp_allowed_mask
;
1325 if (flags
& __GFP_WAIT
)
1328 flags
|= s
->allocflags
;
1331 * Let the initial higher-order allocation fail under memory pressure
1332 * so we fall-back to the minimum order allocation.
1334 alloc_gfp
= (flags
| __GFP_NOWARN
| __GFP_NORETRY
) & ~__GFP_NOFAIL
;
1336 page
= alloc_slab_page(alloc_gfp
, node
, oo
);
1337 if (unlikely(!page
)) {
1340 * Allocation may have failed due to fragmentation.
1341 * Try a lower order alloc if possible
1343 page
= alloc_slab_page(flags
, node
, oo
);
1346 stat(s
, ORDER_FALLBACK
);
1349 if (kmemcheck_enabled
&& page
1350 && !(s
->flags
& (SLAB_NOTRACK
| DEBUG_DEFAULT_FLAGS
))) {
1351 int pages
= 1 << oo_order(oo
);
1353 kmemcheck_alloc_shadow(page
, oo_order(oo
), flags
, node
);
1356 * Objects from caches that have a constructor don't get
1357 * cleared when they're allocated, so we need to do it here.
1360 kmemcheck_mark_uninitialized_pages(page
, pages
);
1362 kmemcheck_mark_unallocated_pages(page
, pages
);
1365 if (flags
& __GFP_WAIT
)
1366 local_irq_disable();
1370 page
->objects
= oo_objects(oo
);
1371 mod_zone_page_state(page_zone(page
),
1372 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
1373 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
1379 static void setup_object(struct kmem_cache
*s
, struct page
*page
,
1382 setup_object_debug(s
, page
, object
);
1383 if (unlikely(s
->ctor
))
1387 static struct page
*new_slab(struct kmem_cache
*s
, gfp_t flags
, int node
)
1395 BUG_ON(flags
& GFP_SLAB_BUG_MASK
);
1397 page
= allocate_slab(s
,
1398 flags
& (GFP_RECLAIM_MASK
| GFP_CONSTRAINT_MASK
), node
);
1402 order
= compound_order(page
);
1403 inc_slabs_node(s
, page_to_nid(page
), page
->objects
);
1404 memcg_bind_pages(s
, order
);
1405 page
->slab_cache
= s
;
1406 __SetPageSlab(page
);
1407 if (page
->pfmemalloc
)
1408 SetPageSlabPfmemalloc(page
);
1410 start
= page_address(page
);
1412 if (unlikely(s
->flags
& SLAB_POISON
))
1413 memset(start
, POISON_INUSE
, PAGE_SIZE
<< order
);
1416 for_each_object(p
, s
, start
, page
->objects
) {
1417 setup_object(s
, page
, last
);
1418 set_freepointer(s
, last
, p
);
1421 setup_object(s
, page
, last
);
1422 set_freepointer(s
, last
, NULL
);
1424 page
->freelist
= start
;
1425 page
->inuse
= page
->objects
;
1431 static void __free_slab(struct kmem_cache
*s
, struct page
*page
)
1433 int order
= compound_order(page
);
1434 int pages
= 1 << order
;
1436 if (kmem_cache_debug(s
)) {
1439 slab_pad_check(s
, page
);
1440 for_each_object(p
, s
, page_address(page
),
1442 check_object(s
, page
, p
, SLUB_RED_INACTIVE
);
1445 kmemcheck_free_shadow(page
, compound_order(page
));
1447 mod_zone_page_state(page_zone(page
),
1448 (s
->flags
& SLAB_RECLAIM_ACCOUNT
) ?
1449 NR_SLAB_RECLAIMABLE
: NR_SLAB_UNRECLAIMABLE
,
1452 __ClearPageSlabPfmemalloc(page
);
1453 __ClearPageSlab(page
);
1455 memcg_release_pages(s
, order
);
1456 page_mapcount_reset(page
);
1457 if (current
->reclaim_state
)
1458 current
->reclaim_state
->reclaimed_slab
+= pages
;
1459 __free_memcg_kmem_pages(page
, order
);
1462 #define need_reserve_slab_rcu \
1463 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1465 static void rcu_free_slab(struct rcu_head
*h
)
1469 if (need_reserve_slab_rcu
)
1470 page
= virt_to_head_page(h
);
1472 page
= container_of((struct list_head
*)h
, struct page
, lru
);
1474 __free_slab(page
->slab_cache
, page
);
1477 static void free_slab(struct kmem_cache
*s
, struct page
*page
)
1479 if (unlikely(s
->flags
& SLAB_DESTROY_BY_RCU
)) {
1480 struct rcu_head
*head
;
1482 if (need_reserve_slab_rcu
) {
1483 int order
= compound_order(page
);
1484 int offset
= (PAGE_SIZE
<< order
) - s
->reserved
;
1486 VM_BUG_ON(s
->reserved
!= sizeof(*head
));
1487 head
= page_address(page
) + offset
;
1490 * RCU free overloads the RCU head over the LRU
1492 head
= (void *)&page
->lru
;
1495 call_rcu(head
, rcu_free_slab
);
1497 __free_slab(s
, page
);
1500 static void discard_slab(struct kmem_cache
*s
, struct page
*page
)
1502 dec_slabs_node(s
, page_to_nid(page
), page
->objects
);
1507 * Management of partially allocated slabs.
1509 * list_lock must be held.
1511 static inline void add_partial(struct kmem_cache_node
*n
,
1512 struct page
*page
, int tail
)
1515 if (tail
== DEACTIVATE_TO_TAIL
)
1516 list_add_tail(&page
->lru
, &n
->partial
);
1518 list_add(&page
->lru
, &n
->partial
);
1522 * list_lock must be held.
1524 static inline void remove_partial(struct kmem_cache_node
*n
,
1527 list_del(&page
->lru
);
1532 * Remove slab from the partial list, freeze it and
1533 * return the pointer to the freelist.
1535 * Returns a list of objects or NULL if it fails.
1537 * Must hold list_lock since we modify the partial list.
1539 static inline void *acquire_slab(struct kmem_cache
*s
,
1540 struct kmem_cache_node
*n
, struct page
*page
,
1541 int mode
, int *objects
)
1544 unsigned long counters
;
1548 * Zap the freelist and set the frozen bit.
1549 * The old freelist is the list of objects for the
1550 * per cpu allocation list.
1552 freelist
= page
->freelist
;
1553 counters
= page
->counters
;
1554 new.counters
= counters
;
1555 *objects
= new.objects
- new.inuse
;
1557 new.inuse
= page
->objects
;
1558 new.freelist
= NULL
;
1560 new.freelist
= freelist
;
1563 VM_BUG_ON(new.frozen
);
1566 if (!__cmpxchg_double_slab(s
, page
,
1568 new.freelist
, new.counters
,
1572 remove_partial(n
, page
);
1577 static void put_cpu_partial(struct kmem_cache
*s
, struct page
*page
, int drain
);
1578 static inline bool pfmemalloc_match(struct page
*page
, gfp_t gfpflags
);
1581 * Try to allocate a partial slab from a specific node.
1583 static void *get_partial_node(struct kmem_cache
*s
, struct kmem_cache_node
*n
,
1584 struct kmem_cache_cpu
*c
, gfp_t flags
)
1586 struct page
*page
, *page2
;
1587 void *object
= NULL
;
1592 * Racy check. If we mistakenly see no partial slabs then we
1593 * just allocate an empty slab. If we mistakenly try to get a
1594 * partial slab and there is none available then get_partials()
1597 if (!n
|| !n
->nr_partial
)
1600 spin_lock(&n
->list_lock
);
1601 list_for_each_entry_safe(page
, page2
, &n
->partial
, lru
) {
1604 if (!pfmemalloc_match(page
, flags
))
1607 t
= acquire_slab(s
, n
, page
, object
== NULL
, &objects
);
1611 available
+= objects
;
1614 stat(s
, ALLOC_FROM_PARTIAL
);
1617 put_cpu_partial(s
, page
, 0);
1618 stat(s
, CPU_PARTIAL_NODE
);
1620 if (kmem_cache_debug(s
) || available
> s
->cpu_partial
/ 2)
1624 spin_unlock(&n
->list_lock
);
1629 * Get a page from somewhere. Search in increasing NUMA distances.
1631 static void *get_any_partial(struct kmem_cache
*s
, gfp_t flags
,
1632 struct kmem_cache_cpu
*c
)
1635 struct zonelist
*zonelist
;
1638 enum zone_type high_zoneidx
= gfp_zone(flags
);
1640 unsigned int cpuset_mems_cookie
;
1643 * The defrag ratio allows a configuration of the tradeoffs between
1644 * inter node defragmentation and node local allocations. A lower
1645 * defrag_ratio increases the tendency to do local allocations
1646 * instead of attempting to obtain partial slabs from other nodes.
1648 * If the defrag_ratio is set to 0 then kmalloc() always
1649 * returns node local objects. If the ratio is higher then kmalloc()
1650 * may return off node objects because partial slabs are obtained
1651 * from other nodes and filled up.
1653 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
1654 * defrag_ratio = 1000) then every (well almost) allocation will
1655 * first attempt to defrag slab caches on other nodes. This means
1656 * scanning over all nodes to look for partial slabs which may be
1657 * expensive if we do it every time we are trying to find a slab
1658 * with available objects.
1660 if (!s
->remote_node_defrag_ratio
||
1661 get_cycles() % 1024 > s
->remote_node_defrag_ratio
)
1665 cpuset_mems_cookie
= get_mems_allowed();
1666 zonelist
= node_zonelist(slab_node(), flags
);
1667 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
1668 struct kmem_cache_node
*n
;
1670 n
= get_node(s
, zone_to_nid(zone
));
1672 if (n
&& cpuset_zone_allowed_hardwall(zone
, flags
) &&
1673 n
->nr_partial
> s
->min_partial
) {
1674 object
= get_partial_node(s
, n
, c
, flags
);
1677 * Return the object even if
1678 * put_mems_allowed indicated that
1679 * the cpuset mems_allowed was
1680 * updated in parallel. It's a
1681 * harmless race between the alloc
1682 * and the cpuset update.
1684 put_mems_allowed(cpuset_mems_cookie
);
1689 } while (!put_mems_allowed(cpuset_mems_cookie
));
1695 * Get a partial page, lock it and return it.
1697 static void *get_partial(struct kmem_cache
*s
, gfp_t flags
, int node
,
1698 struct kmem_cache_cpu
*c
)
1701 int searchnode
= (node
== NUMA_NO_NODE
) ? numa_node_id() : node
;
1703 object
= get_partial_node(s
, get_node(s
, searchnode
), c
, flags
);
1704 if (object
|| node
!= NUMA_NO_NODE
)
1707 return get_any_partial(s
, flags
, c
);
1710 #ifdef CONFIG_PREEMPT
1712 * Calculate the next globally unique transaction for disambiguiation
1713 * during cmpxchg. The transactions start with the cpu number and are then
1714 * incremented by CONFIG_NR_CPUS.
1716 #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1719 * No preemption supported therefore also no need to check for
1725 static inline unsigned long next_tid(unsigned long tid
)
1727 return tid
+ TID_STEP
;
1730 static inline unsigned int tid_to_cpu(unsigned long tid
)
1732 return tid
% TID_STEP
;
1735 static inline unsigned long tid_to_event(unsigned long tid
)
1737 return tid
/ TID_STEP
;
1740 static inline unsigned int init_tid(int cpu
)
1745 static inline void note_cmpxchg_failure(const char *n
,
1746 const struct kmem_cache
*s
, unsigned long tid
)
1748 #ifdef SLUB_DEBUG_CMPXCHG
1749 unsigned long actual_tid
= __this_cpu_read(s
->cpu_slab
->tid
);
1751 printk(KERN_INFO
"%s %s: cmpxchg redo ", n
, s
->name
);
1753 #ifdef CONFIG_PREEMPT
1754 if (tid_to_cpu(tid
) != tid_to_cpu(actual_tid
))
1755 printk("due to cpu change %d -> %d\n",
1756 tid_to_cpu(tid
), tid_to_cpu(actual_tid
));
1759 if (tid_to_event(tid
) != tid_to_event(actual_tid
))
1760 printk("due to cpu running other code. Event %ld->%ld\n",
1761 tid_to_event(tid
), tid_to_event(actual_tid
));
1763 printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
1764 actual_tid
, tid
, next_tid(tid
));
1766 stat(s
, CMPXCHG_DOUBLE_CPU_FAIL
);
1769 static void init_kmem_cache_cpus(struct kmem_cache
*s
)
1773 for_each_possible_cpu(cpu
)
1774 per_cpu_ptr(s
->cpu_slab
, cpu
)->tid
= init_tid(cpu
);
1778 * Remove the cpu slab
1780 static void deactivate_slab(struct kmem_cache
*s
, struct page
*page
, void *freelist
)
1782 enum slab_modes
{ M_NONE
, M_PARTIAL
, M_FULL
, M_FREE
};
1783 struct kmem_cache_node
*n
= get_node(s
, page_to_nid(page
));
1785 enum slab_modes l
= M_NONE
, m
= M_NONE
;
1787 int tail
= DEACTIVATE_TO_HEAD
;
1791 if (page
->freelist
) {
1792 stat(s
, DEACTIVATE_REMOTE_FREES
);
1793 tail
= DEACTIVATE_TO_TAIL
;
1797 * Stage one: Free all available per cpu objects back
1798 * to the page freelist while it is still frozen. Leave the
1801 * There is no need to take the list->lock because the page
1804 while (freelist
&& (nextfree
= get_freepointer(s
, freelist
))) {
1806 unsigned long counters
;
1809 prior
= page
->freelist
;
1810 counters
= page
->counters
;
1811 set_freepointer(s
, freelist
, prior
);
1812 new.counters
= counters
;
1814 VM_BUG_ON(!new.frozen
);
1816 } while (!__cmpxchg_double_slab(s
, page
,
1818 freelist
, new.counters
,
1819 "drain percpu freelist"));
1821 freelist
= nextfree
;
1825 * Stage two: Ensure that the page is unfrozen while the
1826 * list presence reflects the actual number of objects
1829 * We setup the list membership and then perform a cmpxchg
1830 * with the count. If there is a mismatch then the page
1831 * is not unfrozen but the page is on the wrong list.
1833 * Then we restart the process which may have to remove
1834 * the page from the list that we just put it on again
1835 * because the number of objects in the slab may have
1840 old
.freelist
= page
->freelist
;
1841 old
.counters
= page
->counters
;
1842 VM_BUG_ON(!old
.frozen
);
1844 /* Determine target state of the slab */
1845 new.counters
= old
.counters
;
1848 set_freepointer(s
, freelist
, old
.freelist
);
1849 new.freelist
= freelist
;
1851 new.freelist
= old
.freelist
;
1855 if (!new.inuse
&& n
->nr_partial
> s
->min_partial
)
1857 else if (new.freelist
) {
1862 * Taking the spinlock removes the possiblity
1863 * that acquire_slab() will see a slab page that
1866 spin_lock(&n
->list_lock
);
1870 if (kmem_cache_debug(s
) && !lock
) {
1873 * This also ensures that the scanning of full
1874 * slabs from diagnostic functions will not see
1877 spin_lock(&n
->list_lock
);
1885 remove_partial(n
, page
);
1887 else if (l
== M_FULL
)
1889 remove_full(s
, page
);
1891 if (m
== M_PARTIAL
) {
1893 add_partial(n
, page
, tail
);
1896 } else if (m
== M_FULL
) {
1898 stat(s
, DEACTIVATE_FULL
);
1899 add_full(s
, n
, page
);
1905 if (!__cmpxchg_double_slab(s
, page
,
1906 old
.freelist
, old
.counters
,
1907 new.freelist
, new.counters
,
1912 spin_unlock(&n
->list_lock
);
1915 stat(s
, DEACTIVATE_EMPTY
);
1916 discard_slab(s
, page
);
1922 * Unfreeze all the cpu partial slabs.
1924 * This function must be called with interrupts disabled
1925 * for the cpu using c (or some other guarantee must be there
1926 * to guarantee no concurrent accesses).
1928 static void unfreeze_partials(struct kmem_cache
*s
,
1929 struct kmem_cache_cpu
*c
)
1931 struct kmem_cache_node
*n
= NULL
, *n2
= NULL
;
1932 struct page
*page
, *discard_page
= NULL
;
1934 while ((page
= c
->partial
)) {
1938 c
->partial
= page
->next
;
1940 n2
= get_node(s
, page_to_nid(page
));
1943 spin_unlock(&n
->list_lock
);
1946 spin_lock(&n
->list_lock
);
1951 old
.freelist
= page
->freelist
;
1952 old
.counters
= page
->counters
;
1953 VM_BUG_ON(!old
.frozen
);
1955 new.counters
= old
.counters
;
1956 new.freelist
= old
.freelist
;
1960 } while (!__cmpxchg_double_slab(s
, page
,
1961 old
.freelist
, old
.counters
,
1962 new.freelist
, new.counters
,
1963 "unfreezing slab"));
1965 if (unlikely(!new.inuse
&& n
->nr_partial
> s
->min_partial
)) {
1966 page
->next
= discard_page
;
1967 discard_page
= page
;
1969 add_partial(n
, page
, DEACTIVATE_TO_TAIL
);
1970 stat(s
, FREE_ADD_PARTIAL
);
1975 spin_unlock(&n
->list_lock
);
1977 while (discard_page
) {
1978 page
= discard_page
;
1979 discard_page
= discard_page
->next
;
1981 stat(s
, DEACTIVATE_EMPTY
);
1982 discard_slab(s
, page
);
1988 * Put a page that was just frozen (in __slab_free) into a partial page
1989 * slot if available. This is done without interrupts disabled and without
1990 * preemption disabled. The cmpxchg is racy and may put the partial page
1991 * onto a random cpus partial slot.
1993 * If we did not find a slot then simply move all the partials to the
1994 * per node partial list.
1996 static void put_cpu_partial(struct kmem_cache
*s
, struct page
*page
, int drain
)
1998 struct page
*oldpage
;
2005 oldpage
= this_cpu_read(s
->cpu_slab
->partial
);
2008 pobjects
= oldpage
->pobjects
;
2009 pages
= oldpage
->pages
;
2010 if (drain
&& pobjects
> s
->cpu_partial
) {
2011 unsigned long flags
;
2013 * partial array is full. Move the existing
2014 * set to the per node partial list.
2016 local_irq_save(flags
);
2017 unfreeze_partials(s
, this_cpu_ptr(s
->cpu_slab
));
2018 local_irq_restore(flags
);
2022 stat(s
, CPU_PARTIAL_DRAIN
);
2027 pobjects
+= page
->objects
- page
->inuse
;
2029 page
->pages
= pages
;
2030 page
->pobjects
= pobjects
;
2031 page
->next
= oldpage
;
2033 } while (this_cpu_cmpxchg(s
->cpu_slab
->partial
, oldpage
, page
) != oldpage
);
2036 static inline void flush_slab(struct kmem_cache
*s
, struct kmem_cache_cpu
*c
)
2038 stat(s
, CPUSLAB_FLUSH
);
2039 deactivate_slab(s
, c
->page
, c
->freelist
);
2041 c
->tid
= next_tid(c
->tid
);
2049 * Called from IPI handler with interrupts disabled.
2051 static inline void __flush_cpu_slab(struct kmem_cache
*s
, int cpu
)
2053 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
2059 unfreeze_partials(s
, c
);
2063 static void flush_cpu_slab(void *d
)
2065 struct kmem_cache
*s
= d
;
2067 __flush_cpu_slab(s
, smp_processor_id());
2070 static bool has_cpu_slab(int cpu
, void *info
)
2072 struct kmem_cache
*s
= info
;
2073 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
2075 return c
->page
|| c
->partial
;
2078 static void flush_all(struct kmem_cache
*s
)
2080 on_each_cpu_cond(has_cpu_slab
, flush_cpu_slab
, s
, 1, GFP_ATOMIC
);
2084 * Check if the objects in a per cpu structure fit numa
2085 * locality expectations.
2087 static inline int node_match(struct page
*page
, int node
)
2090 if (!page
|| (node
!= NUMA_NO_NODE
&& page_to_nid(page
) != node
))
2096 static int count_free(struct page
*page
)
2098 return page
->objects
- page
->inuse
;
2101 static unsigned long count_partial(struct kmem_cache_node
*n
,
2102 int (*get_count
)(struct page
*))
2104 unsigned long flags
;
2105 unsigned long x
= 0;
2108 spin_lock_irqsave(&n
->list_lock
, flags
);
2109 list_for_each_entry(page
, &n
->partial
, lru
)
2110 x
+= get_count(page
);
2111 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2115 static inline unsigned long node_nr_objs(struct kmem_cache_node
*n
)
2117 #ifdef CONFIG_SLUB_DEBUG
2118 return atomic_long_read(&n
->total_objects
);
2124 static noinline
void
2125 slab_out_of_memory(struct kmem_cache
*s
, gfp_t gfpflags
, int nid
)
2130 "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
2132 printk(KERN_WARNING
" cache: %s, object size: %d, buffer size: %d, "
2133 "default order: %d, min order: %d\n", s
->name
, s
->object_size
,
2134 s
->size
, oo_order(s
->oo
), oo_order(s
->min
));
2136 if (oo_order(s
->min
) > get_order(s
->object_size
))
2137 printk(KERN_WARNING
" %s debugging increased min order, use "
2138 "slub_debug=O to disable.\n", s
->name
);
2140 for_each_online_node(node
) {
2141 struct kmem_cache_node
*n
= get_node(s
, node
);
2142 unsigned long nr_slabs
;
2143 unsigned long nr_objs
;
2144 unsigned long nr_free
;
2149 nr_free
= count_partial(n
, count_free
);
2150 nr_slabs
= node_nr_slabs(n
);
2151 nr_objs
= node_nr_objs(n
);
2154 " node %d: slabs: %ld, objs: %ld, free: %ld\n",
2155 node
, nr_slabs
, nr_objs
, nr_free
);
2159 static inline void *new_slab_objects(struct kmem_cache
*s
, gfp_t flags
,
2160 int node
, struct kmem_cache_cpu
**pc
)
2163 struct kmem_cache_cpu
*c
= *pc
;
2166 freelist
= get_partial(s
, flags
, node
, c
);
2171 page
= new_slab(s
, flags
, node
);
2173 c
= __this_cpu_ptr(s
->cpu_slab
);
2178 * No other reference to the page yet so we can
2179 * muck around with it freely without cmpxchg
2181 freelist
= page
->freelist
;
2182 page
->freelist
= NULL
;
2184 stat(s
, ALLOC_SLAB
);
2193 static inline bool pfmemalloc_match(struct page
*page
, gfp_t gfpflags
)
2195 if (unlikely(PageSlabPfmemalloc(page
)))
2196 return gfp_pfmemalloc_allowed(gfpflags
);
2202 * Check the page->freelist of a page and either transfer the freelist to the per cpu freelist
2203 * or deactivate the page.
2205 * The page is still frozen if the return value is not NULL.
2207 * If this function returns NULL then the page has been unfrozen.
2209 * This function must be called with interrupt disabled.
2211 static inline void *get_freelist(struct kmem_cache
*s
, struct page
*page
)
2214 unsigned long counters
;
2218 freelist
= page
->freelist
;
2219 counters
= page
->counters
;
2221 new.counters
= counters
;
2222 VM_BUG_ON(!new.frozen
);
2224 new.inuse
= page
->objects
;
2225 new.frozen
= freelist
!= NULL
;
2227 } while (!__cmpxchg_double_slab(s
, page
,
2236 * Slow path. The lockless freelist is empty or we need to perform
2239 * Processing is still very fast if new objects have been freed to the
2240 * regular freelist. In that case we simply take over the regular freelist
2241 * as the lockless freelist and zap the regular freelist.
2243 * If that is not working then we fall back to the partial lists. We take the
2244 * first element of the freelist as the object to allocate now and move the
2245 * rest of the freelist to the lockless freelist.
2247 * And if we were unable to get a new slab from the partial slab lists then
2248 * we need to allocate a new slab. This is the slowest path since it involves
2249 * a call to the page allocator and the setup of a new slab.
2251 static void *__slab_alloc(struct kmem_cache
*s
, gfp_t gfpflags
, int node
,
2252 unsigned long addr
, struct kmem_cache_cpu
*c
)
2256 unsigned long flags
;
2258 local_irq_save(flags
);
2259 #ifdef CONFIG_PREEMPT
2261 * We may have been preempted and rescheduled on a different
2262 * cpu before disabling interrupts. Need to reload cpu area
2265 c
= this_cpu_ptr(s
->cpu_slab
);
2273 if (unlikely(!node_match(page
, node
))) {
2274 stat(s
, ALLOC_NODE_MISMATCH
);
2275 deactivate_slab(s
, page
, c
->freelist
);
2282 * By rights, we should be searching for a slab page that was
2283 * PFMEMALLOC but right now, we are losing the pfmemalloc
2284 * information when the page leaves the per-cpu allocator
2286 if (unlikely(!pfmemalloc_match(page
, gfpflags
))) {
2287 deactivate_slab(s
, page
, c
->freelist
);
2293 /* must check again c->freelist in case of cpu migration or IRQ */
2294 freelist
= c
->freelist
;
2298 stat(s
, ALLOC_SLOWPATH
);
2300 freelist
= get_freelist(s
, page
);
2304 stat(s
, DEACTIVATE_BYPASS
);
2308 stat(s
, ALLOC_REFILL
);
2312 * freelist is pointing to the list of objects to be used.
2313 * page is pointing to the page from which the objects are obtained.
2314 * That page must be frozen for per cpu allocations to work.
2316 VM_BUG_ON(!c
->page
->frozen
);
2317 c
->freelist
= get_freepointer(s
, freelist
);
2318 c
->tid
= next_tid(c
->tid
);
2319 local_irq_restore(flags
);
2325 page
= c
->page
= c
->partial
;
2326 c
->partial
= page
->next
;
2327 stat(s
, CPU_PARTIAL_ALLOC
);
2332 freelist
= new_slab_objects(s
, gfpflags
, node
, &c
);
2334 if (unlikely(!freelist
)) {
2335 if (!(gfpflags
& __GFP_NOWARN
) && printk_ratelimit())
2336 slab_out_of_memory(s
, gfpflags
, node
);
2338 local_irq_restore(flags
);
2343 if (likely(!kmem_cache_debug(s
) && pfmemalloc_match(page
, gfpflags
)))
2346 /* Only entered in the debug case */
2347 if (kmem_cache_debug(s
) && !alloc_debug_processing(s
, page
, freelist
, addr
))
2348 goto new_slab
; /* Slab failed checks. Next slab needed */
2350 deactivate_slab(s
, page
, get_freepointer(s
, freelist
));
2353 local_irq_restore(flags
);
2358 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2359 * have the fastpath folded into their functions. So no function call
2360 * overhead for requests that can be satisfied on the fastpath.
2362 * The fastpath works by first checking if the lockless freelist can be used.
2363 * If not then __slab_alloc is called for slow processing.
2365 * Otherwise we can simply pick the next object from the lockless free list.
2367 static __always_inline
void *slab_alloc_node(struct kmem_cache
*s
,
2368 gfp_t gfpflags
, int node
, unsigned long addr
)
2371 struct kmem_cache_cpu
*c
;
2375 if (slab_pre_alloc_hook(s
, gfpflags
))
2378 s
= memcg_kmem_get_cache(s
, gfpflags
);
2381 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2382 * enabled. We may switch back and forth between cpus while
2383 * reading from one cpu area. That does not matter as long
2384 * as we end up on the original cpu again when doing the cmpxchg.
2386 * Preemption is disabled for the retrieval of the tid because that
2387 * must occur from the current processor. We cannot allow rescheduling
2388 * on a different processor between the determination of the pointer
2389 * and the retrieval of the tid.
2392 c
= __this_cpu_ptr(s
->cpu_slab
);
2395 * The transaction ids are globally unique per cpu and per operation on
2396 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2397 * occurs on the right processor and that there was no operation on the
2398 * linked list in between.
2403 object
= c
->freelist
;
2405 if (unlikely(!object
|| !node_match(page
, node
)))
2406 object
= __slab_alloc(s
, gfpflags
, node
, addr
, c
);
2409 void *next_object
= get_freepointer_safe(s
, object
);
2412 * The cmpxchg will only match if there was no additional
2413 * operation and if we are on the right processor.
2415 * The cmpxchg does the following atomically (without lock semantics!)
2416 * 1. Relocate first pointer to the current per cpu area.
2417 * 2. Verify that tid and freelist have not been changed
2418 * 3. If they were not changed replace tid and freelist
2420 * Since this is without lock semantics the protection is only against
2421 * code executing on this cpu *not* from access by other cpus.
2423 if (unlikely(!this_cpu_cmpxchg_double(
2424 s
->cpu_slab
->freelist
, s
->cpu_slab
->tid
,
2426 next_object
, next_tid(tid
)))) {
2428 note_cmpxchg_failure("slab_alloc", s
, tid
);
2431 prefetch_freepointer(s
, next_object
);
2432 stat(s
, ALLOC_FASTPATH
);
2435 if (unlikely(gfpflags
& __GFP_ZERO
) && object
)
2436 memset(object
, 0, s
->object_size
);
2438 slab_post_alloc_hook(s
, gfpflags
, object
);
2443 static __always_inline
void *slab_alloc(struct kmem_cache
*s
,
2444 gfp_t gfpflags
, unsigned long addr
)
2446 return slab_alloc_node(s
, gfpflags
, NUMA_NO_NODE
, addr
);
2449 void *kmem_cache_alloc(struct kmem_cache
*s
, gfp_t gfpflags
)
2451 void *ret
= slab_alloc(s
, gfpflags
, _RET_IP_
);
2453 trace_kmem_cache_alloc(_RET_IP_
, ret
, s
->object_size
, s
->size
, gfpflags
);
2457 EXPORT_SYMBOL(kmem_cache_alloc
);
2459 #ifdef CONFIG_TRACING
2460 void *kmem_cache_alloc_trace(struct kmem_cache
*s
, gfp_t gfpflags
, size_t size
)
2462 void *ret
= slab_alloc(s
, gfpflags
, _RET_IP_
);
2463 trace_kmalloc(_RET_IP_
, ret
, size
, s
->size
, gfpflags
);
2466 EXPORT_SYMBOL(kmem_cache_alloc_trace
);
2468 void *kmalloc_order_trace(size_t size
, gfp_t flags
, unsigned int order
)
2470 void *ret
= kmalloc_order(size
, flags
, order
);
2471 trace_kmalloc(_RET_IP_
, ret
, size
, PAGE_SIZE
<< order
, flags
);
2474 EXPORT_SYMBOL(kmalloc_order_trace
);
2478 void *kmem_cache_alloc_node(struct kmem_cache
*s
, gfp_t gfpflags
, int node
)
2480 void *ret
= slab_alloc_node(s
, gfpflags
, node
, _RET_IP_
);
2482 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
2483 s
->object_size
, s
->size
, gfpflags
, node
);
2487 EXPORT_SYMBOL(kmem_cache_alloc_node
);
2489 #ifdef CONFIG_TRACING
2490 void *kmem_cache_alloc_node_trace(struct kmem_cache
*s
,
2492 int node
, size_t size
)
2494 void *ret
= slab_alloc_node(s
, gfpflags
, node
, _RET_IP_
);
2496 trace_kmalloc_node(_RET_IP_
, ret
,
2497 size
, s
->size
, gfpflags
, node
);
2500 EXPORT_SYMBOL(kmem_cache_alloc_node_trace
);
2505 * Slow patch handling. This may still be called frequently since objects
2506 * have a longer lifetime than the cpu slabs in most processing loads.
2508 * So we still attempt to reduce cache line usage. Just take the slab
2509 * lock and free the item. If there is no additional partial page
2510 * handling required then we can return immediately.
2512 static void __slab_free(struct kmem_cache
*s
, struct page
*page
,
2513 void *x
, unsigned long addr
)
2516 void **object
= (void *)x
;
2519 unsigned long counters
;
2520 struct kmem_cache_node
*n
= NULL
;
2521 unsigned long uninitialized_var(flags
);
2523 stat(s
, FREE_SLOWPATH
);
2525 if (kmem_cache_debug(s
) &&
2526 !(n
= free_debug_processing(s
, page
, x
, addr
, &flags
)))
2531 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2534 prior
= page
->freelist
;
2535 counters
= page
->counters
;
2536 set_freepointer(s
, object
, prior
);
2537 new.counters
= counters
;
2538 was_frozen
= new.frozen
;
2540 if ((!new.inuse
|| !prior
) && !was_frozen
) {
2542 if (!kmem_cache_debug(s
) && !prior
)
2545 * Slab was on no list before and will be partially empty
2546 * We can defer the list move and instead freeze it.
2550 else { /* Needs to be taken off a list */
2552 n
= get_node(s
, page_to_nid(page
));
2554 * Speculatively acquire the list_lock.
2555 * If the cmpxchg does not succeed then we may
2556 * drop the list_lock without any processing.
2558 * Otherwise the list_lock will synchronize with
2559 * other processors updating the list of slabs.
2561 spin_lock_irqsave(&n
->list_lock
, flags
);
2566 } while (!cmpxchg_double_slab(s
, page
,
2568 object
, new.counters
,
2574 * If we just froze the page then put it onto the
2575 * per cpu partial list.
2577 if (new.frozen
&& !was_frozen
) {
2578 put_cpu_partial(s
, page
, 1);
2579 stat(s
, CPU_PARTIAL_FREE
);
2582 * The list lock was not taken therefore no list
2583 * activity can be necessary.
2586 stat(s
, FREE_FROZEN
);
2590 if (unlikely(!new.inuse
&& n
->nr_partial
> s
->min_partial
))
2594 * Objects left in the slab. If it was not on the partial list before
2597 if (kmem_cache_debug(s
) && unlikely(!prior
)) {
2598 remove_full(s
, page
);
2599 add_partial(n
, page
, DEACTIVATE_TO_TAIL
);
2600 stat(s
, FREE_ADD_PARTIAL
);
2602 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2608 * Slab on the partial list.
2610 remove_partial(n
, page
);
2611 stat(s
, FREE_REMOVE_PARTIAL
);
2613 /* Slab must be on the full list */
2614 remove_full(s
, page
);
2616 spin_unlock_irqrestore(&n
->list_lock
, flags
);
2618 discard_slab(s
, page
);
2622 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2623 * can perform fastpath freeing without additional function calls.
2625 * The fastpath is only possible if we are freeing to the current cpu slab
2626 * of this processor. This typically the case if we have just allocated
2629 * If fastpath is not possible then fall back to __slab_free where we deal
2630 * with all sorts of special processing.
2632 static __always_inline
void slab_free(struct kmem_cache
*s
,
2633 struct page
*page
, void *x
, unsigned long addr
)
2635 void **object
= (void *)x
;
2636 struct kmem_cache_cpu
*c
;
2639 slab_free_hook(s
, x
);
2643 * Determine the currently cpus per cpu slab.
2644 * The cpu may change afterward. However that does not matter since
2645 * data is retrieved via this pointer. If we are on the same cpu
2646 * during the cmpxchg then the free will succedd.
2649 c
= __this_cpu_ptr(s
->cpu_slab
);
2654 if (likely(page
== c
->page
)) {
2655 set_freepointer(s
, object
, c
->freelist
);
2657 if (unlikely(!this_cpu_cmpxchg_double(
2658 s
->cpu_slab
->freelist
, s
->cpu_slab
->tid
,
2660 object
, next_tid(tid
)))) {
2662 note_cmpxchg_failure("slab_free", s
, tid
);
2665 stat(s
, FREE_FASTPATH
);
2667 __slab_free(s
, page
, x
, addr
);
2671 void kmem_cache_free(struct kmem_cache
*s
, void *x
)
2673 s
= cache_from_obj(s
, x
);
2676 slab_free(s
, virt_to_head_page(x
), x
, _RET_IP_
);
2677 trace_kmem_cache_free(_RET_IP_
, x
);
2679 EXPORT_SYMBOL(kmem_cache_free
);
2682 * Object placement in a slab is made very easy because we always start at
2683 * offset 0. If we tune the size of the object to the alignment then we can
2684 * get the required alignment by putting one properly sized object after
2687 * Notice that the allocation order determines the sizes of the per cpu
2688 * caches. Each processor has always one slab available for allocations.
2689 * Increasing the allocation order reduces the number of times that slabs
2690 * must be moved on and off the partial lists and is therefore a factor in
2695 * Mininum / Maximum order of slab pages. This influences locking overhead
2696 * and slab fragmentation. A higher order reduces the number of partial slabs
2697 * and increases the number of allocations possible without having to
2698 * take the list_lock.
2700 static int slub_min_order
;
2701 static int slub_max_order
= PAGE_ALLOC_COSTLY_ORDER
;
2702 static int slub_min_objects
;
2705 * Merge control. If this is set then no merging of slab caches will occur.
2706 * (Could be removed. This was introduced to pacify the merge skeptics.)
2708 static int slub_nomerge
;
2711 * Calculate the order of allocation given an slab object size.
2713 * The order of allocation has significant impact on performance and other
2714 * system components. Generally order 0 allocations should be preferred since
2715 * order 0 does not cause fragmentation in the page allocator. Larger objects
2716 * be problematic to put into order 0 slabs because there may be too much
2717 * unused space left. We go to a higher order if more than 1/16th of the slab
2720 * In order to reach satisfactory performance we must ensure that a minimum
2721 * number of objects is in one slab. Otherwise we may generate too much
2722 * activity on the partial lists which requires taking the list_lock. This is
2723 * less a concern for large slabs though which are rarely used.
2725 * slub_max_order specifies the order where we begin to stop considering the
2726 * number of objects in a slab as critical. If we reach slub_max_order then
2727 * we try to keep the page order as low as possible. So we accept more waste
2728 * of space in favor of a small page order.
2730 * Higher order allocations also allow the placement of more objects in a
2731 * slab and thereby reduce object handling overhead. If the user has
2732 * requested a higher mininum order then we start with that one instead of
2733 * the smallest order which will fit the object.
2735 static inline int slab_order(int size
, int min_objects
,
2736 int max_order
, int fract_leftover
, int reserved
)
2740 int min_order
= slub_min_order
;
2742 if (order_objects(min_order
, size
, reserved
) > MAX_OBJS_PER_PAGE
)
2743 return get_order(size
* MAX_OBJS_PER_PAGE
) - 1;
2745 for (order
= max(min_order
,
2746 fls(min_objects
* size
- 1) - PAGE_SHIFT
);
2747 order
<= max_order
; order
++) {
2749 unsigned long slab_size
= PAGE_SIZE
<< order
;
2751 if (slab_size
< min_objects
* size
+ reserved
)
2754 rem
= (slab_size
- reserved
) % size
;
2756 if (rem
<= slab_size
/ fract_leftover
)
2764 static inline int calculate_order(int size
, int reserved
)
2772 * Attempt to find best configuration for a slab. This
2773 * works by first attempting to generate a layout with
2774 * the best configuration and backing off gradually.
2776 * First we reduce the acceptable waste in a slab. Then
2777 * we reduce the minimum objects required in a slab.
2779 min_objects
= slub_min_objects
;
2781 min_objects
= 4 * (fls(nr_cpu_ids
) + 1);
2782 max_objects
= order_objects(slub_max_order
, size
, reserved
);
2783 min_objects
= min(min_objects
, max_objects
);
2785 while (min_objects
> 1) {
2787 while (fraction
>= 4) {
2788 order
= slab_order(size
, min_objects
,
2789 slub_max_order
, fraction
, reserved
);
2790 if (order
<= slub_max_order
)
2798 * We were unable to place multiple objects in a slab. Now
2799 * lets see if we can place a single object there.
2801 order
= slab_order(size
, 1, slub_max_order
, 1, reserved
);
2802 if (order
<= slub_max_order
)
2806 * Doh this slab cannot be placed using slub_max_order.
2808 order
= slab_order(size
, 1, MAX_ORDER
, 1, reserved
);
2809 if (order
< MAX_ORDER
)
2815 init_kmem_cache_node(struct kmem_cache_node
*n
)
2818 spin_lock_init(&n
->list_lock
);
2819 INIT_LIST_HEAD(&n
->partial
);
2820 #ifdef CONFIG_SLUB_DEBUG
2821 atomic_long_set(&n
->nr_slabs
, 0);
2822 atomic_long_set(&n
->total_objects
, 0);
2823 INIT_LIST_HEAD(&n
->full
);
2827 static inline int alloc_kmem_cache_cpus(struct kmem_cache
*s
)
2829 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE
<
2830 KMALLOC_SHIFT_HIGH
* sizeof(struct kmem_cache_cpu
));
2833 * Must align to double word boundary for the double cmpxchg
2834 * instructions to work; see __pcpu_double_call_return_bool().
2836 s
->cpu_slab
= __alloc_percpu(sizeof(struct kmem_cache_cpu
),
2837 2 * sizeof(void *));
2842 init_kmem_cache_cpus(s
);
2847 static struct kmem_cache
*kmem_cache_node
;
2850 * No kmalloc_node yet so do it by hand. We know that this is the first
2851 * slab on the node for this slabcache. There are no concurrent accesses
2854 * Note that this function only works on the kmalloc_node_cache
2855 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2856 * memory on a fresh node that has no slab structures yet.
2858 static void early_kmem_cache_node_alloc(int node
)
2861 struct kmem_cache_node
*n
;
2863 BUG_ON(kmem_cache_node
->size
< sizeof(struct kmem_cache_node
));
2865 page
= new_slab(kmem_cache_node
, GFP_NOWAIT
, node
);
2868 if (page_to_nid(page
) != node
) {
2869 printk(KERN_ERR
"SLUB: Unable to allocate memory from "
2871 printk(KERN_ERR
"SLUB: Allocating a useless per node structure "
2872 "in order to be able to continue\n");
2877 page
->freelist
= get_freepointer(kmem_cache_node
, n
);
2880 kmem_cache_node
->node
[node
] = n
;
2881 #ifdef CONFIG_SLUB_DEBUG
2882 init_object(kmem_cache_node
, n
, SLUB_RED_ACTIVE
);
2883 init_tracking(kmem_cache_node
, n
);
2885 init_kmem_cache_node(n
);
2886 inc_slabs_node(kmem_cache_node
, node
, page
->objects
);
2888 add_partial(n
, page
, DEACTIVATE_TO_HEAD
);
2891 static void free_kmem_cache_nodes(struct kmem_cache
*s
)
2895 for_each_node_state(node
, N_NORMAL_MEMORY
) {
2896 struct kmem_cache_node
*n
= s
->node
[node
];
2899 kmem_cache_free(kmem_cache_node
, n
);
2901 s
->node
[node
] = NULL
;
2905 static int init_kmem_cache_nodes(struct kmem_cache
*s
)
2909 for_each_node_state(node
, N_NORMAL_MEMORY
) {
2910 struct kmem_cache_node
*n
;
2912 if (slab_state
== DOWN
) {
2913 early_kmem_cache_node_alloc(node
);
2916 n
= kmem_cache_alloc_node(kmem_cache_node
,
2920 free_kmem_cache_nodes(s
);
2925 init_kmem_cache_node(n
);
2930 static void set_min_partial(struct kmem_cache
*s
, unsigned long min
)
2932 if (min
< MIN_PARTIAL
)
2934 else if (min
> MAX_PARTIAL
)
2936 s
->min_partial
= min
;
2940 * calculate_sizes() determines the order and the distribution of data within
2943 static int calculate_sizes(struct kmem_cache
*s
, int forced_order
)
2945 unsigned long flags
= s
->flags
;
2946 unsigned long size
= s
->object_size
;
2950 * Round up object size to the next word boundary. We can only
2951 * place the free pointer at word boundaries and this determines
2952 * the possible location of the free pointer.
2954 size
= ALIGN(size
, sizeof(void *));
2956 #ifdef CONFIG_SLUB_DEBUG
2958 * Determine if we can poison the object itself. If the user of
2959 * the slab may touch the object after free or before allocation
2960 * then we should never poison the object itself.
2962 if ((flags
& SLAB_POISON
) && !(flags
& SLAB_DESTROY_BY_RCU
) &&
2964 s
->flags
|= __OBJECT_POISON
;
2966 s
->flags
&= ~__OBJECT_POISON
;
2970 * If we are Redzoning then check if there is some space between the
2971 * end of the object and the free pointer. If not then add an
2972 * additional word to have some bytes to store Redzone information.
2974 if ((flags
& SLAB_RED_ZONE
) && size
== s
->object_size
)
2975 size
+= sizeof(void *);
2979 * With that we have determined the number of bytes in actual use
2980 * by the object. This is the potential offset to the free pointer.
2984 if (((flags
& (SLAB_DESTROY_BY_RCU
| SLAB_POISON
)) ||
2987 * Relocate free pointer after the object if it is not
2988 * permitted to overwrite the first word of the object on
2991 * This is the case if we do RCU, have a constructor or
2992 * destructor or are poisoning the objects.
2995 size
+= sizeof(void *);
2998 #ifdef CONFIG_SLUB_DEBUG
2999 if (flags
& SLAB_STORE_USER
)
3001 * Need to store information about allocs and frees after
3004 size
+= 2 * sizeof(struct track
);
3006 if (flags
& SLAB_RED_ZONE
)
3008 * Add some empty padding so that we can catch
3009 * overwrites from earlier objects rather than let
3010 * tracking information or the free pointer be
3011 * corrupted if a user writes before the start
3014 size
+= sizeof(void *);
3018 * SLUB stores one object immediately after another beginning from
3019 * offset 0. In order to align the objects we have to simply size
3020 * each object to conform to the alignment.
3022 size
= ALIGN(size
, s
->align
);
3024 if (forced_order
>= 0)
3025 order
= forced_order
;
3027 order
= calculate_order(size
, s
->reserved
);
3034 s
->allocflags
|= __GFP_COMP
;
3036 if (s
->flags
& SLAB_CACHE_DMA
)
3037 s
->allocflags
|= GFP_DMA
;
3039 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
3040 s
->allocflags
|= __GFP_RECLAIMABLE
;
3043 * Determine the number of objects per slab
3045 s
->oo
= oo_make(order
, size
, s
->reserved
);
3046 s
->min
= oo_make(get_order(size
), size
, s
->reserved
);
3047 if (oo_objects(s
->oo
) > oo_objects(s
->max
))
3050 return !!oo_objects(s
->oo
);
3053 static int kmem_cache_open(struct kmem_cache
*s
, unsigned long flags
)
3055 s
->flags
= kmem_cache_flags(s
->size
, flags
, s
->name
, s
->ctor
);
3058 if (need_reserve_slab_rcu
&& (s
->flags
& SLAB_DESTROY_BY_RCU
))
3059 s
->reserved
= sizeof(struct rcu_head
);
3061 if (!calculate_sizes(s
, -1))
3063 if (disable_higher_order_debug
) {
3065 * Disable debugging flags that store metadata if the min slab
3068 if (get_order(s
->size
) > get_order(s
->object_size
)) {
3069 s
->flags
&= ~DEBUG_METADATA_FLAGS
;
3071 if (!calculate_sizes(s
, -1))
3076 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
3077 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
3078 if (system_has_cmpxchg_double() && (s
->flags
& SLAB_DEBUG_FLAGS
) == 0)
3079 /* Enable fast mode */
3080 s
->flags
|= __CMPXCHG_DOUBLE
;
3084 * The larger the object size is, the more pages we want on the partial
3085 * list to avoid pounding the page allocator excessively.
3087 set_min_partial(s
, ilog2(s
->size
) / 2);
3090 * cpu_partial determined the maximum number of objects kept in the
3091 * per cpu partial lists of a processor.
3093 * Per cpu partial lists mainly contain slabs that just have one
3094 * object freed. If they are used for allocation then they can be
3095 * filled up again with minimal effort. The slab will never hit the
3096 * per node partial lists and therefore no locking will be required.
3098 * This setting also determines
3100 * A) The number of objects from per cpu partial slabs dumped to the
3101 * per node list when we reach the limit.
3102 * B) The number of objects in cpu partial slabs to extract from the
3103 * per node list when we run out of per cpu objects. We only fetch 50%
3104 * to keep some capacity around for frees.
3106 if (kmem_cache_debug(s
))
3108 else if (s
->size
>= PAGE_SIZE
)
3110 else if (s
->size
>= 1024)
3112 else if (s
->size
>= 256)
3113 s
->cpu_partial
= 13;
3115 s
->cpu_partial
= 30;
3118 s
->remote_node_defrag_ratio
= 1000;
3120 if (!init_kmem_cache_nodes(s
))
3123 if (alloc_kmem_cache_cpus(s
))
3126 free_kmem_cache_nodes(s
);
3128 if (flags
& SLAB_PANIC
)
3129 panic("Cannot create slab %s size=%lu realsize=%u "
3130 "order=%u offset=%u flags=%lx\n",
3131 s
->name
, (unsigned long)s
->size
, s
->size
, oo_order(s
->oo
),
3136 static void list_slab_objects(struct kmem_cache
*s
, struct page
*page
,
3139 #ifdef CONFIG_SLUB_DEBUG
3140 void *addr
= page_address(page
);
3142 unsigned long *map
= kzalloc(BITS_TO_LONGS(page
->objects
) *
3143 sizeof(long), GFP_ATOMIC
);
3146 slab_err(s
, page
, text
, s
->name
);
3149 get_map(s
, page
, map
);
3150 for_each_object(p
, s
, addr
, page
->objects
) {
3152 if (!test_bit(slab_index(p
, s
, addr
), map
)) {
3153 printk(KERN_ERR
"INFO: Object 0x%p @offset=%tu\n",
3155 print_tracking(s
, p
);
3164 * Attempt to free all partial slabs on a node.
3165 * This is called from kmem_cache_close(). We must be the last thread
3166 * using the cache and therefore we do not need to lock anymore.
3168 static void free_partial(struct kmem_cache
*s
, struct kmem_cache_node
*n
)
3170 struct page
*page
, *h
;
3172 list_for_each_entry_safe(page
, h
, &n
->partial
, lru
) {
3174 remove_partial(n
, page
);
3175 discard_slab(s
, page
);
3177 list_slab_objects(s
, page
,
3178 "Objects remaining in %s on kmem_cache_close()");
3184 * Release all resources used by a slab cache.
3186 static inline int kmem_cache_close(struct kmem_cache
*s
)
3191 /* Attempt to free all objects */
3192 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3193 struct kmem_cache_node
*n
= get_node(s
, node
);
3196 if (n
->nr_partial
|| slabs_node(s
, node
))
3199 free_percpu(s
->cpu_slab
);
3200 free_kmem_cache_nodes(s
);
3204 int __kmem_cache_shutdown(struct kmem_cache
*s
)
3206 int rc
= kmem_cache_close(s
);
3210 * We do the same lock strategy around sysfs_slab_add, see
3211 * __kmem_cache_create. Because this is pretty much the last
3212 * operation we do and the lock will be released shortly after
3213 * that in slab_common.c, we could just move sysfs_slab_remove
3214 * to a later point in common code. We should do that when we
3215 * have a common sysfs framework for all allocators.
3217 mutex_unlock(&slab_mutex
);
3218 sysfs_slab_remove(s
);
3219 mutex_lock(&slab_mutex
);
3225 /********************************************************************
3227 *******************************************************************/
3229 static int __init
setup_slub_min_order(char *str
)
3231 get_option(&str
, &slub_min_order
);
3236 __setup("slub_min_order=", setup_slub_min_order
);
3238 static int __init
setup_slub_max_order(char *str
)
3240 get_option(&str
, &slub_max_order
);
3241 slub_max_order
= min(slub_max_order
, MAX_ORDER
- 1);
3246 __setup("slub_max_order=", setup_slub_max_order
);
3248 static int __init
setup_slub_min_objects(char *str
)
3250 get_option(&str
, &slub_min_objects
);
3255 __setup("slub_min_objects=", setup_slub_min_objects
);
3257 static int __init
setup_slub_nomerge(char *str
)
3263 __setup("slub_nomerge", setup_slub_nomerge
);
3265 void *__kmalloc(size_t size
, gfp_t flags
)
3267 struct kmem_cache
*s
;
3270 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
))
3271 return kmalloc_large(size
, flags
);
3273 s
= kmalloc_slab(size
, flags
);
3275 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3278 ret
= slab_alloc(s
, flags
, _RET_IP_
);
3280 trace_kmalloc(_RET_IP_
, ret
, size
, s
->size
, flags
);
3284 EXPORT_SYMBOL(__kmalloc
);
3287 static void *kmalloc_large_node(size_t size
, gfp_t flags
, int node
)
3292 flags
|= __GFP_COMP
| __GFP_NOTRACK
| __GFP_KMEMCG
;
3293 page
= alloc_pages_node(node
, flags
, get_order(size
));
3295 ptr
= page_address(page
);
3297 kmemleak_alloc(ptr
, size
, 1, flags
);
3301 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3303 struct kmem_cache
*s
;
3306 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
)) {
3307 ret
= kmalloc_large_node(size
, flags
, node
);
3309 trace_kmalloc_node(_RET_IP_
, ret
,
3310 size
, PAGE_SIZE
<< get_order(size
),
3316 s
= kmalloc_slab(size
, flags
);
3318 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3321 ret
= slab_alloc_node(s
, flags
, node
, _RET_IP_
);
3323 trace_kmalloc_node(_RET_IP_
, ret
, size
, s
->size
, flags
, node
);
3327 EXPORT_SYMBOL(__kmalloc_node
);
3330 size_t ksize(const void *object
)
3334 if (unlikely(object
== ZERO_SIZE_PTR
))
3337 page
= virt_to_head_page(object
);
3339 if (unlikely(!PageSlab(page
))) {
3340 WARN_ON(!PageCompound(page
));
3341 return PAGE_SIZE
<< compound_order(page
);
3344 return slab_ksize(page
->slab_cache
);
3346 EXPORT_SYMBOL(ksize
);
3348 #ifdef CONFIG_SLUB_DEBUG
3349 bool verify_mem_not_deleted(const void *x
)
3352 void *object
= (void *)x
;
3353 unsigned long flags
;
3356 if (unlikely(ZERO_OR_NULL_PTR(x
)))
3359 local_irq_save(flags
);
3361 page
= virt_to_head_page(x
);
3362 if (unlikely(!PageSlab(page
))) {
3363 /* maybe it was from stack? */
3369 if (on_freelist(page
->slab_cache
, page
, object
)) {
3370 object_err(page
->slab_cache
, page
, object
, "Object is on free-list");
3378 local_irq_restore(flags
);
3381 EXPORT_SYMBOL(verify_mem_not_deleted
);
3384 void kfree(const void *x
)
3387 void *object
= (void *)x
;
3389 trace_kfree(_RET_IP_
, x
);
3391 if (unlikely(ZERO_OR_NULL_PTR(x
)))
3394 page
= virt_to_head_page(x
);
3395 if (unlikely(!PageSlab(page
))) {
3396 BUG_ON(!PageCompound(page
));
3398 __free_memcg_kmem_pages(page
, compound_order(page
));
3401 slab_free(page
->slab_cache
, page
, object
, _RET_IP_
);
3403 EXPORT_SYMBOL(kfree
);
3406 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
3407 * the remaining slabs by the number of items in use. The slabs with the
3408 * most items in use come first. New allocations will then fill those up
3409 * and thus they can be removed from the partial lists.
3411 * The slabs with the least items are placed last. This results in them
3412 * being allocated from last increasing the chance that the last objects
3413 * are freed in them.
3415 int kmem_cache_shrink(struct kmem_cache
*s
)
3419 struct kmem_cache_node
*n
;
3422 int objects
= oo_objects(s
->max
);
3423 struct list_head
*slabs_by_inuse
=
3424 kmalloc(sizeof(struct list_head
) * objects
, GFP_KERNEL
);
3425 unsigned long flags
;
3427 if (!slabs_by_inuse
)
3431 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3432 n
= get_node(s
, node
);
3437 for (i
= 0; i
< objects
; i
++)
3438 INIT_LIST_HEAD(slabs_by_inuse
+ i
);
3440 spin_lock_irqsave(&n
->list_lock
, flags
);
3443 * Build lists indexed by the items in use in each slab.
3445 * Note that concurrent frees may occur while we hold the
3446 * list_lock. page->inuse here is the upper limit.
3448 list_for_each_entry_safe(page
, t
, &n
->partial
, lru
) {
3449 list_move(&page
->lru
, slabs_by_inuse
+ page
->inuse
);
3455 * Rebuild the partial list with the slabs filled up most
3456 * first and the least used slabs at the end.
3458 for (i
= objects
- 1; i
> 0; i
--)
3459 list_splice(slabs_by_inuse
+ i
, n
->partial
.prev
);
3461 spin_unlock_irqrestore(&n
->list_lock
, flags
);
3463 /* Release empty slabs */
3464 list_for_each_entry_safe(page
, t
, slabs_by_inuse
, lru
)
3465 discard_slab(s
, page
);
3468 kfree(slabs_by_inuse
);
3471 EXPORT_SYMBOL(kmem_cache_shrink
);
3473 static int slab_mem_going_offline_callback(void *arg
)
3475 struct kmem_cache
*s
;
3477 mutex_lock(&slab_mutex
);
3478 list_for_each_entry(s
, &slab_caches
, list
)
3479 kmem_cache_shrink(s
);
3480 mutex_unlock(&slab_mutex
);
3485 static void slab_mem_offline_callback(void *arg
)
3487 struct kmem_cache_node
*n
;
3488 struct kmem_cache
*s
;
3489 struct memory_notify
*marg
= arg
;
3492 offline_node
= marg
->status_change_nid_normal
;
3495 * If the node still has available memory. we need kmem_cache_node
3498 if (offline_node
< 0)
3501 mutex_lock(&slab_mutex
);
3502 list_for_each_entry(s
, &slab_caches
, list
) {
3503 n
= get_node(s
, offline_node
);
3506 * if n->nr_slabs > 0, slabs still exist on the node
3507 * that is going down. We were unable to free them,
3508 * and offline_pages() function shouldn't call this
3509 * callback. So, we must fail.
3511 BUG_ON(slabs_node(s
, offline_node
));
3513 s
->node
[offline_node
] = NULL
;
3514 kmem_cache_free(kmem_cache_node
, n
);
3517 mutex_unlock(&slab_mutex
);
3520 static int slab_mem_going_online_callback(void *arg
)
3522 struct kmem_cache_node
*n
;
3523 struct kmem_cache
*s
;
3524 struct memory_notify
*marg
= arg
;
3525 int nid
= marg
->status_change_nid_normal
;
3529 * If the node's memory is already available, then kmem_cache_node is
3530 * already created. Nothing to do.
3536 * We are bringing a node online. No memory is available yet. We must
3537 * allocate a kmem_cache_node structure in order to bring the node
3540 mutex_lock(&slab_mutex
);
3541 list_for_each_entry(s
, &slab_caches
, list
) {
3543 * XXX: kmem_cache_alloc_node will fallback to other nodes
3544 * since memory is not yet available from the node that
3547 n
= kmem_cache_alloc(kmem_cache_node
, GFP_KERNEL
);
3552 init_kmem_cache_node(n
);
3556 mutex_unlock(&slab_mutex
);
3560 static int slab_memory_callback(struct notifier_block
*self
,
3561 unsigned long action
, void *arg
)
3566 case MEM_GOING_ONLINE
:
3567 ret
= slab_mem_going_online_callback(arg
);
3569 case MEM_GOING_OFFLINE
:
3570 ret
= slab_mem_going_offline_callback(arg
);
3573 case MEM_CANCEL_ONLINE
:
3574 slab_mem_offline_callback(arg
);
3577 case MEM_CANCEL_OFFLINE
:
3581 ret
= notifier_from_errno(ret
);
3587 static struct notifier_block slab_memory_callback_nb
= {
3588 .notifier_call
= slab_memory_callback
,
3589 .priority
= SLAB_CALLBACK_PRI
,
3592 /********************************************************************
3593 * Basic setup of slabs
3594 *******************************************************************/
3597 * Used for early kmem_cache structures that were allocated using
3598 * the page allocator. Allocate them properly then fix up the pointers
3599 * that may be pointing to the wrong kmem_cache structure.
3602 static struct kmem_cache
* __init
bootstrap(struct kmem_cache
*static_cache
)
3605 struct kmem_cache
*s
= kmem_cache_zalloc(kmem_cache
, GFP_NOWAIT
);
3607 memcpy(s
, static_cache
, kmem_cache
->object_size
);
3610 * This runs very early, and only the boot processor is supposed to be
3611 * up. Even if it weren't true, IRQs are not up so we couldn't fire
3614 __flush_cpu_slab(s
, smp_processor_id());
3615 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3616 struct kmem_cache_node
*n
= get_node(s
, node
);
3620 list_for_each_entry(p
, &n
->partial
, lru
)
3623 #ifdef CONFIG_SLUB_DEBUG
3624 list_for_each_entry(p
, &n
->full
, lru
)
3629 list_add(&s
->list
, &slab_caches
);
3633 void __init
kmem_cache_init(void)
3635 static __initdata
struct kmem_cache boot_kmem_cache
,
3636 boot_kmem_cache_node
;
3638 if (debug_guardpage_minorder())
3641 kmem_cache_node
= &boot_kmem_cache_node
;
3642 kmem_cache
= &boot_kmem_cache
;
3644 create_boot_cache(kmem_cache_node
, "kmem_cache_node",
3645 sizeof(struct kmem_cache_node
), SLAB_HWCACHE_ALIGN
);
3647 register_hotmemory_notifier(&slab_memory_callback_nb
);
3649 /* Able to allocate the per node structures */
3650 slab_state
= PARTIAL
;
3652 create_boot_cache(kmem_cache
, "kmem_cache",
3653 offsetof(struct kmem_cache
, node
) +
3654 nr_node_ids
* sizeof(struct kmem_cache_node
*),
3655 SLAB_HWCACHE_ALIGN
);
3657 kmem_cache
= bootstrap(&boot_kmem_cache
);
3660 * Allocate kmem_cache_node properly from the kmem_cache slab.
3661 * kmem_cache_node is separately allocated so no need to
3662 * update any list pointers.
3664 kmem_cache_node
= bootstrap(&boot_kmem_cache_node
);
3666 /* Now we can use the kmem_cache to allocate kmalloc slabs */
3667 create_kmalloc_caches(0);
3670 register_cpu_notifier(&slab_notifier
);
3674 "SLUB: HWalign=%d, Order=%d-%d, MinObjects=%d,"
3675 " CPUs=%d, Nodes=%d\n",
3677 slub_min_order
, slub_max_order
, slub_min_objects
,
3678 nr_cpu_ids
, nr_node_ids
);
3681 void __init
kmem_cache_init_late(void)
3686 * Find a mergeable slab cache
3688 static int slab_unmergeable(struct kmem_cache
*s
)
3690 if (slub_nomerge
|| (s
->flags
& SLUB_NEVER_MERGE
))
3697 * We may have set a slab to be unmergeable during bootstrap.
3699 if (s
->refcount
< 0)
3705 static struct kmem_cache
*find_mergeable(struct mem_cgroup
*memcg
, size_t size
,
3706 size_t align
, unsigned long flags
, const char *name
,
3707 void (*ctor
)(void *))
3709 struct kmem_cache
*s
;
3711 if (slub_nomerge
|| (flags
& SLUB_NEVER_MERGE
))
3717 size
= ALIGN(size
, sizeof(void *));
3718 align
= calculate_alignment(flags
, align
, size
);
3719 size
= ALIGN(size
, align
);
3720 flags
= kmem_cache_flags(size
, flags
, name
, NULL
);
3722 list_for_each_entry(s
, &slab_caches
, list
) {
3723 if (slab_unmergeable(s
))
3729 if ((flags
& SLUB_MERGE_SAME
) != (s
->flags
& SLUB_MERGE_SAME
))
3732 * Check if alignment is compatible.
3733 * Courtesy of Adrian Drzewiecki
3735 if ((s
->size
& ~(align
- 1)) != s
->size
)
3738 if (s
->size
- size
>= sizeof(void *))
3741 if (!cache_match_memcg(s
, memcg
))
3750 __kmem_cache_alias(struct mem_cgroup
*memcg
, const char *name
, size_t size
,
3751 size_t align
, unsigned long flags
, void (*ctor
)(void *))
3753 struct kmem_cache
*s
;
3755 s
= find_mergeable(memcg
, size
, align
, flags
, name
, ctor
);
3759 * Adjust the object sizes so that we clear
3760 * the complete object on kzalloc.
3762 s
->object_size
= max(s
->object_size
, (int)size
);
3763 s
->inuse
= max_t(int, s
->inuse
, ALIGN(size
, sizeof(void *)));
3765 if (sysfs_slab_alias(s
, name
)) {
3774 int __kmem_cache_create(struct kmem_cache
*s
, unsigned long flags
)
3778 err
= kmem_cache_open(s
, flags
);
3782 /* Mutex is not taken during early boot */
3783 if (slab_state
<= UP
)
3786 memcg_propagate_slab_attrs(s
);
3787 mutex_unlock(&slab_mutex
);
3788 err
= sysfs_slab_add(s
);
3789 mutex_lock(&slab_mutex
);
3792 kmem_cache_close(s
);
3799 * Use the cpu notifier to insure that the cpu slabs are flushed when
3802 static int __cpuinit
slab_cpuup_callback(struct notifier_block
*nfb
,
3803 unsigned long action
, void *hcpu
)
3805 long cpu
= (long)hcpu
;
3806 struct kmem_cache
*s
;
3807 unsigned long flags
;
3810 case CPU_UP_CANCELED
:
3811 case CPU_UP_CANCELED_FROZEN
:
3813 case CPU_DEAD_FROZEN
:
3814 mutex_lock(&slab_mutex
);
3815 list_for_each_entry(s
, &slab_caches
, list
) {
3816 local_irq_save(flags
);
3817 __flush_cpu_slab(s
, cpu
);
3818 local_irq_restore(flags
);
3820 mutex_unlock(&slab_mutex
);
3828 static struct notifier_block __cpuinitdata slab_notifier
= {
3829 .notifier_call
= slab_cpuup_callback
3834 void *__kmalloc_track_caller(size_t size
, gfp_t gfpflags
, unsigned long caller
)
3836 struct kmem_cache
*s
;
3839 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
))
3840 return kmalloc_large(size
, gfpflags
);
3842 s
= kmalloc_slab(size
, gfpflags
);
3844 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3847 ret
= slab_alloc(s
, gfpflags
, caller
);
3849 /* Honor the call site pointer we received. */
3850 trace_kmalloc(caller
, ret
, size
, s
->size
, gfpflags
);
3856 void *__kmalloc_node_track_caller(size_t size
, gfp_t gfpflags
,
3857 int node
, unsigned long caller
)
3859 struct kmem_cache
*s
;
3862 if (unlikely(size
> KMALLOC_MAX_CACHE_SIZE
)) {
3863 ret
= kmalloc_large_node(size
, gfpflags
, node
);
3865 trace_kmalloc_node(caller
, ret
,
3866 size
, PAGE_SIZE
<< get_order(size
),
3872 s
= kmalloc_slab(size
, gfpflags
);
3874 if (unlikely(ZERO_OR_NULL_PTR(s
)))
3877 ret
= slab_alloc_node(s
, gfpflags
, node
, caller
);
3879 /* Honor the call site pointer we received. */
3880 trace_kmalloc_node(caller
, ret
, size
, s
->size
, gfpflags
, node
);
3887 static int count_inuse(struct page
*page
)
3892 static int count_total(struct page
*page
)
3894 return page
->objects
;
3898 #ifdef CONFIG_SLUB_DEBUG
3899 static int validate_slab(struct kmem_cache
*s
, struct page
*page
,
3903 void *addr
= page_address(page
);
3905 if (!check_slab(s
, page
) ||
3906 !on_freelist(s
, page
, NULL
))
3909 /* Now we know that a valid freelist exists */
3910 bitmap_zero(map
, page
->objects
);
3912 get_map(s
, page
, map
);
3913 for_each_object(p
, s
, addr
, page
->objects
) {
3914 if (test_bit(slab_index(p
, s
, addr
), map
))
3915 if (!check_object(s
, page
, p
, SLUB_RED_INACTIVE
))
3919 for_each_object(p
, s
, addr
, page
->objects
)
3920 if (!test_bit(slab_index(p
, s
, addr
), map
))
3921 if (!check_object(s
, page
, p
, SLUB_RED_ACTIVE
))
3926 static void validate_slab_slab(struct kmem_cache
*s
, struct page
*page
,
3930 validate_slab(s
, page
, map
);
3934 static int validate_slab_node(struct kmem_cache
*s
,
3935 struct kmem_cache_node
*n
, unsigned long *map
)
3937 unsigned long count
= 0;
3939 unsigned long flags
;
3941 spin_lock_irqsave(&n
->list_lock
, flags
);
3943 list_for_each_entry(page
, &n
->partial
, lru
) {
3944 validate_slab_slab(s
, page
, map
);
3947 if (count
!= n
->nr_partial
)
3948 printk(KERN_ERR
"SLUB %s: %ld partial slabs counted but "
3949 "counter=%ld\n", s
->name
, count
, n
->nr_partial
);
3951 if (!(s
->flags
& SLAB_STORE_USER
))
3954 list_for_each_entry(page
, &n
->full
, lru
) {
3955 validate_slab_slab(s
, page
, map
);
3958 if (count
!= atomic_long_read(&n
->nr_slabs
))
3959 printk(KERN_ERR
"SLUB: %s %ld slabs counted but "
3960 "counter=%ld\n", s
->name
, count
,
3961 atomic_long_read(&n
->nr_slabs
));
3964 spin_unlock_irqrestore(&n
->list_lock
, flags
);
3968 static long validate_slab_cache(struct kmem_cache
*s
)
3971 unsigned long count
= 0;
3972 unsigned long *map
= kmalloc(BITS_TO_LONGS(oo_objects(s
->max
)) *
3973 sizeof(unsigned long), GFP_KERNEL
);
3979 for_each_node_state(node
, N_NORMAL_MEMORY
) {
3980 struct kmem_cache_node
*n
= get_node(s
, node
);
3982 count
+= validate_slab_node(s
, n
, map
);
3988 * Generate lists of code addresses where slabcache objects are allocated
3992 #ifdef CONFIG_MTK_MEMCFG
3993 #define MTK_MEMCFG_SLABTRACE_CNT 4
3994 /* MTK_MEMCFG_SLABTRACE_CNT should be always <= TRACK_ADDRS_COUNT */
3995 #if (MTK_MEMCFG_SLABTRACE_CNT > TRACK_ADDRS_COUNT)
3996 #error (MTK_MEMCFG_SLABTRACE_CNT > TRACK_ADDRS_COUNT)
4001 unsigned long count
;
4003 #ifdef CONFIG_MTK_MEMCFG
4004 #ifdef CONFIG_STACKTRACE
4005 unsigned long addrs
[MTK_MEMCFG_SLABTRACE_CNT
]; /* Called from address */
4013 DECLARE_BITMAP(cpus
, NR_CPUS
);
4019 unsigned long count
;
4020 struct location
*loc
;
4023 static void free_loc_track(struct loc_track
*t
)
4026 #ifndef CONFIG_MTK_PAGERECORDER
4027 free_pages((unsigned long)t
->loc
,
4028 get_order(sizeof(struct location
) * t
->max
));
4030 __free_pages_nopagedebug((struct page
*)t
->loc
,
4031 get_order(sizeof(struct location
) * t
->max
));
4035 static int alloc_loc_track(struct loc_track
*t
, unsigned long max
, gfp_t flags
)
4040 order
= get_order(sizeof(struct location
) * max
);
4042 #ifndef CONFIG_MTK_PAGERECORDER
4043 l
= (void *)__get_free_pages(flags
, order
);
4045 l
= (void *)__get_free_pages_nopagedebug(flags
, order
);
4051 memcpy(l
, t
->loc
, sizeof(struct location
) * t
->count
);
4059 static int add_location(struct loc_track
*t
, struct kmem_cache
*s
,
4060 const struct track
*track
)
4062 long start
, end
, pos
;
4064 unsigned long caddr
;
4065 unsigned long age
= jiffies
- track
->when
;
4071 pos
= start
+ (end
- start
+ 1) / 2;
4074 * There is nothing at "end". If we end up there
4075 * we need to add something to before end.
4080 caddr
= t
->loc
[pos
].addr
;
4081 if (track
->addr
== caddr
) {
4087 if (age
< l
->min_time
)
4089 if (age
> l
->max_time
)
4092 if (track
->pid
< l
->min_pid
)
4093 l
->min_pid
= track
->pid
;
4094 if (track
->pid
> l
->max_pid
)
4095 l
->max_pid
= track
->pid
;
4097 cpumask_set_cpu(track
->cpu
,
4098 to_cpumask(l
->cpus
));
4100 node_set(page_to_nid(virt_to_page(track
)), l
->nodes
);
4104 if (track
->addr
< caddr
)
4111 * Not found. Insert new tracking element.
4113 if (t
->count
>= t
->max
&& !alloc_loc_track(t
, 2 * t
->max
, GFP_ATOMIC
))
4119 (t
->count
- pos
) * sizeof(struct location
));
4122 l
->addr
= track
->addr
;
4126 l
->min_pid
= track
->pid
;
4127 l
->max_pid
= track
->pid
;
4128 cpumask_clear(to_cpumask(l
->cpus
));
4129 cpumask_set_cpu(track
->cpu
, to_cpumask(l
->cpus
));
4130 nodes_clear(l
->nodes
);
4131 node_set(page_to_nid(virt_to_page(track
)), l
->nodes
);
4135 static void process_slab(struct loc_track
*t
, struct kmem_cache
*s
,
4136 struct page
*page
, enum track_item alloc
,
4139 void *addr
= page_address(page
);
4142 bitmap_zero(map
, page
->objects
);
4143 get_map(s
, page
, map
);
4145 for_each_object(p
, s
, addr
, page
->objects
)
4146 if (!test_bit(slab_index(p
, s
, addr
), map
))
4147 add_location(t
, s
, get_track(s
, p
, alloc
));
4150 static int list_locations(struct kmem_cache
*s
, char *buf
,
4151 enum track_item alloc
)
4155 struct loc_track t
= { 0, 0, NULL
};
4157 unsigned long *map
= kmalloc(BITS_TO_LONGS(oo_objects(s
->max
)) *
4158 sizeof(unsigned long), GFP_KERNEL
);
4160 if (!map
|| !alloc_loc_track(&t
, PAGE_SIZE
/ sizeof(struct location
),
4163 return sprintf(buf
, "Out of memory\n");
4165 /* Push back cpu slabs */
4168 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4169 struct kmem_cache_node
*n
= get_node(s
, node
);
4170 unsigned long flags
;
4173 if (!atomic_long_read(&n
->nr_slabs
))
4176 spin_lock_irqsave(&n
->list_lock
, flags
);
4177 list_for_each_entry(page
, &n
->partial
, lru
)
4178 process_slab(&t
, s
, page
, alloc
, map
);
4179 list_for_each_entry(page
, &n
->full
, lru
)
4180 process_slab(&t
, s
, page
, alloc
, map
);
4181 spin_unlock_irqrestore(&n
->list_lock
, flags
);
4184 for (i
= 0; i
< t
.count
; i
++) {
4185 struct location
*l
= &t
.loc
[i
];
4187 if (len
> PAGE_SIZE
- KSYM_SYMBOL_LEN
- 100)
4189 len
+= sprintf(buf
+ len
, "%7ld ", l
->count
);
4192 len
+= sprintf(buf
+ len
, "%pS", (void *)l
->addr
);
4194 len
+= sprintf(buf
+ len
, "<not-available>");
4196 if (l
->sum_time
!= l
->min_time
) {
4197 len
+= sprintf(buf
+ len
, " age=%ld/%ld/%ld",
4199 (long)div_u64(l
->sum_time
, l
->count
),
4202 len
+= sprintf(buf
+ len
, " age=%ld",
4205 if (l
->min_pid
!= l
->max_pid
)
4206 len
+= sprintf(buf
+ len
, " pid=%ld-%ld",
4207 l
->min_pid
, l
->max_pid
);
4209 len
+= sprintf(buf
+ len
, " pid=%ld",
4212 if (num_online_cpus() > 1 &&
4213 !cpumask_empty(to_cpumask(l
->cpus
)) &&
4214 len
< PAGE_SIZE
- 60) {
4215 len
+= sprintf(buf
+ len
, " cpus=");
4216 len
+= cpulist_scnprintf(buf
+ len
, PAGE_SIZE
- len
- 50,
4217 to_cpumask(l
->cpus
));
4220 if (nr_online_nodes
> 1 && !nodes_empty(l
->nodes
) &&
4221 len
< PAGE_SIZE
- 60) {
4222 len
+= sprintf(buf
+ len
, " nodes=");
4223 len
+= nodelist_scnprintf(buf
+ len
, PAGE_SIZE
- len
- 50,
4227 len
+= sprintf(buf
+ len
, "\n");
4233 len
+= sprintf(buf
, "No data\n");
4238 #ifdef SLUB_RESILIENCY_TEST
4239 static void resiliency_test(void)
4243 BUILD_BUG_ON(KMALLOC_MIN_SIZE
> 16 || KMALLOC_SHIFT_HIGH
< 10);
4245 printk(KERN_ERR
"SLUB resiliency testing\n");
4246 printk(KERN_ERR
"-----------------------\n");
4247 printk(KERN_ERR
"A. Corruption after allocation\n");
4249 p
= kzalloc(16, GFP_KERNEL
);
4251 printk(KERN_ERR
"\n1. kmalloc-16: Clobber Redzone/next pointer"
4252 " 0x12->0x%p\n\n", p
+ 16);
4254 validate_slab_cache(kmalloc_caches
[4]);
4256 /* Hmmm... The next two are dangerous */
4257 p
= kzalloc(32, GFP_KERNEL
);
4258 p
[32 + sizeof(void *)] = 0x34;
4259 printk(KERN_ERR
"\n2. kmalloc-32: Clobber next pointer/next slab"
4260 " 0x34 -> -0x%p\n", p
);
4262 "If allocated object is overwritten then not detectable\n\n");
4264 validate_slab_cache(kmalloc_caches
[5]);
4265 p
= kzalloc(64, GFP_KERNEL
);
4266 p
+= 64 + (get_cycles() & 0xff) * sizeof(void *);
4268 printk(KERN_ERR
"\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4271 "If allocated object is overwritten then not detectable\n\n");
4272 validate_slab_cache(kmalloc_caches
[6]);
4274 printk(KERN_ERR
"\nB. Corruption after free\n");
4275 p
= kzalloc(128, GFP_KERNEL
);
4278 printk(KERN_ERR
"1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p
);
4279 validate_slab_cache(kmalloc_caches
[7]);
4281 p
= kzalloc(256, GFP_KERNEL
);
4284 printk(KERN_ERR
"\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
4286 validate_slab_cache(kmalloc_caches
[8]);
4288 p
= kzalloc(512, GFP_KERNEL
);
4291 printk(KERN_ERR
"\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p
);
4292 validate_slab_cache(kmalloc_caches
[9]);
4296 static void resiliency_test(void) {};
4301 enum slab_stat_type
{
4302 SL_ALL
, /* All slabs */
4303 SL_PARTIAL
, /* Only partially allocated slabs */
4304 SL_CPU
, /* Only slabs used for cpu caches */
4305 SL_OBJECTS
, /* Determine allocated objects not slabs */
4306 SL_TOTAL
/* Determine object capacity not slabs */
4309 #define SO_ALL (1 << SL_ALL)
4310 #define SO_PARTIAL (1 << SL_PARTIAL)
4311 #define SO_CPU (1 << SL_CPU)
4312 #define SO_OBJECTS (1 << SL_OBJECTS)
4313 #define SO_TOTAL (1 << SL_TOTAL)
4315 static ssize_t
show_slab_objects(struct kmem_cache
*s
,
4316 char *buf
, unsigned long flags
)
4318 unsigned long total
= 0;
4321 unsigned long *nodes
;
4322 unsigned long *per_cpu
;
4324 nodes
= kzalloc(2 * sizeof(unsigned long) * nr_node_ids
, GFP_KERNEL
);
4327 per_cpu
= nodes
+ nr_node_ids
;
4329 if (flags
& SO_CPU
) {
4332 for_each_possible_cpu(cpu
) {
4333 struct kmem_cache_cpu
*c
= per_cpu_ptr(s
->cpu_slab
, cpu
);
4337 page
= ACCESS_ONCE(c
->page
);
4341 node
= page_to_nid(page
);
4342 if (flags
& SO_TOTAL
)
4344 else if (flags
& SO_OBJECTS
)
4352 page
= ACCESS_ONCE(c
->partial
);
4354 node
= page_to_nid(page
);
4355 if (flags
& SO_TOTAL
)
4357 else if (flags
& SO_OBJECTS
)
4369 lock_memory_hotplug();
4370 #ifdef CONFIG_SLUB_DEBUG
4371 if (flags
& SO_ALL
) {
4372 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4373 struct kmem_cache_node
*n
= get_node(s
, node
);
4375 if (flags
& SO_TOTAL
)
4376 x
= atomic_long_read(&n
->total_objects
);
4377 else if (flags
& SO_OBJECTS
)
4378 x
= atomic_long_read(&n
->total_objects
) -
4379 count_partial(n
, count_free
);
4382 x
= atomic_long_read(&n
->nr_slabs
);
4389 if (flags
& SO_PARTIAL
) {
4390 for_each_node_state(node
, N_NORMAL_MEMORY
) {
4391 struct kmem_cache_node
*n
= get_node(s
, node
);
4393 if (flags
& SO_TOTAL
)
4394 x
= count_partial(n
, count_total
);
4395 else if (flags
& SO_OBJECTS
)
4396 x
= count_partial(n
, count_inuse
);
4403 x
= sprintf(buf
, "%lu", total
);
4405 for_each_node_state(node
, N_NORMAL_MEMORY
)
4407 x
+= sprintf(buf
+ x
, " N%d=%lu",
4410 unlock_memory_hotplug();
4412 return x
+ sprintf(buf
+ x
, "\n");
4415 #ifdef CONFIG_SLUB_DEBUG
4416 static int any_slab_objects(struct kmem_cache
*s
)
4420 for_each_online_node(node
) {
4421 struct kmem_cache_node
*n
= get_node(s
, node
);
4426 if (atomic_long_read(&n
->total_objects
))
4433 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
4434 #define to_slab(n) container_of(n, struct kmem_cache, kobj)
4436 struct slab_attribute
{
4437 struct attribute attr
;
4438 ssize_t (*show
)(struct kmem_cache
*s
, char *buf
);
4439 ssize_t (*store
)(struct kmem_cache
*s
, const char *x
, size_t count
);
4442 #define SLAB_ATTR_RO(_name) \
4443 static struct slab_attribute _name##_attr = \
4444 __ATTR(_name, 0400, _name##_show, NULL)
4446 #define SLAB_ATTR(_name) \
4447 static struct slab_attribute _name##_attr = \
4448 __ATTR(_name, 0600, _name##_show, _name##_store)
4450 static ssize_t
slab_size_show(struct kmem_cache
*s
, char *buf
)
4452 return sprintf(buf
, "%d\n", s
->size
);
4454 SLAB_ATTR_RO(slab_size
);
4456 static ssize_t
align_show(struct kmem_cache
*s
, char *buf
)
4458 return sprintf(buf
, "%d\n", s
->align
);
4460 SLAB_ATTR_RO(align
);
4462 static ssize_t
object_size_show(struct kmem_cache
*s
, char *buf
)
4464 return sprintf(buf
, "%d\n", s
->object_size
);
4466 SLAB_ATTR_RO(object_size
);
4468 static ssize_t
objs_per_slab_show(struct kmem_cache
*s
, char *buf
)
4470 return sprintf(buf
, "%d\n", oo_objects(s
->oo
));
4472 SLAB_ATTR_RO(objs_per_slab
);
4474 static ssize_t
order_store(struct kmem_cache
*s
,
4475 const char *buf
, size_t length
)
4477 unsigned long order
;
4480 err
= strict_strtoul(buf
, 10, &order
);
4484 if (order
> slub_max_order
|| order
< slub_min_order
)
4487 calculate_sizes(s
, order
);
4491 static ssize_t
order_show(struct kmem_cache
*s
, char *buf
)
4493 return sprintf(buf
, "%d\n", oo_order(s
->oo
));
4497 static ssize_t
min_partial_show(struct kmem_cache
*s
, char *buf
)
4499 return sprintf(buf
, "%lu\n", s
->min_partial
);
4502 static ssize_t
min_partial_store(struct kmem_cache
*s
, const char *buf
,
4508 err
= strict_strtoul(buf
, 10, &min
);
4512 set_min_partial(s
, min
);
4515 SLAB_ATTR(min_partial
);
4517 static ssize_t
cpu_partial_show(struct kmem_cache
*s
, char *buf
)
4519 return sprintf(buf
, "%u\n", s
->cpu_partial
);
4522 static ssize_t
cpu_partial_store(struct kmem_cache
*s
, const char *buf
,
4525 unsigned long objects
;
4528 err
= strict_strtoul(buf
, 10, &objects
);
4531 if (objects
&& kmem_cache_debug(s
))
4534 s
->cpu_partial
= objects
;
4538 SLAB_ATTR(cpu_partial
);
4540 static ssize_t
ctor_show(struct kmem_cache
*s
, char *buf
)
4544 return sprintf(buf
, "%pS\n", s
->ctor
);
4548 static ssize_t
aliases_show(struct kmem_cache
*s
, char *buf
)
4550 return sprintf(buf
, "%d\n", s
->refcount
- 1);
4552 SLAB_ATTR_RO(aliases
);
4554 static ssize_t
partial_show(struct kmem_cache
*s
, char *buf
)
4556 return show_slab_objects(s
, buf
, SO_PARTIAL
);
4558 SLAB_ATTR_RO(partial
);
4560 static ssize_t
cpu_slabs_show(struct kmem_cache
*s
, char *buf
)
4562 return show_slab_objects(s
, buf
, SO_CPU
);
4564 SLAB_ATTR_RO(cpu_slabs
);
4566 static ssize_t
objects_show(struct kmem_cache
*s
, char *buf
)
4568 return show_slab_objects(s
, buf
, SO_ALL
|SO_OBJECTS
);
4570 SLAB_ATTR_RO(objects
);
4572 static ssize_t
objects_partial_show(struct kmem_cache
*s
, char *buf
)
4574 return show_slab_objects(s
, buf
, SO_PARTIAL
|SO_OBJECTS
);
4576 SLAB_ATTR_RO(objects_partial
);
4578 static ssize_t
slabs_cpu_partial_show(struct kmem_cache
*s
, char *buf
)
4585 for_each_online_cpu(cpu
) {
4586 struct page
*page
= per_cpu_ptr(s
->cpu_slab
, cpu
)->partial
;
4589 pages
+= page
->pages
;
4590 objects
+= page
->pobjects
;
4594 len
= sprintf(buf
, "%d(%d)", objects
, pages
);
4597 for_each_online_cpu(cpu
) {
4598 struct page
*page
= per_cpu_ptr(s
->cpu_slab
, cpu
) ->partial
;
4600 if (page
&& len
< PAGE_SIZE
- 20)
4601 len
+= sprintf(buf
+ len
, " C%d=%d(%d)", cpu
,
4602 page
->pobjects
, page
->pages
);
4605 return len
+ sprintf(buf
+ len
, "\n");
4607 SLAB_ATTR_RO(slabs_cpu_partial
);
4609 static ssize_t
reclaim_account_show(struct kmem_cache
*s
, char *buf
)
4611 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RECLAIM_ACCOUNT
));
4614 static ssize_t
reclaim_account_store(struct kmem_cache
*s
,
4615 const char *buf
, size_t length
)
4617 s
->flags
&= ~SLAB_RECLAIM_ACCOUNT
;
4619 s
->flags
|= SLAB_RECLAIM_ACCOUNT
;
4622 SLAB_ATTR(reclaim_account
);
4624 static ssize_t
hwcache_align_show(struct kmem_cache
*s
, char *buf
)
4626 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_HWCACHE_ALIGN
));
4628 SLAB_ATTR_RO(hwcache_align
);
4630 #ifdef CONFIG_ZONE_DMA
4631 static ssize_t
cache_dma_show(struct kmem_cache
*s
, char *buf
)
4633 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_CACHE_DMA
));
4635 SLAB_ATTR_RO(cache_dma
);
4638 static ssize_t
destroy_by_rcu_show(struct kmem_cache
*s
, char *buf
)
4640 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DESTROY_BY_RCU
));
4642 SLAB_ATTR_RO(destroy_by_rcu
);
4644 static ssize_t
reserved_show(struct kmem_cache
*s
, char *buf
)
4646 return sprintf(buf
, "%d\n", s
->reserved
);
4648 SLAB_ATTR_RO(reserved
);
4650 #ifdef CONFIG_SLUB_DEBUG
4651 static ssize_t
slabs_show(struct kmem_cache
*s
, char *buf
)
4653 return show_slab_objects(s
, buf
, SO_ALL
);
4655 SLAB_ATTR_RO(slabs
);
4657 static ssize_t
total_objects_show(struct kmem_cache
*s
, char *buf
)
4659 return show_slab_objects(s
, buf
, SO_ALL
|SO_TOTAL
);
4661 SLAB_ATTR_RO(total_objects
);
4663 static ssize_t
sanity_checks_show(struct kmem_cache
*s
, char *buf
)
4665 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_DEBUG_FREE
));
4668 static ssize_t
sanity_checks_store(struct kmem_cache
*s
,
4669 const char *buf
, size_t length
)
4671 s
->flags
&= ~SLAB_DEBUG_FREE
;
4672 if (buf
[0] == '1') {
4673 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4674 s
->flags
|= SLAB_DEBUG_FREE
;
4678 SLAB_ATTR(sanity_checks
);
4680 static ssize_t
trace_show(struct kmem_cache
*s
, char *buf
)
4682 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_TRACE
));
4685 static ssize_t
trace_store(struct kmem_cache
*s
, const char *buf
,
4688 s
->flags
&= ~SLAB_TRACE
;
4689 if (buf
[0] == '1') {
4690 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4691 s
->flags
|= SLAB_TRACE
;
4697 static ssize_t
red_zone_show(struct kmem_cache
*s
, char *buf
)
4699 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_RED_ZONE
));
4702 static ssize_t
red_zone_store(struct kmem_cache
*s
,
4703 const char *buf
, size_t length
)
4705 if (any_slab_objects(s
))
4708 s
->flags
&= ~SLAB_RED_ZONE
;
4709 if (buf
[0] == '1') {
4710 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4711 s
->flags
|= SLAB_RED_ZONE
;
4713 calculate_sizes(s
, -1);
4716 SLAB_ATTR(red_zone
);
4718 static ssize_t
poison_show(struct kmem_cache
*s
, char *buf
)
4720 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_POISON
));
4723 static ssize_t
poison_store(struct kmem_cache
*s
,
4724 const char *buf
, size_t length
)
4726 if (any_slab_objects(s
))
4729 s
->flags
&= ~SLAB_POISON
;
4730 if (buf
[0] == '1') {
4731 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4732 s
->flags
|= SLAB_POISON
;
4734 calculate_sizes(s
, -1);
4739 static ssize_t
store_user_show(struct kmem_cache
*s
, char *buf
)
4741 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_STORE_USER
));
4744 static ssize_t
store_user_store(struct kmem_cache
*s
,
4745 const char *buf
, size_t length
)
4747 if (any_slab_objects(s
))
4750 s
->flags
&= ~SLAB_STORE_USER
;
4751 if (buf
[0] == '1') {
4752 s
->flags
&= ~__CMPXCHG_DOUBLE
;
4753 s
->flags
|= SLAB_STORE_USER
;
4755 calculate_sizes(s
, -1);
4758 SLAB_ATTR(store_user
);
4760 static ssize_t
validate_show(struct kmem_cache
*s
, char *buf
)
4765 static ssize_t
validate_store(struct kmem_cache
*s
,
4766 const char *buf
, size_t length
)
4770 if (buf
[0] == '1') {
4771 ret
= validate_slab_cache(s
);
4777 SLAB_ATTR(validate
);
4779 static ssize_t
alloc_calls_show(struct kmem_cache
*s
, char *buf
)
4781 if (!(s
->flags
& SLAB_STORE_USER
))
4783 return list_locations(s
, buf
, TRACK_ALLOC
);
4785 SLAB_ATTR_RO(alloc_calls
);
4787 static ssize_t
free_calls_show(struct kmem_cache
*s
, char *buf
)
4789 if (!(s
->flags
& SLAB_STORE_USER
))
4791 return list_locations(s
, buf
, TRACK_FREE
);
4793 SLAB_ATTR_RO(free_calls
);
4794 #endif /* CONFIG_SLUB_DEBUG */
4796 #ifdef CONFIG_FAILSLAB
4797 static ssize_t
failslab_show(struct kmem_cache
*s
, char *buf
)
4799 return sprintf(buf
, "%d\n", !!(s
->flags
& SLAB_FAILSLAB
));
4802 static ssize_t
failslab_store(struct kmem_cache
*s
, const char *buf
,
4805 s
->flags
&= ~SLAB_FAILSLAB
;
4807 s
->flags
|= SLAB_FAILSLAB
;
4810 SLAB_ATTR(failslab
);
4813 static ssize_t
shrink_show(struct kmem_cache
*s
, char *buf
)
4818 static ssize_t
shrink_store(struct kmem_cache
*s
,
4819 const char *buf
, size_t length
)
4821 if (buf
[0] == '1') {
4822 int rc
= kmem_cache_shrink(s
);
4833 static ssize_t
remote_node_defrag_ratio_show(struct kmem_cache
*s
, char *buf
)
4835 return sprintf(buf
, "%d\n", s
->remote_node_defrag_ratio
/ 10);
4838 static ssize_t
remote_node_defrag_ratio_store(struct kmem_cache
*s
,
4839 const char *buf
, size_t length
)
4841 unsigned long ratio
;
4844 err
= strict_strtoul(buf
, 10, &ratio
);
4849 s
->remote_node_defrag_ratio
= ratio
* 10;
4853 SLAB_ATTR(remote_node_defrag_ratio
);
4856 #ifdef CONFIG_SLUB_STATS
4857 static int show_stat(struct kmem_cache
*s
, char *buf
, enum stat_item si
)
4859 unsigned long sum
= 0;
4862 int *data
= kmalloc(nr_cpu_ids
* sizeof(int), GFP_KERNEL
);
4867 for_each_online_cpu(cpu
) {
4868 unsigned x
= per_cpu_ptr(s
->cpu_slab
, cpu
)->stat
[si
];
4874 len
= sprintf(buf
, "%lu", sum
);
4877 for_each_online_cpu(cpu
) {
4878 if (data
[cpu
] && len
< PAGE_SIZE
- 20)
4879 len
+= sprintf(buf
+ len
, " C%d=%u", cpu
, data
[cpu
]);
4883 return len
+ sprintf(buf
+ len
, "\n");
4886 static void clear_stat(struct kmem_cache
*s
, enum stat_item si
)
4890 for_each_online_cpu(cpu
)
4891 per_cpu_ptr(s
->cpu_slab
, cpu
)->stat
[si
] = 0;
4894 #define STAT_ATTR(si, text) \
4895 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4897 return show_stat(s, buf, si); \
4899 static ssize_t text##_store(struct kmem_cache *s, \
4900 const char *buf, size_t length) \
4902 if (buf[0] != '0') \
4904 clear_stat(s, si); \
4909 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4910 STAT_ATTR(ALLOC_SLOWPATH
, alloc_slowpath
);
4911 STAT_ATTR(FREE_FASTPATH
, free_fastpath
);
4912 STAT_ATTR(FREE_SLOWPATH
, free_slowpath
);
4913 STAT_ATTR(FREE_FROZEN
, free_frozen
);
4914 STAT_ATTR(FREE_ADD_PARTIAL
, free_add_partial
);
4915 STAT_ATTR(FREE_REMOVE_PARTIAL
, free_remove_partial
);
4916 STAT_ATTR(ALLOC_FROM_PARTIAL
, alloc_from_partial
);
4917 STAT_ATTR(ALLOC_SLAB
, alloc_slab
);
4918 STAT_ATTR(ALLOC_REFILL
, alloc_refill
);
4919 STAT_ATTR(ALLOC_NODE_MISMATCH
, alloc_node_mismatch
);
4920 STAT_ATTR(FREE_SLAB
, free_slab
);
4921 STAT_ATTR(CPUSLAB_FLUSH
, cpuslab_flush
);
4922 STAT_ATTR(DEACTIVATE_FULL
, deactivate_full
);
4923 STAT_ATTR(DEACTIVATE_EMPTY
, deactivate_empty
);
4924 STAT_ATTR(DEACTIVATE_TO_HEAD
, deactivate_to_head
);
4925 STAT_ATTR(DEACTIVATE_TO_TAIL
, deactivate_to_tail
);
4926 STAT_ATTR(DEACTIVATE_REMOTE_FREES
, deactivate_remote_frees
);
4927 STAT_ATTR(DEACTIVATE_BYPASS
, deactivate_bypass
);
4928 STAT_ATTR(ORDER_FALLBACK
, order_fallback
);
4929 STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL
, cmpxchg_double_cpu_fail
);
4930 STAT_ATTR(CMPXCHG_DOUBLE_FAIL
, cmpxchg_double_fail
);
4931 STAT_ATTR(CPU_PARTIAL_ALLOC
, cpu_partial_alloc
);
4932 STAT_ATTR(CPU_PARTIAL_FREE
, cpu_partial_free
);
4933 STAT_ATTR(CPU_PARTIAL_NODE
, cpu_partial_node
);
4934 STAT_ATTR(CPU_PARTIAL_DRAIN
, cpu_partial_drain
);
4937 static struct attribute
*slab_attrs
[] = {
4938 &slab_size_attr
.attr
,
4939 &object_size_attr
.attr
,
4940 &objs_per_slab_attr
.attr
,
4942 &min_partial_attr
.attr
,
4943 &cpu_partial_attr
.attr
,
4945 &objects_partial_attr
.attr
,
4947 &cpu_slabs_attr
.attr
,
4951 &hwcache_align_attr
.attr
,
4952 &reclaim_account_attr
.attr
,
4953 &destroy_by_rcu_attr
.attr
,
4955 &reserved_attr
.attr
,
4956 &slabs_cpu_partial_attr
.attr
,
4957 #ifdef CONFIG_SLUB_DEBUG
4958 &total_objects_attr
.attr
,
4960 &sanity_checks_attr
.attr
,
4962 &red_zone_attr
.attr
,
4964 &store_user_attr
.attr
,
4965 &validate_attr
.attr
,
4966 &alloc_calls_attr
.attr
,
4967 &free_calls_attr
.attr
,
4969 #ifdef CONFIG_ZONE_DMA
4970 &cache_dma_attr
.attr
,
4973 &remote_node_defrag_ratio_attr
.attr
,
4975 #ifdef CONFIG_SLUB_STATS
4976 &alloc_fastpath_attr
.attr
,
4977 &alloc_slowpath_attr
.attr
,
4978 &free_fastpath_attr
.attr
,
4979 &free_slowpath_attr
.attr
,
4980 &free_frozen_attr
.attr
,
4981 &free_add_partial_attr
.attr
,
4982 &free_remove_partial_attr
.attr
,
4983 &alloc_from_partial_attr
.attr
,
4984 &alloc_slab_attr
.attr
,
4985 &alloc_refill_attr
.attr
,
4986 &alloc_node_mismatch_attr
.attr
,
4987 &free_slab_attr
.attr
,
4988 &cpuslab_flush_attr
.attr
,
4989 &deactivate_full_attr
.attr
,
4990 &deactivate_empty_attr
.attr
,
4991 &deactivate_to_head_attr
.attr
,
4992 &deactivate_to_tail_attr
.attr
,
4993 &deactivate_remote_frees_attr
.attr
,
4994 &deactivate_bypass_attr
.attr
,
4995 &order_fallback_attr
.attr
,
4996 &cmpxchg_double_fail_attr
.attr
,
4997 &cmpxchg_double_cpu_fail_attr
.attr
,
4998 &cpu_partial_alloc_attr
.attr
,
4999 &cpu_partial_free_attr
.attr
,
5000 &cpu_partial_node_attr
.attr
,
5001 &cpu_partial_drain_attr
.attr
,
5003 #ifdef CONFIG_FAILSLAB
5004 &failslab_attr
.attr
,
5010 static struct attribute_group slab_attr_group
= {
5011 .attrs
= slab_attrs
,
5014 static ssize_t
slab_attr_show(struct kobject
*kobj
,
5015 struct attribute
*attr
,
5018 struct slab_attribute
*attribute
;
5019 struct kmem_cache
*s
;
5022 attribute
= to_slab_attr(attr
);
5025 if (!attribute
->show
)
5028 err
= attribute
->show(s
, buf
);
5033 static ssize_t
slab_attr_store(struct kobject
*kobj
,
5034 struct attribute
*attr
,
5035 const char *buf
, size_t len
)
5037 struct slab_attribute
*attribute
;
5038 struct kmem_cache
*s
;
5041 attribute
= to_slab_attr(attr
);
5044 if (!attribute
->store
)
5047 err
= attribute
->store(s
, buf
, len
);
5048 #ifdef CONFIG_MEMCG_KMEM
5049 if (slab_state
>= FULL
&& err
>= 0 && is_root_cache(s
)) {
5052 mutex_lock(&slab_mutex
);
5053 if (s
->max_attr_size
< len
)
5054 s
->max_attr_size
= len
;
5057 * This is a best effort propagation, so this function's return
5058 * value will be determined by the parent cache only. This is
5059 * basically because not all attributes will have a well
5060 * defined semantics for rollbacks - most of the actions will
5061 * have permanent effects.
5063 * Returning the error value of any of the children that fail
5064 * is not 100 % defined, in the sense that users seeing the
5065 * error code won't be able to know anything about the state of
5068 * Only returning the error code for the parent cache at least
5069 * has well defined semantics. The cache being written to
5070 * directly either failed or succeeded, in which case we loop
5071 * through the descendants with best-effort propagation.
5073 for_each_memcg_cache_index(i
) {
5074 struct kmem_cache
*c
= cache_from_memcg(s
, i
);
5076 attribute
->store(c
, buf
, len
);
5078 mutex_unlock(&slab_mutex
);
5084 static void memcg_propagate_slab_attrs(struct kmem_cache
*s
)
5086 #ifdef CONFIG_MEMCG_KMEM
5088 char *buffer
= NULL
;
5090 if (!is_root_cache(s
))
5094 * This mean this cache had no attribute written. Therefore, no point
5095 * in copying default values around
5097 if (!s
->max_attr_size
)
5100 for (i
= 0; i
< ARRAY_SIZE(slab_attrs
); i
++) {
5103 struct slab_attribute
*attr
= to_slab_attr(slab_attrs
[i
]);
5105 if (!attr
|| !attr
->store
|| !attr
->show
)
5109 * It is really bad that we have to allocate here, so we will
5110 * do it only as a fallback. If we actually allocate, though,
5111 * we can just use the allocated buffer until the end.
5113 * Most of the slub attributes will tend to be very small in
5114 * size, but sysfs allows buffers up to a page, so they can
5115 * theoretically happen.
5119 else if (s
->max_attr_size
< ARRAY_SIZE(mbuf
))
5122 buffer
= (char *) get_zeroed_page(GFP_KERNEL
);
5123 if (WARN_ON(!buffer
))
5128 attr
->show(s
->memcg_params
->root_cache
, buf
);
5129 attr
->store(s
, buf
, strlen(buf
));
5133 free_page((unsigned long)buffer
);
5137 static const struct sysfs_ops slab_sysfs_ops
= {
5138 .show
= slab_attr_show
,
5139 .store
= slab_attr_store
,
5142 static struct kobj_type slab_ktype
= {
5143 .sysfs_ops
= &slab_sysfs_ops
,
5146 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
5148 struct kobj_type
*ktype
= get_ktype(kobj
);
5150 if (ktype
== &slab_ktype
)
5155 static const struct kset_uevent_ops slab_uevent_ops
= {
5156 .filter
= uevent_filter
,
5159 static struct kset
*slab_kset
;
5161 #define ID_STR_LENGTH 64
5163 /* Create a unique string id for a slab cache:
5165 * Format :[flags-]size
5167 static char *create_unique_id(struct kmem_cache
*s
)
5169 char *name
= kmalloc(ID_STR_LENGTH
, GFP_KERNEL
);
5176 * First flags affecting slabcache operations. We will only
5177 * get here for aliasable slabs so we do not need to support
5178 * too many flags. The flags here must cover all flags that
5179 * are matched during merging to guarantee that the id is
5182 if (s
->flags
& SLAB_CACHE_DMA
)
5184 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
5186 if (s
->flags
& SLAB_DEBUG_FREE
)
5188 if (!(s
->flags
& SLAB_NOTRACK
))
5192 p
+= sprintf(p
, "%07d", s
->size
);
5194 #ifdef CONFIG_MEMCG_KMEM
5195 if (!is_root_cache(s
))
5196 p
+= sprintf(p
, "-%08d", memcg_cache_id(s
->memcg_params
->memcg
));
5199 BUG_ON(p
> name
+ ID_STR_LENGTH
- 1);
5203 static int sysfs_slab_add(struct kmem_cache
*s
)
5207 int unmergeable
= slab_unmergeable(s
);
5211 * Slabcache can never be merged so we can use the name proper.
5212 * This is typically the case for debug situations. In that
5213 * case we can catch duplicate names easily.
5215 sysfs_remove_link(&slab_kset
->kobj
, s
->name
);
5219 * Create a unique name for the slab as a target
5222 name
= create_unique_id(s
);
5225 s
->kobj
.kset
= slab_kset
;
5226 err
= kobject_init_and_add(&s
->kobj
, &slab_ktype
, NULL
, name
);
5228 kobject_put(&s
->kobj
);
5232 err
= sysfs_create_group(&s
->kobj
, &slab_attr_group
);
5234 kobject_del(&s
->kobj
);
5235 kobject_put(&s
->kobj
);
5238 kobject_uevent(&s
->kobj
, KOBJ_ADD
);
5240 /* Setup first alias */
5241 sysfs_slab_alias(s
, s
->name
);
5247 static void sysfs_slab_remove(struct kmem_cache
*s
)
5249 if (slab_state
< FULL
)
5251 * Sysfs has not been setup yet so no need to remove the
5256 kobject_uevent(&s
->kobj
, KOBJ_REMOVE
);
5257 kobject_del(&s
->kobj
);
5258 kobject_put(&s
->kobj
);
5262 * Need to buffer aliases during bootup until sysfs becomes
5263 * available lest we lose that information.
5265 struct saved_alias
{
5266 struct kmem_cache
*s
;
5268 struct saved_alias
*next
;
5271 static struct saved_alias
*alias_list
;
5273 static int sysfs_slab_alias(struct kmem_cache
*s
, const char *name
)
5275 struct saved_alias
*al
;
5277 if (slab_state
== FULL
) {
5279 * If we have a leftover link then remove it.
5281 sysfs_remove_link(&slab_kset
->kobj
, name
);
5282 return sysfs_create_link(&slab_kset
->kobj
, &s
->kobj
, name
);
5285 al
= kmalloc(sizeof(struct saved_alias
), GFP_KERNEL
);
5291 al
->next
= alias_list
;
5296 static int __init
slab_sysfs_init(void)
5298 struct kmem_cache
*s
;
5301 mutex_lock(&slab_mutex
);
5303 slab_kset
= kset_create_and_add("slab", &slab_uevent_ops
, kernel_kobj
);
5305 mutex_unlock(&slab_mutex
);
5306 printk(KERN_ERR
"Cannot register slab subsystem.\n");
5312 list_for_each_entry(s
, &slab_caches
, list
) {
5313 err
= sysfs_slab_add(s
);
5315 printk(KERN_ERR
"SLUB: Unable to add boot slab %s"
5316 " to sysfs\n", s
->name
);
5319 while (alias_list
) {
5320 struct saved_alias
*al
= alias_list
;
5322 alias_list
= alias_list
->next
;
5323 err
= sysfs_slab_alias(al
->s
, al
->name
);
5325 printk(KERN_ERR
"SLUB: Unable to add boot slab alias"
5326 " %s to sysfs\n", al
->name
);
5330 mutex_unlock(&slab_mutex
);
5335 __initcall(slab_sysfs_init
);
5336 #endif /* CONFIG_SYSFS */
5339 * The /proc/slabinfo ABI
5341 #ifdef CONFIG_SLABINFO
5342 void get_slabinfo(struct kmem_cache
*s
, struct slabinfo
*sinfo
)
5344 unsigned long nr_partials
= 0;
5345 unsigned long nr_slabs
= 0;
5346 unsigned long nr_objs
= 0;
5347 unsigned long nr_free
= 0;
5350 for_each_online_node(node
) {
5351 struct kmem_cache_node
*n
= get_node(s
, node
);
5356 nr_partials
+= n
->nr_partial
;
5357 nr_slabs
+= atomic_long_read(&n
->nr_slabs
);
5358 nr_objs
+= atomic_long_read(&n
->total_objects
);
5359 nr_free
+= count_partial(n
, count_free
);
5362 sinfo
->active_objs
= nr_objs
- nr_free
;
5363 sinfo
->num_objs
= nr_objs
;
5364 sinfo
->active_slabs
= nr_slabs
;
5365 sinfo
->num_slabs
= nr_slabs
;
5366 sinfo
->objects_per_slab
= oo_objects(s
->oo
);
5367 sinfo
->cache_order
= oo_order(s
->oo
);
5370 void slabinfo_show_stats(struct seq_file
*m
, struct kmem_cache
*s
)
5374 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
5375 size_t count
, loff_t
*ppos
)
5380 #ifdef CONFIG_MTK_MEMCFG
5382 static int mtk_memcfg_add_location(struct loc_track
*t
, struct kmem_cache
*s
,
5383 const struct track
*track
)
5385 long start
, end
, pos
;
5387 unsigned long (*caddrs
)[MTK_MEMCFG_SLABTRACE_CNT
]; /* Called from addresses */
5388 unsigned long taddrs
[MTK_MEMCFG_SLABTRACE_CNT
]
5389 = { [0 ... MTK_MEMCFG_SLABTRACE_CNT
- 1] = 0,}; /* Called from addresses of track */
5390 unsigned long age
= jiffies
- track
->when
;
5396 /* find the index of track->addr */
5397 for (i
= 0; i
< TRACK_ADDRS_COUNT
; i
++) {
5398 if ((track
->addr
== track
->addrs
[i
]) ||
5399 (track
->addr
- 4 == track
->addrs
[i
]))
5402 cnt
= min(MTK_MEMCFG_SLABTRACE_CNT
, TRACK_ADDRS_COUNT
- i
);
5403 memcpy(taddrs
, track
->addrs
+ i
, (cnt
* sizeof (unsigned long)));
5406 pos
= start
+ (end
- start
+ 1) / 2;
5409 * There is nothing at "end". If we end up there
5410 * we need to add something to before end.
5415 caddrs
= &(t
->loc
[pos
].addrs
);
5416 if (!memcmp(caddrs
, taddrs
, MTK_MEMCFG_SLABTRACE_CNT
* sizeof (unsigned long))) {
5422 if (age
< l
->min_time
)
5424 if (age
> l
->max_time
)
5427 if (track
->pid
< l
->min_pid
)
5428 l
->min_pid
= track
->pid
;
5429 if (track
->pid
> l
->max_pid
)
5430 l
->max_pid
= track
->pid
;
5432 cpumask_set_cpu(track
->cpu
,
5433 to_cpumask(l
->cpus
));
5435 node_set(page_to_nid(virt_to_page(track
)), l
->nodes
);
5439 if (memcmp(caddrs
, taddrs
, MTK_MEMCFG_SLABTRACE_CNT
* sizeof (unsigned long)) < 0)
5446 * Not found. Insert new tracking element.
5448 if (t
->count
>= t
->max
&& !alloc_loc_track(t
, 2 * t
->max
, GFP_ATOMIC
))
5454 (t
->count
- pos
) * sizeof(struct location
));
5457 l
->addr
= track
->addr
;
5458 memcpy(l
->addrs
, taddrs
, MTK_MEMCFG_SLABTRACE_CNT
* sizeof (unsigned long));
5462 l
->min_pid
= track
->pid
;
5463 l
->max_pid
= track
->pid
;
5464 cpumask_clear(to_cpumask(l
->cpus
));
5465 cpumask_set_cpu(track
->cpu
, to_cpumask(l
->cpus
));
5466 nodes_clear(l
->nodes
);
5467 node_set(page_to_nid(virt_to_page(track
)), l
->nodes
);
5471 static void mtk_memcfg_process_slab(struct loc_track
*t
, struct kmem_cache
*s
,
5472 struct page
*page
, enum track_item alloc
,
5475 void *addr
= page_address(page
);
5478 bitmap_zero(map
, page
->objects
);
5479 get_map(s
, page
, map
);
5481 for_each_object(p
, s
, addr
, page
->objects
)
5482 if (!test_bit(slab_index(p
, s
, addr
), map
))
5483 mtk_memcfg_add_location(t
, s
, get_track(s
, p
, alloc
));
5486 static int mtk_memcfg_list_locations(struct kmem_cache
*s
, struct seq_file
*m
,
5487 enum track_item alloc
)
5490 struct loc_track t
= { 0, 0, NULL
};
5492 unsigned long *map
= kmalloc(BITS_TO_LONGS(oo_objects(s
->max
)) *
5493 sizeof(unsigned long), GFP_KERNEL
);
5495 if (!map
|| !alloc_loc_track(&t
, PAGE_SIZE
/ sizeof(struct location
),
5498 return seq_printf(m
, "Out of memory\n");
5500 /* Push back cpu slabs */
5503 for_each_node_state(node
, N_NORMAL_MEMORY
) {
5504 struct kmem_cache_node
*n
= get_node(s
, node
);
5505 unsigned long flags
;
5508 if (!atomic_long_read(&n
->nr_slabs
))
5511 spin_lock_irqsave(&n
->list_lock
, flags
);
5512 list_for_each_entry(page
, &n
->partial
, lru
)
5513 mtk_memcfg_process_slab(&t
, s
, page
, alloc
, map
);
5514 list_for_each_entry(page
, &n
->full
, lru
)
5515 mtk_memcfg_process_slab(&t
, s
, page
, alloc
, map
);
5516 spin_unlock_irqrestore(&n
->list_lock
, flags
);
5519 for (i
= 0; i
< t
.count
; i
++) {
5520 struct location
*l
= &t
.loc
[i
];
5522 seq_printf(m
, "%7ld ", l
->count
);
5525 seq_printf(m
, "%pS", (void *)l
->addr
);
5527 seq_printf(m
, "<not-available>");
5529 for (j
= 0; j
< MTK_MEMCFG_SLABTRACE_CNT
; j
++)
5531 seq_printf(m
, " %p", (void *)l
->addrs
[j
]);
5533 seq_printf(m
, "\n");
5540 seq_printf(m
, "No data\n");
5544 static int mtk_memcfg_slabtrace_show(struct seq_file
*m
, void *p
)
5546 struct kmem_cache
*s
;
5547 mutex_lock(&slab_mutex
);
5548 list_for_each_entry(s
, &slab_caches
, list
) {
5549 seq_printf(m
, "========== kmem_cache: %s alloc_calls ==========\n", s
->name
);
5550 if (!(s
->flags
& SLAB_STORE_USER
)) {
5553 mtk_memcfg_list_locations(s
, m
, TRACK_ALLOC
);
5556 mutex_unlock(&slab_mutex
);
5560 int slabtrace_open(struct inode
*inode
, struct file
*file
)
5562 return single_open(file
, mtk_memcfg_slabtrace_show
, NULL
);
5567 #endif /* CONFIG_SLABINFO */