Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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