Check for ambiguities in create alias ioctl.
PATCH 1/1 Check for ambiguities in create alias ioctl.
The current alias ioctl allows for the creation of
an alias covering a gpa that already exists. It is invalid, because the gpa space needs to be uniquely mapped. So, if there's a memory slot covering gpa range 0x123000 to 0x124000, and we create an alias from any gpa within that range to a different target, we create an essential ambiguity that brings no value at the cost of a lot of confusion. Right now this confusion manifests itself as a BUG() triggered in the rmaps code path.
Signed-off-by Glauber Costa <[email protected]>
--- arch/ia64/kvm/kvm-ia64.c | 6 ++++++ arch/powerpc/kvm/powerpc.c | 5 +++++ arch/s390/kvm/kvm-s390.c | 6 ++++++ arch/x86/kvm/x86.c | 23 +++++++++++++++++++++-- include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 31 ++++++++++++++++++++----------- 6 files changed, 62 insertions(+), 13 deletions(-)
arch/ia64/kvm/kvm-ia64.c +6 -0
@@ -1695,6 +1695,12 @@ void kvm_arch_hardware_unsetup(void) { } +int kvm_arch_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long npages,void *me) +{ +return 0 ; +} +static void vcpu_kick_intr (void *info) {#ifdef DEBUG
arch/powerpc/kvm/powerpc.c +5 -0
@@ -45,6 +45,11 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)return !(v->arch.msr &MSR_WE ); } +int kvm_arch_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long npages,void *me) +{ +return 0 ; +}int kvmppc_emulate_mmio (struct kvm_run *run,struct kvm_vcpu *vcpu) {
arch/s390/kvm/kvm-s390.c +6 -0
@@ -101,6 +101,12 @@ void kvm_arch_exit(void) { } +int kvm_arch_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long npages,void *me) +{ +return 0 ; +} +/* Section: device related */ long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg)
arch/x86/kvm/x86.c +21 -2
@@ -1506,6 +1506,22 @@ gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)return gfn; } +int kvm_arch_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long npages,void *me) +{ +int i; +for (i =0 ; i < kvm->arch.naliases ; i++) { +struct kvm_mem_alias *p = &kvm->arch.aliases[i]; +if (p == me) +continue ; + +if (!((base_gfn + npages < p->base_gfn) || + (base_gfn > p->base_gfn + p->npages))) +return -EEXIST ; + } +return 0 ; +} +/* * Set a new alias region. Aliases map a portion of physical memory into * another portion. This is useful for memory windows, for example the PC
@@ -1540,18 +1556,21 @@ static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, p->npages = alias->memory_size >>PAGE_SHIFT ; p->target_gfn = alias->target_phys_addr >>PAGE_SHIFT ; + r =kvm_check_overlap (kvm, p->base_gfn, p->npages, p); +if (r) + goto out_locked; +for (n =KVM_ALIAS_SLOTS ; n >0 ; --n)if (kvm->arch.aliases[n -1 ].npages)break ; kvm->arch.naliases = n; +out_locked:spin_unlock (&kvm->mmu_lock);kvm_mmu_zap_all (kvm);up_write (&kvm->slots_lock); -return 0 ; - out:return r; }
include/linux/kvm_host.h +4 -0
@@ -178,6 +178,10 @@ int kvm_arch_set_memory_region(struct kvm *kvm,struct kvm_userspace_memory_region *mem, struct kvm_memory_slot old, int user_alloc); +int kvm_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long pages,void *me); +int kvm_arch_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long pages,void *me);void kvm_arch_flush_shadow (struct kvm *kvm);gfn_t unalias_gfn (struct kvm *kvm,gfn_t gfn);struct page *gfn_to_page (struct kvm *kvm,gfn_t gfn);
virt/kvm/kvm_main.c +20 -11
@@ -685,6 +685,23 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)return 0 ; } +int kvm_check_overlap (struct kvm *kvm,gfn_t base_gfn, +unsigned long npages,void *me) +{ +int i; + +for (i =0 ; i <KVM_MEMORY_SLOTS ; ++i) { +struct kvm_memory_slot *s = &kvm->memslots[i]; + +if (s == me) +continue ; +if (!((base_gfn + npages <= s->base_gfn) || + (base_gfn >= s->base_gfn + s->npages))) +return -EEXIST ; + } +return kvm_arch_check_overlap (kvm, base_gfn, npages, me); +} +/* * Allocate some memory and give it an address in the guest physical address * space.
@@ -700,7 +717,6 @@ int __kvm_set_memory_region(struct kvm *kvm,int r;gfn_t base_gfn;unsigned long npages; -unsigned long i;struct kvm_memory_slot *memslot;struct kvm_memory_slot old, new;
@@ -733,17 +749,10 @@ int __kvm_set_memory_region(struct kvm *kvm,if (npages && old.npages && npages != old.npages) goto out_free; -/* Check for overlaps */ - r = -EEXIST ; -for (i =0 ; i <KVM_MEMORY_SLOTS ; ++i) { -struct kvm_memory_slot *s = &kvm->memslots[i]; + r =kvm_check_overlap (kvm, base_gfn, npages, memslot); +if (r) + goto out_free; -if (s == memslot) -continue ; -if (!((base_gfn + npages <= s->base_gfn) || - (base_gfn >= s->base_gfn + s->npages))) - goto out_free; - }/* Free page dirty bitmap if unneeded */ if (!(new.flags &KVM_MEM_LOG_DIRTY_PAGES ))
Signature · 1 line
1.5.6.5