Blame view
lib/crc-t10dif.c
3.51 KB
40b0b3f8f treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
f11f594ed [SCSI] lib: Add s... |
2 3 4 5 6 |
/* * T10 Data Integrity Field CRC16 calculation * * Copyright (c) 2007 Oracle Corporation. All rights reserved. * Written by Martin K. Petersen <martin.petersen@oracle.com> |
f11f594ed [SCSI] lib: Add s... |
7 8 9 10 11 |
*/ #include <linux/types.h> #include <linux/module.h> #include <linux/crc-t10dif.h> |
68411521c Reinstate "crypto... |
12 13 14 |
#include <linux/err.h> #include <linux/init.h> #include <crypto/hash.h> |
b76377543 crc-t10dif: Pick ... |
15 |
#include <crypto/algapi.h> |
26052f9b9 crypto: crct10dif... |
16 |
#include <linux/static_key.h> |
b76377543 crc-t10dif: Pick ... |
17 |
#include <linux/notifier.h> |
f11f594ed [SCSI] lib: Add s... |
18 |
|
b76377543 crc-t10dif: Pick ... |
19 |
static struct crypto_shash __rcu *crct10dif_tfm; |
26052f9b9 crypto: crct10dif... |
20 |
static struct static_key crct10dif_fallback __read_mostly; |
a7e7edfea crc-t10dif: crc_t... |
21 |
static DEFINE_MUTEX(crc_t10dif_mutex); |
f2b639b2a crc-t10dif: Fix p... |
22 |
static struct work_struct crct10dif_rehash_work; |
b76377543 crc-t10dif: Pick ... |
23 |
|
f2b639b2a crc-t10dif: Fix p... |
24 |
static int crc_t10dif_notify(struct notifier_block *self, unsigned long val, void *data) |
b76377543 crc-t10dif: Pick ... |
25 26 |
{ struct crypto_alg *alg = data; |
b76377543 crc-t10dif: Pick ... |
27 28 29 30 31 |
if (val != CRYPTO_MSG_ALG_LOADED || static_key_false(&crct10dif_fallback) || strncmp(alg->cra_name, CRC_T10DIF_STRING, strlen(CRC_T10DIF_STRING))) return 0; |
f2b639b2a crc-t10dif: Fix p... |
32 33 34 35 36 37 38 |
schedule_work(&crct10dif_rehash_work); return 0; } static void crc_t10dif_rehash(struct work_struct *work) { struct crypto_shash *new, *old; |
b76377543 crc-t10dif: Pick ... |
39 40 41 42 43 |
mutex_lock(&crc_t10dif_mutex); old = rcu_dereference_protected(crct10dif_tfm, lockdep_is_held(&crc_t10dif_mutex)); if (!old) { mutex_unlock(&crc_t10dif_mutex); |
f2b639b2a crc-t10dif: Fix p... |
44 |
return; |
b76377543 crc-t10dif: Pick ... |
45 46 47 48 |
} new = crypto_alloc_shash("crct10dif", 0, 0); if (IS_ERR(new)) { mutex_unlock(&crc_t10dif_mutex); |
f2b639b2a crc-t10dif: Fix p... |
49 |
return; |
b76377543 crc-t10dif: Pick ... |
50 51 52 53 54 55 |
} rcu_assign_pointer(crct10dif_tfm, new); mutex_unlock(&crc_t10dif_mutex); synchronize_rcu(); crypto_free_shash(old); |
b76377543 crc-t10dif: Pick ... |
56 57 58 |
} static struct notifier_block crc_t10dif_nb = { |
f2b639b2a crc-t10dif: Fix p... |
59 |
.notifier_call = crc_t10dif_notify, |
b76377543 crc-t10dif: Pick ... |
60 |
}; |
f11f594ed [SCSI] lib: Add s... |
61 |
|
10081fb53 lib: introduce cr... |
62 |
__u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) |
f11f594ed [SCSI] lib: Add s... |
63 |
{ |
68411521c Reinstate "crypto... |
64 65 66 67 68 |
struct { struct shash_desc shash; char ctx[2]; } desc; int err; |
26052f9b9 crypto: crct10dif... |
69 |
if (static_key_false(&crct10dif_fallback)) |
10081fb53 lib: introduce cr... |
70 |
return crc_t10dif_generic(crc, buffer, len); |
26052f9b9 crypto: crct10dif... |
71 |
|
b76377543 crc-t10dif: Pick ... |
72 73 |
rcu_read_lock(); desc.shash.tfm = rcu_dereference(crct10dif_tfm); |
10081fb53 lib: introduce cr... |
74 |
*(__u16 *)desc.ctx = crc; |
f11f594ed [SCSI] lib: Add s... |
75 |
|
68411521c Reinstate "crypto... |
76 |
err = crypto_shash_update(&desc.shash, buffer, len); |
b76377543 crc-t10dif: Pick ... |
77 |
rcu_read_unlock(); |
68411521c Reinstate "crypto... |
78 |
BUG_ON(err); |
f11f594ed [SCSI] lib: Add s... |
79 |
|
68411521c Reinstate "crypto... |
80 |
return *(__u16 *)desc.ctx; |
f11f594ed [SCSI] lib: Add s... |
81 |
} |
10081fb53 lib: introduce cr... |
82 83 84 85 86 87 |
EXPORT_SYMBOL(crc_t10dif_update); __u16 crc_t10dif(const unsigned char *buffer, size_t len) { return crc_t10dif_update(0, buffer, len); } |
f11f594ed [SCSI] lib: Add s... |
88 |
EXPORT_SYMBOL(crc_t10dif); |
68411521c Reinstate "crypto... |
89 90 |
static int __init crc_t10dif_mod_init(void) { |
f2b639b2a crc-t10dif: Fix p... |
91 92 93 |
struct crypto_shash *tfm; INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash); |
b76377543 crc-t10dif: Pick ... |
94 |
crypto_register_notifier(&crc_t10dif_nb); |
f2b639b2a crc-t10dif: Fix p... |
95 96 97 |
mutex_lock(&crc_t10dif_mutex); tfm = crypto_alloc_shash("crct10dif", 0, 0); if (IS_ERR(tfm)) { |
26052f9b9 crypto: crct10dif... |
98 |
static_key_slow_inc(&crct10dif_fallback); |
f2b639b2a crc-t10dif: Fix p... |
99 |
tfm = NULL; |
26052f9b9 crypto: crct10dif... |
100 |
} |
f2b639b2a crc-t10dif: Fix p... |
101 102 |
RCU_INIT_POINTER(crct10dif_tfm, tfm); mutex_unlock(&crc_t10dif_mutex); |
26052f9b9 crypto: crct10dif... |
103 |
return 0; |
68411521c Reinstate "crypto... |
104 105 106 107 |
} static void __exit crc_t10dif_mod_fini(void) { |
b76377543 crc-t10dif: Pick ... |
108 |
crypto_unregister_notifier(&crc_t10dif_nb); |
f2b639b2a crc-t10dif: Fix p... |
109 110 |
cancel_work_sync(&crct10dif_rehash_work); crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1)); |
68411521c Reinstate "crypto... |
111 112 113 114 |
} module_init(crc_t10dif_mod_init); module_exit(crc_t10dif_mod_fini); |
11dcb1037 crc-t10dif: Allow... |
115 116 |
static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp) { |
f2b639b2a crc-t10dif: Fix p... |
117 118 119 |
struct crypto_shash *tfm; const char *name; int len; |
11dcb1037 crc-t10dif: Allow... |
120 121 122 |
if (static_key_false(&crct10dif_fallback)) return sprintf(buffer, "fallback "); |
f2b639b2a crc-t10dif: Fix p... |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
rcu_read_lock(); tfm = rcu_dereference(crct10dif_tfm); if (!tfm) { len = sprintf(buffer, "init "); goto unlock; } name = crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm)); len = sprintf(buffer, "%s ", name); unlock: rcu_read_unlock(); return len; |
11dcb1037 crc-t10dif: Allow... |
139 140 141 |
} module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0644); |
f11f594ed [SCSI] lib: Add s... |
142 143 |
MODULE_DESCRIPTION("T10 DIF CRC calculation"); MODULE_LICENSE("GPL"); |
68411521c Reinstate "crypto... |
144 |
MODULE_SOFTDEP("pre: crct10dif"); |