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.
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 1/4 lib/crc64: add crc64 option to lib/Kconfig
From: Andy Shevchenko <[email protected]>
This patch adds crc64 option entry in lib/Kconfig. This patch is part of a patch originally written by Andy Shevchenko, Coly Li cherry-picks it and compose the commit log for a re-written crc64 source code.
--- lib/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
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 he + config CRC4 tristate "CRC4 functions" help
Signature · 1 line
2.17.1
Re: [PATCH 1/4] lib/crc64: add crc64 option to lib/Kconfig
Quoted 22 lines · Coly Li
From: Andy Shevchenko <[email protected]>
This patch adds crc64 option entry in lib/Kconfig. This patch is part of a patch originally written by Andy Shevchenko, Coly Li cherry-picks it and compose the commit log for a re-written crc64 source code.
Signed-off-by Andy Shevchenko <[email protected]>Signed-off-by Coly Li <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[email protected]>Cc Linus Torvalds <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Kate Stewart <[email protected]>--- lib/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)lib/Kconfig +0
-0
lib/Kconfig config CRC32_BIT · line 179
Quoted 10 lines
lib/Kconfig +7
-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 he
eh? missing something here.
Quoted 5 lines
lib/Kconfig +1
-0+ config CRC4 tristate "CRC4 functions" help
Signature · 1 line
~Randy
Re: [PATCH 1/4] lib/crc64: add crc64 option to lib/Kconfig
Quoted 26 lines · Randy Dunlap
Quoted 25 lines · Coly Li
From: Andy Shevchenko <[email protected]>
This patch adds crc64 option entry in lib/Kconfig. This patch is part of a patch originally written by Andy Shevchenko, Coly Li cherry-picks it and compose the commit log for a re-written crc64 source code.
Signed-off-by Andy Shevchenko <[email protected]>Signed-off-by Coly Li <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[email protected]>Cc Linus Torvalds <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Kate Stewart <[email protected]>--- lib/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)lib/Kconfig +0
-0@@ -170,6 +170,14 @@ config CRC32_BIT endchoice
Quoted 8 lines
lib/Kconfig config CRC32_BIT · line 179
Quoted 7 lines
lib/Kconfig +7
-0+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 he
Hi Randy,
Quoted 2 lines
eh? missing something here.
Fix it in v2 series. Thanks.
Coly Li
PATCH 2/4 lib: add crc64 calculation routines
In reply to Coly LiThis 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/Makefile | 11 +++++++ lib/crc64.c | 71 +++++++++++++++++++++++++++++++++++++++ lib/gen_crc64table.c | 77 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 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/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
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 51 lines · Coly Li
lib/crc64.c new file +44
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ;
lib/crc64.c line 54
Quoted 10 lines
lib/crc64.c new file +10
-0+ crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ;
Hi,
What's wrong with just using 0ULL ?
thanks.
Quoted 5 lines
lib/crc64.c new file +5
-0+ + crc =crc64_le_update (crc, p, len); + +return crc; +}
Signature · 1 line
~Randy
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 54 lines · Randy Dunlap
Quoted 53 lines · Coly Li
lib/crc64.c new file +46
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + }
Quoted 9 lines
lib/crc64.c line 54
Quoted 8 lines
lib/crc64.c new file +8
-0+ +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ;
Hi Randy,
Quoted 2 lines
Hi,
What's wrong with just using 0ULL ?
In v2 series it will be 0x0ULL :-)
Thanks.
Coly Li
kbuild test robot
build botRe: [PATCH 2/4] 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-20180716]
[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/add-crc64-calculation-as-kernel-library/20180717-025930
config: i386-allyesconfig (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=i386 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
Re: [PATCH 2/4] lib: add crc64 calculation routines
In reply to Coly LiHi Coly,
Quoted 17 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 67 lines
lib/crc64.c new file +61
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ; + + crc =crc64_le_update (crc, p, len); + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le); +
lib/crc64.c line 71
Quoted 10 lines
lib/crc64.c new file +10
-0+/* For checksum calculation in drivers/md/bcache/ */ +__le64 crc64_le_bch (const void *p,size_t len) +{ +__le64 crc =0xFFFFFFFFFFFFFFFFULL ; + + crc =crc64_le_update (crc, p, len); + +return (crc ^0xFFFFFFFFFFFFFFFFULL ); +} +EXPORT_SYMBOL_GPL (crc64_le_bch);
Using __le64 here makes no sense, because that type indicates the endianness of the *bytes*, whereas with CRC's "little endian" and "big endian" refer to the order in which the *bits* are mapped to the polynomial coefficients.
Also as you can see for lib/crc32.c you really only need to provide a function
u64 __pure crc64_le(u64 crc, unsigned char const *p, size_t len);
and the callers can invert at the beginning and/or end if needed.
Also your function names make it sound like inverting the bits is the exception or not recommended, since you called the function which does the inversions "crc32_le_bch()" so it sounds like a bcache-specific hack, while the one that doesn't do the inversions is simply called "crc32_le()". But actually it's normally recommended to do CRC's with the inversions, so that leading and trailing zeroes affect the resulting CRC.
Quoted 19 lines
lib/gen_crc64table.c new file +13
-0@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Generate lookup table for the talbe-driven CRC64 calculation. +* +* 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. +* +* 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 <linux/swab.h> +#include <stdio.h> +#include "../usr/include/asm/byteorder.h" + +#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
Okay, that's actually the ECMA-182 polynomial in "big endian" form (highest order bit is the coefficient of x^63, lowest order bit is the coefficient of x^0), so you're actually doing a "big endian" CRC. So everything in your patch series that claims it's a little endian or "le" CRC is incorrect.
Quoted 13 lines
lib/gen_crc64table.c new file +13
-0+ +#ifdef __LITTLE_ENDIAN +# define cpu_to_le64 (x) ((__le64)(x)) +#else +# define cpu_to_le64 (x) ((__le64)__swab64(x)) +#endif + +static int64_t crc64_table[256 ] = {0 ,}; + +static void generate_crc64_table (void ) +{ +uint64_t i, j, c, crc; +
lib/gen_crc64table.c line 46
Quoted 10 lines
lib/gen_crc64table.c new file +10
-0+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 ;
See here, it's shifting out the most significant bit, which means it's the coefficient of the x^63 term ("big endian" or "normal" convention), not the x^0 term ("little endian" or "reversed" convention).
Eric
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 90 lines · Eric Biggers
Hi Coly,
Quoted 17 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 69 lines
lib/crc64.c new file +63
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ; + + crc =crc64_le_update (crc, p, len); + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le); + +/* For checksum calculation in drivers/md/bcache/ */ +__le64 crc64_le_bch (const void *p,size_t len)
Quoted 9 lines
lib/crc64.c line 71
Quoted 8 lines
lib/crc64.c new file +8
-0+{ +__le64 crc =0xFFFFFFFFFFFFFFFFULL ; + + crc =crc64_le_update (crc, p, len); + +return (crc ^0xFFFFFFFFFFFFFFFFULL ); +} +EXPORT_SYMBOL_GPL (crc64_le_bch);
Hi Eric,
Quoted 9 lines
Using
__le64here makes no sense, because that type indicates the endianness of the *bytes*, whereas with CRC's "little endian" and "big endian" refer to the order in which the *bits* are mapped to the polynomial coefficients.Also as you can see for
lib/crc32.cyou really only need to provide a functionu64 __pure crc64_le(u64 crc, unsigned char const *p, size_t len);and the callers can invert at the beginning and/or end if needed.
Let me explain why I explicit use __le64 here. When crc64 is used as on-disk checksum, the input of crc64 calculation should be in a explicit specific byte order. Currently check sum in bcache code assumes the CPU is in little endian and just feeds in-memory data into crc64 calculation, then the code does not work on big endian machine like s390x.
To solve such problem, before calculating CRC the in-memory data should be swapped into a specific byte order (in bcache case it should be little endian). For data storage or transfer, CRC calculation without explicit endian is more easy to introduce bugs.
When I declare the type of input and output value as __le64, on big endian machine, I expect a type mismatch warning if the input memory buffer is not swapped into little endian. For u64, there is no such type checking warning.
This is the initial version of lib/crc64.c, people may add their crc64 calculation routines when necessary, e.g. crc64_be() or crc64(). I only add crc64_le_update() and crc64_le_bch() because bcache code needs them.
Indeed there is no user of crc64_le() for now, but the file is name as lib/crc64.c, I think there should be a crc64 calculation at least, so I add crc64_le().
Quoted 8 lines
Also your function names make it sound like inverting the bits is the exception or not recommended, since you called the function which does the inversions "
crc32_le_bch()" so it sounds like a bcache-specific hack, while the one that doesn't do the inversions is simply called "crc32_le()". But actually it's normally recommended to do CRC's with the inversions, so that leading and trailing zeroes affect the resulting CRC.
I notice this, normally there are two crc routines provided, with and without inversion. The reason that there is no inversion version is no-user in Linux kernel. Indeed there is no user of crc64_le() in Linnux kernel so far. For performance reason, I doubt whether there will be more user to do 64bit crc in kernel.
I prefer two crc32 calculation for a 64bit value, but meta data checksum by crc64 calculation is used in bcache for years, the consistency has to be kept.
Quoted 54 lines
Quoted 19 lines
lib/gen_crc64table.c new file +13
-0@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Generate lookup table for the talbe-driven CRC64 calculation. +* +* 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. +* +* 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 <linux/swab.h> +#include <stdio.h> +#include "../usr/include/asm/byteorder.h" + +#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULLOkay, that's actually the ECMA-182 polynomial in "big endian" form (highest order bit is the coefficient of x^63, lowest order bit is the coefficient of x^0), so you're actually doing a "big endian" CRC. So everything in your patch series that claims it's a little endian or "le" CRC is incorrect.
Quoted 19 lines
lib/gen_crc64table.c new file +19
-0+ +#ifdef __LITTLE_ENDIAN +# define cpu_to_le64 (x) ((__le64)(x)) +#else +# define cpu_to_le64 (x) ((__le64)__swab64(x)) +#endif + +static int64_t crc64_table[256 ] = {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 )
Quoted 8 lines
lib/gen_crc64table.c line 46
Quoted 4 lines
lib/gen_crc64table.c new file +4
-0+ crc = (crc <<1 ) ^CRC64_ECMA182_POLY ; + else + crc <<= 1; + c <<=1 ;See here, it's shifting out the most significant bit, which means it's the coefficient of the x^63 term ("big endian" or "normal" convention), not the x^0 term ("little endian" or "reversed" convention).
I see your point here. I am not expert in coding theory, the knowledge I have is from wikipedia, ECMA-182 and the document from Dr. Ross Williams. From ECMA-182 document, I don't see any word with 'big endian', so I take it as a standard poly and regardless the byte order.
And on wikepedia page
https://en.wikipedia.org/wiki/Cyclic_redundancy_check , CRC-64-ECMA references the same poly and call "0x42F0E1EBA9EA3693" as normal poly, which one links to polynomial
"x^64 + x^62 + x^57 + x^55 + x^54 + ....x^7 + x^4 + x + 1"
if I understand correctly. But from your information, it seems the polynomial in generate_crc64_table() is x^64 + x^61 ..... Maybe I misunderstand you, could you please give me more hint ?
Thanks.
Coly Li
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 119 lines · Coly Li
Quoted 90 lines · Eric Biggers
Hi Coly,
Quoted 17 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 69 lines
lib/crc64.c new file +63
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ; + + crc =crc64_le_update (crc, p, len); + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le); + +/* For checksum calculation in drivers/md/bcache/ */ +__le64 crc64_le_bch (const void *p,size_t len)Quoted 9 lines
lib/crc64.c line 71
Quoted 8 lines
lib/crc64.c new file +8
-0+{ +__le64 crc =0xFFFFFFFFFFFFFFFFULL ; + + crc =crc64_le_update (crc, p, len); + +return (crc ^0xFFFFFFFFFFFFFFFFULL ); +} +EXPORT_SYMBOL_GPL (crc64_le_bch);Hi Eric,
Quoted 9 lines
Using
__le64here makes no sense, because that type indicates the endianness of the *bytes*, whereas with CRC's "little endian" and "big endian" refer to the order in which the *bits* are mapped to the polynomial coefficients.Also as you can see for
lib/crc32.cyou really only need to provide a functionu64 __pure crc64_le(u64 crc, unsigned char const *p, size_t len);and the callers can invert at the beginning and/or end if needed.
Let me explain why I explicit use
__le64here. When crc64 is used as on-disk checksum, the input of crc64 calculation should be in a explicit specific byte order. Currently check sum in bcache code assumes the CPU is in little endian and just feeds in-memory data into crc64 calculation, then the code does not work on big endian machine like s390x.
Quoted 4 lines
To solve such problem, before calculating CRC the in-memory data should be swapped into a specific byte order (in bcache case it should be little endian). For data storage or transfer, CRC calculation without explicit endian is more easy to introduce bugs.
No, the implementation never loads multi-byte values, so CPU endianness doesn't matter for the input. CPU endianness *does* matter when serializing the final calculated CRC into a byte array for storing on-disk, so maybe bcache gets that part wrong, I don't know. Either way, that has nothing to do with how the polynomial coefficients (bits) are ordered *within bytes*, which is what the "_be" and "_le" refer to in the CRC-32 implementation. Yes, the naming is unfortunate as it can easily be confused with the usual "bytewise" endianness, but you need to understand it.
Again, using __le64 makes absolutely no sense. You're even doing operations like shifts directly on a "__le64" which sparse will (correctly) complain about.
Quoted 29 lines
When I declare the type of input and output value as
__le64, on big endian machine, I expect a type mismatch warning if the input memory buffer is not swapped into little endian. For u64, there is no such type checking warning.This is the initial version of
lib/crc64.c, people may add their crc64 calculation routines when necessary, e.g.crc64_be()orcrc64(). I only addcrc64_le_update()andcrc64_le_bch()because bcache code needs them.Indeed there is no user of
crc64_le()for now, but the file is name aslib/crc64.c, I think there should be a crc64 calculation at least, so I addcrc64_le().Quoted 8 lines
Also your function names make it sound like inverting the bits is the exception or not recommended, since you called the function which does the inversions "
crc32_le_bch()" so it sounds like a bcache-specific hack, while the one that doesn't do the inversions is simply called "crc32_le()". But actually it's normally recommended to do CRC's with the inversions, so that leading and trailing zeroes affect the resulting CRC.I notice this, normally there are two crc routines provided, with and without inversion. The reason that there is no inversion version is no-user in Linux kernel. Indeed there is no user of
crc64_le()in Linnux kernel so far. For performance reason, I doubt whether there will be more user to do 64bit crc in kernel.
Quoted 3 lines
I prefer two crc32 calculation for a 64bit value, but meta data checksum by crc64 calculation is used in bcache for years, the consistency has to be kept.
Well, your response didn't actually address my points. But it raises the
question: if there won't be any other users, then why move CRC-64 to lib/ at all?
Quoted 70 lines
Quoted 56 lines
Quoted 21 lines
lib/gen_crc64table.c new file +13
-0@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Generate lookup table for the talbe-driven CRC64 calculation. +* +* 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. +* +* 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 <linux/swab.h> +#include <stdio.h> +#include "../usr/include/asm/byteorder.h" + +#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULLOkay, that's actually the ECMA-182 polynomial in "big endian" form (highest order bit is the coefficient of x^63, lowest order bit is the coefficient of x^0), so you're actually doing a "big endian" CRC. So everything in your patch series that claims it's a little endian or "le" CRC is incorrect.
Quoted 19 lines
lib/gen_crc64table.c new file +19
-0+ +#ifdef __LITTLE_ENDIAN +# define cpu_to_le64 (x) ((__le64)(x)) +#else +# define cpu_to_le64 (x) ((__le64)__swab64(x)) +#endif + +static int64_t crc64_table[256 ] = {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 )Quoted 8 lines
lib/gen_crc64table.c line 46
Quoted 4 lines
lib/gen_crc64table.c new file +4
-0+ crc = (crc <<1 ) ^CRC64_ECMA182_POLY ; + else + crc <<= 1; + c <<=1 ;See here, it's shifting out the most significant bit, which means it's the coefficient of the x^63 term ("big endian" or "normal" convention), not the x^0 term ("little endian" or "reversed" convention).
I see your point here. I am not expert in coding theory, the knowledge I have is from wikipedia, ECMA-182 and the document from Dr. Ross Williams. From ECMA-182 document, I don't see any word with 'big endian', so I take it as a standard poly and regardless the byte order.
Quoted 8 lines
And on wikepedia page
https://en.wikipedia.org/wiki/Cyclic_redundancy_check , CRC-64-ECMA references the same poly and call "0x42F0E1EBA9EA3693" as normal poly, which one links to polynomial"x^64 + x^62 + x^57 + x^55 + x^54 + ....x^7 + x^4 + x + 1"if I understand correctly. But from your information, it seems the polynomial in
generate_crc64_table()is x^64 + x^61 ..... Maybe I misunderstand you, could you please give me more hint ?
As I said, the "normal" convention is the same as "big endian", and the "reversed" convention is the same as "little endian" (again, meaning "bitwise" endianness, not the usual "bytewise" endianness). The polynomial is correct but you are claiming the polynomial coefficients are mapped to bits in a different order than they actually are.
- Eric
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 120 lines · Eric Biggers
Quoted 119 lines · Coly Li
Quoted 90 lines · Eric Biggers
Hi Coly,
Quoted 17 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 69 lines
lib/crc64.c new file +63
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ; + + crc =crc64_le_update (crc, p, len); + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le); + +/* For checksum calculation in drivers/md/bcache/ */ +__le64 crc64_le_bch (const void *p,size_t len)Quoted 9 lines
lib/crc64.c line 71
Quoted 8 lines
lib/crc64.c new file +8
-0+{ +__le64 crc =0xFFFFFFFFFFFFFFFFULL ; + + crc =crc64_le_update (crc, p, len); + +return (crc ^0xFFFFFFFFFFFFFFFFULL ); +} +EXPORT_SYMBOL_GPL (crc64_le_bch);Hi Eric,
Quoted 9 lines
Using
__le64here makes no sense, because that type indicates the endianness of the *bytes*, whereas with CRC's "little endian" and "big endian" refer to the order in which the *bits* are mapped to the polynomial coefficients.Also as you can see for
lib/crc32.cyou really only need to provide a functionu64 __pure crc64_le(u64 crc, unsigned char const *p, size_t len);and the callers can invert at the beginning and/or end if needed.
Let me explain why I explicit use
__le64here. When crc64 is used as on-disk checksum, the input of crc64 calculation should be in a explicit specific byte order. Currently check sum in bcache code assumes the CPU is in little endian and just feeds in-memory data into crc64 calculation, then the code does not work on big endian machine like s390x.
Quoted 7 lines
Quoted 4 lines
To solve such problem, before calculating CRC the in-memory data should be swapped into a specific byte order (in bcache case it should be little endian). For data storage or transfer, CRC calculation without explicit endian is more easy to introduce bugs.
No, the implementation never loads multi-byte values, so CPU endianness doesn't matter for the input. CPU endianness *does* matter when serializing the final
If the checksum is generated on big endian machine and checked on little endian machine, non-specific endianness will be problematic.
Quoted 7 lines
calculated CRC into a byte array for storing on-disk, so maybe bcache gets that part wrong, I don't know. Either way, that has nothing to do with how the polynomial coefficients (bits) are ordered *within bytes*, which is what the "
_be" and "_le" refer to in the CRC-32 implementation. Yes, the naming is unfortunate as it can easily be confused with the usual "bytewise" endianness, but you need to understand it.
I see, it seems I misunderstand _le and _be in CRC-32 implementation. OK, I will find a way to fix the naming and data type issues in v3 series.
Quoted 3 lines
Again, using
__le64makes absolutely no sense. You're even doing operations like shifts directly on a "__le64" which sparse will (correctly) complain about.
Sure, you are correct here :-)
Quoted 29 lines
Quoted 29 lines
When I declare the type of input and output value as
__le64, on big endian machine, I expect a type mismatch warning if the input memory buffer is not swapped into little endian. For u64, there is no such type checking warning.This is the initial version of
lib/crc64.c, people may add their crc64 calculation routines when necessary, e.g.crc64_be()orcrc64(). I only addcrc64_le_update()andcrc64_le_bch()because bcache code needs them.Indeed there is no user of
crc64_le()for now, but the file is name aslib/crc64.c, I think there should be a crc64 calculation at least, so I addcrc64_le().Quoted 8 lines
Also your function names make it sound like inverting the bits is the exception or not recommended, since you called the function which does the inversions "
crc32_le_bch()" so it sounds like a bcache-specific hack, while the one that doesn't do the inversions is simply called "crc32_le()". But actually it's normally recommended to do CRC's with the inversions, so that leading and trailing zeroes affect the resulting CRC.I notice this, normally there are two crc routines provided, with and without inversion. The reason that there is no inversion version is no-user in Linux kernel. Indeed there is no user of
crc64_le()in Linnux kernel so far. For performance reason, I doubt whether there will be more user to do 64bit crc in kernel.
Quoted 8 lines
Quoted 3 lines
I prefer two crc32 calculation for a 64bit value, but meta data checksum by crc64 calculation is used in bcache for years, the consistency has to be kept.
Well, your response didn't actually address my points. But it raises the
question: if there won't be any other users, then why move CRC-64 tolib/at all?
The only motivation I can see is becachefs, which share part of the code base with bcache, including crc64 calculation.
And before CPU supports build-in instructors for CRC64, I don't see the reason why people should use 64bit CRC other than 32bit ones.
Quoted 79 lines
Quoted 70 lines
Quoted 56 lines
Quoted 21 lines
lib/gen_crc64table.c new file +13
-0@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Generate lookup table for the talbe-driven CRC64 calculation. +* +* 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. +* +* 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 <linux/swab.h> +#include <stdio.h> +#include "../usr/include/asm/byteorder.h" + +#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULLOkay, that's actually the ECMA-182 polynomial in "big endian" form (highest order bit is the coefficient of x^63, lowest order bit is the coefficient of x^0), so you're actually doing a "big endian" CRC. So everything in your patch series that claims it's a little endian or "le" CRC is incorrect.
Quoted 19 lines
lib/gen_crc64table.c new file +19
-0+ +#ifdef __LITTLE_ENDIAN +# define cpu_to_le64 (x) ((__le64)(x)) +#else +# define cpu_to_le64 (x) ((__le64)__swab64(x)) +#endif + +static int64_t crc64_table[256 ] = {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 )Quoted 8 lines
lib/gen_crc64table.c line 46
Quoted 4 lines
lib/gen_crc64table.c new file +4
-0+ crc = (crc <<1 ) ^CRC64_ECMA182_POLY ; + else + crc <<= 1; + c <<=1 ;See here, it's shifting out the most significant bit, which means it's the coefficient of the x^63 term ("big endian" or "normal" convention), not the x^0 term ("little endian" or "reversed" convention).
I see your point here. I am not expert in coding theory, the knowledge I have is from wikipedia, ECMA-182 and the document from Dr. Ross Williams. From ECMA-182 document, I don't see any word with 'big endian', so I take it as a standard poly and regardless the byte order.
Quoted 8 lines
And on wikepedia page
https://en.wikipedia.org/wiki/Cyclic_redundancy_check , CRC-64-ECMA references the same poly and call "0x42F0E1EBA9EA3693" as normal poly, which one links to polynomial"x^64 + x^62 + x^57 + x^55 + x^54 + ....x^7 + x^4 + x + 1"if I understand correctly. But from your information, it seems the polynomial in
generate_crc64_table()is x^64 + x^61 ..... Maybe I misunderstand you, could you please give me more hint ?
Quoted 5 lines
As I said, the "normal" convention is the same as "big endian", and the "reversed" convention is the same as "little endian" (again, meaning "bitwise" endianness, not the usual "bytewise" endianness). The polynomial is correct but you are claiming the polynomial coefficients are mapped to bits in a different order than they actually are.
Copied, thanks for the hint :-)
Coly Li
Re: [PATCH 2/4] lib: add crc64 calculation routines
Quoted 101 lines · Coly Li
Quoted 100 lines · Eric Biggers
Quoted 99 lines · Coly Li
Quoted 98 lines · Eric Biggers
Hi Coly,
Quoted 17 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 77 lines
lib/crc64.c new file +71
-0@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Normal 64bit CRC calculation. +* +* This is a basic crc64 implementation following ECMA-182 specification, +* which can be found from, +* http://www.ecma-international.org/publications/standards/Ecma-182.htm +* +* Dr. Ross N. Williams has a great document to introduce the idea of CRC +* algorithm, here the CRC64 code is also inspired by the table-driven +* algorithm and detail example from this paper. This paper can be found +* from, +* http://www.ross.net/crc/download/crc_v3.txt +* +* crc64table_le[256] is the lookup table of a table-driver 64bit CRC +* 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]> +* +*/ + +#include <linux/module.h> +#include <uapi/linux/types.h> +#include "crc64table.h" + +MODULE_DESCRIPTION ("CRC64 calculations" ); +MODULE_LICENSE ("GPL" ); + +__le64 crc64_le_update (__le64 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 ) ^ (__le64)(*p++)) &0xFF ; + crc = crc64table_le[t] ^ (crc <<8 ); + } + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le_update); + +__le64 crc64_le (const void *p,size_t len) +{ +__le64 crc =0x0000000000000000ULL ; + + crc =crc64_le_update (crc, p, len); + +return crc; +} +EXPORT_SYMBOL_GPL (crc64_le); + +/* For checksum calculation in drivers/md/bcache/ */ +__le64 crc64_le_bch (const void *p,size_t len) +{ +__le64 crc =0xFFFFFFFFFFFFFFFFULL ; + + crc =crc64_le_update (crc, p, len); + +return (crc ^0xFFFFFFFFFFFFFFFFULL ); +} +EXPORT_SYMBOL_GPL (crc64_le_bch);
[snipped]
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
I see your point here. I am not expert in coding theory, the knowledge I have is from wikipedia, ECMA-182 and the document from Dr. Ross Williams. From ECMA-182 document, I don't see any word with 'big endian', so I take it as a standard poly and regardless the byte order.
And on wikepedia page
https://en.wikipedia.org/wiki/Cyclic_redundancy_check , CRC-64-ECMA references the same poly and call "0x42F0E1EBA9EA3693" as normal poly, which one links to polynomial"x^64 + x^62 + x^57 + x^55 + x^54 + ....x^7 + x^4 + x + 1"if I understand correctly. But from your information, it seems the polynomial in
generate_crc64_table()is x^64 + x^61 ..... Maybe I misunderstand you, could you please give me more hint ?
Quoted 5 lines
Quoted 5 lines
As I said, the "normal" convention is the same as "big endian", and the "reversed" convention is the same as "little endian" (again, meaning "bitwise" endianness, not the usual "bytewise" endianness). The polynomial is correct but you are claiming the polynomial coefficients are mapped to bits in a different order than they actually are.
In v3 series, I remove _le and __le64 from the patch, that means the calculation is not explicit resitricted to little endian, and the caller should make sure the data in a proper byte order. The result is, there is no misleading naming issue. Since so far the only user is bcache, and potential user is bcachefs, the developers should know how to use crc64_update() and crc64_bch() properly.
So now the routines are named as crc64(), crc64_update(), crc64_bch(), and return value is u64.
I hope now the misleading can be avoided.
Thanks.
Coly Li
PATCH 3/4 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 4/4 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 can is a testing frame work for all 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 | 136 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 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" + default n + 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
@@ -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 +136 -0
136 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 4/4] lib/test_crc: Add test cases for crc calculation
Quoted 9 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)
Quoted 5 lines
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.ccan is a testing frame work for all crc consistency
drop ^^^ "can"
Well, we already have (from lib/Makefile):
obj-$(CONFIG_CRC32_SELFTEST) += crc32test.o
so lib/test_crc.c isn't exactly "for all crc".
Quoted 25 lines
testings. For now, there are only test caes for 3 crc routines,
-crc64_le()
-crc64_le_bch()
-crc64_le_update()Signed-off-by Coly Li <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[email protected]>Cc Linus Torvalds <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Kate Stewart <[email protected]>--- lib/Kconfig.debug | 11 ++++ lib/Makefile | 1 + lib/test_crc.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 lib/test_crc.clib/test_crc.c new file +0
-0
lib/test_crc.c line 8
Quoted 8 lines
lib/test_crc.c new file +8
-0@@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* CRC test driver +* +* Copyright (C) 2018 Coly Li <[email protected]> +* +* This module provides an simple framework to check the consistency of +* Linux kernel crc calculation routines in lib/crc*.c. This driver
CRC
Quoted 33 lines
lib/test_crc.c new file +33
-0+* requires CONFIG_CRC* items to be enabled if the associated routines are +* tested here. The test results will be printed to kernel message +* when this test driver is loaded. +* +* Current test routines are, +* - crc64_le() +* - crc64_le_bch() +* - crc64_le_update() +* +*/ + +#include <linux/init.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/printk.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/async.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include <linux/crc64.h> + +struct crc_test_record { + +char *name; +__le64 data[4 ]; +__le64 initval; +__le64 expval; +int (*handler)(struct crc_test_record *rec); +}; +
lib/test_crc.c line 51
Quoted 10 lines
lib/test_crc.c new file +10
-0+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, expval 0x%016llx)" , + name, crc, expval); + }else { +pr_err ("test_crc: %s: FAILED:(0x%016llx, expval 0x%016llx)" , + name, crc, expval);
For both pr_err() lines, please print "expected" instead of "expval".
lib/test_crc.c line 58
Quoted 7 lines
lib/test_crc.c new file +7
-0+ ret = -EINVAL ; + } + +return ret; +} + +/* Add your crc test caese here */
CRC test cases
Quoted 52 lines
lib/test_crc.c new file +52
-0+static int test_crc64_le (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval); + +} + +static int test_crc64_le_bch (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le_bch (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval); +} + +static int test_crc64_le_update (struct crc_test_record *rec) +{ +__le64 crc = rec->initval; + + crc =crc64_le_update (crc, rec->data,sizeof (rec->data)); +return 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. +*/ +static struct crc_test_record test_data[] = { + { .name ="crc64_le" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0 , + .expval =0xe2b9911e7b997201 , + .handler = test_crc64_le, + }, + { .name ="crc64_le_bch" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0 , + .expval =0xd2753a20fd862892 , + .handler = test_crc64_le_bch, + }, + { .name ="crc64_le_update" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0x61C8864680B583EB , + .expval =0xb2c863673f4292bf , + .handler = test_crc64_le_update, + },
lib/test_crc.c line 120
Quoted 10 lines
lib/test_crc.c new file +10
-0+ { .name = NULL, } +}; + + +static int __init test_crc_init (void ) +{ +int i; +int v, ret =0 ; + +pr_info ("Kernel crc consitency testing:" );
CRC consistency
Quoted 7 lines
lib/test_crc.c new file +6
-0+for (i =0 ; test_data[i].name; i++) { + v = test_data[i].handler (&test_data[i]); +if (v <0 && ret ==0 ) + ret = -EINVAL ; + } +
lib/test_crc.c line 136
Quoted 10 lines
lib/test_crc.c new file +10
-0+return ret; +} +late_initcall (test_crc_init); + +static void __exit test_crc_exit(void) { } +module_exit (test_crc_exit); + +MODULE_DESCRIPTION ("CRC consistency testing driver" ); +MODULE_AUTHOR ("Coly Li <[email protected]>" ); +MODULE_LICENSE ("GPL" );
thanks,
Signature · 1 line
~Randy
Re: [PATCH 4/4] lib/test_crc: Add test cases for crc calculation
Quoted 6 lines · Randy Dunlap
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_CRCenabled.The test results are printed into kernel message, which look like,
Quoted 8 lines
Quoted 8 lines
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.
Hi Randy,
Quoted 4 lines
Quoted 1 line
lib/test_crc.ccan is a testing frame work for all crc consistencydrop ^^^ "can"
All the following issues are fixed in v2 series, thank you for the review.
Quoted 5 lines
Well, we already have (from lib/Makefile):
obj-$(CONFIG_CRC32_SELFTEST) += crc32test.oso
lib/test_crc.cisn't exactly "for all crc".
My plan is to add extra consistency test cases for crc32/crc32c for consistency testing. Consistency testing case is much simpler than the code in crc32test.c, the results of test_crc.c are in unified format so 0day testing can handle them in a simple way.
I agree with you that 'for all crc' is inaccurate before the crc32/crc32c test cases added into test_crc. So in v2 series, it will be modified to 'for many crc consistency testings'.
Thanks.
Coly Li
Quoted 179 lines
Quoted 26 lines
testings. For now, there are only test caes for 3 crc routines,
-crc64_le()
-crc64_le_bch()
-crc64_le_update()Signed-off-by Coly Li <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[email protected]>Cc Linus Torvalds <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Kate Stewart <[email protected]>--- lib/Kconfig.debug | 11 ++++ lib/Makefile | 1 + lib/test_crc.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 lib/test_crc.clib/test_crc.c new file +0
-0lib/test_crc.c line 8
Quoted 8 lines
lib/test_crc.c new file +8
-0@@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* CRC test driver +* +* Copyright (C) 2018 Coly Li <[email protected]> +* +* This module provides an simple framework to check the consistency of +* Linux kernel crc calculation routines in lib/crc*.c. This driver CRC
Quoted 33 lines
lib/test_crc.c new file +33
-0+* requires CONFIG_CRC* items to be enabled if the associated routines are +* tested here. The test results will be printed to kernel message +* when this test driver is loaded. +* +* Current test routines are, +* - crc64_le() +* - crc64_le_bch() +* - crc64_le_update() +* +*/ + +#include <linux/init.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/printk.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/async.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include <linux/crc64.h> + +struct crc_test_record { + +char *name; +__le64 data[4 ]; +__le64 initval; +__le64 expval; +int (*handler)(struct crc_test_record *rec); +}; +lib/test_crc.c line 51
Quoted 10 lines
lib/test_crc.c new file +10
-0+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, expval 0x%016llx)" , + name, crc, expval); + }else { +pr_err ("test_crc: %s: FAILED:(0x%016llx, expval 0x%016llx)" , + name, crc, expval);For both
pr_err()lines, please print "expected" instead of "expval".lib/test_crc.c line 58
Quoted 7 lines
lib/test_crc.c new file +7
-0+ ret = -EINVAL ; + } + +return ret; +} + +/* Add your crc test caese here */ CRC test cases
Quoted 52 lines
lib/test_crc.c new file +52
-0+static int test_crc64_le (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval); + +} + +static int test_crc64_le_bch (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le_bch (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval); +} + +static int test_crc64_le_update (struct crc_test_record *rec) +{ +__le64 crc = rec->initval; + + crc =crc64_le_update (crc, rec->data,sizeof (rec->data)); +return 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. +*/ +static struct crc_test_record test_data[] = { + { .name ="crc64_le" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0 , + .expval =0xe2b9911e7b997201 , + .handler = test_crc64_le, + }, + { .name ="crc64_le_bch" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0 , + .expval =0xd2753a20fd862892 , + .handler = test_crc64_le_bch, + }, + { .name ="crc64_le_update" , + .data = {0x42F0E1EBA9EA3693 ,0x85E1C3D753D46D26 , +0xC711223CFA3E5BB5 ,0x493366450E42ECDF }, + .initval =0x61C8864680B583EB , + .expval =0xb2c863673f4292bf , + .handler = test_crc64_le_update, + },lib/test_crc.c line 120
Quoted 10 lines
lib/test_crc.c new file +10
-0+ { .name = NULL, } +}; + + +static int __init test_crc_init (void ) +{ +int i; +int v, ret =0 ; + +pr_info ("Kernel crc consitency testing:" );CRC consistency
Quoted 7 lines
lib/test_crc.c new file +6
-0+for (i =0 ; test_data[i].name; i++) { + v = test_data[i].handler (&test_data[i]); +if (v <0 && ret ==0 ) + ret = -EINVAL ; + } +lib/test_crc.c line 136
Quoted 10 lines
lib/test_crc.c new file +10
-0+return ret; +} +late_initcall (test_crc_init); + +static void __exit test_crc_exit(void) { } +module_exit (test_crc_exit); + +MODULE_DESCRIPTION ("CRC consistency testing driver" ); +MODULE_AUTHOR ("Coly Li <[email protected]>" ); +MODULE_LICENSE ("GPL" );thanks,
Re: [PATCH 4/4] lib/test_crc: Add test cases for crc calculation
In reply to Coly LiQuoted 6 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 (0x4e6b> +
1ff972fa8c55, expval 0x4e6b1ff972fa8c55)
Quoted 13 lines
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.ccan is a testing frame work for all 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 1916
Quoted 4 lines
lib/Kconfig.debug +3
-0+config TEST_CRC + tristate "CRC calculation test driver" + default n
Default default is n.
Quoted 4 lines
lib/test_crc.c new file +3
-0+ depends on CRC64 +#include <linux/init.h> +#include <linux/list.h>
lib/test_crc.c line 31
Quoted 10 lines
lib/test_crc.c new file +10
-0+#include <linux/module.h> +#include <linux/printk.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/async.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include <linux/crc64.h>
Perhaps in order?
Moreover, either init.h or module.h depending on the Kconfig (here seems module.h is a right choice).
lib/test_crc.c line 34
Quoted 3 lines
lib/test_crc.c new file +2
-0+struct crc_test_record { +
Redundant.
Quoted 11 lines
lib/test_crc.c new file +11
-0+char *name; +__le64 data[4 ]; +__le64 initval; +__le64 expval; +int (*handler)(struct crc_test_record *rec); +}; + +static int chk_and_msg (const char *name,__le64 crc,__le64 expval) +{ +int ret =0 ; +
lib/test_crc.c line 55
Quoted 10 lines
lib/test_crc.c new file +10
-0+if (crc == expval) { +pr_info ("test_crc: %s: PASSED:(0x%016llx, expval 0x%016llx)" , + name, crc, expval); + }else { +pr_err ("test_crc: %s: FAILED:(0x%016llx, expval 0x%016llx)" , + name, crc, expval); + ret = -EINVAL ; + } + +return ret;
Perhaps collect statistics instead how it's done in many other tests?
lib/test_crc.c line 58
Quoted 3 lines
lib/test_crc.c new file +3
-0+} + +/* Add your crc test caese here */
caese ?
lib/test_crc.c line 65
Quoted 8 lines
lib/test_crc.c new file +7
-0+static int test_crc64_le (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval); +
Redundant.
lib/test_crc.c line 111
Quoted 3 lines
lib/test_crc.c new file +2
-0+} + { .name = NULL, }
Simple {} would work.
lib/test_crc.c line 124
Quoted 11 lines
lib/test_crc.c new file +10
-0+static int __init test_crc_init (void ) +{ +int i; +int v, ret =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 ;
A bit strange. Anyway, better to collect statistics and print it at the end with corresponding return code.
lib/test_crc.c line 129
Quoted 6 lines
lib/test_crc.c new file +5
-0+ } + +return ret; +} +late_initcall (test_crc_init);
Why?
lib/test_crc.c line 136
Quoted 4 lines
lib/test_crc.c new file +3
-0+static void __exit test_crc_exit(void) { } +module_exit (test_crc_exit); +MODULE_LICENSE ("GPL" );
It's not the same as in SPDX.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 4/4] lib/test_crc: Add test cases for crc calculation
Quoted 24 lines · Andy Shevchenko
Quoted 6 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 (0x4e6b> +
1ff972fa8c55, expval0x4e6b1ff972fa8c55)Quoted 15 lines
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.ccan is a testing frame work for all 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 +2
-0+config TEST_CRC + tristate "CRC calculation test driver"
Hi Andy,
Quoted 4 lines
Quoted 1 line
+ default n
Default default is n.
I see TEST_FIRMWARE, TEST_SYSCTL, TEST_UDELAY, TEST_STATIC_KEYS arround TEST_CRC all have 'default n', then I think to follow the style it might be better to have 'default n' here too.
Quoted 16 lines
Quoted 4 lines
lib/test_crc.c new file +3
-0+ depends on CRC64 +#include <linux/init.h> +#include <linux/list.h> lib/test_crc.c line 31
Quoted 10 lines
lib/test_crc.c new file +10
-0+#include <linux/module.h> +#include <linux/printk.h> +#include <linux/fs.h> +#include <linux/miscdevice.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/async.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include <linux/crc64.h> Perhaps in order?
Do you mean in alphabet order of header file names ? I will do it in v2 series.
Quoted 4 lines
Moreover, either
init.hormodule.hdepending on theKconfig(here seemsmodule.his a right choice).
Sure I will keep module.h in v2 series.
Quoted 5 lines
lib/test_crc.c line 34
Quoted 3 lines
lib/test_crc.c new file +2
-0+struct crc_test_record { +Redundant.
Removed.
Quoted 16 lines
Quoted 16 lines
lib/test_crc.c new file +15
-0+char *name; +__le64 data[4 ]; +__le64 initval; +__le64 expval; +int (*handler)(struct crc_test_record *rec); +}; + +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, expval 0x%016llx)" , + name, crc, expval); + }else {
Quoted 9 lines
lib/test_crc.c line 55
Quoted 6 lines
lib/test_crc.c new file +6
-0+pr_err ("test_crc: %s: FAILED:(0x%016llx, expval 0x%016llx)" , + name, crc, expval); + ret = -EINVAL ; + } + +return ret;Perhaps collect statistics instead how it's done in many other tests?
Good idea. I will add this in v2 series.
Quoted 5 lines
lib/test_crc.c line 58
Quoted 3 lines
lib/test_crc.c new file +3
-0+} + +/* Add your crc test caese here */ caese ?
Fixed.
Quoted 8 lines
Quoted 7 lines
lib/test_crc.c new file +6
-0+static int test_crc64_le (struct crc_test_record *rec) +{ +__le64 crc; + + crc =crc64_le (rec->data,sizeof (rec->data)); +return chk_and_msg (rec->name, crc, rec->expval);
Quoted 4 lines
Quoted 1 line
+
Redundant.
Fixed.
Quoted 6 lines
lib/test_crc.c line 111
Quoted 3 lines
lib/test_crc.c new file +2
-0+} + { .name = NULL, }Simple {} would work.
Fixed.
Quoted 8 lines
Quoted 7 lines
lib/test_crc.c new file +7
-0+static int __init test_crc_init (void ) +{ +int i; +int v, ret =0 ; + +pr_info ("Kernel crc consitency testing:" ); +for (i =0 ; test_data[i].name; i++) {
Quoted 7 lines
lib/test_crc.c line 124
Quoted 3 lines
lib/test_crc.c new file +3
-0+ v = test_data[i].handler (&test_data[i]); +if (v <0 && ret ==0 ) + ret = -EINVAL ;A bit strange. Anyway, better to collect statistics and print it at the end with corresponding return code.
Sure, I will add this :-)
Quoted 8 lines
lib/test_crc.c line 129
Quoted 6 lines
lib/test_crc.c new file +5
-0+ } + +return ret; +} +late_initcall (test_crc_init);Why?
Oh, IMHO this is a test module, we don't need to occupy boot time and it should be good to invoke it after other modules loaded. As I see many other test modules do this.
Quoted 8 lines
lib/test_crc.c line 136
Quoted 5 lines
lib/test_crc.c new file +3
-0+static void __exit test_crc_exit(void) { } +module_exit (test_crc_exit); +MODULE_LICENSE ("GPL" );It's not the same as in SPDX.
Nice catch, I will change it to MODULE_LICENSE("GPL v2").
Thanks for all your review !
Coly Li
Quoted 40 lines · Coly Li
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.
Coly Li
Cc Andy Shevchenko <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[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]>---
Andy Shevchenko (1):
lib/crc64: add crc64 option to lib/KconfigColy Li (3):
lib: add crc64 calculation routines
bcache: use routines fromlib/crc64.cfor CRC64 calculationlib/test_crc: Add test cases for crc calculationdrivers/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 ++++
Quoted 9 lines
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
Actually, I would merge patch 1 & 2 together (pointless to introduce an option for which no code exists).
Otherwise for the whole series:
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton HRB 21284 (AG Nürnberg)
Quoted 42 lines · Hannes Reinecke
Quoted 41 lines · Coly Li
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.
Coly Li
Cc Andy Shevchenko <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Luis R. Rodriguez <[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]>---
Andy Shevchenko (1):
lib/crc64: add crc64 option to lib/KconfigColy Li (3):
lib: add crc64 calculation routines
bcache: use routines fromlib/crc64.cfor CRC64 calculationlib/test_crc: Add test cases for crc calculationdrivers/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 +++++++++++++++++++
Quoted 8 lines
Quoted 8 lines
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
Hi Hannes,
Quoted 2 lines
Actually, I would merge patch 1 & 2 together (pointless to introduce an option for which no code exists).
The patch 1 was originally from Andy Shevchenko, and I want to keep his contribute. If it seems not a good idea from me, I will merge them into 1 patch in v2 series.
Quoted 3 lines
Otherwise for the whole series:
Reviewed-by Hannes Reinecke <[email protected]>
Thanks.
Coly Li
Quoted 6 lines · Coly Li
Quoted 3 lines · Hannes Reinecke
Actually, I would merge patch 1 & 2 together (pointless to introduce an
option for which no code exists).
I was just about to say the same.
Quoted 5 lines
The patch 1 was originally from Andy Shevchenko, and I want to keep his
contribute. If it seems not a good idea from me, I will merge them into
1 patch in v2 series.
We have Co-developed-by tag, for example, which you may use here (if you choose this way, leave my SoB as well).
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Coly Li
Quoted 3 lines · Hannes Reinecke
Actually, I would merge patch 1 & 2 together (pointless to introduce an
option for which no code exists).I was just about to say the same.
Quoted 9 lines
Quoted 5 lines
The patch 1 was originally from Andy Shevchenko, and I want to keep his
contribute. If it seems not a good idea from me, I will merge them into
1 patch in v2 series.We have Co-developed-by tag, for example, which you may use here (if you choose this way, leave my SoB as well).
Thanks for the hint. I add a Co-developed-by tag for you :-)
Coly Li