blk-crypto-fallback.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "blk-cgroup.h"
  19. #include "blk-crypto-internal.h"
  20. static unsigned int num_prealloc_bounce_pg = BIO_MAX_VECS;
  21. module_param(num_prealloc_bounce_pg, uint, 0);
  22. MODULE_PARM_DESC(num_prealloc_bounce_pg,
  23. "Number of preallocated bounce pages for the blk-crypto crypto API fallback");
  24. static unsigned int blk_crypto_num_keyslots = 100;
  25. module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0);
  26. MODULE_PARM_DESC(num_keyslots,
  27. "Number of keyslots for the blk-crypto crypto API fallback");
  28. static unsigned int num_prealloc_fallback_crypt_ctxs = 128;
  29. module_param(num_prealloc_fallback_crypt_ctxs, uint, 0);
  30. MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs,
  31. "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback");
  32. struct bio_fallback_crypt_ctx {
  33. struct bio_crypt_ctx crypt_ctx;
  34. /*
  35. * Copy of the bvec_iter when this bio was submitted.
  36. * We only want to en/decrypt the part of the bio as described by the
  37. * bvec_iter upon submission because bio might be split before being
  38. * resubmitted
  39. */
  40. struct bvec_iter crypt_iter;
  41. union {
  42. struct {
  43. struct work_struct work;
  44. struct bio *bio;
  45. };
  46. struct {
  47. void *bi_private_orig;
  48. bio_end_io_t *bi_end_io_orig;
  49. };
  50. };
  51. };
  52. static struct kmem_cache *bio_fallback_crypt_ctx_cache;
  53. static mempool_t *bio_fallback_crypt_ctx_pool;
  54. /*
  55. * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate
  56. * all of a mode's tfms when that mode starts being used. Since each mode may
  57. * need all the keyslots at some point, each mode needs its own tfm for each
  58. * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to
  59. * match the behavior of real inline encryption hardware (which only supports a
  60. * single encryption context per keyslot), we only allow one tfm per keyslot to
  61. * be used at a time - the rest of the unused tfms have their keys cleared.
  62. */
  63. static DEFINE_MUTEX(tfms_init_lock);
  64. static bool tfms_inited[BLK_ENCRYPTION_MODE_MAX];
  65. static struct blk_crypto_fallback_keyslot {
  66. enum blk_crypto_mode_num crypto_mode;
  67. struct crypto_sync_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX];
  68. } *blk_crypto_keyslots;
  69. static struct blk_crypto_profile *blk_crypto_fallback_profile;
  70. static struct workqueue_struct *blk_crypto_wq;
  71. static mempool_t *blk_crypto_bounce_page_pool;
  72. static struct bio_set enc_bio_set;
  73. /*
  74. * This is the key we set when evicting a keyslot. This *should* be the all 0's
  75. * key, but AES-XTS rejects that key, so we use some random bytes instead.
  76. */
  77. static u8 blank_key[BLK_CRYPTO_MAX_RAW_KEY_SIZE];
  78. static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
  79. {
  80. struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
  81. enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode;
  82. int err;
  83. WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID);
  84. /* Clear the key in the skcipher */
  85. err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], blank_key,
  86. blk_crypto_modes[crypto_mode].keysize);
  87. WARN_ON(err);
  88. slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID;
  89. }
  90. static int
  91. blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
  92. const struct blk_crypto_key *key,
  93. unsigned int slot)
  94. {
  95. struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
  96. const enum blk_crypto_mode_num crypto_mode =
  97. key->crypto_cfg.crypto_mode;
  98. int err;
  99. if (crypto_mode != slotp->crypto_mode &&
  100. slotp->crypto_mode != BLK_ENCRYPTION_MODE_INVALID)
  101. blk_crypto_fallback_evict_keyslot(slot);
  102. slotp->crypto_mode = crypto_mode;
  103. err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], key->bytes,
  104. key->size);
  105. if (err) {
  106. blk_crypto_fallback_evict_keyslot(slot);
  107. return err;
  108. }
  109. return 0;
  110. }
  111. static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile,
  112. const struct blk_crypto_key *key,
  113. unsigned int slot)
  114. {
  115. blk_crypto_fallback_evict_keyslot(slot);
  116. return 0;
  117. }
  118. static const struct blk_crypto_ll_ops blk_crypto_fallback_ll_ops = {
  119. .keyslot_program = blk_crypto_fallback_keyslot_program,
  120. .keyslot_evict = blk_crypto_fallback_keyslot_evict,
  121. };
  122. static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio)
  123. {
  124. struct bio *src_bio = enc_bio->bi_private;
  125. struct page **pages = (struct page **)enc_bio->bi_io_vec;
  126. struct bio_vec *bv;
  127. unsigned int i;
  128. /*
  129. * Use the same trick as the alloc side to avoid the need for an extra
  130. * pages array.
  131. */
  132. bio_for_each_bvec_all(bv, enc_bio, i)
  133. pages[i] = bv->bv_page;
  134. i = mempool_free_bulk(blk_crypto_bounce_page_pool, (void **)pages,
  135. enc_bio->bi_vcnt);
  136. if (i < enc_bio->bi_vcnt)
  137. release_pages(pages + i, enc_bio->bi_vcnt - i);
  138. if (enc_bio->bi_status)
  139. cmpxchg(&src_bio->bi_status, 0, enc_bio->bi_status);
  140. bio_put(enc_bio);
  141. bio_endio(src_bio);
  142. }
  143. #define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *))
  144. static struct bio *blk_crypto_alloc_enc_bio(struct bio *bio_src,
  145. unsigned int nr_segs, struct page ***pages_ret)
  146. {
  147. unsigned int memflags = memalloc_noio_save();
  148. unsigned int nr_allocated;
  149. struct page **pages;
  150. struct bio *bio;
  151. bio = bio_alloc_bioset(bio_src->bi_bdev, nr_segs, bio_src->bi_opf,
  152. GFP_NOIO, &enc_bio_set);
  153. if (bio_flagged(bio_src, BIO_REMAPPED))
  154. bio_set_flag(bio, BIO_REMAPPED);
  155. bio->bi_private = bio_src;
  156. bio->bi_end_io = blk_crypto_fallback_encrypt_endio;
  157. bio->bi_ioprio = bio_src->bi_ioprio;
  158. bio->bi_write_hint = bio_src->bi_write_hint;
  159. bio->bi_write_stream = bio_src->bi_write_stream;
  160. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  161. bio_clone_blkg_association(bio, bio_src);
  162. /*
  163. * Move page array up in the allocated memory for the bio vecs as far as
  164. * possible so that we can start filling biovecs from the beginning
  165. * without overwriting the temporary page array.
  166. */
  167. static_assert(PAGE_PTRS_PER_BVEC > 1);
  168. pages = (struct page **)bio->bi_io_vec;
  169. pages += nr_segs * (PAGE_PTRS_PER_BVEC - 1);
  170. /*
  171. * Try a bulk allocation first. This could leave random pages in the
  172. * array unallocated, but we'll fix that up later in mempool_alloc_bulk.
  173. *
  174. * Note: alloc_pages_bulk needs the array to be zeroed, as it assumes
  175. * any non-zero slot already contains a valid allocation.
  176. */
  177. memset(pages, 0, sizeof(struct page *) * nr_segs);
  178. nr_allocated = alloc_pages_bulk(GFP_KERNEL, nr_segs, pages);
  179. if (nr_allocated < nr_segs)
  180. mempool_alloc_bulk(blk_crypto_bounce_page_pool, (void **)pages,
  181. nr_segs, nr_allocated);
  182. memalloc_noio_restore(memflags);
  183. *pages_ret = pages;
  184. return bio;
  185. }
  186. static struct crypto_sync_skcipher *
  187. blk_crypto_fallback_tfm(struct blk_crypto_keyslot *slot)
  188. {
  189. const struct blk_crypto_fallback_keyslot *slotp =
  190. &blk_crypto_keyslots[blk_crypto_keyslot_index(slot)];
  191. return slotp->tfms[slotp->crypto_mode];
  192. }
  193. union blk_crypto_iv {
  194. __le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  195. u8 bytes[BLK_CRYPTO_MAX_IV_SIZE];
  196. };
  197. static void blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  198. union blk_crypto_iv *iv)
  199. {
  200. int i;
  201. for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++)
  202. iv->dun[i] = cpu_to_le64(dun[i]);
  203. }
  204. static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio,
  205. struct crypto_sync_skcipher *tfm)
  206. {
  207. struct bio_crypt_ctx *bc = src_bio->bi_crypt_context;
  208. int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
  209. SYNC_SKCIPHER_REQUEST_ON_STACK(ciph_req, tfm);
  210. u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  211. struct scatterlist src, dst;
  212. union blk_crypto_iv iv;
  213. unsigned int nr_enc_pages, enc_idx;
  214. struct page **enc_pages;
  215. struct bio *enc_bio;
  216. unsigned int i;
  217. skcipher_request_set_callback(ciph_req,
  218. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  219. NULL, NULL);
  220. memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
  221. sg_init_table(&src, 1);
  222. sg_init_table(&dst, 1);
  223. skcipher_request_set_crypt(ciph_req, &src, &dst, data_unit_size,
  224. iv.bytes);
  225. /*
  226. * Encrypt each page in the source bio. Because the source bio could
  227. * have bio_vecs that span more than a single page, but the encrypted
  228. * bios are limited to a single page per bio_vec, this can generate
  229. * more than a single encrypted bio per source bio.
  230. */
  231. new_bio:
  232. nr_enc_pages = min(bio_segments(src_bio), BIO_MAX_VECS);
  233. enc_bio = blk_crypto_alloc_enc_bio(src_bio, nr_enc_pages, &enc_pages);
  234. enc_idx = 0;
  235. for (;;) {
  236. struct bio_vec src_bv =
  237. bio_iter_iovec(src_bio, src_bio->bi_iter);
  238. struct page *enc_page = enc_pages[enc_idx];
  239. if (!IS_ALIGNED(src_bv.bv_len | src_bv.bv_offset,
  240. data_unit_size)) {
  241. enc_bio->bi_status = BLK_STS_INVAL;
  242. goto out_free_enc_bio;
  243. }
  244. __bio_add_page(enc_bio, enc_page, src_bv.bv_len,
  245. src_bv.bv_offset);
  246. sg_set_page(&src, src_bv.bv_page, data_unit_size,
  247. src_bv.bv_offset);
  248. sg_set_page(&dst, enc_page, data_unit_size, src_bv.bv_offset);
  249. /*
  250. * Increment the index now that the encrypted page is added to
  251. * the bio. This is important for the error unwind path.
  252. */
  253. enc_idx++;
  254. /*
  255. * Encrypt each data unit in this page.
  256. */
  257. for (i = 0; i < src_bv.bv_len; i += data_unit_size) {
  258. blk_crypto_dun_to_iv(curr_dun, &iv);
  259. if (crypto_skcipher_encrypt(ciph_req)) {
  260. enc_bio->bi_status = BLK_STS_IOERR;
  261. goto out_free_enc_bio;
  262. }
  263. bio_crypt_dun_increment(curr_dun, 1);
  264. src.offset += data_unit_size;
  265. dst.offset += data_unit_size;
  266. }
  267. bio_advance_iter_single(src_bio, &src_bio->bi_iter,
  268. src_bv.bv_len);
  269. if (!src_bio->bi_iter.bi_size)
  270. break;
  271. if (enc_idx == nr_enc_pages) {
  272. /*
  273. * For each additional encrypted bio submitted,
  274. * increment the source bio's remaining count. Each
  275. * encrypted bio's completion handler calls bio_endio on
  276. * the source bio, so this keeps the source bio from
  277. * completing until the last encrypted bio does.
  278. */
  279. bio_inc_remaining(src_bio);
  280. submit_bio(enc_bio);
  281. goto new_bio;
  282. }
  283. }
  284. submit_bio(enc_bio);
  285. return;
  286. out_free_enc_bio:
  287. /*
  288. * Add the remaining pages to the bio so that the normal completion path
  289. * in blk_crypto_fallback_encrypt_endio frees them. The exact data
  290. * layout does not matter for that, so don't bother iterating the source
  291. * bio.
  292. */
  293. for (; enc_idx < nr_enc_pages; enc_idx++)
  294. __bio_add_page(enc_bio, enc_pages[enc_idx], PAGE_SIZE, 0);
  295. bio_endio(enc_bio);
  296. }
  297. /*
  298. * The crypto API fallback's encryption routine.
  299. *
  300. * Allocate one or more bios for encryption, encrypt the input bio using the
  301. * crypto API, and submit the encrypted bios. Sets bio->bi_status and
  302. * completes the source bio on error
  303. */
  304. static void blk_crypto_fallback_encrypt_bio(struct bio *src_bio)
  305. {
  306. struct bio_crypt_ctx *bc = src_bio->bi_crypt_context;
  307. struct blk_crypto_keyslot *slot;
  308. blk_status_t status;
  309. status = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
  310. bc->bc_key, &slot);
  311. if (status != BLK_STS_OK) {
  312. src_bio->bi_status = status;
  313. bio_endio(src_bio);
  314. return;
  315. }
  316. __blk_crypto_fallback_encrypt_bio(src_bio,
  317. blk_crypto_fallback_tfm(slot));
  318. blk_crypto_put_keyslot(slot);
  319. }
  320. static blk_status_t __blk_crypto_fallback_decrypt_bio(struct bio *bio,
  321. struct bio_crypt_ctx *bc, struct bvec_iter iter,
  322. struct crypto_sync_skcipher *tfm)
  323. {
  324. SYNC_SKCIPHER_REQUEST_ON_STACK(ciph_req, tfm);
  325. u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  326. union blk_crypto_iv iv;
  327. struct scatterlist sg;
  328. struct bio_vec bv;
  329. const int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
  330. unsigned int i;
  331. skcipher_request_set_callback(ciph_req,
  332. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  333. NULL, NULL);
  334. memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
  335. sg_init_table(&sg, 1);
  336. skcipher_request_set_crypt(ciph_req, &sg, &sg, data_unit_size,
  337. iv.bytes);
  338. /* Decrypt each segment in the bio */
  339. __bio_for_each_segment(bv, bio, iter, iter) {
  340. struct page *page = bv.bv_page;
  341. if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size))
  342. return BLK_STS_INVAL;
  343. sg_set_page(&sg, page, data_unit_size, bv.bv_offset);
  344. /* Decrypt each data unit in the segment */
  345. for (i = 0; i < bv.bv_len; i += data_unit_size) {
  346. blk_crypto_dun_to_iv(curr_dun, &iv);
  347. if (crypto_skcipher_decrypt(ciph_req))
  348. return BLK_STS_IOERR;
  349. bio_crypt_dun_increment(curr_dun, 1);
  350. sg.offset += data_unit_size;
  351. }
  352. }
  353. return BLK_STS_OK;
  354. }
  355. /*
  356. * The crypto API fallback's main decryption routine.
  357. *
  358. * Decrypts input bio in place, and calls bio_endio on the bio.
  359. */
  360. static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
  361. {
  362. struct bio_fallback_crypt_ctx *f_ctx =
  363. container_of(work, struct bio_fallback_crypt_ctx, work);
  364. struct bio *bio = f_ctx->bio;
  365. struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx;
  366. struct blk_crypto_keyslot *slot;
  367. blk_status_t status;
  368. status = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
  369. bc->bc_key, &slot);
  370. if (status == BLK_STS_OK) {
  371. status = __blk_crypto_fallback_decrypt_bio(bio, bc,
  372. f_ctx->crypt_iter,
  373. blk_crypto_fallback_tfm(slot));
  374. blk_crypto_put_keyslot(slot);
  375. }
  376. mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
  377. bio->bi_status = status;
  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. * @bio: bio to prepare
  406. *
  407. * If bio is doing a WRITE operation, allocate one or more bios to contain the
  408. * encrypted payload and submit them.
  409. *
  410. * For a READ operation, mark the bio for decryption by using bi_private and
  411. * bi_end_io.
  412. *
  413. * In either case, this function will make the submitted bio(s) look like
  414. * regular bios (i.e. as if no encryption context was ever specified) for the
  415. * purposes of the rest of the stack except for blk-integrity (blk-integrity and
  416. * blk-crypto are not currently supported together).
  417. *
  418. * Return: true if @bio should be submitted to the driver by the caller, else
  419. * false. Sets bio->bi_status, calls bio_endio and returns false on error.
  420. */
  421. bool blk_crypto_fallback_bio_prep(struct bio *bio)
  422. {
  423. struct bio_crypt_ctx *bc = bio->bi_crypt_context;
  424. struct bio_fallback_crypt_ctx *f_ctx;
  425. if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) {
  426. /* User didn't call blk_crypto_start_using_key() first */
  427. bio_io_error(bio);
  428. return false;
  429. }
  430. if (!__blk_crypto_cfg_supported(blk_crypto_fallback_profile,
  431. &bc->bc_key->crypto_cfg)) {
  432. bio->bi_status = BLK_STS_NOTSUPP;
  433. bio_endio(bio);
  434. return false;
  435. }
  436. if (bio_data_dir(bio) == WRITE) {
  437. blk_crypto_fallback_encrypt_bio(bio);
  438. return false;
  439. }
  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, sizeof(blank_key));
  466. err = bioset_init(&enc_bio_set, 64, 0, BIOSET_NEED_BVECS);
  467. if (err)
  468. goto out;
  469. /* Dynamic allocation is needed because of lockdep_register_key(). */
  470. blk_crypto_fallback_profile = kzalloc_obj(*blk_crypto_fallback_profile);
  471. if (!blk_crypto_fallback_profile) {
  472. err = -ENOMEM;
  473. goto fail_free_bioset;
  474. }
  475. err = blk_crypto_profile_init(blk_crypto_fallback_profile,
  476. blk_crypto_num_keyslots);
  477. if (err)
  478. goto fail_free_profile;
  479. err = -ENOMEM;
  480. blk_crypto_fallback_profile->ll_ops = blk_crypto_fallback_ll_ops;
  481. blk_crypto_fallback_profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
  482. blk_crypto_fallback_profile->key_types_supported = BLK_CRYPTO_KEY_TYPE_RAW;
  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 = kzalloc_objs(blk_crypto_keyslots[0],
  493. blk_crypto_num_keyslots);
  494. if (!blk_crypto_keyslots)
  495. goto fail_free_wq;
  496. blk_crypto_bounce_page_pool =
  497. mempool_create_page_pool(num_prealloc_bounce_pg, 0);
  498. if (!blk_crypto_bounce_page_pool)
  499. goto fail_free_keyslots;
  500. bio_fallback_crypt_ctx_cache = KMEM_CACHE(bio_fallback_crypt_ctx, 0);
  501. if (!bio_fallback_crypt_ctx_cache)
  502. goto fail_free_bounce_page_pool;
  503. bio_fallback_crypt_ctx_pool =
  504. mempool_create_slab_pool(num_prealloc_fallback_crypt_ctxs,
  505. bio_fallback_crypt_ctx_cache);
  506. if (!bio_fallback_crypt_ctx_pool)
  507. goto fail_free_crypt_ctx_cache;
  508. blk_crypto_fallback_inited = true;
  509. return 0;
  510. fail_free_crypt_ctx_cache:
  511. kmem_cache_destroy(bio_fallback_crypt_ctx_cache);
  512. fail_free_bounce_page_pool:
  513. mempool_destroy(blk_crypto_bounce_page_pool);
  514. fail_free_keyslots:
  515. kfree(blk_crypto_keyslots);
  516. fail_free_wq:
  517. destroy_workqueue(blk_crypto_wq);
  518. fail_destroy_profile:
  519. blk_crypto_profile_destroy(blk_crypto_fallback_profile);
  520. fail_free_profile:
  521. kfree(blk_crypto_fallback_profile);
  522. fail_free_bioset:
  523. bioset_exit(&enc_bio_set);
  524. out:
  525. return err;
  526. }
  527. /*
  528. * Prepare blk-crypto-fallback for the specified crypto mode.
  529. * Returns -ENOPKG if the needed crypto API support is missing.
  530. */
  531. int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
  532. {
  533. const char *cipher_str = blk_crypto_modes[mode_num].cipher_str;
  534. struct blk_crypto_fallback_keyslot *slotp;
  535. unsigned int i;
  536. int err = 0;
  537. /*
  538. * Fast path
  539. * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num]
  540. * for each i are visible before we try to access them.
  541. */
  542. if (likely(smp_load_acquire(&tfms_inited[mode_num])))
  543. return 0;
  544. mutex_lock(&tfms_init_lock);
  545. if (tfms_inited[mode_num])
  546. goto out;
  547. err = blk_crypto_fallback_init();
  548. if (err)
  549. goto out;
  550. for (i = 0; i < blk_crypto_num_keyslots; i++) {
  551. slotp = &blk_crypto_keyslots[i];
  552. slotp->tfms[mode_num] = crypto_alloc_sync_skcipher(cipher_str,
  553. 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_sync_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_sync_skcipher(slotp->tfms[mode_num]);
  577. slotp->tfms[mode_num] = NULL;
  578. }
  579. out:
  580. mutex_unlock(&tfms_init_lock);
  581. return err;
  582. }