mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, dmatlack@google.com
Subject: Re: [PATCH] KVM: allow struct kvm to outlive the file descriptors
Date: Wed, 2 Mar 2022 17:47:25 +0000	[thread overview]
Message-ID: <Yh+trXegvWs+e5l3@google.com> (raw)
In-Reply-To: <20220302174321.326189-1-pbonzini@redhat.com>

On Wed, Mar 02, 2022, Paolo Bonzini wrote:
> 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);

This isn't sufficient.  For x86, it only grabs a reference to kvm.ko, not the
vendor module.  Instead, we can do:

	if (!try_module_get(kvm_chardev_ops.owner))
		return ERR_PTR(-EINVAL);

And then on top, revert commit revert ("KVM: set owner of cpu and vm file operations").
vCPUs file descriptors hold reference to the VM, which means they indirectly hold a
reference to the module. So once the "real" bug of struct kvm not holding a reference
to the module is fixed, grabbing a reference when a VM/vCPU inode is opened becomes
unnecessary.

  reply	other threads:[~2022-03-02 17:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-02 17:43 Paolo Bonzini
2022-03-02 17:47 ` Sean Christopherson [this message]
2022-03-02 17:48   ` Paolo Bonzini

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=Yh+trXegvWs+e5l3@google.com \
    --to=seanjc@google.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.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®