diff options
Diffstat (limited to 'lib/crc-t10dif.c')
| -rw-r--r-- | lib/crc-t10dif.c | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/lib/crc-t10dif.c b/lib/crc-t10dif.c index 1ad33e555805..4d0d47c1ffbd 100644 --- a/lib/crc-t10dif.c +++ b/lib/crc-t10dif.c | |||
| @@ -14,10 +14,47 @@ | |||
| 14 | #include <linux/err.h> | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> | 15 | #include <linux/init.h> |
| 16 | #include <crypto/hash.h> | 16 | #include <crypto/hash.h> |
| 17 | #include <crypto/algapi.h> | ||
| 17 | #include <linux/static_key.h> | 18 | #include <linux/static_key.h> |
| 19 | #include <linux/notifier.h> | ||
| 18 | 20 | ||
| 19 | static struct crypto_shash *crct10dif_tfm; | 21 | static struct crypto_shash __rcu *crct10dif_tfm; |
| 20 | static struct static_key crct10dif_fallback __read_mostly; | 22 | static struct static_key crct10dif_fallback __read_mostly; |
| 23 | static DEFINE_MUTEX(crc_t10dif_mutex); | ||
| 24 | |||
| 25 | static int crc_t10dif_rehash(struct notifier_block *self, unsigned long val, void *data) | ||
| 26 | { | ||
| 27 | struct crypto_alg *alg = data; | ||
| 28 | struct crypto_shash *new, *old; | ||
| 29 | |||
| 30 | if (val != CRYPTO_MSG_ALG_LOADED || | ||
| 31 | static_key_false(&crct10dif_fallback) || | ||
| 32 | strncmp(alg->cra_name, CRC_T10DIF_STRING, strlen(CRC_T10DIF_STRING))) | ||
| 33 | return 0; | ||
| 34 | |||
| 35 | mutex_lock(&crc_t10dif_mutex); | ||
| 36 | old = rcu_dereference_protected(crct10dif_tfm, | ||
| 37 | lockdep_is_held(&crc_t10dif_mutex)); | ||
| 38 | if (!old) { | ||
| 39 | mutex_unlock(&crc_t10dif_mutex); | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | new = crypto_alloc_shash("crct10dif", 0, 0); | ||
| 43 | if (IS_ERR(new)) { | ||
| 44 | mutex_unlock(&crc_t10dif_mutex); | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | rcu_assign_pointer(crct10dif_tfm, new); | ||
| 48 | mutex_unlock(&crc_t10dif_mutex); | ||
| 49 | |||
| 50 | synchronize_rcu(); | ||
| 51 | crypto_free_shash(old); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | static struct notifier_block crc_t10dif_nb = { | ||
| 56 | .notifier_call = crc_t10dif_rehash, | ||
| 57 | }; | ||
| 21 | 58 | ||
| 22 | __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) | 59 | __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) |
| 23 | { | 60 | { |
| @@ -30,11 +67,14 @@ __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) | |||
| 30 | if (static_key_false(&crct10dif_fallback)) | 67 | if (static_key_false(&crct10dif_fallback)) |
| 31 | return crc_t10dif_generic(crc, buffer, len); | 68 | return crc_t10dif_generic(crc, buffer, len); |
| 32 | 69 | ||
| 33 | desc.shash.tfm = crct10dif_tfm; | 70 | rcu_read_lock(); |
| 71 | desc.shash.tfm = rcu_dereference(crct10dif_tfm); | ||
| 34 | desc.shash.flags = 0; | 72 | desc.shash.flags = 0; |
| 35 | *(__u16 *)desc.ctx = crc; | 73 | *(__u16 *)desc.ctx = crc; |
| 36 | 74 | ||
| 37 | err = crypto_shash_update(&desc.shash, buffer, len); | 75 | err = crypto_shash_update(&desc.shash, buffer, len); |
| 76 | rcu_read_unlock(); | ||
| 77 | |||
| 38 | BUG_ON(err); | 78 | BUG_ON(err); |
| 39 | 79 | ||
| 40 | return *(__u16 *)desc.ctx; | 80 | return *(__u16 *)desc.ctx; |
| @@ -49,6 +89,7 @@ EXPORT_SYMBOL(crc_t10dif); | |||
| 49 | 89 | ||
| 50 | static int __init crc_t10dif_mod_init(void) | 90 | static int __init crc_t10dif_mod_init(void) |
| 51 | { | 91 | { |
| 92 | crypto_register_notifier(&crc_t10dif_nb); | ||
| 52 | crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); | 93 | crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); |
| 53 | if (IS_ERR(crct10dif_tfm)) { | 94 | if (IS_ERR(crct10dif_tfm)) { |
| 54 | static_key_slow_inc(&crct10dif_fallback); | 95 | static_key_slow_inc(&crct10dif_fallback); |
| @@ -59,12 +100,24 @@ static int __init crc_t10dif_mod_init(void) | |||
| 59 | 100 | ||
| 60 | static void __exit crc_t10dif_mod_fini(void) | 101 | static void __exit crc_t10dif_mod_fini(void) |
| 61 | { | 102 | { |
| 103 | crypto_unregister_notifier(&crc_t10dif_nb); | ||
| 62 | crypto_free_shash(crct10dif_tfm); | 104 | crypto_free_shash(crct10dif_tfm); |
| 63 | } | 105 | } |
| 64 | 106 | ||
| 65 | module_init(crc_t10dif_mod_init); | 107 | module_init(crc_t10dif_mod_init); |
| 66 | module_exit(crc_t10dif_mod_fini); | 108 | module_exit(crc_t10dif_mod_fini); |
| 67 | 109 | ||
| 110 | static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp) | ||
| 111 | { | ||
| 112 | if (static_key_false(&crct10dif_fallback)) | ||
| 113 | return sprintf(buffer, "fallback\n"); | ||
| 114 | |||
| 115 | return sprintf(buffer, "%s\n", | ||
| 116 | crypto_tfm_alg_driver_name(crypto_shash_tfm(crct10dif_tfm))); | ||
| 117 | } | ||
| 118 | |||
| 119 | module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0644); | ||
| 120 | |||
| 68 | MODULE_DESCRIPTION("T10 DIF CRC calculation"); | 121 | MODULE_DESCRIPTION("T10 DIF CRC calculation"); |
| 69 | MODULE_LICENSE("GPL"); | 122 | MODULE_LICENSE("GPL"); |
| 70 | MODULE_SOFTDEP("pre: crct10dif"); | 123 | MODULE_SOFTDEP("pre: crct10dif"); |
