linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Overstreet <koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
To: linux-bcache-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	dm-devel-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Cc: Kent Overstreet
	<koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
	tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	vgoyal-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	mpatocka-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	bharrosh-C4P08NqkoRlBDgjK7y7TUQ@public.gmane.org,
	Jens Axboe <axboe-tSWWG44O7X1aa/9Udqfwiw@public.gmane.org>
Subject: [PATCH v7 6/9] block: Consolidate bio_alloc_bioset(), bio_kmalloc()
Date: Tue, 28 Aug 2012 10:37:33 -0700	[thread overview]
Message-ID: <1346175456-1572-7-git-send-email-koverstreet@google.com> (raw)
In-Reply-To: <1346175456-1572-1-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>

Previously, bio_kmalloc() and bio_alloc_bioset() behaved slightly
different because there was some almost-duplicated code - this fixes
some of that.

The important change is that previously bio_kmalloc() always set
bi_io_vec = bi_inline_vecs, even if nr_iovecs == 0 - unlike
bio_alloc_bioset(). This would cause bio_has_data() to return true; I
don't know if this resulted in any actual bugs but it was certainly
wrong.

bio_kmalloc() and bio_alloc_bioset() also have different arbitrary
limits on nr_iovecs - 1024 (UIO_MAXIOV) for bio_kmalloc(), 256
(BIO_MAX_PAGES) for bio_alloc_bioset(). This patch doesn't fix that, but
at least they're enforced closer together and hopefully they will be
fixed in a later patch.

This'll also help with some future cleanups - there are a fair number of
functions that allocate bios (e.g. bio_clone()), and now they don't have
to be duplicated for bio_alloc(), bio_alloc_bioset(), and bio_kmalloc().

Signed-off-by: Kent Overstreet <koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
CC: Jens Axboe <axboe-tSWWG44O7X1aa/9Udqfwiw@public.gmane.org>
v7: Re-add dropped comments, improv patch description
---
 fs/bio.c            | 111 ++++++++++++++++++----------------------------------
 include/linux/bio.h |  16 ++++++--
 2 files changed, 49 insertions(+), 78 deletions(-)

diff --git a/fs/bio.c b/fs/bio.c
index ce829c8..5e947d3 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -55,6 +55,7 @@ static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  * IO code that does not need private memory pools.
  */
 struct bio_set *fs_bio_set;
+EXPORT_SYMBOL(fs_bio_set);
 
 /*
  * Our slab pool management
@@ -291,39 +292,58 @@ EXPORT_SYMBOL(bio_reset);
  * @bs:		the bio_set to allocate from.
  *
  * Description:
- *   bio_alloc_bioset will try its own mempool to satisfy the allocation.
- *   If %__GFP_WAIT is set then we will block on the internal pool waiting
- *   for a &struct bio to become free.
- **/
+ *   If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
+ *   backed by the @bs's mempool.
+ *
+ *   When @bs is not NULL, 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 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.
+ *
+ *   RETURNS:
+ *   Pointer to new bio on success, NULL on failure.
+ */
 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
 {
+	unsigned front_pad;
+	unsigned inline_vecs;
 	unsigned long idx = BIO_POOL_NONE;
 	struct bio_vec *bvl = NULL;
 	struct bio *bio;
 	void *p;
 
-	p = mempool_alloc(bs->bio_pool, gfp_mask);
+	if (!bs) {
+		if (nr_iovecs > UIO_MAXIOV)
+			return NULL;
+
+		p = kmalloc(sizeof(struct bio) +
+			    nr_iovecs * sizeof(struct bio_vec),
+			    gfp_mask);
+		front_pad = 0;
+		inline_vecs = nr_iovecs;
+	} else {
+		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 + bs->front_pad;
 
+	bio = p + front_pad;
 	bio_init(bio);
-	bio->bi_pool = bs;
-
-	if (unlikely(!nr_iovecs))
-		goto out_set;
 
-	if (nr_iovecs <= BIO_INLINE_VECS) {
-		bvl = bio->bi_inline_vecs;
-		nr_iovecs = BIO_INLINE_VECS;
-	} else {
+	if (nr_iovecs > inline_vecs) {
 		bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
 		if (unlikely(!bvl))
 			goto err_free;
-
-		nr_iovecs = bvec_nr_vecs(idx);
+	} else if (nr_iovecs) {
+		bvl = bio->bi_inline_vecs;
 	}
-out_set:
+
+	bio->bi_pool = bs;
 	bio->bi_flags |= idx << BIO_POOL_OFFSET;
 	bio->bi_max_vecs = nr_iovecs;
 	bio->bi_io_vec = bvl;
@@ -335,63 +355,6 @@ err_free:
 }
 EXPORT_SYMBOL(bio_alloc_bioset);
 
-/**
- *	bio_alloc - allocate a new bio, memory pool backed
- *	@gfp_mask: allocation mask to use
- *	@nr_iovecs: number of iovecs
- *
- *	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 and @bio_kmalloc.
- *
- *	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 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 livelocks under memory pressure.
- *
- *	RETURNS:
- *	Pointer to new bio on success, NULL on failure.
- */
-struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
-{
-	return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
-}
-EXPORT_SYMBOL(bio_alloc);
-
-/**
- * bio_kmalloc - allocate a bio for I/O using kmalloc()
- * @gfp_mask:   the GFP_ mask given to the slab allocator
- * @nr_iovecs:	number of iovecs to pre-allocate
- *
- * Description:
- *   Allocate a new bio with @nr_iovecs bvecs.  If @gfp_mask contains
- *   %__GFP_WAIT, the allocation is guaranteed to succeed.
- *
- **/
-struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
-{
-	struct bio *bio;
-
-	if (nr_iovecs > UIO_MAXIOV)
-		return NULL;
-
-	bio = kmalloc(sizeof(struct bio) + nr_iovecs * sizeof(struct bio_vec),
-		      gfp_mask);
-	if (unlikely(!bio))
-		return NULL;
-
-	bio_init(bio);
-	bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
-	bio->bi_max_vecs = nr_iovecs;
-	bio->bi_io_vec = bio->bi_inline_vecs;
-	bio->bi_pool = NULL;
-
-	return bio;
-}
-EXPORT_SYMBOL(bio_kmalloc);
-
 void zero_fill_bio(struct bio *bio)
 {
 	unsigned long flags;
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 393c87e..8bfd572 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -212,12 +212,22 @@ extern void bio_pair_release(struct bio_pair *dbio);
 extern struct bio_set *bioset_create(unsigned int, unsigned int);
 extern void bioset_free(struct bio_set *);
 
-extern struct bio *bio_alloc(gfp_t, unsigned int);
-extern struct bio *bio_kmalloc(gfp_t, unsigned int);
 extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
 extern void bio_put(struct bio *);
 extern void bio_free(struct bio *);
 
+extern struct bio_set *fs_bio_set;
+
+static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
+{
+	return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
+}
+
+static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
+{
+	return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
+}
+
 extern void bio_endio(struct bio *, int);
 struct request_queue;
 extern int bio_phys_segments(struct request_queue *, struct bio *);
@@ -305,8 +315,6 @@ struct biovec_slab {
 	struct kmem_cache *slab;
 };
 
-extern struct bio_set *fs_bio_set;
-
 /*
  * a small number of entries is fine, not going to be performance critical.
  * basically we just need to survive
-- 
1.7.12

  parent reply	other threads:[~2012-08-28 17:37 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-28 17:37 [PATCH v7 0/9] Block cleanups, deadlock fix Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 1/9] block: Generalized bio pool freeing Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 2/9] dm: Use bioset's front_pad for dm_rq_clone_bio_info Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 3/9] block: Add bio_reset() Kent Overstreet
     [not found]   ` <1346175456-1572-4-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:31     ` Tejun Heo
2012-08-28 22:17       ` Kent Overstreet
     [not found]         ` <20120828221715.GD1048-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-08-28 22:53           ` Kent Overstreet
2012-09-01  2:23           ` Tejun Heo
2012-09-05 20:13             ` Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 4/9] pktcdvd: Switch to bio_kmalloc() Kent Overstreet
     [not found]   ` <1346175456-1572-5-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:32     ` Tejun Heo
2012-08-28 22:24       ` Kent Overstreet
     [not found]       ` <20120828203247.GC24608-RcKxWJ4Cfj1J2suj2OqeGauc2jM2gXBXkQQo+JxHRPFibQn6LdNjmg@public.gmane.org>
2012-09-04  9:05         ` Jiri Kosina
     [not found]           ` <alpine.LRH.2.00.1209041104020.26301-1ReQVI26iDCaZKY3DrU6dA@public.gmane.org>
2012-09-05 19:44             ` Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 5/9] block: Kill bi_destructor Kent Overstreet
     [not found]   ` <1346175456-1572-6-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:36     ` Tejun Heo
2012-08-28 22:07       ` Kent Overstreet
     [not found] ` <1346175456-1572-1-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 17:37   ` Kent Overstreet [this message]
     [not found]     ` <1346175456-1572-7-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:41       ` [PATCH v7 6/9] block: Consolidate bio_alloc_bioset(), bio_kmalloc() Tejun Heo
     [not found]         ` <20120828204148.GE24608-RcKxWJ4Cfj1J2suj2OqeGauc2jM2gXBXkQQo+JxHRPFibQn6LdNjmg@public.gmane.org>
2012-08-28 22:03           ` Kent Overstreet
2012-09-01  2:17             ` Tejun Heo
2012-08-28 17:37 ` [PATCH v7 7/9] block: Add bio_clone_bioset(), bio_clone_kmalloc() Kent Overstreet
     [not found]   ` <1346175456-1572-8-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:44     ` Tejun Heo
2012-08-28 22:05       ` Kent Overstreet
     [not found]         ` <20120828220532.GB1048-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-09-01  2:19           ` Tejun Heo
2012-08-28 17:37 ` [PATCH v7 8/9] block: Reorder struct bio_set Kent Overstreet
2012-08-28 17:37 ` [PATCH v7 9/9] block: Avoid deadlocks with bio allocation by stacking drivers Kent Overstreet
     [not found]   ` <1346175456-1572-10-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-28 20:49     ` Tejun Heo
2012-08-28 22:28       ` Kent Overstreet
     [not found]         ` <20120828222800.GG1048-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-08-28 23:01           ` Kent Overstreet
     [not found]             ` <20120828230108.GI1048-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-08-29  1:31               ` Vivek Goyal
2012-08-29  3:25                 ` Kent Overstreet
     [not found]                   ` <20120829032558.GA22214-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-08-29 12:57                     ` Vivek Goyal
     [not found]                       ` <20120829125759.GB12504-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-08-29 14:39                         ` [dm-devel] " Alasdair G Kergon
     [not found]                           ` <20120829143913.GA5500-FDJ95KluN3Z0klwcnFlA1dvLeJWuRmrY@public.gmane.org>
2012-08-29 16:26                             ` Kent Overstreet
2012-08-29 21:01                               ` John Stoffel
     [not found]                                 ` <20542.33557.833272.561494-QCwP04CIH+34g5BaSWARrrNAH6kLmebB@public.gmane.org>
2012-08-29 21:08                                   ` Kent Overstreet
2012-08-28 22:06     ` Vivek Goyal
     [not found]       ` <20120828220610.GH31674-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-08-28 22:23         ` Kent Overstreet
2012-08-29 16:24   ` Mikulas Patocka
2012-08-29 16:50     ` Kent Overstreet
     [not found]       ` <20120829165006.GB20312-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-29 16:57         ` [dm-devel] " Alasdair G Kergon
2012-08-29 17:07         ` Vivek Goyal
     [not found]           ` <20120829170711.GC12504-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-08-29 17:13             ` Kent Overstreet
     [not found]               ` <20120829171345.GC20312-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-08-29 17:23                 ` [dm-devel] " Alasdair G Kergon
2012-08-29 17:32                   ` Kent Overstreet
2012-08-30 22:07                 ` Vivek Goyal
     [not found]                   ` <20120830220745.GI27257-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-08-31  1:43                     ` Kent Overstreet
2012-08-31  1:55                       ` Kent Overstreet
2012-08-31 15:01                       ` Vivek Goyal
     [not found]                         ` <20120831150159.GB13483-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-09-03  1:26                           ` Kent Overstreet
     [not found]                       ` <20120831014359.GB15218-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-09-03 20:41                         ` Mikulas Patocka
     [not found]                           ` <Pine.LNX.4.64.1209031638110.15620-e+HWlsje6Db1wF9wiOj0lkEOCMrvLtNR@public.gmane.org>
2012-09-04  3:41                             ` Kent Overstreet
     [not found]                               ` <20120904034100.GA21602-jC9Py7bek1znysI04z7BkA@public.gmane.org>
2012-09-04 18:55                                 ` Tejun Heo
2012-09-04 19:01                                   ` Tejun Heo
     [not found]                                     ` <20120904190119.GD3638-RcKxWJ4Cfj1J2suj2OqeGauc2jM2gXBXkQQo+JxHRPFibQn6LdNjmg@public.gmane.org>
2012-09-04 19:43                                       ` Kent Overstreet
2012-09-04 19:42                                   ` Kent Overstreet
     [not found]                                     ` <20120904194237.GC25236-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-09-04 21:03                                       ` Tejun Heo
2012-09-04 19:26                                 ` Mikulas Patocka
     [not found]                                   ` <Pine.LNX.4.64.1209041509160.22689-e+HWlsje6Db1wF9wiOj0lkEOCMrvLtNR@public.gmane.org>
2012-09-04 19:39                                     ` Vivek Goyal
2012-09-04 19:51                                     ` [PATCH] dm: Use bioset's front_pad for dm_target_io Kent Overstreet
     [not found]                                       ` <20120904195156.GE25236-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-09-04 21:20                                         ` Tejun Heo
2012-09-11 19:28                                         ` [PATCH 2] " Mikulas Patocka
     [not found]                                           ` <Pine.LNX.4.64.1209111349210.23947-e+HWlsje6Db1wF9wiOj0lkEOCMrvLtNR@public.gmane.org>
2012-09-11 19:50                                             ` Kent Overstreet
     [not found]                                               ` <20120911195029.GJ19739-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-09-12 22:31                                                 ` Mikulas Patocka
     [not found]                                                   ` <Pine.LNX.4.64.1209121831110.21106-e+HWlsje6Db1wF9wiOj0lkEOCMrvLtNR@public.gmane.org>
2012-09-14 23:09                                                     ` [dm-devel] " Alasdair G Kergon
2012-09-01  2:13                     ` [PATCH v7 9/9] block: Avoid deadlocks with bio allocation by stacking drivers Tejun Heo
     [not found]                       ` <20120901021348.GB19535-RcKxWJ4Cfj1J2suj2OqeGauc2jM2gXBXkQQo+JxHRPFibQn6LdNjmg@public.gmane.org>
2012-09-03  1:34                         ` [PATCH v2] " Kent Overstreet
2012-09-04 15:00                         ` [PATCH v7 9/9] " Vivek Goyal
2012-09-03  0:49                     ` Dave Chinner
2012-09-03  1:17                       ` Kent Overstreet
2012-09-04 13:54                       ` Vivek Goyal
     [not found]                         ` <20120904135422.GC13768-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2012-09-04 18:26                           ` Tejun Heo
2012-09-05  3:57                             ` Dave Chinner
2012-09-05  4:37                               ` Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346175456-1572-7-git-send-email-koverstreet@google.com \
    --to=koverstreet-hpiqsd4aklfqt0dzr+alfa@public.gmane.org \
    --cc=axboe-tSWWG44O7X1aa/9Udqfwiw@public.gmane.org \
    --cc=bharrosh-C4P08NqkoRlBDgjK7y7TUQ@public.gmane.org \
    --cc=dm-devel-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=linux-bcache-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=mpatocka-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=vgoyal-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).