Skip to contentSkip to conversation

KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

PATCH New Vincent Chen 3 participants 8 messages Dec 23, 2021 – Dec 27, 2021

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

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +8 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
 void kvm_arch_hardware_disable(void)
 {
+	/*
+	 * After clearing the hideleg CSR, the host kernel will receive spurious
+	 * interrupts if hvip CSR has pending interrupts and the corresponding
+	 * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+	 * vsie CSR shall be cleared early than hideleg CSR.
+	 */
+	csr_write(CSR_VSIE, 0);
+	csr_write(CSR_HVIP, 0);
 	csr_write(CSR_HEDELEG, 0);
 	csr_write(CSR_HIDELEG, 0);
 }
Signature · 1 line
2.17.1
maintainer
#1 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

Quoted 8 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

Small correction in last sentence:
".... must be cleared before clearing hideleg CSR."

Quoted 11 lines
---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 66

Quoted 9 lines
arch/riscv/kvm/main.c +6 -0
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */

Same correction as above.

Quoted 9 lines
arch/riscv/kvm/main.c +2 -1
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

Quoted 4 lines

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

Overall, this is a good fix but it is late in the development cycle so please add the "Fixes:" tag and also CC stable.

Regards,
Anup

#2 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

Quoted 11 lines · Anup Patel
Quoted 8 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

Quoted 3 lines

Small correction in last sentence:
".... must be cleared before clearing hideleg CSR."

OK

Quoted 12 lines
Quoted 12 lines
---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
Quoted 10 lines

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 66

Quoted 8 lines
arch/riscv/kvm/main.c +6 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */

Same correction as above.

OK

Quoted 10 lines
Quoted 10 lines
arch/riscv/kvm/main.c +2 -1
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

Quoted 8 lines
Quoted 4 lines

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

Overall, this is a good fix but it is late in the development cycle so please add the "Fixes:" tag and also CC stable.

OK, I will add the "Fixes:" tag and also CC stable in my next version patch. Thank you

Quoted 4 lines

Regards,
Anup

#3 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

In reply to Vincent Chen
Quoted 20 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 68

Quoted 10 lines
arch/riscv/kvm/main.c +8 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);

From hw design view, CSR_VSIE is enough, why apend CSR_HVIP clearing?

How can I reproduce it on qemu?

Quoted 11 lines
arch/riscv/kvm/main.c +0 -1
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#4 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

Quoted 22 lines · Guo Ren
Quoted 20 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
Quoted 11 lines

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 68

Quoted 10 lines
arch/riscv/kvm/main.c +8 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);

From hw design view, CSR_VSIE is enough, why apend CSR_HVIP clearing?

The CSR_HVIP is for the host OS to trigger virtual interrupts intended for VS-mode. Because the host OS has terminated all guest OS at this moment, I think the host OS also needs to clear CSR_HVIP as well.

Quoted 2 lines

How can I reproduce it on qemu?

After booting a guest OS to the shell, if you are lucky enough, you will see the console repeatedly prints "unexpected IRQ trap at vector 00" after using "halt -f" to terminate the guest OS.
(It is difficult to reproduce. This is because this bug happens only if $HVIP has pending interrupts whenever the guest OS is terminated)

Quoted 20 lines
Quoted 8 lines
arch/riscv/kvm/main.c +0 -1
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

Quoted 4 lines

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

#5 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

Quoted 41 lines · Vincent Chen
Quoted 22 lines · Guo Ren
Quoted 20 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
Quoted 11 lines

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 68

Quoted 10 lines
arch/riscv/kvm/main.c +8 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);

From hw design view, CSR_VSIE is enough, why apend CSR_HVIP clearing?

The CSR_HVIP is for the host OS to trigger virtual interrupts intended for VS-mode. Because the host OS has terminated all guest OS at this moment, I think the host OS also needs to clear CSR_HVIP as well.

Quoted 1 line
Quoted 7 lines
Quoted 1 line

How can I reproduce it on qemu?

After booting a guest OS to the shell, if you are lucky enough, you will see the console repeatedly prints "unexpected IRQ trap at vector 00" after using "halt -f" to terminate the guest OS.
(It is difficult to reproduce. This is because this bug happens only if $HVIP has pending interrupts whenever the guest OS is terminated)

Got it, thx.

Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
arch/riscv/kvm/main.c +0 -1
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

Quoted 4 lines

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
maintainer
#6 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

In reply to Guo Ren
Quoted 22 lines · Guo Ren
Quoted 20 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
Quoted 11 lines

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 68

Quoted 10 lines
arch/riscv/kvm/main.c +8 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);

From hw design view, CSR_VSIE is enough, why apend CSR_HVIP clearing?

Actually, both HVIP and VSIE are collectively responsible for injecting virtual interrupts. Rather than clearing just one of them, it's better to clear both in kvm_arch_hardware_disable() since this is in the non-performance critical path.

Regards,
Anup

Quoted 27 lines

How can I reproduce it on qemu?

Quoted 11 lines
arch/riscv/kvm/main.c +0 -1
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

#7 lore raw

PATCH 1/1 KVM: RISC-V: Avoid spurious virtual interrupts after clearing hideleg CSR

Quoted 36 lines · Anup Patel
Quoted 22 lines · Guo Ren
Quoted 20 lines · Vincent Chen

When the last VM is terminated, the host kernel will invoke function hardware_disable_nolock() on each CPU to disable the related virtualization functions. Here, RISC-V currently only clears hideleg CSR and hedeleg CSR. This behavior will cause the host kernel to receive spurious interrupts if hvip CSR has pending interrupts and the corresponding enable bits in vsie CSR are asserted. To avoid it, hvip CSR and vsie CSR shall be cleared earlier than hideleg CSR.

---
 arch/riscv/kvm/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kvm/main.c +0 -0
index 421ecf4e6360..31777d29e864 100644
@@ -58,6 +58,14 @@ int kvm_arch_hardware_enable(void)
 
Quoted 11 lines

arch/riscv/kvm/main.c int kvm_arch_hardware_enable(void) · line 68

Quoted 10 lines
arch/riscv/kvm/main.c +8 -0
 void kvm_arch_hardware_disable(void)
 {
+       /*
+        * After clearing the hideleg CSR, the host kernel will receive spurious
+        * interrupts if hvip CSR has pending interrupts and the corresponding
+        * enable bits in vsie CSR are asserted. To avoid it, hvip CSR and
+        * vsie CSR shall be cleared early than hideleg CSR.
+        */
+       csr_write(CSR_VSIE, 0);
+       csr_write(CSR_HVIP, 0);

From hw design view, CSR_VSIE is enough, why apend CSR_HVIP clearing?

Quoted 4 lines

Actually, both HVIP and VSIE are collectively responsible for injecting virtual interrupts. Rather than clearing just one of them, it's better to clear both in kvm_arch_hardware_disable() since this is in the non-performance critical path.

Clear enough, thx for the reply.

Quoted 31 lines

Regards,
Anup

Quoted 27 lines

How can I reproduce it on qemu?

Quoted 11 lines
arch/riscv/kvm/main.c +0 -1
        csr_write(CSR_HEDELEG, 0);
        csr_write(CSR_HIDELEG, 0);
 }
--

2.17.1

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/

--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/