Skip to content

Commit cb77cb5

Browse files
ebiggersaxboe
authored andcommitted
blk-crypto: rename blk_keyslot_manager to blk_crypto_profile
blk_keyslot_manager is misnamed because it doesn't necessarily manage keyslots. It actually does several different things: - Contains the crypto capabilities of the device. - Provides functions to control the inline encryption hardware. Originally these were just for programming/evicting keyslots; however, new functionality (hardware-wrapped keys) will require new functions here which are unrelated to keyslots. Moreover, device-mapper devices already (ab)use "keyslot_evict" to pass key eviction requests to their underlying devices even though device-mapper devices don't have any keyslots themselves (so it really should be "evict_key", not "keyslot_evict"). - Sometimes (but not always!) it manages keyslots. Originally it always did, but device-mapper devices don't have keyslots themselves, so they use a "passthrough keyslot manager" which doesn't actually manage keyslots. This hack works, but the terminology is unnatural. Also, some hardware doesn't have keyslots and thus also uses a "passthrough keyslot manager" (support for such hardware is yet to be upstreamed, but it will happen eventually). Let's stop having keyslot managers which don't actually manage keyslots. Instead, rename blk_keyslot_manager to blk_crypto_profile. This is a fairly big change, since for consistency it also has to update keyslot manager-related function names, variable names, and comments -- not just the actual struct name. However it's still a fairly straightforward change, as it doesn't change any actual functionality. Acked-by: Ulf Hansson <[email protected]> # For MMC Reviewed-by: Mike Snitzer <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 1e8d44b commit cb77cb5

18 files changed

+555
-522
lines changed

block/blk-crypto-fallback.c

+35-36
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static struct blk_crypto_fallback_keyslot {
7878
struct crypto_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX];
7979
} *blk_crypto_keyslots;
8080

81-
static struct blk_keyslot_manager blk_crypto_ksm;
81+
static struct blk_crypto_profile blk_crypto_fallback_profile;
8282
static struct workqueue_struct *blk_crypto_wq;
8383
static mempool_t *blk_crypto_bounce_page_pool;
8484
static struct bio_set crypto_bio_split;
@@ -104,9 +104,10 @@ static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
104104
slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID;
105105
}
106106

107-
static int blk_crypto_fallback_keyslot_program(struct blk_keyslot_manager *ksm,
108-
const struct blk_crypto_key *key,
109-
unsigned int slot)
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)
110111
{
111112
struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
112113
const enum blk_crypto_mode_num crypto_mode =
@@ -127,22 +128,17 @@ static int blk_crypto_fallback_keyslot_program(struct blk_keyslot_manager *ksm,
127128
return 0;
128129
}
129130

130-
static int blk_crypto_fallback_keyslot_evict(struct blk_keyslot_manager *ksm,
131+
static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile,
131132
const struct blk_crypto_key *key,
132133
unsigned int slot)
133134
{
134135
blk_crypto_fallback_evict_keyslot(slot);
135136
return 0;
136137
}
137138

138-
/*
139-
* The crypto API fallback KSM ops - only used for a bio when it specifies a
140-
* blk_crypto_key that was not supported by the device's inline encryption
141-
* hardware.
142-
*/
143-
static const struct blk_ksm_ll_ops blk_crypto_ksm_ll_ops = {
144-
.keyslot_program = blk_crypto_fallback_keyslot_program,
145-
.keyslot_evict = blk_crypto_fallback_keyslot_evict,
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,
146142
};
147143

148144
static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio)
@@ -188,13 +184,13 @@ static struct bio *blk_crypto_fallback_clone_bio(struct bio *bio_src)
188184
}
189185

190186
static bool
191-
blk_crypto_fallback_alloc_cipher_req(struct blk_ksm_keyslot *slot,
187+
blk_crypto_fallback_alloc_cipher_req(struct blk_crypto_keyslot *slot,
192188
struct skcipher_request **ciph_req_ret,
193189
struct crypto_wait *wait)
194190
{
195191
struct skcipher_request *ciph_req;
196192
const struct blk_crypto_fallback_keyslot *slotp;
197-
int keyslot_idx = blk_ksm_get_slot_idx(slot);
193+
int keyslot_idx = blk_crypto_keyslot_index(slot);
198194

199195
slotp = &blk_crypto_keyslots[keyslot_idx];
200196
ciph_req = skcipher_request_alloc(slotp->tfms[slotp->crypto_mode],
@@ -266,7 +262,7 @@ static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
266262
{
267263
struct bio *src_bio, *enc_bio;
268264
struct bio_crypt_ctx *bc;
269-
struct blk_ksm_keyslot *slot;
265+
struct blk_crypto_keyslot *slot;
270266
int data_unit_size;
271267
struct skcipher_request *ciph_req = NULL;
272268
DECLARE_CRYPTO_WAIT(wait);
@@ -293,10 +289,11 @@ static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
293289
}
294290

295291
/*
296-
* Use the crypto API fallback keyslot manager to get a crypto_skcipher
297-
* for the algorithm and key specified for this bio.
292+
* Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
293+
* this bio's algorithm and key.
298294
*/
299-
blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot);
295+
blk_st = blk_crypto_get_keyslot(&blk_crypto_fallback_profile,
296+
bc->bc_key, &slot);
300297
if (blk_st != BLK_STS_OK) {
301298
src_bio->bi_status = blk_st;
302299
goto out_put_enc_bio;
@@ -364,7 +361,7 @@ static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
364361
out_free_ciph_req:
365362
skcipher_request_free(ciph_req);
366363
out_release_keyslot:
367-
blk_ksm_put_slot(slot);
364+
blk_crypto_put_keyslot(slot);
368365
out_put_enc_bio:
369366
if (enc_bio)
370367
bio_put(enc_bio);
@@ -382,7 +379,7 @@ static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
382379
container_of(work, struct bio_fallback_crypt_ctx, work);
383380
struct bio *bio = f_ctx->bio;
384381
struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx;
385-
struct blk_ksm_keyslot *slot;
382+
struct blk_crypto_keyslot *slot;
386383
struct skcipher_request *ciph_req = NULL;
387384
DECLARE_CRYPTO_WAIT(wait);
388385
u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
@@ -395,10 +392,11 @@ static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
395392
blk_status_t blk_st;
396393

397394
/*
398-
* Use the crypto API fallback keyslot manager to get a crypto_skcipher
399-
* for the algorithm and key specified for this bio.
395+
* Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
396+
* this bio's algorithm and key.
400397
*/
401-
blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot);
398+
blk_st = blk_crypto_get_keyslot(&blk_crypto_fallback_profile,
399+
bc->bc_key, &slot);
402400
if (blk_st != BLK_STS_OK) {
403401
bio->bi_status = blk_st;
404402
goto out_no_keyslot;
@@ -436,7 +434,7 @@ static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
436434

437435
out:
438436
skcipher_request_free(ciph_req);
439-
blk_ksm_put_slot(slot);
437+
blk_crypto_put_keyslot(slot);
440438
out_no_keyslot:
441439
mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
442440
bio_endio(bio);
@@ -501,8 +499,8 @@ bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
501499
return false;
502500
}
503501

504-
if (!blk_ksm_crypto_cfg_supported(&blk_crypto_ksm,
505-
&bc->bc_key->crypto_cfg)) {
502+
if (!__blk_crypto_cfg_supported(&blk_crypto_fallback_profile,
503+
&bc->bc_key->crypto_cfg)) {
506504
bio->bi_status = BLK_STS_NOTSUPP;
507505
return false;
508506
}
@@ -528,14 +526,15 @@ bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
528526

529527
int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
530528
{
531-
return blk_ksm_evict_key(&blk_crypto_ksm, key);
529+
return __blk_crypto_evict_key(&blk_crypto_fallback_profile, key);
532530
}
533531

534532
static bool blk_crypto_fallback_inited;
535533
static int blk_crypto_fallback_init(void)
536534
{
537535
int i;
538536
int err;
537+
struct blk_crypto_profile *profile = &blk_crypto_fallback_profile;
539538

540539
if (blk_crypto_fallback_inited)
541540
return 0;
@@ -546,24 +545,24 @@ static int blk_crypto_fallback_init(void)
546545
if (err)
547546
goto out;
548547

549-
err = blk_ksm_init(&blk_crypto_ksm, blk_crypto_num_keyslots);
548+
err = blk_crypto_profile_init(profile, blk_crypto_num_keyslots);
550549
if (err)
551550
goto fail_free_bioset;
552551
err = -ENOMEM;
553552

554-
blk_crypto_ksm.ksm_ll_ops = blk_crypto_ksm_ll_ops;
555-
blk_crypto_ksm.max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
553+
profile->ll_ops = blk_crypto_fallback_ll_ops;
554+
profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
556555

557556
/* All blk-crypto modes have a crypto API fallback. */
558557
for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++)
559-
blk_crypto_ksm.crypto_modes_supported[i] = 0xFFFFFFFF;
560-
blk_crypto_ksm.crypto_modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0;
558+
profile->modes_supported[i] = 0xFFFFFFFF;
559+
profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0;
561560

562561
blk_crypto_wq = alloc_workqueue("blk_crypto_wq",
563562
WQ_UNBOUND | WQ_HIGHPRI |
564563
WQ_MEM_RECLAIM, num_online_cpus());
565564
if (!blk_crypto_wq)
566-
goto fail_free_ksm;
565+
goto fail_destroy_profile;
567566

568567
blk_crypto_keyslots = kcalloc(blk_crypto_num_keyslots,
569568
sizeof(blk_crypto_keyslots[0]),
@@ -597,8 +596,8 @@ static int blk_crypto_fallback_init(void)
597596
kfree(blk_crypto_keyslots);
598597
fail_free_wq:
599598
destroy_workqueue(blk_crypto_wq);
600-
fail_free_ksm:
601-
blk_ksm_destroy(&blk_crypto_ksm);
599+
fail_destroy_profile:
600+
blk_crypto_profile_destroy(profile);
602601
fail_free_bioset:
603602
bioset_exit(&crypto_bio_split);
604603
out:

0 commit comments

Comments
 (0)