Merge tag 'fbdev-for-6.4-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-block.git] / include / linux / slab_def.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
2e892f43
CL
2#ifndef _LINUX_SLAB_DEF_H
3#define _LINUX_SLAB_DEF_H
4
d3fb45f3 5#include <linux/kfence.h>
809fa972
HFS
6#include <linux/reciprocal_div.h>
7
2e892f43
CL
8/*
9 * Definitions unique to the original Linux SLAB allocator.
8eae985f
PE
10 */
11
12struct kmem_cache {
bf0dea23
JK
13 struct array_cache __percpu *cpu_cache;
14
24755e2e 15/* 1) Cache tunables. Protected by slab_mutex */
8eae985f
PE
16 unsigned int batchcount;
17 unsigned int limit;
18 unsigned int shared;
19
3b0efdfa 20 unsigned int size;
809fa972 21 struct reciprocal_value reciprocal_buffer_size;
b56efcf0 22/* 2) touched by every alloc & free from the backend */
8eae985f 23
d50112ed 24 slab_flags_t flags; /* constant flags */
8eae985f
PE
25 unsigned int num; /* # of objs per slab */
26
b56efcf0 27/* 3) cache_grow/shrink */
8eae985f
PE
28 /* order of pgs per slab (2^n) */
29 unsigned int gfporder;
30
31 /* force GFP flags, e.g. GFP_DMA */
a618e89f 32 gfp_t allocflags;
8eae985f
PE
33
34 size_t colour; /* cache colouring range */
35 unsigned int colour_off; /* colour offset */
8456a648 36 unsigned int freelist_size;
8eae985f
PE
37
38 /* constructor func */
39 void (*ctor)(void *obj);
40
b56efcf0 41/* 4) cache creation/removal */
8eae985f 42 const char *name;
3b0efdfa
CL
43 struct list_head list;
44 int refcount;
45 int object_size;
46 int align;
8eae985f 47
b56efcf0 48/* 5) statistics */
8eae985f
PE
49#ifdef CONFIG_DEBUG_SLAB
50 unsigned long num_active;
51 unsigned long num_allocations;
52 unsigned long high_mark;
53 unsigned long grown;
54 unsigned long reaped;
55 unsigned long errors;
56 unsigned long max_freeable;
57 unsigned long node_allocs;
58 unsigned long node_frees;
59 unsigned long node_overflow;
60 atomic_t allochit;
61 atomic_t allocmiss;
62 atomic_t freehit;
63 atomic_t freemiss;
64
65 /*
66 * If debugging is enabled, then the allocator can add additional
05fec35e
BH
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
70 * size.
8eae985f
PE
71 */
72 int obj_offset;
8eae985f 73#endif /* CONFIG_DEBUG_SLAB */
127424c8 74
bbc61844 75#ifdef CONFIG_KASAN_GENERIC
7ed2f9e6
AP
76 struct kasan_cache kasan_info;
77#endif
8eae985f 78
c7ce4f60 79#ifdef CONFIG_SLAB_FREELIST_RANDOM
7c00fce9 80 unsigned int *random_seq;
c7ce4f60
TG
81#endif
82
346907ce 83#ifdef CONFIG_HARDENED_USERCOPY
7bbdb81e
AD
84 unsigned int useroffset; /* Usercopy region offset */
85 unsigned int usersize; /* Usercopy region size */
346907ce 86#endif
8eb8284b 87
bf0dea23 88 struct kmem_cache_node *node[MAX_NUMNODES];
8eae985f
PE
89};
90
40f3bf0c 91static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab,
80a9201a
AP
92 void *x)
93{
40f3bf0c
VB
94 void *object = x - (x - slab->s_mem) % cache->size;
95 void *last_object = slab->s_mem + (cache->num - 1) * cache->size;
7ed2f9e6
AP
96
97 if (unlikely(object > last_object))
98 return last_object;
99 else
100 return object;
101}
102
5b7c4148
AK
103/*
104 * We want to avoid an expensive divide : (offset / cache->size)
105 * Using the fact that size is a constant for a particular cache,
106 * we can replace (offset / cache->size) by
107 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
108 */
109static inline unsigned int obj_to_index(const struct kmem_cache *cache,
40f3bf0c 110 const struct slab *slab, void *obj)
5b7c4148 111{
40f3bf0c 112 u32 offset = (obj - slab->s_mem);
5b7c4148
AK
113 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
114}
115
40f3bf0c
VB
116static inline int objs_per_slab(const struct kmem_cache *cache,
117 const struct slab *slab)
286e04b8 118{
40f3bf0c 119 if (is_kfence_address(slab_address(slab)))
d3fb45f3 120 return 1;
286e04b8
RG
121 return cache->num;
122}
123
2e892f43 124#endif /* _LINUX_SLAB_DEF_H */