Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

blk-crypto: show supported key types in sysfs

Add sysfs files that indicate which type(s) of keys are supported by the
inline encryption hardware associated with a particular request queue:

/sys/block/$disk/queue/crypto/hw_wrapped_keys
/sys/block/$disk/queue/crypto/raw_keys

Userspace can use the presence or absence of these files to decide what
encyption settings to use.

Don't use a single key_type file, as devices might support both key
types at the same time.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Tested-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> # sm8650
Link: https://lore.kernel.org/r/20250204060041.409950-3-ebiggers@kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Eric Biggers and committed by
Jens Axboe
e35fde43 ebc41765

+55
+20
Documentation/ABI/stable/sysfs-block
··· 229 229 encryption, refer to Documentation/block/inline-encryption.rst. 230 230 231 231 232 + What: /sys/block/<disk>/queue/crypto/hw_wrapped_keys 233 + Date: February 2025 234 + Contact: linux-block@vger.kernel.org 235 + Description: 236 + [RO] The presence of this file indicates that the device 237 + supports hardware-wrapped inline encryption keys, i.e. key blobs 238 + that can only be unwrapped and used by dedicated hardware. For 239 + more information about hardware-wrapped inline encryption keys, 240 + see Documentation/block/inline-encryption.rst. 241 + 242 + 232 243 What: /sys/block/<disk>/queue/crypto/max_dun_bits 233 244 Date: February 2022 234 245 Contact: linux-block@vger.kernel.org ··· 276 265 Description: 277 266 [RO] This file shows the number of keyslots the device has for 278 267 use with inline encryption. 268 + 269 + 270 + What: /sys/block/<disk>/queue/crypto/raw_keys 271 + Date: February 2025 272 + Contact: linux-block@vger.kernel.org 273 + Description: 274 + [RO] The presence of this file indicates that the device 275 + supports raw inline encryption keys, i.e. keys that are managed 276 + in raw, plaintext form in software. 279 277 280 278 281 279 What: /sys/block/<disk>/queue/dax
+35
block/blk-crypto-sysfs.c
··· 31 31 return container_of(attr, struct blk_crypto_attr, attr); 32 32 } 33 33 34 + static ssize_t hw_wrapped_keys_show(struct blk_crypto_profile *profile, 35 + struct blk_crypto_attr *attr, char *page) 36 + { 37 + /* Always show supported, since the file doesn't exist otherwise. */ 38 + return sysfs_emit(page, "supported\n"); 39 + } 40 + 34 41 static ssize_t max_dun_bits_show(struct blk_crypto_profile *profile, 35 42 struct blk_crypto_attr *attr, char *page) 36 43 { ··· 50 43 return sysfs_emit(page, "%u\n", profile->num_slots); 51 44 } 52 45 46 + static ssize_t raw_keys_show(struct blk_crypto_profile *profile, 47 + struct blk_crypto_attr *attr, char *page) 48 + { 49 + /* Always show supported, since the file doesn't exist otherwise. */ 50 + return sysfs_emit(page, "supported\n"); 51 + } 52 + 53 53 #define BLK_CRYPTO_RO_ATTR(_name) \ 54 54 static struct blk_crypto_attr _name##_attr = __ATTR_RO(_name) 55 55 56 + BLK_CRYPTO_RO_ATTR(hw_wrapped_keys); 56 57 BLK_CRYPTO_RO_ATTR(max_dun_bits); 57 58 BLK_CRYPTO_RO_ATTR(num_keyslots); 59 + BLK_CRYPTO_RO_ATTR(raw_keys); 60 + 61 + static umode_t blk_crypto_is_visible(struct kobject *kobj, 62 + struct attribute *attr, int n) 63 + { 64 + struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj); 65 + struct blk_crypto_attr *a = attr_to_crypto_attr(attr); 66 + 67 + if (a == &hw_wrapped_keys_attr && 68 + !(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED)) 69 + return 0; 70 + if (a == &raw_keys_attr && 71 + !(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_RAW)) 72 + return 0; 73 + 74 + return 0444; 75 + } 58 76 59 77 static struct attribute *blk_crypto_attrs[] = { 78 + &hw_wrapped_keys_attr.attr, 60 79 &max_dun_bits_attr.attr, 61 80 &num_keyslots_attr.attr, 81 + &raw_keys_attr.attr, 62 82 NULL, 63 83 }; 64 84 65 85 static const struct attribute_group blk_crypto_attr_group = { 66 86 .attrs = blk_crypto_attrs, 87 + .is_visible = blk_crypto_is_visible, 67 88 }; 68 89 69 90 /*