blk-crypto-profile.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. /**
  6. * DOC: blk-crypto profiles
  7. *
  8. * 'struct blk_crypto_profile' contains all generic inline encryption-related
  9. * state for a particular inline encryption device. blk_crypto_profile serves
  10. * as the way that drivers for inline encryption hardware expose their crypto
  11. * capabilities and certain functions (e.g., functions to program and evict
  12. * keys) to upper layers. Device drivers that want to support inline encryption
  13. * construct a crypto profile, then associate it with the disk's request_queue.
  14. *
  15. * If the device has keyslots, then its blk_crypto_profile also handles managing
  16. * these keyslots in a device-independent way, using the driver-provided
  17. * functions to program and evict keys as needed. This includes keeping track
  18. * of which key and how many I/O requests are using each keyslot, getting
  19. * keyslots for I/O requests, and handling key eviction requests.
  20. *
  21. * For more information, see Documentation/block/inline-encryption.rst.
  22. */
  23. #define pr_fmt(fmt) "blk-crypto: " fmt
  24. #include <linux/blk-crypto-profile.h>
  25. #include <linux/device.h>
  26. #include <linux/atomic.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/wait.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/blk-integrity.h>
  32. #include "blk-crypto-internal.h"
  33. struct blk_crypto_keyslot {
  34. atomic_t slot_refs;
  35. struct list_head idle_slot_node;
  36. struct hlist_node hash_node;
  37. const struct blk_crypto_key *key;
  38. struct blk_crypto_profile *profile;
  39. };
  40. static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
  41. {
  42. /*
  43. * Calling into the driver requires profile->lock held and the device
  44. * resumed. But we must resume the device first, since that can acquire
  45. * and release profile->lock via blk_crypto_reprogram_all_keys().
  46. */
  47. if (profile->dev)
  48. pm_runtime_get_sync(profile->dev);
  49. down_write(&profile->lock);
  50. }
  51. static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
  52. {
  53. up_write(&profile->lock);
  54. if (profile->dev)
  55. pm_runtime_put_sync(profile->dev);
  56. }
  57. /**
  58. * blk_crypto_profile_init() - Initialize a blk_crypto_profile
  59. * @profile: the blk_crypto_profile to initialize
  60. * @num_slots: the number of keyslots
  61. *
  62. * Storage drivers must call this when starting to set up a blk_crypto_profile,
  63. * before filling in additional fields.
  64. *
  65. * Return: 0 on success, or else a negative error code.
  66. */
  67. int blk_crypto_profile_init(struct blk_crypto_profile *profile,
  68. unsigned int num_slots)
  69. {
  70. unsigned int slot;
  71. unsigned int i;
  72. unsigned int slot_hashtable_size;
  73. memset(profile, 0, sizeof(*profile));
  74. /*
  75. * profile->lock of an underlying device can nest inside profile->lock
  76. * of a device-mapper device, so use a dynamic lock class to avoid
  77. * false-positive lockdep reports.
  78. */
  79. lockdep_register_key(&profile->lockdep_key);
  80. __init_rwsem(&profile->lock, "&profile->lock", &profile->lockdep_key);
  81. if (num_slots == 0)
  82. return 0;
  83. /* Initialize keyslot management data. */
  84. profile->slots = kvcalloc(num_slots, sizeof(profile->slots[0]),
  85. GFP_KERNEL);
  86. if (!profile->slots)
  87. goto err_destroy;
  88. profile->num_slots = num_slots;
  89. init_waitqueue_head(&profile->idle_slots_wait_queue);
  90. INIT_LIST_HEAD(&profile->idle_slots);
  91. for (slot = 0; slot < num_slots; slot++) {
  92. profile->slots[slot].profile = profile;
  93. list_add_tail(&profile->slots[slot].idle_slot_node,
  94. &profile->idle_slots);
  95. }
  96. spin_lock_init(&profile->idle_slots_lock);
  97. slot_hashtable_size = roundup_pow_of_two(num_slots);
  98. /*
  99. * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
  100. * buckets. This only makes a difference when there is only 1 keyslot.
  101. */
  102. if (slot_hashtable_size < 2)
  103. slot_hashtable_size = 2;
  104. profile->log_slot_ht_size = ilog2(slot_hashtable_size);
  105. profile->slot_hashtable =
  106. kvmalloc_array(slot_hashtable_size,
  107. sizeof(profile->slot_hashtable[0]), GFP_KERNEL);
  108. if (!profile->slot_hashtable)
  109. goto err_destroy;
  110. for (i = 0; i < slot_hashtable_size; i++)
  111. INIT_HLIST_HEAD(&profile->slot_hashtable[i]);
  112. return 0;
  113. err_destroy:
  114. blk_crypto_profile_destroy(profile);
  115. return -ENOMEM;
  116. }
  117. EXPORT_SYMBOL_GPL(blk_crypto_profile_init);
  118. static void blk_crypto_profile_destroy_callback(void *profile)
  119. {
  120. blk_crypto_profile_destroy(profile);
  121. }
  122. /**
  123. * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init()
  124. * @dev: the device which owns the blk_crypto_profile
  125. * @profile: the blk_crypto_profile to initialize
  126. * @num_slots: the number of keyslots
  127. *
  128. * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be
  129. * called automatically on driver detach.
  130. *
  131. * Return: 0 on success, or else a negative error code.
  132. */
  133. int devm_blk_crypto_profile_init(struct device *dev,
  134. struct blk_crypto_profile *profile,
  135. unsigned int num_slots)
  136. {
  137. int err = blk_crypto_profile_init(profile, num_slots);
  138. if (err)
  139. return err;
  140. return devm_add_action_or_reset(dev,
  141. blk_crypto_profile_destroy_callback,
  142. profile);
  143. }
  144. EXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init);
  145. static inline struct hlist_head *
  146. blk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile,
  147. const struct blk_crypto_key *key)
  148. {
  149. return &profile->slot_hashtable[
  150. hash_ptr(key, profile->log_slot_ht_size)];
  151. }
  152. static void
  153. blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot)
  154. {
  155. struct blk_crypto_profile *profile = slot->profile;
  156. unsigned long flags;
  157. spin_lock_irqsave(&profile->idle_slots_lock, flags);
  158. list_del(&slot->idle_slot_node);
  159. spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
  160. }
  161. static struct blk_crypto_keyslot *
  162. blk_crypto_find_keyslot(struct blk_crypto_profile *profile,
  163. const struct blk_crypto_key *key)
  164. {
  165. const struct hlist_head *head =
  166. blk_crypto_hash_bucket_for_key(profile, key);
  167. struct blk_crypto_keyslot *slotp;
  168. hlist_for_each_entry(slotp, head, hash_node) {
  169. if (slotp->key == key)
  170. return slotp;
  171. }
  172. return NULL;
  173. }
  174. static struct blk_crypto_keyslot *
  175. blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile,
  176. const struct blk_crypto_key *key)
  177. {
  178. struct blk_crypto_keyslot *slot;
  179. slot = blk_crypto_find_keyslot(profile, key);
  180. if (!slot)
  181. return NULL;
  182. if (atomic_inc_return(&slot->slot_refs) == 1) {
  183. /* Took first reference to this slot; remove it from LRU list */
  184. blk_crypto_remove_slot_from_lru_list(slot);
  185. }
  186. return slot;
  187. }
  188. /**
  189. * blk_crypto_keyslot_index() - Get the index of a keyslot
  190. * @slot: a keyslot that blk_crypto_get_keyslot() returned
  191. *
  192. * Return: the 0-based index of the keyslot within the device's keyslots.
  193. */
  194. unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot)
  195. {
  196. return slot - slot->profile->slots;
  197. }
  198. EXPORT_SYMBOL_GPL(blk_crypto_keyslot_index);
  199. /**
  200. * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed.
  201. * @profile: the crypto profile of the device the key will be used on
  202. * @key: the key that will be used
  203. * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct
  204. * will be stored here. blk_crypto_put_keyslot() must be called
  205. * later to release it. Otherwise, NULL will be stored here.
  206. *
  207. * If the device has keyslots, this gets a keyslot that's been programmed with
  208. * the specified key. If the key is already in a slot, this reuses it;
  209. * otherwise this waits for a slot to become idle and programs the key into it.
  210. *
  211. * Context: Process context. Takes and releases profile->lock.
  212. * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or
  213. * one wasn't needed; or a blk_status_t error on failure.
  214. */
  215. blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
  216. const struct blk_crypto_key *key,
  217. struct blk_crypto_keyslot **slot_ptr)
  218. {
  219. struct blk_crypto_keyslot *slot;
  220. int slot_idx;
  221. int err;
  222. *slot_ptr = NULL;
  223. /*
  224. * If the device has no concept of "keyslots", then there is no need to
  225. * get one.
  226. */
  227. if (profile->num_slots == 0)
  228. return BLK_STS_OK;
  229. down_read(&profile->lock);
  230. slot = blk_crypto_find_and_grab_keyslot(profile, key);
  231. up_read(&profile->lock);
  232. if (slot)
  233. goto success;
  234. for (;;) {
  235. blk_crypto_hw_enter(profile);
  236. slot = blk_crypto_find_and_grab_keyslot(profile, key);
  237. if (slot) {
  238. blk_crypto_hw_exit(profile);
  239. goto success;
  240. }
  241. /*
  242. * If we're here, that means there wasn't a slot that was
  243. * already programmed with the key. So try to program it.
  244. */
  245. if (!list_empty(&profile->idle_slots))
  246. break;
  247. blk_crypto_hw_exit(profile);
  248. wait_event(profile->idle_slots_wait_queue,
  249. !list_empty(&profile->idle_slots));
  250. }
  251. slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot,
  252. idle_slot_node);
  253. slot_idx = blk_crypto_keyslot_index(slot);
  254. err = profile->ll_ops.keyslot_program(profile, key, slot_idx);
  255. if (err) {
  256. wake_up(&profile->idle_slots_wait_queue);
  257. blk_crypto_hw_exit(profile);
  258. return errno_to_blk_status(err);
  259. }
  260. /* Move this slot to the hash list for the new key. */
  261. if (slot->key)
  262. hlist_del(&slot->hash_node);
  263. slot->key = key;
  264. hlist_add_head(&slot->hash_node,
  265. blk_crypto_hash_bucket_for_key(profile, key));
  266. atomic_set(&slot->slot_refs, 1);
  267. blk_crypto_remove_slot_from_lru_list(slot);
  268. blk_crypto_hw_exit(profile);
  269. success:
  270. *slot_ptr = slot;
  271. return BLK_STS_OK;
  272. }
  273. /**
  274. * blk_crypto_put_keyslot() - Release a reference to a keyslot
  275. * @slot: The keyslot to release the reference of
  276. *
  277. * Context: Any context.
  278. */
  279. void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot)
  280. {
  281. struct blk_crypto_profile *profile = slot->profile;
  282. unsigned long flags;
  283. if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
  284. &profile->idle_slots_lock, flags)) {
  285. list_add_tail(&slot->idle_slot_node, &profile->idle_slots);
  286. spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
  287. wake_up(&profile->idle_slots_wait_queue);
  288. }
  289. }
  290. /**
  291. * __blk_crypto_cfg_supported() - Check whether the given crypto profile
  292. * supports the given crypto configuration.
  293. * @profile: the crypto profile to check
  294. * @cfg: the crypto configuration to check for
  295. *
  296. * Return: %true if @profile supports the given @cfg.
  297. */
  298. bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
  299. const struct blk_crypto_config *cfg)
  300. {
  301. if (!profile)
  302. return false;
  303. if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size))
  304. return false;
  305. if (profile->max_dun_bytes_supported < cfg->dun_bytes)
  306. return false;
  307. return true;
  308. }
  309. /*
  310. * This is an internal function that evicts a key from an inline encryption
  311. * device that can be either a real device or the blk-crypto-fallback "device".
  312. * It is used only by blk_crypto_evict_key(); see that function for details.
  313. */
  314. int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
  315. const struct blk_crypto_key *key)
  316. {
  317. struct blk_crypto_keyslot *slot;
  318. int err;
  319. if (profile->num_slots == 0) {
  320. if (profile->ll_ops.keyslot_evict) {
  321. blk_crypto_hw_enter(profile);
  322. err = profile->ll_ops.keyslot_evict(profile, key, -1);
  323. blk_crypto_hw_exit(profile);
  324. return err;
  325. }
  326. return 0;
  327. }
  328. blk_crypto_hw_enter(profile);
  329. slot = blk_crypto_find_keyslot(profile, key);
  330. if (!slot) {
  331. /*
  332. * Not an error, since a key not in use by I/O is not guaranteed
  333. * to be in a keyslot. There can be more keys than keyslots.
  334. */
  335. err = 0;
  336. goto out;
  337. }
  338. if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
  339. /* BUG: key is still in use by I/O */
  340. err = -EBUSY;
  341. goto out_remove;
  342. }
  343. err = profile->ll_ops.keyslot_evict(profile, key,
  344. blk_crypto_keyslot_index(slot));
  345. out_remove:
  346. /*
  347. * Callers free the key even on error, so unlink the key from the hash
  348. * table and clear slot->key even on error.
  349. */
  350. hlist_del(&slot->hash_node);
  351. slot->key = NULL;
  352. out:
  353. blk_crypto_hw_exit(profile);
  354. return err;
  355. }
  356. /**
  357. * blk_crypto_reprogram_all_keys() - Re-program all keyslots.
  358. * @profile: The crypto profile
  359. *
  360. * Re-program all keyslots that are supposed to have a key programmed. This is
  361. * intended only for use by drivers for hardware that loses its keys on reset.
  362. *
  363. * Context: Process context. Takes and releases profile->lock.
  364. */
  365. void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
  366. {
  367. unsigned int slot;
  368. if (profile->num_slots == 0)
  369. return;
  370. /* This is for device initialization, so don't resume the device */
  371. down_write(&profile->lock);
  372. for (slot = 0; slot < profile->num_slots; slot++) {
  373. const struct blk_crypto_key *key = profile->slots[slot].key;
  374. int err;
  375. if (!key)
  376. continue;
  377. err = profile->ll_ops.keyslot_program(profile, key, slot);
  378. WARN_ON(err);
  379. }
  380. up_write(&profile->lock);
  381. }
  382. EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys);
  383. void blk_crypto_profile_destroy(struct blk_crypto_profile *profile)
  384. {
  385. if (!profile)
  386. return;
  387. lockdep_unregister_key(&profile->lockdep_key);
  388. kvfree(profile->slot_hashtable);
  389. kvfree_sensitive(profile->slots,
  390. sizeof(profile->slots[0]) * profile->num_slots);
  391. memzero_explicit(profile, sizeof(*profile));
  392. }
  393. EXPORT_SYMBOL_GPL(blk_crypto_profile_destroy);
  394. bool blk_crypto_register(struct blk_crypto_profile *profile,
  395. struct request_queue *q)
  396. {
  397. if (blk_integrity_queue_supports_integrity(q)) {
  398. pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
  399. return false;
  400. }
  401. q->crypto_profile = profile;
  402. return true;
  403. }
  404. EXPORT_SYMBOL_GPL(blk_crypto_register);
  405. /**
  406. * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities
  407. * by child device
  408. * @parent: the crypto profile for the parent device
  409. * @child: the crypto profile for the child device, or NULL
  410. *
  411. * This clears all crypto capabilities in @parent that aren't set in @child. If
  412. * @child is NULL, then this clears all parent capabilities.
  413. *
  414. * Only use this when setting up the crypto profile for a layered device, before
  415. * it's been exposed yet.
  416. */
  417. void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
  418. const struct blk_crypto_profile *child)
  419. {
  420. if (child) {
  421. unsigned int i;
  422. parent->max_dun_bytes_supported =
  423. min(parent->max_dun_bytes_supported,
  424. child->max_dun_bytes_supported);
  425. for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++)
  426. parent->modes_supported[i] &= child->modes_supported[i];
  427. } else {
  428. parent->max_dun_bytes_supported = 0;
  429. memset(parent->modes_supported, 0,
  430. sizeof(parent->modes_supported));
  431. }
  432. }
  433. EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities);
  434. /**
  435. * blk_crypto_has_capabilities() - Check whether @target supports at least all
  436. * the crypto capabilities that @reference does.
  437. * @target: the target profile
  438. * @reference: the reference profile
  439. *
  440. * Return: %true if @target supports all the crypto capabilities of @reference.
  441. */
  442. bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
  443. const struct blk_crypto_profile *reference)
  444. {
  445. int i;
  446. if (!reference)
  447. return true;
  448. if (!target)
  449. return false;
  450. for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) {
  451. if (reference->modes_supported[i] & ~target->modes_supported[i])
  452. return false;
  453. }
  454. if (reference->max_dun_bytes_supported >
  455. target->max_dun_bytes_supported)
  456. return false;
  457. return true;
  458. }
  459. EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities);
  460. /**
  461. * blk_crypto_update_capabilities() - Update the capabilities of a crypto
  462. * profile to match those of another crypto
  463. * profile.
  464. * @dst: The crypto profile whose capabilities to update.
  465. * @src: The crypto profile whose capabilities this function will update @dst's
  466. * capabilities to.
  467. *
  468. * Blk-crypto requires that crypto capabilities that were
  469. * advertised when a bio was created continue to be supported by the
  470. * device until that bio is ended. This is turn means that a device cannot
  471. * shrink its advertised crypto capabilities without any explicit
  472. * synchronization with upper layers. So if there's no such explicit
  473. * synchronization, @src must support all the crypto capabilities that
  474. * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)).
  475. *
  476. * Note also that as long as the crypto capabilities are being expanded, the
  477. * order of updates becoming visible is not important because it's alright
  478. * for blk-crypto to see stale values - they only cause blk-crypto to
  479. * believe that a crypto capability isn't supported when it actually is (which
  480. * might result in blk-crypto-fallback being used if available, or the bio being
  481. * failed).
  482. */
  483. void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
  484. const struct blk_crypto_profile *src)
  485. {
  486. memcpy(dst->modes_supported, src->modes_supported,
  487. sizeof(dst->modes_supported));
  488. dst->max_dun_bytes_supported = src->max_dun_bytes_supported;
  489. }
  490. EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);