2 * SLOB Allocator: Simple List Of Blocks
4 * Matt Mackall <mpm@selenic.com> 12/30/03
6 * NUMA support by Paul Mundt, 2007.
10 * The core of SLOB is a traditional K&R style heap allocator, with
11 * support for returning aligned objects. The granularity of this
12 * allocator is as little as 2 bytes, however typically most architectures
13 * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
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
20 * Above this is an implementation of kmalloc/kfree. Blocks returned
21 * from kmalloc are prepended with a 4-byte header with the kmalloc size.
22 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
23 * alloc_pages() directly, allocating compound pages so the page order
24 * does not have to be separately tracked, and also stores the exact
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()
29 * SLAB is emulated on top of SLOB by simply calling constructors and
30 * destructors for every SLAB allocation. Objects are returned with the
31 * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
32 * case the low-level allocator will fragment blocks to create the proper
33 * alignment. Again, objects of page-size or greater are allocated by
34 * calling alloc_pages(). As SLAB objects know their size, no separate
35 * size bookkeeping is necessary and there is essentially no allocation
36 * space overhead, and compound pages aren't needed for multi-page
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().
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.
53 #include <linux/kernel.h>
54 #include <linux/slab.h>
56 #include <linux/cache.h>
57 #include <linux/init.h>
58 #include <linux/module.h>
59 #include <linux/rcupdate.h>
60 #include <linux/list.h>
61 #include <asm/atomic.h>
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).
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.
71 #if PAGE_SIZE <= (32767 * 2)
72 typedef s16 slobidx_t
;
74 typedef s32 slobidx_t
;
80 typedef struct slob_block slob_t
;
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.
90 unsigned long flags
; /* mandatory */
91 atomic_t _count
; /* mandatory */
92 slobidx_t units
; /* free units left in page */
94 slob_t
*free
; /* first free slob_t in page */
95 struct list_head list
; /* linked list of free pages */
100 static inline void struct_slob_page_wrong_size(void)
101 { BUILD_BUG_ON(sizeof(struct slob_page
) != sizeof(struct page
)); }
104 * free_slob_page: call before a slob_page is returned to the page allocator.
106 static inline void free_slob_page(struct slob_page
*sp
)
108 reset_page_mapcount(&sp
->page
);
109 sp
->page
.mapping
= NULL
;
113 * All (partially) free slob pages go on this list.
115 static LIST_HEAD(free_slob_pages
);
118 * slob_page: True for all slob pages (false for bigblock pages)
120 static inline int slob_page(struct slob_page
*sp
)
122 return test_bit(PG_active
, &sp
->flags
);
125 static inline void set_slob_page(struct slob_page
*sp
)
127 __set_bit(PG_active
, &sp
->flags
);
130 static inline void clear_slob_page(struct slob_page
*sp
)
132 __clear_bit(PG_active
, &sp
->flags
);
136 * slob_page_free: true for pages on free_slob_pages list.
138 static inline int slob_page_free(struct slob_page
*sp
)
140 return test_bit(PG_private
, &sp
->flags
);
143 static inline void set_slob_page_free(struct slob_page
*sp
)
145 list_add(&sp
->list
, &free_slob_pages
);
146 __set_bit(PG_private
, &sp
->flags
);
149 static inline void clear_slob_page_free(struct slob_page
*sp
)
152 __clear_bit(PG_private
, &sp
->flags
);
155 #define SLOB_UNIT sizeof(slob_t)
156 #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
157 #define SLOB_ALIGN L1_CACHE_BYTES
160 * struct slob_rcu is inserted at the tail of allocated slob blocks, which
161 * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
162 * the block using call_rcu.
165 struct rcu_head head
;
170 * slob_lock protects all slob allocator structures.
172 static DEFINE_SPINLOCK(slob_lock
);
175 * Encode the given size and next info into a free slob block s.
177 static void set_slob(slob_t
*s
, slobidx_t size
, slob_t
*next
)
179 slob_t
*base
= (slob_t
*)((unsigned long)s
& PAGE_MASK
);
180 slobidx_t offset
= next
- base
;
186 s
[0].units
= -offset
;
190 * Return the size of a slob block.
192 static slobidx_t
slob_units(slob_t
*s
)
200 * Return the next free slob block pointer after this one.
202 static slob_t
*slob_next(slob_t
*s
)
204 slob_t
*base
= (slob_t
*)((unsigned long)s
& PAGE_MASK
);
215 * Returns true if s is the last free block in its page.
217 static int slob_last(slob_t
*s
)
219 return !((unsigned long)slob_next(s
) & ~PAGE_MASK
);
222 static void *slob_new_page(gfp_t gfp
, int order
, int node
)
228 page
= alloc_pages_node(node
, gfp
, order
);
231 page
= alloc_pages(gfp
, order
);
236 return page_address(page
);
240 * Allocate a slob block within a given slob_page sp.
242 static void *slob_page_alloc(struct slob_page
*sp
, size_t size
, int align
)
244 slob_t
*prev
, *cur
, *aligned
= 0;
245 int delta
= 0, units
= SLOB_UNITS(size
);
247 for (prev
= NULL
, cur
= sp
->free
; ; prev
= cur
, cur
= slob_next(cur
)) {
248 slobidx_t avail
= slob_units(cur
);
251 aligned
= (slob_t
*)ALIGN((unsigned long)cur
, align
);
252 delta
= aligned
- cur
;
254 if (avail
>= units
+ delta
) { /* room enough? */
257 if (delta
) { /* need to fragment head to align? */
258 next
= slob_next(cur
);
259 set_slob(aligned
, avail
- delta
, next
);
260 set_slob(cur
, delta
, aligned
);
263 avail
= slob_units(cur
);
266 next
= slob_next(cur
);
267 if (avail
== units
) { /* exact fit? unlink. */
269 set_slob(prev
, slob_units(prev
), next
);
272 } else { /* fragment */
274 set_slob(prev
, slob_units(prev
), cur
+ units
);
276 sp
->free
= cur
+ units
;
277 set_slob(cur
+ units
, avail
- units
, next
);
282 clear_slob_page_free(sp
);
291 * slob_alloc: entry point into the slob allocator.
293 static void *slob_alloc(size_t size
, gfp_t gfp
, int align
, int node
)
295 struct slob_page
*sp
;
299 spin_lock_irqsave(&slob_lock
, flags
);
300 /* Iterate through each partially free page, try to find room */
301 list_for_each_entry(sp
, &free_slob_pages
, list
) {
304 * If there's a node specification, search for a partial
305 * page with a matching node id in the freelist.
307 if (node
!= -1 && page_to_nid(&sp
->page
) != node
)
311 if (sp
->units
>= SLOB_UNITS(size
)) {
312 b
= slob_page_alloc(sp
, size
, align
);
317 spin_unlock_irqrestore(&slob_lock
, flags
);
319 /* Not enough space: must allocate a new page */
321 b
= slob_new_page(gfp
, 0, node
);
324 sp
= (struct slob_page
*)virt_to_page(b
);
327 spin_lock_irqsave(&slob_lock
, flags
);
328 sp
->units
= SLOB_UNITS(PAGE_SIZE
);
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
);
335 spin_unlock_irqrestore(&slob_lock
, flags
);
337 if (unlikely((gfp
& __GFP_ZERO
) && b
))
343 * slob_free: entry point into the slob allocator.
345 static void slob_free(void *block
, int size
)
347 struct slob_page
*sp
;
348 slob_t
*prev
, *next
, *b
= (slob_t
*)block
;
352 if (ZERO_OR_NULL_PTR(block
))
356 sp
= (struct slob_page
*)virt_to_page(block
);
357 units
= SLOB_UNITS(size
);
359 spin_lock_irqsave(&slob_lock
, flags
);
361 if (sp
->units
+ units
== SLOB_UNITS(PAGE_SIZE
)) {
362 /* Go directly to page allocator. Do not pass slob allocator */
363 if (slob_page_free(sp
))
364 clear_slob_page_free(sp
);
367 free_page((unsigned long)b
);
371 if (!slob_page_free(sp
)) {
372 /* This slob page is about to become partially free. Easy! */
376 (void *)((unsigned long)(b
+
377 SLOB_UNITS(PAGE_SIZE
)) & PAGE_MASK
));
378 set_slob_page_free(sp
);
383 * Otherwise the page is already partially free, so find reinsertion
389 set_slob(b
, units
, sp
->free
);
393 next
= slob_next(prev
);
396 next
= slob_next(prev
);
399 if (!slob_last(prev
) && b
+ units
== next
) {
400 units
+= slob_units(next
);
401 set_slob(b
, units
, slob_next(next
));
403 set_slob(b
, units
, next
);
405 if (prev
+ slob_units(prev
) == b
) {
406 units
= slob_units(b
) + slob_units(prev
);
407 set_slob(prev
, units
, slob_next(b
));
409 set_slob(prev
, slob_units(prev
), b
);
412 spin_unlock_irqrestore(&slob_lock
, flags
);
416 * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
419 #ifndef ARCH_KMALLOC_MINALIGN
420 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
423 #ifndef ARCH_SLAB_MINALIGN
424 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
427 void *__kmalloc_node(size_t size
, gfp_t gfp
, int node
)
430 int align
= max(ARCH_KMALLOC_MINALIGN
, ARCH_SLAB_MINALIGN
);
432 if (size
< PAGE_SIZE
- align
) {
434 return ZERO_SIZE_PTR
;
436 m
= slob_alloc(size
+ align
, gfp
, align
, node
);
439 return (void *)m
+ align
;
443 ret
= slob_new_page(gfp
| __GFP_COMP
, get_order(size
), node
);
446 page
= virt_to_page(ret
);
447 page
->private = size
;
452 EXPORT_SYMBOL(__kmalloc_node
);
454 void kfree(const void *block
)
456 struct slob_page
*sp
;
458 if (ZERO_OR_NULL_PTR(block
))
461 sp
= (struct slob_page
*)virt_to_page(block
);
463 int align
= max(ARCH_KMALLOC_MINALIGN
, ARCH_SLAB_MINALIGN
);
464 unsigned int *m
= (unsigned int *)(block
- align
);
465 slob_free(m
, *m
+ align
);
469 EXPORT_SYMBOL(kfree
);
471 /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
472 size_t ksize(const void *block
)
474 struct slob_page
*sp
;
476 if (ZERO_OR_NULL_PTR(block
))
479 sp
= (struct slob_page
*)virt_to_page(block
);
481 return ((slob_t
*)block
- 1)->units
+ SLOB_UNIT
;
483 return sp
->page
.private;
487 unsigned int size
, align
;
490 void (*ctor
)(void *, struct kmem_cache
*, unsigned long);
493 struct kmem_cache
*kmem_cache_create(const char *name
, size_t size
,
494 size_t align
, unsigned long flags
,
495 void (*ctor
)(void*, struct kmem_cache
*, unsigned long),
496 void (*dtor
)(void*, struct kmem_cache
*, unsigned long))
498 struct kmem_cache
*c
;
500 c
= slob_alloc(sizeof(struct kmem_cache
), flags
, 0, -1);
505 if (flags
& SLAB_DESTROY_BY_RCU
) {
506 /* leave room for rcu footer at the end of object */
507 c
->size
+= sizeof(struct slob_rcu
);
511 /* ignore alignment unless it's forced */
512 c
->align
= (flags
& SLAB_HWCACHE_ALIGN
) ? SLOB_ALIGN
: 0;
513 if (c
->align
< ARCH_SLAB_MINALIGN
)
514 c
->align
= ARCH_SLAB_MINALIGN
;
515 if (c
->align
< align
)
517 } else if (flags
& SLAB_PANIC
)
518 panic("Cannot create slab cache %s\n", name
);
522 EXPORT_SYMBOL(kmem_cache_create
);
524 void kmem_cache_destroy(struct kmem_cache
*c
)
526 slob_free(c
, sizeof(struct kmem_cache
));
528 EXPORT_SYMBOL(kmem_cache_destroy
);
530 void *kmem_cache_alloc_node(struct kmem_cache
*c
, gfp_t flags
, int node
)
534 if (c
->size
< PAGE_SIZE
)
535 b
= slob_alloc(c
->size
, flags
, c
->align
, node
);
537 b
= slob_new_page(flags
, get_order(c
->size
), node
);
544 EXPORT_SYMBOL(kmem_cache_alloc_node
);
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 static unsigned int slob_ready __read_mostly
;
601 int slab_is_available(void)
606 void __init
kmem_cache_init(void)