From patchwork Thu Feb 25 23:02:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 12105165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95140C433E0 for ; Thu, 25 Feb 2021 23:04:06 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C964B64EF5 for ; Thu, 25 Feb 2021 23:04:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C964B64EF5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:58336 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFPfw-0007WY-UC for qemu-devel@archiver.kernel.org; Thu, 25 Feb 2021 18:04:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33454) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFPeu-0006Or-6v for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:03:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:54350) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lFPeo-0006xO-Cc for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:02:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614294173; 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=Rb5qZ/DY7PxrmnoW/cEQPE0NqKu0gQA3ZFeTkjLX7VeFh+R4RbbdFQQZbO+6h/s3QMsZol b8qVj5WnBfahFbHsjyNdN1xYtRSQoHNhfQrz4anddEhhmTf7nw46flQkVKhyJKuamloFud yJaBRyCAOW9vXoPsC3S2BPweZ46ADWc= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-303-vfVAGkjmNmG29Xia5Vqhdw-1; Thu, 25 Feb 2021 18:02:49 -0500 X-MC-Unique: vfVAGkjmNmG29Xia5Vqhdw-1 Received: by mail-ed1-f70.google.com with SMTP id t9so3617392edd.3 for ; Thu, 25 Feb 2021 15:02:49 -0800 (PST) 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=g1VfA63WW6GnkYtXwjp93dXdlaOfJ36acfN8U3JQqIHxWusNtuFLb28rdusK1U3A79 bWjrjqJ/wUMEKDS/nCmKSPbC2t5tXOF1+n4k6Oc6xYgHbwLs+WgZd5qHhtjCufAIQj8D drkfjs3ULjETBXg0PIwBGkZmqcSK6pJpqSywlf/56hrs1Vfn8rh0KjDC7LK5ZzvlvGi7 ghS6ICr6VI3y5bzPxKvODyuAqfKtQwDaczxKY6ByG8kBG9QNI1TiMC1Y9cqG6FgFFZUB XTSa6ZtKSyNiIhvmtbT7wCHzcn6JuPu548Qinp1zQW7IKLZO8uD7wkTO5AHCa01nXxuY KXGA== X-Gm-Message-State: AOAM5316+uLi/eopKnUphhQYKRWCjKU3iW+g6Q0m/QaluWbsjvncqe/r S0rI5pNK5uS6E5nPsVbunctS/6yLDv54RiVT28DYQG9do3NlmwAzGPEPNrXjv+4k7G4+FpiNufI 2M9YzKANb5JVGdHNPdFOqrMGdqbkl5sEIL+bllv3jXlHNMPRnUyQKq/pfehHCETog X-Received: by 2002:a17:907:3daa:: with SMTP id he42mr5338757ejc.54.1614294167963; 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) 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) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson 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 Received-SPF: pass client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, RCVD_IN_MSPIKE_H3=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Xu Yandong , Markus Armbruster , Max Reitz , Zheng Xiang , haibinzhang , Stefan Hajnoczi , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Stefano Garzarella Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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é --- 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 }; struct IOMMUMemoryRegion { @@ -1131,6 +1134,43 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, uint64_t size, Error **errp); +/** + * 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 stream + * 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_SHARED + * - 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 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_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); +/** + * 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 stream + * 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_SHARED + * - 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(MemoryRegion *mr) qemu_ram_free(mr->ram_block); } +#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 == '/' || c == '[' || c == '\\' || c == ']'; @@ -1712,6 +1720,96 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, } } +void memory_region_init_rom_device_from_file_nomigrate(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) +{ + Error *err = NULL; + + assert(ops); +#ifdef CONFIG_POSIX + memory_region_init(mr, owner, name, size); + mr->opaque = opaque; + mr->ops = ops; + mr->rom_device = true; + mr->readonly = readonly; + mr->ram = true; + mr->align = align; + mr->terminates = true; + mr->destructor = memory_region_destructor_ram; + mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, + readonly, &err); + if (err) { + mr->size = int128_zero(); + object_unparent(OBJECT(mr)); + error_propagate(errp, err); + } +#else + g_autoptr(GError) gerr = NULL; + gsize len; + + memory_region_init(mr, owner, name, size); + mr->ops = ops; + mr->opaque = opaque; + mr->terminates = true; + mr->rom_device = 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 = memory_region_destructor_contents; + mr->contents = g_realloc(mr->contents, size); + mr->ram_block = qemu_ram_alloc_from_ptr(size, mr->contents, mr, &err); + if (err) { + mr->size = 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 = NULL; + + memory_region_init_rom_device_from_file_nomigrate(mr, owner, ops, opaque, + name, size, align, + ram_flags, path, readonly, + &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 = DEVICE(owner); + vmstate_register_ram(mr, owner_dev); +} + void memory_region_init_iommu(void *_iommu_mr, size_t instance_size, const char *mrtypename, From patchwork Thu Feb 25 23:02:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 12105169 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22A69C433DB for ; Thu, 25 Feb 2021 23:06:41 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B39056148E for ; Thu, 25 Feb 2021 23:06:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B39056148E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFPiR-0001LR-QM for qemu-devel@archiver.kernel.org; Thu, 25 Feb 2021 18:06:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33482) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFPew-0006Ru-8j for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:03:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:23683) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lFPet-0006ys-LA for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:03:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614294176; 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=RWMuNXStdqGEU5Fc9VXSJaRAL1AA4+F5bPLnj/2Z+xhBDNOjyGHz8QNlaGMbM3Jmp3W36D U2vvCnKK2jIMr4ksNBnNvcgOxSDCmUwovaFF44ypziPuREY5uq8L3wigCPw7S0G5zk5gB1 Ta43HJkuLy9VDpc/gAlpT7RsxZCoNno= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-570-Ad0DP5XqOPSP-HaDK547qw-1; Thu, 25 Feb 2021 18:02:55 -0500 X-MC-Unique: Ad0DP5XqOPSP-HaDK547qw-1 Received: by mail-ej1-f69.google.com with SMTP id ml13so3257898ejb.2 for ; Thu, 25 Feb 2021 15:02:54 -0800 (PST) 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=RX6qy7Oq1fv6zB4hgAx9vgzmaeM1NK/QIrkuwyj7ZYEf/UyNcSfD2Py872a/BLLQYD 6rtmAuST5xZ+jFlXHPmIQiFiBNNWxDWLjLL5s23ye8qAw14IeNfTMbWDwa60fqBwDlQM byz6PSLiBvEtsuW0DLGAJ4Xh7DAnkMdX+GS+3s2QxvjJCq+Hftb+uA3UJmr2R8CV1UVs V8hf2b9ne3D19aBdjDh1jW/n8jfCquRlL4WE4I96TkN7DLSB0IV2eRSPcSR9mjXG/T68 iitd/VR1Nzy2fRBrXOe7V/sdAL9/H2ObcpcKuTz77g0KFEinYJxyGC8xAsDrAI7GSAeF ceAw== X-Gm-Message-State: AOAM533PUQlaHlxLIMfBGeBr+junCLUl4qxeOBksQYaRNi+AyXgOEvRS LWvuiPr/Jo4JVBIwhCmyF9SaGPp/lIJzRMzNH92SJEuaf8co2+Tq30IlY61Q3bRPD7k3J+pRL+B YTm4FjVxjcvsck4l9nvyLR2ky/8ownHLar3/UCaMh/AxZ3t/gBRLy6RX4paJ6CJLO X-Received: by 2002:a17:906:3510:: with SMTP id r16mr5259878eja.186.1614294173513; 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) 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) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Xu Yandong , Markus Armbruster , Max Reitz , Zheng Xiang , haibinzhang , Stefan Hajnoczi , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Stefano Garzarella Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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é 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, Error **errp) } device_len = sector_len_per_device * blocks_per_device; - memory_region_init_rom_device( - &pfl->mem, OBJECT(dev), - &pflash_cfi01_ops, - pfl, - pfl->name, total_len, errp); - if (*errp) { - return; - } - - pfl->storage = memory_region_get_ram_ptr(&pfl->mem); - sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); - if (pfl->blk) { uint64_t perm; pfl->ro = !blk_supports_write_perm(pfl->blk); @@ -755,6 +743,18 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) pfl->ro = 0; } + memory_region_init_rom_device( + &pfl->mem, OBJECT(dev), + &pflash_cfi01_ops, + pfl, + pfl->name, total_len, errp); + if (*errp) { + return; + } + + pfl->storage = 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, Error **errp) return; } - 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 = memory_region_get_ram_ptr(&pfl->orig_mem); - if (pfl->blk) { uint64_t perm; pfl->ro = !blk_supports_write_perm(pfl->blk); @@ -812,6 +803,15 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp) pfl->ro = 0; } + 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 = 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)) { From patchwork Thu Feb 25 23:02:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 12105167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9A89C433E0 for ; Thu, 25 Feb 2021 23:06:29 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7DDB164EDB for ; Thu, 25 Feb 2021 23:06:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7DDB164EDB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFPiG-000147-9k for qemu-devel@archiver.kernel.org; Thu, 25 Feb 2021 18:06:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33544) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFPf0-0006ZO-IT for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:03:06 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:59927) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lFPey-00071D-Ce for qemu-devel@nongnu.org; Thu, 25 Feb 2021 18:03:06 -0500 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= 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-450-e2VJ5zZxPZKMI58rycymug-1; Thu, 25 Feb 2021 18:03:02 -0500 X-MC-Unique: e2VJ5zZxPZKMI58rycymug-1 Received: by mail-ed1-f69.google.com with SMTP id i4so3625604edt.11 for ; Thu, 25 Feb 2021 15:03:01 -0800 (PST) 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=Bas3cnHKb2i8Si0vAeQ33LLvAYVQEhhQN6Bn/pwOguc/4Z3+lhS/NSjexI/xdEYDin PrAiWxi7LuvV9hs/ldaa2BHp2hMxIe0pOHu62XbchfriAay3Y4+Zu+1oGJScpFgBBoyT TvQSOW7zsI/Fwla5XYbne6ji3ko3/ePEYkWmjpEeuzN1htX5lAAb5g9KAJs6QtDytUBd ILA+BS5Pa5I+kZDnvj31xUsJQJnCrV3SpaEqtEYUtC4MqKc1TqN5FkB6f0nE7ZaNtL4r RGNLsZueJezNQaVJMHP2sT05Dwcz10R+3I7oj7LCov/1ARx6+RD2MbgV4IGpe+BVLxky R6TQ== X-Gm-Message-State: AOAM5336ZbVELdExtrazpkFF38CJ3A/QN0vQbp7ewjroQQBm1Lx99JGQ cB+2oilG0YqCmKqUgNIwrH45lla9s/RBGFq13tRJ0HWaYME6Idx8+byqL83qpt2WqyI6gjW24gy GJaQGyYtiNB7Bbd4M8BP6iEXx5MAdgKmr0OVXNAMvsZAXBXsWBLk38SvXh0s95A1J X-Received: by 2002:a17:906:2558:: with SMTP id j24mr5124001ejb.447.1614294179147; 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) 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) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, David Edmondson 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 Received-SPF: pass client-ip=63.128.21.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-block@nongnu.org, Xu Yandong , Markus Armbruster , Max Reitz , Zheng Xiang , haibinzhang , Stefan Hajnoczi , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Stefano Garzarella Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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é --- 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, Error **errp) pfl->ro = 0; } - 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, Error **errp) pfl->storage = memory_region_get_ram_ptr(&pfl->mem); sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); - 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, Error **errp) pfl->ro = 0; } - 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; } pfl->storage = memory_region_get_ram_ptr(&pfl->orig_mem); - 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));