Skip to contentSkip to conversation

hw/intc: sifive_plic: Fix multi-socket plic configuraiton

PATCH Applied Atish Patra 2 participants 6 messages Jul 23, 2022 – Jul 27, 2022 SiFive drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/intc: sifive_plic: Fix multi-socket plic configuraiton

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the addr_config buffer)
---
 hw/intc/sifive_plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +2 -2
index 56d60e9ac935..fdac028a521f 100644
@@ -454,10 +454,10 @@ DeviceState *sifive_plic_create(hwaddr addr, char *hart_config,
 
     for (i = 0; i < plic->num_addrs; i++) {
         int cpu_num = plic->addr_config[i].hartid;
-        CPUState *cpu = qemu_get_cpu(hartid_base + cpu_num);
+        CPUState *cpu = qemu_get_cpu(cpu_num);
 
         if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, num_harts + cpu_num,
+            qdev_connect_gpio_out(dev, cpu_num,
                                   qdev_get_gpio_in(DEVICE(cpu), IRQ_M_EXT));
         }
         if (plic->addr_config[i].mode == PLICMode_S) {
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Atish Patra

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Quoted 3 lines
Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the addr_config buffer)

Can you share the -cpu options that causes the segfault? I'll add it to my test case

Alistair

Quoted 25 lines
---
 hw/intc/sifive_plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +2 -3
index 56d60e9ac935..fdac028a521f 100644
@@ -454,10 +454,10 @@ DeviceState *sifive_plic_create(hwaddr addr, char *hart_config,
 
     for (i = 0; i < plic->num_addrs; i++) {
         int cpu_num = plic->addr_config[i].hartid;
-        CPUState *cpu = qemu_get_cpu(hartid_base + cpu_num);
+        CPUState *cpu = qemu_get_cpu(cpu_num);
 
         if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, num_harts + cpu_num,
+            qdev_connect_gpio_out(dev, cpu_num,
                                   qdev_get_gpio_in(DEVICE(cpu), IRQ_M_EXT));
         }
         if (plic->addr_config[i].mode == PLICMode_S) {
--

2.25.1

author Reviewed-by Alistair Francis
#2 lore raw
Quoted 10 lines · Alistair Francis
Quoted 9 lines · Atish Patra

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Quoted 9 lines
Quoted 1 line
Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the

addr_config buffer)

Quoted 2 lines

Can you share the -cpu options that causes the segfault? I'll add it to my test case

"-cpu rv64 -M virt -m 2G -smp 4 -object memory-backend-ram,size=1G,policy=bind,host-nodes=0,id=ram-node0 \
-numa node,memdev=ram-node0 \
-object memory-backend-ram,size=1G,policy=bind,host-nodes=0,id=ram-node1 \
-numa node,memdev=ram-node1"

You also need to enable CONFIG_NUMA in kernel.

Quoted 31 lines

Alistair

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char

Quoted 9 lines
---
 hw/intc/sifive_plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +0 -0
index 56d60e9ac935..fdac028a521f 100644
@@ -454,10 +454,10 @@ DeviceState *sifive_plic_create(hwaddr addr, char

*hart_config,

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char *hart_config,

Quoted 10 lines
hw/intc/sifive_plic.c +2 -2
     for (i = 0; i < plic->num_addrs; i++) {
         int cpu_num = plic->addr_config[i].hartid;
-        CPUState *cpu = qemu_get_cpu(hartid_base + cpu_num);
+        CPUState *cpu = qemu_get_cpu(cpu_num);
 
         if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, num_harts + cpu_num,
+            qdev_connect_gpio_out(dev, cpu_num,
                                   qdev_get_gpio_in(DEVICE(cpu),

IRQ_M_EXT));

Quoted 6 lines
hw/intc/sifive_plic.c +0 -1
         }
         if (plic->addr_config[i].mode == PLICMode_S) {
--

2.25.1

#3 lore raw
Quoted 29 lines · Atish Kumar Patra
Quoted 11 lines · Alistair Francis
Quoted 9 lines · Atish Patra

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Quoted 7 lines
Quoted 3 lines
Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the addr_config buffer)

Can you share the -cpu options that causes the segfault? I'll add it to my test case

"-cpu rv64 -M virt -m 2G -smp 4 -object memory-backend-ram,size=1G,policy=bind,host-nodes=0,id=ram-node0 \

(quoted diff) +0 -3
-numa node,memdev=ram-node0   \
-object memory-backend-ram,size=1G,policy=bind,host-nodes=0,id=ram-node1 \
-numa node,memdev=ram-node1"
Quoted 1 line

You also need to enable CONFIG_NUMA in kernel.

Thanks!

Applied to riscv-to-apply.next

Alistair

#4 lore raw
In reply to Atish Patra
Quoted 21 lines · Atish Patra

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the addr_config buffer)
---
 hw/intc/sifive_plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +0 -0
index 56d60e9ac935..fdac028a521f 100644

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char *hart_config, · line 460

Quoted 9 lines
hw/intc/sifive_plic.c +2 -2
@@ -454,10 +454,10 @@ DeviceState *sifive_plic_create(hwaddr addr, char *hart_config,
 
     for (i = 0; i < plic->num_addrs; i++) {
         int cpu_num = plic->addr_config[i].hartid;
-        CPUState *cpu = qemu_get_cpu(hartid_base + cpu_num);
+        CPUState *cpu = qemu_get_cpu(cpu_num);
 
         if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, num_harts + cpu_num,
+            qdev_connect_gpio_out(dev, cpu_num,

Argh!

I was trying to get this ready to go into 7.1. I have been working on updating my tests to catch this failure in the future as well.

While testing this change I noticed that it breaks the noMMU test case.

I think the correct fix is actually this (on top of your patch):

hw/intc/sifive_plic.c +0 -0
index fdac028a52..af4ae3630e 100644
@@ -457,7 +457,7 @@ DeviceState *sifive_plic_create(hwaddr addr, char

*hart_config,

        CPUState *cpu = qemu_get_cpu(cpu_num);

        if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, cpu_num,
+            qdev_connect_gpio_out(dev, num_harts - plic->hartid_base + cpu_num,
                                  qdev_get_gpio_in(DEVICE(cpu), IRQ_M_EXT));
        }
        if (plic->addr_config[i].mode == PLICMode_S) {

The idea is that we need to increment the second argument to qdev_connect_gpio_out() for the PLICMode_M compared to the PLICMode_S case.

This ensures that we do that correctly without breaking anything.

How does that look to you?

Alistair

Quoted 7 lines
hw/intc/sifive_plic.c +0 -1
                                   qdev_get_gpio_in(DEVICE(cpu), IRQ_M_EXT));
         }
         if (plic->addr_config[i].mode == PLICMode_S) {
--

2.25.1

#5 lore raw
Quoted 60 lines · Alistair Francis
Quoted 10 lines · Atish Patra

Since commit 40244040a7ac, multi-socket configuration with plic is broken as the hartid for second socket is calculated incorrectly. The hartid stored in addr_config already includes the offset for the base hartid for that socket. Adding it again would lead to segfault while creating the plic device for the virt machine. qdev_connect_gpio_out was also invoked with incorrect number of gpio lines.

Fixes 40244040a7ac (hw/intc: sifive_plic: Avoid overflowing the

addr_config buffer)

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char

Quoted 11 lines
---
 hw/intc/sifive_plic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/intc/sifive_plic.c +0 -0
index 56d60e9ac935..fdac028a521f 100644
@@ -454,10 +454,10 @@ DeviceState *sifive_plic_create(hwaddr addr, char

*hart_config,

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char · line 459

Quoted 9 lines
hw/intc/sifive_plic.c +2 -2
     for (i = 0; i < plic->num_addrs; i++) {
         int cpu_num = plic->addr_config[i].hartid;
-        CPUState *cpu = qemu_get_cpu(hartid_base + cpu_num);
+        CPUState *cpu = qemu_get_cpu(cpu_num);
 
         if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, num_harts + cpu_num,
+            qdev_connect_gpio_out(dev, cpu_num,

Argh!

I was trying to get this ready to go into 7.1. I have been working on updating my tests to catch this failure in the future as well.

While testing this change I noticed that it breaks the noMMU test case.

I think the correct fix is actually this (on top of your patch):

hw/intc/sifive_plic.c +1 -1
index fdac028a52..af4ae3630e 100644
@@ -457,7 +457,7 @@ DeviceState *sifive_plic_create(hwaddr addr, char
 *hart_config,
        CPUState *cpu = qemu_get_cpu(cpu_num);
 
        if (plic->addr_config[i].mode == PLICMode_M) {
-            qdev_connect_gpio_out(dev, cpu_num,
+            qdev_connect_gpio_out(dev, num_harts - plic->hartid_base +
 cpu_num,
                                  qdev_get_gpio_in(DEVICE(cpu),
IRQ_M_EXT));
        }
        if (plic->addr_config[i].mode == PLICMode_S) {
Quoted 9 lines

The idea is that we need to increment the second argument to qdev_connect_gpio_out() for the PLICMode_M compared to the PLICMode_S case.

This ensures that we do that correctly without breaking anything.

How does that look to you?

Ahh yes. That makes sense.
Tested the updated change on multi-socket as well.

Quoted 11 lines

Alistair

hw/intc/sifive_plic.c DeviceState *sifive_plic_create(hwaddr addr, char · line 460

Quoted 1 line
hw/intc/sifive_plic.c +0 -0
                                   qdev_get_gpio_in(DEVICE(cpu),

IRQ_M_EXT));

Quoted 6 lines
hw/intc/sifive_plic.c +0 -1
         }
         if (plic->addr_config[i].mode == PLICMode_S) {
--

2.25.1