From nobody Tue Nov 26 08:58:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1709120978; cv=none; d=zohomail.com; s=zohoarc; b=VUA6pZNmplGaUJ8B2VtMG9qQ0pC0GwcIOX0bl49/OcQNe88yBXC823IOaaMOtWnUKYeDcR6VmPQPERfuvDOpNMP4PzttEN31ANNn4l8LzBmrgqv+YPU89tQeGLv43i6qpTaE12H6KiXNIXqAwD+0xKZnQ5o9u/MhxmjJ1jKtxSw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709120978; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fLM8N8KufGIAXLoDANm2UQXu1s3D3+QLrZtKYGXzVoE=; b=LTV6kY8IYTtDQZiBMUqOs6fG0M6dICi6/J1BXvugGTIDMbfAhIkerUZBPA2mC+KtmzBOVMXRNc3hydDgbIu+Vl8RRtv9MKlooH9oTqLIK1ve9iQ/nHWCF3uAloCt4it5rev6CmseGX+w9QQLA5+uWDkhSK8jppVlwO3+x05ArPc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1709120978590100.65173468772048; Wed, 28 Feb 2024 03:49:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rfIRI-0006Bh-AK; Wed, 28 Feb 2024 06:49:32 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfIRF-0005z6-OC for qemu-devel@nongnu.org; Wed, 28 Feb 2024 06:49:29 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rfIR0-00043G-MM for qemu-devel@nongnu.org; Wed, 28 Feb 2024 06:49:29 -0500 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-664-ATKOrPTgMVq0db0zGoDf_g-1; Wed, 28 Feb 2024 06:49:12 -0500 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-69033ba441fso6424406d6.3 for ; Wed, 28 Feb 2024 03:49:12 -0800 (PST) Received: from step1.redhat.com (host-82-57-51-64.retail.telecomitalia.it. [82.57.51.64]) by smtp.gmail.com with ESMTPSA id qj24-20020a056214321800b0068f9fd1d688sm5121717qvb.21.2024.02.28.03.49.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 03:49:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709120954; 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=fLM8N8KufGIAXLoDANm2UQXu1s3D3+QLrZtKYGXzVoE=; b=L2SuMPT3hUFWfsW7ZNxQyChGYGn+C1HTWcXC/Hkn+dRMiFgTx/31rqM2OocoPaFoYwGvMI S9jsO+eSU8Qm+RR3T/S1X1Htm4rg2aIu/zjz0lRXOzCU3ah3brfJYzcNsUW1d5yl7X7/Rs rRwKkH+q6Y3FndexKiAsiG4HHjLjQVk= X-MC-Unique: ATKOrPTgMVq0db0zGoDf_g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709120952; x=1709725752; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fLM8N8KufGIAXLoDANm2UQXu1s3D3+QLrZtKYGXzVoE=; b=xAn6Jv8ZrS+RxIp1I5vWA+jEiJrwv5MuCquxwHiyn+dOu9rjfWyPbubxau4sOKin+O x/a9knjAJk/MOSFKPyGSGO9ACGMshj/y8+1FnA+LZdT5CT+dpprrJtX+i+qfmyWrzBxv yFIAd84xSjI6xSOpRpINKcSrUMzgM6H3rCe8X4o5RXHWtOzdm80JfvwpNlEy3yJlogaG 0H4Mq/OEiRot1Dv8er/Yjupq9M9UVEOWw3CfUFSGLln5fBVjvNvstX3FymKm/xGEZoXk V3IgCWAzxsPN6KdJKuLQfeAq9RF6je/FVvDzPWALpX7AUTngeTg3+8ZNAf0E4KGK0X+e HwBQ== X-Gm-Message-State: AOJu0YxfBOH2aeK/8BOhSFpqdO0MiD9eFqYzWrfFUo7bYxck8r5kYiLM OU5DsTXpXb1CkLfsw2MU9jFNSlRHyrIhfcwtBBeUdw1EBkiCJXeuW8Jfy6X4bObyUolvUSXwpNp GO4FdcxXilqIqe2Xz//7mSLKKAzI8dXSRzKKbonpx9si2S4/s23HdUCyCgPIQmTu8ZamSsPsYB2 cUdY5LwAeXOOig230qvyP5IRYVsfIqUL+DGIUx X-Received: by 2002:a05:6214:3019:b0:690:b3f:8a1 with SMTP id ke25-20020a056214301900b006900b3f08a1mr5770451qvb.53.1709120951936; Wed, 28 Feb 2024 03:49:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFH1Xe9yloE9WF1xN62J37kqng1CId0u7Gqr9fCNzmg+QWyugEloWRMBvLpAyAPZ4B95+bZ0Q== X-Received: by 2002:a05:6214:3019:b0:690:b3f:8a1 with SMTP id ke25-20020a056214301900b006900b3f08a1mr5770418qvb.53.1709120951591; Wed, 28 Feb 2024 03:49:11 -0800 (PST) From: Stefano Garzarella To: qemu-devel@nongnu.org Cc: Paolo Bonzini , qemu-block@nongnu.org, =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Thomas Huth , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Markus Armbruster , Eric Blake , Coiby Xu , slp@redhat.com, Eduardo Habkost , Hanna Reitz , Igor Mammedov , Raphael Norwitz , Kevin Wolf , David Hildenbrand , stefanha@redhat.com, gmaglione@redhat.com, Stefano Garzarella Subject: [PATCH 9/9] hostmem-file: support POSIX shm_open() Date: Wed, 28 Feb 2024 12:47:59 +0100 Message-ID: <20240228114759.44758-10-sgarzare@redhat.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240228114759.44758-1-sgarzare@redhat.com> References: <20240228114759.44758-1-sgarzare@redhat.com> MIME-Version: 1.0 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=170.10.133.124; envelope-from=sgarzare@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_TEMPERROR=0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1709120979760100002 Content-Type: text/plain; charset="utf-8" Add a new `shm` bool option for `-object memory-backend-file`. When this option is set to true, the POSIX shm_open(3) is used instead of open(2). So a file will not be created in the filesystem, but a "POSIX shared memory object" will be instantiated. In Linux this turns into a file in /dev/shm, but in other OSes this may not happen (for example in macOS or FreeBSD nothing is shown in any filesystem). This new feature is useful when we need to share guest memory with another process (e.g. vhost-user backend), but we don't have memfd_create() or any special filesystems (e.g. /dev/shm) available as in macOS. Signed-off-by: Stefano Garzarella --- I am not sure this is the best way to support shm_open() in QEMU. Other solutions I had in mind were: - create a new memory-backend-shm - extend memory-backend-memfd to use shm_open() on systems where memfd is not available (problem: shm_open wants a name to assign to the object, but we can do a workaround by using a random name and do the unlink right away) Any preference/suggestion? Thanks, Stefano --- qapi/qom.json | 4 +++ backends/hostmem-file.c | 57 ++++++++++++++++++++++++++++++++++++++++- backends/meson.build | 2 +- qemu-options.hx | 10 +++++++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/qapi/qom.json b/qapi/qom.json index 2a6e49365a..bfb01b909f 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -682,6 +682,9 @@ # writable RAM instead of ROM, and want to set this property to 'off= '. # (default: auto, since 8.2) # +# @shm: if true, shm_open(3) is used to create/open POSIX shared memory +# object; if false, an open(2) is used. (default: false) (since 9.0) +# # Since: 2.1 ## { 'struct': 'MemoryBackendFileProperties', @@ -692,6 +695,7 @@ 'mem-path': 'str', '*pmem': { 'type': 'bool', 'if': 'CONFIG_LIBPMEM' }, '*readonly': 'bool', + '*shm': 'bool', '*rom': 'OnOffAuto' } } =20 ## diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index ac3e433cbd..9d60375c1f 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -34,6 +34,7 @@ struct HostMemoryBackendFile { bool is_pmem; bool readonly; OnOffAuto rom; + bool shm; }; =20 static bool @@ -86,7 +87,37 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Er= ror **errp) ram_flags |=3D fb->rom =3D=3D ON_OFF_AUTO_ON ? RAM_READONLY : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; + /* TODO: check if this should be enabled if shm is enabled */ ram_flags |=3D RAM_NAMED_FILE; + + if (fb->shm) { + mode_t mode =3D S_IRUSR | S_IWUSR; + int fd, oflag =3D 0; + + oflag |=3D fb->readonly ? O_RDONLY : O_RDWR; + oflag |=3D O_CREAT; + + fd =3D shm_open(fb->mem_path, oflag, mode); + if (fd < 0) { + error_setg_errno(errp, errno, + "failed to create POSIX shared memory"); + return false; + } + + if (ftruncate(fd, backend->size) =3D=3D -1) { + error_setg_errno(errp, errno, + "failed to resize POSIX shared memory to %" P= RIu64, + backend->size); + shm_unlink(fb->mem_path); + return false; + } + + return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend= ), + name, backend->size, ram_fla= gs, + fd, fb->offset, errp); + + } + return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),= name, backend->size, fb->align, ram_= flags, fb->mem_path, fb->offset, errp= ); @@ -254,17 +285,36 @@ static void file_memory_backend_set_rom(Object *obj, = Visitor *v, visit_type_OnOffAuto(v, name, &fb->rom, errp); } =20 +static bool file_memory_backend_get_shm(Object *obj, Error **errp) +{ + return MEMORY_BACKEND_FILE(obj)->shm; +} + +static void file_memory_backend_set_shm(Object *obj, bool value, + Error **errp) +{ + MEMORY_BACKEND_FILE(obj)->shm =3D value; +} + static void file_backend_unparent(Object *obj) { HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); =20 - if (host_memory_backend_mr_inited(backend) && fb->discard_data) { + if (!host_memory_backend_mr_inited(backend)) { + return; + } + + if (fb->discard_data) { void *ptr =3D memory_region_get_ram_ptr(&backend->mr); uint64_t sz =3D memory_region_size(&backend->mr); =20 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE); } + + if (fb->shm) { + shm_unlink(fb->mem_path); + } } =20 static void @@ -300,6 +350,11 @@ file_backend_class_init(ObjectClass *oc, void *data) file_memory_backend_get_rom, file_memory_backend_set_rom, NULL, NU= LL); object_class_property_set_description(oc, "rom", "Whether to create Read Only Memory (ROM)"); + object_class_property_add_bool(oc, "shm", + file_memory_backend_get_shm, + file_memory_backend_set_shm); + object_class_property_set_description(oc, "shm", + "Use shm_open(3) to create/open POSIX shared memory objects"); } =20 static void file_backend_instance_finalize(Object *o) diff --git a/backends/meson.build b/backends/meson.build index 8b2b111497..64520c0a7e 100644 --- a/backends/meson.build +++ b/backends/meson.build @@ -12,7 +12,7 @@ system_ss.add([files( =20 if host_os !=3D 'windows' system_ss.add(files('rng-random.c')) - system_ss.add(files('hostmem-file.c')) + system_ss.add([files('hostmem-file.c'), rt]) endif if host_os =3D=3D 'linux' system_ss.add(files('hostmem-memfd.c')) diff --git a/qemu-options.hx b/qemu-options.hx index 9be1e5817c..da96ee506d 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -5079,7 +5079,7 @@ SRST they are specified. Note that the 'id' property must be set. These objects are placed in the '/objects' path. =20 - ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off,prealloc=3Don|= off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|interleave,alig= n=3Dalign,offset=3Doffset,readonly=3Don|off,rom=3Don|off|auto`` + ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off,prealloc=3Don|= off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|interleave,alig= n=3Dalign,offset=3Doffset,readonly=3Don|off,rom=3Don|off|auto,shm=3Don|off`` Creates a memory file backend object, which can be used to back the guest RAM with huge pages. =20 @@ -5183,6 +5183,14 @@ SRST (``share=3Doff``). For this use case, we need writable RAM instead of ROM, and want to also set ``rom=3Doff``. =20 + The ``shm`` option specifies whether to create/open a POSIX shared + memory object identified by ``mem-path``. + If set to ``on``, use shm_open(3); if set to ``off`` (default), + use open(2); For portable use, a shared memory object should be + identified by a name of the form ``/somename``; consisting of an + initial slash, followed by one or more characters, none of which + are slashes. + ``-object memory-backend-ram,id=3Did,merge=3Don|off,dump=3Don|off,shar= e=3Don|off,prealloc=3Don|off,size=3Dsize,host-nodes=3Dhost-nodes,policy=3Dd= efault|preferred|bind|interleave`` Creates a memory backend object, which can be used to back the guest RAM. Memory backend objects offer more control than the --=20 2.43.2