[1/2] x86/alternative: Add per-vendor patching

Message ID 20231027153418.GLZTvYejCkXb03rArO@fat_crate.local
State New
Headers
Series [1/2] x86/alternative: Add per-vendor patching |

Commit Message

Borislav Petkov Oct. 27, 2023, 3:34 p.m. UTC
  From: "Borislav Petkov (AMD)" <bp@alien8.de>
Date: Fri, 27 Oct 2023 13:34:12 +0200

Add the capability to apply alternatives not based on a CPU feature but
on the current vendor the machine is running on.

Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
 arch/x86/include/asm/alternative.h |  7 ++++++-
 arch/x86/kernel/alternative.c      | 14 +++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)
  

Comments

Peter Zijlstra Oct. 27, 2023, 6:11 p.m. UTC | #1
On Fri, Oct 27, 2023 at 05:34:18PM +0200, Borislav Petkov wrote:
> From: "Borislav Petkov (AMD)" <bp@alien8.de>
> Date: Fri, 27 Oct 2023 13:34:12 +0200
> 
> Add the capability to apply alternatives not based on a CPU feature but
> on the current vendor the machine is running on.
> 
> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
> ---
>  arch/x86/include/asm/alternative.h |  7 ++++++-
>  arch/x86/kernel/alternative.c      | 14 +++++++++-----
>  2 files changed, 15 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
> index 65f79092c9d9..76750f8b5aba 100644
> --- a/arch/x86/include/asm/alternative.h
> +++ b/arch/x86/include/asm/alternative.h
> @@ -8,8 +8,13 @@
>  
>  #define ALT_FLAGS_SHIFT		16
>  
> -#define ALT_FLAG_NOT		(1 << 0)
> +/* Negate the tested feature flag */
> +#define ALT_FLAG_NOT		BIT(0)
> +/* Check X86_VENDOR_* instead of a feature flag */
> +#define	ALT_FLAG_VENDOR		BIT(1)
> +
>  #define ALT_NOT(feature)	((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
> +#define ALT_VENDOR(x86_vendor)	((ALT_FLAG_VENDOR << ALT_FLAGS_SHIFT) | (x86_vendor))
>  
>  #ifndef __ASSEMBLY__
>  
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index 73be3931e4f0..8b67b5c6090e 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -433,19 +433,23 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
>  
>  		/*
>  		 * Patch if either:
> +		 * - running on the respective x86 vendor
>  		 * - feature is present
>  		 * - feature not present but ALT_FLAG_NOT is set to mean,
>  		 *   patch if feature is *NOT* present.
>  		 */
> -		if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
> +		if (a->flags & ALT_FLAG_VENDOR) {
> +			if (boot_cpu_data.x86_vendor != a->cpuid) {
> +				optimize_nops(instr, a->instrlen);
> +				continue;
> +			}
> +		} else if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
>  			optimize_nops(instr, a->instrlen);
>  			continue;
>  		}

But what if I want to do ALT_FLAG_VENDOR | ALT_FLAG_NOT?

/me runs
  
Borislav Petkov Oct. 27, 2023, 6:25 p.m. UTC | #2
On Fri, Oct 27, 2023 at 08:11:47PM +0200, Peter Zijlstra wrote:
> But what if I want to do ALT_FLAG_VENDOR | ALT_FLAG_NOT?

I knew you were gonna say that...

You sit down and you implement it. :-P
  

Patch

diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index 65f79092c9d9..76750f8b5aba 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -8,8 +8,13 @@ 
 
 #define ALT_FLAGS_SHIFT		16
 
-#define ALT_FLAG_NOT		(1 << 0)
+/* Negate the tested feature flag */
+#define ALT_FLAG_NOT		BIT(0)
+/* Check X86_VENDOR_* instead of a feature flag */
+#define	ALT_FLAG_VENDOR		BIT(1)
+
 #define ALT_NOT(feature)	((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
+#define ALT_VENDOR(x86_vendor)	((ALT_FLAG_VENDOR << ALT_FLAGS_SHIFT) | (x86_vendor))
 
 #ifndef __ASSEMBLY__
 
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 73be3931e4f0..8b67b5c6090e 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -433,19 +433,23 @@  void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 
 		/*
 		 * Patch if either:
+		 * - running on the respective x86 vendor
 		 * - feature is present
 		 * - feature not present but ALT_FLAG_NOT is set to mean,
 		 *   patch if feature is *NOT* present.
 		 */
-		if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
+		if (a->flags & ALT_FLAG_VENDOR) {
+			if (boot_cpu_data.x86_vendor != a->cpuid) {
+				optimize_nops(instr, a->instrlen);
+				continue;
+			}
+		} else if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
 			optimize_nops(instr, a->instrlen);
 			continue;
 		}
 
-		DPRINTK(ALT, "feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
-			(a->flags & ALT_FLAG_NOT) ? "!" : "",
-			a->cpuid >> 5,
-			a->cpuid & 0x1f,
+		DPRINTK(ALT, "feat: 0x%x, flags: 0x%x, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
+			a->cpuid, a->flags,
 			instr, instr, a->instrlen,
 			replacement, a->replacementlen);