[PATCH 4.9 012/152] arm/arm64: KVM: Take mmap_sem in kvm_arch_prepare_memory_region

From: Greg Kroah-Hartman
Date: Mon Apr 10 2017 - 13:55:39 EST


4.9-stable review patch. If anyone has any objections, please let me know.

------------------

From: Marc Zyngier <marc.zyngier@xxxxxxx>

commit 72f310481a08db821b614e7b5d00febcc9064b36 upstream.

We don't hold the mmap_sem while searching for VMAs (via find_vma), in
kvm_arch_prepare_memory_region, which can end up in expected failures.

Fixes: commit 8eef91239e57 ("arm/arm64: KVM: map MMIO regions at creation time")
Cc: Ard Biesheuvel <ard.biesheuvel@xxxxxxxxxx>
Cc: Eric Auger <eric.auger@xxxxxxxxx>
Reviewed-by: Christoffer Dall <cdall@xxxxxxxxxx>
[ Handle dirty page logging failure case ]
Signed-off-by: Suzuki K Poulose <suzuki.poulose@xxxxxxx>
Signed-off-by: Marc Zyngier <marc.zyngier@xxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>

---
arch/arm/kvm/mmu.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

--- a/arch/arm/kvm/mmu.c
+++ b/arch/arm/kvm/mmu.c
@@ -1806,6 +1806,7 @@ int kvm_arch_prepare_memory_region(struc
(KVM_PHYS_SIZE >> PAGE_SHIFT))
return -EFAULT;

+ down_read(&current->mm->mmap_sem);
/*
* A memory region could potentially cover multiple VMAs, and any holes
* between them, so iterate over all of them to find out if we can map
@@ -1849,8 +1850,10 @@ int kvm_arch_prepare_memory_region(struc
pa += vm_start - vma->vm_start;

/* IO region dirty page logging not allowed */
- if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
- return -EINVAL;
+ if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
+ ret = -EINVAL;
+ goto out;
+ }

ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
vm_end - vm_start,
@@ -1862,7 +1865,7 @@ int kvm_arch_prepare_memory_region(struc
} while (hva < reg_end);

if (change == KVM_MR_FLAGS_ONLY)
- return ret;
+ goto out;

spin_lock(&kvm->mmu_lock);
if (ret)
@@ -1870,6 +1873,8 @@ int kvm_arch_prepare_memory_region(struc
else
stage2_flush_memslot(kvm, memslot);
spin_unlock(&kvm->mmu_lock);
+out:
+ up_read(&current->mm->mmap_sem);
return ret;
}