All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: linux-crypto@vger.kernel.org,
	Herbert Xu <herbert@gondor.apana.org.au>,
	David Miller <davem@davemloft.net>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Samuel Neves <sneves@dei.uc.pt>, Arnd Bergmann <arnd@arndb.de>,
	Andy Lutomirski <luto@kernel.org>,
	Martin Willi <martin@strongswan.org>,
	Rene van Dorst <opensource@vdorst.com>
Subject: Re: [PATCH v3 02/29] crypto: x86/chacha - depend on generic chacha library instead of crypto driver
Date: Thu, 10 Oct 2019 23:00:28 -0700	[thread overview]
Message-ID: <20191011060028.GA23882@sol.localdomain> (raw)
In-Reply-To: <20191007164610.6881-3-ard.biesheuvel@linaro.org>

On Mon, Oct 07, 2019 at 06:45:43PM +0200, Ard Biesheuvel wrote:
> In preparation of extending the x86 ChaCha driver to also expose the ChaCha
> library interface, drop the dependency on the chacha_generic crypto driver
> as a non-SIMD fallback, and depend on the generic ChaCha library directly.
> This way, we only pull in the code we actually need, without registering
> a set of ChaCha skciphers that we will never use.
> 
> Since turning the FPU on and off is cheap these days, simplify the SIMD
> routine by dropping the per-page yield, which makes for a cleaner switch
> to the library API as well.
> 
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  arch/x86/crypto/chacha_glue.c | 77 ++++++++++----------
>  crypto/Kconfig                |  2 +-
>  2 files changed, 40 insertions(+), 39 deletions(-)
> 
> diff --git a/arch/x86/crypto/chacha_glue.c b/arch/x86/crypto/chacha_glue.c
> index bc62daa8dafd..3a1a11a4326d 100644
> --- a/arch/x86/crypto/chacha_glue.c
> +++ b/arch/x86/crypto/chacha_glue.c
> @@ -127,32 +127,32 @@ static int chacha_simd_stream_xor(struct skcipher_walk *walk,
>  				  const struct chacha_ctx *ctx, const u8 *iv)
>  {
>  	u32 *state, state_buf[16 + 2] __aligned(8);
> -	int next_yield = 4096; /* bytes until next FPU yield */
> +	bool do_simd;
>  	int err = 0;
>  
>  	BUILD_BUG_ON(CHACHA_STATE_ALIGN != 16);
>  	state = PTR_ALIGN(state_buf + 0, CHACHA_STATE_ALIGN);
>  
> -	crypto_chacha_init(state, ctx, iv);
> +	chacha_init_generic(state, ctx->key, iv);
>  
> +	do_simd = (walk->total > CHACHA_BLOCK_SIZE) && crypto_simd_usable();
>  	while (walk->nbytes > 0) {
>  		unsigned int nbytes = walk->nbytes;
>  
> -		if (nbytes < walk->total) {
> +		if (nbytes < walk->total)
>  			nbytes = round_down(nbytes, walk->stride);
> -			next_yield -= nbytes;
> -		}
> -
> -		chacha_dosimd(state, walk->dst.virt.addr, walk->src.virt.addr,
> -			      nbytes, ctx->nrounds);
>  
> -		if (next_yield <= 0) {
> -			/* temporarily allow preemption */
> -			kernel_fpu_end();
> +		if (!do_simd) {
> +			chacha_crypt_generic(state, walk->dst.virt.addr,
> +					     walk->src.virt.addr, nbytes,
> +					     ctx->nrounds);
> +		} else {
>  			kernel_fpu_begin();
> -			next_yield = 4096;
> +			chacha_dosimd(state, walk->dst.virt.addr,
> +				      walk->src.virt.addr, nbytes,
> +				      ctx->nrounds);
> +			kernel_fpu_end();

Since the kernel_fpu_begin() and kernel_fpu_end() were moved here, it's now
possible to simplify the code by moving the call to skcipher_walk_virt() into
chacha_simd_stream_xor() rather than making the caller do it.

I.e., see what the code was like prior to the following commit:

	commit f9c9bdb5131eee60dc3b92e5126d4c0e291703e2
	Author: Eric Biggers <ebiggers@google.com>
	Date:   Sat Dec 15 12:40:17 2018 -0800

	    crypto: x86/chacha - avoid sleeping under kernel_fpu_begin()

>  		}
> -
>  		err = skcipher_walk_done(walk, walk->nbytes - nbytes);
>  	}
>  
> @@ -164,19 +164,9 @@ static int chacha_simd(struct skcipher_request *req)
>  	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
>  	struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
>  	struct skcipher_walk walk;
> -	int err;
>  
> -	if (req->cryptlen <= CHACHA_BLOCK_SIZE || !crypto_simd_usable())
> -		return crypto_chacha_crypt(req);
> -
> -	err = skcipher_walk_virt(&walk, req, true);
> -	if (err)
> -		return err;
> -
> -	kernel_fpu_begin();
> -	err = chacha_simd_stream_xor(&walk, ctx, req->iv);
> -	kernel_fpu_end();
> -	return err;
> +	return skcipher_walk_virt(&walk, req, true) ?:
> +	       chacha_simd_stream_xor(&walk, ctx, req->iv);
>  }
>  
>  static int xchacha_simd(struct skcipher_request *req)
> @@ -189,31 +179,42 @@ static int xchacha_simd(struct skcipher_request *req)
>  	u8 real_iv[16];
>  	int err;
>  
> -	if (req->cryptlen <= CHACHA_BLOCK_SIZE || !crypto_simd_usable())
> -		return crypto_xchacha_crypt(req);
> -
>  	err = skcipher_walk_virt(&walk, req, true);
>  	if (err)
>  		return err;
>  
>  	BUILD_BUG_ON(CHACHA_STATE_ALIGN != 16);
>  	state = PTR_ALIGN(state_buf + 0, CHACHA_STATE_ALIGN);
> -	crypto_chacha_init(state, ctx, req->iv);
> -
> -	kernel_fpu_begin();
> -
> -	hchacha_block_ssse3(state, subctx.key, ctx->nrounds);
> +	chacha_init_generic(state, ctx->key, req->iv);
> +
> +	if (req->cryptlen > CHACHA_BLOCK_SIZE && crypto_simd_usable()) {
> +		kernel_fpu_begin();
> +		hchacha_block_ssse3(state, subctx.key, ctx->nrounds);
> +		kernel_fpu_end();
> +	} else {
> +		hchacha_block_generic(state, subctx.key, ctx->nrounds);
> +	}
>  	subctx.nrounds = ctx->nrounds;
>  
>  	memcpy(&real_iv[0], req->iv + 24, 8);
>  	memcpy(&real_iv[8], req->iv + 16, 8);
>  	err = chacha_simd_stream_xor(&walk, &subctx, real_iv);
>  
> -	kernel_fpu_end();
> -
>  	return err;

Can use 'return chacha_simd_stream_xor(...') here.

- Eric

  reply	other threads:[~2019-10-11  6:00 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-07 16:45 [PATCH v3 00/29] crypto: crypto API library interfaces for WireGuard Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 01/29] crypto: chacha - move existing library code into lib/crypto Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 02/29] crypto: x86/chacha - depend on generic chacha library instead of crypto driver Ard Biesheuvel
2019-10-11  6:00   ` Eric Biggers [this message]
2019-10-15 10:00   ` Martin Willi
2019-10-15 10:12     ` Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 03/29] crypto: x86/chacha - expose SIMD ChaCha routine as library function Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 04/29] crypto: arm64/chacha - depend on generic chacha library instead of crypto driver Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 05/29] crypto: arm64/chacha - expose arm64 ChaCha routine as library function Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 06/29] crypto: arm/chacha - import Eric Biggers's scalar accelerated ChaCha code Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 07/29] crypto: arm/chacha - remove dependency on generic ChaCha driver Ard Biesheuvel
2019-10-11  6:12   ` Eric Biggers
2019-10-11  6:31   ` Eric Biggers
2019-10-07 16:45 ` [PATCH v3 08/29] crypto: arm/chacha - expose ARM ChaCha routine as library function Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 09/29] crypto: mips/chacha - import 32r2 ChaCha code from Zinc Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 10/29] crypto: mips/chacha - wire up accelerated 32r2 " Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 11/29] crypto: chacha - unexport chacha_generic routines Ard Biesheuvel
2019-10-11  6:04   ` Eric Biggers
2019-10-07 16:45 ` [PATCH v3 12/29] crypto: poly1305 - move core routines into a separate library Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 13/29] crypto: x86/poly1305 - unify Poly1305 state struct with generic code Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 14/29] crypto: poly1305 - expose init/update/final library interface Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 15/29] crypto: x86/poly1305 - depend on generic library not generic shash Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 16/29] crypto: x86/poly1305 - expose existing driver as poly1305 library Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 17/29] crypto: arm64/poly1305 - incorporate OpenSSL/CRYPTOGAMS NEON implementation Ard Biesheuvel
2019-10-07 16:45 ` [PATCH v3 18/29] crypto: arm/poly1305 " Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 19/29] crypto: mips/poly1305 - incorporate OpenSSL/CRYPTOGAMS optimized implementation Ard Biesheuvel
2019-10-07 21:02   ` René van Dorst
2019-10-08  5:55     ` Ard Biesheuvel
2019-10-08 11:38     ` Andy Polyakov
2019-10-08 17:46       ` René van Dorst
2019-10-11 14:14       ` Andy Polyakov
2019-10-11 17:21         ` René van Dorst
2019-10-11 18:49           ` Andy Polyakov
2019-10-11 21:38           ` Arnd Bergmann
2019-10-07 16:46 ` [PATCH v3 20/29] int128: move __uint128_t compiler test to Kconfig Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 21/29] crypto: BLAKE2s - generic C library implementation and selftest Ard Biesheuvel
2019-10-11  6:02   ` Eric Biggers
2019-10-11 16:45     ` Jason A. Donenfeld
2019-10-14 12:53       ` Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 22/29] crypto: BLAKE2s - x86_64 library implementation Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 23/29] crypto: Curve25519 - generic C library implementations and selftest Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 24/29] crypto: lib/curve25519 - work around Clang stack spilling issue Ard Biesheuvel
2019-10-14 14:13   ` Jason A. Donenfeld
2019-10-14 16:07     ` Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 25/29] crypto: Curve25519 - x86_64 library implementation Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 26/29] crypto: arm - import Bernstein and Schwabe's Curve25519 ARM implementation Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 27/29] crypto: arm/Curve25519 - wire up NEON implementation Ard Biesheuvel
2019-10-07 16:46 ` [PATCH v3 28/29] crypto: chacha20poly1305 - import construction and selftest from Zinc Ard Biesheuvel
2019-10-11  6:14   ` Eric Biggers
2019-10-07 16:46 ` [PATCH v3 29/29] crypto: lib/chacha20poly1305 - reimplement crypt_from_sg() routine Ard Biesheuvel
2019-10-14 14:33 ` [PATCH v3 00/29] crypto: crypto API library interfaces for WireGuard Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191011060028.GA23882@sol.localdomain \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=arnd@arndb.de \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=martin@strongswan.org \
    --cc=opensource@vdorst.com \
    --cc=sneves@dei.uc.pt \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.