Skip to contentSkip to conversation

Wire up CRC32 library functions to arch-optimized code

PATCH Superseded Eric Biggers 6 participants 41 messages Oct 25, 2024 – Nov 5, 2024

Subsystems (22)
Arm (32-bit) x86 SCSI PowerPC +18 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

v2 cover Wire up CRC32 library functions to arch-optimized code

This patchset is also available in git via:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git crc32-lib-v2

CRC32 is a family of common non-cryptographic integrity check algorithms that are fairly fast with a portable C implementation and become far faster still with the CRC32 or carryless multiplication instructions that most CPUs have. 9 architectures already have optimized code for at least some CRC32 variants; however, except for arm64 this optimized code was only accessible through the crypto API, not the library functions.

This patchset fixes that so that the CRC32 library functions use the optimized code. This allows users to just use the library instead of the crypto API. This is much simpler and also improves performance due to eliminating the crypto API overhead including an indirect call. Some examples of updating users are included at the end of the patchset.

Note: crc32c() was a weird case. It was a library function layered on top of the crypto API, which in turn is layered on top of the real library functions. So while it was easy to use, it was still subject to the crypto API overhead. This patchset provides CRC32C acceleration in the real library functions directly.

The updated CRC32 library design is:

- Each arch's CRC32 code (all variants) is in arch/$ARCH/lib/crc32*. This adopts what arm64 and riscv already did. Note, the crypto directory is not used because CRC32 is not a cryptographic algorithm.

- Weak symbols are no longer used. Instead there are crc32*_base() and crc32*_arch(), and the appropriate ones are called based on the kconfig. This is similar to how the ChaCha20 library code works.

- Each arch's CRC32 code is enabled by default when CRC32 is enabled, but it can now be disabled, controlled by the choice that previously controlled the base implementation only. It can also now be built as a module if CRC32 is a module too, in which case it will be automatically loaded via direct symbol dependency when appropriate.

- Instead of lots of pointless glue code that wires up each CRC32 variant to the crypto API for each architecture, we now just rely on the existing shash algorithms that use the library functions.

- As before, the library functions don't provide access to off-CPU crypto accelerators. But these appear to have very little, if any, real-world relevance for CRC32 which is very fast on CPUs.

Future work should apply a similar cleanup to crct10dif which is a variant of CRC16.

I tested all arches in QEMU using CONFIG_CRC32_SELFTEST and the crypto self-tests, except for mips which I couldn't figure out how to do.

This patchset has the following dependencies on recent patches, which hopefully will be merged soon through the appropriate trees:

- "crypto - move crypto_simd_disabled_for_test to lib"
(https://lore.kernel.org/linux-crypto/[email protected]/)
- "crypto: x86/crc32c - jump table elimination and other cleanups"
(https://lore.kernel.org/linux-crypto/[email protected]/)
- "arm64: Speed up CRC-32 using PMULL instructions"
(https://lore.kernel.org/linux-crypto/[email protected]/)
- "crypto: Enable fuzz testing for arch code"
(https://lore.kernel.org/linux-crypto/[email protected]/)
- "crypto: mips/crc32 - fix the CRC32C implementation"
(https://lore.kernel.org/linux-crypto/[email protected]/)
- "bcachefs: Explicitly select CRYPTO from BCACHEFS_FS"
(https://lore.kernel.org/linux-bcachefs/[email protected]/)

Everything can be retrieved from git using the command given earlier.

Since this patchset touches many areas, getting it merged may be difficult. One option is a pull request with the whole patchset directly to Linus. Another is to have at least patches 1-4 taken through the crypto tree in v6.13; then the arch patches can land separately afterwards, followed by the rest.

Changed in v2:
- Added a way to determine if the arch-optimized code is actually being used at runtime, and used this to register the appropriate shash algorithms with crypto API.
- Added a patch that converts iSCSI to use the library.
- Listed a bcachefs patch as a dependency.
- Added Ard's Reviewed-by.

Eric Biggers (18):
lib/crc32: drop leading underscores from __crc32c_le_base lib/crc32: improve support for arch-specific overrides lib/crc32: expose whether the lib is really optimized at runtime
crypto: crc32 - don't unnecessarily register arch algorithms arm/crc32: expose CRC32 functions through lib loongarch/crc32: expose CRC32 functions through lib mips/crc32: expose CRC32 functions through lib powerpc/crc32: expose CRC32 functions through lib s390/crc32: expose CRC32 functions through lib sparc/crc32: expose CRC32 functions through lib x86/crc32: update prototype for crc_pcl()
x86/crc32: update prototype for crc32_pclmul_le_16() x86/crc32: expose CRC32 functions through lib lib/crc32: make crc32c() go directly to lib
ext4: switch to using the crc32c library
jbd2: switch to using the crc32c library
f2fs: switch to using the crc32 library
scsi: target: iscsi: switch to using the crc32c library

 arch/arm/Kconfig                              |   1 +
 arch/arm/configs/milbeaut_m10v_defconfig      |   1 -
 arch/arm/configs/multi_v7_defconfig           |   1 -
 arch/arm/crypto/Kconfig                       |  14 -
 arch/arm/crypto/Makefile                      |   2 -
 arch/arm/crypto/crc32-ce-glue.c               | 247 ------------
 arch/arm/lib/Makefile                         |   3 +
 .../crc32-ce-core.S => lib/crc32-core.S}      |   0
 arch/arm/lib/crc32-glue.c                     | 118 ++++++
 arch/arm64/Kconfig                            |   1 +
 arch/arm64/lib/Makefile                       |   3 +-
 arch/arm64/lib/crc32-glue.c                   |  30 +-
 arch/loongarch/Kconfig                        |   1 +
 arch/loongarch/configs/loongson3_defconfig    |   1 -
 arch/loongarch/crypto/Kconfig                 |   9 -
 arch/loongarch/crypto/Makefile                |   2 -
 arch/loongarch/crypto/crc32-loongarch.c       | 300 ---------------
 arch/loongarch/lib/Makefile                   |   2 +
 arch/loongarch/lib/crc32-loongarch.c          | 130 +++++++
 arch/mips/Kconfig                             |   5 +-
 arch/mips/configs/eyeq5_defconfig             |   1 -
 arch/mips/configs/eyeq6_defconfig             |   1 -
 arch/mips/configs/generic/32r6.config         |   2 -
 arch/mips/configs/generic/64r6.config         |   1 -
 arch/mips/crypto/Kconfig                      |   9 -
 arch/mips/crypto/Makefile                     |   2 -
 arch/mips/crypto/crc32-mips.c                 | 354 ------------------
 arch/mips/lib/Makefile                        |   2 +
 arch/mips/lib/crc32-mips.c                    | 187 +++++++++
 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/configs/powernv_defconfig        |   1 -
 arch/powerpc/configs/ppc64_defconfig          |   1 -
 arch/powerpc/crypto/Kconfig                   |  15 +-
 arch/powerpc/crypto/Makefile                  |   2 -
 arch/powerpc/crypto/crc32c-vpmsum_glue.c      | 173 ---------
 arch/powerpc/crypto/crct10dif-vpmsum_asm.S    |   2 +-
 arch/powerpc/lib/Makefile                     |   3 +
 arch/powerpc/lib/crc32-glue.c                 |  86 +++++
 .../{crypto => lib}/crc32-vpmsum_core.S       |   0
 .../{crypto => lib}/crc32c-vpmsum_asm.S       |   0
 arch/riscv/Kconfig                            |   1 +
 arch/riscv/lib/Makefile                       |   3 +-
 arch/riscv/lib/{crc32.c => crc32-riscv.c}     |  30 +-
 arch/s390/Kconfig                             |   1 +
 arch/s390/configs/debug_defconfig             |   1 -
 arch/s390/configs/defconfig                   |   1 -
 arch/s390/crypto/Kconfig                      |  12 -
 arch/s390/crypto/Makefile                     |   2 -
 arch/s390/crypto/crc32-vx.c                   | 306 ---------------
 arch/s390/lib/Makefile                        |   3 +
 arch/s390/lib/crc32-glue.c                    |  86 +++++
 arch/s390/{crypto => lib}/crc32-vx.h          |   0
 arch/s390/{crypto => lib}/crc32be-vx.c        |   0
 arch/s390/{crypto => lib}/crc32le-vx.c        |   0
 arch/sparc/Kconfig                            |   1 +
 arch/sparc/crypto/Kconfig                     |  10 -
 arch/sparc/crypto/Makefile                    |   4 -
 arch/sparc/crypto/crc32c_glue.c               | 184 ---------
 arch/sparc/lib/Makefile                       |   2 +
 arch/sparc/lib/crc32_glue.c                   |  86 +++++
 arch/sparc/{crypto => lib}/crc32c_asm.S       |   2 +-
 arch/x86/Kconfig                              |   1 +
 arch/x86/crypto/Kconfig                       |  22 --
 arch/x86/crypto/Makefile                      |   7 -
 arch/x86/crypto/crc32-pclmul_glue.c           | 202 ----------
 arch/x86/crypto/crc32c-intel_glue.c           | 250 -------------
 arch/x86/lib/Makefile                         |   4 +
 arch/x86/lib/crc32-glue.c                     | 116 ++++++
 .../crc32-pclmul_asm.S => lib/crc32-pclmul.S} |  19 +-
 .../crc32c-3way.S}                            |  63 ++--
 crypto/crc32_generic.c                        |   8 +-
 crypto/crc32c_generic.c                       |  12 +-
 drivers/target/iscsi/Kconfig                  |   3 +-
 drivers/target/iscsi/iscsi_target.c           | 153 +++-----
 drivers/target/iscsi/iscsi_target_login.c     |  50 ---
 drivers/target/iscsi/iscsi_target_login.h     |   1 -
 drivers/target/iscsi/iscsi_target_nego.c      |  21 +-
 fs/ext4/Kconfig                               |   3 +-
 fs/ext4/ext4.h                                |  25 +-
 fs/ext4/super.c                               |  15 -
 fs/f2fs/Kconfig                               |   3 +-
 fs/f2fs/f2fs.h                                |  19 +-
 fs/f2fs/super.c                               |  15 -
 fs/jbd2/Kconfig                               |   2 -
 fs/jbd2/journal.c                             |  25 +-
 include/linux/crc32.h                         |  50 ++-
 include/linux/crc32c.h                        |   7 +-
 include/linux/jbd2.h                          |  31 +-
 include/target/iscsi/iscsi_target_core.h      |   3 -
 lib/Kconfig                                   |  80 ++--
 lib/Makefile                                  |   1 -
 lib/crc32.c                                   |  29 +-
 lib/crc32test.c                               |   2 +-
 lib/libcrc32c.c                               |  74 ----
 94 files changed, 1128 insertions(+), 2637 deletions(-)
 delete mode 100644 arch/arm/crypto/crc32-ce-glue.c
 rename arch/arm/{crypto/crc32-ce-core.S => lib/crc32-core.S} (100%)
 create mode 100644 arch/arm/lib/crc32-glue.c
 delete mode 100644 arch/loongarch/crypto/crc32-loongarch.c
 create mode 100644 arch/loongarch/lib/crc32-loongarch.c
 delete mode 100644 arch/mips/crypto/crc32-mips.c
 create mode 100644 arch/mips/lib/crc32-mips.c
 delete mode 100644 arch/powerpc/crypto/crc32c-vpmsum_glue.c
 create mode 100644 arch/powerpc/lib/crc32-glue.c
 rename arch/powerpc/{crypto => lib}/crc32-vpmsum_core.S (100%)
 rename arch/powerpc/{crypto => lib}/crc32c-vpmsum_asm.S (100%)
 rename arch/riscv/lib/{crc32.c => crc32-riscv.c} (90%)
 delete mode 100644 arch/s390/crypto/crc32-vx.c
 create mode 100644 arch/s390/lib/crc32-glue.c
 rename arch/s390/{crypto => lib}/crc32-vx.h (100%)
 rename arch/s390/{crypto => lib}/crc32be-vx.c (100%)
 rename arch/s390/{crypto => lib}/crc32le-vx.c (100%)
 delete mode 100644 arch/sparc/crypto/crc32c_glue.c
 create mode 100644 arch/sparc/lib/crc32_glue.c
 rename arch/sparc/{crypto => lib}/crc32c_asm.S (92%)
 delete mode 100644 arch/x86/crypto/crc32-pclmul_glue.c
 delete mode 100644 arch/x86/crypto/crc32c-intel_glue.c
 create mode 100644 arch/x86/lib/crc32-glue.c
 rename arch/x86/{crypto/crc32-pclmul_asm.S => lib/crc32-pclmul.S} (95%)
 rename arch/x86/{crypto/crc32c-pcl-intel-asm_64.S => lib/crc32c-3way.S} (92%)
 delete mode 100644 lib/libcrc32c.c
Signature · 1 line
2.47.0
author maintainer
#1 lore raw

PATCH v2 1/18 lib/crc32: drop leading underscores from __crc32c_le_base

From: Eric Biggers <[email protected]>

Remove the leading underscores from __crc32c_le_base().

This is in preparation for adding crc32c_le_arch() and eventually renaming __crc32c_le() to crc32c_le().

---
 arch/arm64/lib/crc32-glue.c | 2 +-
 arch/riscv/lib/crc32.c      | 2 +-
 crypto/crc32c_generic.c     | 8 ++++----
 include/linux/crc32.h       | 2 +-
 lib/crc32.c                 | 4 ++--
 lib/crc32test.c             | 2 +-
 6 files changed, 10 insertions(+), 10 deletions(-)
arch/arm64/lib/crc32-glue.c +1 -1
index 295ae3e6b997..ad015223d15d 100644
@@ -42,11 +42,11 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
 }
 
 u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
 {
 	if (!alternative_has_cap_likely(ARM64_HAS_CRC32))
-		return __crc32c_le_base(crc, p, len);
+		return crc32c_le_base(crc, p, len);
 
 	if (len >= min_len && cpu_have_named_feature(PMULL) && crypto_simd_usable()) {
 		kernel_neon_begin();
 		crc = crc32c_le_arm64_4way(crc, p, len);
 		kernel_neon_end();
arch/riscv/lib/crc32.c +1 -1
index d7dc599af3ef..333fb7af1192 100644
@@ -224,11 +224,11 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
 }
 
 u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, CRC32C_POLY_LE,
-				CRC32C_POLY_QT_LE, __crc32c_le_base);
+				CRC32C_POLY_QT_LE, crc32c_le_base);
 }
 
 static inline u32 crc32_be_unaligned(u32 crc, unsigned char const *p,
 				     size_t len)
 {
crypto/crc32c_generic.c +4 -4
index 7c2357c30fdf..635599b255ec 100644
@@ -83,11 +83,11 @@ static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
 static int chksum_update(struct shash_desc *desc, const u8 *data,
 			 unsigned int length)
 {
 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
 
-	ctx->crc = __crc32c_le_base(ctx->crc, data, length);
+	ctx->crc = crc32c_le_base(ctx->crc, data, length);
 	return 0;
 }
 
 static int chksum_update_arch(struct shash_desc *desc, const u8 *data,
 			      unsigned int length)
@@ -106,11 +106,11 @@ static int chksum_final(struct shash_desc *desc, u8 *out)
 	return 0;
 }
 
 static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
 {
-	put_unaligned_le32(~__crc32c_le_base(*crcp, data, len), out);
+	put_unaligned_le32(~crc32c_le_base(*crcp, data, len), out);
 	return 0;
 }
 
 static int __chksum_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
 			       u8 *out)
@@ -198,16 +198,16 @@ static struct shash_alg algs[] = {{
 }};
 
 static int __init crc32c_mod_init(void)
 {
 	/* register the arch flavor only if it differs from the generic one */
-	return crypto_register_shashes(algs, 1 + (&__crc32c_le != &__crc32c_le_base));
+	return crypto_register_shashes(algs, 1 + (&__crc32c_le != &crc32c_le_base));
 }
 
 static void __exit crc32c_mod_fini(void)
 {
-	crypto_unregister_shashes(algs, 1 + (&__crc32c_le != &__crc32c_le_base));
+	crypto_unregister_shashes(algs, 1 + (&__crc32c_le != &crc32c_le_base));
 }
 
 subsys_initcall(crc32c_mod_init);
 module_exit(crc32c_mod_fini);
 
include/linux/crc32.h +1 -1
index 87f788c0d607..5b07fc9081c4 100644
@@ -37,11 +37,11 @@ static inline u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
 {
 	return crc32_le_shift(crc1, len2) ^ crc2;
 }
 
 u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len);
-u32 __pure __crc32c_le_base(u32 crc, unsigned char const *p, size_t len);
+u32 __pure crc32c_le_base(u32 crc, unsigned char const *p, size_t len);
 
 /**
  * __crc32c_le_combine - Combine two crc32c check values into one. For two
  * 			 sequences of bytes, seq1 and seq2 with lengths len1
  * 			 and len2, __crc32c_le() check values were calculated
lib/crc32.c +2 -2
index ff587fee3893..c67059b0082b 100644
@@ -205,12 +205,12 @@ EXPORT_SYMBOL(crc32_le);
 EXPORT_SYMBOL(__crc32c_le);
 
 u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
 EXPORT_SYMBOL(crc32_le_base);
 
-u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
-EXPORT_SYMBOL(__crc32c_le_base);
+u32 __pure crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
+EXPORT_SYMBOL(crc32c_le_base);
 
 u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
 
 /*
  * This multiplies the polynomials x and y modulo the given modulus.
lib/crc32test.c +1 -1
index 03cf5c1f2f5d..30b8da4d8be4 100644
@@ -824,11 +824,11 @@ static void crc32test_regenerate(void)
 	for (i = 0; i < ARRAY_SIZE(test); i++) {
 		pr_info("{0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x},\n",
 			test[i].crc, test[i].start, test[i].length,
 			crc32_le_base(test[i].crc, test_buf + test[i].start, test[i].length),
 			crc32_be_base(test[i].crc, test_buf + test[i].start, test[i].length),
-			__crc32c_le_base(test[i].crc, test_buf + test[i].start, test[i].length));
+			crc32c_le_base(test[i].crc, test_buf + test[i].start, test[i].length));
 	}
 }
 
 static int __init crc32test_init(void)
 {
Signature · 1 line
2.47.0
author maintainer
#2 lore raw

PATCH v2 2/18 lib/crc32: improve support for arch-specific overrides

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Currently the CRC32 library functions are defined as weak symbols, and the arm64 and riscv architectures override them.

This method of arch-specific overrides has the limitation that it only works when both the base and arch code is built-in. Also, it makes the arch-specific code be silently not used if it is accidentally built with lib-y instead of obj-y; unfortunately the RISC-V code does this.

This commit reorganizes the code to have explicit *_arch() functions that are called when they are enabled, similar to how some of the crypto library code works (e.g. chacha_crypt() calls chacha_crypt_arch()).

Make the existing kconfig choice for the CRC32 implementation also control whether the arch-optimized implementation (if one is available) is enabled or not. Make it enabled by default if CRC32 is also enabled.

The result is that arch-optimized CRC32 library functions will be included automatically when appropriate, but it is now possible to disable them. They can also now be built as a loadable module if the CRC32 library functions happen to be used only by loadable modules, in which case the arch and base CRC32 modules will be automatically loaded via direct symbol dependency when appropriate.

---
 arch/arm64/Kconfig                        |  1 +
 arch/arm64/lib/Makefile                   |  3 +-
 arch/arm64/lib/crc32-glue.c               | 13 ++++-
 arch/riscv/Kconfig                        |  1 +
 arch/riscv/lib/Makefile                   |  3 +-
 arch/riscv/lib/{crc32.c => crc32-riscv.c} | 13 ++++-
 crypto/crc32_generic.c                    |  4 +-
 crypto/crc32c_generic.c                   |  4 +-
 include/linux/crc32.h                     | 35 +++++++++---
 lib/Kconfig                               | 70 +++++++++++++++++------
 lib/crc32.c                               | 22 +++----
 11 files changed, 118 insertions(+), 51 deletions(-)
 rename arch/riscv/lib/{crc32.c => crc32-riscv.c} (94%)
arch/arm64/Kconfig +1 -0
index fd9df6dcc593..1e48f40f654e 100644
@@ -19,10 +19,11 @@ config ARM64
 	select ARCH_ENABLE_MEMORY_HOTPLUG
 	select ARCH_ENABLE_MEMORY_HOTREMOVE
 	select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2
 	select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
 	select ARCH_HAS_CACHE_LINE_SIZE
+	select ARCH_HAS_CRC32
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL
 	select ARCH_HAS_DEBUG_VM_PGTABLE
 	select ARCH_HAS_DMA_OPS if XEN
 	select ARCH_HAS_DMA_PREP_COHERENT
arch/arm64/lib/Makefile +2 -1
index 8e882f479d98..5fbcf0d56665 100644
@@ -11,11 +11,12 @@ CFLAGS_xor-neon.o		+= $(CC_FLAGS_FPU)
 CFLAGS_REMOVE_xor-neon.o	+= $(CC_FLAGS_NO_FPU)
 endif
 
 lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o
 
-obj-$(CONFIG_CRC32) += crc32.o crc32-glue.o
+obj-$(CONFIG_CRC32_ARCH) += crc32-arm64.o
+crc32-arm64-y := crc32.o crc32-glue.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 
 obj-$(CONFIG_ARM64_MTE) += mte.o
 
arch/arm64/lib/crc32-glue.c +10 -3
index ad015223d15d..d7f6e1cbf0d2 100644
@@ -1,9 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <linux/crc32.h>
 #include <linux/linkage.h>
+#include <linux/module.h>
 
 #include <asm/alternative.h>
 #include <asm/cpufeature.h>
 #include <asm/neon.h>
 #include <asm/simd.h>
@@ -19,11 +20,11 @@ asmlinkage u32 crc32_be_arm64(u32 crc, unsigned char const *p, size_t len);
 
 asmlinkage u32 crc32_le_arm64_4way(u32 crc, unsigned char const *p, size_t len);
 asmlinkage u32 crc32c_le_arm64_4way(u32 crc, unsigned char const *p, size_t len);
 asmlinkage u32 crc32_be_arm64_4way(u32 crc, unsigned char const *p, size_t len);
 
-u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_le_arch(u32 crc, const u8 *p, size_t len)
 {
 	if (!alternative_has_cap_likely(ARM64_HAS_CRC32))
 		return crc32_le_base(crc, p, len);
 
 	if (len >= min_len && cpu_have_named_feature(PMULL) && crypto_simd_usable()) {
@@ -38,12 +39,13 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
 			return crc;
 	}
 
 	return crc32_le_arm64(crc, p, len);
 }
+EXPORT_SYMBOL(crc32_le_arch);
 
-u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32c_le_arch(u32 crc, const u8 *p, size_t len)
 {
 	if (!alternative_has_cap_likely(ARM64_HAS_CRC32))
 		return crc32c_le_base(crc, p, len);
 
 	if (len >= min_len && cpu_have_named_feature(PMULL) && crypto_simd_usable()) {
@@ -58,12 +60,13 @@ u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
 			return crc;
 	}
 
 	return crc32c_le_arm64(crc, p, len);
 }
+EXPORT_SYMBOL(crc32c_le_arch);
 
-u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
 {
 	if (!alternative_has_cap_likely(ARM64_HAS_CRC32))
 		return crc32_be_base(crc, p, len);
 
 	if (len >= min_len && cpu_have_named_feature(PMULL) && crypto_simd_usable()) {
@@ -78,5 +81,9 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
 			return crc;
 	}
 
 	return crc32_be_arm64(crc, p, len);
 }
+EXPORT_SYMBOL(crc32_be_arch);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("arm64-optimized CRC32 functions");
arch/riscv/Kconfig +1 -0
index 62545946ecf4..1c32e51eb3a4 100644
@@ -22,10 +22,11 @@ config RISCV
 	select ARCH_ENABLE_MEMORY_HOTPLUG if SPARSEMEM_VMEMMAP
 	select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG
 	select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2
 	select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
 	select ARCH_HAS_BINFMT_FLAT
+	select ARCH_HAS_CRC32 if RISCV_ISA_ZBC
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL if MMU
 	select ARCH_HAS_DEBUG_VM_PGTABLE
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAS_FAST_MULTIPLIER
arch/riscv/lib/Makefile +1 -2
index 8eec6b69a875..79368a895fee 100644
@@ -13,10 +13,9 @@ ifeq ($(CONFIG_MMU), y)
 lib-$(CONFIG_RISCV_ISA_V)	+= uaccess_vector.o
 endif
 lib-$(CONFIG_MMU)	+= uaccess.o
 lib-$(CONFIG_64BIT)	+= tishift.o
 lib-$(CONFIG_RISCV_ISA_ZICBOZ)	+= clear_page.o
-lib-$(CONFIG_RISCV_ISA_ZBC)	+= crc32.o
-
+obj-$(CONFIG_CRC32_ARCH)	+= crc32-riscv.o
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 lib-$(CONFIG_RISCV_ISA_V)	+= xor.o
 lib-$(CONFIG_RISCV_ISA_V)	+= riscv_v_helpers.o
arch/riscv/lib/crc32-riscv.c renamed from arch/riscv/lib/crc32.c +10 -3
similarity index 94%
rename from arch/riscv/lib/crc32.c
rename to arch/riscv/lib/crc32-riscv.c
index 333fb7af1192..a3ff7db2a1ce 100644
@@ -12,10 +12,11 @@
 #include <linux/types.h>
 #include <linux/minmax.h>
 #include <linux/crc32poly.h>
 #include <linux/crc32.h>
 #include <linux/byteorder/generic.h>
+#include <linux/module.h>
 
 /*
  * Refer to https://www.corsix.org/content/barrett-reduction-polynomials for
  * better understanding of how this math works.
  *
@@ -215,21 +216,23 @@ static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
 
 legacy:
 	return crc_fb(crc, p, len);
 }
 
-u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_le_arch(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, CRC32_POLY_LE, CRC32_POLY_QT_LE,
 				crc32_le_base);
 }
+EXPORT_SYMBOL(crc32_le_arch);
 
-u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32c_le_arch(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, CRC32C_POLY_LE,
 				CRC32C_POLY_QT_LE, crc32c_le_base);
 }
+EXPORT_SYMBOL(crc32c_le_arch);
 
 static inline u32 crc32_be_unaligned(u32 crc, unsigned char const *p,
 				     size_t len)
 {
 	size_t bits = len * 8;
@@ -251,11 +254,11 @@ static inline u32 crc32_be_unaligned(u32 crc, unsigned char const *p,
 	crc ^= crc_low;
 
 	return crc;
 }
 
-u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
 {
 	size_t offset, head_len, tail_len;
 	unsigned long const *p_ul;
 	unsigned long s;
 
@@ -290,5 +293,9 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
 	return crc;
 
 legacy:
 	return crc32_be_base(crc, p, len);
 }
+EXPORT_SYMBOL(crc32_be_arch);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Accelerated CRC32 implementation with Zbc extension");
crypto/crc32_generic.c +2 -2
index 6a55d206fab3..cc064ea8240e 100644
@@ -158,16 +158,16 @@ static struct shash_alg algs[] = {{
 }};
 
 static int __init crc32_mod_init(void)
 {
 	/* register the arch flavor only if it differs from the generic one */
-	return crypto_register_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
+	return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
 }
 
 static void __exit crc32_mod_fini(void)
 {
-	crypto_unregister_shashes(algs, 1 + (&crc32_le != &crc32_le_base));
+	crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
 }
 
 subsys_initcall(crc32_mod_init);
 module_exit(crc32_mod_fini);
 
crypto/crc32c_generic.c +2 -2
index 635599b255ec..04b03d825cf4 100644
@@ -198,16 +198,16 @@ static struct shash_alg algs[] = {{
 }};
 
 static int __init crc32c_mod_init(void)
 {
 	/* register the arch flavor only if it differs from the generic one */
-	return crypto_register_shashes(algs, 1 + (&__crc32c_le != &crc32c_le_base));
+	return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
 }
 
 static void __exit crc32c_mod_fini(void)
 {
-	crypto_unregister_shashes(algs, 1 + (&__crc32c_le != &crc32c_le_base));
+	crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
 }
 
 subsys_initcall(crc32c_mod_init);
 module_exit(crc32c_mod_fini);
 
include/linux/crc32.h +28 -7
index 5b07fc9081c4..58c632533b08 100644
@@ -6,14 +6,38 @@
 #define _LINUX_CRC32_H
 
 #include <linux/types.h>
 #include <linux/bitrev.h>
 
-u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len);
-u32 __pure crc32_le_base(u32 crc, unsigned char const *p, size_t len);
-u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len);
-u32 __pure crc32_be_base(u32 crc, unsigned char const *p, size_t len);
+u32 __pure crc32_le_arch(u32 crc, const u8 *p, size_t len);
+u32 __pure crc32_le_base(u32 crc, const u8 *p, size_t len);
+u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len);
+u32 __pure crc32_be_base(u32 crc, const u8 *p, size_t len);
+u32 __pure crc32c_le_arch(u32 crc, const u8 *p, size_t len);
+u32 __pure crc32c_le_base(u32 crc, const u8 *p, size_t len);
+
+static inline u32 __pure crc32_le(u32 crc, const u8 *p, size_t len)
+{
+	if (IS_ENABLED(CONFIG_CRC32_ARCH))
+		return crc32_le_arch(crc, p, len);
+	return crc32_le_base(crc, p, len);
+}
+
+static inline u32 __pure crc32_be(u32 crc, const u8 *p, size_t len)
+{
+	if (IS_ENABLED(CONFIG_CRC32_ARCH))
+		return crc32_be_arch(crc, p, len);
+	return crc32_be_base(crc, p, len);
+}
+
+/* TODO: leading underscores should be dropped once callers have been updated */
+static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
+{
+	if (IS_ENABLED(CONFIG_CRC32_ARCH))
+		return crc32c_le_arch(crc, p, len);
+	return crc32c_le_base(crc, p, len);
+}
 
 /**
  * crc32_le_combine - Combine two crc32 check values into one. For two
  * 		      sequences of bytes, seq1 and seq2 with lengths len1
  * 		      and len2, crc32_le() check values were calculated
@@ -36,13 +60,10 @@ u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len);
 static inline u32 crc32_le_combine(u32 crc1, u32 crc2, size_t len2)
 {
 	return crc32_le_shift(crc1, len2) ^ crc2;
 }
 
-u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len);
-u32 __pure crc32c_le_base(u32 crc, unsigned char const *p, size_t len);
-
 /**
  * __crc32c_le_combine - Combine two crc32c check values into one. For two
  * 			 sequences of bytes, seq1 and seq2 with lengths len1
  * 			 and len2, __crc32c_le() check values were calculated
  * 			 for each, crc1 and crc2.
lib/Kconfig +54 -16

118 lines not on this page · raw message

lib/crc32.c +7 -15
index c67059b0082b..47151624332e 100644
@@ -181,39 +181,31 @@ static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
 #endif
 	return crc;
 }
 
 #if CRC_LE_BITS == 1
-u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_le_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
 }
-u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32c_le_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
 }
 #else
-u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_le_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, crc32table_le, CRC32_POLY_LE);
 }
-u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32c_le_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_le_generic(crc, p, len, crc32ctable_le, CRC32C_POLY_LE);
 }
 #endif
-EXPORT_SYMBOL(crc32_le);
-EXPORT_SYMBOL(__crc32c_le);
-
-u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
 EXPORT_SYMBOL(crc32_le_base);
-
-u32 __pure crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
 EXPORT_SYMBOL(crc32c_le_base);
 
-u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
-
 /*
  * This multiplies the polynomials x and y modulo the given modulus.
  * This follows the "little-endian" CRC convention that the lsbit
  * represents the highest power of x, and the msbit represents x^0.
  */
@@ -333,16 +325,16 @@ static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
 # endif
 	return crc;
 }
 
 #if CRC_BE_BITS == 1
-u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_be_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
 }
 #else
-u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
+u32 __pure crc32_be_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
 }
 #endif
-EXPORT_SYMBOL(crc32_be);
+EXPORT_SYMBOL(crc32_be_base);
Signature · 1 line
2.47.0
author maintainer
#3 lore raw

PATCH v2 3/18 lib/crc32: expose whether the lib is really optimized at runtime

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Make the CRC32 library export some flags that indicate which CRC32 functions are actually executing optimized code at runtime. Set these correctly from the architectures that implement the CRC32 functions.

This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name.

---
 arch/arm64/lib/crc32-glue.c  | 15 +++++++++++++++
 arch/riscv/lib/crc32-riscv.c | 15 +++++++++++++++
 include/linux/crc32.h        | 15 +++++++++++++++
 lib/crc32.c                  |  5 +++++
 4 files changed, 50 insertions(+)
arch/arm64/lib/crc32-glue.c +15 -0
index d7f6e1cbf0d2..16f2b7c04294 100644
@@ -83,7 +83,22 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
 
 	return crc32_be_arm64(crc, p, len);
 }
 EXPORT_SYMBOL(crc32_be_arch);
 
+static int __init crc32_arm64_init(void)
+{
+	if (cpus_have_cap(ARM64_HAS_CRC32))
+		crc32_optimizations = CRC32_LE_OPTIMIZATION |
+				      CRC32_BE_OPTIMIZATION |
+				      CRC32C_OPTIMIZATION;
+	return 0;
+}
+arch_initcall(crc32_arm64_init);
+
+static void __exit crc32_arm64_exit(void)
+{
+}
+module_exit(crc32_arm64_exit);
+
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("arm64-optimized CRC32 functions");
arch/riscv/lib/crc32-riscv.c +15 -0
index a3ff7db2a1ce..a61c13d89364 100644
@@ -295,7 +295,22 @@ u32 __pure crc32_be_arch(u32 crc, const u8 *p, size_t len)
 legacy:
 	return crc32_be_base(crc, p, len);
 }
 EXPORT_SYMBOL(crc32_be_arch);
 
+static int __init crc32_riscv_init(void)
+{
+	if (riscv_has_extension_likely(RISCV_ISA_EXT_ZBC))
+		crc32_optimizations = CRC32_LE_OPTIMIZATION |
+				      CRC32_BE_OPTIMIZATION |
+				      CRC32C_OPTIMIZATION;
+	return 0;
+}
+arch_initcall(crc32_riscv_init);
+
+static void __exit crc32_riscv_exit(void)
+{
+}
+module_exit(crc32_riscv_exit);
+
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Accelerated CRC32 implementation with Zbc extension");
include/linux/crc32.h +15 -0
index 58c632533b08..bf26d454b60d 100644
@@ -35,10 +35,25 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
 	if (IS_ENABLED(CONFIG_CRC32_ARCH))
 		return crc32c_le_arch(crc, p, len);
 	return crc32c_le_base(crc, p, len);
 }
 
+/*
+ * crc32_optimizations contains flags that indicate which CRC32 library
+ * functions are using architecture-specific optimizations.  Unlike
+ * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
+ * variants and also whether any needed CPU features are available at runtime.
+ */
+#define CRC32_LE_OPTIMIZATION	BIT(0) /* crc32_le() is optimized */
+#define CRC32_BE_OPTIMIZATION	BIT(1) /* crc32_be() is optimized */
+#define CRC32C_OPTIMIZATION	BIT(2) /* __crc32c_le() is optimized */
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
+extern u32 crc32_optimizations;
+#else
+#define crc32_optimizations 0
+#endif
+
 /**
  * crc32_le_combine - Combine two crc32 check values into one. For two
  * 		      sequences of bytes, seq1 and seq2 with lengths len1
  * 		      and len2, crc32_le() check values were calculated
  * 		      for each, crc1 and crc2.
lib/crc32.c +5 -0
index 47151624332e..194de73f30f8 100644
@@ -336,5 +336,10 @@ u32 __pure crc32_be_base(u32 crc, const u8 *p, size_t len)
 {
 	return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
 }
 #endif
 EXPORT_SYMBOL(crc32_be_base);
+
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
+u32 crc32_optimizations;
+EXPORT_SYMBOL(crc32_optimizations);
+#endif
Signature · 1 line
2.47.0
#4 lore raw

Re: [PATCH v2 03/18] lib/crc32: expose whether the lib is really optimized at runtime

Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Make the CRC32 library export some flags that indicate which CRC32 functions are actually executing optimized code at runtime. Set these correctly from the architectures that implement the CRC32 functions.

This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name.

---
 arch/arm64/lib/crc32-glue.c  | 15 +++++++++++++++
 arch/riscv/lib/crc32-riscv.c | 15 +++++++++++++++
 include/linux/crc32.h        | 15 +++++++++++++++
 lib/crc32.c                  |  5 +++++
 4 files changed, 50 insertions(+)

...

Quoted 15 lines
include/linux/crc32.h +5 -0
index 58c632533b08..bf26d454b60d 100644
@@ -35,10 +35,25 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
        if (IS_ENABLED(CONFIG_CRC32_ARCH))
                return crc32c_le_arch(crc, p, len);
        return crc32c_le_base(crc, p, len);
 }
 
+/*
+ * crc32_optimizations contains flags that indicate which CRC32 library
+ * functions are using architecture-specific optimizations.  Unlike
+ * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
+ * variants and also whether any needed CPU features are available at runtime.

include/linux/crc32.h static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len) · line 54

Quoted 10 lines
include/linux/crc32.h +10 -0
+ */
+#define CRC32_LE_OPTIMIZATION  BIT(0) /* crc32_le() is optimized */
+#define CRC32_BE_OPTIMIZATION  BIT(1) /* crc32_be() is optimized */
+#define CRC32C_OPTIMIZATION    BIT(2) /* __crc32c_le() is optimized */
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
+extern u32 crc32_optimizations;
+#else
+#define crc32_optimizations 0
+#endif
+

Wouldn't it be cleaner to add a new library function for this, instead of using a global variable?

Quoted 24 lines
include/linux/crc32.h +0 -0
 /**
  * crc32_le_combine - Combine two crc32 check values into one. For two
  *                   sequences of bytes, seq1 and seq2 with lengths len1
  *                   and len2, crc32_le() check values were calculated
  *                   for each, crc1 and crc2.
lib/crc32.c +5 -1
index 47151624332e..194de73f30f8 100644
@@ -336,5 +336,10 @@ u32 __pure crc32_be_base(u32 crc, const u8 *p, size_t len)
 {
        return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
 }
 #endif
 EXPORT_SYMBOL(crc32_be_base);
+
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
+u32 crc32_optimizations;
+EXPORT_SYMBOL(crc32_optimizations);
+#endif
--

2.47.0

author maintainer
#5 lore raw

Re: [PATCH v2 03/18] lib/crc32: expose whether the lib is really optimized at runtime

Quoted 42 lines · Ard Biesheuvel
Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Make the CRC32 library export some flags that indicate which CRC32 functions are actually executing optimized code at runtime. Set these correctly from the architectures that implement the CRC32 functions.

This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name.

---
 arch/arm64/lib/crc32-glue.c  | 15 +++++++++++++++
 arch/riscv/lib/crc32-riscv.c | 15 +++++++++++++++
 include/linux/crc32.h        | 15 +++++++++++++++
 lib/crc32.c                  |  5 +++++
 4 files changed, 50 insertions(+)

...

Quoted 20 lines
include/linux/crc32.h +10 -0
index 58c632533b08..bf26d454b60d 100644
@@ -35,10 +35,25 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
        if (IS_ENABLED(CONFIG_CRC32_ARCH))
                return crc32c_le_arch(crc, p, len);
        return crc32c_le_base(crc, p, len);
 }
 
+/*
+ * crc32_optimizations contains flags that indicate which CRC32 library
+ * functions are using architecture-specific optimizations.  Unlike
+ * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
+ * variants and also whether any needed CPU features are available at runtime.
+ */
+#define CRC32_LE_OPTIMIZATION  BIT(0) /* crc32_le() is optimized */
+#define CRC32_BE_OPTIMIZATION  BIT(1) /* crc32_be() is optimized */
+#define CRC32C_OPTIMIZATION    BIT(2) /* __crc32c_le() is optimized */
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
Quoted 8 lines

include/linux/crc32.h static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len) · line 54

Quoted 5 lines
include/linux/crc32.h +5 -0
+extern u32 crc32_optimizations;
+#else
+#define crc32_optimizations 0
+#endif
+

Wouldn't it be cleaner to add a new library function for this, instead of using a global variable?

The architecture crc32 modules need to be able to write to this. There could be a setter function and a getter function, but just using a variable is simpler.

- Eric

#6 lore raw

Re: [PATCH v2 03/18] lib/crc32: expose whether the lib is really optimized at runtime

Quoted 53 lines · Eric Biggers
Quoted 42 lines · Ard Biesheuvel
Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Make the CRC32 library export some flags that indicate which CRC32 functions are actually executing optimized code at runtime. Set these correctly from the architectures that implement the CRC32 functions.

This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name.

---
 arch/arm64/lib/crc32-glue.c  | 15 +++++++++++++++
 arch/riscv/lib/crc32-riscv.c | 15 +++++++++++++++
 include/linux/crc32.h        | 15 +++++++++++++++
 lib/crc32.c                  |  5 +++++
 4 files changed, 50 insertions(+)

...

Quoted 20 lines
include/linux/crc32.h +10 -0
index 58c632533b08..bf26d454b60d 100644
@@ -35,10 +35,25 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
        if (IS_ENABLED(CONFIG_CRC32_ARCH))
                return crc32c_le_arch(crc, p, len);
        return crc32c_le_base(crc, p, len);
 }
 
+/*
+ * crc32_optimizations contains flags that indicate which CRC32 library
+ * functions are using architecture-specific optimizations.  Unlike
+ * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
+ * variants and also whether any needed CPU features are available at runtime.
+ */
+#define CRC32_LE_OPTIMIZATION  BIT(0) /* crc32_le() is optimized */
+#define CRC32_BE_OPTIMIZATION  BIT(1) /* crc32_be() is optimized */
+#define CRC32C_OPTIMIZATION    BIT(2) /* __crc32c_le() is optimized */
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
Quoted 8 lines

include/linux/crc32.h static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len) · line 54

Quoted 5 lines
include/linux/crc32.h +5 -0
+extern u32 crc32_optimizations;
+#else
+#define crc32_optimizations 0
+#endif
+

Wouldn't it be cleaner to add a new library function for this, instead of using a global variable?

Quoted 3 lines

The architecture crc32 modules need to be able to write to this. There could be a setter function and a getter function, but just using a variable is simpler.

If we just add 'u32 crc32_optimizations()', there is no need for those modules to have init/exit hooks, the only thing they need to export is this routine.

Or perhaps it could even be a static inline, with the right plumbing of header files. At least on arm64,

static inline u32 crc32_optimizations() {
if (!alternative_have_const_cap_likely(ARM64_HAS_CRC32))

    return 0;

return CRC32_LE_OPTIMIZATION | CRC32_BE_OPTIMIZATION | CRC32C_OPTIMIZATION;
}

should be all we need.

author maintainer
#7 lore raw

Re: [PATCH v2 03/18] lib/crc32: expose whether the lib is really optimized at runtime

Quoted 65 lines · Ard Biesheuvel
Quoted 53 lines · Eric Biggers
Quoted 42 lines · Ard Biesheuvel
Quoted 20 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Make the CRC32 library export some flags that indicate which CRC32 functions are actually executing optimized code at runtime. Set these correctly from the architectures that implement the CRC32 functions.

This will be used to determine whether the crc32[c]-$arch shash algorithms should be registered in the crypto API. btrfs could also start using these flags instead of the hack that it currently uses where it parses the crypto_shash_driver_name.

---
 arch/arm64/lib/crc32-glue.c  | 15 +++++++++++++++
 arch/riscv/lib/crc32-riscv.c | 15 +++++++++++++++
 include/linux/crc32.h        | 15 +++++++++++++++
 lib/crc32.c                  |  5 +++++
 4 files changed, 50 insertions(+)

...

Quoted 20 lines
include/linux/crc32.h +10 -0
index 58c632533b08..bf26d454b60d 100644
@@ -35,10 +35,25 @@ static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len)
        if (IS_ENABLED(CONFIG_CRC32_ARCH))
                return crc32c_le_arch(crc, p, len);
        return crc32c_le_base(crc, p, len);
 }
 
+/*
+ * crc32_optimizations contains flags that indicate which CRC32 library
+ * functions are using architecture-specific optimizations.  Unlike
+ * IS_ENABLED(CONFIG_CRC32_ARCH) it takes into account the different CRC32
+ * variants and also whether any needed CPU features are available at runtime.
+ */
+#define CRC32_LE_OPTIMIZATION  BIT(0) /* crc32_le() is optimized */
+#define CRC32_BE_OPTIMIZATION  BIT(1) /* crc32_be() is optimized */
+#define CRC32C_OPTIMIZATION    BIT(2) /* __crc32c_le() is optimized */
+#if IS_ENABLED(CONFIG_CRC32_ARCH)
Quoted 8 lines

include/linux/crc32.h static inline u32 __pure __crc32c_le(u32 crc, const u8 *p, size_t len) · line 54

Quoted 5 lines
include/linux/crc32.h +5 -0
+extern u32 crc32_optimizations;
+#else
+#define crc32_optimizations 0
+#endif
+

Wouldn't it be cleaner to add a new library function for this, instead of using a global variable?

Quoted 3 lines

The architecture crc32 modules need to be able to write to this. There could be a setter function and a getter function, but just using a variable is simpler.

If we just add 'u32 crc32_optimizations()', there is no need for those modules to have init/exit hooks, the only thing they need to export is this routine.

Or perhaps it could even be a static inline, with the right plumbing of header files. At least on arm64,

Quoted 7 lines

static inline u32 crc32_optimizations() {
if (!alternative_have_const_cap_likely(ARM64_HAS_CRC32))

    return 0;

return CRC32_LE_OPTIMIZATION | CRC32_BE_OPTIMIZATION | CRC32C_OPTIMIZATION;
}

should be all we need.

In 7 of the 9 affected arches, I already have a module_init function that checks the CPU features in order to set up static keys. (arm64 and riscv already used alternatives patching, so I kept that.) It's slightly convenient to set these flags at the same time, but yes the above solution would work too.

- Eric

author maintainer
#8 lore raw

PATCH v2 4/18 crypto: crc32 - don't unnecessarily register arch algorithms

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Instead of registering the crc32-$arch and crc32c-$arch algorithms if the arch-specific code was built, only register them when that code was built *and* is not falling back to the base implementation at runtime.

This avoids confusing users like btrfs which checks the shash driver name to determine whether it is crc32c-generic.

(It would also make sense to change btrfs to test the crc32_optimization flags itself, so that it doesn't have to use the weird hack of parsing the driver name. This change still makes sense either way though.)

---
 crypto/crc32_generic.c  | 8 ++++++--
 crypto/crc32c_generic.c | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)
crypto/crc32_generic.c +6 -2
index cc064ea8240e..cecd01e4d6e6 100644
@@ -155,19 +155,23 @@ static struct shash_alg algs[] = {{
 	.base.cra_ctxsize	= sizeof(u32),
 	.base.cra_module	= THIS_MODULE,
 	.base.cra_init		= crc32_cra_init,
 }};
 
+static int num_algs;
+
 static int __init crc32_mod_init(void)
 {
 	/* register the arch flavor only if it differs from the generic one */
-	return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+	num_algs = 1 + ((crc32_optimizations & CRC32_LE_OPTIMIZATION) != 0);
+
+	return crypto_register_shashes(algs, num_algs);
 }
 
 static void __exit crc32_mod_fini(void)
 {
-	crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+	crypto_unregister_shashes(algs, num_algs);
 }
 
 subsys_initcall(crc32_mod_init);
 module_exit(crc32_mod_fini);
 
crypto/crc32c_generic.c +6 -2
index 04b03d825cf4..47d694da9d4a 100644
@@ -195,19 +195,23 @@ static struct shash_alg algs[] = {{
 	.base.cra_ctxsize	= sizeof(struct chksum_ctx),
 	.base.cra_module	= THIS_MODULE,
 	.base.cra_init		= crc32c_cra_init,
 }};
 
+static int num_algs;
+
 static int __init crc32c_mod_init(void)
 {
 	/* register the arch flavor only if it differs from the generic one */
-	return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+	num_algs = 1 + ((crc32_optimizations & CRC32C_OPTIMIZATION) != 0);
+
+	return crypto_register_shashes(algs, num_algs);
 }
 
 static void __exit crc32c_mod_fini(void)
 {
-	crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+	crypto_unregister_shashes(algs, num_algs);
 }
 
 subsys_initcall(crc32c_mod_init);
 module_exit(crc32c_mod_fini);
 
Signature · 1 line
2.47.0
#9 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Instead of registering the crc32-$arch and crc32c-$arch algorithms if the arch-specific code was built, only register them when that code was built *and* is not falling back to the base implementation at runtime.

This avoids confusing users like btrfs which checks the shash driver name to determine whether it is crc32c-generic.

I think we agree that 'generic' specifically means a C implementation that is identical across all architectures, which is why I updated my patch to export -arch instead of wrapping the C code in yet another driver just for the fuzzing tests.

So why is this a problem? If no optimizations are available at runtime, crc32-arch and crc32-generic are interchangeable, and so it shouldn't matter whether you use one or the other.

You can infer from the driver name whether the C code is being used, not whether or not the implementation is 'fast', and the btrfs hack is already broken on arm64.

Quoted 4 lines

(It would also make sense to change btrfs to test the crc32_optimization flags itself, so that it doesn't have to use the weird hack of parsing the driver name. This change still makes sense either way though.)

Indeed. That hack is very dubious and I'd be inclined just to ignore this. On x86 and arm64, it shouldn't make a difference, given that crc32-arch will be 'fast' in the vast majority of cases. On other architectures, btrfs may use the C implementation while assuming it is something faster, and if anyone actually notices the difference, we can work with the btrfs devs to do something more sensible here.

Quoted 70 lines
---
 crypto/crc32_generic.c  | 8 ++++++--
 crypto/crc32c_generic.c | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)
crypto/crc32_generic.c +6 -2
index cc064ea8240e..cecd01e4d6e6 100644
@@ -155,19 +155,23 @@ static struct shash_alg algs[] = {{
        .base.cra_ctxsize       = sizeof(u32),
        .base.cra_module        = THIS_MODULE,
        .base.cra_init          = crc32_cra_init,
 }};
 
+static int num_algs;
+
 static int __init crc32_mod_init(void)
 {
        /* register the arch flavor only if it differs from the generic one */
-       return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+       num_algs = 1 + ((crc32_optimizations & CRC32_LE_OPTIMIZATION) != 0);
+
+       return crypto_register_shashes(algs, num_algs);
 }
 
 static void __exit crc32_mod_fini(void)
 {
-       crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+       crypto_unregister_shashes(algs, num_algs);
 }
 
 subsys_initcall(crc32_mod_init);
 module_exit(crc32_mod_fini);
 
crypto/crc32c_generic.c +6 -3
index 04b03d825cf4..47d694da9d4a 100644
@@ -195,19 +195,23 @@ static struct shash_alg algs[] = {{
        .base.cra_ctxsize       = sizeof(struct chksum_ctx),
        .base.cra_module        = THIS_MODULE,
        .base.cra_init          = crc32c_cra_init,
 }};
 
+static int num_algs;
+
 static int __init crc32c_mod_init(void)
 {
        /* register the arch flavor only if it differs from the generic one */
-       return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+       num_algs = 1 + ((crc32_optimizations & CRC32C_OPTIMIZATION) != 0);
+
+       return crypto_register_shashes(algs, num_algs);
 }
 
 static void __exit crc32c_mod_fini(void)
 {
-       crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+       crypto_unregister_shashes(algs, num_algs);
 }
 
 subsys_initcall(crc32c_mod_init);
 module_exit(crc32c_mod_fini);
 
--

2.47.0

author maintainer
#10 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 30 lines · Ard Biesheuvel
Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Instead of registering the crc32-$arch and crc32c-$arch algorithms if the arch-specific code was built, only register them when that code was built *and* is not falling back to the base implementation at runtime.

This avoids confusing users like btrfs which checks the shash driver name to determine whether it is crc32c-generic.

I think we agree that 'generic' specifically means a C implementation that is identical across all architectures, which is why I updated my patch to export -arch instead of wrapping the C code in yet another driver just for the fuzzing tests.

So why is this a problem? If no optimizations are available at runtime, crc32-arch and crc32-generic are interchangeable, and so it shouldn't matter whether you use one or the other.

You can infer from the driver name whether the C code is being used, not whether or not the implementation is 'fast', and the btrfs hack is already broken on arm64.

Quoted 4 lines

(It would also make sense to change btrfs to test the crc32_optimization flags itself, so that it doesn't have to use the weird hack of parsing the driver name. This change still makes sense either way though.)

Quoted 6 lines

Indeed. That hack is very dubious and I'd be inclined just to ignore this. On x86 and arm64, it shouldn't make a difference, given that crc32-arch will be 'fast' in the vast majority of cases. On other architectures, btrfs may use the C implementation while assuming it is something faster, and if anyone actually notices the difference, we can work with the btrfs devs to do something more sensible here.

Yes, we probably could get away without this. It's never really been appropriate to use the crypto driver names for anything important. And btrfs probably should just assume CRC32C == fast unconditionally, like what it does with xxHash64, or even do a quick benchmark to measure the actual speed of its hash algorithm (which can also be sha256 or blake2b which can be very fast too).

Besides the btrfs case, my concern was there may be advice floating around about checking /proc/crypto to check what optimized code is being used. Having crc32-$arch potentially be running the generic code would make that misleading. It might make sense to keep it working similar to how it did before.

But I do agree that we could probably get away without this.

- Eric

author maintainer
#11 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 44 lines · Eric Biggers
Quoted 30 lines · Ard Biesheuvel
Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Instead of registering the crc32-$arch and crc32c-$arch algorithms if the arch-specific code was built, only register them when that code was built *and* is not falling back to the base implementation at runtime.

This avoids confusing users like btrfs which checks the shash driver name to determine whether it is crc32c-generic.

I think we agree that 'generic' specifically means a C implementation that is identical across all architectures, which is why I updated my patch to export -arch instead of wrapping the C code in yet another driver just for the fuzzing tests.

So why is this a problem? If no optimizations are available at runtime, crc32-arch and crc32-generic are interchangeable, and so it shouldn't matter whether you use one or the other.

You can infer from the driver name whether the C code is being used, not whether or not the implementation is 'fast', and the btrfs hack is already broken on arm64.

Quoted 4 lines

(It would also make sense to change btrfs to test the crc32_optimization flags itself, so that it doesn't have to use the weird hack of parsing the driver name. This change still makes sense either way though.)

Quoted 6 lines

Indeed. That hack is very dubious and I'd be inclined just to ignore this. On x86 and arm64, it shouldn't make a difference, given that crc32-arch will be 'fast' in the vast majority of cases. On other architectures, btrfs may use the C implementation while assuming it is something faster, and if anyone actually notices the difference, we can work with the btrfs devs to do something more sensible here.

Yes, we probably could get away without this. It's never really been appropriate to use the crypto driver names for anything important. And btrfs probably should just assume CRC32C == fast unconditionally, like what it does with xxHash64, or even do a quick benchmark to measure the actual speed of its hash algorithm (which can also be sha256 or blake2b which can be very fast too).

Quoted 6 lines

Besides the btrfs case, my concern was there may be advice floating around about checking /proc/crypto to check what optimized code is being used. Having crc32-$arch potentially be running the generic code would make that misleading. It might make sense to keep it working similar to how it did before.

But I do agree that we could probably get away without this.

While testing this patchset I notice that none of the crypto API drivers for crc32 or crc32c even need to be loaded on my system anymore, as everything on my system that uses those algorithms (such as ext4) just uses the library APIs now. That makes the "check /proc/crypto" trick stop working anyway.

I think you're right that we shouldn't bother with patches 3-4, and I'll plan to go back to leaving them out in the next version, unless someone yells.

- Eric

#12 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 53 lines · Eric Biggers
Quoted 44 lines · Eric Biggers
Quoted 30 lines · Ard Biesheuvel
Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Instead of registering the crc32-$arch and crc32c-$arch algorithms if the arch-specific code was built, only register them when that code was built *and* is not falling back to the base implementation at runtime.

This avoids confusing users like btrfs which checks the shash driver name to determine whether it is crc32c-generic.

I think we agree that 'generic' specifically means a C implementation that is identical across all architectures, which is why I updated my patch to export -arch instead of wrapping the C code in yet another driver just for the fuzzing tests.

So why is this a problem? If no optimizations are available at runtime, crc32-arch and crc32-generic are interchangeable, and so it shouldn't matter whether you use one or the other.

You can infer from the driver name whether the C code is being used, not whether or not the implementation is 'fast', and the btrfs hack is already broken on arm64.

Quoted 4 lines

(It would also make sense to change btrfs to test the crc32_optimization flags itself, so that it doesn't have to use the weird hack of parsing the driver name. This change still makes sense either way though.)

Quoted 6 lines

Indeed. That hack is very dubious and I'd be inclined just to ignore this. On x86 and arm64, it shouldn't make a difference, given that crc32-arch will be 'fast' in the vast majority of cases. On other architectures, btrfs may use the C implementation while assuming it is something faster, and if anyone actually notices the difference, we can work with the btrfs devs to do something more sensible here.

Yes, we probably could get away without this. It's never really been appropriate to use the crypto driver names for anything important. And btrfs probably should just assume CRC32C == fast unconditionally, like what it does with xxHash64, or even do a quick benchmark to measure the actual speed of its hash algorithm (which can also be sha256 or blake2b which can be very fast too).

Quoted 6 lines

Besides the btrfs case, my concern was there may be advice floating around about checking /proc/crypto to check what optimized code is being used. Having crc32-$arch potentially be running the generic code would make that misleading. It might make sense to keep it working similar to how it did before.

But I do agree that we could probably get away without this.

Quoted 8 lines

While testing this patchset I notice that none of the crypto API drivers for crc32 or crc32c even need to be loaded on my system anymore, as everything on my system that uses those algorithms (such as ext4) just uses the library APIs now. That makes the "check /proc/crypto" trick stop working anyway.

I think you're right that we shouldn't bother with patches 3-4, and I'll plan to go back to leaving them out in the next version, unless someone yells.

Agreed.

If we need to make this distinction, it might be cleaner to use the static_call API instead, e.g.,

+DECLARE_STATIC_CALL(crc32_le_arch, crc32_le_base);
+
static inline u32 __pure crc32_le(u32 crc, const u8 *p, size_t len)

 {
        if (IS_ENABLED(CONFIG_CRC32_ARCH))
-               return crc32_le_arch(crc, p, len);
+               return static_call(crc32_le_arch)(crc, p, len);
        return crc32_le_base(crc, p, len);

}

and use static_call_update() to update the target if the feature is supported. Then, we could check in the driver whether the static call points to the default or not:

+static bool have_arch;
+
static int __init crc32_mod_init(void)
{
+ have_arch = IS_ENABLED(CONFIG_CRC32_ARCH) &&
+ static_call_query(crc32_le_arch) != crc32_le_base;
+

        /* register the arch flavor only if it differs from the generic one */

- return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
+ return crypto_register_shashes(algs, 1 + have_arch);
}

maintainer
#13 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

In reply to Eric Biggers
Quoted 5 lines · Eric Biggers

While testing this patchset I notice that none of the crypto API drivers for crc32 or crc32c even need to be loaded on my system anymore, as everything on my system that uses those algorithms (such as ext4) just uses the library APIs now. That makes the "check /proc/crypto" trick stop working anyway.

What's stopping us from removing them altogether?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#14 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 10 lines · Herbert Xu
Quoted 5 lines · Eric Biggers

While testing this patchset I notice that none of the crypto API drivers for crc32 or crc32c even need to be loaded on my system anymore, as everything on my system that uses those algorithms (such as ext4) just uses the library APIs now. That makes the "check /proc/crypto" trick stop working anyway.

What's stopping us from removing them altogether?

At least btrfs supports a variety of checksums/hashes (crc32c, xxhash, sha) via the shash API.

There are some other remaining uses of crc32c using shash or sync ahash where the algo is hardcoded (NVMe, infiniband) so I imagine those might be candidates for conversion as well.

maintainer
#15 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 3 lines · Ard Biesheuvel

At least btrfs supports a variety of checksums/hashes (crc32c, xxhash, sha) via the shash API.

OK, given that btrfs is still doing this, I think we should still register crc32c-arch conditionally. Having it point to crc32c-generic is just confusing (at least if you use btrfs).

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#16 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 10 lines · Herbert Xu
Quoted 3 lines · Ard Biesheuvel

At least btrfs supports a variety of checksums/hashes (crc32c, xxhash, sha) via the shash API.

OK, given that btrfs is still doing this, I think we should still register crc32c-arch conditionally. Having it point to crc32c-generic is just confusing (at least if you use btrfs).

Agreed. So we should take this patch.

The current issue with btrfs is that it will misidentify crc32c-generic on arm64 as being 'slow', but this was already fixed by my patches that are already in cryptodev.

On arm64, crc32 instructions are always available (the only known micro-architecture that omitted them has been obsolete for years), and on x86_64 the situation is similar in practice (introduced in SSE 4.2), and so this patch changes very little for the majority of btrfs users.

But on architectures such as 32-bit ARM, where these instructions are only available if you are booting a 32-bit kernel on a 64-bit CPU (which is more common than you might think), this patch will ensure that crc32-arm / crc32c-arm are only registered if the instructions are actually available, and btrfs will take the slow async patch for checksumming if they are not. (I seriously doubt that btrfs on 32-bit ARM is a thing but who knows)

#17 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 25 lines · Ard Biesheuvel
Quoted 10 lines · Herbert Xu
Quoted 3 lines · Ard Biesheuvel

At least btrfs supports a variety of checksums/hashes (crc32c, xxhash, sha) via the shash API.

OK, given that btrfs is still doing this, I think we should still register crc32c-arch conditionally. Having it point to crc32c-generic is just confusing (at least if you use btrfs).

Agreed. So we should take this patch.

The current issue with btrfs is that it will misidentify crc32c-generic on arm64 as being 'slow', but this was already fixed by my patches that are already in cryptodev.

On arm64, crc32 instructions are always available (the only known micro-architecture that omitted them has been obsolete for years), and on x86_64 the situation is similar in practice (introduced in SSE 4.2), and so this patch changes very little for the majority of btrfs users.

Quoted 7 lines

But on architectures such as 32-bit ARM, where these instructions are only available if you are booting a 32-bit kernel on a 64-bit CPU (which is more common than you might think), this patch will ensure that crc32-arm / crc32c-arm are only registered if the instructions are actually available, and btrfs will take the slow async patch for checksumming if they are not. (I seriously doubt that btrfs on 32-bit ARM is a thing but who knows)

(actually, backpedalling a little bit - apologies)

OTOH,btrfs is the only user where this makes a difference, and its use of the driver name is highly questionable IMO. On x86, it shouldn't make a difference in practice, on arm64, it was broken for a long time, and on the remaining architectures, I seriously doubt that anyone cares about this, and so we can fix this properly if there is a need.

The only issue resulting from *not* taking this patch is that btrfs may misidentify the CRC32 implementation as being 'slow' and take an alternative code path, which does not necessarily result in worse performance.

And I'd prefer static_call() / static_call_query() over a separate global variable to keep track of whether or not the generic code is in use.

maintainer
#18 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 5 lines · Ard Biesheuvel

The only issue resulting from *not* taking this patch is that btrfs may misidentify the CRC32 implementation as being 'slow' and take an alternative code path, which does not necessarily result in worse performance.

If we were removing crc32* (or at least crc32*-arch) from the Crypto API then these patches would be redundant. But if we're keeping them because btrfs uses them then we should definitely make crc32*-arch do the right thing. IOW they should not be registered if they're the same as crc32*-generic.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#19 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 7 lines · Herbert Xu
Quoted 5 lines · Ard Biesheuvel

The only issue resulting from *not* taking this patch is that btrfs may misidentify the CRC32 implementation as being 'slow' and take an alternative code path, which does not necessarily result in worse performance.

Quoted 7 lines

If we were removing crc32* (or at least crc32*-arch) from the Crypto API then these patches would be redundant. But if we're keeping them because btrfs uses them then we should definitely make crc32*-arch do the right thing. IOW they should not be registered if they're the same as crc32*-generic.

Thanks,

I would like to eventually remove crc32 and crc32c from the crypto API, but it will take some time to get all the users converted. If there are AF_ALG users it could even be impossible, though the usual culprit, iwd, doesn't appear to use any CRCs, so hopefully we are fine there.

I will plan to keep this patch, but change it to use a crc32_optimizations() function instead which was Ard's first suggestion.

I don't think Ard's static_call suggestion would work as-is, since considering the following:

    static inline u32 __pure crc32_le(u32 crc, const u8 *p, size_t len)
    {
            if (IS_ENABLED(CONFIG_CRC32_ARCH))
                    return static_call(crc32_le_arch)(crc, p, len);
            return crc32_le_base(crc, p, len);
    }

... the 'static_call(crc32_le_arch)(crc, p, len)' will be inlined into every user, which could be a loadable module which gets loaded after crc32-${arch}.ko. And AFAIK, static calls in that module won't be updated in that case.

That could be avoided by making crc32_le() a non-inline function in lib/crc32.c, so the static call would only be in that one place. That has the slight disadvantage that it would introduce an extra jump into the common case where the optimized function is enabled. Considering that some users are passing small amounts of data into the CRC functions (e.g., 4 bytes), I would like to minimize the overhead as much as possible.

It could also be avoided by making CRC32 and CRC32_ARCH bool rather than tristate. I would prefer not to do that, since there can be situations where only loadable modules need these functions so they should not have to be built into the core kernel.

So I plan to go with the crc32_optimizations() solution in v3.

- Eric

#20 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

Quoted 35 lines · Eric Biggers
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Ard Biesheuvel

The only issue resulting from *not* taking this patch is that btrfs may misidentify the CRC32 implementation as being 'slow' and take an alternative code path, which does not necessarily result in worse performance.

Quoted 7 lines

If we were removing crc32* (or at least crc32*-arch) from the Crypto API then these patches would be redundant. But if we're keeping them because btrfs uses them then we should definitely make crc32*-arch do the right thing. IOW they should not be registered if they're the same as crc32*-generic.

Thanks,

I would like to eventually remove crc32 and crc32c from the crypto API, but it will take some time to get all the users converted. If there are AF_ALG users it could even be impossible, though the usual culprit, iwd, doesn't appear to use any CRCs, so hopefully we are fine there.

I will plan to keep this patch, but change it to use a crc32_optimizations() function instead which was Ard's first suggestion.

I don't think Ard's static_call suggestion would work as-is, since considering the following:

    static inline u32 __pure crc32_le(u32 crc, const u8 *p, size_t len)
    {
            if (IS_ENABLED(CONFIG_CRC32_ARCH))
                    return static_call(crc32_le_arch)(crc, p, len);
            return crc32_le_base(crc, p, len);
    }
Quoted 4 lines

... the 'static_call(crc32_le_arch)(crc, p, len)' will be inlined into every user, which could be a loadable module which gets loaded after crc32-${arch}.ko. And AFAIK, static calls in that module won't be updated in that case.

Any call to static_call_update() will update all existing users, so this should work as expected.

(Only x86 has a non-trivial implementation that patches callers inline
- otherwise, it is either an indirect call involving a global function pointer variable, or a single trampoline that gets patched to point somewhere else)

...

Quoted 3 lines

So I plan to go with the crc32_optimizations() solution in v3.

That is also fine with me.

#21 lore raw

Re: [PATCH v2 04/18] crypto: crc32 - don't unnecessarily register arch algorithms

In reply to Eric Biggers
Quoted 14 lines · Eric Biggers
Quoted 13 lines · Herbert Xu
Quoted 5 lines · Ard Biesheuvel

The only issue resulting from *not* taking this patch is that btrfs may misidentify the CRC32 implementation as being 'slow' and take an alternative code path, which does not necessarily result in worse performance.

If we were removing crc32* (or at least crc32*-arch) from the Crypto API then these patches would be redundant. But if we're keeping them because btrfs uses them then we should definitely make crc32*-arch do the right thing. IOW they should not be registered if they're the same as crc32*-generic.

Quoted 6 lines
Quoted 1 line

Thanks,

I would like to eventually remove crc32 and crc32c from the crypto API, but it will take some time to get all the users converted. If there are AF_ALG users it could even be impossible, though the usual culprit, iwd, doesn't appear to use any CRCs, so hopefully we are fine there.

Hi,

Please do not forget about dm-integrity, it can use crc32/crc32c for non-cryptographic integrity through crypto API.
To test it, cryptsetup testsuite should cover these variants (integrity-compat-test).

Also, libcryptsetup can be compiled with userspace kernel crypto (AF_ALG) as a crypto backend. At least, I think we never used CRC32 though AF_ALG from userspace there...

Thanks,
Milan

author maintainer
#22 lore raw

PATCH v2 5/18 arm/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the arm CRC32 assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/arm/crypto/crc32-ce-glue.c to arch/arm/lib/crc32-glue.c, view this commit with 'git show -M10'.

---
 arch/arm/Kconfig                              |   1 +
 arch/arm/configs/milbeaut_m10v_defconfig      |   1 -
 arch/arm/configs/multi_v7_defconfig           |   1 -
 arch/arm/crypto/Kconfig                       |  14 -
 arch/arm/crypto/Makefile                      |   2 -
 arch/arm/crypto/crc32-ce-glue.c               | 247 ------------------
 arch/arm/lib/Makefile                         |   3 +
 .../crc32-ce-core.S => lib/crc32-core.S}      |   0
 arch/arm/lib/crc32-glue.c                     | 118 +++++++++
 9 files changed, 122 insertions(+), 265 deletions(-)
 delete mode 100644 arch/arm/crypto/crc32-ce-glue.c
 rename arch/arm/{crypto/crc32-ce-core.S => lib/crc32-core.S} (100%)
 create mode 100644 arch/arm/lib/crc32-glue.c
arch/arm/Kconfig +1 -0
index 749179a1d162..851260303234 100644
@@ -5,10 +5,11 @@ config ARM
 	select ARCH_32BIT_OFF_T
 	select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES && FRAME_POINTER && !ARM_UNWIND
 	select ARCH_HAS_BINFMT_FLAT
 	select ARCH_HAS_CPU_CACHE_ALIASING
 	select ARCH_HAS_CPU_FINALIZE_INIT if MMU
+	select ARCH_HAS_CRC32 if KERNEL_MODE_NEON
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL if MMU
 	select ARCH_HAS_DMA_ALLOC if MMU
 	select ARCH_HAS_DMA_OPS
 	select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE
arch/arm/configs/milbeaut_m10v_defconfig +0 -1
index f5eeac9c65c3..acd16204f8d7 100644
@@ -105,11 +105,10 @@ CONFIG_CRYPTO_SHA2_ARM_CE=m
 CONFIG_CRYPTO_SHA512_ARM=m
 CONFIG_CRYPTO_AES_ARM=m
 CONFIG_CRYPTO_AES_ARM_BS=m
 CONFIG_CRYPTO_AES_ARM_CE=m
 CONFIG_CRYPTO_CHACHA20_NEON=m
-CONFIG_CRYPTO_CRC32_ARM_CE=m
 # CONFIG_CRYPTO_HW is not set
 CONFIG_CRC_CCITT=m
 CONFIG_CRC_ITU_T=m
 CONFIG_DMA_CMA=y
 CONFIG_CMA_SIZE_MBYTES=64
arch/arm/configs/multi_v7_defconfig +0 -1
index 9a5f5c439b87..287ca055965f 100644
@@ -1304,11 +1304,10 @@ CONFIG_CRYPTO_SHA2_ARM_CE=m
 CONFIG_CRYPTO_SHA512_ARM=m
 CONFIG_CRYPTO_AES_ARM=m
 CONFIG_CRYPTO_AES_ARM_BS=m
 CONFIG_CRYPTO_AES_ARM_CE=m
 CONFIG_CRYPTO_CHACHA20_NEON=m
-CONFIG_CRYPTO_CRC32_ARM_CE=m
 CONFIG_CRYPTO_DEV_SUN4I_SS=m
 CONFIG_CRYPTO_DEV_FSL_CAAM=m
 CONFIG_CRYPTO_DEV_EXYNOS_RNG=m
 CONFIG_CRYPTO_DEV_S5P=m
 CONFIG_CRYPTO_DEV_ATMEL_AES=m
arch/arm/crypto/Kconfig +0 -14
index 5ff49a5e9afc..ea0ebf336d0d 100644
@@ -220,24 +220,10 @@ config CRYPTO_CHACHA20_NEON
 	  stream cipher algorithms
 
 	  Architecture: arm using:
 	  - NEON (Advanced SIMD) extensions
 
-config CRYPTO_CRC32_ARM_CE
-	tristate "CRC32C and CRC32"
-	depends on KERNEL_MODE_NEON
-	depends on CRC32
-	select CRYPTO_HASH
-	help
-	  CRC32c CRC algorithm with the iSCSI polynomial (RFC 3385 and RFC 3720)
-	  and CRC32 CRC algorithm (IEEE 802.3)
-
-	  Architecture: arm using:
-	  - CRC and/or PMULL instructions
-
-	  Drivers: crc32-arm-ce and crc32c-arm-ce
-
 config CRYPTO_CRCT10DIF_ARM_CE
 	tristate "CRCT10DIF"
 	depends on KERNEL_MODE_NEON
 	depends on CRC_T10DIF
 	select CRYPTO_HASH
arch/arm/crypto/Makefile +0 -2
index 13e62c7c25dc..38ec5cc1e844 100644
@@ -19,11 +19,10 @@ obj-$(CONFIG_CRYPTO_CURVE25519_NEON) += curve25519-neon.o
 obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
 obj-$(CONFIG_CRYPTO_SHA1_ARM_CE) += sha1-arm-ce.o
 obj-$(CONFIG_CRYPTO_SHA2_ARM_CE) += sha2-arm-ce.o
 obj-$(CONFIG_CRYPTO_GHASH_ARM_CE) += ghash-arm-ce.o
 obj-$(CONFIG_CRYPTO_CRCT10DIF_ARM_CE) += crct10dif-arm-ce.o
-obj-$(CONFIG_CRYPTO_CRC32_ARM_CE) += crc32-arm-ce.o
 
 aes-arm-y	:= aes-cipher-core.o aes-cipher-glue.o
 aes-arm-bs-y	:= aes-neonbs-core.o aes-neonbs-glue.o
 sha1-arm-y	:= sha1-armv4-large.o sha1_glue.o
 sha1-arm-neon-y	:= sha1-armv7-neon.o sha1_neon_glue.o
@@ -36,11 +35,10 @@ blake2b-neon-y  := blake2b-neon-core.o blake2b-neon-glue.o
 sha1-arm-ce-y	:= sha1-ce-core.o sha1-ce-glue.o
 sha2-arm-ce-y	:= sha2-ce-core.o sha2-ce-glue.o
 aes-arm-ce-y	:= aes-ce-core.o aes-ce-glue.o
 ghash-arm-ce-y	:= ghash-ce-core.o ghash-ce-glue.o
 crct10dif-arm-ce-y	:= crct10dif-ce-core.o crct10dif-ce-glue.o
-crc32-arm-ce-y:= crc32-ce-core.o crc32-ce-glue.o
 chacha-neon-y := chacha-scalar-core.o chacha-glue.o
 chacha-neon-$(CONFIG_KERNEL_MODE_NEON) += chacha-neon-core.o
 poly1305-arm-y := poly1305-core.o poly1305-glue.o
 nhpoly1305-neon-y := nh-neon-core.o nhpoly1305-neon-glue.o
 curve25519-neon-y := curve25519-core.o curve25519-glue.o
arch/arm/crypto/crc32-ce-glue.c deleted +0 -247

247 lines not on this page · raw message

arch/arm/lib/Makefile +3 -0
index 0ca5aae1bcc3..01cd4db2ed47 100644
@@ -43,5 +43,8 @@ ifeq ($(CONFIG_KERNEL_MODE_NEON),y)
   CFLAGS_xor-neon.o		+= $(CC_FLAGS_FPU)
   obj-$(CONFIG_XOR_BLOCKS)	+= xor-neon.o
 endif
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
+
+obj-$(CONFIG_CRC32_ARCH) += crc32-arm.o
+crc32-arm-y := crc32-glue.o crc32-core.o
arch/arm/lib/crc32-core.S renamed from arch/arm/crypto/crc32-ce-core.S +0 -0
similarity index 100%
rename from arch/arm/crypto/crc32-ce-core.S
rename to arch/arm/lib/crc32-core.S
arch/arm/lib/crc32-glue.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#23 lore raw

PATCH v2 6/18 loongarch/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the loongarch CRC32 assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/loongarch/crypto/crc32-loongarch.c to arch/loongarch/lib/crc32-loongarch.c, view this commit with 'git show -M10'.

---
 arch/loongarch/Kconfig                     |   1 +
 arch/loongarch/configs/loongson3_defconfig |   1 -
 arch/loongarch/crypto/Kconfig              |   9 -
 arch/loongarch/crypto/Makefile             |   2 -
 arch/loongarch/crypto/crc32-loongarch.c    | 300 ---------------------
 arch/loongarch/lib/Makefile                |   2 +
 arch/loongarch/lib/crc32-loongarch.c       | 130 +++++++++
 7 files changed, 133 insertions(+), 312 deletions(-)
 delete mode 100644 arch/loongarch/crypto/crc32-loongarch.c
 create mode 100644 arch/loongarch/lib/crc32-loongarch.c
arch/loongarch/Kconfig +1 -0
index bb35c34f86d2..455f1af0bf88 100644
@@ -13,10 +13,11 @@ config LOONGARCH
 	select ARCH_ENABLE_MEMORY_HOTPLUG
 	select ARCH_ENABLE_MEMORY_HOTREMOVE
 	select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
 	select ARCH_HAS_CPU_FINALIZE_INIT
+	select ARCH_HAS_CRC32
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VM_PGTABLE
 	select ARCH_HAS_FAST_MULTIPLIER
 	select ARCH_HAS_FORTIFY_SOURCE
 	select ARCH_HAS_KCOV
arch/loongarch/configs/loongson3_defconfig +0 -1
index 75b366407a60..0487ac21b38b 100644
@@ -967,11 +967,10 @@ CONFIG_CRYPTO_LZ4=m
 CONFIG_CRYPTO_LZ4HC=m
 CONFIG_CRYPTO_USER_API_HASH=m
 CONFIG_CRYPTO_USER_API_SKCIPHER=m
 CONFIG_CRYPTO_USER_API_RNG=m
 CONFIG_CRYPTO_USER_API_AEAD=m
-CONFIG_CRYPTO_CRC32_LOONGARCH=m
 CONFIG_CRYPTO_DEV_VIRTIO=m
 CONFIG_DMA_CMA=y
 CONFIG_DMA_NUMA_CMA=y
 CONFIG_CMA_SIZE_MBYTES=0
 CONFIG_PRINTK_TIME=y
arch/loongarch/crypto/Kconfig +0 -9
index 200a6e8b43b1..a0270b3e5b30 100644
@@ -1,14 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
 menu "Accelerated Cryptographic Algorithms for CPU (loongarch)"
 
-config CRYPTO_CRC32_LOONGARCH
-	tristate "CRC32c and CRC32"
-	select CRC32
-	select CRYPTO_HASH
-	help
-	  CRC32c and CRC32 CRC algorithms
-
-	  Architecture: LoongArch with CRC32 instructions
-
 endmenu
arch/loongarch/crypto/Makefile +0 -2
index d22613d27ce9..ba83755dde2b 100644
@@ -1,6 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 #
 # Makefile for LoongArch crypto files..
 #
-
-obj-$(CONFIG_CRYPTO_CRC32_LOONGARCH) += crc32-loongarch.o
arch/loongarch/crypto/crc32-loongarch.c deleted +0 -300

300 lines not on this page · raw message

arch/loongarch/lib/Makefile +2 -0
index ccea3bbd4353..fae77809048b 100644
@@ -9,5 +9,7 @@ lib-y	+= delay.o memset.o memcpy.o memmove.o \
 obj-$(CONFIG_ARCH_SUPPORTS_INT128) += tishift.o
 
 obj-$(CONFIG_CPU_HAS_LSX) += xor_simd.o xor_simd_glue.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
+
+obj-$(CONFIG_CRC32_ARCH) += crc32-loongarch.o
arch/loongarch/lib/crc32-loongarch.c new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.47.0
#24 lore raw

Re: [PATCH v2 06/18] loongarch/crc32: expose CRC32 functions through lib

Even though the narrower CRC instructions doesn't require GRLEN=64, they still *aren't* part of LA32 (LoongArch reference manual v1.10, Volume 1, Table 2-1).

Therefore, we could not directly add ARCH_HAS_CRC32 to config LOONGARCH.

Thanks,
--
WangYuli

author maintainer
#25 lore raw

Re: [PATCH v2 06/18] loongarch/crc32: expose CRC32 functions through lib

Quoted 5 lines · WangYuli

Even though the narrower CRC instructions doesn't require GRLEN=64, they still *aren't* part of LA32 (LoongArch reference manual v1.10, Volume 1, Table 2-1).

Therefore, we could not directly add ARCH_HAS_CRC32 to config LOONGARCH.

There's still a runtime CPU feature check of cpu_has(CPU_FEATURE_CRC32). See arch/loongarch/lib/crc32-loongarch.c. So it's the same as before. ARCH_HAS_CRC32 just means that the file will be compiled.

If you're trying to say that you think this file should be built only when CONFIG_64BIT=y, then that would be an existing bug since the existing file arch/loongarch/crypto/crc32-loongarch.c was built for both 32-bit and 64-bit. But if you think this is a bug, I can fix this too.

- Eric

Reviewed-by
#26 lore raw

Re: [PATCH v2 06/18] loongarch/crc32: expose CRC32 functions through lib

Quoted 10 lines · Eric Biggers
Quoted 5 lines · WangYuli

Even though the narrower CRC instructions doesn't require GRLEN=64, they still *aren't* part of LA32 (LoongArch reference manual v1.10, Volume 1, Table 2-1).

Therefore, we could not directly add ARCH_HAS_CRC32 to config LOONGARCH.

There's still a runtime CPU feature check of cpu_has(CPU_FEATURE_CRC32). See arch/loongarch/lib/crc32-loongarch.c. So it's the same as before. ARCH_HAS_CRC32 just means that the file will be compiled.

Quoted 7 lines

If you're trying to say that you think this file should be built only when CONFIG_64BIT=y, then that would be an existing bug since the existing file arch/loongarch/crypto/crc32-loongarch.c was built for both 32-bit and 64-bit. But if you think this is a bug, I can fix this too.

- Eric

Actually, my originally mean is that directly declaring LoongArch ARCH_HAS_CRC32 without distinguishing between 32-bit and 64-bit might mislead those reading the code. And it's not rigorous. However, according to Huacai Chen's recent reply, there are many similar issues and they won't cause build errors for now.

So, this change should be fine for now.

Thanks,

Signature · 1 line
WangYuli
author maintainer
#27 lore raw

PATCH v2 7/18 mips/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the mips CRC32 assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/mips/crypto/crc32-mips.c to arch/mips/lib/crc32-mips.c, view this commit with 'git show -M10'.

---
 arch/mips/Kconfig                     |   5 +-
 arch/mips/configs/eyeq5_defconfig     |   1 -
 arch/mips/configs/eyeq6_defconfig     |   1 -
 arch/mips/configs/generic/32r6.config |   2 -
 arch/mips/configs/generic/64r6.config |   1 -
 arch/mips/crypto/Kconfig              |   9 -
 arch/mips/crypto/Makefile             |   2 -
 arch/mips/crypto/crc32-mips.c         | 354 --------------------------
 arch/mips/lib/Makefile                |   2 +
 arch/mips/lib/crc32-mips.c            | 187 ++++++++++++++
 10 files changed, 190 insertions(+), 374 deletions(-)
 delete mode 100644 arch/mips/crypto/crc32-mips.c
 create mode 100644 arch/mips/lib/crc32-mips.c
arch/mips/Kconfig +1 -4
index 397edf05dd72..f80ea80d792f 100644
@@ -1993,15 +1993,15 @@ config CPU_MIPSR5
 	select MIPS_SPRAM
 
 config CPU_MIPSR6
 	bool
 	default y if CPU_MIPS32_R6 || CPU_MIPS64_R6
+	select ARCH_HAS_CRC32
 	select CPU_HAS_RIXI
 	select CPU_HAS_DIEI if !CPU_DIEI_BROKEN
 	select HAVE_ARCH_BITREVERSE
 	select MIPS_ASID_BITS_VARIABLE
-	select MIPS_CRC_SUPPORT
 	select MIPS_SPRAM
 
 config TARGET_ISA_REV
 	int
 	default 1 if CPU_MIPSR1
@@ -2473,13 +2473,10 @@ config MIPS_ASID_BITS
 	default 8
 
 config MIPS_ASID_BITS_VARIABLE
 	bool
 
-config MIPS_CRC_SUPPORT
-	bool
-
 # R4600 erratum.  Due to the lack of errata information the exact
 # technical details aren't known.  I've experimentally found that disabling
 # interrupts during indexed I-cache flushes seems to be sufficient to deal
 # with the issue.
 config WAR_R4600_V1_INDEX_ICACHEOP
arch/mips/configs/eyeq5_defconfig +0 -1
index ae9a09b16e40..ff7af5dc6d9d 100644
@@ -97,11 +97,10 @@ CONFIG_NFS_FS=y
 CONFIG_NFS_V3_ACL=y
 CONFIG_NFS_V4=y
 CONFIG_NFS_V4_1=y
 CONFIG_NFS_V4_2=y
 CONFIG_ROOT_NFS=y
-CONFIG_CRYPTO_CRC32_MIPS=y
 CONFIG_FRAME_WARN=1024
 CONFIG_DEBUG_FS=y
 # CONFIG_RCU_TRACE is not set
 # CONFIG_FTRACE is not set
 CONFIG_CMDLINE_BOOL=y
arch/mips/configs/eyeq6_defconfig +0 -1
index 6597d5e88b33..0afbb45a78e8 100644
@@ -100,11 +100,10 @@ CONFIG_NFS_FS=y
 CONFIG_NFS_V3_ACL=y
 CONFIG_NFS_V4=y
 CONFIG_NFS_V4_1=y
 CONFIG_NFS_V4_2=y
 CONFIG_ROOT_NFS=y
-CONFIG_CRYPTO_CRC32_MIPS=y
 CONFIG_FRAME_WARN=1024
 CONFIG_DEBUG_FS=y
 # CONFIG_RCU_TRACE is not set
 # CONFIG_FTRACE is not set
 CONFIG_CMDLINE_BOOL=y
arch/mips/configs/generic/32r6.config +0 -2
index 1a5d5ea4ab2b..ca606e71f4d0 100644
@@ -1,4 +1,2 @@
 CONFIG_CPU_MIPS32_R6=y
 CONFIG_HIGHMEM=y
-
-CONFIG_CRYPTO_CRC32_MIPS=y
arch/mips/configs/generic/64r6.config +0 -1
index 63b4e95f303d..23a300914957 100644
@@ -2,7 +2,6 @@ CONFIG_CPU_MIPS64_R6=y
 CONFIG_64BIT=y
 CONFIG_MIPS32_O32=y
 CONFIG_MIPS32_N32=y
 
 CONFIG_CPU_HAS_MSA=y
-CONFIG_CRYPTO_CRC32_MIPS=y
 CONFIG_VIRTUALIZATION=y
arch/mips/crypto/Kconfig +0 -9
index 9003a5c1e879..7decd40c4e20 100644
@@ -1,18 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 
 menu "Accelerated Cryptographic Algorithms for CPU (mips)"
 
-config CRYPTO_CRC32_MIPS
-	tristate "CRC32c and CRC32"
-	depends on MIPS_CRC_SUPPORT
-	select CRYPTO_HASH
-	help
-	  CRC32c and CRC32 CRC algorithms
-
-	  Architecture: mips
-
 config CRYPTO_POLY1305_MIPS
 	tristate "Hash functions: Poly1305"
 	depends on MIPS
 	select CRYPTO_ARCH_HAVE_LIB_POLY1305
 	help
arch/mips/crypto/Makefile +0 -2
index 5e4105cccf9f..fddc88281412 100644
@@ -1,12 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0
 #
 # Makefile for MIPS crypto files..
 #
 
-obj-$(CONFIG_CRYPTO_CRC32_MIPS) += crc32-mips.o
-
 obj-$(CONFIG_CRYPTO_CHACHA_MIPS) += chacha-mips.o
 chacha-mips-y := chacha-core.o chacha-glue.o
 AFLAGS_chacha-core.o += -O2 # needed to fill branch delay slots
 
 obj-$(CONFIG_CRYPTO_POLY1305_MIPS) += poly1305-mips.o
arch/mips/crypto/crc32-mips.c deleted +0 -354

354 lines not on this page · raw message

arch/mips/lib/Makefile +2 -0
index 5d5b993cbc2b..9c024e6d5e54 100644
@@ -12,7 +12,9 @@ obj-$(CONFIG_PCI)	+= iomap-pci.o
 lib-$(CONFIG_GENERIC_CSUM)	:= $(filter-out csum_partial.o, $(lib-y))
 
 obj-$(CONFIG_CPU_GENERIC_DUMP_TLB) += dump_tlb.o
 obj-$(CONFIG_CPU_R3000)		+= r3k_dump_tlb.o
 
+obj-$(CONFIG_CRC32_ARCH)	+= crc32-mips.o
+
 # libgcc-style stuff needed in the kernel
 obj-y += bswapsi.o bswapdi.o multi3.o
arch/mips/lib/crc32-mips.c new file +187 -0

187 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#28 lore raw

PATCH v2 8/18 powerpc/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the powerpc CRC32C assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/powerpc/crypto/crc32c-vpmsum_glue.c to arch/powerpc/lib/crc32-glue.c, view this commit with 'git show -M10'.

---
 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/configs/powernv_defconfig        |   1 -
 arch/powerpc/configs/ppc64_defconfig          |   1 -
 arch/powerpc/crypto/Kconfig                   |  15 +-
 arch/powerpc/crypto/Makefile                  |   2 -
 arch/powerpc/crypto/crc32c-vpmsum_glue.c      | 173 ------------------
 arch/powerpc/crypto/crct10dif-vpmsum_asm.S    |   2 +-
 arch/powerpc/lib/Makefile                     |   3 +
 arch/powerpc/lib/crc32-glue.c                 |  86 +++++++++
 .../{crypto => lib}/crc32-vpmsum_core.S       |   0
 .../{crypto => lib}/crc32c-vpmsum_asm.S       |   0
 11 files changed, 92 insertions(+), 192 deletions(-)
 delete mode 100644 arch/powerpc/crypto/crc32c-vpmsum_glue.c
 create mode 100644 arch/powerpc/lib/crc32-glue.c
 rename arch/powerpc/{crypto => lib}/crc32-vpmsum_core.S (100%)
 rename arch/powerpc/{crypto => lib}/crc32c-vpmsum_asm.S (100%)
arch/powerpc/Kconfig +1 -0
index 8094a01974cc..b05889400b04 100644
@@ -125,10 +125,11 @@ config PPC
 	select ARCH_DISABLE_KASAN_INLINE	if PPC_RADIX_MMU
 	select ARCH_DMA_DEFAULT_COHERENT	if !NOT_COHERENT_CACHE
 	select ARCH_ENABLE_MEMORY_HOTPLUG
 	select ARCH_ENABLE_MEMORY_HOTREMOVE
 	select ARCH_HAS_COPY_MC			if PPC64
+	select ARCH_HAS_CRC32			if PPC64 && ALTIVEC
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL
 	select ARCH_HAS_DEBUG_VM_PGTABLE
 	select ARCH_HAS_DEBUG_WX		if STRICT_KERNEL_RWX
 	select ARCH_HAS_DEVMEM_IS_ALLOWED
arch/powerpc/configs/powernv_defconfig +0 -1
index ee84ade7a033..4a7ddea05b4d 100644
@@ -318,11 +318,10 @@ CONFIG_FTR_FIXUP_SELFTEST=y
 CONFIG_MSI_BITMAP_SELFTEST=y
 CONFIG_XMON=y
 CONFIG_CRYPTO_TEST=m
 CONFIG_CRYPTO_PCBC=m
 CONFIG_CRYPTO_HMAC=y
-CONFIG_CRYPTO_CRC32C_VPMSUM=m
 CONFIG_CRYPTO_CRCT10DIF_VPMSUM=m
 CONFIG_CRYPTO_MD5_PPC=m
 CONFIG_CRYPTO_MICHAEL_MIC=m
 CONFIG_CRYPTO_SHA1_PPC=m
 CONFIG_CRYPTO_SHA256=y
arch/powerpc/configs/ppc64_defconfig +0 -1
index a5e3e7f97f4d..ea01c0d6705f 100644
@@ -388,11 +388,10 @@ CONFIG_CRYPTO_TWOFISH=m
 CONFIG_CRYPTO_PCBC=m
 CONFIG_CRYPTO_MICHAEL_MIC=m
 CONFIG_CRYPTO_SHA256=y
 CONFIG_CRYPTO_WP512=m
 CONFIG_CRYPTO_LZO=m
-CONFIG_CRYPTO_CRC32C_VPMSUM=m
 CONFIG_CRYPTO_CRCT10DIF_VPMSUM=m
 CONFIG_CRYPTO_VPMSUM_TESTER=m
 CONFIG_CRYPTO_MD5_PPC=m
 CONFIG_CRYPTO_SHA1_PPC=m
 CONFIG_CRYPTO_AES_GCM_P10=m
arch/powerpc/crypto/Kconfig +1 -14
index 46a4c85e85e2..5c016ec39530 100644
@@ -11,23 +11,10 @@ config CRYPTO_CURVE25519_PPC64
 	  Curve25519 algorithm
 
 	  Architecture: PowerPC64
 	  - Little-endian
 
-config CRYPTO_CRC32C_VPMSUM
-	tristate "CRC32c"
-	depends on PPC64 && ALTIVEC
-	select CRYPTO_HASH
-	select CRC32
-	help
-	  CRC32c CRC algorithm with the iSCSI polynomial (RFC 3385 and RFC 3720)
-
-	  Architecture: powerpc64 using
-	  - AltiVec extensions
-
-	  Enable on POWER8 and newer processors for improved performance.
-
 config CRYPTO_CRCT10DIF_VPMSUM
 	tristate "CRC32T10DIF"
 	depends on PPC64 && ALTIVEC && CRC_T10DIF
 	select CRYPTO_HASH
 	help
@@ -38,11 +25,11 @@ config CRYPTO_CRCT10DIF_VPMSUM
 
 	  Enable on POWER8 and newer processors for improved performance.
 
 config CRYPTO_VPMSUM_TESTER
 	tristate "CRC32c and CRC32T10DIF hardware acceleration tester"
-	depends on CRYPTO_CRCT10DIF_VPMSUM && CRYPTO_CRC32C_VPMSUM
+	depends on CRYPTO_CRCT10DIF_VPMSUM && CRC32_ARCH
 	help
 	  Stress test for CRC32c and CRCT10DIF algorithms implemented with
 	  powerpc64 AltiVec extensions (POWER8 vpmsum instructions).
 	  Unless you are testing these algorithms, you don't need this.
 
arch/powerpc/crypto/Makefile +0 -2
index 59808592f0a1..54486192273c 100644
@@ -8,11 +8,10 @@
 obj-$(CONFIG_CRYPTO_AES_PPC_SPE) += aes-ppc-spe.o
 obj-$(CONFIG_CRYPTO_MD5_PPC) += md5-ppc.o
 obj-$(CONFIG_CRYPTO_SHA1_PPC) += sha1-powerpc.o
 obj-$(CONFIG_CRYPTO_SHA1_PPC_SPE) += sha1-ppc-spe.o
 obj-$(CONFIG_CRYPTO_SHA256_PPC_SPE) += sha256-ppc-spe.o
-obj-$(CONFIG_CRYPTO_CRC32C_VPMSUM) += crc32c-vpmsum.o
 obj-$(CONFIG_CRYPTO_CRCT10DIF_VPMSUM) += crct10dif-vpmsum.o
 obj-$(CONFIG_CRYPTO_VPMSUM_TESTER) += crc-vpmsum_test.o
 obj-$(CONFIG_CRYPTO_AES_GCM_P10) += aes-gcm-p10-crypto.o
 obj-$(CONFIG_CRYPTO_CHACHA20_P10) += chacha-p10-crypto.o
 obj-$(CONFIG_CRYPTO_POLY1305_P10) += poly1305-p10-crypto.o
@@ -22,11 +21,10 @@ obj-$(CONFIG_CRYPTO_CURVE25519_PPC64) += curve25519-ppc64le.o
 aes-ppc-spe-y := aes-spe-core.o aes-spe-keys.o aes-tab-4k.o aes-spe-modes.o aes-spe-glue.o
 md5-ppc-y := md5-asm.o md5-glue.o
 sha1-powerpc-y := sha1-powerpc-asm.o sha1.o
 sha1-ppc-spe-y := sha1-spe-asm.o sha1-spe-glue.o
 sha256-ppc-spe-y := sha256-spe-asm.o sha256-spe-glue.o
-crc32c-vpmsum-y := crc32c-vpmsum_asm.o crc32c-vpmsum_glue.o
 crct10dif-vpmsum-y := crct10dif-vpmsum_asm.o crct10dif-vpmsum_glue.o
 aes-gcm-p10-crypto-y := aes-gcm-p10-glue.o aes-gcm-p10.o ghashp10-ppc.o aesp10-ppc.o
 chacha-p10-crypto-y := chacha-p10-glue.o chacha-p10le-8x.o
 poly1305-p10-crypto-y := poly1305-p10-glue.o poly1305-p10le_64.o
 vmx-crypto-objs := vmx.o aesp8-ppc.o ghashp8-ppc.o aes.o aes_cbc.o aes_ctr.o aes_xts.o ghash.o
arch/powerpc/crypto/crc32c-vpmsum_glue.c deleted +0 -173

173 lines not on this page · raw message

arch/powerpc/crypto/crct10dif-vpmsum_asm.S +1 -1
index f0b93a0fe168..0a52261bf859 100644
@@ -840,6 +840,6 @@
 	.octa 0x000000000000000000000001f65a57f8	/* x^64 div p(x) */
 	/* Barrett constant n */
 	.octa 0x0000000000000000000000018bb70000
 
 #define CRC_FUNCTION_NAME __crct10dif_vpmsum
-#include "crc32-vpmsum_core.S"
+#include "../lib/crc32-vpmsum_core.S"
arch/powerpc/lib/Makefile +3 -0
index f14ecab674a3..da9381a1c95b 100644
@@ -76,6 +76,9 @@ obj-$(CONFIG_FTR_FIXUP_SELFTEST) += feature-fixups-test.o
 obj-$(CONFIG_ALTIVEC)	+= xor_vmx.o xor_vmx_glue.o
 CFLAGS_xor_vmx.o += -mhard-float -maltivec $(call cc-option,-mabi=altivec)
 # Enable <altivec.h>
 CFLAGS_xor_vmx.o += -isystem $(shell $(CC) -print-file-name=include)
 
+obj-$(CONFIG_CRC32_ARCH) += crc32-powerpc.o
+crc32-powerpc-y := crc32-glue.o crc32c-vpmsum_asm.o
+
 obj-$(CONFIG_PPC64) += $(obj64-y)
arch/powerpc/lib/crc32-glue.c new file +86 -0

86 lines not on this page · raw message

arch/powerpc/lib/crc32-vpmsum_core.S renamed from arch/powerpc/crypto/crc32-vpmsum_core.S +0 -0
similarity index 100%
rename from arch/powerpc/crypto/crc32-vpmsum_core.S
rename to arch/powerpc/lib/crc32-vpmsum_core.S
arch/powerpc/lib/crc32c-vpmsum_asm.S renamed from arch/powerpc/crypto/crc32c-vpmsum_asm.S +0 -0
similarity index 100%
rename from arch/powerpc/crypto/crc32c-vpmsum_asm.S
rename to arch/powerpc/lib/crc32c-vpmsum_asm.S
Signature · 1 line
2.47.0
author maintainer
#29 lore raw

PATCH v2 9/18 s390/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the s390 CRC32 assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/s390/crypto/crc32-vx.c to arch/s390/lib/crc32-glue.c, view this commit with 'git show -M10'.

---
 arch/s390/Kconfig                      |   1 +
 arch/s390/configs/debug_defconfig      |   1 -
 arch/s390/configs/defconfig            |   1 -
 arch/s390/crypto/Kconfig               |  12 -
 arch/s390/crypto/Makefile              |   2 -
 arch/s390/crypto/crc32-vx.c            | 306 -------------------------
 arch/s390/lib/Makefile                 |   3 +
 arch/s390/lib/crc32-glue.c             |  86 +++++++
 arch/s390/{crypto => lib}/crc32-vx.h   |   0
 arch/s390/{crypto => lib}/crc32be-vx.c |   0
 arch/s390/{crypto => lib}/crc32le-vx.c |   0
 11 files changed, 90 insertions(+), 322 deletions(-)
 delete mode 100644 arch/s390/crypto/crc32-vx.c
 create mode 100644 arch/s390/lib/crc32-glue.c
 rename arch/s390/{crypto => lib}/crc32-vx.h (100%)
 rename arch/s390/{crypto => lib}/crc32be-vx.c (100%)
 rename arch/s390/{crypto => lib}/crc32le-vx.c (100%)
arch/s390/Kconfig +1 -0
index d339fe4fdedf..d1fde8b941d2 100644
@@ -63,10 +63,11 @@ config S390
 	select ARCH_BINFMT_ELF_STATE
 	select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
 	select ARCH_ENABLE_MEMORY_HOTPLUG if SPARSEMEM
 	select ARCH_ENABLE_MEMORY_HOTREMOVE
 	select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2
+	select ARCH_HAS_CRC32
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL
 	select ARCH_HAS_DEBUG_VM_PGTABLE
 	select ARCH_HAS_DEBUG_WX
 	select ARCH_HAS_DEVMEM_IS_ALLOWED
arch/s390/configs/debug_defconfig +0 -1
index fb0e9a1d9be2..fd83d8958f0b 100644
@@ -792,11 +792,10 @@ CONFIG_CRYPTO_ZSTD=m
 CONFIG_CRYPTO_ANSI_CPRNG=m
 CONFIG_CRYPTO_USER_API_HASH=m
 CONFIG_CRYPTO_USER_API_SKCIPHER=m
 CONFIG_CRYPTO_USER_API_RNG=m
 CONFIG_CRYPTO_USER_API_AEAD=m
-CONFIG_CRYPTO_CRC32_S390=y
 CONFIG_CRYPTO_SHA512_S390=m
 CONFIG_CRYPTO_SHA1_S390=m
 CONFIG_CRYPTO_SHA256_S390=m
 CONFIG_CRYPTO_SHA3_256_S390=m
 CONFIG_CRYPTO_SHA3_512_S390=m
arch/s390/configs/defconfig +0 -1
index 88be0a734b60..3bdeb6d5cbd9 100644
@@ -779,11 +779,10 @@ CONFIG_CRYPTO_ANSI_CPRNG=m
 CONFIG_CRYPTO_JITTERENTROPY_OSR=1
 CONFIG_CRYPTO_USER_API_HASH=m
 CONFIG_CRYPTO_USER_API_SKCIPHER=m
 CONFIG_CRYPTO_USER_API_RNG=m
 CONFIG_CRYPTO_USER_API_AEAD=m
-CONFIG_CRYPTO_CRC32_S390=y
 CONFIG_CRYPTO_SHA512_S390=m
 CONFIG_CRYPTO_SHA1_S390=m
 CONFIG_CRYPTO_SHA256_S390=m
 CONFIG_CRYPTO_SHA3_256_S390=m
 CONFIG_CRYPTO_SHA3_512_S390=m
arch/s390/crypto/Kconfig +0 -12
index d3eb3a233693..b760232537f1 100644
@@ -1,21 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 
 menu "Accelerated Cryptographic Algorithms for CPU (s390)"
 
-config CRYPTO_CRC32_S390
-	tristate "CRC32c and CRC32"
-	depends on S390
-	select CRYPTO_HASH
-	select CRC32
-	help
-	  CRC32c and CRC32 CRC algorithms
-
-	  Architecture: s390
-
-	  It is available with IBM z13 or later.
-
 config CRYPTO_SHA512_S390
 	tristate "Hash functions: SHA-384 and SHA-512"
 	depends on S390
 	select CRYPTO_HASH
 	help
arch/s390/crypto/Makefile +0 -2
index a0cb96937c3d..14dafadbcbed 100644
@@ -12,11 +12,9 @@ obj-$(CONFIG_CRYPTO_DES_S390) += des_s390.o
 obj-$(CONFIG_CRYPTO_AES_S390) += aes_s390.o
 obj-$(CONFIG_CRYPTO_PAES_S390) += paes_s390.o
 obj-$(CONFIG_CRYPTO_CHACHA_S390) += chacha_s390.o
 obj-$(CONFIG_S390_PRNG) += prng.o
 obj-$(CONFIG_CRYPTO_GHASH_S390) += ghash_s390.o
-obj-$(CONFIG_CRYPTO_CRC32_S390) += crc32-vx_s390.o
 obj-$(CONFIG_CRYPTO_HMAC_S390) += hmac_s390.o
 obj-y += arch_random.o
 
-crc32-vx_s390-y := crc32-vx.o crc32le-vx.o crc32be-vx.o
 chacha_s390-y := chacha-glue.o chacha-s390.o
arch/s390/crypto/crc32-vx.c deleted +0 -306

306 lines not on this page · raw message

arch/s390/lib/Makefile +3 -0
index f43f897d3fc0..14bbfe50033c 100644
@@ -22,5 +22,8 @@ obj-$(CONFIG_S390_MODULES_SANITY_TEST) += test_modules.o
 obj-$(CONFIG_S390_MODULES_SANITY_TEST_HELPERS) += test_modules_helpers.o
 
 lib-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 
 obj-$(CONFIG_EXPOLINE_EXTERN) += expoline.o
+
+obj-$(CONFIG_CRC32_ARCH) += crc32-s390.o
+crc32-s390-y := crc32-glue.o crc32le-vx.o crc32be-vx.o
arch/s390/lib/crc32-glue.c new file +86 -0

86 lines not on this page · raw message

arch/s390/lib/crc32-vx.h renamed from arch/s390/crypto/crc32-vx.h +0 -0
similarity index 100%
rename from arch/s390/crypto/crc32-vx.h
rename to arch/s390/lib/crc32-vx.h
arch/s390/lib/crc32be-vx.c renamed from arch/s390/crypto/crc32be-vx.c +0 -0
similarity index 100%
rename from arch/s390/crypto/crc32be-vx.c
rename to arch/s390/lib/crc32be-vx.c
arch/s390/lib/crc32le-vx.c renamed from arch/s390/crypto/crc32le-vx.c +0 -0
similarity index 100%
rename from arch/s390/crypto/crc32le-vx.c
rename to arch/s390/lib/crc32le-vx.c
Signature · 1 line
2.47.0
author maintainer
#30 lore raw

PATCH v2 10/18 sparc/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the sparc CRC32C assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

Note: to see the diff from arch/sparc/crypto/crc32c_glue.c to arch/sparc/lib/crc32_glue.c, view this commit with 'git show -M10'.

---
 arch/sparc/Kconfig                      |   1 +
 arch/sparc/crypto/Kconfig               |  10 --
 arch/sparc/crypto/Makefile              |   4 -
 arch/sparc/crypto/crc32c_glue.c         | 184 ------------------------
 arch/sparc/lib/Makefile                 |   2 +
 arch/sparc/lib/crc32_glue.c             |  86 +++++++++++
 arch/sparc/{crypto => lib}/crc32c_asm.S |   2 +-
 7 files changed, 90 insertions(+), 199 deletions(-)
 delete mode 100644 arch/sparc/crypto/crc32c_glue.c
 create mode 100644 arch/sparc/lib/crc32_glue.c
 rename arch/sparc/{crypto => lib}/crc32c_asm.S (92%)
arch/sparc/Kconfig +1 -0
index dcfdb7f1dae9..0f88123925a4 100644
@@ -108,10 +108,11 @@ config SPARC64
 	select ARCH_HAS_GIGANTIC_PAGE
 	select HAVE_SOFTIRQ_ON_OWN_STACK
 	select HAVE_SETUP_PER_CPU_AREA
 	select NEED_PER_CPU_EMBED_FIRST_CHUNK
 	select NEED_PER_CPU_PAGE_FIRST_CHUNK
+	select ARCH_HAS_CRC32
 
 config ARCH_PROC_KCORE_TEXT
 	def_bool y
 
 config CPU_BIG_ENDIAN
arch/sparc/crypto/Kconfig +0 -10
index cfe5102b1c68..e858597de89d 100644
@@ -14,20 +14,10 @@ config CRYPTO_DES_SPARC64
 	  Length-preserving ciphers: DES with ECB and CBC modes
 	  Length-preserving ciphers: Tripe DES EDE with ECB and CBC modes
 
 	  Architecture: sparc64
 
-config CRYPTO_CRC32C_SPARC64
-	tristate "CRC32c"
-	depends on SPARC64
-	select CRYPTO_HASH
-	select CRC32
-	help
-	  CRC32c CRC algorithm with the iSCSI polynomial (RFC 3385 and RFC 3720)
-
-	  Architecture: sparc64
-
 config CRYPTO_MD5_SPARC64
 	tristate "Digests: MD5"
 	depends on SPARC64
 	select CRYPTO_MD5
 	select CRYPTO_HASH
arch/sparc/crypto/Makefile +0 -4
index d257186c27d1..a2d7fca40cb4 100644
@@ -10,17 +10,13 @@ obj-$(CONFIG_CRYPTO_MD5_SPARC64) += md5-sparc64.o
 
 obj-$(CONFIG_CRYPTO_AES_SPARC64) += aes-sparc64.o
 obj-$(CONFIG_CRYPTO_DES_SPARC64) += des-sparc64.o
 obj-$(CONFIG_CRYPTO_CAMELLIA_SPARC64) += camellia-sparc64.o
 
-obj-$(CONFIG_CRYPTO_CRC32C_SPARC64) += crc32c-sparc64.o
-
 sha1-sparc64-y := sha1_asm.o sha1_glue.o
 sha256-sparc64-y := sha256_asm.o sha256_glue.o
 sha512-sparc64-y := sha512_asm.o sha512_glue.o
 md5-sparc64-y := md5_asm.o md5_glue.o
 
 aes-sparc64-y := aes_asm.o aes_glue.o
 des-sparc64-y := des_asm.o des_glue.o
 camellia-sparc64-y := camellia_asm.o camellia_glue.o
-
-crc32c-sparc64-y := crc32c_asm.o crc32c_glue.o
arch/sparc/crypto/crc32c_glue.c deleted +0 -184

184 lines not on this page · raw message

arch/sparc/lib/Makefile +2 -0
index ee5091dd67ed..5724d0f356eb 100644
@@ -51,5 +51,7 @@ lib-$(CONFIG_SPARC64) += copy_in_user.o memmove.o
 lib-$(CONFIG_SPARC64) += mcount.o ipcsum.o xor.o hweight.o ffs.o
 
 obj-$(CONFIG_SPARC64) += iomap.o
 obj-$(CONFIG_SPARC32) += atomic32.o
 obj-$(CONFIG_SPARC64) += PeeCeeI.o
+obj-$(CONFIG_CRC32_ARCH) += crc32-sparc.o
+crc32-sparc-y := crc32_glue.o crc32c_asm.o
arch/sparc/lib/crc32_glue.c new file +86 -0

86 lines not on this page · raw message

arch/sparc/lib/crc32c_asm.S renamed from arch/sparc/crypto/crc32c_asm.S +1 -1
similarity index 92%
rename from arch/sparc/crypto/crc32c_asm.S
rename to arch/sparc/lib/crc32c_asm.S
index b8659a479242..ee454fa6aed6 100644
@@ -1,11 +1,11 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <linux/linkage.h>
 #include <asm/visasm.h>
 #include <asm/asi.h>
 
-#include "opcodes.h"
+#include "../crypto/opcodes.h"
 
 ENTRY(crc32c_sparc64)
 	/* %o0=crc32p, %o1=data_ptr, %o2=len */
 	VISEntryHalf
 	lda	[%o0] ASI_PL, %f1
Signature · 1 line
2.47.0
author maintainer
#31 lore raw

PATCH v2 11/18 x86/crc32: update prototype for crc_pcl()

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

- Change the len parameter from unsigned int to size_t, so that the library function which takes a size_t can safely use this code.

- Rename to crc32c_x86_3way() which is much clearer.

- Move the crc parameter to the front, as this is the usual convention.

---
 arch/x86/crypto/crc32c-intel_glue.c       |  7 ++-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 63 ++++++++++++-----------
 2 files changed, 35 insertions(+), 35 deletions(-)
arch/x86/crypto/crc32c-intel_glue.c +3 -4
index 52c5d47ef5a1..603d159de400 100644
@@ -39,12 +39,11 @@
  * size is >= 512 to account
  * for fpu state save/restore overhead.
  */
 #define CRC32C_PCL_BREAKEVEN	512
 
-asmlinkage unsigned int crc_pcl(const u8 *buffer, unsigned int len,
-				unsigned int crc_init);
+asmlinkage u32 crc32c_x86_3way(u32 crc, const u8 *buffer, size_t len);
 #endif /* CONFIG_X86_64 */
 
 static u32 crc32c_intel_le_hw_byte(u32 crc, unsigned char const *data, size_t length)
 {
 	while (length--) {
@@ -157,11 +156,11 @@ static int crc32c_pcl_intel_update(struct shash_desc *desc, const u8 *data,
 	 * use faster PCL version if datasize is large enough to
 	 * overcome kernel fpu state save/restore overhead
 	 */
 	if (len >= CRC32C_PCL_BREAKEVEN && crypto_simd_usable()) {
 		kernel_fpu_begin();
-		*crcp = crc_pcl(data, len, *crcp);
+		*crcp = crc32c_x86_3way(*crcp, data, len);
 		kernel_fpu_end();
 	} else
 		*crcp = crc32c_intel_le_hw(*crcp, data, len);
 	return 0;
 }
@@ -169,11 +168,11 @@ static int crc32c_pcl_intel_update(struct shash_desc *desc, const u8 *data,
 static int __crc32c_pcl_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
 				u8 *out)
 {
 	if (len >= CRC32C_PCL_BREAKEVEN && crypto_simd_usable()) {
 		kernel_fpu_begin();
-		*(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
+		*(__le32 *)out = ~cpu_to_le32(crc32c_x86_3way(*crcp, data, len));
 		kernel_fpu_end();
 	} else
 		*(__le32 *)out =
 			~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
 	return 0;
arch/x86/crypto/crc32c-pcl-intel-asm_64.S +32 -31

175 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#32 lore raw

PATCH v2 12/18 x86/crc32: update prototype for crc32_pclmul_le_16()

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

- Change the len parameter from unsigned int to size_t, so that the library function which takes a size_t can safely use this code.

- Move the crc parameter to the front, as this is the usual convention.

---
 arch/x86/crypto/crc32-pclmul_asm.S  | 19 +++++++++----------
 arch/x86/crypto/crc32-pclmul_glue.c |  4 ++--
 2 files changed, 11 insertions(+), 12 deletions(-)
arch/x86/crypto/crc32-pclmul_asm.S +9 -10
index 5d31137e2c7d..f9637789cac1 100644
@@ -56,30 +56,29 @@
 	.octa 0x00000001F701164100000001DB710641
 
 #define CONSTANT %xmm0
 
 #ifdef __x86_64__
-#define BUF     %rdi
-#define LEN     %rsi
-#define CRC     %edx
+#define CRC     %edi
+#define BUF     %rsi
+#define LEN     %rdx
 #else
-#define BUF     %eax
-#define LEN     %edx
-#define CRC     %ecx
+#define CRC     %eax
+#define BUF     %edx
+#define LEN     %ecx
 #endif
 
 
 
 .text
 /**
  *      Calculate crc32
- *      BUF - buffer (16 bytes aligned)
- *      LEN - sizeof buffer (16 bytes aligned), LEN should be grater than 63
  *      CRC - initial crc32
+ *      BUF - buffer (16 bytes aligned)
+ *      LEN - sizeof buffer (16 bytes aligned), LEN should be greater than 63
  *      return %eax crc32
- *      uint crc32_pclmul_le_16(unsigned char const *buffer,
- *	                     size_t len, uint crc32)
+ *      u32 crc32_pclmul_le_16(u32 crc, const u8 *buffer, size_t len);
  */
 
 SYM_FUNC_START(crc32_pclmul_le_16) /* buffer and buffer size are 16 bytes aligned */
 	movdqa  (BUF), %xmm1
 	movdqa  0x10(BUF), %xmm2
arch/x86/crypto/crc32-pclmul_glue.c +2 -2
index 9f5e342b9845..9d14eac51c5b 100644
@@ -44,11 +44,11 @@
 #define PCLMUL_MIN_LEN		64L     /* minimum size of buffer
 					 * for crc32_pclmul_le_16 */
 #define SCALE_F			16L	/* size of xmm register */
 #define SCALE_F_MASK		(SCALE_F - 1)
 
-u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
+u32 crc32_pclmul_le_16(u32 crc, const u8 *buffer, size_t len);
 
 static u32 __attribute__((pure))
 	crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
 {
 	unsigned int iquotient;
@@ -69,11 +69,11 @@ static u32 __attribute__((pure))
 	}
 	iquotient = len & (~SCALE_F_MASK);
 	iremainder = len & SCALE_F_MASK;
 
 	kernel_fpu_begin();
-	crc = crc32_pclmul_le_16(p, iquotient, crc);
+	crc = crc32_pclmul_le_16(crc, p, iquotient);
 	kernel_fpu_end();
 
 	if (iremainder)
 		crc = crc32_le(crc, p + iquotient, iremainder);
 
Signature · 1 line
2.47.0
author maintainer
#33 lore raw

PATCH v2 13/18 x86/crc32: expose CRC32 functions through lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Move the x86 CRC32 assembly code into the lib directory and wire it up to the library interface. This allows it to be used without going through the crypto API. It remains usable via the crypto API too via the shash algorithms that use the library interface. Thus all the arch-specific "shash" code becomes unnecessary and is removed.

---
 arch/x86/Kconfig                              |   1 +
 arch/x86/crypto/Kconfig                       |  22 --
 arch/x86/crypto/Makefile                      |   7 -
 arch/x86/crypto/crc32-pclmul_glue.c           | 202 --------------
 arch/x86/crypto/crc32c-intel_glue.c           | 249 ------------------
 arch/x86/lib/Makefile                         |   4 +
 arch/x86/lib/crc32-glue.c                     | 116 ++++++++
 .../crc32-pclmul_asm.S => lib/crc32-pclmul.S} |   0
 .../crc32c-3way.S}                            |   0
 drivers/target/iscsi/Kconfig                  |   1 -
 10 files changed, 121 insertions(+), 481 deletions(-)
 delete mode 100644 arch/x86/crypto/crc32-pclmul_glue.c
 delete mode 100644 arch/x86/crypto/crc32c-intel_glue.c
 create mode 100644 arch/x86/lib/crc32-glue.c
 rename arch/x86/{crypto/crc32-pclmul_asm.S => lib/crc32-pclmul.S} (100%)
 rename arch/x86/{crypto/crc32c-pcl-intel-asm_64.S => lib/crc32c-3way.S} (100%)
arch/x86/Kconfig +1 -0
index 2852fcd82cbd..a7a1a1448d23 100644
@@ -74,10 +74,11 @@ config X86
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
 	select ARCH_HAS_CACHE_LINE_SIZE
 	select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
 	select ARCH_HAS_CPU_FINALIZE_INIT
 	select ARCH_HAS_CPU_PASID		if IOMMU_SVA
+	select ARCH_HAS_CRC32
 	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DEBUG_VIRTUAL
 	select ARCH_HAS_DEBUG_VM_PGTABLE	if !X86_PAE
 	select ARCH_HAS_DEVMEM_IS_ALLOWED
 	select ARCH_HAS_DMA_OPS			if GART_IOMMU || XEN
arch/x86/crypto/Kconfig +0 -22
index 7b1bebed879d..1ca53e847966 100644
@@ -490,32 +490,10 @@ config CRYPTO_GHASH_CLMUL_NI_INTEL
 	  GCM GHASH hash function (NIST SP800-38D)
 
 	  Architecture: x86_64 using:
 	  - CLMUL-NI (carry-less multiplication new instructions)
 
-config CRYPTO_CRC32C_INTEL
-	tristate "CRC32c (SSE4.2/PCLMULQDQ)"
-	depends on X86
-	select CRYPTO_HASH
-	help
-	  CRC32c CRC algorithm with the iSCSI polynomial (RFC 3385 and RFC 3720)
-
-	  Architecture: x86 (32-bit and 64-bit) using:
-	  - SSE4.2 (Streaming SIMD Extensions 4.2) CRC32 instruction
-	  - PCLMULQDQ (carry-less multiplication)
-
-config CRYPTO_CRC32_PCLMUL
-	tristate "CRC32 (PCLMULQDQ)"
-	depends on X86
-	select CRYPTO_HASH
-	select CRC32
-	help
-	  CRC32 CRC algorithm (IEEE 802.3)
-
-	  Architecture: x86 (32-bit and 64-bit) using:
-	  - PCLMULQDQ (carry-less multiplication)
-
 config CRYPTO_CRCT10DIF_PCLMUL
 	tristate "CRCT10DIF (PCLMULQDQ)"
 	depends on X86 && 64BIT && CRC_T10DIF
 	select CRYPTO_HASH
 	help
arch/x86/crypto/Makefile +0 -7
index 53b4a277809e..030b925ca4e2 100644
@@ -73,17 +73,10 @@ obj-$(CONFIG_CRYPTO_GHASH_CLMUL_NI_INTEL) += ghash-clmulni-intel.o
 ghash-clmulni-intel-y := ghash-clmulni-intel_asm.o ghash-clmulni-intel_glue.o
 
 obj-$(CONFIG_CRYPTO_POLYVAL_CLMUL_NI) += polyval-clmulni.o
 polyval-clmulni-y := polyval-clmulni_asm.o polyval-clmulni_glue.o
 
-obj-$(CONFIG_CRYPTO_CRC32C_INTEL) += crc32c-intel.o
-crc32c-intel-y := crc32c-intel_glue.o
-crc32c-intel-$(CONFIG_64BIT) += crc32c-pcl-intel-asm_64.o
-
-obj-$(CONFIG_CRYPTO_CRC32_PCLMUL) += crc32-pclmul.o
-crc32-pclmul-y := crc32-pclmul_asm.o crc32-pclmul_glue.o
-
 obj-$(CONFIG_CRYPTO_CRCT10DIF_PCLMUL) += crct10dif-pclmul.o
 crct10dif-pclmul-y := crct10dif-pcl-asm_64.o crct10dif-pclmul_glue.o
 
 obj-$(CONFIG_CRYPTO_POLY1305_X86_64) += poly1305-x86_64.o
 poly1305-x86_64-y := poly1305-x86_64-cryptogams.o poly1305_glue.o
arch/x86/crypto/crc32-pclmul_glue.c deleted +0 -202

202 lines not on this page · raw message

arch/x86/crypto/crc32c-intel_glue.c deleted +0 -249

249 lines not on this page · raw message

arch/x86/lib/Makefile +4 -0
index 98583a9dbab3..17510da06c9f 100644
@@ -36,10 +36,14 @@ lib-$(CONFIG_ARCH_HAS_COPY_MC) += copy_mc.o copy_mc_64.o
 lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o
 lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
 lib-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 lib-$(CONFIG_MITIGATION_RETPOLINE) += retpoline.o
 
+obj-$(CONFIG_CRC32_ARCH) += crc32-x86.o
+crc32-x86-y := crc32-glue.o crc32-pclmul.o
+crc32-x86-$(CONFIG_64BIT) += crc32c-3way.o
+
 obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
 obj-y += iomem.o
 
 ifeq ($(CONFIG_X86_32),y)
         obj-y += atomic64_32.o
arch/x86/lib/crc32-glue.c new file +116 -0

116 lines not on this page · raw message

arch/x86/lib/crc32-pclmul.S renamed from arch/x86/crypto/crc32-pclmul_asm.S +0 -0
similarity index 100%
rename from arch/x86/crypto/crc32-pclmul_asm.S
rename to arch/x86/lib/crc32-pclmul.S
arch/x86/lib/crc32c-3way.S renamed from arch/x86/crypto/crc32c-pcl-intel-asm_64.S +0 -0
similarity index 100%
rename from arch/x86/crypto/crc32c-pcl-intel-asm_64.S
rename to arch/x86/lib/crc32c-3way.S
drivers/target/iscsi/Kconfig +0 -1
index 922b207bc69d..1c0517a12571 100644
@@ -2,11 +2,10 @@
 config ISCSI_TARGET
 	tristate "SCSI Target Mode Stack"
 	depends on INET
 	select CRYPTO
 	select CRYPTO_CRC32C
-	select CRYPTO_CRC32C_INTEL if X86
 	help
 	Say M to enable the SCSI target mode stack. A SCSI target mode stack
 	is software that makes local storage available over a storage network
 	to a SCSI initiator system. The supported storage network technologies
 	include iSCSI, Fibre Channel and the SCSI RDMA Protocol (SRP).
Signature · 1 line
2.47.0
author maintainer
#34 lore raw

PATCH v2 14/18 lib/crc32: make crc32c() go directly to lib

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that the lower level __crc32c_le() library function is optimized for each architecture, make crc32c() just call that instead of taking an inefficient and error-prone detour through the shash API.

Note: a future cleanup should make crc32c_le() be the actual library function instead of __crc32c_le(). That will require updating callers of __crc32c_le() to use crc32c_le() instead, and updating callers of crc32c_le() that expect a 'const void *' arg to expect 'const u8 *' instead. Similarly, a future cleanup should remove LIBCRC32C by making everyone who is selecting it just select CRC32 directly instead.

---
 include/linux/crc32c.h |  7 ++--
 lib/Kconfig            | 10 ++----
 lib/Makefile           |  1 -
 lib/libcrc32c.c        | 74 ------------------------------------------
 4 files changed, 8 insertions(+), 84 deletions(-)
 delete mode 100644 lib/libcrc32c.c
include/linux/crc32c.h +5 -2
index 357ae4611a45..47eb78003c26 100644
@@ -1,12 +1,15 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _LINUX_CRC32C_H
 #define _LINUX_CRC32C_H
 
-#include <linux/types.h>
+#include <linux/crc32.h>
 
-extern u32 crc32c(u32 crc, const void *address, unsigned int length);
+static inline u32 crc32c(u32 crc, const void *address, unsigned int length)
+{
+	return __crc32c_le(crc, address, length);
+}
 
 /* This macro exists for backwards-compatibility. */
 #define crc32c_le crc32c
 
 #endif	/* _LINUX_CRC32C_H */
lib/Kconfig +3 -7
index 07afcf214f35..b894ee64ff95 100644
@@ -296,18 +296,14 @@ config CRC7
 	  the kernel tree does. Such modules that use library CRC7
 	  functions require M here.
 
 config LIBCRC32C
 	tristate "CRC32c (Castagnoli, et al) Cyclic Redundancy-Check"
-	select CRYPTO
-	select CRYPTO_CRC32C
+	select CRC32
 	help
-	  This option is provided for the case where no in-kernel-tree
-	  modules require CRC32c functions, but a module built outside the
-	  kernel tree does. Such modules that use library CRC32c functions
-	  require M here.  See Castagnoli93.
-	  Module will be libcrc32c.
+	  This option just selects CRC32 and is provided for compatibility
+	  purposes until the users are updated to select CRC32 directly.
 
 config CRC8
 	tristate "CRC8 function"
 	help
 	  This option provides CRC8 function. Drivers may select this
lib/Makefile +0 -1
index 773adf88af41..15646679aee2 100644
@@ -161,11 +161,10 @@ obj-$(CONFIG_CRC_ITU_T)	+= crc-itu-t.o
 obj-$(CONFIG_CRC32)	+= crc32.o
 obj-$(CONFIG_CRC64)     += crc64.o
 obj-$(CONFIG_CRC32_SELFTEST)	+= crc32test.o
 obj-$(CONFIG_CRC4)	+= crc4.o
 obj-$(CONFIG_CRC7)	+= crc7.o
-obj-$(CONFIG_LIBCRC32C)	+= libcrc32c.o
 obj-$(CONFIG_CRC8)	+= crc8.o
 obj-$(CONFIG_CRC64_ROCKSOFT) += crc64-rocksoft.o
 obj-$(CONFIG_XXHASH)	+= xxhash.o
 obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o
 
lib/libcrc32c.c deleted +0 -74

74 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#35 lore raw

PATCH v2 15/18 ext4: switch to using the crc32c library

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32c() library function directly takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32c(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

---
 fs/ext4/Kconfig |  3 +--
 fs/ext4/ext4.h  | 25 +++----------------------
 fs/ext4/super.c | 15 ---------------
 3 files changed, 4 insertions(+), 39 deletions(-)
fs/ext4/Kconfig +1 -2
index e20d59221fc0..c9ca41d91a6c 100644
@@ -29,12 +29,11 @@ config EXT3_FS_SECURITY
 config EXT4_FS
 	tristate "The Extended 4 (ext4) filesystem"
 	select BUFFER_HEAD
 	select JBD2
 	select CRC16
-	select CRYPTO
-	select CRYPTO_CRC32C
+	select CRC32
 	select FS_IOMAP
 	select FS_ENCRYPTION_ALGS if FS_ENCRYPTION
 	help
 	  This is the next generation of the ext3 filesystem.
 
fs/ext4/ext4.h +3 -22
index 44b0d418143c..99aa512a7de1 100644
@@ -31,11 +31,11 @@
 #include <linux/wait.h>
 #include <linux/sched/signal.h>
 #include <linux/blockgroup_lock.h>
 #include <linux/percpu_counter.h>
 #include <linux/ratelimit.h>
-#include <crypto/hash.h>
+#include <linux/crc32c.h>
 #include <linux/falloc.h>
 #include <linux/percpu-rwsem.h>
 #include <linux/fiemap.h>
 #ifdef __KERNEL__
 #include <linux/compat.h>
@@ -1660,13 +1660,10 @@ struct ext4_sb_info {
 	struct task_struct *s_mmp_tsk;
 
 	/* record the last minlen when FITRIM is called. */
 	unsigned long s_last_trim_minblks;
 
-	/* Reference to checksum algorithm driver via cryptoapi */
-	struct crypto_shash *s_chksum_driver;
-
 	/* Precomputed FS UUID checksum for seeding other checksums */
 	__u32 s_csum_seed;
 
 	/* Reclaim extents from extent status tree */
 	struct shrinker *s_es_shrinker;
@@ -2465,23 +2462,11 @@ static inline __le16 ext4_rec_len_to_disk(unsigned len, unsigned blocksize)
 #define DX_HASH_LAST 			DX_HASH_SIPHASH
 
 static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
 			      const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[4];
-	} desc;
-
-	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
-
-	desc.shash.tfm = sbi->s_chksum_driver;
-	*(u32 *)desc.ctx = crc;
-
-	BUG_ON(crypto_shash_update(&desc.shash, address, length));
-
-	return *(u32 *)desc.ctx;
+	return crc32c(crc, address, length);
 }
 
 #ifdef __KERNEL__
 
 /* hash info structure used by the directory hash */
@@ -3278,15 +3263,11 @@ extern void ext4_group_desc_csum_set(struct super_block *sb, __u32 group,
 extern int ext4_register_li_request(struct super_block *sb,
 				    ext4_group_t first_not_zeroed);
 
 static inline int ext4_has_metadata_csum(struct super_block *sb)
 {
-	WARN_ON_ONCE(ext4_has_feature_metadata_csum(sb) &&
-		     !EXT4_SB(sb)->s_chksum_driver);
-
-	return ext4_has_feature_metadata_csum(sb) &&
-	       (EXT4_SB(sb)->s_chksum_driver != NULL);
+	return ext4_has_feature_metadata_csum(sb);
 }
 
 static inline int ext4_has_group_desc_csum(struct super_block *sb)
 {
 	return ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb);
fs/ext4/super.c +0 -15
index 16a4ce704460..1a821093cc0d 100644
@@ -1371,12 +1371,10 @@ static void ext4_put_super(struct super_block *sb)
 	 * Now that we are completely done shutting down the
 	 * superblock, we need to actually destroy the kobject.
 	 */
 	kobject_put(&sbi->s_kobj);
 	wait_for_completion(&sbi->s_kobj_unregister);
-	if (sbi->s_chksum_driver)
-		crypto_free_shash(sbi->s_chksum_driver);
 	kfree(sbi->s_blockgroup_lock);
 	fs_put_dax(sbi->s_daxdev, NULL);
 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
 #if IS_ENABLED(CONFIG_UNICODE)
 	utf8_unload(sb->s_encoding);
@@ -4586,19 +4584,10 @@ static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_blo
 		return -EINVAL;
 	}
 	ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
 				ext4_orphan_file_block_trigger);
 
-	/* Load the checksum driver */
-	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
-	if (IS_ERR(sbi->s_chksum_driver)) {
-		int ret = PTR_ERR(sbi->s_chksum_driver);
-		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
-		sbi->s_chksum_driver = NULL;
-		return ret;
-	}
-
 	/* Check superblock checksum */
 	if (!ext4_superblock_csum_verify(sb, es)) {
 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
 			 "invalid superblock checksum.  Run e2fsck?");
 		return -EFSBADCRC;
@@ -5638,13 +5627,10 @@ failed_mount8: __maybe_unused
 	flush_work(&sbi->s_sb_upd_work);
 	ext4_stop_mmpd(sbi);
 	del_timer_sync(&sbi->s_err_report);
 	ext4_group_desc_free(sbi);
 failed_mount:
-	if (sbi->s_chksum_driver)
-		crypto_free_shash(sbi->s_chksum_driver);
-
 #if IS_ENABLED(CONFIG_UNICODE)
 	utf8_unload(sb->s_encoding);
 #endif
 
 #ifdef CONFIG_QUOTA
@@ -7433,8 +7419,7 @@ static void __exit ext4_exit_fs(void)
 }
 
 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
 MODULE_DESCRIPTION("Fourth Extended Filesystem");
 MODULE_LICENSE("GPL");
-MODULE_SOFTDEP("pre: crc32c");
 module_init(ext4_init_fs)
 module_exit(ext4_exit_fs)
Signature · 1 line
2.47.0
maintainer Acked-by on v2 15/18
#36 lore raw

Re: [PATCH v2 15/18] ext4: switch to using the crc32c library

Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32c() library function directly takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32c(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

author maintainer
#37 lore raw

PATCH v2 16/18 jbd2: switch to using the crc32c library

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32c() library function directly takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32c(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

---
 fs/jbd2/Kconfig      |  2 --
 fs/jbd2/journal.c    | 25 ++-----------------------
 include/linux/jbd2.h | 31 +++----------------------------
 3 files changed, 5 insertions(+), 53 deletions(-)
fs/jbd2/Kconfig +0 -2
index 4ad2c67f93f1..9c19e1512101 100644
@@ -1,11 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config JBD2
 	tristate
 	select CRC32
-	select CRYPTO
-	select CRYPTO_CRC32C
 	help
 	  This is a generic journaling layer for block devices that support
 	  both 32-bit and 64-bit block numbers.  It is currently used by
 	  the ext4 and OCFS2 filesystems, but it could also be used to add
 	  journal support to other file systems or block devices such
fs/jbd2/journal.c +2 -23
index 97f487c3d8fc..56cea5a738a7 100644
@@ -1373,24 +1373,16 @@ static int journal_check_superblock(journal_t *journal)
 		printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
 		       "at the same time!\n");
 		return err;
 	}
 
-	/* Load the checksum driver */
 	if (jbd2_journal_has_csum_v2or3_feature(journal)) {
 		if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) {
 			printk(KERN_ERR "JBD2: Unknown checksum type\n");
 			return err;
 		}
 
-		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
-		if (IS_ERR(journal->j_chksum_driver)) {
-			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
-			err = PTR_ERR(journal->j_chksum_driver);
-			journal->j_chksum_driver = NULL;
-			return err;
-		}
 		/* Check superblock checksum */
 		if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
 			printk(KERN_ERR "JBD2: journal checksum error\n");
 			err = -EFSBADCRC;
 			return err;
@@ -1611,12 +1603,10 @@ static journal_t *journal_init_common(struct block_device *bdev,
 
 	return journal;
 
 err_cleanup:
 	percpu_counter_destroy(&journal->j_checkpoint_jh_count);
-	if (journal->j_chksum_driver)
-		crypto_free_shash(journal->j_chksum_driver);
 	kfree(journal->j_wbuf);
 	jbd2_journal_destroy_revoke(journal);
 	journal_fail_superblock(journal);
 	kfree(journal);
 	return ERR_PTR(err);
@@ -2194,12 +2184,10 @@ int jbd2_journal_destroy(journal_t *journal)
 	if (journal->j_proc_entry)
 		jbd2_stats_proc_exit(journal);
 	iput(journal->j_inode);
 	if (journal->j_revoke)
 		jbd2_journal_destroy_revoke(journal);
-	if (journal->j_chksum_driver)
-		crypto_free_shash(journal->j_chksum_driver);
 	kfree(journal->j_fc_wbuf);
 	kfree(journal->j_wbuf);
 	kfree(journal);
 
 	return err;
@@ -2340,23 +2328,14 @@ int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
 			pr_err("JBD2: Cannot enable fast commits.\n");
 			return 0;
 		}
 	}
 
-	/* Load the checksum driver if necessary */
-	if ((journal->j_chksum_driver == NULL) &&
-	    INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
-		journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
-		if (IS_ERR(journal->j_chksum_driver)) {
-			printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
-			journal->j_chksum_driver = NULL;
-			return 0;
-		}
-		/* Precompute checksum seed for all metadata */
+	/* Precompute checksum seed for all metadata */
+	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3))
 		journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
 						   sizeof(sb->s_uuid));
-	}
 
 	lock_buffer(journal->j_sb_buffer);
 
 	/* If enabling v3 checksums, update superblock */
 	if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
include/linux/jbd2.h +3 -28
index 8aef9bb6ad57..33d25a3d15f1 100644
@@ -26,11 +26,11 @@
 #include <linux/mutex.h>
 #include <linux/timer.h>
 #include <linux/slab.h>
 #include <linux/bit_spinlock.h>
 #include <linux/blkdev.h>
-#include <crypto/hash.h>
+#include <linux/crc32c.h>
 #endif
 
 #define journal_oom_retry 1
 
 /*
@@ -1239,17 +1239,10 @@ struct journal_s
 	 * An opaque pointer to fs-private information.  ext3 puts its
 	 * superblock pointer here.
 	 */
 	void *j_private;
 
-	/**
-	 * @j_chksum_driver:
-	 *
-	 * Reference to checksum algorithm driver via cryptoapi.
-	 */
-	struct crypto_shash *j_chksum_driver;
-
 	/**
 	 * @j_csum_seed:
 	 *
 	 * Precomputed journal UUID checksum for seeding other checksums.
 	 */
@@ -1748,14 +1741,11 @@ static inline bool jbd2_journal_has_csum_v2or3_feature(journal_t *j)
 	return jbd2_has_feature_csum2(j) || jbd2_has_feature_csum3(j);
 }
 
 static inline int jbd2_journal_has_csum_v2or3(journal_t *journal)
 {
-	WARN_ON_ONCE(jbd2_journal_has_csum_v2or3_feature(journal) &&
-		     journal->j_chksum_driver == NULL);
-
-	return journal->j_chksum_driver != NULL;
+	return jbd2_journal_has_csum_v2or3_feature(journal);
 }
 
 static inline int jbd2_journal_get_num_fc_blks(journal_superblock_t *jsb)
 {
 	int num_fc_blocks = be32_to_cpu(jsb->s_num_fc_blks);
@@ -1794,26 +1784,11 @@ static inline unsigned long jbd2_log_space_left(journal_t *journal)
 #define JBD_MAX_CHECKSUM_SIZE 4
 
 static inline u32 jbd2_chksum(journal_t *journal, u32 crc,
 			      const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[JBD_MAX_CHECKSUM_SIZE];
-	} desc;
-	int err;
-
-	BUG_ON(crypto_shash_descsize(journal->j_chksum_driver) >
-		JBD_MAX_CHECKSUM_SIZE);
-
-	desc.shash.tfm = journal->j_chksum_driver;
-	*(u32 *)desc.ctx = crc;
-
-	err = crypto_shash_update(&desc.shash, address, length);
-	BUG_ON(err);
-
-	return *(u32 *)desc.ctx;
+	return crc32c(crc, address, length);
 }
 
 /* Return most recent uncommitted transaction */
 static inline tid_t  jbd2_get_latest_transaction(journal_t *journal)
 {
Signature · 1 line
2.47.0
author maintainer
#38 lore raw

PATCH v2 17/18 f2fs: switch to using the crc32 library

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32() library function takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

---
 fs/f2fs/Kconfig |  3 +--
 fs/f2fs/f2fs.h  | 19 +------------------
 fs/f2fs/super.c | 15 ---------------
 3 files changed, 2 insertions(+), 35 deletions(-)
fs/f2fs/Kconfig +1 -2
index 68a1e23e1557..5916a02fb46d 100644
@@ -2,12 +2,11 @@
 config F2FS_FS
 	tristate "F2FS filesystem support"
 	depends on BLOCK
 	select BUFFER_HEAD
 	select NLS
-	select CRYPTO
-	select CRYPTO_CRC32
+	select CRC32
 	select F2FS_FS_XATTR if FS_ENCRYPTION
 	select FS_ENCRYPTION_ALGS if FS_ENCRYPTION
 	select FS_IOMAP
 	select LZ4_COMPRESS if F2FS_FS_LZ4
 	select LZ4_DECOMPRESS if F2FS_FS_LZ4
fs/f2fs/f2fs.h +1 -18
index 33f5449dc22d..1fc5c2743c8d 100644
@@ -1761,13 +1761,10 @@ struct f2fs_sb_info {
 
 	/* For write statistics */
 	u64 sectors_written_start;
 	u64 kbytes_written;
 
-	/* Reference to checksum algorithm driver via cryptoapi */
-	struct crypto_shash *s_chksum_driver;
-
 	/* Precomputed FS UUID checksum for seeding other checksums */
 	__u32 s_chksum_seed;
 
 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
 
@@ -1941,25 +1938,11 @@ static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
  * Inline functions
  */
 static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
 			      const void *address, unsigned int length)
 {
-	struct {
-		struct shash_desc shash;
-		char ctx[4];
-	} desc;
-	int err;
-
-	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
-
-	desc.shash.tfm = sbi->s_chksum_driver;
-	*(u32 *)desc.ctx = crc;
-
-	err = crypto_shash_update(&desc.shash, address, length);
-	BUG_ON(err);
-
-	return *(u32 *)desc.ctx;
+	return crc32(crc, address, length);
 }
 
 static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
 			   unsigned int length)
 {
fs/f2fs/super.c +0 -15
index 87ab5696bd48..003d3bcb0caa 100644
@@ -1670,12 +1670,10 @@ static void f2fs_put_super(struct super_block *sb)
 
 	f2fs_destroy_post_read_wq(sbi);
 
 	kvfree(sbi->ckpt);
 
-	if (sbi->s_chksum_driver)
-		crypto_free_shash(sbi->s_chksum_driver);
 	kfree(sbi->raw_super);
 
 	f2fs_destroy_page_array_cache(sbi);
 	f2fs_destroy_xattr_caches(sbi);
 #ifdef CONFIG_QUOTA
@@ -4419,19 +4417,10 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 		INIT_LIST_HEAD(&sbi->inode_list[i]);
 		spin_lock_init(&sbi->inode_lock[i]);
 	}
 	mutex_init(&sbi->flush_lock);
 
-	/* Load the checksum driver */
-	sbi->s_chksum_driver = crypto_alloc_shash("crc32", 0, 0);
-	if (IS_ERR(sbi->s_chksum_driver)) {
-		f2fs_err(sbi, "Cannot load crc32 driver.");
-		err = PTR_ERR(sbi->s_chksum_driver);
-		sbi->s_chksum_driver = NULL;
-		goto free_sbi;
-	}
-
 	/* set a block size */
 	if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
 		f2fs_err(sbi, "unable to set blocksize");
 		goto free_sbi;
 	}
@@ -4872,12 +4861,10 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 	fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
 	kvfree(options);
 free_sb_buf:
 	kfree(raw_super);
 free_sbi:
-	if (sbi->s_chksum_driver)
-		crypto_free_shash(sbi->s_chksum_driver);
 	kfree(sbi);
 	sb->s_fs_info = NULL;
 
 	/* give only one another chance */
 	if (retry_cnt > 0 && skip_recovery) {
@@ -5080,7 +5067,5 @@ module_init(init_f2fs_fs)
 module_exit(exit_f2fs_fs)
 
 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
 MODULE_DESCRIPTION("Flash Friendly File System");
 MODULE_LICENSE("GPL");
-MODULE_SOFTDEP("pre: crc32");
-
Signature · 1 line
2.47.0
author maintainer
#39 lore raw

PATCH v2 18/18 scsi: target: iscsi: switch to using the crc32c library

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32c() library function directly takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32c(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

---
 drivers/target/iscsi/Kconfig              |   2 +-
 drivers/target/iscsi/iscsi_target.c       | 153 +++++++---------------
 drivers/target/iscsi/iscsi_target_login.c |  50 -------
 drivers/target/iscsi/iscsi_target_login.h |   1 -
 drivers/target/iscsi/iscsi_target_nego.c  |  21 +--
 include/target/iscsi/iscsi_target_core.h  |   3 -
 6 files changed, 48 insertions(+), 182 deletions(-)
drivers/target/iscsi/Kconfig +1 -1
index 1c0517a12571..2f2f3dc831b8 100644
@@ -1,11 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config ISCSI_TARGET
 	tristate "SCSI Target Mode Stack"
 	depends on INET
+	select CRC32
 	select CRYPTO
-	select CRYPTO_CRC32C
 	help
 	Say M to enable the SCSI target mode stack. A SCSI target mode stack
 	is software that makes local storage available over a storage network
 	to a SCSI initiator system. The supported storage network technologies
 	include iSCSI, Fibre Channel and the SCSI RDMA Protocol (SRP).
drivers/target/iscsi/iscsi_target.c +45 -108

309 lines not on this page · raw message

drivers/target/iscsi/iscsi_target_login.c +0 -50

80 lines not on this page · raw message

drivers/target/iscsi/iscsi_target_login.h +0 -1
index e8760735486b..03c7d695d58f 100644
@@ -7,11 +7,10 @@
 struct iscsit_conn;
 struct iscsi_login;
 struct iscsi_np;
 struct sockaddr_storage;
 
-extern int iscsi_login_setup_crypto(struct iscsit_conn *);
 extern int iscsi_check_for_session_reinstatement(struct iscsit_conn *);
 extern int iscsi_login_post_auth_non_zero_tsih(struct iscsit_conn *, u16, u32);
 extern int iscsit_setup_np(struct iscsi_np *,
 				struct sockaddr_storage *);
 extern int iscsi_target_setup_login_socket(struct iscsi_np *,
drivers/target/iscsi/iscsi_target_nego.c +2 -19
index fa3fb5f4e6bc..16e3ded98c32 100644
@@ -1192,18 +1192,11 @@ int iscsi_target_locate_portal(
 	if (!sessiontype) {
 		if (!login->leading_connection)
 			goto get_target;
 
 		sess->sess_ops->SessionType = 1;
-		/*
-		 * Setup crc32c modules from libcrypto
-		 */
-		if (iscsi_login_setup_crypto(conn) < 0) {
-			pr_err("iscsi_login_setup_crypto() failed\n");
-			ret = -1;
-			goto out;
-		}
+
 		/*
 		 * Serialize access across the discovery struct iscsi_portal_group to
 		 * process login attempt.
 		 */
 		conn->tpg = iscsit_global->discovery_tpg;
@@ -1256,21 +1249,11 @@ int iscsi_target_locate_portal(
 		ret = -1;
 		goto out;
 	}
 	conn->tpg_np = tpg_np;
 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
-	/*
-	 * Setup crc32c modules from libcrypto
-	 */
-	if (iscsi_login_setup_crypto(conn) < 0) {
-		pr_err("iscsi_login_setup_crypto() failed\n");
-		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
-		iscsit_put_tiqn_for_login(tiqn);
-		conn->tpg = NULL;
-		ret = -1;
-		goto out;
-	}
+
 	/*
 	 * Serialize access across the struct iscsi_portal_group to
 	 * process login attempt.
 	 */
 	if (iscsit_access_np(np, conn->tpg) < 0) {
include/target/iscsi/iscsi_target_core.h +0 -3
index 60af7c63b34e..51ca80abacf7 100644
@@ -574,13 +574,10 @@ struct iscsit_conn {
 	spinlock_t		nopin_timer_lock;
 	spinlock_t		response_queue_lock;
 	spinlock_t		state_lock;
 	spinlock_t		login_timer_lock;
 	spinlock_t		login_worker_lock;
-	/* libcrypto RX and TX contexts for crc32c */
-	struct ahash_request	*conn_rx_hash;
-	struct ahash_request	*conn_tx_hash;
 	/* Used for scheduling TX and RX connection kthreads */
 	cpumask_var_t		conn_cpumask;
 	cpumask_var_t		allowed_cpumask;
 	unsigned int		conn_rx_reset_cpumask:1;
 	unsigned int		conn_tx_reset_cpumask:1;
Signature · 1 line
2.47.0
reviewer Reviewed-by on v2 18/18
#40 lore raw

Re: [PATCH v2 18/18] scsi: target: iscsi: switch to using the crc32c library

Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Now that the crc32c() library function directly takes advantage of architecture-specific optimizations, it is unnecessary to go through the crypto API. Just use crc32c(). This is much simpler, and it improves performance due to eliminating the crypto API overhead.

Quoted 525 lines
---
 drivers/target/iscsi/Kconfig              |   2 +-
 drivers/target/iscsi/iscsi_target.c       | 153 +++++++---------------
 drivers/target/iscsi/iscsi_target_login.c |  50 -------
 drivers/target/iscsi/iscsi_target_login.h |   1 -
 drivers/target/iscsi/iscsi_target_nego.c  |  21 +--
 include/target/iscsi/iscsi_target_core.h  |   3 -
 6 files changed, 48 insertions(+), 182 deletions(-)
drivers/target/iscsi/Kconfig +1 -1
index 1c0517a12571..2f2f3dc831b8 100644
@@ -1,11 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config ISCSI_TARGET
        tristate "SCSI Target Mode Stack"
        depends on INET
+       select CRC32
        select CRYPTO
-       select CRYPTO_CRC32C
        help
        Say M to enable the SCSI target mode stack. A SCSI target mode stack
        is software that makes local storage available over a storage network
        to a SCSI initiator system. The supported storage network technologies
        include iSCSI, Fibre Channel and the SCSI RDMA Protocol (SRP).
drivers/target/iscsi/iscsi_target.c +45 -108
index 6002283cbeba..091c1efccfb7 100644
@@ -6,11 +6,11 @@
  *
  * Author: Nicholas A. Bellinger <[email protected]>
  *
  ******************************************************************************/
 
-#include <crypto/hash.h>
+#include <linux/crc32c.h>
 #include <linux/string.h>
 #include <linux/kthread.h>
 #include <linux/completion.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
@@ -488,12 +488,12 @@ void iscsit_aborted_task(struct iscsit_conn *conn, struct iscsit_cmd *cmd)
 
        __iscsit_free_cmd(cmd, true);
 }
 EXPORT_SYMBOL(iscsit_aborted_task);
 
-static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
-                                     u32, u32, const void *, void *);
+static u32 iscsit_crc_buf(const void *buf, u32 payload_length,
+                         u32 padding, const void *pad_bytes);
 static void iscsit_tx_thread_wait_for_tcp(struct iscsit_conn *);
 
 static int
 iscsit_xmit_nondatain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                          const void *data_buf, u32 data_buf_len)
@@ -508,13 +508,11 @@ iscsit_xmit_nondatain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
        iov[niov++].iov_len     = ISCSI_HDR_LEN;
 
        if (conn->conn_ops->HeaderDigest) {
                u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
 
-               iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
-                                         ISCSI_HDR_LEN, 0, NULL,
-                                         header_digest);
+               *header_digest = iscsit_crc_buf(hdr, ISCSI_HDR_LEN, 0, NULL);
 
                iov[0].iov_len += ISCSI_CRC_LEN;
                tx_size += ISCSI_CRC_LEN;
                pr_debug("Attaching CRC32C HeaderDigest"
                         " to opcode 0x%x 0x%08x\n",
@@ -535,15 +533,13 @@ iscsit_xmit_nondatain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                        pr_debug("Attaching %u additional"
                                 " padding bytes.\n", padding);
                }
 
                if (conn->conn_ops->DataDigest) {
-                       iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
-                                                 data_buf, data_buf_len,
-                                                 padding, &cmd->pad_bytes,
-                                                 &cmd->data_crc);
-
+                       cmd->data_crc = iscsit_crc_buf(data_buf, data_buf_len,
+                                                      padding,
+                                                      &cmd->pad_bytes);
                        iov[niov].iov_base = &cmd->data_crc;
                        iov[niov++].iov_len = ISCSI_CRC_LEN;
                        tx_size += ISCSI_CRC_LEN;
                        pr_debug("Attached DataDigest for %u"
                                 " bytes opcode 0x%x, CRC 0x%08x\n",
@@ -564,12 +560,12 @@ iscsit_xmit_nondatain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
 }
 
 static int iscsit_map_iovec(struct iscsit_cmd *cmd, struct kvec *iov, int nvec,
                            u32 data_offset, u32 data_length);
 static void iscsit_unmap_iovec(struct iscsit_cmd *);
-static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsit_cmd *,
-                                   u32, u32, u32, u8 *);
+static u32 iscsit_crc_sglist(const struct iscsit_cmd *cmd, u32 data_length,
+                            u32 padding, const u8 *pad_bytes);
 static int
 iscsit_xmit_datain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                       const struct iscsi_datain *datain)
 {
        struct kvec *iov;
@@ -582,14 +578,12 @@ iscsit_xmit_datain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
        tx_size += ISCSI_HDR_LEN;
 
        if (conn->conn_ops->HeaderDigest) {
                u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
 
-               iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
-                                         ISCSI_HDR_LEN, 0, NULL,
-                                         header_digest);
-
+               *header_digest = iscsit_crc_buf(cmd->pdu, ISCSI_HDR_LEN, 0,
+                                               NULL);
                iov[0].iov_len += ISCSI_CRC_LEN;
                tx_size += ISCSI_CRC_LEN;
 
                pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
                         *header_digest);
@@ -612,16 +606,12 @@ iscsit_xmit_datain_pdu(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
 
                pr_debug("Attaching %u padding bytes\n", cmd->padding);
        }
 
        if (conn->conn_ops->DataDigest) {
-               cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
-                                                        cmd, datain->offset,
-                                                        datain->length,
-                                                        cmd->padding,
-                                                        cmd->pad_bytes);
-
+               cmd->data_crc = iscsit_crc_sglist(cmd, datain->length,
+                                                 cmd->padding, cmd->pad_bytes);
                iov[iov_count].iov_base = &cmd->data_crc;
                iov[iov_count++].iov_len = ISCSI_CRC_LEN;
                tx_size += ISCSI_CRC_LEN;
 
                pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
@@ -1402,81 +1392,49 @@ iscsit_handle_scsi_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                return 0;
 
        return iscsit_get_immediate_data(cmd, hdr, dump_payload);
 }
 
-static u32 iscsit_do_crypto_hash_sg(
-       struct ahash_request *hash,
-       struct iscsit_cmd *cmd,
-       u32 data_offset,
-       u32 data_length,
-       u32 padding,
-       u8 *pad_bytes)
+static u32 iscsit_crc_sglist(const struct iscsit_cmd *cmd, u32 data_length,
+                            u32 padding, const u8 *pad_bytes)
 {
-       u32 data_crc;
-       struct scatterlist *sg;
-       unsigned int page_off;
-
-       crypto_ahash_init(hash);
-
-       sg = cmd->first_data_sg;
-       page_off = cmd->first_data_sg_off;
-
-       if (data_length && page_off) {
-               struct scatterlist first_sg;
-               u32 len = min_t(u32, data_length, sg->length - page_off);
-
-               sg_init_table(&first_sg, 1);
-               sg_set_page(&first_sg, sg_page(sg), len, sg->offset + page_off);
-
-               ahash_request_set_crypt(hash, &first_sg, NULL, len);
-               crypto_ahash_update(hash);
-
-               data_length -= len;
-               sg = sg_next(sg);
-       }
+       struct scatterlist *sg = cmd->first_data_sg;
+       unsigned int page_off = cmd->first_data_sg_off;
+       u32 crc = ~0;
 
        while (data_length) {
-               u32 cur_len = min_t(u32, data_length, sg->length);
+               u32 cur_len = min_t(u32, data_length, sg->length - page_off);
+               const void *virt;
 
-               ahash_request_set_crypt(hash, sg, NULL, cur_len);
-               crypto_ahash_update(hash);
+               virt = kmap_local_page(sg_page(sg)) + sg->offset + page_off;
+               crc = crc32c(crc, virt, cur_len);
+               kunmap_local(virt);
 
-               data_length -= cur_len;
                /* iscsit_map_iovec has already checked for invalid sg pointers */
                sg = sg_next(sg);
-       }
 
-       if (padding) {
-               struct scatterlist pad_sg;
-
-               sg_init_one(&pad_sg, pad_bytes, padding);
-               ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
-                                       padding);
-               crypto_ahash_finup(hash);
-       } else {
-               ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
-               crypto_ahash_final(hash);
+               page_off = 0;
+               data_length -= cur_len;
        }
 
-       return data_crc;
+       if (padding)
+               crc = crc32c(crc, pad_bytes, padding);
+
+       return ~crc;
 }
 
-static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
-       const void *buf, u32 payload_length, u32 padding,
-       const void *pad_bytes, void *data_crc)
+static u32 iscsit_crc_buf(const void *buf, u32 payload_length,
+                         u32 padding, const void *pad_bytes)
 {
-       struct scatterlist sg[2];
+       u32 crc = ~0;
 
-       sg_init_table(sg, ARRAY_SIZE(sg));
-       sg_set_buf(sg, buf, payload_length);
-       if (padding)
-               sg_set_buf(sg + 1, pad_bytes, padding);
+       crc = crc32c(crc, buf, payload_length);
 
-       ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
+       if (padding)
+               crc = crc32c(crc, pad_bytes, padding);
 
-       crypto_ahash_digest(hash);
+       return ~crc;
 }
 
 int
 __iscsit_check_dataout_hdr(struct iscsit_conn *conn, void *buf,
                           struct iscsit_cmd *cmd, u32 payload_length,
@@ -1660,15 +1618,12 @@ iscsit_get_dataout(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                return -1;
 
        if (conn->conn_ops->DataDigest) {
                u32 data_crc;
 
-               data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
-                                                   be32_to_cpu(hdr->offset),
-                                                   payload_length, padding,
-                                                   cmd->pad_bytes);
-
+               data_crc = iscsit_crc_sglist(cmd, payload_length, padding,
+                                            cmd->pad_bytes);
                if (checksum != data_crc) {
                        pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
                                " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
                                " does not match computed 0x%08x\n",
                                hdr->itt, hdr->offset, payload_length,
@@ -1923,14 +1878,12 @@ static int iscsit_handle_nop_out(struct iscsit_conn *conn, struct iscsit_cmd *cm
                        ret = -1;
                        goto out;
                }
 
                if (conn->conn_ops->DataDigest) {
-                       iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
-                                                 payload_length, padding,
-                                                 cmd->pad_bytes, &data_crc);
-
+                       data_crc = iscsit_crc_buf(ping_data, payload_length,
+                                                 padding, cmd->pad_bytes);
                        if (checksum != data_crc) {
                                pr_err("Ping data CRC32C DataDigest"
                                " 0x%08x does not match computed 0x%08x\n",
                                        checksum, data_crc);
                                if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
@@ -2326,14 +2279,11 @@ iscsit_handle_text_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
                rx_got = rx_data(conn, &iov[0], niov, rx_size);
                if (rx_got != rx_size)
                        goto reject;
 
                if (conn->conn_ops->DataDigest) {
-                       iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
-                                                 text_in, rx_size, 0, NULL,
-                                                 &data_crc);
-
+                       data_crc = iscsit_crc_buf(text_in, rx_size, 0, NULL);
                        if (checksum != data_crc) {
                                pr_err("Text data CRC32C DataDigest"
                                        " 0x%08x does not match computed"
                                        " 0x%08x\n", checksum, data_crc);
                                if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
@@ -2686,14 +2636,12 @@ static int iscsit_handle_immediate_data(
        }
 
        if (conn->conn_ops->DataDigest) {
                u32 data_crc;
 
-               data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
-                                                   cmd->write_data_done, length, padding,
-                                                   cmd->pad_bytes);
-
+               data_crc = iscsit_crc_sglist(cmd, length, padding,
+                                            cmd->pad_bytes);
                if (checksum != data_crc) {
                        pr_err("ImmediateData CRC32C DataDigest 0x%08x"
                                " does not match computed 0x%08x\n", checksum,
                                data_crc);
 
@@ -4114,14 +4062,12 @@ static void iscsit_get_rx_pdu(struct iscsit_conn *conn)
                        if (ret != ISCSI_CRC_LEN) {
                                iscsit_rx_thread_wait_for_tcp(conn);
                                break;
                        }
 
-                       iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
-                                                 ISCSI_HDR_LEN, 0, NULL,
-                                                 &checksum);
-
+                       checksum = iscsit_crc_buf(buffer, ISCSI_HDR_LEN, 0,
+                                                 NULL);
                        if (digest != checksum) {
                                pr_err("HeaderDigest CRC32C failed,"
                                        " received 0x%08x, computed 0x%08x\n",
                                        digest, checksum);
                                /*
@@ -4404,19 +4350,10 @@ int iscsit_close_connection(
         * If any other processes are accessing this connection pointer we
         * must wait until they have completed.
         */
        iscsit_check_conn_usage_count(conn);
 
-       ahash_request_free(conn->conn_tx_hash);
-       if (conn->conn_rx_hash) {
-               struct crypto_ahash *tfm;
-
-               tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
-               ahash_request_free(conn->conn_rx_hash);
-               crypto_free_ahash(tfm);
-       }
-
        if (conn->sock)
                sock_release(conn->sock);
 
        if (conn->conn_transport->iscsit_free_conn)
                conn->conn_transport->iscsit_free_conn(conn);
drivers/target/iscsi/iscsi_target_login.c +0 -50
index 90b870f234f0..c2ac9a99ebbb 100644
@@ -6,11 +6,10 @@
  *
  * Author: Nicholas A. Bellinger <[email protected]>
  *
  ******************************************************************************/
 
-#include <crypto/hash.h>
 #include <linux/module.h>
 #include <linux/string.h>
 #include <linux/kthread.h>
 #include <linux/sched/signal.h>
 #include <linux/idr.h>
@@ -69,50 +68,10 @@ static struct iscsi_login *iscsi_login_init_conn(struct iscsit_conn *conn)
 out_login:
        kfree(login);
        return NULL;
 }
 
-/*
- * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
- * per struct iscsit_conn libcrypto contexts for crc32c and crc32-intel
- */
-int iscsi_login_setup_crypto(struct iscsit_conn *conn)
-{
-       struct crypto_ahash *tfm;
-
-       /*
-        * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
-        * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
-        * to software 1x8 byte slicing from crc32c.ko
-        */
-       tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
-       if (IS_ERR(tfm)) {
-               pr_err("crypto_alloc_ahash() failed\n");
-               return -ENOMEM;
-       }
-
-       conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
-       if (!conn->conn_rx_hash) {
-               pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
-               crypto_free_ahash(tfm);
-               return -ENOMEM;
-       }
-       ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
-
-       conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
-       if (!conn->conn_tx_hash) {
-               pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
-               ahash_request_free(conn->conn_rx_hash);
-               conn->conn_rx_hash = NULL;
-               crypto_free_ahash(tfm);
-               return -ENOMEM;
-       }
-       ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
-
-       return 0;
-}
-
 static int iscsi_login_check_initiator_version(
        struct iscsit_conn *conn,
        u8 version_max,
        u8 version_min)
 {
@@ -1163,19 +1122,10 @@ void iscsi_target_login_sess_out(struct iscsit_conn *conn,
                } else
                        spin_unlock_bh(&conn->sess->conn_lock);
                iscsit_dec_session_usage_count(conn->sess);
        }
 
-       ahash_request_free(conn->conn_tx_hash);
-       if (conn->conn_rx_hash) {
-               struct crypto_ahash *tfm;
-
-               tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
-               ahash_request_free(conn->conn_rx_hash);
-               crypto_free_ahash(tfm);
-       }
-
        if (conn->param_list) {
                iscsi_release_param_list(conn->param_list);
                conn->param_list = NULL;
        }
        iscsi_target_nego_release(conn);
drivers/target/iscsi/iscsi_target_login.h +0 -1
index e8760735486b..03c7d695d58f 100644
@@ -7,11 +7,10 @@
 struct iscsit_conn;
 struct iscsi_login;
 struct iscsi_np;
 struct sockaddr_storage;
 
-extern int iscsi_login_setup_crypto(struct iscsit_conn *);
 extern int iscsi_check_for_session_reinstatement(struct iscsit_conn *);
 extern int iscsi_login_post_auth_non_zero_tsih(struct iscsit_conn *, u16, u32);
 extern int iscsit_setup_np(struct iscsi_np *,
                                struct sockaddr_storage *);
 extern int iscsi_target_setup_login_socket(struct iscsi_np *,
drivers/target/iscsi/iscsi_target_nego.c +2 -19
index fa3fb5f4e6bc..16e3ded98c32 100644
@@ -1192,18 +1192,11 @@ int iscsi_target_locate_portal(
        if (!sessiontype) {
                if (!login->leading_connection)
                        goto get_target;
 
                sess->sess_ops->SessionType = 1;
-               /*
-                * Setup crc32c modules from libcrypto
-                */
-               if (iscsi_login_setup_crypto(conn) < 0) {
-                       pr_err("iscsi_login_setup_crypto() failed\n");
-                       ret = -1;
-                       goto out;
-               }
+
                /*
                 * Serialize access across the discovery struct iscsi_portal_group to
                 * process login attempt.
                 */
                conn->tpg = iscsit_global->discovery_tpg;
@@ -1256,21 +1249,11 @@ int iscsi_target_locate_portal(
                ret = -1;
                goto out;
        }
        conn->tpg_np = tpg_np;
        pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
-       /*
-        * Setup crc32c modules from libcrypto
-        */
-       if (iscsi_login_setup_crypto(conn) < 0) {
-               pr_err("iscsi_login_setup_crypto() failed\n");
-               kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
-               iscsit_put_tiqn_for_login(tiqn);
-               conn->tpg = NULL;
-               ret = -1;
-               goto out;
-       }
+
        /*
         * Serialize access across the struct iscsi_portal_group to
         * process login attempt.
         */
        if (iscsit_access_np(np, conn->tpg) < 0) {
include/target/iscsi/iscsi_target_core.h +0 -4
index 60af7c63b34e..51ca80abacf7 100644
@@ -574,13 +574,10 @@ struct iscsit_conn {
        spinlock_t              nopin_timer_lock;
        spinlock_t              response_queue_lock;
        spinlock_t              state_lock;
        spinlock_t              login_timer_lock;
        spinlock_t              login_worker_lock;
-       /* libcrypto RX and TX contexts for crc32c */
-       struct ahash_request    *conn_rx_hash;
-       struct ahash_request    *conn_tx_hash;
        /* Used for scheduling TX and RX connection kthreads */
        cpumask_var_t           conn_cpumask;
        cpumask_var_t           allowed_cpumask;
        unsigned int            conn_rx_reset_cpumask:1;
        unsigned int            conn_tx_reset_cpumask:1;
--

2.47.0