blob: 1ed2ed4870971ed305f7b9b5d4a1761824694964 [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Martin K. Petersenf11f5942008-06-25 11:22:42 -04002/*
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>
Martin K. Petersenf11f5942008-06-25 11:22:42 -04007 */
8
9#include <linux/types.h>
10#include <linux/module.h>
11#include <linux/crc-t10dif.h>
Herbert Xu684115212013-09-07 12:56:26 +100012#include <linux/err.h>
13#include <linux/init.h>
14#include <crypto/hash.h>
Martin K. Petersenb7637752018-08-30 11:00:15 -040015#include <crypto/algapi.h>
Herbert Xu26052f92013-09-12 15:31:34 +100016#include <linux/static_key.h>
Martin K. Petersenb7637752018-08-30 11:00:15 -040017#include <linux/notifier.h>
Martin K. Petersenf11f5942008-06-25 11:22:42 -040018
Martin K. Petersenb7637752018-08-30 11:00:15 -040019static struct crypto_shash __rcu *crct10dif_tfm;
Eric Biggersbe924e02020-06-09 23:39:42 -070020static DEFINE_STATIC_KEY_TRUE(crct10dif_fallback);
kbuild test robota7e7edf2018-09-05 01:52:44 +080021static DEFINE_MUTEX(crc_t10dif_mutex);
Herbert Xu3906f642020-06-05 16:59:18 +100022static struct work_struct crct10dif_rehash_work;
Martin K. Petersenb7637752018-08-30 11:00:15 -040023
Herbert Xu3906f642020-06-05 16:59:18 +100024static int crc_t10dif_notify(struct notifier_block *self, unsigned long val, void *data)
Martin K. Petersenb7637752018-08-30 11:00:15 -040025{
26 struct crypto_alg *alg = data;
Martin K. Petersenb7637752018-08-30 11:00:15 -040027
28 if (val != CRYPTO_MSG_ALG_LOADED ||
Eric Biggers29195232020-06-09 23:39:43 -070029 strcmp(alg->cra_name, CRC_T10DIF_STRING))
30 return NOTIFY_DONE;
Martin K. Petersenb7637752018-08-30 11:00:15 -040031
Herbert Xu3906f642020-06-05 16:59:18 +100032 schedule_work(&crct10dif_rehash_work);
Eric Biggers29195232020-06-09 23:39:43 -070033 return NOTIFY_OK;
Herbert Xu3906f642020-06-05 16:59:18 +100034}
35
36static void crc_t10dif_rehash(struct work_struct *work)
37{
38 struct crypto_shash *new, *old;
39
Martin K. Petersenb7637752018-08-30 11:00:15 -040040 mutex_lock(&crc_t10dif_mutex);
41 old = rcu_dereference_protected(crct10dif_tfm,
42 lockdep_is_held(&crc_t10dif_mutex));
Eric Biggers29195232020-06-09 23:39:43 -070043 new = crypto_alloc_shash(CRC_T10DIF_STRING, 0, 0);
Martin K. Petersenb7637752018-08-30 11:00:15 -040044 if (IS_ERR(new)) {
45 mutex_unlock(&crc_t10dif_mutex);
Herbert Xu3906f642020-06-05 16:59:18 +100046 return;
Martin K. Petersenb7637752018-08-30 11:00:15 -040047 }
48 rcu_assign_pointer(crct10dif_tfm, new);
49 mutex_unlock(&crc_t10dif_mutex);
50
Eric Biggersbe924e02020-06-09 23:39:42 -070051 if (old) {
52 synchronize_rcu();
53 crypto_free_shash(old);
54 } else {
55 static_branch_disable(&crct10dif_fallback);
56 }
Martin K. Petersenb7637752018-08-30 11:00:15 -040057}
58
59static struct notifier_block crc_t10dif_nb = {
Herbert Xu3906f642020-06-05 16:59:18 +100060 .notifier_call = crc_t10dif_notify,
Martin K. Petersenb7637752018-08-30 11:00:15 -040061};
Martin K. Petersenf11f5942008-06-25 11:22:42 -040062
Akinobu Mita10081fb2015-05-01 15:23:50 +090063__u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
Martin K. Petersenf11f5942008-06-25 11:22:42 -040064{
Herbert Xu684115212013-09-07 12:56:26 +100065 struct {
66 struct shash_desc shash;
Eric Biggers29195232020-06-09 23:39:43 -070067 __u16 crc;
Herbert Xu684115212013-09-07 12:56:26 +100068 } desc;
69 int err;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040070
Eric Biggersbe924e02020-06-09 23:39:42 -070071 if (static_branch_unlikely(&crct10dif_fallback))
Akinobu Mita10081fb2015-05-01 15:23:50 +090072 return crc_t10dif_generic(crc, buffer, len);
Herbert Xu26052f92013-09-12 15:31:34 +100073
Martin K. Petersenb7637752018-08-30 11:00:15 -040074 rcu_read_lock();
75 desc.shash.tfm = rcu_dereference(crct10dif_tfm);
Eric Biggers29195232020-06-09 23:39:43 -070076 desc.crc = crc;
Herbert Xu684115212013-09-07 12:56:26 +100077 err = crypto_shash_update(&desc.shash, buffer, len);
Martin K. Petersenb7637752018-08-30 11:00:15 -040078 rcu_read_unlock();
79
Herbert Xu684115212013-09-07 12:56:26 +100080 BUG_ON(err);
81
Eric Biggers29195232020-06-09 23:39:43 -070082 return desc.crc;
Martin K. Petersenf11f5942008-06-25 11:22:42 -040083}
Akinobu Mita10081fb2015-05-01 15:23:50 +090084EXPORT_SYMBOL(crc_t10dif_update);
85
86__u16 crc_t10dif(const unsigned char *buffer, size_t len)
87{
88 return crc_t10dif_update(0, buffer, len);
89}
Martin K. Petersenf11f5942008-06-25 11:22:42 -040090EXPORT_SYMBOL(crc_t10dif);
91
Herbert Xu684115212013-09-07 12:56:26 +100092static int __init crc_t10dif_mod_init(void)
93{
Herbert Xu3906f642020-06-05 16:59:18 +100094 INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash);
Martin K. Petersenb7637752018-08-30 11:00:15 -040095 crypto_register_notifier(&crc_t10dif_nb);
Eric Biggersbe924e02020-06-09 23:39:42 -070096 crc_t10dif_rehash(&crct10dif_rehash_work);
Herbert Xu26052f92013-09-12 15:31:34 +100097 return 0;
Herbert Xu684115212013-09-07 12:56:26 +100098}
99
100static void __exit crc_t10dif_mod_fini(void)
101{
Martin K. Petersenb7637752018-08-30 11:00:15 -0400102 crypto_unregister_notifier(&crc_t10dif_nb);
Herbert Xu3906f642020-06-05 16:59:18 +1000103 cancel_work_sync(&crct10dif_rehash_work);
104 crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1));
Herbert Xu684115212013-09-07 12:56:26 +1000105}
106
107module_init(crc_t10dif_mod_init);
108module_exit(crc_t10dif_mod_fini);
109
Martin K. Petersen11dcb102018-08-30 11:00:16 -0400110static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
111{
Herbert Xu3906f642020-06-05 16:59:18 +1000112 struct crypto_shash *tfm;
Herbert Xu3906f642020-06-05 16:59:18 +1000113 int len;
114
Eric Biggersbe924e02020-06-09 23:39:42 -0700115 if (static_branch_unlikely(&crct10dif_fallback))
Martin K. Petersen11dcb102018-08-30 11:00:16 -0400116 return sprintf(buffer, "fallback\n");
117
Herbert Xu3906f642020-06-05 16:59:18 +1000118 rcu_read_lock();
119 tfm = rcu_dereference(crct10dif_tfm);
Eric Biggers29195232020-06-09 23:39:43 -0700120 len = snprintf(buffer, PAGE_SIZE, "%s\n",
121 crypto_shash_driver_name(tfm));
Herbert Xu3906f642020-06-05 16:59:18 +1000122 rcu_read_unlock();
123
124 return len;
Martin K. Petersen11dcb102018-08-30 11:00:16 -0400125}
126
Eric Biggers29195232020-06-09 23:39:43 -0700127module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444);
Martin K. Petersen11dcb102018-08-30 11:00:16 -0400128
Eric Biggers29195232020-06-09 23:39:43 -0700129MODULE_DESCRIPTION("T10 DIF CRC calculation (library API)");
Martin K. Petersenf11f5942008-06-25 11:22:42 -0400130MODULE_LICENSE("GPL");
Herbert Xu684115212013-09-07 12:56:26 +1000131MODULE_SOFTDEP("pre: crct10dif");