From patchwork Thu Mar 16 16:01:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 9628807 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AA0CC6048C for ; Thu, 16 Mar 2017 16:02:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9BA9B28676 for ; Thu, 16 Mar 2017 16:02:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 900AE2867A; Thu, 16 Mar 2017 16:02:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,HK_RANDOM_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45BE528676 for ; Thu, 16 Mar 2017 16:02:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752709AbdCPQCb (ORCPT ); Thu, 16 Mar 2017 12:02:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52510 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752555AbdCPQCa (ORCPT ); Thu, 16 Mar 2017 12:02:30 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1BFF6437F7A; Thu, 16 Mar 2017 16:02:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1BFF6437F7A Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=rkrcmar@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1BFF6437F7A Received: from potion (dhcp-1-208.brq.redhat.com [10.34.1.208]) by smtp.corp.redhat.com (Postfix) with SMTP id 424B960A9E; Thu, 16 Mar 2017 16:02:00 +0000 (UTC) Received: by potion (sSMTP sendmail emulation); Thu, 16 Mar 2017 17:01:58 +0100 Date: Thu, 16 Mar 2017 17:01:58 +0100 From: Radim =?utf-8?B?S3LEjW3DocWZ?= To: "Gabriel L. Somlo" Cc: "Michael S. Tsirkin" , linux-kernel@vger.kernel.org, Paolo Bonzini , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Joerg Roedel , kvm@vger.kernel.org, linux-doc@vger.kernel.org Subject: Re: [PATCH v5 untested] kvm: better MWAIT emulation for guests Message-ID: <20170316160157.GN14081@potion> References: <1489612895-12799-1-git-send-email-mst@redhat.com> <20170315233534.GG2239@HEDWIG.INI.CMU.EDU> <20170316013903-mutt-send-email-mst@kernel.org> <20170316132426.GB4085@HEDWIG.INI.CMU.EDU> <20170316155550-mutt-send-email-mst@kernel.org> <20170316145819.GC4085@HEDWIG.INI.CMU.EDU> <20170316153517.GL14081@potion> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20170316153517.GL14081@potion> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 16 Mar 2017 16:02:07 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 2017-03-16 16:35+0100, Radim Krčmář: > 2017-03-16 10:58-0400, Gabriel L. Somlo: >> The intel manual said the same thing back in 2010 as well. However, >> regardless of how any flags were set, interrupt-window exiting or not, >> "normal" L1 MWAIT behavior was that it woke up immediately regardless. >> Remember, never going to sleep is still correct ("normal" ?) behavior >> per the ISA definition of MWAIT :) > > I'll write a simple kvm-unit-test to better understand why it is broken > for you ... Please get git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git and try this, thanks! ---8<--- x86/mwait: crappy test `./configure && make` to build it, then follow the comment in code to try few cases. --- x86/Makefile.common | 1 + x86/mwait.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 x86/mwait.c diff --git a/x86/Makefile.common b/x86/Makefile.common index 1dad18ba26e1..1e708a6acd39 100644 --- a/x86/Makefile.common +++ b/x86/Makefile.common @@ -46,6 +46,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \ $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \ $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \ + $(TEST_DIR)/mwait.flat \ ifdef API tests-common += api/api-sample diff --git a/x86/mwait.c b/x86/mwait.c new file mode 100644 index 000000000000..c21dab5cc97d --- /dev/null +++ b/x86/mwait.c @@ -0,0 +1,41 @@ +#include "vm.h" + +#define TARGET_RESUMES 10000 +volatile unsigned page[4096 / 4]; + +/* + * Execute + * time TIMEOUT=20 ./x86-run x86/mwait.flat -append '0 1 1' + * (first two arguments are eax and ecx for MWAIT, the third is FLAGS.IF bit) + * I assume you have 1000 Hz scheduler, so the test should take about 10 + * seconds to run if mwait works (host timer interrupts will kick mwait). + * + * If you get far less, then mwait is just nop, as in the case of + * + * time TIMEOUT=20 ./x86-run x86/mwait.flat -append '0 1 0' + * + * All other combinations of arguments should take 10 seconds. + * Getting killed by the TIMEOUT most likely means that you have different HZ, + * but could also be a bug ... + */ +int main(int argc, char **argv) +{ + uint32_t eax = atol(argv[1]); + uint32_t ecx = atol(argv[2]); + bool sti = atol(argv[3]); + unsigned resumes = 0; + + if (sti) + asm volatile ("sti"); + else + asm volatile ("cli"); + + while (resumes < TARGET_RESUMES) { + asm volatile("monitor" :: "a" (page), "c" (0), "d" (0)); + asm volatile("mwait" :: "a" (eax), "c" (ecx)); + resumes++; + } + + report("resumed from mwait %u times", resumes == TARGET_RESUMES, resumes); + return report_summary(); +}