blob: f74716b59ce233fc44a9816d567e32fe1bba495d [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 Lameter4c93c3552007-10-16 01:26:08 -070019};
Christoph Lameterdfb4f092007-10-16 01:26:05 -070020
Christoph Lameter81819f02007-05-06 14:49:36 -070021struct kmem_cache_node {
22 spinlock_t list_lock; /* Protect partial list and nr_partial */
23 unsigned long nr_partial;
24 atomic_long_t nr_slabs;
25 struct list_head partial;
Christoph Lameter0c710012007-07-17 04:03:24 -070026#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter643b1132007-05-06 14:49:42 -070027 struct list_head full;
Christoph Lameter0c710012007-07-17 04:03:24 -070028#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070029};
30
31/*
32 * Slab cache management.
33 */
34struct kmem_cache {
35 /* Used for retriving partial slabs etc */
36 unsigned long flags;
37 int size; /* The size of an object including meta data */
38 int objsize; /* The size of an object without meta data */
39 int offset; /* Free pointer offset. */
Christoph Lameter4b356be2007-06-16 10:16:13 -070040 int order;
Christoph Lameter81819f02007-05-06 14:49:36 -070041
42 /*
43 * Avoid an extra cache line for UP, SMP and for the node local to
44 * struct kmem_cache.
45 */
46 struct kmem_cache_node local_node;
47
48 /* Allocation and freeing of slabs */
49 int objects; /* Number of objects in slab */
50 int refcount; /* Refcount for slab cache destroy */
51 void (*ctor)(void *, struct kmem_cache *, unsigned long);
Christoph Lameter81819f02007-05-06 14:49:36 -070052 int inuse; /* Offset to metadata */
53 int align; /* Alignment */
54 const char *name; /* Name (only for display!) */
55 struct list_head list; /* List of slab caches */
Christoph Lameter0c710012007-07-17 04:03:24 -070056#ifdef CONFIG_SLUB_DEBUG
Christoph Lameter81819f02007-05-06 14:49:36 -070057 struct kobject kobj; /* For sysfs */
Christoph Lameter0c710012007-07-17 04:03:24 -070058#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070059
60#ifdef CONFIG_NUMA
61 int defrag_ratio;
62 struct kmem_cache_node *node[MAX_NUMNODES];
63#endif
Christoph Lameter4c93c3552007-10-16 01:26:08 -070064#ifdef CONFIG_SMP
65 struct kmem_cache_cpu *cpu_slab[NR_CPUS];
66#else
67 struct kmem_cache_cpu cpu_slab;
68#endif
Christoph Lameter81819f02007-05-06 14:49:36 -070069};
70
71/*
72 * Kmalloc subsystem.
73 */
Christoph Lameter4b356be2007-06-16 10:16:13 -070074#if defined(ARCH_KMALLOC_MINALIGN) && ARCH_KMALLOC_MINALIGN > 8
75#define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
76#else
77#define KMALLOC_MIN_SIZE 8
78#endif
79
80#define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
Christoph Lameter81819f02007-05-06 14:49:36 -070081
Christoph Lameter81819f02007-05-06 14:49:36 -070082/*
83 * We keep the general caches in an array of slab caches that are used for
84 * 2^x bytes of allocations.
85 */
Christoph Lameteraadb4bc2007-10-16 01:24:38 -070086extern struct kmem_cache kmalloc_caches[PAGE_SHIFT];
Christoph Lameter81819f02007-05-06 14:49:36 -070087
88/*
89 * Sorry that the following has to be that ugly but some versions of GCC
90 * have trouble with constant propagation and loops.
91 */
Christoph Lameteraa137f92007-08-31 00:48:45 -070092static __always_inline int kmalloc_index(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -070093{
Christoph Lameter272c1d22007-06-08 13:46:49 -070094 if (!size)
95 return 0;
Christoph Lameter614410d2007-05-06 14:49:38 -070096
Christoph Lameter4b356be2007-06-16 10:16:13 -070097 if (size <= KMALLOC_MIN_SIZE)
98 return KMALLOC_SHIFT_LOW;
99
Christoph Lameter81819f02007-05-06 14:49:36 -0700100 if (size > 64 && size <= 96)
101 return 1;
102 if (size > 128 && size <= 192)
103 return 2;
104 if (size <= 8) return 3;
105 if (size <= 16) return 4;
106 if (size <= 32) return 5;
107 if (size <= 64) return 6;
108 if (size <= 128) return 7;
109 if (size <= 256) return 8;
110 if (size <= 512) return 9;
111 if (size <= 1024) return 10;
112 if (size <= 2 * 1024) return 11;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700113/*
114 * The following is only needed to support architectures with a larger page
115 * size than 4k.
116 */
Christoph Lameter81819f02007-05-06 14:49:36 -0700117 if (size <= 4 * 1024) return 12;
118 if (size <= 8 * 1024) return 13;
119 if (size <= 16 * 1024) return 14;
120 if (size <= 32 * 1024) return 15;
121 if (size <= 64 * 1024) return 16;
122 if (size <= 128 * 1024) return 17;
123 if (size <= 256 * 1024) return 18;
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700124 if (size <= 512 * 1024) return 19;
Christoph Lameter81819f02007-05-06 14:49:36 -0700125 if (size <= 1024 * 1024) return 20;
Christoph Lameter81819f02007-05-06 14:49:36 -0700126 if (size <= 2 * 1024 * 1024) return 21;
Christoph Lameter81819f02007-05-06 14:49:36 -0700127 return -1;
128
129/*
130 * What we really wanted to do and cannot do because of compiler issues is:
131 * int i;
132 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
133 * if (size <= (1 << i))
134 * return i;
135 */
136}
137
138/*
139 * Find the slab cache for a given combination of allocation flags and size.
140 *
141 * This ought to end up with a global pointer to the right cache
142 * in kmalloc_caches.
143 */
Christoph Lameteraa137f92007-08-31 00:48:45 -0700144static __always_inline struct kmem_cache *kmalloc_slab(size_t size)
Christoph Lameter81819f02007-05-06 14:49:36 -0700145{
146 int index = kmalloc_index(size);
147
148 if (index == 0)
149 return NULL;
150
Christoph Lameter81819f02007-05-06 14:49:36 -0700151 return &kmalloc_caches[index];
152}
153
154#ifdef CONFIG_ZONE_DMA
155#define SLUB_DMA __GFP_DMA
156#else
157/* Disable DMA functionality */
Al Virod0469432007-07-20 16:18:06 +0100158#define SLUB_DMA (__force gfp_t)0
Christoph Lameter81819f02007-05-06 14:49:36 -0700159#endif
160
Paul Mundt6193a2f2007-07-15 23:38:22 -0700161void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
162void *__kmalloc(size_t size, gfp_t flags);
163
Christoph Lameteraa137f92007-08-31 00:48:45 -0700164static __always_inline void *kmalloc(size_t size, gfp_t flags)
Christoph Lameter81819f02007-05-06 14:49:36 -0700165{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700166 if (__builtin_constant_p(size)) {
167 if (size > PAGE_SIZE / 2)
168 return (void *)__get_free_pages(flags | __GFP_COMP,
169 get_order(size));
Christoph Lameter81819f02007-05-06 14:49:36 -0700170
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700171 if (!(flags & SLUB_DMA)) {
172 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700173
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700174 if (!s)
175 return ZERO_SIZE_PTR;
176
177 return kmem_cache_alloc(s, flags);
178 }
179 }
180 return __kmalloc(size, flags);
Christoph Lameter81819f02007-05-06 14:49:36 -0700181}
182
Christoph Lameter81819f02007-05-06 14:49:36 -0700183#ifdef CONFIG_NUMA
Paul Mundt6193a2f2007-07-15 23:38:22 -0700184void *__kmalloc_node(size_t size, gfp_t flags, int node);
185void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700186
Christoph Lameteraa137f92007-08-31 00:48:45 -0700187static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
Christoph Lameter81819f02007-05-06 14:49:36 -0700188{
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700189 if (__builtin_constant_p(size) &&
190 size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) {
191 struct kmem_cache *s = kmalloc_slab(size);
Christoph Lameter81819f02007-05-06 14:49:36 -0700192
193 if (!s)
Christoph Lameter272c1d22007-06-08 13:46:49 -0700194 return ZERO_SIZE_PTR;
Christoph Lameter81819f02007-05-06 14:49:36 -0700195
196 return kmem_cache_alloc_node(s, flags, node);
Christoph Lameteraadb4bc2007-10-16 01:24:38 -0700197 }
198 return __kmalloc_node(size, flags, node);
Christoph Lameter81819f02007-05-06 14:49:36 -0700199}
200#endif
201
202#endif /* _LINUX_SLUB_DEF_H */