From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 3A018EB8FAF for ; Wed, 6 Sep 2023 07:43:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qdnBY-0005xY-M0; Wed, 06 Sep 2023 03:42:48 -0400 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 1qdnBW-0005x2-9C for qemu-devel@nongnu.org; Wed, 06 Sep 2023 03:42:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qdnBS-0004pX-Ng for qemu-devel@nongnu.org; Wed, 06 Sep 2023 03:42:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693986161; 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: in-reply-to:in-reply-to:references:references; bh=ImRzxhsTOwyWbSjOwu2IdQnURQvv3rHMe4eORJwyH2I=; b=By1pC+kXZTHD18ighSLQyVNf9V1vabvX9o/rxkxKTQBFvZ/1ky8fFLfsnxZtkOjjBperWR oVeh0Y/0ILoAEGmu2X7+erU2yxys8XYL8xUgO3qN29lpCaGvtmr4SjH44S10NwgHPJ7d5G m2mQ8tR1qKn3ifICpSrE1oN7xow3kC8= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-191-8p9Xc0G0Mviz0rxcwXPqHw-1; Wed, 06 Sep 2023 03:42:39 -0400 X-MC-Unique: 8p9Xc0G0Mviz0rxcwXPqHw-1 Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-26f97567cb8so822976a91.0 for ; Wed, 06 Sep 2023 00:42:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693986158; x=1694590958; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ImRzxhsTOwyWbSjOwu2IdQnURQvv3rHMe4eORJwyH2I=; b=RZfqWWzSYT/gRPRN8fdcpQPREliloW5I6zeWwYfokJ9bA49E3Y0dOMBL4kUa+LbrY6 NEY2d13ZUVKJ9JLZbso9whqYNV9E6WZK5XPDwTqPdEDQANAzqNp67bYQCfn5YuHKZz6X 2V7yvyeGzdZ4TZc0w5iguu1uIW7OEyDGy2FqKMjTHFR8zdJfAlIRfdI9ZguVlYjv2SjU Z4/+f4mh7ZAVM+FZfn0l4AHFpxpBB/jN5GJL3ObnHIcIVhNzS6vevnF/DDe7404Rbqvz A45NaHcdn5/cQXTQGHOt92Z6z0Q0bNyYIJ31T2VRHDrq2E3/iG+A4nuUEMOovu8ufWNQ BovQ== X-Gm-Message-State: AOJu0YzZM9t6Jpy7965+ABSeWeawQPpKNPKj4vYNczCDxfNAn5QyXQGu Yu7riCgkZkpqCeNbwSTmPQrSM9GNsLxcDPMboiEu/d333BrSStsPrO/hgb7A68sueOEGF7ICNCb bIQDvisTqvVIUj7gBgdQZMQmha190mbI= X-Received: by 2002:a17:90a:b947:b0:26b:455b:8d61 with SMTP id f7-20020a17090ab94700b0026b455b8d61mr19627210pjw.22.1693986158044; Wed, 06 Sep 2023 00:42:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBSFKYZjr7ZnZGenc6vqMl0PAeD78SQm+7gO4KOx6owWqRVArHQl15AzHGmKz6Z6S1WLpH05mVqGbNXZ7RyW0= X-Received: by 2002:a17:90a:b947:b0:26b:455b:8d61 with SMTP id f7-20020a17090ab94700b0026b455b8d61mr19627199pjw.22.1693986157743; Wed, 06 Sep 2023 00:42:37 -0700 (PDT) MIME-Version: 1.0 References: <20230802090824.91688-1-aesteve@redhat.com> <20230802090824.91688-3-aesteve@redhat.com> In-Reply-To: From: Albert Esteve Date: Wed, 6 Sep 2023 09:42:26 +0200 Message-ID: Subject: Re: [PATCH v5 2/4] virtio-dmabuf: introduce virtio-dmabuf To: =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= Cc: qemu-devel@nongnu.org, marcandre.lureau@gmail.com, kraxel@redhat.com, cohuck@redhat.com, Fam Zheng , "Michael S. Tsirkin" Content-Type: multipart/alternative; boundary="0000000000002fe8440604abe35e" Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --0000000000002fe8440604abe35e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Sep 6, 2023 at 7:56=E2=80=AFAM Philippe Mathieu-Daud=C3=A9 wrote: > Hi Albert, > > On 2/8/23 11:08, Albert Esteve wrote: > > This API manages objects (in this iteration, > > dmabuf fds) that can be shared along different > > virtio devices, associated to a UUID. > > > > The API allows the different devices to add, > > remove and/or retrieve the objects by simply > > invoking the public functions that reside in the > > virtio-dmabuf file. > > > > For vhost backends, the API stores the pointer > > to the backend holding the object. > > > > Suggested-by: Gerd Hoffmann > > Signed-off-by: Albert Esteve > > --- > > MAINTAINERS | 7 ++ > > hw/display/meson.build | 1 + > > hw/display/virtio-dmabuf.c | 136 +++++++++++++++++++++++++++++ > > include/hw/virtio/virtio-dmabuf.h | 103 ++++++++++++++++++++++ > > tests/unit/meson.build | 1 + > > tests/unit/test-virtio-dmabuf.c | 137 +++++++++++++++++++++++++++++= + > > 6 files changed, 385 insertions(+) > > create mode 100644 hw/display/virtio-dmabuf.c > > create mode 100644 include/hw/virtio/virtio-dmabuf.h > > create mode 100644 tests/unit/test-virtio-dmabuf.c > > > > diff --git a/MAINTAINERS b/MAINTAINERS > > index 12e59b6b27..cd8487785a 100644 > > --- a/MAINTAINERS > > +++ b/MAINTAINERS > > @@ -2158,6 +2158,13 @@ T: git https://gitlab.com/cohuck/qemu.git > s390-next > > T: git https://github.com/borntraeger/qemu.git s390-next > > L: qemu-s390x@nongnu.org > > > > +virtio-dmabuf > > +M: Albert Esteve > > +S: Supported > > +F: hw/display/virtio-dmabuf.c > > +F: include/hw/virtio/virtio-dmabuf.h > > +F: tests/unit/test-virtio-dmabuf.c > > + > > virtiofs > > M: Stefan Hajnoczi > > S: Supported > > diff --git a/hw/display/meson.build b/hw/display/meson.build > > index 413ba4ab24..05619c6968 100644 > > --- a/hw/display/meson.build > > +++ b/hw/display/meson.build > > @@ -37,6 +37,7 @@ system_ss.add(when: 'CONFIG_MACFB', if_true: > files('macfb.c')) > > system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) > > > > system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) > > +system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')= ) > > > > if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') or > > config_all_devices.has_key('CONFIG_VGA_PCI') or > > diff --git a/hw/display/virtio-dmabuf.c b/hw/display/virtio-dmabuf.c > > new file mode 100644 > > index 0000000000..e852c71ba9 > > --- /dev/null > > +++ b/hw/display/virtio-dmabuf.c > > @@ -0,0 +1,136 @@ > > +/* > > + * Virtio Shared dma-buf > > + * > > + * Copyright Red Hat, Inc. 2023 > > + * > > + * Authors: > > + * Albert Esteve > > + * > > + * This work is licensed under the terms of the GNU GPL, version 2 or > later. > > + * See the COPYING file in the top-level directory. > > + */ > > + > > +#include "hw/virtio/virtio-dmabuf.h" > > + > > + > > +static GMutex lock; > > +static GHashTable *resource_uuids; > > + > > +/* > > + * uuid_equal_func: wrapper for UUID is_equal function to > > + * satisfy g_hash_table_new expected parameters signatures. > > + */ > > +static int uuid_equal_func(const void *lhv, const void *rhv) > > +{ > > + return qemu_uuid_is_equal(lhv, rhv); > > +} > > + > > +static bool virtio_add_resource(QemuUUID *uuid, struct > VirtioSharedObject *value) > > Per QEMU coding style we use typedefs, so "VirtioSharedObject" here. > > > +{ > > + if (resource_uuids =3D=3D NULL) { > > + resource_uuids =3D g_hash_table_new_full( > > + qemu_uuid_hash, uuid_equal_func, NULL, g_free); > > + } > > + if (g_hash_table_lookup(resource_uuids, uuid) !=3D NULL) { > > + return false; > > + } > > + > > + return g_hash_table_insert(resource_uuids, uuid, value); > > Hmm shouldn't this function take the lock to access resource_uuids? > > > +} > > + > > +static gpointer virtio_lookup_resource(const QemuUUID *uuid) > > +{ > > + if (resource_uuids =3D=3D NULL) { > > + return NULL; > > + } > > + > > + return g_hash_table_lookup(resource_uuids, uuid); > > Ditto. > > Here you can directly return the casted type (VirtioSharedObject *), > since a plain gpointer isn't really used / useful. > > > +} > > + > > +bool virtio_add_dmabuf(QemuUUID *uuid, int udmabuf_fd) > > +{ > > + bool result; > > + struct VirtioSharedObject *vso; > > + if (udmabuf_fd < 0) { > > + return false; > > + } > > + vso =3D g_new0(struct VirtioSharedObject, 1); > > s/g_new0/g_new/ > > > + g_mutex_lock(&lock); > > + vso->type =3D TYPE_DMABUF; > > + vso->value =3D GINT_TO_POINTER(udmabuf_fd); > > + result =3D virtio_add_resource(uuid, vso); > > + g_mutex_unlock(&lock); > > + > > + return result; > > +} > > + > > +bool virtio_add_vhost_device(QemuUUID *uuid, struct vhost_dev *dev) > > +{ > > + bool result; > > + struct VirtioSharedObject *vso; > > + if (dev =3D=3D NULL) { > > + return false; > > + } > > + vso =3D g_new0(struct VirtioSharedObject, 1); > > + g_mutex_lock(&lock); > > + vso->type =3D TYPE_VHOST_DEV; > > + vso->value =3D dev; > > + result =3D virtio_add_resource(uuid, vso); > > Ah, you lock here... I'd rather do it in the callee. > > > + g_mutex_unlock(&lock); > > + > > + return result; > > +} > > + > > +bool virtio_remove_resource(const QemuUUID *uuid) > > +{ > > + bool result; > > + g_mutex_lock(&lock); > > + result =3D g_hash_table_remove(resource_uuids, uuid); > > + g_mutex_unlock(&lock); > > virtio_remove_resource() correctly locks. For API parity, > virtio_add_resource() should too. > > > + > > + return result; > > +} > > + > > +static struct VirtioSharedObject *get_shared_object(const QemuUUID > *uuid) > > +{ > > + g_mutex_lock(&lock); > > + gpointer lookup_res =3D virtio_lookup_resource(uuid); > > + g_mutex_unlock(&lock); > > + return (struct VirtioSharedObject*) lookup_res; > > See earlier, this function can be merged with virtio_lookup_resource(). > > > +} > > + > > +int virtio_lookup_dmabuf(const QemuUUID *uuid) > > +{ > > + struct VirtioSharedObject *vso =3D get_shared_object(uuid); > > + if (vso =3D=3D NULL) { > > + return -1; > > + } > > + assert(vso->type =3D=3D TYPE_DMABUF); > > + return GPOINTER_TO_INT(vso->value); > > +} > > + > > +struct vhost_dev *virtio_lookup_vhost_device(const QemuUUID *uuid) > > +{ > > + struct VirtioSharedObject *vso =3D get_shared_object(uuid); > > + if (vso =3D=3D NULL) { > > + return NULL; > > + } > > + assert(vso->type =3D=3D TYPE_VHOST_DEV); > > + return (struct vhost_dev *) vso->value; > > +} > > + > > +enum SharedObjectType virtio_object_type(const QemuUUID *uuid) > > +{ > > + struct VirtioSharedObject *vso =3D get_shared_object(uuid); > > + if (vso =3D=3D NULL) { > > + return TYPE_INVALID; > > + } > > + return vso->type; > > +} > > + > > +void virtio_free_resources(void) > > +{ > > + g_hash_table_destroy(resource_uuids); > > Lock? > > > + /* Reference count shall be 0 after the implicit unref on destroy = */ > > + resource_uuids =3D NULL; > > +} > > diff --git a/include/hw/virtio/virtio-dmabuf.h > b/include/hw/virtio/virtio-dmabuf.h > > new file mode 100644 > > index 0000000000..536e622555 > > --- /dev/null > > +++ b/include/hw/virtio/virtio-dmabuf.h > > @@ -0,0 +1,103 @@ > > +/* > > + * Virtio Shared dma-buf > > + * > > + * Copyright Red Hat, Inc. 2023 > > + * > > + * Authors: > > + * Albert Esteve > > + * > > + * This work is licensed under the terms of the GNU GPL, version 2. > > + * See the COPYING file in the top-level directory. > > + */ > > + > > +#ifndef VIRTIO_DMABUF_H > > +#define VIRTIO_DMABUF_H > > + > > +#include "qemu/osdep.h" > > + > > +#include > > +#include "qemu/uuid.h" > > +#include "vhost.h" > > + > > +enum SharedObjectType { > > + TYPE_INVALID =3D 0, > > + TYPE_DMABUF, > > + TYPE_VHOST_DEV, > > +}; > > + > > Please declare a > > typedef > > > +struct VirtioSharedObject { > > + enum SharedObjectType type; > > + gpointer value; > > +}; > > VirtioSharedObject; > > and use it instead of 'struct VirtioSharedObject'. > > You mean making the struct anonymous and typedefing? Should I do the same with the enum? In other files I see enums are typedef too, but not anonymous (e.g., block/qcow2.h). So I could do the same here. For the rest... Ack! > Regards, > > Phil. > > --0000000000002fe8440604abe35e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Wed, Sep 6, 2023 at 7:56=E2=80=AFA= M Philippe Mathieu-Daud=C3=A9 <phil= md@linaro.org> wrote:
Hi Albert,

On 2/8/23 11:08, Albert Esteve wrote:
> This API manages objects (in this iteration,
> dmabuf fds) that can be shared along different
> virtio devices, associated to a UUID.
>
> The API allows the different devices to add,
> remove and/or retrieve the objects by simply
> invoking the public functions that reside in the
> virtio-dmabuf file.
>
> For vhost backends, the API stores the pointer
> to the backend holding the object.
>
> Suggested-by: Gerd Hoffmann <kraxel@redhat.com>
> Signed-off-by: Albert Esteve <aesteve@redhat.com>
> ---
>=C2=A0 =C2=A0MAINTAINERS=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A07 ++
>=C2=A0 =C2=A0hw/display/meson.build=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 =C2=A01 +
>=C2=A0 =C2=A0hw/display/virtio-dmabuf.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 | 13= 6 +++++++++++++++++++++++++++++
>=C2=A0 =C2=A0include/hw/virtio/virtio-dmabuf.h | 103 ++++++++++++++++++= ++++
>=C2=A0 =C2=A0tests/unit/meson.build=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 |=C2=A0 =C2=A01 +
>=C2=A0 =C2=A0tests/unit/test-virtio-dmabuf.c=C2=A0 =C2=A0| 137 ++++++++= ++++++++++++++++++++++
>=C2=A0 =C2=A06 files changed, 385 insertions(+)
>=C2=A0 =C2=A0create mode 100644 hw/display/virtio-dmabuf.c
>=C2=A0 =C2=A0create mode 100644 include/hw/virtio/virtio-dmabuf.h
>=C2=A0 =C2=A0create mode 100644 tests/unit/test-virtio-dmabuf.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 12e59b6b27..cd8487785a 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -2158,6 +2158,13 @@ T: git https://gitlab.com/cohuck/qemu.= git s390-next
>=C2=A0 =C2=A0T: git https://github.com/borntraeger/qemu.g= it s390-next
>=C2=A0 =C2=A0L: qemu-s390x@nongnu.org
>=C2=A0 =C2=A0
> +virtio-dmabuf
> +M: Albert Esteve <aesteve@redhat.com>
> +S: Supported
> +F: hw/display/virtio-dmabuf.c
> +F: include/hw/virtio/virtio-dmabuf.h
> +F: tests/unit/test-virtio-dmabuf.c
> +
>=C2=A0 =C2=A0virtiofs
>=C2=A0 =C2=A0M: Stefan Hajnoczi <stefanha@redhat.com>
>=C2=A0 =C2=A0S: Supported
> diff --git a/hw/display/meson.build b/hw/display/meson.build
> index 413ba4ab24..05619c6968 100644
> --- a/hw/display/meson.build
> +++ b/hw/display/meson.build
> @@ -37,6 +37,7 @@ system_ss.add(when: 'CONFIG_MACFB', if_true:= files('macfb.c'))
>=C2=A0 =C2=A0system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: fi= les('next-fb.c'))
>=C2=A0 =C2=A0
>=C2=A0 =C2=A0system_ss.add(when: 'CONFIG_VGA', if_true: files(&= #39;vga.c'))
> +system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virt= io-dmabuf.c'))
>=C2=A0 =C2=A0
>=C2=A0 =C2=A0if (config_all_devices.has_key('CONFIG_VGA_CIRRUS'= ) or
>=C2=A0 =C2=A0 =C2=A0 =C2=A0config_all_devices.has_key('CONFIG_VGA_P= CI') or
> diff --git a/hw/display/virtio-dmabuf.c b/hw/display/virtio-dmabuf.c > new file mode 100644
> index 0000000000..e852c71ba9
> --- /dev/null
> +++ b/hw/display/virtio-dmabuf.c
> @@ -0,0 +1,136 @@
> +/*
> + * Virtio Shared dma-buf
> + *
> + * Copyright Red Hat, Inc. 2023
> + *
> + * Authors:
> + *=C2=A0 =C2=A0 =C2=A0Albert Esteve <aesteve@redhat.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or= later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include "hw/virtio/virtio-dmabuf.h"
> +
> +
> +static GMutex lock;
> +static GHashTable *resource_uuids;
> +
> +/*
> + * uuid_equal_func: wrapper for UUID is_equal function to
> + * satisfy g_hash_table_new expected parameters signatures.
> + */
> +static int uuid_equal_func(const void *lhv, const void *rhv)
> +{
> +=C2=A0 =C2=A0 return qemu_uuid_is_equal(lhv, rhv);
> +}
> +
> +static bool virtio_add_resource(QemuUUID *uuid, struct VirtioSharedOb= ject *value)

Per QEMU coding style we use typedefs, so "VirtioSharedObject" he= re.

> +{
> +=C2=A0 =C2=A0 if (resource_uuids =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 resource_uuids =3D g_hash_table_new_full(=
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 qemu_uuid_hash, uuid_equal_= func, NULL, g_free);
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 if (g_hash_table_lookup(resource_uuids, uuid) !=3D NULL= ) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return false;
> +=C2=A0 =C2=A0 }
> +
> +=C2=A0 =C2=A0 return g_hash_table_insert(resource_uuids, uuid, value)= ;

Hmm shouldn't this function take the lock to access resource_uuids?

> +}
> +
> +static gpointer virtio_lookup_resource(const QemuUUID *uuid)
> +{
> +=C2=A0 =C2=A0 if (resource_uuids =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return NULL;
> +=C2=A0 =C2=A0 }
> +
> +=C2=A0 =C2=A0 return g_hash_table_lookup(resource_uuids, uuid);

Ditto.

Here you can directly return the casted type (VirtioSharedObject *),
since a plain gpointer isn't really used / useful.

> +}
> +
> +bool virtio_add_dmabuf(QemuUUID *uuid, int udmabuf_fd)
> +{
> +=C2=A0 =C2=A0 bool result;
> +=C2=A0 =C2=A0 struct VirtioSharedObject *vso;
> +=C2=A0 =C2=A0 if (udmabuf_fd < 0) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return false;
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 vso =3D g_new0(struct VirtioSharedObject, 1);

s/g_new0/g_new/

> +=C2=A0 =C2=A0 g_mutex_lock(&lock);
> +=C2=A0 =C2=A0 vso->type =3D TYPE_DMABUF;
> +=C2=A0 =C2=A0 vso->value =3D GINT_TO_POINTER(udmabuf_fd);
> +=C2=A0 =C2=A0 result =3D virtio_add_resource(uuid, vso);
> +=C2=A0 =C2=A0 g_mutex_unlock(&lock);
> +
> +=C2=A0 =C2=A0 return result;
> +}
> +
> +bool virtio_add_vhost_device(QemuUUID *uuid, struct vhost_dev *dev) > +{
> +=C2=A0 =C2=A0 bool result;
> +=C2=A0 =C2=A0 struct VirtioSharedObject *vso;
> +=C2=A0 =C2=A0 if (dev =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return false;
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 vso =3D g_new0(struct VirtioSharedObject, 1);
> +=C2=A0 =C2=A0 g_mutex_lock(&lock);
> +=C2=A0 =C2=A0 vso->type =3D TYPE_VHOST_DEV;
> +=C2=A0 =C2=A0 vso->value =3D dev;
> +=C2=A0 =C2=A0 result =3D virtio_add_resource(uuid, vso);

Ah, you lock here... I'd rather do it in the callee.

> +=C2=A0 =C2=A0 g_mutex_unlock(&lock);
> +
> +=C2=A0 =C2=A0 return result;
> +}
> +
> +bool virtio_remove_resource(const QemuUUID *uuid)
> +{
> +=C2=A0 =C2=A0 bool result;
> +=C2=A0 =C2=A0 g_mutex_lock(&lock);
> +=C2=A0 =C2=A0 result =3D g_hash_table_remove(resource_uuids, uuid); > +=C2=A0 =C2=A0 g_mutex_unlock(&lock);

virtio_remove_resource() correctly locks. For API parity,
virtio_add_resource() should too.

> +
> +=C2=A0 =C2=A0 return result;
> +}
> +
> +static struct VirtioSharedObject *get_shared_object(const QemuUUID *u= uid)
> +{
> +=C2=A0 =C2=A0 g_mutex_lock(&lock);
> +=C2=A0 =C2=A0 gpointer lookup_res =3D virtio_lookup_resource(uuid); > +=C2=A0 =C2=A0 g_mutex_unlock(&lock);
> +=C2=A0 =C2=A0 return (struct VirtioSharedObject*) lookup_res;

See earlier, this function can be merged with virtio_lookup_resource().

> +}
> +
> +int virtio_lookup_dmabuf(const QemuUUID *uuid)
> +{
> +=C2=A0 =C2=A0 struct VirtioSharedObject *vso =3D get_shared_object(uu= id);
> +=C2=A0 =C2=A0 if (vso =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return -1;
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 assert(vso->type =3D=3D TYPE_DMABUF);
> +=C2=A0 =C2=A0 return GPOINTER_TO_INT(vso->value);
> +}
> +
> +struct vhost_dev *virtio_lookup_vhost_device(const QemuUUID *uuid) > +{
> +=C2=A0 =C2=A0 struct VirtioSharedObject *vso =3D get_shared_object(uu= id);
> +=C2=A0 =C2=A0 if (vso =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return NULL;
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 assert(vso->type =3D=3D TYPE_VHOST_DEV);
> +=C2=A0 =C2=A0 return (struct vhost_dev *) vso->value;
> +}
> +
> +enum SharedObjectType virtio_object_type(const QemuUUID *uuid)
> +{
> +=C2=A0 =C2=A0 struct VirtioSharedObject *vso =3D get_shared_object(uu= id);
> +=C2=A0 =C2=A0 if (vso =3D=3D NULL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return TYPE_INVALID;
> +=C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 return vso->type;
> +}
> +
> +void virtio_free_resources(void)
> +{
> +=C2=A0 =C2=A0 g_hash_table_destroy(resource_uuids);

Lock?

> +=C2=A0 =C2=A0 /* Reference count shall be 0 after the implicit unref = on destroy */
> +=C2=A0 =C2=A0 resource_uuids =3D NULL;
> +}
> diff --git a/include/hw/virtio/virtio-dmabuf.h b/include/hw/virtio/vir= tio-dmabuf.h
> new file mode 100644
> index 0000000000..536e622555
> --- /dev/null
> +++ b/include/hw/virtio/virtio-dmabuf.h
> @@ -0,0 +1,103 @@
> +/*
> + * Virtio Shared dma-buf
> + *
> + * Copyright Red Hat, Inc. 2023
> + *
> + * Authors:
> + *=C2=A0 =C2=A0 =C2=A0Albert Esteve <aesteve@redhat.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2. > + * See the COPYING file in the top-level directory.
> + */
> +
> +#ifndef VIRTIO_DMABUF_H
> +#define VIRTIO_DMABUF_H
> +
> +#include "qemu/osdep.h"
> +
> +#include <glib.h>
> +#include "qemu/uuid.h"
> +#include "vhost.h"
> +
> +enum SharedObjectType {
> +=C2=A0 =C2=A0 TYPE_INVALID =3D 0,
> +=C2=A0 =C2=A0 TYPE_DMABUF,
> +=C2=A0 =C2=A0 TYPE_VHOST_DEV,
> +};
> +

Please declare a

typedef

> +struct VirtioSharedObject {
> +=C2=A0 =C2=A0 enum SharedObjectType type;
> +=C2=A0 =C2=A0 gpointer value;
> +};

VirtioSharedObject;

and use it instead of 'struct VirtioSharedObject'.


You mean making the struct anonymous a= nd typedefing?
Should I do the same with the enum? In other files= I see enums are typedef too, but not anonymous (e.g., block/qcow2.h).
So I could do the same here.

For the rest...= Ack!
=C2=A0
Regards,

Phil.

--0000000000002fe8440604abe35e--