]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/slub.c
slub: actually use 'message' in restore_bytes()
[mirror_ubuntu-jammy-kernel.git] / mm / slub.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
81819f0f
CL
2/*
3 * SLUB: A slab allocator that limits cache line use instead of queuing
4 * objects in per cpu and per node lists.
5 *
dc84207d 6 * The allocator synchronizes using per slab locks or atomic operations
881db7fb 7 * and only uses a centralized lock to manage a pool of partial slabs.
81819f0f 8 *
cde53535 9 * (C) 2007 SGI, Christoph Lameter
881db7fb 10 * (C) 2011 Linux Foundation, Christoph Lameter
81819f0f
CL
11 */
12
13#include <linux/mm.h>
1eb5ac64 14#include <linux/swap.h> /* struct reclaim_state */
81819f0f
CL
15#include <linux/module.h>
16#include <linux/bit_spinlock.h>
17#include <linux/interrupt.h>
1b3865d0 18#include <linux/swab.h>
81819f0f
CL
19#include <linux/bitops.h>
20#include <linux/slab.h>
97d06609 21#include "slab.h"
7b3c3a50 22#include <linux/proc_fs.h>
81819f0f 23#include <linux/seq_file.h>
a79316c6 24#include <linux/kasan.h>
81819f0f
CL
25#include <linux/cpu.h>
26#include <linux/cpuset.h>
27#include <linux/mempolicy.h>
28#include <linux/ctype.h>
3ac7fe5a 29#include <linux/debugobjects.h>
81819f0f 30#include <linux/kallsyms.h>
b89fb5ef 31#include <linux/kfence.h>
b9049e23 32#include <linux/memory.h>
f8bd2258 33#include <linux/math64.h>
773ff60e 34#include <linux/fault-inject.h>
bfa71457 35#include <linux/stacktrace.h>
4de900b4 36#include <linux/prefetch.h>
2633d7a0 37#include <linux/memcontrol.h>
2482ddec 38#include <linux/random.h>
1f9f78b1 39#include <kunit/test.h>
81819f0f 40
4a92379b
RK
41#include <trace/events/kmem.h>
42
072bb0aa
MG
43#include "internal.h"
44
81819f0f
CL
45/*
46 * Lock order:
18004c5d 47 * 1. slab_mutex (Global Mutex)
881db7fb
CL
48 * 2. node->list_lock
49 * 3. slab_lock(page) (Only on some arches and for debugging)
81819f0f 50 *
18004c5d 51 * slab_mutex
881db7fb 52 *
18004c5d 53 * The role of the slab_mutex is to protect the list of all the slabs
881db7fb
CL
54 * and to synchronize major metadata changes to slab cache structures.
55 *
56 * The slab_lock is only used for debugging and on arches that do not
b7ccc7f8 57 * have the ability to do a cmpxchg_double. It only protects:
881db7fb 58 * A. page->freelist -> List of object free in a page
b7ccc7f8
MW
59 * B. page->inuse -> Number of objects in use
60 * C. page->objects -> Number of objects in page
61 * D. page->frozen -> frozen state
881db7fb
CL
62 *
63 * If a slab is frozen then it is exempt from list management. It is not
632b2ef0
LX
64 * on any list except per cpu partial list. The processor that froze the
65 * slab is the one who can perform list operations on the page. Other
66 * processors may put objects onto the freelist but the processor that
67 * froze the slab is the only one that can retrieve the objects from the
68 * page's freelist.
81819f0f
CL
69 *
70 * The list_lock protects the partial and full list on each node and
71 * the partial slab counter. If taken then no new slabs may be added or
72 * removed from the lists nor make the number of partial slabs be modified.
73 * (Note that the total number of slabs is an atomic value that may be
74 * modified without taking the list lock).
75 *
76 * The list_lock is a centralized lock and thus we avoid taking it as
77 * much as possible. As long as SLUB does not have to handle partial
78 * slabs, operations can continue without any centralized lock. F.e.
79 * allocating a long series of objects that fill up slabs does not require
80 * the list lock.
81819f0f
CL
81 * Interrupts are disabled during allocation and deallocation in order to
82 * make the slab allocator safe to use in the context of an irq. In addition
83 * interrupts are disabled to ensure that the processor does not change
84 * while handling per_cpu slabs, due to kernel preemption.
85 *
86 * SLUB assigns one slab for allocation to each processor.
87 * Allocations only occur from these slabs called cpu slabs.
88 *
672bba3a
CL
89 * Slabs with free elements are kept on a partial list and during regular
90 * operations no list for full slabs is used. If an object in a full slab is
81819f0f 91 * freed then the slab will show up again on the partial lists.
672bba3a
CL
92 * We track full slabs for debugging purposes though because otherwise we
93 * cannot scan all objects.
81819f0f
CL
94 *
95 * Slabs are freed when they become empty. Teardown and setup is
96 * minimal so we rely on the page allocators per cpu caches for
97 * fast frees and allocs.
98 *
aed68148 99 * page->frozen The slab is frozen and exempt from list processing.
4b6f0750
CL
100 * This means that the slab is dedicated to a purpose
101 * such as satisfying allocations for a specific
102 * processor. Objects may be freed in the slab while
103 * it is frozen but slab_free will then skip the usual
104 * list operations. It is up to the processor holding
105 * the slab to integrate the slab into the slab lists
106 * when the slab is no longer needed.
107 *
108 * One use of this flag is to mark slabs that are
109 * used for allocations. Then such a slab becomes a cpu
110 * slab. The cpu slab may be equipped with an additional
dfb4f096 111 * freelist that allows lockless access to
894b8788
CL
112 * free objects in addition to the regular freelist
113 * that requires the slab lock.
81819f0f 114 *
aed68148 115 * SLAB_DEBUG_FLAGS Slab requires special handling due to debug
81819f0f 116 * options set. This moves slab handling out of
894b8788 117 * the fast path and disables lockless freelists.
81819f0f
CL
118 */
119
ca0cab65
VB
120#ifdef CONFIG_SLUB_DEBUG
121#ifdef CONFIG_SLUB_DEBUG_ON
122DEFINE_STATIC_KEY_TRUE(slub_debug_enabled);
123#else
124DEFINE_STATIC_KEY_FALSE(slub_debug_enabled);
125#endif
126#endif
127
59052e89
VB
128static inline bool kmem_cache_debug(struct kmem_cache *s)
129{
130 return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
af537b0a 131}
5577bd8a 132
117d54df 133void *fixup_red_left(struct kmem_cache *s, void *p)
d86bd1be 134{
59052e89 135 if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
d86bd1be
JK
136 p += s->red_left_pad;
137
138 return p;
139}
140
345c905d
JK
141static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
142{
143#ifdef CONFIG_SLUB_CPU_PARTIAL
144 return !kmem_cache_debug(s);
145#else
146 return false;
147#endif
148}
149
81819f0f
CL
150/*
151 * Issues still to be resolved:
152 *
81819f0f
CL
153 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
154 *
81819f0f
CL
155 * - Variable sizing of the per node arrays
156 */
157
b789ef51
CL
158/* Enable to log cmpxchg failures */
159#undef SLUB_DEBUG_CMPXCHG
160
2086d26a 161/*
dc84207d 162 * Minimum number of partial slabs. These will be left on the partial
2086d26a
CL
163 * lists even if they are empty. kmem_cache_shrink may reclaim them.
164 */
76be8950 165#define MIN_PARTIAL 5
e95eed57 166
2086d26a
CL
167/*
168 * Maximum number of desirable partial slabs.
169 * The existence of more partial slabs makes kmem_cache_shrink
721ae22a 170 * sort the partial list by the number of objects in use.
2086d26a
CL
171 */
172#define MAX_PARTIAL 10
173
becfda68 174#define DEBUG_DEFAULT_FLAGS (SLAB_CONSISTENCY_CHECKS | SLAB_RED_ZONE | \
81819f0f 175 SLAB_POISON | SLAB_STORE_USER)
672bba3a 176
149daaf3
LA
177/*
178 * These debug flags cannot use CMPXCHG because there might be consistency
179 * issues when checking or reading debug information
180 */
181#define SLAB_NO_CMPXCHG (SLAB_CONSISTENCY_CHECKS | SLAB_STORE_USER | \
182 SLAB_TRACE)
183
184
fa5ec8a1 185/*
3de47213
DR
186 * Debugging flags that require metadata to be stored in the slab. These get
187 * disabled when slub_debug=O is used and a cache's min order increases with
188 * metadata.
fa5ec8a1 189 */
3de47213 190#define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
fa5ec8a1 191
210b5c06
CG
192#define OO_SHIFT 16
193#define OO_MASK ((1 << OO_SHIFT) - 1)
50d5c41c 194#define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
210b5c06 195
81819f0f 196/* Internal SLUB flags */
d50112ed 197/* Poison object */
4fd0b46e 198#define __OBJECT_POISON ((slab_flags_t __force)0x80000000U)
d50112ed 199/* Use cmpxchg_double */
4fd0b46e 200#define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U)
81819f0f 201
02cbc874
CL
202/*
203 * Tracking user of a slab.
204 */
d6543e39 205#define TRACK_ADDRS_COUNT 16
02cbc874 206struct track {
ce71e27c 207 unsigned long addr; /* Called from address */
d6543e39
BG
208#ifdef CONFIG_STACKTRACE
209 unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
210#endif
02cbc874
CL
211 int cpu; /* Was running on cpu */
212 int pid; /* Pid context */
213 unsigned long when; /* When did the operation occur */
214};
215
216enum track_item { TRACK_ALLOC, TRACK_FREE };
217
ab4d5ed5 218#ifdef CONFIG_SYSFS
81819f0f
CL
219static int sysfs_slab_add(struct kmem_cache *);
220static int sysfs_slab_alias(struct kmem_cache *, const char *);
81819f0f 221#else
0c710013
CL
222static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
223static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
224 { return 0; }
81819f0f
CL
225#endif
226
4fdccdfb 227static inline void stat(const struct kmem_cache *s, enum stat_item si)
8ff12cfc
CL
228{
229#ifdef CONFIG_SLUB_STATS
88da03a6
CL
230 /*
231 * The rmw is racy on a preemptible kernel but this is acceptable, so
232 * avoid this_cpu_add()'s irq-disable overhead.
233 */
234 raw_cpu_inc(s->cpu_slab->stat[si]);
8ff12cfc
CL
235#endif
236}
237
7e1fa93d
VB
238/*
239 * Tracks for which NUMA nodes we have kmem_cache_nodes allocated.
240 * Corresponds to node_state[N_NORMAL_MEMORY], but can temporarily
241 * differ during memory hotplug/hotremove operations.
242 * Protected by slab_mutex.
243 */
244static nodemask_t slab_nodes;
245
81819f0f
CL
246/********************************************************************
247 * Core slab cache functions
248 *******************************************************************/
249
2482ddec
KC
250/*
251 * Returns freelist pointer (ptr). With hardening, this is obfuscated
252 * with an XOR of the address where the pointer is held and a per-cache
253 * random number.
254 */
255static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr,
256 unsigned long ptr_addr)
257{
258#ifdef CONFIG_SLAB_FREELIST_HARDENED
d36a63a9 259 /*
aa1ef4d7 260 * When CONFIG_KASAN_SW/HW_TAGS is enabled, ptr_addr might be tagged.
d36a63a9
AK
261 * Normally, this doesn't cause any issues, as both set_freepointer()
262 * and get_freepointer() are called with a pointer with the same tag.
263 * However, there are some issues with CONFIG_SLUB_DEBUG code. For
264 * example, when __free_slub() iterates over objects in a cache, it
265 * passes untagged pointers to check_object(). check_object() in turns
266 * calls get_freepointer() with an untagged pointer, which causes the
267 * freepointer to be restored incorrectly.
268 */
269 return (void *)((unsigned long)ptr ^ s->random ^
1ad53d9f 270 swab((unsigned long)kasan_reset_tag((void *)ptr_addr)));
2482ddec
KC
271#else
272 return ptr;
273#endif
274}
275
276/* Returns the freelist pointer recorded at location ptr_addr. */
277static inline void *freelist_dereference(const struct kmem_cache *s,
278 void *ptr_addr)
279{
280 return freelist_ptr(s, (void *)*(unsigned long *)(ptr_addr),
281 (unsigned long)ptr_addr);
282}
283
7656c72b
CL
284static inline void *get_freepointer(struct kmem_cache *s, void *object)
285{
aa1ef4d7 286 object = kasan_reset_tag(object);
2482ddec 287 return freelist_dereference(s, object + s->offset);
7656c72b
CL
288}
289
0ad9500e
ED
290static void prefetch_freepointer(const struct kmem_cache *s, void *object)
291{
0882ff91 292 prefetch(object + s->offset);
0ad9500e
ED
293}
294
1393d9a1
CL
295static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
296{
2482ddec 297 unsigned long freepointer_addr;
1393d9a1
CL
298 void *p;
299
8e57f8ac 300 if (!debug_pagealloc_enabled_static())
922d566c
JK
301 return get_freepointer(s, object);
302
f70b0049 303 object = kasan_reset_tag(object);
2482ddec 304 freepointer_addr = (unsigned long)object + s->offset;
fe557319 305 copy_from_kernel_nofault(&p, (void **)freepointer_addr, sizeof(p));
2482ddec 306 return freelist_ptr(s, p, freepointer_addr);
1393d9a1
CL
307}
308
7656c72b
CL
309static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
310{
2482ddec
KC
311 unsigned long freeptr_addr = (unsigned long)object + s->offset;
312
ce6fa91b
AP
313#ifdef CONFIG_SLAB_FREELIST_HARDENED
314 BUG_ON(object == fp); /* naive detection of double free or corruption */
315#endif
316
aa1ef4d7 317 freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr);
2482ddec 318 *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr);
7656c72b
CL
319}
320
321/* Loop over all objects in a slab */
224a88be 322#define for_each_object(__p, __s, __addr, __objects) \
d86bd1be
JK
323 for (__p = fixup_red_left(__s, __addr); \
324 __p < (__addr) + (__objects) * (__s)->size; \
325 __p += (__s)->size)
7656c72b 326
9736d2a9 327static inline unsigned int order_objects(unsigned int order, unsigned int size)
ab9a0f19 328{
9736d2a9 329 return ((unsigned int)PAGE_SIZE << order) / size;
ab9a0f19
LJ
330}
331
19af27af 332static inline struct kmem_cache_order_objects oo_make(unsigned int order,
9736d2a9 333 unsigned int size)
834f3d11
CL
334{
335 struct kmem_cache_order_objects x = {
9736d2a9 336 (order << OO_SHIFT) + order_objects(order, size)
834f3d11
CL
337 };
338
339 return x;
340}
341
19af27af 342static inline unsigned int oo_order(struct kmem_cache_order_objects x)
834f3d11 343{
210b5c06 344 return x.x >> OO_SHIFT;
834f3d11
CL
345}
346
19af27af 347static inline unsigned int oo_objects(struct kmem_cache_order_objects x)
834f3d11 348{
210b5c06 349 return x.x & OO_MASK;
834f3d11
CL
350}
351
881db7fb
CL
352/*
353 * Per slab locking using the pagelock
354 */
355static __always_inline void slab_lock(struct page *page)
356{
48c935ad 357 VM_BUG_ON_PAGE(PageTail(page), page);
881db7fb
CL
358 bit_spin_lock(PG_locked, &page->flags);
359}
360
361static __always_inline void slab_unlock(struct page *page)
362{
48c935ad 363 VM_BUG_ON_PAGE(PageTail(page), page);
881db7fb
CL
364 __bit_spin_unlock(PG_locked, &page->flags);
365}
366
1d07171c
CL
367/* Interrupts must be disabled (for the fallback code to work right) */
368static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
369 void *freelist_old, unsigned long counters_old,
370 void *freelist_new, unsigned long counters_new,
371 const char *n)
372{
373 VM_BUG_ON(!irqs_disabled());
2565409f
HC
374#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
375 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
1d07171c 376 if (s->flags & __CMPXCHG_DOUBLE) {
cdcd6298 377 if (cmpxchg_double(&page->freelist, &page->counters,
0aa9a13d
DC
378 freelist_old, counters_old,
379 freelist_new, counters_new))
6f6528a1 380 return true;
1d07171c
CL
381 } else
382#endif
383 {
384 slab_lock(page);
d0e0ac97
CG
385 if (page->freelist == freelist_old &&
386 page->counters == counters_old) {
1d07171c 387 page->freelist = freelist_new;
7d27a04b 388 page->counters = counters_new;
1d07171c 389 slab_unlock(page);
6f6528a1 390 return true;
1d07171c
CL
391 }
392 slab_unlock(page);
393 }
394
395 cpu_relax();
396 stat(s, CMPXCHG_DOUBLE_FAIL);
397
398#ifdef SLUB_DEBUG_CMPXCHG
f9f58285 399 pr_info("%s %s: cmpxchg double redo ", n, s->name);
1d07171c
CL
400#endif
401
6f6528a1 402 return false;
1d07171c
CL
403}
404
b789ef51
CL
405static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
406 void *freelist_old, unsigned long counters_old,
407 void *freelist_new, unsigned long counters_new,
408 const char *n)
409{
2565409f
HC
410#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
411 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
b789ef51 412 if (s->flags & __CMPXCHG_DOUBLE) {
cdcd6298 413 if (cmpxchg_double(&page->freelist, &page->counters,
0aa9a13d
DC
414 freelist_old, counters_old,
415 freelist_new, counters_new))
6f6528a1 416 return true;
b789ef51
CL
417 } else
418#endif
419 {
1d07171c
CL
420 unsigned long flags;
421
422 local_irq_save(flags);
881db7fb 423 slab_lock(page);
d0e0ac97
CG
424 if (page->freelist == freelist_old &&
425 page->counters == counters_old) {
b789ef51 426 page->freelist = freelist_new;
7d27a04b 427 page->counters = counters_new;
881db7fb 428 slab_unlock(page);
1d07171c 429 local_irq_restore(flags);
6f6528a1 430 return true;
b789ef51 431 }
881db7fb 432 slab_unlock(page);
1d07171c 433 local_irq_restore(flags);
b789ef51
CL
434 }
435
436 cpu_relax();
437 stat(s, CMPXCHG_DOUBLE_FAIL);
438
439#ifdef SLUB_DEBUG_CMPXCHG
f9f58285 440 pr_info("%s %s: cmpxchg double redo ", n, s->name);
b789ef51
CL
441#endif
442
6f6528a1 443 return false;
b789ef51
CL
444}
445
41ecc55b 446#ifdef CONFIG_SLUB_DEBUG
90e9f6a6
YZ
447static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
448static DEFINE_SPINLOCK(object_map_lock);
449
1f9f78b1
OG
450#if IS_ENABLED(CONFIG_KUNIT)
451static bool slab_add_kunit_errors(void)
452{
453 struct kunit_resource *resource;
454
455 if (likely(!current->kunit_test))
456 return false;
457
458 resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
459 if (!resource)
460 return false;
461
462 (*(int *)resource->data)++;
463 kunit_put_resource(resource);
464 return true;
465}
466#else
467static inline bool slab_add_kunit_errors(void) { return false; }
468#endif
469
5f80b13a
CL
470/*
471 * Determine a map of object in use on a page.
472 *
881db7fb 473 * Node listlock must be held to guarantee that the page does
5f80b13a
CL
474 * not vanish from under us.
475 */
90e9f6a6 476static unsigned long *get_map(struct kmem_cache *s, struct page *page)
31364c2e 477 __acquires(&object_map_lock)
5f80b13a
CL
478{
479 void *p;
480 void *addr = page_address(page);
481
90e9f6a6
YZ
482 VM_BUG_ON(!irqs_disabled());
483
484 spin_lock(&object_map_lock);
485
486 bitmap_zero(object_map, page->objects);
487
5f80b13a 488 for (p = page->freelist; p; p = get_freepointer(s, p))
4138fdfc 489 set_bit(__obj_to_index(s, addr, p), object_map);
90e9f6a6
YZ
490
491 return object_map;
492}
493
81aba9e0 494static void put_map(unsigned long *map) __releases(&object_map_lock)
90e9f6a6
YZ
495{
496 VM_BUG_ON(map != object_map);
90e9f6a6 497 spin_unlock(&object_map_lock);
5f80b13a
CL
498}
499
870b1fbb 500static inline unsigned int size_from_object(struct kmem_cache *s)
d86bd1be
JK
501{
502 if (s->flags & SLAB_RED_ZONE)
503 return s->size - s->red_left_pad;
504
505 return s->size;
506}
507
508static inline void *restore_red_left(struct kmem_cache *s, void *p)
509{
510 if (s->flags & SLAB_RED_ZONE)
511 p -= s->red_left_pad;
512
513 return p;
514}
515
41ecc55b
CL
516/*
517 * Debug settings:
518 */
89d3c87e 519#if defined(CONFIG_SLUB_DEBUG_ON)
d50112ed 520static slab_flags_t slub_debug = DEBUG_DEFAULT_FLAGS;
f0630fff 521#else
d50112ed 522static slab_flags_t slub_debug;
f0630fff 523#endif
41ecc55b 524
e17f1dfb 525static char *slub_debug_string;
fa5ec8a1 526static int disable_higher_order_debug;
41ecc55b 527
a79316c6
AR
528/*
529 * slub is about to manipulate internal object metadata. This memory lies
530 * outside the range of the allocated object, so accessing it would normally
531 * be reported by kasan as a bounds error. metadata_access_enable() is used
532 * to tell kasan that these accesses are OK.
533 */
534static inline void metadata_access_enable(void)
535{
536 kasan_disable_current();
537}
538
539static inline void metadata_access_disable(void)
540{
541 kasan_enable_current();
542}
543
81819f0f
CL
544/*
545 * Object debugging
546 */
d86bd1be
JK
547
548/* Verify that a pointer has an address that is valid within a slab page */
549static inline int check_valid_pointer(struct kmem_cache *s,
550 struct page *page, void *object)
551{
552 void *base;
553
554 if (!object)
555 return 1;
556
557 base = page_address(page);
338cfaad 558 object = kasan_reset_tag(object);
d86bd1be
JK
559 object = restore_red_left(s, object);
560 if (object < base || object >= base + page->objects * s->size ||
561 (object - base) % s->size) {
562 return 0;
563 }
564
565 return 1;
566}
567
aa2efd5e
DT
568static void print_section(char *level, char *text, u8 *addr,
569 unsigned int length)
81819f0f 570{
a79316c6 571 metadata_access_enable();
aa1ef4d7
AK
572 print_hex_dump(level, kasan_reset_tag(text), DUMP_PREFIX_ADDRESS,
573 16, 1, addr, length, 1);
a79316c6 574 metadata_access_disable();
81819f0f
CL
575}
576
cbfc35a4
WL
577/*
578 * See comment in calculate_sizes().
579 */
580static inline bool freeptr_outside_object(struct kmem_cache *s)
581{
582 return s->offset >= s->inuse;
583}
584
585/*
586 * Return offset of the end of info block which is inuse + free pointer if
587 * not overlapping with object.
588 */
589static inline unsigned int get_info_end(struct kmem_cache *s)
590{
591 if (freeptr_outside_object(s))
592 return s->inuse + sizeof(void *);
593 else
594 return s->inuse;
595}
596
81819f0f
CL
597static struct track *get_track(struct kmem_cache *s, void *object,
598 enum track_item alloc)
599{
600 struct track *p;
601
cbfc35a4 602 p = object + get_info_end(s);
81819f0f 603
aa1ef4d7 604 return kasan_reset_tag(p + alloc);
81819f0f
CL
605}
606
607static void set_track(struct kmem_cache *s, void *object,
ce71e27c 608 enum track_item alloc, unsigned long addr)
81819f0f 609{
1a00df4a 610 struct track *p = get_track(s, object, alloc);
81819f0f 611
81819f0f 612 if (addr) {
d6543e39 613#ifdef CONFIG_STACKTRACE
79716799 614 unsigned int nr_entries;
d6543e39 615
a79316c6 616 metadata_access_enable();
aa1ef4d7
AK
617 nr_entries = stack_trace_save(kasan_reset_tag(p->addrs),
618 TRACK_ADDRS_COUNT, 3);
a79316c6 619 metadata_access_disable();
d6543e39 620
79716799
TG
621 if (nr_entries < TRACK_ADDRS_COUNT)
622 p->addrs[nr_entries] = 0;
d6543e39 623#endif
81819f0f
CL
624 p->addr = addr;
625 p->cpu = smp_processor_id();
88e4ccf2 626 p->pid = current->pid;
81819f0f 627 p->when = jiffies;
b8ca7ff7 628 } else {
81819f0f 629 memset(p, 0, sizeof(struct track));
b8ca7ff7 630 }
81819f0f
CL
631}
632
81819f0f
CL
633static void init_tracking(struct kmem_cache *s, void *object)
634{
24922684
CL
635 if (!(s->flags & SLAB_STORE_USER))
636 return;
637
ce71e27c
EGM
638 set_track(s, object, TRACK_FREE, 0UL);
639 set_track(s, object, TRACK_ALLOC, 0UL);
81819f0f
CL
640}
641
86609d33 642static void print_track(const char *s, struct track *t, unsigned long pr_time)
81819f0f
CL
643{
644 if (!t->addr)
645 return;
646
96b94abc 647 pr_err("%s in %pS age=%lu cpu=%u pid=%d\n",
86609d33 648 s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid);
d6543e39
BG
649#ifdef CONFIG_STACKTRACE
650 {
651 int i;
652 for (i = 0; i < TRACK_ADDRS_COUNT; i++)
653 if (t->addrs[i])
f9f58285 654 pr_err("\t%pS\n", (void *)t->addrs[i]);
d6543e39
BG
655 else
656 break;
657 }
658#endif
24922684
CL
659}
660
e42f174e 661void print_tracking(struct kmem_cache *s, void *object)
24922684 662{
86609d33 663 unsigned long pr_time = jiffies;
24922684
CL
664 if (!(s->flags & SLAB_STORE_USER))
665 return;
666
86609d33
CP
667 print_track("Allocated", get_track(s, object, TRACK_ALLOC), pr_time);
668 print_track("Freed", get_track(s, object, TRACK_FREE), pr_time);
24922684
CL
669}
670
671static void print_page_info(struct page *page)
672{
96b94abc 673 pr_err("Slab 0x%p objects=%u used=%u fp=0x%p flags=%#lx(%pGp)\n",
4a8ef190
YS
674 page, page->objects, page->inuse, page->freelist,
675 page->flags, &page->flags);
24922684
CL
676
677}
678
679static void slab_bug(struct kmem_cache *s, char *fmt, ...)
680{
ecc42fbe 681 struct va_format vaf;
24922684 682 va_list args;
24922684
CL
683
684 va_start(args, fmt);
ecc42fbe
FF
685 vaf.fmt = fmt;
686 vaf.va = &args;
f9f58285 687 pr_err("=============================================================================\n");
ecc42fbe 688 pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf);
f9f58285 689 pr_err("-----------------------------------------------------------------------------\n\n");
645df230 690
373d4d09 691 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
ecc42fbe 692 va_end(args);
81819f0f
CL
693}
694
24922684
CL
695static void slab_fix(struct kmem_cache *s, char *fmt, ...)
696{
ecc42fbe 697 struct va_format vaf;
24922684 698 va_list args;
24922684 699
1f9f78b1
OG
700 if (slab_add_kunit_errors())
701 return;
702
24922684 703 va_start(args, fmt);
ecc42fbe
FF
704 vaf.fmt = fmt;
705 vaf.va = &args;
706 pr_err("FIX %s: %pV\n", s->name, &vaf);
24922684 707 va_end(args);
24922684
CL
708}
709
52f23478 710static bool freelist_corrupted(struct kmem_cache *s, struct page *page,
dc07a728 711 void **freelist, void *nextfree)
52f23478
DZ
712{
713 if ((s->flags & SLAB_CONSISTENCY_CHECKS) &&
dc07a728
ER
714 !check_valid_pointer(s, page, nextfree) && freelist) {
715 object_err(s, page, *freelist, "Freechain corrupt");
716 *freelist = NULL;
52f23478
DZ
717 slab_fix(s, "Isolate corrupted freechain");
718 return true;
719 }
720
721 return false;
722}
723
24922684 724static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
81819f0f
CL
725{
726 unsigned int off; /* Offset of last byte */
a973e9dd 727 u8 *addr = page_address(page);
24922684
CL
728
729 print_tracking(s, p);
730
731 print_page_info(page);
732
96b94abc 733 pr_err("Object 0x%p @offset=%tu fp=0x%p\n\n",
f9f58285 734 p, p - addr, get_freepointer(s, p));
24922684 735
d86bd1be 736 if (s->flags & SLAB_RED_ZONE)
8669dbab 737 print_section(KERN_ERR, "Redzone ", p - s->red_left_pad,
aa2efd5e 738 s->red_left_pad);
d86bd1be 739 else if (p > addr + 16)
aa2efd5e 740 print_section(KERN_ERR, "Bytes b4 ", p - 16, 16);
81819f0f 741
8669dbab 742 print_section(KERN_ERR, "Object ", p,
1b473f29 743 min_t(unsigned int, s->object_size, PAGE_SIZE));
81819f0f 744 if (s->flags & SLAB_RED_ZONE)
8669dbab 745 print_section(KERN_ERR, "Redzone ", p + s->object_size,
3b0efdfa 746 s->inuse - s->object_size);
81819f0f 747
cbfc35a4 748 off = get_info_end(s);
81819f0f 749
24922684 750 if (s->flags & SLAB_STORE_USER)
81819f0f 751 off += 2 * sizeof(struct track);
81819f0f 752
80a9201a
AP
753 off += kasan_metadata_size(s);
754
d86bd1be 755 if (off != size_from_object(s))
81819f0f 756 /* Beginning of the filler is the free pointer */
8669dbab 757 print_section(KERN_ERR, "Padding ", p + off,
aa2efd5e 758 size_from_object(s) - off);
24922684
CL
759
760 dump_stack();
81819f0f
CL
761}
762
75c66def 763void object_err(struct kmem_cache *s, struct page *page,
81819f0f
CL
764 u8 *object, char *reason)
765{
1f9f78b1
OG
766 if (slab_add_kunit_errors())
767 return;
768
3dc50637 769 slab_bug(s, "%s", reason);
24922684 770 print_trailer(s, page, object);
81819f0f
CL
771}
772
a38965bf 773static __printf(3, 4) void slab_err(struct kmem_cache *s, struct page *page,
d0e0ac97 774 const char *fmt, ...)
81819f0f
CL
775{
776 va_list args;
777 char buf[100];
778
1f9f78b1
OG
779 if (slab_add_kunit_errors())
780 return;
781
24922684
CL
782 va_start(args, fmt);
783 vsnprintf(buf, sizeof(buf), fmt, args);
81819f0f 784 va_end(args);
3dc50637 785 slab_bug(s, "%s", buf);
24922684 786 print_page_info(page);
81819f0f
CL
787 dump_stack();
788}
789
f7cb1933 790static void init_object(struct kmem_cache *s, void *object, u8 val)
81819f0f 791{
aa1ef4d7 792 u8 *p = kasan_reset_tag(object);
81819f0f 793
d86bd1be
JK
794 if (s->flags & SLAB_RED_ZONE)
795 memset(p - s->red_left_pad, val, s->red_left_pad);
796
81819f0f 797 if (s->flags & __OBJECT_POISON) {
3b0efdfa
CL
798 memset(p, POISON_FREE, s->object_size - 1);
799 p[s->object_size - 1] = POISON_END;
81819f0f
CL
800 }
801
802 if (s->flags & SLAB_RED_ZONE)
3b0efdfa 803 memset(p + s->object_size, val, s->inuse - s->object_size);
81819f0f
CL
804}
805
24922684
CL
806static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
807 void *from, void *to)
808{
1a88ef87 809 slab_fix(s, "Restoring %s 0x%p-0x%p=0x%x\n", message, from, to - 1, data);
24922684
CL
810 memset(from, data, to - from);
811}
812
813static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
814 u8 *object, char *what,
06428780 815 u8 *start, unsigned int value, unsigned int bytes)
24922684
CL
816{
817 u8 *fault;
818 u8 *end;
e1b70dd1 819 u8 *addr = page_address(page);
24922684 820
a79316c6 821 metadata_access_enable();
aa1ef4d7 822 fault = memchr_inv(kasan_reset_tag(start), value, bytes);
a79316c6 823 metadata_access_disable();
24922684
CL
824 if (!fault)
825 return 1;
826
827 end = start + bytes;
828 while (end > fault && end[-1] == value)
829 end--;
830
1f9f78b1
OG
831 if (slab_add_kunit_errors())
832 goto skip_bug_print;
833
24922684 834 slab_bug(s, "%s overwritten", what);
96b94abc 835 pr_err("0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n",
e1b70dd1
MC
836 fault, end - 1, fault - addr,
837 fault[0], value);
24922684
CL
838 print_trailer(s, page, object);
839
1f9f78b1 840skip_bug_print:
24922684
CL
841 restore_bytes(s, what, value, fault, end);
842 return 0;
81819f0f
CL
843}
844
81819f0f
CL
845/*
846 * Object layout:
847 *
848 * object address
849 * Bytes of the object to be managed.
850 * If the freepointer may overlay the object then the free
cbfc35a4 851 * pointer is at the middle of the object.
672bba3a 852 *
81819f0f
CL
853 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
854 * 0xa5 (POISON_END)
855 *
3b0efdfa 856 * object + s->object_size
81819f0f 857 * Padding to reach word boundary. This is also used for Redzoning.
672bba3a 858 * Padding is extended by another word if Redzoning is enabled and
3b0efdfa 859 * object_size == inuse.
672bba3a 860 *
81819f0f
CL
861 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
862 * 0xcc (RED_ACTIVE) for objects in use.
863 *
864 * object + s->inuse
672bba3a
CL
865 * Meta data starts here.
866 *
81819f0f
CL
867 * A. Free pointer (if we cannot overwrite object on free)
868 * B. Tracking data for SLAB_STORE_USER
dc84207d 869 * C. Padding to reach required alignment boundary or at minimum
6446faa2 870 * one word if debugging is on to be able to detect writes
672bba3a
CL
871 * before the word boundary.
872 *
873 * Padding is done using 0x5a (POISON_INUSE)
81819f0f
CL
874 *
875 * object + s->size
672bba3a 876 * Nothing is used beyond s->size.
81819f0f 877 *
3b0efdfa 878 * If slabcaches are merged then the object_size and inuse boundaries are mostly
672bba3a 879 * ignored. And therefore no slab options that rely on these boundaries
81819f0f
CL
880 * may be used with merged slabcaches.
881 */
882
81819f0f
CL
883static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
884{
cbfc35a4 885 unsigned long off = get_info_end(s); /* The end of info */
81819f0f
CL
886
887 if (s->flags & SLAB_STORE_USER)
888 /* We also have user information there */
889 off += 2 * sizeof(struct track);
890
80a9201a
AP
891 off += kasan_metadata_size(s);
892
d86bd1be 893 if (size_from_object(s) == off)
81819f0f
CL
894 return 1;
895
24922684 896 return check_bytes_and_report(s, page, p, "Object padding",
d86bd1be 897 p + off, POISON_INUSE, size_from_object(s) - off);
81819f0f
CL
898}
899
39b26464 900/* Check the pad bytes at the end of a slab page */
81819f0f
CL
901static int slab_pad_check(struct kmem_cache *s, struct page *page)
902{
24922684
CL
903 u8 *start;
904 u8 *fault;
905 u8 *end;
5d682681 906 u8 *pad;
24922684
CL
907 int length;
908 int remainder;
81819f0f
CL
909
910 if (!(s->flags & SLAB_POISON))
911 return 1;
912
a973e9dd 913 start = page_address(page);
a50b854e 914 length = page_size(page);
39b26464
CL
915 end = start + length;
916 remainder = length % s->size;
81819f0f
CL
917 if (!remainder)
918 return 1;
919
5d682681 920 pad = end - remainder;
a79316c6 921 metadata_access_enable();
aa1ef4d7 922 fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder);
a79316c6 923 metadata_access_disable();
24922684
CL
924 if (!fault)
925 return 1;
926 while (end > fault && end[-1] == POISON_INUSE)
927 end--;
928
e1b70dd1
MC
929 slab_err(s, page, "Padding overwritten. 0x%p-0x%p @offset=%tu",
930 fault, end - 1, fault - start);
5d682681 931 print_section(KERN_ERR, "Padding ", pad, remainder);
24922684 932
5d682681 933 restore_bytes(s, "slab padding", POISON_INUSE, fault, end);
24922684 934 return 0;
81819f0f
CL
935}
936
937static int check_object(struct kmem_cache *s, struct page *page,
f7cb1933 938 void *object, u8 val)
81819f0f
CL
939{
940 u8 *p = object;
3b0efdfa 941 u8 *endobject = object + s->object_size;
81819f0f
CL
942
943 if (s->flags & SLAB_RED_ZONE) {
8669dbab 944 if (!check_bytes_and_report(s, page, object, "Left Redzone",
d86bd1be
JK
945 object - s->red_left_pad, val, s->red_left_pad))
946 return 0;
947
8669dbab 948 if (!check_bytes_and_report(s, page, object, "Right Redzone",
3b0efdfa 949 endobject, val, s->inuse - s->object_size))
81819f0f 950 return 0;
81819f0f 951 } else {
3b0efdfa 952 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
3adbefee 953 check_bytes_and_report(s, page, p, "Alignment padding",
d0e0ac97
CG
954 endobject, POISON_INUSE,
955 s->inuse - s->object_size);
3adbefee 956 }
81819f0f
CL
957 }
958
959 if (s->flags & SLAB_POISON) {
f7cb1933 960 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
24922684 961 (!check_bytes_and_report(s, page, p, "Poison", p,
3b0efdfa 962 POISON_FREE, s->object_size - 1) ||
8669dbab 963 !check_bytes_and_report(s, page, p, "End Poison",
3b0efdfa 964 p + s->object_size - 1, POISON_END, 1)))
81819f0f 965 return 0;
81819f0f
CL
966 /*
967 * check_pad_bytes cleans up on its own.
968 */
969 check_pad_bytes(s, page, p);
970 }
971
cbfc35a4 972 if (!freeptr_outside_object(s) && val == SLUB_RED_ACTIVE)
81819f0f
CL
973 /*
974 * Object and freepointer overlap. Cannot check
975 * freepointer while object is allocated.
976 */
977 return 1;
978
979 /* Check free pointer validity */
980 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
981 object_err(s, page, p, "Freepointer corrupt");
982 /*
9f6c708e 983 * No choice but to zap it and thus lose the remainder
81819f0f 984 * of the free objects in this slab. May cause
672bba3a 985 * another error because the object count is now wrong.
81819f0f 986 */
a973e9dd 987 set_freepointer(s, p, NULL);
81819f0f
CL
988 return 0;
989 }
990 return 1;
991}
992
993static int check_slab(struct kmem_cache *s, struct page *page)
994{
39b26464
CL
995 int maxobj;
996
81819f0f
CL
997 VM_BUG_ON(!irqs_disabled());
998
999 if (!PageSlab(page)) {
24922684 1000 slab_err(s, page, "Not a valid slab page");
81819f0f
CL
1001 return 0;
1002 }
39b26464 1003
9736d2a9 1004 maxobj = order_objects(compound_order(page), s->size);
39b26464
CL
1005 if (page->objects > maxobj) {
1006 slab_err(s, page, "objects %u > max %u",
f6edde9c 1007 page->objects, maxobj);
39b26464
CL
1008 return 0;
1009 }
1010 if (page->inuse > page->objects) {
24922684 1011 slab_err(s, page, "inuse %u > max %u",
f6edde9c 1012 page->inuse, page->objects);
81819f0f
CL
1013 return 0;
1014 }
1015 /* Slab_pad_check fixes things up after itself */
1016 slab_pad_check(s, page);
1017 return 1;
1018}
1019
1020/*
672bba3a
CL
1021 * Determine if a certain object on a page is on the freelist. Must hold the
1022 * slab lock to guarantee that the chains are in a consistent state.
81819f0f
CL
1023 */
1024static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
1025{
1026 int nr = 0;
881db7fb 1027 void *fp;
81819f0f 1028 void *object = NULL;
f6edde9c 1029 int max_objects;
81819f0f 1030
881db7fb 1031 fp = page->freelist;
39b26464 1032 while (fp && nr <= page->objects) {
81819f0f
CL
1033 if (fp == search)
1034 return 1;
1035 if (!check_valid_pointer(s, page, fp)) {
1036 if (object) {
1037 object_err(s, page, object,
1038 "Freechain corrupt");
a973e9dd 1039 set_freepointer(s, object, NULL);
81819f0f 1040 } else {
24922684 1041 slab_err(s, page, "Freepointer corrupt");
a973e9dd 1042 page->freelist = NULL;
39b26464 1043 page->inuse = page->objects;
24922684 1044 slab_fix(s, "Freelist cleared");
81819f0f
CL
1045 return 0;
1046 }
1047 break;
1048 }
1049 object = fp;
1050 fp = get_freepointer(s, object);
1051 nr++;
1052 }
1053
9736d2a9 1054 max_objects = order_objects(compound_order(page), s->size);
210b5c06
CG
1055 if (max_objects > MAX_OBJS_PER_PAGE)
1056 max_objects = MAX_OBJS_PER_PAGE;
224a88be
CL
1057
1058 if (page->objects != max_objects) {
756a025f
JP
1059 slab_err(s, page, "Wrong number of objects. Found %d but should be %d",
1060 page->objects, max_objects);
224a88be
CL
1061 page->objects = max_objects;
1062 slab_fix(s, "Number of objects adjusted.");
1063 }
39b26464 1064 if (page->inuse != page->objects - nr) {
756a025f
JP
1065 slab_err(s, page, "Wrong object count. Counter is %d but counted were %d",
1066 page->inuse, page->objects - nr);
39b26464 1067 page->inuse = page->objects - nr;
24922684 1068 slab_fix(s, "Object count adjusted.");
81819f0f
CL
1069 }
1070 return search == NULL;
1071}
1072
0121c619
CL
1073static void trace(struct kmem_cache *s, struct page *page, void *object,
1074 int alloc)
3ec09742
CL
1075{
1076 if (s->flags & SLAB_TRACE) {
f9f58285 1077 pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
3ec09742
CL
1078 s->name,
1079 alloc ? "alloc" : "free",
1080 object, page->inuse,
1081 page->freelist);
1082
1083 if (!alloc)
aa2efd5e 1084 print_section(KERN_INFO, "Object ", (void *)object,
d0e0ac97 1085 s->object_size);
3ec09742
CL
1086
1087 dump_stack();
1088 }
1089}
1090
643b1138 1091/*
672bba3a 1092 * Tracking of fully allocated slabs for debugging purposes.
643b1138 1093 */
5cc6eee8
CL
1094static void add_full(struct kmem_cache *s,
1095 struct kmem_cache_node *n, struct page *page)
643b1138 1096{
5cc6eee8
CL
1097 if (!(s->flags & SLAB_STORE_USER))
1098 return;
1099
255d0884 1100 lockdep_assert_held(&n->list_lock);
916ac052 1101 list_add(&page->slab_list, &n->full);
643b1138
CL
1102}
1103
c65c1877 1104static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct page *page)
643b1138 1105{
643b1138
CL
1106 if (!(s->flags & SLAB_STORE_USER))
1107 return;
1108
255d0884 1109 lockdep_assert_held(&n->list_lock);
916ac052 1110 list_del(&page->slab_list);
643b1138
CL
1111}
1112
0f389ec6
CL
1113/* Tracking of the number of slabs for debugging purposes */
1114static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1115{
1116 struct kmem_cache_node *n = get_node(s, node);
1117
1118 return atomic_long_read(&n->nr_slabs);
1119}
1120
26c02cf0
AB
1121static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1122{
1123 return atomic_long_read(&n->nr_slabs);
1124}
1125
205ab99d 1126static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
1127{
1128 struct kmem_cache_node *n = get_node(s, node);
1129
1130 /*
1131 * May be called early in order to allocate a slab for the
1132 * kmem_cache_node structure. Solve the chicken-egg
1133 * dilemma by deferring the increment of the count during
1134 * bootstrap (see early_kmem_cache_node_alloc).
1135 */
338b2642 1136 if (likely(n)) {
0f389ec6 1137 atomic_long_inc(&n->nr_slabs);
205ab99d
CL
1138 atomic_long_add(objects, &n->total_objects);
1139 }
0f389ec6 1140}
205ab99d 1141static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
0f389ec6
CL
1142{
1143 struct kmem_cache_node *n = get_node(s, node);
1144
1145 atomic_long_dec(&n->nr_slabs);
205ab99d 1146 atomic_long_sub(objects, &n->total_objects);
0f389ec6
CL
1147}
1148
1149/* Object debug checks for alloc/free paths */
3ec09742
CL
1150static void setup_object_debug(struct kmem_cache *s, struct page *page,
1151 void *object)
1152{
8fc8d666 1153 if (!kmem_cache_debug_flags(s, SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON))
3ec09742
CL
1154 return;
1155
f7cb1933 1156 init_object(s, object, SLUB_RED_INACTIVE);
3ec09742
CL
1157 init_tracking(s, object);
1158}
1159
a50b854e
MWO
1160static
1161void setup_page_debug(struct kmem_cache *s, struct page *page, void *addr)
a7101224 1162{
8fc8d666 1163 if (!kmem_cache_debug_flags(s, SLAB_POISON))
a7101224
AK
1164 return;
1165
1166 metadata_access_enable();
aa1ef4d7 1167 memset(kasan_reset_tag(addr), POISON_INUSE, page_size(page));
a7101224
AK
1168 metadata_access_disable();
1169}
1170
becfda68 1171static inline int alloc_consistency_checks(struct kmem_cache *s,
278d7756 1172 struct page *page, void *object)
81819f0f
CL
1173{
1174 if (!check_slab(s, page))
becfda68 1175 return 0;
81819f0f 1176
81819f0f
CL
1177 if (!check_valid_pointer(s, page, object)) {
1178 object_err(s, page, object, "Freelist Pointer check fails");
becfda68 1179 return 0;
81819f0f
CL
1180 }
1181
f7cb1933 1182 if (!check_object(s, page, object, SLUB_RED_INACTIVE))
becfda68
LA
1183 return 0;
1184
1185 return 1;
1186}
1187
1188static noinline int alloc_debug_processing(struct kmem_cache *s,
1189 struct page *page,
1190 void *object, unsigned long addr)
1191{
1192 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
278d7756 1193 if (!alloc_consistency_checks(s, page, object))
becfda68
LA
1194 goto bad;
1195 }
81819f0f 1196
3ec09742
CL
1197 /* Success perform special debug activities for allocs */
1198 if (s->flags & SLAB_STORE_USER)
1199 set_track(s, object, TRACK_ALLOC, addr);
1200 trace(s, page, object, 1);
f7cb1933 1201 init_object(s, object, SLUB_RED_ACTIVE);
81819f0f 1202 return 1;
3ec09742 1203
81819f0f
CL
1204bad:
1205 if (PageSlab(page)) {
1206 /*
1207 * If this is a slab page then lets do the best we can
1208 * to avoid issues in the future. Marking all objects
672bba3a 1209 * as used avoids touching the remaining objects.
81819f0f 1210 */
24922684 1211 slab_fix(s, "Marking all objects used");
39b26464 1212 page->inuse = page->objects;
a973e9dd 1213 page->freelist = NULL;
81819f0f
CL
1214 }
1215 return 0;
1216}
1217
becfda68
LA
1218static inline int free_consistency_checks(struct kmem_cache *s,
1219 struct page *page, void *object, unsigned long addr)
81819f0f 1220{
81819f0f 1221 if (!check_valid_pointer(s, page, object)) {
70d71228 1222 slab_err(s, page, "Invalid object pointer 0x%p", object);
becfda68 1223 return 0;
81819f0f
CL
1224 }
1225
1226 if (on_freelist(s, page, object)) {
24922684 1227 object_err(s, page, object, "Object already free");
becfda68 1228 return 0;
81819f0f
CL
1229 }
1230
f7cb1933 1231 if (!check_object(s, page, object, SLUB_RED_ACTIVE))
becfda68 1232 return 0;
81819f0f 1233
1b4f59e3 1234 if (unlikely(s != page->slab_cache)) {
3adbefee 1235 if (!PageSlab(page)) {
756a025f
JP
1236 slab_err(s, page, "Attempt to free object(0x%p) outside of slab",
1237 object);
1b4f59e3 1238 } else if (!page->slab_cache) {
f9f58285
FF
1239 pr_err("SLUB <none>: no slab for object 0x%p.\n",
1240 object);
70d71228 1241 dump_stack();
06428780 1242 } else
24922684
CL
1243 object_err(s, page, object,
1244 "page slab pointer corrupt.");
becfda68
LA
1245 return 0;
1246 }
1247 return 1;
1248}
1249
1250/* Supports checking bulk free of a constructed freelist */
1251static noinline int free_debug_processing(
1252 struct kmem_cache *s, struct page *page,
1253 void *head, void *tail, int bulk_cnt,
1254 unsigned long addr)
1255{
1256 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1257 void *object = head;
1258 int cnt = 0;
3f649ab7 1259 unsigned long flags;
becfda68
LA
1260 int ret = 0;
1261
1262 spin_lock_irqsave(&n->list_lock, flags);
1263 slab_lock(page);
1264
1265 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
1266 if (!check_slab(s, page))
1267 goto out;
1268 }
1269
1270next_object:
1271 cnt++;
1272
1273 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
1274 if (!free_consistency_checks(s, page, object, addr))
1275 goto out;
81819f0f 1276 }
3ec09742 1277
3ec09742
CL
1278 if (s->flags & SLAB_STORE_USER)
1279 set_track(s, object, TRACK_FREE, addr);
1280 trace(s, page, object, 0);
81084651 1281 /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
f7cb1933 1282 init_object(s, object, SLUB_RED_INACTIVE);
81084651
JDB
1283
1284 /* Reached end of constructed freelist yet? */
1285 if (object != tail) {
1286 object = get_freepointer(s, object);
1287 goto next_object;
1288 }
804aa132
LA
1289 ret = 1;
1290
5c2e4bbb 1291out:
81084651
JDB
1292 if (cnt != bulk_cnt)
1293 slab_err(s, page, "Bulk freelist count(%d) invalid(%d)\n",
1294 bulk_cnt, cnt);
1295
881db7fb 1296 slab_unlock(page);
282acb43 1297 spin_unlock_irqrestore(&n->list_lock, flags);
804aa132
LA
1298 if (!ret)
1299 slab_fix(s, "Object at 0x%p not freed", object);
1300 return ret;
81819f0f
CL
1301}
1302
e17f1dfb
VB
1303/*
1304 * Parse a block of slub_debug options. Blocks are delimited by ';'
1305 *
1306 * @str: start of block
1307 * @flags: returns parsed flags, or DEBUG_DEFAULT_FLAGS if none specified
1308 * @slabs: return start of list of slabs, or NULL when there's no list
1309 * @init: assume this is initial parsing and not per-kmem-create parsing
1310 *
1311 * returns the start of next block if there's any, or NULL
1312 */
1313static char *
1314parse_slub_debug_flags(char *str, slab_flags_t *flags, char **slabs, bool init)
41ecc55b 1315{
e17f1dfb 1316 bool higher_order_disable = false;
f0630fff 1317
e17f1dfb
VB
1318 /* Skip any completely empty blocks */
1319 while (*str && *str == ';')
1320 str++;
1321
1322 if (*str == ',') {
f0630fff
CL
1323 /*
1324 * No options but restriction on slabs. This means full
1325 * debugging for slabs matching a pattern.
1326 */
e17f1dfb 1327 *flags = DEBUG_DEFAULT_FLAGS;
f0630fff 1328 goto check_slabs;
e17f1dfb
VB
1329 }
1330 *flags = 0;
f0630fff 1331
e17f1dfb
VB
1332 /* Determine which debug features should be switched on */
1333 for (; *str && *str != ',' && *str != ';'; str++) {
f0630fff 1334 switch (tolower(*str)) {
e17f1dfb
VB
1335 case '-':
1336 *flags = 0;
1337 break;
f0630fff 1338 case 'f':
e17f1dfb 1339 *flags |= SLAB_CONSISTENCY_CHECKS;
f0630fff
CL
1340 break;
1341 case 'z':
e17f1dfb 1342 *flags |= SLAB_RED_ZONE;
f0630fff
CL
1343 break;
1344 case 'p':
e17f1dfb 1345 *flags |= SLAB_POISON;
f0630fff
CL
1346 break;
1347 case 'u':
e17f1dfb 1348 *flags |= SLAB_STORE_USER;
f0630fff
CL
1349 break;
1350 case 't':
e17f1dfb 1351 *flags |= SLAB_TRACE;
f0630fff 1352 break;
4c13dd3b 1353 case 'a':
e17f1dfb 1354 *flags |= SLAB_FAILSLAB;
4c13dd3b 1355 break;
08303a73
CA
1356 case 'o':
1357 /*
1358 * Avoid enabling debugging on caches if its minimum
1359 * order would increase as a result.
1360 */
e17f1dfb 1361 higher_order_disable = true;
08303a73 1362 break;
f0630fff 1363 default:
e17f1dfb
VB
1364 if (init)
1365 pr_err("slub_debug option '%c' unknown. skipped\n", *str);
f0630fff 1366 }
41ecc55b 1367 }
f0630fff 1368check_slabs:
41ecc55b 1369 if (*str == ',')
e17f1dfb
VB
1370 *slabs = ++str;
1371 else
1372 *slabs = NULL;
1373
1374 /* Skip over the slab list */
1375 while (*str && *str != ';')
1376 str++;
1377
1378 /* Skip any completely empty blocks */
1379 while (*str && *str == ';')
1380 str++;
1381
1382 if (init && higher_order_disable)
1383 disable_higher_order_debug = 1;
1384
1385 if (*str)
1386 return str;
1387 else
1388 return NULL;
1389}
1390
1391static int __init setup_slub_debug(char *str)
1392{
1393 slab_flags_t flags;
1394 char *saved_str;
1395 char *slab_list;
1396 bool global_slub_debug_changed = false;
1397 bool slab_list_specified = false;
1398
1399 slub_debug = DEBUG_DEFAULT_FLAGS;
1400 if (*str++ != '=' || !*str)
1401 /*
1402 * No options specified. Switch on full debugging.
1403 */
1404 goto out;
1405
1406 saved_str = str;
1407 while (str) {
1408 str = parse_slub_debug_flags(str, &flags, &slab_list, true);
1409
1410 if (!slab_list) {
1411 slub_debug = flags;
1412 global_slub_debug_changed = true;
1413 } else {
1414 slab_list_specified = true;
1415 }
1416 }
1417
1418 /*
1419 * For backwards compatibility, a single list of flags with list of
1420 * slabs means debugging is only enabled for those slabs, so the global
1421 * slub_debug should be 0. We can extended that to multiple lists as
1422 * long as there is no option specifying flags without a slab list.
1423 */
1424 if (slab_list_specified) {
1425 if (!global_slub_debug_changed)
1426 slub_debug = 0;
1427 slub_debug_string = saved_str;
1428 }
f0630fff 1429out:
ca0cab65
VB
1430 if (slub_debug != 0 || slub_debug_string)
1431 static_branch_enable(&slub_debug_enabled);
02ac47d0
SB
1432 else
1433 static_branch_disable(&slub_debug_enabled);
6471384a
AP
1434 if ((static_branch_unlikely(&init_on_alloc) ||
1435 static_branch_unlikely(&init_on_free)) &&
1436 (slub_debug & SLAB_POISON))
1437 pr_info("mem auto-init: SLAB_POISON will take precedence over init_on_alloc/init_on_free\n");
41ecc55b
CL
1438 return 1;
1439}
1440
1441__setup("slub_debug", setup_slub_debug);
1442
c5fd3ca0
AT
1443/*
1444 * kmem_cache_flags - apply debugging options to the cache
1445 * @object_size: the size of an object without meta data
1446 * @flags: flags to set
1447 * @name: name of the cache
c5fd3ca0
AT
1448 *
1449 * Debug option(s) are applied to @flags. In addition to the debug
1450 * option(s), if a slab name (or multiple) is specified i.e.
1451 * slub_debug=<Debug-Options>,<slab name1>,<slab name2> ...
1452 * then only the select slabs will receive the debug option(s).
1453 */
0293d1fd 1454slab_flags_t kmem_cache_flags(unsigned int object_size,
37540008 1455 slab_flags_t flags, const char *name)
41ecc55b 1456{
c5fd3ca0
AT
1457 char *iter;
1458 size_t len;
e17f1dfb
VB
1459 char *next_block;
1460 slab_flags_t block_flags;
ca220593
JB
1461 slab_flags_t slub_debug_local = slub_debug;
1462
1463 /*
1464 * If the slab cache is for debugging (e.g. kmemleak) then
1465 * don't store user (stack trace) information by default,
1466 * but let the user enable it via the command line below.
1467 */
1468 if (flags & SLAB_NOLEAKTRACE)
1469 slub_debug_local &= ~SLAB_STORE_USER;
c5fd3ca0 1470
c5fd3ca0 1471 len = strlen(name);
e17f1dfb
VB
1472 next_block = slub_debug_string;
1473 /* Go through all blocks of debug options, see if any matches our slab's name */
1474 while (next_block) {
1475 next_block = parse_slub_debug_flags(next_block, &block_flags, &iter, false);
1476 if (!iter)
1477 continue;
1478 /* Found a block that has a slab list, search it */
1479 while (*iter) {
1480 char *end, *glob;
1481 size_t cmplen;
1482
1483 end = strchrnul(iter, ',');
1484 if (next_block && next_block < end)
1485 end = next_block - 1;
1486
1487 glob = strnchr(iter, end - iter, '*');
1488 if (glob)
1489 cmplen = glob - iter;
1490 else
1491 cmplen = max_t(size_t, len, (end - iter));
c5fd3ca0 1492
e17f1dfb
VB
1493 if (!strncmp(name, iter, cmplen)) {
1494 flags |= block_flags;
1495 return flags;
1496 }
c5fd3ca0 1497
e17f1dfb
VB
1498 if (!*end || *end == ';')
1499 break;
1500 iter = end + 1;
c5fd3ca0 1501 }
c5fd3ca0 1502 }
ba0268a8 1503
ca220593 1504 return flags | slub_debug_local;
41ecc55b 1505}
b4a64718 1506#else /* !CONFIG_SLUB_DEBUG */
3ec09742
CL
1507static inline void setup_object_debug(struct kmem_cache *s,
1508 struct page *page, void *object) {}
a50b854e
MWO
1509static inline
1510void setup_page_debug(struct kmem_cache *s, struct page *page, void *addr) {}
41ecc55b 1511
3ec09742 1512static inline int alloc_debug_processing(struct kmem_cache *s,
ce71e27c 1513 struct page *page, void *object, unsigned long addr) { return 0; }
41ecc55b 1514
282acb43 1515static inline int free_debug_processing(
81084651
JDB
1516 struct kmem_cache *s, struct page *page,
1517 void *head, void *tail, int bulk_cnt,
282acb43 1518 unsigned long addr) { return 0; }
41ecc55b 1519
41ecc55b
CL
1520static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1521 { return 1; }
1522static inline int check_object(struct kmem_cache *s, struct page *page,
f7cb1933 1523 void *object, u8 val) { return 1; }
5cc6eee8
CL
1524static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
1525 struct page *page) {}
c65c1877
PZ
1526static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
1527 struct page *page) {}
0293d1fd 1528slab_flags_t kmem_cache_flags(unsigned int object_size,
37540008 1529 slab_flags_t flags, const char *name)
ba0268a8
CL
1530{
1531 return flags;
1532}
41ecc55b 1533#define slub_debug 0
0f389ec6 1534
fdaa45e9
IM
1535#define disable_higher_order_debug 0
1536
0f389ec6
CL
1537static inline unsigned long slabs_node(struct kmem_cache *s, int node)
1538 { return 0; }
26c02cf0
AB
1539static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1540 { return 0; }
205ab99d
CL
1541static inline void inc_slabs_node(struct kmem_cache *s, int node,
1542 int objects) {}
1543static inline void dec_slabs_node(struct kmem_cache *s, int node,
1544 int objects) {}
7d550c56 1545
52f23478 1546static bool freelist_corrupted(struct kmem_cache *s, struct page *page,
dc07a728 1547 void **freelist, void *nextfree)
52f23478
DZ
1548{
1549 return false;
1550}
02e72cc6
AR
1551#endif /* CONFIG_SLUB_DEBUG */
1552
1553/*
1554 * Hooks for other subsystems that check memory allocations. In a typical
1555 * production configuration these hooks all should produce no code at all.
1556 */
0116523c 1557static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
d56791b3 1558{
53128245 1559 ptr = kasan_kmalloc_large(ptr, size, flags);
a2f77575 1560 /* As ptr might get tagged, call kmemleak hook after KASAN. */
d56791b3 1561 kmemleak_alloc(ptr, size, 1, flags);
53128245 1562 return ptr;
d56791b3
RB
1563}
1564
ee3ce779 1565static __always_inline void kfree_hook(void *x)
d56791b3
RB
1566{
1567 kmemleak_free(x);
027b37b5 1568 kasan_kfree_large(x);
d56791b3
RB
1569}
1570
d57a964e
AK
1571static __always_inline bool slab_free_hook(struct kmem_cache *s,
1572 void *x, bool init)
d56791b3
RB
1573{
1574 kmemleak_free_recursive(x, s->flags);
7d550c56 1575
02e72cc6
AR
1576 /*
1577 * Trouble is that we may no longer disable interrupts in the fast path
1578 * So in order to make the debug calls that expect irqs to be
1579 * disabled we need to disable interrupts temporarily.
1580 */
4675ff05 1581#ifdef CONFIG_LOCKDEP
02e72cc6
AR
1582 {
1583 unsigned long flags;
1584
1585 local_irq_save(flags);
02e72cc6
AR
1586 debug_check_no_locks_freed(x, s->object_size);
1587 local_irq_restore(flags);
1588 }
1589#endif
1590 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1591 debug_check_no_obj_freed(x, s->object_size);
0316bec2 1592
cfbe1636
ME
1593 /* Use KCSAN to help debug racy use-after-free. */
1594 if (!(s->flags & SLAB_TYPESAFE_BY_RCU))
1595 __kcsan_check_access(x, s->object_size,
1596 KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT);
1597
d57a964e
AK
1598 /*
1599 * As memory initialization might be integrated into KASAN,
1600 * kasan_slab_free and initialization memset's must be
1601 * kept together to avoid discrepancies in behavior.
1602 *
1603 * The initialization memset's clear the object and the metadata,
1604 * but don't touch the SLAB redzone.
1605 */
1606 if (init) {
1607 int rsize;
1608
1609 if (!kasan_has_integrated_init())
1610 memset(kasan_reset_tag(x), 0, s->object_size);
1611 rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0;
1612 memset((char *)kasan_reset_tag(x) + s->inuse, 0,
1613 s->size - s->inuse - rsize);
1614 }
1615 /* KASAN might put x into memory quarantine, delaying its reuse. */
1616 return kasan_slab_free(s, x, init);
02e72cc6 1617}
205ab99d 1618
c3895391
AK
1619static inline bool slab_free_freelist_hook(struct kmem_cache *s,
1620 void **head, void **tail)
81084651 1621{
6471384a
AP
1622
1623 void *object;
1624 void *next = *head;
1625 void *old_tail = *tail ? *tail : *head;
6471384a 1626
b89fb5ef 1627 if (is_kfence_address(next)) {
d57a964e 1628 slab_free_hook(s, next, false);
b89fb5ef
AP
1629 return true;
1630 }
1631
aea4df4c
LA
1632 /* Head and tail of the reconstructed freelist */
1633 *head = NULL;
1634 *tail = NULL;
1b7e816f 1635
aea4df4c
LA
1636 do {
1637 object = next;
1638 next = get_freepointer(s, object);
1639
c3895391 1640 /* If object's reuse doesn't have to be delayed */
d57a964e 1641 if (!slab_free_hook(s, object, slab_want_init_on_free(s))) {
c3895391
AK
1642 /* Move object to the new freelist */
1643 set_freepointer(s, object, *head);
1644 *head = object;
1645 if (!*tail)
1646 *tail = object;
1647 }
1648 } while (object != old_tail);
1649
1650 if (*head == *tail)
1651 *tail = NULL;
1652
1653 return *head != NULL;
81084651
JDB
1654}
1655
4d176711 1656static void *setup_object(struct kmem_cache *s, struct page *page,
588f8ba9
TG
1657 void *object)
1658{
1659 setup_object_debug(s, page, object);
4d176711 1660 object = kasan_init_slab_obj(s, object);
588f8ba9
TG
1661 if (unlikely(s->ctor)) {
1662 kasan_unpoison_object_data(s, object);
1663 s->ctor(object);
1664 kasan_poison_object_data(s, object);
1665 }
4d176711 1666 return object;
588f8ba9
TG
1667}
1668
81819f0f
CL
1669/*
1670 * Slab allocation and freeing
1671 */
5dfb4175
VD
1672static inline struct page *alloc_slab_page(struct kmem_cache *s,
1673 gfp_t flags, int node, struct kmem_cache_order_objects oo)
65c3376a 1674{
5dfb4175 1675 struct page *page;
19af27af 1676 unsigned int order = oo_order(oo);
65c3376a 1677
2154a336 1678 if (node == NUMA_NO_NODE)
5dfb4175 1679 page = alloc_pages(flags, order);
65c3376a 1680 else
96db800f 1681 page = __alloc_pages_node(node, flags, order);
5dfb4175 1682
5dfb4175 1683 return page;
65c3376a
CL
1684}
1685
210e7a43
TG
1686#ifdef CONFIG_SLAB_FREELIST_RANDOM
1687/* Pre-initialize the random sequence cache */
1688static int init_cache_random_seq(struct kmem_cache *s)
1689{
19af27af 1690 unsigned int count = oo_objects(s->oo);
210e7a43 1691 int err;
210e7a43 1692
a810007a
SR
1693 /* Bailout if already initialised */
1694 if (s->random_seq)
1695 return 0;
1696
210e7a43
TG
1697 err = cache_random_seq_create(s, count, GFP_KERNEL);
1698 if (err) {
1699 pr_err("SLUB: Unable to initialize free list for %s\n",
1700 s->name);
1701 return err;
1702 }
1703
1704 /* Transform to an offset on the set of pages */
1705 if (s->random_seq) {
19af27af
AD
1706 unsigned int i;
1707
210e7a43
TG
1708 for (i = 0; i < count; i++)
1709 s->random_seq[i] *= s->size;
1710 }
1711 return 0;
1712}
1713
1714/* Initialize each random sequence freelist per cache */
1715static void __init init_freelist_randomization(void)
1716{
1717 struct kmem_cache *s;
1718
1719 mutex_lock(&slab_mutex);
1720
1721 list_for_each_entry(s, &slab_caches, list)
1722 init_cache_random_seq(s);
1723
1724 mutex_unlock(&slab_mutex);
1725}
1726
1727/* Get the next entry on the pre-computed freelist randomized */
1728static void *next_freelist_entry(struct kmem_cache *s, struct page *page,
1729 unsigned long *pos, void *start,
1730 unsigned long page_limit,
1731 unsigned long freelist_count)
1732{
1733 unsigned int idx;
1734
1735 /*
1736 * If the target page allocation failed, the number of objects on the
1737 * page might be smaller than the usual size defined by the cache.
1738 */
1739 do {
1740 idx = s->random_seq[*pos];
1741 *pos += 1;
1742 if (*pos >= freelist_count)
1743 *pos = 0;
1744 } while (unlikely(idx >= page_limit));
1745
1746 return (char *)start + idx;
1747}
1748
1749/* Shuffle the single linked freelist based on a random pre-computed sequence */
1750static bool shuffle_freelist(struct kmem_cache *s, struct page *page)
1751{
1752 void *start;
1753 void *cur;
1754 void *next;
1755 unsigned long idx, pos, page_limit, freelist_count;
1756
1757 if (page->objects < 2 || !s->random_seq)
1758 return false;
1759
1760 freelist_count = oo_objects(s->oo);
1761 pos = get_random_int() % freelist_count;
1762
1763 page_limit = page->objects * s->size;
1764 start = fixup_red_left(s, page_address(page));
1765
1766 /* First entry is used as the base of the freelist */
1767 cur = next_freelist_entry(s, page, &pos, start, page_limit,
1768 freelist_count);
4d176711 1769 cur = setup_object(s, page, cur);
210e7a43
TG
1770 page->freelist = cur;
1771
1772 for (idx = 1; idx < page->objects; idx++) {
210e7a43
TG
1773 next = next_freelist_entry(s, page, &pos, start, page_limit,
1774 freelist_count);
4d176711 1775 next = setup_object(s, page, next);
210e7a43
TG
1776 set_freepointer(s, cur, next);
1777 cur = next;
1778 }
210e7a43
TG
1779 set_freepointer(s, cur, NULL);
1780
1781 return true;
1782}
1783#else
1784static inline int init_cache_random_seq(struct kmem_cache *s)
1785{
1786 return 0;
1787}
1788static inline void init_freelist_randomization(void) { }
1789static inline bool shuffle_freelist(struct kmem_cache *s, struct page *page)
1790{
1791 return false;
1792}
1793#endif /* CONFIG_SLAB_FREELIST_RANDOM */
1794
81819f0f
CL
1795static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1796{
06428780 1797 struct page *page;
834f3d11 1798 struct kmem_cache_order_objects oo = s->oo;
ba52270d 1799 gfp_t alloc_gfp;
4d176711 1800 void *start, *p, *next;
a50b854e 1801 int idx;
210e7a43 1802 bool shuffle;
81819f0f 1803
7e0528da
CL
1804 flags &= gfp_allowed_mask;
1805
d0164adc 1806 if (gfpflags_allow_blocking(flags))
7e0528da
CL
1807 local_irq_enable();
1808
b7a49f0d 1809 flags |= s->allocflags;
e12ba74d 1810
ba52270d
PE
1811 /*
1812 * Let the initial higher-order allocation fail under memory pressure
1813 * so we fall-back to the minimum order allocation.
1814 */
1815 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
d0164adc 1816 if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min))
444eb2a4 1817 alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~(__GFP_RECLAIM|__GFP_NOFAIL);
ba52270d 1818
5dfb4175 1819 page = alloc_slab_page(s, alloc_gfp, node, oo);
65c3376a
CL
1820 if (unlikely(!page)) {
1821 oo = s->min;
80c3a998 1822 alloc_gfp = flags;
65c3376a
CL
1823 /*
1824 * Allocation may have failed due to fragmentation.
1825 * Try a lower order alloc if possible
1826 */
5dfb4175 1827 page = alloc_slab_page(s, alloc_gfp, node, oo);
588f8ba9
TG
1828 if (unlikely(!page))
1829 goto out;
1830 stat(s, ORDER_FALLBACK);
65c3376a 1831 }
5a896d9e 1832
834f3d11 1833 page->objects = oo_objects(oo);
81819f0f 1834
2e9bd483 1835 account_slab_page(page, oo_order(oo), s, flags);
1f3147b4 1836
1b4f59e3 1837 page->slab_cache = s;
c03f94cc 1838 __SetPageSlab(page);
2f064f34 1839 if (page_is_pfmemalloc(page))
072bb0aa 1840 SetPageSlabPfmemalloc(page);
81819f0f 1841
a7101224 1842 kasan_poison_slab(page);
81819f0f 1843
a7101224 1844 start = page_address(page);
81819f0f 1845
a50b854e 1846 setup_page_debug(s, page, start);
0316bec2 1847
210e7a43
TG
1848 shuffle = shuffle_freelist(s, page);
1849
1850 if (!shuffle) {
4d176711
AK
1851 start = fixup_red_left(s, start);
1852 start = setup_object(s, page, start);
1853 page->freelist = start;
18e50661
AK
1854 for (idx = 0, p = start; idx < page->objects - 1; idx++) {
1855 next = p + s->size;
1856 next = setup_object(s, page, next);
1857 set_freepointer(s, p, next);
1858 p = next;
1859 }
1860 set_freepointer(s, p, NULL);
81819f0f 1861 }
81819f0f 1862
e6e82ea1 1863 page->inuse = page->objects;
8cb0a506 1864 page->frozen = 1;
588f8ba9 1865
81819f0f 1866out:
d0164adc 1867 if (gfpflags_allow_blocking(flags))
588f8ba9
TG
1868 local_irq_disable();
1869 if (!page)
1870 return NULL;
1871
588f8ba9
TG
1872 inc_slabs_node(s, page_to_nid(page), page->objects);
1873
81819f0f
CL
1874 return page;
1875}
1876
588f8ba9
TG
1877static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1878{
44405099
LL
1879 if (unlikely(flags & GFP_SLAB_BUG_MASK))
1880 flags = kmalloc_fix_flags(flags);
588f8ba9
TG
1881
1882 return allocate_slab(s,
1883 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1884}
1885
81819f0f
CL
1886static void __free_slab(struct kmem_cache *s, struct page *page)
1887{
834f3d11
CL
1888 int order = compound_order(page);
1889 int pages = 1 << order;
81819f0f 1890
8fc8d666 1891 if (kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) {
81819f0f
CL
1892 void *p;
1893
1894 slab_pad_check(s, page);
224a88be
CL
1895 for_each_object(p, s, page_address(page),
1896 page->objects)
f7cb1933 1897 check_object(s, page, p, SLUB_RED_INACTIVE);
81819f0f
CL
1898 }
1899
072bb0aa 1900 __ClearPageSlabPfmemalloc(page);
49bd5221 1901 __ClearPageSlab(page);
0c06dd75
VB
1902 /* In union with page->mapping where page allocator expects NULL */
1903 page->slab_cache = NULL;
1eb5ac64
NP
1904 if (current->reclaim_state)
1905 current->reclaim_state->reclaimed_slab += pages;
74d555be 1906 unaccount_slab_page(page, order, s);
27ee57c9 1907 __free_pages(page, order);
81819f0f
CL
1908}
1909
1910static void rcu_free_slab(struct rcu_head *h)
1911{
bf68c214 1912 struct page *page = container_of(h, struct page, rcu_head);
da9a638c 1913
1b4f59e3 1914 __free_slab(page->slab_cache, page);
81819f0f
CL
1915}
1916
1917static void free_slab(struct kmem_cache *s, struct page *page)
1918{
5f0d5a3a 1919 if (unlikely(s->flags & SLAB_TYPESAFE_BY_RCU)) {
bf68c214 1920 call_rcu(&page->rcu_head, rcu_free_slab);
81819f0f
CL
1921 } else
1922 __free_slab(s, page);
1923}
1924
1925static void discard_slab(struct kmem_cache *s, struct page *page)
1926{
205ab99d 1927 dec_slabs_node(s, page_to_nid(page), page->objects);
81819f0f
CL
1928 free_slab(s, page);
1929}
1930
1931/*
5cc6eee8 1932 * Management of partially allocated slabs.
81819f0f 1933 */
1e4dd946
SR
1934static inline void
1935__add_partial(struct kmem_cache_node *n, struct page *page, int tail)
81819f0f 1936{
e95eed57 1937 n->nr_partial++;
136333d1 1938 if (tail == DEACTIVATE_TO_TAIL)
916ac052 1939 list_add_tail(&page->slab_list, &n->partial);
7c2e132c 1940 else
916ac052 1941 list_add(&page->slab_list, &n->partial);
81819f0f
CL
1942}
1943
1e4dd946
SR
1944static inline void add_partial(struct kmem_cache_node *n,
1945 struct page *page, int tail)
62e346a8 1946{
c65c1877 1947 lockdep_assert_held(&n->list_lock);
1e4dd946
SR
1948 __add_partial(n, page, tail);
1949}
c65c1877 1950
1e4dd946
SR
1951static inline void remove_partial(struct kmem_cache_node *n,
1952 struct page *page)
1953{
1954 lockdep_assert_held(&n->list_lock);
916ac052 1955 list_del(&page->slab_list);
52b4b950 1956 n->nr_partial--;
1e4dd946
SR
1957}
1958
81819f0f 1959/*
7ced3719
CL
1960 * Remove slab from the partial list, freeze it and
1961 * return the pointer to the freelist.
81819f0f 1962 *
497b66f2 1963 * Returns a list of objects or NULL if it fails.
81819f0f 1964 */
497b66f2 1965static inline void *acquire_slab(struct kmem_cache *s,
acd19fd1 1966 struct kmem_cache_node *n, struct page *page,
633b0764 1967 int mode, int *objects)
81819f0f 1968{
2cfb7455
CL
1969 void *freelist;
1970 unsigned long counters;
1971 struct page new;
1972
c65c1877
PZ
1973 lockdep_assert_held(&n->list_lock);
1974
2cfb7455
CL
1975 /*
1976 * Zap the freelist and set the frozen bit.
1977 * The old freelist is the list of objects for the
1978 * per cpu allocation list.
1979 */
7ced3719
CL
1980 freelist = page->freelist;
1981 counters = page->counters;
1982 new.counters = counters;
633b0764 1983 *objects = new.objects - new.inuse;
23910c50 1984 if (mode) {
7ced3719 1985 new.inuse = page->objects;
23910c50
PE
1986 new.freelist = NULL;
1987 } else {
1988 new.freelist = freelist;
1989 }
2cfb7455 1990
a0132ac0 1991 VM_BUG_ON(new.frozen);
7ced3719 1992 new.frozen = 1;
2cfb7455 1993
7ced3719 1994 if (!__cmpxchg_double_slab(s, page,
2cfb7455 1995 freelist, counters,
02d7633f 1996 new.freelist, new.counters,
7ced3719 1997 "acquire_slab"))
7ced3719 1998 return NULL;
2cfb7455
CL
1999
2000 remove_partial(n, page);
7ced3719 2001 WARN_ON(!freelist);
49e22585 2002 return freelist;
81819f0f
CL
2003}
2004
633b0764 2005static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
8ba00bb6 2006static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
49e22585 2007
81819f0f 2008/*
672bba3a 2009 * Try to allocate a partial slab from a specific node.
81819f0f 2010 */
8ba00bb6
JK
2011static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
2012 struct kmem_cache_cpu *c, gfp_t flags)
81819f0f 2013{
49e22585
CL
2014 struct page *page, *page2;
2015 void *object = NULL;
e5d9998f 2016 unsigned int available = 0;
633b0764 2017 int objects;
81819f0f
CL
2018
2019 /*
2020 * Racy check. If we mistakenly see no partial slabs then we
2021 * just allocate an empty slab. If we mistakenly try to get a
70b6d25e 2022 * partial slab and there is none available then get_partial()
672bba3a 2023 * will return NULL.
81819f0f
CL
2024 */
2025 if (!n || !n->nr_partial)
2026 return NULL;
2027
2028 spin_lock(&n->list_lock);
916ac052 2029 list_for_each_entry_safe(page, page2, &n->partial, slab_list) {
8ba00bb6 2030 void *t;
49e22585 2031
8ba00bb6
JK
2032 if (!pfmemalloc_match(page, flags))
2033 continue;
2034
633b0764 2035 t = acquire_slab(s, n, page, object == NULL, &objects);
49e22585 2036 if (!t)
9b1ea29b 2037 break;
49e22585 2038
633b0764 2039 available += objects;
12d79634 2040 if (!object) {
49e22585 2041 c->page = page;
49e22585 2042 stat(s, ALLOC_FROM_PARTIAL);
49e22585 2043 object = t;
49e22585 2044 } else {
633b0764 2045 put_cpu_partial(s, page, 0);
8028dcea 2046 stat(s, CPU_PARTIAL_NODE);
49e22585 2047 }
345c905d 2048 if (!kmem_cache_has_cpu_partial(s)
e6d0e1dc 2049 || available > slub_cpu_partial(s) / 2)
49e22585
CL
2050 break;
2051
497b66f2 2052 }
81819f0f 2053 spin_unlock(&n->list_lock);
497b66f2 2054 return object;
81819f0f
CL
2055}
2056
2057/*
672bba3a 2058 * Get a page from somewhere. Search in increasing NUMA distances.
81819f0f 2059 */
de3ec035 2060static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
acd19fd1 2061 struct kmem_cache_cpu *c)
81819f0f
CL
2062{
2063#ifdef CONFIG_NUMA
2064 struct zonelist *zonelist;
dd1a239f 2065 struct zoneref *z;
54a6eb5c 2066 struct zone *zone;
97a225e6 2067 enum zone_type highest_zoneidx = gfp_zone(flags);
497b66f2 2068 void *object;
cc9a6c87 2069 unsigned int cpuset_mems_cookie;
81819f0f
CL
2070
2071 /*
672bba3a
CL
2072 * The defrag ratio allows a configuration of the tradeoffs between
2073 * inter node defragmentation and node local allocations. A lower
2074 * defrag_ratio increases the tendency to do local allocations
2075 * instead of attempting to obtain partial slabs from other nodes.
81819f0f 2076 *
672bba3a
CL
2077 * If the defrag_ratio is set to 0 then kmalloc() always
2078 * returns node local objects. If the ratio is higher then kmalloc()
2079 * may return off node objects because partial slabs are obtained
2080 * from other nodes and filled up.
81819f0f 2081 *
43efd3ea
LP
2082 * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100
2083 * (which makes defrag_ratio = 1000) then every (well almost)
2084 * allocation will first attempt to defrag slab caches on other nodes.
2085 * This means scanning over all nodes to look for partial slabs which
2086 * may be expensive if we do it every time we are trying to find a slab
672bba3a 2087 * with available objects.
81819f0f 2088 */
9824601e
CL
2089 if (!s->remote_node_defrag_ratio ||
2090 get_cycles() % 1024 > s->remote_node_defrag_ratio)
81819f0f
CL
2091 return NULL;
2092
cc9a6c87 2093 do {
d26914d1 2094 cpuset_mems_cookie = read_mems_allowed_begin();
2a389610 2095 zonelist = node_zonelist(mempolicy_slab_node(), flags);
97a225e6 2096 for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
cc9a6c87
MG
2097 struct kmem_cache_node *n;
2098
2099 n = get_node(s, zone_to_nid(zone));
2100
dee2f8aa 2101 if (n && cpuset_zone_allowed(zone, flags) &&
cc9a6c87 2102 n->nr_partial > s->min_partial) {
8ba00bb6 2103 object = get_partial_node(s, n, c, flags);
cc9a6c87
MG
2104 if (object) {
2105 /*
d26914d1
MG
2106 * Don't check read_mems_allowed_retry()
2107 * here - if mems_allowed was updated in
2108 * parallel, that was a harmless race
2109 * between allocation and the cpuset
2110 * update
cc9a6c87 2111 */
cc9a6c87
MG
2112 return object;
2113 }
c0ff7453 2114 }
81819f0f 2115 }
d26914d1 2116 } while (read_mems_allowed_retry(cpuset_mems_cookie));
6dfd1b65 2117#endif /* CONFIG_NUMA */
81819f0f
CL
2118 return NULL;
2119}
2120
2121/*
2122 * Get a partial page, lock it and return it.
2123 */
497b66f2 2124static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
acd19fd1 2125 struct kmem_cache_cpu *c)
81819f0f 2126{
497b66f2 2127 void *object;
a561ce00
JK
2128 int searchnode = node;
2129
2130 if (node == NUMA_NO_NODE)
2131 searchnode = numa_mem_id();
81819f0f 2132
8ba00bb6 2133 object = get_partial_node(s, get_node(s, searchnode), c, flags);
497b66f2
CL
2134 if (object || node != NUMA_NO_NODE)
2135 return object;
81819f0f 2136
acd19fd1 2137 return get_any_partial(s, flags, c);
81819f0f
CL
2138}
2139
923717cb 2140#ifdef CONFIG_PREEMPTION
8a5ec0ba 2141/*
0d645ed1 2142 * Calculate the next globally unique transaction for disambiguation
8a5ec0ba
CL
2143 * during cmpxchg. The transactions start with the cpu number and are then
2144 * incremented by CONFIG_NR_CPUS.
2145 */
2146#define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
2147#else
2148/*
2149 * No preemption supported therefore also no need to check for
2150 * different cpus.
2151 */
2152#define TID_STEP 1
2153#endif
2154
2155static inline unsigned long next_tid(unsigned long tid)
2156{
2157 return tid + TID_STEP;
2158}
2159
9d5f0be0 2160#ifdef SLUB_DEBUG_CMPXCHG
8a5ec0ba
CL
2161static inline unsigned int tid_to_cpu(unsigned long tid)
2162{
2163 return tid % TID_STEP;
2164}
2165
2166static inline unsigned long tid_to_event(unsigned long tid)
2167{
2168 return tid / TID_STEP;
2169}
9d5f0be0 2170#endif
8a5ec0ba
CL
2171
2172static inline unsigned int init_tid(int cpu)
2173{
2174 return cpu;
2175}
2176
2177static inline void note_cmpxchg_failure(const char *n,
2178 const struct kmem_cache *s, unsigned long tid)
2179{
2180#ifdef SLUB_DEBUG_CMPXCHG
2181 unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
2182
f9f58285 2183 pr_info("%s %s: cmpxchg redo ", n, s->name);
8a5ec0ba 2184
923717cb 2185#ifdef CONFIG_PREEMPTION
8a5ec0ba 2186 if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
f9f58285 2187 pr_warn("due to cpu change %d -> %d\n",
8a5ec0ba
CL
2188 tid_to_cpu(tid), tid_to_cpu(actual_tid));
2189 else
2190#endif
2191 if (tid_to_event(tid) != tid_to_event(actual_tid))
f9f58285 2192 pr_warn("due to cpu running other code. Event %ld->%ld\n",
8a5ec0ba
CL
2193 tid_to_event(tid), tid_to_event(actual_tid));
2194 else
f9f58285 2195 pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n",
8a5ec0ba
CL
2196 actual_tid, tid, next_tid(tid));
2197#endif
4fdccdfb 2198 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
8a5ec0ba
CL
2199}
2200
788e1aad 2201static void init_kmem_cache_cpus(struct kmem_cache *s)
8a5ec0ba 2202{
8a5ec0ba
CL
2203 int cpu;
2204
2205 for_each_possible_cpu(cpu)
2206 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
8a5ec0ba 2207}
2cfb7455 2208
81819f0f
CL
2209/*
2210 * Remove the cpu slab
2211 */
d0e0ac97 2212static void deactivate_slab(struct kmem_cache *s, struct page *page,
d4ff6d35 2213 void *freelist, struct kmem_cache_cpu *c)
81819f0f 2214{
2cfb7455 2215 enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
2cfb7455 2216 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
d930ff03 2217 int lock = 0, free_delta = 0;
2cfb7455 2218 enum slab_modes l = M_NONE, m = M_NONE;
d930ff03 2219 void *nextfree, *freelist_iter, *freelist_tail;
136333d1 2220 int tail = DEACTIVATE_TO_HEAD;
2cfb7455
CL
2221 struct page new;
2222 struct page old;
2223
2224 if (page->freelist) {
84e554e6 2225 stat(s, DEACTIVATE_REMOTE_FREES);
136333d1 2226 tail = DEACTIVATE_TO_TAIL;
2cfb7455
CL
2227 }
2228
894b8788 2229 /*
d930ff03
VB
2230 * Stage one: Count the objects on cpu's freelist as free_delta and
2231 * remember the last object in freelist_tail for later splicing.
2cfb7455 2232 */
d930ff03
VB
2233 freelist_tail = NULL;
2234 freelist_iter = freelist;
2235 while (freelist_iter) {
2236 nextfree = get_freepointer(s, freelist_iter);
2cfb7455 2237
52f23478
DZ
2238 /*
2239 * If 'nextfree' is invalid, it is possible that the object at
d930ff03
VB
2240 * 'freelist_iter' is already corrupted. So isolate all objects
2241 * starting at 'freelist_iter' by skipping them.
52f23478 2242 */
d930ff03 2243 if (freelist_corrupted(s, page, &freelist_iter, nextfree))
52f23478
DZ
2244 break;
2245
d930ff03
VB
2246 freelist_tail = freelist_iter;
2247 free_delta++;
2cfb7455 2248
d930ff03 2249 freelist_iter = nextfree;
2cfb7455
CL
2250 }
2251
894b8788 2252 /*
d930ff03
VB
2253 * Stage two: Unfreeze the page while splicing the per-cpu
2254 * freelist to the head of page's freelist.
2255 *
2256 * Ensure that the page is unfrozen while the list presence
2257 * reflects the actual number of objects during unfreeze.
2cfb7455
CL
2258 *
2259 * We setup the list membership and then perform a cmpxchg
2260 * with the count. If there is a mismatch then the page
2261 * is not unfrozen but the page is on the wrong list.
2262 *
2263 * Then we restart the process which may have to remove
2264 * the page from the list that we just put it on again
2265 * because the number of objects in the slab may have
2266 * changed.
894b8788 2267 */
2cfb7455 2268redo:
894b8788 2269
d930ff03
VB
2270 old.freelist = READ_ONCE(page->freelist);
2271 old.counters = READ_ONCE(page->counters);
a0132ac0 2272 VM_BUG_ON(!old.frozen);
7c2e132c 2273
2cfb7455
CL
2274 /* Determine target state of the slab */
2275 new.counters = old.counters;
d930ff03
VB
2276 if (freelist_tail) {
2277 new.inuse -= free_delta;
2278 set_freepointer(s, freelist_tail, old.freelist);
2cfb7455
CL
2279 new.freelist = freelist;
2280 } else
2281 new.freelist = old.freelist;
2282
2283 new.frozen = 0;
2284
8a5b20ae 2285 if (!new.inuse && n->nr_partial >= s->min_partial)
2cfb7455
CL
2286 m = M_FREE;
2287 else if (new.freelist) {
2288 m = M_PARTIAL;
2289 if (!lock) {
2290 lock = 1;
2291 /*
8bb4e7a2 2292 * Taking the spinlock removes the possibility
2cfb7455
CL
2293 * that acquire_slab() will see a slab page that
2294 * is frozen
2295 */
2296 spin_lock(&n->list_lock);
2297 }
2298 } else {
2299 m = M_FULL;
965c4848 2300 if (kmem_cache_debug_flags(s, SLAB_STORE_USER) && !lock) {
2cfb7455
CL
2301 lock = 1;
2302 /*
2303 * This also ensures that the scanning of full
2304 * slabs from diagnostic functions will not see
2305 * any frozen slabs.
2306 */
2307 spin_lock(&n->list_lock);
2308 }
2309 }
2310
2311 if (l != m) {
2cfb7455 2312 if (l == M_PARTIAL)
2cfb7455 2313 remove_partial(n, page);
2cfb7455 2314 else if (l == M_FULL)
c65c1877 2315 remove_full(s, n, page);
2cfb7455 2316
88349a28 2317 if (m == M_PARTIAL)
2cfb7455 2318 add_partial(n, page, tail);
88349a28 2319 else if (m == M_FULL)
2cfb7455 2320 add_full(s, n, page);
2cfb7455
CL
2321 }
2322
2323 l = m;
1d07171c 2324 if (!__cmpxchg_double_slab(s, page,
2cfb7455
CL
2325 old.freelist, old.counters,
2326 new.freelist, new.counters,
2327 "unfreezing slab"))
2328 goto redo;
2329
2cfb7455
CL
2330 if (lock)
2331 spin_unlock(&n->list_lock);
2332
88349a28
WY
2333 if (m == M_PARTIAL)
2334 stat(s, tail);
2335 else if (m == M_FULL)
2336 stat(s, DEACTIVATE_FULL);
2337 else if (m == M_FREE) {
2cfb7455
CL
2338 stat(s, DEACTIVATE_EMPTY);
2339 discard_slab(s, page);
2340 stat(s, FREE_SLAB);
894b8788 2341 }
d4ff6d35
WY
2342
2343 c->page = NULL;
2344 c->freelist = NULL;
81819f0f
CL
2345}
2346
d24ac77f
JK
2347/*
2348 * Unfreeze all the cpu partial slabs.
2349 *
59a09917
CL
2350 * This function must be called with interrupts disabled
2351 * for the cpu using c (or some other guarantee must be there
2352 * to guarantee no concurrent accesses).
d24ac77f 2353 */
59a09917
CL
2354static void unfreeze_partials(struct kmem_cache *s,
2355 struct kmem_cache_cpu *c)
49e22585 2356{
345c905d 2357#ifdef CONFIG_SLUB_CPU_PARTIAL
43d77867 2358 struct kmem_cache_node *n = NULL, *n2 = NULL;
9ada1934 2359 struct page *page, *discard_page = NULL;
49e22585 2360
4c7ba22e 2361 while ((page = slub_percpu_partial(c))) {
49e22585
CL
2362 struct page new;
2363 struct page old;
2364
4c7ba22e 2365 slub_set_percpu_partial(c, page);
43d77867
JK
2366
2367 n2 = get_node(s, page_to_nid(page));
2368 if (n != n2) {
2369 if (n)
2370 spin_unlock(&n->list_lock);
2371
2372 n = n2;
2373 spin_lock(&n->list_lock);
2374 }
49e22585
CL
2375
2376 do {
2377
2378 old.freelist = page->freelist;
2379 old.counters = page->counters;
a0132ac0 2380 VM_BUG_ON(!old.frozen);
49e22585
CL
2381
2382 new.counters = old.counters;
2383 new.freelist = old.freelist;
2384
2385 new.frozen = 0;
2386
d24ac77f 2387 } while (!__cmpxchg_double_slab(s, page,
49e22585
CL
2388 old.freelist, old.counters,
2389 new.freelist, new.counters,
2390 "unfreezing slab"));
2391
8a5b20ae 2392 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) {
9ada1934
SL
2393 page->next = discard_page;
2394 discard_page = page;
43d77867
JK
2395 } else {
2396 add_partial(n, page, DEACTIVATE_TO_TAIL);
2397 stat(s, FREE_ADD_PARTIAL);
49e22585
CL
2398 }
2399 }
2400
2401 if (n)
2402 spin_unlock(&n->list_lock);
9ada1934
SL
2403
2404 while (discard_page) {
2405 page = discard_page;
2406 discard_page = discard_page->next;
2407
2408 stat(s, DEACTIVATE_EMPTY);
2409 discard_slab(s, page);
2410 stat(s, FREE_SLAB);
2411 }
6dfd1b65 2412#endif /* CONFIG_SLUB_CPU_PARTIAL */
49e22585
CL
2413}
2414
2415/*
9234bae9
WY
2416 * Put a page that was just frozen (in __slab_free|get_partial_node) into a
2417 * partial page slot if available.
49e22585
CL
2418 *
2419 * If we did not find a slot then simply move all the partials to the
2420 * per node partial list.
2421 */
633b0764 2422static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
49e22585 2423{
345c905d 2424#ifdef CONFIG_SLUB_CPU_PARTIAL
49e22585
CL
2425 struct page *oldpage;
2426 int pages;
2427 int pobjects;
2428
d6e0b7fa 2429 preempt_disable();
49e22585
CL
2430 do {
2431 pages = 0;
2432 pobjects = 0;
2433 oldpage = this_cpu_read(s->cpu_slab->partial);
2434
2435 if (oldpage) {
2436 pobjects = oldpage->pobjects;
2437 pages = oldpage->pages;
bbd4e305 2438 if (drain && pobjects > slub_cpu_partial(s)) {
49e22585
CL
2439 unsigned long flags;
2440 /*
2441 * partial array is full. Move the existing
2442 * set to the per node partial list.
2443 */
2444 local_irq_save(flags);
59a09917 2445 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
49e22585 2446 local_irq_restore(flags);
e24fc410 2447 oldpage = NULL;
49e22585
CL
2448 pobjects = 0;
2449 pages = 0;
8028dcea 2450 stat(s, CPU_PARTIAL_DRAIN);
49e22585
CL
2451 }
2452 }
2453
2454 pages++;
2455 pobjects += page->objects - page->inuse;
2456
2457 page->pages = pages;
2458 page->pobjects = pobjects;
2459 page->next = oldpage;
2460
d0e0ac97
CG
2461 } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
2462 != oldpage);
bbd4e305 2463 if (unlikely(!slub_cpu_partial(s))) {
d6e0b7fa
VD
2464 unsigned long flags;
2465
2466 local_irq_save(flags);
2467 unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
2468 local_irq_restore(flags);
2469 }
2470 preempt_enable();
6dfd1b65 2471#endif /* CONFIG_SLUB_CPU_PARTIAL */
49e22585
CL
2472}
2473
dfb4f096 2474static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
81819f0f 2475{
84e554e6 2476 stat(s, CPUSLAB_FLUSH);
d4ff6d35 2477 deactivate_slab(s, c->page, c->freelist, c);
c17dda40
CL
2478
2479 c->tid = next_tid(c->tid);
81819f0f
CL
2480}
2481
2482/*
2483 * Flush cpu slab.
6446faa2 2484 *
81819f0f
CL
2485 * Called from IPI handler with interrupts disabled.
2486 */
0c710013 2487static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
81819f0f 2488{
9dfc6e68 2489 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
81819f0f 2490
1265ef2d
WY
2491 if (c->page)
2492 flush_slab(s, c);
49e22585 2493
1265ef2d 2494 unfreeze_partials(s, c);
81819f0f
CL
2495}
2496
2497static void flush_cpu_slab(void *d)
2498{
2499 struct kmem_cache *s = d;
81819f0f 2500
dfb4f096 2501 __flush_cpu_slab(s, smp_processor_id());
81819f0f
CL
2502}
2503
a8364d55
GBY
2504static bool has_cpu_slab(int cpu, void *info)
2505{
2506 struct kmem_cache *s = info;
2507 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
2508
a93cf07b 2509 return c->page || slub_percpu_partial(c);
a8364d55
GBY
2510}
2511
81819f0f
CL
2512static void flush_all(struct kmem_cache *s)
2513{
cb923159 2514 on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1);
81819f0f
CL
2515}
2516
a96a87bf
SAS
2517/*
2518 * Use the cpu notifier to insure that the cpu slabs are flushed when
2519 * necessary.
2520 */
2521static int slub_cpu_dead(unsigned int cpu)
2522{
2523 struct kmem_cache *s;
2524 unsigned long flags;
2525
2526 mutex_lock(&slab_mutex);
2527 list_for_each_entry(s, &slab_caches, list) {
2528 local_irq_save(flags);
2529 __flush_cpu_slab(s, cpu);
2530 local_irq_restore(flags);
2531 }
2532 mutex_unlock(&slab_mutex);
2533 return 0;
2534}
2535
dfb4f096
CL
2536/*
2537 * Check if the objects in a per cpu structure fit numa
2538 * locality expectations.
2539 */
57d437d2 2540static inline int node_match(struct page *page, int node)
dfb4f096
CL
2541{
2542#ifdef CONFIG_NUMA
6159d0f5 2543 if (node != NUMA_NO_NODE && page_to_nid(page) != node)
dfb4f096
CL
2544 return 0;
2545#endif
2546 return 1;
2547}
2548
9a02d699 2549#ifdef CONFIG_SLUB_DEBUG
781b2ba6
PE
2550static int count_free(struct page *page)
2551{
2552 return page->objects - page->inuse;
2553}
2554
9a02d699
DR
2555static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
2556{
2557 return atomic_long_read(&n->total_objects);
2558}
2559#endif /* CONFIG_SLUB_DEBUG */
2560
2561#if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS)
781b2ba6
PE
2562static unsigned long count_partial(struct kmem_cache_node *n,
2563 int (*get_count)(struct page *))
2564{
2565 unsigned long flags;
2566 unsigned long x = 0;
2567 struct page *page;
2568
2569 spin_lock_irqsave(&n->list_lock, flags);
916ac052 2570 list_for_each_entry(page, &n->partial, slab_list)
781b2ba6
PE
2571 x += get_count(page);
2572 spin_unlock_irqrestore(&n->list_lock, flags);
2573 return x;
2574}
9a02d699 2575#endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
26c02cf0 2576
781b2ba6
PE
2577static noinline void
2578slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
2579{
9a02d699
DR
2580#ifdef CONFIG_SLUB_DEBUG
2581 static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
2582 DEFAULT_RATELIMIT_BURST);
781b2ba6 2583 int node;
fa45dc25 2584 struct kmem_cache_node *n;
781b2ba6 2585
9a02d699
DR
2586 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
2587 return;
2588
5b3810e5
VB
2589 pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
2590 nid, gfpflags, &gfpflags);
19af27af 2591 pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n",
f9f58285
FF
2592 s->name, s->object_size, s->size, oo_order(s->oo),
2593 oo_order(s->min));
781b2ba6 2594
3b0efdfa 2595 if (oo_order(s->min) > get_order(s->object_size))
f9f58285
FF
2596 pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
2597 s->name);
fa5ec8a1 2598
fa45dc25 2599 for_each_kmem_cache_node(s, node, n) {
781b2ba6
PE
2600 unsigned long nr_slabs;
2601 unsigned long nr_objs;
2602 unsigned long nr_free;
2603
26c02cf0
AB
2604 nr_free = count_partial(n, count_free);
2605 nr_slabs = node_nr_slabs(n);
2606 nr_objs = node_nr_objs(n);
781b2ba6 2607
f9f58285 2608 pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
781b2ba6
PE
2609 node, nr_slabs, nr_objs, nr_free);
2610 }
9a02d699 2611#endif
781b2ba6
PE
2612}
2613
497b66f2
CL
2614static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
2615 int node, struct kmem_cache_cpu **pc)
2616{
6faa6833 2617 void *freelist;
188fd063
CL
2618 struct kmem_cache_cpu *c = *pc;
2619 struct page *page;
497b66f2 2620
128227e7
MW
2621 WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
2622
188fd063 2623 freelist = get_partial(s, flags, node, c);
497b66f2 2624
188fd063
CL
2625 if (freelist)
2626 return freelist;
2627
2628 page = new_slab(s, flags, node);
497b66f2 2629 if (page) {
7c8e0181 2630 c = raw_cpu_ptr(s->cpu_slab);
497b66f2
CL
2631 if (c->page)
2632 flush_slab(s, c);
2633
2634 /*
2635 * No other reference to the page yet so we can
2636 * muck around with it freely without cmpxchg
2637 */
6faa6833 2638 freelist = page->freelist;
497b66f2
CL
2639 page->freelist = NULL;
2640
2641 stat(s, ALLOC_SLAB);
497b66f2
CL
2642 c->page = page;
2643 *pc = c;
edde82b6 2644 }
497b66f2 2645
6faa6833 2646 return freelist;
497b66f2
CL
2647}
2648
072bb0aa
MG
2649static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
2650{
2651 if (unlikely(PageSlabPfmemalloc(page)))
2652 return gfp_pfmemalloc_allowed(gfpflags);
2653
2654 return true;
2655}
2656
213eeb9f 2657/*
d0e0ac97
CG
2658 * Check the page->freelist of a page and either transfer the freelist to the
2659 * per cpu freelist or deactivate the page.
213eeb9f
CL
2660 *
2661 * The page is still frozen if the return value is not NULL.
2662 *
2663 * If this function returns NULL then the page has been unfrozen.
d24ac77f
JK
2664 *
2665 * This function must be called with interrupt disabled.
213eeb9f
CL
2666 */
2667static inline void *get_freelist(struct kmem_cache *s, struct page *page)
2668{
2669 struct page new;
2670 unsigned long counters;
2671 void *freelist;
2672
2673 do {
2674 freelist = page->freelist;
2675 counters = page->counters;
6faa6833 2676
213eeb9f 2677 new.counters = counters;
a0132ac0 2678 VM_BUG_ON(!new.frozen);
213eeb9f
CL
2679
2680 new.inuse = page->objects;
2681 new.frozen = freelist != NULL;
2682
d24ac77f 2683 } while (!__cmpxchg_double_slab(s, page,
213eeb9f
CL
2684 freelist, counters,
2685 NULL, new.counters,
2686 "get_freelist"));
2687
2688 return freelist;
2689}
2690
81819f0f 2691/*
894b8788
CL
2692 * Slow path. The lockless freelist is empty or we need to perform
2693 * debugging duties.
2694 *
894b8788
CL
2695 * Processing is still very fast if new objects have been freed to the
2696 * regular freelist. In that case we simply take over the regular freelist
2697 * as the lockless freelist and zap the regular freelist.
81819f0f 2698 *
894b8788
CL
2699 * If that is not working then we fall back to the partial lists. We take the
2700 * first element of the freelist as the object to allocate now and move the
2701 * rest of the freelist to the lockless freelist.
81819f0f 2702 *
894b8788 2703 * And if we were unable to get a new slab from the partial slab lists then
6446faa2
CL
2704 * we need to allocate a new slab. This is the slowest path since it involves
2705 * a call to the page allocator and the setup of a new slab.
a380a3c7
CL
2706 *
2707 * Version of __slab_alloc to use when we know that interrupts are
2708 * already disabled (which is the case for bulk allocation).
81819f0f 2709 */
a380a3c7 2710static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
ce71e27c 2711 unsigned long addr, struct kmem_cache_cpu *c)
81819f0f 2712{
6faa6833 2713 void *freelist;
f6e7def7 2714 struct page *page;
81819f0f 2715
9f986d99
AW
2716 stat(s, ALLOC_SLOWPATH);
2717
f6e7def7 2718 page = c->page;
0715e6c5
VB
2719 if (!page) {
2720 /*
2721 * if the node is not online or has no normal memory, just
2722 * ignore the node constraint
2723 */
2724 if (unlikely(node != NUMA_NO_NODE &&
7e1fa93d 2725 !node_isset(node, slab_nodes)))
0715e6c5 2726 node = NUMA_NO_NODE;
81819f0f 2727 goto new_slab;
0715e6c5 2728 }
49e22585 2729redo:
6faa6833 2730
57d437d2 2731 if (unlikely(!node_match(page, node))) {
0715e6c5
VB
2732 /*
2733 * same as above but node_match() being false already
2734 * implies node != NUMA_NO_NODE
2735 */
7e1fa93d 2736 if (!node_isset(node, slab_nodes)) {
0715e6c5
VB
2737 node = NUMA_NO_NODE;
2738 goto redo;
2739 } else {
a561ce00 2740 stat(s, ALLOC_NODE_MISMATCH);
d4ff6d35 2741 deactivate_slab(s, page, c->freelist, c);
a561ce00
JK
2742 goto new_slab;
2743 }
fc59c053 2744 }
6446faa2 2745
072bb0aa
MG
2746 /*
2747 * By rights, we should be searching for a slab page that was
2748 * PFMEMALLOC but right now, we are losing the pfmemalloc
2749 * information when the page leaves the per-cpu allocator
2750 */
2751 if (unlikely(!pfmemalloc_match(page, gfpflags))) {
d4ff6d35 2752 deactivate_slab(s, page, c->freelist, c);
072bb0aa
MG
2753 goto new_slab;
2754 }
2755
73736e03 2756 /* must check again c->freelist in case of cpu migration or IRQ */
6faa6833
CL
2757 freelist = c->freelist;
2758 if (freelist)
73736e03 2759 goto load_freelist;
03e404af 2760
f6e7def7 2761 freelist = get_freelist(s, page);
6446faa2 2762
6faa6833 2763 if (!freelist) {
03e404af
CL
2764 c->page = NULL;
2765 stat(s, DEACTIVATE_BYPASS);
fc59c053 2766 goto new_slab;
03e404af 2767 }
6446faa2 2768
84e554e6 2769 stat(s, ALLOC_REFILL);
6446faa2 2770
894b8788 2771load_freelist:
507effea
CL
2772 /*
2773 * freelist is pointing to the list of objects to be used.
2774 * page is pointing to the page from which the objects are obtained.
2775 * That page must be frozen for per cpu allocations to work.
2776 */
a0132ac0 2777 VM_BUG_ON(!c->page->frozen);
6faa6833 2778 c->freelist = get_freepointer(s, freelist);
8a5ec0ba 2779 c->tid = next_tid(c->tid);
6faa6833 2780 return freelist;
81819f0f 2781
81819f0f 2782new_slab:
2cfb7455 2783
a93cf07b
WY
2784 if (slub_percpu_partial(c)) {
2785 page = c->page = slub_percpu_partial(c);
2786 slub_set_percpu_partial(c, page);
49e22585 2787 stat(s, CPU_PARTIAL_ALLOC);
49e22585 2788 goto redo;
81819f0f
CL
2789 }
2790
188fd063 2791 freelist = new_slab_objects(s, gfpflags, node, &c);
01ad8a7b 2792
f4697436 2793 if (unlikely(!freelist)) {
9a02d699 2794 slab_out_of_memory(s, gfpflags, node);
f4697436 2795 return NULL;
81819f0f 2796 }
2cfb7455 2797
f6e7def7 2798 page = c->page;
5091b74a 2799 if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
4b6f0750 2800 goto load_freelist;
2cfb7455 2801
497b66f2 2802 /* Only entered in the debug case */
d0e0ac97
CG
2803 if (kmem_cache_debug(s) &&
2804 !alloc_debug_processing(s, page, freelist, addr))
497b66f2 2805 goto new_slab; /* Slab failed checks. Next slab needed */
894b8788 2806
d4ff6d35 2807 deactivate_slab(s, page, get_freepointer(s, freelist), c);
6faa6833 2808 return freelist;
894b8788
CL
2809}
2810
a380a3c7
CL
2811/*
2812 * Another one that disabled interrupt and compensates for possible
2813 * cpu changes by refetching the per cpu area pointer.
2814 */
2815static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
2816 unsigned long addr, struct kmem_cache_cpu *c)
2817{
2818 void *p;
2819 unsigned long flags;
2820
2821 local_irq_save(flags);
923717cb 2822#ifdef CONFIG_PREEMPTION
a380a3c7
CL
2823 /*
2824 * We may have been preempted and rescheduled on a different
2825 * cpu before disabling interrupts. Need to reload cpu area
2826 * pointer.
2827 */
2828 c = this_cpu_ptr(s->cpu_slab);
2829#endif
2830
2831 p = ___slab_alloc(s, gfpflags, node, addr, c);
2832 local_irq_restore(flags);
2833 return p;
2834}
2835
0f181f9f
AP
2836/*
2837 * If the object has been wiped upon free, make sure it's fully initialized by
2838 * zeroing out freelist pointer.
2839 */
2840static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
2841 void *obj)
2842{
2843 if (unlikely(slab_want_init_on_free(s)) && obj)
ce5716c6
AK
2844 memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
2845 0, sizeof(void *));
0f181f9f
AP
2846}
2847
894b8788
CL
2848/*
2849 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
2850 * have the fastpath folded into their functions. So no function call
2851 * overhead for requests that can be satisfied on the fastpath.
2852 *
2853 * The fastpath works by first checking if the lockless freelist can be used.
2854 * If not then __slab_alloc is called for slow processing.
2855 *
2856 * Otherwise we can simply pick the next object from the lockless free list.
2857 */
2b847c3c 2858static __always_inline void *slab_alloc_node(struct kmem_cache *s,
b89fb5ef 2859 gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)
894b8788 2860{
03ec0ed5 2861 void *object;
dfb4f096 2862 struct kmem_cache_cpu *c;
57d437d2 2863 struct page *page;
8a5ec0ba 2864 unsigned long tid;
964d4bd3 2865 struct obj_cgroup *objcg = NULL;
da844b78 2866 bool init = false;
1f84260c 2867
964d4bd3 2868 s = slab_pre_alloc_hook(s, &objcg, 1, gfpflags);
8135be5a 2869 if (!s)
773ff60e 2870 return NULL;
b89fb5ef
AP
2871
2872 object = kfence_alloc(s, orig_size, gfpflags);
2873 if (unlikely(object))
2874 goto out;
2875
8a5ec0ba 2876redo:
8a5ec0ba
CL
2877 /*
2878 * Must read kmem_cache cpu data via this cpu ptr. Preemption is
2879 * enabled. We may switch back and forth between cpus while
2880 * reading from one cpu area. That does not matter as long
2881 * as we end up on the original cpu again when doing the cmpxchg.
7cccd80b 2882 *
9aabf810 2883 * We should guarantee that tid and kmem_cache are retrieved on
923717cb 2884 * the same cpu. It could be different if CONFIG_PREEMPTION so we need
9aabf810 2885 * to check if it is matched or not.
8a5ec0ba 2886 */
9aabf810
JK
2887 do {
2888 tid = this_cpu_read(s->cpu_slab->tid);
2889 c = raw_cpu_ptr(s->cpu_slab);
923717cb 2890 } while (IS_ENABLED(CONFIG_PREEMPTION) &&
859b7a0e 2891 unlikely(tid != READ_ONCE(c->tid)));
9aabf810
JK
2892
2893 /*
2894 * Irqless object alloc/free algorithm used here depends on sequence
2895 * of fetching cpu_slab's data. tid should be fetched before anything
2896 * on c to guarantee that object and page associated with previous tid
2897 * won't be used with current tid. If we fetch tid first, object and
2898 * page could be one associated with next tid and our alloc/free
2899 * request will be failed. In this case, we will retry. So, no problem.
2900 */
2901 barrier();
8a5ec0ba 2902
8a5ec0ba
CL
2903 /*
2904 * The transaction ids are globally unique per cpu and per operation on
2905 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
2906 * occurs on the right processor and that there was no operation on the
2907 * linked list in between.
2908 */
8a5ec0ba 2909
9dfc6e68 2910 object = c->freelist;
57d437d2 2911 page = c->page;
22e4663e 2912 if (unlikely(!object || !page || !node_match(page, node))) {
dfb4f096 2913 object = __slab_alloc(s, gfpflags, node, addr, c);
8eae1492 2914 } else {
0ad9500e
ED
2915 void *next_object = get_freepointer_safe(s, object);
2916
8a5ec0ba 2917 /*
25985edc 2918 * The cmpxchg will only match if there was no additional
8a5ec0ba
CL
2919 * operation and if we are on the right processor.
2920 *
d0e0ac97
CG
2921 * The cmpxchg does the following atomically (without lock
2922 * semantics!)
8a5ec0ba
CL
2923 * 1. Relocate first pointer to the current per cpu area.
2924 * 2. Verify that tid and freelist have not been changed
2925 * 3. If they were not changed replace tid and freelist
2926 *
d0e0ac97
CG
2927 * Since this is without lock semantics the protection is only
2928 * against code executing on this cpu *not* from access by
2929 * other cpus.
8a5ec0ba 2930 */
933393f5 2931 if (unlikely(!this_cpu_cmpxchg_double(
8a5ec0ba
CL
2932 s->cpu_slab->freelist, s->cpu_slab->tid,
2933 object, tid,
0ad9500e 2934 next_object, next_tid(tid)))) {
8a5ec0ba
CL
2935
2936 note_cmpxchg_failure("slab_alloc", s, tid);
2937 goto redo;
2938 }
0ad9500e 2939 prefetch_freepointer(s, next_object);
84e554e6 2940 stat(s, ALLOC_FASTPATH);
894b8788 2941 }
0f181f9f 2942
ce5716c6 2943 maybe_wipe_obj_freeptr(s, object);
da844b78 2944 init = slab_want_init_on_alloc(gfpflags, s);
d07dbea4 2945
b89fb5ef 2946out:
da844b78 2947 slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init);
5a896d9e 2948
894b8788 2949 return object;
81819f0f
CL
2950}
2951
2b847c3c 2952static __always_inline void *slab_alloc(struct kmem_cache *s,
b89fb5ef 2953 gfp_t gfpflags, unsigned long addr, size_t orig_size)
2b847c3c 2954{
b89fb5ef 2955 return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr, orig_size);
2b847c3c
EG
2956}
2957
81819f0f
CL
2958void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
2959{
b89fb5ef 2960 void *ret = slab_alloc(s, gfpflags, _RET_IP_, s->object_size);
5b882be4 2961
d0e0ac97
CG
2962 trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size,
2963 s->size, gfpflags);
5b882be4
EGM
2964
2965 return ret;
81819f0f
CL
2966}
2967EXPORT_SYMBOL(kmem_cache_alloc);
2968
0f24f128 2969#ifdef CONFIG_TRACING
4a92379b
RK
2970void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
2971{
b89fb5ef 2972 void *ret = slab_alloc(s, gfpflags, _RET_IP_, size);
4a92379b 2973 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
0116523c 2974 ret = kasan_kmalloc(s, ret, size, gfpflags);
4a92379b
RK
2975 return ret;
2976}
2977EXPORT_SYMBOL(kmem_cache_alloc_trace);
5b882be4
EGM
2978#endif
2979
81819f0f
CL
2980#ifdef CONFIG_NUMA
2981void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
2982{
b89fb5ef 2983 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_, s->object_size);
5b882be4 2984
ca2b84cb 2985 trace_kmem_cache_alloc_node(_RET_IP_, ret,
3b0efdfa 2986 s->object_size, s->size, gfpflags, node);
5b882be4
EGM
2987
2988 return ret;
81819f0f
CL
2989}
2990EXPORT_SYMBOL(kmem_cache_alloc_node);
81819f0f 2991
0f24f128 2992#ifdef CONFIG_TRACING
4a92379b 2993void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
5b882be4 2994 gfp_t gfpflags,
4a92379b 2995 int node, size_t size)
5b882be4 2996{
b89fb5ef 2997 void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_, size);
4a92379b
RK
2998
2999 trace_kmalloc_node(_RET_IP_, ret,
3000 size, s->size, gfpflags, node);
0316bec2 3001
0116523c 3002 ret = kasan_kmalloc(s, ret, size, gfpflags);
4a92379b 3003 return ret;
5b882be4 3004}
4a92379b 3005EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
5b882be4 3006#endif
6dfd1b65 3007#endif /* CONFIG_NUMA */
5b882be4 3008
81819f0f 3009/*
94e4d712 3010 * Slow path handling. This may still be called frequently since objects
894b8788 3011 * have a longer lifetime than the cpu slabs in most processing loads.
81819f0f 3012 *
894b8788
CL
3013 * So we still attempt to reduce cache line usage. Just take the slab
3014 * lock and free the item. If there is no additional partial page
3015 * handling required then we can return immediately.
81819f0f 3016 */
894b8788 3017static void __slab_free(struct kmem_cache *s, struct page *page,
81084651
JDB
3018 void *head, void *tail, int cnt,
3019 unsigned long addr)
3020
81819f0f
CL
3021{
3022 void *prior;
2cfb7455 3023 int was_frozen;
2cfb7455
CL
3024 struct page new;
3025 unsigned long counters;
3026 struct kmem_cache_node *n = NULL;
3f649ab7 3027 unsigned long flags;
81819f0f 3028
8a5ec0ba 3029 stat(s, FREE_SLOWPATH);
81819f0f 3030
b89fb5ef
AP
3031 if (kfence_free(head))
3032 return;
3033
19c7ff9e 3034 if (kmem_cache_debug(s) &&
282acb43 3035 !free_debug_processing(s, page, head, tail, cnt, addr))
80f08c19 3036 return;
6446faa2 3037
2cfb7455 3038 do {
837d678d
JK
3039 if (unlikely(n)) {
3040 spin_unlock_irqrestore(&n->list_lock, flags);
3041 n = NULL;
3042 }
2cfb7455
CL
3043 prior = page->freelist;
3044 counters = page->counters;
81084651 3045 set_freepointer(s, tail, prior);
2cfb7455
CL
3046 new.counters = counters;
3047 was_frozen = new.frozen;
81084651 3048 new.inuse -= cnt;
837d678d 3049 if ((!new.inuse || !prior) && !was_frozen) {
49e22585 3050
c65c1877 3051 if (kmem_cache_has_cpu_partial(s) && !prior) {
49e22585
CL
3052
3053 /*
d0e0ac97
CG
3054 * Slab was on no list before and will be
3055 * partially empty
3056 * We can defer the list move and instead
3057 * freeze it.
49e22585
CL
3058 */
3059 new.frozen = 1;
3060
c65c1877 3061 } else { /* Needs to be taken off a list */
49e22585 3062
b455def2 3063 n = get_node(s, page_to_nid(page));
49e22585
CL
3064 /*
3065 * Speculatively acquire the list_lock.
3066 * If the cmpxchg does not succeed then we may
3067 * drop the list_lock without any processing.
3068 *
3069 * Otherwise the list_lock will synchronize with
3070 * other processors updating the list of slabs.
3071 */
3072 spin_lock_irqsave(&n->list_lock, flags);
3073
3074 }
2cfb7455 3075 }
81819f0f 3076
2cfb7455
CL
3077 } while (!cmpxchg_double_slab(s, page,
3078 prior, counters,
81084651 3079 head, new.counters,
2cfb7455 3080 "__slab_free"));
81819f0f 3081
2cfb7455 3082 if (likely(!n)) {
49e22585 3083
c270cf30
AW
3084 if (likely(was_frozen)) {
3085 /*
3086 * The list lock was not taken therefore no list
3087 * activity can be necessary.
3088 */
3089 stat(s, FREE_FROZEN);
3090 } else if (new.frozen) {
3091 /*
3092 * If we just froze the page then put it onto the
3093 * per cpu partial list.
3094 */
49e22585 3095 put_cpu_partial(s, page, 1);
8028dcea
AS
3096 stat(s, CPU_PARTIAL_FREE);
3097 }
c270cf30 3098
b455def2
L
3099 return;
3100 }
81819f0f 3101
8a5b20ae 3102 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
837d678d
JK
3103 goto slab_empty;
3104
81819f0f 3105 /*
837d678d
JK
3106 * Objects left in the slab. If it was not on the partial list before
3107 * then add it.
81819f0f 3108 */
345c905d 3109 if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) {
a4d3f891 3110 remove_full(s, n, page);
837d678d
JK
3111 add_partial(n, page, DEACTIVATE_TO_TAIL);
3112 stat(s, FREE_ADD_PARTIAL);
8ff12cfc 3113 }
80f08c19 3114 spin_unlock_irqrestore(&n->list_lock, flags);
81819f0f
CL
3115 return;
3116
3117slab_empty:
a973e9dd 3118 if (prior) {
81819f0f 3119 /*
6fbabb20 3120 * Slab on the partial list.
81819f0f 3121 */
5cc6eee8 3122 remove_partial(n, page);
84e554e6 3123 stat(s, FREE_REMOVE_PARTIAL);
c65c1877 3124 } else {
6fbabb20 3125 /* Slab must be on the full list */
c65c1877
PZ
3126 remove_full(s, n, page);
3127 }
2cfb7455 3128
80f08c19 3129 spin_unlock_irqrestore(&n->list_lock, flags);
84e554e6 3130 stat(s, FREE_SLAB);
81819f0f 3131 discard_slab(s, page);
81819f0f
CL
3132}
3133
894b8788
CL
3134/*
3135 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
3136 * can perform fastpath freeing without additional function calls.
3137 *
3138 * The fastpath is only possible if we are freeing to the current cpu slab
3139 * of this processor. This typically the case if we have just allocated
3140 * the item before.
3141 *
3142 * If fastpath is not possible then fall back to __slab_free where we deal
3143 * with all sorts of special processing.
81084651
JDB
3144 *
3145 * Bulk free of a freelist with several objects (all pointing to the
3146 * same page) possible by specifying head and tail ptr, plus objects
3147 * count (cnt). Bulk free indicated by tail pointer being set.
894b8788 3148 */
80a9201a
AP
3149static __always_inline void do_slab_free(struct kmem_cache *s,
3150 struct page *page, void *head, void *tail,
3151 int cnt, unsigned long addr)
894b8788 3152{
81084651 3153 void *tail_obj = tail ? : head;
dfb4f096 3154 struct kmem_cache_cpu *c;
8a5ec0ba 3155 unsigned long tid;
964d4bd3 3156
d1b2cf6c 3157 memcg_slab_free_hook(s, &head, 1);
8a5ec0ba
CL
3158redo:
3159 /*
3160 * Determine the currently cpus per cpu slab.
3161 * The cpu may change afterward. However that does not matter since
3162 * data is retrieved via this pointer. If we are on the same cpu
2ae44005 3163 * during the cmpxchg then the free will succeed.
8a5ec0ba 3164 */
9aabf810
JK
3165 do {
3166 tid = this_cpu_read(s->cpu_slab->tid);
3167 c = raw_cpu_ptr(s->cpu_slab);
923717cb 3168 } while (IS_ENABLED(CONFIG_PREEMPTION) &&
859b7a0e 3169 unlikely(tid != READ_ONCE(c->tid)));
c016b0bd 3170
9aabf810
JK
3171 /* Same with comment on barrier() in slab_alloc_node() */
3172 barrier();
c016b0bd 3173
442b06bc 3174 if (likely(page == c->page)) {
5076190d
LT
3175 void **freelist = READ_ONCE(c->freelist);
3176
3177 set_freepointer(s, tail_obj, freelist);
8a5ec0ba 3178
933393f5 3179 if (unlikely(!this_cpu_cmpxchg_double(
8a5ec0ba 3180 s->cpu_slab->freelist, s->cpu_slab->tid,
5076190d 3181 freelist, tid,
81084651 3182 head, next_tid(tid)))) {
8a5ec0ba
CL
3183
3184 note_cmpxchg_failure("slab_free", s, tid);
3185 goto redo;
3186 }
84e554e6 3187 stat(s, FREE_FASTPATH);
894b8788 3188 } else
81084651 3189 __slab_free(s, page, head, tail_obj, cnt, addr);
894b8788 3190
894b8788
CL
3191}
3192
80a9201a
AP
3193static __always_inline void slab_free(struct kmem_cache *s, struct page *page,
3194 void *head, void *tail, int cnt,
3195 unsigned long addr)
3196{
80a9201a 3197 /*
c3895391
AK
3198 * With KASAN enabled slab_free_freelist_hook modifies the freelist
3199 * to remove objects, whose reuse must be delayed.
80a9201a 3200 */
c3895391
AK
3201 if (slab_free_freelist_hook(s, &head, &tail))
3202 do_slab_free(s, page, head, tail, cnt, addr);
80a9201a
AP
3203}
3204
2bd926b4 3205#ifdef CONFIG_KASAN_GENERIC
80a9201a
AP
3206void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr)
3207{
3208 do_slab_free(cache, virt_to_head_page(x), x, NULL, 1, addr);
3209}
3210#endif
3211
81819f0f
CL
3212void kmem_cache_free(struct kmem_cache *s, void *x)
3213{
b9ce5ef4
GC
3214 s = cache_from_obj(s, x);
3215 if (!s)
79576102 3216 return;
81084651 3217 slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
3544de8e 3218 trace_kmem_cache_free(_RET_IP_, x, s->name);
81819f0f
CL
3219}
3220EXPORT_SYMBOL(kmem_cache_free);
3221
d0ecd894 3222struct detached_freelist {
fbd02630 3223 struct page *page;
d0ecd894
JDB
3224 void *tail;
3225 void *freelist;
3226 int cnt;
376bf125 3227 struct kmem_cache *s;
d0ecd894 3228};
fbd02630 3229
d0ecd894
JDB
3230/*
3231 * This function progressively scans the array with free objects (with
3232 * a limited look ahead) and extract objects belonging to the same
3233 * page. It builds a detached freelist directly within the given
3234 * page/objects. This can happen without any need for
3235 * synchronization, because the objects are owned by running process.
3236 * The freelist is build up as a single linked list in the objects.
3237 * The idea is, that this detached freelist can then be bulk
3238 * transferred to the real freelist(s), but only requiring a single
3239 * synchronization primitive. Look ahead in the array is limited due
3240 * to performance reasons.
3241 */
376bf125
JDB
3242static inline
3243int build_detached_freelist(struct kmem_cache *s, size_t size,
3244 void **p, struct detached_freelist *df)
d0ecd894
JDB
3245{
3246 size_t first_skipped_index = 0;
3247 int lookahead = 3;
3248 void *object;
ca257195 3249 struct page *page;
fbd02630 3250
d0ecd894
JDB
3251 /* Always re-init detached_freelist */
3252 df->page = NULL;
fbd02630 3253
d0ecd894
JDB
3254 do {
3255 object = p[--size];
ca257195 3256 /* Do we need !ZERO_OR_NULL_PTR(object) here? (for kfree) */
d0ecd894 3257 } while (!object && size);
3eed034d 3258
d0ecd894
JDB
3259 if (!object)
3260 return 0;
fbd02630 3261
ca257195
JDB
3262 page = virt_to_head_page(object);
3263 if (!s) {
3264 /* Handle kalloc'ed objects */
3265 if (unlikely(!PageSlab(page))) {
3266 BUG_ON(!PageCompound(page));
3267 kfree_hook(object);
4949148a 3268 __free_pages(page, compound_order(page));
ca257195
JDB
3269 p[size] = NULL; /* mark object processed */
3270 return size;
3271 }
3272 /* Derive kmem_cache from object */
3273 df->s = page->slab_cache;
3274 } else {
3275 df->s = cache_from_obj(s, object); /* Support for memcg */
3276 }
376bf125 3277
b89fb5ef 3278 if (is_kfence_address(object)) {
d57a964e 3279 slab_free_hook(df->s, object, false);
b89fb5ef
AP
3280 __kfence_free(object);
3281 p[size] = NULL; /* mark object processed */
3282 return size;
3283 }
3284
d0ecd894 3285 /* Start new detached freelist */
ca257195 3286 df->page = page;
376bf125 3287 set_freepointer(df->s, object, NULL);
d0ecd894
JDB
3288 df->tail = object;
3289 df->freelist = object;
3290 p[size] = NULL; /* mark object processed */
3291 df->cnt = 1;
3292
3293 while (size) {
3294 object = p[--size];
3295 if (!object)
3296 continue; /* Skip processed objects */
3297
3298 /* df->page is always set at this point */
3299 if (df->page == virt_to_head_page(object)) {
3300 /* Opportunity build freelist */
376bf125 3301 set_freepointer(df->s, object, df->freelist);
d0ecd894
JDB
3302 df->freelist = object;
3303 df->cnt++;
3304 p[size] = NULL; /* mark object processed */
3305
3306 continue;
fbd02630 3307 }
d0ecd894
JDB
3308
3309 /* Limit look ahead search */
3310 if (!--lookahead)
3311 break;
3312
3313 if (!first_skipped_index)
3314 first_skipped_index = size + 1;
fbd02630 3315 }
d0ecd894
JDB
3316
3317 return first_skipped_index;
3318}
3319
d0ecd894 3320/* Note that interrupts must be enabled when calling this function. */
376bf125 3321void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
d0ecd894
JDB
3322{
3323 if (WARN_ON(!size))
3324 return;
3325
d1b2cf6c 3326 memcg_slab_free_hook(s, p, size);
d0ecd894
JDB
3327 do {
3328 struct detached_freelist df;
3329
3330 size = build_detached_freelist(s, size, p, &df);
84582c8a 3331 if (!df.page)
d0ecd894
JDB
3332 continue;
3333
457c82c3 3334 slab_free(df.s, df.page, df.freelist, df.tail, df.cnt, _RET_IP_);
d0ecd894 3335 } while (likely(size));
484748f0
CL
3336}
3337EXPORT_SYMBOL(kmem_cache_free_bulk);
3338
994eb764 3339/* Note that interrupts must be enabled when calling this function. */
865762a8
JDB
3340int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
3341 void **p)
484748f0 3342{
994eb764
JDB
3343 struct kmem_cache_cpu *c;
3344 int i;
964d4bd3 3345 struct obj_cgroup *objcg = NULL;
994eb764 3346
03ec0ed5 3347 /* memcg and kmem_cache debug support */
964d4bd3 3348 s = slab_pre_alloc_hook(s, &objcg, size, flags);
03ec0ed5
JDB
3349 if (unlikely(!s))
3350 return false;
994eb764
JDB
3351 /*
3352 * Drain objects in the per cpu slab, while disabling local
3353 * IRQs, which protects against PREEMPT and interrupts
3354 * handlers invoking normal fastpath.
3355 */
3356 local_irq_disable();
3357 c = this_cpu_ptr(s->cpu_slab);
3358
3359 for (i = 0; i < size; i++) {
b89fb5ef 3360 void *object = kfence_alloc(s, s->object_size, flags);
994eb764 3361
b89fb5ef
AP
3362 if (unlikely(object)) {
3363 p[i] = object;
3364 continue;
3365 }
3366
3367 object = c->freelist;
ebe909e0 3368 if (unlikely(!object)) {
fd4d9c7d
JH
3369 /*
3370 * We may have removed an object from c->freelist using
3371 * the fastpath in the previous iteration; in that case,
3372 * c->tid has not been bumped yet.
3373 * Since ___slab_alloc() may reenable interrupts while
3374 * allocating memory, we should bump c->tid now.
3375 */
3376 c->tid = next_tid(c->tid);
3377
ebe909e0
JDB
3378 /*
3379 * Invoking slow path likely have side-effect
3380 * of re-populating per CPU c->freelist
3381 */
87098373 3382 p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
ebe909e0 3383 _RET_IP_, c);
87098373
CL
3384 if (unlikely(!p[i]))
3385 goto error;
3386
ebe909e0 3387 c = this_cpu_ptr(s->cpu_slab);
0f181f9f
AP
3388 maybe_wipe_obj_freeptr(s, p[i]);
3389
ebe909e0
JDB
3390 continue; /* goto for-loop */
3391 }
994eb764
JDB
3392 c->freelist = get_freepointer(s, object);
3393 p[i] = object;
0f181f9f 3394 maybe_wipe_obj_freeptr(s, p[i]);
994eb764
JDB
3395 }
3396 c->tid = next_tid(c->tid);
3397 local_irq_enable();
3398
da844b78
AK
3399 /*
3400 * memcg and kmem_cache debug support and memory initialization.
3401 * Done outside of the IRQ disabled fastpath loop.
3402 */
3403 slab_post_alloc_hook(s, objcg, flags, size, p,
3404 slab_want_init_on_alloc(flags, s));
865762a8 3405 return i;
87098373 3406error:
87098373 3407 local_irq_enable();
da844b78 3408 slab_post_alloc_hook(s, objcg, flags, i, p, false);
03ec0ed5 3409 __kmem_cache_free_bulk(s, i, p);
865762a8 3410 return 0;
484748f0
CL
3411}
3412EXPORT_SYMBOL(kmem_cache_alloc_bulk);
3413
3414
81819f0f 3415/*
672bba3a
CL
3416 * Object placement in a slab is made very easy because we always start at
3417 * offset 0. If we tune the size of the object to the alignment then we can
3418 * get the required alignment by putting one properly sized object after
3419 * another.
81819f0f
CL
3420 *
3421 * Notice that the allocation order determines the sizes of the per cpu
3422 * caches. Each processor has always one slab available for allocations.
3423 * Increasing the allocation order reduces the number of times that slabs
672bba3a 3424 * must be moved on and off the partial lists and is therefore a factor in
81819f0f 3425 * locking overhead.
81819f0f
CL
3426 */
3427
3428/*
f0953a1b 3429 * Minimum / Maximum order of slab pages. This influences locking overhead
81819f0f
CL
3430 * and slab fragmentation. A higher order reduces the number of partial slabs
3431 * and increases the number of allocations possible without having to
3432 * take the list_lock.
3433 */
19af27af
AD
3434static unsigned int slub_min_order;
3435static unsigned int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
3436static unsigned int slub_min_objects;
81819f0f 3437
81819f0f
CL
3438/*
3439 * Calculate the order of allocation given an slab object size.
3440 *
672bba3a
CL
3441 * The order of allocation has significant impact on performance and other
3442 * system components. Generally order 0 allocations should be preferred since
3443 * order 0 does not cause fragmentation in the page allocator. Larger objects
3444 * be problematic to put into order 0 slabs because there may be too much
c124f5b5 3445 * unused space left. We go to a higher order if more than 1/16th of the slab
672bba3a
CL
3446 * would be wasted.
3447 *
3448 * In order to reach satisfactory performance we must ensure that a minimum
3449 * number of objects is in one slab. Otherwise we may generate too much
3450 * activity on the partial lists which requires taking the list_lock. This is
3451 * less a concern for large slabs though which are rarely used.
81819f0f 3452 *
672bba3a
CL
3453 * slub_max_order specifies the order where we begin to stop considering the
3454 * number of objects in a slab as critical. If we reach slub_max_order then
3455 * we try to keep the page order as low as possible. So we accept more waste
3456 * of space in favor of a small page order.
81819f0f 3457 *
672bba3a
CL
3458 * Higher order allocations also allow the placement of more objects in a
3459 * slab and thereby reduce object handling overhead. If the user has
dc84207d 3460 * requested a higher minimum order then we start with that one instead of
672bba3a 3461 * the smallest order which will fit the object.
81819f0f 3462 */
19af27af
AD
3463static inline unsigned int slab_order(unsigned int size,
3464 unsigned int min_objects, unsigned int max_order,
9736d2a9 3465 unsigned int fract_leftover)
81819f0f 3466{
19af27af
AD
3467 unsigned int min_order = slub_min_order;
3468 unsigned int order;
81819f0f 3469
9736d2a9 3470 if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
210b5c06 3471 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
39b26464 3472
9736d2a9 3473 for (order = max(min_order, (unsigned int)get_order(min_objects * size));
5e6d444e 3474 order <= max_order; order++) {
81819f0f 3475
19af27af
AD
3476 unsigned int slab_size = (unsigned int)PAGE_SIZE << order;
3477 unsigned int rem;
81819f0f 3478
9736d2a9 3479 rem = slab_size % size;
81819f0f 3480
5e6d444e 3481 if (rem <= slab_size / fract_leftover)
81819f0f 3482 break;
81819f0f 3483 }
672bba3a 3484
81819f0f
CL
3485 return order;
3486}
3487
9736d2a9 3488static inline int calculate_order(unsigned int size)
5e6d444e 3489{
19af27af
AD
3490 unsigned int order;
3491 unsigned int min_objects;
3492 unsigned int max_objects;
3286222f 3493 unsigned int nr_cpus;
5e6d444e
CL
3494
3495 /*
3496 * Attempt to find best configuration for a slab. This
3497 * works by first attempting to generate a layout with
3498 * the best configuration and backing off gradually.
3499 *
422ff4d7 3500 * First we increase the acceptable waste in a slab. Then
5e6d444e
CL
3501 * we reduce the minimum objects required in a slab.
3502 */
3503 min_objects = slub_min_objects;
3286222f
VB
3504 if (!min_objects) {
3505 /*
3506 * Some architectures will only update present cpus when
3507 * onlining them, so don't trust the number if it's just 1. But
3508 * we also don't want to use nr_cpu_ids always, as on some other
3509 * architectures, there can be many possible cpus, but never
3510 * onlined. Here we compromise between trying to avoid too high
3511 * order on systems that appear larger than they are, and too
3512 * low order on systems that appear smaller than they are.
3513 */
3514 nr_cpus = num_present_cpus();
3515 if (nr_cpus <= 1)
3516 nr_cpus = nr_cpu_ids;
3517 min_objects = 4 * (fls(nr_cpus) + 1);
3518 }
9736d2a9 3519 max_objects = order_objects(slub_max_order, size);
e8120ff1
ZY
3520 min_objects = min(min_objects, max_objects);
3521
5e6d444e 3522 while (min_objects > 1) {
19af27af
AD
3523 unsigned int fraction;
3524
c124f5b5 3525 fraction = 16;
5e6d444e
CL
3526 while (fraction >= 4) {
3527 order = slab_order(size, min_objects,
9736d2a9 3528 slub_max_order, fraction);
5e6d444e
CL
3529 if (order <= slub_max_order)
3530 return order;
3531 fraction /= 2;
3532 }
5086c389 3533 min_objects--;
5e6d444e
CL
3534 }
3535
3536 /*
3537 * We were unable to place multiple objects in a slab. Now
3538 * lets see if we can place a single object there.
3539 */
9736d2a9 3540 order = slab_order(size, 1, slub_max_order, 1);
5e6d444e
CL
3541 if (order <= slub_max_order)
3542 return order;
3543
3544 /*
3545 * Doh this slab cannot be placed using slub_max_order.
3546 */
9736d2a9 3547 order = slab_order(size, 1, MAX_ORDER, 1);
818cf590 3548 if (order < MAX_ORDER)
5e6d444e
CL
3549 return order;
3550 return -ENOSYS;
3551}
3552
5595cffc 3553static void
4053497d 3554init_kmem_cache_node(struct kmem_cache_node *n)
81819f0f
CL
3555{
3556 n->nr_partial = 0;
81819f0f
CL
3557 spin_lock_init(&n->list_lock);
3558 INIT_LIST_HEAD(&n->partial);
8ab1372f 3559#ifdef CONFIG_SLUB_DEBUG
0f389ec6 3560 atomic_long_set(&n->nr_slabs, 0);
02b71b70 3561 atomic_long_set(&n->total_objects, 0);
643b1138 3562 INIT_LIST_HEAD(&n->full);
8ab1372f 3563#endif
81819f0f
CL
3564}
3565
55136592 3566static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
4c93c355 3567{
6c182dc0 3568 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
95a05b42 3569 KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
4c93c355 3570
8a5ec0ba 3571 /*
d4d84fef
CM
3572 * Must align to double word boundary for the double cmpxchg
3573 * instructions to work; see __pcpu_double_call_return_bool().
8a5ec0ba 3574 */
d4d84fef
CM
3575 s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
3576 2 * sizeof(void *));
8a5ec0ba
CL
3577
3578 if (!s->cpu_slab)
3579 return 0;
3580
3581 init_kmem_cache_cpus(s);
4c93c355 3582
8a5ec0ba 3583 return 1;
4c93c355 3584}
4c93c355 3585
51df1142
CL
3586static struct kmem_cache *kmem_cache_node;
3587
81819f0f
CL
3588/*
3589 * No kmalloc_node yet so do it by hand. We know that this is the first
3590 * slab on the node for this slabcache. There are no concurrent accesses
3591 * possible.
3592 *
721ae22a
ZYW
3593 * Note that this function only works on the kmem_cache_node
3594 * when allocating for the kmem_cache_node. This is used for bootstrapping
4c93c355 3595 * memory on a fresh node that has no slab structures yet.
81819f0f 3596 */
55136592 3597static void early_kmem_cache_node_alloc(int node)
81819f0f
CL
3598{
3599 struct page *page;
3600 struct kmem_cache_node *n;
3601
51df1142 3602 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
81819f0f 3603
51df1142 3604 page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
81819f0f
CL
3605
3606 BUG_ON(!page);
a2f92ee7 3607 if (page_to_nid(page) != node) {
f9f58285
FF
3608 pr_err("SLUB: Unable to allocate memory from node %d\n", node);
3609 pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
a2f92ee7
CL
3610 }
3611
81819f0f
CL
3612 n = page->freelist;
3613 BUG_ON(!n);
8ab1372f 3614#ifdef CONFIG_SLUB_DEBUG
f7cb1933 3615 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
51df1142 3616 init_tracking(kmem_cache_node, n);
8ab1372f 3617#endif
da844b78 3618 n = kasan_slab_alloc(kmem_cache_node, n, GFP_KERNEL, false);
12b22386
AK
3619 page->freelist = get_freepointer(kmem_cache_node, n);
3620 page->inuse = 1;
3621 page->frozen = 0;
3622 kmem_cache_node->node[node] = n;
4053497d 3623 init_kmem_cache_node(n);
51df1142 3624 inc_slabs_node(kmem_cache_node, node, page->objects);
6446faa2 3625
67b6c900 3626 /*
1e4dd946
SR
3627 * No locks need to be taken here as it has just been
3628 * initialized and there is no concurrent access.
67b6c900 3629 */
1e4dd946 3630 __add_partial(n, page, DEACTIVATE_TO_HEAD);
81819f0f
CL
3631}
3632
3633static void free_kmem_cache_nodes(struct kmem_cache *s)
3634{
3635 int node;
fa45dc25 3636 struct kmem_cache_node *n;
81819f0f 3637
fa45dc25 3638 for_each_kmem_cache_node(s, node, n) {
81819f0f 3639 s->node[node] = NULL;
ea37df54 3640 kmem_cache_free(kmem_cache_node, n);
81819f0f
CL
3641 }
3642}
3643
52b4b950
DS
3644void __kmem_cache_release(struct kmem_cache *s)
3645{
210e7a43 3646 cache_random_seq_destroy(s);
52b4b950
DS
3647 free_percpu(s->cpu_slab);
3648 free_kmem_cache_nodes(s);
3649}
3650
55136592 3651static int init_kmem_cache_nodes(struct kmem_cache *s)
81819f0f
CL
3652{
3653 int node;
81819f0f 3654
7e1fa93d 3655 for_each_node_mask(node, slab_nodes) {
81819f0f
CL
3656 struct kmem_cache_node *n;
3657
73367bd8 3658 if (slab_state == DOWN) {
55136592 3659 early_kmem_cache_node_alloc(node);
73367bd8
AD
3660 continue;
3661 }
51df1142 3662 n = kmem_cache_alloc_node(kmem_cache_node,
55136592 3663 GFP_KERNEL, node);
81819f0f 3664
73367bd8
AD
3665 if (!n) {
3666 free_kmem_cache_nodes(s);
3667 return 0;
81819f0f 3668 }
73367bd8 3669
4053497d 3670 init_kmem_cache_node(n);
ea37df54 3671 s->node[node] = n;
81819f0f
CL
3672 }
3673 return 1;
3674}
81819f0f 3675
c0bdb232 3676static void set_min_partial(struct kmem_cache *s, unsigned long min)
3b89d7d8
DR
3677{
3678 if (min < MIN_PARTIAL)
3679 min = MIN_PARTIAL;
3680 else if (min > MAX_PARTIAL)
3681 min = MAX_PARTIAL;
3682 s->min_partial = min;
3683}
3684
e6d0e1dc
WY
3685static void set_cpu_partial(struct kmem_cache *s)
3686{
3687#ifdef CONFIG_SLUB_CPU_PARTIAL
3688 /*
3689 * cpu_partial determined the maximum number of objects kept in the
3690 * per cpu partial lists of a processor.
3691 *
3692 * Per cpu partial lists mainly contain slabs that just have one
3693 * object freed. If they are used for allocation then they can be
3694 * filled up again with minimal effort. The slab will never hit the
3695 * per node partial lists and therefore no locking will be required.
3696 *
3697 * This setting also determines
3698 *
3699 * A) The number of objects from per cpu partial slabs dumped to the
3700 * per node list when we reach the limit.
3701 * B) The number of objects in cpu partial slabs to extract from the
3702 * per node list when we run out of per cpu objects. We only fetch
3703 * 50% to keep some capacity around for frees.
3704 */
3705 if (!kmem_cache_has_cpu_partial(s))
bbd4e305 3706 slub_set_cpu_partial(s, 0);
e6d0e1dc 3707 else if (s->size >= PAGE_SIZE)
bbd4e305 3708 slub_set_cpu_partial(s, 2);
e6d0e1dc 3709 else if (s->size >= 1024)
bbd4e305 3710 slub_set_cpu_partial(s, 6);
e6d0e1dc 3711 else if (s->size >= 256)
bbd4e305 3712 slub_set_cpu_partial(s, 13);
e6d0e1dc 3713 else
bbd4e305 3714 slub_set_cpu_partial(s, 30);
e6d0e1dc
WY
3715#endif
3716}
3717
81819f0f
CL
3718/*
3719 * calculate_sizes() determines the order and the distribution of data within
3720 * a slab object.
3721 */
06b285dc 3722static int calculate_sizes(struct kmem_cache *s, int forced_order)
81819f0f 3723{
d50112ed 3724 slab_flags_t flags = s->flags;
be4a7988 3725 unsigned int size = s->object_size;
19af27af 3726 unsigned int order;
81819f0f 3727
d8b42bf5
CL
3728 /*
3729 * Round up object size to the next word boundary. We can only
3730 * place the free pointer at word boundaries and this determines
3731 * the possible location of the free pointer.
3732 */
3733 size = ALIGN(size, sizeof(void *));
3734
3735#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
3736 /*
3737 * Determine if we can poison the object itself. If the user of
3738 * the slab may touch the object after free or before allocation
3739 * then we should never poison the object itself.
3740 */
5f0d5a3a 3741 if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) &&
c59def9f 3742 !s->ctor)
81819f0f
CL
3743 s->flags |= __OBJECT_POISON;
3744 else
3745 s->flags &= ~__OBJECT_POISON;
3746
81819f0f
CL
3747
3748 /*
672bba3a 3749 * If we are Redzoning then check if there is some space between the
81819f0f 3750 * end of the object and the free pointer. If not then add an
672bba3a 3751 * additional word to have some bytes to store Redzone information.
81819f0f 3752 */
3b0efdfa 3753 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
81819f0f 3754 size += sizeof(void *);
41ecc55b 3755#endif
81819f0f
CL
3756
3757 /*
672bba3a 3758 * With that we have determined the number of bytes in actual use
e41a49fa 3759 * by the object and redzoning.
81819f0f
CL
3760 */
3761 s->inuse = size;
3762
74c1d3e0
KC
3763 if ((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
3764 ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||
3765 s->ctor) {
81819f0f
CL
3766 /*
3767 * Relocate free pointer after the object if it is not
3768 * permitted to overwrite the first word of the object on
3769 * kmem_cache_free.
3770 *
3771 * This is the case if we do RCU, have a constructor or
74c1d3e0
KC
3772 * destructor, are poisoning the objects, or are
3773 * redzoning an object smaller than sizeof(void *).
cbfc35a4
WL
3774 *
3775 * The assumption that s->offset >= s->inuse means free
3776 * pointer is outside of the object is used in the
3777 * freeptr_outside_object() function. If that is no
3778 * longer true, the function needs to be modified.
81819f0f
CL
3779 */
3780 s->offset = size;
3781 size += sizeof(void *);
e41a49fa 3782 } else {
3202fa62
KC
3783 /*
3784 * Store freelist pointer near middle of object to keep
3785 * it away from the edges of the object to avoid small
3786 * sized over/underflows from neighboring allocations.
3787 */
e41a49fa 3788 s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *));
81819f0f
CL
3789 }
3790
c12b3c62 3791#ifdef CONFIG_SLUB_DEBUG
81819f0f
CL
3792 if (flags & SLAB_STORE_USER)
3793 /*
3794 * Need to store information about allocs and frees after
3795 * the object.
3796 */
3797 size += 2 * sizeof(struct track);
80a9201a 3798#endif
81819f0f 3799
80a9201a
AP
3800 kasan_cache_create(s, &size, &s->flags);
3801#ifdef CONFIG_SLUB_DEBUG
d86bd1be 3802 if (flags & SLAB_RED_ZONE) {
81819f0f
CL
3803 /*
3804 * Add some empty padding so that we can catch
3805 * overwrites from earlier objects rather than let
3806 * tracking information or the free pointer be
0211a9c8 3807 * corrupted if a user writes before the start
81819f0f
CL
3808 * of the object.
3809 */
3810 size += sizeof(void *);
d86bd1be
JK
3811
3812 s->red_left_pad = sizeof(void *);
3813 s->red_left_pad = ALIGN(s->red_left_pad, s->align);
3814 size += s->red_left_pad;
3815 }
41ecc55b 3816#endif
672bba3a 3817
81819f0f
CL
3818 /*
3819 * SLUB stores one object immediately after another beginning from
3820 * offset 0. In order to align the objects we have to simply size
3821 * each object to conform to the alignment.
3822 */
45906855 3823 size = ALIGN(size, s->align);
81819f0f 3824 s->size = size;
4138fdfc 3825 s->reciprocal_size = reciprocal_value(size);
06b285dc
CL
3826 if (forced_order >= 0)
3827 order = forced_order;
3828 else
9736d2a9 3829 order = calculate_order(size);
81819f0f 3830
19af27af 3831 if ((int)order < 0)
81819f0f
CL
3832 return 0;
3833
b7a49f0d 3834 s->allocflags = 0;
834f3d11 3835 if (order)
b7a49f0d
CL
3836 s->allocflags |= __GFP_COMP;
3837
3838 if (s->flags & SLAB_CACHE_DMA)
2c59dd65 3839 s->allocflags |= GFP_DMA;
b7a49f0d 3840
6d6ea1e9
NB
3841 if (s->flags & SLAB_CACHE_DMA32)
3842 s->allocflags |= GFP_DMA32;
3843
b7a49f0d
CL
3844 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3845 s->allocflags |= __GFP_RECLAIMABLE;
3846
81819f0f
CL
3847 /*
3848 * Determine the number of objects per slab
3849 */
9736d2a9
MW
3850 s->oo = oo_make(order, size);
3851 s->min = oo_make(get_order(size), size);
205ab99d
CL
3852 if (oo_objects(s->oo) > oo_objects(s->max))
3853 s->max = s->oo;
81819f0f 3854
834f3d11 3855 return !!oo_objects(s->oo);
81819f0f
CL
3856}
3857
d50112ed 3858static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags)
81819f0f 3859{
37540008 3860 s->flags = kmem_cache_flags(s->size, flags, s->name);
2482ddec
KC
3861#ifdef CONFIG_SLAB_FREELIST_HARDENED
3862 s->random = get_random_long();
3863#endif
81819f0f 3864
06b285dc 3865 if (!calculate_sizes(s, -1))
81819f0f 3866 goto error;
3de47213
DR
3867 if (disable_higher_order_debug) {
3868 /*
3869 * Disable debugging flags that store metadata if the min slab
3870 * order increased.
3871 */
3b0efdfa 3872 if (get_order(s->size) > get_order(s->object_size)) {
3de47213
DR
3873 s->flags &= ~DEBUG_METADATA_FLAGS;
3874 s->offset = 0;
3875 if (!calculate_sizes(s, -1))
3876 goto error;
3877 }
3878 }
81819f0f 3879
2565409f
HC
3880#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
3881 defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
149daaf3 3882 if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0)
b789ef51
CL
3883 /* Enable fast mode */
3884 s->flags |= __CMPXCHG_DOUBLE;
3885#endif
3886
3b89d7d8
DR
3887 /*
3888 * The larger the object size is, the more pages we want on the partial
3889 * list to avoid pounding the page allocator excessively.
3890 */
49e22585
CL
3891 set_min_partial(s, ilog2(s->size) / 2);
3892
e6d0e1dc 3893 set_cpu_partial(s);
49e22585 3894
81819f0f 3895#ifdef CONFIG_NUMA
e2cb96b7 3896 s->remote_node_defrag_ratio = 1000;
81819f0f 3897#endif
210e7a43
TG
3898
3899 /* Initialize the pre-computed randomized freelist if slab is up */
3900 if (slab_state >= UP) {
3901 if (init_cache_random_seq(s))
3902 goto error;
3903 }
3904
55136592 3905 if (!init_kmem_cache_nodes(s))
dfb4f096 3906 goto error;
81819f0f 3907
55136592 3908 if (alloc_kmem_cache_cpus(s))
278b1bb1 3909 return 0;
ff12059e 3910
4c93c355 3911 free_kmem_cache_nodes(s);
81819f0f 3912error:
278b1bb1 3913 return -EINVAL;
81819f0f 3914}
81819f0f 3915
33b12c38 3916static void list_slab_objects(struct kmem_cache *s, struct page *page,
55860d96 3917 const char *text)
33b12c38
CL
3918{
3919#ifdef CONFIG_SLUB_DEBUG
3920 void *addr = page_address(page);
55860d96 3921 unsigned long *map;
33b12c38 3922 void *p;
aa456c7a 3923
945cf2b6 3924 slab_err(s, page, text, s->name);
33b12c38 3925 slab_lock(page);
33b12c38 3926
90e9f6a6 3927 map = get_map(s, page);
33b12c38
CL
3928 for_each_object(p, s, addr, page->objects) {
3929
4138fdfc 3930 if (!test_bit(__obj_to_index(s, addr, p), map)) {
96b94abc 3931 pr_err("Object 0x%p @offset=%tu\n", p, p - addr);
33b12c38
CL
3932 print_tracking(s, p);
3933 }
3934 }
55860d96 3935 put_map(map);
33b12c38
CL
3936 slab_unlock(page);
3937#endif
3938}
3939
81819f0f 3940/*
599870b1 3941 * Attempt to free all partial slabs on a node.
52b4b950
DS
3942 * This is called from __kmem_cache_shutdown(). We must take list_lock
3943 * because sysfs file might still access partial list after the shutdowning.
81819f0f 3944 */
599870b1 3945static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
81819f0f 3946{
60398923 3947 LIST_HEAD(discard);
81819f0f
CL
3948 struct page *page, *h;
3949
52b4b950
DS
3950 BUG_ON(irqs_disabled());
3951 spin_lock_irq(&n->list_lock);
916ac052 3952 list_for_each_entry_safe(page, h, &n->partial, slab_list) {
81819f0f 3953 if (!page->inuse) {
52b4b950 3954 remove_partial(n, page);
916ac052 3955 list_add(&page->slab_list, &discard);
33b12c38
CL
3956 } else {
3957 list_slab_objects(s, page,
55860d96 3958 "Objects remaining in %s on __kmem_cache_shutdown()");
599870b1 3959 }
33b12c38 3960 }
52b4b950 3961 spin_unlock_irq(&n->list_lock);
60398923 3962
916ac052 3963 list_for_each_entry_safe(page, h, &discard, slab_list)
60398923 3964 discard_slab(s, page);
81819f0f
CL
3965}
3966
f9e13c0a
SB
3967bool __kmem_cache_empty(struct kmem_cache *s)
3968{
3969 int node;
3970 struct kmem_cache_node *n;
3971
3972 for_each_kmem_cache_node(s, node, n)
3973 if (n->nr_partial || slabs_node(s, node))
3974 return false;
3975 return true;
3976}
3977
81819f0f 3978/*
672bba3a 3979 * Release all resources used by a slab cache.
81819f0f 3980 */
52b4b950 3981int __kmem_cache_shutdown(struct kmem_cache *s)
81819f0f
CL
3982{
3983 int node;
fa45dc25 3984 struct kmem_cache_node *n;
81819f0f
CL
3985
3986 flush_all(s);
81819f0f 3987 /* Attempt to free all objects */
fa45dc25 3988 for_each_kmem_cache_node(s, node, n) {
599870b1
CL
3989 free_partial(s, n);
3990 if (n->nr_partial || slabs_node(s, node))
81819f0f
CL
3991 return 1;
3992 }
81819f0f
CL
3993 return 0;
3994}
3995
5bb1bb35 3996#ifdef CONFIG_PRINTK
8e7f37f2
PM
3997void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct page *page)
3998{
3999 void *base;
4000 int __maybe_unused i;
4001 unsigned int objnr;
4002 void *objp;
4003 void *objp0;
4004 struct kmem_cache *s = page->slab_cache;
4005 struct track __maybe_unused *trackp;
4006
4007 kpp->kp_ptr = object;
4008 kpp->kp_page = page;
4009 kpp->kp_slab_cache = s;
4010 base = page_address(page);
4011 objp0 = kasan_reset_tag(object);
4012#ifdef CONFIG_SLUB_DEBUG
4013 objp = restore_red_left(s, objp0);
4014#else
4015 objp = objp0;
4016#endif
4017 objnr = obj_to_index(s, page, objp);
4018 kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp);
4019 objp = base + s->size * objnr;
4020 kpp->kp_objp = objp;
4021 if (WARN_ON_ONCE(objp < base || objp >= base + page->objects * s->size || (objp - base) % s->size) ||
4022 !(s->flags & SLAB_STORE_USER))
4023 return;
4024#ifdef CONFIG_SLUB_DEBUG
4025 trackp = get_track(s, objp, TRACK_ALLOC);
4026 kpp->kp_ret = (void *)trackp->addr;
4027#ifdef CONFIG_STACKTRACE
4028 for (i = 0; i < KS_ADDRS_COUNT && i < TRACK_ADDRS_COUNT; i++) {
4029 kpp->kp_stack[i] = (void *)trackp->addrs[i];
4030 if (!kpp->kp_stack[i])
4031 break;
4032 }
4033#endif
4034#endif
4035}
5bb1bb35 4036#endif
8e7f37f2 4037
81819f0f
CL
4038/********************************************************************
4039 * Kmalloc subsystem
4040 *******************************************************************/
4041
81819f0f
CL
4042static int __init setup_slub_min_order(char *str)
4043{
19af27af 4044 get_option(&str, (int *)&slub_min_order);
81819f0f
CL
4045
4046 return 1;
4047}
4048
4049__setup("slub_min_order=", setup_slub_min_order);
4050
4051static int __init setup_slub_max_order(char *str)
4052{
19af27af
AD
4053 get_option(&str, (int *)&slub_max_order);
4054 slub_max_order = min(slub_max_order, (unsigned int)MAX_ORDER - 1);
81819f0f
CL
4055
4056 return 1;
4057}
4058
4059__setup("slub_max_order=", setup_slub_max_order);
4060
4061static int __init setup_slub_min_objects(char *str)
4062{
19af27af 4063 get_option(&str, (int *)&slub_min_objects);
81819f0f
CL
4064
4065 return 1;
4066}
4067
4068__setup("slub_min_objects=", setup_slub_min_objects);
4069
81819f0f
CL
4070void *__kmalloc(size_t size, gfp_t flags)
4071{
aadb4bc4 4072 struct kmem_cache *s;
5b882be4 4073 void *ret;
81819f0f 4074
95a05b42 4075 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
eada35ef 4076 return kmalloc_large(size, flags);
aadb4bc4 4077
2c59dd65 4078 s = kmalloc_slab(size, flags);
aadb4bc4
CL
4079
4080 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
4081 return s;
4082
b89fb5ef 4083 ret = slab_alloc(s, flags, _RET_IP_, size);
5b882be4 4084
ca2b84cb 4085 trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
5b882be4 4086
0116523c 4087 ret = kasan_kmalloc(s, ret, size, flags);
0316bec2 4088
5b882be4 4089 return ret;
81819f0f
CL
4090}
4091EXPORT_SYMBOL(__kmalloc);
4092
5d1f57e4 4093#ifdef CONFIG_NUMA
f619cfe1
CL
4094static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
4095{
b1eeab67 4096 struct page *page;
e4f7c0b4 4097 void *ptr = NULL;
6a486c0a 4098 unsigned int order = get_order(size);
f619cfe1 4099
75f296d9 4100 flags |= __GFP_COMP;
6a486c0a
VB
4101 page = alloc_pages_node(node, flags, order);
4102 if (page) {
e4f7c0b4 4103 ptr = page_address(page);
96403bfe
MS
4104 mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
4105 PAGE_SIZE << order);
6a486c0a 4106 }
e4f7c0b4 4107
0116523c 4108 return kmalloc_large_node_hook(ptr, size, flags);
f619cfe1
CL
4109}
4110
81819f0f
CL
4111void *__kmalloc_node(size_t size, gfp_t flags, int node)
4112{
aadb4bc4 4113 struct kmem_cache *s;
5b882be4 4114 void *ret;
81819f0f 4115
95a05b42 4116 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
5b882be4
EGM
4117 ret = kmalloc_large_node(size, flags, node);
4118
ca2b84cb
EGM
4119 trace_kmalloc_node(_RET_IP_, ret,
4120 size, PAGE_SIZE << get_order(size),
4121 flags, node);
5b882be4
EGM
4122
4123 return ret;
4124 }
aadb4bc4 4125
2c59dd65 4126 s = kmalloc_slab(size, flags);
aadb4bc4
CL
4127
4128 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913
CL
4129 return s;
4130
b89fb5ef 4131 ret = slab_alloc_node(s, flags, node, _RET_IP_, size);
5b882be4 4132
ca2b84cb 4133 trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
5b882be4 4134
0116523c 4135 ret = kasan_kmalloc(s, ret, size, flags);
0316bec2 4136
5b882be4 4137 return ret;
81819f0f
CL
4138}
4139EXPORT_SYMBOL(__kmalloc_node);
6dfd1b65 4140#endif /* CONFIG_NUMA */
81819f0f 4141
ed18adc1
KC
4142#ifdef CONFIG_HARDENED_USERCOPY
4143/*
afcc90f8
KC
4144 * Rejects incorrectly sized objects and objects that are to be copied
4145 * to/from userspace but do not fall entirely within the containing slab
4146 * cache's usercopy region.
ed18adc1
KC
4147 *
4148 * Returns NULL if check passes, otherwise const char * to name of cache
4149 * to indicate an error.
4150 */
f4e6e289
KC
4151void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
4152 bool to_user)
ed18adc1
KC
4153{
4154 struct kmem_cache *s;
44065b2e 4155 unsigned int offset;
ed18adc1 4156 size_t object_size;
b89fb5ef 4157 bool is_kfence = is_kfence_address(ptr);
ed18adc1 4158
96fedce2
AK
4159 ptr = kasan_reset_tag(ptr);
4160
ed18adc1
KC
4161 /* Find object and usable object size. */
4162 s = page->slab_cache;
ed18adc1
KC
4163
4164 /* Reject impossible pointers. */
4165 if (ptr < page_address(page))
f4e6e289
KC
4166 usercopy_abort("SLUB object not in SLUB page?!", NULL,
4167 to_user, 0, n);
ed18adc1
KC
4168
4169 /* Find offset within object. */
b89fb5ef
AP
4170 if (is_kfence)
4171 offset = ptr - kfence_object_start(ptr);
4172 else
4173 offset = (ptr - page_address(page)) % s->size;
ed18adc1
KC
4174
4175 /* Adjust for redzone and reject if within the redzone. */
b89fb5ef 4176 if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) {
ed18adc1 4177 if (offset < s->red_left_pad)
f4e6e289
KC
4178 usercopy_abort("SLUB object in left red zone",
4179 s->name, to_user, offset, n);
ed18adc1
KC
4180 offset -= s->red_left_pad;
4181 }
4182
afcc90f8
KC
4183 /* Allow address range falling entirely within usercopy region. */
4184 if (offset >= s->useroffset &&
4185 offset - s->useroffset <= s->usersize &&
4186 n <= s->useroffset - offset + s->usersize)
f4e6e289 4187 return;
ed18adc1 4188
afcc90f8
KC
4189 /*
4190 * If the copy is still within the allocated object, produce
4191 * a warning instead of rejecting the copy. This is intended
4192 * to be a temporary method to find any missing usercopy
4193 * whitelists.
4194 */
4195 object_size = slab_ksize(s);
2d891fbc
KC
4196 if (usercopy_fallback &&
4197 offset <= object_size && n <= object_size - offset) {
afcc90f8
KC
4198 usercopy_warn("SLUB object", s->name, to_user, offset, n);
4199 return;
4200 }
ed18adc1 4201
f4e6e289 4202 usercopy_abort("SLUB object", s->name, to_user, offset, n);
ed18adc1
KC
4203}
4204#endif /* CONFIG_HARDENED_USERCOPY */
4205
10d1f8cb 4206size_t __ksize(const void *object)
81819f0f 4207{
272c1d21 4208 struct page *page;
81819f0f 4209
ef8b4520 4210 if (unlikely(object == ZERO_SIZE_PTR))
272c1d21
CL
4211 return 0;
4212
294a80a8 4213 page = virt_to_head_page(object);
294a80a8 4214
76994412
PE
4215 if (unlikely(!PageSlab(page))) {
4216 WARN_ON(!PageCompound(page));
a50b854e 4217 return page_size(page);
76994412 4218 }
81819f0f 4219
1b4f59e3 4220 return slab_ksize(page->slab_cache);
81819f0f 4221}
10d1f8cb 4222EXPORT_SYMBOL(__ksize);
81819f0f
CL
4223
4224void kfree(const void *x)
4225{
81819f0f 4226 struct page *page;
5bb983b0 4227 void *object = (void *)x;
81819f0f 4228
2121db74
PE
4229 trace_kfree(_RET_IP_, x);
4230
2408c550 4231 if (unlikely(ZERO_OR_NULL_PTR(x)))
81819f0f
CL
4232 return;
4233
b49af68f 4234 page = virt_to_head_page(x);
aadb4bc4 4235 if (unlikely(!PageSlab(page))) {
6a486c0a
VB
4236 unsigned int order = compound_order(page);
4237
0937502a 4238 BUG_ON(!PageCompound(page));
47adccce 4239 kfree_hook(object);
96403bfe
MS
4240 mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
4241 -(PAGE_SIZE << order));
6a486c0a 4242 __free_pages(page, order);
aadb4bc4
CL
4243 return;
4244 }
81084651 4245 slab_free(page->slab_cache, page, object, NULL, 1, _RET_IP_);
81819f0f
CL
4246}
4247EXPORT_SYMBOL(kfree);
4248
832f37f5
VD
4249#define SHRINK_PROMOTE_MAX 32
4250
2086d26a 4251/*
832f37f5
VD
4252 * kmem_cache_shrink discards empty slabs and promotes the slabs filled
4253 * up most to the head of the partial lists. New allocations will then
4254 * fill those up and thus they can be removed from the partial lists.
672bba3a
CL
4255 *
4256 * The slabs with the least items are placed last. This results in them
4257 * being allocated from last increasing the chance that the last objects
4258 * are freed in them.
2086d26a 4259 */
c9fc5864 4260int __kmem_cache_shrink(struct kmem_cache *s)
2086d26a
CL
4261{
4262 int node;
4263 int i;
4264 struct kmem_cache_node *n;
4265 struct page *page;
4266 struct page *t;
832f37f5
VD
4267 struct list_head discard;
4268 struct list_head promote[SHRINK_PROMOTE_MAX];
2086d26a 4269 unsigned long flags;
ce3712d7 4270 int ret = 0;
2086d26a 4271
2086d26a 4272 flush_all(s);
fa45dc25 4273 for_each_kmem_cache_node(s, node, n) {
832f37f5
VD
4274 INIT_LIST_HEAD(&discard);
4275 for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
4276 INIT_LIST_HEAD(promote + i);
2086d26a
CL
4277
4278 spin_lock_irqsave(&n->list_lock, flags);
4279
4280 /*
832f37f5 4281 * Build lists of slabs to discard or promote.
2086d26a 4282 *
672bba3a
CL
4283 * Note that concurrent frees may occur while we hold the
4284 * list_lock. page->inuse here is the upper limit.
2086d26a 4285 */
916ac052 4286 list_for_each_entry_safe(page, t, &n->partial, slab_list) {
832f37f5
VD
4287 int free = page->objects - page->inuse;
4288
4289 /* Do not reread page->inuse */
4290 barrier();
4291
4292 /* We do not keep full slabs on the list */
4293 BUG_ON(free <= 0);
4294
4295 if (free == page->objects) {
916ac052 4296 list_move(&page->slab_list, &discard);
69cb8e6b 4297 n->nr_partial--;
832f37f5 4298 } else if (free <= SHRINK_PROMOTE_MAX)
916ac052 4299 list_move(&page->slab_list, promote + free - 1);
2086d26a
CL
4300 }
4301
2086d26a 4302 /*
832f37f5
VD
4303 * Promote the slabs filled up most to the head of the
4304 * partial list.
2086d26a 4305 */
832f37f5
VD
4306 for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
4307 list_splice(promote + i, &n->partial);
2086d26a 4308
2086d26a 4309 spin_unlock_irqrestore(&n->list_lock, flags);
69cb8e6b
CL
4310
4311 /* Release empty slabs */
916ac052 4312 list_for_each_entry_safe(page, t, &discard, slab_list)
69cb8e6b 4313 discard_slab(s, page);
ce3712d7
VD
4314
4315 if (slabs_node(s, node))
4316 ret = 1;
2086d26a
CL
4317 }
4318
ce3712d7 4319 return ret;
2086d26a 4320}
2086d26a 4321
b9049e23
YG
4322static int slab_mem_going_offline_callback(void *arg)
4323{
4324 struct kmem_cache *s;
4325
18004c5d 4326 mutex_lock(&slab_mutex);
b9049e23 4327 list_for_each_entry(s, &slab_caches, list)
c9fc5864 4328 __kmem_cache_shrink(s);
18004c5d 4329 mutex_unlock(&slab_mutex);
b9049e23
YG
4330
4331 return 0;
4332}
4333
4334static void slab_mem_offline_callback(void *arg)
4335{
b9049e23
YG
4336 struct memory_notify *marg = arg;
4337 int offline_node;
4338
b9d5ab25 4339 offline_node = marg->status_change_nid_normal;
b9049e23
YG
4340
4341 /*
4342 * If the node still has available memory. we need kmem_cache_node
4343 * for it yet.
4344 */
4345 if (offline_node < 0)
4346 return;
4347
18004c5d 4348 mutex_lock(&slab_mutex);
7e1fa93d 4349 node_clear(offline_node, slab_nodes);
666716fd
VB
4350 /*
4351 * We no longer free kmem_cache_node structures here, as it would be
4352 * racy with all get_node() users, and infeasible to protect them with
4353 * slab_mutex.
4354 */
18004c5d 4355 mutex_unlock(&slab_mutex);
b9049e23
YG
4356}
4357
4358static int slab_mem_going_online_callback(void *arg)
4359{
4360 struct kmem_cache_node *n;
4361 struct kmem_cache *s;
4362 struct memory_notify *marg = arg;
b9d5ab25 4363 int nid = marg->status_change_nid_normal;
b9049e23
YG
4364 int ret = 0;
4365
4366 /*
4367 * If the node's memory is already available, then kmem_cache_node is
4368 * already created. Nothing to do.
4369 */
4370 if (nid < 0)
4371 return 0;
4372
4373 /*
0121c619 4374 * We are bringing a node online. No memory is available yet. We must
b9049e23
YG
4375 * allocate a kmem_cache_node structure in order to bring the node
4376 * online.
4377 */
18004c5d 4378 mutex_lock(&slab_mutex);
b9049e23 4379 list_for_each_entry(s, &slab_caches, list) {
666716fd
VB
4380 /*
4381 * The structure may already exist if the node was previously
4382 * onlined and offlined.
4383 */
4384 if (get_node(s, nid))
4385 continue;
b9049e23
YG
4386 /*
4387 * XXX: kmem_cache_alloc_node will fallback to other nodes
4388 * since memory is not yet available from the node that
4389 * is brought up.
4390 */
8de66a0c 4391 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
b9049e23
YG
4392 if (!n) {
4393 ret = -ENOMEM;
4394 goto out;
4395 }
4053497d 4396 init_kmem_cache_node(n);
b9049e23
YG
4397 s->node[nid] = n;
4398 }
7e1fa93d
VB
4399 /*
4400 * Any cache created after this point will also have kmem_cache_node
4401 * initialized for the new node.
4402 */
4403 node_set(nid, slab_nodes);
b9049e23 4404out:
18004c5d 4405 mutex_unlock(&slab_mutex);
b9049e23
YG
4406 return ret;
4407}
4408
4409static int slab_memory_callback(struct notifier_block *self,
4410 unsigned long action, void *arg)
4411{
4412 int ret = 0;
4413
4414 switch (action) {
4415 case MEM_GOING_ONLINE:
4416 ret = slab_mem_going_online_callback(arg);
4417 break;
4418 case MEM_GOING_OFFLINE:
4419 ret = slab_mem_going_offline_callback(arg);
4420 break;
4421 case MEM_OFFLINE:
4422 case MEM_CANCEL_ONLINE:
4423 slab_mem_offline_callback(arg);
4424 break;
4425 case MEM_ONLINE:
4426 case MEM_CANCEL_OFFLINE:
4427 break;
4428 }
dc19f9db
KH
4429 if (ret)
4430 ret = notifier_from_errno(ret);
4431 else
4432 ret = NOTIFY_OK;
b9049e23
YG
4433 return ret;
4434}
4435
3ac38faa
AM
4436static struct notifier_block slab_memory_callback_nb = {
4437 .notifier_call = slab_memory_callback,
4438 .priority = SLAB_CALLBACK_PRI,
4439};
b9049e23 4440
81819f0f
CL
4441/********************************************************************
4442 * Basic setup of slabs
4443 *******************************************************************/
4444
51df1142
CL
4445/*
4446 * Used for early kmem_cache structures that were allocated using
dffb4d60
CL
4447 * the page allocator. Allocate them properly then fix up the pointers
4448 * that may be pointing to the wrong kmem_cache structure.
51df1142
CL
4449 */
4450
dffb4d60 4451static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
51df1142
CL
4452{
4453 int node;
dffb4d60 4454 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
fa45dc25 4455 struct kmem_cache_node *n;
51df1142 4456
dffb4d60 4457 memcpy(s, static_cache, kmem_cache->object_size);
51df1142 4458
7d557b3c
GC
4459 /*
4460 * This runs very early, and only the boot processor is supposed to be
4461 * up. Even if it weren't true, IRQs are not up so we couldn't fire
4462 * IPIs around.
4463 */
4464 __flush_cpu_slab(s, smp_processor_id());
fa45dc25 4465 for_each_kmem_cache_node(s, node, n) {
51df1142
CL
4466 struct page *p;
4467
916ac052 4468 list_for_each_entry(p, &n->partial, slab_list)
fa45dc25 4469 p->slab_cache = s;
51df1142 4470
607bf324 4471#ifdef CONFIG_SLUB_DEBUG
916ac052 4472 list_for_each_entry(p, &n->full, slab_list)
fa45dc25 4473 p->slab_cache = s;
51df1142 4474#endif
51df1142 4475 }
dffb4d60
CL
4476 list_add(&s->list, &slab_caches);
4477 return s;
51df1142
CL
4478}
4479
81819f0f
CL
4480void __init kmem_cache_init(void)
4481{
dffb4d60
CL
4482 static __initdata struct kmem_cache boot_kmem_cache,
4483 boot_kmem_cache_node;
7e1fa93d 4484 int node;
51df1142 4485
fc8d8620
SG
4486 if (debug_guardpage_minorder())
4487 slub_max_order = 0;
4488
dffb4d60
CL
4489 kmem_cache_node = &boot_kmem_cache_node;
4490 kmem_cache = &boot_kmem_cache;
51df1142 4491
7e1fa93d
VB
4492 /*
4493 * Initialize the nodemask for which we will allocate per node
4494 * structures. Here we don't need taking slab_mutex yet.
4495 */
4496 for_each_node_state(node, N_NORMAL_MEMORY)
4497 node_set(node, slab_nodes);
4498
dffb4d60 4499 create_boot_cache(kmem_cache_node, "kmem_cache_node",
8eb8284b 4500 sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0);
b9049e23 4501
3ac38faa 4502 register_hotmemory_notifier(&slab_memory_callback_nb);
81819f0f
CL
4503
4504 /* Able to allocate the per node structures */
4505 slab_state = PARTIAL;
4506
dffb4d60
CL
4507 create_boot_cache(kmem_cache, "kmem_cache",
4508 offsetof(struct kmem_cache, node) +
4509 nr_node_ids * sizeof(struct kmem_cache_node *),
8eb8284b 4510 SLAB_HWCACHE_ALIGN, 0, 0);
8a13a4cc 4511
dffb4d60 4512 kmem_cache = bootstrap(&boot_kmem_cache);
dffb4d60 4513 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
51df1142
CL
4514
4515 /* Now we can use the kmem_cache to allocate kmalloc slabs */
34cc6990 4516 setup_kmalloc_cache_index_table();
f97d5f63 4517 create_kmalloc_caches(0);
81819f0f 4518
210e7a43
TG
4519 /* Setup random freelists for each cache */
4520 init_freelist_randomization();
4521
a96a87bf
SAS
4522 cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", NULL,
4523 slub_cpu_dead);
81819f0f 4524
b9726c26 4525 pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%u\n",
f97d5f63 4526 cache_line_size(),
81819f0f
CL
4527 slub_min_order, slub_max_order, slub_min_objects,
4528 nr_cpu_ids, nr_node_ids);
4529}
4530
7e85ee0c
PE
4531void __init kmem_cache_init_late(void)
4532{
7e85ee0c
PE
4533}
4534
2633d7a0 4535struct kmem_cache *
f4957d5b 4536__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
d50112ed 4537 slab_flags_t flags, void (*ctor)(void *))
81819f0f 4538{
10befea9 4539 struct kmem_cache *s;
81819f0f 4540
a44cb944 4541 s = find_mergeable(size, align, flags, name, ctor);
81819f0f
CL
4542 if (s) {
4543 s->refcount++;
84d0ddd6 4544
81819f0f
CL
4545 /*
4546 * Adjust the object sizes so that we clear
4547 * the complete object on kzalloc.
4548 */
1b473f29 4549 s->object_size = max(s->object_size, size);
52ee6d74 4550 s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
6446faa2 4551
7b8f3b66 4552 if (sysfs_slab_alias(s, name)) {
7b8f3b66 4553 s->refcount--;
cbb79694 4554 s = NULL;
7b8f3b66 4555 }
a0e1d1be 4556 }
6446faa2 4557
cbb79694
CL
4558 return s;
4559}
84c1cf62 4560
d50112ed 4561int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags)
cbb79694 4562{
aac3a166
PE
4563 int err;
4564
4565 err = kmem_cache_open(s, flags);
4566 if (err)
4567 return err;
20cea968 4568
45530c44
CL
4569 /* Mutex is not taken during early boot */
4570 if (slab_state <= UP)
4571 return 0;
4572
aac3a166 4573 err = sysfs_slab_add(s);
aac3a166 4574 if (err)
52b4b950 4575 __kmem_cache_release(s);
20cea968 4576
aac3a166 4577 return err;
81819f0f 4578}
81819f0f 4579
ce71e27c 4580void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
81819f0f 4581{
aadb4bc4 4582 struct kmem_cache *s;
94b528d0 4583 void *ret;
aadb4bc4 4584
95a05b42 4585 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
eada35ef
PE
4586 return kmalloc_large(size, gfpflags);
4587
2c59dd65 4588 s = kmalloc_slab(size, gfpflags);
81819f0f 4589
2408c550 4590 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 4591 return s;
81819f0f 4592
b89fb5ef 4593 ret = slab_alloc(s, gfpflags, caller, size);
94b528d0 4594
25985edc 4595 /* Honor the call site pointer we received. */
ca2b84cb 4596 trace_kmalloc(caller, ret, size, s->size, gfpflags);
94b528d0
EGM
4597
4598 return ret;
81819f0f 4599}
fd7cb575 4600EXPORT_SYMBOL(__kmalloc_track_caller);
81819f0f 4601
5d1f57e4 4602#ifdef CONFIG_NUMA
81819f0f 4603void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
ce71e27c 4604 int node, unsigned long caller)
81819f0f 4605{
aadb4bc4 4606 struct kmem_cache *s;
94b528d0 4607 void *ret;
aadb4bc4 4608
95a05b42 4609 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
d3e14aa3
XF
4610 ret = kmalloc_large_node(size, gfpflags, node);
4611
4612 trace_kmalloc_node(caller, ret,
4613 size, PAGE_SIZE << get_order(size),
4614 gfpflags, node);
4615
4616 return ret;
4617 }
eada35ef 4618
2c59dd65 4619 s = kmalloc_slab(size, gfpflags);
81819f0f 4620
2408c550 4621 if (unlikely(ZERO_OR_NULL_PTR(s)))
6cb8f913 4622 return s;
81819f0f 4623
b89fb5ef 4624 ret = slab_alloc_node(s, gfpflags, node, caller, size);
94b528d0 4625
25985edc 4626 /* Honor the call site pointer we received. */
ca2b84cb 4627 trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
94b528d0
EGM
4628
4629 return ret;
81819f0f 4630}
fd7cb575 4631EXPORT_SYMBOL(__kmalloc_node_track_caller);
5d1f57e4 4632#endif
81819f0f 4633
ab4d5ed5 4634#ifdef CONFIG_SYSFS
205ab99d
CL
4635static int count_inuse(struct page *page)
4636{
4637 return page->inuse;
4638}
4639
4640static int count_total(struct page *page)
4641{
4642 return page->objects;
4643}
ab4d5ed5 4644#endif
205ab99d 4645
ab4d5ed5 4646#ifdef CONFIG_SLUB_DEBUG
90e9f6a6 4647static void validate_slab(struct kmem_cache *s, struct page *page)
53e15af0
CL
4648{
4649 void *p;
a973e9dd 4650 void *addr = page_address(page);
90e9f6a6
YZ
4651 unsigned long *map;
4652
4653 slab_lock(page);
53e15af0 4654
dd98afd4 4655 if (!check_slab(s, page) || !on_freelist(s, page, NULL))
90e9f6a6 4656 goto unlock;
53e15af0
CL
4657
4658 /* Now we know that a valid freelist exists */
90e9f6a6 4659 map = get_map(s, page);
5f80b13a 4660 for_each_object(p, s, addr, page->objects) {
4138fdfc 4661 u8 val = test_bit(__obj_to_index(s, addr, p), map) ?
dd98afd4 4662 SLUB_RED_INACTIVE : SLUB_RED_ACTIVE;
53e15af0 4663
dd98afd4
YZ
4664 if (!check_object(s, page, p, val))
4665 break;
4666 }
90e9f6a6
YZ
4667 put_map(map);
4668unlock:
881db7fb 4669 slab_unlock(page);
53e15af0
CL
4670}
4671
434e245d 4672static int validate_slab_node(struct kmem_cache *s,
90e9f6a6 4673 struct kmem_cache_node *n)
53e15af0
CL
4674{
4675 unsigned long count = 0;
4676 struct page *page;
4677 unsigned long flags;
4678
4679 spin_lock_irqsave(&n->list_lock, flags);
4680
916ac052 4681 list_for_each_entry(page, &n->partial, slab_list) {
90e9f6a6 4682 validate_slab(s, page);
53e15af0
CL
4683 count++;
4684 }
1f9f78b1 4685 if (count != n->nr_partial) {
f9f58285
FF
4686 pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
4687 s->name, count, n->nr_partial);
1f9f78b1
OG
4688 slab_add_kunit_errors();
4689 }
53e15af0
CL
4690
4691 if (!(s->flags & SLAB_STORE_USER))
4692 goto out;
4693
916ac052 4694 list_for_each_entry(page, &n->full, slab_list) {
90e9f6a6 4695 validate_slab(s, page);
53e15af0
CL
4696 count++;
4697 }
1f9f78b1 4698 if (count != atomic_long_read(&n->nr_slabs)) {
f9f58285
FF
4699 pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
4700 s->name, count, atomic_long_read(&n->nr_slabs));
1f9f78b1
OG
4701 slab_add_kunit_errors();
4702 }
53e15af0
CL
4703
4704out:
4705 spin_unlock_irqrestore(&n->list_lock, flags);
4706 return count;
4707}
4708
1f9f78b1 4709long validate_slab_cache(struct kmem_cache *s)
53e15af0
CL
4710{
4711 int node;
4712 unsigned long count = 0;
fa45dc25 4713 struct kmem_cache_node *n;
53e15af0
CL
4714
4715 flush_all(s);
fa45dc25 4716 for_each_kmem_cache_node(s, node, n)
90e9f6a6
YZ
4717 count += validate_slab_node(s, n);
4718
53e15af0
CL
4719 return count;
4720}
1f9f78b1
OG
4721EXPORT_SYMBOL(validate_slab_cache);
4722
88a420e4 4723/*
672bba3a 4724 * Generate lists of code addresses where slabcache objects are allocated
88a420e4
CL
4725 * and freed.
4726 */
4727
4728struct location {
4729 unsigned long count;
ce71e27c 4730 unsigned long addr;
45edfa58
CL
4731 long long sum_time;
4732 long min_time;
4733 long max_time;
4734 long min_pid;
4735 long max_pid;
174596a0 4736 DECLARE_BITMAP(cpus, NR_CPUS);
45edfa58 4737 nodemask_t nodes;
88a420e4
CL
4738};
4739
4740struct loc_track {
4741 unsigned long max;
4742 unsigned long count;
4743 struct location *loc;
4744};
4745
4746static void free_loc_track(struct loc_track *t)
4747{
4748 if (t->max)
4749 free_pages((unsigned long)t->loc,
4750 get_order(sizeof(struct location) * t->max));
4751}
4752
68dff6a9 4753static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
88a420e4
CL
4754{
4755 struct location *l;
4756 int order;
4757
88a420e4
CL
4758 order = get_order(sizeof(struct location) * max);
4759
68dff6a9 4760 l = (void *)__get_free_pages(flags, order);
88a420e4
CL
4761 if (!l)
4762 return 0;
4763
4764 if (t->count) {
4765 memcpy(l, t->loc, sizeof(struct location) * t->count);
4766 free_loc_track(t);
4767 }
4768 t->max = max;
4769 t->loc = l;
4770 return 1;
4771}
4772
4773static int add_location(struct loc_track *t, struct kmem_cache *s,
45edfa58 4774 const struct track *track)
88a420e4
CL
4775{
4776 long start, end, pos;
4777 struct location *l;
ce71e27c 4778 unsigned long caddr;
45edfa58 4779 unsigned long age = jiffies - track->when;
88a420e4
CL
4780
4781 start = -1;
4782 end = t->count;
4783
4784 for ( ; ; ) {
4785 pos = start + (end - start + 1) / 2;
4786
4787 /*
4788 * There is nothing at "end". If we end up there
4789 * we need to add something to before end.
4790 */
4791 if (pos == end)
4792 break;
4793
4794 caddr = t->loc[pos].addr;
45edfa58
CL
4795 if (track->addr == caddr) {
4796
4797 l = &t->loc[pos];
4798 l->count++;
4799 if (track->when) {
4800 l->sum_time += age;
4801 if (age < l->min_time)
4802 l->min_time = age;
4803 if (age > l->max_time)
4804 l->max_time = age;
4805
4806 if (track->pid < l->min_pid)
4807 l->min_pid = track->pid;
4808 if (track->pid > l->max_pid)
4809 l->max_pid = track->pid;
4810
174596a0
RR
4811 cpumask_set_cpu(track->cpu,
4812 to_cpumask(l->cpus));
45edfa58
CL
4813 }
4814 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
4815 return 1;
4816 }
4817
45edfa58 4818 if (track->addr < caddr)
88a420e4
CL
4819 end = pos;
4820 else
4821 start = pos;
4822 }
4823
4824 /*
672bba3a 4825 * Not found. Insert new tracking element.
88a420e4 4826 */
68dff6a9 4827 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
88a420e4
CL
4828 return 0;
4829
4830 l = t->loc + pos;
4831 if (pos < t->count)
4832 memmove(l + 1, l,
4833 (t->count - pos) * sizeof(struct location));
4834 t->count++;
4835 l->count = 1;
45edfa58
CL
4836 l->addr = track->addr;
4837 l->sum_time = age;
4838 l->min_time = age;
4839 l->max_time = age;
4840 l->min_pid = track->pid;
4841 l->max_pid = track->pid;
174596a0
RR
4842 cpumask_clear(to_cpumask(l->cpus));
4843 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
45edfa58
CL
4844 nodes_clear(l->nodes);
4845 node_set(page_to_nid(virt_to_page(track)), l->nodes);
88a420e4
CL
4846 return 1;
4847}
4848
4849static void process_slab(struct loc_track *t, struct kmem_cache *s,
90e9f6a6 4850 struct page *page, enum track_item alloc)
88a420e4 4851{
a973e9dd 4852 void *addr = page_address(page);
88a420e4 4853 void *p;
90e9f6a6 4854 unsigned long *map;
88a420e4 4855
90e9f6a6 4856 map = get_map(s, page);
224a88be 4857 for_each_object(p, s, addr, page->objects)
4138fdfc 4858 if (!test_bit(__obj_to_index(s, addr, p), map))
45edfa58 4859 add_location(t, s, get_track(s, p, alloc));
90e9f6a6 4860 put_map(map);
88a420e4
CL
4861}
4862
4863static int list_locations(struct kmem_cache *s, char *buf,
bf16d19a 4864 enum track_item alloc)
88a420e4 4865{
e374d483 4866 int len = 0;
88a420e4 4867 unsigned long i;
68dff6a9 4868 struct loc_track t = { 0, 0, NULL };
88a420e4 4869 int node;
fa45dc25 4870 struct kmem_cache_node *n;
88a420e4 4871
90e9f6a6
YZ
4872 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
4873 GFP_KERNEL)) {
bf16d19a 4874 return sysfs_emit(buf, "Out of memory\n");
bbd7d57b 4875 }
88a420e4
CL
4876 /* Push back cpu slabs */
4877 flush_all(s);
4878
fa45dc25 4879 for_each_kmem_cache_node(s, node, n) {
88a420e4
CL
4880 unsigned long flags;
4881 struct page *page;
4882
9e86943b 4883 if (!atomic_long_read(&n->nr_slabs))
88a420e4
CL
4884 continue;
4885
4886 spin_lock_irqsave(&n->list_lock, flags);
916ac052 4887 list_for_each_entry(page, &n->partial, slab_list)
90e9f6a6 4888 process_slab(&t, s, page, alloc);
916ac052 4889 list_for_each_entry(page, &n->full, slab_list)
90e9f6a6 4890 process_slab(&t, s, page, alloc);
88a420e4
CL
4891 spin_unlock_irqrestore(&n->list_lock, flags);
4892 }
4893
4894 for (i = 0; i < t.count; i++) {
45edfa58 4895 struct location *l = &t.loc[i];
88a420e4 4896
bf16d19a 4897 len += sysfs_emit_at(buf, len, "%7ld ", l->count);
45edfa58
CL
4898
4899 if (l->addr)
bf16d19a 4900 len += sysfs_emit_at(buf, len, "%pS", (void *)l->addr);
88a420e4 4901 else
bf16d19a
JP
4902 len += sysfs_emit_at(buf, len, "<not-available>");
4903
4904 if (l->sum_time != l->min_time)
4905 len += sysfs_emit_at(buf, len, " age=%ld/%ld/%ld",
4906 l->min_time,
4907 (long)div_u64(l->sum_time,
4908 l->count),
4909 l->max_time);
4910 else
4911 len += sysfs_emit_at(buf, len, " age=%ld", l->min_time);
45edfa58
CL
4912
4913 if (l->min_pid != l->max_pid)
bf16d19a
JP
4914 len += sysfs_emit_at(buf, len, " pid=%ld-%ld",
4915 l->min_pid, l->max_pid);
45edfa58 4916 else
bf16d19a
JP
4917 len += sysfs_emit_at(buf, len, " pid=%ld",
4918 l->min_pid);
45edfa58 4919
174596a0 4920 if (num_online_cpus() > 1 &&
bf16d19a
JP
4921 !cpumask_empty(to_cpumask(l->cpus)))
4922 len += sysfs_emit_at(buf, len, " cpus=%*pbl",
4923 cpumask_pr_args(to_cpumask(l->cpus)));
4924
4925 if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
4926 len += sysfs_emit_at(buf, len, " nodes=%*pbl",
4927 nodemask_pr_args(&l->nodes));
4928
4929 len += sysfs_emit_at(buf, len, "\n");
88a420e4
CL
4930 }
4931
4932 free_loc_track(&t);
4933 if (!t.count)
bf16d19a
JP
4934 len += sysfs_emit_at(buf, len, "No data\n");
4935
e374d483 4936 return len;
88a420e4 4937}
6dfd1b65 4938#endif /* CONFIG_SLUB_DEBUG */
88a420e4 4939
ab4d5ed5 4940#ifdef CONFIG_SYSFS
81819f0f 4941enum slab_stat_type {
205ab99d
CL
4942 SL_ALL, /* All slabs */
4943 SL_PARTIAL, /* Only partially allocated slabs */
4944 SL_CPU, /* Only slabs used for cpu caches */
4945 SL_OBJECTS, /* Determine allocated objects not slabs */
4946 SL_TOTAL /* Determine object capacity not slabs */
81819f0f
CL
4947};
4948
205ab99d 4949#define SO_ALL (1 << SL_ALL)
81819f0f
CL
4950#define SO_PARTIAL (1 << SL_PARTIAL)
4951#define SO_CPU (1 << SL_CPU)
4952#define SO_OBJECTS (1 << SL_OBJECTS)
205ab99d 4953#define SO_TOTAL (1 << SL_TOTAL)
81819f0f 4954
62e5c4b4 4955static ssize_t show_slab_objects(struct kmem_cache *s,
bf16d19a 4956 char *buf, unsigned long flags)
81819f0f
CL
4957{
4958 unsigned long total = 0;
81819f0f
CL
4959 int node;
4960 int x;
4961 unsigned long *nodes;
bf16d19a 4962 int len = 0;
81819f0f 4963
6396bb22 4964 nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL);
62e5c4b4
CG
4965 if (!nodes)
4966 return -ENOMEM;
81819f0f 4967
205ab99d
CL
4968 if (flags & SO_CPU) {
4969 int cpu;
81819f0f 4970
205ab99d 4971 for_each_possible_cpu(cpu) {
d0e0ac97
CG
4972 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab,
4973 cpu);
ec3ab083 4974 int node;
49e22585 4975 struct page *page;
dfb4f096 4976
4db0c3c2 4977 page = READ_ONCE(c->page);
ec3ab083
CL
4978 if (!page)
4979 continue;
205ab99d 4980
ec3ab083
CL
4981 node = page_to_nid(page);
4982 if (flags & SO_TOTAL)
4983 x = page->objects;
4984 else if (flags & SO_OBJECTS)
4985 x = page->inuse;
4986 else
4987 x = 1;
49e22585 4988
ec3ab083
CL
4989 total += x;
4990 nodes[node] += x;
4991
a93cf07b 4992 page = slub_percpu_partial_read_once(c);
49e22585 4993 if (page) {
8afb1474
LZ
4994 node = page_to_nid(page);
4995 if (flags & SO_TOTAL)
4996 WARN_ON_ONCE(1);
4997 else if (flags & SO_OBJECTS)
4998 WARN_ON_ONCE(1);
4999 else
5000 x = page->pages;
bc6697d8
ED
5001 total += x;
5002 nodes[node] += x;
49e22585 5003 }
81819f0f
CL
5004 }
5005 }
5006
e4f8e513
QC
5007 /*
5008 * It is impossible to take "mem_hotplug_lock" here with "kernfs_mutex"
5009 * already held which will conflict with an existing lock order:
5010 *
5011 * mem_hotplug_lock->slab_mutex->kernfs_mutex
5012 *
5013 * We don't really need mem_hotplug_lock (to hold off
5014 * slab_mem_going_offline_callback) here because slab's memory hot
5015 * unplug code doesn't destroy the kmem_cache->node[] data.
5016 */
5017
ab4d5ed5 5018#ifdef CONFIG_SLUB_DEBUG
205ab99d 5019 if (flags & SO_ALL) {
fa45dc25
CL
5020 struct kmem_cache_node *n;
5021
5022 for_each_kmem_cache_node(s, node, n) {
205ab99d 5023
d0e0ac97
CG
5024 if (flags & SO_TOTAL)
5025 x = atomic_long_read(&n->total_objects);
5026 else if (flags & SO_OBJECTS)
5027 x = atomic_long_read(&n->total_objects) -
5028 count_partial(n, count_free);
81819f0f 5029 else
205ab99d 5030 x = atomic_long_read(&n->nr_slabs);
81819f0f
CL
5031 total += x;
5032 nodes[node] += x;
5033 }
5034
ab4d5ed5
CL
5035 } else
5036#endif
5037 if (flags & SO_PARTIAL) {
fa45dc25 5038 struct kmem_cache_node *n;
81819f0f 5039
fa45dc25 5040 for_each_kmem_cache_node(s, node, n) {
205ab99d
CL
5041 if (flags & SO_TOTAL)
5042 x = count_partial(n, count_total);
5043 else if (flags & SO_OBJECTS)
5044 x = count_partial(n, count_inuse);
81819f0f 5045 else
205ab99d 5046 x = n->nr_partial;
81819f0f
CL
5047 total += x;
5048 nodes[node] += x;
5049 }
5050 }
bf16d19a
JP
5051
5052 len += sysfs_emit_at(buf, len, "%lu", total);
81819f0f 5053#ifdef CONFIG_NUMA
bf16d19a 5054 for (node = 0; node < nr_node_ids; node++) {
81819f0f 5055 if (nodes[node])
bf16d19a
JP
5056 len += sysfs_emit_at(buf, len, " N%d=%lu",
5057 node, nodes[node]);
5058 }
81819f0f 5059#endif
bf16d19a 5060 len += sysfs_emit_at(buf, len, "\n");
81819f0f 5061 kfree(nodes);
bf16d19a
JP
5062
5063 return len;
81819f0f
CL
5064}
5065
81819f0f 5066#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
497888cf 5067#define to_slab(n) container_of(n, struct kmem_cache, kobj)
81819f0f
CL
5068
5069struct slab_attribute {
5070 struct attribute attr;
5071 ssize_t (*show)(struct kmem_cache *s, char *buf);
5072 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
5073};
5074
5075#define SLAB_ATTR_RO(_name) \
ab067e99
VK
5076 static struct slab_attribute _name##_attr = \
5077 __ATTR(_name, 0400, _name##_show, NULL)
81819f0f
CL
5078
5079#define SLAB_ATTR(_name) \
5080 static struct slab_attribute _name##_attr = \
ab067e99 5081 __ATTR(_name, 0600, _name##_show, _name##_store)
81819f0f 5082
81819f0f
CL
5083static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
5084{
bf16d19a 5085 return sysfs_emit(buf, "%u\n", s->size);
81819f0f
CL
5086}
5087SLAB_ATTR_RO(slab_size);
5088
5089static ssize_t align_show(struct kmem_cache *s, char *buf)
5090{
bf16d19a 5091 return sysfs_emit(buf, "%u\n", s->align);
81819f0f
CL
5092}
5093SLAB_ATTR_RO(align);
5094
5095static ssize_t object_size_show(struct kmem_cache *s, char *buf)
5096{
bf16d19a 5097 return sysfs_emit(buf, "%u\n", s->object_size);
81819f0f
CL
5098}
5099SLAB_ATTR_RO(object_size);
5100
5101static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
5102{
bf16d19a 5103 return sysfs_emit(buf, "%u\n", oo_objects(s->oo));
81819f0f
CL
5104}
5105SLAB_ATTR_RO(objs_per_slab);
5106
5107static ssize_t order_show(struct kmem_cache *s, char *buf)
5108{
bf16d19a 5109 return sysfs_emit(buf, "%u\n", oo_order(s->oo));
81819f0f 5110}
32a6f409 5111SLAB_ATTR_RO(order);
81819f0f 5112
73d342b1
DR
5113static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
5114{
bf16d19a 5115 return sysfs_emit(buf, "%lu\n", s->min_partial);
73d342b1
DR
5116}
5117
5118static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
5119 size_t length)
5120{
5121 unsigned long min;
5122 int err;
5123
3dbb95f7 5124 err = kstrtoul(buf, 10, &min);
73d342b1
DR
5125 if (err)
5126 return err;
5127
c0bdb232 5128 set_min_partial(s, min);
73d342b1
DR
5129 return length;
5130}
5131SLAB_ATTR(min_partial);
5132
49e22585
CL
5133static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
5134{
bf16d19a 5135 return sysfs_emit(buf, "%u\n", slub_cpu_partial(s));
49e22585
CL
5136}
5137
5138static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
5139 size_t length)
5140{
e5d9998f 5141 unsigned int objects;
49e22585
CL
5142 int err;
5143
e5d9998f 5144 err = kstrtouint(buf, 10, &objects);
49e22585
CL
5145 if (err)
5146 return err;
345c905d 5147 if (objects && !kmem_cache_has_cpu_partial(s))
74ee4ef1 5148 return -EINVAL;
49e22585 5149
e6d0e1dc 5150 slub_set_cpu_partial(s, objects);
49e22585
CL
5151 flush_all(s);
5152 return length;
5153}
5154SLAB_ATTR(cpu_partial);
5155
81819f0f
CL
5156static ssize_t ctor_show(struct kmem_cache *s, char *buf)
5157{
62c70bce
JP
5158 if (!s->ctor)
5159 return 0;
bf16d19a 5160 return sysfs_emit(buf, "%pS\n", s->ctor);
81819f0f
CL
5161}
5162SLAB_ATTR_RO(ctor);
5163
81819f0f
CL
5164static ssize_t aliases_show(struct kmem_cache *s, char *buf)
5165{
bf16d19a 5166 return sysfs_emit(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
81819f0f
CL
5167}
5168SLAB_ATTR_RO(aliases);
5169
81819f0f
CL
5170static ssize_t partial_show(struct kmem_cache *s, char *buf)
5171{
d9acf4b7 5172 return show_slab_objects(s, buf, SO_PARTIAL);
81819f0f
CL
5173}
5174SLAB_ATTR_RO(partial);
5175
5176static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
5177{
d9acf4b7 5178 return show_slab_objects(s, buf, SO_CPU);
81819f0f
CL
5179}
5180SLAB_ATTR_RO(cpu_slabs);
5181
5182static ssize_t objects_show(struct kmem_cache *s, char *buf)
5183{
205ab99d 5184 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
81819f0f
CL
5185}
5186SLAB_ATTR_RO(objects);
5187
205ab99d
CL
5188static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
5189{
5190 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
5191}
5192SLAB_ATTR_RO(objects_partial);
5193
49e22585
CL
5194static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
5195{
5196 int objects = 0;
5197 int pages = 0;
5198 int cpu;
bf16d19a 5199 int len = 0;
49e22585
CL
5200
5201 for_each_online_cpu(cpu) {
a93cf07b
WY
5202 struct page *page;
5203
5204 page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
49e22585
CL
5205
5206 if (page) {
5207 pages += page->pages;
5208 objects += page->pobjects;
5209 }
5210 }
5211
bf16d19a 5212 len += sysfs_emit_at(buf, len, "%d(%d)", objects, pages);
49e22585
CL
5213
5214#ifdef CONFIG_SMP
5215 for_each_online_cpu(cpu) {
a93cf07b
WY
5216 struct page *page;
5217
5218 page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
bf16d19a
JP
5219 if (page)
5220 len += sysfs_emit_at(buf, len, " C%d=%d(%d)",
5221 cpu, page->pobjects, page->pages);
49e22585
CL
5222 }
5223#endif
bf16d19a
JP
5224 len += sysfs_emit_at(buf, len, "\n");
5225
5226 return len;
49e22585
CL
5227}
5228SLAB_ATTR_RO(slabs_cpu_partial);
5229
a5a84755
CL
5230static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
5231{
bf16d19a 5232 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
a5a84755 5233}
8f58119a 5234SLAB_ATTR_RO(reclaim_account);
a5a84755
CL
5235
5236static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
5237{
bf16d19a 5238 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
a5a84755
CL
5239}
5240SLAB_ATTR_RO(hwcache_align);
5241
5242#ifdef CONFIG_ZONE_DMA
5243static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
5244{
bf16d19a 5245 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
a5a84755
CL
5246}
5247SLAB_ATTR_RO(cache_dma);
5248#endif
5249
8eb8284b
DW
5250static ssize_t usersize_show(struct kmem_cache *s, char *buf)
5251{
bf16d19a 5252 return sysfs_emit(buf, "%u\n", s->usersize);
8eb8284b
DW
5253}
5254SLAB_ATTR_RO(usersize);
5255
a5a84755
CL
5256static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
5257{
bf16d19a 5258 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU));
a5a84755
CL
5259}
5260SLAB_ATTR_RO(destroy_by_rcu);
5261
ab4d5ed5 5262#ifdef CONFIG_SLUB_DEBUG
a5a84755
CL
5263static ssize_t slabs_show(struct kmem_cache *s, char *buf)
5264{
5265 return show_slab_objects(s, buf, SO_ALL);
5266}
5267SLAB_ATTR_RO(slabs);
5268
205ab99d
CL
5269static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
5270{
5271 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
5272}
5273SLAB_ATTR_RO(total_objects);
5274
81819f0f
CL
5275static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
5276{
bf16d19a 5277 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS));
81819f0f 5278}
060807f8 5279SLAB_ATTR_RO(sanity_checks);
81819f0f
CL
5280
5281static ssize_t trace_show(struct kmem_cache *s, char *buf)
5282{
bf16d19a 5283 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TRACE));
81819f0f 5284}
060807f8 5285SLAB_ATTR_RO(trace);
81819f0f 5286
81819f0f
CL
5287static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
5288{
bf16d19a 5289 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
81819f0f
CL
5290}
5291
ad38b5b1 5292SLAB_ATTR_RO(red_zone);
81819f0f
CL
5293
5294static ssize_t poison_show(struct kmem_cache *s, char *buf)
5295{
bf16d19a 5296 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_POISON));
81819f0f
CL
5297}
5298
ad38b5b1 5299SLAB_ATTR_RO(poison);
81819f0f
CL
5300
5301static ssize_t store_user_show(struct kmem_cache *s, char *buf)
5302{
bf16d19a 5303 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
81819f0f
CL
5304}
5305
ad38b5b1 5306SLAB_ATTR_RO(store_user);
81819f0f 5307
53e15af0
CL
5308static ssize_t validate_show(struct kmem_cache *s, char *buf)
5309{
5310 return 0;
5311}
5312
5313static ssize_t validate_store(struct kmem_cache *s,
5314 const char *buf, size_t length)
5315{
434e245d
CL
5316 int ret = -EINVAL;
5317
5318 if (buf[0] == '1') {
5319 ret = validate_slab_cache(s);
5320 if (ret >= 0)
5321 ret = length;
5322 }
5323 return ret;
53e15af0
CL
5324}
5325SLAB_ATTR(validate);
a5a84755
CL
5326
5327static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
5328{
5329 if (!(s->flags & SLAB_STORE_USER))
5330 return -ENOSYS;
5331 return list_locations(s, buf, TRACK_ALLOC);
5332}
5333SLAB_ATTR_RO(alloc_calls);
5334
5335static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
5336{
5337 if (!(s->flags & SLAB_STORE_USER))
5338 return -ENOSYS;
5339 return list_locations(s, buf, TRACK_FREE);
5340}
5341SLAB_ATTR_RO(free_calls);
5342#endif /* CONFIG_SLUB_DEBUG */
5343
5344#ifdef CONFIG_FAILSLAB
5345static ssize_t failslab_show(struct kmem_cache *s, char *buf)
5346{
bf16d19a 5347 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
a5a84755 5348}
060807f8 5349SLAB_ATTR_RO(failslab);
ab4d5ed5 5350#endif
53e15af0 5351
2086d26a
CL
5352static ssize_t shrink_show(struct kmem_cache *s, char *buf)
5353{
5354 return 0;
5355}
5356
5357static ssize_t shrink_store(struct kmem_cache *s,
5358 const char *buf, size_t length)
5359{
832f37f5 5360 if (buf[0] == '1')
10befea9 5361 kmem_cache_shrink(s);
832f37f5 5362 else
2086d26a
CL
5363 return -EINVAL;
5364 return length;
5365}
5366SLAB_ATTR(shrink);
5367
81819f0f 5368#ifdef CONFIG_NUMA
9824601e 5369static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
81819f0f 5370{
bf16d19a 5371 return sysfs_emit(buf, "%u\n", s->remote_node_defrag_ratio / 10);
81819f0f
CL
5372}
5373
9824601e 5374static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
81819f0f
CL
5375 const char *buf, size_t length)
5376{
eb7235eb 5377 unsigned int ratio;
0121c619
CL
5378 int err;
5379
eb7235eb 5380 err = kstrtouint(buf, 10, &ratio);
0121c619
CL
5381 if (err)
5382 return err;
eb7235eb
AD
5383 if (ratio > 100)
5384 return -ERANGE;
0121c619 5385
eb7235eb 5386 s->remote_node_defrag_ratio = ratio * 10;
81819f0f 5387
81819f0f
CL
5388 return length;
5389}
9824601e 5390SLAB_ATTR(remote_node_defrag_ratio);
81819f0f
CL
5391#endif
5392
8ff12cfc 5393#ifdef CONFIG_SLUB_STATS
8ff12cfc
CL
5394static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
5395{
5396 unsigned long sum = 0;
5397 int cpu;
bf16d19a 5398 int len = 0;
6da2ec56 5399 int *data = kmalloc_array(nr_cpu_ids, sizeof(int), GFP_KERNEL);
8ff12cfc
CL
5400
5401 if (!data)
5402 return -ENOMEM;
5403
5404 for_each_online_cpu(cpu) {
9dfc6e68 5405 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
8ff12cfc
CL
5406
5407 data[cpu] = x;
5408 sum += x;
5409 }
5410
bf16d19a 5411 len += sysfs_emit_at(buf, len, "%lu", sum);
8ff12cfc 5412
50ef37b9 5413#ifdef CONFIG_SMP
8ff12cfc 5414 for_each_online_cpu(cpu) {
bf16d19a
JP
5415 if (data[cpu])
5416 len += sysfs_emit_at(buf, len, " C%d=%u",
5417 cpu, data[cpu]);
8ff12cfc 5418 }
50ef37b9 5419#endif
8ff12cfc 5420 kfree(data);
bf16d19a
JP
5421 len += sysfs_emit_at(buf, len, "\n");
5422
5423 return len;
8ff12cfc
CL
5424}
5425
78eb00cc
DR
5426static void clear_stat(struct kmem_cache *s, enum stat_item si)
5427{
5428 int cpu;
5429
5430 for_each_online_cpu(cpu)
9dfc6e68 5431 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
78eb00cc
DR
5432}
5433
8ff12cfc
CL
5434#define STAT_ATTR(si, text) \
5435static ssize_t text##_show(struct kmem_cache *s, char *buf) \
5436{ \
5437 return show_stat(s, buf, si); \
5438} \
78eb00cc
DR
5439static ssize_t text##_store(struct kmem_cache *s, \
5440 const char *buf, size_t length) \
5441{ \
5442 if (buf[0] != '0') \
5443 return -EINVAL; \
5444 clear_stat(s, si); \
5445 return length; \
5446} \
5447SLAB_ATTR(text); \
8ff12cfc
CL
5448
5449STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
5450STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
5451STAT_ATTR(FREE_FASTPATH, free_fastpath);
5452STAT_ATTR(FREE_SLOWPATH, free_slowpath);
5453STAT_ATTR(FREE_FROZEN, free_frozen);
5454STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
5455STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
5456STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
5457STAT_ATTR(ALLOC_SLAB, alloc_slab);
5458STAT_ATTR(ALLOC_REFILL, alloc_refill);
e36a2652 5459STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
8ff12cfc
CL
5460STAT_ATTR(FREE_SLAB, free_slab);
5461STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
5462STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
5463STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
5464STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
5465STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
5466STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
03e404af 5467STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
65c3376a 5468STAT_ATTR(ORDER_FALLBACK, order_fallback);
b789ef51
CL
5469STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
5470STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
49e22585
CL
5471STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
5472STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
8028dcea
AS
5473STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
5474STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
6dfd1b65 5475#endif /* CONFIG_SLUB_STATS */
8ff12cfc 5476
06428780 5477static struct attribute *slab_attrs[] = {
81819f0f
CL
5478 &slab_size_attr.attr,
5479 &object_size_attr.attr,
5480 &objs_per_slab_attr.attr,
5481 &order_attr.attr,
73d342b1 5482 &min_partial_attr.attr,
49e22585 5483 &cpu_partial_attr.attr,
81819f0f 5484 &objects_attr.attr,
205ab99d 5485 &objects_partial_attr.attr,
81819f0f
CL
5486 &partial_attr.attr,
5487 &cpu_slabs_attr.attr,
5488 &ctor_attr.attr,
81819f0f
CL
5489 &aliases_attr.attr,
5490 &align_attr.attr,
81819f0f
CL
5491 &hwcache_align_attr.attr,
5492 &reclaim_account_attr.attr,
5493 &destroy_by_rcu_attr.attr,
a5a84755 5494 &shrink_attr.attr,
49e22585 5495 &slabs_cpu_partial_attr.attr,
ab4d5ed5 5496#ifdef CONFIG_SLUB_DEBUG
a5a84755
CL
5497 &total_objects_attr.attr,
5498 &slabs_attr.attr,
5499 &sanity_checks_attr.attr,
5500 &trace_attr.attr,
81819f0f
CL
5501 &red_zone_attr.attr,
5502 &poison_attr.attr,
5503 &store_user_attr.attr,
53e15af0 5504 &validate_attr.attr,
88a420e4
CL
5505 &alloc_calls_attr.attr,
5506 &free_calls_attr.attr,
ab4d5ed5 5507#endif
81819f0f
CL
5508#ifdef CONFIG_ZONE_DMA
5509 &cache_dma_attr.attr,
5510#endif
5511#ifdef CONFIG_NUMA
9824601e 5512 &remote_node_defrag_ratio_attr.attr,
8ff12cfc
CL
5513#endif
5514#ifdef CONFIG_SLUB_STATS
5515 &alloc_fastpath_attr.attr,
5516 &alloc_slowpath_attr.attr,
5517 &free_fastpath_attr.attr,
5518 &free_slowpath_attr.attr,
5519 &free_frozen_attr.attr,
5520 &free_add_partial_attr.attr,
5521 &free_remove_partial_attr.attr,
5522 &alloc_from_partial_attr.attr,
5523 &alloc_slab_attr.attr,
5524 &alloc_refill_attr.attr,
e36a2652 5525 &alloc_node_mismatch_attr.attr,
8ff12cfc
CL
5526 &free_slab_attr.attr,
5527 &cpuslab_flush_attr.attr,
5528 &deactivate_full_attr.attr,
5529 &deactivate_empty_attr.attr,
5530 &deactivate_to_head_attr.attr,
5531 &deactivate_to_tail_attr.attr,
5532 &deactivate_remote_frees_attr.attr,
03e404af 5533 &deactivate_bypass_attr.attr,
65c3376a 5534 &order_fallback_attr.attr,
b789ef51
CL
5535 &cmpxchg_double_fail_attr.attr,
5536 &cmpxchg_double_cpu_fail_attr.attr,
49e22585
CL
5537 &cpu_partial_alloc_attr.attr,
5538 &cpu_partial_free_attr.attr,
8028dcea
AS
5539 &cpu_partial_node_attr.attr,
5540 &cpu_partial_drain_attr.attr,
81819f0f 5541#endif
4c13dd3b
DM
5542#ifdef CONFIG_FAILSLAB
5543 &failslab_attr.attr,
5544#endif
8eb8284b 5545 &usersize_attr.attr,
4c13dd3b 5546
81819f0f
CL
5547 NULL
5548};
5549
1fdaaa23 5550static const struct attribute_group slab_attr_group = {
81819f0f
CL
5551 .attrs = slab_attrs,
5552};
5553
5554static ssize_t slab_attr_show(struct kobject *kobj,
5555 struct attribute *attr,
5556 char *buf)
5557{
5558 struct slab_attribute *attribute;
5559 struct kmem_cache *s;
5560 int err;
5561
5562 attribute = to_slab_attr(attr);
5563 s = to_slab(kobj);
5564
5565 if (!attribute->show)
5566 return -EIO;
5567
5568 err = attribute->show(s, buf);
5569
5570 return err;
5571}
5572
5573static ssize_t slab_attr_store(struct kobject *kobj,
5574 struct attribute *attr,
5575 const char *buf, size_t len)
5576{
5577 struct slab_attribute *attribute;
5578 struct kmem_cache *s;
5579 int err;
5580
5581 attribute = to_slab_attr(attr);
5582 s = to_slab(kobj);
5583
5584 if (!attribute->store)
5585 return -EIO;
5586
5587 err = attribute->store(s, buf, len);
81819f0f
CL
5588 return err;
5589}
5590
41a21285
CL
5591static void kmem_cache_release(struct kobject *k)
5592{
5593 slab_kmem_cache_release(to_slab(k));
5594}
5595
52cf25d0 5596static const struct sysfs_ops slab_sysfs_ops = {
81819f0f
CL
5597 .show = slab_attr_show,
5598 .store = slab_attr_store,
5599};
5600
5601static struct kobj_type slab_ktype = {
5602 .sysfs_ops = &slab_sysfs_ops,
41a21285 5603 .release = kmem_cache_release,
81819f0f
CL
5604};
5605
27c3a314 5606static struct kset *slab_kset;
81819f0f 5607
9a41707b
VD
5608static inline struct kset *cache_kset(struct kmem_cache *s)
5609{
9a41707b
VD
5610 return slab_kset;
5611}
5612
81819f0f
CL
5613#define ID_STR_LENGTH 64
5614
5615/* Create a unique string id for a slab cache:
6446faa2
CL
5616 *
5617 * Format :[flags-]size
81819f0f
CL
5618 */
5619static char *create_unique_id(struct kmem_cache *s)
5620{
5621 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
5622 char *p = name;
5623
5624 BUG_ON(!name);
5625
5626 *p++ = ':';
5627 /*
5628 * First flags affecting slabcache operations. We will only
5629 * get here for aliasable slabs so we do not need to support
5630 * too many flags. The flags here must cover all flags that
5631 * are matched during merging to guarantee that the id is
5632 * unique.
5633 */
5634 if (s->flags & SLAB_CACHE_DMA)
5635 *p++ = 'd';
6d6ea1e9
NB
5636 if (s->flags & SLAB_CACHE_DMA32)
5637 *p++ = 'D';
81819f0f
CL
5638 if (s->flags & SLAB_RECLAIM_ACCOUNT)
5639 *p++ = 'a';
becfda68 5640 if (s->flags & SLAB_CONSISTENCY_CHECKS)
81819f0f 5641 *p++ = 'F';
230e9fc2
VD
5642 if (s->flags & SLAB_ACCOUNT)
5643 *p++ = 'A';
81819f0f
CL
5644 if (p != name + 1)
5645 *p++ = '-';
44065b2e 5646 p += sprintf(p, "%07u", s->size);
2633d7a0 5647
81819f0f
CL
5648 BUG_ON(p > name + ID_STR_LENGTH - 1);
5649 return name;
5650}
5651
5652static int sysfs_slab_add(struct kmem_cache *s)
5653{
5654 int err;
5655 const char *name;
1663f26d 5656 struct kset *kset = cache_kset(s);
45530c44 5657 int unmergeable = slab_unmergeable(s);
81819f0f 5658
1663f26d
TH
5659 if (!kset) {
5660 kobject_init(&s->kobj, &slab_ktype);
5661 return 0;
5662 }
5663
11066386
MC
5664 if (!unmergeable && disable_higher_order_debug &&
5665 (slub_debug & DEBUG_METADATA_FLAGS))
5666 unmergeable = 1;
5667
81819f0f
CL
5668 if (unmergeable) {
5669 /*
5670 * Slabcache can never be merged so we can use the name proper.
5671 * This is typically the case for debug situations. In that
5672 * case we can catch duplicate names easily.
5673 */
27c3a314 5674 sysfs_remove_link(&slab_kset->kobj, s->name);
81819f0f
CL
5675 name = s->name;
5676 } else {
5677 /*
5678 * Create a unique name for the slab as a target
5679 * for the symlinks.
5680 */
5681 name = create_unique_id(s);
5682 }
5683
1663f26d 5684 s->kobj.kset = kset;
26e4f205 5685 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
757fed1d 5686 if (err)
80da026a 5687 goto out;
81819f0f
CL
5688
5689 err = sysfs_create_group(&s->kobj, &slab_attr_group);
54b6a731
DJ
5690 if (err)
5691 goto out_del_kobj;
9a41707b 5692
81819f0f
CL
5693 if (!unmergeable) {
5694 /* Setup first alias */
5695 sysfs_slab_alias(s, s->name);
81819f0f 5696 }
54b6a731
DJ
5697out:
5698 if (!unmergeable)
5699 kfree(name);
5700 return err;
5701out_del_kobj:
5702 kobject_del(&s->kobj);
54b6a731 5703 goto out;
81819f0f
CL
5704}
5705
d50d82fa
MP
5706void sysfs_slab_unlink(struct kmem_cache *s)
5707{
5708 if (slab_state >= FULL)
5709 kobject_del(&s->kobj);
5710}
5711
bf5eb3de
TH
5712void sysfs_slab_release(struct kmem_cache *s)
5713{
5714 if (slab_state >= FULL)
5715 kobject_put(&s->kobj);
81819f0f
CL
5716}
5717
5718/*
5719 * Need to buffer aliases during bootup until sysfs becomes
9f6c708e 5720 * available lest we lose that information.
81819f0f
CL
5721 */
5722struct saved_alias {
5723 struct kmem_cache *s;
5724 const char *name;
5725 struct saved_alias *next;
5726};
5727
5af328a5 5728static struct saved_alias *alias_list;
81819f0f
CL
5729
5730static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
5731{
5732 struct saved_alias *al;
5733
97d06609 5734 if (slab_state == FULL) {
81819f0f
CL
5735 /*
5736 * If we have a leftover link then remove it.
5737 */
27c3a314
GKH
5738 sysfs_remove_link(&slab_kset->kobj, name);
5739 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
81819f0f
CL
5740 }
5741
5742 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
5743 if (!al)
5744 return -ENOMEM;
5745
5746 al->s = s;
5747 al->name = name;
5748 al->next = alias_list;
5749 alias_list = al;
5750 return 0;
5751}
5752
5753static int __init slab_sysfs_init(void)
5754{
5b95a4ac 5755 struct kmem_cache *s;
81819f0f
CL
5756 int err;
5757
18004c5d 5758 mutex_lock(&slab_mutex);
2bce6485 5759
d7660ce5 5760 slab_kset = kset_create_and_add("slab", NULL, kernel_kobj);
27c3a314 5761 if (!slab_kset) {
18004c5d 5762 mutex_unlock(&slab_mutex);
f9f58285 5763 pr_err("Cannot register slab subsystem.\n");
81819f0f
CL
5764 return -ENOSYS;
5765 }
5766
97d06609 5767 slab_state = FULL;
26a7bd03 5768
5b95a4ac 5769 list_for_each_entry(s, &slab_caches, list) {
26a7bd03 5770 err = sysfs_slab_add(s);
5d540fb7 5771 if (err)
f9f58285
FF
5772 pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
5773 s->name);
26a7bd03 5774 }
81819f0f
CL
5775
5776 while (alias_list) {
5777 struct saved_alias *al = alias_list;
5778
5779 alias_list = alias_list->next;
5780 err = sysfs_slab_alias(al->s, al->name);
5d540fb7 5781 if (err)
f9f58285
FF
5782 pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
5783 al->name);
81819f0f
CL
5784 kfree(al);
5785 }
5786
18004c5d 5787 mutex_unlock(&slab_mutex);
81819f0f
CL
5788 return 0;
5789}
5790
5791__initcall(slab_sysfs_init);
ab4d5ed5 5792#endif /* CONFIG_SYSFS */
57ed3eda
PE
5793
5794/*
5795 * The /proc/slabinfo ABI
5796 */
5b365771 5797#ifdef CONFIG_SLUB_DEBUG
0d7561c6 5798void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
57ed3eda 5799{
57ed3eda 5800 unsigned long nr_slabs = 0;
205ab99d
CL
5801 unsigned long nr_objs = 0;
5802 unsigned long nr_free = 0;
57ed3eda 5803 int node;
fa45dc25 5804 struct kmem_cache_node *n;
57ed3eda 5805
fa45dc25 5806 for_each_kmem_cache_node(s, node, n) {
c17fd13e
WL
5807 nr_slabs += node_nr_slabs(n);
5808 nr_objs += node_nr_objs(n);
205ab99d 5809 nr_free += count_partial(n, count_free);
57ed3eda
PE
5810 }
5811
0d7561c6
GC
5812 sinfo->active_objs = nr_objs - nr_free;
5813 sinfo->num_objs = nr_objs;
5814 sinfo->active_slabs = nr_slabs;
5815 sinfo->num_slabs = nr_slabs;
5816 sinfo->objects_per_slab = oo_objects(s->oo);
5817 sinfo->cache_order = oo_order(s->oo);
57ed3eda
PE
5818}
5819
0d7561c6 5820void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
7b3c3a50 5821{
7b3c3a50
AD
5822}
5823
b7454ad3
GC
5824ssize_t slabinfo_write(struct file *file, const char __user *buffer,
5825 size_t count, loff_t *ppos)
7b3c3a50 5826{
b7454ad3 5827 return -EIO;
7b3c3a50 5828}
5b365771 5829#endif /* CONFIG_SLUB_DEBUG */