blob: a06ee26193c24687552ba5f44ab700ed052dc832 [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
Christoph Lameterdfb4f092007-10-16 01:26:05 -070014struct kmem_cache_cpu {
15 void **freelist;
16 struct page *page;
17 int node;
Christoph Lameterb3fba8d2007-10-16 01:26:06 -070018 unsigned int offset;
Christoph Lameter42a9fdb2007-10-16 01:26:09 -070019 unsigned int objsize;
Christoph Lameter4c93c3552007-10-16 01:26:08 -070020};
Christoph Lameterdfb4f092007-10-16 01:26:05 -070021
Christoph Lameter81819f02007-05-06 14:49:36 -070022struct 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;
Christoph Lameter0c710012007-07-17 04:03:24 -070027#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -070028 struct list_head full;
Christoph Lameter0c710012007-07-17 04:03:24 -070029#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070030};
31
32/*
33 * Slab cache management.
34 */
35struct 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. */
Christoph Lameter4b356be2007-06-16 10:16:13 -070041 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -070042
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 */
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -070052 void (*ctor)(struct kmem_cache *, void *);
Christoph Lameter81819f02007-05-06 14:49:36 -070053 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 */
Christoph Lameter0c710012007-07-17 04:03:24 -070057#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -070058 struct kobject kobj; /* For sysfs */
Christoph Lameter0c710012007-07-17 04:03:24 -070059#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070060
61#ifdef CONFIG_NUMA
Christoph Lameter98246012008-01-07 23:20:26 -080062 /*
63 * Defragmentation by allocating from a remote node.
64 */
65 int remote_node_defrag_ratio;
Christoph Lameter81819f02007-05-06 14:49:36 -070066 struct kmem_cache_node *node[MAX_NUMNODES];
67#endif
Christoph Lameter4c93c3552007-10-16 01:26:08 -070068#ifdef CONFIG_SMP
69 struct kmem_cache_cpu *cpu_slab[NR_CPUS];
70#else
71 struct kmem_cache_cpu cpu_slab;
72#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070073};
74
75/*
76 * Kmalloc subsystem.
77 */
Christoph Lameter4b356be2007-06-16 10:16:13 -070078#if defined(ARCH_KMALLOC_MINALIGN) && ARCH_KMALLOC_MINALIGN > 8
79#define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
80#else
81#define KMALLOC_MIN_SIZE 8
82#endif
83
84#define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
Christoph Lameter81819f02007-05-06 14:49:36 -070085
Christoph Lameter81819f02007-05-06 14:49:36 -070086/*
87 * We keep the general caches in an array of slab caches that are used for
88 * 2^x bytes of allocations.
89 */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -070090extern struct kmem_cache kmalloc_caches[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -070091
92/*
93 * Sorry that the following has to be that ugly but some versions of GCC
94 * have trouble with constant propagation and loops.
95 */
Christoph Lameteraa137f92007-08-31 00:48:45 -070096static __always_inline int kmalloc_index(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -070097{
Christoph Lameter272c1d22007-06-08 13:46:49 -070098 if (!size)
99 return 0;
Christoph Lameter614410d2007-05-06 14:49:38 -0700100
Christoph Lameter4b356be2007-06-16 10:16:13 -0700101 if (size <= KMALLOC_MIN_SIZE)
102 return KMALLOC_SHIFT_LOW;
103
Christoph Lameter81819f02007-05-06 14:49:36 -0700104 if (size > 64 && size <= 96)
105 return 1;
106 if (size > 128 && size <= 192)
107 return 2;
108 if (size <= 8) return 3;
109 if (size <= 16) return 4;
110 if (size <= 32) return 5;
111 if (size <= 64) return 6;
112 if (size <= 128) return 7;
113 if (size <= 256) return 8;
114 if (size <= 512) return 9;
115 if (size <= 1024) return 10;
116 if (size <= 2 * 1024) return 11;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700117/*
118 * The following is only needed to support architectures with a larger page
119 * size than 4k.
120 */
Christoph Lameter81819f02007-05-06 14:49:36 -0700121 if (size <= 4 * 1024) return 12;
122 if (size <= 8 * 1024) return 13;
123 if (size <= 16 * 1024) return 14;
124 if (size <= 32 * 1024) return 15;
125 if (size <= 64 * 1024) return 16;
126 if (size <= 128 * 1024) return 17;
127 if (size <= 256 * 1024) return 18;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700128 if (size <= 512 * 1024) return 19;
Christoph Lameter81819f02007-05-06 14:49:36 -0700129 if (size <= 1024 * 1024) return 20;
Christoph Lameter81819f02007-05-06 14:49:36 -0700130 if (size <= 2 * 1024 * 1024) return 21;
Christoph Lameter81819f02007-05-06 14:49:36 -0700131 return -1;
132
133/*
134 * What we really wanted to do and cannot do because of compiler issues is:
135 * int i;
136 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
137 * if (size <= (1 << i))
138 * return i;
139 */
140}
141
142/*
143 * Find the slab cache for a given combination of allocation flags and size.
144 *
145 * This ought to end up with a global pointer to the right cache
146 * in kmalloc_caches.
147 */
Christoph Lameteraa137f92007-08-31 00:48:45 -0700148static __always_inline struct kmem_cache *kmalloc_slab(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -0700149{
150 int index = kmalloc_index(size);
151
152 if (index == 0)
153 return NULL;
154
Christoph Lameter81819f02007-05-06 14:49:36 -0700155 return &kmalloc_caches[index];
156}
157
158#ifdef CONFIG_ZONE_DMA
159#define SLUB_DMA __GFP_DMA
160#else
161/* Disable DMA functionality */
Al Virod0469432007-07-20 16:18:06 +0100162#define SLUB_DMA (__force gfp_t)0
Christoph Lameter81819f02007-05-06 14:49:36 -0700163#endif
164
Paul Mundt6193a2f2007-07-15 23:38:22 -0700165void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
166void *__kmalloc(size_t size, gfp_t flags);
167
Christoph Lameteraa137f92007-08-31 00:48:45 -0700168static __always_inline void *kmalloc(size_t size, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -0700169{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700170 if (__builtin_constant_p(size)) {
171 if (size > PAGE_SIZE / 2)
172 return (void *)__get_free_pages(flags | __GFP_COMP,
173 get_order(size));
Christoph Lameter81819f02007-05-06 14:49:36 -0700174
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700175 if (!(flags & SLUB_DMA)) {
176 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700177
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700178 if (!s)
179 return ZERO_SIZE_PTR;
180
181 return kmem_cache_alloc(s, flags);
182 }
183 }
184 return __kmalloc(size, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -0700185}
186
Christoph Lameter81819f02007-05-06 14:49:36 -0700187#ifdef CONFIG_NUMA
Paul Mundt6193a2f2007-07-15 23:38:22 -0700188void *__kmalloc_node(size_t size, gfp_t flags, int node);
189void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700190
Christoph Lameteraa137f92007-08-31 00:48:45 -0700191static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
Christoph Lameter81819f02007-05-06 14:49:36 -0700192{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700193 if (__builtin_constant_p(size) &&
194 size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) {
195 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700196
197 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -0700198 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -0700199
200 return kmem_cache_alloc_node(s, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700201 }
202 return __kmalloc_node(size, flags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700203}
204#endif
205
Christoph Lameter81819f02007-05-06 14:49:36 -0700206#endif /* _LINUX_SLUB_DEF_H */