riscv: Add fine-tuned checksum functions
v4 cover riscv: Add fine-tuned checksum functions
Each architecture generally implements fine-tuned checksum functions to leverage the instruction set. This patch adds the main checksum functions that are used in networking.
Vector support is included in this patch to start a discussion on that, it can probably be optimized more. The vector patches still need some work as they rely on GCC vector intrinsics types which cannot work in the kernel since it requires C vector support rather than just assembler support. I have tested the vector patches as standalone algorithms in QEMU.
This patch takes heavy use of the Zbb extension using alternatives patching.
To test this patch, enable the configs for KUNIT, then CHECKSUM_KUNIT and RISCV_CHECKSUM_KUNIT.
I have attempted to make these functions as optimal as possible, but I have not ran anything on actual riscv hardware. My performance testing has been limited to inspecting the assembly, running the algorithms on x86 hardware, and running in QEMU.
ip_fast_csum is a relatively small function so even though it is possible to read 64 bits at a time on compatible hardware, the bottleneck becomes the clean up and setup code so loading 32 bits at a time is actually faster.
---
Changes in v4:
- Suggestion by David Laight to use an improved checksum used in
arch/arc.
- Eliminates zero-extension on rv32, but not on rv64.
- Reduces data dependency which should improve execution speed on
rv32 and rv64
- Still passes CHECKSUM_KUNIT and RISCV_CHECKSUM_KUNIT on rv32 and
rv64 with and without zbb.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Use riscv_has_extension_likely and has_vector where possible (Conor)
- Reduce ifdefs by using IS_ENABLED where possible (Conor)
- Use kernel_vector_begin in the vector code (Samuel)
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- After more benchmarking, rework functions to improve performance.
- Remove tests that overlapped with the already existing checksum
tests and make tests more extensive.
- Use alternatives to activate code with Zbb and vector extensions
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Charlie Jenkins (5):
riscv: Checksum header
riscv: Add checksum library
riscv: Vector checksum header
riscv: Vector checksum library
riscv: Test checksum functions
arch/riscv/Kconfig.debug | 1 + arch/riscv/include/asm/checksum.h | 181 +++++++++++++++++++ arch/riscv/lib/Kconfig.debug | 31 ++++ arch/riscv/lib/Makefile | 3 + arch/riscv/lib/csum.c | 302 +++++++++++++++++++++++++++++++ arch/riscv/lib/riscv_checksum_kunit.c | 330 ++++++++++++++++++++++++++++++++++ 6 files changed, 848 insertions(+)
---
base-commit: af3c30d33476bc2694b0d699173544b07f7ae7de
change-id: 20230804-optimize_checksum-db145288ac21
Signature · 7 lines
- Charlie _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 1/5 riscv: Checksum header
Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking.
--- arch/riscv/include/asm/checksum.h | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
arch/riscv/include/asm/checksum.h new file +95 -0
@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* IP checksum routines +* +* Copyright (C) 2023 Rivos Inc. +*/ +#ifndef __ASM_RISCV_CHECKSUM_H +#define __ASM_RISCV_CHECKSUM_H + +#include <linux/in6.h> +#include <linux/uaccess.h> + +#ifdef CONFIG_32BIT +typedef unsigned int csum_t ; +#else +typedef unsigned long csum_t ; +#endif + +/* +* Fold a partial checksum without adding pseudo headers +*/ +static inline __sum16 csum_fold (__wsum sum) +{ +return (~sum -ror32 (sum,16 )) >>16 ; +} + +#define csum_fold csum_fold + +/* +* Quickly compute an IP checksum with the assumption that IPv4 headers will +* always be in multiples of 32-bits, and have an ihl of at least 5. +* @ihl is the number of 32 bit segments and must be greater than or equal to 5. +* @iph is assumed to be word aligned. +*/ +static inline __sum16 ip_fast_csum (const void *iph,unsigned int ihl) +{ +csum_t csum =0 ; +int pos =0 ; + +do { + csum += ((const unsigned int *)iph)[pos]; +if (IS_ENABLED (CONFIG_32BIT )) + csum += csum < ((const unsigned int *)iph)[pos]; + }while (++pos < ihl); + +/* +* ZBB only saves three instructions on 32-bit and five on 64-bit so not +* worth checking if supported without Alternatives. +*/ +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + +if (IS_ENABLED (CONFIG_32BIT )) { + asm(".option push \n\ +.option arch,+zbb \n\ +not %[fold_temp], %[csum] \n\ +rori %[csum], %[csum], 16 \n\ +sub %[csum], %[fold_temp], %[csum] \n\ +.option pop" + : [csum]"+r" (csum), [fold_temp]"=&r" (fold_temp)); + }else { + asm(".option push \n\ +.option arch,+zbb \n\ +rori %[fold_temp], %[csum], 32 \n\ +add %[csum], %[fold_temp], %[csum] \n\ +srli %[csum], %[csum], 32 \n\ +not %[fold_temp], %[csum] \n\ +roriw %[csum], %[csum], 16 \n\ +subw %[csum], %[fold_temp], %[csum] \n\ +.option pop" + : [csum]"+r" (csum), [fold_temp]"=&r" (fold_temp)); + } +return csum >>16 ; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ; +#endif +return csum_fold ((__force __wsum )csum); +} + +#define ip_fast_csum ip_fast_csum + +#include <asm-generic/checksum.h> + +#endif // __ASM_RISCV_CHECKSUM_H
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v4 1/5] riscv: Checksum header
Quoted 22 lines · Charlie Jenkins
Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/include/asm/checksum.h | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)arch/riscv/include/asm/checksum.h new file +7
-0@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* IP checksum routines +* +* Copyright (C) 2023 Rivos Inc. +*/ +#ifndef __ASM_RISCV_CHECKSUM_H
arch/riscv/include/asm/checksum.h line 17
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+#define __ASM_RISCV_CHECKSUM_H + +#include <linux/in6.h> +#include <linux/uaccess.h> + +#ifdef CONFIG_32BIT +typedef unsigned int csum_t ; +#else +typedef unsigned long csum_t ; +#endif
Hi Charlie,
Isn't unsigned long already 32bit on 32bit RISC-V, so why is this #ifdef needed?
Quoted 59 lines
arch/riscv/include/asm/checksum.h new file +59
-0+ +/* +* Fold a partial checksum without adding pseudo headers +*/ +static inline __sum16 csum_fold (__wsum sum) +{ +return (~sum -ror32 (sum,16 )) >>16 ; +} + +#define csum_fold csum_fold + +/* +* Quickly compute an IP checksum with the assumption that IPv4 headers will +* always be in multiples of 32-bits, and have an ihl of at least 5. +* @ihl is the number of 32 bit segments and must be greater than or equal to 5. +* @iph is assumed to be word aligned. +*/ +static inline __sum16 ip_fast_csum (const void *iph,unsigned int ihl) +{ +csum_t csum =0 ; +int pos =0 ; + +do { + csum += ((const unsigned int *)iph)[pos]; +if (IS_ENABLED (CONFIG_32BIT )) + csum += csum < ((const unsigned int *)iph)[pos]; + }while (++pos < ihl); + +/* +* ZBB only saves three instructions on 32-bit and five on 64-bit so not +* worth checking if supported without Alternatives. +*/ +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + +if (IS_ENABLED (CONFIG_32BIT )) { + asm(".option push \n\ +.option arch,+zbb \n\ +not %[fold_temp], %[csum] \n\ +rori %[csum], %[csum], 16 \n\ +sub %[csum], %[fold_temp], %[csum] \n\ +.option pop" + : [csum]"+r" (csum), [fold_temp]"=&r" (fold_temp)); + }else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\
arch/riscv/include/asm/checksum.h line 86
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+ subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return csum >> 16; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ;
The indentation seems off here.
/Emil
Quoted 18 lines
arch/riscv/include/asm/checksum.h new file +9
-1+#endif + return csum_fold((__force __wsum)csum); +} + +#define ip_fast_csum ip_fast_csum + +#include <asm-generic/checksum.h> + +#endif // __ASM_RISCV_CHECKSUM_H --2.42.0
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.inradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v4 1/5] riscv: Checksum header
Quoted 36 lines · Emil Renner Berthing
Quoted 22 lines · Charlie Jenkins
Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/include/asm/checksum.h | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)arch/riscv/include/asm/checksum.h new file +7
-0@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* IP checksum routines +* +* Copyright (C) 2023 Rivos Inc. +*/ +#ifndef __ASM_RISCV_CHECKSUM_Harch/riscv/include/asm/checksum.h line 17
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+#define __ASM_RISCV_CHECKSUM_H + +#include <linux/in6.h> +#include <linux/uaccess.h> + +#ifdef CONFIG_32BIT +typedef unsigned int csum_t ; +#else +typedef unsigned long csum_t ; +#endif Hi Charlie,
Quoted 2 lines
Isn't unsigned long already 32bit on 32bit RISC-V, so why is this #ifdef needed?
Oh, I wasn't sure so I ran sizeof(long) in qemu-system-riscv32 and it gave me 8 so assumed a long was 8 bytes. Do you think it would make what is going on more clear if I use u32 and u64 or would you recommend just using long?
Quoted 93 lines
Quoted 60 lines
arch/riscv/include/asm/checksum.h new file +59
-0+ +/* +* Fold a partial checksum without adding pseudo headers +*/ +static inline __sum16 csum_fold (__wsum sum) +{ +return (~sum -ror32 (sum,16 )) >>16 ; +} + +#define csum_fold csum_fold + +/* +* Quickly compute an IP checksum with the assumption that IPv4 headers will +* always be in multiples of 32-bits, and have an ihl of at least 5. +* @ihl is the number of 32 bit segments and must be greater than or equal to 5. +* @iph is assumed to be word aligned. +*/ +static inline __sum16 ip_fast_csum (const void *iph,unsigned int ihl) +{ +csum_t csum =0 ; +int pos =0 ; + +do { + csum += ((const unsigned int *)iph)[pos]; +if (IS_ENABLED (CONFIG_32BIT )) + csum += csum < ((const unsigned int *)iph)[pos]; + }while (++pos < ihl); + +/* +* ZBB only saves three instructions on 32-bit and five on 64-bit so not +* worth checking if supported without Alternatives. +*/ +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + +if (IS_ENABLED (CONFIG_32BIT )) { + asm(".option push \n\ +.option arch,+zbb \n\ +not %[fold_temp], %[csum] \n\ +rori %[csum], %[csum], 16 \n\ +sub %[csum], %[fold_temp], %[csum] \n\ +.option pop" + : [csum]"+r" (csum), [fold_temp]"=&r" (fold_temp)); + }else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\arch/riscv/include/asm/checksum.h line 86
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+ subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return csum >> 16; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ;The indentation seems off here.
/Emil
Quoted 18 lines
arch/riscv/include/asm/checksum.h new file +9
-1+#endif + return csum_fold((__force __wsum)csum); +} + +#define ip_fast_csum ip_fast_csum + +#include <asm-generic/checksum.h> + +#endif // __ASM_RISCV_CHECKSUM_H --2.42.0
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.inradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v4 1/5] riscv: Checksum header
Quoted 37 lines · Charlie Jenkins
Quoted 36 lines · Emil Renner Berthing
Quoted 22 lines · Charlie Jenkins
Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/include/asm/checksum.h | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)arch/riscv/include/asm/checksum.h new file +7
-0@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* IP checksum routines +* +* Copyright (C) 2023 Rivos Inc. +*/ +#ifndef __ASM_RISCV_CHECKSUM_Harch/riscv/include/asm/checksum.h line 17
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+#define __ASM_RISCV_CHECKSUM_H + +#include <linux/in6.h> +#include <linux/uaccess.h> + +#ifdef CONFIG_32BIT +typedef unsigned int csum_t ; +#else +typedef unsigned long csum_t ; +#endif Hi Charlie,
Quoted 6 lines
Quoted 2 lines
Isn't unsigned long already 32bit on 32bit RISC-V, so why is this #ifdef needed?
Oh, I wasn't sure so I ran sizeof(long) in qemu-system-riscv32 and it gave me 8 so assumed a long was 8 bytes. Do you think it would make what is going on more clear if I use u32 and u64 or would you recommend just using long?
Yeah, it doesn't seem like csum_t is used anywhere else, so I'd just use unsigned long if all you want is a register sized unsigned value. It'll be more familiar and easier to read for most people.
Quoted 93 lines
Quoted 93 lines
Quoted 60 lines
arch/riscv/include/asm/checksum.h new file +59
-0+ +/* +* Fold a partial checksum without adding pseudo headers +*/ +static inline __sum16 csum_fold (__wsum sum) +{ +return (~sum -ror32 (sum,16 )) >>16 ; +} + +#define csum_fold csum_fold + +/* +* Quickly compute an IP checksum with the assumption that IPv4 headers will +* always be in multiples of 32-bits, and have an ihl of at least 5. +* @ihl is the number of 32 bit segments and must be greater than or equal to 5. +* @iph is assumed to be word aligned. +*/ +static inline __sum16 ip_fast_csum (const void *iph,unsigned int ihl) +{ +csum_t csum =0 ; +int pos =0 ; + +do { + csum += ((const unsigned int *)iph)[pos]; +if (IS_ENABLED (CONFIG_32BIT )) + csum += csum < ((const unsigned int *)iph)[pos]; + }while (++pos < ihl); + +/* +* ZBB only saves three instructions on 32-bit and five on 64-bit so not +* worth checking if supported without Alternatives. +*/ +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + +if (IS_ENABLED (CONFIG_32BIT )) { + asm(".option push \n\ +.option arch,+zbb \n\ +not %[fold_temp], %[csum] \n\ +rori %[csum], %[csum], 16 \n\ +sub %[csum], %[fold_temp], %[csum] \n\ +.option pop" + : [csum]"+r" (csum), [fold_temp]"=&r" (fold_temp)); + }else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\arch/riscv/include/asm/checksum.h line 86
Quoted 10 lines
arch/riscv/include/asm/checksum.h new file +10
-0+ subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return csum >> 16; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ;The indentation seems off here.
/Emil
Quoted 18 lines
arch/riscv/include/asm/checksum.h new file +9
-1+#endif + return csum_fold((__force __wsum)csum); +} + +#define ip_fast_csum ip_fast_csum + +#include <asm-generic/checksum.h> + +#endif // __ASM_RISCV_CHECKSUM_H --2.42.0
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.inradead.org/mailman/listinfo/linux-riscv
PATCH v4 2/5 riscv: Add checksum library
In reply to Charlie JenkinsProvide a 32 and 64 bit version of do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compiling csum_ipv6_magic (64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads.
--- arch/riscv/include/asm/checksum.h | 11 ++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/csum.c | 210 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+)
arch/riscv/include/asm/checksum.h +11 -0
@@ -16,6 +16,14 @@ typedef unsigned int csum_t;typedef unsigned long csum_t ;#endif +/* Default version is sufficient for 32 bit */ +#ifdef CONFIG_64BIT +#define _HAVE_ARCH_IPV6_CSUM +__sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum sum); +#endif +/* * Fold a partial checksum without adding pseudo headers */
@@ -90,6 +98,9 @@ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)#define ip_fast_csum ip_fast_csum +extern unsigned int do_csum (const unsigned char *buff,int len); +#define do_csum do_csum +#include <asm-generic/checksum.h> #endif // __ASM_RISCV_CHECKSUM_H
arch/riscv/lib/Makefile +1 -0
@@ -6,6 +6,7 @@ lib-y += memmove.o lib-y += strcmp.o lib-y += strlen.o lib-y += strncmp.o +lib-y += csum.o lib-$(CONFIG_MMU) += uaccess.o lib-$(CONFIG_64BIT) += tishift.o lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.o
arch/riscv/lib/csum.c new file +210 -0
210 lines not on this page · raw message
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
RE: [PATCH v4 2/5] riscv: Add checksum library
From: Charlie Jenkins
Quoted 9 lines
Sent: 11 September 2023 23:57
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads.
...
Quoted 9 lines
arch/riscv/lib/csum.c new file +9
-0+ sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ];
arch/riscv/lib/csum.c line 52
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4;
Have you got gcc to compile that as-is?
Whenever I've tried to get a 'tree add' compiled so that the early adds can be executed in parallel gcc always pessimises it to a linear sequence of adds.
But I agree that adding 32bit values to a 64bit register may be no slower than trying to do an 'add carry' sequence that is guaranteed to only do one add/clock.
(And on Intel cpu from core-2 until IIRC Haswell adc took 2 clocks!)
IIRC RISCV doesn't have a carry flag, so the adc sequence is hard - probably takes two extra instructions per value. Although with parallel execute it may not matter. Consider:
val = buf[offset]; sum += val; carry += sum < val; val = buf[offset1]; sum += val; ...
the compare and 'carry +=' can be executed at the same time as the following two instructions.
You do then a final sum += carry; sum += sum < carry;
Assuming all instructions are 1 clock and any read delays get filled with other instructions (by source or hardware instruction re-ordering) even without parallel execute that is 4 clocks for 64 bits, which is much the same as the 2 clocks for 32 bits.
Remember that all the 32bit values can summed first as they won't overflow.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 62 lines · David Laight
From: Charlie Jenkins
Quoted 9 lines
Sent: 11 September 2023 23:57
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads....
Quoted 9 lines
arch/riscv/lib/csum.c new file +9
-0+ sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ];arch/riscv/lib/csum.c line 52
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4;Have you got gcc to compile that as-is?
Whenever I've tried to get a 'tree add' compiled so that the early adds can be executed in parallel gcc always pessimises it to a linear sequence of adds.
But I agree that adding 32bit values to a 64bit register may be no slower than trying to do an 'add carry' sequence that is guaranteed to only do one add/clock.
(And on Intel cpu from core-2 until IIRC Haswell adc took 2 clocks!)IIRC RISCV doesn't have a carry flag, so the adc sequence is hard - probably takes two extra instructions per value. Although with parallel execute it may not matter. Consider:
val = buf[offset]; sum += val; carry += sum < val; val = buf[offset1]; sum += val; ...the compare and 'carry +=' can be executed at the same time as the following two instructions.
You do then a final sum += carry; sum += sum < carry;Assuming all instructions are 1 clock and any read delays get filled with other instructions (by source or hardware instruction re-ordering) even without parallel execute that is 4 clocks for 64 bits, which is much the same as the 2 clocks for 32 bits.
Quoted 8 lines
Remember that all the 32bit values can summed first as they won't overflow.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Yeah it does seem like the tree-add does just do a linear add. All three of them were pretty much the same on riscv so I used the version that did best on x86 with the knowledge that my QEMU setup does not accurately represent real hardware.
I don't quite understand how doing the carry in the middle of each stage, even though it can be executed at the same time, would be faster than just doing a single overflow check at the end. I can just revert back to the non-tree add version since there is no improvement on riscv. I can also revert back to the default version that uses carry += sum < val as well.
- Charlie
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
RE: [PATCH v4 2/5] riscv: Add checksum library
From: Charlie Jenkins
Quoted 68 lines
Sent: 13 September 2023 04:10
Quoted 65 lines · David Laight
From: Charlie Jenkins
Quoted 9 lines
Sent: 11 September 2023 23:57
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads....
Quoted 9 lines
arch/riscv/lib/csum.c new file +9
-0+ sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ];arch/riscv/lib/csum.c line 52
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4;Have you got gcc to compile that as-is?
Whenever I've tried to get a 'tree add' compiled so that the early adds can be executed in parallel gcc always pessimises it to a linear sequence of adds.
But I agree that adding 32bit values to a 64bit register may be no slower than trying to do an 'add carry' sequence that is guaranteed to only do one add/clock.
(And on Intel cpu from core-2 until IIRC Haswell adc took 2 clocks!)IIRC RISCV doesn't have a carry flag, so the adc sequence is hard - probably takes two extra instructions per value. Although with parallel execute it may not matter. Consider:
val = buf[offset]; sum += val; carry += sum < val; val = buf[offset1]; sum += val; ...the compare and 'carry +=' can be executed at the same time as the following two instructions.
You do then a final sum += carry; sum += sum < carry;Assuming all instructions are 1 clock and any read delays get filled with other instructions (by source or hardware instruction re-ordering) even without parallel execute that is 4 clocks for 64 bits, which is much the same as the 2 clocks for 32 bits.
Remember that all the 32bit values can summed first as they won't overflow.
Quoted 6 lines
Quoted 1 line
David
Yeah it does seem like the tree-add does just do a linear add. All three of them were pretty much the same on riscv so I used the version that did best on x86 with the knowledge that my QEMU setup does not accurately represent real hardware.
The problem there is that any measurement on x86 has pretty much no relevance to what any RISCV cpu might do.
The multiple execution units and out of order execution on x86 are far different from anything any RISCV cpu is likely to have for many years.
You might get nearer running on one of the Atom cpu - but it won't really match.
There are too many fundamental differences between the architectures.
All you can do is to find and read the instruction timings for a target physical cpu and look for things like:
- Whether arithmetic results are available next clock. (It probably is)
- How many clocks it takes for read data to be available. I suspect the cpu will stall if the data is needed.
A block of sequential reads is one way to avoid the stall. On x86 the instruction that needs the data is just deferred until it is available, the following instructions execute (provided their input are all available).
- Clock delays for taken/not taken predicted/not predicted branches.
Quoted 3 lines
I don't quite understand how doing the carry in the middle of each stage, even though it can be executed at the same time, would be faster than just doing a single overflow check at the end.
You need to do half as many reads and adds.
Quoted 2 lines
I can just revert
back to the non-tree add version since there is no improvement on riscv.
The 'tree' version is only likely to be faster on cpu (like x86) that can (at least sometimes) do two memory reads in one clock and can do two adds and two read in the same clock.
Even then, without out of order execution, it is hard to get right.
Oh, you might want to try getting the default csum_fold() to be the faster 'arc' version rather than adding your own version.
David
Quoted 4 lines
I can also revert back to the default version that uses carry += sum < val as well.
- Charlie
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 97 lines · David Laight
From: Charlie Jenkins
Quoted 68 lines
Sent: 13 September 2023 04:10
Quoted 65 lines · David Laight
From: Charlie Jenkins
Quoted 9 lines
Sent: 11 September 2023 23:57
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads....
Quoted 9 lines
arch/riscv/lib/csum.c new file +9
-0+ sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ];arch/riscv/lib/csum.c line 52
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4;Have you got gcc to compile that as-is?
Whenever I've tried to get a 'tree add' compiled so that the early adds can be executed in parallel gcc always pessimises it to a linear sequence of adds.
But I agree that adding 32bit values to a 64bit register may be no slower than trying to do an 'add carry' sequence that is guaranteed to only do one add/clock.
(And on Intel cpu from core-2 until IIRC Haswell adc took 2 clocks!)IIRC RISCV doesn't have a carry flag, so the adc sequence is hard - probably takes two extra instructions per value. Although with parallel execute it may not matter. Consider:
val = buf[offset]; sum += val; carry += sum < val; val = buf[offset1]; sum += val; ...the compare and 'carry +=' can be executed at the same time as the following two instructions.
You do then a final sum += carry; sum += sum < carry;Assuming all instructions are 1 clock and any read delays get filled with other instructions (by source or hardware instruction re-ordering) even without parallel execute that is 4 clocks for 64 bits, which is much the same as the 2 clocks for 32 bits.
Remember that all the 32bit values can summed first as they won't overflow.
Quoted 6 lines
Quoted 1 line
David
Yeah it does seem like the tree-add does just do a linear add. All three of them were pretty much the same on riscv so I used the version that did best on x86 with the knowledge that my QEMU setup does not accurately represent real hardware.
The problem there is that any measurement on x86 has pretty much no relevance to what any RISCV cpu might do.
The multiple execution units and out of order execution on x86 are far different from anything any RISCV cpu is likely to have for many years.
You might get nearer running on one of the Atom cpu - but it won't really match.
There are too many fundamental differences between the architectures.All you can do is to find and read the instruction timings for a target physical cpu and look for things like:
- Whether arithmetic results are available next clock. (It probably is)
- How many clocks it takes for read data to be available. I suspect the cpu will stall if the data is needed.
A block of sequential reads is one way to avoid the stall. On x86 the instruction that needs the data is just deferred until it is available, the following instructions execute (provided their input are all available).
- Clock delays for taken/not taken predicted/not predicted branches.
Quoted 6 lines
Quoted 3 lines
I don't quite understand how doing the carry in the middle of each stage, even though it can be executed at the same time, would be faster than just doing a single overflow check at the end.
You need to do half as many reads and adds.
I missed that you were talking about 64-bit reads. I was talking to somebody about this a couple weeks ago and they mentioned a counter example that showed that adding the carry after was not the same as adding it in the middle. Even though addition is commutative, I wasn't sure if the overflow checking was. I can't rememeber what the counter example was, but I have a feeling it was flawed.
Quoted 22 lines
Quoted 2 lines
I can just revert
back to the non-tree add version since there is no improvement on riscv.The 'tree' version is only likely to be faster on cpu (like x86) that can (at least sometimes) do two memory reads in one clock and can do two adds and two read in the same clock.
Even then, without out of order execution, it is hard to get right.Oh, you might want to try getting the default
csum_fold()to be the faster 'arc' version rather than adding your own version.David
Quoted 4 lines
I can also revert back to the default version that uses carry += sum < val as well.
- Charlie
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 102 lines · Charlie Jenkins
Quoted 101 lines · David Laight
From: Charlie Jenkins
Quoted 68 lines
Sent: 13 September 2023 04:10
Quoted 65 lines · David Laight
From: Charlie Jenkins
Quoted 9 lines
Sent: 11 September 2023 23:57
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads....
Quoted 9 lines
arch/riscv/lib/csum.c new file +9
-0+ sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ];arch/riscv/lib/csum.c line 52
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4;Have you got gcc to compile that as-is?
Whenever I've tried to get a 'tree add' compiled so that the early adds can be executed in parallel gcc always pessimises it to a linear sequence of adds.
But I agree that adding 32bit values to a 64bit register may be no slower than trying to do an 'add carry' sequence that is guaranteed to only do one add/clock.
(And on Intel cpu from core-2 until IIRC Haswell adc took 2 clocks!)IIRC RISCV doesn't have a carry flag, so the adc sequence is hard - probably takes two extra instructions per value. Although with parallel execute it may not matter. Consider:
val = buf[offset]; sum += val; carry += sum < val; val = buf[offset1]; sum += val; ...the compare and 'carry +=' can be executed at the same time as the following two instructions.
You do then a final sum += carry; sum += sum < carry;Assuming all instructions are 1 clock and any read delays get filled with other instructions (by source or hardware instruction re-ordering) even without parallel execute that is 4 clocks for 64 bits, which is much the same as the 2 clocks for 32 bits.
Remember that all the 32bit values can summed first as they won't overflow.
Quoted 6 lines
Quoted 1 line
David
Yeah it does seem like the tree-add does just do a linear add. All three of them were pretty much the same on riscv so I used the version that did best on x86 with the knowledge that my QEMU setup does not accurately represent real hardware.
The problem there is that any measurement on x86 has pretty much no relevance to what any RISCV cpu might do.
The multiple execution units and out of order execution on x86 are far different from anything any RISCV cpu is likely to have for many years.
You might get nearer running on one of the Atom cpu - but it won't really match.
There are too many fundamental differences between the architectures.All you can do is to find and read the instruction timings for a target physical cpu and look for things like:
- Whether arithmetic results are available next clock. (It probably is)
- How many clocks it takes for read data to be available. I suspect the cpu will stall if the data is needed.
A block of sequential reads is one way to avoid the stall. On x86 the instruction that needs the data is just deferred until it is available, the following instructions execute (provided their input are all available).
- Clock delays for taken/not taken predicted/not predicted branches.Quoted 3 lines
I don't quite understand how doing the carry in the middle of each stage, even though it can be executed at the same time, would be faster than just doing a single overflow check at the end.
Quoted 8 lines
Quoted 2 lines
You need to do half as many reads and adds.
I missed that you were talking about 64-bit reads. I was talking to somebody about this a couple weeks ago and they mentioned a counter example that showed that adding the carry after was not the same as adding it in the middle. Even though addition is commutative, I wasn't sure if the overflow checking was. I can't rememeber what the counter example was, but I have a feeling it was flawed.
Sorry to double respond to this. It seems like it is the same. However it seems like it is still slower. After cleaning up my benchmarking more, it seems like the best way to go is to use the 32-bit adds and accumulate the overflow in the upper 32 bits.
Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
I can just revert
back to the non-tree add version since there is no improvement on riscv.The 'tree' version is only likely to be faster on cpu (like x86) that can (at least sometimes) do two memory reads in one clock and can do two adds and two read in the same clock.
Even then, without out of order execution, it is hard to get right.Oh, you might want to try getting the default
csum_fold()to be the faster 'arc' version rather than adding your own version.
I do like this idea. I can extract out the changes into the default version.
- Charlie
Quoted 12 lines
Quoted 12 lines
David
Quoted 4 lines
I can also revert back to the default version that uses carry += sum < val as well.
- Charlie
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v4 2/5] riscv: Add checksum library
In reply to Charlie JenkinsQuoted 188 lines · Charlie Jenkins
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads.Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/include/asm/checksum.h | 11 ++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/csum.c | 210 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+)arch/riscv/include/asm/checksum.h +11
-0@@ -16,6 +16,14 @@ typedef unsigned int csum_t;typedef unsigned long csum_t ;#endif +/* Default version is sufficient for 32 bit */ +#ifdef CONFIG_64BIT +#define _HAVE_ARCH_IPV6_CSUM +__sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum sum); +#endif +/* * Fold a partial checksum without adding pseudo headers */ @@ -90,6 +98,9 @@ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)#define ip_fast_csum ip_fast_csum +extern unsigned int do_csum (const unsigned char *buff,int len); +#define do_csum do_csum +#include <asm-generic/checksum.h> #endif // __ASM_RISCV_CHECKSUM_H arch/riscv/lib/Makefile +1
-0@@ -6,6 +6,7 @@ lib-y += memmove.o lib-y += strcmp.o lib-y += strlen.o lib-y += strncmp.o +lib-y += csum.o lib-$(CONFIG_MMU) += uaccess.o lib-$(CONFIG_64BIT) += tishift.o lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.oarch/riscv/lib/csum.c new file +127
-0@@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* IP checksum library +* +* Influenced by arch/arm64/lib/csum.c +* Copyright (C) 2023 Rivos Inc. +*/ +#include <linux/bitops.h> +#include <linux/compiler.h> +#include <linux/kasan-checks.h> +#include <linux/kernel.h> + +#include <net/checksum.h> + +/* Default version is sufficient for 32 bit */ +#ifndef CONFIG_32BIT +__sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum csum) +{ +/* +* Inform the compiler/processor that the operation we are performing is +* "Commutative and Associative" by summing parts of the checksum in a +* tree-like structure (Section 2(A) of "Computing the Internet +* Checksum"). Furthermore, defer the overflow until the end of the +* computation which is shown to be valid in Section 2(C)(1) of the +* same handbook. +*/ +unsigned long sum, sum1, sum2, sum3, sum4, ulen, uproto; + + uproto =htonl (proto); + ulen =htonl (len); + + sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ]; + + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4; + +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +/* +* Zbb is likely available when the kernel is compiled with Zbb +* support, so nop when Zbb is available and jump when Zbb is +* not available. +*/ +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + asm(".option push \n\ +.option arch,+zbb \n\ +rori %[fold_temp], %[sum], 32 \n\ +add %[sum], %[fold_temp], %[sum] \n\ +srli %[sum], %[sum], 32 \n\ +not %[fold_temp], %[sum] \n\ +roriw %[sum], %[sum], 16 \n\ +subw %[sum], %[fold_temp], %[sum] \n\ +.option pop" + : [sum]"+r" (sum), [fold_temp]"=&r" (fold_temp)); +return (__force __sum16)(sum >>16 ); + } +no_zbb: + sum += (sum >>32 ) | (sum <<32 ); + sum >>=32 ; +return csum_fold ((__force __wsum )sum); +} +EXPORT_SYMBOL (csum_ipv6_magic); +#endif // !CONFIG_32BIT + +#ifdef CONFIG_32BIT +#define OFFSET_MASK 3 +#elif CONFIG_64BIT +#define OFFSET_MASK 7 +#endif + +/* +* Perform a checksum on an arbitrary memory address. +* Algorithm accounts for buff being misaligned. +* If buff is not aligned, will over-read bytes but not use the bytes that it +* shouldn't. The same thing will occur on the tail-end of the read. +*/ +unsigned int __no_sanitize_address do_csum(const unsigned char *buff, int len) +{ +unsigned int offset, shift; +csum_t csum, data; +const csum_t *ptr; + +if (unlikely (len <=0 )) +return 0 ; +/* +* To align the address, grab the whole first byte in buff. +* Since it is inside of a same byte, it will never cross pages or cache +* lines. +* Directly call KASAN with the alignment we will be using. +*/ + offset = (csum_t )buff &OFFSET_MASK ; +kasan_check_read (buff, len); + ptr = (const csum_t *)(buff - offset); + len = len + offset -sizeof (csum_t); + +/* +* Clear the most signifant bits that were over-read if buff was not +* aligned. +*/ + shift = offset *8 ; + data = *ptr; +#ifdef __LITTLE_ENDIAN + data = (data >> shift) << shift;
arch/riscv/lib/csum.c line 137
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+#else + data = (data << shift) >> shift; +#endif +/* +* Do 32-bit reads on RV32 and 64-bit reads otherwise. This should be +* faster than doing 32-bit reads on architectures that support larger +* reads. +*/ +while (len >0 ) { + csum += data;
arch/riscv/lib/csum.c:137:3: warning: variable 'csum' is uninitialized when used here [-Wuninitialized]
csum += data;
^~~~
arch/riscv/lib/csum.c:104:13: note: initialize the variable 'csum' to silence this warning
csum_t csum, data;
^
= 0Quoted 11 lines
arch/riscv/lib/csum.c new file +11
-0+ csum += csum < data; + len -=sizeof (csum_t); + ptr +=1 ; + data = *ptr; + } + +/* +* Perform alignment (and over-read) bytes on the tail if any bytes +* leftover. +*/ + shift = len *-8 ;
arch/riscv/lib/csum.c line 158
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+#ifdef __LITTLE_ENDIAN + data = (data << shift) >> shift; +#else + data = (data >> shift) << shift; +#endif + csum += data; + csum += csum < data; + +if (!riscv_has_extension_likely (RISCV_ISA_EXT_ZBB )) + goto no_zbb;
I think this is missing a change for IS_ENABLED(CONFIG_RISCV_ISA_ZBB)?arch/riscv/lib/csum.c:180:1: warning: unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or 'norelax' [-Winline-asm]
.option arch,+zbb \n\
^
<inline asm>:2:11: note: instantiated into assembly here
.option arch,+zbb
^
arch/riscv/lib/csum.c:181:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography)
rori %[fold_temp], %[csum], 32 \n\
^
<inline asm>:3:4: note: instantiated into assembly here
rori a2, a0, 32
^
arch/riscv/lib/csum.c:184:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography)
roriw %[fold_temp], %[csum], 16 \n\
^
<inline asm>:6:4: note: instantiated into assembly here
roriw a2, a0, 16
^
arch/riscv/lib/csum.c:188:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography)
rev8 %[csum], %[csum] \n\
^
<inline asm>:10:4: note: instantiated into assembly here
rev8 a0, a0
^2 warnings and 3 errors generated.
clang before 17 doesn't support .option arch, so the guard is required around any code using it. You've got the guard on the other .option arch user above, so that one seems to have escaped notice ;)
Going forward, it'd be good to test this stuff with non-latest clang to make sure you appropriately consider the !Zbb cases.
Quoted 34 lines
arch/riscv/lib/csum.c new file +34
-0+ +unsigned int fold_temp; + +if (IS_ENABLED (CONFIG_32BIT )) { +asm_volatile_goto (".option push \n\ +.option arch,+zbb \n\ +rori %[fold_temp], %[csum], 16 \n\ +andi %[offset], %[offset], 1 \n\ +add %[csum], %[fold_temp], %[csum] \n\ +beq %[offset], zero, %l[end] \n\ +rev8 %[csum], %[csum] \n\ +zext.h %[csum], %[csum] \n\ +.option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp) + : [offset] "r" (offset) + : + : end); + +return csum; + } else { + asm_volatile_goto(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + roriw %[fold_temp], %[csum], 16 \n\ + addw %[csum], %[fold_temp], %[csum] \n\ + andi %[offset], %[offset], 1 \n\ + beq %[offset], zero, %l[end] \n\ + rev8 %[csum], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + zext.h %[csum], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)
arch/riscv/lib/csum.c line 202
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ : [offset] "r" (offset) + : + : end); + +return csum; + } +end: +return csum >>16 ; +no_zbb: +#ifndef CONFIG_32BIT
These can also be moved to IS_ENABLED() FYI, since there's no 32-bit stuff here that'd break the build for 64-bit. Ditto elsewhere where you've got similar stuff.
Cheers,
Conor.
Quoted 12 lines
arch/riscv/lib/csum.c new file +8
-0+ csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ; +#endif + csum = (unsigned int )csum + (((unsigned int )csum >>16 ) | ((unsigned int )csum <<16 )); +if (offset &1 ) +return (unsigned short )swab32 (csum); +return csum >>16 ; +}Signature · 2 lines
-- 2.42.0
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 254 lines · Conor Dooley
Quoted 188 lines · Charlie Jenkins
Provide a 32 and 64 bit version of
do_csum. When compiled for 32-bit will load from the buffer in groups of 32 bits, and when compiled for 64-bit will load in groups of 64 bits. Benchmarking by proxy compilingcsum_ipv6_magic(64-bit version) for an x86 chip as well as running the riscv generated code in QEMU, discovered that summing in a tree-like structure is about 4% faster than doing 64-bit reads.Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/include/asm/checksum.h | 11 ++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/csum.c | 210 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+)arch/riscv/include/asm/checksum.h +11
-0@@ -16,6 +16,14 @@ typedef unsigned int csum_t;typedef unsigned long csum_t ;#endif +/* Default version is sufficient for 32 bit */ +#ifdef CONFIG_64BIT +#define _HAVE_ARCH_IPV6_CSUM +__sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum sum); +#endif +/* * Fold a partial checksum without adding pseudo headers */ @@ -90,6 +98,9 @@ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)#define ip_fast_csum ip_fast_csum +extern unsigned int do_csum (const unsigned char *buff,int len); +#define do_csum do_csum +#include <asm-generic/checksum.h> #endif // __ASM_RISCV_CHECKSUM_H arch/riscv/lib/Makefile +1
-0@@ -6,6 +6,7 @@ lib-y += memmove.o lib-y += strcmp.o lib-y += strlen.o lib-y += strncmp.o +lib-y += csum.o lib-$(CONFIG_MMU) += uaccess.o lib-$(CONFIG_64BIT) += tishift.o lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.oarch/riscv/lib/csum.c new file +127
-0@@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* IP checksum library +* +* Influenced by arch/arm64/lib/csum.c +* Copyright (C) 2023 Rivos Inc. +*/ +#include <linux/bitops.h> +#include <linux/compiler.h> +#include <linux/kasan-checks.h> +#include <linux/kernel.h> + +#include <net/checksum.h> + +/* Default version is sufficient for 32 bit */ +#ifndef CONFIG_32BIT +__sum16 csum_ipv6_magic (const struct in6_addr *saddr, +const struct in6_addr *daddr, +__u32 len,__u8 proto,__wsum csum) +{ +/* +* Inform the compiler/processor that the operation we are performing is +* "Commutative and Associative" by summing parts of the checksum in a +* tree-like structure (Section 2(A) of "Computing the Internet +* Checksum"). Furthermore, defer the overflow until the end of the +* computation which is shown to be valid in Section 2(C)(1) of the +* same handbook. +*/ +unsigned long sum, sum1, sum2, sum3, sum4, ulen, uproto; + + uproto =htonl (proto); + ulen =htonl (len); + + sum = saddr->s6_addr32[0 ]; + sum += saddr->s6_addr32[1 ]; + sum1 = saddr->s6_addr32[2 ]; + sum1 += saddr->s6_addr32[3 ]; + + sum2 = daddr->s6_addr32[0 ]; + sum2 += daddr->s6_addr32[1 ]; + sum3 = daddr->s6_addr32[2 ]; + sum3 += daddr->s6_addr32[3 ]; + + sum4 = csum; + sum4 += ulen; + sum4 += uproto; + + sum += sum1; + sum2 += sum3; + + sum += sum2; + sum += sum4; + +if (IS_ENABLED (CONFIG_RISCV_ISA_ZBB ) && +IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) { +csum_t fold_temp; + +/* +* Zbb is likely available when the kernel is compiled with Zbb +* support, so nop when Zbb is available and jump when Zbb is +* not available. +*/ +asm_volatile_goto (ALTERNATIVE ("j %l[no_zbb]" ,"nop" ,0 , +RISCV_ISA_EXT_ZBB ,1 ) + : + : + : + : no_zbb); + asm(".option push \n\ +.option arch,+zbb \n\ +rori %[fold_temp], %[sum], 32 \n\ +add %[sum], %[fold_temp], %[sum] \n\ +srli %[sum], %[sum], 32 \n\ +not %[fold_temp], %[sum] \n\ +roriw %[sum], %[sum], 16 \n\ +subw %[sum], %[fold_temp], %[sum] \n\ +.option pop" + : [sum]"+r" (sum), [fold_temp]"=&r" (fold_temp)); +return (__force __sum16)(sum >>16 ); + } +no_zbb: + sum += (sum >>32 ) | (sum <<32 ); + sum >>=32 ; +return csum_fold ((__force __wsum )sum); +} +EXPORT_SYMBOL (csum_ipv6_magic); +#endif // !CONFIG_32BIT + +#ifdef CONFIG_32BIT +#define OFFSET_MASK 3 +#elif CONFIG_64BIT +#define OFFSET_MASK 7 +#endif + +/* +* Perform a checksum on an arbitrary memory address. +* Algorithm accounts for buff being misaligned. +* If buff is not aligned, will over-read bytes but not use the bytes that it +* shouldn't. The same thing will occur on the tail-end of the read. +*/ +unsigned int __no_sanitize_address do_csum(const unsigned char *buff, int len) +{ +unsigned int offset, shift; +csum_t csum, data; +const csum_t *ptr; + +if (unlikely (len <=0 )) +return 0 ; +/* +* To align the address, grab the whole first byte in buff. +* Since it is inside of a same byte, it will never cross pages or cache +* lines. +* Directly call KASAN with the alignment we will be using. +*/ + offset = (csum_t )buff &OFFSET_MASK ; +kasan_check_read (buff, len); + ptr = (const csum_t *)(buff - offset); + len = len + offset -sizeof (csum_t); + +/* +* Clear the most signifant bits that were over-read if buff was not +* aligned. +*/ + shift = offset *8 ; + data = *ptr; +#ifdef __LITTLE_ENDIAN + data = (data >> shift) << shift;arch/riscv/lib/csum.c line 137
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+#else + data = (data << shift) >> shift; +#endif +/* +* Do 32-bit reads on RV32 and 64-bit reads otherwise. This should be +* faster than doing 32-bit reads on architectures that support larger +* reads. +*/ +while (len >0 ) { + csum += data;
arch/riscv/lib/csum.c:137:3: warning: variable 'csum' is uninitialized when used here [-Wuninitialized]csum += data; ^~~~ arch/riscv/lib/csum.c:104:13: note: initialize the variable 'csum' to silence this warning csum_t csum, data; ^ = 0Quoted 11 lines
arch/riscv/lib/csum.c new file +11
-0+ csum += csum < data; + len -=sizeof (csum_t); + ptr +=1 ; + data = *ptr; + } + +/* +* Perform alignment (and over-read) bytes on the tail if any bytes +* leftover. +*/ + shift = len *-8 ;arch/riscv/lib/csum.c line 158
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+#ifdef __LITTLE_ENDIAN + data = (data << shift) >> shift; +#else + data = (data >> shift) << shift; +#endif + csum += data; + csum += csum < data; + +if (!riscv_has_extension_likely (RISCV_ISA_EXT_ZBB )) + goto no_zbb;I think this is missing a change for
IS_ENABLED(CONFIG_RISCV_ISA_ZBB)?arch/riscv/lib/csum.c:180:1: warning: unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or 'norelax' [-Winline-asm].option arch,+zbb \n\ ^ <inline asm>:2:11: note: instantiated into assembly here .option arch,+zbb ^ arch/riscv/lib/csum.c:181:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography) rori %[fold_temp], %[csum], 32 \n\ ^ <inline asm>:3:4: note: instantiated into assembly here rori a2, a0, 32 ^ arch/riscv/lib/csum.c:184:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography) roriw %[fold_temp], %[csum], 16 \n\ ^ <inline asm>:6:4: note: instantiated into assembly here roriw a2, a0, 16 ^ arch/riscv/lib/csum.c:188:1: error: instruction requires the following: 'Zbb' (Basic Bit-Manipulation) or 'Zbkb' (Bitmanip instructions for Cryptography) rev8 %[csum], %[csum] \n\ ^ <inline asm>:10:4: note: instantiated into assembly here rev8 a0, a0 ^
Quoted 9 lines
2 warnings and 3 errors generated.
clang before 17 doesn't support
.option arch, so the guard is required around any code using it. You've got the guard on the other.option archuser above, so that one seems to have escaped notice ;)Going forward, it'd be good to test this stuff with non-latest clang to make sure you appropriately consider the !Zbb cases.
Yes this was an oversight to drop the guard.
Quoted 50 lines
Quoted 35 lines
arch/riscv/lib/csum.c new file +34
-0+ +unsigned int fold_temp; + +if (IS_ENABLED (CONFIG_32BIT )) { +asm_volatile_goto (".option push \n\ +.option arch,+zbb \n\ +rori %[fold_temp], %[csum], 16 \n\ +andi %[offset], %[offset], 1 \n\ +add %[csum], %[fold_temp], %[csum] \n\ +beq %[offset], zero, %l[end] \n\ +rev8 %[csum], %[csum] \n\ +zext.h %[csum], %[csum] \n\ +.option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp) + : [offset] "r" (offset) + : + : end); + +return csum; + } else { + asm_volatile_goto(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + roriw %[fold_temp], %[csum], 16 \n\ + addw %[csum], %[fold_temp], %[csum] \n\ + andi %[offset], %[offset], 1 \n\ + beq %[offset], zero, %l[end] \n\ + rev8 %[csum], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + zext.h %[csum], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)arch/riscv/lib/csum.c line 202
Quoted 10 lines
arch/riscv/lib/csum.c new file +10
-0+ : [offset] "r" (offset) + : + : end); + +return csum; + } +end: +return csum >>16 ; +no_zbb: +#ifndef CONFIG_32BITThese can also be moved to
IS_ENABLED()FYI, since there's no 32-bit stuff here that'd break the build for 64-bit. Ditto elsewhere where you've got similar stuff.
Quoted 2 lines
Cheers,
Conor.
This is an ifndef, so 32-bit compilation would throw a warning about shifting by 32 bits if IS_ENABLED was used instead.
- Charlie
Quoted 13 lines
Quoted 13 lines
arch/riscv/lib/csum.c new file +8
-0+ csum += (csum >>32 ) | (csum <<32 ); + csum >>=32 ; +#endif + csum = (unsigned int )csum + (((unsigned int )csum >>16 ) | ((unsigned int )csum <<16 )); +if (offset &1 ) +return (unsigned short )swab32 (csum); +return csum >>16 ; +}Signature · 2 lines
-- 2.42.0
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 10 lines
Quoted 8 lines
Quoted 1 line
+#ifndef
CONFIG_32BITThese can also be moved to
IS_ENABLED()FYI, since there's no 32-bit stuff here that'd break the build for 64-bit. Ditto elsewhere where you've got similar stuff.Cheers,
Conor.This is an ifndef, so 32-bit compilation would throw a warning about shifting by 32 bits if
IS_ENABLEDwas used instead.
Fair enough. I did accidentally invert things in my mail, I did notice the n, I just thought it did the elimination beforehand those checks, sorry for the noise.
Re: [PATCH v4 2/5] riscv: Add checksum library
Quoted 6 lines · Conor Dooley
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
+#ifndef
CONFIG_32BITThese can also be moved to
IS_ENABLED()FYI, since there's no 32-bit stuff here that'd break the build for 64-bit. Ditto elsewhere where you've got similar stuff.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Cheers,
Conor.This is an ifndef, so 32-bit compilation would throw a warning about shifting by 32 bits if
IS_ENABLEDwas used instead.Fair enough. I did accidentally invert things in my mail, I did notice the n, I just thought it did the elimination beforehand those checks, sorry for the noise.
It appears that LLVM is smart enough to not attempt to compile code that will never execute but GCC is not. Pretty interesting because it allows the ".option arch" code to be encased in IS_ENABLED because it is only not supported on LLVM, but the shifting code needs to be in ifdef because otherwise gcc will complain.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 3/5 riscv: Vector checksum header
In reply to Charlie JenkinsVector code is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code. Vector instrinsic types are still used so the inline assembly can appropriately select vector registers. However, this code cannot be merged yet because it is currently not possible to use vector instrinsics in the kernel because vector support needs to be directly enabled by assembly.
--- arch/riscv/include/asm/checksum.h | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
arch/riscv/include/asm/checksum.h +75 -0
@@ -10,6 +10,10 @@#include <linux/in6.h> #include <linux/uaccess.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> +#endif +#ifdef CONFIG_32BIT typedef unsigned int csum_t ;#else
@@ -42,6 +46,77 @@ static inline __sum16 csum_fold(__wsum sum)*/ static inline __sum16 ip_fast_csum (const void *iph,unsigned int ihl) { +#ifdef CONFIG_RISCV_ISA_V +if (!has_vector ()) + goto no_vector; + +vuint64m1_t prev_buffer; +vuint32m1_t curr_buffer; +unsigned int vl; + +if (IS_ENABLED (CONFIG_32BIT )) { +csum_t high_result, low_result; + +kernel_vector_begin (); + asm(".option push \n\ +.option arch, +v \n\ +vsetivli x0, 1, e64, ta, ma \n\ +vmv.v.i %[prev_buffer], 0 \n\ +1: \n\ +vsetvli %[vl], %[ihl], e32, m1, ta, ma \n\ +vle32.v %[curr_buffer], (%[iph]) \n\ +vwredsumu.vs %[prev_buffer], %[curr_buffer], %[prev_buffer] \n\ +sub %[ihl], %[ihl], %[vl] \n\ +slli %[vl], %[vl], 2 \n\ +add %[iph], %[vl], %[iph] \n\ +# If not all of iph could fit into vector reg, do another sum \n\ +bne %[ihl], zero, 1b \n\ +vsetivli x0, 1, e64, m1, ta, ma \n\ +vmv.x.s %[low_result], %[prev_buffer] \n\ +addi %[vl], x0, 32 \n\ +vsrl.vx %[prev_buffer], %[prev_buffer], %[vl] \n\ +vmv.x.s %[high_result], %[prev_buffer] \n\ +.option pop" + : [vl]"=&r" (vl), [prev_buffer]"=&vd" (prev_buffer), + [curr_buffer]"=&vd" (curr_buffer), + [high_result]"=&r" (high_result), + [low_result]"=&r" (low_result) + : [iph]"r" (iph), [ihl]"r" (ihl)); +kernel_vector_end (); + + high_result += low_result; + high_result += high_result < low_result; + }else { +csum_t result; + +kernel_vector_begin (); + asm(".option push \n\ +.option arch, +v \n\ +vsetivli x0, 1, e64, ta, ma \n\ +vmv.v.i %[prev_buffer], 0 \n\ +1: \n\ +# Setup 32-bit sum of iph \n\ +vsetvli %[vl], %[ihl], e32, m1, ta, ma \n\ +vle32.v %[curr_buffer], (%[iph]) \n\ +# Sum each 32-bit segment of iph that can fit into a vector reg \n\ +vwredsumu.vs %[prev_buffer], %[curr_buffer], %[prev_buffer] \n\ +subw %[ihl], %[ihl], %[vl] \n\ +slli %[vl], %[vl], 2 \n\ +addw %[iph], %[vl], %[iph] \n\ +# If not all of iph could fit into vector reg, do another sum \n\ +bne %[ihl], zero, 1b \n\ +vsetvli x0, x0, e64, m1, ta, ma \n\ +vmv.x.s %[result], %[prev_buffer] \n\ +.option pop" + : [vl]"=&r" (vl), [prev_buffer]"=&vd" (prev_buffer), + [curr_buffer]"=&vd" (curr_buffer), + [result]"=&r" (result) + : [iph]"r" (iph), [ihl]"r" (ihl)); +kernel_vector_end (); + } +no_vector: +#endif // !CONFIG_RISCV_ISA_V +csum_t csum =0 ;int pos =0 ;
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 4/5 riscv: Vector checksum library
In reply to Charlie JenkinsThis patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)
arch/riscv/lib/csum.c +92 -0
@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> +#endif +/* Default version is sufficient for 32 bit */ #ifndef CONFIG_32BIT __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
@@ -115,6 +119,94 @@ unsigned int __no_sanitize_address do_csum(const unsigned char *buff, int len) offset = (csum_t )buff &OFFSET_MASK ;kasan_check_read (buff, len); ptr = (const csum_t *)(buff - offset); +#ifdef CONFIG_RISCV_ISA_V +if (!has_vector ()) + goto no_vector; + + len += offset; + +vuint64m1_t prev_buffer; +vuint32m1_t curr_buffer; +unsigned int shift, cl, tail_seg; +csum_t vl, csum; +const csum_t *ptr; + +#ifdef CONFIG_32BIT +csum_t high_result, low_result; +#else +csum_t result; +#endif + +// Read the tail segment + tail_seg = len %4 ; + csum =0 ; +if (tail_seg) { + shift = (4 - tail_seg) *8 ; + csum = *(unsigned int *)((const unsigned char *)ptr + len - tail_seg); + csum = ((unsigned int )csum << shift) >> shift; + len -= tail_seg; + } + +unsigned int start_mask = (unsigned int )(~(~0U << offset)); + +kernel_vector_begin (); + asm(".option push \n\ +.option arch, +v \n\ +vsetvli %[vl], %[len], e8, m1, ta, ma \n\ +# clear out mask and vector registers since we switch up sizes \n\ +vmclr.m v0 \n\ +vmclr.m %[prev_buffer] \n\ +vmclr.m %[curr_buffer] \n\ +# Mask out the leading bits of a misaligned address \n\ +vsetivli x0, 1, e64, m1, ta, ma \n\ +vmv.s.x %[prev_buffer], %[csum] \n\ +vmv.s.x v0, %[start_mask] \n\ +vsetvli %[vl], %[len], e8, m1, ta, ma \n\ +vmnot.m v0, v0 \n\ +vle8.v %[curr_buffer], (%[buff]), v0.t \n\ +j 2f \n\ +# Iterate through the buff and sum all words \n\ +1: \n\ +vsetvli %[vl], %[len], e8, m1, ta, ma \n\ +vle8.v %[curr_buffer], (%[buff]) \n\ +2: \n\ +vsetvli x0, x0, e32, m1, ta, ma \n\ +vwredsumu.vs %[prev_buffer], %[curr_buffer], %[prev_buffer] \n\t" +#ifdef CONFIG_32BIT +"sub %[len], %[len], %[vl] \n\ +slli %[vl], %[vl], 2 \n\ +add %[buff], %[vl], %[buff] \n\ +bnez %[len], 1b \n\ +vsetvli x0, x0, e64, m1, ta, ma \n\ +vmv.x.s %[result], %[prev_buffer] \n\ +addi %[vl], x0, 32 \n\ +vsrl.vx %[prev_buffer], %[prev_buffer], %[vl] \n\ +vmv.x.s %[high_result], %[prev_buffer] \n\ +.option pop" + : [vl]"=&r" (vl), [prev_buffer]"=&vd" (prev_buffer), + [curr_buffer]"=&vd" (curr_buffer), + [high_result]"=&r" (high_result), [low_result]"=&r" (low_result) + : [buff]"r" (ptr), [len]"r" (len), [start_mask]"r" (start_mask), + [csum]"r" (csum)); + + high_result += low_result; + high_result += high_result < low_result; +#else // !CONFIG_32BIT +"subw %[len], %[len], %[vl] \n\ +slli %[vl], %[vl], 2 \n\ +addw %[buff], %[vl], %[buff] \n\ +bnez %[len], 1b \n\ +vsetvli x0, x0, e64, m1, ta, ma \n\ +vmv.x.s %[result], %[prev_buffer] \n\ +.option pop" + : [vl] "=&r"(vl), [prev_buffer] "=&vd"(prev_buffer), + [curr_buffer] "=&vd"(curr_buffer), [result] "=&r"(result) + : [buff] "r"(ptr), [len] "r"(len), [start_mask] "r"(start_mask), + [csum] "r"(csum)); +#endif // !CONFIG_32BIT +kernel_vector_end (); +no_vector: +#endif // CONFIG_RISCV_ISA_V len = len + offset -sizeof (csum_t);/*
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0
arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h>
What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.
Thanks,
Conor.
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 28 lines · Conor Dooley
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.
Quoted 2 lines
Thanks,
Conor.
It is defined here:
https://github.com/riscv-non-isa/rvv-intrinsic-doc/blob/main/doc/rvv-intrinsic-spec.adoc.
The header is for the vector intrinsics that are supported by llvm and gcc.
- Charlie
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 29 lines · Charlie Jenkins
Quoted 28 lines · Conor Dooley
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.
Quoted 7 lines
Quoted 2 lines
Thanks,
Conor.It is defined here:
https://github.com/riscv-non-isa/rvv-intrinsic-doc/blob/main/doc/rvv-intrinsic-spec.adoc.
The header is for the vector intrinsics that are supported by llvm and gcc.
Well, whatever you're doing with it does not work, producing 3600 or so
fatal errors during compilation, all saying:../arch/riscv/include/asm/checksum.h:14:10: fatal error: riscv_vector.h: No such file or directory
Do you have some makefile hack somewhere that's not part of this patchset? Also, I'm dumb, but can you show me where are the actual intrinsics are being used in this patch anyway? I just seem some types & asm.
Thanks,
Conor.
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 42 lines · Conor Dooley
Quoted 29 lines · Charlie Jenkins
Quoted 28 lines · Conor Dooley
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.Quoted 7 lines
Quoted 2 lines
Thanks,
Conor.It is defined here:
https://github.com/riscv-non-isa/rvv-intrinsic-doc/blob/main/doc/rvv-intrinsic-spec.adoc.
The header is for the vector intrinsics that are supported by llvm and gcc.Well, whatever you're doing with it does not work, producing 3600 or so
fatal errors during compilation, all saying:../arch/riscv/include/asm/checksum.h:14:10: fatal error:riscv_vector.h: No such file or directory
Quoted 8 lines
Do you have some makefile hack somewhere that's not part of this patchset? Also, I'm dumb, but can you show me where are the actual intrinsics are being used in this patch anyway? I just seem some types & asm.
Thanks,
Conor.
Intrinsics are needed for the vector types. Vector types are needed to get the inline asm to select vector registers at compile time. I could manually select vector registers to use but that is not ideal. In order to get this to work, vector has to be enabled in the compiler. This patch will not compile right now, but since people are working on vector I was hoping that it would be possible in the future. Palmer recommended that I just put up this patch for now since I had the code, but only the non-vector versions should be candidates for release for now.
- Charlie
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 52 lines · Charlie Jenkins
Quoted 42 lines · Conor Dooley
Quoted 29 lines · Charlie Jenkins
Quoted 28 lines · Conor Dooley
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.Quoted 7 lines
Quoted 2 lines
Thanks,
Conor.It is defined here:
https://github.com/riscv-non-isa/rvv-intrinsic-doc/blob/main/doc/rvv-intrinsic-spec.adoc.
The header is for the vector intrinsics that are supported by llvm and gcc.Well, whatever you're doing with it does not work, producing 3600 or so
fatal errors during compilation, all saying:../arch/riscv/include/asm/checksum.h:14:10: fatal error:riscv_vector.h: No such file or directoryQuoted 8 lines
Do you have some makefile hack somewhere that's not part of this patchset? Also, I'm dumb, but can you show me where are the actual intrinsics are being used in this patch anyway? I just seem some types & asm.
Thanks,
Conor.
Quoted 8 lines
Intrinsics are needed for the vector types. Vector types are needed to get the inline asm to select vector registers at compile time. I could manually select vector registers to use but that is not ideal. In order to get this to work, vector has to be enabled in the compiler. This patch will not compile right now, but since people are working on vector I was hoping that it would be possible in the future. Palmer recommended that I just put up this patch for now since I had the code, but only the non-vector versions should be candidates for release for now.
I see. I was pretty unclear to me anyway what the craic was, you should probably note that the build failures from here onwards are known-broken. If you want that header, I guess you probably need to have v set in -march?
If so, the in-kernel vector patches that have been posted do not do that. Oh-so-far from an expert on what is a safe way to do these kinda things though, sadly.
Re: [PATCH v4 4/5] riscv: Vector checksum library
Quoted 62 lines · Conor Dooley
Quoted 52 lines · Charlie Jenkins
Quoted 42 lines · Conor Dooley
Quoted 29 lines · Charlie Jenkins
Quoted 28 lines · Conor Dooley
Quoted 17 lines · Charlie Jenkins
This patch is not ready for merge as vector support in the kernel is limited. However, the code has been tested in QEMU so the algorithms do work. This code requires the kernel to be compiled with C vector support, but that is not yet possible. It is written in assembly rather than using the GCC vector instrinsics because they did not provide optimal code.
Signed-off-by Charlie Jenkins <[email protected]>--- arch/riscv/lib/csum.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)arch/riscv/lib/csum.c +0
-0arch/riscv/lib/csum.c line 16
Quoted 5 lines
arch/riscv/lib/csum.c +2
-0@@ -12,6 +12,10 @@#include <net/checksum.h> +#ifdef CONFIG_RISCV_ISA_V +#include <riscv_vector.h> What actually includes this header, I don't see it in either Andy's in-kernel vector series or Bjorn's blake2 one.
Can you link to the pre-requisites in your cover letter please.Quoted 7 lines
Quoted 2 lines
Thanks,
Conor.It is defined here:
https://github.com/riscv-non-isa/rvv-intrinsic-doc/blob/main/doc/rvv-intrinsic-spec.adoc.
The header is for the vector intrinsics that are supported by llvm and gcc.Well, whatever you're doing with it does not work, producing 3600 or so
fatal errors during compilation, all saying:../arch/riscv/include/asm/checksum.h:14:10: fatal error:riscv_vector.h: No such file or directoryQuoted 8 lines
Do you have some makefile hack somewhere that's not part of this patchset? Also, I'm dumb, but can you show me where are the actual intrinsics are being used in this patch anyway? I just seem some types & asm.
Thanks,
Conor.Quoted 8 lines
Intrinsics are needed for the vector types. Vector types are needed to get the inline asm to select vector registers at compile time. I could manually select vector registers to use but that is not ideal. In order to get this to work, vector has to be enabled in the compiler. This patch will not compile right now, but since people are working on vector I was hoping that it would be possible in the future. Palmer recommended that I just put up this patch for now since I had the code, but only the non-vector versions should be candidates for release for now.
Quoted 7 lines
I see. I was pretty unclear to me anyway what the craic was, you should probably note that the build failures from here onwards are known-broken. If you want that header, I guess you probably need to have v set in -march?
If so, the in-kernel vector patches that have been posted do not do that. Oh-so-far from an expert on what is a safe way to do these kinda things though, sadly.
It seems like more than just enabling v in the march will need to be done. Because linux uses -nostdinc the header file won't be included. After doing some research it also seems like llvm and gcc do not share inline asm constraints. llvm is missing "vd" to specify a register that is not a mask register. I think I will drop these vector patches for now since there seems to be more work than I expected to get this functional.
- Charlie
PATCH v4 5/5 riscv: Test checksum functions
In reply to Charlie JenkinsAdd Kconfig support for riscv specific testing modules. This was created to supplement lib/checksum_kunit.c, and add tests for ip_fast_csum and csum_ipv6_magic.
--- arch/riscv/Kconfig.debug | 1 + arch/riscv/lib/Kconfig.debug | 31 ++++ arch/riscv/lib/Makefile | 2 + arch/riscv/lib/riscv_checksum_kunit.c | 330 ++++++++++++++++++++++++++++++++++ 4 files changed, 364 insertions(+)
arch/riscv/Kconfig.debug +1 -0
@@ -0,0 +1 @@ +source "arch/riscv/lib/Kconfig.debug"
arch/riscv/lib/Kconfig.debug new file +31 -0
@@ -0,0 +1,31 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "riscv Testing and Coverage" + +menuconfig RUNTIME_TESTING_MENU + bool "Runtime Testing" + def_bool y + help + Enable riscv runtime testing. + +if RUNTIME_TESTING_MENU + +config RISCV_CHECKSUM_KUNIT + tristate "KUnit test riscv checksum functions at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable this option to test the checksum functions at boot. + + KUnit tests run during boot and output the results to the debug log + in TAP format (http://testanything.org/). Only useful for kernel devs + running the KUnit test harness, and not intended for inclusion into a + production build. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + +endif # RUNTIME_TESTING_MENU + +endmenu # "riscv Testing and Coverage"
arch/riscv/lib/Makefile +2 -0
@@ -12,3 +12,5 @@ lib-$(CONFIG_64BIT) += tishift.o lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o + +obj-$(CONFIG_RISCV_CHECKSUM_KUNIT) += riscv_checksum_kunit.o
arch/riscv/lib/riscv_checksum_kunit.c new file +330 -0
330 lines not on this page · raw message
Signature · 7 lines
2.42.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv