Skip to content

Commit 87da236

Browse files
wildea01ctmarinas
authored andcommitted
arm64: KVM: Add support for VPIPT I-caches
A VPIPT I-cache has two main properties: 1. Lines allocated into the cache are tagged by VMID and a lookup can only hit lines that were allocated with the current VMID. 2. I-cache invalidation from EL1/0 only invalidates lines that match the current VMID of the CPU doing the invalidation. This can cause issues with non-VHE configurations, where the host runs at EL1 and wants to invalidate I-cache entries for a guest running with a different VMID. VHE is not affected, because the host runs at EL2 and I-cache invalidation applies as expected. This patch solves the problem by invalidating the I-cache when unmapping a page at stage 2 on a system with a VPIPT I-cache but not running with VHE enabled. Hopefully this is an obscure enough configuration that the overhead isn't anything to worry about, although it does mean that the by-range I-cache invalidation currently performed when mapping at stage 2 can be elided on such systems, because the I-cache will be clean for the guest VMID following a rollover event. Acked-by: Marc Zyngier <[email protected]> Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent dda288d commit 87da236

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

arch/arm64/include/asm/kvm_mmu.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,13 @@ static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu,
242242

243243
kvm_flush_dcache_to_poc(va, size);
244244

245-
if (!icache_is_aliasing()) { /* PIPT */
246-
flush_icache_range((unsigned long)va,
247-
(unsigned long)va + size);
248-
} else {
245+
if (icache_is_aliasing()) {
249246
/* any kind of VIPT cache */
250247
__flush_icache_all();
248+
} else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) {
249+
/* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
250+
flush_icache_range((unsigned long)va,
251+
(unsigned long)va + size);
251252
}
252253
}
253254

arch/arm64/kvm/hyp/tlb.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
9494
dsb(ish);
9595
isb();
9696

97+
/*
98+
* If the host is running at EL1 and we have a VPIPT I-cache,
99+
* then we must perform I-cache maintenance at EL2 in order for
100+
* it to have an effect on the guest. Since the guest cannot hit
101+
* I-cache lines allocated with a different VMID, we don't need
102+
* to worry about junk out of guest reset (we nuke the I-cache on
103+
* VMID rollover), but we do need to be careful when remapping
104+
* executable pages for the same guest. This can happen when KSM
105+
* takes a CoW fault on an executable page, copies the page into
106+
* a page that was previously mapped in the guest and then needs
107+
* to invalidate the guest view of the I-cache for that page
108+
* from EL1. To solve this, we invalidate the entire I-cache when
109+
* unmapping a page from a guest if we have a VPIPT I-cache but
110+
* the host is running at EL1. As above, we could do better if
111+
* we had the VA.
112+
*
113+
* The moral of this story is: if you have a VPIPT I-cache, then
114+
* you should be running with VHE enabled.
115+
*/
116+
if (!has_vhe() && icache_is_vpipt())
117+
__flush_icache_all();
118+
97119
__tlb_switch_to_host()(kvm);
98120
}
99121

0 commit comments

Comments
 (0)