Skip to contentSkip to conversation

kvm-ppc: halt secondary cpus when guest reset

PATCH Alexander Graf 3 participants 8 messages Jan 9, 2012 – Jan 10, 2012 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 56 lines · Scott Wood
Quoted 55 lines · Liu Yu

When guest reset, we need to halt secondary cpus until guest kick them. This already works for tcg. The patch add the support for kvm.

There are two cases for kvm:

1. If not use in-kernel mpic. We return env->halted in kvm_arch_process_async_events(), in order to tell kvm common code whether we request to halt.

2. If use in-kernel mpic. Even we request to halt,
the common code cpu_thread_is_idle() refuses to halt, because if vcpu halts while use in-kernel mpic, then there's no external interrupt handled in qemu to wake up the vcpu thread. In this case, we set stopped to pause the sencondaries instead, And resume secondaries when primary cpu access ppce500_spin.

---
v2:
Add comment and modify commit log

hw/ppce500_spin.c | 1 +
target-ppc/kvm.c | 15 ++++++++++++++-

2 files changed, 15 insertions(+), 1 deletions(-)
hw/ppce500_spin.c +1 -0
index cccd940..2b52728 100644
@@ -112,6 +112,7 @@ static void spin_kick(void *data)
 
    env->halted = 0;
    env->exception_index = -1;
+    env->stopped = 0;
    qemu_cpu_kick(env);
 }
 
target-ppc/kvm.c +9 -1
index 429349f..b7c70e2 100644
@@ -504,7 +504,20 @@ void kvm_arch_post_run(CPUState *env, struct kvm_run *run)
 
 int kvm_arch_process_async_events(CPUState *env)
 {
-    return 0;
+    if (kvm_irqchip_in_kernel()) {
+        if (env->halted == 1 && env->exception_index == EXCP_HLT) {
+            /* We're here because secondary vcpus are requested to halt
+             * before get kicked. But it's not allowed to halt vcpu when
+             * kvm_irqchip_in_kernel() is true. Instead, set stopped=1
+             * so that vcpu thread can be paused until guest kick them */
+            env->stopped = 1;
+            return 1;
+        } else {
Quoted 8 lines

target-ppc/kvm.c void kvm_arch_post_run(CPUState *env, struct kvm_run *run) · line 521

Quoted 6 lines
target-ppc/kvm.c +5 -0
+            return 0;
+        }
+    }
+
+    return env->halted;
 }

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

But x86 does some weird things with an mp_state variable that I have no clue what it's about. Jan, do you know what's going on here?

Alex

#3 lore raw
Quoted 68 lines · Alexander Graf
Quoted 56 lines · Scott Wood
Quoted 55 lines · Liu Yu

When guest reset, we need to halt secondary cpus until guest kick them. This already works for tcg. The patch add the support for kvm.

There are two cases for kvm:

1. If not use in-kernel mpic. We return env->halted in kvm_arch_process_async_events(), in order to tell kvm common code whether we request to halt.

2. If use in-kernel mpic. Even we request to halt,
the common code cpu_thread_is_idle() refuses to halt, because if vcpu halts while use in-kernel mpic, then there's no external interrupt handled in qemu to wake up the vcpu thread. In this case, we set stopped to pause the sencondaries instead, And resume secondaries when primary cpu access ppce500_spin.

---
v2:
Add comment and modify commit log

hw/ppce500_spin.c | 1 +
target-ppc/kvm.c | 15 ++++++++++++++-

2 files changed, 15 insertions(+), 1 deletions(-)
hw/ppce500_spin.c +1 -0
index cccd940..2b52728 100644
@@ -112,6 +112,7 @@ static void spin_kick(void *data)
 
    env->halted = 0;
    env->exception_index = -1;
+    env->stopped = 0;
    qemu_cpu_kick(env);
 }
 
target-ppc/kvm.c +9 -1
index 429349f..b7c70e2 100644
@@ -504,7 +504,20 @@ void kvm_arch_post_run(CPUState *env, struct kvm_run *run)
 
 int kvm_arch_process_async_events(CPUState *env)
 {
-    return 0;
+    if (kvm_irqchip_in_kernel()) {
+        if (env->halted == 1 && env->exception_index == EXCP_HLT) {
+            /* We're here because secondary vcpus are requested to halt
+             * before get kicked. But it's not allowed to halt vcpu when
+             * kvm_irqchip_in_kernel() is true. Instead, set stopped=1
+             * so that vcpu thread can be paused until guest kick them */
+            env->stopped = 1;
+            return 1;
+        } else {
Quoted 8 lines

target-ppc/kvm.c void kvm_arch_post_run(CPUState *env, struct kvm_run *run) · line 521

Quoted 6 lines
target-ppc/kvm.c +5 -0
+            return 0;
+        }
+    }
+
+    return env->halted;
 }

Alex, is there a better way to deal with the IRQ chip issue?

Quoted 1 line

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

-Scott

#4 lore raw
Quoted 84 lines · Scott Wood
Quoted 68 lines · Alexander Graf
Quoted 56 lines · Scott Wood
Quoted 55 lines · Liu Yu

When guest reset, we need to halt secondary cpus until guest kick them. This already works for tcg. The patch add the support for kvm.

There are two cases for kvm:

1. If not use in-kernel mpic. We return env->halted in kvm_arch_process_async_events(), in order to tell kvm common code whether we request to halt.

2. If use in-kernel mpic. Even we request to halt,
the common code cpu_thread_is_idle() refuses to halt, because if vcpu halts while use in-kernel mpic, then there's no external interrupt handled in qemu to wake up the vcpu thread. In this case, we set stopped to pause the sencondaries instead, And resume secondaries when primary cpu access ppce500_spin.

---
v2:
Add comment and modify commit log

hw/ppce500_spin.c | 1 +
target-ppc/kvm.c | 15 ++++++++++++++-

2 files changed, 15 insertions(+), 1 deletions(-)
hw/ppce500_spin.c +1 -0
index cccd940..2b52728 100644
@@ -112,6 +112,7 @@ static void spin_kick(void *data)
 
    env->halted = 0;
    env->exception_index = -1;
+    env->stopped = 0;
    qemu_cpu_kick(env);
 }
 
target-ppc/kvm.c +9 -1
index 429349f..b7c70e2 100644
@@ -504,7 +504,20 @@ void kvm_arch_post_run(CPUState *env, struct kvm_run *run)
 
 int kvm_arch_process_async_events(CPUState *env)
 {
-    return 0;
+    if (kvm_irqchip_in_kernel()) {
+        if (env->halted == 1 && env->exception_index == EXCP_HLT) {
+            /* We're here because secondary vcpus are requested to halt
+             * before get kicked. But it's not allowed to halt vcpu when
+             * kvm_irqchip_in_kernel() is true. Instead, set stopped=1
+             * so that vcpu thread can be paused until guest kick them */
+            env->stopped = 1;
+            return 1;
+        } else {
Quoted 8 lines

target-ppc/kvm.c void kvm_arch_post_run(CPUState *env, struct kvm_run *run) · line 521

Quoted 6 lines
target-ppc/kvm.c +5 -0
+            return 0;
+        }
+    }
+
+    return env->halted;
 }

Alex, is there a better way to deal with the IRQ chip issue?

Quoted 1 line

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

Quoted 2 lines

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#5 lore raw
Quoted 21 lines · Jan Kiszka
Quoted 20 lines · Scott Wood
Quoted 5 lines · Alexander Graf
Quoted 1 line · Scott Wood

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

Quoted 5 lines
Quoted 2 lines

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

We do for normal idling. This is a bit different, in that we're not waiting for an interrupt, but for an MMIO that releases the cpu at boot-time.

-Scott

#6 lore raw
Quoted 25 lines · Scott Wood
Quoted 24 lines · Jan Kiszka
Quoted 22 lines · Scott Wood
Quoted 5 lines · Alexander Graf
Quoted 1 line · Scott Wood

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Quoted 6 lines
Quoted 2 lines

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

We do for normal idling. This is a bit different, in that we're not waiting for an interrupt, but for an MMIO that releases the cpu at boot-time.

Where is the state stored that declares a VCPU to wait for that event? Where is it set, where removed?

What about implementing MP_STATE on PPC, at least those states that make sense? Don't you need that anyway for normal HALT<->RUNNABLE transitions?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#7 lore raw
Quoted 29 lines · Jan Kiszka
Quoted 28 lines · Scott Wood
Quoted 26 lines · Jan Kiszka
Quoted 20 lines · Scott Wood
Quoted 5 lines · Alexander Graf
Quoted 1 line · Scott Wood

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

Quoted 2 lines

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

Quoted 6 lines
Quoted 3 lines

We do for normal idling. This is a bit different, in that we're not waiting for an interrupt, but for an MMIO that releases the cpu at boot-time.

Where is the state stored that declares a VCPU to wait for that event? Where is it set, where removed?

It's set in the reset callback of the vcpu in QEMU. Basically we're starting all secondary CPUs in "waiting for an MMIO to come" state to not waste CPU cycles. On real hardware, secondaries are running in a tight loop trying to check if they should wake up.

When the primary is good to go, it will then flip a bit in MMIO space (on real hardware this is just RAM) which gives us an event to wake up a secondary.

Quoted 2 lines

What about implementing MP_STATE on PPC, at least those states that make sense? Don't you need that anyway for normal HALT<->RUNNABLE transitions?

The in-kernel code isn't upstream yet, so we're free to change things still. But yes, I guess doing it the same way as x86 does make sense here.

Alex

#8 lore raw
In reply to Jan Kiszka
Quoted 33 lines · Jan Kiszka
Quoted 25 lines · Scott Wood
Quoted 24 lines · Jan Kiszka
Quoted 22 lines · Scott Wood
Quoted 5 lines · Alexander Graf
Quoted 1 line · Scott Wood

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Quoted 6 lines
Quoted 2 lines

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

We do for normal idling. This is a bit different, in that we're not waiting for an interrupt, but for an MMIO that releases the cpu at boot-time.

Quoted 5 lines

Where is the state stored that declares a VCPU to wait for that event? Where is it set, where removed?

What about implementing MP_STATE on PPC, at least those states that make sense? Don't you need that anyway for normal HALT<->RUNNABLE transitions?

On ppc, normal halt/runnable transitions are handled entirely in the kernel, even without irqchip.

So, the idea is that on secondary VCPU creation, QEMU sets MP_STATE to KVM_MP_STATE_UNITIALIZED, and KVM will hold the thread idle until the MMIO is done and QEMU sets MP_STATE to KVM_MP_STATE_RUNNABLE? It seems excessive compared to QEMU being able to figure out for itself when it doesn't want to run a VCPU thread, when the decision is based entirely on things that are modeled in QEMU (which it will still need to do in the non-KVM case).

-Scott

#9 lore raw
Quoted 43 lines · Scott Wood
Quoted 33 lines · Jan Kiszka
Quoted 25 lines · Scott Wood
Quoted 24 lines · Jan Kiszka
Quoted 22 lines · Scott Wood
Quoted 5 lines · Alexander Graf
Quoted 1 line · Scott Wood

Alex, is there a better way to deal with the IRQ chip issue?

To be honest, I'm not sure what the issue really is.

If irqchip is enabled, env->halted won't result in a CPU being considered idle -- since QEMU won't see the interrupt that wakes the vcpu, and the idling is handled in the kernel. In this case we're waiting for MMIO rather than an interrupt, and it's the kernel that doesn't know what's going on.

It seems wrong to use env->stopped, though, as a spin-table release should not override a user's explicit request to stop a CPU. It might be OK (though a bit ugly) if the only usage of env->stopped is through pause_all_vcpus(), and the boot thread is the first one to be kicked (though in theory the boot cpu could wake another cpu, and that could wake a cpu that comes before it, causing a race with pause_all_vcpus()).

If it is OK to use env->stopped, is there any reason not to always use it (versus just with irqchip)?

Quoted 6 lines
Quoted 2 lines

Why don't you wait in the kernel with in-kernel irqchip under all condition (except pausing VCPUs, of course) on PPC? Just like x86 does.

We do for normal idling. This is a bit different, in that we're not waiting for an interrupt, but for an MMIO that releases the cpu at boot-time.

Quoted 5 lines

Where is the state stored that declares a VCPU to wait for that event? Where is it set, where removed?

What about implementing MP_STATE on PPC, at least those states that make sense? Don't you need that anyway for normal HALT<->RUNNABLE transitions?

On ppc, normal halt/runnable transitions are handled entirely in the kernel, even without irqchip.

Quoted 7 lines

So, the idea is that on secondary VCPU creation, QEMU sets MP_STATE to KVM_MP_STATE_UNITIALIZED, and KVM will hold the thread idle until the MMIO is done and QEMU sets MP_STATE to KVM_MP_STATE_RUNNABLE? It seems excessive compared to QEMU being able to figure out for itself when it doesn't want to run a VCPU thread, when the decision is based entirely on things that are modeled in QEMU (which it will still need to do in the non-KVM case).

I agree, but the closer we can stick with how x86 models it today the more generic code we have, the better.

Alex