From nobody Thu Nov 14 17:49:01 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=1708353379; cv=none; d=zohomail.com; s=zohoarc; b=FlZEuL1+StLiHCGikx73pymiKQmjs2XY7N2iRIGtzH4ry4TiOF0bJCaSZXE3bQXBGsIpPOcMB8aMDn7Yh3oI24dmSsYKN0YtnuDgLZ4FSqx6brIvwEneFO0zNSXmtCPud+XsWQtm9Y1VfR4zj3Nmc9pzCDu3UMGLhj0IS2P09BQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708353379; h=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=98yhwqbsidcnwnpY+o364o71w+Bbdr+rkpAcCec0C3k=; b=av9xK52MOOKoV6A2xcJyFf/BvE6DrcW8KWp2m43ImnVjffGtJKsY4jNTuO+Zu/kMlJ6dblIx//dZPdRxoSuhQWWaf/V6NpU2E6to8jhk/PKLob/pbm5cVeYpLJpRBgPaGDHfMSvlYZ5rRDvg9t1q2KaEJR7iOSEaeZvU2Gax8A8= 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 1708353379401303.25864269890167; Mon, 19 Feb 2024 06:36:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rc4jN-0007jb-AX; Mon, 19 Feb 2024 09:34:53 -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 1rc4jG-0007iP-7w for qemu-devel@nongnu.org; Mon, 19 Feb 2024 09:34:46 -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 1rc4jE-0003rJ-6t for qemu-devel@nongnu.org; Mon, 19 Feb 2024 09:34:45 -0500 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-488-qUchfjb8PxKuwrLLrO8_UA-1; Mon, 19 Feb 2024 09:34:40 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9B68D3830092; Mon, 19 Feb 2024 14:34:39 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.250]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E799200B2D7; Mon, 19 Feb 2024 14:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708353283; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=98yhwqbsidcnwnpY+o364o71w+Bbdr+rkpAcCec0C3k=; b=ctil9gSJmVtBHTCW7g9f/wlTBghPEW31/sTeTYGFUFeRnv0STmEUOB08hb1dhgTUe619ee FZA8FAP5US5qOiWEkvfiTk1kVTUihpuP4Fvt5URaXHj5buQGIbXr62jC6E9p1ARnBBXBgs N0qP2HZYusEtXA6AMG6l2NLmky7UfSE= X-MC-Unique: qUchfjb8PxKuwrLLrO8_UA-1 From: Albert Esteve To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , stefanha@gmail.com, alex.bennee@linaro.org, philmd@linaro.org, kraxel@redhat.com, marcandre.lureau@gmail.com, Albert Esteve Subject: [PATCH v4 3/5] hw/virtio: change dmabuf mutex to QemuMutex Date: Mon, 19 Feb 2024 15:34:21 +0100 Message-ID: <20240219143423.272012-4-aesteve@redhat.com> In-Reply-To: <20240219143423.272012-1-aesteve@redhat.com> References: <20240219143423.272012-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 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=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.072, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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, T_SCC_BODY_TEXT_LINE=-0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1708353380142100003 Content-Type: text/plain; charset="utf-8" Change GMutex by QemuMutex to be able to use lock contexts with `WITH_QEMU_LOCK_GUARD`. As the lock needs to be initialised and there is no central point for initialisation, add an init public function and call it from virtio.c, each time a new backend structure is initialised. Signed-off-by: Albert Esteve --- hw/display/virtio-dmabuf.c | 55 +++++++++++++++++-------------- hw/virtio/virtio.c | 3 ++ include/hw/virtio/virtio-dmabuf.h | 5 +++ tests/unit/test-virtio-dmabuf.c | 5 +++ 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/hw/display/virtio-dmabuf.c b/hw/display/virtio-dmabuf.c index 497cb6fa7c..961094a561 100644 --- a/hw/display/virtio-dmabuf.c +++ b/hw/display/virtio-dmabuf.c @@ -11,11 +11,12 @@ */ =20 #include "qemu/osdep.h" +#include "include/qemu/lockable.h" =20 #include "hw/virtio/virtio-dmabuf.h" =20 =20 -static GMutex lock; +static QemuMutex lock; static GHashTable *resource_uuids; =20 /* @@ -27,23 +28,27 @@ static int uuid_equal_func(const void *lhv, const void = *rhv) return qemu_uuid_is_equal(lhv, rhv); } =20 +void virtio_dmabuf_init(void) { + qemu_mutex_init(&lock); +} + static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) { bool result =3D true; =20 - g_mutex_lock(&lock); - 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=3D NULL) { - g_hash_table_insert(resource_uuids, uuid, value); - } else { - result =3D false; + WITH_QEMU_LOCK_GUARD(&lock) { + 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=3D NULL) { + g_hash_table_insert(resource_uuids, uuid, value); + } else { + result =3D false; + } } - g_mutex_unlock(&lock); =20 return result; } @@ -87,9 +92,9 @@ bool virtio_add_vhost_device(QemuUUID *uuid, struct vhost= _dev *dev) 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); + WITH_QEMU_LOCK_GUARD(&lock) { + result =3D g_hash_table_remove(resource_uuids, uuid); + } =20 return result; } @@ -98,11 +103,11 @@ static VirtioSharedObject *get_shared_object(const Qem= uUUID *uuid) { gpointer lookup_res =3D NULL; =20 - g_mutex_lock(&lock); - if (resource_uuids !=3D NULL) { - lookup_res =3D g_hash_table_lookup(resource_uuids, uuid); + WITH_QEMU_LOCK_GUARD(&lock) { + if (resource_uuids !=3D NULL) { + lookup_res =3D g_hash_table_lookup(resource_uuids, uuid); + } } - g_mutex_unlock(&lock); =20 return (VirtioSharedObject *) lookup_res; } @@ -138,9 +143,9 @@ SharedObjectType virtio_object_type(const QemuUUID *uui= d) =20 void virtio_free_resources(void) { - g_mutex_lock(&lock); - g_hash_table_destroy(resource_uuids); - /* Reference count shall be 0 after the implicit unref on destroy */ - resource_uuids =3D NULL; - g_mutex_unlock(&lock); + WITH_QEMU_LOCK_GUARD(&lock) { + g_hash_table_destroy(resource_uuids); + /* Reference count shall be 0 after the implicit unref on destroy = */ + resource_uuids =3D NULL; + } } diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index d229755eae..88189e7178 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -29,6 +29,7 @@ #include "hw/virtio/virtio-bus.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio-access.h" +#include "hw/virtio/virtio-dmabuf.h" #include "sysemu/dma.h" #include "sysemu/runstate.h" #include "virtio-qmp.h" @@ -3221,6 +3222,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_= id, size_t config_size) int i; int nvectors =3D k->query_nvectors ? k->query_nvectors(qbus->parent) := 0; =20 + // Ensure virtio dmabuf table is initialised. + virtio_dmabuf_init(); if (nvectors) { vdev->vector_queues =3D g_malloc0(sizeof(*vdev->vector_queues) * nvectors); diff --git a/include/hw/virtio/virtio-dmabuf.h b/include/hw/virtio/virtio-d= mabuf.h index 891a43162d..627d84dce9 100644 --- a/include/hw/virtio/virtio-dmabuf.h +++ b/include/hw/virtio/virtio-dmabuf.h @@ -50,6 +50,11 @@ typedef struct VirtioSharedObject { } value; } VirtioSharedObject; =20 +/** + * virtio_dmabuf_init() - Initialise virtio dmabuf internal structures. + */ +void virtio_dmabuf_init(void); + /** * virtio_add_dmabuf() - Add a new dma-buf resource to the lookup table * @uuid: new resource's UUID diff --git a/tests/unit/test-virtio-dmabuf.c b/tests/unit/test-virtio-dmabu= f.c index a45ec52f42..20213455ee 100644 --- a/tests/unit/test-virtio-dmabuf.c +++ b/tests/unit/test-virtio-dmabuf.c @@ -27,6 +27,7 @@ static void test_add_remove_resources(void) QemuUUID uuid; int i, dmabuf_fd; =20 + virtio_dmabuf_init(); for (i =3D 0; i < 100; ++i) { qemu_uuid_generate(&uuid); dmabuf_fd =3D g_random_int_range(3, 500); @@ -46,6 +47,7 @@ static void test_add_remove_dev(void) struct vhost_dev *dev =3D g_new0(struct vhost_dev, 1); int i; =20 + virtio_dmabuf_init(); for (i =3D 0; i < 100; ++i) { qemu_uuid_generate(&uuid); virtio_add_vhost_device(&uuid, dev); @@ -64,6 +66,7 @@ static void test_remove_invalid_resource(void) QemuUUID uuid; int i; =20 + virtio_dmabuf_init(); for (i =3D 0; i < 20; ++i) { qemu_uuid_generate(&uuid); g_assert_cmpint(virtio_lookup_dmabuf(&uuid), =3D=3D, -1); @@ -78,6 +81,7 @@ static void test_add_invalid_resource(void) struct vhost_dev *dev =3D NULL; int i, dmabuf_fd =3D -2, alt_dmabuf =3D 2; =20 + virtio_dmabuf_init(); for (i =3D 0; i < 20; ++i) { qemu_uuid_generate(&uuid); /* Add a new resource with invalid (negative) resource fd */ @@ -108,6 +112,7 @@ static void test_free_resources(void) QemuUUID uuids[20]; int i, dmabuf_fd; =20 + virtio_dmabuf_init(); for (i =3D 0; i < ARRAY_SIZE(uuids); ++i) { qemu_uuid_generate(&uuids[i]); dmabuf_fd =3D g_random_int_range(3, 500); --=20 2.43.1