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, x86@kernel.org
Cc: "KarimAllah Ahmed" <karahmed@amazon.de>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Radim Krčmář" <rkrcmar@redhat.com>,
	"Borislav Petkov" <bp@suse.de>,
	"Denys Vlasenko" <dvlasenk@redhat.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Toshi Kani" <toshi.kani@hpe.com>,
	"Tony Luck" <tony.luck@intel.com>
Subject: [PATCH] kvm, x86: Properly check whether a pfn is an MMIO or not
Date: Wed, 22 Jun 2016 04:34:26 +0200	[thread overview]
Message-ID: <1466562866-31524-1-git-send-email-karahmed@amazon.de> (raw)

pfn_valid check is not sufficient because it only checks if a page has a struct
page or not, if for example "mem=" was passed to the kernel some valid pages
won't have a struct page. This means that if guests were assigned valid memory
that lies after the mem= boundary it will be passed uncached to the guest no
matter what the guest caching attributes are for this memory.

Use the original e820 map to check whether a certain pfn belongs to RAM or not.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Toshi Kani <toshi.kani@hpe.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org
Cc: x86@kernel.org
Signed-off-by: KarimAllah Ahmed <karahmed@amazon.de>
---
 arch/x86/include/asm/e820.h |  1 +
 arch/x86/kernel/e820.c      | 18 ++++++++++++++++++
 arch/x86/kvm/mmu.c          |  2 +-
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/e820.h b/arch/x86/include/asm/e820.h
index 3ab0537..2d4f7d8 100644
--- a/arch/x86/include/asm/e820.h
+++ b/arch/x86/include/asm/e820.h
@@ -16,6 +16,7 @@ extern struct e820map e820_saved;
 extern unsigned long pci_mem_start;
 extern int e820_any_mapped(u64 start, u64 end, unsigned type);
 extern int e820_all_mapped(u64 start, u64 end, unsigned type);
+extern bool e820_is_ram(u64 addr);
 extern void e820_add_region(u64 start, u64 size, int type);
 extern void e820_print_map(char *who);
 extern int
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 621b501..387cdba 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -105,6 +105,24 @@ int __init e820_all_mapped(u64 start, u64 end, unsigned type)
 	return 0;
 }
 
+bool
+e820_is_ram(u64 addr)
+{
+	int i;
+
+	for (i = 0; i < e820_saved.nr_map; i++) {
+		struct e820entry *ei = &e820_saved.map[i];
+
+		if (ei->type != E820_RAM)
+			continue;
+		if ((addr >= ei->addr) && (addr < (ei->addr + ei->size)))
+			return true;
+	}
+
+	return false;
+}
+EXPORT_SYMBOL_GPL(e820_is_ram);
+
 /*
  * Add a memory region to the kernel e820 map.
  */
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 24e8001..5e07bf5 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -2507,7 +2507,7 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
 	if (pfn_valid(pfn))
 		return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn));
 
-	return true;
+	return !e820_is_ram(pfn << PAGE_SHIFT);
 }
 
 static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
-- 
2.8.2

             reply	other threads:[~2016-06-22  2:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-22  2:34 KarimAllah Ahmed [this message]
2016-06-22 13:41 ` [PATCH] kvm, x86: Properly check whether a pfn is an MMIO or not Paolo Bonzini
2016-06-22 14:11   ` Raslan, KarimAllah
2016-09-15  5:54   ` Raslan, KarimAllah
2016-09-15  6:22     ` Paolo Bonzini
2016-09-15 16:04       ` Kani, Toshimitsu
2016-09-15 16:22         ` 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=1466562866-31524-1-git-send-email-karahmed@amazon.de \
    --to=karahmed@amazon.de \
    --cc=akpm@linux-foundation.org \
    --cc=bp@suse.de \
    --cc=dvlasenk@redhat.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=toshi.kani@hpe.com \
    --cc=x86@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;
as well as URLs for NNTP newsgroup(s).