crc-t10dif.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * T10 Data Integrity Field CRC16 calculation
  4. *
  5. * Copyright (c) 2007 Oracle Corporation. All rights reserved.
  6. * Written by Martin K. Petersen <martin.petersen@oracle.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/crc-t10dif.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <crypto/hash.h>
  14. #include <crypto/algapi.h>
  15. #include <linux/static_key.h>
  16. #include <linux/notifier.h>
  17. static struct crypto_shash __rcu *crct10dif_tfm;
  18. static DEFINE_STATIC_KEY_TRUE(crct10dif_fallback);
  19. static DEFINE_MUTEX(crc_t10dif_mutex);
  20. static struct work_struct crct10dif_rehash_work;
  21. static int crc_t10dif_notify(struct notifier_block *self, unsigned long val, void *data)
  22. {
  23. struct crypto_alg *alg = data;
  24. if (val != CRYPTO_MSG_ALG_LOADED ||
  25. strcmp(alg->cra_name, CRC_T10DIF_STRING))
  26. return NOTIFY_DONE;
  27. schedule_work(&crct10dif_rehash_work);
  28. return NOTIFY_OK;
  29. }
  30. static void crc_t10dif_rehash(struct work_struct *work)
  31. {
  32. struct crypto_shash *new, *old;
  33. mutex_lock(&crc_t10dif_mutex);
  34. old = rcu_dereference_protected(crct10dif_tfm,
  35. lockdep_is_held(&crc_t10dif_mutex));
  36. new = crypto_alloc_shash(CRC_T10DIF_STRING, 0, 0);
  37. if (IS_ERR(new)) {
  38. mutex_unlock(&crc_t10dif_mutex);
  39. return;
  40. }
  41. rcu_assign_pointer(crct10dif_tfm, new);
  42. mutex_unlock(&crc_t10dif_mutex);
  43. if (old) {
  44. synchronize_rcu();
  45. crypto_free_shash(old);
  46. } else {
  47. static_branch_disable(&crct10dif_fallback);
  48. }
  49. }
  50. static struct notifier_block crc_t10dif_nb = {
  51. .notifier_call = crc_t10dif_notify,
  52. };
  53. __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
  54. {
  55. struct {
  56. struct shash_desc shash;
  57. __u16 crc;
  58. } desc;
  59. int err;
  60. if (static_branch_unlikely(&crct10dif_fallback))
  61. return crc_t10dif_generic(crc, buffer, len);
  62. rcu_read_lock();
  63. desc.shash.tfm = rcu_dereference(crct10dif_tfm);
  64. desc.crc = crc;
  65. err = crypto_shash_update(&desc.shash, buffer, len);
  66. rcu_read_unlock();
  67. BUG_ON(err);
  68. return desc.crc;
  69. }
  70. EXPORT_SYMBOL(crc_t10dif_update);
  71. __u16 crc_t10dif(const unsigned char *buffer, size_t len)
  72. {
  73. return crc_t10dif_update(0, buffer, len);
  74. }
  75. EXPORT_SYMBOL(crc_t10dif);
  76. static int __init crc_t10dif_mod_init(void)
  77. {
  78. INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash);
  79. crypto_register_notifier(&crc_t10dif_nb);
  80. crc_t10dif_rehash(&crct10dif_rehash_work);
  81. return 0;
  82. }
  83. static void __exit crc_t10dif_mod_fini(void)
  84. {
  85. crypto_unregister_notifier(&crc_t10dif_nb);
  86. cancel_work_sync(&crct10dif_rehash_work);
  87. crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1));
  88. }
  89. module_init(crc_t10dif_mod_init);
  90. module_exit(crc_t10dif_mod_fini);
  91. static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
  92. {
  93. struct crypto_shash *tfm;
  94. int len;
  95. if (static_branch_unlikely(&crct10dif_fallback))
  96. return sprintf(buffer, "fallback\n");
  97. rcu_read_lock();
  98. tfm = rcu_dereference(crct10dif_tfm);
  99. len = snprintf(buffer, PAGE_SIZE, "%s\n",
  100. crypto_shash_driver_name(tfm));
  101. rcu_read_unlock();
  102. return len;
  103. }
  104. module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444);
  105. MODULE_DESCRIPTION("T10 DIF CRC calculation (library API)");
  106. MODULE_LICENSE("GPL");
  107. MODULE_SOFTDEP("pre: crct10dif");