blob: 3b361b2906bdb563b6a082b214ff8b9bd0cb1e73 [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 Lameter0c710012007-07-17 04:03:24 -070019#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -070020 struct list_head full;
Christoph Lameter0c710012007-07-17 04:03:24 -070021#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070022};
23
24/*
25 * Slab cache management.
26 */
27struct kmem_cache {
28 /* Used for retriving partial slabs etc */
29 unsigned long flags;
30 int size; /* The size of an object including meta data */
31 int objsize; /* The size of an object without meta data */
32 int offset; /* Free pointer offset. */
Christoph Lameter4b356be2007-06-16 10:16:13 -070033 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -070034
35 /*
36 * Avoid an extra cache line for UP, SMP and for the node local to
37 * struct kmem_cache.
38 */
39 struct kmem_cache_node local_node;
40
41 /* Allocation and freeing of slabs */
42 int objects; /* Number of objects in slab */
43 int refcount; /* Refcount for slab cache destroy */
44 void (*ctor)(void *, struct kmem_cache *, unsigned long);
Christoph Lameter81819f02007-05-06 14:49:36 -070045 int inuse; /* Offset to metadata */
46 int align; /* Alignment */
47 const char *name; /* Name (only for display!) */
48 struct list_head list; /* List of slab caches */
Christoph Lameter0c710012007-07-17 04:03:24 -070049#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -070050 struct kobject kobj; /* For sysfs */
Christoph Lameter0c710012007-07-17 04:03:24 -070051#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070052
53#ifdef CONFIG_NUMA
54 int defrag_ratio;
55 struct kmem_cache_node *node[MAX_NUMNODES];
56#endif
57 struct page *cpu_slab[NR_CPUS];
58};
59
60/*
61 * Kmalloc subsystem.
62 */
Christoph Lameter4b356be2007-06-16 10:16:13 -070063#if defined(ARCH_KMALLOC_MINALIGN) && ARCH_KMALLOC_MINALIGN > 8
64#define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
65#else
66#define KMALLOC_MIN_SIZE 8
67#endif
68
69#define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
Christoph Lameter81819f02007-05-06 14:49:36 -070070
Christoph Lameter81819f02007-05-06 14:49:36 -070071/*
72 * We keep the general caches in an array of slab caches that are used for
73 * 2^x bytes of allocations.
74 */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -070075extern struct kmem_cache kmalloc_caches[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -070076
77/*
78 * Sorry that the following has to be that ugly but some versions of GCC
79 * have trouble with constant propagation and loops.
80 */
Christoph Lameteraa137f92007-08-31 00:48:45 -070081static __always_inline int kmalloc_index(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -070082{
Christoph Lameter272c1d22007-06-08 13:46:49 -070083 if (!size)
84 return 0;
Christoph Lameter614410d2007-05-06 14:49:38 -070085
Christoph Lameter4b356be2007-06-16 10:16:13 -070086 if (size <= KMALLOC_MIN_SIZE)
87 return KMALLOC_SHIFT_LOW;
88
Christoph Lameter81819f02007-05-06 14:49:36 -070089 if (size > 64 && size <= 96)
90 return 1;
91 if (size > 128 && size <= 192)
92 return 2;
93 if (size <= 8) return 3;
94 if (size <= 16) return 4;
95 if (size <= 32) return 5;
96 if (size <= 64) return 6;
97 if (size <= 128) return 7;
98 if (size <= 256) return 8;
99 if (size <= 512) return 9;
100 if (size <= 1024) return 10;
101 if (size <= 2 * 1024) return 11;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700102/*
103 * The following is only needed to support architectures with a larger page
104 * size than 4k.
105 */
Christoph Lameter81819f02007-05-06 14:49:36 -0700106 if (size <= 4 * 1024) return 12;
107 if (size <= 8 * 1024) return 13;
108 if (size <= 16 * 1024) return 14;
109 if (size <= 32 * 1024) return 15;
110 if (size <= 64 * 1024) return 16;
111 if (size <= 128 * 1024) return 17;
112 if (size <= 256 * 1024) return 18;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700113 if (size <= 512 * 1024) return 19;
Christoph Lameter81819f02007-05-06 14:49:36 -0700114 if (size <= 1024 * 1024) return 20;
Christoph Lameter81819f02007-05-06 14:49:36 -0700115 if (size <= 2 * 1024 * 1024) return 21;
Christoph Lameter81819f02007-05-06 14:49:36 -0700116 return -1;
117
118/*
119 * What we really wanted to do and cannot do because of compiler issues is:
120 * int i;
121 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
122 * if (size <= (1 << i))
123 * return i;
124 */
125}
126
127/*
128 * Find the slab cache for a given combination of allocation flags and size.
129 *
130 * This ought to end up with a global pointer to the right cache
131 * in kmalloc_caches.
132 */
Christoph Lameteraa137f92007-08-31 00:48:45 -0700133static __always_inline struct kmem_cache *kmalloc_slab(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -0700134{
135 int index = kmalloc_index(size);
136
137 if (index == 0)
138 return NULL;
139
Christoph Lameter81819f02007-05-06 14:49:36 -0700140 return &kmalloc_caches[index];
141}
142
143#ifdef CONFIG_ZONE_DMA
144#define SLUB_DMA __GFP_DMA
145#else
146/* Disable DMA functionality */
Al Virod0469432007-07-20 16:18:06 +0100147#define SLUB_DMA (__force gfp_t)0
Christoph Lameter81819f02007-05-06 14:49:36 -0700148#endif
149
Paul Mundt6193a2f2007-07-15 23:38:22 -0700150void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
151void *__kmalloc(size_t size, gfp_t flags);
152
Christoph Lameteraa137f92007-08-31 00:48:45 -0700153static __always_inline void *kmalloc(size_t size, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -0700154{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700155 if (__builtin_constant_p(size)) {
156 if (size > PAGE_SIZE / 2)
157 return (void *)__get_free_pages(flags | __GFP_COMP,
158 get_order(size));
Christoph Lameter81819f02007-05-06 14:49:36 -0700159
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700160 if (!(flags & SLUB_DMA)) {
161 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700162
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700163 if (!s)
164 return ZERO_SIZE_PTR;
165
166 return kmem_cache_alloc(s, flags);
167 }
168 }
169 return __kmalloc(size, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -0700170}
171
Christoph Lameter81819f02007-05-06 14:49:36 -0700172#ifdef CONFIG_NUMA
Paul Mundt6193a2f2007-07-15 23:38:22 -0700173void *__kmalloc_node(size_t size, gfp_t flags, int node);
174void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700175
Christoph Lameteraa137f92007-08-31 00:48:45 -0700176static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
Christoph Lameter81819f02007-05-06 14:49:36 -0700177{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700178 if (__builtin_constant_p(size) &&
179 size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) {
180 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700181
182 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -0700183 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -0700184
185 return kmem_cache_alloc_node(s, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700186 }
187 return __kmalloc_node(size, flags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700188}
189#endif
190
191#endif /* _LINUX_SLUB_DEF_H */