From nobody Tue Dec 16 07:06:22 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; dkim=fail; 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 1520982255376813.2083458437294; Tue, 13 Mar 2018 16:04:15 -0700 (PDT) Received: from localhost ([::1]:43365 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evsxi-0002Oq-Nk for importer@patchew.org; Tue, 13 Mar 2018 19:04:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58131) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evsi8-0005p9-J1 for qemu-devel@nongnu.org; Tue, 13 Mar 2018 18:48:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evsi3-0003NO-Pq for qemu-devel@nongnu.org; Tue, 13 Mar 2018 18:48:00 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:34364) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1evsi3-0003Lt-GH for qemu-devel@nongnu.org; Tue, 13 Mar 2018 18:47:55 -0400 Received: by mail-wm0-x241.google.com with SMTP id a20so16162290wmd.1 for ; Tue, 13 Mar 2018 15:47:55 -0700 (PDT) Received: from donizetti.lan (94-36-191-219.adsl-ull.clienti.tiscali.it. [94.36.191.219]) by smtp.gmail.com with ESMTPSA id x107sm1557951wrb.97.2018.03.13.15.47.52 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 13 Mar 2018 15:47:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=Ry7nhQiq71fqJW0Nx3+t7ZqoUCjrgq6c1KhHLZlBI2U=; b=V7vCGidnd+AE31YzE1LWTiXdXll55iLf2AwnUTUaPtTK+jVWcp4IE2kl01FssEDiqa BlIzCtcl5Sy0OBbPd/2Y0kcu3DpqBMZMvAATODudTzA4n0xZsVSF7COr/nU7x/LFmUzX PLxjmJLMiZ0kAdDZAxc4H271wtlIODgOm95SXwLkGfvbBrutF5c9blXlQJPOVmKifkFA qcd6AYFwwaOpf+f8pf3Op2UyYfWZIho7tpl3OTJFAVIbcmTPvz9/HLuMjBF4R8YUjQfg I/gDa8CUpGt/Eh6GfdVmQjeDmkakeMPJB7KmfV/78+P9slWYhMGSi2KOCWToB0nZJd0d BI3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=Ry7nhQiq71fqJW0Nx3+t7ZqoUCjrgq6c1KhHLZlBI2U=; b=qjUJQXgN1mFI8lDuFiZpAfaalwuye7Xto643OxDJ2uQN24KqraV++3QKwDBU015fcq NczYrzypQx+I13wV10ebRDfDJj7Lo8oo4d90++BANf2ISZBrIJ5Y6UJfLUb6vSC68Qlr hZEGQc7mpEq3Cd+jbQxIj7GiyNP0bZaGNdKfK35LDMDsYeVTyINcF6wu1hPx95Zs5l/m tnIFHDV3LBY3QKtK5fo24odvLfh8DAyxHt6o/H31Ll+7v/+8A4Ca9HyZOEi8Uf+0jH2n Gb37ap2319w7LizkQvRHs0FP7Q3eOa7jyOP+YPGJPICVBH2XMr5CyZf//Xa45S0hc+2v yL8g== X-Gm-Message-State: AElRT7GjiYBbi6/M9gBBwdPL8UUO2UpySeVm1pTlrQ55f+pXOgZgCTMD ikNa9mPH9BxoHDJFrzz3w+BpeP9S X-Google-Smtp-Source: AG47ELsLsy+22G37QcCLbkcGj8Sw+m4eC93QrdJ/VEIJk4GWB59SrswdHaKTYG0MsofIw2sYDMRsKg== X-Received: by 10.28.144.5 with SMTP id s5mr2083297wmd.98.1520981273963; Tue, 13 Mar 2018 15:47:53 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 13 Mar 2018 23:46:36 +0100 Message-Id: <20180313224719.4954-27-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180313224719.4954-1-pbonzini@redhat.com> References: <20180313224719.4954-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PULL 26/69] 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 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 | 42 +++++++++++++++++++++++++++++++++++- include/qemu/sys_membarrier.h | 10 +++++++++ util/Makefile.objs | 1 + util/sys_membarrier.c | 50 +++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 util/sys_membarrier.c diff --git a/configure b/configure index 26d56eb5bb..db87fc4fed 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,37 @@ 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 + #include + int main(void) { + syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, 0); + syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0); + exit(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 +5799,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 +6282,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