Skip to content

Commit a80868f

Browse files
Suzuki K PouloseMarc Zyngier
authored andcommitted
KVM: arm/arm64: Enforce PTE mappings at stage2 when needed
commit 6794ad5 ("KVM: arm/arm64: Fix unintended stage 2 PMD mappings") made the checks to skip huge mappings, stricter. However it introduced a bug where we still use huge mappings, ignoring the flag to use PTE mappings, by not reseting the vma_pagesize to PAGE_SIZE. Also, the checks do not cover the PUD huge pages, that was under review during the same period. This patch fixes both the issues. Fixes : 6794ad5 ("KVM: arm/arm64: Fix unintended stage 2 PMD mappings") Reported-by: Zenghui Yu <yuzenghui@huawei.com> Cc: Zenghui Yu <yuzenghui@huawei.com> Cc: Christoffer Dall <christoffer.dall@arm.com> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 7494cec commit a80868f

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

virt/kvm/arm/mmu.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,9 @@ static void kvm_send_hwpoison_signal(unsigned long address,
15941594
send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
15951595
}
15961596

1597-
static bool fault_supports_stage2_pmd_mappings(struct kvm_memory_slot *memslot,
1598-
unsigned long hva)
1597+
static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,
1598+
unsigned long hva,
1599+
unsigned long map_size)
15991600
{
16001601
gpa_t gpa_start;
16011602
hva_t uaddr_start, uaddr_end;
@@ -1610,34 +1611,34 @@ static bool fault_supports_stage2_pmd_mappings(struct kvm_memory_slot *memslot,
16101611

16111612
/*
16121613
* Pages belonging to memslots that don't have the same alignment
1613-
* within a PMD for userspace and IPA cannot be mapped with stage-2
1614-
* PMD entries, because we'll end up mapping the wrong pages.
1614+
* within a PMD/PUD for userspace and IPA cannot be mapped with stage-2
1615+
* PMD/PUD entries, because we'll end up mapping the wrong pages.
16151616
*
16161617
* Consider a layout like the following:
16171618
*
16181619
* memslot->userspace_addr:
16191620
* +-----+--------------------+--------------------+---+
1620-
* |abcde|fgh Stage-1 PMD | Stage-1 PMD tv|xyz|
1621+
* |abcde|fgh Stage-1 block | Stage-1 block tv|xyz|
16211622
* +-----+--------------------+--------------------+---+
16221623
*
16231624
* memslot->base_gfn << PAGE_SIZE:
16241625
* +---+--------------------+--------------------+-----+
1625-
* |abc|def Stage-2 PMD | Stage-2 PMD |tvxyz|
1626+
* |abc|def Stage-2 block | Stage-2 block |tvxyz|
16261627
* +---+--------------------+--------------------+-----+
16271628
*
1628-
* If we create those stage-2 PMDs, we'll end up with this incorrect
1629+
* If we create those stage-2 blocks, we'll end up with this incorrect
16291630
* mapping:
16301631
* d -> f
16311632
* e -> g
16321633
* f -> h
16331634
*/
1634-
if ((gpa_start & ~S2_PMD_MASK) != (uaddr_start & ~S2_PMD_MASK))
1635+
if ((gpa_start & (map_size - 1)) != (uaddr_start & (map_size - 1)))
16351636
return false;
16361637

16371638
/*
16381639
* Next, let's make sure we're not trying to map anything not covered
1639-
* by the memslot. This means we have to prohibit PMD size mappings
1640-
* for the beginning and end of a non-PMD aligned and non-PMD sized
1640+
* by the memslot. This means we have to prohibit block size mappings
1641+
* for the beginning and end of a non-block aligned and non-block sized
16411642
* memory slot (illustrated by the head and tail parts of the
16421643
* userspace view above containing pages 'abcde' and 'xyz',
16431644
* respectively).
@@ -1646,8 +1647,8 @@ static bool fault_supports_stage2_pmd_mappings(struct kvm_memory_slot *memslot,
16461647
* userspace_addr or the base_gfn, as both are equally aligned (per
16471648
* the check above) and equally sized.
16481649
*/
1649-
return (hva & S2_PMD_MASK) >= uaddr_start &&
1650-
(hva & S2_PMD_MASK) + S2_PMD_SIZE <= uaddr_end;
1650+
return (hva & ~(map_size - 1)) >= uaddr_start &&
1651+
(hva & ~(map_size - 1)) + map_size <= uaddr_end;
16511652
}
16521653

16531654
static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
@@ -1676,12 +1677,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16761677
return -EFAULT;
16771678
}
16781679

1679-
if (!fault_supports_stage2_pmd_mappings(memslot, hva))
1680-
force_pte = true;
1681-
1682-
if (logging_active)
1683-
force_pte = true;
1684-
16851680
/* Let's check if we will get back a huge page backed by hugetlbfs */
16861681
down_read(&current->mm->mmap_sem);
16871682
vma = find_vma_intersection(current->mm, hva, hva + 1);
@@ -1692,18 +1687,22 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16921687
}
16931688

16941689
vma_pagesize = vma_kernel_pagesize(vma);
1690+
if (logging_active ||
1691+
!fault_supports_stage2_huge_mapping(memslot, hva, vma_pagesize)) {
1692+
force_pte = true;
1693+
vma_pagesize = PAGE_SIZE;
1694+
}
1695+
16951696
/*
16961697
* The stage2 has a minimum of 2 level table (For arm64 see
16971698
* kvm_arm_setup_stage2()). Hence, we are guaranteed that we can
16981699
* use PMD_SIZE huge mappings (even when the PMD is folded into PGD).
16991700
* As for PUD huge maps, we must make sure that we have at least
17001701
* 3 levels, i.e, PMD is not folded.
17011702
*/
1702-
if ((vma_pagesize == PMD_SIZE ||
1703-
(vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm))) &&
1704-
!force_pte) {
1703+
if (vma_pagesize == PMD_SIZE ||
1704+
(vma_pagesize == PUD_SIZE && kvm_stage2_has_pmd(kvm)))
17051705
gfn = (fault_ipa & huge_page_mask(hstate_vma(vma))) >> PAGE_SHIFT;
1706-
}
17071706
up_read(&current->mm->mmap_sem);
17081707

17091708
/* We need minimum second+third level pages */

0 commit comments

Comments
 (0)