From: Albert Esteve <aesteve@redhat.com>
To: qemu-devel@nongnu.org
Cc: stefanha@gmail.com, Albert Esteve <aesteve@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
marcandre.lureau@gmail.com, kraxel@redhat.com,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [PATCH v3 2/3] hw/virtio: cleanup shared resources
Date: Tue, 9 Jan 2024 13:56:13 +0100 [thread overview]
Message-ID: <20240109125614.220293-3-aesteve@redhat.com> (raw)
In-Reply-To: <20240109125614.220293-1-aesteve@redhat.com>
Ensure that we cleanup all virtio shared
resources when the vhost devices is cleaned
up (after a hot unplug, or a crash).
To do so, we add a new function to the virtio_dmabuf
API called `virtio_dmabuf_vhost_cleanup`, which
loop through the table and removes all
resources owned by the vhost device parameter.
Also, add a test to verify that the new
function in the API behaves as expected.
Signed-off-by: Albert Esteve <aesteve@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
---
hw/display/virtio-dmabuf.c | 22 +++++++++++++++++++++
hw/virtio/vhost.c | 3 +++
include/hw/virtio/virtio-dmabuf.h | 10 ++++++++++
tests/unit/test-virtio-dmabuf.c | 33 +++++++++++++++++++++++++++++++
4 files changed, 68 insertions(+)
diff --git a/hw/display/virtio-dmabuf.c b/hw/display/virtio-dmabuf.c
index 3dba4577ca..6688809777 100644
--- a/hw/display/virtio-dmabuf.c
+++ b/hw/display/virtio-dmabuf.c
@@ -136,6 +136,28 @@ SharedObjectType virtio_object_type(const QemuUUID *uuid)
return vso->type;
}
+static bool virtio_dmabuf_resource_is_owned(gpointer key,
+ gpointer value,
+ gpointer dev)
+{
+ VirtioSharedObject *vso;
+
+ vso = (VirtioSharedObject *) value;
+ return vso->type == TYPE_VHOST_DEV && vso->value == dev;
+}
+
+int virtio_dmabuf_vhost_cleanup(struct vhost_dev *dev)
+{
+ int num_removed;
+
+ g_mutex_lock(&lock);
+ num_removed = g_hash_table_foreach_remove(
+ resource_uuids, (GHRFunc) virtio_dmabuf_resource_is_owned, dev);
+ g_mutex_unlock(&lock);
+
+ return num_removed;
+}
+
void virtio_free_resources(void)
{
g_mutex_lock(&lock);
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 2c9ac79468..c5622eac14 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -16,6 +16,7 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "hw/virtio/vhost.h"
+#include "hw/virtio/virtio-dmabuf.h"
#include "qemu/atomic.h"
#include "qemu/range.h"
#include "qemu/error-report.h"
@@ -1599,6 +1600,8 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
migrate_del_blocker(&hdev->migration_blocker);
g_free(hdev->mem);
g_free(hdev->mem_sections);
+ /* free virtio shared objects */
+ virtio_dmabuf_vhost_cleanup(hdev);
if (hdev->vhost_ops) {
hdev->vhost_ops->vhost_backend_cleanup(hdev);
}
diff --git a/include/hw/virtio/virtio-dmabuf.h b/include/hw/virtio/virtio-dmabuf.h
index 627c3b6db7..73f70fb482 100644
--- a/include/hw/virtio/virtio-dmabuf.h
+++ b/include/hw/virtio/virtio-dmabuf.h
@@ -91,6 +91,16 @@ struct vhost_dev *virtio_lookup_vhost_device(const QemuUUID *uuid);
*/
SharedObjectType virtio_object_type(const QemuUUID *uuid);
+/**
+ * virtio_dmabuf_vhost_cleanup() - Destroys all entries of the shared
+ * resources lookup table that are owned by the vhost backend
+ * @dev: the pointer to the vhost device that owns the entries. Data is owned
+ * by the called of the function.
+ *
+ * Return: the number of resource entries removed.
+ */
+int virtio_dmabuf_vhost_cleanup(struct vhost_dev *dev);
+
/**
* virtio_free_resources() - Destroys all keys and values of the shared
* resources lookup table, and frees them
diff --git a/tests/unit/test-virtio-dmabuf.c b/tests/unit/test-virtio-dmabuf.c
index a45ec52f42..1c8123c2d2 100644
--- a/tests/unit/test-virtio-dmabuf.c
+++ b/tests/unit/test-virtio-dmabuf.c
@@ -103,6 +103,38 @@ static void test_add_invalid_resource(void)
}
}
+static void test_cleanup_res(void)
+{
+ QemuUUID uuids[20], uuid_alt;
+ struct vhost_dev *dev = g_new0(struct vhost_dev, 1);
+ struct vhost_dev *dev_alt = g_new0(struct vhost_dev, 1);
+ int i, num_removed;
+
+ for (i = 0; i < ARRAY_SIZE(uuids); ++i) {
+ qemu_uuid_generate(&uuids[i]);
+ virtio_add_vhost_device(&uuids[i], dev);
+ /* vhost device is found */
+ g_assert(virtio_lookup_vhost_device(&uuids[i]) != NULL);
+ }
+ qemu_uuid_generate(&uuid_alt);
+ virtio_add_vhost_device(&uuid_alt, dev_alt);
+ /* vhost device is found */
+ g_assert(virtio_lookup_vhost_device(&uuid_alt) != NULL);
+ /* cleanup all dev resources */
+ num_removed = virtio_dmabuf_vhost_cleanup(dev);
+ g_assert_cmpint(num_removed, ==, ARRAY_SIZE(uuids));
+ for (i = 0; i < ARRAY_SIZE(uuids); ++i) {
+ /* None of the dev resources is found after free'd */
+ g_assert_cmpint(virtio_lookup_dmabuf(&uuids[i]), ==, -1);
+ }
+ /* uuid_alt is still in the hash table */
+ g_assert(virtio_lookup_vhost_device(&uuid_alt) != NULL);
+
+ virtio_free_resources();
+ g_free(dev);
+ g_free(dev_alt);
+}
+
static void test_free_resources(void)
{
QemuUUID uuids[20];
@@ -131,6 +163,7 @@ int main(int argc, char **argv)
test_remove_invalid_resource);
g_test_add_func("/virtio-dmabuf/add_invalid_res",
test_add_invalid_resource);
+ g_test_add_func("/virtio-dmabuf/cleanup_dev", test_cleanup_res);
g_test_add_func("/virtio-dmabuf/free_res", test_free_resources);
return g_test_run();
--
2.43.0
next prev parent reply other threads:[~2024-01-09 12:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-09 12:56 [PATCH v3 0/3] Virtio dmabuf improvements Albert Esteve
2024-01-09 12:56 ` [PATCH v3 1/3] hw/virtio: check owner for removing objects Albert Esteve
2024-02-05 12:57 ` Alex Bennée
2024-02-15 9:37 ` Albert Esteve
2024-01-09 12:56 ` Albert Esteve [this message]
2024-02-05 23:11 ` [PATCH v3 2/3] hw/virtio: cleanup shared resources Alex Bennée
2024-02-15 9:45 ` Albert Esteve
2024-02-15 11:41 ` Alex Bennée
2024-02-19 10:45 ` Albert Esteve
2024-02-19 12:07 ` Albert Esteve
2024-01-09 12:56 ` [PATCH v3 3/3] hw/virtio: rename virtio dmabuf API Albert Esteve
2024-01-09 21:14 ` Philippe Mathieu-Daudé
2024-02-05 9:58 ` [PATCH v3 0/3] Virtio dmabuf improvements Albert Esteve
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240109125614.220293-3-aesteve@redhat.com \
--to=aesteve@redhat.com \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).