All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wanpeng Li <kernellwp@gmail.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>
Subject: [PATCH] KVM: x86/xen: Take srcu lock when accessing kvm_memslots()
Date: Fri, 23 Apr 2021 15:11:23 +0800	[thread overview]
Message-ID: <1619161883-5963-1-git-send-email-wanpengli@tencent.com> (raw)

From: Wanpeng Li <wanpengli@tencent.com>

kvm_memslots() will be called by kvm_write_guest_offset_cached() so 
take the srcu lock.

Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/xen.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index ae17250..d0df782 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -96,6 +96,7 @@ void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
 	struct kvm_vcpu_xen *vx = &v->arch.xen;
 	uint64_t state_entry_time;
 	unsigned int offset;
+	int idx;
 
 	kvm_xen_update_runstate(v, state);
 
@@ -133,10 +134,16 @@ void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
 	BUILD_BUG_ON(sizeof(((struct compat_vcpu_runstate_info *)0)->state_entry_time) !=
 		     sizeof(state_entry_time));
 
+	/*
+	 * Take the srcu lock as memslots will be accessed to check the gfn
+	 * cache generation against the memslots generation.
+	 */
+	idx = srcu_read_lock(&v->kvm->srcu);
+
 	if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
 					  &state_entry_time, offset,
 					  sizeof(state_entry_time)))
-		return;
+		goto out;
 	smp_wmb();
 
 	/*
@@ -154,7 +161,7 @@ void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
 					  &vx->current_runstate,
 					  offsetof(struct vcpu_runstate_info, state),
 					  sizeof(vx->current_runstate)))
-		return;
+		goto out;
 
 	/*
 	 * Write the actual runstate times immediately after the
@@ -173,7 +180,7 @@ void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
 					  &vx->runstate_times[0],
 					  offset + sizeof(u64),
 					  sizeof(vx->runstate_times)))
-		return;
+		goto out;
 
 	smp_wmb();
 
@@ -186,7 +193,10 @@ void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
 	if (kvm_write_guest_offset_cached(v->kvm, &v->arch.xen.runstate_cache,
 					  &state_entry_time, offset,
 					  sizeof(state_entry_time)))
-		return;
+		goto out;
+
+out:
+	srcu_read_unlock(&v->kvm->srcu, idx);
 }
 
 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
-- 
2.7.4


             reply	other threads:[~2021-04-23  7:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23  7:11 Wanpeng Li [this message]
2021-04-23  8:13 ` [PATCH] KVM: x86/xen: Take srcu lock when accessing kvm_memslots() Paolo Bonzini
2021-04-23  8:15   ` Wanpeng Li

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=1619161883-5963-1-git-send-email-wanpengli@tencent.com \
    --to=kernellwp@gmail.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.