From patchwork Tue Aug 29 11:46:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Zhang X-Patchwork-Id: 9927203 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 4884F6022E for ; Tue, 29 Aug 2017 11:48:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3516528874 for ; Tue, 29 Aug 2017 11:48:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A212288B6; Tue, 29 Aug 2017 11:48:50 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_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 C880128874 for ; Tue, 29 Aug 2017 11:48:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753553AbdH2LsS (ORCPT ); Tue, 29 Aug 2017 07:48:18 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:33601 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753494AbdH2Lr1 (ORCPT ); Tue, 29 Aug 2017 07:47:27 -0400 Received: by mail-pf0-f194.google.com with SMTP id c28so2191066pfe.0; Tue, 29 Aug 2017 04:47:27 -0700 (PDT) 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; bh=U1kD4I2Kk19Yi+ktjg+bPD3yQL3g8atG4QtgdxWo6S4=; b=iVAxXLOaXljQNoqBRSI9lhP+RB8K9vI+0QLL6mJd2K9FCoEinsZw5qIh7F2fWAUScj NmDmFQKVdYd43nppmB1rnoHZcGthZabv2H7zrnGla0mSbYBoq9W5LnKJLSgS1A32bGPC yEscqAIwx2vKPljSfWJNadEEjmO+iTvxhTXHeIFRUPRSosbR3Kj12Oscpe8eTIIxcQvQ AUTYbIT1fJkX/N/cN+NeaL1hj4l/zJbtfMZuCkt+aEzJqeUYruEZsyHMFyXskf1AwfzN hWU6X1FnaFFx3dv8Vwxb/DTG5W/QILrE6a7A62nZtQH1AqUxJ42NJpRcUrnHme2HjrAs RFtA== 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; bh=U1kD4I2Kk19Yi+ktjg+bPD3yQL3g8atG4QtgdxWo6S4=; b=bUZdRx8Fb+tQdet5YYzklyoOec+m1RvQ9vRJ0II23CgW18wG+sXPBbLVsqJhUhJ4fp VDEdtMQZmympD5bIRm1UyXZz1FkL4eR860QXfT5tN3aX3JzrbMPj7ZxrX/1gJh7yj/+V MxkAVWNWmHns99LnB21i18FapJqUeliDjsbVprmsxcmumgvTSd8hKfWCbLQHXNetUL/D FKgVuyuZ4ofpOGeMV/FKJNh7g17BWEecMZko/VmIRzFyi4DPPNy8jjkKyZS3rwFe85Xl yPR5jflHkY5S8p3Xadf9tIQCqdhuzVf/Al8b4YRk9UuC5owACoV9TbKqAnwAzWafUgLE R+ug== X-Gm-Message-State: AHYfb5j+Wjuai7/4+WUt7jyOYn2zCE3iQiD2TtsAtHZAKGuKddmjzNNl 9uBuWLf0Up10cXfdMyE= X-Received: by 10.98.72.151 with SMTP id q23mr63113pfi.60.1504007246822; Tue, 29 Aug 2017 04:47:26 -0700 (PDT) Received: from ip-172-31-39-62.us-west-2.compute.internal (ec2-52-40-99-241.us-west-2.compute.amazonaws.com. [52.40.99.241]) by smtp.googlemail.com with ESMTPSA id e90sm2550747pfb.172.2017.08.29.04.47.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 29 Aug 2017 04:47:26 -0700 (PDT) From: Yang Zhang To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, wanpeng.li@hotmail.com, mst@redhat.com, pbonzini@redhat.com, tglx@linutronix.de, rkrcmar@redhat.com, dmatlack@google.com, agraf@suse.de, peterz@infradead.org, linux-doc@vger.kernel.org, Yang Zhang , Quan Xu , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org Subject: [RFC PATCH v2 6/7] KVM guest: introduce smart idle poll algorithm Date: Tue, 29 Aug 2017 11:46:40 +0000 Message-Id: <1504007201-12904-7-git-send-email-yang.zhang.wz@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1504007201-12904-1-git-send-email-yang.zhang.wz@gmail.com> References: <1504007201-12904-1-git-send-email-yang.zhang.wz@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP using smart idle poll to reduce the useless poll when system is idle. Signed-off-by: Yang Zhang Signed-off-by: Quan Xu Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- arch/x86/kernel/kvm.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 7d84a02..ffc8307 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -373,12 +373,53 @@ static void kvm_idle_poll(void) } while (ktime_before(cur, stop)); } +static unsigned int grow_poll_ns(unsigned int old, unsigned int grow, + unsigned int max) +{ + unsigned int val; + + /* set base poll time to 10000ns */ + if (old == 0 && grow) + return 10000; + + val = old * grow; + if (val > max) + val = max; + + return val; +} + +static unsigned int shrink_poll_ns(unsigned int old, unsigned int shrink) +{ + if (shrink == 0) + return 0; + + return old / shrink; +} + +static int kvm_idle_update_poll_duration(unsigned long idle_duration) +{ + unsigned long poll_duration = this_cpu_read(poll_duration_ns); + + if (poll_duration && idle_duration > poll_threshold_ns) + poll_duration = shrink_poll_ns(poll_duration, poll_shrink); + else if (poll_duration < poll_threshold_ns && + idle_duration < poll_threshold_ns) + poll_duration = grow_poll_ns(poll_duration, poll_grow, + poll_threshold_ns); + + this_cpu_write(poll_duration_ns, poll_duration); + + return 0; +} + static void kvm_guest_idle_init(void) { if (!kvm_para_available()) return; pv_idle_ops.poll = kvm_idle_poll; + pv_idle_ops.update = kvm_idle_update_poll_duration; } static void kvm_pv_disable_apf(void)