Skip to content

Commit 2e8f7b1

Browse files
author
Eric Biggers
committed
lib/crypto: blake2b: Roll up BLAKE2b round loop on 32-bit
BLAKE2b has a state of 16 64-bit words. Add the message data in and there are 32 64-bit words. With the current code where all the rounds are unrolled to enable constant-folding of the blake2b_sigma values, this results in a very large code size on 32-bit kernels, including a recurring issue where gcc uses a large amount of stack. There's just not much benefit to this unrolling when the code is already so large. Let's roll up the rounds when !CONFIG_64BIT. To avoid having to duplicate the code, just write the code once using a loop, and conditionally use 'unrolled_full' from <linux/unroll.h>. Then, fold the now-unneeded ROUND() macro into the loop. Finally, also remove the now-unneeded override of the stack frame size warning. Code size improvements for blake2b_compress_generic(): Size before (bytes) Size after (bytes) ------------------- ------------------ i386, gcc 27584 3632 i386, clang 18208 3248 arm32, gcc 19912 2860 arm32, clang 21336 3344 Running the BLAKE2b benchmark on a !CONFIG_64BIT kernel on an x86_64 processor shows a 16384B throughput change of 351 => 340 MB/s (gcc) or 442 MB/s => 375 MB/s (clang). So clearly not much of a slowdown either. But also that microbenchmark also effectively disregards cache usage, which is important in practice and is far better in the smaller code. Note: If we rolled up the loop on x86_64 too, the change would be 7024 bytes => 1584 bytes and 1960 MB/s => 1396 MB/s (gcc), or 6848 bytes => 1696 bytes and 1920 MB/s => 1263 MB/s (clang). Maybe still worth it, though not quite as clearly beneficial. Fixes: 91d6893 ("crypto: blake2b - add blake2b generic implementation") Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20251205050330.89704-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 1cd5bb6 commit 2e8f7b1

2 files changed

Lines changed: 20 additions & 25 deletions

File tree

lib/crypto/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o
3333

3434
obj-$(CONFIG_CRYPTO_LIB_BLAKE2B) += libblake2b.o
3535
libblake2b-y := blake2b.o
36-
CFLAGS_blake2b.o := -Wframe-larger-than=4096 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930
3736
ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2B_ARCH),y)
3837
CFLAGS_blake2b.o += -I$(src)/$(SRCARCH)
3938
libblake2b-$(CONFIG_ARM) += arm/blake2b-neon-core.o

lib/crypto/blake2b.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/kernel.h>
1515
#include <linux/module.h>
1616
#include <linux/string.h>
17+
#include <linux/unroll.h>
1718
#include <linux/types.h>
1819

1920
static const u8 blake2b_sigma[12][16] = {
@@ -73,31 +74,26 @@ blake2b_compress_generic(struct blake2b_ctx *ctx,
7374
b = ror64(b ^ c, 63); \
7475
} while (0)
7576

76-
#define ROUND(r) do { \
77-
G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
78-
G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
79-
G(r, 2, v[2], v[ 6], v[10], v[14]); \
80-
G(r, 3, v[3], v[ 7], v[11], v[15]); \
81-
G(r, 4, v[0], v[ 5], v[10], v[15]); \
82-
G(r, 5, v[1], v[ 6], v[11], v[12]); \
83-
G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
84-
G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
85-
} while (0)
86-
ROUND(0);
87-
ROUND(1);
88-
ROUND(2);
89-
ROUND(3);
90-
ROUND(4);
91-
ROUND(5);
92-
ROUND(6);
93-
ROUND(7);
94-
ROUND(8);
95-
ROUND(9);
96-
ROUND(10);
97-
ROUND(11);
98-
77+
#ifdef CONFIG_64BIT
78+
/*
79+
* Unroll the rounds loop to enable constant-folding of the
80+
* blake2b_sigma values. Seems worthwhile on 64-bit kernels.
81+
* Not worthwhile on 32-bit kernels because the code size is
82+
* already so large there due to BLAKE2b using 64-bit words.
83+
*/
84+
unrolled_full
85+
#endif
86+
for (int r = 0; r < 12; r++) {
87+
G(r, 0, v[0], v[4], v[8], v[12]);
88+
G(r, 1, v[1], v[5], v[9], v[13]);
89+
G(r, 2, v[2], v[6], v[10], v[14]);
90+
G(r, 3, v[3], v[7], v[11], v[15]);
91+
G(r, 4, v[0], v[5], v[10], v[15]);
92+
G(r, 5, v[1], v[6], v[11], v[12]);
93+
G(r, 6, v[2], v[7], v[8], v[13]);
94+
G(r, 7, v[3], v[4], v[9], v[14]);
95+
}
9996
#undef G
100-
#undef ROUND
10197

10298
for (i = 0; i < 8; ++i)
10399
ctx->h[i] ^= v[i] ^ v[i + 8];

0 commit comments

Comments
 (0)