Skip to contentSkip to conversation

arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

RFC PATCH New Ivan Djelic 7 participants 19 messages Feb 2, 2013 – Mar 6, 2013 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0 <debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0 <memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

This patch fixes the return value of the assembly version of memset. Could you please review, or suggest better alternatives ?

Thanks,

--
Ivan

(this is a shorter and (hopefully) clearer repost of
http://lists.infradead.org/pipermail/linux-arm-kernel/2013-January/144916.html)

The patch adds a 'mov' instruction and merges an additional load+store into existing load/store instructions.
For ease of review, here is a breakdown of the patch into 4 simple steps:

Step 1
======
Perform the following substitutions:
ip -> r8, then
r0 -> ip,
and insert 'mov ip, r0' as the first statement of the function. At this point, we have a memset() implementation returning the proper result, but corrupting r8 on some paths (the ones that were using ip).

Step 2
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 1:

save r8:

-       str     lr, [sp, #-4]!
+       stmfd   sp!, {r8, lr}

and restore r8 on both exit paths:

-       ldmeqfd sp!, {pc}               @ Now <64 bytes to go.
+       ldmeqfd sp!, {r8, pc}           @ Now <64 bytes to go.
(...)
        tst     r2, #16
        stmneia ip!, {r1, r3, r8, lr}
-       ldr     lr, [sp], #4
+       ldmfd   sp!, {r8, lr}

Step 3
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 0:

save r8:

-       stmfd   sp!, {r4-r7, lr}
+       stmfd   sp!, {r4-r8, lr}

and restore r8 on both exit paths:
bgt 3b
- ldmeqfd sp!, {r4-r7, pc}
+ ldmeqfd sp!, {r4-r8, pc}
(...)

        tst     r2, #16
        stmneia ip!, {r4-r7}
-       ldmfd   sp!, {r4-r7, lr}
+       ldmfd   sp!, {r4-r8, lr}

Step 4
======
Rewrite register list "r4-r7, r8" as "r4-r8".

---
 arch/arm/lib/memset.S |   85 +++++++++++++++++++++++++------------------------
 1 file changed, 44 insertions(+), 41 deletions(-)
arch/arm/lib/memset.S +44 -41
index 650d592..eafd5ee 100644
@@ -19,9 +19,9 @@
 1:	subs	r2, r2, #4		@ 1 do we have enough
 	blt	5f			@ 1 bytes to align with?
 	cmp	r3, #2			@ 1
-	strltb	r1, [r0], #1		@ 1
-	strleb	r1, [r0], #1		@ 1
-	strb	r1, [r0], #1		@ 1
+	strltb	r1, [ip], #1		@ 1
+	strleb	r1, [ip], #1		@ 1
+	strb	r1, [ip], #1		@ 1
 	add	r2, r2, r3		@ 1 (r2 = r2 - (4 - r3))
 /*
  * The pointer is now aligned and the length is adjusted.  Try doing the
@@ -29,10 +29,14 @@
  */
 
 ENTRY(memset)
-	ands	r3, r0, #3		@ 1 unaligned?
+/*
+ * Preserve the contents of r0 for the return value.
+ */
+	mov	ip, r0
+	ands	r3, ip, #3		@ 1 unaligned?
 	bne	1b			@ 1
 /*
- * we know that the pointer in r0 is aligned to a word boundary.
+ * we know that the pointer in ip is aligned to a word boundary.
  */
 	orr	r1, r1, r1, lsl #8
 	orr	r1, r1, r1, lsl #16
@@ -43,29 +47,28 @@ ENTRY(memset)
 #if ! CALGN(1)+0
 
 /*
- * We need an extra register for this loop - save the return address and
- * use the LR
+ * We need an 2 extra registers for this loop - use r8 and the LR
  */
-	str	lr, [sp, #-4]!
-	mov	ip, r1
+	stmfd	sp!, {r8, lr}
+	mov	r8, r1
 	mov	lr, r1
 
 2:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3, ip, lr}	@ 64 bytes at a time.
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}	@ 64 bytes at a time.
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
 	bgt	2b
-	ldmeqfd	sp!, {pc}		@ Now <64 bytes to go.
+	ldmeqfd	sp!, {r8, pc}		@ Now <64 bytes to go.
 /*
  * No need to correct the count; we're only testing bits from now on
  */
 	tst	r2, #32
-	stmneia	r0!, {r1, r3, ip, lr}
-	stmneia	r0!, {r1, r3, ip, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r1, r3, ip, lr}
-	ldr	lr, [sp], #4
+	stmneia	ip!, {r1, r3, r8, lr}
+	ldmfd	sp!, {r8, lr}
 
 #else
 
@@ -74,54 +77,54 @@ ENTRY(memset)
  * whole cache lines@once.
  */
 
-	stmfd	sp!, {r4-r7, lr}
+	stmfd	sp!, {r4-r8, lr}
 	mov	r4, r1
 	mov	r5, r1
 	mov	r6, r1
 	mov	r7, r1
-	mov	ip, r1
+	mov	r8, r1
 	mov	lr, r1
 
 	cmp	r2, #96
-	tstgt	r0, #31
+	tstgt	ip, #31
 	ble	3f
 
-	and	ip, r0, #31
-	rsb	ip, ip, #32
-	sub	r2, r2, ip
-	movs	ip, ip, lsl #(32 - 4)
-	stmcsia	r0!, {r4, r5, r6, r7}
-	stmmiia	r0!, {r4, r5}
-	tst	ip, #(1 << 30)
-	mov	ip, r1
-	strne	r1, [r0], #4
+	and	r8, ip, #31
+	rsb	r8, r8, #32
+	sub	r2, r2, r8
+	movs	r8, r8, lsl #(32 - 4)
+	stmcsia	ip!, {r4, r5, r6, r7}
+	stmmiia	ip!, {r4, r5}
+	tst	r8, #(1 << 30)
+	mov	r8, r1
+	strne	r1, [ip], #4
 
 3:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
 	bgt	3b
-	ldmeqfd	sp!, {r4-r7, pc}
+	ldmeqfd	sp!, {r4-r8, pc}
 
 	tst	r2, #32
-	stmneia	r0!, {r1, r3-r7, ip, lr}
+	stmneia	ip!, {r1, r3-r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r4-r7}
-	ldmfd	sp!, {r4-r7, lr}
+	stmneia	ip!, {r4-r7}
+	ldmfd	sp!, {r4-r8, lr}
 
 #endif
 
 4:	tst	r2, #8
-	stmneia	r0!, {r1, r3}
+	stmneia	ip!, {r1, r3}
 	tst	r2, #4
-	strne	r1, [r0], #4
+	strne	r1, [ip], #4
 /*
  * When we get here, we've got less than 4 bytes to zero.  We
  * may have an unaligned pointer as well.
  */
 5:	tst	r2, #2
-	strneb	r1, [r0], #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
+	strneb	r1, [ip], #1
 	tst	r2, #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
 	mov	pc, lr
 ENDPROC(memset)
Signature · 1 line
1.7.10.4
#1 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 28 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0 <debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0 <memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}
Quoted 5 lines

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

This patch fixes the return value of the assembly version of memset. Could you please review, or suggest better alternatives ?

Ping,

Thanks,
--
Ivan

Quoted 221 lines

Thanks,

--
Ivan

(this is a shorter and (hopefully) clearer repost of
http://lists.infradead.org/pipermail/linux-arm-kernel/2013-January/144916.html)

The patch adds a 'mov' instruction and merges an additional load+store into existing load/store instructions.
For ease of review, here is a breakdown of the patch into 4 simple steps:

Step 1
======
Perform the following substitutions:
ip -> r8, then
r0 -> ip,
and insert 'mov ip, r0' as the first statement of the function. At this point, we have a memset() implementation returning the proper result, but corrupting r8 on some paths (the ones that were using ip).

Step 2
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 1:

save r8:

arch/arm/lib/memset.S +1 -1
-       str     lr, [sp, #-4]!
+       stmfd   sp!, {r8, lr}

and restore r8 on both exit paths:

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {pc}               @ Now <64 bytes to go.
+       ldmeqfd sp!, {r8, pc}           @ Now <64 bytes to go.
(...)
        tst     r2, #16
        stmneia ip!, {r1, r3, r8, lr}
-       ldr     lr, [sp], #4
+       ldmfd   sp!, {r8, lr}

Step 3
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 0:

save r8:

arch/arm/lib/memset.S +1 -1
-       stmfd   sp!, {r4-r7, lr}
+       stmfd   sp!, {r4-r8, lr}

and restore r8 on both exit paths:
bgt 3b

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {r4-r7, pc}
+       ldmeqfd sp!, {r4-r8, pc}
(...)
        tst     r2, #16
        stmneia ip!, {r4-r7}
-       ldmfd   sp!, {r4-r7, lr}
+       ldmfd   sp!, {r4-r8, lr}

Step 4
======
Rewrite register list "r4-r7, r8" as "r4-r8".

---
 arch/arm/lib/memset.S |   85 +++++++++++++++++++++++++------------------------
 1 file changed, 44 insertions(+), 41 deletions(-)
arch/arm/lib/memset.S +44 -41
index 650d592..eafd5ee 100644
@@ -19,9 +19,9 @@
 1:	subs	r2, r2, #4		@ 1 do we have enough
 	blt	5f			@ 1 bytes to align with?
 	cmp	r3, #2			@ 1
-	strltb	r1, [r0], #1		@ 1
-	strleb	r1, [r0], #1		@ 1
-	strb	r1, [r0], #1		@ 1
+	strltb	r1, [ip], #1		@ 1
+	strleb	r1, [ip], #1		@ 1
+	strb	r1, [ip], #1		@ 1
 	add	r2, r2, r3		@ 1 (r2 = r2 - (4 - r3))
 /*
  * The pointer is now aligned and the length is adjusted.  Try doing the
@@ -29,10 +29,14 @@
  */
 
 ENTRY(memset)
-	ands	r3, r0, #3		@ 1 unaligned?
+/*
+ * Preserve the contents of r0 for the return value.
+ */
+	mov	ip, r0
+	ands	r3, ip, #3		@ 1 unaligned?
 	bne	1b			@ 1
 /*
- * we know that the pointer in r0 is aligned to a word boundary.
+ * we know that the pointer in ip is aligned to a word boundary.
  */
 	orr	r1, r1, r1, lsl #8
 	orr	r1, r1, r1, lsl #16
@@ -43,29 +47,28 @@ ENTRY(memset)
 #if ! CALGN(1)+0
 
 /*
- * We need an extra register for this loop - save the return address and
- * use the LR
+ * We need an 2 extra registers for this loop - use r8 and the LR
  */
-	str	lr, [sp, #-4]!
-	mov	ip, r1
+	stmfd	sp!, {r8, lr}
+	mov	r8, r1
 	mov	lr, r1
 
 2:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3, ip, lr}	@ 64 bytes at a time.
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}	@ 64 bytes at a time.
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
 	bgt	2b
-	ldmeqfd	sp!, {pc}		@ Now <64 bytes to go.
+	ldmeqfd	sp!, {r8, pc}		@ Now <64 bytes to go.
 /*
  * No need to correct the count; we're only testing bits from now on
  */
 	tst	r2, #32
-	stmneia	r0!, {r1, r3, ip, lr}
-	stmneia	r0!, {r1, r3, ip, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r1, r3, ip, lr}
-	ldr	lr, [sp], #4
+	stmneia	ip!, {r1, r3, r8, lr}
+	ldmfd	sp!, {r8, lr}
 
 #else
 
@@ -74,54 +77,54 @@ ENTRY(memset)
  * whole cache lines at once.
  */
 
-	stmfd	sp!, {r4-r7, lr}
+	stmfd	sp!, {r4-r8, lr}
 	mov	r4, r1
 	mov	r5, r1
 	mov	r6, r1
 	mov	r7, r1
-	mov	ip, r1
+	mov	r8, r1
 	mov	lr, r1
 
 	cmp	r2, #96
-	tstgt	r0, #31
+	tstgt	ip, #31
 	ble	3f
 
-	and	ip, r0, #31
-	rsb	ip, ip, #32
-	sub	r2, r2, ip
-	movs	ip, ip, lsl #(32 - 4)
-	stmcsia	r0!, {r4, r5, r6, r7}
-	stmmiia	r0!, {r4, r5}
-	tst	ip, #(1 << 30)
-	mov	ip, r1
-	strne	r1, [r0], #4
+	and	r8, ip, #31
+	rsb	r8, r8, #32
+	sub	r2, r2, r8
+	movs	r8, r8, lsl #(32 - 4)
+	stmcsia	ip!, {r4, r5, r6, r7}
+	stmmiia	ip!, {r4, r5}
+	tst	r8, #(1 << 30)
+	mov	r8, r1
+	strne	r1, [ip], #4
 
 3:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
 	bgt	3b
-	ldmeqfd	sp!, {r4-r7, pc}
+	ldmeqfd	sp!, {r4-r8, pc}
 
 	tst	r2, #32
-	stmneia	r0!, {r1, r3-r7, ip, lr}
+	stmneia	ip!, {r1, r3-r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r4-r7}
-	ldmfd	sp!, {r4-r7, lr}
+	stmneia	ip!, {r4-r7}
+	ldmfd	sp!, {r4-r8, lr}
 
 #endif
 
 4:	tst	r2, #8
-	stmneia	r0!, {r1, r3}
+	stmneia	ip!, {r1, r3}
 	tst	r2, #4
-	strne	r1, [r0], #4
+	strne	r1, [ip], #4
 /*
  * When we get here, we've got less than 4 bytes to zero.  We
  * may have an unaligned pointer as well.
  */
 5:	tst	r2, #2
-	strneb	r1, [r0], #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
+	strneb	r1, [ip], #1
 	tst	r2, #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
 	mov	pc, lr
 ENDPROC(memset)
Signature · 1 line
1.7.10.4
#2 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

In reply to Ivan Djelic
Quoted 90 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0 <debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0 <memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

This patch fixes the return value of the assembly version of memset. Could you please review, or suggest better alternatives ?

Thanks,

--
Ivan

(this is a shorter and (hopefully) clearer repost of
http://lists.infradead.org/pipermail/linux-arm-kernel/2013-January/144916.html)

The patch adds a 'mov' instruction and merges an additional load+store into existing load/store instructions.
For ease of review, here is a breakdown of the patch into 4 simple steps:

Step 1
======
Perform the following substitutions:
ip -> r8, then
r0 -> ip,
and insert 'mov ip, r0' as the first statement of the function. At this point, we have a memset() implementation returning the proper result, but corrupting r8 on some paths (the ones that were using ip).

Step 2
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 1:

save r8:

arch/arm/lib/memset.S +1 -1
-       str     lr, [sp, #-4]!
+       stmfd   sp!, {r8, lr}

and restore r8 on both exit paths:

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {pc}               @ Now <64 bytes to go.
+       ldmeqfd sp!, {r8, pc}           @ Now <64 bytes to go.
(...)
        tst     r2, #16
        stmneia ip!, {r1, r3, r8, lr}
-       ldr     lr, [sp], #4
+       ldmfd   sp!, {r8, lr}

Step 3
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 0:

save r8:

arch/arm/lib/memset.S +1 -1
-       stmfd   sp!, {r4-r7, lr}
+       stmfd   sp!, {r4-r8, lr}

and restore r8 on both exit paths:
bgt 3b

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {r4-r7, pc}
+       ldmeqfd sp!, {r4-r8, pc}
(...)
        tst     r2, #16
        stmneia ip!, {r4-r7}
-       ldmfd   sp!, {r4-r7, lr}
+       ldmfd   sp!, {r4-r8, lr}
Quoted 5 lines

Step 4
======
Rewrite register list "r4-r7, r8" as "r4-r8".

This is good for the stable tree as well.

Please include in your official commit text the above breakdown explanation as well.

Quoted 167 lines
---
 arch/arm/lib/memset.S |   85 +++++++++++++++++++++++++------------------------
 1 file changed, 44 insertions(+), 41 deletions(-)
arch/arm/lib/memset.S +44 -41
index 650d592..eafd5ee 100644
@@ -19,9 +19,9 @@
 1:	subs	r2, r2, #4		@ 1 do we have enough
 	blt	5f			@ 1 bytes to align with?
 	cmp	r3, #2			@ 1
-	strltb	r1, [r0], #1		@ 1
-	strleb	r1, [r0], #1		@ 1
-	strb	r1, [r0], #1		@ 1
+	strltb	r1, [ip], #1		@ 1
+	strleb	r1, [ip], #1		@ 1
+	strb	r1, [ip], #1		@ 1
 	add	r2, r2, r3		@ 1 (r2 = r2 - (4 - r3))
 /*
  * The pointer is now aligned and the length is adjusted.  Try doing the
@@ -29,10 +29,14 @@
  */
 
 ENTRY(memset)
-	ands	r3, r0, #3		@ 1 unaligned?
+/*
+ * Preserve the contents of r0 for the return value.
+ */
+	mov	ip, r0
+	ands	r3, ip, #3		@ 1 unaligned?
 	bne	1b			@ 1
 /*
- * we know that the pointer in r0 is aligned to a word boundary.
+ * we know that the pointer in ip is aligned to a word boundary.
  */
 	orr	r1, r1, r1, lsl #8
 	orr	r1, r1, r1, lsl #16
@@ -43,29 +47,28 @@ ENTRY(memset)
 #if ! CALGN(1)+0
 
 /*
- * We need an extra register for this loop - save the return address and
- * use the LR
+ * We need an 2 extra registers for this loop - use r8 and the LR
  */
-	str	lr, [sp, #-4]!
-	mov	ip, r1
+	stmfd	sp!, {r8, lr}
+	mov	r8, r1
 	mov	lr, r1
 
 2:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3, ip, lr}	@ 64 bytes at a time.
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}	@ 64 bytes at a time.
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
 	bgt	2b
-	ldmeqfd	sp!, {pc}		@ Now <64 bytes to go.
+	ldmeqfd	sp!, {r8, pc}		@ Now <64 bytes to go.
 /*
  * No need to correct the count; we're only testing bits from now on
  */
 	tst	r2, #32
-	stmneia	r0!, {r1, r3, ip, lr}
-	stmneia	r0!, {r1, r3, ip, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
+	stmneia	ip!, {r1, r3, r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r1, r3, ip, lr}
-	ldr	lr, [sp], #4
+	stmneia	ip!, {r1, r3, r8, lr}
+	ldmfd	sp!, {r8, lr}
 
 #else
 
@@ -74,54 +77,54 @@ ENTRY(memset)
  * whole cache lines at once.
  */
 
-	stmfd	sp!, {r4-r7, lr}
+	stmfd	sp!, {r4-r8, lr}
 	mov	r4, r1
 	mov	r5, r1
 	mov	r6, r1
 	mov	r7, r1
-	mov	ip, r1
+	mov	r8, r1
 	mov	lr, r1
 
 	cmp	r2, #96
-	tstgt	r0, #31
+	tstgt	ip, #31
 	ble	3f
 
-	and	ip, r0, #31
-	rsb	ip, ip, #32
-	sub	r2, r2, ip
-	movs	ip, ip, lsl #(32 - 4)
-	stmcsia	r0!, {r4, r5, r6, r7}
-	stmmiia	r0!, {r4, r5}
-	tst	ip, #(1 << 30)
-	mov	ip, r1
-	strne	r1, [r0], #4
+	and	r8, ip, #31
+	rsb	r8, r8, #32
+	sub	r2, r2, r8
+	movs	r8, r8, lsl #(32 - 4)
+	stmcsia	ip!, {r4, r5, r6, r7}
+	stmmiia	ip!, {r4, r5}
+	tst	r8, #(1 << 30)
+	mov	r8, r1
+	strne	r1, [ip], #4
 
 3:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
-	stmgeia	r0!, {r1, r3-r7, ip, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
+	stmgeia	ip!, {r1, r3-r8, lr}
 	bgt	3b
-	ldmeqfd	sp!, {r4-r7, pc}
+	ldmeqfd	sp!, {r4-r8, pc}
 
 	tst	r2, #32
-	stmneia	r0!, {r1, r3-r7, ip, lr}
+	stmneia	ip!, {r1, r3-r8, lr}
 	tst	r2, #16
-	stmneia	r0!, {r4-r7}
-	ldmfd	sp!, {r4-r7, lr}
+	stmneia	ip!, {r4-r7}
+	ldmfd	sp!, {r4-r8, lr}
 
 #endif
 
 4:	tst	r2, #8
-	stmneia	r0!, {r1, r3}
+	stmneia	ip!, {r1, r3}
 	tst	r2, #4
-	strne	r1, [r0], #4
+	strne	r1, [ip], #4
 /*
  * When we get here, we've got less than 4 bytes to zero.  We
  * may have an unaligned pointer as well.
  */
 5:	tst	r2, #2
-	strneb	r1, [r0], #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
+	strneb	r1, [ip], #1
 	tst	r2, #1
-	strneb	r1, [r0], #1
+	strneb	r1, [ip], #1
 	mov	pc, lr
 ENDPROC(memset)
Signature · 7 lines
1.7.10.4


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#3 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 96 lines · Nicolas Pitre
Quoted 94 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0 <debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0 <memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

This patch fixes the return value of the assembly version of memset. Could you please review, or suggest better alternatives ?

Thanks,

--
Ivan

(this is a shorter and (hopefully) clearer repost of
http://lists.infradead.org/pipermail/linux-arm-kernel/2013-January/144916.html)

The patch adds a 'mov' instruction and merges an additional load+store into existing load/store instructions.
For ease of review, here is a breakdown of the patch into 4 simple steps:

Step 1
======
Perform the following substitutions:
ip -> r8, then
r0 -> ip,
and insert 'mov ip, r0' as the first statement of the function. At this point, we have a memset() implementation returning the proper result, but corrupting r8 on some paths (the ones that were using ip).

Step 2
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 1:

save r8:

arch/arm/lib/memset.S +1 -1
-       str     lr, [sp, #-4]!
+       stmfd   sp!, {r8, lr}

and restore r8 on both exit paths:

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {pc}               @ Now <64 bytes to go.
+       ldmeqfd sp!, {r8, pc}           @ Now <64 bytes to go.
(...)
        tst     r2, #16
        stmneia ip!, {r1, r3, r8, lr}
-       ldr     lr, [sp], #4
+       ldmfd   sp!, {r8, lr}

Step 3
======
Make sure r8 is saved and restored when (! CALGN(1)+0) == 0:

save r8:

arch/arm/lib/memset.S +1 -1
-       stmfd   sp!, {r4-r7, lr}
+       stmfd   sp!, {r4-r8, lr}

and restore r8 on both exit paths:
bgt 3b

arch/arm/lib/memset.S +2 -2
-       ldmeqfd sp!, {r4-r7, pc}
+       ldmeqfd sp!, {r4-r8, pc}
(...)
        tst     r2, #16
        stmneia ip!, {r4-r7}
-       ldmfd   sp!, {r4-r7, lr}
+       ldmfd   sp!, {r4-r8, lr}

Step 4
======
Rewrite register list "r4-r7, r8" as "r4-r8".

Quoted 8 lines
Quoted 1 line

This is good for the stable tree as well.

Please include in your official commit text the above breakdown explanation as well.

OK, will do.
Thanks a lot for reviewing,
--
Ivan

#4 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

In reply to Ivan Djelic
Quoted 35 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0<debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0<memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

arch/arm/lib/memset.S +0 -0
@@ -43,29 +47,28 @@ ENTRY(memset)
  #if ! CALGN(1)+0
 

arch/arm/lib/memset.S ENTRY(memset) · line 54

Quoted 10 lines
arch/arm/lib/memset.S +3 -4
  /*
- * We need an extra register for this loop - save the return address and
- * use the LR
+ * We need an 2 extra registers for this loop - use r8 and the LR
   */
-	str	lr, [sp, #-4]!
-	mov	ip, r1
+	stmfd	sp!, {r8, lr}
+	mov	r8, r1
  	mov	lr, r1

Out of interest, why not save {r0, lr} and avoid having to re-write the entirety of the inner loop?

Quoted 13 lines
arch/arm/lib/memset.S +5 -5
  2:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3, ip, lr}	@ 64 bytes at a time.
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}	@ 64 bytes at a time.
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
  	bgt	2b
-	ldmeqfd	sp!, {pc}		@ Now<64 bytes to go.
+	ldmeqfd	sp!, {r8, pc}		@ Now<64 bytes to go.
Signature · 2 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius
#5 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 36 lines · Ben Dooks
Quoted 35 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

For instance in the following function:

void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

compiled as:

800554d0<debug_mutex_lock_common>:

800554d0:       e92d4008        push    {r3, lr}
800554d4:       e1a00001        mov     r0, r1
800554d8:       e3a02010        mov     r2, #16 ; 0x10
800554dc:       e3a01011        mov     r1, #17 ; 0x11
800554e0:       eb04426e        bl      80165ea0<memset>
800554e4:       e1a03000        mov     r3, r0
800554e8:       e583000c        str     r0, [r3, #12]
800554ec:       e5830000        str     r0, [r3]
800554f0:       e5830004        str     r0, [r3, #4]
800554f4:       e8bd8008        pop     {r3, pc}

GCC assumes memset returns the value of pointer 'waiter' in register r0; causing register/memory corruptions.

arch/arm/lib/memset.S +0 -0
@@ -43,29 +47,28 @@ ENTRY(memset)
  #if ! CALGN(1)+0
 
Quoted 13 lines

arch/arm/lib/memset.S ENTRY(memset) · line 54

Quoted 10 lines
arch/arm/lib/memset.S +3 -4
  /*
- * We need an extra register for this loop - save the return address and
- * use the LR
+ * We need an 2 extra registers for this loop - use r8 and the LR
   */
-	str	lr, [sp, #-4]!
-	mov	ip, r1
+	stmfd	sp!, {r8, lr}
+	mov	r8, r1
  	mov	lr, r1

Out of interest, why not save {r0, lr} and avoid having to re-write the entirety of the inner loop?

Because at the inner loop entry, r0 no longer contains the current write pointer: ip is the current pointer, possibly different from r0 because of the unaligned fixup code. The idea was to avoid introducing an extra load+store on load/store-free paths; thereby using a register other than r0 for the current pointer...

BR,
--
Ivan

Quoted 19 lines
Quoted 14 lines
arch/arm/lib/memset.S +5 -5
  2:	subs	r2, r2, #64
-	stmgeia	r0!, {r1, r3, ip, lr}	@ 64 bytes at a time.
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
-	stmgeia	r0!, {r1, r3, ip, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}	@ 64 bytes at a time.
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
+	stmgeia	ip!, {r1, r3, r8, lr}
  	bgt	2b
-	ldmeqfd	sp!, {pc}		@ Now<64 bytes to go.
+	ldmeqfd	sp!, {r8, pc}		@ Now<64 bytes to go.
Signature · 2 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius
#6 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

In reply to Ivan Djelic
Quoted 4 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

How does GCC do this? By strcmping the function name and assuming that memset is a libc implementation?

If so, maybe passing something like -ffreestanding would make sense to turn this behaviour off in the compiler (otherwise we should also vet the rest of the standard string functions).

Will

#7 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 6 lines · Will Deacon
Quoted 4 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

Quoted 6 lines

How does GCC do this? By strcmping the function name and assuming that memset is a libc implementation?

If so, maybe passing something like -ffreestanding would make sense to turn this behaviour off in the compiler (otherwise we should also vet the rest of the standard string functions).

In theory, yes; but there is actually a short list of libc functions that GCC always requires from the environment, even when -ffreestanding is used: memcpy, memmove, memset and memcmp (see [1] below).

Which also means that the same corruption problem potentially exists for current ARM assembly implementations of memcpy and memmove...

--
Ivan

[1] http://gcc.gnu.org/onlinedocs/gcc/Standards.html

GCC aims towards being usable as a conforming freestanding implementation, or as the compiler for a conforming hosted implementation. By default, it will act as the compiler for a hosted implementation, defining __STDC_HOSTED__ as 1 and presuming that when the names of ISO C functions are used, they have the semantics defined in the standard. To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding; it will then define __STDC_HOSTED__ to 0 and not make assumptions about the meanings of function names from the standard library, with exceptions noted below. To build an OS kernel, you may well still need to make your own arrangements for linking and startup. See Options Controlling C Dialect.

GCC does not provide the library facilities required only of hosted implementations, nor yet all the facilities required by C99 of freestanding implementations; to use the facilities of a hosted environment, you will need to find them elsewhere (for example, in the GNU C library). See Standard Libraries.

Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions. GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. Finally, if __builtin_trap is used, and the target does not implement the trap pattern, then GCC will emit a call to abort.

#8 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 10 lines · Ivan Djelic
Quoted 9 lines · Will Deacon
Quoted 4 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

How does GCC do this? By strcmping the function name and assuming that memset is a libc implementation?

Quoted 7 lines
Quoted 3 lines

If so, maybe passing something like -ffreestanding would make sense to turn this behaviour off in the compiler (otherwise we should also vet the rest of the standard string functions).

In theory, yes; but there is actually a short list of libc functions that GCC always requires from the environment, even when -ffreestanding is used: memcpy, memmove, memset and memcmp (see [1] below).

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Will

#9 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 15 lines · Will Deacon
Quoted 14 lines · Ivan Djelic
Quoted 12 lines · Will Deacon
Quoted 4 lines · Ivan Djelic

Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on assumptions about the implementation of memset and similar functions. The current ARM optimized memset code does not return the value of its first argument, as is usually expected from standard implementations.

How does GCC do this? By strcmping the function name and assuming that memset is a libc implementation?

If so, maybe passing something like -ffreestanding would make sense to turn this behaviour off in the compiler (otherwise we should also vet the rest of the standard string functions).

Quoted 8 lines
Quoted 3 lines

In theory, yes; but there is actually a short list of libc functions that GCC always requires from the environment, even when -ffreestanding is used: memcpy, memmove, memset and memcmp (see [1] below).

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

#10 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 10 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

Quoted 7 lines

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Will

#11 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 4 lines · Will Deacon

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

No we don't. memset() is the only one which I created in a non-standard way.

#12 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 8 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

No we don't. memset() is the only one which I created in a non-standard way.

Ok, I hadn't checked so that saves us some effort.

Cheers,

Will

#13 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

In reply to Will Deacon
Quoted 16 lines · Will Deacon
Quoted 15 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

Quoted 7 lines
Quoted 2 lines

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Do we already have an agreed solution for this issue anywhere, now?

It seems some people hit by this issue are about to import Ivan's v2 patch. Which doesn't seem to be the final fix, though, if I read the history of this thread correctly?

Many thanks

Dirk

#14 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Hi Dirk,

Quoted 20 lines · Dirk Behme
Quoted 19 lines · Will Deacon
Quoted 17 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

Quoted 6 lines
Quoted 4 lines

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Do we already have an agreed solution for this issue anywhere, now?

I thought that the conclusion was to go with Ivan's suggestion since memset is the only function which doesn't follow what the compiler expects.

Will

#15 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 21 lines · Will Deacon

Hi Dirk,

Quoted 18 lines · Dirk Behme
Quoted 17 lines · Will Deacon
Quoted 16 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Do we already have an agreed solution for this issue anywhere, now?

I thought that the conclusion was to go with Ivan's suggestion since memset is the only function which doesn't follow what the compiler expects.

Ok, thanks! :)

Could we get Ivan's v2 patch integrated or at least sent to the patch system, then, please?

Many thanks,

Dirk

#16 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

In reply to Dirk Behme
Quoted 20 lines · Dirk Behme
Quoted 19 lines · Will Deacon
Quoted 17 lines · Russell King - ARM Linux
Quoted 4 lines · Will Deacon

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

Quoted 6 lines
Quoted 4 lines

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Do we already have an agreed solution for this issue anywhere, now?

No idea. I've stated above what I think should happen. Where's the disagreement?

#17 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Am 06.03.2013 18:11, schrieb Russell King - ARM Linux:

Quoted 26 lines · Dirk Behme
Quoted 25 lines · Will Deacon
Quoted 23 lines · Russell King - ARM Linux
Quoted 10 lines · Will Deacon
Quoted 4 lines

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

Quoted 7 lines

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Quoted 4 lines
Quoted 1 line

Do we already have an agreed solution for this issue anywhere, now?

No idea. I've stated above what I think should happen. Where's the disagreement?

I'm not sure if there even is a disagreement ;)

I've asked because it seems that Ivan's v2 patch [1] isn't applied anywhere, yet. This let me to the impression that this might not be the final ('agreed') fix and that something still has to be done.

Will's answer to my question [2] sounds like Ivan's v2 patch [1] is the final solution, though. If this is correct, what stops us from applying [1]?

Sorry if I misunderstood anything.

Thanks

Dirk

[1] http://www.spinics.net/lists/arm-kernel/msg224019.html

[2] http://www.spinics.net/lists/arm-kernel/msg228036.html

#18 lore raw

RFC PATCH 1/1 arm: fix memset-related crashes caused by recent GCC (4.7.2) optimizations

Quoted 29 lines · Dirk Behme

Am 06.03.2013 18:11, schrieb Russell King - ARM Linux:

Quoted 28 lines · Dirk Behme
Quoted 26 lines · Will Deacon
Quoted 16 lines · Russell King - ARM Linux
Quoted 15 lines · Will Deacon
Quoted 4 lines

Interesting... the GCC documentation also states that ffreestanding implies fno-builtin, so memset and co shouldn't be targetted for this sort of optimisation by GCC. Have you observed this problem even when passing this option?

Rather than wondering whether we should be using -ffreestanding or not (which, x86 people have strongly resisted) I suggest that we just fix our memset() implementation to be compliant.

The reason it's not compliant is that I saw no reason for it to be compliant back in the gcc 2.7.x days, and it's persisted like that for the last 19-ish years. If GCC is now making use of the return value, then we need to fix that and undo the "optimization" in our string.h.

Quoted 7 lines
Quoted 2 lines

So let's just bite the bullet, make the asm memset() compliant, and clean up string.h.

That would be the ideal thing to do, because it allows the compiler to optimise around these functions, however it does mean we need to check/fix *all* of the string functions at least (if we don't pass -fno-builtin then any builtin function is up for optimisation, including strcpy etc).

Do we already have an agreed solution for this issue anywhere, now?

Quoted 8 lines
Quoted 2 lines

No idea. I've stated above what I think should happen. Where's the disagreement?

I'm not sure if there even is a disagreement ;)

I've asked because it seems that Ivan's v2 patch [1] isn't applied anywhere, yet. This let me to the impression that this might not be the final ('agreed') fix and that something still has to be done.

Well, I don't generally pick patches off the mailing list. It's too difficult for me to do that. Emails *very* *very* quickly get buried and forgotten, especially as I don't look at emails each and every day (this afternoon is the first time since Monday afternoon, and I'm not going to look back before Monday as there's been 500 _new_ emails which I'm still going through.)

This is exactly why I have the patch system - to help capture the patches which need to be applied.