1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Google LLC
7 * DOC: The Keyslot Manager
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.
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.
18 * The keyslot manager manages these keyslots appropriately, and also acts as
19 * an abstraction between the inline encryption hardware and the upper layers.
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.
25 * Upper layers will call blk_ksm_get_slot_for_key() to program a
26 * key into some slot in the inline encryption hardware.
29 #define pr_fmt(fmt) "blk-crypto: " fmt
31 #include <linux/keyslot-manager.h>
32 #include <linux/atomic.h>
33 #include <linux/mutex.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/wait.h>
36 #include <linux/blkdev.h>
38 struct blk_ksm_keyslot {
40 struct list_head idle_slot_node;
41 struct hlist_node hash_node;
42 const struct blk_crypto_key *key;
43 struct blk_keyslot_manager *ksm;
46 static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm)
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
51 * and release ksm->lock via blk_ksm_reprogram_all_keys().
54 pm_runtime_get_sync(ksm->dev);
55 down_write(&ksm->lock);
58 static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm)
62 pm_runtime_put_sync(ksm->dev);
65 static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm)
67 return ksm->num_slots == 0;
71 * blk_ksm_init() - Initialize a keyslot manager
72 * @ksm: The keyslot_manager to initialize.
73 * @num_slots: The number of key slots to manage.
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.
78 * Return: 0 on success, or else a negative error code.
80 int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
84 unsigned int slot_hashtable_size;
86 memset(ksm, 0, sizeof(*ksm));
91 ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL);
95 ksm->num_slots = num_slots;
97 init_rwsem(&ksm->lock);
99 init_waitqueue_head(&ksm->idle_slots_wait_queue);
100 INIT_LIST_HEAD(&ksm->idle_slots);
102 for (slot = 0; slot < num_slots; slot++) {
103 ksm->slots[slot].ksm = ksm;
104 list_add_tail(&ksm->slots[slot].idle_slot_node,
108 spin_lock_init(&ksm->idle_slots_lock);
110 slot_hashtable_size = roundup_pow_of_two(num_slots);
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.
115 if (slot_hashtable_size < 2)
116 slot_hashtable_size = 2;
118 ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
119 ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
120 sizeof(ksm->slot_hashtable[0]),
122 if (!ksm->slot_hashtable)
123 goto err_destroy_ksm;
124 for (i = 0; i < slot_hashtable_size; i++)
125 INIT_HLIST_HEAD(&ksm->slot_hashtable[i]);
130 blk_ksm_destroy(ksm);
133 EXPORT_SYMBOL_GPL(blk_ksm_init);
135 static inline struct hlist_head *
136 blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm,
137 const struct blk_crypto_key *key)
139 return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)];
142 static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot)
144 struct blk_keyslot_manager *ksm = slot->ksm;
147 spin_lock_irqsave(&ksm->idle_slots_lock, flags);
148 list_del(&slot->idle_slot_node);
149 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
152 static struct blk_ksm_keyslot *blk_ksm_find_keyslot(
153 struct blk_keyslot_manager *ksm,
154 const struct blk_crypto_key *key)
156 const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key);
157 struct blk_ksm_keyslot *slotp;
159 hlist_for_each_entry(slotp, head, hash_node) {
160 if (slotp->key == key)
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)
170 struct blk_ksm_keyslot *slot;
172 slot = blk_ksm_find_keyslot(ksm, key);
175 if (atomic_inc_return(&slot->slot_refs) == 1) {
176 /* Took first reference to this slot; remove it from LRU list */
177 blk_ksm_remove_slot_from_lru_list(slot);
182 unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot)
184 return slot - slot->ksm->slots;
186 EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx);
189 * blk_ksm_get_slot_for_key() - Program a key into a keyslot.
190 * @ksm: The keyslot manager to program the key into.
191 * @key: Pointer to the key object to program, including the raw key, crypto
192 * mode, and data unit size.
193 * @slot_ptr: A pointer to return the pointer of the allocated keyslot.
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.
199 * Context: Process context. Takes and releases ksm->lock.
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).
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)
208 struct blk_ksm_keyslot *slot;
214 if (blk_ksm_is_passthrough(ksm))
217 down_read(&ksm->lock);
218 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
224 blk_ksm_hw_enter(ksm);
225 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
227 blk_ksm_hw_exit(ksm);
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.
235 if (!list_empty(&ksm->idle_slots))
238 blk_ksm_hw_exit(ksm);
239 wait_event(ksm->idle_slots_wait_queue,
240 !list_empty(&ksm->idle_slots));
243 slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot,
245 slot_idx = blk_ksm_get_slot_idx(slot);
247 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx);
249 wake_up(&ksm->idle_slots_wait_queue);
250 blk_ksm_hw_exit(ksm);
251 return errno_to_blk_status(err);
254 /* Move this slot to the hash list for the new key. */
256 hlist_del(&slot->hash_node);
258 hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key));
260 atomic_set(&slot->slot_refs, 1);
262 blk_ksm_remove_slot_from_lru_list(slot);
264 blk_ksm_hw_exit(ksm);
271 * blk_ksm_put_slot() - Release a reference to a slot
272 * @slot: The keyslot to release the reference of.
274 * Context: Any context.
276 void blk_ksm_put_slot(struct blk_ksm_keyslot *slot)
278 struct blk_keyslot_manager *ksm;
286 if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
287 &ksm->idle_slots_lock, flags)) {
288 list_add_tail(&slot->idle_slot_node, &ksm->idle_slots);
289 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
290 wake_up(&ksm->idle_slots_wait_queue);
295 * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is
296 * supported by a ksm.
297 * @ksm: The keyslot manager to check
298 * @cfg: The crypto configuration to check for.
300 * Checks for crypto_mode/data unit size/dun bytes support.
302 * Return: Whether or not this ksm supports the specified crypto config.
304 bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm,
305 const struct blk_crypto_config *cfg)
309 if (!(ksm->crypto_modes_supported[cfg->crypto_mode] &
310 cfg->data_unit_size))
312 if (ksm->max_dun_bytes_supported < cfg->dun_bytes)
318 * blk_ksm_evict_key() - Evict a key from the lower layer device.
319 * @ksm: The keyslot manager to evict from
320 * @key: The key to evict
322 * Find the keyslot that the specified key was programmed into, and evict that
323 * slot from the lower layer device. The slot must not be in use by any
324 * in-flight IO when this function is called.
326 * Context: Process context. Takes and releases ksm->lock.
327 * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY
328 * if the keyslot is still in use, or another -errno value on other
331 int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
332 const struct blk_crypto_key *key)
334 struct blk_ksm_keyslot *slot;
337 if (blk_ksm_is_passthrough(ksm)) {
338 if (ksm->ksm_ll_ops.keyslot_evict) {
339 blk_ksm_hw_enter(ksm);
340 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1);
341 blk_ksm_hw_exit(ksm);
347 blk_ksm_hw_enter(ksm);
348 slot = blk_ksm_find_keyslot(ksm, key);
352 if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
356 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key,
357 blk_ksm_get_slot_idx(slot));
361 hlist_del(&slot->hash_node);
365 blk_ksm_hw_exit(ksm);
370 * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
371 * @ksm: The keyslot manager
373 * Re-program all keyslots that are supposed to have a key programmed. This is
374 * intended only for use by drivers for hardware that loses its keys on reset.
376 * Context: Process context. Takes and releases ksm->lock.
378 void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm)
382 if (blk_ksm_is_passthrough(ksm))
385 /* This is for device initialization, so don't resume the device */
386 down_write(&ksm->lock);
387 for (slot = 0; slot < ksm->num_slots; slot++) {
388 const struct blk_crypto_key *key = ksm->slots[slot].key;
394 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
397 up_write(&ksm->lock);
399 EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys);
401 void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
405 kvfree(ksm->slot_hashtable);
406 kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots);
407 memzero_explicit(ksm, sizeof(*ksm));
409 EXPORT_SYMBOL_GPL(blk_ksm_destroy);
411 bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
413 if (blk_integrity_queue_supports_integrity(q)) {
414 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
420 EXPORT_SYMBOL_GPL(blk_ksm_register);
422 void blk_ksm_unregister(struct request_queue *q)
428 * blk_ksm_init_passthrough() - Init a passthrough keyslot manager
429 * @ksm: The keyslot manager to init
431 * Initialize a passthrough keyslot manager.
432 * Called by e.g. storage drivers to set up a keyslot manager in their
433 * request_queue, when the storage driver wants to manage its keys by itself.
434 * This is useful for inline encryption hardware that doesn't have the concept
435 * of keyslots, and for layered devices.
437 void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm)
439 memset(ksm, 0, sizeof(*ksm));
440 init_rwsem(&ksm->lock);
442 EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough);