From nobody Mon Feb 9 19:04:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1644359658; cv=none; d=zohomail.com; s=zohoarc; b=cCl6bIHDZYYkO5I03EcLAh3MLia3CIoJfpyOqgKfEa/Y5IUC11H5GZF4HPKxpoICF41my4ORGdBYT61MmRe+Uv6ZzqXy3C7UqL33zQW4fLdNvc+xpmQFAoRjTbXcUxcR5nomwW0BkPDdx+PFtU2Sg5IwOJLhyhUbpKQ+Q9YP5os= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1644359658; 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=xmi6zvi4U2Q0Cxcw/qS++ZPanG5ZEx4N6rXCkjqHu74=; b=CJPSLWxxZaLDqPDA513/mKn34VRx+QbIeDq+sCjLlqPnY9vrPuw98LunDonHYH/jCG7n8Pam4CvtMvgP7DO7AS4Nll/8JU4AM8Uyi/xuK862ycinBiVUUkJ5dwVYgj6bn7V+TJxPYKNsAxIcRq6UqHr4OVl2ZZC92YTe9f8W8YU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1644359658463234.94013692838212; Tue, 8 Feb 2022 14:34:18 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-340-93dBNi-NP4WjZibhspi-XQ-1; Tue, 08 Feb 2022 17:34:13 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E6B1A1853033; Tue, 8 Feb 2022 22:34:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C460456F9D; Tue, 8 Feb 2022 22:34:07 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 96DAF59CB4; Tue, 8 Feb 2022 22:34:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 217DCSx8006397 for ; Mon, 7 Feb 2022 08:12:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id 007331038AC9; Mon, 7 Feb 2022 13:12:28 +0000 (UTC) Received: from work.redhat.com (unknown [10.39.192.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4BF531038AC4; Mon, 7 Feb 2022 13:12:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644359657; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=xmi6zvi4U2Q0Cxcw/qS++ZPanG5ZEx4N6rXCkjqHu74=; b=bFzdEB6hW6xd4sX1iEuHF2+tWnG/ftkmp6c7c+o6I1bHwSjEMSuMAzHb6Ctzkek4sfat14 cvVhqI2txy2oQ2uJWLmZGywf4kQDuhwWmriRvBjtFV3oRsRP8t3MRfoS5pxFu2BrkVbfLq 0XERDMBhCJii72/sMvXn3MAT1tQAh88= X-MC-Unique: 93dBNi-NP4WjZibhspi-XQ-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 11/11] node_device: Use automatic mutex management Date: Mon, 7 Feb 2022 14:12:14 +0100 Message-Id: <20220207131214.54855-12-twiederh@redhat.com> In-Reply-To: <20220207131214.54855-1-twiederh@redhat.com> References: <20220207131214.54855-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: Tim Wiederhake X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1644359659553100037 Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake --- src/node_device/node_device_driver.c | 23 +++--------- src/node_device/node_device_udev.c | 52 ++++++++++++---------------- 2 files changed, 26 insertions(+), 49 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index d19ed7d948..7b2fb3d953 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -156,33 +156,18 @@ nodeDeviceUpdateDriverName(virNodeDeviceDef *def G_GN= UC_UNUSED) #endif =20 =20 -void -nodeDeviceLock(void) -{ - virMutexLock(&driver->lock); -} - - -void -nodeDeviceUnlock(void) -{ - virMutexUnlock(&driver->lock); -} - - static int nodeDeviceInitWait(void) { - nodeDeviceLock(); + VIR_LOCK_GUARD lock =3D virLockGuardLock(&driver->lock); + while (!driver->initialized) { if (virCondWait(&driver->initCond, &driver->lock) < 0) { - virReportSystemError(errno, "%s", - _("failed to wait on condition")); - nodeDeviceUnlock(); + virReportSystemError(errno, "%s", _("failed to wait on conditi= on")); return -1; } } - nodeDeviceUnlock(); + return 0; } =20 diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index a9e8bf10da..3d5e25424a 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -93,9 +93,9 @@ udevEventDataDispose(void *obj) udev_monitor_unref(priv->udev_monitor); udev_unref(udev); =20 - virMutexLock(&priv->mdevctlLock); - g_list_free_full(priv->mdevctlMonitors, g_object_unref); - virMutexUnlock(&priv->mdevctlLock); + VIR_WITH_MUTEX_LOCK_GUARD(&priv->mdevctlLock) { + g_list_free_full(priv->mdevctlMonitors, g_object_unref); + } virMutexDestroy(&priv->mdevctlLock); =20 virCondDestroy(&priv->threadCond); @@ -348,13 +348,9 @@ udevTranslatePCIIds(unsigned int vendor, m.match_data =3D 0; =20 /* pci_get_strings returns void and unfortunately is not thread safe. = */ - virMutexLock(&pciaccessMutex); - pci_get_strings(&m, - &device_name, - &vendor_name, - NULL, - NULL); - virMutexUnlock(&pciaccessMutex); + VIR_WITH_MUTEX_LOCK_GUARD(&pciaccessMutex) { + pci_get_strings(&m, &device_name, &vendor_name, NULL, NULL); + } =20 *vendor_string =3D g_strdup(vendor_name); *product_string =3D g_strdup(device_name); @@ -373,11 +369,11 @@ udevProcessPCI(struct udev_device *device, virPCIDeviceAddress devAddr; int ret =3D -1; char *p; - bool privileged; + bool privileged =3D false; =20 - nodeDeviceLock(); - privileged =3D driver->privileged; - nodeDeviceUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) { + privileged =3D driver->privileged; + } =20 pci_dev->klass =3D -1; if (udevGetIntProperty(device, "PCI_CLASS", &pci_dev->klass, 16) < 0) @@ -1989,10 +1985,10 @@ nodeStateInitializeEnumerate(void *opaque) goto error; =20 cleanup: - nodeDeviceLock(); - driver->initialized =3D true; - virCondBroadcast(&driver->initCond); - nodeDeviceUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) { + driver->initialized =3D true; + virCondBroadcast(&driver->initCond); + } =20 return; =20 @@ -2036,12 +2032,10 @@ static void mdevctlHandlerThread(void *opaque G_GNUC_UNUSED) { udevEventData *priv =3D driver->privateData; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&priv->mdevctlLock); =20 - /* ensure only a single thread can query mdevctl at a time */ - virMutexLock(&priv->mdevctlLock); if (nodeDeviceUpdateMediatedDevices() < 0) VIR_WARN("mdevctl failed to updated mediated devices"); - virMutexUnlock(&priv->mdevctlLock); } =20 =20 @@ -2143,13 +2137,10 @@ mdevctlEnableMonitor(udevEventData *priv) * mdevctl configuration is stored in a directory tree within * /etc/mdevctl.d/. There is a directory for each parent device, which * contains a file defining each mediated device */ - virMutexLock(&priv->mdevctlLock); - if (!(priv->mdevctlMonitors =3D monitorFileRecursively(priv, - mdevctlConfigDir)= )) { - virMutexUnlock(&priv->mdevctlLock); - return -1; + VIR_WITH_MUTEX_LOCK_GUARD(&priv->mdevctlLock) { + if (!(priv->mdevctlMonitors =3D monitorFileRecursively(priv, mdevc= tlConfigDir))) + return -1; } - virMutexUnlock(&priv->mdevctlLock); =20 return 0; } @@ -2171,9 +2162,10 @@ mdevctlEventHandleCallback(GFileMonitor *monitor G_G= NUC_UNUSED, if (file_type =3D=3D G_FILE_TYPE_DIRECTORY) { GList *newmonitors =3D monitorFileRecursively(priv, file); =20 - virMutexLock(&priv->mdevctlLock); - priv->mdevctlMonitors =3D g_list_concat(priv->mdevctlMonitors,= newmonitors); - virMutexUnlock(&priv->mdevctlLock); + VIR_WITH_MUTEX_LOCK_GUARD(&priv->mdevctlLock) { + priv->mdevctlMonitors =3D g_list_concat(priv->mdevctlMonit= ors, + newmonitors); + } } } =20 --=20 2.31.1