Skip to contentSkip to conversation

mips: Minor simplifications for KVM use

PATCH In review Philippe Mathieu-Daudé 5 participants 13 messages Apr 29, 2020 – Dec 7, 2020

lore
Share & tools
lore
#0 lore raw

cover mips: Minor simplifications for KVM use

A pair of trivial patches while reviewing Huacai's "KVM target support for MIPS64" series.

Philippe Mathieu-Daudé (2):
hw/mips/mips_int: De-duplicate KVM interrupt delivery target/mips/kvm: Assert unreachable code is not used

 hw/mips/mips_int.c | 11 +++--------
 target/mips/kvm.c  |  8 ++------
 2 files changed, 5 insertions(+), 14 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH 1/2 hw/mips/mips_int: De-duplicate KVM interrupt delivery

Refactor duplicated code in a single place.

---
 hw/mips/mips_int.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
hw/mips/mips_int.c +3 -8
index 796730b11d..4a1bf846da 100644
@@ -47,17 +47,12 @@ static void cpu_mips_irq_request(void *opaque, int irq, int level)
 
     if (level) {
         env->CP0_Cause |= 1 << (irq + CP0Ca_IP);
-
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
-
     } else {
         env->CP0_Cause &= ~(1 << (irq + CP0Ca_IP));
+    }
 
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
+    if (kvm_enabled() && irq == 2) {
+        kvm_mips_set_interrupt(cpu, irq, level);
     }
 
     if (env->CP0_Cause & CP0Ca_IP_mask) {
Signature · 1 line
2.21.1
#2 lore raw

Re: [PATCH 1/2] hw/mips/mips_int: De-duplicate KVM interrupt delivery

Hi, Philippe,

Quoted 14 lines · Philippe Mathieu-Daudé

Refactor duplicated code in a single place.

---
 hw/mips/mips_int.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
hw/mips/mips_int.c +0 -0
index 796730b11d..4a1bf846da 100644
@@ -47,17 +47,12 @@ static void cpu_mips_irq_request(void *opaque, int irq, int level)
 

hw/mips/mips_int.c static void cpu_mips_irq_request(void *opaque, int irq, int level) · line 52

Quoted 10 lines
hw/mips/mips_int.c +1 -5
     if (level) {
         env->CP0_Cause |= 1 << (irq + CP0Ca_IP);
-
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
-
     } else {
         env->CP0_Cause &= ~(1 << (irq + CP0Ca_IP));
+    }

Since the if-else has become one line, so can we remove { and } here?

Quoted 13 lines
hw/mips/mips_int.c +2 -4
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
+    if (kvm_enabled() && irq == 2) {
+        kvm_mips_set_interrupt(cpu, irq, level);
     }
 
     if (env->CP0_Cause & CP0Ca_IP_mask) {
--

2.21.1

Signature · 1 line
Huacai Chen
#3 lore raw

Re: [PATCH 1/2] hw/mips/mips_int: De-duplicate KVM interrupt delivery

Quoted 17 lines · chen huacai

Hi, Philippe,

Quoted 14 lines · Philippe Mathieu-Daudé

Refactor duplicated code in a single place.

---
  hw/mips/mips_int.c | 11 +++--------
  1 file changed, 3 insertions(+), 8 deletions(-)
hw/mips/mips_int.c +0 -0
index 796730b11d..4a1bf846da 100644
@@ -47,17 +47,12 @@ static void cpu_mips_irq_request(void *opaque, int irq, int level)
 
Quoted 11 lines

hw/mips/mips_int.c static void cpu_mips_irq_request(void *opaque, int irq, int level) · line 52

Quoted 10 lines
hw/mips/mips_int.c +1 -5
      if (level) {
          env->CP0_Cause |= 1 << (irq + CP0Ca_IP);
-
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
-
      } else {
          env->CP0_Cause &= ~(1 << (irq + CP0Ca_IP));
+    }

Since the if-else has become one line, so can we remove { and } here?

This is the QEMU coding style, see CODING_STYLE.rst:

Block structure
===============

Every indented statement is braced; even if the block contains just one statement. The opening brace is on the line that contains the control flow statement that introduces the new block; the closing brace is on the same line as the else keyword, or on a line by itself if there is no else keyword. Example:

.. code-block:: c

    if (a == 5) {
        printf("a was 5.\n");
    } else if (a == 6) {
        printf("a was 6.\n");
    } else {
        printf("a was something else entirely.\n");
    }

Rationale: a consistent (except for functions...) bracing style reduces ambiguity and avoids needless churn when lines are added or removed. Furthermore, it is the QEMU coding style.

Quoted 16 lines
Quoted 14 lines
hw/mips/mips_int.c +2 -4
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
+    if (kvm_enabled() && irq == 2) {
+        kvm_mips_set_interrupt(cpu, irq, level);
      }
 
      if (env->CP0_Cause & CP0Ca_IP_mask) {
--

2.21.1

Reviewed-by
#4 lore raw

Re: [PATCH 1/2] hw/mips/mips_int: De-duplicate KVM interrupt delivery

Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 37 lines · chen huacai

Hi, Philippe,

Quoted 33 lines · Philippe Mathieu-Daudé

Refactor duplicated code in a single place.

---
  hw/mips/mips_int.c | 11 +++--------

  1 file changed, 3 insertions(+), 8 deletions(-)
hw/mips/mips_int.c +3 -8
index 796730b11d..4a1bf846da 100644
@@ -47,17 +47,12 @@ static void cpu_mips_irq_request(void *opaque,
 int irq, int level)
 
       if (level) {
           env->CP0_Cause |= 1 << (irq + CP0Ca_IP);
-
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
-
       } else {
           env->CP0_Cause &= ~(1 << (irq + CP0Ca_IP));
+    }
 
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
+    if (kvm_enabled() && irq == 2) {
+        kvm_mips_set_interrupt(cpu, irq, level);
       }
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

      if (env->CP0_Cause & CP0Ca_IP_mask) {

Signature · 1 line
2.21.1
#5 lore raw

Re: [PATCH 1/2] hw/mips/mips_int: De-duplicate KVM interrupt delivery

Quoted 39 lines · Thomas Huth
Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 37 lines · chen huacai

Hi, Philippe,

Quoted 33 lines · Philippe Mathieu-Daudé

Refactor duplicated code in a single place.

---
  hw/mips/mips_int.c | 11 +++--------

  1 file changed, 3 insertions(+), 8 deletions(-)
hw/mips/mips_int.c +3 -8
index 796730b11d..4a1bf846da 100644
@@ -47,17 +47,12 @@ static void cpu_mips_irq_request(void *opaque,
 int irq, int level)
 
       if (level) {
           env->CP0_Cause |= 1 << (irq + CP0Ca_IP);
-
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
-
       } else {
           env->CP0_Cause &= ~(1 << (irq + CP0Ca_IP));
+    }
 
-        if (kvm_enabled() && irq == 2) {
-            kvm_mips_set_interrupt(cpu, irq, level);
-        }
+    if (kvm_enabled() && irq == 2) {
+        kvm_mips_set_interrupt(cpu, irq, level);
       }
 
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

      if (env->CP0_Cause & CP0Ca_IP_mask) {

Signature · 1 line
2.21.1

Thanks, queued to mips-next.

#6 lore raw

PATCH 2/2 target/mips/kvm: Assert unreachable code is not used

In reply to Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
 target/mips/kvm.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +2 -6
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq, int level)
     CPUState *cs = CPU(cpu);
     struct kvm_mips_interrupt intr;
 
-    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
 
     intr.cpu = -1;
 
@@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int irq, int level)
     CPUState *dest_cs = CPU(cpu);
     struct kvm_mips_interrupt intr;
 
-    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
 
     intr.cpu = dest_cs->cpu_index;
 
Signature · 1 line
2.21.1
#7 lore raw

Re: [PATCH 2/2] target/mips/kvm: Assert unreachable code is not used

+Paolo

Quoted 34 lines · Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
  target/mips/kvm.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +2 -6
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq, int level)
      CPUState *cs = CPU(cpu);
      struct kvm_mips_interrupt intr;
 
-    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
 
      intr.cpu = -1;
 
@@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int irq, int level)
      CPUState *dest_cs = CPU(cpu);
      struct kvm_mips_interrupt intr;
 
-    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
 
      intr.cpu = dest_cs->cpu_index;
 
#8 lore raw

Re: [PATCH 2/2] target/mips/kvm: Assert unreachable code is not used

Huacai, ping?

Quoted 34 lines · Philippe Mathieu-Daudé

+Paolo

Quoted 30 lines · Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
  target/mips/kvm.c | 8 ++------

  1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +1 -2
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq,
 int level)
       CPUState *cs = CPU(cpu);
       struct kvm_mips_interrupt intr;
   -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
         intr.cpu = -1;

  @@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int irq, int level)
      CPUState *dest_cs = CPU(cpu);
      struct kvm_mips_interrupt intr;
  -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
        intr.cpu = dest_cs->cpu_index;

Acked-by
#9 lore raw

Re: [PATCH 2/2] target/mips/kvm: Assert unreachable code is not used

Quoted 38 lines · Philippe Mathieu-Daudé

Huacai, ping?

Quoted 34 lines · Philippe Mathieu-Daudé

+Paolo

Quoted 30 lines · Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
   target/mips/kvm.c | 8 ++------
   1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +1 -2
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq,
 int level)
       CPUState *cs = CPU(cpu);
       struct kvm_mips_interrupt intr;
   -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
         intr.cpu = -1;
   @@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int

irq, int level)
      CPUState *dest_cs = CPU(cpu);
      struct kvm_mips_interrupt intr;
  -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
        intr.cpu = dest_cs->cpu_index;

For kvm_mips_set_ipi_interrupt, however, it would be nicer if hw/intc/mips_gic.c always used gic->vps[vp].env->irq[], and the qemu_irq handler took care of calling kvm_mips_set_ipi_interrupt.

Likewise, there is some duplication between kvm_mips_interrupt's caller and kvm_arch_pre_run. I'm not sure if kvm_arch_pre_run is needed at all.

Paolo

#10 lore raw

Re: [PATCH 2/2] target/mips/kvm: Assert unreachable code is not used

Quoted 40 lines · Paolo Bonzini
Quoted 38 lines · Philippe Mathieu-Daudé

Huacai, ping?

Quoted 34 lines · Philippe Mathieu-Daudé

+Paolo

Quoted 30 lines · Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
   target/mips/kvm.c | 8 ++------

   1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +1 -2
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq,
 int level)
        CPUState *cs = CPU(cpu);
        struct kvm_mips_interrupt intr;
    -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
          intr.cpu = -1;

   @@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int irq, int level)
       CPUState *dest_cs = CPU(cpu);
       struct kvm_mips_interrupt intr;
   -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
         intr.cpu = dest_cs->cpu_index;

Quoted 8 lines

For kvm_mips_set_ipi_interrupt, however, it would be nicer if hw/intc/mips_gic.c always used gic->vps[vp].env->irq[], and the qemu_irq handler took care of calling kvm_mips_set_ipi_interrupt.

Likewise, there is some duplication between kvm_mips_interrupt's caller and kvm_arch_pre_run.  I'm not sure if kvm_arch_pre_run is needed at all.

OK, I'll have a look and ask Huacai to test.

Meanwhile, patch applied to mips-next.

Thanks,

Phil.

Reviewed-by
#11 lore raw

Re: [PATCH 2/2] target/mips/kvm: Assert unreachable code is not used

In reply to Philippe Mathieu-Daudé
Quoted 38 lines · Philippe Mathieu-Daudé

Huacai, ping?

Quoted 34 lines · Philippe Mathieu-Daudé

+Paolo

Quoted 30 lines · Philippe Mathieu-Daudé

This code must not be used outside of KVM. Abort if it is.

---
  target/mips/kvm.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
target/mips/kvm.c +1 -2
index de3e26ef1f..050bfbd7fa 100644
@@ -196,9 +196,7 @@ int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq,
 int level)
      CPUState *cs = CPU(cpu);
      struct kvm_mips_interrupt intr;
  -    if (!kvm_enabled()) {
-        return 0;
-    }
+    assert(kvm_enabled());
        intr.cpu = -1;
  @@ -219,9 +217,7 @@ int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int

irq, int level)

      CPUState *dest_cs = CPU(cpu);
      struct kvm_mips_interrupt intr;

- if (!kvm_enabled()) {

target/mips/kvm.c +1 -2
-        return 0;
-    }
+    assert(kvm_enabled());
        intr.cpu = dest_cs->cpu_index;
#12 lore raw
In reply to Philippe Mathieu-Daudé

ping...

Quoted 11 lines · Philippe Mathieu-Daudé

A pair of trivial patches while reviewing Huacai's "KVM target support for MIPS64" series.

Philippe Mathieu-Daudé (2):
hw/mips/mips_int: De-duplicate KVM interrupt delivery target/mips/kvm: Assert unreachable code is not used

 hw/mips/mips_int.c | 11 +++--------
 target/mips/kvm.c  |  8 ++------
 2 files changed, 5 insertions(+), 14 deletions(-)