blob: 2b18b17a15fb82e29bf400e00df92b3a8acacb19 [file] [log] [blame]
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -07001// SPDX-License-Identifier: GPL-2.0
2/*
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -07003 * 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 Tangiralac2b86b72020-06-16 14:33:37 -070013 * As the number of slots is limited, and programming keys is expensive on
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070014 * 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 Tangiralac2b86b72020-06-16 14:33:37 -070025 * Upper layers will call blk_ksm_get_slot_for_key() to program a
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070026 * key into some slot in the inline encryption hardware.
27 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -070028
29#define pr_fmt(fmt) "blk-crypto: " fmt
30
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070031#include <linux/keyslot-manager.h>
32#include <linux/atomic.h>
33#include <linux/mutex.h>
Eric Biggersa59152c2020-02-13 15:08:24 -080034#include <linux/pm_runtime.h>
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070035#include <linux/wait.h>
36#include <linux/blkdev.h>
37
Satya Tangiralac2b86b72020-06-16 14:33:37 -070038struct blk_ksm_keyslot {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070039 atomic_t slot_refs;
40 struct list_head idle_slot_node;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -080041 struct hlist_node hash_node;
Satya Tangiralac2b86b72020-06-16 14:33:37 -070042 const struct blk_crypto_key *key;
43 struct blk_keyslot_manager *ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070044};
45
Satya Tangiralac2b86b72020-06-16 14:33:37 -070046static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm)
Eric Biggersa59152c2020-02-13 15:08:24 -080047{
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 Tangiralac2b86b72020-06-16 14:33:37 -070051 * and release ksm->lock via blk_ksm_reprogram_all_keys().
Eric Biggersa59152c2020-02-13 15:08:24 -080052 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -070053 if (ksm->dev)
54 pm_runtime_get_sync(ksm->dev);
Eric Biggersa59152c2020-02-13 15:08:24 -080055 down_write(&ksm->lock);
56}
57
Satya Tangiralac2b86b72020-06-16 14:33:37 -070058static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm)
Eric Biggersa59152c2020-02-13 15:08:24 -080059{
60 up_write(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -070061 if (ksm->dev)
62 pm_runtime_put_sync(ksm->dev);
63}
64
65static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm)
66{
67 return ksm->num_slots == 0;
Eric Biggersa59152c2020-02-13 15:08:24 -080068}
69
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070070/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -070071 * blk_ksm_init() - Initialize a keyslot manager
72 * @ksm: The keyslot_manager to initialize.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070073 * @num_slots: The number of key slots to manage.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070074 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -070075 * 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 Tangiralaaac6c3d2019-10-24 14:44:23 -070077 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -070078 * Return: 0 on success, or else a negative error code.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070079 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -070080int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070081{
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -080082 unsigned int slot;
83 unsigned int i;
Satya Tangiralac2b86b72020-06-16 14:33:37 -070084 unsigned int slot_hashtable_size;
85
86 memset(ksm, 0, sizeof(*ksm));
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070087
88 if (num_slots == 0)
Satya Tangiralac2b86b72020-06-16 14:33:37 -070089 return -EINVAL;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070090
Satya Tangiralac2b86b72020-06-16 14:33:37 -070091 ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL);
92 if (!ksm->slots)
93 return -ENOMEM;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070094
95 ksm->num_slots = num_slots;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070096
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 Tangiralac2b86b72020-06-16 14:33:37 -0700103 ksm->slots[slot].ksm = ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700104 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 Tangiralac2b86b72020-06-16 14:33:37 -0700110 slot_hashtable_size = roundup_pow_of_two(num_slots);
Eric Biggers47a846532020-11-11 13:48:55 -0800111 /*
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 Tangiralac2b86b72020-06-16 14:33:37 -0700118 ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
119 ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800120 sizeof(ksm->slot_hashtable[0]),
121 GFP_KERNEL);
122 if (!ksm->slot_hashtable)
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700123 goto err_destroy_ksm;
124 for (i = 0; i < slot_hashtable_size; i++)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800125 INIT_HLIST_HEAD(&ksm->slot_hashtable[i]);
126
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700127 return 0;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800128
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700129err_destroy_ksm:
130 blk_ksm_destroy(ksm);
131 return -ENOMEM;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700132}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700133EXPORT_SYMBOL_GPL(blk_ksm_init);
Eric Biggers77058132020-05-06 14:15:06 -0700134
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800135static inline struct hlist_head *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700136blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm,
137 const struct blk_crypto_key *key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800138{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700139 return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)];
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800140}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700141
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700142static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700143{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700144 struct blk_keyslot_manager *ksm = slot->ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700145 unsigned long flags;
146
147 spin_lock_irqsave(&ksm->idle_slots_lock, flags);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700148 list_del(&slot->idle_slot_node);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700149 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700150}
151
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700152static struct blk_ksm_keyslot *blk_ksm_find_keyslot(
153 struct blk_keyslot_manager *ksm,
154 const struct blk_crypto_key *key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800155{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700156 const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key);
157 struct blk_ksm_keyslot *slotp;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800158
159 hlist_for_each_entry(slotp, head, hash_node) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700160 if (slotp->key == key)
161 return slotp;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800162 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700163 return NULL;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800164}
165
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700166static struct blk_ksm_keyslot *blk_ksm_find_and_grab_keyslot(
167 struct blk_keyslot_manager *ksm,
168 const struct blk_crypto_key *key)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700169{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700170 struct blk_ksm_keyslot *slot;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700171
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700172 slot = blk_ksm_find_keyslot(ksm, key);
173 if (!slot)
174 return NULL;
175 if (atomic_inc_return(&slot->slot_refs) == 1) {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700176 /* Took first reference to this slot; remove it from LRU list */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700177 blk_ksm_remove_slot_from_lru_list(slot);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700178 }
179 return slot;
180}
181
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700182unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot)
183{
184 return slot - slot->ksm->slots;
185}
186EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx);
187
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700188/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700189 * blk_ksm_get_slot_for_key() - Program a key into a keyslot.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700190 * @ksm: The keyslot manager to program the key into.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800191 * @key: Pointer to the key object to program, including the raw key, crypto
192 * mode, and data unit size.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700193 * @slot_ptr: A pointer to return the pointer of the allocated keyslot.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700194 *
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800195 * 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700198 *
199 * Context: Process context. Takes and releases ksm->lock.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700200 * 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700203 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700204blk_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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700207{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700208 struct blk_ksm_keyslot *slot;
209 int slot_idx;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700210 int err;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700211
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700212 *slot_ptr = NULL;
213
214 if (blk_ksm_is_passthrough(ksm))
215 return BLK_STS_OK;
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800216
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700217 down_read(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700218 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700219 up_read(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700220 if (slot)
221 goto success;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700222
223 for (;;) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700224 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700229 }
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 Tangiralacfd7e6c2019-12-17 14:26:29 -0800235 if (!list_empty(&ksm->idle_slots))
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700236 break;
237
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700238 blk_ksm_hw_exit(ksm);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700239 wait_event(ksm->idle_slots_wait_queue,
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800240 !list_empty(&ksm->idle_slots));
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700241 }
242
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700243 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700246
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700247 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700248 if (err) {
249 wake_up(&ksm->idle_slots_wait_queue);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700250 blk_ksm_hw_exit(ksm);
251 return errno_to_blk_status(err);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700252 }
253
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800254 /* Move this slot to the hash list for the new key. */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700255 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 Tangiralacfd7e6c2019-12-17 14:26:29 -0800259
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700260 atomic_set(&slot->slot_refs, 1);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800261
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700262 blk_ksm_remove_slot_from_lru_list(slot);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700263
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700264 blk_ksm_hw_exit(ksm);
265success:
266 *slot_ptr = slot;
267 return BLK_STS_OK;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700268}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700269
270/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700271 * blk_ksm_put_slot() - Release a reference to a slot
272 * @slot: The keyslot to release the reference of.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700273 *
274 * Context: Any context.
275 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700276void blk_ksm_put_slot(struct blk_ksm_keyslot *slot)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700277{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700278 struct blk_keyslot_manager *ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700279 unsigned long flags;
280
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700281 if (!slot)
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800282 return;
283
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700284 ksm = slot->ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700285
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700286 if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700287 &ksm->idle_slots_lock, flags)) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700288 list_add_tail(&slot->idle_slot_node, &ksm->idle_slots);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700289 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700290 wake_up(&ksm->idle_slots_wait_queue);
291 }
292}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700293
294/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700295 * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is
296 * supported by a ksm.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800297 * @ksm: The keyslot manager to check
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700298 * @cfg: The crypto configuration to check for.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700299 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700300 * Checks for crypto_mode/data unit size/dun bytes support.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700301 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700302 * Return: Whether or not this ksm supports the specified crypto config.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700303 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700304bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm,
305 const struct blk_crypto_config *cfg)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700306{
307 if (!ksm)
308 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700309 if (!(ksm->crypto_modes_supported[cfg->crypto_mode] &
310 cfg->data_unit_size))
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800311 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700312 if (ksm->max_dun_bytes_supported < cfg->dun_bytes)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800313 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700314 if (cfg->is_hw_wrapped) {
Eric Biggers935b0c42020-04-03 12:06:11 -0700315 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 Tangiralac2b86b72020-06-16 14:33:37 -0700321 return true;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700322}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700323
324/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700325 * blk_ksm_evict_key() - Evict a key from the lower layer device.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800326 * @ksm: The keyslot manager to evict from
327 * @key: The key to evict
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700328 *
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800329 * Find the keyslot that the specified key was programmed into, and evict that
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700330 * 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700332 *
333 * Context: Process context. Takes and releases ksm->lock.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700334 * 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 Tangiralaaac6c3d2019-10-24 14:44:23 -0700337 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700338int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
339 const struct blk_crypto_key *key)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700340{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700341 struct blk_ksm_keyslot *slot;
342 int err = 0;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700343
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700344 if (blk_ksm_is_passthrough(ksm)) {
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800345 if (ksm->ksm_ll_ops.keyslot_evict) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700346 blk_ksm_hw_enter(ksm);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800347 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700348 blk_ksm_hw_exit(ksm);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800349 return err;
350 }
351 return 0;
352 }
353
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700354 blk_ksm_hw_enter(ksm);
355 slot = blk_ksm_find_keyslot(ksm, key);
356 if (!slot)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800357 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700358
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700359 if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700360 err = -EBUSY;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800361 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700362 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700363 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key,
364 blk_ksm_get_slot_idx(slot));
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800365 if (err)
366 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700367
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700368 hlist_del(&slot->hash_node);
369 slot->key = NULL;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800370 err = 0;
371out_unlock:
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700372 blk_ksm_hw_exit(ksm);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700373 return err;
374}
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800375
376/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700377 * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800378 * @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 Tangiralac2b86b72020-06-16 14:33:37 -0700385void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800386{
387 unsigned int slot;
388
Eric Biggers537d3bb2021-02-12 11:53:49 -0800389 if (blk_ksm_is_passthrough(ksm))
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800390 return;
391
Eric Biggersa59152c2020-02-13 15:08:24 -0800392 /* This is for device initialization, so don't resume the device */
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800393 down_write(&ksm->lock);
394 for (slot = 0; slot < ksm->num_slots; slot++) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700395 const struct blk_crypto_key *key = ksm->slots[slot].key;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800396 int err;
397
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700398 if (!key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800399 continue;
400
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700401 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800402 WARN_ON(err);
403 }
404 up_write(&ksm->lock);
405}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700406EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800407
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700408void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800409{
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800410 if (!ksm)
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700411 return;
412 kvfree(ksm->slot_hashtable);
Eric Biggers3e20aa92020-06-16 08:56:54 -0700413 kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700414 memzero_explicit(ksm, sizeof(*ksm));
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800415}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700416EXPORT_SYMBOL_GPL(blk_ksm_destroy);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800417
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700418bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
Eric Biggersbea2b962020-01-21 09:27:43 -0800419{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700420 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 Biggersbea2b962020-01-21 09:27:43 -0800423 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700424 q->ksm = ksm;
425 return true;
Eric Biggersbea2b962020-01-21 09:27:43 -0800426}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700427EXPORT_SYMBOL_GPL(blk_ksm_register);
428
429void blk_ksm_unregister(struct request_queue *q)
430{
431 q->ksm = NULL;
432}
Eric Biggersbea2b962020-01-21 09:27:43 -0800433
434/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700435 * blk_ksm_derive_raw_secret() - Derive software secret from wrapped key
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800436 * @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 Tangiralac2b86b72020-06-16 14:33:37 -0700451int 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 Muthukumaran1daa0582020-01-02 12:01:34 -0800455{
456 int err;
457
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800458 if (ksm->ksm_ll_ops.derive_raw_secret) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700459 blk_ksm_hw_enter(ksm);
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800460 err = ksm->ksm_ll_ops.derive_raw_secret(ksm, wrapped_key,
461 wrapped_key_size,
462 secret, secret_size);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700463 blk_ksm_hw_exit(ksm);
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800464 } else {
465 err = -EOPNOTSUPP;
466 }
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800467
468 return err;
469}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700470EXPORT_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 */
483void 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 Biggers6b78d002020-08-11 09:35:23 -0700492 for (i = 0; i < ARRAY_SIZE(child->crypto_modes_supported);
493 i++) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700494 parent->crypto_modes_supported[i] &=
495 child->crypto_modes_supported[i];
496 }
Eric Biggers537d3bb2021-02-12 11:53:49 -0800497 parent->features &= child->features;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700498 } else {
499 parent->max_dun_bytes_supported = 0;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700500 memset(parent->crypto_modes_supported, 0,
501 sizeof(parent->crypto_modes_supported));
Eric Biggers537d3bb2021-02-12 11:53:49 -0800502 parent->features = 0;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700503 }
504}
505EXPORT_SYMBOL_GPL(blk_ksm_intersect_modes);
506
507/**
Eric Biggers537d3bb2021-02-12 11:53:49 -0800508 * 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 */
519bool 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}
547EXPORT_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 */
572void 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}
584EXPORT_SYMBOL_GPL(blk_ksm_update_capabilities);
585
586/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700587 * 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 Biggers537d3bb2021-02-12 11:53:49 -0800593 * This is useful for inline encryption hardware that doesn't have the concept
594 * of keyslots, and for layered devices.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700595 */
596void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm)
597{
598 memset(ksm, 0, sizeof(*ksm));
599 init_rwsem(&ksm->lock);
600}
601EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough);