[PATCH v2] hw/intc/loongarch_ipi: use logical CPU ID for kvm_ipi_access_regs

Song Gao posted 1 patch 3 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250725012528.2582498-1-gaosong@loongson.cn
Maintainers: Song Gao <gaosong@loongson.cn>, Bibo Mao <maobibo@loongson.cn>, Jiaxun Yang <jiaxun.yang@flygoat.com>
hw/intc/loongarch_ipi_kvm.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
[PATCH v2] hw/intc/loongarch_ipi: use logical CPU ID for kvm_ipi_access_regs
Posted by Song Gao 3 months, 3 weeks ago
QEMU reboot after inserting no-configuous cpus may start failed
becaue the vcpu context may not have created on KVM, On QEMU side use logical CPU ID
for kvm_ipi_access_regs and do some check. On KVM use kvm_get_vcpu_by_id() get vcpu.

Signed-off-by: Song Gao <gaosong@loongson.cn>
---
 hw/intc/loongarch_ipi_kvm.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/hw/intc/loongarch_ipi_kvm.c b/hw/intc/loongarch_ipi_kvm.c
index 4cb3acc921..d0a2f2343f 100644
--- a/hw/intc/loongarch_ipi_kvm.c
+++ b/hw/intc/loongarch_ipi_kvm.c
@@ -23,14 +23,19 @@ static void kvm_ipi_access_regs(void *opaque, bool write)
     LoongarchIPIState *lis = LOONGARCH_IPI(opaque);
     IPICore *core;
     uint64_t attr;
-    int cpu, fd = lis->dev_fd;
+    int i, cpu, fd = lis->dev_fd;
 
     if (fd == 0) {
         return;
     }
 
-    for (cpu = 0; cpu < ipi->num_cpu; cpu++) {
-        core = &ipi->cpu[cpu];
+    for (i = 0; i < ipi->num_cpu; i++) {
+        core = &ipi->cpu[i];
+        if (core == NULL || core->cpu == NULL) {
+            continue;
+        }
+        cpu = core->cpu->cpu_index;
+
         attr = (cpu << 16) | CORE_STATUS_OFF;
         kvm_ipi_access_reg(fd, attr, &core->status, write);
 
-- 
2.43.0
Re: [PATCH v2] hw/intc/loongarch_ipi: use logical CPU ID for kvm_ipi_access_regs
Posted by Philippe Mathieu-Daudé 3 months, 3 weeks ago
On 25/7/25 03:25, Song Gao wrote:
> QEMU reboot after inserting no-configuous cpus may start failed
> becaue the vcpu context may not have created on KVM, On QEMU side use logical CPU ID
> for kvm_ipi_access_regs and do some check. On KVM use kvm_get_vcpu_by_id() get vcpu.
> 
> Signed-off-by: Song Gao <gaosong@loongson.cn>
> ---
>   hw/intc/loongarch_ipi_kvm.c | 11 ++++++++---
>   1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/intc/loongarch_ipi_kvm.c b/hw/intc/loongarch_ipi_kvm.c
> index 4cb3acc921..d0a2f2343f 100644
> --- a/hw/intc/loongarch_ipi_kvm.c
> +++ b/hw/intc/loongarch_ipi_kvm.c
> @@ -23,14 +23,19 @@ static void kvm_ipi_access_regs(void *opaque, bool write)
>       LoongarchIPIState *lis = LOONGARCH_IPI(opaque);
>       IPICore *core;
>       uint64_t attr;
> -    int cpu, fd = lis->dev_fd;
> +    int i, cpu, fd = lis->dev_fd;

While here, we could rename s/cpu/cpu_index/,>
>       if (fd == 0) {
>           return;
>       }
>   
> -    for (cpu = 0; cpu < ipi->num_cpu; cpu++) {

and declare @i here: for (int i = 0; ...).

> -        core = &ipi->cpu[cpu];
> +    for (i = 0; i < ipi->num_cpu; i++) {
> +        core = &ipi->cpu[i];
> +        if (core == NULL || core->cpu == NULL) {
> +            continue;
> +        }
> +        cpu = core->cpu->cpu_index;
> +
>           attr = (cpu << 16) | CORE_STATUS_OFF;
>           kvm_ipi_access_reg(fd, attr, &core->status, write);
>
Re: [PATCH v2] hw/intc/loongarch_ipi: use logical CPU ID for kvm_ipi_access_regs
Posted by gaosong 3 months, 3 weeks ago
在 2025/7/25 下午3:22, Philippe Mathieu-Daudé 写道:
> On 25/7/25 03:25, Song Gao wrote:
>> QEMU reboot after inserting no-configuous cpus may start failed
>> becaue the vcpu context may not have created on KVM, On QEMU side use 
>> logical CPU ID
>> for kvm_ipi_access_regs and do some check. On KVM use 
>> kvm_get_vcpu_by_id() get vcpu.
>>
Hi,
Sorry
this patch not fix this problem ,
This patch  just  fix qemu start fail with smp cpu < smp maxcpus on KVM.
e.g '-smp 1,maxcpus=4,sockets=4,cores=1,threads=1'

I 'll send a new patch,

Thanks.
Song Gao.
>> Signed-off-by: Song Gao <gaosong@loongson.cn>
>> ---
>>   hw/intc/loongarch_ipi_kvm.c | 11 ++++++++---
>>   1 file changed, 8 insertions(+), 3 deletions(-)
>>
>> diff --git a/hw/intc/loongarch_ipi_kvm.c b/hw/intc/loongarch_ipi_kvm.c
>> index 4cb3acc921..d0a2f2343f 100644
>> --- a/hw/intc/loongarch_ipi_kvm.c
>> +++ b/hw/intc/loongarch_ipi_kvm.c
>> @@ -23,14 +23,19 @@ static void kvm_ipi_access_regs(void *opaque, 
>> bool write)
>>       LoongarchIPIState *lis = LOONGARCH_IPI(opaque);
>>       IPICore *core;
>>       uint64_t attr;
>> -    int cpu, fd = lis->dev_fd;
>> +    int i, cpu, fd = lis->dev_fd;
>
> While here, we could rename s/cpu/cpu_index/,>
>>       if (fd == 0) {
>>           return;
>>       }
>>   -    for (cpu = 0; cpu < ipi->num_cpu; cpu++) {
>
> and declare @i here: for (int i = 0; ...).
>
>> -        core = &ipi->cpu[cpu];
>> +    for (i = 0; i < ipi->num_cpu; i++) {
>> +        core = &ipi->cpu[i];
>> +        if (core == NULL || core->cpu == NULL) {
>> +            continue;
>> +        }
>> +        cpu = core->cpu->cpu_index;
>> +
>>           attr = (cpu << 16) | CORE_STATUS_OFF;
>>           kvm_ipi_access_reg(fd, attr, &core->status, write);