Skip to contentSkip to conversation

disable the decrementer interrupt when a CPU is unplugged

PATCH Superseded Cédric Le Goater 2 participants 7 messages Nov 20, 2017 – Nov 22, 2017

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover disable the decrementer interrupt when a CPU is unplugged

Hello,

When a CPU is stopped with the 'stop-self' RTAS call, its state 'halted' is switched to 1 and, in this case, the MSR is not taken into account anymore in the cpu_has_work() routine. Only the pending hardware interrupts are checked with their LPCR:PECE* enablement bit.

If the DECR timer fires after 'stop-self' is called and before the CPU 'stop' state is reached, the nearly-dead CPU will have some work to do and the guest will crash. This case happens very frequently with the not yet upstream P9 XIVE exploitation mode. In XICS mode, the DECR is occasionally fired but after 'stop' state, so no work is to be done and the guest survives.

I suspect there is a race between the QEMU mainloop triggering the timers and the TCG CPU thread but I could not quite identify the root cause. To be safe, let's disable the decrementer interrupt in the LPCR when the CPU is halted and reenable it when the CPU is restarted. Reseting the MSR is now pointless, so remove this dubious workaround.

Thanks,

C.

Changes in v3:

- removed the ppc_cpu_pvr_match() routine testing the CPU family.
- introduced a cpu_ppc_papr_pece_bits() helper to gather the PECE bits depending on the CPU family.
- enabled Power-saving mode Exit Cause exceptions only on the boot CPU.

Changes in v2:

- used a new routine ppc_cpu_pvr_match() to discriminate CPU versions
- removed the LPCR:PECE* enablement bit when the CPU is initialized if it is a secondary
- included Nikunj's fix to reboot SMP TCG guests

Cédric Le Goater (3):
spapr/rtas: disable the decrementer interrupt when a CPU is unplugged spapr/rtas: fix reboot of a a SMP TCG guest
spapr/rtas: do not reset the MSR in stop-self command

 hw/ppc/spapr_cpu_core.c     |  7 +++++++
 hw/ppc/spapr_rtas.c         | 19 +++++++++----------
 target/ppc/cpu.h            |  1 +
 target/ppc/translate_init.c | 33 +++++++++++++++++++++++++--------
 4 files changed, 42 insertions(+), 18 deletions(-)
Signature · 1 line
2.13.6
#1 lore raw

PATCH v3 1/3 spapr/rtas: disable the decrementer interrupt when a CPU is unplugged

When a CPU is stopped with the 'stop-self' RTAS call, its state 'halted' is switched to 1 and, in this case, the MSR is not taken into account anymore in the cpu_has_work() routine. Only the pending hardware interrupts are checked with their LPCR:PECE* enablement bit.

If the DECR timer fires after 'stop-self' is called and before the CPU 'stop' state is reached, the nearly-dead CPU will have some work to do and the guest will crash. This case happens very frequently with the not yet upstream P9 XIVE exploitation mode. In XICS mode, the DECR is occasionally fired but after 'stop' state, so no work is to be done and the guest survives.

I suspect there is a race between the QEMU mainloop triggering the timers and the TCG CPU thread but I could not quite identify the root cause. To be safe, let's disable in the LPCR all the exceptions which can cause an exit while the CPU is in power-saving mode and reenable them when the CPU is started.

For this purpose, we introduce a little helper routine to calculate the PECE bits for a processor variant. We could also use the mask value LPCR_PECE_L_MASK for the P8 and P9 processors. bit 47 and 48 are reserved on P7 but it is still compatible.

---

Changes in v3:

- introduced a cpu_ppc_papr_pece_bits() helper to gather the PECE bits depending on the CPU family.
- enabled Power-saving mode Exit Cause exceptions only on the boot CPU.

Changes in v2:

- used a new routine ppc_cpu_pvr_match() to discriminate CPU versions
- removed the LPCR:PECE* enablement bit when the CPU is initialized if it is a secondary

 hw/ppc/spapr_rtas.c         |  9 +++++++++
 target/ppc/cpu.h            |  1 +
 target/ppc/translate_init.c | 33 +++++++++++++++++++++++++--------
 3 files changed, 35 insertions(+), 8 deletions(-)
hw/ppc/spapr_rtas.c +9 -0
index cdf0b607a0a0..b5cff3ab29d3 100644
@@ -174,6 +174,10 @@ static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
         kvm_cpu_synchronize_state(cs);
 
         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
+
+        /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
+        env->spr[SPR_LPCR] |= cpu_ppc_papr_pece_bits(env);
+
         env->nip = start;
         env->gpr[3] = r3;
         cs->halted = 0;
@@ -210,6 +214,11 @@ static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
      * no need to bother with specific bits, we just clear it.
      */
     env->msr = 0;
+
+    /* Disable Power-saving mode Exit Cause exceptions for the CPU.
+     * This could deliver an interrupt on a dying CPU and crash the
+     * guest */
+    env->spr[SPR_LPCR] &= ~cpu_ppc_papr_pece_bits(env);
 }
 
 static inline int sysparm_st(target_ulong addr, target_ulong len,
target/ppc/cpu.h +1 -0
index 989761b79569..7c84344421f3 100644
@@ -1327,6 +1327,7 @@ void store_booke_tsr (CPUPPCState *env, target_ulong val);
 void ppc_tlb_invalidate_all (CPUPPCState *env);
 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr);
 void cpu_ppc_set_papr(PowerPCCPU *cpu, PPCVirtualHypervisor *vhyp);
+target_ulong cpu_ppc_papr_pece_bits(CPUPPCState *env);
 #endif
 #endif
 
target/ppc/translate_init.c +25 -8
index b9c49c22f29f..a0bf5e01dc52 100644
@@ -8901,11 +8901,28 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data)
 }
 
 #if !defined(CONFIG_USER_ONLY)
+
+target_ulong cpu_ppc_papr_pece_bits(CPUPPCState *env)
+{
+    switch (env->mmu_model) {
+    case POWERPC_MMU_3_00:
+        return LPCR_PDEE | LPCR_HDEE | LPCR_EEE | LPCR_DEE | LPCR_OEE;
+    default:
+        /* P7 and P8 has slightly different PECE bits, mostly because P8 adds
+         * bit 47 and 48 which are reserved on P7. Here we set them all, which
+         * will work as expected for both implementations
+         */
+        return LPCR_P8_PECE0 | LPCR_P8_PECE1 | LPCR_P8_PECE2 | LPCR_P8_PECE3 |
+            LPCR_P8_PECE4;
+    }
+}
+
 void cpu_ppc_set_papr(PowerPCCPU *cpu, PPCVirtualHypervisor *vhyp)
 {
     CPUPPCState *env = &cpu->env;
     ppc_spr_t *lpcr = &env->spr_cb[SPR_LPCR];
     ppc_spr_t *amor = &env->spr_cb[SPR_AMOR];
+    CPUState *cs = CPU(cpu);
 
     cpu->vhyp = vhyp;
 
@@ -8947,16 +8964,16 @@ void cpu_ppc_set_papr(PowerPCCPU *cpu, PPCVirtualHypervisor *vhyp)
         } else {
             lpcr->default_value &= ~(LPCR_UPRT | LPCR_GTSE);
         }
-        lpcr->default_value |= LPCR_PDEE | LPCR_HDEE | LPCR_EEE | LPCR_DEE |
-                               LPCR_OEE;
         break;
     default:
-        /* P7 and P8 has slightly different PECE bits, mostly because P8 adds
-         * bit 47 and 48 which are reserved on P7. Here we set them all, which
-         * will work as expected for both implementations
-         */
-        lpcr->default_value |= LPCR_P8_PECE0 | LPCR_P8_PECE1 | LPCR_P8_PECE2 |
-                               LPCR_P8_PECE3 | LPCR_P8_PECE4;
+        ;
+    }
+
+    /* Only enable Power-saving mode Exit Cause exceptions on the boot
+     * CPU. The RTAS command start-cpu will enable them on secondaries.
+     */
+    if (cs == first_cpu) {
+        lpcr->default_value |= cpu_ppc_papr_pece_bits(env);
     }
 
     /* We should be followed by a CPU reset but update the active value
Signature · 1 line
2.13.6
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v3 1/3] spapr/rtas: disable the decrementer interrupt when a CPU is unplugged

Quoted 18 lines · Cédric Le Goater

When a CPU is stopped with the 'stop-self' RTAS call, its state 'halted' is switched to 1 and, in this case, the MSR is not taken into account anymore in the cpu_has_work() routine. Only the pending hardware interrupts are checked with their LPCR:PECE* enablement bit.

If the DECR timer fires after 'stop-self' is called and before the CPU 'stop' state is reached, the nearly-dead CPU will have some work to do and the guest will crash. This case happens very frequently with the not yet upstream P9 XIVE exploitation mode. In XICS mode, the DECR is occasionally fired but after 'stop' state, so no work is to be done and the guest survives.

I suspect there is a race between the QEMU mainloop triggering the timers and the TCG CPU thread but I could not quite identify the root cause. To be safe, let's disable in the LPCR all the exceptions which can cause an exit while the CPU is in power-saving mode and reenable them when the CPU is started.

Quoted 6 lines

For this purpose, we introduce a little helper routine to calculate the PECE bits for a processor variant. We could also use the mask value LPCR_PECE_L_MASK for the P8 and P9 processors. bit 47 and 48 are reserved on P7 but it is still compatible.

I'm not thrilled about addressing this without 100% knowing what's going on, but this seems like a sensible change in any case, so I'm ok with applying something like this.

A detail however..

[snip]

Quoted 6 lines

#if !defined(CONFIG_USER_ONLY)

target/ppc/translate_init.c +5 -0
+
+target_ulong cpu_ppc_papr_pece_bits(CPUPPCState *env)
+{
+    switch (env->mmu_model) {
+    case POWERPC_MMU_3_00:

target/ppc/translate_init.c POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data) · line 8918

Quoted 10 lines
target/ppc/translate_init.c +10 -0
+        return LPCR_PDEE | LPCR_HDEE | LPCR_EEE | LPCR_DEE | LPCR_OEE;
+    default:
+        /* P7 and P8 has slightly different PECE bits, mostly because P8 adds
+         * bit 47 and 48 which are reserved on P7. Here we set them all, which
+         * will work as expected for both implementations
+         */
+        return LPCR_P8_PECE0 | LPCR_P8_PECE1 | LPCR_P8_PECE2 | LPCR_P8_PECE3 |
+            LPCR_P8_PECE4;
+    }
+}

..since we're working in this area, might as well clean up this inappropriate use of mmu_model. Two options which I'd be ok with:

1) Add a pece_bits field to the PowerPCCPUClass, correctly initialized for the various processors.

2) A similar helper but using ppc_check_compat() to check the arch level, instead of using env->mmu_model.

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
#3 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v3 1/3] spapr/rtas: disable the decrementer interrupt when a CPU is unplugged

Quoted 24 lines · David Gibson
Quoted 23 lines · Cédric Le Goater

When a CPU is stopped with the 'stop-self' RTAS call, its state 'halted' is switched to 1 and, in this case, the MSR is not taken into account anymore in the cpu_has_work() routine. Only the pending hardware interrupts are checked with their LPCR:PECE* enablement bit.

If the DECR timer fires after 'stop-self' is called and before the CPU 'stop' state is reached, the nearly-dead CPU will have some work to do and the guest will crash. This case happens very frequently with the not yet upstream P9 XIVE exploitation mode. In XICS mode, the DECR is occasionally fired but after 'stop' state, so no work is to be done and the guest survives.

I suspect there is a race between the QEMU mainloop triggering the timers and the TCG CPU thread but I could not quite identify the root cause. To be safe, let's disable in the LPCR all the exceptions which can cause an exit while the CPU is in power-saving mode and reenable them when the CPU is started.

For this purpose, we introduce a little helper routine to calculate the PECE bits for a processor variant. We could also use the mask value LPCR_PECE_L_MASK for the P8 and P9 processors. bit 47 and 48 are reserved on P7 but it is still compatible.

Quoted 4 lines
Quoted 1 line

I'm not thrilled about addressing this without 100% knowing what's going on,

me either :/ I have spent hours, days, on QEMU logs trying to catch a possible race in the state machine of the CPUs and didn't find it. I need a better understanding of the internals.

Quoted 26 lines

but this seems like a sensible change in any case, so I'm ok with applying something like this.

A detail however..

[snip]

Quoted 6 lines

#if !defined(CONFIG_USER_ONLY)

target/ppc/translate_init.c +5 -0
+
+target_ulong cpu_ppc_papr_pece_bits(CPUPPCState *env)
+{
+    switch (env->mmu_model) {
+    case POWERPC_MMU_3_00:

target/ppc/translate_init.c POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data) · line 8918

Quoted 10 lines
target/ppc/translate_init.c +10 -0
+        return LPCR_PDEE | LPCR_HDEE | LPCR_EEE | LPCR_DEE | LPCR_OEE;
+    default:
+        /* P7 and P8 has slightly different PECE bits, mostly because P8 adds
+         * bit 47 and 48 which are reserved on P7. Here we set them all, which
+         * will work as expected for both implementations
+         */
+        return LPCR_P8_PECE0 | LPCR_P8_PECE1 | LPCR_P8_PECE2 | LPCR_P8_PECE3 |
+            LPCR_P8_PECE4;
+    }
+}

..since we're working in this area, might as well clean up this inappropriate use of mmu_model. Two options which I'd be ok with:

Quoted 6 lines

1) Add a pece_bits field to the PowerPCCPUClass, correctly initialized for the various processors.

2) A similar helper but using ppc_check_compat() to check the arch level, instead of using env->mmu_model.

OK.

Thanks,

C.

#4 lore raw

PATCH v3 2/3 spapr/rtas: fix reboot of a a SMP TCG guest

In reply to Cédric Le Goater

Just like for hot unplug CPUs, when a guest is rebooted, the secondary CPUs can be awaken by the decrementer and start entering SLOF at the same time the boot CPU is.

To be safe, let's disable on the secondaries all the exceptions which can cause an exit while the CPU is in power-saving mode.

Based on previous work from Nikunj A Dadhania <[email protected]>

---

Changes in v3:

- used the cpu_ppc_papr_pece_bits() helper

 hw/ppc/spapr_cpu_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr_cpu_core.c +7 -0
index 3a4c17401226..4ba8563d49e4 100644
@@ -35,6 +35,13 @@ static void spapr_cpu_reset(void *opaque)
     cs->halted = 1;
 
     env->spr[SPR_HIOR] = 0;
+
+    /* Disable Power-saving mode Exit Cause exceptions for the CPU.
+     * This can cause issues when rebooting the guest if a secondary
+     * is awaken */
+    if (cs != first_cpu) {
+        env->spr[SPR_LPCR] &= ~cpu_ppc_papr_pece_bits(env);
+    }
 }
 
 static void spapr_cpu_destroy(PowerPCCPU *cpu)
Signature · 1 line
2.13.6
Reviewed-by on v3 2/3
#5 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v3 2/3] spapr/rtas: fix reboot of a a SMP TCG guest

Quoted 10 lines · Cédric Le Goater

Just like for hot unplug CPUs, when a guest is rebooted, the secondary CPUs can be awaken by the decrementer and start entering SLOF at the same time the boot CPU is.

To be safe, let's disable on the secondaries all the exceptions which can cause an exit while the CPU is in power-saving mode.

Based on previous work from Nikunj A Dadhania <[email protected]>

but not applying pending an update on 1/3.

Quoted 27 lines

---

Changes in v3:

- used the cpu_ppc_papr_pece_bits() helper

 hw/ppc/spapr_cpu_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr_cpu_core.c +7 -0
index 3a4c17401226..4ba8563d49e4 100644
@@ -35,6 +35,13 @@ static void spapr_cpu_reset(void *opaque)
     cs->halted = 1;
 
     env->spr[SPR_HIOR] = 0;
+
+    /* Disable Power-saving mode Exit Cause exceptions for the CPU.
+     * This can cause issues when rebooting the guest if a secondary
+     * is awaken */
+    if (cs != first_cpu) {
+        env->spr[SPR_LPCR] &= ~cpu_ppc_papr_pece_bits(env);
+    }
 }
 
 static void spapr_cpu_destroy(PowerPCCPU *cpu)
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
#6 lore raw

PATCH v3 3/3 spapr/rtas: do not reset the MSR in stop-self command

In reply to Cédric Le Goater

When a CPU is stopped with the 'stop-self' RTAS call, its state 'halted' is switched to 1 and, in this case, the MSR is not taken into account anymore in the cpu_has_work() routine. Only the pending hardware interrupts are checked with their LPCR:PECE* enablement bit.

The CPU is now also protected from the decrementer interrupt by the LPCR:PECE* bits which are disabled in the 'stop-self' RTAS call. Reseting the MSR is pointless.

---
 hw/ppc/spapr_rtas.c | 10 ----------
 1 file changed, 10 deletions(-)
hw/ppc/spapr_rtas.c +0 -10
index b5cff3ab29d3..7ddddc1c9c8a 100644
@@ -204,16 +204,6 @@ static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 
     cs->halted = 1;
     qemu_cpu_kick(cs);
-    /*
-     * While stopping a CPU, the guest calls H_CPPR which
-     * effectively disables interrupts on XICS level.
-     * However decrementer interrupts in TCG can still
-     * wake the CPU up so here we disable interrupts in MSR
-     * as well.
-     * As rtas_start_cpu() resets the whole MSR anyway, there is
-     * no need to bother with specific bits, we just clear it.
-     */
-    env->msr = 0;
 
     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
      * This could deliver an interrupt on a dying CPU and crash the
Signature · 1 line
2.13.6