public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: David Daney <ddaney.cavm@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: Q: What is the struct kvm srcu protecting?
Date: Thu, 2 May 2013 22:48:36 -0300	[thread overview]
Message-ID: <20130503014836.GB8741@amt.cnet> (raw)
In-Reply-To: <5182AEFC.9070703@gmail.com>

On Thu, May 02, 2013 at 11:22:52AM -0700, David Daney wrote:
> Hi,
> 
> I am working on the MIPS KVM port, and am trying to figure out under
> which circumstances do I need to srcu_read_lock()/srcu_read_unlock()
> the kvm->srcu.

For x86: kvm->srcu protects memory slot information (kvm->memslots) and
in-kernel MMIO/PIO address->device structure mapping (kvm->buses).
Search for synchronize_srcu_expedited() in virt/kvm/ to locate the
updaters.

> I am looking at implementing something similar to arch/x86/kvm/x86.c
> at __msr_io(), where we see:
> 
> .
> .
> .
> 	idx = srcu_read_lock(&vcpu->kvm->srcu);
> 	for (i = 0; i < msrs->nmsrs; ++i)
> 		if (do_msr(vcpu, entries[i].index, &entries[i].data))
> 			break;
> 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
> .
> .
> .
> 
> Why is the srcu_read_lock() taken here?  I see no srcu_dereference()
> in the code path that would indicate the need for obtaining the
> lock.

        case KVM_GET_MSRS:
                r = msr_io(vcpu, argp, kvm_get_msr, 1);
                break;
        case KVM_SET_MSRS:
                r = msr_io(vcpu, argp, do_set_msr, 0);
                break;

to

int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
{
        return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
}

Somewhere down that path memslot information must be accessed.

> I have a feeling that I am missing some essential concept about the
> design of this code, but I don't know what it is.
> 
> Can someone explain what is happening here?

For x86 the usage is: 

VCPU CODE PATH
--------------

IOCTL(KVM_FD, KVM_VCPU_RUN)
ENTER-KERNEL
SRCU_READ_LOCK() 
	... large parte of vcpu context code performed with srcu lock held, 
	so that memory slot information can be used to access guest memory
	(gfn_to_memslot for example).
SRCU_READ_UNLOCK()
VMENTER
	while in guest mode srcu is not held so that updaters can make
	progress
VMEXIT
SRCU_READ_LOCK()
	back to vcpu context code

SRCU_READ_UNLOCK before return to userspace.

Also, when emulating HALT (kvm_vcpu_block) srcu is dropped.

UPDATERS
--------
See synchronize_srcu_expedited in virt/kvm/


  reply	other threads:[~2013-05-03  1:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-02 18:22 Q: What is the struct kvm srcu protecting? David Daney
2013-05-03  1:48 ` Marcelo Tosatti [this message]
2013-05-03 10:51   ` Gleb Natapov
2013-05-03 17:21     ` David Daney
2013-05-05  7:37       ` Gleb Natapov

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=20130503014836.GB8741@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=ddaney.cavm@gmail.com \
    --cc=kvm@vger.kernel.org \
    /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