]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
riscv: KVM: Fix SBI IPI error generation
authorAndrew Jones <ajones@ventanamicro.com>
Mon, 17 Feb 2025 08:45:10 +0000 (09:45 +0100)
committerAnup Patel <anup@brainfault.org>
Mon, 17 Feb 2025 10:58:28 +0000 (16:28 +0530)
When an invalid function ID of an SBI extension is used we should
return not-supported, not invalid-param. Also, when we see that at
least one hartid constructed from the base and mask parameters is
invalid, then we should return invalid-param. Finally, rather than
relying on overflowing a left shift to result in zero and then using
that zero in a condition which [correctly] skips sending an IPI (but
loops unnecessarily), explicitly check for overflow and exit the loop
immediately.

Fixes: 5f862df5585c ("RISC-V: KVM: Add v0.1 replacement SBI extensions defined in v0.2")
Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20250217084506.18763-10-ajones@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/vcpu_sbi_replace.c

index 9c2ab3dfa93aa535788a4a04a36717220653e864..74e3a38c6a29ee2ae57b32ebb153d814813f9279 100644 (file)
@@ -51,9 +51,10 @@ static int kvm_sbi_ext_ipi_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
        struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
        unsigned long hmask = cp->a0;
        unsigned long hbase = cp->a1;
+       unsigned long hart_bit = 0, sentmask = 0;
 
        if (cp->a6 != SBI_EXT_IPI_SEND_IPI) {
-               retdata->err_val = SBI_ERR_INVALID_PARAM;
+               retdata->err_val = SBI_ERR_NOT_SUPPORTED;
                return 0;
        }
 
@@ -62,15 +63,23 @@ static int kvm_sbi_ext_ipi_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
                if (hbase != -1UL) {
                        if (tmp->vcpu_id < hbase)
                                continue;
-                       if (!(hmask & (1UL << (tmp->vcpu_id - hbase))))
+                       hart_bit = tmp->vcpu_id - hbase;
+                       if (hart_bit >= __riscv_xlen)
+                               goto done;
+                       if (!(hmask & (1UL << hart_bit)))
                                continue;
                }
                ret = kvm_riscv_vcpu_set_interrupt(tmp, IRQ_VS_SOFT);
                if (ret < 0)
                        break;
+               sentmask |= 1UL << hart_bit;
                kvm_riscv_vcpu_pmu_incr_fw(tmp, SBI_PMU_FW_IPI_RCVD);
        }
 
+done:
+       if (hbase != -1UL && (hmask ^ sentmask))
+               retdata->err_val = SBI_ERR_INVALID_PARAM;
+
        return ret;
 }