mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] KVM: allow struct kvm to outlive the file descriptors
@ 2022-03-02 17:43 Paolo Bonzini
  2022-03-02 17:47 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2022-03-02 17:43 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: dmatlack, seanjc

Right now, the kvm module is kept alive by VFS via fops_get/fops_put, but there
may be cases in which a kvm_get_kvm's matching kvm_put_kvm happens after
the file descriptor is closed.  One case that will be introduced soon is
when work is delegated to the system work queue; the worker might be
a bit late and the file descriptor can be closed in the meantime.  Ensure
that the module has not gone away by tying a module reference explicitly
to the lifetime of the struct kvm.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 64eb99444688..e3f37fc2ebf1 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1131,6 +1131,9 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	preempt_notifier_inc();
 	kvm_init_pm_notifier(kvm);
 
+	/* This is safe, since we have a reference from open(). */
+	__module_get(THIS_MODULE);
+
 	return kvm;
 
 out_err:
@@ -1220,6 +1223,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	preempt_notifier_dec();
 	hardware_disable_all();
 	mmdrop(mm);
+	module_put(THIS_MODULE);
 }
 
 void kvm_get_kvm(struct kvm *kvm)
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-02 17:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-02 17:43 [PATCH] KVM: allow struct kvm to outlive the file descriptors Paolo Bonzini
2022-03-02 17:47 ` Sean Christopherson
2022-03-02 17:48   ` Paolo Bonzini

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®