diff mbox

kvm, x86: Properly check whether a pfn is an MMIO or not

Message ID 1466562866-31524-1-git-send-email-karahmed@amazon.de (mailing list archive)
State New, archived
Headers show

Commit Message

KarimAllah Ahmed June 22, 2016, 2:34 a.m. UTC
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(-)

Comments

Paolo Bonzini June 22, 2016, 1:41 p.m. UTC | #1
On 22/06/2016 04:34, KarimAllah Ahmed wrote:
> 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.

How can you pass memory after the mem= boundary to the guest?

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
KarimAllah Ahmed June 22, 2016, 2:11 p.m. UTC | #2
On 6/22/16, 3:41 PM, "Paolo Bonzini" <pbonzini@redhat.com> wrote:

>

>

>On 22/06/2016 04:34, KarimAllah Ahmed wrote:

>> 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.

>

>How can you pass memory after the mem= boundary to the guest?


Memory regions can be assigned to the guest as long as you've a valid host
virtual address. You can always get a host virtual address for any RAM that
lives after 'mem=' for example by simply mmaping /dev/mem or even using some
special kernel module to mmap these regions from user space.

>

>Paolo

>
KarimAllah Ahmed Sept. 15, 2016, 5:54 a.m. UTC | #3
> On Jun 22, 2016, at 3:41 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> 
> 
> 
> On 22/06/2016 04:34, KarimAllah Ahmed wrote:
>> 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.
> 
> How can you pass memory after the mem= boundary to the guest?

Does my previous reply answer your question?

> 
> Paolo
> 

Amazon Development Center Germany GmbH
Berlin - Dresden - Aachen
main office: Krausenstr. 38, 10117 Berlin
Geschaeftsfuehrer: Dr. Ralf Herbrich, Christian Schlaeger
Ust-ID: DE289237879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Paolo Bonzini Sept. 15, 2016, 6:22 a.m. UTC | #4
On 15/09/2016 07:54, Raslan, KarimAllah wrote:
> 
>> On Jun 22, 2016, at 3:41 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>>
>>
>>
>> On 22/06/2016 04:34, KarimAllah Ahmed wrote:
>>> 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.
>>
>> How can you pass memory after the mem= boundary to the guest?
> 
> Does my previous reply answer your question?

Yes, but I'm not sure it's the right way to do it.  Looking at the e820
memory map seems pretty hacky.

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Kani, Toshi Sept. 15, 2016, 4:04 p.m. UTC | #5
T24gVGh1LCAyMDE2LTA5LTE1IGF0IDA4OjIyICswMjAwLCBQYW9sbyBCb256aW5pIHdyb3RlOg0K
PiANCj4gT24gMTUvMDkvMjAxNiAwNzo1NCwgUmFzbGFuLCBLYXJpbUFsbGFoIHdyb3RlOg0KPiA+
IA0KPiA+IA0KPiA+ID4gDQo+ID4gPiBPbiBKdW4gMjIsIDIwMTYsIGF0IDM6NDEgUE0sIFBhb2xv
IEJvbnppbmkgPHBib256aW5pQHJlZGhhdC5jb20+DQo+ID4gPiB3cm90ZToNCj4gPiA+IA0KPiA+
ID4gDQo+ID4gPiANCj4gPiA+IE9uIDIyLzA2LzIwMTYgMDQ6MzQsIEthcmltQWxsYWggQWhtZWQg
d3JvdGU6DQo+ID4gPiA+IA0KPiA+ID4gPiBwZm5fdmFsaWQgY2hlY2sgaXMgbm90IHN1ZmZpY2ll
bnQgYmVjYXVzZSBpdCBvbmx5IGNoZWNrcyBpZiBhDQo+ID4gPiA+IHBhZ2UgaGFzIGEgc3RydWN0
IHBhZ2Ugb3Igbm90LCBpZiBmb3IgZXhhbXBsZSAibWVtPSIgd2FzIHBhc3NlZA0KPiA+ID4gPiB0
byB0aGUga2VybmVsIHNvbWUgdmFsaWQgcGFnZXMgd29uJ3QgaGF2ZSBhIHN0cnVjdCBwYWdlLiBU
aGlzDQo+ID4gPiA+IG1lYW5zIHRoYXQgaWYgZ3Vlc3RzIHdlcmUgYXNzaWduZWQgdmFsaWQgbWVt
b3J5IHRoYXQgbGllcyBhZnRlcg0KPiA+ID4gPiB0aGUgbWVtPSBib3VuZGFyeSBpdCB3aWxsIGJl
IHBhc3NlZCB1bmNhY2hlZCB0byB0aGUgZ3Vlc3Qgbm8NCj4gPiA+ID4gbWF0dGVyIHdoYXQgdGhl
IGd1ZXN0IGNhY2hpbmcgYXR0cmlidXRlcyBhcmUgZm9yIHRoaXMgbWVtb3J5Lg0KPiA+ID4gDQo+
ID4gPiBIb3cgY2FuIHlvdSBwYXNzIG1lbW9yeSBhZnRlciB0aGUgbWVtPSBib3VuZGFyeSB0byB0
aGUgZ3Vlc3Q/DQo+ID4gDQo+ID4gRG9lcyBteSBwcmV2aW91cyByZXBseSBhbnN3ZXIgeW91ciBx
dWVzdGlvbj8NCj4gDQo+IFllcywgYnV0IEknbSBub3Qgc3VyZSBpdCdzIHRoZSByaWdodCB3YXkg
dG8gZG8gaXQuwqDCoExvb2tpbmcgYXQgdGhlDQo+IGU4MjAgbWVtb3J5IG1hcCBzZWVtcyBwcmV0
dHkgaGFja3kuDQoNCkNhbiB5b3UgdXNlwqByZWdpb25faW50ZXJzZWN0cygpIGZvciB0aGlzIGNo
ZWNrPyDCoFNlZcKgbWVtcmVtYXAoKSBhcyBhbg0KZXhhbXBsZSB1c2luZyB0aGlzIGludGVyZmFj
ZS4NCg0KLVRvc2hp
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Paolo Bonzini Sept. 15, 2016, 4:22 p.m. UTC | #6
On 15/09/2016 18:04, Kani, Toshimitsu wrote:
> On Thu, 2016-09-15 at 08:22 +0200, Paolo Bonzini wrote:
>>
>> On 15/09/2016 07:54, Raslan, KarimAllah wrote:
>>>
>>>
>>>>
>>>> On Jun 22, 2016, at 3:41 PM, Paolo Bonzini <pbonzini@redhat.com>
>>>> wrote:
>>>>
>>>>
>>>>
>>>> On 22/06/2016 04:34, KarimAllah Ahmed wrote:
>>>>>
>>>>> 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.
>>>>
>>>> How can you pass memory after the mem= boundary to the guest?
>>>
>>> Does my previous reply answer your question?
>>
>> Yes, but I'm not sure it's the right way to do it.  Looking at the
>> e820 memory map seems pretty hacky.
> 
> Can you use region_intersects() for this check?  See memremap() as an
> example using this interface.

This one seems more generic but very slow.

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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,