Skip to contentSkip to conversation

KVM: arm64: Set a linux errno on SMCCC error in kvm_call_hyp_nvhe()

PATCH In review Vincent Donnefort 4 participants 4 messages Jun 3 – Jun 7

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

PATCH 1/1 KVM: arm64: Set a linux errno on SMCCC error in kvm_call_hyp_nvhe()

If the HVC called in kvm_call_hyp_nvhe() fails with an SMCCC error code, we WARN. However, the returned value isn't initialized and the caller might get garbage or 0 which is likely to be interpreted as success.

Set a default -EPERM error value, ensuring all callers get the message when SMCCC calls fail.

---

I have encountered this issue while working on a follow-up contribution to the hypervisor tracing. In that case it completely crashed the kernel because IS_ERR() failed on that res.a1 value.

Now, if it makes that function more robust, I do not believe it is fixing any existing bug which is why I haven't added a "Fixes:" tag.

In case we want to stick one, here it is:

Fixes 054698316d87 ("KVM: arm64: nVHE: Migrate hyp interface to SMCCC")
arch/arm64/include/asm/kvm_host.h +3 -2
index a49042bfa801..6b8fd494792c 100644
@@ -1273,13 +1273,14 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 #define vcpu_has_run_once(vcpu)	(!!READ_ONCE((vcpu)->pid))
 
 #ifndef __KVM_NVHE_HYPERVISOR__
-#define kvm_call_hyp_nvhe(f, ...)						\
+#define kvm_call_hyp_nvhe(f, ...)					\
 	({								\
 		struct arm_smccc_res res;				\
 									\
 		arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f),		\
 				  ##__VA_ARGS__, &res);			\
-		WARN_ON(res.a0 != SMCCC_RET_SUCCESS);			\
+		if (WARN_ON(res.a0 != SMCCC_RET_SUCCESS))		\
+			res.a1 = -EPERM;				\
 									\
 		res.a1;							\
 	})

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8

Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
Reviewed-by on v1 1/1
#1 lore raw

Hi Vincent,

Quoted 16 lines · Vincent Donnefort

If the HVC called in kvm_call_hyp_nvhe() fails with an SMCCC error code, we WARN. However, the returned value isn't initialized and the caller might get garbage or 0 which is likely to be interpreted as success.

Set a default -EPERM error value, ensuring all callers get the message when SMCCC calls fail.

---

I have encountered this issue while working on a follow-up contribution to the hypervisor tracing. In that case it completely crashed the kernel because IS_ERR() failed on that res.a1 value.

Quoted 6 lines

Now, if it makes that function more robust, I do not believe it is fixing any existing bug which is why I haven't added a "Fixes:" tag.

In case we want to stick one, here it is:

Fixes 054698316d87 ("KVM: arm64: nVHE: Migrate hyp interface to SMCCC")

You are fixing a real bug, even if it's latent; I think you should include it.

arch/arm64/include/asm/kvm_host.h void kvm_arm_resume_guest(struct kvm *kvm); · line 1276

Quoted 11 lines
arch/arm64/include/asm/kvm_host.h +1 -1
index a49042bfa801..6b8fd494792c 100644
@@ -1273,13 +1273,14 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 #define vcpu_has_run_once(vcpu)        (!!READ_ONCE((vcpu)->pid))
 
 #ifndef __KVM_NVHE_HYPERVISOR__
-#define kvm_call_hyp_nvhe(f, ...)                                              \
+#define kvm_call_hyp_nvhe(f, ...)                                      \

nit: This realignment would muddle a git blame on this. Prefer you drop it.

Otherwise,

Cheers,
/fuad

Quoted 16 lines
arch/arm64/include/asm/kvm_host.h +2 -1
        ({                                                              \
                struct arm_smccc_res res;                               \
                                                                        \
                arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f),               \
                                  ##__VA_ARGS__, &res);                 \
-               WARN_ON(res.a0 != SMCCC_RET_SUCCESS);                   \
+               if (WARN_ON(res.a0 != SMCCC_RET_SUCCESS))               \
+                       res.a1 = -EPERM;                                \
                                                                        \
                res.a1;                                                 \
        })

base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8 --
2.54.0.1032.g2f8565e1d1-goog

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Vincent Donnefort
Quoted 33 lines · Vincent Donnefort

If the HVC called in kvm_call_hyp_nvhe() fails with an SMCCC error code, we WARN. However, the returned value isn't initialized and the caller might get garbage or 0 which is likely to be interpreted as success.

Set a default -EPERM error value, ensuring all callers get the message when SMCCC calls fail.

---

I have encountered this issue while working on a follow-up contribution to the hypervisor tracing. In that case it completely crashed the kernel because IS_ERR() failed on that res.a1 value.

Now, if it makes that function more robust, I do not believe it is fixing any existing bug which is why I haven't added a "Fixes:" tag.

In case we want to stick one, here it is:

Fixes 054698316d87 ("KVM: arm64: nVHE: Migrate hyp interface to SMCCC")
arch/arm64/include/asm/kvm_host.h +1 -1
index a49042bfa801..6b8fd494792c 100644
@@ -1273,13 +1273,14 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 #define vcpu_has_run_once(vcpu)	(!!READ_ONCE((vcpu)->pid))
 
 #ifndef __KVM_NVHE_HYPERVISOR__
-#define kvm_call_hyp_nvhe(f, ...)						\
+#define kvm_call_hyp_nvhe(f, ...)					\
 	({								\

arch/arm64/include/asm/kvm_host.h void kvm_arm_resume_guest(struct kvm *kvm); · line 1286

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +2 -1
 		struct arm_smccc_res res;				\
 									\
 		arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f),		\
 				  ##__VA_ARGS__, &res);			\
-		WARN_ON(res.a0 != SMCCC_RET_SUCCESS);			\
+		if (WARN_ON(res.a0 != SMCCC_RET_SUCCESS))		\
+			res.a1 = -EPERM;				\
 									\
 		res.a1;							\
 	})

Looks like the only error code we return to the host is SMCCC_RET_NOT_SUPPORTED, so maybe -EOPNOTSUPP would be more appropriate?

Either way:

Will

maintainer
#3 lore raw
Quoted 40 lines · Will Deacon
Quoted 38 lines · Vincent Donnefort

If the HVC called in kvm_call_hyp_nvhe() fails with an SMCCC error code, we WARN. However, the returned value isn't initialized and the caller might get garbage or 0 which is likely to be interpreted as success.

Set a default -EPERM error value, ensuring all callers get the message when SMCCC calls fail.

---

I have encountered this issue while working on a follow-up contribution to the hypervisor tracing. In that case it completely crashed the kernel because IS_ERR() failed on that res.a1 value.

Now, if it makes that function more robust, I do not believe it is fixing any existing bug which is why I haven't added a "Fixes:" tag.

In case we want to stick one, here it is:

Fixes 054698316d87 ("KVM: arm64: nVHE: Migrate hyp interface to SMCCC")
arch/arm64/include/asm/kvm_host.h +1 -2
index a49042bfa801..6b8fd494792c 100644
@@ -1273,13 +1273,14 @@ void kvm_arm_resume_guest(struct kvm *kvm);
 #define vcpu_has_run_once(vcpu)	(!!READ_ONCE((vcpu)->pid))
 
 #ifndef __KVM_NVHE_HYPERVISOR__
-#define kvm_call_hyp_nvhe(f, ...)						\
+#define kvm_call_hyp_nvhe(f, ...)					\
 	({								\
 		struct arm_smccc_res res;				\
 									\
 		arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f),		\
 				  ##__VA_ARGS__, &res);			\
-		WARN_ON(res.a0 != SMCCC_RET_SUCCESS);			\
Quoted 8 lines

arch/arm64/include/asm/kvm_host.h void kvm_arm_resume_guest(struct kvm *kvm); · line 1286

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+		if (WARN_ON(res.a0 != SMCCC_RET_SUCCESS))		\
+			res.a1 = -EPERM;				\
 									\
 		res.a1;							\
 	})

Looks like the only error code we return to the host is SMCCC_RET_NOT_SUPPORTED, so maybe -EOPNOTSUPP would be more appropriate?

Yes, this is better. I've hacked the patch to reflect this upon applying it.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.