Add KUnit tests for lib/crc16.c
Subsystems (2)
cover Add KUnit tests for lib/crc16.c
Hi all,
This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).
We noticed there were tests for CRC32 in lib/crc32test.c and thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).
Although this patch turned out quite big, most of the LOCs come from tables containing randomly-generated test data that we use to validate the kernel's implementation of CRC-16.
We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)
Vinicius Peixoto (1):
lib/crc16_kunit.c: add KUnit tests for crc16
lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/crc16_kunit.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 724 insertions(+) create mode 100644 lib/crc16_kunit.c
Signature · 1 line
2.43.0
PATCH 1/1 lib/crc16_kunit.c: add KUnit tests for crc16
Add Kunit tests for the kernel's implementation of the standard CRC-16 algorithm (<linux/crc16.h>). The test data consists of 100 randomly-generated test cases, validated against a reference implementation.
This implementation follows roughly the same logic as lib/crc32test.c, but without the performance measurements.
--- lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/crc16_kunit.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 724 insertions(+) create mode 100644 lib/crc16_kunit.c
lib/Kconfig.debug +8 -0
@@ -2835,6 +2835,14 @@ config USERCOPY_KUNIT_TEST on the copy_to/from_user infrastructure, making sure basic user/kernel boundary testing is working. +config CRC16_KUNIT_TEST + tristate "KUnit tests for CRC16" + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable this option to run unit tests for the kernel's CRC16 + implementation (<linux/crc16.h>). + config TEST_UDELAY tristate "udelay test driver" help
lib/Makefile +1 -0
@@ -389,6 +389,7 @@ CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o +obj-$(CONFIG_CRC16_KUNIT_TEST) += crc16_kunit.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
lib/crc16_kunit.c new file +715 -0
715 lines not on this page · raw message
Signature · 1 line
2.43.0
Hey!
Quoted 8 lines · Vinicius Peixoto
Hi all,
This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).
Quoted 7 lines
We noticed there were tests for CRC32 in
lib/crc32test.cand thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).Although this patch turned out quite big, most of the LOCs come from tables containing randomly-generated test data that we use to validate the kernel's implementation of CRC-16.
Can you share how you created the tables? Given that is impossible to review the table itself, at least people will be able to see how they got created at least.
Quoted 12 lines
We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)
Vinicius Peixoto (1):
lib/crc16_kunit.c: add KUnit tests for crc16lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/crc16_kunit.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 724 insertions(+)create mode 100644
lib/crc16_kunit.c
Hi André,
Quoted 11 lines · André Almeida
Hey!
Quoted 8 lines · Vinicius Peixoto
Hi all,
This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).
Quoted 8 lines
Quoted 7 lines
We noticed there were tests for CRC32 in
lib/crc32test.cand thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).Although this patch turned out quite big, most of the LOCs come from tables containing randomly-generated test data that we use to validate the kernel's implementation of CRC-16.
Can you share how you created the tables? Given that is impossible to review the table itself, at least people will be able to see how they got created at least.
Yes, of course, that was an oversight on my part. I'll make sure to add a more detailed explanation in the cover letter/commit message for the next revisions. Thanks for the suggestion!
This test follows lib/crc32test.c very closely; the data table is filled with 4096 random bytes, and the idea is to calculate several checksums within it by randomly choosing a i) start offset within the data buffer, ii) number of bytes after the start offset and iii) input CRC.
The checksums for the randomly-generated test cases were calculated using a reference implementation [1] and this test compares them against the values yielded by the kernel's implementation.
Thanks,
Vinicius
[1] https://github.com/lammertb/libcrc/blob/master/src/crc16.c
Quoted 12 lines
Quoted 12 lines
We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)
Vinicius Peixoto (1):
lib/crc16_kunit.c: add KUnit tests for crc16
lib/Kconfig.debug| 8 +
lib/Makefile | 1 +
lib/crc16_kunit.c| 715 ++++++++++++++++++++++++++++++++++++++++++++++3 files changed, 724 insertions(+)create mode 100644
lib/crc16_kunit.c
...
Quoted 3 lines
The checksums for the randomly-generated test cases were calculated using a reference implementation [1] and this test compares them against the values yielded by the kernel's implementation.
I'd just use a naïve implementation - doesn't really matter if it is a bit slow.
Slow is relative - this code only takes 35ms to crc-64 over 5MB of data.
{
volatile const uint32_t *r = (const void *)buf;
for (crc = 0; r < (const uint32_t *)buf_end; r++) {
uint64_t val = le32toh(*r);
crc ^= bswap64(val);
for (i = 0; i < 32; i++) {
if (crc & (1ull << 63))
crc = (crc << 1) ^ 0x42f0e1eba9ea3693ull;
else
crc = crc << 1;
}
}
}David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Hi David,
Quoted 7 lines · David Laight
...
Quoted 3 lines
The checksums for the randomly-generated test cases were calculated using a reference implementation [1] and this test compares them against the values yielded by the kernel's implementation.
I'd just use a naïve implementation - doesn't really matter if it is a bit slow.
Thanks for the feedback. I agree that it makes more sense to use a naive implementation to validate the results from the kernel's crc16 instead of having a table of pre-computed results. I will include in v2 a bog-standard implementation of crc16 similar to yours (using a loop instead of a lookup table) to validate the results.
Thanks,
Vinicius
Quoted 22 lines
Slow is relative - this code only takes 35ms to crc-64 over 5MB of data.
{ volatile const uint32_t *r = (const void *)buf; for (crc = 0; r < (const uint32_t *)buf_end; r++) { uint64_t val = le32toh(*r); crc ^= bswap64(val); for (i = 0; i < 32; i++) { if (crc & (1ull << 63)) crc = (crc << 1) ^ 0x42f0e1eba9ea3693ull; else crc = crc << 1; } } }David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
From: Vinicius Peixoto
Quoted 14 lines
Sent: 23 September 2024 00:27
Hi all,
This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).
We noticed there were tests for CRC32 in
lib/crc32test.cand thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).
Quoted 6 lines
Although this patch turned out quite big, most of the LOCs come from tables containing randomly-generated test data that we use to validate the kernel's implementation of CRC-16.
We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)
Would is be better to use a trivial PRNG to generate repeatable 'random enough' data, rather than having a large static array?
As a matter of interest, how in crc16 implemented (I know I could look). The code version:
uint32_tcrc_step(uint32_t crc, uint32_t byte_val)
{
uint32_t t = crc ^ (byte_val & 0xff);
t = (t ^ t << 4) & 0xff;
return crc >> 8 ^ t << 8 ^ t << 3 ^ t >> 4;
}may well be faster than a lookup table version.
Especially on modern multi-issue cpu and/or for small buffers where the lookup table won't necessarily be resident in the D-cache.
It is slightly slower than the table lookup on the simple Nios-II cpu. But we use a custom instruction to do it in one clock.
David
Quoted 4 lines
Vinicius Peixoto (1):
lib/crc16_kunit.c: add KUnit tests for crc16
Quoted 9 lines
lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/crc16_kunit.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 724 insertions(+) create mode 100644 lib/crc16_kunit.c--
2.43.0
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Hi David,
Quoted 19 lines · David Laight
From: Vinicius Peixoto
Quoted 18 lines
Sent: 23 September 2024 00:27
Hi all,
This patch was developed during a hackathon organized by LKCAMP [1], with the objective of writing KUnit tests, both to introduce people to the kernel development process and to learn about different subsystems (with the positive side effect of improving the kernel test coverage, of course).
We noticed there were tests for CRC32 in
lib/crc32test.cand thought it would be nice to have something similar for CRC16, since it seems to be widely used in network drivers (as well as in some ext4 code).Although this patch turned out quite big, most of the LOCs come from tables containing randomly-generated test data that we use to validate the kernel's implementation of CRC-16.
Quoted 5 lines
Quoted 2 lines
We would really appreciate any feedback/suggestions on how to improve this. Thanks! :-)
Would is be better to use a trivial PRNG to generate repeatable 'random enough' data, rather than having a large static array?
That's fair, the big static arrays are indeed very unwieldy. I reworked it to use next_pseudo_random32 (from include/linux/prandom.h) to fill the tables with pseudorandom data before running the tests, and will send a v2 soon.
The LOC count is a lot smaller, although it is still using static tables to store the data and the tests (I thought it would be simpler than allocating them at runtime). Do you think this is acceptable?
Quoted 11 lines
As a matter of interest, how in crc16 implemented (I know I could look). The code version:
uint32_tcrc_step(uint32_t crc, uint32_t byte_val){ uint32_t t = crc ^ (byte_val & 0xff); t = (t ^ t << 4) & 0xff; return crc >> 8 ^ t << 8 ^ t << 3 ^ t >> 4; }
Quoted 3 lines
may well be faster than a lookup table version.
Especially on modern multi-issue cpu and/or for small buffers where the lookup table won't necessarily be resident in the D-cache.
lib/crc16.c does use a lookup table. I haven't had the time to run benchmarks testing whether this version is faster, but I'm curious as well, so I'll look into it.
Thanks,
Vinicius
Quoted 23 lines
It is slightly slower than the table lookup on the simple Nios-II cpu. But we use a custom instruction to do it in one clock.
David
Quoted 4 lines
Vinicius Peixoto (1):
lib/crc16_kunit.c: add KUnit tests for crc16Quoted 9 lines
lib/Kconfig.debug | 8 + lib/Makefile | 1 + lib/crc16_kunit.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 724 insertions(+)create mode 100644
lib/crc16_kunit.c--
2.43.0-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)