diff mbox series

[2/7] crypto: lib/crc-t10dif - add static call support for optimized versions

Message ID 20210111165237.18178-3-ardb@kernel.org (mailing list archive)
State Changes Requested
Delegated to: Herbert Xu
Headers show
Series crypto: switch to static calls for CRC-T10DIF | expand

Commit Message

Ard Biesheuvel Jan. 11, 2021, 4:52 p.m. UTC
Wire up the new static call facility to the CRC-T10DIF library code, so
that optimized implementations can be swapped in easily, without having
to rely on the complexity of the crypto API shash infrastructure.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 include/linux/crc-t10dif.h | 21 ++++++++++++--
 lib/crc-t10dif.c           | 30 +++++++++++++++-----
 2 files changed, 42 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/include/linux/crc-t10dif.h b/include/linux/crc-t10dif.h
index 6bb0c0bf357b..ad8b0a358680 100644
--- a/include/linux/crc-t10dif.h
+++ b/include/linux/crc-t10dif.h
@@ -3,6 +3,7 @@ 
 #define _LINUX_CRC_T10DIF_H
 
 #include <linux/types.h>
+#include <linux/static_call.h>
 
 #define CRC_T10DIF_DIGEST_SIZE 2
 #define CRC_T10DIF_BLOCK_SIZE 1
@@ -10,7 +11,23 @@ 
 
 extern __u16 crc_t10dif_generic(__u16 crc, const unsigned char *buffer,
 				size_t len);
-extern __u16 crc_t10dif(unsigned char const *, size_t);
-extern __u16 crc_t10dif_update(__u16 crc, unsigned char const *, size_t);
+
+DECLARE_STATIC_CALL(crc_t10dif_arch, crc_t10dif_generic);
+
+static inline __u16 crc_t10dif_update(__u16 crc, unsigned char const *buffer,
+				      size_t len)
+{
+	return static_call(crc_t10dif_arch)(crc, buffer, len);
+}
+
+static inline __u16 crc_t10dif(unsigned char const *buffer, size_t len)
+{
+	return crc_t10dif_update(0, buffer, len);
+}
+
+int crc_t10dif_register(__u16 (*)(__u16, const unsigned char *, size_t),
+			const char *name);
+
+int crc_t10dif_unregister(void);
 
 #endif
diff --git a/lib/crc-t10dif.c b/lib/crc-t10dif.c
index cbb739f0a0d7..b657cdfb118a 100644
--- a/lib/crc-t10dif.c
+++ b/lib/crc-t10dif.c
@@ -18,6 +18,11 @@ 
 #include <linux/static_key.h>
 #include <linux/notifier.h>
 
+DEFINE_STATIC_CALL(crc_t10dif_arch, crc_t10dif_generic);
+EXPORT_STATIC_CALL(crc_t10dif_arch);
+
+static char const *crc_t10dif_arch_name;
+
 /* Table generated using the following polynomium:
  * x^16 + x^15 + x^11 + x^9 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
  * gt: 0x8bb7
@@ -68,21 +73,32 @@  __u16 crc_t10dif_generic(__u16 crc, const unsigned char *buffer, size_t len)
 }
 EXPORT_SYMBOL(crc_t10dif_generic);
 
-__u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
+int crc_t10dif_register(__u16 (*func)(__u16, const unsigned char *, size_t),
+			const char *name)
 {
-	return crc_t10dif_generic(crc, buffer, len);
+	if (!name || cmpxchg(&crc_t10dif_arch_name, NULL, name) != NULL)
+		return -EBUSY;
+
+	static_call_update(crc_t10dif_arch, func);
+	return 0;
 }
-EXPORT_SYMBOL(crc_t10dif_update);
+EXPORT_SYMBOL_NS(crc_t10dif_register, CRYPTO_INTERNAL);
 
-__u16 crc_t10dif(const unsigned char *buffer, size_t len)
+int crc_t10dif_unregister(void)
 {
-	return crc_t10dif_update(0, buffer, len);
+	// revert to generic implementation
+	static_call_update(crc_t10dif_arch, crc_t10dif_generic);
+	crc_t10dif_arch_name = NULL;
+
+	// wait for all potential callers of the unregistered routine to finish
+	synchronize_rcu_tasks();
+	return 0;
 }
-EXPORT_SYMBOL(crc_t10dif);
+EXPORT_SYMBOL_NS(crc_t10dif_unregister, CRYPTO_INTERNAL);
 
 static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
 {
-	return sprintf(buffer, "fallback\n");
+	return sprintf(buffer, "%s\n", crc_t10dif_arch_name ?: "fallback");
 }
 
 module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0444);