From: Quan Xu <quan.xu0@gmail.com>
To: pbonzini@redhat.com, rkrcmar@redhat.com
Cc: yang.zhang.wz@gmail.com, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Ben Luo <bn0418@gmail.com>,
Quan Xu <quan.xu0@gmail.com>
Subject: [PATCH RFC 2/7] kvm: x86: add a function to exchange value
Date: Fri, 8 Dec 2017 16:39:45 +0800 [thread overview]
Message-ID: <1512722390-3654-3-git-send-email-quan.xu0@gmail.com> (raw)
In-Reply-To: <1512722390-3654-1-git-send-email-quan.xu0@gmail.com>
From: Ben Luo <bn0418@gmail.com>
Introduce kvm_xchg_guest_cached to exchange value with guest
page atomically.
Signed-off-by: Yang Zhang <yang.zhang.wz@gmail.com>
Signed-off-by: Quan Xu <quan.xu0@gmail.com>
Signed-off-by: Ben Luo <bn0418@gmail.com>
---
include/linux/kvm_host.h | 3 +++
virt/kvm/kvm_main.c | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 6882538..32949ed 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -688,6 +688,9 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
void *data, int offset, unsigned long len);
int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
gpa_t gpa, unsigned long len);
+unsigned long kvm_xchg_guest_cached(struct kvm *kvm,
+ struct gfn_to_hva_cache *ghc, unsigned long offset,
+ unsigned long new, int size);
int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 9deb5a2..3149e17 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2010,6 +2010,48 @@ int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
}
EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
+unsigned long kvm_xchg_guest_cached(struct kvm *kvm,
+ struct gfn_to_hva_cache *ghc, unsigned long offset,
+ unsigned long new, int size)
+{
+ unsigned long r;
+ void *kva;
+ struct page *page;
+ kvm_pfn_t pfn;
+
+ WARN_ON(offset > ghc->len);
+
+ pfn = gfn_to_pfn_atomic(kvm, ghc->gpa >> PAGE_SHIFT);
+ page = kvm_pfn_to_page(pfn);
+
+ if (is_error_page(page))
+ return -EFAULT;
+
+ kva = kmap_atomic(page) + offset_in_page(ghc->gpa) + offset;
+ switch (size) {
+ case 1:
+ r = xchg((char *)kva, new);
+ break;
+ case 2:
+ r = xchg((short *)kva, new);
+ break;
+ case 4:
+ r = xchg((int *)kva, new);
+ break;
+ case 8:
+ r = xchg((long *)kva, new);
+ break;
+ default:
+ kunmap_atomic(kva);
+ return -EFAULT;
+ }
+
+ kunmap_atomic(kva);
+ mark_page_dirty_in_slot(ghc->memslot, ghc->gpa >> PAGE_SHIFT);
+
+ return r;
+}
+
int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
{
const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
--
1.7.1
next prev parent reply other threads:[~2017-12-08 8:40 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-08 8:39 [PATCH RFC 0/7] kvm pvtimer Quan Xu
2017-12-08 8:39 ` [PATCH RFC 1/7] kvm: x86: emulate MSR_KVM_PV_TIMER_EN MSR Quan Xu
2017-12-08 8:39 ` Quan Xu [this message]
2017-12-08 8:39 ` [PATCH RFC 3/7] KVM: timer: synchronize tsc-deadline timestamp for guest Quan Xu
2017-12-08 15:06 ` Konrad Rzeszutek Wilk
2017-12-14 1:54 ` Quan Xu
2017-12-08 8:39 ` [PATCH RFC 4/7] KVM: timer: program timer to a dedicated CPU Quan Xu
2017-12-08 8:39 ` [PATCH RFC 5/7] KVM: timer: ignore timer migration if pvtimer is enabled Quan Xu
2017-12-08 8:39 ` [PATCH RFC 6/7] Doc/KVM: introduce a new cpuid bit for kvm pvtimer Quan Xu
2017-12-08 8:39 ` [PATCH RFC 7/7] kvm: guest: reprogram guest timer Quan Xu
2017-12-08 15:10 ` [PATCH RFC 0/7] kvm pvtimer Konrad Rzeszutek Wilk
[not found] ` <CAFv8KnF1Re7Zn2LVvqh=Sr8MmaWJJwZBB1i5ws03GvPVKXRjzA@mail.gmail.com>
2017-12-13 16:28 ` Konrad Rzeszutek Wilk
2017-12-14 2:32 ` Quan Xu
2017-12-14 11:56 ` Paolo Bonzini
2017-12-14 12:06 ` Quan Xu
2017-12-14 13:00 ` 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=1512722390-3654-3-git-send-email-quan.xu0@gmail.com \
--to=quan.xu0@gmail.com \
--cc=bn0418@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=rkrcmar@redhat.com \
--cc=yang.zhang.wz@gmail.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®