From: Eric Biggers <ebiggers@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: linux-crypto@vger.kernel.org, x86@kernel.org,
linux-block@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
Keith Busch <kbusch@kernel.org>,
Kent Overstreet <kent.overstreet@linux.dev>,
"Martin K . Petersen" <martin.petersen@oracle.com>
Subject: [PATCH v2 05/11] lib/crc64: add support for arch-optimized implementations
Date: Wed, 29 Jan 2025 19:51:24 -0800 [thread overview]
Message-ID: <20250130035130.180676-6-ebiggers@kernel.org> (raw)
In-Reply-To: <20250130035130.180676-1-ebiggers@kernel.org>
From: Eric Biggers <ebiggers@google.com>
Add support for architecture-optimized implementations of the CRC64
library functions, following the approach taken for the CRC32 and
CRC-T10DIF library functions.
Also take the opportunity to tweak the function prototypes:
- Use 'const void *' for the lib entry points (since this is easier for
users) but 'const u8 *' for the underlying arch and generic functions
(since this is easier for the implementations of these functions).
- Don't bother with __pure. It's an unusual optimization that doesn't
help properly written code. It's a weird quirk we can do without.
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
include/linux/crc64.h | 26 ++++++++++++++++++++++----
lib/Kconfig | 7 +++++++
lib/crc64.c | 36 ++++++++----------------------------
3 files changed, 37 insertions(+), 32 deletions(-)
diff --git a/include/linux/crc64.h b/include/linux/crc64.h
index 17cf5af3e78e..41de30b907df 100644
--- a/include/linux/crc64.h
+++ b/include/linux/crc64.h
@@ -5,12 +5,28 @@
#ifndef _LINUX_CRC64_H
#define _LINUX_CRC64_H
#include <linux/types.h>
-u64 __pure crc64_be(u64 crc, const void *p, size_t len);
-u64 __pure crc64_nvme_generic(u64 crc, const void *p, size_t len);
+u64 crc64_be_arch(u64 crc, const u8 *p, size_t len);
+u64 crc64_be_generic(u64 crc, const u8 *p, size_t len);
+u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len);
+u64 crc64_nvme_generic(u64 crc, const u8 *p, size_t len);
+
+/**
+ * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
+ * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
+ * or the previous crc64 value if computing incrementally.
+ * @p: pointer to buffer over which CRC64 is run
+ * @len: length of buffer @p
+ */
+static inline u64 crc64_be(u64 crc, const void *p, size_t len)
+{
+ if (IS_ENABLED(CONFIG_CRC64_ARCH))
+ return crc64_be_arch(crc, p, len);
+ return crc64_be_generic(crc, p, len);
+}
/**
* crc64_nvme - Calculate CRC64-NVME
* @crc: seed value for computation. 0 for a new CRC calculation, or the
* previous crc64 value if computing incrementally.
@@ -18,11 +34,13 @@ u64 __pure crc64_nvme_generic(u64 crc, const void *p, size_t len);
* @len: length of buffer @p
*
* This computes the CRC64 defined in the NVME NVM Command Set Specification,
* *including the bitwise inversion at the beginning and end*.
*/
-static inline u64 crc64_nvme(u64 crc, const u8 *p, size_t len)
+static inline u64 crc64_nvme(u64 crc, const void *p, size_t len)
{
- return crc64_nvme_generic(crc, p, len);
+ if (IS_ENABLED(CONFIG_CRC64_ARCH))
+ return ~crc64_nvme_arch(~crc, p, len);
+ return ~crc64_nvme_generic(~crc, p, len);
}
#endif /* _LINUX_CRC64_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index da07fd39cf97..67bbf4f64dd9 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -199,10 +199,17 @@ config CRC64
This option is provided for the case where no in-kernel-tree
modules require CRC64 functions, but a module built outside
the kernel tree does. Such modules that use library CRC64
functions require M here.
+config ARCH_HAS_CRC64
+ bool
+
+config CRC64_ARCH
+ tristate
+ default CRC64 if ARCH_HAS_CRC64 && CRC_OPTIMIZATIONS
+
config CRC4
tristate "CRC4 functions"
help
This option is provided for the case where no in-kernel-tree
modules require CRC4 functions, but a module built outside
diff --git a/lib/crc64.c b/lib/crc64.c
index d6f3f245eede..5b1b17057f0a 100644
--- a/lib/crc64.c
+++ b/lib/crc64.c
@@ -39,40 +39,20 @@
#include "crc64table.h"
MODULE_DESCRIPTION("CRC64 calculations");
MODULE_LICENSE("GPL v2");
-/**
- * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
- * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
- * or the previous crc64 value if computing incrementally.
- * @p: pointer to buffer over which CRC64 is run
- * @len: length of buffer @p
- */
-u64 __pure crc64_be(u64 crc, const void *p, size_t len)
+u64 crc64_be_generic(u64 crc, const u8 *p, size_t len)
{
- size_t i, t;
-
- const unsigned char *_p = p;
-
- for (i = 0; i < len; i++) {
- t = ((crc >> 56) ^ (*_p++)) & 0xFF;
- crc = crc64table[t] ^ (crc << 8);
- }
-
+ while (len--)
+ crc = (crc << 8) ^ crc64table[(crc >> 56) ^ *p++];
return crc;
}
-EXPORT_SYMBOL_GPL(crc64_be);
+EXPORT_SYMBOL_GPL(crc64_be_generic);
-u64 __pure crc64_nvme_generic(u64 crc, const void *p, size_t len)
+u64 crc64_nvme_generic(u64 crc, const u8 *p, size_t len)
{
- const unsigned char *_p = p;
- size_t i;
-
- crc = ~crc;
-
- for (i = 0; i < len; i++)
- crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *_p++];
-
- return ~crc;
+ while (len--)
+ crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *p++];
+ return crc;
}
EXPORT_SYMBOL_GPL(crc64_nvme_generic);
--
2.48.1
next prev parent reply other threads:[~2025-01-30 3:54 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-30 3:51 [PATCH v2 00/11] CRC64 library rework and x86 CRC optimization Eric Biggers
2025-01-30 3:51 ` [PATCH v2 01/11] lib/crc64-rocksoft: stop wrapping the crypto API Eric Biggers
2025-02-02 14:26 ` Ard Biesheuvel
2025-01-30 3:51 ` [PATCH v2 02/11] crypto: crc64-rocksoft - remove from " Eric Biggers
2025-02-02 14:27 ` Ard Biesheuvel
2025-01-30 3:51 ` [PATCH v2 03/11] lib/crc64: rename CRC64-Rocksoft to CRC64-NVME Eric Biggers
2025-02-02 14:28 ` Ard Biesheuvel
2025-02-08 18:59 ` Eric Biggers
2025-01-30 3:51 ` [PATCH v2 04/11] lib/crc_kunit.c: add test and benchmark for CRC64-NVME Eric Biggers
2025-02-02 14:29 ` Ard Biesheuvel
2025-01-30 3:51 ` Eric Biggers [this message]
2025-02-02 14:31 ` [PATCH v2 05/11] lib/crc64: add support for arch-optimized implementations Ard Biesheuvel
2025-01-30 3:51 ` [PATCH v2 06/11] x86: move ZMM exclusion list into CPU feature flag Eric Biggers
2025-01-30 3:51 ` [PATCH v2 07/11] scripts/gen-crc-consts: add gen-crc-consts.py Eric Biggers
2025-01-30 3:51 ` [PATCH v2 08/11] x86/crc: add "template" for [V]PCLMULQDQ based CRC functions Eric Biggers
2025-01-30 3:51 ` [PATCH v2 09/11] x86/crc32: implement crc32_le using new template Eric Biggers
2025-01-30 3:51 ` [PATCH v2 10/11] x86/crc-t10dif: implement crc_t10dif " Eric Biggers
2025-01-30 3:51 ` [PATCH v2 11/11] x86/crc64: implement crc64_be and crc64_nvme " Eric Biggers
2025-01-30 15:09 ` [PATCH v2 00/11] CRC64 library rework and x86 CRC optimization Keith Busch
2025-01-30 15:20 ` Martin K. Petersen
2025-02-04 19:54 ` Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250130035130.180676-6-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=ardb@kernel.org \
--cc=kbusch@kernel.org \
--cc=kent.overstreet@linux.dev \
--cc=linux-block@vger.kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.