Skip to contentSkip to conversation

ppc patch queue 2012-10-30

GIT PULL New Alexander Graf 3 participants 21 messages Oct 30, 2012 – Oct 31, 2012

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict
* Book3S HV: Allow for lazy thread joining

Alex

The following changes since commit 8ca40a70a70988c0bdea106c894843f763ca2989:
Christoffer Dall (1):
KVM: Take kvm instead of vcpu to mmu_notifier_retry

are available in the git repository at:

git://github.com/agraf/linux-2.6.git for-upstream

Alexander Graf (4):
KVM: PPC: 44x: fix DCR read/write
KVM: Documentation: Fix reentry-to-be-consistent paragraph
KVM: PPC: Move mtspr/mfspr emulation into own functions
PPC: ePAPR: Convert header to uapi

Paul Mackerras (8):
KVM: PPC: Book3S HV: Allow KVM guests to stop secondary threads coming online
KVM: PPC: Book3S HV: Fix some races in starting secondary threads
KVM: PPC: Book3s HV: Don't access runnable threads list without vcore lock
KVM: PPC: Book3S HV: Fixes for late-joining threads
KVM: PPC: Book3S HV: Run virtual core whenever any vcpus in it can run
KVM: PPC: Book3S HV: Fix accounting of stolen time
KVM: PPC: Book3S HV: Allow DTL to be set to address 0, length 0
KVM: PPC: Book3S HV: Fix thinko in try_lock_hpte()

 Documentation/virtual/kvm/api.txt            |    3 +-
 arch/powerpc/include/asm/Kbuild              |    1 -
 arch/powerpc/include/asm/epapr_hcalls.h      |   55 +-----
 arch/powerpc/include/asm/kvm_asm.h           |    1 +
 arch/powerpc/include/asm/kvm_book3s_64.h     |    2 +-
 arch/powerpc/include/asm/kvm_host.h          |   17 +-
 arch/powerpc/include/asm/smp.h               |    8 +
 arch/powerpc/include/uapi/asm/Kbuild         |    1 +
 arch/powerpc/include/uapi/asm/epapr_hcalls.h |   98 ++++++++
 arch/powerpc/kernel/smp.c                    |   46 ++++
 arch/powerpc/kvm/44x_emulate.c               |    2 +
 arch/powerpc/kvm/book3s_hv.c                 |  316 ++++++++++++++++++--------
 arch/powerpc/kvm/book3s_hv_rmhandlers.S      |   11 +-
 arch/powerpc/kvm/emulate.c                   |  221 ++++++++++--------
 14 files changed, 518 insertions(+), 264 deletions(-)
 create mode 100644 arch/powerpc/include/uapi/asm/epapr_hcalls.h
#1 lore raw

PATCH 1/12 KVM: PPC: 44x: fix DCR read/write

When remembering the direction of a DCR transaction, we should write to the same variable that we interpret on later when doing vcpu_run again.

---
 arch/powerpc/kvm/44x_emulate.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/powerpc/kvm/44x_emulate.c +2 -0
index 1a793c4..35ec0a8 100644
@@ -46,6 +46,7 @@ static int emulate_mtdcr(struct kvm_vcpu *vcpu, int rs, int dcrn)
 		vcpu->run->dcr.dcrn = dcrn;
 		vcpu->run->dcr.data = kvmppc_get_gpr(vcpu, rs);
 		vcpu->run->dcr.is_write = 1;
+		vcpu->arch.dcr_is_write = 1;
 		vcpu->arch.dcr_needed = 1;
 		kvmppc_account_exit(vcpu, DCR_EXITS);
 		return EMULATE_DO_DCR;
@@ -80,6 +81,7 @@ static int emulate_mfdcr(struct kvm_vcpu *vcpu, int rt, int dcrn)
 		vcpu->run->dcr.dcrn = dcrn;
 		vcpu->run->dcr.data =  0;
 		vcpu->run->dcr.is_write = 0;
+		vcpu->arch.dcr_is_write = 0;
 		vcpu->arch.io_gpr = rt;
 		vcpu->arch.dcr_needed = 1;
 		kvmppc_account_exit(vcpu, DCR_EXITS);
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH 2/12 KVM: Documentation: Fix reentry-to-be-consistent paragraph

In reply to Alexander Graf

All user space offloaded instruction emulation needs to reenter kvm to produce consistent state again. Fix the section in the documentation to mention all of them.

---
 Documentation/virtual/kvm/api.txt |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
Documentation/virtual/kvm/api.txt +2 -1
index 4258180..6671fdc 100644
@@ -2183,7 +2183,8 @@ executed a memory-mapped I/O instruction which could not be satisfied
 by kvm.  The 'data' member contains the written data if 'is_write' is
 true, and should be filled by application code otherwise.
 
-NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO and KVM_EXIT_OSI, the corresponding
+NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO, KVM_EXIT_OSI, KVM_EXIT_DCR
+      and KVM_EXIT_PAPR the corresponding
 operations are complete (and guest state is consistent) only after userspace
 has re-entered the kernel with KVM_RUN.  The kernel side will first finish
 incomplete operations and then check for pending signals.  Userspace
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 3/12 KVM: PPC: Move mtspr/mfspr emulation into own functions

In reply to Alexander Graf

The mtspr/mfspr emulation code became quite big over time. Move it into its own function so things stay more readable.

---
 arch/powerpc/kvm/emulate.c |  221 ++++++++++++++++++++++++--------------------
 1 files changed, 121 insertions(+), 100 deletions(-)
arch/powerpc/kvm/emulate.c +121 -100
index ee04aba..b0855e5 100644
@@ -131,6 +131,125 @@ u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
 	return vcpu->arch.dec - jd;
 }
 
+static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
+{
+	enum emulation_result emulated = EMULATE_DONE;
+	ulong spr_val = kvmppc_get_gpr(vcpu, rs);
+
+	switch (sprn) {
+	case SPRN_SRR0:
+		vcpu->arch.shared->srr0 = spr_val;
+		break;
+	case SPRN_SRR1:
+		vcpu->arch.shared->srr1 = spr_val;
+		break;
+
+	/* XXX We need to context-switch the timebase for
+	 * watchdog and FIT. */
+	case SPRN_TBWL: break;
+	case SPRN_TBWU: break;
+
+	case SPRN_MSSSR0: break;
+
+	case SPRN_DEC:
+		vcpu->arch.dec = spr_val;
+		kvmppc_emulate_dec(vcpu);
+		break;
+
+	case SPRN_SPRG0:
+		vcpu->arch.shared->sprg0 = spr_val;
+		break;
+	case SPRN_SPRG1:
+		vcpu->arch.shared->sprg1 = spr_val;
+		break;
+	case SPRN_SPRG2:
+		vcpu->arch.shared->sprg2 = spr_val;
+		break;
+	case SPRN_SPRG3:
+		vcpu->arch.shared->sprg3 = spr_val;
+		break;
+
+	default:
+		emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
+						     spr_val);
+		if (emulated = EMULATE_FAIL)
+			printk(KERN_INFO "mtspr: unknown spr "
+				"0x%x\n", sprn);
+		break;
+	}
+
+	kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
+
+	return emulated;
+}
+
+static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
+{
+	enum emulation_result emulated = EMULATE_DONE;
+	ulong spr_val = 0;
+
+	switch (sprn) {
+	case SPRN_SRR0:
+		spr_val = vcpu->arch.shared->srr0;
+		break;
+	case SPRN_SRR1:
+		spr_val = vcpu->arch.shared->srr1;
+		break;
+	case SPRN_PVR:
+		spr_val = vcpu->arch.pvr;
+		break;
+	case SPRN_PIR:
+		spr_val = vcpu->vcpu_id;
+		break;
+	case SPRN_MSSSR0:
+		spr_val = 0;
+		break;
+
+	/* Note: mftb and TBRL/TBWL are user-accessible, so
+	 * the guest can always access the real TB anyways.
+	 * In fact, we probably will never see these traps. */
+	case SPRN_TBWL:
+		spr_val = get_tb() >> 32;
+		break;
+	case SPRN_TBWU:
+		spr_val = get_tb();
+		break;
+
+	case SPRN_SPRG0:
+		spr_val = vcpu->arch.shared->sprg0;
+		break;
+	case SPRN_SPRG1:
+		spr_val = vcpu->arch.shared->sprg1;
+		break;
+	case SPRN_SPRG2:
+		spr_val = vcpu->arch.shared->sprg2;
+		break;
+	case SPRN_SPRG3:
+		spr_val = vcpu->arch.shared->sprg3;
+		break;
+	/* Note: SPRG4-7 are user-readable, so we don't get
+	 * a trap. */
+
+	case SPRN_DEC:
+		spr_val = kvmppc_get_dec(vcpu, get_tb());
+		break;
+	default:
+		emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
+						     &spr_val);
+		if (unlikely(emulated = EMULATE_FAIL)) {
+			printk(KERN_INFO "mfspr: unknown spr "
+				"0x%x\n", sprn);
+		}
+		break;
+	}
+
+	if (emulated = EMULATE_DONE)
+		kvmppc_set_gpr(vcpu, rt, spr_val);
+	kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
+
+	return emulated;
+}
+
 /* XXX to do:
  * lhax
  * lhaux
@@ -156,7 +275,6 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	int sprn = get_sprn(inst);
 	enum emulation_result emulated = EMULATE_DONE;
 	int advance = 1;
-	ulong spr_val = 0;
 
 	/* this default type might be overwritten by subcategories */
 	kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
@@ -236,62 +354,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
 			break;
 
 		case OP_31_XOP_MFSPR:
-			switch (sprn) {
-			case SPRN_SRR0:
-				spr_val = vcpu->arch.shared->srr0;
-				break;
-			case SPRN_SRR1:
-				spr_val = vcpu->arch.shared->srr1;
-				break;
-			case SPRN_PVR:
-				spr_val = vcpu->arch.pvr;
-				break;
-			case SPRN_PIR:
-				spr_val = vcpu->vcpu_id;
-				break;
-			case SPRN_MSSSR0:
-				spr_val = 0;
-				break;
-
-			/* Note: mftb and TBRL/TBWL are user-accessible, so
-			 * the guest can always access the real TB anyways.
-			 * In fact, we probably will never see these traps. */
-			case SPRN_TBWL:
-				spr_val = get_tb() >> 32;
-				break;
-			case SPRN_TBWU:
-				spr_val = get_tb();
-				break;
-
-			case SPRN_SPRG0:
-				spr_val = vcpu->arch.shared->sprg0;
-				break;
-			case SPRN_SPRG1:
-				spr_val = vcpu->arch.shared->sprg1;
-				break;
-			case SPRN_SPRG2:
-				spr_val = vcpu->arch.shared->sprg2;
-				break;
-			case SPRN_SPRG3:
-				spr_val = vcpu->arch.shared->sprg3;
-				break;
-			/* Note: SPRG4-7 are user-readable, so we don't get
-			 * a trap. */
-
-			case SPRN_DEC:
-				spr_val = kvmppc_get_dec(vcpu, get_tb());
-				break;
-			default:
-				emulated = kvmppc_core_emulate_mfspr(vcpu, sprn,
-								     &spr_val);
-				if (unlikely(emulated = EMULATE_FAIL)) {
-					printk(KERN_INFO "mfspr: unknown spr "
-						"0x%x\n", sprn);
-				}
-				break;
-			}
-			kvmppc_set_gpr(vcpu, rt, spr_val);
-			kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
+			emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
 			break;
 
 		case OP_31_XOP_STHX:
@@ -308,49 +371,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
 			break;
 
 		case OP_31_XOP_MTSPR:
-			spr_val = kvmppc_get_gpr(vcpu, rs);
-			switch (sprn) {
-			case SPRN_SRR0:
-				vcpu->arch.shared->srr0 = spr_val;
-				break;
-			case SPRN_SRR1:
-				vcpu->arch.shared->srr1 = spr_val;
-				break;
-
-			/* XXX We need to context-switch the timebase for
-			 * watchdog and FIT. */
-			case SPRN_TBWL: break;
-			case SPRN_TBWU: break;
-
-			case SPRN_MSSSR0: break;
-
-			case SPRN_DEC:
-				vcpu->arch.dec = spr_val;
-				kvmppc_emulate_dec(vcpu);
-				break;
-
-			case SPRN_SPRG0:
-				vcpu->arch.shared->sprg0 = spr_val;
-				break;
-			case SPRN_SPRG1:
-				vcpu->arch.shared->sprg1 = spr_val;
-				break;
-			case SPRN_SPRG2:
-				vcpu->arch.shared->sprg2 = spr_val;
-				break;
-			case SPRN_SPRG3:
-				vcpu->arch.shared->sprg3 = spr_val;
-				break;
-
-			default:
-				emulated = kvmppc_core_emulate_mtspr(vcpu, sprn,
-								     spr_val);
-				if (emulated = EMULATE_FAIL)
-					printk(KERN_INFO "mtspr: unknown spr "
-						"0x%x\n", sprn);
-				break;
-			}
-			kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
+			emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
 			break;
 
 		case OP_31_XOP_DCBI:
Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 4/12 PPC: ePAPR: Convert header to uapi

In reply to Alexander Graf

The new uapi framework splits kernel internal and user space exported bits of header files more cleanly. Adjust the ePAPR header accordingly.

---
 arch/powerpc/include/asm/Kbuild              |    1 -
 arch/powerpc/include/asm/epapr_hcalls.h      |   55 +--------------
 arch/powerpc/include/uapi/asm/Kbuild         |    1 +
 arch/powerpc/include/uapi/asm/epapr_hcalls.h |   98 ++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 55 deletions(-)
 create mode 100644 arch/powerpc/include/uapi/asm/epapr_hcalls.h
arch/powerpc/include/asm/Kbuild +0 -1
index 13d6b7b..7e313f1 100644
@@ -34,6 +34,5 @@ header-y += termios.h
 header-y += types.h
 header-y += ucontext.h
 header-y += unistd.h
-header-y += epapr_hcalls.h
 
 generic-y += rwsem.h
arch/powerpc/include/asm/epapr_hcalls.h +1 -54

61 lines not on this page · raw message

arch/powerpc/include/uapi/asm/Kbuild +1 -0
index baebb3d..e6b5be8 100644
@@ -1,3 +1,4 @@
 # UAPI Header export list
 include include/uapi/asm-generic/Kbuild.asm
 
+header-y += epapr_hcalls.h
arch/powerpc/include/uapi/asm/epapr_hcalls.h new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#5 lore raw

PATCH 5/12 KVM: PPC: Book3S HV: Allow KVM guests to stop secondary threads coming online

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

When a Book3S HV KVM guest is running, we need the host to be in single-thread mode, that is, all of the cores (or at least all of the cores where the KVM guest could run) to be running only one active hardware thread. This is because of the hardware restriction in POWER processors that all of the hardware threads in the core must be in the same logical partition. Complying with this restriction is much easier if, from the host kernel's point of view, only one hardware thread is active.

This adds two hooks in the SMP hotplug code to allow the KVM code to make sure that secondary threads (i.e. hardware threads other than thread 0) cannot come online while any KVM guest exists. The KVM code still has to check that any core where it runs a guest has the secondary threads offline, but having done that check it can now be sure that they will not come online while the guest is running.

---
 arch/powerpc/include/asm/smp.h |    8 +++++++
 arch/powerpc/kernel/smp.c      |   46 ++++++++++++++++++++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv.c   |   12 ++++++++-
 3 files changed, 64 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/smp.h +8 -0
index ebc24dc..b625a1a 100644
@@ -66,6 +66,14 @@ void generic_cpu_die(unsigned int cpu);
 void generic_mach_cpu_die(void);
 void generic_set_cpu_dead(unsigned int cpu);
 int generic_check_cpu_restart(unsigned int cpu);
+
+extern void inhibit_secondary_onlining(void);
+extern void uninhibit_secondary_onlining(void);
+
+#else /* HOTPLUG_CPU */
+static inline void inhibit_secondary_onlining(void) {}
+static inline void uninhibit_secondary_onlining(void) {}
+
 #endif
 
 #ifdef CONFIG_PPC64
arch/powerpc/kernel/smp.c +46 -0

58 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv.c +10 -2
index 9a15da7..c5ddf04 100644
@@ -47,6 +47,7 @@
 #include <asm/page.h>
 #include <asm/hvcall.h>
 #include <asm/switch_to.h>
+#include <asm/smp.h>
 #include <linux/gfp.h>
 #include <linux/vmalloc.h>
 #include <linux/highmem.h>
@@ -1016,8 +1017,6 @@ static int kvmppc_run_core(struct kvmppc_vcore *vc)
 	/*
 	 * Make sure we are running on thread 0, and that
 	 * secondary threads are offline.
-	 * XXX we should also block attempts to bring any
-	 * secondary threads online.
 	 */
 	if (threads_per_core > 1 && !on_primary_thread()) {
 		list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
@@ -1730,11 +1729,20 @@ int kvmppc_core_init_vm(struct kvm *kvm)
 
 	kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
 	spin_lock_init(&kvm->arch.slot_phys_lock);
+
+	/*
+	 * Don't allow secondary CPU threads to come online
+	 * while any KVM VMs exist.
+	 */
+	inhibit_secondary_onlining();
+
 	return 0;
 }
 
 void kvmppc_core_destroy_vm(struct kvm *kvm)
 {
+	uninhibit_secondary_onlining();
+
 	if (kvm->arch.rma) {
 		kvm_release_rma(kvm->arch.rma);
 		kvm->arch.rma = NULL;
Signature · 1 line
1.6.0.2
#6 lore raw

PATCH 6/12 KVM: PPC: Book3S HV: Fix some races in starting secondary threads

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

Subsequent patches implementing in-kernel XICS emulation will make it possible for IPIs to arrive at secondary threads at arbitrary times. This fixes some races in how we start the secondary threads, which if not fixed could lead to occasional crashes of the host kernel.

This makes sure that (a) we have grabbed all the secondary threads, and verified that they are no longer in the kernel, before we start any thread, (b) that the secondary thread loads its vcpu pointer after clearing the IPI that woke it up (so we don't miss a wakeup), and (c) that the secondary thread clears its vcpu pointer before incrementing the nap count. It also removes unnecessary setting of the vcpu and vcore pointers in the paca in kvmppc_core_vcpu_load.

---
 arch/powerpc/kvm/book3s_hv.c            |   41 ++++++++++++++++++-------------
 arch/powerpc/kvm/book3s_hv_rmhandlers.S |   11 ++++++--
 2 files changed, 32 insertions(+), 20 deletions(-)
arch/powerpc/kvm/book3s_hv.c +24 -17
index c5ddf04..77dec0f 100644
@@ -64,8 +64,6 @@ void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
 
-	local_paca->kvm_hstate.kvm_vcpu = vcpu;
-	local_paca->kvm_hstate.kvm_vcore = vc;
 	if (vc->runner = vcpu && vc->vcore_state != VCORE_INACTIVE)
 		vc->stolen_tb += mftb() - vc->preempt_tb;
 }
@@ -880,6 +878,7 @@ static int kvmppc_grab_hwthread(int cpu)
 
 	/* Ensure the thread won't go into the kernel if it wakes */
 	tpaca->kvm_hstate.hwthread_req = 1;
+	tpaca->kvm_hstate.kvm_vcpu = NULL;
 
 	/*
 	 * If the thread is already executing in the kernel (e.g. handling
@@ -929,7 +928,6 @@ static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
 	smp_wmb();
 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
 	if (vcpu->arch.ptid) {
-		kvmppc_grab_hwthread(cpu);
 		xics_wake_cpu(cpu);
 		++vc->n_woken;
 	}
@@ -955,7 +953,8 @@ static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
 
 /*
  * Check that we are on thread 0 and that any other threads in
- * this core are off-line.
+ * this core are off-line.  Then grab the threads so they can't
+ * enter the kernel.
  */
 static int on_primary_thread(void)
 {
@@ -967,6 +966,17 @@ static int on_primary_thread(void)
 	while (++thr < threads_per_core)
 		if (cpu_online(cpu + thr))
 			return 0;
+
+	/* Grab all hw threads so they can't go into the kernel */
+	for (thr = 1; thr < threads_per_core; ++thr) {
+		if (kvmppc_grab_hwthread(cpu + thr)) {
+			/* Couldn't grab one; let the others go */
+			do {
+				kvmppc_release_hwthread(cpu + thr);
+			} while (--thr > 0);
+			return 0;
+		}
+	}
 	return 1;
 }
 
@@ -1015,16 +1025,6 @@ static int kvmppc_run_core(struct kvmppc_vcore *vc)
 	}
 
 	/*
-	 * Make sure we are running on thread 0, and that
-	 * secondary threads are offline.
-	 */
-	if (threads_per_core > 1 && !on_primary_thread()) {
-		list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
-			vcpu->arch.ret = -EBUSY;
-		goto out;
-	}
-
-	/*
 	 * Assign physical thread IDs, first to non-ceded vcpus
 	 * and then to ceded ones.
 	 */
@@ -1043,15 +1043,22 @@ static int kvmppc_run_core(struct kvmppc_vcore *vc)
 		if (vcpu->arch.ceded)
 			vcpu->arch.ptid = ptid++;
 
+	/*
+	 * Make sure we are running on thread 0, and that
+	 * secondary threads are offline.
+	 */
+	if (threads_per_core > 1 && !on_primary_thread()) {
+		list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
+			vcpu->arch.ret = -EBUSY;
+		goto out;
+	}
+
 	vc->stolen_tb += mftb() - vc->preempt_tb;
 	vc->pcpu = smp_processor_id();
 	list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
 		kvmppc_start_thread(vcpu);
 		kvmppc_create_dtl_entry(vcpu, vc);
 	}
-	/* Grab any remaining hw threads so they can't go into the kernel */
-	for (i = ptid; i < threads_per_core; ++i)
-		kvmppc_grab_hwthread(vc->pcpu + i);
 
 	preempt_disable();
 	spin_unlock(&vc->lock);
arch/powerpc/kvm/book3s_hv_rmhandlers.S +8 -3
index 44b72fe..1e90ef6 100644
@@ -134,8 +134,11 @@ kvm_start_guest:
 
 27:	/* XXX should handle hypervisor maintenance interrupts etc. here */
 
+	/* reload vcpu pointer after clearing the IPI */
+	ld	r4,HSTATE_KVM_VCPU(r13)
+	cmpdi	r4,0
 	/* if we have no vcpu to run, go back to sleep */
-	beq	cr1,kvm_no_guest
+	beq	kvm_no_guest
 
 	/* were we napping due to cede? */
 	lbz	r0,HSTATE_NAPPING(r13)
@@ -1587,6 +1590,10 @@ secondary_too_late:
 	.endr
 
 secondary_nap:
+	/* Clear our vcpu pointer so we don't come back in early */
+	li	r0, 0
+	std	r0, HSTATE_KVM_VCPU(r13)
+	lwsync
 	/* Clear any pending IPI - assume we're a secondary thread */
 	ld	r5, HSTATE_XICS_PHYS(r13)
 	li	r7, XICS_XIRR
@@ -1612,8 +1619,6 @@ secondary_nap:
 kvm_no_guest:
 	li	r0, KVM_HWTHREAD_IN_NAP
 	stb	r0, HSTATE_HWTHREAD_STATE(r13)
-	li	r0, 0
-	std	r0, HSTATE_KVM_VCPU(r13)
 
 	li	r3, LPCR_PECE0
 	mfspr	r4, SPRN_LPCR
Signature · 1 line
1.6.0.2
#7 lore raw

PATCH 7/12 KVM: PPC: Book3s HV: Don't access runnable threads list without vcore lock

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

There were a few places where we were traversing the list of runnable threads in a virtual core, i.e. vc->runnable_threads, without holding the vcore spinlock. This extends the places where we hold the vcore spinlock to cover everywhere that we traverse that list.

Since we possibly need to sleep inside kvmppc_book3s_hv_page_fault, this moves the call of it from kvmppc_handle_exit out to kvmppc_vcpu_run, where we don't hold the vcore lock.

In kvmppc_vcore_blocked, we don't actually need to check whether all vcpus are ceded and don't have any pending exceptions, since the caller has already done that. The caller (kvmppc_run_vcpu) wasn't actually checking for pending exceptions, so we add that.

The change of if to while in kvmppc_run_vcpu is to make sure that we never call kvmppc_remove_runnable() when the vcore state is RUNNING or EXITING.

---
 arch/powerpc/include/asm/kvm_asm.h |    1 +
 arch/powerpc/kvm/book3s_hv.c       |   67 +++++++++++++++++------------------
 2 files changed, 34 insertions(+), 34 deletions(-)
arch/powerpc/include/asm/kvm_asm.h +1 -0
index 76fdcfe..aabcdba 100644
@@ -118,6 +118,7 @@
 
 #define RESUME_FLAG_NV          (1<<0)  /* Reload guest nonvolatile state? */
 #define RESUME_FLAG_HOST        (1<<1)  /* Resume host? */
+#define RESUME_FLAG_ARCH1	(1<<2)
 
 #define RESUME_GUEST            0
 #define RESUME_GUEST_NV         RESUME_FLAG_NV
arch/powerpc/kvm/book3s_hv.c +33 -34

168 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#8 lore raw

PATCH 8/12 KVM: PPC: Book3S HV: Fixes for late-joining threads

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

If a thread in a virtual core becomes runnable while other threads in the same virtual core are already running in the guest, it is possible for the latecomer to join the others on the core without first pulling them all out of the guest. Currently this only happens rarely, when a vcpu is first started. This fixes some bugs and omissions in the code in this case.

First, we need to check for VPA updates for the latecomer and make a DTL entry for it. Secondly, if it comes along while the master vcpu is doing a VPA update, we don't need to do anything since the master will pick it up in kvmppc_run_core. To handle this correctly we introduce a new vcore state, VCORE_STARTING. Thirdly, there is a race because we currently clear the hardware thread's hwthread_req before waiting to see it get to nap. A latecomer thread could have its hwthread_req cleared before it gets to test it, and therefore never increment the nap_count, leading to messages about wait_for_nap timeouts.

---
 arch/powerpc/include/asm/kvm_host.h |    7 ++++---
 arch/powerpc/kvm/book3s_hv.c        |   14 +++++++++++---
 2 files changed, 15 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/kvm_host.h +4 -3
index 68f5a30..218534d 100644
@@ -289,9 +289,10 @@ struct kvmppc_vcore {
 
 /* Values for vcore_state */
 #define VCORE_INACTIVE	0
-#define VCORE_RUNNING	1
-#define VCORE_EXITING	2
-#define VCORE_SLEEPING	3
+#define VCORE_SLEEPING	1
+#define VCORE_STARTING	2
+#define VCORE_RUNNING	3
+#define VCORE_EXITING	4
 
 /*
  * Struct used to manage memory for a virtual processor area
arch/powerpc/kvm/book3s_hv.c +11 -3
index 3a737a4..89995fa 100644
@@ -336,6 +336,11 @@ static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
 
 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
 {
+	if (!(vcpu->arch.vpa.update_pending ||
+	      vcpu->arch.slb_shadow.update_pending ||
+	      vcpu->arch.dtl.update_pending))
+		return;
+
 	spin_lock(&vcpu->arch.vpa_update_lock);
 	if (vcpu->arch.vpa.update_pending) {
 		kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
@@ -1009,7 +1014,7 @@ static void kvmppc_run_core(struct kvmppc_vcore *vc)
 	vc->n_woken = 0;
 	vc->nap_count = 0;
 	vc->entry_exit_count = 0;
-	vc->vcore_state = VCORE_RUNNING;
+	vc->vcore_state = VCORE_STARTING;
 	vc->in_guest = 0;
 	vc->napping_threads = 0;
 
@@ -1062,6 +1067,7 @@ static void kvmppc_run_core(struct kvmppc_vcore *vc)
 		kvmppc_create_dtl_entry(vcpu, vc);
 	}
 
+	vc->vcore_state = VCORE_RUNNING;
 	preempt_disable();
 	spin_unlock(&vc->lock);
 
@@ -1070,8 +1076,6 @@ static void kvmppc_run_core(struct kvmppc_vcore *vc)
 	srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
 
 	__kvmppc_vcore_entry(NULL, vcpu0);
-	for (i = 0; i < threads_per_core; ++i)
-		kvmppc_release_hwthread(vc->pcpu + i);
 
 	spin_lock(&vc->lock);
 	/* disable sending of IPIs on virtual external irqs */
@@ -1080,6 +1084,8 @@ static void kvmppc_run_core(struct kvmppc_vcore *vc)
 	/* wait for secondary threads to finish writing their state to memory */
 	if (vc->nap_count < vc->n_woken)
 		kvmppc_wait_for_nap(vc);
+	for (i = 0; i < threads_per_core; ++i)
+		kvmppc_release_hwthread(vc->pcpu + i);
 	/* prevent other vcpu threads from doing kvmppc_start_thread() now */
 	vc->vcore_state = VCORE_EXITING;
 	spin_unlock(&vc->lock);
@@ -1170,6 +1176,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	kvm_run->exit_reason = 0;
 	vcpu->arch.ret = RESUME_GUEST;
 	vcpu->arch.trap = 0;
+	kvmppc_update_vpas(vcpu);
 
 	/*
 	 * Synchronize with other threads in this virtual core
@@ -1193,6 +1200,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 		if (vc->vcore_state == VCORE_RUNNING &&
 		    VCORE_EXIT_COUNT(vc) == 0) {
 			vcpu->arch.ptid = vc->n_runnable - 1;
+			kvmppc_create_dtl_entry(vcpu, vc);
 			kvmppc_start_thread(vcpu);
 		}
 
Signature · 1 line
1.6.0.2
#9 lore raw

PATCH 9/12 KVM: PPC: Book3S HV: Run virtual core whenever any vcpus in it can run

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

Currently the Book3S HV code implements a policy on multi-threaded processors (i.e. POWER7) that requires all of the active vcpus in a virtual core to be ready to run before we run the virtual core. However, that causes problems on reset, because reset stops all vcpus except vcpu 0, and can also reduce throughput since all four threads in a virtual core have to wait whenever any one of them hits a hypervisor page fault.

This relaxes the policy, allowing the virtual core to run as soon as any vcpu in it is runnable. With this, the KVMPPC_VCPU_STOPPED state and the KVMPPC_VCPU_BUSY_IN_HOST state have been combined into a single KVMPPC_VCPU_NOTREADY state, since we no longer need to distinguish between them.

---
 arch/powerpc/include/asm/kvm_host.h |    5 +-
 arch/powerpc/kvm/book3s_hv.c        |   74 ++++++++++++++++++-----------------
 2 files changed, 40 insertions(+), 39 deletions(-)
arch/powerpc/include/asm/kvm_host.h +2 -3
index 218534d..1e8cbd1 100644
@@ -563,9 +563,8 @@ struct kvm_vcpu_arch {
 };
 
 /* Values for vcpu->arch.state */
-#define KVMPPC_VCPU_STOPPED		0
-#define KVMPPC_VCPU_BUSY_IN_HOST	1
-#define KVMPPC_VCPU_RUNNABLE		2
+#define KVMPPC_VCPU_NOTREADY		0
+#define KVMPPC_VCPU_RUNNABLE		1
 
 /* Values for vcpu->arch.io_gpr */
 #define KVM_MMIO_REG_MASK	0x001f
arch/powerpc/kvm/book3s_hv.c +38 -36

129 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#10 lore raw

PATCH 10/12 KVM: PPC: Book3S HV: Fix accounting of stolen time

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

Currently the code that accounts stolen time tends to overestimate the stolen time, and will sometimes report more stolen time in a DTL (dispatch trace log) entry than has elapsed since the last DTL entry. This can cause guests to underflow the user or system time measured for some tasks, leading to ridiculous CPU percentages and total runtimes being reported by top and other utilities.

In addition, the current code was designed for the previous policy where a vcore would only run when all the vcpus in it were runnable, and so only counted stolen time on a per-vcore basis. Now that a vcore can run while some of the vcpus in it are doing other things in the kernel (e.g. handling a page fault), we need to count the time when a vcpu task is preempted while it is not running as part of a vcore as stolen also.

To do this, we bring back the BUSY_IN_HOST vcpu state and extend the vcpu_load/put functions to count preemption time while the vcpu is in that state. Handling the transitions between the RUNNING and BUSY_IN_HOST states requires checking and updating two variables (accumulated time stolen and time last preempted), so we add a new spinlock, vcpu->arch.tbacct_lock. This protects both the per-vcpu stolen/preempt-time variables, and the per-vcore variables while this vcpu is running the vcore.

Finally, we now don't count time spent in userspace as stolen time. The task could be executing in userspace on behalf of the vcpu, or it could be preempted, or the vcpu could be genuinely stopped. Since we have no way of dividing up the time between these cases, we don't count any of it as stolen.

---
 arch/powerpc/include/asm/kvm_host.h |    5 ++
 arch/powerpc/kvm/book3s_hv.c        |  127 ++++++++++++++++++++++++++++++----
 2 files changed, 117 insertions(+), 15 deletions(-)
arch/powerpc/include/asm/kvm_host.h +5 -0
index 1e8cbd1..3093896 100644
@@ -559,12 +559,17 @@ struct kvm_vcpu_arch {
 	unsigned long dtl_index;
 	u64 stolen_logged;
 	struct kvmppc_vpa slb_shadow;
+
+	spinlock_t tbacct_lock;
+	u64 busy_stolen;
+	u64 busy_preempt;
 #endif
 };
 
 /* Values for vcpu->arch.state */
 #define KVMPPC_VCPU_NOTREADY		0
 #define KVMPPC_VCPU_RUNNABLE		1
+#define KVMPPC_VCPU_BUSY_IN_HOST	2
 
 /* Values for vcpu->arch.io_gpr */
 #define KVM_MMIO_REG_MASK	0x001f
arch/powerpc/kvm/book3s_hv.c +112 -15

231 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#11 lore raw

PATCH 11/12 KVM: PPC: Book3S HV: Allow DTL to be set to address 0, length 0

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

Commit 55b665b026 ("KVM: PPC: Book3S HV: Provide a way for userspace to get/set per-vCPU areas") includes a check on the length of the dispatch trace log (DTL) to make sure the buffer is at least one entry long. This is appropriate when registering a buffer, but the interface also allows for any existing buffer to be unregistered by specifying a zero address. In this case the length check is not appropriate. This makes the check conditional on the address being non-zero.

---
 arch/powerpc/kvm/book3s_hv.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/kvm/book3s_hv.c +2 -3
index 8b3c470..812764c 100644
@@ -811,9 +811,8 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
 		addr = val->vpaval.addr;
 		len = val->vpaval.length;
 		r = -EINVAL;
-		if (len < sizeof(struct dtl_entry))
-			break;
-		if (addr && !vcpu->arch.vpa.next_gpa)
+		if (addr && (len < sizeof(struct dtl_entry) ||
+			     !vcpu->arch.vpa.next_gpa))
 			break;
 		len -= len % sizeof(struct dtl_entry);
 		r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
Signature · 1 line
1.6.0.2
#12 lore raw

PATCH 12/12 KVM: PPC: Book3S HV: Fix thinko in try_lock_hpte()

In reply to Alexander Graf

From: Paul Mackerras <[email protected]>

This fixes an error in the inline asm in try_lock_hpte() where we were erroneously using a register number as an immediate operand. The bug only affects an error path, and in fact the code will still work as long as the compiler chooses some register other than r0 for the "bits" variable. Nevertheless it should still be fixed.

---
 arch/powerpc/include/asm/kvm_book3s_64.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +1 -1
index 0dd1d86..1472a5b 100644
@@ -60,7 +60,7 @@ static inline long try_lock_hpte(unsigned long *hpte, unsigned long bits)
 		     "	ori	%0,%0,%4\n"
 		     "  stdcx.	%0,0,%2\n"
 		     "	beq+	2f\n"
-		     "	li	%1,%3\n"
+		     "	mr	%1,%3\n"
 		     "2:	isync"
 		     : "=&r" (tmp), "=&r" (old)
 		     : "r" (hpte), "r" (bits), "i" (HPTE_V_HVLOCK)
Signature · 1 line
1.6.0.2
#13 lore raw
In reply to Alexander Graf
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

#14 lore raw
Quoted 11 lines · Marcelo Tosatti
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

Alex

#15 lore raw
Quoted 9 lines · Alexander Graf
Quoted 5 lines · Marcelo Tosatti
Quoted 4 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

next and queue should be compatible (queue = next + a few patches). Are there any conflicts when merging against queue?

Signature · 1 line
error compiling committee.c: too many arguments to function
#16 lore raw
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Alexander Graf
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

next and queue should be compatible (queue = next + a few patches). Are there any conflicts when merging against queue?

I merely want to know which one I should work against. So far I was under the impression that next is the one.

Alex

Quoted 4 lines
Signature · 2 lines
-- 
error compiling committee.c: too many arguments to function
#17 lore raw
Quoted 19 lines · Alexander Graf
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Alexander Graf
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

next and queue should be compatible (queue = next + a few patches). Are there any conflicts when merging against queue?

I merely want to know which one I should work against. So far I was under the impression that next is the one.

It is. queue might be rebased at times.

Signature · 1 line
error compiling committee.c: too many arguments to function
#18 lore raw
In reply to Avi Kivity
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Alexander Graf
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

next and queue should be compatible (queue = next + a few patches). Are there any conflicts when merging against queue?

There will be, and fsl_hcalls.h needs fixup (better Alexander verify i did not screw up anything else).

#20 lore raw
In reply to Alexander Graf
Quoted 11 lines · Alexander Graf
Quoted 7 lines · Marcelo Tosatti
Quoted 6 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

* Fix 440 target
* Fix uapi conflict

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

Alex

queue

#19 lore raw
In reply to Marcelo Tosatti
Quoted 18 lines · Marcelo Tosatti
Quoted 17 lines · Avi Kivity
Quoted 16 lines · Alexander Graf
Quoted 11 lines · Marcelo Tosatti
Quoted 8 lines · Alexander Graf

Hi Avi / Marcelo,

This is my current patch queue for ppc. Please pull.

Headline changes are:

* Fix 440 target
* Fix uapi conflict

Can you regenerate against queue branch? (btw i forgot to update fsl_hcalls.h...).

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Hrm. So which branch am I supposed to base against? Master, next or queue? This one is against next...

next and queue should be compatible (queue = next + a few patches). Are there any conflicts when merging against queue?

There will be, and fsl_hcalls.h needs fixup (better Alexander verify i did not screw up anything else).

I pushed a tree where I merged for-upstream and your queue and hopefully fixed up the whole mess. It's available as

for-queue

on my github tree.

Alex