Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * SLOB Allocator: Simple List Of Blocks |
| 3 | * |
| 4 | * Matt Mackall <mpm@selenic.com> 12/30/03 |
| 5 | * |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 6 | * NUMA support by Paul Mundt, 2007. |
| 7 | * |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 8 | * How SLOB works: |
| 9 | * |
| 10 | * The core of SLOB is a traditional K&R style heap allocator, with |
| 11 | * support for returning aligned objects. The granularity of this |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 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. |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 14 | * |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 15 | * The slob heap is a linked list of pages from alloc_pages(), and |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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. |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 19 | * |
| 20 | * Above this is an implementation of kmalloc/kfree. Blocks returned |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 21 | * from kmalloc are prepended with a 4-byte header with the kmalloc size. |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 22 | * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 23 | * alloc_pages() directly, allocating compound pages so the page order |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 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() |
| 27 | * is false for them. |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 28 | * |
| 29 | * SLAB is emulated on top of SLOB by simply calling constructors and |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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 |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 34 | * calling alloc_pages(). As SLAB objects know their size, no separate |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 35 | * size bookkeeping is necessary and there is essentially no allocation |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 36 | * space overhead, and compound pages aren't needed for multi-page |
| 37 | * allocations. |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 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. |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 51 | */ |
| 52 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 53 | #include <linux/kernel.h> |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 54 | #include <linux/slab.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/cache.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/module.h> |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 59 | #include <linux/rcupdate.h> |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 60 | #include <linux/list.h> |
| 61 | #include <asm/atomic.h> |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 62 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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 | */ |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 71 | #if PAGE_SIZE <= (32767 * 2) |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 72 | typedef s16 slobidx_t; |
| 73 | #else |
| 74 | typedef s32 slobidx_t; |
| 75 | #endif |
| 76 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 77 | struct slob_block { |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 78 | slobidx_t units; |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 79 | }; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 80 | typedef struct slob_block slob_t; |
| 81 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 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 |
| 158 | |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 159 | /* |
| 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. |
| 163 | */ |
| 164 | struct slob_rcu { |
| 165 | struct rcu_head head; |
| 166 | int size; |
| 167 | }; |
| 168 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 169 | /* |
| 170 | * slob_lock protects all slob allocator structures. |
| 171 | */ |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 172 | static DEFINE_SPINLOCK(slob_lock); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 173 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 174 | /* |
| 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; |
Dimitri Gorokhovik | bcb4ddb | 2006-12-29 16:48:28 -0800 | [diff] [blame] | 181 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 182 | if (size > 1) { |
| 183 | s[0].units = size; |
| 184 | s[1].units = offset; |
| 185 | } else |
| 186 | s[0].units = -offset; |
| 187 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 188 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 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 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 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) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 243 | { |
| 244 | slob_t *prev, *cur, *aligned = 0; |
| 245 | int delta = 0, units = SLOB_UNITS(size); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 246 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 247 | for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) { |
| 248 | slobidx_t avail = slob_units(cur); |
| 249 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 250 | if (align) { |
| 251 | aligned = (slob_t *)ALIGN((unsigned long)cur, align); |
| 252 | delta = aligned - cur; |
| 253 | } |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 254 | if (avail >= units + delta) { /* room enough? */ |
| 255 | slob_t *next; |
| 256 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 257 | if (delta) { /* need to fragment head to align? */ |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 258 | next = slob_next(cur); |
| 259 | set_slob(aligned, avail - delta, next); |
| 260 | set_slob(cur, delta, aligned); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 261 | prev = cur; |
| 262 | cur = aligned; |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 263 | avail = slob_units(cur); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 266 | next = slob_next(cur); |
| 267 | if (avail == units) { /* exact fit? unlink. */ |
| 268 | if (prev) |
| 269 | set_slob(prev, slob_units(prev), next); |
| 270 | else |
| 271 | sp->free = next; |
| 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); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 280 | sp->units -= units; |
| 281 | if (!sp->units) |
| 282 | clear_slob_page_free(sp); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 283 | return cur; |
| 284 | } |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 285 | if (slob_last(cur)) |
| 286 | return NULL; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 290 | /* |
| 291 | * slob_alloc: entry point into the slob allocator. |
| 292 | */ |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 293 | static void *slob_alloc(size_t size, gfp_t gfp, int align, int node) |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 294 | { |
| 295 | struct slob_page *sp; |
Matt Mackall | d626954 | 2007-07-21 04:37:40 -0700 | [diff] [blame] | 296 | struct list_head *prev; |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 297 | slob_t *b = NULL; |
| 298 | unsigned long flags; |
| 299 | |
| 300 | spin_lock_irqsave(&slob_lock, flags); |
| 301 | /* Iterate through each partially free page, try to find room */ |
| 302 | list_for_each_entry(sp, &free_slob_pages, list) { |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 303 | #ifdef CONFIG_NUMA |
| 304 | /* |
| 305 | * If there's a node specification, search for a partial |
| 306 | * page with a matching node id in the freelist. |
| 307 | */ |
| 308 | if (node != -1 && page_to_nid(&sp->page) != node) |
| 309 | continue; |
| 310 | #endif |
Matt Mackall | d626954 | 2007-07-21 04:37:40 -0700 | [diff] [blame] | 311 | /* Enough room on this page? */ |
| 312 | if (sp->units < SLOB_UNITS(size)) |
| 313 | continue; |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 314 | |
Matt Mackall | d626954 | 2007-07-21 04:37:40 -0700 | [diff] [blame] | 315 | /* Attempt to alloc */ |
| 316 | prev = sp->list.prev; |
| 317 | b = slob_page_alloc(sp, size, align); |
| 318 | if (!b) |
| 319 | continue; |
| 320 | |
| 321 | /* Improve fragment distribution and reduce our average |
| 322 | * search time by starting our next search here. (see |
| 323 | * Knuth vol 1, sec 2.5, pg 449) */ |
Nick Piggin | d32ddd8 | 2007-11-15 12:32:04 +0100 | [diff] [blame] | 324 | if (prev != free_slob_pages.prev && |
| 325 | free_slob_pages.next != prev->next) |
Matt Mackall | d626954 | 2007-07-21 04:37:40 -0700 | [diff] [blame] | 326 | list_move_tail(&free_slob_pages, prev->next); |
| 327 | break; |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 328 | } |
| 329 | spin_unlock_irqrestore(&slob_lock, flags); |
| 330 | |
| 331 | /* Not enough space: must allocate a new page */ |
| 332 | if (!b) { |
Linus Torvalds | 7fd2725 | 2007-12-09 10:14:36 -0800 | [diff] [blame] | 333 | b = slob_new_page(gfp & ~__GFP_ZERO, 0, node); |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 334 | if (!b) |
| 335 | return 0; |
| 336 | sp = (struct slob_page *)virt_to_page(b); |
| 337 | set_slob_page(sp); |
| 338 | |
| 339 | spin_lock_irqsave(&slob_lock, flags); |
| 340 | sp->units = SLOB_UNITS(PAGE_SIZE); |
| 341 | sp->free = b; |
| 342 | INIT_LIST_HEAD(&sp->list); |
| 343 | set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE)); |
| 344 | set_slob_page_free(sp); |
| 345 | b = slob_page_alloc(sp, size, align); |
| 346 | BUG_ON(!b); |
| 347 | spin_unlock_irqrestore(&slob_lock, flags); |
| 348 | } |
Christoph Lameter | d07dbea | 2007-07-17 04:03:23 -0700 | [diff] [blame] | 349 | if (unlikely((gfp & __GFP_ZERO) && b)) |
| 350 | memset(b, 0, size); |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 351 | return b; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * slob_free: entry point into the slob allocator. |
| 356 | */ |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 357 | static void slob_free(void *block, int size) |
| 358 | { |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 359 | struct slob_page *sp; |
| 360 | slob_t *prev, *next, *b = (slob_t *)block; |
| 361 | slobidx_t units; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 362 | unsigned long flags; |
| 363 | |
Satyam Sharma | 2408c55 | 2007-10-16 01:24:44 -0700 | [diff] [blame] | 364 | if (unlikely(ZERO_OR_NULL_PTR(block))) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 365 | return; |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 366 | BUG_ON(!size); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 367 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 368 | sp = (struct slob_page *)virt_to_page(block); |
| 369 | units = SLOB_UNITS(size); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 370 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 371 | spin_lock_irqsave(&slob_lock, flags); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 372 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 373 | if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) { |
| 374 | /* Go directly to page allocator. Do not pass slob allocator */ |
| 375 | if (slob_page_free(sp)) |
| 376 | clear_slob_page_free(sp); |
| 377 | clear_slob_page(sp); |
| 378 | free_slob_page(sp); |
| 379 | free_page((unsigned long)b); |
| 380 | goto out; |
| 381 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 382 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 383 | if (!slob_page_free(sp)) { |
| 384 | /* This slob page is about to become partially free. Easy! */ |
| 385 | sp->units = units; |
| 386 | sp->free = b; |
| 387 | set_slob(b, units, |
| 388 | (void *)((unsigned long)(b + |
| 389 | SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK)); |
| 390 | set_slob_page_free(sp); |
| 391 | goto out; |
| 392 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 393 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 394 | /* |
| 395 | * Otherwise the page is already partially free, so find reinsertion |
| 396 | * point. |
| 397 | */ |
| 398 | sp->units += units; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 399 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 400 | if (b < sp->free) { |
Matt Mackall | 679299b | 2008-02-04 22:29:37 -0800 | [diff] [blame^] | 401 | if (b + units == sp->free) { |
| 402 | units += slob_units(sp->free); |
| 403 | sp->free = slob_next(sp->free); |
| 404 | } |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 405 | set_slob(b, units, sp->free); |
| 406 | sp->free = b; |
| 407 | } else { |
| 408 | prev = sp->free; |
| 409 | next = slob_next(prev); |
| 410 | while (b > next) { |
| 411 | prev = next; |
| 412 | next = slob_next(prev); |
| 413 | } |
| 414 | |
| 415 | if (!slob_last(prev) && b + units == next) { |
| 416 | units += slob_units(next); |
| 417 | set_slob(b, units, slob_next(next)); |
| 418 | } else |
| 419 | set_slob(b, units, next); |
| 420 | |
| 421 | if (prev + slob_units(prev) == b) { |
| 422 | units = slob_units(b) + slob_units(prev); |
| 423 | set_slob(prev, units, slob_next(b)); |
| 424 | } else |
| 425 | set_slob(prev, slob_units(prev), b); |
| 426 | } |
| 427 | out: |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 428 | spin_unlock_irqrestore(&slob_lock, flags); |
| 429 | } |
| 430 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 431 | /* |
| 432 | * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend. |
| 433 | */ |
| 434 | |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 435 | #ifndef ARCH_KMALLOC_MINALIGN |
| 436 | #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long) |
| 437 | #endif |
| 438 | |
| 439 | #ifndef ARCH_SLAB_MINALIGN |
| 440 | #define ARCH_SLAB_MINALIGN __alignof__(unsigned long) |
| 441 | #endif |
| 442 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 443 | void *__kmalloc_node(size_t size, gfp_t gfp, int node) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 444 | { |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 445 | unsigned int *m; |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 446 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
| 447 | |
| 448 | if (size < PAGE_SIZE - align) { |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 449 | if (!size) |
| 450 | return ZERO_SIZE_PTR; |
| 451 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 452 | m = slob_alloc(size + align, gfp, align, node); |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 453 | if (m) |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 454 | *m = size; |
| 455 | return (void *)m + align; |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 456 | } else { |
| 457 | void *ret; |
| 458 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 459 | ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node); |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 460 | if (ret) { |
| 461 | struct page *page; |
| 462 | page = virt_to_page(ret); |
| 463 | page->private = size; |
| 464 | } |
| 465 | return ret; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 466 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 467 | } |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 468 | EXPORT_SYMBOL(__kmalloc_node); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 469 | |
| 470 | void kfree(const void *block) |
| 471 | { |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 472 | struct slob_page *sp; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 473 | |
Satyam Sharma | 2408c55 | 2007-10-16 01:24:44 -0700 | [diff] [blame] | 474 | if (unlikely(ZERO_OR_NULL_PTR(block))) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 475 | return; |
| 476 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 477 | sp = (struct slob_page *)virt_to_page(block); |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 478 | if (slob_page(sp)) { |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 479 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
| 480 | unsigned int *m = (unsigned int *)(block - align); |
| 481 | slob_free(m, *m + align); |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 482 | } else |
| 483 | put_page(&sp->page); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 484 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 485 | EXPORT_SYMBOL(kfree); |
| 486 | |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 487 | /* can't use ksize for kmem_cache_alloc memory, only kmalloc */ |
Pekka Enberg | fd76bab | 2007-05-06 14:48:40 -0700 | [diff] [blame] | 488 | size_t ksize(const void *block) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 489 | { |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 490 | struct slob_page *sp; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 491 | |
Christoph Lameter | ef8b452 | 2007-10-16 01:24:46 -0700 | [diff] [blame] | 492 | BUG_ON(!block); |
| 493 | if (unlikely(block == ZERO_SIZE_PTR)) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 494 | return 0; |
| 495 | |
Nick Piggin | 95b3512 | 2007-07-15 23:38:07 -0700 | [diff] [blame] | 496 | sp = (struct slob_page *)virt_to_page(block); |
Nick Piggin | d87a133 | 2007-07-15 23:38:08 -0700 | [diff] [blame] | 497 | if (slob_page(sp)) |
| 498 | return ((slob_t *)block - 1)->units + SLOB_UNIT; |
| 499 | else |
| 500 | return sp->page.private; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 501 | } |
Tetsuo Handa | f8fcc93 | 2007-12-04 23:45:08 -0800 | [diff] [blame] | 502 | EXPORT_SYMBOL(ksize); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 503 | |
| 504 | struct kmem_cache { |
| 505 | unsigned int size, align; |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 506 | unsigned long flags; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 507 | const char *name; |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 508 | void (*ctor)(struct kmem_cache *, void *); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | struct kmem_cache *kmem_cache_create(const char *name, size_t size, |
| 512 | size_t align, unsigned long flags, |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 513 | void (*ctor)(struct kmem_cache *, void *)) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 514 | { |
| 515 | struct kmem_cache *c; |
| 516 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 517 | c = slob_alloc(sizeof(struct kmem_cache), flags, 0, -1); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 518 | |
| 519 | if (c) { |
| 520 | c->name = name; |
| 521 | c->size = size; |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 522 | if (flags & SLAB_DESTROY_BY_RCU) { |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 523 | /* leave room for rcu footer at the end of object */ |
| 524 | c->size += sizeof(struct slob_rcu); |
| 525 | } |
| 526 | c->flags = flags; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 527 | c->ctor = ctor; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 528 | /* ignore alignment unless it's forced */ |
Christoph Lameter | 5af6083 | 2007-05-06 14:49:56 -0700 | [diff] [blame] | 529 | c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0; |
Nick Piggin | 5539484 | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 530 | if (c->align < ARCH_SLAB_MINALIGN) |
| 531 | c->align = ARCH_SLAB_MINALIGN; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 532 | if (c->align < align) |
| 533 | c->align = align; |
Akinobu Mita | bc0055a | 2007-05-06 14:49:52 -0700 | [diff] [blame] | 534 | } else if (flags & SLAB_PANIC) |
| 535 | panic("Cannot create slab cache %s\n", name); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 536 | |
| 537 | return c; |
| 538 | } |
| 539 | EXPORT_SYMBOL(kmem_cache_create); |
| 540 | |
Alexey Dobriyan | 133d205 | 2006-09-27 01:49:41 -0700 | [diff] [blame] | 541 | void kmem_cache_destroy(struct kmem_cache *c) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 542 | { |
| 543 | slob_free(c, sizeof(struct kmem_cache)); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 544 | } |
| 545 | EXPORT_SYMBOL(kmem_cache_destroy); |
| 546 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 547 | void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node) |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 548 | { |
| 549 | void *b; |
| 550 | |
| 551 | if (c->size < PAGE_SIZE) |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 552 | b = slob_alloc(c->size, flags, c->align, node); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 553 | else |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 554 | b = slob_new_page(flags, get_order(c->size), node); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 555 | |
| 556 | if (c->ctor) |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 557 | c->ctor(c, b); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 558 | |
| 559 | return b; |
| 560 | } |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 561 | EXPORT_SYMBOL(kmem_cache_alloc_node); |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 562 | |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 563 | static void __kmem_cache_free(void *b, int size) |
| 564 | { |
| 565 | if (size < PAGE_SIZE) |
| 566 | slob_free(b, size); |
| 567 | else |
| 568 | free_pages((unsigned long)b, get_order(size)); |
| 569 | } |
| 570 | |
| 571 | static void kmem_rcu_free(struct rcu_head *head) |
| 572 | { |
| 573 | struct slob_rcu *slob_rcu = (struct slob_rcu *)head; |
| 574 | void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu)); |
| 575 | |
| 576 | __kmem_cache_free(b, slob_rcu->size); |
| 577 | } |
| 578 | |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 579 | void kmem_cache_free(struct kmem_cache *c, void *b) |
| 580 | { |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 581 | if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) { |
| 582 | struct slob_rcu *slob_rcu; |
| 583 | slob_rcu = b + (c->size - sizeof(struct slob_rcu)); |
| 584 | INIT_RCU_HEAD(&slob_rcu->head); |
| 585 | slob_rcu->size = c->size; |
| 586 | call_rcu(&slob_rcu->head, kmem_rcu_free); |
| 587 | } else { |
Nick Piggin | afc0ced | 2007-05-16 22:10:49 -0700 | [diff] [blame] | 588 | __kmem_cache_free(b, c->size); |
| 589 | } |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 590 | } |
| 591 | EXPORT_SYMBOL(kmem_cache_free); |
| 592 | |
| 593 | unsigned int kmem_cache_size(struct kmem_cache *c) |
| 594 | { |
| 595 | return c->size; |
| 596 | } |
| 597 | EXPORT_SYMBOL(kmem_cache_size); |
| 598 | |
| 599 | const char *kmem_cache_name(struct kmem_cache *c) |
| 600 | { |
| 601 | return c->name; |
| 602 | } |
| 603 | EXPORT_SYMBOL(kmem_cache_name); |
| 604 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 605 | int kmem_cache_shrink(struct kmem_cache *d) |
| 606 | { |
| 607 | return 0; |
| 608 | } |
| 609 | EXPORT_SYMBOL(kmem_cache_shrink); |
| 610 | |
Christoph Lameter | 55935a3 | 2006-12-13 00:34:24 -0800 | [diff] [blame] | 611 | int kmem_ptr_validate(struct kmem_cache *a, const void *b) |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 612 | { |
| 613 | return 0; |
| 614 | } |
| 615 | |
Paul Mundt | 84a01c2 | 2007-07-15 23:38:24 -0700 | [diff] [blame] | 616 | static unsigned int slob_ready __read_mostly; |
| 617 | |
| 618 | int slab_is_available(void) |
| 619 | { |
| 620 | return slob_ready; |
| 621 | } |
| 622 | |
Dimitri Gorokhovik | bcb4ddb | 2006-12-29 16:48:28 -0800 | [diff] [blame] | 623 | void __init kmem_cache_init(void) |
| 624 | { |
Paul Mundt | 84a01c2 | 2007-07-15 23:38:24 -0700 | [diff] [blame] | 625 | slob_ready = 1; |
Matt Mackall | 10cef60 | 2006-01-08 01:01:45 -0800 | [diff] [blame] | 626 | } |