blob: fd6627e2d11507bbc376be736cd9625098eb67df [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001#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
14struct kmem_cache_node {
15 spinlock_t list_lock; /* Protect partial list and nr_partial */
16 unsigned long nr_partial;
17 atomic_long_t nr_slabs;
18 struct list_head partial;
Christoph Lameter643b1132007-05-06 14:49:42 -070019 struct list_head full;
Christoph Lameter81819f02007-05-06 14:49:36 -070020};
21
22/*
23 * Slab cache management.
24 */
25struct kmem_cache {
26 /* Used for retriving partial slabs etc */
27 unsigned long flags;
28 int size; /* The size of an object including meta data */
29 int objsize; /* The size of an object without meta data */
30 int offset; /* Free pointer offset. */
31 unsigned int order;
32
33 /*
34 * Avoid an extra cache line for UP, SMP and for the node local to
35 * struct kmem_cache.
36 */
37 struct kmem_cache_node local_node;
38
39 /* Allocation and freeing of slabs */
40 int objects; /* Number of objects in slab */
41 int refcount; /* Refcount for slab cache destroy */
42 void (*ctor)(void *, struct kmem_cache *, unsigned long);
43 void (*dtor)(void *, struct kmem_cache *, unsigned long);
44 int inuse; /* Offset to metadata */
45 int align; /* Alignment */
46 const char *name; /* Name (only for display!) */
47 struct list_head list; /* List of slab caches */
48 struct kobject kobj; /* For sysfs */
49
50#ifdef CONFIG_NUMA
51 int defrag_ratio;
52 struct kmem_cache_node *node[MAX_NUMNODES];
53#endif
54 struct page *cpu_slab[NR_CPUS];
55};
56
57/*
58 * Kmalloc subsystem.
59 */
60#define KMALLOC_SHIFT_LOW 3
61
62#ifdef CONFIG_LARGE_ALLOCS
Christoph Lametercfbf07f2007-05-15 01:42:06 -070063#define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT) =< 25 ? \
64 (MAX_ORDER + PAGE_SHIFT - 1) : 25)
Christoph Lameter81819f02007-05-06 14:49:36 -070065#else
66#if !defined(CONFIG_MMU) || NR_CPUS > 512 || MAX_NUMNODES > 256
67#define KMALLOC_SHIFT_HIGH 20
68#else
69#define KMALLOC_SHIFT_HIGH 18
70#endif
71#endif
72
73/*
74 * We keep the general caches in an array of slab caches that are used for
75 * 2^x bytes of allocations.
76 */
77extern struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
78
79/*
80 * Sorry that the following has to be that ugly but some versions of GCC
81 * have trouble with constant propagation and loops.
82 */
83static inline int kmalloc_index(int size)
84{
Christoph Lameter614410d2007-05-06 14:49:38 -070085 /*
86 * We should return 0 if size == 0 but we use the smallest object
87 * here for SLAB legacy reasons.
88 */
89 WARN_ON_ONCE(size == 0);
90
Christoph Lametercfbf07f2007-05-15 01:42:06 -070091 if (size >= (1 << KMALLOC_SHIFT_HIGH))
92 return -1;
93
Christoph Lameter81819f02007-05-06 14:49:36 -070094 if (size > 64 && size <= 96)
95 return 1;
96 if (size > 128 && size <= 192)
97 return 2;
98 if (size <= 8) return 3;
99 if (size <= 16) return 4;
100 if (size <= 32) return 5;
101 if (size <= 64) return 6;
102 if (size <= 128) return 7;
103 if (size <= 256) return 8;
104 if (size <= 512) return 9;
105 if (size <= 1024) return 10;
106 if (size <= 2 * 1024) return 11;
107 if (size <= 4 * 1024) return 12;
108 if (size <= 8 * 1024) return 13;
109 if (size <= 16 * 1024) return 14;
110 if (size <= 32 * 1024) return 15;
111 if (size <= 64 * 1024) return 16;
112 if (size <= 128 * 1024) return 17;
113 if (size <= 256 * 1024) return 18;
114#if KMALLOC_SHIFT_HIGH > 18
115 if (size <= 512 * 1024) return 19;
116 if (size <= 1024 * 1024) return 20;
117#endif
118#if KMALLOC_SHIFT_HIGH > 20
119 if (size <= 2 * 1024 * 1024) return 21;
120 if (size <= 4 * 1024 * 1024) return 22;
121 if (size <= 8 * 1024 * 1024) return 23;
122 if (size <= 16 * 1024 * 1024) return 24;
123 if (size <= 32 * 1024 * 1024) return 25;
124#endif
125 return -1;
126
127/*
128 * What we really wanted to do and cannot do because of compiler issues is:
129 * int i;
130 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
131 * if (size <= (1 << i))
132 * return i;
133 */
134}
135
136/*
137 * Find the slab cache for a given combination of allocation flags and size.
138 *
139 * This ought to end up with a global pointer to the right cache
140 * in kmalloc_caches.
141 */
142static inline struct kmem_cache *kmalloc_slab(size_t size)
143{
144 int index = kmalloc_index(size);
145
146 if (index == 0)
147 return NULL;
148
149 if (index < 0) {
150 /*
151 * Generate a link failure. Would be great if we could
152 * do something to stop the compile here.
153 */
154 extern void __kmalloc_size_too_large(void);
155 __kmalloc_size_too_large();
156 }
157 return &kmalloc_caches[index];
158}
159
160#ifdef CONFIG_ZONE_DMA
161#define SLUB_DMA __GFP_DMA
162#else
163/* Disable DMA functionality */
164#define SLUB_DMA 0
165#endif
166
167static inline void *kmalloc(size_t size, gfp_t flags)
168{
169 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
170 struct kmem_cache *s = kmalloc_slab(size);
171
172 if (!s)
173 return NULL;
174
175 return kmem_cache_alloc(s, flags);
176 } else
177 return __kmalloc(size, flags);
178}
179
180static inline void *kzalloc(size_t size, gfp_t flags)
181{
182 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
183 struct kmem_cache *s = kmalloc_slab(size);
184
185 if (!s)
186 return NULL;
187
188 return kmem_cache_zalloc(s, flags);
189 } else
190 return __kzalloc(size, flags);
191}
192
193#ifdef CONFIG_NUMA
194extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
195
196static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
197{
198 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
199 struct kmem_cache *s = kmalloc_slab(size);
200
201 if (!s)
202 return NULL;
203
204 return kmem_cache_alloc_node(s, flags, node);
205 } else
206 return __kmalloc_node(size, flags, node);
207}
208#endif
209
210#endif /* _LINUX_SLUB_DEF_H */