Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * DOC: The Keyslot Manager |
| 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. |
| 12 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 13 | * As the number of slots is limited, and programming keys is expensive on |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 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. |
| 17 | * |
| 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 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 25 | * Upper layers will call blk_ksm_get_slot_for_key() to program a |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 26 | * key into some slot in the inline encryption hardware. |
| 27 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 28 | |
| 29 | #define pr_fmt(fmt) "blk-crypto: " fmt |
| 30 | |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 31 | #include <linux/keyslot-manager.h> |
| 32 | #include <linux/atomic.h> |
| 33 | #include <linux/mutex.h> |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 34 | #include <linux/pm_runtime.h> |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 35 | #include <linux/wait.h> |
| 36 | #include <linux/blkdev.h> |
| 37 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 38 | struct blk_ksm_keyslot { |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 39 | atomic_t slot_refs; |
| 40 | struct list_head idle_slot_node; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 41 | struct hlist_node hash_node; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 42 | const struct blk_crypto_key *key; |
| 43 | struct blk_keyslot_manager *ksm; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 46 | static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm) |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 47 | { |
| 48 | /* |
| 49 | * Calling into the driver requires ksm->lock held and the device |
| 50 | * resumed. But we must resume the device first, since that can acquire |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 51 | * and release ksm->lock via blk_ksm_reprogram_all_keys(). |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 52 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 53 | if (ksm->dev) |
| 54 | pm_runtime_get_sync(ksm->dev); |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 55 | down_write(&ksm->lock); |
| 56 | } |
| 57 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 58 | static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm) |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 59 | { |
| 60 | up_write(&ksm->lock); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 61 | if (ksm->dev) |
| 62 | pm_runtime_put_sync(ksm->dev); |
| 63 | } |
| 64 | |
| 65 | static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm) |
| 66 | { |
| 67 | return ksm->num_slots == 0; |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 70 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 71 | * blk_ksm_init() - Initialize a keyslot manager |
| 72 | * @ksm: The keyslot_manager to initialize. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 73 | * @num_slots: The number of key slots to manage. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 74 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 75 | * Allocate memory for keyslots and initialize a keyslot manager. Called by |
| 76 | * e.g. storage drivers to set up a keyslot manager in their request_queue. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 77 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 78 | * Return: 0 on success, or else a negative error code. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 79 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 80 | int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 81 | { |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 82 | unsigned int slot; |
| 83 | unsigned int i; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 84 | unsigned int slot_hashtable_size; |
| 85 | |
| 86 | memset(ksm, 0, sizeof(*ksm)); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 87 | |
| 88 | if (num_slots == 0) |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 89 | return -EINVAL; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 90 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 91 | ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL); |
| 92 | if (!ksm->slots) |
| 93 | return -ENOMEM; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 94 | |
| 95 | ksm->num_slots = num_slots; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 96 | |
| 97 | init_rwsem(&ksm->lock); |
| 98 | |
| 99 | init_waitqueue_head(&ksm->idle_slots_wait_queue); |
| 100 | INIT_LIST_HEAD(&ksm->idle_slots); |
| 101 | |
| 102 | for (slot = 0; slot < num_slots; slot++) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 103 | ksm->slots[slot].ksm = ksm; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 104 | list_add_tail(&ksm->slots[slot].idle_slot_node, |
| 105 | &ksm->idle_slots); |
| 106 | } |
| 107 | |
| 108 | spin_lock_init(&ksm->idle_slots_lock); |
| 109 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 110 | slot_hashtable_size = roundup_pow_of_two(num_slots); |
Eric Biggers | 47a84653 | 2020-11-11 13:48:55 -0800 | [diff] [blame] | 111 | /* |
| 112 | * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2 |
| 113 | * buckets. This only makes a difference when there is only 1 keyslot. |
| 114 | */ |
| 115 | if (slot_hashtable_size < 2) |
| 116 | slot_hashtable_size = 2; |
| 117 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 118 | ksm->log_slot_ht_size = ilog2(slot_hashtable_size); |
| 119 | ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size, |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 120 | sizeof(ksm->slot_hashtable[0]), |
| 121 | GFP_KERNEL); |
| 122 | if (!ksm->slot_hashtable) |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 123 | goto err_destroy_ksm; |
| 124 | for (i = 0; i < slot_hashtable_size; i++) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 125 | INIT_HLIST_HEAD(&ksm->slot_hashtable[i]); |
| 126 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 127 | return 0; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 128 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 129 | err_destroy_ksm: |
| 130 | blk_ksm_destroy(ksm); |
| 131 | return -ENOMEM; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 132 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(blk_ksm_init); |
Eric Biggers | 7705813 | 2020-05-06 14:15:06 -0700 | [diff] [blame] | 134 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 135 | static inline struct hlist_head * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 136 | blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm, |
| 137 | const struct blk_crypto_key *key) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 138 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 139 | return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)]; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 140 | } |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 141 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 142 | static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 143 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 144 | struct blk_keyslot_manager *ksm = slot->ksm; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 145 | unsigned long flags; |
| 146 | |
| 147 | spin_lock_irqsave(&ksm->idle_slots_lock, flags); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 148 | list_del(&slot->idle_slot_node); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 149 | spin_unlock_irqrestore(&ksm->idle_slots_lock, flags); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 152 | static struct blk_ksm_keyslot *blk_ksm_find_keyslot( |
| 153 | struct blk_keyslot_manager *ksm, |
| 154 | const struct blk_crypto_key *key) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 155 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 156 | const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key); |
| 157 | struct blk_ksm_keyslot *slotp; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 158 | |
| 159 | hlist_for_each_entry(slotp, head, hash_node) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 160 | if (slotp->key == key) |
| 161 | return slotp; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 162 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 163 | return NULL; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 166 | static struct blk_ksm_keyslot *blk_ksm_find_and_grab_keyslot( |
| 167 | struct blk_keyslot_manager *ksm, |
| 168 | const struct blk_crypto_key *key) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 169 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 170 | struct blk_ksm_keyslot *slot; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 171 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 172 | slot = blk_ksm_find_keyslot(ksm, key); |
| 173 | if (!slot) |
| 174 | return NULL; |
| 175 | if (atomic_inc_return(&slot->slot_refs) == 1) { |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 176 | /* Took first reference to this slot; remove it from LRU list */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 177 | blk_ksm_remove_slot_from_lru_list(slot); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 178 | } |
| 179 | return slot; |
| 180 | } |
| 181 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 182 | unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot) |
| 183 | { |
| 184 | return slot - slot->ksm->slots; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx); |
| 187 | |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 188 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 189 | * blk_ksm_get_slot_for_key() - Program a key into a keyslot. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 190 | * @ksm: The keyslot manager to program the key into. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 191 | * @key: Pointer to the key object to program, including the raw key, crypto |
| 192 | * mode, and data unit size. |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 193 | * @slot_ptr: A pointer to return the pointer of the allocated keyslot. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 194 | * |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 195 | * Get a keyslot that's been programmed with the specified key. If one already |
| 196 | * exists, return it with incremented refcount. Otherwise, wait for a keyslot |
| 197 | * to become idle and program it. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 198 | * |
| 199 | * Context: Process context. Takes and releases ksm->lock. |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 200 | * Return: BLK_STS_OK on success (and keyslot is set to the pointer of the |
| 201 | * allocated keyslot), or some other blk_status_t otherwise (and |
| 202 | * keyslot is set to NULL). |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 203 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 204 | blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm, |
| 205 | const struct blk_crypto_key *key, |
| 206 | struct blk_ksm_keyslot **slot_ptr) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 207 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 208 | struct blk_ksm_keyslot *slot; |
| 209 | int slot_idx; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 210 | int err; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 211 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 212 | *slot_ptr = NULL; |
| 213 | |
| 214 | if (blk_ksm_is_passthrough(ksm)) |
| 215 | return BLK_STS_OK; |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 216 | |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 217 | down_read(&ksm->lock); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 218 | slot = blk_ksm_find_and_grab_keyslot(ksm, key); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 219 | up_read(&ksm->lock); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 220 | if (slot) |
| 221 | goto success; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 222 | |
| 223 | for (;;) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 224 | blk_ksm_hw_enter(ksm); |
| 225 | slot = blk_ksm_find_and_grab_keyslot(ksm, key); |
| 226 | if (slot) { |
| 227 | blk_ksm_hw_exit(ksm); |
| 228 | goto success; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * If we're here, that means there wasn't a slot that was |
| 233 | * already programmed with the key. So try to program it. |
| 234 | */ |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 235 | if (!list_empty(&ksm->idle_slots)) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 236 | break; |
| 237 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 238 | blk_ksm_hw_exit(ksm); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 239 | wait_event(ksm->idle_slots_wait_queue, |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 240 | !list_empty(&ksm->idle_slots)); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 243 | slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot, |
| 244 | idle_slot_node); |
| 245 | slot_idx = blk_ksm_get_slot_idx(slot); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 246 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 247 | err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 248 | if (err) { |
| 249 | wake_up(&ksm->idle_slots_wait_queue); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 250 | blk_ksm_hw_exit(ksm); |
| 251 | return errno_to_blk_status(err); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 254 | /* Move this slot to the hash list for the new key. */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 255 | if (slot->key) |
| 256 | hlist_del(&slot->hash_node); |
| 257 | slot->key = key; |
| 258 | hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key)); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 259 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 260 | atomic_set(&slot->slot_refs, 1); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 261 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 262 | blk_ksm_remove_slot_from_lru_list(slot); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 263 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 264 | blk_ksm_hw_exit(ksm); |
| 265 | success: |
| 266 | *slot_ptr = slot; |
| 267 | return BLK_STS_OK; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 268 | } |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 271 | * blk_ksm_put_slot() - Release a reference to a slot |
| 272 | * @slot: The keyslot to release the reference of. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 273 | * |
| 274 | * Context: Any context. |
| 275 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 276 | void blk_ksm_put_slot(struct blk_ksm_keyslot *slot) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 277 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 278 | struct blk_keyslot_manager *ksm; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 279 | unsigned long flags; |
| 280 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 281 | if (!slot) |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 282 | return; |
| 283 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 284 | ksm = slot->ksm; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 285 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 286 | if (atomic_dec_and_lock_irqsave(&slot->slot_refs, |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 287 | &ksm->idle_slots_lock, flags)) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 288 | list_add_tail(&slot->idle_slot_node, &ksm->idle_slots); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 289 | spin_unlock_irqrestore(&ksm->idle_slots_lock, flags); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 290 | wake_up(&ksm->idle_slots_wait_queue); |
| 291 | } |
| 292 | } |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 295 | * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is |
| 296 | * supported by a ksm. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 297 | * @ksm: The keyslot manager to check |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 298 | * @cfg: The crypto configuration to check for. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 299 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 300 | * Checks for crypto_mode/data unit size/dun bytes support. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 301 | * |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 302 | * Return: Whether or not this ksm supports the specified crypto config. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 303 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 304 | bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm, |
| 305 | const struct blk_crypto_config *cfg) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 306 | { |
| 307 | if (!ksm) |
| 308 | return false; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 309 | if (!(ksm->crypto_modes_supported[cfg->crypto_mode] & |
| 310 | cfg->data_unit_size)) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 311 | return false; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 312 | if (ksm->max_dun_bytes_supported < cfg->dun_bytes) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 313 | return false; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 314 | if (cfg->is_hw_wrapped) { |
Eric Biggers | 935b0c4 | 2020-04-03 12:06:11 -0700 | [diff] [blame] | 315 | if (!(ksm->features & BLK_CRYPTO_FEATURE_WRAPPED_KEYS)) |
| 316 | return false; |
| 317 | } else { |
| 318 | if (!(ksm->features & BLK_CRYPTO_FEATURE_STANDARD_KEYS)) |
| 319 | return false; |
| 320 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 321 | return true; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 322 | } |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 323 | |
| 324 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 325 | * blk_ksm_evict_key() - Evict a key from the lower layer device. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 326 | * @ksm: The keyslot manager to evict from |
| 327 | * @key: The key to evict |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 328 | * |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 329 | * Find the keyslot that the specified key was programmed into, and evict that |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 330 | * slot from the lower layer device. The slot must not be in use by any |
| 331 | * in-flight IO when this function is called. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 332 | * |
| 333 | * Context: Process context. Takes and releases ksm->lock. |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 334 | * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY |
| 335 | * if the keyslot is still in use, or another -errno value on other |
| 336 | * error. |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 337 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 338 | int blk_ksm_evict_key(struct blk_keyslot_manager *ksm, |
| 339 | const struct blk_crypto_key *key) |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 340 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 341 | struct blk_ksm_keyslot *slot; |
| 342 | int err = 0; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 343 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 344 | if (blk_ksm_is_passthrough(ksm)) { |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 345 | if (ksm->ksm_ll_ops.keyslot_evict) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 346 | blk_ksm_hw_enter(ksm); |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 347 | err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 348 | blk_ksm_hw_exit(ksm); |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 349 | return err; |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 354 | blk_ksm_hw_enter(ksm); |
| 355 | slot = blk_ksm_find_keyslot(ksm, key); |
| 356 | if (!slot) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 357 | goto out_unlock; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 358 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 359 | if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) { |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 360 | err = -EBUSY; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 361 | goto out_unlock; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 362 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 363 | err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, |
| 364 | blk_ksm_get_slot_idx(slot)); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 365 | if (err) |
| 366 | goto out_unlock; |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 367 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 368 | hlist_del(&slot->hash_node); |
| 369 | slot->key = NULL; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 370 | err = 0; |
| 371 | out_unlock: |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 372 | blk_ksm_hw_exit(ksm); |
Satya Tangirala | aac6c3d | 2019-10-24 14:44:23 -0700 | [diff] [blame] | 373 | return err; |
| 374 | } |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 375 | |
| 376 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 377 | * blk_ksm_reprogram_all_keys() - Re-program all keyslots. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 378 | * @ksm: The keyslot manager |
| 379 | * |
| 380 | * Re-program all keyslots that are supposed to have a key programmed. This is |
| 381 | * intended only for use by drivers for hardware that loses its keys on reset. |
| 382 | * |
| 383 | * Context: Process context. Takes and releases ksm->lock. |
| 384 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 385 | void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 386 | { |
| 387 | unsigned int slot; |
| 388 | |
Eric Biggers | 537d3bb | 2021-02-12 11:53:49 -0800 | [diff] [blame^] | 389 | if (blk_ksm_is_passthrough(ksm)) |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 390 | return; |
| 391 | |
Eric Biggers | a59152c | 2020-02-13 15:08:24 -0800 | [diff] [blame] | 392 | /* This is for device initialization, so don't resume the device */ |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 393 | down_write(&ksm->lock); |
| 394 | for (slot = 0; slot < ksm->num_slots; slot++) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 395 | const struct blk_crypto_key *key = ksm->slots[slot].key; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 396 | int err; |
| 397 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 398 | if (!key) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 399 | continue; |
| 400 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 401 | err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 402 | WARN_ON(err); |
| 403 | } |
| 404 | up_write(&ksm->lock); |
| 405 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 406 | EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 407 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 408 | void blk_ksm_destroy(struct blk_keyslot_manager *ksm) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 409 | { |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 410 | if (!ksm) |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 411 | return; |
| 412 | kvfree(ksm->slot_hashtable); |
Eric Biggers | 3e20aa9 | 2020-06-16 08:56:54 -0700 | [diff] [blame] | 413 | kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 414 | memzero_explicit(ksm, sizeof(*ksm)); |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 415 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(blk_ksm_destroy); |
Satya Tangirala | c7da3f4 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 417 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 418 | bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q) |
Eric Biggers | bea2b96 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 419 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 420 | if (blk_integrity_queue_supports_integrity(q)) { |
| 421 | pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n"); |
| 422 | return false; |
Eric Biggers | bea2b96 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 423 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 424 | q->ksm = ksm; |
| 425 | return true; |
Eric Biggers | bea2b96 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 426 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(blk_ksm_register); |
| 428 | |
| 429 | void blk_ksm_unregister(struct request_queue *q) |
| 430 | { |
| 431 | q->ksm = NULL; |
| 432 | } |
Eric Biggers | bea2b96 | 2020-01-21 09:27:43 -0800 | [diff] [blame] | 433 | |
| 434 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 435 | * blk_ksm_derive_raw_secret() - Derive software secret from wrapped key |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 436 | * @ksm: The keyslot manager |
| 437 | * @wrapped_key: The wrapped key |
| 438 | * @wrapped_key_size: Size of the wrapped key in bytes |
| 439 | * @secret: (output) the software secret |
| 440 | * @secret_size: (output) the number of secret bytes to derive |
| 441 | * |
| 442 | * Given a hardware-wrapped key, ask the hardware to derive a secret which |
| 443 | * software can use for cryptographic tasks other than inline encryption. The |
| 444 | * derived secret is guaranteed to be cryptographically isolated from the key |
| 445 | * with which any inline encryption with this wrapped key would actually be |
| 446 | * done. I.e., both will be derived from the unwrapped key. |
| 447 | * |
| 448 | * Return: 0 on success, -EOPNOTSUPP if hardware-wrapped keys are unsupported, |
| 449 | * or another -errno code. |
| 450 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 451 | int blk_ksm_derive_raw_secret(struct blk_keyslot_manager *ksm, |
| 452 | const u8 *wrapped_key, |
| 453 | unsigned int wrapped_key_size, |
| 454 | u8 *secret, unsigned int secret_size) |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 455 | { |
| 456 | int err; |
| 457 | |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 458 | if (ksm->ksm_ll_ops.derive_raw_secret) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 459 | blk_ksm_hw_enter(ksm); |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 460 | err = ksm->ksm_ll_ops.derive_raw_secret(ksm, wrapped_key, |
| 461 | wrapped_key_size, |
| 462 | secret, secret_size); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 463 | blk_ksm_hw_exit(ksm); |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 464 | } else { |
| 465 | err = -EOPNOTSUPP; |
| 466 | } |
Barani Muthukumaran | 1daa058 | 2020-01-02 12:01:34 -0800 | [diff] [blame] | 467 | |
| 468 | return err; |
| 469 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 470 | EXPORT_SYMBOL_GPL(blk_ksm_derive_raw_secret); |
| 471 | |
| 472 | /** |
| 473 | * blk_ksm_intersect_modes() - restrict supported modes by child device |
| 474 | * @parent: The keyslot manager for parent device |
| 475 | * @child: The keyslot manager for child device, or NULL |
| 476 | * |
| 477 | * Clear any crypto mode support bits in @parent that aren't set in @child. |
| 478 | * If @child is NULL, then all parent bits are cleared. |
| 479 | * |
| 480 | * Only use this when setting up the keyslot manager for a layered device, |
| 481 | * before it's been exposed yet. |
| 482 | */ |
| 483 | void blk_ksm_intersect_modes(struct blk_keyslot_manager *parent, |
| 484 | const struct blk_keyslot_manager *child) |
| 485 | { |
| 486 | if (child) { |
| 487 | unsigned int i; |
| 488 | |
| 489 | parent->max_dun_bytes_supported = |
| 490 | min(parent->max_dun_bytes_supported, |
| 491 | child->max_dun_bytes_supported); |
Eric Biggers | 6b78d00 | 2020-08-11 09:35:23 -0700 | [diff] [blame] | 492 | for (i = 0; i < ARRAY_SIZE(child->crypto_modes_supported); |
| 493 | i++) { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 494 | parent->crypto_modes_supported[i] &= |
| 495 | child->crypto_modes_supported[i]; |
| 496 | } |
Eric Biggers | 537d3bb | 2021-02-12 11:53:49 -0800 | [diff] [blame^] | 497 | parent->features &= child->features; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 498 | } else { |
| 499 | parent->max_dun_bytes_supported = 0; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 500 | memset(parent->crypto_modes_supported, 0, |
| 501 | sizeof(parent->crypto_modes_supported)); |
Eric Biggers | 537d3bb | 2021-02-12 11:53:49 -0800 | [diff] [blame^] | 502 | parent->features = 0; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(blk_ksm_intersect_modes); |
| 506 | |
| 507 | /** |
Eric Biggers | 537d3bb | 2021-02-12 11:53:49 -0800 | [diff] [blame^] | 508 | * blk_ksm_is_superset() - Check if a KSM supports a superset of crypto modes |
| 509 | * and DUN bytes that another KSM supports. Here, |
| 510 | * "superset" refers to the mathematical meaning of the |
| 511 | * word - i.e. if two KSMs have the *same* capabilities, |
| 512 | * they *are* considered supersets of each other. |
| 513 | * @ksm_superset: The KSM that we want to verify is a superset |
| 514 | * @ksm_subset: The KSM that we want to verify is a subset |
| 515 | * |
| 516 | * Return: True if @ksm_superset supports a superset of the crypto modes and DUN |
| 517 | * bytes that @ksm_subset supports. |
| 518 | */ |
| 519 | bool blk_ksm_is_superset(struct blk_keyslot_manager *ksm_superset, |
| 520 | struct blk_keyslot_manager *ksm_subset) |
| 521 | { |
| 522 | int i; |
| 523 | |
| 524 | if (!ksm_subset) |
| 525 | return true; |
| 526 | |
| 527 | if (!ksm_superset) |
| 528 | return false; |
| 529 | |
| 530 | for (i = 0; i < ARRAY_SIZE(ksm_superset->crypto_modes_supported); i++) { |
| 531 | if (ksm_subset->crypto_modes_supported[i] & |
| 532 | (~ksm_superset->crypto_modes_supported[i])) { |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (ksm_subset->max_dun_bytes_supported > |
| 538 | ksm_superset->max_dun_bytes_supported) { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | if (ksm_subset->features & ~ksm_superset->features) |
| 543 | return false; |
| 544 | |
| 545 | return true; |
| 546 | } |
| 547 | EXPORT_SYMBOL_GPL(blk_ksm_is_superset); |
| 548 | |
| 549 | /** |
| 550 | * blk_ksm_update_capabilities() - Update the restrictions of a KSM to those of |
| 551 | * another KSM |
| 552 | * @target_ksm: The KSM whose restrictions to update. |
| 553 | * @reference_ksm: The KSM to whose restrictions this function will update |
| 554 | * @target_ksm's restrictions to. |
| 555 | * |
| 556 | * Blk-crypto requires that crypto capabilities that were |
| 557 | * advertised when a bio was created continue to be supported by the |
| 558 | * device until that bio is ended. This is turn means that a device cannot |
| 559 | * shrink its advertised crypto capabilities without any explicit |
| 560 | * synchronization with upper layers. So if there's no such explicit |
| 561 | * synchronization, @reference_ksm must support all the crypto capabilities that |
| 562 | * @target_ksm does |
| 563 | * (i.e. we need blk_ksm_is_superset(@reference_ksm, @target_ksm) == true). |
| 564 | * |
| 565 | * Note also that as long as the crypto capabilities are being expanded, the |
| 566 | * order of updates becoming visible is not important because it's alright |
| 567 | * for blk-crypto to see stale values - they only cause blk-crypto to |
| 568 | * believe that a crypto capability isn't supported when it actually is (which |
| 569 | * might result in blk-crypto-fallback being used if available, or the bio being |
| 570 | * failed). |
| 571 | */ |
| 572 | void blk_ksm_update_capabilities(struct blk_keyslot_manager *target_ksm, |
| 573 | struct blk_keyslot_manager *reference_ksm) |
| 574 | { |
| 575 | memcpy(target_ksm->crypto_modes_supported, |
| 576 | reference_ksm->crypto_modes_supported, |
| 577 | sizeof(target_ksm->crypto_modes_supported)); |
| 578 | |
| 579 | target_ksm->max_dun_bytes_supported = |
| 580 | reference_ksm->max_dun_bytes_supported; |
| 581 | |
| 582 | target_ksm->features = reference_ksm->features; |
| 583 | } |
| 584 | EXPORT_SYMBOL_GPL(blk_ksm_update_capabilities); |
| 585 | |
| 586 | /** |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 587 | * blk_ksm_init_passthrough() - Init a passthrough keyslot manager |
| 588 | * @ksm: The keyslot manager to init |
| 589 | * |
| 590 | * Initialize a passthrough keyslot manager. |
| 591 | * Called by e.g. storage drivers to set up a keyslot manager in their |
| 592 | * request_queue, when the storage driver wants to manage its keys by itself. |
Eric Biggers | 537d3bb | 2021-02-12 11:53:49 -0800 | [diff] [blame^] | 593 | * This is useful for inline encryption hardware that doesn't have the concept |
| 594 | * of keyslots, and for layered devices. |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 595 | */ |
| 596 | void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm) |
| 597 | { |
| 598 | memset(ksm, 0, sizeof(*ksm)); |
| 599 | init_rwsem(&ksm->lock); |
| 600 | } |
| 601 | EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough); |