Skip to contentSkip to conversation

riscv: kvm: return SBI_ERR_FAILURE for PMU shmem OOM

RFC PATCH In review Osama Abdelkader 2 participants 4 messages Apr 15 – May 14

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

RFC PATCH 1/1 riscv: kvm: return SBI_ERR_FAILURE for PMU shmem OOM

kvm_riscv_vcpu_pmu_snapshot_set_shmem() and kvm_riscv_vcpu_pmu_event_info() returned -ENOMEM from the SBI extension handler, which caused kvm_riscv_vcpu_sbi_ecall() to abort KVM_RUN and surface the error to userspace instead of completing the ECALL with a negative SBI error in a0. Use SBI_ERR_FAILURE and the normal retdata path, matching other PMU handlers and kvm_sbi_ext_pmu_handler comment.

---
 arch/riscv/kvm/vcpu_pmu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +8 -4
index e873430e596b..f36b099f447c 100644
@@ -435,8 +435,10 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
 	}
 
 	kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
-	if (!kvpmu->sdata)
-		return -ENOMEM;
+	if (!kvpmu->sdata) {
+		sbiret = SBI_ERR_FAILURE;
+		goto out;
+	}
 
 	/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
 	if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
@@ -480,8 +482,10 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low
 	}
 
 	einfo = kzalloc(shmem_size, GFP_KERNEL);
-	if (!einfo)
-		return -ENOMEM;
+	if (!einfo) {
+		ret = SBI_ERR_FAILURE;
+		goto out;
+	}
 
 	ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size);
 	if (ret) {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 45 lines · Osama Abdelkader

kvm_riscv_vcpu_pmu_snapshot_set_shmem() and kvm_riscv_vcpu_pmu_event_info() returned -ENOMEM from the SBI extension handler, which caused kvm_riscv_vcpu_sbi_ecall() to abort KVM_RUN and surface the error to userspace instead of completing the ECALL with a negative SBI error in a0. Use SBI_ERR_FAILURE and the normal retdata path, matching other PMU handlers and kvm_sbi_ext_pmu_handler comment.

---
 arch/riscv/kvm/vcpu_pmu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +8 -4
index e873430e596b..f36b099f447c 100644
@@ -435,8 +435,10 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
 	}
 
 	kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
-	if (!kvpmu->sdata)
-		return -ENOMEM;
+	if (!kvpmu->sdata) {
+		sbiret = SBI_ERR_FAILURE;
+		goto out;
+	}
 
 	/* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
 	if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
@@ -480,8 +482,10 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low
 	}
 
 	einfo = kzalloc(shmem_size, GFP_KERNEL);
-	if (!einfo)
-		return -ENOMEM;
+	if (!einfo) {
+		ret = SBI_ERR_FAILURE;
+		goto out;
+	}
 
 	ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size);
 	if (ret) {
Signature · 1 line
2.43.0

ping.

maintainer
#2 lore raw
In reply to Osama Abdelkader
Quoted 9 lines · Osama Abdelkader

kvm_riscv_vcpu_pmu_snapshot_set_shmem() and kvm_riscv_vcpu_pmu_event_info() returned -ENOMEM from the SBI extension handler, which caused kvm_riscv_vcpu_sbi_ecall() to abort KVM_RUN and surface the error to userspace instead of completing the ECALL with a negative SBI error in a0. Use SBI_ERR_FAILURE and the normal retdata path, matching other PMU handlers and kvm_sbi_ext_pmu_handler comment.

Split this into two patches where:

1) One patch fixing kvm_riscv_vcpu_pmu_snapshot_set_shmem()
2) Second patch fixing kvm_riscv_vcpu_pmu_event_info()

Make sure to add Fixes tag in both patches.

Regards,
Anup

Quoted 37 lines
---
 arch/riscv/kvm/vcpu_pmu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +8 -5
index e873430e596b..f36b099f447c 100644
@@ -435,8 +435,10 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
        }
 
        kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
-       if (!kvpmu->sdata)
-               return -ENOMEM;
+       if (!kvpmu->sdata) {
+               sbiret = SBI_ERR_FAILURE;
+               goto out;
+       }
 
        /* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
        if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
@@ -480,8 +482,10 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low
        }
 
        einfo = kzalloc(shmem_size, GFP_KERNEL);
-       if (!einfo)
-               return -ENOMEM;
+       if (!einfo) {
+               ret = SBI_ERR_FAILURE;
+               goto out;
+       }
 
        ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size);
        if (ret) {
--

2.43.0

#3 lore raw
Quoted 14 lines · Anup Patel
Quoted 9 lines · Osama Abdelkader

kvm_riscv_vcpu_pmu_snapshot_set_shmem() and kvm_riscv_vcpu_pmu_event_info() returned -ENOMEM from the SBI extension handler, which caused kvm_riscv_vcpu_sbi_ecall() to abort KVM_RUN and surface the error to userspace instead of completing the ECALL with a negative SBI error in a0. Use SBI_ERR_FAILURE and the normal retdata path, matching other PMU handlers and kvm_sbi_ext_pmu_handler comment.

Split this into two patches where:

Quoted 7 lines

1) One patch fixing kvm_riscv_vcpu_pmu_snapshot_set_shmem()
2) Second patch fixing kvm_riscv_vcpu_pmu_event_info()

Make sure to add Fixes tag in both patches.

Regards,
Anup

Thanks Anup for the review, I just did that.

Regards,
Osama

Quoted 38 lines
Quoted 38 lines
---
 arch/riscv/kvm/vcpu_pmu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
arch/riscv/kvm/vcpu_pmu.c +8 -5
index e873430e596b..f36b099f447c 100644
@@ -435,8 +435,10 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s
        }
 
        kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
-       if (!kvpmu->sdata)
-               return -ENOMEM;
+       if (!kvpmu->sdata) {
+               sbiret = SBI_ERR_FAILURE;
+               goto out;
+       }
 
        /* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
        if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
@@ -480,8 +482,10 @@ int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low
        }
 
        einfo = kzalloc(shmem_size, GFP_KERNEL);
-       if (!einfo)
-               return -ENOMEM;
+       if (!einfo) {
+               ret = SBI_ERR_FAILURE;
+               goto out;
+       }
 
        ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size);
        if (ret) {
--

2.43.0