From nobody Thu May 15 12:35:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1507717115642558.6023759330396; Wed, 11 Oct 2017 03:18:35 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DA47F25BAF; Wed, 11 Oct 2017 10:18:33 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D4F5F6293F; Wed, 11 Oct 2017 10:18:32 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2636218355D8; Wed, 11 Oct 2017 10:18:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v9BABLYr029574 for ; Wed, 11 Oct 2017 06:11:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id CFA836248F; Wed, 11 Oct 2017 10:11:21 +0000 (UTC) Received: from virval.usersys.redhat.com (unknown [10.43.2.105]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D60563BA6 for ; Wed, 11 Oct 2017 10:11:19 +0000 (UTC) Received: by virval.usersys.redhat.com (Postfix, from userid 500) id 58663104219; Wed, 11 Oct 2017 12:11:18 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com DA47F25BAF Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Jiri Denemark To: libvir-list@redhat.com Date: Wed, 11 Oct 2017 12:11:17 +0200 Message-Id: <1ab8d96dc6c46650dc4ecdaabf1edc04d18c8f43.1507710167.git.jdenemar@redhat.com> In-Reply-To: References: In-Reply-To: References: Mail-Followup-To: libvir-list@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/4] qemu: Fix CPU model broken by older libvirt X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 11 Oct 2017 10:18:34 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" When libvirt older than 3.9.0 reconnected to a running domain started by old libvirt it could have messed up the expansion of host-model by adding features QEMU does not support (such as cmt). Thus whenever we reconnect to a running domain, revert to an active snapshot, or restore a saved domain we need to check the guest CPU model and remove the CPU features unknown to QEMU. We can do this because we know the domain was successfully started, which means the CPU did not contain the features when libvirt started the domain. https://bugzilla.redhat.com/show_bug.cgi?id=3D1495171 Signed-off-by: Jiri Denemark Reviewed-by: Pavel Hrdina --- src/qemu/qemu_domain.c | 76 +++++++++++++++++++++++++++++++++++++++++++++= ++++ src/qemu/qemu_domain.h | 4 +++ src/qemu/qemu_driver.c | 14 +++++++++ src/qemu/qemu_process.c | 9 ++++++ 4 files changed, 103 insertions(+) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index f621cf7afc..29b6618a56 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -10021,6 +10021,82 @@ qemuDomainUpdateCPU(virDomainObjPtr vm, return 0; } =20 + +/** + * qemuDomainFixupCPUS: + * @vm: domain object + * @origCPU: original CPU used when the domain was started + * + * Libvirt older than 3.9.0 could have messed up the expansion of host-mod= el + * CPU when reconnecting to a running domain by adding features QEMU does = not + * support (such as cmt). This API fixes both the actual CPU provided by Q= EMU + * (stored in the domain object) and the @origCPU used when starting the + * domain. + * + * This is safe even if the original CPU definition used mode=3D'custom' (= rather + * than host-model) since we know QEMU was able to start the domain and th= us + * the CPU definitions do not contain any features unknown to QEMU. + * + * This function can only be used on an active domain or when restoring a + * domain which was running. + * + * Returns 0 on success, -1 on error. + */ +int +qemuDomainFixupCPUs(virDomainObjPtr vm, + virCPUDefPtr *origCPU) +{ + virCPUDefPtr fixedCPU =3D NULL; + virCPUDefPtr fixedOrig =3D NULL; + virArch arch =3D vm->def->os.arch; + int ret =3D 0; + + if (!ARCH_IS_X86(arch)) + return 0; + + if (!vm->def->cpu || + vm->def->cpu->mode !=3D VIR_CPU_MODE_CUSTOM || + !vm->def->cpu->model) + return 0; + + /* Missing origCPU means QEMU created exactly the same virtual CPU whi= ch + * we asked for or libvirt was too old to mess up the translation from + * host-model. + */ + if (!*origCPU) + return 0; + + if (virCPUDefFindFeature(vm->def->cpu, "cmt") && + (!(fixedCPU =3D virCPUDefCopyWithoutModel(vm->def->cpu)) || + virCPUDefCopyModelFilter(fixedCPU, vm->def->cpu, false, + virQEMUCapsCPUFilterFeatures, &arch) < 0= )) + goto cleanup; + + if (virCPUDefFindFeature(*origCPU, "cmt") && + (!(fixedOrig =3D virCPUDefCopyWithoutModel(*origCPU)) || + virCPUDefCopyModelFilter(fixedOrig, *origCPU, false, + virQEMUCapsCPUFilterFeatures, &arch) < 0= )) + goto cleanup; + + if (fixedCPU) { + virCPUDefFree(vm->def->cpu); + VIR_STEAL_PTR(vm->def->cpu, fixedCPU); + } + + if (fixedOrig) { + virCPUDefFree(*origCPU); + VIR_STEAL_PTR(*origCPU, fixedOrig); + } + + ret =3D 0; + + cleanup: + virCPUDefFree(fixedCPU); + virCPUDefFree(fixedOrig); + return ret; +} + + char * qemuDomainGetMachineName(virDomainObjPtr vm) { diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index c34cd37fc4..e430302519 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -971,6 +971,10 @@ qemuDomainUpdateCPU(virDomainObjPtr vm, virCPUDefPtr cpu, virCPUDefPtr *origCPU); =20 +int +qemuDomainFixupCPUs(virDomainObjPtr vm, + virCPUDefPtr *origCPU); + char * qemuDomainGetMachineName(virDomainObjPtr vm); =20 diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 842e088519..2715d86615 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6551,6 +6551,13 @@ qemuDomainSaveImageStartVM(virConnectPtr conn, } } =20 + /* No cookie means libvirt which saved the domain was too old to mess = up + * the CPU definitions. + */ + if (cookie && + qemuDomainFixupCPUs(vm, &cookie->cpu) < 0) + goto cleanup; + if (qemuProcessStart(conn, driver, vm, cookie ? cookie->cpu : NULL, asyncJob, "stdio", *fd, path, NULL, VIR_NETDEV_VPORT_PROFILE_OP_RESTORE, @@ -15754,6 +15761,13 @@ qemuDomainRevertToSnapshot(virDomainSnapshotPtr sn= apshot, if (config) virDomainObjAssignDef(vm, config, false, NULL); =20 + /* No cookie means libvirt which saved the domain was too old = to + * mess up the CPU definitions. + */ + if (cookie && + qemuDomainFixupCPUs(vm, &cookie->cpu) < 0) + goto cleanup; + rc =3D qemuProcessStart(snapshot->domain->conn, driver, vm, cookie ? cookie->cpu : NULL, QEMU_ASYNC_JOB_START, NULL, -1, NULL, sn= ap, diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 8553c5126f..780af5bd78 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6881,6 +6881,7 @@ qemuProcessRefreshCPU(virQEMUDriverPtr driver, virDomainObjPtr vm) { virCapsPtr caps =3D virQEMUDriverGetCapabilities(driver, false); + qemuDomainObjPrivatePtr priv =3D vm->privateData; virCPUDefPtr host =3D NULL; virCPUDefPtr cpu =3D NULL; int ret =3D -1; @@ -6913,6 +6914,14 @@ qemuProcessRefreshCPU(virQEMUDriverPtr driver, =20 if (qemuProcessUpdateCPU(driver, vm, QEMU_ASYNC_JOB_NONE) < 0) goto cleanup; + } else if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QUERY_CPU_MODEL_E= XPANSION)) { + /* We only try to fix CPUs when the libvirt/QEMU combo used to sta= rt + * the domain did not know about query-cpu-model-expansion in which + * case the host-model is known to not contain features which QEMU + * doesn't know about. + */ + if (qemuDomainFixupCPUs(vm, &priv->origCPU) < 0) + goto cleanup; } =20 ret =3D 0; --=20 2.14.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list