1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 Google LLC
4 */
5
6 #include <linux/bio.h>
7 #include <linux/blkdev.h>
8 #include <linux/keyslot-manager.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11
12 #include "blk-crypto-internal.h"
13
14 static int num_prealloc_crypt_ctxs = 128;
15
16 module_param(num_prealloc_crypt_ctxs, int, 0444);
17 MODULE_PARM_DESC(num_prealloc_crypt_ctxs,
18 "Number of bio crypto contexts to preallocate");
19
20 static struct kmem_cache *bio_crypt_ctx_cache;
21 static mempool_t *bio_crypt_ctx_pool;
22
bio_crypt_ctx_init(void)23 int __init bio_crypt_ctx_init(void)
24 {
25 size_t i;
26
27 bio_crypt_ctx_cache = KMEM_CACHE(bio_crypt_ctx, 0);
28 if (!bio_crypt_ctx_cache)
29 return -ENOMEM;
30
31 bio_crypt_ctx_pool = mempool_create_slab_pool(num_prealloc_crypt_ctxs,
32 bio_crypt_ctx_cache);
33 if (!bio_crypt_ctx_pool)
34 return -ENOMEM;
35
36 /* This is assumed in various places. */
37 BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
38
39 /* Sanity check that no algorithm exceeds the defined limits. */
40 for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) {
41 BUG_ON(blk_crypto_modes[i].keysize > BLK_CRYPTO_MAX_KEY_SIZE);
42 BUG_ON(blk_crypto_modes[i].ivsize > BLK_CRYPTO_MAX_IV_SIZE);
43 }
44
45 return 0;
46 }
47
bio_crypt_alloc_ctx(gfp_t gfp_mask)48 struct bio_crypt_ctx *bio_crypt_alloc_ctx(gfp_t gfp_mask)
49 {
50 return mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
51 }
52 EXPORT_SYMBOL_GPL(bio_crypt_alloc_ctx);
53
bio_crypt_free_ctx(struct bio * bio)54 void bio_crypt_free_ctx(struct bio *bio)
55 {
56 mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
57 bio->bi_crypt_context = NULL;
58 }
59
bio_crypt_clone(struct bio * dst,struct bio * src,gfp_t gfp_mask)60 void bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
61 {
62 const struct bio_crypt_ctx *src_bc = src->bi_crypt_context;
63
64 bio_clone_skip_dm_default_key(dst, src);
65
66 /*
67 * If a bio is fallback_crypted, then it will be decrypted when
68 * bio_endio is called. As we only want the data to be decrypted once,
69 * copies of the bio must not have have a crypt context.
70 */
71 if (!src_bc || bio_crypt_fallback_crypted(src_bc))
72 return;
73
74 dst->bi_crypt_context = bio_crypt_alloc_ctx(gfp_mask);
75 *dst->bi_crypt_context = *src_bc;
76
77 if (src_bc->bc_keyslot >= 0)
78 keyslot_manager_get_slot(src_bc->bc_ksm, src_bc->bc_keyslot);
79 }
80 EXPORT_SYMBOL_GPL(bio_crypt_clone);
81
bio_crypt_should_process(struct request * rq)82 bool bio_crypt_should_process(struct request *rq)
83 {
84 struct bio *bio = rq->bio;
85
86 if (!bio || !bio->bi_crypt_context)
87 return false;
88
89 return rq->q->ksm == bio->bi_crypt_context->bc_ksm;
90 }
91 EXPORT_SYMBOL_GPL(bio_crypt_should_process);
92
93 /*
94 * Checks that two bio crypt contexts are compatible - i.e. that
95 * they are mergeable except for data_unit_num continuity.
96 */
bio_crypt_ctx_compatible(struct bio * b_1,struct bio * b_2)97 bool bio_crypt_ctx_compatible(struct bio *b_1, struct bio *b_2)
98 {
99 struct bio_crypt_ctx *bc1 = b_1->bi_crypt_context;
100 struct bio_crypt_ctx *bc2 = b_2->bi_crypt_context;
101
102 if (!bc1)
103 return !bc2;
104 return bc2 && bc1->bc_key == bc2->bc_key;
105 }
106
107 /*
108 * Checks that two bio crypt contexts are compatible, and also
109 * that their data_unit_nums are continuous (and can hence be merged)
110 * in the order b_1 followed by b_2.
111 */
bio_crypt_ctx_mergeable(struct bio * b_1,unsigned int b1_bytes,struct bio * b_2)112 bool bio_crypt_ctx_mergeable(struct bio *b_1, unsigned int b1_bytes,
113 struct bio *b_2)
114 {
115 struct bio_crypt_ctx *bc1 = b_1->bi_crypt_context;
116 struct bio_crypt_ctx *bc2 = b_2->bi_crypt_context;
117
118 if (!bio_crypt_ctx_compatible(b_1, b_2))
119 return false;
120
121 return !bc1 || bio_crypt_dun_is_contiguous(bc1, b1_bytes, bc2->bc_dun);
122 }
123
bio_crypt_ctx_release_keyslot(struct bio_crypt_ctx * bc)124 void bio_crypt_ctx_release_keyslot(struct bio_crypt_ctx *bc)
125 {
126 keyslot_manager_put_slot(bc->bc_ksm, bc->bc_keyslot);
127 bc->bc_ksm = NULL;
128 bc->bc_keyslot = -1;
129 }
130
bio_crypt_ctx_acquire_keyslot(struct bio_crypt_ctx * bc,struct keyslot_manager * ksm)131 int bio_crypt_ctx_acquire_keyslot(struct bio_crypt_ctx *bc,
132 struct keyslot_manager *ksm)
133 {
134 int slot = keyslot_manager_get_slot_for_key(ksm, bc->bc_key);
135
136 if (slot < 0)
137 return slot;
138
139 bc->bc_keyslot = slot;
140 bc->bc_ksm = ksm;
141 return 0;
142 }
143