From: "Martin K. Petersen" Subject: Re: [PATCH] Performance Improvement in CRC16 Calculations. Date: Fri, 24 Aug 2018 17:46:15 -0400 Message-ID: References: <1533928331-21303-1-git-send-email-jeff.lien@wdc.com> <20180822062016.GA10356@infradead.org> Mime-Version: 1.0 Content-Type: text/plain Cc: "Martin K. Petersen" , Jeffrey Lien , Christoph Hellwig , "linux-kernel\@vger.kernel.org" , "linux-crypto\@vger.kernel.org" , "linux-block\@vger.kernel.org" , "linux-scsi\@vger.kernel.org" , "herbert\@gondor.apana.org.au" , "tim.c.chen\@linux.intel.com" , David Darrington , Jeff Furlong To: Ard Biesheuvel Return-path: In-Reply-To: (Ard Biesheuvel's message of "Fri, 24 Aug 2018 18:38:36 +0100") Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-crypto.vger.kernel.org Ard, > I'd prefer to handle this without help from userland. > > It shouldn't be too difficult to register a module notifier that only > sets a flag (or the static key, even?), and to free and re-allocate > the crc_t10dif transform if the flag is set. Something like this proof of concept? diff --git a/lib/crc-t10dif.c b/lib/crc-t10dif.c index 1ad33e555805..87d0e8f0794a 100644 --- a/lib/crc-t10dif.c +++ b/lib/crc-t10dif.c @@ -15,10 +15,50 @@ #include #include #include +#include static struct crypto_shash *crct10dif_tfm; static struct static_key crct10dif_fallback __read_mostly; +static void crc_t10dif_print(void) +{ + if (static_key_false(&crct10dif_fallback)) + pr_info("CRC T10 DIF calculated using library function\n"); + else + pr_info("CRC T10 DIF calculated using crypto hash %s\n", + crypto_tfm_alg_driver_name(crypto_shash_tfm(crct10dif_tfm))); +} + +static int crc_t10dif_rehash(struct notifier_block *self, unsigned long val, void *data) +{ +#ifdef CONFIG_MODULES + struct module *mod = data; + + if (val != MODULE_STATE_LIVE || + strncmp(mod->name, "crct10dif", strlen("crct10dif"))) + return 0; + + /* Fall back to library function while we replace the tfm */ + static_key_slow_inc(&crct10dif_fallback); + + crypto_free_shash(crct10dif_tfm); + crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); + if (IS_ERR(crct10dif_tfm)) { + crct10dif_tfm = NULL; + goto out; + } + + static_key_slow_dec(&crct10dif_fallback); +out: + crc_t10dif_print(); + return 0; +#endif /* CONFIG_MODULES */ +} + +static struct notifier_block crc_t10dif_nb = { + .notifier_call = crc_t10dif_rehash, +}; + __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len) { struct { @@ -49,16 +90,21 @@ EXPORT_SYMBOL(crc_t10dif); static int __init crc_t10dif_mod_init(void) { + register_module_notifier(&crc_t10dif_nb); + crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); if (IS_ERR(crct10dif_tfm)) { static_key_slow_inc(&crct10dif_fallback); crct10dif_tfm = NULL; } + + crc_t10dif_print(); return 0; } static void __exit crc_t10dif_mod_fini(void) { + unregister_module_notifier(&crc_t10dif_nb); crypto_free_shash(crct10dif_tfm); }