Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[CRYPTO]: Added CRYPTO_TFM_REQ_MAY_SLEEP flag

The crypto layer currently uses in_atomic() to determine whether it is
allowed to sleep. This is incorrect since spin locks don't always cause
in_atomic() to return true.

Instead of that, this patch returns to an earlier idea of a per-tfm flag
which determines whether sleeping is allowed. Unlike the earlier version,
the default is to not allow sleeping. This ensures that no existing code
can break.

As usual, this flag may either be set through crypto_alloc_tfm(), or
just before a specific crypto operation.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Herbert Xu and committed by
David S. Miller
64baf3cf fb4f10ed

+5 -6
+2 -1
crypto/api.c
··· 66 66 67 67 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags) 68 68 { 69 - tfm->crt_flags = 0; 69 + tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK; 70 + flags &= ~CRYPTO_TFM_REQ_MASK; 70 71 71 72 switch (crypto_tfm_alg_type(tfm)) { 72 73 case CRYPTO_ALG_TYPE_CIPHER:
-4
crypto/cipher.c
··· 377 377 int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags) 378 378 { 379 379 u32 mode = flags & CRYPTO_TFM_MODE_MASK; 380 - 381 380 tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB; 382 - if (flags & CRYPTO_TFM_REQ_WEAK_KEY) 383 - tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY; 384 - 385 381 return 0; 386 382 } 387 383
+2 -1
crypto/internal.h
··· 17 17 #include <linux/interrupt.h> 18 18 #include <linux/init.h> 19 19 #include <linux/kernel.h> 20 + #include <linux/slab.h> 20 21 #include <asm/kmap_types.h> 21 22 22 23 extern enum km_type crypto_km_types[]; ··· 39 38 40 39 static inline void crypto_yield(struct crypto_tfm *tfm) 41 40 { 42 - if (!in_atomic()) 41 + if (tfm->crt_flags & CRYPTO_TFM_REQ_MAY_SLEEP) 43 42 cond_resched(); 44 43 } 45 44
+1
include/linux/crypto.h
··· 45 45 #define CRYPTO_TFM_MODE_CTR 0x00000008 46 46 47 47 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 48 + #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 48 49 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 49 50 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 50 51 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000