From: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Jason Andryuk" <jason.andryuk@amd.com>,
"Xenia Ragiadakou" <xenia.ragiadakou@amd.com>,
"Stefano Stabellini" <sstabellini@kernel.org>,
xen-devel@lists.xenproject.org
Subject: Re: [RFC PATCH 04/11] x86: Refactor early vendor lookup code to use x86_vendor_is()
Date: Thu, 27 Nov 2025 14:35:19 +0100 [thread overview]
Message-ID: <DEJIM3SR50B4.2EEY2CX5KDM9M@amd.com> (raw)
In-Reply-To: <ad217ce2-0b3c-4746-b32e-3b3bc7c9cc51@suse.com>
On Thu Nov 27, 2025 at 11:51 AM CET, Jan Beulich wrote:
> On 26.11.2025 17:44, Alejandro Vallejo wrote:
>> Move the "unknown" vendor ahead of all others and have it NOT rely
>> on x86_vendor_is(), as that would yield incorrect values for the
>> single-vendor+no-fallback case when running in incorrect hardware
>> (because x86_vendor_is() becomes a folded constant of the single
>> compiled-in vendor).
>>
>> This is one of the two places where x86_vendor_is() cannot be used,
>> along with the compatibility check on loaded guest CPU policies.
>>
>> Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
>> ---
>> xen/arch/x86/cpu/common.c | 31 +++++++++++++++++++++++--------
>> 1 file changed, 23 insertions(+), 8 deletions(-)
>>
>> diff --git a/xen/arch/x86/cpu/common.c b/xen/arch/x86/cpu/common.c
>> index 393c30227f..c0c3606dd2 100644
>> --- a/xen/arch/x86/cpu/common.c
>> +++ b/xen/arch/x86/cpu/common.c
>> @@ -342,23 +342,38 @@ void __init early_cpu_init(bool verbose)
>>
>> c->x86_vendor = x86_cpuid_lookup_vendor(ebx, ecx, edx) &
>> X86_ENABLED_VENDORS;
>> - switch (c->x86_vendor) {
>> - case X86_VENDOR_INTEL: intel_unlock_cpuid_leaves(c);
>> - actual_cpu = intel_cpu_dev; break;
>> - case X86_VENDOR_AMD: actual_cpu = amd_cpu_dev; break;
>> - case X86_VENDOR_CENTAUR: actual_cpu = centaur_cpu_dev; break;
>> - case X86_VENDOR_SHANGHAI: actual_cpu = shanghai_cpu_dev; break;
>> - case X86_VENDOR_HYGON: actual_cpu = hygon_cpu_dev; break;
>> - default:
>> +
>> + /*
>> + * We can't rely on x86_vendor_is() here due to the single-vendor
>> + * optimisation. It makes x86_vendor_is(x, y) rely on the constant `y`
>> + * matching the single vendor Xen was compiled for and ignore the
>> + * runtime variable `x`. In order to preserve sanity we must assert here
>> + * that we never boot such a build in a CPU from another vendor, or
>> + * major chaos would ensue.
>> + */
>> + if (c->x86_vendor == X86_VENDOR_UNKNOWN)
>> + {
>
> Nit: No mix of styles please. Here it wants to be Linux style.
>
>> if (verbose || !IS_ENABLED(CONFIG_UNKNOWN_CPU))
>> printk(XENLOG_ERR
>> "Unrecognised or unsupported CPU vendor '%.12s'\n",
>> c->x86_vendor_id);
>> +
>> if (!IS_ENABLED(CONFIG_UNKNOWN_CPU))
>> panic("Cannot run in unknown/compiled-out CPU vendor.\n");
>>
>> actual_cpu = default_cpu;
>> }
>> + else if (x86_vendor_is(c->x86_vendor, X86_VENDOR_INTEL)) {
>> + intel_unlock_cpuid_leaves(c);
>> + actual_cpu = intel_cpu_dev;
>> + } else if (x86_vendor_is(c->x86_vendor, X86_VENDOR_AMD))
>> + actual_cpu = amd_cpu_dev;
>> + else if (x86_vendor_is(c->x86_vendor, X86_VENDOR_CENTAUR))
>> + actual_cpu = centaur_cpu_dev;
>> + else if (x86_vendor_is(c->x86_vendor, X86_VENDOR_SHANGHAI))
>> + actual_cpu = shanghai_cpu_dev;
>> + else if (x86_vendor_is(c->x86_vendor, X86_VENDOR_HYGON))
>> + actual_cpu = hygon_cpu_dev;
>
> If it needs to be like this, then so be it, but I view it as a downside to
> not be able to use switch() anymore. It's not quite clear to me though what
> extra gains the transformation brings. The masking by X86_ENABLED_VENDORS
> already does most of what you want, and X86_VENDOR_UNKNOWN continues to be
> handled separately anyway.
In this particular switch that's the case, but it's not so elsewhere.
Any switch would resolve to an unavoidable runtime check with compiled-out
branches left out (so long as the variable is ANDed with the enabled vendors
mask, which it currently isn't). However, this still forces the compiler to emit
a runtime check in case the vendor is actually zero. The conversion to if-elseif
ensures we can statically decide a branch at compile time and remove all others
(including the default one).
On a more stylistic note, though obviously this is all subjective, the patch
that migrates switch statements to if-elseif chains shaves 100 LoC and manages
to squash multiple checks onto single ones by making use of the bitmap nature of
the vendor field. The gains there are marginal, so I don't care that much, about
that but it's a measurable benefit in LoC and a (small) benefit in codegen.
Cheers,
Alejandro
next prev parent reply other threads:[~2025-11-27 13:36 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-26 16:44 [RFC PATCH 00/11] x86 vendor check optimisations Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 01/11] x86: Add more granularity to the vendors in Kconfig Alejandro Vallejo
2025-11-27 9:43 ` Jan Beulich
2025-11-27 12:36 ` Alejandro Vallejo
2025-11-27 13:19 ` Jan Beulich
2025-11-27 13:44 ` Alejandro Vallejo
2025-11-27 14:00 ` Jan Beulich
2025-11-27 9:59 ` Jan Beulich
2025-11-27 12:37 ` Alejandro Vallejo
2025-11-27 21:01 ` Andrew Cooper
2025-11-26 16:44 ` [RFC PATCH 02/11] x86: Reject CPU policies with vendors other than the host's Alejandro Vallejo
2025-11-27 20:15 ` Andrew Cooper
2025-11-26 16:44 ` [RFC PATCH 03/11] x86: Add x86_vendor_is() by itself before using it Alejandro Vallejo
2025-11-27 10:46 ` Jan Beulich
2025-11-27 13:15 ` Alejandro Vallejo
2025-11-27 13:37 ` Jan Beulich
2025-11-27 14:06 ` Alejandro Vallejo
2025-11-27 14:14 ` Jan Beulich
2025-11-27 20:36 ` Andrew Cooper
2025-11-28 9:08 ` Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 04/11] x86: Refactor early vendor lookup code to use x86_vendor_is() Alejandro Vallejo
2025-11-27 10:51 ` Jan Beulich
2025-11-27 13:35 ` Alejandro Vallejo [this message]
2025-11-26 16:44 ` [RFC PATCH 05/11] x86: Conditionalise the inclusion of Hygon/Centaur/Shanghai cpu/ files Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 06/11] x86: Migrate switch-based vendor checks to x86_vendor_is() Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 07/11] x86: Migrate MSR handler " Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 08/11] x86: Migrate insn emulator to use x86_vendor_is() Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 09/11] x86: Migrate spec_ctrl vendor checks to x86_vendor_is() Alejandro Vallejo
2025-12-08 16:01 ` Jan Beulich
2025-12-08 16:04 ` Jan Beulich
2025-12-11 10:31 ` Alejandro Vallejo
2025-12-11 15:38 ` Andrew Cooper
2025-12-11 16:58 ` Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 10/11] x86: Migrate everything under cpu/ to use x86_vendor_is() Alejandro Vallejo
2025-11-26 16:44 ` [RFC PATCH 11/11] x86: Migrate every remaining raw vendor check to x86_vendor_is() Alejandro Vallejo
2025-11-27 22:11 ` [RFC PATCH 00/11] x86 vendor check optimisations Andrew Cooper
2025-11-28 9:18 ` Alejandro Vallejo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DEJIM3SR50B4.2EEY2CX5KDM9M@amd.com \
--to=alejandro.garciavallejo@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=jason.andryuk@amd.com \
--cc=jbeulich@suse.com \
--cc=roger.pau@citrix.com \
--cc=sstabellini@kernel.org \
--cc=xen-devel@lists.xenproject.org \
--cc=xenia.ragiadakou@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.