diff mbox series

[v2,04/11] lib/crc_kunit.c: add test and benchmark for CRC64-NVME

Message ID 20250130035130.180676-5-ebiggers@kernel.org (mailing list archive)
State New
Headers show
Series CRC64 library rework and x86 CRC optimization | expand

Commit Message

Eric Biggers Jan. 30, 2025, 3:51 a.m. UTC
From: Eric Biggers <ebiggers@google.com>

Wire up crc64_nvme() to the new CRC unit test and benchmark.

This replaces and improves on the test coverage that was lost by
removing this CRC variant from the crypto API.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 lib/crc_kunit.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

Comments

Ard Biesheuvel Feb. 2, 2025, 2:29 p.m. UTC | #1
On Thu, 30 Jan 2025 at 04:54, Eric Biggers <ebiggers@kernel.org> wrote:
>
> From: Eric Biggers <ebiggers@google.com>
>
> Wire up crc64_nvme() to the new CRC unit test and benchmark.
>
> This replaces and improves on the test coverage that was lost by
> removing this CRC variant from the crypto API.
>
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  lib/crc_kunit.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
>

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>

> diff --git a/lib/crc_kunit.c b/lib/crc_kunit.c
> index 6a61d4b5fd45..1e82fcf9489e 100644
> --- a/lib/crc_kunit.c
> +++ b/lib/crc_kunit.c
> @@ -30,11 +30,12 @@ static size_t test_buflen;
>   *     polynomial coefficients in each byte), false if it's a "big endian" CRC
>   *     (natural mapping between bits and polynomial coefficients in each byte)
>   * @poly: The generator polynomial with the highest-order term omitted.
>   *       Bit-reversed if @le is true.
>   * @func: The function to compute a CRC.  The type signature uses u64 so that it
> - *       can fit any CRC up to CRC-64.
> + *       can fit any CRC up to CRC-64.  The function is expected to *not*
> + *       invert the CRC at the beginning and end.
>   * @combine_func: Optional function to combine two CRCs.
>   */
>  struct crc_variant {
>         int bits;
>         bool le;
> @@ -405,10 +406,35 @@ static void crc64_be_test(struct kunit *test)
>  static void crc64_be_benchmark(struct kunit *test)
>  {
>         crc_benchmark(test, crc64_be_wrapper);
>  }
>
> +/* crc64_nvme */
> +
> +static u64 crc64_nvme_wrapper(u64 crc, const u8 *p, size_t len)
> +{
> +       /* The inversions that crc64_nvme() does have to be undone here. */
> +       return ~crc64_nvme(~crc, p, len);
> +}
> +
> +static const struct crc_variant crc_variant_crc64_nvme = {
> +       .bits = 64,
> +       .le = true,
> +       .poly = 0x9a6c9329ac4bc9b5,
> +       .func = crc64_nvme_wrapper,
> +};
> +
> +static void crc64_nvme_test(struct kunit *test)
> +{
> +       crc_test(test, &crc_variant_crc64_nvme);
> +}
> +
> +static void crc64_nvme_benchmark(struct kunit *test)
> +{
> +       crc_benchmark(test, crc64_nvme_wrapper);
> +}
> +
>  static struct kunit_case crc_test_cases[] = {
>         KUNIT_CASE(crc16_test),
>         KUNIT_CASE(crc16_benchmark),
>         KUNIT_CASE(crc_t10dif_test),
>         KUNIT_CASE(crc_t10dif_benchmark),
> @@ -418,10 +444,12 @@ static struct kunit_case crc_test_cases[] = {
>         KUNIT_CASE(crc32_be_benchmark),
>         KUNIT_CASE(crc32c_test),
>         KUNIT_CASE(crc32c_benchmark),
>         KUNIT_CASE(crc64_be_test),
>         KUNIT_CASE(crc64_be_benchmark),
> +       KUNIT_CASE(crc64_nvme_test),
> +       KUNIT_CASE(crc64_nvme_benchmark),
>         {},
>  };
>
>  static struct kunit_suite crc_test_suite = {
>         .name = "crc",
> --
> 2.48.1
>
diff mbox series

Patch

diff --git a/lib/crc_kunit.c b/lib/crc_kunit.c
index 6a61d4b5fd45..1e82fcf9489e 100644
--- a/lib/crc_kunit.c
+++ b/lib/crc_kunit.c
@@ -30,11 +30,12 @@  static size_t test_buflen;
  *	polynomial coefficients in each byte), false if it's a "big endian" CRC
  *	(natural mapping between bits and polynomial coefficients in each byte)
  * @poly: The generator polynomial with the highest-order term omitted.
  *	  Bit-reversed if @le is true.
  * @func: The function to compute a CRC.  The type signature uses u64 so that it
- *	  can fit any CRC up to CRC-64.
+ *	  can fit any CRC up to CRC-64.  The function is expected to *not*
+ *	  invert the CRC at the beginning and end.
  * @combine_func: Optional function to combine two CRCs.
  */
 struct crc_variant {
 	int bits;
 	bool le;
@@ -405,10 +406,35 @@  static void crc64_be_test(struct kunit *test)
 static void crc64_be_benchmark(struct kunit *test)
 {
 	crc_benchmark(test, crc64_be_wrapper);
 }
 
+/* crc64_nvme */
+
+static u64 crc64_nvme_wrapper(u64 crc, const u8 *p, size_t len)
+{
+	/* The inversions that crc64_nvme() does have to be undone here. */
+	return ~crc64_nvme(~crc, p, len);
+}
+
+static const struct crc_variant crc_variant_crc64_nvme = {
+	.bits = 64,
+	.le = true,
+	.poly = 0x9a6c9329ac4bc9b5,
+	.func = crc64_nvme_wrapper,
+};
+
+static void crc64_nvme_test(struct kunit *test)
+{
+	crc_test(test, &crc_variant_crc64_nvme);
+}
+
+static void crc64_nvme_benchmark(struct kunit *test)
+{
+	crc_benchmark(test, crc64_nvme_wrapper);
+}
+
 static struct kunit_case crc_test_cases[] = {
 	KUNIT_CASE(crc16_test),
 	KUNIT_CASE(crc16_benchmark),
 	KUNIT_CASE(crc_t10dif_test),
 	KUNIT_CASE(crc_t10dif_benchmark),
@@ -418,10 +444,12 @@  static struct kunit_case crc_test_cases[] = {
 	KUNIT_CASE(crc32_be_benchmark),
 	KUNIT_CASE(crc32c_test),
 	KUNIT_CASE(crc32c_benchmark),
 	KUNIT_CASE(crc64_be_test),
 	KUNIT_CASE(crc64_be_benchmark),
+	KUNIT_CASE(crc64_nvme_test),
+	KUNIT_CASE(crc64_nvme_benchmark),
 	{},
 };
 
 static struct kunit_suite crc_test_suite = {
 	.name = "crc",