blob: ca0c1f8a9f332f091b5dec610c0f287f0fd67eb1 [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 Kimdacbe1f2016-03-15 14:55:12 -0700127static inline void *fixup_red_left(struct kmem_cache *s, void *p)
128{
129 if (kmem_cache_debug(s) && s->flags & SLAB_RED_ZONE)
130 p += s->red_left_pad;
131
132 return p;
133}
134
Joonsoo Kim345c9052013-06-19 14:05:52 +0900135static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
136{
137#ifdef CONFIG_SLUB_CPU_PARTIAL
138 return !kmem_cache_debug(s);
139#else
140 return false;
141#endif
142}
143
Christoph Lameter81819f02007-05-06 14:49:36 -0700144/*
145 * Issues still to be resolved:
146 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700147 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
148 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700149 * - Variable sizing of the per node arrays
150 */
151
152/* Enable to test recovery from slab corruption on boot */
153#undef SLUB_RESILIENCY_TEST
154
Christoph Lameterb789ef52011-06-01 12:25:49 -0500155/* Enable to log cmpxchg failures */
156#undef SLUB_DEBUG_CMPXCHG
157
Christoph Lameter81819f02007-05-06 14:49:36 -0700158/*
Christoph Lameter2086d262007-05-06 14:49:46 -0700159 * Mininum number of partial slabs. These will be left on the partial
160 * lists even if they are empty. kmem_cache_shrink may reclaim them.
161 */
Christoph Lameter76be8952007-12-21 14:37:37 -0800162#define MIN_PARTIAL 5
Christoph Lametere95eed52007-05-06 14:49:44 -0700163
Christoph Lameter2086d262007-05-06 14:49:46 -0700164/*
165 * Maximum number of desirable partial slabs.
166 * The existence of more partial slabs makes kmem_cache_shrink
Zhi Yong Wu721ae222013-11-08 20:47:37 +0800167 * sort the partial list by the number of objects in use.
Christoph Lameter2086d262007-05-06 14:49:46 -0700168 */
169#define MAX_PARTIAL 10
170
Christoph Lameter81819f02007-05-06 14:49:36 -0700171#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
172 SLAB_POISON | SLAB_STORE_USER)
Christoph Lameter672bba32007-05-09 02:32:39 -0700173
Christoph Lameter81819f02007-05-06 14:49:36 -0700174/*
David Rientjes3de472132009-07-27 18:30:35 -0700175 * Debugging flags that require metadata to be stored in the slab. These get
176 * disabled when slub_debug=O is used and a cache's min order increases with
177 * metadata.
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700178 */
David Rientjes3de472132009-07-27 18:30:35 -0700179#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700180
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400181#define OO_SHIFT 16
182#define OO_MASK ((1 << OO_SHIFT) - 1)
Christoph Lameter50d5c412011-06-01 12:25:45 -0500183#define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400184
Christoph Lameter81819f02007-05-06 14:49:36 -0700185/* Internal SLUB flags */
Christoph Lameterf90ec392010-07-09 14:07:11 -0500186#define __OBJECT_POISON 0x80000000UL /* Poison object */
Christoph Lameterb789ef52011-06-01 12:25:49 -0500187#define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
Christoph Lameter81819f02007-05-06 14:49:36 -0700188
Christoph Lameter81819f02007-05-06 14:49:36 -0700189#ifdef CONFIG_SMP
190static struct notifier_block slab_notifier;
191#endif
192
Christoph Lameter02cbc872007-05-09 02:32:43 -0700193/*
194 * Tracking user of a slab.
195 */
Ben Greeard6543e32011-07-07 11:36:36 -0700196#define TRACK_ADDRS_COUNT 16
Christoph Lameter02cbc872007-05-09 02:32:43 -0700197struct track {
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300198 unsigned long addr; /* Called from address */
Ben Greeard6543e32011-07-07 11:36:36 -0700199#ifdef CONFIG_STACKTRACE
200 unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
201#endif
Christoph Lameter02cbc872007-05-09 02:32:43 -0700202 int cpu; /* Was running on cpu */
203 int pid; /* Pid context */
204 unsigned long when; /* When did the operation occur */
205};
206
207enum track_item { TRACK_ALLOC, TRACK_FREE };
208
Christoph Lameterab4d5ed2010-10-05 13:57:26 -0500209#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -0700210static int sysfs_slab_add(struct kmem_cache *);
211static int sysfs_slab_alias(struct kmem_cache *, const char *);
Glauber Costa107dab52012-12-18 14:23:05 -0800212static void memcg_propagate_slab_attrs(struct kmem_cache *s);
Christoph Lameter81819f02007-05-06 14:49:36 -0700213#else
Christoph Lameter0c710012007-07-17 04:03:24 -0700214static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
215static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
216 { return 0; }
Glauber Costa107dab52012-12-18 14:23:05 -0800217static inline void memcg_propagate_slab_attrs(struct kmem_cache *s) { }
Christoph Lameter81819f02007-05-06 14:49:36 -0700218#endif
219
Christoph Lameter4fdccdf2011-03-22 13:35:00 -0500220static inline void stat(const struct kmem_cache *s, enum stat_item si)
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800221{
222#ifdef CONFIG_SLUB_STATS
Christoph Lameter88da03a2014-04-07 15:39:42 -0700223 /*
224 * The rmw is racy on a preemptible kernel but this is acceptable, so
225 * avoid this_cpu_add()'s irq-disable overhead.
226 */
227 raw_cpu_inc(s->cpu_slab->stat[si]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -0800228#endif
229}
230
Christoph Lameter81819f02007-05-06 14:49:36 -0700231/********************************************************************
232 * Core slab cache functions
233 *******************************************************************/
234
Christoph Lameter7656c722007-05-09 02:32:40 -0700235static inline void *get_freepointer(struct kmem_cache *s, void *object)
236{
237 return *(void **)(object + s->offset);
238}
239
Eric Dumazet0ad95002011-12-16 16:25:34 +0100240static void prefetch_freepointer(const struct kmem_cache *s, void *object)
241{
242 prefetch(object + s->offset);
243}
244
Christoph Lameter1393d9a2011-05-16 15:26:08 -0500245static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
246{
247 void *p;
248
249#ifdef CONFIG_DEBUG_PAGEALLOC
250 probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
251#else
252 p = get_freepointer(s, object);
253#endif
254 return p;
255}
256
Christoph Lameter7656c722007-05-09 02:32:40 -0700257static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
258{
259 *(void **)(object + s->offset) = fp;
260}
261
262/* Loop over all objects in a slab */
Christoph Lameter224a88b2008-04-14 19:11:31 +0300263#define for_each_object(__p, __s, __addr, __objects) \
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700264 for (__p = fixup_red_left(__s, __addr); \
265 __p < (__addr) + (__objects) * (__s)->size; \
266 __p += (__s)->size)
Christoph Lameter7656c722007-05-09 02:32:40 -0700267
Wei Yang54266642014-08-06 16:04:42 -0700268#define for_each_object_idx(__p, __idx, __s, __addr, __objects) \
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700269 for (__p = fixup_red_left(__s, __addr), __idx = 1; \
270 __idx <= __objects; \
271 __p += (__s)->size, __idx++)
Wei Yang54266642014-08-06 16:04:42 -0700272
Christoph Lameter7656c722007-05-09 02:32:40 -0700273/* Determine object index from a given position */
274static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
275{
276 return (p - addr) / s->size;
277}
278
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100279static inline size_t slab_ksize(const struct kmem_cache *s)
280{
281#ifdef CONFIG_SLUB_DEBUG
282 /*
283 * Debugging requires use of the padding between object
284 * and whatever may come after it.
285 */
286 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500287 return s->object_size;
Mariusz Kozlowskid71f6062011-02-26 20:10:26 +0100288
289#endif
290 /*
291 * If we have the need to store the freelist pointer
292 * back there or track user information then we can
293 * only use the space before that information.
294 */
295 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
296 return s->inuse;
297 /*
298 * Else we can use all the padding etc for the allocation
299 */
300 return s->size;
301}
302
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800303static inline int order_objects(int order, unsigned long size, int reserved)
304{
305 return ((PAGE_SIZE << order) - reserved) / size;
306}
307
Christoph Lameter834f3d12008-04-14 19:11:31 +0300308static inline struct kmem_cache_order_objects oo_make(int order,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800309 unsigned long size, int reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300310{
311 struct kmem_cache_order_objects x = {
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800312 (order << OO_SHIFT) + order_objects(order, size, reserved)
Christoph Lameter834f3d12008-04-14 19:11:31 +0300313 };
314
315 return x;
316}
317
318static inline int oo_order(struct kmem_cache_order_objects x)
319{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400320 return x.x >> OO_SHIFT;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300321}
322
323static inline int oo_objects(struct kmem_cache_order_objects x)
324{
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400325 return x.x & OO_MASK;
Christoph Lameter834f3d12008-04-14 19:11:31 +0300326}
327
Christoph Lameter881db7f2011-06-01 12:25:53 -0500328/*
329 * Per slab locking using the pagelock
330 */
331static __always_inline void slab_lock(struct page *page)
332{
333 bit_spin_lock(PG_locked, &page->flags);
334}
335
336static __always_inline void slab_unlock(struct page *page)
337{
338 __bit_spin_unlock(PG_locked, &page->flags);
339}
340
Dave Hansena0320862014-01-30 15:46:09 -0800341static inline void set_page_slub_counters(struct page *page, unsigned long counters_new)
342{
343 struct page tmp;
344 tmp.counters = counters_new;
345 /*
346 * page->counters can cover frozen/inuse/objects as well
347 * as page->_count. If we assign to ->counters directly
348 * we run the risk of losing updates to page->_count, so
349 * be careful and only assign to the fields we need.
350 */
351 page->frozen = tmp.frozen;
352 page->inuse = tmp.inuse;
353 page->objects = tmp.objects;
354}
355
Christoph Lameter1d071712011-07-14 12:49:12 -0500356/* Interrupts must be disabled (for the fallback code to work right) */
357static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
Christoph Lameterb789ef52011-06-01 12:25:49 -0500358 void *freelist_old, unsigned long counters_old,
359 void *freelist_new, unsigned long counters_new,
360 const char *n)
361{
Christoph Lameter1d071712011-07-14 12:49:12 -0500362 VM_BUG_ON(!irqs_disabled());
Heiko Carstens25654092012-01-12 17:17:33 -0800363#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
364 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -0500365 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000366 if (cmpxchg_double(&page->freelist, &page->counters,
Dan Carpenter0aa9a132014-08-06 16:04:48 -0700367 freelist_old, counters_old,
368 freelist_new, counters_new))
Joe Perches6f6528a2015-04-14 15:44:31 -0700369 return true;
Christoph Lameterb789ef52011-06-01 12:25:49 -0500370 } else
371#endif
372 {
Christoph Lameter881db7f2011-06-01 12:25:53 -0500373 slab_lock(page);
Chen Gangd0e0ac92013-07-15 09:05:29 +0800374 if (page->freelist == freelist_old &&
375 page->counters == counters_old) {
Christoph Lameterb789ef52011-06-01 12:25:49 -0500376 page->freelist = freelist_new;
Dave Hansena0320862014-01-30 15:46:09 -0800377 set_page_slub_counters(page, counters_new);
Christoph Lameter881db7f2011-06-01 12:25:53 -0500378 slab_unlock(page);
Joe Perches6f6528a2015-04-14 15:44:31 -0700379 return true;
Christoph Lameterb789ef52011-06-01 12:25:49 -0500380 }
Christoph Lameter881db7f2011-06-01 12:25:53 -0500381 slab_unlock(page);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500382 }
383
384 cpu_relax();
385 stat(s, CMPXCHG_DOUBLE_FAIL);
386
387#ifdef SLUB_DEBUG_CMPXCHG
Fabian Frederickf9f58282014-06-04 16:06:34 -0700388 pr_info("%s %s: cmpxchg double redo ", n, s->name);
Christoph Lameterb789ef52011-06-01 12:25:49 -0500389#endif
390
Joe Perches6f6528a2015-04-14 15:44:31 -0700391 return false;
Christoph Lameterb789ef52011-06-01 12:25:49 -0500392}
393
Christoph Lameter1d071712011-07-14 12:49:12 -0500394static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
395 void *freelist_old, unsigned long counters_old,
396 void *freelist_new, unsigned long counters_new,
397 const char *n)
398{
Heiko Carstens25654092012-01-12 17:17:33 -0800399#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
400 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameter1d071712011-07-14 12:49:12 -0500401 if (s->flags & __CMPXCHG_DOUBLE) {
Jan Beulichcdcd6292012-01-02 17:02:18 +0000402 if (cmpxchg_double(&page->freelist, &page->counters,
Dan Carpenter0aa9a132014-08-06 16:04:48 -0700403 freelist_old, counters_old,
404 freelist_new, counters_new))
Joe Perches6f6528a2015-04-14 15:44:31 -0700405 return true;
Christoph Lameter1d071712011-07-14 12:49:12 -0500406 } else
407#endif
408 {
409 unsigned long flags;
410
411 local_irq_save(flags);
412 slab_lock(page);
Chen Gangd0e0ac92013-07-15 09:05:29 +0800413 if (page->freelist == freelist_old &&
414 page->counters == counters_old) {
Christoph Lameter1d071712011-07-14 12:49:12 -0500415 page->freelist = freelist_new;
Dave Hansena0320862014-01-30 15:46:09 -0800416 set_page_slub_counters(page, counters_new);
Christoph Lameter1d071712011-07-14 12:49:12 -0500417 slab_unlock(page);
418 local_irq_restore(flags);
Joe Perches6f6528a2015-04-14 15:44:31 -0700419 return true;
Christoph Lameter1d071712011-07-14 12:49:12 -0500420 }
421 slab_unlock(page);
422 local_irq_restore(flags);
423 }
424
425 cpu_relax();
426 stat(s, CMPXCHG_DOUBLE_FAIL);
427
428#ifdef SLUB_DEBUG_CMPXCHG
Fabian Frederickf9f58282014-06-04 16:06:34 -0700429 pr_info("%s %s: cmpxchg double redo ", n, s->name);
Christoph Lameter1d071712011-07-14 12:49:12 -0500430#endif
431
Joe Perches6f6528a2015-04-14 15:44:31 -0700432 return false;
Christoph Lameter1d071712011-07-14 12:49:12 -0500433}
434
Christoph Lameter41ecc552007-05-09 02:32:44 -0700435#ifdef CONFIG_SLUB_DEBUG
436/*
Christoph Lameter5f80b132011-04-15 14:48:13 -0500437 * Determine a map of object in use on a page.
438 *
Christoph Lameter881db7f2011-06-01 12:25:53 -0500439 * Node listlock must be held to guarantee that the page does
Christoph Lameter5f80b132011-04-15 14:48:13 -0500440 * not vanish from under us.
441 */
442static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
443{
444 void *p;
445 void *addr = page_address(page);
446
447 for (p = page->freelist; p; p = get_freepointer(s, p))
448 set_bit(slab_index(p, s, addr), map);
449}
450
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700451static inline int size_from_object(struct kmem_cache *s)
452{
453 if (s->flags & SLAB_RED_ZONE)
454 return s->size - s->red_left_pad;
455
456 return s->size;
457}
458
459static inline void *restore_red_left(struct kmem_cache *s, void *p)
460{
461 if (s->flags & SLAB_RED_ZONE)
462 p -= s->red_left_pad;
463
464 return p;
465}
466
Christoph Lameter41ecc552007-05-09 02:32:44 -0700467/*
468 * Debug settings:
469 */
Andrey Ryabinin89d3c872015-11-05 18:51:23 -0800470#if defined(CONFIG_SLUB_DEBUG_ON)
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700471static int slub_debug = DEBUG_DEFAULT_FLAGS;
Andrey Ryabinin89d3c872015-11-05 18:51:23 -0800472#elif defined(CONFIG_KASAN)
473static int slub_debug = SLAB_STORE_USER;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700474#else
Christoph Lameter41ecc552007-05-09 02:32:44 -0700475static int slub_debug;
Christoph Lameterf0630ff2007-07-15 23:38:14 -0700476#endif
Christoph Lameter41ecc552007-05-09 02:32:44 -0700477
478static char *slub_debug_slabs;
David Rientjesfa5ec8a2009-07-07 00:14:14 -0700479static int disable_higher_order_debug;
Christoph Lameter41ecc552007-05-09 02:32:44 -0700480
Christoph Lameter7656c722007-05-09 02:32:40 -0700481/*
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800482 * slub is about to manipulate internal object metadata. This memory lies
483 * outside the range of the allocated object, so accessing it would normally
484 * be reported by kasan as a bounds error. metadata_access_enable() is used
485 * to tell kasan that these accesses are OK.
486 */
487static inline void metadata_access_enable(void)
488{
489 kasan_disable_current();
490}
491
492static inline void metadata_access_disable(void)
493{
494 kasan_enable_current();
495}
496
497/*
Christoph Lameter81819f02007-05-06 14:49:36 -0700498 * Object debugging
499 */
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700500
501/* Verify that a pointer has an address that is valid within a slab page */
502static inline int check_valid_pointer(struct kmem_cache *s,
503 struct page *page, void *object)
504{
505 void *base;
506
507 if (!object)
508 return 1;
509
510 base = page_address(page);
511 object = restore_red_left(s, object);
512 if (object < base || object >= base + page->objects * s->size ||
513 (object - base) % s->size) {
514 return 0;
515 }
516
517 return 1;
518}
519
Christoph Lameter81819f02007-05-06 14:49:36 -0700520static void print_section(char *text, u8 *addr, unsigned int length)
521{
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800522 metadata_access_enable();
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200523 print_hex_dump(KERN_ERR, text, DUMP_PREFIX_ADDRESS, 16, 1, addr,
524 length, 1);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800525 metadata_access_disable();
Christoph Lameter81819f02007-05-06 14:49:36 -0700526}
527
Christoph Lameter81819f02007-05-06 14:49:36 -0700528static struct track *get_track(struct kmem_cache *s, void *object,
529 enum track_item alloc)
530{
531 struct track *p;
532
533 if (s->offset)
534 p = object + s->offset + sizeof(void *);
535 else
536 p = object + s->inuse;
537
538 return p + alloc;
539}
540
541static void set_track(struct kmem_cache *s, void *object,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300542 enum track_item alloc, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -0700543{
Akinobu Mita1a00df42009-03-07 00:36:21 +0900544 struct track *p = get_track(s, object, alloc);
Christoph Lameter81819f02007-05-06 14:49:36 -0700545
Christoph Lameter81819f02007-05-06 14:49:36 -0700546 if (addr) {
Ben Greeard6543e32011-07-07 11:36:36 -0700547#ifdef CONFIG_STACKTRACE
548 struct stack_trace trace;
549 int i;
550
551 trace.nr_entries = 0;
552 trace.max_entries = TRACK_ADDRS_COUNT;
553 trace.entries = p->addrs;
554 trace.skip = 3;
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800555 metadata_access_enable();
Ben Greeard6543e32011-07-07 11:36:36 -0700556 save_stack_trace(&trace);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800557 metadata_access_disable();
Ben Greeard6543e32011-07-07 11:36:36 -0700558
559 /* See rant in lockdep.c */
560 if (trace.nr_entries != 0 &&
561 trace.entries[trace.nr_entries - 1] == ULONG_MAX)
562 trace.nr_entries--;
563
564 for (i = trace.nr_entries; i < TRACK_ADDRS_COUNT; i++)
565 p->addrs[i] = 0;
566#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700567 p->addr = addr;
568 p->cpu = smp_processor_id();
Alexey Dobriyan88e4ccf2008-06-23 02:58:37 +0400569 p->pid = current->pid;
Christoph Lameter81819f02007-05-06 14:49:36 -0700570 p->when = jiffies;
571 } else
572 memset(p, 0, sizeof(struct track));
573}
574
Christoph Lameter81819f02007-05-06 14:49:36 -0700575static void init_tracking(struct kmem_cache *s, void *object)
576{
Christoph Lameter24922682007-07-17 04:03:18 -0700577 if (!(s->flags & SLAB_STORE_USER))
578 return;
579
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +0300580 set_track(s, object, TRACK_FREE, 0UL);
581 set_track(s, object, TRACK_ALLOC, 0UL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700582}
583
584static void print_track(const char *s, struct track *t)
585{
586 if (!t->addr)
587 return;
588
Fabian Frederickf9f58282014-06-04 16:06:34 -0700589 pr_err("INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
590 s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
Ben Greeard6543e32011-07-07 11:36:36 -0700591#ifdef CONFIG_STACKTRACE
592 {
593 int i;
594 for (i = 0; i < TRACK_ADDRS_COUNT; i++)
595 if (t->addrs[i])
Fabian Frederickf9f58282014-06-04 16:06:34 -0700596 pr_err("\t%pS\n", (void *)t->addrs[i]);
Ben Greeard6543e32011-07-07 11:36:36 -0700597 else
598 break;
599 }
600#endif
Christoph Lameter81819f02007-05-06 14:49:36 -0700601}
602
Christoph Lameter24922682007-07-17 04:03:18 -0700603static void print_tracking(struct kmem_cache *s, void *object)
604{
605 if (!(s->flags & SLAB_STORE_USER))
606 return;
607
608 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
609 print_track("Freed", get_track(s, object, TRACK_FREE));
610}
611
612static void print_page_info(struct page *page)
613{
Fabian Frederickf9f58282014-06-04 16:06:34 -0700614 pr_err("INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
Chen Gangd0e0ac92013-07-15 09:05:29 +0800615 page, page->objects, page->inuse, page->freelist, page->flags);
Christoph Lameter24922682007-07-17 04:03:18 -0700616
617}
618
619static void slab_bug(struct kmem_cache *s, char *fmt, ...)
620{
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700621 struct va_format vaf;
Christoph Lameter24922682007-07-17 04:03:18 -0700622 va_list args;
Christoph Lameter24922682007-07-17 04:03:18 -0700623
624 va_start(args, fmt);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700625 vaf.fmt = fmt;
626 vaf.va = &args;
Fabian Frederickf9f58282014-06-04 16:06:34 -0700627 pr_err("=============================================================================\n");
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700628 pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf);
Fabian Frederickf9f58282014-06-04 16:06:34 -0700629 pr_err("-----------------------------------------------------------------------------\n\n");
Dave Jones645df232012-09-18 15:54:12 -0400630
Rusty Russell373d4d02013-01-21 17:17:39 +1030631 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700632 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700633}
634
635static void slab_fix(struct kmem_cache *s, char *fmt, ...)
636{
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700637 struct va_format vaf;
Christoph Lameter24922682007-07-17 04:03:18 -0700638 va_list args;
Christoph Lameter24922682007-07-17 04:03:18 -0700639
640 va_start(args, fmt);
Fabian Frederickecc42fb2014-06-04 16:06:35 -0700641 vaf.fmt = fmt;
642 vaf.va = &args;
643 pr_err("FIX %s: %pV\n", s->name, &vaf);
Christoph Lameter24922682007-07-17 04:03:18 -0700644 va_end(args);
Christoph Lameter24922682007-07-17 04:03:18 -0700645}
646
647static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
Christoph Lameter81819f02007-05-06 14:49:36 -0700648{
649 unsigned int off; /* Offset of last byte */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800650 u8 *addr = page_address(page);
Christoph Lameter24922682007-07-17 04:03:18 -0700651
652 print_tracking(s, p);
653
654 print_page_info(page);
655
Fabian Frederickf9f58282014-06-04 16:06:34 -0700656 pr_err("INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
657 p, p - addr, get_freepointer(s, p));
Christoph Lameter24922682007-07-17 04:03:18 -0700658
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700659 if (s->flags & SLAB_RED_ZONE)
660 print_section("Redzone ", p - s->red_left_pad, s->red_left_pad);
661 else if (p > addr + 16)
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200662 print_section("Bytes b4 ", p - 16, 16);
Christoph Lameter24922682007-07-17 04:03:18 -0700663
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500664 print_section("Object ", p, min_t(unsigned long, s->object_size,
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200665 PAGE_SIZE));
Christoph Lameter81819f02007-05-06 14:49:36 -0700666 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500667 print_section("Redzone ", p + s->object_size,
668 s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700669
Christoph Lameter81819f02007-05-06 14:49:36 -0700670 if (s->offset)
671 off = s->offset + sizeof(void *);
672 else
673 off = s->inuse;
674
Christoph Lameter24922682007-07-17 04:03:18 -0700675 if (s->flags & SLAB_STORE_USER)
Christoph Lameter81819f02007-05-06 14:49:36 -0700676 off += 2 * sizeof(struct track);
Christoph Lameter81819f02007-05-06 14:49:36 -0700677
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700678 if (off != size_from_object(s))
Christoph Lameter81819f02007-05-06 14:49:36 -0700679 /* Beginning of the filler is the free pointer */
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700680 print_section("Padding ", p + off, size_from_object(s) - off);
Christoph Lameter24922682007-07-17 04:03:18 -0700681
682 dump_stack();
Christoph Lameter81819f02007-05-06 14:49:36 -0700683}
684
Andrey Ryabinin75c66de2015-02-13 14:39:35 -0800685void object_err(struct kmem_cache *s, struct page *page,
Christoph Lameter81819f02007-05-06 14:49:36 -0700686 u8 *object, char *reason)
687{
Christoph Lameter3dc50632008-04-23 12:28:01 -0700688 slab_bug(s, "%s", reason);
Christoph Lameter24922682007-07-17 04:03:18 -0700689 print_trailer(s, page, object);
Christoph Lameter81819f02007-05-06 14:49:36 -0700690}
691
Mathieu Malaterre32316132018-06-07 17:05:17 -0700692static __printf(3, 4) void slab_err(struct kmem_cache *s, struct page *page,
Chen Gangd0e0ac92013-07-15 09:05:29 +0800693 const char *fmt, ...)
Christoph Lameter81819f02007-05-06 14:49:36 -0700694{
695 va_list args;
696 char buf[100];
697
Christoph Lameter24922682007-07-17 04:03:18 -0700698 va_start(args, fmt);
699 vsnprintf(buf, sizeof(buf), fmt, args);
Christoph Lameter81819f02007-05-06 14:49:36 -0700700 va_end(args);
Christoph Lameter3dc50632008-04-23 12:28:01 -0700701 slab_bug(s, "%s", buf);
Christoph Lameter24922682007-07-17 04:03:18 -0700702 print_page_info(page);
Christoph Lameter81819f02007-05-06 14:49:36 -0700703 dump_stack();
704}
705
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500706static void init_object(struct kmem_cache *s, void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700707{
708 u8 *p = object;
709
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700710 if (s->flags & SLAB_RED_ZONE)
711 memset(p - s->red_left_pad, val, s->red_left_pad);
712
Christoph Lameter81819f02007-05-06 14:49:36 -0700713 if (s->flags & __OBJECT_POISON) {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500714 memset(p, POISON_FREE, s->object_size - 1);
715 p[s->object_size - 1] = POISON_END;
Christoph Lameter81819f02007-05-06 14:49:36 -0700716 }
717
718 if (s->flags & SLAB_RED_ZONE)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500719 memset(p + s->object_size, val, s->inuse - s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700720}
721
Christoph Lameter24922682007-07-17 04:03:18 -0700722static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
723 void *from, void *to)
724{
725 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
726 memset(from, data, to - from);
727}
728
729static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
730 u8 *object, char *what,
Pekka Enberg06428782008-01-07 23:20:27 -0800731 u8 *start, unsigned int value, unsigned int bytes)
Christoph Lameter24922682007-07-17 04:03:18 -0700732{
733 u8 *fault;
734 u8 *end;
735
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800736 metadata_access_enable();
Akinobu Mita798248202011-10-31 17:08:07 -0700737 fault = memchr_inv(start, value, bytes);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800738 metadata_access_disable();
Christoph Lameter24922682007-07-17 04:03:18 -0700739 if (!fault)
740 return 1;
741
742 end = start + bytes;
743 while (end > fault && end[-1] == value)
744 end--;
745
746 slab_bug(s, "%s overwritten", what);
Fabian Frederickf9f58282014-06-04 16:06:34 -0700747 pr_err("INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
Christoph Lameter24922682007-07-17 04:03:18 -0700748 fault, end - 1, fault[0], value);
749 print_trailer(s, page, object);
750
751 restore_bytes(s, what, value, fault, end);
752 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700753}
754
Christoph Lameter81819f02007-05-06 14:49:36 -0700755/*
756 * Object layout:
757 *
758 * object address
759 * Bytes of the object to be managed.
760 * If the freepointer may overlay the object then the free
761 * pointer is the first word of the object.
Christoph Lameter672bba32007-05-09 02:32:39 -0700762 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700763 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
764 * 0xa5 (POISON_END)
765 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500766 * object + s->object_size
Christoph Lameter81819f02007-05-06 14:49:36 -0700767 * Padding to reach word boundary. This is also used for Redzoning.
Christoph Lameter672bba32007-05-09 02:32:39 -0700768 * Padding is extended by another word if Redzoning is enabled and
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500769 * object_size == inuse.
Christoph Lameter672bba32007-05-09 02:32:39 -0700770 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700771 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
772 * 0xcc (RED_ACTIVE) for objects in use.
773 *
774 * object + s->inuse
Christoph Lameter672bba32007-05-09 02:32:39 -0700775 * Meta data starts here.
776 *
Christoph Lameter81819f02007-05-06 14:49:36 -0700777 * A. Free pointer (if we cannot overwrite object on free)
778 * B. Tracking data for SLAB_STORE_USER
Christoph Lameter672bba32007-05-09 02:32:39 -0700779 * C. Padding to reach required alignment boundary or at mininum
Christoph Lameter6446faa2008-02-15 23:45:26 -0800780 * one word if debugging is on to be able to detect writes
Christoph Lameter672bba32007-05-09 02:32:39 -0700781 * before the word boundary.
782 *
783 * Padding is done using 0x5a (POISON_INUSE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700784 *
785 * object + s->size
Christoph Lameter672bba32007-05-09 02:32:39 -0700786 * Nothing is used beyond s->size.
Christoph Lameter81819f02007-05-06 14:49:36 -0700787 *
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500788 * If slabcaches are merged then the object_size and inuse boundaries are mostly
Christoph Lameter672bba32007-05-09 02:32:39 -0700789 * ignored. And therefore no slab options that rely on these boundaries
Christoph Lameter81819f02007-05-06 14:49:36 -0700790 * may be used with merged slabcaches.
791 */
792
Christoph Lameter81819f02007-05-06 14:49:36 -0700793static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
794{
795 unsigned long off = s->inuse; /* The end of info */
796
797 if (s->offset)
798 /* Freepointer is placed after the object. */
799 off += sizeof(void *);
800
801 if (s->flags & SLAB_STORE_USER)
802 /* We also have user information there */
803 off += 2 * sizeof(struct track);
804
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700805 if (size_from_object(s) == off)
Christoph Lameter81819f02007-05-06 14:49:36 -0700806 return 1;
807
Christoph Lameter24922682007-07-17 04:03:18 -0700808 return check_bytes_and_report(s, page, p, "Object padding",
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700809 p + off, POISON_INUSE, size_from_object(s) - off);
Christoph Lameter81819f02007-05-06 14:49:36 -0700810}
811
Christoph Lameter39b26462008-04-14 19:11:30 +0300812/* Check the pad bytes at the end of a slab page */
Christoph Lameter81819f02007-05-06 14:49:36 -0700813static int slab_pad_check(struct kmem_cache *s, struct page *page)
814{
Christoph Lameter24922682007-07-17 04:03:18 -0700815 u8 *start;
816 u8 *fault;
817 u8 *end;
818 int length;
819 int remainder;
Christoph Lameter81819f02007-05-06 14:49:36 -0700820
821 if (!(s->flags & SLAB_POISON))
822 return 1;
823
Christoph Lametera973e9d2008-03-01 13:40:44 -0800824 start = page_address(page);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800825 length = (PAGE_SIZE << compound_order(page)) - s->reserved;
Christoph Lameter39b26462008-04-14 19:11:30 +0300826 end = start + length;
827 remainder = length % s->size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700828 if (!remainder)
829 return 1;
830
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800831 metadata_access_enable();
Akinobu Mita798248202011-10-31 17:08:07 -0700832 fault = memchr_inv(end - remainder, POISON_INUSE, remainder);
Andrey Ryabinina79316c2015-02-13 14:39:38 -0800833 metadata_access_disable();
Christoph Lameter24922682007-07-17 04:03:18 -0700834 if (!fault)
835 return 1;
836 while (end > fault && end[-1] == POISON_INUSE)
837 end--;
838
839 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
Sebastian Andrzej Siewiorffc79d22011-07-29 14:10:20 +0200840 print_section("Padding ", end - remainder, remainder);
Christoph Lameter24922682007-07-17 04:03:18 -0700841
Eric Dumazet8a3d2712009-09-03 16:08:06 +0200842 restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
Christoph Lameter24922682007-07-17 04:03:18 -0700843 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700844}
845
846static int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500847 void *object, u8 val)
Christoph Lameter81819f02007-05-06 14:49:36 -0700848{
849 u8 *p = object;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500850 u8 *endobject = object + s->object_size;
Christoph Lameter81819f02007-05-06 14:49:36 -0700851
852 if (s->flags & SLAB_RED_ZONE) {
Christoph Lameter24922682007-07-17 04:03:18 -0700853 if (!check_bytes_and_report(s, page, object, "Redzone",
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -0700854 object - s->red_left_pad, val, s->red_left_pad))
855 return 0;
856
857 if (!check_bytes_and_report(s, page, object, "Redzone",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500858 endobject, val, s->inuse - s->object_size))
Christoph Lameter81819f02007-05-06 14:49:36 -0700859 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700860 } else {
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500861 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800862 check_bytes_and_report(s, page, p, "Alignment padding",
Chen Gangd0e0ac92013-07-15 09:05:29 +0800863 endobject, POISON_INUSE,
864 s->inuse - s->object_size);
Ingo Molnar3adbefe2008-02-05 17:57:39 -0800865 }
Christoph Lameter81819f02007-05-06 14:49:36 -0700866 }
867
868 if (s->flags & SLAB_POISON) {
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500869 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
Christoph Lameter24922682007-07-17 04:03:18 -0700870 (!check_bytes_and_report(s, page, p, "Poison", p,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500871 POISON_FREE, s->object_size - 1) ||
Christoph Lameter24922682007-07-17 04:03:18 -0700872 !check_bytes_and_report(s, page, p, "Poison",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500873 p + s->object_size - 1, POISON_END, 1)))
Christoph Lameter81819f02007-05-06 14:49:36 -0700874 return 0;
Christoph Lameter81819f02007-05-06 14:49:36 -0700875 /*
876 * check_pad_bytes cleans up on its own.
877 */
878 check_pad_bytes(s, page, p);
879 }
880
Christoph Lameterf7cb1932010-09-29 07:15:01 -0500881 if (!s->offset && val == SLUB_RED_ACTIVE)
Christoph Lameter81819f02007-05-06 14:49:36 -0700882 /*
883 * Object and freepointer overlap. Cannot check
884 * freepointer while object is allocated.
885 */
886 return 1;
887
888 /* Check free pointer validity */
889 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
890 object_err(s, page, p, "Freepointer corrupt");
891 /*
Nick Andrew9f6c708e2008-12-05 14:08:08 +1100892 * No choice but to zap it and thus lose the remainder
Christoph Lameter81819f02007-05-06 14:49:36 -0700893 * of the free objects in this slab. May cause
Christoph Lameter672bba32007-05-09 02:32:39 -0700894 * another error because the object count is now wrong.
Christoph Lameter81819f02007-05-06 14:49:36 -0700895 */
Christoph Lametera973e9d2008-03-01 13:40:44 -0800896 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700897 return 0;
898 }
899 return 1;
900}
901
902static int check_slab(struct kmem_cache *s, struct page *page)
903{
Christoph Lameter39b26462008-04-14 19:11:30 +0300904 int maxobj;
905
Christoph Lameter81819f02007-05-06 14:49:36 -0700906 VM_BUG_ON(!irqs_disabled());
907
908 if (!PageSlab(page)) {
Christoph Lameter24922682007-07-17 04:03:18 -0700909 slab_err(s, page, "Not a valid slab page");
Christoph Lameter81819f02007-05-06 14:49:36 -0700910 return 0;
911 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300912
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800913 maxobj = order_objects(compound_order(page), s->size, s->reserved);
Christoph Lameter39b26462008-04-14 19:11:30 +0300914 if (page->objects > maxobj) {
915 slab_err(s, page, "objects %u > max %u",
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800916 page->objects, maxobj);
Christoph Lameter39b26462008-04-14 19:11:30 +0300917 return 0;
918 }
919 if (page->inuse > page->objects) {
Christoph Lameter24922682007-07-17 04:03:18 -0700920 slab_err(s, page, "inuse %u > max %u",
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800921 page->inuse, page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -0700922 return 0;
923 }
924 /* Slab_pad_check fixes things up after itself */
925 slab_pad_check(s, page);
926 return 1;
927}
928
929/*
Christoph Lameter672bba32007-05-09 02:32:39 -0700930 * Determine if a certain object on a page is on the freelist. Must hold the
931 * slab lock to guarantee that the chains are in a consistent state.
Christoph Lameter81819f02007-05-06 14:49:36 -0700932 */
933static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
934{
935 int nr = 0;
Christoph Lameter881db7f2011-06-01 12:25:53 -0500936 void *fp;
Christoph Lameter81819f02007-05-06 14:49:36 -0700937 void *object = NULL;
Andrey Ryabininf6edde92014-12-10 15:42:22 -0800938 int max_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -0700939
Christoph Lameter881db7f2011-06-01 12:25:53 -0500940 fp = page->freelist;
Christoph Lameter39b26462008-04-14 19:11:30 +0300941 while (fp && nr <= page->objects) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700942 if (fp == search)
943 return 1;
944 if (!check_valid_pointer(s, page, fp)) {
945 if (object) {
946 object_err(s, page, object,
947 "Freechain corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800948 set_freepointer(s, object, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -0700949 } else {
Christoph Lameter24922682007-07-17 04:03:18 -0700950 slab_err(s, page, "Freepointer corrupt");
Christoph Lametera973e9d2008-03-01 13:40:44 -0800951 page->freelist = NULL;
Christoph Lameter39b26462008-04-14 19:11:30 +0300952 page->inuse = page->objects;
Christoph Lameter24922682007-07-17 04:03:18 -0700953 slab_fix(s, "Freelist cleared");
Christoph Lameter81819f02007-05-06 14:49:36 -0700954 return 0;
955 }
956 break;
957 }
958 object = fp;
959 fp = get_freepointer(s, object);
960 nr++;
961 }
962
Lai Jiangshanab9a0f12011-03-10 15:21:48 +0800963 max_objects = order_objects(compound_order(page), s->size, s->reserved);
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +0400964 if (max_objects > MAX_OBJS_PER_PAGE)
965 max_objects = MAX_OBJS_PER_PAGE;
Christoph Lameter224a88b2008-04-14 19:11:31 +0300966
967 if (page->objects != max_objects) {
968 slab_err(s, page, "Wrong number of objects. Found %d but "
969 "should be %d", page->objects, max_objects);
970 page->objects = max_objects;
971 slab_fix(s, "Number of objects adjusted.");
972 }
Christoph Lameter39b26462008-04-14 19:11:30 +0300973 if (page->inuse != page->objects - nr) {
Christoph Lameter70d71222007-05-06 14:49:47 -0700974 slab_err(s, page, "Wrong object count. Counter is %d but "
Christoph Lameter39b26462008-04-14 19:11:30 +0300975 "counted were %d", page->inuse, page->objects - nr);
976 page->inuse = page->objects - nr;
Christoph Lameter24922682007-07-17 04:03:18 -0700977 slab_fix(s, "Object count adjusted.");
Christoph Lameter81819f02007-05-06 14:49:36 -0700978 }
979 return search == NULL;
980}
981
Christoph Lameter0121c6192008-04-29 16:11:12 -0700982static void trace(struct kmem_cache *s, struct page *page, void *object,
983 int alloc)
Christoph Lameter3ec09742007-05-16 22:11:00 -0700984{
985 if (s->flags & SLAB_TRACE) {
Fabian Frederickf9f58282014-06-04 16:06:34 -0700986 pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
Christoph Lameter3ec09742007-05-16 22:11:00 -0700987 s->name,
988 alloc ? "alloc" : "free",
989 object, page->inuse,
990 page->freelist);
991
992 if (!alloc)
Chen Gangd0e0ac92013-07-15 09:05:29 +0800993 print_section("Object ", (void *)object,
994 s->object_size);
Christoph Lameter3ec09742007-05-16 22:11:00 -0700995
996 dump_stack();
997 }
998}
999
Christoph Lameter643b1132007-05-06 14:49:42 -07001000/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001001 * Tracking of fully allocated slabs for debugging purposes.
Christoph Lameter643b1132007-05-06 14:49:42 -07001002 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001003static void add_full(struct kmem_cache *s,
1004 struct kmem_cache_node *n, struct page *page)
Christoph Lameter643b1132007-05-06 14:49:42 -07001005{
Christoph Lameter643b1132007-05-06 14:49:42 -07001006 if (!(s->flags & SLAB_STORE_USER))
1007 return;
1008
David Rientjes255d0882014-02-10 14:25:39 -08001009 lockdep_assert_held(&n->list_lock);
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001010 list_add(&page->lru, &n->full);
1011}
Christoph Lameter643b1132007-05-06 14:49:42 -07001012
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001013static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct page *page)
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001014{
1015 if (!(s->flags & SLAB_STORE_USER))
1016 return;
1017
David Rientjes255d0882014-02-10 14:25:39 -08001018 lockdep_assert_held(&n->list_lock);
Christoph Lameter643b1132007-05-06 14:49:42 -07001019 list_del(&page->lru);
Christoph Lameter643b1132007-05-06 14:49:42 -07001020}
1021
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001022/* Tracking of the number of slabs for debugging purposes */
1023static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1024{
1025 struct kmem_cache_node *n = get_node(s, node);
1026
1027 return atomic_long_read(&n->nr_slabs);
1028}
1029
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001030static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1031{
1032 return atomic_long_read(&n->nr_slabs);
1033}
1034
Christoph Lameter205ab992008-04-14 19:11:40 +03001035static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001036{
1037 struct kmem_cache_node *n = get_node(s, node);
1038
1039 /*
1040 * May be called early in order to allocate a slab for the
1041 * kmem_cache_node structure. Solve the chicken-egg
1042 * dilemma by deferring the increment of the count during
1043 * bootstrap (see early_kmem_cache_node_alloc).
1044 */
Joonsoo Kim338b2642013-01-21 17:01:27 +09001045 if (likely(n)) {
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001046 atomic_long_inc(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001047 atomic_long_add(objects, &n->total_objects);
1048 }
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001049}
Christoph Lameter205ab992008-04-14 19:11:40 +03001050static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001051{
1052 struct kmem_cache_node *n = get_node(s, node);
1053
1054 atomic_long_dec(&n->nr_slabs);
Christoph Lameter205ab992008-04-14 19:11:40 +03001055 atomic_long_sub(objects, &n->total_objects);
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001056}
1057
1058/* Object debug checks for alloc/free paths */
Christoph Lameter3ec09742007-05-16 22:11:00 -07001059static void setup_object_debug(struct kmem_cache *s, struct page *page,
1060 void *object)
1061{
1062 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
1063 return;
1064
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001065 init_object(s, object, SLUB_RED_INACTIVE);
Christoph Lameter3ec09742007-05-16 22:11:00 -07001066 init_tracking(s, object);
1067}
1068
Chen Gangd0e0ac92013-07-15 09:05:29 +08001069static noinline int alloc_debug_processing(struct kmem_cache *s,
1070 struct page *page,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001071 void *object, unsigned long addr)
Christoph Lameter81819f02007-05-06 14:49:36 -07001072{
1073 if (!check_slab(s, page))
1074 goto bad;
1075
Christoph Lameter81819f02007-05-06 14:49:36 -07001076 if (!check_valid_pointer(s, page, object)) {
1077 object_err(s, page, object, "Freelist Pointer check fails");
Christoph Lameter70d71222007-05-06 14:49:47 -07001078 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001079 }
1080
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001081 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
Christoph Lameter81819f02007-05-06 14:49:36 -07001082 goto bad;
Christoph Lameter81819f02007-05-06 14:49:36 -07001083
Christoph Lameter3ec09742007-05-16 22:11:00 -07001084 /* Success perform special debug activities for allocs */
1085 if (s->flags & SLAB_STORE_USER)
1086 set_track(s, object, TRACK_ALLOC, addr);
1087 trace(s, page, object, 1);
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001088 init_object(s, object, SLUB_RED_ACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001089 return 1;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001090
Christoph Lameter81819f02007-05-06 14:49:36 -07001091bad:
1092 if (PageSlab(page)) {
1093 /*
1094 * If this is a slab page then lets do the best we can
1095 * to avoid issues in the future. Marking all objects
Christoph Lameter672bba32007-05-09 02:32:39 -07001096 * as used avoids touching the remaining objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001097 */
Christoph Lameter24922682007-07-17 04:03:18 -07001098 slab_fix(s, "Marking all objects used");
Christoph Lameter39b26462008-04-14 19:11:30 +03001099 page->inuse = page->objects;
Christoph Lametera973e9d2008-03-01 13:40:44 -08001100 page->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001101 }
1102 return 0;
1103}
1104
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001105/* Supports checking bulk free of a constructed freelist */
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001106static noinline struct kmem_cache_node *free_debug_processing(
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001107 struct kmem_cache *s, struct page *page,
1108 void *head, void *tail, int bulk_cnt,
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001109 unsigned long addr, unsigned long *flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001110{
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001111 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001112 void *object = head;
1113 int cnt = 0;
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001114
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001115 spin_lock_irqsave(&n->list_lock, *flags);
Christoph Lameter881db7f2011-06-01 12:25:53 -05001116 slab_lock(page);
1117
Christoph Lameter81819f02007-05-06 14:49:36 -07001118 if (!check_slab(s, page))
1119 goto fail;
1120
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001121next_object:
1122 cnt++;
1123
Christoph Lameter81819f02007-05-06 14:49:36 -07001124 if (!check_valid_pointer(s, page, object)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001125 slab_err(s, page, "Invalid object pointer 0x%p", object);
Christoph Lameter81819f02007-05-06 14:49:36 -07001126 goto fail;
1127 }
1128
1129 if (on_freelist(s, page, object)) {
Christoph Lameter24922682007-07-17 04:03:18 -07001130 object_err(s, page, object, "Object already free");
Christoph Lameter81819f02007-05-06 14:49:36 -07001131 goto fail;
1132 }
1133
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001134 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001135 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07001136
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001137 if (unlikely(s != page->slab_cache)) {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08001138 if (!PageSlab(page)) {
Christoph Lameter70d71222007-05-06 14:49:47 -07001139 slab_err(s, page, "Attempt to free object(0x%p) "
1140 "outside of slab", object);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001141 } else if (!page->slab_cache) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07001142 pr_err("SLUB <none>: no slab for object 0x%p.\n",
1143 object);
Christoph Lameter70d71222007-05-06 14:49:47 -07001144 dump_stack();
Pekka Enberg06428782008-01-07 23:20:27 -08001145 } else
Christoph Lameter24922682007-07-17 04:03:18 -07001146 object_err(s, page, object,
1147 "page slab pointer corrupt.");
Christoph Lameter81819f02007-05-06 14:49:36 -07001148 goto fail;
1149 }
Christoph Lameter3ec09742007-05-16 22:11:00 -07001150
Christoph Lameter3ec09742007-05-16 22:11:00 -07001151 if (s->flags & SLAB_STORE_USER)
1152 set_track(s, object, TRACK_FREE, addr);
1153 trace(s, page, object, 0);
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001154 /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001155 init_object(s, object, SLUB_RED_INACTIVE);
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001156
1157 /* Reached end of constructed freelist yet? */
1158 if (object != tail) {
1159 object = get_freepointer(s, object);
1160 goto next_object;
1161 }
Christoph Lameter5c2e4bb2011-06-01 12:25:54 -05001162out:
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001163 if (cnt != bulk_cnt)
1164 slab_err(s, page, "Bulk freelist count(%d) invalid(%d)\n",
1165 bulk_cnt, cnt);
1166
Christoph Lameter881db7f2011-06-01 12:25:53 -05001167 slab_unlock(page);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001168 /*
1169 * Keep node_lock to preserve integrity
1170 * until the object is actually freed
1171 */
1172 return n;
Christoph Lameter3ec09742007-05-16 22:11:00 -07001173
Christoph Lameter81819f02007-05-06 14:49:36 -07001174fail:
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001175 slab_unlock(page);
1176 spin_unlock_irqrestore(&n->list_lock, *flags);
Christoph Lameter24922682007-07-17 04:03:18 -07001177 slab_fix(s, "Object at 0x%p not freed", object);
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001178 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07001179}
1180
Christoph Lameter41ecc552007-05-09 02:32:44 -07001181static int __init setup_slub_debug(char *str)
1182{
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001183 slub_debug = DEBUG_DEFAULT_FLAGS;
1184 if (*str++ != '=' || !*str)
1185 /*
1186 * No options specified. Switch on full debugging.
1187 */
1188 goto out;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001189
1190 if (*str == ',')
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001191 /*
1192 * No options but restriction on slabs. This means full
1193 * debugging for slabs matching a pattern.
1194 */
1195 goto check_slabs;
1196
1197 slub_debug = 0;
1198 if (*str == '-')
1199 /*
1200 * Switch off all debugging measures.
1201 */
1202 goto out;
1203
1204 /*
1205 * Determine which debug features should be switched on
1206 */
Pekka Enberg06428782008-01-07 23:20:27 -08001207 for (; *str && *str != ','; str++) {
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001208 switch (tolower(*str)) {
1209 case 'f':
1210 slub_debug |= SLAB_DEBUG_FREE;
1211 break;
1212 case 'z':
1213 slub_debug |= SLAB_RED_ZONE;
1214 break;
1215 case 'p':
1216 slub_debug |= SLAB_POISON;
1217 break;
1218 case 'u':
1219 slub_debug |= SLAB_STORE_USER;
1220 break;
1221 case 't':
1222 slub_debug |= SLAB_TRACE;
1223 break;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03001224 case 'a':
1225 slub_debug |= SLAB_FAILSLAB;
1226 break;
Chris J Arges08303a72015-04-14 15:44:25 -07001227 case 'o':
1228 /*
1229 * Avoid enabling debugging on caches if its minimum
1230 * order would increase as a result.
1231 */
1232 disable_higher_order_debug = 1;
1233 break;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001234 default:
Fabian Frederickf9f58282014-06-04 16:06:34 -07001235 pr_err("slub_debug option '%c' unknown. skipped\n",
1236 *str);
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001237 }
1238 }
1239
1240check_slabs:
1241 if (*str == ',')
Christoph Lameter41ecc552007-05-09 02:32:44 -07001242 slub_debug_slabs = str + 1;
Christoph Lameterf0630ff2007-07-15 23:38:14 -07001243out:
Christoph Lameter41ecc552007-05-09 02:32:44 -07001244 return 1;
1245}
1246
1247__setup("slub_debug", setup_slub_debug);
1248
Joonsoo Kim423c9292014-10-09 15:26:22 -07001249unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001250 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001251 void (*ctor)(void *))
Christoph Lameter41ecc552007-05-09 02:32:44 -07001252{
1253 /*
Christoph Lametere1533622008-02-15 23:45:24 -08001254 * Enable debugging if selected on the kernel commandline.
Christoph Lameter41ecc552007-05-09 02:32:44 -07001255 */
Christoph Lameterc6f58d9b2013-11-07 16:29:15 +00001256 if (slub_debug && (!slub_debug_slabs || (name &&
1257 !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)))))
David Rientjes3de472132009-07-27 18:30:35 -07001258 flags |= slub_debug;
Christoph Lameterba0268a2007-09-11 15:24:11 -07001259
1260 return flags;
Christoph Lameter41ecc552007-05-09 02:32:44 -07001261}
Jesper Dangaard Brouerb4a64712015-11-20 15:57:41 -08001262#else /* !CONFIG_SLUB_DEBUG */
Christoph Lameter3ec09742007-05-16 22:11:00 -07001263static inline void setup_object_debug(struct kmem_cache *s,
1264 struct page *page, void *object) {}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001265
Christoph Lameter3ec09742007-05-16 22:11:00 -07001266static inline int alloc_debug_processing(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03001267 struct page *page, void *object, unsigned long addr) { return 0; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001268
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001269static inline struct kmem_cache_node *free_debug_processing(
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001270 struct kmem_cache *s, struct page *page,
1271 void *head, void *tail, int bulk_cnt,
Christoph Lameter19c7ff92012-05-30 12:54:46 -05001272 unsigned long addr, unsigned long *flags) { return NULL; }
Christoph Lameter41ecc552007-05-09 02:32:44 -07001273
Christoph Lameter41ecc552007-05-09 02:32:44 -07001274static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1275 { return 1; }
1276static inline int check_object(struct kmem_cache *s, struct page *page,
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001277 void *object, u8 val) { return 1; }
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001278static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
1279 struct page *page) {}
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001280static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
1281 struct page *page) {}
Joonsoo Kim423c9292014-10-09 15:26:22 -07001282unsigned long kmem_cache_flags(unsigned long object_size,
Christoph Lameterba0268a2007-09-11 15:24:11 -07001283 unsigned long flags, const char *name,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001284 void (*ctor)(void *))
Christoph Lameterba0268a2007-09-11 15:24:11 -07001285{
1286 return flags;
1287}
Christoph Lameter41ecc552007-05-09 02:32:44 -07001288#define slub_debug 0
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001289
Ingo Molnarfdaa45e2009-09-15 11:00:26 +02001290#define disable_higher_order_debug 0
1291
Christoph Lameter0f389ec2008-04-14 18:53:02 +03001292static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1293 { return 0; }
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04001294static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1295 { return 0; }
Christoph Lameter205ab992008-04-14 19:11:40 +03001296static inline void inc_slabs_node(struct kmem_cache *s, int node,
1297 int objects) {}
1298static inline void dec_slabs_node(struct kmem_cache *s, int node,
1299 int objects) {}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001300
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001301#endif /* CONFIG_SLUB_DEBUG */
1302
1303/*
1304 * Hooks for other subsystems that check memory allocations. In a typical
1305 * production configuration these hooks all should produce no code at all.
1306 */
Roman Bobnievd56791b2013-10-08 15:58:57 -07001307static inline void kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
1308{
1309 kmemleak_alloc(ptr, size, 1, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08001310 kasan_kmalloc_large(ptr, size);
Roman Bobnievd56791b2013-10-08 15:58:57 -07001311}
1312
1313static inline void kfree_hook(const void *x)
1314{
1315 kmemleak_free(x);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08001316 kasan_kfree_large(x);
Roman Bobnievd56791b2013-10-08 15:58:57 -07001317}
1318
Vladimir Davydov8135be52014-12-12 16:56:38 -08001319static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
1320 gfp_t flags)
Roman Bobnievd56791b2013-10-08 15:58:57 -07001321{
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001322 flags &= gfp_allowed_mask;
1323 lockdep_trace_alloc(flags);
Mel Gormand0164ad2015-11-06 16:28:21 -08001324 might_sleep_if(gfpflags_allow_blocking(flags));
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001325
Vladimir Davydov8135be52014-12-12 16:56:38 -08001326 if (should_failslab(s->object_size, flags, s->flags))
1327 return NULL;
1328
1329 return memcg_kmem_get_cache(s, flags);
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001330}
1331
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08001332static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
1333 size_t size, void **p)
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001334{
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08001335 size_t i;
1336
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001337 flags &= gfp_allowed_mask;
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08001338 for (i = 0; i < size; i++) {
1339 void *object = p[i];
1340
1341 kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
1342 kmemleak_alloc_recursive(object, s->object_size, 1,
1343 s->flags, flags);
1344 kasan_slab_alloc(s, object);
1345 }
Vladimir Davydov8135be52014-12-12 16:56:38 -08001346 memcg_kmem_put_cache(s);
Roman Bobnievd56791b2013-10-08 15:58:57 -07001347}
Christoph Lameter7d550c52010-08-25 14:07:16 -05001348
Roman Bobnievd56791b2013-10-08 15:58:57 -07001349static inline void slab_free_hook(struct kmem_cache *s, void *x)
1350{
1351 kmemleak_free_recursive(x, s->flags);
Christoph Lameter7d550c52010-08-25 14:07:16 -05001352
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001353 /*
1354 * Trouble is that we may no longer disable interrupts in the fast path
1355 * So in order to make the debug calls that expect irqs to be
1356 * disabled we need to disable interrupts temporarily.
1357 */
1358#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
1359 {
1360 unsigned long flags;
1361
1362 local_irq_save(flags);
1363 kmemcheck_slab_free(s, x, s->object_size);
1364 debug_check_no_locks_freed(x, s->object_size);
1365 local_irq_restore(flags);
1366 }
1367#endif
1368 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1369 debug_check_no_obj_freed(x, s->object_size);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08001370
1371 kasan_slab_free(s, x);
Andrey Ryabinin02e72cc2014-08-06 16:04:18 -07001372}
Christoph Lameter205ab992008-04-14 19:11:40 +03001373
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08001374static inline void slab_free_freelist_hook(struct kmem_cache *s,
1375 void *head, void *tail)
1376{
1377/*
1378 * Compiler cannot detect this function can be removed if slab_free_hook()
1379 * evaluates to nothing. Thus, catch all relevant config debug options here.
1380 */
1381#if defined(CONFIG_KMEMCHECK) || \
1382 defined(CONFIG_LOCKDEP) || \
1383 defined(CONFIG_DEBUG_KMEMLEAK) || \
1384 defined(CONFIG_DEBUG_OBJECTS_FREE) || \
1385 defined(CONFIG_KASAN)
1386
1387 void *object = head;
1388 void *tail_obj = tail ? : head;
1389
1390 do {
1391 slab_free_hook(s, object);
1392 } while ((object != tail_obj) &&
1393 (object = get_freepointer(s, object)));
1394#endif
1395}
1396
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001397static void setup_object(struct kmem_cache *s, struct page *page,
1398 void *object)
1399{
1400 setup_object_debug(s, page, object);
1401 if (unlikely(s->ctor)) {
1402 kasan_unpoison_object_data(s, object);
1403 s->ctor(object);
1404 kasan_poison_object_data(s, object);
1405 }
1406}
1407
Christoph Lameter81819f02007-05-06 14:49:36 -07001408/*
1409 * Slab allocation and freeing
1410 */
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001411static inline struct page *alloc_slab_page(struct kmem_cache *s,
1412 gfp_t flags, int node, struct kmem_cache_order_objects oo)
Christoph Lameter65c33762008-04-14 19:11:40 +03001413{
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001414 struct page *page;
Christoph Lameter65c33762008-04-14 19:11:40 +03001415 int order = oo_order(oo);
1416
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001417 flags |= __GFP_NOTRACK;
1418
Christoph Lameter2154a332010-07-09 14:07:10 -05001419 if (node == NUMA_NO_NODE)
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001420 page = alloc_pages(flags, order);
Christoph Lameter65c33762008-04-14 19:11:40 +03001421 else
Vlastimil Babka96db8002015-09-08 15:03:50 -07001422 page = __alloc_pages_node(node, flags, order);
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001423
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08001424 if (page && memcg_charge_slab(page, flags, order, s)) {
1425 __free_pages(page, order);
1426 page = NULL;
1427 }
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001428
1429 return page;
Christoph Lameter65c33762008-04-14 19:11:40 +03001430}
1431
Christoph Lameter81819f02007-05-06 14:49:36 -07001432static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1433{
Pekka Enberg06428782008-01-07 23:20:27 -08001434 struct page *page;
Christoph Lameter834f3d12008-04-14 19:11:31 +03001435 struct kmem_cache_order_objects oo = s->oo;
Pekka Enbergba522702009-06-24 21:59:51 +03001436 gfp_t alloc_gfp;
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001437 void *start, *p;
1438 int idx, order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001439
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001440 flags &= gfp_allowed_mask;
1441
Mel Gormand0164ad2015-11-06 16:28:21 -08001442 if (gfpflags_allow_blocking(flags))
Christoph Lameter7e0528d2011-06-01 12:25:44 -05001443 local_irq_enable();
1444
Christoph Lameterb7a49f02008-02-14 14:21:32 -08001445 flags |= s->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001446
Pekka Enbergba522702009-06-24 21:59:51 +03001447 /*
1448 * Let the initial higher-order allocation fail under memory pressure
1449 * so we fall-back to the minimum order allocation.
1450 */
1451 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
Mel Gormand0164ad2015-11-06 16:28:21 -08001452 if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min))
1453 alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~__GFP_DIRECT_RECLAIM;
Pekka Enbergba522702009-06-24 21:59:51 +03001454
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001455 page = alloc_slab_page(s, alloc_gfp, node, oo);
Christoph Lameter65c33762008-04-14 19:11:40 +03001456 if (unlikely(!page)) {
1457 oo = s->min;
Joonsoo Kim80c3a992014-03-12 17:26:20 +09001458 alloc_gfp = flags;
Christoph Lameter65c33762008-04-14 19:11:40 +03001459 /*
1460 * Allocation may have failed due to fragmentation.
1461 * Try a lower order alloc if possible
1462 */
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001463 page = alloc_slab_page(s, alloc_gfp, node, oo);
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001464 if (unlikely(!page))
1465 goto out;
1466 stat(s, ORDER_FALLBACK);
Christoph Lameter65c33762008-04-14 19:11:40 +03001467 }
Vegard Nossum5a896d92008-04-04 00:54:48 +02001468
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001469 if (kmemcheck_enabled &&
1470 !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001471 int pages = 1 << oo_order(oo);
1472
Joonsoo Kim80c3a992014-03-12 17:26:20 +09001473 kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001474
1475 /*
1476 * Objects from caches that have a constructor don't get
1477 * cleared when they're allocated, so we need to do it here.
1478 */
1479 if (s->ctor)
1480 kmemcheck_mark_uninitialized_pages(page, pages);
1481 else
1482 kmemcheck_mark_unallocated_pages(page, pages);
Vegard Nossum5a896d92008-04-04 00:54:48 +02001483 }
1484
Christoph Lameter834f3d12008-04-14 19:11:31 +03001485 page->objects = oo_objects(oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07001486
Glauber Costa1f458cb2012-12-18 14:22:50 -08001487 order = compound_order(page);
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001488 page->slab_cache = s;
Joonsoo Kimc03f94c2012-05-18 00:47:47 +09001489 __SetPageSlab(page);
Michal Hocko2f064f32015-08-21 14:11:51 -07001490 if (page_is_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -07001491 SetPageSlabPfmemalloc(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001492
1493 start = page_address(page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001494
1495 if (unlikely(s->flags & SLAB_POISON))
Glauber Costa1f458cb2012-12-18 14:22:50 -08001496 memset(start, POISON_INUSE, PAGE_SIZE << order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001497
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08001498 kasan_poison_slab(page);
1499
Wei Yang54266642014-08-06 16:04:42 -07001500 for_each_object_idx(p, idx, s, start, page->objects) {
1501 setup_object(s, page, p);
1502 if (likely(idx < page->objects))
1503 set_freepointer(s, p, p + s->size);
1504 else
1505 set_freepointer(s, p, NULL);
Christoph Lameter81819f02007-05-06 14:49:36 -07001506 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001507
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -07001508 page->freelist = fixup_red_left(s, start);
Christoph Lametere6e82ea2011-08-09 16:12:24 -05001509 page->inuse = page->objects;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05001510 page->frozen = 1;
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001511
Christoph Lameter81819f02007-05-06 14:49:36 -07001512out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001513 if (gfpflags_allow_blocking(flags))
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001514 local_irq_disable();
1515 if (!page)
1516 return NULL;
1517
1518 mod_zone_page_state(page_zone(page),
1519 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1520 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1521 1 << oo_order(oo));
1522
1523 inc_slabs_node(s, page_to_nid(page), page->objects);
1524
Christoph Lameter81819f02007-05-06 14:49:36 -07001525 return page;
1526}
1527
Thomas Gleixner588f8ba2015-09-04 15:45:48 -07001528static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1529{
1530 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
1531 pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
1532 BUG();
1533 }
1534
1535 return allocate_slab(s,
1536 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1537}
1538
Christoph Lameter81819f02007-05-06 14:49:36 -07001539static void __free_slab(struct kmem_cache *s, struct page *page)
1540{
Christoph Lameter834f3d12008-04-14 19:11:31 +03001541 int order = compound_order(page);
1542 int pages = 1 << order;
Christoph Lameter81819f02007-05-06 14:49:36 -07001543
Christoph Lameteraf537b02010-07-09 14:07:14 -05001544 if (kmem_cache_debug(s)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07001545 void *p;
1546
1547 slab_pad_check(s, page);
Christoph Lameter224a88b2008-04-14 19:11:31 +03001548 for_each_object(p, s, page_address(page),
1549 page->objects)
Christoph Lameterf7cb1932010-09-29 07:15:01 -05001550 check_object(s, page, p, SLUB_RED_INACTIVE);
Christoph Lameter81819f02007-05-06 14:49:36 -07001551 }
1552
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001553 kmemcheck_free_shadow(page, compound_order(page));
Vegard Nossum5a896d92008-04-04 00:54:48 +02001554
Christoph Lameter81819f02007-05-06 14:49:36 -07001555 mod_zone_page_state(page_zone(page),
1556 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1557 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
Pekka Enberg06428782008-01-07 23:20:27 -08001558 -pages);
Christoph Lameter81819f02007-05-06 14:49:36 -07001559
Mel Gorman072bb0a2012-07-31 16:43:58 -07001560 __ClearPageSlabPfmemalloc(page);
Christoph Lameter49bd5222008-04-14 18:52:18 +03001561 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001562
Mel Gorman22b751c2013-02-22 16:34:59 -08001563 page_mapcount_reset(page);
Nick Piggin1eb5ac62009-05-05 19:13:44 +10001564 if (current->reclaim_state)
1565 current->reclaim_state->reclaimed_slab += pages;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08001566 __free_kmem_pages(page, order);
Christoph Lameter81819f02007-05-06 14:49:36 -07001567}
1568
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001569#define need_reserve_slab_rcu \
1570 (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
1571
Christoph Lameter81819f02007-05-06 14:49:36 -07001572static void rcu_free_slab(struct rcu_head *h)
1573{
1574 struct page *page;
1575
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001576 if (need_reserve_slab_rcu)
1577 page = virt_to_head_page(h);
1578 else
1579 page = container_of((struct list_head *)h, struct page, lru);
1580
Glauber Costa1b4f59e32012-10-22 18:05:36 +04001581 __free_slab(page->slab_cache, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07001582}
1583
1584static void free_slab(struct kmem_cache *s, struct page *page)
1585{
1586 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001587 struct rcu_head *head;
1588
1589 if (need_reserve_slab_rcu) {
1590 int order = compound_order(page);
1591 int offset = (PAGE_SIZE << order) - s->reserved;
1592
1593 VM_BUG_ON(s->reserved != sizeof(*head));
1594 head = page_address(page) + offset;
1595 } else {
Kirill A. Shutemovbc4f6102015-11-06 16:29:44 -08001596 head = &page->rcu_head;
Lai Jiangshanda9a6382011-03-10 15:22:00 +08001597 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001598
1599 call_rcu(head, rcu_free_slab);
1600 } else
1601 __free_slab(s, page);
1602}
1603
1604static void discard_slab(struct kmem_cache *s, struct page *page)
1605{
Christoph Lameter205ab992008-04-14 19:11:40 +03001606 dec_slabs_node(s, page_to_nid(page), page->objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07001607 free_slab(s, page);
1608}
1609
1610/*
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001611 * Management of partially allocated slabs.
Christoph Lameter81819f02007-05-06 14:49:36 -07001612 */
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001613static inline void
1614__add_partial(struct kmem_cache_node *n, struct page *page, int tail)
Christoph Lameter81819f02007-05-06 14:49:36 -07001615{
Christoph Lametere95eed52007-05-06 14:49:44 -07001616 n->nr_partial++;
Shaohua Li136333d2011-08-24 08:57:52 +08001617 if (tail == DEACTIVATE_TO_TAIL)
Christoph Lameter7c2e1322008-01-07 23:20:27 -08001618 list_add_tail(&page->lru, &n->partial);
1619 else
1620 list_add(&page->lru, &n->partial);
Christoph Lameter81819f02007-05-06 14:49:36 -07001621}
1622
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001623static inline void add_partial(struct kmem_cache_node *n,
1624 struct page *page, int tail)
1625{
1626 lockdep_assert_held(&n->list_lock);
1627 __add_partial(n, page, tail);
1628}
1629
1630static inline void
1631__remove_partial(struct kmem_cache_node *n, struct page *page)
1632{
1633 list_del(&page->lru);
1634 n->nr_partial--;
1635}
1636
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05001637static inline void remove_partial(struct kmem_cache_node *n,
Christoph Lameter62e346a2010-09-28 08:10:28 -05001638 struct page *page)
1639{
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001640 lockdep_assert_held(&n->list_lock);
Steven Rostedt1e4dd942014-02-10 14:25:46 -08001641 __remove_partial(n, page);
Christoph Lameter62e346a2010-09-28 08:10:28 -05001642}
1643
Christoph Lameter81819f02007-05-06 14:49:36 -07001644/*
Christoph Lameter7ced3712012-05-09 10:09:53 -05001645 * Remove slab from the partial list, freeze it and
1646 * return the pointer to the freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07001647 *
Christoph Lameter497b66f2011-08-09 16:12:26 -05001648 * Returns a list of objects or NULL if it fails.
Christoph Lameter81819f02007-05-06 14:49:36 -07001649 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001650static inline void *acquire_slab(struct kmem_cache *s,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001651 struct kmem_cache_node *n, struct page *page,
Joonsoo Kim633b0762013-01-21 17:01:25 +09001652 int mode, int *objects)
Christoph Lameter81819f02007-05-06 14:49:36 -07001653{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001654 void *freelist;
1655 unsigned long counters;
1656 struct page new;
1657
Peter Zijlstrac65c1872014-01-10 13:23:49 +01001658 lockdep_assert_held(&n->list_lock);
1659
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001660 /*
1661 * Zap the freelist and set the frozen bit.
1662 * The old freelist is the list of objects for the
1663 * per cpu allocation list.
1664 */
Christoph Lameter7ced3712012-05-09 10:09:53 -05001665 freelist = page->freelist;
1666 counters = page->counters;
1667 new.counters = counters;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001668 *objects = new.objects - new.inuse;
Pekka Enberg23910c52012-06-04 10:14:58 +03001669 if (mode) {
Christoph Lameter7ced3712012-05-09 10:09:53 -05001670 new.inuse = page->objects;
Pekka Enberg23910c52012-06-04 10:14:58 +03001671 new.freelist = NULL;
1672 } else {
1673 new.freelist = freelist;
1674 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001675
Dave Hansena0132ac2014-01-29 14:05:50 -08001676 VM_BUG_ON(new.frozen);
Christoph Lameter7ced3712012-05-09 10:09:53 -05001677 new.frozen = 1;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001678
Christoph Lameter7ced3712012-05-09 10:09:53 -05001679 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001680 freelist, counters,
Joonsoo Kim02d76332012-05-17 00:13:02 +09001681 new.freelist, new.counters,
Christoph Lameter7ced3712012-05-09 10:09:53 -05001682 "acquire_slab"))
Christoph Lameter7ced3712012-05-09 10:09:53 -05001683 return NULL;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001684
1685 remove_partial(n, page);
Christoph Lameter7ced3712012-05-09 10:09:53 -05001686 WARN_ON(!freelist);
Christoph Lameter49e22582011-08-09 16:12:27 -05001687 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07001688}
1689
Joonsoo Kim633b0762013-01-21 17:01:25 +09001690static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001691static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
Christoph Lameter49e22582011-08-09 16:12:27 -05001692
Christoph Lameter81819f02007-05-06 14:49:36 -07001693/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001694 * Try to allocate a partial slab from a specific node.
Christoph Lameter81819f02007-05-06 14:49:36 -07001695 */
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001696static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
1697 struct kmem_cache_cpu *c, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07001698{
Christoph Lameter49e22582011-08-09 16:12:27 -05001699 struct page *page, *page2;
1700 void *object = NULL;
Alexey Dobriyande2aac82018-04-05 16:21:10 -07001701 unsigned int available = 0;
Joonsoo Kim633b0762013-01-21 17:01:25 +09001702 int objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07001703
1704 /*
1705 * Racy check. If we mistakenly see no partial slabs then we
1706 * just allocate an empty slab. If we mistakenly try to get a
Christoph Lameter672bba32007-05-09 02:32:39 -07001707 * partial slab and there is none available then get_partials()
1708 * will return NULL.
Christoph Lameter81819f02007-05-06 14:49:36 -07001709 */
1710 if (!n || !n->nr_partial)
1711 return NULL;
1712
1713 spin_lock(&n->list_lock);
Christoph Lameter49e22582011-08-09 16:12:27 -05001714 list_for_each_entry_safe(page, page2, &n->partial, lru) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001715 void *t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001716
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001717 if (!pfmemalloc_match(page, flags))
1718 continue;
1719
Joonsoo Kim633b0762013-01-21 17:01:25 +09001720 t = acquire_slab(s, n, page, object == NULL, &objects);
Christoph Lameter49e22582011-08-09 16:12:27 -05001721 if (!t)
1722 break;
1723
Joonsoo Kim633b0762013-01-21 17:01:25 +09001724 available += objects;
Alex,Shi12d79632011-09-07 10:26:36 +08001725 if (!object) {
Christoph Lameter49e22582011-08-09 16:12:27 -05001726 c->page = page;
Christoph Lameter49e22582011-08-09 16:12:27 -05001727 stat(s, ALLOC_FROM_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05001728 object = t;
Christoph Lameter49e22582011-08-09 16:12:27 -05001729 } else {
Joonsoo Kim633b0762013-01-21 17:01:25 +09001730 put_cpu_partial(s, page, 0);
Alex Shi8028dce2012-02-03 23:34:56 +08001731 stat(s, CPU_PARTIAL_NODE);
Christoph Lameter49e22582011-08-09 16:12:27 -05001732 }
Joonsoo Kim345c9052013-06-19 14:05:52 +09001733 if (!kmem_cache_has_cpu_partial(s)
1734 || available > s->cpu_partial / 2)
Christoph Lameter49e22582011-08-09 16:12:27 -05001735 break;
1736
Christoph Lameter497b66f2011-08-09 16:12:26 -05001737 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001738 spin_unlock(&n->list_lock);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001739 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001740}
1741
1742/*
Christoph Lameter672bba32007-05-09 02:32:39 -07001743 * Get a page from somewhere. Search in increasing NUMA distances.
Christoph Lameter81819f02007-05-06 14:49:36 -07001744 */
Joonsoo Kimde3ec032012-01-27 00:12:23 -08001745static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001746 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001747{
1748#ifdef CONFIG_NUMA
1749 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07001750 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07001751 struct zone *zone;
1752 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001753 void *object;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001754 unsigned int cpuset_mems_cookie;
Christoph Lameter81819f02007-05-06 14:49:36 -07001755
1756 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07001757 * The defrag ratio allows a configuration of the tradeoffs between
1758 * inter node defragmentation and node local allocations. A lower
1759 * defrag_ratio increases the tendency to do local allocations
1760 * instead of attempting to obtain partial slabs from other nodes.
Christoph Lameter81819f02007-05-06 14:49:36 -07001761 *
Christoph Lameter672bba32007-05-09 02:32:39 -07001762 * If the defrag_ratio is set to 0 then kmalloc() always
1763 * returns node local objects. If the ratio is higher then kmalloc()
1764 * may return off node objects because partial slabs are obtained
1765 * from other nodes and filled up.
Christoph Lameter81819f02007-05-06 14:49:36 -07001766 *
Christoph Lameter6446faa2008-02-15 23:45:26 -08001767 * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
Christoph Lameter672bba32007-05-09 02:32:39 -07001768 * defrag_ratio = 1000) then every (well almost) allocation will
1769 * first attempt to defrag slab caches on other nodes. This means
1770 * scanning over all nodes to look for partial slabs which may be
1771 * expensive if we do it every time we are trying to find a slab
1772 * with available objects.
Christoph Lameter81819f02007-05-06 14:49:36 -07001773 */
Christoph Lameter98246012008-01-07 23:20:26 -08001774 if (!s->remote_node_defrag_ratio ||
1775 get_cycles() % 1024 > s->remote_node_defrag_ratio)
Christoph Lameter81819f02007-05-06 14:49:36 -07001776 return NULL;
1777
Mel Gormancc9a6c82012-03-21 16:34:11 -07001778 do {
Mel Gormand26914d2014-04-03 14:47:24 -07001779 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07001780 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001781 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
1782 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07001783
Mel Gormancc9a6c82012-03-21 16:34:11 -07001784 n = get_node(s, zone_to_nid(zone));
Christoph Lameter81819f02007-05-06 14:49:36 -07001785
Vladimir Davydovdee2f8a2014-12-12 16:58:28 -08001786 if (n && cpuset_zone_allowed(zone, flags) &&
Mel Gormancc9a6c82012-03-21 16:34:11 -07001787 n->nr_partial > s->min_partial) {
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001788 object = get_partial_node(s, n, c, flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001789 if (object) {
1790 /*
Mel Gormand26914d2014-04-03 14:47:24 -07001791 * Don't check read_mems_allowed_retry()
1792 * here - if mems_allowed was updated in
1793 * parallel, that was a harmless race
1794 * between allocation and the cpuset
1795 * update
Mel Gormancc9a6c82012-03-21 16:34:11 -07001796 */
Mel Gormancc9a6c82012-03-21 16:34:11 -07001797 return object;
1798 }
Miao Xiec0ff7452010-05-24 14:32:08 -07001799 }
Christoph Lameter81819f02007-05-06 14:49:36 -07001800 }
Mel Gormand26914d2014-04-03 14:47:24 -07001801 } while (read_mems_allowed_retry(cpuset_mems_cookie));
Christoph Lameter81819f02007-05-06 14:49:36 -07001802#endif
1803 return NULL;
1804}
1805
1806/*
1807 * Get a partial page, lock it and return it.
1808 */
Christoph Lameter497b66f2011-08-09 16:12:26 -05001809static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001810 struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07001811{
Christoph Lameter497b66f2011-08-09 16:12:26 -05001812 void *object;
Joonsoo Kima561ce02014-10-09 15:26:15 -07001813 int searchnode = node;
1814
1815 if (node == NUMA_NO_NODE)
1816 searchnode = numa_mem_id();
1817 else if (!node_present_pages(node))
1818 searchnode = node_to_mem_node(node);
Christoph Lameter81819f02007-05-06 14:49:36 -07001819
Joonsoo Kim8ba00bb2012-09-17 14:09:09 -07001820 object = get_partial_node(s, get_node(s, searchnode), c, flags);
Christoph Lameter497b66f2011-08-09 16:12:26 -05001821 if (object || node != NUMA_NO_NODE)
1822 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07001823
Christoph Lameteracd19fd2011-08-09 16:12:25 -05001824 return get_any_partial(s, flags, c);
Christoph Lameter81819f02007-05-06 14:49:36 -07001825}
1826
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001827#ifdef CONFIG_PREEMPT
1828/*
1829 * Calculate the next globally unique transaction for disambiguiation
1830 * during cmpxchg. The transactions start with the cpu number and are then
1831 * incremented by CONFIG_NR_CPUS.
1832 */
1833#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
1834#else
1835/*
1836 * No preemption supported therefore also no need to check for
1837 * different cpus.
1838 */
1839#define TID_STEP 1
1840#endif
1841
1842static inline unsigned long next_tid(unsigned long tid)
1843{
1844 return tid + TID_STEP;
1845}
1846
1847static inline unsigned int tid_to_cpu(unsigned long tid)
1848{
1849 return tid % TID_STEP;
1850}
1851
1852static inline unsigned long tid_to_event(unsigned long tid)
1853{
1854 return tid / TID_STEP;
1855}
1856
1857static inline unsigned int init_tid(int cpu)
1858{
1859 return cpu;
1860}
1861
1862static inline void note_cmpxchg_failure(const char *n,
1863 const struct kmem_cache *s, unsigned long tid)
1864{
1865#ifdef SLUB_DEBUG_CMPXCHG
1866 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
1867
Fabian Frederickf9f58282014-06-04 16:06:34 -07001868 pr_info("%s %s: cmpxchg redo ", n, s->name);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001869
1870#ifdef CONFIG_PREEMPT
1871 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
Fabian Frederickf9f58282014-06-04 16:06:34 -07001872 pr_warn("due to cpu change %d -> %d\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001873 tid_to_cpu(tid), tid_to_cpu(actual_tid));
1874 else
1875#endif
1876 if (tid_to_event(tid) != tid_to_event(actual_tid))
Fabian Frederickf9f58282014-06-04 16:06:34 -07001877 pr_warn("due to cpu running other code. Event %ld->%ld\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001878 tid_to_event(tid), tid_to_event(actual_tid));
1879 else
Fabian Frederickf9f58282014-06-04 16:06:34 -07001880 pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n",
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001881 actual_tid, tid, next_tid(tid));
1882#endif
Christoph Lameter4fdccdf2011-03-22 13:35:00 -05001883 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001884}
1885
Fengguang Wu788e1aa2012-09-28 16:34:05 +08001886static void init_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001887{
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001888 int cpu;
1889
1890 for_each_possible_cpu(cpu)
1891 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06001892}
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001893
1894/*
1895 * Remove the cpu slab
1896 */
Chen Gangd0e0ac92013-07-15 09:05:29 +08001897static void deactivate_slab(struct kmem_cache *s, struct page *page,
1898 void *freelist)
Christoph Lameter81819f02007-05-06 14:49:36 -07001899{
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001900 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001901 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1902 int lock = 0;
1903 enum slab_modes l = M_NONE, m = M_NONE;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001904 void *nextfree;
Shaohua Li136333d2011-08-24 08:57:52 +08001905 int tail = DEACTIVATE_TO_HEAD;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001906 struct page new;
1907 struct page old;
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08001908
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001909 if (page->freelist) {
Christoph Lameter84e554e62009-12-18 16:26:23 -06001910 stat(s, DEACTIVATE_REMOTE_FREES);
Shaohua Li136333d2011-08-24 08:57:52 +08001911 tail = DEACTIVATE_TO_TAIL;
Christoph Lameter894b8782007-05-10 03:15:16 -07001912 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001913
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001914 /*
1915 * Stage one: Free all available per cpu objects back
1916 * to the page freelist while it is still frozen. Leave the
1917 * last one.
1918 *
1919 * There is no need to take the list->lock because the page
1920 * is still frozen.
1921 */
1922 while (freelist && (nextfree = get_freepointer(s, freelist))) {
1923 void *prior;
1924 unsigned long counters;
1925
1926 do {
1927 prior = page->freelist;
1928 counters = page->counters;
1929 set_freepointer(s, freelist, prior);
1930 new.counters = counters;
1931 new.inuse--;
Dave Hansena0132ac2014-01-29 14:05:50 -08001932 VM_BUG_ON(!new.frozen);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001933
Christoph Lameter1d071712011-07-14 12:49:12 -05001934 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001935 prior, counters,
1936 freelist, new.counters,
1937 "drain percpu freelist"));
1938
1939 freelist = nextfree;
1940 }
1941
1942 /*
1943 * Stage two: Ensure that the page is unfrozen while the
1944 * list presence reflects the actual number of objects
1945 * during unfreeze.
1946 *
1947 * We setup the list membership and then perform a cmpxchg
1948 * with the count. If there is a mismatch then the page
1949 * is not unfrozen but the page is on the wrong list.
1950 *
1951 * Then we restart the process which may have to remove
1952 * the page from the list that we just put it on again
1953 * because the number of objects in the slab may have
1954 * changed.
1955 */
1956redo:
1957
1958 old.freelist = page->freelist;
1959 old.counters = page->counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08001960 VM_BUG_ON(!old.frozen);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001961
1962 /* Determine target state of the slab */
1963 new.counters = old.counters;
1964 if (freelist) {
1965 new.inuse--;
1966 set_freepointer(s, freelist, old.freelist);
1967 new.freelist = freelist;
1968 } else
1969 new.freelist = old.freelist;
1970
1971 new.frozen = 0;
1972
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07001973 if (!new.inuse && n->nr_partial >= s->min_partial)
Christoph Lameter2cfb7452011-06-01 12:25:52 -05001974 m = M_FREE;
1975 else if (new.freelist) {
1976 m = M_PARTIAL;
1977 if (!lock) {
1978 lock = 1;
1979 /*
1980 * Taking the spinlock removes the possiblity
1981 * that acquire_slab() will see a slab page that
1982 * is frozen
1983 */
1984 spin_lock(&n->list_lock);
1985 }
1986 } else {
1987 m = M_FULL;
1988 if (kmem_cache_debug(s) && !lock) {
1989 lock = 1;
1990 /*
1991 * This also ensures that the scanning of full
1992 * slabs from diagnostic functions will not see
1993 * any frozen slabs.
1994 */
1995 spin_lock(&n->list_lock);
1996 }
1997 }
1998
1999 if (l != m) {
2000
2001 if (l == M_PARTIAL)
2002
2003 remove_partial(n, page);
2004
2005 else if (l == M_FULL)
2006
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002007 remove_full(s, n, page);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002008
2009 if (m == M_PARTIAL) {
2010
2011 add_partial(n, page, tail);
Shaohua Li136333d2011-08-24 08:57:52 +08002012 stat(s, tail);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002013
2014 } else if (m == M_FULL) {
2015
2016 stat(s, DEACTIVATE_FULL);
2017 add_full(s, n, page);
2018
2019 }
2020 }
2021
2022 l = m;
Christoph Lameter1d071712011-07-14 12:49:12 -05002023 if (!__cmpxchg_double_slab(s, page,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002024 old.freelist, old.counters,
2025 new.freelist, new.counters,
2026 "unfreezing slab"))
2027 goto redo;
2028
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002029 if (lock)
2030 spin_unlock(&n->list_lock);
2031
2032 if (m == M_FREE) {
2033 stat(s, DEACTIVATE_EMPTY);
2034 discard_slab(s, page);
2035 stat(s, FREE_SLAB);
2036 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002037}
2038
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002039/*
2040 * Unfreeze all the cpu partial slabs.
2041 *
Christoph Lameter59a09912012-11-28 16:23:00 +00002042 * This function must be called with interrupts disabled
2043 * for the cpu using c (or some other guarantee must be there
2044 * to guarantee no concurrent accesses).
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002045 */
Christoph Lameter59a09912012-11-28 16:23:00 +00002046static void unfreeze_partials(struct kmem_cache *s,
2047 struct kmem_cache_cpu *c)
Christoph Lameter49e22582011-08-09 16:12:27 -05002048{
Joonsoo Kim345c9052013-06-19 14:05:52 +09002049#ifdef CONFIG_SLUB_CPU_PARTIAL
Joonsoo Kim43d77862012-06-09 02:23:16 +09002050 struct kmem_cache_node *n = NULL, *n2 = NULL;
Shaohua Li9ada1932011-11-14 13:34:13 +08002051 struct page *page, *discard_page = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05002052
2053 while ((page = c->partial)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002054 struct page new;
2055 struct page old;
2056
2057 c->partial = page->next;
Joonsoo Kim43d77862012-06-09 02:23:16 +09002058
2059 n2 = get_node(s, page_to_nid(page));
2060 if (n != n2) {
2061 if (n)
2062 spin_unlock(&n->list_lock);
2063
2064 n = n2;
2065 spin_lock(&n->list_lock);
2066 }
Christoph Lameter49e22582011-08-09 16:12:27 -05002067
2068 do {
2069
2070 old.freelist = page->freelist;
2071 old.counters = page->counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08002072 VM_BUG_ON(!old.frozen);
Christoph Lameter49e22582011-08-09 16:12:27 -05002073
2074 new.counters = old.counters;
2075 new.freelist = old.freelist;
2076
2077 new.frozen = 0;
2078
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002079 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter49e22582011-08-09 16:12:27 -05002080 old.freelist, old.counters,
2081 new.freelist, new.counters,
2082 "unfreezing slab"));
2083
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07002084 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) {
Shaohua Li9ada1932011-11-14 13:34:13 +08002085 page->next = discard_page;
2086 discard_page = page;
Joonsoo Kim43d77862012-06-09 02:23:16 +09002087 } else {
2088 add_partial(n, page, DEACTIVATE_TO_TAIL);
2089 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter49e22582011-08-09 16:12:27 -05002090 }
2091 }
2092
2093 if (n)
2094 spin_unlock(&n->list_lock);
Shaohua Li9ada1932011-11-14 13:34:13 +08002095
2096 while (discard_page) {
2097 page = discard_page;
2098 discard_page = discard_page->next;
2099
2100 stat(s, DEACTIVATE_EMPTY);
2101 discard_slab(s, page);
2102 stat(s, FREE_SLAB);
2103 }
Joonsoo Kim345c9052013-06-19 14:05:52 +09002104#endif
Christoph Lameter49e22582011-08-09 16:12:27 -05002105}
2106
2107/*
2108 * Put a page that was just frozen (in __slab_free) into a partial page
2109 * slot if available. This is done without interrupts disabled and without
2110 * preemption disabled. The cmpxchg is racy and may put the partial page
2111 * onto a random cpus partial slot.
2112 *
2113 * If we did not find a slot then simply move all the partials to the
2114 * per node partial list.
2115 */
Joonsoo Kim633b0762013-01-21 17:01:25 +09002116static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
Christoph Lameter49e22582011-08-09 16:12:27 -05002117{
Joonsoo Kim345c9052013-06-19 14:05:52 +09002118#ifdef CONFIG_SLUB_CPU_PARTIAL
Christoph Lameter49e22582011-08-09 16:12:27 -05002119 struct page *oldpage;
2120 int pages;
2121 int pobjects;
2122
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002123 preempt_disable();
Christoph Lameter49e22582011-08-09 16:12:27 -05002124 do {
2125 pages = 0;
2126 pobjects = 0;
2127 oldpage = this_cpu_read(s->cpu_slab->partial);
2128
2129 if (oldpage) {
2130 pobjects = oldpage->pobjects;
2131 pages = oldpage->pages;
2132 if (drain && pobjects > s->cpu_partial) {
2133 unsigned long flags;
2134 /*
2135 * partial array is full. Move the existing
2136 * set to the per node partial list.
2137 */
2138 local_irq_save(flags);
Christoph Lameter59a09912012-11-28 16:23:00 +00002139 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
Christoph Lameter49e22582011-08-09 16:12:27 -05002140 local_irq_restore(flags);
Joonsoo Kime24fc412012-06-23 03:22:38 +09002141 oldpage = NULL;
Christoph Lameter49e22582011-08-09 16:12:27 -05002142 pobjects = 0;
2143 pages = 0;
Alex Shi8028dce2012-02-03 23:34:56 +08002144 stat(s, CPU_PARTIAL_DRAIN);
Christoph Lameter49e22582011-08-09 16:12:27 -05002145 }
2146 }
2147
2148 pages++;
2149 pobjects += page->objects - page->inuse;
2150
2151 page->pages = pages;
2152 page->pobjects = pobjects;
2153 page->next = oldpage;
2154
Chen Gangd0e0ac92013-07-15 09:05:29 +08002155 } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
2156 != oldpage);
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002157 if (unlikely(!s->cpu_partial)) {
2158 unsigned long flags;
2159
2160 local_irq_save(flags);
2161 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
2162 local_irq_restore(flags);
2163 }
2164 preempt_enable();
Joonsoo Kim345c9052013-06-19 14:05:52 +09002165#endif
Christoph Lameter49e22582011-08-09 16:12:27 -05002166}
2167
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002168static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002169{
Christoph Lameter84e554e62009-12-18 16:26:23 -06002170 stat(s, CPUSLAB_FLUSH);
Christoph Lameterc17dda42012-05-09 10:09:57 -05002171 deactivate_slab(s, c->page, c->freelist);
2172
2173 c->tid = next_tid(c->tid);
2174 c->page = NULL;
2175 c->freelist = NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002176}
2177
2178/*
2179 * Flush cpu slab.
Christoph Lameter6446faa2008-02-15 23:45:26 -08002180 *
Christoph Lameter81819f02007-05-06 14:49:36 -07002181 * Called from IPI handler with interrupts disabled.
2182 */
Christoph Lameter0c710012007-07-17 04:03:24 -07002183static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
Christoph Lameter81819f02007-05-06 14:49:36 -07002184{
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002185 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
Christoph Lameter81819f02007-05-06 14:49:36 -07002186
Christoph Lameter49e22582011-08-09 16:12:27 -05002187 if (likely(c)) {
2188 if (c->page)
2189 flush_slab(s, c);
2190
Christoph Lameter59a09912012-11-28 16:23:00 +00002191 unfreeze_partials(s, c);
Christoph Lameter49e22582011-08-09 16:12:27 -05002192 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002193}
2194
2195static void flush_cpu_slab(void *d)
2196{
2197 struct kmem_cache *s = d;
Christoph Lameter81819f02007-05-06 14:49:36 -07002198
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002199 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameter81819f02007-05-06 14:49:36 -07002200}
2201
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002202static bool has_cpu_slab(int cpu, void *info)
2203{
2204 struct kmem_cache *s = info;
2205 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
2206
majianpeng02e1a9c2012-05-17 17:03:26 -07002207 return c->page || c->partial;
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002208}
2209
Christoph Lameter81819f02007-05-06 14:49:36 -07002210static void flush_all(struct kmem_cache *s)
2211{
Gilad Ben-Yossefa8364d52012-03-28 14:42:44 -07002212 on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
Christoph Lameter81819f02007-05-06 14:49:36 -07002213}
2214
2215/*
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002216 * Check if the objects in a per cpu structure fit numa
2217 * locality expectations.
2218 */
Christoph Lameter57d437d2012-05-09 10:09:59 -05002219static inline int node_match(struct page *page, int node)
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002220{
2221#ifdef CONFIG_NUMA
Christoph Lameter4d7868e2013-01-23 21:45:47 +00002222 if (!page || (node != NUMA_NO_NODE && page_to_nid(page) != node))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002223 return 0;
2224#endif
2225 return 1;
2226}
2227
David Rientjes9a02d692014-06-04 16:06:36 -07002228#ifdef CONFIG_SLUB_DEBUG
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002229static int count_free(struct page *page)
2230{
2231 return page->objects - page->inuse;
2232}
2233
David Rientjes9a02d692014-06-04 16:06:36 -07002234static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
2235{
2236 return atomic_long_read(&n->total_objects);
2237}
2238#endif /* CONFIG_SLUB_DEBUG */
2239
2240#if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS)
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002241static unsigned long count_partial(struct kmem_cache_node *n,
2242 int (*get_count)(struct page *))
2243{
2244 unsigned long flags;
2245 unsigned long x = 0;
2246 struct page *page;
2247
2248 spin_lock_irqsave(&n->list_lock, flags);
2249 list_for_each_entry(page, &n->partial, lru)
2250 x += get_count(page);
2251 spin_unlock_irqrestore(&n->list_lock, flags);
2252 return x;
2253}
David Rientjes9a02d692014-06-04 16:06:36 -07002254#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002255
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002256static noinline void
2257slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
2258{
David Rientjes9a02d692014-06-04 16:06:36 -07002259#ifdef CONFIG_SLUB_DEBUG
2260 static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
2261 DEFAULT_RATELIMIT_BURST);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002262 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002263 struct kmem_cache_node *n;
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002264
David Rientjes9a02d692014-06-04 16:06:36 -07002265 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
2266 return;
2267
Fabian Frederickf9f58282014-06-04 16:06:34 -07002268 pr_warn("SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002269 nid, gfpflags);
Fabian Frederickf9f58282014-06-04 16:06:34 -07002270 pr_warn(" cache: %s, object size: %d, buffer size: %d, default order: %d, min order: %d\n",
2271 s->name, s->object_size, s->size, oo_order(s->oo),
2272 oo_order(s->min));
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002273
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002274 if (oo_order(s->min) > get_order(s->object_size))
Fabian Frederickf9f58282014-06-04 16:06:34 -07002275 pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
2276 s->name);
David Rientjesfa5ec8a2009-07-07 00:14:14 -07002277
Christoph Lameterfa45dc22014-08-06 16:04:09 -07002278 for_each_kmem_cache_node(s, node, n) {
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002279 unsigned long nr_slabs;
2280 unsigned long nr_objs;
2281 unsigned long nr_free;
2282
Alexander Beregalov26c02cf2009-06-11 14:08:48 +04002283 nr_free = count_partial(n, count_free);
2284 nr_slabs = node_nr_slabs(n);
2285 nr_objs = node_nr_objs(n);
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002286
Fabian Frederickf9f58282014-06-04 16:06:34 -07002287 pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002288 node, nr_slabs, nr_objs, nr_free);
2289 }
David Rientjes9a02d692014-06-04 16:06:36 -07002290#endif
Pekka Enberg781b2ba2009-06-10 18:50:32 +03002291}
2292
Christoph Lameter497b66f2011-08-09 16:12:26 -05002293static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
2294 int node, struct kmem_cache_cpu **pc)
2295{
Christoph Lameter6faa6832012-05-09 10:09:51 -05002296 void *freelist;
Christoph Lameter188fd062012-05-09 10:09:55 -05002297 struct kmem_cache_cpu *c = *pc;
2298 struct page *page;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002299
Christoph Lameter188fd062012-05-09 10:09:55 -05002300 freelist = get_partial(s, flags, node, c);
2301
2302 if (freelist)
2303 return freelist;
2304
2305 page = new_slab(s, flags, node);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002306 if (page) {
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002307 c = raw_cpu_ptr(s->cpu_slab);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002308 if (c->page)
2309 flush_slab(s, c);
2310
2311 /*
2312 * No other reference to the page yet so we can
2313 * muck around with it freely without cmpxchg
2314 */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002315 freelist = page->freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002316 page->freelist = NULL;
2317
2318 stat(s, ALLOC_SLAB);
Christoph Lameter497b66f2011-08-09 16:12:26 -05002319 c->page = page;
2320 *pc = c;
2321 } else
Christoph Lameter6faa6832012-05-09 10:09:51 -05002322 freelist = NULL;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002323
Christoph Lameter6faa6832012-05-09 10:09:51 -05002324 return freelist;
Christoph Lameter497b66f2011-08-09 16:12:26 -05002325}
2326
Mel Gorman072bb0a2012-07-31 16:43:58 -07002327static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
2328{
2329 if (unlikely(PageSlabPfmemalloc(page)))
2330 return gfp_pfmemalloc_allowed(gfpflags);
2331
2332 return true;
2333}
2334
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002335/*
Chen Gangd0e0ac92013-07-15 09:05:29 +08002336 * Check the page->freelist of a page and either transfer the freelist to the
2337 * per cpu freelist or deactivate the page.
Christoph Lameter213eeb92011-11-11 14:07:14 -06002338 *
2339 * The page is still frozen if the return value is not NULL.
2340 *
2341 * If this function returns NULL then the page has been unfrozen.
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002342 *
2343 * This function must be called with interrupt disabled.
Christoph Lameter213eeb92011-11-11 14:07:14 -06002344 */
2345static inline void *get_freelist(struct kmem_cache *s, struct page *page)
2346{
2347 struct page new;
2348 unsigned long counters;
2349 void *freelist;
2350
2351 do {
2352 freelist = page->freelist;
2353 counters = page->counters;
Christoph Lameter6faa6832012-05-09 10:09:51 -05002354
Christoph Lameter213eeb92011-11-11 14:07:14 -06002355 new.counters = counters;
Dave Hansena0132ac2014-01-29 14:05:50 -08002356 VM_BUG_ON(!new.frozen);
Christoph Lameter213eeb92011-11-11 14:07:14 -06002357
2358 new.inuse = page->objects;
2359 new.frozen = freelist != NULL;
2360
Joonsoo Kimd24ac772012-05-18 22:01:17 +09002361 } while (!__cmpxchg_double_slab(s, page,
Christoph Lameter213eeb92011-11-11 14:07:14 -06002362 freelist, counters,
2363 NULL, new.counters,
2364 "get_freelist"));
2365
2366 return freelist;
2367}
2368
2369/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002370 * Slow path. The lockless freelist is empty or we need to perform
2371 * debugging duties.
Christoph Lameter81819f02007-05-06 14:49:36 -07002372 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002373 * Processing is still very fast if new objects have been freed to the
2374 * regular freelist. In that case we simply take over the regular freelist
2375 * as the lockless freelist and zap the regular freelist.
Christoph Lameter81819f02007-05-06 14:49:36 -07002376 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002377 * If that is not working then we fall back to the partial lists. We take the
2378 * first element of the freelist as the object to allocate now and move the
2379 * rest of the freelist to the lockless freelist.
2380 *
2381 * And if we were unable to get a new slab from the partial slab lists then
Christoph Lameter6446faa2008-02-15 23:45:26 -08002382 * we need to allocate a new slab. This is the slowest path since it involves
2383 * a call to the page allocator and the setup of a new slab.
Christoph Lametera380a3c2015-11-20 15:57:35 -08002384 *
2385 * Version of __slab_alloc to use when we know that interrupts are
2386 * already disabled (which is the case for bulk allocation).
Christoph Lameter81819f02007-05-06 14:49:36 -07002387 */
Christoph Lametera380a3c2015-11-20 15:57:35 -08002388static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002389 unsigned long addr, struct kmem_cache_cpu *c)
Christoph Lameter81819f02007-05-06 14:49:36 -07002390{
Christoph Lameter6faa6832012-05-09 10:09:51 -05002391 void *freelist;
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002392 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07002393
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002394 page = c->page;
2395 if (!page)
Christoph Lameter81819f02007-05-06 14:49:36 -07002396 goto new_slab;
Christoph Lameter49e22582011-08-09 16:12:27 -05002397redo:
Christoph Lameter6faa6832012-05-09 10:09:51 -05002398
Christoph Lameter57d437d2012-05-09 10:09:59 -05002399 if (unlikely(!node_match(page, node))) {
Joonsoo Kima561ce02014-10-09 15:26:15 -07002400 int searchnode = node;
2401
2402 if (node != NUMA_NO_NODE && !node_present_pages(node))
2403 searchnode = node_to_mem_node(node);
2404
2405 if (unlikely(!node_match(page, searchnode))) {
2406 stat(s, ALLOC_NODE_MISMATCH);
2407 deactivate_slab(s, page, c->freelist);
2408 c->page = NULL;
2409 c->freelist = NULL;
2410 goto new_slab;
2411 }
Christoph Lameterfc59c052011-06-01 12:25:56 -05002412 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08002413
Mel Gorman072bb0a2012-07-31 16:43:58 -07002414 /*
2415 * By rights, we should be searching for a slab page that was
2416 * PFMEMALLOC but right now, we are losing the pfmemalloc
2417 * information when the page leaves the per-cpu allocator
2418 */
2419 if (unlikely(!pfmemalloc_match(page, gfpflags))) {
2420 deactivate_slab(s, page, c->freelist);
2421 c->page = NULL;
2422 c->freelist = NULL;
2423 goto new_slab;
2424 }
2425
Eric Dumazet73736e02011-12-13 04:57:06 +01002426 /* must check again c->freelist in case of cpu migration or IRQ */
Christoph Lameter6faa6832012-05-09 10:09:51 -05002427 freelist = c->freelist;
2428 if (freelist)
Eric Dumazet73736e02011-12-13 04:57:06 +01002429 goto load_freelist;
2430
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002431 freelist = get_freelist(s, page);
Christoph Lameter6446faa2008-02-15 23:45:26 -08002432
Christoph Lameter6faa6832012-05-09 10:09:51 -05002433 if (!freelist) {
Christoph Lameter03e404a2011-06-01 12:25:58 -05002434 c->page = NULL;
2435 stat(s, DEACTIVATE_BYPASS);
Christoph Lameterfc59c052011-06-01 12:25:56 -05002436 goto new_slab;
Christoph Lameter03e404a2011-06-01 12:25:58 -05002437 }
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002438
Christoph Lameter81819f02007-05-06 14:49:36 -07002439 stat(s, ALLOC_REFILL);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08002440
Christoph Lameter894b8782007-05-10 03:15:16 -07002441load_freelist:
Christoph Lameter507effe2012-05-09 10:09:52 -05002442 /*
2443 * freelist is pointing to the list of objects to be used.
2444 * page is pointing to the page from which the objects are obtained.
2445 * That page must be frozen for per cpu allocations to work.
2446 */
Dave Hansena0132ac2014-01-29 14:05:50 -08002447 VM_BUG_ON(!c->page->frozen);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002448 c->freelist = get_freepointer(s, freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002449 c->tid = next_tid(c->tid);
Christoph Lameter6faa6832012-05-09 10:09:51 -05002450 return freelist;
Christoph Lameter81819f02007-05-06 14:49:36 -07002451
Christoph Lameter81819f02007-05-06 14:49:36 -07002452new_slab:
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002453
Christoph Lameter49e22582011-08-09 16:12:27 -05002454 if (c->partial) {
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002455 page = c->page = c->partial;
2456 c->partial = page->next;
Christoph Lameter49e22582011-08-09 16:12:27 -05002457 stat(s, CPU_PARTIAL_ALLOC);
2458 c->freelist = NULL;
2459 goto redo;
Christoph Lameter81819f02007-05-06 14:49:36 -07002460 }
2461
Christoph Lameter188fd062012-05-09 10:09:55 -05002462 freelist = new_slab_objects(s, gfpflags, node, &c);
Christoph Lameterb811c202007-10-16 23:25:51 -07002463
Christoph Lameterf46974362012-05-09 10:09:54 -05002464 if (unlikely(!freelist)) {
David Rientjes9a02d692014-06-04 16:06:36 -07002465 slab_out_of_memory(s, gfpflags, node);
Christoph Lameterf46974362012-05-09 10:09:54 -05002466 return NULL;
Christoph Lameter81819f02007-05-06 14:49:36 -07002467 }
Christoph Lameter894b8782007-05-10 03:15:16 -07002468
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002469 page = c->page;
Christoph Lameter5091b742012-07-31 16:44:00 -07002470 if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
Christoph Lameter81819f02007-05-06 14:49:36 -07002471 goto load_freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07002472
Christoph Lameter497b66f2011-08-09 16:12:26 -05002473 /* Only entered in the debug case */
Chen Gangd0e0ac92013-07-15 09:05:29 +08002474 if (kmem_cache_debug(s) &&
2475 !alloc_debug_processing(s, page, freelist, addr))
Christoph Lameter497b66f2011-08-09 16:12:26 -05002476 goto new_slab; /* Slab failed checks. Next slab needed */
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002477
Christoph Lameterf6e7def2012-05-09 10:09:58 -05002478 deactivate_slab(s, page, get_freepointer(s, freelist));
Christoph Lameterc17dda42012-05-09 10:09:57 -05002479 c->page = NULL;
2480 c->freelist = NULL;
Christoph Lameter6faa6832012-05-09 10:09:51 -05002481 return freelist;
Christoph Lameter894b8782007-05-10 03:15:16 -07002482}
2483
2484/*
Christoph Lametera380a3c2015-11-20 15:57:35 -08002485 * Another one that disabled interrupt and compensates for possible
2486 * cpu changes by refetching the per cpu area pointer.
2487 */
2488static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
2489 unsigned long addr, struct kmem_cache_cpu *c)
2490{
2491 void *p;
2492 unsigned long flags;
2493
2494 local_irq_save(flags);
2495#ifdef CONFIG_PREEMPT
2496 /*
2497 * We may have been preempted and rescheduled on a different
2498 * cpu before disabling interrupts. Need to reload cpu area
2499 * pointer.
2500 */
2501 c = this_cpu_ptr(s->cpu_slab);
2502#endif
2503
2504 p = ___slab_alloc(s, gfpflags, node, addr, c);
2505 local_irq_restore(flags);
2506 return p;
2507}
2508
2509/*
Christoph Lameter894b8782007-05-10 03:15:16 -07002510 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2511 * have the fastpath folded into their functions. So no function call
2512 * overhead for requests that can be satisfied on the fastpath.
2513 *
2514 * The fastpath works by first checking if the lockless freelist can be used.
2515 * If not then __slab_alloc is called for slow processing.
2516 *
2517 * Otherwise we can simply pick the next object from the lockless free list.
2518 */
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002519static __always_inline void *slab_alloc_node(struct kmem_cache *s,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03002520 gfp_t gfpflags, int node, unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002521{
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08002522 void *object;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002523 struct kmem_cache_cpu *c;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002524 struct page *page;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002525 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002526
Vladimir Davydov8135be52014-12-12 16:56:38 -08002527 s = slab_pre_alloc_hook(s, gfpflags);
2528 if (!s)
Akinobu Mita773ff602008-12-23 19:37:01 +09002529 return NULL;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002530redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002531 /*
2532 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2533 * enabled. We may switch back and forth between cpus while
2534 * reading from one cpu area. That does not matter as long
2535 * as we end up on the original cpu again when doing the cmpxchg.
Christoph Lameter7cccd802013-01-23 21:45:48 +00002536 *
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002537 * We should guarantee that tid and kmem_cache are retrieved on
2538 * the same cpu. It could be different if CONFIG_PREEMPT so we need
2539 * to check if it is matched or not.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002540 */
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002541 do {
2542 tid = this_cpu_read(s->cpu_slab->tid);
2543 c = raw_cpu_ptr(s->cpu_slab);
Mark Rutland859b7a02015-03-25 15:55:23 -07002544 } while (IS_ENABLED(CONFIG_PREEMPT) &&
2545 unlikely(tid != READ_ONCE(c->tid)));
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002546
2547 /*
2548 * Irqless object alloc/free algorithm used here depends on sequence
2549 * of fetching cpu_slab's data. tid should be fetched before anything
2550 * on c to guarantee that object and page associated with previous tid
2551 * won't be used with current tid. If we fetch tid first, object and
2552 * page could be one associated with next tid and our alloc/free
2553 * request will be failed. In this case, we will retry. So, no problem.
2554 */
2555 barrier();
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002556
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002557 /*
2558 * The transaction ids are globally unique per cpu and per operation on
2559 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2560 * occurs on the right processor and that there was no operation on the
2561 * linked list in between.
2562 */
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002563
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06002564 object = c->freelist;
Christoph Lameter57d437d2012-05-09 10:09:59 -05002565 page = c->page;
Dave Hansen8eae1492014-06-04 16:06:37 -07002566 if (unlikely(!object || !node_match(page, node))) {
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002567 object = __slab_alloc(s, gfpflags, node, addr, c);
Dave Hansen8eae1492014-06-04 16:06:37 -07002568 stat(s, ALLOC_SLOWPATH);
2569 } else {
Eric Dumazet0ad95002011-12-16 16:25:34 +01002570 void *next_object = get_freepointer_safe(s, object);
2571
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002572 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002573 * The cmpxchg will only match if there was no additional
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002574 * operation and if we are on the right processor.
2575 *
Chen Gangd0e0ac92013-07-15 09:05:29 +08002576 * The cmpxchg does the following atomically (without lock
2577 * semantics!)
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002578 * 1. Relocate first pointer to the current per cpu area.
2579 * 2. Verify that tid and freelist have not been changed
2580 * 3. If they were not changed replace tid and freelist
2581 *
Chen Gangd0e0ac92013-07-15 09:05:29 +08002582 * Since this is without lock semantics the protection is only
2583 * against code executing on this cpu *not* from access by
2584 * other cpus.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002585 */
Christoph Lameter933393f2011-12-22 11:58:51 -06002586 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002587 s->cpu_slab->freelist, s->cpu_slab->tid,
2588 object, tid,
Eric Dumazet0ad95002011-12-16 16:25:34 +01002589 next_object, next_tid(tid)))) {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002590
2591 note_cmpxchg_failure("slab_alloc", s, tid);
2592 goto redo;
2593 }
Eric Dumazet0ad95002011-12-16 16:25:34 +01002594 prefetch_freepointer(s, next_object);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002595 stat(s, ALLOC_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002596 }
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002597
Pekka Enberg74e21342009-11-25 20:14:48 +02002598 if (unlikely(gfpflags & __GFP_ZERO) && object)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002599 memset(object, 0, s->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07002600
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08002601 slab_post_alloc_hook(s, gfpflags, 1, &object);
Vegard Nossum5a896d92008-04-04 00:54:48 +02002602
Christoph Lameter894b8782007-05-10 03:15:16 -07002603 return object;
Christoph Lameter81819f02007-05-06 14:49:36 -07002604}
2605
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002606static __always_inline void *slab_alloc(struct kmem_cache *s,
2607 gfp_t gfpflags, unsigned long addr)
2608{
2609 return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr);
2610}
2611
Christoph Lameter81819f02007-05-06 14:49:36 -07002612void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
2613{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002614 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002615
Chen Gangd0e0ac92013-07-15 09:05:29 +08002616 trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size,
2617 s->size, gfpflags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002618
2619 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002620}
2621EXPORT_SYMBOL(kmem_cache_alloc);
2622
Li Zefan0f24f122009-12-11 15:45:30 +08002623#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002624void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002625{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002626 void *ret = slab_alloc(s, gfpflags, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002627 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08002628 kasan_kmalloc(s, ret, size);
Richard Kennedy4a923792010-10-21 10:29:19 +01002629 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002630}
Richard Kennedy4a923792010-10-21 10:29:19 +01002631EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002632#endif
2633
Christoph Lameter81819f02007-05-06 14:49:36 -07002634#ifdef CONFIG_NUMA
2635void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
2636{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002637 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002638
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002639 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002640 s->object_size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002641
2642 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07002643}
2644EXPORT_SYMBOL(kmem_cache_alloc_node);
Christoph Lameter81819f02007-05-06 14:49:36 -07002645
Li Zefan0f24f122009-12-11 15:45:30 +08002646#ifdef CONFIG_TRACING
Richard Kennedy4a923792010-10-21 10:29:19 +01002647void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002648 gfp_t gfpflags,
Richard Kennedy4a923792010-10-21 10:29:19 +01002649 int node, size_t size)
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002650{
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03002651 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
Richard Kennedy4a923792010-10-21 10:29:19 +01002652
2653 trace_kmalloc_node(_RET_IP_, ret,
2654 size, s->size, gfpflags, node);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08002655
2656 kasan_kmalloc(s, ret, size);
Richard Kennedy4a923792010-10-21 10:29:19 +01002657 return ret;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002658}
Richard Kennedy4a923792010-10-21 10:29:19 +01002659EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002660#endif
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09002661#endif
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03002662
Christoph Lameter81819f02007-05-06 14:49:36 -07002663/*
Kim Phillips94e4d712015-02-10 14:09:37 -08002664 * Slow path handling. This may still be called frequently since objects
Christoph Lameter894b8782007-05-10 03:15:16 -07002665 * have a longer lifetime than the cpu slabs in most processing loads.
Christoph Lameter81819f02007-05-06 14:49:36 -07002666 *
Christoph Lameter894b8782007-05-10 03:15:16 -07002667 * So we still attempt to reduce cache line usage. Just take the slab
2668 * lock and free the item. If there is no additional partial page
2669 * handling required then we can return immediately.
Christoph Lameter81819f02007-05-06 14:49:36 -07002670 */
Christoph Lameter894b8782007-05-10 03:15:16 -07002671static void __slab_free(struct kmem_cache *s, struct page *page,
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002672 void *head, void *tail, int cnt,
2673 unsigned long addr)
2674
Christoph Lameter81819f02007-05-06 14:49:36 -07002675{
2676 void *prior;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002677 int was_frozen;
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002678 struct page new;
2679 unsigned long counters;
2680 struct kmem_cache_node *n = NULL;
Christoph Lameter61728d12011-06-01 12:25:51 -05002681 unsigned long uninitialized_var(flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002682
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002683 stat(s, FREE_SLOWPATH);
Christoph Lameter81819f02007-05-06 14:49:36 -07002684
Christoph Lameter19c7ff92012-05-30 12:54:46 -05002685 if (kmem_cache_debug(s) &&
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002686 !(n = free_debug_processing(s, page, head, tail, cnt,
2687 addr, &flags)))
Christoph Lameter80f08c12011-06-01 12:25:55 -05002688 return;
Christoph Lameter6446faa2008-02-15 23:45:26 -08002689
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002690 do {
Joonsoo Kim837d6782012-08-16 00:02:40 +09002691 if (unlikely(n)) {
2692 spin_unlock_irqrestore(&n->list_lock, flags);
2693 n = NULL;
2694 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002695 prior = page->freelist;
2696 counters = page->counters;
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002697 set_freepointer(s, tail, prior);
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002698 new.counters = counters;
2699 was_frozen = new.frozen;
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002700 new.inuse -= cnt;
Joonsoo Kim837d6782012-08-16 00:02:40 +09002701 if ((!new.inuse || !prior) && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002702
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002703 if (kmem_cache_has_cpu_partial(s) && !prior) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002704
2705 /*
Chen Gangd0e0ac92013-07-15 09:05:29 +08002706 * Slab was on no list before and will be
2707 * partially empty
2708 * We can defer the list move and instead
2709 * freeze it.
Christoph Lameter49e22582011-08-09 16:12:27 -05002710 */
2711 new.frozen = 1;
2712
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002713 } else { /* Needs to be taken off a list */
Christoph Lameter49e22582011-08-09 16:12:27 -05002714
LQYMGTb455def2014-12-10 15:42:13 -08002715 n = get_node(s, page_to_nid(page));
Christoph Lameter49e22582011-08-09 16:12:27 -05002716 /*
2717 * Speculatively acquire the list_lock.
2718 * If the cmpxchg does not succeed then we may
2719 * drop the list_lock without any processing.
2720 *
2721 * Otherwise the list_lock will synchronize with
2722 * other processors updating the list of slabs.
2723 */
2724 spin_lock_irqsave(&n->list_lock, flags);
2725
2726 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002727 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002728
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002729 } while (!cmpxchg_double_slab(s, page,
2730 prior, counters,
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002731 head, new.counters,
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002732 "__slab_free"));
Christoph Lameter81819f02007-05-06 14:49:36 -07002733
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002734 if (likely(!n)) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002735
2736 /*
2737 * If we just froze the page then put it onto the
2738 * per cpu partial list.
2739 */
Alex Shi8028dce2012-02-03 23:34:56 +08002740 if (new.frozen && !was_frozen) {
Christoph Lameter49e22582011-08-09 16:12:27 -05002741 put_cpu_partial(s, page, 1);
Alex Shi8028dce2012-02-03 23:34:56 +08002742 stat(s, CPU_PARTIAL_FREE);
2743 }
Christoph Lameter49e22582011-08-09 16:12:27 -05002744 /*
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002745 * The list lock was not taken therefore no list
2746 * activity can be necessary.
2747 */
LQYMGTb455def2014-12-10 15:42:13 -08002748 if (was_frozen)
2749 stat(s, FREE_FROZEN);
2750 return;
2751 }
Christoph Lameter81819f02007-05-06 14:49:36 -07002752
Joonsoo Kim8a5b20a2014-07-02 15:22:35 -07002753 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
Joonsoo Kim837d6782012-08-16 00:02:40 +09002754 goto slab_empty;
Christoph Lameter81819f02007-05-06 14:49:36 -07002755
Joonsoo Kim837d6782012-08-16 00:02:40 +09002756 /*
2757 * Objects left in the slab. If it was not on the partial list before
2758 * then add it.
2759 */
Joonsoo Kim345c9052013-06-19 14:05:52 +09002760 if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) {
2761 if (kmem_cache_debug(s))
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002762 remove_full(s, n, page);
Joonsoo Kim837d6782012-08-16 00:02:40 +09002763 add_partial(n, page, DEACTIVATE_TO_TAIL);
2764 stat(s, FREE_ADD_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07002765 }
Christoph Lameter80f08c12011-06-01 12:25:55 -05002766 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -07002767 return;
2768
2769slab_empty:
Christoph Lametera973e9d2008-03-01 13:40:44 -08002770 if (prior) {
Christoph Lameter81819f02007-05-06 14:49:36 -07002771 /*
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002772 * Slab on the partial list.
Christoph Lameter81819f02007-05-06 14:49:36 -07002773 */
Christoph Lameter5cc6eee2011-06-01 12:25:50 -05002774 remove_partial(n, page);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002775 stat(s, FREE_REMOVE_PARTIAL);
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002776 } else {
Christoph Lameter6fbabb22011-08-08 11:16:56 -05002777 /* Slab must be on the full list */
Peter Zijlstrac65c1872014-01-10 13:23:49 +01002778 remove_full(s, n, page);
2779 }
Christoph Lameter2cfb7452011-06-01 12:25:52 -05002780
Christoph Lameter80f08c12011-06-01 12:25:55 -05002781 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter84e554e62009-12-18 16:26:23 -06002782 stat(s, FREE_SLAB);
Christoph Lameter81819f02007-05-06 14:49:36 -07002783 discard_slab(s, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07002784}
2785
Christoph Lameter894b8782007-05-10 03:15:16 -07002786/*
2787 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
2788 * can perform fastpath freeing without additional function calls.
2789 *
2790 * The fastpath is only possible if we are freeing to the current cpu slab
2791 * of this processor. This typically the case if we have just allocated
2792 * the item before.
2793 *
2794 * If fastpath is not possible then fall back to __slab_free where we deal
2795 * with all sorts of special processing.
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002796 *
2797 * Bulk free of a freelist with several objects (all pointing to the
2798 * same page) possible by specifying head and tail ptr, plus objects
2799 * count (cnt). Bulk free indicated by tail pointer being set.
Christoph Lameter894b8782007-05-10 03:15:16 -07002800 */
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002801static __always_inline void slab_free(struct kmem_cache *s, struct page *page,
2802 void *head, void *tail, int cnt,
2803 unsigned long addr)
Christoph Lameter894b8782007-05-10 03:15:16 -07002804{
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002805 void *tail_obj = tail ? : head;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07002806 struct kmem_cache_cpu *c;
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002807 unsigned long tid;
Christoph Lameter1f842602008-01-07 23:20:30 -08002808
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002809 slab_free_freelist_hook(s, head, tail);
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002810
Christoph Lametera24c5a02011-03-15 12:45:21 -05002811redo:
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002812 /*
2813 * Determine the currently cpus per cpu slab.
2814 * The cpu may change afterward. However that does not matter since
2815 * data is retrieved via this pointer. If we are on the same cpu
Jesper Dangaard Brouer2ae44002015-09-04 15:45:31 -07002816 * during the cmpxchg then the free will succeed.
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002817 */
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002818 do {
2819 tid = this_cpu_read(s->cpu_slab->tid);
2820 c = raw_cpu_ptr(s->cpu_slab);
Mark Rutland859b7a02015-03-25 15:55:23 -07002821 } while (IS_ENABLED(CONFIG_PREEMPT) &&
2822 unlikely(tid != READ_ONCE(c->tid)));
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002823
Joonsoo Kim9aabf812015-02-10 14:09:32 -08002824 /* Same with comment on barrier() in slab_alloc_node() */
2825 barrier();
Christoph Lameterc016b0b2010-08-20 12:37:16 -05002826
Christoph Lameter442b06b2011-05-17 16:29:31 -05002827 if (likely(page == c->page)) {
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002828 set_freepointer(s, tail_obj, c->freelist);
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002829
Christoph Lameter933393f2011-12-22 11:58:51 -06002830 if (unlikely(!this_cpu_cmpxchg_double(
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002831 s->cpu_slab->freelist, s->cpu_slab->tid,
2832 c->freelist, tid,
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002833 head, next_tid(tid)))) {
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06002834
2835 note_cmpxchg_failure("slab_free", s, tid);
2836 goto redo;
2837 }
Christoph Lameter84e554e62009-12-18 16:26:23 -06002838 stat(s, FREE_FASTPATH);
Christoph Lameter894b8782007-05-10 03:15:16 -07002839 } else
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002840 __slab_free(s, page, head, tail_obj, cnt, addr);
Christoph Lameter894b8782007-05-10 03:15:16 -07002841
Christoph Lameter894b8782007-05-10 03:15:16 -07002842}
2843
Christoph Lameter81819f02007-05-06 14:49:36 -07002844void kmem_cache_free(struct kmem_cache *s, void *x)
2845{
Glauber Costab9ce5ef2012-12-18 14:22:46 -08002846 s = cache_from_obj(s, x);
2847 if (!s)
Christoph Lameter79576102012-09-04 23:06:14 +00002848 return;
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08002849 slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02002850 trace_kmem_cache_free(_RET_IP_, x);
Christoph Lameter81819f02007-05-06 14:49:36 -07002851}
2852EXPORT_SYMBOL(kmem_cache_free);
2853
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002854struct detached_freelist {
2855 struct page *page;
2856 void *tail;
2857 void *freelist;
2858 int cnt;
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002859 struct kmem_cache *s;
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002860};
2861
2862/*
2863 * This function progressively scans the array with free objects (with
2864 * a limited look ahead) and extract objects belonging to the same
2865 * page. It builds a detached freelist directly within the given
2866 * page/objects. This can happen without any need for
2867 * synchronization, because the objects are owned by running process.
2868 * The freelist is build up as a single linked list in the objects.
2869 * The idea is, that this detached freelist can then be bulk
2870 * transferred to the real freelist(s), but only requiring a single
2871 * synchronization primitive. Look ahead in the array is limited due
2872 * to performance reasons.
2873 */
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002874static inline
2875int build_detached_freelist(struct kmem_cache *s, size_t size,
2876 void **p, struct detached_freelist *df)
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002877{
2878 size_t first_skipped_index = 0;
2879 int lookahead = 3;
2880 void *object;
2881
2882 /* Always re-init detached_freelist */
2883 df->page = NULL;
2884
2885 do {
2886 object = p[--size];
2887 } while (!object && size);
2888
2889 if (!object)
2890 return 0;
2891
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002892 /* Support for memcg, compiler can optimize this out */
2893 df->s = cache_from_obj(s, object);
2894
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002895 /* Start new detached freelist */
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002896 set_freepointer(df->s, object, NULL);
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002897 df->page = virt_to_head_page(object);
2898 df->tail = object;
2899 df->freelist = object;
2900 p[size] = NULL; /* mark object processed */
2901 df->cnt = 1;
2902
2903 while (size) {
2904 object = p[--size];
2905 if (!object)
2906 continue; /* Skip processed objects */
2907
2908 /* df->page is always set at this point */
2909 if (df->page == virt_to_head_page(object)) {
2910 /* Opportunity build freelist */
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002911 set_freepointer(df->s, object, df->freelist);
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002912 df->freelist = object;
2913 df->cnt++;
2914 p[size] = NULL; /* mark object processed */
2915
2916 continue;
2917 }
2918
2919 /* Limit look ahead search */
2920 if (!--lookahead)
2921 break;
2922
2923 if (!first_skipped_index)
2924 first_skipped_index = size + 1;
2925 }
2926
2927 return first_skipped_index;
2928}
2929
Jesper Dangaard Brouer994eb762015-09-04 15:45:37 -07002930/* Note that interrupts must be enabled when calling this function. */
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002931void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
Christoph Lameter484748f2015-09-04 15:45:34 -07002932{
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002933 if (WARN_ON(!size))
2934 return;
Jesper Dangaard Brouerfbd02632015-09-04 15:45:43 -07002935
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002936 do {
2937 struct detached_freelist df;
Jesper Dangaard Brouerfbd02632015-09-04 15:45:43 -07002938
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002939 size = build_detached_freelist(s, size, p, &df);
2940 if (unlikely(!df.page))
2941 continue;
Jesper Dangaard Brouerfbd02632015-09-04 15:45:43 -07002942
Jesper Dangaard Brouera4e25ff2016-03-15 14:53:32 -07002943 slab_free(df.s, df.page, df.freelist, df.tail, df.cnt,_RET_IP_);
Jesper Dangaard Brouerd0ecd892015-11-20 15:57:49 -08002944 } while (likely(size));
Christoph Lameter484748f2015-09-04 15:45:34 -07002945}
2946EXPORT_SYMBOL(kmem_cache_free_bulk);
2947
Jesper Dangaard Brouer994eb762015-09-04 15:45:37 -07002948/* Note that interrupts must be enabled when calling this function. */
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -08002949int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
2950 void **p)
Christoph Lameter484748f2015-09-04 15:45:34 -07002951{
Jesper Dangaard Brouer994eb762015-09-04 15:45:37 -07002952 struct kmem_cache_cpu *c;
2953 int i;
2954
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08002955 /* memcg and kmem_cache debug support */
2956 s = slab_pre_alloc_hook(s, flags);
2957 if (unlikely(!s))
2958 return false;
Jesper Dangaard Brouer994eb762015-09-04 15:45:37 -07002959 /*
2960 * Drain objects in the per cpu slab, while disabling local
2961 * IRQs, which protects against PREEMPT and interrupts
2962 * handlers invoking normal fastpath.
2963 */
2964 local_irq_disable();
2965 c = this_cpu_ptr(s->cpu_slab);
2966
2967 for (i = 0; i < size; i++) {
2968 void *object = c->freelist;
2969
Jesper Dangaard Brouerebe909e2015-09-04 15:45:40 -07002970 if (unlikely(!object)) {
Jesper Dangaard Brouerebe909e2015-09-04 15:45:40 -07002971 /*
2972 * Invoking slow path likely have side-effect
2973 * of re-populating per CPU c->freelist
2974 */
Christoph Lameter87098372015-11-20 15:57:38 -08002975 p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
Jesper Dangaard Brouerebe909e2015-09-04 15:45:40 -07002976 _RET_IP_, c);
Christoph Lameter87098372015-11-20 15:57:38 -08002977 if (unlikely(!p[i]))
2978 goto error;
2979
Jesper Dangaard Brouerebe909e2015-09-04 15:45:40 -07002980 c = this_cpu_ptr(s->cpu_slab);
2981 continue; /* goto for-loop */
2982 }
Jesper Dangaard Brouer994eb762015-09-04 15:45:37 -07002983 c->freelist = get_freepointer(s, object);
2984 p[i] = object;
2985 }
2986 c->tid = next_tid(c->tid);
2987 local_irq_enable();
2988
2989 /* Clear memory outside IRQ disabled fastpath loop */
2990 if (unlikely(flags & __GFP_ZERO)) {
2991 int j;
2992
2993 for (j = 0; j < i; j++)
2994 memset(p[j], 0, s->object_size);
2995 }
2996
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08002997 /* memcg and kmem_cache debug support */
2998 slab_post_alloc_hook(s, flags, size, p);
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -08002999 return i;
Christoph Lameter87098372015-11-20 15:57:38 -08003000error:
Christoph Lameter87098372015-11-20 15:57:38 -08003001 local_irq_enable();
Jesper Dangaard Brouer03ec0ed2015-11-20 15:57:52 -08003002 slab_post_alloc_hook(s, flags, i, p);
3003 __kmem_cache_free_bulk(s, i, p);
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -08003004 return 0;
Christoph Lameter484748f2015-09-04 15:45:34 -07003005}
3006EXPORT_SYMBOL(kmem_cache_alloc_bulk);
3007
3008
Christoph Lameter81819f02007-05-06 14:49:36 -07003009/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003010 * Object placement in a slab is made very easy because we always start at
3011 * offset 0. If we tune the size of the object to the alignment then we can
3012 * get the required alignment by putting one properly sized object after
3013 * another.
Christoph Lameter81819f02007-05-06 14:49:36 -07003014 *
3015 * Notice that the allocation order determines the sizes of the per cpu
3016 * caches. Each processor has always one slab available for allocations.
3017 * Increasing the allocation order reduces the number of times that slabs
Christoph Lameter672bba32007-05-09 02:32:39 -07003018 * must be moved on and off the partial lists and is therefore a factor in
Christoph Lameter81819f02007-05-06 14:49:36 -07003019 * locking overhead.
Christoph Lameter81819f02007-05-06 14:49:36 -07003020 */
3021
3022/*
3023 * Mininum / Maximum order of slab pages. This influences locking overhead
3024 * and slab fragmentation. A higher order reduces the number of partial slabs
3025 * and increases the number of allocations possible without having to
3026 * take the list_lock.
3027 */
3028static int slub_min_order;
Christoph Lameter114e9e82008-04-14 19:11:41 +03003029static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03003030static int slub_min_objects;
Christoph Lameter81819f02007-05-06 14:49:36 -07003031
3032/*
Christoph Lameter81819f02007-05-06 14:49:36 -07003033 * Calculate the order of allocation given an slab object size.
3034 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003035 * The order of allocation has significant impact on performance and other
3036 * system components. Generally order 0 allocations should be preferred since
3037 * order 0 does not cause fragmentation in the page allocator. Larger objects
3038 * be problematic to put into order 0 slabs because there may be too much
Christoph Lameterc124f5b2008-04-14 19:13:29 +03003039 * unused space left. We go to a higher order if more than 1/16th of the slab
Christoph Lameter672bba32007-05-09 02:32:39 -07003040 * would be wasted.
Christoph Lameter81819f02007-05-06 14:49:36 -07003041 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003042 * In order to reach satisfactory performance we must ensure that a minimum
3043 * number of objects is in one slab. Otherwise we may generate too much
3044 * activity on the partial lists which requires taking the list_lock. This is
3045 * less a concern for large slabs though which are rarely used.
Christoph Lameter81819f02007-05-06 14:49:36 -07003046 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003047 * slub_max_order specifies the order where we begin to stop considering the
3048 * number of objects in a slab as critical. If we reach slub_max_order then
3049 * we try to keep the page order as low as possible. So we accept more waste
3050 * of space in favor of a small page order.
3051 *
3052 * Higher order allocations also allow the placement of more objects in a
3053 * slab and thereby reduce object handling overhead. If the user has
3054 * requested a higher mininum order then we start with that one instead of
3055 * the smallest order which will fit the object.
Christoph Lameter81819f02007-05-06 14:49:36 -07003056 */
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003057static inline int slab_order(int size, int min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003058 int max_order, int fract_leftover, int reserved)
Christoph Lameter81819f02007-05-06 14:49:36 -07003059{
3060 int order;
3061 int rem;
Christoph Lameter6300ea72007-07-17 04:03:20 -07003062 int min_order = slub_min_order;
Christoph Lameter81819f02007-05-06 14:49:36 -07003063
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003064 if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
Cyrill Gorcunov210b5c02008-10-22 23:00:38 +04003065 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
Christoph Lameter39b26462008-04-14 19:11:30 +03003066
Wei Yang9f835702015-11-05 18:45:51 -08003067 for (order = max(min_order, get_order(min_objects * size + reserved));
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003068 order <= max_order; order++) {
3069
Christoph Lameter81819f02007-05-06 14:49:36 -07003070 unsigned long slab_size = PAGE_SIZE << order;
3071
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003072 rem = (slab_size - reserved) % size;
Christoph Lameter81819f02007-05-06 14:49:36 -07003073
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003074 if (rem <= slab_size / fract_leftover)
Christoph Lameter81819f02007-05-06 14:49:36 -07003075 break;
Christoph Lameter81819f02007-05-06 14:49:36 -07003076 }
Christoph Lameter672bba32007-05-09 02:32:39 -07003077
Christoph Lameter81819f02007-05-06 14:49:36 -07003078 return order;
3079}
3080
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003081static inline int calculate_order(int size, int reserved)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003082{
3083 int order;
3084 int min_objects;
3085 int fraction;
Zhang Yanmine8120ff2009-02-12 18:00:17 +02003086 int max_objects;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003087
3088 /*
3089 * Attempt to find best configuration for a slab. This
3090 * works by first attempting to generate a layout with
3091 * the best configuration and backing off gradually.
3092 *
Wei Yang422ff4d2015-11-05 18:45:46 -08003093 * First we increase the acceptable waste in a slab. Then
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003094 * we reduce the minimum objects required in a slab.
3095 */
3096 min_objects = slub_min_objects;
Christoph Lameter9b2cd502008-04-14 19:11:41 +03003097 if (!min_objects)
3098 min_objects = 4 * (fls(nr_cpu_ids) + 1);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003099 max_objects = order_objects(slub_max_order, size, reserved);
Zhang Yanmine8120ff2009-02-12 18:00:17 +02003100 min_objects = min(min_objects, max_objects);
3101
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003102 while (min_objects > 1) {
Christoph Lameterc124f5b2008-04-14 19:13:29 +03003103 fraction = 16;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003104 while (fraction >= 4) {
3105 order = slab_order(size, min_objects,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003106 slub_max_order, fraction, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003107 if (order <= slub_max_order)
3108 return order;
3109 fraction /= 2;
3110 }
Amerigo Wang5086c389c2009-08-19 21:44:13 +03003111 min_objects--;
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003112 }
3113
3114 /*
3115 * We were unable to place multiple objects in a slab. Now
3116 * lets see if we can place a single object there.
3117 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003118 order = slab_order(size, 1, slub_max_order, 1, reserved);
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003119 if (order <= slub_max_order)
3120 return order;
3121
3122 /*
3123 * Doh this slab cannot be placed using slub_max_order.
3124 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003125 order = slab_order(size, 1, MAX_ORDER, 1, reserved);
David Rientjes818cf592009-04-23 09:58:22 +03003126 if (order < MAX_ORDER)
Christoph Lameter5e6d4442007-05-09 02:32:46 -07003127 return order;
3128 return -ENOSYS;
3129}
3130
Pekka Enberg5595cff2008-08-05 09:28:47 +03003131static void
Joonsoo Kim40534972012-05-11 00:50:47 +09003132init_kmem_cache_node(struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07003133{
3134 n->nr_partial = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003135 spin_lock_init(&n->list_lock);
3136 INIT_LIST_HEAD(&n->partial);
Christoph Lameter8ab13722007-07-17 04:03:32 -07003137#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003138 atomic_long_set(&n->nr_slabs, 0);
Salman Qazi02b71b72008-09-11 12:25:41 -07003139 atomic_long_set(&n->total_objects, 0);
Christoph Lameter643b1132007-05-06 14:49:42 -07003140 INIT_LIST_HEAD(&n->full);
Christoph Lameter8ab13722007-07-17 04:03:32 -07003141#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003142}
3143
Christoph Lameter55136592010-08-20 12:37:13 -05003144static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003145{
Christoph Lameter6c182dc2010-08-20 12:37:14 -05003146 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
Christoph Lameter95a05b42013-01-10 19:14:19 +00003147 KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003148
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06003149 /*
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04003150 * Must align to double word boundary for the double cmpxchg
3151 * instructions to work; see __pcpu_double_call_return_bool().
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06003152 */
Chris Metcalfd4d84fe2011-06-02 10:19:41 -04003153 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
3154 2 * sizeof(void *));
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06003155
Christoph Lameter8a5ec0b2011-02-25 11:38:54 -06003156 if (!s->cpu_slab)
3157 return 0;
3158
3159 init_kmem_cache_cpus(s);
3160
3161 return 1;
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003162}
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003163
Christoph Lameter51df1142010-08-20 12:37:15 -05003164static struct kmem_cache *kmem_cache_node;
3165
Christoph Lameter81819f02007-05-06 14:49:36 -07003166/*
3167 * No kmalloc_node yet so do it by hand. We know that this is the first
3168 * slab on the node for this slabcache. There are no concurrent accesses
3169 * possible.
3170 *
Zhi Yong Wu721ae222013-11-08 20:47:37 +08003171 * Note that this function only works on the kmem_cache_node
3172 * when allocating for the kmem_cache_node. This is used for bootstrapping
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003173 * memory on a fresh node that has no slab structures yet.
Christoph Lameter81819f02007-05-06 14:49:36 -07003174 */
Christoph Lameter55136592010-08-20 12:37:13 -05003175static void early_kmem_cache_node_alloc(int node)
Christoph Lameter81819f02007-05-06 14:49:36 -07003176{
3177 struct page *page;
3178 struct kmem_cache_node *n;
3179
Christoph Lameter51df1142010-08-20 12:37:15 -05003180 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
Christoph Lameter81819f02007-05-06 14:49:36 -07003181
Christoph Lameter51df1142010-08-20 12:37:15 -05003182 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
Christoph Lameter81819f02007-05-06 14:49:36 -07003183
3184 BUG_ON(!page);
Christoph Lametera2f92ee2007-08-22 14:01:57 -07003185 if (page_to_nid(page) != node) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07003186 pr_err("SLUB: Unable to allocate memory from node %d\n", node);
3187 pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
Christoph Lametera2f92ee2007-08-22 14:01:57 -07003188 }
3189
Christoph Lameter81819f02007-05-06 14:49:36 -07003190 n = page->freelist;
3191 BUG_ON(!n);
Christoph Lameter51df1142010-08-20 12:37:15 -05003192 page->freelist = get_freepointer(kmem_cache_node, n);
Christoph Lametere6e82ea2011-08-09 16:12:24 -05003193 page->inuse = 1;
Christoph Lameter8cb0a502011-06-01 12:25:46 -05003194 page->frozen = 0;
Christoph Lameter51df1142010-08-20 12:37:15 -05003195 kmem_cache_node->node[node] = n;
Christoph Lameter8ab13722007-07-17 04:03:32 -07003196#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterf7cb1932010-09-29 07:15:01 -05003197 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
Christoph Lameter51df1142010-08-20 12:37:15 -05003198 init_tracking(kmem_cache_node, n);
Christoph Lameter8ab13722007-07-17 04:03:32 -07003199#endif
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08003200 kasan_kmalloc(kmem_cache_node, n, sizeof(struct kmem_cache_node));
Joonsoo Kim40534972012-05-11 00:50:47 +09003201 init_kmem_cache_node(n);
Christoph Lameter51df1142010-08-20 12:37:15 -05003202 inc_slabs_node(kmem_cache_node, node, page->objects);
Christoph Lameter6446faa2008-02-15 23:45:26 -08003203
Dave Hansen67b6c902014-01-24 07:20:23 -08003204 /*
Steven Rostedt1e4dd942014-02-10 14:25:46 -08003205 * No locks need to be taken here as it has just been
3206 * initialized and there is no concurrent access.
Dave Hansen67b6c902014-01-24 07:20:23 -08003207 */
Steven Rostedt1e4dd942014-02-10 14:25:46 -08003208 __add_partial(n, page, DEACTIVATE_TO_HEAD);
Christoph Lameter81819f02007-05-06 14:49:36 -07003209}
3210
3211static void free_kmem_cache_nodes(struct kmem_cache *s)
3212{
3213 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003214 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003215
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003216 for_each_kmem_cache_node(s, node, n) {
3217 kmem_cache_free(kmem_cache_node, n);
Christoph Lameter81819f02007-05-06 14:49:36 -07003218 s->node[node] = NULL;
3219 }
3220}
3221
Christoph Lameter55136592010-08-20 12:37:13 -05003222static int init_kmem_cache_nodes(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003223{
3224 int node;
Christoph Lameter81819f02007-05-06 14:49:36 -07003225
Christoph Lameterf64dc582007-10-16 01:25:33 -07003226 for_each_node_state(node, N_NORMAL_MEMORY) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003227 struct kmem_cache_node *n;
3228
Alexander Duyck73367bd2010-05-21 14:41:35 -07003229 if (slab_state == DOWN) {
Christoph Lameter55136592010-08-20 12:37:13 -05003230 early_kmem_cache_node_alloc(node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07003231 continue;
Christoph Lameter81819f02007-05-06 14:49:36 -07003232 }
Christoph Lameter51df1142010-08-20 12:37:15 -05003233 n = kmem_cache_alloc_node(kmem_cache_node,
Christoph Lameter55136592010-08-20 12:37:13 -05003234 GFP_KERNEL, node);
Alexander Duyck73367bd2010-05-21 14:41:35 -07003235
3236 if (!n) {
3237 free_kmem_cache_nodes(s);
3238 return 0;
3239 }
3240
Christoph Lameter81819f02007-05-06 14:49:36 -07003241 s->node[node] = n;
Joonsoo Kim40534972012-05-11 00:50:47 +09003242 init_kmem_cache_node(n);
Christoph Lameter81819f02007-05-06 14:49:36 -07003243 }
3244 return 1;
3245}
Christoph Lameter81819f02007-05-06 14:49:36 -07003246
David Rientjesc0bdb232009-02-25 09:16:35 +02003247static void set_min_partial(struct kmem_cache *s, unsigned long min)
David Rientjes3b89d7d2009-02-22 17:40:07 -08003248{
3249 if (min < MIN_PARTIAL)
3250 min = MIN_PARTIAL;
3251 else if (min > MAX_PARTIAL)
3252 min = MAX_PARTIAL;
3253 s->min_partial = min;
3254}
3255
Christoph Lameter81819f02007-05-06 14:49:36 -07003256/*
3257 * calculate_sizes() determines the order and the distribution of data within
3258 * a slab object.
3259 */
Christoph Lameter06b285d2008-04-14 19:11:41 +03003260static int calculate_sizes(struct kmem_cache *s, int forced_order)
Christoph Lameter81819f02007-05-06 14:49:36 -07003261{
3262 unsigned long flags = s->flags;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003263 unsigned long size = s->object_size;
Christoph Lameter834f3d12008-04-14 19:11:31 +03003264 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -07003265
3266 /*
Christoph Lameterd8b42bf2008-02-15 23:45:25 -08003267 * Round up object size to the next word boundary. We can only
3268 * place the free pointer at word boundaries and this determines
3269 * the possible location of the free pointer.
3270 */
3271 size = ALIGN(size, sizeof(void *));
3272
3273#ifdef CONFIG_SLUB_DEBUG
3274 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07003275 * Determine if we can poison the object itself. If the user of
3276 * the slab may touch the object after free or before allocation
3277 * then we should never poison the object itself.
3278 */
3279 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
Christoph Lameterc59def92007-05-16 22:10:50 -07003280 !s->ctor)
Christoph Lameter81819f02007-05-06 14:49:36 -07003281 s->flags |= __OBJECT_POISON;
3282 else
3283 s->flags &= ~__OBJECT_POISON;
3284
Christoph Lameter81819f02007-05-06 14:49:36 -07003285
3286 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003287 * If we are Redzoning then check if there is some space between the
Christoph Lameter81819f02007-05-06 14:49:36 -07003288 * end of the object and the free pointer. If not then add an
Christoph Lameter672bba32007-05-09 02:32:39 -07003289 * additional word to have some bytes to store Redzone information.
Christoph Lameter81819f02007-05-06 14:49:36 -07003290 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003291 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
Christoph Lameter81819f02007-05-06 14:49:36 -07003292 size += sizeof(void *);
Christoph Lameter41ecc552007-05-09 02:32:44 -07003293#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07003294
3295 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07003296 * With that we have determined the number of bytes in actual use
3297 * by the object. This is the potential offset to the free pointer.
Christoph Lameter81819f02007-05-06 14:49:36 -07003298 */
3299 s->inuse = size;
3300
3301 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
Christoph Lameterc59def92007-05-16 22:10:50 -07003302 s->ctor)) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003303 /*
3304 * Relocate free pointer after the object if it is not
3305 * permitted to overwrite the first word of the object on
3306 * kmem_cache_free.
3307 *
3308 * This is the case if we do RCU, have a constructor or
3309 * destructor or are poisoning the objects.
3310 */
3311 s->offset = size;
3312 size += sizeof(void *);
3313 }
3314
Christoph Lameterc12b3c62007-05-23 13:57:31 -07003315#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07003316 if (flags & SLAB_STORE_USER)
3317 /*
3318 * Need to store information about allocs and frees after
3319 * the object.
3320 */
3321 size += 2 * sizeof(struct track);
3322
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -07003323 if (flags & SLAB_RED_ZONE) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003324 /*
3325 * Add some empty padding so that we can catch
3326 * overwrites from earlier objects rather than let
3327 * tracking information or the free pointer be
Frederik Schwarzer0211a9c2008-12-29 22:14:56 +01003328 * corrupted if a user writes before the start
Christoph Lameter81819f02007-05-06 14:49:36 -07003329 * of the object.
3330 */
3331 size += sizeof(void *);
Joonsoo Kimdacbe1f2016-03-15 14:55:12 -07003332
3333 s->red_left_pad = sizeof(void *);
3334 s->red_left_pad = ALIGN(s->red_left_pad, s->align);
3335 size += s->red_left_pad;
3336 }
Christoph Lameter41ecc552007-05-09 02:32:44 -07003337#endif
Christoph Lameter672bba32007-05-09 02:32:39 -07003338
Christoph Lameter81819f02007-05-06 14:49:36 -07003339 /*
Christoph Lameter81819f02007-05-06 14:49:36 -07003340 * SLUB stores one object immediately after another beginning from
3341 * offset 0. In order to align the objects we have to simply size
3342 * each object to conform to the alignment.
3343 */
Christoph Lameter45906852012-11-28 16:23:16 +00003344 size = ALIGN(size, s->align);
Christoph Lameter81819f02007-05-06 14:49:36 -07003345 s->size = size;
Christoph Lameter06b285d2008-04-14 19:11:41 +03003346 if (forced_order >= 0)
3347 order = forced_order;
3348 else
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003349 order = calculate_order(size, s->reserved);
Christoph Lameter81819f02007-05-06 14:49:36 -07003350
Christoph Lameter834f3d12008-04-14 19:11:31 +03003351 if (order < 0)
Christoph Lameter81819f02007-05-06 14:49:36 -07003352 return 0;
3353
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003354 s->allocflags = 0;
Christoph Lameter834f3d12008-04-14 19:11:31 +03003355 if (order)
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003356 s->allocflags |= __GFP_COMP;
3357
3358 if (s->flags & SLAB_CACHE_DMA)
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003359 s->allocflags |= GFP_DMA;
Christoph Lameterb7a49f02008-02-14 14:21:32 -08003360
3361 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3362 s->allocflags |= __GFP_RECLAIMABLE;
3363
Christoph Lameter81819f02007-05-06 14:49:36 -07003364 /*
3365 * Determine the number of objects per slab
3366 */
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003367 s->oo = oo_make(order, size, s->reserved);
3368 s->min = oo_make(get_order(size), size, s->reserved);
Christoph Lameter205ab992008-04-14 19:11:40 +03003369 if (oo_objects(s->oo) > oo_objects(s->max))
3370 s->max = s->oo;
Christoph Lameter81819f02007-05-06 14:49:36 -07003371
Christoph Lameter834f3d12008-04-14 19:11:31 +03003372 return !!oo_objects(s->oo);
Christoph Lameter81819f02007-05-06 14:49:36 -07003373}
3374
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003375static int kmem_cache_open(struct kmem_cache *s, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07003376{
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003377 s->flags = kmem_cache_flags(s->size, flags, s->name, s->ctor);
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08003378 s->reserved = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07003379
Lai Jiangshanda9a6382011-03-10 15:22:00 +08003380 if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
3381 s->reserved = sizeof(struct rcu_head);
Christoph Lameter81819f02007-05-06 14:49:36 -07003382
Christoph Lameter06b285d2008-04-14 19:11:41 +03003383 if (!calculate_sizes(s, -1))
Christoph Lameter81819f02007-05-06 14:49:36 -07003384 goto error;
David Rientjes3de472132009-07-27 18:30:35 -07003385 if (disable_higher_order_debug) {
3386 /*
3387 * Disable debugging flags that store metadata if the min slab
3388 * order increased.
3389 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003390 if (get_order(s->size) > get_order(s->object_size)) {
David Rientjes3de472132009-07-27 18:30:35 -07003391 s->flags &= ~DEBUG_METADATA_FLAGS;
3392 s->offset = 0;
3393 if (!calculate_sizes(s, -1))
3394 goto error;
3395 }
3396 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003397
Heiko Carstens25654092012-01-12 17:17:33 -08003398#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
3399 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
Christoph Lameterb789ef52011-06-01 12:25:49 -05003400 if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
3401 /* Enable fast mode */
3402 s->flags |= __CMPXCHG_DOUBLE;
3403#endif
3404
David Rientjes3b89d7d2009-02-22 17:40:07 -08003405 /*
3406 * The larger the object size is, the more pages we want on the partial
3407 * list to avoid pounding the page allocator excessively.
3408 */
Christoph Lameter49e22582011-08-09 16:12:27 -05003409 set_min_partial(s, ilog2(s->size) / 2);
3410
3411 /*
3412 * cpu_partial determined the maximum number of objects kept in the
3413 * per cpu partial lists of a processor.
3414 *
3415 * Per cpu partial lists mainly contain slabs that just have one
3416 * object freed. If they are used for allocation then they can be
3417 * filled up again with minimal effort. The slab will never hit the
3418 * per node partial lists and therefore no locking will be required.
3419 *
3420 * This setting also determines
3421 *
3422 * A) The number of objects from per cpu partial slabs dumped to the
3423 * per node list when we reach the limit.
Alex Shi9f264902011-09-01 11:32:18 +08003424 * B) The number of objects in cpu partial slabs to extract from the
Chen Gangd0e0ac92013-07-15 09:05:29 +08003425 * per node list when we run out of per cpu objects. We only fetch
3426 * 50% to keep some capacity around for frees.
Christoph Lameter49e22582011-08-09 16:12:27 -05003427 */
Joonsoo Kim345c9052013-06-19 14:05:52 +09003428 if (!kmem_cache_has_cpu_partial(s))
Christoph Lameter8f1e33d2011-11-23 09:24:27 -06003429 s->cpu_partial = 0;
3430 else if (s->size >= PAGE_SIZE)
Christoph Lameter49e22582011-08-09 16:12:27 -05003431 s->cpu_partial = 2;
3432 else if (s->size >= 1024)
3433 s->cpu_partial = 6;
3434 else if (s->size >= 256)
3435 s->cpu_partial = 13;
3436 else
3437 s->cpu_partial = 30;
3438
Christoph Lameter81819f02007-05-06 14:49:36 -07003439#ifdef CONFIG_NUMA
Christoph Lametere2cb96b2008-08-19 08:51:22 -05003440 s->remote_node_defrag_ratio = 1000;
Christoph Lameter81819f02007-05-06 14:49:36 -07003441#endif
Christoph Lameter55136592010-08-20 12:37:13 -05003442 if (!init_kmem_cache_nodes(s))
Christoph Lameterdfb4f092007-10-16 01:26:05 -07003443 goto error;
Christoph Lameter81819f02007-05-06 14:49:36 -07003444
Christoph Lameter55136592010-08-20 12:37:13 -05003445 if (alloc_kmem_cache_cpus(s))
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003446 return 0;
Christoph Lameterff120592009-12-18 16:26:22 -06003447
Christoph Lameter4c93c3552007-10-16 01:26:08 -07003448 free_kmem_cache_nodes(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003449error:
3450 if (flags & SLAB_PANIC)
3451 panic("Cannot create slab %s size=%lu realsize=%u "
3452 "order=%u offset=%u flags=%lx\n",
Chen Gangd0e0ac92013-07-15 09:05:29 +08003453 s->name, (unsigned long)s->size, s->size,
3454 oo_order(s->oo), s->offset, flags);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00003455 return -EINVAL;
Christoph Lameter81819f02007-05-06 14:49:36 -07003456}
Christoph Lameter81819f02007-05-06 14:49:36 -07003457
Christoph Lameter33b12c32008-04-25 12:22:43 -07003458static void list_slab_objects(struct kmem_cache *s, struct page *page,
3459 const char *text)
Christoph Lameter81819f02007-05-06 14:49:36 -07003460{
Christoph Lameter33b12c32008-04-25 12:22:43 -07003461#ifdef CONFIG_SLUB_DEBUG
3462 void *addr = page_address(page);
3463 void *p;
Namhyung Kima5dd5c12010-09-29 21:02:13 +09003464 unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
3465 sizeof(long), GFP_ATOMIC);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003466 if (!map)
3467 return;
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003468 slab_err(s, page, text, s->name);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003469 slab_lock(page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003470
Christoph Lameter5f80b132011-04-15 14:48:13 -05003471 get_map(s, page, map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003472 for_each_object(p, s, addr, page->objects) {
3473
3474 if (!test_bit(slab_index(p, s, addr), map)) {
Fabian Frederickf9f58282014-06-04 16:06:34 -07003475 pr_err("INFO: Object 0x%p @offset=%tu\n", p, p - addr);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003476 print_tracking(s, p);
3477 }
3478 }
3479 slab_unlock(page);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01003480 kfree(map);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003481#endif
3482}
3483
Christoph Lameter81819f02007-05-06 14:49:36 -07003484/*
Christoph Lameter599870b2008-04-23 12:36:52 -07003485 * Attempt to free all partial slabs on a node.
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003486 * This is called from kmem_cache_close(). We must be the last thread
3487 * using the cache and therefore we do not need to lock anymore.
Christoph Lameter81819f02007-05-06 14:49:36 -07003488 */
Christoph Lameter599870b2008-04-23 12:36:52 -07003489static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
Christoph Lameter81819f02007-05-06 14:49:36 -07003490{
Christoph Lameter81819f02007-05-06 14:49:36 -07003491 struct page *page, *h;
3492
Christoph Lameter33b12c32008-04-25 12:22:43 -07003493 list_for_each_entry_safe(page, h, &n->partial, lru) {
Christoph Lameter81819f02007-05-06 14:49:36 -07003494 if (!page->inuse) {
Steven Rostedt1e4dd942014-02-10 14:25:46 -08003495 __remove_partial(n, page);
Christoph Lameter81819f02007-05-06 14:49:36 -07003496 discard_slab(s, page);
Christoph Lameter33b12c32008-04-25 12:22:43 -07003497 } else {
3498 list_slab_objects(s, page,
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003499 "Objects remaining in %s on kmem_cache_close()");
Christoph Lameter599870b2008-04-23 12:36:52 -07003500 }
Christoph Lameter33b12c32008-04-25 12:22:43 -07003501 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003502}
3503
3504/*
Christoph Lameter672bba32007-05-09 02:32:39 -07003505 * Release all resources used by a slab cache.
Christoph Lameter81819f02007-05-06 14:49:36 -07003506 */
Christoph Lameter0c710012007-07-17 04:03:24 -07003507static inline int kmem_cache_close(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003508{
3509 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003510 struct kmem_cache_node *n;
Christoph Lameter81819f02007-05-06 14:49:36 -07003511
3512 flush_all(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003513 /* Attempt to free all objects */
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003514 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter599870b2008-04-23 12:36:52 -07003515 free_partial(s, n);
3516 if (n->nr_partial || slabs_node(s, node))
Christoph Lameter81819f02007-05-06 14:49:36 -07003517 return 1;
3518 }
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003519 free_percpu(s->cpu_slab);
Christoph Lameter81819f02007-05-06 14:49:36 -07003520 free_kmem_cache_nodes(s);
3521 return 0;
3522}
3523
Christoph Lameter945cf2b2012-09-04 23:18:33 +00003524int __kmem_cache_shutdown(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07003525{
Christoph Lameter41a21282014-05-06 12:50:08 -07003526 return kmem_cache_close(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07003527}
Christoph Lameter81819f02007-05-06 14:49:36 -07003528
3529/********************************************************************
3530 * Kmalloc subsystem
3531 *******************************************************************/
3532
Christoph Lameter81819f02007-05-06 14:49:36 -07003533static int __init setup_slub_min_order(char *str)
3534{
Pekka Enberg06428782008-01-07 23:20:27 -08003535 get_option(&str, &slub_min_order);
Christoph Lameter81819f02007-05-06 14:49:36 -07003536
3537 return 1;
3538}
3539
3540__setup("slub_min_order=", setup_slub_min_order);
3541
3542static int __init setup_slub_max_order(char *str)
3543{
Pekka Enberg06428782008-01-07 23:20:27 -08003544 get_option(&str, &slub_max_order);
David Rientjes818cf592009-04-23 09:58:22 +03003545 slub_max_order = min(slub_max_order, MAX_ORDER - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07003546
3547 return 1;
3548}
3549
3550__setup("slub_max_order=", setup_slub_max_order);
3551
3552static int __init setup_slub_min_objects(char *str)
3553{
Pekka Enberg06428782008-01-07 23:20:27 -08003554 get_option(&str, &slub_min_objects);
Christoph Lameter81819f02007-05-06 14:49:36 -07003555
3556 return 1;
3557}
3558
3559__setup("slub_min_objects=", setup_slub_min_objects);
3560
Christoph Lameter81819f02007-05-06 14:49:36 -07003561void *__kmalloc(size_t size, gfp_t flags)
3562{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003563 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003564 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003565
Christoph Lameter95a05b42013-01-10 19:14:19 +00003566 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02003567 return kmalloc_large(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003568
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003569 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003570
3571 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003572 return s;
3573
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003574 ret = slab_alloc(s, flags, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003575
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003576 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003577
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08003578 kasan_kmalloc(s, ret, size);
3579
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003580 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003581}
3582EXPORT_SYMBOL(__kmalloc);
3583
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09003584#ifdef CONFIG_NUMA
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003585static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
3586{
Vegard Nossumb1eeab62008-11-25 16:55:53 +01003587 struct page *page;
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003588 void *ptr = NULL;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003589
Vladimir Davydov52383432014-06-04 16:06:39 -07003590 flags |= __GFP_COMP | __GFP_NOTRACK;
3591 page = alloc_kmem_pages_node(node, flags, get_order(size));
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003592 if (page)
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003593 ptr = page_address(page);
3594
Roman Bobnievd56791b2013-10-08 15:58:57 -07003595 kmalloc_large_node_hook(ptr, size, flags);
Catalin Marinase4f7c0b42009-07-07 10:32:59 +01003596 return ptr;
Christoph Lameterf619cfe2008-03-01 13:56:40 -08003597}
3598
Christoph Lameter81819f02007-05-06 14:49:36 -07003599void *__kmalloc_node(size_t size, gfp_t flags, int node)
3600{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003601 struct kmem_cache *s;
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003602 void *ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003603
Christoph Lameter95a05b42013-01-10 19:14:19 +00003604 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003605 ret = kmalloc_large_node(size, flags, node);
3606
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003607 trace_kmalloc_node(_RET_IP_, ret,
3608 size, PAGE_SIZE << get_order(size),
3609 flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003610
3611 return ret;
3612 }
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003613
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003614 s = kmalloc_slab(size, flags);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003615
3616 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003617 return s;
3618
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03003619 ret = slab_alloc_node(s, flags, node, _RET_IP_);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003620
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003621 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003622
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08003623 kasan_kmalloc(s, ret, size);
3624
Eduard - Gabriel Munteanu5b882be2008-08-19 20:43:26 +03003625 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07003626}
3627EXPORT_SYMBOL(__kmalloc_node);
3628#endif
3629
Kees Cook3ad78ba2016-06-23 15:24:05 -07003630#ifdef CONFIG_HARDENED_USERCOPY
3631/*
3632 * Rejects objects that are incorrectly sized.
3633 *
3634 * Returns NULL if check passes, otherwise const char * to name of cache
3635 * to indicate an error.
3636 */
3637const char *__check_heap_object(const void *ptr, unsigned long n,
3638 struct page *page)
3639{
3640 struct kmem_cache *s;
3641 unsigned long offset;
3642 size_t object_size;
3643
3644 /* Find object and usable object size. */
3645 s = page->slab_cache;
3646 object_size = slab_ksize(s);
3647
3648 /* Reject impossible pointers. */
3649 if (ptr < page_address(page))
3650 return s->name;
3651
3652 /* Find offset within object. */
3653 offset = (ptr - page_address(page)) % s->size;
3654
3655 /* Adjust for redzone and reject if within the redzone. */
3656 if (kmem_cache_debug(s) && s->flags & SLAB_RED_ZONE) {
3657 if (offset < s->red_left_pad)
3658 return s->name;
3659 offset -= s->red_left_pad;
3660 }
3661
3662 /* Allow address range falling entirely within object size. */
3663 if (offset <= object_size && n <= object_size - offset)
3664 return NULL;
3665
3666 return s->name;
3667}
3668#endif /* CONFIG_HARDENED_USERCOPY */
3669
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08003670static size_t __ksize(const void *object)
Christoph Lameter81819f02007-05-06 14:49:36 -07003671{
Christoph Lameter272c1d22007-06-08 13:46:49 -07003672 struct page *page;
Christoph Lameter81819f02007-05-06 14:49:36 -07003673
Christoph Lameteref8b4522007-10-16 01:24:46 -07003674 if (unlikely(object == ZERO_SIZE_PTR))
Christoph Lameter272c1d22007-06-08 13:46:49 -07003675 return 0;
3676
Vegard Nossum294a80a2007-12-04 23:45:30 -08003677 page = virt_to_head_page(object);
Vegard Nossum294a80a2007-12-04 23:45:30 -08003678
Pekka Enberg76994412008-05-22 19:22:25 +03003679 if (unlikely(!PageSlab(page))) {
3680 WARN_ON(!PageCompound(page));
Vegard Nossum294a80a2007-12-04 23:45:30 -08003681 return PAGE_SIZE << compound_order(page);
Pekka Enberg76994412008-05-22 19:22:25 +03003682 }
Christoph Lameter81819f02007-05-06 14:49:36 -07003683
Glauber Costa1b4f59e32012-10-22 18:05:36 +04003684 return slab_ksize(page->slab_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003685}
Andrey Ryabinin0316bec2015-02-13 14:39:42 -08003686
3687size_t ksize(const void *object)
3688{
3689 size_t size = __ksize(object);
3690 /* We assume that ksize callers could use whole allocated area,
3691 so we need unpoison this area. */
3692 kasan_krealloc(object, size);
3693 return size;
3694}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02003695EXPORT_SYMBOL(ksize);
Christoph Lameter81819f02007-05-06 14:49:36 -07003696
3697void kfree(const void *x)
3698{
Christoph Lameter81819f02007-05-06 14:49:36 -07003699 struct page *page;
Christoph Lameter5bb983b2008-02-07 17:47:41 -08003700 void *object = (void *)x;
Christoph Lameter81819f02007-05-06 14:49:36 -07003701
Pekka Enberg2121db72009-03-25 11:05:57 +02003702 trace_kfree(_RET_IP_, x);
3703
Satyam Sharma2408c552007-10-16 01:24:44 -07003704 if (unlikely(ZERO_OR_NULL_PTR(x)))
Christoph Lameter81819f02007-05-06 14:49:36 -07003705 return;
3706
Christoph Lameterb49af682007-05-06 14:49:41 -07003707 page = virt_to_head_page(x);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003708 if (unlikely(!PageSlab(page))) {
Christoph Lameter09375022008-05-28 10:32:22 -07003709 BUG_ON(!PageCompound(page));
Roman Bobnievd56791b2013-10-08 15:58:57 -07003710 kfree_hook(x);
Vladimir Davydov52383432014-06-04 16:06:39 -07003711 __free_kmem_pages(page, compound_order(page));
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07003712 return;
3713 }
Jesper Dangaard Brouer81084652015-11-20 15:57:46 -08003714 slab_free(page->slab_cache, page, object, NULL, 1, _RET_IP_);
Christoph Lameter81819f02007-05-06 14:49:36 -07003715}
3716EXPORT_SYMBOL(kfree);
3717
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003718#define SHRINK_PROMOTE_MAX 32
3719
Christoph Lameter2086d262007-05-06 14:49:46 -07003720/*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003721 * kmem_cache_shrink discards empty slabs and promotes the slabs filled
3722 * up most to the head of the partial lists. New allocations will then
3723 * fill those up and thus they can be removed from the partial lists.
Christoph Lameter672bba32007-05-09 02:32:39 -07003724 *
3725 * The slabs with the least items are placed last. This results in them
3726 * being allocated from last increasing the chance that the last objects
3727 * are freed in them.
Christoph Lameter2086d262007-05-06 14:49:46 -07003728 */
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003729int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
Christoph Lameter2086d262007-05-06 14:49:46 -07003730{
3731 int node;
3732 int i;
3733 struct kmem_cache_node *n;
3734 struct page *page;
3735 struct page *t;
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003736 struct list_head discard;
3737 struct list_head promote[SHRINK_PROMOTE_MAX];
Christoph Lameter2086d262007-05-06 14:49:46 -07003738 unsigned long flags;
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003739 int ret = 0;
Christoph Lameter2086d262007-05-06 14:49:46 -07003740
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003741 if (deactivate) {
3742 /*
3743 * Disable empty slabs caching. Used to avoid pinning offline
3744 * memory cgroups by kmem pages that can be freed.
3745 */
3746 s->cpu_partial = 0;
3747 s->min_partial = 0;
3748
3749 /*
3750 * s->cpu_partial is checked locklessly (see put_cpu_partial),
3751 * so we have to make sure the change is visible.
3752 */
3753 kick_all_cpus_sync();
3754 }
3755
Christoph Lameter2086d262007-05-06 14:49:46 -07003756 flush_all(s);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003757 for_each_kmem_cache_node(s, node, n) {
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003758 INIT_LIST_HEAD(&discard);
3759 for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
3760 INIT_LIST_HEAD(promote + i);
Christoph Lameter2086d262007-05-06 14:49:46 -07003761
3762 spin_lock_irqsave(&n->list_lock, flags);
3763
3764 /*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003765 * Build lists of slabs to discard or promote.
Christoph Lameter2086d262007-05-06 14:49:46 -07003766 *
Christoph Lameter672bba32007-05-09 02:32:39 -07003767 * Note that concurrent frees may occur while we hold the
3768 * list_lock. page->inuse here is the upper limit.
Christoph Lameter2086d262007-05-06 14:49:46 -07003769 */
3770 list_for_each_entry_safe(page, t, &n->partial, lru) {
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003771 int free = page->objects - page->inuse;
3772
3773 /* Do not reread page->inuse */
3774 barrier();
3775
3776 /* We do not keep full slabs on the list */
3777 BUG_ON(free <= 0);
3778
3779 if (free == page->objects) {
3780 list_move(&page->lru, &discard);
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003781 n->nr_partial--;
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003782 } else if (free <= SHRINK_PROMOTE_MAX)
3783 list_move(&page->lru, promote + free - 1);
Christoph Lameter2086d262007-05-06 14:49:46 -07003784 }
3785
Christoph Lameter2086d262007-05-06 14:49:46 -07003786 /*
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003787 * Promote the slabs filled up most to the head of the
3788 * partial list.
Christoph Lameter2086d262007-05-06 14:49:46 -07003789 */
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003790 for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
3791 list_splice(promote + i, &n->partial);
Christoph Lameter2086d262007-05-06 14:49:46 -07003792
Christoph Lameter2086d262007-05-06 14:49:46 -07003793 spin_unlock_irqrestore(&n->list_lock, flags);
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003794
3795 /* Release empty slabs */
Vladimir Davydov832f37f2015-02-12 14:59:41 -08003796 list_for_each_entry_safe(page, t, &discard, lru)
Christoph Lameter69cb8e62011-08-09 16:12:22 -05003797 discard_slab(s, page);
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003798
3799 if (slabs_node(s, node))
3800 ret = 1;
Christoph Lameter2086d262007-05-06 14:49:46 -07003801 }
3802
Vladimir Davydovce3712d2015-02-12 14:59:44 -08003803 return ret;
Christoph Lameter2086d262007-05-06 14:49:46 -07003804}
Christoph Lameter2086d262007-05-06 14:49:46 -07003805
Yasunori Gotob9049e22007-10-21 16:41:37 -07003806static int slab_mem_going_offline_callback(void *arg)
3807{
3808 struct kmem_cache *s;
3809
Christoph Lameter18004c52012-07-06 15:25:12 -05003810 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003811 list_for_each_entry(s, &slab_caches, list)
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08003812 __kmem_cache_shrink(s, false);
Christoph Lameter18004c52012-07-06 15:25:12 -05003813 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003814
3815 return 0;
3816}
3817
3818static void slab_mem_offline_callback(void *arg)
3819{
3820 struct kmem_cache_node *n;
3821 struct kmem_cache *s;
3822 struct memory_notify *marg = arg;
3823 int offline_node;
3824
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003825 offline_node = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003826
3827 /*
3828 * If the node still has available memory. we need kmem_cache_node
3829 * for it yet.
3830 */
3831 if (offline_node < 0)
3832 return;
3833
Christoph Lameter18004c52012-07-06 15:25:12 -05003834 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003835 list_for_each_entry(s, &slab_caches, list) {
3836 n = get_node(s, offline_node);
3837 if (n) {
3838 /*
3839 * if n->nr_slabs > 0, slabs still exist on the node
3840 * that is going down. We were unable to free them,
Adam Buchbinderc9404c92009-12-18 15:40:42 -05003841 * and offline_pages() function shouldn't call this
Yasunori Gotob9049e22007-10-21 16:41:37 -07003842 * callback. So, we must fail.
3843 */
Christoph Lameter0f389ec2008-04-14 18:53:02 +03003844 BUG_ON(slabs_node(s, offline_node));
Yasunori Gotob9049e22007-10-21 16:41:37 -07003845
3846 s->node[offline_node] = NULL;
Christoph Lameter8de66a02010-08-25 14:51:14 -05003847 kmem_cache_free(kmem_cache_node, n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003848 }
3849 }
Christoph Lameter18004c52012-07-06 15:25:12 -05003850 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003851}
3852
3853static int slab_mem_going_online_callback(void *arg)
3854{
3855 struct kmem_cache_node *n;
3856 struct kmem_cache *s;
3857 struct memory_notify *marg = arg;
Lai Jiangshanb9d5ab22012-12-11 16:01:05 -08003858 int nid = marg->status_change_nid_normal;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003859 int ret = 0;
3860
3861 /*
3862 * If the node's memory is already available, then kmem_cache_node is
3863 * already created. Nothing to do.
3864 */
3865 if (nid < 0)
3866 return 0;
3867
3868 /*
Christoph Lameter0121c6192008-04-29 16:11:12 -07003869 * We are bringing a node online. No memory is available yet. We must
Yasunori Gotob9049e22007-10-21 16:41:37 -07003870 * allocate a kmem_cache_node structure in order to bring the node
3871 * online.
3872 */
Christoph Lameter18004c52012-07-06 15:25:12 -05003873 mutex_lock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003874 list_for_each_entry(s, &slab_caches, list) {
3875 /*
3876 * XXX: kmem_cache_alloc_node will fallback to other nodes
3877 * since memory is not yet available from the node that
3878 * is brought up.
3879 */
Christoph Lameter8de66a02010-08-25 14:51:14 -05003880 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003881 if (!n) {
3882 ret = -ENOMEM;
3883 goto out;
3884 }
Joonsoo Kim40534972012-05-11 00:50:47 +09003885 init_kmem_cache_node(n);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003886 s->node[nid] = n;
3887 }
3888out:
Christoph Lameter18004c52012-07-06 15:25:12 -05003889 mutex_unlock(&slab_mutex);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003890 return ret;
3891}
3892
3893static int slab_memory_callback(struct notifier_block *self,
3894 unsigned long action, void *arg)
3895{
3896 int ret = 0;
3897
3898 switch (action) {
3899 case MEM_GOING_ONLINE:
3900 ret = slab_mem_going_online_callback(arg);
3901 break;
3902 case MEM_GOING_OFFLINE:
3903 ret = slab_mem_going_offline_callback(arg);
3904 break;
3905 case MEM_OFFLINE:
3906 case MEM_CANCEL_ONLINE:
3907 slab_mem_offline_callback(arg);
3908 break;
3909 case MEM_ONLINE:
3910 case MEM_CANCEL_OFFLINE:
3911 break;
3912 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -08003913 if (ret)
3914 ret = notifier_from_errno(ret);
3915 else
3916 ret = NOTIFY_OK;
Yasunori Gotob9049e22007-10-21 16:41:37 -07003917 return ret;
3918}
3919
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003920static struct notifier_block slab_memory_callback_nb = {
3921 .notifier_call = slab_memory_callback,
3922 .priority = SLAB_CALLBACK_PRI,
3923};
Yasunori Gotob9049e22007-10-21 16:41:37 -07003924
Christoph Lameter81819f02007-05-06 14:49:36 -07003925/********************************************************************
3926 * Basic setup of slabs
3927 *******************************************************************/
3928
Christoph Lameter51df1142010-08-20 12:37:15 -05003929/*
3930 * Used for early kmem_cache structures that were allocated using
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003931 * the page allocator. Allocate them properly then fix up the pointers
3932 * that may be pointing to the wrong kmem_cache structure.
Christoph Lameter51df1142010-08-20 12:37:15 -05003933 */
3934
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003935static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
Christoph Lameter51df1142010-08-20 12:37:15 -05003936{
3937 int node;
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003938 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003939 struct kmem_cache_node *n;
Christoph Lameter51df1142010-08-20 12:37:15 -05003940
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003941 memcpy(s, static_cache, kmem_cache->object_size);
Christoph Lameter51df1142010-08-20 12:37:15 -05003942
Glauber Costa7d557b32013-02-22 20:20:00 +04003943 /*
3944 * This runs very early, and only the boot processor is supposed to be
3945 * up. Even if it weren't true, IRQs are not up so we couldn't fire
3946 * IPIs around.
3947 */
3948 __flush_cpu_slab(s, smp_processor_id());
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003949 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter51df1142010-08-20 12:37:15 -05003950 struct page *p;
3951
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003952 list_for_each_entry(p, &n->partial, lru)
3953 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003954
Li Zefan607bf322011-04-12 15:22:26 +08003955#ifdef CONFIG_SLUB_DEBUG
Christoph Lameterfa45dc22014-08-06 16:04:09 -07003956 list_for_each_entry(p, &n->full, lru)
3957 p->slab_cache = s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003958#endif
Christoph Lameter51df1142010-08-20 12:37:15 -05003959 }
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08003960 slab_init_memcg_params(s);
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003961 list_add(&s->list, &slab_caches);
3962 return s;
Christoph Lameter51df1142010-08-20 12:37:15 -05003963}
3964
Christoph Lameter81819f02007-05-06 14:49:36 -07003965void __init kmem_cache_init(void)
3966{
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003967 static __initdata struct kmem_cache boot_kmem_cache,
3968 boot_kmem_cache_node;
Christoph Lameter51df1142010-08-20 12:37:15 -05003969
Stanislaw Gruszkafc8d8622012-01-10 15:07:32 -08003970 if (debug_guardpage_minorder())
3971 slub_max_order = 0;
3972
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003973 kmem_cache_node = &boot_kmem_cache_node;
3974 kmem_cache = &boot_kmem_cache;
Christoph Lameter51df1142010-08-20 12:37:15 -05003975
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003976 create_boot_cache(kmem_cache_node, "kmem_cache_node",
3977 sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN);
Yasunori Gotob9049e22007-10-21 16:41:37 -07003978
Andrew Morton3ac38fa2013-04-29 15:08:06 -07003979 register_hotmemory_notifier(&slab_memory_callback_nb);
Christoph Lameter81819f02007-05-06 14:49:36 -07003980
3981 /* Able to allocate the per node structures */
3982 slab_state = PARTIAL;
3983
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003984 create_boot_cache(kmem_cache, "kmem_cache",
3985 offsetof(struct kmem_cache, node) +
3986 nr_node_ids * sizeof(struct kmem_cache_node *),
3987 SLAB_HWCACHE_ALIGN);
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00003988
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003989 kmem_cache = bootstrap(&boot_kmem_cache);
Christoph Lameter81819f02007-05-06 14:49:36 -07003990
Christoph Lameter51df1142010-08-20 12:37:15 -05003991 /*
3992 * Allocate kmem_cache_node properly from the kmem_cache slab.
3993 * kmem_cache_node is separately allocated so no need to
3994 * update any list pointers.
3995 */
Christoph Lameterdffb4d62012-11-28 16:23:07 +00003996 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
Christoph Lameter51df1142010-08-20 12:37:15 -05003997
3998 /* Now we can use the kmem_cache to allocate kmalloc slabs */
Daniel Sanders34cc6992015-06-24 16:55:57 -07003999 setup_kmalloc_cache_index_table();
Christoph Lameterf97d5f62013-01-10 19:12:17 +00004000 create_kmalloc_caches(0);
Christoph Lameter81819f02007-05-06 14:49:36 -07004001
4002#ifdef CONFIG_SMP
4003 register_cpu_notifier(&slab_notifier);
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06004004#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004005
Fabian Frederickf9f58282014-06-04 16:06:34 -07004006 pr_info("SLUB: HWalign=%d, Order=%d-%d, MinObjects=%d, CPUs=%d, Nodes=%d\n",
Christoph Lameterf97d5f62013-01-10 19:12:17 +00004007 cache_line_size(),
Christoph Lameter81819f02007-05-06 14:49:36 -07004008 slub_min_order, slub_max_order, slub_min_objects,
4009 nr_cpu_ids, nr_node_ids);
4010}
4011
Pekka Enberg7e85ee02009-06-12 14:03:06 +03004012void __init kmem_cache_init_late(void)
4013{
Pekka Enberg7e85ee02009-06-12 14:03:06 +03004014}
4015
Glauber Costa2633d7a2012-12-18 14:22:34 -08004016struct kmem_cache *
Vladimir Davydova44cb942014-04-07 15:39:23 -07004017__kmem_cache_alias(const char *name, size_t size, size_t align,
4018 unsigned long flags, void (*ctor)(void *))
Christoph Lameter81819f02007-05-06 14:49:36 -07004019{
Vladimir Davydov426589f2015-02-12 14:59:23 -08004020 struct kmem_cache *s, *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07004021
Vladimir Davydova44cb942014-04-07 15:39:23 -07004022 s = find_mergeable(size, align, flags, name, ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004023 if (s) {
4024 s->refcount++;
Vladimir Davydov84d0ddd2014-04-07 15:39:29 -07004025
Christoph Lameter81819f02007-05-06 14:49:36 -07004026 /*
4027 * Adjust the object sizes so that we clear
4028 * the complete object on kzalloc.
4029 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004030 s->object_size = max(s->object_size, (int)size);
Christoph Lameter81819f02007-05-06 14:49:36 -07004031 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
Christoph Lameter6446faa2008-02-15 23:45:26 -08004032
Vladimir Davydov426589f2015-02-12 14:59:23 -08004033 for_each_memcg_cache(c, s) {
Vladimir Davydov84d0ddd2014-04-07 15:39:29 -07004034 c->object_size = s->object_size;
4035 c->inuse = max_t(int, c->inuse,
4036 ALIGN(size, sizeof(void *)));
4037 }
4038
David Rientjes7b8f3b62008-12-17 22:09:46 -08004039 if (sysfs_slab_alias(s, name)) {
David Rientjes7b8f3b62008-12-17 22:09:46 -08004040 s->refcount--;
Christoph Lametercbb79692012-09-05 00:18:32 +00004041 s = NULL;
David Rientjes7b8f3b62008-12-17 22:09:46 -08004042 }
Christoph Lametera0e1d1b2007-07-17 04:03:31 -07004043 }
Christoph Lameter6446faa2008-02-15 23:45:26 -08004044
Christoph Lametercbb79692012-09-05 00:18:32 +00004045 return s;
4046}
Pekka Enberg84c1cf62010-09-14 23:21:12 +03004047
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00004048int __kmem_cache_create(struct kmem_cache *s, unsigned long flags)
Christoph Lametercbb79692012-09-05 00:18:32 +00004049{
Pekka Enbergaac3a162012-09-05 12:07:44 +03004050 int err;
Christoph Lameter20cea962012-07-06 15:25:13 -05004051
Pekka Enbergaac3a162012-09-05 12:07:44 +03004052 err = kmem_cache_open(s, flags);
4053 if (err)
4054 return err;
Christoph Lameter20cea962012-07-06 15:25:13 -05004055
Christoph Lameter45530c42012-11-28 16:23:07 +00004056 /* Mutex is not taken during early boot */
4057 if (slab_state <= UP)
4058 return 0;
4059
Glauber Costa107dab52012-12-18 14:23:05 -08004060 memcg_propagate_slab_attrs(s);
Pekka Enbergaac3a162012-09-05 12:07:44 +03004061 err = sysfs_slab_add(s);
Pekka Enbergaac3a162012-09-05 12:07:44 +03004062 if (err)
4063 kmem_cache_close(s);
4064
4065 return err;
Christoph Lameter81819f02007-05-06 14:49:36 -07004066}
Christoph Lameter81819f02007-05-06 14:49:36 -07004067
Christoph Lameter81819f02007-05-06 14:49:36 -07004068#ifdef CONFIG_SMP
Christoph Lameter27390bc2007-06-01 00:47:09 -07004069/*
Christoph Lameter672bba32007-05-09 02:32:39 -07004070 * Use the cpu notifier to insure that the cpu slabs are flushed when
4071 * necessary.
Christoph Lameter81819f02007-05-06 14:49:36 -07004072 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004073static int slab_cpuup_callback(struct notifier_block *nfb,
Christoph Lameter81819f02007-05-06 14:49:36 -07004074 unsigned long action, void *hcpu)
4075{
4076 long cpu = (long)hcpu;
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004077 struct kmem_cache *s;
4078 unsigned long flags;
Christoph Lameter81819f02007-05-06 14:49:36 -07004079
4080 switch (action) {
4081 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07004082 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter81819f02007-05-06 14:49:36 -07004083 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07004084 case CPU_DEAD_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05004085 mutex_lock(&slab_mutex);
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07004086 list_for_each_entry(s, &slab_caches, list) {
4087 local_irq_save(flags);
4088 __flush_cpu_slab(s, cpu);
4089 local_irq_restore(flags);
4090 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004091 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07004092 break;
4093 default:
4094 break;
4095 }
4096 return NOTIFY_OK;
4097}
4098
Paul Gortmaker0db06282013-06-19 14:53:51 -04004099static struct notifier_block slab_notifier = {
Ingo Molnar3adbefe2008-02-05 17:57:39 -08004100 .notifier_call = slab_cpuup_callback
Pekka Enberg06428782008-01-07 23:20:27 -08004101};
Christoph Lameter81819f02007-05-06 14:49:36 -07004102
4103#endif
4104
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004105void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07004106{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07004107 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004108 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07004109
Christoph Lameter95a05b42013-01-10 19:14:19 +00004110 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
Pekka Enbergeada35e2008-02-11 22:47:46 +02004111 return kmalloc_large(size, gfpflags);
4112
Christoph Lameter2c59dd62013-01-10 19:14:19 +00004113 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07004114
Satyam Sharma2408c552007-10-16 01:24:44 -07004115 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07004116 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004117
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03004118 ret = slab_alloc(s, gfpflags, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004119
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004120 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02004121 trace_kmalloc(caller, ret, size, s->size, gfpflags);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004122
4123 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07004124}
4125
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09004126#ifdef CONFIG_NUMA
Christoph Lameter81819f02007-05-06 14:49:36 -07004127void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004128 int node, unsigned long caller)
Christoph Lameter81819f02007-05-06 14:49:36 -07004129{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07004130 struct kmem_cache *s;
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004131 void *ret;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -07004132
Christoph Lameter95a05b42013-01-10 19:14:19 +00004133 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
Xiaotian Fengd3e14aa2010-04-08 17:26:44 +08004134 ret = kmalloc_large_node(size, gfpflags, node);
4135
4136 trace_kmalloc_node(caller, ret,
4137 size, PAGE_SIZE << get_order(size),
4138 gfpflags, node);
4139
4140 return ret;
4141 }
Pekka Enbergeada35e2008-02-11 22:47:46 +02004142
Christoph Lameter2c59dd62013-01-10 19:14:19 +00004143 s = kmalloc_slab(size, gfpflags);
Christoph Lameter81819f02007-05-06 14:49:36 -07004144
Satyam Sharma2408c552007-10-16 01:24:44 -07004145 if (unlikely(ZERO_OR_NULL_PTR(s)))
Christoph Lameter6cb8f912007-07-17 04:03:22 -07004146 return s;
Christoph Lameter81819f02007-05-06 14:49:36 -07004147
Ezequiel Garcia2b847c32012-09-08 17:47:58 -03004148 ret = slab_alloc_node(s, gfpflags, node, caller);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004149
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004150 /* Honor the call site pointer we received. */
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02004151 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
Eduard - Gabriel Munteanu94b528d2008-08-24 20:49:35 +03004152
4153 return ret;
Christoph Lameter81819f02007-05-06 14:49:36 -07004154}
Namhyung Kim5d1f57e2010-09-29 21:02:15 +09004155#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004156
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004157#ifdef CONFIG_SYSFS
Christoph Lameter205ab992008-04-14 19:11:40 +03004158static int count_inuse(struct page *page)
4159{
4160 return page->inuse;
4161}
4162
4163static int count_total(struct page *page)
4164{
4165 return page->objects;
4166}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004167#endif
Christoph Lameter205ab992008-04-14 19:11:40 +03004168
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004169#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter434e2452007-07-17 04:03:30 -07004170static int validate_slab(struct kmem_cache *s, struct page *page,
4171 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07004172{
4173 void *p;
Christoph Lametera973e9d2008-03-01 13:40:44 -08004174 void *addr = page_address(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004175
4176 if (!check_slab(s, page) ||
4177 !on_freelist(s, page, NULL))
4178 return 0;
4179
4180 /* Now we know that a valid freelist exists */
Christoph Lameter39b26462008-04-14 19:11:30 +03004181 bitmap_zero(map, page->objects);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004182
Christoph Lameter5f80b132011-04-15 14:48:13 -05004183 get_map(s, page, map);
4184 for_each_object(p, s, addr, page->objects) {
4185 if (test_bit(slab_index(p, s, addr), map))
4186 if (!check_object(s, page, p, SLUB_RED_INACTIVE))
4187 return 0;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004188 }
4189
Christoph Lameter224a88b2008-04-14 19:11:31 +03004190 for_each_object(p, s, addr, page->objects)
Christoph Lameter7656c722007-05-09 02:32:40 -07004191 if (!test_bit(slab_index(p, s, addr), map))
Tero Roponen37d57442010-12-01 20:04:20 +02004192 if (!check_object(s, page, p, SLUB_RED_ACTIVE))
Christoph Lameter53e15af2007-05-06 14:49:43 -07004193 return 0;
4194 return 1;
4195}
4196
Christoph Lameter434e2452007-07-17 04:03:30 -07004197static void validate_slab_slab(struct kmem_cache *s, struct page *page,
4198 unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07004199{
Christoph Lameter881db7f2011-06-01 12:25:53 -05004200 slab_lock(page);
4201 validate_slab(s, page, map);
4202 slab_unlock(page);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004203}
4204
Christoph Lameter434e2452007-07-17 04:03:30 -07004205static int validate_slab_node(struct kmem_cache *s,
4206 struct kmem_cache_node *n, unsigned long *map)
Christoph Lameter53e15af2007-05-06 14:49:43 -07004207{
4208 unsigned long count = 0;
4209 struct page *page;
4210 unsigned long flags;
4211
4212 spin_lock_irqsave(&n->list_lock, flags);
4213
4214 list_for_each_entry(page, &n->partial, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07004215 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004216 count++;
4217 }
4218 if (count != n->nr_partial)
Fabian Frederickf9f58282014-06-04 16:06:34 -07004219 pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
4220 s->name, count, n->nr_partial);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004221
4222 if (!(s->flags & SLAB_STORE_USER))
4223 goto out;
4224
4225 list_for_each_entry(page, &n->full, lru) {
Christoph Lameter434e2452007-07-17 04:03:30 -07004226 validate_slab_slab(s, page, map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004227 count++;
4228 }
4229 if (count != atomic_long_read(&n->nr_slabs))
Fabian Frederickf9f58282014-06-04 16:06:34 -07004230 pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
4231 s->name, count, atomic_long_read(&n->nr_slabs));
Christoph Lameter53e15af2007-05-06 14:49:43 -07004232
4233out:
4234 spin_unlock_irqrestore(&n->list_lock, flags);
4235 return count;
4236}
4237
Christoph Lameter434e2452007-07-17 04:03:30 -07004238static long validate_slab_cache(struct kmem_cache *s)
Christoph Lameter53e15af2007-05-06 14:49:43 -07004239{
4240 int node;
4241 unsigned long count = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03004242 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
Christoph Lameter434e2452007-07-17 04:03:30 -07004243 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004244 struct kmem_cache_node *n;
Christoph Lameter434e2452007-07-17 04:03:30 -07004245
4246 if (!map)
4247 return -ENOMEM;
Christoph Lameter53e15af2007-05-06 14:49:43 -07004248
4249 flush_all(s);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004250 for_each_kmem_cache_node(s, node, n)
Christoph Lameter434e2452007-07-17 04:03:30 -07004251 count += validate_slab_node(s, n, map);
Christoph Lameter434e2452007-07-17 04:03:30 -07004252 kfree(map);
Christoph Lameter53e15af2007-05-06 14:49:43 -07004253 return count;
4254}
Christoph Lameter88a420e2007-05-06 14:49:45 -07004255/*
Christoph Lameter672bba32007-05-09 02:32:39 -07004256 * Generate lists of code addresses where slabcache objects are allocated
Christoph Lameter88a420e2007-05-06 14:49:45 -07004257 * and freed.
4258 */
4259
4260struct location {
4261 unsigned long count;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004262 unsigned long addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004263 long long sum_time;
4264 long min_time;
4265 long max_time;
4266 long min_pid;
4267 long max_pid;
Rusty Russell174596a2009-01-01 10:12:29 +10304268 DECLARE_BITMAP(cpus, NR_CPUS);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004269 nodemask_t nodes;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004270};
4271
4272struct loc_track {
4273 unsigned long max;
4274 unsigned long count;
4275 struct location *loc;
4276};
4277
4278static void free_loc_track(struct loc_track *t)
4279{
4280 if (t->max)
4281 free_pages((unsigned long)t->loc,
4282 get_order(sizeof(struct location) * t->max));
4283}
4284
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004285static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004286{
4287 struct location *l;
4288 int order;
4289
Christoph Lameter88a420e2007-05-06 14:49:45 -07004290 order = get_order(sizeof(struct location) * max);
4291
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004292 l = (void *)__get_free_pages(flags, order);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004293 if (!l)
4294 return 0;
4295
4296 if (t->count) {
4297 memcpy(l, t->loc, sizeof(struct location) * t->count);
4298 free_loc_track(t);
4299 }
4300 t->max = max;
4301 t->loc = l;
4302 return 1;
4303}
4304
4305static int add_location(struct loc_track *t, struct kmem_cache *s,
Christoph Lameter45edfa52007-05-09 02:32:45 -07004306 const struct track *track)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004307{
4308 long start, end, pos;
4309 struct location *l;
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03004310 unsigned long caddr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004311 unsigned long age = jiffies - track->when;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004312
4313 start = -1;
4314 end = t->count;
4315
4316 for ( ; ; ) {
4317 pos = start + (end - start + 1) / 2;
4318
4319 /*
4320 * There is nothing at "end". If we end up there
4321 * we need to add something to before end.
4322 */
4323 if (pos == end)
4324 break;
4325
4326 caddr = t->loc[pos].addr;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004327 if (track->addr == caddr) {
4328
4329 l = &t->loc[pos];
4330 l->count++;
4331 if (track->when) {
4332 l->sum_time += age;
4333 if (age < l->min_time)
4334 l->min_time = age;
4335 if (age > l->max_time)
4336 l->max_time = age;
4337
4338 if (track->pid < l->min_pid)
4339 l->min_pid = track->pid;
4340 if (track->pid > l->max_pid)
4341 l->max_pid = track->pid;
4342
Rusty Russell174596a2009-01-01 10:12:29 +10304343 cpumask_set_cpu(track->cpu,
4344 to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004345 }
4346 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004347 return 1;
4348 }
4349
Christoph Lameter45edfa52007-05-09 02:32:45 -07004350 if (track->addr < caddr)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004351 end = pos;
4352 else
4353 start = pos;
4354 }
4355
4356 /*
Christoph Lameter672bba32007-05-09 02:32:39 -07004357 * Not found. Insert new tracking element.
Christoph Lameter88a420e2007-05-06 14:49:45 -07004358 */
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004359 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004360 return 0;
4361
4362 l = t->loc + pos;
4363 if (pos < t->count)
4364 memmove(l + 1, l,
4365 (t->count - pos) * sizeof(struct location));
4366 t->count++;
4367 l->count = 1;
Christoph Lameter45edfa52007-05-09 02:32:45 -07004368 l->addr = track->addr;
4369 l->sum_time = age;
4370 l->min_time = age;
4371 l->max_time = age;
4372 l->min_pid = track->pid;
4373 l->max_pid = track->pid;
Rusty Russell174596a2009-01-01 10:12:29 +10304374 cpumask_clear(to_cpumask(l->cpus));
4375 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004376 nodes_clear(l->nodes);
4377 node_set(page_to_nid(virt_to_page(track)), l->nodes);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004378 return 1;
4379}
4380
4381static void process_slab(struct loc_track *t, struct kmem_cache *s,
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004382 struct page *page, enum track_item alloc,
Namhyung Kima5dd5c12010-09-29 21:02:13 +09004383 unsigned long *map)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004384{
Christoph Lametera973e9d2008-03-01 13:40:44 -08004385 void *addr = page_address(page);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004386 void *p;
4387
Christoph Lameter39b26462008-04-14 19:11:30 +03004388 bitmap_zero(map, page->objects);
Christoph Lameter5f80b132011-04-15 14:48:13 -05004389 get_map(s, page, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004390
Christoph Lameter224a88b2008-04-14 19:11:31 +03004391 for_each_object(p, s, addr, page->objects)
Christoph Lameter45edfa52007-05-09 02:32:45 -07004392 if (!test_bit(slab_index(p, s, addr), map))
4393 add_location(t, s, get_track(s, p, alloc));
Christoph Lameter88a420e2007-05-06 14:49:45 -07004394}
4395
4396static int list_locations(struct kmem_cache *s, char *buf,
4397 enum track_item alloc)
4398{
Harvey Harrisone374d482008-01-31 15:20:50 -08004399 int len = 0;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004400 unsigned long i;
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004401 struct loc_track t = { 0, 0, NULL };
Christoph Lameter88a420e2007-05-06 14:49:45 -07004402 int node;
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004403 unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
4404 sizeof(unsigned long), GFP_KERNEL);
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004405 struct kmem_cache_node *n;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004406
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004407 if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4408 GFP_TEMPORARY)) {
4409 kfree(map);
Christoph Lameter68dff6a2007-07-17 04:03:20 -07004410 return sprintf(buf, "Out of memory\n");
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004411 }
Christoph Lameter88a420e2007-05-06 14:49:45 -07004412 /* Push back cpu slabs */
4413 flush_all(s);
4414
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004415 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter88a420e2007-05-06 14:49:45 -07004416 unsigned long flags;
4417 struct page *page;
4418
Christoph Lameter9e869432007-08-22 14:01:56 -07004419 if (!atomic_long_read(&n->nr_slabs))
Christoph Lameter88a420e2007-05-06 14:49:45 -07004420 continue;
4421
4422 spin_lock_irqsave(&n->list_lock, flags);
4423 list_for_each_entry(page, &n->partial, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004424 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004425 list_for_each_entry(page, &n->full, lru)
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004426 process_slab(&t, s, page, alloc, map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004427 spin_unlock_irqrestore(&n->list_lock, flags);
4428 }
4429
4430 for (i = 0; i < t.count; i++) {
Christoph Lameter45edfa52007-05-09 02:32:45 -07004431 struct location *l = &t.loc[i];
Christoph Lameter88a420e2007-05-06 14:49:45 -07004432
Hugh Dickins9c246242008-12-09 13:14:27 -08004433 if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
Christoph Lameter88a420e2007-05-06 14:49:45 -07004434 break;
Harvey Harrisone374d482008-01-31 15:20:50 -08004435 len += sprintf(buf + len, "%7ld ", l->count);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004436
4437 if (l->addr)
Joe Perches62c70bc2011-01-13 15:45:52 -08004438 len += sprintf(buf + len, "%pS", (void *)l->addr);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004439 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004440 len += sprintf(buf + len, "<not-available>");
Christoph Lameter45edfa52007-05-09 02:32:45 -07004441
4442 if (l->sum_time != l->min_time) {
Harvey Harrisone374d482008-01-31 15:20:50 -08004443 len += sprintf(buf + len, " age=%ld/%ld/%ld",
Roman Zippelf8bd2252008-05-01 04:34:31 -07004444 l->min_time,
4445 (long)div_u64(l->sum_time, l->count),
4446 l->max_time);
Christoph Lameter45edfa52007-05-09 02:32:45 -07004447 } else
Harvey Harrisone374d482008-01-31 15:20:50 -08004448 len += sprintf(buf + len, " age=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004449 l->min_time);
4450
4451 if (l->min_pid != l->max_pid)
Harvey Harrisone374d482008-01-31 15:20:50 -08004452 len += sprintf(buf + len, " pid=%ld-%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004453 l->min_pid, l->max_pid);
4454 else
Harvey Harrisone374d482008-01-31 15:20:50 -08004455 len += sprintf(buf + len, " pid=%ld",
Christoph Lameter45edfa52007-05-09 02:32:45 -07004456 l->min_pid);
4457
Rusty Russell174596a2009-01-01 10:12:29 +10304458 if (num_online_cpus() > 1 &&
4459 !cpumask_empty(to_cpumask(l->cpus)) &&
Tejun Heo5024c1d2015-02-13 14:37:59 -08004460 len < PAGE_SIZE - 60)
4461 len += scnprintf(buf + len, PAGE_SIZE - len - 50,
4462 " cpus=%*pbl",
4463 cpumask_pr_args(to_cpumask(l->cpus)));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004464
Christoph Lameter62bc62a2009-06-16 15:32:15 -07004465 if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
Tejun Heo5024c1d2015-02-13 14:37:59 -08004466 len < PAGE_SIZE - 60)
4467 len += scnprintf(buf + len, PAGE_SIZE - len - 50,
4468 " nodes=%*pbl",
4469 nodemask_pr_args(&l->nodes));
Christoph Lameter45edfa52007-05-09 02:32:45 -07004470
Harvey Harrisone374d482008-01-31 15:20:50 -08004471 len += sprintf(buf + len, "\n");
Christoph Lameter88a420e2007-05-06 14:49:45 -07004472 }
4473
4474 free_loc_track(&t);
Eric Dumazetbbd7d572010-03-24 22:25:47 +01004475 kfree(map);
Christoph Lameter88a420e2007-05-06 14:49:45 -07004476 if (!t.count)
Harvey Harrisone374d482008-01-31 15:20:50 -08004477 len += sprintf(buf, "No data\n");
4478 return len;
Christoph Lameter88a420e2007-05-06 14:49:45 -07004479}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004480#endif
Christoph Lameter88a420e2007-05-06 14:49:45 -07004481
Christoph Lametera5a84752010-10-05 13:57:27 -05004482#ifdef SLUB_RESILIENCY_TEST
David Rientjesc07b8182014-08-06 16:04:16 -07004483static void __init resiliency_test(void)
Christoph Lametera5a84752010-10-05 13:57:27 -05004484{
4485 u8 *p;
4486
Christoph Lameter95a05b42013-01-10 19:14:19 +00004487 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || KMALLOC_SHIFT_HIGH < 10);
Christoph Lametera5a84752010-10-05 13:57:27 -05004488
Fabian Frederickf9f58282014-06-04 16:06:34 -07004489 pr_err("SLUB resiliency testing\n");
4490 pr_err("-----------------------\n");
4491 pr_err("A. Corruption after allocation\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004492
4493 p = kzalloc(16, GFP_KERNEL);
4494 p[16] = 0x12;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004495 pr_err("\n1. kmalloc-16: Clobber Redzone/next pointer 0x12->0x%p\n\n",
4496 p + 16);
Christoph Lametera5a84752010-10-05 13:57:27 -05004497
4498 validate_slab_cache(kmalloc_caches[4]);
4499
4500 /* Hmmm... The next two are dangerous */
4501 p = kzalloc(32, GFP_KERNEL);
4502 p[32 + sizeof(void *)] = 0x34;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004503 pr_err("\n2. kmalloc-32: Clobber next pointer/next slab 0x34 -> -0x%p\n",
4504 p);
4505 pr_err("If allocated object is overwritten then not detectable\n\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004506
4507 validate_slab_cache(kmalloc_caches[5]);
4508 p = kzalloc(64, GFP_KERNEL);
4509 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
4510 *p = 0x56;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004511 pr_err("\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
4512 p);
4513 pr_err("If allocated object is overwritten then not detectable\n\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004514 validate_slab_cache(kmalloc_caches[6]);
4515
Fabian Frederickf9f58282014-06-04 16:06:34 -07004516 pr_err("\nB. Corruption after free\n");
Christoph Lametera5a84752010-10-05 13:57:27 -05004517 p = kzalloc(128, GFP_KERNEL);
4518 kfree(p);
4519 *p = 0x78;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004520 pr_err("1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004521 validate_slab_cache(kmalloc_caches[7]);
4522
4523 p = kzalloc(256, GFP_KERNEL);
4524 kfree(p);
4525 p[50] = 0x9a;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004526 pr_err("\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004527 validate_slab_cache(kmalloc_caches[8]);
4528
4529 p = kzalloc(512, GFP_KERNEL);
4530 kfree(p);
4531 p[512] = 0xab;
Fabian Frederickf9f58282014-06-04 16:06:34 -07004532 pr_err("\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
Christoph Lametera5a84752010-10-05 13:57:27 -05004533 validate_slab_cache(kmalloc_caches[9]);
4534}
4535#else
4536#ifdef CONFIG_SYSFS
4537static void resiliency_test(void) {};
4538#endif
4539#endif
4540
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004541#ifdef CONFIG_SYSFS
Christoph Lameter81819f02007-05-06 14:49:36 -07004542enum slab_stat_type {
Christoph Lameter205ab992008-04-14 19:11:40 +03004543 SL_ALL, /* All slabs */
4544 SL_PARTIAL, /* Only partially allocated slabs */
4545 SL_CPU, /* Only slabs used for cpu caches */
4546 SL_OBJECTS, /* Determine allocated objects not slabs */
4547 SL_TOTAL /* Determine object capacity not slabs */
Christoph Lameter81819f02007-05-06 14:49:36 -07004548};
4549
Christoph Lameter205ab992008-04-14 19:11:40 +03004550#define SO_ALL (1 << SL_ALL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004551#define SO_PARTIAL (1 << SL_PARTIAL)
4552#define SO_CPU (1 << SL_CPU)
4553#define SO_OBJECTS (1 << SL_OBJECTS)
Christoph Lameter205ab992008-04-14 19:11:40 +03004554#define SO_TOTAL (1 << SL_TOTAL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004555
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004556static ssize_t show_slab_objects(struct kmem_cache *s,
4557 char *buf, unsigned long flags)
Christoph Lameter81819f02007-05-06 14:49:36 -07004558{
4559 unsigned long total = 0;
Christoph Lameter81819f02007-05-06 14:49:36 -07004560 int node;
4561 int x;
4562 unsigned long *nodes;
Christoph Lameter81819f02007-05-06 14:49:36 -07004563
Chen Gange35e1a92013-07-12 08:23:48 +08004564 nodes = kzalloc(sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
Cyrill Gorcunov62e5c4b2008-03-02 23:28:24 +03004565 if (!nodes)
4566 return -ENOMEM;
Christoph Lameter81819f02007-05-06 14:49:36 -07004567
Christoph Lameter205ab992008-04-14 19:11:40 +03004568 if (flags & SO_CPU) {
4569 int cpu;
Christoph Lameter81819f02007-05-06 14:49:36 -07004570
Christoph Lameter205ab992008-04-14 19:11:40 +03004571 for_each_possible_cpu(cpu) {
Chen Gangd0e0ac92013-07-15 09:05:29 +08004572 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab,
4573 cpu);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004574 int node;
Christoph Lameter49e22582011-08-09 16:12:27 -05004575 struct page *page;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004576
Jason Low4db0c3c2015-04-15 16:14:08 -07004577 page = READ_ONCE(c->page);
Christoph Lameterec3ab082012-05-09 10:09:56 -05004578 if (!page)
4579 continue;
Christoph Lameter205ab992008-04-14 19:11:40 +03004580
Christoph Lameterec3ab082012-05-09 10:09:56 -05004581 node = page_to_nid(page);
4582 if (flags & SO_TOTAL)
4583 x = page->objects;
4584 else if (flags & SO_OBJECTS)
4585 x = page->inuse;
4586 else
4587 x = 1;
Christoph Lameter49e22582011-08-09 16:12:27 -05004588
Christoph Lameterec3ab082012-05-09 10:09:56 -05004589 total += x;
4590 nodes[node] += x;
4591
Jason Low4db0c3c2015-04-15 16:14:08 -07004592 page = READ_ONCE(c->partial);
Christoph Lameter49e22582011-08-09 16:12:27 -05004593 if (page) {
Li Zefan8afb1472013-09-10 11:43:37 +08004594 node = page_to_nid(page);
4595 if (flags & SO_TOTAL)
4596 WARN_ON_ONCE(1);
4597 else if (flags & SO_OBJECTS)
4598 WARN_ON_ONCE(1);
4599 else
4600 x = page->pages;
Eric Dumazetbc6697d2011-11-22 16:02:02 +01004601 total += x;
4602 nodes[node] += x;
Christoph Lameter49e22582011-08-09 16:12:27 -05004603 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004604 }
4605 }
4606
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004607 get_online_mems();
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004608#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter205ab992008-04-14 19:11:40 +03004609 if (flags & SO_ALL) {
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004610 struct kmem_cache_node *n;
4611
4612 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter81819f02007-05-06 14:49:36 -07004613
Chen Gangd0e0ac92013-07-15 09:05:29 +08004614 if (flags & SO_TOTAL)
4615 x = atomic_long_read(&n->total_objects);
4616 else if (flags & SO_OBJECTS)
4617 x = atomic_long_read(&n->total_objects) -
4618 count_partial(n, count_free);
Christoph Lameter205ab992008-04-14 19:11:40 +03004619 else
4620 x = atomic_long_read(&n->nr_slabs);
4621 total += x;
4622 nodes[node] += x;
4623 }
4624
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004625 } else
4626#endif
4627 if (flags & SO_PARTIAL) {
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004628 struct kmem_cache_node *n;
Christoph Lameter205ab992008-04-14 19:11:40 +03004629
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004630 for_each_kmem_cache_node(s, node, n) {
Christoph Lameter205ab992008-04-14 19:11:40 +03004631 if (flags & SO_TOTAL)
4632 x = count_partial(n, count_total);
4633 else if (flags & SO_OBJECTS)
4634 x = count_partial(n, count_inuse);
Christoph Lameter81819f02007-05-06 14:49:36 -07004635 else
4636 x = n->nr_partial;
4637 total += x;
4638 nodes[node] += x;
4639 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004640 }
Christoph Lameter81819f02007-05-06 14:49:36 -07004641 x = sprintf(buf, "%lu", total);
4642#ifdef CONFIG_NUMA
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004643 for (node = 0; node < nr_node_ids; node++)
Christoph Lameter81819f02007-05-06 14:49:36 -07004644 if (nodes[node])
4645 x += sprintf(buf + x, " N%d=%lu",
4646 node, nodes[node]);
4647#endif
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07004648 put_online_mems();
Christoph Lameter81819f02007-05-06 14:49:36 -07004649 kfree(nodes);
4650 return x + sprintf(buf + x, "\n");
4651}
4652
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004653#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -07004654static int any_slab_objects(struct kmem_cache *s)
4655{
4656 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004657 struct kmem_cache_node *n;
Christoph Lameterdfb4f092007-10-16 01:26:05 -07004658
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004659 for_each_kmem_cache_node(s, node, n)
Benjamin Herrenschmidt4ea33e22008-05-06 20:42:39 -07004660 if (atomic_long_read(&n->total_objects))
Christoph Lameter81819f02007-05-06 14:49:36 -07004661 return 1;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07004662
Christoph Lameter81819f02007-05-06 14:49:36 -07004663 return 0;
4664}
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004665#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07004666
4667#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
Phil Carmody497888c2011-07-14 15:07:13 +03004668#define to_slab(n) container_of(n, struct kmem_cache, kobj)
Christoph Lameter81819f02007-05-06 14:49:36 -07004669
4670struct slab_attribute {
4671 struct attribute attr;
4672 ssize_t (*show)(struct kmem_cache *s, char *buf);
4673 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
4674};
4675
4676#define SLAB_ATTR_RO(_name) \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004677 static struct slab_attribute _name##_attr = \
4678 __ATTR(_name, 0400, _name##_show, NULL)
Christoph Lameter81819f02007-05-06 14:49:36 -07004679
4680#define SLAB_ATTR(_name) \
4681 static struct slab_attribute _name##_attr = \
Vasiliy Kulikovab067e92011-09-27 21:54:53 +04004682 __ATTR(_name, 0600, _name##_show, _name##_store)
Christoph Lameter81819f02007-05-06 14:49:36 -07004683
Christoph Lameter81819f02007-05-06 14:49:36 -07004684static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4685{
4686 return sprintf(buf, "%d\n", s->size);
4687}
4688SLAB_ATTR_RO(slab_size);
4689
4690static ssize_t align_show(struct kmem_cache *s, char *buf)
4691{
4692 return sprintf(buf, "%d\n", s->align);
4693}
4694SLAB_ATTR_RO(align);
4695
4696static ssize_t object_size_show(struct kmem_cache *s, char *buf)
4697{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004698 return sprintf(buf, "%d\n", s->object_size);
Christoph Lameter81819f02007-05-06 14:49:36 -07004699}
4700SLAB_ATTR_RO(object_size);
4701
4702static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
4703{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004704 return sprintf(buf, "%d\n", oo_objects(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004705}
4706SLAB_ATTR_RO(objs_per_slab);
4707
Christoph Lameter06b285d2008-04-14 19:11:41 +03004708static ssize_t order_store(struct kmem_cache *s,
4709 const char *buf, size_t length)
4710{
Christoph Lameter0121c6192008-04-29 16:11:12 -07004711 unsigned long order;
4712 int err;
4713
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004714 err = kstrtoul(buf, 10, &order);
Christoph Lameter0121c6192008-04-29 16:11:12 -07004715 if (err)
4716 return err;
Christoph Lameter06b285d2008-04-14 19:11:41 +03004717
4718 if (order > slub_max_order || order < slub_min_order)
4719 return -EINVAL;
4720
4721 calculate_sizes(s, order);
4722 return length;
4723}
4724
Christoph Lameter81819f02007-05-06 14:49:36 -07004725static ssize_t order_show(struct kmem_cache *s, char *buf)
4726{
Christoph Lameter834f3d12008-04-14 19:11:31 +03004727 return sprintf(buf, "%d\n", oo_order(s->oo));
Christoph Lameter81819f02007-05-06 14:49:36 -07004728}
Christoph Lameter06b285d2008-04-14 19:11:41 +03004729SLAB_ATTR(order);
Christoph Lameter81819f02007-05-06 14:49:36 -07004730
David Rientjes73d342b2009-02-22 17:40:09 -08004731static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
4732{
4733 return sprintf(buf, "%lu\n", s->min_partial);
4734}
4735
4736static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
4737 size_t length)
4738{
4739 unsigned long min;
4740 int err;
4741
Jingoo Han3dbb95f2013-09-11 14:20:25 -07004742 err = kstrtoul(buf, 10, &min);
David Rientjes73d342b2009-02-22 17:40:09 -08004743 if (err)
4744 return err;
4745
David Rientjesc0bdb232009-02-25 09:16:35 +02004746 set_min_partial(s, min);
David Rientjes73d342b2009-02-22 17:40:09 -08004747 return length;
4748}
4749SLAB_ATTR(min_partial);
4750
Christoph Lameter49e22582011-08-09 16:12:27 -05004751static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
4752{
4753 return sprintf(buf, "%u\n", s->cpu_partial);
4754}
4755
4756static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
4757 size_t length)
4758{
Alexey Dobriyande2aac82018-04-05 16:21:10 -07004759 unsigned int objects;
Christoph Lameter49e22582011-08-09 16:12:27 -05004760 int err;
4761
Alexey Dobriyande2aac82018-04-05 16:21:10 -07004762 err = kstrtouint(buf, 10, &objects);
Christoph Lameter49e22582011-08-09 16:12:27 -05004763 if (err)
4764 return err;
Joonsoo Kim345c9052013-06-19 14:05:52 +09004765 if (objects && !kmem_cache_has_cpu_partial(s))
David Rientjes74ee4ef2012-01-09 13:19:45 -08004766 return -EINVAL;
Christoph Lameter49e22582011-08-09 16:12:27 -05004767
4768 s->cpu_partial = objects;
4769 flush_all(s);
4770 return length;
4771}
4772SLAB_ATTR(cpu_partial);
4773
Christoph Lameter81819f02007-05-06 14:49:36 -07004774static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4775{
Joe Perches62c70bc2011-01-13 15:45:52 -08004776 if (!s->ctor)
4777 return 0;
4778 return sprintf(buf, "%pS\n", s->ctor);
Christoph Lameter81819f02007-05-06 14:49:36 -07004779}
4780SLAB_ATTR_RO(ctor);
4781
Christoph Lameter81819f02007-05-06 14:49:36 -07004782static ssize_t aliases_show(struct kmem_cache *s, char *buf)
4783{
Gu Zheng4307c142014-08-06 16:04:51 -07004784 return sprintf(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004785}
4786SLAB_ATTR_RO(aliases);
4787
Christoph Lameter81819f02007-05-06 14:49:36 -07004788static ssize_t partial_show(struct kmem_cache *s, char *buf)
4789{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004790 return show_slab_objects(s, buf, SO_PARTIAL);
Christoph Lameter81819f02007-05-06 14:49:36 -07004791}
4792SLAB_ATTR_RO(partial);
4793
4794static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
4795{
Christoph Lameterd9acf4b2008-02-15 15:22:21 -08004796 return show_slab_objects(s, buf, SO_CPU);
Christoph Lameter81819f02007-05-06 14:49:36 -07004797}
4798SLAB_ATTR_RO(cpu_slabs);
4799
4800static ssize_t objects_show(struct kmem_cache *s, char *buf)
4801{
Christoph Lameter205ab992008-04-14 19:11:40 +03004802 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
Christoph Lameter81819f02007-05-06 14:49:36 -07004803}
4804SLAB_ATTR_RO(objects);
4805
Christoph Lameter205ab992008-04-14 19:11:40 +03004806static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
4807{
4808 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
4809}
4810SLAB_ATTR_RO(objects_partial);
4811
Christoph Lameter49e22582011-08-09 16:12:27 -05004812static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
4813{
4814 int objects = 0;
4815 int pages = 0;
4816 int cpu;
4817 int len;
4818
4819 for_each_online_cpu(cpu) {
4820 struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
4821
4822 if (page) {
4823 pages += page->pages;
4824 objects += page->pobjects;
4825 }
4826 }
4827
4828 len = sprintf(buf, "%d(%d)", objects, pages);
4829
4830#ifdef CONFIG_SMP
4831 for_each_online_cpu(cpu) {
4832 struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
4833
4834 if (page && len < PAGE_SIZE - 20)
4835 len += sprintf(buf + len, " C%d=%d(%d)", cpu,
4836 page->pobjects, page->pages);
4837 }
4838#endif
4839 return len + sprintf(buf + len, "\n");
4840}
4841SLAB_ATTR_RO(slabs_cpu_partial);
4842
Christoph Lameter81819f02007-05-06 14:49:36 -07004843static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4844{
4845 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4846}
4847
4848static ssize_t reclaim_account_store(struct kmem_cache *s,
4849 const char *buf, size_t length)
4850{
4851 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
4852 if (buf[0] == '1')
4853 s->flags |= SLAB_RECLAIM_ACCOUNT;
4854 return length;
4855}
4856SLAB_ATTR(reclaim_account);
4857
4858static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
4859{
Christoph Lameter5af60832007-05-06 14:49:56 -07004860 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
Christoph Lameter81819f02007-05-06 14:49:36 -07004861}
4862SLAB_ATTR_RO(hwcache_align);
4863
4864#ifdef CONFIG_ZONE_DMA
4865static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
4866{
4867 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
4868}
4869SLAB_ATTR_RO(cache_dma);
4870#endif
4871
4872static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
4873{
4874 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
4875}
4876SLAB_ATTR_RO(destroy_by_rcu);
4877
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08004878static ssize_t reserved_show(struct kmem_cache *s, char *buf)
4879{
4880 return sprintf(buf, "%d\n", s->reserved);
4881}
4882SLAB_ATTR_RO(reserved);
4883
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05004884#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05004885static ssize_t slabs_show(struct kmem_cache *s, char *buf)
4886{
4887 return show_slab_objects(s, buf, SO_ALL);
4888}
4889SLAB_ATTR_RO(slabs);
4890
4891static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
4892{
4893 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
4894}
4895SLAB_ATTR_RO(total_objects);
4896
4897static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
4898{
4899 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
4900}
4901
4902static ssize_t sanity_checks_store(struct kmem_cache *s,
4903 const char *buf, size_t length)
4904{
4905 s->flags &= ~SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004906 if (buf[0] == '1') {
4907 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004908 s->flags |= SLAB_DEBUG_FREE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004909 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004910 return length;
4911}
4912SLAB_ATTR(sanity_checks);
4913
4914static ssize_t trace_show(struct kmem_cache *s, char *buf)
4915{
4916 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
4917}
4918
4919static ssize_t trace_store(struct kmem_cache *s, const char *buf,
4920 size_t length)
4921{
Christoph Lameterc9e16132014-10-09 15:26:11 -07004922 /*
4923 * Tracing a merged cache is going to give confusing results
4924 * as well as cause other issues like converting a mergeable
4925 * cache into an umergeable one.
4926 */
4927 if (s->refcount > 1)
4928 return -EINVAL;
4929
Christoph Lametera5a84752010-10-05 13:57:27 -05004930 s->flags &= ~SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004931 if (buf[0] == '1') {
4932 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lametera5a84752010-10-05 13:57:27 -05004933 s->flags |= SLAB_TRACE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004934 }
Christoph Lametera5a84752010-10-05 13:57:27 -05004935 return length;
4936}
4937SLAB_ATTR(trace);
4938
Christoph Lameter81819f02007-05-06 14:49:36 -07004939static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
4940{
4941 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
4942}
4943
4944static ssize_t red_zone_store(struct kmem_cache *s,
4945 const char *buf, size_t length)
4946{
4947 if (any_slab_objects(s))
4948 return -EBUSY;
4949
4950 s->flags &= ~SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004951 if (buf[0] == '1') {
4952 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004953 s->flags |= SLAB_RED_ZONE;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004954 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004955 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004956 return length;
4957}
4958SLAB_ATTR(red_zone);
4959
4960static ssize_t poison_show(struct kmem_cache *s, char *buf)
4961{
4962 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
4963}
4964
4965static ssize_t poison_store(struct kmem_cache *s,
4966 const char *buf, size_t length)
4967{
4968 if (any_slab_objects(s))
4969 return -EBUSY;
4970
4971 s->flags &= ~SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004972 if (buf[0] == '1') {
4973 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004974 s->flags |= SLAB_POISON;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004975 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004976 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004977 return length;
4978}
4979SLAB_ATTR(poison);
4980
4981static ssize_t store_user_show(struct kmem_cache *s, char *buf)
4982{
4983 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
4984}
4985
4986static ssize_t store_user_store(struct kmem_cache *s,
4987 const char *buf, size_t length)
4988{
4989 if (any_slab_objects(s))
4990 return -EBUSY;
4991
4992 s->flags &= ~SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004993 if (buf[0] == '1') {
4994 s->flags &= ~__CMPXCHG_DOUBLE;
Christoph Lameter81819f02007-05-06 14:49:36 -07004995 s->flags |= SLAB_STORE_USER;
Christoph Lameterb789ef52011-06-01 12:25:49 -05004996 }
Christoph Lameter06b285d2008-04-14 19:11:41 +03004997 calculate_sizes(s, -1);
Christoph Lameter81819f02007-05-06 14:49:36 -07004998 return length;
4999}
5000SLAB_ATTR(store_user);
5001
Christoph Lameter53e15af2007-05-06 14:49:43 -07005002static ssize_t validate_show(struct kmem_cache *s, char *buf)
5003{
5004 return 0;
5005}
5006
5007static ssize_t validate_store(struct kmem_cache *s,
5008 const char *buf, size_t length)
5009{
Christoph Lameter434e2452007-07-17 04:03:30 -07005010 int ret = -EINVAL;
5011
5012 if (buf[0] == '1') {
5013 ret = validate_slab_cache(s);
5014 if (ret >= 0)
5015 ret = length;
5016 }
5017 return ret;
Christoph Lameter53e15af2007-05-06 14:49:43 -07005018}
5019SLAB_ATTR(validate);
Christoph Lametera5a84752010-10-05 13:57:27 -05005020
5021static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
5022{
5023 if (!(s->flags & SLAB_STORE_USER))
5024 return -ENOSYS;
5025 return list_locations(s, buf, TRACK_ALLOC);
5026}
5027SLAB_ATTR_RO(alloc_calls);
5028
5029static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
5030{
5031 if (!(s->flags & SLAB_STORE_USER))
5032 return -ENOSYS;
5033 return list_locations(s, buf, TRACK_FREE);
5034}
5035SLAB_ATTR_RO(free_calls);
5036#endif /* CONFIG_SLUB_DEBUG */
5037
5038#ifdef CONFIG_FAILSLAB
5039static ssize_t failslab_show(struct kmem_cache *s, char *buf)
5040{
5041 return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
5042}
5043
5044static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
5045 size_t length)
5046{
Christoph Lameterc9e16132014-10-09 15:26:11 -07005047 if (s->refcount > 1)
5048 return -EINVAL;
5049
Christoph Lametera5a84752010-10-05 13:57:27 -05005050 s->flags &= ~SLAB_FAILSLAB;
5051 if (buf[0] == '1')
5052 s->flags |= SLAB_FAILSLAB;
5053 return length;
5054}
5055SLAB_ATTR(failslab);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005056#endif
Christoph Lameter53e15af2007-05-06 14:49:43 -07005057
Christoph Lameter2086d262007-05-06 14:49:46 -07005058static ssize_t shrink_show(struct kmem_cache *s, char *buf)
5059{
5060 return 0;
5061}
5062
5063static ssize_t shrink_store(struct kmem_cache *s,
5064 const char *buf, size_t length)
5065{
Vladimir Davydov832f37f2015-02-12 14:59:41 -08005066 if (buf[0] == '1')
5067 kmem_cache_shrink(s);
5068 else
Christoph Lameter2086d262007-05-06 14:49:46 -07005069 return -EINVAL;
5070 return length;
5071}
5072SLAB_ATTR(shrink);
5073
Christoph Lameter81819f02007-05-06 14:49:36 -07005074#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08005075static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
Christoph Lameter81819f02007-05-06 14:49:36 -07005076{
Christoph Lameter98246012008-01-07 23:20:26 -08005077 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
Christoph Lameter81819f02007-05-06 14:49:36 -07005078}
5079
Christoph Lameter98246012008-01-07 23:20:26 -08005080static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
Christoph Lameter81819f02007-05-06 14:49:36 -07005081 const char *buf, size_t length)
5082{
Christoph Lameter0121c6192008-04-29 16:11:12 -07005083 unsigned long ratio;
5084 int err;
Christoph Lameter81819f02007-05-06 14:49:36 -07005085
Jingoo Han3dbb95f2013-09-11 14:20:25 -07005086 err = kstrtoul(buf, 10, &ratio);
Christoph Lameter0121c6192008-04-29 16:11:12 -07005087 if (err)
5088 return err;
5089
Christoph Lametere2cb96b2008-08-19 08:51:22 -05005090 if (ratio <= 100)
Christoph Lameter0121c6192008-04-29 16:11:12 -07005091 s->remote_node_defrag_ratio = ratio * 10;
5092
Christoph Lameter81819f02007-05-06 14:49:36 -07005093 return length;
5094}
Christoph Lameter98246012008-01-07 23:20:26 -08005095SLAB_ATTR(remote_node_defrag_ratio);
Christoph Lameter81819f02007-05-06 14:49:36 -07005096#endif
5097
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005098#ifdef CONFIG_SLUB_STATS
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005099static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
5100{
5101 unsigned long sum = 0;
5102 int cpu;
5103 int len;
5104 int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
5105
5106 if (!data)
5107 return -ENOMEM;
5108
5109 for_each_online_cpu(cpu) {
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06005110 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005111
5112 data[cpu] = x;
5113 sum += x;
5114 }
5115
5116 len = sprintf(buf, "%lu", sum);
5117
Christoph Lameter50ef37b2008-04-14 18:52:05 +03005118#ifdef CONFIG_SMP
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005119 for_each_online_cpu(cpu) {
5120 if (data[cpu] && len < PAGE_SIZE - 20)
Christoph Lameter50ef37b2008-04-14 18:52:05 +03005121 len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005122 }
Christoph Lameter50ef37b2008-04-14 18:52:05 +03005123#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005124 kfree(data);
5125 return len + sprintf(buf + len, "\n");
5126}
5127
David Rientjes78eb00c2009-10-15 02:20:22 -07005128static void clear_stat(struct kmem_cache *s, enum stat_item si)
5129{
5130 int cpu;
5131
5132 for_each_online_cpu(cpu)
Christoph Lameter9dfc6e62009-12-18 16:26:20 -06005133 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
David Rientjes78eb00c2009-10-15 02:20:22 -07005134}
5135
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005136#define STAT_ATTR(si, text) \
5137static ssize_t text##_show(struct kmem_cache *s, char *buf) \
5138{ \
5139 return show_stat(s, buf, si); \
5140} \
David Rientjes78eb00c2009-10-15 02:20:22 -07005141static ssize_t text##_store(struct kmem_cache *s, \
5142 const char *buf, size_t length) \
5143{ \
5144 if (buf[0] != '0') \
5145 return -EINVAL; \
5146 clear_stat(s, si); \
5147 return length; \
5148} \
5149SLAB_ATTR(text); \
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005150
5151STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
5152STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
5153STAT_ATTR(FREE_FASTPATH, free_fastpath);
5154STAT_ATTR(FREE_SLOWPATH, free_slowpath);
5155STAT_ATTR(FREE_FROZEN, free_frozen);
5156STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
5157STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
5158STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
5159STAT_ATTR(ALLOC_SLAB, alloc_slab);
5160STAT_ATTR(ALLOC_REFILL, alloc_refill);
Christoph Lametere36a2652011-06-01 12:25:57 -05005161STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005162STAT_ATTR(FREE_SLAB, free_slab);
5163STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
5164STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
5165STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
5166STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
5167STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
5168STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
Christoph Lameter03e404a2011-06-01 12:25:58 -05005169STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
Christoph Lameter65c33762008-04-14 19:11:40 +03005170STAT_ATTR(ORDER_FALLBACK, order_fallback);
Christoph Lameterb789ef52011-06-01 12:25:49 -05005171STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
5172STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
Christoph Lameter49e22582011-08-09 16:12:27 -05005173STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
5174STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
Alex Shi8028dce2012-02-03 23:34:56 +08005175STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
5176STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005177#endif
5178
Pekka Enberg06428782008-01-07 23:20:27 -08005179static struct attribute *slab_attrs[] = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005180 &slab_size_attr.attr,
5181 &object_size_attr.attr,
5182 &objs_per_slab_attr.attr,
5183 &order_attr.attr,
David Rientjes73d342b2009-02-22 17:40:09 -08005184 &min_partial_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05005185 &cpu_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005186 &objects_attr.attr,
Christoph Lameter205ab992008-04-14 19:11:40 +03005187 &objects_partial_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005188 &partial_attr.attr,
5189 &cpu_slabs_attr.attr,
5190 &ctor_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005191 &aliases_attr.attr,
5192 &align_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005193 &hwcache_align_attr.attr,
5194 &reclaim_account_attr.attr,
5195 &destroy_by_rcu_attr.attr,
Christoph Lametera5a84752010-10-05 13:57:27 -05005196 &shrink_attr.attr,
Lai Jiangshanab9a0f12011-03-10 15:21:48 +08005197 &reserved_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05005198 &slabs_cpu_partial_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005199#ifdef CONFIG_SLUB_DEBUG
Christoph Lametera5a84752010-10-05 13:57:27 -05005200 &total_objects_attr.attr,
5201 &slabs_attr.attr,
5202 &sanity_checks_attr.attr,
5203 &trace_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005204 &red_zone_attr.attr,
5205 &poison_attr.attr,
5206 &store_user_attr.attr,
Christoph Lameter53e15af2007-05-06 14:49:43 -07005207 &validate_attr.attr,
Christoph Lameter88a420e2007-05-06 14:49:45 -07005208 &alloc_calls_attr.attr,
5209 &free_calls_attr.attr,
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005210#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07005211#ifdef CONFIG_ZONE_DMA
5212 &cache_dma_attr.attr,
5213#endif
5214#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -08005215 &remote_node_defrag_ratio_attr.attr,
Christoph Lameter81819f02007-05-06 14:49:36 -07005216#endif
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005217#ifdef CONFIG_SLUB_STATS
5218 &alloc_fastpath_attr.attr,
5219 &alloc_slowpath_attr.attr,
5220 &free_fastpath_attr.attr,
5221 &free_slowpath_attr.attr,
5222 &free_frozen_attr.attr,
5223 &free_add_partial_attr.attr,
5224 &free_remove_partial_attr.attr,
5225 &alloc_from_partial_attr.attr,
5226 &alloc_slab_attr.attr,
5227 &alloc_refill_attr.attr,
Christoph Lametere36a2652011-06-01 12:25:57 -05005228 &alloc_node_mismatch_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005229 &free_slab_attr.attr,
5230 &cpuslab_flush_attr.attr,
5231 &deactivate_full_attr.attr,
5232 &deactivate_empty_attr.attr,
5233 &deactivate_to_head_attr.attr,
5234 &deactivate_to_tail_attr.attr,
5235 &deactivate_remote_frees_attr.attr,
Christoph Lameter03e404a2011-06-01 12:25:58 -05005236 &deactivate_bypass_attr.attr,
Christoph Lameter65c33762008-04-14 19:11:40 +03005237 &order_fallback_attr.attr,
Christoph Lameterb789ef52011-06-01 12:25:49 -05005238 &cmpxchg_double_fail_attr.attr,
5239 &cmpxchg_double_cpu_fail_attr.attr,
Christoph Lameter49e22582011-08-09 16:12:27 -05005240 &cpu_partial_alloc_attr.attr,
5241 &cpu_partial_free_attr.attr,
Alex Shi8028dce2012-02-03 23:34:56 +08005242 &cpu_partial_node_attr.attr,
5243 &cpu_partial_drain_attr.attr,
Christoph Lameter8ff12cf2008-02-07 17:47:41 -08005244#endif
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03005245#ifdef CONFIG_FAILSLAB
5246 &failslab_attr.attr,
5247#endif
5248
Christoph Lameter81819f02007-05-06 14:49:36 -07005249 NULL
5250};
5251
5252static struct attribute_group slab_attr_group = {
5253 .attrs = slab_attrs,
5254};
5255
5256static ssize_t slab_attr_show(struct kobject *kobj,
5257 struct attribute *attr,
5258 char *buf)
5259{
5260 struct slab_attribute *attribute;
5261 struct kmem_cache *s;
5262 int err;
5263
5264 attribute = to_slab_attr(attr);
5265 s = to_slab(kobj);
5266
5267 if (!attribute->show)
5268 return -EIO;
5269
5270 err = attribute->show(s, buf);
5271
5272 return err;
5273}
5274
5275static ssize_t slab_attr_store(struct kobject *kobj,
5276 struct attribute *attr,
5277 const char *buf, size_t len)
5278{
5279 struct slab_attribute *attribute;
5280 struct kmem_cache *s;
5281 int err;
5282
5283 attribute = to_slab_attr(attr);
5284 s = to_slab(kobj);
5285
5286 if (!attribute->store)
5287 return -EIO;
5288
5289 err = attribute->store(s, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08005290#ifdef CONFIG_MEMCG_KMEM
5291 if (slab_state >= FULL && err >= 0 && is_root_cache(s)) {
Vladimir Davydov426589f2015-02-12 14:59:23 -08005292 struct kmem_cache *c;
Christoph Lameter81819f02007-05-06 14:49:36 -07005293
Glauber Costa107dab52012-12-18 14:23:05 -08005294 mutex_lock(&slab_mutex);
5295 if (s->max_attr_size < len)
5296 s->max_attr_size = len;
5297
Glauber Costaebe945c2012-12-18 14:23:10 -08005298 /*
5299 * This is a best effort propagation, so this function's return
5300 * value will be determined by the parent cache only. This is
5301 * basically because not all attributes will have a well
5302 * defined semantics for rollbacks - most of the actions will
5303 * have permanent effects.
5304 *
5305 * Returning the error value of any of the children that fail
5306 * is not 100 % defined, in the sense that users seeing the
5307 * error code won't be able to know anything about the state of
5308 * the cache.
5309 *
5310 * Only returning the error code for the parent cache at least
5311 * has well defined semantics. The cache being written to
5312 * directly either failed or succeeded, in which case we loop
5313 * through the descendants with best-effort propagation.
5314 */
Vladimir Davydov426589f2015-02-12 14:59:23 -08005315 for_each_memcg_cache(c, s)
5316 attribute->store(c, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08005317 mutex_unlock(&slab_mutex);
5318 }
5319#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07005320 return err;
5321}
5322
Glauber Costa107dab52012-12-18 14:23:05 -08005323static void memcg_propagate_slab_attrs(struct kmem_cache *s)
5324{
5325#ifdef CONFIG_MEMCG_KMEM
5326 int i;
5327 char *buffer = NULL;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005328 struct kmem_cache *root_cache;
Glauber Costa107dab52012-12-18 14:23:05 -08005329
Vladimir Davydov93030d82014-05-06 12:49:59 -07005330 if (is_root_cache(s))
Glauber Costa107dab52012-12-18 14:23:05 -08005331 return;
5332
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08005333 root_cache = s->memcg_params.root_cache;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005334
Glauber Costa107dab52012-12-18 14:23:05 -08005335 /*
5336 * This mean this cache had no attribute written. Therefore, no point
5337 * in copying default values around
5338 */
Vladimir Davydov93030d82014-05-06 12:49:59 -07005339 if (!root_cache->max_attr_size)
Glauber Costa107dab52012-12-18 14:23:05 -08005340 return;
5341
5342 for (i = 0; i < ARRAY_SIZE(slab_attrs); i++) {
5343 char mbuf[64];
5344 char *buf;
5345 struct slab_attribute *attr = to_slab_attr(slab_attrs[i]);
Thomas Gleixner14bfe112017-06-02 14:46:25 -07005346 ssize_t len;
Glauber Costa107dab52012-12-18 14:23:05 -08005347
5348 if (!attr || !attr->store || !attr->show)
5349 continue;
5350
5351 /*
5352 * It is really bad that we have to allocate here, so we will
5353 * do it only as a fallback. If we actually allocate, though,
5354 * we can just use the allocated buffer until the end.
5355 *
5356 * Most of the slub attributes will tend to be very small in
5357 * size, but sysfs allows buffers up to a page, so they can
5358 * theoretically happen.
5359 */
5360 if (buffer)
5361 buf = buffer;
Vladimir Davydov93030d82014-05-06 12:49:59 -07005362 else if (root_cache->max_attr_size < ARRAY_SIZE(mbuf))
Glauber Costa107dab52012-12-18 14:23:05 -08005363 buf = mbuf;
5364 else {
5365 buffer = (char *) get_zeroed_page(GFP_KERNEL);
5366 if (WARN_ON(!buffer))
5367 continue;
5368 buf = buffer;
5369 }
5370
Thomas Gleixner14bfe112017-06-02 14:46:25 -07005371 len = attr->show(root_cache, buf);
5372 if (len > 0)
5373 attr->store(s, buf, len);
Glauber Costa107dab52012-12-18 14:23:05 -08005374 }
5375
5376 if (buffer)
5377 free_page((unsigned long)buffer);
5378#endif
5379}
5380
Christoph Lameter41a21282014-05-06 12:50:08 -07005381static void kmem_cache_release(struct kobject *k)
5382{
5383 slab_kmem_cache_release(to_slab(k));
5384}
5385
Emese Revfy52cf25d2010-01-19 02:58:23 +01005386static const struct sysfs_ops slab_sysfs_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005387 .show = slab_attr_show,
5388 .store = slab_attr_store,
5389};
5390
5391static struct kobj_type slab_ktype = {
5392 .sysfs_ops = &slab_sysfs_ops,
Christoph Lameter41a21282014-05-06 12:50:08 -07005393 .release = kmem_cache_release,
Christoph Lameter81819f02007-05-06 14:49:36 -07005394};
5395
5396static int uevent_filter(struct kset *kset, struct kobject *kobj)
5397{
5398 struct kobj_type *ktype = get_ktype(kobj);
5399
5400 if (ktype == &slab_ktype)
5401 return 1;
5402 return 0;
5403}
5404
Emese Revfy9cd43612009-12-31 14:52:51 +01005405static const struct kset_uevent_ops slab_uevent_ops = {
Christoph Lameter81819f02007-05-06 14:49:36 -07005406 .filter = uevent_filter,
5407};
5408
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005409static struct kset *slab_kset;
Christoph Lameter81819f02007-05-06 14:49:36 -07005410
Vladimir Davydov9a417072014-04-07 15:39:31 -07005411static inline struct kset *cache_kset(struct kmem_cache *s)
5412{
5413#ifdef CONFIG_MEMCG_KMEM
5414 if (!is_root_cache(s))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08005415 return s->memcg_params.root_cache->memcg_kset;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005416#endif
5417 return slab_kset;
5418}
5419
Christoph Lameter81819f02007-05-06 14:49:36 -07005420#define ID_STR_LENGTH 64
5421
5422/* Create a unique string id for a slab cache:
Christoph Lameter6446faa2008-02-15 23:45:26 -08005423 *
5424 * Format :[flags-]size
Christoph Lameter81819f02007-05-06 14:49:36 -07005425 */
5426static char *create_unique_id(struct kmem_cache *s)
5427{
5428 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
5429 char *p = name;
5430
5431 BUG_ON(!name);
5432
5433 *p++ = ':';
5434 /*
5435 * First flags affecting slabcache operations. We will only
5436 * get here for aliasable slabs so we do not need to support
5437 * too many flags. The flags here must cover all flags that
5438 * are matched during merging to guarantee that the id is
5439 * unique.
5440 */
5441 if (s->flags & SLAB_CACHE_DMA)
5442 *p++ = 'd';
5443 if (s->flags & SLAB_RECLAIM_ACCOUNT)
5444 *p++ = 'a';
5445 if (s->flags & SLAB_DEBUG_FREE)
5446 *p++ = 'F';
Vegard Nossum5a896d92008-04-04 00:54:48 +02005447 if (!(s->flags & SLAB_NOTRACK))
5448 *p++ = 't';
Christoph Lameter81819f02007-05-06 14:49:36 -07005449 if (p != name + 1)
5450 *p++ = '-';
5451 p += sprintf(p, "%07d", s->size);
Glauber Costa2633d7a2012-12-18 14:22:34 -08005452
Christoph Lameter81819f02007-05-06 14:49:36 -07005453 BUG_ON(p > name + ID_STR_LENGTH - 1);
5454 return name;
5455}
5456
5457static int sysfs_slab_add(struct kmem_cache *s)
5458{
5459 int err;
5460 const char *name;
Christoph Lameter45530c42012-11-28 16:23:07 +00005461 int unmergeable = slab_unmergeable(s);
Christoph Lameter81819f02007-05-06 14:49:36 -07005462
Christoph Lameter81819f02007-05-06 14:49:36 -07005463 if (unmergeable) {
5464 /*
5465 * Slabcache can never be merged so we can use the name proper.
5466 * This is typically the case for debug situations. In that
5467 * case we can catch duplicate names easily.
5468 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005469 sysfs_remove_link(&slab_kset->kobj, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005470 name = s->name;
5471 } else {
5472 /*
5473 * Create a unique name for the slab as a target
5474 * for the symlinks.
5475 */
5476 name = create_unique_id(s);
5477 }
5478
Vladimir Davydov9a417072014-04-07 15:39:31 -07005479 s->kobj.kset = cache_kset(s);
Tetsuo Handa26e4f202014-01-04 16:32:31 +09005480 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
Dave Jones54b6a732014-04-07 15:39:32 -07005481 if (err)
Konstantin Khlebnikov80da0262015-09-04 15:45:51 -07005482 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07005483
5484 err = sysfs_create_group(&s->kobj, &slab_attr_group);
Dave Jones54b6a732014-04-07 15:39:32 -07005485 if (err)
5486 goto out_del_kobj;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005487
5488#ifdef CONFIG_MEMCG_KMEM
5489 if (is_root_cache(s)) {
5490 s->memcg_kset = kset_create_and_add("cgroup", NULL, &s->kobj);
5491 if (!s->memcg_kset) {
Dave Jones54b6a732014-04-07 15:39:32 -07005492 err = -ENOMEM;
5493 goto out_del_kobj;
Vladimir Davydov9a417072014-04-07 15:39:31 -07005494 }
5495 }
5496#endif
5497
Christoph Lameter81819f02007-05-06 14:49:36 -07005498 kobject_uevent(&s->kobj, KOBJ_ADD);
5499 if (!unmergeable) {
5500 /* Setup first alias */
5501 sysfs_slab_alias(s, s->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005502 }
Dave Jones54b6a732014-04-07 15:39:32 -07005503out:
5504 if (!unmergeable)
5505 kfree(name);
5506 return err;
5507out_del_kobj:
5508 kobject_del(&s->kobj);
Dave Jones54b6a732014-04-07 15:39:32 -07005509 goto out;
Christoph Lameter81819f02007-05-06 14:49:36 -07005510}
5511
Christoph Lameter41a21282014-05-06 12:50:08 -07005512void sysfs_slab_remove(struct kmem_cache *s)
Christoph Lameter81819f02007-05-06 14:49:36 -07005513{
Christoph Lameter97d06602012-07-06 15:25:11 -05005514 if (slab_state < FULL)
Christoph Lameter2bce64852010-07-19 11:39:11 -05005515 /*
5516 * Sysfs has not been setup yet so no need to remove the
5517 * cache from sysfs.
5518 */
5519 return;
5520
Vladimir Davydov9a417072014-04-07 15:39:31 -07005521#ifdef CONFIG_MEMCG_KMEM
5522 kset_unregister(s->memcg_kset);
5523#endif
Christoph Lameter81819f02007-05-06 14:49:36 -07005524 kobject_uevent(&s->kobj, KOBJ_REMOVE);
5525 kobject_del(&s->kobj);
Christoph Lameter151c6022008-01-07 22:29:05 -08005526 kobject_put(&s->kobj);
Christoph Lameter81819f02007-05-06 14:49:36 -07005527}
5528
5529/*
5530 * Need to buffer aliases during bootup until sysfs becomes
Nick Andrew9f6c708e2008-12-05 14:08:08 +11005531 * available lest we lose that information.
Christoph Lameter81819f02007-05-06 14:49:36 -07005532 */
5533struct saved_alias {
5534 struct kmem_cache *s;
5535 const char *name;
5536 struct saved_alias *next;
5537};
5538
Adrian Bunk5af328a2007-07-17 04:03:27 -07005539static struct saved_alias *alias_list;
Christoph Lameter81819f02007-05-06 14:49:36 -07005540
5541static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
5542{
5543 struct saved_alias *al;
5544
Christoph Lameter97d06602012-07-06 15:25:11 -05005545 if (slab_state == FULL) {
Christoph Lameter81819f02007-05-06 14:49:36 -07005546 /*
5547 * If we have a leftover link then remove it.
5548 */
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005549 sysfs_remove_link(&slab_kset->kobj, name);
5550 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005551 }
5552
5553 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
5554 if (!al)
5555 return -ENOMEM;
5556
5557 al->s = s;
5558 al->name = name;
5559 al->next = alias_list;
5560 alias_list = al;
5561 return 0;
5562}
5563
5564static int __init slab_sysfs_init(void)
5565{
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005566 struct kmem_cache *s;
Christoph Lameter81819f02007-05-06 14:49:36 -07005567 int err;
5568
Christoph Lameter18004c52012-07-06 15:25:12 -05005569 mutex_lock(&slab_mutex);
Christoph Lameter2bce64852010-07-19 11:39:11 -05005570
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -08005571 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
Greg Kroah-Hartman27c3a312007-11-01 09:29:06 -06005572 if (!slab_kset) {
Christoph Lameter18004c52012-07-06 15:25:12 -05005573 mutex_unlock(&slab_mutex);
Fabian Frederickf9f58282014-06-04 16:06:34 -07005574 pr_err("Cannot register slab subsystem.\n");
Christoph Lameter81819f02007-05-06 14:49:36 -07005575 return -ENOSYS;
5576 }
5577
Christoph Lameter97d06602012-07-06 15:25:11 -05005578 slab_state = FULL;
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005579
Christoph Lameter5b95a4ac2007-07-17 04:03:19 -07005580 list_for_each_entry(s, &slab_caches, list) {
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005581 err = sysfs_slab_add(s);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005582 if (err)
Fabian Frederickf9f58282014-06-04 16:06:34 -07005583 pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
5584 s->name);
Christoph Lameter26a7bd02007-05-09 02:32:39 -07005585 }
Christoph Lameter81819f02007-05-06 14:49:36 -07005586
5587 while (alias_list) {
5588 struct saved_alias *al = alias_list;
5589
5590 alias_list = alias_list->next;
5591 err = sysfs_slab_alias(al->s, al->name);
Christoph Lameter5d540fb2007-08-30 23:56:26 -07005592 if (err)
Fabian Frederickf9f58282014-06-04 16:06:34 -07005593 pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
5594 al->name);
Christoph Lameter81819f02007-05-06 14:49:36 -07005595 kfree(al);
5596 }
5597
Christoph Lameter18004c52012-07-06 15:25:12 -05005598 mutex_unlock(&slab_mutex);
Christoph Lameter81819f02007-05-06 14:49:36 -07005599 resiliency_test();
5600 return 0;
5601}
5602
5603__initcall(slab_sysfs_init);
Christoph Lameterab4d5ed2010-10-05 13:57:26 -05005604#endif /* CONFIG_SYSFS */
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005605
5606/*
5607 * The /proc/slabinfo ABI
5608 */
Linus Torvalds158a9622008-01-02 13:04:48 -08005609#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04005610void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005611{
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005612 unsigned long nr_slabs = 0;
Christoph Lameter205ab992008-04-14 19:11:40 +03005613 unsigned long nr_objs = 0;
5614 unsigned long nr_free = 0;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005615 int node;
Christoph Lameterfa45dc22014-08-06 16:04:09 -07005616 struct kmem_cache_node *n;
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005617
Christoph Lameterfa45dc22014-08-06 16:04:09 -07005618 for_each_kmem_cache_node(s, node, n) {
Wanpeng Lic17fd132013-07-04 08:33:26 +08005619 nr_slabs += node_nr_slabs(n);
5620 nr_objs += node_nr_objs(n);
Christoph Lameter205ab992008-04-14 19:11:40 +03005621 nr_free += count_partial(n, count_free);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005622 }
5623
Glauber Costa0d7561c2012-10-19 18:20:27 +04005624 sinfo->active_objs = nr_objs - nr_free;
5625 sinfo->num_objs = nr_objs;
5626 sinfo->active_slabs = nr_slabs;
5627 sinfo->num_slabs = nr_slabs;
5628 sinfo->objects_per_slab = oo_objects(s->oo);
5629 sinfo->cache_order = oo_order(s->oo);
Pekka J Enberg57ed3ed2008-01-01 17:23:28 +01005630}
5631
Glauber Costa0d7561c2012-10-19 18:20:27 +04005632void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005633{
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005634}
5635
Glauber Costab7454ad2012-10-19 18:20:25 +04005636ssize_t slabinfo_write(struct file *file, const char __user *buffer,
5637 size_t count, loff_t *ppos)
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005638{
Glauber Costab7454ad2012-10-19 18:20:25 +04005639 return -EIO;
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04005640}
Linus Torvalds158a9622008-01-02 13:04:48 -08005641#endif /* CONFIG_SLABINFO */