From nobody Mon Feb 9 17:34: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=1645103043; cv=none; d=zohomail.com; s=zohoarc; b=J7J2DskPxIsGvOaRM6+7cPAGd/skNBKqiLW2BxtflelnIWmcSoSiDbI6BG/guaijIHXyqx9TI1O/LpUtWFG9ucdVkydQPbfVd2DxST7sfln5tsRIrEzH/m+U74MIZiRpdsOPZw/+PvKD7WlxNhPeTVgSW3B8Mgg35kVtQbSw6To= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645103043; 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=Bi9TmH8Q5uGAAQHp+dyJySb/BuJbUOV6MI024ZMg3JTBob8J2r2hxzeRL43t1bqzmvbNmdcTqa5dnG5ItwLPwBKQnVEhuJ3H6ZdwBoC9L4T2bp20RtQz3z1BNT/j6SxhRtyHE1Mt4iX1aBx8tjcKl1r5goVj3Mz/rZt3N8/NZUU= 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 1645103043513318.57354913686424; Thu, 17 Feb 2022 05:04:03 -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-351-b5CVf_DKPU-qkOw6oeElwg-1; Thu, 17 Feb 2022 08:03:53 -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 A9AA91853024; Thu, 17 Feb 2022 13:03:48 +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 889AC66E3B; Thu, 17 Feb 2022 13:03:48 +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 59A4450486; Thu, 17 Feb 2022 13:03:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 21HD1A0F031212 for ; Thu, 17 Feb 2022 08:01:10 -0500 Received: by smtp.corp.redhat.com (Postfix) id E22E52B4B1; Thu, 17 Feb 2022 13:01:10 +0000 (UTC) Received: from work.speedport.ip (unknown [10.39.194.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 328712B3A7; Thu, 17 Feb 2022 13:01:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645103042; 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=bRLQmESmZg5qUIfTqL+ETZVYJ3t5AwR+v0X+e6aXsfLgXb+HwExwaPQPbnFyiY0Kd3oomD 8SDx14gIgaDJTEuZspjCfAEImyVx/28UnHHM0sgW76Yvg4R0o4Cq/AETuGD9+M/Nf/ck4S SzQzQPUT7CiM5GhSwPgalSSY62lqSos= X-MC-Unique: b5CVf_DKPU-qkOw6oeElwg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 05/11] nodesuspend: Use automatic mutex management Date: Thu, 17 Feb 2022 14:00:57 +0100 Message-Id: <20220217130103.18543-6-twiederh@redhat.com> In-Reply-To: <20220217130103.18543-1-twiederh@redhat.com> References: <20220217130103.18543-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: 1645103052033100002 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