Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933238AbbHZJO5 (ORCPT ); Wed, 26 Aug 2015 05:14:57 -0400 Received: from blu004-omc1s34.hotmail.com ([65.55.116.45]:58128 "EHLO BLU004-OMC1S34.hotmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933179AbbHZJOx (ORCPT ); Wed, 26 Aug 2015 05:14:53 -0400 X-TMN: [1fa/6+x7Q2QnK4sDTbmduuDvCBuDwAr/YeOiRL5RegM=] X-Originating-Email: [wanpeng.li@hotmail.com] Message-ID: From: Wanpeng Li To: Paolo Bonzini CC: David Matlack , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wanpeng Li Subject: [PATCH v3 2/3] KVM: dynamic halt_poll_ns adjustment Date: Wed, 26 Aug 2015 17:14:32 +0800 X-Mailer: git-send-email 1.9.1 In-Reply-To: <1440580473-12099-1-git-send-email-wanpeng.li@hotmail.com> References: <1440580473-12099-1-git-send-email-wanpeng.li@hotmail.com> X-OriginalArrivalTime: 26 Aug 2015 09:14:51.0761 (UTC) FILETIME=[AA0FD610:01D0DFDF] MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2708 Lines: 95 There is a downside of halt_poll_ns since poll is still happen for idle VCPU which can waste cpu usage. This patch adds the ability to adjust halt_poll_ns dynamically. There are two new kernel parameters for changing the halt_poll_ns: halt_poll_ns_grow and halt_poll_ns_shrink. The shrink/grow matrix is suggested by David: if (poll successfully for interrupt): stay the same else if (length of kvm_vcpu_block is longer than halt_poll_ns_max): shrink else if (length of kvm_vcpu_block is less than halt_poll_ns_max): grow Signed-off-by: Wanpeng Li --- virt/kvm/kvm_main.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c06e57c..1ed02d1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -66,9 +66,18 @@ MODULE_AUTHOR("Qumranet"); MODULE_LICENSE("GPL"); -static unsigned int halt_poll_ns; +/* halt polling only reduces halt latency by 5-7 us, 2ms is enough */ +static unsigned int halt_poll_ns = 2000000; module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR); +/* Default doubles per-vcpu halt_poll_ns. */ +static unsigned int halt_poll_ns_grow = 2; +module_param(halt_poll_ns_grow, int, S_IRUGO); + +/* Default resets per-vcpu halt_poll_ns . */ +static unsigned int halt_poll_ns_shrink; +module_param(halt_poll_ns_shrink, int, S_IRUGO); + /* * Ordering of locks: * @@ -1907,6 +1916,31 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) } EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); +static void grow_halt_poll_ns(struct kvm_vcpu *vcpu) +{ + int val = vcpu->halt_poll_ns; + + /* 500us step */ + if (val == 0 && halt_poll_ns_grow) + val = 500000; + else + val *= halt_poll_ns_grow; + + vcpu->halt_poll_ns = val; +} + +static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu) +{ + int val = vcpu->halt_poll_ns; + + if (halt_poll_ns_shrink == 0) + val = 0; + else + val /= halt_poll_ns_shrink; + + vcpu->halt_poll_ns = val; +} + static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) { if (kvm_arch_vcpu_runnable(vcpu)) { @@ -1960,6 +1994,13 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) finish_wait(&vcpu->wq, &wait); cur = ktime_get(); + if (waited) { + if (vcpu->halt_poll_ns > halt_poll_ns) + shrink_halt_poll_ns(vcpu); + else + grow_halt_poll_ns(vcpu); + } + out: trace_kvm_vcpu_wakeup(ktime_to_ns(cur) - ktime_to_ns(start), waited); } -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/