From nobody Fri Oct 18 08:32:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719834211944875.8879694333754; Mon, 1 Jul 2024 04:43:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOFRC-00052a-Cr; Mon, 01 Jul 2024 07:43:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOFQm-0004od-Fo for qemu-devel@nongnu.org; Mon, 01 Jul 2024 07:42:50 -0400 Received: from proxmox-new.maurer-it.com ([94.136.29.106]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOFQf-0006hR-8N for qemu-devel@nongnu.org; Mon, 01 Jul 2024 07:42:47 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 0742B45902; Mon, 1 Jul 2024 13:42:31 +0200 (CEST) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Stefan Weil , Kevin Wolf , Hanna Reitz , Stefan Hajnoczi , Fam Zheng Subject: [PATCH 1/2] graph-lock: make sure reader_count access is atomic Date: Mon, 1 Jul 2024 13:42:29 +0200 Message-Id: <20240701114230.193307-2-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240701114230.193307-1-w.bumiller@proxmox.com> References: <20240701114230.193307-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=94.136.29.106; envelope-from=w.bumiller@proxmox.com; helo=proxmox-new.maurer-it.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1719834214243100007 Content-Type: text/plain; charset="utf-8" There's one case where `reader_count` is accessed non-atomically. This was likely seen as being "guarded by a mutex" held in that block, but other access to this does not actually depend on the mutex and already uses atomics. Additionally this replaces the pattern of atomic_set(atomic_read() + 1) with qatomic_inc() (and -1 with _dec) Signed-off-by: Wolfgang Bumiller --- block/graph-lock.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/block/graph-lock.c b/block/graph-lock.c index c81162b147..32fb29b841 100644 --- a/block/graph-lock.c +++ b/block/graph-lock.c @@ -176,8 +176,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void) bdrv_graph =3D qemu_get_current_aio_context()->bdrv_graph; =20 for (;;) { - qatomic_set(&bdrv_graph->reader_count, - bdrv_graph->reader_count + 1); + qatomic_inc(&bdrv_graph->reader_count); /* make sure writer sees reader_count before we check has_writer */ smp_mb(); =20 @@ -226,7 +225,7 @@ void coroutine_fn bdrv_graph_co_rdlock(void) } =20 /* slow path where reader sleeps */ - bdrv_graph->reader_count--; + qatomic_dec(&bdrv_graph->reader_count); aio_wait_kick(); qemu_co_queue_wait(&reader_queue, &aio_context_list_lock); } @@ -238,8 +237,7 @@ void coroutine_fn bdrv_graph_co_rdunlock(void) BdrvGraphRWlock *bdrv_graph; bdrv_graph =3D qemu_get_current_aio_context()->bdrv_graph; =20 - qatomic_store_release(&bdrv_graph->reader_count, - bdrv_graph->reader_count - 1); + qatomic_dec(&bdrv_graph->reader_count); /* make sure writer sees reader_count before we check has_writer */ smp_mb(); =20 --=20 2.39.2 From nobody Fri Oct 18 08:32:58 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719834212012568.7827267624701; Mon, 1 Jul 2024 04:43:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOFR9-0004wP-VW; Mon, 01 Jul 2024 07:43:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOFQm-0004ol-MR for qemu-devel@nongnu.org; Mon, 01 Jul 2024 07:42:50 -0400 Received: from proxmox-new.maurer-it.com ([94.136.29.106]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOFQf-0006hQ-8T for qemu-devel@nongnu.org; Mon, 01 Jul 2024 07:42:48 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3B40445F0E; Mon, 1 Jul 2024 13:42:31 +0200 (CEST) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Stefan Weil , Kevin Wolf , Hanna Reitz , Stefan Hajnoczi , Fam Zheng Subject: [PATCH 2/2] atomics: replace fetch-use-store with direct atomic operations Date: Mon, 1 Jul 2024 13:42:30 +0200 Message-Id: <20240701114230.193307-3-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240701114230.193307-1-w.bumiller@proxmox.com> References: <20240701114230.193307-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=94.136.29.106; envelope-from=w.bumiller@proxmox.com; helo=proxmox-new.maurer-it.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1719834213926100005 Content-Type: text/plain; charset="utf-8" Replaces the pattern `atomic_store(atomic_load() something)` pattern with its direct atomic function. Signed-off-by: Wolfgang Bumiller --- Note: these previously used RELEASE ordering for the store and `relaxed` ordering for the reads, while the replacement uses SEQ_CST, as there are no other wrappers around yet. Should we add `qatomic_fetch_{sub,and}_release` variants? util/aio-posix.c | 3 +-- util/aio-win32.c | 3 +-- util/async.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/util/aio-posix.c b/util/aio-posix.c index 266c9dd35f..9cf7fed8fc 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -672,8 +672,7 @@ bool aio_poll(AioContext *ctx, bool blocking) =20 if (use_notify_me) { /* Finish the poll before clearing the flag. */ - qatomic_store_release(&ctx->notify_me, - qatomic_read(&ctx->notify_me) - 2); + qatomic_fetch_sub(&ctx->notify_me, 2); } =20 aio_notify_accept(ctx); diff --git a/util/aio-win32.c b/util/aio-win32.c index d144f9391f..ff6d1ebf97 100644 --- a/util/aio-win32.c +++ b/util/aio-win32.c @@ -387,8 +387,7 @@ bool aio_poll(AioContext *ctx, bool blocking) ret =3D WaitForMultipleObjects(count, events, FALSE, timeout); if (blocking) { assert(first); - qatomic_store_release(&ctx->notify_me, - qatomic_read(&ctx->notify_me) - 2); + qatomic_fetch_sub(&ctx->notify_me, 2); aio_notify_accept(ctx); } =20 diff --git a/util/async.c b/util/async.c index 0467890052..d17deeceea 100644 --- a/util/async.c +++ b/util/async.c @@ -330,7 +330,7 @@ aio_ctx_check(GSource *source) BHListSlice *s; =20 /* Finish computing the timeout before clearing the flag. */ - qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) &= ~1); + qatomic_fetch_and(&ctx->notify_me, ~1); aio_notify_accept(ctx); =20 QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) { --=20 2.39.2