Skip to contentSkip to conversation

Memory regions and VMAs across architectures

DISCUSSION Christoffer Dall 3 participants 6 messages Nov 8, 2019 – Nov 21, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

I had a look at our relatively complicated logic in kvm_arch_prepare_memory_region(), and was wondering if there was room to unify some of this handling between architectures.

(If you haven't seen our implementation, you can find it in virt/kvm/arm/mmu.c, and it has lovely ASCII art!)

I then had a look at the x86 code, but that doesn't actually do anything when creating memory regions, which makes me wonder why the arhitectures differ in this aspect.

The reason we added the logic that we have for arm/arm64 is that we don't really want to take faults for I/O accesses. I'm not actually sure if this is a corretness thing, or an optimization effort, and the original commit message doesn't really explain. Ard, you wrote that code, do you recall the details?

In any case, what we do is to check for each VMA backing a memslot, we check if the memslot flags and vma flags are a reasonable match, and we try to detect I/O mappings by looking for the VM_PFNMAP flag on the VMA and pre-populate stage 2 page tables (our equivalent of EPT/NPT/...). However, there are some things which are not clear to me:

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

Thanks,

Christoffer

#1 lore raw
Quoted 13 lines · Christoffer Dall

Hi,

I had a look at our relatively complicated logic in kvm_arch_prepare_memory_region(), and was wondering if there was room to unify some of this handling between architectures.

(If you haven't seen our implementation, you can find it in virt/kvm/arm/mmu.c, and it has lovely ASCII art!)

I then had a look at the x86 code, but that doesn't actually do anything when creating memory regions, which makes me wonder why the arhitectures differ in this aspect.

Quoted 6 lines

The reason we added the logic that we have for arm/arm64 is that we don't really want to take faults for I/O accesses. I'm not actually sure if this is a corretness thing, or an optimization effort, and the original commit message doesn't really explain. Ard, you wrote that code, do you recall the details?

I have a vague recollection of implementing execution from read-only guest memory in order to support execute-in-place from emulated NOR flash in UEFI, and going down a rabbit hole debugging random, seemingly unrelated crashes in the host which turned out to be caused by the zero page getting corrupted because it was mapped read-write in the guest to back uninitialized regions of the NOR flash.

That doesn't quite answer your question, though - I think it was just an optimization ...

Quoted 10 lines

In any case, what we do is to check for each VMA backing a memslot, we check if the memslot flags and vma flags are a reasonable match, and we try to detect I/O mappings by looking for the VM_PFNMAP flag on the VMA and pre-populate stage 2 page tables (our equivalent of EPT/NPT/...). However, there are some things which are not clear to me:

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Quoted 9 lines

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

Thanks,

Christoffer

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

#2 lore raw
In reply to Christoffer Dall
Quoted 25 lines · Christoffer Dall

Hi,

I had a look at our relatively complicated logic in kvm_arch_prepare_memory_region(), and was wondering if there was room to unify some of this handling between architectures.

(If you haven't seen our implementation, you can find it in virt/kvm/arm/mmu.c, and it has lovely ASCII art!)

I then had a look at the x86 code, but that doesn't actually do anything when creating memory regions, which makes me wonder why the arhitectures differ in this aspect.

The reason we added the logic that we have for arm/arm64 is that we don't really want to take faults for I/O accesses. I'm not actually sure if this is a corretness thing, or an optimization effort, and the original commit message doesn't really explain. Ard, you wrote that code, do you recall the details?

In any case, what we do is to check for each VMA backing a memslot, we check if the memslot flags and vma flags are a reasonable match, and we try to detect I/O mappings by looking for the VM_PFNMAP flag on the VMA and pre-populate stage 2 page tables (our equivalent of EPT/NPT/...). However, there are some things which are not clear to me:

Quoted 3 lines

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Arm's prepare_memory_region() holds mmap_sem and mmu_lock while processing the VMAs and populating the stage 2 page tables. Holding mmap_sem prevents the VMAs from being invalidated while the stage 2 tables are populated, e.g. prevents racing with the mmu notifier. The VMAs could be modified after prepare_memory_region(), but the mmu notifier will ensure they are unmapped from stage2 prior the the host change taking effect. So I think you're safe (famous last words).

Quoted 3 lines

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

As Ard mentioned, it looks like an optimization. The "passthrough" part from the changelog implies that VM_PFNMAP memory regions are exclusive to the guest. Mapping the entire thing would be a nice boot optimization as it would save taking page faults on every page of the MMIO region.

As for how this is different from other archs... at least on x86, VM_PFNMAP isn't guaranteed to be passthrough or even MMIO, e.g. prefaulting the pages may actually trigger allocation, and remapping the addresses could be flat out wrong.

commit 8eef91239e57d2e932e7470879c9a504d5494ebb
Author: Ard Biesheuvel <[email protected]>
Date: Fri Oct 10 17:00:32 2014 +0200

arm/arm64: KVM: map MMIO regions at creation time

There is really no point in faulting in memory regions page by page if they are not backed by demand paged system RAM but by a linear passthrough mapping of a host MMIO region. So instead, detect such regions at setup time and install the mappings for the backing all at once.

#3 lore raw
Quoted 30 lines · Sean Christopherson
Quoted 25 lines · Christoffer Dall

Hi,

I had a look at our relatively complicated logic in kvm_arch_prepare_memory_region(), and was wondering if there was room to unify some of this handling between architectures.

(If you haven't seen our implementation, you can find it in virt/kvm/arm/mmu.c, and it has lovely ASCII art!)

I then had a look at the x86 code, but that doesn't actually do anything when creating memory regions, which makes me wonder why the arhitectures differ in this aspect.

The reason we added the logic that we have for arm/arm64 is that we don't really want to take faults for I/O accesses. I'm not actually sure if this is a corretness thing, or an optimization effort, and the original commit message doesn't really explain. Ard, you wrote that code, do you recall the details?

In any case, what we do is to check for each VMA backing a memslot, we check if the memslot flags and vma flags are a reasonable match, and we try to detect I/O mappings by looking for the VM_PFNMAP flag on the VMA and pre-populate stage 2 page tables (our equivalent of EPT/NPT/...). However, there are some things which are not clear to me:

Quoted 3 lines

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Quoted 8 lines

Arm's prepare_memory_region() holds mmap_sem and mmu_lock while processing the VMAs and populating the stage 2 page tables. Holding mmap_sem prevents the VMAs from being invalidated while the stage 2 tables are populated, e.g. prevents racing with the mmu notifier. The VMAs could be modified after prepare_memory_region(), but the mmu notifier will ensure they are unmapped from stage2 prior the the host change taking effect. So I think you're safe (famous last words).

So we for example check:

	writeable = !(memslot->falgs & KVM_MEM_READONLY);
	if (writeable && !(vma->vm_flags & VM_WRITE))
		return -EPERM;

And yes, user space can then unmap the VMAs and MMU notifiers will unmap the stage 2 entries, but user space can then create a new read-only VMA covering the area of the memslot and the fault-handling path will have to deal with this same check later. Only, the fault handling path, via gfn_to_pfn_prot(), returns an address based on an entirely different set of mechanics, than our prepare_memory_region, which I think indicates we are doing something wrong somewhere, and we should have a common path for faulting things in, for I/O, both if we do this up-front or if we do this at fault time.

Quoted 9 lines
Quoted 3 lines

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

As Ard mentioned, it looks like an optimization. The "passthrough" part from the changelog implies that VM_PFNMAP memory regions are exclusive to the guest. Mapping the entire thing would be a nice boot optimization as it would save taking page faults on every page of the MMIO region.

Quoted 4 lines

As for how this is different from other archs... at least on x86, VM_PFNMAP isn't guaranteed to be passthrough or even MMIO, e.g. prefaulting the pages may actually trigger allocation, and remapping the addresses could be flat out wrong.

What does VM_PFNMAP mean on x86? I didn't think we were relying on anything architecture specific in their meaning in the arm code, and I thought the VM_PFNMAP was a generic mm flag with generic mm meaning, but I could be wrong here?

Is there any valid semantics for creating a memslot backed by a VM_PFNMAP on x86, and if so, what are those?

Similarly, if you do map a device region straight to the guest on x86, how is that handled? (A pointer to the right place in the myriad of EPT and shadow code in x86 would be much appreciated.)

Thanks!

Christoffer

#4 lore raw
Quoted 21 lines · Christoffer Dall
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · Christoffer Dall

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Quoted 8 lines

Arm's prepare_memory_region() holds mmap_sem and mmu_lock while processing the VMAs and populating the stage 2 page tables. Holding mmap_sem prevents the VMAs from being invalidated while the stage 2 tables are populated, e.g. prevents racing with the mmu notifier. The VMAs could be modified after prepare_memory_region(), but the mmu notifier will ensure they are unmapped from stage2 prior the the host change taking effect. So I think you're safe (famous last words).

So we for example check:

	writeable = !(memslot->falgs & KVM_MEM_READONLY);
	if (writeable && !(vma->vm_flags & VM_WRITE))
		return -EPERM;
Quoted 9 lines

And yes, user space can then unmap the VMAs and MMU notifiers will unmap the stage 2 entries, but user space can then create a new read-only VMA covering the area of the memslot and the fault-handling path will have to deal with this same check later.Only, the fault handling path, via gfn_to_pfn_prot(), returns an address based on an entirely different set of mechanics, than our prepare_memory_region, which I think indicates we are doing something wrong somewhere, and we should have a common path for faulting things in, for I/O, both if we do this up-front or if we do this at fault time.

Unconditionally interpreting vm_pgoff as a physical address does not seem correct. There are cases where that might be correct, e.g. if the backing (virtual) file is a flat representation of the address space, which appears to be the case on some architectures, e.g. for PCI handling. But even then there should be some confirmation that the VMA is actually associated with such a file, otherwise KVM is at the mercy of userspace to do the right thing (unless there are other guarantees on arm I am unaware of).

Quoted 14 lines
Quoted 9 lines
Quoted 3 lines

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

As Ard mentioned, it looks like an optimization. The "passthrough" part from the changelog implies that VM_PFNMAP memory regions are exclusive to the guest. Mapping the entire thing would be a nice boot optimization as it would save taking page faults on every page of the MMIO region.

Quoted 4 lines

As for how this is different from other archs... at least on x86, VM_PFNMAP isn't guaranteed to be passthrough or even MMIO, e.g. prefaulting the pages may actually trigger allocation, and remapping the addresses could be flat out wrong.

Quoted 4 lines

What does VM_PFNMAP mean on x86? I didn't think we were relying on anything architecture specific in their meaning in the arm code, and I thought the VM_PFNMAP was a generic mm flag with generic mm meaning, but I could be wrong here?

No, you're correct, VM_PFNMAP is a generic flag that state the VMA doesn't have an associated struct page and is being managed directly by something other than the core mmu.

But not having a struct page doesn't guarantee that the PFN is backed by MMIO, or that it is exclusive to the guest (although in practice this is probably the case 99.9999% of the time). E.g. x86 supports having guest memory backed by regular ram that is hidden from the host kernel via 'mem=', which will show up as VM_PFNMAP.

Quoted 6 lines

Is there any valid semantics for creating a memslot backed by a VM_PFNMAP on x86, and if so, what are those?

Similarly, if you do map a device region straight to the guest on x86, how is that handled? (A pointer to the right place in the myriad of EPT and shadow code in x86 would be much appreciated.)

There is no special handling in x86 for VM_PFNMAP memory, we rely on KVM's generic __gfn_to_pfn_memslot() to retrieve the PFN on demand, and use mmu_notifier_seq to ensure the stale PFNs (invalidated in the host) aren't inserted into the guest page tables. Effectively the same thing arm does, sans the prepare_memory_region() shenanigans.

#5 lore raw
Quoted 76 lines · Sean Christopherson
Quoted 21 lines · Christoffer Dall
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · Christoffer Dall

First, what prevents user space from messing around with the VMAs after kvm_arch_prepare_memory_region() completes? If nothing, then what is the value of the cheks we perform wrt. to VMAs?

Quoted 8 lines

Arm's prepare_memory_region() holds mmap_sem and mmu_lock while processing the VMAs and populating the stage 2 page tables. Holding mmap_sem prevents the VMAs from being invalidated while the stage 2 tables are populated, e.g. prevents racing with the mmu notifier. The VMAs could be modified after prepare_memory_region(), but the mmu notifier will ensure they are unmapped from stage2 prior the the host change taking effect. So I think you're safe (famous last words).

So we for example check:

	writeable = !(memslot->falgs & KVM_MEM_READONLY);
	if (writeable && !(vma->vm_flags & VM_WRITE))
		return -EPERM;
Quoted 9 lines

And yes, user space can then unmap the VMAs and MMU notifiers will unmap the stage 2 entries, but user space can then create a new read-only VMA covering the area of the memslot and the fault-handling path will have to deal with this same check later.Only, the fault handling path, via gfn_to_pfn_prot(), returns an address based on an entirely different set of mechanics, than our prepare_memory_region, which I think indicates we are doing something wrong somewhere, and we should have a common path for faulting things in, for I/O, both if we do this up-front or if we do this at fault time.

Unconditionally interpreting vm_pgoff as a physical address does not seem correct. There are cases where that might be correct, e.g. if the backing (virtual) file is a flat representation of the address space, which appears to be the case on some architectures, e.g. for PCI handling. But even then there should be some confirmation that the VMA is actually associated with such a file, otherwise KVM is at the mercy of userspace to do the right thing (unless there are other guarantees on arm I am unaware of).

Quoted 14 lines
Quoted 9 lines
Quoted 3 lines

Second, why would arm/arm64 need special handling for I/O mappings compared to other architectures, and how is this dealt with for x86/s390/power/... ?

As Ard mentioned, it looks like an optimization. The "passthrough" part from the changelog implies that VM_PFNMAP memory regions are exclusive to the guest. Mapping the entire thing would be a nice boot optimization as it would save taking page faults on every page of the MMIO region.

Quoted 4 lines

As for how this is different from other archs... at least on x86, VM_PFNMAP isn't guaranteed to be passthrough or even MMIO, e.g. prefaulting the pages may actually trigger allocation, and remapping the addresses could be flat out wrong.

Quoted 4 lines

What does VM_PFNMAP mean on x86? I didn't think we were relying on anything architecture specific in their meaning in the arm code, and I thought the VM_PFNMAP was a generic mm flag with generic mm meaning, but I could be wrong here?

No, you're correct, VM_PFNMAP is a generic flag that state the VMA doesn't have an associated struct page and is being managed directly by something other than the core mmu.

But not having a struct page doesn't guarantee that the PFN is backed by MMIO, or that it is exclusive to the guest (although in practice this is probably the case 99.9999% of the time). E.g. x86 supports having guest memory backed by regular ram that is hidden from the host kernel via 'mem=', which will show up as VM_PFNMAP.

Quoted 6 lines

Is there any valid semantics for creating a memslot backed by a VM_PFNMAP on x86, and if so, what are those?

Similarly, if you do map a device region straight to the guest on x86, how is that handled? (A pointer to the right place in the myriad of EPT and shadow code in x86 would be much appreciated.)

Quoted 5 lines

There is no special handling in x86 for VM_PFNMAP memory, we rely on KVM's generic __gfn_to_pfn_memslot() to retrieve the PFN on demand, and use mmu_notifier_seq to ensure the stale PFNs (invalidated in the host) aren't inserted into the guest page tables. Effectively the same thing arm does, sans the prepare_memory_region() shenanigans.

Thanks Sean, I'll have a look at reworking said shenanigans ;)

Christoffer