diff options
Diffstat (limited to 'fs/bio-integrity.c')
-rw-r--r-- | fs/bio-integrity.c | 144 |
1 files changed, 54 insertions, 90 deletions
diff --git a/fs/bio-integrity.c b/fs/bio-integrity.c index a3f28f331b2b..8fb42916d8a2 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 | ||
30 | struct 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) } | ||
37 | struct 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 | ||
32 | static struct kmem_cache *bip_slab; | ||
42 | static struct workqueue_struct *kintegrityd_wq; | 33 | static struct workqueue_struct *kintegrityd_wq; |
43 | 34 | ||
44 | static 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 | |||
64 | static 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,37 +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 | bio->bi_integrity = bip; | 79 | bio->bi_integrity = bip; |
116 | 80 | ||
117 | return bip; | 81 | return bip; |
82 | err: | ||
83 | mempool_free(bip, bs->bio_integrity_pool); | ||
84 | return NULL; | ||
118 | } | 85 | } |
119 | EXPORT_SYMBOL(bio_integrity_alloc); | 86 | EXPORT_SYMBOL(bio_integrity_alloc); |
120 | 87 | ||
@@ -130,20 +97,18 @@ void bio_integrity_free(struct bio *bio) | |||
130 | struct bio_integrity_payload *bip = bio->bi_integrity; | 97 | struct bio_integrity_payload *bip = bio->bi_integrity; |
131 | struct bio_set *bs = bio->bi_pool; | 98 | struct bio_set *bs = bio->bi_pool; |
132 | 99 | ||
133 | if (!bs) | 100 | if (bip->bip_owns_buf) |
134 | bs = fs_bio_set; | ||
135 | |||
136 | BUG_ON(bip == NULL); | ||
137 | |||
138 | /* A cloned bio doesn't own the integrity metadata */ | ||
139 | if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY) | ||
140 | && bip->bip_buf != NULL) | ||
141 | kfree(bip->bip_buf); | 101 | kfree(bip->bip_buf); |
142 | 102 | ||
143 | if (use_bip_pool(bip->bip_slab)) | 103 | if (bs) { |
104 | if (bip->bip_slab != BIO_POOL_NONE) | ||
105 | bvec_free(bs->bvec_integrity_pool, bip->bip_vec, | ||
106 | bip->bip_slab); | ||
107 | |||
144 | mempool_free(bip, bs->bio_integrity_pool); | 108 | mempool_free(bip, bs->bio_integrity_pool); |
145 | else | 109 | } else { |
146 | kmem_cache_free(bip_slab[bip->bip_slab].slab, bip); | 110 | kfree(bip); |
111 | } | ||
147 | 112 | ||
148 | bio->bi_integrity = NULL; | 113 | bio->bi_integrity = NULL; |
149 | } | 114 | } |
@@ -419,6 +384,7 @@ int bio_integrity_prep(struct bio *bio) | |||
419 | return -EIO; | 384 | return -EIO; |
420 | } | 385 | } |
421 | 386 | ||
387 | bip->bip_owns_buf = 1; | ||
422 | bip->bip_buf = buf; | 388 | bip->bip_buf = buf; |
423 | bip->bip_size = len; | 389 | bip->bip_size = len; |
424 | bip->bip_sector = bio->bi_sector; | 390 | bip->bip_sector = bio->bi_sector; |
@@ -694,11 +660,11 @@ void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors) | |||
694 | bp->bio1.bi_integrity = &bp->bip1; | 660 | bp->bio1.bi_integrity = &bp->bip1; |
695 | bp->bio2.bi_integrity = &bp->bip2; | 661 | bp->bio2.bi_integrity = &bp->bip2; |
696 | 662 | ||
697 | bp->iv1 = bip->bip_vec[0]; | 663 | bp->iv1 = bip->bip_vec[bip->bip_idx]; |
698 | bp->iv2 = bip->bip_vec[0]; | 664 | bp->iv2 = bip->bip_vec[bip->bip_idx]; |
699 | 665 | ||
700 | bp->bip1.bip_vec[0] = bp->iv1; | 666 | bp->bip1.bip_vec = &bp->iv1; |
701 | bp->bip2.bip_vec[0] = bp->iv2; | 667 | bp->bip2.bip_vec = &bp->iv2; |
702 | 668 | ||
703 | bp->iv1.bv_len = sectors * bi->tuple_size; | 669 | bp->iv1.bv_len = sectors * bi->tuple_size; |
704 | bp->iv2.bv_offset += sectors * bi->tuple_size; | 670 | bp->iv2.bv_offset += sectors * bi->tuple_size; |
@@ -746,13 +712,14 @@ EXPORT_SYMBOL(bio_integrity_clone); | |||
746 | 712 | ||
747 | int bioset_integrity_create(struct bio_set *bs, int pool_size) | 713 | int bioset_integrity_create(struct bio_set *bs, int pool_size) |
748 | { | 714 | { |
749 | unsigned int max_slab = vecs_to_idx(BIO_MAX_PAGES); | ||
750 | |||
751 | if (bs->bio_integrity_pool) | 715 | if (bs->bio_integrity_pool) |
752 | return 0; | 716 | return 0; |
753 | 717 | ||
754 | bs->bio_integrity_pool = | 718 | bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab); |
755 | mempool_create_slab_pool(pool_size, bip_slab[max_slab].slab); | 719 | |
720 | bs->bvec_integrity_pool = biovec_create_pool(bs, pool_size); | ||
721 | if (!bs->bvec_integrity_pool) | ||
722 | return -1; | ||
756 | 723 | ||
757 | if (!bs->bio_integrity_pool) | 724 | if (!bs->bio_integrity_pool) |
758 | return -1; | 725 | return -1; |
@@ -765,13 +732,14 @@ void bioset_integrity_free(struct bio_set *bs) | |||
765 | { | 732 | { |
766 | if (bs->bio_integrity_pool) | 733 | if (bs->bio_integrity_pool) |
767 | mempool_destroy(bs->bio_integrity_pool); | 734 | mempool_destroy(bs->bio_integrity_pool); |
735 | |||
736 | if (bs->bvec_integrity_pool) | ||
737 | mempool_destroy(bs->bio_integrity_pool); | ||
768 | } | 738 | } |
769 | EXPORT_SYMBOL(bioset_integrity_free); | 739 | EXPORT_SYMBOL(bioset_integrity_free); |
770 | 740 | ||
771 | void __init bio_integrity_init(void) | 741 | void __init bio_integrity_init(void) |
772 | { | 742 | { |
773 | unsigned int i; | ||
774 | |||
775 | /* | 743 | /* |
776 | * kintegrityd won't block much but may burn a lot of CPU cycles. | 744 | * kintegrityd won't block much but may burn a lot of CPU cycles. |
777 | * Make it highpri CPU intensive wq with max concurrency of 1. | 745 | * Make it highpri CPU intensive wq with max concurrency of 1. |
@@ -781,14 +749,10 @@ void __init bio_integrity_init(void) | |||
781 | if (!kintegrityd_wq) | 749 | if (!kintegrityd_wq) |
782 | panic("Failed to create kintegrityd\n"); | 750 | panic("Failed to create kintegrityd\n"); |
783 | 751 | ||
784 | for (i = 0 ; i < BIOVEC_NR_POOLS ; i++) { | 752 | bip_slab = kmem_cache_create("bio_integrity_payload", |
785 | unsigned int size; | 753 | sizeof(struct bio_integrity_payload) + |
786 | 754 | sizeof(struct bio_vec) * BIP_INLINE_VECS, | |
787 | size = sizeof(struct bio_integrity_payload) | 755 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
788 | + bip_slab[i].nr_vecs * sizeof(struct bio_vec); | 756 | if (!bip_slab) |
789 | 757 | panic("Failed to create slab\n"); | |
790 | bip_slab[i].slab = | ||
791 | kmem_cache_create(bip_slab[i].name, size, 0, | ||
792 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); | ||
793 | } | ||
794 | } | 758 | } |