blob: 37555ad8894d3882f1dc07a4fc154b2f18e60597 [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001/*
2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
4 *
Christoph Lameter881db7f2011-06-01 12:25:53 -05005 * The allocator synchronizes using per slab locks or atomic operatios
6 * and only uses a centralized lock to manage a pool of partial slabs.
Christoph Lameter81819f02007-05-06 14:49:36 -07007 *
Christoph Lametercde53532008-07-04 09:59:22 -07008 * (C) 2007 SGI, Christoph Lameter
Christoph Lameter881db7f2011-06-01 12:25:53 -05009 * (C) 2011 Linux Foundation, Christoph Lameter
Christoph Lameter81819f02007-05-06 14:49:36 -070010 */
11
12#include <linux/mm.h>
Nick Piggin1eb5ac62009-05-05 19:13:44 +100013#include <linux/swap.h> /* struct reclaim_state */
Christoph Lameter81819f02007-05-06 14:49:36 -070014#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>
Christoph Lameter97d06602012-07-06 15:25:11 -050019#include "slab.h"
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +040020#include <linux/proc_fs.h>
Andrew Morton3ac38fa2013-04-29 15:08:06 -070021#include <linux/notifier.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070022#include <linux/seq_file.h>
Andrey Ryabinina79316c2015-02-13 14:39:38 -080023#include <linux/kasan.h>
Vegard Nossum5a896d92008-04-04 00:54:48 +020024#include <linux/kmemcheck.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070025#include <linux/cpu.h>
26#include <linux/cpuset.h>
27#include <linux/mempolicy.h>
28#include <linux/ctype.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070029#include <linux/debugobjects.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070030#include <linux/kallsyms.h>
Yasunori Gotob9049e22007-10-21 16:41:37 -070031#include <linux/memory.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -070032#include <linux/math64.h>
Akinobu Mita773ff602008-12-23 19:37:01 +090033#include <linux/fault-inject.h>
Pekka Enbergbfa71452011-07-07 22:47:01 +030034#include <linux/stacktrace.h>
Christoph Lameter4de900b2012-01-30 15:53:51 -060035#include <linux/prefetch.h>
Glauber Costa2633d7a2012-12-18 14:22:34 -080036#include <linux/memcontrol.h>
Christoph Lameter81819f02007-05-06 14:49:36 -070037
Richard Kennedy4a923792010-10-21 10:29:19 +010038#include <trace/events/kmem.h>
39
Mel Gorman072bb0a2012-07-31 16:43:58 -070040#include "internal.h"
41
Christoph Lameter81819f02007-05-06 14:49:36 -070042/*
43 * Lock order:
Christoph Lameter18004c52012-07-06 15:25:12 -050044 * 1. slab_mutex (Global Mutex)
Christoph Lameter881db7f2011-06-01 12:25:53 -050045 * 2. node->list_lock
46 * 3. slab_lock(page) (Only on some arches and for debugging)
Christoph Lameter81819f02007-05-06 14:49:36 -070047 *
Christoph Lameter18004c52012-07-06 15:25:12 -050048 * slab_mutex
Christoph Lameter881db7f2011-06-01 12:25:53 -050049 *
Christoph Lameter18004c52012-07-06 15:25:12 -050050 * The role of the slab_mutex is to protect the list of all the slabs
Christoph Lameter881db7f2011-06-01 12:25:53 -050051 * and to synchronize major metadata changes to slab cache structures.
52 *
53 * The slab_lock is only used for debugging and on arches that do not
54 * have the ability to do a cmpxchg_double. It only protects the second
55 * double word in the page struct. Meaning
56 * A. page->freelist -> List of object free in a page
57 * B. page->counters -> Counters of objects
58 * C. page->frozen -> frozen state
59 *
60 * If a slab is frozen then it is exempt from list management. It is not
61 * on any list. The processor that froze the slab is the one who can
62 * perform list operations on the page. Other processors may put objects
63 * onto the freelist but the processor that froze the slab is the only
64 * one that can retrieve the objects from the page's freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -070065 *
66 * The list_lock protects the partial and full list on each node and
67 * the partial slab counter. If taken then no new slabs may be added or
68 * removed from the lists nor make the number of partial slabs be modified.
69 * (Note that the total number of slabs is an atomic value that may be
70 * modified without taking the list lock).
71 *
72 * The list_lock is a centralized lock and thus we avoid taking it as
73 * much as possible. As long as SLUB does not have to handle partial
74 * slabs, operations can continue without any centralized lock. F.e.
75 * allocating a long series of objects that fill up slabs does not require
76 * the list lock.
Christoph Lameter81819f02007-05-06 14:49:36 -070077 * Interrupts are disabled during allocation and deallocation in order to
78 * make the slab allocator safe to use in the context of an irq. In addition
79 * interrupts are disabled to ensure that the processor does not change
80 * while handling per_cpu slabs, due to kernel preemption.
81 *
82 * SLUB assigns one slab for allocation to each processor.
83 * Allocations only occur from these slabs called cpu slabs.
84 *
Christoph Lameter672bba32007-05-09 02:32:39 -070085 * Slabs with free elements are kept on a partial list and during regular
86 * operations no list for full slabs is used. If an object in a full slab is
Christoph Lameter81819f02007-05-06 14:49:36 -070087 * freed then the slab will show up again on the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -070088 * We track full slabs for debugging purposes though because otherwise we
89 * cannot scan all objects.
Christoph Lameter81819f02007-05-06 14:49:36 -070090 *
91 * Slabs are freed when they become empty. Teardown and setup is
92 * minimal so we rely on the page allocators per cpu caches for
93 * fast frees and allocs.
94 *
95 * Overloading of page flags that are otherwise used for LRU management.
96 *
Christoph Lameter4b6f0752007-05-16 22:10:53 -070097 * PageActive The slab is frozen and exempt from list processing.
98 * This means that the slab is dedicated to a purpose
99 * such as satisfying allocations for a specific
100 * processor. Objects may be freed in the slab while
101 * it is frozen but slab_free will then skip the usual
102 * list operations. It is up to the processor holding
103 * the slab to integrate the slab into the slab lists
104 * when the slab is no longer needed.
105 *
106 * One use of this flag is to mark slabs that are
107 * used for allocations. Then such a slab becomes a cpu
108 * slab. The cpu slab may be equipped with an additional
Christoph Lameterdfb4f092007-10-16 01:26:05 -0700109 * freelist that allows lockless access to
Christoph Lameter894b8782007-05-10 03:15:16 -0700110 * free objects in addition to the regular freelist
111 * that requires the slab lock.
Christoph Lameter81819f02007-05-06 14:49:36 -0700112 *
113 * PageError Slab requires special handling due to debug
114 * options set. This moves slab handling out of
Christoph Lameter894b8782007-05-10 03:15:16 -0700115 * the fast path and disables lockless freelists.
Christoph Lameter81819f02007-05-06 14:49:36 -0700116 */
117
Christoph Lameteraf537b02010-07-09 14:07:14 -0500118static inline int kmem_cache_debug(struct kmem_cache *s)
119{
Christoph Lameter5577bd82007-05-16 22:10:56 -0700120#ifdef CONFIG_SLUB_DEBUG
Christoph Lameteraf537b02010-07-09 14:07:14 -0500121 return unlikely(s->flags & SLAB_DEBUG_FLAGS);
Christoph Lameter5577bd82007-05-16 22:10:56 -0700122#else
Christoph Lameteraf537b02010-07-09 14:07:14 -0500123 return 0;
Christoph Lameter5577bd82007-05-16 22:10:56 -0700124#endif
Christoph Lameteraf537b02010-07-09 14:07:14 -0500125}
Christoph Lameter5577bd82007-05-16 22:10:56 -0700126
Joonsoo Kim345c9052013-06-19 14:05:52 +0900127static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
128{
129#ifdef CONFIG_SLUB_CPU_PARTIAL
130 return !kmem_cache_debug(s);
131#else
132 return false;
133#endif
134}
135
Christoph Lameter81819f02007-05-06 14:49:36 -0700136/*
137 * Issues still to be resolved:
138 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700139 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
140 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700141 * - Variable sizing of the per node arrays
142 */
143
144/* Enable to test recovery from slab corruption on boot */
145#undef SLUB_RESILIENCY_TEST
146
Christoph Lameterb789ef52011-06-01 12:25:49 -0500147/* Enable to log cmpxchg failures */
148#undef SLUB_DEBUG_CMPXCHG
149
Christoph Lameter81819f02007-05-06 14:49:36 -0700150/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700151 * Mininum number of partial slabs. These will be left on the partial
152 * lists even if they are empty. kmem_cache_shrink may reclaim them.
153 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800154#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700155
Christoph Lameter2086d262007-05-06 14:49:46 -0700156/*
157 * Maximum number of desirable partial slabs.
158 * The existence of more partial slabs makes kmem_cache_shrink
Zhi Yong Wu721ae222013-11-08 20:47:37 +0800159 * sort the partial list by the number of objects in use.
Christoph Lameter2086d262007-05-06 14:49:46 -0700160 */
161#define MAX_PARTIAL 10
162
Christoph Lameter81819f02007-05-06 14:49:36 -0700163#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
164 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700165
Christoph Lameter81819f02007-05-06 14:49:36 -0700166/*
David Rientjes3de47212009-07-27 18:30:35 -0700167 * Debugging flags that require metadata to be stored in the slab. These get
168 * disabled when slub_debug=O is used and a cache's min order increases with
169 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700170 */
David Rientjes3de47212009-07-27 18:30:35 -0700171#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700172
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400173#define OO_SHIFT 16
174#define OO_MASK ((1 << OO_SHIFT) - 1)
Christoph Lameter50d5c412011-06-01 12:25:45 -0500175#define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400176
Christoph Lameter81819f02007-05-06 14:49:36 -0700177/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500178#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameterb789ef52011-06-01 12:25:49 -0500179#define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
Christoph Lameter81819f02007-05-06 14:49:36 -0700180
Christoph Lameter81819f02007-05-06 14:49:36 -0700181#ifdef CONFIG_SMP
182static struct notifier_block slab_notifier;
183#endif
184
Christoph Lameter02cbc872007-05-09 02:32:43 -0700185/*
186 * Tracking user of a slab.
187 */
Ben Greeard6543e32011-07-07 11:36:36 -0700188#define TRACK_ADDRS_COUNT 16
Christoph Lameter02cbc872007-05-09 02:32:43 -0700189struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300190 unsigned long addr; /* Called from address */
Ben Greeard6543e32011-07-07 11:36:36 -0700191#ifdef CONFIG_STACKTRACE
192 unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
193#endif
Christoph Lameter02cbc872007-05-09 02:32:43 -0700194 int cpu; /* Was running on cpu */
195 int pid; /* Pid context */
196 unsigned long when; /* When did the operation occur */
197};
198
199enum track_item { TRACK_ALLOC, TRACK_FREE };
200
Christoph Lameterab4d5ed2010-10-05 13:57:26 -0500201#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -0700202static int sysfs_slab_add(struct kmem_cache *);
203static int sysfs_slab_alias(struct kmem_cache *, const char *);
Glauber Costa107dab52012-12-18 14:23:05 -0800204static void memcg_propagate_slab_attrs(struct kmem_cache *s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700205#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700206static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
207static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
208 { return 0; }
Glauber Costa107dab52012-12-18 14:23:05 -0800209static inline void memcg_propagate_slab_attrs(struct kmem_cache *s) { }
Christoph Lameter81819f02007-05-06 14:49:36 -0700210#endif
211
Christoph Lameter4fdccdf2011-03-22 13:35:00 -0500212static inline void stat(const struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800213{
214#ifdef CONFIG_SLUB_STATS
Christoph Lameter88da03a2014-04-07 15:39:42 -0700215 /*
216 * The rmw is racy on a preemptible kernel but this is acceptable, so
217 * avoid this_cpu_add()'s irq-disable overhead.
218 */
219 raw_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800220#endif
221}
222
Christoph Lameter81819f02007-05-06 14:49:36 -0700223/********************************************************************
224 * Core slab cache functions
225 *******************************************************************/
226
Christoph Lameter6446faa2008-02-15 23:45:26 -0800227/* Verify that a pointer has an address that is valid within a slab page */
Christoph Lameter02cbc872007-05-09 02:32:43 -0700228static inline int check_valid_pointer(struct kmem_cache *s,
229 struct page *page, const void *object)
230{
231 void *base;
232
Christoph Lametera973e9d2008-03-01 13:40:44 -0800233 if (!object)
Christoph Lameter02cbc872007-05-09 02:32:43 -0700234 return 1;
235
Christoph Lametera973e9d2008-03-01 13:40:44 -0800236 base = page_address(page);
Christoph Lameter39b26462008-04-14 19:11:30 +0300237 if (object < base || object >= base + page->objects * s->size ||
Christoph Lameter02cbc872007-05-09 02:32:43 -0700238 (object - base) % s->size) {
239 return 0;
240 }
241
242 return 1;
243}
244
Christoph Lameter7656c722007-05-09 02:32:40 -0700245static inline void *get_freepointer(struct kmem_cache *s, void *object)
246{
247 return *(void **)(object + s->offset);
248}
249
Eric Dumazet0ad95002011-12-16 16:25:34 +0100250static void prefetch_freepointer(const struct kmem_cache *s, void *object)
251{
252 prefetch(object + s->offset);
253}
254
Christoph Lameter1393d9a2011-05-16 15:26:08 -0500255static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
256{
257 void *p;
258
259#ifdef CONFIG_DEBUG_PAGEALLOC
260 probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
261#else
262 p = get_freepointer(s, object);
263#endif
264 return p;
265}
266
Christoph Lameter7656c722007-05-09 02:32:40 -0700267static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
268{
269 *(void **)(object + s->offset) = fp;
270}
271
272/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300273#define for_each_object(__p, __s, __addr, __objects) \
274 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
Christoph Lameter7656c722007-05-09 02:32:40 -0700275 __p += (__s)->size)
276
Wei Yang54266642014-08-06 16:04:42 -0700277#define for_each_object_idx(__p, __idx, __s, __addr, __objects) \
278 for (__p = (__addr), __idx = 1; __idx <= __objects;\
279 __p += (__s)->size, __idx++)
280
Christoph Lameter7656c722007-05-09 02:32:40 -0700281/* Determine object index from a given position */
282static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
283{
284 return (p - addr) / s->size;
285}
286
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100287static inline size_t slab_ksize(const struct kmem_cache *s)
288{
289#ifdef CONFIG_SLUB_DEBUG
290 /*
291 * Debugging requires use of the padding between object
292 * and whatever may come after it.
293 */
294 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500295 return s->object_size;
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100296
297#endif
298 /*
299 * If we have the need to store the freelist pointer
300 * back there or track user information then we can
301 * only use the space before that information.
302 */
303 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
304 return s->inuse;
305 /*
306 * Else we can use all the padding etc for the allocation
307 */
308 return s->size;
309}
310
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800311static inline int order_objects(int order, unsigned long size, int reserved)
312{
313 return ((PAGE_SIZE << order) - reserved) / size;
314}
315
Christoph Lameter834f3d12008-04-14 19:11:31 +0300316static inline struct kmem_cache_order_objects oo_make(int order,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800317 unsigned long size, int reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300318{
319 struct kmem_cache_order_objects x = {
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800320 (order << OO_SHIFT) + order_objects(order, size, reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300321 };
322
323 return x;
324}
325
326static inline int oo_order(struct kmem_cache_order_objects x)
327{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400328 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300329}
330
331static inline int oo_objects(struct kmem_cache_order_objects x)
332{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400333 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300334}
335
Christoph Lameter881db7f2011-06-01 12:25:53 -0500336/*
337 * Per slab locking using the pagelock
338 */
339static __always_inline void slab_lock(struct page *page)
340{
341 bit_spin_lock(PG_locked, &page->flags);
342}
343
344static __always_inline void slab_unlock(struct page *page)
345{
346 __bit_spin_unlock(PG_locked, &page->flags);
347}
348
Dave Hansena0320862014-01-30 15:46:09 -0800349static inline void set_page_slub_counters(struct page *page, unsigned long counters_new)
350{
351 struct page tmp;
352 tmp.counters = counters_new;
353 /*
354 * page->counters can cover frozen/inuse/objects as well
355 * as page->_count. If we assign to ->counters directly
356 * we run the risk of losing updates to page->_count, so
357 * be careful and only assign to the fields we need.
358 */
359 page->frozen = tmp.frozen;
360 page->inuse = tmp.inuse;
361 page->objects = tmp.objects;
362}
363
Christoph Lameter1d071712011-07-14 12:49:12 -0500364/* Interrupts must be disabled (for the fallback code to work right) */
365static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
Christoph Lameterb789ef52011-06-01 12:25:49 -0500366 void *freelist_old, unsigned long counters_old,
367 void *freelist_new, unsigned long counters_new,
368 const char *n)
369{
Christoph Lameter1d071712011-07-14 12:49:12 -0500370 VM_BUG_ON(!irqs_disabled());
Heiko Carstens25654092012-01-12 17:17:33 -0800371#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
372 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -0500373 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000374 if (cmpxchg_double(&page->freelist, &page->counters,
Dan Carpenter0aa9a132014-08-06 16:04:48 -0700375 freelist_old, counters_old,
376 freelist_new, counters_new))
377 return 1;
Christoph Lameterb789ef52011-06-01 12:25:49 -0500378 } else
379#endif
380 {
Christoph Lameter881db7f2011-06-01 12:25:53 -0500381 slab_lock(page);
Chen Gangd0e0ac92013-07-15 09:05:29 +0800382 if (page->freelist == freelist_old &&
383 page->counters == counters_old) {
Christoph Lameterb789ef52011-06-01 12:25:49 -0500384 page->freelist = freelist_new;
Dave Hansena0320862014-01-30 15:46:09 -0800385 set_page_slub_counters(page, counters_new);
Christoph Lameter881db7f2011-06-01 12:25:53 -0500386 slab_unlock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500387 return 1;
388 }
Christoph Lameter881db7f2011-06-01 12:25:53 -0500389 slab_unlock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500390 }
391
392 cpu_relax();
393 stat(s, CMPXCHG_DOUBLE_FAIL);
394
395#ifdef SLUB_DEBUG_CMPXCHG
Fabian Frederickf9f58282014-06-04 16:06:34 -0700396 pr_info("%s %s: cmpxchg double redo ", n, s->name);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500397#endif
398
399 return 0;
400}
401
Christoph Lameter1d071712011-07-14 12:49:12 -0500402static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
403 void *freelist_old, unsigned long counters_old,
404 void *freelist_new, unsigned long counters_new,
405 const char *n)
406{
Heiko Carstens25654092012-01-12 17:17:33 -0800407#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
408 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameter1d071712011-07-14 12:49:12 -0500409 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000410 if (cmpxchg_double(&page->freelist, &page->counters,
Dan Carpenter0aa9a132014-08-06 16:04:48 -0700411 freelist_old, counters_old,
412 freelist_new, counters_new))
413 return 1;
Christoph Lameter1d071712011-07-14 12:49:12 -0500414 } else
415#endif
416 {
417 unsigned long flags;
418
419 local_irq_save(flags);
420 slab_lock(page);
Chen Gangd0e0ac92013-07-15 09:05:29 +0800421 if (page->freelist == freelist_old &&
422 page->counters == counters_old) {
Christoph Lameter1d071712011-07-14 12:49:12 -0500423 page->freelist = freelist_new;
Dave Hansena0320862014-01-30 15:46:09 -0800424 set_page_slub_counters(page, counters_new);
Christoph Lameter1d071712011-07-14 12:49:12 -0500425 slab_unlock(page);
426 local_irq_restore(flags);
427 return 1;
428 }
429 slab_unlock(page);
430 local_irq_restore(flags);
431 }
432
433 cpu_relax();
434 stat(s, CMPXCHG_DOUBLE_FAIL);
435
436#ifdef SLUB_DEBUG_CMPXCHG
Fabian Frederickf9f58282014-06-04 16:06:34 -0700437 pr_info("%s %s: cmpxchg double redo ", n, s->name);
Christoph Lameter1d071712011-07-14 12:49:12 -0500438#endif
439
440 return 0;
441}
442
Christoph Lameter41ecc552007-05-09 02:32:44 -0700443#ifdef CONFIG_SLUB_DEBUG
444/*
Christoph Lameter5f80b132011-04-15 14:48:13 -0500445 * Determine a map of object in use on a page.
446 *
Christoph Lameter881db7f2011-06-01 12:25:53 -0500447 * Node listlock must be held to guarantee that the page does
Christoph Lameter5f80b132011-04-15 14:48:13 -0500448 * not vanish from under us.
449 */
450static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
451{
452 void *p;
453 void *addr = page_address(page);
454
455 for (p = page->freelist; p; p = get_freepointer(s, p))
456 set_bit(slab_index(p, s, addr), map);
457}
458
Christoph Lameter41ecc552007-05-09 02:32:44 -0700459/*
460 * Debug settings:
461 */
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700462#ifdef CONFIG_SLUB_DEBUG_ON
463static int slub_debug = DEBUG_DEFAULT_FLAGS;
464#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700465static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700466#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700467
468static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700469static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700470
Christoph Lameter7656c722007-05-09 02:32:40 -0700471/*
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800472 * slub is about to manipulate internal object metadata. This memory lies
473 * outside the range of the allocated object, so accessing it would normally
474 * be reported by kasan as a bounds error. metadata_access_enable() is used
475 * to tell kasan that these accesses are OK.
476 */
477static inline void metadata_access_enable(void)
478{
479 kasan_disable_current();
480}
481
482static inline void metadata_access_disable(void)
483{
484 kasan_enable_current();
485}
486
487/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700488 * Object debugging
489 */
490static void print_section(char *text, u8 *addr, unsigned int length)
491{
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800492 metadata_access_enable();
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200493 print_hex_dump(KERN_ERR, text, DUMP_PREFIX_ADDRESS, 16, 1, addr,
494 length, 1);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800495 metadata_access_disable();
Christoph Lameter81819f02007-05-06 14:49:36 -0700496}
497
Christoph Lameter81819f02007-05-06 14:49:36 -0700498static struct track *get_track(struct kmem_cache *s, void *object,
499 enum track_item alloc)
500{
501 struct track *p;
502
503 if (s->offset)
504 p = object + s->offset + sizeof(void *);
505 else
506 p = object + s->inuse;
507
508 return p + alloc;
509}
510
511static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300512 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700513{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900514 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700515
Christoph Lameter81819f02007-05-06 14:49:36 -0700516 if (addr) {
Ben Greeard6543e32011-07-07 11:36:36 -0700517#ifdef CONFIG_STACKTRACE
518 struct stack_trace trace;
519 int i;
520
521 trace.nr_entries = 0;
522 trace.max_entries = TRACK_ADDRS_COUNT;
523 trace.entries = p->addrs;
524 trace.skip = 3;
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800525 metadata_access_enable();
Ben Greeard6543e32011-07-07 11:36:36 -0700526 save_stack_trace(&trace);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800527 metadata_access_disable();
Ben Greeard6543e32011-07-07 11:36:36 -0700528
529 /* See rant in lockdep.c */
530 if (trace.nr_entries != 0 &&
531 trace.entries[trace.nr_entries - 1] == ULONG_MAX)
532 trace.nr_entries--;
533
534 for (i = trace.nr_entries; i < TRACK_ADDRS_COUNT; i++)
535 p->addrs[i] = 0;
536#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700537 p->addr = addr;
538 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400539 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700540 p->when = jiffies;
541 } else
542 memset(p, 0, sizeof(struct track));
543}
544
Christoph Lameter81819f02007-05-06 14:49:36 -0700545static void init_tracking(struct kmem_cache *s, void *object)
546{
Christoph Lameter24922682007-07-17 04:03:18 -0700547 if (!(s->flags & SLAB_STORE_USER))
548 return;
549
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300550 set_track(s, object, TRACK_FREE, 0UL);
551 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700552}
553
554static void print_track(const char *s, struct track *t)
555{
556 if (!t->addr)
557 return;
558
Fabian Frederickf9f58282014-06-04 16:06:34 -0700559 pr_err("INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
560 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Ben Greeard6543e32011-07-07 11:36:36 -0700561#ifdef CONFIG_STACKTRACE
562 {
563 int i;
564 for (i = 0; i < TRACK_ADDRS_COUNT; i++)
565 if (t->addrs[i])
Fabian Frederickf9f58282014-06-04 16:06:34 -0700566 pr_err("\t%pS\n", (void *)t->addrs[i]);
Ben Greeard6543e32011-07-07 11:36:36 -0700567 else
568 break;
569 }
570#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700571}
572
Christoph Lameter24922682007-07-17 04:03:18 -0700573static void print_tracking(struct kmem_cache *s, void *object)
574{
575 if (!(s->flags & SLAB_STORE_USER))
576 return;
577
578 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
579 print_track("Freed", get_track(s, object, TRACK_FREE));
580}
581
582static void print_page_info(struct page *page)
583{
Fabian Frederickf9f58282014-06-04 16:06:34 -0700584 pr_err("INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
Chen Gangd0e0ac92013-07-15 09:05:29 +0800585 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700586
587}
588
589static void slab_bug(struct kmem_cache *s, char *fmt, ...)
590{
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700591 struct va_format vaf;
Christoph Lameter24922682007-07-17 04:03:18 -0700592 va_list args;
Christoph Lameter24922682007-07-17 04:03:18 -0700593
594 va_start(args, fmt);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700595 vaf.fmt = fmt;
596 vaf.va = &args;
Fabian Frederickf9f58282014-06-04 16:06:34 -0700597 pr_err("=============================================================================\n");
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700598 pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf);
Fabian Frederickf9f58282014-06-04 16:06:34 -0700599 pr_err("-----------------------------------------------------------------------------\n\n");
Dave Jones645df232012-09-18 15:54:12 -0400600
Rusty Russell373d4d02013-01-21 17:17:39 +1030601 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700602 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700603}
604
605static void slab_fix(struct kmem_cache *s, char *fmt, ...)
606{
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700607 struct va_format vaf;
Christoph Lameter24922682007-07-17 04:03:18 -0700608 va_list args;
Christoph Lameter24922682007-07-17 04:03:18 -0700609
610 va_start(args, fmt);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700611 vaf.fmt = fmt;
612 vaf.va = &args;
613 pr_err("FIX %s: %pV\n", s->name, &vaf);
Christoph Lameter24922682007-07-17 04:03:18 -0700614 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700615}
616
617static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700618{
619 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800620 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700621
622 print_tracking(s, p);
623
624 print_page_info(page);
625
Fabian Frederickf9f58282014-06-04 16:06:34 -0700626 pr_err("INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
627 p, p - addr, get_freepointer(s, p));
Christoph Lameter24922682007-07-17 04:03:18 -0700628
629 if (p > addr + 16)
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200630 print_section("Bytes b4 ", p - 16, 16);
Christoph Lameter24922682007-07-17 04:03:18 -0700631
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500632 print_section("Object ", p, min_t(unsigned long, s->object_size,
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200633 PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700634 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500635 print_section("Redzone ", p + s->object_size,
636 s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700637
Christoph Lameter81819f02007-05-06 14:49:36 -0700638 if (s->offset)
639 off = s->offset + sizeof(void *);
640 else
641 off = s->inuse;
642
Christoph Lameter24922682007-07-17 04:03:18 -0700643 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700644 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700645
646 if (off != s->size)
647 /* Beginning of the filler is the free pointer */
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200648 print_section("Padding ", p + off, s->size - off);
Christoph Lameter24922682007-07-17 04:03:18 -0700649
650 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700651}
652
Andrey Ryabinin75c66de2015-02-13 14:39:35 -0800653void object_err(struct kmem_cache *s, struct page *page,
Christoph Lameter81819f02007-05-06 14:49:36 -0700654 u8 *object, char *reason)
655{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700656 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700657 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700658}
659
Chen Gangd0e0ac92013-07-15 09:05:29 +0800660static void slab_err(struct kmem_cache *s, struct page *page,
661 const char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700662{
663 va_list args;
664 char buf[100];
665
Christoph Lameter24922682007-07-17 04:03:18 -0700666 va_start(args, fmt);
667 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700668 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700669 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700670 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700671 dump_stack();
672}
673
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500674static void init_object(struct kmem_cache *s, void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700675{
676 u8 *p = object;
677
678 if (s->flags & __OBJECT_POISON) {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500679 memset(p, POISON_FREE, s->object_size - 1);
680 p[s->object_size - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700681 }
682
683 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500684 memset(p + s->object_size, val, s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700685}
686
Christoph Lameter24922682007-07-17 04:03:18 -0700687static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
688 void *from, void *to)
689{
690 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
691 memset(from, data, to - from);
692}
693
694static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
695 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800696 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700697{
698 u8 *fault;
699 u8 *end;
700
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800701 metadata_access_enable();
Akinobu Mita798248202011-10-31 17:08:07 -0700702 fault = memchr_inv(start, value, bytes);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800703 metadata_access_disable();
Christoph Lameter24922682007-07-17 04:03:18 -0700704 if (!fault)
705 return 1;
706
707 end = start + bytes;
708 while (end > fault && end[-1] == value)
709 end--;
710
711 slab_bug(s, "%s overwritten", what);
Fabian Frederickf9f58282014-06-04 16:06:34 -0700712 pr_err("INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
Christoph Lameter24922682007-07-17 04:03:18 -0700713 fault, end - 1, fault[0], value);
714 print_trailer(s, page, object);
715
716 restore_bytes(s, what, value, fault, end);
717 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700718}
719
Christoph Lameter81819f02007-05-06 14:49:36 -0700720/*
721 * Object layout:
722 *
723 * object address
724 * Bytes of the object to be managed.
725 * If the freepointer may overlay the object then the free
726 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700727 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700728 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
729 * 0xa5 (POISON_END)
730 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500731 * object + s->object_size
Christoph Lameter81819f02007-05-06 14:49:36 -0700732 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700733 * Padding is extended by another word if Redzoning is enabled and
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500734 * object_size == inuse.
Christoph Lameter672bba32007-05-09 02:32:39 -0700735 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700736 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
737 * 0xcc (RED_ACTIVE) for objects in use.
738 *
739 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700740 * Meta data starts here.
741 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700742 * A. Free pointer (if we cannot overwrite object on free)
743 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700744 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800745 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700746 * before the word boundary.
747 *
748 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700749 *
750 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700751 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700752 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500753 * If slabcaches are merged then the object_size and inuse boundaries are mostly
Christoph Lameter672bba32007-05-09 02:32:39 -0700754 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700755 * may be used with merged slabcaches.
756 */
757
Christoph Lameter81819f02007-05-06 14:49:36 -0700758static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
759{
760 unsigned long off = s->inuse; /* The end of info */
761
762 if (s->offset)
763 /* Freepointer is placed after the object. */
764 off += sizeof(void *);
765
766 if (s->flags & SLAB_STORE_USER)
767 /* We also have user information there */
768 off += 2 * sizeof(struct track);
769
770 if (s->size == off)
771 return 1;
772
Christoph Lameter24922682007-07-17 04:03:18 -0700773 return check_bytes_and_report(s, page, p, "Object padding",
774 p + off, POISON_INUSE, s->size - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700775}
776
Christoph Lameter39b26462008-04-14 19:11:30 +0300777/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700778static int slab_pad_check(struct kmem_cache *s, struct page *page)
779{
Christoph Lameter24922682007-07-17 04:03:18 -0700780 u8 *start;
781 u8 *fault;
782 u8 *end;
783 int length;
784 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700785
786 if (!(s->flags & SLAB_POISON))
787 return 1;
788
Christoph Lametera973e9d2008-03-01 13:40:44 -0800789 start = page_address(page);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800790 length = (PAGE_SIZE << compound_order(page)) - s->reserved;
Christoph Lameter39b26462008-04-14 19:11:30 +0300791 end = start + length;
792 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700793 if (!remainder)
794 return 1;
795
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800796 metadata_access_enable();
Akinobu Mita798248202011-10-31 17:08:07 -0700797 fault = memchr_inv(end - remainder, POISON_INUSE, remainder);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800798 metadata_access_disable();
Christoph Lameter24922682007-07-17 04:03:18 -0700799 if (!fault)
800 return 1;
801 while (end > fault && end[-1] == POISON_INUSE)
802 end--;
803
804 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200805 print_section("Padding ", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700806
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200807 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700808 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700809}
810
811static int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500812 void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700813{
814 u8 *p = object;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500815 u8 *endobject = object + s->object_size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700816
817 if (s->flags & SLAB_RED_ZONE) {
Christoph Lameter24922682007-07-17 04:03:18 -0700818 if (!check_bytes_and_report(s, page, object, "Redzone",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500819 endobject, val, s->inuse - s->object_size))
Christoph Lameter81819f02007-05-06 14:49:36 -0700820 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700821 } else {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500822 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800823 check_bytes_and_report(s, page, p, "Alignment padding",
Chen Gangd0e0ac92013-07-15 09:05:29 +0800824 endobject, POISON_INUSE,
825 s->inuse - s->object_size);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800826 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700827 }
828
829 if (s->flags & SLAB_POISON) {
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500830 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700831 (!check_bytes_and_report(s, page, p, "Poison", p,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500832 POISON_FREE, s->object_size - 1) ||
Christoph Lameter24922682007-07-17 04:03:18 -0700833 !check_bytes_and_report(s, page, p, "Poison",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500834 p + s->object_size - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700835 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700836 /*
837 * check_pad_bytes cleans up on its own.
838 */
839 check_pad_bytes(s, page, p);
840 }
841
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500842 if (!s->offset && val == SLUB_RED_ACTIVE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700843 /*
844 * Object and freepointer overlap. Cannot check
845 * freepointer while object is allocated.
846 */
847 return 1;
848
849 /* Check free pointer validity */
850 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
851 object_err(s, page, p, "Freepointer corrupt");
852 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100853 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700854 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700855 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700856 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800857 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700858 return 0;
859 }
860 return 1;
861}
862
863static int check_slab(struct kmem_cache *s, struct page *page)
864{
Christoph Lameter39b26462008-04-14 19:11:30 +0300865 int maxobj;
866
Christoph Lameter81819f02007-05-06 14:49:36 -0700867 VM_BUG_ON(!irqs_disabled());
868
869 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700870 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700871 return 0;
872 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300873
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800874 maxobj = order_objects(compound_order(page), s->size, s->reserved);
Christoph Lameter39b26462008-04-14 19:11:30 +0300875 if (page->objects > maxobj) {
876 slab_err(s, page, "objects %u > max %u",
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800877 page->objects, maxobj);
Christoph Lameter39b26462008-04-14 19:11:30 +0300878 return 0;
879 }
880 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700881 slab_err(s, page, "inuse %u > max %u",
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800882 page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700883 return 0;
884 }
885 /* Slab_pad_check fixes things up after itself */
886 slab_pad_check(s, page);
887 return 1;
888}
889
890/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700891 * Determine if a certain object on a page is on the freelist. Must hold the
892 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700893 */
894static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
895{
896 int nr = 0;
Christoph Lameter881db7f2011-06-01 12:25:53 -0500897 void *fp;
Christoph Lameter81819f02007-05-06 14:49:36 -0700898 void *object = NULL;
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800899 int max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700900
Christoph Lameter881db7f2011-06-01 12:25:53 -0500901 fp = page->freelist;
Christoph Lameter39b26462008-04-14 19:11:30 +0300902 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700903 if (fp == search)
904 return 1;
905 if (!check_valid_pointer(s, page, fp)) {
906 if (object) {
907 object_err(s, page, object,
908 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800909 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700910 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700911 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800912 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300913 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700914 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700915 return 0;
916 }
917 break;
918 }
919 object = fp;
920 fp = get_freepointer(s, object);
921 nr++;
922 }
923
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800924 max_objects = order_objects(compound_order(page), s->size, s->reserved);
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400925 if (max_objects > MAX_OBJS_PER_PAGE)
926 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300927
928 if (page->objects != max_objects) {
929 slab_err(s, page, "Wrong number of objects. Found %d but "
930 "should be %d", page->objects, max_objects);
931 page->objects = max_objects;
932 slab_fix(s, "Number of objects adjusted.");
933 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300934 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700935 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300936 "counted were %d", page->inuse, page->objects - nr);
937 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700938 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700939 }
940 return search == NULL;
941}
942
Christoph Lameter0121c6192008-04-29 16:11:12 -0700943static void trace(struct kmem_cache *s, struct page *page, void *object,
944 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700945{
946 if (s->flags & SLAB_TRACE) {
Fabian Frederickf9f58282014-06-04 16:06:34 -0700947 pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
Christoph Lameter3ec09742007-05-16 22:11:00 -0700948 s->name,
949 alloc ? "alloc" : "free",
950 object, page->inuse,
951 page->freelist);
952
953 if (!alloc)
Chen Gangd0e0ac92013-07-15 09:05:29 +0800954 print_section("Object ", (void *)object,
955 s->object_size);
Christoph Lameter3ec09742007-05-16 22:11:00 -0700956
957 dump_stack();
958 }
959}
960
Christoph Lameter643b1132007-05-06 14:49:42 -0700961/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700962 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -0700963 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500964static void add_full(struct kmem_cache *s,
965 struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -0700966{
Christoph Lameter643b1132007-05-06 14:49:42 -0700967 if (!(s->flags & SLAB_STORE_USER))
968 return;
969
David Rientjes255d0882014-02-10 14:25:39 -0800970 lockdep_assert_held(&n->list_lock);
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500971 list_add(&page->lru, &n->full);
972}
Christoph Lameter643b1132007-05-06 14:49:42 -0700973
Peter Zijlstrac65c1872014-01-10 13:23:49 +0100974static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct page *page)
Christoph Lameter5cc6eee2011-06-01 12:25:50 -0500975{
976 if (!(s->flags & SLAB_STORE_USER))
977 return;
978
David Rientjes255d0882014-02-10 14:25:39 -0800979 lockdep_assert_held(&n->list_lock);
Christoph Lameter643b1132007-05-06 14:49:42 -0700980 list_del(&page->lru);
Christoph Lameter643b1132007-05-06 14:49:42 -0700981}
982
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300983/* Tracking of the number of slabs for debugging purposes */
984static inline unsigned long slabs_node(struct kmem_cache *s, int node)
985{
986 struct kmem_cache_node *n = get_node(s, node);
987
988 return atomic_long_read(&n->nr_slabs);
989}
990
Alexander Beregalov26c02cf2009-06-11 14:08:48 +0400991static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
992{
993 return atomic_long_read(&n->nr_slabs);
994}
995
Christoph Lameter205ab992008-04-14 19:11:40 +0300996static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +0300997{
998 struct kmem_cache_node *n = get_node(s, node);
999
1000 /*
1001 * May be called early in order to allocate a slab for the
1002 * kmem_cache_node structure. Solve the chicken-egg
1003 * dilemma by deferring the increment of the count during
1004 * bootstrap (see early_kmem_cache_node_alloc).
1005 */
Joonsoo Kim338b2642013-01-21 17:01:27 +09001006 if (likely(n)) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001007 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001008 atomic_long_add(objects, &n->total_objects);
1009 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001010}
Christoph Lameter205ab992008-04-14 19:11:40 +03001011static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001012{
1013 struct kmem_cache_node *n = get_node(s, node);
1014
1015 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001016 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001017}
1018
1019/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -07001020static void setup_object_debug(struct kmem_cache *s, struct page *page,
1021 void *object)
1022{
1023 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
1024 return;
1025
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001026 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter3ec09742007-05-16 22:11:00 -07001027 init_tracking(s, object);
1028}
1029
Chen Gangd0e0ac92013-07-15 09:05:29 +08001030static noinline int alloc_debug_processing(struct kmem_cache *s,
1031 struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001032 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001033{
1034 if (!check_slab(s, page))
1035 goto bad;
1036
Christoph Lameter81819f02007-05-06 14:49:36 -07001037 if (!check_valid_pointer(s, page, object)) {
1038 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -07001039 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001040 }
1041
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001042 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -07001043 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001044
Christoph Lameter3ec09742007-05-16 22:11:00 -07001045 /* Success perform special debug activities for allocs */
1046 if (s->flags & SLAB_STORE_USER)
1047 set_track(s, object, TRACK_ALLOC, addr);
1048 trace(s, page, object, 1);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001049 init_object(s, object, SLUB_RED_ACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001050 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001051
Christoph Lameter81819f02007-05-06 14:49:36 -07001052bad:
1053 if (PageSlab(page)) {
1054 /*
1055 * If this is a slab page then lets do the best we can
1056 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -07001057 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001058 */
Christoph Lameter24922682007-07-17 04:03:18 -07001059 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +03001060 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001061 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001062 }
1063 return 0;
1064}
1065
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001066static noinline struct kmem_cache_node *free_debug_processing(
1067 struct kmem_cache *s, struct page *page, void *object,
1068 unsigned long addr, unsigned long *flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001069{
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001070 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001071
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001072 spin_lock_irqsave(&n->list_lock, *flags);
Christoph Lameter881db7f2011-06-01 12:25:53 -05001073 slab_lock(page);
1074
Christoph Lameter81819f02007-05-06 14:49:36 -07001075 if (!check_slab(s, page))
1076 goto fail;
1077
1078 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001079 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001080 goto fail;
1081 }
1082
1083 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -07001084 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -07001085 goto fail;
1086 }
1087
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001088 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001089 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07001090
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001091 if (unlikely(s != page->slab_cache)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001092 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001093 slab_err(s, page, "Attempt to free object(0x%p) "
1094 "outside of slab", object);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001095 } else if (!page->slab_cache) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07001096 pr_err("SLUB <none>: no slab for object 0x%p.\n",
1097 object);
Christoph Lameter70d71222007-05-06 14:49:47 -07001098 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -08001099 } else
Christoph Lameter24922682007-07-17 04:03:18 -07001100 object_err(s, page, object,
1101 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -07001102 goto fail;
1103 }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001104
Christoph Lameter3ec09742007-05-16 22:11:00 -07001105 if (s->flags & SLAB_STORE_USER)
1106 set_track(s, object, TRACK_FREE, addr);
1107 trace(s, page, object, 0);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001108 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001109out:
Christoph Lameter881db7f2011-06-01 12:25:53 -05001110 slab_unlock(page);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001111 /*
1112 * Keep node_lock to preserve integrity
1113 * until the object is actually freed
1114 */
1115 return n;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001116
Christoph Lameter81819f02007-05-06 14:49:36 -07001117fail:
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001118 slab_unlock(page);
1119 spin_unlock_irqrestore(&n->list_lock, *flags);
Christoph Lameter24922682007-07-17 04:03:18 -07001120 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001121 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001122}
1123
Christoph Lameter41ecc552007-05-09 02:32:44 -07001124static int __init setup_slub_debug(char *str)
1125{
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001126 slub_debug = DEBUG_DEFAULT_FLAGS;
1127 if (*str++ != '=' || !*str)
1128 /*
1129 * No options specified. Switch on full debugging.
1130 */
1131 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001132
1133 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001134 /*
1135 * No options but restriction on slabs. This means full
1136 * debugging for slabs matching a pattern.
1137 */
1138 goto check_slabs;
1139
David Rientjesfa5ec8a2009-07-07 00:14:14 -07001140 if (tolower(*str) == 'o') {
1141 /*
1142 * Avoid enabling debugging on caches if its minimum order
1143 * would increase as a result.
1144 */
1145 disable_higher_order_debug = 1;
1146 goto out;
1147 }
1148
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001149 slub_debug = 0;
1150 if (*str == '-')
1151 /*
1152 * Switch off all debugging measures.
1153 */
1154 goto out;
1155
1156 /*
1157 * Determine which debug features should be switched on
1158 */
Pekka Enberg06428782008-01-07 23:20:27 -08001159 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001160 switch (tolower(*str)) {
1161 case 'f':
1162 slub_debug |= SLAB_DEBUG_FREE;
1163 break;
1164 case 'z':
1165 slub_debug |= SLAB_RED_ZONE;
1166 break;
1167 case 'p':
1168 slub_debug |= SLAB_POISON;
1169 break;
1170 case 'u':
1171 slub_debug |= SLAB_STORE_USER;
1172 break;
1173 case 't':
1174 slub_debug |= SLAB_TRACE;
1175 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001176 case 'a':
1177 slub_debug |= SLAB_FAILSLAB;
1178 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001179 default:
Fabian Frederickf9f58282014-06-04 16:06:34 -07001180 pr_err("slub_debug option '%c' unknown. skipped\n",
1181 *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001182 }
1183 }
1184
1185check_slabs:
1186 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001187 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001188out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001189 return 1;
1190}
1191
1192__setup("slub_debug", setup_slub_debug);
1193
Joonsoo Kim423c9292014-10-09 15:26:22 -07001194unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001195 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001196 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001197{
1198 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001199 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001200 */
Christoph Lameterc6f58d92013-11-07 16:29:15 +00001201 if (slub_debug && (!slub_debug_slabs || (name &&
1202 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)))))
David Rientjes3de47212009-07-27 18:30:35 -07001203 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001204
1205 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001206}
1207#else
Christoph Lameter3ec09742007-05-16 22:11:00 -07001208static inline void setup_object_debug(struct kmem_cache *s,
1209 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001210
Christoph Lameter3ec09742007-05-16 22:11:00 -07001211static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001212 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001213
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001214static inline struct kmem_cache_node *free_debug_processing(
1215 struct kmem_cache *s, struct page *page, void *object,
1216 unsigned long addr, unsigned long *flags) { return NULL; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001217
Christoph Lameter41ecc552007-05-09 02:32:44 -07001218static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1219 { return 1; }
1220static inline int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001221 void *object, u8 val) { return 1; }
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001222static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
1223 struct page *page) {}
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001224static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
1225 struct page *page) {}
Joonsoo Kim423c9292014-10-09 15:26:22 -07001226unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001227 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001228 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001229{
1230 return flags;
1231}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001232#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001233
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001234#define disable_higher_order_debug 0
1235
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001236static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1237 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001238static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1239 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001240static inline void inc_slabs_node(struct kmem_cache *s, int node,
1241 int objects) {}
1242static inline void dec_slabs_node(struct kmem_cache *s, int node,
1243 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001244
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001245#endif /* CONFIG_SLUB_DEBUG */
1246
1247/*
1248 * Hooks for other subsystems that check memory allocations. In a typical
1249 * production configuration these hooks all should produce no code at all.
1250 */
Roman Bobnievd56791b2013-10-08 15:58:57 -07001251static inline void kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
1252{
1253 kmemleak_alloc(ptr, size, 1, flags);
1254}
1255
1256static inline void kfree_hook(const void *x)
1257{
1258 kmemleak_free(x);
1259}
1260
Vladimir Davydov8135be52014-12-12 16:56:38 -08001261static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
1262 gfp_t flags)
Roman Bobnievd56791b2013-10-08 15:58:57 -07001263{
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001264 flags &= gfp_allowed_mask;
1265 lockdep_trace_alloc(flags);
1266 might_sleep_if(flags & __GFP_WAIT);
1267
Vladimir Davydov8135be52014-12-12 16:56:38 -08001268 if (should_failslab(s->object_size, flags, s->flags))
1269 return NULL;
1270
1271 return memcg_kmem_get_cache(s, flags);
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001272}
1273
1274static inline void slab_post_alloc_hook(struct kmem_cache *s,
1275 gfp_t flags, void *object)
1276{
1277 flags &= gfp_allowed_mask;
1278 kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
1279 kmemleak_alloc_recursive(object, s->object_size, 1, s->flags, flags);
Vladimir Davydov8135be52014-12-12 16:56:38 -08001280 memcg_kmem_put_cache(s);
Roman Bobnievd56791b2013-10-08 15:58:57 -07001281}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001282
Roman Bobnievd56791b2013-10-08 15:58:57 -07001283static inline void slab_free_hook(struct kmem_cache *s, void *x)
1284{
1285 kmemleak_free_recursive(x, s->flags);
Christoph Lameter7d550c52010-08-25 14:07:16 -05001286
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001287 /*
1288 * Trouble is that we may no longer disable interrupts in the fast path
1289 * So in order to make the debug calls that expect irqs to be
1290 * disabled we need to disable interrupts temporarily.
1291 */
1292#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
1293 {
1294 unsigned long flags;
1295
1296 local_irq_save(flags);
1297 kmemcheck_slab_free(s, x, s->object_size);
1298 debug_check_no_locks_freed(x, s->object_size);
1299 local_irq_restore(flags);
1300 }
1301#endif
1302 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1303 debug_check_no_obj_freed(x, s->object_size);
1304}
Christoph Lameter205ab992008-04-14 19:11:40 +03001305
Christoph Lameter81819f02007-05-06 14:49:36 -07001306/*
1307 * Slab allocation and freeing
1308 */
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001309static inline struct page *alloc_slab_page(struct kmem_cache *s,
1310 gfp_t flags, int node, struct kmem_cache_order_objects oo)
Christoph Lameter65c33762008-04-14 19:11:40 +03001311{
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001312 struct page *page;
Christoph Lameter65c33762008-04-14 19:11:40 +03001313 int order = oo_order(oo);
1314
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001315 flags |= __GFP_NOTRACK;
1316
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001317 if (memcg_charge_slab(s, flags, order))
1318 return NULL;
1319
Christoph Lameter2154a332010-07-09 14:07:10 -05001320 if (node == NUMA_NO_NODE)
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001321 page = alloc_pages(flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001322 else
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001323 page = alloc_pages_exact_node(node, flags, order);
1324
1325 if (!page)
1326 memcg_uncharge_slab(s, order);
1327
1328 return page;
Christoph Lameter65c33762008-04-14 19:11:40 +03001329}
1330
Christoph Lameter81819f02007-05-06 14:49:36 -07001331static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1332{
Pekka Enberg06428782008-01-07 23:20:27 -08001333 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001334 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001335 gfp_t alloc_gfp;
Christoph Lameter81819f02007-05-06 14:49:36 -07001336
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001337 flags &= gfp_allowed_mask;
1338
1339 if (flags & __GFP_WAIT)
1340 local_irq_enable();
1341
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001342 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001343
Pekka Enbergba522702009-06-24 21:59:51 +03001344 /*
1345 * Let the initial higher-order allocation fail under memory pressure
1346 * so we fall-back to the minimum order allocation.
1347 */
1348 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
1349
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001350 page = alloc_slab_page(s, alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001351 if (unlikely(!page)) {
1352 oo = s->min;
Joonsoo Kim80c3a992014-03-12 17:26:20 +09001353 alloc_gfp = flags;
Christoph Lameter65c33762008-04-14 19:11:40 +03001354 /*
1355 * Allocation may have failed due to fragmentation.
1356 * Try a lower order alloc if possible
1357 */
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001358 page = alloc_slab_page(s, alloc_gfp, node, oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001359
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001360 if (page)
1361 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001362 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001363
David Rientjes737b7192012-07-09 14:00:38 -07001364 if (kmemcheck_enabled && page
Amerigo Wang5086c389c2009-08-19 21:44:13 +03001365 && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001366 int pages = 1 << oo_order(oo);
1367
Joonsoo Kim80c3a992014-03-12 17:26:20 +09001368 kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001369
1370 /*
1371 * Objects from caches that have a constructor don't get
1372 * cleared when they're allocated, so we need to do it here.
1373 */
1374 if (s->ctor)
1375 kmemcheck_mark_uninitialized_pages(page, pages);
1376 else
1377 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001378 }
1379
David Rientjes737b7192012-07-09 14:00:38 -07001380 if (flags & __GFP_WAIT)
1381 local_irq_disable();
1382 if (!page)
1383 return NULL;
1384
Christoph Lameter834f3d12008-04-14 19:11:31 +03001385 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001386 mod_zone_page_state(page_zone(page),
1387 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1388 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Christoph Lameter65c33762008-04-14 19:11:40 +03001389 1 << oo_order(oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07001390
1391 return page;
1392}
1393
1394static void setup_object(struct kmem_cache *s, struct page *page,
1395 void *object)
1396{
Christoph Lameter3ec09742007-05-16 22:11:00 -07001397 setup_object_debug(s, page, object);
Christoph Lameter4f104932007-05-06 14:50:17 -07001398 if (unlikely(s->ctor))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001399 s->ctor(object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001400}
1401
1402static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1403{
1404 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07001405 void *start;
Christoph Lameter81819f02007-05-06 14:49:36 -07001406 void *p;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001407 int order;
Wei Yang54266642014-08-06 16:04:42 -07001408 int idx;
Christoph Lameter81819f02007-05-06 14:49:36 -07001409
Andrew Mortonc871ac42014-12-10 15:42:25 -08001410 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
1411 pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
1412 BUG();
1413 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001414
Christoph Lameter6cb06222007-10-16 01:25:41 -07001415 page = allocate_slab(s,
1416 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001417 if (!page)
1418 goto out;
1419
Glauber Costa1f458cb2012-12-18 14:22:50 -08001420 order = compound_order(page);
Christoph Lameter205ab992008-04-14 19:11:40 +03001421 inc_slabs_node(s, page_to_nid(page), page->objects);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001422 page->slab_cache = s;
Joonsoo Kimc03f94c2012-05-18 00:47:47 +09001423 __SetPageSlab(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001424 if (page->pfmemalloc)
1425 SetPageSlabPfmemalloc(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001426
1427 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001428
1429 if (unlikely(s->flags & SLAB_POISON))
Glauber Costa1f458cb2012-12-18 14:22:50 -08001430 memset(start, POISON_INUSE, PAGE_SIZE << order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001431
Wei Yang54266642014-08-06 16:04:42 -07001432 for_each_object_idx(p, idx, s, start, page->objects) {
1433 setup_object(s, page, p);
1434 if (likely(idx < page->objects))
1435 set_freepointer(s, p, p + s->size);
1436 else
1437 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001438 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001439
1440 page->freelist = start;
Christoph Lametere6e82ea2011-08-09 16:12:24 -05001441 page->inuse = page->objects;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05001442 page->frozen = 1;
Christoph Lameter81819f02007-05-06 14:49:36 -07001443out:
Christoph Lameter81819f02007-05-06 14:49:36 -07001444 return page;
1445}
1446
1447static void __free_slab(struct kmem_cache *s, struct page *page)
1448{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001449 int order = compound_order(page);
1450 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001451
Christoph Lameteraf537b02010-07-09 14:07:14 -05001452 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001453 void *p;
1454
1455 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001456 for_each_object(p, s, page_address(page),
1457 page->objects)
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001458 check_object(s, page, p, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001459 }
1460
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001461 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001462
Christoph Lameter81819f02007-05-06 14:49:36 -07001463 mod_zone_page_state(page_zone(page),
1464 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1465 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001466 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001467
Mel Gorman072bb0a2012-07-31 16:43:58 -07001468 __ClearPageSlabPfmemalloc(page);
Christoph Lameter49bd5222008-04-14 18:52:18 +03001469 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001470
Mel Gorman22b751c2013-02-22 16:34:59 -08001471 page_mapcount_reset(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001472 if (current->reclaim_state)
1473 current->reclaim_state->reclaimed_slab += pages;
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001474 __free_pages(page, order);
1475 memcg_uncharge_slab(s, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001476}
1477
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001478#define need_reserve_slab_rcu \
1479 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1480
Christoph Lameter81819f02007-05-06 14:49:36 -07001481static void rcu_free_slab(struct rcu_head *h)
1482{
1483 struct page *page;
1484
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001485 if (need_reserve_slab_rcu)
1486 page = virt_to_head_page(h);
1487 else
1488 page = container_of((struct list_head *)h, struct page, lru);
1489
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001490 __free_slab(page->slab_cache, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001491}
1492
1493static void free_slab(struct kmem_cache *s, struct page *page)
1494{
1495 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001496 struct rcu_head *head;
1497
1498 if (need_reserve_slab_rcu) {
1499 int order = compound_order(page);
1500 int offset = (PAGE_SIZE << order) - s->reserved;
1501
1502 VM_BUG_ON(s->reserved != sizeof(*head));
1503 head = page_address(page) + offset;
1504 } else {
1505 /*
1506 * RCU free overloads the RCU head over the LRU
1507 */
1508 head = (void *)&page->lru;
1509 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001510
1511 call_rcu(head, rcu_free_slab);
1512 } else
1513 __free_slab(s, page);
1514}
1515
1516static void discard_slab(struct kmem_cache *s, struct page *page)
1517{
Christoph Lameter205ab992008-04-14 19:11:40 +03001518 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001519 free_slab(s, page);
1520}
1521
1522/*
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001523 * Management of partially allocated slabs.
Christoph Lameter81819f02007-05-06 14:49:36 -07001524 */
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001525static inline void
1526__add_partial(struct kmem_cache_node *n, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001527{
Christoph Lametere95eed52007-05-06 14:49:44 -07001528 n->nr_partial++;
Shaohua Li136333d2011-08-24 08:57:52 +08001529 if (tail == DEACTIVATE_TO_TAIL)
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001530 list_add_tail(&page->lru, &n->partial);
1531 else
1532 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001533}
1534
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001535static inline void add_partial(struct kmem_cache_node *n,
1536 struct page *page, int tail)
1537{
1538 lockdep_assert_held(&n->list_lock);
1539 __add_partial(n, page, tail);
1540}
1541
1542static inline void
1543__remove_partial(struct kmem_cache_node *n, struct page *page)
1544{
1545 list_del(&page->lru);
1546 n->nr_partial--;
1547}
1548
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001549static inline void remove_partial(struct kmem_cache_node *n,
Christoph Lameter62e346a2010-09-28 08:10:28 -05001550 struct page *page)
1551{
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001552 lockdep_assert_held(&n->list_lock);
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001553 __remove_partial(n, page);
Christoph Lameter62e346a2010-09-28 08:10:28 -05001554}
1555
Christoph Lameter81819f02007-05-06 14:49:36 -07001556/*
Christoph Lameter7ced3712012-05-09 10:09:53 -05001557 * Remove slab from the partial list, freeze it and
1558 * return the pointer to the freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001559 *
Christoph Lameter497b66f2011-08-09 16:12:26 -05001560 * Returns a list of objects or NULL if it fails.
Christoph Lameter81819f02007-05-06 14:49:36 -07001561 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001562static inline void *acquire_slab(struct kmem_cache *s,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001563 struct kmem_cache_node *n, struct page *page,
Joonsoo Kim633b0762013-01-21 17:01:25 +09001564 int mode, int *objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001565{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001566 void *freelist;
1567 unsigned long counters;
1568 struct page new;
1569
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001570 lockdep_assert_held(&n->list_lock);
1571
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001572 /*
1573 * Zap the freelist and set the frozen bit.
1574 * The old freelist is the list of objects for the
1575 * per cpu allocation list.
1576 */
Christoph Lameter7ced3712012-05-09 10:09:53 -05001577 freelist = page->freelist;
1578 counters = page->counters;
1579 new.counters = counters;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001580 *objects = new.objects - new.inuse;
Pekka Enberg23910c52012-06-04 10:14:58 +03001581 if (mode) {
Christoph Lameter7ced3712012-05-09 10:09:53 -05001582 new.inuse = page->objects;
Pekka Enberg23910c52012-06-04 10:14:58 +03001583 new.freelist = NULL;
1584 } else {
1585 new.freelist = freelist;
1586 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001587
Dave Hansena0132ac2014-01-29 14:05:50 -08001588 VM_BUG_ON(new.frozen);
Christoph Lameter7ced3712012-05-09 10:09:53 -05001589 new.frozen = 1;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001590
Christoph Lameter7ced3712012-05-09 10:09:53 -05001591 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001592 freelist, counters,
Joonsoo Kim02d76332012-05-17 00:13:02 +09001593 new.freelist, new.counters,
Christoph Lameter7ced3712012-05-09 10:09:53 -05001594 "acquire_slab"))
Christoph Lameter7ced3712012-05-09 10:09:53 -05001595 return NULL;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001596
1597 remove_partial(n, page);
Christoph Lameter7ced3712012-05-09 10:09:53 -05001598 WARN_ON(!freelist);
Christoph Lameter49e22582011-08-09 16:12:27 -05001599 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001600}
1601
Joonsoo Kim633b0762013-01-21 17:01:25 +09001602static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001603static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
Christoph Lameter49e22582011-08-09 16:12:27 -05001604
Christoph Lameter81819f02007-05-06 14:49:36 -07001605/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001606 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001607 */
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001608static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
1609 struct kmem_cache_cpu *c, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001610{
Christoph Lameter49e22582011-08-09 16:12:27 -05001611 struct page *page, *page2;
1612 void *object = NULL;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001613 int available = 0;
1614 int objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001615
1616 /*
1617 * Racy check. If we mistakenly see no partial slabs then we
1618 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001619 * partial slab and there is none available then get_partials()
1620 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001621 */
1622 if (!n || !n->nr_partial)
1623 return NULL;
1624
1625 spin_lock(&n->list_lock);
Christoph Lameter49e22582011-08-09 16:12:27 -05001626 list_for_each_entry_safe(page, page2, &n->partial, lru) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001627 void *t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001628
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001629 if (!pfmemalloc_match(page, flags))
1630 continue;
1631
Joonsoo Kim633b0762013-01-21 17:01:25 +09001632 t = acquire_slab(s, n, page, object == NULL, &objects);
Christoph Lameter49e22582011-08-09 16:12:27 -05001633 if (!t)
1634 break;
1635
Joonsoo Kim633b0762013-01-21 17:01:25 +09001636 available += objects;
Alex,Shi12d79632011-09-07 10:26:36 +08001637 if (!object) {
Christoph Lameter49e22582011-08-09 16:12:27 -05001638 c->page = page;
Christoph Lameter49e22582011-08-09 16:12:27 -05001639 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05001640 object = t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001641 } else {
Joonsoo Kim633b0762013-01-21 17:01:25 +09001642 put_cpu_partial(s, page, 0);
Alex Shi8028dce2012-02-03 23:34:56 +08001643 stat(s, CPU_PARTIAL_NODE);
Christoph Lameter49e22582011-08-09 16:12:27 -05001644 }
Joonsoo Kim345c9052013-06-19 14:05:52 +09001645 if (!kmem_cache_has_cpu_partial(s)
1646 || available > s->cpu_partial / 2)
Christoph Lameter49e22582011-08-09 16:12:27 -05001647 break;
1648
Christoph Lameter497b66f2011-08-09 16:12:26 -05001649 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001650 spin_unlock(&n->list_lock);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001651 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001652}
1653
1654/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001655 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001656 */
Joonsoo Kimde3ec032012-01-27 00:12:23 -08001657static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001658 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001659{
1660#ifdef CONFIG_NUMA
1661 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001662 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001663 struct zone *zone;
1664 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001665 void *object;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001666 unsigned int cpuset_mems_cookie;
Christoph Lameter81819f02007-05-06 14:49:36 -07001667
1668 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001669 * The defrag ratio allows a configuration of the tradeoffs between
1670 * inter node defragmentation and node local allocations. A lower
1671 * defrag_ratio increases the tendency to do local allocations
1672 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001673 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001674 * If the defrag_ratio is set to 0 then kmalloc() always
1675 * returns node local objects. If the ratio is higher then kmalloc()
1676 * may return off node objects because partial slabs are obtained
1677 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001678 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001679 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001680 * defrag_ratio = 1000) then every (well almost) allocation will
1681 * first attempt to defrag slab caches on other nodes. This means
1682 * scanning over all nodes to look for partial slabs which may be
1683 * expensive if we do it every time we are trying to find a slab
1684 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001685 */
Christoph Lameter98246012008-01-07 23:20:26 -08001686 if (!s->remote_node_defrag_ratio ||
1687 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001688 return NULL;
1689
Mel Gormancc9a6c82012-03-21 16:34:11 -07001690 do {
Mel Gormand26914d2014-04-03 14:47:24 -07001691 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07001692 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001693 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1694 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07001695
Mel Gormancc9a6c82012-03-21 16:34:11 -07001696 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001697
Vladimir Davydovdee2f8a2014-12-12 16:58:28 -08001698 if (n && cpuset_zone_allowed(zone, flags) &&
Mel Gormancc9a6c82012-03-21 16:34:11 -07001699 n->nr_partial > s->min_partial) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001700 object = get_partial_node(s, n, c, flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001701 if (object) {
1702 /*
Mel Gormand26914d2014-04-03 14:47:24 -07001703 * Don't check read_mems_allowed_retry()
1704 * here - if mems_allowed was updated in
1705 * parallel, that was a harmless race
1706 * between allocation and the cpuset
1707 * update
Mel Gormancc9a6c82012-03-21 16:34:11 -07001708 */
Mel Gormancc9a6c82012-03-21 16:34:11 -07001709 return object;
1710 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001711 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001712 }
Mel Gormand26914d2014-04-03 14:47:24 -07001713 } while (read_mems_allowed_retry(cpuset_mems_cookie));
Christoph Lameter81819f02007-05-06 14:49:36 -07001714#endif
1715 return NULL;
1716}
1717
1718/*
1719 * Get a partial page, lock it and return it.
1720 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001721static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001722 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001723{
Christoph Lameter497b66f2011-08-09 16:12:26 -05001724 void *object;
Joonsoo Kima561ce02014-10-09 15:26:15 -07001725 int searchnode = node;
1726
1727 if (node == NUMA_NO_NODE)
1728 searchnode = numa_mem_id();
1729 else if (!node_present_pages(node))
1730 searchnode = node_to_mem_node(node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001731
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001732 object = get_partial_node(s, get_node(s, searchnode), c, flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001733 if (object || node != NUMA_NO_NODE)
1734 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001735
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001736 return get_any_partial(s, flags, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001737}
1738
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001739#ifdef CONFIG_PREEMPT
1740/*
1741 * Calculate the next globally unique transaction for disambiguiation
1742 * during cmpxchg. The transactions start with the cpu number and are then
1743 * incremented by CONFIG_NR_CPUS.
1744 */
1745#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1746#else
1747/*
1748 * No preemption supported therefore also no need to check for
1749 * different cpus.
1750 */
1751#define TID_STEP 1
1752#endif
1753
1754static inline unsigned long next_tid(unsigned long tid)
1755{
1756 return tid + TID_STEP;
1757}
1758
1759static inline unsigned int tid_to_cpu(unsigned long tid)
1760{
1761 return tid % TID_STEP;
1762}
1763
1764static inline unsigned long tid_to_event(unsigned long tid)
1765{
1766 return tid / TID_STEP;
1767}
1768
1769static inline unsigned int init_tid(int cpu)
1770{
1771 return cpu;
1772}
1773
1774static inline void note_cmpxchg_failure(const char *n,
1775 const struct kmem_cache *s, unsigned long tid)
1776{
1777#ifdef SLUB_DEBUG_CMPXCHG
1778 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
1779
Fabian Frederickf9f58282014-06-04 16:06:34 -07001780 pr_info("%s %s: cmpxchg redo ", n, s->name);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001781
1782#ifdef CONFIG_PREEMPT
1783 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
Fabian Frederickf9f58282014-06-04 16:06:34 -07001784 pr_warn("due to cpu change %d -> %d\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001785 tid_to_cpu(tid), tid_to_cpu(actual_tid));
1786 else
1787#endif
1788 if (tid_to_event(tid) != tid_to_event(actual_tid))
Fabian Frederickf9f58282014-06-04 16:06:34 -07001789 pr_warn("due to cpu running other code. Event %ld->%ld\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001790 tid_to_event(tid), tid_to_event(actual_tid));
1791 else
Fabian Frederickf9f58282014-06-04 16:06:34 -07001792 pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001793 actual_tid, tid, next_tid(tid));
1794#endif
Christoph Lameter4fdccdf2011-03-22 13:35:00 -05001795 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001796}
1797
Fengguang Wu788e1aa2012-09-28 16:34:05 +08001798static void init_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001799{
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001800 int cpu;
1801
1802 for_each_possible_cpu(cpu)
1803 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001804}
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001805
1806/*
1807 * Remove the cpu slab
1808 */
Chen Gangd0e0ac92013-07-15 09:05:29 +08001809static void deactivate_slab(struct kmem_cache *s, struct page *page,
1810 void *freelist)
Christoph Lameter81819f02007-05-06 14:49:36 -07001811{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001812 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001813 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1814 int lock = 0;
1815 enum slab_modes l = M_NONE, m = M_NONE;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001816 void *nextfree;
Shaohua Li136333d2011-08-24 08:57:52 +08001817 int tail = DEACTIVATE_TO_HEAD;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001818 struct page new;
1819 struct page old;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001820
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001821 if (page->freelist) {
Christoph Lameter84e554e62009-12-18 16:26:23 -06001822 stat(s, DEACTIVATE_REMOTE_FREES);
Shaohua Li136333d2011-08-24 08:57:52 +08001823 tail = DEACTIVATE_TO_TAIL;
Christoph Lameter894b8782007-05-10 03:15:16 -07001824 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001825
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001826 /*
1827 * Stage one: Free all available per cpu objects back
1828 * to the page freelist while it is still frozen. Leave the
1829 * last one.
1830 *
1831 * There is no need to take the list->lock because the page
1832 * is still frozen.
1833 */
1834 while (freelist && (nextfree = get_freepointer(s, freelist))) {
1835 void *prior;
1836 unsigned long counters;
1837
1838 do {
1839 prior = page->freelist;
1840 counters = page->counters;
1841 set_freepointer(s, freelist, prior);
1842 new.counters = counters;
1843 new.inuse--;
Dave Hansena0132ac2014-01-29 14:05:50 -08001844 VM_BUG_ON(!new.frozen);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001845
Christoph Lameter1d071712011-07-14 12:49:12 -05001846 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001847 prior, counters,
1848 freelist, new.counters,
1849 "drain percpu freelist"));
1850
1851 freelist = nextfree;
1852 }
1853
1854 /*
1855 * Stage two: Ensure that the page is unfrozen while the
1856 * list presence reflects the actual number of objects
1857 * during unfreeze.
1858 *
1859 * We setup the list membership and then perform a cmpxchg
1860 * with the count. If there is a mismatch then the page
1861 * is not unfrozen but the page is on the wrong list.
1862 *
1863 * Then we restart the process which may have to remove
1864 * the page from the list that we just put it on again
1865 * because the number of objects in the slab may have
1866 * changed.
1867 */
1868redo:
1869
1870 old.freelist = page->freelist;
1871 old.counters = page->counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08001872 VM_BUG_ON(!old.frozen);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001873
1874 /* Determine target state of the slab */
1875 new.counters = old.counters;
1876 if (freelist) {
1877 new.inuse--;
1878 set_freepointer(s, freelist, old.freelist);
1879 new.freelist = freelist;
1880 } else
1881 new.freelist = old.freelist;
1882
1883 new.frozen = 0;
1884
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07001885 if (!new.inuse && n->nr_partial >= s->min_partial)
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001886 m = M_FREE;
1887 else if (new.freelist) {
1888 m = M_PARTIAL;
1889 if (!lock) {
1890 lock = 1;
1891 /*
1892 * Taking the spinlock removes the possiblity
1893 * that acquire_slab() will see a slab page that
1894 * is frozen
1895 */
1896 spin_lock(&n->list_lock);
1897 }
1898 } else {
1899 m = M_FULL;
1900 if (kmem_cache_debug(s) && !lock) {
1901 lock = 1;
1902 /*
1903 * This also ensures that the scanning of full
1904 * slabs from diagnostic functions will not see
1905 * any frozen slabs.
1906 */
1907 spin_lock(&n->list_lock);
1908 }
1909 }
1910
1911 if (l != m) {
1912
1913 if (l == M_PARTIAL)
1914
1915 remove_partial(n, page);
1916
1917 else if (l == M_FULL)
1918
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001919 remove_full(s, n, page);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001920
1921 if (m == M_PARTIAL) {
1922
1923 add_partial(n, page, tail);
Shaohua Li136333d2011-08-24 08:57:52 +08001924 stat(s, tail);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001925
1926 } else if (m == M_FULL) {
1927
1928 stat(s, DEACTIVATE_FULL);
1929 add_full(s, n, page);
1930
1931 }
1932 }
1933
1934 l = m;
Christoph Lameter1d071712011-07-14 12:49:12 -05001935 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001936 old.freelist, old.counters,
1937 new.freelist, new.counters,
1938 "unfreezing slab"))
1939 goto redo;
1940
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001941 if (lock)
1942 spin_unlock(&n->list_lock);
1943
1944 if (m == M_FREE) {
1945 stat(s, DEACTIVATE_EMPTY);
1946 discard_slab(s, page);
1947 stat(s, FREE_SLAB);
1948 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001949}
1950
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001951/*
1952 * Unfreeze all the cpu partial slabs.
1953 *
Christoph Lameter59a09912012-11-28 16:23:00 +00001954 * This function must be called with interrupts disabled
1955 * for the cpu using c (or some other guarantee must be there
1956 * to guarantee no concurrent accesses).
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001957 */
Christoph Lameter59a09912012-11-28 16:23:00 +00001958static void unfreeze_partials(struct kmem_cache *s,
1959 struct kmem_cache_cpu *c)
Christoph Lameter49e22582011-08-09 16:12:27 -05001960{
Joonsoo Kim345c9052013-06-19 14:05:52 +09001961#ifdef CONFIG_SLUB_CPU_PARTIAL
Joonsoo Kim43d77862012-06-09 02:23:16 +09001962 struct kmem_cache_node *n = NULL, *n2 = NULL;
Shaohua Li9ada1932011-11-14 13:34:13 +08001963 struct page *page, *discard_page = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05001964
1965 while ((page = c->partial)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05001966 struct page new;
1967 struct page old;
1968
1969 c->partial = page->next;
Joonsoo Kim43d77862012-06-09 02:23:16 +09001970
1971 n2 = get_node(s, page_to_nid(page));
1972 if (n != n2) {
1973 if (n)
1974 spin_unlock(&n->list_lock);
1975
1976 n = n2;
1977 spin_lock(&n->list_lock);
1978 }
Christoph Lameter49e22582011-08-09 16:12:27 -05001979
1980 do {
1981
1982 old.freelist = page->freelist;
1983 old.counters = page->counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08001984 VM_BUG_ON(!old.frozen);
Christoph Lameter49e22582011-08-09 16:12:27 -05001985
1986 new.counters = old.counters;
1987 new.freelist = old.freelist;
1988
1989 new.frozen = 0;
1990
Joonsoo Kimd24ac772012-05-18 22:01:17 +09001991 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter49e22582011-08-09 16:12:27 -05001992 old.freelist, old.counters,
1993 new.freelist, new.counters,
1994 "unfreezing slab"));
1995
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07001996 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) {
Shaohua Li9ada1932011-11-14 13:34:13 +08001997 page->next = discard_page;
1998 discard_page = page;
Joonsoo Kim43d77862012-06-09 02:23:16 +09001999 } else {
2000 add_partial(n, page, DEACTIVATE_TO_TAIL);
2001 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05002002 }
2003 }
2004
2005 if (n)
2006 spin_unlock(&n->list_lock);
Shaohua Li9ada1932011-11-14 13:34:13 +08002007
2008 while (discard_page) {
2009 page = discard_page;
2010 discard_page = discard_page->next;
2011
2012 stat(s, DEACTIVATE_EMPTY);
2013 discard_slab(s, page);
2014 stat(s, FREE_SLAB);
2015 }
Joonsoo Kim345c9052013-06-19 14:05:52 +09002016#endif
Christoph Lameter49e22582011-08-09 16:12:27 -05002017}
2018
2019/*
2020 * Put a page that was just frozen (in __slab_free) into a partial page
2021 * slot if available. This is done without interrupts disabled and without
2022 * preemption disabled. The cmpxchg is racy and may put the partial page
2023 * onto a random cpus partial slot.
2024 *
2025 * If we did not find a slot then simply move all the partials to the
2026 * per node partial list.
2027 */
Joonsoo Kim633b0762013-01-21 17:01:25 +09002028static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
Christoph Lameter49e22582011-08-09 16:12:27 -05002029{
Joonsoo Kim345c9052013-06-19 14:05:52 +09002030#ifdef CONFIG_SLUB_CPU_PARTIAL
Christoph Lameter49e22582011-08-09 16:12:27 -05002031 struct page *oldpage;
2032 int pages;
2033 int pobjects;
2034
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002035 preempt_disable();
Christoph Lameter49e22582011-08-09 16:12:27 -05002036 do {
2037 pages = 0;
2038 pobjects = 0;
2039 oldpage = this_cpu_read(s->cpu_slab->partial);
2040
2041 if (oldpage) {
2042 pobjects = oldpage->pobjects;
2043 pages = oldpage->pages;
2044 if (drain && pobjects > s->cpu_partial) {
2045 unsigned long flags;
2046 /*
2047 * partial array is full. Move the existing
2048 * set to the per node partial list.
2049 */
2050 local_irq_save(flags);
Christoph Lameter59a09912012-11-28 16:23:00 +00002051 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
Christoph Lameter49e22582011-08-09 16:12:27 -05002052 local_irq_restore(flags);
Joonsoo Kime24fc412012-06-23 03:22:38 +09002053 oldpage = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05002054 pobjects = 0;
2055 pages = 0;
Alex Shi8028dce2012-02-03 23:34:56 +08002056 stat(s, CPU_PARTIAL_DRAIN);
Christoph Lameter49e22582011-08-09 16:12:27 -05002057 }
2058 }
2059
2060 pages++;
2061 pobjects += page->objects - page->inuse;
2062
2063 page->pages = pages;
2064 page->pobjects = pobjects;
2065 page->next = oldpage;
2066
Chen Gangd0e0ac92013-07-15 09:05:29 +08002067 } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
2068 != oldpage);
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002069 if (unlikely(!s->cpu_partial)) {
2070 unsigned long flags;
2071
2072 local_irq_save(flags);
2073 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
2074 local_irq_restore(flags);
2075 }
2076 preempt_enable();
Joonsoo Kim345c9052013-06-19 14:05:52 +09002077#endif
Christoph Lameter49e22582011-08-09 16:12:27 -05002078}
2079
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002080static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002081{
Christoph Lameter84e554e62009-12-18 16:26:23 -06002082 stat(s, CPUSLAB_FLUSH);
Christoph Lameterc17dda42012-05-09 10:09:57 -05002083 deactivate_slab(s, c->page, c->freelist);
2084
2085 c->tid = next_tid(c->tid);
2086 c->page = NULL;
2087 c->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002088}
2089
2090/*
2091 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08002092 *
Christoph Lameter81819f02007-05-06 14:49:36 -07002093 * Called from IPI handler with interrupts disabled.
2094 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002095static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07002096{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002097 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07002098
Christoph Lameter49e22582011-08-09 16:12:27 -05002099 if (likely(c)) {
2100 if (c->page)
2101 flush_slab(s, c);
2102
Christoph Lameter59a09912012-11-28 16:23:00 +00002103 unfreeze_partials(s, c);
Christoph Lameter49e22582011-08-09 16:12:27 -05002104 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002105}
2106
2107static void flush_cpu_slab(void *d)
2108{
2109 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07002110
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002111 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07002112}
2113
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002114static bool has_cpu_slab(int cpu, void *info)
2115{
2116 struct kmem_cache *s = info;
2117 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
2118
majianpeng02e1a9c2012-05-17 17:03:26 -07002119 return c->page || c->partial;
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002120}
2121
Christoph Lameter81819f02007-05-06 14:49:36 -07002122static void flush_all(struct kmem_cache *s)
2123{
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002124 on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
Christoph Lameter81819f02007-05-06 14:49:36 -07002125}
2126
2127/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002128 * Check if the objects in a per cpu structure fit numa
2129 * locality expectations.
2130 */
Christoph Lameter57d437d2012-05-09 10:09:59 -05002131static inline int node_match(struct page *page, int node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002132{
2133#ifdef CONFIG_NUMA
Christoph Lameter4d7868e2013-01-23 21:45:47 +00002134 if (!page || (node != NUMA_NO_NODE && page_to_nid(page) != node))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002135 return 0;
2136#endif
2137 return 1;
2138}
2139
David Rientjes9a02d692014-06-04 16:06:36 -07002140#ifdef CONFIG_SLUB_DEBUG
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002141static int count_free(struct page *page)
2142{
2143 return page->objects - page->inuse;
2144}
2145
David Rientjes9a02d692014-06-04 16:06:36 -07002146static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
2147{
2148 return atomic_long_read(&n->total_objects);
2149}
2150#endif /* CONFIG_SLUB_DEBUG */
2151
2152#if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS)
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002153static unsigned long count_partial(struct kmem_cache_node *n,
2154 int (*get_count)(struct page *))
2155{
2156 unsigned long flags;
2157 unsigned long x = 0;
2158 struct page *page;
2159
2160 spin_lock_irqsave(&n->list_lock, flags);
2161 list_for_each_entry(page, &n->partial, lru)
2162 x += get_count(page);
2163 spin_unlock_irqrestore(&n->list_lock, flags);
2164 return x;
2165}
David Rientjes9a02d692014-06-04 16:06:36 -07002166#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002167
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002168static noinline void
2169slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
2170{
David Rientjes9a02d692014-06-04 16:06:36 -07002171#ifdef CONFIG_SLUB_DEBUG
2172 static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
2173 DEFAULT_RATELIMIT_BURST);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002174 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002175 struct kmem_cache_node *n;
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002176
David Rientjes9a02d692014-06-04 16:06:36 -07002177 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
2178 return;
2179
Fabian Frederickf9f58282014-06-04 16:06:34 -07002180 pr_warn("SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002181 nid, gfpflags);
Fabian Frederickf9f58282014-06-04 16:06:34 -07002182 pr_warn(" cache: %s, object size: %d, buffer size: %d, default order: %d, min order: %d\n",
2183 s->name, s->object_size, s->size, oo_order(s->oo),
2184 oo_order(s->min));
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002185
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002186 if (oo_order(s->min) > get_order(s->object_size))
Fabian Frederickf9f58282014-06-04 16:06:34 -07002187 pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
2188 s->name);
David Rientjesfa5ec8a2009-07-07 00:14:14 -07002189
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002190 for_each_kmem_cache_node(s, node, n) {
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002191 unsigned long nr_slabs;
2192 unsigned long nr_objs;
2193 unsigned long nr_free;
2194
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002195 nr_free = count_partial(n, count_free);
2196 nr_slabs = node_nr_slabs(n);
2197 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002198
Fabian Frederickf9f58282014-06-04 16:06:34 -07002199 pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002200 node, nr_slabs, nr_objs, nr_free);
2201 }
David Rientjes9a02d692014-06-04 16:06:36 -07002202#endif
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002203}
2204
Christoph Lameter497b66f2011-08-09 16:12:26 -05002205static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
2206 int node, struct kmem_cache_cpu **pc)
2207{
Christoph Lameter6faa6832012-05-09 10:09:51 -05002208 void *freelist;
Christoph Lameter188fd062012-05-09 10:09:55 -05002209 struct kmem_cache_cpu *c = *pc;
2210 struct page *page;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002211
Christoph Lameter188fd062012-05-09 10:09:55 -05002212 freelist = get_partial(s, flags, node, c);
2213
2214 if (freelist)
2215 return freelist;
2216
2217 page = new_slab(s, flags, node);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002218 if (page) {
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002219 c = raw_cpu_ptr(s->cpu_slab);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002220 if (c->page)
2221 flush_slab(s, c);
2222
2223 /*
2224 * No other reference to the page yet so we can
2225 * muck around with it freely without cmpxchg
2226 */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002227 freelist = page->freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002228 page->freelist = NULL;
2229
2230 stat(s, ALLOC_SLAB);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002231 c->page = page;
2232 *pc = c;
2233 } else
Christoph Lameter6faa6832012-05-09 10:09:51 -05002234 freelist = NULL;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002235
Christoph Lameter6faa6832012-05-09 10:09:51 -05002236 return freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002237}
2238
Mel Gorman072bb0a2012-07-31 16:43:58 -07002239static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
2240{
2241 if (unlikely(PageSlabPfmemalloc(page)))
2242 return gfp_pfmemalloc_allowed(gfpflags);
2243
2244 return true;
2245}
2246
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002247/*
Chen Gangd0e0ac92013-07-15 09:05:29 +08002248 * Check the page->freelist of a page and either transfer the freelist to the
2249 * per cpu freelist or deactivate the page.
Christoph Lameter213eeb92011-11-11 14:07:14 -06002250 *
2251 * The page is still frozen if the return value is not NULL.
2252 *
2253 * If this function returns NULL then the page has been unfrozen.
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002254 *
2255 * This function must be called with interrupt disabled.
Christoph Lameter213eeb92011-11-11 14:07:14 -06002256 */
2257static inline void *get_freelist(struct kmem_cache *s, struct page *page)
2258{
2259 struct page new;
2260 unsigned long counters;
2261 void *freelist;
2262
2263 do {
2264 freelist = page->freelist;
2265 counters = page->counters;
Christoph Lameter6faa6832012-05-09 10:09:51 -05002266
Christoph Lameter213eeb92011-11-11 14:07:14 -06002267 new.counters = counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08002268 VM_BUG_ON(!new.frozen);
Christoph Lameter213eeb92011-11-11 14:07:14 -06002269
2270 new.inuse = page->objects;
2271 new.frozen = freelist != NULL;
2272
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002273 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter213eeb92011-11-11 14:07:14 -06002274 freelist, counters,
2275 NULL, new.counters,
2276 "get_freelist"));
2277
2278 return freelist;
2279}
2280
2281/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002282 * Slow path. The lockless freelist is empty or we need to perform
2283 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07002284 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002285 * Processing is still very fast if new objects have been freed to the
2286 * regular freelist. In that case we simply take over the regular freelist
2287 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07002288 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002289 * If that is not working then we fall back to the partial lists. We take the
2290 * first element of the freelist as the object to allocate now and move the
2291 * rest of the freelist to the lockless freelist.
2292 *
2293 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08002294 * we need to allocate a new slab. This is the slowest path since it involves
2295 * a call to the page allocator and the setup of a new slab.
Christoph Lameter81819f02007-05-06 14:49:36 -07002296 */
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002297static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
2298 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002299{
Christoph Lameter6faa6832012-05-09 10:09:51 -05002300 void *freelist;
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002301 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002302 unsigned long flags;
2303
2304 local_irq_save(flags);
2305#ifdef CONFIG_PREEMPT
2306 /*
2307 * We may have been preempted and rescheduled on a different
2308 * cpu before disabling interrupts. Need to reload cpu area
2309 * pointer.
2310 */
2311 c = this_cpu_ptr(s->cpu_slab);
2312#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002313
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002314 page = c->page;
2315 if (!page)
Christoph Lameter81819f02007-05-06 14:49:36 -07002316 goto new_slab;
Christoph Lameter49e22582011-08-09 16:12:27 -05002317redo:
Christoph Lameter6faa6832012-05-09 10:09:51 -05002318
Christoph Lameter57d437d2012-05-09 10:09:59 -05002319 if (unlikely(!node_match(page, node))) {
Joonsoo Kima561ce02014-10-09 15:26:15 -07002320 int searchnode = node;
2321
2322 if (node != NUMA_NO_NODE && !node_present_pages(node))
2323 searchnode = node_to_mem_node(node);
2324
2325 if (unlikely(!node_match(page, searchnode))) {
2326 stat(s, ALLOC_NODE_MISMATCH);
2327 deactivate_slab(s, page, c->freelist);
2328 c->page = NULL;
2329 c->freelist = NULL;
2330 goto new_slab;
2331 }
Christoph Lameterfc59c052011-06-01 12:25:56 -05002332 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08002333
Mel Gorman072bb0a2012-07-31 16:43:58 -07002334 /*
2335 * By rights, we should be searching for a slab page that was
2336 * PFMEMALLOC but right now, we are losing the pfmemalloc
2337 * information when the page leaves the per-cpu allocator
2338 */
2339 if (unlikely(!pfmemalloc_match(page, gfpflags))) {
2340 deactivate_slab(s, page, c->freelist);
2341 c->page = NULL;
2342 c->freelist = NULL;
2343 goto new_slab;
2344 }
2345
Eric Dumazet73736e02011-12-13 04:57:06 +01002346 /* must check again c->freelist in case of cpu migration or IRQ */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002347 freelist = c->freelist;
2348 if (freelist)
Eric Dumazet73736e02011-12-13 04:57:06 +01002349 goto load_freelist;
2350
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002351 freelist = get_freelist(s, page);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002352
Christoph Lameter6faa6832012-05-09 10:09:51 -05002353 if (!freelist) {
Christoph Lameter03e404a2011-06-01 12:25:58 -05002354 c->page = NULL;
2355 stat(s, DEACTIVATE_BYPASS);
Christoph Lameterfc59c052011-06-01 12:25:56 -05002356 goto new_slab;
Christoph Lameter03e404a2011-06-01 12:25:58 -05002357 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002358
Christoph Lameter81819f02007-05-06 14:49:36 -07002359 stat(s, ALLOC_REFILL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002360
Christoph Lameter894b8782007-05-10 03:15:16 -07002361load_freelist:
Christoph Lameter507effe2012-05-09 10:09:52 -05002362 /*
2363 * freelist is pointing to the list of objects to be used.
2364 * page is pointing to the page from which the objects are obtained.
2365 * That page must be frozen for per cpu allocations to work.
2366 */
Dave Hansena0132ac2014-01-29 14:05:50 -08002367 VM_BUG_ON(!c->page->frozen);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002368 c->freelist = get_freepointer(s, freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002369 c->tid = next_tid(c->tid);
2370 local_irq_restore(flags);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002371 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07002372
Christoph Lameter81819f02007-05-06 14:49:36 -07002373new_slab:
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002374
Christoph Lameter49e22582011-08-09 16:12:27 -05002375 if (c->partial) {
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002376 page = c->page = c->partial;
2377 c->partial = page->next;
Christoph Lameter49e22582011-08-09 16:12:27 -05002378 stat(s, CPU_PARTIAL_ALLOC);
2379 c->freelist = NULL;
2380 goto redo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002381 }
2382
Christoph Lameter188fd062012-05-09 10:09:55 -05002383 freelist = new_slab_objects(s, gfpflags, node, &c);
Christoph Lameterb811c202007-10-16 23:25:51 -07002384
Christoph Lameterf46974362012-05-09 10:09:54 -05002385 if (unlikely(!freelist)) {
David Rientjes9a02d692014-06-04 16:06:36 -07002386 slab_out_of_memory(s, gfpflags, node);
Christoph Lameterf46974362012-05-09 10:09:54 -05002387 local_irq_restore(flags);
2388 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002389 }
Christoph Lameter894b8782007-05-10 03:15:16 -07002390
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002391 page = c->page;
Christoph Lameter5091b742012-07-31 16:44:00 -07002392 if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002393 goto load_freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07002394
Christoph Lameter497b66f2011-08-09 16:12:26 -05002395 /* Only entered in the debug case */
Chen Gangd0e0ac92013-07-15 09:05:29 +08002396 if (kmem_cache_debug(s) &&
2397 !alloc_debug_processing(s, page, freelist, addr))
Christoph Lameter497b66f2011-08-09 16:12:26 -05002398 goto new_slab; /* Slab failed checks. Next slab needed */
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002399
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002400 deactivate_slab(s, page, get_freepointer(s, freelist));
Christoph Lameterc17dda42012-05-09 10:09:57 -05002401 c->page = NULL;
2402 c->freelist = NULL;
Christoph Lametera71ae472011-05-25 09:47:43 -05002403 local_irq_restore(flags);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002404 return freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07002405}
2406
2407/*
2408 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2409 * have the fastpath folded into their functions. So no function call
2410 * overhead for requests that can be satisfied on the fastpath.
2411 *
2412 * The fastpath works by first checking if the lockless freelist can be used.
2413 * If not then __slab_alloc is called for slow processing.
2414 *
2415 * Otherwise we can simply pick the next object from the lockless free list.
2416 */
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002417static __always_inline void *slab_alloc_node(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002418 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002419{
Christoph Lameter894b8782007-05-10 03:15:16 -07002420 void **object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002421 struct kmem_cache_cpu *c;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002422 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002423 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002424
Vladimir Davydov8135be52014-12-12 16:56:38 -08002425 s = slab_pre_alloc_hook(s, gfpflags);
2426 if (!s)
Akinobu Mita773ff602008-12-23 19:37:01 +09002427 return NULL;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002428redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002429 /*
2430 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2431 * enabled. We may switch back and forth between cpus while
2432 * reading from one cpu area. That does not matter as long
2433 * as we end up on the original cpu again when doing the cmpxchg.
Christoph Lameter7cccd802013-01-23 21:45:48 +00002434 *
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002435 * We should guarantee that tid and kmem_cache are retrieved on
2436 * the same cpu. It could be different if CONFIG_PREEMPT so we need
2437 * to check if it is matched or not.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002438 */
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002439 do {
2440 tid = this_cpu_read(s->cpu_slab->tid);
2441 c = raw_cpu_ptr(s->cpu_slab);
2442 } while (IS_ENABLED(CONFIG_PREEMPT) && unlikely(tid != c->tid));
2443
2444 /*
2445 * Irqless object alloc/free algorithm used here depends on sequence
2446 * of fetching cpu_slab's data. tid should be fetched before anything
2447 * on c to guarantee that object and page associated with previous tid
2448 * won't be used with current tid. If we fetch tid first, object and
2449 * page could be one associated with next tid and our alloc/free
2450 * request will be failed. In this case, we will retry. So, no problem.
2451 */
2452 barrier();
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002453
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002454 /*
2455 * The transaction ids are globally unique per cpu and per operation on
2456 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2457 * occurs on the right processor and that there was no operation on the
2458 * linked list in between.
2459 */
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002460
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002461 object = c->freelist;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002462 page = c->page;
Dave Hansen8eae1492014-06-04 16:06:37 -07002463 if (unlikely(!object || !node_match(page, node))) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002464 object = __slab_alloc(s, gfpflags, node, addr, c);
Dave Hansen8eae1492014-06-04 16:06:37 -07002465 stat(s, ALLOC_SLOWPATH);
2466 } else {
Eric Dumazet0ad95002011-12-16 16:25:34 +01002467 void *next_object = get_freepointer_safe(s, object);
2468
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002469 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002470 * The cmpxchg will only match if there was no additional
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002471 * operation and if we are on the right processor.
2472 *
Chen Gangd0e0ac92013-07-15 09:05:29 +08002473 * The cmpxchg does the following atomically (without lock
2474 * semantics!)
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002475 * 1. Relocate first pointer to the current per cpu area.
2476 * 2. Verify that tid and freelist have not been changed
2477 * 3. If they were not changed replace tid and freelist
2478 *
Chen Gangd0e0ac92013-07-15 09:05:29 +08002479 * Since this is without lock semantics the protection is only
2480 * against code executing on this cpu *not* from access by
2481 * other cpus.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002482 */
Christoph Lameter933393f2011-12-22 11:58:51 -06002483 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002484 s->cpu_slab->freelist, s->cpu_slab->tid,
2485 object, tid,
Eric Dumazet0ad95002011-12-16 16:25:34 +01002486 next_object, next_tid(tid)))) {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002487
2488 note_cmpxchg_failure("slab_alloc", s, tid);
2489 goto redo;
2490 }
Eric Dumazet0ad95002011-12-16 16:25:34 +01002491 prefetch_freepointer(s, next_object);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002492 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002493 }
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002494
Pekka Enberg74e21342009-11-25 20:14:48 +02002495 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002496 memset(object, 0, s->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07002497
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002498 slab_post_alloc_hook(s, gfpflags, object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02002499
Christoph Lameter894b8782007-05-10 03:15:16 -07002500 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07002501}
2502
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002503static __always_inline void *slab_alloc(struct kmem_cache *s,
2504 gfp_t gfpflags, unsigned long addr)
2505{
2506 return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr);
2507}
2508
Christoph Lameter81819f02007-05-06 14:49:36 -07002509void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
2510{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002511 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002512
Chen Gangd0e0ac92013-07-15 09:05:29 +08002513 trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size,
2514 s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002515
2516 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002517}
2518EXPORT_SYMBOL(kmem_cache_alloc);
2519
Li Zefan0f24f122009-12-11 15:45:30 +08002520#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002521void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002522{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002523 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002524 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
2525 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002526}
Richard Kennedy4a923792010-10-21 10:29:19 +01002527EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002528#endif
2529
Christoph Lameter81819f02007-05-06 14:49:36 -07002530#ifdef CONFIG_NUMA
2531void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
2532{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002533 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002534
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002535 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002536 s->object_size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002537
2538 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002539}
2540EXPORT_SYMBOL(kmem_cache_alloc_node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002541
Li Zefan0f24f122009-12-11 15:45:30 +08002542#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002543void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002544 gfp_t gfpflags,
Richard Kennedy4a923792010-10-21 10:29:19 +01002545 int node, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002546{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002547 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002548
2549 trace_kmalloc_node(_RET_IP_, ret,
2550 size, s->size, gfpflags, node);
2551 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002552}
Richard Kennedy4a923792010-10-21 10:29:19 +01002553EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002554#endif
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002555#endif
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002556
Christoph Lameter81819f02007-05-06 14:49:36 -07002557/*
Kim Phillips94e4d712015-02-10 14:09:37 -08002558 * Slow path handling. This may still be called frequently since objects
Christoph Lameter894b8782007-05-10 03:15:16 -07002559 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07002560 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002561 * So we still attempt to reduce cache line usage. Just take the slab
2562 * lock and free the item. If there is no additional partial page
2563 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07002564 */
Christoph Lameter894b8782007-05-10 03:15:16 -07002565static void __slab_free(struct kmem_cache *s, struct page *page,
Christoph Lameterff120592009-12-18 16:26:22 -06002566 void *x, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07002567{
2568 void *prior;
2569 void **object = (void *)x;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002570 int was_frozen;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002571 struct page new;
2572 unsigned long counters;
2573 struct kmem_cache_node *n = NULL;
Christoph Lameter61728d12011-06-01 12:25:51 -05002574 unsigned long uninitialized_var(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002575
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002576 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07002577
Christoph Lameter19c7ff92012-05-30 12:54:46 -05002578 if (kmem_cache_debug(s) &&
2579 !(n = free_debug_processing(s, page, x, addr, &flags)))
Christoph Lameter80f08c12011-06-01 12:25:55 -05002580 return;
Christoph Lameter6446faa2008-02-15 23:45:26 -08002581
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002582 do {
Joonsoo Kim837d6782012-08-16 00:02:40 +09002583 if (unlikely(n)) {
2584 spin_unlock_irqrestore(&n->list_lock, flags);
2585 n = NULL;
2586 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002587 prior = page->freelist;
2588 counters = page->counters;
2589 set_freepointer(s, object, prior);
2590 new.counters = counters;
2591 was_frozen = new.frozen;
2592 new.inuse--;
Joonsoo Kim837d6782012-08-16 00:02:40 +09002593 if ((!new.inuse || !prior) && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002594
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002595 if (kmem_cache_has_cpu_partial(s) && !prior) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002596
2597 /*
Chen Gangd0e0ac92013-07-15 09:05:29 +08002598 * Slab was on no list before and will be
2599 * partially empty
2600 * We can defer the list move and instead
2601 * freeze it.
Christoph Lameter49e22582011-08-09 16:12:27 -05002602 */
2603 new.frozen = 1;
2604
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002605 } else { /* Needs to be taken off a list */
Christoph Lameter49e22582011-08-09 16:12:27 -05002606
LQYMGTb455def2014-12-10 15:42:13 -08002607 n = get_node(s, page_to_nid(page));
Christoph Lameter49e22582011-08-09 16:12:27 -05002608 /*
2609 * Speculatively acquire the list_lock.
2610 * If the cmpxchg does not succeed then we may
2611 * drop the list_lock without any processing.
2612 *
2613 * Otherwise the list_lock will synchronize with
2614 * other processors updating the list of slabs.
2615 */
2616 spin_lock_irqsave(&n->list_lock, flags);
2617
2618 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002619 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002620
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002621 } while (!cmpxchg_double_slab(s, page,
2622 prior, counters,
2623 object, new.counters,
2624 "__slab_free"));
Christoph Lameter81819f02007-05-06 14:49:36 -07002625
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002626 if (likely(!n)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002627
2628 /*
2629 * If we just froze the page then put it onto the
2630 * per cpu partial list.
2631 */
Alex Shi8028dce2012-02-03 23:34:56 +08002632 if (new.frozen && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002633 put_cpu_partial(s, page, 1);
Alex Shi8028dce2012-02-03 23:34:56 +08002634 stat(s, CPU_PARTIAL_FREE);
2635 }
Christoph Lameter49e22582011-08-09 16:12:27 -05002636 /*
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002637 * The list lock was not taken therefore no list
2638 * activity can be necessary.
2639 */
LQYMGTb455def2014-12-10 15:42:13 -08002640 if (was_frozen)
2641 stat(s, FREE_FROZEN);
2642 return;
2643 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002644
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07002645 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
Joonsoo Kim837d6782012-08-16 00:02:40 +09002646 goto slab_empty;
Christoph Lameter81819f02007-05-06 14:49:36 -07002647
Joonsoo Kim837d6782012-08-16 00:02:40 +09002648 /*
2649 * Objects left in the slab. If it was not on the partial list before
2650 * then add it.
2651 */
Joonsoo Kim345c9052013-06-19 14:05:52 +09002652 if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) {
2653 if (kmem_cache_debug(s))
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002654 remove_full(s, n, page);
Joonsoo Kim837d6782012-08-16 00:02:40 +09002655 add_partial(n, page, DEACTIVATE_TO_TAIL);
2656 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07002657 }
Christoph Lameter80f08c12011-06-01 12:25:55 -05002658 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002659 return;
2660
2661slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08002662 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002663 /*
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002664 * Slab on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07002665 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002666 remove_partial(n, page);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002667 stat(s, FREE_REMOVE_PARTIAL);
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002668 } else {
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002669 /* Slab must be on the full list */
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002670 remove_full(s, n, page);
2671 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002672
Christoph Lameter80f08c12011-06-01 12:25:55 -05002673 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002674 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07002675 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002676}
2677
Christoph Lameter894b8782007-05-10 03:15:16 -07002678/*
2679 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2680 * can perform fastpath freeing without additional function calls.
2681 *
2682 * The fastpath is only possible if we are freeing to the current cpu slab
2683 * of this processor. This typically the case if we have just allocated
2684 * the item before.
2685 *
2686 * If fastpath is not possible then fall back to __slab_free where we deal
2687 * with all sorts of special processing.
2688 */
Pekka Enberg06428782008-01-07 23:20:27 -08002689static __always_inline void slab_free(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002690 struct page *page, void *x, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002691{
2692 void **object = (void *)x;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002693 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002694 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002695
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002696 slab_free_hook(s, x);
2697
Christoph Lametera24c5a02011-03-15 12:45:21 -05002698redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002699 /*
2700 * Determine the currently cpus per cpu slab.
2701 * The cpu may change afterward. However that does not matter since
2702 * data is retrieved via this pointer. If we are on the same cpu
2703 * during the cmpxchg then the free will succedd.
2704 */
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002705 do {
2706 tid = this_cpu_read(s->cpu_slab->tid);
2707 c = raw_cpu_ptr(s->cpu_slab);
2708 } while (IS_ENABLED(CONFIG_PREEMPT) && unlikely(tid != c->tid));
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002709
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002710 /* Same with comment on barrier() in slab_alloc_node() */
2711 barrier();
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002712
Christoph Lameter442b06b2011-05-17 16:29:31 -05002713 if (likely(page == c->page)) {
Christoph Lameterff120592009-12-18 16:26:22 -06002714 set_freepointer(s, object, c->freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002715
Christoph Lameter933393f2011-12-22 11:58:51 -06002716 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002717 s->cpu_slab->freelist, s->cpu_slab->tid,
2718 c->freelist, tid,
2719 object, next_tid(tid)))) {
2720
2721 note_cmpxchg_failure("slab_free", s, tid);
2722 goto redo;
2723 }
Christoph Lameter84e554e62009-12-18 16:26:23 -06002724 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002725 } else
Christoph Lameterff120592009-12-18 16:26:22 -06002726 __slab_free(s, page, x, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07002727
Christoph Lameter894b8782007-05-10 03:15:16 -07002728}
2729
Christoph Lameter81819f02007-05-06 14:49:36 -07002730void kmem_cache_free(struct kmem_cache *s, void *x)
2731{
Glauber Costab9ce5ef2012-12-18 14:22:46 -08002732 s = cache_from_obj(s, x);
2733 if (!s)
Christoph Lameter79576102012-09-04 23:06:14 +00002734 return;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08002735 slab_free(s, virt_to_head_page(x), x, _RET_IP_);
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02002736 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002737}
2738EXPORT_SYMBOL(kmem_cache_free);
2739
Christoph Lameter81819f02007-05-06 14:49:36 -07002740/*
Christoph Lameter672bba32007-05-09 02:32:39 -07002741 * Object placement in a slab is made very easy because we always start at
2742 * offset 0. If we tune the size of the object to the alignment then we can
2743 * get the required alignment by putting one properly sized object after
2744 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07002745 *
2746 * Notice that the allocation order determines the sizes of the per cpu
2747 * caches. Each processor has always one slab available for allocations.
2748 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07002749 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07002750 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07002751 */
2752
2753/*
2754 * Mininum / Maximum order of slab pages. This influences locking overhead
2755 * and slab fragmentation. A higher order reduces the number of partial slabs
2756 * and increases the number of allocations possible without having to
2757 * take the list_lock.
2758 */
2759static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03002760static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002761static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07002762
2763/*
Christoph Lameter81819f02007-05-06 14:49:36 -07002764 * Calculate the order of allocation given an slab object size.
2765 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002766 * The order of allocation has significant impact on performance and other
2767 * system components. Generally order 0 allocations should be preferred since
2768 * order 0 does not cause fragmentation in the page allocator. Larger objects
2769 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002770 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07002771 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07002772 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002773 * In order to reach satisfactory performance we must ensure that a minimum
2774 * number of objects is in one slab. Otherwise we may generate too much
2775 * activity on the partial lists which requires taking the list_lock. This is
2776 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07002777 *
Christoph Lameter672bba32007-05-09 02:32:39 -07002778 * slub_max_order specifies the order where we begin to stop considering the
2779 * number of objects in a slab as critical. If we reach slub_max_order then
2780 * we try to keep the page order as low as possible. So we accept more waste
2781 * of space in favor of a small page order.
2782 *
2783 * Higher order allocations also allow the placement of more objects in a
2784 * slab and thereby reduce object handling overhead. If the user has
2785 * requested a higher mininum order then we start with that one instead of
2786 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07002787 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002788static inline int slab_order(int size, int min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002789 int max_order, int fract_leftover, int reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002790{
2791 int order;
2792 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07002793 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07002794
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002795 if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04002796 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03002797
Christoph Lameter6300ea72007-07-17 04:03:20 -07002798 for (order = max(min_order,
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002799 fls(min_objects * size - 1) - PAGE_SHIFT);
2800 order <= max_order; order++) {
2801
Christoph Lameter81819f02007-05-06 14:49:36 -07002802 unsigned long slab_size = PAGE_SIZE << order;
2803
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002804 if (slab_size < min_objects * size + reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07002805 continue;
2806
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002807 rem = (slab_size - reserved) % size;
Christoph Lameter81819f02007-05-06 14:49:36 -07002808
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002809 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07002810 break;
2811
2812 }
Christoph Lameter672bba32007-05-09 02:32:39 -07002813
Christoph Lameter81819f02007-05-06 14:49:36 -07002814 return order;
2815}
2816
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002817static inline int calculate_order(int size, int reserved)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002818{
2819 int order;
2820 int min_objects;
2821 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002822 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002823
2824 /*
2825 * Attempt to find best configuration for a slab. This
2826 * works by first attempting to generate a layout with
2827 * the best configuration and backing off gradually.
2828 *
2829 * First we reduce the acceptable waste in a slab. Then
2830 * we reduce the minimum objects required in a slab.
2831 */
2832 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03002833 if (!min_objects)
2834 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002835 max_objects = order_objects(slub_max_order, size, reserved);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02002836 min_objects = min(min_objects, max_objects);
2837
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002838 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03002839 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002840 while (fraction >= 4) {
2841 order = slab_order(size, min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002842 slub_max_order, fraction, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002843 if (order <= slub_max_order)
2844 return order;
2845 fraction /= 2;
2846 }
Amerigo Wang5086c389c2009-08-19 21:44:13 +03002847 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002848 }
2849
2850 /*
2851 * We were unable to place multiple objects in a slab. Now
2852 * lets see if we can place a single object there.
2853 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002854 order = slab_order(size, 1, slub_max_order, 1, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002855 if (order <= slub_max_order)
2856 return order;
2857
2858 /*
2859 * Doh this slab cannot be placed using slub_max_order.
2860 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08002861 order = slab_order(size, 1, MAX_ORDER, 1, reserved);
David Rientjes818cf592009-04-23 09:58:22 +03002862 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07002863 return order;
2864 return -ENOSYS;
2865}
2866
Pekka Enberg5595cff2008-08-05 09:28:47 +03002867static void
Joonsoo Kim40534972012-05-11 00:50:47 +09002868init_kmem_cache_node(struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07002869{
2870 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07002871 spin_lock_init(&n->list_lock);
2872 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002873#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03002874 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07002875 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07002876 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002877#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07002878}
2879
Christoph Lameter55136592010-08-20 12:37:13 -05002880static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002881{
Christoph Lameter6c182dc2010-08-20 12:37:14 -05002882 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
Christoph Lameter95a05b42013-01-10 19:14:19 +00002883 KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002884
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002885 /*
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002886 * Must align to double word boundary for the double cmpxchg
2887 * instructions to work; see __pcpu_double_call_return_bool().
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002888 */
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04002889 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
2890 2 * sizeof(void *));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002891
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002892 if (!s->cpu_slab)
2893 return 0;
2894
2895 init_kmem_cache_cpus(s);
2896
2897 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002898}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002899
Christoph Lameter51df1142010-08-20 12:37:15 -05002900static struct kmem_cache *kmem_cache_node;
2901
Christoph Lameter81819f02007-05-06 14:49:36 -07002902/*
2903 * No kmalloc_node yet so do it by hand. We know that this is the first
2904 * slab on the node for this slabcache. There are no concurrent accesses
2905 * possible.
2906 *
Zhi Yong Wu721ae222013-11-08 20:47:37 +08002907 * Note that this function only works on the kmem_cache_node
2908 * when allocating for the kmem_cache_node. This is used for bootstrapping
Christoph Lameter4c93c3552007-10-16 01:26:08 -07002909 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07002910 */
Christoph Lameter55136592010-08-20 12:37:13 -05002911static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07002912{
2913 struct page *page;
2914 struct kmem_cache_node *n;
2915
Christoph Lameter51df1142010-08-20 12:37:15 -05002916 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07002917
Christoph Lameter51df1142010-08-20 12:37:15 -05002918 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002919
2920 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002921 if (page_to_nid(page) != node) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07002922 pr_err("SLUB: Unable to allocate memory from node %d\n", node);
2923 pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
Christoph Lametera2f92ee2007-08-22 14:01:57 -07002924 }
2925
Christoph Lameter81819f02007-05-06 14:49:36 -07002926 n = page->freelist;
2927 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002928 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lametere6e82ea2011-08-09 16:12:24 -05002929 page->inuse = 1;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05002930 page->frozen = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05002931 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07002932#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterf7cb1932010-09-29 07:15:01 -05002933 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
Christoph Lameter51df1142010-08-20 12:37:15 -05002934 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07002935#endif
Joonsoo Kim40534972012-05-11 00:50:47 +09002936 init_kmem_cache_node(n);
Christoph Lameter51df1142010-08-20 12:37:15 -05002937 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002938
Dave Hansen67b6c902014-01-24 07:20:23 -08002939 /*
Steven Rostedt1e4dd942014-02-10 14:25:46 -08002940 * No locks need to be taken here as it has just been
2941 * initialized and there is no concurrent access.
Dave Hansen67b6c902014-01-24 07:20:23 -08002942 */
Steven Rostedt1e4dd942014-02-10 14:25:46 -08002943 __add_partial(n, page, DEACTIVATE_TO_HEAD);
Christoph Lameter81819f02007-05-06 14:49:36 -07002944}
2945
2946static void free_kmem_cache_nodes(struct kmem_cache *s)
2947{
2948 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002949 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07002950
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002951 for_each_kmem_cache_node(s, node, n) {
2952 kmem_cache_free(kmem_cache_node, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002953 s->node[node] = NULL;
2954 }
2955}
2956
Christoph Lameter55136592010-08-20 12:37:13 -05002957static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07002958{
2959 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07002960
Christoph Lameterf64dc582007-10-16 01:25:33 -07002961 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002962 struct kmem_cache_node *n;
2963
Alexander Duyck73367bd2010-05-21 14:41:35 -07002964 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05002965 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002966 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07002967 }
Christoph Lameter51df1142010-08-20 12:37:15 -05002968 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05002969 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07002970
2971 if (!n) {
2972 free_kmem_cache_nodes(s);
2973 return 0;
2974 }
2975
Christoph Lameter81819f02007-05-06 14:49:36 -07002976 s->node[node] = n;
Joonsoo Kim40534972012-05-11 00:50:47 +09002977 init_kmem_cache_node(n);
Christoph Lameter81819f02007-05-06 14:49:36 -07002978 }
2979 return 1;
2980}
Christoph Lameter81819f02007-05-06 14:49:36 -07002981
David Rientjesc0bdb232009-02-25 09:16:35 +02002982static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08002983{
2984 if (min < MIN_PARTIAL)
2985 min = MIN_PARTIAL;
2986 else if (min > MAX_PARTIAL)
2987 min = MAX_PARTIAL;
2988 s->min_partial = min;
2989}
2990
Christoph Lameter81819f02007-05-06 14:49:36 -07002991/*
2992 * calculate_sizes() determines the order and the distribution of data within
2993 * a slab object.
2994 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03002995static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07002996{
2997 unsigned long flags = s->flags;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002998 unsigned long size = s->object_size;
Christoph Lameter834f3d12008-04-14 19:11:31 +03002999 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07003000
3001 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08003002 * Round up object size to the next word boundary. We can only
3003 * place the free pointer at word boundaries and this determines
3004 * the possible location of the free pointer.
3005 */
3006 size = ALIGN(size, sizeof(void *));
3007
3008#ifdef CONFIG_SLUB_DEBUG
3009 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07003010 * Determine if we can poison the object itself. If the user of
3011 * the slab may touch the object after free or before allocation
3012 * then we should never poison the object itself.
3013 */
3014 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07003015 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003016 s->flags |= __OBJECT_POISON;
3017 else
3018 s->flags &= ~__OBJECT_POISON;
3019
Christoph Lameter81819f02007-05-06 14:49:36 -07003020
3021 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003022 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07003023 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07003024 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07003025 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003026 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003027 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07003028#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003029
3030 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003031 * With that we have determined the number of bytes in actual use
3032 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07003033 */
3034 s->inuse = size;
3035
3036 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07003037 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003038 /*
3039 * Relocate free pointer after the object if it is not
3040 * permitted to overwrite the first word of the object on
3041 * kmem_cache_free.
3042 *
3043 * This is the case if we do RCU, have a constructor or
3044 * destructor or are poisoning the objects.
3045 */
3046 s->offset = size;
3047 size += sizeof(void *);
3048 }
3049
Christoph Lameterc12b3c62007-05-23 13:57:31 -07003050#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07003051 if (flags & SLAB_STORE_USER)
3052 /*
3053 * Need to store information about allocs and frees after
3054 * the object.
3055 */
3056 size += 2 * sizeof(struct track);
3057
Christoph Lameterbe7b3fb2007-05-09 02:32:36 -07003058 if (flags & SLAB_RED_ZONE)
Christoph Lameter81819f02007-05-06 14:49:36 -07003059 /*
3060 * Add some empty padding so that we can catch
3061 * overwrites from earlier objects rather than let
3062 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01003063 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07003064 * of the object.
3065 */
3066 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07003067#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07003068
Christoph Lameter81819f02007-05-06 14:49:36 -07003069 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07003070 * SLUB stores one object immediately after another beginning from
3071 * offset 0. In order to align the objects we have to simply size
3072 * each object to conform to the alignment.
3073 */
Christoph Lameter45906852012-11-28 16:23:16 +00003074 size = ALIGN(size, s->align);
Christoph Lameter81819f02007-05-06 14:49:36 -07003075 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003076 if (forced_order >= 0)
3077 order = forced_order;
3078 else
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003079 order = calculate_order(size, s->reserved);
Christoph Lameter81819f02007-05-06 14:49:36 -07003080
Christoph Lameter834f3d12008-04-14 19:11:31 +03003081 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07003082 return 0;
3083
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003084 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03003085 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003086 s->allocflags |= __GFP_COMP;
3087
3088 if (s->flags & SLAB_CACHE_DMA)
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003089 s->allocflags |= GFP_DMA;
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003090
3091 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3092 s->allocflags |= __GFP_RECLAIMABLE;
3093
Christoph Lameter81819f02007-05-06 14:49:36 -07003094 /*
3095 * Determine the number of objects per slab
3096 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003097 s->oo = oo_make(order, size, s->reserved);
3098 s->min = oo_make(get_order(size), size, s->reserved);
Christoph Lameter205ab992008-04-14 19:11:40 +03003099 if (oo_objects(s->oo) > oo_objects(s->max))
3100 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07003101
Christoph Lameter834f3d12008-04-14 19:11:31 +03003102 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07003103}
3104
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003105static int kmem_cache_open(struct kmem_cache *s, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003106{
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003107 s->flags = kmem_cache_flags(s->size, flags, s->name, s->ctor);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003108 s->reserved = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003109
Lai Jiangshanda9a6382011-03-10 15:22:00 +08003110 if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
3111 s->reserved = sizeof(struct rcu_head);
Christoph Lameter81819f02007-05-06 14:49:36 -07003112
Christoph Lameter06b285d2008-04-14 19:11:41 +03003113 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07003114 goto error;
David Rientjes3de47212009-07-27 18:30:35 -07003115 if (disable_higher_order_debug) {
3116 /*
3117 * Disable debugging flags that store metadata if the min slab
3118 * order increased.
3119 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003120 if (get_order(s->size) > get_order(s->object_size)) {
David Rientjes3de47212009-07-27 18:30:35 -07003121 s->flags &= ~DEBUG_METADATA_FLAGS;
3122 s->offset = 0;
3123 if (!calculate_sizes(s, -1))
3124 goto error;
3125 }
3126 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003127
Heiko Carstens25654092012-01-12 17:17:33 -08003128#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
3129 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -05003130 if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
3131 /* Enable fast mode */
3132 s->flags |= __CMPXCHG_DOUBLE;
3133#endif
3134
David Rientjes3b89d7d2009-02-22 17:40:07 -08003135 /*
3136 * The larger the object size is, the more pages we want on the partial
3137 * list to avoid pounding the page allocator excessively.
3138 */
Christoph Lameter49e22582011-08-09 16:12:27 -05003139 set_min_partial(s, ilog2(s->size) / 2);
3140
3141 /*
3142 * cpu_partial determined the maximum number of objects kept in the
3143 * per cpu partial lists of a processor.
3144 *
3145 * Per cpu partial lists mainly contain slabs that just have one
3146 * object freed. If they are used for allocation then they can be
3147 * filled up again with minimal effort. The slab will never hit the
3148 * per node partial lists and therefore no locking will be required.
3149 *
3150 * This setting also determines
3151 *
3152 * A) The number of objects from per cpu partial slabs dumped to the
3153 * per node list when we reach the limit.
Alex Shi9f264902011-09-01 11:32:18 +08003154 * B) The number of objects in cpu partial slabs to extract from the
Chen Gangd0e0ac92013-07-15 09:05:29 +08003155 * per node list when we run out of per cpu objects. We only fetch
3156 * 50% to keep some capacity around for frees.
Christoph Lameter49e22582011-08-09 16:12:27 -05003157 */
Joonsoo Kim345c9052013-06-19 14:05:52 +09003158 if (!kmem_cache_has_cpu_partial(s))
Christoph Lameter8f1e33d2011-11-23 09:24:27 -06003159 s->cpu_partial = 0;
3160 else if (s->size >= PAGE_SIZE)
Christoph Lameter49e22582011-08-09 16:12:27 -05003161 s->cpu_partial = 2;
3162 else if (s->size >= 1024)
3163 s->cpu_partial = 6;
3164 else if (s->size >= 256)
3165 s->cpu_partial = 13;
3166 else
3167 s->cpu_partial = 30;
3168
Christoph Lameter81819f02007-05-06 14:49:36 -07003169#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05003170 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07003171#endif
Christoph Lameter55136592010-08-20 12:37:13 -05003172 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003173 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07003174
Christoph Lameter55136592010-08-20 12:37:13 -05003175 if (alloc_kmem_cache_cpus(s))
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003176 return 0;
Christoph Lameterff120592009-12-18 16:26:22 -06003177
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003178 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003179error:
3180 if (flags & SLAB_PANIC)
3181 panic("Cannot create slab %s size=%lu realsize=%u "
3182 "order=%u offset=%u flags=%lx\n",
Chen Gangd0e0ac92013-07-15 09:05:29 +08003183 s->name, (unsigned long)s->size, s->size,
3184 oo_order(s->oo), s->offset, flags);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003185 return -EINVAL;
Christoph Lameter81819f02007-05-06 14:49:36 -07003186}
Christoph Lameter81819f02007-05-06 14:49:36 -07003187
Christoph Lameter33b12c32008-04-25 12:22:43 -07003188static void list_slab_objects(struct kmem_cache *s, struct page *page,
3189 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07003190{
Christoph Lameter33b12c32008-04-25 12:22:43 -07003191#ifdef CONFIG_SLUB_DEBUG
3192 void *addr = page_address(page);
3193 void *p;
Namhyung Kima5dd5c12010-09-29 21:02:13 +09003194 unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
3195 sizeof(long), GFP_ATOMIC);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003196 if (!map)
3197 return;
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003198 slab_err(s, page, text, s->name);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003199 slab_lock(page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003200
Christoph Lameter5f80b132011-04-15 14:48:13 -05003201 get_map(s, page, map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003202 for_each_object(p, s, addr, page->objects) {
3203
3204 if (!test_bit(slab_index(p, s, addr), map)) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07003205 pr_err("INFO: Object 0x%p @offset=%tu\n", p, p - addr);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003206 print_tracking(s, p);
3207 }
3208 }
3209 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003210 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003211#endif
3212}
3213
Christoph Lameter81819f02007-05-06 14:49:36 -07003214/*
Christoph Lameter599870b2008-04-23 12:36:52 -07003215 * Attempt to free all partial slabs on a node.
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003216 * This is called from kmem_cache_close(). We must be the last thread
3217 * using the cache and therefore we do not need to lock anymore.
Christoph Lameter81819f02007-05-06 14:49:36 -07003218 */
Christoph Lameter599870b2008-04-23 12:36:52 -07003219static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07003220{
Christoph Lameter81819f02007-05-06 14:49:36 -07003221 struct page *page, *h;
3222
Christoph Lameter33b12c32008-04-25 12:22:43 -07003223 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003224 if (!page->inuse) {
Steven Rostedt1e4dd942014-02-10 14:25:46 -08003225 __remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07003226 discard_slab(s, page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003227 } else {
3228 list_slab_objects(s, page,
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003229 "Objects remaining in %s on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07003230 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07003231 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003232}
3233
3234/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003235 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07003236 */
Christoph Lameter0c710012007-07-17 04:03:24 -07003237static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003238{
3239 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003240 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003241
3242 flush_all(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003243 /* Attempt to free all objects */
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003244 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter599870b2008-04-23 12:36:52 -07003245 free_partial(s, n);
3246 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07003247 return 1;
3248 }
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003249 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07003250 free_kmem_cache_nodes(s);
3251 return 0;
3252}
3253
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003254int __kmem_cache_shutdown(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003255{
Christoph Lameter41a21282014-05-06 12:50:08 -07003256 return kmem_cache_close(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003257}
Christoph Lameter81819f02007-05-06 14:49:36 -07003258
3259/********************************************************************
3260 * Kmalloc subsystem
3261 *******************************************************************/
3262
Christoph Lameter81819f02007-05-06 14:49:36 -07003263static int __init setup_slub_min_order(char *str)
3264{
Pekka Enberg06428782008-01-07 23:20:27 -08003265 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003266
3267 return 1;
3268}
3269
3270__setup("slub_min_order=", setup_slub_min_order);
3271
3272static int __init setup_slub_max_order(char *str)
3273{
Pekka Enberg06428782008-01-07 23:20:27 -08003274 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03003275 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003276
3277 return 1;
3278}
3279
3280__setup("slub_max_order=", setup_slub_max_order);
3281
3282static int __init setup_slub_min_objects(char *str)
3283{
Pekka Enberg06428782008-01-07 23:20:27 -08003284 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07003285
3286 return 1;
3287}
3288
3289__setup("slub_min_objects=", setup_slub_min_objects);
3290
Christoph Lameter81819f02007-05-06 14:49:36 -07003291void *__kmalloc(size_t size, gfp_t flags)
3292{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003293 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003294 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003295
Christoph Lameter95a05b42013-01-10 19:14:19 +00003296 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003297 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003298
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003299 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003300
3301 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003302 return s;
3303
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003304 ret = slab_alloc(s, flags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003305
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003306 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003307
3308 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003309}
3310EXPORT_SYMBOL(__kmalloc);
3311
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003312#ifdef CONFIG_NUMA
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003313static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
3314{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003315 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003316 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003317
Vladimir Davydov52383432014-06-04 16:06:39 -07003318 flags |= __GFP_COMP | __GFP_NOTRACK;
3319 page = alloc_kmem_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003320 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003321 ptr = page_address(page);
3322
Roman Bobnievd56791b2013-10-08 15:58:57 -07003323 kmalloc_large_node_hook(ptr, size, flags);
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003324 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003325}
3326
Christoph Lameter81819f02007-05-06 14:49:36 -07003327void *__kmalloc_node(size_t size, gfp_t flags, int node)
3328{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003329 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003330 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003331
Christoph Lameter95a05b42013-01-10 19:14:19 +00003332 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003333 ret = kmalloc_large_node(size, flags, node);
3334
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003335 trace_kmalloc_node(_RET_IP_, ret,
3336 size, PAGE_SIZE << get_order(size),
3337 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003338
3339 return ret;
3340 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003341
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003342 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003343
3344 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003345 return s;
3346
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003347 ret = slab_alloc_node(s, flags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003348
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003349 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003350
3351 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003352}
3353EXPORT_SYMBOL(__kmalloc_node);
3354#endif
3355
3356size_t ksize(const void *object)
3357{
Christoph Lameter272c1d22007-06-08 13:46:49 -07003358 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07003359
Christoph Lameteref8b4522007-10-16 01:24:46 -07003360 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07003361 return 0;
3362
Vegard Nossum294a80a2007-12-04 23:45:30 -08003363 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08003364
Pekka Enberg76994412008-05-22 19:22:25 +03003365 if (unlikely(!PageSlab(page))) {
3366 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08003367 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03003368 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003369
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003370 return slab_ksize(page->slab_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003371}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02003372EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07003373
3374void kfree(const void *x)
3375{
Christoph Lameter81819f02007-05-06 14:49:36 -07003376 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08003377 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003378
Pekka Enberg2121db72009-03-25 11:05:57 +02003379 trace_kfree(_RET_IP_, x);
3380
Satyam Sharma2408c552007-10-16 01:24:44 -07003381 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07003382 return;
3383
Christoph Lameterb49af682007-05-06 14:49:41 -07003384 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003385 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07003386 BUG_ON(!PageCompound(page));
Roman Bobnievd56791b2013-10-08 15:58:57 -07003387 kfree_hook(x);
Vladimir Davydov52383432014-06-04 16:06:39 -07003388 __free_kmem_pages(page, compound_order(page));
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003389 return;
3390 }
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003391 slab_free(page->slab_cache, page, object, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07003392}
3393EXPORT_SYMBOL(kfree);
3394
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003395#define SHRINK_PROMOTE_MAX 32
3396
Christoph Lameter2086d262007-05-06 14:49:46 -07003397/*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003398 * kmem_cache_shrink discards empty slabs and promotes the slabs filled
3399 * up most to the head of the partial lists. New allocations will then
3400 * fill those up and thus they can be removed from the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -07003401 *
3402 * The slabs with the least items are placed last. This results in them
3403 * being allocated from last increasing the chance that the last objects
3404 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07003405 */
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003406int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
Christoph Lameter2086d262007-05-06 14:49:46 -07003407{
3408 int node;
3409 int i;
3410 struct kmem_cache_node *n;
3411 struct page *page;
3412 struct page *t;
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003413 struct list_head discard;
3414 struct list_head promote[SHRINK_PROMOTE_MAX];
Christoph Lameter2086d262007-05-06 14:49:46 -07003415 unsigned long flags;
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003416 int ret = 0;
Christoph Lameter2086d262007-05-06 14:49:46 -07003417
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003418 if (deactivate) {
3419 /*
3420 * Disable empty slabs caching. Used to avoid pinning offline
3421 * memory cgroups by kmem pages that can be freed.
3422 */
3423 s->cpu_partial = 0;
3424 s->min_partial = 0;
3425
3426 /*
3427 * s->cpu_partial is checked locklessly (see put_cpu_partial),
3428 * so we have to make sure the change is visible.
3429 */
3430 kick_all_cpus_sync();
3431 }
3432
Christoph Lameter2086d262007-05-06 14:49:46 -07003433 flush_all(s);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003434 for_each_kmem_cache_node(s, node, n) {
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003435 INIT_LIST_HEAD(&discard);
3436 for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
3437 INIT_LIST_HEAD(promote + i);
Christoph Lameter2086d262007-05-06 14:49:46 -07003438
3439 spin_lock_irqsave(&n->list_lock, flags);
3440
3441 /*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003442 * Build lists of slabs to discard or promote.
Christoph Lameter2086d262007-05-06 14:49:46 -07003443 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003444 * Note that concurrent frees may occur while we hold the
3445 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07003446 */
3447 list_for_each_entry_safe(page, t, &n->partial, lru) {
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003448 int free = page->objects - page->inuse;
3449
3450 /* Do not reread page->inuse */
3451 barrier();
3452
3453 /* We do not keep full slabs on the list */
3454 BUG_ON(free <= 0);
3455
3456 if (free == page->objects) {
3457 list_move(&page->lru, &discard);
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003458 n->nr_partial--;
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003459 } else if (free <= SHRINK_PROMOTE_MAX)
3460 list_move(&page->lru, promote + free - 1);
Christoph Lameter2086d262007-05-06 14:49:46 -07003461 }
3462
Christoph Lameter2086d262007-05-06 14:49:46 -07003463 /*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003464 * Promote the slabs filled up most to the head of the
3465 * partial list.
Christoph Lameter2086d262007-05-06 14:49:46 -07003466 */
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003467 for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
3468 list_splice(promote + i, &n->partial);
Christoph Lameter2086d262007-05-06 14:49:46 -07003469
Christoph Lameter2086d262007-05-06 14:49:46 -07003470 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003471
3472 /* Release empty slabs */
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003473 list_for_each_entry_safe(page, t, &discard, lru)
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003474 discard_slab(s, page);
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003475
3476 if (slabs_node(s, node))
3477 ret = 1;
Christoph Lameter2086d262007-05-06 14:49:46 -07003478 }
3479
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003480 return ret;
Christoph Lameter2086d262007-05-06 14:49:46 -07003481}
Christoph Lameter2086d262007-05-06 14:49:46 -07003482
Yasunori Gotob9049e22007-10-21 16:41:37 -07003483static int slab_mem_going_offline_callback(void *arg)
3484{
3485 struct kmem_cache *s;
3486
Christoph Lameter18004c52012-07-06 15:25:12 -05003487 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003488 list_for_each_entry(s, &slab_caches, list)
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003489 __kmem_cache_shrink(s, false);
Christoph Lameter18004c52012-07-06 15:25:12 -05003490 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003491
3492 return 0;
3493}
3494
3495static void slab_mem_offline_callback(void *arg)
3496{
3497 struct kmem_cache_node *n;
3498 struct kmem_cache *s;
3499 struct memory_notify *marg = arg;
3500 int offline_node;
3501
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003502 offline_node = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003503
3504 /*
3505 * If the node still has available memory. we need kmem_cache_node
3506 * for it yet.
3507 */
3508 if (offline_node < 0)
3509 return;
3510
Christoph Lameter18004c52012-07-06 15:25:12 -05003511 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003512 list_for_each_entry(s, &slab_caches, list) {
3513 n = get_node(s, offline_node);
3514 if (n) {
3515 /*
3516 * if n->nr_slabs > 0, slabs still exist on the node
3517 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05003518 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07003519 * callback. So, we must fail.
3520 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003521 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07003522
3523 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05003524 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003525 }
3526 }
Christoph Lameter18004c52012-07-06 15:25:12 -05003527 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003528}
3529
3530static int slab_mem_going_online_callback(void *arg)
3531{
3532 struct kmem_cache_node *n;
3533 struct kmem_cache *s;
3534 struct memory_notify *marg = arg;
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003535 int nid = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003536 int ret = 0;
3537
3538 /*
3539 * If the node's memory is already available, then kmem_cache_node is
3540 * already created. Nothing to do.
3541 */
3542 if (nid < 0)
3543 return 0;
3544
3545 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003546 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003547 * allocate a kmem_cache_node structure in order to bring the node
3548 * online.
3549 */
Christoph Lameter18004c52012-07-06 15:25:12 -05003550 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003551 list_for_each_entry(s, &slab_caches, list) {
3552 /*
3553 * XXX: kmem_cache_alloc_node will fallback to other nodes
3554 * since memory is not yet available from the node that
3555 * is brought up.
3556 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05003557 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003558 if (!n) {
3559 ret = -ENOMEM;
3560 goto out;
3561 }
Joonsoo Kim40534972012-05-11 00:50:47 +09003562 init_kmem_cache_node(n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003563 s->node[nid] = n;
3564 }
3565out:
Christoph Lameter18004c52012-07-06 15:25:12 -05003566 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003567 return ret;
3568}
3569
3570static int slab_memory_callback(struct notifier_block *self,
3571 unsigned long action, void *arg)
3572{
3573 int ret = 0;
3574
3575 switch (action) {
3576 case MEM_GOING_ONLINE:
3577 ret = slab_mem_going_online_callback(arg);
3578 break;
3579 case MEM_GOING_OFFLINE:
3580 ret = slab_mem_going_offline_callback(arg);
3581 break;
3582 case MEM_OFFLINE:
3583 case MEM_CANCEL_ONLINE:
3584 slab_mem_offline_callback(arg);
3585 break;
3586 case MEM_ONLINE:
3587 case MEM_CANCEL_OFFLINE:
3588 break;
3589 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003590 if (ret)
3591 ret = notifier_from_errno(ret);
3592 else
3593 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003594 return ret;
3595}
3596
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003597static struct notifier_block slab_memory_callback_nb = {
3598 .notifier_call = slab_memory_callback,
3599 .priority = SLAB_CALLBACK_PRI,
3600};
Yasunori Gotob9049e22007-10-21 16:41:37 -07003601
Christoph Lameter81819f02007-05-06 14:49:36 -07003602/********************************************************************
3603 * Basic setup of slabs
3604 *******************************************************************/
3605
Christoph Lameter51df1142010-08-20 12:37:15 -05003606/*
3607 * Used for early kmem_cache structures that were allocated using
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003608 * the page allocator. Allocate them properly then fix up the pointers
3609 * that may be pointing to the wrong kmem_cache structure.
Christoph Lameter51df1142010-08-20 12:37:15 -05003610 */
3611
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003612static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
Christoph Lameter51df1142010-08-20 12:37:15 -05003613{
3614 int node;
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003615 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003616 struct kmem_cache_node *n;
Christoph Lameter51df1142010-08-20 12:37:15 -05003617
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003618 memcpy(s, static_cache, kmem_cache->object_size);
Christoph Lameter51df1142010-08-20 12:37:15 -05003619
Glauber Costa7d557b32013-02-22 20:20:00 +04003620 /*
3621 * This runs very early, and only the boot processor is supposed to be
3622 * up. Even if it weren't true, IRQs are not up so we couldn't fire
3623 * IPIs around.
3624 */
3625 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003626 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter51df1142010-08-20 12:37:15 -05003627 struct page *p;
3628
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003629 list_for_each_entry(p, &n->partial, lru)
3630 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003631
Li Zefan607bf322011-04-12 15:22:26 +08003632#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003633 list_for_each_entry(p, &n->full, lru)
3634 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003635#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05003636 }
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08003637 slab_init_memcg_params(s);
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003638 list_add(&s->list, &slab_caches);
3639 return s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003640}
3641
Christoph Lameter81819f02007-05-06 14:49:36 -07003642void __init kmem_cache_init(void)
3643{
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003644 static __initdata struct kmem_cache boot_kmem_cache,
3645 boot_kmem_cache_node;
Christoph Lameter51df1142010-08-20 12:37:15 -05003646
Stanislaw Gruszkafc8d8622012-01-10 15:07:32 -08003647 if (debug_guardpage_minorder())
3648 slub_max_order = 0;
3649
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003650 kmem_cache_node = &boot_kmem_cache_node;
3651 kmem_cache = &boot_kmem_cache;
Christoph Lameter51df1142010-08-20 12:37:15 -05003652
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003653 create_boot_cache(kmem_cache_node, "kmem_cache_node",
3654 sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003655
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003656 register_hotmemory_notifier(&slab_memory_callback_nb);
Christoph Lameter81819f02007-05-06 14:49:36 -07003657
3658 /* Able to allocate the per node structures */
3659 slab_state = PARTIAL;
3660
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003661 create_boot_cache(kmem_cache, "kmem_cache",
3662 offsetof(struct kmem_cache, node) +
3663 nr_node_ids * sizeof(struct kmem_cache_node *),
3664 SLAB_HWCACHE_ALIGN);
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003665
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003666 kmem_cache = bootstrap(&boot_kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003667
Christoph Lameter51df1142010-08-20 12:37:15 -05003668 /*
3669 * Allocate kmem_cache_node properly from the kmem_cache slab.
3670 * kmem_cache_node is separately allocated so no need to
3671 * update any list pointers.
3672 */
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003673 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
Christoph Lameter51df1142010-08-20 12:37:15 -05003674
3675 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Christoph Lameterf97d5f62013-01-10 19:12:17 +00003676 create_kmalloc_caches(0);
Christoph Lameter81819f02007-05-06 14:49:36 -07003677
3678#ifdef CONFIG_SMP
3679 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003680#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003681
Fabian Frederickf9f58282014-06-04 16:06:34 -07003682 pr_info("SLUB: HWalign=%d, Order=%d-%d, MinObjects=%d, CPUs=%d, Nodes=%d\n",
Christoph Lameterf97d5f62013-01-10 19:12:17 +00003683 cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07003684 slub_min_order, slub_max_order, slub_min_objects,
3685 nr_cpu_ids, nr_node_ids);
3686}
3687
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003688void __init kmem_cache_init_late(void)
3689{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003690}
3691
Glauber Costa2633d7a2012-12-18 14:22:34 -08003692struct kmem_cache *
Vladimir Davydova44cb942014-04-07 15:39:23 -07003693__kmem_cache_alias(const char *name, size_t size, size_t align,
3694 unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07003695{
Vladimir Davydov426589f2015-02-12 14:59:23 -08003696 struct kmem_cache *s, *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07003697
Vladimir Davydova44cb942014-04-07 15:39:23 -07003698 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07003699 if (s) {
3700 s->refcount++;
Vladimir Davydov84d0ddd2014-04-07 15:39:29 -07003701
Christoph Lameter81819f02007-05-06 14:49:36 -07003702 /*
3703 * Adjust the object sizes so that we clear
3704 * the complete object on kzalloc.
3705 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003706 s->object_size = max(s->object_size, (int)size);
Christoph Lameter81819f02007-05-06 14:49:36 -07003707 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08003708
Vladimir Davydov426589f2015-02-12 14:59:23 -08003709 for_each_memcg_cache(c, s) {
Vladimir Davydov84d0ddd2014-04-07 15:39:29 -07003710 c->object_size = s->object_size;
3711 c->inuse = max_t(int, c->inuse,
3712 ALIGN(size, sizeof(void *)));
3713 }
3714
David Rientjes7b8f3b62008-12-17 22:09:46 -08003715 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08003716 s->refcount--;
Christoph Lametercbb79692012-09-05 00:18:32 +00003717 s = NULL;
David Rientjes7b8f3b62008-12-17 22:09:46 -08003718 }
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07003719 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08003720
Christoph Lametercbb79692012-09-05 00:18:32 +00003721 return s;
3722}
Pekka Enberg84c1cf62010-09-14 23:21:12 +03003723
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003724int __kmem_cache_create(struct kmem_cache *s, unsigned long flags)
Christoph Lametercbb79692012-09-05 00:18:32 +00003725{
Pekka Enbergaac3a162012-09-05 12:07:44 +03003726 int err;
Christoph Lameter20cea962012-07-06 15:25:13 -05003727
Pekka Enbergaac3a162012-09-05 12:07:44 +03003728 err = kmem_cache_open(s, flags);
3729 if (err)
3730 return err;
Christoph Lameter20cea962012-07-06 15:25:13 -05003731
Christoph Lameter45530c42012-11-28 16:23:07 +00003732 /* Mutex is not taken during early boot */
3733 if (slab_state <= UP)
3734 return 0;
3735
Glauber Costa107dab52012-12-18 14:23:05 -08003736 memcg_propagate_slab_attrs(s);
Pekka Enbergaac3a162012-09-05 12:07:44 +03003737 err = sysfs_slab_add(s);
Pekka Enbergaac3a162012-09-05 12:07:44 +03003738 if (err)
3739 kmem_cache_close(s);
3740
3741 return err;
Christoph Lameter81819f02007-05-06 14:49:36 -07003742}
Christoph Lameter81819f02007-05-06 14:49:36 -07003743
Christoph Lameter81819f02007-05-06 14:49:36 -07003744#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07003745/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003746 * Use the cpu notifier to insure that the cpu slabs are flushed when
3747 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07003748 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04003749static int slab_cpuup_callback(struct notifier_block *nfb,
Christoph Lameter81819f02007-05-06 14:49:36 -07003750 unsigned long action, void *hcpu)
3751{
3752 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003753 struct kmem_cache *s;
3754 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07003755
3756 switch (action) {
3757 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003758 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07003759 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003760 case CPU_DEAD_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05003761 mutex_lock(&slab_mutex);
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07003762 list_for_each_entry(s, &slab_caches, list) {
3763 local_irq_save(flags);
3764 __flush_cpu_slab(s, cpu);
3765 local_irq_restore(flags);
3766 }
Christoph Lameter18004c52012-07-06 15:25:12 -05003767 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07003768 break;
3769 default:
3770 break;
3771 }
3772 return NOTIFY_OK;
3773}
3774
Paul Gortmaker0db06282013-06-19 14:53:51 -04003775static struct notifier_block slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08003776 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08003777};
Christoph Lameter81819f02007-05-06 14:49:36 -07003778
3779#endif
3780
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003781void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003782{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003783 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003784 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003785
Christoph Lameter95a05b42013-01-10 19:14:19 +00003786 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003787 return kmalloc_large(size, gfpflags);
3788
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003789 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003790
Satyam Sharma2408c552007-10-16 01:24:44 -07003791 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003792 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003793
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003794 ret = slab_alloc(s, gfpflags, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003795
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003796 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003797 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003798
3799 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003800}
3801
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003802#ifdef CONFIG_NUMA
Christoph Lameter81819f02007-05-06 14:49:36 -07003803void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003804 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07003805{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003806 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003807 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003808
Christoph Lameter95a05b42013-01-10 19:14:19 +00003809 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08003810 ret = kmalloc_large_node(size, gfpflags, node);
3811
3812 trace_kmalloc_node(caller, ret,
3813 size, PAGE_SIZE << get_order(size),
3814 gfpflags, node);
3815
3816 return ret;
3817 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02003818
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003819 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07003820
Satyam Sharma2408c552007-10-16 01:24:44 -07003821 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003822 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07003823
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003824 ret = slab_alloc_node(s, gfpflags, node, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003825
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003826 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003827 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03003828
3829 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003830}
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003831#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003832
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003833#ifdef CONFIG_SYSFS
Christoph Lameter205ab992008-04-14 19:11:40 +03003834static int count_inuse(struct page *page)
3835{
3836 return page->inuse;
3837}
3838
3839static int count_total(struct page *page)
3840{
3841 return page->objects;
3842}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003843#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03003844
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05003845#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter434e2452007-07-17 04:03:30 -07003846static int validate_slab(struct kmem_cache *s, struct page *page,
3847 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003848{
3849 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08003850 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003851
3852 if (!check_slab(s, page) ||
3853 !on_freelist(s, page, NULL))
3854 return 0;
3855
3856 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03003857 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003858
Christoph Lameter5f80b132011-04-15 14:48:13 -05003859 get_map(s, page, map);
3860 for_each_object(p, s, addr, page->objects) {
3861 if (test_bit(slab_index(p, s, addr), map))
3862 if (!check_object(s, page, p, SLUB_RED_INACTIVE))
3863 return 0;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003864 }
3865
Christoph Lameter224a88b2008-04-14 19:11:31 +03003866 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07003867 if (!test_bit(slab_index(p, s, addr), map))
Tero Roponen37d57442010-12-01 20:04:20 +02003868 if (!check_object(s, page, p, SLUB_RED_ACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07003869 return 0;
3870 return 1;
3871}
3872
Christoph Lameter434e2452007-07-17 04:03:30 -07003873static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3874 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003875{
Christoph Lameter881db7f2011-06-01 12:25:53 -05003876 slab_lock(page);
3877 validate_slab(s, page, map);
3878 slab_unlock(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003879}
3880
Christoph Lameter434e2452007-07-17 04:03:30 -07003881static int validate_slab_node(struct kmem_cache *s,
3882 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003883{
3884 unsigned long count = 0;
3885 struct page *page;
3886 unsigned long flags;
3887
3888 spin_lock_irqsave(&n->list_lock, flags);
3889
3890 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003891 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003892 count++;
3893 }
3894 if (count != n->nr_partial)
Fabian Frederickf9f58282014-06-04 16:06:34 -07003895 pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
3896 s->name, count, n->nr_partial);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003897
3898 if (!(s->flags & SLAB_STORE_USER))
3899 goto out;
3900
3901 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07003902 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003903 count++;
3904 }
3905 if (count != atomic_long_read(&n->nr_slabs))
Fabian Frederickf9f58282014-06-04 16:06:34 -07003906 pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
3907 s->name, count, atomic_long_read(&n->nr_slabs));
Christoph Lameter53e15af2007-05-06 14:49:43 -07003908
3909out:
3910 spin_unlock_irqrestore(&n->list_lock, flags);
3911 return count;
3912}
3913
Christoph Lameter434e2452007-07-17 04:03:30 -07003914static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07003915{
3916 int node;
3917 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03003918 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07003919 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003920 struct kmem_cache_node *n;
Christoph Lameter434e2452007-07-17 04:03:30 -07003921
3922 if (!map)
3923 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07003924
3925 flush_all(s);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003926 for_each_kmem_cache_node(s, node, n)
Christoph Lameter434e2452007-07-17 04:03:30 -07003927 count += validate_slab_node(s, n, map);
Christoph Lameter434e2452007-07-17 04:03:30 -07003928 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07003929 return count;
3930}
Christoph Lameter88a420e2007-05-06 14:49:45 -07003931/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003932 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07003933 * and freed.
3934 */
3935
3936struct location {
3937 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003938 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003939 long long sum_time;
3940 long min_time;
3941 long max_time;
3942 long min_pid;
3943 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10303944 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07003945 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003946};
3947
3948struct loc_track {
3949 unsigned long max;
3950 unsigned long count;
3951 struct location *loc;
3952};
3953
3954static void free_loc_track(struct loc_track *t)
3955{
3956 if (t->max)
3957 free_pages((unsigned long)t->loc,
3958 get_order(sizeof(struct location) * t->max));
3959}
3960
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003961static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003962{
3963 struct location *l;
3964 int order;
3965
Christoph Lameter88a420e2007-05-06 14:49:45 -07003966 order = get_order(sizeof(struct location) * max);
3967
Christoph Lameter68dff6a2007-07-17 04:03:20 -07003968 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07003969 if (!l)
3970 return 0;
3971
3972 if (t->count) {
3973 memcpy(l, t->loc, sizeof(struct location) * t->count);
3974 free_loc_track(t);
3975 }
3976 t->max = max;
3977 t->loc = l;
3978 return 1;
3979}
3980
3981static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07003982 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07003983{
3984 long start, end, pos;
3985 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003986 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07003987 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07003988
3989 start = -1;
3990 end = t->count;
3991
3992 for ( ; ; ) {
3993 pos = start + (end - start + 1) / 2;
3994
3995 /*
3996 * There is nothing at "end". If we end up there
3997 * we need to add something to before end.
3998 */
3999 if (pos == end)
4000 break;
4001
4002 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004003 if (track->addr == caddr) {
4004
4005 l = &t->loc[pos];
4006 l->count++;
4007 if (track->when) {
4008 l->sum_time += age;
4009 if (age < l->min_time)
4010 l->min_time = age;
4011 if (age > l->max_time)
4012 l->max_time = age;
4013
4014 if (track->pid < l->min_pid)
4015 l->min_pid = track->pid;
4016 if (track->pid > l->max_pid)
4017 l->max_pid = track->pid;
4018
Rusty Russell174596a2009-01-01 10:12:29 +10304019 cpumask_set_cpu(track->cpu,
4020 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004021 }
4022 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004023 return 1;
4024 }
4025
Christoph Lameter45edfa52007-05-09 02:32:45 -07004026 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004027 end = pos;
4028 else
4029 start = pos;
4030 }
4031
4032 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07004033 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07004034 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004035 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004036 return 0;
4037
4038 l = t->loc + pos;
4039 if (pos < t->count)
4040 memmove(l + 1, l,
4041 (t->count - pos) * sizeof(struct location));
4042 t->count++;
4043 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004044 l->addr = track->addr;
4045 l->sum_time = age;
4046 l->min_time = age;
4047 l->max_time = age;
4048 l->min_pid = track->pid;
4049 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10304050 cpumask_clear(to_cpumask(l->cpus));
4051 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004052 nodes_clear(l->nodes);
4053 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004054 return 1;
4055}
4056
4057static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004058 struct page *page, enum track_item alloc,
Namhyung Kima5dd5c12010-09-29 21:02:13 +09004059 unsigned long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004060{
Christoph Lametera973e9d2008-03-01 13:40:44 -08004061 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004062 void *p;
4063
Christoph Lameter39b26462008-04-14 19:11:30 +03004064 bitmap_zero(map, page->objects);
Christoph Lameter5f80b132011-04-15 14:48:13 -05004065 get_map(s, page, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004066
Christoph Lameter224a88b2008-04-14 19:11:31 +03004067 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07004068 if (!test_bit(slab_index(p, s, addr), map))
4069 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07004070}
4071
4072static int list_locations(struct kmem_cache *s, char *buf,
4073 enum track_item alloc)
4074{
Harvey Harrisone374d482008-01-31 15:20:50 -08004075 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004076 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004077 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07004078 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004079 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
4080 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004081 struct kmem_cache_node *n;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004082
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004083 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4084 GFP_TEMPORARY)) {
4085 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004086 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004087 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07004088 /* Push back cpu slabs */
4089 flush_all(s);
4090
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004091 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07004092 unsigned long flags;
4093 struct page *page;
4094
Christoph Lameter9e869432007-08-22 14:01:56 -07004095 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004096 continue;
4097
4098 spin_lock_irqsave(&n->list_lock, flags);
4099 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004100 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004101 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004102 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004103 spin_unlock_irqrestore(&n->list_lock, flags);
4104 }
4105
4106 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07004107 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07004108
Hugh Dickins9c246242008-12-09 13:14:27 -08004109 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004110 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08004111 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004112
4113 if (l->addr)
Joe Perches62c70bc2011-01-13 15:45:52 -08004114 len += sprintf(buf + len, "%pS", (void *)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004115 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004116 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07004117
4118 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08004119 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07004120 l->min_time,
4121 (long)div_u64(l->sum_time, l->count),
4122 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004123 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08004124 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004125 l->min_time);
4126
4127 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08004128 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004129 l->min_pid, l->max_pid);
4130 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004131 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004132 l->min_pid);
4133
Rusty Russell174596a2009-01-01 10:12:29 +10304134 if (num_online_cpus() > 1 &&
4135 !cpumask_empty(to_cpumask(l->cpus)) &&
Tejun Heo5024c1d2015-02-13 14:37:59 -08004136 len < PAGE_SIZE - 60)
4137 len += scnprintf(buf + len, PAGE_SIZE - len - 50,
4138 " cpus=%*pbl",
4139 cpumask_pr_args(to_cpumask(l->cpus)));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004140
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004141 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Tejun Heo5024c1d2015-02-13 14:37:59 -08004142 len < PAGE_SIZE - 60)
4143 len += scnprintf(buf + len, PAGE_SIZE - len - 50,
4144 " nodes=%*pbl",
4145 nodemask_pr_args(&l->nodes));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004146
Harvey Harrisone374d482008-01-31 15:20:50 -08004147 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07004148 }
4149
4150 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004151 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004152 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08004153 len += sprintf(buf, "No data\n");
4154 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004155}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004156#endif
Christoph Lameter88a420e2007-05-06 14:49:45 -07004157
Christoph Lametera5a84752010-10-05 13:57:27 -05004158#ifdef SLUB_RESILIENCY_TEST
David Rientjesc07b8182014-08-06 16:04:16 -07004159static void __init resiliency_test(void)
Christoph Lametera5a84752010-10-05 13:57:27 -05004160{
4161 u8 *p;
4162
Christoph Lameter95a05b42013-01-10 19:14:19 +00004163 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || KMALLOC_SHIFT_HIGH < 10);
Christoph Lametera5a84752010-10-05 13:57:27 -05004164
Fabian Frederickf9f58282014-06-04 16:06:34 -07004165 pr_err("SLUB resiliency testing\n");
4166 pr_err("-----------------------\n");
4167 pr_err("A. Corruption after allocation\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004168
4169 p = kzalloc(16, GFP_KERNEL);
4170 p[16] = 0x12;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004171 pr_err("\n1. kmalloc-16: Clobber Redzone/next pointer 0x12->0x%p\n\n",
4172 p + 16);
Christoph Lametera5a84752010-10-05 13:57:27 -05004173
4174 validate_slab_cache(kmalloc_caches[4]);
4175
4176 /* Hmmm... The next two are dangerous */
4177 p = kzalloc(32, GFP_KERNEL);
4178 p[32 + sizeof(void *)] = 0x34;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004179 pr_err("\n2. kmalloc-32: Clobber next pointer/next slab 0x34 -> -0x%p\n",
4180 p);
4181 pr_err("If allocated object is overwritten then not detectable\n\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004182
4183 validate_slab_cache(kmalloc_caches[5]);
4184 p = kzalloc(64, GFP_KERNEL);
4185 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
4186 *p = 0x56;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004187 pr_err("\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4188 p);
4189 pr_err("If allocated object is overwritten then not detectable\n\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004190 validate_slab_cache(kmalloc_caches[6]);
4191
Fabian Frederickf9f58282014-06-04 16:06:34 -07004192 pr_err("\nB. Corruption after free\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004193 p = kzalloc(128, GFP_KERNEL);
4194 kfree(p);
4195 *p = 0x78;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004196 pr_err("1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004197 validate_slab_cache(kmalloc_caches[7]);
4198
4199 p = kzalloc(256, GFP_KERNEL);
4200 kfree(p);
4201 p[50] = 0x9a;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004202 pr_err("\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004203 validate_slab_cache(kmalloc_caches[8]);
4204
4205 p = kzalloc(512, GFP_KERNEL);
4206 kfree(p);
4207 p[512] = 0xab;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004208 pr_err("\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004209 validate_slab_cache(kmalloc_caches[9]);
4210}
4211#else
4212#ifdef CONFIG_SYSFS
4213static void resiliency_test(void) {};
4214#endif
4215#endif
4216
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004217#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -07004218enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03004219 SL_ALL, /* All slabs */
4220 SL_PARTIAL, /* Only partially allocated slabs */
4221 SL_CPU, /* Only slabs used for cpu caches */
4222 SL_OBJECTS, /* Determine allocated objects not slabs */
4223 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07004224};
4225
Christoph Lameter205ab992008-04-14 19:11:40 +03004226#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004227#define SO_PARTIAL (1 << SL_PARTIAL)
4228#define SO_CPU (1 << SL_CPU)
4229#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03004230#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004231
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004232static ssize_t show_slab_objects(struct kmem_cache *s,
4233 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07004234{
4235 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07004236 int node;
4237 int x;
4238 unsigned long *nodes;
Christoph Lameter81819f02007-05-06 14:49:36 -07004239
Chen Gange35e1a92013-07-12 08:23:48 +08004240 nodes = kzalloc(sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004241 if (!nodes)
4242 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07004243
Christoph Lameter205ab992008-04-14 19:11:40 +03004244 if (flags & SO_CPU) {
4245 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07004246
Christoph Lameter205ab992008-04-14 19:11:40 +03004247 for_each_possible_cpu(cpu) {
Chen Gangd0e0ac92013-07-15 09:05:29 +08004248 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab,
4249 cpu);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004250 int node;
Christoph Lameter49e22582011-08-09 16:12:27 -05004251 struct page *page;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004252
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004253 page = ACCESS_ONCE(c->page);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004254 if (!page)
4255 continue;
Christoph Lameter205ab992008-04-14 19:11:40 +03004256
Christoph Lameterec3ab082012-05-09 10:09:56 -05004257 node = page_to_nid(page);
4258 if (flags & SO_TOTAL)
4259 x = page->objects;
4260 else if (flags & SO_OBJECTS)
4261 x = page->inuse;
4262 else
4263 x = 1;
Christoph Lameter49e22582011-08-09 16:12:27 -05004264
Christoph Lameterec3ab082012-05-09 10:09:56 -05004265 total += x;
4266 nodes[node] += x;
4267
4268 page = ACCESS_ONCE(c->partial);
Christoph Lameter49e22582011-08-09 16:12:27 -05004269 if (page) {
Li Zefan8afb1472013-09-10 11:43:37 +08004270 node = page_to_nid(page);
4271 if (flags & SO_TOTAL)
4272 WARN_ON_ONCE(1);
4273 else if (flags & SO_OBJECTS)
4274 WARN_ON_ONCE(1);
4275 else
4276 x = page->pages;
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004277 total += x;
4278 nodes[node] += x;
Christoph Lameter49e22582011-08-09 16:12:27 -05004279 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004280 }
4281 }
4282
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004283 get_online_mems();
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004284#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03004285 if (flags & SO_ALL) {
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004286 struct kmem_cache_node *n;
4287
4288 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004289
Chen Gangd0e0ac92013-07-15 09:05:29 +08004290 if (flags & SO_TOTAL)
4291 x = atomic_long_read(&n->total_objects);
4292 else if (flags & SO_OBJECTS)
4293 x = atomic_long_read(&n->total_objects) -
4294 count_partial(n, count_free);
Christoph Lameter205ab992008-04-14 19:11:40 +03004295 else
4296 x = atomic_long_read(&n->nr_slabs);
4297 total += x;
4298 nodes[node] += x;
4299 }
4300
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004301 } else
4302#endif
4303 if (flags & SO_PARTIAL) {
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004304 struct kmem_cache_node *n;
Christoph Lameter205ab992008-04-14 19:11:40 +03004305
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004306 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter205ab992008-04-14 19:11:40 +03004307 if (flags & SO_TOTAL)
4308 x = count_partial(n, count_total);
4309 else if (flags & SO_OBJECTS)
4310 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07004311 else
4312 x = n->nr_partial;
4313 total += x;
4314 nodes[node] += x;
4315 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004316 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004317 x = sprintf(buf, "%lu", total);
4318#ifdef CONFIG_NUMA
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004319 for (node = 0; node < nr_node_ids; node++)
Christoph Lameter81819f02007-05-06 14:49:36 -07004320 if (nodes[node])
4321 x += sprintf(buf + x, " N%d=%lu",
4322 node, nodes[node]);
4323#endif
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004324 put_online_mems();
Christoph Lameter81819f02007-05-06 14:49:36 -07004325 kfree(nodes);
4326 return x + sprintf(buf + x, "\n");
4327}
4328
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004329#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07004330static int any_slab_objects(struct kmem_cache *s)
4331{
4332 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004333 struct kmem_cache_node *n;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004334
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004335 for_each_kmem_cache_node(s, node, n)
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07004336 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07004337 return 1;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004338
Christoph Lameter81819f02007-05-06 14:49:36 -07004339 return 0;
4340}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004341#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004342
4343#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
Phil Carmody497888c2011-07-14 15:07:13 +03004344#define to_slab(n) container_of(n, struct kmem_cache, kobj)
Christoph Lameter81819f02007-05-06 14:49:36 -07004345
4346struct slab_attribute {
4347 struct attribute attr;
4348 ssize_t (*show)(struct kmem_cache *s, char *buf);
4349 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
4350};
4351
4352#define SLAB_ATTR_RO(_name) \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004353 static struct slab_attribute _name##_attr = \
4354 __ATTR(_name, 0400, _name##_show, NULL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004355
4356#define SLAB_ATTR(_name) \
4357 static struct slab_attribute _name##_attr = \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004358 __ATTR(_name, 0600, _name##_show, _name##_store)
Christoph Lameter81819f02007-05-06 14:49:36 -07004359
Christoph Lameter81819f02007-05-06 14:49:36 -07004360static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4361{
4362 return sprintf(buf, "%d\n", s->size);
4363}
4364SLAB_ATTR_RO(slab_size);
4365
4366static ssize_t align_show(struct kmem_cache *s, char *buf)
4367{
4368 return sprintf(buf, "%d\n", s->align);
4369}
4370SLAB_ATTR_RO(align);
4371
4372static ssize_t object_size_show(struct kmem_cache *s, char *buf)
4373{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004374 return sprintf(buf, "%d\n", s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07004375}
4376SLAB_ATTR_RO(object_size);
4377
4378static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
4379{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004380 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004381}
4382SLAB_ATTR_RO(objs_per_slab);
4383
Christoph Lameter06b285d2008-04-14 19:11:41 +03004384static ssize_t order_store(struct kmem_cache *s,
4385 const char *buf, size_t length)
4386{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004387 unsigned long order;
4388 int err;
4389
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004390 err = kstrtoul(buf, 10, &order);
Christoph Lameter0121c6192008-04-29 16:11:12 -07004391 if (err)
4392 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004393
4394 if (order > slub_max_order || order < slub_min_order)
4395 return -EINVAL;
4396
4397 calculate_sizes(s, order);
4398 return length;
4399}
4400
Christoph Lameter81819f02007-05-06 14:49:36 -07004401static ssize_t order_show(struct kmem_cache *s, char *buf)
4402{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004403 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004404}
Christoph Lameter06b285d2008-04-14 19:11:41 +03004405SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07004406
David Rientjes73d342b2009-02-22 17:40:09 -08004407static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
4408{
4409 return sprintf(buf, "%lu\n", s->min_partial);
4410}
4411
4412static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
4413 size_t length)
4414{
4415 unsigned long min;
4416 int err;
4417
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004418 err = kstrtoul(buf, 10, &min);
David Rientjes73d342b2009-02-22 17:40:09 -08004419 if (err)
4420 return err;
4421
David Rientjesc0bdb232009-02-25 09:16:35 +02004422 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004423 return length;
4424}
4425SLAB_ATTR(min_partial);
4426
Christoph Lameter49e22582011-08-09 16:12:27 -05004427static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
4428{
4429 return sprintf(buf, "%u\n", s->cpu_partial);
4430}
4431
4432static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
4433 size_t length)
4434{
4435 unsigned long objects;
4436 int err;
4437
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004438 err = kstrtoul(buf, 10, &objects);
Christoph Lameter49e22582011-08-09 16:12:27 -05004439 if (err)
4440 return err;
Joonsoo Kim345c9052013-06-19 14:05:52 +09004441 if (objects && !kmem_cache_has_cpu_partial(s))
David Rientjes74ee4ef2012-01-09 13:19:45 -08004442 return -EINVAL;
Christoph Lameter49e22582011-08-09 16:12:27 -05004443
4444 s->cpu_partial = objects;
4445 flush_all(s);
4446 return length;
4447}
4448SLAB_ATTR(cpu_partial);
4449
Christoph Lameter81819f02007-05-06 14:49:36 -07004450static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4451{
Joe Perches62c70bc2011-01-13 15:45:52 -08004452 if (!s->ctor)
4453 return 0;
4454 return sprintf(buf, "%pS\n", s->ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004455}
4456SLAB_ATTR_RO(ctor);
4457
Christoph Lameter81819f02007-05-06 14:49:36 -07004458static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4459{
Gu Zheng4307c142014-08-06 16:04:51 -07004460 return sprintf(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004461}
4462SLAB_ATTR_RO(aliases);
4463
Christoph Lameter81819f02007-05-06 14:49:36 -07004464static ssize_t partial_show(struct kmem_cache *s, char *buf)
4465{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004466 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004467}
4468SLAB_ATTR_RO(partial);
4469
4470static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4471{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004472 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004473}
4474SLAB_ATTR_RO(cpu_slabs);
4475
4476static ssize_t objects_show(struct kmem_cache *s, char *buf)
4477{
Christoph Lameter205ab992008-04-14 19:11:40 +03004478 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004479}
4480SLAB_ATTR_RO(objects);
4481
Christoph Lameter205ab992008-04-14 19:11:40 +03004482static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4483{
4484 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4485}
4486SLAB_ATTR_RO(objects_partial);
4487
Christoph Lameter49e22582011-08-09 16:12:27 -05004488static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
4489{
4490 int objects = 0;
4491 int pages = 0;
4492 int cpu;
4493 int len;
4494
4495 for_each_online_cpu(cpu) {
4496 struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
4497
4498 if (page) {
4499 pages += page->pages;
4500 objects += page->pobjects;
4501 }
4502 }
4503
4504 len = sprintf(buf, "%d(%d)", objects, pages);
4505
4506#ifdef CONFIG_SMP
4507 for_each_online_cpu(cpu) {
4508 struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
4509
4510 if (page && len < PAGE_SIZE - 20)
4511 len += sprintf(buf + len, " C%d=%d(%d)", cpu,
4512 page->pobjects, page->pages);
4513 }
4514#endif
4515 return len + sprintf(buf + len, "\n");
4516}
4517SLAB_ATTR_RO(slabs_cpu_partial);
4518
Christoph Lameter81819f02007-05-06 14:49:36 -07004519static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4520{
4521 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4522}
4523
4524static ssize_t reclaim_account_store(struct kmem_cache *s,
4525 const char *buf, size_t length)
4526{
4527 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4528 if (buf[0] == '1')
4529 s->flags |= SLAB_RECLAIM_ACCOUNT;
4530 return length;
4531}
4532SLAB_ATTR(reclaim_account);
4533
4534static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4535{
Christoph Lameter5af60832007-05-06 14:49:56 -07004536 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004537}
4538SLAB_ATTR_RO(hwcache_align);
4539
4540#ifdef CONFIG_ZONE_DMA
4541static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4542{
4543 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4544}
4545SLAB_ATTR_RO(cache_dma);
4546#endif
4547
4548static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4549{
4550 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4551}
4552SLAB_ATTR_RO(destroy_by_rcu);
4553
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004554static ssize_t reserved_show(struct kmem_cache *s, char *buf)
4555{
4556 return sprintf(buf, "%d\n", s->reserved);
4557}
4558SLAB_ATTR_RO(reserved);
4559
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004560#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004561static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4562{
4563 return show_slab_objects(s, buf, SO_ALL);
4564}
4565SLAB_ATTR_RO(slabs);
4566
4567static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4568{
4569 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4570}
4571SLAB_ATTR_RO(total_objects);
4572
4573static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4574{
4575 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4576}
4577
4578static ssize_t sanity_checks_store(struct kmem_cache *s,
4579 const char *buf, size_t length)
4580{
4581 s->flags &= ~SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004582 if (buf[0] == '1') {
4583 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004584 s->flags |= SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004585 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004586 return length;
4587}
4588SLAB_ATTR(sanity_checks);
4589
4590static ssize_t trace_show(struct kmem_cache *s, char *buf)
4591{
4592 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4593}
4594
4595static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4596 size_t length)
4597{
Christoph Lameterc9e16132014-10-09 15:26:11 -07004598 /*
4599 * Tracing a merged cache is going to give confusing results
4600 * as well as cause other issues like converting a mergeable
4601 * cache into an umergeable one.
4602 */
4603 if (s->refcount > 1)
4604 return -EINVAL;
4605
Christoph Lametera5a84752010-10-05 13:57:27 -05004606 s->flags &= ~SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004607 if (buf[0] == '1') {
4608 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004609 s->flags |= SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004610 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004611 return length;
4612}
4613SLAB_ATTR(trace);
4614
Christoph Lameter81819f02007-05-06 14:49:36 -07004615static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4616{
4617 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4618}
4619
4620static ssize_t red_zone_store(struct kmem_cache *s,
4621 const char *buf, size_t length)
4622{
4623 if (any_slab_objects(s))
4624 return -EBUSY;
4625
4626 s->flags &= ~SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004627 if (buf[0] == '1') {
4628 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004629 s->flags |= SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004630 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004631 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004632 return length;
4633}
4634SLAB_ATTR(red_zone);
4635
4636static ssize_t poison_show(struct kmem_cache *s, char *buf)
4637{
4638 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4639}
4640
4641static ssize_t poison_store(struct kmem_cache *s,
4642 const char *buf, size_t length)
4643{
4644 if (any_slab_objects(s))
4645 return -EBUSY;
4646
4647 s->flags &= ~SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004648 if (buf[0] == '1') {
4649 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004650 s->flags |= SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004651 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004652 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004653 return length;
4654}
4655SLAB_ATTR(poison);
4656
4657static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4658{
4659 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4660}
4661
4662static ssize_t store_user_store(struct kmem_cache *s,
4663 const char *buf, size_t length)
4664{
4665 if (any_slab_objects(s))
4666 return -EBUSY;
4667
4668 s->flags &= ~SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004669 if (buf[0] == '1') {
4670 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004671 s->flags |= SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004672 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004673 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004674 return length;
4675}
4676SLAB_ATTR(store_user);
4677
Christoph Lameter53e15af2007-05-06 14:49:43 -07004678static ssize_t validate_show(struct kmem_cache *s, char *buf)
4679{
4680 return 0;
4681}
4682
4683static ssize_t validate_store(struct kmem_cache *s,
4684 const char *buf, size_t length)
4685{
Christoph Lameter434e2452007-07-17 04:03:30 -07004686 int ret = -EINVAL;
4687
4688 if (buf[0] == '1') {
4689 ret = validate_slab_cache(s);
4690 if (ret >= 0)
4691 ret = length;
4692 }
4693 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004694}
4695SLAB_ATTR(validate);
Christoph Lametera5a84752010-10-05 13:57:27 -05004696
4697static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
4698{
4699 if (!(s->flags & SLAB_STORE_USER))
4700 return -ENOSYS;
4701 return list_locations(s, buf, TRACK_ALLOC);
4702}
4703SLAB_ATTR_RO(alloc_calls);
4704
4705static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
4706{
4707 if (!(s->flags & SLAB_STORE_USER))
4708 return -ENOSYS;
4709 return list_locations(s, buf, TRACK_FREE);
4710}
4711SLAB_ATTR_RO(free_calls);
4712#endif /* CONFIG_SLUB_DEBUG */
4713
4714#ifdef CONFIG_FAILSLAB
4715static ssize_t failslab_show(struct kmem_cache *s, char *buf)
4716{
4717 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
4718}
4719
4720static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
4721 size_t length)
4722{
Christoph Lameterc9e16132014-10-09 15:26:11 -07004723 if (s->refcount > 1)
4724 return -EINVAL;
4725
Christoph Lametera5a84752010-10-05 13:57:27 -05004726 s->flags &= ~SLAB_FAILSLAB;
4727 if (buf[0] == '1')
4728 s->flags |= SLAB_FAILSLAB;
4729 return length;
4730}
4731SLAB_ATTR(failslab);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004732#endif
Christoph Lameter53e15af2007-05-06 14:49:43 -07004733
Christoph Lameter2086d262007-05-06 14:49:46 -07004734static ssize_t shrink_show(struct kmem_cache *s, char *buf)
4735{
4736 return 0;
4737}
4738
4739static ssize_t shrink_store(struct kmem_cache *s,
4740 const char *buf, size_t length)
4741{
Vladimir Davydov832f37f2015-02-12 14:59:41 -08004742 if (buf[0] == '1')
4743 kmem_cache_shrink(s);
4744 else
Christoph Lameter2086d262007-05-06 14:49:46 -07004745 return -EINVAL;
4746 return length;
4747}
4748SLAB_ATTR(shrink);
4749
Christoph Lameter81819f02007-05-06 14:49:36 -07004750#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004751static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07004752{
Christoph Lameter98246012008-01-07 23:20:26 -08004753 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07004754}
4755
Christoph Lameter98246012008-01-07 23:20:26 -08004756static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07004757 const char *buf, size_t length)
4758{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004759 unsigned long ratio;
4760 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004761
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004762 err = kstrtoul(buf, 10, &ratio);
Christoph Lameter0121c6192008-04-29 16:11:12 -07004763 if (err)
4764 return err;
4765
Christoph Lametere2cb96b2008-08-19 08:51:22 -05004766 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07004767 s->remote_node_defrag_ratio = ratio * 10;
4768
Christoph Lameter81819f02007-05-06 14:49:36 -07004769 return length;
4770}
Christoph Lameter98246012008-01-07 23:20:26 -08004771SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07004772#endif
4773
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004774#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004775static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4776{
4777 unsigned long sum = 0;
4778 int cpu;
4779 int len;
4780 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
4781
4782 if (!data)
4783 return -ENOMEM;
4784
4785 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004786 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004787
4788 data[cpu] = x;
4789 sum += x;
4790 }
4791
4792 len = sprintf(buf, "%lu", sum);
4793
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004794#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004795 for_each_online_cpu(cpu) {
4796 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004797 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004798 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03004799#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004800 kfree(data);
4801 return len + sprintf(buf + len, "\n");
4802}
4803
David Rientjes78eb00c2009-10-15 02:20:22 -07004804static void clear_stat(struct kmem_cache *s, enum stat_item si)
4805{
4806 int cpu;
4807
4808 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004809 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07004810}
4811
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004812#define STAT_ATTR(si, text) \
4813static ssize_t text##_show(struct kmem_cache *s, char *buf) \
4814{ \
4815 return show_stat(s, buf, si); \
4816} \
David Rientjes78eb00c2009-10-15 02:20:22 -07004817static ssize_t text##_store(struct kmem_cache *s, \
4818 const char *buf, size_t length) \
4819{ \
4820 if (buf[0] != '0') \
4821 return -EINVAL; \
4822 clear_stat(s, si); \
4823 return length; \
4824} \
4825SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004826
4827STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
4828STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
4829STAT_ATTR(FREE_FASTPATH, free_fastpath);
4830STAT_ATTR(FREE_SLOWPATH, free_slowpath);
4831STAT_ATTR(FREE_FROZEN, free_frozen);
4832STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
4833STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
4834STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4835STAT_ATTR(ALLOC_SLAB, alloc_slab);
4836STAT_ATTR(ALLOC_REFILL, alloc_refill);
Christoph Lametere36a2652011-06-01 12:25:57 -05004837STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004838STAT_ATTR(FREE_SLAB, free_slab);
4839STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4840STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4841STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
4842STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4843STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4844STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter03e404a2011-06-01 12:25:58 -05004845STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
Christoph Lameter65c33762008-04-14 19:11:40 +03004846STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameterb789ef52011-06-01 12:25:49 -05004847STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
4848STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
Christoph Lameter49e22582011-08-09 16:12:27 -05004849STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
4850STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
Alex Shi8028dce2012-02-03 23:34:56 +08004851STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
4852STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004853#endif
4854
Pekka Enberg06428782008-01-07 23:20:27 -08004855static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07004856 &slab_size_attr.attr,
4857 &object_size_attr.attr,
4858 &objs_per_slab_attr.attr,
4859 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08004860 &min_partial_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004861 &cpu_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004862 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03004863 &objects_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004864 &partial_attr.attr,
4865 &cpu_slabs_attr.attr,
4866 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004867 &aliases_attr.attr,
4868 &align_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004869 &hwcache_align_attr.attr,
4870 &reclaim_account_attr.attr,
4871 &destroy_by_rcu_attr.attr,
Christoph Lametera5a84752010-10-05 13:57:27 -05004872 &shrink_attr.attr,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004873 &reserved_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004874 &slabs_cpu_partial_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004875#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004876 &total_objects_attr.attr,
4877 &slabs_attr.attr,
4878 &sanity_checks_attr.attr,
4879 &trace_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004880 &red_zone_attr.attr,
4881 &poison_attr.attr,
4882 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07004883 &validate_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07004884 &alloc_calls_attr.attr,
4885 &free_calls_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004886#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004887#ifdef CONFIG_ZONE_DMA
4888 &cache_dma_attr.attr,
4889#endif
4890#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08004891 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07004892#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004893#ifdef CONFIG_SLUB_STATS
4894 &alloc_fastpath_attr.attr,
4895 &alloc_slowpath_attr.attr,
4896 &free_fastpath_attr.attr,
4897 &free_slowpath_attr.attr,
4898 &free_frozen_attr.attr,
4899 &free_add_partial_attr.attr,
4900 &free_remove_partial_attr.attr,
4901 &alloc_from_partial_attr.attr,
4902 &alloc_slab_attr.attr,
4903 &alloc_refill_attr.attr,
Christoph Lametere36a2652011-06-01 12:25:57 -05004904 &alloc_node_mismatch_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004905 &free_slab_attr.attr,
4906 &cpuslab_flush_attr.attr,
4907 &deactivate_full_attr.attr,
4908 &deactivate_empty_attr.attr,
4909 &deactivate_to_head_attr.attr,
4910 &deactivate_to_tail_attr.attr,
4911 &deactivate_remote_frees_attr.attr,
Christoph Lameter03e404a2011-06-01 12:25:58 -05004912 &deactivate_bypass_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03004913 &order_fallback_attr.attr,
Christoph Lameterb789ef52011-06-01 12:25:49 -05004914 &cmpxchg_double_fail_attr.attr,
4915 &cmpxchg_double_cpu_fail_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05004916 &cpu_partial_alloc_attr.attr,
4917 &cpu_partial_free_attr.attr,
Alex Shi8028dce2012-02-03 23:34:56 +08004918 &cpu_partial_node_attr.attr,
4919 &cpu_partial_drain_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08004920#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03004921#ifdef CONFIG_FAILSLAB
4922 &failslab_attr.attr,
4923#endif
4924
Christoph Lameter81819f02007-05-06 14:49:36 -07004925 NULL
4926};
4927
4928static struct attribute_group slab_attr_group = {
4929 .attrs = slab_attrs,
4930};
4931
4932static ssize_t slab_attr_show(struct kobject *kobj,
4933 struct attribute *attr,
4934 char *buf)
4935{
4936 struct slab_attribute *attribute;
4937 struct kmem_cache *s;
4938 int err;
4939
4940 attribute = to_slab_attr(attr);
4941 s = to_slab(kobj);
4942
4943 if (!attribute->show)
4944 return -EIO;
4945
4946 err = attribute->show(s, buf);
4947
4948 return err;
4949}
4950
4951static ssize_t slab_attr_store(struct kobject *kobj,
4952 struct attribute *attr,
4953 const char *buf, size_t len)
4954{
4955 struct slab_attribute *attribute;
4956 struct kmem_cache *s;
4957 int err;
4958
4959 attribute = to_slab_attr(attr);
4960 s = to_slab(kobj);
4961
4962 if (!attribute->store)
4963 return -EIO;
4964
4965 err = attribute->store(s, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08004966#ifdef CONFIG_MEMCG_KMEM
4967 if (slab_state >= FULL && err >= 0 && is_root_cache(s)) {
Vladimir Davydov426589f2015-02-12 14:59:23 -08004968 struct kmem_cache *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07004969
Glauber Costa107dab52012-12-18 14:23:05 -08004970 mutex_lock(&slab_mutex);
4971 if (s->max_attr_size < len)
4972 s->max_attr_size = len;
4973
Glauber Costaebe945c2012-12-18 14:23:10 -08004974 /*
4975 * This is a best effort propagation, so this function's return
4976 * value will be determined by the parent cache only. This is
4977 * basically because not all attributes will have a well
4978 * defined semantics for rollbacks - most of the actions will
4979 * have permanent effects.
4980 *
4981 * Returning the error value of any of the children that fail
4982 * is not 100 % defined, in the sense that users seeing the
4983 * error code won't be able to know anything about the state of
4984 * the cache.
4985 *
4986 * Only returning the error code for the parent cache at least
4987 * has well defined semantics. The cache being written to
4988 * directly either failed or succeeded, in which case we loop
4989 * through the descendants with best-effort propagation.
4990 */
Vladimir Davydov426589f2015-02-12 14:59:23 -08004991 for_each_memcg_cache(c, s)
4992 attribute->store(c, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08004993 mutex_unlock(&slab_mutex);
4994 }
4995#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004996 return err;
4997}
4998
Glauber Costa107dab52012-12-18 14:23:05 -08004999static void memcg_propagate_slab_attrs(struct kmem_cache *s)
5000{
5001#ifdef CONFIG_MEMCG_KMEM
5002 int i;
5003 char *buffer = NULL;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005004 struct kmem_cache *root_cache;
Glauber Costa107dab52012-12-18 14:23:05 -08005005
Vladimir Davydov93030d82014-05-06 12:49:59 -07005006 if (is_root_cache(s))
Glauber Costa107dab52012-12-18 14:23:05 -08005007 return;
5008
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08005009 root_cache = s->memcg_params.root_cache;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005010
Glauber Costa107dab52012-12-18 14:23:05 -08005011 /*
5012 * This mean this cache had no attribute written. Therefore, no point
5013 * in copying default values around
5014 */
Vladimir Davydov93030d82014-05-06 12:49:59 -07005015 if (!root_cache->max_attr_size)
Glauber Costa107dab52012-12-18 14:23:05 -08005016 return;
5017
5018 for (i = 0; i < ARRAY_SIZE(slab_attrs); i++) {
5019 char mbuf[64];
5020 char *buf;
5021 struct slab_attribute *attr = to_slab_attr(slab_attrs[i]);
5022
5023 if (!attr || !attr->store || !attr->show)
5024 continue;
5025
5026 /*
5027 * It is really bad that we have to allocate here, so we will
5028 * do it only as a fallback. If we actually allocate, though,
5029 * we can just use the allocated buffer until the end.
5030 *
5031 * Most of the slub attributes will tend to be very small in
5032 * size, but sysfs allows buffers up to a page, so they can
5033 * theoretically happen.
5034 */
5035 if (buffer)
5036 buf = buffer;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005037 else if (root_cache->max_attr_size < ARRAY_SIZE(mbuf))
Glauber Costa107dab52012-12-18 14:23:05 -08005038 buf = mbuf;
5039 else {
5040 buffer = (char *) get_zeroed_page(GFP_KERNEL);
5041 if (WARN_ON(!buffer))
5042 continue;
5043 buf = buffer;
5044 }
5045
Vladimir Davydov93030d82014-05-06 12:49:59 -07005046 attr->show(root_cache, buf);
Glauber Costa107dab52012-12-18 14:23:05 -08005047 attr->store(s, buf, strlen(buf));
5048 }
5049
5050 if (buffer)
5051 free_page((unsigned long)buffer);
5052#endif
5053}
5054
Christoph Lameter41a21282014-05-06 12:50:08 -07005055static void kmem_cache_release(struct kobject *k)
5056{
5057 slab_kmem_cache_release(to_slab(k));
5058}
5059
Emese Revfy52cf25d2010-01-19 02:58:23 +01005060static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005061 .show = slab_attr_show,
5062 .store = slab_attr_store,
5063};
5064
5065static struct kobj_type slab_ktype = {
5066 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter41a21282014-05-06 12:50:08 -07005067 .release = kmem_cache_release,
Christoph Lameter81819f02007-05-06 14:49:36 -07005068};
5069
5070static int uevent_filter(struct kset *kset, struct kobject *kobj)
5071{
5072 struct kobj_type *ktype = get_ktype(kobj);
5073
5074 if (ktype == &slab_ktype)
5075 return 1;
5076 return 0;
5077}
5078
Emese Revfy9cd43612009-12-31 14:52:51 +01005079static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005080 .filter = uevent_filter,
5081};
5082
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005083static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07005084
Vladimir Davydov9a417072014-04-07 15:39:31 -07005085static inline struct kset *cache_kset(struct kmem_cache *s)
5086{
5087#ifdef CONFIG_MEMCG_KMEM
5088 if (!is_root_cache(s))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08005089 return s->memcg_params.root_cache->memcg_kset;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005090#endif
5091 return slab_kset;
5092}
5093
Christoph Lameter81819f02007-05-06 14:49:36 -07005094#define ID_STR_LENGTH 64
5095
5096/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08005097 *
5098 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07005099 */
5100static char *create_unique_id(struct kmem_cache *s)
5101{
5102 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
5103 char *p = name;
5104
5105 BUG_ON(!name);
5106
5107 *p++ = ':';
5108 /*
5109 * First flags affecting slabcache operations. We will only
5110 * get here for aliasable slabs so we do not need to support
5111 * too many flags. The flags here must cover all flags that
5112 * are matched during merging to guarantee that the id is
5113 * unique.
5114 */
5115 if (s->flags & SLAB_CACHE_DMA)
5116 *p++ = 'd';
5117 if (s->flags & SLAB_RECLAIM_ACCOUNT)
5118 *p++ = 'a';
5119 if (s->flags & SLAB_DEBUG_FREE)
5120 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02005121 if (!(s->flags & SLAB_NOTRACK))
5122 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07005123 if (p != name + 1)
5124 *p++ = '-';
5125 p += sprintf(p, "%07d", s->size);
Glauber Costa2633d7a2012-12-18 14:22:34 -08005126
Christoph Lameter81819f02007-05-06 14:49:36 -07005127 BUG_ON(p > name + ID_STR_LENGTH - 1);
5128 return name;
5129}
5130
5131static int sysfs_slab_add(struct kmem_cache *s)
5132{
5133 int err;
5134 const char *name;
Christoph Lameter45530c42012-11-28 16:23:07 +00005135 int unmergeable = slab_unmergeable(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07005136
Christoph Lameter81819f02007-05-06 14:49:36 -07005137 if (unmergeable) {
5138 /*
5139 * Slabcache can never be merged so we can use the name proper.
5140 * This is typically the case for debug situations. In that
5141 * case we can catch duplicate names easily.
5142 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005143 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005144 name = s->name;
5145 } else {
5146 /*
5147 * Create a unique name for the slab as a target
5148 * for the symlinks.
5149 */
5150 name = create_unique_id(s);
5151 }
5152
Vladimir Davydov9a417072014-04-07 15:39:31 -07005153 s->kobj.kset = cache_kset(s);
Tetsuo Handa26e4f202014-01-04 16:32:31 +09005154 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
Dave Jones54b6a732014-04-07 15:39:32 -07005155 if (err)
5156 goto out_put_kobj;
Christoph Lameter81819f02007-05-06 14:49:36 -07005157
5158 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Dave Jones54b6a732014-04-07 15:39:32 -07005159 if (err)
5160 goto out_del_kobj;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005161
5162#ifdef CONFIG_MEMCG_KMEM
5163 if (is_root_cache(s)) {
5164 s->memcg_kset = kset_create_and_add("cgroup", NULL, &s->kobj);
5165 if (!s->memcg_kset) {
Dave Jones54b6a732014-04-07 15:39:32 -07005166 err = -ENOMEM;
5167 goto out_del_kobj;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005168 }
5169 }
5170#endif
5171
Christoph Lameter81819f02007-05-06 14:49:36 -07005172 kobject_uevent(&s->kobj, KOBJ_ADD);
5173 if (!unmergeable) {
5174 /* Setup first alias */
5175 sysfs_slab_alias(s, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005176 }
Dave Jones54b6a732014-04-07 15:39:32 -07005177out:
5178 if (!unmergeable)
5179 kfree(name);
5180 return err;
5181out_del_kobj:
5182 kobject_del(&s->kobj);
5183out_put_kobj:
5184 kobject_put(&s->kobj);
5185 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07005186}
5187
Christoph Lameter41a21282014-05-06 12:50:08 -07005188void sysfs_slab_remove(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07005189{
Christoph Lameter97d06602012-07-06 15:25:11 -05005190 if (slab_state < FULL)
Christoph Lameter2bce6482010-07-19 11:39:11 -05005191 /*
5192 * Sysfs has not been setup yet so no need to remove the
5193 * cache from sysfs.
5194 */
5195 return;
5196
Vladimir Davydov9a417072014-04-07 15:39:31 -07005197#ifdef CONFIG_MEMCG_KMEM
5198 kset_unregister(s->memcg_kset);
5199#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07005200 kobject_uevent(&s->kobj, KOBJ_REMOVE);
5201 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08005202 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07005203}
5204
5205/*
5206 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11005207 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07005208 */
5209struct saved_alias {
5210 struct kmem_cache *s;
5211 const char *name;
5212 struct saved_alias *next;
5213};
5214
Adrian Bunk5af328a2007-07-17 04:03:27 -07005215static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07005216
5217static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
5218{
5219 struct saved_alias *al;
5220
Christoph Lameter97d06602012-07-06 15:25:11 -05005221 if (slab_state == FULL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07005222 /*
5223 * If we have a leftover link then remove it.
5224 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005225 sysfs_remove_link(&slab_kset->kobj, name);
5226 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005227 }
5228
5229 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
5230 if (!al)
5231 return -ENOMEM;
5232
5233 al->s = s;
5234 al->name = name;
5235 al->next = alias_list;
5236 alias_list = al;
5237 return 0;
5238}
5239
5240static int __init slab_sysfs_init(void)
5241{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005242 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07005243 int err;
5244
Christoph Lameter18004c52012-07-06 15:25:12 -05005245 mutex_lock(&slab_mutex);
Christoph Lameter2bce6482010-07-19 11:39:11 -05005246
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08005247 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005248 if (!slab_kset) {
Christoph Lameter18004c52012-07-06 15:25:12 -05005249 mutex_unlock(&slab_mutex);
Fabian Frederickf9f58282014-06-04 16:06:34 -07005250 pr_err("Cannot register slab subsystem.\n");
Christoph Lameter81819f02007-05-06 14:49:36 -07005251 return -ENOSYS;
5252 }
5253
Christoph Lameter97d06602012-07-06 15:25:11 -05005254 slab_state = FULL;
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005255
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005256 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005257 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005258 if (err)
Fabian Frederickf9f58282014-06-04 16:06:34 -07005259 pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
5260 s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005261 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005262
5263 while (alias_list) {
5264 struct saved_alias *al = alias_list;
5265
5266 alias_list = alias_list->next;
5267 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005268 if (err)
Fabian Frederickf9f58282014-06-04 16:06:34 -07005269 pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
5270 al->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005271 kfree(al);
5272 }
5273
Christoph Lameter18004c52012-07-06 15:25:12 -05005274 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07005275 resiliency_test();
5276 return 0;
5277}
5278
5279__initcall(slab_sysfs_init);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005280#endif /* CONFIG_SYSFS */
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005281
5282/*
5283 * The /proc/slabinfo ABI
5284 */
Linus Torvalds158a9622008-01-02 13:04:48 -08005285#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04005286void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005287{
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005288 unsigned long nr_slabs = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03005289 unsigned long nr_objs = 0;
5290 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005291 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07005292 struct kmem_cache_node *n;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005293
Christoph Lameterfa45dc22014-08-06 16:04:09 -07005294 for_each_kmem_cache_node(s, node, n) {
Wanpeng Lic17fd132013-07-04 08:33:26 +08005295 nr_slabs += node_nr_slabs(n);
5296 nr_objs += node_nr_objs(n);
Christoph Lameter205ab992008-04-14 19:11:40 +03005297 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005298 }
5299
Glauber Costa0d7561c2012-10-19 18:20:27 +04005300 sinfo->active_objs = nr_objs - nr_free;
5301 sinfo->num_objs = nr_objs;
5302 sinfo->active_slabs = nr_slabs;
5303 sinfo->num_slabs = nr_slabs;
5304 sinfo->objects_per_slab = oo_objects(s->oo);
5305 sinfo->cache_order = oo_order(s->oo);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005306}
5307
Glauber Costa0d7561c2012-10-19 18:20:27 +04005308void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005309{
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005310}
5311
Glauber Costab7454ad2012-10-19 18:20:25 +04005312ssize_t slabinfo_write(struct file *file, const char __user *buffer,
5313 size_t count, loff_t *ppos)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005314{
Glauber Costab7454ad2012-10-19 18:20:25 +04005315 return -EIO;
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005316}
Linus Torvalds158a9622008-01-02 13:04:48 -08005317#endif /* CONFIG_SLABINFO */