Skip to content

Commit 780acb2

Browse files
committed
crypto: crc32 - don't unnecessarily register arch algorithms
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.) Reviewed-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent b5ae12e commit 780acb2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

crypto/crc32_generic.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,19 @@ static struct shash_alg algs[] = {{
157157
.base.cra_init = crc32_cra_init,
158158
}};
159159

160+
static int num_algs;
161+
160162
static int __init crc32_mod_init(void)
161163
{
162164
/* register the arch flavor only if it differs from the generic one */
163-
return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
165+
num_algs = 1 + ((crc32_optimizations() & CRC32_LE_OPTIMIZATION) != 0);
166+
167+
return crypto_register_shashes(algs, num_algs);
164168
}
165169

166170
static void __exit crc32_mod_fini(void)
167171
{
168-
crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
172+
crypto_unregister_shashes(algs, num_algs);
169173
}
170174

171175
subsys_initcall(crc32_mod_init);

crypto/crc32c_generic.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,19 @@ static struct shash_alg algs[] = {{
197197
.base.cra_init = crc32c_cra_init,
198198
}};
199199

200+
static int num_algs;
201+
200202
static int __init crc32c_mod_init(void)
201203
{
202204
/* register the arch flavor only if it differs from the generic one */
203-
return crypto_register_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
205+
num_algs = 1 + ((crc32_optimizations() & CRC32C_OPTIMIZATION) != 0);
206+
207+
return crypto_register_shashes(algs, num_algs);
204208
}
205209

206210
static void __exit crc32c_mod_fini(void)
207211
{
208-
crypto_unregister_shashes(algs, 1 + IS_ENABLED(CONFIG_CRC32_ARCH));
212+
crypto_unregister_shashes(algs, num_algs);
209213
}
210214

211215
subsys_initcall(crc32c_mod_init);

0 commit comments

Comments
 (0)