Skip to contentSkip to conversation

add crc64 calculation as kernel library

PATCH Superseded Coly Li 4 participants 15 messages Jul 18, 2018 – Jul 26, 2018

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Coly Li

author
#0 lore raw

v4 cover add crc64 calculation as kernel library

This patch set adds basic implementation of crc64 calculation as Linux kernel library. Since bcache already does crc64 by itself, this patch set also modifies bcache code to use the new crc64 library routine.

Currently bcache is the only user of crc64 calculation, another potential user is bcachefs which is on the way to be in mainline kernel. Therefore it makes sense to make crc64 calculation to be a public library.

bcache uses crc64 as storage checksum, if a change of crc lib routines results an inconsistent result, the unmatched checksum may make bcache 'think' the on-disk is corrupted, such change should be avoided or detected as early as possible. Therefore the last patch in this series adds a crc test framework, to check consistency of different calculations.

Coly Li

Cc Andy Shevchenko <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Michael Lyle <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>

---
Changelog:
v4: Only keep crc64_be() in lib/crc64.c, tested bcache specific stuffs
moved back to bcache code.
Fixes all review comments of v3.
v3: Remove little endian restriction and remove 'le' from function names.
Fixes all review comments of v2
v2: Combine first two patches into one
Fixes all review comments of v1
v1: Initial version.

Coly Li (3):
lib: add crc64 calculation routines
bcache: use routines from lib/crc64.c for CRC64 calculation lib/test_crc: Add test cases for crc calculation

 drivers/md/bcache/Kconfig |   1 +
 drivers/md/bcache/util.c  | 131 --------------------------------------
 drivers/md/bcache/util.h  |  21 ++++--
 include/linux/crc64.h     |  11 ++++
 lib/.gitignore            |   2 +
 lib/Kconfig               |   8 +++
 lib/Kconfig.debug         |  10 +++
 lib/Makefile              |  12 ++++
 lib/crc64.c               |  56 ++++++++++++++++
 lib/gen_crc64table.c      |  68 ++++++++++++++++++++
 lib/test_crc.c            |  93 +++++++++++++++++++++++++++
 11 files changed, 278 insertions(+), 135 deletions(-)
 create mode 100644 include/linux/crc64.h
 create mode 100644 lib/crc64.c
 create mode 100644 lib/gen_crc64table.c
 create mode 100644 lib/test_crc.c
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v4 1/3 lib: add crc64 calculation routines

This patch adds the re-write crc64 calculation routines for Linux kernel. The CRC64 polynomical arithmetic follows ECMA-182 specification, inspired by CRC paper of Dr. Ross N. Williams
(see http://www.ross.net/crc/download/crc_v3.txt) and other public domain implementations.

All the changes work in this way,
- When Linux kernel is built, host program lib/gen_crc64table.c will be compiled to lib/gen_crc64table and executed.
- The output of gen_crc64table execution is an array called as lookup table (a.k.a POLY 0x42f0e1eba9ea369) which contain 256 64-bit long numbers, this talbe is dumped into header file lib/crc64table.h.
- Then the header file is included by lib/crc64.c for normal 64bit crc calculation.
- Function declaration of the crc64 calculation routines is placed in include/linux/crc64.h

Currently bcache is the only user of crc64_be(), another potential user is bcachefs which is on the way to be in mainline kernel. Therefore it makes sense to move crc64 calculation into lib/crc64.c as public code.

Cc Greg Kroah-Hartman <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Michael Lyle <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>

---
Changelog:
v4: Only keep crc64_be() in lib/crc64.c, move rested bcache specific
stuffs back to bcache code.
Other fixes from the review comments of v3
v3: More fixes for review comments of v2
By review comments from Eric Biggers, current functions naming with 'le' is misleading. Remove 'le' from the crc function names, and use u64 to replace __le64 in parameters and return values.
v2: Fix reivew comments of v1
v1: Initial version.

 include/linux/crc64.h | 11 +++++++
 lib/.gitignore        |  2 ++
 lib/Kconfig           |  8 +++++
 lib/Makefile          | 11 +++++++
 lib/crc64.c           | 56 +++++++++++++++++++++++++++++++++++
 lib/gen_crc64table.c  | 68 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 156 insertions(+)
 create mode 100644 include/linux/crc64.h
 create mode 100644 lib/crc64.c
 create mode 100644 lib/gen_crc64table.c
include/linux/crc64.h new file +11 -0
new file mode 100644
index 000000000000..1fc9f0710c10
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * See lib/crc64.c for the related specification and polynomical arithmetic.
+ */
+#ifndef _LINUX_CRC64_H
+#define _LINUX_CRC64_H
+
+#include <linux/types.h>
+
+u64 __pure crc64_be(u64 crc, const void *p, size_t len);
+#endif /* _LINUX_CRC64_H */
lib/.gitignore +2 -0
index 09aae85418ab..f2a39c9e5485 100644
@@ -2,5 +2,7 @@
 # Generated files
 #
 gen_crc32table
+gen_crc64table
 crc32table.h
+crc64table.h
 oid_registry_data.c
lib/Kconfig +8 -0
index 706836ec314d..9c10b9852563 100644
@@ -170,6 +170,14 @@ config CRC32_BIT
 
 endchoice
 
+config CRC64
+	tristate "CRC64 functions"
+	help
+	  This option is provided for the case where no in-kernel-tree
+	  modules require CRC64 functions, but a module built outside
+	  the kernel tree does. Such modules that use library CRC64
+	  functions require M here.
+
 config CRC4
 	tristate "CRC4 functions"
 	help
lib/Makefile +11 -0
index 90dc5520b784..40c215181687 100644
@@ -102,6 +102,7 @@ obj-$(CONFIG_CRC16)	+= crc16.o
 obj-$(CONFIG_CRC_T10DIF)+= crc-t10dif.o
 obj-$(CONFIG_CRC_ITU_T)	+= crc-itu-t.o
 obj-$(CONFIG_CRC32)	+= crc32.o
+obj-$(CONFIG_CRC64)     += crc64.o
 obj-$(CONFIG_CRC32_SELFTEST)	+= crc32test.o
 obj-$(CONFIG_CRC4)	+= crc4.o
 obj-$(CONFIG_CRC7)	+= crc7.o
@@ -215,7 +216,9 @@ obj-$(CONFIG_FONT_SUPPORT) += fonts/
 obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
 
 hostprogs-y	:= gen_crc32table
+hostprogs-y	+= gen_crc64table
 clean-files	:= crc32table.h
+clean-files	+= crc64table.h
 
 $(obj)/crc32.o: $(obj)/crc32table.h
 
@@ -225,6 +228,14 @@ quiet_cmd_crc32 = GEN     $@
 $(obj)/crc32table.h: $(obj)/gen_crc32table
 	$(call cmd,crc32)
 
+$(obj)/crc64.o: $(obj)/crc64table.h
+
+quiet_cmd_crc64 = GEN     $@
+      cmd_crc64 = $< > $@
+
+$(obj)/crc64table.h: $(obj)/gen_crc64table
+	$(call cmd,crc64)
+
 #
 # Build a fast OID lookip registry from include/linux/oid_registry.h
 #
lib/crc64.c new file +56 -0

56 lines not on this page · raw message

lib/gen_crc64table.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.17.1
author
#8 lore raw

PATCH v4 2/3 bcache: use routines from lib/crc64.c for CRC64 calculation

In reply to Coly Li

Now we have crc64 calculation in lib/crc64.c, it is unnecessary for bcache to use it own version. This patch changes bcache code to use crc64 routines in lib/crc64.c.

Cc Michael Lyle <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>

---
Changelog:
v3: Only call crc64_be() from lib/crc64.c, and implemennt bch_crc64()

    and bch_crc64_update() based on crc64_be().

v2: Call crc64_le_bch(), crc64_le_update() from lib/crc64.c
v1: Initial version

 drivers/md/bcache/Kconfig |   1 +
 drivers/md/bcache/util.c  | 131 --------------------------------------
 drivers/md/bcache/util.h  |  21 ++++--
 3 files changed, 18 insertions(+), 135 deletions(-)
drivers/md/bcache/Kconfig +1 -0
index 17bf109c58e9..af247298409a 100644
@@ -1,6 +1,7 @@
 
 config BCACHE
 	tristate "Block device as cache"
+	select CRC64
 	---help---
 	Allows a block device to be used as cache for other devices; uses
 	a btree for indexing and the layout is optimized for SSDs.
drivers/md/bcache/util.c +0 -131

134 lines not on this page · raw message

drivers/md/bcache/util.h +17 -4
index cced87f8eb27..a1579e28049f 100644
@@ -11,6 +11,7 @@
 #include <linux/ratelimit.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
+#include <linux/crc64.h>
 
 #include "closure.h"
 
@@ -542,6 +543,22 @@ dup:									\
 #define RB_PREV(ptr, member)						\
 	container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
 
+static inline uint64_t bch_crc64(const void *p, size_t len)
+{
+	uint64_t crc = 0xffffffffffffffffULL;
+
+	crc = crc64_be(crc, p, len);
+	return crc ^ 0xffffffffffffffffULL;
+}
+
+static inline uint64_t bch_crc64_update(uint64_t crc,
+					const void *p,
+					size_t len)
+{
+	crc = crc64_be(crc, p, len);
+	return crc;
+}
+
 /* Does linear interpolation between powers of two */
 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
 {
@@ -561,8 +578,4 @@ static inline sector_t bdev_sectors(struct block_device *bdev)
 {
 	return bdev->bd_inode->i_size >> 9;
 }
-
-uint64_t bch_crc64_update(uint64_t, const void *, size_t);
-uint64_t bch_crc64(const void *, size_t);
-
 #endif /* _BCACHE_UTIL_H */
Signature · 1 line
2.17.1
author
#9 lore raw

PATCH v4 3/3 lib/test_crc: Add test cases for crc calculation

In reply to Coly Li

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Cc Greg Kroah-Hartman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Kate Stewart <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Noah Massey <[email protected]>

---
Changelog:
v4: Improve error statistic and failure message display by suggestion
from Andy Shevchenko and Noah Massey.
Fixes for review commennts of v3
v3: Add test cases passed/failed statistic
More fixes for review comments of v2
v2: Fixes for review comments of v1
v1: Initial version.

 lib/Kconfig.debug | 10 +++++
 lib/Makefile      |  1 +
 lib/test_crc.c    | 93 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
 create mode 100644 lib/test_crc.c
lib/Kconfig.debug +10 -0
index 8838d1158d19..a9c1de0c2a7d 100644
@@ -1911,6 +1911,16 @@ config TEST_SYSCTL
 
 	  If unsure, say N.
 
+config TEST_CRC
+	tristate "CRC calculation test driver"
+	depends on CRC64
+	help
+	  This builds the "test_crc" module. This driver enables to test the
+	  CRC calculation consistency to make sure new modification does not
+	  break existing checksum calculation.
+
+	  if unsure, say N.
+
 config TEST_UDELAY
 	tristate "udelay test driver"
 	default n
lib/Makefile +1 -0
index 40c215181687..224d047d026a 100644
@@ -49,6 +49,7 @@ obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
 obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
+obj-$(CONFIG_TEST_CRC) += test_crc.o
 obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
 obj-$(CONFIG_TEST_KASAN) += test_kasan.o
 CFLAGS_test_kasan.o += -fno-builtin
lib/test_crc.c new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#10 lore raw

Re: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation

Quoted 5 lines · On Thu, 19 Jul 2018 00:55:45 +0800 Coly Li <[email protected]> wrote:

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

Quoted 8 lines

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Some tweaks.

From: Andrew Morton <[email protected]>
Subject: lib-test_crc-add-test-cases-for-crc-calculation-fix

make two globals static, regularize code layout, remove unneeded initialization

---

 lib/test_crc.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
lib/test_crc.c renamed from lib/test_crc.c~lib-test_crc-add-test-cases-for-crc-calculation-fix +5 -6
diff -puN lib/test_crc.c~lib-test_crc-add-test-cases-for-crc-calculation-fix lib/test_crc.c
@@ -25,8 +25,8 @@ struct crc_test_record {
 	void	(*handler)(struct crc_test_record *rec);
 };
 
-int failed_tests;
-int total_tests;
+static int failed_tests;
+static int total_tests;
 
 static void chk_and_msg(const char *name, u64 crc, u64 expval)
 {
@@ -68,9 +68,6 @@ static int __init test_crc_init(void)
 {
 	int i;
 
-	failed_tests = 0;
-	total_tests = 0;
-
 	pr_info("Kernel CRC consistency testing:\n");
 	for (i = 0; test_data[i].name; i++)
 		test_data[i].handler(&test_data[i]);
@@ -85,7 +82,9 @@ static int __init test_crc_init(void)
 }
 late_initcall(test_crc_init);
 
-static void __exit test_crc_exit(void) { }
+static void __exit test_crc_exit(void)
+{
+}
 module_exit(test_crc_exit);
 
 MODULE_DESCRIPTION("CRC consistency testing driver");

_

#2 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

In reply to Coly Li
Quoted 2 lines · Coly Li

This patch adds the re-write crc64 calculation routines for Linux kernel. The CRC64 polynomical arithmetic follows ECMA-182 specification, inspired

"polynomical" => "polynomial". Same everywhere else in the patches.

Quoted 1 line

+ * crc64table[256] is the lookup table of a table-driver 64-bit CRC

"table-driver" => "table-driven"

Quoted 14 lines

+ * calculation, which is generated by gen_crc64table.c in kernel build
+ * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
+ * as well, which is defined as,
+ *
+ * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
+ * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
+ * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
+ * x^7 + x^4 + x + 1
+ *
+ * Copyright 2018 SUSE Linux.
+ * Author: Coly Li <[email protected]>
+ */

lib/crc64.c new file +2 -0
+
+#include <linux/module.h>

lib/crc64.c line 39

Quoted 10 lines
lib/crc64.c new file +10 -0
+#include <linux/types.h>
+#include "crc64table.h"
+
+MODULE_DESCRIPTION("CRC64 calculations");
+MODULE_LICENSE("GPL v2");
+
+/**
+ * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
+ * @crc: seed value for computation. 0 for a new CRC computing, or the
+ *	previous crc64 value if computing incrementally.

"0 or (u64)~0 for a new CRC calculation". Both conventions are common in CRCs. In fact the all-ones convention is generally considered better, because it causes leading zeroes in the data to affect the checksum.

Quoted 23 lines

+ * @p: pointer to buffer over which CRC64 is run
+ * @len: length of buffer @p
+ */

lib/crc64.c new file +14 -0
+u64 __pure crc64_be(u64 crc, const void *p, size_t len)
+{
+	size_t i, t;
+
+	const unsigned char *_p = p;
+
+	for (i = 0; i < len; i++) {
+		t = ((crc >> 56) ^ (*_p++)) & 0xFF;
+		crc = crc64table[t] ^ (crc << 8);
+	}
+
+	return crc;
+}
+EXPORT_SYMBOL_GPL(crc64_be);
lib/gen_crc64table.c new file +0 -0
new file mode 100644
index 000000000000..8296b0c3ea30

lib/gen_crc64table.c line 3

Quoted 3 lines
lib/gen_crc64table.c new file +3 -0
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generate lookup table for the talbe-driven CRC64 calculation.

"talbe-driven" => "table-driven"

lib/gen_crc64table.c line 7

Quoted 4 lines
lib/gen_crc64table.c new file +4 -0
+ *
+ * gen_crc64table is executed in kernel build time and generates
+ * lib/crc64table.h. This header is included by lib/crc64.c for
+ * the table-driver CRC64 calculation.

"table-driver" => "table-driven"

Quoted 6 lines
lib/gen_crc64table.c new file +6 -0
+ *
+ * See lib/crc64.c for more information about which specification
+ * and polynomical arithmetic that gen_crc64table.c follows to
+ * generate the lookup table.
+ *
+ * Copyright 2018 SUSE Linux.

lib/gen_crc64table.c line 23

Quoted 10 lines
lib/gen_crc64table.c new file +10 -0
+ *   Author: Coly Li <[email protected]>
+ */
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <linux/swab.h>
+
+#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
+
+static int64_t crc64_table[256] = {0};

This really should be uint64_t, not int64_t.

Quoted 48 lines
lib/gen_crc64table.c new file +45 -0
+
+static void generate_crc64_table(void)
+{
+	uint64_t i, j, c, crc;
+
+	for (i = 0; i < 256; i++) {
+		crc = 0;
+		c = i << 56;
+
+		for (j = 0; j < 8; j++) {
+			if ((crc ^ c) & 0x8000000000000000ULL)
+				crc = (crc << 1) ^ CRC64_ECMA182_POLY;
+			else
+				crc <<= 1;
+			c <<= 1;
+		}
+
+		crc64_table[i] = crc;
+	}
+}
+
+static void print_crc64_table(void)
+{
+	int i;
+
+	printf("/* this file is generated - do not edit */\n\n");
+	printf("#include <linux/types.h>\n");
+	printf("#include <linux/cache.h>\n\n");
+	printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
+	for (i = 0; i < 256; i++) {
+		printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);
+		if (i & 0x1)
+			printf(",\n");
+		else
+			printf(", ");
+	}
+	printf("};\n");
+}
+
+int main(int argc, char *argv[])
+{
+	generate_crc64_table();
+	print_crc64_table();
+	return 0;
+}
Signature · 1 line
2.17.1

- Eric

#11 lore raw

Re: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation

In reply to Coly Li
Quoted 5 lines · Coly Li

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

Quoted 8 lines

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Are you aware there's already a CRC-32 test module: CONFIG_CRC32_SELFTEST and lib/crc32test.c? Confusingly, your patch uses a different naming convention for the new CRC-64 one, and puts the Kconfig option in a different place, and makes it sound like it's a generic test for all CRC implementations rather than just the CRC-64 one. Please use the existing convention (i.e. add CONFIG_CRC64_SELFTEST and lib/crc64test.c) unless you have a strong argument for why it should be done differently.

(And I don't think it makes sense to combine all CRC tests into one module, since you should be able to e.g. enable just CRC32 and CRC32_SELFTEST without also pulling in a dependency on all the other CRC variants.)

Quoted 14 lines
lib/test_crc.c new file +14 -0
+/* Add your crc test cases here */
+static void test_crc64_be(struct crc_test_record *rec)
+{
+	u64 crc;
+
+	crc = crc64_be(rec->initval, rec->data, sizeof(rec->data));
+	chk_and_msg(rec->name, crc, rec->expval);
+}
+
+/*
+ * Set up your crc test initial data here.
+ * Do not change the existing items, they are hard coded with
+ * pre-calculated values.
+ */

lib/test_crc.c line 65

Quoted 10 lines
lib/test_crc.c new file +10 -0
+static struct crc_test_record test_data[] = {
+	{ .name		= "crc64_be",
+	  .data		= { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26,
+			    0xC711223CFA3E5BB5, 0x493366450E42ECDF },
+	  .initval	= 0x61C8864680B583EB,
+	  .expval	= 0xb2c863673f4292bf,
+	  .handler	= test_crc64_be,
+	},
+	{}
+};

This is incorrect; the test is checksumming data that has a CPU-specific endianness. So, it will fail on big-endian systems. The data needs to be declared as a byte or char array instead. See e.g. what crypto/testmgr.h does for crypto API algorithms.

Also please mark the test data structures 'const'.

- Eric

author
#12 lore raw

Re: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation

Quoted 23 lines · Eric Biggers
Quoted 5 lines · Coly Li

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

Quoted 8 lines

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Are you aware there's already a CRC-32 test module: CONFIG_CRC32_SELFTEST and lib/crc32test.c? Confusingly, your patch uses a different naming convention for the new CRC-64 one, and puts the Kconfig option in a different place, and makes it sound like it's a generic test for all CRC implementations rather than just the CRC-64 one. Please use the existing convention (i.e. add CONFIG_CRC64_SELFTEST and lib/crc64test.c) unless you have a strong argument for why it should be done differently.

Quoted 4 lines

(And I don't think it makes sense to combine all CRC tests into one module, since you should be able to e.g. enable just CRC32 and CRC32_SELFTEST without also pulling in a dependency on all the other CRC variants.)

Hi Eric,

The purpose of test_crc is to provide a unified crc calculation consistency testing for 0day. So far it is only crc64, and I will add more test cases later. I see there is crc-32 test module, which does more testing then consistency check, and no unified format for 0day system to detect. This is why people suggested me to add this test framework.

Quoted 30 lines
Quoted 14 lines
lib/test_crc.c new file +14 -0
+/* Add your crc test cases here */
+static void test_crc64_be(struct crc_test_record *rec)
+{
+	u64 crc;
+
+	crc = crc64_be(rec->initval, rec->data, sizeof(rec->data));
+	chk_and_msg(rec->name, crc, rec->expval);
+}
+
+/*
+ * Set up your crc test initial data here.
+ * Do not change the existing items, they are hard coded with
+ * pre-calculated values.
+ */

lib/test_crc.c line 65

Quoted 10 lines
lib/test_crc.c new file +10 -0
+static struct crc_test_record test_data[] = {
+	{ .name		= "crc64_be",
+	  .data		= { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26,
+			    0xC711223CFA3E5BB5, 0x493366450E42ECDF },
+	  .initval	= 0x61C8864680B583EB,
+	  .expval	= 0xb2c863673f4292bf,
+	  .handler	= test_crc64_be,
+	},
+	{}
+};

This is incorrect; the test is checksumming data that has a CPU-specific endianness. So, it will fail on big-endian systems. The data needs to be declared as a byte or char array instead. See e.g. what crypto/testmgr.h does for crypto API algorithms.

Quoted 1 line

Also please mark the test data structures 'const'.

Sure, I will send fix patches (not rebase the posted patches because they are in linux-next for now) soon.

Thanks for your comments.

Coly Li

author
#3 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

In reply to Eric Biggers
Quoted 6 lines · Eric Biggers
Quoted 2 lines · Coly Li

This patch adds the re-write crc64 calculation routines for Linux kernel. The CRC64 polynomical arithmetic follows ECMA-182 specification, inspired

"polynomical" => "polynomial". Same everywhere else in the patches.

Hi Eric,

Thanks for your careful review. I will post a fixing patch to fix all these issues.

Coly Li

Quoted 139 lines
Quoted 1 line

+ * crc64table[256] is the lookup table of a table-driver 64-bit CRC

"table-driver" => "table-driven"

Quoted 14 lines

+ * calculation, which is generated by gen_crc64table.c in kernel build
+ * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
+ * as well, which is defined as,
+ *
+ * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
+ * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
+ * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
+ * x^7 + x^4 + x + 1
+ *
+ * Copyright 2018 SUSE Linux.
+ * Author: Coly Li <[email protected]>
+ */

lib/crc64.c new file +2 -0
+
+#include <linux/module.h>

lib/crc64.c line 39

Quoted 10 lines
lib/crc64.c new file +10 -0
+#include <linux/types.h>
+#include "crc64table.h"
+
+MODULE_DESCRIPTION("CRC64 calculations");
+MODULE_LICENSE("GPL v2");
+
+/**
+ * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
+ * @crc: seed value for computation. 0 for a new CRC computing, or the
+ *	previous crc64 value if computing incrementally.

"0 or (u64)~0 for a new CRC calculation". Both conventions are common in CRCs. In fact the all-ones convention is generally considered better, because it causes leading zeroes in the data to affect the checksum.

Quoted 23 lines

+ * @p: pointer to buffer over which CRC64 is run
+ * @len: length of buffer @p
+ */

lib/crc64.c new file +14 -0
+u64 __pure crc64_be(u64 crc, const void *p, size_t len)
+{
+	size_t i, t;
+
+	const unsigned char *_p = p;
+
+	for (i = 0; i < len; i++) {
+		t = ((crc >> 56) ^ (*_p++)) & 0xFF;
+		crc = crc64table[t] ^ (crc << 8);
+	}
+
+	return crc;
+}
+EXPORT_SYMBOL_GPL(crc64_be);
lib/gen_crc64table.c new file +0 -0
new file mode 100644
index 000000000000..8296b0c3ea30

lib/gen_crc64table.c line 3

Quoted 3 lines
lib/gen_crc64table.c new file +3 -0
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generate lookup table for the talbe-driven CRC64 calculation.

"talbe-driven" => "table-driven"

lib/gen_crc64table.c line 7

Quoted 4 lines
lib/gen_crc64table.c new file +4 -0
+ *
+ * gen_crc64table is executed in kernel build time and generates
+ * lib/crc64table.h. This header is included by lib/crc64.c for
+ * the table-driver CRC64 calculation.

"table-driver" => "table-driven"

Quoted 6 lines
lib/gen_crc64table.c new file +6 -0
+ *
+ * See lib/crc64.c for more information about which specification
+ * and polynomical arithmetic that gen_crc64table.c follows to
+ * generate the lookup table.
+ *
+ * Copyright 2018 SUSE Linux.

lib/gen_crc64table.c line 23

Quoted 10 lines
lib/gen_crc64table.c new file +10 -0
+ *   Author: Coly Li <[email protected]>
+ */
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <linux/swab.h>
+
+#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
+
+static int64_t crc64_table[256] = {0};

This really should be uint64_t, not int64_t.

Quoted 48 lines
lib/gen_crc64table.c new file +45 -0
+
+static void generate_crc64_table(void)
+{
+	uint64_t i, j, c, crc;
+
+	for (i = 0; i < 256; i++) {
+		crc = 0;
+		c = i << 56;
+
+		for (j = 0; j < 8; j++) {
+			if ((crc ^ c) & 0x8000000000000000ULL)
+				crc = (crc << 1) ^ CRC64_ECMA182_POLY;
+			else
+				crc <<= 1;
+			c <<= 1;
+		}
+
+		crc64_table[i] = crc;
+	}
+}
+
+static void print_crc64_table(void)
+{
+	int i;
+
+	printf("/* this file is generated - do not edit */\n\n");
+	printf("#include <linux/types.h>\n");
+	printf("#include <linux/cache.h>\n\n");
+	printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
+	for (i = 0; i < 256; i++) {
+		printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);
+		if (i & 0x1)
+			printf(",\n");
+		else
+			printf(", ");
+	}
+	printf("};\n");
+}
+
+int main(int argc, char *argv[])
+{
+	generate_crc64_table();
+	print_crc64_table();
+	return 0;
+}
Signature · 1 line
2.17.1

- Eric

#4 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

Quoted 7 lines · Coly Li
Quoted 6 lines · Eric Biggers
Quoted 4 lines · Coly Li

This patch adds the re-write crc64 calculation routines for Linux kernel.
The CRC64 polynomical arithmetic follows ECMA-182 specification, inspired

Quoted 7 lines
Quoted 2 lines

"polynomical" => "polynomial". Same everywhere else in the patches.

Hi Eric,

Thanks for your careful review. I will post a fixing patch to fix all these issues.

IIUC this series in Andrew's quilt. So, it's still possible to replace it with better one, I suppose. Ask Andrew (at least I remember recent case where he just replaced series by fixed one).

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#13 lore raw

Re: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation

In reply to Coly Li
Quoted 29 lines · Coly Li
Quoted 23 lines · Eric Biggers
Quoted 5 lines · Coly Li

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

Quoted 8 lines

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Are you aware there's already a CRC-32 test module: CONFIG_CRC32_SELFTEST and lib/crc32test.c? Confusingly, your patch uses a different naming convention for the new CRC-64 one, and puts the Kconfig option in a different place, and makes it sound like it's a generic test for all CRC implementations rather than just the CRC-64 one. Please use the existing convention (i.e. add CONFIG_CRC64_SELFTEST and lib/crc64test.c) unless you have a strong argument for why it should be done differently.

Quoted 4 lines

(And I don't think it makes sense to combine all CRC tests into one module, since you should be able to e.g. enable just CRC32 and CRC32_SELFTEST without also pulling in a dependency on all the other CRC variants.)

Quoted 9 lines

Hi Eric,

The purpose of test_crc is to provide a unified crc calculation consistency testing for 0day. So far it is only crc64, and I will add more test cases later. I see there is crc-32 test module, which does more testing then consistency check, and no unified format for 0day system to detect. This is why people suggested me to add this test framework.

Actually the code in crc32test is nearly the same as what you're adding for CRC-64. The CRC-32 test is longer because it's testing two different polynomials "crc32" and "crc32c" as well as combining CRC's; neither of those is relevant for CRC-64 yet, as you've implemented just one polynomial and there is no function provided to combine CRC64's yet. The CRC-32 test also tests performance, but if you don't believe CRC performance should be tested, then you should remove the performance test from the existing module rather than implementing a brand new test module just to remove the performance test...

I still don't understand why you decided to do things differently for CRC-64, when there were already CRC-32 tests that used a certain convention for the Kconfig option, filename, etc. It's inconsistent and confusing. Again, please use the existing convention unless you have a strong argument for why it should be done differently. (And if you do want to do things differently, the existing test should be converted first.)

- Eric

author
#5 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Coly Li
Quoted 8 lines · Eric Biggers
Quoted 4 lines · Coly Li

This patch adds the re-write crc64 calculation routines for Linux kernel.
The CRC64 polynomical arithmetic follows ECMA-182 specification, inspired

"polynomical" => "polynomial". Same everywhere else in the patches.

Quoted 8 lines
Quoted 4 lines

Hi Eric,

Thanks for your careful review. I will post a fixing patch to fix all these issues.

IIUC this series in Andrew's quilt. So, it's still possible to replace it with better one, I suppose. Ask Andrew (at least I remember recent case where he just replaced series by fixed one).

Hi Andrew,

It seems there should be some fix/update for this series. I see current series is in linux-next for now. If I want to add the fix, should I post a new version of the series, or the incremental patches to fix current series ?

Thanks in advance for your advice.

Coly Li

author
#14 lore raw

Re: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation

In reply to Eric Biggers
Quoted 49 lines · Eric Biggers
Quoted 29 lines · Coly Li
Quoted 23 lines · Eric Biggers
Quoted 5 lines · Coly Li

This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled.

The test results are printed into kernel message, which look like,

Quoted 8 lines

test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f)

kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon.

lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be().

Are you aware there's already a CRC-32 test module: CONFIG_CRC32_SELFTEST and lib/crc32test.c? Confusingly, your patch uses a different naming convention for the new CRC-64 one, and puts the Kconfig option in a different place, and makes it sound like it's a generic test for all CRC implementations rather than just the CRC-64 one. Please use the existing convention (i.e. add CONFIG_CRC64_SELFTEST and lib/crc64test.c) unless you have a strong argument for why it should be done differently.

Quoted 4 lines

(And I don't think it makes sense to combine all CRC tests into one module, since you should be able to e.g. enable just CRC32 and CRC32_SELFTEST without also pulling in a dependency on all the other CRC variants.)

Quoted 9 lines

Hi Eric,

The purpose of test_crc is to provide a unified crc calculation consistency testing for 0day. So far it is only crc64, and I will add more test cases later. I see there is crc-32 test module, which does more testing then consistency check, and no unified format for 0day system to detect. This is why people suggested me to add this test framework.

Actually the code in crc32test is nearly the same as what you're adding for CRC-64. The CRC-32 test is longer because it's testing two different polynomials "crc32" and "crc32c" as well as combining CRC's; neither of those is relevant for CRC-64 yet, as you've implemented just one polynomial and there is no function provided to combine CRC64's yet. The CRC-32 test also tests performance, but if you don't believe CRC performance should be tested, then you should remove the performance test from the existing module rather than implementing a brand new test module just to remove the performance test...

Quoted 6 lines

I still don't understand why you decided to do things differently for CRC-64, when there were already CRC-32 tests that used a certain convention for the Kconfig option, filename, etc. It's inconsistent and confusing. Again, please use the existing convention unless you have a strong argument for why it should be done differently. (And if you do want to do things differently, the existing test should be converted first.)

Hi Eric,

So far only crc32 has selftesting code, it is hardly to be a convention IMHO. Anyway, considerate your comments and suggestion, I feel crc_test can be a separate patch from this series.

Later I will post another series, which unify all crc test together into test_crc, including other crc calculations which don't have their testing code.

Thanks.

Coly Li

maintainer
#6 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

In reply to Coly Li
Quoted 10 lines · On Wed, 25 Jul 2018 10:37:23 +0800 Coly Li <[email protected]> wrote:
Quoted 3 lines

IIUC this series in Andrew's quilt. So, it's still possible to replace it with better one, I suppose. Ask Andrew (at least I remember recent case where he just replaced series by fixed one).

Hi Andrew,

It seems there should be some fix/update for this series. I see current series is in linux-next for now. If I want to add the fix, should I post a new version of the series, or the incremental patches to fix current series ?

mm... it sounds like the changes will be large, so probably a new series would be better at this early stage. If the changes are small then little fixup patches would be nicer to the people who have already reviewed v1.

author
#7 lore raw

Re: [PATCH v4 1/3] lib: add crc64 calculation routines

Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Wed, 25 Jul 2018 10:37:23 +0800 Coly Li <[email protected]> wrote:
Quoted 3 lines

IIUC this series in Andrew's quilt. So, it's still possible to replace it with better one, I suppose. Ask Andrew (at least I remember recent case where he just replaced series by fixed one).

Hi Andrew,

It seems there should be some fix/update for this series. I see current series is in linux-next for now. If I want to add the fix, should I post a new version of the series, or the incremental patches to fix current series ?

Quoted 5 lines

mm... it sounds like the changes will be large, so probably a new series would be better at this early stage. If the changes are small then little fixup patches would be nicer to the people who have already reviewed v1.

Yes, it is large (patch 3/3 will be removed). So I will post a new series to replace current series.

Thanks for the hint.

Coly Li