nfsd4: lookup up callback cred only once
[safe/jmp/linux-2.6] / fs / bio.c
index eb6b468..cd42bb8 100644 (file)
--- a/fs/bio.c
+++ b/fs/bio.c
 
 DEFINE_TRACE(block_split);
 
-static struct kmem_cache *bio_slab __read_mostly;
+/*
+ * Test patch to inline a certain number of bi_io_vec's inside the bio
+ * itself, to shrink a bio data allocation from two mempool calls to one
+ */
+#define BIO_INLINE_VECS                4
 
 static mempool_t *bio_split_pool __read_mostly;
 
@@ -40,9 +44,8 @@ static mempool_t *bio_split_pool __read_mostly;
  * break badly! cannot be bigger than what you can fit into an
  * unsigned short
  */
-
 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
-static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
+struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
        BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
 };
 #undef BV
@@ -53,11 +56,119 @@ static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  */
 struct bio_set *fs_bio_set;
 
+/*
+ * Our slab pool management
+ */
+struct bio_slab {
+       struct kmem_cache *slab;
+       unsigned int slab_ref;
+       unsigned int slab_size;
+       char name[8];
+};
+static DEFINE_MUTEX(bio_slab_lock);
+static struct bio_slab *bio_slabs;
+static unsigned int bio_slab_nr, bio_slab_max;
+
+static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
+{
+       unsigned int sz = sizeof(struct bio) + extra_size;
+       struct kmem_cache *slab = NULL;
+       struct bio_slab *bslab;
+       unsigned int i, entry = -1;
+
+       mutex_lock(&bio_slab_lock);
+
+       i = 0;
+       while (i < bio_slab_nr) {
+               struct bio_slab *bslab = &bio_slabs[i];
+
+               if (!bslab->slab && entry == -1)
+                       entry = i;
+               else if (bslab->slab_size == sz) {
+                       slab = bslab->slab;
+                       bslab->slab_ref++;
+                       break;
+               }
+               i++;
+       }
+
+       if (slab)
+               goto out_unlock;
+
+       if (bio_slab_nr == bio_slab_max && entry == -1) {
+               bio_slab_max <<= 1;
+               bio_slabs = krealloc(bio_slabs,
+                                    bio_slab_max * sizeof(struct bio_slab),
+                                    GFP_KERNEL);
+               if (!bio_slabs)
+                       goto out_unlock;
+       }
+       if (entry == -1)
+               entry = bio_slab_nr++;
+
+       bslab = &bio_slabs[entry];
+
+       snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
+       slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
+       if (!slab)
+               goto out_unlock;
+
+       printk("bio: create slab <%s> at %d\n", bslab->name, entry);
+       bslab->slab = slab;
+       bslab->slab_ref = 1;
+       bslab->slab_size = sz;
+out_unlock:
+       mutex_unlock(&bio_slab_lock);
+       return slab;
+}
+
+static void bio_put_slab(struct bio_set *bs)
+{
+       struct bio_slab *bslab = NULL;
+       unsigned int i;
+
+       mutex_lock(&bio_slab_lock);
+
+       for (i = 0; i < bio_slab_nr; i++) {
+               if (bs->bio_slab == bio_slabs[i].slab) {
+                       bslab = &bio_slabs[i];
+                       break;
+               }
+       }
+
+       if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
+               goto out;
+
+       WARN_ON(!bslab->slab_ref);
+
+       if (--bslab->slab_ref)
+               goto out;
+
+       kmem_cache_destroy(bslab->slab);
+       bslab->slab = NULL;
+
+out:
+       mutex_unlock(&bio_slab_lock);
+}
+
 unsigned int bvec_nr_vecs(unsigned short idx)
 {
        return bvec_slabs[idx].nr_vecs;
 }
 
+void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx)
+{
+       BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
+
+       if (idx == BIOVEC_MAX_IDX)
+               mempool_free(bv, bs->bvec_pool);
+       else {
+               struct biovec_slab *bvs = bvec_slabs + idx;
+
+               kmem_cache_free(bvs->slab, bv);
+       }
+}
+
 struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
                              struct bio_set *bs)
 {
@@ -69,7 +180,7 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
         * kzalloc() for the exact number of vecs right away.
         */
        if (!bs)
-               bvl = kzalloc(nr * sizeof(struct bio_vec), gfp_mask);
+               bvl = kmalloc(nr * sizeof(struct bio_vec), gfp_mask);
 
        /*
         * see comment near bvec_array define!
@@ -126,32 +237,27 @@ fallback:
                }
        }
 
-       if (bvl)
-               memset(bvl, 0, bvec_nr_vecs(*idx) * sizeof(struct bio_vec));
-
        return bvl;
 }
 
 void bio_free(struct bio *bio, struct bio_set *bs)
 {
-       if (bio->bi_io_vec) {
-               const int pool_idx = BIO_POOL_IDX(bio);
-
-               BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
-
-               if (pool_idx == BIOVEC_MAX_IDX)
-                       mempool_free(bio->bi_io_vec, bs->bvec_pool);
-               else {
-                       struct biovec_slab *bvs = bvec_slabs + pool_idx;
+       void *p;
 
-                       kmem_cache_free(bvs->slab, bio->bi_io_vec);
-               }
-       }
+       if (bio_has_allocated_vec(bio))
+               bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio));
 
        if (bio_integrity(bio))
-               bio_integrity_free(bio, bs);
+               bio_integrity_free(bio);
+
+       /*
+        * If we have front padding, adjust the bio pointer before freeing
+        */
+       p = bio;
+       if (bs->front_pad)
+               p -= bs->front_pad;
 
-       mempool_free(bio, bs->bio_pool);
+       mempool_free(p, bs->bio_pool);
 }
 
 /*
@@ -164,7 +270,8 @@ static void bio_fs_destructor(struct bio *bio)
 
 static void bio_kmalloc_destructor(struct bio *bio)
 {
-       kfree(bio->bi_io_vec);
+       if (bio_has_allocated_vec(bio))
+               kfree(bio->bi_io_vec);
        kfree(bio);
 }
 
@@ -188,43 +295,77 @@ void bio_init(struct bio *bio)
  *   for a &struct bio to become free. If a %NULL @bs is passed in, we will
  *   fall back to just using @kmalloc to allocate the required memory.
  *
- *   allocate bio and iovecs from the memory pools specified by the
- *   bio_set structure, or @kmalloc if none given.
+ *   Note that the caller must set ->bi_destructor on succesful return
+ *   of a bio, to do the appropriate freeing of the bio once the reference
+ *   count drops to zero.
  **/
 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
 {
-       struct bio *bio;
-
-       if (bs)
-               bio = mempool_alloc(bs->bio_pool, gfp_mask);
-       else
+       struct bio_vec *bvl = NULL;
+       struct bio *bio = NULL;
+       unsigned long idx = 0;
+       void *p = NULL;
+
+       if (bs) {
+               p = mempool_alloc(bs->bio_pool, gfp_mask);
+               if (!p)
+                       goto err;
+               bio = p + bs->front_pad;
+       } else {
                bio = kmalloc(sizeof(*bio), gfp_mask);
+               if (!bio)
+                       goto err;
+       }
 
-       if (likely(bio)) {
-               struct bio_vec *bvl = NULL;
-
-               bio_init(bio);
-               if (likely(nr_iovecs)) {
-                       unsigned long uninitialized_var(idx);
-
-                       bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
-                       if (unlikely(!bvl)) {
-                               if (bs)
-                                       mempool_free(bio, bs->bio_pool);
-                               else
-                                       kfree(bio);
-                               bio = NULL;
-                               goto out;
-                       }
-                       bio->bi_flags |= idx << BIO_POOL_OFFSET;
-                       bio->bi_max_vecs = bvec_nr_vecs(idx);
-               }
-               bio->bi_io_vec = bvl;
+       bio_init(bio);
+
+       if (unlikely(!nr_iovecs))
+               goto out_set;
+
+       if (nr_iovecs <= BIO_INLINE_VECS) {
+               bvl = bio->bi_inline_vecs;
+               nr_iovecs = BIO_INLINE_VECS;
+       } else {
+               bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
+               if (unlikely(!bvl))
+                       goto err_free;
+
+               nr_iovecs = bvec_nr_vecs(idx);
        }
-out:
+       bio->bi_flags |= idx << BIO_POOL_OFFSET;
+       bio->bi_max_vecs = nr_iovecs;
+out_set:
+       bio->bi_io_vec = bvl;
+
        return bio;
+
+err_free:
+       if (bs)
+               mempool_free(p, bs->bio_pool);
+       else
+               kfree(bio);
+err:
+       return NULL;
 }
 
+/**
+ * bio_alloc - allocate a bio for I/O
+ * @gfp_mask:   the GFP_ mask given to the slab allocator
+ * @nr_iovecs: number of iovecs to pre-allocate
+ *
+ * Description:
+ *   bio_alloc will allocate a bio and associated bio_vec array that can hold
+ *   at least @nr_iovecs entries. Allocations will be done from the
+ *   fs_bio_set. Also see @bio_alloc_bioset.
+ *
+ *   If %__GFP_WAIT is set, then bio_alloc will always be able to allocate
+ *   a bio. This is due to the mempool guarantees. To make this work, callers
+ *   must never allocate more than 1 bio at the time from this pool. Callers
+ *   that need to allocate more than 1 bio must always submit the previously
+ *   allocate bio for IO before attempting to allocate a new one. Failure to
+ *   do so can cause livelocks under memory pressure.
+ *
+ **/
 struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
 {
        struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
@@ -343,10 +484,12 @@ struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
        if (bio_integrity(bio)) {
                int ret;
 
-               ret = bio_integrity_clone(b, bio, fs_bio_set);
+               ret = bio_integrity_clone(b, bio, gfp_mask);
 
-               if (ret < 0)
+               if (ret < 0) {
+                       bio_put(b);
                        return NULL;
+               }
        }
 
        return b;
@@ -669,6 +812,7 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
        int i, ret;
        int nr_pages = 0;
        unsigned int len = 0;
+       unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
 
        for (i = 0; i < iov_count; i++) {
                unsigned long uaddr;
@@ -695,35 +839,42 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
        bio->bi_rw |= (!write_to_vm << BIO_RW);
 
        ret = 0;
-       i = 0;
+
+       if (map_data) {
+               nr_pages = 1 << map_data->page_order;
+               i = map_data->offset / PAGE_SIZE;
+       }
        while (len) {
-               unsigned int bytes;
+               unsigned int bytes = PAGE_SIZE;
 
-               if (map_data)
-                       bytes = 1U << (PAGE_SHIFT + map_data->page_order);
-               else
-                       bytes = PAGE_SIZE;
+               bytes -= offset;
 
                if (bytes > len)
                        bytes = len;
 
                if (map_data) {
-                       if (i == map_data->nr_entries) {
+                       if (i == map_data->nr_entries * nr_pages) {
                                ret = -ENOMEM;
                                break;
                        }
-                       page = map_data->pages[i++];
-               } else
+
+                       page = map_data->pages[i / nr_pages];
+                       page += (i % nr_pages);
+
+                       i++;
+               } else {
                        page = alloc_page(q->bounce_gfp | gfp_mask);
-               if (!page) {
-                       ret = -ENOMEM;
-                       break;
+                       if (!page) {
+                               ret = -ENOMEM;
+                               break;
+                       }
                }
 
-               if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
+               if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
                        break;
 
                len -= bytes;
+               offset = 0;
        }
 
        if (ret)
@@ -732,7 +883,7 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
        /*
         * success
         */
-       if (!write_to_vm) {
+       if (!write_to_vm && (!map_data || !map_data->null_mapped)) {
                ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 0);
                if (ret)
                        goto cleanup;
@@ -1287,8 +1438,7 @@ static void bio_pair_end_2(struct bio *bi, int err)
 }
 
 /*
- * split a bio - only worry about a bio with a single page
- * in it's iovec
+ * split a bio - only worry about a bio with a single page in its iovec
  */
 struct bio_pair *bio_split(struct bio *bi, int first_sectors)
 {
@@ -1396,27 +1546,47 @@ void bioset_free(struct bio_set *bs)
        if (bs->bio_pool)
                mempool_destroy(bs->bio_pool);
 
-       bioset_integrity_free(bs);
        biovec_free_pools(bs);
+       bio_put_slab(bs);
 
        kfree(bs);
 }
 
-struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size)
+/**
+ * bioset_create  - Create a bio_set
+ * @pool_size: Number of bio and bio_vecs to cache in the mempool
+ * @front_pad: Number of bytes to allocate in front of the returned bio
+ *
+ * Description:
+ *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
+ *    to ask for a number of bytes to be allocated in front of the bio.
+ *    Front pad allocation is useful for embedding the bio inside
+ *    another structure, to avoid allocating extra data to go with the bio.
+ *    Note that the bio must be embedded at the END of that structure always,
+ *    or things will break badly.
+ */
+struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
 {
-       struct bio_set *bs = kzalloc(sizeof(*bs), GFP_KERNEL);
+       unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
+       struct bio_set *bs;
 
+       bs = kzalloc(sizeof(*bs), GFP_KERNEL);
        if (!bs)
                return NULL;
 
-       bs->bio_pool = mempool_create_slab_pool(bio_pool_size, bio_slab);
-       if (!bs->bio_pool)
-               goto bad;
+       bs->front_pad = front_pad;
+
+       bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
+       if (!bs->bio_slab) {
+               kfree(bs);
+               return NULL;
+       }
 
-       if (bioset_integrity_create(bs, bio_pool_size))
+       bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
+       if (!bs->bio_pool)
                goto bad;
 
-       if (!biovec_create_pools(bs, bvec_pool_size))
+       if (!biovec_create_pools(bs, pool_size))
                return bs;
 
 bad:
@@ -1432,6 +1602,13 @@ static void __init biovec_init_slabs(void)
                int size;
                struct biovec_slab *bvs = bvec_slabs + i;
 
+#ifndef CONFIG_BLK_DEV_INTEGRITY
+               if (bvs->nr_vecs <= BIO_INLINE_VECS) {
+                       bvs->slab = NULL;
+                       continue;
+               }
+#endif
+
                size = bvs->nr_vecs * sizeof(struct bio_vec);
                bvs->slab = kmem_cache_create(bvs->name, size, 0,
                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
@@ -1440,12 +1617,15 @@ static void __init biovec_init_slabs(void)
 
 static int __init init_bio(void)
 {
-       bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
+       bio_slab_max = 2;
+       bio_slab_nr = 0;
+       bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
+       if (!bio_slabs)
+               panic("bio: can't allocate bios\n");
 
-       bio_integrity_init_slab();
        biovec_init_slabs();
 
-       fs_bio_set = bioset_create(BIO_POOL_SIZE, 2);
+       fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
        if (!fs_bio_set)
                panic("bio: can't allocate bios\n");