Skip to contentSkip to conversation

x86/asm/32: Modernize memset() functions

PATCH In review Uros Bizjak 4 participants 9 messages May 6, 2025 – May 7, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 x86/asm/32: Modernize memset() functions

Use inout "+" constraint modifier where appropriate and declare temporary variable using __auto_type, similar to what x86_64 does.

No functional changes intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/string_32.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/string_32.h +12 -12
index e9cce169bb4c..9152d2c0f60e 100644
@@ -164,12 +164,12 @@ extern void *memchr(const void *cs, int c, size_t count);
 
 static inline void *__memset_generic(void *s, char c, size_t count)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosb"
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (c), "1" (s), "0" (count)
+		     : "+D" (s), "+c" (count)
+		     : "a" (c)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 /* we might want to write optimized versions of these later */
@@ -197,23 +197,23 @@ extern void *memset(void *, int, size_t);
 #define __HAVE_ARCH_MEMSET16
 static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosw"
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (v), "1" (s), "0" (n)
+		     : "+D" (s), "+c" (n)
+		     : "a" (v)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 #define __HAVE_ARCH_MEMSET32
 static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosl"
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (v), "1" (s), "0" (n)
+		     : "+D" (s), "+c" (n)
+		     : "a" (v)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 /*
Signature · 1 line
2.49.0
#1 lore raw

PATCH 2/3 x86/asm/32: Modernize __constant_memcpy()

Use inout "+" constraint modifier where appropriate and declare temporary variables as unsigned long.

No functional changes intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/string_32.h | 51 +++++++++++++-------------------
 1 file changed, 21 insertions(+), 30 deletions(-)
arch/x86/include/asm/string_32.h +21 -30
index 9152d2c0f60e..00d497837571 100644
@@ -52,7 +52,7 @@ static __always_inline void *__memcpy(void *to, const void *from, size_t n)
 static __always_inline void *__constant_memcpy(void *to, const void *from,
 					       size_t n)
 {
-	long esi, edi;
+	unsigned long esi, edi;
 	if (!n)
 		return to;
 
@@ -84,60 +84,51 @@ static __always_inline void *__constant_memcpy(void *to, const void *from,
 		return to;
 	}
 
-	esi = (long)from;
-	edi = (long)to;
+	esi = (unsigned long)from;
+	edi = (unsigned long)to;
 	if (n >= 5 * 4) {
 		/* large block: use rep prefix */
-		int ecx;
+		unsigned long ecx = n >> 2;
 		asm volatile("rep movsl"
-			     : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
-			     : "0" (n / 4), "1" (edi), "2" (esi)
-			     : "memory"
-		);
+			     : "+D" (edi), "+S" (esi), "+c" (ecx)
+			     : : "memory");
 	} else {
 		/* small block: don't clobber ecx + smaller code */
 		if (n >= 4 * 4)
 			asm volatile("movsl"
-				     : "=&D"(edi), "=&S"(esi)
-				     : "0"(edi), "1"(esi)
-				     : "memory");
+				     : "+D" (edi), "+S" (esi)
+				     : : "memory");
 		if (n >= 3 * 4)
 			asm volatile("movsl"
-				     : "=&D"(edi), "=&S"(esi)
-				     : "0"(edi), "1"(esi)
-				     : "memory");
+				     : "+D" (edi), "+S" (esi)
+				     : : "memory");
 		if (n >= 2 * 4)
 			asm volatile("movsl"
-				     : "=&D"(edi), "=&S"(esi)
-				     : "0"(edi), "1"(esi)
-				     : "memory");
+				     : "+D" (edi), "+S" (esi)
+				     : : "memory");
 		if (n >= 1 * 4)
 			asm volatile("movsl"
-				     : "=&D"(edi), "=&S"(esi)
-				     : "0"(edi), "1"(esi)
-				     : "memory");
+				     : "+D" (edi), "+S" (esi)
+				     : : "memory");
 	}
-	switch (n % 4) {
+	switch (n & 3) {
 		/* tail */
 	case 0:
 		return to;
 	case 1:
 		asm volatile("movsb"
-			     : "=&D"(edi), "=&S"(esi)
-			     : "0"(edi), "1"(esi)
-			     : "memory");
+			     : "+D" (edi), "+S" (esi)
+			     : : "memory");
 		return to;
 	case 2:
 		asm volatile("movsw"
-			     : "=&D"(edi), "=&S"(esi)
-			     : "0"(edi), "1"(esi)
-			     : "memory");
+			     : "+D" (edi), "+S" (esi)
+			     : : "memory");
 		return to;
 	default:
 		asm volatile("movsw\n\tmovsb"
-			     : "=&D"(edi), "=&S"(esi)
-			     : "0"(edi), "1"(esi)
-			     : "memory");
+			     : "+D" (edi), "+S" (esi)
+			     : : "memory");
 		return to;
 	}
 }
Signature · 1 line
2.49.0
#2 lore raw

PATCH 3/3 x86/asm/32: Modernize _memcpy()

In reply to Uros Bizjak

Use inout "+" constraint modifier where appropriate, declare temporary variables as unsigned long and rewrite parts of assembly in plain C. The memcpy() function shrinks by 10 bytes, from:

00e778d0 <memcpy>:

  e778d0:	55                   	push   %ebp
  e778d1:	89 e5                	mov    %esp,%ebp
  e778d3:	83 ec 0c             	sub    $0xc,%esp
  e778d6:	89 5d f4             	mov    %ebx,-0xc(%ebp)
  e778d9:	89 c3                	mov    %eax,%ebx
  e778db:	89 c8                	mov    %ecx,%eax
  e778dd:	89 75 f8             	mov    %esi,-0x8(%ebp)
  e778e0:	c1 e9 02             	shr    $0x2,%ecx
  e778e3:	89 d6                	mov    %edx,%esi
  e778e5:	89 7d fc             	mov    %edi,-0x4(%ebp)
  e778e8:	89 df                	mov    %ebx,%edi
  e778ea:	f3 a5                	rep movsl %ds:(%esi),%es:(%edi)
  e778ec:	89 c1                	mov    %eax,%ecx
  e778ee:	83 e1 03             	and    $0x3,%ecx
  e778f1:	74 02                	je     e778f5 <memcpy+0x25>
  e778f3:	f3 a4                	rep movsb %ds:(%esi),%es:(%edi)
  e778f5:	8b 75 f8             	mov    -0x8(%ebp),%esi
  e778f8:	89 d8                	mov    %ebx,%eax
  e778fa:	8b 5d f4             	mov    -0xc(%ebp),%ebx
  e778fd:	8b 7d fc             	mov    -0x4(%ebp),%edi
  e77900:	89 ec                	mov    %ebp,%esp
  e77902:	5d                   	pop    %ebp
  e77903:	c3                   	ret

to:

00e778b0 <memcpy>:

  e778b0:	55                   	push   %ebp
  e778b1:	89 e5                	mov    %esp,%ebp
  e778b3:	83 ec 08             	sub    $0x8,%esp
  e778b6:	89 75 f8             	mov    %esi,-0x8(%ebp)
  e778b9:	89 d6                	mov    %edx,%esi
  e778bb:	89 ca                	mov    %ecx,%edx
  e778bd:	89 7d fc             	mov    %edi,-0x4(%ebp)
  e778c0:	c1 e9 02             	shr    $0x2,%ecx
  e778c3:	89 c7                	mov    %eax,%edi
  e778c5:	f3 a5                	rep movsl %ds:(%esi),%es:(%edi)
  e778c7:	83 e2 03             	and    $0x3,%edx
  e778ca:	74 04                	je     e778d0 <memcpy+0x20>
  e778cc:	89 d1                	mov    %edx,%ecx
  e778ce:	f3 a4                	rep movsb %ds:(%esi),%es:(%edi)
  e778d0:	8b 75 f8             	mov    -0x8(%ebp),%esi
  e778d3:	8b 7d fc             	mov    -0x4(%ebp),%edi
  e778d6:	89 ec                	mov    %ebp,%esp
  e778d8:	5d                   	pop    %ebp
  e778d9:	c3                   	ret

due to a better register allocation, avoiding the call-saved %ebx register.

No functional changes intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/string_32.h | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
arch/x86/include/asm/string_32.h +12 -10
index 00d497837571..6a4062414495 100644
@@ -32,16 +32,18 @@ extern size_t strlen(const char *s);
 
 static __always_inline void *__memcpy(void *to, const void *from, size_t n)
 {
-	int d0, d1, d2;
-	asm volatile("rep movsl\n\t"
-		     "movl %4,%%ecx\n\t"
-		     "andl $3,%%ecx\n\t"
-		     "jz 1f\n\t"
-		     "rep movsb\n\t"
-		     "1:"
-		     : "=&c" (d0), "=&D" (d1), "=&S" (d2)
-		     : "0" (n / 4), "g" (n), "1" ((long)to), "2" ((long)from)
-		     : "memory");
+	unsigned long esi = (unsigned long)from;
+	unsigned long edi = (unsigned long)to;
+	unsigned long ecx = n >> 2;
+
+	asm volatile("rep movsl"
+		     : "+D" (edi), "+S" (esi), "+c" (ecx)
+		     : : "memory");
+	ecx = n & 3;
+	if (ecx)
+		asm volatile("rep movsb"
+			     : "+D" (edi), "+S" (esi), "+c" (ecx)
+			     : : "memory");
 	return to;
 }
 
Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH -tip 3/3] x86/asm/32: Modernize _memcpy()

Quoted 53 lines · Uros Bizjak

Use inout "+" constraint modifier where appropriate, declare temporary variables as unsigned long and rewrite parts of assembly in plain C. The memcpy() function shrinks by 10 bytes, from:

00e778d0 <memcpy>:

  e778d0:       55                      push   %ebp
  e778d1:       89 e5                   mov    %esp,%ebp
  e778d3:       83 ec 0c                sub    $0xc,%esp
  e778d6:       89 5d f4                mov    %ebx,-0xc(%ebp)
  e778d9:       89 c3                   mov    %eax,%ebx
  e778db:       89 c8                   mov    %ecx,%eax
  e778dd:       89 75 f8                mov    %esi,-0x8(%ebp)
  e778e0:       c1 e9 02                shr    $0x2,%ecx
  e778e3:       89 d6                   mov    %edx,%esi
  e778e5:       89 7d fc                mov    %edi,-0x4(%ebp)
  e778e8:       89 df                   mov    %ebx,%edi
  e778ea:       f3 a5                   rep movsl %ds:(%esi),%es:(%edi)
  e778ec:       89 c1                   mov    %eax,%ecx
  e778ee:       83 e1 03                and    $0x3,%ecx
  e778f1:       74 02                   je     e778f5 <memcpy+0x25>
  e778f3:       f3 a4                   rep movsb %ds:(%esi),%es:(%edi)
  e778f5:       8b 75 f8                mov    -0x8(%ebp),%esi
  e778f8:       89 d8                   mov    %ebx,%eax
  e778fa:       8b 5d f4                mov    -0xc(%ebp),%ebx
  e778fd:       8b 7d fc                mov    -0x4(%ebp),%edi
  e77900:       89 ec                   mov    %ebp,%esp
  e77902:       5d                      pop    %ebp
  e77903:       c3                      ret

to:

00e778b0 <memcpy>:

  e778b0:       55                      push   %ebp
  e778b1:       89 e5                   mov    %esp,%ebp
  e778b3:       83 ec 08                sub    $0x8,%esp
  e778b6:       89 75 f8                mov    %esi,-0x8(%ebp)
  e778b9:       89 d6                   mov    %edx,%esi
  e778bb:       89 ca                   mov    %ecx,%edx
  e778bd:       89 7d fc                mov    %edi,-0x4(%ebp)
  e778c0:       c1 e9 02                shr    $0x2,%ecx
  e778c3:       89 c7                   mov    %eax,%edi
  e778c5:       f3 a5                   rep movsl %ds:(%esi),%es:(%edi)
  e778c7:       83 e2 03                and    $0x3,%edx
  e778ca:       74 04                   je     e778d0 <memcpy+0x20>
  e778cc:       89 d1                   mov    %edx,%ecx
  e778ce:       f3 a4                   rep movsb %ds:(%esi),%es:(%edi)
  e778d0:       8b 75 f8                mov    -0x8(%ebp),%esi
  e778d3:       8b 7d fc                mov    -0x4(%ebp),%edi
  e778d6:       89 ec                   mov    %ebp,%esp
  e778d8:       5d                      pop    %ebp
  e778d9:       c3                      ret
Quoted 1 line

due to a better register allocation, avoiding the call-saved

Oops, this should have been written as "... avoiding the callee-saved ..."

Quoted 1 line

%ebx register.

Uros.

#4 lore raw

Re: [PATCH -tip 3/3] x86/asm/32: Modernize _memcpy()

Quoted 15 lines · On Tue, 6 May 2025 19:34:34 +0200 Uros Bizjak <[email protected]> wrote:
Quoted 14 lines · Uros Bizjak

Use inout "+" constraint modifier where appropriate, declare temporary variables as unsigned long and rewrite parts of assembly in plain C. The memcpy() function shrinks by 10 bytes, from:

00e778d0 <memcpy>:

  e778d0:       55                      push   %ebp
  e778d1:       89 e5                   mov    %esp,%ebp
  e778d3:       83 ec 0c                sub    $0xc,%esp
  e778d6:       89 5d f4                mov    %ebx,-0xc(%ebp)
  e778d9:       89 c3                   mov    %eax,%ebx
  e778db:       89 c8                   mov    %ecx,%eax
  e778dd:       89 75 f8                mov    %esi,-0x8(%ebp)
  e778e0:       c1 e9 02                shr    $0x2,%ecx
Quoted 8 lines
Quoted 8 lines
  e778e3:       89 d6                   mov    %edx,%esi
  e778e5:       89 7d fc                mov    %edi,-0x4(%ebp)
  e778e8:       89 df                   mov    %ebx,%edi
  e778ea:       f3 a5                   rep movsl %ds:(%esi),%es:(%edi)
  e778ec:       89 c1                   mov    %eax,%ecx
  e778ee:       83 e1 03                and    $0x3,%ecx
  e778f1:       74 02                   je     e778f5 <memcpy+0x25>
  e778f3:       f3 a4                   rep movsb %ds:(%esi),%es:(%edi)

Hmmm....
IIRC you really don't want to be doing a [1..3] byte 'rep movsb' there. Some cpu will run it quickly - but most of those will do a 'rep movsb' faster.

It would also be interesting to try to measure the cost of the 'je' being mispredicted.
I bet a beer or two that at least one cpu can't abort the setup cost of the 'rep movsb' - so you take the full hit.

I do need to rerun my 'rep movsb' performance measurements using data dependencies (not lfence/mfence) to synchronise things.
The 'before' data dependency is easy: count += (clocks & volatile_zero). The 'after' one can be done the same way if using the performance counters. It is probably enough to use the updated value of %si or %di rather than doing a read-back of the last memory write.

I've done that for a different function and can see how the clock count for divide depends on its arguments.

David

#5 lore raw

Re: [PATCH -tip 3/3] x86/asm/32: Modernize _memcpy()

Quoted 28 lines · David Laight
Quoted 15 lines · On Tue, 6 May 2025 19:34:34 +0200 Uros Bizjak <[email protected]> wrote:
Quoted 14 lines · Uros Bizjak

Use inout "+" constraint modifier where appropriate, declare temporary variables as unsigned long and rewrite parts of assembly in plain C. The memcpy() function shrinks by 10 bytes, from:

00e778d0 <memcpy>:

  e778d0:       55                      push   %ebp
  e778d1:       89 e5                   mov    %esp,%ebp
  e778d3:       83 ec 0c                sub    $0xc,%esp
  e778d6:       89 5d f4                mov    %ebx,-0xc(%ebp)
  e778d9:       89 c3                   mov    %eax,%ebx
  e778db:       89 c8                   mov    %ecx,%eax
  e778dd:       89 75 f8                mov    %esi,-0x8(%ebp)
  e778e0:       c1 e9 02                shr    $0x2,%ecx
Quoted 8 lines
Quoted 8 lines
  e778e3:       89 d6                   mov    %edx,%esi
  e778e5:       89 7d fc                mov    %edi,-0x4(%ebp)
  e778e8:       89 df                   mov    %ebx,%edi
  e778ea:       f3 a5                   rep movsl %ds:(%esi),%es:(%edi)
  e778ec:       89 c1                   mov    %eax,%ecx
  e778ee:       83 e1 03                and    $0x3,%ecx
  e778f1:       74 02                   je     e778f5 <memcpy+0x25>
  e778f3:       f3 a4                   rep movsb %ds:(%esi),%es:(%edi)
Quoted 8 lines

Hmmm....
IIRC you really don't want to be doing a [1..3] byte 'rep movsb' there. Some cpu will run it quickly - but most of those will do a 'rep movsb' faster.

It would also be interesting to try to measure the cost of the 'je' being mispredicted.
I bet a beer or two that at least one cpu can't abort the setup cost of the 'rep movsb' - so you take the full hit.

The intention of the patch was to keep the existing functionality, and modernize/rewrite the assembly to use inout constraint modifiers. In passing, some parts of the assembly were converted to plain C. At least to me, the source code is much easier to read this way, and also enables some compiler optimizations that result in better assembly. Perhaps functional improvements can be implemented as follow-up patches.

Uros.

Quoted 11 lines

I do need to rerun my 'rep movsb' performance measurements using data dependencies (not lfence/mfence) to synchronise things.
The 'before' data dependency is easy: count += (clocks & volatile_zero). The 'after' one can be done the same way if using the performance counters. It is probably enough to use the updated value of %si or %di rather than doing a read-back of the last memory write.

I've done that for a different function and can see how the clock count for divide depends on its arguments.

David

#6 lore raw
In reply to Uros Bizjak
Quoted 58 lines · Uros Bizjak

Use inout "+" constraint modifier where appropriate and declare temporary variable using __auto_type, similar to what x86_64 does.

No functional changes intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

---
arch/x86/include/asm/string_32.h | 24 ++++++++++++------------

1 file changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/string_32.h +9 -9
index e9cce169bb4c..9152d2c0f60e 100644
@@ -164,12 +164,12 @@ extern void *memchr(const void *cs, int c, size_t count);
 
 static inline void *__memset_generic(void *s, char c, size_t count)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosb"
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (c), "1" (s), "0" (count)
+		     : "+D" (s), "+c" (count)
+		     : "a" (c)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 /* we might want to write optimized versions of these later */
@@ -197,23 +197,23 @@ extern void *memset(void *, int, size_t);
 #define __HAVE_ARCH_MEMSET16
 static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosw"
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (v), "1" (s), "0" (n)
+		     : "+D" (s), "+c" (n)
+		     : "a" (v)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 #define __HAVE_ARCH_MEMSET32
 static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
 {
-	int d0, d1;
+	const __auto_type s0 = s;
 	asm volatile("rep stosl"

arch/x86/include/asm/string_32.h extern void *memset(void *, int, size_t); · line 219

Quoted 10 lines
arch/x86/include/asm/string_32.h +3 -3
-		     : "=&c" (d0), "=&D" (d1)
-		     : "a" (v), "1" (s), "0" (n)
+		     : "+D" (s), "+c" (n)
+		     : "a" (v)
 		     : "memory");
-	return s;
+	return s0;
 }
 
 /*

So __auto_type is spelled "auto" in newer C versions, but "auto" was a (completely useless!) keyword going all the way back to K&R C. Can anyone think of a reason why we don't do:

#define auto __auto_type

... and just start using the modern keyword right away?

-hpa

#7 lore raw
Quoted 6 lines · H. Peter Anvin

Can anyone think of a reason why we don't do:

#define auto __auto_type

... and just start using the modern keyword right away?

Yeah, make it so. The original 'auto' was useless as you say, and afaik we've never used it in the kernel.

And the one advantage of it being that historically useless keyword is that we can't have variables called 'auto' anywhere either.

So it should be a safe thing to do.

Linus

#8 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 6 lines · H. Peter Anvin

Can anyone think of a reason why we don't do:

#define auto __auto_type

... and just start using the modern keyword right away?

Yeah, make it so. The original 'auto' was useless as you say, and afaik we've never used it in the kernel.

Quoted 6 lines

And the one advantage of it being that historically useless keyword is that we can't have variables called 'auto' anywhere either.

So it should be a safe thing to do.

Linus

Ok, I'll cook up a patchset. Should be trivial.