linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dapeng Mi <dapeng1.mi@intel.com>
To: rafael@kernel.org, daniel.lezcano@linaro.org, pbonzini@redhat.com
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, zhenyuw@linux.intel.com,
	Dapeng Mi <dapeng1.mi@intel.com>
Subject: [PATCH] KVM: x86: use TPAUSE to replace PAUSE in halt polling
Date: Wed, 24 Aug 2022 17:11:17 +0800	[thread overview]
Message-ID: <20220824091117.767363-1-dapeng1.mi@intel.com> (raw)

TPAUSE is a new instruction on Intel processors which can instruct
processor enters a power/performance optimized state. Halt polling
uses PAUSE instruction to wait vCPU is waked up. The polling time
could be long and cause extra power consumption in some cases.

Use TPAUSE to replace the PAUSE instruction in halt polling to get
a better power saving and performance.

Signed-off-by: Dapeng Mi <dapeng1.mi@intel.com>
---
 drivers/cpuidle/poll_state.c |  3 ++-
 include/linux/kvm_host.h     | 20 ++++++++++++++++++++
 virt/kvm/kvm_main.c          |  2 +-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c
index f7e83613ae94..51ec333cbf80 100644
--- a/drivers/cpuidle/poll_state.c
+++ b/drivers/cpuidle/poll_state.c
@@ -7,6 +7,7 @@
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
+#include <linux/kvm_host.h>
 
 #define POLL_IDLE_RELAX_COUNT	200
 
@@ -25,7 +26,7 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,
 		limit = cpuidle_poll_time(drv, dev);
 
 		while (!need_resched()) {
-			cpu_relax();
+			kvm_cpu_poll_pause(limit);
 			if (loop_count++ < POLL_IDLE_RELAX_COUNT)
 				continue;
 
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index f4519d3689e1..810e749949b7 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -35,6 +35,7 @@
 #include <linux/interval_tree.h>
 #include <linux/rbtree.h>
 #include <linux/xarray.h>
+#include <linux/delay.h>
 #include <asm/signal.h>
 
 #include <linux/kvm.h>
@@ -2247,6 +2248,25 @@ static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu)
 }
 #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */
 
+/*
+ * This function is intended to replace the cpu_relax function in
+ * halt polling. If TPAUSE instruction is supported, use TPAUSE
+ * instead fo PAUSE to get better power saving and performance.
+ * Selecting 1 us is a compromise between scheduling latency and
+ * power saving time.
+ */
+static inline void kvm_cpu_poll_pause(u64 timeout_ns)
+{
+#ifdef CONFIG_X86
+	if (static_cpu_has(X86_FEATURE_WAITPKG) && timeout_ns > 1000)
+		udelay(1);
+	else
+		cpu_relax();
+#else
+	cpu_relax();
+#endif
+}
+
 /*
  * This defines how many reserved entries we want to keep before we
  * kick the vcpu to the userspace to avoid dirty ring full.  This
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 584a5bab3af3..4afa776d21bd 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3510,7 +3510,7 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu)
 			 */
 			if (kvm_vcpu_check_block(vcpu) < 0)
 				goto out;
-			cpu_relax();
+			kvm_cpu_poll_pause(vcpu->halt_poll_ns);
 			poll_end = cur = ktime_get();
 		} while (kvm_vcpu_can_poll(cur, stop));
 	}
-- 
2.34.1


             reply	other threads:[~2022-08-24  9:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24  9:11 Dapeng Mi [this message]
2022-08-24 14:08 ` [PATCH] KVM: x86: use TPAUSE to replace PAUSE in halt polling David Laight
2022-08-25 11:08   ` Mi, Dapeng1
2022-08-24 15:26 ` Sean Christopherson
2022-08-24 17:19   ` Paolo Bonzini
2022-08-25 12:16     ` Mi, Dapeng1
2022-08-25 11:31   ` Mi, Dapeng1
2022-08-26 10:14     ` Paolo Bonzini
2022-08-30  5:56       ` Mi, Dapeng1

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=20220824091117.767363-1-dapeng1.mi@intel.com \
    --to=dapeng1.mi@intel.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rafael@kernel.org \
    --cc=zhenyuw@linux.intel.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).