[PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted

isaku.yamahata@intel.com posted 26 patches 3 years, 6 months ago
There is a newer version of this series
[PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted
Posted by isaku.yamahata@intel.com 3 years, 6 months ago
From: Isaku Yamahata <isaku.yamahata@intel.com>

and pass kvm_usage_count with kvm_lock.  Move kvm_arch_post_init_vm() under
kvm_arch_add_vm().  Later kvm_arch_post_init_vm() is deleted once x86
overrides kvm_arch_add_vm().

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
---
 include/linux/kvm_host.h |  2 ++
 virt/kvm/kvm_arch.c      | 12 +++++++++++-
 virt/kvm/kvm_main.c      | 21 +++++++++++++++++----
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index dd2a6d98d4de..f78364e01ca9 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1445,6 +1445,8 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu);
 bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);
 int kvm_arch_post_init_vm(struct kvm *kvm);
+int kvm_arch_add_vm(struct kvm *kvm, int usage_count);
+int kvm_arch_del_vm(int usage_count);
 void kvm_arch_pre_destroy_vm(struct kvm *kvm);
 int kvm_arch_create_vm_debugfs(struct kvm *kvm);
 
diff --git a/virt/kvm/kvm_arch.c b/virt/kvm/kvm_arch.c
index 4748a76bcb03..0eac996f4981 100644
--- a/virt/kvm/kvm_arch.c
+++ b/virt/kvm/kvm_arch.c
@@ -10,11 +10,21 @@
 
 #include <linux/kvm_host.h>
 
+__weak int kvm_arch_post_init_vm(struct kvm *kvm)
+{
+	return 0;
+}
+
 /*
  * Called after the VM is otherwise initialized, but just before adding it to
  * the vm_list.
  */
-__weak int kvm_arch_post_init_vm(struct kvm *kvm)
+__weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count)
+{
+	return kvm_arch_post_init_vm(kvm);
+}
+
+__weak int kvm_arch_del_vm(int usage_count)
 {
 	return 0;
 }
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 951f853f6ac9..7acc35e279ec 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -145,6 +145,7 @@ static int kvm_no_compat_open(struct inode *inode, struct file *file)
 static int hardware_enable_all(void);
 static void hardware_disable_all(void);
 static void hardware_disable_nolock(void *junk);
+static void kvm_del_vm(void);
 
 static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
 
@@ -1215,11 +1216,12 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 	if (r)
 		goto out_err_no_debugfs;
 
-	r = kvm_arch_post_init_vm(kvm);
-	if (r)
-		goto out_err;
-
 	mutex_lock(&kvm_lock);
+	r = kvm_arch_add_vm(kvm, kvm_usage_count);
+	if (r) {
+		mutex_unlock(&kvm_lock);
+		goto out_err;
+	}
 	list_add(&kvm->vm_list, &vm_list);
 	mutex_unlock(&kvm_lock);
 
@@ -1239,6 +1241,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 #endif
 out_err_no_mmu_notifier:
 	hardware_disable_all();
+	kvm_del_vm();
 out_err_no_disable:
 	kvm_arch_destroy_vm(kvm);
 out_err_no_arch_destroy_vm:
@@ -1319,6 +1322,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	kvm_arch_free_vm(kvm);
 	preempt_notifier_dec();
 	hardware_disable_all();
+	kvm_del_vm();
 	mmdrop(mm);
 	module_put(kvm_chardev_ops.owner);
 }
@@ -5096,6 +5100,15 @@ static void hardware_disable_all(void)
 	cpus_read_unlock();
 }
 
+static void kvm_del_vm(void)
+{
+	cpus_read_lock();
+	mutex_lock(&kvm_lock);
+	kvm_arch_del_vm(kvm_usage_count);
+	mutex_unlock(&kvm_lock);
+	cpus_read_unlock();
+}
+
 static int hardware_enable_all(void)
 {
 	int r = 0;
-- 
2.25.1
Re: [PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted
Posted by Chao Gao 3 years, 6 months ago
On Thu, Sep 08, 2022 at 04:25:29PM -0700, isaku.yamahata@intel.com wrote:
>+__weak int kvm_arch_post_init_vm(struct kvm *kvm)

int __weak

>+{
>+	return 0;
>+}
>+
> /*
>  * Called after the VM is otherwise initialized, but just before adding it to
>  * the vm_list.
>  */
>-__weak int kvm_arch_post_init_vm(struct kvm *kvm)
>+__weak int kvm_arch_add_vm(struct kvm *kvm, int usage_count)
>+{
>+	return kvm_arch_post_init_vm(kvm);
>+}
>+
>+__weak int kvm_arch_del_vm(int usage_count)

ditto

>+static void kvm_del_vm(void)
>+{
>+	cpus_read_lock();
>+	mutex_lock(&kvm_lock);
>+	kvm_arch_del_vm(kvm_usage_count);
>+	mutex_unlock(&kvm_lock);
>+	cpus_read_unlock();

kvm_arch_del_vm() is an empty function now. I don't understand why
cpus_read_lock() is needed. Maybe add the lock when you fill up
kvm_arch_del_vm().
Re: [PATCH v4 13/26] KVM: Add arch hook when VM is added/deleted
Posted by Isaku Yamahata 3 years, 6 months ago
On Fri, Sep 09, 2022 at 02:08:04PM +0800,
Chao Gao <chao.gao@intel.com> wrote:

> >+static void kvm_del_vm(void)
> >+{
> >+	cpus_read_lock();
> >+	mutex_lock(&kvm_lock);
> >+	kvm_arch_del_vm(kvm_usage_count);
> >+	mutex_unlock(&kvm_lock);
> >+	cpus_read_unlock();
> 
> kvm_arch_del_vm() is an empty function now. I don't understand why
> cpus_read_lock() is needed. Maybe add the lock when you fill up
> kvm_arch_del_vm().

The next patch populates it with on_each_cpu(hardware_disable_nolock, NULL, 1);
I will move adding lock to the next one. Or revise how adding arch hooks and
populating part.
-- 
Isaku Yamahata <isaku.yamahata@gmail.com>