From nobody Sat Feb 7 04:40:43 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1642599164; cv=none; d=zohomail.com; s=zohoarc; b=XK6hO8ojibpWh2H1ul1SHsKQ0YRteiwwWtfSqzq7M/5y9YoEQ43JQ0GNuEptfvVYXjbBLE3j1KqjwmoUgDcWHLsyuPd7EBBTW2sTO30mISwBtIvfYM31xbC/khK0XinVst5YZZezszBWDp7dldyUisFiIKr/m9jghT4lAIxtcWE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1642599164; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dygKYcL4BKM+NcZIZhyBCxQxdNgNh5fb9BnAuVWxW6o=; b=fy2uQCSEn5sa8WV3nFw7XBIncCxNNIMWiUTDiv79zw9CEA4OAwOMowAYUkk+/R0a/JoFAK5A+hNgImriJpDoFX3xnnG2ID0OUMnfEMvEyisSHS6t2/GiVgs+QBL50yN06oTxBjvgsLZCuLrLewh35phlhGccVyKgf0YlFZwQ33M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 16425991647291006.8367624992043; Wed, 19 Jan 2022 05:32:44 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-96-GMhIWG9bP5KP2-Ypsj9lSw-1; Wed, 19 Jan 2022 08:32:40 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6A1721006ADB; Wed, 19 Jan 2022 13:32:32 +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 477C284A0F; Wed, 19 Jan 2022 13:32: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 14C261806D2D; Wed, 19 Jan 2022 13:32:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 20JDWTSF009034 for ; Wed, 19 Jan 2022 08:32:29 -0500 Received: by smtp.corp.redhat.com (Postfix) id 7AD847DE4D; Wed, 19 Jan 2022 13:32:29 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF36A7E205; Wed, 19 Jan 2022 13:32:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642599163; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=dygKYcL4BKM+NcZIZhyBCxQxdNgNh5fb9BnAuVWxW6o=; b=IA436aD/oHPm5yRKx293ROOihmSitsnzG7ghCVaIfoU2mwZdeGmc97zgiXBH0RLZhPwicM BL3rTLkdwKBJ5C/Uv+eqzA2evJO7CPohmnNqkFZ8aBeFQNT01WJqcZvCBPDXD7uSLvdkKL qmjcevkSemYSvN0w+5BOV20ADEqTrws= X-MC-Unique: GMhIWG9bP5KP2-Ypsj9lSw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v1 5/5] qemu: Allow prealloc for virtio-mem-pci Date: Wed, 19 Jan 2022 14:32:18 +0100 Message-Id: <1e3fbd519b1cb13b72c3ea1fbff88b7c1ef5eb63.1642598968.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: david@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1642600066496100001 Content-Type: text/plain; charset="utf-8" There are a some scenarios in which we want to prealloc guest memory (e.g. when requested in domain XML, when using hugepages, etc.). With 'regular' models (like 'dimm', 'nvdimm' or 'virtio-pmem') or regular guest memory it is corresponding memory-backend-* object that ends up with .prealloc attribute set. And that's desired because neither of those devices can change its size on the fly. However, with virtio-mem model things are a bit different. While one can set .prealloc attribute on corresponding memory-backend-* object it doesn't make much sense, because virtio-mem can inflate/deflate on the fly, i.e. change how big of a portion of the memory-backend-* object is exposed to the guest. For instance, from a say 4GiB module only a half can be exposed to the guest. Therefore, it doesn't make much sense to preallocate whole 4GiB and keep them allocated. But we still want the part exposed to the guest preallocated (when conditions described at the beginning are met). Having said that, with new enough QEMU the virtio-mem-pci device gained new attribute ".prealloc" which instructs the device to talk to the memory backend object and allocate only the requested portion of memory. Now, that our algorithm for setting .prealloc was isolated in a single function, the function can be called when constructing cmd line for virtio-mem-pci device. Signed-off-by: Michal Privoznik --- src/qemu/qemu_command.c | 42 +++++++++++++------ src/qemu/qemu_command.h | 4 +- src/qemu/qemu_hotplug.c | 2 +- ...mory-hotplug-virtio-mem.x86_64-latest.args | 2 +- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 9ab2b25fe2..c145f6242f 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3690,10 +3690,14 @@ qemuBuildMemoryGetPagesize(virQEMUDriverConfig *cfg, if (useHugepage) prealloc =3D true; =20 - *pagesizeRet =3D pagesize; - *needHugepageRet =3D needHugepage; - *useHugepageRet =3D useHugepage; - *preallocRet =3D prealloc; + if (pagesizeRet) + *pagesizeRet =3D pagesize; + if (needHugepageRet) + *needHugepageRet =3D needHugepage; + if (useHugepageRet) + *useHugepageRet =3D useHugepage; + if (preallocRet) + *preallocRet =3D prealloc; =20 return 0; } @@ -3869,14 +3873,18 @@ qemuBuildMemoryBackendProps(virJSONValue **backendP= rops, return -1; =20 if (mem->model =3D=3D VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM) { - /* Explicitly disable prealloc for virtio-mem as it's not supported - * currently. Warn users if their config would result in prealloc.= */ - if (priv->memPrealloc || prealloc) { - VIR_WARN("Memory preallocation is unsupported for virtio-mem m= emory devices"); + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_VIRTIO_MEM_PC= I_PREALLOC)) { + /* Explicitly disable prealloc for virtio-mem if it isn't supp= orted. + * Warn users if their config would result in prealloc. */ + if (priv->memPrealloc || prealloc) { + VIR_WARN("Memory preallocation is unsupported for virtio-m= em memory devices"); + } + + if (priv->memPrealloc && + virJSONValueObjectAppendBoolean(props, "prealloc", 0) < 0) + return -1; } - if (priv->memPrealloc && - virJSONValueObjectAppendBoolean(props, "prealloc", 0) < 0) - return -1; + if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_MEMORY_BACKEND_RESERV= E) && virJSONValueObjectAppendBoolean(props, "reserve", 0) < 0) return -1; @@ -4029,7 +4037,9 @@ qemuBuildMemoryDimmBackendStr(virCommand *cmd, =20 =20 virJSONValue * -qemuBuildMemoryDeviceProps(const virDomainDef *def, +qemuBuildMemoryDeviceProps(virQEMUDriverConfig *cfg, + qemuDomainObjPrivate *priv, + const virDomainDef *def, const virDomainMemoryDef *mem) { g_autoptr(virJSONValue) props =3D NULL; @@ -4037,6 +4047,7 @@ qemuBuildMemoryDeviceProps(const virDomainDef *def, g_autofree char *uuidstr =3D NULL; virTristateBool unarmed =3D VIR_TRISTATE_BOOL_ABSENT; g_autofree char *memdev =3D NULL; + bool prealloc =3D false; =20 if (!mem->info.alias) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -4060,6 +4071,10 @@ qemuBuildMemoryDeviceProps(const virDomainDef *def, =20 case VIR_DOMAIN_MEMORY_MODEL_VIRTIO_MEM: device =3D "virtio-mem-pci"; + + if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_VIRTIO_MEM_PCI= _PREALLOC) && + qemuBuildMemoryGetPagesize(cfg, def, mem, NULL, NULL, NULL, &p= realloc) < 0) + return NULL; break; =20 case VIR_DOMAIN_MEMORY_MODEL_NONE: @@ -4087,6 +4102,7 @@ qemuBuildMemoryDeviceProps(const virDomainDef *def, "S:uuid", uuidstr, "T:unarmed", unarmed, "s:memdev", memdev, + "B:prealloc", prealloc, "s:id", mem->info.alias, NULL) < 0) return NULL; @@ -7798,7 +7814,7 @@ qemuBuildMemoryDeviceCommandLine(virCommand *cmd, if (qemuBuildMemoryDimmBackendStr(cmd, def->mems[i], def, cfg, pri= v) < 0) return -1; =20 - if (!(props =3D qemuBuildMemoryDeviceProps(def, def->mems[i]))) + if (!(props =3D qemuBuildMemoryDeviceProps(cfg, priv, def, def->me= ms[i]))) return -1; =20 if (qemuBuildDeviceCommandlineFromJSON(cmd, props, priv->qemuCaps)= < 0) diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index dd981e333f..ba175aff9c 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -153,7 +153,9 @@ int qemuBuildMemoryBackendProps(virJSONValue **backendP= rops, bool systemMemory); =20 virJSONValue * -qemuBuildMemoryDeviceProps(const virDomainDef *def, +qemuBuildMemoryDeviceProps(virQEMUDriverConfig *cfg, + qemuDomainObjPrivate *priv, + const virDomainDef *def, const virDomainMemoryDef *mem); =20 /* Current, best practice */ diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index f3ec24635d..efc6db8455 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -2399,7 +2399,7 @@ qemuDomainAttachMemory(virQEMUDriver *driver, =20 objalias =3D g_strdup_printf("mem%s", mem->info.alias); =20 - if (!(devprops =3D qemuBuildMemoryDeviceProps(vm->def, mem))) + if (!(devprops =3D qemuBuildMemoryDeviceProps(cfg, priv, vm->def, mem)= )) goto cleanup; =20 if (qemuBuildMemoryBackendProps(&props, objalias, cfg, diff --git a/tests/qemuxml2argvdata/memory-hotplug-virtio-mem.x86_64-latest= .args b/tests/qemuxml2argvdata/memory-hotplug-virtio-mem.x86_64-latest.args index 1d83ec88a8..77dbc0c89c 100644 --- a/tests/qemuxml2argvdata/memory-hotplug-virtio-mem.x86_64-latest.args +++ b/tests/qemuxml2argvdata/memory-hotplug-virtio-mem.x86_64-latest.args @@ -21,7 +21,7 @@ XDG_CONFIG_HOME=3D/tmp/lib/domain--1-QEMUGuest1/.config \ -object '{"qom-type":"memory-backend-ram","id":"memvirtiomem0","reserve":f= alse,"size":1073741824}' \ -device '{"driver":"virtio-mem-pci","node":0,"block-size":2097152,"request= ed-size":536870912,"memdev":"memvirtiomem0","id":"virtiomem0","bus":"pci.0"= ,"addr":"0x2"}' \ -object '{"qom-type":"memory-backend-file","id":"memvirtiomem1","mem-path"= :"/dev/hugepages2M/libvirt/qemu/-1-QEMUGuest1","reserve":false,"size":21474= 83648,"host-nodes":[1,2,3],"policy":"bind"}' \ --device '{"driver":"virtio-mem-pci","node":0,"block-size":2097152,"request= ed-size":1073741824,"memdev":"memvirtiomem1","id":"virtiomem1","bus":"pci.0= ","addr":"0x3"}' \ +-device '{"driver":"virtio-mem-pci","node":0,"block-size":2097152,"request= ed-size":1073741824,"memdev":"memvirtiomem1","prealloc":true,"id":"virtiome= m1","bus":"pci.0","addr":"0x3"}' \ -uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \ -display none \ -no-user-config \ --=20 2.34.1