From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804924; cv=none; d=zohomail.com; s=zohoarc; b=KPJ+IkXc5tpHCDpAFBKCuwghLFethvYh7KV6xy3QlrWkkVgst1mrp9H1zonSHX9ujfnKz1gwkkx3qyCeEMIw2zISBA9erYvkP5Z9F91JpRNisi3NXKIuL9TLxaY6LTnIU9C9/8xD+8ib9iajOX2MtiX8kj1wq6efht0w1i9WzC8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804924; h=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=OYmUdM9xnUxYUikMjy3S8zQ+vg85pGF8BhztC9qJ42Y=; b=F+uNA1osVIv4qH0pK9ksPsWTGov0ieiTgExQiF2tmwWEArPtaGxdMmy5ZLSURiecfjJCEaPDLABRFARiDM53RjCqwzXKiILtQOHtGGd8UCh93YCoR6VzGj5196xbKxqHKe6M7AAcA1a6cCpznHRtJz+00whWc0e53fTMImKWZ50= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804923679672.7966779210178; Wed, 23 Aug 2023 08:35:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpsg-0001yn-EA; Wed, 23 Aug 2023 11:34:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsG-0001sz-3U for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsD-0007r0-PR for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:23 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-323-DjnJofkYP9-s-tVund76sw-1; Wed, 23 Aug 2023 11:34:19 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B6499101AA62; Wed, 23 Aug 2023 15:34:18 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id F1AAA1121314; Wed, 23 Aug 2023 15:34:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OYmUdM9xnUxYUikMjy3S8zQ+vg85pGF8BhztC9qJ42Y=; b=XRoSiPKgIEHAGOMXcCZYgOkRml3iVKjq43GJS9W4cyb9GKby1ruSVyILlinzKPL5kFq1Ly bgPwKCWT5hhvebqkRPyVTt2+JB7DsRXEeh02QAZEb14NeJmhOxKb9rA4CvkGp11c9HZKRz o23RJFMiunNfjWzraSaqnoDGkZj662E= X-MC-Unique: DjnJofkYP9-s-tVund76sw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 01/11] nvdimm: Reject writing label data to ROM instead of crashing QEMU Date: Wed, 23 Aug 2023 17:34:01 +0200 Message-ID: <20230823153412.832081-2-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804925485100004 Content-Type: text/plain; charset="utf-8" Currently, when using a true R/O NVDIMM (ROM memory backend) with a label area, the VM can easily crash QEMU by trying to write to the label area, because the ROM memory is mmap'ed without PROT_WRITE. [root@vm-0 ~]# ndctl disable-region region0 disabled 1 region [root@vm-0 ~]# ndctl zero-labels nmem0 -> QEMU segfaults Let's remember whether we have a ROM memory backend and properly reject the write request: [root@vm-0 ~]# ndctl disable-region region0 disabled 1 region [root@vm-0 ~]# ndctl zero-labels nmem0 zeroed 0 nmem In comparison, on a system with a R/W NVDIMM: [root@vm-0 ~]# ndctl disable-region region0 disabled 1 region [root@vm-0 ~]# ndctl zero-labels nmem0 zeroed 1 nmem For ACPI, just return "unsupported", like if no label exists. For spapr, return "H_P2", similar to when no label area exists. Could we rely on the "unarmed" property? Maybe, but it looks cleaner to only disallow what certainly cannot work. After all "unarmed=3Don" primarily means: cannot accept persistent writes. = In theory, there might be setups where devices with "unarmed=3Don" set could be used to host non-persistent data (temporary files, system RAM, ...); for example, in Linux, admins can overwrite the "readonly" setting and still write to the device -- which will work as long as we're not using ROM. Allowing writing label data in such configurations can make sense. Fixes: dbd730e85987 ("nvdimm: check -object memory-backend-file, readonly= =3Don option") Reviewed-by: Stefan Hajnoczi Signed-off-by: David Hildenbrand --- hw/acpi/nvdimm.c | 11 ++++++++--- hw/mem/nvdimm.c | 10 +++++++--- hw/ppc/spapr_nvdimm.c | 3 ++- include/hw/mem/nvdimm.h | 6 ++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/hw/acpi/nvdimm.c b/hw/acpi/nvdimm.c index a3b25a92f3..3cbd41629d 100644 --- a/hw/acpi/nvdimm.c +++ b/hw/acpi/nvdimm.c @@ -670,7 +670,8 @@ static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm,= hwaddr dsm_mem_addr) } =20 static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm, - uint32_t offset, uint32_t lengt= h) + uint32_t offset, uint32_t lengt= h, + bool is_write) { uint32_t ret =3D NVDIMM_DSM_RET_STATUS_INVALID; =20 @@ -690,6 +691,10 @@ static uint32_t nvdimm_rw_label_data_check(NVDIMMDevic= e *nvdimm, return ret; } =20 + if (is_write && nvdimm->readonly) { + return NVDIMM_DSM_RET_STATUS_UNSUPPORT; + } + return NVDIMM_DSM_RET_STATUS_SUCCESS; } =20 @@ -713,7 +718,7 @@ static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvd= imm, NvdimmDsmIn *in, get_label_data->length); =20 status =3D nvdimm_rw_label_data_check(nvdimm, get_label_data->offset, - get_label_data->length); + get_label_data->length, false); if (status !=3D NVDIMM_DSM_RET_STATUS_SUCCESS) { nvdimm_dsm_no_payload(status, dsm_mem_addr); return; @@ -752,7 +757,7 @@ static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvd= imm, NvdimmDsmIn *in, set_label_data->length); =20 status =3D nvdimm_rw_label_data_check(nvdimm, set_label_data->offset, - set_label_data->length); + set_label_data->length, true); if (status !=3D NVDIMM_DSM_RET_STATUS_SUCCESS) { nvdimm_dsm_no_payload(status, dsm_mem_addr); return; diff --git a/hw/mem/nvdimm.c b/hw/mem/nvdimm.c index 31080c22c9..1631a7d13f 100644 --- a/hw/mem/nvdimm.c +++ b/hw/mem/nvdimm.c @@ -154,6 +154,9 @@ static void nvdimm_prepare_memory_region(NVDIMMDevice *= nvdimm, Error **errp) object_get_canonical_path_component(OBJECT(hostmem))); return; } + if (memory_region_is_rom(mr)) { + nvdimm->readonly =3D true; + } =20 nvdimm->nvdimm_mr =3D g_new(MemoryRegion, 1); memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm), @@ -207,15 +210,16 @@ static void nvdimm_unrealize(PCDIMMDevice *dimm) * label read/write functions. */ static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t s= ize, - uint64_t offset) + uint64_t offset, bool is_write) { assert((nvdimm->label_size >=3D size + offset) && (offset + size > off= set)); + assert(!is_write || !nvdimm->readonly); } =20 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf, uint64_t size, uint64_t offset) { - nvdimm_validate_rw_label_data(nvdimm, size, offset); + nvdimm_validate_rw_label_data(nvdimm, size, offset, false); =20 memcpy(buf, nvdimm->label_data + offset, size); } @@ -229,7 +233,7 @@ static void nvdimm_write_label_data(NVDIMMDevice *nvdim= m, const void *buf, "pmem", NULL); uint64_t backend_offset; =20 - nvdimm_validate_rw_label_data(nvdimm, size, offset); + nvdimm_validate_rw_label_data(nvdimm, size, offset, true); =20 if (!is_pmem) { memcpy(nvdimm->label_data + offset, buf, size); diff --git a/hw/ppc/spapr_nvdimm.c b/hw/ppc/spapr_nvdimm.c index a8688243a6..60d6d0acc0 100644 --- a/hw/ppc/spapr_nvdimm.c +++ b/hw/ppc/spapr_nvdimm.c @@ -320,7 +320,8 @@ static target_ulong h_scm_write_metadata(PowerPCCPU *cp= u, =20 nvdimm =3D NVDIMM(drc->dev); if ((offset + len < offset) || - (nvdimm->label_size < len + offset)) { + (nvdimm->label_size < len + offset) || + nvdimm->readonly) { return H_P2; } =20 diff --git a/include/hw/mem/nvdimm.h b/include/hw/mem/nvdimm.h index acf887c83d..d3b763453a 100644 --- a/include/hw/mem/nvdimm.h +++ b/include/hw/mem/nvdimm.h @@ -77,6 +77,12 @@ struct NVDIMMDevice { */ bool unarmed; =20 + /* + * Whether our DIMM is backed by ROM, and even label data cannot be + * written. If set, implies that "unarmed" is also set. + */ + bool readonly; + /* * The PPC64 - spapr requires each nvdimm device have a uuid. */ --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804924; cv=none; d=zohomail.com; s=zohoarc; b=UKGB0dhX5IwDl2YtJoBLKWUCue9CCNgculul3FHghaL3FLTMeeAj5NbNrco3WBo7Tg0PGoQlbN/VXyH0MGgpHigSD6puMrOIFyZGeNiHdTJHtsBWyukl+RXTSKAiw57zByNslKVDn7i3BuIfgcwqGd6Cb32qd+F0yLOmUjDhumQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804924; h=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=9CzMKg1HAEzal46X3K+Yyo/0UZfnJlj1bng/MWpcxIs=; b=CShAlyz0GAEad33AslyqKMRMNp0SnrWqKa7H6+13/zvf8h+PSVzQEDjZCVq+PAu/p8UqxuRFYbISsADA2EfEgaZTR55PqEnGlwk072DHAedXuRwhctTtmBhzzhR1oRzzpQTtdfFShWEbkkO6Iwzfny8TiMY9jOKHdUgA4Brqp2Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169280492419269.56175731514213; Wed, 23 Aug 2023 08:35:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpsO-0001v1-ER; Wed, 23 Aug 2023 11:34:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsK-0001tM-RO for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsI-0007vd-6x for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:28 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-55-LN1_r5KpOyaGVlRmyRKaAQ-1; Wed, 23 Aug 2023 11:34:22 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B65783C0FC9C; Wed, 23 Aug 2023 15:34:21 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2AF71121314; Wed, 23 Aug 2023 15:34:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804865; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9CzMKg1HAEzal46X3K+Yyo/0UZfnJlj1bng/MWpcxIs=; b=fThOW+QMRGqRRc9dz69rApV5jXlwRg1qq96EhzZLG2tiTrCjUeD7jVjAPkntgNd9p6yBuu SH5eMUukvA78+XY4AlyzonbdlBIiHl/GZ7hzEqlYBGA9Y14LkDClczJyKdjAfbX1QWKhZg s+86V/f306hCxBv5gPuqiuEPlxbUE+0= X-MC-Unique: LN1_r5KpOyaGVlRmyRKaAQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 02/11] softmmu/physmem: Distinguish between file access mode and mmap protection Date: Wed, 23 Aug 2023 17:34:02 +0200 Message-ID: <20230823153412.832081-3-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804926083100011 Content-Type: text/plain; charset="utf-8" There is a difference between how we open a file and how we mmap it, and we want to support writable private mappings of readonly files. Let's define RAM_READONLY and RAM_READONLY_FD flags, to replace the single "readonly" parameter for file-related functions. In memory_region_init_ram_from_fd() and memory_region_init_ram_from_file(), initialize mr->readonly based on the new RAM_READONLY flag. While at it, add some RAM_* flags we missed to add to the list of accepted flags in the documentation of some functions. No change in functionality intended. We'll make use of both flags next and start setting them independently for memory-backend-file. Signed-off-by: David Hildenbrand Acked-by: Peter Xu --- backends/hostmem-file.c | 4 ++-- include/exec/memory.h | 14 ++++++++++---- include/exec/ram_addr.h | 8 ++++---- softmmu/memory.c | 8 ++++---- softmmu/physmem.c | 21 ++++++++++----------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index b4335a80e6..ef2d5533af 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -55,13 +55,13 @@ file_backend_memory_alloc(HostMemoryBackend *backend, E= rror **errp) =20 name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; + ram_flags |=3D fb->readonly ? RAM_READONLY | RAM_READONLY_FD : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; ram_flags |=3D RAM_NAMED_FILE; memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, backend->size, fb->align, ram_flags, - fb->mem_path, fb->offset, fb->readonl= y, - errp); + fb->mem_path, fb->offset, errp); g_free(name); #endif } diff --git a/include/exec/memory.h b/include/exec/memory.h index 68284428f8..cc68249eda 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -235,6 +235,12 @@ typedef struct IOMMUTLBEvent { /* RAM is an mmap-ed named file */ #define RAM_NAMED_FILE (1 << 9) =20 +/* RAM is mmap-ed read-only */ +#define RAM_READONLY (1 << 10) + +/* RAM FD is opened read-only */ +#define RAM_READONLY_FD (1 << 11) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, @@ -1331,10 +1337,10 @@ void memory_region_init_resizeable_ram(MemoryRegion= *mr, * @align: alignment of the region base address; if 0, the default alignme= nt * (getpagesize()) will be used. * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, - * RAM_NORESERVE, + * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, + * RAM_READONLY_FD * @path: the path in which to allocate the RAM. * @offset: offset within the file referenced by path - * @readonly: true to open @path for reading, false for read/write. * @errp: pointer to Error*, to store an error if it happens. * * Note that this function does not do anything to cause the data in the @@ -1348,7 +1354,6 @@ void memory_region_init_ram_from_file(MemoryRegion *m= r, uint32_t ram_flags, const char *path, ram_addr_t offset, - bool readonly, Error **errp); =20 /** @@ -1360,7 +1365,8 @@ void memory_region_init_ram_from_file(MemoryRegion *m= r, * @name: the name of the region. * @size: size of the region. * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, - * RAM_NORESERVE, RAM_PROTECTED. + * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, + * RAM_READONLY_FD * @fd: the fd to mmap. * @offset: offset within the file referenced by fd * @errp: pointer to Error*, to store an error if it happens. diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 9f2e3893f5..90676093f5 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -108,10 +108,10 @@ long qemu_maxrampagesize(void); * @size: the size in bytes of the ram block * @mr: the memory region where the ram block is * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, - * RAM_NORESERVE. + * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, + * RAM_READONLY_FD * @mem_path or @fd: specify the backing file or device * @offset: Offset into target file - * @readonly: true to open @path for reading, false for read/write. * @errp: pointer to Error*, to store an error if it happens * * Return: @@ -120,10 +120,10 @@ long qemu_maxrampagesize(void); */ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, uint32_t ram_flags, const char *mem_pat= h, - off_t offset, bool readonly, Error **er= rp); + off_t offset, Error **errp); RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, uint32_t ram_flags, int fd, off_t offset, - bool readonly, Error **errp); + Error **errp); =20 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, MemoryRegion *mr, Error **errp); diff --git a/softmmu/memory.c b/softmmu/memory.c index 7d9494ce70..2cb60ec9b8 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1620,18 +1620,17 @@ void memory_region_init_ram_from_file(MemoryRegion = *mr, uint32_t ram_flags, const char *path, ram_addr_t offset, - bool readonly, Error **errp) { Error *err =3D NULL; memory_region_init(mr, owner, name, size); mr->ram =3D true; - mr->readonly =3D readonly; + mr->readonly =3D !!(ram_flags & RAM_READONLY); mr->terminates =3D true; mr->destructor =3D memory_region_destructor_ram; mr->align =3D align; mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, ram_flags, path, - offset, readonly, &err); + offset, &err); if (err) { mr->size =3D int128_zero(); object_unparent(OBJECT(mr)); @@ -1651,10 +1650,11 @@ void memory_region_init_ram_from_fd(MemoryRegion *m= r, Error *err =3D NULL; memory_region_init(mr, owner, name, size); mr->ram =3D true; + mr->readonly =3D !!(ram_flags & RAM_READONLY); mr->terminates =3D true; mr->destructor =3D memory_region_destructor_ram; mr->ram_block =3D qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offs= et, - false, &err); + &err); if (err) { mr->size =3D int128_zero(); object_unparent(OBJECT(mr)); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 3df73542e1..6add537b2a 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1351,7 +1351,6 @@ static int file_ram_open(const char *path, static void *file_ram_alloc(RAMBlock *block, ram_addr_t memory, int fd, - bool readonly, bool truncate, off_t offset, Error **errp) @@ -1409,7 +1408,7 @@ static void *file_ram_alloc(RAMBlock *block, perror("ftruncate"); } =20 - qemu_map_flags =3D readonly ? QEMU_MAP_READONLY : 0; + qemu_map_flags =3D (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY := 0; qemu_map_flags |=3D (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0; qemu_map_flags |=3D (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0; qemu_map_flags |=3D (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERV= E : 0; @@ -1877,7 +1876,7 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp) #ifdef CONFIG_POSIX RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, uint32_t ram_flags, int fd, off_t offset, - bool readonly, Error **errp) + Error **errp) { RAMBlock *new_block; Error *local_err =3D NULL; @@ -1885,7 +1884,8 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, =20 /* Just support these ram flags by now. */ assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE | - RAM_PROTECTED | RAM_NAMED_FILE)) =3D=3D 0); + RAM_PROTECTED | RAM_NAMED_FILE | RAM_READONLY | + RAM_READONLY_FD)) =3D=3D 0); =20 if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); @@ -1920,8 +1920,8 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, new_block->used_length =3D size; new_block->max_length =3D size; new_block->flags =3D ram_flags; - new_block->host =3D file_ram_alloc(new_block, size, fd, readonly, - !file_size, offset, errp); + new_block->host =3D file_ram_alloc(new_block, size, fd, !file_size, of= fset, + errp); if (!new_block->host) { g_free(new_block); return NULL; @@ -1940,20 +1940,19 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, M= emoryRegion *mr, =20 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, uint32_t ram_flags, const char *mem_pat= h, - off_t offset, bool readonly, Error **er= rp) + off_t offset, Error **errp) { int fd; bool created; RAMBlock *block; =20 - fd =3D file_ram_open(mem_path, memory_region_name(mr), readonly, &crea= ted, - errp); + fd =3D file_ram_open(mem_path, memory_region_name(mr), + !!(ram_flags & RAM_READONLY_FD), &created, errp); if (fd < 0) { return NULL; } =20 - block =3D qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offset, read= only, - errp); + block =3D qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offset, errp= ); if (!block) { if (created) { unlink(mem_path); --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804989; cv=none; d=zohomail.com; s=zohoarc; b=WvFdoeHKezwcnGVb9/WdAfEze4xOMkPNEgJGf6W+hIvpIUD33bSEtpvm3OY7aDvuRs1kH0ViytXpCD789vaSsZphZX+DUFNni9+RmsBcxPIL4L5f7A6eRDZywyV9gPwINvXq5K0rMOxFqhOYQHmruSrM4+Hh6pIBr9QNbBWs7ns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804989; h=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=VO47LSVQYys4ca0LVUTtRTIQSZy6ad56mDKp1WS/7KU=; b=j6P7v8CfARHdRj+rmoQnNWqY7eTVjnpOtIlkxAGdhuQ22Jj1p+gy03Sq5mxjsdXI4W89fuEw38usM4fCY73DTe/tx2MOJR/vIQnkx50Zn0iUVm6tc+oj/b5gHY8SAirztyeQ63dmrOmmye4Ord0C1gQ2+m+FeUKwz2uAZvxCFR8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804989725304.89009330263104; Wed, 23 Aug 2023 08:36:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpsk-000216-95; Wed, 23 Aug 2023 11:34:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsP-0001wD-En for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsN-0007z1-Cr for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:33 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-488-XbPsutRfN1yD8hnyzFsqkg-1; Wed, 23 Aug 2023 11:34:25 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B70F73C0FC92; Wed, 23 Aug 2023 15:34:24 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2BB91121319; Wed, 23 Aug 2023 15:34:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804870; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VO47LSVQYys4ca0LVUTtRTIQSZy6ad56mDKp1WS/7KU=; b=EHxba6ynMGLPI4MtN53qfRPekc1x3bNrVMcVAYXUAvVaY0z6l7Zkhu2IbY02Iw+yf3WAiY 7LA6P6YBpUHWY5PWNRlAA/T3GSUI+3m6rGUz9Ec1s1cYP1TBLojiVgeMWWWo2gETzO9n8i EMl2QrdTCgsifOKM7/DQiLqqNEDDhGU= X-MC-Unique: XbPsutRfN1yD8hnyzFsqkg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 03/11] backends/hostmem-file: Add "rom" property to support VM templating with R/O files Date: Wed, 23 Aug 2023 17:34:03 +0200 Message-ID: <20230823153412.832081-4-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804990862100001 Content-Type: text/plain; charset="utf-8" For now, "share=3Doff,readonly=3Don" would always result in us opening the file R/O and mmap'ing the opened file MAP_PRIVATE R/O -- effectively turning it into ROM. Especially for VM templating, "share=3Doff" is a common use case. However, that use case is impossible with files that lack write permissions, because "share=3Doff,readonly=3Don" will not give us writable RAM. The sole user of ROM via memory-backend-file are R/O NVDIMMs, but as we have users (Kata Containers) that rely on the existing behavior -- malicious VMs should not be able to consume COW memory for R/O NVDIMMs -- we cannot change the semantics of "share=3Doff,readonly=3Don" So let's add a new "rom" property with on/off/auto values. "auto" is the default and what most people will use: for historical reasons, to not change the old semantics, it defaults to the value of the "readonly" property. For VM templating, one can now use: -object memory-backend-file,share=3Doff,readonly=3Don,rom=3Doff,... But we'll disallow: -object memory-backend-file,share=3Don,readonly=3Don,rom=3Doff,... because we would otherwise get an error when trying to mmap the R/O file shared and writable. An explicit error message is cleaner. We will also disallow for now: -object memory-backend-file,share=3Doff,readonly=3Doff,rom=3Don,... -object memory-backend-file,share=3Don,readonly=3Doff,rom=3Don,... It's not harmful, but also not really required for now. Alternatives that were abandoned: * Make "unarmed=3Don" for the NVDIMM set the memory region container readonly. We would still see a change of ROM->RAM and possibly run into memslot limits with vhost-user. Further, there might be use cases for "unarmed=3Don" that should still allow writing to that memory (temporary files, system RAM, ...). * Add a new "readonly=3Don/off/auto" parameter for NVDIMMs. Similar issues as with "unarmed=3Don". * Make "readonly" consume "on/off/file" instead of being a 'bool' type. This would slightly changes the behavior of the "readonly" parameter: values like true/false (as accepted by a 'bool'type) would no longer be accepted. Signed-off-by: David Hildenbrand Acked-by: Markus Armbruster --- backends/hostmem-file.c | 59 ++++++++++++++++++++++++++++++++++++++++- qapi/qom.json | 17 +++++++++++- qemu-options.hx | 16 ++++++++++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index ef2d5533af..361d4a8103 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -18,6 +18,8 @@ #include "sysemu/hostmem.h" #include "qom/object_interfaces.h" #include "qom/object.h" +#include "qapi/visitor.h" +#include "qapi/qapi-visit-common.h" =20 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE) =20 @@ -31,6 +33,7 @@ struct HostMemoryBackendFile { bool discard_data; bool is_pmem; bool readonly; + OnOffAuto rom; }; =20 static void @@ -53,9 +56,33 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Er= ror **errp) return; } =20 + switch (fb->rom) { + case ON_OFF_AUTO_AUTO: + /* Traditionally, opening the file readonly always resulted in ROM= . */ + fb->rom =3D fb->readonly ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; + break; + case ON_OFF_AUTO_ON: + if (!fb->readonly) { + error_setg(errp, "property 'rom' =3D 'on' is not supported wit= h" + " 'readonly' =3D 'off'"); + return; + } + break; + case ON_OFF_AUTO_OFF: + if (fb->readonly && backend->share) { + error_setg(errp, "property 'rom' =3D 'off' is incompatible wit= h" + " 'readonly' =3D 'on' and 'share' =3D 'on'"); + return; + } + break; + default: + assert(false); + } + name =3D host_memory_backend_get_name(backend); ram_flags =3D backend->share ? RAM_SHARED : 0; - ram_flags |=3D fb->readonly ? RAM_READONLY | RAM_READONLY_FD : 0; + ram_flags |=3D fb->readonly ? RAM_READONLY_FD : 0; + ram_flags |=3D fb->rom =3D=3D ON_OFF_AUTO_ON ? RAM_READONLY : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; ram_flags |=3D RAM_NAMED_FILE; @@ -201,6 +228,32 @@ static void file_memory_backend_set_readonly(Object *o= bj, bool value, fb->readonly =3D value; } =20 +static void file_memory_backend_get_rom(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + OnOffAuto rom =3D fb->rom; + + visit_type_OnOffAuto(v, name, &rom, errp); +} + +static void file_memory_backend_set_rom(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + + if (host_memory_backend_mr_inited(backend)) { + error_setg(errp, "cannot change property '%s' of %s.", name, + object_get_typename(obj)); + return; + } + + visit_type_OnOffAuto(v, name, &fb->rom, errp); +} + static void file_backend_unparent(Object *obj) { HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); @@ -243,6 +296,10 @@ file_backend_class_init(ObjectClass *oc, void *data) object_class_property_add_bool(oc, "readonly", file_memory_backend_get_readonly, file_memory_backend_set_readonly); + object_class_property_add(oc, "rom", "OnOffAuto", + file_memory_backend_get_rom, file_memory_backend_set_rom, NULL, NU= LL); + object_class_property_set_description(oc, "rom", + "Whether to create Read Only Memory (ROM)"); } =20 static void file_backend_instance_finalize(Object *o) diff --git a/qapi/qom.json b/qapi/qom.json index fa3e88c8e6..c53ef978ff 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -668,6 +668,20 @@ # @readonly: if true, the backing file is opened read-only; if false, # it is opened read-write. (default: false) # +# @rom: whether to create Read Only Memory (ROM) that cannot be modified +# by the VM. Any write attempts to such ROM will be denied. Most +# use cases want writable RAM instead of ROM. However, selected use +# cases, like R/O NVDIMMs, can benefit from ROM. If set to 'on', +# create ROM; if set to 'off', create writable RAM; if set to +# 'auto', the value of the @readonly property is used. This +# property is primarily helpful when we want to have proper RAM in +# configurations that would traditionally create ROM before this +# property was introduced: VM templating, where we want to open a +# file readonly (@readonly set to true) and mark the memory to be +# private for QEMU (@share set to false). For this use case, we need +# writable RAM instead of ROM, and want to set this property to 'off= '. +# (default: auto, since 8.2) +# # Since: 2.1 ## { 'struct': 'MemoryBackendFileProperties', @@ -677,7 +691,8 @@ '*discard-data': 'bool', 'mem-path': 'str', '*pmem': { 'type': 'bool', 'if': 'CONFIG_LIBPMEM' }, - '*readonly': 'bool' } } + '*readonly': 'bool', + '*rom': 'OnOffAuto' } } =20 ## # @MemoryBackendMemfdProperties: diff --git a/qemu-options.hx b/qemu-options.hx index 29b98c3d4c..44035a0490 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4976,7 +4976,7 @@ SRST they are specified. Note that the 'id' property must be set. These objects are placed in the '/objects' path. =20 - ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off,prealloc=3Don|= off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|interleave,alig= n=3Dalign,offset=3Doffset,readonly=3Don|off`` + ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off,prealloc=3Don|= off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|interleave,alig= n=3Dalign,offset=3Doffset,readonly=3Don|off,rom=3Don|off|auto`` Creates a memory file backend object, which can be used to back the guest RAM with huge pages. =20 @@ -5066,6 +5066,20 @@ SRST The ``readonly`` option specifies whether the backing file is open= ed read-only or read-write (default). =20 + The ``rom`` option specifies whether to create Read Only Memory + (ROM) that cannot be modified by the VM. Any write attempts to such + ROM will be denied. Most use cases want proper RAM instead of ROM. + However, selected use cases, like R/O NVDIMMs, can benefit from + ROM. If set to ``on``, create ROM; if set to ``off``, create + writable RAM; if set to ``auto`` (default), the value of the + ``readonly`` option is used. This option is primarily helpful when + we want to have writable RAM in configurations that would + traditionally create ROM before the ``rom`` option was introduced: + VM templating, where we want to open a file readonly + (``readonly=3Don``) and mark the memory to be private for QEMU + (``share=3Doff``). For this use case, we need writable RAM instead + of ROM, and want to also set ``rom=3Doff``. + ``-object memory-backend-ram,id=3Did,merge=3Don|off,dump=3Don|off,shar= e=3Don|off,prealloc=3Don|off,size=3Dsize,host-nodes=3Dhost-nodes,policy=3Dd= efault|preferred|bind|interleave`` Creates a memory backend object, which can be used to back the guest RAM. Memory backend objects offer more control than the --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804924; cv=none; d=zohomail.com; s=zohoarc; b=m/74qYV7UPNOdmjEqVC2ShOh4JK3VDrl2qQr6IPCTrl3cxkRnFU3yxS/6qHVP86Gn2q2EfCBCo6/GHaiLwwaySrkRrrHPL7FjGeNlHVsz7TfnowsllZT3mfJbfQ6BnyETpiCUrPOGV2ScqseZYJOzQYPX2r3EIn5hG+JcZQd62o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804924; h=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=+avBoJlPY2QA1l0z1rA0IKiYjyv5cmykuGuOlOZllow=; b=JemsQihuifvZOhD42wH96Iz7IL6L6wSEbnU3r05WqfdgmRSCdU44i2ethTrwmZ5h+Pv/IIodQl70jmVX8RiCBIptVqhWPM5aETmN4epdcLv+FrFoSAyIExszdCOvnpMG5KAHUx4eINvG8Nmm//A+APVBW3+2mgKTfG7t8FpYsdU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804924148660.6519994839517; Wed, 23 Aug 2023 08:35:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpsl-00022j-AH; Wed, 23 Aug 2023 11:34:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsQ-0001wg-I1 for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsO-0007zC-Lv for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:34 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-394-3r_YfNurPwWoVIN1UOWSzg-1; Wed, 23 Aug 2023 11:34:28 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB1A6853065; Wed, 23 Aug 2023 15:34:27 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2E351121314; Wed, 23 Aug 2023 15:34:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804872; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+avBoJlPY2QA1l0z1rA0IKiYjyv5cmykuGuOlOZllow=; b=VhA6ObtIVV4hK+mqCHAn+00gZiGjRDJKL1ylJ3akmO01wiha0U76zf4i4jR8D0haSJOJcw tKgD1S+DLw9zFmIT1UceKMsd2/Hy8ZjetNLi2nQ2KmBWuSMv2I9OqQ7QHrKQokwB/39rvQ V7kPrUmTBizCoxZkCMEmLD6l3tsBcMI= X-MC-Unique: 3r_YfNurPwWoVIN1UOWSzg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 04/11] softmmu/physmem: Remap with proper protection in qemu_ram_remap() Date: Wed, 23 Aug 2023 17:34:04 +0200 Message-ID: <20230823153412.832081-5-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804925467100003 Content-Type: text/plain; charset="utf-8" Let's remap with the proper protection that we can derive from RAM_READONLY. Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- softmmu/physmem.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 6add537b2a..c09eda6303 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2070,6 +2070,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t lengt= h) ram_addr_t offset; int flags; void *area, *vaddr; + int prot; =20 RAMBLOCK_FOREACH(block) { offset =3D addr - block->offset; @@ -2084,13 +2085,14 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t len= gth) flags |=3D block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE; flags |=3D block->flags & RAM_NORESERVE ? MAP_NORESERVE : = 0; + prot =3D PROT_READ; + prot |=3D block->flags & RAM_READONLY ? 0 : PROT_WRITE; if (block->fd >=3D 0) { - area =3D mmap(vaddr, length, PROT_READ | PROT_WRITE, - flags, block->fd, offset + block->fd_offse= t); + area =3D mmap(vaddr, length, prot, flags, block->fd, + offset + block->fd_offset); } else { flags |=3D MAP_ANONYMOUS; - area =3D mmap(vaddr, length, PROT_READ | PROT_WRITE, - flags, -1, 0); + area =3D mmap(vaddr, length, prot, flags, -1, 0); } if (area !=3D vaddr) { error_report("Could not remap addr: " --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804980; cv=none; d=zohomail.com; s=zohoarc; b=EhhApAYhFLx5ErVGPM3fIjvvrAh5e+hCBF+eejd55hONnIbIFdymXVLkDtzL2w9f0FmYJPdaW/VQiiVqvllWNcVq96rnUTYu4R+0iOgWpYZx5XVXSf2eZpneQCXT3H0kfJNeEHcDhhY+9Ko94gMnK5f2fFzHFOxgcK2Dp45A6FQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804980; h=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=wOd7zFpQSipVkQaSUcZss+T9Cg0l5e6skiyqv1IOvlA=; b=UIwxpe5ag/DpUZvHGTRjM4LM/UeO0pvFwsKOtZTYra1QHrhxfEd5Tq3qxMLSl7Fnk9w5aQy8YwMcRMW3gg9HJV933+ZPD/I4lJ04atz7VLCHoqS4PeZwFSnu+jeox+kAX5RX7mMVchquKmUjfJNXGRsn5S+t5XTuk5tflFIM5/g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804980691163.45420644908347; Wed, 23 Aug 2023 08:36:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpsx-0002Hx-NK; Wed, 23 Aug 2023 11:35:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsV-0001wz-9v for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsR-0007zw-TY for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:38 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-439-ZHtVveJkNFGIwLo9kfpcZw-1; Wed, 23 Aug 2023 11:34:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E6780381C163; Wed, 23 Aug 2023 15:34:30 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 11B3B1121314; Wed, 23 Aug 2023 15:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wOd7zFpQSipVkQaSUcZss+T9Cg0l5e6skiyqv1IOvlA=; b=DRqyCYq1LDHhfrRElxx/4001+i7TyQ69AgkhT3m/ft02lw7dhCqlOoC6AQIkAc7hMUqB90 2Ps+sAE6GCBKv3c+uHMAm1HkDqmaV4NH+lLcNaBapV2TBPzGt8d503M6+NPE+NCIwPloRs dd3J19PxOBu0qpP69OwWta/9TXSNYqU= X-MC-Unique: ZHtVveJkNFGIwLo9kfpcZw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 05/11] softmmu/physmem: Bail out early in ram_block_discard_range() with readonly files Date: Wed, 23 Aug 2023 17:34:05 +0200 Message-ID: <20230823153412.832081-6-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804982706100007 Content-Type: text/plain; charset="utf-8" fallocate() will fail, let's print a nicer error message. Suggested-by: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- softmmu/physmem.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index c09eda6303..32d641754b 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3457,6 +3457,16 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t s= tart, size_t length) * so a userfault will trigger. */ #ifdef CONFIG_FALLOCATE_PUNCH_HOLE + /* + * fallocate() will fail with readonly files. Let's print a + * proper error message. + */ + if (rb->flags & RAM_READONLY_FD) { + error_report("ram_block_discard_range: Discarding RAM" + " with readonly files is not supported"); + goto err; + + } /* * We'll discard data from the actual file, even though we only * have a MAP_PRIVATE mapping, possibly messing with other --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804980; cv=none; d=zohomail.com; s=zohoarc; b=KG5TuXPcMuZ2iS3MPTe1gWF0JBIKlcbnFJK62Y0tLtdzt2wMgMZKtNuxVebPDR/NxMETvrTNcfkO/N0ybQ3tdVNOjGHvw74ZsEq67AcCkynitdmdz5wrRTI8myl6oTbmRkm7HgXZ66G4Rdq7WBq+b9rliHvKpumIJs+nkmqp4Lk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804980; h=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=TwJjkgHsvXEBoKNEpyqvsbIlhmZVjsh0HgCHFDNzu50=; b=jOU9zcWcyIgxztN+Ql5SqjonaqZQJaDgzC+A0+JuKhQbcfSwwGotFh4iJufaHm2qfxQc7Wvs5mTxdB+S8jy/ZnB2b8yxXnMT3q14D0yoq41iBunNeFAihh1yXQQIWRUuNMhCt90QRJlRgk+gecGZdPc38CGdvaKyeAh2zo1nYls= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804980711520.9574041215426; Wed, 23 Aug 2023 08:36:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpu5-0004VZ-Hg; Wed, 23 Aug 2023 11:36:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsY-0001xS-DL for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsW-000822-Ur for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:42 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-275-EftSaAWGO7ueTBRapkvIIw-1; Wed, 23 Aug 2023 11:34:34 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E690928040A7; Wed, 23 Aug 2023 15:34:33 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F1FA1121314; Wed, 23 Aug 2023 15:34:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TwJjkgHsvXEBoKNEpyqvsbIlhmZVjsh0HgCHFDNzu50=; b=O+koEOwqsAnHLlL33cIOnTFYfc/8NyZFc7e3JI1RtKL8ExDzP7YzxB2Xd+EJP2V3T0+xmE sqSsjksigYLlrorx3HTnT4yleyrM5flC/TWnTLWgYYCehvsWakun/tGAV9UG3UMrP1+IrJ kESNMH642f89L3NW030Ci4R1epjD2Tk= X-MC-Unique: EftSaAWGO7ueTBRapkvIIw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 06/11] softmmu/physmem: Fail creation of new files in file_ram_open() with readonly=true Date: Wed, 23 Aug 2023 17:34:06 +0200 Message-ID: <20230823153412.832081-7-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804981473100001 Content-Type: text/plain; charset="utf-8" Currently, if a file does not exist yet, file_ram_open() will create new empty file and open it writable. However, it even does that when readonly=3Dtrue was specified. Specifying O_RDONLY instead to create a new readonly file would theoretically work, however, ftruncate() will refuse to resize the new empty file and we'll get a warning: ftruncate: Invalid argument And later eventually more problems when actually mmap'ing that file and accessing it. If someone intends to let QEMU open+mmap a file read-only, better create+resize+fill that file ahead of time outside of QEMU context. We'll now fail with: ./qemu-system-x86_64 \ -object memory-backend-file,id=3Dram0,mem-path=3Dtmp,readonly=3Dtrue,si= ze=3D1g qemu-system-x86_64: can't open backing store tmp for guest RAM: No such fil= e or directory All use cases of readonly files (R/O NVDIMMs, VM templating) work on existing files, so silently creating new files might just hide user errors when accidentally specifying a non-existent file. Note that the only memory-backend-file will end up calling memory_region_init_ram_from_file() -> qemu_ram_alloc_from_file() -> file_ram_open(). Move error reporting to the single caller. Signed-off-by: David Hildenbrand Acked-by: Peter Xu --- softmmu/physmem.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 32d641754b..f8c47ecb85 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1289,8 +1289,7 @@ static int64_t get_file_align(int fd) static int file_ram_open(const char *path, const char *region_name, bool readonly, - bool *created, - Error **errp) + bool *created) { char *filename; char *sanitized_name; @@ -1305,6 +1304,10 @@ static int file_ram_open(const char *path, break; } if (errno =3D=3D ENOENT) { + if (readonly) { + /* Refuse to create new, readonly files. */ + return -ENOENT; + } /* @path names a file that doesn't exist, create it */ fd =3D open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd >=3D 0) { @@ -1334,10 +1337,7 @@ static int file_ram_open(const char *path, g_free(filename); } if (errno !=3D EEXIST && errno !=3D EINTR) { - error_setg_errno(errp, errno, - "can't open backing store %s for guest RAM", - path); - return -1; + return -errno; } /* * Try again on EINTR and EEXIST. The latter happens when @@ -1947,8 +1947,10 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, = MemoryRegion *mr, RAMBlock *block; =20 fd =3D file_ram_open(mem_path, memory_region_name(mr), - !!(ram_flags & RAM_READONLY_FD), &created, errp); + !!(ram_flags & RAM_READONLY_FD), &created); if (fd < 0) { + error_setg_errno(errp, -fd, "can't open backing store %s for guest= RAM", + mem_path); return NULL; } =20 --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692805021; cv=none; d=zohomail.com; s=zohoarc; b=a1PFFcWkkodf4bBsnQL8IqsHtbLZbJ56jyuYw6/i2eFVoQqiI75IXFMVo5JJ7zjNGggryDL55YGpIEpNjPJhG6hqsHWar1Xzms/2yquwA2Mi702HQzbui3jAkl2/4mcu/dulhhMe+ng8IT+WRcpMiwFdli+S+KkjO/yHFMtS+Hk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692805021; h=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=RkWOEfnMqqBybIdmhHLeXWKbx/ZYEUVcYQ9Om/+ASEo=; b=cMEIfykLtPZ3GC/fDzNpr03Ij0aapECMymkhouUvRhQONuBfiF2m/dM2TR7LQocTPwFpychVFYsYnEavjpx9ULKaHLFVjj3ulu+JMgQX3BQ2/gA8+vaySJi+guk6+dEPaC8PdVHggQJ8/hDlvvKpBX4/WMAXtgihWLqWWHj+snY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169280502194628.7403998044; Wed, 23 Aug 2023 08:37:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpt1-0002KA-Vh; Wed, 23 Aug 2023 11:35:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsa-0001yS-M1 for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsZ-00085g-6q for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:44 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-671-mP2aubXKMKGfOsUA38CAGg-1; Wed, 23 Aug 2023 11:34:37 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED98A85CBE0; Wed, 23 Aug 2023 15:34:36 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 353CA1121315; Wed, 23 Aug 2023 15:34:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804882; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RkWOEfnMqqBybIdmhHLeXWKbx/ZYEUVcYQ9Om/+ASEo=; b=fTXtJNbpOOYC/yqt9Nd4B4Htqr0SJ+MnlSaqjki7zo1IaPiib+fSevxHi2B2rauE9nTuXF 3VgVE38BvqrlABkbg62axiuCipnPpQQYoQwpWzOe9MAlo4YndDZKeCltEf1dpbaAoC1qmk xCivKXtq+4bK+RUsaLN0ymyc2dCQ2ug= X-MC-Unique: mP2aubXKMKGfOsUA38CAGg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 07/11] softmmu/physmem: Never return directories from file_ram_open() Date: Wed, 23 Aug 2023 17:34:07 +0200 Message-ID: <20230823153412.832081-8-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692805023109100001 Content-Type: text/plain; charset="utf-8" open() does not fail on directories when opening them readonly (O_RDONLY). Currently, we succeed opening such directories and fail later during mmap(), resulting in a misleading error message. $ ./qemu-system-x86_64 \ -object memory-backend-file,id=3Dram0,mem-path=3Dtmp,readonly=3Dtrue,si= ze=3D1g qemu-system-x86_64: unable to map backing store for guest RAM: No such dev= ice To identify directories and handle them accordingly in file_ram_open() also when readonly=3Dtrue was specified, detect if we just opened a directo= ry using fstat() instead. Then, fail file_ram_open() right away, similarly to how we now fail if the file does not exist and we want to open the file readonly. With this change, we get a nicer error message: qemu-system-x86_64: can't open backing store tmp for guest RAM: Is a direc= tory Note that the only memory-backend-file will end up calling memory_region_init_ram_from_file() -> qemu_ram_alloc_from_file() -> file_ram_open(). Reported-by: Thiner Logoer Signed-off-by: David Hildenbrand Reviewed-by: Peter Xu --- softmmu/physmem.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index f8c47ecb85..2d903e479b 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1300,6 +1300,25 @@ static int file_ram_open(const char *path, for (;;) { fd =3D open(path, readonly ? O_RDONLY : O_RDWR); if (fd >=3D 0) { + /* + * open(O_RDONLY) won't fail with EISDIR. Check manually if we + * opened a directory and fail similarly to how we fail ENOENT + * in readonly mode. Note that mkstemp() would imply O_RDWR. + */ + if (readonly) { + struct stat file_stat; + + if (fstat(fd, &file_stat)) { + close(fd); + if (errno =3D=3D EINTR) { + continue; + } + return -errno; + } else if (S_ISDIR(file_stat.st_mode)) { + close(fd); + return -EISDIR; + } + } /* @path names an existing file, use it */ break; } --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692805060; cv=none; d=zohomail.com; s=zohoarc; b=KlKrgSnXfJYqWqwxeskCrwghQf6uv0lJzon8+SqHhHcRtfS8ar032Ne6IlNdJtVnvTW06ZEkh2C0+RCRPfD/ayTAnGEXyHtGjGKjlJPOY5eIioTwSVXnaLmDoKglAb+o4lkZ8AKxQgUw71MhRJe6X0h+p28740b7C1+eNoxdKlM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692805060; h=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=xjl+jwf6S6C7TTZrfneBrUP7JU1jXf7dtndXiCRb7qo=; b=DXWf+3GntXe/JScaVLR7gsVSGtDJY2SQGiI4tAI26S1DzbQxZbSM/NJmXALSFiEA+JOXaQ+Qv/MZHlKcZpoy0ZzKEwSoYqeqZ1FRUbC7+5bGsOGRj2d7z8MZSfKhEOR43wM10yymPJksastQwN+xXkSoSbCMmrkitLB8TqwNMIs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692805060876786.8438992532577; Wed, 23 Aug 2023 08:37:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpu6-0004ci-Aq; Wed, 23 Aug 2023 11:36:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsd-0001yk-8R for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsb-0008AP-T4 for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:47 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-30-CQi_TBjFN4C86HPKSgS4dw-1; Wed, 23 Aug 2023 11:34:41 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 43FAC8D40AA; Wed, 23 Aug 2023 15:34:40 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 85E391121315; Wed, 23 Aug 2023 15:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xjl+jwf6S6C7TTZrfneBrUP7JU1jXf7dtndXiCRb7qo=; b=Cx3V5TgdAcGUPM1P2soV85A3BxnpT3Dgx2MfEqVE6XmIq/3u6TN7Uakmj/QKAQpmFzfeKr id45lgXlGxEK3b1bznYHcWNivF6sWlD9nIBenKQHgRgiou6RaDgRl1dL4M14hWm9C1YFDB tGuoyVoswxyIGyVmiQkfCDowluyqJ4w= X-MC-Unique: CQi_TBjFN4C86HPKSgS4dw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 08/11] docs: Don't mention "-mem-path" in multi-process.rst Date: Wed, 23 Aug 2023 17:34:08 +0200 Message-ID: <20230823153412.832081-9-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692805062872100003 Content-Type: text/plain; charset="utf-8" "-mem-path" corresponds to "memory-backend-file,share=3Doff" and, therefore, creates a private COW mapping of the file. For multi-proces QEMU, we need proper shared file-backed memory. Let's make that clearer. Signed-off-by: David Hildenbrand --- docs/devel/multi-process.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/devel/multi-process.rst b/docs/devel/multi-process.rst index e4801751f2..4ef539c0b0 100644 --- a/docs/devel/multi-process.rst +++ b/docs/devel/multi-process.rst @@ -409,8 +409,9 @@ the initial messages sent to the emulation process is a= guest memory table. Each entry in this table consists of a file descriptor and size that the emulation process can ``mmap()`` to directly access guest memory, similar to ``vhost_user_set_mem_table()``. Note guest memory -must be backed by file descriptors, such as when QEMU is given the -*-mem-path* command line option. +must be backed by shared file-backed memory, for example, using +*-object memory-backend-file,share=3Don* and setting that memory backend +as RAM for the machine. =20 IOMMU operations ^^^^^^^^^^^^^^^^ --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692805068; cv=none; d=zohomail.com; s=zohoarc; b=ksZavitq0nQokNfa4mqoTLdISi4JHgIu0sEsJjSHGYFhzrq/fTpbtFCdVUV3drwH66xkR2ov8sMywM3JhYUmfq3kbOhzb12UTRwkUhreNF9JN/yMjCoZI/+0yfO8JfB1+rjCzmbiiQCj2Tk0EGi9gf8AJ6uv5MWIeLGKj5kLvxo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692805068; h=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=yRwjrEQkp0ocMUr/NIyYCppb9dEB4o9Kris2ocNIc0c=; b=AW7d+JFNBkfGUelftd5251wU2mV4PSXN0chxr5BFA/3o+K/jfIi5RkgxF+B4fVEN2d/DoJZDHZYGOMfwIueIt30gPA7zwuI2daHhLDkr26YXT+HJzfVV2YguNx+syfw15mA2we51zTJn136ZoTruThUW9ezYAnl5javhDb9T/PI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692805068260596.7090520765325; Wed, 23 Aug 2023 08:37:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYptA-0002fl-Q1; Wed, 23 Aug 2023 11:35:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsf-0001yo-NW for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsd-0008Ad-AC for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:48 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-264-835CFNDpOQe6yOntVye4Tw-1; Wed, 23 Aug 2023 11:34:44 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4219D1C09A47; Wed, 23 Aug 2023 15:34:43 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 801231121315; Wed, 23 Aug 2023 15:34:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804886; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yRwjrEQkp0ocMUr/NIyYCppb9dEB4o9Kris2ocNIc0c=; b=DtF2Il9Zsv1qFj2IeghOnGw7AD4CUSAnedqNqyeqzPc34m9mklzzOAWfzUmv7kunYHGT59 BhxQcSMjMabaP9PZ2YcH59So6Scy5hc42MCtamEDQPMrF4VxQu2v526BLj4D7OIHB2eJrI QhJcFDN4MrZX2BWEY/S6H/f4AD9EyCo= X-MC-Unique: 835CFNDpOQe6yOntVye4Tw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 09/11] docs: Start documenting VM templating Date: Wed, 23 Aug 2023 17:34:09 +0200 Message-ID: <20230823153412.832081-10-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692805069966100007 Content-Type: text/plain; charset="utf-8" Let's add some details about VM templating, focusing on the VM memory configuration only. There is much more to VM templating (VM state? block devices?), but I leave that as future work. Signed-off-by: David Hildenbrand Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- MAINTAINERS | 1 + docs/system/index.rst | 1 + docs/system/vm-templating.rst | 125 ++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 docs/system/vm-templating.rst diff --git a/MAINTAINERS b/MAINTAINERS index 6111b6b4d9..096acb71a5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2954,6 +2954,7 @@ M: Igor Mammedov S: Maintained F: backends/hostmem*.c F: include/sysemu/hostmem.h +F: docs/system/vm-templating.rst T: git https://gitlab.com/ehabkost/qemu.git machine-next =20 Cryptodev Backends diff --git a/docs/system/index.rst b/docs/system/index.rst index 3605bbe1ce..28abcf2b17 100644 --- a/docs/system/index.rst +++ b/docs/system/index.rst @@ -38,3 +38,4 @@ or Hypervisor.Framework. security multi-process confidential-guest-support + vm-templating diff --git a/docs/system/vm-templating.rst b/docs/system/vm-templating.rst new file mode 100644 index 0000000000..28905a1eeb --- /dev/null +++ b/docs/system/vm-templating.rst @@ -0,0 +1,125 @@ +QEMU VM templating +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +This document explains how to use VM templating in QEMU. + +For now, the focus is on VM memory aspects, and not about how to save and +restore other VM state (i.e., migrate-to-file with ``x-ignore-shared``). + +Overview +-------- + +With VM templating, a single template VM serves as the starting point for +new VMs. This allows for fast and efficient replication of VMs, resulting +in fast startup times and reduced memory consumption. + +Conceptually, the VM state is frozen, to then be used as a basis for new +VMs. The Copy-On-Write mechanism in the operating systems makes sure that +new VMs are able to read template VM memory; however, any modifications +stay private and don't modify the original template VM or any other +created VM. + +!!! Security Alert !!! +---------------------- + +When effectively cloning VMs by VM templating, hardware identifiers +(such as UUIDs and NIC MAC addresses), and similar data in the guest OS +(such as machine IDs, SSH keys, certificates) that are supposed to be +*unique* are no longer unique, which can be a security concern. + +Please be aware of these implications and how to mitigate them for your +use case, which might involve vmgenid, hot(un)plug of NIC, etc.. + +Memory configuration +-------------------- + +In order to create the template VM, we have to make sure that VM memory +ends up in a file, from where it can be reused for the new VMs: + +Supply VM RAM via memory-backend-file, with ``share=3Don`` (modifications = go +to the file) and ``readonly=3Doff`` (open the file writable). Note that +``readonly=3Doff`` is implicit. + +In the following command-line example, a 2GB VM is created, whereby VM RAM +is to be stored in the ``template`` file. + +.. parsed-literal:: + + |qemu_system| [...] -m 2g \\ + -object memory-backend-file,id=3Dpc.ram,mem-path=3Dtemplate,size= =3D2g,share=3Don,... \\ + -machine q35,memory-backend=3Dpc.ram + +If multiple memory backends are used (vNUMA, DIMMs), configure all +memory backends accordingly. + +Once the VM is in the desired state, stop the VM and save other VM state, +leaving the current state of VM RAM reside in the file. + +In order to have a new VM be based on a template VM, we have to +configure VM RAM to be based on a template VM RAM file; however, the VM +should not be able to modify file content. + +Supply VM RAM via memory-backend-file, with ``share=3Doff`` (modifications +stay private), ``readonly=3Don`` (open the file readonly) and ``rom=3Doff`` +(don't make the memory readonly for the VM). Note that ``share=3Doff`` is +implicit and that other VM state has to be restored separately. + +In the following command-line example, a 2GB VM is created based on the +existing 2GB file ``template``. + +.. parsed-literal:: + + |qemu_system| [...] -m 2g \\ + -object memory-backend-file,id=3Dpc.ram,mem-path=3Dtemplate,size= =3D2g,readonly=3Don,rom=3Doff,... \\ + -machine q35,memory-backend=3Dpc.ram + +If multiple memory backends are used (vNUMA, DIMMs), configure all +memory backends accordingly. + +Note that ``-mem-path`` cannot be used for VM templating when creating the +template VM or when starting new VMs based on a template VM. + +Incompatible features +--------------------- + +Some features are incompatible with VM templating, as the underlying file +cannot be modified to discard VM RAM, or to actually share memory with +another process. + +vhost-user and multi-process QEMU +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +vhost-user and multi-process QEMU are incompatible with VM templating. +These technologies rely on shared memory, however, the template VMs +don't actually share memory (``share=3Doff``), even though they are +file-based. + +virtio-balloon +~~~~~~~~~~~~~~ + +virtio-balloon inflation and "free page reporting" cannot discard VM RAM +and will repeatedly report errors. While virtio-balloon can be used +for template VMs (e.g., report VM RAM stats), "free page reporting" +should be disabled and the balloon should not be inflated. + +virtio-mem +~~~~~~~~~~ + +virtio-mem cannot discard VM RAM that is managed by the virtio-mem +device. virtio-mem will fail early when realizing the device. To use +VM templating with virtio-mem, either hotplug virtio-mem devices to the +new VM, or don't supply any memory to the template VM using virtio-mem +(requested-size=3D0), not using a template VM file as memory backend for t= he +virtio-mem device. + +VM migration +~~~~~~~~~~~~ + +For VM migration, "x-release-ram" similarly relies on discarding of VM +RAM on the migration source to free up migrated RAM, and will +repeatedly report errors. + +Postcopy live migration fails discarding VM RAM on the migration +destination early and refuses to activate postcopy live migration. Note +that postcopy live migration usually only works on selected filesystems +(shmem/tmpfs, hugetlbfs) either way. --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804936049427.7206230323319; Wed, 23 Aug 2023 08:35:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYpt9-0002Yz-0p; Wed, 23 Aug 2023 11:35:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsk-00021X-6h for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsi-0008GA-LT for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:53 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-QNYVl9zUNkOlRsLUs_nQRQ-1; Wed, 23 Aug 2023 11:34:46 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3BCE2185A792; Wed, 23 Aug 2023 15:34:46 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B94A1121314; Wed, 23 Aug 2023 15:34:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804892; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xs5XMyhe7YjtiDAbU2Nk1FNiu4gls1rpQ8zCGsiAoY4=; b=Jkqo8x622UmaY1GPXSlZE+ATbkK4Bh5fJ5d9ILSsm8mWbf5dOfHuy+3nFOE6GaMtTFZJPp lEbgpUdaa/A5Wv0QOLpRx21eNEUYJis15NA4fWJ2kVtAH/+JKCsMA2I9/I6HiKMf/wneNs gLYjGHSR8zvgSjL1rq8qog2184aUZ/U= X-MC-Unique: QNYVl9zUNkOlRsLUs_nQRQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 10/11] softmmu/physmem: Hint that "readonly=on, rom=off" exists when opening file R/W for private mapping fails Date: Wed, 23 Aug 2023 17:34:10 +0200 Message-ID: <20230823153412.832081-11-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1692804938327100003 Content-Type: text/plain; charset="utf-8" It's easy to miss that memory-backend-file with "share=3Doff" (default) will always try opening the file R/W as default, and fail if we don't have write permissions to the file. In that case, the user has to explicit specify "readonly=3Don,rom=3Doff" to get usable RAM, for example, for VM templating. Let's hint that '-object memory-backend-file,readonly=3Don,rom=3Doff,...' exists to consume R/O files in a private mapping to create writable RAM, but only if we have permissions to open the file read-only. Suggested-by: ThinerLogoer Signed-off-by: David Hildenbrand --- softmmu/physmem.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 2d903e479b..d812276f41 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1970,6 +1970,25 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, = MemoryRegion *mr, if (fd < 0) { error_setg_errno(errp, -fd, "can't open backing store %s for guest= RAM", mem_path); + if (!(ram_flags & RAM_READONLY_FD) && !(ram_flags & RAM_SHARED) && + fd =3D=3D -EACCES) { + /* + * If we can open the file R/O (note: will never create a new = file) + * and we are dealing with a private mapping, there are still = ways + * to consume such files and get RAM instead of ROM. + */ + fd =3D file_ram_open(mem_path, memory_region_name(mr), true, + &created); + if (fd < 0) { + return NULL; + } + assert(!created); + close(fd); + error_append_hint(errp, "Consider opening the backing store" + " read-only but still creating writable RAM using" + " '-object memory-backend-file,readonly=3Don,rom=3Doff...'" + " (see \"VM templating\" documentation)\n"); + } return NULL; } =20 --=20 2.41.0 From nobody Thu Nov 28 08:33:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692804938; cv=none; d=zohomail.com; s=zohoarc; b=dayNyE20Yio4wzifzwGDWV/ypAusYS68Rq5KrAL80N/+qgh9Tyvrj1tc3FvMDclVspCTT+EqcOpADUJDcqp6D62kfD0ABucDEi0bZyhYS4tDB+UrP4QiS+fWyKNXW7M5ZMnmu1pxwad2ouvi9Qy8hLrqfjCglk9EqI9kmmdQuao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692804938; h=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=d1oQ+Qnb6PlcyCnKVT0QTOqM0Fb1NeLs1SpKNWY24OU=; b=Enr5m5hX/mmKxsZoCxmQndwldTgO6DMMA/sVOZ2Uv1sVguPk1QiUGuASX6AmUnngrkS9C5hD2M9ImVrIthBV5WJ4zfWqpmSwiUEPESPryga8ipQrQqgl22MpSAHQjmVwfXAqIzLZOPOWEW4hVn6XXR9lEWs0yvdHEeGxPNbFWSU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692804938186906.302282904764; Wed, 23 Aug 2023 08:35:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYptA-0002eW-6g; Wed, 23 Aug 2023 11:35:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsl-00022i-8j for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYpsj-0008GF-Bx for qemu-devel@nongnu.org; Wed, 23 Aug 2023 11:34:55 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-8-tX_XBjv1MyiXgUak00_qJA-1; Wed, 23 Aug 2023 11:34:50 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A1848D40A1; Wed, 23 Aug 2023 15:34:49 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.128]) by smtp.corp.redhat.com (Postfix) with ESMTP id 783171121314; Wed, 23 Aug 2023 15:34:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692804892; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d1oQ+Qnb6PlcyCnKVT0QTOqM0Fb1NeLs1SpKNWY24OU=; b=CTsqh/0FQl2+YO7rkUvYBHtkK/4w3HjeKGH4LfxKvs93Byfh5hyQpnsfLncdJL/9yZt+GF z9PyTrgmEZI7q+9NxV3So2kZQibTAMyVo882Z5usmPZmc44yuLuZXkP+viILOXHAIoHMQ/ v0cVBZ67AHYuFB1OCUFsRSAEmnMH7gg= X-MC-Unique: tX_XBjv1MyiXgUak00_qJA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: qemu-ppc@nongnu.org, David Hildenbrand , Paolo Bonzini , Peter Xu , Igor Mammedov , Thiner Logoer , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefan Hajnoczi , Elena Ufimtseva , Jagannathan Raman , "Michael S. Tsirkin" , Ani Sinha , Xiao Guangrong , Daniel Henrique Barboza , Greg Kurz , Eric Blake , Markus Armbruster , Eduardo Habkost Subject: [PATCH v3 11/11] machine: Improve error message when using default RAM backend id Date: Wed, 23 Aug 2023 17:34:11 +0200 Message-ID: <20230823153412.832081-12-david@redhat.com> In-Reply-To: <20230823153412.832081-1-david@redhat.com> References: <20230823153412.832081-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692804940333100009 Content-Type: text/plain; charset="utf-8" For migration purposes, users might want to reuse the default RAM backend id, but specify a different memory backend. For example, to reuse "pc.ram" on q35, one has to set -machine q35,memory-backend=3Dpc.ram Only then, can a memory backend with the id "pc.ram" be created manually. Let's improve the error message. Unfortuantely, we cannot use error_append_hint(), because the caller passes &error_fatal. Suggested-by: ThinerLogoer Signed-off-by: David Hildenbrand Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Mario Casquero --- hw/core/machine.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hw/core/machine.c b/hw/core/machine.c index f0d35c6401..dbcd124d45 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -1382,7 +1382,9 @@ void machine_run_board_init(MachineState *machine, co= nst char *mem_path, Error * machine_class->default_ram_id)) { error_setg(errp, "object name '%s' is reserved for the default" " RAM backend, it can't be used for any other purposes." - " Change the object's 'id' to something else", + " Change the object's 'id' to something else or disable" + " automatic creation of the default RAM backend by setting" + " the 'memory-backend' machine property", machine_class->default_ram_id); return; } --=20 2.41.0