Skip to content

Commit d478899

Browse files
dmatlackMarc Zyngier
authored andcommitted
KVM: Allow range-based TLB invalidation from common code
Make kvm_flush_remote_tlbs_range() visible in common code and create a default implementation that just invalidates the whole TLB. This paves the way for several future features/cleanups: - Introduction of range-based TLBI on ARM. - Eliminating kvm_arch_flush_remote_tlbs_memslot() - Moving the KVM/x86 TDP MMU to common code. No functional change intended. Signed-off-by: David Matlack <[email protected]> Signed-off-by: Raghavendra Rao Ananta <[email protected]> Reviewed-by: Gavin Shan <[email protected]> Reviewed-by: Shaoqin Huang <[email protected]> Reviewed-by: Anup Patel <[email protected]> Acked-by: Sean Christopherson <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent eddd214 commit d478899

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,8 @@ static inline int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
18041804
return -ENOTSUPP;
18051805
}
18061806

1807+
#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
1808+
18071809
#define kvm_arch_pmi_in_guest(vcpu) \
18081810
((vcpu) && (vcpu)->arch.handling_intr_from_guest)
18091811

arch/x86/kvm/mmu/mmu.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,12 @@ static inline bool kvm_available_flush_remote_tlbs_range(void)
278278
return kvm_x86_ops.flush_remote_tlbs_range;
279279
}
280280

281-
void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t start_gfn,
282-
gfn_t nr_pages)
281+
int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages)
283282
{
284-
int ret = -EOPNOTSUPP;
283+
if (!kvm_x86_ops.flush_remote_tlbs_range)
284+
return -EOPNOTSUPP;
285285

286-
if (kvm_x86_ops.flush_remote_tlbs_range)
287-
ret = static_call(kvm_x86_flush_remote_tlbs_range)(kvm, start_gfn,
288-
nr_pages);
289-
if (ret)
290-
kvm_flush_remote_tlbs(kvm);
286+
return static_call(kvm_x86_flush_remote_tlbs_range)(kvm, gfn, nr_pages);
291287
}
292288

293289
static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index);

arch/x86/kvm/mmu/mmu_internal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
170170
struct kvm_memory_slot *slot, u64 gfn,
171171
int min_level);
172172

173-
void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t start_gfn,
174-
gfn_t nr_pages);
175-
176173
/* Flush the given page (huge or not) of guest memory. */
177174
static inline void kvm_flush_remote_tlbs_gfn(struct kvm *kvm, gfn_t gfn, int level)
178175
{

include/linux/kvm_host.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,7 @@ int kvm_vcpu_yield_to(struct kvm_vcpu *target);
13591359
void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool yield_to_kernel_mode);
13601360

13611361
void kvm_flush_remote_tlbs(struct kvm *kvm);
1362+
void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages);
13621363

13631364
#ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
13641365
int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min);
@@ -1488,6 +1489,16 @@ static inline int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
14881489
int kvm_arch_flush_remote_tlbs(struct kvm *kvm);
14891490
#endif
14901491

1492+
#ifndef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
1493+
static inline int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
1494+
gfn_t gfn, u64 nr_pages)
1495+
{
1496+
return -EOPNOTSUPP;
1497+
}
1498+
#else
1499+
int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages);
1500+
#endif
1501+
14911502
#ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
14921503
void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
14931504
void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);

virt/kvm/kvm_main.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,19 @@ void kvm_flush_remote_tlbs(struct kvm *kvm)
366366
}
367367
EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
368368

369+
void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages)
370+
{
371+
if (!kvm_arch_flush_remote_tlbs_range(kvm, gfn, nr_pages))
372+
return;
373+
374+
/*
375+
* Fall back to a flushing entire TLBs if the architecture range-based
376+
* TLB invalidation is unsupported or can't be performed for whatever
377+
* reason.
378+
*/
379+
kvm_flush_remote_tlbs(kvm);
380+
}
381+
369382
static void kvm_flush_shadow_all(struct kvm *kvm)
370383
{
371384
kvm_arch_flush_shadow_all(kvm);

0 commit comments

Comments
 (0)