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

From: Peter Zijlstra
Date: Fri Oct 27 2023 - 14:12:07 EST


On Fri, Oct 27, 2023 at 05:34:18PM +0200, Borislav Petkov wrote:
> From: "Borislav Petkov (AMD)" <bp@xxxxxxxxx>
> 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@xxxxxxxxx>
> ---
> 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