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 <linux/init.h>
 #include <crypto/hash.h>
 #include <linux/static_key.h>
+#include <linux/notifier.h>
 
 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);
 }
 

Reply via email to