Skip to contentSkip to conversation

add crc64 calculation as kernel library

PATCH In review Coly Li 3 participants 6 messages Jul 26, 2018 – Dec 29, 2018

Subsystems (4)
lore
Share & tools
lore

Coly Li

author
#0 lore raw

v5 cover add crc64 calculation as kernel library

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

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

CRC calculation consistency testing is not trvial, it will be another separated patch set and not in this series any more.

Coly Li

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

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

Coly Li (2):
lib: add crc64 calculation routines
bcache: use routines from lib/crc64.c for CRC64 calculation

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

PATCH v5 1/2 lib: add crc64 calculation routines

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

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

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

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

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

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

56 lines not on this page · raw message

lib/gen_crc64table.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v5 1/2] lib: add crc64 calculation routines

Quoted 17 lines · On Thu, 26 Jul 2018 13:33:51 +0800 Coly Li <[email protected]> wrote:

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

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

Quoted 3 lines

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

delta from v4:

include/linux/crc64.h renamed from include/linux/crc64.h~lib-add-crc64-calculation-routines-v5 +1 -1
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * See lib/crc64.c for the related specification and polynomical arithmetic.
+ * See lib/crc64.c for the related specification and polynomial arithmetic.
  */
 #ifndef _LINUX_CRC64_H
 #define _LINUX_CRC64_H
lib/crc64.c renamed from lib/crc64.c~lib-add-crc64-calculation-routines-v5 +3 -3
@@ -12,7 +12,7 @@
  * from,
  * http://www.ross.net/crc/download/crc_v3.txt
  *
- * crc64table[256] is the lookup table of a table-driver 64-bit CRC
+ * crc64table[256] is the lookup table of a table-driven 64-bit 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,
@@ -35,8 +35,8 @@ MODULE_LICENSE("GPL v2");
 
 /**
  * crc64_be - Calculate bitwise big-endian ECMA-182 CRC64
- * @crc: seed value for computation. 0 for a new CRC computing, or the
- *	previous crc64 value if computing incrementally.
+ * @crc: seed value for computation. 0 or (u64)~0 for a new CRC calculation,
+	or the previous crc64 value if computing incrementally.
  * @p: pointer to buffer over which CRC64 is run
  * @len: length of buffer @p
  */
lib/gen_crc64table.c renamed from lib/gen_crc64table.c~lib-add-crc64-calculation-routines-v5 +4 -4
@@ -1,13 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Generate lookup table for the talbe-driven CRC64 calculation.
+ * Generate lookup table for the table-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.
+ * the table-driven CRC64 calculation.
  *
  * See lib/crc64.c for more information about which specification
- * and polynomical arithmetic that gen_crc64table.c follows to
+ * and polynomial arithmetic that gen_crc64table.c follows to
  * generate the lookup table.
  *
  * Copyright 2018 SUSE Linux.
@@ -20,7 +20,7 @@
 
 #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
 
-static int64_t crc64_table[256] = {0};
+static uint64_t crc64_table[256] = {0};
 
 static void generate_crc64_table(void)
 {
#3 lore raw

Re: [PATCH v5 1/2] lib: add crc64 calculation routines

Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Thu, 26 Jul 2018 13:33:51 +0800 Coly Li <[email protected]> wrote:

This patch adds the re-write crc64 calculation routines for Linux kernel. The CRC64 polynomial 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.

Hi Coly and Andrew,

This patch breaks compilation if linux headers files are not installed (reported at https://github.com/gnubee-git/GnuBee_Docs/issues/75).
Following fixes it.

Thanks,
NeilBrown

From: NeilBrown <[email protected]>
Subject: [PATCH] lib: don't depend on linux headers being installed.

gen_crc64table requires linux include files to be installed in /usr/include/linux. This is a new requrement so hosts that could previously build the kernel, now cannot.

gen_crc64table makes this requirement by including <linux/swab.h>, but nothing from that header is actaully used.

So remove the #include, so that the linux headers no longer need to be installed.

Fixes feba04fd2cf8 ("lib: add crc64 calculation routines")
---
 lib/gen_crc64table.c | 2 --
 1 file changed, 2 deletions(-)
lib/gen_crc64table.c +0 -2
index 9011926e4162..094b43aef8db 100644
@@ -16,8 +16,6 @@
 #include <inttypes.h>
 #include <stdio.h>
 
-#include <linux/swab.h>
-
 #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
 
 static uint64_t crc64_table[256] = {0};
Signature · 1 line
2.14.0.rc0.dirty
author Acked-by
#4 lore raw

Re: [PATCH v5 1/2] lib: add crc64 calculation routines

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 11 lines · NeilBrown
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Thu, 26 Jul 2018 13:33:51 +0800 Coly Li <[email protected]> wrote:

This patch adds the re-write crc64 calculation routines for Linux kernel. The CRC64 polynomial 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.

Quoted 9 lines

Hi Coly and Andrew,

This patch breaks compilation if linux headers files are not installed (reported at
https://github.com/gnubee-git/GnuBee_Docs/issues/75). Following fixes it.

Thanks, NeilBrown

Hi Neil,

Thanks for the fix. I add by Acked-by.

Coly Li

Quoted 7 lines

From: NeilBrown <[email protected]> Subject: [PATCH] lib: don't depend on linux headers being installed.

gen_crc64table requires linux include files to be installed in /usr/include/linux. This is a new requrement so hosts that could previously build the kernel, now cannot.

Quoted 8 lines

gen_crc64table makes this requirement by including <linux/swab.h>, but nothing from that header is actaully used.

So remove the #include, so that the linux headers no longer need to be installed.

Fixes feba04fd2cf8 ("lib: add crc64 calculation routines")
Quoted 7 lines

--- lib/gen_crc64table.c | 2 -- 1 file changed, 2 deletions(-)

lib/gen_crc64table.c index renamed from lib/gen_crc64table.c +0 -0

9011926e4162..094b43aef8db 100644 --- a/lib/gen_crc64table.c +++ b/lib/gen_crc64table.c @@ -16,8 +16,6 @@ #include <inttypes.h> #include <stdio.h>

Quoted 5 lines

-#include <linux/swab.h> - #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL

static uint64_t crc64_table[256] = {0};

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIzBAEBCAAdFiEE6j5FL/T5SGCN6PrQxzkHk2t9+PwFAlwm8JsACgkQxzkHk2t9 +PxELA//U8FohU56Xty9Yfe/5IX0WXHzpLk8efz34xNodo1bavy9yIlpzG8l9cX5 c2vaM4hYvc2rrYokgkxHcANSpFkRzD9TjkwNbij7wd37lEJYzPWr0Qk1Ttuojs7b iJYLis0pbu6WTeUpM4NwdkKzylqWXg0dRrf3tNpF5ttN+LZOSEzbIiP8P2rnQUmD a9WCM6Ka/E9dP5H2CIvR6gAJL/BwsVyFgd6PiYy8ElMVYBpIu2uG34eO0tx6ebSE TPxWrSXY92jWCggJUBNr5zDXyBQADBQfy0YIv36zsplcolF/4mN4LWF2WCC4jp0c XLBrvSslzombjxq3h+zE4GNXsDNiKtaNQy1N30b1+N3ilXa4uav1wVvaBQBMwf4I QQDM8ZZ2alS5zAyzgtnbIr0AhlNEya+GO+42+rY0yrWG2Ffg3ONHSgylQ7hANVdk 2qAWugdXZLw/pqustIodIR3hjsWLc4zZDF+d6BywdB5WqSiS54u7ENSPkbGInKRR 8zprb2tjjjgwxL1MXF+6J1HG+GoTvEjjwjh26XDSiFbWCIX3hRjej+EjltKDARIo RWfX6/hTVnljkNUEpBfsi7DaiDVwkgo1OLQ3OSXgG9rZxs50ASMb4dBuN8wGdRrX jK3PF5m3ddVxCW2I5CFltd3qHlUFn+fmQojXtU6VSACgw0roQhw= =qnbZ
-----END PGP SIGNATURE-----

author
#5 lore raw

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

In reply to Coly Li

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

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

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

    and bch_crc64_update() based on crc64_be().

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

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

134 lines not on this page · raw message

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