|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH 4/5] x86/amd: Make cpu_has_amd_erratum() non-variadic
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@xxxxxxx>
> > ---
> > 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
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |