calculation of pgoff in do_linear_fault() uses mixed units
[linux-2.6-block.git] / mm / slob.c
CommitLineData
10cef602
MM
1/*
2 * SLOB Allocator: Simple List Of Blocks
3 *
4 * Matt Mackall <mpm@selenic.com> 12/30/03
5 *
6193a2ff
PM
6 * NUMA support by Paul Mundt, 2007.
7 *
10cef602
MM
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
55394849
NP
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.
95b35127 14 *
6193a2ff 15 * The slob heap is a linked list of pages from alloc_pages(), and
95b35127
NP
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.
10cef602
MM
19 *
20 * Above this is an implementation of kmalloc/kfree. Blocks returned
55394849 21 * from kmalloc are prepended with a 4-byte header with the kmalloc size.
10cef602 22 * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
6193a2ff 23 * alloc_pages() directly, allocating compound pages so the page order
d87a133f
NP
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.
10cef602
MM
28 *
29 * SLAB is emulated on top of SLOB by simply calling constructors and
95b35127
NP
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
6193a2ff 34 * calling alloc_pages(). As SLAB objects know their size, no separate
95b35127 35 * size bookkeeping is necessary and there is essentially no allocation
d87a133f
NP
36 * space overhead, and compound pages aren't needed for multi-page
37 * allocations.
6193a2ff
PM
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.
10cef602
MM
51 */
52
95b35127 53#include <linux/kernel.h>
10cef602
MM
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>
afc0cedb 59#include <linux/rcupdate.h>
95b35127
NP
60#include <linux/list.h>
61#include <asm/atomic.h>
62
95b35127
NP
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 */
55394849 71#if PAGE_SIZE <= (32767 * 2)
95b35127
NP
72typedef s16 slobidx_t;
73#else
74typedef s32 slobidx_t;
75#endif
76
10cef602 77struct slob_block {
95b35127 78 slobidx_t units;
55394849 79};
10cef602
MM
80typedef struct slob_block slob_t;
81
95b35127
NP
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 */
87struct 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};
100static 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 */
106static 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 */
115static LIST_HEAD(free_slob_pages);
116
117/*
118 * slob_page: True for all slob pages (false for bigblock pages)
119 */
120static inline int slob_page(struct slob_page *sp)
121{
122 return test_bit(PG_active, &sp->flags);
123}
124
125static inline void set_slob_page(struct slob_page *sp)
126{
127 __set_bit(PG_active, &sp->flags);
128}
129
130static 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 */
138static inline int slob_page_free(struct slob_page *sp)
139{
140 return test_bit(PG_private, &sp->flags);
141}
142
143static 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
149static 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
10cef602
MM
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
afc0cedb
NP
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 */
164struct slob_rcu {
165 struct rcu_head head;
166 int size;
167};
168
95b35127
NP
169/*
170 * slob_lock protects all slob allocator structures.
171 */
10cef602 172static DEFINE_SPINLOCK(slob_lock);
10cef602 173
95b35127
NP
174/*
175 * Encode the given size and next info into a free slob block s.
176 */
177static 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;
bcb4ddb4 181
95b35127
NP
182 if (size > 1) {
183 s[0].units = size;
184 s[1].units = offset;
185 } else
186 s[0].units = -offset;
187}
10cef602 188
95b35127
NP
189/*
190 * Return the size of a slob block.
191 */
192static 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 */
202static 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 */
217static int slob_last(slob_t *s)
218{
219 return !((unsigned long)slob_next(s) & ~PAGE_MASK);
220}
221
6193a2ff
PM
222static 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
95b35127
NP
239/*
240 * Allocate a slob block within a given slob_page sp.
241 */
242static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
10cef602
MM
243{
244 slob_t *prev, *cur, *aligned = 0;
245 int delta = 0, units = SLOB_UNITS(size);
10cef602 246
95b35127
NP
247 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
248 slobidx_t avail = slob_units(cur);
249
10cef602
MM
250 if (align) {
251 aligned = (slob_t *)ALIGN((unsigned long)cur, align);
252 delta = aligned - cur;
253 }
95b35127
NP
254 if (avail >= units + delta) { /* room enough? */
255 slob_t *next;
256
10cef602 257 if (delta) { /* need to fragment head to align? */
95b35127
NP
258 next = slob_next(cur);
259 set_slob(aligned, avail - delta, next);
260 set_slob(cur, delta, aligned);
10cef602
MM
261 prev = cur;
262 cur = aligned;
95b35127 263 avail = slob_units(cur);
10cef602
MM
264 }
265
95b35127
NP
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);
10cef602
MM
278 }
279
95b35127
NP
280 sp->units -= units;
281 if (!sp->units)
282 clear_slob_page_free(sp);
10cef602
MM
283 return cur;
284 }
95b35127
NP
285 if (slob_last(cur))
286 return NULL;
287 }
288}
10cef602 289
95b35127
NP
290/*
291 * slob_alloc: entry point into the slob allocator.
292 */
6193a2ff 293static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
95b35127
NP
294{
295 struct slob_page *sp;
d6269543 296 struct list_head *prev;
95b35127
NP
297 slob_t *b = NULL;
298 unsigned long flags;
10cef602 299
95b35127
NP
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) {
6193a2ff
PM
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
d6269543
MM
311 /* Enough room on this page? */
312 if (sp->units < SLOB_UNITS(size))
313 continue;
6193a2ff 314
d6269543
MM
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) */
324 if (free_slob_pages.next != prev->next)
325 list_move_tail(&free_slob_pages, prev->next);
326 break;
10cef602 327 }
95b35127
NP
328 spin_unlock_irqrestore(&slob_lock, flags);
329
330 /* Not enough space: must allocate a new page */
331 if (!b) {
6193a2ff 332 b = slob_new_page(gfp, 0, node);
95b35127
NP
333 if (!b)
334 return 0;
335 sp = (struct slob_page *)virt_to_page(b);
336 set_slob_page(sp);
337
338 spin_lock_irqsave(&slob_lock, flags);
339 sp->units = SLOB_UNITS(PAGE_SIZE);
340 sp->free = b;
341 INIT_LIST_HEAD(&sp->list);
342 set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
343 set_slob_page_free(sp);
344 b = slob_page_alloc(sp, size, align);
345 BUG_ON(!b);
346 spin_unlock_irqrestore(&slob_lock, flags);
347 }
d07dbea4
CL
348 if (unlikely((gfp & __GFP_ZERO) && b))
349 memset(b, 0, size);
95b35127 350 return b;
10cef602
MM
351}
352
95b35127
NP
353/*
354 * slob_free: entry point into the slob allocator.
355 */
10cef602
MM
356static void slob_free(void *block, int size)
357{
95b35127
NP
358 struct slob_page *sp;
359 slob_t *prev, *next, *b = (slob_t *)block;
360 slobidx_t units;
10cef602
MM
361 unsigned long flags;
362
2408c550 363 if (unlikely(ZERO_OR_NULL_PTR(block)))
10cef602 364 return;
95b35127 365 BUG_ON(!size);
10cef602 366
95b35127
NP
367 sp = (struct slob_page *)virt_to_page(block);
368 units = SLOB_UNITS(size);
10cef602 369
10cef602 370 spin_lock_irqsave(&slob_lock, flags);
10cef602 371
95b35127
NP
372 if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
373 /* Go directly to page allocator. Do not pass slob allocator */
374 if (slob_page_free(sp))
375 clear_slob_page_free(sp);
376 clear_slob_page(sp);
377 free_slob_page(sp);
378 free_page((unsigned long)b);
379 goto out;
380 }
10cef602 381
95b35127
NP
382 if (!slob_page_free(sp)) {
383 /* This slob page is about to become partially free. Easy! */
384 sp->units = units;
385 sp->free = b;
386 set_slob(b, units,
387 (void *)((unsigned long)(b +
388 SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
389 set_slob_page_free(sp);
390 goto out;
391 }
392
393 /*
394 * Otherwise the page is already partially free, so find reinsertion
395 * point.
396 */
397 sp->units += units;
10cef602 398
95b35127
NP
399 if (b < sp->free) {
400 set_slob(b, units, sp->free);
401 sp->free = b;
402 } else {
403 prev = sp->free;
404 next = slob_next(prev);
405 while (b > next) {
406 prev = next;
407 next = slob_next(prev);
408 }
10cef602 409
95b35127
NP
410 if (!slob_last(prev) && b + units == next) {
411 units += slob_units(next);
412 set_slob(b, units, slob_next(next));
413 } else
414 set_slob(b, units, next);
415
416 if (prev + slob_units(prev) == b) {
417 units = slob_units(b) + slob_units(prev);
418 set_slob(prev, units, slob_next(b));
419 } else
420 set_slob(prev, slob_units(prev), b);
421 }
422out:
10cef602
MM
423 spin_unlock_irqrestore(&slob_lock, flags);
424}
425
95b35127
NP
426/*
427 * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
428 */
429
55394849
NP
430#ifndef ARCH_KMALLOC_MINALIGN
431#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
432#endif
433
434#ifndef ARCH_SLAB_MINALIGN
435#define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
436#endif
437
6193a2ff 438void *__kmalloc_node(size_t size, gfp_t gfp, int node)
10cef602 439{
6cb8f913 440 unsigned int *m;
55394849
NP
441 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
442
443 if (size < PAGE_SIZE - align) {
6cb8f913
CL
444 if (!size)
445 return ZERO_SIZE_PTR;
446
6193a2ff 447 m = slob_alloc(size + align, gfp, align, node);
95b35127 448 if (m)
55394849
NP
449 *m = size;
450 return (void *)m + align;
d87a133f
NP
451 } else {
452 void *ret;
453
6193a2ff 454 ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node);
d87a133f
NP
455 if (ret) {
456 struct page *page;
457 page = virt_to_page(ret);
458 page->private = size;
459 }
460 return ret;
10cef602 461 }
10cef602 462}
6193a2ff 463EXPORT_SYMBOL(__kmalloc_node);
10cef602
MM
464
465void kfree(const void *block)
466{
95b35127 467 struct slob_page *sp;
10cef602 468
2408c550 469 if (unlikely(ZERO_OR_NULL_PTR(block)))
10cef602
MM
470 return;
471
95b35127 472 sp = (struct slob_page *)virt_to_page(block);
d87a133f 473 if (slob_page(sp)) {
55394849
NP
474 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
475 unsigned int *m = (unsigned int *)(block - align);
476 slob_free(m, *m + align);
d87a133f
NP
477 } else
478 put_page(&sp->page);
10cef602 479}
10cef602
MM
480EXPORT_SYMBOL(kfree);
481
d87a133f 482/* can't use ksize for kmem_cache_alloc memory, only kmalloc */
fd76bab2 483size_t ksize(const void *block)
10cef602 484{
95b35127 485 struct slob_page *sp;
10cef602 486
2408c550 487 if (unlikely(ZERO_OR_NULL_PTR(block)))
10cef602
MM
488 return 0;
489
95b35127 490 sp = (struct slob_page *)virt_to_page(block);
d87a133f
NP
491 if (slob_page(sp))
492 return ((slob_t *)block - 1)->units + SLOB_UNIT;
493 else
494 return sp->page.private;
10cef602
MM
495}
496
497struct kmem_cache {
498 unsigned int size, align;
afc0cedb 499 unsigned long flags;
10cef602
MM
500 const char *name;
501 void (*ctor)(void *, struct kmem_cache *, unsigned long);
10cef602
MM
502};
503
504struct kmem_cache *kmem_cache_create(const char *name, size_t size,
505 size_t align, unsigned long flags,
20c2df83 506 void (*ctor)(void*, struct kmem_cache *, unsigned long))
10cef602
MM
507{
508 struct kmem_cache *c;
509
6193a2ff 510 c = slob_alloc(sizeof(struct kmem_cache), flags, 0, -1);
10cef602
MM
511
512 if (c) {
513 c->name = name;
514 c->size = size;
afc0cedb 515 if (flags & SLAB_DESTROY_BY_RCU) {
afc0cedb
NP
516 /* leave room for rcu footer at the end of object */
517 c->size += sizeof(struct slob_rcu);
518 }
519 c->flags = flags;
10cef602 520 c->ctor = ctor;
10cef602 521 /* ignore alignment unless it's forced */
5af60839 522 c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
55394849
NP
523 if (c->align < ARCH_SLAB_MINALIGN)
524 c->align = ARCH_SLAB_MINALIGN;
10cef602
MM
525 if (c->align < align)
526 c->align = align;
bc0055ae
AM
527 } else if (flags & SLAB_PANIC)
528 panic("Cannot create slab cache %s\n", name);
10cef602
MM
529
530 return c;
531}
532EXPORT_SYMBOL(kmem_cache_create);
533
133d205a 534void kmem_cache_destroy(struct kmem_cache *c)
10cef602
MM
535{
536 slob_free(c, sizeof(struct kmem_cache));
10cef602
MM
537}
538EXPORT_SYMBOL(kmem_cache_destroy);
539
6193a2ff 540void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
10cef602
MM
541{
542 void *b;
543
544 if (c->size < PAGE_SIZE)
6193a2ff 545 b = slob_alloc(c->size, flags, c->align, node);
10cef602 546 else
6193a2ff 547 b = slob_new_page(flags, get_order(c->size), node);
10cef602
MM
548
549 if (c->ctor)
a35afb83 550 c->ctor(b, c, 0);
10cef602
MM
551
552 return b;
553}
6193a2ff 554EXPORT_SYMBOL(kmem_cache_alloc_node);
10cef602 555
afc0cedb 556static void __kmem_cache_free(void *b, int size)
10cef602 557{
afc0cedb
NP
558 if (size < PAGE_SIZE)
559 slob_free(b, size);
10cef602 560 else
afc0cedb
NP
561 free_pages((unsigned long)b, get_order(size));
562}
563
564static void kmem_rcu_free(struct rcu_head *head)
565{
566 struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
567 void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
568
569 __kmem_cache_free(b, slob_rcu->size);
570}
571
572void kmem_cache_free(struct kmem_cache *c, void *b)
573{
574 if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
575 struct slob_rcu *slob_rcu;
576 slob_rcu = b + (c->size - sizeof(struct slob_rcu));
577 INIT_RCU_HEAD(&slob_rcu->head);
578 slob_rcu->size = c->size;
579 call_rcu(&slob_rcu->head, kmem_rcu_free);
580 } else {
afc0cedb
NP
581 __kmem_cache_free(b, c->size);
582 }
10cef602
MM
583}
584EXPORT_SYMBOL(kmem_cache_free);
585
586unsigned int kmem_cache_size(struct kmem_cache *c)
587{
588 return c->size;
589}
590EXPORT_SYMBOL(kmem_cache_size);
591
592const char *kmem_cache_name(struct kmem_cache *c)
593{
594 return c->name;
595}
596EXPORT_SYMBOL(kmem_cache_name);
597
2e892f43
CL
598int kmem_cache_shrink(struct kmem_cache *d)
599{
600 return 0;
601}
602EXPORT_SYMBOL(kmem_cache_shrink);
603
55935a34 604int kmem_ptr_validate(struct kmem_cache *a, const void *b)
2e892f43
CL
605{
606 return 0;
607}
608
84a01c2f
PM
609static unsigned int slob_ready __read_mostly;
610
611int slab_is_available(void)
612{
613 return slob_ready;
614}
615
bcb4ddb4
DG
616void __init kmem_cache_init(void)
617{
84a01c2f 618 slob_ready = 1;
10cef602 619}