Skip to content

Commit 0c67288

Browse files
ankita-nvoupton
authored andcommitted
KVM: arm64: Allow cacheable stage 2 mapping using VMA flags
KVM currently forces non-cacheable memory attributes (either Normal-NC or Device-nGnRE) for a region based on pfn_is_map_memory(), i.e. whether or not the kernel has a cacheable alias for it. This is necessary in situations where KVM needs to perform CMOs on the region but is unnecessarily restrictive when hardware obviates the need for CMOs. KVM doesn't need to perform any CMOs on hardware with FEAT_S2FWB and CTR_EL0.DIC. As luck would have it, there are implementations in the wild that need to map regions of a device with cacheable attributes to function properly. An example of this is Nvidia's Grace Hopper/Blackwell systems where GPU memory is interchangeable with DDR and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. Of course, for this to work in a VM the GPU memory needs to have a cacheable mapping at stage-2. Allow cacheable stage-2 mappings to be created on supporting hardware when the VMA has cacheable memory attributes. Check these preconditions during memslot creation (in addition to fault handling) to potentially 'fail-fast' as a courtesy to userspace. CC: Oliver Upton <[email protected]> CC: Sean Christopherson <[email protected]> Suggested-by: Jason Gunthorpe <[email protected]> Suggested-by: Catalin Marinas <[email protected]> Suggested-by: David Hildenbrand <[email protected]> Tested-by: Donald Dutile <[email protected]> Signed-off-by: Ankit Agrawal <[email protected]> Reviewed-by: David Hildenbrand <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Oliver: refine changelog, squash kvm_supports_cacheable_pfnmap() patch ] Signed-off-by: Oliver Upton <[email protected]>
1 parent 2a8dfab commit 0c67288

2 files changed

Lines changed: 55 additions & 22 deletions

File tree

arch/arm64/include/asm/kvm_mmu.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,24 @@ static inline void kvm_fault_unlock(struct kvm *kvm)
371371
read_unlock(&kvm->mmu_lock);
372372
}
373373

374+
/*
375+
* ARM64 KVM relies on a simple conversion from physaddr to a kernel
376+
* virtual address (KVA) when it does cache maintenance as the CMO
377+
* instructions work on virtual addresses. This is incompatible with
378+
* VM_PFNMAP VMAs which may not have a kernel direct mapping to a
379+
* virtual address.
380+
*
381+
* With S2FWB and CACHE DIC features, KVM need not do cache flushing
382+
* and CMOs are NOP'd. This has the effect of no longer requiring a
383+
* KVA for addresses mapped into the S2. The presence of these features
384+
* are thus necessary to support cacheable S2 mapping of VM_PFNMAP.
385+
*/
386+
static inline bool kvm_supports_cacheable_pfnmap(void)
387+
{
388+
return cpus_have_final_cap(ARM64_HAS_STAGE2_FWB) &&
389+
cpus_have_final_cap(ARM64_HAS_CACHE_DIC);
390+
}
391+
374392
#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS
375393
void kvm_s2_ptdump_create_debugfs(struct kvm *kvm);
376394
#else

arch/arm64/kvm/mmu.c

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,18 +1654,39 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16541654
if (is_error_noslot_pfn(pfn))
16551655
return -EFAULT;
16561656

1657+
/*
1658+
* Check if this is non-struct page memory PFN, and cannot support
1659+
* CMOs. It could potentially be unsafe to access as cachable.
1660+
*/
16571661
if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
1658-
/*
1659-
* If the page was identified as device early by looking at
1660-
* the VMA flags, vma_pagesize is already representing the
1661-
* largest quantity we can map. If instead it was mapped
1662-
* via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
1663-
* and must not be upgraded.
1664-
*
1665-
* In both cases, we don't let transparent_hugepage_adjust()
1666-
* change things at the last minute.
1667-
*/
1668-
s2_force_noncacheable = true;
1662+
if (is_vma_cacheable) {
1663+
/*
1664+
* Whilst the VMA owner expects cacheable mapping to this
1665+
* PFN, hardware also has to support the FWB and CACHE DIC
1666+
* features.
1667+
*
1668+
* ARM64 KVM relies on kernel VA mapping to the PFN to
1669+
* perform cache maintenance as the CMO instructions work on
1670+
* virtual addresses. VM_PFNMAP region are not necessarily
1671+
* mapped to a KVA and hence the presence of hardware features
1672+
* S2FWB and CACHE DIC are mandatory to avoid the need for
1673+
* cache maintenance.
1674+
*/
1675+
if (!kvm_supports_cacheable_pfnmap())
1676+
return -EFAULT;
1677+
} else {
1678+
/*
1679+
* If the page was identified as device early by looking at
1680+
* the VMA flags, vma_pagesize is already representing the
1681+
* largest quantity we can map. If instead it was mapped
1682+
* via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
1683+
* and must not be upgraded.
1684+
*
1685+
* In both cases, we don't let transparent_hugepage_adjust()
1686+
* change things at the last minute.
1687+
*/
1688+
s2_force_noncacheable = true;
1689+
}
16691690
} else if (logging_active && !write_fault) {
16701691
/*
16711692
* Only actually map the page as writable if this was a write
@@ -1674,15 +1695,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
16741695
writable = false;
16751696
}
16761697

1677-
/*
1678-
* Prevent non-cacheable mappings in the stage-2 if a region of memory
1679-
* is cacheable in the primary MMU and the kernel lacks a cacheable
1680-
* alias. KVM cannot guarantee coherency between the guest/host aliases
1681-
* without the ability to perform CMOs.
1682-
*/
1683-
if (is_vma_cacheable && s2_force_noncacheable)
1684-
return -EINVAL;
1685-
16861698
if (exec_fault && s2_force_noncacheable)
16871699
return -ENOEXEC;
16881700

@@ -2243,8 +2255,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
22432255
break;
22442256
}
22452257

2246-
/* Cacheable PFNMAP is not allowed */
2247-
if (kvm_vma_is_cacheable(vma)) {
2258+
/*
2259+
* Cacheable PFNMAP is allowed only if the hardware
2260+
* supports it.
2261+
*/
2262+
if (kvm_vma_is_cacheable(vma) && !kvm_supports_cacheable_pfnmap()) {
22482263
ret = -EINVAL;
22492264
break;
22502265
}

0 commit comments

Comments
 (0)