1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Google LLC
7 * DOC: blk-crypto profiles
9 * 'struct blk_crypto_profile' contains all generic inline encryption-related
10 * state for a particular inline encryption device. blk_crypto_profile serves
11 * as the way that drivers for inline encryption hardware expose their crypto
12 * capabilities and certain functions (e.g., functions to program and evict
13 * keys) to upper layers. Device drivers that want to support inline encryption
14 * construct a crypto profile, then associate it with the disk's request_queue.
16 * If the device has keyslots, then its blk_crypto_profile also handles managing
17 * these keyslots in a device-independent way, using the driver-provided
18 * functions to program and evict keys as needed. This includes keeping track
19 * of which key and how many I/O requests are using each keyslot, getting
20 * keyslots for I/O requests, and handling key eviction requests.
22 * For more information, see Documentation/block/inline-encryption.rst.
25 #define pr_fmt(fmt) "blk-crypto: " fmt
27 #include <linux/blk-crypto-profile.h>
28 #include <linux/device.h>
29 #include <linux/atomic.h>
30 #include <linux/mutex.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/wait.h>
33 #include <linux/blkdev.h>
34 #include <linux/blk-integrity.h>
35 #include "blk-crypto-internal.h"
37 struct blk_crypto_keyslot {
39 struct list_head idle_slot_node;
40 struct hlist_node hash_node;
41 const struct blk_crypto_key *key;
42 struct blk_crypto_profile *profile;
45 static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
48 * Calling into the driver requires profile->lock held and the device
49 * resumed. But we must resume the device first, since that can acquire
50 * and release profile->lock via blk_crypto_reprogram_all_keys().
53 pm_runtime_get_sync(profile->dev);
54 down_write(&profile->lock);
57 static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
59 up_write(&profile->lock);
61 pm_runtime_put_sync(profile->dev);
65 * blk_crypto_profile_init() - Initialize a blk_crypto_profile
66 * @profile: the blk_crypto_profile to initialize
67 * @num_slots: the number of keyslots
69 * Storage drivers must call this when starting to set up a blk_crypto_profile,
70 * before filling in additional fields.
72 * Return: 0 on success, or else a negative error code.
74 int blk_crypto_profile_init(struct blk_crypto_profile *profile,
75 unsigned int num_slots)
79 unsigned int slot_hashtable_size;
81 memset(profile, 0, sizeof(*profile));
82 init_rwsem(&profile->lock);
87 /* Initialize keyslot management data. */
89 profile->slots = kvcalloc(num_slots, sizeof(profile->slots[0]),
94 profile->num_slots = num_slots;
96 init_waitqueue_head(&profile->idle_slots_wait_queue);
97 INIT_LIST_HEAD(&profile->idle_slots);
99 for (slot = 0; slot < num_slots; slot++) {
100 profile->slots[slot].profile = profile;
101 list_add_tail(&profile->slots[slot].idle_slot_node,
102 &profile->idle_slots);
105 spin_lock_init(&profile->idle_slots_lock);
107 slot_hashtable_size = roundup_pow_of_two(num_slots);
109 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
110 * buckets. This only makes a difference when there is only 1 keyslot.
112 if (slot_hashtable_size < 2)
113 slot_hashtable_size = 2;
115 profile->log_slot_ht_size = ilog2(slot_hashtable_size);
116 profile->slot_hashtable =
117 kvmalloc_array(slot_hashtable_size,
118 sizeof(profile->slot_hashtable[0]), GFP_KERNEL);
119 if (!profile->slot_hashtable)
121 for (i = 0; i < slot_hashtable_size; i++)
122 INIT_HLIST_HEAD(&profile->slot_hashtable[i]);
127 blk_crypto_profile_destroy(profile);
130 EXPORT_SYMBOL_GPL(blk_crypto_profile_init);
132 static void blk_crypto_profile_destroy_callback(void *profile)
134 blk_crypto_profile_destroy(profile);
138 * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init()
139 * @dev: the device which owns the blk_crypto_profile
140 * @profile: the blk_crypto_profile to initialize
141 * @num_slots: the number of keyslots
143 * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be
144 * called automatically on driver detach.
146 * Return: 0 on success, or else a negative error code.
148 int devm_blk_crypto_profile_init(struct device *dev,
149 struct blk_crypto_profile *profile,
150 unsigned int num_slots)
152 int err = blk_crypto_profile_init(profile, num_slots);
157 return devm_add_action_or_reset(dev,
158 blk_crypto_profile_destroy_callback,
161 EXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init);
163 static inline struct hlist_head *
164 blk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile,
165 const struct blk_crypto_key *key)
167 return &profile->slot_hashtable[
168 hash_ptr(key, profile->log_slot_ht_size)];
172 blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot)
174 struct blk_crypto_profile *profile = slot->profile;
177 spin_lock_irqsave(&profile->idle_slots_lock, flags);
178 list_del(&slot->idle_slot_node);
179 spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
182 static struct blk_crypto_keyslot *
183 blk_crypto_find_keyslot(struct blk_crypto_profile *profile,
184 const struct blk_crypto_key *key)
186 const struct hlist_head *head =
187 blk_crypto_hash_bucket_for_key(profile, key);
188 struct blk_crypto_keyslot *slotp;
190 hlist_for_each_entry(slotp, head, hash_node) {
191 if (slotp->key == key)
197 static struct blk_crypto_keyslot *
198 blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile,
199 const struct blk_crypto_key *key)
201 struct blk_crypto_keyslot *slot;
203 slot = blk_crypto_find_keyslot(profile, key);
206 if (atomic_inc_return(&slot->slot_refs) == 1) {
207 /* Took first reference to this slot; remove it from LRU list */
208 blk_crypto_remove_slot_from_lru_list(slot);
214 * blk_crypto_keyslot_index() - Get the index of a keyslot
215 * @slot: a keyslot that blk_crypto_get_keyslot() returned
217 * Return: the 0-based index of the keyslot within the device's keyslots.
219 unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot)
221 return slot - slot->profile->slots;
223 EXPORT_SYMBOL_GPL(blk_crypto_keyslot_index);
226 * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed.
227 * @profile: the crypto profile of the device the key will be used on
228 * @key: the key that will be used
229 * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct
230 * will be stored here; otherwise NULL will be stored here.
232 * If the device has keyslots, this gets a keyslot that's been programmed with
233 * the specified key. If the key is already in a slot, this reuses it;
234 * otherwise this waits for a slot to become idle and programs the key into it.
236 * This must be paired with a call to blk_crypto_put_keyslot().
238 * Context: Process context. Takes and releases profile->lock.
239 * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or
240 * one wasn't needed; or a blk_status_t error on failure.
242 blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
243 const struct blk_crypto_key *key,
244 struct blk_crypto_keyslot **slot_ptr)
246 struct blk_crypto_keyslot *slot;
253 * If the device has no concept of "keyslots", then there is no need to
256 if (profile->num_slots == 0)
259 down_read(&profile->lock);
260 slot = blk_crypto_find_and_grab_keyslot(profile, key);
261 up_read(&profile->lock);
266 blk_crypto_hw_enter(profile);
267 slot = blk_crypto_find_and_grab_keyslot(profile, key);
269 blk_crypto_hw_exit(profile);
274 * If we're here, that means there wasn't a slot that was
275 * already programmed with the key. So try to program it.
277 if (!list_empty(&profile->idle_slots))
280 blk_crypto_hw_exit(profile);
281 wait_event(profile->idle_slots_wait_queue,
282 !list_empty(&profile->idle_slots));
285 slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot,
287 slot_idx = blk_crypto_keyslot_index(slot);
289 err = profile->ll_ops.keyslot_program(profile, key, slot_idx);
291 wake_up(&profile->idle_slots_wait_queue);
292 blk_crypto_hw_exit(profile);
293 return errno_to_blk_status(err);
296 /* Move this slot to the hash list for the new key. */
298 hlist_del(&slot->hash_node);
300 hlist_add_head(&slot->hash_node,
301 blk_crypto_hash_bucket_for_key(profile, key));
303 atomic_set(&slot->slot_refs, 1);
305 blk_crypto_remove_slot_from_lru_list(slot);
307 blk_crypto_hw_exit(profile);
314 * blk_crypto_put_keyslot() - Release a reference to a keyslot
315 * @slot: The keyslot to release the reference of (may be NULL).
317 * Context: Any context.
319 void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot)
321 struct blk_crypto_profile *profile;
327 profile = slot->profile;
329 if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
330 &profile->idle_slots_lock, flags)) {
331 list_add_tail(&slot->idle_slot_node, &profile->idle_slots);
332 spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
333 wake_up(&profile->idle_slots_wait_queue);
338 * __blk_crypto_cfg_supported() - Check whether the given crypto profile
339 * supports the given crypto configuration.
340 * @profile: the crypto profile to check
341 * @cfg: the crypto configuration to check for
343 * Return: %true if @profile supports the given @cfg.
345 bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
346 const struct blk_crypto_config *cfg)
350 if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size))
352 if (profile->max_dun_bytes_supported < cfg->dun_bytes)
358 * This is an internal function that evicts a key from an inline encryption
359 * device that can be either a real device or the blk-crypto-fallback "device".
360 * It is used only by blk_crypto_evict_key(); see that function for details.
362 int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
363 const struct blk_crypto_key *key)
365 struct blk_crypto_keyslot *slot;
368 if (profile->num_slots == 0) {
369 if (profile->ll_ops.keyslot_evict) {
370 blk_crypto_hw_enter(profile);
371 err = profile->ll_ops.keyslot_evict(profile, key, -1);
372 blk_crypto_hw_exit(profile);
378 blk_crypto_hw_enter(profile);
379 slot = blk_crypto_find_keyslot(profile, key);
382 * Not an error, since a key not in use by I/O is not guaranteed
383 * to be in a keyslot. There can be more keys than keyslots.
389 if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
390 /* BUG: key is still in use by I/O */
394 err = profile->ll_ops.keyslot_evict(profile, key,
395 blk_crypto_keyslot_index(slot));
398 * Callers free the key even on error, so unlink the key from the hash
399 * table and clear slot->key even on error.
401 hlist_del(&slot->hash_node);
404 blk_crypto_hw_exit(profile);
409 * blk_crypto_reprogram_all_keys() - Re-program all keyslots.
410 * @profile: The crypto profile
412 * Re-program all keyslots that are supposed to have a key programmed. This is
413 * intended only for use by drivers for hardware that loses its keys on reset.
415 * Context: Process context. Takes and releases profile->lock.
417 void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
421 if (profile->num_slots == 0)
424 /* This is for device initialization, so don't resume the device */
425 down_write(&profile->lock);
426 for (slot = 0; slot < profile->num_slots; slot++) {
427 const struct blk_crypto_key *key = profile->slots[slot].key;
433 err = profile->ll_ops.keyslot_program(profile, key, slot);
436 up_write(&profile->lock);
438 EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys);
440 void blk_crypto_profile_destroy(struct blk_crypto_profile *profile)
444 kvfree(profile->slot_hashtable);
445 kvfree_sensitive(profile->slots,
446 sizeof(profile->slots[0]) * profile->num_slots);
447 memzero_explicit(profile, sizeof(*profile));
449 EXPORT_SYMBOL_GPL(blk_crypto_profile_destroy);
451 bool blk_crypto_register(struct blk_crypto_profile *profile,
452 struct request_queue *q)
454 if (blk_integrity_queue_supports_integrity(q)) {
455 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
458 q->crypto_profile = profile;
461 EXPORT_SYMBOL_GPL(blk_crypto_register);
464 * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities
466 * @parent: the crypto profile for the parent device
467 * @child: the crypto profile for the child device, or NULL
469 * This clears all crypto capabilities in @parent that aren't set in @child. If
470 * @child is NULL, then this clears all parent capabilities.
472 * Only use this when setting up the crypto profile for a layered device, before
473 * it's been exposed yet.
475 void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
476 const struct blk_crypto_profile *child)
481 parent->max_dun_bytes_supported =
482 min(parent->max_dun_bytes_supported,
483 child->max_dun_bytes_supported);
484 for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++)
485 parent->modes_supported[i] &= child->modes_supported[i];
487 parent->max_dun_bytes_supported = 0;
488 memset(parent->modes_supported, 0,
489 sizeof(parent->modes_supported));
492 EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities);
495 * blk_crypto_has_capabilities() - Check whether @target supports at least all
496 * the crypto capabilities that @reference does.
497 * @target: the target profile
498 * @reference: the reference profile
500 * Return: %true if @target supports all the crypto capabilities of @reference.
502 bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
503 const struct blk_crypto_profile *reference)
513 for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) {
514 if (reference->modes_supported[i] & ~target->modes_supported[i])
518 if (reference->max_dun_bytes_supported >
519 target->max_dun_bytes_supported)
524 EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities);
527 * blk_crypto_update_capabilities() - Update the capabilities of a crypto
528 * profile to match those of another crypto
530 * @dst: The crypto profile whose capabilities to update.
531 * @src: The crypto profile whose capabilities this function will update @dst's
534 * Blk-crypto requires that crypto capabilities that were
535 * advertised when a bio was created continue to be supported by the
536 * device until that bio is ended. This is turn means that a device cannot
537 * shrink its advertised crypto capabilities without any explicit
538 * synchronization with upper layers. So if there's no such explicit
539 * synchronization, @src must support all the crypto capabilities that
540 * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)).
542 * Note also that as long as the crypto capabilities are being expanded, the
543 * order of updates becoming visible is not important because it's alright
544 * for blk-crypto to see stale values - they only cause blk-crypto to
545 * believe that a crypto capability isn't supported when it actually is (which
546 * might result in blk-crypto-fallback being used if available, or the bio being
549 void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
550 const struct blk_crypto_profile *src)
552 memcpy(dst->modes_supported, src->modes_supported,
553 sizeof(dst->modes_supported));
555 dst->max_dun_bytes_supported = src->max_dun_bytes_supported;
557 EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);