]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
crypto/crc32[c]: register only "-lib" drivers
authorEric Biggers <ebiggers@kernel.org>
Fri, 13 Jun 2025 18:37:53 +0000 (11:37 -0700)
committerEric Biggers <ebiggers@kernel.org>
Mon, 30 Jun 2025 16:31:56 +0000 (09:31 -0700)
For the "crc32" and "crc32c" shash algorithms, instead of registering
"*-generic" drivers as well as conditionally registering "*-$(ARCH)"
drivers, instead just register "*-lib" drivers.  These just use the
regular library functions crc32_le() and crc32c(), so they just do the
right thing and are fully accelerated when supported by the CPU.

This eliminates the need for the CRC library to export crc32_le_base()
and crc32c_base().  Separate commits make those static functions.

Since this commit removes the "crc32-generic" and "crc32c-generic"
driver names which crypto/testmgr.c expects to exist, update testmgr.c
accordingly.  This does mean that testmgr.c will no longer fuzz-test the
"generic" implementation against the "arch" implementation for crc32 and
crc32c, but this was redundant with crc_kunit anyway.

Besides the above, and btrfs_init_csum_hash() which the previous commit
fixed, no code appears to have been relying on the "crc32-generic" or
"crc32c-generic" driver names specifically.

btrfs does export the checksum name and checksum driver name in
/sys/fs/btrfs/$uuid/checksum.  This commit makes the driver name portion
of that file contain "crc32c-lib" instead of "crc32c-generic" or
"crc32c-$(ARCH)".  This should be fine, since in practice the purpose of
the driver name portion of this file seems to have been just to allow
users to manually check whether they needed to enable the optimized
CRC32C code.  This was needed only because of the bug in old kernels
where the optimized CRC32C code defaulted to off and even needed to be
explicitly added to the ramdisk to be used.  Now that it just works in
Linux 6.14 and later, there's no need for users to take any action and
the driver name portion of this is basically obsolete.  (Also, note that
the crc32c driver name already changed in 6.14.)

Acked-by: David Sterba <dsterba@suse.com>
Link: https://lore.kernel.org/r/20250613183753.31864-3-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
crypto/Makefile
crypto/crc32.c
crypto/crc32c.c
crypto/testmgr.c

index 017df3a2e4bb3174edbf0ed7c8876c02511dc203..55dd56332dc80b1cf845b3b13b2a1742494c4276 100644 (file)
@@ -154,10 +154,8 @@ obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c-cryptoapi.o
 crc32c-cryptoapi-y := crc32c.o
-CFLAGS_crc32c.o += -DARCH=$(ARCH)
 obj-$(CONFIG_CRYPTO_CRC32) += crc32-cryptoapi.o
 crc32-cryptoapi-y := crc32.o
-CFLAGS_crc32.o += -DARCH=$(ARCH)
 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
 obj-$(CONFIG_CRYPTO_KRB5ENC) += krb5enc.o
 obj-$(CONFIG_CRYPTO_LZO) += lzo.o lzo-rle.o
index cc371d42601fda164c23ce4c63d328d687ff5b65..489cbed9422e2b2e7de65e6dec914fb9f9aa7f3d 100644 (file)
@@ -59,29 +59,12 @@ static int crc32_update(struct shash_desc *desc, const u8 *data,
 {
        u32 *crcp = shash_desc_ctx(desc);
 
-       *crcp = crc32_le_base(*crcp, data, len);
-       return 0;
-}
-
-static int crc32_update_arch(struct shash_desc *desc, const u8 *data,
-                            unsigned int len)
-{
-       u32 *crcp = shash_desc_ctx(desc);
-
        *crcp = crc32_le(*crcp, data, len);
        return 0;
 }
 
 /* No final XOR 0xFFFFFFFF, like crc32_le */
-static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
-                        u8 *out)
-{
-       put_unaligned_le32(crc32_le_base(*crcp, data, len), out);
-       return 0;
-}
-
-static int __crc32_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
-                             u8 *out)
+static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
 {
        put_unaligned_le32(crc32_le(*crcp, data, len), out);
        return 0;
@@ -93,12 +76,6 @@ static int crc32_finup(struct shash_desc *desc, const u8 *data,
        return __crc32_finup(shash_desc_ctx(desc), data, len, out);
 }
 
-static int crc32_finup_arch(struct shash_desc *desc, const u8 *data,
-                      unsigned int len, u8 *out)
-{
-       return __crc32_finup_arch(shash_desc_ctx(desc), data, len, out);
-}
-
 static int crc32_final(struct shash_desc *desc, u8 *out)
 {
        u32 *crcp = shash_desc_ctx(desc);
@@ -113,13 +90,7 @@ static int crc32_digest(struct shash_desc *desc, const u8 *data,
        return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, out);
 }
 
-static int crc32_digest_arch(struct shash_desc *desc, const u8 *data,
-                            unsigned int len, u8 *out)
-{
-       return __crc32_finup_arch(crypto_shash_ctx(desc->tfm), data, len, out);
-}
-
-static struct shash_alg algs[] = {{
+static struct shash_alg alg = {
        .setkey                 = crc32_setkey,
        .init                   = crc32_init,
        .update                 = crc32_update,
@@ -130,46 +101,23 @@ static struct shash_alg algs[] = {{
        .digestsize             = CHKSUM_DIGEST_SIZE,
 
        .base.cra_name          = "crc32",
-       .base.cra_driver_name   = "crc32-generic",
+       .base.cra_driver_name   = "crc32-lib",
        .base.cra_priority      = 100,
        .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
        .base.cra_ctxsize       = sizeof(u32),
        .base.cra_module        = THIS_MODULE,
        .base.cra_init          = crc32_cra_init,
-}, {
-       .setkey                 = crc32_setkey,
-       .init                   = crc32_init,
-       .update                 = crc32_update_arch,
-       .final                  = crc32_final,
-       .finup                  = crc32_finup_arch,
-       .digest                 = crc32_digest_arch,
-       .descsize               = sizeof(u32),
-       .digestsize             = CHKSUM_DIGEST_SIZE,
-
-       .base.cra_name          = "crc32",
-       .base.cra_driver_name   = "crc32-" __stringify(ARCH),
-       .base.cra_priority      = 150,
-       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-       .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
-       .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 */
-       num_algs = 1 + ((crc32_optimizations() & CRC32_LE_OPTIMIZATION) != 0);
-
-       return crypto_register_shashes(algs, num_algs);
+       return crypto_register_shash(&alg);
 }
 
 static void __exit crc32_mod_fini(void)
 {
-       crypto_unregister_shashes(algs, num_algs);
+       crypto_unregister_shash(&alg);
 }
 
 module_init(crc32_mod_init);
@@ -179,4 +127,3 @@ MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
 MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_CRYPTO("crc32");
-MODULE_ALIAS_CRYPTO("crc32-generic");
index e5377898414a2cbfc34b72ca5be579efe0a0eb17..1eff54dde2f7464083e762e9c359031f60326d34 100644 (file)
@@ -85,15 +85,6 @@ static int chksum_update(struct shash_desc *desc, const u8 *data,
 {
        struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
 
-       ctx->crc = crc32c_base(ctx->crc, data, length);
-       return 0;
-}
-
-static int chksum_update_arch(struct shash_desc *desc, const u8 *data,
-                             unsigned int length)
-{
-       struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
-
        ctx->crc = crc32c(ctx->crc, data, length);
        return 0;
 }
@@ -107,13 +98,6 @@ static int chksum_final(struct shash_desc *desc, u8 *out)
 }
 
 static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
-{
-       put_unaligned_le32(~crc32c_base(*crcp, data, len), out);
-       return 0;
-}
-
-static int __chksum_finup_arch(u32 *crcp, const u8 *data, unsigned int len,
-                              u8 *out)
 {
        put_unaligned_le32(~crc32c(*crcp, data, len), out);
        return 0;
@@ -127,14 +111,6 @@ static int chksum_finup(struct shash_desc *desc, const u8 *data,
        return __chksum_finup(&ctx->crc, data, len, out);
 }
 
-static int chksum_finup_arch(struct shash_desc *desc, const u8 *data,
-                            unsigned int len, u8 *out)
-{
-       struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
-
-       return __chksum_finup_arch(&ctx->crc, data, len, out);
-}
-
 static int chksum_digest(struct shash_desc *desc, const u8 *data,
                         unsigned int length, u8 *out)
 {
@@ -143,14 +119,6 @@ static int chksum_digest(struct shash_desc *desc, const u8 *data,
        return __chksum_finup(&mctx->key, data, length, out);
 }
 
-static int chksum_digest_arch(struct shash_desc *desc, const u8 *data,
-                             unsigned int length, u8 *out)
-{
-       struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
-
-       return __chksum_finup_arch(&mctx->key, data, length, out);
-}
-
 static int crc32c_cra_init(struct crypto_tfm *tfm)
 {
        struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
@@ -159,7 +127,7 @@ static int crc32c_cra_init(struct crypto_tfm *tfm)
        return 0;
 }
 
-static struct shash_alg algs[] = {{
+static struct shash_alg alg = {
        .digestsize             = CHKSUM_DIGEST_SIZE,
        .setkey                 = chksum_setkey,
        .init                   = chksum_init,
@@ -170,46 +138,23 @@ static struct shash_alg algs[] = {{
        .descsize               = sizeof(struct chksum_desc_ctx),
 
        .base.cra_name          = "crc32c",
-       .base.cra_driver_name   = "crc32c-generic",
+       .base.cra_driver_name   = "crc32c-lib",
        .base.cra_priority      = 100,
        .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
        .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
        .base.cra_ctxsize       = sizeof(struct chksum_ctx),
        .base.cra_module        = THIS_MODULE,
        .base.cra_init          = crc32c_cra_init,
-}, {
-       .digestsize             = CHKSUM_DIGEST_SIZE,
-       .setkey                 = chksum_setkey,
-       .init                   = chksum_init,
-       .update                 = chksum_update_arch,
-       .final                  = chksum_final,
-       .finup                  = chksum_finup_arch,
-       .digest                 = chksum_digest_arch,
-       .descsize               = sizeof(struct chksum_desc_ctx),
-
-       .base.cra_name          = "crc32c",
-       .base.cra_driver_name   = "crc32c-" __stringify(ARCH),
-       .base.cra_priority      = 150,
-       .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
-       .base.cra_blocksize     = CHKSUM_BLOCK_SIZE,
-       .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 */
-       num_algs = 1 + ((crc32_optimizations() & CRC32C_OPTIMIZATION) != 0);
-
-       return crypto_register_shashes(algs, num_algs);
+       return crypto_register_shash(&alg);
 }
 
 static void __exit crc32c_mod_fini(void)
 {
-       crypto_unregister_shashes(algs, num_algs);
+       crypto_unregister_shash(&alg);
 }
 
 module_init(crc32c_mod_init);
@@ -219,4 +164,3 @@ MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_CRYPTO("crc32c");
-MODULE_ALIAS_CRYPTO("crc32c-generic");
index e005af57f0bbd37129af988ba53a056dfc732e1b..78545df20d560dd6e3932d6de42a423188e4344a 100644 (file)
@@ -4502,6 +4502,7 @@ static const struct alg_test_desc alg_test_descs[] = {
                }
        }, {
                .alg = "crc32",
+               .generic_driver = "crc32-lib",
                .test = alg_test_hash,
                .fips_allowed = 1,
                .suite = {
@@ -4509,6 +4510,7 @@ static const struct alg_test_desc alg_test_descs[] = {
                }
        }, {
                .alg = "crc32c",
+               .generic_driver = "crc32c-lib",
                .test = alg_test_hash,
                .fips_allowed = 1,
                .suite = {