From nobody Tue Apr 30 18:25:28 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=1678142064; cv=none; d=zohomail.com; s=zohoarc; b=nHhuZ7Xnathqaq0uuBmWEgLry4uETHxfosjaTDEd6YdV6+HadASPUfMygMsTesMoVU3ub26hf30ROAjY+smuIepd6fXz2jUcdBYv5/oP+HsdEaJXKyIccPOhANeJKjc5MWt1F3TLbY8KAqgnFmeKVCP4jb9KE64OxUnf09QyHIY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142064; 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=Ea/eA4r/ivqEoDm9dQVXxGefx6FzskYoNsC2bXbbhBw=; b=ErMik9bmIyMvUop7ptwP18oVReFAwzfzms+WdMDTQ0MOSS5PIrH+jP+83kMPcEZKnyIZUw1xeV9CNZAJhTdyNX2pX9Yr4D8eHMKMGsvh2nrb20TOkp2u/l0YZXHhg947L9ZGH2cXoR8VhN0fkYnFzl5zhhsQPCOwBqwzwuTTzUw= 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 1678142064839836.8800938704; Mon, 6 Mar 2023 14:34:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOT-0004Ia-JR; Mon, 06 Mar 2023 17:33:21 -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 1pZJOR-0004Hq-Nr for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:19 -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 1pZJOQ-0003SW-2D for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:19 -0500 Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-240-3TMIAxsBNNesuafQuM2p1Q-1; Mon, 06 Mar 2023 17:33:16 -0500 Received: by mail-ed1-f69.google.com with SMTP id h11-20020a0564020e8b00b004e59d4722a3so5773364eda.6 for ; Mon, 06 Mar 2023 14:33:15 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id z71-20020a509e4d000000b004c2158e87e6sm5682498ede.97.2023.03.06.14.33.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678141997; 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=Ea/eA4r/ivqEoDm9dQVXxGefx6FzskYoNsC2bXbbhBw=; b=d6o3sv0bPtV5kSpBygAj2D/uyje5cnnnXzEwHt14vihdLryjwRuvM/jRUZMDkjWFlPiPqE 0nRbXfkKerfmc0ESCKl2URujJV9RRARZ8LQgxJ9jCac3fhNt5Czmjkq7eJXgqIEpLO+7Jn Suv+rpSfx7kXtXgf6sUytE2s7yNmv0I= X-MC-Unique: 3TMIAxsBNNesuafQuM2p1Q-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678141993; 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=Ea/eA4r/ivqEoDm9dQVXxGefx6FzskYoNsC2bXbbhBw=; b=nZPqDKG6t6w6d94mHDCbI/R/4YDYupNpqLXMVv2zwkEgYBUUhwTAupTxfvG3FXve5v CA3Jqe8NK7T44+pwCqiTCZH/Yh75VMIG7FhzIA6u9Mi9XR+606GEoLboQPX8uShQ8KhJ Cpxc7UEB9frynILiCIR2kJ8B/R+G/eRNVx+JdiP5li8EbVTd2xiiD5traLR8C4TrrXyb jDVllqJueq8mHDcZAMjPiZq3bBVbdrWsEmFTTy6QGWI79iAfW6oDBkesKT0K7IF8grAf 1IumdWDl8zY6CfYy01t1cDeZ2W1djc0qcKZL/bac7Hli+86GPHqpEZDov4aZNvRgprE7 tNFQ== X-Gm-Message-State: AO0yUKWg9Sofx7lbAFodM3jDf8/YhJn4DG4+1ail+7ri1B1FW+M6m2z/ 8p9LspnusH4LBav/CIfPsu266D2QEwYnT36n+8uhCsuzTAo3kyGxGa6V05PQ41pA0L7+vw8Mhww oKFY9XaC7UHFPECVMAXDvqsPLcJp2gRa87CFP3/eVAS4cR7lhEAQW6M/QoWyVGENQtEWSYmytdj k= X-Received: by 2002:a17:906:7803:b0:8e3:8543:8ebe with SMTP id u3-20020a170906780300b008e385438ebemr12370036ejm.57.1678141993014; Mon, 06 Mar 2023 14:33:13 -0800 (PST) X-Google-Smtp-Source: AK7set9sDpYEm0dwQAaG51+E6njNrSXZs72zCc1ES84kkYdDatGBBreMnPnTXWYnCBB33RjcpZ0PHA== X-Received: by 2002:a17:906:7803:b0:8e3:8543:8ebe with SMTP id u3-20020a170906780300b008e385438ebemr12370018ejm.57.1678141992616; Mon, 06 Mar 2023 14:33:12 -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, richard.henderson@linaro.org Subject: [PATCH v2 1/9] qatomic: add smp_mb__before/after_rmw() Date: Mon, 6 Mar 2023 23:32:58 +0100 Message-Id: <20230306223306.84383-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142065655100004 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 LDAR is also used for load-acquire operations, it also waits for all STLRs to leave the store buffer. Thus, LDAR and STLR alone are load-acquire and store-release operations, but LDAR also provides store-against-load ordering as long as the previous store is a STLR. Compare this to ARMv7, where store-release is DMB+STR and load-acquire is LDR+DMB, but an additional DMB is needed between store-seqcst and load-seqcst (e.g. DMB+STR+DMB+LDR+DMB); or with x86, where MOV provides load-acquire and store-release semantics and the two can be reordered. Likewise, on ARM sequentially consistent read-modify-write operations only need to use LDAXR and STLXR respectively for the load and the store, while on x86 they need to use the stronger LOCK prefix. 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(). Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- 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..633df65a97bc 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 (correct) | + +=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 Tue Apr 30 18:25:28 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=1678142038; cv=none; d=zohomail.com; s=zohoarc; b=I69gqmpy1+GoPu2XjGkxBGdRxSqW8uFHPkV5po7bZk1SJUY540uNaBNGtFxUh7NySzbsdzeKeiDZ2r92NLC1Hral4pafacssoRoDxRj5kU9JLDp1L1HBU/soHH7aCtRLI8lJNLlPpd3fKT1xPHAswjlMcp4+DDrOMsB6a/2piqs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142038; 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=GB/aIT12DbaEHGWq5+K6q22+aJN75bKBTA3rUyP93OQ=; b=IbKhu9XN08C1XzQ7RDzqkkj660aMSCegbxcJECH8Z6Jm8gcC1TWSKzXJ+7iZOnnWKIDt/mHd0NpQ8nQ0fKajTFbdqZi+B4huIjMNldWA2n1sLIHkiUxo4We62HWO4apZIPzIvowoXaYy7/7o3YeJtV2vw2onM1hESMxJMyJl/ro= 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 1678142038307559.2690949558892; Mon, 6 Mar 2023 14:33:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOZ-0004KN-Ce; Mon, 06 Mar 2023 17:33:27 -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 1pZJOY-0004Jw-F4 for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:26 -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 1pZJOX-0003WV-6x for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:26 -0500 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-556-BXTIud4LNIy5ZqvI1bLCEg-1; Mon, 06 Mar 2023 17:33:22 -0500 Received: by mail-ed1-f71.google.com with SMTP id fi8-20020a056402550800b004a26cc7f6cbso15893287edb.4 for ; Mon, 06 Mar 2023 14:33:22 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id v12-20020a50c40c000000b004bb810e0b87sm5798597edf.39.2023.03.06.14.33.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142003; 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=GB/aIT12DbaEHGWq5+K6q22+aJN75bKBTA3rUyP93OQ=; b=Sv5Cy95W3dmfhtGTM2Cc55dDi6XI9kP9/ZvQWy73+LTKAGM7PeMzjxq89NHC0I/Ki252Xf 8cIDAPJ6dd4LYENUzvs6MqEj15kiOiAXE4PDHGc2PRPE53rF0g4KAzJybDbP7LHO/0gvmC tof3VtnPeGjMT4W/XdfrzHK5q7umyY4= X-MC-Unique: BXTIud4LNIy5ZqvI1bLCEg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678141996; 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=GB/aIT12DbaEHGWq5+K6q22+aJN75bKBTA3rUyP93OQ=; b=NjhmPu19kEDqRCXuDbOgtCHNLFid/6Gy7slOfmSK63MvRJIPBiYekb2AJ7WjB4Cmu3 cND2x/qdJLj27SoG5DNLqfT/0XLWSzWSSiTW1Ptf8cLA/gPllKTjEyRp/dj7nI27Kl7T 7Vp4f1fxO22l9N9yEiX/OZKBsSjieks12RTbmBH3N06BF2CKkZYKI2gXGyjoASP/HD50 rJ4EFyievCfKuGapndvmZiV+KvX392AmVaV6z3r1UnbDjfsYEN0T652hUO8nK49hZ8zc HI+O516kXuyAn0XThvXQprbGXQCdn8t2IoYu7iIy4HBu6OvLV3/F0JCItR/Z7yr340EW 1fpA== X-Gm-Message-State: AO0yUKW65d5hQABHyhaXG+L4EzFOkVImykd+E4GooByB/leP2zHaUff+ EMFg1r9qrn2Scl9ujy2BjcZLpG4TqOP+ZeyklPcxxVWZlXfWn3a9NKmwf5xzOS1bWEXA5EezxaH ITMKzkbIhjz2EhEc+S8uC3+lfMdoLy5ns7Dg9nbQ3EeOuH1lflUZAgmSY+yjjpUf6gW8o9lHYfO U= X-Received: by 2002:a05:6402:1506:b0:4ac:c7b3:8c27 with SMTP id f6-20020a056402150600b004acc7b38c27mr11397646edw.28.1678141996650; Mon, 06 Mar 2023 14:33:16 -0800 (PST) X-Google-Smtp-Source: AK7set+1PUB99owqGLZbY5HtyeUmr+oXHKPNfsARiRrh+urcWVgcEf70EWrJGpVA450CIczlJKBjxg== X-Received: by 2002:a05:6402:1506:b0:4ac:c7b3:8c27 with SMTP id f6-20020a056402150600b004acc7b38c27mr11397619edw.28.1678141996344; Mon, 06 Mar 2023 14:33:16 -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, richard.henderson@linaro.org Subject: [PATCH v2 2/9] qemu-thread-posix: cleanup, fix, document QemuEvent Date: Mon, 6 Mar 2023 23:32:59 +0100 Message-Id: <20230306223306.84383-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142039705100004 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. Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- util/qemu-thread-posix.c | 69 ++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 93d250579741..02f674b207cc 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 both qemu_event_reset() and qemu_event_wait(). + * + * 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,40 @@ void qemu_event_wait(QemuEvent *ev) unsigned value; =20 assert(ev->initialized); - value =3D qatomic_read(&ev->value); - smp_mb_acquire(); + + /* + * qemu_event_wait must synchronize with qemu_event_set even if it does + * not go down the slow path, so this load-acquire is needed that + * synchronizes with the first memory barrier in qemu_event_set(). + * + * If we do go down the slow path, there is no requirement at all: we + * might miss a qemu_event_set() here but ultimately the memory barrie= r in + * qemu_futex_wait() will ensure the check is done correctly. + */ + value =3D qatomic_load_acquire(&ev->value); 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. + * + * This cmpxchg doesn't have particular ordering requirements = if it + * succeeds (moving the store earlier can only cause qemu_even= t_set() + * to issue _more_ wakeups), the failing case needs acquire se= mantics + * like the load above. */ 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 Tue Apr 30 18:25:28 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=1678142063; cv=none; d=zohomail.com; s=zohoarc; b=GNzNHnFxOfNPMtbUpx8T5RDdDElH6g2BPrRjGvdvnK0mCNlM4bwSJZTmR0jNF5LdUBe1+DXnS2TbGgJwWjaFBxkfqf90I0L+pTNmK+yCLbeE+aJY7x5qZlLwR542FXAtv7pXkMjFDe67VJMiYxcvKBmotAR7c5KIaap0gPsUfVw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142063; 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=xtOY8UNV2DfBT+9KGY5mEbR/7/aOCUaif3dPvh7XgAU=; b=QdLUu9hY2pUBdxNzFWTK/W1xjJgV0bCOk5Jds4T2Kh6S2X2oDWskwVyHUNy+zPk7o8RWeJptzil1Zctk5M+niNv3bVlF8gmhZHXrz+lMcui4D8mYEc49XDqlj+Cr/9wUAvuFV4odY0ff7gYCgNB6ndWI3xFQj508BhOvPLc2XqQ= 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 1678142063700680.7598307156998; Mon, 6 Mar 2023 14:34:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOa-0004Kt-3f; Mon, 06 Mar 2023 17:33:28 -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 1pZJOY-0004Jv-DT for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:26 -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 1pZJOW-0003W7-Vw for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:26 -0500 Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-7LKNNxPmMZqtsmyqRmU-Ug-1; Mon, 06 Mar 2023 17:33:21 -0500 Received: by mail-ed1-f70.google.com with SMTP id w7-20020a056402268700b004bbcdf3751bso15758096edd.1 for ; Mon, 06 Mar 2023 14:33:21 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id t26-20020a1709060c5a00b008ec793ac3f4sm5088070ejf.192.2023.03.06.14.33.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142003; 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=xtOY8UNV2DfBT+9KGY5mEbR/7/aOCUaif3dPvh7XgAU=; b=hZFLjoSPbDv0VF0oMYbhXS3jTeQpPjjoCCdz6KVbia8PW+arhwzMpAHJ87r5fugTykMpb3 mCL8jaSEbWROn06v2CRARya1o3H+HJ+Ca0ENVw0cE2kYgJQINkQJ0ZdXEqZ5m0XJAiX6yI wes0Kj8/Xrl8DnXPzQ4L2Re7q6YHMYs= X-MC-Unique: 7LKNNxPmMZqtsmyqRmU-Ug-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142000; 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=xtOY8UNV2DfBT+9KGY5mEbR/7/aOCUaif3dPvh7XgAU=; b=WhUexkql0DUk0N7v26CO6rozhFRJT9h8SFhz07q79TR0IN98DbShGCk4Pvw1GCSSC9 /N2yQGSvik7aUX0eiMGTxnYlBqYq+hdcqAAA3na5cnnSGHKN1X6sa1gNeBLrVQJDBsbl KvMnJl89LxIb6sT5bgyFSb5EsV3y7BGo5h8WNoQIVS/Z8RFdyTxbv07sTqBrJM28q6MH ec2u8QUmvGkdDwZhYtG6g0SyMj9Xk5G+DubtpvND1esFcTb7WqjMdFdai5newgwWv4V9 CRhqOS5KXqfUSQybMVndW8XCKw03/T1PQJs2RM+Dve+Kj4Yczj027myadYCMnJCEKwY+ Lzgg== X-Gm-Message-State: AO0yUKVnrmwnjNNatqA/LKbBraDPwklLe6eEm1zQSxKJkwfe52LRz2gG Afek4Z8x9RCFivlHFjetbu9jAN/kMM5BptPviueukV3F4t69Jv2OIa6M/ELpSSCevMUJBuwnS+1 zJmXInvDB+xEV3KkfvfOjZVNXMMaonJ6RLQwZ0khOC5y0/jwLWHufgWbji9iBiNIFZJ9SA4qcgJ U= X-Received: by 2002:aa7:c14e:0:b0:4ac:d2cd:81c7 with SMTP id r14-20020aa7c14e000000b004acd2cd81c7mr12355275edp.5.1678142000172; Mon, 06 Mar 2023 14:33:20 -0800 (PST) X-Google-Smtp-Source: AK7set8L4o/E2dsqmby2n3IdhzUxTZiCHLCgCBEBd4x+nfYZ57O1k/ryFepz+uorZefnL/QM2My1jg== X-Received: by 2002:aa7:c14e:0:b0:4ac:d2cd:81c7 with SMTP id r14-20020aa7c14e000000b004acd2cd81c7mr12355257edp.5.1678141999763; Mon, 06 Mar 2023 14:33:19 -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, richard.henderson@linaro.org Subject: [PATCH v2 3/9] qemu-thread-win32: cleanup, fix, document QemuEvent Date: Mon, 6 Mar 2023 23:33:00 +0100 Message-Id: <20230306223306.84383-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142065645100003 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. Also, it is relying on a memory barrier in ResetEvent(); the barrier _ought_ to be there but there is really no documentation about it, so make it explicit. Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- util/qemu-thread-win32.c | 82 +++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 69db254ac7c1..970ba5642d7b 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 both qemu_event_reset() and qemu_event_wait(). + * + * 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,49 @@ void qemu_event_wait(QemuEvent *ev) unsigned value; =20 assert(ev->initialized); - value =3D qatomic_read(&ev->value); - smp_mb_acquire(); + + /* + * qemu_event_wait must synchronize with qemu_event_set even if it does + * not go down the slow path, so this load-acquire is needed that + * synchronizes with the first memory barrier in qemu_event_set(). + * + * If we do go down the slow path, there is no requirement at all: we + * might miss a qemu_event_set() here but ultimately the memory barrie= r in + * qemu_futex_wait() will ensure the check is done correctly. + */ + value =3D qatomic_load_acquire(&ev->value); 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 Tue Apr 30 18:25:28 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=1678142095; cv=none; d=zohomail.com; s=zohoarc; b=kno/qlXTjG6FwHTTu1RWMUuICBotiysb/HJRXjl7zF7irDymudNEd/b0h75lajGhFm1oj6FsD7j1x599yCT55wAimpH5z/2UnfVVMhyA/xSHIqzoOysEvB/Mw06CsPieaOgxFp9wjpV3W/4ijHXd2KOUshrTzTC1LXhWk6KS6oY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142095; 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=CeETWxXZIcowGLZFcYg1ThDXph6+WV41nOgYMMm09S4=; b=L2357FlOJ95qq1M3sDFYsOztt3tWF9iwAB1JiG3oJbTH6L0PDxckjvsBy3FInBt4VLnWGisFQIjxY93rK2mHXURwiQ+uJsQf2xRj5Dlta8rb2kcdw6p1DMithFOM6iLMxiXhouFgvJ3O+kwqFQ4pikPanY65EOauD88JJsapPts= 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 1678142095774508.074596593411; Mon, 6 Mar 2023 14:34:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOa-0004LS-G1; Mon, 06 Mar 2023 17:33:28 -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 1pZJOZ-0004KO-Jx for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:27 -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 1pZJOY-0003Xp-F9 for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:27 -0500 Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-299-QjVaP087MeWTEOya6MFvdw-1; Mon, 06 Mar 2023 17:33:24 -0500 Received: by mail-ed1-f70.google.com with SMTP id k12-20020a50c8cc000000b004accf30f6d3so15926067edh.14 for ; Mon, 06 Mar 2023 14:33:24 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id t26-20020a50d71a000000b004be774e4587sm5724898edi.80.2023.03.06.14.33.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142005; 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=CeETWxXZIcowGLZFcYg1ThDXph6+WV41nOgYMMm09S4=; b=bHDSMHbNtQHwBzbnORGm/E6RU6KU+4O/LO6pM2kMza/0VqhmJT1j9PypzWRswFDazf6nVq e23YzHTMRdFJhNZ3+3xp4aPbGREnD7gj+eqrrSU2cQ1PXGYKXaWbMZFD0vpYSs391N/Lha zGTudQbEQVmIdUv8xVpbNwjsY+OX4OY= X-MC-Unique: QjVaP087MeWTEOya6MFvdw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142003; 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=CeETWxXZIcowGLZFcYg1ThDXph6+WV41nOgYMMm09S4=; b=cOtqNdRzS66+UuJ+NOqm7v2YkY+gWRzIlcXMox7ZR+p2D3uDDxwQliWWBnyviC4noO vrRPIKsXoEj0ABJUbJAbueEvcGVEdlaO8k4+0viQtiOOykhrzIB51SUEbrpTUwBvWQ12 vIO7v1eBOzfVYxtb26shzbH2JnT+yyLeEwxyb/4kh2Ps/Nmexlk4qE5dT5QD/hzUlFvd reUDcZUuqE01MgtTHJoolxwZeeRKcTLJ9u3nIVhvtjiAHR/VQyug3sYGSRot8wRGGUXH aNhxQyDigTKy2mMYnPnQOQTR3A9fHJxKhPuG+FS4UH1lw0PZ0EkmU25Vfw3skaiaN2+R aKiw== X-Gm-Message-State: AO0yUKVm9RHHuS6VHV3q7bS5aUOda/iK6CLUu/1uh2t2+jqRAWMvekUk SqHXLk/tqXPjOfNoJBQLc4klW9VWYuPN5JRrXF9RQXRLjPJsmhF7JvkYO/UTlaOE6f0nMvNvmyN JO8U4hE5LUK+VC9mK9dXLgY10FpAf1ar2wwUHAVVNXjLAEqZDSF3qnBmCyF11a6bWpzg2yl/z8k M= X-Received: by 2002:a17:906:d928:b0:8b1:7eaf:4708 with SMTP id rn8-20020a170906d92800b008b17eaf4708mr11339128ejb.65.1678142003047; Mon, 06 Mar 2023 14:33:23 -0800 (PST) X-Google-Smtp-Source: AK7set+E7uyWHbKTQwWCKcwgxT+WO4aFBFpJj0p8ZvwrODQKp74DCt0dtqMIU+dl2HdElMFTVzSEeg== X-Received: by 2002:a17:906:d928:b0:8b1:7eaf:4708 with SMTP id rn8-20020a170906d92800b008b17eaf4708mr11339111ejb.65.1678142002740; Mon, 06 Mar 2023 14:33:22 -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, richard.henderson@linaro.org Subject: [PATCH v2 4/9] edu: add smp_mb__after_rmw() Date: Mon, 6 Mar 2023 23:33:01 +0100 Message-Id: <20230306223306.84383-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142097771100007 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. Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- 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 Tue Apr 30 18:25:28 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=1678142038; cv=none; d=zohomail.com; s=zohoarc; b=T0ButsggRFWPTUwvh9XEIbgNXeNXp8LSqjO3fk/OXcOuCMJyFlk8rcTf3DXJVm2TEYOeBNnsJwajMeBZdU3LfB3z7tr3rbaTUzfMThfi78erv2fZlalkg3G515/lss5RQLsJ5UneTgoafCbfv+pi+tfGncnVoEtiK/7yMD0jch4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142038; 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=PDtlNyvpurp2Cm3xJcc1yMYSCuDM+6/Oexiu9ICXnaI=; b=TnxkG7pl+wELIC1Dkr+NrtVud5q0x4DMvFS4DK8L7J5iwdfWPW5F7VN9MSJDmQyTyI4xp1tKPb85SSvc0BwvU4n4/BDuQeoQti5PTlpXeVVIaL18iBmuIwmuhAqqWa27sbwnPng5YyVs1CVIya3aBjq1xQaIzp/lUVFnnqc61pM= 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 1678142038414163.15116543554564; Mon, 6 Mar 2023 14:33:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOe-0004NZ-14; Mon, 06 Mar 2023 17:33:32 -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 1pZJOc-0004Mv-Hh for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:30 -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 1pZJOb-0003YB-B1 for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:30 -0500 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-BgRjYIrVMhORR6MnXQnqMw-1; Mon, 06 Mar 2023 17:33:27 -0500 Received: by mail-ed1-f72.google.com with SMTP id d35-20020a056402402300b004e37aed9832so6509543eda.18 for ; Mon, 06 Mar 2023 14:33:27 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id v12-20020a170906338c00b008ba9e67ea4asm5154594eja.133.2023.03.06.14.33.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142008; 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=PDtlNyvpurp2Cm3xJcc1yMYSCuDM+6/Oexiu9ICXnaI=; b=RS6gTSgxaplRuoff3ZwEXIiCt4TYJjzd25FQuuf+3Nhi6SLF2YWs/tO8s1c6rrwFn3xj3C zY+XLMjz9h0vG8BZYAPeu4hDFSG+l+zQoOI7JCBH9MxKM98jAJ7DMGUKeevzs5mqVPHyN6 XP+rz++GvTOKSx94eW7j4Glw+UHh4EI= X-MC-Unique: BgRjYIrVMhORR6MnXQnqMw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142006; 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=PDtlNyvpurp2Cm3xJcc1yMYSCuDM+6/Oexiu9ICXnaI=; b=hChbW0xoH2l/P1XkZw7v68BbH0tzATNRctCuiYsIznxfucsQaSOkXwSB21bXCj2dcH 1B2RgbieSR7YIMvK7eNvXzo/elMm3NuQJt0OiWiXkd54Q8zm5OVvSdaDsaQX/jkoBI2o DTjSaTAlcEznIioN/K8D4n5Ybflo59LK1koElsCUKKfKeRfzRSSFFBeAgP3SNcfyX4n2 /WJEWSOTe9/xUHeR+e573+7bBC23bsxkkS3IGrOCXSln+vKGuyUkKSqiqOllVHoDQ9+b bJ9KTRHkq4q2D6NGvlQuvmAZWVLcKhvuY6pDzVfp8O7DTcMAMFjDuLQVc9KMBji2QU0b PbDA== X-Gm-Message-State: AO0yUKVzxHFERy3cQ7CpKaVO18nZyrpwKGcMc8TaJJbd46jdkLibX0Ob H7bhQUecT6b4aScXIKhKKnxDYxdHklRqJ9tdT9X/lzCFOOuSnhk+3Ji2F+y7NwcxN3GznLcrPTN fGN0TtOnf6n0w/8HNr9GJiYH4PU3ywn5Srcfk00KWkJUsPdiWT6YpXpy9dgh6xXCS/JdtgkYzK/ o= X-Received: by 2002:a17:907:9702:b0:8b2:d30:e722 with SMTP id jg2-20020a170907970200b008b20d30e722mr15987783ejc.3.1678142006090; Mon, 06 Mar 2023 14:33:26 -0800 (PST) X-Google-Smtp-Source: AK7set+zLAL5/HeUHoEfMnyEVOtw0KnQh2C5cx80Vnpz55rNIky7NzLRHfJ1hwbZnjpZBhvX3ki0xw== X-Received: by 2002:a17:907:9702:b0:8b2:d30:e722 with SMTP id jg2-20020a170907970200b008b20d30e722mr15987757ejc.3.1678142005742; Mon, 06 Mar 2023 14:33:25 -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, richard.henderson@linaro.org Subject: [PATCH v2 5/9] aio-wait: switch to smp_mb__after_rmw() Date: Mon, 6 Mar 2023 23:33:02 +0100 Message-Id: <20230306223306.84383-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142040328100007 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. Reviewed-by: Richard Henderson Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand --- 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 Tue Apr 30 18:25:28 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=1678142065; cv=none; d=zohomail.com; s=zohoarc; b=DPbUIKZoQbodcQtb/5f05ulol56jrheqOpPEvcujV5b6OQa/SN8D4egOhGDlhimU8qWEzQVjsoBsK7AOnZjbkh6w268FuZsIAb2veb/K5cCRSrdX57b7CA9rBsiZIwMZ6mE1XdpMFOCnfGNO6I+yx7skNsOwamwNJyNuwKu1GB4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142065; 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=mOHH4yPNJZf+Sa0450BcZZrU7cumuCnXGhvsXLoiMX8=; b=NHvelQyL6EOp7Aa2OyGTuwftriaz1x9mp8vHhED+2QYfnFoP8AxIro0EAnpjznuIR9RsaLfQ8l7JIaFQfH0e7xLvAzPBWVw3cqiJmf2shoJUMuMbSSLf8demF5xn58uTX5t8g4heGha7x86/FK9Sbpa1H3HDGbsIVOpvslXiN88= 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 1678142065820829.0020512576685; Mon, 6 Mar 2023 14:34:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOh-0004PA-1M; Mon, 06 Mar 2023 17:33:35 -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 1pZJOf-0004O5-Gp for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:33 -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 1pZJOe-0003YT-8G for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:33 -0500 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-646-YGC-_Pk_Nl6Ad4MBdmmSRg-1; Mon, 06 Mar 2023 17:33:30 -0500 Received: by mail-ed1-f71.google.com with SMTP id y1-20020a056402358100b004ea439d57b7so3694951edc.12 for ; Mon, 06 Mar 2023 14:33:30 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id dx11-20020a170906a84b00b008e493b7bb61sm5089287ejb.153.2023.03.06.14.33.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142011; 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=mOHH4yPNJZf+Sa0450BcZZrU7cumuCnXGhvsXLoiMX8=; b=X+2k4iU2GYI8SPXoA6l218yLSa1YOx8xOPLM3sX3rPbytGFn/rSpTvKbVQ8I48UeX5bM6V Gz4+1scSjDAR/j0qiDBCBR2exJj+UWCoHocMgnId0/yirDQtVpSLhxLK0k/CiqAf38ria2 JQxY1A85wW5yi0bImsdz/k042TPHLxs= X-MC-Unique: YGC-_Pk_Nl6Ad4MBdmmSRg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142009; 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=mOHH4yPNJZf+Sa0450BcZZrU7cumuCnXGhvsXLoiMX8=; b=TiLkg7DPpwpeKGpxedF9cPedoIa+pk7Lz7j5pxp0OLAX2xcM3oplJghOexAnAzde9l lMuzIg/qsT1lr1vlk8Tvd3851SC2vZlBudUvF06I/dqtH8fRnOTCZ2rdxDMTsL+2FPfF 0rqR/2woZ2xAaWjD8s3iQuH8BRNW0lBHNDXsABHYA5eQXjrX8giSbi2DBsMhqBZ2UkGs /Qkb78jIsrRH4/XnSQB/FEgDETngXPy6abQtNx5B6zuvG6kcdWXlzHcComeCUo9C3/3D eCXAciXsj7X3poDIrW0CYhTrfR6ZYPlDSpqKtNQYlZJPUwS8q9jJnNtDuaXPtU/Xi3hL H8zg== X-Gm-Message-State: AO0yUKWZxyNCzH+HnXACtU7V2YNmDHZt0pFkff48hsN5ZE/0HUtkUPG4 WkKN/0xliwcuekfGyOnjv9Tar2OYwQE0gIOQ4ppa8wiIW2borogvCFqBe10iQyiJN8jUPWTLRJR G5YQs2QDlM/fYj9J+NWd/4gAG56kPdOVavtegzea0gj5HkYl1cOCA4cgoxqAqCbPWaISMRVKhF2 8= X-Received: by 2002:aa7:c1d6:0:b0:4c1:88e:6ac9 with SMTP id d22-20020aa7c1d6000000b004c1088e6ac9mr11309673edp.10.1678142008895; Mon, 06 Mar 2023 14:33:28 -0800 (PST) X-Google-Smtp-Source: AK7set9ZOQDrKBDC+rHid1ppyEqqqrU4e3oCRRUFTha6BjHi1Z5w/FeAF6EqHD+wBDNGu2WOsfoyEg== X-Received: by 2002:aa7:c1d6:0:b0:4c1:88e:6ac9 with SMTP id d22-20020aa7c1d6000000b004c1088e6ac9mr11309658edp.10.1678142008624; Mon, 06 Mar 2023 14:33:28 -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, richard.henderson@linaro.org Subject: [PATCH v2 6/9] qemu-coroutine-lock: add smp_mb__after_rmw() Date: Mon, 6 Mar 2023 23:33:03 +0100 Message-Id: <20230306223306.84383-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142067617100011 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. Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- 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 Tue Apr 30 18:25:28 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=1678142057; cv=none; d=zohomail.com; s=zohoarc; b=MR2taelh5czN1mDfgML3A9FmZdAdyhrDi1HFjS0M46UDuXGklaZhcLSfHn9EiiF+03bfi8ocA5atSP1YDP4g7fAqf+gvVytyqdrtJfSDfvn4DBPnFohkoPcp7GyeOn+tSQCHlQvNH0HTYPTw2SxUT3ekFyhwwL/oebqJhQ6Nmt8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142057; 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=Pmv4GODEo+jntdE66L5djocg5OACgVqeZY4tkfhD1zA=; b=XxjOz7XLysCYdFqrIbv3L+//1Lzu86RZqGvORN8fz9+nyqfSj3HxQ0yxL4a1HdH8J3miH/mjsk5okkMXtZntVBcr+QeT4UljR54tQcoSQAyBGmgVmnKHG/tIoxAUP+Gt7+ja9glCIsOrrvIDJ26lkAS/8bfEgOrFojCJz+EqlrA= 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 1678142057685900.7401235631953; Mon, 6 Mar 2023 14:34:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOj-0004RD-KB; Mon, 06 Mar 2023 17:33:37 -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 1pZJOi-0004QJ-0i for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:36 -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 1pZJOg-0003Yn-SO for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:36 -0500 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-361-k3eCQ0DQOMiBi5PhIuGZMg-1; Mon, 06 Mar 2023 17:33:33 -0500 Received: by mail-ed1-f72.google.com with SMTP id u10-20020a056402064a00b004c689813557so15672835edx.10 for ; Mon, 06 Mar 2023 14:33:32 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id ca5-20020a170906a3c500b008bc2c2134c5sm5124489ejb.216.2023.03.06.14.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142014; 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=Pmv4GODEo+jntdE66L5djocg5OACgVqeZY4tkfhD1zA=; b=Y06zmmNzwG+gr+01MxwuwalEFJYoPAG8LpJT8cXEU+ut4lDxsSXFAOWsouVcaB/JNkamMv Mhy9Qc3KrONbRd5UsZYK8lIm4Fiwn0t8lxcp+7jST0Z/RE0siFb2TvZvCwk4c57c3LW/Ua VPgJLLqWXrxhDuch/XooanWzWHP23mU= X-MC-Unique: k3eCQ0DQOMiBi5PhIuGZMg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142011; 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=Pmv4GODEo+jntdE66L5djocg5OACgVqeZY4tkfhD1zA=; b=wUDtE7M7anfSgKeVtosrFVKZSrT2B/AmH+BUwTrPyw33QPFgdjwnOcDqTB9JnYSyHm YLndn8AniF+i/uyrNEZdsvzYJYZ5MUUyrkmByqxLHsLwJZY5s0N3Gq0Tsb+9WB5hooGe 6KnnBT/w/3Yk1fkwcFu9D6ZTnvrf65W9FsUVKV+c9WwPyTr5lUcPmLMX7YEoZurKIElg DrDV8fFzS0TBhm+MVJDj0fo52Jtd+iBcLnV5QtnaBKrR8mg1hfj5CtIJ1HoJav6t5DpZ oGXy65il2QYc5VdF3dAqblaF+hy46cm9t00NXOhuRZ3Uu3EqWdL7jvyQ66L33cXCHhEU oq2w== X-Gm-Message-State: AO0yUKXdILZ/gfFc2oE887J2vXN0eJC5gqH48e6RtqNX287MrAaEluwQ ifAusjlDNk8U4Q1tAsUm8R3iIjUTy4yGc7Tp+gOxArwyCzpa9/b4yPx7t2FrO/qe9ETwNlPT/ZZ cieP/JNs6HYizHo9QU9asQy9+oV7uYldJinitWdBS8kDd81TlrzITiFoDLzVHSR+RuBCiSVN+F+ 0= X-Received: by 2002:a17:906:2b99:b0:8b1:fc59:a22b with SMTP id m25-20020a1709062b9900b008b1fc59a22bmr12399643ejg.34.1678142011619; Mon, 06 Mar 2023 14:33:31 -0800 (PST) X-Google-Smtp-Source: AK7set9O+Sd2aQ2n/uwaHBFSJPVhlzAoyF9CTajlunEWiYDWsMDBbe9c3vvxJzM3oiCmKwDcFChuUg== X-Received: by 2002:a17:906:2b99:b0:8b1:fc59:a22b with SMTP id m25-20020a1709062b9900b008b1fc59a22bmr12399621ejg.34.1678142011395; Mon, 06 Mar 2023 14:33:31 -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, richard.henderson@linaro.org Subject: [PATCH v2 7/9] physmem: add missing memory barrier Date: Mon, 6 Mar 2023 23:33:04 +0100 Message-Id: <20230306223306.84383-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142059565100003 Content-Type: text/plain; charset="utf-8" Reviewed-by: Richard Henderson Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- 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 From nobody Tue Apr 30 18:25:28 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=1678142095; cv=none; d=zohomail.com; s=zohoarc; b=oFAa0viZwR4UGl8bBKSbHrUPgfsSceDfuDhpoozm3nX6pId4DNbIX3OfEL2n8Lf3b2yFw4Y3ofaVemeydEWZo4bLIr9CjwWqTs/aRBhZjC/tsryVKR4LRxV03LfjCo9pNttcXkV95n8y8G8SaBd1bz8tQ88cU47Knn2M/YIqEOQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142095; 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=+ixVZqDajBkShvIN1j/NIMX+jAPV2NELs9/rKb8HzyQ=; b=VDp/CvcYOF1BnSZEoohJ04WSF9qw1nLMZWSR/lJtQLYwOI8v/NJXvMTwemV1yyuRWfxnWcyVdMzGkQA5XjnF72gEsnpc/7frj7UvRjPjh8yDVrfm4hOLkxomNX6ZxivJ8byDu4LT4htV+Aawcsz6kiHT/KYtnC9ZGVN4S7RPQOA= 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 16781420952794.638613456079952; Mon, 6 Mar 2023 14:34:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOn-0004Sn-Dx; Mon, 06 Mar 2023 17:33:41 -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 1pZJOl-0004Rj-7U for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:39 -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 1pZJOj-0003Z6-VC for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:39 -0500 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-135-FF27v-JTMsGeY-D7JrvL3Q-1; Mon, 06 Mar 2023 17:33:36 -0500 Received: by mail-ed1-f71.google.com with SMTP id v11-20020a056402348b00b004ce34232666so12631891edc.3 for ; Mon, 06 Mar 2023 14:33:36 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id j22-20020a1709066dd600b008e6bd130b14sm5166606ejt.64.2023.03.06.14.33.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142017; 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=+ixVZqDajBkShvIN1j/NIMX+jAPV2NELs9/rKb8HzyQ=; b=dfrn71K4GKl0AuMu/2Bm84Qn+B58y1AHhsGWd0bU66SMpcTrJZoa3nLBQtPC8ClDuFKIBI gjnXRBGqsoCXRyrYhTMJXMkCNCBmpoB8/x5MuaxFU45nQQvdlXhtEqVe7j8GqkpHeg+jYH X3EZkA1/OBKha7GJBWGvSDv1ER6uoyU= X-MC-Unique: FF27v-JTMsGeY-D7JrvL3Q-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142014; 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=+ixVZqDajBkShvIN1j/NIMX+jAPV2NELs9/rKb8HzyQ=; b=BVCHGpJNBfYcoSfK4Z/US79jgQkDfvlpwOWRkYui8dxhmKoyZa9dlD598uLH2/S3NK z4e/whX8ghQGDMHfJ236OT+MkZt3gox8SqY51UGZBIJoj4kQ5PQcKrepLg2VdgOzm09e qN3TymZh2QAmQ0+NyccPJvnPR/fx4U+ONn4dkCV3zUVlLBrW/ZrWr70OAccOg3tt0Bpd sspqXBiBJ2IkIuIazC+2A7DG7m6G/ZA9Fd7lT6sgMxJlYLH+WBtcjNG7jjlDk6TTsooz cVaa0VeramURmmApBQ3WCHuPgAk1vI2IZBoB8cfwwiHwmfDaukwfKsp5dJ/KsAKLXLHj eY4g== X-Gm-Message-State: AO0yUKV2zd++77Gn5Q5ezCmMucr844OFlem+MjpWDLr2Jdk5z8AkR2iB oDqr0ekmR3/btiRhfAKptgBsc5/yRyszjQSPHhfwR9CcKC+cDH8PDB4Bi2QFjIi7BqIQROqgFc8 UgHIH173RPn8ZperhmNArCOw4+Uc0b19XsW+iaU3tUpPgcfvnq+l1b+unWnBUM2Eyc5OaN1M559 4= X-Received: by 2002:a17:906:b0d9:b0:8af:3519:ea1 with SMTP id bk25-20020a170906b0d900b008af35190ea1mr14512046ejb.57.1678142014596; Mon, 06 Mar 2023 14:33:34 -0800 (PST) X-Google-Smtp-Source: AK7set+iAo9pdfeksIl1HCAZz621RuJP/0dsH5ASzToMoBnC6Q1/jq+e3NEDm1LuwTf+YY7YWJie/A== X-Received: by 2002:a17:906:b0d9:b0:8af:3519:ea1 with SMTP id bk25-20020a170906b0d900b008af35190ea1mr14512020ejb.57.1678142014235; Mon, 06 Mar 2023 14:33:34 -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, richard.henderson@linaro.org Subject: [PATCH v2 8/9] async: update documentation of the memory barriers Date: Mon, 6 Mar 2023 23:33:05 +0100 Message-Id: <20230306223306.84383-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142095786100001 Content-Type: text/plain; charset="utf-8" Ever since commit 8c6b0356b539 ("util/async: make bh_aio_poll() O(1)", 2020-02-22), synchronization between qemu_bh_schedule() and aio_bh_poll() is happening when the bottom half is enqueued in the bh_list; not when the flags are set. Update the documentation to match. Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi --- util/async.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/util/async.c b/util/async.c index 0657b7539777..e4b494150e7d 100644 --- a/util/async.c +++ b/util/async.c @@ -74,14 +74,21 @@ 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. - * 2. ctx is loaded before the callback has a chance to execute and bh - * could be freed. + * Synchronizes with atomic_fetch_and() in aio_bh_dequeue(), ensuring = that + * insertion starts after BH_PENDING is set. */ old_flags =3D qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags); + if (!(old_flags & BH_PENDING)) { + /* + * At this point the bottom half becomes visible to aio_bh_poll(). + * This insertion thus synchronizes with QSLIST_MOVE_ATOMIC in + * aio_bh_poll(), ensuring that: + * 1. any writes needed by the callback are visible from the callb= ack + * after aio_bh_dequeue() returns bh. + * 2. ctx is loaded before the callback has a chance to execute an= d bh + * could be freed. + */ QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next); } =20 @@ -107,11 +114,8 @@ 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 - * 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. + * Synchronizes with qatomic_fetch_or() in aio_bh_enqueue(), ensuring = that + * the removal finishes before BH_PENDING is reset. */ *flags =3D qatomic_fetch_and(&bh->flags, ~(BH_PENDING | BH_SCHEDULED | BH_IDLE)); @@ -158,6 +162,7 @@ int aio_bh_poll(AioContext *ctx) BHListSlice *s; int ret =3D 0; =20 + /* Synchronizes with QSLIST_INSERT_HEAD_ATOMIC in aio_bh_enqueue(). */ QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list); QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next); =20 @@ -448,15 +453,15 @@ LuringState *aio_get_linux_io_uring(AioContext *ctx) void aio_notify(AioContext *ctx) { /* - * Write e.g. bh->flags before writing ctx->notified. Pairs with smp_= mb in - * aio_notify_accept. + * Write e.g. ctx->bh_list before writing ctx->notified. Pairs with + * smp_mb() in aio_notify_accept(). */ smp_wmb(); qatomic_set(&ctx->notified, true); =20 /* - * Write ctx->notified before reading ctx->notify_me. Pairs - * with smp_mb in aio_ctx_prepare or aio_poll. + * Write ctx->notified (and also ctx->bh_list) before reading ctx->not= ify_me. + * Pairs with smp_mb() in aio_ctx_prepare or aio_poll. */ smp_mb(); if (qatomic_read(&ctx->notify_me)) { --=20 2.39.1 From nobody Tue Apr 30 18:25:28 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=1678142081; cv=none; d=zohomail.com; s=zohoarc; b=jF45y9+JUoYYlgQZVN+XRdM9Q+/m3LkMdUlkCaHDaay/Yjwz+ypAtEk15loZjHWbEQzjqEZTcxqPBNF1boUzOtSX560wSDA0jVDb1yRBsXaPbEpOUmOqI2n8tMMsdSy+tLNzmd8vYXJ+IXcK+ZFb9ciydAjEJa+4Q3Pp+sMwGHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678142081; 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=Y1rNPAcKSjGM/N+sMNI8BsxsPy3EoZ3hfvlt7wnsSHM=; b=lYsuRGK8g43AOG309NMDTC4HMqa3KORtVdShGpqAIxH+QkcT8rI4HIPjJ9fD4HlAKfxv2QJDeqYHS8dDI0ZlitTbUbIh68jqrUpMUIinpFTEJ9SXxi4wcRyYdgjJtNfoYDW81cdmrvYUB6rPo90i/tKMJxzRgbg6c1VqJrg7lsc= 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 1678142081701406.46922055560515; Mon, 6 Mar 2023 14:34:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZJOp-0004Tj-8e; Mon, 06 Mar 2023 17:33:43 -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 1pZJOo-0004TQ-Bf for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:42 -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 1pZJOn-0003ZO-3z for qemu-devel@nongnu.org; Mon, 06 Mar 2023 17:33:42 -0500 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-247-tjswA7ypPj2BFnwq-E78nQ-1; Mon, 06 Mar 2023 17:33:39 -0500 Received: by mail-ed1-f71.google.com with SMTP id r9-20020a05640251c900b004d4257341c2so10198259edd.19 for ; Mon, 06 Mar 2023 14:33:38 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id ay24-20020a170906d29800b0090953b9da51sm5111325ejb.194.2023.03.06.14.33.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:33:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678142020; 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=Y1rNPAcKSjGM/N+sMNI8BsxsPy3EoZ3hfvlt7wnsSHM=; b=hBE0cOC3JQbcNl2PRKEZPnOtjnX265FRPljIvFVDoVf6aOOJbYszYtXXmQy0rXqU9J4v1t XtCFJtNBAgJcJgnrlu+yB+J/B5eN1vG5z9YSS7tsaWp/Pd9D/HAytDbBW6lb9n9s5aw1B0 ZGeb/kzCzhDIeSBbNwbhsjHAzlqavLQ= X-MC-Unique: tjswA7ypPj2BFnwq-E78nQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142017; 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=Y1rNPAcKSjGM/N+sMNI8BsxsPy3EoZ3hfvlt7wnsSHM=; b=u59cPZbKvFLajvKND2orYwy+Key8dzjlN7RgNL/CA9S5xsQcR6k5Jwj7cYVSOaXJF6 52YiFNHb7SyVKMLW4vWrSg1hPpDBvoRf6rUiSvdkVl++EJVN/VJLPcAj6wIcdMu7ilEm cMTqhtrgunD2a8ekYOBz6z0nMT7fC0urYqPQbRU1GYHynSIeHe18VkW8/Nw5Ajni+kDf 3tQWfJE+YGOU+dRVa3Kd8cAFMmXAZyIaCeE05yMbyVdPsLwk/Y/cBpeN40mMcZHlzBCw UOg86JXJS769C230++10RjatfpEVwCZ+fNncG4cWomrXJ4TECypdJ1H69bs8aOJYdtpj IwmQ== X-Gm-Message-State: AO0yUKXB5cpwvj1Faq9nDo0WF7Q7SQTxKfgpyHXNVGYEmzUzrrgSRS74 9L0sUep29bPcUMYlQhvGT9GbvxQki6QDRG8mzAI0KwNUa9FoEH484vkWYpY3+W86Jq7IPDYTObj Ar/MP6TQTyZGsS6l98e3ZJTAwky0yBE0NKh8T9aCQ8QUHU15Xb/Q8fP4KHNOH6AQzRKTeWaXiKq s= X-Received: by 2002:a17:906:85c3:b0:881:f614:44ed with SMTP id i3-20020a17090685c300b00881f61444edmr12876635ejy.30.1678142017597; Mon, 06 Mar 2023 14:33:37 -0800 (PST) X-Google-Smtp-Source: AK7set/vTmi9j+MVwfqTUWwYHjVvj3Z4BdZWRStWGuwU751O2iiGBCHbVUss2vDVlBkd+sJ6TF5EIA== X-Received: by 2002:a17:906:85c3:b0:881:f614:44ed with SMTP id i3-20020a17090685c300b00881f61444edmr12876617ejy.30.1678142017330; Mon, 06 Mar 2023 14:33:37 -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, richard.henderson@linaro.org Subject: [PATCH v2 9/9] async: clarify usage of barriers in the polling case Date: Mon, 6 Mar 2023 23:33:06 +0100 Message-Id: <20230306223306.84383-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230306223306.84383-1-pbonzini@redhat.com> References: <20230306223306.84383-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: 1678142083747100003 Content-Type: text/plain; charset="utf-8" Explain that aio_context_notifier_poll() relies on aio_notify_accept() to catch all the memory writes that were done before ctx->notified was set to true. Signed-off-by: Paolo Bonzini Reviewed-by: David Hildenbrand Reviewed-by: Stefan Hajnoczi --- util/async.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/util/async.c b/util/async.c index e4b494150e7d..21016a1ac7c1 100644 --- a/util/async.c +++ b/util/async.c @@ -474,8 +474,9 @@ void aio_notify_accept(AioContext *ctx) qatomic_set(&ctx->notified, false); =20 /* - * Write ctx->notified before reading e.g. bh->flags. Pairs with smp_= wmb - * in aio_notify. + * Order reads of ctx->notified (in aio_context_notifier_poll()) and t= he + * above clearing of ctx->notified before reads of e.g. bh->flags. Pa= irs + * with smp_wmb() in aio_notify. */ smp_mb(); } @@ -498,6 +499,11 @@ static bool aio_context_notifier_poll(void *opaque) EventNotifier *e =3D opaque; AioContext *ctx =3D container_of(e, AioContext, notifier); =20 + /* + * No need for load-acquire because we just want to kick the + * event loop. aio_notify_accept() takes care of synchronizing + * the event loop with the producers. + */ return qatomic_read(&ctx->notified); } =20 --=20 2.39.1