From nobody Fri May 3 09:10:40 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1584358097; cv=none; d=zohomail.com; s=zohoarc; b=B1bWbR5mqFscn64nTq0sKaLG/LGDRQH1btnhZN+SVdFAuGEyQd9EgB9qkYlUOIfZMuRbHhC6nUR9rP2pluTd8pUkJNaqNEHii3GYu959c+gsWX2znpsv5zXqYvRQ+xDzCohnGpv92ePSnifXCaZV+ZE9AUdK9HYiUBlPsl/jkUE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584358097; 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=8jcU/HCwQAmCeBKP7ipq7laiBGMA1+e7a1zl9eXhO+4=; b=mO8ONW1R67rsNW4gxpjPUt1XGJ+Bew85AKKkPehuZxaUZDaxEnr8uJdGfl+ZDmbNc/w+c1TCKP8DzmS4w5rNlZ8bzpE1Exi3YXeJjD4gt1EAibcnGWLkSekyT+4GShA3b97YfMLRsjp2pEXgXvvl3BKr6eULqitceH5AjWG5VUE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584358097078237.4085329187776; Mon, 16 Mar 2020 04:28:17 -0700 (PDT) Received: from localhost ([::1]:37248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jDnup-0000Px-4w for importer@patchew.org; Mon, 16 Mar 2020 07:28:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42428) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jDndy-0002q0-6b for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jDndw-0004lt-Mm for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:50 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:46550 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jDndw-0004iq-Gy for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:48 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-160-Cdc2hFlXOZCg45S3M_k8Uw-1; Mon, 16 Mar 2020 07:10:46 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD4D6101FC6A; Mon, 16 Mar 2020 11:10:45 +0000 (UTC) Received: from localhost (unknown [10.36.119.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA5E71036B54; Mon, 16 Mar 2020 11:10:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584357048; h=from:from: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; bh=8jcU/HCwQAmCeBKP7ipq7laiBGMA1+e7a1zl9eXhO+4=; b=Ws3/mY/v8iRWqOe9Y4l6TcL29PRb5IcQaufxbsI+10ovTtdfyCiv/MALwcybEef7cy3NO3 1rVlGwhGBHfriS3of2VVUU3acj9+pJf6uoXPfq1+otyJQMnwisL2GQ2kn5ijHb1GzWD/oh WdOUvtYrxzy7/p6RjQCZWw3pHA3Khv4= X-MC-Unique: Cdc2hFlXOZCg45S3M_k8Uw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH v2 1/2] lockable: add lock guards Date: Mon, 16 Mar 2020 11:09:56 +0000 Message-Id: <20200316110957.449700-2-stefanha@redhat.com> In-Reply-To: <20200316110957.449700-1-stefanha@redhat.com> References: <20200316110957.449700-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Markus Armbruster , Stefan Hajnoczi , "Dr. David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch introduces two lock guard macros that automatically unlock a lock object (QemuMutex and others): void f(void) { QEMU_LOCK_GUARD(&mutex); if (!may_fail()) { return; /* automatically unlocks mutex */ } ... } and: WITH_QEMU_LOCK_GUARD(&mutex) { if (!may_fail()) { return; /* automatically unlocks mutex */ } } /* automatically unlocks mutex here */ ... Convert qemu-timer.c functions that benefit from these macros as an example. Manual qemu_mutex_lock/unlock() callers are left unmodified in cases where clarity would not improve by switching to the macros. Many other QemuMutex users remain in the codebase that might benefit from lock guards. Over time they can be converted, if that is desirable. Signed-off-by: Stefan Hajnoczi --- include/qemu/lockable.h | 65 +++++++++++++++++++++++++++++++++++++++++ util/qemu-timer.c | 23 +++++++-------- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/include/qemu/lockable.h b/include/qemu/lockable.h index 84ea794bcf..2b52c7c1e5 100644 --- a/include/qemu/lockable.h +++ b/include/qemu/lockable.h @@ -93,4 +93,69 @@ static inline void qemu_lockable_unlock(QemuLockable *x) x->unlock(x->object); } =20 +static inline QemuLockable *qemu_lockable_auto_lock(QemuLockable *x) +{ + qemu_lockable_lock(x); + return x; +} + +static inline void qemu_lockable_auto_unlock(QemuLockable *x) +{ + if (x) { + qemu_lockable_unlock(x); + } +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuLockable, qemu_lockable_auto_unlock) + +#define WITH_QEMU_LOCK_GUARD_(x, var) \ + for (g_autoptr(QemuLockable) var =3D \ + qemu_lockable_auto_lock(QEMU_MAKE_LOCKABLE((x))); \ + var; \ + qemu_lockable_auto_unlock(var), var =3D NULL) + +/** + * WITH_QEMU_LOCK_GUARD - Lock a lock object for scope + * + * @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). + * + * This macro defines a lock scope such that entering the scope takes the = lock + * and leaving the scope releases the lock. Return statements are allowed + * within the scope and release the lock. Break and continue statements l= eave + * the scope early and release the lock. + * + * WITH_QEMU_LOCK_GUARD(&mutex) { + * ... + * if (error) { + * return; <-- mutex is automatically unlocked + * } + * + * if (early_exit) { + * break; <-- leave this scope early + * } + * ... + * } + */ +#define WITH_QEMU_LOCK_GUARD(x) \ + WITH_QEMU_LOCK_GUARD_((x), qemu_lockable_auto##__COUNTER__) + +/** + * QEMU_LOCK_GUARD - Lock an object until the end of the scope + * + * @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). + * + * This macro takes a lock until the end of the scope. Return statements + * release the lock. + * + * ... <-- mutex not locked + * QEMU_LOCK_GUARD(&mutex); <-- mutex locked from here onwards + * ... + * if (error) { + * return; <-- mutex is automatically unlocked + * } + */ +#define QEMU_LOCK_GUARD(x) \ + g_autoptr(QemuLockable) qemu_lockable_auto##__COUNTER__ =3D \ + qemu_lockable_auto_lock(QEMU_MAKE_LOCKABLE((x))) + #endif diff --git a/util/qemu-timer.c b/util/qemu-timer.c index ef52d28d37..d548d3c1ad 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -25,6 +25,7 @@ #include "qemu/osdep.h" #include "qemu/main-loop.h" #include "qemu/timer.h" +#include "qemu/lockable.h" #include "sysemu/replay.h" #include "sysemu/cpus.h" =20 @@ -186,13 +187,12 @@ bool timerlist_expired(QEMUTimerList *timer_list) return false; } =20 - qemu_mutex_lock(&timer_list->active_timers_lock); - if (!timer_list->active_timers) { - qemu_mutex_unlock(&timer_list->active_timers_lock); - return false; + WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) { + if (!timer_list->active_timers) { + return false; + } + expire_time =3D timer_list->active_timers->expire_time; } - expire_time =3D timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); =20 return expire_time <=3D qemu_clock_get_ns(timer_list->clock->type); } @@ -225,13 +225,12 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_li= st) * value but ->notify_cb() is called when the deadline changes. There= fore * the caller should notice the change and there is no race condition. */ - qemu_mutex_lock(&timer_list->active_timers_lock); - if (!timer_list->active_timers) { - qemu_mutex_unlock(&timer_list->active_timers_lock); - return -1; + WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) { + if (!timer_list->active_timers) { + return -1; + } + expire_time =3D timer_list->active_timers->expire_time; } - expire_time =3D timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); =20 delta =3D expire_time - qemu_clock_get_ns(timer_list->clock->type); =20 --=20 2.24.1 From nobody Fri May 3 09:10:40 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1584360170; cv=none; d=zohomail.com; s=zohoarc; b=GYBcBUpmOcxgAy/DTcSRzuozXLO84Z20/T+cxOPr0tOKYiVSfa/v93A5WJsA6rdoiu9QEYw0jJFnhkcvKkKIm/EdIajk/CZ3iKcejOzia3mh1O2pOGRv7hUqjmQCgf/6rjZEW9CK9ZRwc58M+w9doopRtHnU9hTQ7oLBSblZmQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584360170; 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=XgE+vdHZP8+R7ZEnD4oIE/yB0B6AtgFjwBdfiemBG0c=; b=UcZGofj0B7DaXqFkgh4Jws4R03NZIDpdeRb1ujm2x1MWFsk69W/5pmZxMA2clckZ5aIe3nDusrNAGgbsX2rgQA6EdwoTKcWG9NGY9RzraTPEpFMvhluYO75EWbn8N99t5zaPrQW8db0VJf1Id5C1BIOyOvqu9J8RgXN9p96HVIY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158436017039839.967771028200445; Mon, 16 Mar 2020 05:02:50 -0700 (PDT) Received: from localhost ([::1]:37568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jDoSG-0004GN-LR for importer@patchew.org; Mon, 16 Mar 2020 08:02:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42505) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jDne0-0002tb-RR for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jDndz-0005Mc-DQ for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:52 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:57672 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jDndz-0005CS-5L for qemu-devel@nongnu.org; Mon, 16 Mar 2020 07:10:51 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-316-I_bLVJ0gPdeQwc2FAwTqeQ-1; Mon, 16 Mar 2020 07:10:48 -0400 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 0699518FE881; Mon, 16 Mar 2020 11:10:48 +0000 (UTC) Received: from localhost (unknown [10.36.119.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA733610DC; Mon, 16 Mar 2020 11:10:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584357050; h=from:from: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; bh=XgE+vdHZP8+R7ZEnD4oIE/yB0B6AtgFjwBdfiemBG0c=; b=UtaDc8IGZAs9cBWmp2Zy7T2PJ1uU0aNgs+FLllHPlrauveRN0PpDQqBkqx3ZzNbrPXKDfS 7e5n65UpyiUWBbXMoTEcpRKwV6Id+W9ySLRv59jWkqcLcW/ULYoKLQKAii9Ij1VkQjVt83 0Xsa76VrEcQXDSRC4gaZDYfsCoXSvmY= X-MC-Unique: I_bLVJ0gPdeQwc2FAwTqeQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH v2 2/2] lockable: add QemuRecMutex support Date: Mon, 16 Mar 2020 11:09:57 +0000 Message-Id: <20200316110957.449700-3-stefanha@redhat.com> In-Reply-To: <20200316110957.449700-1-stefanha@redhat.com> References: <20200316110957.449700-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Markus Armbruster , Stefan Hajnoczi , "Dr. David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The polymorphic locking macros don't support QemuRecMutex yet. Add it so that lock guards can be used with QemuRecMutex. Convert TCG plugins functions that benefit from these macros. Manual qemu_rec_mutex_lock/unlock() callers are left unmodified in cases where clarity would not improve by switching to the macros. Signed-off-by: Stefan Hajnoczi --- include/qemu/lockable.h | 2 ++ plugins/core.c | 7 +++---- plugins/loader.c | 16 ++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/qemu/lockable.h b/include/qemu/lockable.h index 2b52c7c1e5..44b3f4be72 100644 --- a/include/qemu/lockable.h +++ b/include/qemu/lockable.h @@ -50,6 +50,7 @@ qemu_make_lockable(void *x, QemuLockable *lockable) #define QEMU_LOCK_FUNC(x) ((QemuLockUnlockFunc *) \ QEMU_GENERIC(x, \ (QemuMutex *, qemu_mutex_lock), \ + (QemuRecMutex *, qemu_rec_mutex_lock), \ (CoMutex *, qemu_co_mutex_lock), \ (QemuSpin *, qemu_spin_lock), \ unknown_lock_type)) @@ -57,6 +58,7 @@ qemu_make_lockable(void *x, QemuLockable *lockable) #define QEMU_UNLOCK_FUNC(x) ((QemuLockUnlockFunc *) \ QEMU_GENERIC(x, \ (QemuMutex *, qemu_mutex_unlock), \ + (QemuRecMutex *, qemu_rec_mutex_unlock), \ (CoMutex *, qemu_co_mutex_unlock), \ (QemuSpin *, qemu_spin_unlock), \ unknown_lock_type)) diff --git a/plugins/core.c b/plugins/core.c index ed863011ba..51bfc94787 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -15,6 +15,7 @@ #include "qemu/error-report.h" #include "qemu/config-file.h" #include "qapi/error.h" +#include "qemu/lockable.h" #include "qemu/option.h" #include "qemu/rcu_queue.h" #include "qemu/xxhash.h" @@ -150,11 +151,11 @@ do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_= plugin_event ev, { struct qemu_plugin_ctx *ctx; =20 - qemu_rec_mutex_lock(&plugin.lock); + QEMU_LOCK_GUARD(&plugin.lock); ctx =3D plugin_id_to_ctx_locked(id); /* if the plugin is on its way out, ignore this request */ if (unlikely(ctx->uninstalling)) { - goto out_unlock; + return; } if (func) { struct qemu_plugin_cb *cb =3D ctx->callbacks[ev]; @@ -178,8 +179,6 @@ do_plugin_register_cb(qemu_plugin_id_t id, enum qemu_pl= ugin_event ev, } else { plugin_unregister_cb__locked(ctx, ev); } - out_unlock: - qemu_rec_mutex_unlock(&plugin.lock); } =20 void plugin_register_cb(qemu_plugin_id_t id, enum qemu_plugin_event ev, diff --git a/plugins/loader.c b/plugins/loader.c index 15fc7e5515..685d334e1a 100644 --- a/plugins/loader.c +++ b/plugins/loader.c @@ -19,6 +19,7 @@ #include "qemu/error-report.h" #include "qemu/config-file.h" #include "qapi/error.h" +#include "qemu/lockable.h" #include "qemu/option.h" #include "qemu/rcu_queue.h" #include "qemu/qht.h" @@ -367,15 +368,14 @@ void plugin_reset_uninstall(qemu_plugin_id_t id, struct qemu_plugin_reset_data *data; struct qemu_plugin_ctx *ctx; =20 - qemu_rec_mutex_lock(&plugin.lock); - ctx =3D plugin_id_to_ctx_locked(id); - if (ctx->uninstalling || (reset && ctx->resetting)) { - qemu_rec_mutex_unlock(&plugin.lock); - return; + WITH_QEMU_LOCK_GUARD(&plugin.lock) { + ctx =3D plugin_id_to_ctx_locked(id); + if (ctx->uninstalling || (reset && ctx->resetting)) { + return; + } + ctx->resetting =3D reset; + ctx->uninstalling =3D !reset; } - ctx->resetting =3D reset; - ctx->uninstalling =3D !reset; - qemu_rec_mutex_unlock(&plugin.lock); =20 data =3D g_new(struct qemu_plugin_reset_data, 1); data->ctx =3D ctx; --=20 2.24.1