Skip to content

Commit 20f01f1

Browse files
ebiggersaxboe
authored andcommitted
blk-crypto: show crypto capabilities in sysfs
Add sysfs files that expose the inline encryption capabilities of request queues: /sys/block/$disk/queue/crypto/max_dun_bits /sys/block/$disk/queue/crypto/modes/$mode /sys/block/$disk/queue/crypto/num_keyslots Userspace can use these new files to decide what encryption settings to use, or whether to use inline encryption at all. This also brings the crypto capabilities in line with the other queue properties, which are already discoverable via the queue directory in sysfs. Design notes: - Place the new files in a new subdirectory "crypto" to group them together and to avoid complicating the main "queue" directory. This also makes it possible to replace "crypto" with a symlink later if we ever make the blk_crypto_profiles into real kobjects (see below). - It was necessary to define a new kobject that corresponds to the crypto subdirectory. For now, this kobject just contains a pointer to the blk_crypto_profile. Note that multiple queues (and hence multiple such kobjects) may refer to the same blk_crypto_profile. An alternative design would more closely match the current kernel data structures: the blk_crypto_profile could be a kobject itself, located directly under the host controller device's kobject, while /sys/block/$disk/queue/crypto would be a symlink to it. I decided not to do that for now because it would require a lot more changes, such as no longer embedding blk_crypto_profile in other structures, and also because I'm not sure we can rule out moving the crypto capabilities into 'struct queue_limits' in the future. (Even if multiple queues share the same crypto engine, maybe the supported data unit sizes could differ due to other queue properties.) It would also still be possible to switch to that design later without breaking userspace, by replacing the directory with a symlink. - Use "max_dun_bits" instead of "max_dun_bytes". Currently, the kernel internally stores this value in bytes, but that's an implementation detail. It probably makes more sense to talk about this value in bits, and choosing bits is more future-proof. - "modes" is a sub-subdirectory, since there may be multiple supported crypto modes, sysfs is supposed to have one value per file, and it makes sense to group all the mode files together. - Each mode had to be named. The crypto API names like "xts(aes)" are not appropriate because they don't specify the key size. Therefore, I assigned new names. The exact names chosen are arbitrary, but they happen to match the names used in log messages in fs/crypto/. - The "num_keyslots" file is a bit different from the others in that it is only useful to know for performance reasons. However, it's included as it can still be useful. For example, a user might not want to use inline encryption if there aren't very many keyslots. Reviewed-by: Hannes Reinecke <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 0f69288 commit 20f01f1

File tree

7 files changed

+245
-1
lines changed

7 files changed

+245
-1
lines changed

Documentation/ABI/stable/sysfs-block

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,55 @@ Description:
155155
last zone of the device which may be smaller.
156156

157157

158+
What: /sys/block/<disk>/queue/crypto/
159+
Date: February 2022
160+
161+
Description:
162+
The presence of this subdirectory of /sys/block/<disk>/queue/
163+
indicates that the device supports inline encryption. This
164+
subdirectory contains files which describe the inline encryption
165+
capabilities of the device. For more information about inline
166+
encryption, refer to Documentation/block/inline-encryption.rst.
167+
168+
169+
What: /sys/block/<disk>/queue/crypto/max_dun_bits
170+
Date: February 2022
171+
172+
Description:
173+
[RO] This file shows the maximum length, in bits, of data unit
174+
numbers accepted by the device in inline encryption requests.
175+
176+
177+
What: /sys/block/<disk>/queue/crypto/modes/<mode>
178+
Date: February 2022
179+
180+
Description:
181+
[RO] For each crypto mode (i.e., encryption/decryption
182+
algorithm) the device supports with inline encryption, a file
183+
will exist at this location. It will contain a hexadecimal
184+
number that is a bitmask of the supported data unit sizes, in
185+
bytes, for that crypto mode.
186+
187+
Currently, the crypto modes that may be supported are:
188+
189+
* AES-256-XTS
190+
* AES-128-CBC-ESSIV
191+
* Adiantum
192+
193+
For example, if a device supports AES-256-XTS inline encryption
194+
with data unit sizes of 512 and 4096 bytes, the file
195+
/sys/block/<disk>/queue/crypto/modes/AES-256-XTS will exist and
196+
will contain "0x1200".
197+
198+
199+
What: /sys/block/<disk>/queue/crypto/num_keyslots
200+
Date: February 2022
201+
202+
Description:
203+
[RO] This file shows the number of keyslots the device has for
204+
use with inline encryption.
205+
206+
158207
What: /sys/block/<disk>/queue/dax
159208
Date: June 2016
160209

block/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ obj-$(CONFIG_BLK_DEBUG_FS) += blk-mq-debugfs.o
3636
obj-$(CONFIG_BLK_DEBUG_FS_ZONED)+= blk-mq-debugfs-zoned.o
3737
obj-$(CONFIG_BLK_SED_OPAL) += sed-opal.o
3838
obj-$(CONFIG_BLK_PM) += blk-pm.o
39-
obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o
39+
obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \
40+
blk-crypto-sysfs.o
4041
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
4142
obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o

block/blk-crypto-internal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/* Represents a crypto mode supported by blk-crypto */
1313
struct blk_crypto_mode {
14+
const char *name; /* name of this mode, shown in sysfs */
1415
const char *cipher_str; /* crypto API name (for fallback case) */
1516
unsigned int keysize; /* key size in bytes */
1617
unsigned int ivsize; /* iv size in bytes */
@@ -20,6 +21,10 @@ extern const struct blk_crypto_mode blk_crypto_modes[];
2021

2122
#ifdef CONFIG_BLK_INLINE_ENCRYPTION
2223

24+
int blk_crypto_sysfs_register(struct request_queue *q);
25+
26+
void blk_crypto_sysfs_unregister(struct request_queue *q);
27+
2328
void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
2429
unsigned int inc);
2530

@@ -62,6 +67,13 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
6267

6368
#else /* CONFIG_BLK_INLINE_ENCRYPTION */
6469

70+
static inline int blk_crypto_sysfs_register(struct request_queue *q)
71+
{
72+
return 0;
73+
}
74+
75+
static inline void blk_crypto_sysfs_unregister(struct request_queue *q) { }
76+
6577
static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
6678
struct bio *bio)
6779
{

block/blk-crypto-sysfs.c

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright 2021 Google LLC
4+
*
5+
* sysfs support for blk-crypto. This file contains the code which exports the
6+
* crypto capabilities of devices via /sys/block/$disk/queue/crypto/.
7+
*/
8+
9+
#include <linux/blk-crypto-profile.h>
10+
11+
#include "blk-crypto-internal.h"
12+
13+
struct blk_crypto_kobj {
14+
struct kobject kobj;
15+
struct blk_crypto_profile *profile;
16+
};
17+
18+
struct blk_crypto_attr {
19+
struct attribute attr;
20+
ssize_t (*show)(struct blk_crypto_profile *profile,
21+
struct blk_crypto_attr *attr, char *page);
22+
};
23+
24+
static struct blk_crypto_profile *kobj_to_crypto_profile(struct kobject *kobj)
25+
{
26+
return container_of(kobj, struct blk_crypto_kobj, kobj)->profile;
27+
}
28+
29+
static struct blk_crypto_attr *attr_to_crypto_attr(struct attribute *attr)
30+
{
31+
return container_of(attr, struct blk_crypto_attr, attr);
32+
}
33+
34+
static ssize_t max_dun_bits_show(struct blk_crypto_profile *profile,
35+
struct blk_crypto_attr *attr, char *page)
36+
{
37+
return sysfs_emit(page, "%u\n", 8 * profile->max_dun_bytes_supported);
38+
}
39+
40+
static ssize_t num_keyslots_show(struct blk_crypto_profile *profile,
41+
struct blk_crypto_attr *attr, char *page)
42+
{
43+
return sysfs_emit(page, "%u\n", profile->num_slots);
44+
}
45+
46+
#define BLK_CRYPTO_RO_ATTR(_name) \
47+
static struct blk_crypto_attr _name##_attr = __ATTR_RO(_name)
48+
49+
BLK_CRYPTO_RO_ATTR(max_dun_bits);
50+
BLK_CRYPTO_RO_ATTR(num_keyslots);
51+
52+
static struct attribute *blk_crypto_attrs[] = {
53+
&max_dun_bits_attr.attr,
54+
&num_keyslots_attr.attr,
55+
NULL,
56+
};
57+
58+
static const struct attribute_group blk_crypto_attr_group = {
59+
.attrs = blk_crypto_attrs,
60+
};
61+
62+
/*
63+
* The encryption mode attributes. To avoid hard-coding the list of encryption
64+
* modes, these are initialized at boot time by blk_crypto_sysfs_init().
65+
*/
66+
static struct blk_crypto_attr __blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX];
67+
static struct attribute *blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX + 1];
68+
69+
static umode_t blk_crypto_mode_is_visible(struct kobject *kobj,
70+
struct attribute *attr, int n)
71+
{
72+
struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
73+
struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
74+
int mode_num = a - __blk_crypto_mode_attrs;
75+
76+
if (profile->modes_supported[mode_num])
77+
return 0444;
78+
return 0;
79+
}
80+
81+
static ssize_t blk_crypto_mode_show(struct blk_crypto_profile *profile,
82+
struct blk_crypto_attr *attr, char *page)
83+
{
84+
int mode_num = attr - __blk_crypto_mode_attrs;
85+
86+
return sysfs_emit(page, "0x%x\n", profile->modes_supported[mode_num]);
87+
}
88+
89+
static const struct attribute_group blk_crypto_modes_attr_group = {
90+
.name = "modes",
91+
.attrs = blk_crypto_mode_attrs,
92+
.is_visible = blk_crypto_mode_is_visible,
93+
};
94+
95+
static const struct attribute_group *blk_crypto_attr_groups[] = {
96+
&blk_crypto_attr_group,
97+
&blk_crypto_modes_attr_group,
98+
NULL,
99+
};
100+
101+
static ssize_t blk_crypto_attr_show(struct kobject *kobj,
102+
struct attribute *attr, char *page)
103+
{
104+
struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
105+
struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
106+
107+
return a->show(profile, a, page);
108+
}
109+
110+
static const struct sysfs_ops blk_crypto_attr_ops = {
111+
.show = blk_crypto_attr_show,
112+
};
113+
114+
static void blk_crypto_release(struct kobject *kobj)
115+
{
116+
kfree(container_of(kobj, struct blk_crypto_kobj, kobj));
117+
}
118+
119+
static struct kobj_type blk_crypto_ktype = {
120+
.default_groups = blk_crypto_attr_groups,
121+
.sysfs_ops = &blk_crypto_attr_ops,
122+
.release = blk_crypto_release,
123+
};
124+
125+
/*
126+
* If the request_queue has a blk_crypto_profile, create the "crypto"
127+
* subdirectory in sysfs (/sys/block/$disk/queue/crypto/).
128+
*/
129+
int blk_crypto_sysfs_register(struct request_queue *q)
130+
{
131+
struct blk_crypto_kobj *obj;
132+
int err;
133+
134+
if (!q->crypto_profile)
135+
return 0;
136+
137+
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
138+
if (!obj)
139+
return -ENOMEM;
140+
obj->profile = q->crypto_profile;
141+
142+
err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype, &q->kobj,
143+
"crypto");
144+
if (err) {
145+
kobject_put(&obj->kobj);
146+
return err;
147+
}
148+
q->crypto_kobject = &obj->kobj;
149+
return 0;
150+
}
151+
152+
void blk_crypto_sysfs_unregister(struct request_queue *q)
153+
{
154+
kobject_put(q->crypto_kobject);
155+
}
156+
157+
static int __init blk_crypto_sysfs_init(void)
158+
{
159+
int i;
160+
161+
BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
162+
for (i = 1; i < BLK_ENCRYPTION_MODE_MAX; i++) {
163+
struct blk_crypto_attr *attr = &__blk_crypto_mode_attrs[i];
164+
165+
attr->attr.name = blk_crypto_modes[i].name;
166+
attr->attr.mode = 0444;
167+
attr->show = blk_crypto_mode_show;
168+
blk_crypto_mode_attrs[i - 1] = &attr->attr;
169+
}
170+
return 0;
171+
}
172+
subsys_initcall(blk_crypto_sysfs_init);

block/blk-crypto.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919

2020
const struct blk_crypto_mode blk_crypto_modes[] = {
2121
[BLK_ENCRYPTION_MODE_AES_256_XTS] = {
22+
.name = "AES-256-XTS",
2223
.cipher_str = "xts(aes)",
2324
.keysize = 64,
2425
.ivsize = 16,
2526
},
2627
[BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV] = {
28+
.name = "AES-128-CBC-ESSIV",
2729
.cipher_str = "essiv(cbc(aes),sha256)",
2830
.keysize = 16,
2931
.ivsize = 16,
3032
},
3133
[BLK_ENCRYPTION_MODE_ADIANTUM] = {
34+
.name = "Adiantum",
3235
.cipher_str = "adiantum(xchacha12,aes)",
3336
.keysize = 32,
3437
.ivsize = 32,

block/blk-sysfs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,10 @@ int blk_register_queue(struct gendisk *disk)
880880
goto put_dev;
881881
}
882882

883+
ret = blk_crypto_sysfs_register(q);
884+
if (ret)
885+
goto put_dev;
886+
883887
blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
884888
wbt_enable_default(q);
885889
blk_throtl_register_queue(q);
@@ -910,6 +914,7 @@ int blk_register_queue(struct gendisk *disk)
910914
return ret;
911915

912916
put_dev:
917+
elv_unregister_queue(q);
913918
disk_unregister_independent_access_ranges(disk);
914919
mutex_unlock(&q->sysfs_lock);
915920
mutex_unlock(&q->sysfs_dir_lock);
@@ -954,6 +959,7 @@ void blk_unregister_queue(struct gendisk *disk)
954959
*/
955960
if (queue_is_mq(q))
956961
blk_mq_unregister_dev(disk_to_dev(disk), q);
962+
blk_crypto_sysfs_unregister(q);
957963
blk_trace_remove_sysfs(disk_to_dev(disk));
958964

959965
mutex_lock(&q->sysfs_lock);

include/linux/blkdev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ struct request_queue {
413413

414414
#ifdef CONFIG_BLK_INLINE_ENCRYPTION
415415
struct blk_crypto_profile *crypto_profile;
416+
struct kobject *crypto_kobject;
416417
#endif
417418

418419
unsigned int rq_timeout;

0 commit comments

Comments
 (0)