From patchwork Wed May 25 02:09:38 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takuya Yoshikawa X-Patchwork-Id: 814682 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4P29krc001846 for ; Wed, 25 May 2011 02:09:46 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752785Ab1EYCJo (ORCPT ); Tue, 24 May 2011 22:09:44 -0400 Received: from mail-pv0-f174.google.com ([74.125.83.174]:33963 "EHLO mail-pv0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752365Ab1EYCJn (ORCPT ); Tue, 24 May 2011 22:09:43 -0400 Received: by pvg12 with SMTP id 12so3218649pvg.19 for ; Tue, 24 May 2011 19:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:date:from:to:cc:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding; bh=CDvnqseO+4NrtlMiqf70dDFQtWTwDxDASnOK/XzdzMg=; b=j1FPzebaMbrS2XR2fIpT/Df4Wq4a85LS5hiaJYjjLDZSsfOyfEhkSdWBjlA+geOCvH hTQ4XdZ3CZT10e8fWF4nL1Izz12NWHd+kZRvmq2kImnxL4DCoCJklaBLErmKGuK5X/Du lqL9zZkR+jhV1AkynL7sNX1Nc/JssBs10TFIM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:cc:subject:message-id:in-reply-to:references:x-mailer :mime-version:content-type:content-transfer-encoding; b=ZLp6Lk+Iu6W2LV132yo+yHwBHWt9WsBSn9QMcw6o2QwwB6hAODZcwK10bwNnryXqqN gWxBA5liYbq1tVLXyX7q2vUUSXZuLTmKi+QwKWlJkCrhgQ+x2dH9fT9ykXYQgntd1MX2 A9DZ6u9aETsC9p22Pbi1ip1Qu4YFE6oovSVPw= Received: by 10.142.8.30 with SMTP id 30mr1166436wfh.235.1306289382760; Tue, 24 May 2011 19:09:42 -0700 (PDT) Received: from amd (x096101.dynamic.ppp.asahi-net.or.jp [122.249.96.101]) by mx.google.com with ESMTPS id x12sm7226599wfd.18.2011.05.24.19.09.40 (version=SSLv3 cipher=OTHER); Tue, 24 May 2011 19:09:41 -0700 (PDT) Date: Wed, 25 May 2011 11:09:38 +0900 From: Takuya Yoshikawa To: avi@redhat.com, mtosatti@redhat.com Cc: kvm@vger.kernel.org, yoshikawa.takuya@oss.ntt.co.jp, gleb@redhat.com Subject: [PATCH 2/2] KVM: x86 emulator: Avoid clearing the whole decode_cache Message-Id: <20110525110938.03bdc229.takuya.yoshikawa@gmail.com> In-Reply-To: <20110525110616.7cd61e29.takuya.yoshikawa@gmail.com> References: <20110525110616.7cd61e29.takuya.yoshikawa@gmail.com> X-Mailer: Sylpheed 3.1.0 (GTK+ 2.24.4; x86_64-pc-linux-gnu) Mime-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Wed, 25 May 2011 02:09:46 +0000 (UTC) From: Takuya Yoshikawa During tracing the emulator, we noticed that init_emulate_ctxt() sometimes took a bit longer time than we expected. This patch is for mitigating the problem by some degree. By looking into the function, we soon notice that it clears the whole decode_cache whose size is about 2.5K bytes now. Furthermore, most of the bytes are taken for the two read_cache arrays, which are used only by a few instructions. Considering the fact that we are not assuming the cache arrays have been cleared when we store actual data, we do not need to clear the arrays: 2K bytes elimination. In addition, we can avoid clearing the fetch_cache and regs arrays. This patch changes the initialization not to clear the arrays. On our 64-bit host, init_emulate_ctxt() becomes 0.3 to 0.5us faster with this patch applied. Signed-off-by: Takuya Yoshikawa Cc: Gleb Natapov --- arch/x86/include/asm/kvm_emulate.h | 5 +++-- arch/x86/kvm/x86.c | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h index ab09ba2..c0f77e0 100644 --- a/arch/x86/include/asm/kvm_emulate.h +++ b/arch/x86/include/asm/kvm_emulate.h @@ -246,8 +246,6 @@ struct decode_cache { unsigned int d; int (*execute)(struct x86_emulate_ctxt *ctxt); int (*check_perm)(struct x86_emulate_ctxt *ctxt); - unsigned long regs[NR_VCPU_REGS]; - unsigned long eip; /* modrm */ u8 modrm; u8 modrm_mod; @@ -255,6 +253,9 @@ struct decode_cache { u8 modrm_rm; u8 modrm_seg; bool rip_relative; + unsigned long eip; + /* Fields above regs are cleared together. */ + unsigned long regs[NR_VCPU_REGS]; struct fetch_cache fetch; struct read_cache io_read; struct read_cache mem_read; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e7d337e..57f4160 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4506,6 +4506,20 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) kvm_queue_exception(vcpu, ctxt->exception.vector); } +static void init_decode_cache(struct decode_cache *c, + const unsigned long *regs) +{ + memset(c, 0, offsetof(struct decode_cache, regs)); + memcpy(c->regs, regs, sizeof(c->regs)); + + c->fetch.start = 0; + c->fetch.end = 0; + c->io_read.pos = 0; + c->io_read.end = 0; + c->mem_read.pos = 0; + c->mem_read.end = 0; +} + static void init_emulate_ctxt(struct kvm_vcpu *vcpu) { struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; @@ -4531,8 +4545,7 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) X86EMUL_MODE_PROT16; ctxt->guest_mode = is_guest_mode(vcpu); - memset(c, 0, sizeof(struct decode_cache)); - memcpy(c->regs, vcpu->arch.regs, sizeof c->regs); + init_decode_cache(c, vcpu->arch.regs); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }