Merge tag 'tag-chrome-platform-for-v5.12' of git://git.kernel.org/pub/scm/linux/kerne...
[linux-2.6-microblaze.git] / block / bio.c
index dfd7740..a1c4d29 100644 (file)
 #include "blk.h"
 #include "blk-rq-qos.h"
 
-/*
- * 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
-
-/*
- * if you change this list, also change bvec_alloc or things will
- * break badly! cannot be bigger than what you can fit into an
- * unsigned short
- */
-#define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n }
-static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = {
-       BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max),
+static struct biovec_slab {
+       int nr_vecs;
+       char *name;
+       struct kmem_cache *slab;
+} bvec_slabs[] __read_mostly = {
+       { .nr_vecs = 16, .name = "biovec-16" },
+       { .nr_vecs = 64, .name = "biovec-64" },
+       { .nr_vecs = 128, .name = "biovec-128" },
+       { .nr_vecs = BIO_MAX_PAGES, .name = "biovec-max" },
 };
-#undef BV
+
+static struct biovec_slab *biovec_slab(unsigned short nr_vecs)
+{
+       switch (nr_vecs) {
+       /* smaller bios use inline vecs */
+       case 5 ... 16:
+               return &bvec_slabs[0];
+       case 17 ... 64:
+               return &bvec_slabs[1];
+       case 65 ... 128:
+               return &bvec_slabs[2];
+       case 129 ... BIO_MAX_PAGES:
+               return &bvec_slabs[3];
+       default:
+               BUG();
+               return NULL;
+       }
+}
 
 /*
  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
@@ -137,90 +149,55 @@ out:
        mutex_unlock(&bio_slab_lock);
 }
 
-unsigned int bvec_nr_vecs(unsigned short idx)
+void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)
 {
-       return bvec_slabs[--idx].nr_vecs;
-}
+       BIO_BUG_ON(nr_vecs > BIO_MAX_PAGES);
 
-void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
-{
-       if (!idx)
-               return;
-       idx--;
-
-       BIO_BUG_ON(idx >= BVEC_POOL_NR);
-
-       if (idx == BVEC_POOL_MAX) {
+       if (nr_vecs == BIO_MAX_PAGES)
                mempool_free(bv, pool);
-       } else {
-               struct biovec_slab *bvs = bvec_slabs + idx;
+       else if (nr_vecs > BIO_INLINE_VECS)
+               kmem_cache_free(biovec_slab(nr_vecs)->slab, bv);
+}
 
-               kmem_cache_free(bvs->slab, bv);
-       }
+/*
+ * Make the first allocation restricted and don't dump info on allocation
+ * failures, since we'll fall back to the mempool in case of failure.
+ */
+static inline gfp_t bvec_alloc_gfp(gfp_t gfp)
+{
+       return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) |
+               __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
 }
 
-struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
-                          mempool_t *pool)
+struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
+               gfp_t gfp_mask)
 {
-       struct bio_vec *bvl;
+       struct biovec_slab *bvs = biovec_slab(*nr_vecs);
 
-       /*
-        * see comment near bvec_array define!
-        */
-       switch (nr) {
-       case 1:
-               *idx = 0;
-               break;
-       case 2 ... 4:
-               *idx = 1;
-               break;
-       case 5 ... 16:
-               *idx = 2;
-               break;
-       case 17 ... 64:
-               *idx = 3;
-               break;
-       case 65 ... 128:
-               *idx = 4;
-               break;
-       case 129 ... BIO_MAX_PAGES:
-               *idx = 5;
-               break;
-       default:
+       if (WARN_ON_ONCE(!bvs))
                return NULL;
-       }
 
        /*
-        * idx now points to the pool we want to allocate from. only the
-        * 1-vec entry pool is mempool backed.
+        * Upgrade the nr_vecs request to take full advantage of the allocation.
+        * We also rely on this in the bvec_free path.
         */
-       if (*idx == BVEC_POOL_MAX) {
-fallback:
-               bvl = mempool_alloc(pool, gfp_mask);
-       } else {
-               struct biovec_slab *bvs = bvec_slabs + *idx;
-               gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
+       *nr_vecs = bvs->nr_vecs;
 
-               /*
-                * Make this allocation restricted and don't dump info on
-                * allocation failures, since we'll fallback to the mempool
-                * in case of failure.
-                */
-               __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
+       /*
+        * Try a slab allocation first for all smaller allocations.  If that
+        * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
+        * The mempool is sized to handle up to BIO_MAX_PAGES entries.
+        */
+       if (*nr_vecs < BIO_MAX_PAGES) {
+               struct bio_vec *bvl;
 
-               /*
-                * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM
-                * is set, retry with the 1-entry mempool
-                */
-               bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
-               if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) {
-                       *idx = BVEC_POOL_MAX;
-                       goto fallback;
-               }
+               bvl = kmem_cache_alloc(bvs->slab, bvec_alloc_gfp(gfp_mask));
+               if (likely(bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))
+                       return bvl;
+               *nr_vecs = BIO_MAX_PAGES;
        }
 
-       (*idx)++;
-       return bvl;
+       return mempool_alloc(pool, gfp_mask);
 }
 
 void bio_uninit(struct bio *bio)
@@ -246,7 +223,7 @@ static void bio_free(struct bio *bio)
        bio_uninit(bio);
 
        if (bs) {
-               bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio));
+               bvec_free(&bs->bvec_pool, bio->bi_io_vec, bio->bi_max_vecs);
 
                /*
                 * If we have front padding, adjust the bio pointer before freeing
@@ -290,12 +267,8 @@ EXPORT_SYMBOL(bio_init);
  */
 void bio_reset(struct bio *bio)
 {
-       unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
-
        bio_uninit(bio);
-
        memset(bio, 0, BIO_RESET_BYTES);
-       bio->bi_flags = flags;
        atomic_set(&bio->__bi_remaining, 1);
 }
 EXPORT_SYMBOL(bio_reset);
@@ -396,123 +369,97 @@ static void punt_bios_to_rescuer(struct bio_set *bs)
  * @nr_iovecs: number of iovecs to pre-allocate
  * @bs:                the bio_set to allocate from.
  *
- * Description:
- *   If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
- *   backed by the @bs's mempool.
+ * Allocate a bio from the mempools in @bs.
  *
- *   When @bs is not NULL, if %__GFP_DIRECT_RECLAIM 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 a time
- *   from this pool. Callers that need to allocate more than 1 bio must always
- *   submit the previously allocated bio for IO before attempting to allocate
- *   a new one. Failure to do so can cause deadlocks under memory pressure.
+ * If %__GFP_DIRECT_RECLAIM 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 a time from the general pool.
+ * Callers that need to allocate more than 1 bio must always submit the
+ * previously allocated bio for IO before attempting to allocate a new one.
+ * Failure to do so can cause deadlocks under memory pressure.
  *
- *   Note that when running under submit_bio_noacct() (i.e. any block
- *   driver), bios are not submitted until after you return - see the code in
- *   submit_bio_noacct() that converts recursion into iteration, to prevent
- *   stack overflows.
+ * Note that when running under submit_bio_noacct() (i.e. any block driver),
+ * bios are not submitted until after you return - see the code in
+ * submit_bio_noacct() that converts recursion into iteration, to prevent
+ * stack overflows.
  *
- *   This would normally mean allocating multiple bios under
- *   submit_bio_noacct() would be susceptible to deadlocks, but we have
- *   deadlock avoidance code that resubmits any blocked bios from a rescuer
- *   thread.
+ * This would normally mean allocating multiple bios under submit_bio_noacct()
+ * would be susceptible to deadlocks, but we have
+ * deadlock avoidance code that resubmits any blocked bios from a rescuer
+ * thread.
  *
- *   However, we do not guarantee forward progress for allocations from other
- *   mempools. Doing multiple allocations from the same mempool under
- *   submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
- *   for per bio allocations.
+ * However, we do not guarantee forward progress for allocations from other
+ * mempools. Doing multiple allocations from the same mempool under
+ * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad
+ * for per bio allocations.
  *
- *   RETURNS:
- *   Pointer to new bio on success, NULL on failure.
+ * Returns: Pointer to new bio on success, NULL on failure.
  */
-struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
+struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned short nr_iovecs,
                             struct bio_set *bs)
 {
        gfp_t saved_gfp = gfp_mask;
-       unsigned front_pad;
-       unsigned inline_vecs;
-       struct bio_vec *bvl = NULL;
        struct bio *bio;
        void *p;
 
-       if (!bs) {
-               if (nr_iovecs > UIO_MAXIOV)
-                       return NULL;
-
-               p = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
-               front_pad = 0;
-               inline_vecs = nr_iovecs;
-       } else {
-               /* should not use nobvec bioset for nr_iovecs > 0 */
-               if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) &&
-                                nr_iovecs > 0))
-                       return NULL;
-               /*
-                * submit_bio_noacct() converts recursion to iteration; this
-                * means if we're running beneath it, any bios we allocate and
-                * submit will not be submitted (and thus freed) until after we
-                * return.
-                *
-                * This exposes us to a potential deadlock if we allocate
-                * multiple bios from the same bio_set() while running
-                * underneath submit_bio_noacct(). If we were to allocate
-                * multiple bios (say a stacking block driver that was splitting
-                * bios), we would deadlock if we exhausted the mempool's
-                * reserve.
-                *
-                * We solve this, and guarantee forward progress, with a rescuer
-                * workqueue per bio_set. If we go to allocate and there are
-                * bios on current->bio_list, we first try the allocation
-                * without __GFP_DIRECT_RECLAIM; if that fails, we punt those
-                * bios we would be blocking to the rescuer workqueue before
-                * we retry with the original gfp_flags.
-                */
-
-               if (current->bio_list &&
-                   (!bio_list_empty(&current->bio_list[0]) ||
-                    !bio_list_empty(&current->bio_list[1])) &&
-                   bs->rescue_workqueue)
-                       gfp_mask &= ~__GFP_DIRECT_RECLAIM;
+       /* should not use nobvec bioset for nr_iovecs > 0 */
+       if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_iovecs > 0))
+               return NULL;
 
+       /*
+        * submit_bio_noacct() converts recursion to iteration; this means if
+        * we're running beneath it, any bios we allocate and submit will not be
+        * submitted (and thus freed) until after we return.
+        *
+        * This exposes us to a potential deadlock if we allocate multiple bios
+        * from the same bio_set() while running underneath submit_bio_noacct().
+        * If we were to allocate multiple bios (say a stacking block driver
+        * that was splitting bios), we would deadlock if we exhausted the
+        * mempool's reserve.
+        *
+        * We solve this, and guarantee forward progress, with a rescuer
+        * workqueue per bio_set. If we go to allocate and there are bios on
+        * current->bio_list, we first try the allocation without
+        * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be
+        * blocking to the rescuer workqueue before we retry with the original
+        * gfp_flags.
+        */
+       if (current->bio_list &&
+           (!bio_list_empty(&current->bio_list[0]) ||
+            !bio_list_empty(&current->bio_list[1])) &&
+           bs->rescue_workqueue)
+               gfp_mask &= ~__GFP_DIRECT_RECLAIM;
+
+       p = mempool_alloc(&bs->bio_pool, gfp_mask);
+       if (!p && gfp_mask != saved_gfp) {
+               punt_bios_to_rescuer(bs);
+               gfp_mask = saved_gfp;
                p = mempool_alloc(&bs->bio_pool, gfp_mask);
-               if (!p && gfp_mask != saved_gfp) {
-                       punt_bios_to_rescuer(bs);
-                       gfp_mask = saved_gfp;
-                       p = mempool_alloc(&bs->bio_pool, gfp_mask);
-               }
-
-               front_pad = bs->front_pad;
-               inline_vecs = BIO_INLINE_VECS;
        }
-
        if (unlikely(!p))
                return NULL;
 
-       bio = p + front_pad;
-       bio_init(bio, NULL, 0);
+       bio = p + bs->front_pad;
+       if (nr_iovecs > BIO_INLINE_VECS) {
+               struct bio_vec *bvl = NULL;
 
-       if (nr_iovecs > inline_vecs) {
-               unsigned long idx = 0;
-
-               bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
+               bvl = bvec_alloc(&bs->bvec_pool, &nr_iovecs, gfp_mask);
                if (!bvl && gfp_mask != saved_gfp) {
                        punt_bios_to_rescuer(bs);
                        gfp_mask = saved_gfp;
-                       bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
+                       bvl = bvec_alloc(&bs->bvec_pool, &nr_iovecs, gfp_mask);
                }
-
                if (unlikely(!bvl))
                        goto err_free;
 
-               bio->bi_flags |= idx << BVEC_POOL_OFFSET;
-               bio->bi_max_vecs = bvec_nr_vecs(idx);
+               bio_init(bio, bvl, nr_iovecs);
        } else if (nr_iovecs) {
-               bvl = bio->bi_inline_vecs;
-               bio->bi_max_vecs = inline_vecs;
+               bio_init(bio, bio->bi_inline_vecs, BIO_INLINE_VECS);
+       } else {
+               bio_init(bio, NULL, 0);
        }
 
        bio->bi_pool = bs;
-       bio->bi_io_vec = bvl;
        return bio;
 
 err_free:
@@ -521,6 +468,31 @@ err_free:
 }
 EXPORT_SYMBOL(bio_alloc_bioset);
 
+/**
+ * bio_kmalloc - kmalloc a bio for I/O
+ * @gfp_mask:   the GFP_* mask given to the slab allocator
+ * @nr_iovecs: number of iovecs to pre-allocate
+ *
+ * Use kmalloc to allocate and initialize a bio.
+ *
+ * Returns: Pointer to new bio on success, NULL on failure.
+ */
+struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs)
+{
+       struct bio *bio;
+
+       if (nr_iovecs > UIO_MAXIOV)
+               return NULL;
+
+       bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask);
+       if (unlikely(!bio))
+               return NULL;
+       bio_init(bio, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs);
+       bio->bi_pool = NULL;
+       return bio;
+}
+EXPORT_SYMBOL(bio_kmalloc);
+
 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
 {
        unsigned long flags;
@@ -656,7 +628,7 @@ EXPORT_SYMBOL(bio_put);
  */
 void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 {
-       BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
+       WARN_ON_ONCE(bio->bi_pool && bio->bi_max_vecs);
 
        /*
         * most users will be overriding ->bi_bdev with a new target,
@@ -835,6 +807,39 @@ int bio_add_pc_page(struct request_queue *q, struct bio *bio,
 }
 EXPORT_SYMBOL(bio_add_pc_page);
 
+/**
+ * bio_add_zone_append_page - attempt to add page to zone-append bio
+ * @bio: destination bio
+ * @page: page to add
+ * @len: vec entry length
+ * @offset: vec entry offset
+ *
+ * Attempt to add a page to the bio_vec maplist of a bio that will be submitted
+ * for a zone-append request. This can fail for a number of reasons, such as the
+ * bio being full or the target block device is not a zoned block device or
+ * other limitations of the target block device. The target block device must
+ * allow bio's up to PAGE_SIZE, so it is always possible to add a single page
+ * to an empty bio.
+ *
+ * Returns: number of bytes added to the bio, or 0 in case of a failure.
+ */
+int bio_add_zone_append_page(struct bio *bio, struct page *page,
+                            unsigned int len, unsigned int offset)
+{
+       struct request_queue *q = bio->bi_bdev->bd_disk->queue;
+       bool same_page = false;
+
+       if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_ZONE_APPEND))
+               return 0;
+
+       if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
+               return 0;
+
+       return bio_add_hw_page(q, bio, page, len, offset,
+                              queue_max_zone_append_sectors(q), &same_page);
+}
+EXPORT_SYMBOL_GPL(bio_add_zone_append_page);
+
 /**
  * __bio_try_merge_page - try appending data to an existing bvec.
  * @bio: destination bio
@@ -946,13 +951,14 @@ EXPORT_SYMBOL_GPL(bio_release_pages);
 
 static int bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter)
 {
-       WARN_ON_ONCE(BVEC_POOL_IDX(bio) != 0);
+       WARN_ON_ONCE(bio->bi_max_vecs);
 
        bio->bi_vcnt = iter->nr_segs;
-       bio->bi_max_vecs = iter->nr_segs;
        bio->bi_io_vec = (struct bio_vec *)iter->bvec;
        bio->bi_iter.bi_bvec_done = iter->iov_offset;
        bio->bi_iter.bi_size = iter->count;
+       bio_set_flag(bio, BIO_NO_PAGE_REF);
+       bio_set_flag(bio, BIO_CLONED);
 
        iov_iter_advance(iter, iter->count);
        return 0;
@@ -1090,18 +1096,16 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
        if (iov_iter_is_bvec(iter)) {
                if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
                        return -EINVAL;
-               bio_iov_bvec_set(bio, iter);
-               bio_set_flag(bio, BIO_NO_PAGE_REF);
-               return 0;
-       } else {
-               do {
-                       if (bio_op(bio) == REQ_OP_ZONE_APPEND)
-                               ret = __bio_iov_append_get_pages(bio, iter);
-                       else
-                               ret = __bio_iov_iter_get_pages(bio, iter);
-               } while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
+               return bio_iov_bvec_set(bio, iter);
        }
 
+       do {
+               if (bio_op(bio) == REQ_OP_ZONE_APPEND)
+                       ret = __bio_iov_append_get_pages(bio, iter);
+               else
+                       ret = __bio_iov_iter_get_pages(bio, iter);
+       } while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
+
        /* don't account direct I/O as memory stall */
        bio_clear_flag(bio, BIO_WORKINGSET);
        return bio->bi_vcnt ? 0 : ret;
@@ -1508,7 +1512,7 @@ EXPORT_SYMBOL_GPL(bio_trim);
  */
 int biovec_init_pool(mempool_t *pool, int pool_entries)
 {
-       struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
+       struct biovec_slab *bp = bvec_slabs + ARRAY_SIZE(bvec_slabs) - 1;
 
        return mempool_init_slab_pool(pool, pool_entries, bp->slab);
 }
@@ -1614,31 +1618,19 @@ int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
 }
 EXPORT_SYMBOL(bioset_init_from_src);
 
-static void __init biovec_init_slabs(void)
+static int __init init_bio(void)
 {
        int i;
 
-       for (i = 0; i < BVEC_POOL_NR; i++) {
-               int size;
-               struct biovec_slab *bvs = bvec_slabs + i;
+       bio_integrity_init();
 
-               if (bvs->nr_vecs <= BIO_INLINE_VECS) {
-                       bvs->slab = NULL;
-                       continue;
-               }
+       for (i = 0; i < ARRAY_SIZE(bvec_slabs); i++) {
+               struct biovec_slab *bvs = bvec_slabs + i;
 
-               size = bvs->nr_vecs * sizeof(struct bio_vec);
-               bvs->slab = kmem_cache_create(bvs->name, size, 0,
-                                SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+               bvs->slab = kmem_cache_create(bvs->name,
+                               bvs->nr_vecs * sizeof(struct bio_vec), 0,
+                               SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
        }
-}
-
-static int __init init_bio(void)
-{
-       BUILD_BUG_ON(BIO_FLAG_LAST > BVEC_POOL_OFFSET);
-
-       bio_integrity_init();
-       biovec_init_slabs();
 
        if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
                panic("bio: can't allocate bios\n");