From nobody Wed Nov 19 04:33:30 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