From nobody Mon Feb 9 21:19:39 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1646415043; cv=none; d=zohomail.com; s=zohoarc; b=eYEZMRUPBPaT85IOkLynueQPla3tut/KetmoO0ttdbZws2mlvMFNQwi/SrNT8piCJmFIjczjqMXkdSlMGYBkgHgptHcsPdhErfo8Q/cwWNr0Qw5fUO0T1UjpAV9tQ85Vj/XMrqwq0lIyZECMARV0D+YZ9dY9PaeKZWINO7l1B2k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646415043; 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=L0169zLxQaGZA46+Z78O/nBF05iK9oKuQw8oqQq/+ak=; b=gMY5aUo4xrS1Ee0PuT3s94gbRvt+duCe406k0Ieq9yXIKfxYzBVqakqEFoFvwq3no5PNWu66MmyhYQDiSP3CyX8i2Pz2DD3XLW8R0IVealkMuYzRgcZgIoXB/lO5tr/EN4v4zLDMVi3VX40g4hQJ3HgYorzy+Mia/50FaFB3X48= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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.129.124]) by mx.zohomail.com with SMTPS id 1646415043399638.9458504005181; Fri, 4 Mar 2022 09:30:43 -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-219-o9VvmdnuMT6frIdti_s84A-1; Fri, 04 Mar 2022 12:30:39 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CFC58427C4; Fri, 4 Mar 2022 17:30:33 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AE5CA86C4F; Fri, 4 Mar 2022 17:30:33 +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 7E26D1809CB5; Fri, 4 Mar 2022 17:30:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 224HTQ5D024599 for ; Fri, 4 Mar 2022 12:29:27 -0500 Received: by smtp.corp.redhat.com (Postfix) id E6F227DE4C; Fri, 4 Mar 2022 17:29:26 +0000 (UTC) Received: from work.speedport.ip (unknown [10.39.195.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 32DC58479A; Fri, 4 Mar 2022 17:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646415042; 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=L0169zLxQaGZA46+Z78O/nBF05iK9oKuQw8oqQq/+ak=; b=fWxmfaI81Mv+EAM4ZxGKOltjR6+LSJ7UYWSZ5op41mR/nij36hCV8F9CUF/KYeJfKGJjdD tgutRHYu2K95OuUhBQGdDQpaUUIcgqb17TMeEZzhCVf1F3G5/ZJZbie1iE2Us7PYZnLqSA LJuKqGwuTJzMefqwn/svUOs2ms3Lh1c= X-MC-Unique: o9VvmdnuMT6frIdti_s84A-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v2 05/10] nodesuspend: Use automatic mutex management Date: Fri, 4 Mar 2022 18:28:35 +0100 Message-Id: <20220304172840.66057-6-twiederh@redhat.com> In-Reply-To: <20220304172840.66057-1-twiederh@redhat.com> References: <20220304172840.66057-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.12 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: 1646415044534100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake --- src/util/virnodesuspend.c | 54 +++++++++++++-------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/util/virnodesuspend.c b/src/util/virnodesuspend.c index e1167605ca..5feef79c43 100644 --- a/src/util/virnodesuspend.c +++ b/src/util/virnodesuspend.c @@ -53,17 +53,6 @@ static virMutex virNodeSuspendMutex =3D VIR_MUTEX_INITIA= LIZER; =20 static bool aboutToSuspend; =20 -static void virNodeSuspendLock(void) -{ - virMutexLock(&virNodeSuspendMutex); -} - -static void virNodeSuspendUnlock(void) -{ - virMutexUnlock(&virNodeSuspendMutex); -} - - /** * virNodeSuspendSetNodeWakeup: * @alarmTime: time in seconds from now, at which the RTC alarm has to be = set. @@ -116,9 +105,9 @@ static void virNodeSuspendHelper(void *cmdString) * Now that we have resumed from suspend or the suspend failed, * reset 'aboutToSuspend' flag. */ - virNodeSuspendLock(); - aboutToSuspend =3D false; - virNodeSuspendUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&virNodeSuspendMutex) { + aboutToSuspend =3D false; + } } =20 /** @@ -154,8 +143,8 @@ int virNodeSuspend(unsigned int target, { static virThread thread; const char *cmdString =3D NULL; - int ret =3D -1; unsigned int supported; + VIR_LOCK_GUARD lock =3D { NULL }; =20 virCheckFlags(0, -1); =20 @@ -166,13 +155,13 @@ int virNodeSuspend(unsigned int target, * Ensure that we are the only ones trying to suspend. * Fail if somebody has already initiated a suspend. */ - virNodeSuspendLock(); + lock =3D virLockGuardLock(&virNodeSuspendMutex); =20 if (aboutToSuspend) { /* A suspend operation is already in progress */ virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("Suspend operation already in progress")); - goto cleanup; + return -1; } =20 /* Check if the host supports the requested suspend target */ @@ -180,7 +169,7 @@ int virNodeSuspend(unsigned int target, case VIR_NODE_SUSPEND_TARGET_MEM: if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_MEM))) { virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-= to-RAM")); - goto cleanup; + return -1; } cmdString =3D "pm-suspend"; break; @@ -188,7 +177,7 @@ int virNodeSuspend(unsigned int target, case VIR_NODE_SUSPEND_TARGET_DISK: if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_DISK))) { virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Suspend-= to-Disk")); - goto cleanup; + return -1; } cmdString =3D "pm-hibernate"; break; @@ -196,19 +185,19 @@ int virNodeSuspend(unsigned int target, case VIR_NODE_SUSPEND_TARGET_HYBRID: if (!(supported & (1 << VIR_NODE_SUSPEND_TARGET_HYBRID))) { virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", _("Hybrid-S= uspend")); - goto cleanup; + return -1; } cmdString =3D "pm-suspend-hybrid"; break; =20 default: virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid suspend targe= t")); - goto cleanup; + return -1; } =20 /* Just set the RTC alarm. Don't suspend yet. */ if (virNodeSuspendSetNodeWakeup(duration) < 0) - goto cleanup; + return -1; =20 if (virThreadCreateFull(&thread, false, virNodeSuspendHelper, @@ -217,14 +206,11 @@ int virNodeSuspend(unsigned int target, (void *)cmdString) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Failed to create thread to suspend the host")); - goto cleanup; + return -1; } =20 aboutToSuspend =3D true; - ret =3D 0; - cleanup: - virNodeSuspendUnlock(); - return ret; + return 0; } =20 #ifdef WITH_PM_UTILS @@ -348,11 +334,10 @@ virNodeSuspendSupportsTarget(unsigned int target, boo= l *supported) int virNodeSuspendGetTargetMask(unsigned int *bitmask) { - int ret =3D -1; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&virNodeSuspendMutex); =20 *bitmask =3D 0; =20 - virNodeSuspendLock(); /* Get the power management capabilities supported by the host */ if (!nodeSuspendTargetMaskInit) { bool supported; @@ -360,19 +345,19 @@ virNodeSuspendGetTargetMask(unsigned int *bitmask) =20 /* Check support for Suspend-to-RAM (S3) */ if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_MEM, &sup= ported) < 0) - goto cleanup; + return -1; if (supported) nodeSuspendTargetMask |=3D (1 << VIR_NODE_SUSPEND_TARGET_MEM); =20 /* Check support for Suspend-to-Disk (S4) */ if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_DISK, &su= pported) < 0) - goto cleanup; + return -1; if (supported) nodeSuspendTargetMask |=3D (1 << VIR_NODE_SUSPEND_TARGET_DISK); =20 /* Check support for Hybrid-Suspend */ if (virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_HYBRID, &= supported) < 0) - goto cleanup; + return -1; if (supported) nodeSuspendTargetMask |=3D (1 << VIR_NODE_SUSPEND_TARGET_HYBRI= D); =20 @@ -380,8 +365,5 @@ virNodeSuspendGetTargetMask(unsigned int *bitmask) } =20 *bitmask =3D nodeSuspendTargetMask; - ret =3D 0; - cleanup: - virNodeSuspendUnlock(); - return ret; + return 0; } --=20 2.31.1