Skip to contentSkip to conversation

Fix pc DIMMs capacity calculation

PATCH In review Bharata B Rao 3 participants 10 messages Jan 12, 2015 – Jan 22, 2015

lore
Share & tools
lore
#0 lore raw

cover Fix pc DIMMs capacity calculation

1st patch fixes PC DIMMs capacity calculation and the 2nd patch does code movement so that pc_existing_dimms_capacity() can be used by other architectures.

Bharata B Rao (2):
pc: Fix DIMMs capacity calculation
pc-dimm: Make pc_existing_dimms_capacity global

 hw/i386/pc.c             | 36 +++---------------------------------
 hw/mem/pc-dimm.c         | 25 +++++++++++++++++++++++++
 include/hw/mem/pc-dimm.h |  1 +
 3 files changed, 29 insertions(+), 33 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/2 pc: Fix DIMMs capacity calculation

pc_existing_dimms_capacity() is returning DIMMs count rather than capacity. Fix this to return the capacity. Also consider only realized devices for capacity calculation.

---
 hw/i386/pc.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
hw/i386/pc.c +10 -16
index e07f1fa..125cf0a 100644
@@ -1552,25 +1552,18 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_dimm_count(Object *obj, void *opaque)
-{
-    int *count = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        (*count)++;
-    }
-
-    object_child_foreach(obj, pc_dimm_count, opaque);
-    return 0;
-}
-
 static int pc_existing_dimms_capacity(Object *obj, void *opaque)
 {
     Error *local_err = NULL;
     uint64_t *size = opaque;
 
     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP, &local_err);
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
 
         if (local_err) {
             qerror_report_err(local_err);
@@ -1579,7 +1572,7 @@ static int pc_existing_dimms_capacity(Object *obj, void *opaque)
         }
     }
 
-    object_child_foreach(obj, pc_dimm_count, opaque);
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
     return 0;
 }
 
@@ -1623,8 +1616,9 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
     if (existing_dimms_capacity + memory_region_size(mr) >
         machine->maxram_size - machine->ram_size) {
         error_setg(&local_err, "not enough space, currently 0x%" PRIx64
-                   " in use of total 0x" RAM_ADDR_FMT,
-                   existing_dimms_capacity, machine->maxram_size);
+                   " in use of total hot pluggable 0x" RAM_ADDR_FMT,
+                   existing_dimms_capacity,
+                   machine->maxram_size - machine->ram_size);
         goto out;
     }
 
Signature · 1 line
2.1.0
Reviewed-by on v0 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH v0 1/2] pc: Fix DIMMs capacity calculation

Quoted 5 lines · On Mon, 12 Jan 2015 09:32:33 +0530 Bharata B Rao <[email protected]>…

pc_existing_dimms_capacity() is returning DIMMs count rather than capacity. Fix this to return the capacity. Also consider only realized devices for capacity calculation.

Quoted 61 lines
---
 hw/i386/pc.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
hw/i386/pc.c +10 -16
index e07f1fa..125cf0a 100644
@@ -1552,25 +1552,18 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_dimm_count(Object *obj, void *opaque)
-{
-    int *count = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        (*count)++;
-    }
-
-    object_child_foreach(obj, pc_dimm_count, opaque);
-    return 0;
-}
-
 static int pc_existing_dimms_capacity(Object *obj, void *opaque)
 {
     Error *local_err = NULL;
     uint64_t *size = opaque;
 
     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP, &local_err);
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
 
         if (local_err) {
             qerror_report_err(local_err);
@@ -1579,7 +1572,7 @@ static int pc_existing_dimms_capacity(Object *obj, void *opaque)
         }
     }
 
-    object_child_foreach(obj, pc_dimm_count, opaque);
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
     return 0;
 }
 
@@ -1623,8 +1616,9 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
     if (existing_dimms_capacity + memory_region_size(mr) >
         machine->maxram_size - machine->ram_size) {
         error_setg(&local_err, "not enough space, currently 0x%" PRIx64
-                   " in use of total 0x" RAM_ADDR_FMT,
-                   existing_dimms_capacity, machine->maxram_size);
+                   " in use of total hot pluggable 0x" RAM_ADDR_FMT,
+                   existing_dimms_capacity,
+                   machine->maxram_size - machine->ram_size);
         goto out;
     }
 
#3 lore raw

PATCH 2/2 pc-dimm: Make pc_existing_dimms_capacity global

In reply to Bharata B Rao

Move pc_existing_dimms_capacity() to pc-dimm.c since it would be needed by PowerPC memory hotplug code too.

---
 hw/i386/pc.c             | 24 ------------------------
 hw/mem/pc-dimm.c         | 25 +++++++++++++++++++++++++
 include/hw/mem/pc-dimm.h |  1 +
 3 files changed, 26 insertions(+), 24 deletions(-)
hw/i386/pc.c +0 -24
index 125cf0a..2ec45a4 100644
@@ -1552,30 +1552,6 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_existing_dimms_capacity(Object *obj, void *opaque)
-{
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        DeviceState *dev = DEVICE(obj);
-
-        if (dev->realized) {
-            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
-        }
-
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
-            return 1;
-        }
-    }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
-    return 0;
-}
-
 static void pc_dimm_plug(HotplugHandler *hotplug_dev,
                          DeviceState *dev, Error **errp)
 {
hw/mem/pc-dimm.c +25 -0
index d431834..f02ce6e 100644
@@ -22,6 +22,31 @@
 #include "qemu/config-file.h"
 #include "qapi/visitor.h"
 #include "qemu/range.h"
+#include "qapi/qmp/qerror.h"
+
+int pc_existing_dimms_capacity(Object *obj, void *opaque)
+{
+    Error *local_err = NULL;
+    uint64_t *size = opaque;
+
+    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
+
+        if (local_err) {
+            qerror_report_err(local_err);
+            error_free(local_err);
+            return 1;
+        }
+    }
+
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    return 0;
+}
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
 {
include/hw/mem/pc-dimm.h +1 -0
index e1dcbbc..bbfa53f 100644
@@ -78,4 +78,5 @@ uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque);
+int pc_existing_dimms_capacity(Object *obj, void *opaque);
 #endif
Signature · 1 line
2.1.0
#4 lore raw

Re: [Qemu-devel] [PATCH v0 2/2] pc-dimm: Make pc_existing_dimms_capacity global

Quoted 50 lines · On Mon, 12 Jan 2015 09:32:34 +0530 Bharata B Rao <[email protected]>…

Move pc_existing_dimms_capacity() to pc-dimm.c since it would be needed by PowerPC memory hotplug code too.

---
 hw/i386/pc.c             | 24 ------------------------
 hw/mem/pc-dimm.c         | 25 +++++++++++++++++++++++++
 include/hw/mem/pc-dimm.h |  1 +
 3 files changed, 26 insertions(+), 24 deletions(-)
hw/i386/pc.c +0 -24
index 125cf0a..2ec45a4 100644
@@ -1552,30 +1552,6 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_existing_dimms_capacity(Object *obj, void *opaque)
-{
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        DeviceState *dev = DEVICE(obj);
-
-        if (dev->realized) {
-            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
-        }
-
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
-            return 1;
-        }
-    }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
-    return 0;
-}
-
 static void pc_dimm_plug(HotplugHandler *hotplug_dev,
                          DeviceState *dev, Error **errp)
 {
hw/mem/pc-dimm.c +0 -0
index d431834..f02ce6e 100644

hw/mem/pc-dimm.c line 28

Quoted 7 lines
hw/mem/pc-dimm.c +4 -0
@@ -22,6 +22,31 @@
 #include "qemu/config-file.h"
 #include "qapi/visitor.h"
 #include "qemu/range.h"
+#include "qapi/qmp/qerror.h"
+
+int pc_existing_dimms_capacity(Object *obj, void *opaque)
+{

since you are making it API, could you pass Error **errp argument and deal with error in caller?
Along with it you can make function return void.

Quoted 34 lines
hw/mem/pc-dimm.c +21 -0
+    Error *local_err = NULL;
+    uint64_t *size = opaque;
+
+    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
+
+        if (local_err) {
+            qerror_report_err(local_err);
+            error_free(local_err);
+            return 1;
+        }
+    }
+
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    return 0;
+}
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
 {
include/hw/mem/pc-dimm.h +1 -0
index e1dcbbc..bbfa53f 100644
@@ -78,4 +78,5 @@ uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque);
+int pc_existing_dimms_capacity(Object *obj, void *opaque);
 #endif
#5 lore raw

Re: [Qemu-devel] [PATCH v0 2/2] pc-dimm: Make pc_existing_dimms_capacity global

Quoted 55 lines · Igor Mammedov
Quoted 52 lines · On Mon, 12 Jan 2015 09:32:34 +0530 Bharata B Rao <[email protected]>…

Move pc_existing_dimms_capacity() to pc-dimm.c since it would be needed by PowerPC memory hotplug code too.

---
 hw/i386/pc.c             | 24 ------------------------
 hw/mem/pc-dimm.c         | 25 +++++++++++++++++++++++++
 include/hw/mem/pc-dimm.h |  1 +
 3 files changed, 26 insertions(+), 24 deletions(-)
hw/i386/pc.c +0 -24
index 125cf0a..2ec45a4 100644
@@ -1552,30 +1552,6 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_existing_dimms_capacity(Object *obj, void *opaque)
-{
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        DeviceState *dev = DEVICE(obj);
-
-        if (dev->realized) {
-            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
-        }
-
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
-            return 1;
-        }
-    }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
-    return 0;
-}
-
 static void pc_dimm_plug(HotplugHandler *hotplug_dev,
                          DeviceState *dev, Error **errp)
 {
hw/mem/pc-dimm.c +0 -0
index d431834..f02ce6e 100644
@@ -22,6 +22,31 @@
 #include "qemu/config-file.h"
 #include "qapi/visitor.h"
Quoted 8 lines

hw/mem/pc-dimm.c line 28

Quoted 5 lines
hw/mem/pc-dimm.c +4 -0
 #include "qemu/range.h"
+#include "qapi/qmp/qerror.h"
+
+int pc_existing_dimms_capacity(Object *obj, void *opaque)
+{

since you are making it API, could you pass Error **errp argument and deal with error in caller?
Along with it you can make function return void.

I don't think that can be done because pc_existing_dimms_capacity() calls itself recursively via object_child_foreach() and hence its signature if fixed.

Quoted 22 lines
Quoted 22 lines
hw/mem/pc-dimm.c +21 -0
+    Error *local_err = NULL;
+    uint64_t *size = opaque;
+
+    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
+
+        if (local_err) {
+            qerror_report_err(local_err);
+            error_free(local_err);
+            return 1;
+        }
+    }
+
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    return 0;
+}
#6 lore raw

Re: [Qemu-devel] [PATCH v0 2/2] pc-dimm: Make pc_existing_dimms_capacity global

Quoted 65 lines · On Wed, 21 Jan 2015 14:28:18 +0530 Bharata B Rao <[email protected]>…
Quoted 55 lines · Igor Mammedov
Quoted 52 lines · On Mon, 12 Jan 2015 09:32:34 +0530 Bharata B Rao <[email protected]>…

Move pc_existing_dimms_capacity() to pc-dimm.c since it would be needed by PowerPC memory hotplug code too.

---
 hw/i386/pc.c             | 24 ------------------------
 hw/mem/pc-dimm.c         | 25 +++++++++++++++++++++++++
 include/hw/mem/pc-dimm.h |  1 +
 3 files changed, 26 insertions(+), 24 deletions(-)
hw/i386/pc.c +0 -24
index 125cf0a..2ec45a4 100644
@@ -1552,30 +1552,6 @@ void qemu_register_pc_machine(QEMUMachine *m)
     g_free(name);
 }
 
-static int pc_existing_dimms_capacity(Object *obj, void *opaque)
-{
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
-
-    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
-        DeviceState *dev = DEVICE(obj);
-
-        if (dev->realized) {
-            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
-        }
-
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
-            return 1;
-        }
-    }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
-    return 0;
-}
-
 static void pc_dimm_plug(HotplugHandler *hotplug_dev,
                          DeviceState *dev, Error **errp)
 {
hw/mem/pc-dimm.c +0 -0
index d431834..f02ce6e 100644
@@ -22,6 +22,31 @@
 #include "qemu/config-file.h"
 #include "qapi/visitor.h"
Quoted 8 lines

hw/mem/pc-dimm.c line 28

Quoted 5 lines
hw/mem/pc-dimm.c +4 -0
 #include "qemu/range.h"
+#include "qapi/qmp/qerror.h"
+
+int pc_existing_dimms_capacity(Object *obj, void *opaque)
+{

since you are making it API, could you pass Error **errp argument and deal with error in caller?
Along with it you can make function return void.

Quoted 3 lines

I don't think that can be done because pc_existing_dimms_capacity() calls itself recursively via object_child_foreach() and hence its signature if fixed.

how about:

typedef struct somename {
    uint64_t size;
    Error **errp;
} somename;

somename *foo = opaque;

...

and if you'd be able to hide this structure from API caller, then function could return it's existing capacity via its return value. For example:

static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) {
...
}

/* API */
uint64_t pc_existing_dimms_capacity(Error **errp)
{
...
object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
...
}

Quoted 20 lines
Quoted 19 lines
Quoted 19 lines
hw/mem/pc-dimm.c +19 -0
+
+    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
+        DeviceState *dev = DEVICE(obj);
+
+        if (dev->realized) {
+            (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
+                &local_err);
+        }
+
+        if (local_err) {
+            qerror_report_err(local_err);
+            error_free(local_err);
+            return 1;
+        }
+    }
+
+    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    return 0;
+}
#7 lore raw

Re: [Qemu-devel] [PATCH v0 2/2] pc-dimm: Make pc_existing_dimms_capacity global

Quoted 31 lines · Igor Mammedov

On Wed, 21 Jan 2015 14:28:18 +0530

Quoted 9 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+int pc_existing_dimms_capacity(Object *obj, void *opaque)
+{

since you are making it API, could you pass Error **errp argument and deal with error in caller?
Along with it you can make function return void.

I don't think that can be done because pc_existing_dimms_capacity() calls itself recursively via object_child_foreach() and hence its signature if fixed.

how about:

typedef struct somename {
    uint64_t size;
    Error **errp;
} somename;

somename *foo = opaque;

...

and if you'd be able to hide this structure from API caller, then function could return it's existing capacity via its return value. For example:

static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) {
...
}

Quoted 7 lines

/* API */
uint64_t pc_existing_dimms_capacity(Error **errp)
{
...
object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
...
}

If you find the below patch to be fine, I will repost the series with this patch included.

pc-dimm: Add Error argument to pc_existing_dimms_capacity

From: Bharata B Rao <[email protected]>

Now that pc_existing_dimms_capacity() is an API, include Error pointer as an argument and modify the caller appropriately.

---
 hw/i386/pc.c             |    4 ++--
 hw/mem/pc-dimm.c         |   31 ++++++++++++++++++++++---------
 include/hw/mem/pc-dimm.h |    2 +-
 3 files changed, 25 insertions(+), 12 deletions(-)
hw/i386/pc.c +2 -2
index 2ec45a4..c7af6aa 100644
@@ -1584,8 +1584,8 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
         goto out;
     }
 
-    if (pc_existing_dimms_capacity(OBJECT(machine), &existing_dimms_capacity)) {
-        error_setg(&local_err, "failed to get total size of existing DIMMs");
+    existing_dimms_capacity = pc_existing_dimms_capacity(&local_err);
+    if (local_err) {
         goto out;
     }
 
hw/mem/pc-dimm.c +22 -9
index f02ce6e..3e908b3 100644
@@ -24,30 +24,43 @@
 #include "qemu/range.h"
 #include "qapi/qmp/qerror.h"
 
-int pc_existing_dimms_capacity(Object *obj, void *opaque)
+typedef struct pc_dimms_capacity {
+     uint64_t size;
+     Error    **errp;
+} pc_dimms_capacity;
+
+static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque)
 {
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
+    pc_dimms_capacity *cap = opaque;
+    uint64_t *size = &cap->size;
 
     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
         DeviceState *dev = DEVICE(obj);
 
         if (dev->realized) {
             (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
+                cap->errp);
         }
 
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
+        if (cap->errp && *cap->errp) {
             return 1;
         }
     }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
     return 0;
 }
 
+uint64_t pc_existing_dimms_capacity(Error **errp)
+{
+    pc_dimms_capacity cap;
+
+    cap.size = 0;
+    cap.errp = errp;
+
+    pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
+    return cap.size;
+}
+
 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
 {
     MemoryDeviceInfoList ***prev = opaque;
include/hw/mem/pc-dimm.h +1 -1
index bbfa53f..f7b80b4 100644
@@ -78,5 +78,5 @@ uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque);
-int pc_existing_dimms_capacity(Object *obj, void *opaque);
+uint64_t pc_existing_dimms_capacity(Error **errp);
 #endif
#8 lore raw

Re: [Qemu-devel] [PATCH v0 2/2] pc-dimm: Make pc_existing_dimms_capacity global

On Thu, 22 Jan 2015 20:04:46 +0530
Bharata B Rao <[email protected]> wrote:

[...]

Quoted 2 lines

If you find the below patch to be fine, I will repost the series with this patch included.

Looks fine to me

Quoted 99 lines

pc-dimm: Add Error argument to pc_existing_dimms_capacity

From: Bharata B Rao <[email protected]>

Now that pc_existing_dimms_capacity() is an API, include Error pointer as an argument and modify the caller appropriately.

---
 hw/i386/pc.c             |    4 ++--
 hw/mem/pc-dimm.c         |   31 ++++++++++++++++++++++---------
 include/hw/mem/pc-dimm.h |    2 +-
 3 files changed, 25 insertions(+), 12 deletions(-)
hw/i386/pc.c +2 -2
index 2ec45a4..c7af6aa 100644
@@ -1584,8 +1584,8 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
         goto out;
     }
 
-    if (pc_existing_dimms_capacity(OBJECT(machine), &existing_dimms_capacity)) {
-        error_setg(&local_err, "failed to get total size of existing DIMMs");
+    existing_dimms_capacity = pc_existing_dimms_capacity(&local_err);
+    if (local_err) {
         goto out;
     }
 
hw/mem/pc-dimm.c +22 -9
index f02ce6e..3e908b3 100644
@@ -24,30 +24,43 @@
 #include "qemu/range.h"
 #include "qapi/qmp/qerror.h"
 
-int pc_existing_dimms_capacity(Object *obj, void *opaque)
+typedef struct pc_dimms_capacity {
+     uint64_t size;
+     Error    **errp;
+} pc_dimms_capacity;
+
+static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque)
 {
-    Error *local_err = NULL;
-    uint64_t *size = opaque;
+    pc_dimms_capacity *cap = opaque;
+    uint64_t *size = &cap->size;
 
     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
         DeviceState *dev = DEVICE(obj);
 
         if (dev->realized) {
             (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
-                &local_err);
+                cap->errp);
         }
 
-        if (local_err) {
-            qerror_report_err(local_err);
-            error_free(local_err);
+        if (cap->errp && *cap->errp) {
             return 1;
         }
     }
-
-    object_child_foreach(obj, pc_existing_dimms_capacity, opaque);
+    object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
     return 0;
 }
 
+uint64_t pc_existing_dimms_capacity(Error **errp)
+{
+    pc_dimms_capacity cap;
+
+    cap.size = 0;
+    cap.errp = errp;
+
+    pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
+    return cap.size;
+}
+
 int qmp_pc_dimm_device_list(Object *obj, void *opaque)
 {
     MemoryDeviceInfoList ***prev = opaque;
include/hw/mem/pc-dimm.h +1 -1
index bbfa53f..f7b80b4 100644
@@ -78,5 +78,5 @@ uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
 int qmp_pc_dimm_device_list(Object *obj, void *opaque);
-int pc_existing_dimms_capacity(Object *obj, void *opaque);
+uint64_t pc_existing_dimms_capacity(Error **errp);
 #endif
#9 lore raw
In reply to Bharata B Rao
Quoted 7 lines · Bharata B Rao

1st patch fixes PC DIMMs capacity calculation and the 2nd patch does code movement so that pc_existing_dimms_capacity() can be used by other architectures.

Bharata B Rao (2):
pc: Fix DIMMs capacity calculation
pc-dimm: Make pc_existing_dimms_capacity global

Any feedback on this patchset ?

Regards,
Bharata.