Optimize dm-verity and fsverity using multibuffer hashing
Subsystems (8)
Eric Biggers
maintainerv4 cover Optimize dm-verity and fsverity using multibuffer hashing
On many modern CPUs, it is possible to compute the SHA-256 hash of two equal-length messages in about the same time as a single message, if all the instructions are interleaved. This is because each SHA-256 (and also most other cryptographic hash functions) is inherently serialized and therefore can't always take advantage of the CPU's full throughput.
An earlier attempt to support multibuffer hashing in Linux was based around the ahash API. That approach had some major issues. This patchset instead takes a much simpler approach of just adding a synchronous API for hashing equal-length messages.
This works well for dm-verity and fsverity, which use Merkle trees and therefore hash large numbers of equal-length messages.
This patchset is organized as follows:
- Patch 1-3 add crypto_shash_finup_mb() and tests for it.
- Patch 4-5 implement finup_mb on x86_64 and arm64, using an interleaving factor of 2.
- Patch 6-8 update fsverity and dm-verity to use crypto_shash_finup_mb() to hash pairs of data blocks when possible. Note: the patch "dm-verity: hash blocks with shash import+finup when possible" is revived from its original submission
(https://lore.kernel.org/dm-devel/[email protected]/)
because this new work provides a new motivation for it.
On CPUs that support multiple concurrent SHA-256's (all arm64 CPUs I tested, and AMD Zen CPUs), raw SHA-256 hashing throughput increases by 70-98%, and the throughput of cold-cache reads from dm-verity and fsverity increases by very roughly 35%.
Changed in v4:
- Reorganized the fsverity and dm-verity code to have a unified code path for single-block vs. multi-block processing. For data blocks they now use only crypto_shash_finup_mb().
Changed in v3:
- Change API from finup2x to finup_mb. It now takes arrays of data buffer and output buffers, avoiding hardcoding 2x in the API.
Changed in v2:
- Rebase onto cryptodev/master
- Add more comments to assembly
- Reorganize some of the assembly slightly
- Fix the claimed throughput improvement on arm64
- Fix incorrect kunmap order in fs/verity/verify.c
- Adjust testmgr generation logic slightly
- Explicitly check for INT_MAX before casting unsigned int to int
- Mention SHA3 based parallel hashes
- Mention AVX512-based approach
Eric Biggers (8):
crypto: shash - add support for finup_mb
crypto: testmgr - generate power-of-2 lengths more often
crypto: testmgr - add tests for finup_mb
crypto: x86/sha256-ni - add support for finup_mb
crypto: arm64/sha256-ce - add support for finup_mb
fsverity: improve performance by using multibuffer hashing
dm-verity: hash blocks with shash import+finup when possible
dm-verity: improve performance by using multibuffer hashing
arch/arm64/crypto/sha2-ce-core.S | 281 +++++++++++++- arch/arm64/crypto/sha2-ce-glue.c | 40 ++ arch/x86/crypto/sha256_ni_asm.S | 368 +++++++++++++++++++ arch/x86/crypto/sha256_ssse3_glue.c | 39 ++ crypto/shash.c | 60 +++ crypto/testmgr.c | 90 ++++- drivers/md/dm-verity-fec.c | 31 +- drivers/md/dm-verity-fec.h | 7 +- drivers/md/dm-verity-target.c | 545 ++++++++++++++++++++-------- drivers/md/dm-verity.h | 43 +-- fs/verity/fsverity_private.h | 7 + fs/verity/hash_algs.c | 8 +- fs/verity/verify.c | 173 +++++++-- include/crypto/hash.h | 45 ++- 14 files changed, 1494 insertions(+), 243 deletions(-)
base-commit: aabbf2135f9a9526991f17cb0c78cf1ec878f1c2
Signature · 1 line
2.45.1
Eric Biggers
maintainerPATCH v4 1/8 crypto: shash - add support for finup_mb
From: Eric Biggers <[email protected]>
Most cryptographic hash functions are serialized, in the sense that they have an internal block size and the blocks must be processed serially. (BLAKE3 is a notable exception that has tree-based hashing built-in, but all the more common choices such as the SHAs and BLAKE2 are serialized. ParallelHash and Sakura are parallel hashes based on SHA3, but SHA3 is much slower than SHA256 in software even with the ARMv8 SHA3 extension.)
This limits the performance of computing a single hash. Yet, computing multiple hashes simultaneously does not have this limitation. Modern CPUs are superscalar and often can execute independent instructions in parallel. As a result, on many modern CPUs, it is possible to hash two equal-length messages in about the same time as a single message, if all the instructions are interleaved.
Meanwhile, a very common use case for hashing in the Linux kernel is dm-verity and fs-verity. Both use a Merkle tree that has a fixed block size, usually 4096 bytes with an empty or 32-byte salt prepended. The hash algorithm is usually SHA-256. Usually, many blocks need to be hashed at a time. This is an ideal scenario for multibuffer hashing.
Linux actually used to support SHA-256 multibuffer hashing on x86_64, before it was removed by commit ab8085c130ed ("crypto: x86 - remove SHA multibuffer routines and mcryptd"). However, it was integrated with the crypto API in a weird way, where it behaved as an asynchronous hash that queued up and executed all requests on a global queue. This made it very complex, buggy, and virtually unusable.
This patch takes a new approach of just adding an API crypto_shash_finup_mb() that synchronously computes the hash of multiple equal-length messages, starting from a common state that represents the (possibly empty) common prefix shared by the messages.
The new API is part of the "shash" algorithm type, as it does not make sense in "ahash". It does a "finup" operation rather than a "digest" operation in order to support the salt that is used by dm-verity and fs-verity. The data and output buffers are provided in arrays of length @num_msgs in order to make the API itself extensible to interleaving factors other than 2. (Though, initially only 2x will actually be used. There are some platforms in which a higher factor could help, but there are significant trade-offs.)
--- crypto/shash.c | 60 +++++++++++++++++++++++++++++++++++++++++++ include/crypto/hash.h | 45 +++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 1 deletion(-)
crypto/shash.c +60 -0
@@ -73,10 +73,57 @@ int crypto_shash_finup(struct shash_desc *desc, const u8 *data, {return crypto_shash_alg (desc->tfm)->finup (desc, data, len, out); }EXPORT_SYMBOL_GPL (crypto_shash_finup); +static noinline_for_stack int +shash_finup_mb_fallback (struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs) +{ +struct crypto_shash *tfm = desc->tfm; +SHASH_DESC_ON_STACK (desc2, tfm); +unsigned int i; +int err; + +for (i =0 ; i < num_msgs -1 ; i++) { + desc2->tfm = tfm; +memcpy (shash_desc_ctx (desc2),shash_desc_ctx (desc), +crypto_shash_descsize (tfm)); + err =crypto_shash_finup (desc2, data[i], len, outs[i]); +if (err) +return err; + } +return crypto_shash_finup (desc, data[i], len, outs[i]); +} + +int crypto_shash_finup_mb (struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs) +{ +struct shash_alg *alg =crypto_shash_alg (desc->tfm); +int err; + +if (num_msgs ==1 ) +return crypto_shash_finup (desc, data[0 ], len, outs[0 ]); + +if (num_msgs ==0 ) +return 0 ; + +if (WARN_ON_ONCE (num_msgs > alg->mb_max_msgs)) + goto fallback; + + err = alg->finup_mb (desc, data, len, outs, num_msgs); +if (unlikely (err == -EOPNOTSUPP )) + goto fallback; +return err; + +fallback: +return shash_finup_mb_fallback (desc, data, len, outs, num_msgs); +} +EXPORT_SYMBOL_GPL (crypto_shash_finup_mb); +static int shash_default_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {struct shash_alg *shash =crypto_shash_alg (desc->tfm);
@@ -312,10 +359,20 @@ static int shash_prepare_alg(struct shash_alg *alg)return -EINVAL ;if ((alg->export && !alg->import) || (alg->import && !alg->export))return -EINVAL ; +if (alg->mb_max_msgs) { +if (alg->mb_max_msgs >HASH_MAX_MB_MSGS ) +return -EINVAL ; +if (!alg->finup_mb) +return -EINVAL ; + }else { +if (alg->finup_mb) +return -EINVAL ; + } + err =hash_prepare_alg (&alg->halg);if (err)return err; base->cra_type = &crypto_shash_type;
@@ -339,10 +396,13 @@ static int shash_prepare_alg(struct shash_alg *alg)if (!alg->export) alg->halg.statesize = alg->descsize;if (!alg->setkey) alg->setkey = shash_no_setkey; +if (!alg->mb_max_msgs) + alg->mb_max_msgs =1 ; +return 0 ; }int crypto_register_shash (struct shash_alg *alg) {
include/crypto/hash.h +44 -1
110 lines not on this page · raw message
Signature · 1 line
2.45.1
Re: [PATCH v4 1/8] crypto: shash - add support for finup_mb
Quoted 10 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Most cryptographic hash functions are serialized, in the sense that they have an internal block size and the blocks must be processed serially. (BLAKE3 is a notable exception that has tree-based hashing built-in, but all the more common choices such as the SHAs and BLAKE2 are serialized. ParallelHash and Sakura are parallel hashes based on SHA3, but SHA3 is much slower than SHA256 in software even with the ARMv8 SHA3 extension.)
Quoted 7 lines
This limits the performance of computing a single hash. Yet, computing multiple hashes simultaneously does not have this limitation. Modern CPUs are superscalar and often can execute independent instructions in parallel. As a result, on many modern CPUs, it is possible to hash two equal-length messages in about the same time as a single message, if all the instructions are interleaved.
It's not only about out-of-order/superscalar execution. In some cases (at least on ARM), it takes more than a cycle for the result of an instruction to become available to the next one, even if the computation itself completes in a single cycle, and this affects in-order cores as well.
The crux here is that the associated benefit only exists if the independent inputs can be interleaved at the instruction level. OoO cores will have some tolerance for deviations from this, but in the general case, this kind of multi-stream processing requires meticulous parallellization.
That also means that it is substantially different from the asynchronous accelerator use case where a single IP block may have different queues that can be used in parallel. For these, it might make sense to provide some infrastructure to mix inputs from disparate sources, but the same logic is unlikely to be useful for the CPU based parallel hashing case.
...
Quoted 6 lines
This patch takes a new approach of just adding an API
crypto_shash_finup_mb()that synchronously computes the hash of multiple equal-length messages, starting from a common state that represents the (possibly empty) common prefix shared by the messages.
This is an independent optimization, right? This could be useful even more sequential hashing, and is not a fundamental aspect of parallel hashing?
Quoted 9 lines
The new API is part of the "shash" algorithm type, as it does not make sense in "ahash". It does a "finup" operation rather than a "digest" operation in order to support the salt that is used by dm-verity and fs-verity. The data and output buffers are provided in arrays of length @num_msgs in order to make the API itself extensible to interleaving factors other than 2. (Though, initially only 2x will actually be used. There are some platforms in which a higher factor could help, but there are significant trade-offs.)
I could imagine cases where 3-way would have an advantage over 2-way - it is highly uarch dependent, though, so I wouldn't spend too much time accommodating this before a use case actually materializes.
Quoted 227 lines
Signed-off-by Eric Biggers <[email protected]>--- crypto/shash.c | 60 +++++++++++++++++++++++++++++++++++++++++++ include/crypto/hash.h | 45 +++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 1 deletion(-)crypto/shash.c +60
-0@@ -73,10 +73,57 @@ int crypto_shash_finup(struct shash_desc *desc, const u8 *data, {return crypto_shash_alg (desc->tfm)->finup (desc, data, len, out); }EXPORT_SYMBOL_GPL (crypto_shash_finup); +static noinline_for_stack int +shash_finup_mb_fallback (struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs) +{ +struct crypto_shash *tfm = desc->tfm; +SHASH_DESC_ON_STACK (desc2, tfm); +unsigned int i; +int err; + +for (i =0 ; i < num_msgs -1 ; i++) { + desc2->tfm = tfm; +memcpy (shash_desc_ctx (desc2),shash_desc_ctx (desc), +crypto_shash_descsize (tfm)); + err =crypto_shash_finup (desc2, data[i], len, outs[i]); +if (err) +return err; + } +return crypto_shash_finup (desc, data[i], len, outs[i]); +} + +int crypto_shash_finup_mb (struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs) +{ +struct shash_alg *alg =crypto_shash_alg (desc->tfm); +int err; + +if (num_msgs ==1 ) +return crypto_shash_finup (desc, data[0 ], len, outs[0 ]); + +if (num_msgs ==0 ) +return 0 ; + +if (WARN_ON_ONCE (num_msgs > alg->mb_max_msgs)) + goto fallback; + + err = alg->finup_mb (desc, data, len, outs, num_msgs); +if (unlikely (err == -EOPNOTSUPP )) + goto fallback; +return err; + +fallback: +return shash_finup_mb_fallback (desc, data, len, outs, num_msgs); +} +EXPORT_SYMBOL_GPL (crypto_shash_finup_mb); +static int shash_default_digest (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) {struct shash_alg *shash =crypto_shash_alg (desc->tfm);@@ -312,10 +359,20 @@ static int shash_prepare_alg(struct shash_alg *alg)return -EINVAL ;if ((alg->export && !alg->import) || (alg->import && !alg->export))return -EINVAL ; +if (alg->mb_max_msgs) { +if (alg->mb_max_msgs >HASH_MAX_MB_MSGS ) +return -EINVAL ; +if (!alg->finup_mb) +return -EINVAL ; + }else { +if (alg->finup_mb) +return -EINVAL ; + } + err =hash_prepare_alg (&alg->halg);if (err)return err; base->cra_type = &crypto_shash_type;@@ -339,10 +396,13 @@ static int shash_prepare_alg(struct shash_alg *alg)if (!alg->export) alg->halg.statesize = alg->descsize;if (!alg->setkey) alg->setkey = shash_no_setkey; +if (!alg->mb_max_msgs) + alg->mb_max_msgs =1 ; +return 0 ; }int crypto_register_shash (struct shash_alg *alg) {include/crypto/hash.h +44
-2@@ -154,11 +154,13 @@ struct ahash_alg {struct shash_desc {struct crypto_shash *tfm;void *__ctx[]__aligned(ARCH_SLAB_MINALIGN) ; }; -#define HASH_MAX_DIGESTSIZE 64 +#define HASH_MAX_DIGESTSIZE 64 + +#define HASH_MAX_MB_MSGS 2/* max value of crypto_shash_mb_max_msgs() */ /* * Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' * containing a 'struct sha3_state'. */ @@ -177,10 +179,19 @@ struct shash_desc {* @finup: see struct ahash_alg * @digest: see struct ahash_alg * @export: see struct ahash_alg * @import: see struct ahash_alg * @setkey: see struct ahash_alg +* @finup_mb: **[optional]** Multibuffer hashing support. Finish calculating +* the digests of multiple messages, interleaving the instructions to +* potentially achieve better performance than hashing each message +* individually. The num_msgs argument will be between 2 and +* @mb_max_msgs inclusively. If there are particular values of len +* or num_msgs, or a particular calling context (e.g. no-SIMD) that +* the implementation does not support with this method, the +* implementation may return -EOPNOTSUPP from this method in those +* cases to cause the crypto API to fall back to repeated finups. * @init_tfm: Initialize the cryptographic transformation object. * This function is called only once at the instantiation * time, right after the transformation context was * allocated. In case the cryptographic hardware has * some special requirements which need to be handled @@ -192,10 +203,11 @@ struct shash_desc {* various changes set in @init_tfm. * @clone_tfm: Copy transform into new object, may allocate memory. * @descsize: Size of the operational state for the message digest. This state * size is the memory size that needs to be allocated for * shash_desc.__ctx +* @mb_max_msgs: Maximum supported value of num_msgs argument to @finup_mb * @halg: see struct hash_alg_common * @HASH_ALG_COMMON: see struct hash_alg_common */ struct shash_alg {int (*init)(struct shash_desc *desc);@@ -208,15 +220,19 @@ struct shash_alg { unsigned int len, u8 *out);int (*export)(struct shash_desc *desc,void *out);int (*import)(struct shash_desc *desc,const void *in);int (*setkey)(struct crypto_shash *tfm,const u8 *key,unsigned int keylen); +int (*finup_mb)(struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs);int (*init_tfm)(struct crypto_shash *tfm);void (*exit_tfm)(struct crypto_shash *tfm);int (*clone_tfm)(struct crypto_shash *dst,struct crypto_shash *src);unsigned int descsize; +unsigned int mb_max_msgs;union {struct HASH_ALG_COMMON ;struct hash_alg_common halg; };@@ -750,10 +766,20 @@ static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)static inline unsigned int crypto_shash_statesize (struct crypto_shash *tfm) {return crypto_shash_alg (tfm)->statesize; } +/* +* Return the maximum supported multibuffer hashing interleaving factor, i.e. +* the maximum num_msgs that can be passed to crypto_shash_finup_mb(). The +* return value will be between 1 and HASH_MAX_MB_MSGS inclusively. +*/ +static inline unsigned int crypto_shash_mb_max_msgs (struct crypto_shash *tfm) +{ +return crypto_shash_alg (tfm)->mb_max_msgs; +} +static inline u32 crypto_shash_get_flags (struct crypto_shash *tfm) {return crypto_tfm_get_flags (crypto_shash_tfm (tfm)); }@@ -843,10 +869,27 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,* Return: 0 on success; < 0 if an error occurred. */ int crypto_shash_tfm_digest (struct crypto_shash *tfm,const u8 *data,unsigned int len,u8 *out); +/** +* crypto_shash_finup_mb() - multibuffer message hashing +* @desc: the starting state that is forked for each message. It contains the +* state after hashing a (possibly-empty) common prefix of the messages. +* @data: the data of each message (not including any common prefix from @desc) +* @len: length of each data buffer in bytes +* @outs: output buffer for each message digest +* @num_msgs: number of messages, i.e. the number of entries in @data and @outs. +* This can't be more than crypto_shash_mb_max_msgs(). +* +* Context: Any context. +* Return: 0 on success; a negative errno value on failure. +*/ +int crypto_shash_finup_mb (struct shash_desc *desc,const u8 *const data[], +unsigned int len,u8 *const outs[], +unsigned int num_msgs); +/** * crypto_shash_export() - extract operational state for message digest * @desc: reference to the operational state handle whose state is exported * @out: output buffer of sufficient size that can hold the hash state * -- 2.45.1
Eric Biggers
maintainerRe: [PATCH v4 1/8] crypto: shash - add support for finup_mb
Quoted 10 lines · Ard Biesheuvel
Quoted 6 lines
This patch takes a new approach of just adding an API
crypto_shash_finup_mb()that synchronously computes the hash of multiple equal-length messages, starting from a common state that represents the (possibly empty) common prefix shared by the messages.This is an independent optimization, right? This could be useful even more sequential hashing, and is not a fundamental aspect of parallel hashing?
If you're referring to the part about using a common starting state, that's not an independent optimization. Only multibuffer hashing processes multiple messages in one call and therefore has an opportunity to share a starting shash_desc for finup. This isn't just an optimization but it also makes the multibuffer hashing API and its implementation much simpler.
With single-buffer there has to be one shash_desc per message as usual.
If you're asking if crypto_shash_finup_mb() can be used even without multibuffer hashing support, the answer is yes. This patchset makes crypto_shash_finup_mb() fall back to crypto_shash_finup() as needed, and this is used by fsverity and dm-verity to have one code path that uses crypto_shash_finup_mb() instead of separate code paths that use crypto_shash_finup_mb() and crypto_shash_finup(). This just makes things a bit simpler and isn't an optimization; note that the fallback has to copy the shash_desc for each message beyond the first.
- Eric
Eric Biggers
maintainerPATCH v4 2/8 crypto: testmgr - generate power-of-2 lengths more often
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Implementations of hash functions often have special cases when lengths are a multiple of the hash function's internal block size (e.g. 64 for SHA-256, 128 for SHA-512). Currently, when the fuzz testing code generates lengths, it doesn't prefer any length mod 64 over any other. This limits the coverage of these special cases.
Therefore, this patch updates the fuzz testing code to generate power-of-2 lengths and divide messages exactly in half a bit more often.
--- crypto/testmgr.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
crypto/testmgr.c +12 -4
@@ -901,18 +901,24 @@ static unsigned int generate_random_length(struct rnd_state *rng, {unsigned int len =prandom_u32_below (rng, max_len +1 );switch (prandom_u32_below (rng,4 )) {case 0 : -return len %64 ; + len %=64 ; +break ;case 1 : -return len %256 ; + len %=256 ; +break ;case 2 : -return len %1024 ; + len %=1024 ; +break ;default : -return len; +break ; } +if (prandom_u32_below (rng,4 ) ==0 ) + len =rounddown_pow_of_two (len); +return len; }/* Flip a random bit in the given nonempty data buffer */ static void flip_random_bit (struct rnd_state *rng,u8 *buf,size_t size) {
@@ -1004,10 +1010,12 @@ static char *generate_random_sgl_divisions(struct rnd_state *rng,unsigned int this_len;const char *flushtype_str;if (div == &divs[max_divs -1 ] ||prandom_bool (rng)) this_len = remaining; +else if (prandom_u32_below (rng,4 ) ==0 ) + this_len = (remaining +1 ) /2 ;else this_len =prandom_u32_inclusive (rng,1 , remaining); div->proportion_of_total = this_len;if (prandom_u32_below (rng,4 ) ==0 )
Signature · 1 line
2.45.1
Eric Biggers
maintainerPATCH v4 3/8 crypto: testmgr - add tests for finup_mb
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Update the shash self-tests to test the new finup_mb method when CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y.
--- crypto/testmgr.c | 74 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 7 deletions(-)
crypto/testmgr.c +67 -7
@@ -227,10 +227,11 @@ enum flush_type {/* finalization function for hash algorithms */ enum finalization_type {FINALIZATION_TYPE_FINAL ,/* use final() */ FINALIZATION_TYPE_FINUP ,/* use finup() */ +FINALIZATION_TYPE_FINUP_MB ,/* use finup_mb() */ FINALIZATION_TYPE_DIGEST ,/* use digest() */ };/* * Whether the crypto operation will occur in-place, and if so whether the
@@ -290,10 +291,15 @@ struct test_sg_division {* the @iv_offset * @key_offset: misalignment of the key, where 0 is default alignment * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to * the @key_offset * @finalization_type: what finalization function to use for hashes +* @multibuffer_index: random number used to generate the message index to use +* for finup_mb (if finup_mb is used). +* @multibuffer_count: random number used to generate the num_msgs parameter to +* finup_mb (if finup_mb is used). +* * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP. */ struct testvec_config {const char *name;enum inplace_mode inplace_mode;
@@ -303,10 +309,12 @@ struct testvec_config {unsigned int iv_offset;unsigned int key_offset;bool iv_offset_relative_to_alignmask;bool key_offset_relative_to_alignmask;enum finalization_type finalization_type; +unsigned int multibuffer_index; +unsigned int multibuffer_count;bool nosimd; };#define TESTVEC_CONFIG_NAMELEN 192
@@ -1109,19 +1117,27 @@ static void generate_random_testvec_config(struct rnd_state *rng,if (prandom_bool (rng)) { cfg->req_flags |=CRYPTO_TFM_REQ_MAY_SLEEP ; p +=scnprintf (p, end - p," may_sleep" ); } -switch (prandom_u32_below (rng,4 )) { +switch (prandom_u32_below (rng,8 )) {case 0 : +case 1 : cfg->finalization_type =FINALIZATION_TYPE_FINAL ; p +=scnprintf (p, end - p," use_final" );break ; -case 1 : +case 2 : cfg->finalization_type =FINALIZATION_TYPE_FINUP ; p +=scnprintf (p, end - p," use_finup" );break ; +case 3 : +case 4 : + cfg->finalization_type =FINALIZATION_TYPE_FINUP_MB ; + cfg->multibuffer_index =prandom_u32_state (rng); + cfg->multibuffer_count =prandom_u32_state (rng); + p +=scnprintf (p, end - p," use_finup_mb" ); +break ;default : cfg->finalization_type =FINALIZATION_TYPE_DIGEST ; p +=scnprintf (p, end - p," use_digest" );break ; }
@@ -1270,10 +1286,37 @@ static inline int check_shash_op(const char *op, int err,pr_err ("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n" , driver, op, err, vec_name, cfg->name);return err; } +static int do_finup_mb (struct shash_desc *desc, +const u8 *data,unsigned int len,u8 *result, +const struct testvec_config *cfg, +const struct test_sglist *tsgl) +{ +struct crypto_shash *tfm = desc->tfm; +const u8 *unused_data = tsgl->bufs[XBUFSIZE -1 ]; +u8 unused_result[HASH_MAX_DIGESTSIZE ]; +const u8 *datas[HASH_MAX_MB_MSGS ]; +u8 *outs[HASH_MAX_MB_MSGS ]; +unsigned int num_msgs; +unsigned int msg_idx; +unsigned int i; + + num_msgs =1 + (cfg->multibuffer_count %crypto_shash_mb_max_msgs (tfm)); +if (WARN_ON_ONCE (num_msgs >HASH_MAX_MB_MSGS )) +return -EINVAL ; + msg_idx = cfg->multibuffer_index % num_msgs; +for (i =0 ; i < num_msgs; i++) { + datas[i] = unused_data; + outs[i] = unused_result; + } + datas[msg_idx] = data; + outs[msg_idx] = result; +return crypto_shash_finup_mb (desc, datas, len, outs, num_msgs); +} +/* Test one hash test vector in one configuration, using the shash API */ static int test_shash_vec_cfg(const struct hash_testvec *vec, const char *vec_name, const struct testvec_config *cfg, struct shash_desc *desc,
@@ -1346,11 +1389,14 @@ static int test_shash_vec_cfg(const struct hash_testvec *vec,return -EINVAL ; } goto result_ready; } -/* Using init(), zero or more update(), then final() or finup() */ +/* +* Using init(), zero or more update(), then either final(), finup(), or +* finup_mb(). +*/ if (cfg->nosimd)crypto_disable_simd_for_test (); err =crypto_shash_init (desc);if (cfg->nosimd)
@@ -1358,28 +1404,42 @@ static int test_shash_vec_cfg(const struct hash_testvec *vec, err =check_shash_op ("init" , err, driver, vec_name, cfg);if (err)return err;for (i =0 ; i < tsgl->nents; i++) { +const u8 *data =sg_virt (&tsgl->sgl[i]); +unsigned int len = tsgl->sgl[i].length; +if (i +1 == tsgl->nents && cfg->finalization_type ==FINALIZATION_TYPE_FINUP ) {if (divs[i]->nosimd)crypto_disable_simd_for_test (); - err =crypto_shash_finup (desc,sg_virt (&tsgl->sgl[i]), - tsgl->sgl[i].length, result); + err =crypto_shash_finup (desc, data, len, result);if (divs[i]->nosimd)crypto_reenable_simd_for_test (); err =check_shash_op ("finup" , err, driver, vec_name, cfg);if (err)return err; goto result_ready; } +if (i +1 == tsgl->nents && + cfg->finalization_type ==FINALIZATION_TYPE_FINUP_MB ) { +if (divs[i]->nosimd) +crypto_disable_simd_for_test (); + err =do_finup_mb (desc, data, len, result, cfg, tsgl); +if (divs[i]->nosimd) +crypto_reenable_simd_for_test (); + err =check_shash_op ("finup_mb" , err, driver, vec_name, + cfg); +if (err) +return err; + goto result_ready; + }if (divs[i]->nosimd)crypto_disable_simd_for_test (); - err =crypto_shash_update (desc,sg_virt (&tsgl->sgl[i]), - tsgl->sgl[i].length); + err =crypto_shash_update (desc, data, len);if (divs[i]->nosimd)crypto_reenable_simd_for_test (); err =check_shash_op ("update" , err, driver, vec_name, cfg);if (err)return err;
Signature · 1 line
2.45.1
Eric Biggers
maintainerPATCH v4 4/8 crypto: x86/sha256-ni - add support for finup_mb
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add an implementation of finup_mb to sha256-ni, using an interleaving factor of 2. It interleaves a finup operation for two equal-length messages that share a common prefix. dm-verity and fs-verity will take advantage of this for greatly improved performance on capable CPUs.
This increases the throughput of SHA-256 hashing 4096-byte messages by the following amounts on the following CPUs:
AMD Zen 1: 84%
AMD Zen 4: 98%
Intel Ice Lake: 4%
Intel Sapphire Rapids: 20%
For now, this seems to benefit AMD much more than Intel. This seems to be because current AMD CPUs support concurrent execution of the SHA-NI instructions, but unfortunately current Intel CPUs don't, except for the sha256msg2 instruction. Hopefully future Intel CPUs will support SHA-NI on more execution ports. Zen 1 supports 2 concurrent sha256rnds2, and Zen 4 supports 4 concurrent sha256rnds2, which suggests that even better performance may be achievable on Zen 4 by interleaving more than two hashes; however, doing so poses a number of trade-offs.
It's been reported that the method that achieves the highest SHA-256 throughput on Intel CPUs is actually computing 16 hashes simultaneously using AVX512. That method would be quite different to the SHA-NI method used in this patch. However, such a high interleaving factor isn't practical for the use cases being targeted in the kernel.
--- arch/x86/crypto/sha256_ni_asm.S | 368 ++++++++++++++++++++++++++++ arch/x86/crypto/sha256_ssse3_glue.c | 39 +++ 2 files changed, 407 insertions(+)
arch/x86/crypto/sha256_ni_asm.S +368 -0
@@ -172,10 +172,378 @@ SYM_TYPED_FUNC_START(sha256_ni_transform) .Ldone_hash: RET SYM_FUNC_END(sha256_ni_transform) +#undef DIGEST_PTR +#undef DATA_PTR +#undef NUM_BLKS +#undef SHA256CONSTANTS +#undef MSG +#undef STATE0 +#undef STATE1 +#undef MSG0 +#undef MSG1 +#undef MSG2 +#undef MSG3 +#undef TMP +#undef SHUF_MASK +#undef ABEF_SAVE +#undef CDGH_SAVE + +// parameters for __sha256_ni_finup2x() +#define SCTX %rdi +#define DATA1 %rsi +#define DATA2 %rdx +#define LEN %ecx +#define LEN8 %cl +#define LEN64 %rcx +#define OUT1 %r8 +#define OUT2 %r9 + +// other scalar variables +#define SHA256CONSTANTS %rax +#define COUNT %r10 +#define COUNT32 %r10d +#define FINAL_STEP %r11d + +// rbx is used as a temporary. + +#define MSG %xmm0 // sha256rnds2 implicit operand +#define STATE0_A %xmm1 +#define STATE1_A %xmm2 +#define STATE0_B %xmm3 +#define STATE1_B %xmm4 +#define TMP_A %xmm5 +#define TMP_B %xmm6 +#define MSG0_A %xmm7 +#define MSG1_A %xmm8 +#define MSG2_A %xmm9 +#define MSG3_A %xmm10 +#define MSG0_B %xmm11 +#define MSG1_B %xmm12 +#define MSG2_B %xmm13 +#define MSG3_B %xmm14 +#define SHUF_MASK %xmm15 + +#define OFFSETOF_STATE 0 // offsetof(struct sha256_state, state) +#define OFFSETOF_COUNT 32 // offsetof(struct sha256_state, count) +#define OFFSETOF_BUF 40 // offsetof(struct sha256_state, buf) + +// Do 4 rounds of SHA-256 for each of two messages (interleaved). m0_a and m0_b +// contain the current 4 message schedule words for the first and second message +// respectively. +// +// If not all the message schedule words have been computed yet, then this also +// computes 4 more message schedule words for each message. m1_a-m3_a contain +// the next 3 groups of 4 message schedule words for the first message, and +// likewise m1_b-m3_b for the second. After consuming the current value of +// m0_a, this macro computes the group after m3_a and writes it to m0_a, and +// likewise for *_b. This means that the next (m0_a, m1_a, m2_a, m3_a) is the +// current (m1_a, m2_a, m3_a, m0_a), and likewise for *_b, so the caller must +// cycle through the registers accordingly. +.macro do_4rounds_2x i, m0_a, m1_a, m2_a, m3_a, m0_b, m1_b, m2_b, m3_b + movdqa (\i-32)*4(SHA256CONSTANTS), TMP_A + movdqa TMP_A, TMP_B + paddd \m0_a, TMP_A + paddd \m0_b, TMP_B +.if \i < 48 + sha256msg1 \m1_a, \m0_a + sha256msg1 \m1_b, \m0_b +.endif + movdqa TMP_A, MSG + sha256rnds2 STATE0_A, STATE1_A + movdqa TMP_B, MSG + sha256rnds2 STATE0_B, STATE1_B + pshufd $0x0E, TMP_A, MSG + sha256rnds2 STATE1_A, STATE0_A + pshufd $0x0E, TMP_B, MSG + sha256rnds2 STATE1_B, STATE0_B +.if \i < 48 + movdqa \m3_a, TMP_A + movdqa \m3_b, TMP_B + palignr $4, \m2_a, TMP_A + palignr $4, \m2_b, TMP_B + paddd TMP_A, \m0_a + paddd TMP_B, \m0_b + sha256msg2 \m3_a, \m0_a + sha256msg2 \m3_b, \m0_b +.endif +.endm + +// +// void __sha256_ni_finup2x(const struct sha256_state *sctx, +// const u8 *data1, const u8 *data2, int len, +// u8 out1[SHA256_DIGEST_SIZE], +// u8 out2[SHA256_DIGEST_SIZE]); +// +// This function computes the SHA-256 digests of two messages |data1| and +// |data2| that are both |len| bytes long, starting from the initial state +// |sctx|. |len| must be at least SHA256_BLOCK_SIZE. +// +// The instructions for the two SHA-256 operations are interleaved. On many +// CPUs, this is almost twice as fast as hashing each message individually due +// to taking better advantage of the CPU's SHA-256 and SIMD throughput. +// +SYM_FUNC_START(__sha256_ni_finup2x) + // Allocate 128 bytes of stack space, 16-byte aligned. + push %rbx + push %rbp + mov %rsp, %rbp + sub $128, %rsp + and $~15, %rsp + + // Load the shuffle mask for swapping the endianness of 32-bit words. + movdqa PSHUFFLE_BYTE_FLIP_MASK(%rip), SHUF_MASK + + // Set up pointer to the round constants. + lea K256+32*4(%rip), SHA256CONSTANTS + + // Initially we're not processing the final blocks. + xor FINAL_STEP, FINAL_STEP + + // Load the initial state from sctx->state. + movdqu OFFSETOF_STATE+0*16(SCTX), STATE0_A // DCBA + movdqu OFFSETOF_STATE+1*16(SCTX), STATE1_A // HGFE + movdqa STATE0_A, TMP_A + punpcklqdq STATE1_A, STATE0_A // FEBA + punpckhqdq TMP_A, STATE1_A // DCHG + pshufd $0x1B, STATE0_A, STATE0_A // ABEF + pshufd $0xB1, STATE1_A, STATE1_A // CDGH + + // Load sctx->count. Take the mod 64 of it to get the number of bytes + // that are buffered in sctx->buf. Also save it in a register with LEN + // added to it. + mov LEN, LEN + mov OFFSETOF_COUNT(SCTX), %rbx + lea (%rbx, LEN64, 1), COUNT + and $63, %ebx + jz .Lfinup2x_enter_loop // No bytes buffered? + + // %ebx bytes (1 to 63) are currently buffered in sctx->buf. Load them + // followed by the first 64 - %ebx bytes of data. Since LEN >= 64, we + // just load 64 bytes from each of sctx->buf, DATA1, and DATA2 + // unconditionally and rearrange the data as needed. + + movdqu OFFSETOF_BUF+0*16(SCTX), MSG0_A + movdqu OFFSETOF_BUF+1*16(SCTX), MSG1_A + movdqu OFFSETOF_BUF+2*16(SCTX), MSG2_A + movdqu OFFSETOF_BUF+3*16(SCTX), MSG3_A + movdqa MSG0_A, 0*16(%rsp) + movdqa MSG1_A, 1*16(%rsp) + movdqa MSG2_A, 2*16(%rsp) + movdqa MSG3_A, 3*16(%rsp) + + movdqu 0*16(DATA1), MSG0_A + movdqu 1*16(DATA1), MSG1_A + movdqu 2*16(DATA1), MSG2_A + movdqu 3*16(DATA1), MSG3_A + movdqu MSG0_A, 0*16(%rsp,%rbx) + movdqu MSG1_A, 1*16(%rsp,%rbx) + movdqu MSG2_A, 2*16(%rsp,%rbx) + movdqu MSG3_A, 3*16(%rsp,%rbx) + movdqa 0*16(%rsp), MSG0_A + movdqa 1*16(%rsp), MSG1_A + movdqa 2*16(%rsp), MSG2_A + movdqa 3*16(%rsp), MSG3_A + + movdqu 0*16(DATA2), MSG0_B + movdqu 1*16(DATA2), MSG1_B + movdqu 2*16(DATA2), MSG2_B + movdqu 3*16(DATA2), MSG3_B + movdqu MSG0_B, 0*16(%rsp,%rbx) + movdqu MSG1_B, 1*16(%rsp,%rbx) + movdqu MSG2_B, 2*16(%rsp,%rbx) + movdqu MSG3_B, 3*16(%rsp,%rbx) + movdqa 0*16(%rsp), MSG0_B + movdqa 1*16(%rsp), MSG1_B + movdqa 2*16(%rsp), MSG2_B + movdqa 3*16(%rsp), MSG3_B + + sub $64, %rbx // rbx = buffered - 64 + sub %rbx, DATA1 // DATA1 += 64 - buffered + sub %rbx, DATA2 // DATA2 += 64 - buffered + add %ebx, LEN // LEN += buffered - 64 + movdqa STATE0_A, STATE0_B + movdqa STATE1_A, STATE1_B + jmp .Lfinup2x_loop_have_data + +.Lfinup2x_enter_loop: + sub $64, LEN + movdqa STATE0_A, STATE0_B + movdqa STATE1_A, STATE1_B +.Lfinup2x_loop: + // Load the next two data blocks. + movdqu 0*16(DATA1), MSG0_A + movdqu 0*16(DATA2), MSG0_B + movdqu 1*16(DATA1), MSG1_A + movdqu 1*16(DATA2), MSG1_B + movdqu 2*16(DATA1), MSG2_A + movdqu 2*16(DATA2), MSG2_B + movdqu 3*16(DATA1), MSG3_A + movdqu 3*16(DATA2), MSG3_B + add $64, DATA1 + add $64, DATA2 +.Lfinup2x_loop_have_data: + // Convert the words of the data blocks from big endian. + pshufb SHUF_MASK, MSG0_A + pshufb SHUF_MASK, MSG0_B + pshufb SHUF_MASK, MSG1_A + pshufb SHUF_MASK, MSG1_B + pshufb SHUF_MASK, MSG2_A + pshufb SHUF_MASK, MSG2_B + pshufb SHUF_MASK, MSG3_A + pshufb SHUF_MASK, MSG3_B +.Lfinup2x_loop_have_bswapped_data: + + // Save the original state for each block. + movdqa STATE0_A, 0*16(%rsp) + movdqa STATE0_B, 1*16(%rsp) + movdqa STATE1_A, 2*16(%rsp) + movdqa STATE1_B, 3*16(%rsp) + + // Do the SHA-256 rounds on each block. +.irp i, 0, 16, 32, 48 + do_4rounds_2x (\i + 0), MSG0_A, MSG1_A, MSG2_A, MSG3_A, \ + MSG0_B, MSG1_B, MSG2_B, MSG3_B + do_4rounds_2x (\i + 4), MSG1_A, MSG2_A, MSG3_A, MSG0_A, \ + MSG1_B, MSG2_B, MSG3_B, MSG0_B + do_4rounds_2x (\i + 8), MSG2_A, MSG3_A, MSG0_A, MSG1_A, \ + MSG2_B, MSG3_B, MSG0_B, MSG1_B + do_4rounds_2x (\i + 12), MSG3_A, MSG0_A, MSG1_A, MSG2_A, \ + MSG3_B, MSG0_B, MSG1_B, MSG2_B +.endr + + // Add the original state for each block. + paddd 0*16(%rsp), STATE0_A + paddd 1*16(%rsp), STATE0_B + paddd 2*16(%rsp), STATE1_A + paddd 3*16(%rsp), STATE1_B + + // Update LEN and loop back if more blocks remain. + sub $64, LEN + jge .Lfinup2x_loop + + // Check if any final blocks need to be handled. + // FINAL_STEP = 2: all done + // FINAL_STEP = 1: need to do count-only padding block + // FINAL_STEP = 0: need to do the block with 0x80 padding byte + cmp $1, FINAL_STEP + jg .Lfinup2x_done + je .Lfinup2x_finalize_countonly + add $64, LEN + jz .Lfinup2x_finalize_blockaligned + + // Not block-aligned; 1 <= LEN <= 63 data bytes remain. Pad the block. + // To do this, write the padding starting with the 0x80 byte to + // &sp[64]. Then for each message, copy the last 64 data bytes to sp + // and load from &sp[64 - LEN] to get the needed padding block. This + // code relies on the data buffers being >= 64 bytes in length. + mov $64, %ebx + sub LEN, %ebx // ebx = 64 - LEN + sub %rbx, DATA1 // DATA1 -= 64 - LEN + sub %rbx, DATA2 // DATA2 -= 64 - LEN + mov $0x80, FINAL_STEP // using FINAL_STEP as a temporary + movd FINAL_STEP, MSG0_A + pxor MSG1_A, MSG1_A + movdqa MSG0_A, 4*16(%rsp) + movdqa MSG1_A, 5*16(%rsp) + movdqa MSG1_A, 6*16(%rsp) + movdqa MSG1_A, 7*16(%rsp) + cmp $56, LEN + jge 1f // will COUNT spill into its own block? + shl $3, COUNT + bswap COUNT + mov COUNT, 56(%rsp,%rbx) + mov $2, FINAL_STEP // won't need count-only block + jmp 2f +1: + mov $1, FINAL_STEP // will need count-only block +2: + movdqu 0*16(DATA1), MSG0_A + movdqu 1*16(DATA1), MSG1_A + movdqu 2*16(DATA1), MSG2_A + movdqu 3*16(DATA1), MSG3_A + movdqa MSG0_A, 0*16(%rsp) + movdqa MSG1_A, 1*16(%rsp) + movdqa MSG2_A, 2*16(%rsp) + movdqa MSG3_A, 3*16(%rsp) + movdqu 0*16(%rsp,%rbx), MSG0_A + movdqu 1*16(%rsp,%rbx), MSG1_A + movdqu 2*16(%rsp,%rbx), MSG2_A + movdqu 3*16(%rsp,%rbx), MSG3_A + + movdqu 0*16(DATA2), MSG0_B + movdqu 1*16(DATA2), MSG1_B + movdqu 2*16(DATA2), MSG2_B + movdqu 3*16(DATA2), MSG3_B + movdqa MSG0_B, 0*16(%rsp) + movdqa MSG1_B, 1*16(%rsp) + movdqa MSG2_B, 2*16(%rsp) + movdqa MSG3_B, 3*16(%rsp) + movdqu 0*16(%rsp,%rbx), MSG0_B + movdqu 1*16(%rsp,%rbx), MSG1_B + movdqu 2*16(%rsp,%rbx), MSG2_B + movdqu 3*16(%rsp,%rbx), MSG3_B + jmp .Lfinup2x_loop_have_data + + // Prepare a padding block, either: + // + // {0x80, 0, 0, 0, ..., count (as __be64)} + // This is for a block aligned message. + // + // { 0, 0, 0, 0, ..., count (as __be64)} + // This is for a message whose length mod 64 is >= 56. + // + // Pre-swap the endianness of the words. +.Lfinup2x_finalize_countonly: + pxor MSG0_A, MSG0_A + jmp 1f + +.Lfinup2x_finalize_blockaligned: + mov $0x80000000, %ebx + movd %ebx, MSG0_A +1: + pxor MSG1_A, MSG1_A + pxor MSG2_A, MSG2_A + ror $29, COUNT + movq COUNT, MSG3_A + pslldq $8, MSG3_A + movdqa MSG0_A, MSG0_B + pxor MSG1_B, MSG1_B + pxor MSG2_B, MSG2_B + movdqa MSG3_A, MSG3_B + mov $2, FINAL_STEP + jmp .Lfinup2x_loop_have_bswapped_data + +.Lfinup2x_done: + // Write the two digests with all bytes in the correct order. + movdqa STATE0_A, TMP_A + movdqa STATE0_B, TMP_B + punpcklqdq STATE1_A, STATE0_A // GHEF + punpcklqdq STATE1_B, STATE0_B + punpckhqdq TMP_A, STATE1_A // ABCD + punpckhqdq TMP_B, STATE1_B + pshufd $0xB1, STATE0_A, STATE0_A // HGFE + pshufd $0xB1, STATE0_B, STATE0_B + pshufd $0x1B, STATE1_A, STATE1_A // DCBA + pshufd $0x1B, STATE1_B, STATE1_B + pshufb SHUF_MASK, STATE0_A + pshufb SHUF_MASK, STATE0_B + pshufb SHUF_MASK, STATE1_A + pshufb SHUF_MASK, STATE1_B + movdqu STATE0_A, 1*16(OUT1) + movdqu STATE0_B, 1*16(OUT2) + movdqu STATE1_A, 0*16(OUT1) + movdqu STATE1_B, 0*16(OUT2) + + mov %rbp, %rsp + pop %rbp + pop %rbx + RET +SYM_FUNC_END(__sha256_ni_finup2x) + .section .rodata.cst256.K256, "aM", @progbits, 256 .align 64 K256: .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
arch/x86/crypto/sha256_ssse3_glue.c +39 -0
@@ -331,10 +331,15 @@ static void unregister_sha256_avx2(void)#ifdef CONFIG_AS_SHA256_NI asmlinkage void sha256_ni_transform(struct sha256_state *digest,const u8 *data,int rounds); +asmlinkage void __sha256_ni_finup2x(const struct sha256_state *sctx, +const u8 *data1,const u8 *data2,int len, +u8 out1[SHA256_DIGEST_SIZE ], +u8 out2[SHA256_DIGEST_SIZE ]); +static int sha256_ni_update (struct shash_desc *desc,const u8 *data,unsigned int len) {return _sha256_update (desc, data, len, sha256_ni_transform); }
@@ -355,18 +360,52 @@ static int sha256_ni_digest(struct shash_desc *desc, const u8 *data, {return sha256_base_init (desc) ?:sha256_ni_finup (desc, data, len, out); } +static int sha256_ni_finup_mb (struct shash_desc *desc, +const u8 *const data[],unsigned int len, +u8 *const outs[],unsigned int num_msgs) +{ +struct sha256_state *sctx =shash_desc_ctx (desc); + +/* +* num_msgs != 2 should not happen here, since this algorithm sets +* mb_max_msgs=2, and the crypto API handles num_msgs <= 1 before +* calling into the algorithm's finup_mb method. +*/ +if (WARN_ON_ONCE (num_msgs !=2 )) +return -EOPNOTSUPP ; + +if (unlikely (!crypto_simd_usable ())) +return -EOPNOTSUPP ; + +/* __sha256_ni_finup2x() assumes SHA256_BLOCK_SIZE <= len <= INT_MAX. */ +if (unlikely (len <SHA256_BLOCK_SIZE || len >INT_MAX )) +return -EOPNOTSUPP ; + +/* __sha256_ni_finup2x() assumes the following offsets. */ +BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); +BUILD_BUG_ON (offsetof(struct sha256_state , count) !=32 ); +BUILD_BUG_ON (offsetof(struct sha256_state , buf) !=40 ); + +kernel_fpu_begin (); +__sha256_ni_finup2x (sctx, data[0 ], data[1 ], len, outs[0 ], outs[1 ]); +kernel_fpu_end (); +return 0 ; +} +static struct shash_alg sha256_ni_algs[] = { { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_base_init, .update = sha256_ni_update, .final = sha256_ni_final, .finup = sha256_ni_finup, .digest = sha256_ni_digest, + .finup_mb = sha256_ni_finup_mb, .descsize =sizeof (struct sha256_state ), + .mb_max_msgs =2 , .base = { .cra_name ="sha256" , .cra_driver_name ="sha256-ni" , .cra_priority =250 , .cra_blocksize =SHA256_BLOCK_SIZE ,
Signature · 1 line
2.45.1
Eric Biggers
maintainerPATCH v4 5/8 crypto: arm64/sha256-ce - add support for finup_mb
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add an implementation of finup_mb to sha256-ce, using an interleaving factor of 2. It interleaves a finup operation for two equal-length messages that share a common prefix. dm-verity and fs-verity will take advantage of this for greatly improved performance on capable CPUs.
On an ARM Cortex-X1, this increases the throughput of SHA-256 hashing 4096-byte messages by 70%.
--- arch/arm64/crypto/sha2-ce-core.S | 281 ++++++++++++++++++++++++++++++- arch/arm64/crypto/sha2-ce-glue.c | 40 +++++ 2 files changed, 315 insertions(+), 6 deletions(-)
arch/arm64/crypto/sha2-ce-core.S +275 -6
@@ -68,22 +68,26 @@ .word 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5 .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + .macro load_round_constants tmp + adr_l \tmp, .Lsha2_rcon + ld1 { v0.4s- v3.4s}, [\tmp], #64 + ld1 { v4.4s- v7.4s}, [\tmp], #64 + ld1 { v8.4s-v11.4s}, [\tmp], #64 + ld1 {v12.4s-v15.4s}, [\tmp] + .endm + /* * int __sha256_ce_transform(struct sha256_ce_state *sst, u8 const *src, * int blocks) */ .text SYM_FUNC_START(__sha256_ce_transform) - /* load round constants */ - adr_l x8, .Lsha2_rcon - ld1 { v0.4s- v3.4s}, [x8], #64 - ld1 { v4.4s- v7.4s}, [x8], #64 - ld1 { v8.4s-v11.4s}, [x8], #64 - ld1 {v12.4s-v15.4s}, [x8] + + load_round_constants x8 /* load state */ ld1 {dgav.4s, dgbv.4s}, [x0] /* load sha256_ce_state::finalize */
@@ -153,5 +157,270 @@ CPU_LE( rev32 v19.16b, v19.16b ) /* store new state */ 3: st1 {dgav.4s, dgbv.4s}, [x0] mov w0, w2 ret SYM_FUNC_END(__sha256_ce_transform) + + .unreq dga + .unreq dgav + .unreq dgb + .unreq dgbv + .unreq t0 + .unreq t1 + .unreq dg0q + .unreq dg0v + .unreq dg1q + .unreq dg1v + .unreq dg2q + .unreq dg2v + + // parameters for __sha256_ce_finup2x() + sctx .req x0 + data1 .req x1 + data2 .req x2 + len .req w3 + out1 .req x4 + out2 .req x5 + + // other scalar variables + count .req x6 + final_step .req w7 + + // x8-x9 are used as temporaries. + + // v0-v15 are used to cache the SHA-256 round constants. + // v16-v19 are used for the message schedule for the first message. + // v20-v23 are used for the message schedule for the second message. + // v24-v31 are used for the state and temporaries as given below. + // *_a are for the first message and *_b for the second. + state0_a_q .req q24 + state0_a .req v24 + state1_a_q .req q25 + state1_a .req v25 + state0_b_q .req q26 + state0_b .req v26 + state1_b_q .req q27 + state1_b .req v27 + t0_a .req v28 + t0_b .req v29 + t1_a_q .req q30 + t1_a .req v30 + t1_b_q .req q31 + t1_b .req v31 + +#define OFFSETOF_COUNT 32 // offsetof(struct sha256_state, count) +#define OFFSETOF_BUF 40 // offsetof(struct sha256_state, buf) +// offsetof(struct sha256_state, state) is assumed to be 0. + + // Do 4 rounds of SHA-256 for each of two messages (interleaved). m0_a + // and m0_b contain the current 4 message schedule words for the first + // and second message respectively. + // + // If not all the message schedule words have been computed yet, then + // this also computes 4 more message schedule words for each message. + // m1_a-m3_a contain the next 3 groups of 4 message schedule words for + // the first message, and likewise m1_b-m3_b for the second. After + // consuming the current value of m0_a, this macro computes the group + // after m3_a and writes it to m0_a, and likewise for *_b. This means + // that the next (m0_a, m1_a, m2_a, m3_a) is the current (m1_a, m2_a, + // m3_a, m0_a), and likewise for *_b, so the caller must cycle through + // the registers accordingly. + .macro do_4rounds_2x i, k, m0_a, m1_a, m2_a, m3_a, \ + m0_b, m1_b, m2_b, m3_b + add t0_a\().4s, \m0_a\().4s, \k\().4s + add t0_b\().4s, \m0_b\().4s, \k\().4s + .if \i < 48 + sha256su0 \m0_a\().4s, \m1_a\().4s + sha256su0 \m0_b\().4s, \m1_b\().4s + sha256su1 \m0_a\().4s, \m2_a\().4s, \m3_a\().4s + sha256su1 \m0_b\().4s, \m2_b\().4s, \m3_b\().4s + .endif + mov t1_a.16b, state0_a.16b + mov t1_b.16b, state0_b.16b + sha256h state0_a_q, state1_a_q, t0_a\().4s + sha256h state0_b_q, state1_b_q, t0_b\().4s + sha256h2 state1_a_q, t1_a_q, t0_a\().4s + sha256h2 state1_b_q, t1_b_q, t0_b\().4s + .endm + + .macro do_16rounds_2x i, k0, k1, k2, k3 + do_4rounds_2x \i + 0, \k0, v16, v17, v18, v19, v20, v21, v22, v23 + do_4rounds_2x \i + 4, \k1, v17, v18, v19, v16, v21, v22, v23, v20 + do_4rounds_2x \i + 8, \k2, v18, v19, v16, v17, v22, v23, v20, v21 + do_4rounds_2x \i + 12, \k3, v19, v16, v17, v18, v23, v20, v21, v22 + .endm + +// +// void __sha256_ce_finup2x(const struct sha256_state *sctx, +// const u8 *data1, const u8 *data2, int len, +// u8 out1[SHA256_DIGEST_SIZE], +// u8 out2[SHA256_DIGEST_SIZE]); +// +// This function computes the SHA-256 digests of two messages |data1| and +// |data2| that are both |len| bytes long, starting from the initial state +// |sctx|. |len| must be at least SHA256_BLOCK_SIZE. +// +// The instructions for the two SHA-256 operations are interleaved. On many +// CPUs, this is almost twice as fast as hashing each message individually due +// to taking better advantage of the CPU's SHA-256 and SIMD throughput. +// +SYM_FUNC_START(__sha256_ce_finup2x) + sub sp, sp, #128 + mov final_step, #0 + load_round_constants x8 + + // Load the initial state from sctx->state. + ld1 {state0_a.4s-state1_a.4s}, [sctx] + + // Load sctx->count. Take the mod 64 of it to get the number of bytes + // that are buffered in sctx->buf. Also save it in a register with len + // added to it. + ldr x8, [sctx, #OFFSETOF_COUNT] + add count, x8, len, sxtw + and x8, x8, #63 + cbz x8, .Lfinup2x_enter_loop // No bytes buffered? + + // x8 bytes (1 to 63) are currently buffered in sctx->buf. Load them + // followed by the first 64 - x8 bytes of data. Since len >= 64, we + // just load 64 bytes from each of sctx->buf, data1, and data2 + // unconditionally and rearrange the data as needed. + add x9, sctx, #OFFSETOF_BUF + ld1 {v16.16b-v19.16b}, [x9] + st1 {v16.16b-v19.16b}, [sp] + + ld1 {v16.16b-v19.16b}, [data1], #64 + add x9, sp, x8 + st1 {v16.16b-v19.16b}, [x9] + ld1 {v16.4s-v19.4s}, [sp] + + ld1 {v20.16b-v23.16b}, [data2], #64 + st1 {v20.16b-v23.16b}, [x9] + ld1 {v20.4s-v23.4s}, [sp] + + sub len, len, #64 + sub data1, data1, x8 + sub data2, data2, x8 + add len, len, w8 + mov state0_b.16b, state0_a.16b + mov state1_b.16b, state1_a.16b + b .Lfinup2x_loop_have_data + +.Lfinup2x_enter_loop: + sub len, len, #64 + mov state0_b.16b, state0_a.16b + mov state1_b.16b, state1_a.16b +.Lfinup2x_loop: + // Load the next two data blocks. + ld1 {v16.4s-v19.4s}, [data1], #64 + ld1 {v20.4s-v23.4s}, [data2], #64 +.Lfinup2x_loop_have_data: + // Convert the words of the data blocks from big endian. +CPU_LE( rev32 v16.16b, v16.16b ) +CPU_LE( rev32 v17.16b, v17.16b ) +CPU_LE( rev32 v18.16b, v18.16b ) +CPU_LE( rev32 v19.16b, v19.16b ) +CPU_LE( rev32 v20.16b, v20.16b ) +CPU_LE( rev32 v21.16b, v21.16b ) +CPU_LE( rev32 v22.16b, v22.16b ) +CPU_LE( rev32 v23.16b, v23.16b ) +.Lfinup2x_loop_have_bswapped_data: + + // Save the original state for each block. + st1 {state0_a.4s-state1_b.4s}, [sp] + + // Do the SHA-256 rounds on each block. + do_16rounds_2x 0, v0, v1, v2, v3 + do_16rounds_2x 16, v4, v5, v6, v7 + do_16rounds_2x 32, v8, v9, v10, v11 + do_16rounds_2x 48, v12, v13, v14, v15 + + // Add the original state for each block. + ld1 {v16.4s-v19.4s}, [sp] + add state0_a.4s, state0_a.4s, v16.4s + add state1_a.4s, state1_a.4s, v17.4s + add state0_b.4s, state0_b.4s, v18.4s + add state1_b.4s, state1_b.4s, v19.4s + + // Update len and loop back if more blocks remain. + sub len, len, #64 + tbz len, #31, .Lfinup2x_loop // len >= 0? + + // Check if any final blocks need to be handled. + // final_step = 2: all done + // final_step = 1: need to do count-only padding block + // final_step = 0: need to do the block with 0x80 padding byte + tbnz final_step, #1, .Lfinup2x_done + tbnz final_step, #0, .Lfinup2x_finalize_countonly + add len, len, #64 + cbz len, .Lfinup2x_finalize_blockaligned + + // Not block-aligned; 1 <= len <= 63 data bytes remain. Pad the block. + // To do this, write the padding starting with the 0x80 byte to + // &sp[64]. Then for each message, copy the last 64 data bytes to sp + // and load from &sp[64 - len] to get the needed padding block. This + // code relies on the data buffers being >= 64 bytes in length. + sub w8, len, #64 // w8 = len - 64 + add data1, data1, w8, sxtw // data1 += len - 64 + add data2, data2, w8, sxtw // data2 += len - 64 + mov x9, 0x80 + fmov d16, x9 + movi v17.16b, #0 + stp q16, q17, [sp, #64] + stp q17, q17, [sp, #96] + sub x9, sp, w8, sxtw // x9 = &sp[64 - len] + cmp len, #56 + b.ge 1f // will count spill into its own block? + lsl count, count, #3 + rev count, count + str count, [x9, #56] + mov final_step, #2 // won't need count-only block + b 2f +1: + mov final_step, #1 // will need count-only block +2: + ld1 {v16.16b-v19.16b}, [data1] + st1 {v16.16b-v19.16b}, [sp] + ld1 {v16.4s-v19.4s}, [x9] + ld1 {v20.16b-v23.16b}, [data2] + st1 {v20.16b-v23.16b}, [sp] + ld1 {v20.4s-v23.4s}, [x9] + b .Lfinup2x_loop_have_data + + // Prepare a padding block, either: + // + // {0x80, 0, 0, 0, ..., count (as __be64)} + // This is for a block aligned message. + // + // { 0, 0, 0, 0, ..., count (as __be64)} + // This is for a message whose length mod 64 is >= 56. + // + // Pre-swap the endianness of the words. +.Lfinup2x_finalize_countonly: + movi v16.2d, #0 + b 1f +.Lfinup2x_finalize_blockaligned: + mov x8, #0x80000000 + fmov d16, x8 +1: + movi v17.2d, #0 + movi v18.2d, #0 + ror count, count, #29 // ror(lsl(count, 3), 32) + mov v19.d[0], xzr + mov v19.d[1], count + mov v20.16b, v16.16b + movi v21.2d, #0 + movi v22.2d, #0 + mov v23.16b, v19.16b + mov final_step, #2 + b .Lfinup2x_loop_have_bswapped_data + +.Lfinup2x_done: + // Write the two digests with all bytes in the correct order. +CPU_LE( rev32 state0_a.16b, state0_a.16b ) +CPU_LE( rev32 state1_a.16b, state1_a.16b ) +CPU_LE( rev32 state0_b.16b, state0_b.16b ) +CPU_LE( rev32 state1_b.16b, state1_b.16b ) + st1 {state0_a.4s-state1_a.4s}, [out1] + st1 {state0_b.4s-state1_b.4s}, [out2] + add sp, sp, #128 + ret +SYM_FUNC_END(__sha256_ce_finup2x)
arch/arm64/crypto/sha2-ce-glue.c +40 -0
@@ -31,10 +31,15 @@ extern const u32 sha256_ce_offsetof_count;extern const u32 sha256_ce_offsetof_finalize; asmlinkage int __sha256_ce_transform(struct sha256_ce_state *sst, u8 const *src,int blocks); +asmlinkage void __sha256_ce_finup2x(const struct sha256_state *sctx, +const u8 *data1,const u8 *data2,int len, +u8 out1[SHA256_DIGEST_SIZE ], +u8 out2[SHA256_DIGEST_SIZE ]); +static void sha256_ce_transform (struct sha256_state *sst,u8 const *src,int blocks) {while (blocks) {int rem;
@@ -122,10 +127,43 @@ static int sha256_ce_digest(struct shash_desc *desc, const u8 *data, {sha256_base_init (desc);return sha256_ce_finup (desc, data, len, out); } +static int sha256_ce_finup_mb (struct shash_desc *desc, +const u8 *const data[],unsigned int len, +u8 *const outs[],unsigned int num_msgs) +{ +struct sha256_ce_state *sctx =shash_desc_ctx (desc); + +/* +* num_msgs != 2 should not happen here, since this algorithm sets +* mb_max_msgs=2, and the crypto API handles num_msgs <= 1 before +* calling into the algorithm's finup_mb method. +*/ +if (WARN_ON_ONCE (num_msgs !=2 )) +return -EOPNOTSUPP ; + +if (unlikely (!crypto_simd_usable ())) +return -EOPNOTSUPP ; + +/* __sha256_ce_finup2x() assumes SHA256_BLOCK_SIZE <= len <= INT_MAX. */ +if (unlikely (len <SHA256_BLOCK_SIZE || len >INT_MAX )) +return -EOPNOTSUPP ; + +/* __sha256_ce_finup2x() assumes the following offsets. */ +BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); +BUILD_BUG_ON (offsetof(struct sha256_state , count) !=32 ); +BUILD_BUG_ON (offsetof(struct sha256_state , buf) !=40 ); + +kernel_neon_begin (); +__sha256_ce_finup2x (&sctx->sst, data[0 ], data[1 ], len, outs[0 ], + outs[1 ]); +kernel_neon_end (); +return 0 ; +} +static int sha256_ce_export (struct shash_desc *desc,void *out) {struct sha256_ce_state *sctx =shash_desc_ctx (desc);memcpy (out, &sctx->sst,sizeof (struct sha256_state ));
@@ -162,13 +200,15 @@ static struct shash_alg algs[] = { { .init = sha256_base_init, .update = sha256_ce_update, .final = sha256_ce_final, .finup = sha256_ce_finup, .digest = sha256_ce_digest, + .finup_mb = sha256_ce_finup_mb, .export = sha256_ce_export, .import = sha256_ce_import, .descsize = sizeof(struct sha256_ce_state), + .mb_max_msgs = 2, .statesize = sizeof(struct sha256_state), .digestsize = SHA256_DIGEST_SIZE, .base = { .cra_name = "sha256", .cra_driver_name = "sha256-ce",
Signature · 1 line
2.45.1
Re: [PATCH v4 5/8] crypto: arm64/sha256-ce - add support for finup_mb
Quoted 8 lines · Eric Biggers
From: Eric Biggers <[email protected]>
Add an implementation of
finup_mbto sha256-ce, using an interleaving factor of 2. It interleaves a finup operation for two equal-length messages that share a common prefix. dm-verity and fs-verity will take advantage of this for greatly improved performance on capable CPUs.
Quoted 4 lines
On an ARM Cortex-X1, this increases the throughput of SHA-256 hashing 4096-byte messages by 70%.
Signed-off-by Eric Biggers <[email protected]>
Quoted 396 lines
--- arch/arm64/crypto/sha2-ce-core.S | 281 ++++++++++++++++++++++++++++++- arch/arm64/crypto/sha2-ce-glue.c | 40 +++++ 2 files changed, 315 insertions(+), 6 deletions(-)arch/arm64/crypto/sha2-ce-core.S +275
-6@@ -68,22 +68,26 @@ .word 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5 .word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208 .word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + .macro load_round_constants tmp + adr_l \tmp, .Lsha2_rcon + ld1 { v0.4s- v3.4s}, [\tmp], #64 + ld1 { v4.4s- v7.4s}, [\tmp], #64 + ld1 { v8.4s-v11.4s}, [\tmp], #64 + ld1 {v12.4s-v15.4s}, [\tmp] + .endm + /* * int __sha256_ce_transform(struct sha256_ce_state *sst, u8 const *src, * int blocks) */ .text SYM_FUNC_START(__sha256_ce_transform) - /* load round constants */ - adr_l x8, .Lsha2_rcon - ld1 { v0.4s- v3.4s}, [x8], #64 - ld1 { v4.4s- v7.4s}, [x8], #64 - ld1 { v8.4s-v11.4s}, [x8], #64 - ld1 {v12.4s-v15.4s}, [x8] + + load_round_constants x8 /* load state */ ld1 {dgav.4s, dgbv.4s}, [x0] /* load sha256_ce_state::finalize */@@ -153,5 +157,270 @@ CPU_LE( rev32 v19.16b, v19.16b ) /* store new state */ 3: st1 {dgav.4s, dgbv.4s}, [x0] mov w0, w2 ret SYM_FUNC_END(__sha256_ce_transform) + + .unreq dga + .unreq dgav + .unreq dgb + .unreq dgbv + .unreq t0 + .unreq t1 + .unreq dg0q + .unreq dg0v + .unreq dg1q + .unreq dg1v + .unreq dg2q + .unreq dg2v + + // parameters for __sha256_ce_finup2x() + sctx .req x0 + data1 .req x1 + data2 .req x2 + len .req w3 + out1 .req x4 + out2 .req x5 + + // other scalar variables + count .req x6 + final_step .req w7 + + // x8-x9 are used as temporaries. + + // v0-v15 are used to cache the SHA-256 round constants. + // v16-v19 are used for the message schedule for the first message. + // v20-v23 are used for the message schedule for the second message. + // v24-v31 are used for the state and temporaries as given below. + // *_a are for the first message and *_b for the second. + state0_a_q .req q24 + state0_a .req v24 + state1_a_q .req q25 + state1_a .req v25 + state0_b_q .req q26 + state0_b .req v26 + state1_b_q .req q27 + state1_b .req v27 + t0_a .req v28 + t0_b .req v29 + t1_a_q .req q30 + t1_a .req v30 + t1_b_q .req q31 + t1_b .req v31 + +#define OFFSETOF_COUNT 32 // offsetof(struct sha256_state, count) +#define OFFSETOF_BUF 40 // offsetof(struct sha256_state, buf) +// offsetof(struct sha256_state, state) is assumed to be 0. + + // Do 4 rounds of SHA-256 for each of two messages (interleaved). m0_a + // and m0_b contain the current 4 message schedule words for the first + // and second message respectively. + // + // If not all the message schedule words have been computed yet, then + // this also computes 4 more message schedule words for each message. + // m1_a-m3_a contain the next 3 groups of 4 message schedule words for + // the first message, and likewise m1_b-m3_b for the second. After + // consuming the current value of m0_a, this macro computes the group + // after m3_a and writes it to m0_a, and likewise for *_b. This means + // that the next (m0_a, m1_a, m2_a, m3_a) is the current (m1_a, m2_a, + // m3_a, m0_a), and likewise for *_b, so the caller must cycle through + // the registers accordingly. + .macro do_4rounds_2x i, k, m0_a, m1_a, m2_a, m3_a, \ + m0_b, m1_b, m2_b, m3_b + add t0_a\().4s, \m0_a\().4s, \k\().4s + add t0_b\().4s, \m0_b\().4s, \k\().4s + .if \i < 48 + sha256su0 \m0_a\().4s, \m1_a\().4s + sha256su0 \m0_b\().4s, \m1_b\().4s + sha256su1 \m0_a\().4s, \m2_a\().4s, \m3_a\().4s + sha256su1 \m0_b\().4s, \m2_b\().4s, \m3_b\().4s + .endif + mov t1_a.16b, state0_a.16b + mov t1_b.16b, state0_b.16b + sha256h state0_a_q, state1_a_q, t0_a\().4s + sha256h state0_b_q, state1_b_q, t0_b\().4s + sha256h2 state1_a_q, t1_a_q, t0_a\().4s + sha256h2 state1_b_q, t1_b_q, t0_b\().4s + .endm + + .macro do_16rounds_2x i, k0, k1, k2, k3 + do_4rounds_2x \i + 0, \k0, v16, v17, v18, v19, v20, v21, v22, v23 + do_4rounds_2x \i + 4, \k1, v17, v18, v19, v16, v21, v22, v23, v20 + do_4rounds_2x \i + 8, \k2, v18, v19, v16, v17, v22, v23, v20, v21 + do_4rounds_2x \i + 12, \k3, v19, v16, v17, v18, v23, v20, v21, v22 + .endm + +// +// void __sha256_ce_finup2x(const struct sha256_state *sctx, +// const u8 *data1, const u8 *data2, int len, +// u8 out1[SHA256_DIGEST_SIZE], +// u8 out2[SHA256_DIGEST_SIZE]); +// +// This function computes the SHA-256 digests of two messages |data1| and +// |data2| that are both |len| bytes long, starting from the initial state +// |sctx|. |len| must be at least SHA256_BLOCK_SIZE. +// +// The instructions for the two SHA-256 operations are interleaved. On many +// CPUs, this is almost twice as fast as hashing each message individually due +// to taking better advantage of the CPU's SHA-256 and SIMD throughput. +// +SYM_FUNC_START(__sha256_ce_finup2x) + sub sp, sp, #128 + mov final_step, #0 + load_round_constants x8 + + // Load the initial state from sctx->state. + ld1 {state0_a.4s-state1_a.4s}, [sctx] + + // Load sctx->count. Take the mod 64 of it to get the number of bytes + // that are buffered in sctx->buf. Also save it in a register with len + // added to it. + ldr x8, [sctx, #OFFSETOF_COUNT] + add count, x8, len, sxtw + and x8, x8, #63 + cbz x8, .Lfinup2x_enter_loop // No bytes buffered? + + // x8 bytes (1 to 63) are currently buffered in sctx->buf. Load them + // followed by the first 64 - x8 bytes of data. Since len >= 64, we + // just load 64 bytes from each of sctx->buf, data1, and data2 + // unconditionally and rearrange the data as needed. + add x9, sctx, #OFFSETOF_BUF + ld1 {v16.16b-v19.16b}, [x9] + st1 {v16.16b-v19.16b}, [sp] + + ld1 {v16.16b-v19.16b}, [data1], #64 + add x9, sp, x8 + st1 {v16.16b-v19.16b}, [x9] + ld1 {v16.4s-v19.4s}, [sp] + + ld1 {v20.16b-v23.16b}, [data2], #64 + st1 {v20.16b-v23.16b}, [x9] + ld1 {v20.4s-v23.4s}, [sp] + + sub len, len, #64 + sub data1, data1, x8 + sub data2, data2, x8 + add len, len, w8 + mov state0_b.16b, state0_a.16b + mov state1_b.16b, state1_a.16b + b .Lfinup2x_loop_have_data + +.Lfinup2x_enter_loop: + sub len, len, #64 + mov state0_b.16b, state0_a.16b + mov state1_b.16b, state1_a.16b +.Lfinup2x_loop: + // Load the next two data blocks. + ld1 {v16.4s-v19.4s}, [data1], #64 + ld1 {v20.4s-v23.4s}, [data2], #64 +.Lfinup2x_loop_have_data: + // Convert the words of the data blocks from big endian. +CPU_LE( rev32 v16.16b, v16.16b ) +CPU_LE( rev32 v17.16b, v17.16b ) +CPU_LE( rev32 v18.16b, v18.16b ) +CPU_LE( rev32 v19.16b, v19.16b ) +CPU_LE( rev32 v20.16b, v20.16b ) +CPU_LE( rev32 v21.16b, v21.16b ) +CPU_LE( rev32 v22.16b, v22.16b ) +CPU_LE( rev32 v23.16b, v23.16b ) +.Lfinup2x_loop_have_bswapped_data: + + // Save the original state for each block. + st1 {state0_a.4s-state1_b.4s}, [sp] + + // Do the SHA-256 rounds on each block. + do_16rounds_2x 0, v0, v1, v2, v3 + do_16rounds_2x 16, v4, v5, v6, v7 + do_16rounds_2x 32, v8, v9, v10, v11 + do_16rounds_2x 48, v12, v13, v14, v15 + + // Add the original state for each block. + ld1 {v16.4s-v19.4s}, [sp] + add state0_a.4s, state0_a.4s, v16.4s + add state1_a.4s, state1_a.4s, v17.4s + add state0_b.4s, state0_b.4s, v18.4s + add state1_b.4s, state1_b.4s, v19.4s + + // Update len and loop back if more blocks remain. + sub len, len, #64 + tbz len, #31, .Lfinup2x_loop // len >= 0? + + // Check if any final blocks need to be handled. + // final_step = 2: all done + // final_step = 1: need to do count-only padding block + // final_step = 0: need to do the block with 0x80 padding byte + tbnz final_step, #1, .Lfinup2x_done + tbnz final_step, #0, .Lfinup2x_finalize_countonly + add len, len, #64 + cbz len, .Lfinup2x_finalize_blockaligned + + // Not block-aligned; 1 <= len <= 63 data bytes remain. Pad the block. + // To do this, write the padding starting with the 0x80 byte to + // &sp[64]. Then for each message, copy the last 64 data bytes to sp + // and load from &sp[64 - len] to get the needed padding block. This + // code relies on the data buffers being >= 64 bytes in length. + sub w8, len, #64 // w8 = len - 64 + add data1, data1, w8, sxtw // data1 += len - 64 + add data2, data2, w8, sxtw // data2 += len - 64 + mov x9, 0x80 + fmov d16, x9 + movi v17.16b, #0 + stp q16, q17, [sp, #64] + stp q17, q17, [sp, #96] + sub x9, sp, w8, sxtw // x9 = &sp[64 - len] + cmp len, #56 + b.ge 1f // will count spill into its own block? + lsl count, count, #3 + rev count, count + str count, [x9, #56] + mov final_step, #2 // won't need count-only block + b 2f +1: + mov final_step, #1 // will need count-only block +2: + ld1 {v16.16b-v19.16b}, [data1] + st1 {v16.16b-v19.16b}, [sp] + ld1 {v16.4s-v19.4s}, [x9] + ld1 {v20.16b-v23.16b}, [data2] + st1 {v20.16b-v23.16b}, [sp] + ld1 {v20.4s-v23.4s}, [x9] + b .Lfinup2x_loop_have_data + + // Prepare a padding block, either: + // + // {0x80, 0, 0, 0, ..., count (as __be64)} + // This is for a block aligned message. + // + // { 0, 0, 0, 0, ..., count (as __be64)} + // This is for a message whose length mod 64 is >= 56. + // + // Pre-swap the endianness of the words. +.Lfinup2x_finalize_countonly: + movi v16.2d, #0 + b 1f +.Lfinup2x_finalize_blockaligned: + mov x8, #0x80000000 + fmov d16, x8 +1: + movi v17.2d, #0 + movi v18.2d, #0 + ror count, count, #29 // ror(lsl(count, 3), 32) + mov v19.d[0], xzr + mov v19.d[1], count + mov v20.16b, v16.16b + movi v21.2d, #0 + movi v22.2d, #0 + mov v23.16b, v19.16b + mov final_step, #2 + b .Lfinup2x_loop_have_bswapped_data + +.Lfinup2x_done: + // Write the two digests with all bytes in the correct order. +CPU_LE( rev32 state0_a.16b, state0_a.16b ) +CPU_LE( rev32 state1_a.16b, state1_a.16b ) +CPU_LE( rev32 state0_b.16b, state0_b.16b ) +CPU_LE( rev32 state1_b.16b, state1_b.16b ) + st1 {state0_a.4s-state1_a.4s}, [out1] + st1 {state0_b.4s-state1_b.4s}, [out2] + add sp, sp, #128 + ret +SYM_FUNC_END(__sha256_ce_finup2x)arch/arm64/crypto/sha2-ce-glue.c +40
-1@@ -31,10 +31,15 @@ extern const u32 sha256_ce_offsetof_count;extern const u32 sha256_ce_offsetof_finalize; asmlinkage int __sha256_ce_transform(struct sha256_ce_state *sst, u8 const *src,int blocks); +asmlinkage void __sha256_ce_finup2x(const struct sha256_state *sctx, +const u8 *data1,const u8 *data2,int len, +u8 out1[SHA256_DIGEST_SIZE ], +u8 out2[SHA256_DIGEST_SIZE ]); +static void sha256_ce_transform (struct sha256_state *sst,u8 const *src,int blocks) {while (blocks) {int rem;@@ -122,10 +127,43 @@ static int sha256_ce_digest(struct shash_desc *desc, const u8 *data, {sha256_base_init (desc);return sha256_ce_finup (desc, data, len, out); } +static int sha256_ce_finup_mb (struct shash_desc *desc, +const u8 *const data[],unsigned int len, +u8 *const outs[],unsigned int num_msgs) +{ +struct sha256_ce_state *sctx =shash_desc_ctx (desc); + +/* +* num_msgs != 2 should not happen here, since this algorithm sets +* mb_max_msgs=2, and the crypto API handles num_msgs <= 1 before +* calling into the algorithm's finup_mb method. +*/ +if (WARN_ON_ONCE (num_msgs !=2 )) +return -EOPNOTSUPP ; + +if (unlikely (!crypto_simd_usable ())) +return -EOPNOTSUPP ; + +/* __sha256_ce_finup2x() assumes SHA256_BLOCK_SIZE <= len <= INT_MAX. */ +if (unlikely (len <SHA256_BLOCK_SIZE || len >INT_MAX )) +return -EOPNOTSUPP ; + +/* __sha256_ce_finup2x() assumes the following offsets. */ +BUILD_BUG_ON (offsetof(struct sha256_state , state) !=0 ); +BUILD_BUG_ON (offsetof(struct sha256_state , count) !=32 ); +BUILD_BUG_ON (offsetof(struct sha256_state , buf) !=40 ); + +kernel_neon_begin (); +__sha256_ce_finup2x (&sctx->sst, data[0 ], data[1 ], len, outs[0 ], + outs[1 ]); +kernel_neon_end (); +return 0 ; +} +static int sha256_ce_export (struct shash_desc *desc,void *out) {struct sha256_ce_state *sctx =shash_desc_ctx (desc);memcpy (out, &sctx->sst,sizeof (struct sha256_state ));@@ -162,13 +200,15 @@ static struct shash_alg algs[] = { { .init = sha256_base_init, .update = sha256_ce_update, .final = sha256_ce_final, .finup = sha256_ce_finup, .digest = sha256_ce_digest, + .finup_mb = sha256_ce_finup_mb, .export = sha256_ce_export, .import = sha256_ce_import, .descsize = sizeof(struct sha256_ce_state), + .mb_max_msgs = 2, .statesize = sizeof(struct sha256_state), .digestsize = SHA256_DIGEST_SIZE, .base = { .cra_name = "sha256", .cra_driver_name = "sha256-ce", --2.45.1
Eric Biggers
maintainerPATCH v4 6/8 fsverity: improve performance by using multibuffer hashing
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
When supported by the hash algorithm, use crypto_shash_finup_mb() to interleave the hashing of pairs of data blocks. On some CPUs this nearly doubles hashing performance. The increase in overall throughput of cold-cache fsverity reads that I'm seeing on arm64 and x86_64 is roughly 35% (though this metric is hard to measure as it jumps around a lot).
For now this is only done on the verification path, and only for data blocks, not Merkle tree blocks. We could use finup_mb on Merkle tree blocks too, but that is less important as there aren't as many Merkle tree blocks as data blocks, and that would require some additional code restructuring. We could also use finup_mb to accelerate building the Merkle tree, but verification performance is more important.
--- fs/verity/fsverity_private.h | 7 ++ fs/verity/hash_algs.c | 8 +- fs/verity/verify.c | 173 +++++++++++++++++++++++++++++------ 3 files changed, 156 insertions(+), 32 deletions(-)
fs/verity/fsverity_private.h +7 -0
@@ -27,10 +27,15 @@ struct fsverity_hash_alg {/* * The HASH_ALGO_* constant for this algorithm. This is different from * FS_VERITY_HASH_ALG_*, which uses a different numbering scheme. */ enum hash_algo algo_id; +/* +* The maximum supported interleaving factor for multibuffer hashing, or +* 1 if the algorithm doesn't support multibuffer hashing +*/ +int mb_max_msgs; };/* Merkle tree parameters: hash algorithm, initial hash state, and topology */ struct merkle_tree_params {const struct fsverity_hash_alg *hash_alg;/* the hash algorithm */
@@ -150,8 +155,10 @@ static inline void fsverity_init_signature(void) }#endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */ /* verify.c */ +#define FS_VERITY_MAX_PENDING_DATA_BLOCKS 2 +void __init fsverity_init_workqueue (void );#endif /* _FSVERITY_PRIVATE_H */
fs/verity/hash_algs.c +6 -2
@@ -82,12 +82,16 @@ const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,if (WARN_ON_ONCE (alg->digest_size !=crypto_shash_digestsize (tfm))) goto err_free_tfm;if (WARN_ON_ONCE (alg->block_size !=crypto_shash_blocksize (tfm))) goto err_free_tfm; -pr_info ("%s using implementation \"%s\"\n" , - alg->name,crypto_shash_driver_name (tfm)); + alg->mb_max_msgs =min (crypto_shash_mb_max_msgs (tfm), +FS_VERITY_MAX_PENDING_DATA_BLOCKS ); + +pr_info ("%s using implementation \"%s\"%s\n" , + alg->name,crypto_shash_driver_name (tfm), + alg->mb_max_msgs >1 ?" (multibuffer)" :"" );/* pairs with smp_load_acquire() above */ smp_store_release (&alg->tfm, tfm); goto out_unlock;
fs/verity/verify.c +143 -30
292 lines not on this page · raw message
Signature · 1 line
2.45.1
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 11 lines · Eric Biggers
fs/verity/verify.c +10
-0+for (i =0 ; i < ctx->num_pending; i++) { + data[i] = ctx->pending_blocks[i].data; + outs[i] = ctx->pending_blocks[i].hash; + } + + desc->tfm = params->hash_alg->tfm; +if (params->hashstate) + err =crypto_shash_import (desc, params->hashstate); +else + err =crypto_shash_init (desc);
fs/verity/verify.c verify_data_block(struct inode *inode, struct fsverity_info *vi, · line 329
Quoted 10 lines
fs/verity/verify.c +10
-0+if (err) { +fsverity_err (inode,"Error %d importing hash state" , err); +return false; + } + err =crypto_shash_finup_mb (desc, data, params->block_size, outs, + ctx->num_pending); +if (err) { +fsverity_err (inode,"Error %d computing block hashes" , err); +return false; + }
So with ahash operating in synchronous mode (callback == NULL), this would look like:
struct ahash_request *reqs[FS_VERITY_MAX_PENDING_DATA_BLOCKS];
for (i = 0; i < ctx->num_pending; i++) {
reqs[i] = fsverity_alloc_hash_request();
if (!req) {
free all reqs;
return false;
}
if (params->hashstate)
err = crypto_ahash_import(&reqs[i], params->hashstate);
else
err = crypto_ahash_init(&reqs[i]);
if (err) {
fsverity_err(inode, "Error %d importing hash state", err);
free all reqs;
return false;
}
}
for (i = 0; i < ctx->num_pending; i++) {
unsigned more;
if (params->hashstate)
err = crypto_ahash_import(req, params->hashstate);
else
err = crypto_ahash_init(req);
if (err) {
fsverity_err(inode, "Error %d importing hash state", err);
free all requests;
return false;
}
more = 0;
if (i + 1 < ctx->num_pending)
more = CRYPTO_TFM_REQ_MORE;
ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | more,
NULL, NULL);
ahash_request_set_crypt(req, ctx->pending_blocks[i].sg,
ctx->pending_blocks[i].hash,
params->block_size);
err = crypto_ahash_finup(req);
if (err) {
fsverity_err(inode, "Error %d computing block hashes", err);
free all requests;
return false;
}
}You're hiding some of the complexity by not allocating memory explicitly for each hash state. This might fit on the stack for two requests, but eventually you will have to allocate memory.
With the ahash API, the allocation is explicit.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 78 lines · Herbert Xu
Quoted 11 lines · Eric Biggers
fs/verity/verify.c +10
-0+for (i =0 ; i < ctx->num_pending; i++) { + data[i] = ctx->pending_blocks[i].data; + outs[i] = ctx->pending_blocks[i].hash; + } + + desc->tfm = params->hash_alg->tfm; +if (params->hashstate) + err =crypto_shash_import (desc, params->hashstate); +else + err =crypto_shash_init (desc);fs/verity/verify.c verify_data_block(struct inode *inode, struct fsverity_info *vi, · line 329
Quoted 10 lines
fs/verity/verify.c +10
-0+if (err) { +fsverity_err (inode,"Error %d importing hash state" , err); +return false; + } + err =crypto_shash_finup_mb (desc, data, params->block_size, outs, + ctx->num_pending); +if (err) { +fsverity_err (inode,"Error %d computing block hashes" , err); +return false; + }So with ahash operating in synchronous mode (callback == NULL), this would look like:
struct ahash_request *reqs[FS_VERITY_MAX_PENDING_DATA_BLOCKS]; for (i = 0; i < ctx->num_pending; i++) { reqs[i] = fsverity_alloc_hash_request(); if (!req) { free all reqs; return false; } if (params->hashstate) err = crypto_ahash_import(&reqs[i], params->hashstate); else err = crypto_ahash_init(&reqs[i]); if (err) { fsverity_err(inode, "Error %d importing hash state", err); free all reqs; return false; } } for (i = 0; i < ctx->num_pending; i++) { unsigned more; if (params->hashstate) err = crypto_ahash_import(req, params->hashstate); else err = crypto_ahash_init(req); if (err) { fsverity_err(inode, "Error %d importing hash state", err); free all requests; return false; } more = 0; if (i + 1 < ctx->num_pending) more = CRYPTO_TFM_REQ_MORE; ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | more, NULL, NULL); ahash_request_set_crypt(req, ctx->pending_blocks[i].sg, ctx->pending_blocks[i].hash, params->block_size); err = crypto_ahash_finup(req); if (err) { fsverity_err(inode, "Error %d computing block hashes", err); free all requests; return false; } }
Quoted 6 lines
You're hiding some of the complexity by not allocating memory explicitly for each hash state. This might fit on the stack for two requests, but eventually you will have to allocate memory.
With the ahash API, the allocation is explicit.
This doesn't make any sense, though. First, the requests need to be enqueued for the task, but crypto_ahash_finup() would only have the ability to enqueue it in a queue associated with the tfm, which is shared by many tasks. So it can't actually work unless the tfm maintained a separate queue for each task, which would be really complex. Second, it adds a memory allocation per block which is very undesirable. You claim that it's needed anyway, but actually it's not; with my API there is only one initial hash state regardless of how high the interleaving factor is. In fact, if multiple initial states were allowed, multibuffer hashing would become much more complex because the underlying algorithm would need to validate that these different states are synced up. My proposal is much simpler and avoids all this unnecessary overhead.
Really the only reason to even consider ahash at all would be try to support software hashing and off-CPU hardware accelerators using the "same" code. However, your proposal would not achieve that either, as it would not use the async callback. Note, as far as I know no one actually cares about off-CPU hardware accelerator support in fsverity anyway...
- Eric
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 4 lines · Eric Biggers
This doesn't make any sense, though. First, the requests need to be enqueued for the task, but
crypto_ahash_finup()would only have the ability to enqueue it in a queue associated with the tfm, which is shared by many tasks. So it can't
OK I screwed up that one. But that's not hard to fix. We could simply add request chaining:
ahash_request_chain(req1, req2); ahash_request_chain(req2, req3); ... ahash_request_chain(reqn1, reqn); err = crypto_ahash_finup(req1);
Quoted 5 lines
actually work unless the tfm maintained a separate queue for each task, which would be really complex. Second, it adds a memory allocation per block which is very undesirable. You claim that it's needed anyway, but actually it's not; with my API there is only one initial hash state regardless of how high the interleaving factor is. In fact, if multiple initial states were allowed,
Sure you don't need it for two interleaved requests. But as you scale up to 16 and beyond, surely at some point you're going to want to move the hash states off the stack.
Quoted 3 lines
multibuffer hashing would become much more complex because the underlying algorithm would need to validate that these different states are synced up. My proposal is much simpler and avoids all this unnecessary overhead.
We could simply state that these chained requests must be on block boundaries, similar to how we handle block ciphers. This is not a big deal.
Quoted 5 lines
Really the only reason to even consider ahash at all would be try to support software hashing and off-CPU hardware accelerators using the "same" code. However, your proposal would not achieve that either, as it would not use the async callback. Note, as far as I know no one actually cares about off-CPU hardware accelerator support in fsverity anyway...
The other thing is that verity doesn't benefit from shash at all. It appears to be doing kmap's on every single request.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Hi Eric:
I really appreciate your suggestion of having the user driving multi-request hashing as opposed to the bodge that we had before. I think that is the perfect way of doing this, similar to how we handled TCP segmentation with TSO.
However, I really dislike the idea of shoehorning this into shash. I know you really like shash, but I think there are some clear benefits to be had by coupling this with ahash.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 4 lines · Herbert Xu
However, I really dislike the idea of shoehorning this into shash. I know you really like shash, but I think there are some clear benefits to be had by coupling this with ahash.
If we do this properly, we should be able to immediately use the mb code with IPsec. In the network stack, we already aggregate the data prior to IPsec with GSO. So at the boundary between IPsec and the Crypto API, it's dividing chunks of data up to 64K into 1500-byte packets and feeding them to crypto one at a time.
It really should be sending the whole chain of packets to us as a unit.
Once we have a proper mb interface, we can fix that and immediately get the benefit of mb hashing.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 12 lines · Herbert Xu
Quoted 4 lines · Herbert Xu
However, I really dislike the idea of shoehorning this into shash. I know you really like shash, but I think there are some clear benefits to be had by coupling this with ahash.
If we do this properly, we should be able to immediately use the mb code with IPsec. In the network stack, we already aggregate the data prior to IPsec with GSO. So at the boundary between IPsec and the Crypto API, it's dividing chunks of data up to 64K into 1500-byte packets and feeding them to crypto one at a time.
Quoted 6 lines
It really should be sending the whole chain of packets to us as a unit.
Once we have a proper mb interface, we can fix that and immediately get the benefit of mb hashing.
This would at most apply to AH, not to ESP. Is AH commonly used these days? Also even for AH, the IPsec code would need to be significantly restructured to make use of multibuffer hashing. See how the segmentation happens in xfrm_output_gso(), but the ahash calls happen much lower in the stack.
I'm guessing that you've had the AH use case in mind since your earlier comments. Given you were originally pushing for this to be supported using the existing async support in the ahash API (which would have required fewer code changes on the AH side), but we now agree that is not feasible, maybe it is time to reconsider whether it would still be worthwhile to make all the changes to the AH code needed to support this?
Also, even if it would be worthwhile and would use ahash, ahash is almost always just a wrapper for shash. So the shash support would be needed anyway.
- Eric
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 2 lines · Eric Biggers
This would at most apply to AH, not to ESP. Is AH commonly used these days?
No AH is completely useless. However, this applies perfectly to ESP, in conjunction with authenc. Obviously we would need to add request linking to authenc (AEAD) as well so that it can pass it along to sha.
BTW, does any of this interleaving apply to AES? If so we should explore adding request linking to skcipher as well.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 4 lines · Herbert Xu
Quoted 2 lines · Eric Biggers
This would at most apply to AH, not to ESP. Is AH commonly used these days?
Quoted 8 lines
No AH is completely useless. However, this applies perfectly to ESP, in conjunction with authenc. Obviously we would need to add request linking to authenc (AEAD) as well so that it can pass it along to sha.
BTW, does any of this interleaving apply to AES? If so we should explore adding request linking to skcipher as well.
With AES, interleaving would only help with non-parallelizable modes such as CBC encryption. Anyone who cares about IPsec performance should of course be using AES-GCM, which is parallelizable. Especially since my other patch
https://lore.kernel.org/linux-crypto/[email protected]/
is making AES-GCM twice as fast...
With hashing we unfortunately don't have the luxury of there being widely used and accepted parallelizable algorithms. In particular, all the SHAs are serialized. So that's why interleaving makes sense there.
In any case, it seems that what you're asking for at this point is far beyond the scope of this patchset.
- Eric
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 6 lines · Eric Biggers
With AES, interleaving would only help with non-parallelizable modes such as CBC encryption. Anyone who cares about IPsec performance should of course be using AES-GCM, which is parallelizable. Especially since my other patch
https://lore.kernel.org/linux-crypto/[email protected]/
is making AES-GCM twice as fast...
Algorithm selection may be limited by peer capability. For IPsec, if SHA is being used, then most likely CBC is also being used.
Quoted 2 lines
In any case, it seems that what you're asking for at this point is far beyond the scope of this patchset.
I'm more than happy to take this over if you don't wish to extend it beyond the storage usage cases. According to the original Intel sha2-mb submission, this should result in at least a two-fold speed-up.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 12 lines · Herbert Xu
Quoted 6 lines · Eric Biggers
With AES, interleaving would only help with non-parallelizable modes such as CBC encryption. Anyone who cares about IPsec performance should of course be using AES-GCM, which is parallelizable. Especially since my other patch
https://lore.kernel.org/linux-crypto/[email protected]/
is making AES-GCM twice as fast...Algorithm selection may be limited by peer capability. For IPsec, if SHA is being used, then most likely CBC is also being used.
IPSec users relying on software crypto and authenc() and caring about performance seems like a rather niche use case to me.
Quoted 8 lines
Quoted 2 lines
In any case, it seems that what you're asking for at this point is far beyond the scope of this patchset.
I'm more than happy to take this over if you don't wish to extend it beyond the storage usage cases. According to the original Intel sha2-mb submission, this should result in at least a two-fold speed-up.
I'm struggling to follow this debate. Surely, if this functionality needs to live in ahash, the shash fallbacks need to implement this parallel scheme too, or ahash would end up just feeding the requests into shash sequentially, defeating the purpose. It is then up to the API client to choose between ahash or shash, just as it can today.
So Eric has a pretty strong case for his shash implementation; kmap_local() is essentially a NOP on architectures that anyone still cares about (unlike kmap_atomic() which still disables preemption), so I don't have a problem with the caller relying on that in order to be able to use shash directly. The whole scatterlist / request alloc dance is just too tedious and pointless, given that in practice, it all gets relegated to shash anyway.
But my point is that even if we go with Herbert's proposal for the ahash, we'll still need something like Eric's code on the shash side.
For the true async accelerator use case, none of this should make any difference, right? If the caller already tolerates async (but in-order) completion, implementing this request chaining doesn't buy it anything. So only when the caller is sync and the implementation is async, we might be able to do something smart where the caller waits on a single completion that signals the completion of a set of inputs. But this is also rather niche, so not worth holding up this effort.
So Herbert, what would the ahash_to_shash plumbing look like for the ahash API that you are proposing? What changes would it require to shash, and how much to they deviate from what Eric is suggesting?
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 3 lines · Ard Biesheuvel
IPSec users relying on software crypto and
authenc()and caring about performance seems like a rather niche use case to me.
It's no more niche than fs/verity and dm-integrity. In fact, this could potentially be used for all algorithms. Just the reduction in the number of function calls may produce enough of a benefit (this is something I observed when adding GSO, even without any actual hardware offload, simply aggregating packets into larger units produced a visible benefit).
Quoted 5 lines
I'm struggling to follow this debate. Surely, if this functionality needs to live in ahash, the shash fallbacks need to implement this parallel scheme too, or ahash would end up just feeding the requests into shash sequentially, defeating the purpose. It is then up to the API client to choose between ahash or shash, just as it can today.
I've never suggested it adding it to shash at all. In fact that's my primary problem with this interface. I think it should be ahash only. Just like skcipher and aead.
My reasoning is that this should cater mostly to bulk data, i.e., multiple pages (e.g., for IPsec we're talking about 64K chunks, actually that (the 64K limit) is something that we should really extend, it's not 2014 anymore). These typically will be more easily accessed as a number of distinct pages rather than as a contiguous mapping.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 7 lines · Herbert Xu
Quoted 3 lines · Ard Biesheuvel
IPSec users relying on software crypto and
authenc()and caring about performance seems like a rather niche use case to me.It's no more niche than fs/verity and dm-integrity.
fsverity is used by Android, which is the diametrical opposite of niche when it comes to Linux distros.
Quoted 6 lines
this could potentially be used for all algorithms. Just the reduction in the number of function calls may produce enough of a benefit (this is something I observed when adding GSO, even without any actual hardware offload, simply aggregating packets into larger units produced a visible benefit).
Sure. But my point is that anyone who cares enough about IPsec performance would have stopped using authenc(cbc(aes),hmac(sha2)) long ago and moved to GCM or even ChaChaPoly. This is not just a matter of efficiency in the implementation - using a general purpose hash function such as SHA-256 [twice] rather than GHASH or Poly1305 is just overkill.
So complicating the performance optimization of something that runs on every (non-Apple) phone for the benefit of something that is rarely used in a context where the performance matters seems unreasonable to me.
Quoted 10 lines
Quoted 5 lines
I'm struggling to follow this debate. Surely, if this functionality needs to live in ahash, the shash fallbacks need to implement this parallel scheme too, or ahash would end up just feeding the requests into shash sequentially, defeating the purpose. It is then up to the API client to choose between ahash or shash, just as it can today.
I've never suggested it adding it to shash at all. In fact that's my primary problem with this interface. I think it should be ahash only. Just like skcipher and aead.
So again, how would that work for ahash falling back to shash. Are you saying every existing shash implementation should be duplicated into an ahash so that the multibuffer optimization can be added? shash is a public interface so we cannot just remove the existing ones and we'll end up carrying both forever.
Quoted 7 lines
My reasoning is that this should cater mostly to bulk data, i.e., multiple pages (e.g., for IPsec we're talking about 64K chunks, actually that (the 64K limit) is something that we should really extend, it's not 2014 anymore). These typically will be more easily accessed as a number of distinct pages rather than as a contiguous mapping.
Sure, but the block I/O world is very different. Forcing it to use an API modeled after how IPsec might use it seems, again, unreasonable.
So these 64k chunks are made up of 1500 byte frames that can be hashed in parallel?
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 6 lines · Ard Biesheuvel
So again, how would that work for ahash falling back to shash. Are you saying every existing shash implementation should be duplicated into an ahash so that the multibuffer optimization can be added? shash is a public interface so we cannot just remove the existing ones and we'll end up carrying both forever.
It should do the same thing for ahash algorithms that do not support multiple requests. IOW it should process the requests one by one.
Quoted 2 lines
Sure, but the block I/O world is very different. Forcing it to use an API modeled after how IPsec might use it seems, again, unreasonable.
It's not different at all. You can see that by the proliferation of kmap calls in fs/verity. It's a fundamental issue. You can't consistently get a large contiguous allocation beyond one page due to fragmentation. So large data is always going to be scattered.
BTW, I'm all for elminating the overhead when you already have a linear address for scattered memory, e.g., through vmalloc. We should definitely improve our interface for ahash/skcipher/aead so that vmalloc addresses (as well as kmalloc virtual addresses by extension) are supported as first class citizens, and we don't turn them into SG lists unless it's necessary for DMA.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 12 lines · Herbert Xu
Quoted 6 lines · Ard Biesheuvel
So again, how would that work for ahash falling back to shash. Are you saying every existing shash implementation should be duplicated into an ahash so that the multibuffer optimization can be added? shash is a public interface so we cannot just remove the existing ones and we'll end up carrying both forever.
It should do the same thing for ahash algorithms that do not support multiple requests. IOW it should process the requests one by one.
That is not what I am asking.
Are you suggesting that, e.g., the arm64 sha2 shash implementation that is modified by this series should instead expose both an shash as before, and an ahash built around the same asm code that exposes the multibuffer capability?
Quoted 8 lines
Quoted 2 lines
Sure, but the block I/O world is very different. Forcing it to use an API modeled after how IPsec might use it seems, again, unreasonable.
It's not different at all. You can see that by the proliferation of kmap calls in fs/verity. It's a fundamental issue. You can't consistently get a large contiguous allocation beyond one page due to fragmentation. So large data is always going to be scattered.
I don't think this is true for many uses of the block layer.
Quoted 7 lines
BTW, I'm all for elminating the overhead when you already have a linear address for scattered memory, e.g., through vmalloc. We should definitely improve our interface for ahash/skcipher/aead so that vmalloc addresses (as well as kmalloc virtual addresses by extension) are supported as first class citizens, and we don't turn them into SG lists unless it's necessary for DMA.
Yes, this is something I've been pondering for a while. An ahash/skcipher/aead with CRYPTO_ALG_ASYNC cleared (which would guarantee that any provided VA would not be referenced after the algo invocation returns) should be able to consume a request that carries virtual addresses rather than SG lists. Given that it is up to the caller to choose between sync and async, it would be in a good position also to judge whether it wants to use stack/vmalloc addresses.
I'll have a stab at this.
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 5 lines · Ard Biesheuvel
Are you suggesting that, e.g., the arm64 sha2 shash implementation that is modified by this series should instead expose both an shash as before, and an ahash built around the same asm code that exposes the multibuffer capability?
Yes the multi-request handling should be implemented as ahash only.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Hi Herbert,
Quoted 10 lines · Herbert Xu
Quoted 5 lines · Ard Biesheuvel
Are you suggesting that, e.g., the arm64 sha2 shash implementation that is modified by this series should instead expose both an shash as before, and an ahash built around the same asm code that exposes the multibuffer capability?
Yes the multi-request handling should be implemented as ahash only.
That is not a reasonable way to do it. It would be much more complex, more error-prone, and slower than my proposal. Also your proposal wouldn't actually bring you much closer to being able to optimize your authenc use case.
First, each algorithm implementation that wants to support your scheme would need to implement the ahash API alongside shash. This would be a lot of duplicated code, and it would need to support all quirks of the ahash API including scatterlists.
Second, allowing an ahash_request to actually be a list of requests creates an ambiguity where it will often be unclear when code is supposed to operate on an ahash_request individually vs. on the whole list. This is error-prone, as it invites bugs where a crypto operation is done on only one request of the list. This is a bad design, especially for a cryptographic API. We would have crypto_ahash_*() do some checks to prevent multi-requests from reaching algorithms that don't support the particular kind of request being made. But there will be a lot of cases to consider.
Third, it would be hard to actually implement multibuffer hashing given an arbitrary list of requests. For multibuffer hashing to work, the lengths of all the buffers must be synced up, including the internal buffers in the hash states as well as every buffer that is produced while walking the scatterlists. We can place constraints on what is supported, but those constraints will need to be clearly specified, and each algorithm will actually need to check for them and do something sane (error or fallback) when they are not met. Note that it would be possible for the messages to get out of sync in the middle of walking the scatterlists, which would be difficult to handle. All this would add a lot of complexity and overhead compared to my proposal, which naturally expresses the same-length constraints in the API.
Fourth, having the API be ahash-only would also force fsverity to switch back to the ahash API, which would add complexity and overhead. The shash API is easier to use even when working with pages, as the diffstat of commit 8fcd94add6c5 clearly shows. The ahash API also has more overhead, including in doing the needed memory allocation, setting up a scatterlist, initializing required but irrelevant or redundant fields in the ahash_request, and shash_ahash_finup() running the fully generalized scatterlist walker on the scatterlist just to finally end up with a pointer which the caller could have provided directly. All this overhead adds up, even when hashing 4K blocks. Back when CPU-based crypto was slow this didn't really matter, but now it's fast and these small overheads are significant when trying to keep up with storage device speeds (which are also fast now). E.g., even disregarding the memory allocation, hashing a 4K block is about 5% faster with shash than with ahash on x86_64.
Of course, I'd need to support ahash in fsverity anyway if someone were to actually need support for non-inline hardware offload in fsverity (and if I decide to accept that despite many hardware drivers being buggy). But really the best way to do this would be to support ahash alongside shash, like what I'm proposing in dm-verity -- perhaps with ahash support limited to the data blocks only, as that's the most performance critical part. The ahash code path would use the async callback to actually properly support offload, which would mean the code would be substantially different anyway due to the fundamentally different computational model, especially vs. multibuffer hashing. A "single" API, that works for both hardware offload and for the vast majority of users that simply need very low overhead software crypto, would be nice in theory. But I'm afraid it's been shown repeatedly that just doesn't work... The existence of lib/crypto/, shash, lskcipher, and scomp all reflect this.
I understand that you think the ahash based API would make it easier to add multibuffer support to "authenc(hmac(sha256),cbc(aes))" for IPsec, which seems to be a very important use case for you (though it isn't relevant to nearly as many systems as dm-verity and fsverity are). Regardless, the reality is that it would be much more difficult to take advantage of multibuffer crypto in the IPsec authenc use case than in dm-verity and fsverity. authenc uses multiple underlying algorithms, AES-CBC and HMAC-SHA256, that would both have to use multibuffer crypto in order to see a significant benefit, seeing as even if the SHA-256 support could be wired up through HMAC-SHA256, encryption would be bottlenecked on AES-CBC, especially on Intel CPUs. It also looks like the IPsec code would need a lot of updates to support multibuffer crypto.
At the same time, an easier way to optimize "authenc(hmac(sha256),cbc(aes))" would be to add an implementation of it to arch/x86/crypto/ that interleaves the AES-CBC and SHA-256 and also avoids the overhead associated with the template based approach (such as all the extra indirect calls). Of course, that would require writing assembly, but so would multibuffer crypto. It seems unlikely that someone would step in to do all the work to implement a multibuffer optimization for this algorithm and its use in IPsec, when no one has ever bothered to optimize the single-buffer case in the first place, which has been possible all along and would require no API or IPsec changes...
In any case, any potential multi-request support in ahash, skcipher, or aead should be separate considerations from the simple function in shash that I'm proposing. It makes sense to have the shash support regardless.
Ultimately, I need to have dm-verity and fsverity be properly optimized in the downstreams that are most relevant to me. If you're not going to allow the upstream crypto API to provide the needed functionality in a reasonable way, then I'll need to shift my focus to getting this patchset into downstream kernels such as Android and Chrome OS instead.
- Eric
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 12 lines · Eric Biggers
I understand that you think the ahash based API would make it easier to add multibuffer support to "authenc(hmac(sha256),cbc(aes))" for IPsec, which seems to be a very important use case for you (though it isn't relevant to nearly as many systems as dm-verity and fsverity are). Regardless, the reality is that it would be much more difficult to take advantage of multibuffer crypto in the IPsec authenc use case than in dm-verity and fsverity. authenc uses multiple underlying algorithms, AES-CBC and HMAC-SHA256, that would both have to use multibuffer crypto in order to see a significant benefit, seeing as even if the SHA-256 support could be wired up through HMAC-SHA256, encryption would be bottlenecked on AES-CBC, especially on Intel CPUs. It also looks like the IPsec code would need a lot of updates to support multibuffer crypto.
The linked-request thing feeds nicely into networking. In fact that's where I got the idea of linking them from. In networking a large GSO (currently limited to 64K but theoretically we could make it unlimited) packet is automatically split up into a linked list of MTU-sized skb's.
Therefore if we switched to a linked-list API networking could give us the buffers with minimal changes.
BTW, I found an old Intel paper that claims through their multi- buffer strategy they were able to make AES-CBC-XCBC beat AES-GCM. I wonder if we could still replicate this today:
Quoted 5 lines
Ultimately, I need to have dm-verity and fsverity be properly optimized in the downstreams that are most relevant to me. If you're not going to allow the upstream crypto API to provide the needed functionality in a reasonable way, then I'll need to shift my focus to getting this patchset into downstream kernels such as Android and Chrome OS instead.
I totally understand that this is your priority. But please give me some time to see if we can devise something that works for both scenarios.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 3 lines · Herbert Xu
Therefore if we switched to a linked-list API networking could give us the buffers with minimal changes.
BTW, this is not just about parallelising hashing. Just as one of the most significant benefits of GSO does not come from hardware offload, but rather the amortisation of (network) stack overhead. IOW you're traversing a very deep stack once instead of 40 times (this is the factor for 64K vs MTU, if we extend beyond 64K (which we absolute should do) the benefit would increase as well).
The same should apply to the Crypto API. So even if this was a purely software solution with no assembly code at all, it may well improve GCM performance (at least for users able to feed us bulk data, like networking).
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 12 lines · Herbert Xu
Quoted 3 lines · Herbert Xu
Therefore if we switched to a linked-list API networking could give us the buffers with minimal changes.
BTW, this is not just about parallelising hashing. Just as one of the most significant benefits of GSO does not come from hardware offload, but rather the amortisation of (network) stack overhead. IOW you're traversing a very deep stack once instead of 40 times (this is the factor for 64K vs MTU, if we extend beyond 64K (which we absolute should do) the benefit would increase as well).
Quoted 5 lines
The same should apply to the Crypto API. So even if this was a purely software solution with no assembly code at all, it may well improve GCM performance (at least for users able to feed us bulk data, like networking).
At best this would save an indirect call per message, if the underlying algorithm explicitly added support for it and the user of the API migrated to the multi-request model. This alone doesn't seem worth the effort of migrating to multi-request, especially considering the many other already-possible optimizations that would not require API changes or migrating users to multi-request. The x86_64 AES-GCM is pretty well optimized now after my recent patches, but there's still an indirect call associated with the use of the SIMD helper which could be eliminated, saving one per message (already as much as we could hope to get from multi-request). authenc on the other hand is almost totally unoptimized, as I mentioned before; it makes little sense to talk about any sort of multi-request optimization for it at this point.
- Eric
Re: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
In reply to Herbert XuQuoted 21 lines · Herbert Xu
Quoted 12 lines · Eric Biggers
I understand that you think the ahash based API would make it easier to add multibuffer support to "authenc(hmac(sha256),cbc(aes))" for IPsec, which seems to be a very important use case for you (though it isn't relevant to nearly as many systems as dm-verity and fsverity are). Regardless, the reality is that it would be much more difficult to take advantage of multibuffer crypto in the IPsec authenc use case than in dm-verity and fsverity. authenc uses multiple underlying algorithms, AES-CBC and HMAC-SHA256, that would both have to use multibuffer crypto in order to see a significant benefit, seeing as even if the SHA-256 support could be wired up through HMAC-SHA256, encryption would be bottlenecked on AES-CBC, especially on Intel CPUs. It also looks like the IPsec code would need a lot of updates to support multibuffer crypto.
The linked-request thing feeds nicely into networking. In fact that's where I got the idea of linking them from. In networking a large GSO (currently limited to 64K but theoretically we could make it unlimited) packet is automatically split up into a linked list of MTU-sized skb's.
Quoted 9 lines
Therefore if we switched to a linked-list API networking could give us the buffers with minimal changes.
BTW, I found an old Intel paper that claims through their multi- buffer strategy they were able to make AES-CBC-XCBC beat AES-GCM. I wonder if we could still replicate this today:
This looks like the whitepaper that describes the buggy multibuffer code that we ripped out.
Quoted 10 lines
Quoted 5 lines
Ultimately, I need to have dm-verity and fsverity be properly optimized in the downstreams that are most relevant to me. If you're not going to allow the upstream crypto API to provide the needed functionality in a reasonable way, then I'll need to shift my focus to getting this patchset into downstream kernels such as Android and Chrome OS instead.
I totally understand that this is your priority. But please give me some time to see if we can devise something that works for both scenarios.
The issue here is that the CPU based multibuffer approach has rather tight constraints in terms of input length and the shared prefix, and so designing a more generic API based on ahash doesn't help at all. The intel multibuffer code went off into the weeds entirely attempting to apply this parallel scheme to arbitrary combinations of inputs, so this is something we know we should avoid.
Herbert Xu
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
Quoted 7 lines · Ard Biesheuvel
The issue here is that the CPU based multibuffer approach has rather tight constraints in terms of input length and the shared prefix, and so designing a more generic API based on ahash doesn't help at all. The intel multibuffer code went off into the weeds entirely attempting to apply this parallel scheme to arbitrary combinations of inputs, so this is something we know we should avoid.
The sha-mb approach failed because it failed to aggregate the data properly. By driving this from the data sink, it was doomed to fail.
The correct way to aggregate data is to do it at the source. The user (of the Crypto API) knows exactlty how much data they want to hash and how it's structured. They should be supplying that info to the API so it can use multi-buffer where applicable. Even where multi-buffer isn't available, they would at least benefit from making a single indirect call into the Crypto stack instead of N calls. When N is large (which is almost always the case for TCP) this produces a non-trivial saving.
Sure I understand that you guys are more than happy with N=2 but please let me at least try this out and see if we could make this work for a large value of N.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
In reply to Herbert XuQuoted 6 lines · Herbert Xu
BTW, I found an old Intel paper that claims through their multi- buffer strategy they were able to make AES-CBC-XCBC beat AES-GCM. I wonder if we could still replicate this today:
No, not even close. Even assuming that the lack of parallelizability in AES-CBC and AES-XCBC can be entirely compensated for via multibuffer crypto (which really it can't -- consider single packets, for example), doing AES twice is much more expensive than doing AES and GHASH. GHASH is a universal hash function, and computing a universal hash function is inherently cheaper than computing a cryptographic hash function. But also modern Intel CPUs have very fast carryless multiplication, and it uses a different execution port from what AES uses. So the overhead of AES + GHASH over AES alone is very small. By doing AES twice, you'd be entirely bottlenecked by the ports that can execute the AES instructions, while the other ports go nearly unused. So it would probably be approaching twice as slow as AES-GCM.
Westmere (2010) through Ivy Bridge (2012) are the only Intel CPUs where multibuffer AES-CBC-XCBC could plausibly be faster than AES-GCM (given a sufficiently large number of messages at once), due to the very slow pclmulqdq instruction on those CPUs. This is long since fixed, as pclmulqdq became much faster in Haswell (2013), and faster still in Broadwell. This is exactly what that Intel paper shows; they show AES-GCM becoming fastest in "Gen 4", i.e. Haswell. The paper is from 2012, so of course they don't show anything after that. But AES-GCM has only pulled ahead even more since then.
In theory something like AES-CBC + SHA-256 could be slightly more competitive than AES-CBC + AES-XCBC. But it would still be worse than simply doing AES-GCM -- which again, doesn't need multibuffer, and my recent patches have already fully optimized for recent x86_64 CPUs.
- Eric
Eric Biggers
maintainerRe: [PATCH v4 6/8] fsverity: improve performance by using multibuffer hashing
In reply to Herbert XuQuoted 9 lines · Herbert Xu
Quoted 4 lines · Eric Biggers
This doesn't make any sense, though. First, the requests need to be enqueued for the task, but
crypto_ahash_finup()would only have the ability to enqueue it in a queue associated with the tfm, which is shared by many tasks. So it can'tOK I screwed up that one. But that's not hard to fix. We could simply add request chaining:
Quoted 6 lines
ahash_request_chain(req1, req2); ahash_request_chain(req2, req3); ... ahash_request_chain(reqn1, reqn); err = crypto_ahash_finup(req1);
So after completely changing several times your proposal is getting a bit closer to mine, but it still uses a very clumsy API based around ahash that would be much harder to use and implement correctly. It also says nothing about what the API would look like on the shash side, which would be needed anyway because ahash is almost always just a pointless wrapper for shash. Is there a different API that you are asking for on the shash side? If so, what?
Quoted 9 lines
Quoted 5 lines
actually work unless the tfm maintained a separate queue for each task, which would be really complex. Second, it adds a memory allocation per block which is very undesirable. You claim that it's needed anyway, but actually it's not; with my API there is only one initial hash state regardless of how high the interleaving factor is. In fact, if multiple initial states were allowed,
Sure you don't need it for two interleaved requests. But as you scale up to 16 and beyond, surely at some point you're going to want to move the hash states off the stack.
To reiterate, with my proposal there is only one state in memory. It's a simple API that can't be misused by providing inconsistent properties in the requests. Yes, separate states would be needed if we were to support arbitrary updates, but why add all that complexity before it's actually needed?
Also, "16 and beyond" is highly unlikely to be useful for kernel use cases.
Quoted 7 lines
Quoted 3 lines
multibuffer hashing would become much more complex because the underlying algorithm would need to validate that these different states are synced up. My proposal is much simpler and avoids all this unnecessary overhead.
We could simply state that these chained requests must be on block boundaries, similar to how we handle block ciphers. This is not a big deal.
... which would make it useless for most dm-verity users, as dm-verity uses a 32-byte salt with SHA-256 (which has a 64-byte block size) by default.
Quoted 9 lines
Quoted 6 lines
Really the only reason to even consider ahash at all would be try to support software hashing and off-CPU hardware accelerators using the "same" code. However, your proposal would not achieve that either, as it would not use the async callback. Note, as far as I know no one actually cares about off-CPU hardware accelerator support in fsverity anyway...
The other thing is that verity doesn't benefit from shash at all. It appears to be doing kmap's on every single request.
The diff from switching fsverity from ahash to shash clearly demonstrates otherwise. Yes, fsverity has to map the pages to pass into shash, but that's a very minor thing compared to all the complexity of ahash that was saved. And fsverity already had to map most of the pages anyway to access them.
- Eric
Eric Biggers
maintainerPATCH v4 7/8 dm-verity: hash blocks with shash import+finup when possible
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Currently dm-verity computes the hash of each block by using multiple calls to the "ahash" crypto API. While the exact sequence depends on the chosen dm-verity settings, in the vast majority of cases it is:
1. crypto_ahash_init()
2. crypto_ahash_update() [salt]
3. crypto_ahash_update() [data]
4. crypto_ahash_final()
This is inefficient for two main reasons:
- It makes multiple indirect calls, which is expensive on modern CPUs especially when mitigations for CPU vulnerabilities are enabled.
Since the salt is the same across all blocks on a given dm-verity device, a much more efficient sequence would be to do an import of the pre-salted state, then a finup.
- It uses the ahash (asynchronous hash) API, despite the fact that CPU-based hashing is almost always used in practice, and therefore it experiences the overhead of the ahash-based wrapper for shash. This also means that the new function crypto_shash_finup_mb(), which is specifically designed for fast CPU-based hashing, is unavailable.
Since dm-verity was intentionally converted to ahash to support off-CPU crypto accelerators, wholesale conversion to shash (reverting that change) might not be acceptable. Yet, we should still provide a fast path for shash with the most common dm-verity settings.
Therefore, this patch adds a new shash import+finup based fast path to dm-verity. It is used automatically when appropriate, i.e. when the ahash API and shash APIs resolve to the same underlying algorithm, the dm-verity version is not 0 (so that the salt is hashed before the data), and the data block size is not greater than the page size.
This makes dm-verity optimized for what the vast majority of users want:
CPU-based hashing with the most common settings, while still retaining support for rarer settings and off-CPU crypto accelerators.
In benchmarks with veritysetup's default parameters (SHA-256, 4K data and hash block sizes, 32-byte salt), which also match the parameters that Android currently uses, this patch improves block hashing performance by about 15% on an x86_64 system that supports the SHA-NI instructions, or by about 5% on an arm64 system that supports the ARMv8 SHA2 instructions.
Note that another benefit of using "import" to handle the salt is that if the salt size is equal to the input size of the hash algorithm's compression function, e.g. 64 bytes for SHA-256, then the performance is exactly the same as no salt. (This doesn't seem to be much better than veritysetup's current default of 32-byte salts, due to the way SHA-256's finalization padding works, but it should be marginally better.)
--- drivers/md/dm-verity-fec.c | 13 +- drivers/md/dm-verity-target.c | 336 ++++++++++++++++++++++++---------- drivers/md/dm-verity.h | 27 ++- 3 files changed, 263 insertions(+), 113 deletions(-)
drivers/md/dm-verity-fec.c +7 -6
@@ -184,13 +184,14 @@ static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_io *io,* Locate data block erasures using verity hashes. */ static int fec_is_erasure (struct dm_verity *v,struct dm_verity_io *io,u8 *want_digest,u8 *data) { -if (unlikely (verity_hash (v,verity_io_hash_req (v, io), - data,1 << v->data_dev_block_bits, -verity_io_real_digest (v, io), true))) +if (unlikely (verity_compute_hash_virt (v, io, data, +1 << v->data_dev_block_bits, +verity_io_real_digest (v, io), + true)))return 0 ;return memcmp (verity_io_real_digest (v, io), want_digest, v->digest_size) !=0 ; }
@@ -386,13 +387,13 @@ static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io, pos += fio->nbufs <<DM_VERITY_FEC_BUF_RS_BITS ; }/* Always re-validate the corrected block against the expected hash */ - r =verity_hash (v,verity_io_hash_req (v, io), fio->output, -1 << v->data_dev_block_bits, -verity_io_real_digest (v, io), true); + r =verity_compute_hash_virt (v, io, fio->output, +1 << v->data_dev_block_bits, +verity_io_real_digest (v, io), true);if (unlikely (r <0 ))return r;if (memcmp (verity_io_real_digest (v, io),verity_io_want_digest (v, io), v->digest_size)) {
drivers/md/dm-verity-target.c +239 -97
558 lines not on this page · raw message
drivers/md/dm-verity.h +17 -10
@@ -37,13 +37,15 @@ struct dm_verity {struct dm_dev *data_dev;struct dm_dev *hash_dev;struct dm_target *ti;struct dm_bufio_client *bufio;char *alg_name; -struct crypto_ahash *tfm; +struct crypto_ahash *ahash_tfm;/* either this or shash_tfm is set */ +struct crypto_shash *shash_tfm;/* either this or ahash_tfm is set */ u8 *root_digest;/* digest of the root block */ u8 *salt;/* salt: its size is salt_size */ +u8 *initial_hashstate;/* salted initial state, if shash_tfm is set */ u8 *zero_digest;/* digest for a zero block */ unsigned int salt_size;sector_t data_start;/* data offset in 512-byte sectors */ sector_t hash_start;/* hash start in blocks */ sector_t data_blocks;/* the number of data blocks */
@@ -54,11 +56,11 @@ struct dm_verity {unsigned char levels;/* the number of tree levels */ unsigned char version; bool hash_failed:1; /* set if hash of any block failed */ bool use_bh_wq:1; /* try to verify in BH wq before normal work-queue */unsigned int digest_size;/* digest size for the current hash algorithm */ -unsigned int ahash_reqsize;/* the size of temporary space for crypto */ +unsigned int hash_reqsize;/* the size of temporary space for crypto */ enum verity_mode mode;/* mode for handling verification errors */ unsigned int corrupted_errs;/* Number of errors for corrupted blocks */ struct workqueue_struct *verify_wq;
@@ -92,45 +94,50 @@ struct dm_verity_io {char *recheck_buffer;/* * Three variably-size fields follow this struct: * -* u8 hash_req[v->ahash_reqsize]; +* u8 hash_req[v->hash_reqsize]; * u8 real_digest[v->digest_size]; * u8 want_digest[v->digest_size]; * * To access them use: verity_io_hash_req(), verity_io_real_digest() * and verity_io_want_digest(). +* +* hash_req is either a struct ahash_request or a struct shash_desc, +* depending on whether ahash_tfm or shash_tfm is being used. */ }; -static inline struct ahash_request *verity_io_hash_req (struct dm_verity *v, -struct dm_verity_io *io) +static inline void *verity_io_hash_req (struct dm_verity *v, +struct dm_verity_io *io) { -return (struct ahash_request *)(io +1 ); +return io +1 ; }static inline u8 *verity_io_real_digest (struct dm_verity *v,struct dm_verity_io *io) { -return (u8 *)(io +1 ) + v->ahash_reqsize; +return (u8 *)(io +1 ) + v->hash_reqsize; }static inline u8 *verity_io_want_digest (struct dm_verity *v,struct dm_verity_io *io) { -return (u8 *)(io +1 ) + v->ahash_reqsize + v->digest_size; +return (u8 *)(io +1 ) + v->hash_reqsize + v->digest_size; }extern int verity_for_bv_block (struct dm_verity *v,struct dm_verity_io *io,struct bvec_iter *iter,int (*process)(struct dm_verity *v,struct dm_verity_io *io,u8 *data,size_t len)); -extern int verity_hash (struct dm_verity *v,struct ahash_request *req, -const u8 *data,size_t len,u8 *digest,bool may_sleep); +extern int verity_compute_hash_virt (struct dm_verity *v, +struct dm_verity_io *io, +const u8 *data,size_t len,u8 *digest, +bool may_sleep);extern int verity_hash_for_block (struct dm_verity *v,struct dm_verity_io *io,sector_t block,u8 *digest,bool *is_zero);extern bool dm_is_verity_target (struct dm_target *ti);
Signature · 1 line
2.45.1
Eric Biggers
maintainerPATCH v4 8/8 dm-verity: improve performance by using multibuffer hashing
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
When supported by the hash algorithm, use crypto_shash_finup_mb() to interleave the hashing of pairs of data blocks. On some CPUs this nearly doubles hashing performance. The increase in overall throughput of cold-cache dm-verity reads that I'm seeing on arm64 and x86_64 is roughly 35% (though this metric is hard to measure as it jumps around a lot).
For now this is only done on data blocks, not Merkle tree blocks. We could use finup_mb on Merkle tree blocks too, but that is less important as there aren't as many Merkle tree blocks as data blocks, and that would require some additional code restructuring.
--- drivers/md/dm-verity-fec.c | 24 +-- drivers/md/dm-verity-fec.h | 7 +- drivers/md/dm-verity-target.c | 345 ++++++++++++++++++++++------------ drivers/md/dm-verity.h | 28 +-- 4 files changed, 247 insertions(+), 157 deletions(-)
drivers/md/dm-verity-fec.c +12 -12
@@ -184,18 +184,18 @@ static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_io *io,* Locate data block erasures using verity hashes. */ static int fec_is_erasure (struct dm_verity *v,struct dm_verity_io *io,u8 *want_digest,u8 *data) { +u8 real_digest[HASH_MAX_DIGESTSIZE ]; +if (unlikely (verity_compute_hash_virt (v, io, data,1 << v->data_dev_block_bits, -verity_io_real_digest (v, io), - true))) + real_digest, true)))return 0 ; -return memcmp (verity_io_real_digest (v, io), want_digest, - v->digest_size) !=0 ; +return memcmp (real_digest, want_digest, v->digest_size) !=0 ; }/* * Read data blocks that are part of the RS block and deinterleave as much as * fits into buffers. Check for erasure locations if @neras is non-NULL.
@@ -362,14 +362,15 @@ static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)* (indicated by @offset) in fio->output. If @use_erasures is non-zero, uses * hashes to locate erasures. */ static int fec_decode_rsb (struct dm_verity *v,struct dm_verity_io *io,struct dm_verity_fec_io *fio,u64 rsb,u64 offset, -bool use_erasures) +const u8 *want_digest,bool use_erasures) {int r, neras =0 ;unsigned int pos; +u8 real_digest[HASH_MAX_DIGESTSIZE ]; r =fec_alloc_bufs (v, fio);if (unlikely (r <0 ))return r;
@@ -389,16 +390,15 @@ static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io, }/* Always re-validate the corrected block against the expected hash */ r =verity_compute_hash_virt (v, io, fio->output,1 << v->data_dev_block_bits, -verity_io_real_digest (v, io), true); + real_digest, true);if (unlikely (r <0 ))return r; -if (memcmp (verity_io_real_digest (v, io),verity_io_want_digest (v, io), - v->digest_size)) { +if (memcmp (real_digest, want_digest, v->digest_size)) {DMERR_LIMIT ("%s: FEC %llu: failed to correct (%d erasures)" , v->data_dev->name, (unsigned long long )rsb, neras);return -EILSEQ ; }
@@ -419,12 +419,12 @@ static int fec_bv_copy(struct dm_verity *v, struct dm_verity_io *io, u8 *data,/* * Correct errors in a block. Copies corrected block to dest if non-NULL, * otherwise to a bio_vec starting from iter. */ int verity_fec_decode (struct dm_verity *v,struct dm_verity_io *io, -enum verity_block_type type,sector_t block,u8 *dest, -struct bvec_iter *iter) +enum verity_block_type type,sector_t block, +const u8 *want_digest,u8 *dest,struct bvec_iter *iter) {int r;struct dm_verity_fec_io *fio =fec_io (io);u64 offset, res, rsb;
@@ -463,13 +463,13 @@ int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,/* * Locating erasures is slow, so attempt to recover the block without * them first. Do a second attempt with erasures if the corruption is * bad enough. */ - r =fec_decode_rsb (v, io, fio, rsb, offset, false); + r =fec_decode_rsb (v, io, fio, rsb, offset, want_digest, false);if (r <0 ) { - r =fec_decode_rsb (v, io, fio, rsb, offset, true); + r =fec_decode_rsb (v, io, fio, rsb, offset, want_digest, true);if (r <0 ) goto done; }if (dest)
drivers/md/dm-verity-fec.h +4 -3
@@ -68,11 +68,12 @@ struct dm_verity_fec_io {extern bool verity_fec_is_enabled (struct dm_verity *v);extern int verity_fec_decode (struct dm_verity *v,struct dm_verity_io *io,enum verity_block_type type,sector_t block, -u8 *dest,struct bvec_iter *iter); +const u8 *want_digest,u8 *dest, +struct bvec_iter *iter);extern unsigned int verity_fec_status_table (struct dm_verity *v,unsigned int sz,char *result,unsigned int maxlen);extern void verity_fec_finish_io (struct dm_verity_io *io);
@@ -97,12 +98,12 @@ static inline bool verity_fec_is_enabled(struct dm_verity *v)return false; }static inline int verity_fec_decode (struct dm_verity *v,struct dm_verity_io *io, -enum verity_block_type type, -sector_t block,u8 *dest, +enum verity_block_type type,sector_t block, +const u8 *want_digest,u8 *dest,struct bvec_iter *iter) {return -EOPNOTSUPP ; }
drivers/md/dm-verity-target.c +226 -119
529 lines not on this page · raw message
drivers/md/dm-verity.h +5 -23
@@ -55,10 +55,11 @@ struct dm_verity {unsigned char hash_per_block_bits;/* log2(hashes in hash block) */ unsigned char levels;/* the number of tree levels */ unsigned char version; bool hash_failed:1; /* set if hash of any block failed */ bool use_bh_wq:1; /* try to verify in BH wq before normal work-queue */ +unsigned char mb_max_msgs;/* max multibuffer hashing interleaving factor */ unsigned int digest_size;/* digest size for the current hash algorithm */ unsigned int hash_reqsize;/* the size of temporary space for crypto */ enum verity_mode mode;/* mode for handling verification errors */ unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
@@ -92,42 +93,23 @@ struct dm_verity_io {struct work_struct bh_work;char *recheck_buffer;/* -* Three variably-size fields follow this struct: -* -* u8 hash_req[v->hash_reqsize]; -* u8 real_digest[v->digest_size]; -* u8 want_digest[v->digest_size]; -* -* To access them use: verity_io_hash_req(), verity_io_real_digest() -* and verity_io_want_digest(). -* -* hash_req is either a struct ahash_request or a struct shash_desc, -* depending on whether ahash_tfm or shash_tfm is being used. +* This struct is followed by a variable-sized hash request of size +* v->hash_reqsize, either a struct ahash_request or a struct shash_desc +* (depending on whether ahash_tfm or shash_tfm is being used). To +* access it, use verity_io_hash_req(). */ };static inline void *verity_io_hash_req (struct dm_verity *v,struct dm_verity_io *io) {return io +1 ; } -static inline u8 *verity_io_real_digest (struct dm_verity *v, -struct dm_verity_io *io) -{ -return (u8 *)(io +1 ) + v->hash_reqsize; -} - -static inline u8 *verity_io_want_digest (struct dm_verity *v, -struct dm_verity_io *io) -{ -return (u8 *)(io +1 ) + v->hash_reqsize + v->digest_size; -} -extern int verity_for_bv_block (struct dm_verity *v,struct dm_verity_io *io,struct bvec_iter *iter,int (*process)(struct dm_verity *v,struct dm_verity_io *io,u8 *data,size_t len));
Signature · 1 line
2.45.1