Received: by 10.223.185.116 with SMTP id b49csp7224515wrg; Thu, 1 Mar 2018 01:51:36 -0800 (PST) X-Google-Smtp-Source: AG47ELt7EI/ZZOV7Nxvnip+JZtfQv206VbtaMTc6Qphxbc6NbHOw31ha/wQsvahmGHoAJIWe7YX4 X-Received: by 2002:a17:902:968d:: with SMTP id n13-v6mr1418527plp.33.1519897896650; Thu, 01 Mar 2018 01:51:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519897896; cv=none; d=google.com; s=arc-20160816; b=iN2+8Hk1F7VuSnZegJpUWvUirUfJBrwAii+l2lexV61OS24sqRR5duZIF0GMDhnloO Y3MBTCdXJyrGN90QZJ7H7tXofYu/kRH2x6Sq3/GGiFysoeYSGEF8leDitAxb01MlyY31 qxzClg4DSilkPclEan7/9ys4QGLWmd2j8AgDD1KZ9wBB3pmyniM6kTeSvw0Rm7uqZp4z 1t+5Z3w1FEk2WkY2Xxn4Z5iZ6sPA8Fo7Od+wkWUURSw3q/VQ1euxFiRWVQLO7+LKkD4I WJ00JYh2/1aQ5XjiDbQn6lydbYChyGJeKlANhGLgLjm+IlcM7mzMgfuSy3N8InC4q7hp +YFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=PS1W7NGxiDEINTe1+MEYkaa0yyGeFLLyACsFymXKswI=; b=uwn+TrOTdNRFwPgvlDu5423Fqtp33l0sgfEMguQ85VmsU5vHrHpR6mXRgT6wF9mzq3 PfmtFi7RWEsVFMP7tNKu2DeiVq77/c9nIcDcArRfc4HxFOBcXTaRR59CduElsy3EkH54 JG9F2k7l+twgf4SiST3LuX/CZ+Eb/r8fJMg1/29SUyP4uKhgz5WtusLFzr2C/XmaDqzF z5f9NrniSzOxN135goDE7V0NdbRPMkRiWf995YGp0OskT5P/obIgsYepVyRTuZpL1E3F Y7hCkicA4guOS/K7gyDQO7nBGFIB1cQ4R4Fy+YxfBhthM6P8zA8fvztwnfd2ZH2/o5AN RoBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZikxBMn4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g3-v6si2789264pld.366.2018.03.01.01.51.22; Thu, 01 Mar 2018 01:51:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZikxBMn4; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967274AbeCAJuZ (ORCPT + 99 others); Thu, 1 Mar 2018 04:50:25 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:37189 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967189AbeCAJt4 (ORCPT ); Thu, 1 Mar 2018 04:49:56 -0500 Received: by mail-pf0-f194.google.com with SMTP id h11so1822877pfn.4; Thu, 01 Mar 2018 01:49:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PS1W7NGxiDEINTe1+MEYkaa0yyGeFLLyACsFymXKswI=; b=ZikxBMn462F+JcesvO5b8t4SnEq6ldaGNf5BHyiejMcKp6WHGnLopET3tcA6/cu+6r MR4grIJ86ND5/YcCXilE6k/lAiHuTYOAQj5d8ldR+PhMoXRO88ao+trz5Pmxqd1u2V+e Fy0x0tAzl6QHiakk3bRwR628Z/o7xprRmup4iIbZ0Y4cCpA7UWdT1/OXLOj+t2hW1oQQ MaLht2E6uISsXau57xhprBI4LuCE2VKVg3LJjhvaD4KkzIg7wZ/Jytm26nII1gcn+QMh 5ATuCHn4bmBpR7WcGQVOnRp75YWfxum8vE2TcqpTTzHpkaeEb/bwiE3MHCi8htLMbf2m MR3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PS1W7NGxiDEINTe1+MEYkaa0yyGeFLLyACsFymXKswI=; b=L9iYp/D5S2XTbm5diEkC8jpjV9PYYBrGn6FxZg1J5hvyxWH69Ze4UxADrZQRSTfDTr +mZvo8Roq+QRGTGRGlcs+ccUsWRGMemrEziWx4yO89lISRMu/FhOdwgSOBJh2nBRpBO3 txH4zDAHPuKiPTpufrz3nf8gtRcEfc66v5Mqwbtc6lCSe1OqxPdWH57mJwAR/pEI2k0T DM4w1+nDKZ5u39fNeH8YXN1KHAKcBwG4pXN1PDB2fNlcSrzp3snWFQ8+ypn9UwbmFhrH y9344Iw057MjZCm5c14hwoaEX16V4efD+IbIh5Ow2w9nXFM9jD+TuBYS1OzSqlhTmUsh n4bQ== X-Gm-Message-State: APf1xPAzMFBdCbrLbaNTYRLuQYu5550LJ/yjT+49xrJfOwFk7ZZ7BVs+ PFDmo22KKKtVVyRCjZcUknL7rQ== X-Received: by 10.99.147.91 with SMTP id w27mr1088362pgm.208.1519897795590; Thu, 01 Mar 2018 01:49:55 -0800 (PST) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id 9sm8142398pfq.63.2018.03.01.01.49.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 01:49:55 -0800 (PST) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH 3/3] KVM: X86: Provides userspace with a capability to not intercept PAUSE Date: Thu, 1 Mar 2018 17:49:42 +0800 Message-Id: <1519897782-8124-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519897782-8124-1-git-send-email-wanpengli@tencent.com> References: <1519897782-8124-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li Allow to disable pause loop exit/pause filtering on a per VM basis. If some VMs have dedicated host CPUs, they won't be negatively affected due to needlessly intercepted PAUSE instructions. Thanks to Jan H. Schönherr's initial patch. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 3 ++- arch/x86/kvm/vmx.c | 17 +++++++++++++---- arch/x86/kvm/x86.c | 4 +++- arch/x86/kvm/x86.h | 9 ++++++++- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ee739ad..555f6d4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -809,6 +809,7 @@ struct kvm_arch { bool mwait_in_guest; bool hlt_in_guest; + bool pause_in_guest; bool ept_identity_pagetable_done; gpa_t ept_identity_map_addr; diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index fcf8339..53cb3ff 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -1452,7 +1452,8 @@ static void init_vmcb(struct vcpu_svm *svm) svm->nested.vmcb = 0; svm->vcpu.arch.hflags = 0; - if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { + if (boot_cpu_has(X86_FEATURE_PAUSEFILTER) && + !kvm_pause_in_guest(svm->vcpu.kvm)) { control->pause_filter_count = 3000; set_intercept(svm, INTERCEPT_PAUSE); } diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index f9f887a..b2cffcd 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -5554,7 +5554,7 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) } if (!enable_unrestricted_guest) exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; - if (!ple_gap) + if (kvm_pause_in_guest(vmx->vcpu.kvm)) exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; if (!kvm_vcpu_apicv_active(vcpu)) exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | @@ -5717,7 +5717,7 @@ static void vmx_vcpu_setup(struct vcpu_vmx *vmx) vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); } - if (ple_gap) { + if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { vmcs_write32(PLE_GAP, ple_gap); vmx->ple_window = ple_window; vmx->ple_window_dirty = true; @@ -7149,7 +7149,7 @@ static __exit void hardware_unsetup(void) */ static int handle_pause(struct kvm_vcpu *vcpu) { - if (ple_gap) + if (!kvm_pause_in_guest(vcpu->kvm)) grow_ple_window(vcpu); /* @@ -9844,6 +9844,13 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) return ERR_PTR(err); } +static int vmx_vm_init(struct kvm *kvm) +{ + if (!ple_gap) + kvm->arch.pause_in_guest = true; + return 0; +} + static void __init vmx_check_processor_compat(void *rtn) { struct vmcs_config vmcs_conf; @@ -11965,7 +11972,7 @@ static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) { - if (ple_gap) + if (!kvm_pause_in_guest(vcpu->kvm)) shrink_ple_window(vcpu); } @@ -12324,6 +12331,8 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, + .vm_init = vmx_vm_init, + .vcpu_create = vmx_create_vcpu, .vcpu_free = vmx_free_vcpu, .vcpu_reset = vmx_vcpu_reset, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 13f01d7..eafa0cb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2845,7 +2845,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r = KVM_CLOCK_TSC_STABLE; break; case KVM_CAP_X86_DISABLE_EXITS: - r |= KVM_X86_DISABLE_EXITS_HTL; + r |= KVM_X86_DISABLE_EXITS_HTL | KVM_X86_DISABLE_EXITS_PAUSE; if(kvm_mwait_can_in_guest()) r |= KVM_X86_DISABLE_EXITS_MWAIT; break; @@ -4202,6 +4202,8 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.mwait_in_guest = true; if (cap->args[0] & KVM_X86_DISABLE_EXITS_HTL) kvm->arch.hlt_in_guest = true; + if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) + kvm->arch.pause_in_guest = true; r = 0; break; default: diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index d4ddb00..658ea9a 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -264,8 +264,10 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) #define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0) #define KVM_X86_DISABLE_EXITS_HTL (1 << 1) +#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \ - KVM_X86_DISABLE_EXITS_HTL) + KVM_X86_DISABLE_EXITS_HTL | \ + KVM_X86_DISABLE_EXITS_PAUSE) static inline bool kvm_mwait_in_guest(struct kvm *kvm) { @@ -277,4 +279,9 @@ static inline bool kvm_hlt_in_guest(struct kvm *kvm) return kvm->arch.hlt_in_guest; } +static inline bool kvm_pause_in_guest(struct kvm *kvm) +{ + return kvm->arch.pause_in_guest; +} + #endif -- 2.7.4