All of lore.kernel.org
 help / color / mirror / Atom feed
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>,
	xen-devel@lists.xenproject.org
Subject: Re: [PATCH 07/12] x86: Have x86_emulate/ implement the single-vendor optimisation
Date: Thu, 12 Feb 2026 16:29:16 +0100	[thread overview]
Message-ID: <DGD39B00LVPV.1XCP36GGDSF4U@amd.com> (raw)
In-Reply-To: <100f63bf-cc79-43c3-a0f6-147d2dff080d@suse.com>

On Thu Feb 12, 2026 at 12:26 PM CET, Jan Beulich wrote:
> On 06.02.2026 17:15, Alejandro Vallejo wrote:
>> Open code the vendor check through the policy as a one-off. The emulator
>> embeds amd_like() in macros and is called in MANY places. Using a
>> local variable (cp->x86_vendor) makes it a lot smaller (300-400 bytes
>> smaller). So treat this as the exception it is and let it use the policy
>> rather than boot_cpu_data.
>
> As elsewhere you mainly discuss benefits for the single-vendor case, is the
> above about the opposite situation? Else why would codegen suffer this much
> here?

In single-vendor it doesn't matter. A constant is a constant. It matters in
multivendor. And it matters for 2 reasons.

  1. The x86_emulate() function is HUGE.
  2. The x86_emulate() function has LOTS of amd_like() invocations.

When amd_like() uses the policy it has an advantage over using the global.
Namely, the policy is already cached in a register, so codegen simply has to
pull the vendor from an offset into a register. When we go for a global variable
we need to reach out and pull the variable from its 64bit address (because we
compile with model=large). It normally evens out with the codegen reductions
cpu_vendor() encourages you to have, but in here it just doesn't. There's way
too many accesses to global state and .text suffers.

The fix for this is caching the vendor somewhere else. A "bool amd_like" in a
local variable would shrink code substantially (by having rsp-relative access to
the solution of the question amd_like() asks, and by avoiding the masking). This
optimisation is worth doing with or without this patch in place.

Alas, I didn't test that, because this series was sufficiently complicated
as-is.

>
> Using cp also is preferable for test and fuzzing harnesses, which don't
> even know boot_cpu_data.

They don't now, which is why I made the x86emul_cpu() macro. New subsystems
added to a userlevel testing ground could simply have a boot_cpu_data with the
desired policy as part of their harness' global state.

>
>> @@ -30,8 +31,15 @@ void BUG(void);
>>  #  define X86EMUL_NO_SIMD
>>  # endif
>>  
>> +/* intentionally avoid cpu_vendor(), as it produces much worse codegen */
>
> Nit (style): Capital letter wanted at the start.
>
>> +# define x86emul_cpu(cp) ((X86_ENABLED_VENDORS ==            \
>> +                           ISOLATE_LSB(X86_ENABLED_VENDORS)) \
>> +                               ? X86_ENABLED_VENDORS         \
>> +                               : ((cp)->x86_vendor & X86_ENABLED_VENDORS))
>
> Nit: Indentation. The ? and : want to align with the controlling expression.

sure.

>
> Further, is this a good name, without "vendor" in it?

x86emul_cpu_vendor() is fine with me too.

>
> And then I'm of two minds here as to the use of the macro parameter: On one
> hand we can be pretty certain what is passed in won't have side effects.
> Otoh in a hypothetical odd case (seeing that this lives in a header file,
> not local to an isolated piece of code) where there would be one, the
> argument being evaluated unreliably could cause an unpleasant surprise.
> The more ...
>
>>  #else /* !__XEN__ */
>>  # include "x86-emulate.h"
>> +# define x86emul_cpu(cp) ((cp)->x86_vendor)
>
> ... that the same wouldn't be observable in the fuzzing or test harnesses.

I can turn this into a static inline to avoid such worries.

Cheers,
Alejandro


  reply	other threads:[~2026-02-12 15:29 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06 16:15 [PATCH 00/12] const-ify vendor checks Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 01/12] x86: Reject CPU policies with vendors other than the host's Alejandro Vallejo
2026-02-10  8:19   ` Roger Pau Monné
2026-02-10 10:11     ` Alejandro Vallejo
2026-02-11 15:41   ` Jan Beulich
2026-02-11 17:41     ` Alejandro Vallejo
2026-02-12  7:16       ` Jan Beulich
2026-02-06 16:15 ` [PATCH 02/12] x86: Add more granularity to the vendors in Kconfig Alejandro Vallejo
2026-02-10  8:26   ` Roger Pau Monné
2026-02-10 10:04     ` Alejandro Vallejo
2026-02-11 16:06   ` Jan Beulich
2026-02-11 17:51     ` Alejandro Vallejo
2026-02-12  7:24       ` Jan Beulich
2026-02-06 16:15 ` [PATCH 03/12] x86: Add cpu_vendor() as a wrapper for the host's CPU vendor Alejandro Vallejo
2026-02-10  8:46   ` Roger Pau Monné
2026-02-10 10:35     ` Alejandro Vallejo
2026-02-10 12:06       ` Roger Pau Monné
2026-02-11 16:04   ` Jan Beulich
2026-02-11 17:35     ` Alejandro Vallejo
2026-02-11 17:57       ` Alejandro Vallejo
2026-02-12 10:52   ` Jan Beulich
2026-02-12 14:36     ` Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 04/12] x86: Migrate MSR handler vendor checks to cpu_vendor() Alejandro Vallejo
2026-02-11 16:15   ` Jan Beulich
2026-02-06 16:15 ` [PATCH 05/12] x86: Migrate spec_ctrl " Alejandro Vallejo
2026-02-12 10:49   ` Jan Beulich
2026-02-12 14:55     ` Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 06/12] x86: Migrate switch " Alejandro Vallejo
2026-02-12 11:06   ` Jan Beulich
2026-02-12 15:06     ` Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 07/12] x86: Have x86_emulate/ implement the single-vendor optimisation Alejandro Vallejo
2026-02-12 11:26   ` Jan Beulich
2026-02-12 15:29     ` Alejandro Vallejo [this message]
2026-02-06 16:15 ` [PATCH 07/12] x86: Migrate x86_emulate/ to use cpu_vendor() Alejandro Vallejo
2026-02-12 11:31   ` Jan Beulich
2026-02-12 15:30     ` Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 08/12] x86/acpi: Migrate vendor checks to cpu_vendor() Alejandro Vallejo
2026-02-12 11:52   ` Jan Beulich
2026-02-12 15:34     ` Alejandro Vallejo
2026-02-12 15:52       ` Jan Beulich
2026-02-06 16:15 ` [PATCH 09/12] x86/pv: " Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 10/12] x86/mcheck: Migrate vendor checks to use cpu_vendor() Alejandro Vallejo
2026-02-12 12:02   ` Jan Beulich
2026-02-12 12:21     ` Jan Beulich
2026-02-12 15:46     ` Alejandro Vallejo
2026-02-06 16:15 ` [PATCH 11/12] x86/cpu: " Alejandro Vallejo
2026-02-12 13:17   ` Jan Beulich
2026-02-06 16:15 ` [PATCH 12/12] x86: Migrate every remaining raw vendor check to cpu_vendor() Alejandro Vallejo
2026-02-12 13:29   ` Jan Beulich
2026-02-09  9:21 ` [PATCH 00/12] const-ify vendor checks Jan Beulich
2026-02-09 10:05   ` Alejandro Vallejo
2026-02-09 10:15     ` Jan Beulich
2026-02-09 11:56       ` Alejandro Vallejo
2026-02-09 12:52         ` Jan Beulich
2026-02-09 14:37           ` 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=DGD39B00LVPV.1XCP36GGDSF4U@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=xen-devel@lists.xenproject.org \
    /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.