Skip to contentSkip to conversation

net: Implement fast csum_partial for x86_64

PATCH Superseded Tom Herbert 6 participants 14 messages Jan 5, 2016 – Jan 7, 2016

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: Implement fast csum_partial for x86_64

Implement assembly routine for csum_partial for 64 bit x86. This primarily speeds up checksum calculation for smaller lengths such as those that are present when doing skb_postpull_rcsum when getting CHECKSUM_COMPLETE from device or after CHECKSUM_UNNECESSARY conversion.

This implementation is similar to csum_partial implemented in checksum_32.S, however since we are dealing with 8 bytes at a time there are more cases for small lengths-- for that we employ a jump table. Also, we don't do anything special for alignment, unaligned accesses on x86 do not appear to be a performance issue.

Testing:

Verified correctness by testing arbitrary length buffer filled with random data. For each buffer I compared the computed checksum using the original algorithm for each possible alignment (0-7 bytes).

Checksum performance:

Isolating old and new implementation for some common cases:

Old New
Case nsecs nsecs Improvement
---------------------+--------+--------+-----------------------------

1400 bytes (0 align)    194.5    174.3     10%    (Big packet)
40 bytes (0 align)      13.8     5.8       57%    (Ipv6 hdr common case)
8 bytes (4 align)       8.4      2.9       65%    (UDP, VXLAN in IPv4)
14 bytes (0 align)      10.6     5.8       45%    (Eth hdr)
14 bytes (4 align)      10.8     5.8       46%    (Eth hdr in IPv4)
---
 arch/x86/include/asm/checksum_64.h |   5 ++
 arch/x86/lib/csum-partial_64.S     | 147 ++++++++++++++++++++++++++++++++++++
 arch/x86/lib/csum-partial_64.c     | 148 -------------------------------------
 3 files changed, 152 insertions(+), 148 deletions(-)
 create mode 100644 arch/x86/lib/csum-partial_64.S
 delete mode 100644 arch/x86/lib/csum-partial_64.c
arch/x86/include/asm/checksum_64.h +5 -0
index cd00e17..a888f65 100644
@@ -128,6 +128,11 @@ static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  */
 extern __wsum csum_partial(const void *buff, int len, __wsum sum);
 
+static inline __sum16 ip_compute_csum(const void *buff, int len)
+{
+	return csum_fold(csum_partial(buff, len, 0));
+}
+
 #define  _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1
 #define HAVE_CSUM_COPY_USER 1
 
arch/x86/lib/csum-partial_64.S new file +147 -0

147 lines not on this page · raw message

arch/x86/lib/csum-partial_64.c deleted +0 -148

148 lines not on this page · raw message

Signature · 1 line
2.4.6
Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Tom Herbert

Implement assembly routine for csum_partial for 64 bit x86. This primarily speeds up checksum calculation for smaller lengths such as those that are present when doing skb_postpull_rcsum when getting CHECKSUM_COMPLETE from device or after CHECKSUM_UNNECESSARY conversion.

Very nice !

You might add a comment telling the '4' comes from length of 'adcq 6*8(%rdi),%rax' instruction, and that the 'nop' is to compensate that
'adcq 0*8(%rdi),%rax' is using 3 bytes instead.

We also could use .byte 0x48, 0x13, 0x47, 0x00 to force a 4 bytes instruction and remove the nop.

+       lea     20f(, %rcx, 4), %r11
+       clc
+       jmp     *%r11
+
+.align 8
+       adcq    6*8(%rdi),%rax
+       adcq    5*8(%rdi),%rax
+       adcq    4*8(%rdi),%rax
+       adcq    3*8(%rdi),%rax
+       adcq    2*8(%rdi),%rax
+       adcq    1*8(%rdi),%rax
+       adcq    0*8(%rdi),%rax // could force a 4 byte instruction (.byte 0x48, 0x13, 0x47, 0x00)
+       nop
+20:    /* #quads % 8 jump table base */
Acked-by on v2 1/1
#8 lore raw
In reply to Tom Herbert

Hi,

Quoted 25 lines · Tom Herbert

Implement assembly routine for csum_partial for 64 bit x86. This primarily speeds up checksum calculation for smaller lengths such as those that are present when doing skb_postpull_rcsum when getting CHECKSUM_COMPLETE from device or after CHECKSUM_UNNECESSARY conversion.

This implementation is similar to csum_partial implemented in checksum_32.S, however since we are dealing with 8 bytes at a time there are more cases for small lengths-- for that we employ a jump table. Also, we don't do anything special for alignment, unaligned accesses on x86 do not appear to be a performance issue.

Testing:

Verified correctness by testing arbitrary length buffer filled with random data. For each buffer I compared the computed checksum using the original algorithm for each possible alignment (0-7 bytes).

Checksum performance:

Isolating old and new implementation for some common cases:

Old New
Case nsecs nsecs Improvement
---------------------+--------+--------+-----------------------------

Quoted 7 lines
1400 bytes (0 align)    194.5    174.3     10%    (Big packet)
40 bytes (0 align)      13.8     5.8       57%    (Ipv6 hdr common case)
8 bytes (4 align)       8.4      2.9       65%    (UDP, VXLAN in IPv4)
14 bytes (0 align)      10.6     5.8       45%    (Eth hdr)
14 bytes (4 align)      10.8     5.8       46%    (Eth hdr in IPv4)

Also,

Tested with the same test cases as the old patch and showed no problems and same improvements.

Tom, did you have a look if it makes sense to add a second carry addition train with the adcx instruction, which does not signal carry via the carry flag but with the overflow flag? This instruction should not have any dependencies with the adc instructions and could help the CPU to parallelize the code even more (increased instructions per cycle).

Thanks,
Hannes

#2 lore raw
In reply to Eric Dumazet
Quoted 10 lines · Eric Dumazet
Quoted 5 lines · Tom Herbert

Implement assembly routine for csum_partial for 64 bit x86. This primarily speeds up checksum calculation for smaller lengths such as those that are present when doing skb_postpull_rcsum when getting CHECKSUM_COMPLETE from device or after CHECKSUM_UNNECESSARY conversion.

Very nice !

Quoted 7 lines

You might add a comment telling the '4' comes from length of 'adcq 6*8(%rdi),%rax' instruction, and that the 'nop' is to compensate that
'adcq 0*8(%rdi),%rax' is using 3 bytes instead.

We also could use .byte 0x48, 0x13, 0x47, 0x00 to force a 4 bytes instruction and remove the nop.

Apparently "adcq.d8" will do The Right Thing for this.

-hpa

#3 lore raw
Quoted 1 line · H. Peter Anvin

Apparently "adcq.d8" will do The Right Thing for this.

Nice trick ;)

#9 lore raw
In reply to Hannes Frederic Sowa
Quoted 6 lines · Hannes Frederic Sowa

Tom, did you have a look if it makes sense to add a second carry addition train with the adcx instruction, which does not signal carry via the carry flag but with the overflow flag? This instruction should not have any dependencies with the adc instructions and could help the CPU to parallelize the code even more (increased instructions per cycle).

I guess adcx would (possibly) bring improvements for large areas, but for this case the bottleneck is to bring data from memory.

Note this topic was discussed 2 years ago and no conclusive action was taken.

https://www.mail-archive.com/[email protected]/msg529610.html

#4 lore raw
In reply to Eric Dumazet

From: Eric Dumazet

Quoted 14 lines

Sent: 05 January 2016 22:19
To: Tom Herbert
You might add a comment telling the '4' comes from length of 'adcq 6*8(%rdi),%rax' instruction, and that the 'nop' is to compensate that
'adcq 0*8(%rdi),%rax' is using 3 bytes instead.

We also could use .byte 0x48, 0x13, 0x47, 0x00 to force a 4 bytes instruction and remove the nop.

arch/x86/lib/csum-partial_64.S new file +4 -0
+       lea     20f(, %rcx, 4), %r11
+       clc
+       jmp     *%r11
+

arch/x86/lib/csum-partial_64.S line 74

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+.align 8
+       adcq    6*8(%rdi),%rax
+       adcq    5*8(%rdi),%rax
+       adcq    4*8(%rdi),%rax
+       adcq    3*8(%rdi),%rax
+       adcq    2*8(%rdi),%rax
+       adcq    1*8(%rdi),%rax
+       adcq    0*8(%rdi),%rax // could force a 4 byte instruction (.byte 0x48, 0x13, 0x47, 0x00)
+       nop
+20:    /* #quads % 8 jump table base */

Or move label at the top (after the .align) and adjust the maths. You could add a second label after the first adcq and use the difference between them for the '4'.

David

#5 lore raw
Quoted 26 lines · David Laight

From: Eric Dumazet

Quoted 14 lines

Sent: 05 January 2016 22:19
To: Tom Herbert
You might add a comment telling the '4' comes from length of 'adcq 6*8(%rdi),%rax' instruction, and that the 'nop' is to compensate that
'adcq 0*8(%rdi),%rax' is using 3 bytes instead.

We also could use .byte 0x48, 0x13, 0x47, 0x00 to force a 4 bytes instruction and remove the nop.

arch/x86/lib/csum-partial_64.S new file +4 -0
+       lea     20f(, %rcx, 4), %r11
+       clc
+       jmp     *%r11
+

arch/x86/lib/csum-partial_64.S line 74

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+.align 8
+       adcq    6*8(%rdi),%rax
+       adcq    5*8(%rdi),%rax
+       adcq    4*8(%rdi),%rax
+       adcq    3*8(%rdi),%rax
+       adcq    2*8(%rdi),%rax
+       adcq    1*8(%rdi),%rax
+       adcq    0*8(%rdi),%rax // could force a 4 byte instruction (.byte 0x48, 0x13, 0x47, 0x00)
+       nop
+20:    /* #quads % 8 jump table base */
Quoted 3 lines

Or move label at the top (after the .align) and adjust the maths. You could add a second label after the first adcq and use the difference between them for the '4'.

Not really.

We could not use the trick it the length was 5.

Only 1, 2, 4 and 8 are supported.

#6 lore raw

From: Eric Dumazet

Quoted 32 lines

Sent: 06 January 2016 14:25

Quoted 26 lines · David Laight

From: Eric Dumazet

Quoted 14 lines

Sent: 05 January 2016 22:19
To: Tom Herbert
You might add a comment telling the '4' comes from length of 'adcq 6*8(%rdi),%rax' instruction, and that the 'nop' is to compensate that
'adcq 0*8(%rdi),%rax' is using 3 bytes instead.

We also could use .byte 0x48, 0x13, 0x47, 0x00 to force a 4 bytes instruction and remove the nop.

arch/x86/lib/csum-partial_64.S new file +4 -0
+       lea     20f(, %rcx, 4), %r11
+       clc
+       jmp     *%r11
+

arch/x86/lib/csum-partial_64.S line 74

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+.align 8
+       adcq    6*8(%rdi),%rax
+       adcq    5*8(%rdi),%rax
+       adcq    4*8(%rdi),%rax
+       adcq    3*8(%rdi),%rax
+       adcq    2*8(%rdi),%rax
+       adcq    1*8(%rdi),%rax
+       adcq    0*8(%rdi),%rax // could force a 4 byte instruction (.byte 0x48, 0x13, 0x47, 0x00)
+       nop
+20:    /* #quads % 8 jump table base */
Quoted 3 lines

Or move label at the top (after the .align) and adjust the maths. You could add a second label after the first adcq and use the difference between them for the '4'.

Quoted 5 lines

Not really.

We could not use the trick it the length was 5.

Only 1, 2, 4 and 8 are supported.

Indeed, and 'lea 20f(, %rcx, 5), %r11' will generate an error from the assembler.
Seems appropriate to get the assembler to verify this for you.

Assuming this code block is completely skipped for aligned lengths the nop isn't needed provided the '20:' label is at the right place.

Someone also pointed out that the code is memory limited (dual add chains making no difference), so why is it unrolled at all?

OTOH I'm sure I remember something about false dependencies on the x86 flags register because of instructions only changing some bits. So it might be that you can't (or couldn't) get concurrency between instructions that update different parts of the flags register.

David

#7 lore raw
Quoted 2 lines · David Laight

Someone also pointed out that the code is memory limited (dual add chains making no difference), so why is it unrolled at all?

Because it matters if the data is already present in CPU caches.

So why not unrolling if it helps in some situations ?

#10 lore raw
In reply to Tom Herbert
Quoted 2 lines · Tom Herbert

Also, we don't do anything special for alignment, unaligned accesses on x86 do not appear to be a performance issue.

This is not true on Atom CPUs.

Also on most CPUs there is still a larger penalty when crossing cache lines.

Quoted 7 lines

Verified correctness by testing arbitrary length buffer filled with random data. For each buffer I compared the computed checksum using the original algorithm for each possible alignment (0-7 bytes).

Checksum performance:

Isolating old and new implementation for some common cases:

You forgot to state the CPU. The results likely depend heavily on the micro architecture.

The original C code was optimized for K8 FWIW.

Overall your assembler looks similar to the C code, except for the jump table. Jump table has the disadvantage that it is much harder to branch predict, with a large penalty if it's mispredicted.

I would expect it to be slower for cases where the length changes frequently. Did you benchmark that case?

-Andi

#11 lore raw
In reply to Tom Herbert

Hi Tom,

Quoted 9 lines · Tom Herbert
arch/x86/lib/csum-partial_64.S new file +6 -0
@@ -0,0 +1,147 @@
+/* Copyright 2016 Tom Herbert <[email protected]>
+ *
+ * Checksum partial calculation
+ *
+ * __wsum csum_partial(const void *buff, int len, __wsum sum)
+ *

arch/x86/lib/csum-partial_64.S line 16

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+ * Computes the checksum of a memory block at buff, length len,
+ * and adds in "sum" (32-bit)
+ *
+ * Returns a 32-bit number suitable for feeding into itself
+ * or csum_tcpudp_magic
+ *
+ * Register usage:
+ *   %rdi: argument 1, buff
+ *   %rsi: argument 2, length
+ *   %rdx: argument 3, add in value

I think you forgot to carry-add-in the %rdx register.

The assembly code replaces do_csum but not csum_partial.

Bye,
Hannes

#12 lore raw
Quoted 27 lines · Hannes Frederic Sowa

Hi Tom,

Quoted 10 lines · Tom Herbert
arch/x86/lib/csum-partial_64.S new file +6 -0
@@ -0,0 +1,147 @@
+/* Copyright 2016 Tom Herbert <[email protected]>
+ *
+ * Checksum partial calculation
+ *
+ * __wsum csum_partial(const void *buff, int len, __wsum sum)
+ *

arch/x86/lib/csum-partial_64.S line 16

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+ * Computes the checksum of a memory block at buff, length len,
+ * and adds in "sum" (32-bit)
+ *
+ * Returns a 32-bit number suitable for feeding into itself
+ * or csum_tcpudp_magic
+ *
+ * Register usage:
+ *   %rdi: argument 1, buff
+ *   %rsi: argument 2, length
+ *   %rdx: argument 3, add in value

I think you forgot to carry-add-in the %rdx register.

Quoted 2 lines

The assembly code replaces do_csum but not csum_partial.

First instruction is: movl %edx, %eax /* Initialize with
initial sum argument */

Tom

Quoted 3 lines

Bye,
Hannes

#13 lore raw
Quoted 29 lines · Tom Herbert
Quoted 27 lines · Hannes Frederic Sowa

Hi Tom,

Quoted 10 lines · Tom Herbert
arch/x86/lib/csum-partial_64.S new file +6 -0
@@ -0,0 +1,147 @@
+/* Copyright 2016 Tom Herbert <[email protected]>
+ *
+ * Checksum partial calculation
+ *
+ * __wsum csum_partial(const void *buff, int len, __wsum sum)
+ *

arch/x86/lib/csum-partial_64.S line 16

Quoted 10 lines
arch/x86/lib/csum-partial_64.S new file +10 -0
+ * Computes the checksum of a memory block at buff, length len,
+ * and adds in "sum" (32-bit)
+ *
+ * Returns a 32-bit number suitable for feeding into itself
+ * or csum_tcpudp_magic
+ *
+ * Register usage:
+ *   %rdi: argument 1, buff
+ *   %rsi: argument 2, length
+ *   %rdx: argument 3, add in value

I think you forgot to carry-add-in the %rdx register.

Quoted 4 lines
Quoted 2 lines

The assembly code replaces do_csum but not csum_partial.

First instruction is: movl %edx, %eax /* Initialize with
initial sum argument */

Ups, sorry. I only grepped through the source. Meanwhile my test also uses non-zero sums and shows that it is fine.

Bye,
Hannes