From: "Alejandro Vallejo" <alejandro.garciavallejo@amd.com>
To: "Andrew Cooper" <andrew.cooper3@citrix.com>,
<xen-devel@lists.xenproject.org>
Cc: "Jan Beulich" <jbeulich@suse.com>,
"Roger Pau Monné" <roger@xenproject.org>,
"Teddy Astie" <teddy.astie@vates.tech>
Subject: Re: [PATCH 4/5] x86/amd: Make cpu_has_amd_erratum() non-variadic
Date: Thu, 19 Nov 2026 11:41:30 +0100 [thread overview]
Message-ID: <DMZ4HIDEKZHW.2CAS68YCFHBZ0@amd.com> (raw)
In-Reply-To: <6fd2fff3-d470-430e-a567-3f06b88ac79c@citrix.com>
On Wed Nov 18, 2026 at 8:06 PM CET, Andrew Cooper wrote:
> On 18/11/2026 5:17 pm, Alejandro Vallejo wrote:
> > cpu_has_amd_erratum() takes the OSVW id and the family-model-stepping
> > ranges of an erratum as variadic arguments, which MISRA C Rule 17.1
> > forbids.
> >
> > Have AMD_LEGACY_ERRATUM() and AMD_OSVW_ERRATUM() expand to a
> > zero-terminated array instead, and have cpu_has_amd_erratum() walk it.
> > Callers pass the AMD_ERRATUM_* macros, so they are unaffected.
> >
> > No functional change.
> >
> > Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
> > ---
> > xen/arch/x86/cpu/amd.c | 14 ++++++--------
> > xen/arch/x86/include/asm/amd.h | 11 +++++++----
> > 2 files changed, 13 insertions(+), 12 deletions(-)
> >
> > diff --git a/xen/arch/x86/cpu/amd.c b/xen/arch/x86/cpu/amd.c
> > index fbbba0005c..5e22fff81c 100644
> > --- a/xen/arch/x86/cpu/amd.c
> > +++ b/xen/arch/x86/cpu/amd.c
> > @@ -321,10 +321,10 @@ void amd_set_cpuid_user_dis(bool enable)
> > * Check for the presence of an AMD erratum. Arguments are defined in amd.h
> > * for each known erratum. Return 1 if erratum is found.
> > */
> > -int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)
> > +int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum)
> > {
> > - va_list ap;
> > - u32 range;
> > + int osvw_id = erratum[0];
> > + const int *p;
> > u32 ms;
> >
> > if (cpu->vendor != X86_VENDOR_AMD)
> > @@ -346,19 +346,17 @@ int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)
> > }
> >
> > /* OSVW unavailable or ID unknown, match family-model-stepping range */
> > - va_start(ap, osvw_id);
> > -
> > ms = (cpu->model << 4) | cpu->stepping;
> > - while ((range = va_arg(ap, int))) {
> > + for (p = &erratum[1]; *p; p++) {
> > + u32 range = *p;
> > +
> > if ((cpu->family == AMD_MODEL_RANGE_FAMILY(range)) &&
> > (ms >= AMD_MODEL_RANGE_START(range)) &&
> > (ms <= AMD_MODEL_RANGE_END(range))) {
> > - va_end(ap);
> > return 1;
> > }
> > }
> >
> > - va_end(ap);
> > return 0;
> > }
> >
> > diff --git a/xen/arch/x86/include/asm/amd.h b/xen/arch/x86/include/asm/amd.h
> > index d21df0741a..d897943210 100644
> > --- a/xen/arch/x86/include/asm/amd.h
> > +++ b/xen/arch/x86/include/asm/amd.h
> > @@ -12,7 +12,8 @@
> > * Errata are defined using the AMD_LEGACY_ERRATUM() or AMD_OSVW_ERRATUM()
> > * macros. The latter is intended for newer errata that have an OSVW id
> > * assigned, which it takes as first argument. Both take a variable number
> > - * of family-specific model-stepping ranges created by AMD_MODEL_RANGE().
> > + * of family-specific model-stepping ranges created by AMD_MODEL_RANGE(),
> > + * and yield a zero-terminated array to pass to cpu_has_amd_erratum().
> > *
> > * Example 1:
> > * #define AMD_ERRATUM_319 \
> > @@ -26,8 +27,10 @@
> > *
> > */
> >
> > -#define AMD_LEGACY_ERRATUM(...) -1 /* legacy */, __VA_ARGS__, 0
> > -#define AMD_OSVW_ERRATUM(osvw_id, ...) osvw_id, __VA_ARGS__, 0
> > +#define AMD_LEGACY_ERRATUM(...) \
> > + ((const int[]){ -1 /* legacy */, __VA_ARGS__, 0 })
> > +#define AMD_OSVW_ERRATUM(osvw_id, ...) \
> > + ((const int[]){ osvw_id, __VA_ARGS__, 0 })
> > #define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \
> > (((f) << 24) | ((m_start) << 16) | ((s_start) << 12) | \
> > ((m_end) << 4) | (s_end))
> > @@ -72,7 +75,7 @@
> > #define is_zen6_uarch() boot_cpu_has(X86_FEATURE_FRED)
> >
> > struct cpuinfo_x86;
> > -int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...);
> > +int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum);
> >
> > extern int8_t opt_allow_unsafe;
> >
>
> This whole infrastructure is garbage. I believe Linux dropped it a long
> time back.
I agree. I can only describe it as an overcomplicated mess.
>
> If you see, we only ever encode LEGACY errata with it, meaning the
> osvw_id is always -1, meaning the OSVW MSR accesses is
> cpu_has_amd_erratum() are truly unreachable even in safety builds.
errata 383 (out of context) matches on OSVW before attempting the
fam/model match.
>
> Next, have you seen what a disaster of code generation this actually
> causes at callers? It's an anonymous array on the stack, which means
> it's (re)initialised every time it's called. One of these is even in
> domain_create().
Not to say any of this is GOOD. But it is very definitely not worse.
A variadic function is... pretty much that. They are largely equivalent.
Debug builds are slightly bigger with the anonymous arrays due to
optimisation quirks, but release builds are slightly smaller (See
bloat-o-meter):
add/remove: 0/0 grow/shrink: 4/2 up/down: 61/-113 (-52)
Function old new delta
svm_asid_init 39 60 +21
init_amd 2419 2435 +16
arch_domain_create 1043 1059 +16
_svm_cpu_up 444 452 +8
x86emul_fpu 7369 7321 -48
cpu_has_amd_erratum 260 195 -65
Total: Before=3607126, After=3607074, chg -0.00%
>
> OSVW was a short-sighted and now abandoned idea of AMDs. There are
> something like 4(?) bits defined, but there's no index of what they are
> so you have to search all the revision guides to find them.
>
> Could I at least talk you into dropping the osvw_id part? Xen does
> nothing with OSVW at all, and if we ever feel the need to gain some use,
> it will not be like this.
>
> But, if you were to see about moving the arrays out to being static
> objects too, I think the result would be a whole lot less unpalatable.
I don't mind refactoring this mess a bit more. I may try removing the
arrays altogether and integrating them into the function itself.
Note cpu_has_amd_erratum() does perform the OSVW lookup for errata 383.
Did you _really_ mean to drop that? It's not immediately obvious to me
whether the fam/models listed cover everything OSVW does.
Cheers,
Alejandro
next prev parent reply other threads:[~2026-11-19 10:41 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 17:17 [PATCH 0/5] x86: Add in-scope files under cpu/ to MISRA scans Alejandro Vallejo
2026-11-18 17:17 ` [PATCH 1/5] x86/cpu: Add break to early_cpu_init() switch Alejandro Vallejo
2026-11-18 18:45 ` Andrew Cooper
2026-11-18 17:17 ` [PATCH 2/5] x86/mtrr: Address MISRA C Rules 5.3, 8.3 and 8.4 Alejandro Vallejo
2026-11-18 18:46 ` Andrew Cooper
2026-11-18 17:17 ` [PATCH 3/5] x86/cpu: Use U suffixes on integer constants Alejandro Vallejo
2026-11-18 18:49 ` Andrew Cooper
2026-11-19 7:51 ` Jan Beulich
2026-11-18 17:17 ` [PATCH 4/5] x86/amd: Make cpu_has_amd_erratum() non-variadic Alejandro Vallejo
2026-11-18 19:06 ` Andrew Cooper
2026-11-19 10:41 ` Alejandro Vallejo [this message]
2026-11-18 17:17 ` [PATCH 5/5] eclair/x86: Scan in-scope files inside cpu/ Alejandro Vallejo
2026-11-18 17:32 ` Nicola Vetrini
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=DMZ4HIDEKZHW.2CAS68YCFHBZ0@amd.com \
--to=alejandro.garciavallejo@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=jbeulich@suse.com \
--cc=roger@xenproject.org \
--cc=teddy.astie@vates.tech \
--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.