From nobody Sat May 18 14:10:07 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=1583930366; cv=none; d=zohomail.com; s=zohoarc; b=Dd+c5IN9mJ6aYDr628yMgh4C34woeFCv880UwC1sKfkYrxIwim8dXAPPj1y5WePQ/Tch0Z5OCX23cAYDWloDM0+ftacZ9x4PuEY+x4AwiWM45xUmRXB6vHzWPwJsqQ4lYzHosSZyCTosfTHuSV6+YTfCSQn0AC4AgFCEe+hbq5M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583930366; 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=5nh15vDTrDF7YqOGEJcfqFZtZYBVL088KNsl1qKAeQ4=; b=nDQe7QGYQlRhyjAJB+OtPrUDT2eWR5FqVoy3pxqCWnlAQIRUrZqxaHRjTpTHwjHHxLX++kDu8NPOr5UnyfsZtxXamj08qQdBDv/2inJTKSO/PyGnow7VMWx46dk6axqUpet1n35rUWpsIUqHgfHa+/pKOodA6EO5LtKJsi9WV9A= 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 158393036613185.59920939887411; Wed, 11 Mar 2020 05:39:26 -0700 (PDT) Received: from localhost ([::1]:51036 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jC0dw-00031d-Sz for importer@patchew.org; Wed, 11 Mar 2020 08:39:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46162) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jC0cy-0001IJ-E8 for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jC0cx-0004i3-7V for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:24 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:35257 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 1jC0cx-0004Uu-2U for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:23 -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-419-8cb4jjRxM2y74-WD7T16kA-1; Wed, 11 Mar 2020 08:38:17 -0400 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 A1CF98017CC; Wed, 11 Mar 2020 12:38:16 +0000 (UTC) Received: from localhost (unknown [10.36.118.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id CC16E8F377; Wed, 11 Mar 2020 12:38:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583930301; 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=5nh15vDTrDF7YqOGEJcfqFZtZYBVL088KNsl1qKAeQ4=; b=bJL0lfY6yifpawgh5yi7+GA59vHztvvFv5iJDjkjQptLyKu4FwnlOjmeYokE3XcGVJjDZM ZsDTSv/3GAI08fAMLPkkvIKHC7Ms+bE+FIa5mNRO9uxRrlrUVWGWLrpBkLAcUT3XiPT0v0 /2dBGHk3draql8zXaPpjsGiJmgxBEeg= X-MC-Unique: 8cb4jjRxM2y74-WD7T16kA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 1/2] thread: add QemuRecMutex lock guards Date: Wed, 11 Mar 2020 12:36:23 +0000 Message-Id: <20200311123624.277221-2-stefanha@redhat.com> In-Reply-To: <20200311123624.277221-1-stefanha@redhat.com> References: <20200311123624.277221-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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] [fuzzy] X-Received-From: 207.211.31.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?= , "Dr. David Alan Gilbert" , Stefan Hajnoczi 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 QemuRecMutex: void f(void) { QEMU_REC_MUTEX_LOCK_GUARD(&mutex); if (!may_fail()) { return; /* automatically unlocks mutex */ } ... } and: WITH_QEMU_REC_MUTEX_LOCK_GUARD(&mutex) { if (!may_fail()) { return; /* automatically unlocks mutex */ } } /* automatically unlocks mutex here */ ... 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/thread.h | 26 ++++++++++++++++++++++++++ plugins/core.c | 6 ++---- plugins/loader.c | 15 +++++++-------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 047db0307e..3993ab7b25 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -132,6 +132,32 @@ static inline int (qemu_rec_mutex_trylock)(QemuRecMute= x *mutex) /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */ void qemu_rec_mutex_init(QemuRecMutex *mutex); =20 +static inline QemuRecMutex *qemu_rec_mutex_auto_lock(QemuRecMutex *mutex) +{ + qemu_rec_mutex_lock(mutex); + return mutex; +} + +static inline void qemu_rec_mutex_auto_unlock(QemuRecMutex *mutex) +{ + if (mutex) { + qemu_rec_mutex_unlock(mutex); + } +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuRecMutex, qemu_rec_mutex_auto_unlock) + +#define WITH_QEMU_REC_MUTEX_LOCK_GUARD_(mutex, var) \ + for (g_autoptr(QemuRecMutex) var =3D qemu_rec_mutex_auto_lock((mutex))= ; \ + var; qemu_rec_mutex_auto_unlock(var), var =3D NULL) + +#define WITH_QEMU_REC_MUTEX_LOCK_GUARD(mutex) \ + WITH_QEMU_REC_MUTEX_LOCK_GUARD_((mutex), qemu_rec_mutex_auto##__COUNTE= R__) + +#define QEMU_REC_MUTEX_LOCK_GUARD(mutex) \ + g_autoptr(QemuRecMutex) qemu_rec_mutex_auto##__COUNTER__ =3D \ + qemu_rec_mutex_auto_lock((mutex)) + void qemu_cond_init(QemuCond *cond); void qemu_cond_destroy(QemuCond *cond); =20 diff --git a/plugins/core.c b/plugins/core.c index ed863011ba..cf8c85de9c 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -150,11 +150,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_REC_MUTEX_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 +178,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..9742c8d41d 100644 --- a/plugins/loader.c +++ b/plugins/loader.c @@ -367,15 +367,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_REC_MUTEX_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 From nobody Sat May 18 14:10:07 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=1583930412; cv=none; d=zohomail.com; s=zohoarc; b=h/jjXkjzCsCiHGkbJeCHrhu7nRdd0BOmXPHC+ewicK/WPBekC8PclGCSX8gT/IsN+hX2HgZepCYD45aG/mUTc1o0RiJ3rzz7kmy742UwiBcGAoxPwlMMDimusYlAbWl39S8PqdysBAFbcXM4iAs52Rw0fhCGvmtHEQvT+i7xWpM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1583930412; 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=h4N6uuF6VeTZN8iqYzd6Y0h1MwU3ts141iawsEo5kS4=; b=c3lVw0jfxbzudHS+5B5POLcNTH5F4WiuKmTT/qLzPyRL6LVx+tplGkpOPtxPvhKdXluL1Ny98I2d7s9j9cbScjLYFjJT3wkhlzx6QMvhB0/RfmdF/7ElQ5iJ81c1/tYnSpNRjLRqZD0wVG/YFnTEveZlNImCqqxS5opqGiHoQys= 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 1583930412113780.7308539165366; Wed, 11 Mar 2020 05:40:12 -0700 (PDT) Received: from localhost ([::1]:51040 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jC0eh-0004R9-1n for importer@patchew.org; Wed, 11 Mar 2020 08:40:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46184) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jC0d0-0001Ms-4C for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jC0cy-0004pI-HL for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:26 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:48325 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 1jC0cy-0004lv-DD for qemu-devel@nongnu.org; Wed, 11 Mar 2020 08:38:24 -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-120-l5hgkbOTMAmD2tLVGloI4w-1; Wed, 11 Mar 2020 08:38:22 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 474908017CC; Wed, 11 Mar 2020 12:38:21 +0000 (UTC) Received: from localhost (unknown [10.36.118.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C87E73865; Wed, 11 Mar 2020 12:38:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583930303; 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=h4N6uuF6VeTZN8iqYzd6Y0h1MwU3ts141iawsEo5kS4=; b=Uu4ofFWfBi69wF14/0RWF+Bj/kNLYmDa86LoDq7Oid9RBT1LDwhikIPKz0uMkOQvUsRrsf qgH82KbEjGtsJneK5R7XN/t8ygFm2siWozeHHZRM1u7P4jHpPl3JKhKTEuDuPsLO67SjA6 dlAnuC91sbLADehf4e7ZQOcHBebQGaE= X-MC-Unique: l5hgkbOTMAmD2tLVGloI4w-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 2/2] thread: add QemuMutex lock guards Date: Wed, 11 Mar 2020 12:36:24 +0000 Message-Id: <20200311123624.277221-3-stefanha@redhat.com> In-Reply-To: <20200311123624.277221-1-stefanha@redhat.com> References: <20200311123624.277221-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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?= , "Dr. David Alan Gilbert" , Stefan Hajnoczi 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 QemuMutex: void f(void) { QEMU_MUTEX_LOCK_GUARD(&mutex); if (!may_fail()) { return; /* automatically unlocks mutex */ } ... } and: WITH_QEMU_MUTEX_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/thread.h | 26 ++++++++++++++++++++++++++ util/qemu-timer.c | 22 ++++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 3993ab7b25..8e8254f94f 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -119,6 +119,32 @@ static inline void (qemu_mutex_unlock)(QemuMutex *mute= x) qemu_mutex_unlock(mutex); } =20 +static inline QemuMutex *qemu_mutex_auto_lock(QemuMutex *mutex) +{ + qemu_mutex_lock(mutex); + return mutex; +} + +static inline void qemu_mutex_auto_unlock(QemuMutex *mutex) +{ + if (mutex) { + qemu_mutex_unlock(mutex); + } +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuMutex, qemu_mutex_auto_unlock) + +#define WITH_QEMU_MUTEX_LOCK_GUARD_(mutex, var) \ + for (g_autoptr(QemuMutex) var =3D qemu_mutex_auto_lock((mutex)); \ + var; qemu_mutex_auto_unlock(var), var =3D NULL) + +#define WITH_QEMU_MUTEX_LOCK_GUARD(mutex) \ + WITH_QEMU_MUTEX_LOCK_GUARD_(mutex, qemu_mutex_auto##__COUNTER__) + +#define QEMU_MUTEX_LOCK_GUARD(mutex) \ + g_autoptr(QemuMutex) qemu_mutex_auto##__COUNTER__ =3D \ + qemu_mutex_auto_lock((mutex)) + static inline void (qemu_rec_mutex_lock)(QemuRecMutex *mutex) { qemu_rec_mutex_lock(mutex); diff --git a/util/qemu-timer.c b/util/qemu-timer.c index ef52d28d37..4c42d33cf5 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -186,13 +186,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_MUTEX_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 +224,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_MUTEX_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