1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2019 Google LLC
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /**
7*4882a593Smuzhiyun * DOC: The Keyslot Manager
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Many devices with inline encryption support have a limited number of "slots"
10*4882a593Smuzhiyun * into which encryption contexts may be programmed, and requests can be tagged
11*4882a593Smuzhiyun * with a slot number to specify the key to use for en/decryption.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * As the number of slots is limited, and programming keys is expensive on
14*4882a593Smuzhiyun * many inline encryption hardware, we don't want to program the same key into
15*4882a593Smuzhiyun * multiple slots - if multiple requests are using the same key, we want to
16*4882a593Smuzhiyun * program just one slot with that key and use that slot for all requests.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * The keyslot manager manages these keyslots appropriately, and also acts as
19*4882a593Smuzhiyun * an abstraction between the inline encryption hardware and the upper layers.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Lower layer devices will set up a keyslot manager in their request queue
22*4882a593Smuzhiyun * and tell it how to perform device specific operations like programming/
23*4882a593Smuzhiyun * evicting keys from keyslots.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Upper layers will call blk_ksm_get_slot_for_key() to program a
26*4882a593Smuzhiyun * key into some slot in the inline encryption hardware.
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define pr_fmt(fmt) "blk-crypto: " fmt
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <linux/keyslot-manager.h>
32*4882a593Smuzhiyun #include <linux/device.h>
33*4882a593Smuzhiyun #include <linux/atomic.h>
34*4882a593Smuzhiyun #include <linux/mutex.h>
35*4882a593Smuzhiyun #include <linux/pm_runtime.h>
36*4882a593Smuzhiyun #include <linux/wait.h>
37*4882a593Smuzhiyun #include <linux/blkdev.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct blk_ksm_keyslot {
40*4882a593Smuzhiyun atomic_t slot_refs;
41*4882a593Smuzhiyun struct list_head idle_slot_node;
42*4882a593Smuzhiyun struct hlist_node hash_node;
43*4882a593Smuzhiyun const struct blk_crypto_key *key;
44*4882a593Smuzhiyun struct blk_keyslot_manager *ksm;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
blk_ksm_hw_enter(struct blk_keyslot_manager * ksm)47*4882a593Smuzhiyun static inline void blk_ksm_hw_enter(struct blk_keyslot_manager *ksm)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * Calling into the driver requires ksm->lock held and the device
51*4882a593Smuzhiyun * resumed. But we must resume the device first, since that can acquire
52*4882a593Smuzhiyun * and release ksm->lock via blk_ksm_reprogram_all_keys().
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun if (ksm->dev)
55*4882a593Smuzhiyun pm_runtime_get_sync(ksm->dev);
56*4882a593Smuzhiyun down_write(&ksm->lock);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
blk_ksm_hw_exit(struct blk_keyslot_manager * ksm)59*4882a593Smuzhiyun static inline void blk_ksm_hw_exit(struct blk_keyslot_manager *ksm)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun up_write(&ksm->lock);
62*4882a593Smuzhiyun if (ksm->dev)
63*4882a593Smuzhiyun pm_runtime_put_sync(ksm->dev);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
blk_ksm_is_passthrough(struct blk_keyslot_manager * ksm)66*4882a593Smuzhiyun static inline bool blk_ksm_is_passthrough(struct blk_keyslot_manager *ksm)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun return ksm->num_slots == 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * blk_ksm_init() - Initialize a keyslot manager
73*4882a593Smuzhiyun * @ksm: The keyslot_manager to initialize.
74*4882a593Smuzhiyun * @num_slots: The number of key slots to manage.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * Allocate memory for keyslots and initialize a keyslot manager. Called by
77*4882a593Smuzhiyun * e.g. storage drivers to set up a keyslot manager in their request_queue.
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * Return: 0 on success, or else a negative error code.
80*4882a593Smuzhiyun */
blk_ksm_init(struct blk_keyslot_manager * ksm,unsigned int num_slots)81*4882a593Smuzhiyun int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun unsigned int slot;
84*4882a593Smuzhiyun unsigned int i;
85*4882a593Smuzhiyun unsigned int slot_hashtable_size;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun memset(ksm, 0, sizeof(*ksm));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun if (num_slots == 0)
90*4882a593Smuzhiyun return -EINVAL;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun ksm->slots = kvcalloc(num_slots, sizeof(ksm->slots[0]), GFP_KERNEL);
93*4882a593Smuzhiyun if (!ksm->slots)
94*4882a593Smuzhiyun return -ENOMEM;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun ksm->num_slots = num_slots;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun init_rwsem(&ksm->lock);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun init_waitqueue_head(&ksm->idle_slots_wait_queue);
101*4882a593Smuzhiyun INIT_LIST_HEAD(&ksm->idle_slots);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun for (slot = 0; slot < num_slots; slot++) {
104*4882a593Smuzhiyun ksm->slots[slot].ksm = ksm;
105*4882a593Smuzhiyun list_add_tail(&ksm->slots[slot].idle_slot_node,
106*4882a593Smuzhiyun &ksm->idle_slots);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun spin_lock_init(&ksm->idle_slots_lock);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun slot_hashtable_size = roundup_pow_of_two(num_slots);
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
114*4882a593Smuzhiyun * buckets. This only makes a difference when there is only 1 keyslot.
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun if (slot_hashtable_size < 2)
117*4882a593Smuzhiyun slot_hashtable_size = 2;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
120*4882a593Smuzhiyun ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
121*4882a593Smuzhiyun sizeof(ksm->slot_hashtable[0]),
122*4882a593Smuzhiyun GFP_KERNEL);
123*4882a593Smuzhiyun if (!ksm->slot_hashtable)
124*4882a593Smuzhiyun goto err_destroy_ksm;
125*4882a593Smuzhiyun for (i = 0; i < slot_hashtable_size; i++)
126*4882a593Smuzhiyun INIT_HLIST_HEAD(&ksm->slot_hashtable[i]);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return 0;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun err_destroy_ksm:
131*4882a593Smuzhiyun blk_ksm_destroy(ksm);
132*4882a593Smuzhiyun return -ENOMEM;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_init);
135*4882a593Smuzhiyun
blk_ksm_destroy_callback(void * ksm)136*4882a593Smuzhiyun static void blk_ksm_destroy_callback(void *ksm)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun blk_ksm_destroy(ksm);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /**
142*4882a593Smuzhiyun * devm_blk_ksm_init() - Resource-managed blk_ksm_init()
143*4882a593Smuzhiyun * @dev: The device which owns the blk_keyslot_manager.
144*4882a593Smuzhiyun * @ksm: The blk_keyslot_manager to initialize.
145*4882a593Smuzhiyun * @num_slots: The number of key slots to manage.
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * Like blk_ksm_init(), but causes blk_ksm_destroy() to be called automatically
148*4882a593Smuzhiyun * on driver detach.
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * Return: 0 on success, or else a negative error code.
151*4882a593Smuzhiyun */
devm_blk_ksm_init(struct device * dev,struct blk_keyslot_manager * ksm,unsigned int num_slots)152*4882a593Smuzhiyun int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm,
153*4882a593Smuzhiyun unsigned int num_slots)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun int err = blk_ksm_init(ksm, num_slots);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun if (err)
158*4882a593Smuzhiyun return err;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return devm_add_action_or_reset(dev, blk_ksm_destroy_callback, ksm);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_blk_ksm_init);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun static inline struct hlist_head *
blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager * ksm,const struct blk_crypto_key * key)165*4882a593Smuzhiyun blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm,
166*4882a593Smuzhiyun const struct blk_crypto_key *key)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun return &ksm->slot_hashtable[hash_ptr(key, ksm->log_slot_ht_size)];
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot * slot)171*4882a593Smuzhiyun static void blk_ksm_remove_slot_from_lru_list(struct blk_ksm_keyslot *slot)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct blk_keyslot_manager *ksm = slot->ksm;
174*4882a593Smuzhiyun unsigned long flags;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun spin_lock_irqsave(&ksm->idle_slots_lock, flags);
177*4882a593Smuzhiyun list_del(&slot->idle_slot_node);
178*4882a593Smuzhiyun spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
blk_ksm_find_keyslot(struct blk_keyslot_manager * ksm,const struct blk_crypto_key * key)181*4882a593Smuzhiyun static struct blk_ksm_keyslot *blk_ksm_find_keyslot(
182*4882a593Smuzhiyun struct blk_keyslot_manager *ksm,
183*4882a593Smuzhiyun const struct blk_crypto_key *key)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun const struct hlist_head *head = blk_ksm_hash_bucket_for_key(ksm, key);
186*4882a593Smuzhiyun struct blk_ksm_keyslot *slotp;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun hlist_for_each_entry(slotp, head, hash_node) {
189*4882a593Smuzhiyun if (slotp->key == key)
190*4882a593Smuzhiyun return slotp;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun return NULL;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
blk_ksm_find_and_grab_keyslot(struct blk_keyslot_manager * ksm,const struct blk_crypto_key * key)195*4882a593Smuzhiyun static struct blk_ksm_keyslot *blk_ksm_find_and_grab_keyslot(
196*4882a593Smuzhiyun struct blk_keyslot_manager *ksm,
197*4882a593Smuzhiyun const struct blk_crypto_key *key)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun struct blk_ksm_keyslot *slot;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun slot = blk_ksm_find_keyslot(ksm, key);
202*4882a593Smuzhiyun if (!slot)
203*4882a593Smuzhiyun return NULL;
204*4882a593Smuzhiyun if (atomic_inc_return(&slot->slot_refs) == 1) {
205*4882a593Smuzhiyun /* Took first reference to this slot; remove it from LRU list */
206*4882a593Smuzhiyun blk_ksm_remove_slot_from_lru_list(slot);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun return slot;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
blk_ksm_get_slot_idx(struct blk_ksm_keyslot * slot)211*4882a593Smuzhiyun unsigned int blk_ksm_get_slot_idx(struct blk_ksm_keyslot *slot)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun return slot - slot->ksm->slots;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_get_slot_idx);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun * blk_ksm_get_slot_for_key() - Program a key into a keyslot.
219*4882a593Smuzhiyun * @ksm: The keyslot manager to program the key into.
220*4882a593Smuzhiyun * @key: Pointer to the key object to program, including the raw key, crypto
221*4882a593Smuzhiyun * mode, and data unit size.
222*4882a593Smuzhiyun * @slot_ptr: A pointer to return the pointer of the allocated keyslot.
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * Get a keyslot that's been programmed with the specified key. If one already
225*4882a593Smuzhiyun * exists, return it with incremented refcount. Otherwise, wait for a keyslot
226*4882a593Smuzhiyun * to become idle and program it.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Context: Process context. Takes and releases ksm->lock.
229*4882a593Smuzhiyun * Return: BLK_STS_OK on success (and keyslot is set to the pointer of the
230*4882a593Smuzhiyun * allocated keyslot), or some other blk_status_t otherwise (and
231*4882a593Smuzhiyun * keyslot is set to NULL).
232*4882a593Smuzhiyun */
blk_ksm_get_slot_for_key(struct blk_keyslot_manager * ksm,const struct blk_crypto_key * key,struct blk_ksm_keyslot ** slot_ptr)233*4882a593Smuzhiyun blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm,
234*4882a593Smuzhiyun const struct blk_crypto_key *key,
235*4882a593Smuzhiyun struct blk_ksm_keyslot **slot_ptr)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun struct blk_ksm_keyslot *slot;
238*4882a593Smuzhiyun int slot_idx;
239*4882a593Smuzhiyun int err;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun *slot_ptr = NULL;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (blk_ksm_is_passthrough(ksm))
244*4882a593Smuzhiyun return BLK_STS_OK;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun down_read(&ksm->lock);
247*4882a593Smuzhiyun slot = blk_ksm_find_and_grab_keyslot(ksm, key);
248*4882a593Smuzhiyun up_read(&ksm->lock);
249*4882a593Smuzhiyun if (slot)
250*4882a593Smuzhiyun goto success;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun for (;;) {
253*4882a593Smuzhiyun blk_ksm_hw_enter(ksm);
254*4882a593Smuzhiyun slot = blk_ksm_find_and_grab_keyslot(ksm, key);
255*4882a593Smuzhiyun if (slot) {
256*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
257*4882a593Smuzhiyun goto success;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun * If we're here, that means there wasn't a slot that was
262*4882a593Smuzhiyun * already programmed with the key. So try to program it.
263*4882a593Smuzhiyun */
264*4882a593Smuzhiyun if (!list_empty(&ksm->idle_slots))
265*4882a593Smuzhiyun break;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
268*4882a593Smuzhiyun wait_event(ksm->idle_slots_wait_queue,
269*4882a593Smuzhiyun !list_empty(&ksm->idle_slots));
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun slot = list_first_entry(&ksm->idle_slots, struct blk_ksm_keyslot,
273*4882a593Smuzhiyun idle_slot_node);
274*4882a593Smuzhiyun slot_idx = blk_ksm_get_slot_idx(slot);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot_idx);
277*4882a593Smuzhiyun if (err) {
278*4882a593Smuzhiyun wake_up(&ksm->idle_slots_wait_queue);
279*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
280*4882a593Smuzhiyun return errno_to_blk_status(err);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /* Move this slot to the hash list for the new key. */
284*4882a593Smuzhiyun if (slot->key)
285*4882a593Smuzhiyun hlist_del(&slot->hash_node);
286*4882a593Smuzhiyun slot->key = key;
287*4882a593Smuzhiyun hlist_add_head(&slot->hash_node, blk_ksm_hash_bucket_for_key(ksm, key));
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun atomic_set(&slot->slot_refs, 1);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun blk_ksm_remove_slot_from_lru_list(slot);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
294*4882a593Smuzhiyun success:
295*4882a593Smuzhiyun *slot_ptr = slot;
296*4882a593Smuzhiyun return BLK_STS_OK;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun * blk_ksm_put_slot() - Release a reference to a slot
301*4882a593Smuzhiyun * @slot: The keyslot to release the reference of.
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * Context: Any context.
304*4882a593Smuzhiyun */
blk_ksm_put_slot(struct blk_ksm_keyslot * slot)305*4882a593Smuzhiyun void blk_ksm_put_slot(struct blk_ksm_keyslot *slot)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun struct blk_keyslot_manager *ksm;
308*4882a593Smuzhiyun unsigned long flags;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (!slot)
311*4882a593Smuzhiyun return;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun ksm = slot->ksm;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
316*4882a593Smuzhiyun &ksm->idle_slots_lock, flags)) {
317*4882a593Smuzhiyun list_add_tail(&slot->idle_slot_node, &ksm->idle_slots);
318*4882a593Smuzhiyun spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
319*4882a593Smuzhiyun wake_up(&ksm->idle_slots_wait_queue);
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /**
324*4882a593Smuzhiyun * blk_ksm_crypto_cfg_supported() - Find out if a crypto configuration is
325*4882a593Smuzhiyun * supported by a ksm.
326*4882a593Smuzhiyun * @ksm: The keyslot manager to check
327*4882a593Smuzhiyun * @cfg: The crypto configuration to check for.
328*4882a593Smuzhiyun *
329*4882a593Smuzhiyun * Checks for crypto_mode/data unit size/dun bytes support.
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * Return: Whether or not this ksm supports the specified crypto config.
332*4882a593Smuzhiyun */
blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager * ksm,const struct blk_crypto_config * cfg)333*4882a593Smuzhiyun bool blk_ksm_crypto_cfg_supported(struct blk_keyslot_manager *ksm,
334*4882a593Smuzhiyun const struct blk_crypto_config *cfg)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun if (!ksm)
337*4882a593Smuzhiyun return false;
338*4882a593Smuzhiyun if (!(ksm->crypto_modes_supported[cfg->crypto_mode] &
339*4882a593Smuzhiyun cfg->data_unit_size))
340*4882a593Smuzhiyun return false;
341*4882a593Smuzhiyun if (ksm->max_dun_bytes_supported < cfg->dun_bytes)
342*4882a593Smuzhiyun return false;
343*4882a593Smuzhiyun if (cfg->is_hw_wrapped) {
344*4882a593Smuzhiyun if (!(ksm->features & BLK_CRYPTO_FEATURE_WRAPPED_KEYS))
345*4882a593Smuzhiyun return false;
346*4882a593Smuzhiyun } else {
347*4882a593Smuzhiyun if (!(ksm->features & BLK_CRYPTO_FEATURE_STANDARD_KEYS))
348*4882a593Smuzhiyun return false;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun return true;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /**
354*4882a593Smuzhiyun * blk_ksm_evict_key() - Evict a key from the lower layer device.
355*4882a593Smuzhiyun * @ksm: The keyslot manager to evict from
356*4882a593Smuzhiyun * @key: The key to evict
357*4882a593Smuzhiyun *
358*4882a593Smuzhiyun * Find the keyslot that the specified key was programmed into, and evict that
359*4882a593Smuzhiyun * slot from the lower layer device. The slot must not be in use by any
360*4882a593Smuzhiyun * in-flight IO when this function is called.
361*4882a593Smuzhiyun *
362*4882a593Smuzhiyun * Context: Process context. Takes and releases ksm->lock.
363*4882a593Smuzhiyun * Return: 0 on success or if there's no keyslot with the specified key, -EBUSY
364*4882a593Smuzhiyun * if the keyslot is still in use, or another -errno value on other
365*4882a593Smuzhiyun * error.
366*4882a593Smuzhiyun */
blk_ksm_evict_key(struct blk_keyslot_manager * ksm,const struct blk_crypto_key * key)367*4882a593Smuzhiyun int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
368*4882a593Smuzhiyun const struct blk_crypto_key *key)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun struct blk_ksm_keyslot *slot;
371*4882a593Smuzhiyun int err = 0;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun if (blk_ksm_is_passthrough(ksm)) {
374*4882a593Smuzhiyun if (ksm->ksm_ll_ops.keyslot_evict) {
375*4882a593Smuzhiyun blk_ksm_hw_enter(ksm);
376*4882a593Smuzhiyun err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1);
377*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
378*4882a593Smuzhiyun return err;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun return 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun blk_ksm_hw_enter(ksm);
384*4882a593Smuzhiyun slot = blk_ksm_find_keyslot(ksm, key);
385*4882a593Smuzhiyun if (!slot)
386*4882a593Smuzhiyun goto out_unlock;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
389*4882a593Smuzhiyun err = -EBUSY;
390*4882a593Smuzhiyun goto out_unlock;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun err = ksm->ksm_ll_ops.keyslot_evict(ksm, key,
393*4882a593Smuzhiyun blk_ksm_get_slot_idx(slot));
394*4882a593Smuzhiyun if (err)
395*4882a593Smuzhiyun goto out_unlock;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun hlist_del(&slot->hash_node);
398*4882a593Smuzhiyun slot->key = NULL;
399*4882a593Smuzhiyun err = 0;
400*4882a593Smuzhiyun out_unlock:
401*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
402*4882a593Smuzhiyun return err;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /**
406*4882a593Smuzhiyun * blk_ksm_reprogram_all_keys() - Re-program all keyslots.
407*4882a593Smuzhiyun * @ksm: The keyslot manager
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * Re-program all keyslots that are supposed to have a key programmed. This is
410*4882a593Smuzhiyun * intended only for use by drivers for hardware that loses its keys on reset.
411*4882a593Smuzhiyun *
412*4882a593Smuzhiyun * Context: Process context. Takes and releases ksm->lock.
413*4882a593Smuzhiyun */
blk_ksm_reprogram_all_keys(struct blk_keyslot_manager * ksm)414*4882a593Smuzhiyun void blk_ksm_reprogram_all_keys(struct blk_keyslot_manager *ksm)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun unsigned int slot;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (blk_ksm_is_passthrough(ksm))
419*4882a593Smuzhiyun return;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* This is for device initialization, so don't resume the device */
422*4882a593Smuzhiyun down_write(&ksm->lock);
423*4882a593Smuzhiyun for (slot = 0; slot < ksm->num_slots; slot++) {
424*4882a593Smuzhiyun const struct blk_crypto_key *key = ksm->slots[slot].key;
425*4882a593Smuzhiyun int err;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (!key)
428*4882a593Smuzhiyun continue;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
431*4882a593Smuzhiyun WARN_ON(err);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun up_write(&ksm->lock);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_reprogram_all_keys);
436*4882a593Smuzhiyun
blk_ksm_destroy(struct blk_keyslot_manager * ksm)437*4882a593Smuzhiyun void blk_ksm_destroy(struct blk_keyslot_manager *ksm)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun if (!ksm)
440*4882a593Smuzhiyun return;
441*4882a593Smuzhiyun kvfree(ksm->slot_hashtable);
442*4882a593Smuzhiyun kvfree_sensitive(ksm->slots, sizeof(ksm->slots[0]) * ksm->num_slots);
443*4882a593Smuzhiyun memzero_explicit(ksm, sizeof(*ksm));
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_destroy);
446*4882a593Smuzhiyun
blk_ksm_register(struct blk_keyslot_manager * ksm,struct request_queue * q)447*4882a593Smuzhiyun bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun if (blk_integrity_queue_supports_integrity(q)) {
450*4882a593Smuzhiyun pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
451*4882a593Smuzhiyun return false;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun q->ksm = ksm;
454*4882a593Smuzhiyun return true;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_register);
457*4882a593Smuzhiyun
blk_ksm_unregister(struct request_queue * q)458*4882a593Smuzhiyun void blk_ksm_unregister(struct request_queue *q)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun q->ksm = NULL;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /**
464*4882a593Smuzhiyun * blk_ksm_derive_raw_secret() - Derive software secret from wrapped key
465*4882a593Smuzhiyun * @ksm: The keyslot manager
466*4882a593Smuzhiyun * @wrapped_key: The wrapped key
467*4882a593Smuzhiyun * @wrapped_key_size: Size of the wrapped key in bytes
468*4882a593Smuzhiyun * @secret: (output) the software secret
469*4882a593Smuzhiyun * @secret_size: (output) the number of secret bytes to derive
470*4882a593Smuzhiyun *
471*4882a593Smuzhiyun * Given a hardware-wrapped key, ask the hardware to derive a secret which
472*4882a593Smuzhiyun * software can use for cryptographic tasks other than inline encryption. The
473*4882a593Smuzhiyun * derived secret is guaranteed to be cryptographically isolated from the key
474*4882a593Smuzhiyun * with which any inline encryption with this wrapped key would actually be
475*4882a593Smuzhiyun * done. I.e., both will be derived from the unwrapped key.
476*4882a593Smuzhiyun *
477*4882a593Smuzhiyun * Return: 0 on success, -EOPNOTSUPP if hardware-wrapped keys are unsupported,
478*4882a593Smuzhiyun * or another -errno code.
479*4882a593Smuzhiyun */
blk_ksm_derive_raw_secret(struct blk_keyslot_manager * ksm,const u8 * wrapped_key,unsigned int wrapped_key_size,u8 * secret,unsigned int secret_size)480*4882a593Smuzhiyun int blk_ksm_derive_raw_secret(struct blk_keyslot_manager *ksm,
481*4882a593Smuzhiyun const u8 *wrapped_key,
482*4882a593Smuzhiyun unsigned int wrapped_key_size,
483*4882a593Smuzhiyun u8 *secret, unsigned int secret_size)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun int err;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun if (ksm->ksm_ll_ops.derive_raw_secret) {
488*4882a593Smuzhiyun blk_ksm_hw_enter(ksm);
489*4882a593Smuzhiyun err = ksm->ksm_ll_ops.derive_raw_secret(ksm, wrapped_key,
490*4882a593Smuzhiyun wrapped_key_size,
491*4882a593Smuzhiyun secret, secret_size);
492*4882a593Smuzhiyun blk_ksm_hw_exit(ksm);
493*4882a593Smuzhiyun } else {
494*4882a593Smuzhiyun err = -EOPNOTSUPP;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun return err;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_derive_raw_secret);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /**
502*4882a593Smuzhiyun * blk_ksm_intersect_modes() - restrict supported modes by child device
503*4882a593Smuzhiyun * @parent: The keyslot manager for parent device
504*4882a593Smuzhiyun * @child: The keyslot manager for child device, or NULL
505*4882a593Smuzhiyun *
506*4882a593Smuzhiyun * Clear any crypto mode support bits in @parent that aren't set in @child.
507*4882a593Smuzhiyun * If @child is NULL, then all parent bits are cleared.
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * Only use this when setting up the keyslot manager for a layered device,
510*4882a593Smuzhiyun * before it's been exposed yet.
511*4882a593Smuzhiyun */
blk_ksm_intersect_modes(struct blk_keyslot_manager * parent,const struct blk_keyslot_manager * child)512*4882a593Smuzhiyun void blk_ksm_intersect_modes(struct blk_keyslot_manager *parent,
513*4882a593Smuzhiyun const struct blk_keyslot_manager *child)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun if (child) {
516*4882a593Smuzhiyun unsigned int i;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun parent->max_dun_bytes_supported =
519*4882a593Smuzhiyun min(parent->max_dun_bytes_supported,
520*4882a593Smuzhiyun child->max_dun_bytes_supported);
521*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(child->crypto_modes_supported);
522*4882a593Smuzhiyun i++) {
523*4882a593Smuzhiyun parent->crypto_modes_supported[i] &=
524*4882a593Smuzhiyun child->crypto_modes_supported[i];
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun parent->features &= child->features;
527*4882a593Smuzhiyun } else {
528*4882a593Smuzhiyun parent->max_dun_bytes_supported = 0;
529*4882a593Smuzhiyun memset(parent->crypto_modes_supported, 0,
530*4882a593Smuzhiyun sizeof(parent->crypto_modes_supported));
531*4882a593Smuzhiyun parent->features = 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_intersect_modes);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /**
537*4882a593Smuzhiyun * blk_ksm_is_superset() - Check if a KSM supports a superset of crypto modes
538*4882a593Smuzhiyun * and DUN bytes that another KSM supports. Here,
539*4882a593Smuzhiyun * "superset" refers to the mathematical meaning of the
540*4882a593Smuzhiyun * word - i.e. if two KSMs have the *same* capabilities,
541*4882a593Smuzhiyun * they *are* considered supersets of each other.
542*4882a593Smuzhiyun * @ksm_superset: The KSM that we want to verify is a superset
543*4882a593Smuzhiyun * @ksm_subset: The KSM that we want to verify is a subset
544*4882a593Smuzhiyun *
545*4882a593Smuzhiyun * Return: True if @ksm_superset supports a superset of the crypto modes and DUN
546*4882a593Smuzhiyun * bytes that @ksm_subset supports.
547*4882a593Smuzhiyun */
blk_ksm_is_superset(struct blk_keyslot_manager * ksm_superset,struct blk_keyslot_manager * ksm_subset)548*4882a593Smuzhiyun bool blk_ksm_is_superset(struct blk_keyslot_manager *ksm_superset,
549*4882a593Smuzhiyun struct blk_keyslot_manager *ksm_subset)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun int i;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun if (!ksm_subset)
554*4882a593Smuzhiyun return true;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun if (!ksm_superset)
557*4882a593Smuzhiyun return false;
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ksm_superset->crypto_modes_supported); i++) {
560*4882a593Smuzhiyun if (ksm_subset->crypto_modes_supported[i] &
561*4882a593Smuzhiyun (~ksm_superset->crypto_modes_supported[i])) {
562*4882a593Smuzhiyun return false;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun if (ksm_subset->max_dun_bytes_supported >
567*4882a593Smuzhiyun ksm_superset->max_dun_bytes_supported) {
568*4882a593Smuzhiyun return false;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun if (ksm_subset->features & ~ksm_superset->features)
572*4882a593Smuzhiyun return false;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun return true;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_is_superset);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun * blk_ksm_update_capabilities() - Update the restrictions of a KSM to those of
580*4882a593Smuzhiyun * another KSM
581*4882a593Smuzhiyun * @target_ksm: The KSM whose restrictions to update.
582*4882a593Smuzhiyun * @reference_ksm: The KSM to whose restrictions this function will update
583*4882a593Smuzhiyun * @target_ksm's restrictions to.
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * Blk-crypto requires that crypto capabilities that were
586*4882a593Smuzhiyun * advertised when a bio was created continue to be supported by the
587*4882a593Smuzhiyun * device until that bio is ended. This is turn means that a device cannot
588*4882a593Smuzhiyun * shrink its advertised crypto capabilities without any explicit
589*4882a593Smuzhiyun * synchronization with upper layers. So if there's no such explicit
590*4882a593Smuzhiyun * synchronization, @reference_ksm must support all the crypto capabilities that
591*4882a593Smuzhiyun * @target_ksm does
592*4882a593Smuzhiyun * (i.e. we need blk_ksm_is_superset(@reference_ksm, @target_ksm) == true).
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Note also that as long as the crypto capabilities are being expanded, the
595*4882a593Smuzhiyun * order of updates becoming visible is not important because it's alright
596*4882a593Smuzhiyun * for blk-crypto to see stale values - they only cause blk-crypto to
597*4882a593Smuzhiyun * believe that a crypto capability isn't supported when it actually is (which
598*4882a593Smuzhiyun * might result in blk-crypto-fallback being used if available, or the bio being
599*4882a593Smuzhiyun * failed).
600*4882a593Smuzhiyun */
blk_ksm_update_capabilities(struct blk_keyslot_manager * target_ksm,struct blk_keyslot_manager * reference_ksm)601*4882a593Smuzhiyun void blk_ksm_update_capabilities(struct blk_keyslot_manager *target_ksm,
602*4882a593Smuzhiyun struct blk_keyslot_manager *reference_ksm)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun memcpy(target_ksm->crypto_modes_supported,
605*4882a593Smuzhiyun reference_ksm->crypto_modes_supported,
606*4882a593Smuzhiyun sizeof(target_ksm->crypto_modes_supported));
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun target_ksm->max_dun_bytes_supported =
609*4882a593Smuzhiyun reference_ksm->max_dun_bytes_supported;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun target_ksm->features = reference_ksm->features;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_update_capabilities);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /**
616*4882a593Smuzhiyun * blk_ksm_init_passthrough() - Init a passthrough keyslot manager
617*4882a593Smuzhiyun * @ksm: The keyslot manager to init
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun * Initialize a passthrough keyslot manager.
620*4882a593Smuzhiyun * Called by e.g. storage drivers to set up a keyslot manager in their
621*4882a593Smuzhiyun * request_queue, when the storage driver wants to manage its keys by itself.
622*4882a593Smuzhiyun * This is useful for inline encryption hardware that doesn't have the concept
623*4882a593Smuzhiyun * of keyslots, and for layered devices.
624*4882a593Smuzhiyun */
blk_ksm_init_passthrough(struct blk_keyslot_manager * ksm)625*4882a593Smuzhiyun void blk_ksm_init_passthrough(struct blk_keyslot_manager *ksm)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun memset(ksm, 0, sizeof(*ksm));
628*4882a593Smuzhiyun init_rwsem(&ksm->lock);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blk_ksm_init_passthrough);
631