1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2019 Google LLC 4 */ 5 6 /* 7 * Refer to Documentation/block/inline-encryption.rst for detailed explanation. 8 */ 9 10 #define pr_fmt(fmt) "blk-crypto-fallback: " fmt 11 12 #include <crypto/skcipher.h> 13 #include <linux/blk-crypto.h> 14 #include <linux/blk-crypto-profile.h> 15 #include <linux/blkdev.h> 16 #include <linux/crypto.h> 17 #include <linux/mempool.h> 18 #include <linux/module.h> 19 #include <linux/random.h> 20 #include <linux/scatterlist.h> 21 22 #include "blk-cgroup.h" 23 #include "blk-crypto-internal.h" 24 25 static unsigned int num_prealloc_bounce_pg = BIO_MAX_VECS; 26 module_param(num_prealloc_bounce_pg, uint, 0); 27 MODULE_PARM_DESC(num_prealloc_bounce_pg, 28 "Number of preallocated bounce pages for the blk-crypto crypto API fallback"); 29 30 static unsigned int blk_crypto_num_keyslots = 100; 31 module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0); 32 MODULE_PARM_DESC(num_keyslots, 33 "Number of keyslots for the blk-crypto crypto API fallback"); 34 35 static unsigned int num_prealloc_fallback_crypt_ctxs = 128; 36 module_param(num_prealloc_fallback_crypt_ctxs, uint, 0); 37 MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs, 38 "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback"); 39 40 struct bio_fallback_crypt_ctx { 41 struct bio_crypt_ctx crypt_ctx; 42 /* 43 * Copy of the bvec_iter when this bio was submitted. 44 * We only want to en/decrypt the part of the bio as described by the 45 * bvec_iter upon submission because bio might be split before being 46 * resubmitted 47 */ 48 struct bvec_iter crypt_iter; 49 union { 50 struct { 51 struct work_struct work; 52 struct bio *bio; 53 }; 54 struct { 55 void *bi_private_orig; 56 bio_end_io_t *bi_end_io_orig; 57 }; 58 }; 59 }; 60 61 static struct kmem_cache *bio_fallback_crypt_ctx_cache; 62 static mempool_t *bio_fallback_crypt_ctx_pool; 63 64 /* 65 * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate 66 * all of a mode's tfms when that mode starts being used. Since each mode may 67 * need all the keyslots at some point, each mode needs its own tfm for each 68 * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to 69 * match the behavior of real inline encryption hardware (which only supports a 70 * single encryption context per keyslot), we only allow one tfm per keyslot to 71 * be used at a time - the rest of the unused tfms have their keys cleared. 72 */ 73 static DEFINE_MUTEX(tfms_init_lock); 74 static bool tfms_inited[BLK_ENCRYPTION_MODE_MAX]; 75 76 static struct blk_crypto_fallback_keyslot { 77 enum blk_crypto_mode_num crypto_mode; 78 struct crypto_sync_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX]; 79 } *blk_crypto_keyslots; 80 81 static struct blk_crypto_profile *blk_crypto_fallback_profile; 82 static struct workqueue_struct *blk_crypto_wq; 83 static mempool_t *blk_crypto_bounce_page_pool; 84 static struct bio_set enc_bio_set; 85 86 /* 87 * This is the key we set when evicting a keyslot. This *should* be the all 0's 88 * key, but AES-XTS rejects that key, so we use some random bytes instead. 89 */ 90 static u8 blank_key[BLK_CRYPTO_MAX_RAW_KEY_SIZE]; 91 92 static void blk_crypto_fallback_evict_keyslot(unsigned int slot) 93 { 94 struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot]; 95 enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode; 96 int err; 97 98 WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID); 99 100 /* Clear the key in the skcipher */ 101 err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], blank_key, 102 blk_crypto_modes[crypto_mode].keysize); 103 WARN_ON(err); 104 slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID; 105 } 106 107 static int 108 blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile, 109 const struct blk_crypto_key *key, 110 unsigned int slot) 111 { 112 struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot]; 113 const enum blk_crypto_mode_num crypto_mode = 114 key->crypto_cfg.crypto_mode; 115 int err; 116 117 if (crypto_mode != slotp->crypto_mode && 118 slotp->crypto_mode != BLK_ENCRYPTION_MODE_INVALID) 119 blk_crypto_fallback_evict_keyslot(slot); 120 121 slotp->crypto_mode = crypto_mode; 122 err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], key->bytes, 123 key->size); 124 if (err) { 125 blk_crypto_fallback_evict_keyslot(slot); 126 return err; 127 } 128 return 0; 129 } 130 131 static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile, 132 const struct blk_crypto_key *key, 133 unsigned int slot) 134 { 135 blk_crypto_fallback_evict_keyslot(slot); 136 return 0; 137 } 138 139 static const struct blk_crypto_ll_ops blk_crypto_fallback_ll_ops = { 140 .keyslot_program = blk_crypto_fallback_keyslot_program, 141 .keyslot_evict = blk_crypto_fallback_keyslot_evict, 142 }; 143 144 static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio) 145 { 146 struct bio *src_bio = enc_bio->bi_private; 147 struct page **pages = (struct page **)enc_bio->bi_io_vec; 148 struct bio_vec *bv; 149 unsigned int i; 150 151 /* 152 * Use the same trick as the alloc side to avoid the need for an extra 153 * pages array. 154 */ 155 bio_for_each_bvec_all(bv, enc_bio, i) 156 pages[i] = bv->bv_page; 157 158 i = mempool_free_bulk(blk_crypto_bounce_page_pool, (void **)pages, 159 enc_bio->bi_vcnt); 160 if (i < enc_bio->bi_vcnt) 161 release_pages(pages + i, enc_bio->bi_vcnt - i); 162 163 if (enc_bio->bi_status) 164 cmpxchg(&src_bio->bi_status, 0, enc_bio->bi_status); 165 166 bio_put(enc_bio); 167 bio_endio(src_bio); 168 } 169 170 #define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *)) 171 172 static struct bio *blk_crypto_alloc_enc_bio(struct bio *bio_src, 173 unsigned int nr_segs, struct page ***pages_ret) 174 { 175 unsigned int memflags = memalloc_noio_save(); 176 unsigned int nr_allocated; 177 struct page **pages; 178 struct bio *bio; 179 180 bio = bio_alloc_bioset(bio_src->bi_bdev, nr_segs, bio_src->bi_opf, 181 GFP_NOIO, &enc_bio_set); 182 if (bio_flagged(bio_src, BIO_REMAPPED)) 183 bio_set_flag(bio, BIO_REMAPPED); 184 bio->bi_private = bio_src; 185 bio->bi_end_io = blk_crypto_fallback_encrypt_endio; 186 bio->bi_ioprio = bio_src->bi_ioprio; 187 bio->bi_write_hint = bio_src->bi_write_hint; 188 bio->bi_write_stream = bio_src->bi_write_stream; 189 bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; 190 bio_clone_blkg_association(bio, bio_src); 191 192 /* 193 * Move page array up in the allocated memory for the bio vecs as far as 194 * possible so that we can start filling biovecs from the beginning 195 * without overwriting the temporary page array. 196 */ 197 static_assert(PAGE_PTRS_PER_BVEC > 1); 198 pages = (struct page **)bio->bi_io_vec; 199 pages += nr_segs * (PAGE_PTRS_PER_BVEC - 1); 200 201 /* 202 * Try a bulk allocation first. This might not fill all allocated 203 * pages, but we'll fix that up later in mempool_alloc_bulk. 204 * 205 * Note: alloc_pages_bulk needs the array to be zeroed, as it assumes 206 * any non-zero slot already contains a valid allocation. 207 */ 208 memset(pages, 0, sizeof(struct page *) * nr_segs); 209 nr_allocated = alloc_pages_bulk(GFP_KERNEL, nr_segs, pages); 210 if (nr_allocated < nr_segs) 211 mempool_alloc_bulk(blk_crypto_bounce_page_pool, 212 (void **)pages + nr_allocated, 213 nr_segs - nr_allocated); 214 memalloc_noio_restore(memflags); 215 *pages_ret = pages; 216 return bio; 217 } 218 219 static struct crypto_sync_skcipher * 220 blk_crypto_fallback_tfm(struct blk_crypto_keyslot *slot) 221 { 222 const struct blk_crypto_fallback_keyslot *slotp = 223 &blk_crypto_keyslots[blk_crypto_keyslot_index(slot)]; 224 225 return slotp->tfms[slotp->crypto_mode]; 226 } 227 228 union blk_crypto_iv { 229 __le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 230 u8 bytes[BLK_CRYPTO_MAX_IV_SIZE]; 231 }; 232 233 static void blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], 234 union blk_crypto_iv *iv) 235 { 236 int i; 237 238 for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) 239 iv->dun[i] = cpu_to_le64(dun[i]); 240 } 241 242 static void __blk_crypto_fallback_encrypt_bio(struct bio *src_bio, 243 struct crypto_sync_skcipher *tfm) 244 { 245 struct bio_crypt_ctx *bc = src_bio->bi_crypt_context; 246 int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size; 247 SYNC_SKCIPHER_REQUEST_ON_STACK(ciph_req, tfm); 248 u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 249 struct scatterlist src, dst; 250 union blk_crypto_iv iv; 251 unsigned int nr_enc_pages, enc_idx; 252 struct page **enc_pages; 253 struct bio *enc_bio; 254 unsigned int i; 255 256 skcipher_request_set_callback(ciph_req, 257 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 258 NULL, NULL); 259 260 memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun)); 261 sg_init_table(&src, 1); 262 sg_init_table(&dst, 1); 263 264 skcipher_request_set_crypt(ciph_req, &src, &dst, data_unit_size, 265 iv.bytes); 266 267 /* 268 * Encrypt each page in the source bio. Because the source bio could 269 * have bio_vecs that span more than a single page, but the encrypted 270 * bios are limited to a single page per bio_vec, this can generate 271 * more than a single encrypted bio per source bio. 272 */ 273 new_bio: 274 nr_enc_pages = min(bio_segments(src_bio), BIO_MAX_VECS); 275 enc_bio = blk_crypto_alloc_enc_bio(src_bio, nr_enc_pages, &enc_pages); 276 enc_idx = 0; 277 for (;;) { 278 struct bio_vec src_bv = 279 bio_iter_iovec(src_bio, src_bio->bi_iter); 280 struct page *enc_page = enc_pages[enc_idx]; 281 282 if (!IS_ALIGNED(src_bv.bv_len | src_bv.bv_offset, 283 data_unit_size)) { 284 enc_bio->bi_status = BLK_STS_INVAL; 285 goto out_free_enc_bio; 286 } 287 288 __bio_add_page(enc_bio, enc_page, src_bv.bv_len, 289 src_bv.bv_offset); 290 291 sg_set_page(&src, src_bv.bv_page, data_unit_size, 292 src_bv.bv_offset); 293 sg_set_page(&dst, enc_page, data_unit_size, src_bv.bv_offset); 294 295 /* 296 * Increment the index now that the encrypted page is added to 297 * the bio. This is important for the error unwind path. 298 */ 299 enc_idx++; 300 301 /* 302 * Encrypt each data unit in this page. 303 */ 304 for (i = 0; i < src_bv.bv_len; i += data_unit_size) { 305 blk_crypto_dun_to_iv(curr_dun, &iv); 306 if (crypto_skcipher_encrypt(ciph_req)) { 307 enc_bio->bi_status = BLK_STS_IOERR; 308 goto out_free_enc_bio; 309 } 310 bio_crypt_dun_increment(curr_dun, 1); 311 src.offset += data_unit_size; 312 dst.offset += data_unit_size; 313 } 314 315 bio_advance_iter_single(src_bio, &src_bio->bi_iter, 316 src_bv.bv_len); 317 if (!src_bio->bi_iter.bi_size) 318 break; 319 320 if (enc_idx == nr_enc_pages) { 321 /* 322 * For each additional encrypted bio submitted, 323 * increment the source bio's remaining count. Each 324 * encrypted bio's completion handler calls bio_endio on 325 * the source bio, so this keeps the source bio from 326 * completing until the last encrypted bio does. 327 */ 328 bio_inc_remaining(src_bio); 329 submit_bio(enc_bio); 330 goto new_bio; 331 } 332 } 333 334 submit_bio(enc_bio); 335 return; 336 337 out_free_enc_bio: 338 /* 339 * Add the remaining pages to the bio so that the normal completion path 340 * in blk_crypto_fallback_encrypt_endio frees them. The exact data 341 * layout does not matter for that, so don't bother iterating the source 342 * bio. 343 */ 344 for (; enc_idx < nr_enc_pages; enc_idx++) 345 __bio_add_page(enc_bio, enc_pages[enc_idx], PAGE_SIZE, 0); 346 bio_endio(enc_bio); 347 } 348 349 /* 350 * The crypto API fallback's encryption routine. 351 * 352 * Allocate one or more bios for encryption, encrypt the input bio using the 353 * crypto API, and submit the encrypted bios. Sets bio->bi_status and 354 * completes the source bio on error 355 */ 356 static void blk_crypto_fallback_encrypt_bio(struct bio *src_bio) 357 { 358 struct bio_crypt_ctx *bc = src_bio->bi_crypt_context; 359 struct blk_crypto_keyslot *slot; 360 blk_status_t status; 361 362 status = blk_crypto_get_keyslot(blk_crypto_fallback_profile, 363 bc->bc_key, &slot); 364 if (status != BLK_STS_OK) { 365 bio_endio_status(src_bio, status); 366 return; 367 } 368 __blk_crypto_fallback_encrypt_bio(src_bio, 369 blk_crypto_fallback_tfm(slot)); 370 blk_crypto_put_keyslot(slot); 371 } 372 373 static blk_status_t __blk_crypto_fallback_decrypt_bio(struct bio *bio, 374 struct bio_crypt_ctx *bc, struct bvec_iter iter, 375 struct crypto_sync_skcipher *tfm) 376 { 377 SYNC_SKCIPHER_REQUEST_ON_STACK(ciph_req, tfm); 378 u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 379 union blk_crypto_iv iv; 380 struct scatterlist sg; 381 struct bio_vec bv; 382 const int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size; 383 unsigned int i; 384 385 skcipher_request_set_callback(ciph_req, 386 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 387 NULL, NULL); 388 389 memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun)); 390 sg_init_table(&sg, 1); 391 skcipher_request_set_crypt(ciph_req, &sg, &sg, data_unit_size, 392 iv.bytes); 393 394 /* Decrypt each segment in the bio */ 395 __bio_for_each_segment(bv, bio, iter, iter) { 396 struct page *page = bv.bv_page; 397 398 if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size)) 399 return BLK_STS_INVAL; 400 401 sg_set_page(&sg, page, data_unit_size, bv.bv_offset); 402 403 /* Decrypt each data unit in the segment */ 404 for (i = 0; i < bv.bv_len; i += data_unit_size) { 405 blk_crypto_dun_to_iv(curr_dun, &iv); 406 if (crypto_skcipher_decrypt(ciph_req)) 407 return BLK_STS_IOERR; 408 bio_crypt_dun_increment(curr_dun, 1); 409 sg.offset += data_unit_size; 410 } 411 } 412 413 return BLK_STS_OK; 414 } 415 416 /* 417 * The crypto API fallback's main decryption routine. 418 * 419 * Decrypts input bio in place, and calls bio_endio on the bio. 420 */ 421 static void blk_crypto_fallback_decrypt_bio(struct work_struct *work) 422 { 423 struct bio_fallback_crypt_ctx *f_ctx = 424 container_of(work, struct bio_fallback_crypt_ctx, work); 425 struct bio *bio = f_ctx->bio; 426 struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx; 427 struct blk_crypto_keyslot *slot; 428 blk_status_t status; 429 430 status = blk_crypto_get_keyslot(blk_crypto_fallback_profile, 431 bc->bc_key, &slot); 432 if (status == BLK_STS_OK) { 433 status = __blk_crypto_fallback_decrypt_bio(bio, bc, 434 f_ctx->crypt_iter, 435 blk_crypto_fallback_tfm(slot)); 436 blk_crypto_put_keyslot(slot); 437 } 438 mempool_free(f_ctx, bio_fallback_crypt_ctx_pool); 439 440 bio_endio_status(bio, status); 441 } 442 443 /** 444 * blk_crypto_fallback_decrypt_endio - queue bio for fallback decryption 445 * 446 * @bio: the bio to queue 447 * 448 * Restore bi_private and bi_end_io, and queue the bio for decryption into a 449 * workqueue, since this function will be called from an atomic context. 450 */ 451 static void blk_crypto_fallback_decrypt_endio(struct bio *bio) 452 { 453 struct bio_fallback_crypt_ctx *f_ctx = bio->bi_private; 454 455 bio->bi_private = f_ctx->bi_private_orig; 456 bio->bi_end_io = f_ctx->bi_end_io_orig; 457 458 /* If there was an IO error, don't queue for decrypt. */ 459 if (bio->bi_status) { 460 mempool_free(f_ctx, bio_fallback_crypt_ctx_pool); 461 bio_endio(bio); 462 return; 463 } 464 465 INIT_WORK(&f_ctx->work, blk_crypto_fallback_decrypt_bio); 466 f_ctx->bio = bio; 467 queue_work(blk_crypto_wq, &f_ctx->work); 468 } 469 470 /** 471 * blk_crypto_fallback_bio_prep - Prepare a bio to use fallback en/decryption 472 * @bio: bio to prepare 473 * 474 * If bio is doing a WRITE operation, allocate one or more bios to contain the 475 * encrypted payload and submit them. 476 * 477 * For a READ operation, mark the bio for decryption by using bi_private and 478 * bi_end_io. 479 * 480 * In either case, this function will make the submitted bio(s) look like 481 * regular bios (i.e. as if no encryption context was ever specified) for the 482 * purposes of the rest of the stack except for blk-integrity (blk-integrity and 483 * blk-crypto are not currently supported together). 484 * 485 * Return: true if @bio should be submitted to the driver by the caller, else 486 * false. Sets bio->bi_status, calls bio_endio and returns false on error. 487 */ 488 bool blk_crypto_fallback_bio_prep(struct bio *bio) 489 { 490 struct bio_crypt_ctx *bc = bio->bi_crypt_context; 491 struct bio_fallback_crypt_ctx *f_ctx; 492 493 if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) { 494 /* User didn't call blk_crypto_start_using_key() first */ 495 bio_io_error(bio); 496 return false; 497 } 498 499 if (!__blk_crypto_cfg_supported(blk_crypto_fallback_profile, 500 &bc->bc_key->crypto_cfg)) { 501 bio_endio_status(bio, BLK_STS_NOTSUPP); 502 return false; 503 } 504 505 if (bio_data_dir(bio) == WRITE) { 506 blk_crypto_fallback_encrypt_bio(bio); 507 return false; 508 } 509 510 /* 511 * bio READ case: Set up a f_ctx in the bio's bi_private and set the 512 * bi_end_io appropriately to trigger decryption when the bio is ended. 513 */ 514 f_ctx = mempool_alloc(bio_fallback_crypt_ctx_pool, GFP_NOIO); 515 f_ctx->crypt_ctx = *bc; 516 f_ctx->crypt_iter = bio->bi_iter; 517 f_ctx->bi_private_orig = bio->bi_private; 518 f_ctx->bi_end_io_orig = bio->bi_end_io; 519 bio->bi_private = (void *)f_ctx; 520 bio->bi_end_io = blk_crypto_fallback_decrypt_endio; 521 bio_crypt_free_ctx(bio); 522 523 return true; 524 } 525 526 int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key) 527 { 528 return __blk_crypto_evict_key(blk_crypto_fallback_profile, key); 529 } 530 531 static bool blk_crypto_fallback_inited; 532 static int blk_crypto_fallback_init(void) 533 { 534 int i; 535 int err; 536 537 if (blk_crypto_fallback_inited) 538 return 0; 539 540 get_random_bytes(blank_key, sizeof(blank_key)); 541 542 err = bioset_init(&enc_bio_set, 64, 0, BIOSET_NEED_BVECS); 543 if (err) 544 goto out; 545 546 /* Dynamic allocation is needed because of lockdep_register_key(). */ 547 blk_crypto_fallback_profile = kzalloc_obj(*blk_crypto_fallback_profile); 548 if (!blk_crypto_fallback_profile) { 549 err = -ENOMEM; 550 goto fail_free_bioset; 551 } 552 553 err = blk_crypto_profile_init(blk_crypto_fallback_profile, 554 blk_crypto_num_keyslots); 555 if (err) 556 goto fail_free_profile; 557 err = -ENOMEM; 558 559 blk_crypto_fallback_profile->ll_ops = blk_crypto_fallback_ll_ops; 560 blk_crypto_fallback_profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE; 561 blk_crypto_fallback_profile->key_types_supported = BLK_CRYPTO_KEY_TYPE_RAW; 562 563 /* All blk-crypto modes have a crypto API fallback. */ 564 for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) 565 blk_crypto_fallback_profile->modes_supported[i] = 0xFFFFFFFF; 566 blk_crypto_fallback_profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0; 567 568 blk_crypto_wq = alloc_workqueue("blk_crypto_wq", 569 WQ_UNBOUND | WQ_HIGHPRI | 570 WQ_MEM_RECLAIM, num_online_cpus()); 571 if (!blk_crypto_wq) 572 goto fail_destroy_profile; 573 574 blk_crypto_keyslots = kzalloc_objs(blk_crypto_keyslots[0], 575 blk_crypto_num_keyslots); 576 if (!blk_crypto_keyslots) 577 goto fail_free_wq; 578 579 blk_crypto_bounce_page_pool = 580 mempool_create_page_pool(num_prealloc_bounce_pg, 0); 581 if (!blk_crypto_bounce_page_pool) 582 goto fail_free_keyslots; 583 584 bio_fallback_crypt_ctx_cache = KMEM_CACHE(bio_fallback_crypt_ctx, 0); 585 if (!bio_fallback_crypt_ctx_cache) 586 goto fail_free_bounce_page_pool; 587 588 bio_fallback_crypt_ctx_pool = 589 mempool_create_slab_pool(num_prealloc_fallback_crypt_ctxs, 590 bio_fallback_crypt_ctx_cache); 591 if (!bio_fallback_crypt_ctx_pool) 592 goto fail_free_crypt_ctx_cache; 593 594 blk_crypto_fallback_inited = true; 595 596 return 0; 597 fail_free_crypt_ctx_cache: 598 kmem_cache_destroy(bio_fallback_crypt_ctx_cache); 599 fail_free_bounce_page_pool: 600 mempool_destroy(blk_crypto_bounce_page_pool); 601 fail_free_keyslots: 602 kfree(blk_crypto_keyslots); 603 fail_free_wq: 604 destroy_workqueue(blk_crypto_wq); 605 fail_destroy_profile: 606 blk_crypto_profile_destroy(blk_crypto_fallback_profile); 607 fail_free_profile: 608 kfree(blk_crypto_fallback_profile); 609 fail_free_bioset: 610 bioset_exit(&enc_bio_set); 611 out: 612 return err; 613 } 614 615 /* 616 * Prepare blk-crypto-fallback for the specified crypto mode. 617 * Returns -ENOPKG if the needed crypto API support is missing. 618 */ 619 int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num) 620 { 621 const char *cipher_str = blk_crypto_modes[mode_num].cipher_str; 622 struct blk_crypto_fallback_keyslot *slotp; 623 unsigned int i; 624 int err = 0; 625 626 /* 627 * Fast path 628 * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] 629 * for each i are visible before we try to access them. 630 */ 631 if (likely(smp_load_acquire(&tfms_inited[mode_num]))) 632 return 0; 633 634 mutex_lock(&tfms_init_lock); 635 if (tfms_inited[mode_num]) 636 goto out; 637 638 err = blk_crypto_fallback_init(); 639 if (err) 640 goto out; 641 642 for (i = 0; i < blk_crypto_num_keyslots; i++) { 643 slotp = &blk_crypto_keyslots[i]; 644 slotp->tfms[mode_num] = crypto_alloc_sync_skcipher(cipher_str, 645 0, 0); 646 if (IS_ERR(slotp->tfms[mode_num])) { 647 err = PTR_ERR(slotp->tfms[mode_num]); 648 if (err == -ENOENT) { 649 pr_warn_once("Missing crypto API support for \"%s\"\n", 650 cipher_str); 651 err = -ENOPKG; 652 } 653 slotp->tfms[mode_num] = NULL; 654 goto out_free_tfms; 655 } 656 657 crypto_sync_skcipher_set_flags(slotp->tfms[mode_num], 658 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 659 } 660 661 /* 662 * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] 663 * for each i are visible before we set tfms_inited[mode_num]. 664 */ 665 smp_store_release(&tfms_inited[mode_num], true); 666 goto out; 667 668 out_free_tfms: 669 for (i = 0; i < blk_crypto_num_keyslots; i++) { 670 slotp = &blk_crypto_keyslots[i]; 671 crypto_free_sync_skcipher(slotp->tfms[mode_num]); 672 slotp->tfms[mode_num] = NULL; 673 } 674 out: 675 mutex_unlock(&tfms_init_lock); 676 return err; 677 } 678