From nobody Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189466; cv=none; d=zohomail.com; s=zohoarc; b=CC95T/DMHbdIIB7LSddHUJLFV6nUo8ZYgTsbKrwd/hpGOGt/LaVzV1ioZker5raXXybQu1jq3Qkv/U3odnm1R9CS7lNhDaL0mpGFAO+QYI+t+ReCEeakfGe8+j2SLWSLTkBlMF3bBBUGiwOorOrSXLHd7Hg9UDxoalrjvRkLdkg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189466; 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=kfZnwnhjr7AVeU/44zo/NW6NeZgUD/91FPmnLkhpLJNB3y7RK5PQub6O2+dOgQgwB8U/LhCrAGscDXw/Hafq6gvvMNLaVaOk+Adkcnjtd1sRwDhevG5R4nxNe+ZCZp+dhoMnN8X5CtHujzhtdjfueoN7z8xqDPZga0Ku1K9/dLY= 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+importer2=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 1678189466485498.5417177777683; Tue, 7 Mar 2023 03:44:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjJ-0005qj-7r; Tue, 07 Mar 2023 06:43: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 1pZVjI-0005iy-6p for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:40 -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 1pZVjG-0008Es-81 for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43: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-221-M-5nkEs1N--KCKICyRETiA-1; Tue, 07 Mar 2023 06:43:36 -0500 Received: by mail-ed1-f71.google.com with SMTP id p36-20020a056402502400b004bb926a3d54so18414726eda.2 for ; Tue, 07 Mar 2023 03:43:36 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id h24-20020a1709063b5800b008cda6560404sm5947970ejf.193.2023.03.07.03.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189417; 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=hjNlO5Pg+U4s975l/fM2+ylKrbK9UQ30PDPtDnOQZbQwVDoQFMDF0uCqy/duP6XClC3APv CUkWU1D/r/HloX6eGFzFpZPQyWH+FWDu/2xGlG4dgOdYmO+O+w7HlqUYmeEQXKruVIJFZZ cAQP5c295N/fsUtlM89+yyJgI2eaEHI= X-MC-Unique: M-5nkEs1N--KCKICyRETiA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189414; 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=71BK1OxCOraczMeSwwuiM58YmETKrQN58EwWpc52blsa2LbqXj/YMe5sVaznsUi02A ObL/ukvTAUqu9tZrzB0hPunMmqlEXCbPXSwl2EVKCSU89QVfG+8o9L3ThljYA3sirfJ1 0vn3FsxBuHVhlRQTVlDE86lP2t+JlR71hSShRGSUY5hSjybwcP6VBW5vl6DGB3BkvCpy K+Kwe/8FQorRnGcwVzvcUFFVJtabPNMPWYtN/k9lkTEVUXf/w9tarkVCR+6wJyOpy0nE oXoZt53IbwraK76xXtqdz+CzhY3Sk8SniMSpFlTKRqxObG7kOw4JBzCOD296e+oBwodv CVtg== X-Gm-Message-State: AO0yUKXEQWzYglPqv/Vf1t7ckJ9kVTLbhM76y7I0iqXSaDnZedky5MPg 7ZUal1cduELiHOG52dlv1Rr2YOoH3tN+YOqG1qk6U+wW6X5IFqTybmRSwjgbeby1e9OmVGdK6+a ZSBFBFYSjo6UXoHCO+H9nLJOIALZTosX9QS8X3jqfBhLDmE71xorw69UUi5MJvdZPq/NkixDair c= X-Received: by 2002:a17:906:58c3:b0:878:625c:c8ff with SMTP id e3-20020a17090658c300b00878625cc8ffmr17495297ejs.54.1678189414526; Tue, 07 Mar 2023 03:43:34 -0800 (PST) X-Google-Smtp-Source: AK7set+E6Nvep9kXsViinEvltot1HxgFsdNzmTqWHYp5+uraSmMtL7drDrR5R4K37+lcD83cJLtVow== X-Received: by 2002:a17:906:58c3:b0:878:625c:c8ff with SMTP id e3-20020a17090658c300b00878625cc8ffmr17495281ejs.54.1678189414220; Tue, 07 Mar 2023 03:43:34 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 1/9] qatomic: add smp_mb__before/after_rmw() Date: Tue, 7 Mar 2023 12:43:21 +0100 Message-Id: <20230307114329.158094-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189468479100003 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189470; cv=none; d=zohomail.com; s=zohoarc; b=iyUvj5pPKlMlJCKD/p8C1gNcZrpV4saX0p1l4ZuCAEBIWGRZmbXnVNNW855mPoB/jgFShYFR4e5lRai4in9IeEcru5GjM1JLSuvcgTHB/ER2UqXaFxd1ZLSvTdTGjM8LLY9fikZolticU5jOC6d6LMuJ1nfVQRkMtXnQKWSU9aQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189470; 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=xQiSJuptmSTzUnUzXG3PeEvpkUGVO3Aw//Tl2NkBFUU=; b=JkANodF+D5Tq2Ls4Q5A4BzjMgCufm9WKXCz6jGt+k6rjF9rFn9R7u5GDpQCJuEmub65saPV6g4Gh7YTGKPUb8ttmjh67LKvSRKYMLOkhWK/C3CNgM0tnTXnK+BBHJhB6Jfv73MQi+HM/nG6KUwACLpkme5BE9CdDj3vNTE8eKYs= 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+importer2=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 1678189470356630.6109643101383; Tue, 7 Mar 2023 03:44:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjL-0006GT-Bc; Tue, 07 Mar 2023 06:43: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 1pZVjK-00064s-Af for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:42 -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 1pZVjI-0008F6-LG for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:42 -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-144-__mClokgO1-ng7p7OakO1Q-1; Tue, 07 Mar 2023 06:43:38 -0500 Received: by mail-ed1-f70.google.com with SMTP id r9-20020a05640251c900b004d4257341c2so12829451edd.19 for ; Tue, 07 Mar 2023 03:43:38 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id s15-20020a170906454f00b008d8f1b238fdsm6065433ejq.149.2023.03.07.03.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189419; 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=xQiSJuptmSTzUnUzXG3PeEvpkUGVO3Aw//Tl2NkBFUU=; b=fGUYqyeLHkKZPO+6oC0fhX/waoCq1UmXUldI851eSWeivtmBV8K4r2X7sWH4YnXEWbnP/F /SW4E63W6/nZV70KnCilGXrJ/KRCdEGo46rCemp+Zv9e1nXM2QTUEm+VEHkED23Ya8dLBD xdEMI0lq9P0njj7wEze/IJ1pQ1KLArM= X-MC-Unique: __mClokgO1-ng7p7OakO1Q-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189417; 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=xQiSJuptmSTzUnUzXG3PeEvpkUGVO3Aw//Tl2NkBFUU=; b=wHFEV2LdhgQoQJL3/8z9OEnLAAXGPyhbDJsBq4iYFicRqFrbnP2cqBiplC6CV6E6pq aOZmEJumtFUQGxRIgecyUHrPlvxGhN78NwFBK8f7YFZFFh6c6UTVB31aQ+iJBsdIuW3q uw3D+MpmIeUbhN52AvpcuOxuBEisk0B1nHjDoAUwZgJN72qtxjQiq/WmBrhzWsWgtIuA OQ8NhajWfxKX+EUFbKG43pTkNzfRNqnmd3GKuhR9IjPlx1zxZ/x+jbn9VHl9FtQwuQl4 fFPjvZfpGpx7gw2YkOL5z0pb4bWOdgWrgVFnzzXh7XhaxjqY6HJAGziQiBkruAzfeDBe kFIA== X-Gm-Message-State: AO0yUKWYDlLc8XpsZxbBZFxHB3IHk/eAZbR26U/1RFKeWZw5HrjwwpHg 3l7TbN31VIl8sPxLrEAMtkz2yL5gNQQiRDbJ2rzvUQ4zxxmi/R/zBtCK97/JUXbXqR1phyIYX9h eV+5drDUZouzHzqH3jaqkQd3feOB2zSbAY34MqqNJUjG3UkV/E71TC9U4rQZyv+ilRrM40fenZp Y= X-Received: by 2002:a17:907:dab:b0:8ae:11ca:81de with SMTP id go43-20020a1709070dab00b008ae11ca81demr17428750ejc.34.1678189417236; Tue, 07 Mar 2023 03:43:37 -0800 (PST) X-Google-Smtp-Source: AK7set8gvttryamZCxhsmrPrHfNGQe63XL/I6fg56HOUH7mC/l7HaIs1jrcZ2PQ/yPP4evumfo2Jzw== X-Received: by 2002:a17:907:dab:b0:8ae:11ca:81de with SMTP id go43-20020a1709070dab00b008ae11ca81demr17428733ejc.34.1678189416911; Tue, 07 Mar 2023 03:43:36 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 2/9] qemu-thread-posix: cleanup, fix, document QemuEvent Date: Tue, 7 Mar 2023 12:43:22 +0100 Message-Id: <20230307114329.158094-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189472425100002 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..b2e26e21205b 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189497; cv=none; d=zohomail.com; s=zohoarc; b=mFhKF+JvIA/uDwUyuYXXgkFT1pFZ0kg+Sjs7RxjyrloW8Tkq5BQooYyk7dI+8arPTWm4iUUicwXmfe7zsD16yr8ld2gjihycQ9G4L7sLfXyHTfggYti4zIeEi3TQArcnKfCaMW2KmrHma+UYUTeFMEHmmxeSYSUw5nTD9OXii1w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189497; 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=hSkvATht7CbRsghwuS5jLyOCAgTCld6ffCjxjQQFVDc=; b=IrmUCC7iUh7o+KBc4hRiCSj1cfr/ooe1KBPao3QtPvY8qGIauW5cCWdwDoukPq5ZHtTLBe3NOXay7VWvMKJ9j/ZRU37SP7cR8vMIHdmo4rwdX+3QHJY/UToULhcr0rj9lldCtwJCG68fgOd7DmugqRmwic4KO2DVbZHNe8ib2H0= 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+importer2=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 1678189497751600.7996399493708; Tue, 7 Mar 2023 03:44:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjO-0006g0-9H; Tue, 07 Mar 2023 06:43:46 -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 1pZVjM-0006Us-QK for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:44 -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 1pZVjL-0008FO-2m for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:44 -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-444-dL3Jj-zlNVKaJ3DKxTAlJw-1; Tue, 07 Mar 2023 06:43:41 -0500 Received: by mail-ed1-f69.google.com with SMTP id h11-20020a0564020e8b00b004e59d4722a3so8379315eda.6 for ; Tue, 07 Mar 2023 03:43:41 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id j19-20020a508a93000000b004c3e3a6136dsm6559443edj.21.2023.03.07.03.43.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189422; 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=hSkvATht7CbRsghwuS5jLyOCAgTCld6ffCjxjQQFVDc=; b=HtpI4KxoTdfzL+fwSi4md0D+X2VdP+5FYzmDsAJfE/VLdYGWrdIhwRJz708Cgrgw2IEtZb SfDItDLZeMt33qmLEiQvD8rQ6cHuaHUQS1/lcoNd79bSMD3fF36sIkXQUDVH6Ck8z0U/ua UDa42SPVrOD89HzGQ1RPKWS5gCcbrn0= X-MC-Unique: dL3Jj-zlNVKaJ3DKxTAlJw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189419; 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=hSkvATht7CbRsghwuS5jLyOCAgTCld6ffCjxjQQFVDc=; b=MJ9ZiPZHcat1Cz8NnEFl8dtlCGROdhPYvbdORrfDslxxMYcv5l+1+KpdjmSJWdBn3H 7nMbjwO7V7vo4PjugCkzgerGi/HIVSIt+Zvh6M9djkLXQtvDQHszwniAvvrMzkIVgIDf Tg+YAs6abBFW74yJv7EL0VbiE/pqb2Cr7LVBlfW9HAcZoVw4MTzG+JZiBdRX9dR30R8y RjmGafpakAMSsVSH7Y0WCGZ2UpACxf5fR7u6U+kMkf7XbfREYUZr50h3UZhNL+KsN4RD 8gCF4uk9H0ZCF+d9UJhkk1ma0EsDv9YxsGnKvAbBU9WNjh0mRnVeLLU1Eq/i3pAJfOx/ yd4w== X-Gm-Message-State: AO0yUKXt5+9a5V5+EB+rLp7/AywlGf+0APghaoLeHj+rXRnY7CMxQpLW 7S1J+vNK7bUW4tWt+b2ldKSFZPoMzioxKBpn+z/XiQSOtFQAF5u8vdEzbX0scpSvEwtD+S7zVea TV8K+ypscY2gKfAx1i4K2MEufuxJ8TxYbbMwaPdi3vaLSIkwqwhDrHIlxOXTZBWVD5QOVWyfYGf E= X-Received: by 2002:aa7:c44e:0:b0:4b8:9a32:ec07 with SMTP id n14-20020aa7c44e000000b004b89a32ec07mr11946140edr.39.1678189419719; Tue, 07 Mar 2023 03:43:39 -0800 (PST) X-Google-Smtp-Source: AK7set917WSe64k1idcaRQhtSc+TWxrQYZ1U4yRiQtC/AUtTMaexrio7sDlwR9QSxl1Dz3XrpIN17w== X-Received: by 2002:aa7:c44e:0:b0:4b8:9a32:ec07 with SMTP id n14-20020aa7c44e000000b004b89a32ec07mr11946129edr.39.1678189419420; Tue, 07 Mar 2023 03:43:39 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 3/9] qemu-thread-win32: cleanup, fix, document QemuEvent Date: Tue, 7 Mar 2023 12:43:23 +0100 Message-Id: <20230307114329.158094-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189498798100010 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..a7fe3cc345f0 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189486; cv=none; d=zohomail.com; s=zohoarc; b=bxUwqsWiTiiSk2xRdJGu/F3PGao4bJoDPn19vD8a0Chl8MtRwZgzrDzXXSytxk7DlErBhFgm8HwxWO4C0pYb6ROpYBn1mXIsdu7CXw/wJgagyadDUVdu5AzTujDuAYd2h2phdiP7p/OI9eotuXUtrwaMZvNwkWrqhpb4Qo6SrgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189486; 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=hRQVif+EqVA9g1wkZdXn7uj/me43gIwYNGMtyH2j828CqasV18a4XGy+nXm8sAwxvvBeUSJzB4q8v7U0cZt4WKnuF5onXRSeC44QOMNVMuH6IDa+SibOVRCCOUeqg5+l2I6cRQc0Wn64snUgOTZHPSwkiUoe8HK5Q1vkHOmQDj8= 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+importer2=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 1678189486865831.2028025281662; Tue, 7 Mar 2023 03:44:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjQ-0006lt-7p; Tue, 07 Mar 2023 06:43:48 -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 1pZVjO-0006gR-Ih for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:46 -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 1pZVjN-0008Fi-5v for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:46 -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-662-e_MIgSpmNrO040dEgrUrDA-1; Tue, 07 Mar 2023 06:43:43 -0500 Received: by mail-ed1-f72.google.com with SMTP id ee15-20020a056402290f00b004f059728d91so1524891edb.23 for ; Tue, 07 Mar 2023 03:43:43 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id v5-20020a1709063bc500b008c327bef167sm5952368ejf.7.2023.03.07.03.43.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189424; 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=W/B5bY3HFLBxlj3arHKXH2T3WJyOTfQ6HTpLpLVvyCbemIuAHEmv3a5+6kLxY2yVKZ1xPs b7+6SD6DvjxmXOfPDcMoSkRt0D/geIcKQ9pbBDzL9ugwe4F7ORFSsHEAPtGUSkJjJNg5RX mtJ+6WfnNCVcu0Pji8cC02Z5+UewIHU= X-MC-Unique: e_MIgSpmNrO040dEgrUrDA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189422; 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=3umRdg923yut41ZUNBMGgqbuT8DxtQELxnJ2ZHU85+a4mu6DRxQck7Kn9l6V4O+ok/ vTaVTAJs+t/yxtsGur743BfnPL6g8zgfyBBtmzAYsyXR14sQJLi3UZ6T7tBlpfpafZRh 6rMcQF0E75jS7NrYyh0IeFGxN4hoQSxKXd3ZgMA8u4eMoOIpxlYEVlEn6XHfqmBQqCap BXKmfuYSbj4S2siM/whcDKmuYg67f+RG98vXrtEWLZQh3QfdSH/mRtWgJCsmYlZfNxig tvzVZyUv6RX5y/OdcJy+F/Kk7qijxbqvbX+zyN5h1SQZEc0CmfEw8MZ076sNTsfbPT35 u8LA== X-Gm-Message-State: AO0yUKUOsYoDOXkDfhd7q0f0X7HhAeQu/ttdWmRFix7QjoNJM6bisHN8 5ZjVVWP9Se1AfRhAK81SjbvTWBC9XHKDLSx0Y3jLCqgYJZPEL0sn0PvhbEcOWQHXFIfJqWvIrZT I4fQkFao4qLvuR5aiaWwR9b6dE6Dzk0UGylSZEHPrqC18kutU02TVLAJ7Qsc31Sb4Bj48Uf/4Xe A= X-Received: by 2002:a17:906:154:b0:881:d1ad:1640 with SMTP id 20-20020a170906015400b00881d1ad1640mr15063808ejh.57.1678189422317; Tue, 07 Mar 2023 03:43:42 -0800 (PST) X-Google-Smtp-Source: AK7set9RWCz9bME0ZFYKaCi3QxuWPXFtMNHlttc41b7jr75c9rfvtuwg5jdKV8V4ff0vmT/b19Cvkg== X-Received: by 2002:a17:906:154:b0:881:d1ad:1640 with SMTP id 20-20020a170906015400b00881d1ad1640mr15063787ejh.57.1678189422014; Tue, 07 Mar 2023 03:43:42 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 4/9] edu: add smp_mb__after_rmw() Date: Tue, 7 Mar 2023 12:43:24 +0100 Message-Id: <20230307114329.158094-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189488484100003 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189448; cv=none; d=zohomail.com; s=zohoarc; b=RScYfpry36448SdTo1tds5I2o58s290KE+igFRsp+ozsu5/GVNl2w0xZbm0EMNshwxBaCyHXdYnpE/Te3GVuHyAsivDTtR+IJYuHiSdNVIKCaG5lICMGo67hJg7Mh3RCTiH6YN3aJvD+fV3jOZXqzuD32YdT5cH+VinPpmIzqGk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189448; 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=3U+bn6i48RYLMFGa4DzfAVaTjQOjbQ9iFa0f/zDX6po=; b=UQMQd/dQPO477QoQq2mFt2swVPEB8Pmpirq8KUVw495CkEr+q/KLjn2u9Zcy3JjnoUlh6JN9h/Am10Huo28308SU725xjO/x5rGdCbkibLYtOMkksKM5UkNNiShEUxr5xNRPvky1CiOPJ9WhDTME+1JXoaZRjt7Ki2hgENIgjLQ= 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+importer2=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 1678189448386552.0376891496899; Tue, 7 Mar 2023 03:44:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjT-0006qw-2l; Tue, 07 Mar 2023 06:43:51 -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 1pZVjR-0006nM-7b for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:49 -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 1pZVjP-0008G9-S1 for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:48 -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-PNNK5xLsO1eX9qPrMuqfcQ-1; Tue, 07 Mar 2023 06:43:46 -0500 Received: by mail-ed1-f71.google.com with SMTP id h11-20020a0564020e8b00b004e59d4722a3so8379567eda.6 for ; Tue, 07 Mar 2023 03:43:46 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id x8-20020a170906440800b008e8e975e185sm5954266ejo.32.2023.03.07.03.43.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189427; 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=3U+bn6i48RYLMFGa4DzfAVaTjQOjbQ9iFa0f/zDX6po=; b=MRRjLLWT/x1P0Tdk0kpSoGk/e6sLh0dlmJRh4UnE7Z03fX6Ag8KnLjEKiwLX4OiDklOux6 Epk+2sVzJXjWQnstcRX2glnbXzjcPf8EJfsagfWND6f4RNLJ78vNjjo02xw7asscmL1MDf COVfcbs4H9dNiHIQ8QtIpSDzqSDw/UI= X-MC-Unique: PNNK5xLsO1eX9qPrMuqfcQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189425; 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=3U+bn6i48RYLMFGa4DzfAVaTjQOjbQ9iFa0f/zDX6po=; b=V9A4M6NoWZhV/RBLSSNEh+6VCckkTmxN4lYZtgFyJT8wJndD9DDre47qUiwuS0Qkfy WDEPW/gQmwkHJ9WiYKWAbL4xX+XUHr+F5CJcgXOZbPooWVnORmHj/qCXVC7uYQitgmS+ QzpH7JQUhUHUjwZXBRWxpgYoGH2gHLjpB6C0j/mZS6CDeZ66ThNx4xuIa+HLL9n5fjMk J8CFJrkWtJ9Z9Xqhf3ecxr10kd1VNj2ZT3rtWKV+pcFQ1N6CiA/0l2SYB760Htd8qxq9 q74iEP7JfoA+S/IGgBKtVXKMutcOL0tdb4NrTAp+AB/R4DdwMsqZGzI96vd4ZmbiRYoN xgKA== X-Gm-Message-State: AO0yUKUjn8jq4uOspAbPr12aBd/Z57e8zHCL9nktGKwg2EytGhf8BQrk Q438bEZ1SRtg2P4biPg6exHAUb5YTi76cijdt6e1dl47ZkL5GL0Qiku22qlwp1SSXvOfpIYJMuj AfmQDVCY69uT59hsPwG05973/xAaKMgNmU933raZE5+C1nk/mXARDyhKoJwU+pRSPpeXiPlvxgL s= X-Received: by 2002:a17:906:b097:b0:8af:1a8c:f13f with SMTP id x23-20020a170906b09700b008af1a8cf13fmr13962808ejy.71.1678189424909; Tue, 07 Mar 2023 03:43:44 -0800 (PST) X-Google-Smtp-Source: AK7set/og3oguOk1yPpqFVdx4B+tqkSgWaQU0CQ0LH2AbUiSh/Z3KFBl17p64foJXbO/YIVwt5nbBw== X-Received: by 2002:a17:906:b097:b0:8af:1a8c:f13f with SMTP id x23-20020a170906b09700b008af1a8cf13fmr13962797ejy.71.1678189424723; Tue, 07 Mar 2023 03:43:44 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 5/9] aio-wait: switch to smp_mb__after_rmw() Date: Tue, 7 Mar 2023 12:43:25 +0100 Message-Id: <20230307114329.158094-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189450171100009 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 Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini --- 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189499; cv=none; d=zohomail.com; s=zohoarc; b=P/u8tjg6WqKrAaJ/Bmbch3ppZ7pX/vIs5SNImS6wVxcsPgc+REiURKV08bQ47MfuA/NpmRhSTZCW1vQZRHhWCP9QPwuLOcp8rb7WlKt1Ej5UI0jrl+1xS8h2cTvY5YqCU/s78fjwABK/Eev+5GzdGPsx1z3pz0ACFyV/JiLjrfU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189499; 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=nto5XeqqBEhpWa6DIGE5n4oCsQIFGqTQ2+pUqfI98EfFgiz3NHNF0pFamDogqB6o6ue2h7AOZQxNaZkV7LvrfsSpZrAhShewBCDU4UGadAlfvB0Ed81PWl+nu+wKxMDv1ZB1/Kw3Tj5L9i1VjKPhedwUfQEQ/pYq2KtJAnTRA90= 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+importer2=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 1678189499819449.39353763732436; Tue, 7 Mar 2023 03:44:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjj-0007LL-Ox; Tue, 07 Mar 2023 06:44: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 1pZVjU-0006yd-F6 for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:55 -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 1pZVjS-0008GW-PF for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:51 -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-557-bHbYQNCFNR2YqqfUYmBHyg-1; Tue, 07 Mar 2023 06:43:49 -0500 Received: by mail-ed1-f70.google.com with SMTP id da22-20020a056402177600b004c60694083eso18377645edb.5 for ; Tue, 07 Mar 2023 03:43:49 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id q27-20020a17090622db00b008b1787ce722sm5948250eja.152.2023.03.07.03.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189430; 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=U5EIGzrtlBL59jl9SK45aorfOWeJgp9MEWv+7KNMH4ZMXEWwHdeB3vZgKqu+TrXXNgf3JG FVzeFS0MO7zWYMZWc1Lc+ZE4O23cJJ5zzDp186HFr0emauH84g3L25uukuxLwitWhbykUy XJ6YCG1IpFdKTKw2wJ60+V4F8qgWbzM= X-MC-Unique: bHbYQNCFNR2YqqfUYmBHyg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189427; 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=Z+iU387kvfC3MMJlK1aBjkUfUDN3Gw4VHLd85+KDjcNadNjhSWA28EySrn6spAiaw2 CUcF1q9ki9ZRHIegffegEi2IX/XSqMZPVLTIlUi/1mYFx9Cp+tDClUEj1GuEiZUtXBw/ ehnWrbjeDXULnn0Xbx6+oBXeZx8zFNk2eCyJKufyynYRT/PXRyldGDMmXXCDbN08Sdwa FcYWtojz841uL84DI8nEed3sO0NrlIhFXVGo6LF8aT1JK66wjO7OkPI128nq3neAJ+EK 4m1jB+c9xqlq7ndB9i4K9PnIq1Grztelets4UW7D6TeP96gs/wnqOk0TigjZnFbtVAGy 9V4g== X-Gm-Message-State: AO0yUKX7C4H+jyKzraIZhVAF1tPTPLQWdHrwhmWbzkRn07O5Iq7jr9Gm s5wFGSR0wfs7GE2Gkg1n7uu8NSNtYLlOBlhrzrrK+2oR8hI8X9dmG6/dktnjOg4G+onhlsW5F4i PGA1ETaWE7KvfTSd8cnp7mGZ0bx++X7+p6EU2Ja0/xo/cd3DZ83yEc3ZUNWBQJ7WZ9NXGXLbVtk U= X-Received: by 2002:a05:6402:1512:b0:4c2:6b68:c126 with SMTP id f18-20020a056402151200b004c26b68c126mr2552735edw.8.1678189427739; Tue, 07 Mar 2023 03:43:47 -0800 (PST) X-Google-Smtp-Source: AK7set8mcojLQGQaSxAYVd0sM+ib1urtx2YnLxAxjAKG02qGISXdOG9CBlYRqdqQ/rHH2BZwv0LpTg== X-Received: by 2002:a05:6402:1512:b0:4c2:6b68:c126 with SMTP id f18-20020a056402151200b004c26b68c126mr2552718edw.8.1678189427387; Tue, 07 Mar 2023 03:43:47 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 6/9] qemu-coroutine-lock: add smp_mb__after_rmw() Date: Tue, 7 Mar 2023 12:43:26 +0100 Message-Id: <20230307114329.158094-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189500715100015 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189495; cv=none; d=zohomail.com; s=zohoarc; b=ONyqcmqHxH6zJLu8LVDJ614wVY7qGpXiV7wGkBBbXk9Kbk6XYhem7MCW1du70ZFWgbT1Cgt62DPE4vgHj5rCrdhDvxCOuQvjXSuGeU1zx3I6UKUbrSno5apsNkspXnHxVJJ6kzzNIbxLxi5wIczM8ttGdZTBkKhzapFjJPlqAms= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189495; 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=a0haW5Kw6ugy9rdc0pg/LwPoPorMgrJERenwuOgWCv+qQdKNiHR7GWw3EPTB6HEf8dCUDf7h4XxXEScanGX/C7psTOEx+y4LkiF4IFzBfCoalgxfh2/uqlOHwH3+MmD4vFevPLGc5UHJ5Co3qkT8J9g3I5rZ6ONXlKvfjlkL170= 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+importer2=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 1678189495806125.71830751370817; Tue, 7 Mar 2023 03:44:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjj-0007O3-QY; Tue, 07 Mar 2023 06:44: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 1pZVja-00073u-2D for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:44:02 -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 1pZVjX-0008H2-60 for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:56 -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-460-IFNesTBmN6-ufbNQHsgPjA-1; Tue, 07 Mar 2023 06:43:52 -0500 Received: by mail-ed1-f69.google.com with SMTP id p36-20020a056402502400b004bb926a3d54so18415609eda.2 for ; Tue, 07 Mar 2023 03:43:52 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id gj14-20020a170906e10e00b008df7d2e122dsm5925722ejb.45.2023.03.07.03.43.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189433; 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=PMiMP8PY8ysHbmCKZyhCX2tt6pTB4k4kGzA4J7D+Sl2I0pvcp/uMCe8PsqtJbzK6iBjsru dXfxyXw89swImGjucmstIuV07FG+d4+KevGjxGDuslcw/HsaA2xGPJ6vDB8bGpmrnF99iV H/Dijd+SPK3y6IkGf9a9/RNjtoE5XaQ= X-MC-Unique: IFNesTBmN6-ufbNQHsgPjA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189431; 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=rgSTG8dazCBX/lO7saEmorQjqf6zADMA8rJWrIdiWES1/twaVAX2GkNK2Lbr+pNEF+ q206rFIWLFkbgtT80QGGFaxYOwBGdcCXOoWt3u2dDlaGmUwugwcJdHjhg5GjWgcKnPkG yL6YARErrcZp4JwWupmMTDHSPko7sYWMo86hT/YBwbVVCG139RBXaAy+yGXLdpQzgQSt JhysqHqjYUqVrPZqmxw/wVnWtbyuYZlPRkJUwx+V8opuZCK3mcuDuj0Un6hsiBJsCOV7 atr/Gr6HH3cwD4Nlg3u14c+04fOThvCY9TjkOCX9KgpO1oGeh77ngO+pE9Ie4s8/qtXJ ayYA== X-Gm-Message-State: AO0yUKVP6Q7enWQI6QFWA8ri8QXtrAkvV1ECB+NU5c1Yplx0DT2Jiz3K 5JvRtEaSzok6TaLblUk2WKtLTe5XWe1KMd0JKGmu/dlzQeGk+V9/bNzgg3FDqfOs4OUAlEzqT6w 69saMj59QfzAZ6kOZUGqXCy07HwDg5YgacMo/aY5WfOL/f7jRUW3CRV2N3Dk3C3LQQ3aFO/pxhJ g= X-Received: by 2002:a17:907:8e9a:b0:8f1:da18:c6ca with SMTP id tx26-20020a1709078e9a00b008f1da18c6camr16932941ejc.3.1678189429980; Tue, 07 Mar 2023 03:43:49 -0800 (PST) X-Google-Smtp-Source: AK7set8qWi2Xo795TngyvcTQt/8BsrfCLQZ8ehs072IX9XXWKxBBT3XDOdetj9ou8CfeAX4TAP3oYw== X-Received: by 2002:a17:907:8e9a:b0:8f1:da18:c6ca with SMTP id tx26-20020a1709078e9a00b008f1da18c6camr16932924ejc.3.1678189429738; Tue, 07 Mar 2023 03:43:49 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , David Hildenbrand Subject: [PULL 7/9] physmem: add missing memory barrier Date: Tue, 7 Mar 2023 12:43:27 +0100 Message-Id: <20230307114329.158094-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189496809100003 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189471; cv=none; d=zohomail.com; s=zohoarc; b=PCMa8BppqPwZ7E/XElFLVtPZvQvYKO1Oh8O1UyINZSz++vA8b00OKeDY6A80aUGNNfC+W2MflqnJrId9fdfjeFQErJlVMXB5WTtcLSscio+ereFpaZSEeN+J6ANM5r9w+gk5CkErmMWjZayjGQpjBM2OHRSO5XrCVpWp3XfYqqE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189471; 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=39bWtFgENIeu3JJ6L9W2wxYs6vYdhpkQcON8b38w6ew=; b=Cq0VsBb3Nqg3IDJhMnwfUSHd3chr2G4C8smc8QrmTSU5vdLOkpfd1nEWl2POkGQyzwJY5yyYO/lMXViaXGFn/GUaFULwo5UoIad2b2gfC8UlQSJyLZ/MIWCYoNTG9YLS3FN/zfR4FJRC3TnWnSCZYaxxbSKaVZ8s0Q+T4aq8NsI= 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+importer2=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 1678189471915930.2016712133864; Tue, 7 Mar 2023 03:44:31 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjl-0007gq-9y; Tue, 07 Mar 2023 06:44:09 -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 1pZVja-00073v-2S for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:44:02 -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 1pZVjY-0008HG-7P for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:43:57 -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-308-kA8BXibsOduf28o8s07nKA-1; Tue, 07 Mar 2023 06:43:54 -0500 Received: by mail-ed1-f69.google.com with SMTP id v11-20020a056402348b00b004ce34232666so15226805edc.3 for ; Tue, 07 Mar 2023 03:43:54 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id n24-20020a1709062bd800b008b17aa6afc8sm5970228ejg.30.2023.03.07.03.43.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189435; 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=39bWtFgENIeu3JJ6L9W2wxYs6vYdhpkQcON8b38w6ew=; b=Xuy2Yg3we7EJ6C5JRwVumSVnrT2m1WvNFJqcWX97R/n0bFzWzxQKFYTJXZwqyuLE28KOLt Jw9gNYQ+vLECxachWtgAHAMa8MxOo2sqyKownOwesoqSwX28zmpUIhzZ5tzc6C7IJqSuWH yZWlWoOi3Qwe7rfLYtECh+nzkLfzIZk= X-MC-Unique: kA8BXibsOduf28o8s07nKA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189433; 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=39bWtFgENIeu3JJ6L9W2wxYs6vYdhpkQcON8b38w6ew=; b=uPxyAD11p+TOVdLBHB2eptpm1yQUfPyOARkS2OcpB8OQrEbvj+FCfjmvlR5PqZrN31 60aTrHuqe53mInK6EuVWIKvY4eIpaSG5qUtlQe6Aap24vW/RBFOuULROoSawqkmRYEFU fGQ3RCFOZByXhvy3ZsGXjiSRMaOtj9q+YntOttLDpOPI4DWEAA8uhXhrgNIG18dmBmcs vX45pD2Q8PZMpJvcs9v0WQOW5c6+etggYD8FlJqkRnIthlDqG2wguUpRZbu9Wx5pI8Pb 8AnJAPbTdrk6i4/I3TwZJorVzsn6zJdRoqGoPjL1bn9Ds4n9xSFhj9Fp0rXiRV4F3fQC FHrA== X-Gm-Message-State: AO0yUKWNim/30PB+dBIaGf2ehlA9YJLsGylnga3ZBxLo4e+iJtSDU8tn 1Y8/0KY69vU/cFInAfj630riAH9LcCna7GQqdZAmxR2Gwv/JcU3S+1oKthguyKj6g8d0WpmG+0R UBWrf5eDfCLy73J4hAW8z5UMeQ/zmMluzb93VWC3FRzskG/QnRDXt5K8QOo22/VqLxyqedKhJvq 8= X-Received: by 2002:a17:906:68c5:b0:8b1:353a:2636 with SMTP id y5-20020a17090668c500b008b1353a2636mr12288240ejr.14.1678189432875; Tue, 07 Mar 2023 03:43:52 -0800 (PST) X-Google-Smtp-Source: AK7set9G/oLSjFAGRFBOCRHc062OoLAnxAwZOgh4mCXP6Z5q0M75x2ASShxENwx/iU0BX8dlO547Yg== X-Received: by 2002:a17:906:68c5:b0:8b1:353a:2636 with SMTP id y5-20020a17090668c500b008b1353a2636mr12288222ejr.14.1678189432519; Tue, 07 Mar 2023 03:43:52 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi Subject: [PULL 8/9] async: update documentation of the memory barriers Date: Tue, 7 Mar 2023 12:43:28 +0100 Message-Id: <20230307114329.158094-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189472443100003 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. Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini --- 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 Sun May 5 08:55:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678189496; cv=none; d=zohomail.com; s=zohoarc; b=SBVxr9GTAPGoJ2YfzZWYmhZ3L2TfzGxi2T83lKba2sRBaA61ayddQ3S3TiktltCTCasQdzPa3aj4kTPo8d3KCIU54acFwIrtBzqR9bh09FmAGbfQag2en3JreDofFeo1mdECJjgyZThQo163AG+zDzLTeNu6zqSXdQy/gab0KhM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678189496; 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=J6XR/lXlciUn+TXMfpyWYEJEroqom4bxx+aRXcNtP9o=; b=ju7bL5OezMpfUQaIl8nrcM4ImU3VmoHyIv2PQzoLJwNJTrJ7mJdSegXSZeeIhjs8+gyWnE2o7JHDmVx2dIDInpkdv6ymWfHQK3eJNUHDUWU7Se8N8n9a2qKFKXGLoJnU1FZyKpjF6nfmU07xckUYQMJ/0qSK4lx1gmvbzb9diw0= 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+importer2=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 1678189496927347.11547653271543; Tue, 7 Mar 2023 03:44:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZVjl-0007jG-Dj; Tue, 07 Mar 2023 06:44:09 -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 1pZVjc-00074A-BH for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:44: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 1pZVja-0008Hd-MT for qemu-devel@nongnu.org; Tue, 07 Mar 2023 06:44:00 -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-392-UepdRbDRPvG89-eXWrroPA-1; Tue, 07 Mar 2023 06:43:57 -0500 Received: by mail-ed1-f70.google.com with SMTP id h15-20020a056402280f00b004bf9e193c23so18734024ede.11 for ; Tue, 07 Mar 2023 03:43:56 -0800 (PST) Received: from avogadro.local ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id o21-20020a170906289500b008ef42f9cf48sm5936743ejd.82.2023.03.07.03.43.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 03:43:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678189438; 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=J6XR/lXlciUn+TXMfpyWYEJEroqom4bxx+aRXcNtP9o=; b=f9MLkKV8vUQaRqcmXfHfLscpeudUsp5nLf3S5pFYq2JZ2U9wDEOOr0TTL61kdkBtt/7qJG C9oXTUzky+sq43Z49V3RMkWZo+s1YlZYzZRmQmi/vb4Nesn5KauoN95Jv71QbrdtY8bOZJ cfe/rT9BcFAiaGvR7ozHebF6oGn7HzE= X-MC-Unique: UepdRbDRPvG89-eXWrroPA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678189435; 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=J6XR/lXlciUn+TXMfpyWYEJEroqom4bxx+aRXcNtP9o=; b=2fPikxG21AO90WyOPIB+ixFEgJCtmZk7vdvC1u4OExB4fhkDjhxPmKpXsUNg4LY3ZT aPBHwh0v5YI+JJu2hehKtn6lnzTFgts5Q2ArPKCEgEb50qacSNIQWDN37X7gQ7CXVR3P 7Jrk2W9lcKdBFuufpm6E69O2/8nrfZkBTaWLPN+gb6zIH2gX+N2E58qPWXBojhFL1IBz qIoGiFAspneUWdA6vxcJ9FQQJHX3egH6LJTQ6ga78UJ6z2DWN0M3hLu896G+g9UoqVuJ CV8jyqNm88REsx/I50+U83y/oqj0xsPhrn0zPD31+sEWHV8qW4H5Y9nVbX8sizPNap4R AK5A== X-Gm-Message-State: AO0yUKUmHOK61WXNMYwnEnCg1jnsGZ4J95ZtCv6omuq6p6qn+GDQHeGG 7wPV8dXSBtnwW0XT6R77mQvrzpOGIWx7VUYVHtp3K6U+dJxqnTQuE3Pki2/wLVaeft467fca3jS T3HTK0xM8cikpMQqLNSHRQpqybqbjeHoabCeJZn5mfVksZcR9WzX9kR4Ny7kSrIba/xqdjb7cxV 8= X-Received: by 2002:a17:906:6c84:b0:8b1:32b0:2a24 with SMTP id s4-20020a1709066c8400b008b132b02a24mr14549847ejr.47.1678189435500; Tue, 07 Mar 2023 03:43:55 -0800 (PST) X-Google-Smtp-Source: AK7set9yjiFPOIggOZx9HcDXc8Uc/j7pzIRegSuIOvH9R7CIgR8fXXs/zNAZwUbswLdq7xrGciGgog== X-Received: by 2002:a17:906:6c84:b0:8b1:32b0:2a24 with SMTP id s4-20020a1709066c8400b008b132b02a24mr14549831ejr.47.1678189435210; Tue, 07 Mar 2023 03:43:55 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Richard Henderson , Stefan Hajnoczi Subject: [PULL 9/9] async: clarify usage of barriers in the polling case Date: Tue, 7 Mar 2023 12:43:29 +0100 Message-Id: <20230307114329.158094-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230307114329.158094-1-pbonzini@redhat.com> References: <20230307114329.158094-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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678189498787100009 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. Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi Signed-off-by: Paolo Bonzini --- 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