Blame view
lib/crc-t10dif.c
3.16 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; |
be924e0aa crc-t10dif: use f... |
20 |
static DEFINE_STATIC_KEY_TRUE(crct10dif_fallback); |
a7e7edfea crc-t10dif: crc_t... |
21 |
static DEFINE_MUTEX(crc_t10dif_mutex); |
3906f6402 crc-t10dif: Fix p... |
22 |
static struct work_struct crct10dif_rehash_work; |
b76377543 crc-t10dif: Pick ... |
23 |
|
3906f6402 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 |
if (val != CRYPTO_MSG_ALG_LOADED || |
29195232f crc-t10dif: clean... |
29 30 |
strcmp(alg->cra_name, CRC_T10DIF_STRING)) return NOTIFY_DONE; |
b76377543 crc-t10dif: Pick ... |
31 |
|
3906f6402 crc-t10dif: Fix p... |
32 |
schedule_work(&crct10dif_rehash_work); |
29195232f crc-t10dif: clean... |
33 |
return NOTIFY_OK; |
3906f6402 crc-t10dif: Fix p... |
34 35 36 37 38 |
} static void crc_t10dif_rehash(struct work_struct *work) { struct crypto_shash *new, *old; |
b76377543 crc-t10dif: Pick ... |
39 40 41 |
mutex_lock(&crc_t10dif_mutex); old = rcu_dereference_protected(crct10dif_tfm, lockdep_is_held(&crc_t10dif_mutex)); |
29195232f crc-t10dif: clean... |
42 |
new = crypto_alloc_shash(CRC_T10DIF_STRING, 0, 0); |
b76377543 crc-t10dif: Pick ... |
43 44 |
if (IS_ERR(new)) { mutex_unlock(&crc_t10dif_mutex); |
3906f6402 crc-t10dif: Fix p... |
45 |
return; |
b76377543 crc-t10dif: Pick ... |
46 47 48 |
} rcu_assign_pointer(crct10dif_tfm, new); mutex_unlock(&crc_t10dif_mutex); |
be924e0aa crc-t10dif: use f... |
49 50 51 52 53 54 |
if (old) { synchronize_rcu(); crypto_free_shash(old); } else { static_branch_disable(&crct10dif_fallback); } |
b76377543 crc-t10dif: Pick ... |
55 56 57 |
} static struct notifier_block crc_t10dif_nb = { |
3906f6402 crc-t10dif: Fix p... |
58 |
.notifier_call = crc_t10dif_notify, |
b76377543 crc-t10dif: Pick ... |
59 |
}; |
f11f594ed [SCSI] lib: Add s... |
60 |
|
10081fb53 lib: introduce cr... |
61 |
__u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) |
f11f594ed [SCSI] lib: Add s... |
62 |
{ |
68411521c Reinstate "crypto... |
63 64 |
struct { struct shash_desc shash; |
29195232f crc-t10dif: clean... |
65 |
__u16 crc; |
68411521c Reinstate "crypto... |
66 67 |
} desc; int err; |
be924e0aa crc-t10dif: use f... |
68 |
if (static_branch_unlikely(&crct10dif_fallback)) |
10081fb53 lib: introduce cr... |
69 |
return crc_t10dif_generic(crc, buffer, len); |
26052f9b9 crypto: crct10dif... |
70 |
|
b76377543 crc-t10dif: Pick ... |
71 72 |
rcu_read_lock(); desc.shash.tfm = rcu_dereference(crct10dif_tfm); |
29195232f crc-t10dif: clean... |
73 |
desc.crc = crc; |
68411521c Reinstate "crypto... |
74 |
err = crypto_shash_update(&desc.shash, buffer, len); |
b76377543 crc-t10dif: Pick ... |
75 |
rcu_read_unlock(); |
68411521c Reinstate "crypto... |
76 |
BUG_ON(err); |
f11f594ed [SCSI] lib: Add s... |
77 |
|
29195232f crc-t10dif: clean... |
78 |
return desc.crc; |
f11f594ed [SCSI] lib: Add s... |
79 |
} |
10081fb53 lib: introduce cr... |
80 81 82 83 84 85 |
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... |
86 |
EXPORT_SYMBOL(crc_t10dif); |
68411521c Reinstate "crypto... |
87 88 |
static int __init crc_t10dif_mod_init(void) { |
3906f6402 crc-t10dif: Fix p... |
89 |
INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash); |
b76377543 crc-t10dif: Pick ... |
90 |
crypto_register_notifier(&crc_t10dif_nb); |
be924e0aa crc-t10dif: use f... |
91 |
crc_t10dif_rehash(&crct10dif_rehash_work); |
26052f9b9 crypto: crct10dif... |
92 |
return 0; |
68411521c Reinstate "crypto... |
93 94 95 96 |
} static void __exit crc_t10dif_mod_fini(void) { |
b76377543 crc-t10dif: Pick ... |
97 |
crypto_unregister_notifier(&crc_t10dif_nb); |
3906f6402 crc-t10dif: Fix p... |
98 99 |
cancel_work_sync(&crct10dif_rehash_work); crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1)); |
68411521c Reinstate "crypto... |
100 101 102 103 |
} module_init(crc_t10dif_mod_init); module_exit(crc_t10dif_mod_fini); |
11dcb1037 crc-t10dif: Allow... |
104 105 |
static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp) { |
3906f6402 crc-t10dif: Fix p... |
106 |
struct crypto_shash *tfm; |
3906f6402 crc-t10dif: Fix p... |
107 |
int len; |
be924e0aa crc-t10dif: use f... |
108 |
if (static_branch_unlikely(&crct10dif_fallback)) |
11dcb1037 crc-t10dif: Allow... |
109 110 |
return sprintf(buffer, "fallback "); |
3906f6402 crc-t10dif: Fix p... |
111 112 |
rcu_read_lock(); tfm = rcu_dereference(crct10dif_tfm); |
29195232f crc-t10dif: clean... |
113 114 115 |
len = snprintf(buffer, PAGE_SIZE, "%s ", crypto_shash_driver_name(tfm)); |
3906f6402 crc-t10dif: Fix p... |
116 117 118 |
rcu_read_unlock(); return len; |
11dcb1037 crc-t10dif: Allow... |
119 |
} |
29195232f crc-t10dif: clean... |
120 |
module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444); |
11dcb1037 crc-t10dif: Allow... |
121 |
|
29195232f crc-t10dif: clean... |
122 |
MODULE_DESCRIPTION("T10 DIF CRC calculation (library API)"); |
f11f594ed [SCSI] lib: Add s... |
123 |
MODULE_LICENSE("GPL"); |
68411521c Reinstate "crypto... |
124 |
MODULE_SOFTDEP("pre: crct10dif"); |