[PATCH v10 02/15] x86/asm: Introduce inline memcpy and memset

Sohil Mehta posted 15 patches 4 months ago
There is a newer version of this series
[PATCH v10 02/15] x86/asm: Introduce inline memcpy and memset
Posted by Sohil Mehta 4 months ago
From: "Peter Zijlstra (Intel)" <peterz@infradead.org>

Provide inline memcpy and memset functions that can be used instead of
the GCC builtins when necessary. The immediate use case is for the text
poking functions to avoid the standard memcpy()/memset() calls within an
RFLAGS.AC=1 context.

Some user copy functions such as copy_user_generic() and __clear_user()
have similar rep_{movs,stos} usages. But, those are highly specialized
and hard to combine/reuse for other things. Define these new helpers for
all other usages that need a completely unoptimized, strictly inline
version of memcpy() or memset().

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
v10:
 - Reintroduce the simpler inline patch (dropped in v8).
---
 arch/x86/include/asm/string.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/arch/x86/include/asm/string.h b/arch/x86/include/asm/string.h
index c3c2c1914d65..9cb5aae7fba9 100644
--- a/arch/x86/include/asm/string.h
+++ b/arch/x86/include/asm/string.h
@@ -1,6 +1,32 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_STRING_H
+#define _ASM_X86_STRING_H
+
 #ifdef CONFIG_X86_32
 # include <asm/string_32.h>
 #else
 # include <asm/string_64.h>
 #endif
+
+static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len)
+{
+	void *ret = to;
+
+	asm volatile("rep movsb"
+		     : "+D" (to), "+S" (from), "+c" (len)
+		     : : "memory");
+	return ret;
+}
+
+static __always_inline void *__inline_memset(void *s, int v, size_t n)
+{
+	void *ret = s;
+
+	asm volatile("rep stosb"
+		     : "+D" (s), "+c" (n)
+		     : "a" ((uint8_t)v)
+		     : "memory");
+	return ret;
+}
+
+#endif /* _ASM_X86_STRING_H */
-- 
2.43.0
Re: [PATCH v10 02/15] x86/asm: Introduce inline memcpy and memset
Posted by Borislav Petkov 3 months, 2 weeks ago
On Mon, Oct 06, 2025 at 11:51:06PM -0700, Sohil Mehta wrote:
> From: "Peter Zijlstra (Intel)" <peterz@infradead.org>
> 
> Provide inline memcpy and memset functions that can be used instead of
> the GCC builtins when necessary. The immediate use case is for the text
> poking functions to avoid the standard memcpy()/memset() calls within an
> RFLAGS.AC=1 context.

... because objtool does not allow function calls with AC=1 because... see
objtool/Documentation/objtool.txt, warning type 9, yadda yadda...

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v10 02/15] x86/asm: Introduce inline memcpy and memset
Posted by Sohil Mehta 3 months, 2 weeks ago
On 10/21/2025 5:47 AM, Borislav Petkov wrote:
> On Mon, Oct 06, 2025 at 11:51:06PM -0700, Sohil Mehta wrote:
>> From: "Peter Zijlstra (Intel)" <peterz@infradead.org>
>>
>> Provide inline memcpy and memset functions that can be used instead of
>> the GCC builtins when necessary. The immediate use case is for the text
>> poking functions to avoid the standard memcpy()/memset() calls within an
>> RFLAGS.AC=1 context.
> 
> ... because objtool does not allow function calls with AC=1 because... see
> objtool/Documentation/objtool.txt, warning type 9, yadda yadda...
> 

Sure, will add some notes here as well as in the next patch where it is
used.
Re: [PATCH v10 02/15] x86/asm: Introduce inline memcpy and memset
Posted by David Laight 3 months, 2 weeks ago
On Tue, 21 Oct 2025 14:47:51 +0200
Borislav Petkov <bp@alien8.de> wrote:

> On Mon, Oct 06, 2025 at 11:51:06PM -0700, Sohil Mehta wrote:
> > From: "Peter Zijlstra (Intel)" <peterz@infradead.org>
> > 
> > Provide inline memcpy and memset functions that can be used instead of
> > the GCC builtins when necessary. The immediate use case is for the text
> > poking functions to avoid the standard memcpy()/memset() calls within an
> > RFLAGS.AC=1 context.  
> 
> ... because objtool does not allow function calls with AC=1 because... see
> objtool/Documentation/objtool.txt, warning type 9, yadda yadda...
> 

But for the purpose of code patching they don't need to be 'rep movsb'.
An inline function with a C byte copy loop is fine - provided you do
something to stop gcc pessimising it.

Obvious options are a volatile pointer (or READ_ONCE()) or a barrier().

	David