add crc64 calculation as kernel library
Subsystems (4)
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.
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.
Changelog:
v2: combine first two patches into one
fixes all review commennts of v1
v1: initial version.
Coly Li
---
Andy Shevchenko (1):
lib/crc64: add crc64 option to lib/Kconfig
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/bcache.h | 7 +- drivers/md/bcache/btree.c | 2 +- drivers/md/bcache/request.c | 2 +- drivers/md/bcache/super.c | 5 +- drivers/md/bcache/util.c | 131 ---------------------------------- drivers/md/bcache/util.h | 5 +- include/linux/crc64.h | 15 ++++ lib/.gitignore | 2 + lib/Kconfig | 8 +++ lib/Kconfig.debug | 11 +++ lib/Makefile | 12 ++++ lib/crc64.c | 71 +++++++++++++++++++ lib/gen_crc64table.c | 76 ++++++++++++++++++++ lib/test_crc.c | 136 ++++++++++++++++++++++++++++++++++++ 14 files changed, 341 insertions(+), 142 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
PATCH v2 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 64bits-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
--- include/linux/crc64.h | 15 +++++++++ lib/.gitignore | 2 ++ lib/Kconfig | 8 +++++ lib/Makefile | 11 +++++++ lib/crc64.c | 71 +++++++++++++++++++++++++++++++++++++++ lib/gen_crc64table.c | 77 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 184 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 +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* crc64.h +* +* See lib/crc64.c for the related specification and polynomical arithmetic. +*/ +#ifndef _LINUX_CRC64_H +#define _LINUX_CRC64_H + +#include <linux/types.h> + +__le64 crc64_le_update (__le64 crc,const void *_p,size_t len); +__le64 crc64_le (const void *p,size_t len); +__le64 crc64_le_bch (const void *p,size_t len); +#endif /* _LINUX_CRC64_H */
lib/.gitignore +2 -0
@@ -2,5 +2,7 @@ # Generated files # gen_crc32table +gen_crc64table crc32table.h +crc64table.h oid_registry_data.c
lib/Kconfig +8 -0
@@ -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
@@ -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 +71 -0
71 lines not on this page · raw message
lib/gen_crc64table.c new file +77 -0
77 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 2/3 bcache: use routines from lib/crc64.c for CRC64 calculation
In reply to Coly LiNow 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.
--- drivers/md/bcache/Kconfig | 1 + drivers/md/bcache/bcache.h | 7 +- drivers/md/bcache/btree.c | 2 +- drivers/md/bcache/request.c | 2 +- drivers/md/bcache/super.c | 5 +- drivers/md/bcache/util.c | 131 ------------------------------------ drivers/md/bcache/util.h | 5 +- 7 files changed, 11 insertions(+), 142 deletions(-)
drivers/md/bcache/Kconfig +1 -0
@@ -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/bcache.h +4 -3
@@ -189,6 +189,7 @@#include <linux/types.h> #include <linux/workqueue.h> #include <linux/kthread.h> +#include <linux/crc64.h> #include "bset.h" #include "util.h"
@@ -803,9 +804,9 @@ static inline bool ptr_available(struct cache_set *c, const struct bkey *k,* jset: The checksum is _always_ the first 8 bytes of these structs */ #define csum_set (i) \ - bch_crc64(((void *) (i)) + sizeof(uint64_t), \ - ((void *) bset_bkey_last(i)) - \ - (((void *) (i)) + sizeof(uint64_t))) + crc64_le_bch(((void *) (i)) + sizeof(uint64_t), \ + ((void *) bset_bkey_last(i)) - \ + (((void *) (i)) + sizeof(uint64_t)))/* Error handling macros */
drivers/md/bcache/btree.c +1 -1
@@ -194,7 +194,7 @@ static uint64_t btree_csum_set(struct btree *b, struct bset *i)uint64_t crc = b->key.ptr[0 ];void *data = (void *) i +8 , *end =bset_bkey_last (i); - crc =bch_crc64_update (crc, data, end - data); + crc =crc64_le_update (crc, data, end - data);return crc ^0xffffffffffffffffULL ; }
drivers/md/bcache/request.c +1 -1
@@ -45,7 +45,7 @@ static void bio_csum(struct bio *bio, struct bkey *k)bio_for_each_segment (bv, bio, iter) {void *d =kmap (bv.bv_page) + bv.bv_offset; - csum =bch_crc64_update (csum, d, bv.bv_len); + csum =crc64_le_update (csum, d, bv.bv_len);kunmap (bv.bv_page); }
drivers/md/bcache/super.c +3 -2
@@ -549,7 +549,7 @@ void bch_prio_write(struct cache *ca) p->next_bucket = ca->prio_buckets[i +1 ]; p->magic =pset_magic (&ca->sb); - p->csum =bch_crc64 (&p->magic,bucket_bytes (ca) -8 ); + p->csum =crc64_le_bch (&p->magic,bucket_bytes (ca) -8 ); bucket =bch_bucket_alloc (ca,RESERVE_PRIO , true);BUG_ON (bucket ==-1 );
@@ -599,7 +599,8 @@ static void prio_read(struct cache *ca, uint64_t bucket)prio_io (ca, bucket,REQ_OP_READ ,0 ); -if (p->csum !=bch_crc64 (&p->magic,bucket_bytes (ca) -8 )) +if (p->csum !=crc64_le_bch (&p->magic, +bucket_bytes (ca) -8 ))pr_warn ("bad csum reading priorities" );if (p->magic !=pset_magic (&ca->sb))
drivers/md/bcache/util.c +0 -131
134 lines not on this page · raw message
drivers/md/bcache/util.h +1 -4
@@ -11,6 +11,7 @@#include <linux/ratelimit.h> #include <linux/vmalloc.h> #include <linux/workqueue.h> +#include <linux/crc64.h> #include "closure.h"
@@ -561,8 +562,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
PATCH v2 3/3 lib/test_crc: Add test cases for crc calculation
In reply to Coly LiThis 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_le: PASSED (0x4e6b1ff972fa8c55, expval 0x4e6b1ff972fa8c55)test_crc: crc64_le_bch: PASSED (0x0e4f1391d7a4a62e, expval 0x0e4f1391d7a4a62e)test_crc: crc64_le_update: FAILED (0x03d4d0d85685d9a1, expval 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 are only test caes for 3 crc routines,
- crc64_le()
- crc64_le_bch()
- crc64_le_update()
--- lib/Kconfig.debug | 11 ++++ lib/Makefile | 1 + lib/test_crc.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 lib/test_crc.c
lib/Kconfig.debug +11 -0
@@ -1911,6 +1911,17 @@ config TEST_SYSCTL If unsure, say N. +config TEST_CRC + tristate "CRC calculation test driver" + depends on CRC64 + default n + 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
@@ -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 +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 3/3] lib/test_crc: Add test cases for crc calculation
Quoted 19 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_CRCenabled.The test results are printed into kernel message, which look like,
test_crc:crc64_le: PASSED (0x4e6b1ff972fa8c55, expval0x4e6b1ff972fa8c55)test_crc:crc64_le_bch: PASSED (0x0e4f1391d7a4a62e, expval0x0e4f1391d7a4a62e)test_crc:crc64_le_update: FAILED (0x03d4d0d85685d9a1, expval0x3d4d0d85685d9a1f)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.cis a testing frame work for many crc consistency testings. For now, there are only test caes for 3 crc routines,
-crc64_le()
-crc64_le_bch()
-crc64_le_update()
lib/Kconfig.debug config TEST_SYSCTL · line 1917
Quoted 5 lines
lib/Kconfig.debug +4
-0+config TEST_CRC + tristate "CRC calculation test driver" + depends on CRC64 + default n
Please, remove it.
You Cc'ed the guy who did this:
http://git.infradead.org/linux-platform-drivers-x86.git/commit/0192f17529fa3f8d78ca0181a2b2aaa7cbb0784d
Quoted 13 lines
(quoted diff) +12
-0+ 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. +static int chk_and_msg(const char *name, __le64 crc, __le64 expval) +{ + int ret = 0; + + if (crc == expval) { + pr_info("test_crc: %s: PASSED:(0x%016llx, expected 0x%016llx)", + name, crc, expval); + } else {
Review
Quoted 10 lines
(quoted diff) +9
-0+ pr_err("test_crc: %s: FAILED:(0x%016llx, expected 0x%016llx)", + name, crc, expval); + ret = -EINVAL; + } + + return ret; +} + +
One of the blank lines is redundant.
Quoted 4 lines
lib/test_crc.c new file +4
-0+static int __init test_crc_init (void ) +{ +int i; +int v, ret =0 ;
lib/test_crc.c line 125
Quoted 10 lines
lib/test_crc.c new file +10
-0+ +pr_info ("Kernel CRC consitency testing:" ); +for (i =0 ; test_data[i].name; i++) { + v = test_data[i].handler (&test_data[i]); +if (v <0 && ret ==0 ) + ret = -EINVAL ; + } + +return ret; +}
Didn't notice anything about statistics.
See, for example,
https://elixir.bootlin.com/linux/v4.18-rc5/source/lib/test_printf.c#L536
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/3] lib: add crc64 calculation routines
In reply to Coly LiQuoted 21 lines · Coly Li
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 programlib/gen_crc64table.cwill be
compiled tolib/gen_crc64tableand executed.
- The output ofgen_crc64tableexecution is an array called as lookup table (a.k.a POLY0x42f0e1eba9ea369) which contain 256 64bits-long numbers, this talbe is dumped into header filelib/crc64table.h.
- Then the header file is included bylib/crc64.cfor normal 64bit crc calculation.
- Function declaration of the crc64 calculation routines is placed ininclude/linux/crc64.h
Quoted 2 lines
+ *
crc64.h
+ *
Do we need file name in the file?
lib/crc64.c line 31
Quoted 8 lines
+ * See
lib/crc64.cfor the related specification and polynomical arithmetic.
+ */lib/crc64.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +#include <linux/module.h> +#include <uapi/linux/types.h>
Why not simple linux/types.h ?
lib/crc64.c line 35
Quoted 5 lines
lib/crc64.c new file +4
-0+#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" );
License mismatch with SPDX.
lib/gen_crc64table.c line 20
Quoted 3 lines
lib/gen_crc64table.c new file +3
-0+#include <inttypes.h> +#include <linux/swab.h> +#include <stdio.h>
I would follow the common grouping of the headers, like more generic first, more particular last:
inttupes
stdio
linux/foo
asm/bar
Quoted 1 line
+#include "
../usr/include/asm/byteorder.h"
Hmm...
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Re: [PATCH v2 1/3] lib: add crc64 calculation routines
Quoted 22 lines · Andy Shevchenko
Quoted 20 lines · Coly Li
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 programlib/gen_crc64table.cwill be
compiled tolib/gen_crc64tableand executed.
- The output ofgen_crc64tableexecution is an array called as lookup table (a.k.a POLY0x42f0e1eba9ea369) which contain 256 64bits-long numbers, this talbe is dumped into header filelib/crc64table.h.
- Then the header file is included bylib/crc64.cfor normal 64bit crc calculation.
- Function declaration of the crc64 calculation routines is placed ininclude/linux/crc64.h
Quoted 5 lines
Quoted 2 lines
+ *
crc64.h
+ *Do we need file name in the file?
Not mandatory, I remove it in v3 series.
Quoted 10 lines
lib/crc64.c line 31
Quoted 8 lines
+ * See
lib/crc64.cfor the related specification and polynomical arithmetic.
+ */lib/crc64.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +#include <linux/module.h> +#include <uapi/linux/types.h> Why not simple
linux/types.h?
Yes linux/types.h works, I use this one.
Quoted 9 lines
lib/crc64.c line 35
Quoted 6 lines
lib/crc64.c new file +4
-0+#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" );License mismatch with SPDX.
It is "GPL v2" in v3 series.
Quoted 7 lines
lib/gen_crc64table.c line 20
Quoted 3 lines
lib/gen_crc64table.c new file +3
-0+#include <inttypes.h> +#include <linux/swab.h> +#include <stdio.h> I would follow the common grouping of the headers, like more generic first, more particular last:
Quoted 7 lines
inttupes
stdiolinux/foo
asm/bar
Now it is in the following order,
#include <inttypes.h>
#include <stdio.h>
#include <linux/swab.h>
Quoted 3 lines
Quoted 1 line
+#include "
../usr/include/asm/byteorder.h"Hmm...
In v3 series, I don't use __le64 and cpu_to_le64(), so this header is not necessary. It does not exist in v3 series.
Thanks.
Coly Li
Re: [PATCH v2 3/3] lib/test_crc: Add test cases for crc calculation
In reply to Andy ShevchenkoQuoted 24 lines · Andy Shevchenko
Quoted 22 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_CRCenabled.The test results are printed into kernel message, which look like,
test_crc:crc64_le: PASSED (0x4e6b1ff972fa8c55, expval0x4e6b1ff972fa8c55)test_crc:crc64_le_bch: PASSED (0x0e4f1391d7a4a62e, expval0x0e4f1391d7a4a62e)test_crc:crc64_le_update: FAILED (0x03d4d0d85685d9a1, expval0x3d4d0d85685d9a1f)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.cis a testing frame work for many crc consistency testings. For now, there are only test caes for 3 crc routines,
-crc64_le()
-crc64_le_bch()
-crc64_le_update()lib/Kconfig.debug +3
-0+config TEST_CRC + tristate "CRC calculation test driver" + depends on CRC64
Quoted 6 lines
Quoted 1 line
+ default n
Please, remove it.
You Cc'ed the guy who did this:
http://git.infradead.org/linux-platform-drivers-x86.git/commit/0192f17529fa3f8d78ca0181a2b2aaa7cbb0784d
Sure, this line is removed in v3 series.
Quoted 21 lines
Quoted 20 lines
(quoted diff) +19
-0+ 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. +static int chk_and_msg(const char *name, __le64 crc, __le64 expval) +{ + int ret = 0; + + if (crc == expval) { + pr_info("test_crc: %s: PASSED:(0x%016llx, expected 0x%016llx)", + name, crc, expval); + } else { + pr_err("test_crc: %s: FAILED:(0x%016llx, expected 0x%016llx)", + name, crc, expval); + ret = -EINVAL; + } + + return ret; +}
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +One of the blank lines is redundant.
removed.
Quoted 17 lines
Quoted 4 lines
lib/test_crc.c new file +4
-0+static int __init test_crc_init (void ) +{ +int i; +int v, ret =0 ;lib/test_crc.c line 125
Quoted 10 lines
lib/test_crc.c new file +10
-0+ +pr_info ("Kernel CRC consitency testing:" ); +for (i =0 ; test_data[i].name; i++) { + v = test_data[i].handler (&test_data[i]); +if (v <0 && ret ==0 ) + ret = -EINVAL ; + } + +return ret; +}Didn't notice anything about statistics.
Quoted 3 lines
See, for example,
https://elixir.bootlin.com/linux/v4.18-rc5/source/lib/test_printf.c#L536
The code is added into v3 series.
Thanks for your review :-)
Coly Li
kbuild test robot
build botRe: [PATCH v2 1/3] lib: add crc64 calculation routines
In reply to Coly LiI love your patch! Yet something to improve:
Hi Coly,
I love your patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc5 next-20180717]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Coly-Li/lib-add-crc64-calculation-routines/20180717-151249
config: x86_64-fedora-25 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64 All errors (new ones prefixed by >>):
>> lib/gen_crc64table.c:21:10: fatal error: ../usr/include/asm/byteorder.h: No such file or directory
#include "../usr/include/asm/byteorder.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~compilation terminated.
vim +21 lib/gen_crc64table.c
> 21 #include "../usr/include/asm/byteorder.h"
22 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation