linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: KarimAllah Ahmed <karahmed@amazon.de>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: "KarimAllah Ahmed" <karahmed@amazon.de>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>
Subject: [RFC 01/12] KVM: Introduce helper functions to map/unmap guest memory
Date: Mon,  5 Feb 2018 19:47:20 +0100	[thread overview]
Message-ID: <1517856451-2932-2-git-send-email-karahmed@amazon.de> (raw)
In-Reply-To: <1517856451-2932-1-git-send-email-karahmed@amazon.de>

Introduce helper functions to map and unmap guest memory into host kernel
memory. These helper functions support mapping guest memory both that are
managed by the host kernel (i.e. have a "struct page") and the ones that
are not managed by the kernel (i.e. does not have a "struct page").

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: KarimAllah Ahmed <karahmed@amazon.de>
---
 include/linux/kvm_host.h | 18 ++++++++++++++
 virt/kvm/kvm_main.c      | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 6bdd4b9..45d2854 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -204,6 +204,12 @@ enum {
 	READING_SHADOW_PAGE_TABLES,
 };
 
+struct kvm_host_mapping {
+	struct page *page;
+	void *kaddr;
+	kvm_pfn_t pfn;
+};
+
 /*
  * Sometimes a large or cross-page mmio needs to be broken up into separate
  * exits for userspace servicing.
@@ -700,6 +706,10 @@ struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
 struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn);
 kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn);
 kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
+bool kvm_vcpu_gfn_to_host_mapping(struct kvm_vcpu *vcpu, gfn_t gfn,
+				  struct kvm_host_mapping *mapping,
+				  bool kernel_access);
+void kvm_release_host_mapping(struct kvm_host_mapping *mapping, bool dirty);
 struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn);
 unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
 unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
@@ -990,6 +1000,14 @@ static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn)
 	return (hpa_t)pfn << PAGE_SHIFT;
 }
 
+static inline bool kvm_vcpu_gpa_to_host_mapping(struct kvm_vcpu *vcpu, gpa_t gpa,
+						struct kvm_host_mapping *mapping,
+						bool kernel_access)
+{
+	return kvm_vcpu_gfn_to_host_mapping(vcpu, gpa_to_gfn(gpa), mapping,
+					    kernel_access);
+}
+
 static inline struct page *kvm_vcpu_gpa_to_page(struct kvm_vcpu *vcpu,
 						gpa_t gpa)
 {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 210bf82..2b9f93d 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1653,6 +1653,68 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
 }
 EXPORT_SYMBOL_GPL(gfn_to_page);
 
+bool kvm_vcpu_gfn_to_host_mapping(struct kvm_vcpu *vcpu, gfn_t gfn,
+				  struct kvm_host_mapping *mapping,
+				  bool kernel_access)
+{
+	void *kaddr = NULL;
+	kvm_pfn_t pfn;
+	struct page *page = NULL;
+
+	pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
+
+	if (pfn_valid(pfn)) {
+		page = kvm_pfn_to_page(pfn);
+		if (is_error_page(page))
+			return false;
+
+		if (kernel_access) {
+			kaddr = kmap(page);
+			if (!kaddr)
+				return false;
+		}
+
+		mapping->kaddr = kaddr;
+		mapping->page = page;
+		mapping->pfn = pfn;
+		return true;
+	}
+
+	kaddr = memremap(pfn << PAGE_SHIFT, PAGE_SIZE, MEMREMAP_WB);
+	if (!kaddr)
+		return false;
+
+	mapping->page = NULL;
+	mapping->kaddr = kaddr;
+	mapping->pfn = pfn;
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_kaddr);
+
+void kvm_release_host_mapping(struct kvm_host_mapping *mapping, bool dirty)
+{
+	if (mapping->page) {
+		if (mapping->kaddr)
+			kunmap(mapping->page);
+
+		if (dirty)
+			kvm_release_page_dirty(mapping->page);
+		else
+			kvm_release_page_clean(mapping->page);
+	} else {
+		if (mapping->kaddr)
+			memunmap(mapping->kaddr);
+
+		if (dirty)
+			kvm_release_pfn_dirty(mapping->pfn);
+		else
+			kvm_release_pfn_clean(mapping->pfn);
+	}
+
+	memset(mapping, 0, sizeof(*mapping));
+}
+
 struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
 {
 	kvm_pfn_t pfn;
-- 
2.7.4

  reply	other threads:[~2018-02-05 18:51 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 18:47 [RFC 00/12] KVM/X86: Introduce a new guest mapping API KarimAllah Ahmed
2018-02-05 18:47 ` KarimAllah Ahmed [this message]
2018-02-05 18:47 ` [RFC 02/12] KVM/VMX: Use the new host mapping API for apic_access_page KarimAllah Ahmed
2018-02-05 19:27   ` Jim Mattson
2018-02-05 18:47 ` [RFC 03/12] KVM/VMX: Use the new host mapping API for virtual_apic_page KarimAllah Ahmed
2018-02-05 22:26   ` Jim Mattson
2018-02-09 12:15     ` KarimAllah Ahmed
2018-02-09 22:55       ` Jim Mattson
2018-02-05 18:47 ` [RFC 04/12] KVM/VMX: Use the new host mapping API for pi_desc_page KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 05/12] KVM/VMX: Use the new host mapping API for mapping nested vmptr KarimAllah Ahmed
2018-02-05 22:15   ` Jim Mattson
2018-02-08 18:51     ` KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 06/12] KVM/VMX: Use the new host mapping API for handle_vmptrld KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 07/12] KVM/VMX: Use the new host mapping API for mapping L12 MSR bitmap KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 08/12] KVM/VMX: Use the new host mapping API for mapping nested PML KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 09/12] KVM/VMX: Use the new host mapping API for cmpxchg_emulated KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 10/12] KVM/VMX: Use the new host mapping API for synic_clear_sint_msg_pending KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 11/12] KVM/VMX: Use the new host mapping API for synic_deliver_msg KarimAllah Ahmed
2018-02-05 18:47 ` [RFC 12/12] KVM/VMX: Remove kvm_vcpu_gpa_to_page as it is now unused KarimAllah Ahmed
2018-02-05 19:26 ` [RFC 00/12] KVM/X86: Introduce a new guest mapping API Mihai Donțu
2018-02-10 11:33   ` KarimAllah Ahmed
2018-02-16 11:40     ` Mihai Donțu

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=1517856451-2932-2-git-send-email-karahmed@amazon.de \
    --to=karahmed@amazon.de \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@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;
as well as URLs for NNTP newsgroup(s).