Skip to content

Commit f7e5b9b

Browse files
arndbkuba-moo
authored andcommitted
siphash: use _unaligned version by default
On ARM v6 and later, we define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS because the ordinary load/store instructions (ldr, ldrh, ldrb) can tolerate any misalignment of the memory address. However, load/store double and load/store multiple instructions (ldrd, ldm) may still only be used on memory addresses that are 32-bit aligned, and so we have to use the CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS macro with care, or we may end up with a severe performance hit due to alignment traps that require fixups by the kernel. Testing shows that this currently happens with clang-13 but not gcc-11. In theory, any compiler version can produce this bug or other problems, as we are dealing with undefined behavior in C99 even on architectures that support this in hardware, see also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100363. Fortunately, the get_unaligned() accessors do the right thing: when building for ARMv6 or later, the compiler will emit unaligned accesses using the ordinary load/store instructions (but avoid the ones that require 32-bit alignment). When building for older ARM, those accessors will emit the appropriate sequence of ldrb/mov/orr instructions. And on architectures that can truly tolerate any kind of misalignment, the get_unaligned() accessors resolve to the leXX_to_cpup accessors that operate on aligned addresses. Since the compiler will in fact emit ldrd or ldm instructions when building this code for ARM v6 or later, the solution is to use the unaligned accessors unconditionally on architectures where this is known to be fast. The _aligned version of the hash function is however still needed to get the best performance on architectures that cannot do any unaligned access in hardware. This new version avoids the undefined behavior and should produce the fastest hash on all architectures we support. Link: https://lore.kernel.org/linux-arm-kernel/[email protected]/ Link: https://lore.kernel.org/linux-crypto/CAK8P3a2KfmmGDbVHULWevB0hv71P2oi2ZCHEAqT=8dQfa0=cqQ@mail.gmail.com/ Reported-by: Ard Biesheuvel <[email protected]> Fixes: 2c956a6 ("siphash: add cryptographically secure PRF") Signed-off-by: Arnd Bergmann <[email protected]> Reviewed-by: Jason A. Donenfeld <[email protected]> Acked-by: Ard Biesheuvel <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 4e3fd72 commit f7e5b9b

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

include/linux/siphash.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ static inline bool siphash_key_is_zero(const siphash_key_t *key)
2727
}
2828

2929
u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key);
30-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
3130
u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key);
32-
#endif
3331

3432
u64 siphash_1u64(const u64 a, const siphash_key_t *key);
3533
u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
@@ -82,10 +80,9 @@ static inline u64 ___siphash_aligned(const __le64 *data, size_t len,
8280
static inline u64 siphash(const void *data, size_t len,
8381
const siphash_key_t *key)
8482
{
85-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
86-
if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT))
83+
if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
84+
!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT))
8785
return __siphash_unaligned(data, len, key);
88-
#endif
8986
return ___siphash_aligned(data, len, key);
9087
}
9188

@@ -96,10 +93,8 @@ typedef struct {
9693

9794
u32 __hsiphash_aligned(const void *data, size_t len,
9895
const hsiphash_key_t *key);
99-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
10096
u32 __hsiphash_unaligned(const void *data, size_t len,
10197
const hsiphash_key_t *key);
102-
#endif
10398

10499
u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key);
105100
u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key);
@@ -135,10 +130,9 @@ static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len,
135130
static inline u32 hsiphash(const void *data, size_t len,
136131
const hsiphash_key_t *key)
137132
{
138-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
139-
if (!IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT))
133+
if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
134+
!IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT))
140135
return __hsiphash_unaligned(data, len, key);
141-
#endif
142136
return ___hsiphash_aligned(data, len, key);
143137
}
144138

lib/siphash.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
SIPROUND; \
5050
return (v0 ^ v1) ^ (v2 ^ v3);
5151

52+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
5253
u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key)
5354
{
5455
const u8 *end = data + len - (len % sizeof(u64));
@@ -80,8 +81,8 @@ u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key)
8081
POSTAMBLE
8182
}
8283
EXPORT_SYMBOL(__siphash_aligned);
84+
#endif
8385

84-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
8586
u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key)
8687
{
8788
const u8 *end = data + len - (len % sizeof(u64));
@@ -113,7 +114,6 @@ u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key)
113114
POSTAMBLE
114115
}
115116
EXPORT_SYMBOL(__siphash_unaligned);
116-
#endif
117117

118118
/**
119119
* siphash_1u64 - compute 64-bit siphash PRF value of a u64
@@ -250,6 +250,7 @@ EXPORT_SYMBOL(siphash_3u32);
250250
HSIPROUND; \
251251
return (v0 ^ v1) ^ (v2 ^ v3);
252252

253+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
253254
u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
254255
{
255256
const u8 *end = data + len - (len % sizeof(u64));
@@ -280,8 +281,8 @@ u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
280281
HPOSTAMBLE
281282
}
282283
EXPORT_SYMBOL(__hsiphash_aligned);
284+
#endif
283285

284-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
285286
u32 __hsiphash_unaligned(const void *data, size_t len,
286287
const hsiphash_key_t *key)
287288
{
@@ -313,7 +314,6 @@ u32 __hsiphash_unaligned(const void *data, size_t len,
313314
HPOSTAMBLE
314315
}
315316
EXPORT_SYMBOL(__hsiphash_unaligned);
316-
#endif
317317

318318
/**
319319
* hsiphash_1u32 - compute 64-bit hsiphash PRF value of a u32
@@ -418,6 +418,7 @@ EXPORT_SYMBOL(hsiphash_4u32);
418418
HSIPROUND; \
419419
return v1 ^ v3;
420420

421+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
421422
u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
422423
{
423424
const u8 *end = data + len - (len % sizeof(u32));
@@ -438,8 +439,8 @@ u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
438439
HPOSTAMBLE
439440
}
440441
EXPORT_SYMBOL(__hsiphash_aligned);
442+
#endif
441443

442-
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
443444
u32 __hsiphash_unaligned(const void *data, size_t len,
444445
const hsiphash_key_t *key)
445446
{
@@ -461,7 +462,6 @@ u32 __hsiphash_unaligned(const void *data, size_t len,
461462
HPOSTAMBLE
462463
}
463464
EXPORT_SYMBOL(__hsiphash_unaligned);
464-
#endif
465465

466466
/**
467467
* hsiphash_1u32 - compute 32-bit hsiphash PRF value of a u32

0 commit comments

Comments
 (0)