linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhenzhong Duan <zhenzhong.duan@oracle.com>
To: linux-kernel@vger.kernel.org
Cc: pbonzini@redhat.com, rkrcmar@redhat.com,
	rafael.j.wysocki@intel.com, joao.m.martins@oracle.com,
	mtosatti@redhat.com, kvm@vger.kernel.org,
	linux-pm@vger.kernel.org,
	Zhenzhong Duan <zhenzhong.duan@oracle.com>
Subject: [PATCH RESEND v2 4/4] KVM: ensure vCPU halt_poll_us in right scope
Date: Wed,  6 Nov 2019 19:55:02 +0800	[thread overview]
Message-ID: <1573041302-4904-5-git-send-email-zhenzhong.duan@oracle.com> (raw)
In-Reply-To: <1573041302-4904-1-git-send-email-zhenzhong.duan@oracle.com>

As user can adjust halt_poll_ns_grow_start and halt_poll_ns which
leads to vcpu->halt_poll_ns beyond the two boundaries. This patch
ensures vcpu->halt_poll_ns in that scope after growing or shrinking.

If halt_poll_ns_shrink is 0, shrink vcpu->halt_poll_ns to
halt_poll_ns_grow_start instead of 0. To disable poll we can set
halt_poll_ns to 0.

In case user wrongly set halt_poll_ns_grow_start > halt_poll_ns > 0,
halt_poll_ns take precedency and poll time is a fixed value of
halt_poll_ns.

This patch also simplifies branch check based on the guest haltpoll
code.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@oracle.com>
---
 virt/kvm/kvm_main.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 359516b..b4fca66 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2308,9 +2308,15 @@ static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
 	old = val = vcpu->halt_poll_ns;
 	shrink = READ_ONCE(halt_poll_ns_shrink);
 	if (shrink == 0)
-		val = 0;
-	else
+		val = halt_poll_ns_grow_start;
+	else {
 		val /= shrink;
+		if (val < halt_poll_ns_grow_start)
+			val = halt_poll_ns_grow_start;
+	}
+
+	if (val > halt_poll_ns)
+		val = halt_poll_ns;
 
 	vcpu->halt_poll_ns = val;
 	trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
@@ -2385,21 +2391,12 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
 	block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
 
 	if (!kvm_arch_no_poll(vcpu)) {
-		if (!vcpu_valid_wakeup(vcpu)) {
+		/* we had a long block, shrink polling */
+		if (!vcpu_valid_wakeup(vcpu) || block_ns > halt_poll_ns)
 			shrink_halt_poll_ns(vcpu);
-		} else if (halt_poll_ns) {
-			if (block_ns <= vcpu->halt_poll_ns)
-				;
-			/* we had a long block, shrink polling */
-			else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns)
-				shrink_halt_poll_ns(vcpu);
-			/* we had a short halt and our poll time is too small */
-			else if (vcpu->halt_poll_ns < halt_poll_ns &&
-				block_ns < halt_poll_ns)
-				grow_halt_poll_ns(vcpu);
-		} else {
-			vcpu->halt_poll_ns = 0;
-		}
+		/* we had a short block and our poll time is too small */
+		else if (block_ns > vcpu->halt_poll_ns)
+			grow_halt_poll_ns(vcpu);
 	}
 
 	trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
-- 
1.8.3.1


      parent reply	other threads:[~2019-11-06 11:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06 11:54 [PATCH RESEND v2 0/4] misc fixes on halt-poll code for both KVM and guest Zhenzhong Duan
2019-11-06 11:54 ` [PATCH RESEND v2 1/4] cpuidle-haltpoll: ensure grow start value is nonzero Zhenzhong Duan
2019-11-15 10:06   ` Rafael J. Wysocki
2019-11-15 10:16     ` Rafael J. Wysocki
2019-11-15 10:26   ` Rafael J. Wysocki
2019-11-17  9:02     ` Zhenzhong Duan
2019-11-06 11:55 ` [PATCH RESEND v2 2/4] KVM: " Zhenzhong Duan
2019-11-11 20:13   ` Sean Christopherson
2019-11-12 12:19     ` Zhenzhong Duan
2019-11-06 11:55 ` [PATCH RESEND v2 3/4] cpuidle-haltpoll: ensure cpu_halt_poll_us in right scope Zhenzhong Duan
2019-11-15 10:45   ` Rafael J. Wysocki
2019-11-17  8:57     ` Zhenzhong Duan
2019-11-06 11:55 ` Zhenzhong Duan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1573041302-4904-5-git-send-email-zhenzhong.duan@oracle.com \
    --to=zhenzhong.duan@oracle.com \
    --cc=joao.m.martins@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rkrcmar@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).