From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864013; cv=none; d=zohomail.com; s=zohoarc; b=LLc8GZWVyW2lmeY9KjPkpNrBKHu3VehGV7MXCUMIBEqEDEXg8axhg1R2WpP2Stlid/xTEMVW0+EPSeICc4p3D9GuQy1ssJ0IYv0pHbk5+tFBBQjg/G3ys1bB0yRFAEzk8f0v2odqM4q3j2wF2KZK1mymdo+yoXXq4TfnaStmEjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864013; 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=Pe/Uoc8r2RK2RtqW9FRSug7sbCN6iAxq5CdaDKwC19o=; b=dj1xFd1tBg20eXM7FlDuowcBIunreWIFI68UeZp93gYYzKHYXJkV226JvVeHpNeLaPnBkGypApZiYrX6SCQXk3bVvJ05aiiWUFs6quQIEGFzWfrmetDlL3x/us4xdJY2FwayLcJ4WiYA4mh6OtsP5Fw0WIJWxfiE9LHxKm7kYx0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864013100373.8039703639504; Fri, 3 Mar 2023 09:20:13 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94U-0005Vv-KL; Fri, 03 Mar 2023 12:19:54 -0500 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 1pY94S-0005Vk-OV for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:52 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94Q-0003Qm-Si for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:52 -0500 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-403-d3khjUA8P0C1isND-zW_vA-1; Fri, 03 Mar 2023 12:19:48 -0500 Received: by mail-wm1-f71.google.com with SMTP id az39-20020a05600c602700b003e97eb80524so3135527wmb.4 for ; Fri, 03 Mar 2023 09:19:48 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id r1-20020a056000014100b002c5534db60bsm2792341wrx.71.2023.03.03.09.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:19:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677863989; 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=Pe/Uoc8r2RK2RtqW9FRSug7sbCN6iAxq5CdaDKwC19o=; b=OjWLzaDZ98XPX5rrrvm+UI0m16ejJg0Laf0RPXTNzcnUQaz7dPAcSxHM/occpgvxOO3CNX pFPIrW3xFSgeUDWlixtJQCUIXDAo6oBO/g7aRk1KTTf6qhQxwNpPelUwKSE4H8HTlmgTp+ waYuH3Sbm+b+QrqREKT70Wq3+rUCpH0= X-MC-Unique: d3khjUA8P0C1isND-zW_vA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pe/Uoc8r2RK2RtqW9FRSug7sbCN6iAxq5CdaDKwC19o=; b=dAQ9XB6NpllA9gBeg2iiQlxo5HKo8Kmna+9xYNkdQ64DvRLmbT8rAYMcxZPsoS1gzn FPxs14Db0/vPZX/vV08K/sPqPBJpfsOvgMzl8i+GpU1h0WDCuBpdfL1aPXvzYJkLwhwQ IUVqE82u2T2UvhABwY1A9tvM/ouBAMosJ4B0F/9z6jLglskP0mLBvNDqKzHQe2hDWKY5 t85z7GdLiGE+zo036ICU0qlNNWGiblI02tmaJxVf9XT+/t1wQAOJ4Iy8G8YIB+a53+M2 FwJYkXXKmVlFndcfWtCIM8DhS/hrOlMjVqHCgG9gCPGEvU8WuU9mWcywyjS7I6iPT+dO Kr6w== X-Gm-Message-State: AO0yUKX+gTvWJnkynO4w/WqaGhQTLENBo41N9acV8t5dsKmfEkpfhsea vvtJ0L7VHkHzqSxMsToBGyYs3yHZWu4sfP7gKmBA/MKEb+BxmhIcpdEY2rSsJIBZeayAi9zWGlc o+/Rjh/d/VbWJC9ETuIx4/WyoK+RdWMmGbz3dktWMPbP8XAhK8BWntENXDNu4rVDwKHlWDuaZ X-Received: by 2002:adf:fd41:0:b0:2ce:3ae0:55a7 with SMTP id h1-20020adffd41000000b002ce3ae055a7mr1269381wrs.29.1677863986279; Fri, 03 Mar 2023 09:19:46 -0800 (PST) X-Google-Smtp-Source: AK7set+dyiV8Vp9hJo8MF4EC/iw2sbFeqqK7pvfH1DYHSzPXJTr4i8W/s3OtxZK1kA4pixxAYvbkDQ== X-Received: by 2002:adf:fd41:0:b0:2ce:3ae0:55a7 with SMTP id h1-20020adffd41000000b002ce3ae055a7mr1269362wrs.29.1677863985910; Fri, 03 Mar 2023 09:19:45 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 1/8] qatomic: add smp_mb__before/after_rmw() Date: Fri, 3 Mar 2023 18:19:32 +0100 Message-Id: <20230303171939.237819-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864014825100009 Content-Type: text/plain; charset="utf-8" On ARM, seqcst loads and stores (which QEMU does not use) are compiled respectively as LDAR and STLR instructions. Even though STLR is also used for store-release operations, STLR followed by LDAR provides store-against-load ordering, which is stronger than a store-release. Compare this to ARMv7, where store-release is DMB+STR and store-seqcst is DMB+STR+DMB. This means that on ARM a sequence of qatomic_store_release(&y, ...); // STLR a =3D qatomic_load_acquire(&x); // LDAR provides stronger ordering at the processor level than the two MOV instructions you'd get on x86. Likewise, on ARM sequentially consistent read-modify-write operations only need to use LDAXR and STLXR respectively for the load and the store, which is weaker than the LOCK prefix used on x86. In a strange twist of events, however, the _stronger_ semantics of the ARM instructions can end up causing bugs on ARM, not on x86. The problems occur when seqcst atomics are mixed with relaxed atomics. QEMU's atomics try to bridge the Linux API (that most of the developers are familiar with) and the C11 API, and the two have a substantial difference: - in Linux, strongly-ordered atomics such as atomic_add_return() affect the global ordering of _all_ memory operations, including for example READ_ONCE()/WRITE_ONCE() - in C11, sequentially consistent atomics (except for seq-cst fences) only affect the ordering of sequentially consistent operations. In particular, since relaxed loads are done with LDR on ARM, they are not ordered against seqcst stores (which are done with STLR). QEMU implements high-level synchronization primitives with the idea that the primitives contain the necessary memory barriers, and the callers can use relaxed atomics (qatomic_read/qatomic_set) or even regular accesses. This is very much incompatible with the C11 view that seqcst accesses are only ordered against other seqcst accesses, and requires using seqcst fences as in the following example: qatomic_set(&y, 1); qatomic_set(&x, 1); smp_mb(); smp_mb(); ... qatomic_read(&x) ... ... qatomic_read(&y) ... When a qatomic_*() read-modify write operation is used instead of one or both stores, developers that are more familiar with the Linux API may be tempted to omit the smp_mb(), which will work on x86 but not on ARM. This nasty difference between Linux and C11 read-modify-write operations has already caused issues in util/async.c and more are being found. Provide something similar to Linux smp_mb__before/after_atomic(); this has the double function of documenting clearly why there is a memory barrier, and avoiding a double barrier on x86 and s390x systems. The new macro can already be put to use in qatomic_mb_set(). Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- docs/devel/atomics.rst | 26 +++++++++++++++++++++----- include/qemu/atomic.h | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/devel/atomics.rst b/docs/devel/atomics.rst index 7957310071d9..898f5393c07a 100644 --- a/docs/devel/atomics.rst +++ b/docs/devel/atomics.rst @@ -27,7 +27,8 @@ provides macros that fall in three camps: =20 - weak atomic access and manual memory barriers: ``qatomic_read()``, ``qatomic_set()``, ``smp_rmb()``, ``smp_wmb()``, ``smp_mb()``, - ``smp_mb_acquire()``, ``smp_mb_release()``, ``smp_read_barrier_depends()= ``; + ``smp_mb_acquire()``, ``smp_mb_release()``, ``smp_read_barrier_depends()= ``, + ``smp_mb__before_rmw()``, ``smp_mb__after_rmw()``; =20 - sequentially consistent atomic access: everything else. =20 @@ -472,7 +473,7 @@ and memory barriers, and the equivalents in QEMU: sequential consistency. =20 - in QEMU, ``qatomic_read()`` and ``qatomic_set()`` do not participate in - the total ordering enforced by sequentially-consistent operations. + the ordering enforced by read-modify-write operations. This is because QEMU uses the C11 memory model. The following example is correct in Linux but not in QEMU: =20 @@ -488,9 +489,24 @@ and memory barriers, and the equivalents in QEMU: because the read of ``y`` can be moved (by either the processor or the compiler) before the write of ``x``. =20 - Fixing this requires an ``smp_mb()`` memory barrier between the write - of ``x`` and the read of ``y``. In the common case where only one thread - writes ``x``, it is also possible to write it like this: + Fixing this requires a full memory barrier between the write of ``x`` and + the read of ``y``. QEMU provides ``smp_mb__before_rmw()`` and + ``smp_mb__after_rmw()``; they act both as an optimization, + avoiding the memory barrier on processors where it is unnecessary, + and as a clarification of this corner case of the C11 memory model: + + +--------------------------------+ + | QEMU (incorrect) | + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+ + | :: | + | | + | a =3D qatomic_fetch_add(&x, 2);| + | smp_mb__after_rmw(); | + | b =3D qatomic_read(&y); | + +--------------------------------+ + + In the common case where only one thread writes ``x``, it is also possib= le + to write it like this: =20 +--------------------------------+ | QEMU (correct) | diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h index 874134fd19ae..f85834ee8b20 100644 --- a/include/qemu/atomic.h +++ b/include/qemu/atomic.h @@ -245,6 +245,20 @@ #define smp_wmb() smp_mb_release() #define smp_rmb() smp_mb_acquire() =20 +/* + * SEQ_CST is weaker than the older __sync_* builtins and Linux + * kernel read-modify-write atomics. Provide a macro to obtain + * the same semantics. + */ +#if !defined(QEMU_SANITIZE_THREAD) && \ + (defined(__i386__) || defined(__x86_64__) || defined(__s390x__)) +# define smp_mb__before_rmw() signal_barrier() +# define smp_mb__after_rmw() signal_barrier() +#else +# define smp_mb__before_rmw() smp_mb() +# define smp_mb__after_rmw() smp_mb() +#endif + /* qatomic_mb_read/set semantics map Java volatile variables. They are * less expensive on some platforms (notably POWER) than fully * sequentially consistent operations. @@ -259,7 +273,8 @@ #if !defined(QEMU_SANITIZE_THREAD) && \ (defined(__i386__) || defined(__x86_64__) || defined(__s390x__)) /* This is more efficient than a store plus a fence. */ -# define qatomic_mb_set(ptr, i) ((void)qatomic_xchg(ptr, i)) +# define qatomic_mb_set(ptr, i) \ + ({ (void)qatomic_xchg(ptr, i); smp_mb__after_rmw(); }) #else # define qatomic_mb_set(ptr, i) \ ({ qatomic_store_release(ptr, i); smp_mb(); }) --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864016; cv=none; d=zohomail.com; s=zohoarc; b=QiJ1MPnNR29nfxrt7XnkJcOGcW9gQChFVYolHN8jvFQJfjZbmi4pdRYUxKRWU0pRe6Z983wsUX9sN9LsWhHEHyHCO4RBHZwPOcaT00f9kS347HFKHBkonWvZYLyYeTMOsNfycdFB0l1f4F+mXNiqA27u6NRXXxZAsjTRMIuznDw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864016; 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=jEaglFNvLVCCHa29DJ9XvBF4adORWArJwgAKxmbV+/I=; b=iZUgjWHV4julPTagcuOMuY6UDEuTjFCRu8+sc14/BfKb/DqDVx39eC1y9OYw3vmpWKCCJ9nRkbRM9V2liE9TOSmK1Qwocyf+sQwBfScvuC1FO797F74C+wAU6RhYsJLICJoQpdQmvsCHwrLxroN4vEWwqxj4xY4YAYHQiASyNO0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864016113557.8351497383385; Fri, 3 Mar 2023 09:20:16 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94W-0005WK-0U; Fri, 03 Mar 2023 12:19:56 -0500 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 1pY94U-0005Vz-PY for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:54 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94T-0003SZ-2e for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:54 -0500 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-30-96Aao5rCMu26vuIOITgWfQ-1; Fri, 03 Mar 2023 12:19:51 -0500 Received: by mail-wm1-f71.google.com with SMTP id l20-20020a05600c1d1400b003e10d3e1c23so3144862wms.1 for ; Fri, 03 Mar 2023 09:19:51 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id c21-20020a7bc015000000b003e214803343sm6520180wmb.46.2023.03.03.09.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:19:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677863992; 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=jEaglFNvLVCCHa29DJ9XvBF4adORWArJwgAKxmbV+/I=; b=ede0uvsZQn/DaiInM7oCkQtBZcfSgsJQPSPnozcGjoOk0O4cLDrXbo45BpRmUpwFVdYHL5 LcPI70bwdK/dc+KsLmwwTDxT6ADPKK5mKcaGjs9+XQLJcpspmVy+q8lBei74+Phe5FFzTO vFu/h4bmpZ3u5UJRFt6tfRSJECfN4+s= X-MC-Unique: 96Aao5rCMu26vuIOITgWfQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jEaglFNvLVCCHa29DJ9XvBF4adORWArJwgAKxmbV+/I=; b=Es0hSHzrij7yVaFGX+ObC9M9TrJLoXVhR7+lMQgTJNuHZpewpRdQPLqAomWKffhv5l mGAiyywrXWEwh07OMt7FVGLAGhOYfPY7nQiitqnV7aDbJYo/L3gE1klbhzPMsh3cpsvL sESNW+sbF1J21718sinJh9e3fBfufXdYBip3Af34cvGS2y/Q1epnEqwXJQQVMFKeBGt6 UZkKEyC6lyjij0jqQF+WiZZhif/svMT6JJwuszkDpRgfs5+ecCBeLHxaevo1hPB+XBuO 9HZMSKXV3YBIwHHZp46BCQ8pB9J91vmnw+uYI63x30RgkpRmb8USehmKXFpqUdZpHUPu 2dJA== X-Gm-Message-State: AO0yUKW1loPm1DTkK2uAsAsGjZxQg9s7vThyBN1vfPX9JBDuIirxp693 XR8xQ/SNDX/L23SHvasNHrmUgmU3+SJo6YnGldi9GQckwMiDW++ykpkyI1Kx6b+JSqhUEVcAbN7 EleHuWwTX2SOQg5uKFeHswS5cJ6CSrFrLIywcy7QbBPIm2/PU1Lq6kry0De8TZxlnP8GOYgmp X-Received: by 2002:a05:600c:4448:b0:3dc:18de:b20d with SMTP id v8-20020a05600c444800b003dc18deb20dmr2353209wmn.33.1677863989522; Fri, 03 Mar 2023 09:19:49 -0800 (PST) X-Google-Smtp-Source: AK7set8ly9J7p1Cg8eQdabP7JGRzGPQ7IGIsrMOPta+3lRVuB+BI0mL0u92u/fNeprTh4kuM/91ACg== X-Received: by 2002:a05:600c:4448:b0:3dc:18de:b20d with SMTP id v8-20020a05600c444800b003dc18deb20dmr2353183wmn.33.1677863989139; Fri, 03 Mar 2023 09:19:49 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 2/8] qemu-thread-posix: cleanup, fix, document QemuEvent Date: Fri, 3 Mar 2023 18:19:33 +0100 Message-Id: <20230303171939.237819-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864016694100013 Content-Type: text/plain; charset="utf-8" QemuEvent is currently broken on ARM due to missing memory barriers after qatomic_*(). Apart from adding the memory barrier, a closer look reveals some unpaired memory barriers too. Document more clearly what is going on, and remove optimizations that I couldn't quite prove to be correct. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- util/qemu-thread-posix.c | 64 ++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 93d250579741..06d1bff63bb7 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -384,13 +384,21 @@ void qemu_event_destroy(QemuEvent *ev) =20 void qemu_event_set(QemuEvent *ev) { - /* qemu_event_set has release semantics, but because it *loads* + assert(ev->initialized); + + /* + * Pairs with memory barrier in qemu_event_reset. + * + * qemu_event_set has release semantics, but because it *loads* * ev->value we need a full memory barrier here. */ - assert(ev->initialized); smp_mb(); if (qatomic_read(&ev->value) !=3D EV_SET) { - if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { + int old =3D qatomic_xchg(&ev->value, EV_SET); + + /* Pairs with memory barrier in kernel futex_wait system call. */ + smp_mb__after_rmw(); + if (old =3D=3D EV_BUSY) { /* There were waiters, wake them up. */ qemu_futex_wake(ev, INT_MAX); } @@ -399,18 +407,19 @@ void qemu_event_set(QemuEvent *ev) =20 void qemu_event_reset(QemuEvent *ev) { - unsigned value; - assert(ev->initialized); - value =3D qatomic_read(&ev->value); - smp_mb_acquire(); - if (value =3D=3D EV_SET) { - /* - * If there was a concurrent reset (or even reset+wait), - * do nothing. Otherwise change EV_SET->EV_FREE. - */ - qatomic_or(&ev->value, EV_FREE); - } + + /* + * If there was a concurrent reset (or even reset+wait), + * do nothing. Otherwise change EV_SET->EV_FREE. + */ + qatomic_or(&ev->value, EV_FREE); + + /* + * Order reset before checking the condition in the caller. + * Pairs with the first memory barrier in qemu_event_set(). + */ + smp_mb__after_rmw(); } =20 void qemu_event_wait(QemuEvent *ev) @@ -418,20 +427,37 @@ void qemu_event_wait(QemuEvent *ev) unsigned value; =20 assert(ev->initialized); + + /* + * This read does not have any particular ordering requirements; + * if it moves earlier, we might miss qemu_event_set() and go down the + * slow path unnecessarily, but ultimately the memory barrier in + * qemu_futex_wait() will ensure the check is done correctly. + */ value =3D qatomic_read(&ev->value); - smp_mb_acquire(); if (value !=3D EV_SET) { if (value =3D=3D EV_FREE) { /* - * Leave the event reset and tell qemu_event_set that there - * are waiters. No need to retry, because there cannot be - * a concurrent busy->free transition. After the CAS, the - * event will be either set or busy. + * Leave the event reset and tell qemu_event_set that there are + * waiters. No need to retry, because there cannot be a concu= rrent + * busy->free transition. After the CAS, the event will be ei= ther + * set or busy. + * + * Neither the load nor the store of this cmpxchg have particu= lar + * ordering requirements. The reasoning for the load is the s= ame + * as qatomic_read() above; while moving the store earlier can= only + * cause qemu_event_set() to issue _more_ wakeups. */ if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { return; } } + + /* + * This is the final check for a concurrent set, so it does need + * a smp_mb() pairing with the second barrier of qemu_event_set(). + * The barrier is inside the FUTEX_WAIT system call. + */ qemu_futex_wait(ev, EV_BUSY); } } --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864014; cv=none; d=zohomail.com; s=zohoarc; b=nCUdalwrYKocjNhOye45/T1XgrnSoYvqTAlbTS1tHQdM18jOzHL2v/HRdxhs9omyOIp8/hWqKY01z/4CIeMmU1f5brtKr0QSHW4UwAPRtZ3sC70U+mdLHjL/3DwJzyKk+p2t7H7vMNOhDKbeEmWq/AG7f+mVzJJuKkCXActO/cs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864014; 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=TNbxaEYT2yUK3nTXHv39n1PC3lTAJOLL2zJjKFygI8g=; b=Bgs/m37kFcmNn6IyypC+c25Qs3YCGKS2mTQyMN0nnXnOsoI/7oI05lckeqYjPrZDHb+ckqf0eekBK+QRLIe7XYueHHuv2Asimo58obPhxa9aEjjJ3N9uOyttHeXCb35uNcG1RP0Fhop2BXH35AhGdT6FrYyh+a6i10PJYNxAu/I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864014068195.47986685359763; Fri, 3 Mar 2023 09:20:14 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94Z-0005XB-Hp; Fri, 03 Mar 2023 12:19:59 -0500 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 1pY94X-0005Wc-L0 for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:57 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94W-0003Sy-1i for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:19:57 -0500 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-184-x9K7Jv2GMvmneDDkdvTsZQ-1; Fri, 03 Mar 2023 12:19:54 -0500 Received: by mail-wm1-f69.google.com with SMTP id f14-20020a7bcc0e000000b003dd41ad974bso1299130wmh.3 for ; Fri, 03 Mar 2023 09:19:54 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id x8-20020a1c7c08000000b003eb2e33f327sm10353881wmc.2.2023.03.03.09.19.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:19:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677863995; 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=TNbxaEYT2yUK3nTXHv39n1PC3lTAJOLL2zJjKFygI8g=; b=Dl6+k36RTdUJvpxuy9FTSppI2FCkzuCi27gaAdBs/KAe5nqs7Qe6hlrvKJcZvsGQGNAUJi oU4hiDFq+Bpc7q2Ahws5Jxba/plSmspUBZ/eefb39TiZD//jzsd5wzgxZrxEHrBfXgHOKL Akyo3Oyulub5VwoVKQIVroZmjp4tX4U= X-MC-Unique: x9K7Jv2GMvmneDDkdvTsZQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TNbxaEYT2yUK3nTXHv39n1PC3lTAJOLL2zJjKFygI8g=; b=n8bD4goanswscOH2k86H4nYE/n/9j+rT1mBTlgUgRuiwUy7FKLYvNx3M2r6vFHUx0w Q402T6s6SfpyDIzKBv7a2Fo5ilhTQjXAQtjxhF/Y23MibIQExwQvRk8vAA479fEIiFWk mRrTFUas1BSpQ3DEfUQDuo0or+mgITnfS6w6cjlLDZuLYpReMd00jmQioTxDY/2u9xgB wt0ONeC9bZ+v2wEqxmIM6DavHc0yKojRTIJ8Mcc3OZqkPRNgI9NbtlbWrt58mx6zscdz oPhOjiscT0nQeGRnl1JoSIJakG6v75UapfKIFqc5/hktyCtC4rP7ufnnGFQRzLgPuaXC zDKA== X-Gm-Message-State: AO0yUKUB7aUZvjnj5SWAbTGdjYwjPLHNw5MOnu0xMGxmkNL0u3OgeV6i rsVnbYPmlBSMyITUI0kd40207fDIFCzTkRUFVmre2iEZ5ULp+Ig/BuSnSYUDr+v1gaGbyEuvWPf OyXlLm4WTLNa9mxHk+dswDpG/jtmMDIWKGUe6HwOTaqEAArZZ3hWQLYoDeL6r/CXV1BQUtGgR X-Received: by 2002:a05:600c:1d0e:b0:3eb:fc6:79cf with SMTP id l14-20020a05600c1d0e00b003eb0fc679cfmr2538188wms.6.1677863992577; Fri, 03 Mar 2023 09:19:52 -0800 (PST) X-Google-Smtp-Source: AK7set964RHi8HpWlNWftOqusP6XqUnpMuNUzDyEDH9rHQEb8dHIQ5BCY/56lKWyXqiHTmxF1z+fgw== X-Received: by 2002:a05:600c:1d0e:b0:3eb:fc6:79cf with SMTP id l14-20020a05600c1d0e00b003eb0fc679cfmr2538166wms.6.1677863992203; Fri, 03 Mar 2023 09:19:52 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 3/8] qemu-thread-win32: cleanup, fix, document QemuEvent Date: Fri, 3 Mar 2023 18:19:34 +0100 Message-Id: <20230303171939.237819-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864014822100008 Content-Type: text/plain; charset="utf-8" QemuEvent is currently broken on ARM due to missing memory barriers after qatomic_*(). Apart from adding the memory barrier, a closer look reveals some unpaired memory barriers that are not really needed and complicated the functions unnecessarily, as well as some optimizations that I couldn't quite prove to be correct. Finally, the code is relying on a memory barrier in ResetEvent(); the barrier _ought_ to be there but there is really no documentation about it; it only affects the slow path, so make it explicit. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- util/qemu-thread-win32.c | 78 +++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 69db254ac7c1..eff664ae6b31 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -272,12 +272,20 @@ void qemu_event_destroy(QemuEvent *ev) void qemu_event_set(QemuEvent *ev) { assert(ev->initialized); - /* qemu_event_set has release semantics, but because it *loads* + + /* + * Pairs with memory barrier in qemu_event_reset. + * + * qemu_event_set has release semantics, but because it *loads* * ev->value we need a full memory barrier here. */ smp_mb(); if (qatomic_read(&ev->value) !=3D EV_SET) { - if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { + int old =3D qatomic_xchg(&ev->value, EV_SET); + + /* Pairs with memory barrier after ResetEvent. */ + smp_mb__after_rmw(); + if (old =3D=3D EV_BUSY) { /* There were waiters, wake them up. */ SetEvent(ev->event); } @@ -286,17 +294,19 @@ void qemu_event_set(QemuEvent *ev) =20 void qemu_event_reset(QemuEvent *ev) { - unsigned value; - assert(ev->initialized); - value =3D qatomic_read(&ev->value); - smp_mb_acquire(); - if (value =3D=3D EV_SET) { - /* If there was a concurrent reset (or even reset+wait), - * do nothing. Otherwise change EV_SET->EV_FREE. - */ - qatomic_or(&ev->value, EV_FREE); - } + + /* + * If there was a concurrent reset (or even reset+wait), + * do nothing. Otherwise change EV_SET->EV_FREE. + */ + qatomic_or(&ev->value, EV_FREE); + + /* + * Order reset before checking the condition in the caller. + * Pairs with the first memory barrier in qemu_event_set(). + */ + smp_mb__after_rmw(); } =20 void qemu_event_wait(QemuEvent *ev) @@ -304,29 +314,47 @@ void qemu_event_wait(QemuEvent *ev) unsigned value; =20 assert(ev->initialized); + + /* + * This read does not have any particular ordering requirements; + * if it moves earlier, we might miss qemu_event_set() and go down the + * slow path unnecessarily, but ultimately the memory barrier below, + * plus the internal synchronization of the kernel event, will ensure + * the check is done correctly. + */ value =3D qatomic_read(&ev->value); - smp_mb_acquire(); if (value !=3D EV_SET) { if (value =3D=3D EV_FREE) { - /* qemu_event_set is not yet going to call SetEvent, but we are - * going to do another check for EV_SET below when setting EV_= BUSY. - * At that point it is safe to call WaitForSingleObject. + /* + * Here the underlying kernel event is reset, but qemu_event_s= et is + * not yet going to call SetEvent. However, there will be ano= ther + * check for EV_SET below when setting EV_BUSY. At that point= it + * is safe to call WaitForSingleObject. */ ResetEvent(ev->event); =20 - /* Tell qemu_event_set that there are waiters. No need to ret= ry - * because there cannot be a concurrent busy->free transition. - * After the CAS, the event will be either set or busy. + /* + * It is not clear whether ResetEvent provides this barrier; k= ernel + * APIs (KeResetEvent/KeClearEvent) do not. Better safe than = sorry! + */ + smp_mb(); + + /* + * Leave the event reset and tell qemu_event_set that there are + * waiters. No need to retry, because there cannot be a concu= rrent + * busy->free transition. After the CAS, the event will be ei= ther + * set or busy. */ if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { - value =3D EV_SET; - } else { - value =3D EV_BUSY; + return; } } - if (value =3D=3D EV_BUSY) { - WaitForSingleObject(ev->event, INFINITE); - } + + /* + * ev->value is now EV_BUSY. Since we didn't observe EV_SET, + * qemu_event_set() must observe EV_BUSY and call SetEvent(). + */ + WaitForSingleObject(ev->event, INFINITE); } } =20 --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864097; cv=none; d=zohomail.com; s=zohoarc; b=SCTV1obaQ6RyfG0mAb6SWe2FKK4bdLdOrCWAi2TxizDKUSDohpMqcO148E/Do3gS7Hde2a2K3XdtTlkrOOe/OnX0DB3Jfky8Rb/QF/hIAWpfIqomSBIwIE68D4cBY1Cuh94VcZk8bleqscoeYWD8arAAYPw8Ss2x48nxbN+rW+I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864097; 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=COQmYvo/zlgTTYKqBA+2dmUJpq3V6nJgMWRGO5kE4g4=; b=B6GqVKtlpdd7TZC2zkCOMGs9uEA39bAL632lyMD8TGnAFl7RyGwTIgC2O6HrFV3AR7wtNpdP2VQHijiwstaDEvJYALhCV7Adhqx61UsyVli15MrJ7fYspBWbnZqijddyWh8+eGmAjVTuoxLKONiIp+K8vtdqAUUan2PBkwwBSGQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 167786409755312.105420144041545; Fri, 3 Mar 2023 09:21:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94c-0005ZZ-5O; Fri, 03 Mar 2023 12:20:02 -0500 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 1pY94a-0005YT-AS for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94Y-0003TO-Sj for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:00 -0500 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-135-kK4d6XWUN0OQXpOBp_v_uQ-1; Fri, 03 Mar 2023 12:19:57 -0500 Received: by mail-wm1-f70.google.com with SMTP id t1-20020a7bc3c1000000b003dfe223de49so3130495wmj.5 for ; Fri, 03 Mar 2023 09:19:57 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id o42-20020a05600c33aa00b003e215a796fasm2807670wmp.34.2023.03.03.09.19.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:19:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677863998; 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=COQmYvo/zlgTTYKqBA+2dmUJpq3V6nJgMWRGO5kE4g4=; b=fLWRf7A76BniecqgO1YjCfCtd81TZK5kE7fJCj98DYWJ2yJB/ZKcII4T5bdEfITCb+brtV xkIDG+Ofq+xKsGvYw1XlhRfmAHclv1o22Lo4IAqz+Kc7Cu4WY252UvDdJ6WjbaDuwHTNhn L3BxBKzkm3TsSrV+qOoHBlEAJWm57rs= X-MC-Unique: kK4d6XWUN0OQXpOBp_v_uQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=COQmYvo/zlgTTYKqBA+2dmUJpq3V6nJgMWRGO5kE4g4=; b=S6fkJ1bdglY6wZ7suTPrLSqYkN3d7Hheb43g1l/d5OlPa/5/x0yzsRx0BkJZk5HGSL 5HA+BWe0bj66TXX8ryuIWpzhYfkuxBoF29E8KvFYmKGrZPH7q0RuVc+CkY4AOLEzADss XRJnVaMuphkyIZX6UobiNr8sZUvVGn3D0IirQiNZr4IMkclgg9Yo7UvKAPIqX7AvHIOI Uiv+Bxw7/XOlkyBLPFEkIGIKpPGlNKUQgipkBnjd6UIqgDlCqLufOeyweOJsJTUsBrWg 3PoCNnaOf9Ml1LxjRUsoprRHa+PR+qZZiffFxlvEJICbNwWlWB/1i9pY2uAjdJfER0ZZ LNlQ== X-Gm-Message-State: AO0yUKUx5eVP6f60XwRaXCxKiOnl7CNtsPNJUUF6mTLE5FeOD31U4Hoz +u/rPSE2XKTAiGDaVXE8I068lOrbm/K7VHxGkbZ1EfPyHUgKeV+7/Cqqu/eHWbPPhUAutXiNGxd lLi1gDnkLcJl3j81+DSlKLn5X02hYovv7Wx2+hvfLUlAESYvhtcZvKwmIHrxfysBqRMplb5wN X-Received: by 2002:a05:600c:1908:b0:3eb:39c3:8844 with SMTP id j8-20020a05600c190800b003eb39c38844mr2391012wmq.12.1677863995715; Fri, 03 Mar 2023 09:19:55 -0800 (PST) X-Google-Smtp-Source: AK7set+xMFEP1PIncD67epVyk0KdJTpnF5ILKF1hrwbjsHaTGaFR/EO9ycnnl1cHoLteC8Grw4mSwg== X-Received: by 2002:a05:600c:1908:b0:3eb:39c3:8844 with SMTP id j8-20020a05600c190800b003eb39c38844mr2390985wmq.12.1677863995362; Fri, 03 Mar 2023 09:19:55 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 4/8] edu: add smp_mb__after_rmw() Date: Fri, 3 Mar 2023 18:19:35 +0100 Message-Id: <20230303171939.237819-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864099137100003 Content-Type: text/plain; charset="utf-8" Ensure ordering between clearing the COMPUTING flag and checking IRQFACT, and between setting the IRQFACT flag and checking COMPUTING. This ensures that no wakeups are lost. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- hw/misc/edu.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hw/misc/edu.c b/hw/misc/edu.c index e935c418d400..a1f8bc77e770 100644 --- a/hw/misc/edu.c +++ b/hw/misc/edu.c @@ -267,6 +267,8 @@ static void edu_mmio_write(void *opaque, hwaddr addr, u= int64_t val, case 0x20: if (val & EDU_STATUS_IRQFACT) { qatomic_or(&edu->status, EDU_STATUS_IRQFACT); + /* Order check of the COMPUTING flag after setting IRQFACT. */ + smp_mb__after_rmw(); } else { qatomic_and(&edu->status, ~EDU_STATUS_IRQFACT); } @@ -349,6 +351,9 @@ static void *edu_fact_thread(void *opaque) qemu_mutex_unlock(&edu->thr_mutex); qatomic_and(&edu->status, ~EDU_STATUS_COMPUTING); =20 + /* Clear COMPUTING flag before checking IRQFACT. */ + smp_mb__after_rmw(); + if (qatomic_read(&edu->status) & EDU_STATUS_IRQFACT) { qemu_mutex_lock_iothread(); edu_raise_irq(edu, FACT_IRQ); --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864065; cv=none; d=zohomail.com; s=zohoarc; b=NkRbR9cBeF3BIYLpwk3RzyYDMUcxF9avQf+iqs1vz4BxL0DgUAqueWrCjEs9Yu3YXrS99yxf2QiGTmsWk5kGVJi0EQ9eFznqOfsg97JgsTL5nLPSASMq6aHlPX9YscPVaPRYAa2uQeXuGG9fXPS5uVI5jTqhc9kojV70YpMF7ws= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864065; 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=z6dc+PiE9+oOj3vx1rhfElc/CwN8tiroFq9k4yy+4YI=; b=ZXS3WY2osmwFNEuz1Eee61kjXXFs6xWha+xr7yQDOo6gJogZGWo7lTa2SISiOL3XtgRY/9qU7jVFtzZOusNp5jztR1KtMyR2XCMi9PEJKjJ0tf0Cn5hiPX8nHQ2Fzt9khzdwf9E/tB3lW5Xdv9J4ZhVACGvHRzhaESlTMsGwhTY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864065804148.470982665135; Fri, 3 Mar 2023 09:21:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94f-0005aw-5s; Fri, 03 Mar 2023 12:20:05 -0500 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 1pY94e-0005an-Ai for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:04 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94c-0003UR-Mj for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:04 -0500 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-mv4hcU1sOyq725JA_d99Sg-1; Fri, 03 Mar 2023 12:20:00 -0500 Received: by mail-wm1-f70.google.com with SMTP id k20-20020a05600c1c9400b003e2249bd2b4so1193976wms.5 for ; Fri, 03 Mar 2023 09:19:59 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id j2-20020a5d6042000000b002c70d269b4esm2579482wrt.91.2023.03.03.09.19.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:19:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677864002; 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=z6dc+PiE9+oOj3vx1rhfElc/CwN8tiroFq9k4yy+4YI=; b=AyefvkgW9Htf5opTcuaVZCAI1VViclEKlo+3QgO4Xs2upR/mp+DaSxJ+5HD8SOzrUJt04/ k1uOoQpnB4B/F7mfWPkej4NxdzX3b2TBIWjRsf/dgtix6IOBt40zrKcfcYQWkvRScw0mKI BTZwCEL5hVw67X4/cgwpwNk0er/27GE= X-MC-Unique: mv4hcU1sOyq725JA_d99Sg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z6dc+PiE9+oOj3vx1rhfElc/CwN8tiroFq9k4yy+4YI=; b=4SJh7uv7lle2DkTs+TjuVnL1zdo3yP6j6f72AjRtnzbaDB38vAWquRXM5BwG7VRF32 TLbhgJilLhVXuBnFRFQ4rW8qPJ7aLY1JIMxeOx2v+6l7dgTxXhVbAE1wSTh8m7UOtiJ9 a1W/OjhRGv4xP2kJ5vryhaKimLTP3bcewVaN5Lc7K/dYCRykOcAK9lLBt4xezzPRiMuP VR+b9Yt42vXgMVBUuyZzAi+cvQhWCalg16DyrtOebEe20XJUKaD0o/t3QKY7N0Gf6iu8 yCkL+gD33myCGo9dxfL+cMbYY4ZwzY4ppzuodJlqRvNCTs7PnhDxayWgfcOicgufSozk Usiw== X-Gm-Message-State: AO0yUKVRhJecXp+JOGbXRNcwobgslI7XeVpk7b1YVO7wS8FSG1/yd+/c p6qUNkrOmGIfLd/YqUzKSZ4R82JyCkyqR4TUeiCCJgq6AO9RHmsIbKMIhEhcfufuRiOCAlmMDQS nxmNX2g6OeKueocMKefRTyOPetxJXLixUDMRcdx7LR7B89VHQhBRLTvl6jASVOgxtGU/Wq+YC X-Received: by 2002:a5d:67d2:0:b0:2ce:3a46:cee with SMTP id n18-20020a5d67d2000000b002ce3a460ceemr1537701wrw.2.1677863998604; Fri, 03 Mar 2023 09:19:58 -0800 (PST) X-Google-Smtp-Source: AK7set8h4A8FJDd2kzcSusrREjHmquLkwQt4NfgmQkcYxbaUMCyNBkldEUrwYr0mUkCLPkdmJnn2ag== X-Received: by 2002:a5d:67d2:0:b0:2ce:3a46:cee with SMTP id n18-20020a5d67d2000000b002ce3a460ceemr1537685wrw.2.1677863998328; Fri, 03 Mar 2023 09:19:58 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 5/8] util/async: add smp_mb__after_rmw() around BH enqueue/dequeue Date: Fri, 3 Mar 2023 18:19:36 +0100 Message-Id: <20230303171939.237819-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864067054100001 Content-Type: text/plain; charset="utf-8" There is no implicit memory barrier in qatomic_fetch_or() and atomic_fetch_and() on ARM systems. Add an explicit smp_mb__after_rmw() to match the intended semantics. Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson --- util/async.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/util/async.c b/util/async.c index 0657b7539777..6129f2c991cb 100644 --- a/util/async.c +++ b/util/async.c @@ -74,13 +74,15 @@ static void aio_bh_enqueue(QEMUBH *bh, unsigned new_fla= gs) unsigned old_flags; =20 /* - * The memory barrier implicit in qatomic_fetch_or makes sure that: - * 1. idle & any writes needed by the callback are done before the - * locations are read in the aio_bh_poll. + * The memory barrier makes sure that: + * 1. any writes needed by the callback are visible from the callback + * after aio_bh_dequeue() returns bh. * 2. ctx is loaded before the callback has a chance to execute and bh * could be freed. */ old_flags =3D qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags); + smp_mb__after_rmw(); + if (!(old_flags & BH_PENDING)) { QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next); } @@ -107,14 +109,15 @@ static QEMUBH *aio_bh_dequeue(BHList *head, unsigned = *flags) QSLIST_REMOVE_HEAD(head, next); =20 /* - * The qatomic_and is paired with aio_bh_enqueue(). The implicit memo= ry - * barrier ensures that the callback sees all writes done by the sched= uling + * The memory barrier is paired with aio_bh_enqueue() and it + * ensures that the callback sees all writes done by the scheduling * thread. It also ensures that the scheduling thread sees the cleared * flag before bh->cb has run, and thus will call aio_notify again if * necessary. */ *flags =3D qatomic_fetch_and(&bh->flags, ~(BH_PENDING | BH_SCHEDULED | BH_IDLE)); + smp_mb__after_rmw(); return bh; } =20 --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864088; cv=none; d=zohomail.com; s=zohoarc; b=bEStQ8EZB85T86UP9DZ297QbLpcECBeMlh6q0cJ4EiRgk/iALFGoTfoO63i3tupWRwsUChNmN2TCfVLCRtievlYQBlORiXOaj1GCAtOVcnExFVUvkD+y1AWloKkZR4luFwmBtLnuVCK9kmO8B6qowMZV7KvG2ttDrMeGtsx5sgQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864088; 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=yuToPMiVADxMT2mY36PVNqAmrx6q3c6Vff/ClLGF4k0=; b=X6whV5oMA2ic1Ras9LPJPRxMsbPrgueyf5PD/Bkm6Jb+OKs3xwQZ7cP1C5Z/IBX5KnaYZW4Qrife1V4LXBEK2YegXvvm3DBiPwKMAjUn8w18N/lZVJcH6ppAk5WLs1gXnkkwUO6j2lgpOILUPc3gofNsmbL8fi/S0KWGXxz7sY8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864088604902.5665979758184; Fri, 3 Mar 2023 09:21:28 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94h-0005bV-U3; Fri, 03 Mar 2023 12:20:07 -0500 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 1pY94g-0005bD-AH for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:06 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94e-0003g6-So for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:06 -0500 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-471-5s2FIUeBMjm1RYOeFhcuyA-1; Fri, 03 Mar 2023 12:20:03 -0500 Received: by mail-wm1-f70.google.com with SMTP id p22-20020a7bcc96000000b003e2036a1516so3129843wma.7 for ; Fri, 03 Mar 2023 09:20:02 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id e5-20020a5d5945000000b002c3f9404c45sm2691795wri.7.2023.03.03.09.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:20:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677864004; 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=yuToPMiVADxMT2mY36PVNqAmrx6q3c6Vff/ClLGF4k0=; b=fzl5+2ylJtM9ecNLnYYnRrelfCErxBWxaAzdYBrO2xjL59rUrTsURZ9CvQ2GOlu3gods+l xI2S9RsL8SKk3TymKjCF4Mt6r/YLzsJXStj0rs2U0tp8CyzkCJw6ofvs209Un4ip4pDQ5T 1hVoX85L8pizgGa7EDHE2Cipw+rmhPQ= X-MC-Unique: 5s2FIUeBMjm1RYOeFhcuyA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yuToPMiVADxMT2mY36PVNqAmrx6q3c6Vff/ClLGF4k0=; b=iqRDn14pvaNlAtbClXItQKqJGyjy/SmKs1yO/MWaClpZ6P8WYceF+9t2OfWeHuZHlY 1KiVKcCqCCQU2nX2aeiNZ+lxoBMo7F6PS20OocW5CXiNccKh/TN5olM1AnVAvww4aaLF KDU++/Soo9wOfh5+MXnr+vxKaDDNpikbvjzn+AjrxxEs3Zv+BgjOzd2mFBd7BV0sJiG1 pJpEPizSfbB16+E+tlnixrorLCVSjE2n9DWiK0it2IIWPKX7nP+PxKGKR19uWXrY3n4f vXSxcMJ8HacUoyoB4Teyg6UjdZMjj0PKc4TQqjNOS6F1/KA8WXgxi8J+4u4LFf+e0BS3 xOWg== X-Gm-Message-State: AO0yUKWWH6cn40h5ny3vptbwV65PV98t9QbRtq3iikopD81xGyPW/yXy +5GB6RKAmQXZKypuFS9kcMjNHWDRPyVujj8oZmvq6YXsnp8XLJ+gDEFOjtJkjm5sB2aYtOHXUsB oNirrkNCly6paymOdTS7D3fI+qYcXByCGcRghZx2FlS+ok9+7iKzenEUN7Rk2u5C92PfoWB0f X-Received: by 2002:a05:600c:4592:b0:3dc:4b87:a570 with SMTP id r18-20020a05600c459200b003dc4b87a570mr2568831wmo.35.1677864001782; Fri, 03 Mar 2023 09:20:01 -0800 (PST) X-Google-Smtp-Source: AK7set9v/yhZvizQ1y5nSujp0ZtVhdg1UU5w/ygh8Xb6Idd01OdOfndCZGX7S1q3Me0J/gcl5RWvDQ== X-Received: by 2002:a05:600c:4592:b0:3dc:4b87:a570 with SMTP id r18-20020a05600c459200b003dc4b87a570mr2568808wmo.35.1677864001404; Fri, 03 Mar 2023 09:20:01 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 6/8] aio-wait: switch to smp_mb__after_rmw() Date: Fri, 3 Mar 2023 18:19:37 +0100 Message-Id: <20230303171939.237819-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864089443100001 Content-Type: text/plain; charset="utf-8" The barrier comes after an atomic increment, so it is enough to use smp_mb__after_rmw(); this avoids a double barrier on x86 systems. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi --- include/block/aio-wait.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h index dd9a7f6461ef..da13357bb8cf 100644 --- a/include/block/aio-wait.h +++ b/include/block/aio-wait.h @@ -85,7 +85,7 @@ extern AioWait global_aio_wait; /* Increment wait_->num_waiters before evaluating cond. */ \ qatomic_inc(&wait_->num_waiters); \ /* Paired with smp_mb in aio_wait_kick(). */ \ - smp_mb(); \ + smp_mb__after_rmw(); \ if (ctx_ && in_aio_context_home_thread(ctx_)) { \ while ((cond)) { \ aio_poll(ctx_, true); \ --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864093; cv=none; d=zohomail.com; s=zohoarc; b=ENTZ+5jEQcvJyq09lTfcXIYBPvUfPnMKuNBIVHWUobWZTEI1264uAZ4z9hI3K5hO+vkNjDx9JwvssHboK5XqASpjTmH+Du6VdwljXOxrqfm7/NS9aJCvXixbMgqUfUHx9oGw6dvBiBRK18d6kALd8EnsS52VCMO4unpum1VR+x0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864093; 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=+BoFUgybJ2RFTJiKUylassD3EZJCJvXGXsZmAgyGMEg=; b=mutMfeoM1f75ilUT8vY/2mXSSBRSRtD7/KKR5DjKn+90oWe0VXAQejJNMjQFXA+jSyVhDR5hU8XKuh1wig01Wbxb2shyJP+OZs7ckolNFtq9MSTVoya3DGBgYByi22/ccFVIKKDLXoey21vtztthyLyT0Fk9JEuoqC25i9BgnPY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864093825552.9996712459626; Fri, 3 Mar 2023 09:21:33 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94l-0005k5-7G; Fri, 03 Mar 2023 12:20:11 -0500 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 1pY94k-0005fV-9z for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:10 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94i-0003gu-6A for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:10 -0500 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-160-6H0Fq7hBMSqkiEwar7q_Bg-1; Fri, 03 Mar 2023 12:20:06 -0500 Received: by mail-wm1-f72.google.com with SMTP id l20-20020a05600c1d1400b003e10d3e1c23so3145162wms.1 for ; Fri, 03 Mar 2023 09:20:06 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id z1-20020adff1c1000000b002c70c99db74sm2652300wro.86.2023.03.03.09.20.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:20:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677864007; 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=+BoFUgybJ2RFTJiKUylassD3EZJCJvXGXsZmAgyGMEg=; b=SDadahCjAZGJH6+jW4/GUbpvVbdx3XOu4AK/X+g2n3xLHeB+Oa9wpEyFeoVjgJ685GxTE9 o+G37L0BdMHiGBug70rm3l+Rvh2w1jvdWzGR/mhcsv60XJWobG/p3zWhS0TkgU9qetWx5+ XCV+S+KC5qNNNhv1XOWiGIllyJ4ian4= X-MC-Unique: 6H0Fq7hBMSqkiEwar7q_Bg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+BoFUgybJ2RFTJiKUylassD3EZJCJvXGXsZmAgyGMEg=; b=NN+8d6NJIvggTGNO5F8WQ3XF4i2H9F89EZtaPiK2jg3zgHapecIr4Z4v7w/HYpdfUf jFiId2GA2l6oh0XjSzB/onbu0bRpaFlGJrWrNcPKEqV9nQlzQOmt19Z3AJKS/nD+Hodj Cenwpkw5WnEidvb0MmA6/ctDsFzRrXU/hVvHvJO95dRCb815UHT0lNnVICCklJNOosyE gZxG1BOfLVeKOuRUJ+aMNpPVsnDOepU1Qlj93XrSUvyWSr+6WWlUSiFoc6S11QQ4ydvZ 1azykvhFvgs2NVQH9fbd1pgKOZtDLuAor/i0hdaMMtNz7Wz8XHSPds7b3EDOZF9ZnHXx 5ceA== X-Gm-Message-State: AO0yUKXn9ztDiZm/8P7R+sRpdrXYnzEUAVt/hMIyznvIv1ALDPYpy/YD grE1k/p7dGX54BEa700GmiABh1s6P92wjFZm/gnhigKP6rC0K+tfA4A+6XBqG7eE7HNUBkiFwqV uGE7ILajPbG/pX/23gQTXv9VuonXBKzFloQHk7ICL1PbtcHAxfmwa0sUSh8r0Utt0DkqAJzP9 X-Received: by 2002:adf:e4d0:0:b0:2c7:190b:c3dc with SMTP id v16-20020adfe4d0000000b002c7190bc3dcmr1885237wrm.53.1677864004977; Fri, 03 Mar 2023 09:20:04 -0800 (PST) X-Google-Smtp-Source: AK7set89oWuQauyzBRCurusxBapUDofD+u4BFUzQZgKN0iRSHO1NzjDa8qWZYFlQL4nYcRu5W7nTNg== X-Received: by 2002:adf:e4d0:0:b0:2c7:190b:c3dc with SMTP id v16-20020adfe4d0000000b002c7190bc3dcmr1885221wrm.53.1677864004708; Fri, 03 Mar 2023 09:20:04 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 7/8] qemu-coroutine-lock: add smp_mb__after_rmw() Date: Fri, 3 Mar 2023 18:19:38 +0100 Message-Id: <20230303171939.237819-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864095110100003 Content-Type: text/plain; charset="utf-8" mutex->from_push and mutex->handoff in qemu-coroutine-lock implement the familiar pattern: write a write b smp_mb() smp_mb() read b read a The memory barrier is required by the C memory model even after a SEQ_CST read-modify-write operation such as QSLIST_INSERT_HEAD_ATOMIC. Add it and avoid the unclear qatomic_mb_read() operation. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- util/qemu-coroutine-lock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 58f3f771817b..84a50a9e9117 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -201,10 +201,16 @@ static void coroutine_fn qemu_co_mutex_lock_slowpath(= AioContext *ctx, trace_qemu_co_mutex_lock_entry(mutex, self); push_waiter(mutex, &w); =20 + /* + * Add waiter before reading mutex->handoff. Pairs with qatomic_mb_set + * in qemu_co_mutex_unlock. + */ + smp_mb__after_rmw(); + /* This is the "Responsibility Hand-Off" protocol; a lock() picks from * a concurrent unlock() the responsibility of waking somebody up. */ - old_handoff =3D qatomic_mb_read(&mutex->handoff); + old_handoff =3D qatomic_read(&mutex->handoff); if (old_handoff && has_waiters(mutex) && qatomic_cmpxchg(&mutex->handoff, old_handoff, 0) =3D=3D old_handof= f) { @@ -303,6 +309,7 @@ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) } =20 our_handoff =3D mutex->sequence; + /* Set handoff before checking for waiters. */ qatomic_mb_set(&mutex->handoff, our_handoff); if (!has_waiters(mutex)) { /* The concurrent lock has not added itself yet, so it --=20 2.39.1 From nobody Fri May 3 23:43:53 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677864101; cv=none; d=zohomail.com; s=zohoarc; b=bu/m0qrs3K8wiN5Ct+SsDxgGj8U8FmsODtSoltyB3z3D+FTszxmsVGiLi3RUc4OzOnIdMWIxagKU5MA/YjzRVknOZWw0CuSObt0QNnZ2bcvEyoLamB113mFu07mXbFvbjhjs2ugjBJhcvNhZ6MDTc4HjqJ3RzHlKVaAqjruO8Ag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677864101; 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=x9O6fJIqlLXWdx5dHdf4NLeETtgW5YiFIfwDZxywUT0=; b=jjcv0FZRAjZEqkocKb+s56FQZj+OL4BHBCz+RoXzKUzys2tY9dgBsXFbvGElUg2x2ZZ2rGIhzTflhw/RXOxBF2Ox4dK0JkJ3rxqkVFQS8Ax5oNLSuGxzGp0uylufS0SCl55qTF0MKMViFstVe5qh7WJN4AWATPV1gQmx0WeT0Ds= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1677864101884173.12969824886136; Fri, 3 Mar 2023 09:21:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pY94o-0005xe-S4; Fri, 03 Mar 2023 12:20:14 -0500 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 1pY94n-0005sO-BK for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pY94l-0003hZ-PF for qemu-devel@nongnu.org; Fri, 03 Mar 2023 12:20:13 -0500 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-649-EUsSPXWnNimNuEPm8aX0sA-1; Fri, 03 Mar 2023 12:20:09 -0500 Received: by mail-wm1-f72.google.com with SMTP id m28-20020a05600c3b1c00b003e7d4662b83so3158902wms.0 for ; Fri, 03 Mar 2023 09:20:09 -0800 (PST) Received: from avogadro.local (nat-pool-mxp-u.redhat.com. [149.6.153.187]) by smtp.gmail.com with ESMTPSA id e5-20020a5d5945000000b002c3f9404c45sm2692070wri.7.2023.03.03.09.20.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Mar 2023 09:20:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677864011; 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=x9O6fJIqlLXWdx5dHdf4NLeETtgW5YiFIfwDZxywUT0=; b=IPpKd8aGuHVNiZT4c9W6JyjDsbKmGweaqX+sTGoayzxAsLFI1X2VHM+FvmEEr6EDfPnVOi gELnzSju1sNZFapxltnDpPSBfcfmjXGbX7cvQzADt5G6hGO30wrbVvnWFUMMscaRKOQu+J PKhC0nPji7CDIpPIzH+m2GY1pDzOdTs= X-MC-Unique: EUsSPXWnNimNuEPm8aX0sA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x9O6fJIqlLXWdx5dHdf4NLeETtgW5YiFIfwDZxywUT0=; b=RNdny+3DwZqntG1JleDnZdK8bKq3HYbOLreuQ/8FiStqUqaIszC+7qg84yZAysqF1i GP26JfyzOHVBrtf4QIdtaGg0WLNJ8FkiBR3f8hDl+usCVGi7oyIZaus8uXY/pVqIHs38 o/oEqrp4JeBD+55Bi7L8FotHFpxdCNraOzrBEQ3jPk/L9LIr6HZQgMLjouuKoh3Fjpfy dZSYS3VymuBpIpbF01r2sJZmXfFj5Slt0ZN64R/FSsIbaooemolWIhTDp98xxzNKeXJR KaVeNaZY8xKSdMlsNWaKLHZ3y3+/AGmYpY5S61VlbcPq6BnmcgCC+OoU3tGF9DMpYTuP xPwQ== X-Gm-Message-State: AO0yUKWHH1rGmEPwl8LF7928jim/jf6Z02CyEr+3I5uxqtvNnhEKisY+ twZ3jjOMREGQ7+/HikmvZmkWx+gbrjsONf4xOrLhX8jtVwwU00WFH2oBxWKI60jPgc+b5uLHaBd vNZoJ08cL2eTl0e6QgYht3JHOYwBDkmj1JEFDS1G2rdc/kTX0iwkA6c0XHwGbl0YTfs/Pz0Zv X-Received: by 2002:a7b:cc10:0:b0:3eb:3104:efef with SMTP id f16-20020a7bcc10000000b003eb3104efefmr2262416wmh.31.1677864008200; Fri, 03 Mar 2023 09:20:08 -0800 (PST) X-Google-Smtp-Source: AK7set9cNa09/RKZDrhfaez8DvhU6nHTsiGgKgQntJhCFLHNqVT3pVWpVaaW0RX81XdzyG8Mab4v4Q== X-Received: by 2002:a7b:cc10:0:b0:3eb:3104:efef with SMTP id f16-20020a7bcc10000000b003eb3104efefmr2262397wmh.31.1677864007969; Fri, 03 Mar 2023 09:20:07 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: gshan@redhat.com, eesposit@redhat.com, david@redhat.com, stefanha@redhat.com, cohuck@redhat.com, eauger@redhat.com Subject: [PATCH 8/8] physmem: add missing memory barrier Date: Fri, 3 Mar 2023 18:19:39 +0100 Message-Id: <20230303171939.237819-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230303171939.237819-1-pbonzini@redhat.com> References: <20230303171939.237819-1-pbonzini@redhat.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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677864103128100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Richard Henderson --- softmmu/physmem.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 47143edb4f6c..a6efd8e8dd11 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2927,6 +2927,8 @@ void cpu_register_map_client(QEMUBH *bh) qemu_mutex_lock(&map_client_list_lock); client->bh =3D bh; QLIST_INSERT_HEAD(&map_client_list, client, link); + /* Write map_client_list before reading in_use. */ + smp_mb(); if (!qatomic_read(&bounce.in_use)) { cpu_notify_map_clients_locked(); } @@ -3116,6 +3118,7 @@ void address_space_unmap(AddressSpace *as, void *buff= er, hwaddr len, qemu_vfree(bounce.buffer); bounce.buffer =3D NULL; memory_region_unref(bounce.mr); + /* Clear in_use before reading map_client_list. */ qatomic_mb_set(&bounce.in_use, false); cpu_notify_map_clients(); } --=20 2.39.1