1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SLUB: A slab allocator with low overhead percpu array caches and mostly
4 * lockless freeing of objects to slabs in the slowpath.
5 *
6 * The allocator synchronizes using spin_trylock for percpu arrays in the
7 * fastpath, and cmpxchg_double (or bit spinlock) for slowpath freeing.
8 * Uses a centralized lock to manage a pool of partial slabs.
9 *
10 * (C) 2007 SGI, Christoph Lameter
11 * (C) 2011 Linux Foundation, Christoph Lameter
12 * (C) 2025 SUSE, Vlastimil Babka
13 */
14
15 #include <linux/mm.h>
16 #include <linux/swap.h> /* mm_account_reclaimed_pages() */
17 #include <linux/module.h>
18 #include <linux/bit_spinlock.h>
19 #include <linux/interrupt.h>
20 #include <linux/swab.h>
21 #include <linux/bitops.h>
22 #include <linux/slab.h>
23 #include "slab.h"
24 #include <linux/vmalloc.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/kasan.h>
28 #include <linux/node.h>
29 #include <linux/kmsan.h>
30 #include <linux/cpu.h>
31 #include <linux/cpuset.h>
32 #include <linux/mempolicy.h>
33 #include <linux/ctype.h>
34 #include <linux/stackdepot.h>
35 #include <linux/debugobjects.h>
36 #include <linux/kallsyms.h>
37 #include <linux/kfence.h>
38 #include <linux/memory.h>
39 #include <linux/math64.h>
40 #include <linux/fault-inject.h>
41 #include <linux/kmemleak.h>
42 #include <linux/stacktrace.h>
43 #include <linux/prefetch.h>
44 #include <linux/memcontrol.h>
45 #include <linux/random.h>
46 #include <linux/prandom.h>
47 #include <kunit/test.h>
48 #include <kunit/test-bug.h>
49 #include <linux/sort.h>
50 #include <linux/irq_work.h>
51 #include <linux/kprobes.h>
52 #include <linux/debugfs.h>
53 #include <trace/events/kmem.h>
54
55 #include "internal.h"
56 #include "page_alloc.h"
57
58 /*
59 * Lock order:
60 * 0. cpu_hotplug_lock
61 * 1. slab_mutex (Global Mutex)
62 * 2a. kmem_cache->cpu_sheaves->lock (Local trylock)
63 * 2b. barn->lock (Spinlock)
64 * 2c. node->list_lock (Spinlock)
65 * 3. slab_lock(slab) (Only on some arches)
66 * 4. object_map_lock (Only for debugging)
67 *
68 * slab_mutex
69 *
70 * The role of the slab_mutex is to protect the list of all the slabs
71 * and to synchronize major metadata changes to slab cache structures.
72 * Also synchronizes memory hotplug callbacks.
73 *
74 * slab_lock
75 *
76 * The slab_lock is a wrapper around the page lock, thus it is a bit
77 * spinlock.
78 *
79 * The slab_lock is only used on arches that do not have the ability
80 * to do a cmpxchg_double. It only protects:
81 *
82 * A. slab->freelist -> List of free objects in a slab
83 * B. slab->inuse -> Number of objects in use
84 * C. slab->objects -> Number of objects in slab
85 * D. slab->frozen -> frozen state
86 *
87 * SL_partial slabs
88 *
89 * Slabs on node partial list have at least one free object. A limited number
90 * of slabs on the list can be fully free (slab->inuse == 0), until we start
91 * discarding them. These slabs are marked with SL_partial, and the flag is
92 * cleared while removing them, usually to grab their freelist afterwards.
93 * This clearing also exempts them from list management. Please see
94 * __slab_free() for more details.
95 *
96 * Full slabs
97 *
98 * For caches without debugging enabled, full slabs (slab->inuse ==
99 * slab->objects and slab->freelist == NULL) are not placed on any list.
100 * The __slab_free() freeing the first object from such a slab will place
101 * it on the partial list. Caches with debugging enabled place such slab
102 * on the full list and use different allocation and freeing paths.
103 *
104 * Frozen slabs
105 *
106 * If a slab is frozen then it is exempt from list management. It is used to
107 * indicate a slab that has failed consistency checks and thus cannot be
108 * allocated from anymore - it is also marked as full. Any previously
109 * allocated objects will be simply leaked upon freeing instead of attempting
110 * to modify the potentially corrupted freelist and metadata.
111 *
112 * To sum up, the current scheme is:
113 * - node partial slab: SL_partial && !full && !frozen
114 * - taken off partial list: !SL_partial && !full && !frozen
115 * - full slab, not on any list: !SL_partial && full && !frozen
116 * - frozen due to inconsistency: !SL_partial && full && frozen
117 *
118 * node->list_lock (spinlock)
119 *
120 * The list_lock protects the partial and full list on each node and
121 * the partial slab counter. If taken then no new slabs may be added or
122 * removed from the lists nor make the number of partial slabs be modified.
123 * (Note that the total number of slabs is an atomic value that may be
124 * modified without taking the list lock).
125 *
126 * The list_lock is a centralized lock and thus we avoid taking it as
127 * much as possible. As long as SLUB does not have to handle partial
128 * slabs, operations can continue without any centralized lock.
129 *
130 * For debug caches, all allocations are forced to go through a list_lock
131 * protected region to serialize against concurrent validation.
132 *
133 * cpu_sheaves->lock (local_trylock)
134 *
135 * This lock protects fastpath operations on the percpu sheaves. On !RT it
136 * only disables preemption and does no atomic operations. As long as the main
137 * or spare sheaf can handle the allocation or free, there is no other
138 * overhead.
139 *
140 * barn->lock (spinlock)
141 *
142 * This lock protects the operations on per-NUMA-node barn. It can quickly
143 * serve an empty or full sheaf if available, and avoid more expensive refill
144 * or flush operation.
145 *
146 * Lockless freeing
147 *
148 * Objects may have to be freed to their slabs when they are from a remote
149 * node (where we want to avoid filling local sheaves with remote objects)
150 * or when there are too many full sheaves. On architectures supporting
151 * cmpxchg_double this is done by a lockless update of slab's freelist and
152 * counters, otherwise slab_lock is taken. This only needs to take the
153 * list_lock if it's a first free to a full slab, or when a slab becomes empty
154 * after the free.
155 *
156 * irq, preemption, migration considerations
157 *
158 * Interrupts are disabled as part of list_lock or barn lock operations, or
159 * around the slab_lock operation, in order to make the slab allocator safe
160 * to use in the context of an irq.
161 * Preemption is disabled as part of local_trylock operations.
162 * kmalloc_nolock() and kfree_nolock() are safe in NMI context but see
163 * their limitations.
164 *
165 * SLUB assigns two object arrays called sheaves for caching allocations and
166 * frees on each cpu, with a NUMA node shared barn for balancing between cpus.
167 * Allocations and frees are primarily served from these sheaves.
168 *
169 * Slabs with free elements are kept on a partial list and during regular
170 * operations no list for full slabs is used. If an object in a full slab is
171 * freed then the slab will show up again on the partial lists.
172 * We track full slabs for debugging purposes though because otherwise we
173 * cannot scan all objects.
174 *
175 * Slabs are freed when they become empty. Teardown and setup is minimal so we
176 * rely on the page allocators per cpu caches for fast frees and allocs.
177 *
178 * SLAB_DEBUG_FLAGS Slab requires special handling due to debug
179 * options set. This moves slab handling out of
180 * the fast path and disables lockless freelists.
181 */
182
183 /**
184 * enum slab_flags - How the slab flags bits are used.
185 * @SL_locked: Is locked with slab_lock()
186 * @SL_partial: On the per-node partial list
187 * @SL_pfmemalloc: Was allocated from PF_MEMALLOC reserves
188 *
189 * The slab flags share space with the page flags but some bits have
190 * different interpretations. The high bits are used for information
191 * like zone/node/section.
192 */
193 enum slab_flags {
194 SL_locked = PG_locked,
195 SL_partial = PG_workingset, /* Historical reasons for this bit */
196 SL_pfmemalloc = PG_active, /* Historical reasons for this bit */
197 };
198
199 #ifndef CONFIG_SLUB_TINY
200 #define __fastpath_inline __always_inline
201 #else
202 #define __fastpath_inline
203 #endif
204
205 #ifdef CONFIG_SLUB_DEBUG
206 #ifdef CONFIG_SLUB_DEBUG_ON
207 DEFINE_STATIC_KEY_TRUE(slub_debug_enabled);
208 #else
209 DEFINE_STATIC_KEY_FALSE(slub_debug_enabled);
210 #endif
211 #endif /* CONFIG_SLUB_DEBUG */
212
213 #ifdef CONFIG_NUMA
214 static DEFINE_STATIC_KEY_FALSE(strict_numa);
215 #endif
216
217 #ifdef CONFIG_MEM_ALLOC_PROFILING
218 DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
219 slab_obj_ext_has_codetag_key);
220 #endif
221
222 /* Structure holding extra parameters for slab allocations */
223 struct slab_alloc_context {
224 unsigned long caller_addr;
225 size_t orig_size;
226 unsigned int alloc_flags;
227 struct list_lru *lru;
228 };
229
230 /* Structure holding parameters for get_partial_node_bulk() */
231 struct partial_bulk_context {
232 gfp_t flags;
233 unsigned int min_objects;
234 unsigned int max_objects;
235 struct list_head slabs;
236 };
237
238 /* Structure used to iterate over objects within a slab */
239 struct slab_obj_iter {
240 unsigned long pos;
241 void *start;
242 #ifdef CONFIG_SLAB_FREELIST_RANDOM
243 unsigned long freelist_count;
244 unsigned long page_limit;
245 bool random;
246 #endif
247 };
248
kmem_cache_debug(struct kmem_cache * s)249 static inline bool kmem_cache_debug(struct kmem_cache *s)
250 {
251 return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
252 }
253
fixup_red_left(struct kmem_cache * s,void * p)254 void *fixup_red_left(struct kmem_cache *s, void *p)
255 {
256 if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
257 p += s->red_left_pad;
258
259 return p;
260 }
261
262 /*
263 * Issues still to be resolved:
264 *
265 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
266 *
267 * - Variable sizing of the per node arrays
268 */
269
270 /* Enable to log cmpxchg failures */
271 #undef SLUB_DEBUG_CMPXCHG
272
273 #ifndef CONFIG_SLUB_TINY
274 /*
275 * Minimum number of partial slabs. These will be left on the partial
276 * lists even if they are empty. kmem_cache_shrink may reclaim them.
277 */
278 #define MIN_PARTIAL 5
279
280 /*
281 * Maximum number of desirable partial slabs.
282 * The existence of more partial slabs makes kmem_cache_shrink
283 * sort the partial list by the number of objects in use.
284 */
285 #define MAX_PARTIAL 10
286 #else
287 #define MIN_PARTIAL 0
288 #define MAX_PARTIAL 0
289 #endif
290
291 #define DEBUG_DEFAULT_FLAGS (SLAB_CONSISTENCY_CHECKS | SLAB_RED_ZONE | \
292 SLAB_POISON | SLAB_STORE_USER)
293
294 /*
295 * These debug flags cannot use CMPXCHG because there might be consistency
296 * issues when checking or reading debug information
297 */
298 #define SLAB_NO_CMPXCHG (SLAB_CONSISTENCY_CHECKS | SLAB_STORE_USER | \
299 SLAB_TRACE)
300
301
302 /*
303 * Debugging flags that require metadata to be stored in the slab. These get
304 * disabled when slab_debug=O is used and a cache's min order increases with
305 * metadata.
306 */
307 #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
308
309 #define OO_SHIFT 16
310 #define OO_MASK ((1 << OO_SHIFT) - 1)
311 #define MAX_OBJS_PER_PAGE 32767 /* since slab.objects is u15 */
312
313 /* Internal SLUB flags */
314 /* Poison object */
315 #define __OBJECT_POISON __SLAB_FLAG_BIT(_SLAB_OBJECT_POISON)
316 /* Use cmpxchg_double */
317
318 #ifdef system_has_freelist_aba
319 #define __CMPXCHG_DOUBLE __SLAB_FLAG_BIT(_SLAB_CMPXCHG_DOUBLE)
320 #else
321 #define __CMPXCHG_DOUBLE __SLAB_FLAG_UNUSED
322 #endif
323
324 /*
325 * Tracking user of a slab.
326 */
327 #define TRACK_ADDRS_COUNT 16
328 struct track {
329 unsigned long addr; /* Called from address */
330 #ifdef CONFIG_STACKDEPOT
331 depot_stack_handle_t handle;
332 #endif
333 int cpu; /* Was running on cpu */
334 int pid; /* Pid context */
335 unsigned long when; /* When did the operation occur */
336 };
337
338 enum track_item { TRACK_ALLOC, TRACK_FREE };
339
340 #ifdef SLAB_SUPPORTS_SYSFS
341 static int sysfs_slab_add(struct kmem_cache *);
342 static int __init slab_kset_init(void);
343 static void __init slab_sysfs_process_aliases(void);
344 #else
sysfs_slab_add(struct kmem_cache * s)345 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
slab_kset_init(void)346 static inline int slab_kset_init(void) { return 0; }
slab_sysfs_process_aliases(void)347 static inline void slab_sysfs_process_aliases(void) { }
348 #endif
349
350 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
351 static void debugfs_slab_add(struct kmem_cache *);
352 static void __init slab_debugfs_root_init(void);
353 #else
debugfs_slab_add(struct kmem_cache * s)354 static inline void debugfs_slab_add(struct kmem_cache *s) { }
slab_debugfs_root_init(void)355 static inline void slab_debugfs_root_init(void) { }
356 #endif
357
358 enum add_mode {
359 ADD_TO_HEAD,
360 ADD_TO_TAIL,
361 };
362
363 enum stat_item {
364 ALLOC_FASTPATH, /* Allocation from percpu sheaves */
365 ALLOC_SLOWPATH, /* Allocation from partial or new slab */
366 FREE_RCU_SHEAF, /* Free to rcu_free sheaf */
367 FREE_RCU_SHEAF_FAIL, /* Failed to free to a rcu_free sheaf */
368 FREE_FASTPATH, /* Free to percpu sheaves */
369 FREE_SLOWPATH, /* Free to a slab */
370 FREE_ADD_PARTIAL, /* Freeing moves slab to partial list */
371 FREE_REMOVE_PARTIAL, /* Freeing removes last object */
372 ALLOC_SLAB, /* New slab acquired from page allocator */
373 ALLOC_NODE_MISMATCH, /* Requested node different from cpu sheaf */
374 FREE_SLAB, /* Slab freed to the page allocator */
375 ORDER_FALLBACK, /* Number of times fallback was necessary */
376 CMPXCHG_DOUBLE_FAIL, /* Failures of slab freelist update */
377 SHEAF_FLUSH, /* Objects flushed from a sheaf */
378 SHEAF_REFILL, /* Objects refilled to a sheaf */
379 SHEAF_ALLOC, /* Allocation of an empty sheaf including oversized ones */
380 SHEAF_FREE, /* Freeing of an empty sheaf including oversized ones */
381 BARN_GET, /* Got full sheaf from barn */
382 BARN_GET_FAIL, /* Failed to get full sheaf from barn */
383 BARN_PUT, /* Put full sheaf to barn */
384 BARN_PUT_FAIL, /* Failed to put full sheaf to barn */
385 SHEAF_PREFILL_FAST, /* Sheaf prefill grabbed the spare sheaf */
386 SHEAF_PREFILL_SLOW, /* Sheaf prefill found no spare sheaf */
387 SHEAF_PREFILL_OVERSIZE, /* Allocation of oversize sheaf for prefill */
388 SHEAF_RETURN_FAST, /* Sheaf return reattached spare sheaf */
389 SHEAF_RETURN_SLOW, /* Sheaf return could not reattach spare */
390 NR_SLUB_STAT_ITEMS
391 };
392
393 #ifdef CONFIG_SLUB_STATS
394 struct kmem_cache_stats {
395 unsigned int stat[NR_SLUB_STAT_ITEMS];
396 };
397 #endif
398
stat(const struct kmem_cache * s,enum stat_item si)399 static inline void stat(const struct kmem_cache *s, enum stat_item si)
400 {
401 #ifdef CONFIG_SLUB_STATS
402 /*
403 * The rmw is racy on a preemptible kernel but this is acceptable, so
404 * avoid this_cpu_add()'s irq-disable overhead.
405 */
406 raw_cpu_inc(s->cpu_stats->stat[si]);
407 #endif
408 }
409
410 static inline
stat_add(const struct kmem_cache * s,enum stat_item si,int v)411 void stat_add(const struct kmem_cache *s, enum stat_item si, int v)
412 {
413 #ifdef CONFIG_SLUB_STATS
414 raw_cpu_add(s->cpu_stats->stat[si], v);
415 #endif
416 }
417
418 #define MAX_FULL_SHEAVES 10
419 #define MAX_EMPTY_SHEAVES 10
420
421 struct node_barn {
422 spinlock_t lock;
423 struct list_head sheaves_full;
424 struct list_head sheaves_empty;
425 unsigned int nr_full;
426 unsigned int nr_empty;
427 };
428
429 struct slab_sheaf {
430 union {
431 struct rcu_head rcu_head;
432 struct list_head barn_list;
433 /* only used to defer call_rcu() in unknown context */
434 struct llist_node llnode;
435 /* only used for prefilled sheafs */
436 struct {
437 unsigned int capacity;
438 bool pfmemalloc;
439 };
440 };
441 struct kmem_cache *cache;
442 unsigned int size;
443 int node; /* only used for rcu_sheaf */
444 void *objects[];
445 };
446
447 struct slub_percpu_sheaves {
448 local_trylock_t lock;
449 struct slab_sheaf *main; /* never NULL when unlocked */
450 struct slab_sheaf *spare; /* empty or full, may be NULL */
451 struct slab_sheaf *rcu_free; /* for batching kfree_rcu() */
452 };
453
454 /*
455 * The slab lists for all objects.
456 */
457 struct kmem_cache_node {
458 spinlock_t list_lock;
459 unsigned long nr_partial;
460 struct list_head partial;
461 #ifdef CONFIG_SLUB_DEBUG
462 atomic_long_t nr_slabs;
463 atomic_long_t total_objects;
464 struct list_head full;
465 #endif
466 };
467
get_node(struct kmem_cache * s,int node)468 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
469 {
470 return s->per_node[node].node;
471 }
472
get_barn_node(struct kmem_cache * s,int node)473 static inline struct node_barn *get_barn_node(struct kmem_cache *s, int node)
474 {
475 return s->per_node[node].barn;
476 }
477
478 /*
479 * Get the barn of the current cpu's NUMA node. It may be a memoryless node.
480 */
get_barn(struct kmem_cache * s)481 static inline struct node_barn *get_barn(struct kmem_cache *s)
482 {
483 return get_barn_node(s, numa_node_id());
484 }
485
486 /*
487 * Iterator over all nodes. The body will be executed for each node that has
488 * a kmem_cache_node structure allocated (which is true for all online nodes)
489 */
490 #define for_each_kmem_cache_node(__s, __node, __n) \
491 for (__node = 0; __node < nr_node_ids; __node++) \
492 if ((__n = get_node(__s, __node)))
493
494 /*
495 * Tracks for which NUMA nodes we have kmem_cache_nodes allocated.
496 * Corresponds to node_state[N_MEMORY], but can temporarily
497 * differ during memory hotplug/hotremove operations.
498 * Protected by slab_mutex.
499 */
500 static nodemask_t slab_nodes;
501
502 /*
503 * Similar to slab_nodes but for where we have node_barn allocated.
504 * Corresponds to N_ONLINE nodes.
505 */
506 static nodemask_t slab_barn_nodes;
507
508 /*
509 * Workqueue used for flushing cpu and kfree_rcu sheaves.
510 */
511 static struct workqueue_struct *flushwq;
512
513 struct slub_flush_work {
514 struct work_struct work;
515 struct kmem_cache *s;
516 bool skip;
517 };
518
519 static DEFINE_MUTEX(flush_lock);
520 static DEFINE_PER_CPU(struct slub_flush_work, slub_flush);
521
522 /********************************************************************
523 * Core slab cache functions
524 *******************************************************************/
525
526 /*
527 * Returns freelist pointer (ptr). With hardening, this is obfuscated
528 * with an XOR of the address where the pointer is held and a per-cache
529 * random number.
530 */
freelist_ptr_encode(const struct kmem_cache * s,void * ptr,unsigned long ptr_addr)531 static inline freeptr_t freelist_ptr_encode(const struct kmem_cache *s,
532 void *ptr, unsigned long ptr_addr)
533 {
534 unsigned long encoded;
535
536 #ifdef CONFIG_SLAB_FREELIST_HARDENED
537 encoded = (unsigned long)ptr ^ s->random ^ swab(ptr_addr);
538 #else
539 encoded = (unsigned long)ptr;
540 #endif
541 return (freeptr_t){.v = encoded};
542 }
543
freelist_ptr_decode(const struct kmem_cache * s,freeptr_t ptr,unsigned long ptr_addr)544 static inline void *freelist_ptr_decode(const struct kmem_cache *s,
545 freeptr_t ptr, unsigned long ptr_addr)
546 {
547 void *decoded;
548
549 #ifdef CONFIG_SLAB_FREELIST_HARDENED
550 decoded = (void *)(ptr.v ^ s->random ^ swab(ptr_addr));
551 #else
552 decoded = (void *)ptr.v;
553 #endif
554 return decoded;
555 }
556
get_freepointer(struct kmem_cache * s,void * object)557 static inline void *get_freepointer(struct kmem_cache *s, void *object)
558 {
559 unsigned long ptr_addr;
560 freeptr_t p;
561
562 object = kasan_reset_tag(object);
563 ptr_addr = (unsigned long)object + s->offset;
564 p = *(freeptr_t *)(ptr_addr);
565 return freelist_ptr_decode(s, p, ptr_addr);
566 }
567
set_freepointer(struct kmem_cache * s,void * object,void * fp)568 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
569 {
570 unsigned long freeptr_addr = (unsigned long)object + s->offset;
571
572 #ifdef CONFIG_SLAB_FREELIST_HARDENED
573 BUG_ON(object == fp); /* naive detection of double free or corruption */
574 #endif
575
576 freeptr_addr = (unsigned long)kasan_reset_tag((void *)freeptr_addr);
577 *(freeptr_t *)freeptr_addr = freelist_ptr_encode(s, fp, freeptr_addr);
578 }
579
580 /*
581 * See comment in calculate_sizes().
582 */
freeptr_outside_object(struct kmem_cache * s)583 static inline bool freeptr_outside_object(struct kmem_cache *s)
584 {
585 return s->offset >= s->inuse;
586 }
587
588 /*
589 * Return offset of the end of info block which is inuse + free pointer if
590 * not overlapping with object.
591 */
get_info_end(struct kmem_cache * s)592 static inline unsigned int get_info_end(struct kmem_cache *s)
593 {
594 if (freeptr_outside_object(s))
595 return s->inuse + sizeof(void *);
596 else
597 return s->inuse;
598 }
599
600 /* Loop over all objects in a slab */
601 #define for_each_object(__p, __s, __addr, __objects) \
602 for (__p = fixup_red_left(__s, __addr); \
603 __p < (__addr) + (__objects) * (__s)->size; \
604 __p += (__s)->size)
605
order_objects(unsigned int order,unsigned int size)606 static inline unsigned int order_objects(unsigned int order, unsigned int size)
607 {
608 return ((unsigned int)PAGE_SIZE << order) / size;
609 }
610
oo_make(unsigned int order,unsigned int size)611 static inline struct kmem_cache_order_objects oo_make(unsigned int order,
612 unsigned int size)
613 {
614 struct kmem_cache_order_objects x = {
615 (order << OO_SHIFT) + order_objects(order, size)
616 };
617
618 return x;
619 }
620
oo_order(struct kmem_cache_order_objects x)621 static inline unsigned int oo_order(struct kmem_cache_order_objects x)
622 {
623 return x.x >> OO_SHIFT;
624 }
625
oo_objects(struct kmem_cache_order_objects x)626 static inline unsigned int oo_objects(struct kmem_cache_order_objects x)
627 {
628 return x.x & OO_MASK;
629 }
630
631 /*
632 * If network-based swap is enabled, slub must keep track of whether memory
633 * were allocated from pfmemalloc reserves.
634 */
slab_test_pfmemalloc(const struct slab * slab)635 static inline bool slab_test_pfmemalloc(const struct slab *slab)
636 {
637 return test_bit(SL_pfmemalloc, &slab->flags.f);
638 }
639
slab_set_pfmemalloc(struct slab * slab)640 static inline void slab_set_pfmemalloc(struct slab *slab)
641 {
642 set_bit(SL_pfmemalloc, &slab->flags.f);
643 }
644
__slab_clear_pfmemalloc(struct slab * slab)645 static inline void __slab_clear_pfmemalloc(struct slab *slab)
646 {
647 __clear_bit(SL_pfmemalloc, &slab->flags.f);
648 }
649
650 /*
651 * Per slab locking using the pagelock
652 */
slab_lock(struct slab * slab)653 static __always_inline void slab_lock(struct slab *slab)
654 {
655 bit_spin_lock(SL_locked, &slab->flags.f);
656 }
657
slab_unlock(struct slab * slab)658 static __always_inline void slab_unlock(struct slab *slab)
659 {
660 bit_spin_unlock(SL_locked, &slab->flags.f);
661 }
662
663 static inline bool
__update_freelist_fast(struct slab * slab,struct freelist_counters * old,struct freelist_counters * new)664 __update_freelist_fast(struct slab *slab, struct freelist_counters *old,
665 struct freelist_counters *new)
666 {
667 #ifdef system_has_freelist_aba
668 return try_cmpxchg_freelist(&slab->freelist_counters,
669 &old->freelist_counters,
670 new->freelist_counters);
671 #else
672 return false;
673 #endif
674 }
675
676 static inline bool
__update_freelist_slow(struct slab * slab,struct freelist_counters * old,struct freelist_counters * new)677 __update_freelist_slow(struct slab *slab, struct freelist_counters *old,
678 struct freelist_counters *new)
679 {
680 bool ret = false;
681
682 slab_lock(slab);
683 if (slab->freelist == old->freelist &&
684 slab->counters == old->counters) {
685 slab->freelist = new->freelist;
686 /* prevent tearing for the read in get_partial_node_bulk() */
687 WRITE_ONCE(slab->counters, new->counters);
688 ret = true;
689 }
690 slab_unlock(slab);
691
692 return ret;
693 }
694
695 /*
696 * Interrupts must be disabled (for the fallback code to work right), typically
697 * by an _irqsave() lock variant. On PREEMPT_RT the preempt_disable(), which is
698 * part of bit_spin_lock(), is sufficient because the policy is not to allow any
699 * allocation/ free operation in hardirq context. Therefore nothing can
700 * interrupt the operation.
701 */
__slab_update_freelist(struct kmem_cache * s,struct slab * slab,struct freelist_counters * old,struct freelist_counters * new,const char * n)702 static inline bool __slab_update_freelist(struct kmem_cache *s, struct slab *slab,
703 struct freelist_counters *old, struct freelist_counters *new, const char *n)
704 {
705 bool ret;
706
707 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
708 lockdep_assert_irqs_disabled();
709
710 if (s->flags & __CMPXCHG_DOUBLE)
711 ret = __update_freelist_fast(slab, old, new);
712 else
713 ret = __update_freelist_slow(slab, old, new);
714
715 if (likely(ret))
716 return true;
717
718 cpu_relax();
719 stat(s, CMPXCHG_DOUBLE_FAIL);
720
721 #ifdef SLUB_DEBUG_CMPXCHG
722 pr_info("%s %s: cmpxchg double redo ", n, s->name);
723 #endif
724
725 return false;
726 }
727
slab_update_freelist(struct kmem_cache * s,struct slab * slab,struct freelist_counters * old,struct freelist_counters * new,const char * n)728 static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab,
729 struct freelist_counters *old, struct freelist_counters *new, const char *n)
730 {
731 bool ret;
732
733 if (s->flags & __CMPXCHG_DOUBLE) {
734 ret = __update_freelist_fast(slab, old, new);
735 } else {
736 unsigned long flags;
737
738 local_irq_save(flags);
739 ret = __update_freelist_slow(slab, old, new);
740 local_irq_restore(flags);
741 }
742 if (likely(ret))
743 return true;
744
745 cpu_relax();
746 stat(s, CMPXCHG_DOUBLE_FAIL);
747
748 #ifdef SLUB_DEBUG_CMPXCHG
749 pr_info("%s %s: cmpxchg double redo ", n, s->name);
750 #endif
751
752 return false;
753 }
754
755 /*
756 * kmalloc caches has fixed sizes (mostly power of 2), and kmalloc() API
757 * family will round up the real request size to these fixed ones, so
758 * there could be an extra area than what is requested. Save the original
759 * request size in the meta data area, for better debug and sanity check.
760 */
set_orig_size(struct kmem_cache * s,void * object,unsigned long orig_size)761 static inline void set_orig_size(struct kmem_cache *s,
762 void *object, unsigned long orig_size)
763 {
764 void *p = kasan_reset_tag(object);
765
766 if (!slub_debug_orig_size(s))
767 return;
768
769 p += get_info_end(s);
770 p += sizeof(struct track) * 2;
771
772 *(unsigned long *)p = orig_size;
773 }
774
get_orig_size(struct kmem_cache * s,void * object)775 static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)
776 {
777 void *p = kasan_reset_tag(object);
778
779 if (is_kfence_address(object))
780 return kfence_ksize(object);
781
782 if (!slub_debug_orig_size(s))
783 return s->object_size;
784
785 p += get_info_end(s);
786 p += sizeof(struct track) * 2;
787
788 return *(unsigned long *)p;
789 }
790
791 #ifdef CONFIG_SLAB_OBJ_EXT
792
793 /*
794 * Check if memory cgroup or memory allocation profiling is enabled.
795 * If enabled, SLUB tries to reduce memory overhead of accounting
796 * slab objects. If neither is enabled when this function is called,
797 * the optimization is simply skipped to avoid affecting caches that do not
798 * need slabobj_ext metadata.
799 *
800 * However, this may disable optimization when memory cgroup or memory
801 * allocation profiling is used, but slabs are created too early
802 * even before those subsystems are initialized.
803 */
need_slab_obj_exts(struct kmem_cache * s)804 static inline bool need_slab_obj_exts(struct kmem_cache *s)
805 {
806 if (s->flags & SLAB_NO_OBJ_EXT)
807 return false;
808
809 if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT))
810 return true;
811
812 if (mem_alloc_profiling_enabled())
813 return true;
814
815 return false;
816 }
817
obj_exts_size_in_slab(struct slab * slab)818 static inline unsigned int obj_exts_size_in_slab(struct slab *slab)
819 {
820 return slab_obj_ext_size(slab) * slab->objects;
821 }
822
obj_exts_offset_in_slab(struct kmem_cache * s,struct slab * slab)823 static inline unsigned long obj_exts_offset_in_slab(struct kmem_cache *s,
824 struct slab *slab)
825 {
826 unsigned long objext_offset;
827
828 objext_offset = s->size * slab->objects;
829 objext_offset = ALIGN(objext_offset, sizeof(struct slabobj_ext));
830 return objext_offset;
831 }
832
obj_exts_fit_within_slab_leftover(struct kmem_cache * s,struct slab * slab)833 static inline bool obj_exts_fit_within_slab_leftover(struct kmem_cache *s,
834 struct slab *slab)
835 {
836 unsigned long objext_offset = obj_exts_offset_in_slab(s, slab);
837 unsigned long objext_size = obj_exts_size_in_slab(slab);
838
839 return objext_offset + objext_size <= slab_size(slab);
840 }
841
obj_exts_in_slab(struct kmem_cache * s,struct slab * slab)842 static inline bool obj_exts_in_slab(struct kmem_cache *s, struct slab *slab)
843 {
844 unsigned long obj_exts;
845 unsigned long start;
846 unsigned long end;
847
848 obj_exts = slab_obj_exts(slab);
849 if (!obj_exts)
850 return false;
851
852 start = (unsigned long)slab_address(slab);
853 end = start + slab_size(slab);
854 return (obj_exts >= start) && (obj_exts < end);
855 }
856 #else
need_slab_obj_exts(struct kmem_cache * s)857 static inline bool need_slab_obj_exts(struct kmem_cache *s)
858 {
859 return false;
860 }
861
obj_exts_size_in_slab(struct slab * slab)862 static inline unsigned int obj_exts_size_in_slab(struct slab *slab)
863 {
864 return 0;
865 }
866
obj_exts_offset_in_slab(struct kmem_cache * s,struct slab * slab)867 static inline unsigned long obj_exts_offset_in_slab(struct kmem_cache *s,
868 struct slab *slab)
869 {
870 return 0;
871 }
872
obj_exts_fit_within_slab_leftover(struct kmem_cache * s,struct slab * slab)873 static inline bool obj_exts_fit_within_slab_leftover(struct kmem_cache *s,
874 struct slab *slab)
875 {
876 return false;
877 }
878
obj_exts_in_slab(struct kmem_cache * s,struct slab * slab)879 static inline bool obj_exts_in_slab(struct kmem_cache *s, struct slab *slab)
880 {
881 return false;
882 }
883
884 #endif
885
886 #if defined(CONFIG_SLAB_OBJ_EXT) && defined(CONFIG_64BIT)
obj_exts_offset_in_object(struct kmem_cache * s)887 static unsigned int obj_exts_offset_in_object(struct kmem_cache *s)
888 {
889 unsigned int offset = get_info_end(s);
890
891 if (kmem_cache_debug_flags(s, SLAB_STORE_USER))
892 offset += sizeof(struct track) * 2;
893
894 if (slub_debug_orig_size(s))
895 offset += sizeof(unsigned long);
896
897 offset += kasan_metadata_size(s, false);
898
899 return offset;
900 }
901
slab_set_obj_exts_in_object(struct slab * slab)902 static inline void slab_set_obj_exts_in_object(struct slab *slab)
903 {
904 slab->obj_exts_in_object = 1;
905 }
906 #else
obj_exts_offset_in_object(struct kmem_cache * s)907 static inline unsigned int obj_exts_offset_in_object(struct kmem_cache *s)
908 {
909 return 0;
910 }
911
slab_set_obj_exts_in_object(struct slab * slab)912 static inline void slab_set_obj_exts_in_object(struct slab *slab)
913 {
914 }
915 #endif
916
917 /*
918 * A no-op function used to attach kprobe handlers in slub_kunit tests.
919 * The barrier is needed to prevent the compiler from optimizing out callsites.
920 */
921 #if defined(CONFIG_DEBUG_VM) || defined(CONFIG_PROVE_LOCKING)
slab_attach_kprobe_locked(void)922 static noinline void slab_attach_kprobe_locked(void)
923 {
924 barrier();
925 }
926 #else
slab_attach_kprobe_locked(void)927 static inline void slab_attach_kprobe_locked(void) { }
928 #endif
929
930 #define slab_lockdep_assert_held(lock) do { \
931 lockdep_assert_held(lock); \
932 slab_attach_kprobe_locked(); \
933 } while (0)
934
935 #ifdef CONFIG_SLUB_DEBUG
936
937 /*
938 * For debugging context when we want to check if the struct slab pointer
939 * appears to be valid.
940 */
validate_slab_ptr(struct slab * slab)941 static inline bool validate_slab_ptr(struct slab *slab)
942 {
943 return PageSlab(slab_page(slab));
944 }
945
946 static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
947 static DEFINE_SPINLOCK(object_map_lock);
948
__fill_map(unsigned long * obj_map,struct kmem_cache * s,struct slab * slab)949 static void __fill_map(unsigned long *obj_map, struct kmem_cache *s,
950 struct slab *slab)
951 {
952 void *addr = slab_address(slab);
953 void *p;
954
955 bitmap_zero(obj_map, slab->objects);
956
957 for (p = slab->freelist; p; p = get_freepointer(s, p))
958 set_bit(__obj_to_index(s, addr, p), obj_map);
959 }
960
961 #if IS_ENABLED(CONFIG_KUNIT)
slab_add_kunit_errors(void)962 static bool slab_add_kunit_errors(void)
963 {
964 struct kunit_resource *resource;
965
966 if (!kunit_get_current_test())
967 return false;
968
969 resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
970 if (!resource)
971 return false;
972
973 (*(int *)resource->data)++;
974 kunit_put_resource(resource);
975 return true;
976 }
977
slab_in_kunit_test(void)978 bool slab_in_kunit_test(void)
979 {
980 struct kunit_resource *resource;
981
982 if (!kunit_get_current_test())
983 return false;
984
985 resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
986 if (!resource)
987 return false;
988
989 kunit_put_resource(resource);
990 return true;
991 }
992 #else
slab_add_kunit_errors(void)993 static inline bool slab_add_kunit_errors(void) { return false; }
994 #endif
995
size_from_object(struct kmem_cache * s)996 static inline unsigned int size_from_object(struct kmem_cache *s)
997 {
998 if (s->flags & SLAB_RED_ZONE)
999 return s->size - s->red_left_pad;
1000
1001 return s->size;
1002 }
1003
restore_red_left(struct kmem_cache * s,void * p)1004 static inline void *restore_red_left(struct kmem_cache *s, void *p)
1005 {
1006 if (s->flags & SLAB_RED_ZONE)
1007 p -= s->red_left_pad;
1008
1009 return p;
1010 }
1011
1012 /*
1013 * Debug settings:
1014 */
1015 #if defined(CONFIG_SLUB_DEBUG_ON)
1016 static slab_flags_t slub_debug = DEBUG_DEFAULT_FLAGS;
1017 #else
1018 static slab_flags_t slub_debug;
1019 #endif
1020
1021 static const char *slub_debug_string __ro_after_init;
1022 static int disable_higher_order_debug;
1023
1024 /*
1025 * Object debugging
1026 */
1027
1028 /* Verify that a pointer has an address that is valid within a slab page */
check_valid_pointer(struct kmem_cache * s,struct slab * slab,void * object)1029 static inline int check_valid_pointer(struct kmem_cache *s,
1030 struct slab *slab, void *object)
1031 {
1032 void *base;
1033
1034 if (!object)
1035 return 1;
1036
1037 base = slab_address(slab);
1038 object = kasan_reset_tag(object);
1039 object = restore_red_left(s, object);
1040 if (object < base || object >= base + slab->objects * s->size ||
1041 (object - base) % s->size) {
1042 return 0;
1043 }
1044
1045 return 1;
1046 }
1047
print_section(char * level,char * text,u8 * addr,unsigned int length)1048 static void print_section(char *level, char *text, u8 *addr,
1049 unsigned int length)
1050 {
1051 metadata_access_enable();
1052 print_hex_dump(level, text, DUMP_PREFIX_ADDRESS,
1053 16, 1, kasan_reset_tag((void *)addr), length, 1);
1054 metadata_access_disable();
1055 }
1056
get_track(struct kmem_cache * s,void * object,enum track_item alloc)1057 static struct track *get_track(struct kmem_cache *s, void *object,
1058 enum track_item alloc)
1059 {
1060 struct track *p;
1061
1062 p = object + get_info_end(s);
1063
1064 return kasan_reset_tag(p + alloc);
1065 }
1066
1067 #ifdef CONFIG_STACKDEPOT
set_track_prepare(gfp_t gfp_flags)1068 static noinline depot_stack_handle_t set_track_prepare(gfp_t gfp_flags)
1069 {
1070 depot_stack_handle_t handle;
1071 unsigned long entries[TRACK_ADDRS_COUNT];
1072 unsigned int nr_entries;
1073
1074 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
1075 handle = stack_depot_save(entries, nr_entries, gfp_flags);
1076
1077 return handle;
1078 }
1079 #else
set_track_prepare(gfp_t gfp_flags)1080 static inline depot_stack_handle_t set_track_prepare(gfp_t gfp_flags)
1081 {
1082 return 0;
1083 }
1084 #endif
1085
set_track_update(struct kmem_cache * s,void * object,enum track_item alloc,unsigned long addr,depot_stack_handle_t handle)1086 static void set_track_update(struct kmem_cache *s, void *object,
1087 enum track_item alloc, unsigned long addr,
1088 depot_stack_handle_t handle)
1089 {
1090 struct track *p = get_track(s, object, alloc);
1091
1092 #ifdef CONFIG_STACKDEPOT
1093 p->handle = handle;
1094 #endif
1095 p->addr = addr;
1096 p->cpu = raw_smp_processor_id();
1097 p->pid = current->pid;
1098 p->when = jiffies;
1099 }
1100
set_track(struct kmem_cache * s,void * object,enum track_item alloc,unsigned long addr,gfp_t gfp_flags)1101 static __always_inline void set_track(struct kmem_cache *s, void *object,
1102 enum track_item alloc, unsigned long addr, gfp_t gfp_flags)
1103 {
1104 depot_stack_handle_t handle = set_track_prepare(gfp_flags);
1105
1106 set_track_update(s, object, alloc, addr, handle);
1107 }
1108
init_tracking(struct kmem_cache * s,void * object)1109 static void init_tracking(struct kmem_cache *s, void *object)
1110 {
1111 struct track *p;
1112
1113 if (!(s->flags & SLAB_STORE_USER))
1114 return;
1115
1116 p = get_track(s, object, TRACK_ALLOC);
1117 memset(p, 0, 2*sizeof(struct track));
1118 }
1119
print_track(const char * s,struct track * t,unsigned long pr_time)1120 static void print_track(const char *s, struct track *t, unsigned long pr_time)
1121 {
1122 depot_stack_handle_t handle __maybe_unused;
1123
1124 if (!t->addr)
1125 return;
1126
1127 pr_err("%s in %pS age=%lu cpu=%u pid=%d\n",
1128 s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid);
1129 #ifdef CONFIG_STACKDEPOT
1130 handle = READ_ONCE(t->handle);
1131 if (handle)
1132 stack_depot_print(handle);
1133 else
1134 pr_err("object allocation/free stack trace missing\n");
1135 #endif
1136 }
1137
print_tracking(struct kmem_cache * s,void * object)1138 void print_tracking(struct kmem_cache *s, void *object)
1139 {
1140 unsigned long pr_time = jiffies;
1141 if (!(s->flags & SLAB_STORE_USER))
1142 return;
1143
1144 print_track("Allocated", get_track(s, object, TRACK_ALLOC), pr_time);
1145 print_track("Freed", get_track(s, object, TRACK_FREE), pr_time);
1146 }
1147
print_slab_info(const struct slab * slab)1148 static void print_slab_info(const struct slab *slab)
1149 {
1150 pr_err("Slab 0x%p objects=%u used=%u fp=0x%p flags=%pGp\n",
1151 slab, slab->objects, slab->inuse, slab->freelist,
1152 &slab->flags.f);
1153 }
1154
skip_orig_size_check(struct kmem_cache * s,const void * object)1155 void skip_orig_size_check(struct kmem_cache *s, const void *object)
1156 {
1157 set_orig_size(s, (void *)object, s->object_size);
1158 }
1159
__slab_bug(struct kmem_cache * s,const char * fmt,va_list argsp)1160 static void __slab_bug(struct kmem_cache *s, const char *fmt, va_list argsp)
1161 {
1162 struct va_format vaf;
1163 va_list args;
1164
1165 va_copy(args, argsp);
1166 vaf.fmt = fmt;
1167 vaf.va = &args;
1168 pr_err("=============================================================================\n");
1169 pr_err("BUG %s (%s): %pV\n", s ? s->name : "<unknown>", print_tainted(), &vaf);
1170 pr_err("-----------------------------------------------------------------------------\n\n");
1171 va_end(args);
1172 }
1173
slab_bug(struct kmem_cache * s,const char * fmt,...)1174 static void slab_bug(struct kmem_cache *s, const char *fmt, ...)
1175 {
1176 va_list args;
1177
1178 va_start(args, fmt);
1179 __slab_bug(s, fmt, args);
1180 va_end(args);
1181 }
1182
1183 __printf(2, 3)
slab_fix(struct kmem_cache * s,const char * fmt,...)1184 static void slab_fix(struct kmem_cache *s, const char *fmt, ...)
1185 {
1186 struct va_format vaf;
1187 va_list args;
1188
1189 if (slab_add_kunit_errors())
1190 return;
1191
1192 va_start(args, fmt);
1193 vaf.fmt = fmt;
1194 vaf.va = &args;
1195 pr_err("FIX %s: %pV\n", s->name, &vaf);
1196 va_end(args);
1197 }
1198
print_trailer(struct kmem_cache * s,struct slab * slab,u8 * p)1199 static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p)
1200 {
1201 unsigned int off; /* Offset of last byte */
1202 u8 *addr = slab_address(slab);
1203
1204 print_tracking(s, p);
1205
1206 print_slab_info(slab);
1207
1208 pr_err("Object 0x%p @offset=%tu fp=0x%p\n\n",
1209 p, p - addr, get_freepointer(s, p));
1210
1211 if (s->flags & SLAB_RED_ZONE)
1212 print_section(KERN_ERR, "Redzone ", p - s->red_left_pad,
1213 s->red_left_pad);
1214 else if (p > addr + 16)
1215 print_section(KERN_ERR, "Bytes b4 ", p - 16, 16);
1216
1217 print_section(KERN_ERR, "Object ", p,
1218 min_t(unsigned int, s->object_size, PAGE_SIZE));
1219 if (s->flags & SLAB_RED_ZONE)
1220 print_section(KERN_ERR, "Redzone ", p + s->object_size,
1221 s->inuse - s->object_size);
1222
1223 off = get_info_end(s);
1224
1225 if (s->flags & SLAB_STORE_USER)
1226 off += 2 * sizeof(struct track);
1227
1228 if (slub_debug_orig_size(s))
1229 off += sizeof(unsigned long);
1230
1231 off += kasan_metadata_size(s, false);
1232
1233 if (obj_exts_in_object(slab))
1234 off += slab_obj_ext_size(slab);
1235
1236 if (off != size_from_object(s))
1237 /* Beginning of the filler is the free pointer */
1238 print_section(KERN_ERR, "Padding ", p + off,
1239 size_from_object(s) - off);
1240 }
1241
object_err(struct kmem_cache * s,struct slab * slab,u8 * object,const char * reason)1242 static void object_err(struct kmem_cache *s, struct slab *slab,
1243 u8 *object, const char *reason)
1244 {
1245 if (slab_add_kunit_errors())
1246 return;
1247
1248 slab_bug(s, reason);
1249 if (!object || !check_valid_pointer(s, slab, object)) {
1250 print_slab_info(slab);
1251 pr_err("Invalid pointer 0x%p\n", object);
1252 } else {
1253 print_trailer(s, slab, object);
1254 }
1255 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1256
1257 WARN_ON(1);
1258 }
1259
__slab_err(struct slab * slab)1260 static void __slab_err(struct slab *slab)
1261 {
1262 if (slab_in_kunit_test())
1263 return;
1264
1265 print_slab_info(slab);
1266 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1267
1268 WARN_ON(1);
1269 }
1270
slab_err(struct kmem_cache * s,struct slab * slab,const char * fmt,...)1271 static __printf(3, 4) void slab_err(struct kmem_cache *s, struct slab *slab,
1272 const char *fmt, ...)
1273 {
1274 va_list args;
1275
1276 if (slab_add_kunit_errors())
1277 return;
1278
1279 va_start(args, fmt);
1280 __slab_bug(s, fmt, args);
1281 va_end(args);
1282
1283 __slab_err(slab);
1284 }
1285
init_object(struct kmem_cache * s,void * object,u8 val)1286 static void init_object(struct kmem_cache *s, void *object, u8 val)
1287 {
1288 u8 *p = kasan_reset_tag(object);
1289 unsigned int poison_size = s->object_size;
1290
1291 if (s->flags & SLAB_RED_ZONE) {
1292 /*
1293 * Here and below, avoid overwriting the KMSAN shadow. Keeping
1294 * the shadow makes it possible to distinguish uninit-value
1295 * from use-after-free.
1296 */
1297 memset_no_sanitize_memory(p - s->red_left_pad, val,
1298 s->red_left_pad);
1299
1300 if (slub_debug_orig_size(s) && val == SLUB_RED_ACTIVE) {
1301 /*
1302 * Redzone the extra allocated space by kmalloc than
1303 * requested, and the poison size will be limited to
1304 * the original request size accordingly.
1305 */
1306 poison_size = get_orig_size(s, object);
1307 }
1308 }
1309
1310 if (s->flags & __OBJECT_POISON) {
1311 memset_no_sanitize_memory(p, POISON_FREE, poison_size - 1);
1312 memset_no_sanitize_memory(p + poison_size - 1, POISON_END, 1);
1313 }
1314
1315 if (s->flags & SLAB_RED_ZONE)
1316 memset_no_sanitize_memory(p + poison_size, val,
1317 s->inuse - poison_size);
1318 }
1319
restore_bytes(struct kmem_cache * s,const char * message,u8 data,void * from,void * to)1320 static void restore_bytes(struct kmem_cache *s, const char *message, u8 data,
1321 void *from, void *to)
1322 {
1323 slab_fix(s, "Restoring %s 0x%p-0x%p=0x%x", message, from, to - 1, data);
1324 memset(from, data, to - from);
1325 }
1326
1327 #ifdef CONFIG_KMSAN
1328 #define pad_check_attributes noinline __no_kmsan_checks
1329 #else
1330 #define pad_check_attributes
1331 #endif
1332
1333 static pad_check_attributes int
check_bytes_and_report(struct kmem_cache * s,struct slab * slab,u8 * object,const char * what,u8 * start,unsigned int value,unsigned int bytes,bool slab_obj_print)1334 check_bytes_and_report(struct kmem_cache *s, struct slab *slab,
1335 u8 *object, const char *what, u8 *start, unsigned int value,
1336 unsigned int bytes, bool slab_obj_print)
1337 {
1338 u8 *fault;
1339 u8 *end;
1340 u8 *addr = slab_address(slab);
1341
1342 metadata_access_enable();
1343 fault = memchr_inv(kasan_reset_tag(start), value, bytes);
1344 metadata_access_disable();
1345 if (!fault)
1346 return 1;
1347
1348 end = start + bytes;
1349 while (end > fault && end[-1] == value)
1350 end--;
1351
1352 if (slab_add_kunit_errors())
1353 goto skip_bug_print;
1354
1355 pr_err("[%s overwritten] 0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n",
1356 what, fault, end - 1, fault - addr, fault[0], value);
1357
1358 if (slab_obj_print)
1359 object_err(s, slab, object, "Object corrupt");
1360
1361 skip_bug_print:
1362 restore_bytes(s, what, value, fault, end);
1363 return 0;
1364 }
1365
1366 /*
1367 * Object field layout:
1368 *
1369 * [Left redzone padding] (if SLAB_RED_ZONE)
1370 * - Field size: s->red_left_pad
1371 * - Immediately precedes each object when SLAB_RED_ZONE is set.
1372 * - Filled with 0xbb (SLUB_RED_INACTIVE) for inactive objects and
1373 * 0xcc (SLUB_RED_ACTIVE) for objects in use when SLAB_RED_ZONE.
1374 *
1375 * [Object bytes] (object address starts here)
1376 * - Field size: s->object_size
1377 * - Object payload bytes.
1378 * - If the freepointer may overlap the object, it is stored inside
1379 * the object (typically near the middle).
1380 * - Poisoning uses 0x6b (POISON_FREE) and the last byte is
1381 * 0xa5 (POISON_END) when __OBJECT_POISON is enabled.
1382 *
1383 * [Word-align padding] (right redzone when SLAB_RED_ZONE is set)
1384 * - Field size: s->inuse - s->object_size
1385 * - If redzoning is enabled and ALIGN(size, sizeof(void *)) adds no
1386 * padding, explicitly extend by one word so the right redzone is
1387 * non-empty.
1388 * - Filled with 0xbb (SLUB_RED_INACTIVE) for inactive objects and
1389 * 0xcc (SLUB_RED_ACTIVE) for objects in use when SLAB_RED_ZONE.
1390 *
1391 * [Metadata starts at object + s->inuse]
1392 * - A. freelist pointer (if freeptr_outside_object)
1393 * - B. alloc tracking (SLAB_STORE_USER)
1394 * - C. free tracking (SLAB_STORE_USER)
1395 * - D. original request size (SLAB_KMALLOC && SLAB_STORE_USER)
1396 * - E. KASAN metadata (if enabled)
1397 *
1398 * [Mandatory padding] (if CONFIG_SLUB_DEBUG && SLAB_RED_ZONE)
1399 * - One mandatory debug word to guarantee a minimum poisoned gap
1400 * between metadata and the next object, independent of alignment.
1401 * - Filled with 0x5a (POISON_INUSE) when SLAB_POISON is set.
1402 * [Final alignment padding]
1403 * - Bytes added by ALIGN(size, s->align) to reach s->size.
1404 * - When the padding is large enough, it can be used to store
1405 * struct slabobj_ext for accounting metadata (obj_exts_in_object()).
1406 * - The remaining bytes (if any) are filled with 0x5a (POISON_INUSE)
1407 * when SLAB_POISON is set.
1408 *
1409 * Notes:
1410 * - Redzones are filled by init_object() with SLUB_RED_ACTIVE/INACTIVE.
1411 * - Object contents are poisoned with POISON_FREE/END when __OBJECT_POISON.
1412 * - The trailing padding is pre-filled with POISON_INUSE by
1413 * setup_slab_debug() when SLAB_POISON is set, and is validated by
1414 * check_pad_bytes().
1415 * - The first object pointer is slab_address(slab) +
1416 * (s->red_left_pad if redzoning); subsequent objects are reached by
1417 * adding s->size each time.
1418 *
1419 * If a slab cache flag relies on specific metadata to exist at a fixed
1420 * offset, the flag must be included in SLAB_NEVER_MERGE to prevent merging.
1421 * Otherwise, the cache would misbehave as s->object_size and s->inuse are
1422 * adjusted during cache merging (see __kmem_cache_alias()).
1423 */
check_pad_bytes(struct kmem_cache * s,struct slab * slab,u8 * p)1424 static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p)
1425 {
1426 unsigned long off = get_info_end(s); /* The end of info */
1427
1428 if (s->flags & SLAB_STORE_USER) {
1429 /* We also have user information there */
1430 off += 2 * sizeof(struct track);
1431
1432 if (s->flags & SLAB_KMALLOC)
1433 off += sizeof(unsigned long);
1434 }
1435
1436 off += kasan_metadata_size(s, false);
1437
1438 if (obj_exts_in_object(slab))
1439 off += slab_obj_ext_size(slab);
1440
1441 if (size_from_object(s) == off)
1442 return 1;
1443
1444 return check_bytes_and_report(s, slab, p, "Object padding",
1445 p + off, POISON_INUSE, size_from_object(s) - off, true);
1446 }
1447
1448 /* Check the pad bytes at the end of a slab page */
1449 static pad_check_attributes void
slab_pad_check(struct kmem_cache * s,struct slab * slab)1450 slab_pad_check(struct kmem_cache *s, struct slab *slab)
1451 {
1452 u8 *start;
1453 u8 *fault;
1454 u8 *end;
1455 u8 *pad;
1456 int length;
1457 int remainder;
1458
1459 if (!(s->flags & SLAB_POISON))
1460 return;
1461
1462 start = slab_address(slab);
1463 length = slab_size(slab);
1464 end = start + length;
1465
1466 if (obj_exts_in_slab(s, slab) && !obj_exts_in_object(slab)) {
1467 remainder = length;
1468 remainder -= obj_exts_offset_in_slab(s, slab);
1469 remainder -= obj_exts_size_in_slab(slab);
1470 } else {
1471 remainder = length % s->size;
1472 }
1473
1474 if (!remainder)
1475 return;
1476
1477 pad = end - remainder;
1478 metadata_access_enable();
1479 fault = memchr_inv(kasan_reset_tag(pad), POISON_INUSE, remainder);
1480 metadata_access_disable();
1481 if (!fault)
1482 return;
1483 while (end > fault && end[-1] == POISON_INUSE)
1484 end--;
1485
1486 slab_bug(s, "Padding overwritten. 0x%p-0x%p @offset=%tu",
1487 fault, end - 1, fault - start);
1488 print_section(KERN_ERR, "Padding ", pad, remainder);
1489 __slab_err(slab);
1490
1491 restore_bytes(s, "slab padding", POISON_INUSE, fault, end);
1492 }
1493
check_object(struct kmem_cache * s,struct slab * slab,void * object,u8 val)1494 static int check_object(struct kmem_cache *s, struct slab *slab,
1495 void *object, u8 val)
1496 {
1497 u8 *p = object;
1498 u8 *endobject = object + s->object_size;
1499 unsigned int orig_size, kasan_meta_size;
1500 int ret = 1;
1501
1502 if (s->flags & SLAB_RED_ZONE) {
1503 if (!check_bytes_and_report(s, slab, object, "Left Redzone",
1504 object - s->red_left_pad, val, s->red_left_pad, ret))
1505 ret = 0;
1506
1507 if (!check_bytes_and_report(s, slab, object, "Right Redzone",
1508 endobject, val, s->inuse - s->object_size, ret))
1509 ret = 0;
1510
1511 if (slub_debug_orig_size(s) && val == SLUB_RED_ACTIVE) {
1512 orig_size = get_orig_size(s, object);
1513
1514 if (s->object_size > orig_size &&
1515 !check_bytes_and_report(s, slab, object,
1516 "kmalloc Redzone", p + orig_size,
1517 val, s->object_size - orig_size, ret)) {
1518 ret = 0;
1519 }
1520 }
1521 } else {
1522 if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
1523 if (!check_bytes_and_report(s, slab, p, "Alignment padding",
1524 endobject, POISON_INUSE,
1525 s->inuse - s->object_size, ret))
1526 ret = 0;
1527 }
1528 }
1529
1530 if (s->flags & SLAB_POISON) {
1531 if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON)) {
1532 /*
1533 * KASAN can save its free meta data inside of the
1534 * object at offset 0. Thus, skip checking the part of
1535 * the redzone that overlaps with the meta data.
1536 */
1537 kasan_meta_size = kasan_metadata_size(s, true);
1538 if (kasan_meta_size < s->object_size - 1 &&
1539 !check_bytes_and_report(s, slab, p, "Poison",
1540 p + kasan_meta_size, POISON_FREE,
1541 s->object_size - kasan_meta_size - 1, ret))
1542 ret = 0;
1543 if (kasan_meta_size < s->object_size &&
1544 !check_bytes_and_report(s, slab, p, "End Poison",
1545 p + s->object_size - 1, POISON_END, 1, ret))
1546 ret = 0;
1547 }
1548 /*
1549 * check_pad_bytes cleans up on its own.
1550 */
1551 if (!check_pad_bytes(s, slab, p))
1552 ret = 0;
1553 }
1554
1555 /*
1556 * Cannot check freepointer while object is allocated if
1557 * object and freepointer overlap.
1558 */
1559 if ((freeptr_outside_object(s) || val != SLUB_RED_ACTIVE) &&
1560 !check_valid_pointer(s, slab, get_freepointer(s, p))) {
1561 object_err(s, slab, p, "Freepointer corrupt");
1562 /*
1563 * No choice but to zap it and thus lose the remainder
1564 * of the free objects in this slab. May cause
1565 * another error because the object count is now wrong.
1566 */
1567 set_freepointer(s, p, NULL);
1568 ret = 0;
1569 }
1570
1571 return ret;
1572 }
1573
1574 /*
1575 * Checks if the slab state looks sane. Assumes the struct slab pointer
1576 * was either obtained in a way that ensures it's valid, or validated
1577 * by validate_slab_ptr()
1578 */
check_slab(struct kmem_cache * s,struct slab * slab)1579 static int check_slab(struct kmem_cache *s, struct slab *slab)
1580 {
1581 int maxobj;
1582
1583 maxobj = order_objects(slab_order(slab), s->size);
1584 if (slab->objects > maxobj) {
1585 slab_err(s, slab, "objects %u > max %u",
1586 slab->objects, maxobj);
1587 return 0;
1588 }
1589 if (slab->inuse > slab->objects) {
1590 slab_err(s, slab, "inuse %u > max %u",
1591 slab->inuse, slab->objects);
1592 return 0;
1593 }
1594 if (slab->frozen) {
1595 slab_err(s, slab, "Slab disabled since SLUB metadata consistency check failed");
1596 return 0;
1597 }
1598
1599 /* Slab_pad_check fixes things up after itself */
1600 slab_pad_check(s, slab);
1601 return 1;
1602 }
1603
1604 /*
1605 * Determine if a certain object in a slab is on the freelist. Must hold the
1606 * slab lock to guarantee that the chains are in a consistent state.
1607 */
on_freelist(struct kmem_cache * s,struct slab * slab,void * search)1608 static bool on_freelist(struct kmem_cache *s, struct slab *slab, void *search)
1609 {
1610 int nr = 0;
1611 void *fp;
1612 void *object = NULL;
1613 int max_objects;
1614
1615 fp = slab->freelist;
1616 while (fp && nr <= slab->objects) {
1617 if (fp == search)
1618 return true;
1619 if (!check_valid_pointer(s, slab, fp)) {
1620 if (object) {
1621 object_err(s, slab, object,
1622 "Freechain corrupt");
1623 set_freepointer(s, object, NULL);
1624 break;
1625 } else {
1626 slab_err(s, slab, "Freepointer corrupt");
1627 slab->freelist = NULL;
1628 slab->inuse = slab->objects;
1629 slab_fix(s, "Freelist cleared");
1630 return false;
1631 }
1632 }
1633 object = fp;
1634 fp = get_freepointer(s, object);
1635 nr++;
1636 }
1637
1638 if (nr > slab->objects) {
1639 slab_err(s, slab, "Freelist cycle detected");
1640 slab->freelist = NULL;
1641 slab->inuse = slab->objects;
1642 slab_fix(s, "Freelist cleared");
1643 return false;
1644 }
1645
1646 max_objects = order_objects(slab_order(slab), s->size);
1647 if (max_objects > MAX_OBJS_PER_PAGE)
1648 max_objects = MAX_OBJS_PER_PAGE;
1649
1650 if (slab->objects != max_objects) {
1651 slab_err(s, slab, "Wrong number of objects. Found %d but should be %d",
1652 slab->objects, max_objects);
1653 slab->objects = max_objects;
1654 slab_fix(s, "Number of objects adjusted");
1655 }
1656 if (slab->inuse != slab->objects - nr) {
1657 slab_err(s, slab, "Wrong object count. Counter is %d but counted were %d",
1658 slab->inuse, slab->objects - nr);
1659 slab->inuse = slab->objects - nr;
1660 slab_fix(s, "Object count adjusted");
1661 }
1662 return search == NULL;
1663 }
1664
trace(struct kmem_cache * s,struct slab * slab,void * object,int alloc)1665 static void trace(struct kmem_cache *s, struct slab *slab, void *object,
1666 int alloc)
1667 {
1668 if (s->flags & SLAB_TRACE) {
1669 pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
1670 s->name,
1671 alloc ? "alloc" : "free",
1672 object, slab->inuse,
1673 slab->freelist);
1674
1675 if (!alloc)
1676 print_section(KERN_INFO, "Object ", (void *)object,
1677 s->object_size);
1678
1679 dump_stack();
1680 }
1681 }
1682
1683 /*
1684 * Tracking of fully allocated slabs for debugging purposes.
1685 */
add_full(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab)1686 static void add_full(struct kmem_cache *s,
1687 struct kmem_cache_node *n, struct slab *slab)
1688 {
1689 if (!(s->flags & SLAB_STORE_USER))
1690 return;
1691
1692 slab_lockdep_assert_held(&n->list_lock);
1693 list_add(&slab->slab_list, &n->full);
1694 }
1695
remove_full(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab)1696 static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct slab *slab)
1697 {
1698 if (!(s->flags & SLAB_STORE_USER))
1699 return;
1700
1701 slab_lockdep_assert_held(&n->list_lock);
1702 list_del(&slab->slab_list);
1703 }
1704
node_nr_slabs(struct kmem_cache_node * n)1705 static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
1706 {
1707 return atomic_long_read(&n->nr_slabs);
1708 }
1709
inc_slabs_node(struct kmem_cache * s,int node,int objects)1710 static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
1711 {
1712 struct kmem_cache_node *n = get_node(s, node);
1713
1714 atomic_long_inc(&n->nr_slabs);
1715 atomic_long_add(objects, &n->total_objects);
1716 }
dec_slabs_node(struct kmem_cache * s,int node,int objects)1717 static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
1718 {
1719 struct kmem_cache_node *n = get_node(s, node);
1720
1721 atomic_long_dec(&n->nr_slabs);
1722 atomic_long_sub(objects, &n->total_objects);
1723 }
1724
1725 /* Object debug checks for alloc/free paths */
setup_object_debug(struct kmem_cache * s,void * object)1726 static void setup_object_debug(struct kmem_cache *s, void *object)
1727 {
1728 if (!kmem_cache_debug_flags(s, SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON))
1729 return;
1730
1731 init_object(s, object, SLUB_RED_INACTIVE);
1732 init_tracking(s, object);
1733 }
1734
1735 static
setup_slab_debug(struct kmem_cache * s,struct slab * slab,void * addr)1736 void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr)
1737 {
1738 if (!kmem_cache_debug_flags(s, SLAB_POISON))
1739 return;
1740
1741 metadata_access_enable();
1742 memset(kasan_reset_tag(addr), POISON_INUSE, slab_size(slab));
1743 metadata_access_disable();
1744 }
1745
alloc_consistency_checks(struct kmem_cache * s,struct slab * slab,void * object)1746 static inline int alloc_consistency_checks(struct kmem_cache *s,
1747 struct slab *slab, void *object)
1748 {
1749 if (!check_slab(s, slab))
1750 return 0;
1751
1752 if (!check_valid_pointer(s, slab, object)) {
1753 object_err(s, slab, object, "Freelist Pointer check fails");
1754 return 0;
1755 }
1756
1757 if (!check_object(s, slab, object, SLUB_RED_INACTIVE))
1758 return 0;
1759
1760 return 1;
1761 }
1762
alloc_debug_processing(struct kmem_cache * s,struct slab * slab,void * object,int orig_size)1763 static noinline bool alloc_debug_processing(struct kmem_cache *s,
1764 struct slab *slab, void *object, int orig_size)
1765 {
1766 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
1767 if (!alloc_consistency_checks(s, slab, object))
1768 goto bad;
1769 }
1770
1771 /* Success. Perform special debug activities for allocs */
1772 trace(s, slab, object, 1);
1773 set_orig_size(s, object, orig_size);
1774 init_object(s, object, SLUB_RED_ACTIVE);
1775 return true;
1776
1777 bad:
1778 /*
1779 * Let's do the best we can to avoid issues in the future. Marking all
1780 * objects as used avoids touching the remaining objects.
1781 */
1782 slab_fix(s, "Marking all objects used");
1783 slab->inuse = slab->objects;
1784 slab->freelist = NULL;
1785 slab->frozen = 1; /* mark consistency-failed slab as frozen */
1786
1787 return false;
1788 }
1789
free_consistency_checks(struct kmem_cache * s,struct slab * slab,void * object,unsigned long addr)1790 static inline int free_consistency_checks(struct kmem_cache *s,
1791 struct slab *slab, void *object, unsigned long addr)
1792 {
1793 if (!check_valid_pointer(s, slab, object)) {
1794 slab_err(s, slab, "Invalid object pointer 0x%p", object);
1795 return 0;
1796 }
1797
1798 if (on_freelist(s, slab, object)) {
1799 object_err(s, slab, object, "Object already free");
1800 return 0;
1801 }
1802
1803 if (!check_object(s, slab, object, SLUB_RED_ACTIVE))
1804 return 0;
1805
1806 if (unlikely(s != slab->slab_cache)) {
1807 if (!slab->slab_cache) {
1808 slab_err(NULL, slab, "No slab cache for object 0x%p",
1809 object);
1810 } else {
1811 object_err(s, slab, object,
1812 "page slab pointer corrupt.");
1813 }
1814 return 0;
1815 }
1816 return 1;
1817 }
1818
1819 /*
1820 * Parse a block of slab_debug options. Blocks are delimited by ';'
1821 *
1822 * @str: start of block
1823 * @flags: returns parsed flags, or DEBUG_DEFAULT_FLAGS if none specified
1824 * @slabs: return start of list of slabs, or NULL when there's no list
1825 * @init: assume this is initial parsing and not per-kmem-create parsing
1826 *
1827 * returns the start of next block if there's any, or NULL
1828 */
1829 static const char *
parse_slub_debug_flags(const char * str,slab_flags_t * flags,const char ** slabs,bool init)1830 parse_slub_debug_flags(const char *str, slab_flags_t *flags, const char **slabs, bool init)
1831 {
1832 bool higher_order_disable = false;
1833
1834 /* Skip any completely empty blocks */
1835 while (*str && *str == ';')
1836 str++;
1837
1838 if (*str == ',') {
1839 /*
1840 * No options but restriction on slabs. This means full
1841 * debugging for slabs matching a pattern.
1842 */
1843 *flags = DEBUG_DEFAULT_FLAGS;
1844 goto check_slabs;
1845 }
1846 *flags = 0;
1847
1848 /* Determine which debug features should be switched on */
1849 for (; *str && *str != ',' && *str != ';'; str++) {
1850 switch (tolower(*str)) {
1851 case '-':
1852 *flags = 0;
1853 break;
1854 case 'f':
1855 *flags |= SLAB_CONSISTENCY_CHECKS;
1856 break;
1857 case 'z':
1858 *flags |= SLAB_RED_ZONE;
1859 break;
1860 case 'p':
1861 *flags |= SLAB_POISON;
1862 break;
1863 case 'u':
1864 *flags |= SLAB_STORE_USER;
1865 break;
1866 case 't':
1867 *flags |= SLAB_TRACE;
1868 break;
1869 case 'a':
1870 *flags |= SLAB_FAILSLAB;
1871 break;
1872 case 'o':
1873 /*
1874 * Avoid enabling debugging on caches if its minimum
1875 * order would increase as a result.
1876 */
1877 higher_order_disable = true;
1878 break;
1879 default:
1880 if (init)
1881 pr_err("slab_debug option '%c' unknown. skipped\n", *str);
1882 }
1883 }
1884 check_slabs:
1885 if (*str == ',')
1886 *slabs = ++str;
1887 else
1888 *slabs = NULL;
1889
1890 /* Skip over the slab list */
1891 while (*str && *str != ';')
1892 str++;
1893
1894 /* Skip any completely empty blocks */
1895 while (*str && *str == ';')
1896 str++;
1897
1898 if (init && higher_order_disable)
1899 disable_higher_order_debug = 1;
1900
1901 if (*str)
1902 return str;
1903 else
1904 return NULL;
1905 }
1906
setup_slub_debug(const char * str,const struct kernel_param * kp)1907 static int __init setup_slub_debug(const char *str, const struct kernel_param *kp)
1908 {
1909 slab_flags_t flags;
1910 slab_flags_t global_flags;
1911 const char *saved_str;
1912 const char *slab_list;
1913 bool global_slub_debug_changed = false;
1914 bool slab_list_specified = false;
1915
1916 global_flags = DEBUG_DEFAULT_FLAGS;
1917 if (!str || !*str)
1918 /*
1919 * No options specified. Switch on full debugging.
1920 */
1921 goto out;
1922
1923 saved_str = str;
1924 while (str) {
1925 str = parse_slub_debug_flags(str, &flags, &slab_list, true);
1926
1927 if (!slab_list) {
1928 global_flags = flags;
1929 global_slub_debug_changed = true;
1930 } else {
1931 slab_list_specified = true;
1932 if (flags & SLAB_STORE_USER)
1933 stack_depot_request_early_init();
1934 }
1935 }
1936
1937 /*
1938 * For backwards compatibility, a single list of flags with list of
1939 * slabs means debugging is only changed for those slabs, so the global
1940 * slab_debug should be unchanged (0 or DEBUG_DEFAULT_FLAGS, depending
1941 * on CONFIG_SLUB_DEBUG_ON). We can extended that to multiple lists as
1942 * long as there is no option specifying flags without a slab list.
1943 */
1944 if (slab_list_specified) {
1945 if (!global_slub_debug_changed)
1946 global_flags = slub_debug;
1947 slub_debug_string = saved_str;
1948 }
1949 out:
1950 slub_debug = global_flags;
1951 if (slub_debug & SLAB_STORE_USER)
1952 stack_depot_request_early_init();
1953 if (slub_debug != 0 || slub_debug_string)
1954 static_branch_enable(&slub_debug_enabled);
1955 else
1956 static_branch_disable(&slub_debug_enabled);
1957 if ((static_branch_unlikely(&init_on_alloc) ||
1958 static_branch_unlikely(&init_on_free)) &&
1959 (slub_debug & SLAB_POISON))
1960 pr_info("mem auto-init: SLAB_POISON will take precedence over init_on_alloc/init_on_free\n");
1961 return 0;
1962 }
1963
1964 static const struct kernel_param_ops param_ops_slab_debug __initconst = {
1965 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1966 .set = setup_slub_debug,
1967 };
1968 __core_param_cb(slab_debug, ¶m_ops_slab_debug, NULL, 0);
1969 __core_param_cb(slub_debug, ¶m_ops_slab_debug, NULL, 0);
1970
1971 /*
1972 * kmem_cache_flags - apply debugging options to the cache
1973 * @flags: flags to set
1974 * @name: name of the cache
1975 *
1976 * Debug option(s) are applied to @flags. In addition to the debug
1977 * option(s), if a slab name (or multiple) is specified i.e.
1978 * slab_debug=<Debug-Options>,<slab name1>,<slab name2> ...
1979 * then only the select slabs will receive the debug option(s).
1980 */
kmem_cache_flags(slab_flags_t flags,const char * name)1981 slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name)
1982 {
1983 const char *iter;
1984 size_t len;
1985 const char *next_block;
1986 slab_flags_t block_flags;
1987 slab_flags_t slub_debug_local = slub_debug;
1988
1989 if (flags & SLAB_NO_USER_FLAGS)
1990 return flags;
1991
1992 /*
1993 * If the slab cache is for debugging (e.g. kmemleak) then
1994 * don't store user (stack trace) information by default,
1995 * but let the user enable it via the command line below.
1996 */
1997 if (flags & SLAB_NOLEAKTRACE)
1998 slub_debug_local &= ~SLAB_STORE_USER;
1999
2000 len = strlen(name);
2001 next_block = slub_debug_string;
2002 /* Go through all blocks of debug options, see if any matches our slab's name */
2003 while (next_block) {
2004 next_block = parse_slub_debug_flags(next_block, &block_flags, &iter, false);
2005 if (!iter)
2006 continue;
2007 /* Found a block that has a slab list, search it */
2008 while (*iter) {
2009 const char *end, *glob;
2010 size_t cmplen;
2011
2012 end = strchrnul(iter, ',');
2013 if (next_block && next_block < end)
2014 end = next_block - 1;
2015
2016 glob = strnchr(iter, end - iter, '*');
2017 if (glob)
2018 cmplen = glob - iter;
2019 else
2020 cmplen = max_t(size_t, len, (end - iter));
2021
2022 if (!strncmp(name, iter, cmplen)) {
2023 flags |= block_flags;
2024 return flags;
2025 }
2026
2027 if (!*end || *end == ';')
2028 break;
2029 iter = end + 1;
2030 }
2031 }
2032
2033 return flags | slub_debug_local;
2034 }
2035 #else /* !CONFIG_SLUB_DEBUG */
setup_object_debug(struct kmem_cache * s,void * object)2036 static inline void setup_object_debug(struct kmem_cache *s, void *object) {}
2037 static inline
setup_slab_debug(struct kmem_cache * s,struct slab * slab,void * addr)2038 void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {}
2039
alloc_debug_processing(struct kmem_cache * s,struct slab * slab,void * object,int orig_size)2040 static inline bool alloc_debug_processing(struct kmem_cache *s,
2041 struct slab *slab, void *object, int orig_size) { return true; }
2042
free_debug_processing(struct kmem_cache * s,struct slab * slab,void * head,void * tail,int * bulk_cnt,unsigned long addr,depot_stack_handle_t handle)2043 static inline bool free_debug_processing(struct kmem_cache *s,
2044 struct slab *slab, void *head, void *tail, int *bulk_cnt,
2045 unsigned long addr, depot_stack_handle_t handle) { return true; }
2046
slab_pad_check(struct kmem_cache * s,struct slab * slab)2047 static inline void slab_pad_check(struct kmem_cache *s, struct slab *slab) {}
check_object(struct kmem_cache * s,struct slab * slab,void * object,u8 val)2048 static inline int check_object(struct kmem_cache *s, struct slab *slab,
2049 void *object, u8 val) { return 1; }
set_track_prepare(gfp_t gfp_flags)2050 static inline depot_stack_handle_t set_track_prepare(gfp_t gfp_flags) { return 0; }
set_track(struct kmem_cache * s,void * object,enum track_item alloc,unsigned long addr,gfp_t gfp_flags)2051 static inline void set_track(struct kmem_cache *s, void *object,
2052 enum track_item alloc, unsigned long addr, gfp_t gfp_flags) {}
add_full(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab)2053 static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
2054 struct slab *slab) {}
remove_full(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab)2055 static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
2056 struct slab *slab) {}
kmem_cache_flags(slab_flags_t flags,const char * name)2057 slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name)
2058 {
2059 return flags;
2060 }
2061 #define slub_debug 0
2062
2063 #define disable_higher_order_debug 0
2064
node_nr_slabs(struct kmem_cache_node * n)2065 static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
2066 { return 0; }
inc_slabs_node(struct kmem_cache * s,int node,int objects)2067 static inline void inc_slabs_node(struct kmem_cache *s, int node,
2068 int objects) {}
dec_slabs_node(struct kmem_cache * s,int node,int objects)2069 static inline void dec_slabs_node(struct kmem_cache *s, int node,
2070 int objects) {}
2071 #endif /* CONFIG_SLUB_DEBUG */
2072
2073 /*
2074 * The allocated objcg pointers array or sheaf is not accounted directly.
2075 * Moreover, it should not come from DMA buffer and is not readily
2076 * reclaimable. Node restriction for the parent allocation also should
2077 * not apply to the slab's internal objects, as well as __GFP_COMP used
2078 * for new slab allocations.
2079 * So those GFP bits should be masked off.
2080 */
2081 #define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | \
2082 __GFP_ACCOUNT | __GFP_NOFAIL | \
2083 __GFP_THISNODE | __GFP_COMP)
2084
2085 #ifdef CONFIG_SLAB_OBJ_EXT
2086
2087 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
2088
mark_obj_codetag_empty(const void * obj)2089 static inline void mark_obj_codetag_empty(const void *obj)
2090 {
2091 struct slab *obj_slab;
2092 unsigned long slab_exts;
2093
2094 if (!slab_obj_ext_has_codetag())
2095 return;
2096
2097 obj_slab = virt_to_slab(obj);
2098 slab_exts = slab_obj_exts(obj_slab);
2099 if (slab_exts) {
2100 struct slabobj_ext *ext;
2101 union codetag_ref *ref;
2102
2103 get_slab_obj_exts(slab_exts);
2104 ext = slab_obj_ext(obj_slab->slab_cache, obj_slab, slab_exts, obj);
2105 ref = slab_obj_ext_codetag_ref(obj_slab, ext);
2106
2107 if (unlikely(is_codetag_empty(ref))) {
2108 put_slab_obj_exts(slab_exts);
2109 return;
2110 }
2111
2112 /* codetag should be NULL here */
2113 WARN_ON(ref->ct);
2114 set_codetag_empty(ref);
2115 put_slab_obj_exts(slab_exts);
2116 }
2117 }
2118
mark_failed_objexts_alloc(struct slab * slab)2119 static inline bool mark_failed_objexts_alloc(struct slab *slab)
2120 {
2121 return cmpxchg(&slab->obj_exts, 0, OBJEXTS_ALLOC_FAIL) == 0;
2122 }
2123
handle_failed_objexts_alloc(struct slab * slab,unsigned long obj_exts,struct slabobj_ext * vec)2124 static inline void handle_failed_objexts_alloc(struct slab *slab,
2125 unsigned long obj_exts, struct slabobj_ext *vec)
2126 {
2127 unsigned int stride;
2128
2129 if (!slab_obj_ext_has_codetag())
2130 return;
2131
2132 /*
2133 * If vector previously failed to allocate then we have live
2134 * objects with no tag reference. Mark all references in this
2135 * vector as empty to avoid warnings later on.
2136 */
2137 if (obj_exts != OBJEXTS_ALLOC_FAIL)
2138 return;
2139
2140 stride = slab_obj_ext_size(slab) / sizeof(*vec);
2141
2142 for (unsigned int i = 0; i < slab->objects; i++) {
2143 union codetag_ref *ref = slab_obj_ext_codetag_ref(slab, vec);
2144
2145 set_codetag_empty(ref);
2146 vec += stride;
2147 }
2148 }
2149
2150 #else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
2151
mark_obj_codetag_empty(const void * obj)2152 static inline void mark_obj_codetag_empty(const void *obj) {}
mark_failed_objexts_alloc(struct slab * slab)2153 static inline bool mark_failed_objexts_alloc(struct slab *slab) { return false; }
handle_failed_objexts_alloc(struct slab * slab,unsigned long obj_exts,struct slabobj_ext * vec)2154 static inline void handle_failed_objexts_alloc(struct slab *slab,
2155 unsigned long obj_exts, struct slabobj_ext *vec) {}
2156
2157 #endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
2158
init_slab_obj_exts(struct slab * slab)2159 static inline void init_slab_obj_exts(struct slab *slab)
2160 {
2161 slab->obj_exts = 0;
2162 }
2163
alloc_slab_obj_exts(struct slab * slab,struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags)2164 int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
2165 gfp_t gfp, unsigned int alloc_flags)
2166 {
2167 const bool allow_spin = alloc_flags_allow_spinning(alloc_flags);
2168 bool new_slab = alloc_flags & SLAB_ALLOC_NEW_SLAB;
2169 unsigned long new_exts;
2170 unsigned long old_exts;
2171 struct slabobj_ext *vec;
2172 size_t sz = slab_obj_ext_size(slab) * slab->objects;
2173
2174 gfp &= ~OBJCGS_CLEAR_MASK;
2175 /*
2176 * In most cases, obj_exts arrays are allocated from normal kmalloc.
2177 * However, normal kmalloc caches must allocate them from
2178 * KMALLOC_NO_OBJ_EXT caches to prevent recursion.
2179 */
2180 if (is_kmalloc_normal(s))
2181 alloc_flags |= SLAB_ALLOC_NO_OBJ_EXT;
2182
2183 alloc_flags &= ~SLAB_ALLOC_NEW_SLAB;
2184
2185 /* This will use kmalloc_nolock() if alloc_flags say so */
2186 vec = kmalloc_flags(sz, gfp | __GFP_ZERO, alloc_flags, slab_nid(slab));
2187
2188 if (!vec) {
2189 /*
2190 * Try to mark vectors which failed to allocate.
2191 * If this operation fails, there may be a racing process
2192 * that has already completed the allocation.
2193 */
2194 if (!mark_failed_objexts_alloc(slab) &&
2195 slab_obj_exts(slab))
2196 return 0;
2197
2198 return -ENOMEM;
2199 }
2200
2201 if (IS_ENABLED(CONFIG_DEBUG_VM)) {
2202 struct kmem_cache *exts_cache;
2203 struct slab *exts_slab;
2204
2205 exts_slab = virt_to_slab(vec);
2206 if (exts_slab) {
2207 /*
2208 * The vector must be allocated from either normal or
2209 * KMALLOC_NO_OBJ_EXT kmalloc caches to avoid cycles.
2210 */
2211 exts_cache = exts_slab->slab_cache;
2212 WARN_ON_ONCE(!is_kmalloc_normal(exts_cache) &&
2213 !(exts_cache->flags & SLAB_NO_OBJ_EXT));
2214 }
2215 }
2216
2217 new_exts = (unsigned long)vec;
2218 #ifdef CONFIG_MEMCG
2219 new_exts |= MEMCG_DATA_OBJEXTS;
2220 #endif
2221 retry:
2222 old_exts = READ_ONCE(slab->obj_exts);
2223 handle_failed_objexts_alloc(slab, old_exts, vec);
2224
2225 if (new_slab) {
2226 /*
2227 * If the slab is brand new and nobody can yet access its
2228 * obj_exts, no synchronization is required and obj_exts can
2229 * be simply assigned.
2230 */
2231 slab->obj_exts = new_exts;
2232 } else if (old_exts & ~OBJEXTS_FLAGS_MASK) {
2233 /*
2234 * If the slab is already in use, somebody can allocate and
2235 * assign slabobj_exts in parallel. In this case the existing
2236 * objcg vector should be reused.
2237 */
2238 if (unlikely(!allow_spin))
2239 kfree_nolock(vec);
2240 else
2241 kfree(vec);
2242 return 0;
2243 } else if (cmpxchg(&slab->obj_exts, old_exts, new_exts) != old_exts) {
2244 /* Retry if a racing thread changed slab->obj_exts from under us. */
2245 goto retry;
2246 }
2247
2248 if (allow_spin)
2249 kmemleak_not_leak(vec);
2250 return 0;
2251 }
2252
free_slab_obj_exts(struct slab * slab,bool allow_spin)2253 static inline void free_slab_obj_exts(struct slab *slab, bool allow_spin)
2254 {
2255 struct slabobj_ext *obj_exts;
2256
2257 obj_exts = (struct slabobj_ext *)slab_obj_exts(slab);
2258 if (!obj_exts) {
2259 /*
2260 * If obj_exts allocation failed, slab->obj_exts is set to
2261 * OBJEXTS_ALLOC_FAIL. In this case, we end up here and should
2262 * clear the flag.
2263 */
2264 slab->obj_exts = 0;
2265 return;
2266 }
2267
2268 if (obj_exts_in_slab(slab->slab_cache, slab)) {
2269 slab->obj_exts = 0;
2270 return;
2271 }
2272
2273 if (allow_spin)
2274 kfree(obj_exts);
2275 else
2276 kfree_nolock(obj_exts);
2277 slab->obj_exts = 0;
2278 }
2279
2280 /*
2281 * Try to allocate slabobj_ext array from unused space.
2282 * This function must be called on a freshly allocated slab to prevent
2283 * concurrency problems.
2284 */
alloc_slab_obj_exts_early(struct kmem_cache * s,struct slab * slab)2285 static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab)
2286 {
2287 void *addr;
2288 unsigned long obj_exts;
2289
2290 if (!need_slab_obj_exts(s))
2291 return;
2292
2293 if (obj_exts_fit_within_slab_leftover(s, slab)) {
2294 addr = slab_address(slab) + obj_exts_offset_in_slab(s, slab);
2295 addr = kasan_reset_tag(addr);
2296 obj_exts = (unsigned long)addr;
2297
2298 get_slab_obj_exts(obj_exts);
2299 memset(addr, 0, obj_exts_size_in_slab(slab));
2300 put_slab_obj_exts(obj_exts);
2301
2302 #ifdef CONFIG_MEMCG
2303 obj_exts |= MEMCG_DATA_OBJEXTS;
2304 #endif
2305 slab->obj_exts = obj_exts;
2306 } else if (s->flags & SLAB_OBJ_EXT_IN_OBJ) {
2307 unsigned int offset = obj_exts_offset_in_object(s);
2308
2309 obj_exts = (unsigned long)slab_address(slab);
2310 obj_exts += s->red_left_pad;
2311 obj_exts += offset;
2312
2313 get_slab_obj_exts(obj_exts);
2314 for_each_object(addr, s, slab_address(slab), slab->objects)
2315 memset(kasan_reset_tag(addr) + offset, 0, slab_obj_ext_size(slab));
2316 put_slab_obj_exts(obj_exts);
2317
2318 #ifdef CONFIG_MEMCG
2319 obj_exts |= MEMCG_DATA_OBJEXTS;
2320 #endif
2321 slab->obj_exts = obj_exts;
2322 slab_set_obj_exts_in_object(slab);
2323 }
2324 }
2325
2326 #else /* CONFIG_SLAB_OBJ_EXT */
2327
mark_obj_codetag_empty(const void * obj)2328 static inline void mark_obj_codetag_empty(const void *obj)
2329 {
2330 }
2331
init_slab_obj_exts(struct slab * slab)2332 static inline void init_slab_obj_exts(struct slab *slab)
2333 {
2334 }
2335
alloc_slab_obj_exts(struct slab * slab,struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags)2336 static int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
2337 gfp_t gfp, unsigned int alloc_flags)
2338 {
2339 return 0;
2340 }
2341
free_slab_obj_exts(struct slab * slab,bool allow_spin)2342 static inline void free_slab_obj_exts(struct slab *slab, bool allow_spin)
2343 {
2344 }
2345
alloc_slab_obj_exts_early(struct kmem_cache * s,struct slab * slab)2346 static inline void alloc_slab_obj_exts_early(struct kmem_cache *s,
2347 struct slab *slab)
2348 {
2349 }
2350
2351 #endif /* CONFIG_SLAB_OBJ_EXT */
2352
2353 #ifdef CONFIG_MEM_ALLOC_PROFILING
2354
2355 static inline unsigned long
prepare_slab_obj_exts_hook(struct kmem_cache * s,struct slab * slab,gfp_t flags,unsigned int alloc_flags,void * p)2356 prepare_slab_obj_exts_hook(struct kmem_cache *s, struct slab *slab,
2357 gfp_t flags, unsigned int alloc_flags, void *p)
2358 {
2359 if (!slab_obj_exts(slab)) {
2360 if (is_kfence_address(p))
2361 return 0;
2362
2363 if (alloc_slab_obj_exts(slab, s, flags, alloc_flags)) {
2364 pr_warn_once("%s, %s: Failed to create slab extension vector!\n",
2365 __func__, s->name);
2366 return 0;
2367 }
2368 }
2369
2370 return slab_obj_exts(slab);
2371 }
2372
2373
2374 /* Should be called only if mem_alloc_profiling_enabled() */
2375 static noinline void
__alloc_tagging_slab_alloc_hook(struct kmem_cache * s,void * object,gfp_t flags,unsigned int alloc_flags)2376 __alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags,
2377 unsigned int alloc_flags)
2378 {
2379 unsigned long obj_exts;
2380 struct slabobj_ext *obj_ext;
2381 struct slab *slab;
2382
2383 if (!object)
2384 return;
2385
2386 if (s->flags & (SLAB_NO_OBJ_EXT | SLAB_NOLEAKTRACE))
2387 return;
2388
2389 if (alloc_flags & SLAB_ALLOC_NO_RECURSE)
2390 return;
2391
2392 slab = virt_to_slab(object);
2393 obj_exts = prepare_slab_obj_exts_hook(s, slab, flags, alloc_flags, object);
2394 /*
2395 * Currently obj_exts is used only for allocation profiling.
2396 * If other users appear then mem_alloc_profiling_enabled()
2397 * check should be added before alloc_tag_add().
2398 */
2399 if (obj_exts) {
2400 union codetag_ref *ref;
2401
2402 get_slab_obj_exts(obj_exts);
2403
2404 obj_ext = slab_obj_ext(s, slab, obj_exts, object);
2405 ref = slab_obj_ext_codetag_ref(slab, obj_ext);
2406
2407 alloc_tag_add(ref, current->alloc_tag, s->size);
2408
2409 put_slab_obj_exts(obj_exts);
2410 } else {
2411 /*
2412 * KFENCE allocations are rare and the amount of outstanding
2413 * ones is limited to a small number so it's not worth setting
2414 * tags as inaccurate because of them.
2415 */
2416 if (!is_kfence_address(object))
2417 alloc_tag_set_inaccurate(current->alloc_tag);
2418 }
2419 }
2420
2421 static inline void
alloc_tagging_slab_alloc_hook(struct kmem_cache * s,void * object,gfp_t flags,unsigned int alloc_flags)2422 alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags,
2423 unsigned int alloc_flags)
2424 {
2425 if (mem_alloc_profiling_enabled())
2426 __alloc_tagging_slab_alloc_hook(s, object, flags, alloc_flags);
2427 }
2428
2429 /* Should be called only if mem_alloc_profiling_enabled() */
2430 static noinline void
__alloc_tagging_slab_free_hook(struct kmem_cache * s,struct slab * slab,void ** p,int objects)2431 __alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,
2432 int objects)
2433 {
2434 unsigned long obj_exts;
2435
2436 /* slab->obj_exts might not be NULL if it was created for MEMCG accounting. */
2437 if (s->flags & (SLAB_NO_OBJ_EXT | SLAB_NOLEAKTRACE))
2438 return;
2439
2440 obj_exts = slab_obj_exts(slab);
2441 if (!obj_exts)
2442 return;
2443
2444 get_slab_obj_exts(obj_exts);
2445 for (int i = 0; i < objects; i++) {
2446 struct slabobj_ext *ext;
2447
2448 ext = slab_obj_ext(s, slab, obj_exts, p[i]);
2449 alloc_tag_sub(slab_obj_ext_codetag_ref(slab, ext), s->size);
2450 }
2451 put_slab_obj_exts(obj_exts);
2452 }
2453
2454 static inline void
alloc_tagging_slab_free_hook(struct kmem_cache * s,struct slab * slab,void ** p,int objects)2455 alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,
2456 int objects)
2457 {
2458 if (mem_alloc_profiling_enabled())
2459 __alloc_tagging_slab_free_hook(s, slab, p, objects);
2460 }
2461
2462 /*
2463 * Make sure the static key used by slab_obj_ext_has_codetag() reflects the
2464 * value of !mem_alloc_profiling_permanently_disabled()
2465 *
2466 * Any later mem alloc profiling shutdown won't be reflected in the static key
2467 * because obj_exts with codetags might already exist.
2468 */
slab_obj_ext_has_codetag_init(void)2469 static void __init slab_obj_ext_has_codetag_init(void)
2470 {
2471 bool need_codetag = !mem_alloc_profiling_permanently_disabled();
2472
2473 if (need_codetag != static_key_enabled(&slab_obj_ext_has_codetag_key)) {
2474 if (need_codetag)
2475 static_branch_enable(&slab_obj_ext_has_codetag_key);
2476 else
2477 static_branch_disable(&slab_obj_ext_has_codetag_key);
2478 }
2479 }
2480
2481 #else /* CONFIG_MEM_ALLOC_PROFILING */
2482
2483 static inline void
alloc_tagging_slab_alloc_hook(struct kmem_cache * s,void * object,gfp_t flags,unsigned int alloc_flags)2484 alloc_tagging_slab_alloc_hook(struct kmem_cache *s, void *object, gfp_t flags,
2485 unsigned int alloc_flags)
2486 {
2487 }
2488
2489 static inline void
alloc_tagging_slab_free_hook(struct kmem_cache * s,struct slab * slab,void ** p,int objects)2490 alloc_tagging_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,
2491 int objects)
2492 {
2493 }
2494
slab_obj_ext_has_codetag_init(void)2495 static inline void slab_obj_ext_has_codetag_init(void)
2496 {
2497 }
2498
2499 #endif /* CONFIG_MEM_ALLOC_PROFILING */
2500
2501
2502 #ifdef CONFIG_MEMCG
2503
2504 static void memcg_alloc_abort_single(struct kmem_cache *s, void *object);
2505
2506 static __fastpath_inline
memcg_slab_post_alloc_hook(struct kmem_cache * s,gfp_t flags,size_t size,void ** p,const struct slab_alloc_context * ac)2507 bool memcg_slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
2508 size_t size, void **p,
2509 const struct slab_alloc_context *ac)
2510 {
2511 if (likely(!memcg_kmem_online()))
2512 return true;
2513
2514 if (likely(!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT)))
2515 return true;
2516
2517 if (likely(__memcg_slab_post_alloc_hook(s, ac->lru, flags,
2518 ac->alloc_flags, size, p)))
2519 return true;
2520
2521 if (likely(size == 1)) {
2522 memcg_alloc_abort_single(s, *p);
2523 *p = NULL;
2524 } else {
2525 kmem_cache_free_bulk(s, size, p);
2526 }
2527
2528 return false;
2529 }
2530
2531 static __fastpath_inline
memcg_slab_free_hook(struct kmem_cache * s,struct slab * slab,void ** p,int objects)2532 void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p,
2533 int objects)
2534 {
2535 unsigned long obj_exts;
2536
2537 if (!memcg_kmem_online())
2538 return;
2539
2540 obj_exts = slab_obj_exts(slab);
2541 if (likely(!obj_exts))
2542 return;
2543
2544 if (!slab_needs_objcg(slab))
2545 return;
2546
2547 get_slab_obj_exts(obj_exts);
2548 __memcg_slab_free_hook(s, slab, p, objects, obj_exts);
2549 put_slab_obj_exts(obj_exts);
2550 }
2551
2552 static __fastpath_inline
memcg_slab_post_charge(void * p,gfp_t flags)2553 bool memcg_slab_post_charge(void *p, gfp_t flags)
2554 {
2555 unsigned long obj_exts;
2556 struct slabobj_ext *obj_ext;
2557 struct kmem_cache *s;
2558 struct page *page;
2559 struct slab *slab;
2560
2561 page = virt_to_page(p);
2562 if (PageLargeKmalloc(page)) {
2563 unsigned int order;
2564 int size;
2565
2566 if (PageMemcgKmem(page))
2567 return true;
2568
2569 order = large_kmalloc_order(page);
2570 if (__memcg_kmem_charge_page(page, flags, order))
2571 return false;
2572
2573 /*
2574 * This page has already been accounted in the global stats but
2575 * not in the memcg stats. So, subtract from the global and use
2576 * the interface which adds to both global and memcg stats.
2577 */
2578 size = PAGE_SIZE << order;
2579 mod_node_page_state(page_pgdat(page), NR_SLAB_UNRECLAIMABLE_B, -size);
2580 mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B, size);
2581 return true;
2582 }
2583
2584 slab = page_slab(page);
2585 s = slab->slab_cache;
2586
2587 /*
2588 * Ignore KMALLOC_NORMAL cache to avoid possible circular dependency
2589 * of slab_obj_exts being allocated from the same slab and thus the slab
2590 * becoming effectively unfreeable.
2591 */
2592 if (!cache_needs_objcg(s))
2593 return true;
2594
2595 /* Ignore already charged objects. */
2596 obj_exts = slab_obj_exts(slab);
2597 if (obj_exts) {
2598 get_slab_obj_exts(obj_exts);
2599 obj_ext = slab_obj_ext(s, slab, obj_exts, p);
2600 if (unlikely(slab_obj_ext_objcg(slab, obj_ext))) {
2601 put_slab_obj_exts(obj_exts);
2602 return true;
2603 }
2604 put_slab_obj_exts(obj_exts);
2605 }
2606
2607 return __memcg_slab_post_alloc_hook(s, NULL, flags, SLAB_ALLOC_DEFAULT,
2608 1, &p);
2609 }
2610
2611 #else /* CONFIG_MEMCG */
memcg_slab_post_alloc_hook(struct kmem_cache * s,gfp_t flags,size_t size,void ** p,const struct slab_alloc_context * ac)2612 static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s,
2613 gfp_t flags,
2614 size_t size, void **p,
2615 const struct slab_alloc_context *ac)
2616 {
2617 return true;
2618 }
2619
memcg_slab_free_hook(struct kmem_cache * s,struct slab * slab,void ** p,int objects)2620 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab,
2621 void **p, int objects)
2622 {
2623 }
2624
memcg_slab_post_charge(void * p,gfp_t flags)2625 static inline bool memcg_slab_post_charge(void *p, gfp_t flags)
2626 {
2627 return true;
2628 }
2629 #endif /* CONFIG_MEMCG */
2630
2631 #ifdef CONFIG_SLUB_RCU_DEBUG
2632 static void slab_free_after_rcu_debug(struct rcu_head *rcu_head);
2633
2634 struct rcu_delayed_free {
2635 struct rcu_head head;
2636 void *object;
2637 };
2638 #endif
2639
2640 /*
2641 * Hooks for other subsystems that check memory allocations. In a typical
2642 * production configuration these hooks all should produce no code at all.
2643 *
2644 * Returns true if freeing of the object can proceed, false if its reuse
2645 * was delayed by CONFIG_SLUB_RCU_DEBUG or KASAN quarantine, or it was returned
2646 * to KFENCE.
2647 *
2648 * For objects allocated via kmalloc_nolock(), only a subset of alloc hooks
2649 * are invoked, so some free hooks must handle asymmetric hook calls.
2650 *
2651 * Alloc hooks called for kmalloc_nolock():
2652 * - kmsan_slab_alloc()
2653 * - kasan_slab_alloc()
2654 * - memcg_slab_post_alloc_hook()
2655 * - alloc_tagging_slab_alloc_hook()
2656 *
2657 * Free hooks that must handle missing corresponding alloc hooks:
2658 * - kmemleak_free_recursive()
2659 * - kfence_free()
2660 *
2661 * Free hooks that have no alloc hook counterpart, and thus safe to call:
2662 * - debug_check_no_locks_freed()
2663 * - debug_check_no_obj_freed()
2664 * - __kcsan_check_access()
2665 */
2666 static __always_inline
slab_free_hook(struct kmem_cache * s,void * x,bool init,bool after_rcu_delay)2667 bool slab_free_hook(struct kmem_cache *s, void *x, bool init,
2668 bool after_rcu_delay)
2669 {
2670 /* Are the object contents still accessible? */
2671 bool still_accessible = (s->flags & SLAB_TYPESAFE_BY_RCU) && !after_rcu_delay;
2672
2673 kmemleak_free_recursive(x, s->flags);
2674 kmsan_slab_free(s, x);
2675
2676 debug_check_no_locks_freed(x, s->object_size);
2677
2678 if (!(s->flags & SLAB_DEBUG_OBJECTS))
2679 debug_check_no_obj_freed(x, s->object_size);
2680
2681 /* Use KCSAN to help debug racy use-after-free. */
2682 if (!still_accessible)
2683 __kcsan_check_access(x, s->object_size,
2684 KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT);
2685
2686 if (kfence_free(x))
2687 return false;
2688
2689 /*
2690 * Give KASAN a chance to notice an invalid free operation before we
2691 * modify the object.
2692 */
2693 if (kasan_slab_pre_free(s, x))
2694 return false;
2695
2696 #ifdef CONFIG_SLUB_RCU_DEBUG
2697 if (still_accessible) {
2698 struct rcu_delayed_free *delayed_free;
2699
2700 delayed_free = kmalloc_obj(*delayed_free, GFP_NOWAIT);
2701 if (delayed_free) {
2702 /*
2703 * Let KASAN track our call stack as a "related work
2704 * creation", just like if the object had been freed
2705 * normally via kfree_rcu().
2706 * We have to do this manually because the rcu_head is
2707 * not located inside the object.
2708 */
2709 kasan_record_aux_stack(x);
2710
2711 delayed_free->object = x;
2712 call_rcu(&delayed_free->head, slab_free_after_rcu_debug);
2713 return false;
2714 }
2715 }
2716 #endif /* CONFIG_SLUB_RCU_DEBUG */
2717
2718 /*
2719 * As memory initialization might be integrated into KASAN,
2720 * kasan_slab_free and initialization memset's must be
2721 * kept together to avoid discrepancies in behavior.
2722 *
2723 * The initialization memset's clear the object and the metadata,
2724 * but don't touch the SLAB redzone.
2725 *
2726 * The object's freepointer is also avoided if stored outside the
2727 * object.
2728 */
2729 if (unlikely(init)) {
2730 int rsize;
2731 unsigned int inuse, orig_size;
2732
2733 inuse = get_info_end(s);
2734 orig_size = get_orig_size(s, x);
2735 if (!kasan_has_integrated_init())
2736 memset(kasan_reset_tag(x), 0, orig_size);
2737 rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0;
2738 memset((char *)kasan_reset_tag(x) + inuse, 0,
2739 s->size - inuse - rsize);
2740 /*
2741 * Restore orig_size, otherwise kmalloc redzone overwritten
2742 * would be reported
2743 */
2744 set_orig_size(s, x, orig_size);
2745
2746 }
2747 /* KASAN might put x into memory quarantine, delaying its reuse. */
2748 return !kasan_slab_free(s, x, init, still_accessible, false);
2749 }
2750
2751 static __fastpath_inline
slab_free_freelist_hook(struct kmem_cache * s,void ** head,void ** tail,int * cnt)2752 bool slab_free_freelist_hook(struct kmem_cache *s, void **head, void **tail,
2753 int *cnt)
2754 {
2755
2756 void *object;
2757 void *next = *head;
2758 void *old_tail = *tail;
2759 bool init;
2760
2761 if (is_kfence_address(next)) {
2762 slab_free_hook(s, next, false, false);
2763 return false;
2764 }
2765
2766 /* Head and tail of the reconstructed freelist */
2767 *head = NULL;
2768 *tail = NULL;
2769
2770 init = slab_want_init_on_free(s);
2771
2772 do {
2773 object = next;
2774 next = get_freepointer(s, object);
2775
2776 /* If object's reuse doesn't have to be delayed */
2777 if (likely(slab_free_hook(s, object, init, false))) {
2778 /* Move object to the new freelist */
2779 set_freepointer(s, object, *head);
2780 *head = object;
2781 if (!*tail)
2782 *tail = object;
2783 } else {
2784 /*
2785 * Adjust the reconstructed freelist depth
2786 * accordingly if object's reuse is delayed.
2787 */
2788 --(*cnt);
2789 }
2790 } while (object != old_tail);
2791
2792 return *head != NULL;
2793 }
2794
setup_object(struct kmem_cache * s,void * object)2795 static inline void *setup_object(struct kmem_cache *s, void *object)
2796 {
2797 setup_object_debug(s, object);
2798 object = kasan_init_slab_obj(s, object);
2799 if (unlikely(s->ctor)) {
2800 kasan_unpoison_new_object(s, object);
2801 s->ctor(object);
2802 kasan_poison_new_object(s, object);
2803 }
2804 return object;
2805 }
2806
__alloc_empty_sheaf(struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags,unsigned int capacity)2807 static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
2808 unsigned int alloc_flags, unsigned int capacity)
2809 {
2810 struct slab_sheaf *sheaf;
2811 size_t sheaf_size;
2812
2813 /*
2814 * Prevent recursion to the same cache, or a deep stack of kmallocs of
2815 * varying sizes (sheaf capacity might differ for each kmalloc size
2816 * bucket)
2817 */
2818 if (s->flags & SLAB_KMALLOC)
2819 alloc_flags |= SLAB_ALLOC_NO_RECURSE;
2820
2821 sheaf_size = struct_size(sheaf, objects, capacity);
2822 sheaf = kmalloc_flags(sheaf_size, gfp | __GFP_ZERO, alloc_flags, NUMA_NO_NODE);
2823
2824 if (unlikely(!sheaf))
2825 return NULL;
2826
2827 sheaf->cache = s;
2828
2829 stat(s, SHEAF_ALLOC);
2830
2831 return sheaf;
2832 }
2833
alloc_empty_sheaf(struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags)2834 static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
2835 gfp_t gfp, unsigned int alloc_flags)
2836 {
2837 if (alloc_flags & SLAB_ALLOC_NO_RECURSE)
2838 return NULL;
2839
2840 gfp &= ~OBJCGS_CLEAR_MASK;
2841
2842 return __alloc_empty_sheaf(s, gfp, alloc_flags, s->sheaf_capacity);
2843 }
2844
__free_empty_sheaf(struct kmem_cache * s,struct slab_sheaf * sheaf,unsigned int free_flags)2845 static void __free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
2846 unsigned int free_flags)
2847 {
2848 /*
2849 * If the sheaf was created with SLAB_ALLOC_NO_RECURSE flag then its
2850 * corresponding extension is NULL and alloc_tag_sub() will throw a
2851 * warning, therefore replace NULL with CODETAG_EMPTY to indicate
2852 * that the extension for this sheaf is expected to be NULL.
2853 */
2854 if (s->flags & SLAB_KMALLOC)
2855 mark_obj_codetag_empty(sheaf);
2856
2857 VM_WARN_ON_ONCE(sheaf->size > 0);
2858
2859 if (unlikely(free_flags & SLAB_FREE_NOLOCK))
2860 kfree_nolock(sheaf);
2861 else
2862 kfree(sheaf);
2863
2864 stat(s, SHEAF_FREE);
2865 }
2866
free_empty_sheaf(struct kmem_cache * s,struct slab_sheaf * sheaf)2867 static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
2868 {
2869 __free_empty_sheaf(s, sheaf, SLAB_FREE_DEFAULT);
2870 }
2871
2872 static unsigned int
2873 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
2874 unsigned int max);
2875
refill_sheaf(struct kmem_cache * s,struct slab_sheaf * sheaf,gfp_t gfp)2876 static int refill_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
2877 gfp_t gfp)
2878 {
2879 int to_fill = s->sheaf_capacity - sheaf->size;
2880 int filled;
2881
2882 if (!to_fill)
2883 return 0;
2884
2885 filled = refill_objects(s, &sheaf->objects[sheaf->size], gfp, to_fill,
2886 to_fill);
2887
2888 sheaf->size += filled;
2889
2890 stat_add(s, SHEAF_REFILL, filled);
2891
2892 if (filled < to_fill)
2893 return -ENOMEM;
2894
2895 return 0;
2896 }
2897
2898 /*
2899 * Maximum number of objects freed during a single flush of main pcs sheaf.
2900 * Translates directly to an on-stack array size.
2901 */
2902 #define PCS_BATCH_MAX 32U
2903
2904 static void __kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p);
2905
2906 /*
2907 * Free all objects from the main sheaf. In order to perform
2908 * __kmem_cache_free_bulk() outside of cpu_sheaves->lock, work in batches where
2909 * object pointers are moved to a on-stack array under the lock. To bound the
2910 * stack usage, limit each batch to PCS_BATCH_MAX.
2911 *
2912 * Must be called with s->cpu_sheaves->lock locked, returns with the lock
2913 * unlocked.
2914 *
2915 * Returns how many objects are remaining to be flushed
2916 */
__sheaf_flush_main_batch(struct kmem_cache * s)2917 static unsigned int __sheaf_flush_main_batch(struct kmem_cache *s)
2918 {
2919 struct slub_percpu_sheaves *pcs;
2920 unsigned int batch, remaining;
2921 void *objects[PCS_BATCH_MAX];
2922 struct slab_sheaf *sheaf;
2923
2924 slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
2925
2926 pcs = this_cpu_ptr(s->cpu_sheaves);
2927 sheaf = pcs->main;
2928
2929 batch = min(PCS_BATCH_MAX, sheaf->size);
2930
2931 sheaf->size -= batch;
2932 memcpy(objects, sheaf->objects + sheaf->size, batch * sizeof(void *));
2933
2934 remaining = sheaf->size;
2935
2936 local_unlock(&s->cpu_sheaves->lock);
2937
2938 __kmem_cache_free_bulk(s, batch, &objects[0]);
2939
2940 stat_add(s, SHEAF_FLUSH, batch);
2941
2942 return remaining;
2943 }
2944
sheaf_flush_main(struct kmem_cache * s)2945 static void sheaf_flush_main(struct kmem_cache *s)
2946 {
2947 unsigned int remaining;
2948
2949 do {
2950 local_lock(&s->cpu_sheaves->lock);
2951
2952 remaining = __sheaf_flush_main_batch(s);
2953
2954 } while (remaining);
2955 }
2956
2957 /*
2958 * Returns true if the main sheaf was at least partially flushed.
2959 */
sheaf_try_flush_main(struct kmem_cache * s)2960 static bool sheaf_try_flush_main(struct kmem_cache *s)
2961 {
2962 unsigned int remaining;
2963 bool ret = false;
2964
2965 do {
2966 if (!local_trylock(&s->cpu_sheaves->lock))
2967 return ret;
2968
2969 ret = true;
2970 remaining = __sheaf_flush_main_batch(s);
2971
2972 } while (remaining);
2973
2974 return ret;
2975 }
2976
2977 /*
2978 * Free all objects from a sheaf that's unused, i.e. not linked to any
2979 * cpu_sheaves, so we need no locking and batching. The locking is also not
2980 * necessary when flushing cpu's sheaves (both spare and main) during cpu
2981 * hotremove as the cpu is not executing anymore.
2982 */
sheaf_flush_unused(struct kmem_cache * s,struct slab_sheaf * sheaf)2983 static void sheaf_flush_unused(struct kmem_cache *s, struct slab_sheaf *sheaf)
2984 {
2985 if (!sheaf->size)
2986 return;
2987
2988 stat_add(s, SHEAF_FLUSH, sheaf->size);
2989
2990 __kmem_cache_free_bulk(s, sheaf->size, &sheaf->objects[0]);
2991
2992 sheaf->size = 0;
2993 }
2994
__rcu_free_sheaf_prepare(struct kmem_cache * s,struct slab_sheaf * sheaf)2995 static bool __rcu_free_sheaf_prepare(struct kmem_cache *s,
2996 struct slab_sheaf *sheaf)
2997 {
2998 bool init = slab_want_init_on_free(s);
2999 void **p = &sheaf->objects[0];
3000 unsigned int i = 0;
3001 bool pfmemalloc = false;
3002
3003 while (i < sheaf->size) {
3004 struct slab *slab = virt_to_slab(p[i]);
3005
3006 memcg_slab_free_hook(s, slab, p + i, 1);
3007 alloc_tagging_slab_free_hook(s, slab, p + i, 1);
3008
3009 if (unlikely(!slab_free_hook(s, p[i], init, true))) {
3010 p[i] = p[--sheaf->size];
3011 continue;
3012 }
3013
3014 if (slab_test_pfmemalloc(slab))
3015 pfmemalloc = true;
3016
3017 i++;
3018 }
3019
3020 return pfmemalloc;
3021 }
3022
rcu_free_sheaf_nobarn(struct rcu_head * head)3023 static void rcu_free_sheaf_nobarn(struct rcu_head *head)
3024 {
3025 struct slab_sheaf *sheaf;
3026 struct kmem_cache *s;
3027
3028 sheaf = container_of(head, struct slab_sheaf, rcu_head);
3029 s = sheaf->cache;
3030
3031 __rcu_free_sheaf_prepare(s, sheaf);
3032
3033 sheaf_flush_unused(s, sheaf);
3034
3035 free_empty_sheaf(s, sheaf);
3036 }
3037
3038 /*
3039 * Caller needs to make sure migration is disabled in order to fully flush
3040 * single cpu's sheaves
3041 *
3042 * must not be called from an irq
3043 *
3044 * flushing operations are rare so let's keep it simple and flush to slabs
3045 * directly, skipping the barn
3046 */
pcs_flush_all(struct kmem_cache * s)3047 static void pcs_flush_all(struct kmem_cache *s)
3048 {
3049 struct slub_percpu_sheaves *pcs;
3050 struct slab_sheaf *spare, *rcu_free;
3051
3052 local_lock(&s->cpu_sheaves->lock);
3053 pcs = this_cpu_ptr(s->cpu_sheaves);
3054
3055 spare = pcs->spare;
3056 pcs->spare = NULL;
3057
3058 rcu_free = pcs->rcu_free;
3059 pcs->rcu_free = NULL;
3060
3061 local_unlock(&s->cpu_sheaves->lock);
3062
3063 if (spare) {
3064 sheaf_flush_unused(s, spare);
3065 free_empty_sheaf(s, spare);
3066 }
3067
3068 if (rcu_free)
3069 call_rcu(&rcu_free->rcu_head, rcu_free_sheaf_nobarn);
3070
3071 sheaf_flush_main(s);
3072 }
3073
__pcs_flush_all_cpu(struct kmem_cache * s,unsigned int cpu)3074 static void __pcs_flush_all_cpu(struct kmem_cache *s, unsigned int cpu)
3075 {
3076 struct slub_percpu_sheaves *pcs;
3077
3078 pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
3079
3080 /* The cpu is not executing anymore so we don't need pcs->lock */
3081 sheaf_flush_unused(s, pcs->main);
3082 if (pcs->spare) {
3083 sheaf_flush_unused(s, pcs->spare);
3084 free_empty_sheaf(s, pcs->spare);
3085 pcs->spare = NULL;
3086 }
3087
3088 if (pcs->rcu_free) {
3089 call_rcu(&pcs->rcu_free->rcu_head, rcu_free_sheaf_nobarn);
3090 pcs->rcu_free = NULL;
3091 }
3092 }
3093
pcs_destroy(struct kmem_cache * s)3094 static void pcs_destroy(struct kmem_cache *s)
3095 {
3096 int cpu;
3097
3098 /*
3099 * We may be unwinding cache creation that failed before or during the
3100 * allocation of this.
3101 */
3102 if (!s->cpu_sheaves)
3103 return;
3104
3105 /* pcs->main can only point to the bootstrap sheaf, nothing to free */
3106 if (!cache_has_sheaves(s))
3107 goto free_pcs;
3108
3109 for_each_possible_cpu(cpu) {
3110 struct slub_percpu_sheaves *pcs;
3111
3112 pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
3113
3114 /* This can happen when unwinding failed cache creation. */
3115 if (!pcs->main)
3116 continue;
3117
3118 /*
3119 * We have already passed __kmem_cache_shutdown() so everything
3120 * was flushed and there should be no objects allocated from
3121 * slabs, otherwise kmem_cache_destroy() would have aborted.
3122 * Therefore something would have to be really wrong if the
3123 * warnings here trigger, and we should rather leave objects and
3124 * sheaves to leak in that case.
3125 */
3126
3127 WARN_ON(pcs->spare);
3128 WARN_ON(pcs->rcu_free);
3129
3130 if (!WARN_ON(pcs->main->size)) {
3131 free_empty_sheaf(s, pcs->main);
3132 pcs->main = NULL;
3133 }
3134 }
3135
3136 free_pcs:
3137 free_percpu(s->cpu_sheaves);
3138 s->cpu_sheaves = NULL;
3139 }
3140
barn_get_empty_sheaf(struct node_barn * barn,bool allow_spin)3141 static struct slab_sheaf *barn_get_empty_sheaf(struct node_barn *barn,
3142 bool allow_spin)
3143 {
3144 struct slab_sheaf *empty = NULL;
3145 unsigned long flags;
3146
3147 if (!data_race(barn->nr_empty))
3148 return NULL;
3149
3150 if (likely(allow_spin))
3151 spin_lock_irqsave(&barn->lock, flags);
3152 else if (!spin_trylock_irqsave(&barn->lock, flags))
3153 return NULL;
3154
3155 if (likely(barn->nr_empty)) {
3156 empty = list_first_entry(&barn->sheaves_empty,
3157 struct slab_sheaf, barn_list);
3158 list_del(&empty->barn_list);
3159 barn->nr_empty--;
3160 }
3161
3162 spin_unlock_irqrestore(&barn->lock, flags);
3163
3164 return empty;
3165 }
3166
3167 /*
3168 * The following two functions are used mainly in cases where we have to undo an
3169 * intended action due to a race or cpu migration. Thus they do not check the
3170 * empty or full sheaf limits for simplicity.
3171 */
3172
barn_put_empty_sheaf(struct node_barn * barn,struct slab_sheaf * sheaf)3173 static void barn_put_empty_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
3174 {
3175 unsigned long flags;
3176
3177 spin_lock_irqsave(&barn->lock, flags);
3178
3179 list_add(&sheaf->barn_list, &barn->sheaves_empty);
3180 barn->nr_empty++;
3181
3182 spin_unlock_irqrestore(&barn->lock, flags);
3183 }
3184
barn_put_full_sheaf(struct node_barn * barn,struct slab_sheaf * sheaf)3185 static void barn_put_full_sheaf(struct node_barn *barn, struct slab_sheaf *sheaf)
3186 {
3187 unsigned long flags;
3188
3189 spin_lock_irqsave(&barn->lock, flags);
3190
3191 list_add(&sheaf->barn_list, &barn->sheaves_full);
3192 barn->nr_full++;
3193
3194 spin_unlock_irqrestore(&barn->lock, flags);
3195 }
3196
barn_get_full_or_empty_sheaf(struct node_barn * barn)3197 static struct slab_sheaf *barn_get_full_or_empty_sheaf(struct node_barn *barn)
3198 {
3199 struct slab_sheaf *sheaf = NULL;
3200 unsigned long flags;
3201
3202 if (!data_race(barn->nr_full) && !data_race(barn->nr_empty))
3203 return NULL;
3204
3205 spin_lock_irqsave(&barn->lock, flags);
3206
3207 if (barn->nr_full) {
3208 sheaf = list_first_entry(&barn->sheaves_full, struct slab_sheaf,
3209 barn_list);
3210 list_del(&sheaf->barn_list);
3211 barn->nr_full--;
3212 } else if (barn->nr_empty) {
3213 sheaf = list_first_entry(&barn->sheaves_empty,
3214 struct slab_sheaf, barn_list);
3215 list_del(&sheaf->barn_list);
3216 barn->nr_empty--;
3217 }
3218
3219 spin_unlock_irqrestore(&barn->lock, flags);
3220
3221 return sheaf;
3222 }
3223
3224 /*
3225 * If a full sheaf is available, return it and put the supplied empty one to
3226 * barn. We ignore the limit on empty sheaves as the number of sheaves doesn't
3227 * change.
3228 */
3229 static struct slab_sheaf *
barn_replace_empty_sheaf(struct node_barn * barn,struct slab_sheaf * empty,bool allow_spin)3230 barn_replace_empty_sheaf(struct node_barn *barn, struct slab_sheaf *empty,
3231 bool allow_spin)
3232 {
3233 struct slab_sheaf *full = NULL;
3234 unsigned long flags;
3235
3236 if (!data_race(barn->nr_full))
3237 return NULL;
3238
3239 if (likely(allow_spin))
3240 spin_lock_irqsave(&barn->lock, flags);
3241 else if (!spin_trylock_irqsave(&barn->lock, flags))
3242 return NULL;
3243
3244 if (likely(barn->nr_full)) {
3245 full = list_first_entry(&barn->sheaves_full, struct slab_sheaf,
3246 barn_list);
3247 list_del(&full->barn_list);
3248 list_add(&empty->barn_list, &barn->sheaves_empty);
3249 barn->nr_full--;
3250 barn->nr_empty++;
3251 }
3252
3253 spin_unlock_irqrestore(&barn->lock, flags);
3254
3255 return full;
3256 }
3257
3258 /*
3259 * If an empty sheaf is available, return it and put the supplied full one to
3260 * barn. But if there are too many full sheaves, reject this with -E2BIG.
3261 */
3262 static struct slab_sheaf *
barn_replace_full_sheaf(struct node_barn * barn,struct slab_sheaf * full,bool allow_spin)3263 barn_replace_full_sheaf(struct node_barn *barn, struct slab_sheaf *full,
3264 bool allow_spin)
3265 {
3266 struct slab_sheaf *empty;
3267 unsigned long flags;
3268
3269 /* we don't repeat this check under barn->lock as it's not critical */
3270 if (data_race(barn->nr_full) >= MAX_FULL_SHEAVES)
3271 return ERR_PTR(-E2BIG);
3272 if (!data_race(barn->nr_empty))
3273 return ERR_PTR(-ENOMEM);
3274
3275 if (likely(allow_spin))
3276 spin_lock_irqsave(&barn->lock, flags);
3277 else if (!spin_trylock_irqsave(&barn->lock, flags))
3278 return ERR_PTR(-EBUSY);
3279
3280 if (likely(barn->nr_empty)) {
3281 empty = list_first_entry(&barn->sheaves_empty, struct slab_sheaf,
3282 barn_list);
3283 list_del(&empty->barn_list);
3284 list_add(&full->barn_list, &barn->sheaves_full);
3285 barn->nr_empty--;
3286 barn->nr_full++;
3287 } else {
3288 empty = ERR_PTR(-ENOMEM);
3289 }
3290
3291 spin_unlock_irqrestore(&barn->lock, flags);
3292
3293 return empty;
3294 }
3295
barn_init(struct node_barn * barn)3296 static void barn_init(struct node_barn *barn)
3297 {
3298 spin_lock_init(&barn->lock);
3299 INIT_LIST_HEAD(&barn->sheaves_full);
3300 INIT_LIST_HEAD(&barn->sheaves_empty);
3301 barn->nr_full = 0;
3302 barn->nr_empty = 0;
3303 }
3304
barn_shrink(struct kmem_cache * s,struct node_barn * barn)3305 static void barn_shrink(struct kmem_cache *s, struct node_barn *barn)
3306 {
3307 LIST_HEAD(empty_list);
3308 LIST_HEAD(full_list);
3309 struct slab_sheaf *sheaf, *sheaf2;
3310 unsigned long flags;
3311
3312 spin_lock_irqsave(&barn->lock, flags);
3313
3314 list_splice_init(&barn->sheaves_full, &full_list);
3315 barn->nr_full = 0;
3316 list_splice_init(&barn->sheaves_empty, &empty_list);
3317 barn->nr_empty = 0;
3318
3319 spin_unlock_irqrestore(&barn->lock, flags);
3320
3321 list_for_each_entry_safe(sheaf, sheaf2, &full_list, barn_list) {
3322 sheaf_flush_unused(s, sheaf);
3323 free_empty_sheaf(s, sheaf);
3324 }
3325
3326 list_for_each_entry_safe(sheaf, sheaf2, &empty_list, barn_list)
3327 free_empty_sheaf(s, sheaf);
3328 }
3329
3330 /*
3331 * Slab allocation and freeing
3332 */
alloc_slab_page(gfp_t flags,int node,struct kmem_cache_order_objects oo,bool allow_spin)3333 static inline struct slab *alloc_slab_page(gfp_t flags, int node,
3334 struct kmem_cache_order_objects oo,
3335 bool allow_spin)
3336 {
3337 struct page *page;
3338 struct slab *slab;
3339 unsigned int order = oo_order(oo);
3340
3341 if (unlikely(!allow_spin))
3342 page = alloc_frozen_pages_nolock(0/* __GFP_COMP is implied */,
3343 node, order);
3344 else if (node == NUMA_NO_NODE)
3345 page = alloc_frozen_pages(flags, order);
3346 else
3347 page = __alloc_frozen_pages(flags, order, node, NULL,
3348 ALLOC_DEFAULT);
3349
3350 if (!page)
3351 return NULL;
3352
3353 __SetPageSlab(page);
3354 slab = page_slab(page);
3355 if (page_is_pfmemalloc(page))
3356 slab_set_pfmemalloc(slab);
3357
3358 return slab;
3359 }
3360
3361 #ifdef CONFIG_SLAB_FREELIST_RANDOM
3362 /* Pre-initialize the random sequence cache */
init_cache_random_seq(struct kmem_cache * s)3363 static int init_cache_random_seq(struct kmem_cache *s)
3364 {
3365 unsigned int count = oo_objects(s->oo);
3366 int err;
3367
3368 /* Bailout if already initialised */
3369 if (s->random_seq)
3370 return 0;
3371
3372 err = cache_random_seq_create(s, count, GFP_KERNEL);
3373 if (err) {
3374 pr_err("SLUB: Unable to initialize free list for %s\n",
3375 s->name);
3376 return err;
3377 }
3378
3379 /* Transform to an offset on the set of pages */
3380 if (s->random_seq) {
3381 unsigned int i;
3382
3383 for (i = 0; i < count; i++)
3384 s->random_seq[i] *= s->size;
3385 }
3386 return 0;
3387 }
3388
3389 /* Initialize each random sequence freelist per cache */
init_freelist_randomization(void)3390 static void __init init_freelist_randomization(void)
3391 {
3392 struct kmem_cache *s;
3393
3394 mutex_lock(&slab_mutex);
3395
3396 list_for_each_entry(s, &slab_caches, list)
3397 init_cache_random_seq(s);
3398
3399 mutex_unlock(&slab_mutex);
3400 }
3401
3402 static DEFINE_PER_CPU(struct rnd_state, slab_rnd_state);
3403
3404 #else
init_cache_random_seq(struct kmem_cache * s)3405 static inline int init_cache_random_seq(struct kmem_cache *s)
3406 {
3407 return 0;
3408 }
init_freelist_randomization(void)3409 static inline void init_freelist_randomization(void) { }
3410 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
3411
account_slab(struct slab * slab,int order,struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags)3412 static __always_inline void account_slab(struct slab *slab, int order,
3413 struct kmem_cache *s, gfp_t gfp,
3414 unsigned int alloc_flags)
3415 {
3416 if (memcg_kmem_online() &&
3417 (s->flags & SLAB_ACCOUNT) &&
3418 !slab_obj_exts(slab))
3419 alloc_slab_obj_exts(slab, s, gfp,
3420 alloc_flags | SLAB_ALLOC_NEW_SLAB);
3421
3422 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
3423 PAGE_SIZE << order);
3424 }
3425
unaccount_slab(struct slab * slab,int order,struct kmem_cache * s,bool allow_spin)3426 static __always_inline void unaccount_slab(struct slab *slab, int order,
3427 struct kmem_cache *s, bool allow_spin)
3428 {
3429 /*
3430 * The slab object extensions should now be freed regardless of
3431 * whether mem_alloc_profiling_enabled() or not because profiling
3432 * might have been disabled after slab->obj_exts got allocated.
3433 */
3434 free_slab_obj_exts(slab, allow_spin);
3435
3436 mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
3437 -(PAGE_SIZE << order));
3438 }
3439
3440 /* Allocate and initialize a slab without building its freelist. */
allocate_slab(struct kmem_cache * s,gfp_t flags,unsigned int alloc_flags,int node)3441 static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags,
3442 unsigned int alloc_flags, int node)
3443 {
3444 bool allow_spin = alloc_flags_allow_spinning(alloc_flags);
3445 struct slab *slab;
3446 struct kmem_cache_order_objects oo = s->oo;
3447 gfp_t alloc_gfp;
3448 void *start;
3449
3450 flags &= gfp_allowed_mask;
3451
3452 flags |= s->allocflags;
3453
3454 /*
3455 * Let the initial higher-order allocation fail under memory pressure
3456 * so we fall-back to the minimum order allocation.
3457 */
3458 alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
3459 if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min))
3460 alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~__GFP_RECLAIM;
3461
3462 slab = alloc_slab_page(alloc_gfp, node, oo, allow_spin);
3463 if (unlikely(!slab)) {
3464 oo = s->min;
3465 alloc_gfp = flags;
3466 /*
3467 * Allocation may have failed due to fragmentation.
3468 * Try a lower order alloc if possible
3469 */
3470 slab = alloc_slab_page(alloc_gfp, node, oo, allow_spin);
3471 if (unlikely(!slab))
3472 return NULL;
3473 stat(s, ORDER_FALLBACK);
3474 }
3475
3476 /* Initializes frozen, inuse, and any extra 64bit-only flags */
3477 slab->counters = 0;
3478
3479 slab->objects = oo_objects(oo);
3480
3481 #ifdef CONFIG_64BIT
3482 if (cache_needs_objcg(s))
3483 slab->obj_exts_needs_objcg = 1;
3484 #endif
3485 slab->slab_cache = s;
3486
3487 kasan_poison_slab(slab);
3488
3489 start = slab_address(slab);
3490
3491 setup_slab_debug(s, slab, start);
3492 init_slab_obj_exts(slab);
3493 /*
3494 * Poison the slab before initializing the slabobj_ext array
3495 * to prevent the array from being overwritten.
3496 */
3497 alloc_slab_obj_exts_early(s, slab);
3498 account_slab(slab, oo_order(oo), s, flags, alloc_flags);
3499
3500 return slab;
3501 }
3502
new_slab(struct kmem_cache * s,gfp_t flags,unsigned int alloc_flags,int node)3503 static struct slab *new_slab(struct kmem_cache *s, gfp_t flags,
3504 unsigned int alloc_flags, int node)
3505 {
3506 if (unlikely(flags & GFP_SLAB_BUG_MASK))
3507 flags = kmalloc_fix_flags(flags);
3508
3509 WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
3510
3511 flags &= GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK;
3512
3513 return allocate_slab(s, flags, alloc_flags, node);
3514 }
3515
__free_slab(struct kmem_cache * s,struct slab * slab,bool allow_spin)3516 static void __free_slab(struct kmem_cache *s, struct slab *slab, bool allow_spin)
3517 {
3518 struct page *page = slab_page(slab);
3519 int order = compound_order(page);
3520 int pages = 1 << order;
3521
3522 __slab_clear_pfmemalloc(slab);
3523 page->mapping = NULL;
3524 __ClearPageSlab(page);
3525 mm_account_reclaimed_pages(pages);
3526 unaccount_slab(slab, order, s, allow_spin);
3527 if (allow_spin)
3528 free_frozen_pages(page, order);
3529 else
3530 free_frozen_pages_nolock(page, order);
3531 }
3532
free_new_slab_nolock(struct kmem_cache * s,struct slab * slab)3533 static void free_new_slab_nolock(struct kmem_cache *s, struct slab *slab)
3534 {
3535 /*
3536 * Since it was just allocated, we can skip the actions in
3537 * discard_slab() and free_slab().
3538 */
3539 __free_slab(s, slab, false);
3540 }
3541
rcu_free_slab(struct rcu_head * h)3542 static void rcu_free_slab(struct rcu_head *h)
3543 {
3544 struct slab *slab = container_of(h, struct slab, rcu_head);
3545
3546 __free_slab(slab->slab_cache, slab, true);
3547 }
3548
free_slab(struct kmem_cache * s,struct slab * slab)3549 static void free_slab(struct kmem_cache *s, struct slab *slab)
3550 {
3551 if (kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) {
3552 void *p;
3553
3554 slab_pad_check(s, slab);
3555 for_each_object(p, s, slab_address(slab), slab->objects)
3556 check_object(s, slab, p, SLUB_RED_INACTIVE);
3557 }
3558
3559 if (unlikely(s->flags & SLAB_TYPESAFE_BY_RCU))
3560 call_rcu(&slab->rcu_head, rcu_free_slab);
3561 else
3562 __free_slab(s, slab, true);
3563 }
3564
discard_slab(struct kmem_cache * s,struct slab * slab)3565 static void discard_slab(struct kmem_cache *s, struct slab *slab)
3566 {
3567 dec_slabs_node(s, slab_nid(slab), slab->objects);
3568 free_slab(s, slab);
3569 }
3570
slab_test_node_partial(const struct slab * slab)3571 static inline bool slab_test_node_partial(const struct slab *slab)
3572 {
3573 return test_bit(SL_partial, &slab->flags.f);
3574 }
3575
slab_set_node_partial(struct slab * slab)3576 static inline void slab_set_node_partial(struct slab *slab)
3577 {
3578 set_bit(SL_partial, &slab->flags.f);
3579 }
3580
slab_clear_node_partial(struct slab * slab)3581 static inline void slab_clear_node_partial(struct slab *slab)
3582 {
3583 clear_bit(SL_partial, &slab->flags.f);
3584 }
3585
3586 /*
3587 * Management of partially allocated slabs.
3588 */
set_node_partial_state(struct kmem_cache_node * n,struct slab * slab)3589 static inline void set_node_partial_state(struct kmem_cache_node *n,
3590 struct slab *slab)
3591 {
3592 slab_set_node_partial(slab);
3593 n->nr_partial++;
3594 }
3595
3596 static inline void
__add_partial(struct kmem_cache_node * n,struct slab * slab,enum add_mode mode)3597 __add_partial(struct kmem_cache_node *n, struct slab *slab, enum add_mode mode)
3598 {
3599 if (mode == ADD_TO_TAIL)
3600 list_add_tail(&slab->slab_list, &n->partial);
3601 else
3602 list_add(&slab->slab_list, &n->partial);
3603 set_node_partial_state(n, slab);
3604 }
3605
add_partial(struct kmem_cache_node * n,struct slab * slab,enum add_mode mode)3606 static inline void add_partial(struct kmem_cache_node *n,
3607 struct slab *slab, enum add_mode mode)
3608 {
3609 slab_lockdep_assert_held(&n->list_lock);
3610 __add_partial(n, slab, mode);
3611 }
3612
clear_node_partial_state(struct kmem_cache_node * n,struct slab * slab)3613 static inline void clear_node_partial_state(struct kmem_cache_node *n,
3614 struct slab *slab)
3615 {
3616 slab_clear_node_partial(slab);
3617 n->nr_partial--;
3618 }
3619
remove_partial(struct kmem_cache_node * n,struct slab * slab)3620 static inline void remove_partial(struct kmem_cache_node *n,
3621 struct slab *slab)
3622 {
3623 slab_lockdep_assert_held(&n->list_lock);
3624 list_del(&slab->slab_list);
3625 clear_node_partial_state(n, slab);
3626 }
3627
3628 /*
3629 * Called only for kmem_cache_debug() caches instead of remove_partial(), with a
3630 * slab from the n->partial list. Remove only a single object from the slab, do
3631 * the alloc_debug_processing() checks and leave the slab on the list, or move
3632 * it to full list if it was the last free object.
3633 */
alloc_single_from_partial(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab,int orig_size)3634 static void *alloc_single_from_partial(struct kmem_cache *s,
3635 struct kmem_cache_node *n, struct slab *slab, int orig_size)
3636 {
3637 void *object;
3638
3639 slab_lockdep_assert_held(&n->list_lock);
3640
3641 #ifdef CONFIG_SLUB_DEBUG
3642 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
3643 if (!validate_slab_ptr(slab)) {
3644 slab_err(s, slab, "Not a valid slab page");
3645 return NULL;
3646 }
3647 }
3648 #endif
3649
3650 object = slab->freelist;
3651 slab->freelist = get_freepointer(s, object);
3652 slab->inuse++;
3653
3654 if (!alloc_debug_processing(s, slab, object, orig_size)) {
3655 remove_partial(n, slab);
3656 return NULL;
3657 }
3658
3659 if (slab->inuse == slab->objects) {
3660 remove_partial(n, slab);
3661 add_full(s, n, slab);
3662 }
3663
3664 return object;
3665 }
3666
3667 /* Return the next free object in allocation order. */
next_slab_obj(struct kmem_cache * s,struct slab_obj_iter * iter)3668 static inline void *next_slab_obj(struct kmem_cache *s,
3669 struct slab_obj_iter *iter)
3670 {
3671 #ifdef CONFIG_SLAB_FREELIST_RANDOM
3672 if (iter->random) {
3673 unsigned long idx;
3674
3675 /*
3676 * If the target page allocation failed, the number of objects on the
3677 * page might be smaller than the usual size defined by the cache.
3678 */
3679 do {
3680 idx = s->random_seq[iter->pos];
3681 iter->pos++;
3682 if (iter->pos >= iter->freelist_count)
3683 iter->pos = 0;
3684 } while (unlikely(idx >= iter->page_limit));
3685
3686 return setup_object(s, (char *)iter->start + idx);
3687 }
3688 #endif
3689 return setup_object(s, (char *)iter->start + iter->pos++ * s->size);
3690 }
3691
3692 /* Build a freelist from the objects not yet allocated from a fresh slab. */
build_slab_freelist(struct kmem_cache * s,struct slab * slab,struct slab_obj_iter * iter)3693 static inline void build_slab_freelist(struct kmem_cache *s, struct slab *slab,
3694 struct slab_obj_iter *iter)
3695 {
3696 unsigned int nr = slab->objects - slab->inuse;
3697 unsigned int i;
3698 void *cur, *next;
3699
3700 if (!nr) {
3701 slab->freelist = NULL;
3702 return;
3703 }
3704
3705 cur = next_slab_obj(s, iter);
3706 slab->freelist = cur;
3707
3708 for (i = 1; i < nr; i++) {
3709 next = next_slab_obj(s, iter);
3710 set_freepointer(s, cur, next);
3711 cur = next;
3712 }
3713
3714 set_freepointer(s, cur, NULL);
3715 }
3716
3717 /* Initialize an iterator over free objects in allocation order. */
init_slab_obj_iter(struct kmem_cache * s,struct slab * slab,struct slab_obj_iter * iter,bool allow_spin)3718 static inline void init_slab_obj_iter(struct kmem_cache *s, struct slab *slab,
3719 struct slab_obj_iter *iter,
3720 bool allow_spin)
3721 {
3722 iter->pos = 0;
3723 iter->start = fixup_red_left(s, slab_address(slab));
3724
3725 #ifdef CONFIG_SLAB_FREELIST_RANDOM
3726 iter->random = (slab->objects >= 2 && s->random_seq);
3727 if (!iter->random)
3728 return;
3729
3730 iter->freelist_count = oo_objects(s->oo);
3731 iter->page_limit = slab->objects * s->size;
3732
3733 if (allow_spin) {
3734 iter->pos = get_random_u32_below(iter->freelist_count);
3735 } else {
3736 struct rnd_state *state;
3737
3738 /*
3739 * An interrupt or NMI handler might interrupt and change
3740 * the state in the middle, but that's safe.
3741 */
3742 state = &get_cpu_var(slab_rnd_state);
3743 iter->pos = prandom_u32_state(state) % iter->freelist_count;
3744 put_cpu_var(slab_rnd_state);
3745 }
3746 #endif
3747 }
3748
3749 /*
3750 * Called only for kmem_cache_debug() caches to allocate from a freshly
3751 * allocated slab. Allocate a single object instead of whole freelist
3752 * and put the slab to the partial (or full) list.
3753 */
alloc_single_from_new_slab(struct kmem_cache * s,struct slab * slab,const struct slab_alloc_context * ac)3754 static void *alloc_single_from_new_slab(struct kmem_cache *s, struct slab *slab,
3755 const struct slab_alloc_context *ac)
3756 {
3757 bool allow_spin = alloc_flags_allow_spinning(ac->alloc_flags);
3758 struct kmem_cache_node *n;
3759 struct slab_obj_iter iter;
3760 bool needs_add_partial;
3761 unsigned long flags;
3762 void *object;
3763
3764 init_slab_obj_iter(s, slab, &iter, allow_spin);
3765 object = next_slab_obj(s, &iter);
3766 slab->inuse = 1;
3767
3768 needs_add_partial = (slab->objects > 1);
3769 build_slab_freelist(s, slab, &iter);
3770
3771 /* alloc_debug_processing() always expects a valid freepointer */
3772 set_freepointer(s, object, slab->freelist);
3773
3774 if (!alloc_debug_processing(s, slab, object, ac->orig_size)) {
3775 /*
3776 * It's not really expected that this would fail on a
3777 * freshly allocated slab, but a concurrent memory
3778 * corruption in theory could cause that.
3779 * Leak memory of allocated slab.
3780 */
3781 return NULL;
3782 }
3783
3784 n = get_node(s, slab_nid(slab));
3785 if (allow_spin) {
3786 spin_lock_irqsave(&n->list_lock, flags);
3787 } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
3788 /*
3789 * Unlucky, discard newly allocated slab.
3790 * The slab is not fully free, but it's fine as
3791 * objects are not allocated to users.
3792 */
3793 free_new_slab_nolock(s, slab);
3794 return NULL;
3795 }
3796
3797 if (needs_add_partial)
3798 add_partial(n, slab, ADD_TO_HEAD);
3799 else
3800 add_full(s, n, slab);
3801
3802 /*
3803 * Debug caches require nr_slabs updates under n->list_lock so validation
3804 * cannot race with slab (de)allocations and observe inconsistent state.
3805 */
3806 inc_slabs_node(s, slab_nid(slab), slab->objects);
3807 spin_unlock_irqrestore(&n->list_lock, flags);
3808
3809 return object;
3810 }
3811
3812 static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags);
3813
get_partial_node_bulk(struct kmem_cache * s,struct kmem_cache_node * n,struct partial_bulk_context * pc,bool allow_spin)3814 static bool get_partial_node_bulk(struct kmem_cache *s,
3815 struct kmem_cache_node *n,
3816 struct partial_bulk_context *pc,
3817 bool allow_spin)
3818 {
3819 struct slab *slab, *slab2;
3820 struct slab *first = NULL, *last = NULL;
3821 unsigned int total_free = 0;
3822 unsigned long flags;
3823
3824 /* Racy check to avoid taking the lock unnecessarily. */
3825 if (!n || data_race(!n->nr_partial))
3826 return false;
3827
3828 INIT_LIST_HEAD(&pc->slabs);
3829
3830 if (allow_spin)
3831 spin_lock_irqsave(&n->list_lock, flags);
3832 else if (!spin_trylock_irqsave(&n->list_lock, flags))
3833 return false;
3834
3835 list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) {
3836 struct freelist_counters flc;
3837 unsigned int slab_free;
3838
3839 if (!pfmemalloc_match(slab, pc->flags)) {
3840 if (first) {
3841 list_bulk_move_tail(&pc->slabs,
3842 &first->slab_list,
3843 &last->slab_list);
3844 first = NULL;
3845 }
3846 continue;
3847 }
3848
3849 /*
3850 * determine the number of free objects in the slab racily
3851 *
3852 * slab_free is a lower bound due to possible subsequent
3853 * concurrent freeing, so the caller may get more objects than
3854 * requested and must handle that
3855 */
3856 flc.counters = data_race(READ_ONCE(slab->counters));
3857 slab_free = flc.objects - flc.inuse;
3858
3859 /* we have already min and this would get us over the max */
3860 if (total_free >= pc->min_objects
3861 && total_free + slab_free > pc->max_objects)
3862 break;
3863
3864 if (!first)
3865 first = slab;
3866 last = slab;
3867 clear_node_partial_state(n, slab);
3868
3869 total_free += slab_free;
3870 if (total_free >= pc->max_objects)
3871 break;
3872 }
3873
3874 if (first)
3875 list_bulk_move_tail(&pc->slabs, &first->slab_list,
3876 &last->slab_list);
3877
3878 spin_unlock_irqrestore(&n->list_lock, flags);
3879 return total_free > 0;
3880 }
3881
3882 /*
3883 * Try to allocate object from a partial slab on a specific node.
3884 */
get_from_partial_node(struct kmem_cache * s,struct kmem_cache_node * n,gfp_t gfp_flags,const struct slab_alloc_context * ac)3885 static void *get_from_partial_node(struct kmem_cache *s,
3886 struct kmem_cache_node *n,
3887 gfp_t gfp_flags,
3888 const struct slab_alloc_context *ac)
3889 {
3890 struct slab *slab, *slab2;
3891 unsigned long flags;
3892 void *object = NULL;
3893
3894 /*
3895 * Racy check. If we mistakenly see no partial slabs then we
3896 * just allocate an empty slab. If we mistakenly try to get a
3897 * partial slab and there is none available then get_from_partial()
3898 * will return NULL.
3899 */
3900 if (!n || !n->nr_partial)
3901 return NULL;
3902
3903 if (alloc_flags_allow_spinning(ac->alloc_flags))
3904 spin_lock_irqsave(&n->list_lock, flags);
3905 else if (!spin_trylock_irqsave(&n->list_lock, flags))
3906 return NULL;
3907 list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) {
3908
3909 struct freelist_counters old, new;
3910
3911 if (!pfmemalloc_match(slab, gfp_flags))
3912 continue;
3913
3914 if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) {
3915 object = alloc_single_from_partial(s, n, slab,
3916 ac->orig_size);
3917 if (object)
3918 break;
3919 continue;
3920 }
3921
3922 /*
3923 * get a single object from the slab. This might race against
3924 * __slab_free(), which however has to take the list_lock if
3925 * it's about to make the slab fully free.
3926 */
3927 do {
3928 old.freelist = slab->freelist;
3929 old.counters = slab->counters;
3930
3931 new.freelist = get_freepointer(s, old.freelist);
3932 new.counters = old.counters;
3933 new.inuse++;
3934
3935 } while (!__slab_update_freelist(s, slab, &old, &new, "get_from_partial_node"));
3936
3937 object = old.freelist;
3938 if (!new.freelist)
3939 remove_partial(n, slab);
3940
3941 break;
3942 }
3943 spin_unlock_irqrestore(&n->list_lock, flags);
3944 return object;
3945 }
3946
3947 /*
3948 * Get an object from somewhere. Search in increasing NUMA distances.
3949 */
get_from_any_partial(struct kmem_cache * s,gfp_t gfp_flags,const struct slab_alloc_context * ac)3950 static void *get_from_any_partial(struct kmem_cache *s, gfp_t gfp_flags,
3951 const struct slab_alloc_context *ac)
3952 {
3953 #ifdef CONFIG_NUMA
3954 struct zonelist *zonelist;
3955 struct zoneref *z;
3956 struct zone *zone;
3957 enum zone_type highest_zoneidx = gfp_zone(gfp_flags);
3958 unsigned int cpuset_mems_cookie;
3959 bool allow_spin = alloc_flags_allow_spinning(ac->alloc_flags);
3960
3961 /*
3962 * The defrag ratio allows a configuration of the tradeoffs between
3963 * inter node defragmentation and node local allocations. A lower
3964 * defrag_ratio increases the tendency to do local allocations
3965 * instead of attempting to obtain partial slabs from other nodes.
3966 *
3967 * If the defrag_ratio is set to 0 then kmalloc() always
3968 * returns node local objects. If the ratio is higher then kmalloc()
3969 * may return off node objects because partial slabs are obtained
3970 * from other nodes and filled up.
3971 *
3972 * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100
3973 * (which makes defrag_ratio = 1000) then every (well almost)
3974 * allocation will first attempt to defrag slab caches on other nodes.
3975 * This means scanning over all nodes to look for partial slabs which
3976 * may be expensive if we do it every time we are trying to find a slab
3977 * with available objects.
3978 */
3979 if (!s->remote_node_defrag_ratio ||
3980 get_cycles() % 1024 > s->remote_node_defrag_ratio)
3981 return NULL;
3982
3983 do {
3984 /*
3985 * read_mems_allowed_begin() accesses current->mems_allowed_seq,
3986 * a seqcount_spinlock_t that is not NMI-safe. Do not access
3987 * current->mems_allowed_seq and avoid retry when GFP flags
3988 * indicate spinning is not allowed.
3989 */
3990 if (allow_spin)
3991 cpuset_mems_cookie = read_mems_allowed_begin();
3992
3993 zonelist = node_zonelist(mempolicy_slab_node(), gfp_flags);
3994 for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
3995 struct kmem_cache_node *n;
3996
3997 n = get_node(s, zone_to_nid(zone));
3998
3999 if (n && cpuset_zone_allowed(zone, gfp_flags) &&
4000 n->nr_partial > s->min_partial) {
4001
4002 void *object = get_from_partial_node(s, n,
4003 gfp_flags, ac);
4004
4005 if (object) {
4006 /*
4007 * Don't check read_mems_allowed_retry()
4008 * here - if mems_allowed was updated in
4009 * parallel, that was a harmless race
4010 * between allocation and the cpuset
4011 * update
4012 */
4013 return object;
4014 }
4015 }
4016 }
4017 } while (allow_spin && read_mems_allowed_retry(cpuset_mems_cookie));
4018 #endif /* CONFIG_NUMA */
4019 return NULL;
4020 }
4021
4022 /*
4023 * Get an object from a partial slab
4024 */
get_from_partial(struct kmem_cache * s,int node,gfp_t flags,const struct slab_alloc_context * ac)4025 static void *get_from_partial(struct kmem_cache *s, int node, gfp_t flags,
4026 const struct slab_alloc_context *ac)
4027 {
4028 int searchnode = node;
4029 void *object;
4030
4031 if (node == NUMA_NO_NODE)
4032 searchnode = numa_mem_id();
4033
4034 object = get_from_partial_node(s, get_node(s, searchnode), flags, ac);
4035 if (object || (node != NUMA_NO_NODE && (flags & __GFP_THISNODE)))
4036 return object;
4037
4038 return get_from_any_partial(s, flags, ac);
4039 }
4040
has_pcs_used(int cpu,struct kmem_cache * s)4041 static bool has_pcs_used(int cpu, struct kmem_cache *s)
4042 {
4043 struct slub_percpu_sheaves *pcs;
4044
4045 if (!cache_has_sheaves(s))
4046 return false;
4047
4048 pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
4049
4050 return (pcs->spare || pcs->rcu_free || pcs->main->size);
4051 }
4052
4053 /*
4054 * Flush percpu sheaves
4055 *
4056 * Called from CPU work handler with migration disabled.
4057 */
flush_cpu_sheaves(struct work_struct * w)4058 static void flush_cpu_sheaves(struct work_struct *w)
4059 {
4060 struct kmem_cache *s;
4061 struct slub_flush_work *sfw;
4062
4063 sfw = container_of(w, struct slub_flush_work, work);
4064
4065 s = sfw->s;
4066
4067 if (cache_has_sheaves(s))
4068 pcs_flush_all(s);
4069 }
4070
flush_all_cpus_locked(struct kmem_cache * s)4071 static void flush_all_cpus_locked(struct kmem_cache *s)
4072 {
4073 struct slub_flush_work *sfw;
4074 unsigned int cpu;
4075
4076 lockdep_assert_cpus_held();
4077 mutex_lock(&flush_lock);
4078
4079 for_each_online_cpu(cpu) {
4080 sfw = &per_cpu(slub_flush, cpu);
4081 if (!has_pcs_used(cpu, s)) {
4082 sfw->skip = true;
4083 continue;
4084 }
4085 INIT_WORK(&sfw->work, flush_cpu_sheaves);
4086 sfw->skip = false;
4087 sfw->s = s;
4088 queue_work_on(cpu, flushwq, &sfw->work);
4089 }
4090
4091 for_each_online_cpu(cpu) {
4092 sfw = &per_cpu(slub_flush, cpu);
4093 if (sfw->skip)
4094 continue;
4095 flush_work(&sfw->work);
4096 }
4097
4098 mutex_unlock(&flush_lock);
4099 }
4100
flush_all(struct kmem_cache * s)4101 static void flush_all(struct kmem_cache *s)
4102 {
4103 cpus_read_lock();
4104 flush_all_cpus_locked(s);
4105 cpus_read_unlock();
4106 }
4107
4108 struct deferred_percpu_work {
4109 struct llist_head objects;
4110 struct llist_head objects_by_rcu;
4111 struct llist_head rcu_sheaves;
4112 struct irq_work work;
4113 };
4114
4115 static void deferred_percpu_work_fn(struct irq_work *work);
4116
4117 static DEFINE_PER_CPU(struct deferred_percpu_work, deferred_percpu_work) = {
4118 .objects = LLIST_HEAD_INIT(objects),
4119 .objects_by_rcu = LLIST_HEAD_INIT(objects_by_rcu),
4120 .rcu_sheaves = LLIST_HEAD_INIT(rcu_sheaves),
4121 .work = IRQ_WORK_INIT(deferred_percpu_work_fn),
4122 };
4123
flush_rcu_sheaf(struct work_struct * w)4124 static void flush_rcu_sheaf(struct work_struct *w)
4125 {
4126 struct slub_percpu_sheaves *pcs;
4127 struct slab_sheaf *rcu_free;
4128 struct slub_flush_work *sfw;
4129 struct kmem_cache *s;
4130
4131 sfw = container_of(w, struct slub_flush_work, work);
4132 s = sfw->s;
4133
4134 local_lock(&s->cpu_sheaves->lock);
4135 pcs = this_cpu_ptr(s->cpu_sheaves);
4136
4137 rcu_free = pcs->rcu_free;
4138 pcs->rcu_free = NULL;
4139
4140 local_unlock(&s->cpu_sheaves->lock);
4141
4142 if (rcu_free)
4143 call_rcu(&rcu_free->rcu_head, rcu_free_sheaf_nobarn);
4144 }
4145
4146
4147 /* needed for kvfree_rcu_barrier() */
flush_rcu_sheaves_on_cache(struct kmem_cache * s)4148 void flush_rcu_sheaves_on_cache(struct kmem_cache *s)
4149 {
4150 struct slub_flush_work *sfw;
4151 unsigned int cpu;
4152
4153 lockdep_assert_cpus_held();
4154 mutex_lock(&flush_lock);
4155
4156 for_each_online_cpu(cpu) {
4157 sfw = &per_cpu(slub_flush, cpu);
4158
4159 /*
4160 * we don't check if rcu_free sheaf exists - racing
4161 * __kfree_rcu_sheaf() might have just removed it.
4162 * by executing flush_rcu_sheaf() on the cpu we make
4163 * sure the __kfree_rcu_sheaf() finished its call_rcu()
4164 */
4165
4166 INIT_WORK(&sfw->work, flush_rcu_sheaf);
4167 sfw->s = s;
4168 queue_work_on(cpu, flushwq, &sfw->work);
4169 }
4170
4171 for_each_online_cpu(cpu) {
4172 sfw = &per_cpu(slub_flush, cpu);
4173 flush_work(&sfw->work);
4174 }
4175
4176 mutex_unlock(&flush_lock);
4177 }
4178
flush_all_rcu_sheaves(void)4179 void flush_all_rcu_sheaves(void)
4180 {
4181 struct kmem_cache *s;
4182
4183 deferred_work_barrier();
4184
4185 cpus_read_lock();
4186 mutex_lock(&slab_mutex);
4187
4188 list_for_each_entry(s, &slab_caches, list) {
4189 if (!cache_has_sheaves(s))
4190 continue;
4191 flush_rcu_sheaves_on_cache(s);
4192 }
4193
4194 mutex_unlock(&slab_mutex);
4195 cpus_read_unlock();
4196
4197 rcu_barrier();
4198 }
4199
slub_cpu_setup(unsigned int cpu)4200 static int slub_cpu_setup(unsigned int cpu)
4201 {
4202 int nid = cpu_to_node(cpu);
4203 struct kmem_cache *s;
4204 int ret = 0;
4205
4206 /*
4207 * we never clear a nid so it's safe to do a quick check before taking
4208 * the mutex, and then recheck to handle parallel cpu hotplug safely
4209 */
4210 if (node_isset(nid, slab_barn_nodes))
4211 return 0;
4212
4213 mutex_lock(&slab_mutex);
4214
4215 if (node_isset(nid, slab_barn_nodes))
4216 goto out;
4217
4218 list_for_each_entry(s, &slab_caches, list) {
4219 struct node_barn *barn;
4220
4221 /*
4222 * barn might already exist if a previous callback failed midway
4223 */
4224 if (!cache_has_sheaves(s) || get_barn_node(s, nid))
4225 continue;
4226
4227 barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, nid);
4228
4229 if (!barn) {
4230 ret = -ENOMEM;
4231 goto out;
4232 }
4233
4234 barn_init(barn);
4235 s->per_node[nid].barn = barn;
4236 }
4237 node_set(nid, slab_barn_nodes);
4238
4239 out:
4240 mutex_unlock(&slab_mutex);
4241
4242 return ret;
4243 }
4244
4245 /*
4246 * Use the cpu notifier to insure that the cpu slabs are flushed when
4247 * necessary.
4248 */
slub_cpu_dead(unsigned int cpu)4249 static int slub_cpu_dead(unsigned int cpu)
4250 {
4251 struct kmem_cache *s;
4252
4253 mutex_lock(&slab_mutex);
4254 list_for_each_entry(s, &slab_caches, list) {
4255 if (cache_has_sheaves(s))
4256 __pcs_flush_all_cpu(s, cpu);
4257 }
4258 mutex_unlock(&slab_mutex);
4259 return 0;
4260 }
4261
4262 #ifdef CONFIG_SLUB_DEBUG
count_free(struct slab * slab)4263 static int count_free(struct slab *slab)
4264 {
4265 return slab->objects - slab->inuse;
4266 }
4267
node_nr_objs(struct kmem_cache_node * n)4268 static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
4269 {
4270 return atomic_long_read(&n->total_objects);
4271 }
4272
4273 /* Supports checking bulk free of a constructed freelist */
free_debug_processing(struct kmem_cache * s,struct slab * slab,void * head,void * tail,int * bulk_cnt,unsigned long addr,depot_stack_handle_t handle)4274 static inline bool free_debug_processing(struct kmem_cache *s,
4275 struct slab *slab, void *head, void *tail, int *bulk_cnt,
4276 unsigned long addr, depot_stack_handle_t handle)
4277 {
4278 bool checks_ok = false;
4279 void *object = head;
4280 int cnt = 0;
4281
4282 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
4283 if (!check_slab(s, slab))
4284 goto out;
4285 }
4286
4287 if (slab->inuse < *bulk_cnt) {
4288 slab_err(s, slab, "Slab has %d allocated objects but %d are to be freed\n",
4289 slab->inuse, *bulk_cnt);
4290 goto out;
4291 }
4292
4293 next_object:
4294
4295 if (++cnt > *bulk_cnt)
4296 goto out_cnt;
4297
4298 if (s->flags & SLAB_CONSISTENCY_CHECKS) {
4299 if (!free_consistency_checks(s, slab, object, addr))
4300 goto out;
4301 }
4302
4303 if (s->flags & SLAB_STORE_USER)
4304 set_track_update(s, object, TRACK_FREE, addr, handle);
4305 trace(s, slab, object, 0);
4306 /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
4307 init_object(s, object, SLUB_RED_INACTIVE);
4308
4309 /* Reached end of constructed freelist yet? */
4310 if (object != tail) {
4311 object = get_freepointer(s, object);
4312 goto next_object;
4313 }
4314 checks_ok = true;
4315
4316 out_cnt:
4317 if (cnt != *bulk_cnt) {
4318 slab_err(s, slab, "Bulk free expected %d objects but found %d\n",
4319 *bulk_cnt, cnt);
4320 *bulk_cnt = cnt;
4321 }
4322
4323 out:
4324
4325 if (!checks_ok)
4326 slab_fix(s, "Object at 0x%p not freed", object);
4327
4328 return checks_ok;
4329 }
4330 #endif /* CONFIG_SLUB_DEBUG */
4331
4332 #if defined(CONFIG_SLUB_DEBUG) || defined(SLAB_SUPPORTS_SYSFS)
count_partial(struct kmem_cache_node * n,int (* get_count)(struct slab *))4333 static unsigned long count_partial(struct kmem_cache_node *n,
4334 int (*get_count)(struct slab *))
4335 {
4336 unsigned long flags;
4337 unsigned long x = 0;
4338 struct slab *slab;
4339
4340 spin_lock_irqsave(&n->list_lock, flags);
4341 list_for_each_entry(slab, &n->partial, slab_list)
4342 x += get_count(slab);
4343 spin_unlock_irqrestore(&n->list_lock, flags);
4344 return x;
4345 }
4346 #endif /* CONFIG_SLUB_DEBUG || SLAB_SUPPORTS_SYSFS */
4347
4348 #ifdef CONFIG_SLUB_DEBUG
4349 #define MAX_PARTIAL_TO_SCAN 10000
4350
count_partial_free_approx(struct kmem_cache_node * n)4351 static unsigned long count_partial_free_approx(struct kmem_cache_node *n)
4352 {
4353 unsigned long flags;
4354 unsigned long x = 0;
4355 struct slab *slab;
4356
4357 spin_lock_irqsave(&n->list_lock, flags);
4358 if (n->nr_partial <= MAX_PARTIAL_TO_SCAN) {
4359 list_for_each_entry(slab, &n->partial, slab_list)
4360 x += slab->objects - slab->inuse;
4361 } else {
4362 /*
4363 * For a long list, approximate the total count of objects in
4364 * it to meet the limit on the number of slabs to scan.
4365 * Scan from both the list's head and tail for better accuracy.
4366 */
4367 unsigned long scanned = 0;
4368
4369 list_for_each_entry(slab, &n->partial, slab_list) {
4370 x += slab->objects - slab->inuse;
4371 if (++scanned == MAX_PARTIAL_TO_SCAN / 2)
4372 break;
4373 }
4374 list_for_each_entry_reverse(slab, &n->partial, slab_list) {
4375 x += slab->objects - slab->inuse;
4376 if (++scanned == MAX_PARTIAL_TO_SCAN)
4377 break;
4378 }
4379 x = mult_frac(x, n->nr_partial, scanned);
4380 x = min(x, node_nr_objs(n));
4381 }
4382 spin_unlock_irqrestore(&n->list_lock, flags);
4383 return x;
4384 }
4385
4386 static noinline void
slab_out_of_memory(struct kmem_cache * s,gfp_t gfpflags,int nid)4387 slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
4388 {
4389 static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
4390 DEFAULT_RATELIMIT_BURST);
4391 int cpu = raw_smp_processor_id();
4392 int node;
4393 struct kmem_cache_node *n;
4394
4395 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
4396 return;
4397
4398 pr_warn("SLUB: Unable to allocate memory on CPU %u (of node %d) on node %d, gfp=%#x(%pGg)\n",
4399 cpu, cpu_to_node(cpu), nid, gfpflags, &gfpflags);
4400 pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n",
4401 s->name, s->object_size, s->size, oo_order(s->oo),
4402 oo_order(s->min));
4403
4404 if (oo_order(s->min) > get_order(s->object_size))
4405 pr_warn(" %s debugging increased min order, use slab_debug=O to disable.\n",
4406 s->name);
4407
4408 for_each_kmem_cache_node(s, node, n) {
4409 unsigned long nr_slabs;
4410 unsigned long nr_objs;
4411 unsigned long nr_free;
4412
4413 nr_free = count_partial_free_approx(n);
4414 nr_slabs = node_nr_slabs(n);
4415 nr_objs = node_nr_objs(n);
4416
4417 pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
4418 node, nr_slabs, nr_objs, nr_free);
4419 }
4420 }
4421 #else /* CONFIG_SLUB_DEBUG */
4422 static inline void
slab_out_of_memory(struct kmem_cache * s,gfp_t gfpflags,int nid)4423 slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid) { }
4424 #endif
4425
pfmemalloc_match(struct slab * slab,gfp_t gfpflags)4426 static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags)
4427 {
4428 if (unlikely(slab_test_pfmemalloc(slab)))
4429 return gfp_pfmemalloc_allowed(gfpflags);
4430
4431 return true;
4432 }
4433
4434 /*
4435 * Get the slab's freelist and do not freeze it.
4436 *
4437 * Assumes the slab is isolated from node partial list and not frozen.
4438 *
4439 * Assumes this is performed only for caches without debugging so we
4440 * don't need to worry about adding the slab to the full list.
4441 */
get_freelist_nofreeze(struct kmem_cache * s,struct slab * slab,unsigned int * count)4442 static inline void *get_freelist_nofreeze(struct kmem_cache *s, struct slab *slab,
4443 unsigned int *count)
4444 {
4445 struct freelist_counters old, new;
4446
4447 do {
4448 old.freelist = slab->freelist;
4449 old.counters = slab->counters;
4450
4451 new.freelist = NULL;
4452 new.counters = old.counters;
4453 VM_WARN_ON_ONCE(new.frozen);
4454
4455 new.inuse = old.objects;
4456
4457 } while (!slab_update_freelist(s, slab, &old, &new, "get_freelist_nofreeze"));
4458
4459 *count = old.objects - old.inuse;
4460 return old.freelist;
4461 }
4462
4463 /*
4464 * If the object has been wiped upon free, make sure it's fully initialized by
4465 * zeroing out freelist pointer.
4466 *
4467 * Note that we also wipe custom freelist pointers.
4468 */
maybe_wipe_obj_freeptr(struct kmem_cache * s,void * obj)4469 static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s,
4470 void *obj)
4471 {
4472 if (unlikely(slab_want_init_on_free(s)) && obj &&
4473 !freeptr_outside_object(s))
4474 memset((void *)((char *)kasan_reset_tag(obj) + s->offset),
4475 0, sizeof(void *));
4476 }
4477
alloc_from_new_slab(struct kmem_cache * s,struct slab * slab,void ** p,unsigned int count,bool allow_spin)4478 static unsigned int alloc_from_new_slab(struct kmem_cache *s, struct slab *slab,
4479 void **p, unsigned int count, bool allow_spin)
4480 {
4481 unsigned int allocated = 0;
4482 struct slab_obj_iter iter;
4483 bool needs_add_partial = true;
4484 unsigned long flags;
4485
4486 /*
4487 * Are we going to put the slab on the partial list?
4488 * Note slab->inuse is 0 on a new slab.
4489 */
4490 if (count >= slab->objects) {
4491 needs_add_partial = false;
4492 count = slab->objects;
4493 }
4494
4495 init_slab_obj_iter(s, slab, &iter, allow_spin);
4496
4497 while (allocated < count) {
4498 p[allocated] = next_slab_obj(s, &iter);
4499 allocated++;
4500 }
4501 slab->inuse = count;
4502 build_slab_freelist(s, slab, &iter);
4503
4504 if (needs_add_partial) {
4505 struct kmem_cache_node *n = get_node(s, slab_nid(slab));
4506
4507 if (allow_spin) {
4508 spin_lock_irqsave(&n->list_lock, flags);
4509 } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
4510 /*
4511 * Unlucky, discard newly allocated slab.
4512 * The slab is not fully free, but it's fine as
4513 * objects are not allocated to users.
4514 */
4515 free_new_slab_nolock(s, slab);
4516 return 0;
4517 }
4518 add_partial(n, slab, ADD_TO_HEAD);
4519 spin_unlock_irqrestore(&n->list_lock, flags);
4520 }
4521
4522 inc_slabs_node(s, slab_nid(slab), slab->objects);
4523 return allocated;
4524 }
4525
4526 /*
4527 * Slow path. We failed to allocate via percpu sheaves or they are not available
4528 * due to bootstrap or debugging enabled or SLUB_TINY.
4529 *
4530 * We try to allocate from partial slab lists and fall back to allocating a new
4531 * slab.
4532 */
___slab_alloc(struct kmem_cache * s,gfp_t gfpflags,int node,const struct slab_alloc_context * ac)4533 static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
4534 const struct slab_alloc_context *ac)
4535 {
4536 bool allow_spin = alloc_flags_allow_spinning(ac->alloc_flags);
4537 gfp_t trynode_flags;
4538 void *object;
4539 struct slab *slab;
4540 bool try_thisnode = true;
4541
4542 stat(s, ALLOC_SLOWPATH);
4543
4544 new_objects:
4545
4546 trynode_flags = gfpflags;
4547 /*
4548 * When a preferred node is indicated but no __GFP_THISNODE
4549 *
4550 * 1) try to get a partial slab from target node only by having
4551 * __GFP_THISNODE in trynode_flags for get_from_partial()
4552 * 2) if 1) failed, try to allocate a new slab from target node with
4553 * (at most) GFP_NOWAIT | __GFP_THISNODE opportunistically
4554 * 3) if 2) failed, retry with original gfpflags which will allow
4555 * get_from_partial() try partial lists of other nodes before
4556 * potentially allocating new page from other nodes
4557 */
4558 if (unlikely(node != NUMA_NO_NODE && !(gfpflags & __GFP_THISNODE)
4559 && try_thisnode)) {
4560 trynode_flags &= GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_ACCOUNT;
4561 trynode_flags |= __GFP_NOWARN | __GFP_THISNODE;
4562 }
4563
4564 object = get_from_partial(s, node, trynode_flags, ac);
4565 if (object)
4566 goto success;
4567
4568 slab = new_slab(s, trynode_flags, ac->alloc_flags, node);
4569
4570 if (unlikely(!slab)) {
4571 if (node != NUMA_NO_NODE && !(gfpflags & __GFP_THISNODE)
4572 && try_thisnode) {
4573 try_thisnode = false;
4574 goto new_objects;
4575 }
4576 slab_out_of_memory(s, gfpflags, node);
4577 return NULL;
4578 }
4579
4580 stat(s, ALLOC_SLAB);
4581
4582 if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) {
4583 object = alloc_single_from_new_slab(s, slab, ac);
4584
4585 if (likely(object))
4586 goto success;
4587 } else {
4588 /* we don't need to check SLAB_STORE_USER here */
4589 if (alloc_from_new_slab(s, slab, &object, 1, allow_spin))
4590 return object;
4591 }
4592
4593 if (allow_spin)
4594 goto new_objects;
4595
4596 /* This could cause an endless loop. Fail instead. */
4597 return NULL;
4598
4599 success:
4600 if (kmem_cache_debug_flags(s, SLAB_STORE_USER))
4601 set_track(s, object, TRACK_ALLOC, ac->caller_addr, gfpflags);
4602
4603 return object;
4604 }
4605
apply_strict_numa_policy(int node)4606 static __always_inline int apply_strict_numa_policy(int node)
4607 {
4608 #ifdef CONFIG_NUMA
4609 if (static_branch_unlikely(&strict_numa) &&
4610 node == NUMA_NO_NODE) {
4611
4612 struct mempolicy *mpol = current->mempolicy;
4613
4614 if (mpol) {
4615 /*
4616 * Special BIND rule support. If the local node
4617 * is in permitted set then do not redirect
4618 * to a particular node.
4619 * Otherwise we apply the memory policy to get
4620 * the node we need to allocate on.
4621 */
4622 if (mpol->mode != MPOL_BIND ||
4623 !node_isset(numa_mem_id(), mpol->nodes))
4624 node = mempolicy_slab_node();
4625 }
4626 }
4627 #endif
4628 return node;
4629 }
4630
4631 static __fastpath_inline
slab_pre_alloc_hook(struct kmem_cache * s,gfp_t flags)4632 struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
4633 {
4634 flags &= gfp_allowed_mask;
4635
4636 might_alloc(flags);
4637
4638 if (unlikely(should_failslab(s, flags)))
4639 return NULL;
4640
4641 return s;
4642 }
4643
4644 static __fastpath_inline
slab_post_alloc_hook(struct kmem_cache * s,gfp_t flags,size_t size,void ** p,const struct slab_alloc_context * ac)4645 bool slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, size_t size,
4646 void **p, const struct slab_alloc_context *ac)
4647 {
4648 bool init = slab_want_init_on_alloc(flags, s);
4649 unsigned int zero_size = s->object_size;
4650 gfp_t init_flags = flags & gfp_allowed_mask;
4651 bool kasan_init = false;
4652
4653 /*
4654 * For kmalloc object, the allocated size (object_size) can be larger
4655 * than the requested size (orig_size). We however need to zero the
4656 * whole object_size to handle possible later krealloc() with
4657 *__GFP_ZERO properly.
4658 *
4659 * But if we keep track of the requested size, krealloc() uses that
4660 * information. Additionally if red zoning is enabled, the extra space
4661 * is also red zone, so we should not overwrite it. So limit zeroing to
4662 * orig_size if we track it.
4663 */
4664 if (slub_debug_orig_size(s))
4665 zero_size = ac->orig_size;
4666
4667 /*
4668 * ARM64 can set memory tags and zero the memory using a single
4669 * instruction. Since HW_TAGS KASAN uses that while tagging the object,
4670 * separate zeroing is unnecessary.
4671 *
4672 * However, KASAN never zeroes memory when slab_debug is enabled to
4673 * avoid overwriting SLUB redzones. This does not lead to a performance
4674 * penalty on production builds, as slab_debug is not intended to be
4675 * enabled there.
4676 */
4677 if (kasan_has_integrated_init() && !__slub_debug_enabled()) {
4678 kasan_init = init;
4679 init = false;
4680 }
4681
4682 for (size_t i = 0; i < size; i++) {
4683 p[i] = kasan_slab_alloc(s, p[i], init_flags, kasan_init);
4684
4685 /*
4686 * memset and hooks come after KASAN as p[i] might get tagged
4687 *
4688 * kfence zeroes the object instead of SLUB to avoid overwriting
4689 * its own redzone starting at orig_size, which could happen
4690 * with SLUB zeroing full s->object_size
4691 */
4692 if (init && p[i] && !is_kfence_address(p[i]))
4693 memset(p[i], 0, zero_size);
4694
4695 if (alloc_flags_allow_spinning(ac->alloc_flags))
4696 kmemleak_alloc_recursive(p[i], s->object_size, 1,
4697 s->flags, init_flags);
4698 kmsan_slab_alloc(s, p[i], init_flags);
4699 alloc_tagging_slab_alloc_hook(s, p[i], flags, ac->alloc_flags);
4700 }
4701
4702 return memcg_slab_post_alloc_hook(s, flags, size, p, ac);
4703 }
4704
4705 /*
4706 * Replace the empty main sheaf with a (at least partially) full sheaf.
4707 *
4708 * Must be called with the cpu_sheaves local lock locked. If successful, returns
4709 * the pcs pointer and the local lock locked (possibly on a different cpu than
4710 * initially called). If not successful, returns NULL and the local lock
4711 * unlocked.
4712 */
4713 static struct slub_percpu_sheaves *
__pcs_replace_empty_main(struct kmem_cache * s,struct slub_percpu_sheaves * pcs,gfp_t gfp,unsigned int alloc_flags)4714 __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
4715 gfp_t gfp, unsigned int alloc_flags)
4716 {
4717 struct slab_sheaf *empty = NULL;
4718 struct slab_sheaf *full;
4719 struct node_barn *barn;
4720 bool allow_spin;
4721
4722 slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
4723
4724 /* Bootstrap or debug cache, back off */
4725 if (unlikely(!cache_has_sheaves(s))) {
4726 local_unlock(&s->cpu_sheaves->lock);
4727 return NULL;
4728 }
4729
4730 if (pcs->spare && pcs->spare->size > 0) {
4731 swap(pcs->main, pcs->spare);
4732 return pcs;
4733 }
4734
4735 barn = get_barn(s);
4736 if (!barn) {
4737 local_unlock(&s->cpu_sheaves->lock);
4738 return NULL;
4739 }
4740
4741 allow_spin = alloc_flags_allow_spinning(alloc_flags);
4742
4743 full = barn_replace_empty_sheaf(barn, pcs->main, allow_spin);
4744
4745 if (full) {
4746 stat(s, BARN_GET);
4747 pcs->main = full;
4748 return pcs;
4749 }
4750
4751 stat(s, BARN_GET_FAIL);
4752
4753 if (allow_spin) {
4754 if (pcs->spare) {
4755 empty = pcs->spare;
4756 pcs->spare = NULL;
4757 } else {
4758 empty = barn_get_empty_sheaf(barn, true);
4759 }
4760 }
4761
4762 local_unlock(&s->cpu_sheaves->lock);
4763 pcs = NULL;
4764
4765 if (!allow_spin)
4766 return NULL;
4767
4768 if (!empty) {
4769 empty = alloc_empty_sheaf(s, gfp, alloc_flags);
4770 if (!empty)
4771 return NULL;
4772 }
4773
4774 if (refill_sheaf(s, empty, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
4775 /*
4776 * we must be very low on memory so don't bother
4777 * with the barn
4778 */
4779 sheaf_flush_unused(s, empty);
4780 free_empty_sheaf(s, empty);
4781
4782 return NULL;
4783 }
4784
4785 full = empty;
4786 empty = NULL;
4787
4788 if (!local_trylock(&s->cpu_sheaves->lock))
4789 goto barn_put;
4790 pcs = this_cpu_ptr(s->cpu_sheaves);
4791
4792 /*
4793 * If we put any empty or full sheaf to the barn below, it's due to
4794 * racing or being migrated to a different cpu. Breaching the barn's
4795 * sheaf limits should be thus rare enough so just ignore them to
4796 * simplify the recovery.
4797 */
4798
4799 if (pcs->main->size == 0) {
4800 if (!pcs->spare)
4801 pcs->spare = pcs->main;
4802 else
4803 barn_put_empty_sheaf(barn, pcs->main);
4804 pcs->main = full;
4805 return pcs;
4806 }
4807
4808 if (!pcs->spare) {
4809 pcs->spare = full;
4810 return pcs;
4811 }
4812
4813 if (pcs->spare->size == 0) {
4814 barn_put_empty_sheaf(barn, pcs->spare);
4815 pcs->spare = full;
4816 return pcs;
4817 }
4818
4819 barn_put:
4820 barn_put_full_sheaf(barn, full);
4821 stat(s, BARN_PUT);
4822
4823 return pcs;
4824 }
4825
4826 static __fastpath_inline
alloc_from_pcs(struct kmem_cache * s,gfp_t gfp,unsigned int alloc_flags,int node)4827 void *alloc_from_pcs(struct kmem_cache *s, gfp_t gfp, unsigned int alloc_flags, int node)
4828 {
4829 struct slub_percpu_sheaves *pcs;
4830 bool node_requested;
4831 void *object;
4832
4833 node_requested = IS_ENABLED(CONFIG_NUMA) && node != NUMA_NO_NODE;
4834
4835 /*
4836 * We assume the percpu sheaves contain only local objects although it's
4837 * not completely guaranteed, so we verify later.
4838 */
4839 if (unlikely(node_requested && node != numa_mem_id())) {
4840 stat(s, ALLOC_NODE_MISMATCH);
4841 return NULL;
4842 }
4843
4844 if (!local_trylock(&s->cpu_sheaves->lock))
4845 return NULL;
4846
4847 pcs = this_cpu_ptr(s->cpu_sheaves);
4848
4849 if (unlikely(pcs->main->size == 0)) {
4850 pcs = __pcs_replace_empty_main(s, pcs, gfp, alloc_flags);
4851 if (unlikely(!pcs))
4852 return NULL;
4853 }
4854
4855 object = pcs->main->objects[pcs->main->size - 1];
4856
4857 if (unlikely(node_requested)) {
4858 /*
4859 * Verify that the object was from the node we want. This could
4860 * be false because of cpu migration during an unlocked part of
4861 * the current allocation or previous freeing process.
4862 */
4863 if (page_to_nid(virt_to_page(object)) != node) {
4864 local_unlock(&s->cpu_sheaves->lock);
4865 stat(s, ALLOC_NODE_MISMATCH);
4866 return NULL;
4867 }
4868 }
4869
4870 pcs->main->size--;
4871
4872 local_unlock(&s->cpu_sheaves->lock);
4873
4874 stat(s, ALLOC_FASTPATH);
4875
4876 return object;
4877 }
4878
4879 static __fastpath_inline
alloc_from_pcs_bulk(struct kmem_cache * s,size_t size,void ** p)4880 unsigned int alloc_from_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
4881 {
4882 struct slub_percpu_sheaves *pcs;
4883 struct slab_sheaf *main;
4884 unsigned int allocated = 0;
4885 unsigned int batch;
4886
4887 next_batch:
4888 if (!local_trylock(&s->cpu_sheaves->lock))
4889 return allocated;
4890
4891 pcs = this_cpu_ptr(s->cpu_sheaves);
4892
4893 if (unlikely(pcs->main->size == 0)) {
4894
4895 struct slab_sheaf *full;
4896 struct node_barn *barn;
4897
4898 if (unlikely(!cache_has_sheaves(s))) {
4899 local_unlock(&s->cpu_sheaves->lock);
4900 return allocated;
4901 }
4902
4903 if (pcs->spare && pcs->spare->size > 0) {
4904 swap(pcs->main, pcs->spare);
4905 goto do_alloc;
4906 }
4907
4908 barn = get_barn(s);
4909 if (!barn) {
4910 local_unlock(&s->cpu_sheaves->lock);
4911 return allocated;
4912 }
4913
4914 full = barn_replace_empty_sheaf(barn, pcs->main,
4915 /* allow_spin = */ true);
4916
4917 if (full) {
4918 stat(s, BARN_GET);
4919 pcs->main = full;
4920 goto do_alloc;
4921 }
4922
4923 stat(s, BARN_GET_FAIL);
4924
4925 local_unlock(&s->cpu_sheaves->lock);
4926
4927 /*
4928 * Once full sheaves in barn are depleted, let the bulk
4929 * allocation continue from slab pages, otherwise we would just
4930 * be copying arrays of pointers twice.
4931 */
4932 return allocated;
4933 }
4934
4935 do_alloc:
4936
4937 main = pcs->main;
4938 batch = min(size, main->size);
4939
4940 main->size -= batch;
4941 memcpy(p, main->objects + main->size, batch * sizeof(void *));
4942
4943 local_unlock(&s->cpu_sheaves->lock);
4944
4945 stat_add(s, ALLOC_FASTPATH, batch);
4946
4947 allocated += batch;
4948
4949 if (batch < size) {
4950 p += batch;
4951 size -= batch;
4952 goto next_batch;
4953 }
4954
4955 return allocated;
4956 }
4957
4958
4959 /*
4960 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
4961 * have the fastpath folded into their functions. So no function call
4962 * overhead for requests that can be satisfied on the fastpath.
4963 *
4964 * The fastpath works by first checking if the lockless freelist can be used.
4965 * If not then __slab_alloc is called for slow processing.
4966 *
4967 * Otherwise we can simply pick the next object from the lockless free list.
4968 */
slab_alloc_node(struct kmem_cache * s,gfp_t gfpflags,int node,const struct slab_alloc_context * ac)4969 static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s,
4970 gfp_t gfpflags, int node, const struct slab_alloc_context *ac)
4971 {
4972 void *object;
4973
4974 s = slab_pre_alloc_hook(s, gfpflags);
4975 if (unlikely(!s))
4976 return NULL;
4977
4978 object = kfence_alloc(s, ac->orig_size, gfpflags);
4979 if (unlikely(object))
4980 goto out;
4981
4982 node = apply_strict_numa_policy(node);
4983
4984 object = alloc_from_pcs(s, gfpflags, ac->alloc_flags, node);
4985
4986 if (unlikely(!object))
4987 object = ___slab_alloc(s, gfpflags, node, ac);
4988
4989 maybe_wipe_obj_freeptr(s, object);
4990
4991 out:
4992 /*
4993 * In case this fails due to memcg_slab_post_alloc_hook(),
4994 * object is set to NULL
4995 */
4996 slab_post_alloc_hook(s, gfpflags, 1, &object, ac);
4997
4998 return object;
4999 }
5000
kmem_cache_alloc_noprof(struct kmem_cache * s,gfp_t gfpflags)5001 void *kmem_cache_alloc_noprof(struct kmem_cache *s, gfp_t gfpflags)
5002 {
5003 void *ret;
5004 const struct slab_alloc_context ac = {
5005 .caller_addr = _RET_IP_,
5006 .orig_size = s->object_size,
5007 .alloc_flags = SLAB_ALLOC_DEFAULT,
5008 };
5009
5010 ret = slab_alloc_node(s, gfpflags, NUMA_NO_NODE, &ac);
5011
5012 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, NUMA_NO_NODE);
5013
5014 return ret;
5015 }
5016 EXPORT_SYMBOL(kmem_cache_alloc_noprof);
5017
kmem_cache_alloc_lru_noprof(struct kmem_cache * s,struct list_lru * lru,gfp_t gfpflags)5018 void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
5019 gfp_t gfpflags)
5020 {
5021 void *ret;
5022 const struct slab_alloc_context ac = {
5023 .caller_addr = _RET_IP_,
5024 .orig_size = s->object_size,
5025 .alloc_flags = SLAB_ALLOC_DEFAULT,
5026 .lru = lru,
5027 };
5028
5029 ret = slab_alloc_node(s, gfpflags, NUMA_NO_NODE, &ac);
5030
5031 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, NUMA_NO_NODE);
5032
5033 return ret;
5034 }
5035 EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof);
5036
kmem_cache_charge(void * objp,gfp_t gfpflags)5037 bool kmem_cache_charge(void *objp, gfp_t gfpflags)
5038 {
5039 if (!memcg_kmem_online())
5040 return true;
5041
5042 return memcg_slab_post_charge(objp, gfpflags);
5043 }
5044 EXPORT_SYMBOL(kmem_cache_charge);
5045
5046 /**
5047 * kmem_cache_alloc_node - Allocate an object on the specified node
5048 * @s: The cache to allocate from.
5049 * @gfpflags: See kmalloc().
5050 * @node: node number of the target node.
5051 *
5052 * Identical to kmem_cache_alloc but it will allocate memory on the given
5053 * node, which can improve the performance for cpu bound structures.
5054 *
5055 * Fallback to other node is possible if __GFP_THISNODE is not set.
5056 *
5057 * Return: pointer to the new object or %NULL in case of error
5058 */
kmem_cache_alloc_node_noprof(struct kmem_cache * s,gfp_t gfpflags,int node)5059 void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t gfpflags, int node)
5060 {
5061 void *ret;
5062 const struct slab_alloc_context ac = {
5063 .caller_addr = _RET_IP_,
5064 .orig_size = s->object_size,
5065 .alloc_flags = SLAB_ALLOC_DEFAULT,
5066 };
5067
5068 ret = slab_alloc_node(s, gfpflags, node, &ac);
5069
5070 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, node);
5071
5072 return ret;
5073 }
5074 EXPORT_SYMBOL(kmem_cache_alloc_node_noprof);
5075
__prefill_sheaf_pfmemalloc(struct kmem_cache * s,struct slab_sheaf * sheaf,gfp_t gfp)5076 static int __prefill_sheaf_pfmemalloc(struct kmem_cache *s,
5077 struct slab_sheaf *sheaf, gfp_t gfp)
5078 {
5079 gfp_t gfp_nomemalloc;
5080 int ret;
5081
5082 gfp_nomemalloc = gfp | __GFP_NOMEMALLOC;
5083 if (gfp_pfmemalloc_allowed(gfp))
5084 gfp_nomemalloc |= __GFP_NOWARN;
5085
5086 ret = refill_sheaf(s, sheaf, gfp_nomemalloc);
5087
5088 if (likely(!ret || !gfp_pfmemalloc_allowed(gfp)))
5089 return ret;
5090
5091 /*
5092 * if we are allowed to, refill sheaf with pfmemalloc but then remember
5093 * it for when it's returned
5094 */
5095 ret = refill_sheaf(s, sheaf, gfp);
5096 sheaf->pfmemalloc = true;
5097
5098 return ret;
5099 }
5100
5101 static bool __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags,
5102 size_t size, void **p);
5103
5104 /*
5105 * returns a sheaf that has at least the requested size
5106 * when prefilling is needed, do so with given gfp flags
5107 *
5108 * return NULL if sheaf allocation or prefilling failed
5109 */
5110 struct slab_sheaf *
kmem_cache_prefill_sheaf(struct kmem_cache * s,gfp_t gfp,unsigned int size)5111 kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
5112 {
5113 struct slub_percpu_sheaves *pcs;
5114 struct slab_sheaf *sheaf = NULL;
5115 struct node_barn *barn;
5116
5117 if (unlikely(!size))
5118 return NULL;
5119
5120 if (unlikely(size > s->sheaf_capacity)) {
5121
5122 sheaf = __alloc_empty_sheaf(s, gfp, SLAB_ALLOC_DEFAULT, size);
5123 if (!sheaf)
5124 return NULL;
5125
5126 stat(s, SHEAF_PREFILL_OVERSIZE);
5127 sheaf->capacity = size;
5128
5129 /*
5130 * we do not need to care about pfmemalloc here because oversize
5131 * sheaves are always flushed and freed when returned
5132 */
5133 if (!__kmem_cache_alloc_bulk(s, gfp, size,
5134 &sheaf->objects[0])) {
5135 free_empty_sheaf(s, sheaf);
5136 return NULL;
5137 }
5138
5139 sheaf->size = size;
5140
5141 return sheaf;
5142 }
5143
5144 local_lock(&s->cpu_sheaves->lock);
5145 pcs = this_cpu_ptr(s->cpu_sheaves);
5146
5147 if (pcs->spare) {
5148 sheaf = pcs->spare;
5149 pcs->spare = NULL;
5150 stat(s, SHEAF_PREFILL_FAST);
5151 } else {
5152 barn = get_barn(s);
5153
5154 stat(s, SHEAF_PREFILL_SLOW);
5155 if (barn)
5156 sheaf = barn_get_full_or_empty_sheaf(barn);
5157 if (sheaf && sheaf->size)
5158 stat(s, BARN_GET);
5159 else
5160 stat(s, BARN_GET_FAIL);
5161 }
5162
5163 local_unlock(&s->cpu_sheaves->lock);
5164
5165
5166 if (!sheaf)
5167 sheaf = alloc_empty_sheaf(s, gfp, SLAB_ALLOC_DEFAULT);
5168
5169 if (sheaf) {
5170 sheaf->capacity = s->sheaf_capacity;
5171 sheaf->pfmemalloc = false;
5172
5173 if (sheaf->size < size &&
5174 __prefill_sheaf_pfmemalloc(s, sheaf, gfp)) {
5175 sheaf_flush_unused(s, sheaf);
5176 free_empty_sheaf(s, sheaf);
5177 sheaf = NULL;
5178 }
5179 }
5180
5181 return sheaf;
5182 }
5183
5184 /*
5185 * Use this to return a sheaf obtained by kmem_cache_prefill_sheaf()
5186 *
5187 * If the sheaf cannot simply become the percpu spare sheaf, but there's space
5188 * for a full sheaf in the barn, we try to refill the sheaf back to the cache's
5189 * sheaf_capacity to avoid handling partially full sheaves.
5190 *
5191 * If the refill fails because gfp is e.g. GFP_NOWAIT, or the barn is full, the
5192 * sheaf is instead flushed and freed.
5193 */
kmem_cache_return_sheaf(struct kmem_cache * s,gfp_t gfp,struct slab_sheaf * sheaf)5194 void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
5195 struct slab_sheaf *sheaf)
5196 {
5197 struct slub_percpu_sheaves *pcs;
5198 struct node_barn *barn;
5199
5200 if (unlikely((sheaf->capacity != s->sheaf_capacity)
5201 || sheaf->pfmemalloc)) {
5202 sheaf_flush_unused(s, sheaf);
5203 free_empty_sheaf(s, sheaf);
5204 return;
5205 }
5206
5207 local_lock(&s->cpu_sheaves->lock);
5208 pcs = this_cpu_ptr(s->cpu_sheaves);
5209 barn = get_barn(s);
5210
5211 if (!pcs->spare) {
5212 pcs->spare = sheaf;
5213 sheaf = NULL;
5214 stat(s, SHEAF_RETURN_FAST);
5215 }
5216
5217 local_unlock(&s->cpu_sheaves->lock);
5218
5219 if (!sheaf)
5220 return;
5221
5222 stat(s, SHEAF_RETURN_SLOW);
5223
5224 /*
5225 * If the barn has too many full sheaves or we fail to refill the sheaf,
5226 * simply flush and free it.
5227 */
5228 if (!barn || data_race(barn->nr_full) >= MAX_FULL_SHEAVES ||
5229 refill_sheaf(s, sheaf, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
5230 sheaf_flush_unused(s, sheaf);
5231 free_empty_sheaf(s, sheaf);
5232 return;
5233 }
5234
5235 barn_put_full_sheaf(barn, sheaf);
5236 stat(s, BARN_PUT);
5237 }
5238
5239 /*
5240 * Refill a sheaf previously returned by kmem_cache_prefill_sheaf to at least
5241 * the given size.
5242 *
5243 * Return: 0 on success. The sheaf will contain at least @size objects.
5244 * The sheaf might have been replaced with a new one if more than
5245 * sheaf->capacity objects are requested.
5246 *
5247 * Return: -ENOMEM on failure. Some objects might have been added to the sheaf
5248 * but the sheaf will not be replaced.
5249 *
5250 * In practice we always refill to full sheaf's capacity.
5251 */
kmem_cache_refill_sheaf(struct kmem_cache * s,gfp_t gfp,struct slab_sheaf ** sheafp,unsigned int size)5252 int kmem_cache_refill_sheaf(struct kmem_cache *s, gfp_t gfp,
5253 struct slab_sheaf **sheafp, unsigned int size)
5254 {
5255 struct slab_sheaf *sheaf;
5256
5257 /*
5258 * TODO: do we want to support *sheaf == NULL to be equivalent of
5259 * kmem_cache_prefill_sheaf() ?
5260 */
5261 if (!sheafp || !(*sheafp))
5262 return -EINVAL;
5263
5264 sheaf = *sheafp;
5265 if (sheaf->size >= size)
5266 return 0;
5267
5268 if (likely(sheaf->capacity >= size)) {
5269 if (likely(sheaf->capacity == s->sheaf_capacity))
5270 return __prefill_sheaf_pfmemalloc(s, sheaf, gfp);
5271
5272 if (!__kmem_cache_alloc_bulk(s, gfp, sheaf->capacity - sheaf->size,
5273 &sheaf->objects[sheaf->size]))
5274 return -ENOMEM;
5275 sheaf->size = sheaf->capacity;
5276
5277 return 0;
5278 }
5279
5280 /*
5281 * We had a regular sized sheaf and need an oversize one, or we had an
5282 * oversize one already but need a larger one now.
5283 * This should be a very rare path so let's not complicate it.
5284 */
5285 sheaf = kmem_cache_prefill_sheaf(s, gfp, size);
5286 if (!sheaf)
5287 return -ENOMEM;
5288
5289 kmem_cache_return_sheaf(s, gfp, *sheafp);
5290 *sheafp = sheaf;
5291 return 0;
5292 }
5293
5294 /*
5295 * Allocate from a sheaf obtained by kmem_cache_prefill_sheaf()
5296 *
5297 * Guaranteed not to fail as many allocations as was the requested size.
5298 * After the sheaf is emptied, it fails - no fallback to the slab cache itself.
5299 *
5300 * The gfp parameter is meant only to specify __GFP_ZERO or __GFP_ACCOUNT
5301 * memcg charging is forced over limit if necessary, to avoid failure.
5302 *
5303 * It is possible that the allocation comes from kfence and then the sheaf
5304 * size is not decreased.
5305 */
5306 void *
kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache * s,gfp_t gfp,struct slab_sheaf * sheaf)5307 kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache *s, gfp_t gfp,
5308 struct slab_sheaf *sheaf)
5309 {
5310 void *ret = NULL;
5311 const struct slab_alloc_context ac = {
5312 .orig_size = s->object_size,
5313 .alloc_flags = SLAB_ALLOC_DEFAULT,
5314 };
5315
5316 if (sheaf->size == 0)
5317 goto out;
5318
5319 ret = kfence_alloc(s, s->object_size, gfp);
5320
5321 if (likely(!ret))
5322 ret = sheaf->objects[--sheaf->size];
5323
5324 /* add __GFP_NOFAIL to force successful memcg charging */
5325 slab_post_alloc_hook(s, gfp | __GFP_NOFAIL, 1, &ret, &ac);
5326 out:
5327 trace_kmem_cache_alloc(_RET_IP_, ret, s, gfp, NUMA_NO_NODE);
5328
5329 return ret;
5330 }
5331
kmem_cache_sheaf_size(struct slab_sheaf * sheaf)5332 unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf)
5333 {
5334 return sheaf->size;
5335 }
5336 /*
5337 * To avoid unnecessary overhead, we pass through large allocation requests
5338 * directly to the page allocator. We use __GFP_COMP, because we will need to
5339 * know the allocation order to free the pages properly in kfree.
5340 */
___kmalloc_large_node(size_t size,gfp_t flags,int node)5341 static void *___kmalloc_large_node(size_t size, gfp_t flags, int node)
5342 {
5343 struct page *page;
5344 void *ptr = NULL;
5345 unsigned int order = get_order(size);
5346
5347 if (unlikely(flags & GFP_SLAB_BUG_MASK))
5348 flags = kmalloc_fix_flags(flags);
5349
5350 flags |= __GFP_COMP;
5351
5352 if (node == NUMA_NO_NODE)
5353 page = alloc_frozen_pages_noprof(flags, order);
5354 else
5355 page = __alloc_frozen_pages_noprof(flags, order, node, NULL,
5356 ALLOC_DEFAULT);
5357
5358 if (page) {
5359 ptr = page_address(page);
5360 mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
5361 PAGE_SIZE << order);
5362 __SetPageLargeKmalloc(page);
5363 }
5364
5365 ptr = kasan_kmalloc_large(ptr, size, flags);
5366 /* As ptr might get tagged, call kmemleak hook after KASAN. */
5367 kmemleak_alloc(ptr, size, 1, flags);
5368 kmsan_kmalloc_large(ptr, size, flags);
5369
5370 return ptr;
5371 }
5372
__kmalloc_large_noprof(size_t size,gfp_t flags)5373 void *__kmalloc_large_noprof(size_t size, gfp_t flags)
5374 {
5375 void *ret = ___kmalloc_large_node(size, flags, NUMA_NO_NODE);
5376
5377 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size),
5378 flags, NUMA_NO_NODE);
5379 return ret;
5380 }
5381 EXPORT_SYMBOL(__kmalloc_large_noprof);
5382
__kmalloc_large_node_noprof(size_t size,gfp_t flags,int node)5383 void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
5384 {
5385 void *ret = ___kmalloc_large_node(size, flags, node);
5386
5387 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << get_order(size),
5388 flags, node);
5389 return ret;
5390 }
5391 EXPORT_SYMBOL(__kmalloc_large_node_noprof);
5392
5393 static __always_inline
__do_kmalloc_node(kmem_buckets * b,gfp_t flags,int node,kmalloc_token_t token,const struct slab_alloc_context * ac)5394 void *__do_kmalloc_node(kmem_buckets *b, gfp_t flags, int node,
5395 kmalloc_token_t token, const struct slab_alloc_context *ac)
5396 {
5397 const size_t size = ac->orig_size;
5398 struct kmem_cache *s;
5399 void *ret;
5400
5401 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
5402 ret = __kmalloc_large_node_noprof(size, flags, node);
5403 trace_kmalloc(ac->caller_addr, ret, size,
5404 PAGE_SIZE << get_order(size), flags, node);
5405 return ret;
5406 }
5407
5408 if (unlikely(!size))
5409 return ZERO_SIZE_PTR;
5410
5411 s = kmalloc_slab(size, b, flags, token, ac->alloc_flags);
5412
5413 ret = slab_alloc_node(s, flags, node, ac);
5414 ret = kasan_kmalloc(s, ret, size, flags);
5415 trace_kmalloc(ac->caller_addr, ret, size, s->size, flags, node);
5416 return ret;
5417 }
__kmalloc_node_noprof(DECL_KMALLOC_PARAMS (size,b,token),gfp_t flags,int node)5418 void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node)
5419 {
5420 const struct slab_alloc_context ac = {
5421 .caller_addr = _RET_IP_,
5422 .orig_size = size,
5423 .alloc_flags = SLAB_ALLOC_DEFAULT,
5424 };
5425
5426 return __do_kmalloc_node(PASS_BUCKET_PARAM(b), flags, node,
5427 PASS_TOKEN_PARAM(token), &ac);
5428 }
5429 EXPORT_SYMBOL(__kmalloc_node_noprof);
5430
__kmalloc_noprof(DECL_TOKEN_PARAMS (size,token),gfp_t flags)5431 void *__kmalloc_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags)
5432 {
5433 const struct slab_alloc_context ac = {
5434 .caller_addr = _RET_IP_,
5435 .orig_size = size,
5436 .alloc_flags = SLAB_ALLOC_DEFAULT,
5437 };
5438
5439 return __do_kmalloc_node(NULL, flags, NUMA_NO_NODE,
5440 PASS_TOKEN_PARAM(token), &ac);
5441 }
5442 EXPORT_SYMBOL(__kmalloc_noprof);
5443
__kmalloc_nolock_noprof(DECL_TOKEN_PARAMS (size,token),gfp_t gfp_flags,int node,const struct slab_alloc_context * ac)5444 static void *__kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_flags,
5445 int node, const struct slab_alloc_context *ac)
5446 {
5447 struct kmem_cache *s;
5448 bool can_retry = true;
5449 void *ret;
5450
5451 VM_WARN_ON_ONCE(alloc_flags_allow_spinning(ac->alloc_flags));
5452 VM_WARN_ON_ONCE(gfp_flags & ~(__GFP_ACCOUNT | __GFP_ZERO |
5453 __GFP_NOWARN | __GFP_NOMEMALLOC));
5454
5455 gfp_flags |= __GFP_NOWARN | __GFP_NOMEMALLOC;
5456
5457 if (unlikely(!size))
5458 return ZERO_SIZE_PTR;
5459
5460 if (!can_spin_trylock())
5461 return NULL;
5462
5463 node = apply_strict_numa_policy(node);
5464
5465 retry:
5466 if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
5467 return NULL;
5468
5469 s = kmalloc_slab(size, NULL, gfp_flags, PASS_TOKEN_PARAM(token),
5470 ac->alloc_flags);
5471
5472 if (!(s->flags & __CMPXCHG_DOUBLE) && !kmem_cache_debug(s))
5473 /*
5474 * kmalloc_nolock() is not supported on architectures that
5475 * don't implement cmpxchg16b and thus need slab_lock()
5476 * which could be preempted by a nmi.
5477 * But debug caches don't use that and only rely on
5478 * kmem_cache_node->list_lock, so kmalloc_nolock() can attempt
5479 * to allocate from debug caches by
5480 * spin_trylock_irqsave(&n->list_lock, ...)
5481 */
5482 return NULL;
5483
5484 ret = alloc_from_pcs(s, gfp_flags, ac->alloc_flags, node);
5485 if (ret)
5486 goto success;
5487
5488 /*
5489 * Do not call slab_alloc_node(), since trylock mode isn't
5490 * compatible with slab_pre_alloc_hook/should_failslab and
5491 * kfence_alloc. Hence call ___slab_alloc() (at most twice)
5492 * and slab_post_alloc_hook() directly.
5493 */
5494 ret = ___slab_alloc(s, gfp_flags, node, ac);
5495
5496 /*
5497 * It's possible we failed due to trylock as we preempted someone with
5498 * the sheaves locked, and the list_lock is also held by another cpu.
5499 * But it should be rare that multiple kmalloc buckets would have
5500 * sheaves locked, so try a larger one.
5501 */
5502 if (!ret && can_retry) {
5503 /* pick the next kmalloc bucket */
5504 size = s->object_size + 1;
5505 /*
5506 * Another alternative is to
5507 * if (memcg) gfp_flags &= ~__GFP_ACCOUNT;
5508 * else if (!memcg) gfp_flags |= __GFP_ACCOUNT;
5509 * to retry from bucket of the same size.
5510 */
5511 can_retry = false;
5512 goto retry;
5513 }
5514
5515 success:
5516 maybe_wipe_obj_freeptr(s, ret);
5517 slab_post_alloc_hook(s, gfp_flags, 1, &ret, ac);
5518
5519 ret = kasan_kmalloc(s, ret, ac->orig_size, gfp_flags);
5520 return ret;
5521 }
5522
_kmalloc_nolock_noprof(DECL_TOKEN_PARAMS (size,token),gfp_t gfp_flags,int node)5523 void *_kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_flags, int node)
5524 {
5525 const struct slab_alloc_context ac = {
5526 .caller_addr = _RET_IP_,
5527 .orig_size = size,
5528 .alloc_flags = SLAB_ALLOC_NOLOCK,
5529 };
5530
5531 return __kmalloc_nolock_noprof(PASS_TOKEN_PARAMS(size, token),
5532 gfp_flags, node, &ac);
5533 }
5534 EXPORT_SYMBOL_GPL(_kmalloc_nolock_noprof);
5535
__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS (size,b,token),gfp_t flags,int node,unsigned long caller)5536 void *__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags,
5537 int node, unsigned long caller)
5538 {
5539 const struct slab_alloc_context ac = {
5540 .caller_addr = caller,
5541 .orig_size = size,
5542 .alloc_flags = SLAB_ALLOC_DEFAULT,
5543 };
5544
5545 return __do_kmalloc_node(PASS_BUCKET_PARAM(b), flags, node,
5546 PASS_TOKEN_PARAM(token), &ac);
5547 }
5548 EXPORT_SYMBOL(__kmalloc_node_track_caller_noprof);
5549
__kmalloc_cache_noprof(struct kmem_cache * s,gfp_t gfpflags,size_t size)5550 void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size)
5551 {
5552 void *ret;
5553 const struct slab_alloc_context ac = {
5554 .caller_addr = _RET_IP_,
5555 .orig_size = size,
5556 .alloc_flags = SLAB_ALLOC_DEFAULT,
5557 };
5558
5559 ret = slab_alloc_node(s, gfpflags, NUMA_NO_NODE, &ac);
5560
5561 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE);
5562
5563 ret = kasan_kmalloc(s, ret, size, gfpflags);
5564 return ret;
5565 }
5566 EXPORT_SYMBOL(__kmalloc_cache_noprof);
5567
__kmalloc_cache_node_noprof(struct kmem_cache * s,gfp_t gfpflags,int node,size_t size)5568 void *__kmalloc_cache_node_noprof(struct kmem_cache *s, gfp_t gfpflags,
5569 int node, size_t size)
5570 {
5571 void *ret;
5572 const struct slab_alloc_context ac = {
5573 .caller_addr = _RET_IP_,
5574 .orig_size = size,
5575 .alloc_flags = SLAB_ALLOC_DEFAULT,
5576 };
5577
5578 ret = slab_alloc_node(s, gfpflags, node, &ac);
5579
5580 trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, node);
5581
5582 ret = kasan_kmalloc(s, ret, size, gfpflags);
5583 return ret;
5584 }
5585 EXPORT_SYMBOL(__kmalloc_cache_node_noprof);
5586
5587 /*
5588 * The only version of kmalloc_node() that takes alloc_flags and thus can
5589 * determine on its own whether to handle the allocation via kmalloc_nolock() or
5590 * normally
5591 */
__kmalloc_flags_noprof(DECL_TOKEN_PARAMS (size,token),gfp_t flags,unsigned int alloc_flags,int node)5592 void *__kmalloc_flags_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
5593 unsigned int alloc_flags, int node)
5594 {
5595 const struct slab_alloc_context ac = {
5596 .caller_addr = _RET_IP_,
5597 .orig_size = size,
5598 .alloc_flags = alloc_flags,
5599 };
5600
5601 if (alloc_flags_allow_spinning(alloc_flags)) {
5602 return __do_kmalloc_node(NULL, flags, node,
5603 PASS_TOKEN_PARAM(token), &ac);
5604 } else {
5605 return __kmalloc_nolock_noprof(PASS_TOKEN_PARAMS(size, token),
5606 flags, node, &ac);
5607 }
5608 }
5609
5610
free_to_partial_list(struct kmem_cache * s,struct slab * slab,void * head,void * tail,int bulk_cnt,unsigned long addr)5611 static noinline void free_to_partial_list(
5612 struct kmem_cache *s, struct slab *slab,
5613 void *head, void *tail, int bulk_cnt,
5614 unsigned long addr)
5615 {
5616 struct kmem_cache_node *n = get_node(s, slab_nid(slab));
5617 struct slab *slab_free = NULL;
5618 int cnt = bulk_cnt;
5619 unsigned long flags;
5620 depot_stack_handle_t handle = 0;
5621
5622 /*
5623 * We cannot use GFP_NOWAIT as there are callsites where waking up
5624 * kswapd could deadlock
5625 */
5626 if (s->flags & SLAB_STORE_USER)
5627 handle = set_track_prepare(__GFP_NOWARN);
5628
5629 spin_lock_irqsave(&n->list_lock, flags);
5630
5631 if (free_debug_processing(s, slab, head, tail, &cnt, addr, handle)) {
5632 void *prior = slab->freelist;
5633
5634 /* Perform the actual freeing while we still hold the locks */
5635 slab->inuse -= cnt;
5636 set_freepointer(s, tail, prior);
5637 slab->freelist = head;
5638
5639 /*
5640 * If the slab is empty, and node's partial list is full,
5641 * it should be discarded anyway no matter it's on full or
5642 * partial list.
5643 */
5644 if (slab->inuse == 0 && n->nr_partial >= s->min_partial)
5645 slab_free = slab;
5646
5647 if (!prior) {
5648 /* was on full list */
5649 remove_full(s, n, slab);
5650 if (!slab_free) {
5651 add_partial(n, slab, ADD_TO_TAIL);
5652 stat(s, FREE_ADD_PARTIAL);
5653 }
5654 } else if (slab_free) {
5655 remove_partial(n, slab);
5656 stat(s, FREE_REMOVE_PARTIAL);
5657 }
5658 }
5659
5660 if (slab_free) {
5661 /*
5662 * Update the counters while still holding n->list_lock to
5663 * prevent spurious validation warnings
5664 */
5665 dec_slabs_node(s, slab_nid(slab_free), slab_free->objects);
5666 }
5667
5668 spin_unlock_irqrestore(&n->list_lock, flags);
5669
5670 if (slab_free) {
5671 stat(s, FREE_SLAB);
5672 free_slab(s, slab_free);
5673 }
5674 }
5675
5676 /*
5677 * Try returning (remainder of) the freelist that we just detached from the
5678 * slab. Optimistically assume the slab is still full, so we don't need to find
5679 * the tail of the detached freelist.
5680 *
5681 * Fail if the slab isn't full anymore due to a concurrent free.
5682 */
__slab_try_return_freelist(struct kmem_cache * s,struct kmem_cache_node * n,struct slab * slab,void * head,int cnt)5683 static bool __slab_try_return_freelist(struct kmem_cache *s,
5684 struct kmem_cache_node *n,
5685 struct slab *slab, void *head, int cnt)
5686 {
5687 struct freelist_counters old, new;
5688 unsigned long flags;
5689
5690 old.freelist = slab->freelist;
5691 old.counters = slab->counters;
5692
5693 if (old.freelist)
5694 return false;
5695
5696 new.freelist = head;
5697 new.counters = old.counters;
5698 new.inuse -= cnt;
5699
5700 spin_lock_irqsave(&n->list_lock, flags);
5701
5702 if (!slab_update_freelist(s, slab, &old, &new, "__slab_try_return_freelist")) {
5703 spin_unlock_irqrestore(&n->list_lock, flags);
5704 return false;
5705 }
5706
5707 add_partial(n, slab, ADD_TO_TAIL);
5708 spin_unlock_irqrestore(&n->list_lock, flags);
5709 return true;
5710 }
5711
5712 /*
5713 * Slow path handling. This may still be called frequently since objects
5714 * have a longer lifetime than the cpu slabs in most processing loads.
5715 *
5716 * So we still attempt to reduce cache line usage. Just take the slab
5717 * lock and free the item. If there is no additional partial slab
5718 * handling required then we can return immediately.
5719 */
__slab_free(struct kmem_cache * s,struct slab * slab,void * head,void * tail,int cnt,unsigned long addr)5720 static void __slab_free(struct kmem_cache *s, struct slab *slab,
5721 void *head, void *tail, int cnt,
5722 unsigned long addr)
5723
5724 {
5725 bool was_full;
5726 struct freelist_counters old, new;
5727 struct kmem_cache_node *n = NULL;
5728 unsigned long flags;
5729 bool on_node_partial;
5730
5731 if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) {
5732 free_to_partial_list(s, slab, head, tail, cnt, addr);
5733 return;
5734 }
5735
5736 do {
5737 if (unlikely(n)) {
5738 spin_unlock_irqrestore(&n->list_lock, flags);
5739 n = NULL;
5740 }
5741
5742 old.freelist = slab->freelist;
5743 old.counters = slab->counters;
5744
5745 was_full = (old.freelist == NULL);
5746
5747 set_freepointer(s, tail, old.freelist);
5748
5749 new.freelist = head;
5750 new.counters = old.counters;
5751 new.inuse -= cnt;
5752
5753 /*
5754 * Might need to be taken off (due to becoming empty) or added
5755 * to (due to not being full anymore) the partial list.
5756 * Unless it's frozen.
5757 */
5758 if (!new.inuse || was_full) {
5759
5760 n = get_node(s, slab_nid(slab));
5761 /*
5762 * Speculatively acquire the list_lock.
5763 * If the cmpxchg does not succeed then we may
5764 * drop the list_lock without any processing.
5765 *
5766 * Otherwise the list_lock will synchronize with
5767 * other processors updating the list of slabs.
5768 */
5769 spin_lock_irqsave(&n->list_lock, flags);
5770
5771 on_node_partial = slab_test_node_partial(slab);
5772 }
5773
5774 } while (!slab_update_freelist(s, slab, &old, &new, "__slab_free"));
5775
5776 if (likely(!n)) {
5777 /*
5778 * We didn't take the list_lock because the slab was already on
5779 * the partial list and will remain there.
5780 */
5781 return;
5782 }
5783
5784 /*
5785 * This slab was partially empty but not on the per-node partial list,
5786 * in which case we shouldn't manipulate its list, just return.
5787 */
5788 if (!was_full && !on_node_partial) {
5789 spin_unlock_irqrestore(&n->list_lock, flags);
5790 return;
5791 }
5792
5793 /*
5794 * If slab became empty, should we add/keep it on the partial list or we
5795 * have enough?
5796 */
5797 if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
5798 goto slab_empty;
5799
5800 /*
5801 * Objects left in the slab. If it was not on the partial list before
5802 * then add it.
5803 */
5804 if (unlikely(was_full)) {
5805 add_partial(n, slab, ADD_TO_TAIL);
5806 stat(s, FREE_ADD_PARTIAL);
5807 }
5808 spin_unlock_irqrestore(&n->list_lock, flags);
5809 return;
5810
5811 slab_empty:
5812 /*
5813 * The slab could have a single object and thus go from full to empty in
5814 * a single free, but more likely it was on the partial list. Remove it.
5815 */
5816 if (likely(!was_full)) {
5817 remove_partial(n, slab);
5818 stat(s, FREE_REMOVE_PARTIAL);
5819 }
5820
5821 spin_unlock_irqrestore(&n->list_lock, flags);
5822 stat(s, FREE_SLAB);
5823 discard_slab(s, slab);
5824 }
5825
5826 /*
5827 * pcs is locked. We should have get rid of the spare sheaf and obtained an
5828 * empty sheaf, while the main sheaf is full. We want to install the empty sheaf
5829 * as a main sheaf, and make the current main sheaf a spare sheaf.
5830 *
5831 * However due to having relinquished the cpu_sheaves lock when obtaining
5832 * the empty sheaf, we need to handle some unlikely but possible cases.
5833 *
5834 * If we put any sheaf to barn here, it's because we were interrupted or have
5835 * been migrated to a different cpu, which should be rare enough so just ignore
5836 * the barn's limits to simplify the handling.
5837 *
5838 * An alternative scenario that gets us here is when we fail
5839 * barn_replace_full_sheaf(), because there's no empty sheaf available in the
5840 * barn, so we had to allocate it by alloc_empty_sheaf(). But because we saw the
5841 * limit on full sheaves was not exceeded, we assume it didn't change and just
5842 * put the full sheaf there.
5843 */
__pcs_install_empty_sheaf(struct kmem_cache * s,struct slub_percpu_sheaves * pcs,struct slab_sheaf * empty,struct node_barn * barn)5844 static void __pcs_install_empty_sheaf(struct kmem_cache *s,
5845 struct slub_percpu_sheaves *pcs, struct slab_sheaf *empty,
5846 struct node_barn *barn)
5847 {
5848 slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
5849
5850 /* This is what we expect to find if nobody interrupted us. */
5851 if (likely(!pcs->spare)) {
5852 pcs->spare = pcs->main;
5853 pcs->main = empty;
5854 return;
5855 }
5856
5857 /*
5858 * Unlikely because if the main sheaf had space, we would have just
5859 * freed to it. Get rid of our empty sheaf.
5860 */
5861 if (pcs->main->size < s->sheaf_capacity) {
5862 barn_put_empty_sheaf(barn, empty);
5863 return;
5864 }
5865
5866 /* Also unlikely for the same reason */
5867 if (pcs->spare->size < s->sheaf_capacity) {
5868 swap(pcs->main, pcs->spare);
5869 barn_put_empty_sheaf(barn, empty);
5870 return;
5871 }
5872
5873 /*
5874 * We probably failed barn_replace_full_sheaf() due to no empty sheaf
5875 * available there, but we allocated one, so finish the job.
5876 */
5877 barn_put_full_sheaf(barn, pcs->main);
5878 stat(s, BARN_PUT);
5879 pcs->main = empty;
5880 }
5881
5882 /*
5883 * Replace the full main sheaf with a (at least partially) empty sheaf.
5884 *
5885 * Must be called with the cpu_sheaves local lock locked. If successful, returns
5886 * the pcs pointer and the local lock locked (possibly on a different cpu than
5887 * initially called). If not successful, returns NULL and the local lock
5888 * unlocked.
5889 */
5890 static struct slub_percpu_sheaves *
__pcs_replace_full_main(struct kmem_cache * s,struct slub_percpu_sheaves * pcs,bool allow_spin)5891 __pcs_replace_full_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
5892 bool allow_spin)
5893 {
5894 struct slab_sheaf *empty;
5895 struct node_barn *barn;
5896 bool put_fail;
5897
5898 restart:
5899 slab_lockdep_assert_held(this_cpu_ptr(&s->cpu_sheaves->lock));
5900
5901 /* Bootstrap or debug cache, back off */
5902 if (unlikely(!cache_has_sheaves(s))) {
5903 local_unlock(&s->cpu_sheaves->lock);
5904 return NULL;
5905 }
5906
5907 barn = get_barn(s);
5908 if (!barn) {
5909 local_unlock(&s->cpu_sheaves->lock);
5910 return NULL;
5911 }
5912
5913 put_fail = false;
5914
5915 if (!pcs->spare) {
5916 empty = barn_get_empty_sheaf(barn, allow_spin);
5917 if (empty) {
5918 pcs->spare = pcs->main;
5919 pcs->main = empty;
5920 return pcs;
5921 }
5922 goto alloc_empty;
5923 }
5924
5925 if (pcs->spare->size < s->sheaf_capacity) {
5926 swap(pcs->main, pcs->spare);
5927 return pcs;
5928 }
5929
5930 empty = barn_replace_full_sheaf(barn, pcs->main, allow_spin);
5931
5932 if (!IS_ERR(empty)) {
5933 stat(s, BARN_PUT);
5934 pcs->main = empty;
5935 return pcs;
5936 }
5937
5938 /* sheaf_flush_unused() doesn't support !allow_spin */
5939 if (PTR_ERR(empty) == -E2BIG && allow_spin) {
5940 /* Since we got here, spare exists and is full */
5941 struct slab_sheaf *to_flush = pcs->spare;
5942
5943 stat(s, BARN_PUT_FAIL);
5944
5945 pcs->spare = NULL;
5946 local_unlock(&s->cpu_sheaves->lock);
5947
5948 sheaf_flush_unused(s, to_flush);
5949 empty = to_flush;
5950 goto got_empty;
5951 }
5952
5953 /*
5954 * We could not replace full sheaf because barn had no empty
5955 * sheaves. We can still allocate it and put the full sheaf in
5956 * __pcs_install_empty_sheaf(), but if we fail to allocate it,
5957 * make sure to count the fail.
5958 */
5959 put_fail = true;
5960
5961 alloc_empty:
5962 local_unlock(&s->cpu_sheaves->lock);
5963
5964 /*
5965 * alloc_empty_sheaf() doesn't support !allow_spin and it's
5966 * easier to fall back to freeing directly without sheaves
5967 * than add the support (and to sheaf_flush_unused() above)
5968 */
5969 if (!allow_spin)
5970 return NULL;
5971
5972 empty = alloc_empty_sheaf(s, GFP_NOWAIT, SLAB_ALLOC_DEFAULT);
5973 if (empty)
5974 goto got_empty;
5975
5976 if (put_fail)
5977 stat(s, BARN_PUT_FAIL);
5978
5979 if (!sheaf_try_flush_main(s))
5980 return NULL;
5981
5982 if (!local_trylock(&s->cpu_sheaves->lock))
5983 return NULL;
5984
5985 pcs = this_cpu_ptr(s->cpu_sheaves);
5986
5987 /*
5988 * we flushed the main sheaf so it should be empty now,
5989 * but in case we got preempted or migrated, we need to
5990 * check again
5991 */
5992 if (pcs->main->size == s->sheaf_capacity)
5993 goto restart;
5994
5995 return pcs;
5996
5997 got_empty:
5998 if (!local_trylock(&s->cpu_sheaves->lock)) {
5999 barn_put_empty_sheaf(barn, empty);
6000 return NULL;
6001 }
6002
6003 pcs = this_cpu_ptr(s->cpu_sheaves);
6004 __pcs_install_empty_sheaf(s, pcs, empty, barn);
6005
6006 return pcs;
6007 }
6008
6009 /*
6010 * Free an object to the percpu sheaves.
6011 * The object is expected to have passed slab_free_hook() already.
6012 */
6013 static __fastpath_inline
free_to_pcs(struct kmem_cache * s,void * object,bool allow_spin)6014 bool free_to_pcs(struct kmem_cache *s, void *object, bool allow_spin)
6015 {
6016 struct slub_percpu_sheaves *pcs;
6017
6018 if (!local_trylock(&s->cpu_sheaves->lock))
6019 return false;
6020
6021 pcs = this_cpu_ptr(s->cpu_sheaves);
6022
6023 if (unlikely(pcs->main->size == s->sheaf_capacity)) {
6024
6025 pcs = __pcs_replace_full_main(s, pcs, allow_spin);
6026 if (unlikely(!pcs))
6027 return false;
6028 }
6029
6030 pcs->main->objects[pcs->main->size++] = object;
6031
6032 local_unlock(&s->cpu_sheaves->lock);
6033
6034 stat(s, FREE_FASTPATH);
6035
6036 return true;
6037 }
6038
rcu_free_sheaf(struct rcu_head * head)6039 static void rcu_free_sheaf(struct rcu_head *head)
6040 {
6041 struct slab_sheaf *sheaf;
6042 struct node_barn *barn = NULL;
6043 struct kmem_cache *s;
6044
6045 sheaf = container_of(head, struct slab_sheaf, rcu_head);
6046
6047 s = sheaf->cache;
6048
6049 /*
6050 * This may remove some objects due to slab_free_hook() returning false,
6051 * so that the sheaf might no longer be completely full. But it's easier
6052 * to handle it as full (unless it became completely empty), as the code
6053 * handles it fine. The only downside is that sheaf will serve fewer
6054 * allocations when reused. It only happens due to debugging, which is a
6055 * performance hit anyway.
6056 *
6057 * If it returns true, there was at least one object from pfmemalloc
6058 * slab so simply flush everything.
6059 */
6060 if (__rcu_free_sheaf_prepare(s, sheaf))
6061 goto flush;
6062
6063 barn = get_barn_node(s, sheaf->node);
6064 if (!barn)
6065 goto flush;
6066
6067 /* due to slab_free_hook() */
6068 if (unlikely(sheaf->size == 0))
6069 goto empty;
6070
6071 /*
6072 * Checking nr_full/nr_empty outside lock avoids contention in case the
6073 * barn is at the respective limit. Due to the race we might go over the
6074 * limit but that should be rare and harmless.
6075 */
6076
6077 if (data_race(barn->nr_full) < MAX_FULL_SHEAVES) {
6078 stat(s, BARN_PUT);
6079 barn_put_full_sheaf(barn, sheaf);
6080 return;
6081 }
6082
6083 flush:
6084 stat(s, BARN_PUT_FAIL);
6085 sheaf_flush_unused(s, sheaf);
6086
6087 empty:
6088 if (barn && data_race(barn->nr_empty) < MAX_EMPTY_SHEAVES) {
6089 barn_put_empty_sheaf(barn, sheaf);
6090 return;
6091 }
6092
6093 free_empty_sheaf(s, sheaf);
6094 }
6095
6096 /*
6097 * kvfree_call_rcu() can be called while holding a raw_spinlock_t. Since
6098 * __kfree_rcu_sheaf() may acquire a spinlock_t (sleeping lock on PREEMPT_RT),
6099 * this would violate lock nesting rules. Therefore, kfree_call_rcu_nolock()
6100 * avoids this problem by passing SLAB_FREE_NOLOCK. kvfree_call_rcu() is
6101 * bypassing the sheaves layer completely on PREEMPT_RT.
6102 *
6103 * However, lockdep still complains that it is invalid to acquire spinlock_t
6104 * while holding raw_spinlock_t, even on !PREEMPT_RT where spinlock_t is a
6105 * spinning lock. Tell lockdep that acquiring spinlock_t is valid here
6106 * by temporarily raising the wait-type to LD_WAIT_CONFIG. Skip the lockdep map
6107 * on PREEMPT_RT to avoid suppressing valid lockdep warnings.
6108 */
6109 static DEFINE_WAIT_OVERRIDE_MAP(kfree_rcu_sheaf_map, LD_WAIT_CONFIG);
6110
__kfree_rcu_sheaf(struct kmem_cache * s,void * obj,unsigned int free_flags)6111 bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags)
6112 {
6113 struct slub_percpu_sheaves *pcs;
6114 struct slab_sheaf *rcu_sheaf;
6115 bool allow_spin = free_flags_allow_spinning(free_flags);
6116
6117 VM_WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT) && allow_spin);
6118
6119 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6120 lock_map_acquire_try(&kfree_rcu_sheaf_map);
6121
6122 if (!local_trylock(&s->cpu_sheaves->lock))
6123 goto fail;
6124
6125 pcs = this_cpu_ptr(s->cpu_sheaves);
6126
6127 if (unlikely(!pcs->rcu_free)) {
6128 struct slab_sheaf *empty;
6129 struct node_barn *barn;
6130 unsigned int alloc_flags = to_alloc_flags(free_flags);
6131 gfp_t gfp = allow_spin ? GFP_NOWAIT : __GFP_NOWARN;
6132
6133 /* Bootstrap or debug cache, fall back */
6134 if (unlikely(!cache_has_sheaves(s))) {
6135 local_unlock(&s->cpu_sheaves->lock);
6136 goto fail;
6137 }
6138
6139 if (pcs->spare && pcs->spare->size == 0) {
6140 pcs->rcu_free = pcs->spare;
6141 pcs->spare = NULL;
6142 goto do_free;
6143 }
6144
6145 barn = get_barn(s);
6146 if (!barn) {
6147 local_unlock(&s->cpu_sheaves->lock);
6148 goto fail;
6149 }
6150
6151 empty = barn_get_empty_sheaf(barn, allow_spin);
6152
6153 if (empty) {
6154 pcs->rcu_free = empty;
6155 goto do_free;
6156 }
6157
6158 local_unlock(&s->cpu_sheaves->lock);
6159
6160 empty = alloc_empty_sheaf(s, gfp, alloc_flags);
6161
6162 if (!empty)
6163 goto fail;
6164
6165 if (!local_trylock(&s->cpu_sheaves->lock)) {
6166 __free_empty_sheaf(s, empty, free_flags);
6167 goto fail;
6168 }
6169
6170 pcs = this_cpu_ptr(s->cpu_sheaves);
6171
6172 if (unlikely(pcs->rcu_free))
6173 __free_empty_sheaf(s, empty, free_flags);
6174 else
6175 pcs->rcu_free = empty;
6176 }
6177
6178 do_free:
6179
6180 rcu_sheaf = pcs->rcu_free;
6181
6182 /*
6183 * Since we flush immediately when size reaches capacity, we never reach
6184 * this with size already at capacity, so no OOB write is possible.
6185 */
6186 rcu_sheaf->objects[rcu_sheaf->size++] = obj;
6187
6188 if (likely(rcu_sheaf->size < s->sheaf_capacity)) {
6189 rcu_sheaf = NULL;
6190 } else {
6191 pcs->rcu_free = NULL;
6192 rcu_sheaf->node = numa_node_id();
6193 }
6194
6195 /*
6196 * we flush before local_unlock to make sure a racing
6197 * flush_all_rcu_sheaves() doesn't miss this sheaf
6198 */
6199 if (rcu_sheaf) {
6200 /*
6201 * With !allow_spin, we might have interrupted call_rcu()'s
6202 * IRQ-disabled critical section. If IRQs are not disabled,
6203 * we know that's not the case.
6204 */
6205 if (unlikely(!allow_spin && irqs_disabled())) {
6206 struct deferred_percpu_work *dpw;
6207
6208 dpw = this_cpu_ptr(&deferred_percpu_work);
6209 if (llist_add(&rcu_sheaf->llnode, &dpw->rcu_sheaves))
6210 irq_work_queue(&dpw->work);
6211 } else {
6212 call_rcu(&rcu_sheaf->rcu_head, rcu_free_sheaf);
6213 }
6214 }
6215
6216 local_unlock(&s->cpu_sheaves->lock);
6217
6218 stat(s, FREE_RCU_SHEAF);
6219 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6220 lock_map_release(&kfree_rcu_sheaf_map);
6221 return true;
6222
6223 fail:
6224 stat(s, FREE_RCU_SHEAF_FAIL);
6225 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6226 lock_map_release(&kfree_rcu_sheaf_map);
6227 return false;
6228 }
6229
can_free_to_pcs(struct slab * slab)6230 static __always_inline bool can_free_to_pcs(struct slab *slab)
6231 {
6232 int slab_node;
6233 int numa_node;
6234
6235 if (!IS_ENABLED(CONFIG_NUMA))
6236 goto check_pfmemalloc;
6237
6238 slab_node = slab_nid(slab);
6239
6240 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
6241 /*
6242 * numa_mem_id() points to the closest node with memory so only allow
6243 * objects from that node to the percpu sheaves
6244 */
6245 numa_node = numa_mem_id();
6246
6247 if (likely(slab_node == numa_node))
6248 goto check_pfmemalloc;
6249 #else
6250
6251 /*
6252 * numa_mem_id() is only a wrapper to numa_node_id() which is where this
6253 * cpu belongs to, but it might be a memoryless node anyway. We don't
6254 * know what the closest node is.
6255 */
6256 numa_node = numa_node_id();
6257
6258 /* freed object is from this cpu's node, proceed */
6259 if (likely(slab_node == numa_node))
6260 goto check_pfmemalloc;
6261
6262 /*
6263 * Freed object isn't from this cpu's node, but that node is memoryless
6264 * or only has ZONE_MOVABLE memory, which slab cannot allocate from.
6265 * Proceed as it's better to cache remote objects than falling back to
6266 * the slowpath for everything. The allocation side can never obtain
6267 * a local object anyway, if none exist. We don't have numa_mem_id() to
6268 * point to the closest node as we would on a proper memoryless node
6269 * setup.
6270 */
6271 if (unlikely(!node_state(numa_node, N_NORMAL_MEMORY)))
6272 goto check_pfmemalloc;
6273 #endif
6274
6275 return false;
6276
6277 check_pfmemalloc:
6278 return likely(!slab_test_pfmemalloc(slab));
6279 }
6280
6281 /*
6282 * Try to free as many objects (already processed by free hooks) as possible to
6283 * a single per-cpu sheaf.
6284 *
6285 * Returns how many objects were freed. Zero means failure and the caller should
6286 * fall back to __kmem_cache_free_bulk().
6287 */
__free_to_pcs_batch(struct kmem_cache * s,size_t size,void ** p)6288 static unsigned int __free_to_pcs_batch(struct kmem_cache *s, size_t size, void **p)
6289 {
6290 struct slub_percpu_sheaves *pcs;
6291 struct slab_sheaf *main, *empty;
6292 struct node_barn *barn;
6293 unsigned int batch;
6294
6295 if (!local_trylock(&s->cpu_sheaves->lock))
6296 return 0;
6297
6298 pcs = this_cpu_ptr(s->cpu_sheaves);
6299
6300 if (likely(pcs->main->size < s->sheaf_capacity))
6301 goto do_free;
6302
6303 barn = get_barn(s);
6304 if (!barn)
6305 goto no_empty;
6306
6307 if (!pcs->spare) {
6308 empty = barn_get_empty_sheaf(barn, true);
6309 if (!empty)
6310 goto no_empty;
6311
6312 pcs->spare = pcs->main;
6313 pcs->main = empty;
6314 goto do_free;
6315 }
6316
6317 if (pcs->spare->size < s->sheaf_capacity) {
6318 swap(pcs->main, pcs->spare);
6319 goto do_free;
6320 }
6321
6322 empty = barn_replace_full_sheaf(barn, pcs->main, true);
6323 if (IS_ERR(empty)) {
6324 stat(s, BARN_PUT_FAIL);
6325 goto no_empty;
6326 }
6327
6328 stat(s, BARN_PUT);
6329 pcs->main = empty;
6330
6331 do_free:
6332 main = pcs->main;
6333 batch = min(size, s->sheaf_capacity - main->size);
6334
6335 memcpy(main->objects + main->size, p, batch * sizeof(void *));
6336 main->size += batch;
6337
6338 local_unlock(&s->cpu_sheaves->lock);
6339
6340 stat_add(s, FREE_FASTPATH, batch);
6341
6342 return batch;
6343
6344 no_empty:
6345 local_unlock(&s->cpu_sheaves->lock);
6346
6347 return 0;
6348 }
6349
6350 /*
6351 * Bulk free objects to the percpu sheaves.
6352 * Unlike free_to_pcs() this includes the calls to all necessary hooks
6353 * and the fallback to freeing to slab pages.
6354 */
free_to_pcs_bulk(struct kmem_cache * s,size_t size,void ** p)6355 static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p)
6356 {
6357 bool init = slab_want_init_on_free(s);
6358 void **remote_objects = p;
6359 unsigned int remote_nr = 0;
6360
6361 /*
6362 * Process the free hooks and separate out remote objects by
6363 * partitioning the 'p' array in place:
6364 *
6365 * [0, remote_nr) - processed remote objects
6366 * [remote_nr, i) - processed local objects
6367 * [i, size) - unprocessed objects
6368 */
6369 for (unsigned int i = 0; i < size;) {
6370 struct slab *slab = virt_to_slab(p[i]);
6371
6372 memcg_slab_free_hook(s, slab, p + i, 1);
6373 alloc_tagging_slab_free_hook(s, slab, p + i, 1);
6374
6375 if (unlikely(!slab_free_hook(s, p[i], init, false))) {
6376 p[i] = p[--size];
6377 continue;
6378 }
6379
6380 if (unlikely(!can_free_to_pcs(slab))) {
6381 if (i != remote_nr)
6382 swap(remote_objects[remote_nr], p[i]);
6383 remote_nr++;
6384 }
6385
6386 i++;
6387 }
6388
6389 p += remote_nr;
6390 size -= remote_nr;
6391
6392 while (size) {
6393 unsigned int batch_freed = __free_to_pcs_batch(s, size, p);
6394
6395 if (!batch_freed) {
6396 __kmem_cache_free_bulk(s, size, p);
6397 stat_add(s, FREE_SLOWPATH, size);
6398 break;
6399 }
6400
6401 p += batch_freed;
6402 size -= batch_freed;
6403 }
6404
6405 /*
6406 * Processing remote objects last decreases the chances of cpu migration
6407 * while freeing to sheaves and compromising object locality
6408 */
6409 if (remote_nr) {
6410 __kmem_cache_free_bulk(s, remote_nr, remote_objects);
6411 stat_add(s, FREE_SLOWPATH, remote_nr);
6412 }
6413 }
6414
6415 /*
6416 * In PREEMPT_RT irq_work runs in per-cpu kthread, so it's safe
6417 * to take sleeping spin_locks from __slab_free().
6418 * In !PREEMPT_RT irq_work will run after local_unlock_irqrestore().
6419 */
deferred_percpu_work_fn(struct irq_work * work)6420 static void deferred_percpu_work_fn(struct irq_work *work)
6421 {
6422 struct deferred_percpu_work *dpw;
6423 struct llist_head *objs, *objs_by_rcu, *rcu_sheaves;
6424 struct llist_node *llnode, *pos, *t;
6425 struct slab_sheaf *sheaf, *next;
6426
6427 dpw = container_of(work, struct deferred_percpu_work, work);
6428 rcu_sheaves = &dpw->rcu_sheaves;
6429 objs = &dpw->objects;
6430 objs_by_rcu = &dpw->objects_by_rcu;
6431
6432 llnode = llist_del_all(objs);
6433 llist_for_each_safe(pos, t, llnode) {
6434 struct kmem_cache *s;
6435 struct slab *slab;
6436 void *x = pos;
6437
6438 slab = virt_to_slab(x);
6439 s = slab->slab_cache;
6440
6441 /* Point 'x' back to the beginning of allocated object */
6442 x -= s->offset;
6443
6444 /*
6445 * We used freepointer in 'x' to link 'x' into df->objects.
6446 * Clear it to NULL to avoid false positive detection
6447 * of "Freepointer corruption".
6448 */
6449 set_freepointer(s, x, NULL);
6450
6451 __slab_free(s, slab, x, x, 1, _THIS_IP_);
6452 stat(s, FREE_SLOWPATH);
6453 }
6454
6455 llnode = llist_del_all(objs_by_rcu);
6456 llist_for_each_safe(pos, t, llnode) {
6457 void *head = pos;
6458 void *objp = kvmalloc_obj_start_addr(head);
6459
6460 kvfree_call_rcu(head, objp);
6461 }
6462
6463 llnode = llist_del_all(rcu_sheaves);
6464 llist_for_each_entry_safe(sheaf, next, llnode, llnode)
6465 call_rcu(&sheaf->rcu_head, rcu_free_sheaf);
6466 }
6467
defer_free(struct kmem_cache * s,void * head)6468 static void defer_free(struct kmem_cache *s, void *head)
6469 {
6470 struct deferred_percpu_work *dpw;
6471
6472 guard(preempt)();
6473
6474 head = kasan_reset_tag(head);
6475
6476 dpw = this_cpu_ptr(&deferred_percpu_work);
6477 if (llist_add(head + s->offset, &dpw->objects))
6478 irq_work_queue(&dpw->work);
6479 }
6480
defer_kfree_rcu(struct kvfree_rcu_head * head)6481 void defer_kfree_rcu(struct kvfree_rcu_head *head)
6482 {
6483 struct deferred_percpu_work *dpw;
6484
6485 guard(preempt)();
6486
6487 dpw = this_cpu_ptr(&deferred_percpu_work);
6488 if (llist_add((struct llist_node *)head, &dpw->objects_by_rcu))
6489 irq_work_queue(&dpw->work);
6490 }
6491
6492 /* Must be called before flush_rcu_sheaves_on_cache() */
deferred_work_barrier(void)6493 void deferred_work_barrier(void)
6494 {
6495 int cpu;
6496
6497 for_each_possible_cpu(cpu)
6498 irq_work_sync(&per_cpu_ptr(&deferred_percpu_work, cpu)->work);
6499 }
6500
6501 static __fastpath_inline
slab_free(struct kmem_cache * s,struct slab * slab,void * object,unsigned long addr)6502 void slab_free(struct kmem_cache *s, struct slab *slab, void *object,
6503 unsigned long addr)
6504 {
6505 memcg_slab_free_hook(s, slab, &object, 1);
6506 alloc_tagging_slab_free_hook(s, slab, &object, 1);
6507
6508 if (unlikely(!slab_free_hook(s, object, slab_want_init_on_free(s), false)))
6509 return;
6510
6511 if (likely(can_free_to_pcs(slab)) && likely(free_to_pcs(s, object, true)))
6512 return;
6513
6514 __slab_free(s, slab, object, object, 1, addr);
6515 stat(s, FREE_SLOWPATH);
6516 }
6517
6518 #ifdef CONFIG_MEMCG
6519 /* Do not inline the rare memcg charging failed path into the allocation path */
6520 static noinline
memcg_alloc_abort_single(struct kmem_cache * s,void * object)6521 void memcg_alloc_abort_single(struct kmem_cache *s, void *object)
6522 {
6523 struct slab *slab = virt_to_slab(object);
6524
6525 alloc_tagging_slab_free_hook(s, slab, &object, 1);
6526
6527 if (likely(slab_free_hook(s, object, slab_want_init_on_free(s), false)))
6528 __slab_free(s, slab, object, object, 1, _RET_IP_);
6529 }
6530 #endif
6531
6532 static __fastpath_inline
slab_free_bulk(struct kmem_cache * s,struct slab * slab,void * head,void * tail,void ** p,int cnt,unsigned long addr)6533 void slab_free_bulk(struct kmem_cache *s, struct slab *slab, void *head,
6534 void *tail, void **p, int cnt, unsigned long addr)
6535 {
6536 memcg_slab_free_hook(s, slab, p, cnt);
6537 alloc_tagging_slab_free_hook(s, slab, p, cnt);
6538 /*
6539 * With KASAN enabled slab_free_freelist_hook modifies the freelist
6540 * to remove objects, whose reuse must be delayed.
6541 */
6542 if (likely(slab_free_freelist_hook(s, &head, &tail, &cnt))) {
6543 __slab_free(s, slab, head, tail, cnt, addr);
6544 stat_add(s, FREE_SLOWPATH, cnt);
6545 }
6546 }
6547
6548 #ifdef CONFIG_SLUB_RCU_DEBUG
slab_free_after_rcu_debug(struct rcu_head * rcu_head)6549 static void slab_free_after_rcu_debug(struct rcu_head *rcu_head)
6550 {
6551 struct rcu_delayed_free *delayed_free =
6552 container_of(rcu_head, struct rcu_delayed_free, head);
6553 void *object = delayed_free->object;
6554 struct slab *slab = virt_to_slab(object);
6555 struct kmem_cache *s;
6556
6557 kfree(delayed_free);
6558
6559 if (WARN_ON(is_kfence_address(object)))
6560 return;
6561
6562 /* find the object and the cache again */
6563 if (WARN_ON(!slab))
6564 return;
6565 s = slab->slab_cache;
6566 if (WARN_ON(!(s->flags & SLAB_TYPESAFE_BY_RCU)))
6567 return;
6568
6569 /* resume freeing */
6570 if (slab_free_hook(s, object, slab_want_init_on_free(s), true)) {
6571 __slab_free(s, slab, object, object, 1, _THIS_IP_);
6572 stat(s, FREE_SLOWPATH);
6573 }
6574 }
6575 #endif /* CONFIG_SLUB_RCU_DEBUG */
6576
6577 #ifdef CONFIG_KASAN_GENERIC
___cache_free(struct kmem_cache * cache,void * x,unsigned long addr)6578 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr)
6579 {
6580 __slab_free(cache, virt_to_slab(x), x, x, 1, addr);
6581 stat(cache, FREE_SLOWPATH);
6582 }
6583 #endif
6584
warn_free_bad_obj(struct kmem_cache * s,void * obj)6585 static noinline void warn_free_bad_obj(struct kmem_cache *s, void *obj)
6586 {
6587 struct kmem_cache *cachep;
6588 struct slab *slab;
6589
6590 slab = virt_to_slab(obj);
6591 if (WARN_ONCE(!slab,
6592 "kmem_cache_free(%s, %p): object is not in a slab page\n",
6593 s->name, obj))
6594 return;
6595
6596 cachep = slab->slab_cache;
6597
6598 if (WARN_ONCE(cachep != s,
6599 "kmem_cache_free(%s, %p): object belongs to different cache %s\n",
6600 s->name, obj, cachep ? cachep->name : "(NULL)")) {
6601 if (cachep)
6602 print_tracking(cachep, obj);
6603 return;
6604 }
6605 }
6606
6607 /**
6608 * kmem_cache_free - Deallocate an object
6609 * @s: The cache the allocation was from.
6610 * @x: The previously allocated object.
6611 *
6612 * Free an object which was previously allocated from this
6613 * cache.
6614 */
kmem_cache_free(struct kmem_cache * s,void * x)6615 void kmem_cache_free(struct kmem_cache *s, void *x)
6616 {
6617 struct slab *slab;
6618
6619 slab = virt_to_slab(x);
6620
6621 if (IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) ||
6622 kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) {
6623
6624 /*
6625 * Intentionally leak the object in these cases, because it
6626 * would be too dangerous to continue.
6627 */
6628 if (unlikely(!slab || (slab->slab_cache != s))) {
6629 warn_free_bad_obj(s, x);
6630 return;
6631 }
6632 }
6633
6634 trace_kmem_cache_free(_RET_IP_, x, s);
6635 slab_free(s, slab, x, _RET_IP_);
6636 }
6637 EXPORT_SYMBOL(kmem_cache_free);
6638
slab_ksize(struct slab * slab)6639 static inline size_t slab_ksize(struct slab *slab)
6640 {
6641 struct kmem_cache *s = slab->slab_cache;
6642
6643 #ifdef CONFIG_SLUB_DEBUG
6644 /*
6645 * Debugging requires use of the padding between object
6646 * and whatever may come after it.
6647 */
6648 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
6649 return s->object_size;
6650 #endif
6651 if (s->flags & SLAB_KASAN)
6652 return s->object_size;
6653 /*
6654 * If we have the need to store the freelist pointer
6655 * or any other metadata back there then we can
6656 * only use the space before that information.
6657 */
6658 if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
6659 return s->inuse;
6660 else if (obj_exts_in_object(slab))
6661 return s->inuse;
6662 /*
6663 * Else we can use all the padding etc for the allocation
6664 */
6665 return s->size;
6666 }
6667
__ksize(const void * object)6668 static size_t __ksize(const void *object)
6669 {
6670 struct page *page;
6671 struct slab *slab;
6672
6673 if (unlikely(object == ZERO_SIZE_PTR))
6674 return 0;
6675
6676 page = virt_to_page(object);
6677
6678 if (unlikely(PageLargeKmalloc(page)))
6679 return large_kmalloc_size(page);
6680
6681 slab = page_slab(page);
6682 /* Delete this after we're sure there are no users */
6683 if (WARN_ON(!slab))
6684 return page_size(page);
6685
6686 #ifdef CONFIG_SLUB_DEBUG
6687 skip_orig_size_check(slab->slab_cache, object);
6688 #endif
6689
6690 return slab_ksize(slab);
6691 }
6692
6693 /**
6694 * ksize -- Report full size of underlying allocation
6695 * @objp: pointer to the object
6696 *
6697 * This should only be used internally to query the true size of allocations.
6698 * It is not meant to be a way to discover the usable size of an allocation
6699 * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond
6700 * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS,
6701 * and/or FORTIFY_SOURCE.
6702 *
6703 * Return: size of the actual memory used by @objp in bytes
6704 */
ksize(const void * objp)6705 size_t ksize(const void *objp)
6706 {
6707 /*
6708 * We need to first check that the pointer to the object is valid.
6709 * The KASAN report printed from ksize() is more useful, then when
6710 * it's printed later when the behaviour could be undefined due to
6711 * a potential use-after-free or double-free.
6712 *
6713 * We use kasan_check_byte(), which is supported for the hardware
6714 * tag-based KASAN mode, unlike kasan_check_read/write().
6715 *
6716 * If the pointed to memory is invalid, we return 0 to avoid users of
6717 * ksize() writing to and potentially corrupting the memory region.
6718 *
6719 * We want to perform the check before __ksize(), to avoid potentially
6720 * crashing in __ksize() due to accessing invalid metadata.
6721 */
6722 if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp))
6723 return 0;
6724
6725 return kfence_ksize(objp) ?: __ksize(objp);
6726 }
6727 EXPORT_SYMBOL(ksize);
6728
free_large_kmalloc(struct page * page,void * object)6729 static void free_large_kmalloc(struct page *page, void *object)
6730 {
6731 unsigned int order = compound_order(page);
6732
6733 if (WARN_ON_ONCE(!PageLargeKmalloc(page))) {
6734 dump_page(page, "Not a kmalloc allocation");
6735 return;
6736 }
6737
6738 if (WARN_ON_ONCE(order == 0))
6739 pr_warn_once("object pointer: 0x%p\n", object);
6740
6741 kmemleak_free(object);
6742 kasan_kfree_large(object);
6743 kmsan_kfree_large(object);
6744
6745 mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
6746 -(PAGE_SIZE << order));
6747 __ClearPageLargeKmalloc(page);
6748 free_frozen_pages(page, order);
6749 }
6750
6751 /*
6752 * Given an rcu_head embedded within an object obtained from kvmalloc at an
6753 * offset < 4k, free the object in question.
6754 */
kvfree_rcu_cb(struct rcu_head * head)6755 void kvfree_rcu_cb(struct rcu_head *head)
6756 {
6757 void *obj;
6758
6759 obj = kvmalloc_obj_start_addr(head);
6760
6761 if (is_vmalloc_addr(obj)) {
6762 vfree(obj);
6763 } else {
6764 struct page *page = virt_to_page(obj);
6765 struct slab *slab = page_slab(page);
6766
6767 if (slab)
6768 slab_free(slab->slab_cache, slab, obj, _RET_IP_);
6769 else
6770 free_large_kmalloc(page, obj);
6771 }
6772 }
6773
6774 /**
6775 * kfree - free previously allocated memory
6776 * @object: pointer returned by kmalloc(), kmalloc_nolock(), or kmem_cache_alloc()
6777 *
6778 * If @object is NULL, no operation is performed.
6779 */
kfree(const void * object)6780 void kfree(const void *object)
6781 {
6782 struct page *page;
6783 struct slab *slab;
6784 struct kmem_cache *s;
6785 void *x = (void *)object;
6786
6787 trace_kfree(_RET_IP_, object);
6788
6789 if (unlikely(ZERO_OR_NULL_PTR(object)))
6790 return;
6791
6792 page = virt_to_page(object);
6793 slab = page_slab(page);
6794 if (!slab) {
6795 /* kmalloc_nolock() doesn't support large kmalloc */
6796 free_large_kmalloc(page, (void *)object);
6797 return;
6798 }
6799
6800 s = slab->slab_cache;
6801 slab_free(s, slab, x, _RET_IP_);
6802 }
6803 EXPORT_SYMBOL(kfree);
6804
6805 /*
6806 * Can be called while holding raw_spinlock_t or from IRQ and NMI,
6807 * but ONLY for objects allocated by kmalloc_nolock().
6808 * Debug checks (like kmemleak and kfence) were skipped on allocation,
6809 * hence
6810 * obj = kmalloc(); kfree_nolock(obj);
6811 * will miss kmemleak/kfence book keeping and will cause false positives.
6812 * large_kmalloc is not supported either.
6813 */
kfree_nolock(const void * object)6814 void kfree_nolock(const void *object)
6815 {
6816 struct slab *slab;
6817 struct kmem_cache *s;
6818 void *x = (void *)object;
6819
6820 if (unlikely(ZERO_OR_NULL_PTR(object)))
6821 return;
6822
6823 slab = virt_to_slab(object);
6824 if (unlikely(!slab)) {
6825 WARN_ONCE(1, "large_kmalloc is not supported by kfree_nolock()");
6826 return;
6827 }
6828
6829 s = slab->slab_cache;
6830
6831 memcg_slab_free_hook(s, slab, &x, 1);
6832 alloc_tagging_slab_free_hook(s, slab, &x, 1);
6833 /*
6834 * Unlike slab_free() do NOT call the following:
6835 * kmemleak_free_recursive(x, s->flags);
6836 * debug_check_no_locks_freed(x, s->object_size);
6837 * debug_check_no_obj_freed(x, s->object_size);
6838 * __kcsan_check_access(x, s->object_size, ..);
6839 * kfence_free(x);
6840 * since they take spinlocks or not safe from any context.
6841 */
6842 kmsan_slab_free(s, x);
6843 /*
6844 * If KASAN finds a kernel bug it will do kasan_report_invalid_free()
6845 * which will call raw_spin_lock_irqsave() which is technically
6846 * unsafe from NMI, but take chance and report kernel bug.
6847 * The sequence of
6848 * kasan_report_invalid_free() -> raw_spin_lock_irqsave() -> NMI
6849 * -> kfree_nolock() -> kasan_report_invalid_free() on the same CPU
6850 * is double buggy and deserves to deadlock.
6851 */
6852 if (kasan_slab_pre_free(s, x))
6853 return;
6854 /*
6855 * memcg, kasan_slab_pre_free are done for 'x'.
6856 * The only thing left is kasan_poison without quarantine,
6857 * since kasan quarantine takes locks and not supported from NMI.
6858 */
6859 kasan_slab_free(s, x, false, false, /* skip quarantine */true);
6860
6861 if (likely(can_free_to_pcs(slab)) && likely(free_to_pcs(s, x, false)))
6862 return;
6863
6864 /*
6865 * __slab_free() can locklessly cmpxchg16 into a slab, but then it might
6866 * need to take spin_lock for further processing.
6867 * Avoid the complexity and simply add to a deferred list.
6868 */
6869 defer_free(s, x);
6870 }
6871 EXPORT_SYMBOL_GPL(kfree_nolock);
6872
6873 static __always_inline __realloc_size(2) void *
__do_krealloc(const void * p,size_t new_size,unsigned long align,gfp_t flags,int nid,kmalloc_token_t token)6874 __do_krealloc(const void *p, size_t new_size, unsigned long align, gfp_t flags, int nid, kmalloc_token_t token)
6875 {
6876 void *ret;
6877 size_t ks = 0;
6878 int orig_size = 0;
6879 struct kmem_cache *s = NULL;
6880
6881 if (unlikely(ZERO_OR_NULL_PTR(p)))
6882 goto alloc_new;
6883
6884 /* Check for double-free. */
6885 if (!kasan_check_byte(p))
6886 return NULL;
6887
6888 if (is_kfence_address(p)) {
6889 ks = orig_size = kfence_ksize(p);
6890 } else {
6891 struct page *page = virt_to_page(p);
6892 struct slab *slab = page_slab(page);
6893
6894 if (!slab) {
6895 /* Big kmalloc object */
6896 ks = page_size(page);
6897 WARN_ON(ks <= KMALLOC_MAX_CACHE_SIZE);
6898 WARN_ON(p != page_address(page));
6899 } else {
6900 s = slab->slab_cache;
6901 orig_size = get_orig_size(s, (void *)p);
6902 ks = s->object_size;
6903 }
6904 }
6905
6906 /*
6907 * If reallocation is not necessary (e. g. the new size is less
6908 * than the current allocated size), the current allocation will be
6909 * preserved unless __GFP_THISNODE is set. In the latter case a new
6910 * allocation on the requested node will be attempted.
6911 */
6912 if (unlikely(flags & __GFP_THISNODE) && nid != NUMA_NO_NODE &&
6913 nid != page_to_nid(virt_to_page(p)))
6914 goto alloc_new;
6915
6916 /* If the old object doesn't fit, allocate a bigger one */
6917 if (new_size > ks)
6918 goto alloc_new;
6919
6920 /* If the old object doesn't satisfy the new alignment, allocate a new one */
6921 if (!IS_ALIGNED((unsigned long)p, align))
6922 goto alloc_new;
6923
6924 /* Zero out spare memory. */
6925 if (want_init_on_alloc(flags)) {
6926 kasan_disable_current();
6927 if (orig_size && orig_size < new_size)
6928 memset(kasan_reset_tag(p) + orig_size, 0, new_size - orig_size);
6929 else
6930 memset(kasan_reset_tag(p) + new_size, 0, ks - new_size);
6931 kasan_enable_current();
6932 }
6933
6934 /* Setup kmalloc redzone when needed */
6935 if (s && slub_debug_orig_size(s)) {
6936 set_orig_size(s, (void *)p, new_size);
6937 if (s->flags & SLAB_RED_ZONE && new_size < ks)
6938 memset_no_sanitize_memory(kasan_reset_tag(p) + new_size,
6939 SLUB_RED_ACTIVE, ks - new_size);
6940 }
6941
6942 p = kasan_krealloc(p, new_size, flags);
6943 return (void *)p;
6944
6945 alloc_new:
6946 ret = __kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(new_size, NULL, token), flags, nid, _RET_IP_);
6947 if (ret && p) {
6948 /* Disable KASAN checks as the object's redzone is accessed. */
6949 kasan_disable_current();
6950 memcpy(ret, kasan_reset_tag(p), min(new_size, (size_t)(orig_size ?: ks)));
6951 kasan_enable_current();
6952 }
6953
6954 return ret;
6955 }
6956
krealloc_node_align_noprof(const void * p,DECL_TOKEN_PARAMS (new_size,token),unsigned long align,gfp_t flags,int nid)6957 void *krealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(new_size, token), unsigned long align,
6958 gfp_t flags, int nid)
6959 {
6960 void *ret;
6961
6962 if (unlikely(!new_size)) {
6963 kfree(p);
6964 return ZERO_SIZE_PTR;
6965 }
6966
6967 ret = __do_krealloc(p, new_size, align, flags, nid, PASS_TOKEN_PARAM(token));
6968 if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
6969 kfree(p);
6970
6971 return ret;
6972 }
6973 EXPORT_SYMBOL(krealloc_node_align_noprof);
6974
kmalloc_gfp_adjust(gfp_t flags,size_t size)6975 static gfp_t kmalloc_gfp_adjust(gfp_t flags, size_t size)
6976 {
6977 /*
6978 * We want to attempt a large physically contiguous block first because
6979 * it is less likely to fragment multiple larger blocks and therefore
6980 * contribute to a long term fragmentation less than vmalloc fallback.
6981 * However make sure that larger requests are not too disruptive - i.e.
6982 * do not direct reclaim unless physically continuous memory is preferred
6983 * (__GFP_RETRY_MAYFAIL mode). We still kick in kswapd/kcompactd to
6984 * start working in the background
6985 */
6986 if (size > PAGE_SIZE) {
6987 flags |= __GFP_NOWARN;
6988
6989 if (!(flags & __GFP_RETRY_MAYFAIL))
6990 flags &= ~__GFP_DIRECT_RECLAIM;
6991
6992 /* nofail semantic is implemented by the vmalloc fallback */
6993 flags &= ~__GFP_NOFAIL;
6994 }
6995
6996 return flags;
6997 }
6998
__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS (size,b,token),unsigned long align,gfp_t flags,int node)6999 void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
7000 gfp_t flags, int node)
7001 {
7002 bool allow_block;
7003 void *ret;
7004 const struct slab_alloc_context ac = {
7005 .caller_addr = _RET_IP_,
7006 .orig_size = size,
7007 .alloc_flags = SLAB_ALLOC_DEFAULT,
7008 };
7009
7010 /*
7011 * It doesn't really make sense to fallback to vmalloc for sub page
7012 * requests
7013 */
7014 ret = __do_kmalloc_node(PASS_BUCKET_PARAM(b),
7015 kmalloc_gfp_adjust(flags, size),
7016 node, PASS_TOKEN_PARAM(token), &ac);
7017 if (ret || size <= PAGE_SIZE)
7018 return ret;
7019
7020 /* Don't even allow crazy sizes */
7021 if (unlikely(size > INT_MAX)) {
7022 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
7023 return NULL;
7024 }
7025
7026 /*
7027 * For non-blocking the VM_ALLOW_HUGE_VMAP is not used
7028 * because the huge-mapping path in vmalloc contains at
7029 * least one might_sleep() call.
7030 *
7031 * TODO: Revise huge-mapping path to support non-blocking
7032 * flags.
7033 */
7034 allow_block = gfpflags_allow_blocking(flags);
7035
7036 /*
7037 * kvmalloc() can always use VM_ALLOW_HUGE_VMAP,
7038 * since the callers already cannot assume anything
7039 * about the resulting pointer, and cannot play
7040 * protection games.
7041 */
7042 return __vmalloc_node_range_noprof(size, align, VMALLOC_START, VMALLOC_END,
7043 flags, PAGE_KERNEL, allow_block ? VM_ALLOW_HUGE_VMAP:0,
7044 node, __builtin_return_address(0));
7045 }
7046 EXPORT_SYMBOL(__kvmalloc_node_noprof);
7047
7048 /**
7049 * kvfree() - Free memory.
7050 * @addr: Pointer to allocated memory.
7051 *
7052 * kvfree frees memory allocated by any of vmalloc(), kmalloc() or kvmalloc().
7053 * It is slightly more efficient to use kfree() or vfree() if you are certain
7054 * that you know which one to use.
7055 *
7056 * Context: Either preemptible task context or not-NMI interrupt.
7057 */
kvfree(const void * addr)7058 void kvfree(const void *addr)
7059 {
7060 if (is_vmalloc_addr(addr))
7061 vfree(addr);
7062 else
7063 kfree(addr);
7064 }
7065 EXPORT_SYMBOL(kvfree);
7066
7067 /**
7068 * kvfree_atomic() - Free memory.
7069 * @addr: Pointer to allocated memory.
7070 *
7071 * Same as kvfree(), but uses vfree_atomic() for vmalloc
7072 * backed memory. Must not be called from NMI context.
7073 */
kvfree_atomic(const void * addr)7074 void kvfree_atomic(const void *addr)
7075 {
7076 if (is_vmalloc_addr(addr))
7077 vfree_atomic(addr);
7078 else
7079 kfree(addr);
7080 }
7081 EXPORT_SYMBOL(kvfree_atomic);
7082
7083 /**
7084 * kvfree_sensitive - Free a data object containing sensitive information.
7085 * @addr: address of the data object to be freed.
7086 * @len: length of the data object.
7087 *
7088 * Use the special memzero_explicit() function to clear the content of a
7089 * kvmalloc'ed object containing sensitive data to make sure that the
7090 * compiler won't optimize out the data clearing.
7091 */
kvfree_sensitive(const void * addr,size_t len)7092 void kvfree_sensitive(const void *addr, size_t len)
7093 {
7094 if (likely(!ZERO_OR_NULL_PTR(addr))) {
7095 memzero_explicit((void *)addr, len);
7096 kvfree(addr);
7097 }
7098 }
7099 EXPORT_SYMBOL(kvfree_sensitive);
7100
kvrealloc_node_align_noprof(const void * p,DECL_TOKEN_PARAMS (size,token),unsigned long align,gfp_t flags,int nid)7101 void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token), unsigned long align,
7102 gfp_t flags, int nid)
7103 {
7104 void *n;
7105
7106 if (is_vmalloc_addr(p))
7107 return vrealloc_node_align_noprof(p, size, align, flags, nid);
7108
7109 n = krealloc_node_align_noprof(p, PASS_TOKEN_PARAMS(size, token), align, kmalloc_gfp_adjust(flags, size), nid);
7110 if (!n) {
7111 /* We failed to krealloc(), fall back to kvmalloc(). */
7112 n = __kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(size, NULL, token), align, flags, nid);
7113 if (!n)
7114 return NULL;
7115
7116 if (p) {
7117 /* We already know that `p` is not a vmalloc address. */
7118 kasan_disable_current();
7119 memcpy(n, kasan_reset_tag(p), min(size, ksize(p)));
7120 kasan_enable_current();
7121
7122 kfree(p);
7123 }
7124 }
7125
7126 return n;
7127 }
7128 EXPORT_SYMBOL(kvrealloc_node_align_noprof);
7129
7130 struct detached_freelist {
7131 struct slab *slab;
7132 void *tail;
7133 void *freelist;
7134 int cnt;
7135 struct kmem_cache *s;
7136 };
7137
7138 /*
7139 * This function progressively scans the array with free objects (with
7140 * a limited look ahead) and extract objects belonging to the same
7141 * slab. It builds a detached freelist directly within the given
7142 * slab/objects. This can happen without any need for
7143 * synchronization, because the objects are owned by running process.
7144 * The freelist is build up as a single linked list in the objects.
7145 * The idea is, that this detached freelist can then be bulk
7146 * transferred to the real freelist(s), but only requiring a single
7147 * synchronization primitive. Look ahead in the array is limited due
7148 * to performance reasons.
7149 */
7150 static inline
build_detached_freelist(struct kmem_cache * s,size_t size,void ** p,struct detached_freelist * df)7151 int build_detached_freelist(struct kmem_cache *s, size_t size,
7152 void **p, struct detached_freelist *df)
7153 {
7154 int lookahead = 3;
7155 void *object;
7156 struct page *page;
7157 struct slab *slab;
7158 size_t same;
7159
7160 object = p[--size];
7161 page = virt_to_page(object);
7162 slab = page_slab(page);
7163 if (!s) {
7164 /* Handle kalloc'ed objects */
7165 if (!slab) {
7166 free_large_kmalloc(page, object);
7167 df->slab = NULL;
7168 return size;
7169 }
7170 /* Derive kmem_cache from object */
7171 df->slab = slab;
7172 df->s = slab->slab_cache;
7173 } else {
7174 df->slab = slab;
7175 df->s = s;
7176 }
7177
7178 /* Start new detached freelist */
7179 df->tail = object;
7180 df->freelist = object;
7181 df->cnt = 1;
7182
7183 if (is_kfence_address(object))
7184 return size;
7185
7186 set_freepointer(df->s, object, NULL);
7187
7188 same = size;
7189 while (size) {
7190 object = p[--size];
7191 /* df->slab is always set at this point */
7192 if (df->slab == virt_to_slab(object)) {
7193 /* Opportunity build freelist */
7194 set_freepointer(df->s, object, df->freelist);
7195 df->freelist = object;
7196 df->cnt++;
7197 same--;
7198 if (size != same)
7199 swap(p[size], p[same]);
7200 continue;
7201 }
7202
7203 /* Limit look ahead search */
7204 if (!--lookahead)
7205 break;
7206 }
7207
7208 return same;
7209 }
7210
7211 /*
7212 * Internal bulk free of objects that were not initialised by the post alloc
7213 * hooks and thus should not be processed by the free hooks
7214 */
__kmem_cache_free_bulk(struct kmem_cache * s,size_t size,void ** p)7215 static void __kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
7216 {
7217 if (!size)
7218 return;
7219
7220 do {
7221 struct detached_freelist df;
7222
7223 size = build_detached_freelist(s, size, p, &df);
7224 if (!df.slab)
7225 continue;
7226
7227 if (kfence_free(df.freelist))
7228 continue;
7229
7230 __slab_free(df.s, df.slab, df.freelist, df.tail, df.cnt,
7231 _RET_IP_);
7232 } while (likely(size));
7233 }
7234
7235 /* Note that interrupts must be enabled when calling this function. */
kmem_cache_free_bulk(struct kmem_cache * s,size_t size,void ** p)7236 void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
7237 {
7238 if (!size)
7239 return;
7240
7241 /*
7242 * freeing to sheaves is so incompatible with the detached freelist so
7243 * once we go that way, we have to do everything differently
7244 */
7245 if (s && cache_has_sheaves(s)) {
7246 free_to_pcs_bulk(s, size, p);
7247 return;
7248 }
7249
7250 do {
7251 struct detached_freelist df;
7252
7253 size = build_detached_freelist(s, size, p, &df);
7254 if (!df.slab)
7255 continue;
7256
7257 slab_free_bulk(df.s, df.slab, df.freelist, df.tail, &p[size],
7258 df.cnt, _RET_IP_);
7259 } while (likely(size));
7260 }
7261 EXPORT_SYMBOL(kmem_cache_free_bulk);
7262
7263 static unsigned int
__refill_objects_node(struct kmem_cache * s,void ** p,gfp_t gfp,unsigned int min,unsigned int max,struct kmem_cache_node * n,bool allow_spin)7264 __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
7265 unsigned int max, struct kmem_cache_node *n,
7266 bool allow_spin)
7267 {
7268 struct partial_bulk_context pc;
7269 struct slab *slab, *slab2;
7270 unsigned int refilled = 0;
7271 unsigned long flags;
7272 void *object;
7273
7274 pc.flags = gfp;
7275 pc.min_objects = min;
7276 pc.max_objects = max;
7277
7278 if (!get_partial_node_bulk(s, n, &pc, allow_spin))
7279 return 0;
7280
7281 list_for_each_entry_safe(slab, slab2, &pc.slabs, slab_list) {
7282
7283 unsigned int count;
7284
7285 list_del(&slab->slab_list);
7286
7287 object = get_freelist_nofreeze(s, slab, &count);
7288
7289 while (count && refilled < max) {
7290 p[refilled] = object;
7291 object = get_freepointer(s, object);
7292 maybe_wipe_obj_freeptr(s, p[refilled]);
7293
7294 refilled++;
7295 count--;
7296 }
7297
7298 /*
7299 * Freelist had more objects than we can accommodate, we need to
7300 * free them back. First we try to be optimistic and assume the
7301 * slab is still full since we just detached its freelist.
7302 * Otherwise we must find the tail object.
7303 */
7304 if (unlikely(count)) {
7305 void *head = object;
7306 void *tail;
7307
7308 if (__slab_try_return_freelist(s, n, slab, head, count))
7309 break;
7310
7311 do {
7312 tail = object;
7313 object = get_freepointer(s, object);
7314 } while (object);
7315 __slab_free(s, slab, head, tail, count, _RET_IP_);
7316 }
7317
7318 if (refilled >= max)
7319 break;
7320 }
7321
7322 if (unlikely(!list_empty(&pc.slabs))) {
7323 spin_lock_irqsave(&n->list_lock, flags);
7324
7325 list_for_each_entry(slab, &pc.slabs, slab_list)
7326 set_node_partial_state(n, slab);
7327
7328 list_splice_tail(&pc.slabs, &n->partial);
7329
7330 spin_unlock_irqrestore(&n->list_lock, flags);
7331 }
7332
7333 return refilled;
7334 }
7335
7336 #ifdef CONFIG_NUMA
7337 static unsigned int
__refill_objects_any(struct kmem_cache * s,void ** p,gfp_t gfp,unsigned int min,unsigned int max)7338 __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
7339 unsigned int max)
7340 {
7341 struct zonelist *zonelist;
7342 struct zoneref *z;
7343 struct zone *zone;
7344 enum zone_type highest_zoneidx = gfp_zone(gfp);
7345 unsigned int cpuset_mems_cookie;
7346 unsigned int refilled = 0;
7347
7348 /* see get_from_any_partial() for the defrag ratio description */
7349 if (!s->remote_node_defrag_ratio ||
7350 get_cycles() % 1024 > s->remote_node_defrag_ratio)
7351 return 0;
7352
7353 do {
7354 cpuset_mems_cookie = read_mems_allowed_begin();
7355 zonelist = node_zonelist(mempolicy_slab_node(), gfp);
7356 for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
7357 struct kmem_cache_node *n;
7358 unsigned int r;
7359
7360 n = get_node(s, zone_to_nid(zone));
7361
7362 if (!n || !cpuset_zone_allowed(zone, gfp) ||
7363 n->nr_partial <= s->min_partial)
7364 continue;
7365
7366 r = __refill_objects_node(s, p, gfp, min, max, n,
7367 /* allow_spin = */ false);
7368 refilled += r;
7369
7370 if (r >= min) {
7371 /*
7372 * Don't check read_mems_allowed_retry() here -
7373 * if mems_allowed was updated in parallel, that
7374 * was a harmless race between allocation and
7375 * the cpuset update
7376 */
7377 return refilled;
7378 }
7379 p += r;
7380 min -= r;
7381 max -= r;
7382 }
7383 } while (read_mems_allowed_retry(cpuset_mems_cookie));
7384
7385 return refilled;
7386 }
7387 #else
7388 static inline unsigned int
__refill_objects_any(struct kmem_cache * s,void ** p,gfp_t gfp,unsigned int min,unsigned int max)7389 __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
7390 unsigned int max)
7391 {
7392 return 0;
7393 }
7394 #endif
7395
7396 static unsigned int
refill_objects(struct kmem_cache * s,void ** p,gfp_t gfp,unsigned int min,unsigned int max)7397 refill_objects(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min,
7398 unsigned int max)
7399 {
7400 int local_node = numa_mem_id();
7401 unsigned int refilled;
7402 struct slab *slab;
7403
7404 refilled = __refill_objects_node(s, p, gfp, min, max,
7405 get_node(s, local_node),
7406 /* allow_spin = */ true);
7407 if (refilled >= min)
7408 return refilled;
7409
7410 refilled += __refill_objects_any(s, p + refilled, gfp, min - refilled,
7411 max - refilled);
7412 if (refilled >= min)
7413 return refilled;
7414
7415 new_slab:
7416
7417 slab = new_slab(s, gfp, SLAB_ALLOC_DEFAULT, local_node);
7418 if (!slab)
7419 goto out;
7420
7421 stat(s, ALLOC_SLAB);
7422
7423 refilled += alloc_from_new_slab(s, slab, p + refilled, max - refilled,
7424 /* allow_spin = */ true);
7425
7426 if (refilled < min)
7427 goto new_slab;
7428
7429 out:
7430 return refilled;
7431 }
7432
__kmem_cache_alloc_bulk(struct kmem_cache * s,gfp_t flags,size_t size,void ** p)7433 static bool __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags,
7434 size_t size, void **p)
7435 {
7436 int i;
7437
7438 if (IS_ENABLED(CONFIG_SLUB_TINY) || kmem_cache_debug(s)) {
7439 const struct slab_alloc_context ac = {
7440 .caller_addr = _RET_IP_,
7441 .orig_size = s->object_size,
7442 .alloc_flags = SLAB_ALLOC_DEFAULT,
7443 };
7444 for (i = 0; i < size; i++) {
7445
7446 p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE, &ac);
7447 if (unlikely(!p[i]))
7448 goto error;
7449
7450 maybe_wipe_obj_freeptr(s, p[i]);
7451 }
7452 } else {
7453 i = refill_objects(s, p, flags, size, size);
7454 if (i < size)
7455 goto error;
7456 stat_add(s, ALLOC_SLOWPATH, i);
7457 }
7458
7459 return true;
7460
7461 error:
7462 __kmem_cache_free_bulk(s, i, p);
7463 return false;
7464 }
7465
7466 /**
7467 * kmem_cache_alloc_bulk - Allocate multiple objects
7468 * @s: The cache to allocate from
7469 * @flags: GFP_* flags. See kmalloc().
7470 * @size: Number of objects to allocate
7471 * @p: Array of allocated objects
7472 *
7473 * Allocate @size objects from @s and places them into @p. @size must be larger
7474 * than 0.
7475 *
7476 * Interrupts must be enabled when calling this function.
7477 *
7478 * Unlike alloc_pages_bulk(), this function does not check for already allocated
7479 * objects in @p, and thus the caller does not need to zero it.
7480 *
7481 * Return: %true if the allocation succeeded, or %false if it failed.
7482 */
kmem_cache_alloc_bulk_noprof(struct kmem_cache * s,gfp_t flags,size_t size,void ** p)7483 bool kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags,
7484 size_t size, void **p)
7485 {
7486 unsigned int i = 0;
7487 void *kfence_obj;
7488 const struct slab_alloc_context ac = {
7489 .orig_size = s->object_size,
7490 .alloc_flags = SLAB_ALLOC_DEFAULT,
7491 };
7492
7493 if (!size)
7494 return false;
7495
7496 s = slab_pre_alloc_hook(s, flags);
7497 if (unlikely(!s))
7498 return false;
7499
7500 /*
7501 * to make things simpler, only assume at most once kfence allocated
7502 * object per bulk allocation and choose its index randomly
7503 */
7504 kfence_obj = kfence_alloc(s, s->object_size, flags);
7505
7506 if (unlikely(kfence_obj)) {
7507 if (unlikely(size == 1)) {
7508 p[0] = kfence_obj;
7509 goto out;
7510 }
7511 size--;
7512 }
7513
7514 i = alloc_from_pcs_bulk(s, size, p);
7515 if (i < size) {
7516 /*
7517 * If we ran out of memory, don't bother with freeing back to
7518 * the percpu sheaves, we have bigger problems.
7519 */
7520 if (unlikely(!__kmem_cache_alloc_bulk(s, flags, size - i,
7521 p + i))) {
7522 if (i > 0)
7523 __kmem_cache_free_bulk(s, i, p);
7524 if (kfence_obj)
7525 __kfence_free(kfence_obj);
7526 return false;
7527 }
7528 }
7529
7530 if (unlikely(kfence_obj)) {
7531 int idx = get_random_u32_below(size + 1);
7532
7533 if (idx != size)
7534 p[size] = p[idx];
7535 p[idx] = kfence_obj;
7536
7537 size++;
7538 }
7539
7540 out:
7541 /* memcg and kmem_cache debug support and memory initialization */
7542 return likely(slab_post_alloc_hook(s, flags, size, p, &ac));
7543 }
7544 EXPORT_SYMBOL(kmem_cache_alloc_bulk_noprof);
7545
7546 /*
7547 * Object placement in a slab is made very easy because we always start at
7548 * offset 0. If we tune the size of the object to the alignment then we can
7549 * get the required alignment by putting one properly sized object after
7550 * another.
7551 *
7552 * Notice that the allocation order determines the sizes of the per cpu
7553 * caches. Each processor has always one slab available for allocations.
7554 * Increasing the allocation order reduces the number of times that slabs
7555 * must be moved on and off the partial lists and is therefore a factor in
7556 * locking overhead.
7557 */
7558
7559 /*
7560 * Minimum / Maximum order of slab pages. This influences locking overhead
7561 * and slab fragmentation. A higher order reduces the number of partial slabs
7562 * and increases the number of allocations possible without having to
7563 * take the list_lock.
7564 */
7565 static unsigned int slub_min_order;
7566 static unsigned int slub_max_order =
7567 IS_ENABLED(CONFIG_SLUB_TINY) ? 1 : PAGE_ALLOC_COSTLY_ORDER;
7568 static unsigned int slub_min_objects;
7569
7570 /*
7571 * Calculate the order of allocation given an slab object size.
7572 *
7573 * The order of allocation has significant impact on performance and other
7574 * system components. Generally order 0 allocations should be preferred since
7575 * order 0 does not cause fragmentation in the page allocator. Larger objects
7576 * be problematic to put into order 0 slabs because there may be too much
7577 * unused space left. We go to a higher order if more than 1/16th of the slab
7578 * would be wasted.
7579 *
7580 * In order to reach satisfactory performance we must ensure that a minimum
7581 * number of objects is in one slab. Otherwise we may generate too much
7582 * activity on the partial lists which requires taking the list_lock. This is
7583 * less a concern for large slabs though which are rarely used.
7584 *
7585 * slab_max_order specifies the order where we begin to stop considering the
7586 * number of objects in a slab as critical. If we reach slab_max_order then
7587 * we try to keep the page order as low as possible. So we accept more waste
7588 * of space in favor of a small page order.
7589 *
7590 * Higher order allocations also allow the placement of more objects in a
7591 * slab and thereby reduce object handling overhead. If the user has
7592 * requested a higher minimum order then we start with that one instead of
7593 * the smallest order which will fit the object.
7594 */
calc_slab_order(unsigned int size,unsigned int min_order,unsigned int max_order,unsigned int fract_leftover)7595 static inline unsigned int calc_slab_order(unsigned int size,
7596 unsigned int min_order, unsigned int max_order,
7597 unsigned int fract_leftover)
7598 {
7599 unsigned int order;
7600
7601 for (order = min_order; order <= max_order; order++) {
7602
7603 unsigned int slab_size = (unsigned int)PAGE_SIZE << order;
7604 unsigned int rem;
7605
7606 rem = slab_size % size;
7607
7608 if (rem <= slab_size / fract_leftover)
7609 break;
7610 }
7611
7612 return order;
7613 }
7614
calculate_order(unsigned int size)7615 static inline int calculate_order(unsigned int size)
7616 {
7617 unsigned int order;
7618 unsigned int min_objects;
7619 unsigned int max_objects;
7620 unsigned int min_order;
7621
7622 min_objects = slub_min_objects;
7623 if (!min_objects) {
7624 /*
7625 * Some architectures will only update present cpus when
7626 * onlining them, so don't trust the number if it's just 1. But
7627 * we also don't want to use nr_cpu_ids always, as on some other
7628 * architectures, there can be many possible cpus, but never
7629 * onlined. Here we compromise between trying to avoid too high
7630 * order on systems that appear larger than they are, and too
7631 * low order on systems that appear smaller than they are.
7632 */
7633 unsigned int nr_cpus = num_present_cpus();
7634 if (nr_cpus <= 1)
7635 nr_cpus = nr_cpu_ids;
7636 min_objects = 4 * (fls(nr_cpus) + 1);
7637 }
7638 /* min_objects can't be 0 because get_order(0) is undefined */
7639 max_objects = max(order_objects(slub_max_order, size), 1U);
7640 min_objects = min(min_objects, max_objects);
7641
7642 min_order = max_t(unsigned int, slub_min_order,
7643 get_order(min_objects * size));
7644 if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
7645 return get_order(size * MAX_OBJS_PER_PAGE) - 1;
7646
7647 /*
7648 * Attempt to find best configuration for a slab. This works by first
7649 * attempting to generate a layout with the best possible configuration
7650 * and backing off gradually.
7651 *
7652 * We start with accepting at most 1/16 waste and try to find the
7653 * smallest order from min_objects-derived/slab_min_order up to
7654 * slab_max_order that will satisfy the constraint. Note that increasing
7655 * the order can only result in same or less fractional waste, not more.
7656 *
7657 * If that fails, we increase the acceptable fraction of waste and try
7658 * again. The last iteration with fraction of 1/2 would effectively
7659 * accept any waste and give us the order determined by min_objects, as
7660 * long as at least single object fits within slab_max_order.
7661 */
7662 for (unsigned int fraction = 16; fraction > 1; fraction /= 2) {
7663 order = calc_slab_order(size, min_order, slub_max_order,
7664 fraction);
7665 if (order <= slub_max_order)
7666 return order;
7667 }
7668
7669 /*
7670 * Doh this slab cannot be placed using slab_max_order.
7671 */
7672 order = get_order(size);
7673 if (order <= MAX_PAGE_ORDER)
7674 return order;
7675 return -ENOSYS;
7676 }
7677
7678 static void
init_kmem_cache_node(struct kmem_cache_node * n)7679 init_kmem_cache_node(struct kmem_cache_node *n)
7680 {
7681 n->nr_partial = 0;
7682 spin_lock_init(&n->list_lock);
7683 INIT_LIST_HEAD(&n->partial);
7684 #ifdef CONFIG_SLUB_DEBUG
7685 atomic_long_set(&n->nr_slabs, 0);
7686 atomic_long_set(&n->total_objects, 0);
7687 INIT_LIST_HEAD(&n->full);
7688 #endif
7689 }
7690
7691 #ifdef CONFIG_SLUB_STATS
alloc_kmem_cache_stats(struct kmem_cache * s)7692 static inline int alloc_kmem_cache_stats(struct kmem_cache *s)
7693 {
7694 BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
7695 NR_KMALLOC_TYPES * KMALLOC_SHIFT_HIGH *
7696 sizeof(struct kmem_cache_stats));
7697
7698 s->cpu_stats = alloc_percpu(struct kmem_cache_stats);
7699
7700 if (!s->cpu_stats)
7701 return 0;
7702
7703 return 1;
7704 }
7705 #endif
7706
init_percpu_sheaves(struct kmem_cache * s)7707 static int init_percpu_sheaves(struct kmem_cache *s)
7708 {
7709 static struct slab_sheaf bootstrap_sheaf = {};
7710 int cpu;
7711
7712 for_each_possible_cpu(cpu) {
7713 struct slub_percpu_sheaves *pcs;
7714
7715 pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
7716
7717 local_trylock_init(&pcs->lock);
7718
7719 /*
7720 * Bootstrap sheaf has zero size so fast-path allocation fails.
7721 * It has also size == s->sheaf_capacity, so fast-path free
7722 * fails. In the slow paths we recognize the situation by
7723 * checking s->sheaf_capacity. This allows fast paths to assume
7724 * s->cpu_sheaves and pcs->main always exists and are valid.
7725 * It's also safe to share the single static bootstrap_sheaf
7726 * with zero-sized objects array as it's never modified.
7727 *
7728 * Bootstrap_sheaf also has NULL pointer to kmem_cache so we
7729 * recognize it and not attempt to free it when destroying the
7730 * cache.
7731 *
7732 * We keep bootstrap_sheaf for kmem_cache and kmem_cache_node,
7733 * caches with debug enabled, and all caches with SLUB_TINY.
7734 * For kmalloc caches it's used temporarily during the initial
7735 * bootstrap.
7736 */
7737 if (!s->sheaf_capacity)
7738 pcs->main = &bootstrap_sheaf;
7739 else
7740 pcs->main = alloc_empty_sheaf(s, GFP_KERNEL, SLAB_ALLOC_DEFAULT);
7741
7742 if (!pcs->main)
7743 return -ENOMEM;
7744 }
7745
7746 return 0;
7747 }
7748
7749 static struct kmem_cache *kmem_cache_node;
7750
7751 /*
7752 * No kmalloc_node yet so do it by hand. We know that this is the first
7753 * slab on the node for this slabcache. There are no concurrent accesses
7754 * possible.
7755 *
7756 * Note that this function only works on the kmem_cache_node
7757 * when allocating for the kmem_cache_node. This is used for bootstrapping
7758 * memory on a fresh node that has no slab structures yet.
7759 */
early_kmem_cache_node_alloc(int node)7760 static void early_kmem_cache_node_alloc(int node)
7761 {
7762 struct slab *slab;
7763 struct kmem_cache_node *n;
7764 struct slab_obj_iter iter;
7765
7766 BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
7767
7768 slab = new_slab(kmem_cache_node, GFP_NOWAIT, SLAB_ALLOC_DEFAULT, node);
7769
7770 BUG_ON(!slab);
7771 if (slab_nid(slab) != node) {
7772 pr_err("SLUB: Unable to allocate memory from node %d\n", node);
7773 pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
7774 }
7775
7776 init_slab_obj_iter(kmem_cache_node, slab, &iter, true);
7777
7778 n = next_slab_obj(kmem_cache_node, &iter);
7779 BUG_ON(!n);
7780
7781 slab->inuse = 1;
7782 build_slab_freelist(kmem_cache_node, slab, &iter);
7783
7784 #ifdef CONFIG_SLUB_DEBUG
7785 init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
7786 #endif
7787 n = kasan_slab_alloc(kmem_cache_node, n, GFP_KERNEL, false);
7788 kmem_cache_node->per_node[node].node = n;
7789 init_kmem_cache_node(n);
7790 inc_slabs_node(kmem_cache_node, node, slab->objects);
7791
7792 /*
7793 * No locks need to be taken here as it has just been
7794 * initialized and there is no concurrent access.
7795 */
7796 __add_partial(n, slab, ADD_TO_HEAD);
7797 }
7798
free_kmem_cache_nodes(struct kmem_cache * s)7799 static void free_kmem_cache_nodes(struct kmem_cache *s)
7800 {
7801 int node;
7802 struct kmem_cache_node *n;
7803
7804 for_each_node(node) {
7805 struct node_barn *barn = get_barn_node(s, node);
7806
7807 if (!barn)
7808 continue;
7809
7810 WARN_ON(barn->nr_full);
7811 WARN_ON(barn->nr_empty);
7812 kfree(barn);
7813 s->per_node[node].barn = NULL;
7814 }
7815
7816 for_each_kmem_cache_node(s, node, n) {
7817 s->per_node[node].node = NULL;
7818 kmem_cache_free(kmem_cache_node, n);
7819 }
7820 }
7821
__kmem_cache_release(struct kmem_cache * s)7822 void __kmem_cache_release(struct kmem_cache *s)
7823 {
7824 cache_random_seq_destroy(s);
7825 pcs_destroy(s);
7826 #ifdef CONFIG_SLUB_STATS
7827 free_percpu(s->cpu_stats);
7828 #endif
7829 free_kmem_cache_nodes(s);
7830 }
7831
init_kmem_cache_nodes(struct kmem_cache * s)7832 static int init_kmem_cache_nodes(struct kmem_cache *s)
7833 {
7834 int node;
7835
7836 for_each_node_mask(node, slab_nodes) {
7837 struct kmem_cache_node *n;
7838
7839 if (slab_state == DOWN) {
7840 early_kmem_cache_node_alloc(node);
7841 continue;
7842 }
7843
7844 n = kmem_cache_alloc_node(kmem_cache_node,
7845 GFP_KERNEL, node);
7846 if (!n)
7847 return 0;
7848
7849 init_kmem_cache_node(n);
7850 s->per_node[node].node = n;
7851 }
7852
7853 if (slab_state == DOWN || !cache_has_sheaves(s))
7854 return 1;
7855
7856 for_each_node_mask(node, slab_barn_nodes) {
7857 struct node_barn *barn;
7858
7859 barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, node);
7860
7861 if (!barn)
7862 return 0;
7863
7864 barn_init(barn);
7865 s->per_node[node].barn = barn;
7866 }
7867
7868 return 1;
7869 }
7870
calculate_sheaf_capacity(struct kmem_cache * s,struct kmem_cache_args * args)7871 static unsigned int calculate_sheaf_capacity(struct kmem_cache *s,
7872 struct kmem_cache_args *args)
7873
7874 {
7875 unsigned int capacity;
7876 size_t size;
7877
7878
7879 if (IS_ENABLED(CONFIG_SLUB_TINY) || s->flags & SLAB_DEBUG_FLAGS)
7880 return 0;
7881
7882 /*
7883 * Bootstrap caches can't have sheaves for now (SLAB_NO_SHEAVES).
7884 * SLAB_NOLEAKTRACE caches (e.g., kmemleak's object_cache) must not
7885 * have sheaves to avoid recursion when sheaf allocation triggers
7886 * kmemleak tracking.
7887 */
7888 if (s->flags & (SLAB_NO_SHEAVES | SLAB_NOLEAKTRACE))
7889 return 0;
7890
7891 /*
7892 * For now we use roughly similar formula (divided by two as there are
7893 * two percpu sheaves) as what was used for percpu partial slabs, which
7894 * should result in similar lock contention (barn or list_lock)
7895 */
7896 if (s->size >= PAGE_SIZE)
7897 capacity = 4;
7898 else if (s->size >= 1024)
7899 capacity = 12;
7900 else if (s->size >= 256)
7901 capacity = 26;
7902 else
7903 capacity = 60;
7904
7905 /* Increment capacity to make sheaf exactly a kmalloc size bucket */
7906 size = struct_size_t(struct slab_sheaf, objects, capacity);
7907 size = kmalloc_size_roundup(size);
7908 capacity = (size - struct_size_t(struct slab_sheaf, objects, 0)) / sizeof(void *);
7909
7910 /*
7911 * Respect an explicit request for capacity that's typically motivated by
7912 * expected maximum size of kmem_cache_prefill_sheaf() to not end up
7913 * using low-performance oversize sheaves
7914 */
7915 return max(capacity, args->sheaf_capacity);
7916 }
7917
7918 /*
7919 * calculate_sizes() determines the order and the distribution of data within
7920 * a slab object.
7921 */
calculate_sizes(struct kmem_cache_args * args,struct kmem_cache * s)7922 static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)
7923 {
7924 slab_flags_t flags = s->flags;
7925 unsigned int size = s->object_size;
7926 unsigned int aligned_size;
7927 unsigned int order;
7928
7929 /*
7930 * Round up object size to the next word boundary. We can only
7931 * place the free pointer at word boundaries and this determines
7932 * the possible location of the free pointer.
7933 */
7934 size = ALIGN(size, sizeof(void *));
7935
7936 #ifdef CONFIG_SLUB_DEBUG
7937 /*
7938 * Determine if we can poison the object itself. If the user of
7939 * the slab may touch the object after free or before allocation
7940 * then we should never poison the object itself.
7941 */
7942 if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) &&
7943 !s->ctor)
7944 s->flags |= __OBJECT_POISON;
7945 else
7946 s->flags &= ~__OBJECT_POISON;
7947
7948
7949 /*
7950 * If we are Redzoning and there is no space between the end of the
7951 * object and the following fields, add one word so the right Redzone
7952 * is non-empty.
7953 */
7954 if ((flags & SLAB_RED_ZONE) && size == s->object_size)
7955 size += sizeof(void *);
7956 #endif
7957
7958 /*
7959 * With that we have determined the number of bytes in actual use
7960 * by the object and redzoning.
7961 */
7962 s->inuse = size;
7963
7964 if (((flags & SLAB_TYPESAFE_BY_RCU) && !args->use_freeptr_offset) ||
7965 (flags & SLAB_POISON) ||
7966 (s->ctor && !args->use_freeptr_offset) ||
7967 ((flags & SLAB_RED_ZONE) &&
7968 (s->object_size < sizeof(void *) || slub_debug_orig_size(s)))) {
7969 /*
7970 * Relocate free pointer after the object if it is not
7971 * permitted to overwrite the first word of the object on
7972 * kmem_cache_free.
7973 *
7974 * This is the case if we do RCU, have a constructor, are
7975 * poisoning the objects, or are redzoning an object smaller
7976 * than sizeof(void *) or are redzoning an object with
7977 * slub_debug_orig_size() enabled, in which case the right
7978 * redzone may be extended.
7979 *
7980 * The assumption that s->offset >= s->inuse means free
7981 * pointer is outside of the object is used in the
7982 * freeptr_outside_object() function. If that is no
7983 * longer true, the function needs to be modified.
7984 */
7985 s->offset = size;
7986 size += sizeof(void *);
7987 } else if (((flags & SLAB_TYPESAFE_BY_RCU) || s->ctor) &&
7988 args->use_freeptr_offset) {
7989 s->offset = args->freeptr_offset;
7990 } else {
7991 /*
7992 * Store freelist pointer near middle of object to keep
7993 * it away from the edges of the object to avoid small
7994 * sized over/underflows from neighboring allocations.
7995 */
7996 s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *));
7997 }
7998
7999 #ifdef CONFIG_SLUB_DEBUG
8000 if (flags & SLAB_STORE_USER) {
8001 /*
8002 * Need to store information about allocs and frees after
8003 * the object.
8004 */
8005 size += 2 * sizeof(struct track);
8006
8007 /* Save the original kmalloc request size */
8008 if (flags & SLAB_KMALLOC)
8009 size += sizeof(unsigned long);
8010 }
8011 #endif
8012
8013 kasan_cache_create(s, &size, &s->flags);
8014 #ifdef CONFIG_SLUB_DEBUG
8015 if (flags & SLAB_RED_ZONE) {
8016 /*
8017 * Add some empty padding so that we can catch
8018 * overwrites from earlier objects rather than let
8019 * tracking information or the free pointer be
8020 * corrupted if a user writes before the start
8021 * of the object.
8022 */
8023 size += sizeof(void *);
8024
8025 s->red_left_pad = sizeof(void *);
8026 s->red_left_pad = ALIGN(s->red_left_pad, s->align);
8027 size += s->red_left_pad;
8028 }
8029 #endif
8030
8031 /*
8032 * SLUB stores one object immediately after another beginning from
8033 * offset 0. In order to align the objects we have to simply size
8034 * each object to conform to the alignment.
8035 */
8036 aligned_size = ALIGN(size, s->align);
8037 #if defined(CONFIG_SLAB_OBJ_EXT) && defined(CONFIG_64BIT)
8038 if (slab_args_unmergeable(args, s->flags) &&
8039 (aligned_size - size >= cache_obj_ext_size(s)))
8040 s->flags |= SLAB_OBJ_EXT_IN_OBJ;
8041 #endif
8042 size = aligned_size;
8043
8044 s->size = size;
8045 s->reciprocal_size = reciprocal_value(size);
8046 order = calculate_order(size);
8047
8048 if ((int)order < 0)
8049 return 0;
8050
8051 s->allocflags = __GFP_COMP;
8052
8053 if (s->flags & SLAB_CACHE_DMA)
8054 s->allocflags |= GFP_DMA;
8055
8056 if (s->flags & SLAB_CACHE_DMA32)
8057 s->allocflags |= GFP_DMA32;
8058
8059 if (s->flags & SLAB_RECLAIM_ACCOUNT)
8060 s->allocflags |= __GFP_RECLAIMABLE;
8061
8062 /*
8063 * For kmalloc caches we enable sheaves later by
8064 * bootstrap_kmalloc_sheaves() to avoid recursion.
8065 */
8066 if (!is_kmalloc_cache(s))
8067 s->sheaf_capacity = calculate_sheaf_capacity(s, args);
8068
8069 /*
8070 * Determine the number of objects per slab
8071 */
8072 s->oo = oo_make(order, size);
8073 s->min = oo_make(get_order(size), size);
8074
8075 return !!oo_objects(s->oo);
8076 }
8077
list_slab_objects(struct kmem_cache * s,struct slab * slab)8078 static void list_slab_objects(struct kmem_cache *s, struct slab *slab)
8079 {
8080 #ifdef CONFIG_SLUB_DEBUG
8081 void *addr = slab_address(slab);
8082 void *p;
8083
8084 if (!slab_add_kunit_errors())
8085 slab_bug(s, "Objects remaining on __kmem_cache_shutdown()");
8086
8087 spin_lock(&object_map_lock);
8088 __fill_map(object_map, s, slab);
8089
8090 for_each_object(p, s, addr, slab->objects) {
8091
8092 if (!test_bit(__obj_to_index(s, addr, p), object_map)) {
8093 if (slab_add_kunit_errors())
8094 continue;
8095 pr_err("Object 0x%p @offset=%tu\n", p, p - addr);
8096 print_tracking(s, p);
8097 }
8098 }
8099 spin_unlock(&object_map_lock);
8100
8101 __slab_err(slab);
8102 #endif
8103 }
8104
8105 /*
8106 * Attempt to free all partial slabs on a node.
8107 * This is called from __kmem_cache_shutdown(). We must take list_lock
8108 * because sysfs file might still access partial list after the shutdowning.
8109 */
free_partial(struct kmem_cache * s,struct kmem_cache_node * n)8110 static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
8111 {
8112 LIST_HEAD(discard);
8113 struct slab *slab, *h;
8114
8115 BUG_ON(irqs_disabled());
8116 spin_lock_irq(&n->list_lock);
8117 list_for_each_entry_safe(slab, h, &n->partial, slab_list) {
8118 if (!slab->inuse) {
8119 remove_partial(n, slab);
8120 list_add(&slab->slab_list, &discard);
8121 } else {
8122 list_slab_objects(s, slab);
8123 }
8124 }
8125 spin_unlock_irq(&n->list_lock);
8126
8127 list_for_each_entry_safe(slab, h, &discard, slab_list)
8128 discard_slab(s, slab);
8129 }
8130
__kmem_cache_empty(struct kmem_cache * s)8131 bool __kmem_cache_empty(struct kmem_cache *s)
8132 {
8133 int node;
8134 struct kmem_cache_node *n;
8135
8136 for_each_kmem_cache_node(s, node, n)
8137 if (n->nr_partial || node_nr_slabs(n))
8138 return false;
8139 return true;
8140 }
8141
8142 /*
8143 * Release all resources used by a slab cache.
8144 */
__kmem_cache_shutdown(struct kmem_cache * s)8145 int __kmem_cache_shutdown(struct kmem_cache *s)
8146 {
8147 int node;
8148 struct kmem_cache_node *n;
8149
8150 flush_all_cpus_locked(s);
8151
8152 /* we might have rcu sheaves in flight */
8153 if (cache_has_sheaves(s))
8154 rcu_barrier();
8155
8156 for_each_node(node) {
8157 struct node_barn *barn = get_barn_node(s, node);
8158
8159 if (barn)
8160 barn_shrink(s, barn);
8161 }
8162
8163 /* Attempt to free all objects */
8164 for_each_kmem_cache_node(s, node, n) {
8165 free_partial(s, n);
8166 if (n->nr_partial || node_nr_slabs(n))
8167 return 1;
8168 }
8169 return 0;
8170 }
8171
8172 #ifdef CONFIG_PRINTK
__kmem_obj_info(struct kmem_obj_info * kpp,void * object,struct slab * slab)8173 void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
8174 {
8175 void *base;
8176 int __maybe_unused i;
8177 unsigned int objnr;
8178 void *objp;
8179 void *objp0;
8180 struct kmem_cache *s = slab->slab_cache;
8181 struct track __maybe_unused *trackp;
8182
8183 kpp->kp_ptr = object;
8184 kpp->kp_slab = slab;
8185 kpp->kp_slab_cache = s;
8186 base = slab_address(slab);
8187 objp0 = kasan_reset_tag(object);
8188 #ifdef CONFIG_SLUB_DEBUG
8189 objp = restore_red_left(s, objp0);
8190 #else
8191 objp = objp0;
8192 #endif
8193 objnr = obj_to_index(s, slab, objp);
8194 kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp);
8195 objp = base + s->size * objnr;
8196 kpp->kp_objp = objp;
8197 if (WARN_ON_ONCE(objp < base || objp >= base + slab->objects * s->size
8198 || (objp - base) % s->size) ||
8199 !(s->flags & SLAB_STORE_USER))
8200 return;
8201 #ifdef CONFIG_SLUB_DEBUG
8202 objp = fixup_red_left(s, objp);
8203 trackp = get_track(s, objp, TRACK_ALLOC);
8204 kpp->kp_ret = (void *)trackp->addr;
8205 #ifdef CONFIG_STACKDEPOT
8206 {
8207 depot_stack_handle_t handle;
8208 unsigned long *entries;
8209 unsigned int nr_entries;
8210
8211 handle = READ_ONCE(trackp->handle);
8212 if (handle) {
8213 nr_entries = stack_depot_fetch(handle, &entries);
8214 for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
8215 kpp->kp_stack[i] = (void *)entries[i];
8216 }
8217
8218 trackp = get_track(s, objp, TRACK_FREE);
8219 handle = READ_ONCE(trackp->handle);
8220 if (handle) {
8221 nr_entries = stack_depot_fetch(handle, &entries);
8222 for (i = 0; i < KS_ADDRS_COUNT && i < nr_entries; i++)
8223 kpp->kp_free_stack[i] = (void *)entries[i];
8224 }
8225 }
8226 #endif
8227 #endif
8228 }
8229 #endif
8230
8231 /********************************************************************
8232 * Kmalloc subsystem
8233 *******************************************************************/
8234
setup_slub_min_order(const char * str,const struct kernel_param * kp)8235 static int __init setup_slub_min_order(const char *str, const struct kernel_param *kp)
8236 {
8237 int ret;
8238
8239 ret = kstrtouint(str, 0, &slub_min_order);
8240 if (ret)
8241 return ret;
8242
8243 if (slub_min_order > slub_max_order)
8244 slub_max_order = slub_min_order;
8245
8246 return 0;
8247 }
8248
8249 static const struct kernel_param_ops param_ops_slab_min_order __initconst = {
8250 .set = setup_slub_min_order,
8251 };
8252 __core_param_cb(slab_min_order, ¶m_ops_slab_min_order, &slub_min_order, 0);
8253 __core_param_cb(slub_min_order, ¶m_ops_slab_min_order, &slub_min_order, 0);
8254
setup_slub_max_order(const char * str,const struct kernel_param * kp)8255 static int __init setup_slub_max_order(const char *str, const struct kernel_param *kp)
8256 {
8257 int ret;
8258
8259 ret = kstrtouint(str, 0, &slub_max_order);
8260 if (ret)
8261 return ret;
8262
8263 slub_max_order = min_t(unsigned int, slub_max_order, MAX_PAGE_ORDER);
8264
8265 if (slub_min_order > slub_max_order)
8266 slub_min_order = slub_max_order;
8267
8268 return 0;
8269 }
8270
8271 static const struct kernel_param_ops param_ops_slab_max_order __initconst = {
8272 .set = setup_slub_max_order,
8273 };
8274 __core_param_cb(slab_max_order, ¶m_ops_slab_max_order, &slub_max_order, 0);
8275 __core_param_cb(slub_max_order, ¶m_ops_slab_max_order, &slub_max_order, 0);
8276
8277 core_param(slab_min_objects, slub_min_objects, uint, 0);
8278 core_param(slub_min_objects, slub_min_objects, uint, 0);
8279
8280 #ifdef CONFIG_NUMA
setup_slab_strict_numa(const char * str,const struct kernel_param * kp)8281 static int __init setup_slab_strict_numa(const char *str, const struct kernel_param *kp)
8282 {
8283 if (nr_node_ids > 1) {
8284 static_branch_enable(&strict_numa);
8285 pr_info("SLUB: Strict NUMA enabled.\n");
8286 } else {
8287 pr_warn("slab_strict_numa parameter set on non NUMA system.\n");
8288 }
8289
8290 return 0;
8291 }
8292
8293 static const struct kernel_param_ops param_ops_slab_strict_numa __initconst = {
8294 .flags = KERNEL_PARAM_OPS_FL_NOARG,
8295 .set = setup_slab_strict_numa,
8296 };
8297 __core_param_cb(slab_strict_numa, ¶m_ops_slab_strict_numa, NULL, 0);
8298 #endif
8299
8300
8301 #ifdef CONFIG_HARDENED_USERCOPY
8302 /*
8303 * Rejects incorrectly sized objects and objects that are to be copied
8304 * to/from userspace but do not fall entirely within the containing slab
8305 * cache's usercopy region.
8306 *
8307 * Returns NULL if check passes, otherwise const char * to name of cache
8308 * to indicate an error.
8309 */
__check_heap_object(const void * ptr,unsigned long n,const struct slab * slab,bool to_user)8310 void __check_heap_object(const void *ptr, unsigned long n,
8311 const struct slab *slab, bool to_user)
8312 {
8313 struct kmem_cache *s;
8314 unsigned int offset;
8315 bool is_kfence = is_kfence_address(ptr);
8316
8317 ptr = kasan_reset_tag(ptr);
8318
8319 /* Find object and usable object size. */
8320 s = slab->slab_cache;
8321
8322 /* Reject impossible pointers. */
8323 if (ptr < slab_address(slab))
8324 usercopy_abort("SLUB object not in SLUB page?!", NULL,
8325 to_user, 0, n);
8326
8327 /* Find offset within object. */
8328 if (is_kfence)
8329 offset = ptr - kfence_object_start(ptr);
8330 else
8331 offset = (ptr - slab_address(slab)) % s->size;
8332
8333 /* Adjust for redzone and reject if within the redzone. */
8334 if (!is_kfence && kmem_cache_debug_flags(s, SLAB_RED_ZONE)) {
8335 if (offset < s->red_left_pad)
8336 usercopy_abort("SLUB object in left red zone",
8337 s->name, to_user, offset, n);
8338 offset -= s->red_left_pad;
8339 }
8340
8341 /* Allow address range falling entirely within usercopy region. */
8342 if (offset >= s->useroffset &&
8343 offset - s->useroffset <= s->usersize &&
8344 n <= s->useroffset - offset + s->usersize)
8345 return;
8346
8347 usercopy_abort("SLUB object", s->name, to_user, offset, n);
8348 }
8349 #endif /* CONFIG_HARDENED_USERCOPY */
8350
8351 #define SHRINK_PROMOTE_MAX 32
8352
8353 /*
8354 * kmem_cache_shrink discards empty slabs and promotes the slabs filled
8355 * up most to the head of the partial lists. New allocations will then
8356 * fill those up and thus they can be removed from the partial lists.
8357 *
8358 * The slabs with the least items are placed last. This results in them
8359 * being allocated from last increasing the chance that the last objects
8360 * are freed in them.
8361 */
__kmem_cache_do_shrink(struct kmem_cache * s)8362 static int __kmem_cache_do_shrink(struct kmem_cache *s)
8363 {
8364 int node;
8365 int i;
8366 struct kmem_cache_node *n;
8367 struct slab *slab;
8368 struct slab *t;
8369 struct list_head discard;
8370 struct list_head promote[SHRINK_PROMOTE_MAX];
8371 unsigned long flags;
8372 int ret = 0;
8373
8374 for_each_node(node) {
8375 struct node_barn *barn = get_barn_node(s, node);
8376
8377 if (barn)
8378 barn_shrink(s, barn);
8379 }
8380
8381 for_each_kmem_cache_node(s, node, n) {
8382 INIT_LIST_HEAD(&discard);
8383 for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
8384 INIT_LIST_HEAD(promote + i);
8385
8386 spin_lock_irqsave(&n->list_lock, flags);
8387
8388 /*
8389 * Build lists of slabs to discard or promote.
8390 *
8391 * Note that concurrent frees may occur while we hold the
8392 * list_lock. slab->inuse here is the upper limit.
8393 */
8394 list_for_each_entry_safe(slab, t, &n->partial, slab_list) {
8395 int free = slab->objects - slab->inuse;
8396
8397 /* Do not reread slab->inuse */
8398 barrier();
8399
8400 /* We do not keep full slabs on the list */
8401 BUG_ON(free <= 0);
8402
8403 if (free == slab->objects) {
8404 list_move(&slab->slab_list, &discard);
8405 clear_node_partial_state(n, slab);
8406 dec_slabs_node(s, node, slab->objects);
8407 } else if (free <= SHRINK_PROMOTE_MAX)
8408 list_move(&slab->slab_list, promote + free - 1);
8409 }
8410
8411 /*
8412 * Promote the slabs filled up most to the head of the
8413 * partial list.
8414 */
8415 for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
8416 list_splice(promote + i, &n->partial);
8417
8418 spin_unlock_irqrestore(&n->list_lock, flags);
8419
8420 /* Release empty slabs */
8421 list_for_each_entry_safe(slab, t, &discard, slab_list)
8422 free_slab(s, slab);
8423
8424 if (node_nr_slabs(n))
8425 ret = 1;
8426 }
8427
8428 return ret;
8429 }
8430
__kmem_cache_shrink(struct kmem_cache * s)8431 int __kmem_cache_shrink(struct kmem_cache *s)
8432 {
8433 flush_all(s);
8434 return __kmem_cache_do_shrink(s);
8435 }
8436
slab_mem_going_offline_callback(void)8437 static int slab_mem_going_offline_callback(void)
8438 {
8439 struct kmem_cache *s;
8440
8441 mutex_lock(&slab_mutex);
8442 list_for_each_entry(s, &slab_caches, list) {
8443 flush_all_cpus_locked(s);
8444 __kmem_cache_do_shrink(s);
8445 }
8446 mutex_unlock(&slab_mutex);
8447
8448 return 0;
8449 }
8450
slab_mem_going_online_callback(int nid)8451 static int slab_mem_going_online_callback(int nid)
8452 {
8453 struct kmem_cache_node *n;
8454 struct kmem_cache *s;
8455 int ret = 0;
8456
8457 /*
8458 * We are bringing a node online. No memory is available yet. We must
8459 * allocate a kmem_cache_node structure in order to bring the node
8460 * online.
8461 */
8462 mutex_lock(&slab_mutex);
8463 list_for_each_entry(s, &slab_caches, list) {
8464 struct node_barn *barn = NULL;
8465
8466 /*
8467 * The structure may already exist if the node was previously
8468 * onlined and offlined.
8469 */
8470 if (get_node(s, nid))
8471 continue;
8472
8473 if (cache_has_sheaves(s) && !get_barn_node(s, nid)) {
8474
8475 barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, nid);
8476
8477 if (!barn) {
8478 ret = -ENOMEM;
8479 goto out;
8480 }
8481 }
8482
8483 /*
8484 * XXX: kmem_cache_alloc_node will fallback to other nodes
8485 * since memory is not yet available from the node that
8486 * is brought up.
8487 */
8488 n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
8489 if (!n) {
8490 kfree(barn);
8491 ret = -ENOMEM;
8492 goto out;
8493 }
8494
8495 init_kmem_cache_node(n);
8496 s->per_node[nid].node = n;
8497
8498 if (barn) {
8499 barn_init(barn);
8500 s->per_node[nid].barn = barn;
8501 }
8502 }
8503 /*
8504 * Any cache created after this point will also have kmem_cache_node
8505 * and barn initialized for the new node.
8506 */
8507 node_set(nid, slab_nodes);
8508 node_set(nid, slab_barn_nodes);
8509 out:
8510 mutex_unlock(&slab_mutex);
8511 return ret;
8512 }
8513
slab_memory_callback(struct notifier_block * self,unsigned long action,void * arg)8514 static int slab_memory_callback(struct notifier_block *self,
8515 unsigned long action, void *arg)
8516 {
8517 struct node_notify *nn = arg;
8518 int nid = nn->nid;
8519 int ret = 0;
8520
8521 switch (action) {
8522 case NODE_ADDING_FIRST_MEMORY:
8523 ret = slab_mem_going_online_callback(nid);
8524 break;
8525 case NODE_REMOVING_LAST_MEMORY:
8526 ret = slab_mem_going_offline_callback();
8527 break;
8528 }
8529 if (ret)
8530 ret = notifier_from_errno(ret);
8531 else
8532 ret = NOTIFY_OK;
8533 return ret;
8534 }
8535
8536 /********************************************************************
8537 * Basic setup of slabs
8538 *******************************************************************/
8539
8540 /*
8541 * Used for early kmem_cache structures that were allocated using
8542 * the page allocator. Allocate them properly then fix up the pointers
8543 * that may be pointing to the wrong kmem_cache structure.
8544 */
8545
bootstrap(struct kmem_cache * static_cache)8546 static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
8547 {
8548 int node;
8549 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
8550 struct kmem_cache_node *n;
8551
8552 memcpy(s, static_cache, kmem_cache->object_size);
8553
8554 for_each_kmem_cache_node(s, node, n) {
8555 struct slab *p;
8556
8557 list_for_each_entry(p, &n->partial, slab_list)
8558 p->slab_cache = s;
8559
8560 #ifdef CONFIG_SLUB_DEBUG
8561 list_for_each_entry(p, &n->full, slab_list)
8562 p->slab_cache = s;
8563 #endif
8564 }
8565 list_add(&s->list, &slab_caches);
8566 return s;
8567 }
8568
8569 /*
8570 * Finish the sheaves initialization done normally by init_percpu_sheaves() and
8571 * init_kmem_cache_nodes(). For normal kmalloc caches we have to bootstrap it
8572 * since sheaves and barns are allocated by kmalloc.
8573 */
bootstrap_cache_sheaves(struct kmem_cache * s)8574 static void __init bootstrap_cache_sheaves(struct kmem_cache *s)
8575 {
8576 struct kmem_cache_args empty_args = {};
8577 unsigned int capacity;
8578 bool failed = false;
8579 int node, cpu;
8580
8581 VM_WARN_ON_ONCE(cache_has_sheaves(s));
8582
8583 capacity = calculate_sheaf_capacity(s, &empty_args);
8584
8585 /* capacity can be 0 due to debugging or SLUB_TINY */
8586 if (!capacity)
8587 return;
8588
8589 for_each_node_mask(node, slab_barn_nodes) {
8590 struct node_barn *barn;
8591
8592 barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, node);
8593
8594 if (!barn) {
8595 failed = true;
8596 goto out;
8597 }
8598
8599 barn_init(barn);
8600 s->per_node[node].barn = barn;
8601 }
8602
8603 for_each_possible_cpu(cpu) {
8604 struct slub_percpu_sheaves *pcs;
8605
8606 pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
8607
8608 pcs->main = __alloc_empty_sheaf(s, GFP_KERNEL,
8609 SLAB_ALLOC_DEFAULT, capacity);
8610
8611 if (!pcs->main) {
8612 failed = true;
8613 break;
8614 }
8615 }
8616
8617 out:
8618 /*
8619 * It's still early in boot so treat this like same as a failure to
8620 * create the kmalloc cache in the first place
8621 */
8622 if (failed)
8623 panic("Out of memory when creating kmem_cache %s\n", s->name);
8624
8625 s->sheaf_capacity = capacity;
8626 }
8627
bootstrap_kmalloc_sheaves(void)8628 static void __init bootstrap_kmalloc_sheaves(void)
8629 {
8630 enum kmalloc_cache_type type;
8631
8632 for (type = KMALLOC_NORMAL; type < NR_KMALLOC_TYPES; type++) {
8633 for (int idx = 0; idx < KMALLOC_SHIFT_HIGH + 1; idx++) {
8634 struct kmem_cache *s = kmalloc_caches[type][idx];
8635
8636 /* Do not bootstrap twice when caches are aliased */
8637 if (s && !cache_has_sheaves(s))
8638 bootstrap_cache_sheaves(s);
8639 }
8640 }
8641 }
8642
kmem_cache_init(void)8643 void __init kmem_cache_init(void)
8644 {
8645 static __initdata struct kmem_cache boot_kmem_cache,
8646 boot_kmem_cache_node;
8647 int node;
8648
8649 slab_obj_ext_has_codetag_init();
8650
8651 if (debug_guardpage_minorder())
8652 slub_max_order = 0;
8653
8654 /* Inform pointer hashing choice about slub debugging state. */
8655 hash_pointers_finalize(__slub_debug_enabled());
8656
8657 kmem_cache_node = &boot_kmem_cache_node;
8658 kmem_cache = &boot_kmem_cache;
8659
8660 /*
8661 * Initialize the nodemask for which we will allocate per node
8662 * structures. Here we don't need taking slab_mutex yet.
8663 */
8664 for_each_node_state(node, N_MEMORY)
8665 node_set(node, slab_nodes);
8666
8667 for_each_online_node(node)
8668 node_set(node, slab_barn_nodes);
8669
8670 create_boot_cache(kmem_cache_node, "kmem_cache_node",
8671 sizeof(struct kmem_cache_node),
8672 SLAB_HWCACHE_ALIGN | SLAB_NO_SHEAVES | SLAB_NO_OBJ_EXT,
8673 0, 0);
8674
8675 hotplug_node_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
8676
8677 /* Able to allocate the per node structures */
8678 slab_state = PARTIAL;
8679
8680 create_boot_cache(kmem_cache, "kmem_cache",
8681 offsetof(struct kmem_cache, per_node) +
8682 nr_node_ids * sizeof(struct kmem_cache_per_node_ptrs),
8683 SLAB_HWCACHE_ALIGN | SLAB_NO_SHEAVES | SLAB_NO_OBJ_EXT,
8684 0, 0);
8685
8686 kmem_cache = bootstrap(&boot_kmem_cache);
8687 kmem_cache_node = bootstrap(&boot_kmem_cache_node);
8688
8689 /* Now we can use the kmem_cache to allocate kmalloc slabs */
8690 setup_kmalloc_cache_index_table();
8691 create_kmalloc_caches();
8692
8693 bootstrap_kmalloc_sheaves();
8694
8695 /* Setup random freelists for each cache */
8696 init_freelist_randomization();
8697
8698 cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", slub_cpu_setup,
8699 slub_cpu_dead);
8700
8701 pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%u\n",
8702 cache_line_size(),
8703 slub_min_order, slub_max_order, slub_min_objects,
8704 nr_cpu_ids, nr_node_ids);
8705 }
8706
kmem_cache_init_late(void)8707 void __init kmem_cache_init_late(void)
8708 {
8709 flushwq = alloc_workqueue("slub_flushwq", WQ_MEM_RECLAIM | WQ_PERCPU,
8710 0);
8711 WARN_ON(!flushwq);
8712 #ifdef CONFIG_SLAB_FREELIST_RANDOM
8713 prandom_init_once(&slab_rnd_state);
8714 #endif
8715 }
8716
do_kmem_cache_create(struct kmem_cache * s,const char * name,unsigned int size,struct kmem_cache_args * args,slab_flags_t flags)8717 int do_kmem_cache_create(struct kmem_cache *s, const char *name,
8718 unsigned int size, struct kmem_cache_args *args,
8719 slab_flags_t flags)
8720 {
8721 int err = -EINVAL;
8722
8723 s->name = name;
8724 s->size = s->object_size = size;
8725
8726 s->flags = kmem_cache_flags(flags, s->name);
8727 #ifdef CONFIG_SLAB_FREELIST_HARDENED
8728 s->random = get_random_long();
8729 #endif
8730 s->align = args->align;
8731 s->ctor = args->ctor;
8732 #ifdef CONFIG_HARDENED_USERCOPY
8733 s->useroffset = args->useroffset;
8734 s->usersize = args->usersize;
8735 #endif
8736
8737 if (!calculate_sizes(args, s))
8738 goto out;
8739 if (disable_higher_order_debug) {
8740 /*
8741 * Disable debugging flags that store metadata if the min slab
8742 * order increased.
8743 */
8744 if (get_order(s->size) > get_order(s->object_size)) {
8745 s->flags &= ~DEBUG_METADATA_FLAGS;
8746 s->offset = 0;
8747 if (!calculate_sizes(args, s))
8748 goto out;
8749 }
8750 }
8751
8752 #ifdef system_has_freelist_aba
8753 if (system_has_freelist_aba() && !(s->flags & SLAB_NO_CMPXCHG)) {
8754 /* Enable fast mode */
8755 s->flags |= __CMPXCHG_DOUBLE;
8756 }
8757 #endif
8758
8759 /*
8760 * The larger the object size is, the more slabs we want on the partial
8761 * list to avoid pounding the page allocator excessively.
8762 */
8763 s->min_partial = min_t(unsigned long, MAX_PARTIAL, ilog2(s->size) / 2);
8764 s->min_partial = max_t(unsigned long, MIN_PARTIAL, s->min_partial);
8765
8766 s->cpu_sheaves = alloc_percpu(struct slub_percpu_sheaves);
8767 if (!s->cpu_sheaves) {
8768 err = -ENOMEM;
8769 goto out;
8770 }
8771
8772 #ifdef CONFIG_NUMA
8773 s->remote_node_defrag_ratio = 1000;
8774 #endif
8775
8776 /* Initialize the pre-computed randomized freelist if slab is up */
8777 if (slab_state >= UP) {
8778 if (init_cache_random_seq(s))
8779 goto out;
8780 }
8781
8782 if (!init_kmem_cache_nodes(s))
8783 goto out;
8784
8785 #ifdef CONFIG_SLUB_STATS
8786 if (!alloc_kmem_cache_stats(s))
8787 goto out;
8788 #endif
8789
8790 err = init_percpu_sheaves(s);
8791 if (err)
8792 goto out;
8793
8794 err = 0;
8795
8796 /* Mutex is not taken during early boot */
8797 if (slab_state <= UP)
8798 goto out;
8799
8800 /*
8801 * Failing to create sysfs files is not critical to SLUB functionality.
8802 * If it fails, proceed with cache creation without these files.
8803 */
8804 if (sysfs_slab_add(s))
8805 pr_err("SLUB: Unable to add cache %s to sysfs\n", s->name);
8806
8807 if (s->flags & SLAB_STORE_USER)
8808 debugfs_slab_add(s);
8809
8810 out:
8811 if (err)
8812 __kmem_cache_release(s);
8813 return err;
8814 }
8815
8816 #ifdef SLAB_SUPPORTS_SYSFS
count_inuse(struct slab * slab)8817 static int count_inuse(struct slab *slab)
8818 {
8819 return slab->inuse;
8820 }
8821
count_total(struct slab * slab)8822 static int count_total(struct slab *slab)
8823 {
8824 return slab->objects;
8825 }
8826 #endif
8827
8828 #ifdef CONFIG_SLUB_DEBUG
validate_slab(struct kmem_cache * s,struct slab * slab,unsigned long * obj_map)8829 static void validate_slab(struct kmem_cache *s, struct slab *slab,
8830 unsigned long *obj_map)
8831 {
8832 void *p;
8833 void *addr = slab_address(slab);
8834
8835 if (!validate_slab_ptr(slab)) {
8836 slab_err(s, slab, "Not a valid slab page");
8837 return;
8838 }
8839
8840 if (!check_slab(s, slab) || !on_freelist(s, slab, NULL))
8841 return;
8842
8843 /* Now we know that a valid freelist exists */
8844 __fill_map(obj_map, s, slab);
8845 for_each_object(p, s, addr, slab->objects) {
8846 u8 val = test_bit(__obj_to_index(s, addr, p), obj_map) ?
8847 SLUB_RED_INACTIVE : SLUB_RED_ACTIVE;
8848
8849 if (!check_object(s, slab, p, val))
8850 break;
8851 }
8852 }
8853
validate_slab_node(struct kmem_cache * s,struct kmem_cache_node * n,unsigned long * obj_map)8854 static int validate_slab_node(struct kmem_cache *s,
8855 struct kmem_cache_node *n, unsigned long *obj_map)
8856 {
8857 unsigned long count = 0;
8858 struct slab *slab;
8859 unsigned long flags;
8860
8861 spin_lock_irqsave(&n->list_lock, flags);
8862
8863 list_for_each_entry(slab, &n->partial, slab_list) {
8864 validate_slab(s, slab, obj_map);
8865 count++;
8866 }
8867 if (count != n->nr_partial) {
8868 pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
8869 s->name, count, n->nr_partial);
8870 slab_add_kunit_errors();
8871 }
8872
8873 if (!(s->flags & SLAB_STORE_USER))
8874 goto out;
8875
8876 list_for_each_entry(slab, &n->full, slab_list) {
8877 validate_slab(s, slab, obj_map);
8878 count++;
8879 }
8880 if (count != node_nr_slabs(n)) {
8881 pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
8882 s->name, count, node_nr_slabs(n));
8883 slab_add_kunit_errors();
8884 }
8885
8886 out:
8887 spin_unlock_irqrestore(&n->list_lock, flags);
8888 return count;
8889 }
8890
validate_slab_cache(struct kmem_cache * s)8891 long validate_slab_cache(struct kmem_cache *s)
8892 {
8893 int node;
8894 unsigned long count = 0;
8895 struct kmem_cache_node *n;
8896 unsigned long *obj_map;
8897
8898 obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
8899 if (!obj_map)
8900 return -ENOMEM;
8901
8902 flush_all(s);
8903 for_each_kmem_cache_node(s, node, n)
8904 count += validate_slab_node(s, n, obj_map);
8905
8906 bitmap_free(obj_map);
8907
8908 return count;
8909 }
8910 EXPORT_SYMBOL(validate_slab_cache);
8911
8912 #ifdef CONFIG_DEBUG_FS
8913 /*
8914 * Generate lists of code addresses where slabcache objects are allocated
8915 * and freed.
8916 */
8917
8918 struct location {
8919 depot_stack_handle_t handle;
8920 unsigned long count;
8921 unsigned long addr;
8922 unsigned long waste;
8923 long long sum_time;
8924 long min_time;
8925 long max_time;
8926 long min_pid;
8927 long max_pid;
8928 DECLARE_BITMAP(cpus, NR_CPUS);
8929 nodemask_t nodes;
8930 };
8931
8932 struct loc_track {
8933 unsigned long max;
8934 unsigned long count;
8935 struct location *loc;
8936 loff_t idx;
8937 };
8938
8939 static struct dentry *slab_debugfs_root;
8940
free_loc_track(struct loc_track * t)8941 static void free_loc_track(struct loc_track *t)
8942 {
8943 if (t->max)
8944 free_pages((unsigned long)t->loc,
8945 get_order(sizeof(struct location) * t->max));
8946 }
8947
alloc_loc_track(struct loc_track * t,unsigned long max,gfp_t flags)8948 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
8949 {
8950 struct location *l;
8951 int order;
8952
8953 order = get_order(sizeof(struct location) * max);
8954
8955 l = (void *)__get_free_pages(flags, order);
8956 if (!l)
8957 return 0;
8958
8959 if (t->count) {
8960 memcpy(l, t->loc, sizeof(struct location) * t->count);
8961 free_loc_track(t);
8962 }
8963 t->max = max;
8964 t->loc = l;
8965 return 1;
8966 }
8967
add_location(struct loc_track * t,struct kmem_cache * s,const struct track * track,unsigned int orig_size)8968 static int add_location(struct loc_track *t, struct kmem_cache *s,
8969 const struct track *track,
8970 unsigned int orig_size)
8971 {
8972 long start, end, pos;
8973 struct location *l;
8974 unsigned long caddr, chandle, cwaste;
8975 unsigned long age = jiffies - track->when;
8976 depot_stack_handle_t handle = 0;
8977 unsigned int waste = s->object_size - orig_size;
8978
8979 #ifdef CONFIG_STACKDEPOT
8980 handle = READ_ONCE(track->handle);
8981 #endif
8982 start = -1;
8983 end = t->count;
8984
8985 for ( ; ; ) {
8986 pos = start + (end - start + 1) / 2;
8987
8988 /*
8989 * There is nothing at "end". If we end up there
8990 * we need to add something to before end.
8991 */
8992 if (pos == end)
8993 break;
8994
8995 l = &t->loc[pos];
8996 caddr = l->addr;
8997 chandle = l->handle;
8998 cwaste = l->waste;
8999 if ((track->addr == caddr) && (handle == chandle) &&
9000 (waste == cwaste)) {
9001
9002 l->count++;
9003 if (track->when) {
9004 l->sum_time += age;
9005 if (age < l->min_time)
9006 l->min_time = age;
9007 if (age > l->max_time)
9008 l->max_time = age;
9009
9010 if (track->pid < l->min_pid)
9011 l->min_pid = track->pid;
9012 if (track->pid > l->max_pid)
9013 l->max_pid = track->pid;
9014
9015 cpumask_set_cpu(track->cpu,
9016 to_cpumask(l->cpus));
9017 }
9018 node_set(page_to_nid(virt_to_page(track)), l->nodes);
9019 return 1;
9020 }
9021
9022 if (track->addr < caddr)
9023 end = pos;
9024 else if (track->addr == caddr && handle < chandle)
9025 end = pos;
9026 else if (track->addr == caddr && handle == chandle &&
9027 waste < cwaste)
9028 end = pos;
9029 else
9030 start = pos;
9031 }
9032
9033 /*
9034 * Not found. Insert new tracking element.
9035 */
9036 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
9037 return 0;
9038
9039 l = t->loc + pos;
9040 if (pos < t->count)
9041 memmove(l + 1, l,
9042 (t->count - pos) * sizeof(struct location));
9043 t->count++;
9044 l->count = 1;
9045 l->addr = track->addr;
9046 l->sum_time = age;
9047 l->min_time = age;
9048 l->max_time = age;
9049 l->min_pid = track->pid;
9050 l->max_pid = track->pid;
9051 l->handle = handle;
9052 l->waste = waste;
9053 cpumask_clear(to_cpumask(l->cpus));
9054 cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
9055 nodes_clear(l->nodes);
9056 node_set(page_to_nid(virt_to_page(track)), l->nodes);
9057 return 1;
9058 }
9059
process_slab(struct loc_track * t,struct kmem_cache * s,struct slab * slab,enum track_item alloc,unsigned long * obj_map)9060 static void process_slab(struct loc_track *t, struct kmem_cache *s,
9061 struct slab *slab, enum track_item alloc,
9062 unsigned long *obj_map)
9063 {
9064 void *addr = slab_address(slab);
9065 bool is_alloc = (alloc == TRACK_ALLOC);
9066 void *p;
9067
9068 __fill_map(obj_map, s, slab);
9069
9070 for_each_object(p, s, addr, slab->objects)
9071 if (!test_bit(__obj_to_index(s, addr, p), obj_map))
9072 add_location(t, s, get_track(s, p, alloc),
9073 is_alloc ? get_orig_size(s, p) :
9074 s->object_size);
9075 }
9076 #endif /* CONFIG_DEBUG_FS */
9077 #endif /* CONFIG_SLUB_DEBUG */
9078
9079 #ifdef SLAB_SUPPORTS_SYSFS
9080 enum slab_stat_type {
9081 SL_ALL, /* All slabs */
9082 SL_PARTIAL, /* Only partially allocated slabs */
9083 SL_OBJECTS, /* Determine allocated objects not slabs */
9084 SL_TOTAL /* Determine object capacity not slabs */
9085 };
9086
9087 #define SO_ALL (1 << SL_ALL)
9088 #define SO_PARTIAL (1 << SL_PARTIAL)
9089 #define SO_OBJECTS (1 << SL_OBJECTS)
9090 #define SO_TOTAL (1 << SL_TOTAL)
9091
show_slab_objects(struct kmem_cache * s,char * buf,unsigned long flags)9092 static ssize_t show_slab_objects(struct kmem_cache *s,
9093 char *buf, unsigned long flags)
9094 {
9095 unsigned long total = 0;
9096 int node;
9097 int x;
9098 unsigned long *nodes;
9099 int len = 0;
9100
9101 nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL);
9102 if (!nodes)
9103 return -ENOMEM;
9104
9105 /*
9106 * It is impossible to take "mem_hotplug_lock" here with "kernfs_mutex"
9107 * already held which will conflict with an existing lock order:
9108 *
9109 * mem_hotplug_lock->slab_mutex->kernfs_mutex
9110 *
9111 * We don't really need mem_hotplug_lock (to hold off
9112 * slab_mem_going_offline_callback) here because slab's memory hot
9113 * unplug code doesn't destroy the kmem_cache->node[] data.
9114 */
9115
9116 #ifdef CONFIG_SLUB_DEBUG
9117 if (flags & SO_ALL) {
9118 struct kmem_cache_node *n;
9119
9120 for_each_kmem_cache_node(s, node, n) {
9121
9122 if (flags & SO_TOTAL)
9123 x = node_nr_objs(n);
9124 else if (flags & SO_OBJECTS)
9125 x = node_nr_objs(n) - count_partial(n, count_free);
9126 else
9127 x = node_nr_slabs(n);
9128 total += x;
9129 nodes[node] += x;
9130 }
9131
9132 } else
9133 #endif
9134 if (flags & SO_PARTIAL) {
9135 struct kmem_cache_node *n;
9136
9137 for_each_kmem_cache_node(s, node, n) {
9138 if (flags & SO_TOTAL)
9139 x = count_partial(n, count_total);
9140 else if (flags & SO_OBJECTS)
9141 x = count_partial(n, count_inuse);
9142 else
9143 x = n->nr_partial;
9144 total += x;
9145 nodes[node] += x;
9146 }
9147 }
9148
9149 len += sysfs_emit_at(buf, len, "%lu", total);
9150 #ifdef CONFIG_NUMA
9151 for (node = 0; node < nr_node_ids; node++) {
9152 if (nodes[node])
9153 len += sysfs_emit_at(buf, len, " N%d=%lu",
9154 node, nodes[node]);
9155 }
9156 #endif
9157 len += sysfs_emit_at(buf, len, "\n");
9158 kfree(nodes);
9159
9160 return len;
9161 }
9162
9163 #define to_slab_attr(n) container_of_const(n, struct slab_attribute, attr)
9164 #define to_slab(n) container_of(n, struct kmem_cache, kobj)
9165
9166 struct slab_attribute {
9167 struct attribute attr;
9168 ssize_t (*show)(struct kmem_cache *s, char *buf);
9169 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
9170 };
9171
9172 #define SLAB_ATTR_RO(_name) \
9173 static const struct slab_attribute _name##_attr = __ATTR_RO_MODE(_name, 0400)
9174
9175 #define SLAB_ATTR(_name) \
9176 static const struct slab_attribute _name##_attr = __ATTR_RW_MODE(_name, 0600)
9177
slab_size_show(struct kmem_cache * s,char * buf)9178 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
9179 {
9180 return sysfs_emit(buf, "%u\n", s->size);
9181 }
9182 SLAB_ATTR_RO(slab_size);
9183
align_show(struct kmem_cache * s,char * buf)9184 static ssize_t align_show(struct kmem_cache *s, char *buf)
9185 {
9186 return sysfs_emit(buf, "%u\n", s->align);
9187 }
9188 SLAB_ATTR_RO(align);
9189
object_size_show(struct kmem_cache * s,char * buf)9190 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
9191 {
9192 return sysfs_emit(buf, "%u\n", s->object_size);
9193 }
9194 SLAB_ATTR_RO(object_size);
9195
objs_per_slab_show(struct kmem_cache * s,char * buf)9196 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
9197 {
9198 return sysfs_emit(buf, "%u\n", oo_objects(s->oo));
9199 }
9200 SLAB_ATTR_RO(objs_per_slab);
9201
order_show(struct kmem_cache * s,char * buf)9202 static ssize_t order_show(struct kmem_cache *s, char *buf)
9203 {
9204 return sysfs_emit(buf, "%u\n", oo_order(s->oo));
9205 }
9206 SLAB_ATTR_RO(order);
9207
sheaf_capacity_show(struct kmem_cache * s,char * buf)9208 static ssize_t sheaf_capacity_show(struct kmem_cache *s, char *buf)
9209 {
9210 return sysfs_emit(buf, "%u\n", s->sheaf_capacity);
9211 }
9212 SLAB_ATTR_RO(sheaf_capacity);
9213
min_partial_show(struct kmem_cache * s,char * buf)9214 static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
9215 {
9216 return sysfs_emit(buf, "%lu\n", s->min_partial);
9217 }
9218
min_partial_store(struct kmem_cache * s,const char * buf,size_t length)9219 static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
9220 size_t length)
9221 {
9222 unsigned long min;
9223 int err;
9224
9225 err = kstrtoul(buf, 10, &min);
9226 if (err)
9227 return err;
9228
9229 s->min_partial = min;
9230 return length;
9231 }
9232 SLAB_ATTR(min_partial);
9233
cpu_partial_show(struct kmem_cache * s,char * buf)9234 static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
9235 {
9236 return sysfs_emit(buf, "0\n");
9237 }
9238
cpu_partial_store(struct kmem_cache * s,const char * buf,size_t length)9239 static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
9240 size_t length)
9241 {
9242 unsigned int objects;
9243 int err;
9244
9245 err = kstrtouint(buf, 10, &objects);
9246 if (err)
9247 return err;
9248 if (objects)
9249 return -EINVAL;
9250
9251 return length;
9252 }
9253 SLAB_ATTR(cpu_partial);
9254
ctor_show(struct kmem_cache * s,char * buf)9255 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
9256 {
9257 if (!s->ctor)
9258 return 0;
9259 return sysfs_emit(buf, "%pS\n", s->ctor);
9260 }
9261 SLAB_ATTR_RO(ctor);
9262
aliases_show(struct kmem_cache * s,char * buf)9263 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
9264 {
9265 return sysfs_emit(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
9266 }
9267 SLAB_ATTR_RO(aliases);
9268
partial_show(struct kmem_cache * s,char * buf)9269 static ssize_t partial_show(struct kmem_cache *s, char *buf)
9270 {
9271 return show_slab_objects(s, buf, SO_PARTIAL);
9272 }
9273 SLAB_ATTR_RO(partial);
9274
cpu_slabs_show(struct kmem_cache * s,char * buf)9275 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
9276 {
9277 return sysfs_emit(buf, "0\n");
9278 }
9279 SLAB_ATTR_RO(cpu_slabs);
9280
objects_partial_show(struct kmem_cache * s,char * buf)9281 static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
9282 {
9283 return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
9284 }
9285 SLAB_ATTR_RO(objects_partial);
9286
slabs_cpu_partial_show(struct kmem_cache * s,char * buf)9287 static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
9288 {
9289 return sysfs_emit(buf, "0(0)\n");
9290 }
9291 SLAB_ATTR_RO(slabs_cpu_partial);
9292
reclaim_account_show(struct kmem_cache * s,char * buf)9293 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
9294 {
9295 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
9296 }
9297 SLAB_ATTR_RO(reclaim_account);
9298
hwcache_align_show(struct kmem_cache * s,char * buf)9299 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
9300 {
9301 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
9302 }
9303 SLAB_ATTR_RO(hwcache_align);
9304
9305 #ifdef CONFIG_ZONE_DMA
cache_dma_show(struct kmem_cache * s,char * buf)9306 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
9307 {
9308 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
9309 }
9310 SLAB_ATTR_RO(cache_dma);
9311 #endif
9312
9313 #ifdef CONFIG_HARDENED_USERCOPY
usersize_show(struct kmem_cache * s,char * buf)9314 static ssize_t usersize_show(struct kmem_cache *s, char *buf)
9315 {
9316 return sysfs_emit(buf, "%u\n", s->usersize);
9317 }
9318 SLAB_ATTR_RO(usersize);
9319 #endif
9320
destroy_by_rcu_show(struct kmem_cache * s,char * buf)9321 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
9322 {
9323 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU));
9324 }
9325 SLAB_ATTR_RO(destroy_by_rcu);
9326
9327 #ifdef CONFIG_SLUB_DEBUG
slabs_show(struct kmem_cache * s,char * buf)9328 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
9329 {
9330 return show_slab_objects(s, buf, SO_ALL);
9331 }
9332 SLAB_ATTR_RO(slabs);
9333
total_objects_show(struct kmem_cache * s,char * buf)9334 static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
9335 {
9336 return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
9337 }
9338 SLAB_ATTR_RO(total_objects);
9339
objects_show(struct kmem_cache * s,char * buf)9340 static ssize_t objects_show(struct kmem_cache *s, char *buf)
9341 {
9342 return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
9343 }
9344 SLAB_ATTR_RO(objects);
9345
sanity_checks_show(struct kmem_cache * s,char * buf)9346 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
9347 {
9348 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS));
9349 }
9350 SLAB_ATTR_RO(sanity_checks);
9351
trace_show(struct kmem_cache * s,char * buf)9352 static ssize_t trace_show(struct kmem_cache *s, char *buf)
9353 {
9354 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_TRACE));
9355 }
9356 SLAB_ATTR_RO(trace);
9357
red_zone_show(struct kmem_cache * s,char * buf)9358 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
9359 {
9360 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
9361 }
9362
9363 SLAB_ATTR_RO(red_zone);
9364
poison_show(struct kmem_cache * s,char * buf)9365 static ssize_t poison_show(struct kmem_cache *s, char *buf)
9366 {
9367 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_POISON));
9368 }
9369
9370 SLAB_ATTR_RO(poison);
9371
store_user_show(struct kmem_cache * s,char * buf)9372 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
9373 {
9374 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
9375 }
9376
9377 SLAB_ATTR_RO(store_user);
9378
validate_show(struct kmem_cache * s,char * buf)9379 static ssize_t validate_show(struct kmem_cache *s, char *buf)
9380 {
9381 return 0;
9382 }
9383
validate_store(struct kmem_cache * s,const char * buf,size_t length)9384 static ssize_t validate_store(struct kmem_cache *s,
9385 const char *buf, size_t length)
9386 {
9387 int ret = -EINVAL;
9388
9389 if (buf[0] == '1' && kmem_cache_debug(s)) {
9390 ret = validate_slab_cache(s);
9391 if (ret >= 0)
9392 ret = length;
9393 }
9394 return ret;
9395 }
9396 SLAB_ATTR(validate);
9397
9398 #endif /* CONFIG_SLUB_DEBUG */
9399
9400 #ifdef CONFIG_FAILSLAB
failslab_show(struct kmem_cache * s,char * buf)9401 static ssize_t failslab_show(struct kmem_cache *s, char *buf)
9402 {
9403 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
9404 }
9405
failslab_store(struct kmem_cache * s,const char * buf,size_t length)9406 static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
9407 size_t length)
9408 {
9409 if (s->refcount > 1)
9410 return -EINVAL;
9411
9412 if (buf[0] == '1')
9413 WRITE_ONCE(s->flags, s->flags | SLAB_FAILSLAB);
9414 else
9415 WRITE_ONCE(s->flags, s->flags & ~SLAB_FAILSLAB);
9416
9417 return length;
9418 }
9419 SLAB_ATTR(failslab);
9420 #endif
9421
shrink_show(struct kmem_cache * s,char * buf)9422 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
9423 {
9424 return 0;
9425 }
9426
shrink_store(struct kmem_cache * s,const char * buf,size_t length)9427 static ssize_t shrink_store(struct kmem_cache *s,
9428 const char *buf, size_t length)
9429 {
9430 if (buf[0] == '1')
9431 kmem_cache_shrink(s);
9432 else
9433 return -EINVAL;
9434 return length;
9435 }
9436 SLAB_ATTR(shrink);
9437
9438 #ifdef CONFIG_NUMA
remote_node_defrag_ratio_show(struct kmem_cache * s,char * buf)9439 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
9440 {
9441 return sysfs_emit(buf, "%u\n", s->remote_node_defrag_ratio / 10);
9442 }
9443
remote_node_defrag_ratio_store(struct kmem_cache * s,const char * buf,size_t length)9444 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
9445 const char *buf, size_t length)
9446 {
9447 unsigned int ratio;
9448 int err;
9449
9450 err = kstrtouint(buf, 10, &ratio);
9451 if (err)
9452 return err;
9453 if (ratio > 100)
9454 return -ERANGE;
9455
9456 s->remote_node_defrag_ratio = ratio * 10;
9457
9458 return length;
9459 }
9460 SLAB_ATTR(remote_node_defrag_ratio);
9461 #endif
9462
9463 #ifdef CONFIG_SLUB_STATS
show_stat(struct kmem_cache * s,char * buf,enum stat_item si)9464 static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
9465 {
9466 unsigned long sum = 0;
9467 int cpu;
9468 int len = 0;
9469 int *data = kmalloc_objs(int, nr_cpu_ids);
9470
9471 if (!data)
9472 return -ENOMEM;
9473
9474 for_each_online_cpu(cpu) {
9475 unsigned int x = per_cpu_ptr(s->cpu_stats, cpu)->stat[si];
9476
9477 data[cpu] = x;
9478 sum += x;
9479 }
9480
9481 len += sysfs_emit_at(buf, len, "%lu", sum);
9482
9483 #ifdef CONFIG_SMP
9484 for_each_online_cpu(cpu) {
9485 if (data[cpu])
9486 len += sysfs_emit_at(buf, len, " C%d=%u",
9487 cpu, data[cpu]);
9488 }
9489 #endif
9490 kfree(data);
9491 len += sysfs_emit_at(buf, len, "\n");
9492
9493 return len;
9494 }
9495
clear_stat(struct kmem_cache * s,enum stat_item si)9496 static void clear_stat(struct kmem_cache *s, enum stat_item si)
9497 {
9498 int cpu;
9499
9500 for_each_online_cpu(cpu)
9501 per_cpu_ptr(s->cpu_stats, cpu)->stat[si] = 0;
9502 }
9503
9504 #define STAT_ATTR(si, text) \
9505 static ssize_t text##_show(struct kmem_cache *s, char *buf) \
9506 { \
9507 return show_stat(s, buf, si); \
9508 } \
9509 static ssize_t text##_store(struct kmem_cache *s, \
9510 const char *buf, size_t length) \
9511 { \
9512 if (buf[0] != '0') \
9513 return -EINVAL; \
9514 clear_stat(s, si); \
9515 return length; \
9516 } \
9517 SLAB_ATTR(text); \
9518
9519 STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
9520 STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
9521 STAT_ATTR(FREE_RCU_SHEAF, free_rcu_sheaf);
9522 STAT_ATTR(FREE_RCU_SHEAF_FAIL, free_rcu_sheaf_fail);
9523 STAT_ATTR(FREE_FASTPATH, free_fastpath);
9524 STAT_ATTR(FREE_SLOWPATH, free_slowpath);
9525 STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
9526 STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
9527 STAT_ATTR(ALLOC_SLAB, alloc_slab);
9528 STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
9529 STAT_ATTR(FREE_SLAB, free_slab);
9530 STAT_ATTR(ORDER_FALLBACK, order_fallback);
9531 STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
9532 STAT_ATTR(SHEAF_FLUSH, sheaf_flush);
9533 STAT_ATTR(SHEAF_REFILL, sheaf_refill);
9534 STAT_ATTR(SHEAF_ALLOC, sheaf_alloc);
9535 STAT_ATTR(SHEAF_FREE, sheaf_free);
9536 STAT_ATTR(BARN_GET, barn_get);
9537 STAT_ATTR(BARN_GET_FAIL, barn_get_fail);
9538 STAT_ATTR(BARN_PUT, barn_put);
9539 STAT_ATTR(BARN_PUT_FAIL, barn_put_fail);
9540 STAT_ATTR(SHEAF_PREFILL_FAST, sheaf_prefill_fast);
9541 STAT_ATTR(SHEAF_PREFILL_SLOW, sheaf_prefill_slow);
9542 STAT_ATTR(SHEAF_PREFILL_OVERSIZE, sheaf_prefill_oversize);
9543 STAT_ATTR(SHEAF_RETURN_FAST, sheaf_return_fast);
9544 STAT_ATTR(SHEAF_RETURN_SLOW, sheaf_return_slow);
9545 #endif /* CONFIG_SLUB_STATS */
9546
9547 #ifdef CONFIG_KFENCE
skip_kfence_show(struct kmem_cache * s,char * buf)9548 static ssize_t skip_kfence_show(struct kmem_cache *s, char *buf)
9549 {
9550 return sysfs_emit(buf, "%d\n", !!(s->flags & SLAB_SKIP_KFENCE));
9551 }
9552
skip_kfence_store(struct kmem_cache * s,const char * buf,size_t length)9553 static ssize_t skip_kfence_store(struct kmem_cache *s,
9554 const char *buf, size_t length)
9555 {
9556 int ret = length;
9557
9558 if (buf[0] == '0')
9559 s->flags &= ~SLAB_SKIP_KFENCE;
9560 else if (buf[0] == '1')
9561 s->flags |= SLAB_SKIP_KFENCE;
9562 else
9563 ret = -EINVAL;
9564
9565 return ret;
9566 }
9567 SLAB_ATTR(skip_kfence);
9568 #endif
9569
9570 static const struct attribute *const slab_attrs[] = {
9571 &slab_size_attr.attr,
9572 &object_size_attr.attr,
9573 &objs_per_slab_attr.attr,
9574 &order_attr.attr,
9575 &sheaf_capacity_attr.attr,
9576 &min_partial_attr.attr,
9577 &cpu_partial_attr.attr,
9578 &objects_partial_attr.attr,
9579 &partial_attr.attr,
9580 &cpu_slabs_attr.attr,
9581 &ctor_attr.attr,
9582 &aliases_attr.attr,
9583 &align_attr.attr,
9584 &hwcache_align_attr.attr,
9585 &reclaim_account_attr.attr,
9586 &destroy_by_rcu_attr.attr,
9587 &shrink_attr.attr,
9588 &slabs_cpu_partial_attr.attr,
9589 #ifdef CONFIG_SLUB_DEBUG
9590 &total_objects_attr.attr,
9591 &objects_attr.attr,
9592 &slabs_attr.attr,
9593 &sanity_checks_attr.attr,
9594 &trace_attr.attr,
9595 &red_zone_attr.attr,
9596 &poison_attr.attr,
9597 &store_user_attr.attr,
9598 &validate_attr.attr,
9599 #endif
9600 #ifdef CONFIG_ZONE_DMA
9601 &cache_dma_attr.attr,
9602 #endif
9603 #ifdef CONFIG_NUMA
9604 &remote_node_defrag_ratio_attr.attr,
9605 #endif
9606 #ifdef CONFIG_SLUB_STATS
9607 &alloc_fastpath_attr.attr,
9608 &alloc_slowpath_attr.attr,
9609 &free_rcu_sheaf_attr.attr,
9610 &free_rcu_sheaf_fail_attr.attr,
9611 &free_fastpath_attr.attr,
9612 &free_slowpath_attr.attr,
9613 &free_add_partial_attr.attr,
9614 &free_remove_partial_attr.attr,
9615 &alloc_slab_attr.attr,
9616 &alloc_node_mismatch_attr.attr,
9617 &free_slab_attr.attr,
9618 &order_fallback_attr.attr,
9619 &cmpxchg_double_fail_attr.attr,
9620 &sheaf_flush_attr.attr,
9621 &sheaf_refill_attr.attr,
9622 &sheaf_alloc_attr.attr,
9623 &sheaf_free_attr.attr,
9624 &barn_get_attr.attr,
9625 &barn_get_fail_attr.attr,
9626 &barn_put_attr.attr,
9627 &barn_put_fail_attr.attr,
9628 &sheaf_prefill_fast_attr.attr,
9629 &sheaf_prefill_slow_attr.attr,
9630 &sheaf_prefill_oversize_attr.attr,
9631 &sheaf_return_fast_attr.attr,
9632 &sheaf_return_slow_attr.attr,
9633 #endif
9634 #ifdef CONFIG_FAILSLAB
9635 &failslab_attr.attr,
9636 #endif
9637 #ifdef CONFIG_HARDENED_USERCOPY
9638 &usersize_attr.attr,
9639 #endif
9640 #ifdef CONFIG_KFENCE
9641 &skip_kfence_attr.attr,
9642 #endif
9643
9644 NULL
9645 };
9646
9647 ATTRIBUTE_GROUPS(slab);
9648
slab_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)9649 static ssize_t slab_attr_show(struct kobject *kobj,
9650 struct attribute *attr,
9651 char *buf)
9652 {
9653 const struct slab_attribute *attribute;
9654 struct kmem_cache *s;
9655
9656 attribute = to_slab_attr(attr);
9657 s = to_slab(kobj);
9658
9659 if (!attribute->show)
9660 return -EIO;
9661
9662 return attribute->show(s, buf);
9663 }
9664
slab_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)9665 static ssize_t slab_attr_store(struct kobject *kobj,
9666 struct attribute *attr,
9667 const char *buf, size_t len)
9668 {
9669 const struct slab_attribute *attribute;
9670 struct kmem_cache *s;
9671
9672 attribute = to_slab_attr(attr);
9673 s = to_slab(kobj);
9674
9675 if (!attribute->store)
9676 return -EIO;
9677
9678 return attribute->store(s, buf, len);
9679 }
9680
kmem_cache_release(struct kobject * k)9681 static void kmem_cache_release(struct kobject *k)
9682 {
9683 slab_kmem_cache_release(to_slab(k));
9684 }
9685
9686 static const struct sysfs_ops slab_sysfs_ops = {
9687 .show = slab_attr_show,
9688 .store = slab_attr_store,
9689 };
9690
9691 static const struct kobj_type slab_ktype = {
9692 .sysfs_ops = &slab_sysfs_ops,
9693 .release = kmem_cache_release,
9694 .default_groups = slab_groups,
9695 };
9696
9697 static struct kset *slab_kset;
9698
cache_kset(struct kmem_cache * s)9699 static inline struct kset *cache_kset(struct kmem_cache *s)
9700 {
9701 return slab_kset;
9702 }
9703
9704 #define ID_STR_LENGTH 32
9705
9706 /* Create a unique string id for a slab cache:
9707 *
9708 * Format :[flags-]size
9709 */
create_unique_id(struct kmem_cache * s)9710 static char *create_unique_id(struct kmem_cache *s)
9711 {
9712 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
9713 char *p = name;
9714
9715 if (!name)
9716 return ERR_PTR(-ENOMEM);
9717
9718 *p++ = ':';
9719 /*
9720 * First flags affecting slabcache operations. We will only
9721 * get here for aliasable slabs so we do not need to support
9722 * too many flags. The flags here must cover all flags that
9723 * are matched during merging to guarantee that the id is
9724 * unique.
9725 */
9726 if (s->flags & SLAB_CACHE_DMA)
9727 *p++ = 'd';
9728 if (s->flags & SLAB_CACHE_DMA32)
9729 *p++ = 'D';
9730 if (s->flags & SLAB_RECLAIM_ACCOUNT)
9731 *p++ = 'a';
9732 if (s->flags & SLAB_CONSISTENCY_CHECKS)
9733 *p++ = 'F';
9734 if (s->flags & SLAB_ACCOUNT)
9735 *p++ = 'A';
9736 if (p != name + 1)
9737 *p++ = '-';
9738 p += snprintf(p, ID_STR_LENGTH - (p - name), "%07u", s->size);
9739
9740 if (WARN_ON(p > name + ID_STR_LENGTH - 1)) {
9741 kfree(name);
9742 return ERR_PTR(-EINVAL);
9743 }
9744 kmsan_unpoison_memory(name, p - name);
9745 return name;
9746 }
9747
sysfs_slab_add(struct kmem_cache * s)9748 static int sysfs_slab_add(struct kmem_cache *s)
9749 {
9750 int err;
9751 const char *name;
9752 struct kset *kset = cache_kset(s);
9753 int unmergeable = slab_unmergeable(s);
9754
9755 if (!unmergeable && disable_higher_order_debug &&
9756 (slub_debug & DEBUG_METADATA_FLAGS))
9757 unmergeable = 1;
9758
9759 if (unmergeable) {
9760 /*
9761 * Slabcache can never be merged so we can use the name proper.
9762 * This is typically the case for debug situations. In that
9763 * case we can catch duplicate names easily.
9764 */
9765 sysfs_remove_link(&slab_kset->kobj, s->name);
9766 name = s->name;
9767 } else {
9768 /*
9769 * Create a unique name for the slab as a target
9770 * for the symlinks.
9771 */
9772 name = create_unique_id(s);
9773 if (IS_ERR(name))
9774 return PTR_ERR(name);
9775 }
9776
9777 s->kobj.kset = kset;
9778 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
9779 /*
9780 * Intentionally skip kobject_put(). See commit 2420baa8e046
9781 * ("mm/slab: Allow cache creation to proceed even if sysfs
9782 * registration fails")
9783 */
9784 if (err)
9785 goto out;
9786
9787 if (!unmergeable) {
9788 /* Setup first alias */
9789 sysfs_slab_alias(s, s->name);
9790 }
9791 out:
9792 if (!unmergeable)
9793 kfree(name);
9794 return err;
9795 }
9796
sysfs_slab_unlink(struct kmem_cache * s)9797 void sysfs_slab_unlink(struct kmem_cache *s)
9798 {
9799 if (s->kobj.state_in_sysfs)
9800 kobject_del(&s->kobj);
9801 }
9802
sysfs_slab_release(struct kmem_cache * s)9803 void sysfs_slab_release(struct kmem_cache *s)
9804 {
9805 kobject_put(&s->kobj);
9806 }
9807
9808 /*
9809 * Need to buffer aliases during bootup until sysfs becomes
9810 * available lest we lose that information.
9811 */
9812 struct saved_alias {
9813 struct kmem_cache *s;
9814 const char *name;
9815 struct saved_alias *next;
9816 };
9817
9818 static struct saved_alias *alias_list;
9819
sysfs_slab_alias(struct kmem_cache * s,const char * name)9820 int sysfs_slab_alias(struct kmem_cache *s, const char *name)
9821 {
9822 struct saved_alias *al;
9823
9824 if (slab_state == FULL) {
9825 /*
9826 * If we have a leftover link then remove it.
9827 */
9828 sysfs_remove_link(&slab_kset->kobj, name);
9829 /*
9830 * The original cache may have failed to generate sysfs file.
9831 * In that case, sysfs_create_link() returns -ENOENT and
9832 * symbolic link creation is skipped.
9833 */
9834 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
9835 }
9836
9837 al = kmalloc_obj(struct saved_alias);
9838 if (!al)
9839 return -ENOMEM;
9840
9841 al->s = s;
9842 al->name = name;
9843 al->next = alias_list;
9844 alias_list = al;
9845 kmsan_unpoison_memory(al, sizeof(*al));
9846 return 0;
9847 }
9848
slab_kset_init(void)9849 static int __init slab_kset_init(void)
9850 {
9851 slab_kset = kset_create_and_add("slab", NULL, kernel_kobj);
9852 if (!slab_kset) {
9853 pr_err("Cannot register slab subsystem.\n");
9854 return -ENOMEM;
9855 }
9856
9857 return 0;
9858 }
9859
slab_sysfs_process_aliases(void)9860 static void __init slab_sysfs_process_aliases(void)
9861 {
9862 int err;
9863
9864 while (alias_list) {
9865 struct saved_alias *al = alias_list;
9866
9867 alias_list = alias_list->next;
9868 err = sysfs_slab_alias(al->s, al->name);
9869 if (err)
9870 pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
9871 al->name);
9872 kfree(al);
9873 }
9874 }
9875 #endif /* SLAB_SUPPORTS_SYSFS */
9876
9877 #if defined(SLAB_SUPPORTS_SYSFS) || \
9878 (defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS))
slab_late_init(void)9879 static int __init slab_late_init(void)
9880 {
9881 struct kmem_cache *s;
9882 int err;
9883
9884 mutex_lock(&slab_mutex);
9885
9886 err = slab_kset_init();
9887 if (err)
9888 goto out;
9889
9890 slab_debugfs_root_init();
9891 slab_state = FULL;
9892
9893 list_for_each_entry(s, &slab_caches, list) {
9894 if (sysfs_slab_add(s))
9895 pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
9896 s->name);
9897
9898 if (s->flags & SLAB_STORE_USER)
9899 debugfs_slab_add(s);
9900 }
9901
9902 slab_sysfs_process_aliases();
9903 out:
9904 mutex_unlock(&slab_mutex);
9905 return err;
9906 }
9907 late_initcall(slab_late_init);
9908 #endif
9909
9910 #if defined(CONFIG_SLUB_DEBUG) && defined(CONFIG_DEBUG_FS)
slab_debugfs_show(struct seq_file * seq,void * v)9911 static int slab_debugfs_show(struct seq_file *seq, void *v)
9912 {
9913 struct loc_track *t = seq->private;
9914 struct location *l;
9915 unsigned long idx;
9916
9917 idx = (unsigned long) t->idx;
9918 if (idx < t->count) {
9919 l = &t->loc[idx];
9920
9921 seq_printf(seq, "%7ld ", l->count);
9922
9923 if (l->addr)
9924 seq_printf(seq, "%pS", (void *)l->addr);
9925 else
9926 seq_puts(seq, "<not-available>");
9927
9928 if (l->waste)
9929 seq_printf(seq, " waste=%lu/%lu",
9930 l->count * l->waste, l->waste);
9931
9932 if (l->sum_time != l->min_time) {
9933 seq_printf(seq, " age=%ld/%llu/%ld",
9934 l->min_time, div_u64(l->sum_time, l->count),
9935 l->max_time);
9936 } else
9937 seq_printf(seq, " age=%ld", l->min_time);
9938
9939 if (l->min_pid != l->max_pid)
9940 seq_printf(seq, " pid=%ld-%ld", l->min_pid, l->max_pid);
9941 else
9942 seq_printf(seq, " pid=%ld",
9943 l->min_pid);
9944
9945 if (num_online_cpus() > 1 && !cpumask_empty(to_cpumask(l->cpus)))
9946 seq_printf(seq, " cpus=%*pbl",
9947 cpumask_pr_args(to_cpumask(l->cpus)));
9948
9949 if (nr_online_nodes > 1 && !nodes_empty(l->nodes))
9950 seq_printf(seq, " nodes=%*pbl",
9951 nodemask_pr_args(&l->nodes));
9952
9953 #ifdef CONFIG_STACKDEPOT
9954 {
9955 depot_stack_handle_t handle;
9956 unsigned long *entries;
9957 unsigned int nr_entries, j;
9958
9959 handle = READ_ONCE(l->handle);
9960 if (handle) {
9961 nr_entries = stack_depot_fetch(handle, &entries);
9962 seq_puts(seq, "\n");
9963 for (j = 0; j < nr_entries; j++)
9964 seq_printf(seq, " %pS\n", (void *)entries[j]);
9965 }
9966 }
9967 #endif
9968 seq_puts(seq, "\n");
9969 }
9970
9971 if (!idx && !t->count)
9972 seq_puts(seq, "No data\n");
9973
9974 return 0;
9975 }
9976
slab_debugfs_stop(struct seq_file * seq,void * v)9977 static void slab_debugfs_stop(struct seq_file *seq, void *v)
9978 {
9979 }
9980
slab_debugfs_next(struct seq_file * seq,void * v,loff_t * ppos)9981 static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos)
9982 {
9983 struct loc_track *t = seq->private;
9984
9985 t->idx = ++(*ppos);
9986 if (*ppos <= t->count)
9987 return ppos;
9988
9989 return NULL;
9990 }
9991
cmp_loc_by_count(const void * a,const void * b)9992 static int cmp_loc_by_count(const void *a, const void *b)
9993 {
9994 struct location *loc1 = (struct location *)a;
9995 struct location *loc2 = (struct location *)b;
9996
9997 return cmp_int(loc2->count, loc1->count);
9998 }
9999
slab_debugfs_start(struct seq_file * seq,loff_t * ppos)10000 static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos)
10001 {
10002 struct loc_track *t = seq->private;
10003
10004 t->idx = *ppos;
10005 return ppos;
10006 }
10007
10008 static const struct seq_operations slab_debugfs_sops = {
10009 .start = slab_debugfs_start,
10010 .next = slab_debugfs_next,
10011 .stop = slab_debugfs_stop,
10012 .show = slab_debugfs_show,
10013 };
10014
slab_debug_trace_open(struct inode * inode,struct file * filep)10015 static int slab_debug_trace_open(struct inode *inode, struct file *filep)
10016 {
10017
10018 struct kmem_cache_node *n;
10019 enum track_item alloc;
10020 int node;
10021 struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops,
10022 sizeof(struct loc_track));
10023 struct kmem_cache *s = file_inode(filep)->i_private;
10024 unsigned long *obj_map;
10025
10026 if (!t)
10027 return -ENOMEM;
10028
10029 obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
10030 if (!obj_map) {
10031 seq_release_private(inode, filep);
10032 return -ENOMEM;
10033 }
10034
10035 alloc = debugfs_get_aux_num(filep);
10036
10037 if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL)) {
10038 bitmap_free(obj_map);
10039 seq_release_private(inode, filep);
10040 return -ENOMEM;
10041 }
10042
10043 for_each_kmem_cache_node(s, node, n) {
10044 unsigned long flags;
10045 struct slab *slab;
10046
10047 if (!node_nr_slabs(n))
10048 continue;
10049
10050 spin_lock_irqsave(&n->list_lock, flags);
10051 list_for_each_entry(slab, &n->partial, slab_list)
10052 process_slab(t, s, slab, alloc, obj_map);
10053 list_for_each_entry(slab, &n->full, slab_list)
10054 process_slab(t, s, slab, alloc, obj_map);
10055 spin_unlock_irqrestore(&n->list_lock, flags);
10056 }
10057
10058 /* Sort locations by count */
10059 sort(t->loc, t->count, sizeof(struct location),
10060 cmp_loc_by_count, NULL);
10061
10062 bitmap_free(obj_map);
10063 return 0;
10064 }
10065
slab_debug_trace_release(struct inode * inode,struct file * file)10066 static int slab_debug_trace_release(struct inode *inode, struct file *file)
10067 {
10068 struct seq_file *seq = file->private_data;
10069 struct loc_track *t = seq->private;
10070
10071 free_loc_track(t);
10072 return seq_release_private(inode, file);
10073 }
10074
10075 static const struct file_operations slab_debugfs_fops = {
10076 .open = slab_debug_trace_open,
10077 .read = seq_read,
10078 .llseek = seq_lseek,
10079 .release = slab_debug_trace_release,
10080 };
10081
debugfs_slab_add(struct kmem_cache * s)10082 static void debugfs_slab_add(struct kmem_cache *s)
10083 {
10084 struct dentry *slab_cache_dir;
10085
10086 if (unlikely(!slab_debugfs_root))
10087 return;
10088
10089 slab_cache_dir = debugfs_create_dir(s->name, slab_debugfs_root);
10090
10091 debugfs_create_file_aux_num("alloc_traces", 0400, slab_cache_dir, s,
10092 TRACK_ALLOC, &slab_debugfs_fops);
10093
10094 debugfs_create_file_aux_num("free_traces", 0400, slab_cache_dir, s,
10095 TRACK_FREE, &slab_debugfs_fops);
10096 }
10097
debugfs_slab_release(struct kmem_cache * s)10098 void debugfs_slab_release(struct kmem_cache *s)
10099 {
10100 if (unlikely(!slab_debugfs_root))
10101 return;
10102
10103 debugfs_lookup_and_remove(s->name, slab_debugfs_root);
10104 }
10105
slab_debugfs_root_init(void)10106 static void __init slab_debugfs_root_init(void)
10107 {
10108 slab_debugfs_root = debugfs_create_dir("slab", NULL);
10109 }
10110 #endif
10111 /*
10112 * The /proc/slabinfo ABI
10113 */
10114 #ifdef CONFIG_SLUB_DEBUG
get_slabinfo(struct kmem_cache * s,struct slabinfo * sinfo)10115 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
10116 {
10117 unsigned long nr_slabs = 0;
10118 unsigned long nr_objs = 0;
10119 unsigned long nr_free = 0;
10120 int node;
10121 struct kmem_cache_node *n;
10122
10123 for_each_kmem_cache_node(s, node, n) {
10124 nr_slabs += node_nr_slabs(n);
10125 nr_objs += node_nr_objs(n);
10126 nr_free += count_partial_free_approx(n);
10127 }
10128
10129 sinfo->active_objs = nr_objs - nr_free;
10130 sinfo->num_objs = nr_objs;
10131 sinfo->active_slabs = nr_slabs;
10132 sinfo->num_slabs = nr_slabs;
10133 sinfo->objects_per_slab = oo_objects(s->oo);
10134 sinfo->cache_order = oo_order(s->oo);
10135 }
10136 #endif /* CONFIG_SLUB_DEBUG */
10137