From nobody Sat Oct 25 09:05:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520602417052182.06901271755555; Fri, 9 Mar 2018 05:33:37 -0800 (PST) Received: from localhost ([::1]:45313 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI9K-0002S7-TE for importer@patchew.org; Fri, 09 Mar 2018 08:33:30 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47260) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI5Y-0008E6-Ud for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euI5V-0003oa-OX for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:36 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:38080 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euI5V-0003nz-K3 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:33 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B7F29419DFA2; Fri, 9 Mar 2018 13:29:28 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-35.ams2.redhat.com [10.36.117.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B2A96353B; Fri, 9 Mar 2018 13:29:27 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 9 Mar 2018 14:29:19 +0100 Message-Id: <20180309132922.24211-2-pbonzini@redhat.com> In-Reply-To: <20180309132922.24211-1-pbonzini@redhat.com> References: <20180309132922.24211-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 09 Mar 2018 13:29:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 09 Mar 2018 13:29:28 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 1/4] rcutorture: remove synchronize_rcu from readers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G . Cota" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This gives much worse numbers for readers, especially if synchronize_rcu is made more expensive as is the case with --enable-membarrier. Before: $ tests/rcutorture 10 stress 10 n_reads: 98304 n_updates: 529 n_mberror: 0 rcu_stress_count: 98302 2 0 0 0 0 0 0 0 0 0 After: $ tests/rcutorture 10 stress 10 n_reads: 165158482 n_updates: 429 n_mberror: 0 rcu_stress_count: 165154364 4118 0 0 0 0 0 0 0 0 0 Signed-off-by: Paolo Bonzini --- tests/rcutorture.c | 4 ---- 1 file changed, 4 deletion(-) diff --git a/tests/rcutorture.c b/tests/rcutorture.c index 4002ecf123..2a7201549a 100644 --- a/tests/rcutorture.c +++ b/tests/rcutorture.c @@ -238,7 +238,6 @@ long long rcu_stress_count[RCU_STRESS_PIPE_LEN + 1]; static void *rcu_read_stress_test(void *arg) { int i; - int itercnt =3D 0; struct rcu_stress *p; int pc; long long n_reads_local =3D 0; @@ -269,9 +269,6 @@ static void *rcu_read_stress_test(void *arg) } rcu_stress_local[pc]++; n_reads_local++; - if ((++itercnt % 0x1000) =3D=3D 0) { - synchronize_rcu(); - } } qemu_mutex_lock(&counts_mutex); n_reads +=3D n_reads_local; --=20 2.14.3 From nobody Sat Oct 25 09:05:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520602281329126.83704223516634; Fri, 9 Mar 2018 05:31:21 -0800 (PST) Received: from localhost ([::1]:45298 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI7E-0000rV-Ho for importer@patchew.org; Fri, 09 Mar 2018 08:31:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47261) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI5Y-0008E7-Ug for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euI5V-0003oz-Qe for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:36 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37623 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euI5V-0003o0-KY for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:33 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 78D044074CA7; Fri, 9 Mar 2018 13:29:29 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-35.ams2.redhat.com [10.36.117.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id EDA736353B; Fri, 9 Mar 2018 13:29:28 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 9 Mar 2018 14:29:20 +0100 Message-Id: <20180309132922.24211-3-pbonzini@redhat.com> In-Reply-To: <20180309132922.24211-1-pbonzini@redhat.com> References: <20180309132922.24211-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 13:29:29 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 13:29:29 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 2/4] rcu: make memory barriers more explicit X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G . Cota" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Prepare for introducing smp_mb_placeholder() and smp_mb_global(). The new smp_mb() in synchronize_rcu() is not strictly necessary, since the first atomic_mb_set for rcu_gp_ctr provides the required ordering. However, synchronize_rcu is not performance critical, and it *will* be necessary to introduce a smp_mb_global before calling wait_for_readers(). Signed-off-by: Paolo Bonzini --- include/qemu/rcu.h | 15 +++++++++++++-- util/rcu.c | 12 +++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h index f19413d649..625f09ac09 100644 --- a/include/qemu/rcu.h +++ b/include/qemu/rcu.h @@ -79,7 +79,10 @@ static inline void rcu_read_lock(void) } =20 ctr =3D atomic_read(&rcu_gp_ctr); - atomic_xchg(&p_rcu_reader->ctr, ctr); + atomic_set(&p_rcu_reader->ctr, ctr); + + /* Write p_rcu_reader->ctr before reading RCU-protected pointers. */ + smp_mb(); } =20 static inline void rcu_read_unlock(void) @@ -91,7 +94,15 @@ static inline void rcu_read_unlock(void) return; } =20 - atomic_xchg(&p_rcu_reader->ctr, 0); + /* Ensure that the critical section is seen to precede the + * store to p_rcu_reader->ctr. Together with the following + * smp_mb(), this ensures writes to p_rcu_reader->ctr + * are sequentially consistent. + */ + atomic_store_release(&p_rcu_reader->ctr, 0); + + /* Write p_rcu_reader->ctr before reading p_rcu_reader->waiting. */ + smp_mb(); if (unlikely(atomic_read(&p_rcu_reader->waiting))) { atomic_set(&p_rcu_reader->waiting, false); qemu_event_set(&rcu_gp_event); diff --git a/util/rcu.c b/util/rcu.c index f4d09c8304..7366dc50dd 100644 --- a/util/rcu.c +++ b/util/rcu.c @@ -92,8 +92,9 @@ static void wait_for_readers(void) atomic_set(&index->waiting, true); } =20 - /* Here, order the stores to index->waiting before the - * loads of index->ctr. + /* Here, order the stores to index->waiting before the loads of + * index->ctr. Pairs with smp_mb() in rcu_read_unlock(), + * ensuring that the loads of index->ctr are sequentially consiste= nt. */ smp_mb(); =20 @@ -142,8 +143,13 @@ static void wait_for_readers(void) void synchronize_rcu(void) { qemu_mutex_lock(&rcu_sync_lock); - qemu_mutex_lock(&rcu_registry_lock); =20 + /* Write RCU-protected pointers before reading p_rcu_reader->ctr. + * Pairs with smp_mb() in rcu_read_lock(). + */ + smp_mb(); + + qemu_mutex_lock(&rcu_registry_lock); if (!QLIST_EMPTY(®istry)) { /* In either case, the atomic_mb_set below blocks stores that free * old RCU-protected pointers. --=20 2.14.3 From nobody Sat Oct 25 09:05:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520602420835152.33769719102554; Fri, 9 Mar 2018 05:33:40 -0800 (PST) Received: from localhost ([::1]:45315 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI9U-0002YP-3e for importer@patchew.org; Fri, 09 Mar 2018 08:33:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47257) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI5Y-0008E2-UD for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euI5V-0003oo-Pb for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:36 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:39704 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euI5V-0003nx-KA for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:33 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3C73881A8133; Fri, 9 Mar 2018 13:29:30 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-35.ams2.redhat.com [10.36.117.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1B6C6F9D9; Fri, 9 Mar 2018 13:29:29 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 9 Mar 2018 14:29:21 +0100 Message-Id: <20180309132922.24211-4-pbonzini@redhat.com> In-Reply-To: <20180309132922.24211-1-pbonzini@redhat.com> References: <20180309132922.24211-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 09 Mar 2018 13:29:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 09 Mar 2018 13:29:30 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 3/4] membarrier: introduce qemu/sys_membarrier.h X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G . Cota" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This new header file provides heavy-weight "global" memory barriers that enforce memory ordering on each running thread belonging to the current process. For now, use a dummy implementation that issues memory barriers on both sides (matching what QEMU has been doing so far). Signed-off-by: Paolo Bonzini --- include/qemu/rcu.h | 7 ++++--- include/qemu/sys_membarrier.h | 17 +++++++++++++++++ util/rcu.c | 9 +++++---- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 include/qemu/sys_membarrier.h diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h index 625f09ac09..22876d1428 100644 --- a/include/qemu/rcu.h +++ b/include/qemu/rcu.h @@ -27,6 +27,7 @@ #include "qemu/thread.h" #include "qemu/queue.h" #include "qemu/atomic.h" +#include "qemu/sys_membarrier.h" =20 #ifdef __cplusplus extern "C" { @@ -82,7 +83,7 @@ static inline void rcu_read_lock(void) atomic_set(&p_rcu_reader->ctr, ctr); =20 /* Write p_rcu_reader->ctr before reading RCU-protected pointers. */ - smp_mb(); + smp_mb_placeholder(); } =20 static inline void rcu_read_unlock(void) @@ -96,13 +97,13 @@ static inline void rcu_read_unlock(void) =20 /* Ensure that the critical section is seen to precede the * store to p_rcu_reader->ctr. Together with the following - * smp_mb(), this ensures writes to p_rcu_reader->ctr + * smp_mb_placeholder(), this ensures writes to p_rcu_reader->ctr * are sequentially consistent. */ atomic_store_release(&p_rcu_reader->ctr, 0); =20 /* Write p_rcu_reader->ctr before reading p_rcu_reader->waiting. */ - smp_mb(); + smp_mb_placeholder(); if (unlikely(atomic_read(&p_rcu_reader->waiting))) { atomic_set(&p_rcu_reader->waiting, false); qemu_event_set(&rcu_gp_event); diff --git a/include/qemu/sys_membarrier.h b/include/qemu/sys_membarrier.h new file mode 100644 index 0000000000..9ce7f5210b --- /dev/null +++ b/include/qemu/sys_membarrier.h @@ -0,0 +1,17 @@ +/* + * Process-global memory barriers + * + * Copyright (c) 2018 Red Hat, Inc. + * + * Author: Paolo Bonzini + */ + +#ifndef QEMU_SYS_MEMBARRIER_H +#define QEMU_SYS_MEMBARRIER_H 1 + +/* Keep it simple, execute a real memory barrier on both sides. */ +static inline void smp_mb_global_init(void) {} +#define smp_mb_global() smp_mb() +#define smp_mb_placeholder() smp_mb() + +#endif diff --git a/util/rcu.c b/util/rcu.c index 7366dc50dd..5676c22bd1 100644 --- a/util/rcu.c +++ b/util/rcu.c @@ -93,10 +93,10 @@ static void wait_for_readers(void) } =20 /* Here, order the stores to index->waiting before the loads of - * index->ctr. Pairs with smp_mb() in rcu_read_unlock(), + * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock= (), * ensuring that the loads of index->ctr are sequentially consiste= nt. */ - smp_mb(); + smp_mb_global(); =20 QLIST_FOREACH_SAFE(index, ®istry, node, tmp) { if (!rcu_gp_ongoing(&index->ctr)) { @@ -145,9 +145,9 @@ void synchronize_rcu(void) qemu_mutex_lock(&rcu_sync_lock); =20 /* Write RCU-protected pointers before reading p_rcu_reader->ctr. - * Pairs with smp_mb() in rcu_read_lock(). + * Pairs with smp_mb_placeholder() in rcu_read_lock(). */ - smp_mb(); + smp_mb_global(); =20 qemu_mutex_lock(&rcu_registry_lock); if (!QLIST_EMPTY(®istry)) { @@ -376,6 +376,7 @@ static void rcu_init_child(void) =20 static void __attribute__((__constructor__)) rcu_init(void) { + smp_mb_global_init(); #ifdef CONFIG_POSIX pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child); #endif --=20 2.14.3 From nobody Sat Oct 25 09:05:41 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520602512894280.9771429702905; Fri, 9 Mar 2018 05:35:12 -0800 (PST) Received: from localhost ([::1]:45322 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euIAy-0003gv-46 for importer@patchew.org; Fri, 09 Mar 2018 08:35:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47258) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1euI5Y-0008E4-UE for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1euI5V-0003op-Pr for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:36 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:37622 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1euI5V-0003ny-K8 for qemu-devel@nongnu.org; Fri, 09 Mar 2018 08:29:33 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 00C344074C94; Fri, 9 Mar 2018 13:29:31 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-35.ams2.redhat.com [10.36.117.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 759396353B; Fri, 9 Mar 2018 13:29:30 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 9 Mar 2018 14:29:22 +0100 Message-Id: <20180309132922.24211-5-pbonzini@redhat.com> In-Reply-To: <20180309132922.24211-1-pbonzini@redhat.com> References: <20180309132922.24211-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 13:29:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 09 Mar 2018 13:29:31 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pbonzini@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 4/4] membarrier: add --enable-membarrier X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G . Cota" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Actually enable the global memory barriers if supported by the OS. Because only recent versions of Linux include the support, they are disabled by default. Note that it also has to be disabled for QEMU to run under Wine. Before this patch, rcutorture reports 85 ns/read for my machine, after the patch it reports 12.5 ns/read. On the other hand updates go from 50 *micro*seconds to 20 *milli*seconds. Signed-off-by: Paolo Bonzini --- configure | 41 ++++++++++++++++++++++++++++++++++- include/qemu/sys_membarrier.h | 10 +++++++++ util/Makefile.objs | 1 + util/sys_membarrier.c | 50 +++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 util/sys_membarrier.c diff --git a/configure b/configure index 6f3921c02a..4fcd18f136 100755 --- a/configure +++ b/configure @@ -342,7 +342,7 @@ attr=3D"" libattr=3D"" xfs=3D"" tcg=3D"yes" - +membarrier=3D"" vhost_net=3D"no" vhost_crypto=3D"no" vhost_scsi=3D"no" @@ -1161,6 +1161,10 @@ for opt do ;; --enable-attr) attr=3D"yes" ;; + --disable-membarrier) membarrier=3D"no" + ;; + --enable-membarrier) membarrier=3D"yes" + ;; --disable-blobs) blobs=3D"no" ;; --with-pkgversion=3D*) pkgversion=3D" ($optarg)" @@ -1577,6 +1581,7 @@ disabled with --disable-FEATURE, default is enabled i= f available: xen-pci-passthrough brlapi BrlAPI (Braile) curl curl connectivity + membarrier membarrier system call (for Linux 4.14+ or Windows) fdt fdt device tree bluez bluez stack connectivity kvm KVM acceleration support @@ -5137,6 +5142,35 @@ if compile_prog "" "" ; then have_fsxattr=3Dyes fi =20 +########################################## +# check for usable membarrier system call +if test "$membarrier" =3D "yes"; then + have_membarrier=3Dno + if test "$mingw32" =3D "yes" ; then + have_membarrier=3Dyes + elif test "$linux" =3D "yes" ; then + cat > $TMPC << EOF + #include + #include + #include + int main(void) { + syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, 0); + syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0); + } +EOF + if compile_prog "" "" ; then + have_membarrier=3Dyes + fi + fi + if test "$have_membarrier" =3D "no"; then + feature_not_found "membarrier" "membarrier system call not available" + fi +else + # Do not enable it by default even for Mingw32, because it doesn't + # work on Wine. + membarrier=3Dno +fi + ########################################## # check if rtnetlink.h exists and is useful have_rtnetlink=3Dno @@ -5763,6 +5798,7 @@ fi echo "malloc trim support $malloc_trim" echo "RDMA support $rdma" echo "fdt support $fdt" +echo "membarrier $membarrier" echo "preadv support $preadv" echo "fdatasync $fdatasync" echo "madvise $madvise" @@ -6245,6 +6281,9 @@ fi if test "$fdt" =3D "yes" ; then echo "CONFIG_FDT=3Dy" >> $config_host_mak fi +if test "$membarrier" =3D "yes" ; then + echo "CONFIG_MEMBARRIER=3Dy" >> $config_host_mak +fi if test "$signalfd" =3D "yes" ; then echo "CONFIG_SIGNALFD=3Dy" >> $config_host_mak fi diff --git a/include/qemu/sys_membarrier.h b/include/qemu/sys_membarrier.h index 9ce7f5210b..316e3dc4a2 100644 --- a/include/qemu/sys_membarrier.h +++ b/include/qemu/sys_membarrier.h @@ -9,9 +9,19 @@ #ifndef QEMU_SYS_MEMBARRIER_H #define QEMU_SYS_MEMBARRIER_H 1 =20 +#ifdef CONFIG_MEMBARRIER +/* Only block reordering at the compiler level in the performance-critical + * side. The slow side forces processor-level ordering on all other cores + * through a system call. + */ +extern void smp_mb_global_init(void); +extern void smp_mb_global(void); +#define smp_mb_placeholder() barrier() +#else /* Keep it simple, execute a real memory barrier on both sides. */ static inline void smp_mb_global_init(void) {} #define smp_mb_global() smp_mb() #define smp_mb_placeholder() smp_mb() +#endif =20 #endif diff --git a/util/Makefile.objs b/util/Makefile.objs index ae90b9963d..728c3541db 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -33,6 +33,7 @@ util-obj-y +=3D throttle.o util-obj-y +=3D getauxval.o util-obj-y +=3D readline.o util-obj-y +=3D rcu.o +util-obj-$(CONFIG_MEMBARRIER) +=3D sys_membarrier.o util-obj-y +=3D qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y +=3D qemu-coroutine-sleep.o util-obj-y +=3D coroutine-$(CONFIG_COROUTINE_BACKEND).o diff --git a/util/sys_membarrier.c b/util/sys_membarrier.c new file mode 100644 index 0000000000..8dcb53e63e --- /dev/null +++ b/util/sys_membarrier.c @@ -0,0 +1,50 @@ +/* + * Process-global memory barriers + * + * Copyright (c) 2018 Red Hat, Inc. + * + * Author: Paolo Bonzini + */ + +#include +#include +#include + +#ifdef CONFIG_LINUX +#include +#include + +static int +membarrier(int cmd, int flags) +{ + return syscall(__NR_membarrier, cmd, flags); +} +#endif + +void smp_mb_global(void) +{ +#if defined CONFIG_WIN32 + FlushProcessWriteBuffers(); +#elif defined CONFIG_LINUX + membarrier(MEMBARRIER_CMD_SHARED, 0); +#else +#error --enable-membarrier is not supported on this operating system. +#endif +} + +void smp_mb_global_init(void) +{ +#ifdef CONFIG_LINUX + int ret =3D membarrier(MEMBARRIER_CMD_QUERY, 0); + if (ret < 0) { + error_report("This QEMU binary requires the membarrier system call= ."); + error_report("Please upgrade your system to a newer version of Lin= ux"); + exit(1); + } + if (!(ret & MEMBARRIER_CMD_SHARED)) { + error_report("This QEMU binary requires MEMBARRIER_CMD_SHARED supp= ort."); + error_report("Please upgrade your system to a newer version of Lin= ux"); + exit(1); + } +#endif +} --=20 2.14.3