Skip to contentSkip to conversation

ppcr: Avoid decrementer related kvm exits

PATCH In review David Gibson 3 participants 8 messages Oct 14, 2011 – Oct 28, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ppcr: Avoid decrementer related kvm exits

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
 hw/ppc.c |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)
hw/ppc.c +14 -11
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
                                   int is_excp)
 {
     ppc_tb_t *tb_env = env->tb_env;
-    uint64_t now, next;
 
     LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
                 decr, value);
-    now = qemu_get_clock_ns(vm_clock);
-    next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
-    if (is_excp) {
-        next += *nextp - now;
-    }
-    if (next == now) {
-        next++;
+    if (!kvm_enabled()) {
+        uint64_t now, next;
+
+        now = qemu_get_clock_ns(vm_clock);
+        next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
+        if (is_excp) {
+            next += *nextp - now;
+        }
+        if (next == now) {
+            next++;
+        }
+        *nextp = next;
+        /* Adjust timer */
+        qemu_mod_timer(timer, next);
     }
-    *nextp = next;
-    /* Adjust timer */
-    qemu_mod_timer(timer, next);
 
     /* If we set a negative value and the decrementer was positive, raise an
      * exception.
Signature · 1 line
1.7.6.3
#1 lore raw

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Quoted 21 lines · David Gibson

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
hw/ppc.c | 25 ++++++++++++++-----------

1 files changed, 14 insertions(+), 11 deletions(-)
hw/ppc.c +0 -0
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Do we ever call store_decr in the kvm case? Isn't that only called from emulated mtdec?

Alex

Quoted 38 lines
hw/ppc.c +14 -11
                                  int is_excp)
 {
    ppc_tb_t *tb_env = env->tb_env;
-    uint64_t now, next;
 
    LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
                decr, value);
-    now = qemu_get_clock_ns(vm_clock);
-    next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
-    if (is_excp) {
-        next += *nextp - now;
-    }
-    if (next == now) {
-        next++;
+    if (!kvm_enabled()) {
+        uint64_t now, next;
+
+        now = qemu_get_clock_ns(vm_clock);
+        next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
+        if (is_excp) {
+            next += *nextp - now;
+        }
+        if (next == now) {
+            next++;
+        }
+        *nextp = next;
+        /* Adjust timer */
+        qemu_mod_timer(timer, next);
    }
-    *nextp = next;
-    /* Adjust timer */
-    qemu_mod_timer(timer, next);
 
    /* If we set a negative value and the decrementer was positive, raise an
     * exception.
Signature · 1 line
1.7.6.3
#2 lore raw
Quoted 19 lines · Alexander Graf
Quoted 16 lines · David Gibson

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
hw/ppc.c | 25 ++++++++++++++-----------

1 files changed, 14 insertions(+), 11 deletions(-)
Quoted 8 lines

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Quoted 5 lines
hw/ppc.c +0 -0
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Do we ever call store_decr in the kvm case? Isn't that only called from emulated mtdec?

Yes, from cpu_ppc_set_tb_clk(). Anton observed the kvm exits in the wild, they're not theoretical.

Agh, which reminds me, I forgot to fixup the git author again. The patch should show authorship by Anton Blanchard <[email protected]>, as in the s-o-b.

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
Quoted 29 lines · David Gibson
Quoted 19 lines · Alexander Graf
Quoted 16 lines · David Gibson

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
hw/ppc.c | 25 ++++++++++++++-----------

1 files changed, 14 insertions(+), 11 deletions(-)
Quoted 8 lines

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Quoted 5 lines
hw/ppc.c +0 -0
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Do we ever call store_decr in the kvm case? Isn't that only called from emulated mtdec?

Quoted 6 lines

Yes, from cpu_ppc_set_tb_clk(). Anton observed the kvm exits in the wild, they're not theoretical.

Agh, which reminds me, I forgot to fixup the git author again. The patch should show authorship by Anton Blanchard <[email protected]>, as in the s-o-b.

Wouldn't a simple

if (kvm_enabled()) {
    return;
}

in the beginning of the function make more sense? There's no code connecting the in-qemu and the in-kvm decrementors atm, so any logic applying to the in-qemu one is moot for kvm.

Alex

#4 lore raw
Quoted 41 lines · Alexander Graf
Quoted 29 lines · David Gibson
Quoted 19 lines · Alexander Graf
Quoted 16 lines · David Gibson

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
hw/ppc.c | 25 ++++++++++++++-----------

1 files changed, 14 insertions(+), 11 deletions(-)
Quoted 8 lines

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Quoted 5 lines
hw/ppc.c +0 -0
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Do we ever call store_decr in the kvm case? Isn't that only called from emulated mtdec?

Quoted 6 lines

Yes, from cpu_ppc_set_tb_clk(). Anton observed the kvm exits in the wild, they're not theoretical.

Agh, which reminds me, I forgot to fixup the git author again. The patch should show authorship by Anton Blanchard <[email protected]>, as in the s-o-b.

Wouldn't a simple

Quoted 7 lines
if (kvm_enabled()) {
    return;
}

in the beginning of the function make more sense? There's no code connecting the in-qemu and the in-kvm decrementors atm, so any logic applying to the in-qemu one is moot for kvm.

Uh.. I guess so. I wasn't 100% sure the last bit of code in the function wouldn't have some effect on kvm. But I guess it doesn't; I'll revise.

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
#5 lore raw
Quoted 46 lines · David Gibson
Quoted 45 lines · Alexander Graf
Quoted 29 lines · David Gibson
Quoted 19 lines · Alexander Graf
Quoted 16 lines · David Gibson

In __cpu_ppc_store_decr(), we set up a regular timer used to trigger decrementer interrupts. This is necessary to implement the decrementer properly under TCG, but is unnecessary under KVM (true for both Book3S-PR and Book3S-HV KVM variants), because the kernel handles generating and delivering decrementer exceptions.

Under kvm, in fact, the timer causes expensive and unnecessary exits from kvm to qemu. This patch, therefore, disables setting the timer when kvm is in use.

---
hw/ppc.c | 25 ++++++++++++++-----------

1 files changed, 14 insertions(+), 11 deletions(-)
Quoted 8 lines

hw/ppc.c static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Quoted 5 lines
hw/ppc.c +0 -0
index 25b59dd..87aa4e5 100644
@@ -658,21 +658,24 @@ static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,

Do we ever call store_decr in the kvm case? Isn't that only called from emulated mtdec?

Quoted 6 lines

Yes, from cpu_ppc_set_tb_clk(). Anton observed the kvm exits in the wild, they're not theoretical.

Agh, which reminds me, I forgot to fixup the git author again. The patch should show authorship by Anton Blanchard <[email protected]>, as in the s-o-b.

Wouldn't a simple

if (kvm_enabled()) {
    return;
}
Quoted 7 lines
Quoted 3 lines

in the beginning of the function make more sense? There's no code connecting the in-qemu and the in-kvm decrementors atm, so any logic applying to the in-qemu one is moot for kvm.

Uh.. I guess so. I wasn't 100% sure the last bit of code in the function wouldn't have some effect on kvm. But I guess it doesn't; I'll revise.

Revised patch sent.

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
In reply to Alexander Graf
Quoted 7 lines · Alexander Graf

Wouldn't a simple

if (kvm_enabled()) {
    return;
}

in the beginning of the function make more sense? There's no code connecting the in-qemu and the in-kvm decrementors atm, so any logic applying to the in-qemu one is moot for kvm.

On book3e at least, we can use sregs to set the decrementer, and we probably want this to happen on reset.

-Scott

#7 lore raw
Quoted 11 lines · Scott Wood
Quoted 7 lines · Alexander Graf

Wouldn't a simple

if (kvm_enabled()) {
   return;
}

in the beginning of the function make more sense? There's no code connecting the in-qemu and the in-kvm decrementors atm, so any logic applying to the in-qemu one is moot for kvm.

On book3e at least, we can use sregs to set the decrementer, and we probably want this to happen on reset.

Sure.

if (kvm_enabled()) {
    kvmppc_set_dec(x);
    return;
}

Alex