[PATCH] qemu: Optimize CPU check='partial' for usable CPUs
Ideally check='partial' would check exactly the features QEMU would want to enable when asked for a specific CPU model (and features). But there is no way we could ask QEMU how a specific CPU would look like. So we use our definition from CPU map, which may slightly differ as QEMU adds or removes features from CPU models, and thus we may end up checking features which QEMU would not enable while missing some required ones. We can do better in specific cases, though. If a CPU definition uses only a model and disabled features (or none at all), we already know whether QEMU can enable all features required by the CPU model as that's what we use to set usable='yes' attribute in the list of available CPU models in domain capbilities XML. So when a usable CPU model is requested without asking for additional features (disabling features is fine) we can avoid our possible inaccurate check using our CPU map. For backward compatibility we only consider usable models. If a specified model is not usable, we still check it the old way and even let QEMU start it (and disable some features) in case our definition lacks some features compared to QEMU. Signed-off-by: Jiri Denemark --- src/qemu/qemu_capabilities.c | 44 src/qemu/qemu_capabilities.h | 3 +++ src/qemu/qemu_process.c | 1 + 3 files changed, 48 insertions(+) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index e383d85920..cb9846dfcf 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -2433,6 +2433,50 @@ virQEMUCapsIsCPUDeprecated(virQEMUCaps *qemuCaps, } +/** + * virQEMUCapsIsCPUUsable: + * @qemuCaps: QEMU capabilities + * @type: virtualization type (kvm vs tcg) + * @cpu: CPU definition to check, including explicitly configured features + * + * Checks whether @cpu is considered usable by QEMU, i.e., all features + * required by the CPU model are supported and can be enabled. If so, we can + * avoid checking the CPU according to its definition in the CPU map when + * starting a domain with check='partial'. Our checks could be inaccurate + * anyway because QEMU might have changed the definition of the CPU model + * since we added it into the CPU map. + * + * Returns true iff @cpu is usable. + */ +bool +virQEMUCapsIsCPUUsable(virQEMUCaps *qemuCaps, + virDomainVirtType type, + virCPUDef *cpu) +{ +qemuMonitorCPUDefs *defs; +size_t i; + +if (!cpu->model || +!(defs = virQEMUCapsGetAccel(qemuCaps, type)->cpuModels)) +return false; + +/* CPU model usability is valid only when CPU def does not contain any + * features or all features are disabled. + */ +for (i = 0; i < cpu->nfeatures; i++) { +if (cpu->features[i].policy != VIR_CPU_FEATURE_DISABLE) +return false; +} + +for (i = 0; i < defs->ncpus; i++) { +if (STREQ(defs->cpus[i].name, cpu->model)) +return defs->cpus[i].usable == VIR_DOMCAPS_CPU_USABLE_YES; +} + +return false; +} + + bool virQEMUCapsIsMachineDeprecated(virQEMUCaps *qemuCaps, virDomainVirtType type, diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 486a4a6f63..ef1ad2c01c 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -770,6 +770,9 @@ const char *virQEMUCapsGetMachineDefaultCPU(virQEMUCaps *qemuCaps, bool virQEMUCapsIsCPUDeprecated(virQEMUCaps *qemuCaps, virDomainVirtType type, const char *model); +bool virQEMUCapsIsCPUUsable(virQEMUCaps *qemuCaps, +virDomainVirtType type, +virCPUDef *cpu); bool virQEMUCapsIsMachineDeprecated(virQEMUCaps *qemuCaps, virDomainVirtType type, const char *machine); diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 6e51d6586b..8e35d0a73f 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6288,6 +6288,7 @@ qemuProcessUpdateGuestCPU(virDomainDef *def, g_autoptr(virDomainCapsCPUModels) cpuModels = NULL; if (def->cpu->check == VIR_CPU_CHECK_PARTIAL && +!virQEMUCapsIsCPUUsable(qemuCaps, def->virtType, def->cpu) && virCPUCompare(hostarch, virQEMUCapsGetHostModel(qemuCaps, def->virtType, VIR_QEMU_CAPS_HOST_CPU_FULL), -- 2.44.0 ___ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-le...@lists.libvirt.org
Re: [PATCH] qemu: Optimize CPU check='partial' for usable CPUs
On Mon, Mar 04, 2024 at 18:34:29 +0100, Jiri Denemark wrote: > Ideally check='partial' would check exactly the features QEMU would want > to enable when asked for a specific CPU model (and features). But there > is no way we could ask QEMU how a specific CPU would look like. So we > use our definition from CPU map, which may slightly differ as QEMU adds > or removes features from CPU models, and thus we may end up checking > features which QEMU would not enable while missing some required ones. > > We can do better in specific cases, though. If a CPU definition uses > only a model and disabled features (or none at all), we already know > whether QEMU can enable all features required by the CPU model as that's > what we use to set usable='yes' attribute in the list of available CPU > models in domain capbilities XML. So when a usable CPU model is > requested without asking for additional features (disabling features is > fine) we can avoid our possible inaccurate check using our CPU map. > > For backward compatibility we only consider usable models. If a > specified model is not usable, we still check it the old way and even > let QEMU start it (and disable some features) in case our definition > lacks some features compared to QEMU. > > Signed-off-by: Jiri Denemark > --- > src/qemu/qemu_capabilities.c | 44 > src/qemu/qemu_capabilities.h | 3 +++ > src/qemu/qemu_process.c | 1 + > 3 files changed, 48 insertions(+) > > diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c > index e383d85920..cb9846dfcf 100644 > --- a/src/qemu/qemu_capabilities.c > +++ b/src/qemu/qemu_capabilities.c > @@ -2433,6 +2433,50 @@ virQEMUCapsIsCPUDeprecated(virQEMUCaps *qemuCaps, > } > > > +/** > + * virQEMUCapsIsCPUUsable: > + * @qemuCaps: QEMU capabilities > + * @type: virtualization type (kvm vs tcg) > + * @cpu: CPU definition to check, including explicitly configured features > + * > + * Checks whether @cpu is considered usable by QEMU, i.e., all features > + * required by the CPU model are supported and can be enabled. If so, we can > + * avoid checking the CPU according to its definition in the CPU map when > + * starting a domain with check='partial'. Our checks could be inaccurate > + * anyway because QEMU might have changed the definition of the CPU model > + * since we added it into the CPU map. I'd add: The CPU is considered usabe based on the qemu capabilities if qemu reports it as such and libvirt would only request removal of features. This is implied by the commit message and the comment inside the code but not explicitly stated in this comment. > + * > + * Returns true iff @cpu is usable. > + */ > +bool > +virQEMUCapsIsCPUUsable(virQEMUCaps *qemuCaps, > + virDomainVirtType type, > + virCPUDef *cpu) > +{ > +qemuMonitorCPUDefs *defs; > +size_t i; > + > +if (!cpu->model || > +!(defs = virQEMUCapsGetAccel(qemuCaps, type)->cpuModels)) > +return false; > + > +/* CPU model usability is valid only when CPU def does not contain any > + * features or all features are disabled. > + */ > +for (i = 0; i < cpu->nfeatures; i++) { > +if (cpu->features[i].policy != VIR_CPU_FEATURE_DISABLE) > +return false; > +} > + > +for (i = 0; i < defs->ncpus; i++) { > +if (STREQ(defs->cpus[i].name, cpu->model)) > +return defs->cpus[i].usable == VIR_DOMCAPS_CPU_USABLE_YES; > +} > + > +return false; > +} > + > + > bool > virQEMUCapsIsMachineDeprecated(virQEMUCaps *qemuCaps, > virDomainVirtType type, > diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h > index 486a4a6f63..ef1ad2c01c 100644 > --- a/src/qemu/qemu_capabilities.h > +++ b/src/qemu/qemu_capabilities.h > @@ -770,6 +770,9 @@ const char *virQEMUCapsGetMachineDefaultCPU(virQEMUCaps > *qemuCaps, > bool virQEMUCapsIsCPUDeprecated(virQEMUCaps *qemuCaps, > virDomainVirtType type, > const char *model); > +bool virQEMUCapsIsCPUUsable(virQEMUCaps *qemuCaps, > +virDomainVirtType type, > +virCPUDef *cpu); This name is a bit hairy ... but consistent. Reviewed-by: Peter Krempa ___ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-le...@lists.libvirt.org