Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / mm / slob.c
index 542394184a58e6c825a3b24cc7dfe1ad0e6e4fba..71976c5d40d301373ea9bc3f807553dde819c195 100644 (file)
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -21,7 +21,7 @@
  *
  * SLAB is emulated on top of SLOB by simply calling constructors and
  * destructors for every SLAB allocation. Objects are returned with
- * the 8-byte alignment unless the SLAB_MUST_HWCACHE_ALIGN flag is
+ * the 8-byte alignment unless the SLAB_HWCACHE_ALIGN flag is
  * set, in which case the low-level allocator will fragment blocks to
  * create the proper alignment. Again, objects of page-size or greater
  * are allocated by calling __get_free_pages. As SLAB objects know
@@ -35,6 +35,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/timer.h>
+#include <linux/rcupdate.h>
 
 struct slob_block {
        int units;
@@ -53,6 +54,16 @@ struct bigblock {
 };
 typedef struct bigblock bigblock_t;
 
+/*
+ * struct slob_rcu is inserted at the tail of allocated slob blocks, which
+ * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
+ * the block using call_rcu.
+ */
+struct slob_rcu {
+       struct rcu_head head;
+       int size;
+};
+
 static slob_t arena = { .next = &arena, .units = 1 };
 static slob_t *slobfree = &arena;
 static bigblock_t *bigblocks;
@@ -60,6 +71,8 @@ static DEFINE_SPINLOCK(slob_lock);
 static DEFINE_SPINLOCK(block_lock);
 
 static void slob_free(void *b, int size);
+static void slob_timer_cbk(void);
+
 
 static void *slob_alloc(size_t size, gfp_t gfp, int align)
 {
@@ -148,16 +161,7 @@ static void slob_free(void *block, int size)
        spin_unlock_irqrestore(&slob_lock, flags);
 }
 
-static int FASTCALL(find_order(int size));
-static int fastcall find_order(int size)
-{
-       int order = 0;
-       for ( ; size > 4096 ; size >>=1)
-               order++;
-       return order;
-}
-
-void *kmalloc(size_t size, gfp_t gfp)
+void *__kmalloc(size_t size, gfp_t gfp)
 {
        slob_t *m;
        bigblock_t *bb;
@@ -172,7 +176,7 @@ void *kmalloc(size_t size, gfp_t gfp)
        if (!bb)
                return 0;
 
-       bb->order = find_order(size);
+       bb->order = get_order(size);
        bb->pages = (void *)__get_free_pages(gfp, bb->order);
 
        if (bb->pages) {
@@ -186,8 +190,40 @@ void *kmalloc(size_t size, gfp_t gfp)
        slob_free(bb, sizeof(bigblock_t));
        return 0;
 }
+EXPORT_SYMBOL(__kmalloc);
 
-EXPORT_SYMBOL(kmalloc);
+/**
+ * krealloc - reallocate memory. The contents will remain unchanged.
+ *
+ * @p: object to reallocate memory for.
+ * @new_size: how many bytes of memory are required.
+ * @flags: the type of memory to allocate.
+ *
+ * The contents of the object pointed to are preserved up to the
+ * lesser of the new and old sizes.  If @p is %NULL, krealloc()
+ * behaves exactly like kmalloc().  If @size is 0 and @p is not a
+ * %NULL pointer, the object pointed to is freed.
+ */
+void *krealloc(const void *p, size_t new_size, gfp_t flags)
+{
+       void *ret;
+
+       if (unlikely(!p))
+               return kmalloc_track_caller(new_size, flags);
+
+       if (unlikely(!new_size)) {
+               kfree(p);
+               return NULL;
+       }
+
+       ret = kmalloc_track_caller(new_size, flags);
+       if (ret) {
+               memcpy(ret, p, min(new_size, ksize(p)));
+               kfree(p);
+       }
+       return ret;
+}
+EXPORT_SYMBOL(krealloc);
 
 void kfree(const void *block)
 {
@@ -218,7 +254,7 @@ void kfree(const void *block)
 
 EXPORT_SYMBOL(kfree);
 
-unsigned int ksize(const void *block)
+size_t ksize(const void *block)
 {
        bigblock_t *bb;
        unsigned long flags;
@@ -241,9 +277,9 @@ unsigned int ksize(const void *block)
 
 struct kmem_cache {
        unsigned int size, align;
+       unsigned long flags;
        const char *name;
        void (*ctor)(void *, struct kmem_cache *, unsigned long);
-       void (*dtor)(void *, struct kmem_cache *, unsigned long);
 };
 
 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
@@ -258,13 +294,18 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
        if (c) {
                c->name = name;
                c->size = size;
+               if (flags & SLAB_DESTROY_BY_RCU) {
+                       /* leave room for rcu footer at the end of object */
+                       c->size += sizeof(struct slob_rcu);
+               }
+               c->flags = flags;
                c->ctor = ctor;
-               c->dtor = dtor;
                /* ignore alignment unless it's forced */
-               c->align = (flags & SLAB_MUST_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
+               c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
                if (c->align < align)
                        c->align = align;
-       }
+       } else if (flags & SLAB_PANIC)
+               panic("Cannot create slab cache %s\n", name);
 
        return c;
 }
@@ -283,10 +324,10 @@ void *kmem_cache_alloc(struct kmem_cache *c, gfp_t flags)
        if (c->size < PAGE_SIZE)
                b = slob_alloc(c->size, flags, c->align);
        else
-               b = (void *)__get_free_pages(flags, find_order(c->size));
+               b = (void *)__get_free_pages(flags, get_order(c->size));
 
        if (c->ctor)
-               c->ctor(b, c, SLAB_CTOR_CONSTRUCTOR);
+               c->ctor(b, c, 0);
 
        return b;
 }
@@ -302,15 +343,33 @@ void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
 }
 EXPORT_SYMBOL(kmem_cache_zalloc);
 
-void kmem_cache_free(struct kmem_cache *c, void *b)
+static void __kmem_cache_free(void *b, int size)
 {
-       if (c->dtor)
-               c->dtor(b, c, 0);
-
-       if (c->size < PAGE_SIZE)
-               slob_free(b, c->size);
+       if (size < PAGE_SIZE)
+               slob_free(b, size);
        else
-               free_pages((unsigned long)b, find_order(c->size));
+               free_pages((unsigned long)b, get_order(size));
+}
+
+static void kmem_rcu_free(struct rcu_head *head)
+{
+       struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
+       void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
+
+       __kmem_cache_free(b, slob_rcu->size);
+}
+
+void kmem_cache_free(struct kmem_cache *c, void *b)
+{
+       if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
+               struct slob_rcu *slob_rcu;
+               slob_rcu = b + (c->size - sizeof(struct slob_rcu));
+               INIT_RCU_HEAD(&slob_rcu->head);
+               slob_rcu->size = c->size;
+               call_rcu(&slob_rcu->head, kmem_rcu_free);
+       } else {
+               __kmem_cache_free(b, c->size);
+       }
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
@@ -327,9 +386,25 @@ const char *kmem_cache_name(struct kmem_cache *c)
 EXPORT_SYMBOL(kmem_cache_name);
 
 static struct timer_list slob_timer = TIMER_INITIALIZER(
-       (void (*)(unsigned long))kmem_cache_init, 0, 0);
+       (void (*)(unsigned long))slob_timer_cbk, 0, 0);
+
+int kmem_cache_shrink(struct kmem_cache *d)
+{
+       return 0;
+}
+EXPORT_SYMBOL(kmem_cache_shrink);
+
+int kmem_ptr_validate(struct kmem_cache *a, const void *b)
+{
+       return 0;
+}
+
+void __init kmem_cache_init(void)
+{
+       slob_timer_cbk();
+}
 
-void kmem_cache_init(void)
+static void slob_timer_cbk(void)
 {
        void *p = slob_alloc(PAGE_SIZE, 0, PAGE_SIZE-1);