public inbox for linux-crypto@vger.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Linux Crypto Mailing List <linux-crypto@vger.kernel.org>
Subject: Re: [PATCH 2/10] crypto: aead - Count error stats differently
Date: Wed, 15 Feb 2023 21:35:21 -0800	[thread overview]
Message-ID: <Y+3AmZaoNJBO4xU0@sol.localdomain> (raw)
In-Reply-To: <E1pSE2H-00BVkZ-8X@formenos.hmeau.com>

On Wed, Feb 15, 2023 at 05:25:09PM +0800, Herbert Xu wrote:
>  int crypto_aead_encrypt(struct aead_request *req)
>  {
>  	struct crypto_aead *aead = crypto_aead_reqtfm(req);
> -	struct crypto_alg *alg = aead->base.__crt_alg;
> +	struct aead_alg *alg = crypto_aead_alg(aead);
>  	unsigned int cryptlen = req->cryptlen;

The cryptlen local variable is no longer needed.  Just use req->cryptlen below.

> +	struct crypto_istat_aead *istat;
>  	int ret;
>  
> -	crypto_stats_get(alg);
> +	istat = aead_get_stat(alg);
> +
> +	if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
> +		atomic64_inc(&istat->encrypt_cnt);
> +		atomic64_add(cryptlen, &istat->encrypt_tlen);
> +	}
> +

This could just check whether istat is NULL:

	istat = aead_get_stat(alg);
	if (istat) {
		atomic64_inc(&istat->encrypt_cnt);
		atomic64_add(req->cryptlen, &istat->encrypt_tlen);
	}

That's simpler, and it makes it clearer that the pointer is not dereferenced
when it is NULL.

Note that aead_get_stat() is an inline function, so the stats code will still be
optimized out when !CONFIG_CRYPTO_STATS.

> +static inline int crypto_aead_errstat(struct crypto_istat_aead *istat, int err)
> +{
> +	if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
> +		return err;
> +
> +	if (err && err != -EINPROGRESS && err != -EBUSY)
> +		atomic64_inc(&istat->err_cnt);
> +
> +	return err;
> +}
> +
[...]
>  	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
>  		ret = -ENOKEY;
>  	else
> -		ret = crypto_aead_alg(aead)->encrypt(req);
> -	crypto_stats_aead_encrypt(cryptlen, alg, ret);
> -	return ret;
> +		ret = alg->encrypt(req);
> +
> +	return crypto_aead_errstat(istat, ret);

Similarly, istat != NULL could be used instead of CONFIG_CRYPTO_STATS.

IMO, this would also be easier to read if the stats increment was just coded
directly, like it is above, without the crypto_aead_errstat() function:

	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
		ret = -ENOKEY;
	else
		ret = alg->encrypt(req);

	if (istat && ret && ret != -EINPROGRESS && ret != -EBUSY)
		atomic64_inc(&istat->err_cnt);

	return ret;

Similarly for all the other algorithm types.

- Eric

  reply	other threads:[~2023-02-16  5:35 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-15  9:24 [PATCH 0/10] crypto: api - Restructure stats code Herbert Xu
2023-02-15  9:25 ` [PATCH 1/10] crypto: algapi - Move stat reporting into algapi Herbert Xu
2023-02-15  9:25 ` [PATCH 2/10] crypto: aead - Count error stats differently Herbert Xu
2023-02-16  5:35   ` Eric Biggers [this message]
2023-02-16  5:58     ` Herbert Xu
2023-02-16  5:45   ` Eric Biggers
2023-02-16  6:00     ` Herbert Xu
2023-02-16  6:31       ` Eric Biggers
2023-02-16  8:34         ` Herbert Xu
2023-02-15  9:25 ` [PATCH 3/10] crypto: akcipher " Herbert Xu
2023-02-15  9:25 ` [PATCH 4/10] crypto: hash " Herbert Xu
2023-02-16  5:58   ` Eric Biggers
2023-02-16  6:02     ` Herbert Xu
2023-02-16  6:14       ` Eric Biggers
2023-02-16  8:09         ` Herbert Xu
2023-02-15  9:25 ` [PATCH 5/10] crypto: acomp " Herbert Xu
2023-02-15  9:25 ` [PATCH 6/10] crypto: kpp " Herbert Xu
2023-02-15  9:25 ` [PATCH 7/10] crypto: skcipher " Herbert Xu
2023-02-15  9:25 ` [PATCH 8/10] crypto: rng " Herbert Xu
2023-02-16  5:46   ` Eric Biggers
2023-02-16  5:56     ` Herbert Xu
2023-02-16  6:01       ` Eric Biggers
2023-02-15  9:25 ` [PATCH 9/10] crypto: api - Move MODULE_ALIAS_CRYPTO to algapi.h Herbert Xu
2023-02-16  5:17   ` Eric Biggers
2023-02-16  5:18     ` Herbert Xu
2023-02-15  9:25 ` [PATCH 10/10] crypto: api - Check CRYPTO_USER instead of NET for report Herbert Xu

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=Y+3AmZaoNJBO4xU0@sol.localdomain \
    --to=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox