1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SLAB_DEF_H
3 #define _LINUX_SLAB_DEF_H
5 #include <linux/kfence.h>
6 #include <linux/reciprocal_div.h>
9 * Definitions unique to the original Linux SLAB allocator.
13 struct array_cache __percpu *cpu_cache;
15 /* 1) Cache tunables. Protected by slab_mutex */
16 unsigned int batchcount;
21 struct reciprocal_value reciprocal_buffer_size;
22 /* 2) touched by every alloc & free from the backend */
24 slab_flags_t flags; /* constant flags */
25 unsigned int num; /* # of objs per slab */
27 /* 3) cache_grow/shrink */
28 /* order of pgs per slab (2^n) */
29 unsigned int gfporder;
31 /* force GFP flags, e.g. GFP_DMA */
34 size_t colour; /* cache colouring range */
35 unsigned int colour_off; /* colour offset */
36 unsigned int freelist_size;
38 /* constructor func */
39 void (*ctor)(void *obj);
41 /* 4) cache creation/removal */
43 struct list_head list;
49 #ifdef CONFIG_DEBUG_SLAB
50 unsigned long num_active;
51 unsigned long num_allocations;
52 unsigned long high_mark;
56 unsigned long max_freeable;
57 unsigned long node_allocs;
58 unsigned long node_frees;
59 unsigned long node_overflow;
66 * If debugging is enabled, then the allocator can add additional
67 * fields and/or padding to every object. 'size' contains the total
68 * object size including these internal fields, while 'obj_offset'
69 * and 'object_size' contain the offset to the user object and its
73 #endif /* CONFIG_DEBUG_SLAB */
76 struct kasan_cache kasan_info;
79 #ifdef CONFIG_SLAB_FREELIST_RANDOM
80 unsigned int *random_seq;
83 unsigned int useroffset; /* Usercopy region offset */
84 unsigned int usersize; /* Usercopy region size */
86 struct kmem_cache_node *node[MAX_NUMNODES];
89 static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab,
92 void *object = x - (x - slab->s_mem) % cache->size;
93 void *last_object = slab->s_mem + (cache->num - 1) * cache->size;
95 if (unlikely(object > last_object))
102 * We want to avoid an expensive divide : (offset / cache->size)
103 * Using the fact that size is a constant for a particular cache,
104 * we can replace (offset / cache->size) by
105 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
107 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
108 const struct slab *slab, void *obj)
110 u32 offset = (obj - slab->s_mem);
111 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
114 static inline int objs_per_slab(const struct kmem_cache *cache,
115 const struct slab *slab)
117 if (is_kfence_address(slab_address(slab)))
122 #endif /* _LINUX_SLAB_DEF_H */