2 * SLOB Allocator: Simple List Of Blocks
4 * Matt Mackall <mpm@selenic.com> 12/30/03
8 * The core of SLOB is a traditional K&R style heap allocator, with
9 * support for returning aligned objects. The granularity of this
10 * allocator is as little as 2 bytes, however typically most architectures
11 * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
13 * The slob heap is a linked list of pages from __get_free_page, and
14 * within each page, there is a singly-linked list of free blocks (slob_t).
15 * The heap is grown on demand and allocation from the heap is currently
18 * Above this is an implementation of kmalloc/kfree. Blocks returned
19 * from kmalloc are prepended with a 4-byte header with the kmalloc size.
20 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
21 * __get_free_pages directly, allocating compound pages so the page order
22 * does not have to be separately tracked, and also stores the exact
23 * allocation size in page->private so that it can be used to accurately
24 * provide ksize(). These objects are detected in kfree() because slob_page()
27 * SLAB is emulated on top of SLOB by simply calling constructors and
28 * destructors for every SLAB allocation. Objects are returned with the
29 * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
30 * case the low-level allocator will fragment blocks to create the proper
31 * alignment. Again, objects of page-size or greater are allocated by
32 * calling __get_free_pages. As SLAB objects know their size, no separate
33 * size bookkeeping is necessary and there is essentially no allocation
34 * space overhead, and compound pages aren't needed for multi-page
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
41 #include <linux/cache.h>
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/rcupdate.h>
45 #include <linux/list.h>
46 #include <asm/atomic.h>
49 * slob_block has a field 'units', which indicates size of block if +ve,
50 * or offset of next block if -ve (in SLOB_UNITs).
52 * Free blocks of size 1 unit simply contain the offset of the next block.
53 * Those with larger size contain their size in the first SLOB_UNIT of
54 * memory, and the offset of the next free block in the second SLOB_UNIT.
56 #if PAGE_SIZE <= (32767 * 2)
57 typedef s16 slobidx_t;
59 typedef s32 slobidx_t;
65 typedef struct slob_block slob_t;
68 * We use struct page fields to manage some slob allocation aspects,
69 * however to avoid the horrible mess in include/linux/mm_types.h, we'll
70 * just define our own struct page type variant here.
75 unsigned long flags; /* mandatory */
76 atomic_t _count; /* mandatory */
77 slobidx_t units; /* free units left in page */
79 slob_t *free; /* first free slob_t in page */
80 struct list_head list; /* linked list of free pages */
85 static inline void struct_slob_page_wrong_size(void)
86 { BUILD_BUG_ON(sizeof(struct slob_page) != sizeof(struct page)); }
89 * free_slob_page: call before a slob_page is returned to the page allocator.
91 static inline void free_slob_page(struct slob_page *sp)
93 reset_page_mapcount(&sp->page);
94 sp->page.mapping = NULL;
98 * All (partially) free slob pages go on this list.
100 static LIST_HEAD(free_slob_pages);
103 * slob_page: True for all slob pages (false for bigblock pages)
105 static inline int slob_page(struct slob_page *sp)
107 return test_bit(PG_active, &sp->flags);
110 static inline void set_slob_page(struct slob_page *sp)
112 __set_bit(PG_active, &sp->flags);
115 static inline void clear_slob_page(struct slob_page *sp)
117 __clear_bit(PG_active, &sp->flags);
121 * slob_page_free: true for pages on free_slob_pages list.
123 static inline int slob_page_free(struct slob_page *sp)
125 return test_bit(PG_private, &sp->flags);
128 static inline void set_slob_page_free(struct slob_page *sp)
130 list_add(&sp->list, &free_slob_pages);
131 __set_bit(PG_private, &sp->flags);
134 static inline void clear_slob_page_free(struct slob_page *sp)
137 __clear_bit(PG_private, &sp->flags);
140 #define SLOB_UNIT sizeof(slob_t)
141 #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
142 #define SLOB_ALIGN L1_CACHE_BYTES
145 * struct slob_rcu is inserted at the tail of allocated slob blocks, which
146 * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
147 * the block using call_rcu.
150 struct rcu_head head;
155 * slob_lock protects all slob allocator structures.
157 static DEFINE_SPINLOCK(slob_lock);
160 * Encode the given size and next info into a free slob block s.
162 static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
164 slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
165 slobidx_t offset = next - base;
171 s[0].units = -offset;
175 * Return the size of a slob block.
177 static slobidx_t slob_units(slob_t *s)
185 * Return the next free slob block pointer after this one.
187 static slob_t *slob_next(slob_t *s)
189 slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
200 * Returns true if s is the last free block in its page.
202 static int slob_last(slob_t *s)
204 return !((unsigned long)slob_next(s) & ~PAGE_MASK);
208 * Allocate a slob block within a given slob_page sp.
210 static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
212 slob_t *prev, *cur, *aligned = 0;
213 int delta = 0, units = SLOB_UNITS(size);
215 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
216 slobidx_t avail = slob_units(cur);
219 aligned = (slob_t *)ALIGN((unsigned long)cur, align);
220 delta = aligned - cur;
222 if (avail >= units + delta) { /* room enough? */
225 if (delta) { /* need to fragment head to align? */
226 next = slob_next(cur);
227 set_slob(aligned, avail - delta, next);
228 set_slob(cur, delta, aligned);
231 avail = slob_units(cur);
234 next = slob_next(cur);
235 if (avail == units) { /* exact fit? unlink. */
237 set_slob(prev, slob_units(prev), next);
240 } else { /* fragment */
242 set_slob(prev, slob_units(prev), cur + units);
244 sp->free = cur + units;
245 set_slob(cur + units, avail - units, next);
250 clear_slob_page_free(sp);
259 * slob_alloc: entry point into the slob allocator.
261 static void *slob_alloc(size_t size, gfp_t gfp, int align)
263 struct slob_page *sp;
267 spin_lock_irqsave(&slob_lock, flags);
268 /* Iterate through each partially free page, try to find room */
269 list_for_each_entry(sp, &free_slob_pages, list) {
270 if (sp->units >= SLOB_UNITS(size)) {
271 b = slob_page_alloc(sp, size, align);
276 spin_unlock_irqrestore(&slob_lock, flags);
278 /* Not enough space: must allocate a new page */
280 b = (slob_t *)__get_free_page(gfp);
283 sp = (struct slob_page *)virt_to_page(b);
286 spin_lock_irqsave(&slob_lock, flags);
287 sp->units = SLOB_UNITS(PAGE_SIZE);
289 INIT_LIST_HEAD(&sp->list);
290 set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
291 set_slob_page_free(sp);
292 b = slob_page_alloc(sp, size, align);
294 spin_unlock_irqrestore(&slob_lock, flags);
300 * slob_free: entry point into the slob allocator.
302 static void slob_free(void *block, int size)
304 struct slob_page *sp;
305 slob_t *prev, *next, *b = (slob_t *)block;
313 sp = (struct slob_page *)virt_to_page(block);
314 units = SLOB_UNITS(size);
316 spin_lock_irqsave(&slob_lock, flags);
318 if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
319 /* Go directly to page allocator. Do not pass slob allocator */
320 if (slob_page_free(sp))
321 clear_slob_page_free(sp);
324 free_page((unsigned long)b);
328 if (!slob_page_free(sp)) {
329 /* This slob page is about to become partially free. Easy! */
333 (void *)((unsigned long)(b +
334 SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
335 set_slob_page_free(sp);
340 * Otherwise the page is already partially free, so find reinsertion
346 set_slob(b, units, sp->free);
350 next = slob_next(prev);
353 next = slob_next(prev);
356 if (!slob_last(prev) && b + units == next) {
357 units += slob_units(next);
358 set_slob(b, units, slob_next(next));
360 set_slob(b, units, next);
362 if (prev + slob_units(prev) == b) {
363 units = slob_units(b) + slob_units(prev);
364 set_slob(prev, units, slob_next(b));
366 set_slob(prev, slob_units(prev), b);
369 spin_unlock_irqrestore(&slob_lock, flags);
373 * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
376 #ifndef ARCH_KMALLOC_MINALIGN
377 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
380 #ifndef ARCH_SLAB_MINALIGN
381 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
385 void *__kmalloc(size_t size, gfp_t gfp)
387 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
389 if (size < PAGE_SIZE - align) {
391 m = slob_alloc(size + align, gfp, align);
394 return (void *)m + align;
398 ret = (void *) __get_free_pages(gfp | __GFP_COMP,
402 page = virt_to_page(ret);
403 page->private = size;
408 EXPORT_SYMBOL(__kmalloc);
411 * krealloc - reallocate memory. The contents will remain unchanged.
413 * @p: object to reallocate memory for.
414 * @new_size: how many bytes of memory are required.
415 * @flags: the type of memory to allocate.
417 * The contents of the object pointed to are preserved up to the
418 * lesser of the new and old sizes. If @p is %NULL, krealloc()
419 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
420 * %NULL pointer, the object pointed to is freed.
422 void *krealloc(const void *p, size_t new_size, gfp_t flags)
427 return kmalloc_track_caller(new_size, flags);
429 if (unlikely(!new_size)) {
434 ret = kmalloc_track_caller(new_size, flags);
436 memcpy(ret, p, min(new_size, ksize(p)));
441 EXPORT_SYMBOL(krealloc);
443 void kfree(const void *block)
445 struct slob_page *sp;
450 sp = (struct slob_page *)virt_to_page(block);
452 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
453 unsigned int *m = (unsigned int *)(block - align);
454 slob_free(m, *m + align);
459 EXPORT_SYMBOL(kfree);
461 /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
462 size_t ksize(const void *block)
464 struct slob_page *sp;
469 sp = (struct slob_page *)virt_to_page(block);
471 return ((slob_t *)block - 1)->units + SLOB_UNIT;
473 return sp->page.private;
477 unsigned int size, align;
480 void (*ctor)(void *, struct kmem_cache *, unsigned long);
483 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
484 size_t align, unsigned long flags,
485 void (*ctor)(void*, struct kmem_cache *, unsigned long),
486 void (*dtor)(void*, struct kmem_cache *, unsigned long))
488 struct kmem_cache *c;
490 c = slob_alloc(sizeof(struct kmem_cache), flags, 0);
495 if (flags & SLAB_DESTROY_BY_RCU) {
496 /* leave room for rcu footer at the end of object */
497 c->size += sizeof(struct slob_rcu);
501 /* ignore alignment unless it's forced */
502 c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
503 if (c->align < ARCH_SLAB_MINALIGN)
504 c->align = ARCH_SLAB_MINALIGN;
505 if (c->align < align)
507 } else if (flags & SLAB_PANIC)
508 panic("Cannot create slab cache %s\n", name);
512 EXPORT_SYMBOL(kmem_cache_create);
514 void kmem_cache_destroy(struct kmem_cache *c)
516 slob_free(c, sizeof(struct kmem_cache));
518 EXPORT_SYMBOL(kmem_cache_destroy);
520 void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
524 if (c->size < PAGE_SIZE)
525 b = slob_alloc(c->size, flags, c->align);
527 b = (void *)__get_free_pages(flags, get_order(c->size));
534 EXPORT_SYMBOL(kmem_cache_alloc);
536 void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
538 void *ret = kmem_cache_alloc(c, flags);
540 memset(ret, 0, c->size);
544 EXPORT_SYMBOL(kmem_cache_zalloc);
546 static void __kmem_cache_free(void *b, int size)
548 if (size < PAGE_SIZE)
551 free_pages((unsigned long)b, get_order(size));
554 static void kmem_rcu_free(struct rcu_head *head)
556 struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
557 void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
559 __kmem_cache_free(b, slob_rcu->size);
562 void kmem_cache_free(struct kmem_cache *c, void *b)
564 if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
565 struct slob_rcu *slob_rcu;
566 slob_rcu = b + (c->size - sizeof(struct slob_rcu));
567 INIT_RCU_HEAD(&slob_rcu->head);
568 slob_rcu->size = c->size;
569 call_rcu(&slob_rcu->head, kmem_rcu_free);
571 __kmem_cache_free(b, c->size);
574 EXPORT_SYMBOL(kmem_cache_free);
576 unsigned int kmem_cache_size(struct kmem_cache *c)
580 EXPORT_SYMBOL(kmem_cache_size);
582 const char *kmem_cache_name(struct kmem_cache *c)
586 EXPORT_SYMBOL(kmem_cache_name);
588 int kmem_cache_shrink(struct kmem_cache *d)
592 EXPORT_SYMBOL(kmem_cache_shrink);
594 int kmem_ptr_validate(struct kmem_cache *a, const void *b)
599 void __init kmem_cache_init(void)