From nobody Wed Nov 19 03:02:45 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614294179; cv=none; d=zohomail.com; s=zohoarc; b=gTQD5JA1lo/Iw4D7Oi1OFpwSFzpwkyRXFjEtYpFgqQoPZhV6HqIjJhBHvGdwPL0RMVCMDc+7pGRp4m00ake7K28f9RgqgUUPNRX30qeFjRjkdUYq1fa1/5Uqqv41VdiONnGKmoG1DxifcJnKIGUHI0oaLKPxdN73oT/mv5RgG/0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614294179; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=z3QPbF3kCYrwGbuoCgT50QmHdrj0vIiledduYlpbOmo=; b=UD51w3vKMbEPenOxTIj6kF2eloUzaOm9pBCd+VXn8wym51QH50AImUU7swD4KnAGAnsik0aXoFMfh/zQnO7YBppHTvAusSPH+/RWDxNj3HFRsTjRxkxJvlFC0STqC1j/2PBzy+DQ7sBngTDd2+vdTVWGtS1WIRZcLNgYsRCNRtg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1614294179082781.0129431507228; Thu, 25 Feb 2021 15:02:59 -0800 (PST) Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-517-gN6YyddQNlGvhPGFwIa21A-1; Thu, 25 Feb 2021 18:02:54 -0500 Received: by mail-ed1-f69.google.com with SMTP id g20so3599164edr.6 for ; Thu, 25 Feb 2021 15:02:49 -0800 (PST) Return-Path: Return-Path: Received: from x1w.redhat.com (68.red-83-57-175.dynamicip.rima-tde.net. [83.57.175.68]) by smtp.gmail.com with ESMTPSA id e19sm4406587eds.10.2021.02.25.15.02.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Feb 2021 15:02:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614294178; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z3QPbF3kCYrwGbuoCgT50QmHdrj0vIiledduYlpbOmo=; b=eja7+rOcTjdIYluqMM5nVoat+s3L9JYplH7lqKrMfsxpgmP5KKl4bEaq5SFioxmipr1xQM 0Y42vA5S85S/RaqoHTMXl/K+4v83LyUhNYDXFkp0rYFxQjym3f5B2UlSE3kWmlABZF4F5L Dae40Rx1O01WurJ/wrW5YR8odk5nAUg= X-MC-Unique: gN6YyddQNlGvhPGFwIa21A-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z3QPbF3kCYrwGbuoCgT50QmHdrj0vIiledduYlpbOmo=; b=mAd9Lch1pmk75H25uSU3B8TLwyg0Ge1bp5yWAafa6OVQ45Hy4ncFaAZmOj3O2wqH3L 5U96g/jsK/pLAzCxOnkBvpTiO5NkxLGLStLDj4eZ0LZ0lhfvxFSvl5HmjuP+BBjWMOj7 O3D41k2Ln2ZW7GrZqgYZLaBpq/hFlYVAhrgeQ2KV1/5OwtJDycVT0WZyduYEfI9F9axt 8uyPiYHcQVx07UltDPbBBCM19Zwd/XL3bWdrs3jX9+oRJrjyiPMhVoxeNCv3paEfkWf7 AFXkFa3JLDxlzFfDpx6nxTagFxNaUHK4k+M+Opb1mrzq4B1KGxKQLfKesDdHzF99XnWB 3LxQ== X-Gm-Message-State: AOAM530sgoH9QdnDmRgZr/LlRvJVzAkXtXqBh2CNK1OM/s9h5/ibfybG RFXycnlNEscGKUH96igi5H21iluyiOGkKNS7cz/fpRFx6qp62r4wMgXSdxku1gXDf7me6hdKsm9 8FxLSL8KyKGNn0g== X-Received: by 2002:a17:907:3daa:: with SMTP id he42mr5338755ejc.54.1614294167936; Thu, 25 Feb 2021 15:02:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJw2IeXqZTg4FOpfgBgMVesupcGf6lAj92RLhzi3TSZy0Si3tweytKeJHqVZ4TuPSXI8pxJGeg== X-Received: by 2002:a17:907:3daa:: with SMTP id he42mr5338728ejc.54.1614294167746; Thu, 25 Feb 2021 15:02:47 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , haibinzhang , Zheng Xiang , qemu-block@nongnu.org, Paolo Bonzini , Xu Yandong , Kevin Wolf , Stefano Garzarella , Max Reitz Subject: [RFC PATCH 1/3] exec/memory: Introduce memory_region_init_rom_device_from_file() Date: Fri, 26 Feb 2021 00:02:36 +0100 Message-Id: <20210225230238.3719051-2-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210225230238.3719051-1-philmd@redhat.com> References: <20210225230238.3719051-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Introduce memory_region_init_rom_device_from_file() which mmap the backing file of ROM devices. This allows to reduce QEMU memory footprint as the same file can be shared between multiple instances of QEMU. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- include/exec/memory.h | 85 +++++++++++++++++++++++++++++++++++++ softmmu/memory.c | 98 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index c6fb714e499..bacf7495003 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -487,6 +487,9 @@ struct MemoryRegion { const char *name; unsigned ioeventfd_nb; MemoryRegionIoeventfd *ioeventfds; +#ifndef CONFIG_POSIX + gchar *contents; +#endif }; =20 struct IOMMUMemoryRegion { @@ -1131,6 +1134,43 @@ void memory_region_init_rom_device_nomigrate(MemoryR= egion *mr, uint64_t size, Error **errp); =20 +/** + * memory_region_init_rom_device_from_file_nomigrate: + * Initialize a ROM memory region from the specified backing file. + * Writes are handled via callbacks. + * + * Note that this function does not do anything to cause the data in the + * RAM side of the memory region to be migrated; that is the responsibility + * of the caller. + * + * @mr: the #MemoryRegion to be initialized. + * @owner: the object that tracks the region's reference count + * @ops: callbacks for write access handling (must not be NULL). + * @opaque: passed to the read and write callbacks of the @ops structure. + * @name: Region name, becomes part of RAMBlock name used in migration str= eam + * must be unique within any device + * @size: size of the region. + * @ram_flags: specify the properties of the ram block, which can be one + * or bit-or of following values + * - RAM_SHARED: mmap the backing file or device with MAP_SHAR= ED + * - RAM_PMEM: the backend @mem_path is persistent memory + * Other bits are ignored. + * @path: specify the backing file + * @readonly: true to open @path for reading, false for read/write. + * @errp: pointer to Error*, to store an error if it happens. + */ +void memory_region_init_rom_device_from_file_nomigrate(MemoryRegion *mr, + Object *owner, + const MemoryRegionO= ps *ops, + void *opaque, + const char *name, + uint64_t size, + uint64_t align, + uint32_t ram_flags, + const char *path, + bool readonly, + Error **errp); + /** * memory_region_init_iommu: Initialize a memory region of a custom type * that translates addresses @@ -1249,6 +1289,51 @@ void memory_region_init_rom_device(MemoryRegion *mr, Error **errp); =20 =20 +/** + * memory_region_init_rom_device_from_file: + * Initialize a ROM memory region from the specified backing file. + * Writes are handled via callbacks. + * + * This function initializes a memory region backed by RAM for reads + * and callbacks for writes, and arranges for the RAM backing to + * be migrated (by calling vmstate_register_ram() + * if @owner is a DeviceState, or vmstate_register_ram_global() if + * @owner is NULL). + * + * TODO: Currently we restrict @owner to being either NULL (for + * global RAM regions with no owner) or devices, so that we can + * give the RAM block a unique name for migration purposes. + * We should lift this restriction and allow arbitrary Objects. + * If you pass a non-NULL non-device @owner then we will assert. + * + * @mr: the #MemoryRegion to be initialized. + * @owner: the object that tracks the region's reference count + * @ops: callbacks for write access handling (must not be NULL). + * @opaque: passed to the read and write callbacks of the @ops structure. + * @name: Region name, becomes part of RAMBlock name used in migration str= eam + * must be unique within any device + * @size: size of the region. + * @ram_flags: specify the properties of the ram block, which can be one + * or bit-or of following values + * - RAM_SHARED: mmap the backing file or device with MAP_SHAR= ED + * - RAM_PMEM: the backend @mem_path is persistent memory + * Other bits are ignored. + * @path: specify the backing file + * @readonly: true to open @path for reading, false for read/write. + * @errp: pointer to Error*, to store an error if it happens. + */ +void memory_region_init_rom_device_from_file(MemoryRegion *mr, + Object *owner, + const MemoryRegionOps *ops, + void *opaque, + const char *name, + uint64_t size, + uint64_t align, + uint32_t ram_flags, + const char *path, + bool readonly, + Error **errp); + /** * memory_region_owner: get a memory region's owner. * diff --git a/softmmu/memory.c b/softmmu/memory.c index 874a8fccdee..ea1892a8cd6 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1120,6 +1120,14 @@ static void memory_region_destructor_ram(MemoryRegio= n *mr) qemu_ram_free(mr->ram_block); } =20 +#ifndef CONFIG_POSIX +static void memory_region_destructor_contents(MemoryRegion *mr) +{ + qemu_ram_free(mr->ram_block); + g_free(mr->contents); +} +#endif + static bool memory_region_need_escape(char c) { return c =3D=3D '/' || c =3D=3D '[' || c =3D=3D '\\' || c =3D=3D ']'; @@ -1712,6 +1720,96 @@ void memory_region_init_rom_device_nomigrate(MemoryR= egion *mr, } } =20 +void memory_region_init_rom_device_from_file_nomigrate(MemoryRegion *mr, + Object *owner, + const MemoryRegionO= ps *ops, + void *opaque, + const char *name, + uint64_t size, + uint64_t align, + uint32_t ram_flags, + const char *path, + bool readonly, + Error **errp) +{ + Error *err =3D NULL; + + assert(ops); +#ifdef CONFIG_POSIX + memory_region_init(mr, owner, name, size); + mr->opaque =3D opaque; + mr->ops =3D ops; + mr->rom_device =3D true; + mr->readonly =3D readonly; + mr->ram =3D true; + mr->align =3D align; + mr->terminates =3D true; + mr->destructor =3D memory_region_destructor_ram; + mr->ram_block =3D qemu_ram_alloc_from_file(size, mr, ram_flags, path, + readonly, &err); + if (err) { + mr->size =3D int128_zero(); + object_unparent(OBJECT(mr)); + error_propagate(errp, err); + } +#else + g_autoptr(GError) gerr =3D NULL; + gsize len; + + memory_region_init(mr, owner, name, size); + mr->ops =3D ops; + mr->opaque =3D opaque; + mr->terminates =3D true; + mr->rom_device =3D true; + + if (!g_file_get_contents(path, &mr->contents, &len, &gerr)) { + error_setg(errp, "Unable to read '%s': %s", path, gerr->message); + return; + } + mr->destructor =3D memory_region_destructor_contents; + mr->contents =3D g_realloc(mr->contents, size); + mr->ram_block =3D qemu_ram_alloc_from_ptr(size, mr->contents, mr, &err= ); + if (err) { + mr->size =3D int128_zero(); + object_unparent(OBJECT(mr)); + error_propagate(errp, err); + } +#endif +} + +void memory_region_init_rom_device_from_file(MemoryRegion *mr, + Object *owner, + const MemoryRegionOps *ops, + void *opaque, + const char *name, + uint64_t size, + uint64_t align, + uint32_t ram_flags, + const char *path, + bool readonly, + Error **errp) +{ + DeviceState *owner_dev; + Error *err =3D NULL; + + memory_region_init_rom_device_from_file_nomigrate(mr, owner, ops, opaq= ue, + name, size, align, + ram_flags, path, rea= donly, + &err); + if (err) { + error_propagate(errp, err); + return; + } + /* This will assert if owner is neither NULL nor a DeviceState. + * We only want the owner here for the purposes of defining a + * unique name for migration. TODO: Ideally we should implement + * a naming scheme for Objects which are not DeviceStates, in + * which case we can relax this restriction. + */ + owner_dev =3D DEVICE(owner); + vmstate_register_ram(mr, owner_dev); +} + void memory_region_init_iommu(void *_iommu_mr, size_t instance_size, const char *mrtypename, --=20 2.26.2 From nobody Wed Nov 19 03:02:45 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614294179; cv=none; d=zohomail.com; s=zohoarc; b=dODCm93+TpTZj6/qgnjkoY//flBQ7L8FBoXWmFg6aSQjR8zseyQ922PJCqhaePt5wEGm8sDYBIOa2JsNZ6rOz39TMyP+imqfdaWZakglOtcnpJ0Dq9EjuW6vnq3s2240k4Ky7xHCGuB67u76eGNwDlJuTruSrgMmXvdb0zMO/YY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614294179; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=nJG0er+QNebAk4w+3LZsO8sLinAOdx+C5yOC5sTpKQE=; b=Ukv8snpxmkjT8UX0v62du0HoZUeLOCJDj+9mT4O3gTfrXut59JqiKLL9bkc0kVbCxSjSv8rV6IfYGUt4E1CK1kPETJcXBKjVwALZxfCS0OZ+IT3kEk5vLWuFjdngUNhJvWU5L8F1PQ7iU44IbujwxjpcoR5h+xNFoI2buTXSOAc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1614294179632569.7701836252145; Thu, 25 Feb 2021 15:02:59 -0800 (PST) Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-457-7ozMN4sbNZyZt5Z8KO7RXQ-1; Thu, 25 Feb 2021 18:02:54 -0500 Received: by mail-ed1-f72.google.com with SMTP id dk21so3599521edb.10 for ; Thu, 25 Feb 2021 15:02:54 -0800 (PST) Return-Path: Return-Path: Received: from x1w.redhat.com (68.red-83-57-175.dynamicip.rima-tde.net. [83.57.175.68]) by smtp.gmail.com with ESMTPSA id i13sm3906372ejj.2.2021.02.25.15.02.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Feb 2021 15:02:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614294178; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nJG0er+QNebAk4w+3LZsO8sLinAOdx+C5yOC5sTpKQE=; b=HUCq70hLfyLA7UVP2Mx3QxaojFxsPHSNmSrsnZAU/NAS7CIEjqy2VfxgF2D9AKqi318aSK /YGWNcoOMHUej/umvUZ8gmydIMqRIi+URqtKVTCQ4xmELuR9ZgcX8tIoAX4fZgjBJSviTp nKpUmP+Nfrn8u23fImwO1Gt0BrhD0uM= X-MC-Unique: 7ozMN4sbNZyZt5Z8KO7RXQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nJG0er+QNebAk4w+3LZsO8sLinAOdx+C5yOC5sTpKQE=; b=mM8nIm2lTlwd3+cLFCQ8fy0T6WKsgTKc87OHDFWxDG8eNWz7p+CCpUpoXvQlh5L99j 7+6z3cyLE8ChAEo+ZRsFkYCOUY+vszK/HzFGpvOG5WxZP4olJFNJ+SCceAFd0/PoUmqp T/f1IKdZX78FI/xUiYTv9EUceCi/oXaQjqTR/2U3z5O7S6MYEVuOxY9UPyHIh4ArTpqp SS5ZVCycX/gpscDzkz7YPMOx3ypXH41DFf80giOhkS+tdJ9vPsX71cma4uVp8W5bAgaC ooE1CjSHvjuil0lA2rTJ0sg6JjTZm+IQIPcLTiJCx1RBoWxt5wjZXcOUefkzGuk4P3lp gsTQ== X-Gm-Message-State: AOAM532EYAvLe6syebFw+4pZRxgEW8w7nXCQK7765WwM5kQn/cS/6l1g e88J7ahWgEBxCLF9jHrG9iCvLE1k6XoJ3QAxzTNxIjPRNk1X4qn6zWAaXVkZ2n1fx6YMtlQzAV7 NSTeKmeG5VIGH0Q== X-Received: by 2002:a17:906:3510:: with SMTP id r16mr5259865eja.186.1614294173433; Thu, 25 Feb 2021 15:02:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJwRF6AYpnGCVCg3wn6q8pITbe5nkeK/eQVx9U0JsXetVKsyh8lYFLsBBieZs9vqr6mtZ/H9Hw== X-Received: by 2002:a17:906:3510:: with SMTP id r16mr5259841eja.186.1614294173309; Thu, 25 Feb 2021 15:02:53 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , haibinzhang , Zheng Xiang , qemu-block@nongnu.org, Paolo Bonzini , Xu Yandong , Kevin Wolf , Stefano Garzarella , Max Reitz Subject: [RFC PATCH 2/3] hw/block/pflash: Move code around Date: Fri, 26 Feb 2021 00:02:37 +0100 Message-Id: <20210225230238.3719051-3-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210225230238.3719051-1-philmd@redhat.com> References: <20210225230238.3719051-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) First do the block checks, so we know if it is read-only or not. Then create the MemoryRegion. This will allow optimization in the next commit. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: David Edmondson --- hw/block/pflash_cfi01.c | 24 ++++++++++++------------ hw/block/pflash_cfi02.c | 18 +++++++++--------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 22287a1522e..a5fa8d8b74a 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -731,18 +731,6 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) } device_len =3D sector_len_per_device * blocks_per_device; =20 - memory_region_init_rom_device( - &pfl->mem, OBJECT(dev), - &pflash_cfi01_ops, - pfl, - pfl->name, total_len, errp); - if (*errp) { - return; - } - - pfl->storage =3D memory_region_get_ram_ptr(&pfl->mem); - sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); - if (pfl->blk) { uint64_t perm; pfl->ro =3D !blk_supports_write_perm(pfl->blk); @@ -755,6 +743,18 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) pfl->ro =3D 0; } =20 + memory_region_init_rom_device( + &pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, + pfl, + pfl->name, total_len, errp); + if (*errp) { + return; + } + + pfl->storage =3D memory_region_get_ram_ptr(&pfl->mem); + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); + if (pfl->blk) { if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len, errp)) { diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index 7962cff7455..4f62ce8917d 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -791,15 +791,6 @@ static void pflash_cfi02_realize(DeviceState *dev, Err= or **errp) return; } =20 - memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), - &pflash_cfi02_ops, pfl, pfl->name, - pfl->chip_len, errp); - if (*errp) { - return; - } - - pfl->storage =3D memory_region_get_ram_ptr(&pfl->orig_mem); - if (pfl->blk) { uint64_t perm; pfl->ro =3D !blk_supports_write_perm(pfl->blk); @@ -812,6 +803,15 @@ static void pflash_cfi02_realize(DeviceState *dev, Err= or **errp) pfl->ro =3D 0; } =20 + memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), + &pflash_cfi02_ops, pfl, pfl->name, + pfl->chip_len, errp); + if (*errp) { + return; + } + + pfl->storage =3D memory_region_get_ram_ptr(&pfl->orig_mem); + if (pfl->blk) { if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, pfl->chip_len, errp)) { --=20 2.26.2 From nobody Wed Nov 19 03:02:45 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614294184; cv=none; d=zohomail.com; s=zohoarc; b=QVEUh5QUyv3zJymBR8plMeGrZyP4c3+Y173Ad/9I6i+dI0NQNB6yMpaEaC/gW3tfI15vTTHHLMa5tavXP/tvJ8Dvtol5iheo4RHVQgAzAVCXAOgz5COcYReUP+GzN7WtXJT7psCwcE6uo6pUdz+R/FleLIK4g+4Y8TP+kOWypBA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614294184; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=//g/7OffRCofFHIb1cBxIBJjHhepgsuxtwRomhT/GoA=; b=mBH6He40H+pdfEYvsSIoJpiz7z4JX485c3sYz4DkF8qhPs98BHE+cGOqQ7HALdgt69jEdKfVJX54T+QkXKB57trXB3DI4RmeoCvrmode5YoiqMx7g4LiVktEgiTq4t60jFdNd8lJ1a5bq+PTyenIUBeE0MJ0rbpJr3oUsqDA0dU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1614294184491764.6425022361085; Thu, 25 Feb 2021 15:03:04 -0800 (PST) Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-392-xJWilGXeMlaAwN96e7ixkA-1; Thu, 25 Feb 2021 18:03:01 -0500 Received: by mail-ej1-f71.google.com with SMTP id p6so3258621ejw.1 for ; Thu, 25 Feb 2021 15:03:00 -0800 (PST) Return-Path: Return-Path: Received: from x1w.redhat.com (68.red-83-57-175.dynamicip.rima-tde.net. [83.57.175.68]) by smtp.gmail.com with ESMTPSA id t25sm4591954edt.41.2021.02.25.15.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Feb 2021 15:02:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614294183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=//g/7OffRCofFHIb1cBxIBJjHhepgsuxtwRomhT/GoA=; b=b+ueRqSmgpmg6lJCWCr7AtA6HD7W9Xk95c54oowMBDVx4WFoALpnfU7zMgq3R6M4A/bDQT lQ1jFqVBB8RNIMp9FTS2IdouRCcoW1JGpzC5xKnRMz7eArMIwCTAwohM4DFONb+07R5TjJ 9+mP4SQAkS4M9mDYVH+fz3FHvCCU3G4= X-MC-Unique: xJWilGXeMlaAwN96e7ixkA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=//g/7OffRCofFHIb1cBxIBJjHhepgsuxtwRomhT/GoA=; b=XmwE6fnUdEZXiiyWFIF5s2t5s+riM+MCRqaUHcVLcktw5FKlLYjW/Thf5EKv0xMoKQ HidG6EGna1OGjVH/s0jPd4W7VUs4039Iw7m8Iz9kvJSWmPeFbOdq+0s+ZgM3yxKDvrnI RRyN0xWd91R6XsnIh8Ywp64b8q44l5hoYuv9k8fzg28TXSeHfE2k9G9XJfxzZ+giXQPK J99s4kM6/Y6ZbKvPpsB2m/2xXajwFHjUJ8uPSq/0zfeL/TF5MiwGswLT7qN+RXyKiVaY 1Artxyphaayw69kZWsVlZOL528VAQi8RGeaPz38Dg+zAskD7Q/joaI4CmveI1N+OXBQF SLtg== X-Gm-Message-State: AOAM533cm+oqgXnwGVnV/PDIg019k4jFyqLf1ST6xE/X4yTcRn5KxF+L f0TWBcuQi3PsMHlZtu2Vu70q22zgiKurfqWqfRKhYwLYdbYFHSAyEaHmurJ4p6hPh94DGe9YR+Q IX8v2c28ywHgP7Q== X-Received: by 2002:a17:906:2558:: with SMTP id j24mr5123980ejb.447.1614294179064; Thu, 25 Feb 2021 15:02:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJzPDh0PhxbKHclw/uq42wtixE2aZ/PRB1a3t+RYQmspwOdw8VMv297eHl8H+OHOW1CDhdInwQ== X-Received: by 2002:a17:906:2558:: with SMTP id j24mr5123967ejb.447.1614294178954; Thu, 25 Feb 2021 15:02:58 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , haibinzhang , Zheng Xiang , qemu-block@nongnu.org, Paolo Bonzini , Xu Yandong , Kevin Wolf , Stefano Garzarella , Max Reitz Subject: [RFC PATCH 3/3] hw/block/pflash: use memory_region_init_rom_device_from_file() Date: Fri, 26 Feb 2021 00:02:38 +0100 Message-Id: <20210225230238.3719051-4-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210225230238.3719051-1-philmd@redhat.com> References: <20210225230238.3719051-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) If the block drive is read-only we will model a "protected" flash device. We can thus use memory_region_init_rom_device_from_file() which mmap the backing file when creating the MemoryRegion. If the same backing file is used by multiple QEMU instances, this reduces the memory footprint (this is often the case with the CODE flash image from OVMF and AAVMF). Suggested-by: Stefan Hajnoczi Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/block/pflash_cfi01.c | 20 ++++++++++++++------ hw/block/pflash_cfi02.c | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index a5fa8d8b74a..5757391df1c 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -743,11 +743,19 @@ static void pflash_cfi01_realize(DeviceState *dev, Er= ror **errp) pfl->ro =3D 0; } =20 - memory_region_init_rom_device( - &pfl->mem, OBJECT(dev), - &pflash_cfi01_ops, - pfl, - pfl->name, total_len, errp); + if (pfl->blk && pfl->ro) { + memory_region_init_rom_device_from_file(&pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, pfl, + pfl->name, total_len, + qemu_real_host_page_size, + RAM_SHARED, + blk_bs(pfl->blk)->filename, + true, errp); + } else { + memory_region_init_rom_device(&pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, pfl, + pfl->name, total_len, errp); + } if (*errp) { return; } @@ -755,7 +763,7 @@ static void pflash_cfi01_realize(DeviceState *dev, Erro= r **errp) pfl->storage =3D memory_region_get_ram_ptr(&pfl->mem); sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); =20 - if (pfl->blk) { + if (pfl->blk && !pfl->ro) { if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len, errp)) { vmstate_unregister_ram(&pfl->mem, DEVICE(pfl)); diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c index 4f62ce8917d..d57f64d7732 100644 --- a/hw/block/pflash_cfi02.c +++ b/hw/block/pflash_cfi02.c @@ -803,16 +803,26 @@ static void pflash_cfi02_realize(DeviceState *dev, Er= ror **errp) pfl->ro =3D 0; } =20 - memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), - &pflash_cfi02_ops, pfl, pfl->name, - pfl->chip_len, errp); + if (pfl->blk && pfl->ro) { + memory_region_init_rom_device_from_file(&pfl->orig_mem, OBJECT(pfl= ), + &pflash_cfi02_ops, pfl, + pfl->name, pfl->chip_len, + qemu_real_host_page_size, + RAM_SHARED, + blk_bs(pfl->blk)->filename, + true, errp); + } else { + memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), + &pflash_cfi02_ops, pfl, pfl->name, + pfl->chip_len, errp); + } if (*errp) { return; } =20 pfl->storage =3D memory_region_get_ram_ptr(&pfl->orig_mem); =20 - if (pfl->blk) { + if (pfl->blk && !pfl->ro) { if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, pfl->chip_len, errp)) { vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl)); --=20 2.26.2