Skip to content

Commit 2dbf12a

Browse files
ctmarinasMarc Zyngier
authored andcommitted
KVM: arm64: Simplify the sanitise_mte_tags() logic
Currently sanitise_mte_tags() checks if it's an online page before attempting to sanitise the tags. Such detection should be done in the caller via the VM_MTE_ALLOWED vma flag. Since kvm_set_spte_gfn() does not have the vma, leave the page unmapped if not already tagged. Tag initialisation will be done on a subsequent access fault in user_mem_abort(). Signed-off-by: Catalin Marinas <[email protected]> [[email protected]: fix the page initializer] Signed-off-by: Peter Collingbourne <[email protected]> Reviewed-by: Steven Price <[email protected]> Cc: Will Deacon <[email protected]> Cc: Marc Zyngier <[email protected]> Cc: Peter Collingbourne <[email protected]> Reviewed-by: Cornelia Huck <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e059853 commit 2dbf12a

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

arch/arm64/kvm/mmu.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,32 +1091,21 @@ static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva)
10911091
* - mmap_lock protects between a VM faulting a page in and the VMM performing
10921092
* an mprotect() to add VM_MTE
10931093
*/
1094-
static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
1095-
unsigned long size)
1094+
static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
1095+
unsigned long size)
10961096
{
10971097
unsigned long i, nr_pages = size >> PAGE_SHIFT;
1098-
struct page *page;
1098+
struct page *page = pfn_to_page(pfn);
10991099

11001100
if (!kvm_has_mte(kvm))
1101-
return 0;
1102-
1103-
/*
1104-
* pfn_to_online_page() is used to reject ZONE_DEVICE pages
1105-
* that may not support tags.
1106-
*/
1107-
page = pfn_to_online_page(pfn);
1108-
1109-
if (!page)
1110-
return -EFAULT;
1101+
return;
11111102

11121103
for (i = 0; i < nr_pages; i++, page++) {
11131104
if (!page_mte_tagged(page)) {
11141105
mte_clear_page_tags(page_address(page));
11151106
set_page_mte_tagged(page);
11161107
}
11171108
}
1118-
1119-
return 0;
11201109
}
11211110

11221111
static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
@@ -1127,7 +1116,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
11271116
bool write_fault, writable, force_pte = false;
11281117
bool exec_fault;
11291118
bool device = false;
1130-
bool shared;
11311119
unsigned long mmu_seq;
11321120
struct kvm *kvm = vcpu->kvm;
11331121
struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
@@ -1177,8 +1165,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
11771165
vma_shift = get_vma_page_shift(vma, hva);
11781166
}
11791167

1180-
shared = (vma->vm_flags & VM_SHARED);
1181-
11821168
switch (vma_shift) {
11831169
#ifndef __PAGETABLE_PMD_FOLDED
11841170
case PUD_SHIFT:
@@ -1299,12 +1285,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
12991285

13001286
if (fault_status != FSC_PERM && !device && kvm_has_mte(kvm)) {
13011287
/* Check the VMM hasn't introduced a new VM_SHARED VMA */
1302-
if (!shared)
1303-
ret = sanitise_mte_tags(kvm, pfn, vma_pagesize);
1304-
else
1288+
if ((vma->vm_flags & VM_MTE_ALLOWED) &&
1289+
!(vma->vm_flags & VM_SHARED)) {
1290+
sanitise_mte_tags(kvm, pfn, vma_pagesize);
1291+
} else {
13051292
ret = -EFAULT;
1306-
if (ret)
13071293
goto out_unlock;
1294+
}
13081295
}
13091296

13101297
if (writable)
@@ -1526,15 +1513,18 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
15261513
bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
15271514
{
15281515
kvm_pfn_t pfn = pte_pfn(range->pte);
1529-
int ret;
15301516

15311517
if (!kvm->arch.mmu.pgt)
15321518
return false;
15331519

15341520
WARN_ON(range->end - range->start != 1);
15351521

1536-
ret = sanitise_mte_tags(kvm, pfn, PAGE_SIZE);
1537-
if (ret)
1522+
/*
1523+
* If the page isn't tagged, defer to user_mem_abort() for sanitising
1524+
* the MTE tags. The S2 pte should have been unmapped by
1525+
* mmu_notifier_invalidate_range_end().
1526+
*/
1527+
if (kvm_has_mte(kvm) && !page_mte_tagged(pfn_to_page(pfn)))
15381528
return false;
15391529

15401530
/*

0 commit comments

Comments
 (0)