2024-02-03 11:02:32

by Zhao Liu

[permalink] [raw]
Subject: [RFC 15/26] KVM: VMX: Sync update of Host HFI table to Guest

From: Zhuocheng Ding <[email protected]>

The HFI table could be updated via thermal interrupt as the actual
operating conditions of the processor change during runtime [1], so it
is required to synchronize hardware hint changes to Guest's HFI table in
time.

Provide the interfaces to register/unregister the Host's HFI update,
and in the callback of the notification, make HFI update request to
update Guest's HFI table before entering Guest.

[1]: SDM, vol. 3B, section 15.6.7 Hardware Feedback Interface and Intel
Thread Director Structure Dynamic Update

Tested-by: Yanting Jiang <[email protected]>
Signed-off-by: Zhuocheng Ding <[email protected]>
Co-developed-by: Zhao Liu <[email protected]>
Signed-off-by: Zhao Liu <[email protected]>
---
arch/x86/kvm/vmx/vmx.c | 59 ++++++++++++++++++++++++++++++++++++++++++
arch/x86/kvm/vmx/vmx.h | 8 ++++++
2 files changed, 67 insertions(+)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 93c47ba0817b..0ad5e3473a28 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1651,6 +1651,61 @@ static void vmx_update_hfi_table(struct kvm *kvm)
vmx_inject_therm_interrupt(kvm_get_vcpu(kvm, 0));
}

+static void vmx_hfi_notifier_register(struct kvm *kvm)
+{
+ struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
+ struct hfi_desc *kvm_vmx_hfi = &kvm_vmx->pkg_therm.hfi_desc;
+
+ if (!intel_hfi_enabled())
+ return;
+
+ if (!vmx_hfi_initialized(kvm_vmx))
+ return;
+
+ if (kvm_vmx_hfi->has_hfi_instance)
+ return;
+
+ /*
+ * HFI/ITD virtualization is supported on the platforms with only
+ * 1 HFI instance. Just register notifier for vCPU 0.
+ */
+ kvm_vmx_hfi->has_hfi_instance =
+ !intel_hfi_notifier_register(&kvm_vmx_hfi->hfi_nb,
+ kvm_get_vcpu(kvm, 0)->cpu);
+}
+
+static void vmx_hfi_notifier_unregister(struct kvm *kvm)
+{
+ struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
+ struct hfi_desc *kvm_vmx_hfi = &kvm_vmx->pkg_therm.hfi_desc;
+
+ if (!kvm_vmx_hfi->has_hfi_instance)
+ return;
+
+ intel_hfi_notifier_unregister(&kvm_vmx_hfi->hfi_nb,
+ kvm_get_vcpu(kvm, 0)->cpu);
+ kvm_vmx_hfi->has_hfi_instance = false;
+}
+
+static int vmx_hfi_update_notify(struct notifier_block *nb,
+ unsigned long code, void *data)
+{
+ struct hfi_desc *kvm_vmx_hfi;
+ struct kvm *kvm;
+
+ kvm_vmx_hfi = container_of(nb, struct hfi_desc, hfi_nb);
+ kvm = &kvm_vmx_hfi->vmx->kvm;
+
+ /*
+ * Don't need to check if vcpu 0 belongs to
+ * kvm_vmx_hfi->host_hfi_instance since currently ITD/HFI
+ * virtualization is only supported for client platforms
+ * (with only one HFI instance).
+ */
+ kvm_make_request(KVM_REQ_HFI_UPDATE, kvm_get_vcpu(kvm, 0));
+ return NOTIFY_OK;
+}
+
static void vmx_dynamic_update_hfi_table(struct kvm_vcpu *vcpu)
{
struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
@@ -7871,8 +7926,11 @@ static int vmx_vcpu_create(struct kvm_vcpu *vcpu)
static int vmx_vm_init_pkg_therm(struct kvm *kvm)
{
struct pkg_therm_desc *pkg_therm = &to_kvm_vmx(kvm)->pkg_therm;
+ struct hfi_desc *kvm_vmx_hfi = &pkg_therm->hfi_desc;

mutex_init(&pkg_therm->pkg_therm_lock);
+ kvm_vmx_hfi->hfi_nb.notifier_call = vmx_hfi_update_notify;
+ kvm_vmx_hfi->vmx = to_kvm_vmx(kvm);
return 0;
}

@@ -8542,6 +8600,7 @@ static void vmx_vm_destroy(struct kvm *kvm)
struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
struct hfi_desc *kvm_vmx_hfi = &kvm_vmx->pkg_therm.hfi_desc;

+ vmx_hfi_notifier_unregister(kvm);
kfree(kvm_vmx_hfi->hfi_table.base_addr);
free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
}
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 63874aad7ae3..ff205bc0e99a 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -395,6 +395,11 @@ struct vcpu_vmx {
* the vCPU is running on.
* When KVM updates the Guest's HFI table, it writes the local
* virtual HFI table to the Guest HFI table memory in @table_base.
+ * @has_hfi_instance: Flag indicates if VM registers @hfi_nb on Host's HFI instance.
+ * @hfi_nb: Notifier block to be registered in Host HFI instance.
+ * @vmx: Points to the kvm_vmx where the current nb is located.
+ * Used to get the corresponding kvm_vmx of the nb when it
+ * is executed.
*
* A set of status flags and feature information, used to maintain local virtual HFI table
* and sync updates to Guest HFI table.
@@ -409,6 +414,9 @@ struct hfi_desc {
gpa_t table_base;
struct hfi_features hfi_features;
struct hfi_table hfi_table;
+ bool has_hfi_instance;
+ struct notifier_block hfi_nb;
+ struct kvm_vmx *vmx;
};

struct pkg_therm_desc {
--
2.34.1