From patchwork Tue Aug 25 06:35:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 7068491 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 948C69F344 for ; Tue, 25 Aug 2015 06:36:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B68B420823 for ; Tue, 25 Aug 2015 06:36:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C163520821 for ; Tue, 25 Aug 2015 06:36:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755308AbbHYGgT (ORCPT ); Tue, 25 Aug 2015 02:36:19 -0400 Received: from blu004-omc1s20.hotmail.com ([65.55.116.31]:60606 "EHLO BLU004-OMC1S20.hotmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754892AbbHYGgR (ORCPT ); Tue, 25 Aug 2015 02:36:17 -0400 Received: from BLU436-SMTP242 ([65.55.116.9]) by BLU004-OMC1S20.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Mon, 24 Aug 2015 23:36:16 -0700 X-TMN: [CZHoSgkwiqibpVIFE8f6Hdio6LZXZ9mzcfWaa1N8kAY=] 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 v2 2/3] KVM: dynamic halt_poll_ns adjustment Date: Tue, 25 Aug 2015 14:35:18 +0800 X-Mailer: git-send-email 1.9.1 In-Reply-To: <1440484519-2709-1-git-send-email-wanpeng.li@hotmail.com> References: <1440484519-2709-1-git-send-email-wanpeng.li@hotmail.com> X-OriginalArrivalTime: 25 Aug 2015 06:36:15.0849 (UTC) FILETIME=[57B92590:01D0DF00] MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. A third new parameter, halt_poll_ns_max, controls the maximal halt_poll_ns; it is internally rounded down to a closest multiple of halt_poll_ns_grow. 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 halt_poll_ns_shrink/ | halt_poll_ns_grow | grow halt_poll_ns | shrink halt_poll_ns ---------------------+----------------------+------------------- < 1 | = halt_poll_ns | = 0 < halt_poll_ns | *= halt_poll_ns_grow | /= halt_poll_ns_shrink otherwise | += halt_poll_ns_grow | -= halt_poll_ns_shrink Signed-off-by: Wanpeng Li --- virt/kvm/kvm_main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 93db833..2a4962b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -66,9 +66,26 @@ MODULE_AUTHOR("Qumranet"); MODULE_LICENSE("GPL"); -static unsigned int halt_poll_ns; +#define KVM_HALT_POLL_NS 500000 +#define KVM_HALT_POLL_NS_GROW 2 +#define KVM_HALT_POLL_NS_SHRINK 0 +#define KVM_HALT_POLL_NS_MAX 2000000 + +static unsigned int halt_poll_ns = KVM_HALT_POLL_NS; module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR); +/* Default doubles per-vcpu halt_poll_ns. */ +static unsigned int halt_poll_ns_grow = KVM_HALT_POLL_NS_GROW; +module_param(halt_poll_ns_grow, int, S_IRUGO); + +/* Default resets per-vcpu halt_poll_ns . */ +static unsigned int halt_poll_ns_shrink = KVM_HALT_POLL_NS_SHRINK; +module_param(halt_poll_ns_shrink, int, S_IRUGO); + +/* halt polling only reduces halt latency by 10-15 us, 2ms is enough */ +static unsigned int halt_poll_ns_max = KVM_HALT_POLL_NS_MAX; +module_param(halt_poll_ns_max, int, S_IRUGO); + /* * Ordering of locks: * @@ -1907,6 +1924,48 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) } EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); +static unsigned int __grow_halt_poll_ns(unsigned int val) +{ + if (halt_poll_ns_grow < 1) + return halt_poll_ns; + + val = min(val, halt_poll_ns_max); + + if (val == 0) + return halt_poll_ns; + + if (halt_poll_ns_grow < halt_poll_ns) + val *= halt_poll_ns_grow; + else + val += halt_poll_ns_grow; + + return val; +} + +static unsigned int __shrink_halt_poll_ns(int val, int modifier, int minimum) +{ + if (modifier < 1) + return 0; + + if (modifier < halt_poll_ns) + val /= modifier; + else + val -= modifier; + + return val; +} + +static void grow_halt_poll_ns(struct kvm_vcpu *vcpu) +{ + vcpu->halt_poll_ns = __grow_halt_poll_ns(vcpu->halt_poll_ns); +} + +static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu) +{ + vcpu->halt_poll_ns = __shrink_halt_poll_ns(vcpu->halt_poll_ns, + halt_poll_ns_shrink, halt_poll_ns); +} + static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) { if (kvm_arch_vcpu_runnable(vcpu)) { @@ -1954,6 +2013,10 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) break; waited = true; + if (vcpu->halt_poll_ns > halt_poll_ns_max) + shrink_halt_poll_ns(vcpu); + else + grow_halt_poll_ns(vcpu); schedule(); }