linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Overstreet <koverstreet@google.com>
To: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
	dm-devel@redhat.com
Cc: Kent Overstreet <koverstreet@google.com>,
	tj@kernel.org, axboe@kernel.dk, neilb@suse.de,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Subject: [PATCH v2 01/26] block: Convert integrity to bvec_alloc_bs(), and a bugfix
Date: Mon, 15 Oct 2012 13:09:04 -0700	[thread overview]
Message-ID: <1350331769-14856-31-git-send-email-koverstreet@google.com> (raw)
In-Reply-To: <1350331769-14856-1-git-send-email-koverstreet@google.com>

This adds a pointer to the bvec array to struct bio_integrity_payload,
instead of the bvecs always being inline; then the bvecs are allocated
with bvec_alloc_bs().

This is needed eventually for immutable bio vecs - immutable bvecs
aren't useful if we still have to copy them, hence the need for the
pointer. Less code is always nice too, though.

Also fix an amusing bug in bio_integrity_split() - struct bio_pair
doesn't have the integrity bvecs after the bio_integrity_payloads, so
there was a buffer overrun. The code was confusing pointers with arrays.

Signed-off-by: Kent Overstreet <koverstreet@google.com>
CC: Jens Axboe <axboe@kernel.dk>
CC: Martin K. Petersen <martin.petersen@oracle.com>
---
 fs/bio-integrity.c  | 124 +++++++++++++++++-----------------------------------
 include/linux/bio.h |   5 ++-
 2 files changed, 43 insertions(+), 86 deletions(-)

diff --git a/fs/bio-integrity.c b/fs/bio-integrity.c
index a3f28f3..1d64f7f 100644
--- a/fs/bio-integrity.c
+++ b/fs/bio-integrity.c
@@ -27,48 +27,11 @@
 #include <linux/workqueue.h>
 #include <linux/slab.h>
 
-struct integrity_slab {
-	struct kmem_cache *slab;
-	unsigned short nr_vecs;
-	char name[8];
-};
-
-#define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
-struct integrity_slab bip_slab[BIOVEC_NR_POOLS] __read_mostly = {
-	IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES),
-};
-#undef IS
+#define BIP_INLINE_VECS	4
 
+static struct kmem_cache *bip_slab;
 static struct workqueue_struct *kintegrityd_wq;
 
-static inline unsigned int vecs_to_idx(unsigned int nr)
-{
-	switch (nr) {
-	case 1:
-		return 0;
-	case 2 ... 4:
-		return 1;
-	case 5 ... 16:
-		return 2;
-	case 17 ... 64:
-		return 3;
-	case 65 ... 128:
-		return 4;
-	case 129 ... BIO_MAX_PAGES:
-		return 5;
-	default:
-		BUG();
-	}
-}
-
-static inline int use_bip_pool(unsigned int idx)
-{
-	if (idx == BIOVEC_MAX_IDX)
-		return 1;
-
-	return 0;
-}
-
 /**
  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  * @bio:	bio to attach integrity metadata to
@@ -84,37 +47,38 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 						  unsigned int nr_vecs)
 {
 	struct bio_integrity_payload *bip;
-	unsigned int idx = vecs_to_idx(nr_vecs);
 	struct bio_set *bs = bio->bi_pool;
+	unsigned long idx = BIO_POOL_NONE;
+	unsigned inline_vecs;
+
+	if (!bs) {
+		bip = kmalloc(sizeof(struct bio_integrity_payload) +
+			      sizeof(struct bio_vec) * nr_vecs, gfp_mask);
+		inline_vecs = nr_vecs;
+	} else {
+		bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
+		inline_vecs = BIP_INLINE_VECS;
+	}
 
-	if (!bs)
-		bs = fs_bio_set;
-
-	BUG_ON(bio == NULL);
-	bip = NULL;
+	if (unlikely(!bip))
+		return NULL;
 
-	/* Lower order allocations come straight from slab */
-	if (!use_bip_pool(idx))
-		bip = kmem_cache_alloc(bip_slab[idx].slab, gfp_mask);
+	memset(bip, 0, sizeof(struct bio_integrity_payload));
 
-	/* Use mempool if lower order alloc failed or max vecs were requested */
-	if (bip == NULL) {
-		idx = BIOVEC_MAX_IDX;  /* so we free the payload properly later */
-		bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
-
-		if (unlikely(bip == NULL)) {
-			printk(KERN_ERR "%s: could not alloc bip\n", __func__);
-			return NULL;
-		}
+	if (nr_vecs > inline_vecs) {
+		bip->bip_vec = bvec_alloc_bs(gfp_mask, nr_vecs, &idx, bs);
+		if (!bip->bip_vec)
+			goto err;
 	}
 
-	memset(bip, 0, sizeof(*bip));
-
 	bip->bip_slab = idx;
 	bip->bip_bio = bio;
 	bio->bi_integrity = bip;
 
 	return bip;
+err:
+	mempool_free(bip, bs->bio_integrity_pool);
+	return NULL;
 }
 EXPORT_SYMBOL(bio_integrity_alloc);
 
@@ -130,20 +94,19 @@ void bio_integrity_free(struct bio *bio)
 	struct bio_integrity_payload *bip = bio->bi_integrity;
 	struct bio_set *bs = bio->bi_pool;
 
-	if (!bs)
-		bs = fs_bio_set;
-
-	BUG_ON(bip == NULL);
-
 	/* A cloned bio doesn't own the integrity metadata */
 	if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY)
 	    && bip->bip_buf != NULL)
 		kfree(bip->bip_buf);
 
-	if (use_bip_pool(bip->bip_slab))
+	if (bs) {
+		if (bip->bip_slab != BIO_POOL_NONE)
+			bvec_free_bs(bs, bip->bip_vec, bip->bip_slab);
+
 		mempool_free(bip, bs->bio_integrity_pool);
-	else
-		kmem_cache_free(bip_slab[bip->bip_slab].slab, bip);
+	} else {
+		kfree(bip);
+	}
 
 	bio->bi_integrity = NULL;
 }
@@ -697,8 +660,8 @@ void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors)
 	bp->iv1 = bip->bip_vec[0];
 	bp->iv2 = bip->bip_vec[0];
 
-	bp->bip1.bip_vec[0] = bp->iv1;
-	bp->bip2.bip_vec[0] = bp->iv2;
+	bp->bip1.bip_vec = &bp->iv1;
+	bp->bip2.bip_vec = &bp->iv2;
 
 	bp->iv1.bv_len = sectors * bi->tuple_size;
 	bp->iv2.bv_offset += sectors * bi->tuple_size;
@@ -746,13 +709,10 @@ EXPORT_SYMBOL(bio_integrity_clone);
 
 int bioset_integrity_create(struct bio_set *bs, int pool_size)
 {
-	unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES);
-
 	if (bs->bio_integrity_pool)
 		return 0;
 
-	bs->bio_integrity_pool =
-		mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab);
+	bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
 
 	if (!bs->bio_integrity_pool)
 		return -1;
@@ -770,8 +730,6 @@ EXPORT_SYMBOL(bioset_integrity_free);
 
 void __init bio_integrity_init(void)
 {
-	unsigned int i;
-
 	/*
 	 * kintegrityd won't block much but may burn a lot of CPU cycles.
 	 * Make it highpri CPU intensive wq with max concurrency of 1.
@@ -781,14 +739,10 @@ void __init bio_integrity_init(void)
 	if (!kintegrityd_wq)
 		panic("Failed to create kintegrityd\n");
 
-	for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) {
-		unsigned int size;
-
-		size = sizeof(struct bio_integrity_payload)
-			+ bip_slab[i].nr_vecs * sizeof(struct bio_vec);
-
-		bip_slab[i].slab =
-			kmem_cache_create(bip_slab[i].name, size, 0,
-					  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
-	}
+	bip_slab = kmem_cache_create("bio_integrity_payload",
+				     sizeof(struct bio_integrity_payload) +
+				     sizeof(struct bio_vec) * BIP_INLINE_VECS,
+				     0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+	if (!bip_slab)
+		panic("Failed to create slab\n");
 }
diff --git a/include/linux/bio.h b/include/linux/bio.h
index c32ea0d..7873465 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -182,7 +182,10 @@ struct bio_integrity_payload {
 	unsigned short		bip_idx;	/* current bip_vec index */
 
 	struct work_struct	bip_work;	/* I/O completion */
-	struct bio_vec		bip_vec[0];	/* embedded bvec array */
+
+	struct bio_vec		*bip_vec;
+	struct bio_vec		bip_inline_vecs[0];/* embedded bvec array */
+
 };
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
 
-- 
1.7.12

  parent reply	other threads:[~2012-10-15 20:09 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-15 20:08 [PATCH v4 00/24] Prep work for immutable bio vecs Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 02/24] block: Refactor blk_update_request() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 05/24] block: Use bio_sectors() more consistently Kent Overstreet
2012-10-16  1:54   ` Ed Cashin
2012-10-15 20:08 ` [PATCH v4 06/24] block: Change bio_split() to respect the current value of bi_idx Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 07/24] block: Remove bi_idx references Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 08/24] block: Remove some unnecessary bi_vcnt usage Kent Overstreet
2012-11-06 11:19   ` Reddy, Sreekanth
     [not found]   ` <1350331769-14856-9-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-11-07  6:05     ` Reddy, Sreekanth
2012-10-15 20:08 ` [PATCH v4 10/24] raid10: Use bio_reset() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 11/24] raid1: use bio_reset() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 12/24] raid5: " Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 13/24] raid1: Refactor narrow_write_error() to not use bi_idx Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 14/24] block: Add bio_copy_data() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 15/24] pktcdvd: use bio_copy_data() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 16/24] pktcdvd: Use bio_reset() in disabled code to kill bi_idx usage Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 17/24] raid1: use bio_copy_data() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 18/24] bounce: Refactor __blk_queue_bounce to not use bi_io_vec Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 20/24] block: Convert some code to bio_for_each_segment_all() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 21/24] block: Add bio_alloc_pages() Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 22/24] block: Add an explicit bio flag for bios that own their bvec Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 24/24] block: Add BIO_SUBMITTED flag, kill BIO_CLONED Kent Overstreet
2012-10-15 20:08 ` [PATCH v4 1/2] block: Reorder struct bio_set Kent Overstreet
     [not found]   ` <1350331769-14856-26-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19  1:14     ` Tejun Heo
2012-10-15 20:09 ` [PATCH v4 2/2] block: Avoid deadlocks with bio allocation by stacking drivers Kent Overstreet
2012-10-15 20:09 ` [PATCH v4 1/2] block: Fix a buffer overrun in bio_integrity_split() Kent Overstreet
     [not found]   ` <1350331769-14856-28-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19 20:30     ` Tejun Heo
2012-10-22 15:39       ` Vivek Goyal
2012-10-24 16:34         ` Martin K. Petersen
     [not found]           ` <yq1mwzbbzi1.fsf-+q57XtR/GgMb6DWv4sQWN6xOck334EZe@public.gmane.org>
2012-10-24 16:42             ` Tejun Heo
     [not found]               ` <CAOS58YPFq_rt1Pw-v1XtX7-tnPGceXP5Chpp9JhkkMtnWnNQsg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-10-24 16:57                 ` Martin K. Petersen
2012-10-15 20:09 ` [PATCH v4 2/2] block: Convert integrity to bvec_alloc_bs() Kent Overstreet
     [not found]   ` <1350331769-14856-29-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19 18:11     ` Vivek Goyal
2012-10-24 16:54     ` Martin K. Petersen
2012-10-15 20:09 ` [PATCH v2 00/26] Prep work for immutable bio vecs Kent Overstreet
2012-10-15 20:09 ` Kent Overstreet [this message]
     [not found]   ` <1350331769-14856-31-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19 20:34     ` [PATCH v2 01/26] block: Convert integrity to bvec_alloc_bs(), and a bugfix Tejun Heo
     [not found]       ` <20121019203421.GT13370-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19 20:36         ` Tejun Heo
2012-10-15 20:09 ` [PATCH v2 02/26] block: Add bio_advance() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 03/26] block: Refactor blk_update_request() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 04/26] md: Convert md_trim_bio() to use bio_advance() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 05/26] block: Add bio_end() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 06/26] block: Use bio_sectors() more consistently Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 08/26] block: Remove bi_idx references Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 09/26] block: Remove some unnecessary bi_vcnt usage Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 10/26] block: Add submit_bio_wait(), remove from md Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 12/26] raid1: use bio_reset() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 13/26] raid5: " Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 14/26] raid1: Refactor narrow_write_error() to not use bi_idx Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 15/26] block: Add bio_copy_data() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 17/26] pktcdvd: Use bio_reset() in disabled code to kill bi_idx usage Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 18/26] raid1: use bio_copy_data() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 19/26] bounce: Refactor __blk_queue_bounce to not use bi_io_vec Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 20/26] block: Add bio_for_each_segment_all() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 22/26] block: Add bio_alloc_pages() Kent Overstreet
2012-10-15 20:09 ` [PATCH v2 23/26] raid1: use bio_alloc_pages() Kent Overstreet
     [not found] ` <1350331769-14856-1-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-15 20:08   ` [PATCH v4 01/24] block: Add bio_advance() Kent Overstreet
2012-10-15 20:08   ` [PATCH v4 03/24] md: Convert md_trim_bio() to use bio_advance() Kent Overstreet
2012-10-15 20:08   ` [PATCH v4 04/24] block: Add bio_end_sector() Kent Overstreet
2012-10-15 20:08   ` [PATCH v4 09/24] block: Add submit_bio_wait(), remove from md Kent Overstreet
2012-10-15 20:08   ` [PATCH v4 19/24] block: Add bio_for_each_segment_all() Kent Overstreet
2012-10-15 20:08   ` [PATCH v4 23/24] bio-integrity: Add explicit field for owner of bip_buf Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 07/26] block: Don't use bi_idx in bio_split() or require it to be 0 Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 11/26] raid10: Use bio_reset() Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 16/26] pktcdvd: use bio_copy_data() Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 21/26] block: Convert some code to bio_for_each_segment_all() Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 24/26] block: Add an explicit bio flag for bios that own their bvec Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 25/26] bio-integrity: Add explicit field for owner of bip_buf Kent Overstreet
2012-10-15 20:09   ` [PATCH v2 26/26] block: Add BIO_SUBMITTED flag, kill BIO_CLONED Kent Overstreet
2012-10-19  1:14   ` [PATCH v4 00/24] Prep work for immutable bio vecs Tejun Heo
     [not found]     ` <20121019011445.GJ13370-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-10-19 15:16       ` Mike Snitzer
  -- strict thread matches above, loose matches on Subject: below --
2012-09-11  0:22 [PATCH v2 00/26] " Kent Overstreet
2012-09-11  0:22 ` [PATCH v2 01/26] block: Convert integrity to bvec_alloc_bs(), and a bugfix Kent Overstreet
     [not found]   ` <1347322957-25260-2-git-send-email-koverstreet-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2012-09-20 21:53     ` 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=1350331769-14856-31-git-send-email-koverstreet@google.com \
    --to=koverstreet@google.com \
    --cc=axboe@kernel.dk \
    --cc=dm-devel@redhat.com \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=neilb@suse.de \
    --cc=tj@kernel.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).