blk-crypto-fallback.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. /*
  6. * Refer to Documentation/block/inline-encryption.rst for detailed explanation.
  7. */
  8. #define pr_fmt(fmt) "blk-crypto-fallback: " fmt
  9. #include <crypto/skcipher.h>
  10. #include <linux/blk-crypto.h>
  11. #include <linux/blk-crypto-profile.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/crypto.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/scatterlist.h>
  18. #include <trace/events/block.h>
  19. #include "blk-cgroup.h"
  20. #include "blk-crypto-internal.h"
  21. static unsigned int num_prealloc_bounce_pg = 32;
  22. module_param(num_prealloc_bounce_pg, uint, 0);
  23. MODULE_PARM_DESC(num_prealloc_bounce_pg,
  24. "Number of preallocated bounce pages for the blk-crypto crypto API fallback");
  25. static unsigned int blk_crypto_num_keyslots = 100;
  26. module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0);
  27. MODULE_PARM_DESC(num_keyslots,
  28. "Number of keyslots for the blk-crypto crypto API fallback");
  29. static unsigned int num_prealloc_fallback_crypt_ctxs = 128;
  30. module_param(num_prealloc_fallback_crypt_ctxs, uint, 0);
  31. MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs,
  32. "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback");
  33. struct bio_fallback_crypt_ctx {
  34. struct bio_crypt_ctx crypt_ctx;
  35. /*
  36. * Copy of the bvec_iter when this bio was submitted.
  37. * We only want to en/decrypt the part of the bio as described by the
  38. * bvec_iter upon submission because bio might be split before being
  39. * resubmitted
  40. */
  41. struct bvec_iter crypt_iter;
  42. union {
  43. struct {
  44. struct work_struct work;
  45. struct bio *bio;
  46. };
  47. struct {
  48. void *bi_private_orig;
  49. bio_end_io_t *bi_end_io_orig;
  50. };
  51. };
  52. };
  53. static struct kmem_cache *bio_fallback_crypt_ctx_cache;
  54. static mempool_t *bio_fallback_crypt_ctx_pool;
  55. /*
  56. * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate
  57. * all of a mode's tfms when that mode starts being used. Since each mode may
  58. * need all the keyslots at some point, each mode needs its own tfm for each
  59. * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to
  60. * match the behavior of real inline encryption hardware (which only supports a
  61. * single encryption context per keyslot), we only allow one tfm per keyslot to
  62. * be used at a time - the rest of the unused tfms have their keys cleared.
  63. */
  64. static DEFINE_MUTEX(tfms_init_lock);
  65. static bool tfms_inited[BLK_ENCRYPTION_MODE_MAX];
  66. static struct blk_crypto_fallback_keyslot {
  67. enum blk_crypto_mode_num crypto_mode;
  68. struct crypto_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX];
  69. } *blk_crypto_keyslots;
  70. static struct blk_crypto_profile *blk_crypto_fallback_profile;
  71. static struct workqueue_struct *blk_crypto_wq;
  72. static mempool_t *blk_crypto_bounce_page_pool;
  73. static struct bio_set crypto_bio_split;
  74. /*
  75. * This is the key we set when evicting a keyslot. This *should* be the all 0's
  76. * key, but AES-XTS rejects that key, so we use some random bytes instead.
  77. */
  78. static u8 blank_key[BLK_CRYPTO_MAX_KEY_SIZE];
  79. static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
  80. {
  81. struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
  82. enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode;
  83. int err;
  84. WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID);
  85. /* Clear the key in the skcipher */
  86. err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], blank_key,
  87. blk_crypto_modes[crypto_mode].keysize);
  88. WARN_ON(err);
  89. slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID;
  90. }
  91. static int
  92. blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
  93. const struct blk_crypto_key *key,
  94. unsigned int slot)
  95. {
  96. struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
  97. const enum blk_crypto_mode_num crypto_mode =
  98. key->crypto_cfg.crypto_mode;
  99. int err;
  100. if (crypto_mode != slotp->crypto_mode &&
  101. slotp->crypto_mode != BLK_ENCRYPTION_MODE_INVALID)
  102. blk_crypto_fallback_evict_keyslot(slot);
  103. slotp->crypto_mode = crypto_mode;
  104. err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], key->raw,
  105. key->size);
  106. if (err) {
  107. blk_crypto_fallback_evict_keyslot(slot);
  108. return err;
  109. }
  110. return 0;
  111. }
  112. static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile,
  113. const struct blk_crypto_key *key,
  114. unsigned int slot)
  115. {
  116. blk_crypto_fallback_evict_keyslot(slot);
  117. return 0;
  118. }
  119. static const struct blk_crypto_ll_ops blk_crypto_fallback_ll_ops = {
  120. .keyslot_program = blk_crypto_fallback_keyslot_program,
  121. .keyslot_evict = blk_crypto_fallback_keyslot_evict,
  122. };
  123. static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio)
  124. {
  125. struct bio *src_bio = enc_bio->bi_private;
  126. int i;
  127. for (i = 0; i < enc_bio->bi_vcnt; i++)
  128. mempool_free(enc_bio->bi_io_vec[i].bv_page,
  129. blk_crypto_bounce_page_pool);
  130. src_bio->bi_status = enc_bio->bi_status;
  131. bio_uninit(enc_bio);
  132. kfree(enc_bio);
  133. bio_endio(src_bio);
  134. }
  135. static struct bio *blk_crypto_fallback_clone_bio(struct bio *bio_src)
  136. {
  137. unsigned int nr_segs = bio_segments(bio_src);
  138. struct bvec_iter iter;
  139. struct bio_vec bv;
  140. struct bio *bio;
  141. bio = bio_kmalloc(nr_segs, GFP_NOIO);
  142. if (!bio)
  143. return NULL;
  144. bio_init(bio, bio_src->bi_bdev, bio->bi_inline_vecs, nr_segs,
  145. bio_src->bi_opf);
  146. if (bio_flagged(bio_src, BIO_REMAPPED))
  147. bio_set_flag(bio, BIO_REMAPPED);
  148. bio->bi_ioprio = bio_src->bi_ioprio;
  149. bio->bi_write_hint = bio_src->bi_write_hint;
  150. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  151. bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
  152. bio_for_each_segment(bv, bio_src, iter)
  153. bio->bi_io_vec[bio->bi_vcnt++] = bv;
  154. bio_clone_blkg_association(bio, bio_src);
  155. return bio;
  156. }
  157. static bool
  158. blk_crypto_fallback_alloc_cipher_req(struct blk_crypto_keyslot *slot,
  159. struct skcipher_request **ciph_req_ret,
  160. struct crypto_wait *wait)
  161. {
  162. struct skcipher_request *ciph_req;
  163. const struct blk_crypto_fallback_keyslot *slotp;
  164. int keyslot_idx = blk_crypto_keyslot_index(slot);
  165. slotp = &blk_crypto_keyslots[keyslot_idx];
  166. ciph_req = skcipher_request_alloc(slotp->tfms[slotp->crypto_mode],
  167. GFP_NOIO);
  168. if (!ciph_req)
  169. return false;
  170. skcipher_request_set_callback(ciph_req,
  171. CRYPTO_TFM_REQ_MAY_BACKLOG |
  172. CRYPTO_TFM_REQ_MAY_SLEEP,
  173. crypto_req_done, wait);
  174. *ciph_req_ret = ciph_req;
  175. return true;
  176. }
  177. static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr)
  178. {
  179. struct bio *bio = *bio_ptr;
  180. unsigned int i = 0;
  181. unsigned int num_sectors = 0;
  182. struct bio_vec bv;
  183. struct bvec_iter iter;
  184. bio_for_each_segment(bv, bio, iter) {
  185. num_sectors += bv.bv_len >> SECTOR_SHIFT;
  186. if (++i == BIO_MAX_VECS)
  187. break;
  188. }
  189. if (num_sectors < bio_sectors(bio)) {
  190. struct bio *split_bio;
  191. split_bio = bio_split(bio, num_sectors, GFP_NOIO,
  192. &crypto_bio_split);
  193. if (!split_bio) {
  194. bio->bi_status = BLK_STS_RESOURCE;
  195. return false;
  196. }
  197. bio_chain(split_bio, bio);
  198. trace_block_split(split_bio, bio->bi_iter.bi_sector);
  199. submit_bio_noacct(bio);
  200. *bio_ptr = split_bio;
  201. }
  202. return true;
  203. }
  204. union blk_crypto_iv {
  205. __le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  206. u8 bytes[BLK_CRYPTO_MAX_IV_SIZE];
  207. };
  208. static void blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  209. union blk_crypto_iv *iv)
  210. {
  211. int i;
  212. for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++)
  213. iv->dun[i] = cpu_to_le64(dun[i]);
  214. }
  215. /*
  216. * The crypto API fallback's encryption routine.
  217. * Allocate a bounce bio for encryption, encrypt the input bio using crypto API,
  218. * and replace *bio_ptr with the bounce bio. May split input bio if it's too
  219. * large. Returns true on success. Returns false and sets bio->bi_status on
  220. * error.
  221. */
  222. static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
  223. {
  224. struct bio *src_bio, *enc_bio;
  225. struct bio_crypt_ctx *bc;
  226. struct blk_crypto_keyslot *slot;
  227. int data_unit_size;
  228. struct skcipher_request *ciph_req = NULL;
  229. DECLARE_CRYPTO_WAIT(wait);
  230. u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  231. struct scatterlist src, dst;
  232. union blk_crypto_iv iv;
  233. unsigned int i, j;
  234. bool ret = false;
  235. blk_status_t blk_st;
  236. /* Split the bio if it's too big for single page bvec */
  237. if (!blk_crypto_fallback_split_bio_if_needed(bio_ptr))
  238. return false;
  239. src_bio = *bio_ptr;
  240. bc = src_bio->bi_crypt_context;
  241. data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
  242. /* Allocate bounce bio for encryption */
  243. enc_bio = blk_crypto_fallback_clone_bio(src_bio);
  244. if (!enc_bio) {
  245. src_bio->bi_status = BLK_STS_RESOURCE;
  246. return false;
  247. }
  248. /*
  249. * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
  250. * this bio's algorithm and key.
  251. */
  252. blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
  253. bc->bc_key, &slot);
  254. if (blk_st != BLK_STS_OK) {
  255. src_bio->bi_status = blk_st;
  256. goto out_put_enc_bio;
  257. }
  258. /* and then allocate an skcipher_request for it */
  259. if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
  260. src_bio->bi_status = BLK_STS_RESOURCE;
  261. goto out_release_keyslot;
  262. }
  263. memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
  264. sg_init_table(&src, 1);
  265. sg_init_table(&dst, 1);
  266. skcipher_request_set_crypt(ciph_req, &src, &dst, data_unit_size,
  267. iv.bytes);
  268. /* Encrypt each page in the bounce bio */
  269. for (i = 0; i < enc_bio->bi_vcnt; i++) {
  270. struct bio_vec *enc_bvec = &enc_bio->bi_io_vec[i];
  271. struct page *plaintext_page = enc_bvec->bv_page;
  272. struct page *ciphertext_page =
  273. mempool_alloc(blk_crypto_bounce_page_pool, GFP_NOIO);
  274. enc_bvec->bv_page = ciphertext_page;
  275. if (!ciphertext_page) {
  276. src_bio->bi_status = BLK_STS_RESOURCE;
  277. goto out_free_bounce_pages;
  278. }
  279. sg_set_page(&src, plaintext_page, data_unit_size,
  280. enc_bvec->bv_offset);
  281. sg_set_page(&dst, ciphertext_page, data_unit_size,
  282. enc_bvec->bv_offset);
  283. /* Encrypt each data unit in this page */
  284. for (j = 0; j < enc_bvec->bv_len; j += data_unit_size) {
  285. blk_crypto_dun_to_iv(curr_dun, &iv);
  286. if (crypto_wait_req(crypto_skcipher_encrypt(ciph_req),
  287. &wait)) {
  288. i++;
  289. src_bio->bi_status = BLK_STS_IOERR;
  290. goto out_free_bounce_pages;
  291. }
  292. bio_crypt_dun_increment(curr_dun, 1);
  293. src.offset += data_unit_size;
  294. dst.offset += data_unit_size;
  295. }
  296. }
  297. enc_bio->bi_private = src_bio;
  298. enc_bio->bi_end_io = blk_crypto_fallback_encrypt_endio;
  299. *bio_ptr = enc_bio;
  300. ret = true;
  301. enc_bio = NULL;
  302. goto out_free_ciph_req;
  303. out_free_bounce_pages:
  304. while (i > 0)
  305. mempool_free(enc_bio->bi_io_vec[--i].bv_page,
  306. blk_crypto_bounce_page_pool);
  307. out_free_ciph_req:
  308. skcipher_request_free(ciph_req);
  309. out_release_keyslot:
  310. blk_crypto_put_keyslot(slot);
  311. out_put_enc_bio:
  312. if (enc_bio)
  313. bio_uninit(enc_bio);
  314. kfree(enc_bio);
  315. return ret;
  316. }
  317. /*
  318. * The crypto API fallback's main decryption routine.
  319. * Decrypts input bio in place, and calls bio_endio on the bio.
  320. */
  321. static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
  322. {
  323. struct bio_fallback_crypt_ctx *f_ctx =
  324. container_of(work, struct bio_fallback_crypt_ctx, work);
  325. struct bio *bio = f_ctx->bio;
  326. struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx;
  327. struct blk_crypto_keyslot *slot;
  328. struct skcipher_request *ciph_req = NULL;
  329. DECLARE_CRYPTO_WAIT(wait);
  330. u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  331. union blk_crypto_iv iv;
  332. struct scatterlist sg;
  333. struct bio_vec bv;
  334. struct bvec_iter iter;
  335. const int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
  336. unsigned int i;
  337. blk_status_t blk_st;
  338. /*
  339. * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
  340. * this bio's algorithm and key.
  341. */
  342. blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
  343. bc->bc_key, &slot);
  344. if (blk_st != BLK_STS_OK) {
  345. bio->bi_status = blk_st;
  346. goto out_no_keyslot;
  347. }
  348. /* and then allocate an skcipher_request for it */
  349. if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
  350. bio->bi_status = BLK_STS_RESOURCE;
  351. goto out;
  352. }
  353. memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
  354. sg_init_table(&sg, 1);
  355. skcipher_request_set_crypt(ciph_req, &sg, &sg, data_unit_size,
  356. iv.bytes);
  357. /* Decrypt each segment in the bio */
  358. __bio_for_each_segment(bv, bio, iter, f_ctx->crypt_iter) {
  359. struct page *page = bv.bv_page;
  360. sg_set_page(&sg, page, data_unit_size, bv.bv_offset);
  361. /* Decrypt each data unit in the segment */
  362. for (i = 0; i < bv.bv_len; i += data_unit_size) {
  363. blk_crypto_dun_to_iv(curr_dun, &iv);
  364. if (crypto_wait_req(crypto_skcipher_decrypt(ciph_req),
  365. &wait)) {
  366. bio->bi_status = BLK_STS_IOERR;
  367. goto out;
  368. }
  369. bio_crypt_dun_increment(curr_dun, 1);
  370. sg.offset += data_unit_size;
  371. }
  372. }
  373. out:
  374. skcipher_request_free(ciph_req);
  375. blk_crypto_put_keyslot(slot);
  376. out_no_keyslot:
  377. mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
  378. bio_endio(bio);
  379. }
  380. /**
  381. * blk_crypto_fallback_decrypt_endio - queue bio for fallback decryption
  382. *
  383. * @bio: the bio to queue
  384. *
  385. * Restore bi_private and bi_end_io, and queue the bio for decryption into a
  386. * workqueue, since this function will be called from an atomic context.
  387. */
  388. static void blk_crypto_fallback_decrypt_endio(struct bio *bio)
  389. {
  390. struct bio_fallback_crypt_ctx *f_ctx = bio->bi_private;
  391. bio->bi_private = f_ctx->bi_private_orig;
  392. bio->bi_end_io = f_ctx->bi_end_io_orig;
  393. /* If there was an IO error, don't queue for decrypt. */
  394. if (bio->bi_status) {
  395. mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
  396. bio_endio(bio);
  397. return;
  398. }
  399. INIT_WORK(&f_ctx->work, blk_crypto_fallback_decrypt_bio);
  400. f_ctx->bio = bio;
  401. queue_work(blk_crypto_wq, &f_ctx->work);
  402. }
  403. /**
  404. * blk_crypto_fallback_bio_prep - Prepare a bio to use fallback en/decryption
  405. *
  406. * @bio_ptr: pointer to the bio to prepare
  407. *
  408. * If bio is doing a WRITE operation, this splits the bio into two parts if it's
  409. * too big (see blk_crypto_fallback_split_bio_if_needed()). It then allocates a
  410. * bounce bio for the first part, encrypts it, and updates bio_ptr to point to
  411. * the bounce bio.
  412. *
  413. * For a READ operation, we mark the bio for decryption by using bi_private and
  414. * bi_end_io.
  415. *
  416. * In either case, this function will make the bio look like a regular bio (i.e.
  417. * as if no encryption context was ever specified) for the purposes of the rest
  418. * of the stack except for blk-integrity (blk-integrity and blk-crypto are not
  419. * currently supported together).
  420. *
  421. * Return: true on success. Sets bio->bi_status and returns false on error.
  422. */
  423. bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
  424. {
  425. struct bio *bio = *bio_ptr;
  426. struct bio_crypt_ctx *bc = bio->bi_crypt_context;
  427. struct bio_fallback_crypt_ctx *f_ctx;
  428. if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) {
  429. /* User didn't call blk_crypto_start_using_key() first */
  430. bio->bi_status = BLK_STS_IOERR;
  431. return false;
  432. }
  433. if (!__blk_crypto_cfg_supported(blk_crypto_fallback_profile,
  434. &bc->bc_key->crypto_cfg)) {
  435. bio->bi_status = BLK_STS_NOTSUPP;
  436. return false;
  437. }
  438. if (bio_data_dir(bio) == WRITE)
  439. return blk_crypto_fallback_encrypt_bio(bio_ptr);
  440. /*
  441. * bio READ case: Set up a f_ctx in the bio's bi_private and set the
  442. * bi_end_io appropriately to trigger decryption when the bio is ended.
  443. */
  444. f_ctx = mempool_alloc(bio_fallback_crypt_ctx_pool, GFP_NOIO);
  445. f_ctx->crypt_ctx = *bc;
  446. f_ctx->crypt_iter = bio->bi_iter;
  447. f_ctx->bi_private_orig = bio->bi_private;
  448. f_ctx->bi_end_io_orig = bio->bi_end_io;
  449. bio->bi_private = (void *)f_ctx;
  450. bio->bi_end_io = blk_crypto_fallback_decrypt_endio;
  451. bio_crypt_free_ctx(bio);
  452. return true;
  453. }
  454. int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
  455. {
  456. return __blk_crypto_evict_key(blk_crypto_fallback_profile, key);
  457. }
  458. static bool blk_crypto_fallback_inited;
  459. static int blk_crypto_fallback_init(void)
  460. {
  461. int i;
  462. int err;
  463. if (blk_crypto_fallback_inited)
  464. return 0;
  465. get_random_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
  466. err = bioset_init(&crypto_bio_split, 64, 0, 0);
  467. if (err)
  468. goto out;
  469. /* Dynamic allocation is needed because of lockdep_register_key(). */
  470. blk_crypto_fallback_profile =
  471. kzalloc(sizeof(*blk_crypto_fallback_profile), GFP_KERNEL);
  472. if (!blk_crypto_fallback_profile) {
  473. err = -ENOMEM;
  474. goto fail_free_bioset;
  475. }
  476. err = blk_crypto_profile_init(blk_crypto_fallback_profile,
  477. blk_crypto_num_keyslots);
  478. if (err)
  479. goto fail_free_profile;
  480. err = -ENOMEM;
  481. blk_crypto_fallback_profile->ll_ops = blk_crypto_fallback_ll_ops;
  482. blk_crypto_fallback_profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
  483. /* All blk-crypto modes have a crypto API fallback. */
  484. for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++)
  485. blk_crypto_fallback_profile->modes_supported[i] = 0xFFFFFFFF;
  486. blk_crypto_fallback_profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0;
  487. blk_crypto_wq = alloc_workqueue("blk_crypto_wq",
  488. WQ_UNBOUND | WQ_HIGHPRI |
  489. WQ_MEM_RECLAIM, num_online_cpus());
  490. if (!blk_crypto_wq)
  491. goto fail_destroy_profile;
  492. blk_crypto_keyslots = kcalloc(blk_crypto_num_keyslots,
  493. sizeof(blk_crypto_keyslots[0]),
  494. GFP_KERNEL);
  495. if (!blk_crypto_keyslots)
  496. goto fail_free_wq;
  497. blk_crypto_bounce_page_pool =
  498. mempool_create_page_pool(num_prealloc_bounce_pg, 0);
  499. if (!blk_crypto_bounce_page_pool)
  500. goto fail_free_keyslots;
  501. bio_fallback_crypt_ctx_cache = KMEM_CACHE(bio_fallback_crypt_ctx, 0);
  502. if (!bio_fallback_crypt_ctx_cache)
  503. goto fail_free_bounce_page_pool;
  504. bio_fallback_crypt_ctx_pool =
  505. mempool_create_slab_pool(num_prealloc_fallback_crypt_ctxs,
  506. bio_fallback_crypt_ctx_cache);
  507. if (!bio_fallback_crypt_ctx_pool)
  508. goto fail_free_crypt_ctx_cache;
  509. blk_crypto_fallback_inited = true;
  510. return 0;
  511. fail_free_crypt_ctx_cache:
  512. kmem_cache_destroy(bio_fallback_crypt_ctx_cache);
  513. fail_free_bounce_page_pool:
  514. mempool_destroy(blk_crypto_bounce_page_pool);
  515. fail_free_keyslots:
  516. kfree(blk_crypto_keyslots);
  517. fail_free_wq:
  518. destroy_workqueue(blk_crypto_wq);
  519. fail_destroy_profile:
  520. blk_crypto_profile_destroy(blk_crypto_fallback_profile);
  521. fail_free_profile:
  522. kfree(blk_crypto_fallback_profile);
  523. fail_free_bioset:
  524. bioset_exit(&crypto_bio_split);
  525. out:
  526. return err;
  527. }
  528. /*
  529. * Prepare blk-crypto-fallback for the specified crypto mode.
  530. * Returns -ENOPKG if the needed crypto API support is missing.
  531. */
  532. int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
  533. {
  534. const char *cipher_str = blk_crypto_modes[mode_num].cipher_str;
  535. struct blk_crypto_fallback_keyslot *slotp;
  536. unsigned int i;
  537. int err = 0;
  538. /*
  539. * Fast path
  540. * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num]
  541. * for each i are visible before we try to access them.
  542. */
  543. if (likely(smp_load_acquire(&tfms_inited[mode_num])))
  544. return 0;
  545. mutex_lock(&tfms_init_lock);
  546. if (tfms_inited[mode_num])
  547. goto out;
  548. err = blk_crypto_fallback_init();
  549. if (err)
  550. goto out;
  551. for (i = 0; i < blk_crypto_num_keyslots; i++) {
  552. slotp = &blk_crypto_keyslots[i];
  553. slotp->tfms[mode_num] = crypto_alloc_skcipher(cipher_str, 0, 0);
  554. if (IS_ERR(slotp->tfms[mode_num])) {
  555. err = PTR_ERR(slotp->tfms[mode_num]);
  556. if (err == -ENOENT) {
  557. pr_warn_once("Missing crypto API support for \"%s\"\n",
  558. cipher_str);
  559. err = -ENOPKG;
  560. }
  561. slotp->tfms[mode_num] = NULL;
  562. goto out_free_tfms;
  563. }
  564. crypto_skcipher_set_flags(slotp->tfms[mode_num],
  565. CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
  566. }
  567. /*
  568. * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num]
  569. * for each i are visible before we set tfms_inited[mode_num].
  570. */
  571. smp_store_release(&tfms_inited[mode_num], true);
  572. goto out;
  573. out_free_tfms:
  574. for (i = 0; i < blk_crypto_num_keyslots; i++) {
  575. slotp = &blk_crypto_keyslots[i];
  576. crypto_free_skcipher(slotp->tfms[mode_num]);
  577. slotp->tfms[mode_num] = NULL;
  578. }
  579. out:
  580. mutex_unlock(&tfms_init_lock);
  581. return err;
  582. }