From nobody Sat Oct 25 20:15: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