From 652e1798991dcb503abc9a2588b0f95c47b8e3df Mon Sep 17 00:00:00 2001 Message-Id: <652e1798991dcb503abc9a2588b0f95c47b8e3df@dist-git> From: Jiri Denemark Date: Tue, 26 Apr 2022 15:06:30 +0200 Subject: [PATCH] cpu_x86: Penalize disabled features when computing CPU model For finding the best matching CPU model for a given set of features while we don't know the CPU signature (i.e., when computing a baseline CPU model) we've been using a "shortest list of features" heuristics. This works well if new CPU models are supersets of older models, but that's not always the case. As a result it may actually select a new CPU model as a baseline while removing some features from it to make it compatible with older models. This is in general worse than using an old CPU model with a bunch of added features as a guest OS or apps may crash when using features that were disabled. On the other hand we don't want to end up with a very old model which would guarantee no disabled features as it could stop a guest OS or apps from using some features provided by the CPU because they would not expect them on such an old CPU. This patch changes the heuristics to something in between. Enabled and disabled features are counted separately so that a CPU model requiring some features to be disabled looks worse than a model with fewer disabled features even if its complete list of features is longer. The penalty given for each additional disabled feature gets bigger to make longer list of disabled features look even worse. https://bugzilla.redhat.com/show_bug.cgi?id=1851227 Signed-off-by: Jiri Denemark Reviewed-by: Michal Privoznik (cherry picked from commit 48341b025acdd04a66696a709c7b09b3bfd42acf) Conflicts: tests/domaincapsdata/qemu_2.9.0-tcg.x86_64.xml tests/domaincapsdata/qemu_2.11.0-tcg.x86_64.xml tests/domaincapsdata/qemu_2.12.0-tcg.x86_64.xml tests/domaincapsdata/qemu_3.0.0-tcg.x86_64.xml - not supported upstream anymore https://bugzilla.redhat.com/show_bug.cgi?id=2084030 Signed-off-by: Jiri Denemark --- src/cpu/cpu_x86.c | 44 ++++++++++++++++--- .../x86_64-cpuid-Atom-D510-guest.xml | 5 ++- .../x86_64-cpuid-Atom-N450-guest.xml | 5 ++- .../x86_64-cpuid-Phenom-B95-json.xml | 21 +++++---- ...id-baseline-Broadwell-IBRS+Cascadelake.xml | 11 +++-- ..._64-cpuid-baseline-Cascadelake+Icelake.xml | 13 +++--- ...puid-baseline-Cascadelake+Skylake-IBRS.xml | 5 ++- ...6_64-cpuid-baseline-Cooperlake+Icelake.xml | 13 +++--- .../x86_64-host+guest,models-result.xml | 10 +++-- .../domaincapsdata/qemu_2.11.0-tcg.x86_64.xml | 34 ++++++++------ .../domaincapsdata/qemu_2.12.0-tcg.x86_64.xml | 34 ++++++++------ .../domaincapsdata/qemu_2.9.0-tcg.x86_64.xml | 16 ++++--- .../domaincapsdata/qemu_3.0.0-tcg.x86_64.xml | 35 +++++++++------ .../domaincapsdata/qemu_3.1.0-tcg.x86_64.xml | 35 +++++++++------ .../domaincapsdata/qemu_4.0.0-tcg.x86_64.xml | 36 ++++++++------- .../domaincapsdata/qemu_4.1.0-tcg.x86_64.xml | 37 +++++++++------- .../domaincapsdata/qemu_4.2.0-tcg.x86_64.xml | 37 +++++++++------- .../domaincapsdata/qemu_5.0.0-tcg.x86_64.xml | 36 +++++++++------ .../domaincapsdata/qemu_5.1.0-tcg.x86_64.xml | 36 +++++++++------ .../domaincapsdata/qemu_5.2.0-tcg.x86_64.xml | 36 +++++++++------ .../domaincapsdata/qemu_6.0.0-tcg.x86_64.xml | 36 +++++++++------ .../domaincapsdata/qemu_6.1.0-tcg.x86_64.xml | 36 +++++++++------ .../domaincapsdata/qemu_6.2.0-tcg.x86_64.xml | 36 +++++++++------ tests/qemuxml2argvdata/cpu-fallback.args | 2 +- .../cpu-host-model-cmt.x86_64-4.0.0.args | 2 +- .../cpu-host-model-fallback.args | 2 +- 26 files changed, 382 insertions(+), 231 deletions(-) diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 81c2441b8b..ebcd96edb1 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1970,23 +1970,57 @@ virCPUx86Compare(virCPUDef *host, } +/* Base penalty for disabled features. */ +#define BASE_PENALTY 2 + static int virCPUx86CompareCandidateFeatureList(virCPUDef *cpuCurrent, virCPUDef *cpuCandidate) { size_t current = cpuCurrent->nfeatures; + size_t enabledCurrent = current; + size_t disabledCurrent = 0; size_t candidate = cpuCandidate->nfeatures; + size_t enabled = candidate; + size_t disabled = 0; + + if (cpuCandidate->type != VIR_CPU_TYPE_HOST) { + size_t i; + int penalty = BASE_PENALTY; + + for (i = 0; i < enabledCurrent; i++) { + if (cpuCurrent->features[i].policy == VIR_CPU_FEATURE_DISABLE) { + enabledCurrent--; + disabledCurrent += penalty; + penalty++; + } + } + current = enabledCurrent + disabledCurrent; + + penalty = BASE_PENALTY; + for (i = 0; i < enabled; i++) { + if (cpuCandidate->features[i].policy == VIR_CPU_FEATURE_DISABLE) { + enabled--; + disabled += penalty; + penalty++; + } + } + candidate = enabled + disabled; + } - if (candidate < current) { - VIR_DEBUG("%s is better than %s: %zu < %zu", + if (candidate < current || + (candidate == current && disabled < disabledCurrent)) { + VIR_DEBUG("%s is better than %s: %zu (%zu, %zu) < %zu (%zu, %zu)", cpuCandidate->model, cpuCurrent->model, - candidate, current); + candidate, enabled, disabled, + current, enabledCurrent, disabledCurrent); return 1; } - VIR_DEBUG("%s is not better than %s: %zu >= %zu", + VIR_DEBUG("%s is not better than %s: %zu (%zu, %zu) >= %zu (%zu, %zu)", cpuCandidate->model, cpuCurrent->model, - candidate, current); + candidate, enabled, disabled, + current, enabledCurrent, disabledCurrent); return 0; } diff --git a/tests/cputestdata/x86_64-cpuid-Atom-D510-guest.xml b/tests/cputestdata/x86_64-cpuid-Atom-D510-guest.xml index caa0bfd53b..d2f7a79074 100644 --- a/tests/cputestdata/x86_64-cpuid-Atom-D510-guest.xml +++ b/tests/cputestdata/x86_64-cpuid-Atom-D510-guest.xml @@ -1,6 +1,7 @@ - core2duo + n270 Intel + @@ -14,6 +15,6 @@ + - diff --git a/tests/cputestdata/x86_64-cpuid-Atom-N450-guest.xml b/tests/cputestdata/x86_64-cpuid-Atom-N450-guest.xml index e8f5c93881..779faf6cef 100644 --- a/tests/cputestdata/x86_64-cpuid-Atom-N450-guest.xml +++ b/tests/cputestdata/x86_64-cpuid-Atom-N450-guest.xml @@ -1,5 +1,5 @@ - core2duo + n270 Intel @@ -15,6 +15,7 @@ + + - diff --git a/tests/cputestdata/x86_64-cpuid-Phenom-B95-json.xml b/tests/cputestdata/x86_64-cpuid-Phenom-B95-json.xml index b4198f66a5..7f6289c6d7 100644 --- a/tests/cputestdata/x86_64-cpuid-Phenom-B95-json.xml +++ b/tests/cputestdata/x86_64-cpuid-Phenom-B95-json.xml @@ -1,22 +1,25 @@ - Opteron_G3 + athlon AMD - + + + + + - + - - + + + + + - - - - diff --git a/tests/cputestdata/x86_64-cpuid-baseline-Broadwell-IBRS+Cascadelake.xml b/tests/cputestdata/x86_64-cpuid-baseline-Broadwell-IBRS+Cascadelake.xml index 4e3f253e9b..99bce8db87 100644 --- a/tests/cputestdata/x86_64-cpuid-baseline-Broadwell-IBRS+Cascadelake.xml +++ b/tests/cputestdata/x86_64-cpuid-baseline-Broadwell-IBRS+Cascadelake.xml @@ -1,11 +1,14 @@ - Skylake-Client-IBRS + Broadwell-IBRS Intel + + + + + - - - + diff --git a/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Icelake.xml b/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Icelake.xml index e372a3e446..071c799ba2 100644 --- a/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Icelake.xml +++ b/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Icelake.xml @@ -1,14 +1,15 @@ - Cooperlake + Cascadelake-Server Intel - + + + - - - - + + + diff --git a/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Skylake-IBRS.xml b/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Skylake-IBRS.xml index e559e01583..5e42876b39 100644 --- a/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Skylake-IBRS.xml +++ b/tests/cputestdata/x86_64-cpuid-baseline-Cascadelake+Skylake-IBRS.xml @@ -1,12 +1,13 @@ - Cascadelake-Server + Skylake-Server-IBRS Intel + + - diff --git a/tests/cputestdata/x86_64-cpuid-baseline-Cooperlake+Icelake.xml b/tests/cputestdata/x86_64-cpuid-baseline-Cooperlake+Icelake.xml index e372a3e446..071c799ba2 100644 --- a/tests/cputestdata/x86_64-cpuid-baseline-Cooperlake+Icelake.xml +++ b/tests/cputestdata/x86_64-cpuid-baseline-Cooperlake+Icelake.xml @@ -1,14 +1,15 @@ - Cooperlake + Cascadelake-Server Intel - + + + - - - - + + + diff --git a/tests/cputestdata/x86_64-host+guest,models-result.xml b/tests/cputestdata/x86_64-host+guest,models-result.xml index 0dd6955898..42664a48b4 100644 --- a/tests/cputestdata/x86_64-host+guest,models-result.xml +++ b/tests/cputestdata/x86_64-host+guest,models-result.xml @@ -1,17 +1,19 @@ - Nehalem + qemu64 + + - + - - + + diff --git a/tests/domaincapsdata/qemu_2.11.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_2.11.0-tcg.x86_64.xml index cccc6830f9..4a92b5bead 100644 --- a/tests/domaincapsdata/qemu_2.11.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_2.11.0-tcg.x86_64.xml @@ -36,34 +36,42 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + - - - - - - - - - - + - - qemu64 diff --git a/tests/domaincapsdata/qemu_2.12.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_2.12.0-tcg.x86_64.xml index 575506d852..7799a6a7a6 100644 --- a/tests/domaincapsdata/qemu_2.12.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_2.12.0-tcg.x86_64.xml @@ -36,34 +36,42 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + - - - - - - - - - - + - - qemu64 diff --git a/tests/domaincapsdata/qemu_2.9.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_2.9.0-tcg.x86_64.xml index b47c426f1b..fe12641fbe 100644 --- a/tests/domaincapsdata/qemu_2.9.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_2.9.0-tcg.x86_64.xml @@ -36,12 +36,19 @@ - Opteron_G4 + Opteron_G3 AMD + + + + + + + @@ -61,14 +68,13 @@ + - + + - - - qemu64 diff --git a/tests/domaincapsdata/qemu_3.0.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_3.0.0-tcg.x86_64.xml index 301101095c..daccacba0e 100644 --- a/tests/domaincapsdata/qemu_3.0.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_3.0.0-tcg.x86_64.xml @@ -36,35 +36,42 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_3.1.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_3.1.0-tcg.x86_64.xml index 1a5bc25b99..2a65cb0ad9 100644 --- a/tests/domaincapsdata/qemu_3.1.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_3.1.0-tcg.x86_64.xml @@ -36,35 +36,42 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_4.0.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_4.0.0-tcg.x86_64.xml index e744ac27ac..a4dc7bafc9 100644 --- a/tests/domaincapsdata/qemu_4.0.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.0.0-tcg.x86_64.xml @@ -36,36 +36,42 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_4.1.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_4.1.0-tcg.x86_64.xml index 80b1ce8ef9..b20c02cb68 100644 --- a/tests/domaincapsdata/qemu_4.1.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.1.0-tcg.x86_64.xml @@ -36,36 +36,43 @@ - EPYC-IBPB + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml index 91b3ed6f80..d0ee3f7b7a 100644 --- a/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_4.2.0-tcg.x86_64.xml @@ -36,36 +36,43 @@ - EPYC-IBPB + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_5.0.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_5.0.0-tcg.x86_64.xml index b8737613e9..d277c96426 100644 --- a/tests/domaincapsdata/qemu_5.0.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_5.0.0-tcg.x86_64.xml @@ -36,35 +36,43 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_5.1.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_5.1.0-tcg.x86_64.xml index 034036ca96..4f1ffbb2ba 100644 --- a/tests/domaincapsdata/qemu_5.1.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_5.1.0-tcg.x86_64.xml @@ -36,35 +36,43 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_5.2.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_5.2.0-tcg.x86_64.xml index d3a4b01234..6cff0f815e 100644 --- a/tests/domaincapsdata/qemu_5.2.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_5.2.0-tcg.x86_64.xml @@ -36,35 +36,43 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_6.0.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_6.0.0-tcg.x86_64.xml index 6b85c9c45a..65f4459bcb 100644 --- a/tests/domaincapsdata/qemu_6.0.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_6.0.0-tcg.x86_64.xml @@ -36,35 +36,43 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_6.1.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_6.1.0-tcg.x86_64.xml index 2c761fc1af..40bc875e3c 100644 --- a/tests/domaincapsdata/qemu_6.1.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_6.1.0-tcg.x86_64.xml @@ -36,35 +36,43 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/domaincapsdata/qemu_6.2.0-tcg.x86_64.xml b/tests/domaincapsdata/qemu_6.2.0-tcg.x86_64.xml index 8db840faac..a439dda190 100644 --- a/tests/domaincapsdata/qemu_6.2.0-tcg.x86_64.xml +++ b/tests/domaincapsdata/qemu_6.2.0-tcg.x86_64.xml @@ -36,36 +36,44 @@ - EPYC + Opteron_G3 AMD + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - qemu64 diff --git a/tests/qemuxml2argvdata/cpu-fallback.args b/tests/qemuxml2argvdata/cpu-fallback.args index 1b1769d420..53667dfa3a 100644 --- a/tests/qemuxml2argvdata/cpu-fallback.args +++ b/tests/qemuxml2argvdata/cpu-fallback.args @@ -13,7 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc,usb=off,dump-guest-core=off \ -accel kvm \ --cpu Penryn,sse4.1=off,sse4.2=off,popcnt=off,aes=off \ +-cpu Conroe,cx16=on,sse4.1=off,sse4.2=off,popcnt=off,aes=off \ -m 214 \ -realtime mlock=off \ -smp 6,sockets=6,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/cpu-host-model-cmt.x86_64-4.0.0.args b/tests/qemuxml2argvdata/cpu-host-model-cmt.x86_64-4.0.0.args index 3ff226a289..0de09e1d88 100644 --- a/tests/qemuxml2argvdata/cpu-host-model-cmt.x86_64-4.0.0.args +++ b/tests/qemuxml2argvdata/cpu-host-model-cmt.x86_64-4.0.0.args @@ -13,7 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-i440fx-4.0,usb=off,dump-guest-core=off \ -accel tcg \ --cpu EPYC,acpi=on,ss=on,monitor=on,hypervisor=on,erms=on,mpx=on,pcommit=on,clwb=on,pku=on,la57=on,3dnowext=on,3dnow=on,npt=on,vme=off,fma=off,avx=off,f16c=off,rdrand=off,avx2=off,rdseed=off,sha-ni=off,xsavec=off,fxsr-opt=off,misalignsse=off,3dnowprefetch=off,osvw=off,topoext=off,nrip-save=off \ +-cpu Opteron_G3,acpi=on,ss=on,pclmulqdq=on,monitor=on,ssse3=on,sse4.1=on,sse4.2=on,movbe=on,aes=on,xsave=on,hypervisor=on,arat=on,fsgsbase=on,bmi1=on,smep=on,bmi2=on,erms=on,mpx=on,adx=on,smap=on,pcommit=on,clflushopt=on,clwb=on,pku=on,la57=on,xsaveopt=on,xgetbv1=on,mmxext=on,pdpe1gb=on,3dnowext=on,3dnow=on,cr8legacy=on,npt=on,misalignsse=off \ -m 214 \ -overcommit mem-lock=off \ -smp 6,sockets=6,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/cpu-host-model-fallback.args b/tests/qemuxml2argvdata/cpu-host-model-fallback.args index e90b781da4..763e630120 100644 --- a/tests/qemuxml2argvdata/cpu-host-model-fallback.args +++ b/tests/qemuxml2argvdata/cpu-host-model-fallback.args @@ -13,7 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc,usb=off,dump-guest-core=off \ -accel tcg \ --cpu Penryn,vme=on,ds=on,acpi=on,ss=on,ht=on,tm=on,pbe=on,monitor=on,ds-cpl=on,vmx=on,est=on,tm2=on,xtpr=on,sse4.1=off,cx16=on,lahf-lm=on \ +-cpu Conroe,vme=on,ds=on,acpi=on,ss=on,ht=on,tm=on,pbe=on,monitor=on,ds-cpl=on,vmx=on,est=on,tm2=on,cx16=on,xtpr=on,lahf-lm=on \ -m 214 \ -realtime mlock=off \ -smp 6,sockets=6,cores=1,threads=1 \ -- 2.35.1