Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <ulf.hansson@linaro.org> # For MMC
Reviewed-by: Mike Snitzer <snitzer@redhat.com>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20211018180453.40441-4-ebiggers@kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Eric Biggers and committed by
Jens Axboe
cb77cb5a 1e8d44bd

+556 -523
+35 -36
block/blk-crypto-fallback.c
··· 78 78 struct crypto_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX]; 79 79 } *blk_crypto_keyslots; 80 80 81 - static struct blk_keyslot_manager blk_crypto_ksm; 81 + static struct blk_crypto_profile blk_crypto_fallback_profile; 82 82 static struct workqueue_struct *blk_crypto_wq; 83 83 static mempool_t *blk_crypto_bounce_page_pool; 84 84 static struct bio_set crypto_bio_split; ··· 104 104 slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID; 105 105 } 106 106 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) 110 111 { 111 112 struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot]; 112 113 const enum blk_crypto_mode_num crypto_mode = ··· 128 127 return 0; 129 128 } 130 129 131 - static int blk_crypto_fallback_keyslot_evict(struct blk_keyslot_manager *ksm, 130 + static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile, 132 131 const struct blk_crypto_key *key, 133 132 unsigned int slot) 134 133 { ··· 136 135 return 0; 137 136 } 138 137 139 - /* 140 - * The crypto API fallback KSM ops - only used for a bio when it specifies a 141 - * blk_crypto_key that was not supported by the device's inline encryption 142 - * hardware. 143 - */ 144 - static const struct blk_ksm_ll_ops blk_crypto_ksm_ll_ops = { 145 - .keyslot_program = blk_crypto_fallback_keyslot_program, 146 - .keyslot_evict = blk_crypto_fallback_keyslot_evict, 138 + static const struct blk_crypto_ll_ops blk_crypto_fallback_ll_ops = { 139 + .keyslot_program = blk_crypto_fallback_keyslot_program, 140 + .keyslot_evict = blk_crypto_fallback_keyslot_evict, 147 141 }; 148 142 149 143 static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio) ··· 184 188 } 185 189 186 190 static bool 187 - blk_crypto_fallback_alloc_cipher_req(struct blk_ksm_keyslot *slot, 191 + blk_crypto_fallback_alloc_cipher_req(struct blk_crypto_keyslot *slot, 188 192 struct skcipher_request **ciph_req_ret, 189 193 struct crypto_wait *wait) 190 194 { 191 195 struct skcipher_request *ciph_req; 192 196 const struct blk_crypto_fallback_keyslot *slotp; 193 - int keyslot_idx = blk_ksm_get_slot_idx(slot); 197 + int keyslot_idx = blk_crypto_keyslot_index(slot); 194 198 195 199 slotp = &blk_crypto_keyslots[keyslot_idx]; 196 200 ciph_req = skcipher_request_alloc(slotp->tfms[slotp->crypto_mode], ··· 262 266 { 263 267 struct bio *src_bio, *enc_bio; 264 268 struct bio_crypt_ctx *bc; 265 - struct blk_ksm_keyslot *slot; 269 + struct blk_crypto_keyslot *slot; 266 270 int data_unit_size; 267 271 struct skcipher_request *ciph_req = NULL; 268 272 DECLARE_CRYPTO_WAIT(wait); ··· 289 293 } 290 294 291 295 /* 292 - * Use the crypto API fallback keyslot manager to get a crypto_skcipher 293 - * for the algorithm and key specified for this bio. 296 + * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for 297 + * this bio's algorithm and key. 294 298 */ 295 - blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot); 299 + blk_st = blk_crypto_get_keyslot(&blk_crypto_fallback_profile, 300 + bc->bc_key, &slot); 296 301 if (blk_st != BLK_STS_OK) { 297 302 src_bio->bi_status = blk_st; 298 303 goto out_put_enc_bio; ··· 361 364 out_free_ciph_req: 362 365 skcipher_request_free(ciph_req); 363 366 out_release_keyslot: 364 - blk_ksm_put_slot(slot); 367 + blk_crypto_put_keyslot(slot); 365 368 out_put_enc_bio: 366 369 if (enc_bio) 367 370 bio_put(enc_bio); ··· 379 382 container_of(work, struct bio_fallback_crypt_ctx, work); 380 383 struct bio *bio = f_ctx->bio; 381 384 struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx; 382 - struct blk_ksm_keyslot *slot; 385 + struct blk_crypto_keyslot *slot; 383 386 struct skcipher_request *ciph_req = NULL; 384 387 DECLARE_CRYPTO_WAIT(wait); 385 388 u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; ··· 392 395 blk_status_t blk_st; 393 396 394 397 /* 395 - * Use the crypto API fallback keyslot manager to get a crypto_skcipher 396 - * for the algorithm and key specified for this bio. 398 + * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for 399 + * this bio's algorithm and key. 397 400 */ 398 - blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot); 401 + blk_st = blk_crypto_get_keyslot(&blk_crypto_fallback_profile, 402 + bc->bc_key, &slot); 399 403 if (blk_st != BLK_STS_OK) { 400 404 bio->bi_status = blk_st; 401 405 goto out_no_keyslot; ··· 434 436 435 437 out: 436 438 skcipher_request_free(ciph_req); 437 - blk_ksm_put_slot(slot); 439 + blk_crypto_put_keyslot(slot); 438 440 out_no_keyslot: 439 441 mempool_free(f_ctx, bio_fallback_crypt_ctx_pool); 440 442 bio_endio(bio); ··· 499 501 return false; 500 502 } 501 503 502 - if (!blk_ksm_crypto_cfg_supported(&blk_crypto_ksm, 503 - &bc->bc_key->crypto_cfg)) { 504 + if (!__blk_crypto_cfg_supported(&blk_crypto_fallback_profile, 505 + &bc->bc_key->crypto_cfg)) { 504 506 bio->bi_status = BLK_STS_NOTSUPP; 505 507 return false; 506 508 } ··· 526 528 527 529 int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key) 528 530 { 529 - return blk_ksm_evict_key(&blk_crypto_ksm, key); 531 + return __blk_crypto_evict_key(&blk_crypto_fallback_profile, key); 530 532 } 531 533 532 534 static bool blk_crypto_fallback_inited; ··· 534 536 { 535 537 int i; 536 538 int err; 539 + struct blk_crypto_profile *profile = &blk_crypto_fallback_profile; 537 540 538 541 if (blk_crypto_fallback_inited) 539 542 return 0; ··· 545 546 if (err) 546 547 goto out; 547 548 548 - err = blk_ksm_init(&blk_crypto_ksm, blk_crypto_num_keyslots); 549 + err = blk_crypto_profile_init(profile, blk_crypto_num_keyslots); 549 550 if (err) 550 551 goto fail_free_bioset; 551 552 err = -ENOMEM; 552 553 553 - blk_crypto_ksm.ksm_ll_ops = blk_crypto_ksm_ll_ops; 554 - blk_crypto_ksm.max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE; 554 + profile->ll_ops = blk_crypto_fallback_ll_ops; 555 + profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE; 555 556 556 557 /* All blk-crypto modes have a crypto API fallback. */ 557 558 for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) 558 - blk_crypto_ksm.crypto_modes_supported[i] = 0xFFFFFFFF; 559 - blk_crypto_ksm.crypto_modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0; 559 + profile->modes_supported[i] = 0xFFFFFFFF; 560 + profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0; 560 561 561 562 blk_crypto_wq = alloc_workqueue("blk_crypto_wq", 562 563 WQ_UNBOUND | WQ_HIGHPRI | 563 564 WQ_MEM_RECLAIM, num_online_cpus()); 564 565 if (!blk_crypto_wq) 565 - goto fail_free_ksm; 566 + goto fail_destroy_profile; 566 567 567 568 blk_crypto_keyslots = kcalloc(blk_crypto_num_keyslots, 568 569 sizeof(blk_crypto_keyslots[0]), ··· 596 597 kfree(blk_crypto_keyslots); 597 598 fail_free_wq: 598 599 destroy_workqueue(blk_crypto_wq); 599 - fail_free_ksm: 600 - blk_ksm_destroy(&blk_crypto_ksm); 600 + fail_destroy_profile: 601 + blk_crypto_profile_destroy(profile); 601 602 fail_free_bioset: 602 603 bioset_exit(&crypto_bio_split); 603 604 out:
+253 -267
block/blk-crypto-profile.c
··· 4 4 */ 5 5 6 6 /** 7 - * DOC: The Keyslot Manager 7 + * DOC: blk-crypto profiles 8 8 * 9 - * Many devices with inline encryption support have a limited number of "slots" 10 - * into which encryption contexts may be programmed, and requests can be tagged 11 - * with a slot number to specify the key to use for en/decryption. 9 + * 'struct blk_crypto_profile' contains all generic inline encryption-related 10 + * state for a particular inline encryption device. blk_crypto_profile serves 11 + * as the way that drivers for inline encryption hardware expose their crypto 12 + * capabilities and certain functions (e.g., functions to program and evict 13 + * keys) to upper layers. Device drivers that want to support inline encryption 14 + * construct a crypto profile, then associate it with the disk's request_queue. 12 15 * 13 - * As the number of slots is limited, and programming keys is expensive on 14 - * many inline encryption hardware, we don't want to program the same key into 15 - * multiple slots - if multiple requests are using the same key, we want to 16 - * program just one slot with that key and use that slot for all requests. 16 + * If the device has keyslots, then its blk_crypto_profile also handles managing 17 + * these keyslots in a device-independent way, using the driver-provided 18 + * functions to program and evict keys as needed. This includes keeping track 19 + * of which key and how many I/O requests are using each keyslot, getting 20 + * keyslots for I/O requests, and handling key eviction requests. 17 21 * 18 - * The keyslot manager manages these keyslots appropriately, and also acts as 19 - * an abstraction between the inline encryption hardware and the upper layers. 20 - * 21 - * Lower layer devices will set up a keyslot manager in their request queue 22 - * and tell it how to perform device specific operations like programming/ 23 - * evicting keys from keyslots. 24 - * 25 - * Upper layers will call blk_ksm_get_slot_for_key() to program a 26 - * key into some slot in the inline encryption hardware. 22 + * For more information, see Documentation/block/inline-encryption.rst. 27 23 */ 28 24 29 25 #define pr_fmt(fmt) "blk-crypto: " fmt ··· 33 37 #include <linux/blkdev.h> 34 38 #include <linux/blk-integrity.h> 35 39 36 - struct blk_ksm_keyslot { 40 + struct blk_crypto_keyslot { 37 41 atomic_t slot_refs; 38 42 struct list_head idle_slot_node; 39 43 struct hlist_node hash_node; 40 44 const struct blk_crypto_key *key; 41 - struct blk_keyslot_manager *ksm; 45 + struct blk_crypto_profile *profile; 42 46 }; 43 47 44 - static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm) 48 + static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile) 45 49 { 46 50 /* 47 - * Calling into the driver requires ksm->lock held and the device 51 + * Calling into the driver requires profile->lock held and the device 48 52 * resumed. But we must resume the device first, since that can acquire 49 - * and release ksm->lock via blk_ksm_reprogram_all_keys(). 53 + * and release profile->lock via blk_crypto_reprogram_all_keys(). 50 54 */ 51 - if (ksm->dev) 52 - pm_runtime_get_sync(ksm->dev); 53 - down_write(&ksm->lock); 55 + if (profile->dev) 56 + pm_runtime_get_sync(profile->dev); 57 + down_write(&profile->lock); 54 58 } 55 59 56 - static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm) 60 + static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile) 57 61 { 58 - up_write(&ksm->lock); 59 - if (ksm->dev) 60 - pm_runtime_put_sync(ksm->dev); 61 - } 62 - 63 - static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm) 64 - { 65 - return ksm->num_slots == 0; 62 + up_write(&profile->lock); 63 + if (profile->dev) 64 + pm_runtime_put_sync(profile->dev); 66 65 } 67 66 68 67 /** 69 - * blk_ksm_init() - Initialize a keyslot manager 70 - * @ksm: The keyslot_manager to initialize. 71 - * @num_slots: The number of key slots to manage. 68 + * blk_crypto_profile_init() - Initialize a blk_crypto_profile 69 + * @profile: the blk_crypto_profile to initialize 70 + * @num_slots: the number of keyslots 72 71 * 73 - * Allocate memory for keyslots and initialize a keyslot manager. Called by 74 - * e.g. storage drivers to set up a keyslot manager in their request_queue. 72 + * Storage drivers must call this when starting to set up a blk_crypto_profile, 73 + * before filling in additional fields. 75 74 * 76 75 * Return: 0 on success, or else a negative error code. 77 76 */ 78 - int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) 77 + int blk_crypto_profile_init(struct blk_crypto_profile *profile, 78 + unsigned int num_slots) 79 79 { 80 80 unsigned int slot; 81 81 unsigned int i; 82 82 unsigned int slot_hashtable_size; 83 83 84 - memset(ksm, 0, sizeof(*ksm)); 84 + memset(profile, 0, sizeof(*profile)); 85 + init_rwsem(&profile->lock); 85 86 86 87 if (num_slots == 0) 87 - return -EINVAL; 88 + return 0; 88 89 89 - ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL); 90 - if (!ksm->slots) 90 + /* Initialize keyslot management data. */ 91 + 92 + profile->slots = kvcalloc(num_slots, sizeof(profile->slots[0]), 93 + GFP_KERNEL); 94 + if (!profile->slots) 91 95 return -ENOMEM; 92 96 93 - ksm->num_slots = num_slots; 97 + profile->num_slots = num_slots; 94 98 95 - init_rwsem(&ksm->lock); 96 - 97 - init_waitqueue_head(&ksm->idle_slots_wait_queue); 98 - INIT_LIST_HEAD(&ksm->idle_slots); 99 + init_waitqueue_head(&profile->idle_slots_wait_queue); 100 + INIT_LIST_HEAD(&profile->idle_slots); 99 101 100 102 for (slot = 0; slot < num_slots; slot++) { 101 - ksm->slots[slot].ksm = ksm; 102 - list_add_tail(&ksm->slots[slot].idle_slot_node, 103 - &ksm->idle_slots); 103 + profile->slots[slot].profile = profile; 104 + list_add_tail(&profile->slots[slot].idle_slot_node, 105 + &profile->idle_slots); 104 106 } 105 107 106 - spin_lock_init(&ksm->idle_slots_lock); 108 + spin_lock_init(&profile->idle_slots_lock); 107 109 108 110 slot_hashtable_size = roundup_pow_of_two(num_slots); 109 111 /* ··· 111 117 if (slot_hashtable_size < 2) 112 118 slot_hashtable_size = 2; 113 119 114 - ksm->log_slot_ht_size = ilog2(slot_hashtable_size); 115 - ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size, 116 - sizeof(ksm->slot_hashtable[0]), 117 - GFP_KERNEL); 118 - if (!ksm->slot_hashtable) 119 - goto err_destroy_ksm; 120 + profile->log_slot_ht_size = ilog2(slot_hashtable_size); 121 + profile->slot_hashtable = 122 + kvmalloc_array(slot_hashtable_size, 123 + sizeof(profile->slot_hashtable[0]), GFP_KERNEL); 124 + if (!profile->slot_hashtable) 125 + goto err_destroy; 120 126 for (i = 0; i < slot_hashtable_size; i++) 121 - INIT_HLIST_HEAD(&ksm->slot_hashtable[i]); 127 + INIT_HLIST_HEAD(&profile->slot_hashtable[i]); 122 128 123 129 return 0; 124 130 125 - err_destroy_ksm: 126 - blk_ksm_destroy(ksm); 131 + err_destroy: 132 + blk_crypto_profile_destroy(profile); 127 133 return -ENOMEM; 128 134 } 129 - EXPORT_SYMBOL_GPL(blk_ksm_init); 135 + EXPORT_SYMBOL_GPL(blk_crypto_profile_init); 130 136 131 - static void blk_ksm_destroy_callback(void *ksm) 137 + static void blk_crypto_profile_destroy_callback(void *profile) 132 138 { 133 - blk_ksm_destroy(ksm); 139 + blk_crypto_profile_destroy(profile); 134 140 } 135 141 136 142 /** 137 - * devm_blk_ksm_init() - Resource-managed blk_ksm_init() 138 - * @dev: The device which owns the blk_keyslot_manager. 139 - * @ksm: The blk_keyslot_manager to initialize. 140 - * @num_slots: The number of key slots to manage. 143 + * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init() 144 + * @dev: the device which owns the blk_crypto_profile 145 + * @profile: the blk_crypto_profile to initialize 146 + * @num_slots: the number of keyslots 141 147 * 142 - * Like blk_ksm_init(), but causes blk_ksm_destroy() to be called automatically 143 - * on driver detach. 148 + * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be 149 + * called automatically on driver detach. 144 150 * 145 151 * Return: 0 on success, or else a negative error code. 146 152 */ 147 - int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm, 148 - unsigned int num_slots) 153 + int devm_blk_crypto_profile_init(struct device *dev, 154 + struct blk_crypto_profile *profile, 155 + unsigned int num_slots) 149 156 { 150 - int err = blk_ksm_init(ksm, num_slots); 157 + int err = blk_crypto_profile_init(profile, num_slots); 151 158 152 159 if (err) 153 160 return err; 154 161 155 - return devm_add_action_or_reset(dev, blk_ksm_destroy_callback, ksm); 162 + return devm_add_action_or_reset(dev, 163 + blk_crypto_profile_destroy_callback, 164 + profile); 156 165 } 157 - EXPORT_SYMBOL_GPL(devm_blk_ksm_init); 166 + EXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init); 158 167 159 168 static inline struct hlist_head * 160 - blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm, 161 - const struct blk_crypto_key *key) 169 + blk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile, 170 + const struct blk_crypto_key *key) 162 171 { 163 - return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)]; 172 + return &profile->slot_hashtable[ 173 + hash_ptr(key, profile->log_slot_ht_size)]; 164 174 } 165 175 166 - static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot) 176 + static void 177 + blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot) 167 178 { 168 - struct blk_keyslot_manager *ksm = slot->ksm; 179 + struct blk_crypto_profile *profile = slot->profile; 169 180 unsigned long flags; 170 181 171 - spin_lock_irqsave(&ksm->idle_slots_lock, flags); 182 + spin_lock_irqsave(&profile->idle_slots_lock, flags); 172 183 list_del(&slot->idle_slot_node); 173 - spin_unlock_irqrestore(&ksm->idle_slots_lock, flags); 184 + spin_unlock_irqrestore(&profile->idle_slots_lock, flags); 174 185 } 175 186 176 - static struct blk_ksm_keyslot *blk_ksm_find_keyslot( 177 - struct blk_keyslot_manager *ksm, 178 - const struct blk_crypto_key *key) 187 + static struct blk_crypto_keyslot * 188 + blk_crypto_find_keyslot(struct blk_crypto_profile *profile, 189 + const struct blk_crypto_key *key) 179 190 { 180 - const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key); 181 - struct blk_ksm_keyslot *slotp; 191 + const struct hlist_head *head = 192 + blk_crypto_hash_bucket_for_key(profile, key); 193 + struct blk_crypto_keyslot *slotp; 182 194 183 195 hlist_for_each_entry(slotp, head, hash_node) { 184 196 if (slotp->key == key) ··· 193 193 return NULL; 194 194 } 195 195 196 - static struct blk_ksm_keyslot *blk_ksm_find_and_grab_keyslot( 197 - struct blk_keyslot_manager *ksm, 198 - const struct blk_crypto_key *key) 196 + static struct blk_crypto_keyslot * 197 + blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile, 198 + const struct blk_crypto_key *key) 199 199 { 200 - struct blk_ksm_keyslot *slot; 200 + struct blk_crypto_keyslot *slot; 201 201 202 - slot = blk_ksm_find_keyslot(ksm, key); 202 + slot = blk_crypto_find_keyslot(profile, key); 203 203 if (!slot) 204 204 return NULL; 205 205 if (atomic_inc_return(&slot->slot_refs) == 1) { 206 206 /* Took first reference to this slot; remove it from LRU list */ 207 - blk_ksm_remove_slot_from_lru_list(slot); 207 + blk_crypto_remove_slot_from_lru_list(slot); 208 208 } 209 209 return slot; 210 210 } 211 211 212 - unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot) 212 + /** 213 + * blk_crypto_keyslot_index() - Get the index of a keyslot 214 + * @slot: a keyslot that blk_crypto_get_keyslot() returned 215 + * 216 + * Return: the 0-based index of the keyslot within the device's keyslots. 217 + */ 218 + unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot) 213 219 { 214 - return slot - slot->ksm->slots; 220 + return slot - slot->profile->slots; 215 221 } 216 - EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx); 222 + EXPORT_SYMBOL_GPL(blk_crypto_keyslot_index); 217 223 218 224 /** 219 - * blk_ksm_get_slot_for_key() - Program a key into a keyslot. 220 - * @ksm: The keyslot manager to program the key into. 221 - * @key: Pointer to the key object to program, including the raw key, crypto 222 - * mode, and data unit size. 223 - * @slot_ptr: A pointer to return the pointer of the allocated keyslot. 225 + * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed. 226 + * @profile: the crypto profile of the device the key will be used on 227 + * @key: the key that will be used 228 + * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct 229 + * will be stored here; otherwise NULL will be stored here. 224 230 * 225 - * Get a keyslot that's been programmed with the specified key. If one already 226 - * exists, return it with incremented refcount. Otherwise, wait for a keyslot 227 - * to become idle and program it. 231 + * If the device has keyslots, this gets a keyslot that's been programmed with 232 + * the specified key. If the key is already in a slot, this reuses it; 233 + * otherwise this waits for a slot to become idle and programs the key into it. 228 234 * 229 - * Context: Process context. Takes and releases ksm->lock. 230 - * Return: BLK_STS_OK on success (and keyslot is set to the pointer of the 231 - * allocated keyslot), or some other blk_status_t otherwise (and 232 - * keyslot is set to NULL). 235 + * This must be paired with a call to blk_crypto_put_keyslot(). 236 + * 237 + * Context: Process context. Takes and releases profile->lock. 238 + * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or 239 + * one wasn't needed; or a blk_status_t error on failure. 233 240 */ 234 - blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm, 235 - const struct blk_crypto_key *key, 236 - struct blk_ksm_keyslot **slot_ptr) 241 + blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile, 242 + const struct blk_crypto_key *key, 243 + struct blk_crypto_keyslot **slot_ptr) 237 244 { 238 - struct blk_ksm_keyslot *slot; 245 + struct blk_crypto_keyslot *slot; 239 246 int slot_idx; 240 247 int err; 241 248 242 249 *slot_ptr = NULL; 243 250 244 - if (blk_ksm_is_passthrough(ksm)) 251 + /* 252 + * If the device has no concept of "keyslots", then there is no need to 253 + * get one. 254 + */ 255 + if (profile->num_slots == 0) 245 256 return BLK_STS_OK; 246 257 247 - down_read(&ksm->lock); 248 - slot = blk_ksm_find_and_grab_keyslot(ksm, key); 249 - up_read(&ksm->lock); 258 + down_read(&profile->lock); 259 + slot = blk_crypto_find_and_grab_keyslot(profile, key); 260 + up_read(&profile->lock); 250 261 if (slot) 251 262 goto success; 252 263 253 264 for (;;) { 254 - blk_ksm_hw_enter(ksm); 255 - slot = blk_ksm_find_and_grab_keyslot(ksm, key); 265 + blk_crypto_hw_enter(profile); 266 + slot = blk_crypto_find_and_grab_keyslot(profile, key); 256 267 if (slot) { 257 - blk_ksm_hw_exit(ksm); 268 + blk_crypto_hw_exit(profile); 258 269 goto success; 259 270 } 260 271 ··· 273 262 * If we're here, that means there wasn't a slot that was 274 263 * already programmed with the key. So try to program it. 275 264 */ 276 - if (!list_empty(&ksm->idle_slots)) 265 + if (!list_empty(&profile->idle_slots)) 277 266 break; 278 267 279 - blk_ksm_hw_exit(ksm); 280 - wait_event(ksm->idle_slots_wait_queue, 281 - !list_empty(&ksm->idle_slots)); 268 + blk_crypto_hw_exit(profile); 269 + wait_event(profile->idle_slots_wait_queue, 270 + !list_empty(&profile->idle_slots)); 282 271 } 283 272 284 - slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot, 273 + slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot, 285 274 idle_slot_node); 286 - slot_idx = blk_ksm_get_slot_idx(slot); 275 + slot_idx = blk_crypto_keyslot_index(slot); 287 276 288 - err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx); 277 + err = profile->ll_ops.keyslot_program(profile, key, slot_idx); 289 278 if (err) { 290 - wake_up(&ksm->idle_slots_wait_queue); 291 - blk_ksm_hw_exit(ksm); 279 + wake_up(&profile->idle_slots_wait_queue); 280 + blk_crypto_hw_exit(profile); 292 281 return errno_to_blk_status(err); 293 282 } 294 283 ··· 296 285 if (slot->key) 297 286 hlist_del(&slot->hash_node); 298 287 slot->key = key; 299 - hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key)); 288 + hlist_add_head(&slot->hash_node, 289 + blk_crypto_hash_bucket_for_key(profile, key)); 300 290 301 291 atomic_set(&slot->slot_refs, 1); 302 292 303 - blk_ksm_remove_slot_from_lru_list(slot); 293 + blk_crypto_remove_slot_from_lru_list(slot); 304 294 305 - blk_ksm_hw_exit(ksm); 295 + blk_crypto_hw_exit(profile); 306 296 success: 307 297 *slot_ptr = slot; 308 298 return BLK_STS_OK; 309 299 } 310 300 311 301 /** 312 - * blk_ksm_put_slot() - Release a reference to a slot 313 - * @slot: The keyslot to release the reference of. 302 + * blk_crypto_put_keyslot() - Release a reference to a keyslot 303 + * @slot: The keyslot to release the reference of (may be NULL). 314 304 * 315 305 * Context: Any context. 316 306 */ 317 - void blk_ksm_put_slot(struct blk_ksm_keyslot *slot) 307 + void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot) 318 308 { 319 - struct blk_keyslot_manager *ksm; 309 + struct blk_crypto_profile *profile; 320 310 unsigned long flags; 321 311 322 312 if (!slot) 323 313 return; 324 314 325 - ksm = slot->ksm; 315 + profile = slot->profile; 326 316 327 317 if (atomic_dec_and_lock_irqsave(&slot->slot_refs, 328 - &ksm->idle_slots_lock, flags)) { 329 - list_add_tail(&slot->idle_slot_node, &ksm->idle_slots); 330 - spin_unlock_irqrestore(&ksm->idle_slots_lock, flags); 331 - wake_up(&ksm->idle_slots_wait_queue); 318 + &profile->idle_slots_lock, flags)) { 319 + list_add_tail(&slot->idle_slot_node, &profile->idle_slots); 320 + spin_unlock_irqrestore(&profile->idle_slots_lock, flags); 321 + wake_up(&profile->idle_slots_wait_queue); 332 322 } 333 323 } 334 324 335 325 /** 336 - * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is 337 - * supported by a ksm. 338 - * @ksm: The keyslot manager to check 339 - * @cfg: The crypto configuration to check for. 326 + * __blk_crypto_cfg_supported() - Check whether the given crypto profile 327 + * supports the given crypto configuration. 328 + * @profile: the crypto profile to check 329 + * @cfg: the crypto configuration to check for 340 330 * 341 - * Checks for crypto_mode/data unit size/dun bytes support. 342 - * 343 - * Return: Whether or not this ksm supports the specified crypto config. 331 + * Return: %true if @profile supports the given @cfg. 344 332 */ 345 - bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm, 346 - const struct blk_crypto_config *cfg) 333 + bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile, 334 + const struct blk_crypto_config *cfg) 347 335 { 348 - if (!ksm) 336 + if (!profile) 349 337 return false; 350 - if (!(ksm->crypto_modes_supported[cfg->crypto_mode] & 351 - cfg->data_unit_size)) 338 + if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size)) 352 339 return false; 353 - if (ksm->max_dun_bytes_supported < cfg->dun_bytes) 340 + if (profile->max_dun_bytes_supported < cfg->dun_bytes) 354 341 return false; 355 342 return true; 356 343 } 357 344 358 345 /** 359 - * blk_ksm_evict_key() - Evict a key from the lower layer device. 360 - * @ksm: The keyslot manager to evict from 361 - * @key: The key to evict 346 + * __blk_crypto_evict_key() - Evict a key from a device. 347 + * @profile: the crypto profile of the device 348 + * @key: the key to evict. It must not still be used in any I/O. 362 349 * 363 - * Find the keyslot that the specified key was programmed into, and evict that 364 - * slot from the lower layer device. The slot must not be in use by any 365 - * in-flight IO when this function is called. 350 + * If the device has keyslots, this finds the keyslot (if any) that contains the 351 + * specified key and calls the driver's keyslot_evict function to evict it. 366 352 * 367 - * Context: Process context. Takes and releases ksm->lock. 353 + * Otherwise, this just calls the driver's keyslot_evict function if it is 354 + * implemented, passing just the key (without any particular keyslot). This 355 + * allows layered devices to evict the key from their underlying devices. 356 + * 357 + * Context: Process context. Takes and releases profile->lock. 368 358 * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY 369 359 * if the keyslot is still in use, or another -errno value on other 370 360 * error. 371 361 */ 372 - int blk_ksm_evict_key(struct blk_keyslot_manager *ksm, 373 - const struct blk_crypto_key *key) 362 + int __blk_crypto_evict_key(struct blk_crypto_profile *profile, 363 + const struct blk_crypto_key *key) 374 364 { 375 - struct blk_ksm_keyslot *slot; 365 + struct blk_crypto_keyslot *slot; 376 366 int err = 0; 377 367 378 - if (blk_ksm_is_passthrough(ksm)) { 379 - if (ksm->ksm_ll_ops.keyslot_evict) { 380 - blk_ksm_hw_enter(ksm); 381 - err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1); 382 - blk_ksm_hw_exit(ksm); 368 + if (profile->num_slots == 0) { 369 + if (profile->ll_ops.keyslot_evict) { 370 + blk_crypto_hw_enter(profile); 371 + err = profile->ll_ops.keyslot_evict(profile, key, -1); 372 + blk_crypto_hw_exit(profile); 383 373 return err; 384 374 } 385 375 return 0; 386 376 } 387 377 388 - blk_ksm_hw_enter(ksm); 389 - slot = blk_ksm_find_keyslot(ksm, key); 378 + blk_crypto_hw_enter(profile); 379 + slot = blk_crypto_find_keyslot(profile, key); 390 380 if (!slot) 391 381 goto out_unlock; 392 382 ··· 395 383 err = -EBUSY; 396 384 goto out_unlock; 397 385 } 398 - err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, 399 - blk_ksm_get_slot_idx(slot)); 386 + err = profile->ll_ops.keyslot_evict(profile, key, 387 + blk_crypto_keyslot_index(slot)); 400 388 if (err) 401 389 goto out_unlock; 402 390 ··· 404 392 slot->key = NULL; 405 393 err = 0; 406 394 out_unlock: 407 - blk_ksm_hw_exit(ksm); 395 + blk_crypto_hw_exit(profile); 408 396 return err; 409 397 } 410 398 411 399 /** 412 - * blk_ksm_reprogram_all_keys() - Re-program all keyslots. 413 - * @ksm: The keyslot manager 400 + * blk_crypto_reprogram_all_keys() - Re-program all keyslots. 401 + * @profile: The crypto profile 414 402 * 415 403 * Re-program all keyslots that are supposed to have a key programmed. This is 416 404 * intended only for use by drivers for hardware that loses its keys on reset. 417 405 * 418 - * Context: Process context. Takes and releases ksm->lock. 406 + * Context: Process context. Takes and releases profile->lock. 419 407 */ 420 - void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm) 408 + void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile) 421 409 { 422 410 unsigned int slot; 423 411 424 - if (blk_ksm_is_passthrough(ksm)) 412 + if (profile->num_slots == 0) 425 413 return; 426 414 427 415 /* This is for device initialization, so don't resume the device */ 428 - down_write(&ksm->lock); 429 - for (slot = 0; slot < ksm->num_slots; slot++) { 430 - const struct blk_crypto_key *key = ksm->slots[slot].key; 416 + down_write(&profile->lock); 417 + for (slot = 0; slot < profile->num_slots; slot++) { 418 + const struct blk_crypto_key *key = profile->slots[slot].key; 431 419 int err; 432 420 433 421 if (!key) 434 422 continue; 435 423 436 - err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot); 424 + err = profile->ll_ops.keyslot_program(profile, key, slot); 437 425 WARN_ON(err); 438 426 } 439 - up_write(&ksm->lock); 427 + up_write(&profile->lock); 440 428 } 441 - EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys); 429 + EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys); 442 430 443 - void blk_ksm_destroy(struct blk_keyslot_manager *ksm) 431 + void blk_crypto_profile_destroy(struct blk_crypto_profile *profile) 444 432 { 445 - if (!ksm) 433 + if (!profile) 446 434 return; 447 - kvfree(ksm->slot_hashtable); 448 - kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots); 449 - memzero_explicit(ksm, sizeof(*ksm)); 435 + kvfree(profile->slot_hashtable); 436 + kvfree_sensitive(profile->slots, 437 + sizeof(profile->slots[0]) * profile->num_slots); 438 + memzero_explicit(profile, sizeof(*profile)); 450 439 } 451 - EXPORT_SYMBOL_GPL(blk_ksm_destroy); 440 + EXPORT_SYMBOL_GPL(blk_crypto_profile_destroy); 452 441 453 - bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q) 442 + bool blk_crypto_register(struct blk_crypto_profile *profile, 443 + struct request_queue *q) 454 444 { 455 445 if (blk_integrity_queue_supports_integrity(q)) { 456 446 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n"); 457 447 return false; 458 448 } 459 - q->ksm = ksm; 449 + q->crypto_profile = profile; 460 450 return true; 461 451 } 462 - EXPORT_SYMBOL_GPL(blk_ksm_register); 452 + EXPORT_SYMBOL_GPL(blk_crypto_register); 463 453 464 - void blk_ksm_unregister(struct request_queue *q) 454 + void blk_crypto_unregister(struct request_queue *q) 465 455 { 466 - q->ksm = NULL; 456 + q->crypto_profile = NULL; 467 457 } 468 458 469 459 /** 470 - * blk_ksm_intersect_modes() - restrict supported modes by child device 471 - * @parent: The keyslot manager for parent device 472 - * @child: The keyslot manager for child device, or NULL 460 + * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities 461 + * by child device 462 + * @parent: the crypto profile for the parent device 463 + * @child: the crypto profile for the child device, or NULL 473 464 * 474 - * Clear any crypto mode support bits in @parent that aren't set in @child. 475 - * If @child is NULL, then all parent bits are cleared. 465 + * This clears all crypto capabilities in @parent that aren't set in @child. If 466 + * @child is NULL, then this clears all parent capabilities. 476 467 * 477 - * Only use this when setting up the keyslot manager for a layered device, 478 - * before it's been exposed yet. 468 + * Only use this when setting up the crypto profile for a layered device, before 469 + * it's been exposed yet. 479 470 */ 480 - void blk_ksm_intersect_modes(struct blk_keyslot_manager *parent, 481 - const struct blk_keyslot_manager *child) 471 + void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent, 472 + const struct blk_crypto_profile *child) 482 473 { 483 474 if (child) { 484 475 unsigned int i; ··· 489 474 parent->max_dun_bytes_supported = 490 475 min(parent->max_dun_bytes_supported, 491 476 child->max_dun_bytes_supported); 492 - for (i = 0; i < ARRAY_SIZE(child->crypto_modes_supported); 493 - i++) { 494 - parent->crypto_modes_supported[i] &= 495 - child->crypto_modes_supported[i]; 496 - } 477 + for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++) 478 + parent->modes_supported[i] &= child->modes_supported[i]; 497 479 } else { 498 480 parent->max_dun_bytes_supported = 0; 499 - memset(parent->crypto_modes_supported, 0, 500 - sizeof(parent->crypto_modes_supported)); 481 + memset(parent->modes_supported, 0, 482 + sizeof(parent->modes_supported)); 501 483 } 502 484 } 503 - EXPORT_SYMBOL_GPL(blk_ksm_intersect_modes); 485 + EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities); 504 486 505 487 /** 506 - * blk_ksm_is_superset() - Check if a KSM supports a superset of crypto modes 507 - * and DUN bytes that another KSM supports. Here, 508 - * "superset" refers to the mathematical meaning of the 509 - * word - i.e. if two KSMs have the *same* capabilities, 510 - * they *are* considered supersets of each other. 511 - * @ksm_superset: The KSM that we want to verify is a superset 512 - * @ksm_subset: The KSM that we want to verify is a subset 488 + * blk_crypto_has_capabilities() - Check whether @target supports at least all 489 + * the crypto capabilities that @reference does. 490 + * @target: the target profile 491 + * @reference: the reference profile 513 492 * 514 - * Return: True if @ksm_superset supports a superset of the crypto modes and DUN 515 - * bytes that @ksm_subset supports. 493 + * Return: %true if @target supports all the crypto capabilities of @reference. 516 494 */ 517 - bool blk_ksm_is_superset(struct blk_keyslot_manager *ksm_superset, 518 - struct blk_keyslot_manager *ksm_subset) 495 + bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target, 496 + const struct blk_crypto_profile *reference) 519 497 { 520 498 int i; 521 499 522 - if (!ksm_subset) 500 + if (!reference) 523 501 return true; 524 502 525 - if (!ksm_superset) 503 + if (!target) 526 504 return false; 527 505 528 - for (i = 0; i < ARRAY_SIZE(ksm_superset->crypto_modes_supported); i++) { 529 - if (ksm_subset->crypto_modes_supported[i] & 530 - (~ksm_superset->crypto_modes_supported[i])) { 506 + for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) { 507 + if (reference->modes_supported[i] & ~target->modes_supported[i]) 531 508 return false; 532 - } 533 509 } 534 510 535 - if (ksm_subset->max_dun_bytes_supported > 536 - ksm_superset->max_dun_bytes_supported) { 511 + if (reference->max_dun_bytes_supported > 512 + target->max_dun_bytes_supported) 537 513 return false; 538 - } 539 514 540 515 return true; 541 516 } 542 - EXPORT_SYMBOL_GPL(blk_ksm_is_superset); 517 + EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities); 543 518 544 519 /** 545 - * blk_ksm_update_capabilities() - Update the restrictions of a KSM to those of 546 - * another KSM 547 - * @target_ksm: The KSM whose restrictions to update. 548 - * @reference_ksm: The KSM to whose restrictions this function will update 549 - * @target_ksm's restrictions to. 520 + * blk_crypto_update_capabilities() - Update the capabilities of a crypto 521 + * profile to match those of another crypto 522 + * profile. 523 + * @dst: The crypto profile whose capabilities to update. 524 + * @src: The crypto profile whose capabilities this function will update @dst's 525 + * capabilities to. 550 526 * 551 527 * Blk-crypto requires that crypto capabilities that were 552 528 * advertised when a bio was created continue to be supported by the 553 529 * device until that bio is ended. This is turn means that a device cannot 554 530 * shrink its advertised crypto capabilities without any explicit 555 531 * synchronization with upper layers. So if there's no such explicit 556 - * synchronization, @reference_ksm must support all the crypto capabilities that 557 - * @target_ksm does 558 - * (i.e. we need blk_ksm_is_superset(@reference_ksm, @target_ksm) == true). 532 + * synchronization, @src must support all the crypto capabilities that 533 + * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)). 559 534 * 560 535 * Note also that as long as the crypto capabilities are being expanded, the 561 536 * order of updates becoming visible is not important because it's alright ··· 554 549 * might result in blk-crypto-fallback being used if available, or the bio being 555 550 * failed). 556 551 */ 557 - void blk_ksm_update_capabilities(struct blk_keyslot_manager *target_ksm, 558 - struct blk_keyslot_manager *reference_ksm) 552 + void blk_crypto_update_capabilities(struct blk_crypto_profile *dst, 553 + const struct blk_crypto_profile *src) 559 554 { 560 - memcpy(target_ksm->crypto_modes_supported, 561 - reference_ksm->crypto_modes_supported, 562 - sizeof(target_ksm->crypto_modes_supported)); 555 + memcpy(dst->modes_supported, src->modes_supported, 556 + sizeof(dst->modes_supported)); 563 557 564 - target_ksm->max_dun_bytes_supported = 565 - reference_ksm->max_dun_bytes_supported; 558 + dst->max_dun_bytes_supported = src->max_dun_bytes_supported; 566 559 } 567 - EXPORT_SYMBOL_GPL(blk_ksm_update_capabilities); 568 - 569 - /** 570 - * blk_ksm_init_passthrough() - Init a passthrough keyslot manager 571 - * @ksm: The keyslot manager to init 572 - * 573 - * Initialize a passthrough keyslot manager. 574 - * Called by e.g. storage drivers to set up a keyslot manager in their 575 - * request_queue, when the storage driver wants to manage its keys by itself. 576 - * This is useful for inline encryption hardware that doesn't have the concept 577 - * of keyslots, and for layered devices. 578 - */ 579 - void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm) 580 - { 581 - memset(ksm, 0, sizeof(*ksm)); 582 - init_rwsem(&ksm->lock); 583 - } 584 - EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough); 560 + EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);
+14 -13
block/blk-crypto.c
··· 218 218 219 219 blk_status_t __blk_crypto_init_request(struct request *rq) 220 220 { 221 - return blk_ksm_get_slot_for_key(rq->q->ksm, rq->crypt_ctx->bc_key, 222 - &rq->crypt_keyslot); 221 + return blk_crypto_get_keyslot(rq->q->crypto_profile, 222 + rq->crypt_ctx->bc_key, 223 + &rq->crypt_keyslot); 223 224 } 224 225 225 226 /** ··· 234 233 */ 235 234 void __blk_crypto_free_request(struct request *rq) 236 235 { 237 - blk_ksm_put_slot(rq->crypt_keyslot); 236 + blk_crypto_put_keyslot(rq->crypt_keyslot); 238 237 mempool_free(rq->crypt_ctx, bio_crypt_ctx_pool); 239 238 blk_crypto_rq_set_defaults(rq); 240 239 } ··· 265 264 { 266 265 struct bio *bio = *bio_ptr; 267 266 const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key; 267 + struct blk_crypto_profile *profile; 268 268 269 269 /* Error if bio has no data. */ 270 270 if (WARN_ON_ONCE(!bio_has_data(bio))) { ··· 282 280 * Success if device supports the encryption context, or if we succeeded 283 281 * in falling back to the crypto API. 284 282 */ 285 - if (blk_ksm_crypto_cfg_supported(bdev_get_queue(bio->bi_bdev)->ksm, 286 - &bc_key->crypto_cfg)) 283 + profile = bdev_get_queue(bio->bi_bdev)->crypto_profile; 284 + if (__blk_crypto_cfg_supported(profile, &bc_key->crypto_cfg)) 287 285 return true; 288 286 289 287 if (blk_crypto_fallback_bio_prep(bio_ptr)) ··· 359 357 const struct blk_crypto_config *cfg) 360 358 { 361 359 return IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) || 362 - blk_ksm_crypto_cfg_supported(q->ksm, cfg); 360 + __blk_crypto_cfg_supported(q->crypto_profile, cfg); 363 361 } 364 362 365 363 /** ··· 380 378 int blk_crypto_start_using_key(const struct blk_crypto_key *key, 381 379 struct request_queue *q) 382 380 { 383 - if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg)) 381 + if (__blk_crypto_cfg_supported(q->crypto_profile, &key->crypto_cfg)) 384 382 return 0; 385 383 return blk_crypto_fallback_start_using_mode(key->crypto_cfg.crypto_mode); 386 384 } ··· 396 394 * evicted from any hardware that it might have been programmed into. The key 397 395 * must not be in use by any in-flight IO when this function is called. 398 396 * 399 - * Return: 0 on success or if key is not present in the q's ksm, -err on error. 397 + * Return: 0 on success or if the key wasn't in any keyslot; -errno on error. 400 398 */ 401 399 int blk_crypto_evict_key(struct request_queue *q, 402 400 const struct blk_crypto_key *key) 403 401 { 404 - if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg)) 405 - return blk_ksm_evict_key(q->ksm, key); 402 + if (__blk_crypto_cfg_supported(q->crypto_profile, &key->crypto_cfg)) 403 + return __blk_crypto_evict_key(q->crypto_profile, key); 406 404 407 405 /* 408 - * If the request queue's associated inline encryption hardware didn't 409 - * have support for the key, then the key might have been programmed 410 - * into the fallback keyslot manager, so try to evict from there. 406 + * If the request_queue didn't support the key, then blk-crypto-fallback 407 + * may have been used, so try to evict the key from blk-crypto-fallback. 411 408 */ 412 409 return blk_crypto_fallback_evict_key(key); 413 410 }
+2 -2
block/blk-integrity.c
··· 409 409 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, disk->queue); 410 410 411 411 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 412 - if (disk->queue->ksm) { 412 + if (disk->queue->crypto_profile) { 413 413 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n"); 414 - blk_ksm_unregister(disk->queue); 414 + blk_crypto_unregister(disk->queue); 415 415 } 416 416 #endif 417 417 }
+1 -1
drivers/md/dm-core.h
··· 200 200 struct dm_md_mempools *mempools; 201 201 202 202 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 203 - struct blk_keyslot_manager *ksm; 203 + struct blk_crypto_profile *crypto_profile; 204 204 #endif 205 205 }; 206 206
+82 -86
drivers/md/dm-table.c
··· 170 170 } 171 171 } 172 172 173 - static void dm_table_destroy_keyslot_manager(struct dm_table *t); 173 + static void dm_table_destroy_crypto_profile(struct dm_table *t); 174 174 175 175 void dm_table_destroy(struct dm_table *t) 176 176 { ··· 200 200 201 201 dm_free_md_mempools(t->mempools); 202 202 203 - dm_table_destroy_keyslot_manager(t); 203 + dm_table_destroy_crypto_profile(t); 204 204 205 205 kfree(t); 206 206 } ··· 1187 1187 1188 1188 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1189 1189 1190 - struct dm_keyslot_manager { 1191 - struct blk_keyslot_manager ksm; 1190 + struct dm_crypto_profile { 1191 + struct blk_crypto_profile profile; 1192 1192 struct mapped_device *md; 1193 1193 }; 1194 1194 ··· 1214 1214 * When an inline encryption key is evicted from a device-mapper device, evict 1215 1215 * it from all the underlying devices. 1216 1216 */ 1217 - static int dm_keyslot_evict(struct blk_keyslot_manager *ksm, 1217 + static int dm_keyslot_evict(struct blk_crypto_profile *profile, 1218 1218 const struct blk_crypto_key *key, unsigned int slot) 1219 1219 { 1220 - struct dm_keyslot_manager *dksm = container_of(ksm, 1221 - struct dm_keyslot_manager, 1222 - ksm); 1223 - struct mapped_device *md = dksm->md; 1220 + struct mapped_device *md = 1221 + container_of(profile, struct dm_crypto_profile, profile)->md; 1224 1222 struct dm_keyslot_evict_args args = { key }; 1225 1223 struct dm_table *t; 1226 1224 int srcu_idx; ··· 1238 1240 return args.err; 1239 1241 } 1240 1242 1241 - static const struct blk_ksm_ll_ops dm_ksm_ll_ops = { 1242 - .keyslot_evict = dm_keyslot_evict, 1243 - }; 1244 - 1245 - static int device_intersect_crypto_modes(struct dm_target *ti, 1246 - struct dm_dev *dev, sector_t start, 1247 - sector_t len, void *data) 1243 + static int 1244 + device_intersect_crypto_capabilities(struct dm_target *ti, struct dm_dev *dev, 1245 + sector_t start, sector_t len, void *data) 1248 1246 { 1249 - struct blk_keyslot_manager *parent = data; 1250 - struct blk_keyslot_manager *child = bdev_get_queue(dev->bdev)->ksm; 1247 + struct blk_crypto_profile *parent = data; 1248 + struct blk_crypto_profile *child = 1249 + bdev_get_queue(dev->bdev)->crypto_profile; 1251 1250 1252 - blk_ksm_intersect_modes(parent, child); 1251 + blk_crypto_intersect_capabilities(parent, child); 1253 1252 return 0; 1254 1253 } 1255 1254 1256 - void dm_destroy_keyslot_manager(struct blk_keyslot_manager *ksm) 1255 + void dm_destroy_crypto_profile(struct blk_crypto_profile *profile) 1257 1256 { 1258 - struct dm_keyslot_manager *dksm = container_of(ksm, 1259 - struct dm_keyslot_manager, 1260 - ksm); 1257 + struct dm_crypto_profile *dmcp = container_of(profile, 1258 + struct dm_crypto_profile, 1259 + profile); 1261 1260 1262 - if (!ksm) 1261 + if (!profile) 1263 1262 return; 1264 1263 1265 - blk_ksm_destroy(ksm); 1266 - kfree(dksm); 1264 + blk_crypto_profile_destroy(profile); 1265 + kfree(dmcp); 1267 1266 } 1268 1267 1269 - static void dm_table_destroy_keyslot_manager(struct dm_table *t) 1268 + static void dm_table_destroy_crypto_profile(struct dm_table *t) 1270 1269 { 1271 - dm_destroy_keyslot_manager(t->ksm); 1272 - t->ksm = NULL; 1270 + dm_destroy_crypto_profile(t->crypto_profile); 1271 + t->crypto_profile = NULL; 1273 1272 } 1274 1273 1275 1274 /* 1276 - * Constructs and initializes t->ksm with a keyslot manager that 1277 - * represents the common set of crypto capabilities of the devices 1278 - * described by the dm_table. However, if the constructed keyslot 1279 - * manager does not support a superset of the crypto capabilities 1280 - * supported by the current keyslot manager of the mapped_device, 1281 - * it returns an error instead, since we don't support restricting 1282 - * crypto capabilities on table changes. Finally, if the constructed 1283 - * keyslot manager doesn't actually support any crypto modes at all, 1284 - * it just returns NULL. 1275 + * Constructs and initializes t->crypto_profile with a crypto profile that 1276 + * represents the common set of crypto capabilities of the devices described by 1277 + * the dm_table. However, if the constructed crypto profile doesn't support all 1278 + * crypto capabilities that are supported by the current mapped_device, it 1279 + * returns an error instead, since we don't support removing crypto capabilities 1280 + * on table changes. Finally, if the constructed crypto profile is "empty" (has 1281 + * no crypto capabilities at all), it just sets t->crypto_profile to NULL. 1285 1282 */ 1286 - static int dm_table_construct_keyslot_manager(struct dm_table *t) 1283 + static int dm_table_construct_crypto_profile(struct dm_table *t) 1287 1284 { 1288 - struct dm_keyslot_manager *dksm; 1289 - struct blk_keyslot_manager *ksm; 1285 + struct dm_crypto_profile *dmcp; 1286 + struct blk_crypto_profile *profile; 1290 1287 struct dm_target *ti; 1291 1288 unsigned int i; 1292 - bool ksm_is_empty = true; 1289 + bool empty_profile = true; 1293 1290 1294 - dksm = kmalloc(sizeof(*dksm), GFP_KERNEL); 1295 - if (!dksm) 1291 + dmcp = kmalloc(sizeof(*dmcp), GFP_KERNEL); 1292 + if (!dmcp) 1296 1293 return -ENOMEM; 1297 - dksm->md = t->md; 1294 + dmcp->md = t->md; 1298 1295 1299 - ksm = &dksm->ksm; 1300 - blk_ksm_init_passthrough(ksm); 1301 - ksm->ksm_ll_ops = dm_ksm_ll_ops; 1302 - ksm->max_dun_bytes_supported = UINT_MAX; 1303 - memset(ksm->crypto_modes_supported, 0xFF, 1304 - sizeof(ksm->crypto_modes_supported)); 1296 + profile = &dmcp->profile; 1297 + blk_crypto_profile_init(profile, 0); 1298 + profile->ll_ops.keyslot_evict = dm_keyslot_evict; 1299 + profile->max_dun_bytes_supported = UINT_MAX; 1300 + memset(profile->modes_supported, 0xFF, 1301 + sizeof(profile->modes_supported)); 1305 1302 1306 1303 for (i = 0; i < dm_table_get_num_targets(t); i++) { 1307 1304 ti = dm_table_get_target(t, i); 1308 1305 1309 1306 if (!dm_target_passes_crypto(ti->type)) { 1310 - blk_ksm_intersect_modes(ksm, NULL); 1307 + blk_crypto_intersect_capabilities(profile, NULL); 1311 1308 break; 1312 1309 } 1313 1310 if (!ti->type->iterate_devices) 1314 1311 continue; 1315 - ti->type->iterate_devices(ti, device_intersect_crypto_modes, 1316 - ksm); 1312 + ti->type->iterate_devices(ti, 1313 + device_intersect_crypto_capabilities, 1314 + profile); 1317 1315 } 1318 1316 1319 - if (t->md->queue && !blk_ksm_is_superset(ksm, t->md->queue->ksm)) { 1317 + if (t->md->queue && 1318 + !blk_crypto_has_capabilities(profile, 1319 + t->md->queue->crypto_profile)) { 1320 1320 DMWARN("Inline encryption capabilities of new DM table were more restrictive than the old table's. This is not supported!"); 1321 - dm_destroy_keyslot_manager(ksm); 1321 + dm_destroy_crypto_profile(profile); 1322 1322 return -EINVAL; 1323 1323 } 1324 1324 1325 1325 /* 1326 - * If the new KSM doesn't actually support any crypto modes, we may as 1327 - * well represent it with a NULL ksm. 1326 + * If the new profile doesn't actually support any crypto capabilities, 1327 + * we may as well represent it with a NULL profile. 1328 1328 */ 1329 - ksm_is_empty = true; 1330 - for (i = 0; i < ARRAY_SIZE(ksm->crypto_modes_supported); i++) { 1331 - if (ksm->crypto_modes_supported[i]) { 1332 - ksm_is_empty = false; 1329 + for (i = 0; i < ARRAY_SIZE(profile->modes_supported); i++) { 1330 + if (profile->modes_supported[i]) { 1331 + empty_profile = false; 1333 1332 break; 1334 1333 } 1335 1334 } 1336 1335 1337 - if (ksm_is_empty) { 1338 - dm_destroy_keyslot_manager(ksm); 1339 - ksm = NULL; 1336 + if (empty_profile) { 1337 + dm_destroy_crypto_profile(profile); 1338 + profile = NULL; 1340 1339 } 1341 1340 1342 1341 /* 1343 - * t->ksm is only set temporarily while the table is being set 1344 - * up, and it gets set to NULL after the capabilities have 1345 - * been transferred to the request_queue. 1342 + * t->crypto_profile is only set temporarily while the table is being 1343 + * set up, and it gets set to NULL after the profile has been 1344 + * transferred to the request_queue. 1346 1345 */ 1347 - t->ksm = ksm; 1346 + t->crypto_profile = profile; 1348 1347 1349 1348 return 0; 1350 1349 } 1351 1350 1352 - static void dm_update_keyslot_manager(struct request_queue *q, 1353 - struct dm_table *t) 1351 + static void dm_update_crypto_profile(struct request_queue *q, 1352 + struct dm_table *t) 1354 1353 { 1355 - if (!t->ksm) 1354 + if (!t->crypto_profile) 1356 1355 return; 1357 1356 1358 - /* Make the ksm less restrictive */ 1359 - if (!q->ksm) { 1360 - blk_ksm_register(t->ksm, q); 1357 + /* Make the crypto profile less restrictive. */ 1358 + if (!q->crypto_profile) { 1359 + blk_crypto_register(t->crypto_profile, q); 1361 1360 } else { 1362 - blk_ksm_update_capabilities(q->ksm, t->ksm); 1363 - dm_destroy_keyslot_manager(t->ksm); 1361 + blk_crypto_update_capabilities(q->crypto_profile, 1362 + t->crypto_profile); 1363 + dm_destroy_crypto_profile(t->crypto_profile); 1364 1364 } 1365 - t->ksm = NULL; 1365 + t->crypto_profile = NULL; 1366 1366 } 1367 1367 1368 1368 #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1369 1369 1370 - static int dm_table_construct_keyslot_manager(struct dm_table *t) 1370 + static int dm_table_construct_crypto_profile(struct dm_table *t) 1371 1371 { 1372 1372 return 0; 1373 1373 } 1374 1374 1375 - void dm_destroy_keyslot_manager(struct blk_keyslot_manager *ksm) 1375 + void dm_destroy_crypto_profile(struct blk_crypto_profile *profile) 1376 1376 { 1377 1377 } 1378 1378 1379 - static void dm_table_destroy_keyslot_manager(struct dm_table *t) 1379 + static void dm_table_destroy_crypto_profile(struct dm_table *t) 1380 1380 { 1381 1381 } 1382 1382 1383 - static void dm_update_keyslot_manager(struct request_queue *q, 1384 - struct dm_table *t) 1383 + static void dm_update_crypto_profile(struct request_queue *q, 1384 + struct dm_table *t) 1385 1385 { 1386 1386 } 1387 1387 ··· 1411 1415 return r; 1412 1416 } 1413 1417 1414 - r = dm_table_construct_keyslot_manager(t); 1418 + r = dm_table_construct_crypto_profile(t); 1415 1419 if (r) { 1416 - DMERR("could not construct keyslot manager."); 1420 + DMERR("could not construct crypto profile."); 1417 1421 return r; 1418 1422 } 1419 1423 ··· 2067 2071 return r; 2068 2072 } 2069 2073 2070 - dm_update_keyslot_manager(q, t); 2074 + dm_update_crypto_profile(q, t); 2071 2075 disk_update_readahead(t->md->disk); 2072 2076 2073 2077 return 0;
+4 -4
drivers/md/dm.c
··· 1663 1663 static void dm_wq_work(struct work_struct *work); 1664 1664 1665 1665 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1666 - static void dm_queue_destroy_keyslot_manager(struct request_queue *q) 1666 + static void dm_queue_destroy_crypto_profile(struct request_queue *q) 1667 1667 { 1668 - dm_destroy_keyslot_manager(q->ksm); 1668 + dm_destroy_crypto_profile(q->crypto_profile); 1669 1669 } 1670 1670 1671 1671 #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1672 1672 1673 - static inline void dm_queue_destroy_keyslot_manager(struct request_queue *q) 1673 + static inline void dm_queue_destroy_crypto_profile(struct request_queue *q) 1674 1674 { 1675 1675 } 1676 1676 #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ ··· 1696 1696 dm_sysfs_exit(md); 1697 1697 del_gendisk(md->disk); 1698 1698 } 1699 - dm_queue_destroy_keyslot_manager(md->queue); 1699 + dm_queue_destroy_crypto_profile(md->queue); 1700 1700 blk_cleanup_disk(md->disk); 1701 1701 } 1702 1702
+7 -4
drivers/mmc/core/crypto.c
··· 16 16 { 17 17 /* Reset might clear all keys, so reprogram all the keys. */ 18 18 if (host->caps2 & MMC_CAP2_CRYPTO) 19 - blk_ksm_reprogram_all_keys(&host->ksm); 19 + blk_crypto_reprogram_all_keys(&host->crypto_profile); 20 20 } 21 21 22 22 void mmc_crypto_setup_queue(struct request_queue *q, struct mmc_host *host) 23 23 { 24 24 if (host->caps2 & MMC_CAP2_CRYPTO) 25 - blk_ksm_register(&host->ksm, q); 25 + blk_crypto_register(&host->crypto_profile, q); 26 26 } 27 27 EXPORT_SYMBOL_GPL(mmc_crypto_setup_queue); 28 28 ··· 30 30 { 31 31 struct request *req = mmc_queue_req_to_req(mqrq); 32 32 struct mmc_request *mrq = &mqrq->brq.mrq; 33 + struct blk_crypto_keyslot *keyslot; 33 34 34 35 if (!req->crypt_ctx) 35 36 return; 36 37 37 38 mrq->crypto_ctx = req->crypt_ctx; 38 - if (req->crypt_keyslot) 39 - mrq->crypto_key_slot = blk_ksm_get_slot_idx(req->crypt_keyslot); 39 + 40 + keyslot = req->crypt_keyslot; 41 + if (keyslot) 42 + mrq->crypto_key_slot = blk_crypto_keyslot_index(keyslot); 40 43 } 41 44 EXPORT_SYMBOL_GPL(mmc_crypto_prepare_req);
+16 -15
drivers/mmc/host/cqhci-crypto.c
··· 23 23 }; 24 24 25 25 static inline struct cqhci_host * 26 - cqhci_host_from_ksm(struct blk_keyslot_manager *ksm) 26 + cqhci_host_from_crypto_profile(struct blk_crypto_profile *profile) 27 27 { 28 - struct mmc_host *mmc = container_of(ksm, struct mmc_host, ksm); 28 + struct mmc_host *mmc = 29 + container_of(profile, struct mmc_host, crypto_profile); 29 30 30 31 return mmc->cqe_private; 31 32 } ··· 58 57 return 0; 59 58 } 60 59 61 - static int cqhci_crypto_keyslot_program(struct blk_keyslot_manager *ksm, 60 + static int cqhci_crypto_keyslot_program(struct blk_crypto_profile *profile, 62 61 const struct blk_crypto_key *key, 63 62 unsigned int slot) 64 63 65 64 { 66 - struct cqhci_host *cq_host = cqhci_host_from_ksm(ksm); 65 + struct cqhci_host *cq_host = cqhci_host_from_crypto_profile(profile); 67 66 const union cqhci_crypto_cap_entry *ccap_array = 68 67 cq_host->crypto_cap_array; 69 68 const struct cqhci_crypto_alg_entry *alg = ··· 116 115 return cqhci_crypto_program_key(cq_host, &cfg, slot); 117 116 } 118 117 119 - static int cqhci_crypto_keyslot_evict(struct blk_keyslot_manager *ksm, 118 + static int cqhci_crypto_keyslot_evict(struct blk_crypto_profile *profile, 120 119 const struct blk_crypto_key *key, 121 120 unsigned int slot) 122 121 { 123 - struct cqhci_host *cq_host = cqhci_host_from_ksm(ksm); 122 + struct cqhci_host *cq_host = cqhci_host_from_crypto_profile(profile); 124 123 125 124 return cqhci_crypto_clear_keyslot(cq_host, slot); 126 125 } ··· 133 132 * "enabled" when these are called, i.e. CQHCI_ENABLE might not be set in the 134 133 * CQHCI_CFG register. But the hardware allows that. 135 134 */ 136 - static const struct blk_ksm_ll_ops cqhci_ksm_ops = { 135 + static const struct blk_crypto_ll_ops cqhci_crypto_ops = { 137 136 .keyslot_program = cqhci_crypto_keyslot_program, 138 137 .keyslot_evict = cqhci_crypto_keyslot_evict, 139 138 }; ··· 158 157 * 159 158 * If the driver previously set MMC_CAP2_CRYPTO and the CQE declares 160 159 * CQHCI_CAP_CS, initialize the crypto support. This involves reading the 161 - * crypto capability registers, initializing the keyslot manager, clearing all 162 - * keyslots, and enabling 128-bit task descriptors. 160 + * crypto capability registers, initializing the blk_crypto_profile, clearing 161 + * all keyslots, and enabling 128-bit task descriptors. 163 162 * 164 163 * Return: 0 if crypto was initialized or isn't supported; whether 165 164 * MMC_CAP2_CRYPTO remains set indicates which one of those cases it is. ··· 169 168 { 170 169 struct mmc_host *mmc = cq_host->mmc; 171 170 struct device *dev = mmc_dev(mmc); 172 - struct blk_keyslot_manager *ksm = &mmc->ksm; 171 + struct blk_crypto_profile *profile = &mmc->crypto_profile; 173 172 unsigned int num_keyslots; 174 173 unsigned int cap_idx; 175 174 enum blk_crypto_mode_num blk_mode_num; ··· 200 199 */ 201 200 num_keyslots = cq_host->crypto_capabilities.config_count + 1; 202 201 203 - err = devm_blk_ksm_init(dev, ksm, num_keyslots); 202 + err = devm_blk_crypto_profile_init(dev, profile, num_keyslots); 204 203 if (err) 205 204 goto out; 206 205 207 - ksm->ksm_ll_ops = cqhci_ksm_ops; 208 - ksm->dev = dev; 206 + profile->ll_ops = cqhci_crypto_ops; 207 + profile->dev = dev; 209 208 210 209 /* Unfortunately, CQHCI crypto only supports 32 DUN bits. */ 211 - ksm->max_dun_bytes_supported = 4; 210 + profile->max_dun_bytes_supported = 4; 212 211 213 212 /* 214 213 * Cache all the crypto capabilities and advertise the supported crypto ··· 224 223 cq_host->crypto_cap_array[cap_idx]); 225 224 if (blk_mode_num == BLK_ENCRYPTION_MODE_INVALID) 226 225 continue; 227 - ksm->crypto_modes_supported[blk_mode_num] |= 226 + profile->modes_supported[blk_mode_num] |= 228 227 cq_host->crypto_cap_array[cap_idx].sdus_mask * 512; 229 228 } 230 229
+17 -15
drivers/scsi/ufs/ufshcd-crypto.c
··· 48 48 return err; 49 49 } 50 50 51 - static int ufshcd_crypto_keyslot_program(struct blk_keyslot_manager *ksm, 51 + static int ufshcd_crypto_keyslot_program(struct blk_crypto_profile *profile, 52 52 const struct blk_crypto_key *key, 53 53 unsigned int slot) 54 54 { 55 - struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); 55 + struct ufs_hba *hba = 56 + container_of(profile, struct ufs_hba, crypto_profile); 56 57 const union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array; 57 58 const struct ufs_crypto_alg_entry *alg = 58 59 &ufs_crypto_algs[key->crypto_cfg.crypto_mode]; ··· 106 105 return ufshcd_program_key(hba, &cfg, slot); 107 106 } 108 107 109 - static int ufshcd_crypto_keyslot_evict(struct blk_keyslot_manager *ksm, 108 + static int ufshcd_crypto_keyslot_evict(struct blk_crypto_profile *profile, 110 109 const struct blk_crypto_key *key, 111 110 unsigned int slot) 112 111 { 113 - struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); 112 + struct ufs_hba *hba = 113 + container_of(profile, struct ufs_hba, crypto_profile); 114 114 115 115 return ufshcd_clear_keyslot(hba, slot); 116 116 } ··· 122 120 return false; 123 121 124 122 /* Reset might clear all keys, so reprogram all the keys. */ 125 - blk_ksm_reprogram_all_keys(&hba->ksm); 123 + blk_crypto_reprogram_all_keys(&hba->crypto_profile); 126 124 return true; 127 125 } 128 126 129 - static const struct blk_ksm_ll_ops ufshcd_ksm_ops = { 127 + static const struct blk_crypto_ll_ops ufshcd_crypto_ops = { 130 128 .keyslot_program = ufshcd_crypto_keyslot_program, 131 129 .keyslot_evict = ufshcd_crypto_keyslot_evict, 132 130 }; ··· 181 179 } 182 180 183 181 /* The actual number of configurations supported is (CFGC+1) */ 184 - err = devm_blk_ksm_init(hba->dev, &hba->ksm, 185 - hba->crypto_capabilities.config_count + 1); 182 + err = devm_blk_crypto_profile_init( 183 + hba->dev, &hba->crypto_profile, 184 + hba->crypto_capabilities.config_count + 1); 186 185 if (err) 187 186 goto out; 188 187 189 - hba->ksm.ksm_ll_ops = ufshcd_ksm_ops; 188 + hba->crypto_profile.ll_ops = ufshcd_crypto_ops; 190 189 /* UFS only supports 8 bytes for any DUN */ 191 - hba->ksm.max_dun_bytes_supported = 8; 192 - hba->ksm.dev = hba->dev; 190 + hba->crypto_profile.max_dun_bytes_supported = 8; 191 + hba->crypto_profile.dev = hba->dev; 193 192 194 193 /* 195 194 * Cache all the UFS crypto capabilities and advertise the supported ··· 205 202 blk_mode_num = ufshcd_find_blk_crypto_mode( 206 203 hba->crypto_cap_array[cap_idx]); 207 204 if (blk_mode_num != BLK_ENCRYPTION_MODE_INVALID) 208 - hba->ksm.crypto_modes_supported[blk_mode_num] |= 205 + hba->crypto_profile.modes_supported[blk_mode_num] |= 209 206 hba->crypto_cap_array[cap_idx].sdus_mask * 512; 210 207 } 211 208 ··· 233 230 ufshcd_clear_keyslot(hba, slot); 234 231 } 235 232 236 - void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba, 237 - struct request_queue *q) 233 + void ufshcd_crypto_register(struct ufs_hba *hba, struct request_queue *q) 238 234 { 239 235 if (hba->caps & UFSHCD_CAP_CRYPTO) 240 - blk_ksm_register(&hba->ksm, q); 236 + blk_crypto_register(&hba->crypto_profile, q); 241 237 }
+4 -5
drivers/scsi/ufs/ufshcd-crypto.h
··· 18 18 return; 19 19 } 20 20 21 - lrbp->crypto_key_slot = blk_ksm_get_slot_idx(rq->crypt_keyslot); 21 + lrbp->crypto_key_slot = blk_crypto_keyslot_index(rq->crypt_keyslot); 22 22 lrbp->data_unit_num = rq->crypt_ctx->bc_dun[0]; 23 23 } 24 24 ··· 40 40 41 41 void ufshcd_init_crypto(struct ufs_hba *hba); 42 42 43 - void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba, 44 - struct request_queue *q); 43 + void ufshcd_crypto_register(struct ufs_hba *hba, struct request_queue *q); 45 44 46 45 #else /* CONFIG_SCSI_UFS_CRYPTO */ 47 46 ··· 63 64 64 65 static inline void ufshcd_init_crypto(struct ufs_hba *hba) { } 65 66 66 - static inline void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba, 67 - struct request_queue *q) { } 67 + static inline void ufshcd_crypto_register(struct ufs_hba *hba, 68 + struct request_queue *q) { } 68 69 69 70 #endif /* CONFIG_SCSI_UFS_CRYPTO */ 70 71
+1 -1
drivers/scsi/ufs/ufshcd.c
··· 4986 4986 else if (ufshcd_is_rpm_autosuspend_allowed(hba)) 4987 4987 sdev->rpm_autosuspend = 1; 4988 4988 4989 - ufshcd_crypto_setup_rq_keyslot_manager(hba, q); 4989 + ufshcd_crypto_register(hba, q); 4990 4990 4991 4991 return 0; 4992 4992 }
+2 -2
drivers/scsi/ufs/ufshcd.h
··· 766 766 * @crypto_capabilities: Content of crypto capabilities register (0x100) 767 767 * @crypto_cap_array: Array of crypto capabilities 768 768 * @crypto_cfg_register: Start of the crypto cfg array 769 - * @ksm: the keyslot manager tied to this hba 769 + * @crypto_profile: the crypto profile of this hba (if applicable) 770 770 */ 771 771 struct ufs_hba { 772 772 void __iomem *mmio_base; ··· 911 911 union ufs_crypto_capabilities crypto_capabilities; 912 912 union ufs_crypto_cap_entry *crypto_cap_array; 913 913 u32 crypto_cfg_register; 914 - struct blk_keyslot_manager ksm; 914 + struct blk_crypto_profile crypto_profile; 915 915 #endif 916 916 #ifdef CONFIG_DEBUG_FS 917 917 struct dentry *debugfs_root;
+106 -60
include/linux/blk-crypto-profile.h
··· 3 3 * Copyright 2019 Google LLC 4 4 */ 5 5 6 - #ifndef __LINUX_KEYSLOT_MANAGER_H 7 - #define __LINUX_KEYSLOT_MANAGER_H 6 + #ifndef __LINUX_BLK_CRYPTO_PROFILE_H 7 + #define __LINUX_BLK_CRYPTO_PROFILE_H 8 8 9 9 #include <linux/bio.h> 10 10 #include <linux/blk-crypto.h> 11 11 12 - struct blk_keyslot_manager; 12 + struct blk_crypto_profile; 13 13 14 14 /** 15 - * struct blk_ksm_ll_ops - functions to manage keyslots in hardware 16 - * @keyslot_program: Program the specified key into the specified slot in the 17 - * inline encryption hardware. 18 - * @keyslot_evict: Evict key from the specified keyslot in the hardware. 19 - * The key is provided so that e.g. dm layers can evict 20 - * keys from the devices that they map over. 21 - * Returns 0 on success, -errno otherwise. 15 + * struct blk_crypto_ll_ops - functions to control inline encryption hardware 22 16 * 23 - * This structure should be provided by storage device drivers when they set up 24 - * a keyslot manager - this structure holds the function ptrs that the keyslot 25 - * manager will use to manipulate keyslots in the hardware. 17 + * Low-level operations for controlling inline encryption hardware. This 18 + * interface must be implemented by storage drivers that support inline 19 + * encryption. All functions may sleep, are serialized by profile->lock, and 20 + * are never called while profile->dev (if set) is runtime-suspended. 26 21 */ 27 - struct blk_ksm_ll_ops { 28 - int (*keyslot_program)(struct blk_keyslot_manager *ksm, 22 + struct blk_crypto_ll_ops { 23 + 24 + /** 25 + * @keyslot_program: Program a key into the inline encryption hardware. 26 + * 27 + * Program @key into the specified @slot in the inline encryption 28 + * hardware, overwriting any key that the keyslot may already contain. 29 + * The keyslot is guaranteed to not be in-use by any I/O. 30 + * 31 + * This is required if the device has keyslots. Otherwise (i.e. if the 32 + * device is a layered device, or if the device is real hardware that 33 + * simply doesn't have the concept of keyslots) it is never called. 34 + * 35 + * Must return 0 on success, or -errno on failure. 36 + */ 37 + int (*keyslot_program)(struct blk_crypto_profile *profile, 29 38 const struct blk_crypto_key *key, 30 39 unsigned int slot); 31 - int (*keyslot_evict)(struct blk_keyslot_manager *ksm, 40 + 41 + /** 42 + * @keyslot_evict: Evict a key from the inline encryption hardware. 43 + * 44 + * If the device has keyslots, this function must evict the key from the 45 + * specified @slot. The slot will contain @key, but there should be no 46 + * need for the @key argument to be used as @slot should be sufficient. 47 + * The keyslot is guaranteed to not be in-use by any I/O. 48 + * 49 + * If the device doesn't have keyslots itself, this function must evict 50 + * @key from any underlying devices. @slot won't be valid in this case. 51 + * 52 + * If there are no keyslots and no underlying devices, this function 53 + * isn't required. 54 + * 55 + * Must return 0 on success, or -errno on failure. 56 + */ 57 + int (*keyslot_evict)(struct blk_crypto_profile *profile, 32 58 const struct blk_crypto_key *key, 33 59 unsigned int slot); 34 60 }; 35 61 36 - struct blk_keyslot_manager { 37 - /* 38 - * The struct blk_ksm_ll_ops that this keyslot manager will use 39 - * to perform operations like programming and evicting keys on the 40 - * device 41 - */ 42 - struct blk_ksm_ll_ops ksm_ll_ops; 62 + /** 63 + * struct blk_crypto_profile - inline encryption profile for a device 64 + * 65 + * This struct contains a storage device's inline encryption capabilities (e.g. 66 + * the supported crypto algorithms), driver-provided functions to control the 67 + * inline encryption hardware (e.g. programming and evicting keys), and optional 68 + * device-independent keyslot management data. 69 + */ 70 + struct blk_crypto_profile { 43 71 44 - /* 45 - * The maximum number of bytes supported for specifying the data unit 46 - * number. 72 + /* public: Drivers must initialize the following fields. */ 73 + 74 + /** 75 + * @ll_ops: Driver-provided functions to control the inline encryption 76 + * hardware, e.g. program and evict keys. 77 + */ 78 + struct blk_crypto_ll_ops ll_ops; 79 + 80 + /** 81 + * @max_dun_bytes_supported: The maximum number of bytes supported for 82 + * specifying the data unit number (DUN). Specifically, the range of 83 + * supported DUNs is 0 through (1 << (8 * max_dun_bytes_supported)) - 1. 47 84 */ 48 85 unsigned int max_dun_bytes_supported; 49 86 50 - /* 51 - * Array of size BLK_ENCRYPTION_MODE_MAX of bitmasks that represents 52 - * whether a crypto mode and data unit size are supported. The i'th 53 - * bit of crypto_mode_supported[crypto_mode] is set iff a data unit 54 - * size of (1 << i) is supported. We only support data unit sizes 55 - * that are powers of 2. 87 + /** 88 + * @modes_supported: Array of bitmasks that specifies whether each 89 + * combination of crypto mode and data unit size is supported. 90 + * Specifically, the i'th bit of modes_supported[crypto_mode] is set if 91 + * crypto_mode can be used with a data unit size of (1 << i). Note that 92 + * only data unit sizes that are powers of 2 can be supported. 56 93 */ 57 - unsigned int crypto_modes_supported[BLK_ENCRYPTION_MODE_MAX]; 94 + unsigned int modes_supported[BLK_ENCRYPTION_MODE_MAX]; 58 95 59 - /* Device for runtime power management (NULL if none) */ 96 + /** 97 + * @dev: An optional device for runtime power management. If the driver 98 + * provides this device, it will be runtime-resumed before any function 99 + * in @ll_ops is called and will remain resumed during the call. 100 + */ 60 101 struct device *dev; 61 102 62 - /* Here onwards are *private* fields for internal keyslot manager use */ 103 + /* private: The following fields shouldn't be accessed by drivers. */ 63 104 105 + /* Number of keyslots, or 0 if not applicable */ 64 106 unsigned int num_slots; 65 107 66 - /* Protects programming and evicting keys from the device */ 108 + /* 109 + * Serializes all calls to functions in @ll_ops as well as all changes 110 + * to @slot_hashtable. This can also be taken in read mode to look up 111 + * keyslots while ensuring that they can't be changed concurrently. 112 + */ 67 113 struct rw_semaphore lock; 68 114 69 115 /* List of idle slots, with least recently used slot at front */ ··· 126 80 unsigned int log_slot_ht_size; 127 81 128 82 /* Per-keyslot data */ 129 - struct blk_ksm_keyslot *slots; 83 + struct blk_crypto_keyslot *slots; 130 84 }; 131 85 132 - int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots); 86 + int blk_crypto_profile_init(struct blk_crypto_profile *profile, 87 + unsigned int num_slots); 133 88 134 - int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm, 135 - unsigned int num_slots); 89 + int devm_blk_crypto_profile_init(struct device *dev, 90 + struct blk_crypto_profile *profile, 91 + unsigned int num_slots); 136 92 137 - blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm, 138 - const struct blk_crypto_key *key, 139 - struct blk_ksm_keyslot **slot_ptr); 93 + unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot); 140 94 141 - unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot); 95 + blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile, 96 + const struct blk_crypto_key *key, 97 + struct blk_crypto_keyslot **slot_ptr); 142 98 143 - void blk_ksm_put_slot(struct blk_ksm_keyslot *slot); 99 + void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot); 144 100 145 - bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm, 146 - const struct blk_crypto_config *cfg); 101 + bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile, 102 + const struct blk_crypto_config *cfg); 147 103 148 - int blk_ksm_evict_key(struct blk_keyslot_manager *ksm, 149 - const struct blk_crypto_key *key); 104 + int __blk_crypto_evict_key(struct blk_crypto_profile *profile, 105 + const struct blk_crypto_key *key); 150 106 151 - void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm); 107 + void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile); 152 108 153 - void blk_ksm_destroy(struct blk_keyslot_manager *ksm); 109 + void blk_crypto_profile_destroy(struct blk_crypto_profile *profile); 154 110 155 - void blk_ksm_intersect_modes(struct blk_keyslot_manager *parent, 156 - const struct blk_keyslot_manager *child); 111 + void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent, 112 + const struct blk_crypto_profile *child); 157 113 158 - void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm); 114 + bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target, 115 + const struct blk_crypto_profile *reference); 159 116 160 - bool blk_ksm_is_superset(struct blk_keyslot_manager *ksm_superset, 161 - struct blk_keyslot_manager *ksm_subset); 117 + void blk_crypto_update_capabilities(struct blk_crypto_profile *dst, 118 + const struct blk_crypto_profile *src); 162 119 163 - void blk_ksm_update_capabilities(struct blk_keyslot_manager *target_ksm, 164 - struct blk_keyslot_manager *reference_ksm); 165 - 166 - #endif /* __LINUX_KEYSLOT_MANAGER_H */ 120 + #endif /* __LINUX_BLK_CRYPTO_PROFILE_H */
+1 -1
include/linux/blk-mq.h
··· 133 133 134 134 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 135 135 struct bio_crypt_ctx *crypt_ctx; 136 - struct blk_ksm_keyslot *crypt_keyslot; 136 + struct blk_crypto_keyslot *crypt_keyslot; 137 137 #endif 138 138 139 139 unsigned short write_hint;
+8 -8
include/linux/blkdev.h
··· 30 30 struct rq_qos; 31 31 struct blk_queue_stats; 32 32 struct blk_stat_callback; 33 - struct blk_keyslot_manager; 33 + struct blk_crypto_profile; 34 34 35 35 /* Must be consistent with blk_mq_poll_stats_bkt() */ 36 36 #define BLK_MQ_POLL_STATS_BKTS 16 ··· 224 224 unsigned int dma_alignment; 225 225 226 226 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 227 - /* Inline crypto capabilities */ 228 - struct blk_keyslot_manager *ksm; 227 + struct blk_crypto_profile *crypto_profile; 229 228 #endif 230 229 231 230 unsigned int rq_timeout; ··· 1141 1142 1142 1143 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1143 1144 1144 - bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q); 1145 + bool blk_crypto_register(struct blk_crypto_profile *profile, 1146 + struct request_queue *q); 1145 1147 1146 - void blk_ksm_unregister(struct request_queue *q); 1148 + void blk_crypto_unregister(struct request_queue *q); 1147 1149 1148 1150 #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1149 1151 1150 - static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm, 1151 - struct request_queue *q) 1152 + static inline bool blk_crypto_register(struct blk_crypto_profile *profile, 1153 + struct request_queue *q) 1152 1154 { 1153 1155 return true; 1154 1156 } 1155 1157 1156 - static inline void blk_ksm_unregister(struct request_queue *q) { } 1158 + static inline void blk_crypto_unregister(struct request_queue *q) { } 1157 1159 1158 1160 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */ 1159 1161
+2 -2
include/linux/device-mapper.h
··· 576 576 struct dm_table *t); 577 577 578 578 /* 579 - * Table keyslot manager functions 579 + * Table blk_crypto_profile functions 580 580 */ 581 - void dm_destroy_keyslot_manager(struct blk_keyslot_manager *ksm); 581 + void dm_destroy_crypto_profile(struct blk_crypto_profile *profile); 582 582 583 583 /*----------------------------------------------------------------- 584 584 * Macros.
+1 -1
include/linux/mmc/host.h
··· 492 492 493 493 /* Inline encryption support */ 494 494 #ifdef CONFIG_MMC_CRYPTO 495 - struct blk_keyslot_manager ksm; 495 + struct blk_crypto_profile crypto_profile; 496 496 #endif 497 497 498 498 /* Host Software Queue support */