Skip to contentSkip to conversation

powerpc/64: Fix checksum folding in csum_tcpudp_nofold and ip_fast_csum_nofold

PATCH Applied Paul Mackerras 3 participants 7 messages Nov 3, 2016 – Jan 27, 2017 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 powerpc/64: Fix checksum folding in csum_tcpudp_nofold and ip_fast_csum_nofold

These functions compute an IP checksum by computing a 64-bit sum and folding it to 32 bits (the "nofold" in their names refers to folding down to 16 bits). However, doing (u32) (s + (s >> 32)) is not sufficient to fold a 64-bit sum to 32 bits correctly. The addition can produce a carry out from bit 31, which needs to be added in to the sum to produce the correct result.

To fix this, we copy the from64to32() function from lib/checksum.c and use that.

---
 arch/powerpc/include/asm/checksum.h | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
arch/powerpc/include/asm/checksum.h +12 -5
index ee655ed..c16c6f8 100644
@@ -53,19 +53,27 @@ static inline __sum16 csum_fold(__wsum sum)
 	return (__force __sum16)(~((__force u32)sum + tmp) >> 16);
 }
 
+static inline u32 from64to32(u64 x)
+{
+	/* add up 32-bit and 32-bit for 32+c bit */
+	x = (x & 0xffffffff) + (x >> 32);
+	/* add up carry.. */
+	x = (x & 0xffffffff) + (x >> 32);
+	return (u32)x;
+}
+
 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
                                      unsigned short len,
                                      unsigned short proto,
                                      __wsum sum)
 {
 #ifdef __powerpc64__
-	unsigned long s = (__force u32)sum;
+	u64 s = (__force u32)sum;
 
 	s += (__force u32)saddr;
 	s += (__force u32)daddr;
 	s += proto + len;
-	s += (s >> 32);
-	return (__force __wsum) s;
+	return (__force __wsum) from64to32(s);
 #else
     __asm__("\n\
 	addc %0,%0,%1 \n\
@@ -127,8 +135,7 @@ static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
 
 	for (i = 0; i < ihl - 1; i++, ptr++)
 		s += *ptr;
-	s += (s >> 32);
-	return (__force __wsum)s;
+	return (__force __wsum)from64to32(s);
 #else
 	__wsum sum, tmp;
 
Signature · 1 line
2.10.1
#1 lore raw

PATCH 2/2 powerpc/64: Use optimized checksum routines on little-endian

Currently we have optimized hand-coded assembly checksum routines for big-endian 64-bit systems, but for little-endian we use the generic C routines. This modifies the optimized routines to work for little-endian. With this, we no longer need to enable CONFIG_GENERIC_CSUM. This also fixes a couple of comments in checksum_64.S so they accurately reflect what the associated instruction does.

---
 arch/powerpc/Kconfig                |  2 +-
 arch/powerpc/include/asm/checksum.h |  4 ++++
 arch/powerpc/lib/Makefile           |  2 --
 arch/powerpc/lib/checksum_64.S      | 12 ++++++++++--
 4 files changed, 15 insertions(+), 5 deletions(-)
arch/powerpc/Kconfig +1 -1
index 65fba4c..514e6dd 100644
@@ -164,7 +164,7 @@ config PPC
 	select HAVE_KERNEL_GZIP
 
 config GENERIC_CSUM
-	def_bool CPU_LITTLE_ENDIAN
+	def_bool n
 
 config EARLY_PRINTK
 	bool
arch/powerpc/include/asm/checksum.h +4 -0
index c16c6f8..5f8297b 100644
@@ -72,7 +72,11 @@ static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
 
 	s += (__force u32)saddr;
 	s += (__force u32)daddr;
+#ifdef __BIG_ENDIAN
 	s += proto + len;
+#else
+	s += (proto + len) << 8;
+#endif
 	return (__force __wsum) from64to32(s);
 #else
     __asm__("\n\
arch/powerpc/lib/Makefile +0 -2
index 309361e8..0e649d7 100644
@@ -21,9 +21,7 @@ obj64-y	+= copypage_64.o copyuser_64.o usercopy_64.o mem_64.o hweight_64.o \
 obj64-$(CONFIG_SMP)	+= locks.o
 obj64-$(CONFIG_ALTIVEC)	+= vmx-helper.o
 
-ifeq ($(CONFIG_GENERIC_CSUM),)
 obj-y			+= checksum_$(BITS).o checksum_wrappers.o
-endif
 
 obj-$(CONFIG_PPC_EMULATE_SSTEP)	+= sstep.o ldstfp.o
 
arch/powerpc/lib/checksum_64.S +10 -2
index fd91766..4dd2761 100644
@@ -36,7 +36,7 @@ _GLOBAL(__csum_partial)
 	 * work to calculate the correct checksum, we ignore that case
 	 * and take the potential slowdown of unaligned loads.
 	 */
-	rldicl. r6,r3,64-1,64-2		/* r6 = (r3 & 0x3) >> 1 */
+	rldicl. r6,r3,64-1,64-2		/* r6 = (r3 >> 1) & 0x3 */
 	beq	.Lcsum_aligned
 
 	li	r7,4
@@ -168,8 +168,12 @@ _GLOBAL(__csum_partial)
 	beq	.Lcsum_finish
 
 	lbz	r6,0(r3)
+#ifdef __BIG_ENDIAN
 	sldi	r9,r6,8			/* Pad the byte out to 16 bits */
 	adde	r0,r0,r9
+#else
+	adde	r0,r0,r6
+#endif
 
 .Lcsum_finish:
 	addze	r0,r0			/* add in final carry */
@@ -236,7 +240,7 @@ _GLOBAL(csum_partial_copy_generic)
 	 * If the source and destination are relatively unaligned we only
 	 * align the source. This keeps things simple.
 	 */
-	rldicl. r6,r3,64-1,64-2		/* r6 = (r3 & 0x3) >> 1 */
+	rldicl. r6,r3,64-1,64-2		/* r6 = (r3 >> 1) & 0x3 */
 	beq	.Lcopy_aligned
 
 	li	r9,4
@@ -398,8 +402,12 @@ dstnr;	sth	r6,0(r4)
 	beq	.Lcopy_finish
 
 srcnr;	lbz	r6,0(r3)
+#ifdef __BIG_ENDIAN
 	sldi	r9,r6,8			/* Pad the byte out to 16 bits */
 	adde	r0,r0,r9
+#else
+	adde	r0,r0,r6
+#endif
 dstnr;	stb	r6,0(r4)
 
 .Lcopy_finish:
Signature · 1 line
2.10.1
#2 lore raw

Re: [2/2] powerpc/64: Use optimized checksum routines on little-endian

Quoted 9 lines · Paul Mackerras

Currently we have optimized hand-coded assembly checksum routines for big-endian 64-bit systems, but for little-endian we use the generic C routines. This modifies the optimized routines to work for little-endian. With this, we no longer need to enable CONFIG_GENERIC_CSUM. This also fixes a couple of comments in checksum_64.S so they accurately reflect what the associated instruction does.

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/d4fde568a34a93897dfb9ae64cfe9d

cheers

#3 lore raw
In reply to Paul Mackerras
Quoted 9 lines · Paul Mackerras

These functions compute an IP checksum by computing a 64-bit sum and folding it to 32 bits (the "nofold" in their names refers to folding down to 16 bits). However, doing (u32) (s + (s >> 32)) is not sufficient to fold a 64-bit sum to 32 bits correctly. The addition can produce a carry out from bit 31, which needs to be added in to the sum to produce the correct result.

To fix this, we copy the from64to32() function from lib/checksum.c and use that.

This seems to have been broken since ~forever. Do we just not hit that case very often, or do we just incorrectly report checksum failures?

Should it go to stable?

cheers

#4 lore raw
Quoted 9 lines · Michael Ellerman
Quoted 7 lines · Paul Mackerras

These functions compute an IP checksum by computing a 64-bit sum and folding it to 32 bits (the "nofold" in their names refers to folding down to 16 bits). However, doing (u32) (s + (s >> 32)) is not sufficient to fold a 64-bit sum to 32 bits correctly. The addition can produce a carry out from bit 31, which needs to be added in to the sum to produce the correct result.

Quoted 5 lines
Quoted 2 lines

To fix this, we copy the from64to32() function from lib/checksum.c and use that.

This seems to have been broken since ~forever. Do we just not hit that case very often, or do we just incorrectly report checksum failures?

I think there would be about a 1 in a billion chance of hitting it by chance, though you could probably construct a test case that would hit it every time. If you did hit it in real life it would result in a packet being dropped and presumably retransmitted, and I expect that the IP header of the retransmitted packet would be sufficiently different (i.e. different id field or something) that it wouldn't hit the bug a second time.

Quoted 1 line

Should it go to stable?

Probably... though nobody has actually noticed a problem in real life and pinned it down to this.

Paul.

#5 lore raw
In reply to Paul Mackerras
Quoted 9 lines · Paul Mackerras

These functions compute an IP checksum by computing a 64-bit sum and folding it to 32 bits (the "nofold" in their names refers to folding down to 16 bits). However, doing (u32) (s + (s >> 32)) is not sufficient to fold a 64-bit sum to 32 bits correctly. The addition can produce a carry out from bit 31, which needs to be added in to the sum to produce the correct result.

To fix this, we copy the from64to32() function from lib/checksum.c and use that.

This collided with:

f9d4286b9516 ("arch/powerpc: Update parameters for csum_tcpudp_magic & csum_tcpudp_nofold")

Mind rebasing?

cheers

#6 lore raw
In reply to Paul Mackerras
Quoted 11 lines · Paul Mackerras

These functions compute an IP checksum by computing a 64-bit sum and folding it to 32 bits (the "nofold" in their names refers to folding down to 16 bits). However, doing (u32) (s + (s >> 32)) is not sufficient to fold a 64-bit sum to 32 bits correctly. The addition can produce a carry out from bit 31, which needs to be added in to the sum to produce the correct result.

To fix this, we copy the from64to32() function from lib/checksum.c and use that.

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/b492f7e4e07a28e706db26cf4943bb

cheers