mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anthony Harivel <aharivel@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, seanjc@google.com,
	linux-kernel@vger.kernel.org,
	Anthony Harivel <aharivel@redhat.com>
Subject: [PATCH RFC v3 2/3] KVM: x86: add KVM_CAP_CSTATE_POLICY for per-VM C-state enforcement
Date: Mon, 14 Sep 2026 16:37:01 +0200	[thread overview]
Message-ID: <20260914143702.915401-3-aharivel@redhat.com> (raw)
In-Reply-To: <20260914143702.915401-1-aharivel@redhat.com>

Add a new VM-scoped capability that allows userspace to set a maximum
C-state ceiling for host cpuidle when vCPUs halt.

When a vCPU enters kvm_vcpu_block(), KVM temporarily disables cpuidle
states deeper than max_cstate on the current pCPU using the existing
states_usage[].disable mechanism (CPUIDLE_STATE_DISABLED_BY_DRIVER).
After wakeup, the original disable flags are restored.

The capability follows the same pattern as KVM_CAP_HALT_POLL:
  - VM-scoped ioctl via KVM_ENABLE_CAP
  - args[0] = max_cstate (-1 to 6, -1 disables the policy)
  - Re-callable at runtime without VM restart
  - Memory ordering via smp_wmb/rmb

This fills an operational gap for NFV and latency-sensitive deployments
where the host operator needs per-VM control over idle depth without
requiring guest cooperation. The enforcement is scoped to pinned-core
configurations where the disable flags do not race with other tasks.

Measured VM exit overhead (Intel Xeon, ftrace):
  Median: 867 ns — negligible relative to C-state exit latencies
  (C1E: 10us, C3: 33us, C6: 133us).

Signed-off-by: Anthony Harivel <aharivel@redhat.com>
---
 include/linux/kvm_host.h |   2 +
 include/uapi/linux/kvm.h |   1 +
 virt/kvm/kvm_main.c      | 101 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index ab8cfaec82d3..3eca172e62ff 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -863,6 +863,8 @@ struct kvm {
 	pid_t userspace_pid;
 	bool override_halt_poll_ns;
 	unsigned int max_halt_poll_ns;
+	bool override_cstate_policy;
+	int max_cstate;
 	u32 dirty_ring_size;
 	bool dirty_ring_with_bitmap;
 	bool vm_bugged;
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 70e36e6a0ad4..559d23e01127 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -998,6 +998,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_VSIE_ESAMODE 248
 #define KVM_CAP_S390_HPAGE_2G 249
 #define KVM_CAP_PPC_COMPAT_CAPS 250
+#define KVM_CAP_CSTATE_POLICY 251
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 45e784462ec6..306891a2c135 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -48,6 +48,7 @@
 #include <linux/lockdep.h>
 #include <linux/kthread.h>
 #include <linux/suspend.h>
+#include <linux/cpuidle.h>
 #include <linux/rseq.h>
 
 #include <asm/processor.h>
@@ -3635,6 +3636,79 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
 	return ret;
 }
 
+/*
+ * Temporarily disable cpuidle states deeper than max_cstate on the current
+ * CPU. Returns the CPU number where the constraint was applied, or -1 if
+ * no constraint was needed. Must be called with preempt disabled.
+ *
+ * Only correct when the vCPU is pinned to this pCPU — with overcommit,
+ * multiple VMs could race on the same per-CPU disable flags.
+ */
+static int kvm_cstate_policy_apply(struct kvm *kvm,
+				   unsigned long long saved_disable[CPUIDLE_STATE_MAX])
+{
+	struct cpuidle_device *dev;
+	struct cpuidle_driver *drv;
+	int cpu, i;
+
+	if (!kvm->override_cstate_policy)
+		return -1;
+
+	smp_rmb();
+
+	if (kvm->max_cstate < 0)
+		return -1;
+
+	cpu = smp_processor_id();
+	dev = cpuidle_get_device();
+	drv = cpuidle_get_driver();
+	if (!dev || !drv)
+		return -1;
+
+	for (i = 0; i < drv->state_count; i++) {
+		saved_disable[i] = dev->states_usage[i].disable;
+		if (i > kvm->max_cstate)
+			dev->states_usage[i].disable |=
+				CPUIDLE_STATE_DISABLED_BY_DRIVER;
+	}
+
+	return cpu;
+}
+
+static void kvm_cstate_policy_clear(int saved_cpu,
+				    unsigned long long saved_disable[CPUIDLE_STATE_MAX])
+{
+	struct cpuidle_device *dev;
+	struct cpuidle_driver *drv;
+	int i, cur_cpu;
+
+	if (saved_cpu < 0)
+		return;
+
+	cur_cpu = smp_processor_id();
+	if (cur_cpu != saved_cpu) {
+		/*
+		 * vCPU migrated during block — we cannot safely restore the
+		 * original CPU's cpuidle state from here. This only happens
+		 * with unpinned vCPUs. The stale disable flags will persist
+		 * until the next policy application on that CPU clears them.
+		 *
+		 * TODO: use smp_call_function_single() to restore remotely,
+		 * or scope the feature to pinned vCPUs only.
+		 */
+		WARN_ON_ONCE(1);
+		return;
+	}
+
+	dev = cpuidle_get_device();
+	drv = cpuidle_get_driver();
+	if (!dev || !drv)
+		return;
+
+	for (i = 0; i < drv->state_count; i++)
+		dev->states_usage[i].disable = saved_disable[i];
+}
+
 /*
  * Block the vCPU until the vCPU is runnable, an event arrives, or a signal is
  * pending.  This is mostly used when halting a vCPU, but may also be used
@@ -3643,11 +3717,14 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
 bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
 {
 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
+	unsigned long long saved_disable[CPUIDLE_STATE_MAX] = {};
+	int saved_cpu = -1;
 	bool waited = false;
 
 	vcpu->stat.generic.blocking = 1;
 
 	preempt_disable();
+	saved_cpu = kvm_cstate_policy_apply(vcpu->kvm, saved_disable);
 	kvm_arch_vcpu_blocking(vcpu);
 	prepare_to_rcuwait(wait);
 	preempt_enable();
@@ -3665,6 +3742,7 @@ bool kvm_vcpu_block(struct kvm_vcpu *vcpu)
 	preempt_disable();
 	finish_rcuwait(wait);
 	kvm_arch_vcpu_unblocking(vcpu);
+	kvm_cstate_policy_clear(saved_cpu, saved_disable);
 	preempt_enable();
 
 	vcpu->stat.generic.blocking = 0;
@@ -4880,6 +4958,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_CHECK_EXTENSION_VM:
 	case KVM_CAP_ENABLE_CAP_VM:
 	case KVM_CAP_HALT_POLL:
+	case KVM_CAP_CSTATE_POLICY:
 		return 1;
 #ifdef CONFIG_KVM_MMIO
 	case KVM_CAP_COALESCED_MMIO:
@@ -5056,6 +5135,28 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
 
 		return 0;
 	}
+	case KVM_CAP_CSTATE_POLICY: {
+		int max_cstate = (int)cap->args[0];
+
+		if (cap->flags || cap->args[1])
+			return -EINVAL;
+
+		if (max_cstate < -1 || max_cstate > 6)
+			return -EINVAL;
+
+		kvm->max_cstate = max_cstate;
+
+		/*
+		 * Ensure kvm->override_cstate_policy does not become visible
+		 * before kvm->max_cstate.
+		 *
+		 * Pairs with the smp_rmb() in kvm_vcpu_block().
+		 */
+		smp_wmb();
+		kvm->override_cstate_policy = true;
+
+		return 0;
+	}
 	case KVM_CAP_DIRTY_LOG_RING:
 	case KVM_CAP_DIRTY_LOG_RING_ACQ_REL:
 		if (!kvm_vm_ioctl_check_extension_generic(kvm, cap->cap))
-- 
2.55.0


  parent reply	other threads:[~2026-09-14 14:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14 14:36 [PATCH RFC v3 0/3] KVM: x86: per-VM C-state policy enforcement (KVM_CAP_CSTATE_POLICY) Anthony Harivel
2026-09-14 14:37 ` [PATCH RFC v3 1/3] cpuidle: export cpuidle_devices for KVM C-state policy enforcement Anthony Harivel
2026-09-14 14:37 ` Anthony Harivel [this message]
2026-09-14 15:23   ` [PATCH RFC v3 2/3] KVM: x86: add KVM_CAP_CSTATE_POLICY for per-VM C-state enforcement Sean Christopherson
2026-09-14 14:37 ` [PATCH RFC v3 3/3] KVM: selftests: add cstate_policy_test for KVM_CAP_CSTATE_POLICY Anthony Harivel

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=20260914143702.915401-3-aharivel@redhat.com \
    --to=aharivel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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

all inboxes | Powered by JetHome®