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