Skip to contentSkip to conversation

hw/i386/cxl: ensure maxram is greater than ram size for calculating cxl range

PATCH In review Ani Sinha 2 participants 4 messages Oct 11, 2023 – Oct 12, 2023

lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH 1/1 hw/i386/cxl: ensure maxram is greater than ram size for calculating cxl range

pc_get_device_memory_range() finds the device memory size by calculating the difference between maxram and ram sizes. This calculation makes sense only when maxram is greater than the ram size. Make sure we check for that before calling pc_get_device_memory_range().

---
 hw/i386/pc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index f72e2c3b35..948c58171c 100644
@@ -820,10 +820,12 @@ static void pc_get_device_memory_range(PCMachineState *pcms,
 static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
 {
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+    MachineState *ms = MACHINE(pcms);
     hwaddr cxl_base;
     ram_addr_t size;
 
-    if (pcmc->has_reserved_memory) {
+    if (pcmc->has_reserved_memory &&
+        (ms->ram_size < ms->maxram_size)) {
         pc_get_device_memory_range(pcms, &cxl_base, &size);
         cxl_base += size;
     } else {
Signature · 1 line
2.42.0
#1 lore raw
Quoted 6 lines · On Wed, 11 Oct 2023 16:23:35 +0530 Ani Sinha <[email protected]> wrote:

pc_get_device_memory_range() finds the device memory size by calculating the difference between maxram and ram sizes. This calculation makes sense only when maxram is greater than the ram size. Make sure we check for that before calling pc_get_device_memory_range().

Whilst this is similar to other cases, I can't remember or quickly work out if the 'else' path here is appropriate. Can we add something to the patch description to talk about that?

For reference it's:

cxl_base = pc_above_4g_end(pcms);

Quoted 22 lines
---
 hw/i386/pc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index f72e2c3b35..948c58171c 100644
@@ -820,10 +820,12 @@ static void pc_get_device_memory_range(PCMachineState *pcms,
 static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
 {
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+    MachineState *ms = MACHINE(pcms);
     hwaddr cxl_base;
     ram_addr_t size;
 
-    if (pcmc->has_reserved_memory) {
+    if (pcmc->has_reserved_memory &&
+        (ms->ram_size < ms->maxram_size)) {
         pc_get_device_memory_range(pcms, &cxl_base, &size);
         cxl_base += size;
     } else {
author
#2 lore raw
Quoted 14 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 11 Oct 2023 16:23:35 +0530 Ani Sinha <[email protected]> wrote:

pc_get_device_memory_range() finds the device memory size by calculating the difference between maxram and ram sizes. This calculation makes sense only when maxram is greater than the ram size. Make sure we check for that before calling pc_get_device_memory_range().

Quoted 7 lines

Whilst this is similar to other cases, I can't remember or quickly work out if the 'else' path here is appropriate. Can we add something to the patch description to talk about that?

For reference it's:

cxl_base = pc_above_4g_end(pcms);

Leaving the alignment adjustments aside, the hotplugged memory devices are mapped starting from where the “above_4g” memory ends.
The cxl memory starts after the region reserved for hot plugged memory devices. If there is no hot plugged memory device region (maxmem == mem), the cxl memory can start right after where “above_4g” memory ends. See also pc_pci_hole64_start() and pc_max_used_gpa().
I did not want to add any alignment adjustments because I was not sure if it would add regression and incompatibility with older machine types.

Quoted 24 lines
Quoted 23 lines

---
hw/i386/pc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index f72e2c3b35..948c58171c 100644
@@ -820,10 +820,12 @@ static void pc_get_device_memory_range(PCMachineState *pcms,
 static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
 {
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+    MachineState *ms = MACHINE(pcms);
    hwaddr cxl_base;
    ram_addr_t size;
 
-    if (pcmc->has_reserved_memory) {
+    if (pcmc->has_reserved_memory &&
+        (ms->ram_size < ms->maxram_size)) {
        pc_get_device_memory_range(pcms, &cxl_base, &size);
        cxl_base += size;
    } else {
Acked-by Jonathan Cameron
#3 lore raw
Quoted 16 lines · On Thu, 12 Oct 2023 10:10:36 +0530 Ani Sinha <[email protected]> wrote:
Quoted 16 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 11 Oct 2023 16:23:35 +0530 Ani Sinha <[email protected]> wrote:

pc_get_device_memory_range() finds the device memory size by calculating the difference between maxram and ram sizes. This calculation makes sense only when maxram is greater than the ram size. Make sure we check for that before calling pc_get_device_memory_range().

Whilst this is similar to other cases, I can't remember or quickly work out if the 'else' path here is appropriate. Can we add something to the patch description to talk about that?

Quoted 9 lines
Quoted 3 lines

For reference it's:

cxl_base = pc_above_4g_end(pcms);

Leaving the alignment adjustments aside, the hotplugged memory devices are mapped starting from where the “above_4g” memory ends.
The cxl memory starts after the region reserved for hot plugged memory devices. If there is no hot plugged memory device region (maxmem == mem), the cxl memory can start right after where “above_4g” memory ends. See also pc_pci_hole64_start() and pc_max_used_gpa().
I did not want to add any alignment adjustments because I was not sure if it would add regression and incompatibility with older machine types.

Thanks for explanation.

Quoted 26 lines
Quoted 25 lines
Quoted 24 lines

---
hw/i386/pc.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index f72e2c3b35..948c58171c 100644
@@ -820,10 +820,12 @@ static void pc_get_device_memory_range(PCMachineState *pcms,
 static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
 {
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+    MachineState *ms = MACHINE(pcms);
    hwaddr cxl_base;
    ram_addr_t size;
 
-    if (pcmc->has_reserved_memory) {
+    if (pcmc->has_reserved_memory &&
+        (ms->ram_size < ms->maxram_size)) {
        pc_get_device_memory_range(pcms, &cxl_base, &size);
        cxl_base += size;
    } else {