Skip to contentSkip to conversation

arch/powerpc/kvm: Avoid extra checks when emulating HFSCR bits

PATCH In review Gautam Menghani 2 participants 4 messages Jun 26, 2024 – Jul 16, 2024

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

PATCH 1/1 arch/powerpc/kvm: Avoid extra checks when emulating HFSCR bits

When a KVM guest tries to use a feature disabled by HFSCR, it exits to the host for emulation support, and the code checks for all bits which are emulated. Avoid checking all the bits by using a switch case.

---
 arch/powerpc/kvm/book3s_hv.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
arch/powerpc/kvm/book3s_hv.c +12 -4
index 99c7ce825..a72fd2543 100644
@@ -1922,14 +1922,22 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
 
 		r = EMULATE_FAIL;
 		if (cpu_has_feature(CPU_FTR_ARCH_300)) {
-			if (cause == FSCR_MSGP_LG)
+			switch (cause) {
+			case FSCR_MSGP_LG:
 				r = kvmppc_emulate_doorbell_instr(vcpu);
-			if (cause == FSCR_PM_LG)
+				break;
+			case FSCR_PM_LG:
 				r = kvmppc_pmu_unavailable(vcpu);
-			if (cause == FSCR_EBB_LG)
+				break;
+			case FSCR_EBB_LG:
 				r = kvmppc_ebb_unavailable(vcpu);
-			if (cause == FSCR_TM_LG)
+				break;
+			case FSCR_TM_LG:
 				r = kvmppc_tm_unavailable(vcpu);
+				break;
+			default:
+				break;
+			}
 		}
 		if (r == EMULATE_FAIL) {
 			kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
Signature · 1 line
2.45.2
#1 lore raw

Hello,

Please review this patch and let me know if any changes are needed.

Thanks,
Gautam

#2 lore raw
In reply to Gautam Menghani
Quoted 3 lines · Gautam Menghani

When a KVM guest tries to use a feature disabled by HFSCR, it exits to the host for emulation support, and the code checks for all bits which are emulated. Avoid checking all the bits by using a switch case.

The patch looks fine, but I don't know what you mean by "avoid checking all the bits".

The existing code checks 4 cases, the case statement checks the same 4 cases (plus the default case).

There are other cause values (not bits), but the new and old code don't check them all anyway. (Which is OK because the default return value is EMULATE_FAIL)

AFAICS it generates almost identical code.

So I think the change log should just say something like "all the FSCR cause values are exclusive so use a case statement which better expresses that" ?

Also please try to copy the existing subject style for the KVM code, for this file it would be "KVM: PPC: Book3S HV: ...". I agree it's verbose, and wouldn't be my choice, but thats what's always been used so let's stick to it.

cheers

Quoted 33 lines
arch/powerpc/kvm/book3s_hv.c +12 -4
index 99c7ce825..a72fd2543 100644
@@ -1922,14 +1922,22 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
 
 		r = EMULATE_FAIL;
 		if (cpu_has_feature(CPU_FTR_ARCH_300)) {
-			if (cause == FSCR_MSGP_LG)
+			switch (cause) {
+			case FSCR_MSGP_LG:
 				r = kvmppc_emulate_doorbell_instr(vcpu);
-			if (cause == FSCR_PM_LG)
+				break;
+			case FSCR_PM_LG:
 				r = kvmppc_pmu_unavailable(vcpu);
-			if (cause == FSCR_EBB_LG)
+				break;
+			case FSCR_EBB_LG:
 				r = kvmppc_ebb_unavailable(vcpu);
-			if (cause == FSCR_TM_LG)
+				break;
+			case FSCR_TM_LG:
 				r = kvmppc_tm_unavailable(vcpu);
+				break;
+			default:
+				break;
+			}
 		}
 		if (r == EMULATE_FAIL) {
 			kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
Signature · 1 line
2.45.2
#3 lore raw
Quoted 15 lines · Michael Ellerman
Quoted 3 lines · Gautam Menghani

When a KVM guest tries to use a feature disabled by HFSCR, it exits to the host for emulation support, and the code checks for all bits which are emulated. Avoid checking all the bits by using a switch case.

The patch looks fine, but I don't know what you mean by "avoid checking all the bits".

The existing code checks 4 cases, the case statement checks the same 4 cases (plus the default case).

There are other cause values (not bits), but the new and old code don't check them all anyway. (Which is OK because the default return value is EMULATE_FAIL)

Quoted 5 lines

AFAICS it generates almost identical code.

So I think the change log should just say something like "all the FSCR cause values are exclusive so use a case statement which better expresses that" ?

Yes agreed, will send a v2 with suggested changes.

Quoted 6 lines

Also please try to copy the existing subject style for the KVM code, for this file it would be "KVM: PPC: Book3S HV: ...". I agree it's verbose, and wouldn't be my choice, but thats what's always been used so let's stick to it.

Ack.

Thanks,
Gautam

Quoted 35 lines

cheers

Quoted 33 lines
arch/powerpc/kvm/book3s_hv.c +12 -4
index 99c7ce825..a72fd2543 100644
@@ -1922,14 +1922,22 @@ static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
 
 		r = EMULATE_FAIL;
 		if (cpu_has_feature(CPU_FTR_ARCH_300)) {
-			if (cause == FSCR_MSGP_LG)
+			switch (cause) {
+			case FSCR_MSGP_LG:
 				r = kvmppc_emulate_doorbell_instr(vcpu);
-			if (cause == FSCR_PM_LG)
+				break;
+			case FSCR_PM_LG:
 				r = kvmppc_pmu_unavailable(vcpu);
-			if (cause == FSCR_EBB_LG)
+				break;
+			case FSCR_EBB_LG:
 				r = kvmppc_ebb_unavailable(vcpu);
-			if (cause == FSCR_TM_LG)
+				break;
+			case FSCR_TM_LG:
 				r = kvmppc_tm_unavailable(vcpu);
+				break;
+			default:
+				break;
+			}
 		}
 		if (r == EMULATE_FAIL) {
 			kvmppc_core_queue_program(vcpu, SRR1_PROGILL |
Signature · 1 line
2.45.2