Skip to content

Commit 660d206

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto - shash: reduce minimum alignment of shash_desc structure
Unlike many other structure types defined in the crypto API, the 'shash_desc' structure is permitted to live on the stack, which implies its contents may not be accessed by DMA masters. (This is due to the fact that the stack may be located in the vmalloc area, which requires a different virtual-to-physical translation than the one implemented by the DMA subsystem) Our definition of CRYPTO_MINALIGN_ATTR is based on ARCH_KMALLOC_MINALIGN, which may take DMA constraints into account on architectures that support non-cache coherent DMA such as ARM and arm64. In this case, the value is chosen to reflect the largest cacheline size in the system, in order to ensure that explicit cache maintenance as required by non-coherent DMA masters does not affect adjacent, unrelated slab allocations. On arm64, this value is currently set at 128 bytes. This means that applying CRYPTO_MINALIGN_ATTR to struct shash_desc is both unnecessary (as it is never used for DMA), and undesirable, given that it wastes stack space (on arm64, performing the alignment costs 112 bytes in the worst case, and the hole between the 'tfm' and '__ctx' members takes up another 120 bytes, resulting in an increased stack footprint of up to 232 bytes.) So instead, let's switch to the minimum SLAB alignment, which does not take DMA constraints into account. Note that this is a no-op for x86. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent e281119 commit 660d206

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

include/crypto/hash.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ struct ahash_alg {
149149

150150
struct shash_desc {
151151
struct crypto_shash *tfm;
152-
void *__ctx[] CRYPTO_MINALIGN_ATTR;
152+
void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
153153
};
154154

155155
#define HASH_MAX_DIGESTSIZE 64
@@ -162,9 +162,9 @@ struct shash_desc {
162162

163163
#define HASH_MAX_STATESIZE 512
164164

165-
#define SHASH_DESC_ON_STACK(shash, ctx) \
166-
char __##shash##_desc[sizeof(struct shash_desc) + \
167-
HASH_MAX_DESCSIZE] CRYPTO_MINALIGN_ATTR; \
165+
#define SHASH_DESC_ON_STACK(shash, ctx) \
166+
char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
167+
__aligned(__alignof__(struct shash_desc)); \
168168
struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
169169

170170
/**

include/linux/crypto.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@
151151
* The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
152152
* declaration) is used to ensure that the crypto_tfm context structure is
153153
* aligned correctly for the given architecture so that there are no alignment
154-
* faults for C data types. In particular, this is required on platforms such
155-
* as arm where pointers are 32-bit aligned but there are data types such as
156-
* u64 which require 64-bit alignment.
154+
* faults for C data types. On architectures that support non-cache coherent
155+
* DMA, such as ARM or arm64, it also takes into account the minimal alignment
156+
* that is required to ensure that the context struct member does not share any
157+
* cachelines with the rest of the struct. This is needed to ensure that cache
158+
* maintenance for non-coherent DMA (cache invalidation in particular) does not
159+
* affect data that may be accessed by the CPU concurrently.
157160
*/
158161
#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
159162

0 commit comments

Comments
 (0)