From nobody Sun Apr 13 18:12:55 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612544744; cv=none; d=zohomail.com; s=zohoarc; b=jjYspRh0PPOQmgPUq/pDgQsODamN0VR2xTBxMkofUUZJIvZqVQ6cvm99+Ift5X1MlgpLkvnSCL+xGF6ww8JrFg6l7WoxAYkS6RcqWonNPPl7h7UfcyVnO1TZ3NKyV2v5Z6Wkpikw2596xdeh7Kgz74Sayay4kBED5pbGS+12lC0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612544744; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gNOmSK3NIM0FgxnSY4g6Eu3LT6fyyI9NgNkBdvwHS5g=; b=fm/NT0VlKJjP6bH6tKSm50fcUMxjiac9J7zvqZ2ldgQi2jm88k8y1FMOosSzblR7KwdT7A7NI2LVByezu1vhDjmFTENjBn8zLTBfr7Cj77QCoWM/vy0U2k3GpGtN98iD0aI3C6yp1raZ9Z80l+ZxZfr4Bi/rcSH82dZRyXdOIKY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1612544744130756.7036840564621; Fri, 5 Feb 2021 09:05:44 -0800 (PST) Received: from localhost ([::1]:41726 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l84Y9-0003Me-Qy for importer@patchew.org; Fri, 05 Feb 2021 12:05:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42478) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l84G2-0001h3-QB for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:46:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39448) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l84Fz-0006d7-Gv for qemu-devel@nongnu.org; Fri, 05 Feb 2021 11:46:58 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-1-Y9LVXJs1P92XjjvyA7BEGA-1; Fri, 05 Feb 2021 11:46:50 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B4C5419611A2; Fri, 5 Feb 2021 16:46:48 +0000 (UTC) Received: from localhost (ovpn-112-54.ams2.redhat.com [10.36.112.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8054519C47; Fri, 5 Feb 2021 16:46:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612543614; 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=gNOmSK3NIM0FgxnSY4g6Eu3LT6fyyI9NgNkBdvwHS5g=; b=Xnk13fdqaIaqhxQu4bZvJGzYSr4fPS7i+zv8FVeJUcX/y4D7063trBTu++FVlee6kg1ec7 llK6NyTjDGsHpSKGmCyBHTgt+zXNDzc37peDFvPHFo00N6kQ+oxutqye27MJbPMRPofcgO d1UcwkgVtWF1KipZjHZv9hAEW7fUfrM= X-MC-Unique: Y9LVXJs1P92XjjvyA7BEGA-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL v3 09/27] memory: alloc RAM from file at offset Date: Fri, 5 Feb 2021 16:44:41 +0000 Message-Id: <20210205164459.432714-10-stefanha@redhat.com> In-Reply-To: <20210205164459.432714-1-stefanha@redhat.com> References: <20210205164459.432714-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.352, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, 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: Fam Zheng , John G Johnson , Thomas Huth , Jagannathan Raman , Stefan Hajnoczi , qemu-block@nongnu.org, "Michael S. Tsirkin" , "Denis V. Lunev" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Elena Ufimtseva , Wainer dos Santos Moschetta , Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Jagannathan Raman Allow RAM MemoryRegion to be created from an offset in a file, instead of allocating at offset of 0 by default. This is needed to synchronize RAM between QEMU & remote process. Signed-off-by: Jagannathan Raman Signed-off-by: John G Johnson Signed-off-by: Elena Ufimtseva Reviewed-by: Stefan Hajnoczi Message-id: 609996697ad8617e3b01df38accc5c208c24d74e.1611938319.git.jag.ram= an@oracle.com Signed-off-by: Stefan Hajnoczi --- include/exec/memory.h | 2 ++ include/exec/ram_addr.h | 4 ++-- include/qemu/mmap-alloc.h | 4 +++- backends/hostmem-memfd.c | 2 +- hw/misc/ivshmem.c | 3 ++- softmmu/memory.c | 3 ++- softmmu/physmem.c | 12 +++++++----- util/mmap-alloc.c | 8 +++++--- util/oslib-posix.c | 2 +- 9 files changed, 25 insertions(+), 15 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index c6ce74fb79..0f66fcc6c2 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -992,6 +992,7 @@ void memory_region_init_ram_from_file(MemoryRegion *mr, * @size: size of the region. * @share: %true if memory must be mmaped with the MAP_SHARED flag * @fd: the fd to mmap. + * @offset: offset within the file referenced by fd * @errp: pointer to Error*, to store an error if it happens. * * Note that this function does not do anything to cause the data in the @@ -1003,6 +1004,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr, uint64_t size, bool share, int fd, + ram_addr_t offset, Error **errp); #endif =20 diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 40b16609ab..3cb9791df3 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -121,8 +121,8 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, Mem= oryRegion *mr, uint32_t ram_flags, const char *mem_pat= h, bool readonly, Error **errp); RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, - uint32_t ram_flags, int fd, bool readonly, - Error **errp); + uint32_t ram_flags, int fd, off_t offset, + bool readonly, Error **errp); =20 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, MemoryRegion *mr, Error **errp); diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index 8b7a5c70f3..456ff87df1 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -17,6 +17,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path); * @readonly: true for a read-only mapping, false for read/write. * @shared: map has RAM_SHARED flag. * @is_pmem: map has RAM_PMEM flag. + * @map_offset: map starts at offset of map_offset from the start of fd * * Return: * On success, return a pointer to the mapped area. @@ -27,7 +28,8 @@ void *qemu_ram_mmap(int fd, size_t align, bool readonly, bool shared, - bool is_pmem); + bool is_pmem, + off_t map_offset); =20 void qemu_ram_munmap(int fd, void *ptr, size_t size); =20 diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c index e5626d4330..69b0ae30bb 100644 --- a/backends/hostmem-memfd.c +++ b/backends/hostmem-memfd.c @@ -55,7 +55,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Er= ror **errp) name =3D host_memory_backend_get_name(backend); memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name, backend->size, - backend->share, fd, errp); + backend->share, fd, 0, errp); g_free(name); } =20 diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 0505b52c98..603e992a7f 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -495,7 +495,8 @@ static void process_msg_shmem(IVShmemState *s, int fd, = Error **errp) =20 /* mmap the region and map into the BAR2 */ memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s), - "ivshmem.bar2", size, true, fd, &local_= err); + "ivshmem.bar2", size, true, fd, 0, + &local_err); if (local_err) { error_propagate(errp, local_err); return; diff --git a/softmmu/memory.c b/softmmu/memory.c index 676c298b60..b4f48c0c29 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1612,6 +1612,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr, uint64_t size, bool share, int fd, + ram_addr_t offset, Error **errp) { Error *err =3D NULL; @@ -1621,7 +1622,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr, mr->destructor =3D memory_region_destructor_ram; mr->ram_block =3D qemu_ram_alloc_from_fd(size, mr, share ? RAM_SHARED : 0, - fd, false, &err); + fd, offset, false, &err); if (err) { mr->size =3D int128_zero(); object_unparent(OBJECT(mr)); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 60760a3bdc..bf562cf6fb 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1463,6 +1463,7 @@ static void *file_ram_alloc(RAMBlock *block, int fd, bool readonly, bool truncate, + off_t offset, Error **errp) { void *area; @@ -1513,7 +1514,8 @@ static void *file_ram_alloc(RAMBlock *block, } =20 area =3D qemu_ram_mmap(fd, memory, block->mr->align, readonly, - block->flags & RAM_SHARED, block->flags & RAM_PME= M); + block->flags & RAM_SHARED, block->flags & RAM_PME= M, + offset); if (area =3D=3D MAP_FAILED) { error_setg_errno(errp, errno, "unable to map backing store for guest RAM"); @@ -1944,8 +1946,8 @@ static void ram_block_add(RAMBlock *new_block, Error = **errp, bool shared) =20 #ifdef CONFIG_POSIX RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, - uint32_t ram_flags, int fd, bool readonly, - Error **errp) + uint32_t ram_flags, int fd, off_t offset, + bool readonly, Error **errp) { RAMBlock *new_block; Error *local_err =3D NULL; @@ -1999,7 +2001,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, new_block->max_length =3D size; new_block->flags =3D ram_flags; new_block->host =3D file_ram_alloc(new_block, size, fd, readonly, - !file_size, errp); + !file_size, offset, errp); if (!new_block->host) { g_free(new_block); return NULL; @@ -2030,7 +2032,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, M= emoryRegion *mr, return NULL; } =20 - block =3D qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, readonly, er= rp); + block =3D qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, readonly,= errp); if (!block) { if (created) { unlink(mem_path); diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 890fda6a35..e6fa8b598b 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -87,7 +87,8 @@ void *qemu_ram_mmap(int fd, size_t align, bool readonly, bool shared, - bool is_pmem) + bool is_pmem, + off_t map_offset) { int prot; int flags; @@ -150,7 +151,8 @@ void *qemu_ram_mmap(int fd, =20 prot =3D PROT_READ | (readonly ? 0 : PROT_WRITE); =20 - ptr =3D mmap(guardptr + offset, size, prot, flags | map_sync_flags, fd= , 0); + ptr =3D mmap(guardptr + offset, size, prot, + flags | map_sync_flags, fd, map_offset); =20 if (ptr =3D=3D MAP_FAILED && map_sync_flags) { if (errno =3D=3D ENOTSUP) { @@ -174,7 +176,7 @@ void *qemu_ram_mmap(int fd, * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC, * we will remove these flags to handle compatibility. */ - ptr =3D mmap(guardptr + offset, size, prot, flags, fd, 0); + ptr =3D mmap(guardptr + offset, size, prot, flags, fd, map_offset); } =20 if (ptr =3D=3D MAP_FAILED) { diff --git a/util/oslib-posix.c b/util/oslib-posix.c index bf57d3b030..36820fec16 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -230,7 +230,7 @@ void *qemu_memalign(size_t alignment, size_t size) void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared) { size_t align =3D QEMU_VMALLOC_ALIGN; - void *ptr =3D qemu_ram_mmap(-1, size, align, false, shared, false); + void *ptr =3D qemu_ram_mmap(-1, size, align, false, shared, false, 0); =20 if (ptr =3D=3D MAP_FAILED) { return NULL; --=20 2.29.2