xemu/crypto
Daniel P. Berrangé dcb80cd908 crypto: fix bogus error benchmarking pbkdf on fast machines
We're seeing periodic reports of errors like:

$ qemu-img create -f luks --object secret,data=123456,id=sec0 \
                  -o key-secret=sec0 luks-info.img 1M
  Formatting 'luks-info.img', fmt=luks size=1048576 key-secret=sec0
  qemu-img: luks-info.img: Unable to get accurate CPU usage

This error message comes from a recent attempt to workaround a
kernel bug with measuring rusage in long running processes:

  commit c72cab5ad9
  Author: Tiago Pasqualini <tiago.pasqualini@canonical.com>
  Date:   Wed Sep 4 20:52:30 2024 -0300

    crypto: run qcrypto_pbkdf2_count_iters in a new thread

Unfortunately this has a subtle bug on machines which are very fast.

On the first time around the loop, the 'iterations' value is quite
small (1 << 15), and so will run quite fast. Testing has shown that
some machines can complete this benchmarking task in as little as
7 milliseconds.

Unfortunately the 'getrusage' data is not updated at the time of
the 'getrusage' call, it is done asynchronously by the scheduler.
The 7 millisecond completion time for the benchmark is short
enough that 'getrusage' sometimes reports 0 accumulated execution
time.

As a result the 'delay_ms == 0' sanity check in the above commit
is triggering non-deterministically on such machines.

The benchmarking loop intended to run multiple times, increasing
the 'iterations' value until the benchmark ran for > 500 ms, but
the sanity check doesn't allow this to happen.

To fix it, we keep a loop counter and only run the sanity check
after we've been around the loop more than 5 times. At that point
the 'iterations' value is high enough that even with infrequent
updates of 'getrusage' accounting data on fast machines, we should
see a non-zero value.

Fixes: https://lore.kernel.org/qemu-devel/ffe542bb-310c-4616-b0ca-13182f849fd1@redhat.com/
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2336437
Reported-by: Thomas Huth <thuth@redhat.com>
Reported-by: Richard W.M. Jones <rjones@redhat.com>
Tested-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20250109093746.1216300-1-berrange@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
(cherry picked from commit 145f12ea885c8fcfbe2d0ac5230630f071b5a9fb)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2025-01-22 21:58:16 +03:00
..
aes.c target/riscv: Use existing lookup tables for MixColumns 2023-09-11 11:45:54 +10:00
afalg.c qapi/crypto: Rename QCryptoAFAlg to QCryptoAFAlgo 2024-09-10 14:03:30 +02:00
afalgpriv.h qapi/crypto: Rename QCryptoAFAlg to QCryptoAFAlgo 2024-09-10 14:03:30 +02:00
afsplit.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
akcipher-gcrypt.c.inc qapi/crypto: Rename QCryptoRSAPaddingAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
akcipher-nettle.c.inc qapi/crypto: Rename QCryptoRSAPaddingAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
akcipher.c qapi/crypto: Rename QCryptoAkCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
akcipherpriv.h qapi/crypto: Rename QCryptoAkCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
block-luks-priv.h crypto: Clean up includes 2023-02-08 07:16:23 +01:00
block-luks.c qapi/crypto: Rename QCryptoIVGenAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
block-luks.h crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
block-qcow.c qapi/crypto: Rename QCryptoIVGenAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
block-qcow.h crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
block.c qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
blockpriv.h qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipher-afalg.c qapi/crypto: Rename QCryptoAFAlg to QCryptoAFAlgo 2024-09-10 14:03:30 +02:00
cipher-builtin.c.inc qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipher-gcrypt.c.inc qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipher-gnutls.c.inc qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipher-nettle.c.inc qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipher.c qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
cipherpriv.h qapi/crypto: Rename QCryptoCipherAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
clmul.c crypto: Add generic 64-bit carry-less multiply routine 2023-09-15 13:57:00 +00:00
der.c crypto: Remove unused DER string functions 2024-10-22 11:44:23 +01:00
der.h crypto: Remove unused DER string functions 2024-10-22 11:44:23 +01:00
hash-afalg.c crypto/hash-afalg: Fix broken build 2024-10-22 11:44:23 +01:00
hash-gcrypt.c crypto: perform runtime check for hash/hmac support in gcrypt 2024-11-05 18:37:18 +00:00
hash-glib.c crypto/hash: avoid overwriting user supplied result pointer 2024-10-22 11:44:23 +01:00
hash-gnutls.c crypto/hash: avoid overwriting user supplied result pointer 2024-10-22 11:44:23 +01:00
hash-nettle.c crypto: Introduce SM3 hash hmac pbkdf algorithm 2024-11-05 18:37:18 +00:00
hash.c crypto: Introduce SM3 hash hmac pbkdf algorithm 2024-11-05 18:37:18 +00:00
hashpriv.h crypto/hashpriv: Remove old hash API function 2024-10-10 12:34:11 +01:00
hmac-gcrypt.c crypto: perform runtime check for hash/hmac support in gcrypt 2024-11-05 18:37:18 +00:00
hmac-glib.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
hmac-gnutls.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
hmac-nettle.c crypto: Introduce SM3 hash hmac pbkdf algorithm 2024-11-05 18:37:18 +00:00
hmac.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
hmacpriv.h qapi/crypto: Rename QCryptoAFAlg to QCryptoAFAlgo 2024-09-10 14:03:30 +02:00
init.c crypto: drop gnutls debug logging support 2024-07-24 10:39:10 +01:00
ivgen-essiv.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
ivgen-essiv.h crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
ivgen-plain.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
ivgen-plain.h Clean up ill-advised or unusual header guards 2022-05-11 16:50:01 +02:00
ivgen-plain64.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
ivgen-plain64.h crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
ivgen.c qapi/crypto: Rename QCryptoIVGenAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
ivgenpriv.h qapi/crypto: Rename QCryptoIVGenAlgorithm to *Algo, and drop prefix 2024-09-10 14:03:30 +02:00
meson.build crypto: Introduce x509 utils 2024-09-09 15:13:38 +01:00
pbkdf-gcrypt.c crypto: Introduce SM3 hash hmac pbkdf algorithm 2024-11-05 18:37:18 +00:00
pbkdf-gnutls.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
pbkdf-nettle.c crypto: Introduce SM3 hash hmac pbkdf algorithm 2024-11-05 18:37:18 +00:00
pbkdf-stub.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
pbkdf.c crypto: fix bogus error benchmarking pbkdf on fast machines 2025-01-22 21:58:16 +03:00
random-gcrypt.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
random-gnutls.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
random-none.c crypto: add "none" random provider 2020-06-15 11:33:50 +01:00
random-platform.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00
rsakey-builtin.c.inc qapi/crypto: Drop unwanted 'prefix' 2024-09-10 13:22:47 +02:00
rsakey-nettle.c.inc qapi/crypto: Drop unwanted 'prefix' 2024-09-10 13:22:47 +02:00
rsakey.c crypto: Support export akcipher to pkcs8 2022-11-02 06:56:32 -04:00
rsakey.h crypto: Support export akcipher to pkcs8 2022-11-02 06:56:32 -04:00
secret.c crypto: Move USER_CREATABLE to secret_common base class 2021-01-29 17:07:53 +00:00
secret_common.c crypto: purge 'loaded' property that was not fully removed 2024-11-05 18:37:15 +00:00
secret_keyring.c crypto: Move USER_CREATABLE to secret_common base class 2021-01-29 17:07:53 +00:00
sm4.c crypto: Add SM4 constant parameter CK 2023-09-11 11:45:55 +10:00
tls-cipher-suites.c crypto: remove shadowed 'ret' variable 2023-09-29 10:07:20 +02:00
tlscreds.c crypto/tlscreds: Introduce qcrypto_tls_creds_check_endpoint() helper 2021-06-29 18:29:43 +01:00
tlscredsanon.c crypto: purge 'loaded' property that was not fully removed 2024-11-05 18:37:15 +00:00
tlscredspriv.h crypto: Make QCryptoTLSCreds* structures private 2021-06-29 18:30:24 +01:00
tlscredspsk.c crypto: purge 'loaded' property that was not fully removed 2024-11-05 18:37:15 +00:00
tlscredsx509.c crypto: purge 'loaded' property that was not fully removed 2024-11-05 18:37:15 +00:00
tlssession.c crypto: propagate errors from TLS session I/O callbacks 2024-07-24 10:39:10 +01:00
trace-events docs: fix references to docs/devel/tracing.rst 2021-06-02 06:51:09 +02:00
trace.h trace: switch position of headers to what Meson requires 2020-08-21 06:18:24 -04:00
x509-utils.c qapi/crypto: Rename QCryptoHashAlgorithm to *Algo, and drop prefix 2024-09-10 14:02:16 +02:00
xts.c crypto: Fix LGPL information in the file headers 2019-07-19 14:21:25 +01:00