Skip to content

Commit 5851d3b

Browse files
ebiggersstorulf
authored andcommitted
block/keyslot-manager: introduce devm_blk_ksm_init()
Add a resource-managed variant of blk_ksm_init() so that drivers don't have to worry about calling blk_ksm_destroy(). Note that the implementation uses a custom devres action to call blk_ksm_destroy() rather than switching the two allocations to be directly devres-managed, e.g. with devm_kmalloc(). This is because we need to keep zeroing the memory containing the keyslots when it is freed, and also because we want to continue using kvmalloc() (and there is no devm_kvmalloc()). Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Satya Tangirala <[email protected]> Acked-by: Jens Axboe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ulf Hansson <[email protected]>
1 parent 4af307f commit 5851d3b

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Documentation/block/inline-encryption.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ API presented to device drivers
182182

183183
A :c:type:``struct blk_keyslot_manager`` should be set up by device drivers in
184184
the ``request_queue`` of the device. The device driver needs to call
185-
``blk_ksm_init`` on the ``blk_keyslot_manager``, which specifying the number of
186-
keyslots supported by the hardware.
185+
``blk_ksm_init`` (or its resource-managed variant ``devm_blk_ksm_init``) on the
186+
``blk_keyslot_manager``, while specifying the number of keyslots supported by
187+
the hardware.
187188

188189
The device driver also needs to tell the KSM how to actually manipulate the
189190
IE hardware in the device to do things like programming the crypto key into
@@ -202,10 +203,9 @@ needs each and every of its keyslots to be reprogrammed with the key it
202203
"should have" at the point in time when the function is called. This is useful
203204
e.g. if a device loses all its keys on runtime power down/up.
204205

205-
``blk_ksm_destroy`` should be called to free up all resources used by a keyslot
206-
manager upon ``blk_ksm_init``, once the ``blk_keyslot_manager`` is no longer
207-
needed.
208-
206+
If the driver used ``blk_ksm_init`` instead of ``devm_blk_ksm_init``, then
207+
``blk_ksm_destroy`` should be called to free up all resources used by a
208+
``blk_keyslot_manager`` once it is no longer needed.
209209

210210
Layered Devices
211211
===============

block/keyslot-manager.c

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define pr_fmt(fmt) "blk-crypto: " fmt
3030

3131
#include <linux/keyslot-manager.h>
32+
#include <linux/device.h>
3233
#include <linux/atomic.h>
3334
#include <linux/mutex.h>
3435
#include <linux/pm_runtime.h>
@@ -127,6 +128,34 @@ int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
127128
}
128129
EXPORT_SYMBOL_GPL(blk_ksm_init);
129130

131+
static void blk_ksm_destroy_callback(void *ksm)
132+
{
133+
blk_ksm_destroy(ksm);
134+
}
135+
136+
/**
137+
* devm_blk_ksm_init() - Resource-managed blk_ksm_init()
138+
* @dev: The device which owns the blk_keyslot_manager.
139+
* @ksm: The blk_keyslot_manager to initialize.
140+
* @num_slots: The number of key slots to manage.
141+
*
142+
* Like blk_ksm_init(), but causes blk_ksm_destroy() to be called automatically
143+
* on driver detach.
144+
*
145+
* Return: 0 on success, or else a negative error code.
146+
*/
147+
int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm,
148+
unsigned int num_slots)
149+
{
150+
int err = blk_ksm_init(ksm, num_slots);
151+
152+
if (err)
153+
return err;
154+
155+
return devm_add_action_or_reset(dev, blk_ksm_destroy_callback, ksm);
156+
}
157+
EXPORT_SYMBOL_GPL(devm_blk_ksm_init);
158+
130159
static inline struct hlist_head *
131160
blk_ksm_hash_bucket_for_key(struct blk_keyslot_manager *ksm,
132161
const struct blk_crypto_key *key)

include/linux/keyslot-manager.h

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ struct blk_keyslot_manager {
8585

8686
int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots);
8787

88+
int devm_blk_ksm_init(struct device *dev, struct blk_keyslot_manager *ksm,
89+
unsigned int num_slots);
90+
8891
blk_status_t blk_ksm_get_slot_for_key(struct blk_keyslot_manager *ksm,
8992
const struct blk_crypto_key *key,
9093
struct blk_ksm_keyslot **slot_ptr);

0 commit comments

Comments
 (0)