Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * T10 Data Integrity Field CRC16 calculation
0004  *
0005  * Copyright (c) 2007 Oracle Corporation.  All rights reserved.
0006  * Written by Martin K. Petersen <martin.petersen@oracle.com>
0007  */
0008 
0009 #include <linux/types.h>
0010 #include <linux/module.h>
0011 #include <linux/crc-t10dif.h>
0012 #include <linux/err.h>
0013 #include <linux/init.h>
0014 #include <crypto/hash.h>
0015 #include <crypto/algapi.h>
0016 #include <linux/static_key.h>
0017 #include <linux/notifier.h>
0018 
0019 static struct crypto_shash __rcu *crct10dif_tfm;
0020 static DEFINE_STATIC_KEY_TRUE(crct10dif_fallback);
0021 static DEFINE_MUTEX(crc_t10dif_mutex);
0022 static struct work_struct crct10dif_rehash_work;
0023 
0024 static int crc_t10dif_notify(struct notifier_block *self, unsigned long val, void *data)
0025 {
0026     struct crypto_alg *alg = data;
0027 
0028     if (val != CRYPTO_MSG_ALG_LOADED ||
0029         strcmp(alg->cra_name, CRC_T10DIF_STRING))
0030         return NOTIFY_DONE;
0031 
0032     schedule_work(&crct10dif_rehash_work);
0033     return NOTIFY_OK;
0034 }
0035 
0036 static void crc_t10dif_rehash(struct work_struct *work)
0037 {
0038     struct crypto_shash *new, *old;
0039 
0040     mutex_lock(&crc_t10dif_mutex);
0041     old = rcu_dereference_protected(crct10dif_tfm,
0042                     lockdep_is_held(&crc_t10dif_mutex));
0043     new = crypto_alloc_shash(CRC_T10DIF_STRING, 0, 0);
0044     if (IS_ERR(new)) {
0045         mutex_unlock(&crc_t10dif_mutex);
0046         return;
0047     }
0048     rcu_assign_pointer(crct10dif_tfm, new);
0049     mutex_unlock(&crc_t10dif_mutex);
0050 
0051     if (old) {
0052         synchronize_rcu();
0053         crypto_free_shash(old);
0054     } else {
0055         static_branch_disable(&crct10dif_fallback);
0056     }
0057 }
0058 
0059 static struct notifier_block crc_t10dif_nb = {
0060     .notifier_call = crc_t10dif_notify,
0061 };
0062 
0063 __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
0064 {
0065     struct {
0066         struct shash_desc shash;
0067         __u16 crc;
0068     } desc;
0069     int err;
0070 
0071     if (static_branch_unlikely(&crct10dif_fallback))
0072         return crc_t10dif_generic(crc, buffer, len);
0073 
0074     rcu_read_lock();
0075     desc.shash.tfm = rcu_dereference(crct10dif_tfm);
0076     desc.crc = crc;
0077     err = crypto_shash_update(&desc.shash, buffer, len);
0078     rcu_read_unlock();
0079 
0080     BUG_ON(err);
0081 
0082     return desc.crc;
0083 }
0084 EXPORT_SYMBOL(crc_t10dif_update);
0085 
0086 __u16 crc_t10dif(const unsigned char *buffer, size_t len)
0087 {
0088     return crc_t10dif_update(0, buffer, len);
0089 }
0090 EXPORT_SYMBOL(crc_t10dif);
0091 
0092 static int __init crc_t10dif_mod_init(void)
0093 {
0094     INIT_WORK(&crct10dif_rehash_work, crc_t10dif_rehash);
0095     crypto_register_notifier(&crc_t10dif_nb);
0096     crc_t10dif_rehash(&crct10dif_rehash_work);
0097     return 0;
0098 }
0099 
0100 static void __exit crc_t10dif_mod_fini(void)
0101 {
0102     crypto_unregister_notifier(&crc_t10dif_nb);
0103     cancel_work_sync(&crct10dif_rehash_work);
0104     crypto_free_shash(rcu_dereference_protected(crct10dif_tfm, 1));
0105 }
0106 
0107 module_init(crc_t10dif_mod_init);
0108 module_exit(crc_t10dif_mod_fini);
0109 
0110 static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
0111 {
0112     struct crypto_shash *tfm;
0113     int len;
0114 
0115     if (static_branch_unlikely(&crct10dif_fallback))
0116         return sprintf(buffer, "fallback\n");
0117 
0118     rcu_read_lock();
0119     tfm = rcu_dereference(crct10dif_tfm);
0120     len = snprintf(buffer, PAGE_SIZE, "%s\n",
0121                crypto_shash_driver_name(tfm));
0122     rcu_read_unlock();
0123 
0124     return len;
0125 }
0126 
0127 module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444);
0128 
0129 MODULE_DESCRIPTION("T10 DIF CRC calculation (library API)");
0130 MODULE_LICENSE("GPL");
0131 MODULE_SOFTDEP("pre: crct10dif");