From nobody Mon May 20 18:31:36 2024 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=1614599622; cv=none; d=zohomail.com; s=zohoarc; b=h9WlwymN5lMWIf8A95rnP5Et/NNp3tYigWTovjlR7KwJ3KW4680iSiR4t/NBkVYhWexXng36I+2rFrrs1eT/30BwuQSi6dnUjEdVgFYSLU58BIPWP45G/YzkY1mhjCPtc2ry1aKUfsxUQvPfvi3jtFKV5x+HQCkygnQhCETWsxM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614599622; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=z3QPbF3kCYrwGbuoCgT50QmHdrj0vIiledduYlpbOmo=; b=Q+Biv1DWI6A/Ur3APxyXv11S9M4z5HefnHHtn1qVu4frcWHxx8q+T5FlUPM8nQPJeylUk/KFAe6ucixvlBzXxjeoH0DQz/k8T0znbLgmzkzc6XPb1cJhPryD4Nzy7/E6k8poH6M8FaH2BcopSiaV5KNtslq1tJNkVRyHAfXCf9E= 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 1614599622467950.9382498161206; Mon, 1 Mar 2021 03:53:42 -0800 (PST) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-475-CIn2gfS3OKmYdOkjQszKnA-1; Mon, 01 Mar 2021 06:53:37 -0500 Received: by mail-wm1-f70.google.com with SMTP id w10so1493050wmk.1 for ; Mon, 01 Mar 2021 03:53:37 -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 d29sm24928181wra.51.2021.03.01.03.53.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Mar 2021 03:53:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614599621; 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=U2ogz4kyO9YnahBpQiPRoZpLmR1ZBP7cv31NmMmMSAE7T/ZQFSK7Iz/mLfQ16Fd8xy6UM1 GMRw1yD+4MrH3Uzza5eyzDWz6MHc3Z18DQ8HwJzemrUdpmaLnBl+SEmX8ZGtGe+MOUFjxL u9UcWoHeho/6R0vw7ba98DQOLPXsqTg= X-MC-Unique: CIn2gfS3OKmYdOkjQszKnA-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=YyO9khsN4CUQXRyP3vvRYRraZ+5lMPvxNuk7uG4WPb6wytHbcNr9uiCZ3DnCZbDzuX a/447CvRWZnrLfoSZ0x+EjRzwxHnNY5X2ahEmfXmDiy8btSjGCVvk+qGClbngQv2STie HSfBz3w+FK4s5m2a+V6s/DOGYNlpU0p/FrfIx44lYNGen8RxwD3lam2TIh0rQO2AskAn yM8wPUrQOPFdFJjqF38hVgXHO1QiquQsLst1YtqI3DuV2jAqp/umM/d+I7nflv7EFPlI M9j6cGZl1wpHVZmr7jRmrsQrRjDhSD8CV4vEMUpwn3mOaT4L0zhZwJXl8V6a2GBYCqdH Leuw== X-Gm-Message-State: AOAM532TCu1mRblhD9z2Z2Bwv6MpI2WlNj2+VAiqlMQCH4VHiNBdjBZn B0DedPl3VdBPHIHpTD4+viEs/J6Wupuo4hQmwfKYYWjLBoNXKcVepsvPisi7dLAzPklkhqneXa/ Pn2UqQs62lQ0WOA== X-Received: by 2002:a05:6000:191:: with SMTP id p17mr10412372wrx.154.1614599616354; Mon, 01 Mar 2021 03:53:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzlssJzedcjxlgmMmgIoVO7axdJ52yUIzncIcS0L7LALJ17shPwJVTcHBUkTpeDXsYvVHT3Cw== X-Received: by 2002:a05:6000:191:: with SMTP id p17mr10412343wrx.154.1614599616172; Mon, 01 Mar 2021 03:53:36 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Max Reitz , Markus Armbruster , Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org, Xu Yandong , haibinzhang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , Stefano Garzarella Subject: [RFC PATCH v2 1/3] exec/memory: Introduce memory_region_init_rom_device_from_file() Date: Mon, 1 Mar 2021 12:53:27 +0100 Message-Id: <20210301115329.411762-2-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210301115329.411762-1-philmd@redhat.com> References: <20210301115329.411762-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 Mon May 20 18:31:36 2024 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=1614599624; cv=none; d=zohomail.com; s=zohoarc; b=dDc3MMIKNu4pxSL1m5kybPYS7M6ReiZZIk8yLUaGjX7G5lR034cPJowoG4488LEjC1tZz3kRNeEjMnI3rxVeSpOrHLFCmof2EY/yWh5LXdODX3fw77Dd+DqNiyhm5R4uxfyrZBCgEHaLqqL/RsS8es+aw/wh6kVkdWVqFFX/Kzc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614599624; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=xepneabtMBs6KXDAhAA/tNxteramngj4xAhOLaaKo9c=; b=Wjr67etKAK1dQKNO40DmniMcvdYprnCB+psXsfU4lOdN4uL1AoK20Nr8jAa0XZQ2awRBs52KK1/XPfRts3rrI7KNVNDSFo7Mm3uC8R/5wNZqaEX9loylmtGPU0DoTC82ukCfGgvYbvsXUfj7E4tkfhgbdRklSmbakE3dTc3ueu0= 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 1614599624804120.33068553267651; Mon, 1 Mar 2021 03:53:44 -0800 (PST) Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-229-YdtzsqC5N0WFPCJh_rIHQg-1; Mon, 01 Mar 2021 06:53:42 -0500 Received: by mail-wm1-f69.google.com with SMTP id a63so1183523wmd.8 for ; Mon, 01 Mar 2021 03:53:42 -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 j20sm3089944wmp.30.2021.03.01.03.53.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Mar 2021 03:53:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614599623; 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=xepneabtMBs6KXDAhAA/tNxteramngj4xAhOLaaKo9c=; b=GurEYjKZpb89MJqRGTlzwv06gtN5M0vLYE/37jCIJTnyOTkwE+4kFTaIchoTeiZUbFJY/k Ecm2QqN8IMGMkwZNf7aIzCRrDRD1FfcCaQnyjESSATz2L73hyJlt2wok5RxAHU11+NyQ/1 1LEuCPg9QQ+ATLyOik564p7F+4Kz+Yc= X-MC-Unique: YdtzsqC5N0WFPCJh_rIHQg-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=xepneabtMBs6KXDAhAA/tNxteramngj4xAhOLaaKo9c=; b=KbptqCNtXyjk+s8T/jQDq8+K3TtKYoWloO7czSlpLg9N1YQgbuBc3+9iQTYJ4hITIF xA3MxEgXKdMa32Ylv62lsQOYsV6iCh0lcUQsBSN3Nk+cwFyYcF1jPcK5jSA0JjSJyizs 5MxHVRZubRHw4rTLTo2NT62JM3wpLcYTbFaIX4qJwJJK7xCuvjU4IisrepkuLu8icIp8 EXRR6bfbJgXdd1JTDWgJAj86a4dL4HXePUwBG6ng0ynBx5ofU/0QS7Skyvej4up7tecx 45NQ3TqLuVsovZfcY8I2t65kY/YM8c1LGlBrUJ+YE34g02WyWAa7DoX/8r4O682FukEU bDYg== X-Gm-Message-State: AOAM5322zTd6fjhvs7Js6o9qo0K1XHuu3dCaDx0J6DTPXgYhEB4u3gva vAwLnaiypHPfUElXPg2VbwqKJBLJzEFGXj+79yGHtQyw6MdkPeKY2BiFSxADnk3B9EfTWpx4l6L g1sokAnjljZjQ6A== X-Received: by 2002:a05:6000:191:: with SMTP id p17mr10412723wrx.154.1614599621233; Mon, 01 Mar 2021 03:53:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbSJLJuO+qUHga/oM29TRxmPqrK07VydbWzvMt7RdfoPM8pPg8OJhd2ySGk59IiVOlBfTLaw== X-Received: by 2002:a05:6000:191:: with SMTP id p17mr10412706wrx.154.1614599621111; Mon, 01 Mar 2021 03:53:41 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Max Reitz , Markus Armbruster , Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org, Xu Yandong , haibinzhang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , Stefano Garzarella Subject: [RFC PATCH v2 2/3] hw/block/pflash: Move code around Date: Mon, 1 Mar 2021 12:53:28 +0100 Message-Id: <20210301115329.411762-3-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210301115329.411762-1-philmd@redhat.com> References: <20210301115329.411762-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. Reviewed-by: David Edmondson Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- 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 Mon May 20 18:31:36 2024 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=1614599629; cv=none; d=zohomail.com; s=zohoarc; b=kkMo6yJFDwYHDftzlnTpmsPVHkmfMQyHR3Xjd4HH56VbOP7qrpNOC2bD4PsSL9xd/sJVaLcNyn7CjbXdrsdrHhZrLHSgr/7EK0eKGqNwG+Fz2Ar9zR8v/0Lt6VSlEdpWBYcU3MLG7tLoL43NHGFHNzrIhwy25HRpZUGudgQTFkk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614599629; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=YDEQgbWck1j5/yNbxcJrrrXZNKuh+ZlCvLLks2REe7U=; b=XwDIAe56IwOikIksba62nowjRaSO19y0KagjGD6l9Lau0s5RZEbxj4tRBQBIS+aTrI0pQC5RIyxj/MJ76PKERCUt5TGIzLZgk2A4R0+n5yDu5ygSNy6HQ4ML/76AxK7jjIvk/T0R/biacJtyT5B/ScGnJNgHHTx7YVJ/DFMgBhg= 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 1614599629707789.2627339275241; Mon, 1 Mar 2021 03:53:49 -0800 (PST) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-395-f6CqUlbPOHCYRjA6Qj9Bfg-1; Mon, 01 Mar 2021 06:53:47 -0500 Received: by mail-wm1-f70.google.com with SMTP id h16so5519632wmq.8 for ; Mon, 01 Mar 2021 03:53:47 -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 z11sm22420878wmf.28.2021.03.01.03.53.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Mar 2021 03:53:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614599628; 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=YDEQgbWck1j5/yNbxcJrrrXZNKuh+ZlCvLLks2REe7U=; b=Uxm5DTqu5vZcABQgy/305nehWAoeQYA68U3kWpHMdRMuz4Tp98f9TLaql14WEqHiqOxvnl I6uToyMqe2NZ/Bv0w/mqDTDiWhWOAU8A74BrEc2/dP9yokmufoJWKdUztFz+5JdGd65sGv cFRNaKfMVyagXsTTNLWDgwHkdKenVGw= X-MC-Unique: f6CqUlbPOHCYRjA6Qj9Bfg-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=YDEQgbWck1j5/yNbxcJrrrXZNKuh+ZlCvLLks2REe7U=; b=JnA8YDpI1+u80sTf4UXwT7R1ect8c3Z4C1VIiIfsPlk+to3XoN84bgAbzRHTgWDFw8 yARzAQh6mDxLhzdGZDauvitAoTecSfv/2Ztbrcf3M1aq5xkwo992BqNboaMcn7La8P6x piyycm3s5XdCRfMe4GkpuO55kNJ8M/Qn2oiDIS6W9UBIGyuw0j4OSVIcVnzYIdsV9XvF dOlMVHehQC2glXSGA0aqR4gGq199/khIZqgFLOB1Ar5ghXBrvaq/9r26Zan+kVjETCcB 0chL2+vA62ZgqI3FV40PSdU/lyntBGlCiO90Jh+aBtxsmv9hqzkLc9AqXj8Dj0PPyz6i WYNg== X-Gm-Message-State: AOAM530kRtqdY6eZzS+tZviAdkvvKLUobTWSHG6E6vZJIWugscelbO+N lhYVCjShOeFlKJQ0NNOlzzGwI03ALTf6/G3nN/KKJOqfg1iTV7UDpQAJtvYp5irJZ39f3CUgwqx 0SYwErOz+Gk77Uw== X-Received: by 2002:adf:dbc2:: with SMTP id e2mr15629736wrj.227.1614599626120; Mon, 01 Mar 2021 03:53:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYt96kuiaD+I9OlJAtRoODtoD2cQgKW9T0gdObGflo+M2HoBYw/4GoXiI6mLOXJ//naYaVVQ== X-Received: by 2002:adf:dbc2:: with SMTP id e2mr15629719wrj.227.1614599625982; Mon, 01 Mar 2021 03:53:45 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson Cc: Max Reitz , Markus Armbruster , Kevin Wolf , Paolo Bonzini , qemu-block@nongnu.org, Xu Yandong , haibinzhang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , Stefano Garzarella Subject: [RFC PATCH v2 3/3] hw/block/pflash: use memory_region_init_rom_device_from_file() Date: Mon, 1 Mar 2021 12:53:29 +0100 Message-Id: <20210301115329.411762-4-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210301115329.411762-1-philmd@redhat.com> References: <20210301115329.411762-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 | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index a5fa8d8b74a..ec290636298 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -702,6 +702,7 @@ static void pflash_cfi01_realize(DeviceState *dev, Erro= r **errp) int ret; uint64_t blocks_per_device, sector_len_per_device, device_len; int num_devices; + bool romd_mr_shared_mapped; =20 if (pfl->sector_len =3D=3D 0) { error_setg(errp, "attribute \"sector-length\" not specified or zer= o."); @@ -743,19 +744,41 @@ 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 (*errp) { - return; + if (pfl->ro && pfl->blk) { + BlockDriverState *bs =3D blk_bs(pfl->blk); + + /* If "raw" driver used, try to mmap the backing file as RAM_SHARE= D */ + if (bs->drv =3D=3D &bdrv_raw) { /* FIXME check offset=3D0 ? */ + Error *local_err =3D NULL; + + memory_region_init_rom_device_from_file(&pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, pfl, + pfl->name, total_len, + qemu_real_host_page_si= ze, + RAM_SHARED, + bs->exact_filename, + true, &local_err); + if (local_err) { + error_report_err(local_err); + /* fall back to memory_region_init_rom_device() */ + } else { + romd_mr_shared_mapped =3D true; + } + } + } + if (!romd_mr_shared_mapped) { + memory_region_init_rom_device(&pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, pfl, + pfl->name, total_len, errp); + if (*errp) { + return; + } } =20 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 && !romd_mr_shared_mapped) { if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len, errp)) { vmstate_unregister_ram(&pfl->mem, DEVICE(pfl)); --=20 2.26.2