Merge tag 'platform-drivers-x86-v5.12-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / block / keyslot-manager.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 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  *
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.
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  *
25  * Upper layers will call blk_ksm_get_slot_for_key() to program a
26  * key into some slot in the inline encryption hardware.
27  */
28
29 #define pr_fmt(fmt) "blk-crypto: " fmt
30
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>
37
38 struct blk_ksm_keyslot {
39         atomic_t slot_refs;
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;
44 };
45
46 static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm)
47 {
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
51          * and release ksm->lock via blk_ksm_reprogram_all_keys().
52          */
53         if (ksm->dev)
54                 pm_runtime_get_sync(ksm->dev);
55         down_write(&ksm->lock);
56 }
57
58 static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm)
59 {
60         up_write(&ksm->lock);
61         if (ksm->dev)
62                 pm_runtime_put_sync(ksm->dev);
63 }
64
65 /**
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.
69  *
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.
72  *
73  * Return: 0 on success, or else a negative error code.
74  */
75 int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
76 {
77         unsigned int slot;
78         unsigned int i;
79         unsigned int slot_hashtable_size;
80
81         memset(ksm, 0, sizeof(*ksm));
82
83         if (num_slots == 0)
84                 return -EINVAL;
85
86         ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL);
87         if (!ksm->slots)
88                 return -ENOMEM;
89
90         ksm->num_slots = num_slots;
91
92         init_rwsem(&ksm->lock);
93
94         init_waitqueue_head(&ksm->idle_slots_wait_queue);
95         INIT_LIST_HEAD(&ksm->idle_slots);
96
97         for (slot = 0; slot < num_slots; slot++) {
98                 ksm->slots[slot].ksm = ksm;
99                 list_add_tail(&ksm->slots[slot].idle_slot_node,
100                               &ksm->idle_slots);
101         }
102
103         spin_lock_init(&ksm->idle_slots_lock);
104
105         slot_hashtable_size = roundup_pow_of_two(num_slots);
106         /*
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.
109          */
110         if (slot_hashtable_size < 2)
111                 slot_hashtable_size = 2;
112
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]),
116                                              GFP_KERNEL);
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]);
121
122         return 0;
123
124 err_destroy_ksm:
125         blk_ksm_destroy(ksm);
126         return -ENOMEM;
127 }
128 EXPORT_SYMBOL_GPL(blk_ksm_init);
129
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)
133 {
134         return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)];
135 }
136
137 static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot)
138 {
139         struct blk_keyslot_manager *ksm = slot->ksm;
140         unsigned long flags;
141
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);
145 }
146
147 static struct blk_ksm_keyslot *blk_ksm_find_keyslot(
148                                         struct blk_keyslot_manager *ksm,
149                                         const struct blk_crypto_key *key)
150 {
151         const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key);
152         struct blk_ksm_keyslot *slotp;
153
154         hlist_for_each_entry(slotp, head, hash_node) {
155                 if (slotp->key == key)
156                         return slotp;
157         }
158         return NULL;
159 }
160
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)
164 {
165         struct blk_ksm_keyslot *slot;
166
167         slot = blk_ksm_find_keyslot(ksm, key);
168         if (!slot)
169                 return NULL;
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);
173         }
174         return slot;
175 }
176
177 unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot)
178 {
179         return slot - slot->ksm->slots;
180 }
181 EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx);
182
183 /**
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.
189  *
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.
193  *
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).
198  */
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)
202 {
203         struct blk_ksm_keyslot *slot;
204         int slot_idx;
205         int err;
206
207         *slot_ptr = NULL;
208         down_read(&ksm->lock);
209         slot = blk_ksm_find_and_grab_keyslot(ksm, key);
210         up_read(&ksm->lock);
211         if (slot)
212                 goto success;
213
214         for (;;) {
215                 blk_ksm_hw_enter(ksm);
216                 slot = blk_ksm_find_and_grab_keyslot(ksm, key);
217                 if (slot) {
218                         blk_ksm_hw_exit(ksm);
219                         goto success;
220                 }
221
222                 /*
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.
225                  */
226                 if (!list_empty(&ksm->idle_slots))
227                         break;
228
229                 blk_ksm_hw_exit(ksm);
230                 wait_event(ksm->idle_slots_wait_queue,
231                            !list_empty(&ksm->idle_slots));
232         }
233
234         slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot,
235                                 idle_slot_node);
236         slot_idx = blk_ksm_get_slot_idx(slot);
237
238         err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx);
239         if (err) {
240                 wake_up(&ksm->idle_slots_wait_queue);
241                 blk_ksm_hw_exit(ksm);
242                 return errno_to_blk_status(err);
243         }
244
245         /* Move this slot to the hash list for the new key. */
246         if (slot->key)
247                 hlist_del(&slot->hash_node);
248         slot->key = key;
249         hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key));
250
251         atomic_set(&slot->slot_refs, 1);
252
253         blk_ksm_remove_slot_from_lru_list(slot);
254
255         blk_ksm_hw_exit(ksm);
256 success:
257         *slot_ptr = slot;
258         return BLK_STS_OK;
259 }
260
261 /**
262  * blk_ksm_put_slot() - Release a reference to a slot
263  * @slot: The keyslot to release the reference of.
264  *
265  * Context: Any context.
266  */
267 void blk_ksm_put_slot(struct blk_ksm_keyslot *slot)
268 {
269         struct blk_keyslot_manager *ksm;
270         unsigned long flags;
271
272         if (!slot)
273                 return;
274
275         ksm = slot->ksm;
276
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);
282         }
283 }
284
285 /**
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.
290  *
291  * Checks for crypto_mode/data unit size/dun bytes support.
292  *
293  * Return: Whether or not this ksm supports the specified crypto config.
294  */
295 bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm,
296                                   const struct blk_crypto_config *cfg)
297 {
298         if (!ksm)
299                 return false;
300         if (!(ksm->crypto_modes_supported[cfg->crypto_mode] &
301               cfg->data_unit_size))
302                 return false;
303         if (ksm->max_dun_bytes_supported < cfg->dun_bytes)
304                 return false;
305         return true;
306 }
307
308 /**
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
312  *
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.
316  *
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
320  *         error.
321  */
322 int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
323                       const struct blk_crypto_key *key)
324 {
325         struct blk_ksm_keyslot *slot;
326         int err = 0;
327
328         blk_ksm_hw_enter(ksm);
329         slot = blk_ksm_find_keyslot(ksm, key);
330         if (!slot)
331                 goto out_unlock;
332
333         if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
334                 err = -EBUSY;
335                 goto out_unlock;
336         }
337         err = ksm->ksm_ll_ops.keyslot_evict(ksm, key,
338                                             blk_ksm_get_slot_idx(slot));
339         if (err)
340                 goto out_unlock;
341
342         hlist_del(&slot->hash_node);
343         slot->key = NULL;
344         err = 0;
345 out_unlock:
346         blk_ksm_hw_exit(ksm);
347         return err;
348 }
349
350 /**
351  * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
352  * @ksm: The keyslot manager
353  *
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.
356  *
357  * Context: Process context. Takes and releases ksm->lock.
358  */
359 void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm)
360 {
361         unsigned int slot;
362
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;
367                 int err;
368
369                 if (!key)
370                         continue;
371
372                 err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
373                 WARN_ON(err);
374         }
375         up_write(&ksm->lock);
376 }
377 EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys);
378
379 void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
380 {
381         if (!ksm)
382                 return;
383         kvfree(ksm->slot_hashtable);
384         kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots);
385         memzero_explicit(ksm, sizeof(*ksm));
386 }
387 EXPORT_SYMBOL_GPL(blk_ksm_destroy);
388
389 bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
390 {
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");
393                 return false;
394         }
395         q->ksm = ksm;
396         return true;
397 }
398 EXPORT_SYMBOL_GPL(blk_ksm_register);
399
400 void blk_ksm_unregister(struct request_queue *q)
401 {
402         q->ksm = NULL;
403 }