blob: 1ed2ed4870971ed305f7b9b5d4a1761824694964 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/types.h>
10#include <linux/module.h>
11#include <linux/crc-t10dif.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <crypto/hash.h>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <crypto/algapi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/static_key.h>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <linux/notifier.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
David Brazdil0f672f62019-12-10 10:32:29 +000019static struct crypto_shash __rcu *crct10dif_tfm;
Olivier Deprez157378f2022-04-04 15:47:50 +020020static DEFINE_STATIC_KEY_TRUE(crct10dif_fallback);
David Brazdil0f672f62019-12-10 10:32:29 +000021static DEFINE_MUTEX(crc_t10dif_mutex);
Olivier Deprez0e641232021-09-23 10:07:05 +020022static struct work_struct crct10dif_rehash_work;
David Brazdil0f672f62019-12-10 10:32:29 +000023
Olivier Deprez0e641232021-09-23 10:07:05 +020024static int crc_t10dif_notify(struct notifier_block *self, unsigned long val, void *data)
David Brazdil0f672f62019-12-10 10:32:29 +000025{
26 struct crypto_alg *alg = data;
David Brazdil0f672f62019-12-10 10:32:29 +000027
28 if (val != CRYPTO_MSG_ALG_LOADED ||
Olivier Deprez157378f2022-04-04 15:47:50 +020029 strcmp(alg->cra_name, CRC_T10DIF_STRING))
30 return NOTIFY_DONE;
David Brazdil0f672f62019-12-10 10:32:29 +000031
Olivier Deprez0e641232021-09-23 10:07:05 +020032 schedule_work(&crct10dif_rehash_work);
Olivier Deprez157378f2022-04-04 15:47:50 +020033 return NOTIFY_OK;
Olivier Deprez0e641232021-09-23 10:07:05 +020034}
35
36static void crc_t10dif_rehash(struct work_struct *work)
37{
38 struct crypto_shash *new, *old;
39
David Brazdil0f672f62019-12-10 10:32:29 +000040 mutex_lock(&crc_t10dif_mutex);
41 old = rcu_dereference_protected(crct10dif_tfm,
42 lockdep_is_held(&crc_t10dif_mutex));
Olivier Deprez157378f2022-04-04 15:47:50 +020043 new = crypto_alloc_shash(CRC_T10DIF_STRING, 0, 0);
David Brazdil0f672f62019-12-10 10:32:29 +000044 if (IS_ERR(new)) {
45 mutex_unlock(&crc_t10dif_mutex);
Olivier Deprez0e641232021-09-23 10:07:05 +020046 return;
David Brazdil0f672f62019-12-10 10:32:29 +000047 }
48 rcu_assign_pointer(crct10dif_tfm, new);
49 mutex_unlock(&crc_t10dif_mutex);
50
Olivier Deprez157378f2022-04-04 15:47:50 +020051 if (old) {
52 synchronize_rcu();
53 crypto_free_shash(old);
54 } else {
55 static_branch_disable(&crct10dif_fallback);
56 }
David Brazdil0f672f62019-12-10 10:32:29 +000057}
58
59static struct notifier_block crc_t10dif_nb = {
Olivier Deprez0e641232021-09-23 10:07:05 +020060 .notifier_call = crc_t10dif_notify,
David Brazdil0f672f62019-12-10 10:32:29 +000061};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062
63__u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
64{
65 struct {
66 struct shash_desc shash;
Olivier Deprez157378f2022-04-04 15:47:50 +020067 __u16 crc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 } desc;
69 int err;
70
Olivier Deprez157378f2022-04-04 15:47:50 +020071 if (static_branch_unlikely(&crct10dif_fallback))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072 return crc_t10dif_generic(crc, buffer, len);
73
David Brazdil0f672f62019-12-10 10:32:29 +000074 rcu_read_lock();
75 desc.shash.tfm = rcu_dereference(crct10dif_tfm);
Olivier Deprez157378f2022-04-04 15:47:50 +020076 desc.crc = crc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 err = crypto_shash_update(&desc.shash, buffer, len);
David Brazdil0f672f62019-12-10 10:32:29 +000078 rcu_read_unlock();
79
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 BUG_ON(err);
81
Olivier Deprez157378f2022-04-04 15:47:50 +020082 return desc.crc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083}
84EXPORT_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}
90EXPORT_SYMBOL(crc_t10dif);
91
92static int __init crc_t10dif_mod_init(void)
93{
Olivier Deprez0e641232021-09-23 10:07:05 +020094 INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash);
David Brazdil0f672f62019-12-10 10:32:29 +000095 crypto_register_notifier(&crc_t10dif_nb);
Olivier Deprez157378f2022-04-04 15:47:50 +020096 crc_t10dif_rehash(&crct10dif_rehash_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 return 0;
98}
99
100static void __exit crc_t10dif_mod_fini(void)
101{
David Brazdil0f672f62019-12-10 10:32:29 +0000102 crypto_unregister_notifier(&crc_t10dif_nb);
Olivier Deprez0e641232021-09-23 10:07:05 +0200103 cancel_work_sync(&crct10dif_rehash_work);
104 crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105}
106
107module_init(crc_t10dif_mod_init);
108module_exit(crc_t10dif_mod_fini);
109
David Brazdil0f672f62019-12-10 10:32:29 +0000110static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
111{
Olivier Deprez0e641232021-09-23 10:07:05 +0200112 struct crypto_shash *tfm;
Olivier Deprez0e641232021-09-23 10:07:05 +0200113 int len;
114
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 if (static_branch_unlikely(&crct10dif_fallback))
David Brazdil0f672f62019-12-10 10:32:29 +0000116 return sprintf(buffer, "fallback\n");
117
Olivier Deprez0e641232021-09-23 10:07:05 +0200118 rcu_read_lock();
119 tfm = rcu_dereference(crct10dif_tfm);
Olivier Deprez157378f2022-04-04 15:47:50 +0200120 len = snprintf(buffer, PAGE_SIZE, "%s\n",
121 crypto_shash_driver_name(tfm));
Olivier Deprez0e641232021-09-23 10:07:05 +0200122 rcu_read_unlock();
123
124 return len;
David Brazdil0f672f62019-12-10 10:32:29 +0000125}
126
Olivier Deprez157378f2022-04-04 15:47:50 +0200127module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444);
David Brazdil0f672f62019-12-10 10:32:29 +0000128
Olivier Deprez157378f2022-04-04 15:47:50 +0200129MODULE_DESCRIPTION("T10 DIF CRC calculation (library API)");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000130MODULE_LICENSE("GPL");
131MODULE_SOFTDEP("pre: crct10dif");