aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Overstreet <koverstreet@google.com>2012-10-12 18:29:33 -0400
committerKent Overstreet <koverstreet@google.com>2013-03-23 17:15:27 -0400
commit9f060e2231ca96ca94f2ffcff730acd72606b280 (patch)
tree4818da8b59010493810e04d7a6273707875dc73c
parent6fda981cafbf908acd11e1e636fec50e99d56a47 (diff)
block: Convert integrity to bvec_alloc_bs()
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(). Changed bvec_alloc_bs() and bvec_free_bs() to take a pointer to a mempool instead of the bioset, so that bio integrity can use a different mempool for its bvecs, and thus avoid a potential deadlock. This is 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, bio_integrity_alloc() was using fs_bio_set if no bio_set was specified. This was wrong - using the bio_set doesn't protect us from memory allocation failures, because we just used kmalloc for the bio_integrity_payload. But it does introduce the possibility of deadlock, if for some reason we weren't supposed to be using fs_bio_set. Signed-off-by: Kent Overstreet <koverstreet@google.com> CC: Jens Axboe <axboe@kernel.dk> CC: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--fs/bio-integrity.c132
-rw-r--r--fs/bio.c36
-rw-r--r--include/linux/bio.h8
3 files changed, 68 insertions, 108 deletions
diff --git a/fs/bio-integrity.c b/fs/bio-integrity.c
index 94fa1c562c0e..8c4c604c840d 100644
--- a/fs/bio-integrity.c
+++ b/fs/bio-integrity.c
@@ -27,48 +27,11 @@
27#include <linux/workqueue.h> 27#include <linux/workqueue.h>
28#include <linux/slab.h> 28#include <linux/slab.h>
29 29
30struct integrity_slab { 30#define BIP_INLINE_VECS 4
31 struct kmem_cache *slab;
32 unsigned short nr_vecs;
33 char name[8];
34};
35
36#define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
37struct integrity_slab bip_slab[BIOVEC_NR_POOLS] __read_mostly = {
38 IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES),
39};
40#undef IS
41 31
32static struct kmem_cache *bip_slab;
42static struct workqueue_struct *kintegrityd_wq; 33static struct workqueue_struct *kintegrityd_wq;
43 34
44static inline unsigned int vecs_to_idx(unsigned int nr)
45{
46 switch (nr) {
47 case 1:
48 return 0;
49 case 2 ... 4:
50 return 1;
51 case 5 ... 16:
52 return 2;
53 case 17 ... 64:
54 return 3;
55 case 65 ... 128:
56 return 4;
57 case 129 ... BIO_MAX_PAGES:
58 return 5;
59 default:
60 BUG();
61 }
62}
63
64static inline int use_bip_pool(unsigned int idx)
65{
66 if (idx == BIOVEC_MAX_IDX)
67 return 1;
68
69 return 0;
70}
71
72/** 35/**
73 * bio_integrity_alloc - Allocate integrity payload and attach it to bio 36 * bio_integrity_alloc - Allocate integrity payload and attach it to bio
74 * @bio: bio to attach integrity metadata to 37 * @bio: bio to attach integrity metadata to
@@ -84,38 +47,41 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
84 unsigned int nr_vecs) 47 unsigned int nr_vecs)
85{ 48{
86 struct bio_integrity_payload *bip; 49 struct bio_integrity_payload *bip;
87 unsigned int idx = vecs_to_idx(nr_vecs);
88 struct bio_set *bs = bio->bi_pool; 50 struct bio_set *bs = bio->bi_pool;
89 51 unsigned long idx = BIO_POOL_NONE;
90 if (!bs) 52 unsigned inline_vecs;
91 bs = fs_bio_set; 53
92 54 if (!bs) {
93 BUG_ON(bio == NULL); 55 bip = kmalloc(sizeof(struct bio_integrity_payload) +
94 bip = NULL; 56 sizeof(struct bio_vec) * nr_vecs, gfp_mask);
95 57 inline_vecs = nr_vecs;
96 /* Lower order allocations come straight from slab */ 58 } else {
97 if (!use_bip_pool(idx))
98 bip = kmem_cache_alloc(bip_slab[idx].slab, gfp_mask);
99
100 /* Use mempool if lower order alloc failed or max vecs were requested */
101 if (bip == NULL) {
102 idx = BIOVEC_MAX_IDX; /* so we free the payload properly later */
103 bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask); 59 bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
104 60 inline_vecs = BIP_INLINE_VECS;
105 if (unlikely(bip == NULL)) {
106 printk(KERN_ERR "%s: could not alloc bip\n", __func__);
107 return NULL;
108 }
109 } 61 }
110 62
63 if (unlikely(!bip))
64 return NULL;
65
111 memset(bip, 0, sizeof(*bip)); 66 memset(bip, 0, sizeof(*bip));
112 67
68 if (nr_vecs > inline_vecs) {
69 bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
70 bs->bvec_integrity_pool);
71 if (!bip->bip_vec)
72 goto err;
73 } else {
74 bip->bip_vec = bip->bip_inline_vecs;
75 }
76
113 bip->bip_slab = idx; 77 bip->bip_slab = idx;
114 bip->bip_bio = bio; 78 bip->bip_bio = bio;
115 bip->bip_vec = bip->bip_inline_vecs;
116 bio->bi_integrity = bip; 79 bio->bi_integrity = bip;
117 80
118 return bip; 81 return bip;
82err:
83 mempool_free(bip, bs->bio_integrity_pool);
84 return NULL;
119} 85}
120EXPORT_SYMBOL(bio_integrity_alloc); 86EXPORT_SYMBOL(bio_integrity_alloc);
121 87
@@ -131,20 +97,20 @@ void bio_integrity_free(struct bio *bio)
131 struct bio_integrity_payload *bip = bio->bi_integrity; 97 struct bio_integrity_payload *bip = bio->bi_integrity;
132 struct bio_set *bs = bio->bi_pool; 98 struct bio_set *bs = bio->bi_pool;
133 99
134 if (!bs)
135 bs = fs_bio_set;
136
137 BUG_ON(bip == NULL);
138
139 /* A cloned bio doesn't own the integrity metadata */ 100 /* A cloned bio doesn't own the integrity metadata */
140 if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY) 101 if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY)
141 && bip->bip_buf != NULL) 102 && bip->bip_buf != NULL)
142 kfree(bip->bip_buf); 103 kfree(bip->bip_buf);
143 104
144 if (use_bip_pool(bip->bip_slab)) 105 if (bs) {
106 if (bip->bip_slab != BIO_POOL_NONE)
107 bvec_free(bs->bvec_integrity_pool, bip->bip_vec,
108 bip->bip_slab);
109
145 mempool_free(bip, bs->bio_integrity_pool); 110 mempool_free(bip, bs->bio_integrity_pool);
146 else 111 } else {
147 kmem_cache_free(bip_slab[bip->bip_slab].slab, bip); 112 kfree(bip);
113 }
148 114
149 bio->bi_integrity = NULL; 115 bio->bi_integrity = NULL;
150} 116}
@@ -747,13 +713,14 @@ EXPORT_SYMBOL(bio_integrity_clone);
747 713
748int bioset_integrity_create(struct bio_set *bs, int pool_size) 714int bioset_integrity_create(struct bio_set *bs, int pool_size)
749{ 715{
750 unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES);
751
752 if (bs->bio_integrity_pool) 716 if (bs->bio_integrity_pool)
753 return 0; 717 return 0;
754 718
755 bs->bio_integrity_pool = 719 bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
756 mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab); 720
721 bs->bvec_integrity_pool = biovec_create_pool(bs, pool_size);
722 if (!bs->bvec_integrity_pool)
723 return -1;
757 724
758 if (!bs->bio_integrity_pool) 725 if (!bs->bio_integrity_pool)
759 return -1; 726 return -1;
@@ -766,13 +733,14 @@ void bioset_integrity_free(struct bio_set *bs)
766{ 733{
767 if (bs->bio_integrity_pool) 734 if (bs->bio_integrity_pool)
768 mempool_destroy(bs->bio_integrity_pool); 735 mempool_destroy(bs->bio_integrity_pool);
736
737 if (bs->bvec_integrity_pool)
738 mempool_destroy(bs->bio_integrity_pool);
769} 739}
770EXPORT_SYMBOL(bioset_integrity_free); 740EXPORT_SYMBOL(bioset_integrity_free);
771 741
772void __init bio_integrity_init(void) 742void __init bio_integrity_init(void)
773{ 743{
774 unsigned int i;
775
776 /* 744 /*
777 * kintegrityd won't block much but may burn a lot of CPU cycles. 745 * kintegrityd won't block much but may burn a lot of CPU cycles.
778 * Make it highpri CPU intensive wq with max concurrency of 1. 746 * Make it highpri CPU intensive wq with max concurrency of 1.
@@ -782,14 +750,10 @@ void __init bio_integrity_init(void)
782 if (!kintegrityd_wq) 750 if (!kintegrityd_wq)
783 panic("Failed to create kintegrityd\n"); 751 panic("Failed to create kintegrityd\n");
784 752
785 for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) { 753 bip_slab = kmem_cache_create("bio_integrity_payload",
786 unsigned int size; 754 sizeof(struct bio_integrity_payload) +
787 755 sizeof(struct bio_vec) * BIP_INLINE_VECS,
788 size = sizeof(struct bio_integrity_payload) 756 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
789 + bip_slab[i].nr_vecs * sizeof(struct bio_vec); 757 if (!bip_slab)
790 758 panic("Failed to create slab\n");
791 bip_slab[i].slab =
792 kmem_cache_create(bip_slab[i].name, size, 0,
793 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
794 }
795} 759}
diff --git a/fs/bio.c b/fs/bio.c
index 73b544709945..40aa96eae99f 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -160,12 +160,12 @@ unsigned int bvec_nr_vecs(unsigned short idx)
160 return bvec_slabs[idx].nr_vecs; 160 return bvec_slabs[idx].nr_vecs;
161} 161}
162 162
163void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx) 163void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
164{ 164{
165 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS); 165 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
166 166
167 if (idx == BIOVEC_MAX_IDX) 167 if (idx == BIOVEC_MAX_IDX)
168 mempool_free(bv, bs->bvec_pool); 168 mempool_free(bv, pool);
169 else { 169 else {
170 struct biovec_slab *bvs = bvec_slabs + idx; 170 struct biovec_slab *bvs = bvec_slabs + idx;
171 171
@@ -173,8 +173,8 @@ void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx)
173 } 173 }
174} 174}
175 175
176struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx, 176struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
177 struct bio_set *bs) 177 mempool_t *pool)
178{ 178{
179 struct bio_vec *bvl; 179 struct bio_vec *bvl;
180 180
@@ -210,7 +210,7 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
210 */ 210 */
211 if (*idx == BIOVEC_MAX_IDX) { 211 if (*idx == BIOVEC_MAX_IDX) {
212fallback: 212fallback:
213 bvl = mempool_alloc(bs->bvec_pool, gfp_mask); 213 bvl = mempool_alloc(pool, gfp_mask);
214 } else { 214 } else {
215 struct biovec_slab *bvs = bvec_slabs + *idx; 215 struct biovec_slab *bvs = bvec_slabs + *idx;
216 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO); 216 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
@@ -253,7 +253,7 @@ static void bio_free(struct bio *bio)
253 253
254 if (bs) { 254 if (bs) {
255 if (bio_has_allocated_vec(bio)) 255 if (bio_has_allocated_vec(bio))
256 bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio)); 256 bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
257 257
258 /* 258 /*
259 * If we have front padding, adjust the bio pointer before freeing 259 * If we have front padding, adjust the bio pointer before freeing
@@ -442,11 +442,11 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
442 bio_init(bio); 442 bio_init(bio);
443 443
444 if (nr_iovecs > inline_vecs) { 444 if (nr_iovecs > inline_vecs) {
445 bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs); 445 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
446 if (!bvl && gfp_mask != saved_gfp) { 446 if (!bvl && gfp_mask != saved_gfp) {
447 punt_bios_to_rescuer(bs); 447 punt_bios_to_rescuer(bs);
448 gfp_mask = saved_gfp; 448 gfp_mask = saved_gfp;
449 bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs); 449 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
450 } 450 }
451 451
452 if (unlikely(!bvl)) 452 if (unlikely(!bvl))
@@ -1661,20 +1661,11 @@ EXPORT_SYMBOL(bio_sector_offset);
1661 * create memory pools for biovec's in a bio_set. 1661 * create memory pools for biovec's in a bio_set.
1662 * use the global biovec slabs created for general use. 1662 * use the global biovec slabs created for general use.
1663 */ 1663 */
1664static int biovec_create_pools(struct bio_set *bs, int pool_entries) 1664mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
1665{ 1665{
1666 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX; 1666 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
1667 1667
1668 bs->bvec_pool = mempool_create_slab_pool(pool_entries, bp->slab); 1668 return mempool_create_slab_pool(pool_entries, bp->slab);
1669 if (!bs->bvec_pool)
1670 return -ENOMEM;
1671
1672 return 0;
1673}
1674
1675static void biovec_free_pools(struct bio_set *bs)
1676{
1677 mempool_destroy(bs->bvec_pool);
1678} 1669}
1679 1670
1680void bioset_free(struct bio_set *bs) 1671void bioset_free(struct bio_set *bs)
@@ -1685,8 +1676,10 @@ void bioset_free(struct bio_set *bs)
1685 if (bs->bio_pool) 1676 if (bs->bio_pool)
1686 mempool_destroy(bs->bio_pool); 1677 mempool_destroy(bs->bio_pool);
1687 1678
1679 if (bs->bvec_pool)
1680 mempool_destroy(bs->bvec_pool);
1681
1688 bioset_integrity_free(bs); 1682 bioset_integrity_free(bs);
1689 biovec_free_pools(bs);
1690 bio_put_slab(bs); 1683 bio_put_slab(bs);
1691 1684
1692 kfree(bs); 1685 kfree(bs);
@@ -1731,7 +1724,8 @@ struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
1731 if (!bs->bio_pool) 1724 if (!bs->bio_pool)
1732 goto bad; 1725 goto bad;
1733 1726
1734 if (biovec_create_pools(bs, pool_size)) 1727 bs->bvec_pool = biovec_create_pool(bs, pool_size);
1728 if (!bs->bvec_pool)
1735 goto bad; 1729 goto bad;
1736 1730
1737 bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0); 1731 bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 81004fdcc277..669b1cb18fee 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -213,6 +213,7 @@ extern void bio_pair_release(struct bio_pair *dbio);
213 213
214extern struct bio_set *bioset_create(unsigned int, unsigned int); 214extern struct bio_set *bioset_create(unsigned int, unsigned int);
215extern void bioset_free(struct bio_set *); 215extern void bioset_free(struct bio_set *);
216extern mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries);
216 217
217extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *); 218extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
218extern void bio_put(struct bio *); 219extern void bio_put(struct bio *);
@@ -288,8 +289,8 @@ extern struct bio *bio_copy_user_iov(struct request_queue *,
288 int, int, gfp_t); 289 int, int, gfp_t);
289extern int bio_uncopy_user(struct bio *); 290extern int bio_uncopy_user(struct bio *);
290void zero_fill_bio(struct bio *bio); 291void zero_fill_bio(struct bio *bio);
291extern struct bio_vec *bvec_alloc_bs(gfp_t, int, unsigned long *, struct bio_set *); 292extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
292extern void bvec_free_bs(struct bio_set *, struct bio_vec *, unsigned int); 293extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
293extern unsigned int bvec_nr_vecs(unsigned short idx); 294extern unsigned int bvec_nr_vecs(unsigned short idx);
294 295
295#ifdef CONFIG_BLK_CGROUP 296#ifdef CONFIG_BLK_CGROUP
@@ -511,10 +512,11 @@ struct bio_set {
511 unsigned int front_pad; 512 unsigned int front_pad;
512 513
513 mempool_t *bio_pool; 514 mempool_t *bio_pool;
515 mempool_t *bvec_pool;
514#if defined(CONFIG_BLK_DEV_INTEGRITY) 516#if defined(CONFIG_BLK_DEV_INTEGRITY)
515 mempool_t *bio_integrity_pool; 517 mempool_t *bio_integrity_pool;
518 mempool_t *bvec_integrity_pool;
516#endif 519#endif
517 mempool_t *bvec_pool;
518 520
519 /* 521 /*
520 * Deadlock avoidance for stacking block drivers: see comments in 522 * Deadlock avoidance for stacking block drivers: see comments in