diff options
Diffstat (limited to 'mm/slob.c')
-rw-r--r-- | mm/slob.c | 538 |
1 files changed, 385 insertions, 153 deletions
@@ -3,57 +3,159 @@ | |||
3 | * | 3 | * |
4 | * Matt Mackall <mpm@selenic.com> 12/30/03 | 4 | * Matt Mackall <mpm@selenic.com> 12/30/03 |
5 | * | 5 | * |
6 | * NUMA support by Paul Mundt, 2007. | ||
7 | * | ||
6 | * How SLOB works: | 8 | * How SLOB works: |
7 | * | 9 | * |
8 | * The core of SLOB is a traditional K&R style heap allocator, with | 10 | * The core of SLOB is a traditional K&R style heap allocator, with |
9 | * support for returning aligned objects. The granularity of this | 11 | * support for returning aligned objects. The granularity of this |
10 | * allocator is 8 bytes on x86, though it's perhaps possible to reduce | 12 | * allocator is as little as 2 bytes, however typically most architectures |
11 | * this to 4 if it's deemed worth the effort. The slob heap is a | 13 | * will require 4 bytes on 32-bit and 8 bytes on 64-bit. |
12 | * singly-linked list of pages from __get_free_page, grown on demand | 14 | * |
13 | * and allocation from the heap is currently first-fit. | 15 | * The slob heap is a linked list of pages from alloc_pages(), and |
16 | * within each page, there is a singly-linked list of free blocks (slob_t). | ||
17 | * The heap is grown on demand and allocation from the heap is currently | ||
18 | * first-fit. | ||
14 | * | 19 | * |
15 | * Above this is an implementation of kmalloc/kfree. Blocks returned | 20 | * Above this is an implementation of kmalloc/kfree. Blocks returned |
16 | * from kmalloc are 8-byte aligned and prepended with a 8-byte header. | 21 | * from kmalloc are prepended with a 4-byte header with the kmalloc size. |
17 | * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls | 22 | * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls |
18 | * __get_free_pages directly so that it can return page-aligned blocks | 23 | * alloc_pages() directly, allocating compound pages so the page order |
19 | * and keeps a linked list of such pages and their orders. These | 24 | * does not have to be separately tracked, and also stores the exact |
20 | * objects are detected in kfree() by their page alignment. | 25 | * allocation size in page->private so that it can be used to accurately |
26 | * provide ksize(). These objects are detected in kfree() because slob_page() | ||
27 | * is false for them. | ||
21 | * | 28 | * |
22 | * SLAB is emulated on top of SLOB by simply calling constructors and | 29 | * SLAB is emulated on top of SLOB by simply calling constructors and |
23 | * destructors for every SLAB allocation. Objects are returned with | 30 | * destructors for every SLAB allocation. Objects are returned with the |
24 | * the 8-byte alignment unless the SLAB_HWCACHE_ALIGN flag is | 31 | * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which |
25 | * set, in which case the low-level allocator will fragment blocks to | 32 | * case the low-level allocator will fragment blocks to create the proper |
26 | * create the proper alignment. Again, objects of page-size or greater | 33 | * alignment. Again, objects of page-size or greater are allocated by |
27 | * are allocated by calling __get_free_pages. As SLAB objects know | 34 | * calling alloc_pages(). As SLAB objects know their size, no separate |
28 | * their size, no separate size bookkeeping is necessary and there is | 35 | * size bookkeeping is necessary and there is essentially no allocation |
29 | * essentially no allocation space overhead. | 36 | * space overhead, and compound pages aren't needed for multi-page |
37 | * allocations. | ||
38 | * | ||
39 | * NUMA support in SLOB is fairly simplistic, pushing most of the real | ||
40 | * logic down to the page allocator, and simply doing the node accounting | ||
41 | * on the upper levels. In the event that a node id is explicitly | ||
42 | * provided, alloc_pages_node() with the specified node id is used | ||
43 | * instead. The common case (or when the node id isn't explicitly provided) | ||
44 | * will default to the current node, as per numa_node_id(). | ||
45 | * | ||
46 | * Node aware pages are still inserted in to the global freelist, and | ||
47 | * these are scanned for by matching against the node id encoded in the | ||
48 | * page flags. As a result, block allocations that can be satisfied from | ||
49 | * the freelist will only be done so on pages residing on the same node, | ||
50 | * in order to prevent random node placement. | ||
30 | */ | 51 | */ |
31 | 52 | ||
53 | #include <linux/kernel.h> | ||
32 | #include <linux/slab.h> | 54 | #include <linux/slab.h> |
33 | #include <linux/mm.h> | 55 | #include <linux/mm.h> |
34 | #include <linux/cache.h> | 56 | #include <linux/cache.h> |
35 | #include <linux/init.h> | 57 | #include <linux/init.h> |
36 | #include <linux/module.h> | 58 | #include <linux/module.h> |
37 | #include <linux/timer.h> | ||
38 | #include <linux/rcupdate.h> | 59 | #include <linux/rcupdate.h> |
60 | #include <linux/list.h> | ||
61 | #include <asm/atomic.h> | ||
62 | |||
63 | /* | ||
64 | * slob_block has a field 'units', which indicates size of block if +ve, | ||
65 | * or offset of next block if -ve (in SLOB_UNITs). | ||
66 | * | ||
67 | * Free blocks of size 1 unit simply contain the offset of the next block. | ||
68 | * Those with larger size contain their size in the first SLOB_UNIT of | ||
69 | * memory, and the offset of the next free block in the second SLOB_UNIT. | ||
70 | */ | ||
71 | #if PAGE_SIZE <= (32767 * 2) | ||
72 | typedef s16 slobidx_t; | ||
73 | #else | ||
74 | typedef s32 slobidx_t; | ||
75 | #endif | ||
39 | 76 | ||
40 | struct slob_block { | 77 | struct slob_block { |
41 | int units; | 78 | slobidx_t units; |
42 | struct slob_block *next; | ||
43 | }; | 79 | }; |
44 | typedef struct slob_block slob_t; | 80 | typedef struct slob_block slob_t; |
45 | 81 | ||
82 | /* | ||
83 | * We use struct page fields to manage some slob allocation aspects, | ||
84 | * however to avoid the horrible mess in include/linux/mm_types.h, we'll | ||
85 | * just define our own struct page type variant here. | ||
86 | */ | ||
87 | struct slob_page { | ||
88 | union { | ||
89 | struct { | ||
90 | unsigned long flags; /* mandatory */ | ||
91 | atomic_t _count; /* mandatory */ | ||
92 | slobidx_t units; /* free units left in page */ | ||
93 | unsigned long pad[2]; | ||
94 | slob_t *free; /* first free slob_t in page */ | ||
95 | struct list_head list; /* linked list of free pages */ | ||
96 | }; | ||
97 | struct page page; | ||
98 | }; | ||
99 | }; | ||
100 | static inline void struct_slob_page_wrong_size(void) | ||
101 | { BUILD_BUG_ON(sizeof(struct slob_page) != sizeof(struct page)); } | ||
102 | |||
103 | /* | ||
104 | * free_slob_page: call before a slob_page is returned to the page allocator. | ||
105 | */ | ||
106 | static inline void free_slob_page(struct slob_page *sp) | ||
107 | { | ||
108 | reset_page_mapcount(&sp->page); | ||
109 | sp->page.mapping = NULL; | ||
110 | } | ||
111 | |||
112 | /* | ||
113 | * All (partially) free slob pages go on this list. | ||
114 | */ | ||
115 | static LIST_HEAD(free_slob_pages); | ||
116 | |||
117 | /* | ||
118 | * slob_page: True for all slob pages (false for bigblock pages) | ||
119 | */ | ||
120 | static inline int slob_page(struct slob_page *sp) | ||
121 | { | ||
122 | return test_bit(PG_active, &sp->flags); | ||
123 | } | ||
124 | |||
125 | static inline void set_slob_page(struct slob_page *sp) | ||
126 | { | ||
127 | __set_bit(PG_active, &sp->flags); | ||
128 | } | ||
129 | |||
130 | static inline void clear_slob_page(struct slob_page *sp) | ||
131 | { | ||
132 | __clear_bit(PG_active, &sp->flags); | ||
133 | } | ||
134 | |||
135 | /* | ||
136 | * slob_page_free: true for pages on free_slob_pages list. | ||
137 | */ | ||
138 | static inline int slob_page_free(struct slob_page *sp) | ||
139 | { | ||
140 | return test_bit(PG_private, &sp->flags); | ||
141 | } | ||
142 | |||
143 | static inline void set_slob_page_free(struct slob_page *sp) | ||
144 | { | ||
145 | list_add(&sp->list, &free_slob_pages); | ||
146 | __set_bit(PG_private, &sp->flags); | ||
147 | } | ||
148 | |||
149 | static inline void clear_slob_page_free(struct slob_page *sp) | ||
150 | { | ||
151 | list_del(&sp->list); | ||
152 | __clear_bit(PG_private, &sp->flags); | ||
153 | } | ||
154 | |||
46 | #define SLOB_UNIT sizeof(slob_t) | 155 | #define SLOB_UNIT sizeof(slob_t) |
47 | #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT) | 156 | #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT) |
48 | #define SLOB_ALIGN L1_CACHE_BYTES | 157 | #define SLOB_ALIGN L1_CACHE_BYTES |
49 | 158 | ||
50 | struct bigblock { | ||
51 | int order; | ||
52 | void *pages; | ||
53 | struct bigblock *next; | ||
54 | }; | ||
55 | typedef struct bigblock bigblock_t; | ||
56 | |||
57 | /* | 159 | /* |
58 | * struct slob_rcu is inserted at the tail of allocated slob blocks, which | 160 | * struct slob_rcu is inserted at the tail of allocated slob blocks, which |
59 | * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free | 161 | * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free |
@@ -64,133 +166,285 @@ struct slob_rcu { | |||
64 | int size; | 166 | int size; |
65 | }; | 167 | }; |
66 | 168 | ||
67 | static slob_t arena = { .next = &arena, .units = 1 }; | 169 | /* |
68 | static slob_t *slobfree = &arena; | 170 | * slob_lock protects all slob allocator structures. |
69 | static bigblock_t *bigblocks; | 171 | */ |
70 | static DEFINE_SPINLOCK(slob_lock); | 172 | static DEFINE_SPINLOCK(slob_lock); |
71 | static DEFINE_SPINLOCK(block_lock); | ||
72 | 173 | ||
73 | static void slob_free(void *b, int size); | 174 | /* |
74 | static void slob_timer_cbk(void); | 175 | * Encode the given size and next info into a free slob block s. |
176 | */ | ||
177 | static void set_slob(slob_t *s, slobidx_t size, slob_t *next) | ||
178 | { | ||
179 | slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK); | ||
180 | slobidx_t offset = next - base; | ||
75 | 181 | ||
182 | if (size > 1) { | ||
183 | s[0].units = size; | ||
184 | s[1].units = offset; | ||
185 | } else | ||
186 | s[0].units = -offset; | ||
187 | } | ||
76 | 188 | ||
77 | static void *slob_alloc(size_t size, gfp_t gfp, int align) | 189 | /* |
190 | * Return the size of a slob block. | ||
191 | */ | ||
192 | static slobidx_t slob_units(slob_t *s) | ||
193 | { | ||
194 | if (s->units > 0) | ||
195 | return s->units; | ||
196 | return 1; | ||
197 | } | ||
198 | |||
199 | /* | ||
200 | * Return the next free slob block pointer after this one. | ||
201 | */ | ||
202 | static slob_t *slob_next(slob_t *s) | ||
203 | { | ||
204 | slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK); | ||
205 | slobidx_t next; | ||
206 | |||
207 | if (s[0].units < 0) | ||
208 | next = -s[0].units; | ||
209 | else | ||
210 | next = s[1].units; | ||
211 | return base+next; | ||
212 | } | ||
213 | |||
214 | /* | ||
215 | * Returns true if s is the last free block in its page. | ||
216 | */ | ||
217 | static int slob_last(slob_t *s) | ||
218 | { | ||
219 | return !((unsigned long)slob_next(s) & ~PAGE_MASK); | ||
220 | } | ||
221 | |||
222 | static void *slob_new_page(gfp_t gfp, int order, int node) | ||
223 | { | ||
224 | void *page; | ||
225 | |||
226 | #ifdef CONFIG_NUMA | ||
227 | if (node != -1) | ||
228 | page = alloc_pages_node(node, gfp, order); | ||
229 | else | ||
230 | #endif | ||
231 | page = alloc_pages(gfp, order); | ||
232 | |||
233 | if (!page) | ||
234 | return NULL; | ||
235 | |||
236 | return page_address(page); | ||
237 | } | ||
238 | |||
239 | /* | ||
240 | * Allocate a slob block within a given slob_page sp. | ||
241 | */ | ||
242 | static void *slob_page_alloc(struct slob_page *sp, size_t size, int align) | ||
78 | { | 243 | { |
79 | slob_t *prev, *cur, *aligned = 0; | 244 | slob_t *prev, *cur, *aligned = 0; |
80 | int delta = 0, units = SLOB_UNITS(size); | 245 | int delta = 0, units = SLOB_UNITS(size); |
81 | unsigned long flags; | ||
82 | 246 | ||
83 | spin_lock_irqsave(&slob_lock, flags); | 247 | for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) { |
84 | prev = slobfree; | 248 | slobidx_t avail = slob_units(cur); |
85 | for (cur = prev->next; ; prev = cur, cur = cur->next) { | 249 | |
86 | if (align) { | 250 | if (align) { |
87 | aligned = (slob_t *)ALIGN((unsigned long)cur, align); | 251 | aligned = (slob_t *)ALIGN((unsigned long)cur, align); |
88 | delta = aligned - cur; | 252 | delta = aligned - cur; |
89 | } | 253 | } |
90 | if (cur->units >= units + delta) { /* room enough? */ | 254 | if (avail >= units + delta) { /* room enough? */ |
255 | slob_t *next; | ||
256 | |||
91 | if (delta) { /* need to fragment head to align? */ | 257 | if (delta) { /* need to fragment head to align? */ |
92 | aligned->units = cur->units - delta; | 258 | next = slob_next(cur); |
93 | aligned->next = cur->next; | 259 | set_slob(aligned, avail - delta, next); |
94 | cur->next = aligned; | 260 | set_slob(cur, delta, aligned); |
95 | cur->units = delta; | ||
96 | prev = cur; | 261 | prev = cur; |
97 | cur = aligned; | 262 | cur = aligned; |
263 | avail = slob_units(cur); | ||
98 | } | 264 | } |
99 | 265 | ||
100 | if (cur->units == units) /* exact fit? */ | 266 | next = slob_next(cur); |
101 | prev->next = cur->next; /* unlink */ | 267 | if (avail == units) { /* exact fit? unlink. */ |
102 | else { /* fragment */ | 268 | if (prev) |
103 | prev->next = cur + units; | 269 | set_slob(prev, slob_units(prev), next); |
104 | prev->next->units = cur->units - units; | 270 | else |
105 | prev->next->next = cur->next; | 271 | sp->free = next; |
106 | cur->units = units; | 272 | } else { /* fragment */ |
273 | if (prev) | ||
274 | set_slob(prev, slob_units(prev), cur + units); | ||
275 | else | ||
276 | sp->free = cur + units; | ||
277 | set_slob(cur + units, avail - units, next); | ||
107 | } | 278 | } |
108 | 279 | ||
109 | slobfree = prev; | 280 | sp->units -= units; |
110 | spin_unlock_irqrestore(&slob_lock, flags); | 281 | if (!sp->units) |
282 | clear_slob_page_free(sp); | ||
111 | return cur; | 283 | return cur; |
112 | } | 284 | } |
113 | if (cur == slobfree) { | 285 | if (slob_last(cur)) |
114 | spin_unlock_irqrestore(&slob_lock, flags); | 286 | return NULL; |
115 | 287 | } | |
116 | if (size == PAGE_SIZE) /* trying to shrink arena? */ | 288 | } |
117 | return 0; | ||
118 | 289 | ||
119 | cur = (slob_t *)__get_free_page(gfp); | 290 | /* |
120 | if (!cur) | 291 | * slob_alloc: entry point into the slob allocator. |
121 | return 0; | 292 | */ |
293 | static void *slob_alloc(size_t size, gfp_t gfp, int align, int node) | ||
294 | { | ||
295 | struct slob_page *sp; | ||
296 | slob_t *b = NULL; | ||
297 | unsigned long flags; | ||
122 | 298 | ||
123 | slob_free(cur, PAGE_SIZE); | 299 | spin_lock_irqsave(&slob_lock, flags); |
124 | spin_lock_irqsave(&slob_lock, flags); | 300 | /* Iterate through each partially free page, try to find room */ |
125 | cur = slobfree; | 301 | list_for_each_entry(sp, &free_slob_pages, list) { |
302 | #ifdef CONFIG_NUMA | ||
303 | /* | ||
304 | * If there's a node specification, search for a partial | ||
305 | * page with a matching node id in the freelist. | ||
306 | */ | ||
307 | if (node != -1 && page_to_nid(&sp->page) != node) | ||
308 | continue; | ||
309 | #endif | ||
310 | |||
311 | if (sp->units >= SLOB_UNITS(size)) { | ||
312 | b = slob_page_alloc(sp, size, align); | ||
313 | if (b) | ||
314 | break; | ||
126 | } | 315 | } |
127 | } | 316 | } |
317 | spin_unlock_irqrestore(&slob_lock, flags); | ||
318 | |||
319 | /* Not enough space: must allocate a new page */ | ||
320 | if (!b) { | ||
321 | b = slob_new_page(gfp, 0, node); | ||
322 | if (!b) | ||
323 | return 0; | ||
324 | sp = (struct slob_page *)virt_to_page(b); | ||
325 | set_slob_page(sp); | ||
326 | |||
327 | spin_lock_irqsave(&slob_lock, flags); | ||
328 | sp->units = SLOB_UNITS(PAGE_SIZE); | ||
329 | sp->free = b; | ||
330 | INIT_LIST_HEAD(&sp->list); | ||
331 | set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE)); | ||
332 | set_slob_page_free(sp); | ||
333 | b = slob_page_alloc(sp, size, align); | ||
334 | BUG_ON(!b); | ||
335 | spin_unlock_irqrestore(&slob_lock, flags); | ||
336 | } | ||
337 | return b; | ||
128 | } | 338 | } |
129 | 339 | ||
340 | /* | ||
341 | * slob_free: entry point into the slob allocator. | ||
342 | */ | ||
130 | static void slob_free(void *block, int size) | 343 | static void slob_free(void *block, int size) |
131 | { | 344 | { |
132 | slob_t *cur, *b = (slob_t *)block; | 345 | struct slob_page *sp; |
346 | slob_t *prev, *next, *b = (slob_t *)block; | ||
347 | slobidx_t units; | ||
133 | unsigned long flags; | 348 | unsigned long flags; |
134 | 349 | ||
135 | if (!block) | 350 | if (!block) |
136 | return; | 351 | return; |
352 | BUG_ON(!size); | ||
137 | 353 | ||
138 | if (size) | 354 | sp = (struct slob_page *)virt_to_page(block); |
139 | b->units = SLOB_UNITS(size); | 355 | units = SLOB_UNITS(size); |
140 | 356 | ||
141 | /* Find reinsertion point */ | ||
142 | spin_lock_irqsave(&slob_lock, flags); | 357 | spin_lock_irqsave(&slob_lock, flags); |
143 | for (cur = slobfree; !(b > cur && b < cur->next); cur = cur->next) | ||
144 | if (cur >= cur->next && (b > cur || b < cur->next)) | ||
145 | break; | ||
146 | 358 | ||
147 | if (b + b->units == cur->next) { | 359 | if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) { |
148 | b->units += cur->next->units; | 360 | /* Go directly to page allocator. Do not pass slob allocator */ |
149 | b->next = cur->next->next; | 361 | if (slob_page_free(sp)) |
150 | } else | 362 | clear_slob_page_free(sp); |
151 | b->next = cur->next; | 363 | clear_slob_page(sp); |
364 | free_slob_page(sp); | ||
365 | free_page((unsigned long)b); | ||
366 | goto out; | ||
367 | } | ||
152 | 368 | ||
153 | if (cur + cur->units == b) { | 369 | if (!slob_page_free(sp)) { |
154 | cur->units += b->units; | 370 | /* This slob page is about to become partially free. Easy! */ |
155 | cur->next = b->next; | 371 | sp->units = units; |
156 | } else | 372 | sp->free = b; |
157 | cur->next = b; | 373 | set_slob(b, units, |
374 | (void *)((unsigned long)(b + | ||
375 | SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK)); | ||
376 | set_slob_page_free(sp); | ||
377 | goto out; | ||
378 | } | ||
158 | 379 | ||
159 | slobfree = cur; | 380 | /* |
381 | * Otherwise the page is already partially free, so find reinsertion | ||
382 | * point. | ||
383 | */ | ||
384 | sp->units += units; | ||
160 | 385 | ||
386 | if (b < sp->free) { | ||
387 | set_slob(b, units, sp->free); | ||
388 | sp->free = b; | ||
389 | } else { | ||
390 | prev = sp->free; | ||
391 | next = slob_next(prev); | ||
392 | while (b > next) { | ||
393 | prev = next; | ||
394 | next = slob_next(prev); | ||
395 | } | ||
396 | |||
397 | if (!slob_last(prev) && b + units == next) { | ||
398 | units += slob_units(next); | ||
399 | set_slob(b, units, slob_next(next)); | ||
400 | } else | ||
401 | set_slob(b, units, next); | ||
402 | |||
403 | if (prev + slob_units(prev) == b) { | ||
404 | units = slob_units(b) + slob_units(prev); | ||
405 | set_slob(prev, units, slob_next(b)); | ||
406 | } else | ||
407 | set_slob(prev, slob_units(prev), b); | ||
408 | } | ||
409 | out: | ||
161 | spin_unlock_irqrestore(&slob_lock, flags); | 410 | spin_unlock_irqrestore(&slob_lock, flags); |
162 | } | 411 | } |
163 | 412 | ||
164 | void *__kmalloc(size_t size, gfp_t gfp) | 413 | /* |
165 | { | 414 | * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend. |
166 | slob_t *m; | 415 | */ |
167 | bigblock_t *bb; | ||
168 | unsigned long flags; | ||
169 | 416 | ||
170 | if (size < PAGE_SIZE - SLOB_UNIT) { | 417 | #ifndef ARCH_KMALLOC_MINALIGN |
171 | m = slob_alloc(size + SLOB_UNIT, gfp, 0); | 418 | #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long) |
172 | return m ? (void *)(m + 1) : 0; | 419 | #endif |
173 | } | ||
174 | 420 | ||
175 | bb = slob_alloc(sizeof(bigblock_t), gfp, 0); | 421 | #ifndef ARCH_SLAB_MINALIGN |
176 | if (!bb) | 422 | #define ARCH_SLAB_MINALIGN __alignof__(unsigned long) |
177 | return 0; | 423 | #endif |
178 | 424 | ||
179 | bb->order = get_order(size); | 425 | void *__kmalloc_node(size_t size, gfp_t gfp, int node) |
180 | bb->pages = (void *)__get_free_pages(gfp, bb->order); | 426 | { |
427 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); | ||
428 | |||
429 | if (size < PAGE_SIZE - align) { | ||
430 | unsigned int *m; | ||
431 | m = slob_alloc(size + align, gfp, align, node); | ||
432 | if (m) | ||
433 | *m = size; | ||
434 | return (void *)m + align; | ||
435 | } else { | ||
436 | void *ret; | ||
181 | 437 | ||
182 | if (bb->pages) { | 438 | ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node); |
183 | spin_lock_irqsave(&block_lock, flags); | 439 | if (ret) { |
184 | bb->next = bigblocks; | 440 | struct page *page; |
185 | bigblocks = bb; | 441 | page = virt_to_page(ret); |
186 | spin_unlock_irqrestore(&block_lock, flags); | 442 | page->private = size; |
187 | return bb->pages; | 443 | } |
444 | return ret; | ||
188 | } | 445 | } |
189 | |||
190 | slob_free(bb, sizeof(bigblock_t)); | ||
191 | return 0; | ||
192 | } | 446 | } |
193 | EXPORT_SYMBOL(__kmalloc); | 447 | EXPORT_SYMBOL(__kmalloc_node); |
194 | 448 | ||
195 | /** | 449 | /** |
196 | * krealloc - reallocate memory. The contents will remain unchanged. | 450 | * krealloc - reallocate memory. The contents will remain unchanged. |
@@ -227,52 +481,34 @@ EXPORT_SYMBOL(krealloc); | |||
227 | 481 | ||
228 | void kfree(const void *block) | 482 | void kfree(const void *block) |
229 | { | 483 | { |
230 | bigblock_t *bb, **last = &bigblocks; | 484 | struct slob_page *sp; |
231 | unsigned long flags; | ||
232 | 485 | ||
233 | if (!block) | 486 | if (!block) |
234 | return; | 487 | return; |
235 | 488 | ||
236 | if (!((unsigned long)block & (PAGE_SIZE-1))) { | 489 | sp = (struct slob_page *)virt_to_page(block); |
237 | /* might be on the big block list */ | 490 | if (slob_page(sp)) { |
238 | spin_lock_irqsave(&block_lock, flags); | 491 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
239 | for (bb = bigblocks; bb; last = &bb->next, bb = bb->next) { | 492 | unsigned int *m = (unsigned int *)(block - align); |
240 | if (bb->pages == block) { | 493 | slob_free(m, *m + align); |
241 | *last = bb->next; | 494 | } else |
242 | spin_unlock_irqrestore(&block_lock, flags); | 495 | put_page(&sp->page); |
243 | free_pages((unsigned long)block, bb->order); | ||
244 | slob_free(bb, sizeof(bigblock_t)); | ||
245 | return; | ||
246 | } | ||
247 | } | ||
248 | spin_unlock_irqrestore(&block_lock, flags); | ||
249 | } | ||
250 | |||
251 | slob_free((slob_t *)block - 1, 0); | ||
252 | return; | ||
253 | } | 496 | } |
254 | |||
255 | EXPORT_SYMBOL(kfree); | 497 | EXPORT_SYMBOL(kfree); |
256 | 498 | ||
499 | /* can't use ksize for kmem_cache_alloc memory, only kmalloc */ | ||
257 | size_t ksize(const void *block) | 500 | size_t ksize(const void *block) |
258 | { | 501 | { |
259 | bigblock_t *bb; | 502 | struct slob_page *sp; |
260 | unsigned long flags; | ||
261 | 503 | ||
262 | if (!block) | 504 | if (!block) |
263 | return 0; | 505 | return 0; |
264 | 506 | ||
265 | if (!((unsigned long)block & (PAGE_SIZE-1))) { | 507 | sp = (struct slob_page *)virt_to_page(block); |
266 | spin_lock_irqsave(&block_lock, flags); | 508 | if (slob_page(sp)) |
267 | for (bb = bigblocks; bb; bb = bb->next) | 509 | return ((slob_t *)block - 1)->units + SLOB_UNIT; |
268 | if (bb->pages == block) { | 510 | else |
269 | spin_unlock_irqrestore(&slob_lock, flags); | 511 | return sp->page.private; |
270 | return PAGE_SIZE << bb->order; | ||
271 | } | ||
272 | spin_unlock_irqrestore(&block_lock, flags); | ||
273 | } | ||
274 | |||
275 | return ((slob_t *)block - 1)->units * SLOB_UNIT; | ||
276 | } | 512 | } |
277 | 513 | ||
278 | struct kmem_cache { | 514 | struct kmem_cache { |
@@ -289,7 +525,7 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, | |||
289 | { | 525 | { |
290 | struct kmem_cache *c; | 526 | struct kmem_cache *c; |
291 | 527 | ||
292 | c = slob_alloc(sizeof(struct kmem_cache), flags, 0); | 528 | c = slob_alloc(sizeof(struct kmem_cache), flags, 0, -1); |
293 | 529 | ||
294 | if (c) { | 530 | if (c) { |
295 | c->name = name; | 531 | c->name = name; |
@@ -302,6 +538,8 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, | |||
302 | c->ctor = ctor; | 538 | c->ctor = ctor; |
303 | /* ignore alignment unless it's forced */ | 539 | /* ignore alignment unless it's forced */ |
304 | c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0; | 540 | c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0; |
541 | if (c->align < ARCH_SLAB_MINALIGN) | ||
542 | c->align = ARCH_SLAB_MINALIGN; | ||
305 | if (c->align < align) | 543 | if (c->align < align) |
306 | c->align = align; | 544 | c->align = align; |
307 | } else if (flags & SLAB_PANIC) | 545 | } else if (flags & SLAB_PANIC) |
@@ -317,21 +555,21 @@ void kmem_cache_destroy(struct kmem_cache *c) | |||
317 | } | 555 | } |
318 | EXPORT_SYMBOL(kmem_cache_destroy); | 556 | EXPORT_SYMBOL(kmem_cache_destroy); |
319 | 557 | ||
320 | void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags) | 558 | void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node) |
321 | { | 559 | { |
322 | void *b; | 560 | void *b; |
323 | 561 | ||
324 | if (c->size < PAGE_SIZE) | 562 | if (c->size < PAGE_SIZE) |
325 | b = slob_alloc(c->size, flags, c->align); | 563 | b = slob_alloc(c->size, flags, c->align, node); |
326 | else | 564 | else |
327 | b = (void *)__get_free_pages(flags, get_order(c->size)); | 565 | b = slob_new_page(flags, get_order(c->size), node); |
328 | 566 | ||
329 | if (c->ctor) | 567 | if (c->ctor) |
330 | c->ctor(b, c, 0); | 568 | c->ctor(b, c, 0); |
331 | 569 | ||
332 | return b; | 570 | return b; |
333 | } | 571 | } |
334 | EXPORT_SYMBOL(kmem_cache_alloc); | 572 | EXPORT_SYMBOL(kmem_cache_alloc_node); |
335 | 573 | ||
336 | void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags) | 574 | void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags) |
337 | { | 575 | { |
@@ -385,9 +623,6 @@ const char *kmem_cache_name(struct kmem_cache *c) | |||
385 | } | 623 | } |
386 | EXPORT_SYMBOL(kmem_cache_name); | 624 | EXPORT_SYMBOL(kmem_cache_name); |
387 | 625 | ||
388 | static struct timer_list slob_timer = TIMER_INITIALIZER( | ||
389 | (void (*)(unsigned long))slob_timer_cbk, 0, 0); | ||
390 | |||
391 | int kmem_cache_shrink(struct kmem_cache *d) | 626 | int kmem_cache_shrink(struct kmem_cache *d) |
392 | { | 627 | { |
393 | return 0; | 628 | return 0; |
@@ -399,17 +634,14 @@ int kmem_ptr_validate(struct kmem_cache *a, const void *b) | |||
399 | return 0; | 634 | return 0; |
400 | } | 635 | } |
401 | 636 | ||
402 | void __init kmem_cache_init(void) | 637 | static unsigned int slob_ready __read_mostly; |
638 | |||
639 | int slab_is_available(void) | ||
403 | { | 640 | { |
404 | slob_timer_cbk(); | 641 | return slob_ready; |
405 | } | 642 | } |
406 | 643 | ||
407 | static void slob_timer_cbk(void) | 644 | void __init kmem_cache_init(void) |
408 | { | 645 | { |
409 | void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1); | 646 | slob_ready = 1; |
410 | |||
411 | if (p) | ||
412 | free_page((unsigned long)p); | ||
413 | |||
414 | mod_timer(&slob_timer, jiffies + HZ); | ||
415 | } | 647 | } |