On Fri, Nov 06, 2026 at 15:52:59 +0100, Denis V. Lunev wrote:
> From: Denis V. Lunev <[email protected]>
> 
> Since Linux kernel commit de572491a975 ("KVM: x86: AMD's IBPB is not
> equivalent to Intel's IBPB") KVM offers spec-ctrl on AMD hosts only
> when the CPU enumerates IBPB_RET, which Zen 1-3 do not. A host-model
> guest started on such a CPU under an older kernel has spec-ctrl in its
> live definition. On a destination with the newer kernel QEMU filters
> the feature out and the incoming migration fails:
> 
>   guest CPU doesn't match specification: missing features: spec-ctrl
> 
> Restoring a save image or reverting a snapshot fails the same way, so
> the guest cannot be moved to the new kernel without a restart.
> 
> Dropping the bit in libvirt costs the guest nothing beyond what KVM
> already withholds. KVM grants PRED_CMD for spec-ctrl or ibpb and
> SPEC_CTRL for spec-ctrl or any of ibrs, amd-stibp and amd-ssbd (kernel
> commit 39485ed95d6b), and checks the values written to SPEC_CTRL
> against the host, not the guest CPUID (kernel commit 841c2be09fe4).
> Both predate de572491a975. Since kernel commit e592ec657d84 KVM also
> ignores guest CPUID bits it does not support, so a host without
> IBPB_RET treats the guest as lacking spec-ctrl even if the bit stayed.
> QEMU drops the bit either way; libvirt only decides whether the guest
> survives.
> 
> When a domain is started (fresh start, incoming migration, restore,
> snapshot revert) and the hypervisor dropped a required spec-ctrl while
> providing ibpb and one of ibrs, amd-stibp and amd-ssbd,
> virCPUUpdateLive() disables spec-ctrl in the live definition and logs it
> at VIR_INFO. spec-ctrl is also removed from the features QEMU reports as
> filtered, because check='full' fails on any filtered feature whatever
> the definition says. Any other missing or extra feature still fails with
> check='full'.
> 
> Signed-off-by: Denis V. Lunev <[email protected]>
> ---
>  src/cpu/cpu_x86.c                             | 93 ++++++++++++++++++-
>  tests/cputest.c                               | 15 +++
>  ...PYC-7713-64-Core-no-spec-ctrl-disabled.xml |  4 +
>  ...EPYC-7713-64-Core-no-spec-ctrl-enabled.xml | 11 +++
>  ...d-EPYC-7713-64-Core-no-spec-ctrl-guest.xml | 56 +++++++++++
>  ...id-EPYC-7713-64-Core-no-spec-ctrl-json.xml | 56 +++++++++++
>  ...-64-Core-no-spec-ctrl-no-ibpb-disabled.xml |  4 +
>  ...3-64-Core-no-spec-ctrl-no-ibpb-enabled.xml | 11 +++
>  ...713-64-Core-no-spec-ctrl-no-ibpb-guest.xml | 56 +++++++++++
>  9 files changed, 302 insertions(+), 4 deletions(-)
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-disabled.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-enabled.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-guest.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-json.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-no-ibpb-disabled.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-no-ibpb-enabled.xml
>  create mode 100644 
> tests/cputestdata/x86_64-cpuid-EPYC-7713-64-Core-no-spec-ctrl-no-ibpb-guest.xml
> 
> diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
> index 18c5756ef9..3e89d5ef0a 100644
> --- a/src/cpu/cpu_x86.c
> +++ b/src/cpu/cpu_x86.c
> @@ -3144,6 +3144,88 @@ virCPUx86Update(virCPUDef *guest,
>  }
>  
>  
> +/*
> + * Since kernel commit de572491a975 KVM no longer offers spec-ctrl on AMD
> + * hosts without IBPB_RET. The MSRs it stands for are still available to
> + * the guest through ibpb and any of ibrs, amd-stibp and amd-ssbd.
> + */
> +static int
> +x86SpecCtrlDroppable(const virCPUDef *cpu G_GNUC_UNUSED,
> +                     virCPUx86Map *map,
> +                     const virCPUx86Data *enabled)
> +{
> +    const char *specCtrlMSR[] = { "ibrs", "amd-stibp", "amd-ssbd" };
> +    size_t i;
> +    int rc;
> +
> +    if ((rc = x86FeatureInData("ibpb", enabled, map)) <= 0)
> +        return rc;
> +
> +    for (i = 0; i < G_N_ELEMENTS(specCtrlMSR); i++) {
> +        if ((rc = x86FeatureInData(specCtrlMSR[i], enabled, map)) != 0)
> +            return rc;
> +    }
> +
> +    return 0;
> +}
> +
> +

I would add a comment about the expected return values from droppable.
Something like

    /* The @droppable callback decides what should be done with the
     * feature called @name by returning one of the following values:
     *    -1 on error
     *     0 when @name should stay in the CPU definition
     *     1 if it safe to remove @name from the CPU
     */

> +static const struct {
> +    const char *name;
> +    int (*droppable)(const virCPUDef *cpu,
> +                     virCPUx86Map *map,
> +                     const virCPUx86Data *enabled);
> +} x86DroppableFeatures[] = {
> +    { "spec-ctrl", x86SpecCtrlDroppable },
> +};
...
> diff --git a/tests/cputest.c b/tests/cputest.c
> index e29c81cb8e..35b426155e 100644
> --- a/tests/cputest.c
> +++ b/tests/cputest.c
> @@ -857,6 +857,15 @@ cpuTestUpdateLive(const void *arg)
>          !(disabledData = virCPUDataParse(disabled)))
>          return -1;
>  
> +    if (data->result == -1) {
> +        if (virCPUUpdateLive(data->arch, cpu, enabledData, disabledData) == 
> 0) {
> +            VIR_TEST_VERBOSE("virCPUUpdateLive succeeded unexpectedly");
> +            return -1;
> +        }
> +        virResetLastError();
> +        return 0;
> +    }
> +
>      expectedFile = g_strdup_printf("cpuid-%s-json", data->host);
>      if (!(expected = cpuTestLoadXML(data->arch, expectedFile,
>                                      data->existingTestCases))) {

This is a minimal change, but the code looks weird as virCPUUpdateLive
is in two places. I would drop this hunk and make a separate patch
adding support for negative tests. I sent the patch for review.

No matter what approach we choose, I can make the required changes to
this patch (including the additional comment) if you agree and push it
to eliminate the need for a trivial v3.

Reviewed-by: Jiri Denemark <[email protected]>

Reply via email to