arm/arm64: KVM: allow the use of THP on 2MB aligned memslots
RFC PATCH 1/1 arm/arm64: KVM: allow the use of THP on 2MB aligned memslots
The THP code in KVM/ARM is a bit restrictive in not allowing a THP to be used if the VMA is not 2MB aligned. Actually, it is not so much the VMA that matters, but the associated memslot:
A process can perfectly mmap a region with no particular alignment restriction, and then pass a 2MB aligned address to KVM. In this case, KVM will only use this 2MB aligned region, and will ignore the range between vma->vm_start and memslot->userspace_addr.
The fix is then to check the alignment of memslot->userspace_addr.
--- arch/arm/kvm/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/kvm/mmu.c +2 -2
@@ -667,14 +667,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn = (fault_ipa &PMD_MASK ) >>PAGE_SHIFT ; }else {/* -* Pages belonging to VMAs not aligned to the PMD mapping +* Pages belonging to memslots not aligned to the PMD mapping * granularity cannot be mapped using block descriptors even * if the pages belong to a THP for the process, because the * stage-2 block descriptor will cover more than a single THP * and we loose atomicity for unmapping, updates, and splits * of the THP or other pages in the stage-2 block range. */ -if (vma->vm_start & ~PMD_MASK ) +if (memslot->userspace_addr & ~PMD_MASK ) force_pte = true; }up_read (¤t->mm->mmap_sem);
Signature · 1 line
1.8.2.3
RFC PATCH 1/1 arm/arm64: KVM: allow the use of THP on 2MB aligned memslots
Quoted 10 lines · Marc Zyngier
The THP code in KVM/ARM is a bit restrictive in not allowing a THP to be used if the VMA is not 2MB aligned. Actually, it is not so much the VMA that matters, but the associated memslot:
A process can perfectly mmap a region with no particular alignment restriction, and then pass a 2MB aligned address to KVM. In this case, KVM will only use this 2MB aligned region, and will ignore the range between
vma->vm_startandmemslot->userspace_addr.The fix is then to check the alignment of
memslot->userspace_addr.
That's more correct, but I'm wondering if it's enough.
What happens if the base_gfn is not aligned to a 2MB region, will we not be mapping something completely bogus here?
Quoted 32 lines
Cc Christoffer Dall <[email protected]>Signed-off-by Marc Zyngier <[email protected]>--- arch/arm/kvm/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm/kvm/mmu.c +2
-2@@ -667,14 +667,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn = (fault_ipa &PMD_MASK ) >>PAGE_SHIFT ; }else {/* -* Pages belonging to VMAs not aligned to the PMD mapping +* Pages belonging to memslots not aligned to the PMD mapping * granularity cannot be mapped using block descriptors even * if the pages belong to a THP for the process, because the * stage-2 block descriptor will cover more than a single THP * and we loose atomicity for unmapping, updates, and splits * of the THP or other pages in the stage-2 block range. */ -if (vma->vm_start & ~PMD_MASK ) +if (memslot->userspace_addr & ~PMD_MASK ) force_pte = true; }up_read (¤t->mm->mmap_sem);Signature · 1 line
1.8.2.3
Signature · 1 line
Christoffer
RFC PATCH 1/1 arm/arm64: KVM: allow the use of THP on 2MB aligned memslots
Quoted 11 lines · Christoffer Dall
Quoted 10 lines · Marc Zyngier
The THP code in KVM/ARM is a bit restrictive in not allowing a THP to be used if the VMA is not 2MB aligned. Actually, it is not so much
the VMA that matters, but the associated memslot:A process can perfectly mmap a region with no particular alignment restriction, and then pass a 2MB aligned address to KVM. In this case, KVM will only use this 2MB aligned region, and will ignore the range between
vma->vm_startandmemslot->userspace_addr.
Quoted 7 lines
Quoted 1 line
The fix is then to check the alignment of
memslot->userspace_addr.That's more correct, but I'm wondering if it's enough.
What happens if the
base_gfnis not aligned to a 2MB region, will we not
be mapping something completely bogus here?
Indeed. So far, we haven't seen a stupid enough userspace, but I'm sure it will happen.
I'll update this patch to also check for the base IPA of the memslot.
Thanks,
M.
Quoted 34 lines
Quoted 34 lines
Cc Christoffer Dall <[email protected]>Signed-off-by Marc Zyngier <[email protected]>--- arch/arm/kvm/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm/kvm/mmu.c +2
-2@@ -667,14 +667,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn = (fault_ipa &PMD_MASK ) >>PAGE_SHIFT ; }else {/* -* Pages belonging to VMAs not aligned to the PMD mapping +* Pages belonging to memslots not aligned to the PMD mapping * granularity cannot be mapped using block descriptors even * if the pages belong to a THP for the process, because the * stage-2 block descriptor will cover more than a single THP * and we loose atomicity for unmapping, updates, and splits * of the THP or other pages in the stage-2 block range. */ -if (vma->vm_start & ~PMD_MASK ) +if (memslot->userspace_addr & ~PMD_MASK ) force_pte = true; }
up_read(¤t->mm->mmap_sem);
--
1.8.2.3
Signature · 1 line
Fast, cheap, reliable. Pick two.