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
);
66 * blk_ksm_init() - Initialize a keyslot manager
67 * @ksm: The keyslot_manager to initialize.
68 * @num_slots: The number of key slots to manage.
70 * Allocate memory for keyslots and initialize a keyslot manager. Called by
71 * e.g. storage drivers to set up a keyslot manager in their request_queue.
73 * Return: 0 on success, or else a negative error code.
75 int blk_ksm_init(struct blk_keyslot_manager
*ksm
, unsigned int num_slots
)
79 unsigned int slot_hashtable_size
;
81 memset(ksm
, 0, sizeof(*ksm
));
86 ksm
->slots
= kvcalloc(num_slots
, sizeof(ksm
->slots
[0]), GFP_KERNEL
);
90 ksm
->num_slots
= num_slots
;
92 init_rwsem(&ksm
->lock
);
94 init_waitqueue_head(&ksm
->idle_slots_wait_queue
);
95 INIT_LIST_HEAD(&ksm
->idle_slots
);
97 for (slot
= 0; slot
< num_slots
; slot
++) {
98 ksm
->slots
[slot
].ksm
= ksm
;
99 list_add_tail(&ksm
->slots
[slot
].idle_slot_node
,
103 spin_lock_init(&ksm
->idle_slots_lock
);
105 slot_hashtable_size
= roundup_pow_of_two(num_slots
);
107 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
108 * buckets. This only makes a difference when there is only 1 keyslot.
110 if (slot_hashtable_size
< 2)
111 slot_hashtable_size
= 2;
113 ksm
->log_slot_ht_size
= ilog2(slot_hashtable_size
);
114 ksm
->slot_hashtable
= kvmalloc_array(slot_hashtable_size
,
115 sizeof(ksm
->slot_hashtable
[0]),
117 if (!ksm
->slot_hashtable
)
118 goto err_destroy_ksm
;
119 for (i
= 0; i
< slot_hashtable_size
; i
++)
120 INIT_HLIST_HEAD(&ksm
->slot_hashtable
[i
]);
125 blk_ksm_destroy(ksm
);
128 EXPORT_SYMBOL_GPL(blk_ksm_init
);
130 static inline struct hlist_head
*
131 blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager
*ksm
,
132 const struct blk_crypto_key
*key
)
134 return &ksm
->slot_hashtable
[hash_ptr(key
, ksm
->log_slot_ht_size
)];
137 static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot
*slot
)
139 struct blk_keyslot_manager
*ksm
= slot
->ksm
;
142 spin_lock_irqsave(&ksm
->idle_slots_lock
, flags
);
143 list_del(&slot
->idle_slot_node
);
144 spin_unlock_irqrestore(&ksm
->idle_slots_lock
, flags
);
147 static struct blk_ksm_keyslot
*blk_ksm_find_keyslot(
148 struct blk_keyslot_manager
*ksm
,
149 const struct blk_crypto_key
*key
)
151 const struct hlist_head
*head
= blk_ksm_hash_bucket_for_key(ksm
, key
);
152 struct blk_ksm_keyslot
*slotp
;
154 hlist_for_each_entry(slotp
, head
, hash_node
) {
155 if (slotp
->key
== key
)
161 static struct blk_ksm_keyslot
*blk_ksm_find_and_grab_keyslot(
162 struct blk_keyslot_manager
*ksm
,
163 const struct blk_crypto_key
*key
)
165 struct blk_ksm_keyslot
*slot
;
167 slot
= blk_ksm_find_keyslot(ksm
, key
);
170 if (atomic_inc_return(&slot
->slot_refs
) == 1) {
171 /* Took first reference to this slot; remove it from LRU list */
172 blk_ksm_remove_slot_from_lru_list(slot
);
177 unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot
*slot
)
179 return slot
- slot
->ksm
->slots
;
181 EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx
);
184 * blk_ksm_get_slot_for_key() - Program a key into a keyslot.
185 * @ksm: The keyslot manager to program the key into.
186 * @key: Pointer to the key object to program, including the raw key, crypto
187 * mode, and data unit size.
188 * @slot_ptr: A pointer to return the pointer of the allocated keyslot.
190 * Get a keyslot that's been programmed with the specified key. If one already
191 * exists, return it with incremented refcount. Otherwise, wait for a keyslot
192 * to become idle and program it.
194 * Context: Process context. Takes and releases ksm->lock.
195 * Return: BLK_STS_OK on success (and keyslot is set to the pointer of the
196 * allocated keyslot), or some other blk_status_t otherwise (and
197 * keyslot is set to NULL).
199 blk_status_t
blk_ksm_get_slot_for_key(struct blk_keyslot_manager
*ksm
,
200 const struct blk_crypto_key
*key
,
201 struct blk_ksm_keyslot
**slot_ptr
)
203 struct blk_ksm_keyslot
*slot
;
208 down_read(&ksm
->lock
);
209 slot
= blk_ksm_find_and_grab_keyslot(ksm
, key
);
215 blk_ksm_hw_enter(ksm
);
216 slot
= blk_ksm_find_and_grab_keyslot(ksm
, key
);
218 blk_ksm_hw_exit(ksm
);
223 * If we're here, that means there wasn't a slot that was
224 * already programmed with the key. So try to program it.
226 if (!list_empty(&ksm
->idle_slots
))
229 blk_ksm_hw_exit(ksm
);
230 wait_event(ksm
->idle_slots_wait_queue
,
231 !list_empty(&ksm
->idle_slots
));
234 slot
= list_first_entry(&ksm
->idle_slots
, struct blk_ksm_keyslot
,
236 slot_idx
= blk_ksm_get_slot_idx(slot
);
238 err
= ksm
->ksm_ll_ops
.keyslot_program(ksm
, key
, slot_idx
);
240 wake_up(&ksm
->idle_slots_wait_queue
);
241 blk_ksm_hw_exit(ksm
);
242 return errno_to_blk_status(err
);
245 /* Move this slot to the hash list for the new key. */
247 hlist_del(&slot
->hash_node
);
249 hlist_add_head(&slot
->hash_node
, blk_ksm_hash_bucket_for_key(ksm
, key
));
251 atomic_set(&slot
->slot_refs
, 1);
253 blk_ksm_remove_slot_from_lru_list(slot
);
255 blk_ksm_hw_exit(ksm
);
262 * blk_ksm_put_slot() - Release a reference to a slot
263 * @slot: The keyslot to release the reference of.
265 * Context: Any context.
267 void blk_ksm_put_slot(struct blk_ksm_keyslot
*slot
)
269 struct blk_keyslot_manager
*ksm
;
277 if (atomic_dec_and_lock_irqsave(&slot
->slot_refs
,
278 &ksm
->idle_slots_lock
, flags
)) {
279 list_add_tail(&slot
->idle_slot_node
, &ksm
->idle_slots
);
280 spin_unlock_irqrestore(&ksm
->idle_slots_lock
, flags
);
281 wake_up(&ksm
->idle_slots_wait_queue
);
286 * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is
287 * supported by a ksm.
288 * @ksm: The keyslot manager to check
289 * @cfg: The crypto configuration to check for.
291 * Checks for crypto_mode/data unit size/dun bytes support.
293 * Return: Whether or not this ksm supports the specified crypto config.
295 bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager
*ksm
,
296 const struct blk_crypto_config
*cfg
)
300 if (!(ksm
->crypto_modes_supported
[cfg
->crypto_mode
] &
301 cfg
->data_unit_size
))
303 if (ksm
->max_dun_bytes_supported
< cfg
->dun_bytes
)
309 * blk_ksm_evict_key() - Evict a key from the lower layer device.
310 * @ksm: The keyslot manager to evict from
311 * @key: The key to evict
313 * Find the keyslot that the specified key was programmed into, and evict that
314 * slot from the lower layer device. The slot must not be in use by any
315 * in-flight IO when this function is called.
317 * Context: Process context. Takes and releases ksm->lock.
318 * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY
319 * if the keyslot is still in use, or another -errno value on other
322 int blk_ksm_evict_key(struct blk_keyslot_manager
*ksm
,
323 const struct blk_crypto_key
*key
)
325 struct blk_ksm_keyslot
*slot
;
328 blk_ksm_hw_enter(ksm
);
329 slot
= blk_ksm_find_keyslot(ksm
, key
);
333 if (WARN_ON_ONCE(atomic_read(&slot
->slot_refs
) != 0)) {
337 err
= ksm
->ksm_ll_ops
.keyslot_evict(ksm
, key
,
338 blk_ksm_get_slot_idx(slot
));
342 hlist_del(&slot
->hash_node
);
346 blk_ksm_hw_exit(ksm
);
351 * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
352 * @ksm: The keyslot manager
354 * Re-program all keyslots that are supposed to have a key programmed. This is
355 * intended only for use by drivers for hardware that loses its keys on reset.
357 * Context: Process context. Takes and releases ksm->lock.
359 void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager
*ksm
)
363 /* This is for device initialization, so don't resume the device */
364 down_write(&ksm
->lock
);
365 for (slot
= 0; slot
< ksm
->num_slots
; slot
++) {
366 const struct blk_crypto_key
*key
= ksm
->slots
[slot
].key
;
372 err
= ksm
->ksm_ll_ops
.keyslot_program(ksm
, key
, slot
);
375 up_write(&ksm
->lock
);
377 EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys
);
379 void blk_ksm_destroy(struct blk_keyslot_manager
*ksm
)
383 kvfree(ksm
->slot_hashtable
);
384 kvfree_sensitive(ksm
->slots
, sizeof(ksm
->slots
[0]) * ksm
->num_slots
);
385 memzero_explicit(ksm
, sizeof(*ksm
));
387 EXPORT_SYMBOL_GPL(blk_ksm_destroy
);
389 bool blk_ksm_register(struct blk_keyslot_manager
*ksm
, struct request_queue
*q
)
391 if (blk_integrity_queue_supports_integrity(q
)) {
392 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
398 EXPORT_SYMBOL_GPL(blk_ksm_register
);
400 void blk_ksm_unregister(struct request_queue
*q
)