Skip to contentSkip to conversation

KVM: PPC: Book3S HV: Invalidate TLB when nested vcpu moves physical cpu

RFC PATCH Superseded Paul Mackerras 2 participants 2 messages Sep 21, 2018 – Sep 27, 2018

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 27/32 KVM: PPC: Book3S HV: Invalidate TLB when nested vcpu moves physical cpu

From: Suraj Jitindar Singh <[email protected]>

This is only done at level 0, since only level 0 knows which physical CPU a vcpu is running on. This does for nested guests what L0 already did for its own guests, which is to flush the TLB on a pCPU when it goes to run a vCPU there, and there is another vCPU in the same VM which previously ran on this pCPU and has now started to run on another pCPU. This is to handle the situation where the other vCPU touched a mapping, moved to another pCPU and did a tlbiel (local-only tlbie) on that new pCPU and thus left behind a stale TLB entry on this pCPU.

---
 arch/powerpc/include/asm/kvm_book3s_64.h |  3 +
 arch/powerpc/kvm/book3s_hv.c             | 98 +++++++++++++++++++-------------
 arch/powerpc/kvm/book3s_hv_nested.c      |  5 ++
 3 files changed, 68 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +3 -0
index dadd40e..84c4c0e 100644
@@ -35,6 +35,9 @@ struct kvm_nested_guest {
 	long refcnt;			/* number of pointers to this struct */
 	struct mutex tlb_lock;		/* serialize page faults and tlbies */
 	struct kvm_nested_guest *next;
+	cpumask_t need_tlb_flush;
+	cpumask_t cpu_in_guest;
+	int prev_cpu[NR_CPUS];
 };
 
 /* Structure for a nested guest rmap entry */
arch/powerpc/kvm/book3s_hv.c +59 -38

151 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_nested.c +5 -0
index 84c82a2..0d7cd66 100644
@@ -168,6 +168,9 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
 	if (err)
 		return H_PARAMETER;
 
+	if (l2_hv.vcpu_token >= NR_CPUS)
+		return H_PARAMETER;
+
 	/* translate lpid */
 	l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
 	if (!l2)
@@ -377,6 +380,8 @@ struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
 		goto out_free2;
 	gp->shadow_lpid = shadow_lpid;
 
+	memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
+
 	return gp;
 
  out_free2:
Signature · 1 line
2.7.4
Reviewed-by on v1 27/32
#1 lore raw
Quoted 11 lines · Paul Mackerras

From: Suraj Jitindar Singh <[email protected]>

This is only done at level 0, since only level 0 knows which physical CPU a vcpu is running on. This does for nested guests what L0 already did for its own guests, which is to flush the TLB on a pCPU when it goes to run a vCPU there, and there is another vCPU in the same VM which previously ran on this pCPU and has now started to run on another pCPU. This is to handle the situation where the other vCPU touched a mapping, moved to another pCPU and did a tlbiel (local-only tlbie) on that new pCPU and thus left behind a stale TLB entry on this pCPU.

Quoted 2 lines

[snip]

Quoted 5 lines

static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu) {

arch/powerpc/kvm/book3s_hv.c +2 -0
+	struct kvm_nested_guest *nested = vcpu->arch.nested;
+	cpumask_t *cpu_in_guest;
 	int i;

arch/powerpc/kvm/book3s_hv.c static void kvmppc_release_hwthread(int cpu) · line 2428

Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +7 -1
 
 	cpu = cpu_first_thread_sibling(cpu);
-	cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
+	if (nested) {
+		cpumask_set_cpu(cpu, &nested->need_tlb_flush);
+		cpu_in_guest = &nested->cpu_in_guest;
+	} else {
+		cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
+		cpu_in_guest = &kvm->arch.cpu_in_guest;
+	}

I don't think it's important for now, but a possible followup might be to make a "lpid_state" or something structure for the information that's common between a direct guest and a nested guest. That might collapse a bunch of these if (nested) tests.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson