[PATCH] x86/lib/memmove: Decouple ERMS from FSRM

From: Borislav Petkov
Date: Mon Feb 27 2023 - 05:50:06 EST


On Wed, Feb 08, 2023 at 06:10:50PM +0100, Peter Zijlstra wrote:
> Hi,
>
> This version is fully tested and includes a few NOP twiddles inspired by the
> objtool patches I'll post separately.
>
> Also available at:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=x86/core

Ontop of the first two:

From: "Borislav Petkov (AMD)" <bp@xxxxxxxxx>
Date: Sun, 26 Feb 2023 21:04:26 +0100
Subject: [PATCH] x86/lib/memmove: Decouple ERMS from FSRM

Up until now it was perceived that FSRM is an improvement to ERMS and
thus it was made dependent on latter.

However, there are AMD BIOSes out there which allow for disabling of
either features and this preventing kernels from booting due to the CMP
disappearing and thus breaking the logic in the memmove() function.

Similar observation happens on some VM migration scenarios.

Patch the proper sequences depending on which feature is enabled.

Signed-off-by: Borislav Petkov (AMD) <bp@xxxxxxxxx>
Reported-by: Daniel Verkamp <dverkamp@xxxxxxxxxxxx>
Reported-by: Jiri Slaby <jirislaby@xxxxxxxxxx>
---
arch/x86/lib/memmove_64.S | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/arch/x86/lib/memmove_64.S b/arch/x86/lib/memmove_64.S
index 02661861e5dd..0559b206fb11 100644
--- a/arch/x86/lib/memmove_64.S
+++ b/arch/x86/lib/memmove_64.S
@@ -38,10 +38,12 @@ SYM_FUNC_START(__memmove)
cmp %rdi, %r8
jg 2f

- /* FSRM implies ERMS => no length checks, do the copy directly */
+#define CHECK_LEN cmp $0x20, %rdx; jb 1f
+#define MEMMOVE_BYTES movq %rdx, %rcx; rep movsb; RET
.Lmemmove_begin_forward:
- ALTERNATIVE "cmp $0x20, %rdx; jb 1f", "", X86_FEATURE_FSRM
- ALTERNATIVE "", "jmp .Lmemmove_erms", X86_FEATURE_ERMS
+ ALTERNATIVE_2 __stringify(CHECK_LEN), \
+ __stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \
+ __stringify(MEMMOVE_BYTES), X86_FEATURE_FSRM

/*
* movsq instruction have many startup latency
@@ -207,11 +209,6 @@ SYM_FUNC_START(__memmove)
movb %r11b, (%rdi)
13:
RET
-
-.Lmemmove_erms:
- movq %rdx, %rcx
- rep movsb
- RET
SYM_FUNC_END(__memmove)
EXPORT_SYMBOL(__memmove)

--
2.35.1


--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette