kvm: IOMMU read-only mapping support
cover kvm: IOMMU read-only mapping support
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Alex
---
Alex Williamson (2):
kvm: Force IOMMU remapping on memory slot read-only flag changes
kvm: Obey read-only mappings in iommu
virt/kvm/iommu.c | 4 +++- virt/kvm/kvm_main.c | 28 ++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-)
PATCH 1/2 kvm: Force IOMMU remapping on memory slot read-only flag changes
Memory slot flags can be altered without changing other parameters of the slot. The read-only attribute is the only one the IOMMU cares about, so generate an un-map, re-map when this occurs. This also avoid unnecessarily re-mapping the slot when no IOMMU visible changes are made.
--- virt/kvm/kvm_main.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
virt/kvm/kvm_main.c +24 -4
@@ -731,6 +731,7 @@ int __kvm_set_memory_region(struct kvm *kvm,struct kvm_memory_slot *slot;struct kvm_memory_slot old, new;struct kvm_memslots *slots =NULL , *old_memslots; +bool old_iommu_mapped; r =check_memory_region_flags (mem);if (r)
@@ -772,6 +773,8 @@ int __kvm_set_memory_region(struct kvm *kvm, new.npages = npages; new.flags = mem->flags; + old_iommu_mapped = old.npages; +/* * Disallow changing a memory slot's size or changing anything about * zero sized slots that doesn't involve making them non-zero.
@@ -835,6 +838,7 @@ int __kvm_set_memory_region(struct kvm *kvm,/* slot was deleted or moved, clear iommu mapping */ kvm_iommu_unmap_pages (kvm, &old); + old_iommu_mapped = false;/* From this point no new shadow pages pointing to a deleted, * or moved, memslot will be created. *
@@ -863,11 +867,27 @@ int __kvm_set_memory_region(struct kvm *kvm, goto out_free; } -/* map new memory slot into the iommu */ +/* +* IOMMU mapping: New slots need to be mapped. Old slots need to be +* un-mapped and re-mapped if their base changes or if flags that the +* iommu cares about change (read-only). Base change unmapping is +* handled above with slot deletion, so we only unmap incompatible +* flags here. Anything else the iommu might care about for existing +* slots (size changes, userspace addr changes) is disallowed above, +* so any other attribute changes getting here can be skipped. +*/ if (npages) { - r =kvm_iommu_map_pages (kvm, &new); -if (r) - goto out_slots; +if (old_iommu_mapped && + ((new.flags ^ old.flags) &KVM_MEM_READONLY )) { +kvm_iommu_unmap_pages (kvm, &old); + old_iommu_mapped = false; + } + +if (!old_iommu_mapped) { + r =kvm_iommu_map_pages (kvm, &new); +if (r) + goto out_slots; + } }/* actual memory is freed via old in kvm_free_physmem_slot below */
PATCH 2/2 kvm: Obey read-only mappings in iommu
In reply to Alex WilliamsonWe've been ignoring read-only mappings and programming everything into the iommu as read-write. Fix this to only include the write access flag when read-only is not set.
--- virt/kvm/iommu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/iommu.c +3 -1
@@ -76,7 +76,9 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) gfn = slot->base_gfn; end_gfn = gfn + slot->npages; - flags =IOMMU_READ |IOMMU_WRITE ; + flags =IOMMU_READ ; +if (!(slot->flags &KVM_MEM_READONLY )) + flags |=IOMMU_WRITE ;if (kvm->arch.iommu_flags &KVM_IOMMU_CACHE_COHERENCY ) flags |=IOMMU_CACHE ;
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Thanks,
Takuya
Re: [PATCH 1/2] kvm: Force IOMMU remapping on memory slot read-only flag changes
In reply to Alex WilliamsonQuoted 6 lines · Alex Williamson
Memory slot flags can be altered without changing other parameters of the slot. The read-only attribute is the only one the IOMMU cares about, so generate an un-map, re-map when this occurs. This also avoid unnecessarily re-mapping the slot when no IOMMU visible changes are made.
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit c972f3b1 changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]
Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):
host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);
It sets spte based on the old value that means the readonly flag check is missed. We need to call kvm_arch_flush_shadow_all under this case.
Quoted 4 lines · On Fri, 25 Jan 2013 11:28:40 +0800 Xiao Guangrong…
Quoted 2 lines
I think I can naturally update my patch after this gets merged.
Please wait.
The patch I mentioned above won't change anything. Just cleans up set_memory_region(). The only possible change which we discussed before was whether we call iommu_map() on a flags change.
Quoted 3 lines
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]
I'll look at the commit later, after the lunch break.
Quoted 12 lines
Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.
So the change needed will be in arch/x86.
in arch_commit_* one.
Right?
Note: I'm not touching arch_* memory slot APIs now because ARM KVM is coming now. So no problem, the flags will be passed as before.
Takuya
Quoted 5 lines · On Fri, 25 Jan 2013 12:59:12 +0900 Takuya Yoshikawa…
Quoted 3 lines
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]I'll look at the commit later, after the lunch break.
I've done!
So we need to do:
if (npages && (GET_DIRTY || RDONLY)) write protect
About sync_page, I'm not sure what is the best fix.
Takuya
Quoted 25 lines
Quoted 13 lines
Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.So the change needed will be in
arch/x86.
in arch_commit_* one.Right?
Note: I'm not touching arch_* memory slot APIs now because ARM KVM is coming now. So no problem, the flags will be passed as before.
Takuya
Signature · 1 line
Takuya Yoshikawa <[email protected]>
Gleb Natapov
AppliedQuoted 6 lines · Alex Williamson
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Alex
Applied, thanks.
Quoted 10 lines
---
Alex Williamson (2):
kvm: Force IOMMU remapping on memory slot read-only flag changes
kvm: Obey read-only mappings in iommuvirt/kvm/iommu.c | 4 +++- virt/kvm/kvm_main.c | 28 ++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-)
--
Gleb.
Quoted 31 lines · Xiao Guangrong
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.
Why not just disallow changing memory region KVM_MEM_READONLY flag without deleting the region?
--
Gleb.
Quoted 4 lines · On Mon, 28 Jan 2013 12:59:03 +0200 Gleb Natapov <[email protected]> wrote:
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?
Sounds good.
If you prefer, I can fold the required change into my patch.
Takuya
Quoted 11 lines · Takuya Yoshikawa
Quoted 4 lines · On Mon, 28 Jan 2013 12:59:03 +0200 Gleb Natapov <[email protected]> wrote:
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?Sounds good.
If you prefer, I can fold the required change into my patch.
That would seem to make my patch 1/2 unnecessary. Thanks,
Alex
Quoted 6 lines · On Mon, 28 Jan 2013 08:36:56 -0700 Alex Williamson…
Quoted 5 lines · Takuya Yoshikawa
Quoted 2 lines · On Mon, 28 Jan 2013 12:59:03 +0200 Gleb Natapov <[email protected]> wrote:
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?Sounds good.
If you prefer, I can fold the required change into my patch.
That would seem to make my patch 1/2 unnecessary. Thanks,
I've decided not to fold that change into my
KVM: set_memory_region: Identify the requested change explicitly
since this is a functional change, see v3 I posted today.
Since QEMU is not the only hypervisor using KVM, we should make the change with a proper subject informing every user of the new restriction.
If the maintainers prefer to remove Alex's patch 1/2 first and then rebase my v3 patch, I can do so.
Takuya
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Xiao Guangrong
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);
Quoted 4 lines
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?
It will introduce some restriction when VM-sharing-mem is being implemented, but we need to do some optimization for it, at least, properly write-protect readonly pages (fix sync_page()) instead of zap_all_page.
So, i guess we can do the simple fix first.
Quoted 33 lines · Xiao Guangrong
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Xiao Guangrong
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?It will introduce some restriction when VM-sharing-mem is being implemented, but we need to do some optimization for it, at least, properly write-protect readonly pages (fix
sync_page()) instead ofzap_all_page.
What is VM-sharing-mem?
Quoted 2 lines
So, i guess we can do the simple fix first.
By simple fix you mean calling kvm_arch_flush_shadow_all() on READONLY flag change?
--
Gleb.
Quoted 36 lines · Gleb Natapov
Quoted 35 lines · Xiao Guangrong
Quoted 34 lines · Gleb Natapov
Quoted 33 lines · Xiao Guangrong
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?It will introduce some restriction when VM-sharing-mem is being implemented, but we need to do some optimization for it, at least, properly write-protect readonly pages (fix
sync_page()) instead ofzap_all_page.What is VM-sharing-mem?
Sharing memory between different guests.
Quoted 5 lines
Quoted 3 lines
So, i guess we can do the simple fix first.
By simple fix you mean calling
kvm_arch_flush_shadow_all()on READONLY flag change?
Simply disallow READONLY flag changing.
Quoted 45 lines · Gleb Natapov
Quoted 44 lines · Xiao Guangrong
Quoted 43 lines · Gleb Natapov
Quoted 42 lines · Xiao Guangrong
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Xiao Guangrong
Quoted 11 lines · Takuya Yoshikawa
Quoted 3 lines · On Thu, 24 Jan 2013 15:03:57 -0700 Alex Williamson…
A couple patches to make KVM IOMMU support honor read-only mappings. This causes an un-map, re-map when the read-only flag changes and makes use of it when setting IOMMU attributes. Thanks,
Looks good to me.
I think I can naturally update my patch after this gets merged.
Please wait.
The commit
c972f3b1changed the write-protect behaviour - it does wirte-protection only when dirty flag is set.
[ I did not see this commit when we discussed the problem before. ]Further more, i notice that write-protect is not enough, when do sync shadow page:
FNAME(sync_page):host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE; set_spte(vcpu, &sp->spt[i], pte_access, PT_PAGE_TABLE_LEVEL, gfn, spte_to_pfn(sp->spt[i]), true, false, host_writable);Quoted 4 lines
Quoted 2 lines
It sets spte based on the old value that means the readonly flag check is missed. We need to call
kvm_arch_flush_shadow_allunder this case.Why not just disallow changing memory region
KVM_MEM_READONLYflag without deleting the region?It will introduce some restriction when VM-sharing-mem is being implemented, but we need to do some optimization for it, at least, properly write-protect readonly pages (fix
sync_page()) instead ofzap_all_page.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
What is VM-sharing-mem?
Sharing memory between different guests.
That much I can figure out for the name itself. My question is how this sharing will work? Why
KVM_MEM_READONLYis needed for it? Why ability to changeKVM_MEM_READONLYflag without destroying memory region will be important. What's wrong with nahanni, shared memory device we have today?
I'm not clear now or maybe my memory is wrong, i need to find the origin discussion...
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
So, i guess we can do the simple fix first.
By simple fix you mean calling
kvm_arch_flush_shadow_all()on READONLY flag change?Simply disallow READONLY flag changing.
Ok, can somebody craft a patch?
Takuya, will you? ;)
Quoted 9 lines · On Wed, 30 Jan 2013 12:06:32 +0800 Xiao Guangrong…
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So, i guess we can do the simple fix first.
By simple fix you mean calling
kvm_arch_flush_shadow_all()on READONLY flag change?Simply disallow READONLY flag changing.
Ok, can somebody craft a patch?
Takuya, will you? ;)
I will.
Thank you for pointing out the problem.
We can revisit the API later to determine what kind of changes should be allowed.
Takuya