]>
Commit | Line | Data |
---|---|---|
81819f0f CL |
1 | #ifndef _LINUX_SLUB_DEF_H |
2 | #define _LINUX_SLUB_DEF_H | |
3 | ||
4 | /* | |
5 | * SLUB : A Slab allocator without object queues. | |
6 | * | |
7 | * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com> | |
8 | */ | |
9 | #include <linux/types.h> | |
10 | #include <linux/gfp.h> | |
11 | #include <linux/workqueue.h> | |
12 | #include <linux/kobject.h> | |
13 | ||
dfb4f096 CL |
14 | struct kmem_cache_cpu { |
15 | void **freelist; | |
16 | struct page *page; | |
17 | int node; | |
b3fba8da | 18 | unsigned int offset; |
dfb4f096 CL |
19 | /* Lots of wasted space */ |
20 | } ____cacheline_aligned_in_smp; | |
21 | ||
81819f0f CL |
22 | struct kmem_cache_node { |
23 | spinlock_t list_lock; /* Protect partial list and nr_partial */ | |
24 | unsigned long nr_partial; | |
25 | atomic_long_t nr_slabs; | |
26 | struct list_head partial; | |
0c710013 | 27 | #ifdef CONFIG_SLUB_DEBUG |
643b1138 | 28 | struct list_head full; |
0c710013 | 29 | #endif |
81819f0f CL |
30 | }; |
31 | ||
32 | /* | |
33 | * Slab cache management. | |
34 | */ | |
35 | struct kmem_cache { | |
36 | /* Used for retriving partial slabs etc */ | |
37 | unsigned long flags; | |
38 | int size; /* The size of an object including meta data */ | |
39 | int objsize; /* The size of an object without meta data */ | |
40 | int offset; /* Free pointer offset. */ | |
4b356be0 | 41 | int order; |
81819f0f CL |
42 | |
43 | /* | |
44 | * Avoid an extra cache line for UP, SMP and for the node local to | |
45 | * struct kmem_cache. | |
46 | */ | |
47 | struct kmem_cache_node local_node; | |
48 | ||
49 | /* Allocation and freeing of slabs */ | |
50 | int objects; /* Number of objects in slab */ | |
51 | int refcount; /* Refcount for slab cache destroy */ | |
52 | void (*ctor)(void *, struct kmem_cache *, unsigned long); | |
81819f0f CL |
53 | int inuse; /* Offset to metadata */ |
54 | int align; /* Alignment */ | |
55 | const char *name; /* Name (only for display!) */ | |
56 | struct list_head list; /* List of slab caches */ | |
0c710013 | 57 | #ifdef CONFIG_SLUB_DEBUG |
81819f0f | 58 | struct kobject kobj; /* For sysfs */ |
0c710013 | 59 | #endif |
81819f0f CL |
60 | |
61 | #ifdef CONFIG_NUMA | |
62 | int defrag_ratio; | |
63 | struct kmem_cache_node *node[MAX_NUMNODES]; | |
64 | #endif | |
dfb4f096 | 65 | struct kmem_cache_cpu cpu_slab[NR_CPUS]; |
81819f0f CL |
66 | }; |
67 | ||
68 | /* | |
69 | * Kmalloc subsystem. | |
70 | */ | |
4b356be0 CL |
71 | #if defined(ARCH_KMALLOC_MINALIGN) && ARCH_KMALLOC_MINALIGN > 8 |
72 | #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN | |
73 | #else | |
74 | #define KMALLOC_MIN_SIZE 8 | |
75 | #endif | |
76 | ||
77 | #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE) | |
81819f0f | 78 | |
81819f0f CL |
79 | /* |
80 | * We keep the general caches in an array of slab caches that are used for | |
81 | * 2^x bytes of allocations. | |
82 | */ | |
aadb4bc4 | 83 | extern struct kmem_cache kmalloc_caches[PAGE_SHIFT]; |
81819f0f CL |
84 | |
85 | /* | |
86 | * Sorry that the following has to be that ugly but some versions of GCC | |
87 | * have trouble with constant propagation and loops. | |
88 | */ | |
aa137f9d | 89 | static __always_inline int kmalloc_index(size_t size) |
81819f0f | 90 | { |
272c1d21 CL |
91 | if (!size) |
92 | return 0; | |
614410d5 | 93 | |
4b356be0 CL |
94 | if (size <= KMALLOC_MIN_SIZE) |
95 | return KMALLOC_SHIFT_LOW; | |
96 | ||
81819f0f CL |
97 | if (size > 64 && size <= 96) |
98 | return 1; | |
99 | if (size > 128 && size <= 192) | |
100 | return 2; | |
101 | if (size <= 8) return 3; | |
102 | if (size <= 16) return 4; | |
103 | if (size <= 32) return 5; | |
104 | if (size <= 64) return 6; | |
105 | if (size <= 128) return 7; | |
106 | if (size <= 256) return 8; | |
107 | if (size <= 512) return 9; | |
108 | if (size <= 1024) return 10; | |
109 | if (size <= 2 * 1024) return 11; | |
aadb4bc4 CL |
110 | /* |
111 | * The following is only needed to support architectures with a larger page | |
112 | * size than 4k. | |
113 | */ | |
81819f0f CL |
114 | if (size <= 4 * 1024) return 12; |
115 | if (size <= 8 * 1024) return 13; | |
116 | if (size <= 16 * 1024) return 14; | |
117 | if (size <= 32 * 1024) return 15; | |
118 | if (size <= 64 * 1024) return 16; | |
119 | if (size <= 128 * 1024) return 17; | |
120 | if (size <= 256 * 1024) return 18; | |
aadb4bc4 | 121 | if (size <= 512 * 1024) return 19; |
81819f0f | 122 | if (size <= 1024 * 1024) return 20; |
81819f0f | 123 | if (size <= 2 * 1024 * 1024) return 21; |
81819f0f CL |
124 | return -1; |
125 | ||
126 | /* | |
127 | * What we really wanted to do and cannot do because of compiler issues is: | |
128 | * int i; | |
129 | * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) | |
130 | * if (size <= (1 << i)) | |
131 | * return i; | |
132 | */ | |
133 | } | |
134 | ||
135 | /* | |
136 | * Find the slab cache for a given combination of allocation flags and size. | |
137 | * | |
138 | * This ought to end up with a global pointer to the right cache | |
139 | * in kmalloc_caches. | |
140 | */ | |
aa137f9d | 141 | static __always_inline struct kmem_cache *kmalloc_slab(size_t size) |
81819f0f CL |
142 | { |
143 | int index = kmalloc_index(size); | |
144 | ||
145 | if (index == 0) | |
146 | return NULL; | |
147 | ||
81819f0f CL |
148 | return &kmalloc_caches[index]; |
149 | } | |
150 | ||
151 | #ifdef CONFIG_ZONE_DMA | |
152 | #define SLUB_DMA __GFP_DMA | |
153 | #else | |
154 | /* Disable DMA functionality */ | |
d046943c | 155 | #define SLUB_DMA (__force gfp_t)0 |
81819f0f CL |
156 | #endif |
157 | ||
6193a2ff PM |
158 | void *kmem_cache_alloc(struct kmem_cache *, gfp_t); |
159 | void *__kmalloc(size_t size, gfp_t flags); | |
160 | ||
aa137f9d | 161 | static __always_inline void *kmalloc(size_t size, gfp_t flags) |
81819f0f | 162 | { |
aadb4bc4 CL |
163 | if (__builtin_constant_p(size)) { |
164 | if (size > PAGE_SIZE / 2) | |
165 | return (void *)__get_free_pages(flags | __GFP_COMP, | |
166 | get_order(size)); | |
81819f0f | 167 | |
aadb4bc4 CL |
168 | if (!(flags & SLUB_DMA)) { |
169 | struct kmem_cache *s = kmalloc_slab(size); | |
170 | ||
171 | if (!s) | |
172 | return ZERO_SIZE_PTR; | |
81819f0f | 173 | |
aadb4bc4 CL |
174 | return kmem_cache_alloc(s, flags); |
175 | } | |
176 | } | |
177 | return __kmalloc(size, flags); | |
81819f0f CL |
178 | } |
179 | ||
81819f0f | 180 | #ifdef CONFIG_NUMA |
6193a2ff PM |
181 | void *__kmalloc_node(size_t size, gfp_t flags, int node); |
182 | void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); | |
81819f0f | 183 | |
aa137f9d | 184 | static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) |
81819f0f | 185 | { |
aadb4bc4 CL |
186 | if (__builtin_constant_p(size) && |
187 | size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) { | |
188 | struct kmem_cache *s = kmalloc_slab(size); | |
81819f0f CL |
189 | |
190 | if (!s) | |
272c1d21 | 191 | return ZERO_SIZE_PTR; |
81819f0f CL |
192 | |
193 | return kmem_cache_alloc_node(s, flags, node); | |
aadb4bc4 CL |
194 | } |
195 | return __kmalloc_node(size, flags, node); | |
81819f0f CL |
196 | } |
197 | #endif | |
198 | ||
199 | #endif /* _LINUX_SLUB_DEF_H */ |