blob: 7ba541f73ce8030f37c78701d2c7cc28ad9518e4 [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>
Eric Biggers25b3bc72021-01-21 00:21:54 -080032#include <linux/device.h>
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070033#include <linux/atomic.h>
34#include <linux/mutex.h>
Eric Biggersa59152c2020-02-13 15:08:24 -080035#include <linux/pm_runtime.h>
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070036#include <linux/wait.h>
37#include <linux/blkdev.h>
38
Satya Tangiralac2b86b72020-06-16 14:33:37 -070039struct blk_ksm_keyslot {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070040 atomic_t slot_refs;
41 struct list_head idle_slot_node;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -080042 struct hlist_node hash_node;
Satya Tangiralac2b86b72020-06-16 14:33:37 -070043 const struct blk_crypto_key *key;
44 struct blk_keyslot_manager *ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070045};
46
Satya Tangiralac2b86b72020-06-16 14:33:37 -070047static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm)
Eric Biggersa59152c2020-02-13 15:08:24 -080048{
49 /*
50 * Calling into the driver requires ksm->lock held and the device
51 * resumed. But we must resume the device first, since that can acquire
Satya Tangiralac2b86b72020-06-16 14:33:37 -070052 * and release ksm->lock via blk_ksm_reprogram_all_keys().
Eric Biggersa59152c2020-02-13 15:08:24 -080053 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -070054 if (ksm->dev)
55 pm_runtime_get_sync(ksm->dev);
Eric Biggersa59152c2020-02-13 15:08:24 -080056 down_write(&ksm->lock);
57}
58
Satya Tangiralac2b86b72020-06-16 14:33:37 -070059static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm)
Eric Biggersa59152c2020-02-13 15:08:24 -080060{
61 up_write(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -070062 if (ksm->dev)
63 pm_runtime_put_sync(ksm->dev);
64}
65
66static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm)
67{
68 return ksm->num_slots == 0;
Eric Biggersa59152c2020-02-13 15:08:24 -080069}
70
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070071/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -070072 * blk_ksm_init() - Initialize a keyslot manager
73 * @ksm: The keyslot_manager to initialize.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070074 * @num_slots: The number of key slots to manage.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070075 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -070076 * Allocate memory for keyslots and initialize a keyslot manager. Called by
77 * e.g. storage drivers to set up a keyslot manager in their request_queue.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070078 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -070079 * Return: 0 on success, or else a negative error code.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070080 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -070081int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070082{
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -080083 unsigned int slot;
84 unsigned int i;
Satya Tangiralac2b86b72020-06-16 14:33:37 -070085 unsigned int slot_hashtable_size;
86
87 memset(ksm, 0, sizeof(*ksm));
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070088
89 if (num_slots == 0)
Satya Tangiralac2b86b72020-06-16 14:33:37 -070090 return -EINVAL;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070091
Satya Tangiralac2b86b72020-06-16 14:33:37 -070092 ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL);
93 if (!ksm->slots)
94 return -ENOMEM;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070095
96 ksm->num_slots = num_slots;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -070097
98 init_rwsem(&ksm->lock);
99
100 init_waitqueue_head(&ksm->idle_slots_wait_queue);
101 INIT_LIST_HEAD(&ksm->idle_slots);
102
103 for (slot = 0; slot < num_slots; slot++) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700104 ksm->slots[slot].ksm = ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700105 list_add_tail(&ksm->slots[slot].idle_slot_node,
106 &ksm->idle_slots);
107 }
108
109 spin_lock_init(&ksm->idle_slots_lock);
110
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700111 slot_hashtable_size = roundup_pow_of_two(num_slots);
Eric Biggers47a846532020-11-11 13:48:55 -0800112 /*
113 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
114 * buckets. This only makes a difference when there is only 1 keyslot.
115 */
116 if (slot_hashtable_size < 2)
117 slot_hashtable_size = 2;
118
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700119 ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
120 ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800121 sizeof(ksm->slot_hashtable[0]),
122 GFP_KERNEL);
123 if (!ksm->slot_hashtable)
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700124 goto err_destroy_ksm;
125 for (i = 0; i < slot_hashtable_size; i++)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800126 INIT_HLIST_HEAD(&ksm->slot_hashtable[i]);
127
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700128 return 0;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800129
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700130err_destroy_ksm:
131 blk_ksm_destroy(ksm);
132 return -ENOMEM;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700133}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700134EXPORT_SYMBOL_GPL(blk_ksm_init);
Eric Biggers77058132020-05-06 14:15:06 -0700135
Eric Biggers25b3bc72021-01-21 00:21:54 -0800136static void blk_ksm_destroy_callback(void *ksm)
137{
138 blk_ksm_destroy(ksm);
139}
140
141/**
142 * devm_blk_ksm_init() - Resource-managed blk_ksm_init()
143 * @dev: The device which owns the blk_keyslot_manager.
144 * @ksm: The blk_keyslot_manager to initialize.
145 * @num_slots: The number of key slots to manage.
146 *
147 * Like blk_ksm_init(), but causes blk_ksm_destroy() to be called automatically
148 * on driver detach.
149 *
150 * Return: 0 on success, or else a negative error code.
151 */
152int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm,
153 unsigned int num_slots)
154{
155 int err = blk_ksm_init(ksm, num_slots);
156
157 if (err)
158 return err;
159
160 return devm_add_action_or_reset(dev, blk_ksm_destroy_callback, ksm);
161}
162EXPORT_SYMBOL_GPL(devm_blk_ksm_init);
163
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800164static inline struct hlist_head *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700165blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm,
166 const struct blk_crypto_key *key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800167{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700168 return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)];
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800169}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700170
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700171static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700172{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700173 struct blk_keyslot_manager *ksm = slot->ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700174 unsigned long flags;
175
176 spin_lock_irqsave(&ksm->idle_slots_lock, flags);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700177 list_del(&slot->idle_slot_node);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700178 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700179}
180
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700181static struct blk_ksm_keyslot *blk_ksm_find_keyslot(
182 struct blk_keyslot_manager *ksm,
183 const struct blk_crypto_key *key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800184{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700185 const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key);
186 struct blk_ksm_keyslot *slotp;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800187
188 hlist_for_each_entry(slotp, head, hash_node) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700189 if (slotp->key == key)
190 return slotp;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800191 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700192 return NULL;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800193}
194
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700195static struct blk_ksm_keyslot *blk_ksm_find_and_grab_keyslot(
196 struct blk_keyslot_manager *ksm,
197 const struct blk_crypto_key *key)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700198{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700199 struct blk_ksm_keyslot *slot;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700200
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700201 slot = blk_ksm_find_keyslot(ksm, key);
202 if (!slot)
203 return NULL;
204 if (atomic_inc_return(&slot->slot_refs) == 1) {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700205 /* Took first reference to this slot; remove it from LRU list */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700206 blk_ksm_remove_slot_from_lru_list(slot);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700207 }
208 return slot;
209}
210
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700211unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot)
212{
213 return slot - slot->ksm->slots;
214}
215EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx);
216
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700217/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700218 * blk_ksm_get_slot_for_key() - Program a key into a keyslot.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700219 * @ksm: The keyslot manager to program the key into.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800220 * @key: Pointer to the key object to program, including the raw key, crypto
221 * mode, and data unit size.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700222 * @slot_ptr: A pointer to return the pointer of the allocated keyslot.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700223 *
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800224 * Get a keyslot that's been programmed with the specified key. If one already
225 * exists, return it with incremented refcount. Otherwise, wait for a keyslot
226 * to become idle and program it.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700227 *
228 * Context: Process context. Takes and releases ksm->lock.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700229 * Return: BLK_STS_OK on success (and keyslot is set to the pointer of the
230 * allocated keyslot), or some other blk_status_t otherwise (and
231 * keyslot is set to NULL).
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700232 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700233blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm,
234 const struct blk_crypto_key *key,
235 struct blk_ksm_keyslot **slot_ptr)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700236{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700237 struct blk_ksm_keyslot *slot;
238 int slot_idx;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700239 int err;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700240
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700241 *slot_ptr = NULL;
242
243 if (blk_ksm_is_passthrough(ksm))
244 return BLK_STS_OK;
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800245
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700246 down_read(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700247 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700248 up_read(&ksm->lock);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700249 if (slot)
250 goto success;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700251
252 for (;;) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700253 blk_ksm_hw_enter(ksm);
254 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
255 if (slot) {
256 blk_ksm_hw_exit(ksm);
257 goto success;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700258 }
259
260 /*
261 * If we're here, that means there wasn't a slot that was
262 * already programmed with the key. So try to program it.
263 */
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800264 if (!list_empty(&ksm->idle_slots))
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700265 break;
266
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700267 blk_ksm_hw_exit(ksm);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700268 wait_event(ksm->idle_slots_wait_queue,
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800269 !list_empty(&ksm->idle_slots));
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700270 }
271
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700272 slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot,
273 idle_slot_node);
274 slot_idx = blk_ksm_get_slot_idx(slot);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700275
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700276 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700277 if (err) {
278 wake_up(&ksm->idle_slots_wait_queue);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700279 blk_ksm_hw_exit(ksm);
280 return errno_to_blk_status(err);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700281 }
282
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800283 /* Move this slot to the hash list for the new key. */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700284 if (slot->key)
285 hlist_del(&slot->hash_node);
286 slot->key = key;
287 hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key));
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800288
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700289 atomic_set(&slot->slot_refs, 1);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800290
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700291 blk_ksm_remove_slot_from_lru_list(slot);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700292
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700293 blk_ksm_hw_exit(ksm);
294success:
295 *slot_ptr = slot;
296 return BLK_STS_OK;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700297}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700298
299/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700300 * blk_ksm_put_slot() - Release a reference to a slot
301 * @slot: The keyslot to release the reference of.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700302 *
303 * Context: Any context.
304 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700305void blk_ksm_put_slot(struct blk_ksm_keyslot *slot)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700306{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700307 struct blk_keyslot_manager *ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700308 unsigned long flags;
309
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700310 if (!slot)
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800311 return;
312
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700313 ksm = slot->ksm;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700314
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700315 if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700316 &ksm->idle_slots_lock, flags)) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700317 list_add_tail(&slot->idle_slot_node, &ksm->idle_slots);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700318 spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700319 wake_up(&ksm->idle_slots_wait_queue);
320 }
321}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700322
323/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700324 * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is
325 * supported by a ksm.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800326 * @ksm: The keyslot manager to check
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700327 * @cfg: The crypto configuration to check for.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700328 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700329 * Checks for crypto_mode/data unit size/dun bytes support.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700330 *
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700331 * Return: Whether or not this ksm supports the specified crypto config.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700332 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700333bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm,
334 const struct blk_crypto_config *cfg)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700335{
336 if (!ksm)
337 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700338 if (!(ksm->crypto_modes_supported[cfg->crypto_mode] &
339 cfg->data_unit_size))
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800340 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700341 if (ksm->max_dun_bytes_supported < cfg->dun_bytes)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800342 return false;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700343 if (cfg->is_hw_wrapped) {
Eric Biggers935b0c42020-04-03 12:06:11 -0700344 if (!(ksm->features & BLK_CRYPTO_FEATURE_WRAPPED_KEYS))
345 return false;
346 } else {
347 if (!(ksm->features & BLK_CRYPTO_FEATURE_STANDARD_KEYS))
348 return false;
349 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700350 return true;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700351}
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700352
353/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700354 * blk_ksm_evict_key() - Evict a key from the lower layer device.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800355 * @ksm: The keyslot manager to evict from
356 * @key: The key to evict
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700357 *
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800358 * Find the keyslot that the specified key was programmed into, and evict that
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700359 * slot from the lower layer device. The slot must not be in use by any
360 * in-flight IO when this function is called.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700361 *
362 * Context: Process context. Takes and releases ksm->lock.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700363 * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY
364 * if the keyslot is still in use, or another -errno value on other
365 * error.
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700366 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700367int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
368 const struct blk_crypto_key *key)
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700369{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700370 struct blk_ksm_keyslot *slot;
371 int err = 0;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700372
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700373 if (blk_ksm_is_passthrough(ksm)) {
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800374 if (ksm->ksm_ll_ops.keyslot_evict) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700375 blk_ksm_hw_enter(ksm);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800376 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700377 blk_ksm_hw_exit(ksm);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800378 return err;
379 }
380 return 0;
381 }
382
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700383 blk_ksm_hw_enter(ksm);
384 slot = blk_ksm_find_keyslot(ksm, key);
385 if (!slot)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800386 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700387
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700388 if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700389 err = -EBUSY;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800390 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700391 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700392 err = ksm->ksm_ll_ops.keyslot_evict(ksm, key,
393 blk_ksm_get_slot_idx(slot));
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800394 if (err)
395 goto out_unlock;
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700396
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700397 hlist_del(&slot->hash_node);
398 slot->key = NULL;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800399 err = 0;
400out_unlock:
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700401 blk_ksm_hw_exit(ksm);
Satya Tangiralaaac6c3d2019-10-24 14:44:23 -0700402 return err;
403}
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800404
405/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700406 * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800407 * @ksm: The keyslot manager
408 *
409 * Re-program all keyslots that are supposed to have a key programmed. This is
410 * intended only for use by drivers for hardware that loses its keys on reset.
411 *
412 * Context: Process context. Takes and releases ksm->lock.
413 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700414void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800415{
416 unsigned int slot;
417
Eric Biggers537d3bb2021-02-12 11:53:49 -0800418 if (blk_ksm_is_passthrough(ksm))
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800419 return;
420
Eric Biggersa59152c2020-02-13 15:08:24 -0800421 /* This is for device initialization, so don't resume the device */
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800422 down_write(&ksm->lock);
423 for (slot = 0; slot < ksm->num_slots; slot++) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700424 const struct blk_crypto_key *key = ksm->slots[slot].key;
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800425 int err;
426
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700427 if (!key)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800428 continue;
429
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700430 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800431 WARN_ON(err);
432 }
433 up_write(&ksm->lock);
434}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700435EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys);
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800436
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700437void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
Satya Tangiralacfd7e6c2019-12-17 14:26:29 -0800438{
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800439 if (!ksm)
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700440 return;
441 kvfree(ksm->slot_hashtable);
Eric Biggers3e20aa92020-06-16 08:56:54 -0700442 kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700443 memzero_explicit(ksm, sizeof(*ksm));
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800444}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700445EXPORT_SYMBOL_GPL(blk_ksm_destroy);
Satya Tangiralac7da3f42020-01-21 09:27:43 -0800446
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700447bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
Eric Biggersbea2b962020-01-21 09:27:43 -0800448{
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700449 if (blk_integrity_queue_supports_integrity(q)) {
450 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
451 return false;
Eric Biggersbea2b962020-01-21 09:27:43 -0800452 }
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700453 q->ksm = ksm;
454 return true;
Eric Biggersbea2b962020-01-21 09:27:43 -0800455}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700456EXPORT_SYMBOL_GPL(blk_ksm_register);
457
458void blk_ksm_unregister(struct request_queue *q)
459{
460 q->ksm = NULL;
461}
Eric Biggersbea2b962020-01-21 09:27:43 -0800462
463/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700464 * blk_ksm_derive_raw_secret() - Derive software secret from wrapped key
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800465 * @ksm: The keyslot manager
466 * @wrapped_key: The wrapped key
467 * @wrapped_key_size: Size of the wrapped key in bytes
468 * @secret: (output) the software secret
469 * @secret_size: (output) the number of secret bytes to derive
470 *
471 * Given a hardware-wrapped key, ask the hardware to derive a secret which
472 * software can use for cryptographic tasks other than inline encryption. The
473 * derived secret is guaranteed to be cryptographically isolated from the key
474 * with which any inline encryption with this wrapped key would actually be
475 * done. I.e., both will be derived from the unwrapped key.
476 *
477 * Return: 0 on success, -EOPNOTSUPP if hardware-wrapped keys are unsupported,
478 * or another -errno code.
479 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700480int blk_ksm_derive_raw_secret(struct blk_keyslot_manager *ksm,
481 const u8 *wrapped_key,
482 unsigned int wrapped_key_size,
483 u8 *secret, unsigned int secret_size)
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800484{
485 int err;
486
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800487 if (ksm->ksm_ll_ops.derive_raw_secret) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700488 blk_ksm_hw_enter(ksm);
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800489 err = ksm->ksm_ll_ops.derive_raw_secret(ksm, wrapped_key,
490 wrapped_key_size,
491 secret, secret_size);
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700492 blk_ksm_hw_exit(ksm);
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800493 } else {
494 err = -EOPNOTSUPP;
495 }
Barani Muthukumaran1daa0582020-01-02 12:01:34 -0800496
497 return err;
498}
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700499EXPORT_SYMBOL_GPL(blk_ksm_derive_raw_secret);
500
501/**
502 * blk_ksm_intersect_modes() - restrict supported modes by child device
503 * @parent: The keyslot manager for parent device
504 * @child: The keyslot manager for child device, or NULL
505 *
506 * Clear any crypto mode support bits in @parent that aren't set in @child.
507 * If @child is NULL, then all parent bits are cleared.
508 *
509 * Only use this when setting up the keyslot manager for a layered device,
510 * before it's been exposed yet.
511 */
512void blk_ksm_intersect_modes(struct blk_keyslot_manager *parent,
513 const struct blk_keyslot_manager *child)
514{
515 if (child) {
516 unsigned int i;
517
518 parent->max_dun_bytes_supported =
519 min(parent->max_dun_bytes_supported,
520 child->max_dun_bytes_supported);
Eric Biggers6b78d002020-08-11 09:35:23 -0700521 for (i = 0; i < ARRAY_SIZE(child->crypto_modes_supported);
522 i++) {
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700523 parent->crypto_modes_supported[i] &=
524 child->crypto_modes_supported[i];
525 }
Eric Biggers537d3bb2021-02-12 11:53:49 -0800526 parent->features &= child->features;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700527 } else {
528 parent->max_dun_bytes_supported = 0;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700529 memset(parent->crypto_modes_supported, 0,
530 sizeof(parent->crypto_modes_supported));
Eric Biggers537d3bb2021-02-12 11:53:49 -0800531 parent->features = 0;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700532 }
533}
534EXPORT_SYMBOL_GPL(blk_ksm_intersect_modes);
535
536/**
Eric Biggers537d3bb2021-02-12 11:53:49 -0800537 * blk_ksm_is_superset() - Check if a KSM supports a superset of crypto modes
538 * and DUN bytes that another KSM supports. Here,
539 * "superset" refers to the mathematical meaning of the
540 * word - i.e. if two KSMs have the *same* capabilities,
541 * they *are* considered supersets of each other.
542 * @ksm_superset: The KSM that we want to verify is a superset
543 * @ksm_subset: The KSM that we want to verify is a subset
544 *
545 * Return: True if @ksm_superset supports a superset of the crypto modes and DUN
546 * bytes that @ksm_subset supports.
547 */
548bool blk_ksm_is_superset(struct blk_keyslot_manager *ksm_superset,
549 struct blk_keyslot_manager *ksm_subset)
550{
551 int i;
552
553 if (!ksm_subset)
554 return true;
555
556 if (!ksm_superset)
557 return false;
558
559 for (i = 0; i < ARRAY_SIZE(ksm_superset->crypto_modes_supported); i++) {
560 if (ksm_subset->crypto_modes_supported[i] &
561 (~ksm_superset->crypto_modes_supported[i])) {
562 return false;
563 }
564 }
565
566 if (ksm_subset->max_dun_bytes_supported >
567 ksm_superset->max_dun_bytes_supported) {
568 return false;
569 }
570
571 if (ksm_subset->features & ~ksm_superset->features)
572 return false;
573
574 return true;
575}
576EXPORT_SYMBOL_GPL(blk_ksm_is_superset);
577
578/**
579 * blk_ksm_update_capabilities() - Update the restrictions of a KSM to those of
580 * another KSM
581 * @target_ksm: The KSM whose restrictions to update.
582 * @reference_ksm: The KSM to whose restrictions this function will update
583 * @target_ksm's restrictions to.
584 *
585 * Blk-crypto requires that crypto capabilities that were
586 * advertised when a bio was created continue to be supported by the
587 * device until that bio is ended. This is turn means that a device cannot
588 * shrink its advertised crypto capabilities without any explicit
589 * synchronization with upper layers. So if there's no such explicit
590 * synchronization, @reference_ksm must support all the crypto capabilities that
591 * @target_ksm does
592 * (i.e. we need blk_ksm_is_superset(@reference_ksm, @target_ksm) == true).
593 *
594 * Note also that as long as the crypto capabilities are being expanded, the
595 * order of updates becoming visible is not important because it's alright
596 * for blk-crypto to see stale values - they only cause blk-crypto to
597 * believe that a crypto capability isn't supported when it actually is (which
598 * might result in blk-crypto-fallback being used if available, or the bio being
599 * failed).
600 */
601void blk_ksm_update_capabilities(struct blk_keyslot_manager *target_ksm,
602 struct blk_keyslot_manager *reference_ksm)
603{
604 memcpy(target_ksm->crypto_modes_supported,
605 reference_ksm->crypto_modes_supported,
606 sizeof(target_ksm->crypto_modes_supported));
607
608 target_ksm->max_dun_bytes_supported =
609 reference_ksm->max_dun_bytes_supported;
610
611 target_ksm->features = reference_ksm->features;
612}
613EXPORT_SYMBOL_GPL(blk_ksm_update_capabilities);
614
615/**
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700616 * blk_ksm_init_passthrough() - Init a passthrough keyslot manager
617 * @ksm: The keyslot manager to init
618 *
619 * Initialize a passthrough keyslot manager.
620 * Called by e.g. storage drivers to set up a keyslot manager in their
621 * request_queue, when the storage driver wants to manage its keys by itself.
Eric Biggers537d3bb2021-02-12 11:53:49 -0800622 * This is useful for inline encryption hardware that doesn't have the concept
623 * of keyslots, and for layered devices.
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700624 */
625void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm)
626{
627 memset(ksm, 0, sizeof(*ksm));
628 init_rwsem(&ksm->lock);
629}
630EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough);